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