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