This source file includes following definitions.
- ax25_clear_queues
- ax25_frames_acked
- ax25_requeue_frames
- ax25_validate_nr
- ax25_decode
- ax25_send_control
- ax25_return_dm
- ax25_calculate_t1
- ax25_calculate_rtt
- ax25_parse_addr
- build_ax25_addr
- size_ax25_addr
- ax25_digi_invert
- ax25_kiss_cmd
- ax25_dama_on
- ax25_dama_off
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 #include <linux/config.h>
36 #ifdef CONFIG_AX25
37 #include <linux/errno.h>
38 #include <linux/types.h>
39 #include <linux/socket.h>
40 #include <linux/in.h>
41 #include <linux/kernel.h>
42 #include <linux/sched.h>
43 #include <linux/timer.h>
44 #include <linux/string.h>
45 #include <linux/sockios.h>
46 #include <linux/net.h>
47 #include <net/ax25.h>
48 #include <linux/inet.h>
49 #include <linux/netdevice.h>
50 #include <linux/skbuff.h>
51 #include <net/sock.h>
52 #include <asm/segment.h>
53 #include <asm/system.h>
54 #include <linux/fcntl.h>
55 #include <linux/mm.h>
56 #include <linux/interrupt.h>
57
58
59
60
61 void ax25_clear_queues(ax25_cb *ax25)
62 {
63 struct sk_buff *skb;
64
65 while ((skb = skb_dequeue(&ax25->write_queue)) != NULL) {
66 skb->free = 1;
67 kfree_skb(skb, FREE_WRITE);
68 }
69
70 while ((skb = skb_dequeue(&ax25->ack_queue)) != NULL) {
71 skb->free = 1;
72 kfree_skb(skb, FREE_WRITE);
73 }
74
75 while ((skb = skb_dequeue(&ax25->reseq_queue)) != NULL) {
76 kfree_skb(skb, FREE_READ);
77 }
78
79 while ((skb = skb_dequeue(&ax25->frag_queue)) != NULL) {
80 kfree_skb(skb, FREE_READ);
81 }
82 }
83
84
85
86
87
88
89 void ax25_frames_acked(ax25_cb *ax25, unsigned short nr)
90 {
91 struct sk_buff *skb;
92
93
94
95
96 if (ax25->va != nr) {
97 while (skb_peek(&ax25->ack_queue) != NULL && ax25->va != nr) {
98 skb = skb_dequeue(&ax25->ack_queue);
99 skb->free = 1;
100 kfree_skb(skb, FREE_WRITE);
101 ax25->va = (ax25->va + 1) % ax25->modulus;
102 if (ax25->dama_slave)
103 ax25->n2count = 0;
104 }
105 }
106 }
107
108
109
110
111
112 void ax25_requeue_frames(ax25_cb *ax25)
113 {
114 struct sk_buff *skb, *skb_prev = NULL;
115
116
117
118
119
120
121 while ((skb = skb_dequeue(&ax25->ack_queue)) != NULL) {
122 if (skb_prev == NULL)
123 skb_queue_head(&ax25->write_queue, skb);
124 else
125 skb_append(skb_prev, skb);
126 skb_prev = skb;
127 }
128 }
129
130
131
132
133
134 int ax25_validate_nr(ax25_cb *ax25, unsigned short nr)
135 {
136 unsigned short vc = ax25->va;
137
138 while (vc != ax25->vs) {
139 if (nr == vc) return 1;
140 vc = (vc + 1) % ax25->modulus;
141 }
142
143 if (nr == ax25->vs) return 1;
144
145 return 0;
146 }
147
148
149
150
151
152 int ax25_decode(ax25_cb *ax25, struct sk_buff *skb, int *ns, int *nr, int *pf)
153 {
154 unsigned char *frame;
155 int frametype = ILLEGAL;
156
157 frame = skb->data;
158 *ns = *nr = *pf = 0;
159
160 if (ax25->modulus == MODULUS) {
161 if ((frame[0] & S) == 0) {
162 frametype = I;
163 *ns = (frame[0] >> 1) & 0x07;
164 *nr = (frame[0] >> 5) & 0x07;
165 *pf = frame[0] & PF;
166 } else if ((frame[0] & U) == 1) {
167 frametype = frame[0] & 0x0F;
168 *nr = (frame[0] >> 5) & 0x07;
169 *pf = frame[0] & PF;
170 } else if ((frame[0] & U) == 3) {
171 frametype = frame[0] & ~PF;
172 *pf = frame[0] & PF;
173 }
174 skb_pull(skb, 1);
175 } else {
176 if ((frame[0] & S) == 0) {
177 frametype = I;
178 *ns = (frame[0] >> 1) & 0x7F;
179 *nr = (frame[1] >> 1) & 0x7F;
180 *pf = frame[1] & EPF;
181 skb_pull(skb, 2);
182 } else if ((frame[0] & U) == 1) {
183 frametype = frame[0] & 0x0F;
184 *nr = (frame[1] >> 1) & 0x7F;
185 *pf = frame[1] & EPF;
186 skb_pull(skb, 2);
187 } else if ((frame[0] & U) == 3) {
188 frametype = frame[0] & ~PF;
189 *pf = frame[0] & PF;
190 skb_pull(skb, 1);
191 }
192 }
193
194 return frametype;
195 }
196
197
198
199
200
201
202 void ax25_send_control(ax25_cb *ax25, int frametype, int poll_bit, int type)
203 {
204 struct sk_buff *skb;
205 unsigned char *dptr;
206 struct device *dev;
207
208 if ((dev = ax25->device) == NULL)
209 return;
210
211 if ((skb = alloc_skb(AX25_BPQ_HEADER_LEN + size_ax25_addr(ax25->digipeat) + 2, GFP_ATOMIC)) == NULL)
212 return;
213
214 skb_reserve(skb, AX25_BPQ_HEADER_LEN + size_ax25_addr(ax25->digipeat));
215
216 if (ax25->sk != NULL) {
217 skb->sk = ax25->sk;
218 atomic_add(skb->truesize, &ax25->sk->wmem_alloc);
219 }
220
221
222 if (ax25->modulus == MODULUS) {
223 dptr = skb_put(skb, 1);
224 *dptr = frametype;
225 *dptr |= (poll_bit) ? PF : 0;
226 if ((frametype & U) == S)
227 *dptr |= (ax25->vr << 5);
228 } else {
229 if ((frametype & U) == U) {
230 dptr = skb_put(skb, 1);
231 *dptr = frametype;
232 *dptr |= (poll_bit) ? PF : 0;
233 } else {
234 dptr = skb_put(skb, 2);
235 dptr[0] = frametype;
236 dptr[1] = (ax25->vr << 1);
237 dptr[1] |= (poll_bit) ? EPF : 0;
238 }
239 }
240
241 skb->free = 1;
242
243 ax25_transmit_buffer(ax25, skb, type);
244 }
245
246
247
248
249
250
251 void ax25_return_dm(struct device *dev, ax25_address *src, ax25_address *dest, ax25_digi *digi)
252 {
253 struct sk_buff *skb;
254 char *dptr;
255 ax25_digi retdigi;
256
257 if (dev == NULL)
258 return;
259
260 if ((skb = alloc_skb(AX25_BPQ_HEADER_LEN + size_ax25_addr(digi) + 1, GFP_ATOMIC)) == NULL)
261 return;
262
263 skb_reserve(skb, AX25_BPQ_HEADER_LEN + size_ax25_addr(digi));
264
265 ax25_digi_invert(digi, &retdigi);
266
267 dptr = skb_put(skb, 1);
268 skb->sk = NULL;
269
270 *dptr = DM | PF;
271
272
273
274
275
276 dptr = skb_push(skb, size_ax25_addr(digi));
277 dptr += build_ax25_addr(dptr, dest, src, &retdigi, C_RESPONSE, MODULUS);
278
279 skb->arp = 1;
280 skb->free = 1;
281
282 ax25_queue_xmit(skb, dev, SOPRI_NORMAL);
283 }
284
285
286
287
288 unsigned short ax25_calculate_t1(ax25_cb *ax25)
289 {
290 int n, t = 2;
291
292 if (ax25->backoff) {
293 for (n = 0; n < ax25->n2count; n++)
294 t *= 2;
295
296 if (t > 8) t = 8;
297 }
298
299 return t * ax25->rtt;
300 }
301
302
303
304
305 void ax25_calculate_rtt(ax25_cb *ax25)
306 {
307 if (ax25->t1timer > 0 && ax25->n2count == 0)
308 ax25->rtt = (9 * ax25->rtt + ax25->t1 - ax25->t1timer) / 10;
309
310 #ifdef AX25_T1CLAMPLO
311
312 if (ax25->rtt < (AX25_T1CLAMPLO))
313 ax25->rtt = (AX25_T1CLAMPLO);
314 #else
315 if (ax25->rtt == 0)
316 ax25->rtt = PR_SLOWHZ;
317 #endif
318 #ifdef AX25_T1CLAMPHI
319
320 if (ax25->rtt > (AX25_T1CLAMPHI))
321 ax25->rtt = (AX25_T1CLAMPHI);
322 #endif
323 }
324
325
326
327
328
329
330
331
332
333
334 unsigned char *ax25_parse_addr(unsigned char *buf, int len, ax25_address *src, ax25_address *dest, ax25_digi *digi, int *flags, int *dama)
335 {
336 int d = 0;
337
338 if (len < 14) return NULL;
339
340 if (flags != NULL) {
341 *flags = 0;
342
343 if (buf[6] & LAPB_C) {
344 *flags = C_COMMAND;
345 }
346 if (buf[13] & LAPB_C) {
347 *flags = C_RESPONSE;
348 }
349 }
350
351 if (dama != NULL)
352 *dama = ~buf[13] & DAMA_FLAG;
353
354
355 if (dest != NULL)
356 memcpy(dest, buf + 0, AX25_ADDR_LEN);
357 if (src != NULL)
358 memcpy(src, buf + 7, AX25_ADDR_LEN);
359 buf += 2 * AX25_ADDR_LEN;
360 len -= 2 * AX25_ADDR_LEN;
361 digi->lastrepeat = -1;
362 digi->ndigi = 0;
363
364 while (!(buf[-1] & LAPB_E)) {
365 if (d >= AX25_MAX_DIGIS) return NULL;
366 if (len < 7) return NULL;
367
368 if (digi != NULL) {
369 memcpy(&digi->calls[d], buf, AX25_ADDR_LEN);
370 digi->ndigi = d + 1;
371 if (buf[6] & AX25_REPEATED) {
372 digi->repeated[d] = 1;
373 digi->lastrepeat = d;
374 } else {
375 digi->repeated[d] = 0;
376 }
377 }
378
379 buf += AX25_ADDR_LEN;
380 len -= AX25_ADDR_LEN;
381 d++;
382 }
383
384 return buf;
385 }
386
387
388
389
390 int build_ax25_addr(unsigned char *buf, ax25_address *src, ax25_address *dest, ax25_digi *d, int flag, int modulus)
391 {
392 int len = 0;
393 int ct = 0;
394
395 memcpy(buf, dest, AX25_ADDR_LEN);
396 buf[6] &= ~(LAPB_E | LAPB_C);
397 buf[6] |= SSSID_SPARE;
398
399 if (flag == C_COMMAND) buf[6] |= LAPB_C;
400
401 buf += AX25_ADDR_LEN;
402 len += AX25_ADDR_LEN;
403
404 memcpy(buf, src, AX25_ADDR_LEN);
405 buf[6] &= ~(LAPB_E | LAPB_C);
406 buf[6] &= ~SSSID_SPARE;
407
408 if (modulus == MODULUS) {
409 buf[6] |= SSSID_SPARE;
410 } else {
411 buf[6] |= ESSID_SPARE;
412 }
413
414 if (flag == C_RESPONSE) buf[6] |= LAPB_C;
415
416
417
418
419 if (d == NULL || d->ndigi == 0) {
420 buf[6] |= LAPB_E;
421 return 2 * AX25_ADDR_LEN;
422 }
423
424 buf += AX25_ADDR_LEN;
425 len += AX25_ADDR_LEN;
426
427 while (ct < d->ndigi) {
428 memcpy(buf, &d->calls[ct], AX25_ADDR_LEN);
429 if (d->repeated[ct])
430 buf[6] |= AX25_REPEATED;
431 else
432 buf[6] &= ~AX25_REPEATED;
433 buf[6] &= ~LAPB_E;
434 buf[6] |= SSSID_SPARE;
435
436 buf += AX25_ADDR_LEN;
437 len += AX25_ADDR_LEN;
438 ct++;
439 }
440
441 buf[-1] |= LAPB_E;
442
443 return len;
444 }
445
446 int size_ax25_addr(ax25_digi *dp)
447 {
448 if (dp == NULL)
449 return 2 * AX25_ADDR_LEN;
450
451 return AX25_ADDR_LEN * (2 + dp->ndigi);
452 }
453
454
455
456
457 void ax25_digi_invert(ax25_digi *in, ax25_digi *out)
458 {
459 int ct = 0;
460
461
462
463 while (ct < in->ndigi) {
464 out->calls[ct] = in->calls[in->ndigi - ct - 1];
465 out->repeated[ct] = 0;
466 ct++;
467 }
468
469
470 out->ndigi = in->ndigi;
471
472
473 out->lastrepeat = 0;
474 }
475
476
477
478
479
480
481
482
483
484
485 void ax25_kiss_cmd(ax25_cb *ax25, unsigned char cmd, unsigned char param)
486 {
487 struct sk_buff *skb;
488 unsigned char *p;
489
490 if (ax25->device == NULL)
491 return;
492
493 if ((skb = alloc_skb(2, GFP_ATOMIC)) == NULL)
494 return;
495
496 skb->free = 1;
497 skb->arp = 1;
498
499 if (ax25->sk != NULL) {
500 skb->sk = ax25->sk;
501 atomic_add(skb->truesize, &ax25->sk->wmem_alloc);
502 }
503
504 skb->protocol = htons(ETH_P_AX25);
505
506 p = skb_put(skb, 2);
507
508 *p++=cmd;
509 *p =param;
510
511 dev_queue_xmit(skb, ax25->device, SOPRI_NORMAL);
512 }
513
514 void ax25_dama_on(ax25_cb *ax25)
515 {
516 if (ax25_dev_is_dama_slave(ax25->device) == 0) {
517 if (ax25->sk != NULL && ax25->sk->debug)
518 printk("ax25_dama_on: DAMA on\n");
519 ax25_kiss_cmd(ax25, 5, 1);
520 }
521 }
522
523 void ax25_dama_off(ax25_cb *ax25)
524 {
525 if (ax25->dama_slave == 0)
526 return;
527
528 ax25->dama_slave = 0;
529 if (ax25_dev_is_dama_slave(ax25->device) == 0) {
530 if (ax25->sk != NULL && ax25->sk->debug)
531 printk("ax25_dama_off: DAMA off\n");
532 ax25_kiss_cmd(ax25, 5, 0);
533 }
534 }
535
536 #endif