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