blob: 46b8ff24020d7bb6788e985686bef12b4fbc83d0 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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 */
Randy Dunlap4fc268d2006-01-11 12:17:47 -08009
Randy Dunlap4fc268d2006-01-11 12:17:47 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010020#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020027#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080034#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070043#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <net/ip.h>
45#include <net/arp.h>
46
47static int rose_ndevs = 10;
48
49int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0;
50int sysctl_rose_call_request_timeout = ROSE_DEFAULT_T1;
51int sysctl_rose_reset_request_timeout = ROSE_DEFAULT_T2;
52int sysctl_rose_clear_request_timeout = ROSE_DEFAULT_T3;
53int sysctl_rose_no_activity_timeout = ROSE_DEFAULT_IDLE;
54int sysctl_rose_ack_hold_back_timeout = ROSE_DEFAULT_HB;
55int sysctl_rose_routing_control = ROSE_DEFAULT_ROUTING;
56int sysctl_rose_link_fail_timeout = ROSE_DEFAULT_FAIL_TIMEOUT;
57int sysctl_rose_maximum_vcs = ROSE_DEFAULT_MAXVC;
58int sysctl_rose_window_size = ROSE_DEFAULT_WINDOW_SIZE;
59
60static HLIST_HEAD(rose_list);
61static DEFINE_SPINLOCK(rose_list_lock);
62
Alexey Dobriyan5708e862009-09-14 12:23:23 +000063static const struct proto_ops rose_proto_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65ax25_address rose_callsign;
66
67/*
68 * Convert a ROSE address into text.
69 */
Ralf Baechledcf777f2009-07-26 19:11:14 -070070char *rose2asc(char *buf, const rose_address *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 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) {
Ralf Baechledcf777f2009-07-26 19:11:14 -070075 strcpy(buf, "*");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 } else {
Ralf Baechledcf777f2009-07-26 19:11:14 -070077 sprintf(buf, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 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
Ralf Baechledcf777f2009-07-26 19:11:14 -070084 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/*
88 * Compare two ROSE addresses, 0 == equal.
89 */
90int 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 */
104int rosecmpm(rose_address *addr1, rose_address *addr2, unsigned short mask)
105{
Eric Dumazet95b7d922008-01-15 03:30:35 -0800106 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
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 */
129static 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 */
140void rose_kill_by_neigh(struct rose_neigh *neigh)
141{
142 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 spin_lock_bh(&rose_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800145 sk_for_each(s, &rose_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 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 */
160static void rose_kill_by_device(struct net_device *dev)
161{
162 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 spin_lock_bh(&rose_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800165 sk_for_each(s, &rose_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct rose_sock *rose = rose_sk(s);
167
168 if (rose->device == dev) {
169 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
Ralf Baechled496f782015-06-19 00:46:53 +0200170 if (rose->neighbour)
171 rose->neighbour->use--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 rose->device = NULL;
173 }
174 }
175 spin_unlock_bh(&rose_list_lock);
176}
177
178/*
179 * Handle device status changes.
180 */
Jiri Pirko351638e2013-05-28 01:30:21 +0000181static int rose_device_event(struct notifier_block *this,
182 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Jiri Pirko351638e2013-05-28 01:30:21 +0000184 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700186 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200187 return NOTIFY_DONE;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (event != NETDEV_DOWN)
190 return NOTIFY_DONE;
191
192 switch (dev->type) {
193 case ARPHRD_ROSE:
194 rose_kill_by_device(dev);
195 break;
196 case ARPHRD_AX25:
197 rose_link_device_down(dev);
198 rose_rt_device_down(dev);
199 break;
200 }
201
202 return NOTIFY_DONE;
203}
204
205/*
206 * Add a socket to the bound sockets list.
207 */
208static void rose_insert_socket(struct sock *sk)
209{
210
211 spin_lock_bh(&rose_list_lock);
212 sk_add_node(sk, &rose_list);
213 spin_unlock_bh(&rose_list_lock);
214}
215
216/*
217 * Find a socket that wants to accept the Call Request we just
218 * received.
219 */
220static struct sock *rose_find_listener(rose_address *addr, ax25_address *call)
221{
222 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 spin_lock_bh(&rose_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800225 sk_for_each(s, &rose_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct rose_sock *rose = rose_sk(s);
227
228 if (!rosecmp(&rose->source_addr, addr) &&
229 !ax25cmp(&rose->source_call, call) &&
230 !rose->source_ndigis && s->sk_state == TCP_LISTEN)
231 goto found;
232 }
233
Sasha Levinb67bfe02013-02-27 17:06:00 -0800234 sk_for_each(s, &rose_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct rose_sock *rose = rose_sk(s);
236
237 if (!rosecmp(&rose->source_addr, addr) &&
238 !ax25cmp(&rose->source_call, &null_ax25_address) &&
239 s->sk_state == TCP_LISTEN)
240 goto found;
241 }
242 s = NULL;
243found:
244 spin_unlock_bh(&rose_list_lock);
245 return s;
246}
247
248/*
249 * Find a connected ROSE socket given my LCI and device.
250 */
251struct sock *rose_find_socket(unsigned int lci, struct rose_neigh *neigh)
252{
253 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 spin_lock_bh(&rose_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800256 sk_for_each(s, &rose_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct rose_sock *rose = rose_sk(s);
258
259 if (rose->lci == lci && rose->neighbour == neigh)
260 goto found;
261 }
262 s = NULL;
263found:
264 spin_unlock_bh(&rose_list_lock);
265 return s;
266}
267
268/*
269 * Find a unique LCI for a given device.
270 */
271unsigned int rose_new_lci(struct rose_neigh *neigh)
272{
273 int lci;
274
275 if (neigh->dce_mode) {
276 for (lci = 1; lci <= sysctl_rose_maximum_vcs; lci++)
277 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
278 return lci;
279 } else {
280 for (lci = sysctl_rose_maximum_vcs; lci > 0; lci--)
281 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
282 return lci;
283 }
284
285 return 0;
286}
287
288/*
289 * Deferred destroy.
290 */
291void rose_destroy_socket(struct sock *);
292
293/*
294 * Handler for deferred kills.
295 */
Kees Cook4966bab2017-10-16 17:28:47 -0700296static void rose_destroy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Kees Cook4966bab2017-10-16 17:28:47 -0700298 struct sock *sk = from_timer(sk, t, sk_timer);
299
300 rose_destroy_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303/*
304 * This is called from user mode and the timers. Thus it protects itself
305 * against interrupt users but doesn't worry about being called during
306 * work. Once it is removed from the queue no interrupt or bottom half
307 * will touch it and we are (fairly 8-) ) safe.
308 */
309void rose_destroy_socket(struct sock *sk)
310{
311 struct sk_buff *skb;
312
313 rose_remove_socket(sk);
314 rose_stop_heartbeat(sk);
315 rose_stop_idletimer(sk);
316 rose_stop_timer(sk);
317
318 rose_clear_queues(sk); /* Flush the queues */
319
320 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
321 if (skb->sk != sk) { /* A pending connection */
322 /* Queue the unaccepted socket for death */
323 sock_set_flag(skb->sk, SOCK_DEAD);
324 rose_start_heartbeat(skb->sk);
325 rose_sk(skb->sk)->state = ROSE_STATE_0;
326 }
327
328 kfree_skb(skb);
329 }
330
Eric Dumazetc5640392009-06-16 10:12:03 +0000331 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* Defer: outstanding buffers */
Kees Cook4966bab2017-10-16 17:28:47 -0700333 timer_setup(&sk->sk_timer, rose_destroy_timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 sk->sk_timer.expires = jiffies + 10 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 add_timer(&sk->sk_timer);
336 } else
337 sock_put(sk);
338}
339
340/*
341 * Handling for system calls applied via the various interfaces to a
342 * ROSE socket object.
343 */
344
345static int rose_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700346 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct sock *sk = sock->sk;
349 struct rose_sock *rose = rose_sk(sk);
350 int opt;
351
352 if (level != SOL_ROSE)
353 return -ENOPROTOOPT;
354
355 if (optlen < sizeof(int))
356 return -EINVAL;
357
358 if (get_user(opt, (int __user *)optval))
359 return -EFAULT;
360
361 switch (optname) {
362 case ROSE_DEFER:
363 rose->defer = opt ? 1 : 0;
364 return 0;
365
366 case ROSE_T1:
367 if (opt < 1)
368 return -EINVAL;
369 rose->t1 = opt * HZ;
370 return 0;
371
372 case ROSE_T2:
373 if (opt < 1)
374 return -EINVAL;
375 rose->t2 = opt * HZ;
376 return 0;
377
378 case ROSE_T3:
379 if (opt < 1)
380 return -EINVAL;
381 rose->t3 = opt * HZ;
382 return 0;
383
384 case ROSE_HOLDBACK:
385 if (opt < 1)
386 return -EINVAL;
387 rose->hb = opt * HZ;
388 return 0;
389
390 case ROSE_IDLE:
391 if (opt < 0)
392 return -EINVAL;
393 rose->idle = opt * 60 * HZ;
394 return 0;
395
396 case ROSE_QBITINCL:
397 rose->qbitincl = opt ? 1 : 0;
398 return 0;
399
400 default:
401 return -ENOPROTOOPT;
402 }
403}
404
405static int rose_getsockopt(struct socket *sock, int level, int optname,
406 char __user *optval, int __user *optlen)
407{
408 struct sock *sk = sock->sk;
409 struct rose_sock *rose = rose_sk(sk);
410 int val = 0;
411 int len;
412
413 if (level != SOL_ROSE)
414 return -ENOPROTOOPT;
415
416 if (get_user(len, optlen))
417 return -EFAULT;
418
419 if (len < 0)
420 return -EINVAL;
421
422 switch (optname) {
423 case ROSE_DEFER:
424 val = rose->defer;
425 break;
426
427 case ROSE_T1:
428 val = rose->t1 / HZ;
429 break;
430
431 case ROSE_T2:
432 val = rose->t2 / HZ;
433 break;
434
435 case ROSE_T3:
436 val = rose->t3 / HZ;
437 break;
438
439 case ROSE_HOLDBACK:
440 val = rose->hb / HZ;
441 break;
442
443 case ROSE_IDLE:
444 val = rose->idle / (60 * HZ);
445 break;
446
447 case ROSE_QBITINCL:
448 val = rose->qbitincl;
449 break;
450
451 default:
452 return -ENOPROTOOPT;
453 }
454
455 len = min_t(unsigned int, len, sizeof(int));
456
457 if (put_user(len, optlen))
458 return -EFAULT;
459
460 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
461}
462
463static int rose_listen(struct socket *sock, int backlog)
464{
465 struct sock *sk = sock->sk;
466
467 if (sk->sk_state != TCP_LISTEN) {
468 struct rose_sock *rose = rose_sk(sk);
469
470 rose->dest_ndigis = 0;
471 memset(&rose->dest_addr, 0, ROSE_ADDR_LEN);
472 memset(&rose->dest_call, 0, AX25_ADDR_LEN);
473 memset(rose->dest_digis, 0, AX25_ADDR_LEN * ROSE_MAX_DIGIS);
474 sk->sk_max_ack_backlog = backlog;
475 sk->sk_state = TCP_LISTEN;
476 return 0;
477 }
478
479 return -EOPNOTSUPP;
480}
481
482static struct proto rose_proto = {
483 .name = "ROSE",
484 .owner = THIS_MODULE,
485 .obj_size = sizeof(struct rose_sock),
486};
487
Eric Paris3f378b62009-11-05 22:18:14 -0800488static int rose_create(struct net *net, struct socket *sock, int protocol,
489 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct sock *sk;
492 struct rose_sock *rose;
493
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800494 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700495 return -EAFNOSUPPORT;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (sock->type != SOCK_SEQPACKET || protocol != 0)
498 return -ESOCKTNOSUPPORT;
499
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500500 sk = sk_alloc(net, PF_ROSE, GFP_ATOMIC, &rose_proto, kern);
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700501 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -ENOMEM;
503
504 rose = rose_sk(sk);
505
506 sock_init_data(sock, sk);
507
508 skb_queue_head_init(&rose->ack_queue);
509#ifdef M_BIT
510 skb_queue_head_init(&rose->frag_queue);
511 rose->fraglen = 0;
512#endif
513
514 sock->ops = &rose_proto_ops;
515 sk->sk_protocol = protocol;
516
Kees Cook4966bab2017-10-16 17:28:47 -0700517 timer_setup(&rose->timer, NULL, 0);
518 timer_setup(&rose->idletimer, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Ralf Baechle82e84242006-05-03 23:28:20 -0700520 rose->t1 = msecs_to_jiffies(sysctl_rose_call_request_timeout);
521 rose->t2 = msecs_to_jiffies(sysctl_rose_reset_request_timeout);
522 rose->t3 = msecs_to_jiffies(sysctl_rose_clear_request_timeout);
523 rose->hb = msecs_to_jiffies(sysctl_rose_ack_hold_back_timeout);
524 rose->idle = msecs_to_jiffies(sysctl_rose_no_activity_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 rose->state = ROSE_STATE_0;
527
528 return 0;
529}
530
531static struct sock *rose_make_new(struct sock *osk)
532{
533 struct sock *sk;
534 struct rose_sock *rose, *orose;
535
536 if (osk->sk_type != SOCK_SEQPACKET)
537 return NULL;
538
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500539 sk = sk_alloc(sock_net(osk), PF_ROSE, GFP_ATOMIC, &rose_proto, 0);
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700540 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return NULL;
542
543 rose = rose_sk(sk);
544
545 sock_init_data(NULL, sk);
546
547 skb_queue_head_init(&rose->ack_queue);
548#ifdef M_BIT
549 skb_queue_head_init(&rose->frag_queue);
550 rose->fraglen = 0;
551#endif
552
553 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 sk->sk_priority = osk->sk_priority;
555 sk->sk_protocol = osk->sk_protocol;
556 sk->sk_rcvbuf = osk->sk_rcvbuf;
557 sk->sk_sndbuf = osk->sk_sndbuf;
558 sk->sk_state = TCP_ESTABLISHED;
Ralf Baechle53b924b2005-08-23 10:11:30 -0700559 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Kees Cook4966bab2017-10-16 17:28:47 -0700561 timer_setup(&rose->timer, NULL, 0);
562 timer_setup(&rose->idletimer, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 orose = rose_sk(osk);
565 rose->t1 = orose->t1;
566 rose->t2 = orose->t2;
567 rose->t3 = orose->t3;
568 rose->hb = orose->hb;
569 rose->idle = orose->idle;
570 rose->defer = orose->defer;
571 rose->device = orose->device;
572 rose->qbitincl = orose->qbitincl;
573
574 return sk;
575}
576
577static int rose_release(struct socket *sock)
578{
579 struct sock *sk = sock->sk;
580 struct rose_sock *rose;
581
582 if (sk == NULL) return 0;
583
Jarek Poplawski49652912008-04-01 23:56:17 -0700584 sock_hold(sk);
585 sock_orphan(sk);
586 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 rose = rose_sk(sk);
588
589 switch (rose->state) {
590 case ROSE_STATE_0:
Jarek Poplawski49652912008-04-01 23:56:17 -0700591 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 rose_disconnect(sk, 0, -1, -1);
Jarek Poplawski49652912008-04-01 23:56:17 -0700593 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 rose_destroy_socket(sk);
595 break;
596
597 case ROSE_STATE_2:
598 rose->neighbour->use--;
Jarek Poplawski49652912008-04-01 23:56:17 -0700599 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 rose_disconnect(sk, 0, -1, -1);
Jarek Poplawski49652912008-04-01 23:56:17 -0700601 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 rose_destroy_socket(sk);
603 break;
604
605 case ROSE_STATE_1:
606 case ROSE_STATE_3:
607 case ROSE_STATE_4:
608 case ROSE_STATE_5:
609 rose_clear_queues(sk);
610 rose_stop_idletimer(sk);
611 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
612 rose_start_t3timer(sk);
613 rose->state = ROSE_STATE_2;
614 sk->sk_state = TCP_CLOSE;
615 sk->sk_shutdown |= SEND_SHUTDOWN;
616 sk->sk_state_change(sk);
617 sock_set_flag(sk, SOCK_DEAD);
618 sock_set_flag(sk, SOCK_DESTROY);
619 break;
620
621 default:
622 break;
623 }
624
625 sock->sk = NULL;
Jarek Poplawski49652912008-04-01 23:56:17 -0700626 release_sock(sk);
627 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 return 0;
630}
631
632static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
633{
634 struct sock *sk = sock->sk;
635 struct rose_sock *rose = rose_sk(sk);
636 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
637 struct net_device *dev;
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700638 ax25_address *source;
639 ax25_uid_assoc *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 int n;
641
642 if (!sock_flag(sk, SOCK_ZAPPED))
643 return -EINVAL;
644
645 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
646 return -EINVAL;
647
648 if (addr->srose_family != AF_ROSE)
649 return -EINVAL;
650
651 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
652 return -EINVAL;
653
David S. Miller9828e6e2010-09-20 15:40:35 -0700654 if ((unsigned int) addr->srose_ndigis > ROSE_MAX_DIGIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -EINVAL;
656
Ralf Baechle8849b722011-04-14 00:20:07 -0700657 if ((dev = rose_dev_get(&addr->srose_addr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 source = &addr->srose_call;
661
David Howellsc2a2b8d2008-11-14 10:39:08 +1100662 user = ax25_findbyuid(current_euid());
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700663 if (user) {
664 rose->source_call = user->call;
665 ax25_uid_put(user);
666 } else {
YueHaibing3b9c9f32019-02-19 16:06:22 +0800667 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
668 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -EACCES;
YueHaibing3b9c9f32019-02-19 16:06:22 +0800670 }
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700671 rose->source_call = *source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
674 rose->source_addr = addr->srose_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 rose->device = dev;
676 rose->source_ndigis = addr->srose_ndigis;
677
678 if (addr_len == sizeof(struct full_sockaddr_rose)) {
679 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
680 for (n = 0 ; n < addr->srose_ndigis ; n++)
681 rose->source_digis[n] = full_addr->srose_digis[n];
682 } else {
683 if (rose->source_ndigis == 1) {
684 rose->source_digis[0] = addr->srose_digi;
685 }
686 }
687
688 rose_insert_socket(sk);
689
690 sock_reset_flag(sk, SOCK_ZAPPED);
Ralf Baechle8849b722011-04-14 00:20:07 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693}
694
695static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
696{
697 struct sock *sk = sock->sk;
698 struct rose_sock *rose = rose_sk(sk);
699 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
700 unsigned char cause, diagnostic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct net_device *dev;
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700702 ax25_uid_assoc *user;
Ralf Baechle2536b942007-03-12 15:53:33 -0700703 int n, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
706 return -EINVAL;
707
708 if (addr->srose_family != AF_ROSE)
709 return -EINVAL;
710
711 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
712 return -EINVAL;
713
David S. Miller9828e6e2010-09-20 15:40:35 -0700714 if ((unsigned int) addr->srose_ndigis > ROSE_MAX_DIGIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EINVAL;
716
717 /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
718 if ((rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
719 return -EINVAL;
720
Ralf Baechle2536b942007-03-12 15:53:33 -0700721 lock_sock(sk);
722
723 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
724 /* Connect completed during a ERESTARTSYS event */
725 sock->state = SS_CONNECTED;
726 goto out_release;
727 }
728
729 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
730 sock->state = SS_UNCONNECTED;
731 err = -ECONNREFUSED;
732 goto out_release;
733 }
734
735 if (sk->sk_state == TCP_ESTABLISHED) {
736 /* No reconnect on a seqpacket socket */
737 err = -EISCONN;
738 goto out_release;
739 }
740
741 sk->sk_state = TCP_CLOSE;
742 sock->state = SS_UNCONNECTED;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause,
Bernard Pidouxfe2c8022008-06-17 17:08:32 -0700745 &diagnostic, 0);
Bernard Pidoux43837b12008-04-19 18:41:51 -0700746 if (!rose->neighbour) {
747 err = -ENETUNREACH;
748 goto out_release;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 rose->lci = rose_new_lci(rose->neighbour);
Ralf Baechle2536b942007-03-12 15:53:33 -0700752 if (!rose->lci) {
753 err = -ENETUNREACH;
754 goto out_release;
755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
758 sock_reset_flag(sk, SOCK_ZAPPED);
759
Ralf Baechle2536b942007-03-12 15:53:33 -0700760 if ((dev = rose_dev_first()) == NULL) {
761 err = -ENETUNREACH;
762 goto out_release;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
David Howellsc2a2b8d2008-11-14 10:39:08 +1100765 user = ax25_findbyuid(current_euid());
Ralf Baechle2536b942007-03-12 15:53:33 -0700766 if (!user) {
767 err = -EINVAL;
768 goto out_release;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 memcpy(&rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700772 rose->source_call = user->call;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 rose->device = dev;
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700774 ax25_uid_put(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 rose_insert_socket(sk); /* Finish the bind */
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 rose->dest_addr = addr->srose_addr;
779 rose->dest_call = addr->srose_call;
780 rose->rand = ((long)rose & 0xFFFF) + rose->lci;
781 rose->dest_ndigis = addr->srose_ndigis;
782
783 if (addr_len == sizeof(struct full_sockaddr_rose)) {
784 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
785 for (n = 0 ; n < addr->srose_ndigis ; n++)
786 rose->dest_digis[n] = full_addr->srose_digis[n];
787 } else {
788 if (rose->dest_ndigis == 1) {
789 rose->dest_digis[0] = addr->srose_digi;
790 }
791 }
792
793 /* Move to connecting socket, start sending Connect Requests */
794 sock->state = SS_CONNECTING;
795 sk->sk_state = TCP_SYN_SENT;
796
797 rose->state = ROSE_STATE_1;
798
799 rose->neighbour->use++;
800
801 rose_write_internal(sk, ROSE_CALL_REQUEST);
802 rose_start_heartbeat(sk);
803 rose_start_t1timer(sk);
804
805 /* Now the loop */
Ralf Baechle2536b942007-03-12 15:53:33 -0700806 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
807 err = -EINPROGRESS;
808 goto out_release;
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /*
812 * A Connect Ack with Choke or timeout or failed routing will go to
813 * closed.
814 */
815 if (sk->sk_state == TCP_SYN_SENT) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700816 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000819 prepare_to_wait(sk_sleep(sk), &wait,
YOSHIFUJI Hideaki6140efb2007-07-19 10:44:40 +0900820 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (sk->sk_state != TCP_SYN_SENT)
822 break;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700823 if (!signal_pending(current)) {
824 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 schedule();
Ralf Baechle2536b942007-03-12 15:53:33 -0700826 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 continue;
828 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700829 err = -ERESTARTSYS;
830 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000832 finish_wait(sk_sleep(sk), &wait);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700833
834 if (err)
835 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
838 if (sk->sk_state != TCP_ESTABLISHED) {
839 sock->state = SS_UNCONNECTED;
Ralf Baechle2536b942007-03-12 15:53:33 -0700840 err = sock_error(sk); /* Always set at this point */
841 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843
844 sock->state = SS_CONNECTED;
845
Ralf Baechle2536b942007-03-12 15:53:33 -0700846out_release:
847 release_sock(sk);
848
849 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
David Howellscdfbabf2017-03-09 08:09:05 +0000852static int rose_accept(struct socket *sock, struct socket *newsock, int flags,
853 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct sk_buff *skb;
856 struct sock *newsk;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700857 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 struct sock *sk;
859 int err = 0;
860
861 if ((sk = sock->sk) == NULL)
862 return -EINVAL;
863
864 lock_sock(sk);
865 if (sk->sk_type != SOCK_SEQPACKET) {
866 err = -EOPNOTSUPP;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700867 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
870 if (sk->sk_state != TCP_LISTEN) {
871 err = -EINVAL;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700872 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
875 /*
876 * The write queue this time is holding sockets ready to use
877 * hooked into the SABM we saved
878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000880 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 skb = skb_dequeue(&sk->sk_receive_queue);
883 if (skb)
884 break;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (flags & O_NONBLOCK) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700887 err = -EWOULDBLOCK;
888 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700890 if (!signal_pending(current)) {
891 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 schedule();
893 lock_sock(sk);
894 continue;
895 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700896 err = -ERESTARTSYS;
897 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000899 finish_wait(sk_sleep(sk), &wait);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700900 if (err)
901 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 newsk = skb->sk;
David S. Miller44ccff12008-06-17 02:39:21 -0700904 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* Now attach up the new socket */
907 skb->sk = NULL;
908 kfree_skb(skb);
Eric Dumazet7976a112019-11-05 14:11:52 -0800909 sk_acceptq_removed(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Ralf Baechle75606dc2007-04-20 16:06:45 -0700911out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 release_sock(sk);
913
914 return err;
915}
916
917static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100918 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
921 struct sock *sk = sock->sk;
922 struct rose_sock *rose = rose_sk(sk);
923 int n;
924
Eric Dumazet17ac2e92009-08-06 03:34:06 +0000925 memset(srose, 0, sizeof(*srose));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (peer != 0) {
927 if (sk->sk_state != TCP_ESTABLISHED)
928 return -ENOTCONN;
929 srose->srose_family = AF_ROSE;
930 srose->srose_addr = rose->dest_addr;
931 srose->srose_call = rose->dest_call;
932 srose->srose_ndigis = rose->dest_ndigis;
933 for (n = 0; n < rose->dest_ndigis; n++)
934 srose->srose_digis[n] = rose->dest_digis[n];
935 } else {
936 srose->srose_family = AF_ROSE;
937 srose->srose_addr = rose->source_addr;
938 srose->srose_call = rose->source_call;
939 srose->srose_ndigis = rose->source_ndigis;
940 for (n = 0; n < rose->source_ndigis; n++)
941 srose->srose_digis[n] = rose->source_digis[n];
942 }
943
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100944 return sizeof(struct full_sockaddr_rose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct rose_neigh *neigh, unsigned int lci)
948{
949 struct sock *sk;
950 struct sock *make;
951 struct rose_sock *make_rose;
952 struct rose_facilities_struct facilities;
Ben Hutchingse0bccd32011-03-20 06:48:05 +0000953 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 skb->sk = NULL; /* Initially we don't know who it's for */
956
957 /*
958 * skb->data points to the rose frame start
959 */
960 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
961
Ben Hutchingse0bccd32011-03-20 06:48:05 +0000962 if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
963 skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
964 &facilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
966 return 0;
967 }
968
969 sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
970
971 /*
972 * We can't accept the Call Request.
973 */
974 if (sk == NULL || sk_acceptq_is_full(sk) ||
975 (make = rose_make_new(sk)) == NULL) {
976 rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
977 return 0;
978 }
979
980 skb->sk = make;
981 make->sk_state = TCP_ESTABLISHED;
982 make_rose = rose_sk(make);
983
984 make_rose->lci = lci;
985 make_rose->dest_addr = facilities.dest_addr;
986 make_rose->dest_call = facilities.dest_call;
987 make_rose->dest_ndigis = facilities.dest_ndigis;
988 for (n = 0 ; n < facilities.dest_ndigis ; n++)
989 make_rose->dest_digis[n] = facilities.dest_digis[n];
990 make_rose->source_addr = facilities.source_addr;
991 make_rose->source_call = facilities.source_call;
992 make_rose->source_ndigis = facilities.source_ndigis;
993 for (n = 0 ; n < facilities.source_ndigis ; n++)
Weilong Chendb34de92013-12-19 11:55:17 +0800994 make_rose->source_digis[n] = facilities.source_digis[n];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 make_rose->neighbour = neigh;
996 make_rose->device = dev;
997 make_rose->facilities = facilities;
998
999 make_rose->neighbour->use++;
1000
1001 if (rose_sk(sk)->defer) {
1002 make_rose->state = ROSE_STATE_5;
1003 } else {
1004 rose_write_internal(make, ROSE_CALL_ACCEPTED);
1005 make_rose->state = ROSE_STATE_3;
1006 rose_start_idletimer(make);
1007 }
1008
1009 make_rose->condition = 0x00;
1010 make_rose->vs = 0;
1011 make_rose->va = 0;
1012 make_rose->vr = 0;
1013 make_rose->vl = 0;
Eric Dumazet7976a112019-11-05 14:11:52 -08001014 sk_acceptq_added(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 rose_insert_socket(make);
1017
1018 skb_queue_head(&sk->sk_receive_queue, skb);
1019
1020 rose_start_heartbeat(make);
1021
1022 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04001023 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 return 1;
1026}
1027
Ying Xue1b784142015-03-02 15:37:48 +08001028static int rose_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct sock *sk = sock->sk;
1031 struct rose_sock *rose = rose_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001032 DECLARE_SOCKADDR(struct sockaddr_rose *, usrose, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 int err;
1034 struct full_sockaddr_rose srose;
1035 struct sk_buff *skb;
1036 unsigned char *asmptr;
1037 int n, size, qbit = 0;
1038
1039 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1040 return -EINVAL;
1041
1042 if (sock_flag(sk, SOCK_ZAPPED))
1043 return -EADDRNOTAVAIL;
1044
1045 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1046 send_sig(SIGPIPE, current, 0);
1047 return -EPIPE;
1048 }
1049
1050 if (rose->neighbour == NULL || rose->device == NULL)
1051 return -ENETUNREACH;
1052
1053 if (usrose != NULL) {
1054 if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
1055 return -EINVAL;
1056 memset(&srose, 0, sizeof(struct full_sockaddr_rose));
1057 memcpy(&srose, usrose, msg->msg_namelen);
1058 if (rosecmp(&rose->dest_addr, &srose.srose_addr) != 0 ||
1059 ax25cmp(&rose->dest_call, &srose.srose_call) != 0)
1060 return -EISCONN;
1061 if (srose.srose_ndigis != rose->dest_ndigis)
1062 return -EISCONN;
1063 if (srose.srose_ndigis == rose->dest_ndigis) {
1064 for (n = 0 ; n < srose.srose_ndigis ; n++)
1065 if (ax25cmp(&rose->dest_digis[n],
1066 &srose.srose_digis[n]))
1067 return -EISCONN;
1068 }
1069 if (srose.srose_family != AF_ROSE)
1070 return -EINVAL;
1071 } else {
1072 if (sk->sk_state != TCP_ESTABLISHED)
1073 return -ENOTCONN;
1074
1075 srose.srose_family = AF_ROSE;
1076 srose.srose_addr = rose->dest_addr;
1077 srose.srose_call = rose->dest_call;
1078 srose.srose_ndigis = rose->dest_ndigis;
1079 for (n = 0 ; n < rose->dest_ndigis ; n++)
1080 srose.srose_digis[n] = rose->dest_digis[n];
1081 }
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* Build a packet */
Alan Cox83e0bbc2009-03-27 00:28:21 -07001084 /* Sanity check the packet size */
1085 if (len > 65535)
1086 return -EMSGSIZE;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
1089
1090 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1091 return err;
1092
1093 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
1094
1095 /*
1096 * Put the data on the end
1097 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001099 skb_reset_transport_header(skb);
1100 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Al Viro6ce8e9c2014-04-06 21:25:44 -04001102 err = memcpy_from_msg(skb_transport_header(skb), msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (err) {
1104 kfree_skb(skb);
1105 return err;
1106 }
1107
1108 /*
1109 * If the Q BIT Include socket option is in force, the first
1110 * byte of the user data is the logical value of the Q Bit.
1111 */
1112 if (rose->qbitincl) {
1113 qbit = skb->data[0];
1114 skb_pull(skb, 1);
1115 }
1116
1117 /*
1118 * Push down the ROSE header
1119 */
1120 asmptr = skb_push(skb, ROSE_MIN_LEN);
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* Build a ROSE Network header */
1123 asmptr[0] = ((rose->lci >> 8) & 0x0F) | ROSE_GFI;
1124 asmptr[1] = (rose->lci >> 0) & 0xFF;
1125 asmptr[2] = ROSE_DATA;
1126
1127 if (qbit)
1128 asmptr[0] |= ROSE_Q_BIT;
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (sk->sk_state != TCP_ESTABLISHED) {
1131 kfree_skb(skb);
1132 return -ENOTCONN;
1133 }
1134
1135#ifdef M_BIT
1136#define ROSE_PACLEN (256-ROSE_MIN_LEN)
1137 if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
1138 unsigned char header[ROSE_MIN_LEN];
1139 struct sk_buff *skbn;
1140 int frontlen;
1141 int lg;
1142
1143 /* Save a copy of the Header */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001144 skb_copy_from_linear_data(skb, header, ROSE_MIN_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 skb_pull(skb, ROSE_MIN_LEN);
1146
1147 frontlen = skb_headroom(skb);
1148
1149 while (skb->len > 0) {
1150 if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, &err)) == NULL) {
1151 kfree_skb(skb);
1152 return err;
1153 }
1154
1155 skbn->sk = sk;
1156 skbn->free = 1;
1157 skbn->arp = 1;
1158
1159 skb_reserve(skbn, frontlen);
1160
1161 lg = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN;
1162
1163 /* Copy the user data */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001164 skb_copy_from_linear_data(skb, skb_put(skbn, lg), lg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 skb_pull(skb, lg);
1166
1167 /* Duplicate the Header */
1168 skb_push(skbn, ROSE_MIN_LEN);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001169 skb_copy_to_linear_data(skbn, header, ROSE_MIN_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 if (skb->len > 0)
1172 skbn->data[2] |= M_BIT;
1173
1174 skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
1175 }
1176
1177 skb->free = 1;
1178 kfree_skb(skb);
1179 } else {
1180 skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
1181 }
1182#else
1183 skb_queue_tail(&sk->sk_write_queue, skb); /* Shove it onto the queue */
1184#endif
1185
1186 rose_kick(sk);
1187
1188 return len;
1189}
1190
1191
Ying Xue1b784142015-03-02 15:37:48 +08001192static int rose_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1193 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct sock *sk = sock->sk;
1196 struct rose_sock *rose = rose_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 size_t copied;
1198 unsigned char *asmptr;
1199 struct sk_buff *skb;
1200 int n, er, qbit;
1201
1202 /*
1203 * This works for seqpacket too. The receiver has ordered the queue for
1204 * us! We do one quick check first though
1205 */
1206 if (sk->sk_state != TCP_ESTABLISHED)
1207 return -ENOTCONN;
1208
1209 /* Now we can treat all alike */
1210 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1211 return er;
1212
1213 qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
1214
1215 skb_pull(skb, ROSE_MIN_LEN);
1216
1217 if (rose->qbitincl) {
1218 asmptr = skb_push(skb, 1);
1219 *asmptr = qbit;
1220 }
1221
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001222 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 copied = skb->len;
1224
1225 if (copied > size) {
1226 copied = size;
1227 msg->msg_flags |= MSG_TRUNC;
1228 }
1229
David S. Miller51f3d022014-11-05 16:46:40 -05001230 skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001232 if (msg->msg_name) {
1233 struct sockaddr_rose *srose;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001234 DECLARE_SOCKADDR(struct full_sockaddr_rose *, full_srose,
1235 msg->msg_name);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001236
1237 memset(msg->msg_name, 0, sizeof(struct full_sockaddr_rose));
1238 srose = msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 srose->srose_family = AF_ROSE;
1240 srose->srose_addr = rose->dest_addr;
1241 srose->srose_call = rose->dest_call;
1242 srose->srose_ndigis = rose->dest_ndigis;
Florian Westphalf81152e2013-12-23 00:32:31 +01001243 for (n = 0 ; n < rose->dest_ndigis ; n++)
1244 full_srose->srose_digis[n] = rose->dest_digis[n];
1245 msg->msg_namelen = sizeof(struct full_sockaddr_rose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
1248 skb_free_datagram(sk, skb);
1249
1250 return copied;
1251}
1252
1253
1254static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1255{
1256 struct sock *sk = sock->sk;
1257 struct rose_sock *rose = rose_sk(sk);
1258 void __user *argp = (void __user *)arg;
1259
1260 switch (cmd) {
1261 case TIOCOUTQ: {
1262 long amount;
Eric Dumazet31e6d362009-06-17 19:05:41 -07001263
1264 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (amount < 0)
1266 amount = 0;
Ralf Baechle20b7d102005-09-12 14:24:55 -07001267 return put_user(amount, (unsigned int __user *) argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269
1270 case TIOCINQ: {
1271 struct sk_buff *skb;
1272 long amount = 0L;
1273 /* These two are safe on a single CPU system as only user tasks fiddle here */
1274 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1275 amount = skb->len;
Ralf Baechle20b7d102005-09-12 14:24:55 -07001276 return put_user(amount, (unsigned int __user *) argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 case SIOCGIFADDR:
1280 case SIOCSIFADDR:
1281 case SIOCGIFDSTADDR:
1282 case SIOCSIFDSTADDR:
1283 case SIOCGIFBRDADDR:
1284 case SIOCSIFBRDADDR:
1285 case SIOCGIFNETMASK:
1286 case SIOCSIFNETMASK:
1287 case SIOCGIFMETRIC:
1288 case SIOCSIFMETRIC:
1289 return -EINVAL;
1290
1291 case SIOCADDRT:
1292 case SIOCDELRT:
1293 case SIOCRSCLRRT:
1294 if (!capable(CAP_NET_ADMIN))
1295 return -EPERM;
1296 return rose_rt_ioctl(cmd, argp);
1297
1298 case SIOCRSGCAUSE: {
1299 struct rose_cause_struct rose_cause;
1300 rose_cause.cause = rose->cause;
1301 rose_cause.diagnostic = rose->diagnostic;
1302 return copy_to_user(argp, &rose_cause, sizeof(struct rose_cause_struct)) ? -EFAULT : 0;
1303 }
1304
1305 case SIOCRSSCAUSE: {
1306 struct rose_cause_struct rose_cause;
1307 if (copy_from_user(&rose_cause, argp, sizeof(struct rose_cause_struct)))
1308 return -EFAULT;
1309 rose->cause = rose_cause.cause;
1310 rose->diagnostic = rose_cause.diagnostic;
1311 return 0;
1312 }
1313
1314 case SIOCRSSL2CALL:
1315 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1316 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1317 ax25_listen_release(&rose_callsign, NULL);
1318 if (copy_from_user(&rose_callsign, argp, sizeof(ax25_address)))
1319 return -EFAULT;
1320 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
Ralf Baechle81dcd162006-12-14 15:50:34 -08001321 return ax25_listen_register(&rose_callsign, NULL);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return 0;
1324
1325 case SIOCRSGL2CALL:
1326 return copy_to_user(argp, &rose_callsign, sizeof(ax25_address)) ? -EFAULT : 0;
1327
1328 case SIOCRSACCEPT:
1329 if (rose->state == ROSE_STATE_5) {
1330 rose_write_internal(sk, ROSE_CALL_ACCEPTED);
1331 rose_start_idletimer(sk);
1332 rose->condition = 0x00;
1333 rose->vs = 0;
1334 rose->va = 0;
1335 rose->vr = 0;
1336 rose->vl = 0;
1337 rose->state = ROSE_STATE_3;
1338 }
1339 return 0;
1340
1341 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001342 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
1345 return 0;
1346}
1347
1348#ifdef CONFIG_PROC_FS
1349static void *rose_info_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet95b7d922008-01-15 03:30:35 -08001350 __acquires(rose_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 spin_lock_bh(&rose_list_lock);
Li Zefanb9997482010-02-08 23:19:17 +00001353 return seq_hlist_start_head(&rose_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356static void *rose_info_next(struct seq_file *seq, void *v, loff_t *pos)
1357{
Li Zefanb9997482010-02-08 23:19:17 +00001358 return seq_hlist_next(v, &rose_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361static void rose_info_stop(struct seq_file *seq, void *v)
Eric Dumazet95b7d922008-01-15 03:30:35 -08001362 __releases(rose_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 spin_unlock_bh(&rose_list_lock);
1365}
1366
1367static int rose_info_show(struct seq_file *seq, void *v)
1368{
Ralf Baechledcf777f2009-07-26 19:11:14 -07001369 char buf[11], rsbuf[11];
Ralf Baechlef75268c2005-09-06 15:49:39 -07001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001372 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 "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");
1374
1375 else {
Li Zefanb9997482010-02-08 23:19:17 +00001376 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 struct rose_sock *rose = rose_sk(s);
1378 const char *devname, *callsign;
1379 const struct net_device *dev = rose->device;
1380
1381 if (!dev)
1382 devname = "???";
1383 else
1384 devname = dev->name;
YOSHIFUJI Hideaki3dcf7c52007-02-09 23:25:12 +09001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 seq_printf(seq, "%-10s %-9s ",
Ralf Baechledcf777f2009-07-26 19:11:14 -07001387 rose2asc(rsbuf, &rose->dest_addr),
1388 ax2asc(buf, &rose->dest_call));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 if (ax25cmp(&rose->source_call, &null_ax25_address) == 0)
1391 callsign = "??????-?";
1392 else
Ralf Baechlef75268c2005-09-06 15:49:39 -07001393 callsign = ax2asc(buf, &rose->source_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 seq_printf(seq,
1396 "%-10s %-9s %-5s %3.3X %05d %d %d %d %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
Ralf Baechledcf777f2009-07-26 19:11:14 -07001397 rose2asc(rsbuf, &rose->source_addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 callsign,
1399 devname,
1400 rose->lci & 0x0FFF,
1401 (rose->neighbour) ? rose->neighbour->number : 0,
1402 rose->state,
1403 rose->vs,
1404 rose->vr,
1405 rose->va,
1406 ax25_display_timer(&rose->timer) / HZ,
1407 rose->t1 / HZ,
1408 rose->t2 / HZ,
1409 rose->t3 / HZ,
1410 rose->hb / HZ,
1411 ax25_display_timer(&rose->idletimer) / (60 * HZ),
1412 rose->idle / (60 * HZ),
Eric Dumazet31e6d362009-06-17 19:05:41 -07001413 sk_wmem_alloc_get(s),
1414 sk_rmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1416 }
1417
1418 return 0;
1419}
1420
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001421static const struct seq_operations rose_info_seqops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 .start = rose_info_start,
1423 .next = rose_info_next,
1424 .stop = rose_info_stop,
1425 .show = rose_info_show,
1426};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427#endif /* CONFIG_PROC_FS */
1428
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001429static const struct net_proto_family rose_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 .family = PF_ROSE,
1431 .create = rose_create,
1432 .owner = THIS_MODULE,
1433};
1434
Alexey Dobriyan5708e862009-09-14 12:23:23 +00001435static const struct proto_ops rose_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 .family = PF_ROSE,
1437 .owner = THIS_MODULE,
1438 .release = rose_release,
1439 .bind = rose_bind,
1440 .connect = rose_connect,
1441 .socketpair = sock_no_socketpair,
1442 .accept = rose_accept,
1443 .getname = rose_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001444 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 .ioctl = rose_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001446 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 .listen = rose_listen,
1448 .shutdown = sock_no_shutdown,
1449 .setsockopt = rose_setsockopt,
1450 .getsockopt = rose_getsockopt,
1451 .sendmsg = rose_sendmsg,
1452 .recvmsg = rose_recvmsg,
1453 .mmap = sock_no_mmap,
1454 .sendpage = sock_no_sendpage,
1455};
1456
1457static struct notifier_block rose_dev_notifier = {
1458 .notifier_call = rose_device_event,
1459};
1460
1461static struct net_device **dev_rose;
1462
Ralf Baechle8d5cf592006-12-14 15:50:01 -08001463static struct ax25_protocol rose_pid = {
1464 .pid = AX25_P_ROSE,
1465 .func = rose_route_frame
1466};
1467
Ralf Baechlea4282712006-12-14 15:51:23 -08001468static struct ax25_linkfail rose_linkfail_notifier = {
1469 .func = rose_link_failed
1470};
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472static int __init rose_proto_init(void)
1473{
1474 int i;
Alexey Dobriyana83cd2c2005-09-27 15:44:36 -07001475 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Alexey Dobriyana83cd2c2005-09-27 15:44:36 -07001477 if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
1478 printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
1479 rc = -EINVAL;
1480 goto out;
1481 }
1482
1483 rc = proto_register(&rose_proto, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (rc != 0)
1485 goto out;
1486
1487 rose_callsign = null_ax25_address;
1488
Kees Cook6396bb22018-06-12 14:03:40 -07001489 dev_rose = kcalloc(rose_ndevs, sizeof(struct net_device *),
1490 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (dev_rose == NULL) {
1492 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
Alexey Dobriyana83cd2c2005-09-27 15:44:36 -07001493 rc = -ENOMEM;
1494 goto out_proto_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 for (i = 0; i < rose_ndevs; i++) {
1498 struct net_device *dev;
1499 char name[IFNAMSIZ];
1500
1501 sprintf(name, "rose%d", i);
Tom Gundersenc835a672014-07-14 16:37:24 +02001502 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, rose_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (!dev) {
1504 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
Alexey Dobriyan70ff3b62005-09-27 15:43:46 -07001505 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 goto fail;
1507 }
Alexey Dobriyan70ff3b62005-09-27 15:43:46 -07001508 rc = register_netdev(dev);
1509 if (rc) {
Alexey Dobriyan520d1b82005-09-27 15:45:15 -07001510 printk(KERN_ERR "ROSE: netdevice registration failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 free_netdev(dev);
1512 goto fail;
1513 }
1514 dev_rose[i] = dev;
1515 }
1516
1517 sock_register(&rose_family_ops);
1518 register_netdevice_notifier(&rose_dev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Ralf Baechle8d5cf592006-12-14 15:50:01 -08001520 ax25_register_pid(&rose_pid);
Ralf Baechlea4282712006-12-14 15:51:23 -08001521 ax25_linkfail_register(&rose_linkfail_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523#ifdef CONFIG_SYSCTL
1524 rose_register_sysctl();
1525#endif
1526 rose_loopback_init();
1527
1528 rose_add_loopback_neigh();
1529
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02001530 proc_create_seq("rose", 0444, init_net.proc_net, &rose_info_seqops);
1531 proc_create_seq("rose_neigh", 0444, init_net.proc_net,
1532 &rose_neigh_seqops);
1533 proc_create_seq("rose_nodes", 0444, init_net.proc_net,
1534 &rose_node_seqops);
1535 proc_create_seq("rose_routes", 0444, init_net.proc_net,
1536 &rose_route_seqops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537out:
1538 return rc;
1539fail:
1540 while (--i >= 0) {
1541 unregister_netdev(dev_rose[i]);
1542 free_netdev(dev_rose[i]);
1543 }
1544 kfree(dev_rose);
Alexey Dobriyana83cd2c2005-09-27 15:44:36 -07001545out_proto_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 proto_unregister(&rose_proto);
Alexey Dobriyan70ff3b62005-09-27 15:43:46 -07001547 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549module_init(rose_proto_init);
1550
1551module_param(rose_ndevs, int, 0);
1552MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
1553
1554MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1555MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1556MODULE_LICENSE("GPL");
1557MODULE_ALIAS_NETPROTO(PF_ROSE);
1558
1559static void __exit rose_exit(void)
1560{
1561 int i;
1562
Gao fengece31ff2013-02-18 01:34:56 +00001563 remove_proc_entry("rose", init_net.proc_net);
1564 remove_proc_entry("rose_neigh", init_net.proc_net);
1565 remove_proc_entry("rose_nodes", init_net.proc_net);
1566 remove_proc_entry("rose_routes", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 rose_loopback_clear();
1568
1569 rose_rt_free();
1570
1571 ax25_protocol_release(AX25_P_ROSE);
Ralf Baechlea4282712006-12-14 15:51:23 -08001572 ax25_linkfail_release(&rose_linkfail_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1575 ax25_listen_release(&rose_callsign, NULL);
1576
1577#ifdef CONFIG_SYSCTL
1578 rose_unregister_sysctl();
1579#endif
1580 unregister_netdevice_notifier(&rose_dev_notifier);
1581
1582 sock_unregister(PF_ROSE);
1583
1584 for (i = 0; i < rose_ndevs; i++) {
1585 struct net_device *dev = dev_rose[i];
1586
1587 if (dev) {
1588 unregister_netdev(dev);
1589 free_netdev(dev);
1590 }
1591 }
1592
1593 kfree(dev_rose);
1594 proto_unregister(&rose_proto);
1595}
1596
1597module_exit(rose_exit);