This source file includes following definitions.
- ax2asc
- ax25cmp
- ax25_remove_socket
- ax25_kill_by_device
- ax25_device_event
- ax25_insert_socket
- ax25_find_listener
- ax25_find_socket
- ax25_find_cb
- ax25_addr_match
- ax25_send_to_raw
- ax25_destroy_timer
- ax25_destroy_socket
- ax25_findbyuid
- ax25_uid_ioctl
- ax25_create_cb
- ax25_fillin_cb
- ax25_send_frame
- ax25rtr_get_dev
- ax25_fcntl
- ax25_setsockopt
- ax25_getsockopt
- ax25_listen
- def_callback1
- def_callback2
- ax25_create
- ax25_make_new
- ax25_dup
- ax25_release
- ax25_bind
- ax25_connect
- ax25_socketpair
- ax25_accept
- ax25_getname
- ax25_rcv
- kiss_rcv
- bpq_rcv
- ax25_sendmsg
- ax25_sendto
- ax25_send
- ax25_write
- ax25_recvmsg
- ax25_recvfrom
- ax25_recv
- ax25_read
- ax25_shutdown
- ax25_select
- ax25_ioctl
- ax25_get_info
- ax25_proto_init
- ax25_queue_xmit
- ax25_encapsulate
- ax25_rebuild_header
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 #include <linux/config.h>
77 #ifdef CONFIG_AX25
78 #include <linux/errno.h>
79 #include <linux/types.h>
80 #include <linux/socket.h>
81 #include <linux/in.h>
82 #include <linux/kernel.h>
83 #include <linux/sched.h>
84 #include <linux/timer.h>
85 #include <linux/string.h>
86 #include <linux/sockios.h>
87 #include <linux/net.h>
88 #include <net/ax25.h>
89 #include <linux/inet.h>
90 #include <linux/netdevice.h>
91 #include <linux/if_arp.h>
92 #include <linux/skbuff.h>
93 #include <net/sock.h>
94 #include <asm/segment.h>
95 #include <asm/system.h>
96 #include <linux/fcntl.h>
97 #include <linux/termios.h>
98 #include <linux/mm.h>
99 #include <linux/interrupt.h>
100 #include <linux/notifier.h>
101 #include <linux/proc_fs.h>
102 #include <linux/stat.h>
103 #include <linux/firewall.h>
104
105 #include <net/ip.h>
106 #include <net/arp.h>
107
108
109
110
111
112
113
114
115 static ax25_cb *volatile ax25_list = NULL;
116
117
118
119
120 char *ax2asc(ax25_address *a)
121 {
122 static char buf[11];
123 char c, *s;
124 int n;
125
126 for (n = 0, s = buf; n < 6; n++)
127 {
128 c = (a->ax25_call[n] >> 1) & 0x7F;
129
130 if (c != ' ') *s++ = c;
131 }
132
133 *s++ = '-';
134
135 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9)
136 {
137 *s++ = '1';
138 n -= 10;
139 }
140
141 *s++ = n + '0';
142 *s++ = '\0';
143
144 if (*buf == '\0' || *buf == '-')
145 return "*";
146
147 return buf;
148
149 }
150
151
152
153
154 int ax25cmp(ax25_address *a, ax25_address *b)
155 {
156 int ct = 0;
157
158 while (ct < 6) {
159 if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE))
160 return 1;
161 ct++;
162 }
163
164 if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E))
165 return 0;
166
167 return 2;
168 }
169
170
171
172
173 static void ax25_remove_socket(ax25_cb *ax25)
174 {
175 ax25_cb *s;
176 unsigned long flags;
177
178 save_flags(flags);
179 cli();
180
181 if ((s = ax25_list) == ax25) {
182 ax25_list = s->next;
183 restore_flags(flags);
184 return;
185 }
186
187 while (s != NULL && s->next != NULL) {
188 if (s->next == ax25) {
189 s->next = ax25->next;
190 restore_flags(flags);
191 return;
192 }
193
194 s = s->next;
195 }
196
197 restore_flags(flags);
198 }
199
200
201
202
203 static void ax25_kill_by_device(struct device *dev)
204 {
205 ax25_cb *s;
206
207 for (s = ax25_list; s != NULL; s = s->next) {
208 if (s->device == dev) {
209 s->state = AX25_STATE_0;
210 s->device = NULL;
211 if (s->sk != NULL) {
212 s->sk->state = TCP_CLOSE;
213 s->sk->err = ENETUNREACH;
214 if (!s->sk->dead)
215 s->sk->state_change(s->sk);
216 s->sk->dead = 1;
217 }
218 }
219 }
220 }
221
222
223
224
225 static int ax25_device_event(unsigned long event, void *ptr)
226 {
227 struct device *dev = (struct device *)ptr;
228
229 switch (event) {
230 case NETDEV_UP:
231 ax25_dev_device_up(dev);
232 break;
233 case NETDEV_DOWN:
234 ax25_kill_by_device(dev);
235 ax25_rt_device_down(dev);
236 ax25_dev_device_down(dev);
237 break;
238 default:
239 break;
240 }
241
242 return NOTIFY_DONE;
243 }
244
245
246
247
248 static void ax25_insert_socket(ax25_cb *ax25)
249 {
250 unsigned long flags;
251
252 save_flags(flags);
253 cli();
254
255 ax25->next = ax25_list;
256 ax25_list = ax25;
257
258 restore_flags(flags);
259 }
260
261
262
263
264
265 static struct sock *ax25_find_listener(ax25_address *addr, struct device *dev, int type)
266 {
267 unsigned long flags;
268 ax25_cb *s;
269
270 save_flags(flags);
271 cli();
272
273 for (s = ax25_list; s != NULL; s = s->next) {
274 if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == type && s->sk->state == TCP_LISTEN) {
275
276 if (s->device == NULL || s->device == dev) {
277 restore_flags(flags);
278 return s->sk;
279 }
280 }
281 }
282
283 restore_flags(flags);
284 return NULL;
285 }
286
287
288
289
290 static struct sock *ax25_find_socket(ax25_address *my_addr, ax25_address *dest_addr, int type)
291 {
292 ax25_cb *s;
293 unsigned long flags;
294
295 save_flags(flags);
296 cli();
297
298 for (s = ax25_list; s != NULL; s = s->next) {
299 if (s->sk != NULL && ax25cmp(&s->source_addr, my_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->sk->type == type) {
300 restore_flags(flags);
301 return s->sk;
302 }
303 }
304
305 restore_flags(flags);
306
307 return NULL;
308 }
309
310
311
312
313
314 static ax25_cb *ax25_find_cb(ax25_address *my_addr, ax25_address *dest_addr, struct device *dev)
315 {
316 ax25_cb *s;
317 unsigned long flags;
318
319 save_flags(flags);
320 cli();
321
322 for (s = ax25_list; s != NULL; s = s->next) {
323 if (s->sk != NULL && s->sk->type != SOCK_SEQPACKET)
324 continue;
325 if (ax25cmp(&s->source_addr, my_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->device == dev) {
326 restore_flags(flags);
327 return s;
328 }
329 }
330
331 restore_flags(flags);
332
333 return NULL;
334 }
335
336
337
338
339 static struct sock *ax25_addr_match(ax25_address *addr)
340 {
341 unsigned long flags;
342 ax25_cb *s;
343
344 save_flags(flags);
345 cli();
346
347 for (s = ax25_list; s != NULL; s = s->next) {
348 if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == SOCK_RAW) {
349 restore_flags(flags);
350 return s->sk;
351 }
352 }
353
354 restore_flags(flags);
355
356 return NULL;
357 }
358
359 static void ax25_send_to_raw(struct sock *sk, struct sk_buff *skb, int proto)
360 {
361 struct sk_buff *copy;
362
363 while (sk != NULL) {
364 if (sk->type == SOCK_RAW && sk->protocol == proto && sk->rmem_alloc <= sk->rcvbuf) {
365 if ((copy = skb_clone(skb, GFP_ATOMIC)) == NULL)
366 return;
367
368 copy->sk = sk;
369 sk->rmem_alloc += copy->truesize;
370 skb_queue_tail(&sk->receive_queue, copy);
371 if (!sk->dead)
372 sk->data_ready(sk, skb->len);
373 }
374
375 sk = sk->next;
376 }
377 }
378
379
380
381
382 void ax25_destory_socket(ax25_cb *);
383
384
385
386
387 static void ax25_destroy_timer(unsigned long data)
388 {
389 ax25_destroy_socket((ax25_cb *)data);
390 }
391
392
393
394
395
396
397
398 void ax25_destroy_socket(ax25_cb *ax25)
399 {
400 struct sk_buff *skb;
401 unsigned long flags;
402
403 save_flags(flags);
404 cli();
405
406 del_timer(&ax25->timer);
407
408 ax25_remove_socket(ax25);
409 ax25_clear_queues(ax25);
410
411 if (ax25->sk != NULL) {
412 while ((skb = skb_dequeue(&ax25->sk->receive_queue)) != NULL) {
413 if (skb->sk != ax25->sk) {
414 skb->sk->dead = 1;
415 ax25_set_timer(skb->sk->ax25);
416 skb->sk->ax25->state = AX25_STATE_0;
417 }
418
419 kfree_skb(skb, FREE_READ);
420 }
421 }
422
423 if (ax25->digipeat != NULL) {
424 kfree_s(ax25->digipeat, sizeof(ax25_digi));
425 ax25->digipeat = NULL;
426 }
427
428 if (ax25->sk != NULL) {
429 if (ax25->sk->wmem_alloc || ax25->sk->rmem_alloc) {
430 init_timer(&ax25->timer);
431 ax25->timer.expires = jiffies + 10 * HZ;
432 ax25->timer.function = ax25_destroy_timer;
433 ax25->timer.data = (unsigned long)ax25;
434 add_timer(&ax25->timer);
435 } else {
436 kfree_s(ax25->sk, sizeof(*ax25->sk));
437 kfree_s(ax25, sizeof(*ax25));
438 }
439 } else {
440 kfree_s(ax25, sizeof(*ax25));
441 }
442
443 restore_flags(flags);
444 }
445
446
447
448
449
450 ax25_uid_assoc *ax25_uid_list;
451
452 int ax25_uid_policy = 0;
453
454 ax25_address *ax25_findbyuid(uid_t uid)
455 {
456 ax25_uid_assoc *a;
457
458 for (a = ax25_uid_list; a != NULL; a = a->next) {
459 if (a->uid == uid)
460 return &a->call;
461 }
462
463 return NULL;
464 }
465
466 static int ax25_uid_ioctl(int cmd, struct sockaddr_ax25 *sax)
467 {
468 ax25_uid_assoc *a;
469
470 switch (cmd) {
471 case SIOCAX25GETUID:
472 for (a = ax25_uid_list; a != NULL; a = a->next) {
473 if (ax25cmp(&sax->sax25_call, &a->call) == 0)
474 return a->uid;
475 }
476 return -ENOENT;
477 case SIOCAX25ADDUID:
478 if(!suser())
479 return -EPERM;
480 if (ax25_findbyuid(sax->sax25_uid))
481 return -EEXIST;
482 a = (ax25_uid_assoc *)kmalloc(sizeof(*a), GFP_KERNEL);
483 if (a == NULL)
484 return -ENOMEM;
485 a->uid = sax->sax25_uid;
486 a->call = sax->sax25_call;
487 a->next = ax25_uid_list;
488 ax25_uid_list = a;
489 return 0;
490 case SIOCAX25DELUID:
491 {
492 ax25_uid_assoc **l;
493
494 if(!suser())
495 return -EPERM;
496 l = &ax25_uid_list;
497 while ((*l) != NULL) {
498 if (ax25cmp(&((*l)->call), &(sax->sax25_call)) == 0) {
499 a = *l;
500 *l = (*l)->next;
501 kfree_s(a, sizeof(*a));
502 return 0;
503 }
504
505 l = &((*l)->next);
506 }
507 return -ENOENT;
508 }
509 }
510
511 return -EINVAL;
512 }
513
514
515
516
517 static ax25_cb *ax25_create_cb(void)
518 {
519 ax25_cb *ax25;
520
521 if ((ax25 = (ax25_cb *)kmalloc(sizeof(*ax25), GFP_ATOMIC)) == NULL)
522 return NULL;
523
524 skb_queue_head_init(&ax25->write_queue);
525 skb_queue_head_init(&ax25->frag_queue);
526 skb_queue_head_init(&ax25->ack_queue);
527 skb_queue_head_init(&ax25->reseq_queue);
528
529 init_timer(&ax25->timer);
530
531 ax25->rtt = (AX25_DEF_T1 * PR_SLOWHZ) / 2;
532 ax25->t1 = AX25_DEF_T1 * PR_SLOWHZ;
533 ax25->t2 = AX25_DEF_T2 * PR_SLOWHZ;
534 ax25->t3 = AX25_DEF_T3 * PR_SLOWHZ;
535 ax25->n2 = AX25_DEF_N2;
536
537 ax25->modulus = AX25_DEF_AXDEFMODE;
538 ax25->fragno = 0;
539 ax25->fraglen = 0;
540 ax25->hdrincl = 0;
541 ax25->backoff = AX25_DEF_BACKOFF == 'E';
542 ax25->condition = 0x00;
543 ax25->t1timer = 0;
544 ax25->t2timer = 0;
545 ax25->t3timer = 0;
546 ax25->n2count = 0;
547
548 ax25->va = 0;
549 ax25->vr = 0;
550 ax25->vs = 0;
551
552 if (AX25_DEF_AXDEFMODE == EMODULUS) {
553 ax25->window = AX25_DEF_EWINDOW;
554 } else {
555 ax25->window = AX25_DEF_WINDOW;
556 }
557 ax25->device = NULL;
558 ax25->digipeat = NULL;
559 ax25->sk = NULL;
560
561 ax25->state = AX25_STATE_0;
562
563 memset(&ax25->dest_addr, '\0', sizeof(ax25_address));
564 memset(&ax25->source_addr, '\0', sizeof(ax25_address));
565
566 return ax25;
567 }
568
569
570
571
572
573 static void ax25_fillin_cb(ax25_cb *ax25, struct device *dev)
574 {
575 ax25->device = dev;
576
577 ax25->rtt = ax25_dev_get_value(dev, AX25_VALUES_T1);
578 ax25->t1 = ax25_dev_get_value(dev, AX25_VALUES_T1);
579 ax25->t2 = ax25_dev_get_value(dev, AX25_VALUES_T2);
580 ax25->t3 = ax25_dev_get_value(dev, AX25_VALUES_T3);
581 ax25->n2 = ax25_dev_get_value(dev, AX25_VALUES_N2);
582
583 ax25->modulus = ax25_dev_get_value(dev, AX25_VALUES_AXDEFMODE);
584
585 if (ax25->modulus == MODULUS) {
586 ax25->window = ax25_dev_get_value(dev, AX25_VALUES_WINDOW);
587 } else {
588 ax25->window = ax25_dev_get_value(dev, AX25_VALUES_EWINDOW);
589 }
590
591 ax25->backoff = ax25_dev_get_value(dev, AX25_VALUES_BACKOFF) == 'E';
592 }
593
594 int ax25_send_frame(struct sk_buff *skb, ax25_address *src, ax25_address *dest,
595 ax25_digi *digi, struct device *dev)
596 {
597 ax25_cb *ax25;
598
599 if (skb == NULL)
600 return 0;
601
602
603
604
605 for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
606 if (ax25->sk != NULL && ax25->sk->type != SOCK_SEQPACKET)
607 continue;
608
609 if (ax25cmp(&ax25->source_addr, src) == 0 && ax25cmp(&ax25->dest_addr, dest) == 0 && ax25->device == dev) {
610 ax25_output(ax25, skb);
611 return 1;
612 }
613 }
614
615 if ((ax25 = ax25_create_cb()) == NULL)
616 return 0;
617
618 ax25_fillin_cb(ax25, dev);
619
620 memcpy(&ax25->source_addr, src, sizeof(ax25_address));
621 memcpy(&ax25->dest_addr, dest, sizeof(ax25_address));
622
623 if (digi != NULL) {
624 if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
625 kfree_s(ax25, sizeof(ax25));
626 return 0;
627 }
628 memcpy(ax25->digipeat, digi, sizeof(ax25_digi));
629 }
630
631 ax25_establish_data_link(ax25);
632 ax25_insert_socket(ax25);
633
634 ax25->state = AX25_STATE_1;
635
636 ax25_set_timer(ax25);
637
638 ax25_output(ax25, skb);
639
640 return 1;
641 }
642
643
644
645
646
647
648
649 struct device *ax25rtr_get_dev(ax25_address *addr)
650 {
651 struct device *dev;
652 ax25_address dev_addr;
653
654 for (dev = dev_base; dev != NULL; dev = dev->next) {
655 if (dev->flags & IFF_UP) {
656 switch (dev->type) {
657 case ARPHRD_AX25:
658 if (ax25cmp(addr, (ax25_address *)dev->dev_addr) == 0)
659 return dev;
660 break;
661 case ARPHRD_ETHER:
662 if (arp_query((unsigned char *)&dev_addr, dev->pa_addr, ARPHRD_AX25))
663 if (ax25cmp(addr, &dev_addr) == 0)
664 return dev;
665 break;
666 default:
667 break;
668 }
669 }
670 }
671
672 return NULL;
673 }
674
675
676
677
678
679
680
681 static int ax25_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
682 {
683 switch(cmd)
684 {
685 default:
686 return(-EINVAL);
687 }
688 }
689
690 static int ax25_setsockopt(struct socket *sock, int level, int optname,
691 char *optval, int optlen)
692 {
693 struct sock *sk;
694 int err, opt;
695
696 sk = (struct sock *)sock->data;
697
698 if (level == SOL_SOCKET)
699 return sock_setsockopt(sk, level, optname, optval, optlen);
700
701 if (level != SOL_AX25)
702 return -EOPNOTSUPP;
703
704 if (optval == NULL)
705 return -EINVAL;
706
707 if ((err = verify_area(VERIFY_READ, optval, sizeof(int))) != 0)
708 return err;
709
710 opt = get_fs_long((unsigned long *)optval);
711
712 switch (optname) {
713 case AX25_WINDOW:
714 if (sk->ax25->modulus == MODULUS) {
715 if (opt < 1 || opt > 7)
716 return -EINVAL;
717 } else {
718 if (opt < 1 || opt > 63)
719 return -EINVAL;
720 }
721 sk->ax25->window = opt;
722 return 0;
723
724 case AX25_T1:
725 if (opt < 1)
726 return -EINVAL;
727 sk->ax25->rtt = (opt * PR_SLOWHZ) / 2;
728 return 0;
729
730 case AX25_T2:
731 if (opt < 1)
732 return -EINVAL;
733 sk->ax25->t2 = opt * PR_SLOWHZ;
734 return 0;
735
736 case AX25_N2:
737 if (opt < 1 || opt > 31)
738 return -EINVAL;
739 sk->ax25->n2 = opt;
740 return 0;
741
742 case AX25_T3:
743 if (opt < 1)
744 return -EINVAL;
745 sk->ax25->t3 = opt * PR_SLOWHZ;
746 return 0;
747
748 case AX25_BACKOFF:
749 sk->ax25->backoff = opt ? 1 : 0;
750 return 0;
751
752 case AX25_EXTSEQ:
753 sk->ax25->modulus = opt ? EMODULUS : MODULUS;
754 return 0;
755
756 case AX25_HDRINCL:
757 sk->ax25->hdrincl = opt ? 1 : 0;
758 return 0;
759
760 default:
761 return -ENOPROTOOPT;
762 }
763 }
764
765 static int ax25_getsockopt(struct socket *sock, int level, int optname,
766 char *optval, int *optlen)
767 {
768 struct sock *sk;
769 int val = 0;
770 int err;
771
772 sk = (struct sock *)sock->data;
773
774 if (level == SOL_SOCKET)
775 return sock_getsockopt(sk, level, optname, optval, optlen);
776
777 if (level != SOL_AX25)
778 return -EOPNOTSUPP;
779
780 switch (optname) {
781 case AX25_WINDOW:
782 val = sk->ax25->window;
783 break;
784
785 case AX25_T1:
786 val = (sk->ax25->t1 * 2) / PR_SLOWHZ;
787 break;
788
789 case AX25_T2:
790 val = sk->ax25->t2 / PR_SLOWHZ;
791 break;
792
793 case AX25_N2:
794 val = sk->ax25->n2;
795 break;
796
797 case AX25_T3:
798 val = sk->ax25->t3 / PR_SLOWHZ;
799 break;
800
801 case AX25_BACKOFF:
802 val = sk->ax25->backoff;
803 break;
804
805 case AX25_EXTSEQ:
806 val = (sk->ax25->modulus == EMODULUS);
807 break;
808
809 case AX25_HDRINCL:
810 val = sk->ax25->hdrincl;
811 break;
812
813 default:
814 return -ENOPROTOOPT;
815 }
816
817 if ((err = verify_area(VERIFY_WRITE, optlen, sizeof(int))) != 0)
818 return err;
819
820 put_user(sizeof(int), optlen);
821
822 if ((err = verify_area(VERIFY_WRITE, optval, sizeof(int))) != 0)
823 return err;
824
825 put_user(val, optval);
826
827 return 0;
828 }
829
830 static int ax25_listen(struct socket *sock, int backlog)
831 {
832 struct sock *sk = (struct sock *)sock->data;
833
834 if (sk->type == SOCK_SEQPACKET && sk->state != TCP_LISTEN) {
835 sk->max_ack_backlog = backlog;
836 sk->state = TCP_LISTEN;
837 return 0;
838 }
839
840 return -EOPNOTSUPP;
841 }
842
843 static void def_callback1(struct sock *sk)
844 {
845 if (!sk->dead)
846 wake_up_interruptible(sk->sleep);
847 }
848
849 static void def_callback2(struct sock *sk, int len)
850 {
851 if (!sk->dead)
852 wake_up_interruptible(sk->sleep);
853 }
854
855 static int ax25_create(struct socket *sock, int protocol)
856 {
857 struct sock *sk;
858 ax25_cb *ax25;
859
860 switch (sock->type) {
861 case SOCK_DGRAM:
862 if (protocol == 0 || protocol == AF_AX25)
863 protocol = AX25_P_TEXT;
864 break;
865 case SOCK_SEQPACKET:
866 switch (protocol) {
867 case 0:
868 case AF_AX25:
869 protocol = AX25_P_TEXT;
870 break;
871 case AX25_P_SEGMENT:
872 #ifdef CONFIG_INET
873 case AX25_P_ARP:
874 case AX25_P_IP:
875 #endif
876 #ifdef CONFIG_NETROM
877 case AX25_P_NETROM:
878 #endif
879 return -ESOCKTNOSUPPORT;
880 default:
881 break;
882 }
883 break;
884 case SOCK_RAW:
885 break;
886 default:
887 return -ESOCKTNOSUPPORT;
888 }
889
890 if ((sk = (struct sock *)kmalloc(sizeof(*sk), GFP_ATOMIC)) == NULL)
891 return -ENOMEM;
892
893 if ((ax25 = ax25_create_cb()) == NULL) {
894 kfree_s(sk, sizeof(*sk));
895 return -ENOMEM;
896 }
897
898 skb_queue_head_init(&sk->receive_queue);
899 skb_queue_head_init(&sk->write_queue);
900 skb_queue_head_init(&sk->back_log);
901
902 sk->socket = sock;
903 sk->type = sock->type;
904 sk->protocol = protocol;
905 sk->dead = 0;
906 sk->next = NULL;
907 sk->broadcast = 0;
908 sk->allocation = GFP_KERNEL;
909 sk->rcvbuf = SK_RMEM_MAX;
910 sk->sndbuf = SK_WMEM_MAX;
911 sk->wmem_alloc = 0;
912 sk->rmem_alloc = 0;
913 sk->inuse = 0;
914 sk->debug = 0;
915 sk->destroy = 0;
916 sk->prot = NULL;
917 sk->err = 0;
918 sk->localroute = 0;
919 sk->send_head = NULL;
920 sk->state = TCP_CLOSE;
921 sk->shutdown = 0;
922 sk->priority = SOPRI_NORMAL;
923 sk->ack_backlog = 0;
924 sk->mtu = AX25_MTU;
925 sk->zapped = 1;
926
927 sk->state_change = def_callback1;
928 sk->data_ready = def_callback2;
929 sk->write_space = def_callback1;
930 sk->error_report = def_callback1;
931
932 if (sock != NULL) {
933 sock->data = (void *)sk;
934 sk->sleep = sock->wait;
935 }
936
937 ax25->sk = sk;
938 sk->ax25 = ax25;
939
940 return 0;
941 }
942
943 static struct sock *ax25_make_new(struct sock *osk, struct device *dev)
944 {
945 struct sock *sk;
946 ax25_cb *ax25;
947
948 if ((sk = (struct sock *)kmalloc(sizeof(*sk), GFP_ATOMIC)) == NULL)
949 return NULL;
950
951 if ((ax25 = ax25_create_cb()) == NULL) {
952 kfree_s(sk, sizeof(*sk));
953 return NULL;
954 }
955
956 ax25_fillin_cb(ax25, dev);
957
958 sk->type = osk->type;
959 sk->socket = osk->socket;
960
961 switch(osk->type)
962 {
963 case SOCK_DGRAM:
964 break;
965 case SOCK_SEQPACKET:
966 break;
967 default:
968 kfree_s((void *)sk, sizeof(*sk));
969 kfree_s((void *)ax25, sizeof(*ax25));
970 return NULL;
971 }
972
973 skb_queue_head_init(&sk->receive_queue);
974 skb_queue_head_init(&sk->write_queue);
975 skb_queue_head_init(&sk->back_log);
976
977 sk->dead = 0;
978 sk->next = NULL;
979 sk->priority = osk->priority;
980 sk->broadcast = 0;
981 sk->protocol = osk->protocol;
982 sk->rcvbuf = osk->rcvbuf;
983 sk->sndbuf = osk->sndbuf;
984 sk->wmem_alloc = 0;
985 sk->rmem_alloc = 0;
986 sk->inuse = 0;
987 sk->ack_backlog = 0;
988 sk->destroy = 0;
989 sk->prot = NULL;
990 sk->err = 0;
991 sk->localroute = 0;
992 sk->send_head = NULL;
993 sk->debug = osk->debug;
994 sk->state = TCP_ESTABLISHED;
995 sk->window = osk->window;
996 sk->shutdown = 0;
997 sk->mtu = osk->mtu;
998 sk->sleep = osk->sleep;
999 sk->zapped = osk->zapped;
1000
1001 sk->state_change = def_callback1;
1002 sk->data_ready = def_callback2;
1003 sk->write_space = def_callback1;
1004 sk->error_report = def_callback1;
1005
1006 ax25->modulus = osk->ax25->modulus;
1007 ax25->backoff = osk->ax25->backoff;
1008 ax25->hdrincl = osk->ax25->hdrincl;
1009 ax25->rtt = osk->ax25->rtt;
1010 ax25->t1 = osk->ax25->t1;
1011 ax25->t2 = osk->ax25->t2;
1012 ax25->t3 = osk->ax25->t3;
1013 ax25->n2 = osk->ax25->n2;
1014
1015 ax25->window = osk->ax25->window;
1016
1017 memcpy(&ax25->source_addr, &osk->ax25->source_addr, sizeof(ax25_address));
1018
1019 if (osk->ax25->digipeat != NULL) {
1020 if ((ax25->digipeat = (ax25_digi *)kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
1021 kfree_s(sk, sizeof(*sk));
1022 kfree_s(ax25, sizeof(*ax25));
1023 return NULL;
1024 }
1025 }
1026
1027 sk->ax25 = ax25;
1028 ax25->sk = sk;
1029
1030 return sk;
1031 }
1032
1033 static int ax25_dup(struct socket *newsock, struct socket *oldsock)
1034 {
1035 struct sock *sk = (struct sock *)oldsock->data;
1036
1037 return ax25_create(newsock, sk->protocol);
1038 }
1039
1040 static int ax25_release(struct socket *sock, struct socket *peer)
1041 {
1042 struct sock *sk = (struct sock *)sock->data;
1043
1044 if (sk == NULL) return 0;
1045
1046 if (sk->type == SOCK_SEQPACKET) {
1047 switch (sk->ax25->state) {
1048 case AX25_STATE_0:
1049 sk->state = TCP_CLOSE;
1050 sk->state_change(sk);
1051 sk->dead = 1;
1052 ax25_destroy_socket(sk->ax25);
1053 break;
1054
1055 case AX25_STATE_1:
1056 ax25_send_control(sk->ax25, DISC, POLLON, C_COMMAND);
1057 sk->ax25->state = AX25_STATE_0;
1058 sk->state = TCP_CLOSE;
1059 sk->state_change(sk);
1060 sk->dead = 1;
1061 ax25_destroy_socket(sk->ax25);
1062 break;
1063
1064 case AX25_STATE_2:
1065 ax25_send_control(sk->ax25, DM, POLLON, C_RESPONSE);
1066 sk->ax25->state = AX25_STATE_0;
1067 sk->state = TCP_CLOSE;
1068 sk->state_change(sk);
1069 sk->dead = 1;
1070 ax25_destroy_socket(sk->ax25);
1071 break;
1072
1073 case AX25_STATE_3:
1074 case AX25_STATE_4:
1075 ax25_clear_queues(sk->ax25);
1076 sk->ax25->n2count = 0;
1077 ax25_send_control(sk->ax25, DISC, POLLON, C_COMMAND);
1078 sk->ax25->t3timer = 0;
1079 sk->ax25->t1timer = sk->ax25->t1 = ax25_calculate_t1(sk->ax25);
1080 sk->ax25->state = AX25_STATE_2;
1081 sk->state = TCP_CLOSE;
1082 sk->state_change(sk);
1083 sk->dead = 1;
1084 sk->destroy = 1;
1085 break;
1086
1087 default:
1088 break;
1089 }
1090 } else {
1091 sk->state = TCP_CLOSE;
1092 sk->state_change(sk);
1093 sk->dead = 1;
1094 ax25_destroy_socket(sk->ax25);
1095 }
1096
1097 sock->data = NULL;
1098 sk->socket = NULL;
1099
1100 return 0;
1101 }
1102
1103
1104
1105
1106
1107
1108
1109 static int ax25_bind(struct socket *sock, struct sockaddr *uaddr,int addr_len)
1110 {
1111 struct sock *sk;
1112 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
1113 struct device *dev;
1114 ax25_address *call;
1115
1116 sk = (struct sock *)sock->data;
1117
1118 if (sk->zapped == 0)
1119 return -EIO;
1120
1121 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
1122 return -EINVAL;
1123
1124 #ifdef DONTDO
1125 if (ax25_find_socket(&addr->fsa_ax25.sax25_call, sk->type) != NULL) {
1126 if (sk->debug)
1127 printk("AX25: bind failed: in use\n");
1128 return -EADDRINUSE;
1129 }
1130 #endif
1131
1132 call = ax25_findbyuid(current->euid);
1133 if (call == NULL && ax25_uid_policy && !suser())
1134 return -EPERM;
1135
1136 if (call == NULL)
1137 memcpy(&sk->ax25->source_addr, &addr->fsa_ax25.sax25_call, sizeof(ax25_address));
1138 else
1139 memcpy(&sk->ax25->source_addr, call, sizeof(ax25_address));
1140
1141 if (addr_len == sizeof(struct full_sockaddr_ax25) && addr->fsa_ax25.sax25_ndigis == 1) {
1142 if (!suser())
1143 return -EPERM;
1144 call = &addr->fsa_digipeater[0];
1145 } else {
1146 call = &addr->fsa_ax25.sax25_call;
1147 }
1148
1149 if ((dev = ax25rtr_get_dev(call)) == NULL) {
1150 if (sk->debug)
1151 printk("AX25 bind failed: no device\n");
1152 return -EADDRNOTAVAIL;
1153 }
1154
1155 ax25_fillin_cb(sk->ax25, dev);
1156 ax25_insert_socket(sk->ax25);
1157
1158 sk->zapped = 0;
1159
1160 if (sk->debug)
1161 printk("AX25: socket is bound\n");
1162
1163 return 0;
1164 }
1165
1166 static int ax25_connect(struct socket *sock, struct sockaddr *uaddr,
1167 int addr_len, int flags)
1168 {
1169 struct sock *sk = (struct sock *)sock->data;
1170 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
1171 int err;
1172
1173 if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
1174 sock->state = SS_CONNECTED;
1175 return 0;
1176 }
1177
1178 if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
1179 sock->state = SS_UNCONNECTED;
1180 return -ECONNREFUSED;
1181 }
1182
1183 if (sk->state == TCP_ESTABLISHED && sk->type == SOCK_SEQPACKET)
1184 return -EISCONN;
1185
1186 sk->state = TCP_CLOSE;
1187 sock->state = SS_UNCONNECTED;
1188
1189 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
1190 return -EINVAL;
1191 if (addr_len == sizeof(struct full_sockaddr_ax25) && addr->sax25_ndigis != 0) {
1192 int ct = 0;
1193 struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)addr;
1194
1195
1196 if (addr->sax25_ndigis < 1 || addr->sax25_ndigis > AX25_MAX_DIGIS)
1197 return -EINVAL;
1198
1199 if (sk->ax25->digipeat == NULL) {
1200 if ((sk->ax25->digipeat = (ax25_digi *)kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL)
1201 return -ENOMEM;
1202 }
1203
1204 sk->ax25->digipeat->ndigi = addr->sax25_ndigis;
1205
1206 while (ct < addr->sax25_ndigis) {
1207 sk->ax25->digipeat->repeated[ct] = 0;
1208 memcpy(&sk->ax25->digipeat->calls[ct], &fsa->fsa_digipeater[ct], sizeof(ax25_address));
1209 ct++;
1210 }
1211
1212 sk->ax25->digipeat->lastrepeat = 0;
1213 }
1214
1215 if (sk->zapped) {
1216 if ((err = ax25_rt_autobind(sk->ax25, &addr->sax25_call)) < 0)
1217 return err;
1218 ax25_insert_socket(sk->ax25);
1219 }
1220
1221 if (sk->type == SOCK_SEQPACKET && ax25_find_cb(&sk->ax25->source_addr, &addr->sax25_call, sk->ax25->device) != NULL)
1222 return -EBUSY;
1223
1224 memcpy(&sk->ax25->dest_addr, &addr->sax25_call, sizeof(ax25_address));
1225
1226
1227 if (sk->type != SOCK_SEQPACKET) {
1228 sock->state = SS_CONNECTED;
1229 sk->state = TCP_ESTABLISHED;
1230 return 0;
1231 }
1232
1233
1234 sock->state = SS_CONNECTING;
1235 sk->state = TCP_SYN_SENT;
1236 ax25_establish_data_link(sk->ax25);
1237 sk->ax25->state = AX25_STATE_1;
1238 ax25_set_timer(sk->ax25);
1239
1240
1241 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1242 return -EINPROGRESS;
1243
1244 cli();
1245
1246
1247 while (sk->state == TCP_SYN_SENT) {
1248 interruptible_sleep_on(sk->sleep);
1249 if (current->signal & ~current->blocked) {
1250 sti();
1251 return -ERESTARTSYS;
1252 }
1253 }
1254
1255 if (sk->state != TCP_ESTABLISHED) {
1256 sti();
1257 sock->state = SS_UNCONNECTED;
1258 return -sk->err;
1259 }
1260
1261 sock->state = SS_CONNECTED;
1262
1263 sti();
1264
1265 return 0;
1266 }
1267
1268 static int ax25_socketpair(struct socket *sock1, struct socket *sock2)
1269 {
1270 return -EOPNOTSUPP;
1271 }
1272
1273 static int ax25_accept(struct socket *sock, struct socket *newsock, int flags)
1274 {
1275 struct sock *sk;
1276 struct sock *newsk;
1277 struct sk_buff *skb;
1278
1279 if (newsock->data)
1280 kfree_s(newsock->data, sizeof(struct sock));
1281
1282 newsock->data = NULL;
1283
1284 sk = (struct sock *)sock->data;
1285
1286 if (sk->type != SOCK_SEQPACKET)
1287 return -EOPNOTSUPP;
1288
1289 if (sk->state != TCP_LISTEN)
1290 return -EINVAL;
1291
1292
1293
1294 do {
1295 cli();
1296 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
1297 if (flags & O_NONBLOCK) {
1298 sti();
1299 return 0;
1300 }
1301 interruptible_sleep_on(sk->sleep);
1302 if (current->signal & ~current->blocked) {
1303 sti();
1304 return -ERESTARTSYS;
1305 }
1306 }
1307 } while (skb == NULL);
1308
1309 newsk = skb->sk;
1310 newsk->pair = NULL;
1311 sti();
1312
1313
1314 skb->sk = NULL;
1315 kfree_skb(skb, FREE_READ);
1316 sk->ack_backlog--;
1317 newsock->data = newsk;
1318
1319 return 0;
1320 }
1321
1322 static int ax25_getname(struct socket *sock, struct sockaddr *uaddr,
1323 int *uaddr_len, int peer)
1324 {
1325 ax25_address *addr;
1326 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
1327 struct sock *sk;
1328 unsigned char ndigi, i;
1329
1330 sk = (struct sock *)sock->data;
1331
1332 if (peer != 0) {
1333 if (sk->state != TCP_ESTABLISHED)
1334 return -ENOTCONN;
1335 addr = &sk->ax25->dest_addr;
1336 } else {
1337 addr = &sk->ax25->source_addr;
1338 }
1339
1340 sax->fsa_ax25.sax25_family = AF_AX25;
1341 memcpy(&sax->fsa_ax25.sax25_call, addr, sizeof(ax25_address));
1342 sax->fsa_ax25.sax25_ndigis = 0;
1343 *uaddr_len = sizeof(struct sockaddr_ax25);
1344
1345
1346 if (sk->ax25->digipeat != NULL) {
1347 ndigi = sk->ax25->digipeat->ndigi;
1348 sax->fsa_ax25.sax25_ndigis = ndigi;
1349 *uaddr_len += sizeof(ax25_address) * ndigi;
1350 for (i = 0; i < ndigi; i++)
1351 memcpy(&sax->fsa_digipeater[i], &sk->ax25->digipeat->calls[i], sizeof(ax25_address));
1352 }
1353
1354 return 0;
1355 }
1356
1357 static int ax25_rcv(struct sk_buff *skb, struct device *dev, ax25_address *dev_addr, struct packet_type *ptype)
1358 {
1359 struct sock *make;
1360 struct sock *sk;
1361 int type = 0;
1362 ax25_digi dp;
1363 ax25_cb *ax25;
1364 ax25_address src, dest;
1365 struct sock *raw;
1366 int mine = 0;
1367
1368
1369
1370
1371
1372 skb->h.raw = skb->data;
1373
1374 #ifdef CONFIG_FIREWALL
1375
1376 if(call_in_firewall(PF_AX25, skb, skb->h.raw)!=FW_ACCEPT)
1377 {
1378 kfree_skb(skb, FREE_READ);
1379 return 0;
1380 }
1381 #endif
1382
1383
1384
1385
1386
1387 if (ax25_parse_addr(skb->data, skb->len, &src, &dest, &dp, &type) == NULL) {
1388 kfree_skb(skb, FREE_READ);
1389 return 0;
1390 }
1391
1392
1393
1394
1395 ax25_rt_rx_frame(&src, dev, &dp);
1396
1397
1398
1399
1400 if (dp.lastrepeat + 1 < dp.ndigi) {
1401 if (ax25cmp(&dp.calls[dp.lastrepeat + 1], dev_addr) == 0)
1402 {
1403 struct device *dev_out = dev;
1404
1405
1406
1407 dp.lastrepeat++;
1408 dp.repeated[(int)dp.lastrepeat] = 1;
1409
1410 if (ax25_dev_get_value(dev, AX25_VALUES_DIGI) & AX25_DIGI_XBAND)
1411 {
1412 while (dp.lastrepeat + 1 < dp.ndigi)
1413 {
1414 struct device *dev_scan;
1415 if ((dev_scan = ax25rtr_get_dev(&dp.calls[dp.lastrepeat + 1])) == NULL)
1416 break;
1417 dp.lastrepeat++;
1418 dp.repeated[(int)dp.lastrepeat] = 1;
1419 dev_out = dev_scan;
1420 }
1421 if (dev != dev_out && (ax25_dev_get_value(dev_out, AX25_VALUES_DIGI) & AX25_DIGI_XBAND) == 0)
1422 kfree_skb(skb, FREE_READ);
1423 }
1424
1425 if (dev == dev_out && (ax25_dev_get_value(dev, AX25_VALUES_DIGI) & AX25_DIGI_INBAND) == 0)
1426 kfree_skb(skb, FREE_READ);
1427
1428 build_ax25_addr(skb->data, &src, &dest, &dp, type, MODULUS);
1429 #ifdef CONFIG_FIREWALL
1430 if(call_fw_firewall(PF_AX25, skb,skb->data)!=FW_ACCEPT)
1431 {
1432 kfree_skb(skb, FREE_READ);
1433 return 0;
1434 }
1435 #endif
1436 skb->arp = 1;
1437 ax25_queue_xmit(skb, dev_out, SOPRI_NORMAL);
1438 } else {
1439 kfree_skb(skb, FREE_READ);
1440 }
1441
1442 return 0;
1443 }
1444
1445
1446
1447
1448 skb_pull(skb, size_ax25_addr(&dp));
1449
1450
1451 if (ax25cmp(&dest, dev_addr) == 0)
1452 mine = 1;
1453
1454 #ifdef CONFIG_NETROM
1455
1456 if (!mine && nr_dev_get(&dest) != NULL)
1457 mine = 1;
1458 #endif
1459
1460 if ((*skb->data & ~0x10) == LAPB_UI) {
1461 skb->h.raw = skb->data + 2;
1462
1463 if ((raw = ax25_addr_match(&dest)) != NULL)
1464 ax25_send_to_raw(raw, skb, skb->data[1]);
1465
1466 if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0) {
1467 kfree_skb(skb, FREE_READ);
1468 return 0;
1469 }
1470
1471
1472 switch (skb->data[1]) {
1473 #ifdef CONFIG_INET
1474 case AX25_P_IP:
1475 skb_pull(skb,2);
1476 ax25_ip_mode_set(&src, dev, 'D');
1477 ip_rcv(skb, dev, ptype);
1478 break;
1479
1480 case AX25_P_ARP:
1481 skb_pull(skb,2);
1482 arp_rcv(skb, dev, ptype);
1483 break;
1484 #endif
1485 case AX25_P_TEXT:
1486
1487 if ((sk = ax25_find_socket(&dest, &src, SOCK_DGRAM)) != NULL) {
1488 if (sk->rmem_alloc >= sk->rcvbuf) {
1489 kfree_skb(skb, FREE_READ);
1490 } else {
1491
1492
1493
1494 skb_pull(skb, 2);
1495 skb_queue_tail(&sk->receive_queue, skb);
1496 skb->sk = sk;
1497 sk->rmem_alloc += skb->truesize;
1498 if (!sk->dead)
1499 sk->data_ready(sk, skb->len);
1500 }
1501 } else {
1502 kfree_skb(skb, FREE_READ);
1503 }
1504 break;
1505
1506 default:
1507 kfree_skb(skb, FREE_READ);
1508 break;
1509 }
1510
1511 return 0;
1512 }
1513
1514
1515
1516
1517
1518
1519 if (!ax25_dev_get_value(dev, AX25_VALUES_CONMODE)) {
1520 kfree_skb(skb, FREE_READ);
1521 return 0;
1522 }
1523
1524
1525 if ((ax25 = ax25_find_cb(&dest, &src, dev)) != NULL) {
1526
1527
1528
1529
1530
1531 if (ax25_process_rx_frame(ax25, skb, type) == 0)
1532 kfree_skb(skb, FREE_READ);
1533
1534 return 0;
1535 }
1536
1537 if ((*skb->data & ~PF) != SABM && (*skb->data & ~PF) != SABME) {
1538
1539
1540
1541
1542 if ((*skb->data & ~PF) != DM && mine)
1543 ax25_return_dm(dev, &src, &dest, &dp);
1544
1545 kfree_skb(skb, FREE_READ);
1546 return 0;
1547 }
1548
1549 if ((sk = ax25_find_listener(&dest, dev, SOCK_SEQPACKET)) != NULL) {
1550 if (sk->ack_backlog == sk->max_ack_backlog || (make = ax25_make_new(sk, dev)) == NULL) {
1551 if (mine)
1552 ax25_return_dm(dev, &src, &dest, &dp);
1553
1554 kfree_skb(skb, FREE_READ);
1555 return 0;
1556 }
1557
1558 ax25 = make->ax25;
1559
1560 skb_queue_head(&sk->receive_queue, skb);
1561
1562 skb->sk = make;
1563 make->state = TCP_ESTABLISHED;
1564 make->pair = sk;
1565
1566 sk->ack_backlog++;
1567 } else {
1568 #ifdef CONFIG_NETROM
1569 if (!mine) {
1570 kfree_skb(skb, FREE_READ);
1571 return 0;
1572 }
1573
1574 if ((ax25 = ax25_create_cb()) == NULL) {
1575 ax25_return_dm(dev, &src, &dest, &dp);
1576 kfree_skb(skb, FREE_READ);
1577 return 0;
1578 }
1579
1580 ax25_fillin_cb(ax25, dev);
1581 #else
1582 if (mine)
1583 ax25_return_dm(dev, &src, &dest, &dp);
1584
1585 kfree_skb(skb, FREE_READ);
1586 return 0;
1587 #endif
1588 }
1589
1590 memcpy(&ax25->source_addr, &dest, sizeof(ax25_address));
1591 memcpy(&ax25->dest_addr, &src, sizeof(ax25_address));
1592
1593
1594
1595
1596 if (dp.ndigi != 0 && ax25->digipeat == NULL && (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
1597 kfree_skb(skb, FREE_READ);
1598 ax25_destroy_socket(ax25);
1599 return 0;
1600 }
1601
1602 if (dp.ndigi == 0) {
1603 if (ax25->digipeat != NULL) {
1604 kfree_s(ax25->digipeat, sizeof(ax25_digi));
1605 ax25->digipeat = NULL;
1606 }
1607 } else {
1608
1609 ax25_digi_invert(&dp, ax25->digipeat);
1610 }
1611
1612 if ((*skb->data & ~PF) == SABME) {
1613 ax25->modulus = EMODULUS;
1614 ax25->window = ax25_dev_get_value(dev, AX25_VALUES_EWINDOW);
1615 } else {
1616 ax25->modulus = MODULUS;
1617 ax25->window = ax25_dev_get_value(dev, AX25_VALUES_WINDOW);
1618 }
1619
1620 ax25->device = dev;
1621
1622 ax25_send_control(ax25, UA, POLLON, C_RESPONSE);
1623
1624 ax25->t3timer = ax25->t3;
1625 ax25->state = AX25_STATE_3;
1626
1627 ax25_insert_socket(ax25);
1628
1629 ax25_set_timer(ax25);
1630
1631 if (sk != NULL) {
1632 if (!sk->dead)
1633 sk->data_ready(sk, skb->len );
1634 } else {
1635 kfree_skb(skb, FREE_READ);
1636 }
1637
1638 return 0;
1639 }
1640
1641
1642
1643
1644 static int kiss_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *ptype)
1645 {
1646 skb->sk = NULL;
1647
1648 if ((*skb->data & 0x0F) != 0) {
1649 kfree_skb(skb, FREE_READ);
1650 return 0;
1651 }
1652
1653 skb_pull(skb, AX25_KISS_HEADER_LEN);
1654
1655 return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);
1656 }
1657
1658
1659
1660
1661 static int bpq_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *ptype)
1662 {
1663 ax25_address port_call;
1664 int len;
1665
1666 skb->sk = NULL;
1667
1668 if (!arp_query((unsigned char *)&port_call, dev->pa_addr, ARPHRD_AX25)) {
1669 kfree_skb(skb, FREE_READ);
1670 return 0;
1671 }
1672
1673 len = skb->data[0] + skb->data[1] * 256 - 5;
1674
1675 skb_pull(skb, 2);
1676 skb_trim(skb, len);
1677
1678 return ax25_rcv(skb, dev, &port_call, ptype);
1679 }
1680
1681 static int ax25_sendmsg(struct socket *sock, struct msghdr *msg, int len, int noblock, int flags)
1682 {
1683 struct sock *sk = (struct sock *)sock->data;
1684 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1685 int err;
1686 struct sockaddr_ax25 sax;
1687 struct sk_buff *skb;
1688 unsigned char *asmptr;
1689 int size;
1690 ax25_digi *dp;
1691 ax25_digi dtmp;
1692 int lv;
1693 int addr_len=msg->msg_namelen;
1694
1695 if (sk->err) {
1696 cli();
1697 err = sk->err;
1698 sk->err = 0;
1699 sti();
1700 return -err;
1701 }
1702
1703 if (flags|| msg->msg_accrights)
1704 return -EINVAL;
1705
1706 if (sk->zapped)
1707 return -EADDRNOTAVAIL;
1708
1709 if (sk->ax25->device == NULL)
1710 return -ENETUNREACH;
1711
1712 if (usax) {
1713 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
1714 return -EINVAL;
1715 if (usax->sax25_family != AF_AX25)
1716 return -EINVAL;
1717 if (addr_len == sizeof(struct full_sockaddr_ax25) && usax->sax25_ndigis != 0) {
1718 int ct = 0;
1719 struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)usax;
1720
1721
1722 if (usax->sax25_ndigis < 1 || usax->sax25_ndigis > AX25_MAX_DIGIS)
1723 return -EINVAL;
1724
1725 dtmp.ndigi = usax->sax25_ndigis;
1726
1727 while (ct < usax->sax25_ndigis) {
1728 dtmp.repeated[ct] = 0;
1729 memcpy(&dtmp.calls[ct], &fsa->fsa_digipeater[ct], sizeof(ax25_address));
1730 ct++;
1731 }
1732
1733 dtmp.lastrepeat = 0;
1734 }
1735
1736 memcpy(&sax, usax, sizeof(sax));
1737 if (sk->type == SOCK_SEQPACKET && memcmp(&sk->ax25->dest_addr, &sax.sax25_call, sizeof(ax25_address)) != 0)
1738 return -EISCONN;
1739 if (usax->sax25_ndigis == 0)
1740 dp = NULL;
1741 else
1742 dp = &dtmp;
1743 } else {
1744 if (sk->state != TCP_ESTABLISHED)
1745 return -ENOTCONN;
1746 sax.sax25_family = AF_AX25;
1747 memcpy(&sax.sax25_call, &sk->ax25->dest_addr, sizeof(ax25_address));
1748 dp = sk->ax25->digipeat;
1749 }
1750
1751 if (sk->debug)
1752 printk("AX.25: sendto: Addresses built.\n");
1753
1754
1755 if (sk->debug)
1756 printk("AX.25: sendto: building packet.\n");
1757
1758
1759 size = len + 3 + size_ax25_addr(dp) + AX25_BPQ_HEADER_LEN;
1760
1761 if ((skb = sock_alloc_send_skb(sk, size, 0, 0, &err)) == NULL)
1762 return err;
1763
1764 skb->sk = sk;
1765 skb->free = 1;
1766 skb->arp = 1;
1767
1768 skb_reserve(skb, size - len);
1769
1770 if (sk->debug)
1771 printk("AX.25: Appending user data\n");
1772
1773
1774 memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1775
1776
1777 asmptr = skb_push(skb, 1);
1778 *asmptr = sk->protocol;
1779
1780 if (sk->debug)
1781 printk("AX.25: Transmitting buffer\n");
1782
1783 if (sk->type == SOCK_SEQPACKET) {
1784
1785 if (sk->state != TCP_ESTABLISHED) {
1786 kfree_skb(skb, FREE_WRITE);
1787 return -ENOTCONN;
1788 }
1789
1790 ax25_output(sk->ax25, skb);
1791
1792 return len;
1793 } else {
1794 asmptr = skb_push(skb, 1 + size_ax25_addr(dp));
1795
1796 if (sk->debug) {
1797 printk("Building AX.25 Header (dp=%p).\n", dp);
1798 if (dp != 0)
1799 printk("Num digipeaters=%d\n", dp->ndigi);
1800 }
1801
1802
1803 asmptr += (lv = build_ax25_addr(asmptr, &sk->ax25->source_addr, &sax.sax25_call, dp, C_COMMAND, MODULUS));
1804
1805 if (sk->debug)
1806 printk("Built header (%d bytes)\n",lv);
1807
1808 skb->h.raw = asmptr;
1809
1810 if (sk->debug)
1811 printk("base=%p pos=%p\n", skb->data, asmptr);
1812
1813 *asmptr = LAPB_UI;
1814
1815
1816 ax25_queue_xmit(skb, sk->ax25->device, SOPRI_NORMAL);
1817
1818 return len;
1819 }
1820
1821 }
1822
1823 static int ax25_sendto(struct socket *sock, const void *ubuf, int size, int noblock, unsigned flags,
1824 struct sockaddr *sa, int addr_len)
1825 {
1826 struct iovec iov;
1827 struct msghdr msg;
1828
1829 iov.iov_base = (void *)ubuf;
1830 iov.iov_len = size;
1831
1832 msg.msg_name = (void *)sa;
1833 msg.msg_namelen = addr_len;
1834 msg.msg_accrights = NULL;
1835 msg.msg_iov = &iov;
1836 msg.msg_iovlen = 1;
1837
1838 return ax25_sendmsg(sock, &msg, size, noblock, flags);
1839 }
1840
1841 static int ax25_send(struct socket *sock, const void *ubuf, int size, int noblock, unsigned flags)
1842 {
1843 return ax25_sendto(sock, ubuf, size, noblock, flags, NULL, 0);
1844 }
1845
1846 static int ax25_write(struct socket *sock, const char *ubuf, int size, int noblock)
1847 {
1848 return ax25_sendto(sock, ubuf, size, noblock, 0, NULL, 0);
1849 }
1850
1851 static int ax25_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, int flags, int *addr_len)
1852 {
1853 struct sock *sk = (struct sock *)sock->data;
1854 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1855 int copied, length;
1856 struct sk_buff *skb;
1857 int er;
1858
1859 if (sk->err) {
1860 cli();
1861 er = -sk->err;
1862 sk->err = 0;
1863 sti();
1864 return er;
1865 }
1866
1867 if (addr_len != NULL)
1868 *addr_len = sizeof(*sax);
1869
1870
1871
1872
1873
1874 if (sk->type == SOCK_SEQPACKET && sk->state != TCP_ESTABLISHED)
1875 return -ENOTCONN;
1876
1877
1878 if ((skb = skb_recv_datagram(sk, flags, noblock, &er)) == NULL)
1879 return er;
1880
1881 if (sk->ax25->hdrincl) {
1882 length = skb->len + (skb->data - skb->h.raw);
1883 } else {
1884 if (sk->type == SOCK_SEQPACKET)
1885 skb_pull(skb, 1);
1886 length = skb->len;
1887 skb->h.raw = skb->data;
1888 }
1889
1890 copied = (size < length) ? size : length;
1891 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1892
1893 if (sax) {
1894 ax25_digi digi;
1895 ax25_address dest;
1896
1897 if (addr_len == (int *)0)
1898 return -EINVAL;
1899 if (*addr_len != sizeof(struct sockaddr_ax25) && *addr_len != sizeof(struct full_sockaddr_ax25))
1900 return -EINVAL;
1901
1902 ax25_parse_addr(skb->data, skb->len, NULL, &dest, &digi, NULL);
1903
1904 sax->sax25_family = AF_AX25;
1905
1906
1907
1908 sax->sax25_ndigis = digi.ndigi;
1909 memcpy(&sax->sax25_call, &dest, sizeof(ax25_address));
1910
1911 *addr_len = sizeof(struct sockaddr_ax25);
1912
1913 if (*addr_len == sizeof(struct full_sockaddr_ax25) && sax->sax25_ndigis != 0) {
1914 int ct = 0;
1915 struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)sax;
1916
1917 while (ct < digi.ndigi) {
1918 memcpy(&fsa->fsa_digipeater[ct], &digi.calls[ct], sizeof(ax25_address));
1919 ct++;
1920 }
1921
1922 *addr_len = sizeof(struct full_sockaddr_ax25);
1923 }
1924 }
1925
1926 skb_free_datagram(skb);
1927
1928 return copied;
1929 }
1930
1931 static int ax25_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, unsigned flags,
1932 struct sockaddr *sa, int *addr_len)
1933 {
1934 struct iovec iov;
1935 struct msghdr msg;
1936
1937 iov.iov_base = ubuf;
1938 iov.iov_len = size;
1939
1940 msg.msg_name = (void *)sa;
1941 msg.msg_namelen = 0;
1942 if (addr_len)
1943 msg.msg_namelen = *addr_len;
1944 msg.msg_accrights = NULL;
1945 msg.msg_iov = &iov;
1946 msg.msg_iovlen = 1;
1947
1948 return ax25_recvmsg(sock, &msg, size, noblock, flags, addr_len);
1949 }
1950
1951 static int ax25_recv(struct socket *sock, void *ubuf, int size , int noblock,
1952 unsigned flags)
1953 {
1954 struct sock *sk = (struct sock *)sock->data;
1955
1956 if (sk->zapped)
1957 return -ENOTCONN;
1958
1959 return ax25_recvfrom(sock, ubuf, size, noblock, flags, NULL, NULL);
1960 }
1961
1962 static int ax25_read(struct socket *sock, char *ubuf, int size, int noblock)
1963 {
1964 return ax25_recv(sock, ubuf, size, noblock, 0);
1965 }
1966
1967 static int ax25_shutdown(struct socket *sk, int how)
1968 {
1969
1970 return -EOPNOTSUPP;
1971 }
1972
1973 static int ax25_select(struct socket *sock , int sel_type, select_table *wait)
1974 {
1975 struct sock *sk = (struct sock *)sock->data;
1976
1977 return datagram_select(sk, sel_type, wait);
1978 }
1979
1980 static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1981 {
1982 struct sock *sk = (struct sock *)sock->data;
1983 int err;
1984 long amount = 0;
1985
1986 switch (cmd) {
1987 case TIOCOUTQ:
1988 if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned long))) != 0)
1989 return err;
1990 amount = sk->sndbuf - sk->wmem_alloc;
1991 if (amount < 0)
1992 amount = 0;
1993 put_fs_long(amount, (unsigned long *)arg);
1994 return 0;
1995
1996 case TIOCINQ:
1997 {
1998 struct sk_buff *skb;
1999
2000 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
2001 amount = skb->len;
2002 if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned long))) != 0)
2003 return err;
2004 put_fs_long(amount, (unsigned long *)arg);
2005 return 0;
2006 }
2007
2008 case SIOCGSTAMP:
2009 if (sk != NULL) {
2010 if (sk->stamp.tv_sec==0)
2011 return -ENOENT;
2012 if ((err = verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval))) != 0)
2013 return err;
2014 memcpy_tofs((void *)arg, &sk->stamp, sizeof(struct timeval));
2015 return 0;
2016 }
2017 return -EINVAL;
2018
2019 case SIOCAX25ADDUID:
2020 case SIOCAX25DELUID:
2021 case SIOCAX25GETUID:
2022 {
2023 struct sockaddr_ax25 sax25;
2024 if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(struct sockaddr_ax25))) != 0)
2025 return err;
2026 memcpy_fromfs(&sax25, (void *)arg, sizeof(sax25));
2027 return ax25_uid_ioctl(cmd, &sax25);
2028 }
2029
2030 case SIOCAX25NOUID:
2031 if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(unsigned long))) != 0)
2032 return err;
2033 if(!suser())
2034 return -EPERM;
2035 amount = get_fs_long((void *)arg);
2036 if (amount > AX25_NOUID_BLOCK)
2037 return -EINVAL;
2038 ax25_uid_policy = amount;
2039 return 0;
2040
2041 case SIOCAX25GETPARMS:
2042 case SIOCAX25SETPARMS:
2043 return ax25_dev_ioctl(cmd, (void *)arg);
2044
2045 case SIOCADDRT:
2046 case SIOCDELRT:
2047 if (!suser())
2048 return -EPERM;
2049 return ax25_rt_ioctl(cmd, (void *)arg);
2050
2051 case SIOCGIFADDR:
2052 case SIOCSIFADDR:
2053 case SIOCGIFDSTADDR:
2054 case SIOCSIFDSTADDR:
2055 case SIOCGIFBRDADDR:
2056 case SIOCSIFBRDADDR:
2057 case SIOCGIFNETMASK:
2058 case SIOCSIFNETMASK:
2059 case SIOCGIFMETRIC:
2060 case SIOCSIFMETRIC:
2061 return -EINVAL;
2062
2063 default:
2064 return(dev_ioctl(cmd, (void *)arg));
2065 }
2066
2067
2068 return(0);
2069 }
2070
2071 static int ax25_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
2072 {
2073 ax25_cb *ax25;
2074 struct device *dev;
2075 const char *devname;
2076 int len = 0;
2077 off_t pos = 0;
2078 off_t begin = 0;
2079
2080 cli();
2081
2082 len += sprintf(buffer, "dest_addr src_addr dev st vs vr va t1 t2 t3 n2 rtt wnd Snd-Q Rcv-Q\n");
2083
2084 for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
2085 if ((dev = ax25->device) == NULL)
2086 devname = "???";
2087 else
2088 devname = dev->name;
2089
2090 len += sprintf(buffer + len, "%-9s ",
2091 ax2asc(&ax25->dest_addr));
2092 len += sprintf(buffer + len, "%-9s %-4s %2d %3d %3d %3d %3d/%03d %2d/%02d %3d/%03d %2d/%02d %3d %3d",
2093 ax2asc(&ax25->source_addr), devname,
2094 ax25->state,
2095 ax25->vs, ax25->vr, ax25->va,
2096 ax25->t1timer / PR_SLOWHZ,
2097 ax25->t1 / PR_SLOWHZ,
2098 ax25->t2timer / PR_SLOWHZ,
2099 ax25->t2 / PR_SLOWHZ,
2100 ax25->t3timer / PR_SLOWHZ,
2101 ax25->t3 / PR_SLOWHZ,
2102 ax25->n2count, ax25->n2,
2103 ax25->rtt / PR_SLOWHZ,
2104 ax25->window);
2105
2106 if (ax25->sk != NULL) {
2107 len += sprintf(buffer + len, " %5ld %5ld\n",
2108 ax25->sk->wmem_alloc,
2109 ax25->sk->rmem_alloc);
2110 } else {
2111 len += sprintf(buffer + len, "\n");
2112 }
2113
2114 pos = begin + len;
2115
2116 if (pos < offset) {
2117 len = 0;
2118 begin = pos;
2119 }
2120
2121 if (pos > offset + length)
2122 break;
2123 }
2124
2125 sti();
2126
2127 *start = buffer + (offset - begin);
2128 len -= (offset - begin);
2129
2130 if (len > length) len = length;
2131
2132 return(len);
2133 }
2134
2135 static struct proto_ops ax25_proto_ops = {
2136 AF_AX25,
2137
2138 ax25_create,
2139 ax25_dup,
2140 ax25_release,
2141 ax25_bind,
2142 ax25_connect,
2143 ax25_socketpair,
2144 ax25_accept,
2145 ax25_getname,
2146 ax25_read,
2147 ax25_write,
2148 ax25_select,
2149 ax25_ioctl,
2150 ax25_listen,
2151 ax25_send,
2152 ax25_recv,
2153 ax25_sendto,
2154 ax25_recvfrom,
2155 ax25_shutdown,
2156 ax25_setsockopt,
2157 ax25_getsockopt,
2158 ax25_fcntl,
2159 ax25_sendmsg,
2160 ax25_recvmsg
2161 };
2162
2163
2164
2165 static struct packet_type ax25_packet_type =
2166 {
2167 0,
2168 0,
2169 kiss_rcv,
2170 NULL,
2171 NULL,
2172 };
2173
2174 static struct packet_type bpq_packet_type =
2175 {
2176 0,
2177 0,
2178 bpq_rcv,
2179 NULL,
2180 NULL,
2181 };
2182
2183 static struct notifier_block ax25_dev_notifier = {
2184 ax25_device_event,
2185 0
2186 };
2187
2188 void ax25_proto_init(struct net_proto *pro)
2189 {
2190 sock_register(ax25_proto_ops.family, &ax25_proto_ops);
2191 ax25_packet_type.type = htons(ETH_P_AX25);
2192 dev_add_pack(&ax25_packet_type);
2193 bpq_packet_type.type = htons(ETH_P_BPQ);
2194 dev_add_pack(&bpq_packet_type);
2195 register_netdevice_notifier(&ax25_dev_notifier);
2196
2197 proc_net_register(&(struct proc_dir_entry) {
2198 PROC_NET_AX25_ROUTE, 10, "ax25_route",
2199 S_IFREG | S_IRUGO, 1, 0, 0,
2200 0, &proc_net_inode_operations,
2201 ax25_rt_get_info
2202 });
2203 proc_net_register(&(struct proc_dir_entry) {
2204 PROC_NET_AX25, 4, "ax25",
2205 S_IFREG | S_IRUGO, 1, 0, 0,
2206 0, &proc_net_inode_operations,
2207 ax25_get_info
2208 });
2209 proc_net_register(&(struct proc_dir_entry) {
2210 PROC_NET_AX25_CALLS, 10, "ax25_calls",
2211 S_IFREG | S_IRUGO, 1, 0, 0,
2212 0, &proc_net_inode_operations,
2213 ax25_cs_get_info
2214 });
2215
2216 printk("G4KLX/GW4PTS AX.25 for Linux. Version 0.30 BETA for Linux NET3.032 (Linux 1.3.35)\n");
2217 }
2218
2219
2220
2221
2222
2223
2224 void ax25_queue_xmit(struct sk_buff *skb, struct device *dev, int pri)
2225 {
2226 static char bcast_addr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2227 unsigned char *ptr;
2228 int size;
2229
2230 #ifdef CONFIG_FIREWALL
2231
2232 if(call_out_firewall(PF_AX25, skb, skb->data)!=FW_ACCEPT)
2233 {
2234 kfree_skb(skb, FREE_WRITE);
2235 return;
2236 }
2237
2238 #endif
2239
2240 if (dev->type == ARPHRD_ETHER)
2241 {
2242 if (skb_headroom(skb) < AX25_BPQ_HEADER_LEN)
2243 {
2244 printk("ax25_queue_xmit: not enough space to add BPQ Ether header\n");
2245 skb->free = 1;
2246 kfree_skb(skb, FREE_WRITE);
2247 return;
2248 }
2249
2250 size = skb->len;
2251
2252 ptr = skb_push(skb, 2);
2253
2254 *ptr++ = (size + 5) % 256;
2255 *ptr++ = (size + 5) / 256;
2256
2257 dev->hard_header(skb, dev, ETH_P_BPQ, bcast_addr, NULL, 0);
2258 }
2259 else
2260 {
2261 ptr = skb_push(skb, 1);
2262 *ptr++ = 0;
2263 }
2264
2265 dev_queue_xmit(skb, dev, pri);
2266 }
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279 #ifdef CONFIG_INET
2280
2281 int ax25_encapsulate(struct sk_buff *skb, struct device *dev, unsigned short type, void *daddr,
2282 void *saddr, unsigned len)
2283 {
2284
2285 unsigned char *buff = skb_push(skb, AX25_HEADER_LEN);
2286
2287 *buff++ = 0;
2288
2289 if (daddr != NULL)
2290 memcpy(buff, daddr, dev->addr_len);
2291
2292 buff[6] &= ~LAPB_C;
2293 buff[6] &= ~LAPB_E;
2294 buff[6] |= SSSID_SPARE;
2295 buff += AX25_ADDR_LEN;
2296
2297 if (saddr != NULL)
2298 memcpy(buff, saddr, dev->addr_len);
2299 else
2300 memcpy(buff, dev->dev_addr, dev->addr_len);
2301
2302 buff[6] &= ~LAPB_C;
2303 buff[6] |= LAPB_E;
2304 buff[6] |= SSSID_SPARE;
2305 buff += AX25_ADDR_LEN;
2306
2307 *buff++ = LAPB_UI;
2308
2309
2310 switch (type) {
2311 case ETH_P_IP:
2312 *buff++ = AX25_P_IP;
2313 break;
2314
2315 case ETH_P_ARP:
2316 *buff++ = AX25_P_ARP;
2317 break;
2318 default:
2319 *buff++ = 0;
2320 break;
2321 }
2322
2323 if (daddr != NULL)
2324 return AX25_HEADER_LEN;
2325
2326 return -AX25_HEADER_LEN;
2327 }
2328
2329 int ax25_rebuild_header(unsigned char *bp, struct device *dev, unsigned long dest, struct sk_buff *skb)
2330 {
2331 int mode;
2332
2333 if (arp_find(bp + 1, dest, dev, dev->pa_addr, skb))
2334 return 1;
2335
2336 if (bp[16] == AX25_P_IP)
2337 {
2338 mode = ax25_ip_mode_get((ax25_address *)(bp + 1), dev);
2339 if (mode == 'V' || mode == 'v' || (mode == ' ' && ax25_dev_get_value(dev, AX25_VALUES_IPDEFMODE) == 'V'))
2340 {
2341 skb_device_unlock(skb);
2342 skb_pull(skb, AX25_HEADER_LEN - 1);
2343 ax25_send_frame(skb, (ax25_address *)(bp + 8), (ax25_address *)(bp + 1), NULL, dev);
2344 return 1;
2345 }
2346 }
2347
2348 bp[7] &= ~LAPB_C;
2349 bp[7] &= ~LAPB_E;
2350 bp[7] |= SSSID_SPARE;
2351
2352 bp[14] &= ~LAPB_C;
2353 bp[14] |= LAPB_E;
2354 bp[14] |= SSSID_SPARE;
2355
2356 return 0;
2357 }
2358
2359 #endif
2360
2361 #endif