blob: eccc7d366e17f6eb52129908ee02f521da8bcb96 [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/*
Cong Wang1a33e102020-05-02 22:22:19 -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;
73
74static void nr_set_lockdep_one(struct net_device *dev,
75 struct netdev_queue *txq,
76 void *_unused)
77{
78 lockdep_set_class(&txq->_xmit_lock, &nr_netdev_xmit_lock_key);
79}
80
81static void nr_set_lockdep_key(struct net_device *dev)
82{
83 netdev_for_each_tx_queue(dev, nr_set_lockdep_one, NULL);
84}
85
86/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Socket removal during an interrupt is now safe.
88 */
89static void nr_remove_socket(struct sock *sk)
90{
91 spin_lock_bh(&nr_list_lock);
92 sk_del_node_init(sk);
93 spin_unlock_bh(&nr_list_lock);
94}
95
96/*
97 * Kill all bound sockets on a dropped device.
98 */
99static void nr_kill_by_device(struct net_device *dev)
100{
101 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 spin_lock_bh(&nr_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800104 sk_for_each(s, &nr_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (nr_sk(s)->device == dev)
106 nr_disconnect(s, ENETUNREACH);
107 spin_unlock_bh(&nr_list_lock);
108}
109
110/*
111 * Handle device status changes.
112 */
113static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
114{
Jiri Pirko351638e2013-05-28 01:30:21 +0000115 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700117 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200118 return NOTIFY_DONE;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (event != NETDEV_DOWN)
121 return NOTIFY_DONE;
122
123 nr_kill_by_device(dev);
124 nr_rt_device_down(dev);
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return NOTIFY_DONE;
127}
128
129/*
130 * Add a socket to the bound sockets list.
131 */
132static void nr_insert_socket(struct sock *sk)
133{
134 spin_lock_bh(&nr_list_lock);
135 sk_add_node(sk, &nr_list);
136 spin_unlock_bh(&nr_list_lock);
137}
138
139/*
140 * Find a socket that wants to accept the Connect Request we just
141 * received.
142 */
143static struct sock *nr_find_listener(ax25_address *addr)
144{
145 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 spin_lock_bh(&nr_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800148 sk_for_each(s, &nr_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
150 s->sk_state == TCP_LISTEN) {
Cong Wang7314f542018-12-29 13:56:38 -0800151 sock_hold(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto found;
153 }
154 s = NULL;
155found:
156 spin_unlock_bh(&nr_list_lock);
157 return s;
158}
159
160/*
161 * Find a connected NET/ROM socket given my circuit IDs.
162 */
163static struct sock *nr_find_socket(unsigned char index, unsigned char id)
164{
165 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 spin_lock_bh(&nr_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800168 sk_for_each(s, &nr_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct nr_sock *nr = nr_sk(s);
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (nr->my_index == index && nr->my_id == id) {
Cong Wang7314f542018-12-29 13:56:38 -0800172 sock_hold(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 goto found;
174 }
175 }
176 s = NULL;
177found:
178 spin_unlock_bh(&nr_list_lock);
179 return s;
180}
181
182/*
183 * Find a connected NET/ROM socket given their circuit IDs.
184 */
185static struct sock *nr_find_peer(unsigned char index, unsigned char id,
186 ax25_address *dest)
187{
188 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 spin_lock_bh(&nr_list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800191 sk_for_each(s, &nr_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct nr_sock *nr = nr_sk(s);
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (nr->your_index == index && nr->your_id == id &&
195 !ax25cmp(&nr->dest_addr, dest)) {
Cong Wang7314f542018-12-29 13:56:38 -0800196 sock_hold(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto found;
198 }
199 }
200 s = NULL;
201found:
202 spin_unlock_bh(&nr_list_lock);
203 return s;
204}
205
206/*
207 * Find next free circuit ID.
208 */
209static unsigned short nr_find_next_circuit(void)
210{
211 unsigned short id = circuit;
212 unsigned char i, j;
213 struct sock *sk;
214
215 for (;;) {
216 i = id / 256;
217 j = id % 256;
218
219 if (i != 0 && j != 0) {
220 if ((sk=nr_find_socket(i, j)) == NULL)
221 break;
Cong Wang7314f542018-12-29 13:56:38 -0800222 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224
225 id++;
226 }
227
228 return id;
229}
230
231/*
232 * Deferred destroy.
233 */
234void nr_destroy_socket(struct sock *);
235
236/*
237 * Handler for deferred kills.
238 */
Kees Cook99767f22017-10-16 17:29:36 -0700239static void nr_destroy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Kees Cook99767f22017-10-16 17:29:36 -0700241 struct sock *sk = from_timer(sk, t, sk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 bh_lock_sock(sk);
243 sock_hold(sk);
244 nr_destroy_socket(sk);
245 bh_unlock_sock(sk);
246 sock_put(sk);
247}
248
249/*
250 * This is called from user mode and the timers. Thus it protects itself
251 * against interrupt users but doesn't worry about being called during
252 * work. Once it is removed from the queue no interrupt or bottom half
253 * will touch it and we are (fairly 8-) ) safe.
254 */
255void nr_destroy_socket(struct sock *sk)
256{
257 struct sk_buff *skb;
258
259 nr_remove_socket(sk);
260
261 nr_stop_heartbeat(sk);
262 nr_stop_t1timer(sk);
263 nr_stop_t2timer(sk);
264 nr_stop_t4timer(sk);
265 nr_stop_idletimer(sk);
266
267 nr_clear_queues(sk); /* Flush the queues */
268
269 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
270 if (skb->sk != sk) { /* A pending connection */
271 /* Queue the unaccepted socket for death */
272 sock_set_flag(skb->sk, SOCK_DEAD);
273 nr_start_heartbeat(skb->sk);
274 nr_sk(skb->sk)->state = NR_STATE_0;
275 }
276
277 kfree_skb(skb);
278 }
279
Eric Dumazetc5640392009-06-16 10:12:03 +0000280 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* Defer: outstanding buffers */
Kees Cook841b86f2017-10-23 09:40:42 +0200282 sk->sk_timer.function = nr_destroy_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 sk->sk_timer.expires = jiffies + 2 * HZ;
284 add_timer(&sk->sk_timer);
285 } else
286 sock_put(sk);
287}
288
289/*
290 * Handling for system calls applied via the various interfaces to a
291 * NET/ROM socket object.
292 */
293
294static int nr_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700295 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct sock *sk = sock->sk;
298 struct nr_sock *nr = nr_sk(sk);
Xi Wang32288eb2011-12-27 09:44:53 +0000299 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (level != SOL_NETROM)
302 return -ENOPROTOOPT;
303
Xi Wang32288eb2011-12-27 09:44:53 +0000304 if (optlen < sizeof(unsigned int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return -EINVAL;
306
Xi Wang32288eb2011-12-27 09:44:53 +0000307 if (get_user(opt, (unsigned int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -EFAULT;
309
310 switch (optname) {
311 case NETROM_T1:
Xi Wang32288eb2011-12-27 09:44:53 +0000312 if (opt < 1 || opt > ULONG_MAX / HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -EINVAL;
314 nr->t1 = opt * HZ;
315 return 0;
316
317 case NETROM_T2:
Xi Wang32288eb2011-12-27 09:44:53 +0000318 if (opt < 1 || opt > ULONG_MAX / HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return -EINVAL;
320 nr->t2 = opt * HZ;
321 return 0;
322
323 case NETROM_N2:
324 if (opt < 1 || opt > 31)
325 return -EINVAL;
326 nr->n2 = opt;
327 return 0;
328
329 case NETROM_T4:
Xi Wang32288eb2011-12-27 09:44:53 +0000330 if (opt < 1 || opt > ULONG_MAX / HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return -EINVAL;
332 nr->t4 = opt * HZ;
333 return 0;
334
335 case NETROM_IDLE:
Xi Wang32288eb2011-12-27 09:44:53 +0000336 if (opt > ULONG_MAX / (60 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return -EINVAL;
338 nr->idle = opt * 60 * HZ;
339 return 0;
340
341 default:
342 return -ENOPROTOOPT;
343 }
344}
345
346static int nr_getsockopt(struct socket *sock, int level, int optname,
347 char __user *optval, int __user *optlen)
348{
349 struct sock *sk = sock->sk;
350 struct nr_sock *nr = nr_sk(sk);
351 int val = 0;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900352 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (level != SOL_NETROM)
355 return -ENOPROTOOPT;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (get_user(len, optlen))
358 return -EFAULT;
359
360 if (len < 0)
361 return -EINVAL;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 switch (optname) {
364 case NETROM_T1:
365 val = nr->t1 / HZ;
366 break;
367
368 case NETROM_T2:
369 val = nr->t2 / HZ;
370 break;
371
372 case NETROM_N2:
373 val = nr->n2;
374 break;
375
376 case NETROM_T4:
377 val = nr->t4 / HZ;
378 break;
379
380 case NETROM_IDLE:
381 val = nr->idle / (60 * HZ);
382 break;
383
384 default:
385 return -ENOPROTOOPT;
386 }
387
388 len = min_t(unsigned int, len, sizeof(int));
389
390 if (put_user(len, optlen))
391 return -EFAULT;
392
393 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
394}
395
396static int nr_listen(struct socket *sock, int backlog)
397{
398 struct sock *sk = sock->sk;
399
400 lock_sock(sk);
401 if (sk->sk_state != TCP_LISTEN) {
402 memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
403 sk->sk_max_ack_backlog = backlog;
404 sk->sk_state = TCP_LISTEN;
405 release_sock(sk);
406 return 0;
407 }
408 release_sock(sk);
409
410 return -EOPNOTSUPP;
411}
412
413static struct proto nr_proto = {
414 .name = "NETROM",
415 .owner = THIS_MODULE,
416 .obj_size = sizeof(struct nr_sock),
417};
418
Eric Paris3f378b62009-11-05 22:18:14 -0800419static int nr_create(struct net *net, struct socket *sock, int protocol,
420 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct sock *sk;
423 struct nr_sock *nr;
424
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800425 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700426 return -EAFNOSUPPORT;
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (sock->type != SOCK_SEQPACKET || protocol != 0)
429 return -ESOCKTNOSUPPORT;
430
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500431 sk = sk_alloc(net, PF_NETROM, GFP_ATOMIC, &nr_proto, kern);
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700432 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -ENOMEM;
434
435 nr = nr_sk(sk);
436
437 sock_init_data(sock, sk);
438
439 sock->ops = &nr_proto_ops;
440 sk->sk_protocol = protocol;
441
442 skb_queue_head_init(&nr->ack_queue);
443 skb_queue_head_init(&nr->reseq_queue);
444 skb_queue_head_init(&nr->frag_queue);
445
446 nr_init_timers(sk);
447
Ralf Baechle4d8937d2006-05-03 23:27:47 -0700448 nr->t1 =
449 msecs_to_jiffies(sysctl_netrom_transport_timeout);
450 nr->t2 =
451 msecs_to_jiffies(sysctl_netrom_transport_acknowledge_delay);
452 nr->n2 =
453 msecs_to_jiffies(sysctl_netrom_transport_maximum_tries);
454 nr->t4 =
455 msecs_to_jiffies(sysctl_netrom_transport_busy_delay);
456 nr->idle =
457 msecs_to_jiffies(sysctl_netrom_transport_no_activity_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 nr->window = sysctl_netrom_transport_requested_window_size;
459
460 nr->bpqext = 1;
461 nr->state = NR_STATE_0;
462
463 return 0;
464}
465
466static struct sock *nr_make_new(struct sock *osk)
467{
468 struct sock *sk;
469 struct nr_sock *nr, *onr;
470
471 if (osk->sk_type != SOCK_SEQPACKET)
472 return NULL;
473
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500474 sk = sk_alloc(sock_net(osk), PF_NETROM, GFP_ATOMIC, osk->sk_prot, 0);
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700475 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return NULL;
477
478 nr = nr_sk(sk);
479
480 sock_init_data(NULL, sk);
481
482 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 sk->sk_priority = osk->sk_priority;
484 sk->sk_protocol = osk->sk_protocol;
485 sk->sk_rcvbuf = osk->sk_rcvbuf;
486 sk->sk_sndbuf = osk->sk_sndbuf;
487 sk->sk_state = TCP_ESTABLISHED;
Ralf Baechle53b924b2005-08-23 10:11:30 -0700488 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 skb_queue_head_init(&nr->ack_queue);
491 skb_queue_head_init(&nr->reseq_queue);
492 skb_queue_head_init(&nr->frag_queue);
493
494 nr_init_timers(sk);
495
496 onr = nr_sk(osk);
497
498 nr->t1 = onr->t1;
499 nr->t2 = onr->t2;
500 nr->n2 = onr->n2;
501 nr->t4 = onr->t4;
502 nr->idle = onr->idle;
503 nr->window = onr->window;
504
505 nr->device = onr->device;
506 nr->bpqext = onr->bpqext;
507
508 return sk;
509}
510
511static int nr_release(struct socket *sock)
512{
513 struct sock *sk = sock->sk;
514 struct nr_sock *nr;
515
516 if (sk == NULL) return 0;
517
518 sock_hold(sk);
Jarek Poplawski859f4c72008-10-06 12:54:57 -0700519 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 lock_sock(sk);
521 nr = nr_sk(sk);
522
523 switch (nr->state) {
524 case NR_STATE_0:
525 case NR_STATE_1:
526 case NR_STATE_2:
527 nr_disconnect(sk, 0);
528 nr_destroy_socket(sk);
529 break;
530
531 case NR_STATE_3:
532 nr_clear_queues(sk);
533 nr->n2count = 0;
534 nr_write_internal(sk, NR_DISCREQ);
535 nr_start_t1timer(sk);
536 nr_stop_t2timer(sk);
537 nr_stop_t4timer(sk);
538 nr_stop_idletimer(sk);
539 nr->state = NR_STATE_2;
540 sk->sk_state = TCP_CLOSE;
541 sk->sk_shutdown |= SEND_SHUTDOWN;
542 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 sock_set_flag(sk, SOCK_DESTROY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545
546 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548 }
549
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900550 sock->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 release_sock(sk);
552 sock_put(sk);
553
554 return 0;
555}
556
557static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
558{
559 struct sock *sk = sock->sk;
560 struct nr_sock *nr = nr_sk(sk);
561 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
562 struct net_device *dev;
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700563 ax25_uid_assoc *user;
564 ax25_address *source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 lock_sock(sk);
567 if (!sock_flag(sk, SOCK_ZAPPED)) {
568 release_sock(sk);
569 return -EINVAL;
570 }
571 if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
572 release_sock(sk);
573 return -EINVAL;
574 }
575 if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
576 release_sock(sk);
577 return -EINVAL;
578 }
579 if (addr->fsa_ax25.sax25_family != AF_NETROM) {
580 release_sock(sk);
581 return -EINVAL;
582 }
583 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 release_sock(sk);
585 return -EADDRNOTAVAIL;
586 }
587
588 /*
589 * Only the super user can set an arbitrary user callsign.
590 */
591 if (addr->fsa_ax25.sax25_ndigis == 1) {
592 if (!capable(CAP_NET_BIND_SERVICE)) {
593 dev_put(dev);
594 release_sock(sk);
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +0000595 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597 nr->user_addr = addr->fsa_digipeater[0];
598 nr->source_addr = addr->fsa_ax25.sax25_call;
599 } else {
600 source = &addr->fsa_ax25.sax25_call;
601
David Howellsba95b232008-11-14 10:39:08 +1100602 user = ax25_findbyuid(current_euid());
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700603 if (user) {
604 nr->user_addr = user->call;
605 ax25_uid_put(user);
606 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
608 release_sock(sk);
609 dev_put(dev);
610 return -EPERM;
611 }
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700612 nr->user_addr = *source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 nr->source_addr = *source;
616 }
617
618 nr->device = dev;
619 nr_insert_socket(sk);
620
621 sock_reset_flag(sk, SOCK_ZAPPED);
622 dev_put(dev);
623 release_sock(sk);
Ralf Baechle8849b722011-04-14 00:20:07 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return 0;
626}
627
628static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
629 int addr_len, int flags)
630{
631 struct sock *sk = sock->sk;
632 struct nr_sock *nr = nr_sk(sk);
633 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700634 ax25_address *source = NULL;
635 ax25_uid_assoc *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct net_device *dev;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700637 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 lock_sock(sk);
640 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
641 sock->state = SS_CONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700642 goto out_release; /* Connect completed during a ERESTARTSYS event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
646 sock->state = SS_UNCONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700647 err = -ECONNREFUSED;
648 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
651 if (sk->sk_state == TCP_ESTABLISHED) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700652 err = -EISCONN; /* No reconnect on a seqpacket socket */
653 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900656 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 sock->state = SS_UNCONNECTED;
658
659 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700660 err = -EINVAL;
661 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 if (addr->sax25_family != AF_NETROM) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700664 err = -EINVAL;
665 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
668 sock_reset_flag(sk, SOCK_ZAPPED);
669
670 if ((dev = nr_dev_first()) == NULL) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700671 err = -ENETUNREACH;
672 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674 source = (ax25_address *)dev->dev_addr;
675
David Howellsba95b232008-11-14 10:39:08 +1100676 user = ax25_findbyuid(current_euid());
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700677 if (user) {
678 nr->user_addr = user->call;
679 ax25_uid_put(user);
680 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
682 dev_put(dev);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700683 err = -EPERM;
684 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700686 nr->user_addr = *source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 nr->source_addr = *source;
690 nr->device = dev;
691
692 dev_put(dev);
693 nr_insert_socket(sk); /* Finish the bind */
694 }
695
696 nr->dest_addr = addr->sax25_call;
697
698 release_sock(sk);
699 circuit = nr_find_next_circuit();
700 lock_sock(sk);
701
702 nr->my_index = circuit / 256;
703 nr->my_id = circuit % 256;
704
705 circuit++;
706
707 /* Move to connecting socket, start sending Connect Requests */
708 sock->state = SS_CONNECTING;
709 sk->sk_state = TCP_SYN_SENT;
710
711 nr_establish_data_link(sk);
712
713 nr->state = NR_STATE_1;
714
715 nr_start_heartbeat(sk);
716
717 /* Now the loop */
718 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700719 err = -EINPROGRESS;
720 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
724 * A Connect Ack with Choke or timeout or failed routing will go to
725 * closed.
726 */
727 if (sk->sk_state == TCP_SYN_SENT) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700728 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000731 prepare_to_wait(sk_sleep(sk), &wait,
YOSHIFUJI Hideaki639fc4c2007-07-19 10:44:32 +0900732 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (sk->sk_state != TCP_SYN_SENT)
734 break;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700735 if (!signal_pending(current)) {
736 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 schedule();
738 lock_sock(sk);
739 continue;
740 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700741 err = -ERESTARTSYS;
742 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000744 finish_wait(sk_sleep(sk), &wait);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700745 if (err)
746 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 if (sk->sk_state != TCP_ESTABLISHED) {
750 sock->state = SS_UNCONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700751 err = sock_error(sk); /* Always set at this point */
752 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 sock->state = SS_CONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700756
757out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 release_sock(sk);
759
Ralf Baechle75606dc2007-04-20 16:06:45 -0700760 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
David Howellscdfbabf2017-03-09 08:09:05 +0000763static int nr_accept(struct socket *sock, struct socket *newsock, int flags,
764 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct sk_buff *skb;
767 struct sock *newsk;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700768 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct sock *sk;
770 int err = 0;
771
772 if ((sk = sock->sk) == NULL)
773 return -EINVAL;
774
775 lock_sock(sk);
776 if (sk->sk_type != SOCK_SEQPACKET) {
777 err = -EOPNOTSUPP;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700778 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
781 if (sk->sk_state != TCP_LISTEN) {
782 err = -EINVAL;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700783 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
786 /*
787 * The write queue this time is holding sockets ready to use
788 * hooked into the SABM we saved
789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000791 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 skb = skb_dequeue(&sk->sk_receive_queue);
793 if (skb)
794 break;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (flags & O_NONBLOCK) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700797 err = -EWOULDBLOCK;
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700800 if (!signal_pending(current)) {
801 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 schedule();
803 lock_sock(sk);
804 continue;
805 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700806 err = -ERESTARTSYS;
807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000809 finish_wait(sk_sleep(sk), &wait);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700810 if (err)
811 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 newsk = skb->sk;
David S. Miller7b667672008-06-17 02:36:44 -0700814 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /* Now attach up the new socket */
817 kfree_skb(skb);
Ralf Baechle DL5RB18601a72006-07-03 19:31:14 -0700818 sk_acceptq_removed(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Ralf Baechle75606dc2007-04-20 16:06:45 -0700820out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 release_sock(sk);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return err;
824}
825
826static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100827 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
830 struct sock *sk = sock->sk;
831 struct nr_sock *nr = nr_sk(sk);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100832 int uaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Dan Carpenter7a3b6842013-04-22 20:22:51 +0000834 memset(&sax->fsa_ax25, 0, sizeof(struct sockaddr_ax25));
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 lock_sock(sk);
837 if (peer != 0) {
838 if (sk->sk_state != TCP_ESTABLISHED) {
839 release_sock(sk);
840 return -ENOTCONN;
841 }
842 sax->fsa_ax25.sax25_family = AF_NETROM;
843 sax->fsa_ax25.sax25_ndigis = 1;
844 sax->fsa_ax25.sax25_call = nr->user_addr;
Eric Dumazetf6b97b22009-08-06 03:31:07 +0000845 memset(sax->fsa_digipeater, 0, sizeof(sax->fsa_digipeater));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 sax->fsa_digipeater[0] = nr->dest_addr;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100847 uaddr_len = sizeof(struct full_sockaddr_ax25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 } else {
849 sax->fsa_ax25.sax25_family = AF_NETROM;
850 sax->fsa_ax25.sax25_ndigis = 0;
851 sax->fsa_ax25.sax25_call = nr->source_addr;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100852 uaddr_len = sizeof(struct sockaddr_ax25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854 release_sock(sk);
855
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100856 return uaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
860{
861 struct sock *sk;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900862 struct sock *make;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 struct nr_sock *nr_make;
864 ax25_address *src, *dest, *user;
865 unsigned short circuit_index, circuit_id;
866 unsigned short peer_circuit_index, peer_circuit_id;
867 unsigned short frametype, flags, window, timeout;
868 int ret;
869
Cong Wangc8c82182019-06-27 14:30:58 -0700870 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /*
873 * skb->data points to the netrom frame start
874 */
875
876 src = (ax25_address *)(skb->data + 0);
877 dest = (ax25_address *)(skb->data + 7);
878
879 circuit_index = skb->data[15];
880 circuit_id = skb->data[16];
881 peer_circuit_index = skb->data[17];
882 peer_circuit_id = skb->data[18];
883 frametype = skb->data[19] & 0x0F;
884 flags = skb->data[19] & 0xF0;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * Check for an incoming IP over NET/ROM frame.
888 */
Ralf Baechle98a82fe2005-08-24 11:35:51 -0700889 if (frametype == NR_PROTOEXT &&
890 circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300892 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 return nr_rx_ip(skb, dev);
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /*
898 * Find an existing socket connection, based on circuit ID, if it's
899 * a Connect Request base it on their circuit ID.
900 *
901 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
902 * circuit that no longer exists at the other end ...
903 */
904
905 sk = NULL;
906
907 if (circuit_index == 0 && circuit_id == 0) {
908 if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
909 sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
910 } else {
911 if (frametype == NR_CONNREQ)
912 sk = nr_find_peer(circuit_index, circuit_id, src);
913 else
914 sk = nr_find_socket(circuit_index, circuit_id);
915 }
916
917 if (sk != NULL) {
Cong Wang7314f542018-12-29 13:56:38 -0800918 bh_lock_sock(sk);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300919 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (frametype == NR_CONNACK && skb->len == 22)
922 nr_sk(sk)->bpqext = 1;
923 else
924 nr_sk(sk)->bpqext = 0;
925
926 ret = nr_process_rx_frame(sk, skb);
927 bh_unlock_sock(sk);
Cong Wang7314f542018-12-29 13:56:38 -0800928 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return ret;
930 }
931
932 /*
933 * Now it should be a CONNREQ.
934 */
935 if (frametype != NR_CONNREQ) {
936 /*
937 * Here it would be nice to be able to send a reset but
Ralf Baechlee21ce8c2005-09-12 14:27:37 -0700938 * NET/ROM doesn't have one. We've tried to extend the protocol
939 * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
940 * apparently kills BPQ boxes... :-(
941 * So now we try to follow the established behaviour of
942 * G8PZT's Xrouter which is sending packets with command type 7
943 * as an extension of the protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
Ralf Baechlee21ce8c2005-09-12 14:27:37 -0700945 if (sysctl_netrom_reset_circuit &&
946 (frametype != NR_RESET || flags != 0))
947 nr_transmit_reset(skb, 1);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950 }
951
952 sk = nr_find_listener(dest);
953
954 user = (ax25_address *)(skb->data + 21);
955
956 if (sk == NULL || sk_acceptq_is_full(sk) ||
957 (make = nr_make_new(sk)) == NULL) {
958 nr_transmit_refusal(skb, 0);
959 if (sk)
Cong Wang7314f542018-12-29 13:56:38 -0800960 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
962 }
963
Cong Wang7314f542018-12-29 13:56:38 -0800964 bh_lock_sock(sk);
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 window = skb->data[20];
967
Cong Wang4638faa2019-07-22 20:41:22 -0700968 sock_hold(make);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 skb->sk = make;
Cong Wangc8c82182019-06-27 14:30:58 -0700970 skb->destructor = sock_efree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 make->sk_state = TCP_ESTABLISHED;
972
973 /* Fill in his circuit details */
974 nr_make = nr_sk(make);
975 nr_make->source_addr = *dest;
976 nr_make->dest_addr = *src;
977 nr_make->user_addr = *user;
978
979 nr_make->your_index = circuit_index;
980 nr_make->your_id = circuit_id;
981
982 bh_unlock_sock(sk);
983 circuit = nr_find_next_circuit();
984 bh_lock_sock(sk);
985
986 nr_make->my_index = circuit / 256;
987 nr_make->my_id = circuit % 256;
988
989 circuit++;
990
991 /* Window negotiation */
992 if (window < nr_make->window)
993 nr_make->window = window;
994
995 /* L4 timeout negotiation */
996 if (skb->len == 37) {
997 timeout = skb->data[36] * 256 + skb->data[35];
998 if (timeout * HZ < nr_make->t1)
999 nr_make->t1 = timeout * HZ;
1000 nr_make->bpqext = 1;
1001 } else {
1002 nr_make->bpqext = 0;
1003 }
1004
1005 nr_write_internal(make, NR_CONNACK);
1006
1007 nr_make->condition = 0x00;
1008 nr_make->vs = 0;
1009 nr_make->va = 0;
1010 nr_make->vr = 0;
1011 nr_make->vl = 0;
1012 nr_make->state = NR_STATE_3;
Ralf Baechle DL5RB18601a72006-07-03 19:31:14 -07001013 sk_acceptq_added(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 skb_queue_head(&sk->sk_receive_queue, skb);
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04001017 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 bh_unlock_sock(sk);
Cong Wang7314f542018-12-29 13:56:38 -08001020 sock_put(sk);
Ralf Baechle5cc29e32006-07-10 16:23:21 -07001021
1022 nr_insert_socket(make);
1023
1024 nr_start_heartbeat(make);
1025 nr_start_idletimer(make);
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return 1;
1028}
1029
Ying Xue1b784142015-03-02 15:37:48 +08001030static int nr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 struct sock *sk = sock->sk;
1033 struct nr_sock *nr = nr_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001034 DECLARE_SOCKADDR(struct sockaddr_ax25 *, usax, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 int err;
1036 struct sockaddr_ax25 sax;
1037 struct sk_buff *skb;
1038 unsigned char *asmptr;
1039 int size;
1040
1041 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1042 return -EINVAL;
1043
1044 lock_sock(sk);
1045 if (sock_flag(sk, SOCK_ZAPPED)) {
1046 err = -EADDRNOTAVAIL;
1047 goto out;
1048 }
1049
1050 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1051 send_sig(SIGPIPE, current, 0);
1052 err = -EPIPE;
1053 goto out;
1054 }
1055
1056 if (nr->device == NULL) {
1057 err = -ENETUNREACH;
1058 goto out;
1059 }
1060
1061 if (usax) {
1062 if (msg->msg_namelen < sizeof(sax)) {
1063 err = -EINVAL;
1064 goto out;
1065 }
1066 sax = *usax;
1067 if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
1068 err = -EISCONN;
1069 goto out;
1070 }
1071 if (sax.sax25_family != AF_NETROM) {
1072 err = -EINVAL;
1073 goto out;
1074 }
1075 } else {
1076 if (sk->sk_state != TCP_ESTABLISHED) {
1077 err = -ENOTCONN;
1078 goto out;
1079 }
1080 sax.sax25_family = AF_NETROM;
1081 sax.sax25_call = nr->dest_addr;
1082 }
1083
Alan Cox83e0bbc2009-03-27 00:28:21 -07001084 /* Build a packet - the conventional user limit is 236 bytes. We can
1085 do ludicrously large NetROM frames but must not overflow */
Jean Delvarecc29c702009-04-22 00:49:51 -07001086 if (len > 65536) {
1087 err = -EMSGSIZE;
1088 goto out;
1089 }
Alan Cox83e0bbc2009-03-27 00:28:21 -07001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1092
1093 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1094 goto out;
1095
1096 skb_reserve(skb, size - len);
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001097 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /*
1100 * Push down the NET/ROM header
1101 */
1102
1103 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 /* Build a NET/ROM Transport header */
1106
1107 *asmptr++ = nr->your_index;
1108 *asmptr++ = nr->your_id;
1109 *asmptr++ = 0; /* To be filled in later */
1110 *asmptr++ = 0; /* Ditto */
1111 *asmptr++ = NR_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 /*
1114 * Put the data on the end
1115 */
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001116 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /* User data follows immediately after the NET/ROM transport header */
Al Viro6ce8e9c2014-04-06 21:25:44 -04001119 if (memcpy_from_msg(skb_transport_header(skb), msg, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 kfree_skb(skb);
1121 err = -EFAULT;
1122 goto out;
1123 }
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (sk->sk_state != TCP_ESTABLISHED) {
1126 kfree_skb(skb);
1127 err = -ENOTCONN;
1128 goto out;
1129 }
1130
1131 nr_output(sk, skb); /* Shove it onto the queue */
1132
1133 err = len;
1134out:
1135 release_sock(sk);
1136 return err;
1137}
1138
Ying Xue1b784142015-03-02 15:37:48 +08001139static int nr_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1140 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001143 DECLARE_SOCKADDR(struct sockaddr_ax25 *, sax, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 size_t copied;
1145 struct sk_buff *skb;
1146 int er;
1147
1148 /*
1149 * This works for seqpacket too. The receiver has ordered the queue for
1150 * us! We do one quick check first though
1151 */
1152
1153 lock_sock(sk);
1154 if (sk->sk_state != TCP_ESTABLISHED) {
1155 release_sock(sk);
1156 return -ENOTCONN;
1157 }
1158
1159 /* Now we can treat all alike */
1160 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
1161 release_sock(sk);
1162 return er;
1163 }
1164
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001165 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 copied = skb->len;
1167
1168 if (copied > size) {
1169 copied = size;
1170 msg->msg_flags |= MSG_TRUNC;
1171 }
1172
David S. Miller51f3d022014-11-05 16:46:40 -05001173 er = skb_copy_datagram_msg(skb, 0, msg, copied);
Alan Cox6cf5c952012-09-04 04:13:18 +00001174 if (er < 0) {
1175 skb_free_datagram(sk, skb);
1176 release_sock(sk);
1177 return er;
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 if (sax != NULL) {
Wei Yongjunc802d752013-04-09 10:07:19 +08001181 memset(sax, 0, sizeof(*sax));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 sax->sax25_family = AF_NETROM;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001183 skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call,
1184 AX25_ADDR_LEN);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001185 msg->msg_namelen = sizeof(*sax);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
David S. Miller80e20f62009-03-27 17:22:55 -07001188 skb_free_datagram(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 release_sock(sk);
1191 return copied;
1192}
1193
1194
1195static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1196{
1197 struct sock *sk = sock->sk;
1198 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 switch (cmd) {
1201 case TIOCOUTQ: {
1202 long amount;
Christoph Hellwig5ff76302006-01-03 14:14:46 -08001203
1204 lock_sock(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07001205 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (amount < 0)
1207 amount = 0;
1208 release_sock(sk);
1209 return put_user(amount, (int __user *)argp);
1210 }
1211
1212 case TIOCINQ: {
1213 struct sk_buff *skb;
1214 long amount = 0L;
Christoph Hellwig5ff76302006-01-03 14:14:46 -08001215
1216 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /* These two are safe on a single CPU system as only user tasks fiddle here */
1218 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1219 amount = skb->len;
1220 release_sock(sk);
1221 return put_user(amount, (int __user *)argp);
1222 }
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 case SIOCGIFADDR:
1225 case SIOCSIFADDR:
1226 case SIOCGIFDSTADDR:
1227 case SIOCSIFDSTADDR:
1228 case SIOCGIFBRDADDR:
1229 case SIOCSIFBRDADDR:
1230 case SIOCGIFNETMASK:
1231 case SIOCSIFNETMASK:
1232 case SIOCGIFMETRIC:
1233 case SIOCSIFMETRIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return -EINVAL;
1235
1236 case SIOCADDRT:
1237 case SIOCDELRT:
1238 case SIOCNRDECOBS:
Ralf Baechle0f20f5a2011-11-24 23:54:10 +00001239 if (!capable(CAP_NET_ADMIN))
1240 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return nr_rt_ioctl(cmd, argp);
1242
1243 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001244 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 return 0;
1248}
1249
1250#ifdef CONFIG_PROC_FS
1251
1252static void *nr_info_start(struct seq_file *seq, loff_t *pos)
Jules Irenged087f182020-02-23 23:16:49 +00001253 __acquires(&nr_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
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)
Jules Irenge8b003f02020-02-23 23:16:50 +00001265 __releases(&nr_list_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 spin_unlock_bh(&nr_list_lock);
1268}
1269
1270static int nr_info_show(struct seq_file *seq, void *v)
1271{
Li Zefan90dd7f52010-02-08 23:19:42 +00001272 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct net_device *dev;
1274 struct nr_sock *nr;
1275 const char *devname;
Ralf Baechlef75268c2005-09-06 15:49:39 -07001276 char buf[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 if (v == SEQ_START_TOKEN)
1279 seq_puts(seq,
1280"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");
1281
1282 else {
1283
1284 bh_lock_sock(s);
1285 nr = nr_sk(s);
1286
1287 if ((dev = nr->device) == NULL)
1288 devname = "???";
1289 else
1290 devname = dev->name;
1291
Ralf Baechlef75268c2005-09-06 15:49:39 -07001292 seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr));
1293 seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr));
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001294 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295"%-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 -07001296 ax2asc(buf, &nr->source_addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 devname,
1298 nr->my_index,
1299 nr->my_id,
1300 nr->your_index,
1301 nr->your_id,
1302 nr->state,
1303 nr->vs,
1304 nr->vr,
1305 nr->va,
1306 ax25_display_timer(&nr->t1timer) / HZ,
1307 nr->t1 / HZ,
1308 ax25_display_timer(&nr->t2timer) / HZ,
1309 nr->t2 / HZ,
1310 ax25_display_timer(&nr->t4timer) / HZ,
1311 nr->t4 / HZ,
1312 ax25_display_timer(&nr->idletimer) / (60 * HZ),
1313 nr->idle / (60 * HZ),
1314 nr->n2count,
1315 nr->n2,
1316 nr->window,
Eric Dumazet31e6d362009-06-17 19:05:41 -07001317 sk_wmem_alloc_get(s),
1318 sk_rmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1320
1321 bh_unlock_sock(s);
1322 }
1323 return 0;
1324}
1325
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001326static const struct seq_operations nr_info_seqops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 .start = nr_info_start,
1328 .next = nr_info_next,
1329 .stop = nr_info_stop,
1330 .show = nr_info_show,
1331};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332#endif /* CONFIG_PROC_FS */
1333
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001334static const struct net_proto_family nr_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 .family = PF_NETROM,
1336 .create = nr_create,
1337 .owner = THIS_MODULE,
1338};
1339
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001340static const struct proto_ops nr_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 .family = PF_NETROM,
1342 .owner = THIS_MODULE,
1343 .release = nr_release,
1344 .bind = nr_bind,
1345 .connect = nr_connect,
1346 .socketpair = sock_no_socketpair,
1347 .accept = nr_accept,
1348 .getname = nr_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001349 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 .ioctl = nr_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02001351 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 .listen = nr_listen,
1353 .shutdown = sock_no_shutdown,
1354 .setsockopt = nr_setsockopt,
1355 .getsockopt = nr_getsockopt,
1356 .sendmsg = nr_sendmsg,
1357 .recvmsg = nr_recvmsg,
1358 .mmap = sock_no_mmap,
1359 .sendpage = sock_no_sendpage,
1360};
1361
1362static struct notifier_block nr_dev_notifier = {
1363 .notifier_call = nr_device_event,
1364};
1365
1366static struct net_device **dev_nr;
1367
Ralf Baechle8d5cf592006-12-14 15:50:01 -08001368static struct ax25_protocol nr_pid = {
1369 .pid = AX25_P_NETROM,
1370 .func = nr_route_frame
1371};
1372
Ralf Baechlea4282712006-12-14 15:51:23 -08001373static struct ax25_linkfail nr_linkfail_notifier = {
1374 .func = nr_link_failed,
1375};
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377static int __init nr_proto_init(void)
1378{
1379 int i;
1380 int rc = proto_register(&nr_proto, 0);
1381
YueHaibingd3706562019-04-09 19:53:55 +08001382 if (rc)
1383 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
YueHaibingd3706562019-04-09 19:53:55 +08001386 pr_err("NET/ROM: %s - nr_ndevs parameter too large\n",
1387 __func__);
1388 rc = -EINVAL;
1389 goto unregister_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
Kees Cook6396bb22018-06-12 14:03:40 -07001392 dev_nr = kcalloc(nr_ndevs, sizeof(struct net_device *), GFP_KERNEL);
YueHaibingd3706562019-04-09 19:53:55 +08001393 if (!dev_nr) {
1394 pr_err("NET/ROM: %s - unable to allocate device array\n",
1395 __func__);
1396 rc = -ENOMEM;
1397 goto unregister_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 for (i = 0; i < nr_ndevs; i++) {
1401 char name[IFNAMSIZ];
1402 struct net_device *dev;
1403
1404 sprintf(name, "nr%d", i);
Tom Gundersenc835a672014-07-14 16:37:24 +02001405 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, nr_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (!dev) {
YueHaibingd3706562019-04-09 19:53:55 +08001407 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 goto fail;
1409 }
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 dev->base_addr = i;
YueHaibingd3706562019-04-09 19:53:55 +08001412 rc = register_netdev(dev);
1413 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 free_netdev(dev);
1415 goto fail;
1416 }
Cong Wang1a33e102020-05-02 22:22:19 -07001417 nr_set_lockdep_key(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 dev_nr[i] = dev;
1419 }
1420
YueHaibingd3706562019-04-09 19:53:55 +08001421 rc = sock_register(&nr_family_ops);
1422 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 goto fail;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001424
YueHaibingd3706562019-04-09 19:53:55 +08001425 rc = register_netdevice_notifier(&nr_dev_notifier);
1426 if (rc)
1427 goto out_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Ralf Baechle8d5cf592006-12-14 15:50:01 -08001429 ax25_register_pid(&nr_pid);
Ralf Baechlea4282712006-12-14 15:51:23 -08001430 ax25_linkfail_register(&nr_linkfail_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432#ifdef CONFIG_SYSCTL
YueHaibingd3706562019-04-09 19:53:55 +08001433 rc = nr_register_sysctl();
1434 if (rc)
1435 goto out_sysctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436#endif
1437
1438 nr_loopback_init();
1439
YueHaibingd3706562019-04-09 19:53:55 +08001440 rc = -ENOMEM;
1441 if (!proc_create_seq("nr", 0444, init_net.proc_net, &nr_info_seqops))
1442 goto proc_remove1;
1443 if (!proc_create_seq("nr_neigh", 0444, init_net.proc_net,
1444 &nr_neigh_seqops))
1445 goto proc_remove2;
1446 if (!proc_create_seq("nr_nodes", 0444, init_net.proc_net,
1447 &nr_node_seqops))
1448 goto proc_remove3;
1449
1450 return 0;
1451
1452proc_remove3:
1453 remove_proc_entry("nr_neigh", init_net.proc_net);
1454proc_remove2:
1455 remove_proc_entry("nr", init_net.proc_net);
1456proc_remove1:
1457
1458 nr_loopback_clear();
1459 nr_rt_free();
1460
1461#ifdef CONFIG_SYSCTL
1462 nr_unregister_sysctl();
1463out_sysctl:
1464#endif
1465 ax25_linkfail_release(&nr_linkfail_notifier);
1466 ax25_protocol_release(AX25_P_NETROM);
1467 unregister_netdevice_notifier(&nr_dev_notifier);
1468out_sock:
1469 sock_unregister(PF_NETROM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470fail:
1471 while (--i >= 0) {
1472 unregister_netdev(dev_nr[i]);
1473 free_netdev(dev_nr[i]);
1474 }
1475 kfree(dev_nr);
YueHaibingd3706562019-04-09 19:53:55 +08001476unregister_proto:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 proto_unregister(&nr_proto);
YueHaibingd3706562019-04-09 19:53:55 +08001478 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481module_init(nr_proto_init);
1482
1483module_param(nr_ndevs, int, 0);
1484MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1485
1486MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1487MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1488MODULE_LICENSE("GPL");
1489MODULE_ALIAS_NETPROTO(PF_NETROM);
1490
1491static void __exit nr_exit(void)
1492{
1493 int i;
1494
Gao fengece31ff2013-02-18 01:34:56 +00001495 remove_proc_entry("nr", init_net.proc_net);
1496 remove_proc_entry("nr_neigh", init_net.proc_net);
1497 remove_proc_entry("nr_nodes", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 nr_loopback_clear();
1499
1500 nr_rt_free();
1501
1502#ifdef CONFIG_SYSCTL
1503 nr_unregister_sysctl();
1504#endif
1505
Ralf Baechlea4282712006-12-14 15:51:23 -08001506 ax25_linkfail_release(&nr_linkfail_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 ax25_protocol_release(AX25_P_NETROM);
1508
1509 unregister_netdevice_notifier(&nr_dev_notifier);
1510
1511 sock_unregister(PF_NETROM);
1512
1513 for (i = 0; i < nr_ndevs; i++) {
1514 struct net_device *dev = dev_nr[i];
1515 if (dev) {
1516 unregister_netdev(dev);
1517 free_netdev(dev);
1518 }
1519 }
1520
1521 kfree(dev_nr);
1522 proto_unregister(&nr_proto);
1523}
1524module_exit(nr_exit);