Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_netrom.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * Copyright Jonathan Naylor G4KLX ([email protected])
8  * Copyright Alan Cox GW4PTS ([email protected])
9  * Copyright Darryl Miles G7LED ([email protected])
10  */
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/capability.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/socket.h>
17 #include <linux/in.h>
18 #include <linux/slab.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/stat.h>
26 #include <net/ax25.h>
27 #include <linux/inet.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/skbuff.h>
31 #include <net/net_namespace.h>
32 #include <net/sock.h>
33 #include <asm/uaccess.h>
34 #include <linux/fcntl.h>
35 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
36 #include <linux/mm.h>
37 #include <linux/interrupt.h>
38 #include <linux/notifier.h>
39 #include <net/netrom.h>
40 #include <linux/proc_fs.h>
41 #include <linux/seq_file.h>
42 #include <net/ip.h>
43 #include <net/tcp_states.h>
44 #include <net/arp.h>
45 #include <linux/init.h>
46 
47 static int nr_ndevs = 4;
48 
61 
62 static unsigned short circuit = 0x101;
63 
64 static HLIST_HEAD(nr_list);
65 static DEFINE_SPINLOCK(nr_list_lock);
66 
67 static const struct proto_ops nr_proto_ops;
68 
69 /*
70  * NETROM network devices are virtual network devices encapsulating NETROM
71  * frames into AX.25 which will be sent through an AX.25 device, so form a
72  * special "super class" of normal net devices; split their locks off into a
73  * separate class since they always nest.
74  */
75 static struct lock_class_key nr_netdev_xmit_lock_key;
76 static struct lock_class_key nr_netdev_addr_lock_key;
77 
78 static void nr_set_lockdep_one(struct net_device *dev,
79  struct netdev_queue *txq,
80  void *_unused)
81 {
82  lockdep_set_class(&txq->_xmit_lock, &nr_netdev_xmit_lock_key);
83 }
84 
85 static void nr_set_lockdep_key(struct net_device *dev)
86 {
87  lockdep_set_class(&dev->addr_list_lock, &nr_netdev_addr_lock_key);
88  netdev_for_each_tx_queue(dev, nr_set_lockdep_one, NULL);
89 }
90 
91 /*
92  * Socket removal during an interrupt is now safe.
93  */
94 static void nr_remove_socket(struct sock *sk)
95 {
96  spin_lock_bh(&nr_list_lock);
97  sk_del_node_init(sk);
98  spin_unlock_bh(&nr_list_lock);
99 }
100 
101 /*
102  * Kill all bound sockets on a dropped device.
103  */
104 static void nr_kill_by_device(struct net_device *dev)
105 {
106  struct sock *s;
107  struct hlist_node *node;
108 
109  spin_lock_bh(&nr_list_lock);
110  sk_for_each(s, node, &nr_list)
111  if (nr_sk(s)->device == dev)
113  spin_unlock_bh(&nr_list_lock);
114 }
115 
116 /*
117  * Handle device status changes.
118  */
119 static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
120 {
121  struct net_device *dev = (struct net_device *)ptr;
122 
123  if (!net_eq(dev_net(dev), &init_net))
124  return NOTIFY_DONE;
125 
126  if (event != NETDEV_DOWN)
127  return NOTIFY_DONE;
128 
129  nr_kill_by_device(dev);
130  nr_rt_device_down(dev);
131 
132  return NOTIFY_DONE;
133 }
134 
135 /*
136  * Add a socket to the bound sockets list.
137  */
138 static void nr_insert_socket(struct sock *sk)
139 {
140  spin_lock_bh(&nr_list_lock);
141  sk_add_node(sk, &nr_list);
142  spin_unlock_bh(&nr_list_lock);
143 }
144 
145 /*
146  * Find a socket that wants to accept the Connect Request we just
147  * received.
148  */
149 static struct sock *nr_find_listener(ax25_address *addr)
150 {
151  struct sock *s;
152  struct hlist_node *node;
153 
154  spin_lock_bh(&nr_list_lock);
155  sk_for_each(s, node, &nr_list)
156  if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
157  s->sk_state == TCP_LISTEN) {
158  bh_lock_sock(s);
159  goto found;
160  }
161  s = NULL;
162 found:
163  spin_unlock_bh(&nr_list_lock);
164  return s;
165 }
166 
167 /*
168  * Find a connected NET/ROM socket given my circuit IDs.
169  */
170 static struct sock *nr_find_socket(unsigned char index, unsigned char id)
171 {
172  struct sock *s;
173  struct hlist_node *node;
174 
175  spin_lock_bh(&nr_list_lock);
176  sk_for_each(s, node, &nr_list) {
177  struct nr_sock *nr = nr_sk(s);
178 
179  if (nr->my_index == index && nr->my_id == id) {
180  bh_lock_sock(s);
181  goto found;
182  }
183  }
184  s = NULL;
185 found:
186  spin_unlock_bh(&nr_list_lock);
187  return s;
188 }
189 
190 /*
191  * Find a connected NET/ROM socket given their circuit IDs.
192  */
193 static struct sock *nr_find_peer(unsigned char index, unsigned char id,
195 {
196  struct sock *s;
197  struct hlist_node *node;
198 
199  spin_lock_bh(&nr_list_lock);
200  sk_for_each(s, node, &nr_list) {
201  struct nr_sock *nr = nr_sk(s);
202 
203  if (nr->your_index == index && nr->your_id == id &&
204  !ax25cmp(&nr->dest_addr, dest)) {
205  bh_lock_sock(s);
206  goto found;
207  }
208  }
209  s = NULL;
210 found:
211  spin_unlock_bh(&nr_list_lock);
212  return s;
213 }
214 
215 /*
216  * Find next free circuit ID.
217  */
218 static unsigned short nr_find_next_circuit(void)
219 {
220  unsigned short id = circuit;
221  unsigned char i, j;
222  struct sock *sk;
223 
224  for (;;) {
225  i = id / 256;
226  j = id % 256;
227 
228  if (i != 0 && j != 0) {
229  if ((sk=nr_find_socket(i, j)) == NULL)
230  break;
231  bh_unlock_sock(sk);
232  }
233 
234  id++;
235  }
236 
237  return id;
238 }
239 
240 /*
241  * Deferred destroy.
242  */
243 void nr_destroy_socket(struct sock *);
244 
245 /*
246  * Handler for deferred kills.
247  */
248 static void nr_destroy_timer(unsigned long data)
249 {
250  struct sock *sk=(struct sock *)data;
251  bh_lock_sock(sk);
252  sock_hold(sk);
253  nr_destroy_socket(sk);
254  bh_unlock_sock(sk);
255  sock_put(sk);
256 }
257 
258 /*
259  * This is called from user mode and the timers. Thus it protects itself
260  * against interrupt users but doesn't worry about being called during
261  * work. Once it is removed from the queue no interrupt or bottom half
262  * will touch it and we are (fairly 8-) ) safe.
263  */
264 void nr_destroy_socket(struct sock *sk)
265 {
266  struct sk_buff *skb;
267 
268  nr_remove_socket(sk);
269 
270  nr_stop_heartbeat(sk);
271  nr_stop_t1timer(sk);
272  nr_stop_t2timer(sk);
273  nr_stop_t4timer(sk);
274  nr_stop_idletimer(sk);
275 
276  nr_clear_queues(sk); /* Flush the queues */
277 
278  while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
279  if (skb->sk != sk) { /* A pending connection */
280  /* Queue the unaccepted socket for death */
281  sock_set_flag(skb->sk, SOCK_DEAD);
282  nr_start_heartbeat(skb->sk);
283  nr_sk(skb->sk)->state = NR_STATE_0;
284  }
285 
286  kfree_skb(skb);
287  }
288 
289  if (sk_has_allocations(sk)) {
290  /* Defer: outstanding buffers */
291  sk->sk_timer.function = nr_destroy_timer;
292  sk->sk_timer.expires = jiffies + 2 * HZ;
293  add_timer(&sk->sk_timer);
294  } else
295  sock_put(sk);
296 }
297 
298 /*
299  * Handling for system calls applied via the various interfaces to a
300  * NET/ROM socket object.
301  */
302 
303 static int nr_setsockopt(struct socket *sock, int level, int optname,
304  char __user *optval, unsigned int optlen)
305 {
306  struct sock *sk = sock->sk;
307  struct nr_sock *nr = nr_sk(sk);
308  unsigned long opt;
309 
310  if (level != SOL_NETROM)
311  return -ENOPROTOOPT;
312 
313  if (optlen < sizeof(unsigned int))
314  return -EINVAL;
315 
316  if (get_user(opt, (unsigned int __user *)optval))
317  return -EFAULT;
318 
319  switch (optname) {
320  case NETROM_T1:
321  if (opt < 1 || opt > ULONG_MAX / HZ)
322  return -EINVAL;
323  nr->t1 = opt * HZ;
324  return 0;
325 
326  case NETROM_T2:
327  if (opt < 1 || opt > ULONG_MAX / HZ)
328  return -EINVAL;
329  nr->t2 = opt * HZ;
330  return 0;
331 
332  case NETROM_N2:
333  if (opt < 1 || opt > 31)
334  return -EINVAL;
335  nr->n2 = opt;
336  return 0;
337 
338  case NETROM_T4:
339  if (opt < 1 || opt > ULONG_MAX / HZ)
340  return -EINVAL;
341  nr->t4 = opt * HZ;
342  return 0;
343 
344  case NETROM_IDLE:
345  if (opt > ULONG_MAX / (60 * HZ))
346  return -EINVAL;
347  nr->idle = opt * 60 * HZ;
348  return 0;
349 
350  default:
351  return -ENOPROTOOPT;
352  }
353 }
354 
355 static int nr_getsockopt(struct socket *sock, int level, int optname,
356  char __user *optval, int __user *optlen)
357 {
358  struct sock *sk = sock->sk;
359  struct nr_sock *nr = nr_sk(sk);
360  int val = 0;
361  int len;
362 
363  if (level != SOL_NETROM)
364  return -ENOPROTOOPT;
365 
366  if (get_user(len, optlen))
367  return -EFAULT;
368 
369  if (len < 0)
370  return -EINVAL;
371 
372  switch (optname) {
373  case NETROM_T1:
374  val = nr->t1 / HZ;
375  break;
376 
377  case NETROM_T2:
378  val = nr->t2 / HZ;
379  break;
380 
381  case NETROM_N2:
382  val = nr->n2;
383  break;
384 
385  case NETROM_T4:
386  val = nr->t4 / HZ;
387  break;
388 
389  case NETROM_IDLE:
390  val = nr->idle / (60 * HZ);
391  break;
392 
393  default:
394  return -ENOPROTOOPT;
395  }
396 
397  len = min_t(unsigned int, len, sizeof(int));
398 
399  if (put_user(len, optlen))
400  return -EFAULT;
401 
402  return copy_to_user(optval, &val, len) ? -EFAULT : 0;
403 }
404 
405 static int nr_listen(struct socket *sock, int backlog)
406 {
407  struct sock *sk = sock->sk;
408 
409  lock_sock(sk);
410  if (sk->sk_state != TCP_LISTEN) {
411  memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
413  sk->sk_state = TCP_LISTEN;
414  release_sock(sk);
415  return 0;
416  }
417  release_sock(sk);
418 
419  return -EOPNOTSUPP;
420 }
421 
422 static struct proto nr_proto = {
423  .name = "NETROM",
424  .owner = THIS_MODULE,
425  .obj_size = sizeof(struct nr_sock),
426 };
427 
428 static int nr_create(struct net *net, struct socket *sock, int protocol,
429  int kern)
430 {
431  struct sock *sk;
432  struct nr_sock *nr;
433 
434  if (!net_eq(net, &init_net))
435  return -EAFNOSUPPORT;
436 
437  if (sock->type != SOCK_SEQPACKET || protocol != 0)
438  return -ESOCKTNOSUPPORT;
439 
440  sk = sk_alloc(net, PF_NETROM, GFP_ATOMIC, &nr_proto);
441  if (sk == NULL)
442  return -ENOMEM;
443 
444  nr = nr_sk(sk);
445 
446  sock_init_data(sock, sk);
447 
448  sock->ops = &nr_proto_ops;
449  sk->sk_protocol = protocol;
450 
451  skb_queue_head_init(&nr->ack_queue);
452  skb_queue_head_init(&nr->reseq_queue);
453  skb_queue_head_init(&nr->frag_queue);
454 
455  nr_init_timers(sk);
456 
457  nr->t1 =
459  nr->t2 =
461  nr->n2 =
463  nr->t4 =
465  nr->idle =
468 
469  nr->bpqext = 1;
470  nr->state = NR_STATE_0;
471 
472  return 0;
473 }
474 
475 static struct sock *nr_make_new(struct sock *osk)
476 {
477  struct sock *sk;
478  struct nr_sock *nr, *onr;
479 
480  if (osk->sk_type != SOCK_SEQPACKET)
481  return NULL;
482 
483  sk = sk_alloc(sock_net(osk), PF_NETROM, GFP_ATOMIC, osk->sk_prot);
484  if (sk == NULL)
485  return NULL;
486 
487  nr = nr_sk(sk);
488 
489  sock_init_data(NULL, sk);
490 
491  sk->sk_type = osk->sk_type;
492  sk->sk_priority = osk->sk_priority;
493  sk->sk_protocol = osk->sk_protocol;
494  sk->sk_rcvbuf = osk->sk_rcvbuf;
495  sk->sk_sndbuf = osk->sk_sndbuf;
496  sk->sk_state = TCP_ESTABLISHED;
497  sock_copy_flags(sk, osk);
498 
499  skb_queue_head_init(&nr->ack_queue);
500  skb_queue_head_init(&nr->reseq_queue);
501  skb_queue_head_init(&nr->frag_queue);
502 
503  nr_init_timers(sk);
504 
505  onr = nr_sk(osk);
506 
507  nr->t1 = onr->t1;
508  nr->t2 = onr->t2;
509  nr->n2 = onr->n2;
510  nr->t4 = onr->t4;
511  nr->idle = onr->idle;
512  nr->window = onr->window;
513 
514  nr->device = onr->device;
515  nr->bpqext = onr->bpqext;
516 
517  return sk;
518 }
519 
520 static int nr_release(struct socket *sock)
521 {
522  struct sock *sk = sock->sk;
523  struct nr_sock *nr;
524 
525  if (sk == NULL) return 0;
526 
527  sock_hold(sk);
528  sock_orphan(sk);
529  lock_sock(sk);
530  nr = nr_sk(sk);
531 
532  switch (nr->state) {
533  case NR_STATE_0:
534  case NR_STATE_1:
535  case NR_STATE_2:
536  nr_disconnect(sk, 0);
537  nr_destroy_socket(sk);
538  break;
539 
540  case NR_STATE_3:
541  nr_clear_queues(sk);
542  nr->n2count = 0;
544  nr_start_t1timer(sk);
545  nr_stop_t2timer(sk);
546  nr_stop_t4timer(sk);
547  nr_stop_idletimer(sk);
548  nr->state = NR_STATE_2;
549  sk->sk_state = TCP_CLOSE;
550  sk->sk_shutdown |= SEND_SHUTDOWN;
551  sk->sk_state_change(sk);
552  sock_set_flag(sk, SOCK_DESTROY);
553  break;
554 
555  default:
556  break;
557  }
558 
559  sock->sk = NULL;
560  release_sock(sk);
561  sock_put(sk);
562 
563  return 0;
564 }
565 
566 static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
567 {
568  struct sock *sk = sock->sk;
569  struct nr_sock *nr = nr_sk(sk);
570  struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
571  struct net_device *dev;
574 
575  lock_sock(sk);
576  if (!sock_flag(sk, SOCK_ZAPPED)) {
577  release_sock(sk);
578  return -EINVAL;
579  }
580  if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
581  release_sock(sk);
582  return -EINVAL;
583  }
584  if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
585  release_sock(sk);
586  return -EINVAL;
587  }
588  if (addr->fsa_ax25.sax25_family != AF_NETROM) {
589  release_sock(sk);
590  return -EINVAL;
591  }
592  if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
593  release_sock(sk);
594  return -EADDRNOTAVAIL;
595  }
596 
597  /*
598  * Only the super user can set an arbitrary user callsign.
599  */
600  if (addr->fsa_ax25.sax25_ndigis == 1) {
602  dev_put(dev);
603  release_sock(sk);
604  return -EPERM;
605  }
606  nr->user_addr = addr->fsa_digipeater[0];
607  nr->source_addr = addr->fsa_ax25.sax25_call;
608  } else {
609  source = &addr->fsa_ax25.sax25_call;
610 
611  user = ax25_findbyuid(current_euid());
612  if (user) {
613  nr->user_addr = user->call;
614  ax25_uid_put(user);
615  } else {
617  release_sock(sk);
618  dev_put(dev);
619  return -EPERM;
620  }
621  nr->user_addr = *source;
622  }
623 
624  nr->source_addr = *source;
625  }
626 
627  nr->device = dev;
628  nr_insert_socket(sk);
629 
630  sock_reset_flag(sk, SOCK_ZAPPED);
631  dev_put(dev);
632  release_sock(sk);
633 
634  return 0;
635 }
636 
637 static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
638  int addr_len, int flags)
639 {
640  struct sock *sk = sock->sk;
641  struct nr_sock *nr = nr_sk(sk);
642  struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
643  ax25_address *source = NULL;
645  struct net_device *dev;
646  int err = 0;
647 
648  lock_sock(sk);
649  if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
650  sock->state = SS_CONNECTED;
651  goto out_release; /* Connect completed during a ERESTARTSYS event */
652  }
653 
654  if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
655  sock->state = SS_UNCONNECTED;
656  err = -ECONNREFUSED;
657  goto out_release;
658  }
659 
660  if (sk->sk_state == TCP_ESTABLISHED) {
661  err = -EISCONN; /* No reconnect on a seqpacket socket */
662  goto out_release;
663  }
664 
665  sk->sk_state = TCP_CLOSE;
666  sock->state = SS_UNCONNECTED;
667 
668  if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
669  err = -EINVAL;
670  goto out_release;
671  }
672  if (addr->sax25_family != AF_NETROM) {
673  err = -EINVAL;
674  goto out_release;
675  }
676  if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
677  sock_reset_flag(sk, SOCK_ZAPPED);
678 
679  if ((dev = nr_dev_first()) == NULL) {
680  err = -ENETUNREACH;
681  goto out_release;
682  }
683  source = (ax25_address *)dev->dev_addr;
684 
685  user = ax25_findbyuid(current_euid());
686  if (user) {
687  nr->user_addr = user->call;
688  ax25_uid_put(user);
689  } else {
691  dev_put(dev);
692  err = -EPERM;
693  goto out_release;
694  }
695  nr->user_addr = *source;
696  }
697 
698  nr->source_addr = *source;
699  nr->device = dev;
700 
701  dev_put(dev);
702  nr_insert_socket(sk); /* Finish the bind */
703  }
704 
705  nr->dest_addr = addr->sax25_call;
706 
707  release_sock(sk);
708  circuit = nr_find_next_circuit();
709  lock_sock(sk);
710 
711  nr->my_index = circuit / 256;
712  nr->my_id = circuit % 256;
713 
714  circuit++;
715 
716  /* Move to connecting socket, start sending Connect Requests */
717  sock->state = SS_CONNECTING;
718  sk->sk_state = TCP_SYN_SENT;
719 
721 
722  nr->state = NR_STATE_1;
723 
724  nr_start_heartbeat(sk);
725 
726  /* Now the loop */
727  if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
728  err = -EINPROGRESS;
729  goto out_release;
730  }
731 
732  /*
733  * A Connect Ack with Choke or timeout or failed routing will go to
734  * closed.
735  */
736  if (sk->sk_state == TCP_SYN_SENT) {
737  DEFINE_WAIT(wait);
738 
739  for (;;) {
740  prepare_to_wait(sk_sleep(sk), &wait,
742  if (sk->sk_state != TCP_SYN_SENT)
743  break;
744  if (!signal_pending(current)) {
745  release_sock(sk);
746  schedule();
747  lock_sock(sk);
748  continue;
749  }
750  err = -ERESTARTSYS;
751  break;
752  }
753  finish_wait(sk_sleep(sk), &wait);
754  if (err)
755  goto out_release;
756  }
757 
758  if (sk->sk_state != TCP_ESTABLISHED) {
759  sock->state = SS_UNCONNECTED;
760  err = sock_error(sk); /* Always set at this point */
761  goto out_release;
762  }
763 
764  sock->state = SS_CONNECTED;
765 
766 out_release:
767  release_sock(sk);
768 
769  return err;
770 }
771 
772 static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
773 {
774  struct sk_buff *skb;
775  struct sock *newsk;
776  DEFINE_WAIT(wait);
777  struct sock *sk;
778  int err = 0;
779 
780  if ((sk = sock->sk) == NULL)
781  return -EINVAL;
782 
783  lock_sock(sk);
784  if (sk->sk_type != SOCK_SEQPACKET) {
785  err = -EOPNOTSUPP;
786  goto out_release;
787  }
788 
789  if (sk->sk_state != TCP_LISTEN) {
790  err = -EINVAL;
791  goto out_release;
792  }
793 
794  /*
795  * The write queue this time is holding sockets ready to use
796  * hooked into the SABM we saved
797  */
798  for (;;) {
799  prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
800  skb = skb_dequeue(&sk->sk_receive_queue);
801  if (skb)
802  break;
803 
804  if (flags & O_NONBLOCK) {
805  err = -EWOULDBLOCK;
806  break;
807  }
808  if (!signal_pending(current)) {
809  release_sock(sk);
810  schedule();
811  lock_sock(sk);
812  continue;
813  }
814  err = -ERESTARTSYS;
815  break;
816  }
817  finish_wait(sk_sleep(sk), &wait);
818  if (err)
819  goto out_release;
820 
821  newsk = skb->sk;
822  sock_graft(newsk, newsock);
823 
824  /* Now attach up the new socket */
825  kfree_skb(skb);
826  sk_acceptq_removed(sk);
827 
828 out_release:
829  release_sock(sk);
830 
831  return err;
832 }
833 
834 static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
835  int *uaddr_len, int peer)
836 {
837  struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
838  struct sock *sk = sock->sk;
839  struct nr_sock *nr = nr_sk(sk);
840 
841  lock_sock(sk);
842  if (peer != 0) {
843  if (sk->sk_state != TCP_ESTABLISHED) {
844  release_sock(sk);
845  return -ENOTCONN;
846  }
847  sax->fsa_ax25.sax25_family = AF_NETROM;
848  sax->fsa_ax25.sax25_ndigis = 1;
849  sax->fsa_ax25.sax25_call = nr->user_addr;
850  memset(sax->fsa_digipeater, 0, sizeof(sax->fsa_digipeater));
851  sax->fsa_digipeater[0] = nr->dest_addr;
852  *uaddr_len = sizeof(struct full_sockaddr_ax25);
853  } else {
854  sax->fsa_ax25.sax25_family = AF_NETROM;
855  sax->fsa_ax25.sax25_ndigis = 0;
856  sax->fsa_ax25.sax25_call = nr->source_addr;
857  *uaddr_len = sizeof(struct sockaddr_ax25);
858  }
859  release_sock(sk);
860 
861  return 0;
862 }
863 
864 int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
865 {
866  struct sock *sk;
867  struct sock *make;
868  struct nr_sock *nr_make;
869  ax25_address *src, *dest, *user;
870  unsigned short circuit_index, circuit_id;
871  unsigned short peer_circuit_index, peer_circuit_id;
872  unsigned short frametype, flags, window, timeout;
873  int ret;
874 
875  skb->sk = NULL; /* Initially we don't know who it's for */
876 
877  /*
878  * skb->data points to the netrom frame start
879  */
880 
881  src = (ax25_address *)(skb->data + 0);
882  dest = (ax25_address *)(skb->data + 7);
883 
884  circuit_index = skb->data[15];
885  circuit_id = skb->data[16];
886  peer_circuit_index = skb->data[17];
887  peer_circuit_id = skb->data[18];
888  frametype = skb->data[19] & 0x0F;
889  flags = skb->data[19] & 0xF0;
890 
891  /*
892  * Check for an incoming IP over NET/ROM frame.
893  */
894  if (frametype == NR_PROTOEXT &&
895  circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
897  skb_reset_transport_header(skb);
898 
899  return nr_rx_ip(skb, dev);
900  }
901 
902  /*
903  * Find an existing socket connection, based on circuit ID, if it's
904  * a Connect Request base it on their circuit ID.
905  *
906  * Circuit ID 0/0 is not valid but it could still be a "reset" for a
907  * circuit that no longer exists at the other end ...
908  */
909 
910  sk = NULL;
911 
912  if (circuit_index == 0 && circuit_id == 0) {
913  if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
914  sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
915  } else {
916  if (frametype == NR_CONNREQ)
917  sk = nr_find_peer(circuit_index, circuit_id, src);
918  else
919  sk = nr_find_socket(circuit_index, circuit_id);
920  }
921 
922  if (sk != NULL) {
923  skb_reset_transport_header(skb);
924 
925  if (frametype == NR_CONNACK && skb->len == 22)
926  nr_sk(sk)->bpqext = 1;
927  else
928  nr_sk(sk)->bpqext = 0;
929 
930  ret = nr_process_rx_frame(sk, skb);
931  bh_unlock_sock(sk);
932  return ret;
933  }
934 
935  /*
936  * Now it should be a CONNREQ.
937  */
938  if (frametype != NR_CONNREQ) {
939  /*
940  * Here it would be nice to be able to send a reset but
941  * NET/ROM doesn't have one. We've tried to extend the protocol
942  * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
943  * apparently kills BPQ boxes... :-(
944  * So now we try to follow the established behaviour of
945  * G8PZT's Xrouter which is sending packets with command type 7
946  * as an extension of the protocol.
947  */
949  (frametype != NR_RESET || flags != 0))
950  nr_transmit_reset(skb, 1);
951 
952  return 0;
953  }
954 
955  sk = nr_find_listener(dest);
956 
957  user = (ax25_address *)(skb->data + 21);
958 
959  if (sk == NULL || sk_acceptq_is_full(sk) ||
960  (make = nr_make_new(sk)) == NULL) {
961  nr_transmit_refusal(skb, 0);
962  if (sk)
963  bh_unlock_sock(sk);
964  return 0;
965  }
966 
967  window = skb->data[20];
968 
969  skb->sk = make;
970  make->sk_state = TCP_ESTABLISHED;
971 
972  /* Fill in his circuit details */
973  nr_make = nr_sk(make);
974  nr_make->source_addr = *dest;
975  nr_make->dest_addr = *src;
976  nr_make->user_addr = *user;
977 
978  nr_make->your_index = circuit_index;
979  nr_make->your_id = circuit_id;
980 
981  bh_unlock_sock(sk);
982  circuit = nr_find_next_circuit();
983  bh_lock_sock(sk);
984 
985  nr_make->my_index = circuit / 256;
986  nr_make->my_id = circuit % 256;
987 
988  circuit++;
989 
990  /* Window negotiation */
991  if (window < nr_make->window)
992  nr_make->window = window;
993 
994  /* L4 timeout negotiation */
995  if (skb->len == 37) {
996  timeout = skb->data[36] * 256 + skb->data[35];
997  if (timeout * HZ < nr_make->t1)
998  nr_make->t1 = timeout * HZ;
999  nr_make->bpqext = 1;
1000  } else {
1001  nr_make->bpqext = 0;
1002  }
1003 
1005 
1006  nr_make->condition = 0x00;
1007  nr_make->vs = 0;
1008  nr_make->va = 0;
1009  nr_make->vr = 0;
1010  nr_make->vl = 0;
1011  nr_make->state = NR_STATE_3;
1012  sk_acceptq_added(sk);
1013  skb_queue_head(&sk->sk_receive_queue, skb);
1014 
1015  if (!sock_flag(sk, SOCK_DEAD))
1016  sk->sk_data_ready(sk, skb->len);
1017 
1018  bh_unlock_sock(sk);
1019 
1020  nr_insert_socket(make);
1021 
1022  nr_start_heartbeat(make);
1023  nr_start_idletimer(make);
1024 
1025  return 1;
1026 }
1027 
1028 static int nr_sendmsg(struct kiocb *iocb, struct socket *sock,
1029  struct msghdr *msg, size_t len)
1030 {
1031  struct sock *sk = sock->sk;
1032  struct nr_sock *nr = nr_sk(sk);
1033  struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1034  int err;
1035  struct sockaddr_ax25 sax;
1036  struct sk_buff *skb;
1037  unsigned char *asmptr;
1038  int size;
1039 
1041  return -EINVAL;
1042 
1043  lock_sock(sk);
1044  if (sock_flag(sk, SOCK_ZAPPED)) {
1045  err = -EADDRNOTAVAIL;
1046  goto out;
1047  }
1048 
1049  if (sk->sk_shutdown & SEND_SHUTDOWN) {
1050  send_sig(SIGPIPE, current, 0);
1051  err = -EPIPE;
1052  goto out;
1053  }
1054 
1055  if (nr->device == NULL) {
1056  err = -ENETUNREACH;
1057  goto out;
1058  }
1059 
1060  if (usax) {
1061  if (msg->msg_namelen < sizeof(sax)) {
1062  err = -EINVAL;
1063  goto out;
1064  }
1065  sax = *usax;
1066  if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
1067  err = -EISCONN;
1068  goto out;
1069  }
1070  if (sax.sax25_family != AF_NETROM) {
1071  err = -EINVAL;
1072  goto out;
1073  }
1074  } else {
1075  if (sk->sk_state != TCP_ESTABLISHED) {
1076  err = -ENOTCONN;
1077  goto out;
1078  }
1079  sax.sax25_family = AF_NETROM;
1080  sax.sax25_call = nr->dest_addr;
1081  }
1082 
1083  /* Build a packet - the conventional user limit is 236 bytes. We can
1084  do ludicrously large NetROM frames but must not overflow */
1085  if (len > 65536) {
1086  err = -EMSGSIZE;
1087  goto out;
1088  }
1089 
1091 
1092  if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1093  goto out;
1094 
1095  skb_reserve(skb, size - len);
1096  skb_reset_transport_header(skb);
1097 
1098  /*
1099  * Push down the NET/ROM header
1100  */
1101 
1102  asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1103 
1104  /* Build a NET/ROM Transport header */
1105 
1106  *asmptr++ = nr->your_index;
1107  *asmptr++ = nr->your_id;
1108  *asmptr++ = 0; /* To be filled in later */
1109  *asmptr++ = 0; /* Ditto */
1110  *asmptr++ = NR_INFO;
1111 
1112  /*
1113  * Put the data on the end
1114  */
1115  skb_put(skb, len);
1116 
1117  /* User data follows immediately after the NET/ROM transport header */
1118  if (memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len)) {
1119  kfree_skb(skb);
1120  err = -EFAULT;
1121  goto out;
1122  }
1123 
1124  if (sk->sk_state != TCP_ESTABLISHED) {
1125  kfree_skb(skb);
1126  err = -ENOTCONN;
1127  goto out;
1128  }
1129 
1130  nr_output(sk, skb); /* Shove it onto the queue */
1131 
1132  err = len;
1133 out:
1134  release_sock(sk);
1135  return err;
1136 }
1137 
1138 static int nr_recvmsg(struct kiocb *iocb, struct socket *sock,
1139  struct msghdr *msg, size_t size, int flags)
1140 {
1141  struct sock *sk = sock->sk;
1142  struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1143  size_t copied;
1144  struct sk_buff *skb;
1145  int er;
1146 
1147  /*
1148  * This works for seqpacket too. The receiver has ordered the queue for
1149  * us! We do one quick check first though
1150  */
1151 
1152  lock_sock(sk);
1153  if (sk->sk_state != TCP_ESTABLISHED) {
1154  release_sock(sk);
1155  return -ENOTCONN;
1156  }
1157 
1158  /* Now we can treat all alike */
1159  if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
1160  release_sock(sk);
1161  return er;
1162  }
1163 
1164  skb_reset_transport_header(skb);
1165  copied = skb->len;
1166 
1167  if (copied > size) {
1168  copied = size;
1169  msg->msg_flags |= MSG_TRUNC;
1170  }
1171 
1172  er = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1173  if (er < 0) {
1174  skb_free_datagram(sk, skb);
1175  release_sock(sk);
1176  return er;
1177  }
1178 
1179  if (sax != NULL) {
1180  sax->sax25_family = AF_NETROM;
1181  skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call,
1182  AX25_ADDR_LEN);
1183  }
1184 
1185  msg->msg_namelen = sizeof(*sax);
1186 
1187  skb_free_datagram(sk, skb);
1188 
1189  release_sock(sk);
1190  return copied;
1191 }
1192 
1193 
1194 static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1195 {
1196  struct sock *sk = sock->sk;
1197  void __user *argp = (void __user *)arg;
1198  int ret;
1199 
1200  switch (cmd) {
1201  case TIOCOUTQ: {
1202  long amount;
1203 
1204  lock_sock(sk);
1205  amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1206  if (amount < 0)
1207  amount = 0;
1208  release_sock(sk);
1209  return put_user(amount, (int __user *)argp);
1210  }
1211 
1212  case TIOCINQ: {
1213  struct sk_buff *skb;
1214  long amount = 0L;
1215 
1216  lock_sock(sk);
1217  /* These two are safe on a single CPU system as only user tasks fiddle here */
1218  if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1219  amount = skb->len;
1220  release_sock(sk);
1221  return put_user(amount, (int __user *)argp);
1222  }
1223 
1224  case SIOCGSTAMP:
1225  lock_sock(sk);
1226  ret = sock_get_timestamp(sk, argp);
1227  release_sock(sk);
1228  return ret;
1229 
1230  case SIOCGSTAMPNS:
1231  lock_sock(sk);
1232  ret = sock_get_timestampns(sk, argp);
1233  release_sock(sk);
1234  return ret;
1235 
1236  case SIOCGIFADDR:
1237  case SIOCSIFADDR:
1238  case SIOCGIFDSTADDR:
1239  case SIOCSIFDSTADDR:
1240  case SIOCGIFBRDADDR:
1241  case SIOCSIFBRDADDR:
1242  case SIOCGIFNETMASK:
1243  case SIOCSIFNETMASK:
1244  case SIOCGIFMETRIC:
1245  case SIOCSIFMETRIC:
1246  return -EINVAL;
1247 
1248  case SIOCADDRT:
1249  case SIOCDELRT:
1250  case SIOCNRDECOBS:
1251  if (!capable(CAP_NET_ADMIN))
1252  return -EPERM;
1253  return nr_rt_ioctl(cmd, argp);
1254 
1255  default:
1256  return -ENOIOCTLCMD;
1257  }
1258 
1259  return 0;
1260 }
1261 
1262 #ifdef CONFIG_PROC_FS
1263 
1264 static void *nr_info_start(struct seq_file *seq, loff_t *pos)
1265 {
1266  spin_lock_bh(&nr_list_lock);
1267  return seq_hlist_start_head(&nr_list, *pos);
1268 }
1269 
1270 static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
1271 {
1272  return seq_hlist_next(v, &nr_list, pos);
1273 }
1274 
1275 static void nr_info_stop(struct seq_file *seq, void *v)
1276 {
1277  spin_unlock_bh(&nr_list_lock);
1278 }
1279 
1280 static int nr_info_show(struct seq_file *seq, void *v)
1281 {
1282  struct sock *s = sk_entry(v);
1283  struct net_device *dev;
1284  struct nr_sock *nr;
1285  const char *devname;
1286  char buf[11];
1287 
1288  if (v == SEQ_START_TOKEN)
1289  seq_puts(seq,
1290 "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");
1291 
1292  else {
1293 
1294  bh_lock_sock(s);
1295  nr = nr_sk(s);
1296 
1297  if ((dev = nr->device) == NULL)
1298  devname = "???";
1299  else
1300  devname = dev->name;
1301 
1302  seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr));
1303  seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr));
1304  seq_printf(seq,
1305 "%-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",
1306  ax2asc(buf, &nr->source_addr),
1307  devname,
1308  nr->my_index,
1309  nr->my_id,
1310  nr->your_index,
1311  nr->your_id,
1312  nr->state,
1313  nr->vs,
1314  nr->vr,
1315  nr->va,
1316  ax25_display_timer(&nr->t1timer) / HZ,
1317  nr->t1 / HZ,
1318  ax25_display_timer(&nr->t2timer) / HZ,
1319  nr->t2 / HZ,
1320  ax25_display_timer(&nr->t4timer) / HZ,
1321  nr->t4 / HZ,
1322  ax25_display_timer(&nr->idletimer) / (60 * HZ),
1323  nr->idle / (60 * HZ),
1324  nr->n2count,
1325  nr->n2,
1326  nr->window,
1327  sk_wmem_alloc_get(s),
1328  sk_rmem_alloc_get(s),
1329  s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1330 
1331  bh_unlock_sock(s);
1332  }
1333  return 0;
1334 }
1335 
1336 static const struct seq_operations nr_info_seqops = {
1337  .start = nr_info_start,
1338  .next = nr_info_next,
1339  .stop = nr_info_stop,
1340  .show = nr_info_show,
1341 };
1342 
1343 static int nr_info_open(struct inode *inode, struct file *file)
1344 {
1345  return seq_open(file, &nr_info_seqops);
1346 }
1347 
1348 static const struct file_operations nr_info_fops = {
1349  .owner = THIS_MODULE,
1350  .open = nr_info_open,
1351  .read = seq_read,
1352  .llseek = seq_lseek,
1353  .release = seq_release,
1354 };
1355 #endif /* CONFIG_PROC_FS */
1356 
1357 static const struct net_proto_family nr_family_ops = {
1358  .family = PF_NETROM,
1359  .create = nr_create,
1360  .owner = THIS_MODULE,
1361 };
1362 
1363 static const struct proto_ops nr_proto_ops = {
1364  .family = PF_NETROM,
1365  .owner = THIS_MODULE,
1366  .release = nr_release,
1367  .bind = nr_bind,
1368  .connect = nr_connect,
1369  .socketpair = sock_no_socketpair,
1370  .accept = nr_accept,
1371  .getname = nr_getname,
1372  .poll = datagram_poll,
1373  .ioctl = nr_ioctl,
1374  .listen = nr_listen,
1375  .shutdown = sock_no_shutdown,
1376  .setsockopt = nr_setsockopt,
1377  .getsockopt = nr_getsockopt,
1378  .sendmsg = nr_sendmsg,
1379  .recvmsg = nr_recvmsg,
1380  .mmap = sock_no_mmap,
1381  .sendpage = sock_no_sendpage,
1382 };
1383 
1384 static struct notifier_block nr_dev_notifier = {
1385  .notifier_call = nr_device_event,
1386 };
1387 
1388 static struct net_device **dev_nr;
1389 
1390 static struct ax25_protocol nr_pid = {
1391  .pid = AX25_P_NETROM,
1392  .func = nr_route_frame
1393 };
1394 
1395 static struct ax25_linkfail nr_linkfail_notifier = {
1396  .func = nr_link_failed,
1397 };
1398 
1399 static int __init nr_proto_init(void)
1400 {
1401  int i;
1402  int rc = proto_register(&nr_proto, 0);
1403 
1404  if (rc != 0)
1405  goto out;
1406 
1407  if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
1408  printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
1409  return -1;
1410  }
1411 
1412  dev_nr = kzalloc(nr_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1413  if (dev_nr == NULL) {
1414  printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device array\n");
1415  return -1;
1416  }
1417 
1418  for (i = 0; i < nr_ndevs; i++) {
1419  char name[IFNAMSIZ];
1420  struct net_device *dev;
1421 
1422  sprintf(name, "nr%d", i);
1423  dev = alloc_netdev(0, name, nr_setup);
1424  if (!dev) {
1425  printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
1426  goto fail;
1427  }
1428 
1429  dev->base_addr = i;
1430  if (register_netdev(dev)) {
1431  printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n");
1432  free_netdev(dev);
1433  goto fail;
1434  }
1435  nr_set_lockdep_key(dev);
1436  dev_nr[i] = dev;
1437  }
1438 
1439  if (sock_register(&nr_family_ops)) {
1440  printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n");
1441  goto fail;
1442  }
1443 
1444  register_netdevice_notifier(&nr_dev_notifier);
1445 
1446  ax25_register_pid(&nr_pid);
1447  ax25_linkfail_register(&nr_linkfail_notifier);
1448 
1449 #ifdef CONFIG_SYSCTL
1451 #endif
1452 
1453  nr_loopback_init();
1454 
1455  proc_net_fops_create(&init_net, "nr", S_IRUGO, &nr_info_fops);
1456  proc_net_fops_create(&init_net, "nr_neigh", S_IRUGO, &nr_neigh_fops);
1457  proc_net_fops_create(&init_net, "nr_nodes", S_IRUGO, &nr_nodes_fops);
1458 out:
1459  return rc;
1460 fail:
1461  while (--i >= 0) {
1462  unregister_netdev(dev_nr[i]);
1463  free_netdev(dev_nr[i]);
1464  }
1465  kfree(dev_nr);
1466  proto_unregister(&nr_proto);
1467  rc = -1;
1468  goto out;
1469 }
1470 
1471 module_init(nr_proto_init);
1472 
1473 module_param(nr_ndevs, int, 0);
1474 MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1475 
1476 MODULE_AUTHOR("Jonathan Naylor G4KLX <[email protected]>");
1477 MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1478 MODULE_LICENSE("GPL");
1480 
1481 static void __exit nr_exit(void)
1482 {
1483  int i;
1484 
1485  proc_net_remove(&init_net, "nr");
1486  proc_net_remove(&init_net, "nr_neigh");
1487  proc_net_remove(&init_net, "nr_nodes");
1489 
1490  nr_rt_free();
1491 
1492 #ifdef CONFIG_SYSCTL
1494 #endif
1495 
1496  ax25_linkfail_release(&nr_linkfail_notifier);
1498 
1499  unregister_netdevice_notifier(&nr_dev_notifier);
1500 
1502 
1503  for (i = 0; i < nr_ndevs; i++) {
1504  struct net_device *dev = dev_nr[i];
1505  if (dev) {
1506  unregister_netdev(dev);
1507  free_netdev(dev);
1508  }
1509  }
1510 
1511  kfree(dev_nr);
1512  proto_unregister(&nr_proto);
1513 }
1514 module_exit(nr_exit);