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