blob: 96740d389377170b90a5d3d2dba3482bd293eb9a [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 Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5 * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
6 * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/moduleparam.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
12#include <linux/types.h>
13#include <linux/socket.h>
14#include <linux/in.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010017#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/timer.h>
19#include <linux/string.h>
20#include <linux/sockios.h>
21#include <linux/net.h>
22#include <linux/stat.h>
23#include <net/ax25.h>
24#include <linux/inet.h>
25#include <linux/netdevice.h>
26#include <linux/if_arp.h>
27#include <linux/skbuff.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020028#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/sock.h>
Fabian Frederickdc8e5412014-10-17 22:00:22 +020030#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/fcntl.h>
32#include <linux/termios.h> /* For TIOCINQ/OUTQ */
33#include <linux/mm.h>
34#include <linux/interrupt.h>
35#include <linux/notifier.h>
36#include <net/netrom.h>
37#include <linux/proc_fs.h>
38#include <linux/seq_file.h>
39#include <net/ip.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070040#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/arp.h>
42#include <linux/init.h>
43
44static int nr_ndevs = 4;
45
46int sysctl_netrom_default_path_quality = NR_DEFAULT_QUAL;
47int sysctl_netrom_obsolescence_count_initialiser = NR_DEFAULT_OBS;
48int sysctl_netrom_network_ttl_initialiser = NR_DEFAULT_TTL;
49int sysctl_netrom_transport_timeout = NR_DEFAULT_T1;
50int sysctl_netrom_transport_maximum_tries = NR_DEFAULT_N2;
51int sysctl_netrom_transport_acknowledge_delay = NR_DEFAULT_T2;
52int sysctl_netrom_transport_busy_delay = NR_DEFAULT_T4;
53int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW;
54int sysctl_netrom_transport_no_activity_timeout = NR_DEFAULT_IDLE;
55int sysctl_netrom_routing_control = NR_DEFAULT_ROUTING;
56int sysctl_netrom_link_fails_count = NR_DEFAULT_FAILS;
Ralf Baechlee21ce8c2005-09-12 14:27:37 -070057int sysctl_netrom_reset_circuit = NR_DEFAULT_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static unsigned short circuit = 0x101;
60
61static HLIST_HEAD(nr_list);
62static DEFINE_SPINLOCK(nr_list_lock);
63
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080064static const struct proto_ops nr_proto_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
Ralf Baechle4b260a92006-07-12 13:26:26 -070067 * NETROM network devices are virtual network devices encapsulating NETROM
68 * frames into AX.25 which will be sent through an AX.25 device, so form a
69 * special "super class" of normal net devices; split their locks off into a
70 * separate class since they always nest.
71 */
72static struct lock_class_key nr_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -070073static struct lock_class_key nr_netdev_addr_lock_key;
Ralf Baechle4b260a92006-07-12 13:26:26 -070074
David S. Millere8a04642008-07-17 00:34:19 -070075static void nr_set_lockdep_one(struct net_device *dev,
76 struct netdev_queue *txq,
77 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -070078{
79 lockdep_set_class(&txq->_xmit_lock, &nr_netdev_xmit_lock_key);
80}
81
82static void nr_set_lockdep_key(struct net_device *dev)
83{
David S. Millercf508b12008-07-22 14:16:42 -070084 lockdep_set_class(&dev->addr_list_lock, &nr_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -070085 netdev_for_each_tx_queue(dev, nr_set_lockdep_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -070086}
87
Ralf Baechle4b260a92006-07-12 13:26:26 -070088/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * Socket removal during an interrupt is now safe.
90 */
91static void nr_remove_socket(struct sock *sk)
92{
93 spin_lock_bh(&nr_list_lock);
94 sk_del_node_init(sk);
95 spin_unlock_bh(&nr_list_lock);
96}
97
98/*
99 * Kill all bound sockets on a dropped device.
100 */
101static void nr_kill_by_device(struct net_device *dev)
102{
103 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 spin_lock_bh(&nr_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800106 sk_for_each(s, &nr_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (nr_sk(s)->device == dev)
108 nr_disconnect(s, ENETUNREACH);
109 spin_unlock_bh(&nr_list_lock);
110}
111
112/*
113 * Handle device status changes.
114 */
115static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
116{
Jiri Pirko351638e2013-05-28 01:30:21 +0000117 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700119 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200120 return NOTIFY_DONE;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (event != NETDEV_DOWN)
123 return NOTIFY_DONE;
124
125 nr_kill_by_device(dev);
126 nr_rt_device_down(dev);
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return NOTIFY_DONE;
129}
130
131/*
132 * Add a socket to the bound sockets list.
133 */
134static void nr_insert_socket(struct sock *sk)
135{
136 spin_lock_bh(&nr_list_lock);
137 sk_add_node(sk, &nr_list);
138 spin_unlock_bh(&nr_list_lock);
139}
140
141/*
142 * Find a socket that wants to accept the Connect Request we just
143 * received.
144 */
145static struct sock *nr_find_listener(ax25_address *addr)
146{
147 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 spin_lock_bh(&nr_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800150 sk_for_each(s, &nr_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
152 s->sk_state == TCP_LISTEN) {
Cong Wang7314f542018-12-29 13:56:38 -0800153 sock_hold(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 goto found;
155 }
156 s = NULL;
157found:
158 spin_unlock_bh(&nr_list_lock);
159 return s;
160}
161
162/*
163 * Find a connected NET/ROM socket given my circuit IDs.
164 */
165static struct sock *nr_find_socket(unsigned char index, unsigned char id)
166{
167 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 spin_lock_bh(&nr_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800170 sk_for_each(s, &nr_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct nr_sock *nr = nr_sk(s);
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (nr->my_index == index && nr->my_id == id) {
Cong Wang7314f542018-12-29 13:56:38 -0800174 sock_hold(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto found;
176 }
177 }
178 s = NULL;
179found:
180 spin_unlock_bh(&nr_list_lock);
181 return s;
182}
183
184/*
185 * Find a connected NET/ROM socket given their circuit IDs.
186 */
187static struct sock *nr_find_peer(unsigned char index, unsigned char id,
188 ax25_address *dest)
189{
190 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 spin_lock_bh(&nr_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800193 sk_for_each(s, &nr_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct nr_sock *nr = nr_sk(s);
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (nr->your_index == index && nr->your_id == id &&
197 !ax25cmp(&nr->dest_addr, dest)) {
Cong Wang7314f542018-12-29 13:56:38 -0800198 sock_hold(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto found;
200 }
201 }
202 s = NULL;
203found:
204 spin_unlock_bh(&nr_list_lock);
205 return s;
206}
207
208/*
209 * Find next free circuit ID.
210 */
211static unsigned short nr_find_next_circuit(void)
212{
213 unsigned short id = circuit;
214 unsigned char i, j;
215 struct sock *sk;
216
217 for (;;) {
218 i = id / 256;
219 j = id % 256;
220
221 if (i != 0 && j != 0) {
222 if ((sk=nr_find_socket(i, j)) == NULL)
223 break;
Cong Wang7314f542018-12-29 13:56:38 -0800224 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
227 id++;
228 }
229
230 return id;
231}
232
233/*
234 * Deferred destroy.
235 */
236void nr_destroy_socket(struct sock *);
237
238/*
239 * Handler for deferred kills.
240 */
Kees Cook99767f22017-10-16 17:29:36 -0700241static void nr_destroy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Kees Cook99767f22017-10-16 17:29:36 -0700243 struct sock *sk = from_timer(sk, t, sk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 bh_lock_sock(sk);
245 sock_hold(sk);
246 nr_destroy_socket(sk);
247 bh_unlock_sock(sk);
248 sock_put(sk);
249}
250
251/*
252 * This is called from user mode and the timers. Thus it protects itself
253 * against interrupt users but doesn't worry about being called during
254 * work. Once it is removed from the queue no interrupt or bottom half
255 * will touch it and we are (fairly 8-) ) safe.
256 */
257void nr_destroy_socket(struct sock *sk)
258{
259 struct sk_buff *skb;
260
261 nr_remove_socket(sk);
262
263 nr_stop_heartbeat(sk);
264 nr_stop_t1timer(sk);
265 nr_stop_t2timer(sk);
266 nr_stop_t4timer(sk);
267 nr_stop_idletimer(sk);
268
269 nr_clear_queues(sk); /* Flush the queues */
270
271 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
272 if (skb->sk != sk) { /* A pending connection */
273 /* Queue the unaccepted socket for death */
274 sock_set_flag(skb->sk, SOCK_DEAD);
275 nr_start_heartbeat(skb->sk);
276 nr_sk(skb->sk)->state = NR_STATE_0;
277 }
278
279 kfree_skb(skb);
280 }
281
Eric Dumazetc5640392009-06-16 10:12:03 +0000282 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* Defer: outstanding buffers */
Kees Cook841b86f2017-10-23 09:40:42 +0200284 sk->sk_timer.function = nr_destroy_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 sk->sk_timer.expires = jiffies + 2 * HZ;
286 add_timer(&sk->sk_timer);
287 } else
288 sock_put(sk);
289}
290
291/*
292 * Handling for system calls applied via the various interfaces to a
293 * NET/ROM socket object.
294 */
295
296static int nr_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700297 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct sock *sk = sock->sk;
300 struct nr_sock *nr = nr_sk(sk);
Xi Wang32288eb2011-12-27 09:44:53 +0000301 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (level != SOL_NETROM)
304 return -ENOPROTOOPT;
305
Xi Wang32288eb2011-12-27 09:44:53 +0000306 if (optlen < sizeof(unsigned int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -EINVAL;
308
Xi Wang32288eb2011-12-27 09:44:53 +0000309 if (get_user(opt, (unsigned int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -EFAULT;
311
312 switch (optname) {
313 case NETROM_T1:
Xi Wang32288eb2011-12-27 09:44:53 +0000314 if (opt < 1 || opt > ULONG_MAX / HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -EINVAL;
316 nr->t1 = opt * HZ;
317 return 0;
318
319 case NETROM_T2:
Xi Wang32288eb2011-12-27 09:44:53 +0000320 if (opt < 1 || opt > ULONG_MAX / HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -EINVAL;
322 nr->t2 = opt * HZ;
323 return 0;
324
325 case NETROM_N2:
326 if (opt < 1 || opt > 31)
327 return -EINVAL;
328 nr->n2 = opt;
329 return 0;
330
331 case NETROM_T4:
Xi Wang32288eb2011-12-27 09:44:53 +0000332 if (opt < 1 || opt > ULONG_MAX / HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return -EINVAL;
334 nr->t4 = opt * HZ;
335 return 0;
336
337 case NETROM_IDLE:
Xi Wang32288eb2011-12-27 09:44:53 +0000338 if (opt > ULONG_MAX / (60 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -EINVAL;
340 nr->idle = opt * 60 * HZ;
341 return 0;
342
343 default:
344 return -ENOPROTOOPT;
345 }
346}
347
348static int nr_getsockopt(struct socket *sock, int level, int optname,
349 char __user *optval, int __user *optlen)
350{
351 struct sock *sk = sock->sk;
352 struct nr_sock *nr = nr_sk(sk);
353 int val = 0;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900354 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (level != SOL_NETROM)
357 return -ENOPROTOOPT;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (get_user(len, optlen))
360 return -EFAULT;
361
362 if (len < 0)
363 return -EINVAL;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 switch (optname) {
366 case NETROM_T1:
367 val = nr->t1 / HZ;
368 break;
369
370 case NETROM_T2:
371 val = nr->t2 / HZ;
372 break;
373
374 case NETROM_N2:
375 val = nr->n2;
376 break;
377
378 case NETROM_T4:
379 val = nr->t4 / HZ;
380 break;
381
382 case NETROM_IDLE:
383 val = nr->idle / (60 * HZ);
384 break;
385
386 default:
387 return -ENOPROTOOPT;
388 }
389
390 len = min_t(unsigned int, len, sizeof(int));
391
392 if (put_user(len, optlen))
393 return -EFAULT;
394
395 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
396}
397
398static int nr_listen(struct socket *sock, int backlog)
399{
400 struct sock *sk = sock->sk;
401
402 lock_sock(sk);
403 if (sk->sk_state != TCP_LISTEN) {
404 memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
405 sk->sk_max_ack_backlog = backlog;
406 sk->sk_state = TCP_LISTEN;
407 release_sock(sk);
408 return 0;
409 }
410 release_sock(sk);
411
412 return -EOPNOTSUPP;
413}
414
415static struct proto nr_proto = {
416 .name = "NETROM",
417 .owner = THIS_MODULE,
418 .obj_size = sizeof(struct nr_sock),
419};
420
Eric Paris3f378b62009-11-05 22:18:14 -0800421static int nr_create(struct net *net, struct socket *sock, int protocol,
422 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct sock *sk;
425 struct nr_sock *nr;
426
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800427 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700428 return -EAFNOSUPPORT;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (sock->type != SOCK_SEQPACKET || protocol != 0)
431 return -ESOCKTNOSUPPORT;
432
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500433 sk = sk_alloc(net, PF_NETROM, GFP_ATOMIC, &nr_proto, kern);
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700434 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return -ENOMEM;
436
437 nr = nr_sk(sk);
438
439 sock_init_data(sock, sk);
440
441 sock->ops = &nr_proto_ops;
442 sk->sk_protocol = protocol;
443
444 skb_queue_head_init(&nr->ack_queue);
445 skb_queue_head_init(&nr->reseq_queue);
446 skb_queue_head_init(&nr->frag_queue);
447
448 nr_init_timers(sk);
449
Ralf Baechle4d8937d2006-05-03 23:27:47 -0700450 nr->t1 =
451 msecs_to_jiffies(sysctl_netrom_transport_timeout);
452 nr->t2 =
453 msecs_to_jiffies(sysctl_netrom_transport_acknowledge_delay);
454 nr->n2 =
455 msecs_to_jiffies(sysctl_netrom_transport_maximum_tries);
456 nr->t4 =
457 msecs_to_jiffies(sysctl_netrom_transport_busy_delay);
458 nr->idle =
459 msecs_to_jiffies(sysctl_netrom_transport_no_activity_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 nr->window = sysctl_netrom_transport_requested_window_size;
461
462 nr->bpqext = 1;
463 nr->state = NR_STATE_0;
464
465 return 0;
466}
467
468static struct sock *nr_make_new(struct sock *osk)
469{
470 struct sock *sk;
471 struct nr_sock *nr, *onr;
472
473 if (osk->sk_type != SOCK_SEQPACKET)
474 return NULL;
475
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500476 sk = sk_alloc(sock_net(osk), PF_NETROM, GFP_ATOMIC, osk->sk_prot, 0);
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700477 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return NULL;
479
480 nr = nr_sk(sk);
481
482 sock_init_data(NULL, sk);
483
484 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 sk->sk_priority = osk->sk_priority;
486 sk->sk_protocol = osk->sk_protocol;
487 sk->sk_rcvbuf = osk->sk_rcvbuf;
488 sk->sk_sndbuf = osk->sk_sndbuf;
489 sk->sk_state = TCP_ESTABLISHED;
Ralf Baechle53b924b2005-08-23 10:11:30 -0700490 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 skb_queue_head_init(&nr->ack_queue);
493 skb_queue_head_init(&nr->reseq_queue);
494 skb_queue_head_init(&nr->frag_queue);
495
496 nr_init_timers(sk);
497
498 onr = nr_sk(osk);
499
500 nr->t1 = onr->t1;
501 nr->t2 = onr->t2;
502 nr->n2 = onr->n2;
503 nr->t4 = onr->t4;
504 nr->idle = onr->idle;
505 nr->window = onr->window;
506
507 nr->device = onr->device;
508 nr->bpqext = onr->bpqext;
509
510 return sk;
511}
512
513static int nr_release(struct socket *sock)
514{
515 struct sock *sk = sock->sk;
516 struct nr_sock *nr;
517
518 if (sk == NULL) return 0;
519
520 sock_hold(sk);
Jarek Poplawski859f4c72008-10-06 12:54:57 -0700521 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 lock_sock(sk);
523 nr = nr_sk(sk);
524
525 switch (nr->state) {
526 case NR_STATE_0:
527 case NR_STATE_1:
528 case NR_STATE_2:
529 nr_disconnect(sk, 0);
530 nr_destroy_socket(sk);
531 break;
532
533 case NR_STATE_3:
534 nr_clear_queues(sk);
535 nr->n2count = 0;
536 nr_write_internal(sk, NR_DISCREQ);
537 nr_start_t1timer(sk);
538 nr_stop_t2timer(sk);
539 nr_stop_t4timer(sk);
540 nr_stop_idletimer(sk);
541 nr->state = NR_STATE_2;
542 sk->sk_state = TCP_CLOSE;
543 sk->sk_shutdown |= SEND_SHUTDOWN;
544 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 sock_set_flag(sk, SOCK_DESTROY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
547
548 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
550 }
551
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900552 sock->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 release_sock(sk);
554 sock_put(sk);
555
556 return 0;
557}
558
559static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
560{
561 struct sock *sk = sock->sk;
562 struct nr_sock *nr = nr_sk(sk);
563 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
564 struct net_device *dev;
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700565 ax25_uid_assoc *user;
566 ax25_address *source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 lock_sock(sk);
569 if (!sock_flag(sk, SOCK_ZAPPED)) {
570 release_sock(sk);
571 return -EINVAL;
572 }
573 if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
574 release_sock(sk);
575 return -EINVAL;
576 }
577 if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
578 release_sock(sk);
579 return -EINVAL;
580 }
581 if (addr->fsa_ax25.sax25_family != AF_NETROM) {
582 release_sock(sk);
583 return -EINVAL;
584 }
585 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 release_sock(sk);
587 return -EADDRNOTAVAIL;
588 }
589
590 /*
591 * Only the super user can set an arbitrary user callsign.
592 */
593 if (addr->fsa_ax25.sax25_ndigis == 1) {
594 if (!capable(CAP_NET_BIND_SERVICE)) {
595 dev_put(dev);
596 release_sock(sk);
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +0000597 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599 nr->user_addr = addr->fsa_digipeater[0];
600 nr->source_addr = addr->fsa_ax25.sax25_call;
601 } else {
602 source = &addr->fsa_ax25.sax25_call;
603
David Howellsba95b232008-11-14 10:39:08 +1100604 user = ax25_findbyuid(current_euid());
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700605 if (user) {
606 nr->user_addr = user->call;
607 ax25_uid_put(user);
608 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
610 release_sock(sk);
611 dev_put(dev);
612 return -EPERM;
613 }
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700614 nr->user_addr = *source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 nr->source_addr = *source;
618 }
619
620 nr->device = dev;
621 nr_insert_socket(sk);
622
623 sock_reset_flag(sk, SOCK_ZAPPED);
624 dev_put(dev);
625 release_sock(sk);
Ralf Baechle8849b722011-04-14 00:20:07 -0700626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
628}
629
630static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
631 int addr_len, int flags)
632{
633 struct sock *sk = sock->sk;
634 struct nr_sock *nr = nr_sk(sk);
635 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700636 ax25_address *source = NULL;
637 ax25_uid_assoc *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct net_device *dev;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700639 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 lock_sock(sk);
642 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
643 sock->state = SS_CONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700644 goto out_release; /* Connect completed during a ERESTARTSYS event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
648 sock->state = SS_UNCONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700649 err = -ECONNREFUSED;
650 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
653 if (sk->sk_state == TCP_ESTABLISHED) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700654 err = -EISCONN; /* No reconnect on a seqpacket socket */
655 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900658 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 sock->state = SS_UNCONNECTED;
660
661 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700662 err = -EINVAL;
663 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665 if (addr->sax25_family != AF_NETROM) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700666 err = -EINVAL;
667 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
670 sock_reset_flag(sk, SOCK_ZAPPED);
671
672 if ((dev = nr_dev_first()) == NULL) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700673 err = -ENETUNREACH;
674 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 source = (ax25_address *)dev->dev_addr;
677
David Howellsba95b232008-11-14 10:39:08 +1100678 user = ax25_findbyuid(current_euid());
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700679 if (user) {
680 nr->user_addr = user->call;
681 ax25_uid_put(user);
682 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
684 dev_put(dev);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700685 err = -EPERM;
686 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700688 nr->user_addr = *source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 nr->source_addr = *source;
692 nr->device = dev;
693
694 dev_put(dev);
695 nr_insert_socket(sk); /* Finish the bind */
696 }
697
698 nr->dest_addr = addr->sax25_call;
699
700 release_sock(sk);
701 circuit = nr_find_next_circuit();
702 lock_sock(sk);
703
704 nr->my_index = circuit / 256;
705 nr->my_id = circuit % 256;
706
707 circuit++;
708
709 /* Move to connecting socket, start sending Connect Requests */
710 sock->state = SS_CONNECTING;
711 sk->sk_state = TCP_SYN_SENT;
712
713 nr_establish_data_link(sk);
714
715 nr->state = NR_STATE_1;
716
717 nr_start_heartbeat(sk);
718
719 /* Now the loop */
720 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700721 err = -EINPROGRESS;
722 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
726 * A Connect Ack with Choke or timeout or failed routing will go to
727 * closed.
728 */
729 if (sk->sk_state == TCP_SYN_SENT) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700730 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000733 prepare_to_wait(sk_sleep(sk), &wait,
YOSHIFUJI Hideaki639fc4c2007-07-19 10:44:32 +0900734 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (sk->sk_state != TCP_SYN_SENT)
736 break;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700737 if (!signal_pending(current)) {
738 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 schedule();
740 lock_sock(sk);
741 continue;
742 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700743 err = -ERESTARTSYS;
744 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000746 finish_wait(sk_sleep(sk), &wait);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700747 if (err)
748 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
751 if (sk->sk_state != TCP_ESTABLISHED) {
752 sock->state = SS_UNCONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700753 err = sock_error(sk); /* Always set at this point */
754 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
757 sock->state = SS_CONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700758
759out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 release_sock(sk);
761
Ralf Baechle75606dc2007-04-20 16:06:45 -0700762 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
David Howellscdfbabf2017-03-09 08:09:05 +0000765static int nr_accept(struct socket *sock, struct socket *newsock, int flags,
766 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct sk_buff *skb;
769 struct sock *newsk;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700770 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct sock *sk;
772 int err = 0;
773
774 if ((sk = sock->sk) == NULL)
775 return -EINVAL;
776
777 lock_sock(sk);
778 if (sk->sk_type != SOCK_SEQPACKET) {
779 err = -EOPNOTSUPP;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700780 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
783 if (sk->sk_state != TCP_LISTEN) {
784 err = -EINVAL;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700785 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
788 /*
789 * The write queue this time is holding sockets ready to use
790 * hooked into the SABM we saved
791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000793 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 skb = skb_dequeue(&sk->sk_receive_queue);
795 if (skb)
796 break;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (flags & O_NONBLOCK) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700799 err = -EWOULDBLOCK;
800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700802 if (!signal_pending(current)) {
803 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 schedule();
805 lock_sock(sk);
806 continue;
807 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700808 err = -ERESTARTSYS;
809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000811 finish_wait(sk_sleep(sk), &wait);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700812 if (err)
813 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 newsk = skb->sk;
David S. Miller7b667672008-06-17 02:36:44 -0700816 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* Now attach up the new socket */
819 kfree_skb(skb);
Ralf Baechle DL5RB18601a72006-07-03 19:31:14 -0700820 sk_acceptq_removed(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Ralf Baechle75606dc2007-04-20 16:06:45 -0700822out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 release_sock(sk);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return err;
826}
827
828static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100829 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
832 struct sock *sk = sock->sk;
833 struct nr_sock *nr = nr_sk(sk);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100834 int uaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Dan Carpenter7a3b6842013-04-22 20:22:51 +0000836 memset(&sax->fsa_ax25, 0, sizeof(struct sockaddr_ax25));
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 lock_sock(sk);
839 if (peer != 0) {
840 if (sk->sk_state != TCP_ESTABLISHED) {
841 release_sock(sk);
842 return -ENOTCONN;
843 }
844 sax->fsa_ax25.sax25_family = AF_NETROM;
845 sax->fsa_ax25.sax25_ndigis = 1;
846 sax->fsa_ax25.sax25_call = nr->user_addr;
Eric Dumazetf6b97b22009-08-06 03:31:07 +0000847 memset(sax->fsa_digipeater, 0, sizeof(sax->fsa_digipeater));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 sax->fsa_digipeater[0] = nr->dest_addr;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100849 uaddr_len = sizeof(struct full_sockaddr_ax25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 } else {
851 sax->fsa_ax25.sax25_family = AF_NETROM;
852 sax->fsa_ax25.sax25_ndigis = 0;
853 sax->fsa_ax25.sax25_call = nr->source_addr;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100854 uaddr_len = sizeof(struct sockaddr_ax25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 release_sock(sk);
857
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100858 return uaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
862{
863 struct sock *sk;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900864 struct sock *make;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 struct nr_sock *nr_make;
866 ax25_address *src, *dest, *user;
867 unsigned short circuit_index, circuit_id;
868 unsigned short peer_circuit_index, peer_circuit_id;
869 unsigned short frametype, flags, window, timeout;
870 int ret;
871
Cong Wangc8c82182019-06-27 14:30:58 -0700872 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /*
875 * skb->data points to the netrom frame start
876 */
877
878 src = (ax25_address *)(skb->data + 0);
879 dest = (ax25_address *)(skb->data + 7);
880
881 circuit_index = skb->data[15];
882 circuit_id = skb->data[16];
883 peer_circuit_index = skb->data[17];
884 peer_circuit_id = skb->data[18];
885 frametype = skb->data[19] & 0x0F;
886 flags = skb->data[19] & 0xF0;
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /*
889 * Check for an incoming IP over NET/ROM frame.
890 */
Ralf Baechle98a82fe2005-08-24 11:35:51 -0700891 if (frametype == NR_PROTOEXT &&
892 circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300894 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 return nr_rx_ip(skb, dev);
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /*
900 * Find an existing socket connection, based on circuit ID, if it's
901 * a Connect Request base it on their circuit ID.
902 *
903 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
904 * circuit that no longer exists at the other end ...
905 */
906
907 sk = NULL;
908
909 if (circuit_index == 0 && circuit_id == 0) {
910 if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
911 sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
912 } else {
913 if (frametype == NR_CONNREQ)
914 sk = nr_find_peer(circuit_index, circuit_id, src);
915 else
916 sk = nr_find_socket(circuit_index, circuit_id);
917 }
918
919 if (sk != NULL) {
Cong Wang7314f542018-12-29 13:56:38 -0800920 bh_lock_sock(sk);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300921 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (frametype == NR_CONNACK && skb->len == 22)
924 nr_sk(sk)->bpqext = 1;
925 else
926 nr_sk(sk)->bpqext = 0;
927
928 ret = nr_process_rx_frame(sk, skb);
929 bh_unlock_sock(sk);
Cong Wang7314f542018-12-29 13:56:38 -0800930 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return ret;
932 }
933
934 /*
935 * Now it should be a CONNREQ.
936 */
937 if (frametype != NR_CONNREQ) {
938 /*
939 * Here it would be nice to be able to send a reset but
Ralf Baechlee21ce8c2005-09-12 14:27:37 -0700940 * NET/ROM doesn't have one. We've tried to extend the protocol
941 * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
942 * apparently kills BPQ boxes... :-(
943 * So now we try to follow the established behaviour of
944 * G8PZT's Xrouter which is sending packets with command type 7
945 * as an extension of the protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
Ralf Baechlee21ce8c2005-09-12 14:27:37 -0700947 if (sysctl_netrom_reset_circuit &&
948 (frametype != NR_RESET || flags != 0))
949 nr_transmit_reset(skb, 1);
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return 0;
952 }
953
954 sk = nr_find_listener(dest);
955
956 user = (ax25_address *)(skb->data + 21);
957
958 if (sk == NULL || sk_acceptq_is_full(sk) ||
959 (make = nr_make_new(sk)) == NULL) {
960 nr_transmit_refusal(skb, 0);
961 if (sk)
Cong Wang7314f542018-12-29 13:56:38 -0800962 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return 0;
964 }
965
Cong Wang7314f542018-12-29 13:56:38 -0800966 bh_lock_sock(sk);
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 window = skb->data[20];
969
970 skb->sk = make;
Cong Wangc8c82182019-06-27 14:30:58 -0700971 skb->destructor = sock_efree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 make->sk_state = TCP_ESTABLISHED;
973
974 /* Fill in his circuit details */
975 nr_make = nr_sk(make);
976 nr_make->source_addr = *dest;
977 nr_make->dest_addr = *src;
978 nr_make->user_addr = *user;
979
980 nr_make->your_index = circuit_index;
981 nr_make->your_id = circuit_id;
982
983 bh_unlock_sock(sk);
984 circuit = nr_find_next_circuit();
985 bh_lock_sock(sk);
986
987 nr_make->my_index = circuit / 256;
988 nr_make->my_id = circuit % 256;
989
990 circuit++;
991
992 /* Window negotiation */
993 if (window < nr_make->window)
994 nr_make->window = window;
995
996 /* L4 timeout negotiation */
997 if (skb->len == 37) {
998 timeout = skb->data[36] * 256 + skb->data[35];
999 if (timeout * HZ < nr_make->t1)
1000 nr_make->t1 = timeout * HZ;
1001 nr_make->bpqext = 1;
1002 } else {
1003 nr_make->bpqext = 0;
1004 }
1005
1006 nr_write_internal(make, NR_CONNACK);
1007
1008 nr_make->condition = 0x00;
1009 nr_make->vs = 0;
1010 nr_make->va = 0;
1011 nr_make->vr = 0;
1012 nr_make->vl = 0;
1013 nr_make->state = NR_STATE_3;
Ralf Baechle DL5RB18601a72006-07-03 19:31:14 -07001014 sk_acceptq_added(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 skb_queue_head(&sk->sk_receive_queue, skb);
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04001018 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 bh_unlock_sock(sk);
Cong Wang7314f542018-12-29 13:56:38 -08001021 sock_put(sk);
Ralf Baechle5cc29e32006-07-10 16:23:21 -07001022
1023 nr_insert_socket(make);
1024
1025 nr_start_heartbeat(make);
1026 nr_start_idletimer(make);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return 1;
1029}
1030
Ying Xue1b784142015-03-02 15:37:48 +08001031static int nr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 struct sock *sk = sock->sk;
1034 struct nr_sock *nr = nr_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001035 DECLARE_SOCKADDR(struct sockaddr_ax25 *, usax, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 int err;
1037 struct sockaddr_ax25 sax;
1038 struct sk_buff *skb;
1039 unsigned char *asmptr;
1040 int size;
1041
1042 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1043 return -EINVAL;
1044
1045 lock_sock(sk);
1046 if (sock_flag(sk, SOCK_ZAPPED)) {
1047 err = -EADDRNOTAVAIL;
1048 goto out;
1049 }
1050
1051 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1052 send_sig(SIGPIPE, current, 0);
1053 err = -EPIPE;
1054 goto out;
1055 }
1056
1057 if (nr->device == NULL) {
1058 err = -ENETUNREACH;
1059 goto out;
1060 }
1061
1062 if (usax) {
1063 if (msg->msg_namelen < sizeof(sax)) {
1064 err = -EINVAL;
1065 goto out;
1066 }
1067 sax = *usax;
1068 if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
1069 err = -EISCONN;
1070 goto out;
1071 }
1072 if (sax.sax25_family != AF_NETROM) {
1073 err = -EINVAL;
1074 goto out;
1075 }
1076 } else {
1077 if (sk->sk_state != TCP_ESTABLISHED) {
1078 err = -ENOTCONN;
1079 goto out;
1080 }
1081 sax.sax25_family = AF_NETROM;
1082 sax.sax25_call = nr->dest_addr;
1083 }
1084
Alan Cox83e0bbc2009-03-27 00:28:21 -07001085 /* Build a packet - the conventional user limit is 236 bytes. We can
1086 do ludicrously large NetROM frames but must not overflow */
Jean Delvarecc29c702009-04-22 00:49:51 -07001087 if (len > 65536) {
1088 err = -EMSGSIZE;
1089 goto out;
1090 }
Alan Cox83e0bbc2009-03-27 00:28:21 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1093
1094 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1095 goto out;
1096
1097 skb_reserve(skb, size - len);
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001098 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /*
1101 * Push down the NET/ROM header
1102 */
1103
1104 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 /* Build a NET/ROM Transport header */
1107
1108 *asmptr++ = nr->your_index;
1109 *asmptr++ = nr->your_id;
1110 *asmptr++ = 0; /* To be filled in later */
1111 *asmptr++ = 0; /* Ditto */
1112 *asmptr++ = NR_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 /*
1115 * Put the data on the end
1116 */
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001117 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /* User data follows immediately after the NET/ROM transport header */
Al Viro6ce8e9c2014-04-06 21:25:44 -04001120 if (memcpy_from_msg(skb_transport_header(skb), msg, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 kfree_skb(skb);
1122 err = -EFAULT;
1123 goto out;
1124 }
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (sk->sk_state != TCP_ESTABLISHED) {
1127 kfree_skb(skb);
1128 err = -ENOTCONN;
1129 goto out;
1130 }
1131
1132 nr_output(sk, skb); /* Shove it onto the queue */
1133
1134 err = len;
1135out:
1136 release_sock(sk);
1137 return err;
1138}
1139
Ying Xue1b784142015-03-02 15:37:48 +08001140static int nr_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1141 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001144 DECLARE_SOCKADDR(struct sockaddr_ax25 *, sax, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 size_t copied;
1146 struct sk_buff *skb;
1147 int er;
1148
1149 /*
1150 * This works for seqpacket too. The receiver has ordered the queue for
1151 * us! We do one quick check first though
1152 */
1153
1154 lock_sock(sk);
1155 if (sk->sk_state != TCP_ESTABLISHED) {
1156 release_sock(sk);
1157 return -ENOTCONN;
1158 }
1159
1160 /* Now we can treat all alike */
1161 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
1162 release_sock(sk);
1163 return er;
1164 }
1165
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001166 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 copied = skb->len;
1168
1169 if (copied > size) {
1170 copied = size;
1171 msg->msg_flags |= MSG_TRUNC;
1172 }
1173
David S. Miller51f3d022014-11-05 16:46:40 -05001174 er = skb_copy_datagram_msg(skb, 0, msg, copied);
Alan Cox6cf5c952012-09-04 04:13:18 +00001175 if (er < 0) {
1176 skb_free_datagram(sk, skb);
1177 release_sock(sk);
1178 return er;
1179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 if (sax != NULL) {
Wei Yongjunc802d752013-04-09 10:07:19 +08001182 memset(sax, 0, sizeof(*sax));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 sax->sax25_family = AF_NETROM;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001184 skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call,
1185 AX25_ADDR_LEN);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001186 msg->msg_namelen = sizeof(*sax);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
David S. Miller80e20f62009-03-27 17:22:55 -07001189 skb_free_datagram(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 release_sock(sk);
1192 return copied;
1193}
1194
1195
1196static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1197{
1198 struct sock *sk = sock->sk;
1199 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 switch (cmd) {
1202 case TIOCOUTQ: {
1203 long amount;
Christoph Hellwig5ff76302006-01-03 14:14:46 -08001204
1205 lock_sock(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07001206 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (amount < 0)
1208 amount = 0;
1209 release_sock(sk);
1210 return put_user(amount, (int __user *)argp);
1211 }
1212
1213 case TIOCINQ: {
1214 struct sk_buff *skb;
1215 long amount = 0L;
Christoph Hellwig5ff76302006-01-03 14:14:46 -08001216
1217 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 /* These two are safe on a single CPU system as only user tasks fiddle here */
1219 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1220 amount = skb->len;
1221 release_sock(sk);
1222 return put_user(amount, (int __user *)argp);
1223 }
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 case SIOCGIFADDR:
1226 case SIOCSIFADDR:
1227 case SIOCGIFDSTADDR:
1228 case SIOCSIFDSTADDR:
1229 case SIOCGIFBRDADDR:
1230 case SIOCSIFBRDADDR:
1231 case SIOCGIFNETMASK:
1232 case SIOCSIFNETMASK:
1233 case SIOCGIFMETRIC:
1234 case SIOCSIFMETRIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return -EINVAL;
1236
1237 case SIOCADDRT:
1238 case SIOCDELRT:
1239 case SIOCNRDECOBS:
Ralf Baechle0f20f5a2011-11-24 23:54:10 +00001240 if (!capable(CAP_NET_ADMIN))
1241 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return nr_rt_ioctl(cmd, argp);
1243
1244 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001245 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 return 0;
1249}
1250
1251#ifdef CONFIG_PROC_FS
1252
1253static void *nr_info_start(struct seq_file *seq, loff_t *pos)
1254{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 spin_lock_bh(&nr_list_lock);
Li Zefan90dd7f52010-02-08 23:19:42 +00001256 return seq_hlist_start_head(&nr_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
1260{
Li Zefan90dd7f52010-02-08 23:19:42 +00001261 return seq_hlist_next(v, &nr_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264static void nr_info_stop(struct seq_file *seq, void *v)
1265{
1266 spin_unlock_bh(&nr_list_lock);
1267}
1268
1269static int nr_info_show(struct seq_file *seq, void *v)
1270{
Li Zefan90dd7f52010-02-08 23:19:42 +00001271 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 struct net_device *dev;
1273 struct nr_sock *nr;
1274 const char *devname;
Ralf Baechlef75268c2005-09-06 15:49:39 -07001275 char buf[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 if (v == SEQ_START_TOKEN)
1278 seq_puts(seq,
1279"user_addr dest_node src_node dev my your st vs vr va t1 t2 t4 idle n2 wnd Snd-Q Rcv-Q inode\n");
1280
1281 else {
1282
1283 bh_lock_sock(s);
1284 nr = nr_sk(s);
1285
1286 if ((dev = nr->device) == NULL)
1287 devname = "???";
1288 else
1289 devname = dev->name;
1290
Ralf Baechlef75268c2005-09-06 15:49:39 -07001291 seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr));
1292 seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr));
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001293 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294"%-9s %-3s %02X/%02X %02X/%02X %2d %3d %3d %3d %3lu/%03lu %2lu/%02lu %3lu/%03lu %3lu/%03lu %2d/%02d %3d %5d %5d %ld\n",
Ralf Baechlef75268c2005-09-06 15:49:39 -07001295 ax2asc(buf, &nr->source_addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 devname,
1297 nr->my_index,
1298 nr->my_id,
1299 nr->your_index,
1300 nr->your_id,
1301 nr->state,
1302 nr->vs,
1303 nr->vr,
1304 nr->va,
1305 ax25_display_timer(&nr->t1timer) / HZ,
1306 nr->t1 / HZ,
1307 ax25_display_timer(&nr->t2timer) / HZ,
1308 nr->t2 / HZ,
1309 ax25_display_timer(&nr->t4timer) / HZ,
1310 nr->t4 / HZ,
1311 ax25_display_timer(&nr->idletimer) / (60 * HZ),
1312 nr->idle / (60 * HZ),
1313 nr->n2count,
1314 nr->n2,
1315 nr->window,
Eric Dumazet31e6d362009-06-17 19:05:41 -07001316 sk_wmem_alloc_get(s),
1317 sk_rmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1319
1320 bh_unlock_sock(s);
1321 }
1322 return 0;
1323}
1324
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001325static const struct seq_operations nr_info_seqops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 .start = nr_info_start,
1327 .next = nr_info_next,
1328 .stop = nr_info_stop,
1329 .show = nr_info_show,
1330};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331#endif /* CONFIG_PROC_FS */
1332
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001333static const struct net_proto_family nr_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 .family = PF_NETROM,
1335 .create = nr_create,
1336 .owner = THIS_MODULE,
1337};
1338
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001339static const struct proto_ops nr_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 .family = PF_NETROM,
1341 .owner = THIS_MODULE,
1342 .release = nr_release,
1343 .bind = nr_bind,
1344 .connect = nr_connect,
1345 .socketpair = sock_no_socketpair,
1346 .accept = nr_accept,
1347 .getname = nr_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001348 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 .ioctl = nr_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001350 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 .listen = nr_listen,
1352 .shutdown = sock_no_shutdown,
1353 .setsockopt = nr_setsockopt,
1354 .getsockopt = nr_getsockopt,
1355 .sendmsg = nr_sendmsg,
1356 .recvmsg = nr_recvmsg,
1357 .mmap = sock_no_mmap,
1358 .sendpage = sock_no_sendpage,
1359};
1360
1361static struct notifier_block nr_dev_notifier = {
1362 .notifier_call = nr_device_event,
1363};
1364
1365static struct net_device **dev_nr;
1366
Ralf Baechle8d5cf592006-12-14 15:50:01 -08001367static struct ax25_protocol nr_pid = {
1368 .pid = AX25_P_NETROM,
1369 .func = nr_route_frame
1370};
1371
Ralf Baechlea4282712006-12-14 15:51:23 -08001372static struct ax25_linkfail nr_linkfail_notifier = {
1373 .func = nr_link_failed,
1374};
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376static int __init nr_proto_init(void)
1377{
1378 int i;
1379 int rc = proto_register(&nr_proto, 0);
1380
YueHaibingd3706562019-04-09 19:53:55 +08001381 if (rc)
1382 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
YueHaibingd3706562019-04-09 19:53:55 +08001385 pr_err("NET/ROM: %s - nr_ndevs parameter too large\n",
1386 __func__);
1387 rc = -EINVAL;
1388 goto unregister_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
Kees Cook6396bb22018-06-12 14:03:40 -07001391 dev_nr = kcalloc(nr_ndevs, sizeof(struct net_device *), GFP_KERNEL);
YueHaibingd3706562019-04-09 19:53:55 +08001392 if (!dev_nr) {
1393 pr_err("NET/ROM: %s - unable to allocate device array\n",
1394 __func__);
1395 rc = -ENOMEM;
1396 goto unregister_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 for (i = 0; i < nr_ndevs; i++) {
1400 char name[IFNAMSIZ];
1401 struct net_device *dev;
1402
1403 sprintf(name, "nr%d", i);
Tom Gundersenc835a672014-07-14 16:37:24 +02001404 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, nr_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (!dev) {
YueHaibingd3706562019-04-09 19:53:55 +08001406 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 goto fail;
1408 }
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 dev->base_addr = i;
YueHaibingd3706562019-04-09 19:53:55 +08001411 rc = register_netdev(dev);
1412 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 free_netdev(dev);
1414 goto fail;
1415 }
David S. Millerc773e842008-07-08 23:13:53 -07001416 nr_set_lockdep_key(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 dev_nr[i] = dev;
1418 }
1419
YueHaibingd3706562019-04-09 19:53:55 +08001420 rc = sock_register(&nr_family_ops);
1421 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 goto fail;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001423
YueHaibingd3706562019-04-09 19:53:55 +08001424 rc = register_netdevice_notifier(&nr_dev_notifier);
1425 if (rc)
1426 goto out_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Ralf Baechle8d5cf592006-12-14 15:50:01 -08001428 ax25_register_pid(&nr_pid);
Ralf Baechlea4282712006-12-14 15:51:23 -08001429 ax25_linkfail_register(&nr_linkfail_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431#ifdef CONFIG_SYSCTL
YueHaibingd3706562019-04-09 19:53:55 +08001432 rc = nr_register_sysctl();
1433 if (rc)
1434 goto out_sysctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435#endif
1436
1437 nr_loopback_init();
1438
YueHaibingd3706562019-04-09 19:53:55 +08001439 rc = -ENOMEM;
1440 if (!proc_create_seq("nr", 0444, init_net.proc_net, &nr_info_seqops))
1441 goto proc_remove1;
1442 if (!proc_create_seq("nr_neigh", 0444, init_net.proc_net,
1443 &nr_neigh_seqops))
1444 goto proc_remove2;
1445 if (!proc_create_seq("nr_nodes", 0444, init_net.proc_net,
1446 &nr_node_seqops))
1447 goto proc_remove3;
1448
1449 return 0;
1450
1451proc_remove3:
1452 remove_proc_entry("nr_neigh", init_net.proc_net);
1453proc_remove2:
1454 remove_proc_entry("nr", init_net.proc_net);
1455proc_remove1:
1456
1457 nr_loopback_clear();
1458 nr_rt_free();
1459
1460#ifdef CONFIG_SYSCTL
1461 nr_unregister_sysctl();
1462out_sysctl:
1463#endif
1464 ax25_linkfail_release(&nr_linkfail_notifier);
1465 ax25_protocol_release(AX25_P_NETROM);
1466 unregister_netdevice_notifier(&nr_dev_notifier);
1467out_sock:
1468 sock_unregister(PF_NETROM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469fail:
1470 while (--i >= 0) {
1471 unregister_netdev(dev_nr[i]);
1472 free_netdev(dev_nr[i]);
1473 }
1474 kfree(dev_nr);
YueHaibingd3706562019-04-09 19:53:55 +08001475unregister_proto:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 proto_unregister(&nr_proto);
YueHaibingd3706562019-04-09 19:53:55 +08001477 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480module_init(nr_proto_init);
1481
1482module_param(nr_ndevs, int, 0);
1483MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1484
1485MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1486MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1487MODULE_LICENSE("GPL");
1488MODULE_ALIAS_NETPROTO(PF_NETROM);
1489
1490static void __exit nr_exit(void)
1491{
1492 int i;
1493
Gao fengece31ff2013-02-18 01:34:56 +00001494 remove_proc_entry("nr", init_net.proc_net);
1495 remove_proc_entry("nr_neigh", init_net.proc_net);
1496 remove_proc_entry("nr_nodes", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 nr_loopback_clear();
1498
1499 nr_rt_free();
1500
1501#ifdef CONFIG_SYSCTL
1502 nr_unregister_sysctl();
1503#endif
1504
Ralf Baechlea4282712006-12-14 15:51:23 -08001505 ax25_linkfail_release(&nr_linkfail_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 ax25_protocol_release(AX25_P_NETROM);
1507
1508 unregister_netdevice_notifier(&nr_dev_notifier);
1509
1510 sock_unregister(PF_NETROM);
1511
1512 for (i = 0; i < nr_ndevs; i++) {
1513 struct net_device *dev = dev_nr[i];
1514 if (dev) {
1515 unregister_netdev(dev);
1516 free_netdev(dev);
1517 }
1518 }
1519
1520 kfree(dev_nr);
1521 proto_unregister(&nr_proto);
1522}
1523module_exit(nr_exit);