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