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 #include <linux/config.h>
35 #ifdef CONFIG_AX25
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/segment.h>
52 #include <asm/system.h>
53 #include <linux/fcntl.h>
54 #include <linux/mm.h>
55 #include <linux/interrupt.h>
56
57
58
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 skb->free = 1;
66 kfree_skb(skb, FREE_WRITE);
67 }
68
69 while ((skb = skb_dequeue(&ax25->ack_queue)) != NULL) {
70 skb->free = 1;
71 kfree_skb(skb, FREE_WRITE);
72 }
73
74 while ((skb = skb_dequeue(&ax25->reseq_queue)) != NULL) {
75 kfree_skb(skb, FREE_READ);
76 }
77
78 while ((skb = skb_dequeue(&ax25->frag_queue)) != NULL) {
79 kfree_skb(skb, FREE_READ);
80 }
81 }
82
83
84
85
86
87
88 void ax25_frames_acked(ax25_cb *ax25, unsigned short nr)
89 {
90 struct sk_buff *skb;
91
92
93
94
95 if (ax25->va != nr) {
96 while (skb_peek(&ax25->ack_queue) != NULL && ax25->va != nr) {
97 skb = skb_dequeue(&ax25->ack_queue);
98 skb->free = 1;
99 kfree_skb(skb, FREE_WRITE);
100 ax25->va = (ax25->va + 1) % ax25->modulus;
101 if (ax25->dama_slave)
102 ax25->n2count = 0;
103 }
104 }
105 }
106
107
108
109
110
111 void ax25_requeue_frames(ax25_cb *ax25)
112 {
113 struct sk_buff *skb, *skb_prev = NULL;
114
115
116
117
118
119
120 while ((skb = skb_dequeue(&ax25->ack_queue)) != NULL) {
121 if (skb_prev == NULL)
122 skb_queue_head(&ax25->write_queue, skb);
123 else
124 skb_append(skb_prev, skb);
125 skb_prev = skb;
126 }
127 }
128
129
130
131
132
133 int ax25_validate_nr(ax25_cb *ax25, unsigned short nr)
134 {
135 unsigned short vc = ax25->va;
136
137 while (vc != ax25->vs) {
138 if (nr == vc) return 1;
139 vc = (vc + 1) % ax25->modulus;
140 }
141
142 if (nr == ax25->vs) return 1;
143
144 return 0;
145 }
146
147
148
149
150
151 int ax25_decode(ax25_cb *ax25, struct sk_buff *skb, int *ns, int *nr, int *pf)
152 {
153 unsigned char *frame;
154 int frametype = ILLEGAL;
155
156 frame = skb->data;
157 *ns = *nr = *pf = 0;
158
159 if (ax25->modulus == MODULUS) {
160 if ((frame[0] & S) == 0) {
161 frametype = I;
162 *ns = (frame[0] >> 1) & 0x07;
163 *nr = (frame[0] >> 5) & 0x07;
164 *pf = frame[0] & PF;
165 } else if ((frame[0] & U) == 1) {
166 frametype = frame[0] & 0x0F;
167 *nr = (frame[0] >> 5) & 0x07;
168 *pf = frame[0] & PF;
169 } else if ((frame[0] & U) == 3) {
170 frametype = frame[0] & ~PF;
171 *pf = frame[0] & PF;
172 }
173 skb_pull(skb, 1);
174 } else {
175 if ((frame[0] & S) == 0) {
176 frametype = I;
177 *ns = (frame[0] >> 1) & 0x7F;
178 *nr = (frame[1] >> 1) & 0x7F;
179 *pf = frame[1] & EPF;
180 skb_pull(skb, 2);
181 } else if ((frame[0] & U) == 1) {
182 frametype = frame[0] & 0x0F;
183 *nr = (frame[1] >> 1) & 0x7F;
184 *pf = frame[1] & EPF;
185 skb_pull(skb, 2);
186 } else if ((frame[0] & U) == 3) {
187 frametype = frame[0] & ~PF;
188 *pf = frame[0] & PF;
189 skb_pull(skb, 1);
190 }
191 }
192
193 return frametype;
194 }
195
196
197
198
199
200
201 void ax25_send_control(ax25_cb *ax25, int frametype, int poll_bit, int type)
202 {
203 struct sk_buff *skb;
204 unsigned char *dptr;
205 struct device *dev;
206
207 if ((dev = ax25->device) == NULL)
208 return;
209
210 if ((skb = alloc_skb(AX25_BPQ_HEADER_LEN + size_ax25_addr(ax25->digipeat) + 2, GFP_ATOMIC)) == NULL)
211 return;
212
213 skb_reserve(skb, AX25_BPQ_HEADER_LEN + size_ax25_addr(ax25->digipeat));
214
215 if (ax25->sk != NULL) {
216 skb->sk = ax25->sk;
217 ax25->sk->wmem_alloc += skb->truesize;
218 }
219
220
221 if (ax25->modulus == MODULUS) {
222 dptr = skb_put(skb, 1);
223 *dptr = frametype;
224 *dptr |= (poll_bit) ? PF : 0;
225 if ((frametype & U) == S)
226 *dptr |= (ax25->vr << 5);
227 } else {
228 if ((frametype & U) == U) {
229 dptr = skb_put(skb, 1);
230 *dptr = frametype;
231 *dptr |= (poll_bit) ? PF : 0;
232 } else {
233 dptr = skb_put(skb, 2);
234 dptr[0] = frametype;
235 dptr[1] = (ax25->vr << 1);
236 dptr[1] |= (poll_bit) ? EPF : 0;
237 }
238 }
239
240 skb->free = 1;
241
242 ax25_transmit_buffer(ax25, skb, type);
243 }
244
245
246
247
248
249
250 void ax25_return_dm(struct device *dev, ax25_address *src, ax25_address *dest, ax25_digi *digi)
251 {
252 struct sk_buff *skb;
253 char *dptr;
254 ax25_digi retdigi;
255
256 if (dev == NULL)
257 return;
258
259 if ((skb = alloc_skb(AX25_BPQ_HEADER_LEN + size_ax25_addr(digi) + 1, GFP_ATOMIC)) == NULL)
260 return;
261
262 skb_reserve(skb, AX25_BPQ_HEADER_LEN + size_ax25_addr(digi));
263
264 ax25_digi_invert(digi, &retdigi);
265
266 dptr = skb_put(skb, 1);
267 skb->sk = NULL;
268
269 *dptr = DM | PF;
270
271
272
273
274
275 dptr = skb_push(skb, size_ax25_addr(digi));
276 dptr += build_ax25_addr(dptr, dest, src, &retdigi, C_RESPONSE, MODULUS);
277
278 skb->arp = 1;
279 skb->free = 1;
280
281 ax25_queue_xmit(skb, dev, SOPRI_NORMAL);
282 }
283
284
285
286
287 unsigned short ax25_calculate_t1(ax25_cb *ax25)
288 {
289 int n, t = 2;
290
291 if (ax25->backoff) {
292 for (n = 0; n < ax25->n2count; n++)
293 t *= 2;
294
295 if (t > 8) t = 8;
296 }
297
298 return t * ax25->rtt;
299 }
300
301
302
303
304 void ax25_calculate_rtt(ax25_cb *ax25)
305 {
306 if (ax25->t1timer > 0 && ax25->n2count == 0)
307 ax25->rtt = (9 * ax25->rtt + ax25->t1 - ax25->t1timer) / 10;
308
309 #ifdef AX25_T1CLAMPLO
310
311 if (ax25->rtt < (AX25_T1CLAMPLO))
312 ax25->rtt = (AX25_T1CLAMPLO);
313 #else
314 if (ax25->rtt == 0)
315 ax25->rtt = PR_SLOWHZ;
316 #endif
317 #ifdef AX25_T1CLAMPHI
318
319 if (ax25->rtt > (AX25_T1CLAMPHI))
320 ax25->rtt = (AX25_T1CLAMPHI);
321 #endif
322 }
323
324
325
326
327
328
329
330
331
332
333 unsigned char *ax25_parse_addr(unsigned char *buf, int len, ax25_address *src, ax25_address *dest, ax25_digi *digi, int *flags, int *dama)
334 {
335 int d = 0;
336
337 if (len < 14) return NULL;
338
339 if (flags != NULL) {
340 *flags = 0;
341
342 if (buf[6] & LAPB_C) {
343 *flags = C_COMMAND;
344 }
345 if (buf[13] & LAPB_C) {
346 *flags = C_RESPONSE;
347 }
348 }
349
350 if (dama != NULL)
351 *dama = !(buf[13] & DAMA_FLAG);
352
353
354 if (dest != NULL)
355 memcpy(dest, buf + 0, AX25_ADDR_LEN);
356 if (src != NULL)
357 memcpy(src, buf + 7, AX25_ADDR_LEN);
358 buf += 2 * AX25_ADDR_LEN;
359 len -= 2 * AX25_ADDR_LEN;
360 digi->lastrepeat = -1;
361 digi->ndigi = 0;
362
363 while (!(buf[-1] & LAPB_E))
364 {
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 void ax25_kiss_cmd(ax25_cb * ax25, unsigned char cmd, unsigned char param)
477 {
478 struct sk_buff *skb;
479 unsigned char *p;
480
481 if (ax25->device == NULL)
482 return;
483
484 if ((skb = alloc_skb(2, GFP_ATOMIC)) == NULL)
485 return;
486
487 skb->free = 1;
488 skb->arp = 1;
489
490 if (ax25->sk != NULL)
491 {
492 skb->sk = ax25->sk;
493 ax25->sk->wmem_alloc += skb->truesize;
494 }
495
496 skb->protocol = htons(ETH_P_AX25);
497
498 p = skb_put(skb, 2);
499
500 *p++=cmd;
501 *p =param;
502
503 dev_queue_xmit(skb, ax25->device, SOPRI_NORMAL);
504 }
505
506 void ax25_dama_on(ax25_cb *ax25)
507 {
508 int count = ax25_dev_is_dama_slave(ax25->device);
509
510 if (count == 0)
511 {
512 if (ax25->sk && ax25->sk->debug)
513 printk("DAMA on\n");
514 ax25_kiss_cmd(ax25, 5, 1);
515 }
516 }
517
518 void ax25_dama_off(ax25_cb *ax25)
519 {
520 int count = ax25_dev_is_dama_slave(ax25->device);
521
522
523 if (count == 0)
524 {
525 if (ax25->sk && ax25->sk->debug)
526 printk("DAMA off\n");
527 ax25_kiss_cmd(ax25, 5, 0);
528 }
529 }
530
531 #endif