4 * This code REQUIRES 2.1.15 or higher/ NET3.038
7 * This module is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * NET/ROM 001 Jonathan(G4KLX) Cloned from ax25_out.c
14 * NET/ROM 003 Jonathan(G4KLX) Added NET/ROM fragmentation.
15 * Darryl(G7LED) Fixed NAK, to give out correct reponse.
16 * NET/ROM 007 Jonathan(G4KLX) New timer architecture.
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/skbuff.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <linux/fcntl.h>
38 #include <linux/interrupt.h>
39 #include <net/netrom.h>
42 * This is where all NET/ROM frames pass, except for IP-over-NET/ROM which
43 * cannot be fragmented in this manner.
45 void nr_output(struct sock *sk, struct sk_buff *skb)
48 unsigned char transport[NR_TRANSPORT_LEN];
49 int err, frontlen, len;
51 if (skb->len - NR_TRANSPORT_LEN > NR_MAX_PACKET_SIZE) {
52 /* Save a copy of the Transport Header */
53 memcpy(transport, skb->data, NR_TRANSPORT_LEN);
54 skb_pull(skb, NR_TRANSPORT_LEN);
56 frontlen = skb_headroom(skb);
58 while (skb->len > 0) {
59 if ((skbn = sock_alloc_send_skb(sk, frontlen + NR_MAX_PACKET_SIZE, 0, &err)) == NULL)
62 skb_reserve(skbn, frontlen);
64 len = (NR_MAX_PACKET_SIZE > skb->len) ? skb->len : NR_MAX_PACKET_SIZE;
66 /* Copy the user data */
67 memcpy(skb_put(skbn, len), skb->data, len);
70 /* Duplicate the Transport Header */
71 skb_push(skbn, NR_TRANSPORT_LEN);
72 memcpy(skbn->data, transport, NR_TRANSPORT_LEN);
75 skbn->data[4] |= NR_MORE_FLAG;
77 skb_queue_tail(&sk->write_queue, skbn); /* Throw it on the queue */
82 skb_queue_tail(&sk->write_queue, skb); /* Throw it on the queue */
89 * This procedure is passed a buffer descriptor for an iframe. It builds
90 * the rest of the control part of the frame and then writes it out.
92 static void nr_send_iframe(struct sock *sk, struct sk_buff *skb)
97 skb->data[2] = sk->protinfo.nr->vs;
98 skb->data[3] = sk->protinfo.nr->vr;
100 if (sk->protinfo.nr->condition & NR_COND_OWN_RX_BUSY)
101 skb->data[4] |= NR_CHOKE_FLAG;
103 nr_start_idletimer(sk);
105 nr_transmit_buffer(sk, skb);
108 void nr_send_nak_frame(struct sock *sk)
110 struct sk_buff *skb, *skbn;
112 if ((skb = skb_peek(&sk->protinfo.nr->ack_queue)) == NULL)
115 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL)
118 skbn->data[2] = sk->protinfo.nr->va;
119 skbn->data[3] = sk->protinfo.nr->vr;
121 if (sk->protinfo.nr->condition & NR_COND_OWN_RX_BUSY)
122 skbn->data[4] |= NR_CHOKE_FLAG;
124 nr_transmit_buffer(sk, skbn);
126 sk->protinfo.nr->condition &= ~NR_COND_ACK_PENDING;
127 sk->protinfo.nr->vl = sk->protinfo.nr->vr;
132 void nr_kick(struct sock *sk)
134 struct sk_buff *skb, *skbn;
135 unsigned short start, end;
137 if (sk->protinfo.nr->state != NR_STATE_3)
140 if (sk->protinfo.nr->condition & NR_COND_PEER_RX_BUSY)
143 if (skb_peek(&sk->write_queue) == NULL)
146 start = (skb_peek(&sk->protinfo.nr->ack_queue) == NULL) ? sk->protinfo.nr->va : sk->protinfo.nr->vs;
147 end = (sk->protinfo.nr->va + sk->protinfo.nr->window) % NR_MODULUS;
152 sk->protinfo.nr->vs = start;
155 * Transmit data until either we're out of data to send or
156 * the window is full.
160 * Dequeue the frame and copy it.
162 skb = skb_dequeue(&sk->write_queue);
165 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
166 skb_queue_head(&sk->write_queue, skb);
170 skb_set_owner_w(skbn, sk);
173 * Transmit the frame copy.
175 nr_send_iframe(sk, skbn);
177 sk->protinfo.nr->vs = (sk->protinfo.nr->vs + 1) % NR_MODULUS;
180 * Requeue the original data frame.
182 skb_queue_tail(&sk->protinfo.nr->ack_queue, skb);
184 } while (sk->protinfo.nr->vs != end && (skb = skb_dequeue(&sk->write_queue)) != NULL);
186 sk->protinfo.nr->vl = sk->protinfo.nr->vr;
187 sk->protinfo.nr->condition &= ~NR_COND_ACK_PENDING;
189 if (!nr_t1timer_running(sk))
190 nr_start_t1timer(sk);
193 void nr_transmit_buffer(struct sock *sk, struct sk_buff *skb)
198 * Add the protocol byte and network header.
200 dptr = skb_push(skb, NR_NETWORK_LEN);
202 memcpy(dptr, &sk->protinfo.nr->source_addr, AX25_ADDR_LEN);
203 dptr[6] &= ~AX25_CBIT;
204 dptr[6] &= ~AX25_EBIT;
205 dptr[6] |= AX25_SSSID_SPARE;
206 dptr += AX25_ADDR_LEN;
208 memcpy(dptr, &sk->protinfo.nr->dest_addr, AX25_ADDR_LEN);
209 dptr[6] &= ~AX25_CBIT;
210 dptr[6] |= AX25_EBIT;
211 dptr[6] |= AX25_SSSID_SPARE;
212 dptr += AX25_ADDR_LEN;
214 *dptr++ = sysctl_netrom_network_ttl_initialiser;
216 if (!nr_route_frame(skb, NULL)) {
218 nr_disconnect(sk, ENETUNREACH);
223 * The following routines are taken from page 170 of the 7th ARRL Computer
224 * Networking Conference paper, as is the whole state machine.
227 void nr_establish_data_link(struct sock *sk)
229 sk->protinfo.nr->condition = 0x00;
230 sk->protinfo.nr->n2count = 0;
232 nr_write_internal(sk, NR_CONNREQ);
236 nr_stop_idletimer(sk);
237 nr_start_t1timer(sk);
241 * Never send a NAK when we are CHOKEd.
243 void nr_enquiry_response(struct sock *sk)
245 int frametype = NR_INFOACK;
247 if (sk->protinfo.nr->condition & NR_COND_OWN_RX_BUSY) {
248 frametype |= NR_CHOKE_FLAG;
250 if (skb_peek(&sk->protinfo.nr->reseq_queue) != NULL)
251 frametype |= NR_NAK_FLAG;
254 nr_write_internal(sk, frametype);
256 sk->protinfo.nr->vl = sk->protinfo.nr->vr;
257 sk->protinfo.nr->condition &= ~NR_COND_ACK_PENDING;
260 void nr_check_iframes_acked(struct sock *sk, unsigned short nr)
262 if (sk->protinfo.nr->vs == nr) {
263 nr_frames_acked(sk, nr);
265 sk->protinfo.nr->n2count = 0;
267 if (sk->protinfo.nr->va != nr) {
268 nr_frames_acked(sk, nr);
269 nr_start_t1timer(sk);