This source file includes following definitions.
- nr_remove_socket
- nr_kill_by_device
- nr_device_event
- nr_insert_socket
- nr_find_listener
- nr_find_socket
- nr_find_peer
- nr_destroy_timer
- nr_destroy_socket
- nr_fcntl
- nr_setsockopt
- nr_getsockopt
- nr_listen
- def_callback1
- def_callback2
- nr_create
- nr_make_new
- nr_dup
- nr_release
- nr_bind
- nr_connect
- nr_socketpair
- nr_accept
- nr_getname
- nr_rx_frame
- nr_sendmsg
- nr_sendto
- nr_send
- nr_write
- nr_recvmsg
- nr_recvfrom
- nr_recv
- nr_read
- nr_shutdown
- nr_select
- nr_ioctl
- nr_get_info
- nr_proto_init
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 #include <linux/config.h>
28 #ifdef CONFIG_NETROM
29 #include <linux/errno.h>
30 #include <linux/types.h>
31 #include <linux/socket.h>
32 #include <linux/in.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/timer.h>
36 #include <linux/string.h>
37 #include <linux/sockios.h>
38 #include <linux/net.h>
39 #include <linux/stat.h>
40 #include <net/ax25.h>
41 #include <linux/inet.h>
42 #include <linux/netdevice.h>
43 #include <linux/if_arp.h>
44 #include <linux/skbuff.h>
45 #include <net/sock.h>
46 #include <asm/segment.h>
47 #include <asm/system.h>
48 #include <linux/fcntl.h>
49 #include <linux/termios.h>
50 #include <linux/mm.h>
51 #include <linux/interrupt.h>
52 #include <linux/notifier.h>
53 #include <net/netrom.h>
54 #include <linux/proc_fs.h>
55 #include <net/ip.h>
56 #include <net/arp.h>
57 #include <linux/if_arp.h>
58 #include <linux/proc_fs.h>
59
60
61
62
63
64
65
66 struct nr_parms_struct nr_default;
67
68 static unsigned short circuit = 0x101;
69
70 static struct sock *volatile nr_list = NULL;
71
72
73
74
75 static void nr_remove_socket(struct sock *sk)
76 {
77 struct sock *s;
78 unsigned long flags;
79
80 save_flags(flags);
81 cli();
82
83 if ((s = nr_list) == sk) {
84 nr_list = s->next;
85 restore_flags(flags);
86 return;
87 }
88
89 while (s != NULL && s->next != NULL) {
90 if (s->next == sk) {
91 s->next = sk->next;
92 restore_flags(flags);
93 return;
94 }
95
96 s = s->next;
97 }
98
99 restore_flags(flags);
100 }
101
102
103
104
105 static void nr_kill_by_device(struct device *dev)
106 {
107 struct sock *s;
108
109 for (s = nr_list; s != NULL; s = s->next) {
110 if (s->nr->device == dev) {
111 s->nr->state = NR_STATE_0;
112 s->nr->device = NULL;
113 s->state = TCP_CLOSE;
114 s->err = ENETUNREACH;
115 s->state_change(s);
116 s->dead = 1;
117 }
118 }
119 }
120
121
122
123
124 static int nr_device_event(unsigned long event, void *ptr)
125 {
126 struct device *dev = (struct device *)ptr;
127
128 if (event != NETDEV_DOWN)
129 return NOTIFY_DONE;
130
131 nr_kill_by_device(dev);
132 nr_rt_device_down(dev);
133
134 return NOTIFY_DONE;
135 }
136
137
138
139
140 static void nr_insert_socket(struct sock *sk)
141 {
142 unsigned long flags;
143
144 save_flags(flags);
145 cli();
146
147 sk->next = nr_list;
148 nr_list = sk;
149
150 restore_flags(flags);
151 }
152
153
154
155
156
157 static struct sock *nr_find_listener(ax25_address *addr, int type)
158 {
159 unsigned long flags;
160 struct sock *s;
161
162 save_flags(flags);
163 cli();
164
165 for (s = nr_list; s != NULL; s = s->next) {
166 if (ax25cmp(&s->nr->source_addr, addr) == 0 && s->type == type && s->state == TCP_LISTEN) {
167 restore_flags(flags);
168 return s;
169 }
170 }
171
172 restore_flags(flags);
173 return NULL;
174 }
175
176
177
178
179 static struct sock *nr_find_socket(unsigned char index, unsigned char id, int type)
180 {
181 struct sock *s;
182 unsigned long flags;
183
184 save_flags(flags);
185 cli();
186
187 for (s = nr_list; s != NULL; s = s->next) {
188 if (s->nr->my_index == index && s->nr->my_id == id && s->type == type) {
189 restore_flags(flags);
190 return s;
191 }
192 }
193
194 restore_flags(flags);
195
196 return NULL;
197 }
198
199
200
201
202 static struct sock *nr_find_peer(unsigned char index, unsigned char id, int type)
203 {
204 struct sock *s;
205 unsigned long flags;
206
207 save_flags(flags);
208 cli();
209
210 for (s = nr_list; s != NULL; s = s->next) {
211 if (s->nr->your_index == index && s->nr->your_id == id && s->type == type) {
212 restore_flags(flags);
213 return s;
214 }
215 }
216
217 restore_flags(flags);
218
219 return NULL;
220 }
221
222
223
224
225 void nr_destory_socket(struct sock *);
226
227
228
229
230 static void nr_destroy_timer(unsigned long data)
231 {
232 nr_destroy_socket((struct sock *)data);
233 }
234
235
236
237
238
239
240
241 void nr_destroy_socket(struct sock *sk)
242 {
243 struct sk_buff *skb;
244 unsigned long flags;
245
246 save_flags(flags);
247 cli();
248
249 del_timer(&sk->timer);
250
251 nr_remove_socket(sk);
252 nr_clear_queues(sk);
253
254 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
255 if (skb->sk != sk) {
256 skb->sk->dead = 1;
257 nr_set_timer(skb->sk);
258 skb->sk->nr->state = NR_STATE_0;
259 }
260
261 kfree_skb(skb, FREE_READ);
262 }
263
264 if (sk->wmem_alloc || sk->rmem_alloc) {
265 init_timer(&sk->timer);
266 sk->timer.expires = jiffies + 10 * HZ;
267 sk->timer.function = nr_destroy_timer;
268 sk->timer.data = (unsigned long)sk;
269 add_timer(&sk->timer);
270 } else {
271 kfree_s(sk->nr, sizeof(*sk->nr));
272 kfree_s(sk, sizeof(*sk));
273 }
274
275 restore_flags(flags);
276 }
277
278
279
280
281
282
283
284 static int nr_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
285 {
286 switch(cmd)
287 {
288 default:
289 return(-EINVAL);
290 }
291 }
292
293 static int nr_setsockopt(struct socket *sock, int level, int optname,
294 char *optval, int optlen)
295 {
296 struct sock *sk;
297 int err, opt;
298
299 sk = (struct sock *)sock->data;
300
301 if (level == SOL_SOCKET)
302 return sock_setsockopt(sk, level, optname, optval, optlen);
303
304 if (level != SOL_NETROM)
305 return -EOPNOTSUPP;
306
307 if (optval == NULL)
308 return -EINVAL;
309
310 if ((err = verify_area(VERIFY_READ, optval, sizeof(int))) != 0)
311 return err;
312
313 opt = get_fs_long((unsigned long *)optval);
314
315 switch (optname) {
316 case NETROM_T1:
317 if (opt < 1)
318 return -EINVAL;
319 sk->nr->rtt = (opt * PR_SLOWHZ) / 2;
320 return 0;
321
322 case NETROM_T2:
323 if (opt < 1)
324 return -EINVAL;
325 sk->nr->t2 = opt * PR_SLOWHZ;
326 return 0;
327
328 case NETROM_N2:
329 if (opt < 1 || opt > 31)
330 return -EINVAL;
331 sk->nr->n2 = opt;
332 return 0;
333
334 case NETROM_HDRINCL:
335 sk->nr->hdrincl = opt ? 1 : 0;
336 return 0;
337
338 default:
339 return -ENOPROTOOPT;
340 }
341 }
342
343 static int nr_getsockopt(struct socket *sock, int level, int optname,
344 char *optval, int *optlen)
345 {
346 struct sock *sk;
347 int val = 0;
348 int err;
349
350 sk = (struct sock *)sock->data;
351
352 if (level == SOL_SOCKET)
353 return sock_getsockopt(sk, level, optname, optval, optlen);
354
355 if (level != SOL_NETROM)
356 return -EOPNOTSUPP;
357
358 switch (optname) {
359 case NETROM_T1:
360 val = (sk->nr->t1 * 2) / PR_SLOWHZ;
361 break;
362
363 case NETROM_T2:
364 val = sk->nr->t2 / PR_SLOWHZ;
365 break;
366
367 case NETROM_N2:
368 val = sk->nr->n2;
369 break;
370
371 case NETROM_HDRINCL:
372 val = sk->nr->hdrincl;
373 break;
374
375 default:
376 return -ENOPROTOOPT;
377 }
378
379 if ((err = verify_area(VERIFY_WRITE, optlen, sizeof(int))) != 0)
380 return err;
381
382 put_fs_long(sizeof(int), (unsigned long *)optlen);
383
384 if ((err = verify_area(VERIFY_WRITE, optval, sizeof(int))) != 0)
385 return err;
386
387 put_fs_long(val, (unsigned long *)optval);
388
389 return 0;
390 }
391
392 static int nr_listen(struct socket *sock, int backlog)
393 {
394 struct sock *sk = (struct sock *)sock->data;
395
396 if (sk->type == SOCK_SEQPACKET && sk->state != TCP_LISTEN) {
397 memset(&sk->nr->user_addr, '\0', sizeof(ax25_address));
398 sk->max_ack_backlog = backlog;
399 sk->state = TCP_LISTEN;
400 return 0;
401 }
402
403 return -EOPNOTSUPP;
404 }
405
406 static void def_callback1(struct sock *sk)
407 {
408 if (!sk->dead)
409 wake_up_interruptible(sk->sleep);
410 }
411
412 static void def_callback2(struct sock *sk, int len)
413 {
414 if (!sk->dead)
415 wake_up_interruptible(sk->sleep);
416 }
417
418 static int nr_create(struct socket *sock, int protocol)
419 {
420 struct sock *sk;
421 nr_cb *nr;
422
423 if ((sk = (struct sock *)kmalloc(sizeof(*sk), GFP_ATOMIC)) == NULL)
424 return -ENOMEM;
425
426 if ((nr = (nr_cb *)kmalloc(sizeof(*nr), GFP_ATOMIC)) == NULL) {
427 kfree_s(sk, sizeof(*sk));
428 return -ENOMEM;
429 }
430
431 sk->type = sock->type;
432
433 switch (sock->type) {
434 case SOCK_SEQPACKET:
435 break;
436 default:
437 kfree_s((void *)sk, sizeof(*sk));
438 kfree_s((void *)nr, sizeof(*nr));
439 return -ESOCKTNOSUPPORT;
440 }
441
442 skb_queue_head_init(&sk->receive_queue);
443 skb_queue_head_init(&sk->write_queue);
444 skb_queue_head_init(&sk->back_log);
445
446 init_timer(&sk->timer);
447
448 sk->socket = sock;
449 sk->protocol = protocol;
450 sk->dead = 0;
451 sk->next = NULL;
452 sk->broadcast = 0;
453 sk->allocation = GFP_KERNEL;
454 sk->rcvbuf = SK_RMEM_MAX;
455 sk->sndbuf = SK_WMEM_MAX;
456 sk->wmem_alloc = 0;
457 sk->rmem_alloc = 0;
458 sk->inuse = 0;
459 sk->debug = 0;
460 sk->prot = NULL;
461 sk->err = 0;
462 sk->localroute = 0;
463 sk->send_head = NULL;
464 sk->state = TCP_CLOSE;
465 sk->shutdown = 0;
466 sk->priority = SOPRI_NORMAL;
467 sk->ack_backlog = 0;
468 sk->mtu = NETROM_MTU;
469 sk->zapped = 1;
470 sk->window = nr_default.window;
471
472 sk->state_change = def_callback1;
473 sk->data_ready = def_callback2;
474 sk->write_space = def_callback1;
475 sk->error_report = def_callback1;
476
477 if (sock != NULL) {
478 sock->data = (void *)sk;
479 sk->sleep = sock->wait;
480 }
481
482 skb_queue_head_init(&nr->ack_queue);
483 skb_queue_head_init(&nr->reseq_queue);
484 skb_queue_head_init(&nr->frag_queue);
485
486 nr->my_index = 0;
487 nr->my_id = 0;
488 nr->rtt = nr_default.timeout;
489 nr->t1 = nr_default.timeout;
490 nr->t2 = nr_default.ack_delay;
491 nr->n2 = nr_default.tries;
492
493 nr->t1timer = 0;
494 nr->t2timer = 0;
495 nr->t4timer = 0;
496 nr->n2count = 0;
497
498 nr->va = 0;
499 nr->vr = 0;
500 nr->vs = 0;
501 nr->vl = 0;
502
503 nr->your_index = 0;
504 nr->your_id = 0;
505
506 nr->my_index = 0;
507 nr->my_id = 0;
508
509 nr->bpqext = 1;
510 nr->fraglen = 0;
511 nr->hdrincl = 0;
512 nr->state = NR_STATE_0;
513 nr->device = NULL;
514
515 memset(&nr->source_addr, '\0', sizeof(ax25_address));
516 memset(&nr->user_addr, '\0', sizeof(ax25_address));
517 memset(&nr->dest_addr, '\0', sizeof(ax25_address));
518
519 nr->sk = sk;
520 sk->nr = nr;
521
522 return 0;
523 }
524
525 static struct sock *nr_make_new(struct sock *osk)
526 {
527 struct sock *sk;
528 nr_cb *nr;
529
530 if ((sk = (struct sock *)kmalloc(sizeof(*sk), GFP_ATOMIC)) == NULL)
531 return NULL;
532
533 if ((nr = (nr_cb *)kmalloc(sizeof(*nr), GFP_ATOMIC)) == NULL) {
534 kfree_s(sk, sizeof(*sk));
535 return NULL;
536 }
537
538 sk->type = osk->type;
539 sk->socket = osk->socket;
540
541 switch (osk->type) {
542 case SOCK_SEQPACKET:
543 break;
544 default:
545 kfree_s((void *)sk, sizeof(*sk));
546 kfree_s((void *)nr, sizeof(*nr));
547 return NULL;
548 }
549
550 skb_queue_head_init(&sk->receive_queue);
551 skb_queue_head_init(&sk->write_queue);
552 skb_queue_head_init(&sk->back_log);
553
554 init_timer(&sk->timer);
555
556 sk->dead = 0;
557 sk->next = NULL;
558 sk->priority = osk->priority;
559 sk->broadcast = 0;
560 sk->protocol = osk->protocol;
561 sk->rcvbuf = osk->rcvbuf;
562 sk->sndbuf = osk->sndbuf;
563 sk->wmem_alloc = 0;
564 sk->rmem_alloc = 0;
565 sk->inuse = 0;
566 sk->ack_backlog = 0;
567 sk->prot = NULL;
568 sk->err = 0;
569 sk->localroute = 0;
570 sk->send_head = NULL;
571 sk->debug = osk->debug;
572 sk->state = TCP_ESTABLISHED;
573 sk->window = osk->window;
574 sk->shutdown = 0;
575 sk->mtu = osk->mtu;
576 sk->sleep = osk->sleep;
577 sk->zapped = osk->zapped;
578
579 sk->state_change = def_callback1;
580 sk->data_ready = def_callback2;
581 sk->write_space = def_callback1;
582 sk->error_report = def_callback1;
583
584 skb_queue_head_init(&nr->ack_queue);
585 skb_queue_head_init(&nr->reseq_queue);
586 skb_queue_head_init(&nr->frag_queue);
587
588 nr->rtt = osk->nr->rtt;
589 nr->t1 = osk->nr->t1;
590 nr->t2 = osk->nr->t2;
591 nr->n2 = osk->nr->n2;
592
593 nr->device = osk->nr->device;
594 nr->bpqext = osk->nr->bpqext;
595 nr->hdrincl = osk->nr->hdrincl;
596 nr->fraglen = 0;
597
598 nr->t1timer = 0;
599 nr->t2timer = 0;
600 nr->t4timer = 0;
601 nr->n2count = 0;
602
603 nr->va = 0;
604 nr->vr = 0;
605 nr->vs = 0;
606 nr->vl = 0;
607
608 sk->nr = nr;
609 nr->sk = sk;
610
611 return sk;
612 }
613
614 static int nr_dup(struct socket *newsock, struct socket *oldsock)
615 {
616 struct sock *sk = (struct sock *)oldsock->data;
617
618 return nr_create(newsock, sk->protocol);
619 }
620
621 static int nr_release(struct socket *sock, struct socket *peer)
622 {
623 struct sock *sk = (struct sock *)sock->data;
624
625 if (sk == NULL) return 0;
626
627 if (sk->type == SOCK_SEQPACKET) {
628 switch (sk->nr->state) {
629 case NR_STATE_0:
630 sk->dead = 1;
631 sk->state_change(sk);
632 nr_destroy_socket(sk);
633 break;
634
635 case NR_STATE_1:
636 sk->nr->state = NR_STATE_0;
637 sk->dead = 1;
638 sk->state_change(sk);
639 nr_destroy_socket(sk);
640 break;
641
642 case NR_STATE_2:
643 nr_write_internal(sk, NR_DISCACK);
644 sk->nr->state = NR_STATE_0;
645 sk->dead = 1;
646 sk->state_change(sk);
647 nr_destroy_socket(sk);
648 break;
649
650 case NR_STATE_3:
651 nr_clear_queues(sk);
652 sk->nr->n2count = 0;
653 nr_write_internal(sk, NR_DISCREQ);
654 sk->nr->t1timer = sk->nr->t1 = nr_calculate_t1(sk);
655 sk->nr->t2timer = 0;
656 sk->nr->t4timer = 0;
657 sk->nr->state = NR_STATE_2;
658 sk->state_change(sk);
659 sk->dead = 1;
660 break;
661
662 default:
663 break;
664 }
665 } else {
666 sk->dead = 1;
667 sk->state_change(sk);
668 nr_destroy_socket(sk);
669 }
670
671 sock->data = NULL;
672
673 return 0;
674 }
675
676 static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
677 {
678 struct sock *sk;
679 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
680 struct device *dev;
681 ax25_address *user, *source;
682
683 sk = (struct sock *)sock->data;
684
685 if (sk->zapped == 0)
686 return -EIO;
687
688 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
689 return -EINVAL;
690
691 #ifdef DONTDO
692 if (nr_find_listener(&addr->fsa_ax25.sax25_call, sk->type) != NULL) {
693 if (sk->debug)
694 printk("NET/ROM: bind failed: in use\n");
695 return -EADDRINUSE;
696 }
697 #endif
698
699 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
700 if (sk->debug)
701 printk("NET/ROM: bind failed: invalid node callsign\n");
702 return -EADDRNOTAVAIL;
703 }
704
705
706
707
708 if (addr->fsa_ax25.sax25_ndigis == 1) {
709 if (!suser())
710 return -EPERM;
711 memcpy(&sk->nr->user_addr, &addr->fsa_digipeater[0], sizeof(ax25_address));
712 memcpy(&sk->nr->source_addr, &addr->fsa_ax25.sax25_call, sizeof(ax25_address));
713 } else {
714 source = &addr->fsa_ax25.sax25_call;
715
716 if ((user = ax25_findbyuid(current->euid)) == NULL) {
717 if (ax25_uid_policy && !suser())
718 return -EPERM;
719 user = source;
720 }
721
722 memcpy(&sk->nr->user_addr, user, sizeof(ax25_address));
723 memcpy(&sk->nr->source_addr, source, sizeof(ax25_address));
724 }
725
726 sk->nr->device = dev;
727 nr_insert_socket(sk);
728
729 sk->zapped = 0;
730
731 if (sk->debug)
732 printk("NET/ROM: socket is bound\n");
733
734 return 0;
735 }
736
737 static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
738 int addr_len, int flags)
739 {
740 struct sock *sk = (struct sock *)sock->data;
741 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
742 ax25_address *user, *source = NULL;
743 struct device *dev;
744
745 if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
746 sock->state = SS_CONNECTED;
747 return 0;
748 }
749
750 if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
751 sock->state = SS_UNCONNECTED;
752 return -ECONNREFUSED;
753 }
754
755 if (sk->state == TCP_ESTABLISHED && sk->type == SOCK_SEQPACKET)
756 return -EISCONN;
757
758 sk->state = TCP_CLOSE;
759 sock->state = SS_UNCONNECTED;
760
761 if (addr_len != sizeof(struct sockaddr_ax25))
762 return -EINVAL;
763
764 if ((dev = nr_dev_first()) == NULL)
765 return -ENETUNREACH;
766
767 if (sk->zapped) {
768 sk->zapped = 0;
769
770 source = (ax25_address *)dev->dev_addr;
771
772 if ((user = ax25_findbyuid(current->euid)) == NULL) {
773 if (ax25_uid_policy && !suser())
774 return -EPERM;
775 user = source;
776 }
777
778 memcpy(&sk->nr->user_addr, user, sizeof(ax25_address));
779 memcpy(&sk->nr->source_addr, source, sizeof(ax25_address));
780
781 nr_insert_socket(sk);
782 }
783
784 memcpy(&sk->nr->dest_addr, &addr->sax25_call, sizeof(ax25_address));
785
786 sk->nr->my_index = circuit / 256;
787 sk->nr->my_id = circuit % 256;
788
789 circuit++;
790
791
792 sock->state = SS_CONNECTING;
793 sk->state = TCP_SYN_SENT;
794 nr_establish_data_link(sk);
795 sk->nr->state = NR_STATE_1;
796 nr_set_timer(sk);
797
798
799 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
800 return -EINPROGRESS;
801
802 cli();
803
804
805
806
807 while (sk->state == TCP_SYN_SENT) {
808 interruptible_sleep_on(sk->sleep);
809 if (current->signal & ~current->blocked) {
810 sti();
811 return -ERESTARTSYS;
812 }
813 }
814
815 if (sk->state != TCP_ESTABLISHED) {
816 sti();
817 sock->state = SS_UNCONNECTED;
818 return -sk->err;
819 }
820
821 sock->state = SS_CONNECTED;
822
823 sti();
824
825 return 0;
826 }
827
828 static int nr_socketpair(struct socket *sock1, struct socket *sock2)
829 {
830 return -EOPNOTSUPP;
831 }
832
833 static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
834 {
835 struct sock *sk;
836 struct sock *newsk;
837 struct sk_buff *skb;
838
839 if (newsock->data)
840 kfree_s(newsock->data, sizeof(struct sock));
841
842 newsock->data = NULL;
843
844 sk = (struct sock *)sock->data;
845
846 if (sk->type != SOCK_SEQPACKET)
847 return -EOPNOTSUPP;
848
849 if (sk->state != TCP_LISTEN)
850 return -EINVAL;
851
852
853
854 do {
855 cli();
856 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
857 if (flags & O_NONBLOCK) {
858 sti();
859 return 0;
860 }
861 interruptible_sleep_on(sk->sleep);
862 if (current->signal & ~current->blocked) {
863 sti();
864 return -ERESTARTSYS;
865 }
866 }
867 } while (skb == NULL);
868
869 newsk = skb->sk;
870 newsk->pair = NULL;
871 sti();
872
873
874 skb->sk = NULL;
875 kfree_skb(skb, FREE_READ);
876 sk->ack_backlog--;
877 newsock->data = newsk;
878
879 return 0;
880 }
881
882 static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
883 int *uaddr_len, int peer)
884 {
885 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
886 struct sock *sk;
887
888 sk = (struct sock *)sock->data;
889
890 if (peer != 0) {
891 if (sk->state != TCP_ESTABLISHED)
892 return -ENOTCONN;
893 sax->fsa_ax25.sax25_family = AF_NETROM;
894 sax->fsa_ax25.sax25_ndigis = 1;
895 memcpy(&sax->fsa_ax25.sax25_call, &sk->nr->user_addr, sizeof(ax25_address));
896 memcpy(&sax->fsa_digipeater[0], &sk->nr->dest_addr, sizeof(ax25_address));
897 *uaddr_len = sizeof(struct sockaddr_ax25) + sizeof(ax25_address);
898 } else {
899 sax->fsa_ax25.sax25_family = AF_NETROM;
900 sax->fsa_ax25.sax25_ndigis = 0;
901 memcpy(&sax->fsa_ax25.sax25_call, &sk->nr->source_addr, sizeof(ax25_address));
902 *uaddr_len = sizeof(struct sockaddr_ax25);
903 }
904
905 return 0;
906 }
907
908 int nr_rx_frame(struct sk_buff *skb, struct device *dev)
909 {
910 struct sock *sk;
911 struct sock *make;
912 ax25_address *src, *dest, *user;
913 unsigned short circuit_index, circuit_id;
914 unsigned short frametype, window, timeout;
915
916 skb->sk = NULL;
917
918
919
920
921
922 src = (ax25_address *)(skb->data + 0);
923 dest = (ax25_address *)(skb->data + 7);
924
925 circuit_index = skb->data[15];
926 circuit_id = skb->data[16];
927 frametype = skb->data[19];
928
929 #ifdef CONFIG_INET
930
931
932
933 if ((frametype & 0x0F) == NR_PROTOEXT && circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
934 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
935 skb->h.raw = skb->data;
936
937 return nr_rx_ip(skb, dev);
938 }
939 #endif
940
941
942
943
944
945 if (((frametype & 0x0F) != NR_CONNREQ && (sk = nr_find_socket(circuit_index, circuit_id, SOCK_SEQPACKET)) != NULL) ||
946 ((frametype & 0x0F) == NR_CONNREQ && (sk = nr_find_peer(circuit_index, circuit_id, SOCK_SEQPACKET)) != NULL)) {
947 skb->h.raw = skb->data;
948
949 if ((frametype & 0x0F) == NR_CONNACK && skb->len == 22)
950 sk->nr->bpqext = 1;
951 else
952 sk->nr->bpqext = 0;
953
954 return nr_process_rx_frame(sk, skb);
955 }
956
957 if ((frametype & 0x0F) != NR_CONNREQ)
958 return 0;
959
960 sk = nr_find_listener(dest, SOCK_SEQPACKET);
961
962 if (sk == NULL || sk->ack_backlog == sk->max_ack_backlog || (make = nr_make_new(sk)) == NULL) {
963 nr_transmit_dm(skb);
964 return 0;
965 }
966
967 user = (ax25_address *)(skb->data + 21);
968 window = skb->data[20];
969
970 skb->sk = make;
971 make->state = TCP_ESTABLISHED;
972
973
974 memcpy(&make->nr->source_addr, dest, sizeof(ax25_address));
975 memcpy(&make->nr->dest_addr, src, sizeof(ax25_address));
976 memcpy(&make->nr->user_addr, user, sizeof(ax25_address));
977
978 make->nr->your_index = circuit_index;
979 make->nr->your_id = circuit_id;
980
981 make->nr->my_index = circuit / 256;
982 make->nr->my_id = circuit % 256;
983
984 circuit++;
985
986
987 if (window < make->window)
988 make->window = window;
989
990
991 if (skb->len == 37) {
992 timeout = skb->data[36] * 256 + skb->data[35];
993 if (timeout * PR_SLOWHZ < make->nr->rtt * 2)
994 make->nr->rtt = (timeout * PR_SLOWHZ) / 2;
995 make->nr->bpqext = 1;
996 } else {
997 make->nr->bpqext = 0;
998 }
999
1000 nr_write_internal(make, NR_CONNACK);
1001
1002 make->nr->condition = 0x00;
1003 make->nr->vs = 0;
1004 make->nr->va = 0;
1005 make->nr->vr = 0;
1006 make->nr->vl = 0;
1007 make->nr->state = NR_STATE_3;
1008 sk->ack_backlog++;
1009 make->pair = sk;
1010
1011 nr_insert_socket(make);
1012
1013 skb_queue_head(&sk->receive_queue, skb);
1014
1015 nr_set_timer(make);
1016
1017 if (!sk->dead)
1018 sk->data_ready(sk, skb->len);
1019
1020 return 1;
1021 }
1022
1023 static int nr_sendmsg(struct socket *sock, struct msghdr *msg, int len, int noblock, int flags)
1024 {
1025 struct sock *sk = (struct sock *)sock->data;
1026 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1027 int err;
1028 struct sockaddr_ax25 sax;
1029 struct sk_buff *skb;
1030 unsigned char *asmptr;
1031 int size;
1032
1033 if (sk->err) {
1034 err = sk->err;
1035 sk->err = 0;
1036 return -err;
1037 }
1038
1039 if (flags)
1040 return -EINVAL;
1041
1042 if (sk->zapped)
1043 return -EADDRNOTAVAIL;
1044
1045 if (sk->nr->device == NULL)
1046 return -ENETUNREACH;
1047
1048 if (usax) {
1049 if (msg->msg_namelen < sizeof(sax))
1050 return -EINVAL;
1051 memcpy(&sax, usax, sizeof(sax));
1052 if (sk->type == SOCK_SEQPACKET && memcmp(&sk->nr->dest_addr, &sax.sax25_call, sizeof(ax25_address)) != 0)
1053 return -EISCONN;
1054 if (sax.sax25_family != AF_NETROM)
1055 return -EINVAL;
1056 } else {
1057 if (sk->state != TCP_ESTABLISHED)
1058 return -ENOTCONN;
1059 sax.sax25_family = AF_NETROM;
1060 memcpy(&sax.sax25_call, &sk->nr->dest_addr, sizeof(ax25_address));
1061 }
1062
1063 if (sk->debug)
1064 printk("NET/ROM: sendto: Addresses built.\n");
1065
1066
1067 if (sk->debug)
1068 printk("NET/ROM: sendto: building packet.\n");
1069
1070 size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 3 + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1071
1072 if ((skb = sock_alloc_send_skb(sk, size, 0, 0, &err)) == NULL)
1073 return err;
1074
1075 skb->sk = sk;
1076 skb->free = 1;
1077 skb->arp = 1;
1078
1079 skb_reserve(skb, size - len);
1080
1081
1082
1083
1084
1085 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1086
1087 if (sk->debug)
1088 printk("Building NET/ROM Header.\n");
1089
1090
1091
1092 *asmptr++ = sk->nr->your_index;
1093 *asmptr++ = sk->nr->your_id;
1094 *asmptr++ = 0;
1095 *asmptr++ = 0;
1096 *asmptr++ = NR_INFO;
1097
1098 if (sk->debug)
1099 printk("Built header.\n");
1100
1101
1102
1103
1104
1105 skb->h.raw = skb_put(skb, len);
1106
1107 asmptr = skb->h.raw;
1108
1109 if (sk->debug)
1110 printk("NET/ROM: Appending user data\n");
1111
1112
1113 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1114
1115 if (sk->debug)
1116 printk("NET/ROM: Transmitting buffer\n");
1117
1118 if (sk->state != TCP_ESTABLISHED) {
1119 kfree_skb(skb, FREE_WRITE);
1120 return -ENOTCONN;
1121 }
1122
1123 nr_output(sk, skb);
1124
1125 return len;
1126 }
1127
1128 static int nr_sendto(struct socket *sock, const void *ubuf, int size, int noblock, unsigned flags,
1129 struct sockaddr *sa, int addr_len)
1130 {
1131 struct iovec iov;
1132 struct msghdr msg;
1133 iov.iov_base=(void *)ubuf;
1134 iov.iov_len=size;
1135 msg.msg_name=(void *)sa;
1136 msg.msg_namelen=addr_len;
1137 msg.msg_accrights=NULL;
1138 msg.msg_iov=&iov;
1139 msg.msg_iovlen=1;
1140 return nr_sendmsg(sock,&msg,size,noblock,flags);
1141 }
1142
1143 static int nr_send(struct socket *sock, const void *ubuf, int size, int noblock, unsigned flags)
1144 {
1145 return nr_sendto(sock, ubuf, size, noblock, flags, NULL, 0);
1146 }
1147
1148 static int nr_write(struct socket *sock, const char *ubuf, int size, int noblock)
1149 {
1150 return nr_sendto(sock, ubuf, size, noblock, 0, NULL, 0);
1151 }
1152
1153 static int nr_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock,
1154 int flags, int *addr_len)
1155 {
1156 struct sock *sk = (struct sock *)sock->data;
1157 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1158 int copied;
1159 struct sk_buff *skb;
1160 int er;
1161
1162 if (sk->err) {
1163 cli();
1164 er = -sk->err;
1165 sk->err = 0;
1166 sti();
1167 return er;
1168 }
1169
1170 if (addr_len != NULL)
1171 *addr_len = sizeof(*sax);
1172
1173
1174
1175
1176
1177 if (sk->type == SOCK_SEQPACKET && sk->state != TCP_ESTABLISHED)
1178 return -ENOTCONN;
1179
1180
1181 if ((skb = skb_recv_datagram(sk, flags, noblock, &er)) == NULL)
1182 return er;
1183
1184 if (!sk->nr->hdrincl) {
1185 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
1186 skb->h.raw = skb->data;
1187 }
1188
1189 copied = (size < skb->len) ? size : skb->len;
1190 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1191
1192 if (sax != NULL) {
1193 struct sockaddr_ax25 addr;
1194
1195 addr.sax25_family = AF_NETROM;
1196 memcpy(&addr.sax25_call, skb->data + 7, sizeof(ax25_address));
1197
1198 memcpy(sax, &addr, sizeof(*sax));
1199
1200 *addr_len = sizeof(*sax);
1201 }
1202
1203 skb_free_datagram(skb);
1204
1205 return copied;
1206 }
1207
1208 static int nr_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, unsigned flags,
1209 struct sockaddr *sa, int *addr_len)
1210 {
1211 struct iovec iov;
1212 struct msghdr msg;
1213 iov.iov_base=ubuf;
1214 iov.iov_len=size;
1215 msg.msg_name=(void *)sa;
1216 msg.msg_namelen=0;
1217 if (addr_len)
1218 msg.msg_namelen = *addr_len;
1219 msg.msg_accrights=NULL;
1220 msg.msg_iov=&iov;
1221 msg.msg_iovlen=1;
1222 return nr_recvmsg(sock,&msg,size,noblock,flags,addr_len);
1223 }
1224
1225
1226 static int nr_recv(struct socket *sock, void *ubuf, int size , int noblock,
1227 unsigned flags)
1228 {
1229 struct sock *sk = (struct sock *)sock->data;
1230
1231 if (sk->zapped)
1232 return -ENOTCONN;
1233
1234 return nr_recvfrom(sock, ubuf, size, noblock, flags, NULL, NULL);
1235 }
1236
1237 static int nr_read(struct socket *sock, char *ubuf, int size, int noblock)
1238 {
1239 return nr_recv(sock, ubuf, size, noblock, 0);
1240 }
1241
1242 static int nr_shutdown(struct socket *sk, int how)
1243 {
1244 return -EOPNOTSUPP;
1245 }
1246
1247 static int nr_select(struct socket *sock , int sel_type, select_table *wait)
1248 {
1249 struct sock *sk = (struct sock *)sock->data;
1250
1251 return datagram_select(sk, sel_type, wait);
1252 }
1253
1254 static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1255 {
1256 struct sock *sk = (struct sock *)sock->data;
1257 int err;
1258 long amount = 0;
1259
1260 switch (cmd) {
1261 case TIOCOUTQ:
1262 if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned long))) != 0)
1263 return err;
1264 amount = sk->sndbuf - sk->wmem_alloc;
1265 if (amount < 0)
1266 amount = 0;
1267 put_fs_long(amount, (unsigned long *)arg);
1268 return 0;
1269
1270 case TIOCINQ:
1271 {
1272 struct sk_buff *skb;
1273
1274 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1275 amount = skb->len - 20;
1276 if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned long))) != 0)
1277 return err;
1278 put_fs_long(amount, (unsigned long *)arg);
1279 return 0;
1280 }
1281
1282 case SIOCGSTAMP:
1283 if (sk != NULL) {
1284 if (sk->stamp.tv_sec==0)
1285 return -ENOENT;
1286 if ((err = verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval))) != 0)
1287 return err;
1288 memcpy_tofs((void *)arg, &sk->stamp, sizeof(struct timeval));
1289 return 0;
1290 }
1291 return -EINVAL;
1292
1293 case SIOCGIFADDR:
1294 case SIOCSIFADDR:
1295 case SIOCGIFDSTADDR:
1296 case SIOCSIFDSTADDR:
1297 case SIOCGIFBRDADDR:
1298 case SIOCSIFBRDADDR:
1299 case SIOCGIFNETMASK:
1300 case SIOCSIFNETMASK:
1301 case SIOCGIFMETRIC:
1302 case SIOCSIFMETRIC:
1303 return -EINVAL;
1304
1305 case SIOCADDRT:
1306 case SIOCDELRT:
1307 case SIOCNRDECOBS:
1308 case SIOCNRRTCTL:
1309 if (!suser()) return -EPERM;
1310 return nr_rt_ioctl(cmd, (void *)arg);
1311
1312 case SIOCNRGETPARMS:
1313 {
1314 struct nr_parms_struct nr_parms;
1315 if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct nr_parms_struct))) != 0)
1316 return err;
1317 memcpy_fromfs(&nr_parms, (void *)arg, sizeof(struct nr_parms_struct));
1318 nr_parms = nr_default;
1319 memcpy_tofs((void *)arg, &nr_parms, sizeof(struct nr_parms_struct));
1320 return 0;
1321 }
1322
1323 case SIOCNRSETPARMS:
1324 {
1325 struct nr_parms_struct nr_parms;
1326 if (!suser()) return -EPERM;
1327 if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(struct nr_parms_struct))) != 0)
1328 return err;
1329 memcpy_fromfs(&nr_parms, (void *)arg, sizeof(struct nr_parms_struct));
1330 nr_default = nr_parms;
1331 return 0;
1332 }
1333
1334 default:
1335 return dev_ioctl(cmd, (void *)arg);
1336 }
1337
1338
1339 return(0);
1340 }
1341
1342 static int nr_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1343 {
1344 struct sock *s;
1345 struct device *dev;
1346 const char *devname;
1347 int len = 0;
1348 off_t pos = 0;
1349 off_t begin = 0;
1350
1351 cli();
1352
1353 len += sprintf(buffer, "user_addr dest_node src_node dev my your st vs vr va t1 t2 n2 rtt wnd Snd-Q Rcv-Q\n");
1354
1355 for (s = nr_list; s != NULL; s = s->next) {
1356 if ((dev = s->nr->device) == NULL)
1357 devname = "???";
1358 else
1359 devname = dev->name;
1360
1361 len += sprintf(buffer + len, "%-9s ",
1362 ax2asc(&s->nr->user_addr));
1363 len += sprintf(buffer + len, "%-9s ",
1364 ax2asc(&s->nr->dest_addr));
1365 len += sprintf(buffer + len, "%-9s %-3s %02X/%02X %02X/%02X %2d %2d %2d %2d %3d/%03d %2d/%02d %2d/%02d %3d %3d %5ld %5ld\n",
1366 ax2asc(&s->nr->source_addr),
1367 devname, s->nr->my_index, s->nr->my_id,
1368 s->nr->your_index, s->nr->your_id,
1369 s->nr->state,
1370 s->nr->vs, s->nr->vr, s->nr->va,
1371 s->nr->t1timer / PR_SLOWHZ,
1372 s->nr->t1 / PR_SLOWHZ,
1373 s->nr->t2timer / PR_SLOWHZ,
1374 s->nr->t2 / PR_SLOWHZ,
1375 s->nr->n2count, s->nr->n2,
1376 s->nr->rtt / PR_SLOWHZ,
1377 s->window,
1378 s->wmem_alloc, s->rmem_alloc);
1379
1380 pos = begin + len;
1381
1382 if (pos < offset) {
1383 len = 0;
1384 begin = pos;
1385 }
1386
1387 if (pos > offset + length)
1388 break;
1389 }
1390
1391 sti();
1392
1393 *start = buffer + (offset - begin);
1394 len -= (offset - begin);
1395
1396 if (len > length) len = length;
1397
1398 return(len);
1399 }
1400
1401 static struct proto_ops nr_proto_ops = {
1402 AF_NETROM,
1403
1404 nr_create,
1405 nr_dup,
1406 nr_release,
1407 nr_bind,
1408 nr_connect,
1409 nr_socketpair,
1410 nr_accept,
1411 nr_getname,
1412 nr_read,
1413 nr_write,
1414 nr_select,
1415 nr_ioctl,
1416 nr_listen,
1417 nr_send,
1418 nr_recv,
1419 nr_sendto,
1420 nr_recvfrom,
1421 nr_shutdown,
1422 nr_setsockopt,
1423 nr_getsockopt,
1424 nr_fcntl,
1425 nr_sendmsg,
1426 nr_recvmsg
1427 };
1428
1429 static struct notifier_block nr_dev_notifier = {
1430 nr_device_event,
1431 0
1432 };
1433
1434 void nr_proto_init(struct net_proto *pro)
1435 {
1436 sock_register(nr_proto_ops.family, &nr_proto_ops);
1437 register_netdevice_notifier(&nr_dev_notifier);
1438 printk("G4KLX NET/ROM for Linux. Version 0.3 ALPHA for AX25.030 Linux 1.3.25\n");
1439
1440 nr_default.quality = NR_DEFAULT_QUAL;
1441 nr_default.obs_count = NR_DEFAULT_OBS;
1442 nr_default.ttl = NR_DEFAULT_TTL;
1443 nr_default.timeout = NR_DEFAULT_T1;
1444 nr_default.ack_delay = NR_DEFAULT_T2;
1445 nr_default.busy_delay = NR_DEFAULT_T4;
1446 nr_default.tries = NR_DEFAULT_N2;
1447 nr_default.window = NR_DEFAULT_WINDOW;
1448
1449 proc_net_register(&(struct proc_dir_entry) {
1450 PROC_NET_NR, 2, "nr",
1451 S_IFREG | S_IRUGO, 1, 0, 0,
1452 0, &proc_net_inode_operations,
1453 nr_get_info
1454 });
1455 proc_net_register(&(struct proc_dir_entry) {
1456 PROC_NET_NR_NEIGH, 8, "nr_neigh",
1457 S_IFREG | S_IRUGO, 1, 0, 0,
1458 0, &proc_net_inode_operations,
1459 nr_neigh_get_info
1460 });
1461 proc_net_register(&(struct proc_dir_entry) {
1462 PROC_NET_NR_NODES, 8, "nr_nodes",
1463 S_IFREG | S_IRUGO, 1, 0, 0,
1464 0, &proc_net_inode_operations,
1465 nr_nodes_get_info
1466 });
1467 }
1468
1469 #endif