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