1 /*
2 * NET/ROM release 007
3 *
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
5 *
6 * This module:
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.
11 *
12 * History
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.
17 */
18
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/socket.h>
22 #include <linux/in.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>
29 #include <net/ax25.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/skbuff.h>
33 #include <net/sock.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <linux/fcntl.h>
37 #include <linux/mm.h>
38 #include <linux/interrupt.h>
39 #include <net/netrom.h>
40
41 /*
42 * This is where all NET/ROM frames pass, except for IP-over-NET/ROM which
43 * cannot be fragmented in this manner.
44 */
nr_output(struct sock * sk,struct sk_buff * skb)45 void nr_output(struct sock *sk, struct sk_buff *skb)
46 {
47 struct sk_buff *skbn;
48 unsigned char transport[NR_TRANSPORT_LEN];
49 int err, frontlen, len;
50
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);
55
56 frontlen = skb_headroom(skb);
57
58 while (skb->len > 0) {
59 if ((skbn = sock_alloc_send_skb(sk, frontlen + NR_MAX_PACKET_SIZE, 0, &err)) == NULL)
60 return;
61
62 skb_reserve(skbn, frontlen);
63
64 len = (NR_MAX_PACKET_SIZE > skb->len) ? skb->len : NR_MAX_PACKET_SIZE;
65
66 /* Copy the user data */
67 memcpy(skb_put(skbn, len), skb->data, len);
68 skb_pull(skb, len);
69
70 /* Duplicate the Transport Header */
71 skb_push(skbn, NR_TRANSPORT_LEN);
72 memcpy(skbn->data, transport, NR_TRANSPORT_LEN);
73
74 if (skb->len > 0)
75 skbn->data[4] |= NR_MORE_FLAG;
76
77 skb_queue_tail(&sk->write_queue, skbn); /* Throw it on the queue */
78 }
79
80 kfree_skb(skb);
81 } else {
82 skb_queue_tail(&sk->write_queue, skb); /* Throw it on the queue */
83 }
84
85 nr_kick(sk);
86 }
87
88 /*
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.
91 */
nr_send_iframe(struct sock * sk,struct sk_buff * skb)92 static void nr_send_iframe(struct sock *sk, struct sk_buff *skb)
93 {
94 if (skb == NULL)
95 return;
96
97 skb->data[2] = sk->protinfo.nr->vs;
98 skb->data[3] = sk->protinfo.nr->vr;
99
100 if (sk->protinfo.nr->condition & NR_COND_OWN_RX_BUSY)
101 skb->data[4] |= NR_CHOKE_FLAG;
102
103 nr_start_idletimer(sk);
104
105 nr_transmit_buffer(sk, skb);
106 }
107
nr_send_nak_frame(struct sock * sk)108 void nr_send_nak_frame(struct sock *sk)
109 {
110 struct sk_buff *skb, *skbn;
111
112 if ((skb = skb_peek(&sk->protinfo.nr->ack_queue)) == NULL)
113 return;
114
115 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL)
116 return;
117
118 skbn->data[2] = sk->protinfo.nr->va;
119 skbn->data[3] = sk->protinfo.nr->vr;
120
121 if (sk->protinfo.nr->condition & NR_COND_OWN_RX_BUSY)
122 skbn->data[4] |= NR_CHOKE_FLAG;
123
124 nr_transmit_buffer(sk, skbn);
125
126 sk->protinfo.nr->condition &= ~NR_COND_ACK_PENDING;
127 sk->protinfo.nr->vl = sk->protinfo.nr->vr;
128
129 nr_stop_t1timer(sk);
130 }
131
nr_kick(struct sock * sk)132 void nr_kick(struct sock *sk)
133 {
134 struct sk_buff *skb, *skbn;
135 unsigned short start, end;
136
137 if (sk->protinfo.nr->state != NR_STATE_3)
138 return;
139
140 if (sk->protinfo.nr->condition & NR_COND_PEER_RX_BUSY)
141 return;
142
143 if (skb_peek(&sk->write_queue) == NULL)
144 return;
145
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;
148
149 if (start == end)
150 return;
151
152 sk->protinfo.nr->vs = start;
153
154 /*
155 * Transmit data until either we're out of data to send or
156 * the window is full.
157 */
158
159 /*
160 * Dequeue the frame and copy it.
161 */
162 skb = skb_dequeue(&sk->write_queue);
163
164 do {
165 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
166 skb_queue_head(&sk->write_queue, skb);
167 break;
168 }
169
170 skb_set_owner_w(skbn, sk);
171
172 /*
173 * Transmit the frame copy.
174 */
175 nr_send_iframe(sk, skbn);
176
177 sk->protinfo.nr->vs = (sk->protinfo.nr->vs + 1) % NR_MODULUS;
178
179 /*
180 * Requeue the original data frame.
181 */
182 skb_queue_tail(&sk->protinfo.nr->ack_queue, skb);
183
184 } while (sk->protinfo.nr->vs != end && (skb = skb_dequeue(&sk->write_queue)) != NULL);
185
186 sk->protinfo.nr->vl = sk->protinfo.nr->vr;
187 sk->protinfo.nr->condition &= ~NR_COND_ACK_PENDING;
188
189 if (!nr_t1timer_running(sk))
190 nr_start_t1timer(sk);
191 }
192
nr_transmit_buffer(struct sock * sk,struct sk_buff * skb)193 void nr_transmit_buffer(struct sock *sk, struct sk_buff *skb)
194 {
195 unsigned char *dptr;
196
197 /*
198 * Add the protocol byte and network header.
199 */
200 dptr = skb_push(skb, NR_NETWORK_LEN);
201
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;
207
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;
213
214 *dptr++ = sysctl_netrom_network_ttl_initialiser;
215
216 if (!nr_route_frame(skb, NULL)) {
217 kfree_skb(skb);
218 nr_disconnect(sk, ENETUNREACH);
219 }
220 }
221
222 /*
223 * The following routines are taken from page 170 of the 7th ARRL Computer
224 * Networking Conference paper, as is the whole state machine.
225 */
226
nr_establish_data_link(struct sock * sk)227 void nr_establish_data_link(struct sock *sk)
228 {
229 sk->protinfo.nr->condition = 0x00;
230 sk->protinfo.nr->n2count = 0;
231
232 nr_write_internal(sk, NR_CONNREQ);
233
234 nr_stop_t2timer(sk);
235 nr_stop_t4timer(sk);
236 nr_stop_idletimer(sk);
237 nr_start_t1timer(sk);
238 }
239
240 /*
241 * Never send a NAK when we are CHOKEd.
242 */
nr_enquiry_response(struct sock * sk)243 void nr_enquiry_response(struct sock *sk)
244 {
245 int frametype = NR_INFOACK;
246
247 if (sk->protinfo.nr->condition & NR_COND_OWN_RX_BUSY) {
248 frametype |= NR_CHOKE_FLAG;
249 } else {
250 if (skb_peek(&sk->protinfo.nr->reseq_queue) != NULL)
251 frametype |= NR_NAK_FLAG;
252 }
253
254 nr_write_internal(sk, frametype);
255
256 sk->protinfo.nr->vl = sk->protinfo.nr->vr;
257 sk->protinfo.nr->condition &= ~NR_COND_ACK_PENDING;
258 }
259
nr_check_iframes_acked(struct sock * sk,unsigned short nr)260 void nr_check_iframes_acked(struct sock *sk, unsigned short nr)
261 {
262 if (sk->protinfo.nr->vs == nr) {
263 nr_frames_acked(sk, nr);
264 nr_stop_t1timer(sk);
265 sk->protinfo.nr->n2count = 0;
266 } else {
267 if (sk->protinfo.nr->va != nr) {
268 nr_frames_acked(sk, nr);
269 nr_start_t1timer(sk);
270 }
271 }
272 }
273