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