Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * |
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 | */ | |
1da177e4 LT |
8 | #include <linux/module.h> |
9 | #include <linux/moduleparam.h> | |
4fc268d2 | 10 | #include <linux/capability.h> |
1da177e4 LT |
11 | #include <linux/errno.h> |
12 | #include <linux/types.h> | |
13 | #include <linux/socket.h> | |
14 | #include <linux/in.h> | |
5a0e3ad6 | 15 | #include <linux/slab.h> |
1da177e4 | 16 | #include <linux/kernel.h> |
3f07c014 | 17 | #include <linux/sched/signal.h> |
1da177e4 LT |
18 | #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> | |
457c4cbc | 28 | #include <net/net_namespace.h> |
1da177e4 | 29 | #include <net/sock.h> |
dc8e5416 | 30 | #include <linux/uaccess.h> |
1da177e4 LT |
31 | #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> | |
c752f073 | 40 | #include <net/tcp_states.h> |
1da177e4 LT |
41 | #include <net/arp.h> |
42 | #include <linux/init.h> | |
43 | ||
44 | static int nr_ndevs = 4; | |
45 | ||
46 | int sysctl_netrom_default_path_quality = NR_DEFAULT_QUAL; | |
47 | int sysctl_netrom_obsolescence_count_initialiser = NR_DEFAULT_OBS; | |
48 | int sysctl_netrom_network_ttl_initialiser = NR_DEFAULT_TTL; | |
49 | int sysctl_netrom_transport_timeout = NR_DEFAULT_T1; | |
50 | int sysctl_netrom_transport_maximum_tries = NR_DEFAULT_N2; | |
51 | int sysctl_netrom_transport_acknowledge_delay = NR_DEFAULT_T2; | |
52 | int sysctl_netrom_transport_busy_delay = NR_DEFAULT_T4; | |
53 | int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW; | |
54 | int sysctl_netrom_transport_no_activity_timeout = NR_DEFAULT_IDLE; | |
55 | int sysctl_netrom_routing_control = NR_DEFAULT_ROUTING; | |
56 | int sysctl_netrom_link_fails_count = NR_DEFAULT_FAILS; | |
e21ce8c7 | 57 | int sysctl_netrom_reset_circuit = NR_DEFAULT_RESET; |
1da177e4 LT |
58 | |
59 | static unsigned short circuit = 0x101; | |
60 | ||
61 | static HLIST_HEAD(nr_list); | |
62 | static DEFINE_SPINLOCK(nr_list_lock); | |
63 | ||
90ddc4f0 | 64 | static const struct proto_ops nr_proto_ops; |
1da177e4 | 65 | |
1a33e10e CW |
66 | /* |
67 | * 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 | */ | |
72 | static struct lock_class_key nr_netdev_xmit_lock_key; | |
845e0ebb | 73 | static struct lock_class_key nr_netdev_addr_lock_key; |
1a33e10e CW |
74 | |
75 | static void nr_set_lockdep_one(struct net_device *dev, | |
76 | struct netdev_queue *txq, | |
77 | void *_unused) | |
78 | { | |
79 | lockdep_set_class(&txq->_xmit_lock, &nr_netdev_xmit_lock_key); | |
80 | } | |
81 | ||
82 | static void nr_set_lockdep_key(struct net_device *dev) | |
83 | { | |
845e0ebb | 84 | lockdep_set_class(&dev->addr_list_lock, &nr_netdev_addr_lock_key); |
1a33e10e CW |
85 | netdev_for_each_tx_queue(dev, nr_set_lockdep_one, NULL); |
86 | } | |
87 | ||
1da177e4 LT |
88 | /* |
89 | * Socket removal during an interrupt is now safe. | |
90 | */ | |
91 | static 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 | */ | |
101 | static void nr_kill_by_device(struct net_device *dev) | |
102 | { | |
103 | struct sock *s; | |
1da177e4 LT |
104 | |
105 | spin_lock_bh(&nr_list_lock); | |
b67bfe0d | 106 | sk_for_each(s, &nr_list) |
1da177e4 LT |
107 | 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 | */ | |
115 | static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr) | |
116 | { | |
351638e7 | 117 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
1da177e4 | 118 | |
721499e8 | 119 | if (!net_eq(dev_net(dev), &init_net)) |
e9dc8653 EB |
120 | return NOTIFY_DONE; |
121 | ||
1da177e4 LT |
122 | if (event != NETDEV_DOWN) |
123 | return NOTIFY_DONE; | |
124 | ||
125 | nr_kill_by_device(dev); | |
126 | nr_rt_device_down(dev); | |
5f8f59d6 | 127 | |
1da177e4 LT |
128 | return NOTIFY_DONE; |
129 | } | |
130 | ||
131 | /* | |
132 | * Add a socket to the bound sockets list. | |
133 | */ | |
134 | static 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 | */ | |
145 | static struct sock *nr_find_listener(ax25_address *addr) | |
146 | { | |
147 | struct sock *s; | |
1da177e4 LT |
148 | |
149 | spin_lock_bh(&nr_list_lock); | |
b67bfe0d | 150 | sk_for_each(s, &nr_list) |
1da177e4 LT |
151 | if (!ax25cmp(&nr_sk(s)->source_addr, addr) && |
152 | s->sk_state == TCP_LISTEN) { | |
7314f548 | 153 | sock_hold(s); |
1da177e4 LT |
154 | goto found; |
155 | } | |
156 | s = NULL; | |
157 | found: | |
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 | */ | |
165 | static struct sock *nr_find_socket(unsigned char index, unsigned char id) | |
166 | { | |
167 | struct sock *s; | |
1da177e4 LT |
168 | |
169 | spin_lock_bh(&nr_list_lock); | |
b67bfe0d | 170 | sk_for_each(s, &nr_list) { |
1da177e4 | 171 | struct nr_sock *nr = nr_sk(s); |
5f8f59d6 | 172 | |
1da177e4 | 173 | if (nr->my_index == index && nr->my_id == id) { |
7314f548 | 174 | sock_hold(s); |
1da177e4 LT |
175 | goto found; |
176 | } | |
177 | } | |
178 | s = NULL; | |
179 | found: | |
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 | */ | |
187 | static struct sock *nr_find_peer(unsigned char index, unsigned char id, | |
188 | ax25_address *dest) | |
189 | { | |
190 | struct sock *s; | |
1da177e4 LT |
191 | |
192 | spin_lock_bh(&nr_list_lock); | |
b67bfe0d | 193 | sk_for_each(s, &nr_list) { |
1da177e4 | 194 | struct nr_sock *nr = nr_sk(s); |
5f8f59d6 | 195 | |
1da177e4 LT |
196 | if (nr->your_index == index && nr->your_id == id && |
197 | !ax25cmp(&nr->dest_addr, dest)) { | |
7314f548 | 198 | sock_hold(s); |
1da177e4 LT |
199 | goto found; |
200 | } | |
201 | } | |
202 | s = NULL; | |
203 | found: | |
204 | spin_unlock_bh(&nr_list_lock); | |
205 | return s; | |
206 | } | |
207 | ||
208 | /* | |
209 | * Find next free circuit ID. | |
210 | */ | |
211 | static 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; | |
7314f548 | 224 | sock_put(sk); |
1da177e4 LT |
225 | } |
226 | ||
227 | id++; | |
228 | } | |
229 | ||
230 | return id; | |
231 | } | |
232 | ||
233 | /* | |
234 | * Deferred destroy. | |
235 | */ | |
236 | void nr_destroy_socket(struct sock *); | |
237 | ||
238 | /* | |
239 | * Handler for deferred kills. | |
240 | */ | |
99767f27 | 241 | static void nr_destroy_timer(struct timer_list *t) |
1da177e4 | 242 | { |
99767f27 | 243 | struct sock *sk = from_timer(sk, t, sk_timer); |
1da177e4 LT |
244 | 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 | */ | |
257 | void 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 | ||
c564039f | 282 | if (sk_has_allocations(sk)) { |
1da177e4 | 283 | /* Defer: outstanding buffers */ |
841b86f3 | 284 | sk->sk_timer.function = nr_destroy_timer; |
1da177e4 LT |
285 | 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 | ||
296 | static int nr_setsockopt(struct socket *sock, int level, int optname, | |
a7b75c5a | 297 | sockptr_t optval, unsigned int optlen) |
1da177e4 LT |
298 | { |
299 | struct sock *sk = sock->sk; | |
300 | struct nr_sock *nr = nr_sk(sk); | |
dc35616e | 301 | unsigned int opt; |
1da177e4 LT |
302 | |
303 | if (level != SOL_NETROM) | |
304 | return -ENOPROTOOPT; | |
305 | ||
32288eb4 | 306 | if (optlen < sizeof(unsigned int)) |
1da177e4 LT |
307 | return -EINVAL; |
308 | ||
dc35616e | 309 | if (copy_from_sockptr(&opt, optval, sizeof(opt))) |
1da177e4 LT |
310 | return -EFAULT; |
311 | ||
312 | switch (optname) { | |
313 | case NETROM_T1: | |
dc35616e | 314 | if (opt < 1 || opt > UINT_MAX / HZ) |
1da177e4 LT |
315 | return -EINVAL; |
316 | nr->t1 = opt * HZ; | |
317 | return 0; | |
318 | ||
319 | case NETROM_T2: | |
dc35616e | 320 | if (opt < 1 || opt > UINT_MAX / HZ) |
1da177e4 LT |
321 | 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: | |
dc35616e | 332 | if (opt < 1 || opt > UINT_MAX / HZ) |
1da177e4 LT |
333 | return -EINVAL; |
334 | nr->t4 = opt * HZ; | |
335 | return 0; | |
336 | ||
337 | case NETROM_IDLE: | |
dc35616e | 338 | if (opt > UINT_MAX / (60 * HZ)) |
1da177e4 LT |
339 | return -EINVAL; |
340 | nr->idle = opt * 60 * HZ; | |
341 | return 0; | |
342 | ||
343 | default: | |
344 | return -ENOPROTOOPT; | |
345 | } | |
346 | } | |
347 | ||
348 | static 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; | |
5f8f59d6 | 354 | int len; |
1da177e4 LT |
355 | |
356 | if (level != SOL_NETROM) | |
357 | return -ENOPROTOOPT; | |
5f8f59d6 | 358 | |
1da177e4 LT |
359 | if (get_user(len, optlen)) |
360 | return -EFAULT; | |
361 | ||
362 | if (len < 0) | |
363 | return -EINVAL; | |
5f8f59d6 | 364 | |
1da177e4 LT |
365 | 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 | ||
398 | static int nr_listen(struct socket *sock, int backlog) | |
399 | { | |
400 | struct sock *sk = sock->sk; | |
401 | ||
402 | lock_sock(sk); | |
61179292 HK |
403 | if (sock->state != SS_UNCONNECTED) { |
404 | release_sock(sk); | |
405 | return -EINVAL; | |
406 | } | |
407 | ||
1da177e4 LT |
408 | if (sk->sk_state != TCP_LISTEN) { |
409 | memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN); | |
410 | sk->sk_max_ack_backlog = backlog; | |
411 | sk->sk_state = TCP_LISTEN; | |
412 | release_sock(sk); | |
413 | return 0; | |
414 | } | |
415 | release_sock(sk); | |
416 | ||
417 | return -EOPNOTSUPP; | |
418 | } | |
419 | ||
420 | static struct proto nr_proto = { | |
421 | .name = "NETROM", | |
422 | .owner = THIS_MODULE, | |
423 | .obj_size = sizeof(struct nr_sock), | |
424 | }; | |
425 | ||
3f378b68 EP |
426 | static int nr_create(struct net *net, struct socket *sock, int protocol, |
427 | int kern) | |
1da177e4 LT |
428 | { |
429 | struct sock *sk; | |
430 | struct nr_sock *nr; | |
431 | ||
09ad9bc7 | 432 | if (!net_eq(net, &init_net)) |
1b8d7ae4 EB |
433 | return -EAFNOSUPPORT; |
434 | ||
1da177e4 LT |
435 | if (sock->type != SOCK_SEQPACKET || protocol != 0) |
436 | return -ESOCKTNOSUPPORT; | |
437 | ||
11aa9c28 | 438 | sk = sk_alloc(net, PF_NETROM, GFP_ATOMIC, &nr_proto, kern); |
6257ff21 | 439 | if (sk == NULL) |
1da177e4 LT |
440 | return -ENOMEM; |
441 | ||
442 | nr = nr_sk(sk); | |
443 | ||
444 | sock_init_data(sock, sk); | |
445 | ||
446 | sock->ops = &nr_proto_ops; | |
447 | sk->sk_protocol = protocol; | |
448 | ||
449 | skb_queue_head_init(&nr->ack_queue); | |
450 | skb_queue_head_init(&nr->reseq_queue); | |
451 | skb_queue_head_init(&nr->frag_queue); | |
452 | ||
453 | nr_init_timers(sk); | |
454 | ||
4d8937d0 RB |
455 | nr->t1 = |
456 | msecs_to_jiffies(sysctl_netrom_transport_timeout); | |
457 | nr->t2 = | |
458 | msecs_to_jiffies(sysctl_netrom_transport_acknowledge_delay); | |
459 | nr->n2 = | |
460 | msecs_to_jiffies(sysctl_netrom_transport_maximum_tries); | |
461 | nr->t4 = | |
462 | msecs_to_jiffies(sysctl_netrom_transport_busy_delay); | |
463 | nr->idle = | |
464 | msecs_to_jiffies(sysctl_netrom_transport_no_activity_timeout); | |
1da177e4 LT |
465 | nr->window = sysctl_netrom_transport_requested_window_size; |
466 | ||
467 | nr->bpqext = 1; | |
468 | nr->state = NR_STATE_0; | |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
473 | static struct sock *nr_make_new(struct sock *osk) | |
474 | { | |
475 | struct sock *sk; | |
476 | struct nr_sock *nr, *onr; | |
477 | ||
478 | if (osk->sk_type != SOCK_SEQPACKET) | |
479 | return NULL; | |
480 | ||
11aa9c28 | 481 | sk = sk_alloc(sock_net(osk), PF_NETROM, GFP_ATOMIC, osk->sk_prot, 0); |
6257ff21 | 482 | if (sk == NULL) |
1da177e4 LT |
483 | return NULL; |
484 | ||
485 | nr = nr_sk(sk); | |
486 | ||
487 | sock_init_data(NULL, sk); | |
488 | ||
489 | sk->sk_type = osk->sk_type; | |
1da177e4 LT |
490 | sk->sk_priority = osk->sk_priority; |
491 | sk->sk_protocol = osk->sk_protocol; | |
492 | sk->sk_rcvbuf = osk->sk_rcvbuf; | |
493 | sk->sk_sndbuf = osk->sk_sndbuf; | |
494 | sk->sk_state = TCP_ESTABLISHED; | |
53b924b3 | 495 | sock_copy_flags(sk, osk); |
1da177e4 LT |
496 | |
497 | skb_queue_head_init(&nr->ack_queue); | |
498 | skb_queue_head_init(&nr->reseq_queue); | |
499 | skb_queue_head_init(&nr->frag_queue); | |
500 | ||
501 | nr_init_timers(sk); | |
502 | ||
503 | onr = nr_sk(osk); | |
504 | ||
505 | nr->t1 = onr->t1; | |
506 | nr->t2 = onr->t2; | |
507 | nr->n2 = onr->n2; | |
508 | nr->t4 = onr->t4; | |
509 | nr->idle = onr->idle; | |
510 | nr->window = onr->window; | |
511 | ||
512 | nr->device = onr->device; | |
513 | nr->bpqext = onr->bpqext; | |
514 | ||
515 | return sk; | |
516 | } | |
517 | ||
518 | static int nr_release(struct socket *sock) | |
519 | { | |
520 | struct sock *sk = sock->sk; | |
521 | struct nr_sock *nr; | |
522 | ||
523 | if (sk == NULL) return 0; | |
524 | ||
525 | sock_hold(sk); | |
859f4c74 | 526 | sock_orphan(sk); |
1da177e4 LT |
527 | lock_sock(sk); |
528 | nr = nr_sk(sk); | |
529 | ||
530 | switch (nr->state) { | |
531 | case NR_STATE_0: | |
532 | case NR_STATE_1: | |
533 | case NR_STATE_2: | |
534 | nr_disconnect(sk, 0); | |
535 | nr_destroy_socket(sk); | |
536 | break; | |
537 | ||
538 | case NR_STATE_3: | |
539 | nr_clear_queues(sk); | |
540 | nr->n2count = 0; | |
541 | nr_write_internal(sk, NR_DISCREQ); | |
542 | nr_start_t1timer(sk); | |
543 | nr_stop_t2timer(sk); | |
544 | nr_stop_t4timer(sk); | |
545 | nr_stop_idletimer(sk); | |
546 | nr->state = NR_STATE_2; | |
547 | sk->sk_state = TCP_CLOSE; | |
548 | sk->sk_shutdown |= SEND_SHUTDOWN; | |
549 | sk->sk_state_change(sk); | |
1da177e4 | 550 | sock_set_flag(sk, SOCK_DESTROY); |
1da177e4 LT |
551 | break; |
552 | ||
553 | default: | |
1da177e4 LT |
554 | break; |
555 | } | |
556 | ||
5f8f59d6 | 557 | sock->sk = NULL; |
1da177e4 LT |
558 | release_sock(sk); |
559 | sock_put(sk); | |
560 | ||
561 | return 0; | |
562 | } | |
563 | ||
564 | static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) | |
565 | { | |
566 | struct sock *sk = sock->sk; | |
567 | struct nr_sock *nr = nr_sk(sk); | |
568 | struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr; | |
569 | struct net_device *dev; | |
01d7dd0e RB |
570 | ax25_uid_assoc *user; |
571 | ax25_address *source; | |
1da177e4 LT |
572 | |
573 | lock_sock(sk); | |
574 | if (!sock_flag(sk, SOCK_ZAPPED)) { | |
575 | release_sock(sk); | |
576 | return -EINVAL; | |
577 | } | |
578 | if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) { | |
579 | release_sock(sk); | |
580 | return -EINVAL; | |
581 | } | |
582 | if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) { | |
583 | release_sock(sk); | |
584 | return -EINVAL; | |
585 | } | |
586 | if (addr->fsa_ax25.sax25_family != AF_NETROM) { | |
587 | release_sock(sk); | |
588 | return -EINVAL; | |
589 | } | |
590 | if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) { | |
1da177e4 LT |
591 | release_sock(sk); |
592 | return -EADDRNOTAVAIL; | |
593 | } | |
594 | ||
595 | /* | |
596 | * Only the super user can set an arbitrary user callsign. | |
597 | */ | |
598 | if (addr->fsa_ax25.sax25_ndigis == 1) { | |
599 | if (!capable(CAP_NET_BIND_SERVICE)) { | |
600 | dev_put(dev); | |
601 | release_sock(sk); | |
bf5b30b8 | 602 | return -EPERM; |
1da177e4 LT |
603 | } |
604 | nr->user_addr = addr->fsa_digipeater[0]; | |
605 | nr->source_addr = addr->fsa_ax25.sax25_call; | |
606 | } else { | |
607 | source = &addr->fsa_ax25.sax25_call; | |
608 | ||
ba95b235 | 609 | user = ax25_findbyuid(current_euid()); |
01d7dd0e RB |
610 | if (user) { |
611 | nr->user_addr = user->call; | |
612 | ax25_uid_put(user); | |
613 | } else { | |
1da177e4 LT |
614 | if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) { |
615 | release_sock(sk); | |
616 | dev_put(dev); | |
617 | return -EPERM; | |
618 | } | |
01d7dd0e | 619 | nr->user_addr = *source; |
1da177e4 LT |
620 | } |
621 | ||
1da177e4 LT |
622 | nr->source_addr = *source; |
623 | } | |
624 | ||
625 | nr->device = dev; | |
626 | nr_insert_socket(sk); | |
627 | ||
628 | sock_reset_flag(sk, SOCK_ZAPPED); | |
629 | dev_put(dev); | |
630 | release_sock(sk); | |
8849b720 | 631 | |
1da177e4 LT |
632 | return 0; |
633 | } | |
634 | ||
635 | static int nr_connect(struct socket *sock, struct sockaddr *uaddr, | |
636 | int addr_len, int flags) | |
637 | { | |
638 | struct sock *sk = sock->sk; | |
639 | struct nr_sock *nr = nr_sk(sk); | |
640 | struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr; | |
c045ad2c | 641 | const ax25_address *source = NULL; |
01d7dd0e | 642 | ax25_uid_assoc *user; |
1da177e4 | 643 | struct net_device *dev; |
75606dc6 | 644 | int err = 0; |
1da177e4 LT |
645 | |
646 | lock_sock(sk); | |
647 | if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) { | |
648 | sock->state = SS_CONNECTED; | |
75606dc6 | 649 | goto out_release; /* Connect completed during a ERESTARTSYS event */ |
1da177e4 LT |
650 | } |
651 | ||
652 | if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) { | |
653 | sock->state = SS_UNCONNECTED; | |
75606dc6 RB |
654 | err = -ECONNREFUSED; |
655 | goto out_release; | |
1da177e4 LT |
656 | } |
657 | ||
658 | if (sk->sk_state == TCP_ESTABLISHED) { | |
75606dc6 RB |
659 | err = -EISCONN; /* No reconnect on a seqpacket socket */ |
660 | goto out_release; | |
1da177e4 LT |
661 | } |
662 | ||
5f8f59d6 | 663 | sk->sk_state = TCP_CLOSE; |
1da177e4 LT |
664 | sock->state = SS_UNCONNECTED; |
665 | ||
666 | if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) { | |
75606dc6 RB |
667 | err = -EINVAL; |
668 | goto out_release; | |
1da177e4 LT |
669 | } |
670 | if (addr->sax25_family != AF_NETROM) { | |
75606dc6 RB |
671 | err = -EINVAL; |
672 | goto out_release; | |
1da177e4 LT |
673 | } |
674 | if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */ | |
675 | sock_reset_flag(sk, SOCK_ZAPPED); | |
676 | ||
677 | if ((dev = nr_dev_first()) == NULL) { | |
75606dc6 RB |
678 | err = -ENETUNREACH; |
679 | goto out_release; | |
1da177e4 | 680 | } |
c045ad2c | 681 | source = (const ax25_address *)dev->dev_addr; |
1da177e4 | 682 | |
ba95b235 | 683 | user = ax25_findbyuid(current_euid()); |
01d7dd0e RB |
684 | if (user) { |
685 | nr->user_addr = user->call; | |
686 | ax25_uid_put(user); | |
687 | } else { | |
1da177e4 LT |
688 | if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) { |
689 | dev_put(dev); | |
75606dc6 RB |
690 | err = -EPERM; |
691 | goto out_release; | |
1da177e4 | 692 | } |
01d7dd0e | 693 | nr->user_addr = *source; |
1da177e4 LT |
694 | } |
695 | ||
1da177e4 LT |
696 | nr->source_addr = *source; |
697 | nr->device = dev; | |
698 | ||
699 | dev_put(dev); | |
700 | nr_insert_socket(sk); /* Finish the bind */ | |
701 | } | |
702 | ||
703 | nr->dest_addr = addr->sax25_call; | |
704 | ||
705 | release_sock(sk); | |
706 | circuit = nr_find_next_circuit(); | |
707 | lock_sock(sk); | |
708 | ||
709 | nr->my_index = circuit / 256; | |
710 | nr->my_id = circuit % 256; | |
711 | ||
712 | circuit++; | |
713 | ||
714 | /* Move to connecting socket, start sending Connect Requests */ | |
715 | sock->state = SS_CONNECTING; | |
716 | sk->sk_state = TCP_SYN_SENT; | |
717 | ||
718 | nr_establish_data_link(sk); | |
719 | ||
720 | nr->state = NR_STATE_1; | |
721 | ||
722 | nr_start_heartbeat(sk); | |
723 | ||
724 | /* Now the loop */ | |
725 | if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) { | |
75606dc6 RB |
726 | err = -EINPROGRESS; |
727 | goto out_release; | |
1da177e4 | 728 | } |
5f8f59d6 | 729 | |
1da177e4 LT |
730 | /* |
731 | * A Connect Ack with Choke or timeout or failed routing will go to | |
732 | * closed. | |
733 | */ | |
734 | if (sk->sk_state == TCP_SYN_SENT) { | |
75606dc6 | 735 | DEFINE_WAIT(wait); |
1da177e4 | 736 | |
1da177e4 | 737 | for (;;) { |
aa395145 | 738 | prepare_to_wait(sk_sleep(sk), &wait, |
639fc4c3 | 739 | TASK_INTERRUPTIBLE); |
1da177e4 LT |
740 | if (sk->sk_state != TCP_SYN_SENT) |
741 | break; | |
75606dc6 RB |
742 | if (!signal_pending(current)) { |
743 | release_sock(sk); | |
1da177e4 LT |
744 | schedule(); |
745 | lock_sock(sk); | |
746 | continue; | |
747 | } | |
75606dc6 RB |
748 | err = -ERESTARTSYS; |
749 | break; | |
1da177e4 | 750 | } |
aa395145 | 751 | finish_wait(sk_sleep(sk), &wait); |
75606dc6 RB |
752 | if (err) |
753 | goto out_release; | |
1da177e4 LT |
754 | } |
755 | ||
756 | if (sk->sk_state != TCP_ESTABLISHED) { | |
757 | sock->state = SS_UNCONNECTED; | |
75606dc6 RB |
758 | err = sock_error(sk); /* Always set at this point */ |
759 | goto out_release; | |
1da177e4 LT |
760 | } |
761 | ||
762 | sock->state = SS_CONNECTED; | |
75606dc6 RB |
763 | |
764 | out_release: | |
1da177e4 LT |
765 | release_sock(sk); |
766 | ||
75606dc6 | 767 | return err; |
1da177e4 LT |
768 | } |
769 | ||
cdfbabfb DH |
770 | static int nr_accept(struct socket *sock, struct socket *newsock, int flags, |
771 | bool kern) | |
1da177e4 | 772 | { |
1da177e4 LT |
773 | struct sk_buff *skb; |
774 | struct sock *newsk; | |
75606dc6 | 775 | DEFINE_WAIT(wait); |
1da177e4 LT |
776 | struct sock *sk; |
777 | int err = 0; | |
778 | ||
779 | if ((sk = sock->sk) == NULL) | |
780 | return -EINVAL; | |
781 | ||
782 | lock_sock(sk); | |
783 | if (sk->sk_type != SOCK_SEQPACKET) { | |
784 | err = -EOPNOTSUPP; | |
75606dc6 | 785 | goto out_release; |
1da177e4 LT |
786 | } |
787 | ||
788 | if (sk->sk_state != TCP_LISTEN) { | |
789 | err = -EINVAL; | |
75606dc6 | 790 | goto out_release; |
1da177e4 LT |
791 | } |
792 | ||
793 | /* | |
794 | * The write queue this time is holding sockets ready to use | |
795 | * hooked into the SABM we saved | |
796 | */ | |
1da177e4 | 797 | for (;;) { |
aa395145 | 798 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
1da177e4 LT |
799 | skb = skb_dequeue(&sk->sk_receive_queue); |
800 | if (skb) | |
801 | break; | |
802 | ||
1da177e4 | 803 | if (flags & O_NONBLOCK) { |
75606dc6 RB |
804 | err = -EWOULDBLOCK; |
805 | break; | |
1da177e4 | 806 | } |
75606dc6 RB |
807 | if (!signal_pending(current)) { |
808 | release_sock(sk); | |
1da177e4 LT |
809 | schedule(); |
810 | lock_sock(sk); | |
811 | continue; | |
812 | } | |
75606dc6 RB |
813 | err = -ERESTARTSYS; |
814 | break; | |
1da177e4 | 815 | } |
aa395145 | 816 | finish_wait(sk_sleep(sk), &wait); |
75606dc6 RB |
817 | if (err) |
818 | goto out_release; | |
1da177e4 LT |
819 | |
820 | newsk = skb->sk; | |
7b66767f | 821 | sock_graft(newsk, newsock); |
1da177e4 LT |
822 | |
823 | /* Now attach up the new socket */ | |
824 | kfree_skb(skb); | |
18601a7d | 825 | sk_acceptq_removed(sk); |
1da177e4 | 826 | |
75606dc6 | 827 | out_release: |
1da177e4 | 828 | release_sock(sk); |
75606dc6 | 829 | |
1da177e4 LT |
830 | return err; |
831 | } | |
832 | ||
833 | static int nr_getname(struct socket *sock, struct sockaddr *uaddr, | |
9b2c45d4 | 834 | int peer) |
1da177e4 LT |
835 | { |
836 | struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr; | |
837 | struct sock *sk = sock->sk; | |
838 | struct nr_sock *nr = nr_sk(sk); | |
9b2c45d4 | 839 | int uaddr_len; |
1da177e4 | 840 | |
7a3b6843 DC |
841 | memset(&sax->fsa_ax25, 0, sizeof(struct sockaddr_ax25)); |
842 | ||
1da177e4 LT |
843 | lock_sock(sk); |
844 | if (peer != 0) { | |
845 | if (sk->sk_state != TCP_ESTABLISHED) { | |
846 | release_sock(sk); | |
847 | return -ENOTCONN; | |
848 | } | |
849 | sax->fsa_ax25.sax25_family = AF_NETROM; | |
850 | sax->fsa_ax25.sax25_ndigis = 1; | |
851 | sax->fsa_ax25.sax25_call = nr->user_addr; | |
f6b97b29 | 852 | memset(sax->fsa_digipeater, 0, sizeof(sax->fsa_digipeater)); |
1da177e4 | 853 | sax->fsa_digipeater[0] = nr->dest_addr; |
9b2c45d4 | 854 | uaddr_len = sizeof(struct full_sockaddr_ax25); |
1da177e4 LT |
855 | } else { |
856 | sax->fsa_ax25.sax25_family = AF_NETROM; | |
857 | sax->fsa_ax25.sax25_ndigis = 0; | |
858 | sax->fsa_ax25.sax25_call = nr->source_addr; | |
9b2c45d4 | 859 | uaddr_len = sizeof(struct sockaddr_ax25); |
1da177e4 LT |
860 | } |
861 | release_sock(sk); | |
862 | ||
9b2c45d4 | 863 | return uaddr_len; |
1da177e4 LT |
864 | } |
865 | ||
866 | int nr_rx_frame(struct sk_buff *skb, struct net_device *dev) | |
867 | { | |
868 | struct sock *sk; | |
5f8f59d6 | 869 | struct sock *make; |
1da177e4 LT |
870 | struct nr_sock *nr_make; |
871 | ax25_address *src, *dest, *user; | |
872 | unsigned short circuit_index, circuit_id; | |
873 | unsigned short peer_circuit_index, peer_circuit_id; | |
874 | unsigned short frametype, flags, window, timeout; | |
875 | int ret; | |
876 | ||
c8c8218e | 877 | skb_orphan(skb); |
1da177e4 LT |
878 | |
879 | /* | |
880 | * skb->data points to the netrom frame start | |
881 | */ | |
882 | ||
883 | src = (ax25_address *)(skb->data + 0); | |
884 | dest = (ax25_address *)(skb->data + 7); | |
885 | ||
886 | circuit_index = skb->data[15]; | |
887 | circuit_id = skb->data[16]; | |
888 | peer_circuit_index = skb->data[17]; | |
889 | peer_circuit_id = skb->data[18]; | |
890 | frametype = skb->data[19] & 0x0F; | |
891 | flags = skb->data[19] & 0xF0; | |
892 | ||
1da177e4 LT |
893 | /* |
894 | * Check for an incoming IP over NET/ROM frame. | |
895 | */ | |
98a82feb RB |
896 | if (frametype == NR_PROTOEXT && |
897 | circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) { | |
1da177e4 | 898 | skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN); |
badff6d0 | 899 | skb_reset_transport_header(skb); |
1da177e4 LT |
900 | |
901 | return nr_rx_ip(skb, dev); | |
902 | } | |
1da177e4 LT |
903 | |
904 | /* | |
905 | * Find an existing socket connection, based on circuit ID, if it's | |
906 | * a Connect Request base it on their circuit ID. | |
907 | * | |
908 | * Circuit ID 0/0 is not valid but it could still be a "reset" for a | |
909 | * circuit that no longer exists at the other end ... | |
910 | */ | |
911 | ||
912 | sk = NULL; | |
913 | ||
914 | if (circuit_index == 0 && circuit_id == 0) { | |
915 | if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG) | |
916 | sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src); | |
917 | } else { | |
918 | if (frametype == NR_CONNREQ) | |
919 | sk = nr_find_peer(circuit_index, circuit_id, src); | |
920 | else | |
921 | sk = nr_find_socket(circuit_index, circuit_id); | |
922 | } | |
923 | ||
924 | if (sk != NULL) { | |
7314f548 | 925 | bh_lock_sock(sk); |
badff6d0 | 926 | skb_reset_transport_header(skb); |
1da177e4 LT |
927 | |
928 | if (frametype == NR_CONNACK && skb->len == 22) | |
929 | nr_sk(sk)->bpqext = 1; | |
930 | else | |
931 | nr_sk(sk)->bpqext = 0; | |
932 | ||
933 | ret = nr_process_rx_frame(sk, skb); | |
934 | bh_unlock_sock(sk); | |
7314f548 | 935 | sock_put(sk); |
1da177e4 LT |
936 | return ret; |
937 | } | |
938 | ||
939 | /* | |
940 | * Now it should be a CONNREQ. | |
941 | */ | |
942 | if (frametype != NR_CONNREQ) { | |
943 | /* | |
944 | * Here it would be nice to be able to send a reset but | |
e21ce8c7 RB |
945 | * NET/ROM doesn't have one. We've tried to extend the protocol |
946 | * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that | |
947 | * apparently kills BPQ boxes... :-( | |
948 | * So now we try to follow the established behaviour of | |
949 | * G8PZT's Xrouter which is sending packets with command type 7 | |
950 | * as an extension of the protocol. | |
1da177e4 | 951 | */ |
e21ce8c7 RB |
952 | if (sysctl_netrom_reset_circuit && |
953 | (frametype != NR_RESET || flags != 0)) | |
954 | nr_transmit_reset(skb, 1); | |
955 | ||
1da177e4 LT |
956 | return 0; |
957 | } | |
958 | ||
959 | sk = nr_find_listener(dest); | |
960 | ||
961 | user = (ax25_address *)(skb->data + 21); | |
962 | ||
963 | if (sk == NULL || sk_acceptq_is_full(sk) || | |
964 | (make = nr_make_new(sk)) == NULL) { | |
965 | nr_transmit_refusal(skb, 0); | |
966 | if (sk) | |
7314f548 | 967 | sock_put(sk); |
1da177e4 LT |
968 | return 0; |
969 | } | |
970 | ||
7314f548 CW |
971 | bh_lock_sock(sk); |
972 | ||
1da177e4 LT |
973 | window = skb->data[20]; |
974 | ||
4638faac | 975 | sock_hold(make); |
1da177e4 | 976 | skb->sk = make; |
c8c8218e | 977 | skb->destructor = sock_efree; |
1da177e4 LT |
978 | make->sk_state = TCP_ESTABLISHED; |
979 | ||
980 | /* Fill in his circuit details */ | |
981 | nr_make = nr_sk(make); | |
982 | nr_make->source_addr = *dest; | |
983 | nr_make->dest_addr = *src; | |
984 | nr_make->user_addr = *user; | |
985 | ||
986 | nr_make->your_index = circuit_index; | |
987 | nr_make->your_id = circuit_id; | |
988 | ||
989 | bh_unlock_sock(sk); | |
990 | circuit = nr_find_next_circuit(); | |
991 | bh_lock_sock(sk); | |
992 | ||
993 | nr_make->my_index = circuit / 256; | |
994 | nr_make->my_id = circuit % 256; | |
995 | ||
996 | circuit++; | |
997 | ||
998 | /* Window negotiation */ | |
999 | if (window < nr_make->window) | |
1000 | nr_make->window = window; | |
1001 | ||
1002 | /* L4 timeout negotiation */ | |
1003 | if (skb->len == 37) { | |
1004 | timeout = skb->data[36] * 256 + skb->data[35]; | |
1005 | if (timeout * HZ < nr_make->t1) | |
1006 | nr_make->t1 = timeout * HZ; | |
1007 | nr_make->bpqext = 1; | |
1008 | } else { | |
1009 | nr_make->bpqext = 0; | |
1010 | } | |
1011 | ||
1012 | nr_write_internal(make, NR_CONNACK); | |
1013 | ||
1014 | nr_make->condition = 0x00; | |
1015 | nr_make->vs = 0; | |
1016 | nr_make->va = 0; | |
1017 | nr_make->vr = 0; | |
1018 | nr_make->vl = 0; | |
1019 | nr_make->state = NR_STATE_3; | |
18601a7d | 1020 | sk_acceptq_added(sk); |
1da177e4 LT |
1021 | skb_queue_head(&sk->sk_receive_queue, skb); |
1022 | ||
1da177e4 | 1023 | if (!sock_flag(sk, SOCK_DEAD)) |
676d2369 | 1024 | sk->sk_data_ready(sk); |
1da177e4 LT |
1025 | |
1026 | bh_unlock_sock(sk); | |
7314f548 | 1027 | sock_put(sk); |
5cc29e3b RB |
1028 | |
1029 | nr_insert_socket(make); | |
1030 | ||
1031 | nr_start_heartbeat(make); | |
1032 | nr_start_idletimer(make); | |
1033 | ||
1da177e4 LT |
1034 | return 1; |
1035 | } | |
1036 | ||
1b784140 | 1037 | static int nr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
1da177e4 LT |
1038 | { |
1039 | struct sock *sk = sock->sk; | |
1040 | struct nr_sock *nr = nr_sk(sk); | |
342dfc30 | 1041 | DECLARE_SOCKADDR(struct sockaddr_ax25 *, usax, msg->msg_name); |
1da177e4 LT |
1042 | int err; |
1043 | struct sockaddr_ax25 sax; | |
1044 | struct sk_buff *skb; | |
1045 | unsigned char *asmptr; | |
1046 | int size; | |
1047 | ||
1048 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT)) | |
1049 | return -EINVAL; | |
1050 | ||
1051 | lock_sock(sk); | |
1052 | if (sock_flag(sk, SOCK_ZAPPED)) { | |
1053 | err = -EADDRNOTAVAIL; | |
1054 | goto out; | |
1055 | } | |
1056 | ||
1057 | if (sk->sk_shutdown & SEND_SHUTDOWN) { | |
1058 | send_sig(SIGPIPE, current, 0); | |
1059 | err = -EPIPE; | |
1060 | goto out; | |
1061 | } | |
1062 | ||
1063 | if (nr->device == NULL) { | |
1064 | err = -ENETUNREACH; | |
1065 | goto out; | |
1066 | } | |
1067 | ||
1068 | if (usax) { | |
1069 | if (msg->msg_namelen < sizeof(sax)) { | |
1070 | err = -EINVAL; | |
1071 | goto out; | |
1072 | } | |
1073 | sax = *usax; | |
1074 | if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) { | |
1075 | err = -EISCONN; | |
1076 | goto out; | |
1077 | } | |
1078 | if (sax.sax25_family != AF_NETROM) { | |
1079 | err = -EINVAL; | |
1080 | goto out; | |
1081 | } | |
1082 | } else { | |
1083 | if (sk->sk_state != TCP_ESTABLISHED) { | |
1084 | err = -ENOTCONN; | |
1085 | goto out; | |
1086 | } | |
1087 | sax.sax25_family = AF_NETROM; | |
1088 | sax.sax25_call = nr->dest_addr; | |
1089 | } | |
1090 | ||
83e0bbcb AC |
1091 | /* Build a packet - the conventional user limit is 236 bytes. We can |
1092 | do ludicrously large NetROM frames but must not overflow */ | |
cc29c70d JD |
1093 | if (len > 65536) { |
1094 | err = -EMSGSIZE; | |
1095 | goto out; | |
1096 | } | |
83e0bbcb | 1097 | |
1da177e4 LT |
1098 | size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN; |
1099 | ||
1100 | if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL) | |
1101 | goto out; | |
1102 | ||
1103 | skb_reserve(skb, size - len); | |
eeeb0374 | 1104 | skb_reset_transport_header(skb); |
1da177e4 LT |
1105 | |
1106 | /* | |
1107 | * Push down the NET/ROM header | |
1108 | */ | |
1109 | ||
1110 | asmptr = skb_push(skb, NR_TRANSPORT_LEN); | |
1da177e4 LT |
1111 | |
1112 | /* Build a NET/ROM Transport header */ | |
1113 | ||
1114 | *asmptr++ = nr->your_index; | |
1115 | *asmptr++ = nr->your_id; | |
1116 | *asmptr++ = 0; /* To be filled in later */ | |
1117 | *asmptr++ = 0; /* Ditto */ | |
1118 | *asmptr++ = NR_INFO; | |
1da177e4 LT |
1119 | |
1120 | /* | |
1121 | * Put the data on the end | |
1122 | */ | |
eeeb0374 | 1123 | skb_put(skb, len); |
1da177e4 | 1124 | |
1da177e4 | 1125 | /* User data follows immediately after the NET/ROM transport header */ |
6ce8e9ce | 1126 | if (memcpy_from_msg(skb_transport_header(skb), msg, len)) { |
1da177e4 LT |
1127 | kfree_skb(skb); |
1128 | err = -EFAULT; | |
1129 | goto out; | |
1130 | } | |
1131 | ||
1da177e4 LT |
1132 | if (sk->sk_state != TCP_ESTABLISHED) { |
1133 | kfree_skb(skb); | |
1134 | err = -ENOTCONN; | |
1135 | goto out; | |
1136 | } | |
1137 | ||
1138 | nr_output(sk, skb); /* Shove it onto the queue */ | |
1139 | ||
1140 | err = len; | |
1141 | out: | |
1142 | release_sock(sk); | |
1143 | return err; | |
1144 | } | |
1145 | ||
1b784140 YX |
1146 | static int nr_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, |
1147 | int flags) | |
1da177e4 LT |
1148 | { |
1149 | struct sock *sk = sock->sk; | |
342dfc30 | 1150 | DECLARE_SOCKADDR(struct sockaddr_ax25 *, sax, msg->msg_name); |
1da177e4 LT |
1151 | size_t copied; |
1152 | struct sk_buff *skb; | |
1153 | int er; | |
1154 | ||
1155 | /* | |
1156 | * This works for seqpacket too. The receiver has ordered the queue for | |
1157 | * us! We do one quick check first though | |
1158 | */ | |
1159 | ||
1160 | lock_sock(sk); | |
1161 | if (sk->sk_state != TCP_ESTABLISHED) { | |
1162 | release_sock(sk); | |
1163 | return -ENOTCONN; | |
1164 | } | |
1165 | ||
1166 | /* Now we can treat all alike */ | |
f4b41f06 OH |
1167 | skb = skb_recv_datagram(sk, flags, &er); |
1168 | if (!skb) { | |
1da177e4 LT |
1169 | release_sock(sk); |
1170 | return er; | |
1171 | } | |
1172 | ||
badff6d0 | 1173 | skb_reset_transport_header(skb); |
1da177e4 LT |
1174 | copied = skb->len; |
1175 | ||
1176 | if (copied > size) { | |
1177 | copied = size; | |
1178 | msg->msg_flags |= MSG_TRUNC; | |
1179 | } | |
1180 | ||
51f3d02b | 1181 | er = skb_copy_datagram_msg(skb, 0, msg, copied); |
6cf5c951 AC |
1182 | if (er < 0) { |
1183 | skb_free_datagram(sk, skb); | |
1184 | release_sock(sk); | |
1185 | return er; | |
1186 | } | |
1da177e4 LT |
1187 | |
1188 | if (sax != NULL) { | |
c802d759 | 1189 | memset(sax, 0, sizeof(*sax)); |
1da177e4 | 1190 | sax->sax25_family = AF_NETROM; |
d626f62b ACM |
1191 | skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call, |
1192 | AX25_ADDR_LEN); | |
f3d33426 | 1193 | msg->msg_namelen = sizeof(*sax); |
1da177e4 LT |
1194 | } |
1195 | ||
80e20f6f | 1196 | skb_free_datagram(sk, skb); |
1da177e4 LT |
1197 | |
1198 | release_sock(sk); | |
1199 | return copied; | |
1200 | } | |
1201 | ||
1202 | ||
1203 | static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) | |
1204 | { | |
1205 | struct sock *sk = sock->sk; | |
1206 | void __user *argp = (void __user *)arg; | |
1da177e4 | 1207 | |
1da177e4 LT |
1208 | switch (cmd) { |
1209 | case TIOCOUTQ: { | |
1210 | long amount; | |
5ff7630e CH |
1211 | |
1212 | lock_sock(sk); | |
31e6d363 | 1213 | amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); |
1da177e4 LT |
1214 | if (amount < 0) |
1215 | amount = 0; | |
1216 | release_sock(sk); | |
1217 | return put_user(amount, (int __user *)argp); | |
1218 | } | |
1219 | ||
1220 | case TIOCINQ: { | |
1221 | struct sk_buff *skb; | |
1222 | long amount = 0L; | |
5ff7630e CH |
1223 | |
1224 | lock_sock(sk); | |
1da177e4 LT |
1225 | /* These two are safe on a single CPU system as only user tasks fiddle here */ |
1226 | if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) | |
1227 | amount = skb->len; | |
1228 | release_sock(sk); | |
1229 | return put_user(amount, (int __user *)argp); | |
1230 | } | |
1231 | ||
1da177e4 LT |
1232 | case SIOCGIFADDR: |
1233 | case SIOCSIFADDR: | |
1234 | case SIOCGIFDSTADDR: | |
1235 | case SIOCSIFDSTADDR: | |
1236 | case SIOCGIFBRDADDR: | |
1237 | case SIOCSIFBRDADDR: | |
1238 | case SIOCGIFNETMASK: | |
1239 | case SIOCSIFNETMASK: | |
1240 | case SIOCGIFMETRIC: | |
1241 | case SIOCSIFMETRIC: | |
1da177e4 LT |
1242 | return -EINVAL; |
1243 | ||
1244 | case SIOCADDRT: | |
1245 | case SIOCDELRT: | |
1246 | case SIOCNRDECOBS: | |
0f20f5a7 RB |
1247 | if (!capable(CAP_NET_ADMIN)) |
1248 | return -EPERM; | |
1da177e4 LT |
1249 | return nr_rt_ioctl(cmd, argp); |
1250 | ||
1251 | default: | |
b5e5fa5e | 1252 | return -ENOIOCTLCMD; |
1da177e4 | 1253 | } |
1da177e4 LT |
1254 | |
1255 | return 0; | |
1256 | } | |
1257 | ||
1258 | #ifdef CONFIG_PROC_FS | |
1259 | ||
1260 | static void *nr_info_start(struct seq_file *seq, loff_t *pos) | |
d087f183 | 1261 | __acquires(&nr_list_lock) |
1da177e4 | 1262 | { |
1da177e4 | 1263 | spin_lock_bh(&nr_list_lock); |
90dd7f5a | 1264 | return seq_hlist_start_head(&nr_list, *pos); |
1da177e4 LT |
1265 | } |
1266 | ||
1267 | static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos) | |
1268 | { | |
90dd7f5a | 1269 | return seq_hlist_next(v, &nr_list, pos); |
1da177e4 | 1270 | } |
5f8f59d6 | 1271 | |
1da177e4 | 1272 | static void nr_info_stop(struct seq_file *seq, void *v) |
8b003f0d | 1273 | __releases(&nr_list_lock) |
1da177e4 LT |
1274 | { |
1275 | spin_unlock_bh(&nr_list_lock); | |
1276 | } | |
1277 | ||
1278 | static int nr_info_show(struct seq_file *seq, void *v) | |
1279 | { | |
90dd7f5a | 1280 | struct sock *s = sk_entry(v); |
1da177e4 LT |
1281 | struct net_device *dev; |
1282 | struct nr_sock *nr; | |
1283 | const char *devname; | |
f75268cd | 1284 | char buf[11]; |
1da177e4 LT |
1285 | |
1286 | if (v == SEQ_START_TOKEN) | |
1287 | seq_puts(seq, | |
1288 | "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"); | |
1289 | ||
1290 | else { | |
1291 | ||
1292 | bh_lock_sock(s); | |
1293 | nr = nr_sk(s); | |
1294 | ||
1295 | if ((dev = nr->device) == NULL) | |
1296 | devname = "???"; | |
1297 | else | |
1298 | devname = dev->name; | |
1299 | ||
f75268cd RB |
1300 | seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr)); |
1301 | seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr)); | |
5f8f59d6 | 1302 | seq_printf(seq, |
1da177e4 | 1303 | "%-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", |
f75268cd | 1304 | ax2asc(buf, &nr->source_addr), |
1da177e4 LT |
1305 | devname, |
1306 | nr->my_index, | |
1307 | nr->my_id, | |
1308 | nr->your_index, | |
1309 | nr->your_id, | |
1310 | nr->state, | |
1311 | nr->vs, | |
1312 | nr->vr, | |
1313 | nr->va, | |
1314 | ax25_display_timer(&nr->t1timer) / HZ, | |
1315 | nr->t1 / HZ, | |
1316 | ax25_display_timer(&nr->t2timer) / HZ, | |
1317 | nr->t2 / HZ, | |
1318 | ax25_display_timer(&nr->t4timer) / HZ, | |
1319 | nr->t4 / HZ, | |
1320 | ax25_display_timer(&nr->idletimer) / (60 * HZ), | |
1321 | nr->idle / (60 * HZ), | |
1322 | nr->n2count, | |
1323 | nr->n2, | |
1324 | nr->window, | |
31e6d363 ED |
1325 | sk_wmem_alloc_get(s), |
1326 | sk_rmem_alloc_get(s), | |
1da177e4 LT |
1327 | s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L); |
1328 | ||
1329 | bh_unlock_sock(s); | |
1330 | } | |
1331 | return 0; | |
1332 | } | |
1333 | ||
56b3d975 | 1334 | static const struct seq_operations nr_info_seqops = { |
1da177e4 LT |
1335 | .start = nr_info_start, |
1336 | .next = nr_info_next, | |
1337 | .stop = nr_info_stop, | |
1338 | .show = nr_info_show, | |
1339 | }; | |
1da177e4 LT |
1340 | #endif /* CONFIG_PROC_FS */ |
1341 | ||
ec1b4cf7 | 1342 | static const struct net_proto_family nr_family_ops = { |
1da177e4 LT |
1343 | .family = PF_NETROM, |
1344 | .create = nr_create, | |
1345 | .owner = THIS_MODULE, | |
1346 | }; | |
1347 | ||
90ddc4f0 | 1348 | static const struct proto_ops nr_proto_ops = { |
1da177e4 LT |
1349 | .family = PF_NETROM, |
1350 | .owner = THIS_MODULE, | |
1351 | .release = nr_release, | |
1352 | .bind = nr_bind, | |
1353 | .connect = nr_connect, | |
1354 | .socketpair = sock_no_socketpair, | |
1355 | .accept = nr_accept, | |
1356 | .getname = nr_getname, | |
a11e1d43 | 1357 | .poll = datagram_poll, |
1da177e4 | 1358 | .ioctl = nr_ioctl, |
c7cbdbf2 | 1359 | .gettstamp = sock_gettstamp, |
1da177e4 LT |
1360 | .listen = nr_listen, |
1361 | .shutdown = sock_no_shutdown, | |
1362 | .setsockopt = nr_setsockopt, | |
1363 | .getsockopt = nr_getsockopt, | |
1364 | .sendmsg = nr_sendmsg, | |
1365 | .recvmsg = nr_recvmsg, | |
1366 | .mmap = sock_no_mmap, | |
1367 | .sendpage = sock_no_sendpage, | |
1368 | }; | |
1369 | ||
1370 | static struct notifier_block nr_dev_notifier = { | |
1371 | .notifier_call = nr_device_event, | |
1372 | }; | |
1373 | ||
1374 | static struct net_device **dev_nr; | |
1375 | ||
8d5cf596 RB |
1376 | static struct ax25_protocol nr_pid = { |
1377 | .pid = AX25_P_NETROM, | |
1378 | .func = nr_route_frame | |
1379 | }; | |
1380 | ||
a4282717 RB |
1381 | static struct ax25_linkfail nr_linkfail_notifier = { |
1382 | .func = nr_link_failed, | |
1383 | }; | |
1384 | ||
1da177e4 LT |
1385 | static int __init nr_proto_init(void) |
1386 | { | |
1387 | int i; | |
1388 | int rc = proto_register(&nr_proto, 0); | |
1389 | ||
d3706566 Y |
1390 | if (rc) |
1391 | return rc; | |
1da177e4 LT |
1392 | |
1393 | if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) { | |
d3706566 Y |
1394 | pr_err("NET/ROM: %s - nr_ndevs parameter too large\n", |
1395 | __func__); | |
1396 | rc = -EINVAL; | |
1397 | goto unregister_proto; | |
1da177e4 LT |
1398 | } |
1399 | ||
6396bb22 | 1400 | dev_nr = kcalloc(nr_ndevs, sizeof(struct net_device *), GFP_KERNEL); |
d3706566 Y |
1401 | if (!dev_nr) { |
1402 | pr_err("NET/ROM: %s - unable to allocate device array\n", | |
1403 | __func__); | |
1404 | rc = -ENOMEM; | |
1405 | goto unregister_proto; | |
1da177e4 LT |
1406 | } |
1407 | ||
1da177e4 LT |
1408 | for (i = 0; i < nr_ndevs; i++) { |
1409 | char name[IFNAMSIZ]; | |
1410 | struct net_device *dev; | |
1411 | ||
1412 | sprintf(name, "nr%d", i); | |
c835a677 | 1413 | dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, nr_setup); |
1da177e4 | 1414 | if (!dev) { |
d3706566 | 1415 | rc = -ENOMEM; |
1da177e4 LT |
1416 | goto fail; |
1417 | } | |
5f8f59d6 | 1418 | |
1da177e4 | 1419 | dev->base_addr = i; |
d3706566 Y |
1420 | rc = register_netdev(dev); |
1421 | if (rc) { | |
1da177e4 LT |
1422 | free_netdev(dev); |
1423 | goto fail; | |
1424 | } | |
1a33e10e | 1425 | nr_set_lockdep_key(dev); |
1da177e4 LT |
1426 | dev_nr[i] = dev; |
1427 | } | |
1428 | ||
d3706566 Y |
1429 | rc = sock_register(&nr_family_ops); |
1430 | if (rc) | |
1da177e4 | 1431 | goto fail; |
5f8f59d6 | 1432 | |
d3706566 Y |
1433 | rc = register_netdevice_notifier(&nr_dev_notifier); |
1434 | if (rc) | |
1435 | goto out_sock; | |
1da177e4 | 1436 | |
8d5cf596 | 1437 | ax25_register_pid(&nr_pid); |
a4282717 | 1438 | ax25_linkfail_register(&nr_linkfail_notifier); |
1da177e4 LT |
1439 | |
1440 | #ifdef CONFIG_SYSCTL | |
d3706566 Y |
1441 | rc = nr_register_sysctl(); |
1442 | if (rc) | |
1443 | goto out_sysctl; | |
1da177e4 LT |
1444 | #endif |
1445 | ||
1446 | nr_loopback_init(); | |
1447 | ||
d3706566 Y |
1448 | rc = -ENOMEM; |
1449 | if (!proc_create_seq("nr", 0444, init_net.proc_net, &nr_info_seqops)) | |
1450 | goto proc_remove1; | |
1451 | if (!proc_create_seq("nr_neigh", 0444, init_net.proc_net, | |
1452 | &nr_neigh_seqops)) | |
1453 | goto proc_remove2; | |
1454 | if (!proc_create_seq("nr_nodes", 0444, init_net.proc_net, | |
1455 | &nr_node_seqops)) | |
1456 | goto proc_remove3; | |
1457 | ||
1458 | return 0; | |
1459 | ||
1460 | proc_remove3: | |
1461 | remove_proc_entry("nr_neigh", init_net.proc_net); | |
1462 | proc_remove2: | |
1463 | remove_proc_entry("nr", init_net.proc_net); | |
1464 | proc_remove1: | |
1465 | ||
1466 | nr_loopback_clear(); | |
1467 | nr_rt_free(); | |
1468 | ||
1469 | #ifdef CONFIG_SYSCTL | |
1470 | nr_unregister_sysctl(); | |
1471 | out_sysctl: | |
1472 | #endif | |
1473 | ax25_linkfail_release(&nr_linkfail_notifier); | |
1474 | ax25_protocol_release(AX25_P_NETROM); | |
1475 | unregister_netdevice_notifier(&nr_dev_notifier); | |
1476 | out_sock: | |
1477 | sock_unregister(PF_NETROM); | |
1da177e4 LT |
1478 | fail: |
1479 | while (--i >= 0) { | |
1480 | unregister_netdev(dev_nr[i]); | |
1481 | free_netdev(dev_nr[i]); | |
1482 | } | |
1483 | kfree(dev_nr); | |
d3706566 | 1484 | unregister_proto: |
1da177e4 | 1485 | proto_unregister(&nr_proto); |
d3706566 | 1486 | return rc; |
1da177e4 LT |
1487 | } |
1488 | ||
1489 | module_init(nr_proto_init); | |
1490 | ||
1491 | module_param(nr_ndevs, int, 0); | |
1492 | MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices"); | |
1493 | ||
1494 | MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>"); | |
1495 | MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol"); | |
1496 | MODULE_LICENSE("GPL"); | |
1497 | MODULE_ALIAS_NETPROTO(PF_NETROM); | |
1498 | ||
1499 | static void __exit nr_exit(void) | |
1500 | { | |
1501 | int i; | |
1502 | ||
ece31ffd G |
1503 | remove_proc_entry("nr", init_net.proc_net); |
1504 | remove_proc_entry("nr_neigh", init_net.proc_net); | |
1505 | remove_proc_entry("nr_nodes", init_net.proc_net); | |
1da177e4 LT |
1506 | nr_loopback_clear(); |
1507 | ||
1508 | nr_rt_free(); | |
1509 | ||
1510 | #ifdef CONFIG_SYSCTL | |
1511 | nr_unregister_sysctl(); | |
1512 | #endif | |
1513 | ||
a4282717 | 1514 | ax25_linkfail_release(&nr_linkfail_notifier); |
1da177e4 LT |
1515 | ax25_protocol_release(AX25_P_NETROM); |
1516 | ||
1517 | unregister_netdevice_notifier(&nr_dev_notifier); | |
1518 | ||
1519 | sock_unregister(PF_NETROM); | |
1520 | ||
1521 | for (i = 0; i < nr_ndevs; i++) { | |
1522 | struct net_device *dev = dev_nr[i]; | |
1523 | if (dev) { | |
1524 | unregister_netdev(dev); | |
1525 | free_netdev(dev); | |
1526 | } | |
1527 | } | |
1528 | ||
1529 | kfree(dev_nr); | |
1530 | proto_unregister(&nr_proto); | |
1531 | } | |
1532 | module_exit(nr_exit); |