net: igmp: Use correct source address on IGMPv3 reports
[pandora-kernel.git] / net / rose / af_rose.c
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 (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8  * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
9  * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
10  * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi)
11  */
12
13 #include <linux/capability.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/in.h>
21 #include <linux/slab.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/spinlock.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/stat.h>
30 #include <net/net_namespace.h>
31 #include <net/ax25.h>
32 #include <linux/inet.h>
33 #include <linux/netdevice.h>
34 #include <linux/if_arp.h>
35 #include <linux/skbuff.h>
36 #include <net/sock.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include <linux/fcntl.h>
40 #include <linux/termios.h>
41 #include <linux/mm.h>
42 #include <linux/interrupt.h>
43 #include <linux/notifier.h>
44 #include <net/rose.h>
45 #include <linux/proc_fs.h>
46 #include <linux/seq_file.h>
47 #include <net/tcp_states.h>
48 #include <net/ip.h>
49 #include <net/arp.h>
50
51 static int rose_ndevs = 10;
52
53 int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0;
54 int sysctl_rose_call_request_timeout    = ROSE_DEFAULT_T1;
55 int sysctl_rose_reset_request_timeout   = ROSE_DEFAULT_T2;
56 int sysctl_rose_clear_request_timeout   = ROSE_DEFAULT_T3;
57 int sysctl_rose_no_activity_timeout     = ROSE_DEFAULT_IDLE;
58 int sysctl_rose_ack_hold_back_timeout   = ROSE_DEFAULT_HB;
59 int sysctl_rose_routing_control         = ROSE_DEFAULT_ROUTING;
60 int sysctl_rose_link_fail_timeout       = ROSE_DEFAULT_FAIL_TIMEOUT;
61 int sysctl_rose_maximum_vcs             = ROSE_DEFAULT_MAXVC;
62 int sysctl_rose_window_size             = ROSE_DEFAULT_WINDOW_SIZE;
63
64 static HLIST_HEAD(rose_list);
65 static DEFINE_SPINLOCK(rose_list_lock);
66
67 static const struct proto_ops rose_proto_ops;
68
69 ax25_address rose_callsign;
70
71 /*
72  * ROSE network devices are virtual network devices encapsulating ROSE
73  * frames into AX.25 which will be sent through an AX.25 device, so form a
74  * special "super class" of normal net devices; split their locks off into a
75  * separate class since they always nest.
76  */
77 static struct lock_class_key rose_netdev_xmit_lock_key;
78 static struct lock_class_key rose_netdev_addr_lock_key;
79
80 static void rose_set_lockdep_one(struct net_device *dev,
81                                  struct netdev_queue *txq,
82                                  void *_unused)
83 {
84         lockdep_set_class(&txq->_xmit_lock, &rose_netdev_xmit_lock_key);
85 }
86
87 static void rose_set_lockdep_key(struct net_device *dev)
88 {
89         lockdep_set_class(&dev->addr_list_lock, &rose_netdev_addr_lock_key);
90         netdev_for_each_tx_queue(dev, rose_set_lockdep_one, NULL);
91 }
92
93 /*
94  *      Convert a ROSE address into text.
95  */
96 char *rose2asc(char *buf, const rose_address *addr)
97 {
98         if (addr->rose_addr[0] == 0x00 && addr->rose_addr[1] == 0x00 &&
99             addr->rose_addr[2] == 0x00 && addr->rose_addr[3] == 0x00 &&
100             addr->rose_addr[4] == 0x00) {
101                 strcpy(buf, "*");
102         } else {
103                 sprintf(buf, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF,
104                                                 addr->rose_addr[1] & 0xFF,
105                                                 addr->rose_addr[2] & 0xFF,
106                                                 addr->rose_addr[3] & 0xFF,
107                                                 addr->rose_addr[4] & 0xFF);
108         }
109
110         return buf;
111 }
112
113 /*
114  *      Compare two ROSE addresses, 0 == equal.
115  */
116 int rosecmp(rose_address *addr1, rose_address *addr2)
117 {
118         int i;
119
120         for (i = 0; i < 5; i++)
121                 if (addr1->rose_addr[i] != addr2->rose_addr[i])
122                         return 1;
123
124         return 0;
125 }
126
127 /*
128  *      Compare two ROSE addresses for only mask digits, 0 == equal.
129  */
130 int rosecmpm(rose_address *addr1, rose_address *addr2, unsigned short mask)
131 {
132         unsigned int i, j;
133
134         if (mask > 10)
135                 return 1;
136
137         for (i = 0; i < mask; i++) {
138                 j = i / 2;
139
140                 if ((i % 2) != 0) {
141                         if ((addr1->rose_addr[j] & 0x0F) != (addr2->rose_addr[j] & 0x0F))
142                                 return 1;
143                 } else {
144                         if ((addr1->rose_addr[j] & 0xF0) != (addr2->rose_addr[j] & 0xF0))
145                                 return 1;
146                 }
147         }
148
149         return 0;
150 }
151
152 /*
153  *      Socket removal during an interrupt is now safe.
154  */
155 static void rose_remove_socket(struct sock *sk)
156 {
157         spin_lock_bh(&rose_list_lock);
158         sk_del_node_init(sk);
159         spin_unlock_bh(&rose_list_lock);
160 }
161
162 /*
163  *      Kill all bound sockets on a broken link layer connection to a
164  *      particular neighbour.
165  */
166 void rose_kill_by_neigh(struct rose_neigh *neigh)
167 {
168         struct sock *s;
169         struct hlist_node *node;
170
171         spin_lock_bh(&rose_list_lock);
172         sk_for_each(s, node, &rose_list) {
173                 struct rose_sock *rose = rose_sk(s);
174
175                 if (rose->neighbour == neigh) {
176                         rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
177                         rose->neighbour->use--;
178                         rose->neighbour = NULL;
179                 }
180         }
181         spin_unlock_bh(&rose_list_lock);
182 }
183
184 /*
185  *      Kill all bound sockets on a dropped device.
186  */
187 static void rose_kill_by_device(struct net_device *dev)
188 {
189         struct sock *s;
190         struct hlist_node *node;
191
192         spin_lock_bh(&rose_list_lock);
193         sk_for_each(s, node, &rose_list) {
194                 struct rose_sock *rose = rose_sk(s);
195
196                 if (rose->device == dev) {
197                         rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
198                         if (rose->neighbour)
199                                 rose->neighbour->use--;
200                         rose->device = NULL;
201                 }
202         }
203         spin_unlock_bh(&rose_list_lock);
204 }
205
206 /*
207  *      Handle device status changes.
208  */
209 static int rose_device_event(struct notifier_block *this, unsigned long event,
210         void *ptr)
211 {
212         struct net_device *dev = (struct net_device *)ptr;
213
214         if (!net_eq(dev_net(dev), &init_net))
215                 return NOTIFY_DONE;
216
217         if (event != NETDEV_DOWN)
218                 return NOTIFY_DONE;
219
220         switch (dev->type) {
221         case ARPHRD_ROSE:
222                 rose_kill_by_device(dev);
223                 break;
224         case ARPHRD_AX25:
225                 rose_link_device_down(dev);
226                 rose_rt_device_down(dev);
227                 break;
228         }
229
230         return NOTIFY_DONE;
231 }
232
233 /*
234  *      Add a socket to the bound sockets list.
235  */
236 static void rose_insert_socket(struct sock *sk)
237 {
238
239         spin_lock_bh(&rose_list_lock);
240         sk_add_node(sk, &rose_list);
241         spin_unlock_bh(&rose_list_lock);
242 }
243
244 /*
245  *      Find a socket that wants to accept the Call Request we just
246  *      received.
247  */
248 static struct sock *rose_find_listener(rose_address *addr, ax25_address *call)
249 {
250         struct sock *s;
251         struct hlist_node *node;
252
253         spin_lock_bh(&rose_list_lock);
254         sk_for_each(s, node, &rose_list) {
255                 struct rose_sock *rose = rose_sk(s);
256
257                 if (!rosecmp(&rose->source_addr, addr) &&
258                     !ax25cmp(&rose->source_call, call) &&
259                     !rose->source_ndigis && s->sk_state == TCP_LISTEN)
260                         goto found;
261         }
262
263         sk_for_each(s, node, &rose_list) {
264                 struct rose_sock *rose = rose_sk(s);
265
266                 if (!rosecmp(&rose->source_addr, addr) &&
267                     !ax25cmp(&rose->source_call, &null_ax25_address) &&
268                     s->sk_state == TCP_LISTEN)
269                         goto found;
270         }
271         s = NULL;
272 found:
273         spin_unlock_bh(&rose_list_lock);
274         return s;
275 }
276
277 /*
278  *      Find a connected ROSE socket given my LCI and device.
279  */
280 struct sock *rose_find_socket(unsigned int lci, struct rose_neigh *neigh)
281 {
282         struct sock *s;
283         struct hlist_node *node;
284
285         spin_lock_bh(&rose_list_lock);
286         sk_for_each(s, node, &rose_list) {
287                 struct rose_sock *rose = rose_sk(s);
288
289                 if (rose->lci == lci && rose->neighbour == neigh)
290                         goto found;
291         }
292         s = NULL;
293 found:
294         spin_unlock_bh(&rose_list_lock);
295         return s;
296 }
297
298 /*
299  *      Find a unique LCI for a given device.
300  */
301 unsigned int rose_new_lci(struct rose_neigh *neigh)
302 {
303         int lci;
304
305         if (neigh->dce_mode) {
306                 for (lci = 1; lci <= sysctl_rose_maximum_vcs; lci++)
307                         if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
308                                 return lci;
309         } else {
310                 for (lci = sysctl_rose_maximum_vcs; lci > 0; lci--)
311                         if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
312                                 return lci;
313         }
314
315         return 0;
316 }
317
318 /*
319  *      Deferred destroy.
320  */
321 void rose_destroy_socket(struct sock *);
322
323 /*
324  *      Handler for deferred kills.
325  */
326 static void rose_destroy_timer(unsigned long data)
327 {
328         rose_destroy_socket((struct sock *)data);
329 }
330
331 /*
332  *      This is called from user mode and the timers. Thus it protects itself
333  *      against interrupt users but doesn't worry about being called during
334  *      work.  Once it is removed from the queue no interrupt or bottom half
335  *      will touch it and we are (fairly 8-) ) safe.
336  */
337 void rose_destroy_socket(struct sock *sk)
338 {
339         struct sk_buff *skb;
340
341         rose_remove_socket(sk);
342         rose_stop_heartbeat(sk);
343         rose_stop_idletimer(sk);
344         rose_stop_timer(sk);
345
346         rose_clear_queues(sk);          /* Flush the queues */
347
348         while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
349                 if (skb->sk != sk) {    /* A pending connection */
350                         /* Queue the unaccepted socket for death */
351                         sock_set_flag(skb->sk, SOCK_DEAD);
352                         rose_start_heartbeat(skb->sk);
353                         rose_sk(skb->sk)->state = ROSE_STATE_0;
354                 }
355
356                 kfree_skb(skb);
357         }
358
359         if (sk_has_allocations(sk)) {
360                 /* Defer: outstanding buffers */
361                 setup_timer(&sk->sk_timer, rose_destroy_timer,
362                                 (unsigned long)sk);
363                 sk->sk_timer.expires  = jiffies + 10 * HZ;
364                 add_timer(&sk->sk_timer);
365         } else
366                 sock_put(sk);
367 }
368
369 /*
370  *      Handling for system calls applied via the various interfaces to a
371  *      ROSE socket object.
372  */
373
374 static int rose_setsockopt(struct socket *sock, int level, int optname,
375         char __user *optval, unsigned int optlen)
376 {
377         struct sock *sk = sock->sk;
378         struct rose_sock *rose = rose_sk(sk);
379         int opt;
380
381         if (level != SOL_ROSE)
382                 return -ENOPROTOOPT;
383
384         if (optlen < sizeof(int))
385                 return -EINVAL;
386
387         if (get_user(opt, (int __user *)optval))
388                 return -EFAULT;
389
390         switch (optname) {
391         case ROSE_DEFER:
392                 rose->defer = opt ? 1 : 0;
393                 return 0;
394
395         case ROSE_T1:
396                 if (opt < 1)
397                         return -EINVAL;
398                 rose->t1 = opt * HZ;
399                 return 0;
400
401         case ROSE_T2:
402                 if (opt < 1)
403                         return -EINVAL;
404                 rose->t2 = opt * HZ;
405                 return 0;
406
407         case ROSE_T3:
408                 if (opt < 1)
409                         return -EINVAL;
410                 rose->t3 = opt * HZ;
411                 return 0;
412
413         case ROSE_HOLDBACK:
414                 if (opt < 1)
415                         return -EINVAL;
416                 rose->hb = opt * HZ;
417                 return 0;
418
419         case ROSE_IDLE:
420                 if (opt < 0)
421                         return -EINVAL;
422                 rose->idle = opt * 60 * HZ;
423                 return 0;
424
425         case ROSE_QBITINCL:
426                 rose->qbitincl = opt ? 1 : 0;
427                 return 0;
428
429         default:
430                 return -ENOPROTOOPT;
431         }
432 }
433
434 static int rose_getsockopt(struct socket *sock, int level, int optname,
435         char __user *optval, int __user *optlen)
436 {
437         struct sock *sk = sock->sk;
438         struct rose_sock *rose = rose_sk(sk);
439         int val = 0;
440         int len;
441
442         if (level != SOL_ROSE)
443                 return -ENOPROTOOPT;
444
445         if (get_user(len, optlen))
446                 return -EFAULT;
447
448         if (len < 0)
449                 return -EINVAL;
450
451         switch (optname) {
452         case ROSE_DEFER:
453                 val = rose->defer;
454                 break;
455
456         case ROSE_T1:
457                 val = rose->t1 / HZ;
458                 break;
459
460         case ROSE_T2:
461                 val = rose->t2 / HZ;
462                 break;
463
464         case ROSE_T3:
465                 val = rose->t3 / HZ;
466                 break;
467
468         case ROSE_HOLDBACK:
469                 val = rose->hb / HZ;
470                 break;
471
472         case ROSE_IDLE:
473                 val = rose->idle / (60 * HZ);
474                 break;
475
476         case ROSE_QBITINCL:
477                 val = rose->qbitincl;
478                 break;
479
480         default:
481                 return -ENOPROTOOPT;
482         }
483
484         len = min_t(unsigned int, len, sizeof(int));
485
486         if (put_user(len, optlen))
487                 return -EFAULT;
488
489         return copy_to_user(optval, &val, len) ? -EFAULT : 0;
490 }
491
492 static int rose_listen(struct socket *sock, int backlog)
493 {
494         struct sock *sk = sock->sk;
495
496         if (sk->sk_state != TCP_LISTEN) {
497                 struct rose_sock *rose = rose_sk(sk);
498
499                 rose->dest_ndigis = 0;
500                 memset(&rose->dest_addr, 0, ROSE_ADDR_LEN);
501                 memset(&rose->dest_call, 0, AX25_ADDR_LEN);
502                 memset(rose->dest_digis, 0, AX25_ADDR_LEN * ROSE_MAX_DIGIS);
503                 sk->sk_max_ack_backlog = backlog;
504                 sk->sk_state           = TCP_LISTEN;
505                 return 0;
506         }
507
508         return -EOPNOTSUPP;
509 }
510
511 static struct proto rose_proto = {
512         .name     = "ROSE",
513         .owner    = THIS_MODULE,
514         .obj_size = sizeof(struct rose_sock),
515 };
516
517 static int rose_create(struct net *net, struct socket *sock, int protocol,
518                        int kern)
519 {
520         struct sock *sk;
521         struct rose_sock *rose;
522
523         if (!net_eq(net, &init_net))
524                 return -EAFNOSUPPORT;
525
526         if (sock->type != SOCK_SEQPACKET || protocol != 0)
527                 return -ESOCKTNOSUPPORT;
528
529         sk = sk_alloc(net, PF_ROSE, GFP_ATOMIC, &rose_proto);
530         if (sk == NULL)
531                 return -ENOMEM;
532
533         rose = rose_sk(sk);
534
535         sock_init_data(sock, sk);
536
537         skb_queue_head_init(&rose->ack_queue);
538 #ifdef M_BIT
539         skb_queue_head_init(&rose->frag_queue);
540         rose->fraglen    = 0;
541 #endif
542
543         sock->ops    = &rose_proto_ops;
544         sk->sk_protocol = protocol;
545
546         init_timer(&rose->timer);
547         init_timer(&rose->idletimer);
548
549         rose->t1   = msecs_to_jiffies(sysctl_rose_call_request_timeout);
550         rose->t2   = msecs_to_jiffies(sysctl_rose_reset_request_timeout);
551         rose->t3   = msecs_to_jiffies(sysctl_rose_clear_request_timeout);
552         rose->hb   = msecs_to_jiffies(sysctl_rose_ack_hold_back_timeout);
553         rose->idle = msecs_to_jiffies(sysctl_rose_no_activity_timeout);
554
555         rose->state = ROSE_STATE_0;
556
557         return 0;
558 }
559
560 static struct sock *rose_make_new(struct sock *osk)
561 {
562         struct sock *sk;
563         struct rose_sock *rose, *orose;
564
565         if (osk->sk_type != SOCK_SEQPACKET)
566                 return NULL;
567
568         sk = sk_alloc(sock_net(osk), PF_ROSE, GFP_ATOMIC, &rose_proto);
569         if (sk == NULL)
570                 return NULL;
571
572         rose = rose_sk(sk);
573
574         sock_init_data(NULL, sk);
575
576         skb_queue_head_init(&rose->ack_queue);
577 #ifdef M_BIT
578         skb_queue_head_init(&rose->frag_queue);
579         rose->fraglen  = 0;
580 #endif
581
582         sk->sk_type     = osk->sk_type;
583         sk->sk_priority = osk->sk_priority;
584         sk->sk_protocol = osk->sk_protocol;
585         sk->sk_rcvbuf   = osk->sk_rcvbuf;
586         sk->sk_sndbuf   = osk->sk_sndbuf;
587         sk->sk_state    = TCP_ESTABLISHED;
588         sock_copy_flags(sk, osk);
589
590         init_timer(&rose->timer);
591         init_timer(&rose->idletimer);
592
593         orose           = rose_sk(osk);
594         rose->t1        = orose->t1;
595         rose->t2        = orose->t2;
596         rose->t3        = orose->t3;
597         rose->hb        = orose->hb;
598         rose->idle      = orose->idle;
599         rose->defer     = orose->defer;
600         rose->device    = orose->device;
601         rose->qbitincl  = orose->qbitincl;
602
603         return sk;
604 }
605
606 static int rose_release(struct socket *sock)
607 {
608         struct sock *sk = sock->sk;
609         struct rose_sock *rose;
610
611         if (sk == NULL) return 0;
612
613         sock_hold(sk);
614         sock_orphan(sk);
615         lock_sock(sk);
616         rose = rose_sk(sk);
617
618         switch (rose->state) {
619         case ROSE_STATE_0:
620                 release_sock(sk);
621                 rose_disconnect(sk, 0, -1, -1);
622                 lock_sock(sk);
623                 rose_destroy_socket(sk);
624                 break;
625
626         case ROSE_STATE_2:
627                 rose->neighbour->use--;
628                 release_sock(sk);
629                 rose_disconnect(sk, 0, -1, -1);
630                 lock_sock(sk);
631                 rose_destroy_socket(sk);
632                 break;
633
634         case ROSE_STATE_1:
635         case ROSE_STATE_3:
636         case ROSE_STATE_4:
637         case ROSE_STATE_5:
638                 rose_clear_queues(sk);
639                 rose_stop_idletimer(sk);
640                 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
641                 rose_start_t3timer(sk);
642                 rose->state  = ROSE_STATE_2;
643                 sk->sk_state    = TCP_CLOSE;
644                 sk->sk_shutdown |= SEND_SHUTDOWN;
645                 sk->sk_state_change(sk);
646                 sock_set_flag(sk, SOCK_DEAD);
647                 sock_set_flag(sk, SOCK_DESTROY);
648                 break;
649
650         default:
651                 break;
652         }
653
654         sock->sk = NULL;
655         release_sock(sk);
656         sock_put(sk);
657
658         return 0;
659 }
660
661 static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
662 {
663         struct sock *sk = sock->sk;
664         struct rose_sock *rose = rose_sk(sk);
665         struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
666         struct net_device *dev;
667         ax25_address *source;
668         ax25_uid_assoc *user;
669         int n;
670
671         if (!sock_flag(sk, SOCK_ZAPPED))
672                 return -EINVAL;
673
674         if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
675                 return -EINVAL;
676
677         if (addr->srose_family != AF_ROSE)
678                 return -EINVAL;
679
680         if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
681                 return -EINVAL;
682
683         if ((unsigned int) addr->srose_ndigis > ROSE_MAX_DIGIS)
684                 return -EINVAL;
685
686         if ((dev = rose_dev_get(&addr->srose_addr)) == NULL)
687                 return -EADDRNOTAVAIL;
688
689         source = &addr->srose_call;
690
691         user = ax25_findbyuid(current_euid());
692         if (user) {
693                 rose->source_call = user->call;
694                 ax25_uid_put(user);
695         } else {
696                 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE))
697                         return -EACCES;
698                 rose->source_call   = *source;
699         }
700
701         rose->source_addr   = addr->srose_addr;
702         rose->device        = dev;
703         rose->source_ndigis = addr->srose_ndigis;
704
705         if (addr_len == sizeof(struct full_sockaddr_rose)) {
706                 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
707                 for (n = 0 ; n < addr->srose_ndigis ; n++)
708                         rose->source_digis[n] = full_addr->srose_digis[n];
709         } else {
710                 if (rose->source_ndigis == 1) {
711                         rose->source_digis[0] = addr->srose_digi;
712                 }
713         }
714
715         rose_insert_socket(sk);
716
717         sock_reset_flag(sk, SOCK_ZAPPED);
718
719         return 0;
720 }
721
722 static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
723 {
724         struct sock *sk = sock->sk;
725         struct rose_sock *rose = rose_sk(sk);
726         struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
727         unsigned char cause, diagnostic;
728         struct net_device *dev;
729         ax25_uid_assoc *user;
730         int n, err = 0;
731
732         if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
733                 return -EINVAL;
734
735         if (addr->srose_family != AF_ROSE)
736                 return -EINVAL;
737
738         if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
739                 return -EINVAL;
740
741         if ((unsigned int) addr->srose_ndigis > ROSE_MAX_DIGIS)
742                 return -EINVAL;
743
744         /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
745         if ((rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
746                 return -EINVAL;
747
748         lock_sock(sk);
749
750         if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
751                 /* Connect completed during a ERESTARTSYS event */
752                 sock->state = SS_CONNECTED;
753                 goto out_release;
754         }
755
756         if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
757                 sock->state = SS_UNCONNECTED;
758                 err = -ECONNREFUSED;
759                 goto out_release;
760         }
761
762         if (sk->sk_state == TCP_ESTABLISHED) {
763                 /* No reconnect on a seqpacket socket */
764                 err = -EISCONN;
765                 goto out_release;
766         }
767
768         sk->sk_state   = TCP_CLOSE;
769         sock->state = SS_UNCONNECTED;
770
771         rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause,
772                                          &diagnostic, 0);
773         if (!rose->neighbour) {
774                 err = -ENETUNREACH;
775                 goto out_release;
776         }
777
778         rose->lci = rose_new_lci(rose->neighbour);
779         if (!rose->lci) {
780                 err = -ENETUNREACH;
781                 goto out_release;
782         }
783
784         if (sock_flag(sk, SOCK_ZAPPED)) {       /* Must bind first - autobinding in this may or may not work */
785                 sock_reset_flag(sk, SOCK_ZAPPED);
786
787                 if ((dev = rose_dev_first()) == NULL) {
788                         err = -ENETUNREACH;
789                         goto out_release;
790                 }
791
792                 user = ax25_findbyuid(current_euid());
793                 if (!user) {
794                         err = -EINVAL;
795                         goto out_release;
796                 }
797
798                 memcpy(&rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
799                 rose->source_call = user->call;
800                 rose->device      = dev;
801                 ax25_uid_put(user);
802
803                 rose_insert_socket(sk);         /* Finish the bind */
804         }
805         rose->dest_addr   = addr->srose_addr;
806         rose->dest_call   = addr->srose_call;
807         rose->rand        = ((long)rose & 0xFFFF) + rose->lci;
808         rose->dest_ndigis = addr->srose_ndigis;
809
810         if (addr_len == sizeof(struct full_sockaddr_rose)) {
811                 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
812                 for (n = 0 ; n < addr->srose_ndigis ; n++)
813                         rose->dest_digis[n] = full_addr->srose_digis[n];
814         } else {
815                 if (rose->dest_ndigis == 1) {
816                         rose->dest_digis[0] = addr->srose_digi;
817                 }
818         }
819
820         /* Move to connecting socket, start sending Connect Requests */
821         sock->state   = SS_CONNECTING;
822         sk->sk_state     = TCP_SYN_SENT;
823
824         rose->state = ROSE_STATE_1;
825
826         rose->neighbour->use++;
827
828         rose_write_internal(sk, ROSE_CALL_REQUEST);
829         rose_start_heartbeat(sk);
830         rose_start_t1timer(sk);
831
832         /* Now the loop */
833         if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
834                 err = -EINPROGRESS;
835                 goto out_release;
836         }
837
838         /*
839          * A Connect Ack with Choke or timeout or failed routing will go to
840          * closed.
841          */
842         if (sk->sk_state == TCP_SYN_SENT) {
843                 DEFINE_WAIT(wait);
844
845                 for (;;) {
846                         prepare_to_wait(sk_sleep(sk), &wait,
847                                         TASK_INTERRUPTIBLE);
848                         if (sk->sk_state != TCP_SYN_SENT)
849                                 break;
850                         if (!signal_pending(current)) {
851                                 release_sock(sk);
852                                 schedule();
853                                 lock_sock(sk);
854                                 continue;
855                         }
856                         err = -ERESTARTSYS;
857                         break;
858                 }
859                 finish_wait(sk_sleep(sk), &wait);
860
861                 if (err)
862                         goto out_release;
863         }
864
865         if (sk->sk_state != TCP_ESTABLISHED) {
866                 sock->state = SS_UNCONNECTED;
867                 err = sock_error(sk);   /* Always set at this point */
868                 goto out_release;
869         }
870
871         sock->state = SS_CONNECTED;
872
873 out_release:
874         release_sock(sk);
875
876         return err;
877 }
878
879 static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
880 {
881         struct sk_buff *skb;
882         struct sock *newsk;
883         DEFINE_WAIT(wait);
884         struct sock *sk;
885         int err = 0;
886
887         if ((sk = sock->sk) == NULL)
888                 return -EINVAL;
889
890         lock_sock(sk);
891         if (sk->sk_type != SOCK_SEQPACKET) {
892                 err = -EOPNOTSUPP;
893                 goto out_release;
894         }
895
896         if (sk->sk_state != TCP_LISTEN) {
897                 err = -EINVAL;
898                 goto out_release;
899         }
900
901         /*
902          *      The write queue this time is holding sockets ready to use
903          *      hooked into the SABM we saved
904          */
905         for (;;) {
906                 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
907
908                 skb = skb_dequeue(&sk->sk_receive_queue);
909                 if (skb)
910                         break;
911
912                 if (flags & O_NONBLOCK) {
913                         err = -EWOULDBLOCK;
914                         break;
915                 }
916                 if (!signal_pending(current)) {
917                         release_sock(sk);
918                         schedule();
919                         lock_sock(sk);
920                         continue;
921                 }
922                 err = -ERESTARTSYS;
923                 break;
924         }
925         finish_wait(sk_sleep(sk), &wait);
926         if (err)
927                 goto out_release;
928
929         newsk = skb->sk;
930         sock_graft(newsk, newsock);
931
932         /* Now attach up the new socket */
933         skb->sk = NULL;
934         kfree_skb(skb);
935         sk->sk_ack_backlog--;
936
937 out_release:
938         release_sock(sk);
939
940         return err;
941 }
942
943 static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
944         int *uaddr_len, int peer)
945 {
946         struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
947         struct sock *sk = sock->sk;
948         struct rose_sock *rose = rose_sk(sk);
949         int n;
950
951         memset(srose, 0, sizeof(*srose));
952         if (peer != 0) {
953                 if (sk->sk_state != TCP_ESTABLISHED)
954                         return -ENOTCONN;
955                 srose->srose_family = AF_ROSE;
956                 srose->srose_addr   = rose->dest_addr;
957                 srose->srose_call   = rose->dest_call;
958                 srose->srose_ndigis = rose->dest_ndigis;
959                 for (n = 0; n < rose->dest_ndigis; n++)
960                         srose->srose_digis[n] = rose->dest_digis[n];
961         } else {
962                 srose->srose_family = AF_ROSE;
963                 srose->srose_addr   = rose->source_addr;
964                 srose->srose_call   = rose->source_call;
965                 srose->srose_ndigis = rose->source_ndigis;
966                 for (n = 0; n < rose->source_ndigis; n++)
967                         srose->srose_digis[n] = rose->source_digis[n];
968         }
969
970         *uaddr_len = sizeof(struct full_sockaddr_rose);
971         return 0;
972 }
973
974 int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct rose_neigh *neigh, unsigned int lci)
975 {
976         struct sock *sk;
977         struct sock *make;
978         struct rose_sock *make_rose;
979         struct rose_facilities_struct facilities;
980         int n;
981
982         skb->sk = NULL;         /* Initially we don't know who it's for */
983
984         /*
985          *      skb->data points to the rose frame start
986          */
987         memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
988
989         if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
990                                    skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
991                                    &facilities)) {
992                 rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
993                 return 0;
994         }
995
996         sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
997
998         /*
999          * We can't accept the Call Request.
1000          */
1001         if (sk == NULL || sk_acceptq_is_full(sk) ||
1002             (make = rose_make_new(sk)) == NULL) {
1003                 rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1004                 return 0;
1005         }
1006
1007         skb->sk     = make;
1008         make->sk_state = TCP_ESTABLISHED;
1009         make_rose = rose_sk(make);
1010
1011         make_rose->lci           = lci;
1012         make_rose->dest_addr     = facilities.dest_addr;
1013         make_rose->dest_call     = facilities.dest_call;
1014         make_rose->dest_ndigis   = facilities.dest_ndigis;
1015         for (n = 0 ; n < facilities.dest_ndigis ; n++)
1016                 make_rose->dest_digis[n] = facilities.dest_digis[n];
1017         make_rose->source_addr   = facilities.source_addr;
1018         make_rose->source_call   = facilities.source_call;
1019         make_rose->source_ndigis = facilities.source_ndigis;
1020         for (n = 0 ; n < facilities.source_ndigis ; n++)
1021                 make_rose->source_digis[n]= facilities.source_digis[n];
1022         make_rose->neighbour     = neigh;
1023         make_rose->device        = dev;
1024         make_rose->facilities    = facilities;
1025
1026         make_rose->neighbour->use++;
1027
1028         if (rose_sk(sk)->defer) {
1029                 make_rose->state = ROSE_STATE_5;
1030         } else {
1031                 rose_write_internal(make, ROSE_CALL_ACCEPTED);
1032                 make_rose->state = ROSE_STATE_3;
1033                 rose_start_idletimer(make);
1034         }
1035
1036         make_rose->condition = 0x00;
1037         make_rose->vs        = 0;
1038         make_rose->va        = 0;
1039         make_rose->vr        = 0;
1040         make_rose->vl        = 0;
1041         sk->sk_ack_backlog++;
1042
1043         rose_insert_socket(make);
1044
1045         skb_queue_head(&sk->sk_receive_queue, skb);
1046
1047         rose_start_heartbeat(make);
1048
1049         if (!sock_flag(sk, SOCK_DEAD))
1050                 sk->sk_data_ready(sk, skb->len);
1051
1052         return 1;
1053 }
1054
1055 static int rose_sendmsg(struct kiocb *iocb, struct socket *sock,
1056                         struct msghdr *msg, size_t len)
1057 {
1058         struct sock *sk = sock->sk;
1059         struct rose_sock *rose = rose_sk(sk);
1060         struct sockaddr_rose *usrose = (struct sockaddr_rose *)msg->msg_name;
1061         int err;
1062         struct full_sockaddr_rose srose;
1063         struct sk_buff *skb;
1064         unsigned char *asmptr;
1065         int n, size, qbit = 0;
1066
1067         if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1068                 return -EINVAL;
1069
1070         if (sock_flag(sk, SOCK_ZAPPED))
1071                 return -EADDRNOTAVAIL;
1072
1073         if (sk->sk_shutdown & SEND_SHUTDOWN) {
1074                 send_sig(SIGPIPE, current, 0);
1075                 return -EPIPE;
1076         }
1077
1078         if (rose->neighbour == NULL || rose->device == NULL)
1079                 return -ENETUNREACH;
1080
1081         if (usrose != NULL) {
1082                 if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
1083                         return -EINVAL;
1084                 memset(&srose, 0, sizeof(struct full_sockaddr_rose));
1085                 memcpy(&srose, usrose, msg->msg_namelen);
1086                 if (rosecmp(&rose->dest_addr, &srose.srose_addr) != 0 ||
1087                     ax25cmp(&rose->dest_call, &srose.srose_call) != 0)
1088                         return -EISCONN;
1089                 if (srose.srose_ndigis != rose->dest_ndigis)
1090                         return -EISCONN;
1091                 if (srose.srose_ndigis == rose->dest_ndigis) {
1092                         for (n = 0 ; n < srose.srose_ndigis ; n++)
1093                                 if (ax25cmp(&rose->dest_digis[n],
1094                                             &srose.srose_digis[n]))
1095                                         return -EISCONN;
1096                 }
1097                 if (srose.srose_family != AF_ROSE)
1098                         return -EINVAL;
1099         } else {
1100                 if (sk->sk_state != TCP_ESTABLISHED)
1101                         return -ENOTCONN;
1102
1103                 srose.srose_family = AF_ROSE;
1104                 srose.srose_addr   = rose->dest_addr;
1105                 srose.srose_call   = rose->dest_call;
1106                 srose.srose_ndigis = rose->dest_ndigis;
1107                 for (n = 0 ; n < rose->dest_ndigis ; n++)
1108                         srose.srose_digis[n] = rose->dest_digis[n];
1109         }
1110
1111         /* Build a packet */
1112         /* Sanity check the packet size */
1113         if (len > 65535)
1114                 return -EMSGSIZE;
1115
1116         size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
1117
1118         if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1119                 return err;
1120
1121         skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
1122
1123         /*
1124          *      Put the data on the end
1125          */
1126
1127         skb_reset_transport_header(skb);
1128         skb_put(skb, len);
1129
1130         err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1131         if (err) {
1132                 kfree_skb(skb);
1133                 return err;
1134         }
1135
1136         /*
1137          *      If the Q BIT Include socket option is in force, the first
1138          *      byte of the user data is the logical value of the Q Bit.
1139          */
1140         if (rose->qbitincl) {
1141                 qbit = skb->data[0];
1142                 skb_pull(skb, 1);
1143         }
1144
1145         /*
1146          *      Push down the ROSE header
1147          */
1148         asmptr = skb_push(skb, ROSE_MIN_LEN);
1149
1150         /* Build a ROSE Network header */
1151         asmptr[0] = ((rose->lci >> 8) & 0x0F) | ROSE_GFI;
1152         asmptr[1] = (rose->lci >> 0) & 0xFF;
1153         asmptr[2] = ROSE_DATA;
1154
1155         if (qbit)
1156                 asmptr[0] |= ROSE_Q_BIT;
1157
1158         if (sk->sk_state != TCP_ESTABLISHED) {
1159                 kfree_skb(skb);
1160                 return -ENOTCONN;
1161         }
1162
1163 #ifdef M_BIT
1164 #define ROSE_PACLEN (256-ROSE_MIN_LEN)
1165         if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
1166                 unsigned char header[ROSE_MIN_LEN];
1167                 struct sk_buff *skbn;
1168                 int frontlen;
1169                 int lg;
1170
1171                 /* Save a copy of the Header */
1172                 skb_copy_from_linear_data(skb, header, ROSE_MIN_LEN);
1173                 skb_pull(skb, ROSE_MIN_LEN);
1174
1175                 frontlen = skb_headroom(skb);
1176
1177                 while (skb->len > 0) {
1178                         if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, &err)) == NULL) {
1179                                 kfree_skb(skb);
1180                                 return err;
1181                         }
1182
1183                         skbn->sk   = sk;
1184                         skbn->free = 1;
1185                         skbn->arp  = 1;
1186
1187                         skb_reserve(skbn, frontlen);
1188
1189                         lg = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN;
1190
1191                         /* Copy the user data */
1192                         skb_copy_from_linear_data(skb, skb_put(skbn, lg), lg);
1193                         skb_pull(skb, lg);
1194
1195                         /* Duplicate the Header */
1196                         skb_push(skbn, ROSE_MIN_LEN);
1197                         skb_copy_to_linear_data(skbn, header, ROSE_MIN_LEN);
1198
1199                         if (skb->len > 0)
1200                                 skbn->data[2] |= M_BIT;
1201
1202                         skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
1203                 }
1204
1205                 skb->free = 1;
1206                 kfree_skb(skb);
1207         } else {
1208                 skb_queue_tail(&sk->sk_write_queue, skb);               /* Throw it on the queue */
1209         }
1210 #else
1211         skb_queue_tail(&sk->sk_write_queue, skb);       /* Shove it onto the queue */
1212 #endif
1213
1214         rose_kick(sk);
1215
1216         return len;
1217 }
1218
1219
1220 static int rose_recvmsg(struct kiocb *iocb, struct socket *sock,
1221                         struct msghdr *msg, size_t size, int flags)
1222 {
1223         struct sock *sk = sock->sk;
1224         struct rose_sock *rose = rose_sk(sk);
1225         size_t copied;
1226         unsigned char *asmptr;
1227         struct sk_buff *skb;
1228         int n, er, qbit;
1229
1230         /*
1231          * This works for seqpacket too. The receiver has ordered the queue for
1232          * us! We do one quick check first though
1233          */
1234         if (sk->sk_state != TCP_ESTABLISHED)
1235                 return -ENOTCONN;
1236
1237         /* Now we can treat all alike */
1238         if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1239                 return er;
1240
1241         qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
1242
1243         skb_pull(skb, ROSE_MIN_LEN);
1244
1245         if (rose->qbitincl) {
1246                 asmptr  = skb_push(skb, 1);
1247                 *asmptr = qbit;
1248         }
1249
1250         skb_reset_transport_header(skb);
1251         copied     = skb->len;
1252
1253         if (copied > size) {
1254                 copied = size;
1255                 msg->msg_flags |= MSG_TRUNC;
1256         }
1257
1258         skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1259
1260         if (msg->msg_name) {
1261                 struct sockaddr_rose *srose;
1262                 struct full_sockaddr_rose *full_srose = msg->msg_name;
1263
1264                 memset(msg->msg_name, 0, sizeof(struct full_sockaddr_rose));
1265                 srose = msg->msg_name;
1266                 srose->srose_family = AF_ROSE;
1267                 srose->srose_addr   = rose->dest_addr;
1268                 srose->srose_call   = rose->dest_call;
1269                 srose->srose_ndigis = rose->dest_ndigis;
1270                 for (n = 0 ; n < rose->dest_ndigis ; n++)
1271                         full_srose->srose_digis[n] = rose->dest_digis[n];
1272                 msg->msg_namelen = sizeof(struct full_sockaddr_rose);
1273         }
1274
1275         skb_free_datagram(sk, skb);
1276
1277         return copied;
1278 }
1279
1280
1281 static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1282 {
1283         struct sock *sk = sock->sk;
1284         struct rose_sock *rose = rose_sk(sk);
1285         void __user *argp = (void __user *)arg;
1286
1287         switch (cmd) {
1288         case TIOCOUTQ: {
1289                 long amount;
1290
1291                 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1292                 if (amount < 0)
1293                         amount = 0;
1294                 return put_user(amount, (unsigned int __user *) argp);
1295         }
1296
1297         case TIOCINQ: {
1298                 struct sk_buff *skb;
1299                 long amount = 0L;
1300                 /* These two are safe on a single CPU system as only user tasks fiddle here */
1301                 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1302                         amount = skb->len;
1303                 return put_user(amount, (unsigned int __user *) argp);
1304         }
1305
1306         case SIOCGSTAMP:
1307                 return sock_get_timestamp(sk, (struct timeval __user *) argp);
1308
1309         case SIOCGSTAMPNS:
1310                 return sock_get_timestampns(sk, (struct timespec __user *) argp);
1311
1312         case SIOCGIFADDR:
1313         case SIOCSIFADDR:
1314         case SIOCGIFDSTADDR:
1315         case SIOCSIFDSTADDR:
1316         case SIOCGIFBRDADDR:
1317         case SIOCSIFBRDADDR:
1318         case SIOCGIFNETMASK:
1319         case SIOCSIFNETMASK:
1320         case SIOCGIFMETRIC:
1321         case SIOCSIFMETRIC:
1322                 return -EINVAL;
1323
1324         case SIOCADDRT:
1325         case SIOCDELRT:
1326         case SIOCRSCLRRT:
1327                 if (!capable(CAP_NET_ADMIN))
1328                         return -EPERM;
1329                 return rose_rt_ioctl(cmd, argp);
1330
1331         case SIOCRSGCAUSE: {
1332                 struct rose_cause_struct rose_cause;
1333                 rose_cause.cause      = rose->cause;
1334                 rose_cause.diagnostic = rose->diagnostic;
1335                 return copy_to_user(argp, &rose_cause, sizeof(struct rose_cause_struct)) ? -EFAULT : 0;
1336         }
1337
1338         case SIOCRSSCAUSE: {
1339                 struct rose_cause_struct rose_cause;
1340                 if (copy_from_user(&rose_cause, argp, sizeof(struct rose_cause_struct)))
1341                         return -EFAULT;
1342                 rose->cause      = rose_cause.cause;
1343                 rose->diagnostic = rose_cause.diagnostic;
1344                 return 0;
1345         }
1346
1347         case SIOCRSSL2CALL:
1348                 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1349                 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1350                         ax25_listen_release(&rose_callsign, NULL);
1351                 if (copy_from_user(&rose_callsign, argp, sizeof(ax25_address)))
1352                         return -EFAULT;
1353                 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1354                         return ax25_listen_register(&rose_callsign, NULL);
1355
1356                 return 0;
1357
1358         case SIOCRSGL2CALL:
1359                 return copy_to_user(argp, &rose_callsign, sizeof(ax25_address)) ? -EFAULT : 0;
1360
1361         case SIOCRSACCEPT:
1362                 if (rose->state == ROSE_STATE_5) {
1363                         rose_write_internal(sk, ROSE_CALL_ACCEPTED);
1364                         rose_start_idletimer(sk);
1365                         rose->condition = 0x00;
1366                         rose->vs        = 0;
1367                         rose->va        = 0;
1368                         rose->vr        = 0;
1369                         rose->vl        = 0;
1370                         rose->state     = ROSE_STATE_3;
1371                 }
1372                 return 0;
1373
1374         default:
1375                 return -ENOIOCTLCMD;
1376         }
1377
1378         return 0;
1379 }
1380
1381 #ifdef CONFIG_PROC_FS
1382 static void *rose_info_start(struct seq_file *seq, loff_t *pos)
1383         __acquires(rose_list_lock)
1384 {
1385         spin_lock_bh(&rose_list_lock);
1386         return seq_hlist_start_head(&rose_list, *pos);
1387 }
1388
1389 static void *rose_info_next(struct seq_file *seq, void *v, loff_t *pos)
1390 {
1391         return seq_hlist_next(v, &rose_list, pos);
1392 }
1393
1394 static void rose_info_stop(struct seq_file *seq, void *v)
1395         __releases(rose_list_lock)
1396 {
1397         spin_unlock_bh(&rose_list_lock);
1398 }
1399
1400 static int rose_info_show(struct seq_file *seq, void *v)
1401 {
1402         char buf[11], rsbuf[11];
1403
1404         if (v == SEQ_START_TOKEN)
1405                 seq_puts(seq,
1406                          "dest_addr  dest_call src_addr   src_call  dev   lci neigh st vs vr va   t  t1  t2  t3  hb    idle Snd-Q Rcv-Q inode\n");
1407
1408         else {
1409                 struct sock *s = sk_entry(v);
1410                 struct rose_sock *rose = rose_sk(s);
1411                 const char *devname, *callsign;
1412                 const struct net_device *dev = rose->device;
1413
1414                 if (!dev)
1415                         devname = "???";
1416                 else
1417                         devname = dev->name;
1418
1419                 seq_printf(seq, "%-10s %-9s ",
1420                            rose2asc(rsbuf, &rose->dest_addr),
1421                            ax2asc(buf, &rose->dest_call));
1422
1423                 if (ax25cmp(&rose->source_call, &null_ax25_address) == 0)
1424                         callsign = "??????-?";
1425                 else
1426                         callsign = ax2asc(buf, &rose->source_call);
1427
1428                 seq_printf(seq,
1429                            "%-10s %-9s %-5s %3.3X %05d  %d  %d  %d  %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
1430                         rose2asc(rsbuf, &rose->source_addr),
1431                         callsign,
1432                         devname,
1433                         rose->lci & 0x0FFF,
1434                         (rose->neighbour) ? rose->neighbour->number : 0,
1435                         rose->state,
1436                         rose->vs,
1437                         rose->vr,
1438                         rose->va,
1439                         ax25_display_timer(&rose->timer) / HZ,
1440                         rose->t1 / HZ,
1441                         rose->t2 / HZ,
1442                         rose->t3 / HZ,
1443                         rose->hb / HZ,
1444                         ax25_display_timer(&rose->idletimer) / (60 * HZ),
1445                         rose->idle / (60 * HZ),
1446                         sk_wmem_alloc_get(s),
1447                         sk_rmem_alloc_get(s),
1448                         s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1449         }
1450
1451         return 0;
1452 }
1453
1454 static const struct seq_operations rose_info_seqops = {
1455         .start = rose_info_start,
1456         .next = rose_info_next,
1457         .stop = rose_info_stop,
1458         .show = rose_info_show,
1459 };
1460
1461 static int rose_info_open(struct inode *inode, struct file *file)
1462 {
1463         return seq_open(file, &rose_info_seqops);
1464 }
1465
1466 static const struct file_operations rose_info_fops = {
1467         .owner = THIS_MODULE,
1468         .open = rose_info_open,
1469         .read = seq_read,
1470         .llseek = seq_lseek,
1471         .release = seq_release,
1472 };
1473 #endif  /* CONFIG_PROC_FS */
1474
1475 static const struct net_proto_family rose_family_ops = {
1476         .family         =       PF_ROSE,
1477         .create         =       rose_create,
1478         .owner          =       THIS_MODULE,
1479 };
1480
1481 static const struct proto_ops rose_proto_ops = {
1482         .family         =       PF_ROSE,
1483         .owner          =       THIS_MODULE,
1484         .release        =       rose_release,
1485         .bind           =       rose_bind,
1486         .connect        =       rose_connect,
1487         .socketpair     =       sock_no_socketpair,
1488         .accept         =       rose_accept,
1489         .getname        =       rose_getname,
1490         .poll           =       datagram_poll,
1491         .ioctl          =       rose_ioctl,
1492         .listen         =       rose_listen,
1493         .shutdown       =       sock_no_shutdown,
1494         .setsockopt     =       rose_setsockopt,
1495         .getsockopt     =       rose_getsockopt,
1496         .sendmsg        =       rose_sendmsg,
1497         .recvmsg        =       rose_recvmsg,
1498         .mmap           =       sock_no_mmap,
1499         .sendpage       =       sock_no_sendpage,
1500 };
1501
1502 static struct notifier_block rose_dev_notifier = {
1503         .notifier_call  =       rose_device_event,
1504 };
1505
1506 static struct net_device **dev_rose;
1507
1508 static struct ax25_protocol rose_pid = {
1509         .pid    = AX25_P_ROSE,
1510         .func   = rose_route_frame
1511 };
1512
1513 static struct ax25_linkfail rose_linkfail_notifier = {
1514         .func   = rose_link_failed
1515 };
1516
1517 static int __init rose_proto_init(void)
1518 {
1519         int i;
1520         int rc;
1521
1522         if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
1523                 printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
1524                 rc = -EINVAL;
1525                 goto out;
1526         }
1527
1528         rc = proto_register(&rose_proto, 0);
1529         if (rc != 0)
1530                 goto out;
1531
1532         rose_callsign = null_ax25_address;
1533
1534         dev_rose = kzalloc(rose_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1535         if (dev_rose == NULL) {
1536                 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
1537                 rc = -ENOMEM;
1538                 goto out_proto_unregister;
1539         }
1540
1541         for (i = 0; i < rose_ndevs; i++) {
1542                 struct net_device *dev;
1543                 char name[IFNAMSIZ];
1544
1545                 sprintf(name, "rose%d", i);
1546                 dev = alloc_netdev(0, name, rose_setup);
1547                 if (!dev) {
1548                         printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
1549                         rc = -ENOMEM;
1550                         goto fail;
1551                 }
1552                 rc = register_netdev(dev);
1553                 if (rc) {
1554                         printk(KERN_ERR "ROSE: netdevice registration failed\n");
1555                         free_netdev(dev);
1556                         goto fail;
1557                 }
1558                 rose_set_lockdep_key(dev);
1559                 dev_rose[i] = dev;
1560         }
1561
1562         sock_register(&rose_family_ops);
1563         register_netdevice_notifier(&rose_dev_notifier);
1564
1565         ax25_register_pid(&rose_pid);
1566         ax25_linkfail_register(&rose_linkfail_notifier);
1567
1568 #ifdef CONFIG_SYSCTL
1569         rose_register_sysctl();
1570 #endif
1571         rose_loopback_init();
1572
1573         rose_add_loopback_neigh();
1574
1575         proc_net_fops_create(&init_net, "rose", S_IRUGO, &rose_info_fops);
1576         proc_net_fops_create(&init_net, "rose_neigh", S_IRUGO, &rose_neigh_fops);
1577         proc_net_fops_create(&init_net, "rose_nodes", S_IRUGO, &rose_nodes_fops);
1578         proc_net_fops_create(&init_net, "rose_routes", S_IRUGO, &rose_routes_fops);
1579 out:
1580         return rc;
1581 fail:
1582         while (--i >= 0) {
1583                 unregister_netdev(dev_rose[i]);
1584                 free_netdev(dev_rose[i]);
1585         }
1586         kfree(dev_rose);
1587 out_proto_unregister:
1588         proto_unregister(&rose_proto);
1589         goto out;
1590 }
1591 module_init(rose_proto_init);
1592
1593 module_param(rose_ndevs, int, 0);
1594 MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
1595
1596 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1597 MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1598 MODULE_LICENSE("GPL");
1599 MODULE_ALIAS_NETPROTO(PF_ROSE);
1600
1601 static void __exit rose_exit(void)
1602 {
1603         int i;
1604
1605         proc_net_remove(&init_net, "rose");
1606         proc_net_remove(&init_net, "rose_neigh");
1607         proc_net_remove(&init_net, "rose_nodes");
1608         proc_net_remove(&init_net, "rose_routes");
1609         rose_loopback_clear();
1610
1611         rose_rt_free();
1612
1613         ax25_protocol_release(AX25_P_ROSE);
1614         ax25_linkfail_release(&rose_linkfail_notifier);
1615
1616         if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1617                 ax25_listen_release(&rose_callsign, NULL);
1618
1619 #ifdef CONFIG_SYSCTL
1620         rose_unregister_sysctl();
1621 #endif
1622         unregister_netdevice_notifier(&rose_dev_notifier);
1623
1624         sock_unregister(PF_ROSE);
1625
1626         for (i = 0; i < rose_ndevs; i++) {
1627                 struct net_device *dev = dev_rose[i];
1628
1629                 if (dev) {
1630                         unregister_netdev(dev);
1631                         free_netdev(dev);
1632                 }
1633         }
1634
1635         kfree(dev_rose);
1636         proto_unregister(&rose_proto);
1637 }
1638
1639 module_exit(rose_exit);