1 /*
2 * AX.25 release 037
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 * Most of this code is based on the SDL diagrams published in the 7th
13 * ARRL Computer Networking Conference papers. The diagrams have mistakes
14 * in them, but are mostly correct. Before you modify the code could you
15 * read the SDL diagrams as the code is not obvious and probably very
16 * easy to break;
17 *
18 * History
19 * AX.25 029 Alan(GW4PTS) Switched to KA9Q constant names. Removed
20 * old BSD code.
21 * AX.25 030 Jonathan(G4KLX) Added support for extended AX.25.
22 * Added fragmentation support.
23 * Darryl(G7LED) Added function ax25_requeue_frames() to split
24 * it up from ax25_frames_acked().
25 * AX.25 031 Joerg(DL1BKE) DAMA needs KISS Fullduplex ON/OFF.
26 * Thus we have ax25_kiss_cmd() now... ;-)
27 * Dave Brown(N2RJT)
28 * Killed a silly bug in the DAMA code.
29 * Joerg(DL1BKE) Found the real bug in ax25.h, sri.
30 * AX.25 032 Joerg(DL1BKE) Added ax25_queue_length to count the number of
31 * enqueued buffers of a socket..
32 * AX.25 035 Frederic(F1OAT) Support for pseudo-digipeating.
33 * AX.25 037 Jonathan(G4KLX) New timer architecture.
34 */
35
36 #include <linux/errno.h>
37 #include <linux/types.h>
38 #include <linux/socket.h>
39 #include <linux/in.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/timer.h>
43 #include <linux/string.h>
44 #include <linux/sockios.h>
45 #include <linux/net.h>
46 #include <net/ax25.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/skbuff.h>
50 #include <net/sock.h>
51 #include <asm/uaccess.h>
52 #include <asm/system.h>
53 #include <linux/fcntl.h>
54 #include <linux/mm.h>
55 #include <linux/interrupt.h>
56
57 /*
58 * This routine purges all the queues of frames.
59 */
60 void ax25_clear_queues(ax25_cb *ax25)
61 {
62 struct sk_buff *skb;
63
64 while ((skb = skb_dequeue(&ax25->write_queue)) != NULL)
65 kfree_skb(skb);
66
67 while ((skb = skb_dequeue(&ax25->ack_queue)) != NULL)
68 kfree_skb(skb);
69
70 while ((skb = skb_dequeue(&ax25->reseq_queue)) != NULL)
71 kfree_skb(skb);
72
73 while ((skb = skb_dequeue(&ax25->frag_queue)) != NULL)
74 kfree_skb(skb);
75 }
76
77 /*
78 * This routine purges the input queue of those frames that have been
79 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
80 * SDL diagram.
81 */
82 void ax25_frames_acked(ax25_cb *ax25, unsigned short nr)
83 {
84 struct sk_buff *skb;
85
86 /*
87 * Remove all the ack-ed frames from the ack queue.
88 */
89 if (ax25->va != nr) {
90 while (skb_peek(&ax25->ack_queue) != NULL && ax25->va != nr) {
91 skb = skb_dequeue(&ax25->ack_queue);
92 kfree_skb(skb);
93 ax25->va = (ax25->va + 1) % ax25->modulus;
94 }
95 }
96 }
97
98 void ax25_requeue_frames(ax25_cb *ax25)
99 {
100 struct sk_buff *skb, *skb_prev = NULL;
101
102 /*
103 * Requeue all the un-ack-ed frames on the output queue to be picked
104 * up by ax25_kick called from the timer. This arrangement handles the
105 * possibility of an empty output queue.
106 */
107 while ((skb = skb_dequeue(&ax25->ack_queue)) != NULL) {
108 if (skb_prev == NULL)
109 skb_queue_head(&ax25->write_queue, skb);
110 else
111 skb_append(skb_prev, skb);
112 skb_prev = skb;
113 }
114 }
115
116 /*
117 * Validate that the value of nr is between va and vs. Return true or
118 * false for testing.
119 */
120 int ax25_validate_nr(ax25_cb *ax25, unsigned short nr)
121 {
122 unsigned short vc = ax25->va;
123
124 while (vc != ax25->vs) {
125 if (nr == vc) return 1;
126 vc = (vc + 1) % ax25->modulus;
127 }
128
129 if (nr == ax25->vs) return 1;
130
131 return 0;
132 }
133
134 /*
135 * This routine is the centralised routine for parsing the control
136 * information for the different frame formats.
137 */
138 int ax25_decode(ax25_cb *ax25, struct sk_buff *skb, int *ns, int *nr, int *pf)
139 {
140 unsigned char *frame;
141 int frametype = AX25_ILLEGAL;
142
143 frame = skb->data;
144 *ns = *nr = *pf = 0;
145
146 if (ax25->modulus == AX25_MODULUS) {
147 if ((frame[0] & AX25_S) == 0) {
148 frametype = AX25_I; /* I frame - carries NR/NS/PF */
149 *ns = (frame[0] >> 1) & 0x07;
150 *nr = (frame[0] >> 5) & 0x07;
151 *pf = frame[0] & AX25_PF;
152 } else if ((frame[0] & AX25_U) == 1) { /* S frame - take out PF/NR */
153 frametype = frame[0] & 0x0F;
154 *nr = (frame[0] >> 5) & 0x07;
155 *pf = frame[0] & AX25_PF;
156 } else if ((frame[0] & AX25_U) == 3) { /* U frame - take out PF */
157 frametype = frame[0] & ~AX25_PF;
158 *pf = frame[0] & AX25_PF;
159 }
160 skb_pull(skb, 1);
161 } else {
162 if ((frame[0] & AX25_S) == 0) {
163 frametype = AX25_I; /* I frame - carries NR/NS/PF */
164 *ns = (frame[0] >> 1) & 0x7F;
165 *nr = (frame[1] >> 1) & 0x7F;
166 *pf = frame[1] & AX25_EPF;
167 skb_pull(skb, 2);
168 } else if ((frame[0] & AX25_U) == 1) { /* S frame - take out PF/NR */
169 frametype = frame[0] & 0x0F;
170 *nr = (frame[1] >> 1) & 0x7F;
171 *pf = frame[1] & AX25_EPF;
172 skb_pull(skb, 2);
173 } else if ((frame[0] & AX25_U) == 3) { /* U frame - take out PF */
174 frametype = frame[0] & ~AX25_PF;
175 *pf = frame[0] & AX25_PF;
176 skb_pull(skb, 1);
177 }
178 }
179
180 return frametype;
181 }
182
183 /*
184 * This routine is called when the HDLC layer internally generates a
185 * command or response for the remote machine ( eg. RR, UA etc. ).
186 * Only supervisory or unnumbered frames are processed.
187 */
188 void ax25_send_control(ax25_cb *ax25, int frametype, int poll_bit, int type)
189 {
190 struct sk_buff *skb;
191 unsigned char *dptr;
192
193 if ((skb = alloc_skb(AX25_BPQ_HEADER_LEN + ax25_addr_size(ax25->digipeat) + 2, GFP_ATOMIC)) == NULL)
194 return;
195
196 skb_reserve(skb, AX25_BPQ_HEADER_LEN + ax25_addr_size(ax25->digipeat));
197
198 skb->nh.raw = skb->data;
199
200 /* Assume a response - address structure for DTE */
201 if (ax25->modulus == AX25_MODULUS) {
202 dptr = skb_put(skb, 1);
203 *dptr = frametype;
204 *dptr |= (poll_bit) ? AX25_PF : 0;
205 if ((frametype & AX25_U) == AX25_S) /* S frames carry NR */
206 *dptr |= (ax25->vr << 5);
207 } else {
208 if ((frametype & AX25_U) == AX25_U) {
209 dptr = skb_put(skb, 1);
210 *dptr = frametype;
211 *dptr |= (poll_bit) ? AX25_PF : 0;
212 } else {
213 dptr = skb_put(skb, 2);
214 dptr[0] = frametype;
215 dptr[1] = (ax25->vr << 1);
216 dptr[1] |= (poll_bit) ? AX25_EPF : 0;
217 }
218 }
219
220 ax25_transmit_buffer(ax25, skb, type);
221 }
222
223 /*
224 * Send a 'DM' to an unknown connection attempt, or an invalid caller.
225 *
226 * Note: src here is the sender, thus it's the target of the DM
227 */
228 void ax25_return_dm(struct net_device *dev, ax25_address *src, ax25_address *dest, ax25_digi *digi)
229 {
230 struct sk_buff *skb;
231 char *dptr;
232 ax25_digi retdigi;
233
234 if (dev == NULL)
235 return;
236
237 if ((skb = alloc_skb(AX25_BPQ_HEADER_LEN + ax25_addr_size(digi) + 1, GFP_ATOMIC)) == NULL)
238 return; /* Next SABM will get DM'd */
239
240 skb_reserve(skb, AX25_BPQ_HEADER_LEN + ax25_addr_size(digi));
241 skb->nh.raw = skb->data;
242
243 ax25_digi_invert(digi, &retdigi);
244
245 dptr = skb_put(skb, 1);
246
247 *dptr = AX25_DM | AX25_PF;
248
249 /*
250 * Do the address ourselves
251 */
252 dptr = skb_push(skb, ax25_addr_size(digi));
253 dptr += ax25_addr_build(dptr, dest, src, &retdigi, AX25_RESPONSE, AX25_MODULUS);
254
255 skb->dev = dev;
256
257 ax25_queue_xmit(skb);
258 }
259
260 /*
261 * Exponential backoff for AX.25
262 */
263 void ax25_calculate_t1(ax25_cb *ax25)
264 {
265 int n, t = 2;
266
267 switch (ax25->backoff) {
268 case 0:
269 break;
270
271 case 1:
272 t += 2 * ax25->n2count;
273 break;
274
275 case 2:
276 for (n = 0; n < ax25->n2count; n++)
277 t *= 2;
278 if (t > 8) t = 8;
279 break;
280 }
281
282 ax25->t1 = t * ax25->rtt;
283 }
284
285 /*
286 * Calculate the Round Trip Time
287 */
288 void ax25_calculate_rtt(ax25_cb *ax25)
289 {
290 if (ax25->backoff == 0)
291 return;
292
293 if (ax25_t1timer_running(ax25) && ax25->n2count == 0)
294 ax25->rtt = (9 * ax25->rtt + ax25->t1 - ax25_display_timer(&ax25->t1timer)) / 10;
295
296 if (ax25->rtt < AX25_T1CLAMPLO)
297 ax25->rtt = AX25_T1CLAMPLO;
298
299 if (ax25->rtt > AX25_T1CLAMPHI)
300 ax25->rtt = AX25_T1CLAMPHI;
301 }
302
303 void ax25_disconnect(ax25_cb *ax25, int reason)
304 {
305 ax25_clear_queues(ax25);
306
307 ax25_stop_t1timer(ax25);
308 ax25_stop_t2timer(ax25);
309 ax25_stop_t3timer(ax25);
310 ax25_stop_idletimer(ax25);
311
312 ax25->state = AX25_STATE_0;
313
314 ax25_link_failed(ax25, reason);
315
316 if (ax25->sk != NULL) {
317 ax25->sk->state = TCP_CLOSE;
318 ax25->sk->err = reason;
319 ax25->sk->shutdown |= SEND_SHUTDOWN;
320 if (!ax25->sk->dead)
321 ax25->sk->state_change(ax25->sk);
322 ax25->sk->dead = 1;
323 }
324 }
325
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.