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