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