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