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