1 /*
2 * NET/ROM release 007
3 *
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
5 *
6 * This module:
7 * This module is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * History
13 * NET/ROM 001 Jonathan(G4KLX) Cloned from the AX25 code.
14 * NET/ROM 002 Darryl(G7LED) Fixes and address enhancement.
15 * Jonathan(G4KLX) Complete bind re-think.
16 * Alan(GW4PTS) Trivial tweaks into new format.
17 * NET/ROM 003 Jonathan(G4KLX) Added G8BPQ extensions.
18 * Added NET/ROM routing ioctl.
19 * Darryl(G7LED) Fix autobinding (on connect).
20 * Fixed nr_release(), set TCP_CLOSE, wakeup app
21 * context, THEN make the sock dead.
22 * Circuit ID check before allocating it on
23 * a connection.
24 * Alan(GW4PTS) sendmsg/recvmsg only. Fixed connect clear bug
25 * inherited from AX.25
26 * NET/ROM 004 Jonathan(G4KLX) Converted to module.
27 * NET/ROM 005 Jonathan(G4KLX) Linux 2.1
28 * Alan(GW4PTS) Started POSIXisms
29 * NET/ROM 006 Alan(GW4PTS) Brought in line with the ANK changes
30 * Jonathan(G4KLX) Removed hdrincl.
31 * NET/ROM 007 Jonathan(G4KLX) New timer architecture.
32 * Impmented Idle timer.
33 * Arnaldo C. Melo s/suser/capable/, micro cleanups
34 * Jeroen(PE1RXQ) Use sock_orphan() on release.
35 * Tomi(OH2BNS) Better frame type checking.
36 * Device refcnt fixes.
37 */
38
39 #include <linux/config.h>
40 #include <linux/module.h>
41 #include <linux/errno.h>
42 #include <linux/types.h>
43 #include <linux/socket.h>
44 #include <linux/in.h>
45 #include <linux/kernel.h>
46 #include <linux/sched.h>
47 #include <linux/timer.h>
48 #include <linux/string.h>
49 #include <linux/sockios.h>
50 #include <linux/net.h>
51 #include <linux/stat.h>
52 #include <net/ax25.h>
53 #include <linux/inet.h>
54 #include <linux/netdevice.h>
55 #include <linux/if_arp.h>
56 #include <linux/skbuff.h>
57 #include <net/sock.h>
58 #include <asm/uaccess.h>
59 #include <asm/system.h>
60 #include <linux/fcntl.h>
61 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
62 #include <linux/mm.h>
63 #include <linux/interrupt.h>
64 #include <linux/notifier.h>
65 #include <net/netrom.h>
66 #include <linux/proc_fs.h>
67 #include <net/ip.h>
68 #include <net/arp.h>
69 #include <linux/init.h>
70
71 int nr_ndevs = 4;
72
73 int sysctl_netrom_default_path_quality = NR_DEFAULT_QUAL;
74 int sysctl_netrom_obsolescence_count_initialiser = NR_DEFAULT_OBS;
75 int sysctl_netrom_network_ttl_initialiser = NR_DEFAULT_TTL;
76 int sysctl_netrom_transport_timeout = NR_DEFAULT_T1;
77 int sysctl_netrom_transport_maximum_tries = NR_DEFAULT_N2;
78 int sysctl_netrom_transport_acknowledge_delay = NR_DEFAULT_T2;
79 int sysctl_netrom_transport_busy_delay = NR_DEFAULT_T4;
80 int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW;
81 int sysctl_netrom_transport_no_activity_timeout = NR_DEFAULT_IDLE;
82 int sysctl_netrom_routing_control = NR_DEFAULT_ROUTING;
83 int sysctl_netrom_link_fails_count = NR_DEFAULT_FAILS;
84
85 static unsigned short circuit = 0x101;
86
87 static struct sock *volatile nr_list;
88
89 static struct proto_ops nr_proto_ops;
90
nr_free_sock(struct sock * sk)91 static void nr_free_sock(struct sock *sk)
92 {
93 sk_free(sk);
94
95 MOD_DEC_USE_COUNT;
96 }
97
nr_alloc_sock(void)98 static struct sock *nr_alloc_sock(void)
99 {
100 struct sock *sk;
101 nr_cb *nr;
102
103 if ((sk = sk_alloc(PF_NETROM, GFP_ATOMIC, 1)) == NULL)
104 return NULL;
105
106 if ((nr = kmalloc(sizeof(*nr), GFP_ATOMIC)) == NULL) {
107 sk_free(sk);
108 return NULL;
109 }
110
111 MOD_INC_USE_COUNT;
112
113 memset(nr, 0x00, sizeof(*nr));
114
115 sk->protinfo.nr = nr;
116 nr->sk = sk;
117
118 return sk;
119 }
120
121 /*
122 * Socket removal during an interrupt is now safe.
123 */
nr_remove_socket(struct sock * sk)124 static void nr_remove_socket(struct sock *sk)
125 {
126 struct sock *s;
127 unsigned long flags;
128
129 save_flags(flags); cli();
130
131 if ((s = nr_list) == sk) {
132 nr_list = s->next;
133 dev_put(sk->protinfo.nr->device);
134 restore_flags(flags);
135 return;
136 }
137
138 while (s != NULL && s->next != NULL) {
139 if (s->next == sk) {
140 s->next = sk->next;
141 dev_put(sk->protinfo.nr->device);
142 restore_flags(flags);
143 return;
144 }
145
146 s = s->next;
147 }
148
149 restore_flags(flags);
150 }
151
152 /*
153 * Kill all bound sockets on a dropped device.
154 */
nr_kill_by_device(struct net_device * dev)155 static void nr_kill_by_device(struct net_device *dev)
156 {
157 struct sock *s;
158
159 for (s = nr_list; s != NULL; s = s->next) {
160 if (s->protinfo.nr->device == dev)
161 nr_disconnect(s, ENETUNREACH);
162 }
163 }
164
165 /*
166 * Handle device status changes.
167 */
nr_device_event(struct notifier_block * this,unsigned long event,void * ptr)168 static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
169 {
170 struct net_device *dev = (struct net_device *)ptr;
171
172 if (event != NETDEV_DOWN)
173 return NOTIFY_DONE;
174
175 nr_kill_by_device(dev);
176 nr_rt_device_down(dev);
177
178 return NOTIFY_DONE;
179 }
180
181 /*
182 * Add a socket to the bound sockets list.
183 */
nr_insert_socket(struct sock * sk)184 static void nr_insert_socket(struct sock *sk)
185 {
186 unsigned long flags;
187
188 save_flags(flags); cli();
189
190 sk->next = nr_list;
191 nr_list = sk;
192
193 restore_flags(flags);
194 }
195
196 /*
197 * Find a socket that wants to accept the Connect Request we just
198 * received.
199 */
nr_find_listener(ax25_address * addr)200 static struct sock *nr_find_listener(ax25_address *addr)
201 {
202 unsigned long flags;
203 struct sock *s;
204
205 save_flags(flags);
206 cli();
207
208 for (s = nr_list; s != NULL; s = s->next) {
209 if (ax25cmp(&s->protinfo.nr->source_addr, addr) == 0 && s->state == TCP_LISTEN) {
210 restore_flags(flags);
211 return s;
212 }
213 }
214
215 restore_flags(flags);
216 return NULL;
217 }
218
219 /*
220 * Find a connected NET/ROM socket given my circuit IDs.
221 */
nr_find_socket(unsigned char index,unsigned char id)222 static struct sock *nr_find_socket(unsigned char index, unsigned char id)
223 {
224 struct sock *s;
225 unsigned long flags;
226
227 save_flags(flags);
228 cli();
229
230 for (s = nr_list; s != NULL; s = s->next) {
231 if (s->protinfo.nr->my_index == index && s->protinfo.nr->my_id == id) {
232 restore_flags(flags);
233 return s;
234 }
235 }
236
237 restore_flags(flags);
238
239 return NULL;
240 }
241
242 /*
243 * Find a connected NET/ROM socket given their circuit IDs.
244 */
nr_find_peer(unsigned char index,unsigned char id,ax25_address * dest)245 static struct sock *nr_find_peer(unsigned char index, unsigned char id, ax25_address *dest)
246 {
247 struct sock *s;
248 unsigned long flags;
249
250 save_flags(flags);
251 cli();
252
253 for (s = nr_list; s != NULL; s = s->next) {
254 if (s->protinfo.nr->your_index == index && s->protinfo.nr->your_id == id && ax25cmp(&s->protinfo.nr->dest_addr, dest) == 0) {
255 restore_flags(flags);
256 return s;
257 }
258 }
259
260 restore_flags(flags);
261
262 return NULL;
263 }
264
265 /*
266 * Find next free circuit ID.
267 */
nr_find_next_circuit(void)268 static unsigned short nr_find_next_circuit(void)
269 {
270 unsigned short id = circuit;
271 unsigned char i, j;
272
273 for (;;) {
274 i = id / 256;
275 j = id % 256;
276
277 if (i != 0 && j != 0)
278 if (nr_find_socket(i, j) == NULL)
279 break;
280
281 id++;
282 }
283
284 return id;
285 }
286
287 /*
288 * Deferred destroy.
289 */
290 void nr_destroy_socket(struct sock *);
291
292 /*
293 * Handler for deferred kills.
294 */
nr_destroy_timer(unsigned long data)295 static void nr_destroy_timer(unsigned long data)
296 {
297 nr_destroy_socket((struct sock *)data);
298 }
299
300 /*
301 * This is called from user mode and the timers. Thus it protects itself against
302 * interrupt users but doesn't worry about being called during work.
303 * Once it is removed from the queue no interrupt or bottom half will
304 * touch it and we are (fairly 8-) ) safe.
305 */
nr_destroy_socket(struct sock * sk)306 void nr_destroy_socket(struct sock *sk) /* Not static as it's used by the timer */
307 {
308 struct sk_buff *skb;
309 unsigned long flags;
310
311 save_flags(flags); cli();
312
313 nr_stop_heartbeat(sk);
314 nr_stop_t1timer(sk);
315 nr_stop_t2timer(sk);
316 nr_stop_t4timer(sk);
317 nr_stop_idletimer(sk);
318
319 nr_remove_socket(sk);
320 nr_clear_queues(sk); /* Flush the queues */
321
322 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
323 if (skb->sk != sk) { /* A pending connection */
324 skb->sk->dead = 1; /* Queue the unaccepted socket for death */
325 nr_start_heartbeat(skb->sk);
326 skb->sk->protinfo.nr->state = NR_STATE_0;
327 }
328
329 kfree_skb(skb);
330 }
331
332 if (atomic_read(&sk->wmem_alloc) != 0 || atomic_read(&sk->rmem_alloc) != 0) {
333 /* Defer: outstanding buffers */
334 init_timer(&sk->timer);
335 sk->timer.expires = jiffies + 10 * HZ;
336 sk->timer.function = nr_destroy_timer;
337 sk->timer.data = (unsigned long)sk;
338 add_timer(&sk->timer);
339 } else {
340 nr_free_sock(sk);
341 }
342
343 restore_flags(flags);
344 }
345
346 /*
347 * Handling for system calls applied via the various interfaces to a
348 * NET/ROM socket object.
349 */
350
nr_setsockopt(struct socket * sock,int level,int optname,char * optval,int optlen)351 static int nr_setsockopt(struct socket *sock, int level, int optname,
352 char *optval, int optlen)
353 {
354 struct sock *sk = sock->sk;
355 int opt;
356
357 if (level != SOL_NETROM)
358 return -ENOPROTOOPT;
359
360 if (optlen < sizeof(int))
361 return -EINVAL;
362
363 if (get_user(opt, (int *)optval))
364 return -EFAULT;
365
366 switch (optname) {
367 case NETROM_T1:
368 if (opt < 1)
369 return -EINVAL;
370 sk->protinfo.nr->t1 = opt * HZ;
371 return 0;
372
373 case NETROM_T2:
374 if (opt < 1)
375 return -EINVAL;
376 sk->protinfo.nr->t2 = opt * HZ;
377 return 0;
378
379 case NETROM_N2:
380 if (opt < 1 || opt > 31)
381 return -EINVAL;
382 sk->protinfo.nr->n2 = opt;
383 return 0;
384
385 case NETROM_T4:
386 if (opt < 1)
387 return -EINVAL;
388 sk->protinfo.nr->t4 = opt * HZ;
389 return 0;
390
391 case NETROM_IDLE:
392 if (opt < 0)
393 return -EINVAL;
394 sk->protinfo.nr->idle = opt * 60 * HZ;
395 return 0;
396
397 default:
398 return -ENOPROTOOPT;
399 }
400 }
401
nr_getsockopt(struct socket * sock,int level,int optname,char * optval,int * optlen)402 static int nr_getsockopt(struct socket *sock, int level, int optname,
403 char *optval, int *optlen)
404 {
405 struct sock *sk = sock->sk;
406 int val = 0;
407 int len;
408
409 if (level != SOL_NETROM)
410 return -ENOPROTOOPT;
411
412 if (get_user(len, optlen))
413 return -EFAULT;
414
415 if (len < 0)
416 return -EINVAL;
417
418 switch (optname) {
419 case NETROM_T1:
420 val = sk->protinfo.nr->t1 / HZ;
421 break;
422
423 case NETROM_T2:
424 val = sk->protinfo.nr->t2 / HZ;
425 break;
426
427 case NETROM_N2:
428 val = sk->protinfo.nr->n2;
429 break;
430
431 case NETROM_T4:
432 val = sk->protinfo.nr->t4 / HZ;
433 break;
434
435 case NETROM_IDLE:
436 val = sk->protinfo.nr->idle / (60 * HZ);
437 break;
438
439 default:
440 return -ENOPROTOOPT;
441 }
442
443 len = min_t(unsigned int, len, sizeof(int));
444
445 if (put_user(len, optlen))
446 return -EFAULT;
447
448 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
449 }
450
nr_listen(struct socket * sock,int backlog)451 static int nr_listen(struct socket *sock, int backlog)
452 {
453 struct sock *sk = sock->sk;
454
455 if (sk->state != TCP_LISTEN) {
456 memset(&sk->protinfo.nr->user_addr, '\0', AX25_ADDR_LEN);
457 sk->max_ack_backlog = backlog;
458 sk->state = TCP_LISTEN;
459 return 0;
460 }
461
462 return -EOPNOTSUPP;
463 }
464
nr_create(struct socket * sock,int protocol)465 static int nr_create(struct socket *sock, int protocol)
466 {
467 struct sock *sk;
468 nr_cb *nr;
469
470 if (sock->type != SOCK_SEQPACKET || protocol != 0)
471 return -ESOCKTNOSUPPORT;
472
473 if ((sk = nr_alloc_sock()) == NULL)
474 return -ENOMEM;
475
476 nr = sk->protinfo.nr;
477
478 sock_init_data(sock, sk);
479
480 sock->ops = &nr_proto_ops;
481 sk->protocol = protocol;
482
483 skb_queue_head_init(&nr->ack_queue);
484 skb_queue_head_init(&nr->reseq_queue);
485 skb_queue_head_init(&nr->frag_queue);
486
487 init_timer(&nr->t1timer);
488 init_timer(&nr->t2timer);
489 init_timer(&nr->t4timer);
490 init_timer(&nr->idletimer);
491
492 nr->t1 = sysctl_netrom_transport_timeout;
493 nr->t2 = sysctl_netrom_transport_acknowledge_delay;
494 nr->n2 = sysctl_netrom_transport_maximum_tries;
495 nr->t4 = sysctl_netrom_transport_busy_delay;
496 nr->idle = sysctl_netrom_transport_no_activity_timeout;
497 nr->window = sysctl_netrom_transport_requested_window_size;
498
499 nr->bpqext = 1;
500 nr->state = NR_STATE_0;
501
502 return 0;
503 }
504
nr_make_new(struct sock * osk)505 static struct sock *nr_make_new(struct sock *osk)
506 {
507 struct sock *sk;
508 nr_cb *nr;
509
510 if (osk->type != SOCK_SEQPACKET)
511 return NULL;
512
513 if ((sk = nr_alloc_sock()) == NULL)
514 return NULL;
515
516 nr = sk->protinfo.nr;
517
518 sock_init_data(NULL, sk);
519
520 sk->type = osk->type;
521 sk->socket = osk->socket;
522 sk->priority = osk->priority;
523 sk->protocol = osk->protocol;
524 sk->rcvbuf = osk->rcvbuf;
525 sk->sndbuf = osk->sndbuf;
526 sk->debug = osk->debug;
527 sk->state = TCP_ESTABLISHED;
528 sk->sleep = osk->sleep;
529 sk->zapped = osk->zapped;
530
531 skb_queue_head_init(&nr->ack_queue);
532 skb_queue_head_init(&nr->reseq_queue);
533 skb_queue_head_init(&nr->frag_queue);
534
535 init_timer(&nr->t1timer);
536 init_timer(&nr->t2timer);
537 init_timer(&nr->t4timer);
538 init_timer(&nr->idletimer);
539
540 nr->t1 = osk->protinfo.nr->t1;
541 nr->t2 = osk->protinfo.nr->t2;
542 nr->n2 = osk->protinfo.nr->n2;
543 nr->t4 = osk->protinfo.nr->t4;
544 nr->idle = osk->protinfo.nr->idle;
545 nr->window = osk->protinfo.nr->window;
546
547 nr->device = osk->protinfo.nr->device;
548 nr->bpqext = osk->protinfo.nr->bpqext;
549
550 return sk;
551 }
552
nr_release(struct socket * sock)553 static int nr_release(struct socket *sock)
554 {
555 struct sock *sk = sock->sk;
556
557 if (sk == NULL) return 0;
558
559 switch (sk->protinfo.nr->state) {
560
561 case NR_STATE_0:
562 case NR_STATE_1:
563 case NR_STATE_2:
564 nr_disconnect(sk, 0);
565 nr_destroy_socket(sk);
566 break;
567
568 case NR_STATE_3:
569 nr_clear_queues(sk);
570 sk->protinfo.nr->n2count = 0;
571 nr_write_internal(sk, NR_DISCREQ);
572 nr_start_t1timer(sk);
573 nr_stop_t2timer(sk);
574 nr_stop_t4timer(sk);
575 nr_stop_idletimer(sk);
576 sk->protinfo.nr->state = NR_STATE_2;
577 sk->state = TCP_CLOSE;
578 sk->shutdown |= SEND_SHUTDOWN;
579 sk->state_change(sk);
580 sock_orphan(sk);
581 sk->destroy = 1;
582 break;
583
584 default:
585 sk->socket = NULL;
586 break;
587 }
588
589 sock->sk = NULL;
590
591 return 0;
592 }
593
nr_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)594 static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
595 {
596 struct sock *sk = sock->sk;
597 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
598 struct net_device *dev;
599 ax25_address *user, *source;
600
601 if (sk->zapped == 0)
602 return -EINVAL;
603
604 if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct
605 full_sockaddr_ax25))
606 return -EINVAL;
607
608 if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25)))
609 return -EINVAL;
610
611 if (addr->fsa_ax25.sax25_family != AF_NETROM)
612 return -EINVAL;
613
614 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
615 SOCK_DEBUG(sk, "NET/ROM: bind failed: invalid node callsign\n");
616 return -EADDRNOTAVAIL;
617 }
618
619 /*
620 * Only the super user can set an arbitrary user callsign.
621 */
622 if (addr->fsa_ax25.sax25_ndigis == 1) {
623 if (!capable(CAP_NET_BIND_SERVICE)) {
624 dev_put(dev);
625 return -EACCES;
626 }
627 sk->protinfo.nr->user_addr = addr->fsa_digipeater[0];
628 sk->protinfo.nr->source_addr = addr->fsa_ax25.sax25_call;
629 } else {
630 source = &addr->fsa_ax25.sax25_call;
631
632 if ((user = ax25_findbyuid(current->euid)) == NULL) {
633 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
634 dev_put(dev);
635 return -EPERM;
636 }
637 user = source;
638 }
639
640 sk->protinfo.nr->user_addr = *user;
641 sk->protinfo.nr->source_addr = *source;
642 }
643
644 sk->protinfo.nr->device = dev;
645 nr_insert_socket(sk);
646
647 sk->zapped = 0;
648 SOCK_DEBUG(sk, "NET/ROM: socket is bound\n");
649 return 0;
650 }
651
nr_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)652 static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
653 int addr_len, int flags)
654 {
655 struct sock *sk = sock->sk;
656 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
657 ax25_address *user, *source = NULL;
658 struct net_device *dev;
659
660 if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
661 sock->state = SS_CONNECTED;
662 return 0; /* Connect completed during a ERESTARTSYS event */
663 }
664
665 if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
666 sock->state = SS_UNCONNECTED;
667 return -ECONNREFUSED;
668 }
669
670 if (sk->state == TCP_ESTABLISHED)
671 return -EISCONN; /* No reconnect on a seqpacket socket */
672
673 sk->state = TCP_CLOSE;
674 sock->state = SS_UNCONNECTED;
675
676 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
677 return -EINVAL;
678
679 if (addr->sax25_family != AF_NETROM)
680 return -EINVAL;
681
682 if (sk->zapped) { /* Must bind first - autobinding in this may or may not work */
683 sk->zapped = 0;
684
685 if ((dev = nr_dev_first()) == NULL)
686 return -ENETUNREACH;
687
688 source = (ax25_address *)dev->dev_addr;
689
690 if ((user = ax25_findbyuid(current->euid)) == NULL) {
691 if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
692 dev_put(dev);
693 return -EPERM;
694 }
695 user = source;
696 }
697
698 sk->protinfo.nr->user_addr = *user;
699 sk->protinfo.nr->source_addr = *source;
700 sk->protinfo.nr->device = dev;
701
702 nr_insert_socket(sk); /* Finish the bind */
703 }
704
705 sk->protinfo.nr->dest_addr = addr->sax25_call;
706
707 circuit = nr_find_next_circuit();
708
709 sk->protinfo.nr->my_index = circuit / 256;
710 sk->protinfo.nr->my_id = circuit % 256;
711
712 circuit++;
713
714 /* Move to connecting socket, start sending Connect Requests */
715 sock->state = SS_CONNECTING;
716 sk->state = TCP_SYN_SENT;
717
718 nr_establish_data_link(sk);
719
720 sk->protinfo.nr->state = NR_STATE_1;
721
722 nr_start_heartbeat(sk);
723
724 /* Now the loop */
725 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
726 return -EINPROGRESS;
727
728 cli(); /* To avoid races on the sleep */
729
730 /*
731 * A Connect Ack with Choke or timeout or failed routing will go to closed.
732 */
733 while (sk->state == TCP_SYN_SENT) {
734 interruptible_sleep_on(sk->sleep);
735 if (signal_pending(current)) {
736 sti();
737 return -ERESTARTSYS;
738 }
739 }
740
741 if (sk->state != TCP_ESTABLISHED) {
742 sti();
743 sock->state = SS_UNCONNECTED;
744 return sock_error(sk); /* Always set at this point */
745 }
746
747 sock->state = SS_CONNECTED;
748
749 sti();
750
751 return 0;
752 }
753
nr_accept(struct socket * sock,struct socket * newsock,int flags)754 static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
755 {
756 struct sock *sk;
757 struct sock *newsk;
758 struct sk_buff *skb;
759
760 if ((sk = sock->sk) == NULL)
761 return -EINVAL;
762
763 if (sk->type != SOCK_SEQPACKET)
764 return -EOPNOTSUPP;
765
766 if (sk->state != TCP_LISTEN)
767 return -EINVAL;
768
769 /*
770 * The write queue this time is holding sockets ready to use
771 * hooked into the SABM we saved
772 */
773 do {
774 cli();
775 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
776 if (flags & O_NONBLOCK) {
777 sti();
778 return -EWOULDBLOCK;
779 }
780 interruptible_sleep_on(sk->sleep);
781 if (signal_pending(current)) {
782 sti();
783 return -ERESTARTSYS;
784 }
785 }
786 } while (skb == NULL);
787
788 newsk = skb->sk;
789 newsk->pair = NULL;
790 newsk->socket = newsock;
791 newsk->sleep = &newsock->wait;
792 sti();
793
794 /* Now attach up the new socket */
795 kfree_skb(skb);
796 sk->ack_backlog--;
797 newsock->sk = newsk;
798
799 return 0;
800 }
801
nr_getname(struct socket * sock,struct sockaddr * uaddr,int * uaddr_len,int peer)802 static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
803 int *uaddr_len, int peer)
804 {
805 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
806 struct sock *sk = sock->sk;
807
808 if (peer != 0) {
809 if (sk->state != TCP_ESTABLISHED)
810 return -ENOTCONN;
811 sax->fsa_ax25.sax25_family = AF_NETROM;
812 sax->fsa_ax25.sax25_ndigis = 1;
813 sax->fsa_ax25.sax25_call = sk->protinfo.nr->user_addr;
814 memset(sax->fsa_digipeater, 0, sizeof(sax->fsa_digipeater));
815 sax->fsa_digipeater[0] = sk->protinfo.nr->dest_addr;
816 *uaddr_len = sizeof(struct full_sockaddr_ax25);
817 } else {
818 sax->fsa_ax25.sax25_family = AF_NETROM;
819 sax->fsa_ax25.sax25_ndigis = 0;
820 sax->fsa_ax25.sax25_call = sk->protinfo.nr->source_addr;
821 *uaddr_len = sizeof(struct sockaddr_ax25);
822 }
823
824 return 0;
825 }
826
nr_rx_frame(struct sk_buff * skb,struct net_device * dev)827 int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
828 {
829 struct sock *sk;
830 struct sock *make;
831 ax25_address *src, *dest, *user;
832 unsigned short circuit_index, circuit_id;
833 unsigned short peer_circuit_index, peer_circuit_id;
834 unsigned short frametype, flags, window, timeout;
835
836 skb->sk = NULL; /* Initially we don't know who it's for */
837
838 /*
839 * skb->data points to the netrom frame start
840 */
841
842 src = (ax25_address *)(skb->data + 0);
843 dest = (ax25_address *)(skb->data + 7);
844
845 circuit_index = skb->data[15];
846 circuit_id = skb->data[16];
847 peer_circuit_index = skb->data[17];
848 peer_circuit_id = skb->data[18];
849 frametype = skb->data[19] & 0x0F;
850 flags = skb->data[19] & 0xF0;
851
852 switch (frametype) {
853 case NR_PROTOEXT:
854 #ifdef CONFIG_INET
855 /*
856 * Check for an incoming IP over NET/ROM frame.
857 */
858 if (circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
859 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
860 skb->h.raw = skb->data;
861
862 return nr_rx_ip(skb, dev);
863 }
864 #endif
865 return 0;
866
867 case NR_CONNREQ:
868 case NR_CONNACK:
869 case NR_DISCREQ:
870 case NR_DISCACK:
871 case NR_INFO:
872 case NR_INFOACK:
873 /*
874 * These frame types we understand.
875 */
876 break;
877
878 default:
879 /*
880 * Everything else is ignored.
881 */
882 return 0;
883 }
884
885 /*
886 * Find an existing socket connection, based on circuit ID, if it's
887 * a Connect Request base it on their circuit ID.
888 *
889 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
890 * circuit that no longer exists at the other end ...
891 */
892
893 sk = NULL;
894
895 if (circuit_index == 0 && circuit_id == 0) {
896 if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
897 sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
898 } else {
899 if (frametype == NR_CONNREQ)
900 sk = nr_find_peer(circuit_index, circuit_id, src);
901 else
902 sk = nr_find_socket(circuit_index, circuit_id);
903 }
904
905 if (sk != NULL) {
906 skb->h.raw = skb->data;
907
908 if (frametype == NR_CONNACK && skb->len == 22)
909 sk->protinfo.nr->bpqext = 1;
910 else
911 sk->protinfo.nr->bpqext = 0;
912
913 return nr_process_rx_frame(sk, skb);
914 }
915
916 /*
917 * Now it should be a CONNREQ.
918 */
919 if (frametype != NR_CONNREQ) {
920 /*
921 * Here it would be nice to be able to send a reset but
922 * NET/ROM doesn't have one. The following hack would
923 * have been a way to extend the protocol but apparently
924 * it kills BPQ boxes... :-(
925 */
926 #if 0
927 /*
928 * Never reply to a CONNACK/CHOKE.
929 */
930 if (frametype != NR_CONNACK || flags != NR_CHOKE_FLAG)
931 nr_transmit_refusal(skb, 1);
932 #endif
933 return 0;
934 }
935
936 sk = nr_find_listener(dest);
937
938 user = (ax25_address *)(skb->data + 21);
939
940 if (sk == NULL || sk->ack_backlog == sk->max_ack_backlog || (make = nr_make_new(sk)) == NULL) {
941 nr_transmit_refusal(skb, 0);
942 return 0;
943 }
944
945 window = skb->data[20];
946
947 skb->sk = make;
948 make->state = TCP_ESTABLISHED;
949
950 /* Fill in his circuit details */
951 make->protinfo.nr->source_addr = *dest;
952 make->protinfo.nr->dest_addr = *src;
953 make->protinfo.nr->user_addr = *user;
954
955 make->protinfo.nr->your_index = circuit_index;
956 make->protinfo.nr->your_id = circuit_id;
957
958 circuit = nr_find_next_circuit();
959
960 make->protinfo.nr->my_index = circuit / 256;
961 make->protinfo.nr->my_id = circuit % 256;
962
963 circuit++;
964
965 /* Window negotiation */
966 if (window < make->protinfo.nr->window)
967 make->protinfo.nr->window = window;
968
969 /* L4 timeout negotiation */
970 if (skb->len == 37) {
971 timeout = skb->data[36] * 256 + skb->data[35];
972 if (timeout * HZ < make->protinfo.nr->t1)
973 make->protinfo.nr->t1 = timeout * HZ;
974 make->protinfo.nr->bpqext = 1;
975 } else {
976 make->protinfo.nr->bpqext = 0;
977 }
978
979 nr_write_internal(make, NR_CONNACK);
980
981 make->protinfo.nr->condition = 0x00;
982 make->protinfo.nr->vs = 0;
983 make->protinfo.nr->va = 0;
984 make->protinfo.nr->vr = 0;
985 make->protinfo.nr->vl = 0;
986 make->protinfo.nr->state = NR_STATE_3;
987 sk->ack_backlog++;
988 make->pair = sk;
989
990 dev_hold(make->protinfo.nr->device);
991
992 nr_insert_socket(make);
993
994 skb_queue_head(&sk->receive_queue, skb);
995
996 nr_start_heartbeat(make);
997 nr_start_idletimer(make);
998
999 if (!sk->dead)
1000 sk->data_ready(sk, skb->len);
1001
1002 return 1;
1003 }
1004
nr_sendmsg(struct socket * sock,struct msghdr * msg,int len,struct scm_cookie * scm)1005 static int nr_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
1006 {
1007 struct sock *sk = sock->sk;
1008 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1009 int err;
1010 struct sockaddr_ax25 sax;
1011 struct sk_buff *skb;
1012 unsigned char *asmptr;
1013 int size;
1014
1015 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR))
1016 return -EINVAL;
1017
1018 if (sk->zapped)
1019 return -EADDRNOTAVAIL;
1020
1021 if (sk->shutdown & SEND_SHUTDOWN) {
1022 send_sig(SIGPIPE, current, 0);
1023 return -EPIPE;
1024 }
1025
1026 if (sk->protinfo.nr->device == NULL)
1027 return -ENETUNREACH;
1028
1029 if (usax) {
1030 if (msg->msg_namelen < sizeof(sax))
1031 return -EINVAL;
1032 sax = *usax;
1033 if (ax25cmp(&sk->protinfo.nr->dest_addr, &sax.sax25_call) != 0)
1034 return -EISCONN;
1035 if (sax.sax25_family != AF_NETROM)
1036 return -EINVAL;
1037 } else {
1038 if (sk->state != TCP_ESTABLISHED)
1039 return -ENOTCONN;
1040 sax.sax25_family = AF_NETROM;
1041 sax.sax25_call = sk->protinfo.nr->dest_addr;
1042 }
1043
1044 SOCK_DEBUG(sk, "NET/ROM: sendto: Addresses built.\n");
1045
1046 /* Build a packet - the conventional user limit is 236 bytes. We can
1047 do ludicrously large NetROM frames but must not overflow */
1048 if (len > 65536)
1049 return -EMSGSIZE;
1050
1051 SOCK_DEBUG(sk, "NET/ROM: sendto: building packet.\n");
1052 size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1053
1054 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1055 return err;
1056
1057 skb_reserve(skb, size - len);
1058
1059 /*
1060 * Push down the NET/ROM header
1061 */
1062
1063 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1064 SOCK_DEBUG(sk, "Building NET/ROM Header.\n");
1065
1066 /* Build a NET/ROM Transport header */
1067
1068 *asmptr++ = sk->protinfo.nr->your_index;
1069 *asmptr++ = sk->protinfo.nr->your_id;
1070 *asmptr++ = 0; /* To be filled in later */
1071 *asmptr++ = 0; /* Ditto */
1072 *asmptr++ = NR_INFO;
1073 SOCK_DEBUG(sk, "Built header.\n");
1074
1075 /*
1076 * Put the data on the end
1077 */
1078
1079 skb->h.raw = skb_put(skb, len);
1080
1081 asmptr = skb->h.raw;
1082 SOCK_DEBUG(sk, "NET/ROM: Appending user data\n");
1083
1084 /* User data follows immediately after the NET/ROM transport header */
1085 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1086 SOCK_DEBUG(sk, "NET/ROM: Transmitting buffer\n");
1087
1088 if (sk->state != TCP_ESTABLISHED) {
1089 kfree_skb(skb);
1090 return -ENOTCONN;
1091 }
1092
1093 nr_output(sk, skb); /* Shove it onto the queue */
1094
1095 return len;
1096 }
1097
nr_recvmsg(struct socket * sock,struct msghdr * msg,int size,int flags,struct scm_cookie * scm)1098 static int nr_recvmsg(struct socket *sock, struct msghdr *msg, int size,
1099 int flags, struct scm_cookie *scm)
1100 {
1101 struct sock *sk = sock->sk;
1102 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1103 int copied;
1104 struct sk_buff *skb;
1105 int er;
1106
1107 /*
1108 * This works for seqpacket too. The receiver has ordered the queue for
1109 * us! We do one quick check first though
1110 */
1111
1112 if (sk->state != TCP_ESTABLISHED)
1113 return -ENOTCONN;
1114
1115 /* Now we can treat all alike */
1116 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1117 return er;
1118
1119 skb->h.raw = skb->data;
1120 copied = skb->len;
1121
1122 if (copied > size) {
1123 copied = size;
1124 msg->msg_flags |= MSG_TRUNC;
1125 }
1126
1127 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1128
1129 if (sax != NULL) {
1130 sax->sax25_family = AF_NETROM;
1131 memcpy(sax->sax25_call.ax25_call, skb->data + 7, AX25_ADDR_LEN);
1132 }
1133
1134 msg->msg_namelen = sizeof(*sax);
1135
1136 skb_free_datagram(sk, skb);
1137
1138 return copied;
1139 }
1140
1141
nr_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)1142 static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1143 {
1144 struct sock *sk = sock->sk;
1145
1146 switch (cmd) {
1147 case TIOCOUTQ: {
1148 long amount;
1149 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1150 if (amount < 0)
1151 amount = 0;
1152 return put_user(amount, (int *)arg);
1153 }
1154
1155 case TIOCINQ: {
1156 struct sk_buff *skb;
1157 long amount = 0L;
1158 /* These two are safe on a single CPU system as only user tasks fiddle here */
1159 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1160 amount = skb->len;
1161 return put_user(amount, (int *)arg);
1162 }
1163
1164 case SIOCGSTAMP:
1165 if (sk != NULL) {
1166 if (sk->stamp.tv_sec == 0)
1167 return -ENOENT;
1168 return copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)) ? -EFAULT : 0;
1169 }
1170 return -EINVAL;
1171
1172 case SIOCGIFADDR:
1173 case SIOCSIFADDR:
1174 case SIOCGIFDSTADDR:
1175 case SIOCSIFDSTADDR:
1176 case SIOCGIFBRDADDR:
1177 case SIOCSIFBRDADDR:
1178 case SIOCGIFNETMASK:
1179 case SIOCSIFNETMASK:
1180 case SIOCGIFMETRIC:
1181 case SIOCSIFMETRIC:
1182 return -EINVAL;
1183
1184 case SIOCADDRT:
1185 case SIOCDELRT:
1186 case SIOCNRDECOBS:
1187 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1188 return nr_rt_ioctl(cmd, (void *)arg);
1189
1190 default:
1191 return dev_ioctl(cmd, (void *)arg);
1192 }
1193
1194 /*NOTREACHED*/
1195 return 0;
1196 }
1197
nr_get_info(char * buffer,char ** start,off_t offset,int length)1198 static int nr_get_info(char *buffer, char **start, off_t offset, int length)
1199 {
1200 struct sock *s;
1201 struct net_device *dev;
1202 const char *devname;
1203 int len = 0;
1204 off_t pos = 0;
1205 off_t begin = 0;
1206
1207 cli();
1208
1209 len += sprintf(buffer, "user_addr dest_node src_node dev my your st vs vr va t1 t2 t4 idle n2 wnd Snd-Q Rcv-Q inode\n");
1210
1211 for (s = nr_list; s != NULL; s = s->next) {
1212 if ((dev = s->protinfo.nr->device) == NULL)
1213 devname = "???";
1214 else
1215 devname = dev->name;
1216
1217 len += sprintf(buffer + len, "%-9s ",
1218 ax2asc(&s->protinfo.nr->user_addr));
1219 len += sprintf(buffer + len, "%-9s ",
1220 ax2asc(&s->protinfo.nr->dest_addr));
1221 len += sprintf(buffer + len, "%-9s %-3s %02X/%02X %02X/%02X %2d %3d %3d %3d %3lu/%03lu %2lu/%02lu %3lu/%03lu %3lu/%03lu %2d/%02d %3d %5d %5d %ld\n",
1222 ax2asc(&s->protinfo.nr->source_addr),
1223 devname,
1224 s->protinfo.nr->my_index,
1225 s->protinfo.nr->my_id,
1226 s->protinfo.nr->your_index,
1227 s->protinfo.nr->your_id,
1228 s->protinfo.nr->state,
1229 s->protinfo.nr->vs,
1230 s->protinfo.nr->vr,
1231 s->protinfo.nr->va,
1232 ax25_display_timer(&s->protinfo.nr->t1timer) / HZ,
1233 s->protinfo.nr->t1 / HZ,
1234 ax25_display_timer(&s->protinfo.nr->t2timer) / HZ,
1235 s->protinfo.nr->t2 / HZ,
1236 ax25_display_timer(&s->protinfo.nr->t4timer) / HZ,
1237 s->protinfo.nr->t4 / HZ,
1238 ax25_display_timer(&s->protinfo.nr->idletimer) / (60 * HZ),
1239 s->protinfo.nr->idle / (60 * HZ),
1240 s->protinfo.nr->n2count,
1241 s->protinfo.nr->n2,
1242 s->protinfo.nr->window,
1243 atomic_read(&s->wmem_alloc),
1244 atomic_read(&s->rmem_alloc),
1245 s->socket != NULL ? s->socket->inode->i_ino : 0L);
1246
1247 pos = begin + len;
1248
1249 if (pos < offset) {
1250 len = 0;
1251 begin = pos;
1252 }
1253
1254 if (pos > offset + length)
1255 break;
1256 }
1257
1258 sti();
1259
1260 *start = buffer + (offset - begin);
1261 len -= (offset - begin);
1262
1263 if (len > length) len = length;
1264
1265 return(len);
1266 }
1267
1268 static struct net_proto_family nr_family_ops = {
1269 family: PF_NETROM,
1270 create: nr_create,
1271 };
1272
1273 static struct proto_ops SOCKOPS_WRAPPED(nr_proto_ops) = {
1274 family: PF_NETROM,
1275
1276 release: nr_release,
1277 bind: nr_bind,
1278 connect: nr_connect,
1279 socketpair: sock_no_socketpair,
1280 accept: nr_accept,
1281 getname: nr_getname,
1282 poll: datagram_poll,
1283 ioctl: nr_ioctl,
1284 listen: nr_listen,
1285 shutdown: sock_no_shutdown,
1286 setsockopt: nr_setsockopt,
1287 getsockopt: nr_getsockopt,
1288 sendmsg: nr_sendmsg,
1289 recvmsg: nr_recvmsg,
1290 mmap: sock_no_mmap,
1291 sendpage: sock_no_sendpage,
1292 };
1293
1294 #include <linux/smp_lock.h>
1295 SOCKOPS_WRAP(nr_proto, PF_NETROM);
1296
1297 static struct notifier_block nr_dev_notifier = {
1298 notifier_call: nr_device_event,
1299 };
1300
1301 static struct net_device *dev_nr;
1302
1303 static char banner[] __initdata = KERN_INFO "G4KLX NET/ROM for Linux. Version 0.7 for AX25.037 Linux 2.4\n";
1304
nr_proto_init(void)1305 static int __init nr_proto_init(void)
1306 {
1307 int i;
1308
1309 if (nr_ndevs > 0x7fffffff/sizeof(struct net_device)) {
1310 printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
1311 return -1;
1312 }
1313
1314 if ((dev_nr = kmalloc(nr_ndevs * sizeof(struct net_device), GFP_KERNEL)) == NULL) {
1315 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
1316 return -1;
1317 }
1318
1319 memset(dev_nr, 0x00, nr_ndevs * sizeof(struct net_device));
1320
1321 for (i = 0; i < nr_ndevs; i++) {
1322 sprintf(dev_nr[i].name, "nr%d", i);
1323 dev_nr[i].init = nr_init;
1324 register_netdev(&dev_nr[i]);
1325 }
1326
1327 sock_register(&nr_family_ops);
1328 register_netdevice_notifier(&nr_dev_notifier);
1329 printk(banner);
1330
1331 ax25_protocol_register(AX25_P_NETROM, nr_route_frame);
1332 ax25_linkfail_register(nr_link_failed);
1333
1334 #ifdef CONFIG_SYSCTL
1335 nr_register_sysctl();
1336 #endif
1337
1338 nr_loopback_init();
1339
1340 proc_net_create("nr", 0, nr_get_info);
1341 proc_net_create("nr_neigh", 0, nr_neigh_get_info);
1342 proc_net_create("nr_nodes", 0, nr_nodes_get_info);
1343 return 0;
1344 }
1345
1346 module_init(nr_proto_init);
1347
1348
1349 EXPORT_NO_SYMBOLS;
1350
1351 MODULE_PARM(nr_ndevs, "i");
1352 MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1353
1354 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1355 MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1356 MODULE_LICENSE("GPL");
1357
nr_exit(void)1358 static void __exit nr_exit(void)
1359 {
1360 int i;
1361
1362 proc_net_remove("nr");
1363 proc_net_remove("nr_neigh");
1364 proc_net_remove("nr_nodes");
1365 nr_loopback_clear();
1366
1367 nr_rt_free();
1368
1369 ax25_protocol_release(AX25_P_NETROM);
1370 ax25_linkfail_release(nr_link_failed);
1371
1372 unregister_netdevice_notifier(&nr_dev_notifier);
1373
1374 #ifdef CONFIG_SYSCTL
1375 nr_unregister_sysctl();
1376 #endif
1377 sock_unregister(PF_NETROM);
1378
1379 for (i = 0; i < nr_ndevs; i++) {
1380 if (dev_nr[i].priv != NULL) {
1381 kfree(dev_nr[i].priv);
1382 dev_nr[i].priv = NULL;
1383 unregister_netdev(&dev_nr[i]);
1384 }
1385 }
1386
1387 kfree(dev_nr);
1388 }
1389 module_exit(nr_exit);
1390