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