blob: 819afc449e1e9fcf0e74c6a958133bd7ebdd8e12 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
9 * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/moduleparam.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
15#include <linux/types.h>
16#include <linux/socket.h>
17#include <linux/in.h>
18#include <linux/kernel.h>
19#include <linux/sched.h>
20#include <linux/timer.h>
21#include <linux/string.h>
22#include <linux/sockios.h>
23#include <linux/net.h>
24#include <linux/stat.h>
25#include <net/ax25.h>
26#include <linux/inet.h>
27#include <linux/netdevice.h>
28#include <linux/if_arp.h>
29#include <linux/skbuff.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020030#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/sock.h>
32#include <asm/uaccess.h>
33#include <asm/system.h>
34#include <linux/fcntl.h>
35#include <linux/termios.h> /* For TIOCINQ/OUTQ */
36#include <linux/mm.h>
37#include <linux/interrupt.h>
38#include <linux/notifier.h>
39#include <net/netrom.h>
40#include <linux/proc_fs.h>
41#include <linux/seq_file.h>
42#include <net/ip.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070043#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <net/arp.h>
45#include <linux/init.h>
46
47static int nr_ndevs = 4;
48
49int sysctl_netrom_default_path_quality = NR_DEFAULT_QUAL;
50int sysctl_netrom_obsolescence_count_initialiser = NR_DEFAULT_OBS;
51int sysctl_netrom_network_ttl_initialiser = NR_DEFAULT_TTL;
52int sysctl_netrom_transport_timeout = NR_DEFAULT_T1;
53int sysctl_netrom_transport_maximum_tries = NR_DEFAULT_N2;
54int sysctl_netrom_transport_acknowledge_delay = NR_DEFAULT_T2;
55int sysctl_netrom_transport_busy_delay = NR_DEFAULT_T4;
56int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW;
57int sysctl_netrom_transport_no_activity_timeout = NR_DEFAULT_IDLE;
58int sysctl_netrom_routing_control = NR_DEFAULT_ROUTING;
59int sysctl_netrom_link_fails_count = NR_DEFAULT_FAILS;
Ralf Baechlee21ce8c2005-09-12 14:27:37 -070060int sysctl_netrom_reset_circuit = NR_DEFAULT_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62static unsigned short circuit = 0x101;
63
64static HLIST_HEAD(nr_list);
65static DEFINE_SPINLOCK(nr_list_lock);
66
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080067static const struct proto_ops nr_proto_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
Ralf Baechle4b260a92006-07-12 13:26:26 -070070 * NETROM network devices are virtual network devices encapsulating NETROM
71 * frames into AX.25 which will be sent through an AX.25 device, so form a
72 * special "super class" of normal net devices; split their locks off into a
73 * separate class since they always nest.
74 */
75static struct lock_class_key nr_netdev_xmit_lock_key;
76
David S. Millerc773e842008-07-08 23:13:53 -070077static void nr_set_lockdep_one(struct netdev_queue *txq)
78{
79 lockdep_set_class(&txq->_xmit_lock, &nr_netdev_xmit_lock_key);
80}
81
82static void nr_set_lockdep_key(struct net_device *dev)
83{
84 nr_set_lockdep_one(&dev->tx_queue);
85}
86
Ralf Baechle4b260a92006-07-12 13:26:26 -070087/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * Socket removal during an interrupt is now safe.
89 */
90static void nr_remove_socket(struct sock *sk)
91{
92 spin_lock_bh(&nr_list_lock);
93 sk_del_node_init(sk);
94 spin_unlock_bh(&nr_list_lock);
95}
96
97/*
98 * Kill all bound sockets on a dropped device.
99 */
100static void nr_kill_by_device(struct net_device *dev)
101{
102 struct sock *s;
103 struct hlist_node *node;
104
105 spin_lock_bh(&nr_list_lock);
106 sk_for_each(s, node, &nr_list)
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 */
115static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
116{
117 struct net_device *dev = (struct net_device *)ptr;
118
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900119 if (dev_net(dev) != &init_net)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200120 return NOTIFY_DONE;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (event != NETDEV_DOWN)
123 return NOTIFY_DONE;
124
125 nr_kill_by_device(dev);
126 nr_rt_device_down(dev);
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return NOTIFY_DONE;
129}
130
131/*
132 * Add a socket to the bound sockets list.
133 */
134static void nr_insert_socket(struct sock *sk)
135{
136 spin_lock_bh(&nr_list_lock);
137 sk_add_node(sk, &nr_list);
138 spin_unlock_bh(&nr_list_lock);
139}
140
141/*
142 * Find a socket that wants to accept the Connect Request we just
143 * received.
144 */
145static struct sock *nr_find_listener(ax25_address *addr)
146{
147 struct sock *s;
148 struct hlist_node *node;
149
150 spin_lock_bh(&nr_list_lock);
151 sk_for_each(s, node, &nr_list)
152 if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
153 s->sk_state == TCP_LISTEN) {
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900154 bh_lock_sock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 goto found;
156 }
157 s = NULL;
158found:
159 spin_unlock_bh(&nr_list_lock);
160 return s;
161}
162
163/*
164 * Find a connected NET/ROM socket given my circuit IDs.
165 */
166static struct sock *nr_find_socket(unsigned char index, unsigned char id)
167{
168 struct sock *s;
169 struct hlist_node *node;
170
171 spin_lock_bh(&nr_list_lock);
172 sk_for_each(s, node, &nr_list) {
173 struct nr_sock *nr = nr_sk(s);
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (nr->my_index == index && nr->my_id == id) {
176 bh_lock_sock(s);
177 goto found;
178 }
179 }
180 s = NULL;
181found:
182 spin_unlock_bh(&nr_list_lock);
183 return s;
184}
185
186/*
187 * Find a connected NET/ROM socket given their circuit IDs.
188 */
189static struct sock *nr_find_peer(unsigned char index, unsigned char id,
190 ax25_address *dest)
191{
192 struct sock *s;
193 struct hlist_node *node;
194
195 spin_lock_bh(&nr_list_lock);
196 sk_for_each(s, node, &nr_list) {
197 struct nr_sock *nr = nr_sk(s);
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (nr->your_index == index && nr->your_id == id &&
200 !ax25cmp(&nr->dest_addr, dest)) {
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900201 bh_lock_sock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto found;
203 }
204 }
205 s = NULL;
206found:
207 spin_unlock_bh(&nr_list_lock);
208 return s;
209}
210
211/*
212 * Find next free circuit ID.
213 */
214static unsigned short nr_find_next_circuit(void)
215{
216 unsigned short id = circuit;
217 unsigned char i, j;
218 struct sock *sk;
219
220 for (;;) {
221 i = id / 256;
222 j = id % 256;
223
224 if (i != 0 && j != 0) {
225 if ((sk=nr_find_socket(i, j)) == NULL)
226 break;
227 bh_unlock_sock(sk);
228 }
229
230 id++;
231 }
232
233 return id;
234}
235
236/*
237 * Deferred destroy.
238 */
239void nr_destroy_socket(struct sock *);
240
241/*
242 * Handler for deferred kills.
243 */
244static void nr_destroy_timer(unsigned long data)
245{
246 struct sock *sk=(struct sock *)data;
247 bh_lock_sock(sk);
248 sock_hold(sk);
249 nr_destroy_socket(sk);
250 bh_unlock_sock(sk);
251 sock_put(sk);
252}
253
254/*
255 * This is called from user mode and the timers. Thus it protects itself
256 * against interrupt users but doesn't worry about being called during
257 * work. Once it is removed from the queue no interrupt or bottom half
258 * will touch it and we are (fairly 8-) ) safe.
259 */
260void nr_destroy_socket(struct sock *sk)
261{
262 struct sk_buff *skb;
263
264 nr_remove_socket(sk);
265
266 nr_stop_heartbeat(sk);
267 nr_stop_t1timer(sk);
268 nr_stop_t2timer(sk);
269 nr_stop_t4timer(sk);
270 nr_stop_idletimer(sk);
271
272 nr_clear_queues(sk); /* Flush the queues */
273
274 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
275 if (skb->sk != sk) { /* A pending connection */
276 /* Queue the unaccepted socket for death */
277 sock_set_flag(skb->sk, SOCK_DEAD);
278 nr_start_heartbeat(skb->sk);
279 nr_sk(skb->sk)->state = NR_STATE_0;
280 }
281
282 kfree_skb(skb);
283 }
284
285 if (atomic_read(&sk->sk_wmem_alloc) ||
286 atomic_read(&sk->sk_rmem_alloc)) {
287 /* Defer: outstanding buffers */
288 sk->sk_timer.function = nr_destroy_timer;
289 sk->sk_timer.expires = jiffies + 2 * HZ;
290 add_timer(&sk->sk_timer);
291 } else
292 sock_put(sk);
293}
294
295/*
296 * Handling for system calls applied via the various interfaces to a
297 * NET/ROM socket object.
298 */
299
300static int nr_setsockopt(struct socket *sock, int level, int optname,
301 char __user *optval, int optlen)
302{
303 struct sock *sk = sock->sk;
304 struct nr_sock *nr = nr_sk(sk);
305 int opt;
306
307 if (level != SOL_NETROM)
308 return -ENOPROTOOPT;
309
310 if (optlen < sizeof(int))
311 return -EINVAL;
312
313 if (get_user(opt, (int __user *)optval))
314 return -EFAULT;
315
316 switch (optname) {
317 case NETROM_T1:
318 if (opt < 1)
319 return -EINVAL;
320 nr->t1 = opt * HZ;
321 return 0;
322
323 case NETROM_T2:
324 if (opt < 1)
325 return -EINVAL;
326 nr->t2 = opt * HZ;
327 return 0;
328
329 case NETROM_N2:
330 if (opt < 1 || opt > 31)
331 return -EINVAL;
332 nr->n2 = opt;
333 return 0;
334
335 case NETROM_T4:
336 if (opt < 1)
337 return -EINVAL;
338 nr->t4 = opt * HZ;
339 return 0;
340
341 case NETROM_IDLE:
342 if (opt < 0)
343 return -EINVAL;
344 nr->idle = opt * 60 * HZ;
345 return 0;
346
347 default:
348 return -ENOPROTOOPT;
349 }
350}
351
352static int nr_getsockopt(struct socket *sock, int level, int optname,
353 char __user *optval, int __user *optlen)
354{
355 struct sock *sk = sock->sk;
356 struct nr_sock *nr = nr_sk(sk);
357 int val = 0;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900358 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (level != SOL_NETROM)
361 return -ENOPROTOOPT;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (get_user(len, optlen))
364 return -EFAULT;
365
366 if (len < 0)
367 return -EINVAL;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 switch (optname) {
370 case NETROM_T1:
371 val = nr->t1 / HZ;
372 break;
373
374 case NETROM_T2:
375 val = nr->t2 / HZ;
376 break;
377
378 case NETROM_N2:
379 val = nr->n2;
380 break;
381
382 case NETROM_T4:
383 val = nr->t4 / HZ;
384 break;
385
386 case NETROM_IDLE:
387 val = nr->idle / (60 * HZ);
388 break;
389
390 default:
391 return -ENOPROTOOPT;
392 }
393
394 len = min_t(unsigned int, len, sizeof(int));
395
396 if (put_user(len, optlen))
397 return -EFAULT;
398
399 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
400}
401
402static int nr_listen(struct socket *sock, int backlog)
403{
404 struct sock *sk = sock->sk;
405
406 lock_sock(sk);
407 if (sk->sk_state != TCP_LISTEN) {
408 memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
409 sk->sk_max_ack_backlog = backlog;
410 sk->sk_state = TCP_LISTEN;
411 release_sock(sk);
412 return 0;
413 }
414 release_sock(sk);
415
416 return -EOPNOTSUPP;
417}
418
419static struct proto nr_proto = {
420 .name = "NETROM",
421 .owner = THIS_MODULE,
422 .obj_size = sizeof(struct nr_sock),
423};
424
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700425static int nr_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct sock *sk;
428 struct nr_sock *nr;
429
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700430 if (net != &init_net)
431 return -EAFNOSUPPORT;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (sock->type != SOCK_SEQPACKET || protocol != 0)
434 return -ESOCKTNOSUPPORT;
435
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700436 sk = sk_alloc(net, PF_NETROM, GFP_ATOMIC, &nr_proto);
437 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return -ENOMEM;
439
440 nr = nr_sk(sk);
441
442 sock_init_data(sock, sk);
443
444 sock->ops = &nr_proto_ops;
445 sk->sk_protocol = protocol;
446
447 skb_queue_head_init(&nr->ack_queue);
448 skb_queue_head_init(&nr->reseq_queue);
449 skb_queue_head_init(&nr->frag_queue);
450
451 nr_init_timers(sk);
452
Ralf Baechle4d8937d2006-05-03 23:27:47 -0700453 nr->t1 =
454 msecs_to_jiffies(sysctl_netrom_transport_timeout);
455 nr->t2 =
456 msecs_to_jiffies(sysctl_netrom_transport_acknowledge_delay);
457 nr->n2 =
458 msecs_to_jiffies(sysctl_netrom_transport_maximum_tries);
459 nr->t4 =
460 msecs_to_jiffies(sysctl_netrom_transport_busy_delay);
461 nr->idle =
462 msecs_to_jiffies(sysctl_netrom_transport_no_activity_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 nr->window = sysctl_netrom_transport_requested_window_size;
464
465 nr->bpqext = 1;
466 nr->state = NR_STATE_0;
467
468 return 0;
469}
470
471static struct sock *nr_make_new(struct sock *osk)
472{
473 struct sock *sk;
474 struct nr_sock *nr, *onr;
475
476 if (osk->sk_type != SOCK_SEQPACKET)
477 return NULL;
478
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900479 sk = sk_alloc(sock_net(osk), PF_NETROM, GFP_ATOMIC, osk->sk_prot);
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700480 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return NULL;
482
483 nr = nr_sk(sk);
484
485 sock_init_data(NULL, sk);
486
487 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 sk->sk_priority = osk->sk_priority;
489 sk->sk_protocol = osk->sk_protocol;
490 sk->sk_rcvbuf = osk->sk_rcvbuf;
491 sk->sk_sndbuf = osk->sk_sndbuf;
492 sk->sk_state = TCP_ESTABLISHED;
Ralf Baechle53b924b2005-08-23 10:11:30 -0700493 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 skb_queue_head_init(&nr->ack_queue);
496 skb_queue_head_init(&nr->reseq_queue);
497 skb_queue_head_init(&nr->frag_queue);
498
499 nr_init_timers(sk);
500
501 onr = nr_sk(osk);
502
503 nr->t1 = onr->t1;
504 nr->t2 = onr->t2;
505 nr->n2 = onr->n2;
506 nr->t4 = onr->t4;
507 nr->idle = onr->idle;
508 nr->window = onr->window;
509
510 nr->device = onr->device;
511 nr->bpqext = onr->bpqext;
512
513 return sk;
514}
515
516static int nr_release(struct socket *sock)
517{
518 struct sock *sk = sock->sk;
519 struct nr_sock *nr;
520
521 if (sk == NULL) return 0;
522
523 sock_hold(sk);
524 lock_sock(sk);
525 nr = nr_sk(sk);
526
527 switch (nr->state) {
528 case NR_STATE_0:
529 case NR_STATE_1:
530 case NR_STATE_2:
531 nr_disconnect(sk, 0);
532 nr_destroy_socket(sk);
533 break;
534
535 case NR_STATE_3:
536 nr_clear_queues(sk);
537 nr->n2count = 0;
538 nr_write_internal(sk, NR_DISCREQ);
539 nr_start_t1timer(sk);
540 nr_stop_t2timer(sk);
541 nr_stop_t4timer(sk);
542 nr_stop_idletimer(sk);
543 nr->state = NR_STATE_2;
544 sk->sk_state = TCP_CLOSE;
545 sk->sk_shutdown |= SEND_SHUTDOWN;
546 sk->sk_state_change(sk);
547 sock_orphan(sk);
548 sock_set_flag(sk, SOCK_DESTROY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
550
551 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
553 }
554
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900555 sock->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 release_sock(sk);
557 sock_put(sk);
558
559 return 0;
560}
561
562static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
563{
564 struct sock *sk = sock->sk;
565 struct nr_sock *nr = nr_sk(sk);
566 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
567 struct net_device *dev;
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700568 ax25_uid_assoc *user;
569 ax25_address *source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 lock_sock(sk);
572 if (!sock_flag(sk, SOCK_ZAPPED)) {
573 release_sock(sk);
574 return -EINVAL;
575 }
576 if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
577 release_sock(sk);
578 return -EINVAL;
579 }
580 if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
581 release_sock(sk);
582 return -EINVAL;
583 }
584 if (addr->fsa_ax25.sax25_family != AF_NETROM) {
585 release_sock(sk);
586 return -EINVAL;
587 }
588 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
589 SOCK_DEBUG(sk, "NET/ROM: bind failed: invalid node callsign\n");
590 release_sock(sk);
591 return -EADDRNOTAVAIL;
592 }
593
594 /*
595 * Only the super user can set an arbitrary user callsign.
596 */
597 if (addr->fsa_ax25.sax25_ndigis == 1) {
598 if (!capable(CAP_NET_BIND_SERVICE)) {
599 dev_put(dev);
600 release_sock(sk);
601 return -EACCES;
602 }
603 nr->user_addr = addr->fsa_digipeater[0];
604 nr->source_addr = addr->fsa_ax25.sax25_call;
605 } else {
606 source = &addr->fsa_ax25.sax25_call;
607
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700608 user = ax25_findbyuid(current->euid);
609 if (user) {
610 nr->user_addr = user->call;
611 ax25_uid_put(user);
612 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
614 release_sock(sk);
615 dev_put(dev);
616 return -EPERM;
617 }
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700618 nr->user_addr = *source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 nr->source_addr = *source;
622 }
623
624 nr->device = dev;
625 nr_insert_socket(sk);
626
627 sock_reset_flag(sk, SOCK_ZAPPED);
628 dev_put(dev);
629 release_sock(sk);
630 SOCK_DEBUG(sk, "NET/ROM: socket is bound\n");
631 return 0;
632}
633
634static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
635 int addr_len, int flags)
636{
637 struct sock *sk = sock->sk;
638 struct nr_sock *nr = nr_sk(sk);
639 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700640 ax25_address *source = NULL;
641 ax25_uid_assoc *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct net_device *dev;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700643 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 lock_sock(sk);
646 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
647 sock->state = SS_CONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700648 goto out_release; /* Connect completed during a ERESTARTSYS event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
651 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
652 sock->state = SS_UNCONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700653 err = -ECONNREFUSED;
654 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
657 if (sk->sk_state == TCP_ESTABLISHED) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700658 err = -EISCONN; /* No reconnect on a seqpacket socket */
659 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900662 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 sock->state = SS_UNCONNECTED;
664
665 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700666 err = -EINVAL;
667 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 if (addr->sax25_family != AF_NETROM) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700670 err = -EINVAL;
671 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
674 sock_reset_flag(sk, SOCK_ZAPPED);
675
676 if ((dev = nr_dev_first()) == NULL) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700677 err = -ENETUNREACH;
678 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 source = (ax25_address *)dev->dev_addr;
681
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700682 user = ax25_findbyuid(current->euid);
683 if (user) {
684 nr->user_addr = user->call;
685 ax25_uid_put(user);
686 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
688 dev_put(dev);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700689 err = -EPERM;
690 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700692 nr->user_addr = *source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 nr->source_addr = *source;
696 nr->device = dev;
697
698 dev_put(dev);
699 nr_insert_socket(sk); /* Finish the bind */
700 }
701
702 nr->dest_addr = addr->sax25_call;
703
704 release_sock(sk);
705 circuit = nr_find_next_circuit();
706 lock_sock(sk);
707
708 nr->my_index = circuit / 256;
709 nr->my_id = circuit % 256;
710
711 circuit++;
712
713 /* Move to connecting socket, start sending Connect Requests */
714 sock->state = SS_CONNECTING;
715 sk->sk_state = TCP_SYN_SENT;
716
717 nr_establish_data_link(sk);
718
719 nr->state = NR_STATE_1;
720
721 nr_start_heartbeat(sk);
722
723 /* Now the loop */
724 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700725 err = -EINPROGRESS;
726 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /*
730 * A Connect Ack with Choke or timeout or failed routing will go to
731 * closed.
732 */
733 if (sk->sk_state == TCP_SYN_SENT) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700734 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 for (;;) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700737 prepare_to_wait(sk->sk_sleep, &wait,
YOSHIFUJI Hideaki639fc4c2007-07-19 10:44:32 +0900738 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (sk->sk_state != TCP_SYN_SENT)
740 break;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700741 if (!signal_pending(current)) {
742 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 schedule();
744 lock_sock(sk);
745 continue;
746 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700747 err = -ERESTARTSYS;
748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700750 finish_wait(sk->sk_sleep, &wait);
751 if (err)
752 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 if (sk->sk_state != TCP_ESTABLISHED) {
756 sock->state = SS_UNCONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700757 err = sock_error(sk); /* Always set at this point */
758 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 sock->state = SS_CONNECTED;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700762
763out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 release_sock(sk);
765
Ralf Baechle75606dc2007-04-20 16:06:45 -0700766 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
770{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct sk_buff *skb;
772 struct sock *newsk;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700773 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 struct sock *sk;
775 int err = 0;
776
777 if ((sk = sock->sk) == NULL)
778 return -EINVAL;
779
780 lock_sock(sk);
781 if (sk->sk_type != SOCK_SEQPACKET) {
782 err = -EOPNOTSUPP;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700783 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
786 if (sk->sk_state != TCP_LISTEN) {
787 err = -EINVAL;
Ralf Baechle75606dc2007-04-20 16:06:45 -0700788 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
791 /*
792 * The write queue this time is holding sockets ready to use
793 * hooked into the SABM we saved
794 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 for (;;) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700796 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 skb = skb_dequeue(&sk->sk_receive_queue);
798 if (skb)
799 break;
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (flags & O_NONBLOCK) {
Ralf Baechle75606dc2007-04-20 16:06:45 -0700802 err = -EWOULDBLOCK;
803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700805 if (!signal_pending(current)) {
806 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 schedule();
808 lock_sock(sk);
809 continue;
810 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700811 err = -ERESTARTSYS;
812 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Ralf Baechle75606dc2007-04-20 16:06:45 -0700814 finish_wait(sk->sk_sleep, &wait);
815 if (err)
816 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 newsk = skb->sk;
David S. Miller7b667672008-06-17 02:36:44 -0700819 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* Now attach up the new socket */
822 kfree_skb(skb);
Ralf Baechle DL5RB18601a72006-07-03 19:31:14 -0700823 sk_acceptq_removed(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Ralf Baechle75606dc2007-04-20 16:06:45 -0700825out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 release_sock(sk);
Ralf Baechle75606dc2007-04-20 16:06:45 -0700827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return err;
829}
830
831static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
832 int *uaddr_len, int peer)
833{
834 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
835 struct sock *sk = sock->sk;
836 struct nr_sock *nr = nr_sk(sk);
837
838 lock_sock(sk);
839 if (peer != 0) {
840 if (sk->sk_state != TCP_ESTABLISHED) {
841 release_sock(sk);
842 return -ENOTCONN;
843 }
844 sax->fsa_ax25.sax25_family = AF_NETROM;
845 sax->fsa_ax25.sax25_ndigis = 1;
846 sax->fsa_ax25.sax25_call = nr->user_addr;
847 sax->fsa_digipeater[0] = nr->dest_addr;
848 *uaddr_len = sizeof(struct full_sockaddr_ax25);
849 } else {
850 sax->fsa_ax25.sax25_family = AF_NETROM;
851 sax->fsa_ax25.sax25_ndigis = 0;
852 sax->fsa_ax25.sax25_call = nr->source_addr;
853 *uaddr_len = sizeof(struct sockaddr_ax25);
854 }
855 release_sock(sk);
856
857 return 0;
858}
859
860int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
861{
862 struct sock *sk;
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +0900863 struct sock *make;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct nr_sock *nr_make;
865 ax25_address *src, *dest, *user;
866 unsigned short circuit_index, circuit_id;
867 unsigned short peer_circuit_index, peer_circuit_id;
868 unsigned short frametype, flags, window, timeout;
869 int ret;
870
871 skb->sk = NULL; /* Initially we don't know who it's for */
872
873 /*
874 * skb->data points to the netrom frame start
875 */
876
877 src = (ax25_address *)(skb->data + 0);
878 dest = (ax25_address *)(skb->data + 7);
879
880 circuit_index = skb->data[15];
881 circuit_id = skb->data[16];
882 peer_circuit_index = skb->data[17];
883 peer_circuit_id = skb->data[18];
884 frametype = skb->data[19] & 0x0F;
885 flags = skb->data[19] & 0xF0;
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /*
888 * Check for an incoming IP over NET/ROM frame.
889 */
Ralf Baechle98a82fe2005-08-24 11:35:51 -0700890 if (frametype == NR_PROTOEXT &&
891 circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300893 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 return nr_rx_ip(skb, dev);
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /*
899 * Find an existing socket connection, based on circuit ID, if it's
900 * a Connect Request base it on their circuit ID.
901 *
902 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
903 * circuit that no longer exists at the other end ...
904 */
905
906 sk = NULL;
907
908 if (circuit_index == 0 && circuit_id == 0) {
909 if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
910 sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
911 } else {
912 if (frametype == NR_CONNREQ)
913 sk = nr_find_peer(circuit_index, circuit_id, src);
914 else
915 sk = nr_find_socket(circuit_index, circuit_id);
916 }
917
918 if (sk != NULL) {
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);
928 return ret;
929 }
930
931 /*
932 * Now it should be a CONNREQ.
933 */
934 if (frametype != NR_CONNREQ) {
935 /*
936 * Here it would be nice to be able to send a reset but
Ralf Baechlee21ce8c2005-09-12 14:27:37 -0700937 * NET/ROM doesn't have one. We've tried to extend the protocol
938 * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
939 * apparently kills BPQ boxes... :-(
940 * So now we try to follow the established behaviour of
941 * G8PZT's Xrouter which is sending packets with command type 7
942 * as an extension of the protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
Ralf Baechlee21ce8c2005-09-12 14:27:37 -0700944 if (sysctl_netrom_reset_circuit &&
945 (frametype != NR_RESET || flags != 0))
946 nr_transmit_reset(skb, 1);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return 0;
949 }
950
951 sk = nr_find_listener(dest);
952
953 user = (ax25_address *)(skb->data + 21);
954
955 if (sk == NULL || sk_acceptq_is_full(sk) ||
956 (make = nr_make_new(sk)) == NULL) {
957 nr_transmit_refusal(skb, 0);
958 if (sk)
959 bh_unlock_sock(sk);
960 return 0;
961 }
962
963 window = skb->data[20];
964
965 skb->sk = make;
966 make->sk_state = TCP_ESTABLISHED;
967
968 /* Fill in his circuit details */
969 nr_make = nr_sk(make);
970 nr_make->source_addr = *dest;
971 nr_make->dest_addr = *src;
972 nr_make->user_addr = *user;
973
974 nr_make->your_index = circuit_index;
975 nr_make->your_id = circuit_id;
976
977 bh_unlock_sock(sk);
978 circuit = nr_find_next_circuit();
979 bh_lock_sock(sk);
980
981 nr_make->my_index = circuit / 256;
982 nr_make->my_id = circuit % 256;
983
984 circuit++;
985
986 /* Window negotiation */
987 if (window < nr_make->window)
988 nr_make->window = window;
989
990 /* L4 timeout negotiation */
991 if (skb->len == 37) {
992 timeout = skb->data[36] * 256 + skb->data[35];
993 if (timeout * HZ < nr_make->t1)
994 nr_make->t1 = timeout * HZ;
995 nr_make->bpqext = 1;
996 } else {
997 nr_make->bpqext = 0;
998 }
999
1000 nr_write_internal(make, NR_CONNACK);
1001
1002 nr_make->condition = 0x00;
1003 nr_make->vs = 0;
1004 nr_make->va = 0;
1005 nr_make->vr = 0;
1006 nr_make->vl = 0;
1007 nr_make->state = NR_STATE_3;
Ralf Baechle DL5RB18601a72006-07-03 19:31:14 -07001008 sk_acceptq_added(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 skb_queue_head(&sk->sk_receive_queue, skb);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (!sock_flag(sk, SOCK_DEAD))
1012 sk->sk_data_ready(sk, skb->len);
1013
1014 bh_unlock_sock(sk);
Ralf Baechle5cc29e32006-07-10 16:23:21 -07001015
1016 nr_insert_socket(make);
1017
1018 nr_start_heartbeat(make);
1019 nr_start_idletimer(make);
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return 1;
1022}
1023
1024static int nr_sendmsg(struct kiocb *iocb, struct socket *sock,
1025 struct msghdr *msg, size_t len)
1026{
1027 struct sock *sk = sock->sk;
1028 struct nr_sock *nr = nr_sk(sk);
1029 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1030 int err;
1031 struct sockaddr_ax25 sax;
1032 struct sk_buff *skb;
1033 unsigned char *asmptr;
1034 int size;
1035
1036 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1037 return -EINVAL;
1038
1039 lock_sock(sk);
1040 if (sock_flag(sk, SOCK_ZAPPED)) {
1041 err = -EADDRNOTAVAIL;
1042 goto out;
1043 }
1044
1045 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1046 send_sig(SIGPIPE, current, 0);
1047 err = -EPIPE;
1048 goto out;
1049 }
1050
1051 if (nr->device == NULL) {
1052 err = -ENETUNREACH;
1053 goto out;
1054 }
1055
1056 if (usax) {
1057 if (msg->msg_namelen < sizeof(sax)) {
1058 err = -EINVAL;
1059 goto out;
1060 }
1061 sax = *usax;
1062 if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
1063 err = -EISCONN;
1064 goto out;
1065 }
1066 if (sax.sax25_family != AF_NETROM) {
1067 err = -EINVAL;
1068 goto out;
1069 }
1070 } else {
1071 if (sk->sk_state != TCP_ESTABLISHED) {
1072 err = -ENOTCONN;
1073 goto out;
1074 }
1075 sax.sax25_family = AF_NETROM;
1076 sax.sax25_call = nr->dest_addr;
1077 }
1078
1079 SOCK_DEBUG(sk, "NET/ROM: sendto: Addresses built.\n");
1080
1081 /* Build a packet */
1082 SOCK_DEBUG(sk, "NET/ROM: sendto: building packet.\n");
1083 size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1084
1085 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1086 goto out;
1087
1088 skb_reserve(skb, size - len);
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001089 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /*
1092 * Push down the NET/ROM header
1093 */
1094
1095 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1096 SOCK_DEBUG(sk, "Building NET/ROM Header.\n");
1097
1098 /* Build a NET/ROM Transport header */
1099
1100 *asmptr++ = nr->your_index;
1101 *asmptr++ = nr->your_id;
1102 *asmptr++ = 0; /* To be filled in later */
1103 *asmptr++ = 0; /* Ditto */
1104 *asmptr++ = NR_INFO;
1105 SOCK_DEBUG(sk, "Built header.\n");
1106
1107 /*
1108 * Put the data on the end
1109 */
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001110 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 SOCK_DEBUG(sk, "NET/ROM: Appending user data\n");
1113
1114 /* User data follows immediately after the NET/ROM transport header */
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001115 if (memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 kfree_skb(skb);
1117 err = -EFAULT;
1118 goto out;
1119 }
1120
1121 SOCK_DEBUG(sk, "NET/ROM: Transmitting buffer\n");
1122
1123 if (sk->sk_state != TCP_ESTABLISHED) {
1124 kfree_skb(skb);
1125 err = -ENOTCONN;
1126 goto out;
1127 }
1128
1129 nr_output(sk, skb); /* Shove it onto the queue */
1130
1131 err = len;
1132out:
1133 release_sock(sk);
1134 return err;
1135}
1136
1137static int nr_recvmsg(struct kiocb *iocb, struct socket *sock,
1138 struct msghdr *msg, size_t size, int flags)
1139{
1140 struct sock *sk = sock->sk;
1141 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1142 size_t copied;
1143 struct sk_buff *skb;
1144 int er;
1145
1146 /*
1147 * This works for seqpacket too. The receiver has ordered the queue for
1148 * us! We do one quick check first though
1149 */
1150
1151 lock_sock(sk);
1152 if (sk->sk_state != TCP_ESTABLISHED) {
1153 release_sock(sk);
1154 return -ENOTCONN;
1155 }
1156
1157 /* Now we can treat all alike */
1158 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
1159 release_sock(sk);
1160 return er;
1161 }
1162
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001163 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 copied = skb->len;
1165
1166 if (copied > size) {
1167 copied = size;
1168 msg->msg_flags |= MSG_TRUNC;
1169 }
1170
1171 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1172
1173 if (sax != NULL) {
1174 sax->sax25_family = AF_NETROM;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001175 skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call,
1176 AX25_ADDR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178
1179 msg->msg_namelen = sizeof(*sax);
1180
1181 skb_free_datagram(sk, skb);
1182
1183 release_sock(sk);
1184 return copied;
1185}
1186
1187
1188static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1189{
1190 struct sock *sk = sock->sk;
1191 void __user *argp = (void __user *)arg;
1192 int ret;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 switch (cmd) {
1195 case TIOCOUTQ: {
1196 long amount;
Christoph Hellwig5ff76302006-01-03 14:14:46 -08001197
1198 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1200 if (amount < 0)
1201 amount = 0;
1202 release_sock(sk);
1203 return put_user(amount, (int __user *)argp);
1204 }
1205
1206 case TIOCINQ: {
1207 struct sk_buff *skb;
1208 long amount = 0L;
Christoph Hellwig5ff76302006-01-03 14:14:46 -08001209
1210 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* These two are safe on a single CPU system as only user tasks fiddle here */
1212 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1213 amount = skb->len;
1214 release_sock(sk);
1215 return put_user(amount, (int __user *)argp);
1216 }
1217
1218 case SIOCGSTAMP:
Christoph Hellwig5ff76302006-01-03 14:14:46 -08001219 lock_sock(sk);
Ralf Baechle9b37ee72005-09-12 14:23:52 -07001220 ret = sock_get_timestamp(sk, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 release_sock(sk);
1222 return ret;
1223
Eric Dumazetae40eb12007-03-18 17:33:16 -07001224 case SIOCGSTAMPNS:
1225 lock_sock(sk);
1226 ret = sock_get_timestampns(sk, argp);
1227 release_sock(sk);
1228 return ret;
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 case SIOCGIFADDR:
1231 case SIOCSIFADDR:
1232 case SIOCGIFDSTADDR:
1233 case SIOCSIFDSTADDR:
1234 case SIOCGIFBRDADDR:
1235 case SIOCSIFBRDADDR:
1236 case SIOCGIFNETMASK:
1237 case SIOCSIFNETMASK:
1238 case SIOCGIFMETRIC:
1239 case SIOCSIFMETRIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return -EINVAL;
1241
1242 case SIOCADDRT:
1243 case SIOCDELRT:
1244 case SIOCNRDECOBS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1246 return nr_rt_ioctl(cmd, argp);
1247
1248 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001249 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 return 0;
1253}
1254
1255#ifdef CONFIG_PROC_FS
1256
1257static void *nr_info_start(struct seq_file *seq, loff_t *pos)
1258{
1259 struct sock *s;
1260 struct hlist_node *node;
1261 int i = 1;
1262
1263 spin_lock_bh(&nr_list_lock);
1264 if (*pos == 0)
1265 return SEQ_START_TOKEN;
1266
1267 sk_for_each(s, node, &nr_list) {
1268 if (i == *pos)
1269 return s;
1270 ++i;
1271 }
1272 return NULL;
1273}
1274
1275static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
1276{
1277 ++*pos;
1278
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001279 return (v == SEQ_START_TOKEN) ? sk_head(&nr_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 : sk_next((struct sock *)v);
1281}
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static void nr_info_stop(struct seq_file *seq, void *v)
1284{
1285 spin_unlock_bh(&nr_list_lock);
1286}
1287
1288static int nr_info_show(struct seq_file *seq, void *v)
1289{
1290 struct sock *s = v;
1291 struct net_device *dev;
1292 struct nr_sock *nr;
1293 const char *devname;
Ralf Baechlef75268c2005-09-06 15:49:39 -07001294 char buf[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (v == SEQ_START_TOKEN)
1297 seq_puts(seq,
1298"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");
1299
1300 else {
1301
1302 bh_lock_sock(s);
1303 nr = nr_sk(s);
1304
1305 if ((dev = nr->device) == NULL)
1306 devname = "???";
1307 else
1308 devname = dev->name;
1309
Ralf Baechlef75268c2005-09-06 15:49:39 -07001310 seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr));
1311 seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr));
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001312 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313"%-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 -07001314 ax2asc(buf, &nr->source_addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 devname,
1316 nr->my_index,
1317 nr->my_id,
1318 nr->your_index,
1319 nr->your_id,
1320 nr->state,
1321 nr->vs,
1322 nr->vr,
1323 nr->va,
1324 ax25_display_timer(&nr->t1timer) / HZ,
1325 nr->t1 / HZ,
1326 ax25_display_timer(&nr->t2timer) / HZ,
1327 nr->t2 / HZ,
1328 ax25_display_timer(&nr->t4timer) / HZ,
1329 nr->t4 / HZ,
1330 ax25_display_timer(&nr->idletimer) / (60 * HZ),
1331 nr->idle / (60 * HZ),
1332 nr->n2count,
1333 nr->n2,
1334 nr->window,
1335 atomic_read(&s->sk_wmem_alloc),
1336 atomic_read(&s->sk_rmem_alloc),
1337 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1338
1339 bh_unlock_sock(s);
1340 }
1341 return 0;
1342}
1343
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001344static const struct seq_operations nr_info_seqops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 .start = nr_info_start,
1346 .next = nr_info_next,
1347 .stop = nr_info_stop,
1348 .show = nr_info_show,
1349};
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static int nr_info_open(struct inode *inode, struct file *file)
1352{
1353 return seq_open(file, &nr_info_seqops);
1354}
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001355
Arjan van de Venda7071d2007-02-12 00:55:36 -08001356static const struct file_operations nr_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 .owner = THIS_MODULE,
1358 .open = nr_info_open,
1359 .read = seq_read,
1360 .llseek = seq_lseek,
1361 .release = seq_release,
1362};
1363#endif /* CONFIG_PROC_FS */
1364
1365static struct net_proto_family nr_family_ops = {
1366 .family = PF_NETROM,
1367 .create = nr_create,
1368 .owner = THIS_MODULE,
1369};
1370
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001371static const struct proto_ops nr_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 .family = PF_NETROM,
1373 .owner = THIS_MODULE,
1374 .release = nr_release,
1375 .bind = nr_bind,
1376 .connect = nr_connect,
1377 .socketpair = sock_no_socketpair,
1378 .accept = nr_accept,
1379 .getname = nr_getname,
1380 .poll = datagram_poll,
1381 .ioctl = nr_ioctl,
1382 .listen = nr_listen,
1383 .shutdown = sock_no_shutdown,
1384 .setsockopt = nr_setsockopt,
1385 .getsockopt = nr_getsockopt,
1386 .sendmsg = nr_sendmsg,
1387 .recvmsg = nr_recvmsg,
1388 .mmap = sock_no_mmap,
1389 .sendpage = sock_no_sendpage,
1390};
1391
1392static struct notifier_block nr_dev_notifier = {
1393 .notifier_call = nr_device_event,
1394};
1395
1396static struct net_device **dev_nr;
1397
Ralf Baechle8d5cf592006-12-14 15:50:01 -08001398static struct ax25_protocol nr_pid = {
1399 .pid = AX25_P_NETROM,
1400 .func = nr_route_frame
1401};
1402
Ralf Baechlea4282712006-12-14 15:51:23 -08001403static struct ax25_linkfail nr_linkfail_notifier = {
1404 .func = nr_link_failed,
1405};
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407static int __init nr_proto_init(void)
1408{
1409 int i;
1410 int rc = proto_register(&nr_proto, 0);
1411
1412 if (rc != 0)
1413 goto out;
1414
1415 if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
1416 printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
1417 return -1;
1418 }
1419
Ralf Baechle1b30dd32006-07-09 12:14:22 -07001420 dev_nr = kzalloc(nr_ndevs * sizeof(struct net_device *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (dev_nr == NULL) {
1422 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device array\n");
1423 return -1;
1424 }
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 for (i = 0; i < nr_ndevs; i++) {
1427 char name[IFNAMSIZ];
1428 struct net_device *dev;
1429
1430 sprintf(name, "nr%d", i);
Ralf Baechleb88a7622005-09-12 14:28:03 -07001431 dev = alloc_netdev(sizeof(struct nr_private), name, nr_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (!dev) {
1433 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
1434 goto fail;
1435 }
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 dev->base_addr = i;
1438 if (register_netdev(dev)) {
1439 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n");
1440 free_netdev(dev);
1441 goto fail;
1442 }
David S. Millerc773e842008-07-08 23:13:53 -07001443 nr_set_lockdep_key(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 dev_nr[i] = dev;
1445 }
1446
1447 if (sock_register(&nr_family_ops)) {
1448 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n");
1449 goto fail;
1450 }
YOSHIFUJI Hideaki5f8f59d2007-02-09 23:25:09 +09001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 register_netdevice_notifier(&nr_dev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Ralf Baechle8d5cf592006-12-14 15:50:01 -08001454 ax25_register_pid(&nr_pid);
Ralf Baechlea4282712006-12-14 15:51:23 -08001455 ax25_linkfail_register(&nr_linkfail_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457#ifdef CONFIG_SYSCTL
1458 nr_register_sysctl();
1459#endif
1460
1461 nr_loopback_init();
1462
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001463 proc_net_fops_create(&init_net, "nr", S_IRUGO, &nr_info_fops);
1464 proc_net_fops_create(&init_net, "nr_neigh", S_IRUGO, &nr_neigh_fops);
1465 proc_net_fops_create(&init_net, "nr_nodes", S_IRUGO, &nr_nodes_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466out:
1467 return rc;
1468fail:
1469 while (--i >= 0) {
1470 unregister_netdev(dev_nr[i]);
1471 free_netdev(dev_nr[i]);
1472 }
1473 kfree(dev_nr);
1474 proto_unregister(&nr_proto);
1475 rc = -1;
1476 goto out;
1477}
1478
1479module_init(nr_proto_init);
1480
1481module_param(nr_ndevs, int, 0);
1482MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1483
1484MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1485MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1486MODULE_LICENSE("GPL");
1487MODULE_ALIAS_NETPROTO(PF_NETROM);
1488
1489static void __exit nr_exit(void)
1490{
1491 int i;
1492
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001493 proc_net_remove(&init_net, "nr");
1494 proc_net_remove(&init_net, "nr_neigh");
1495 proc_net_remove(&init_net, "nr_nodes");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 nr_loopback_clear();
1497
1498 nr_rt_free();
1499
1500#ifdef CONFIG_SYSCTL
1501 nr_unregister_sysctl();
1502#endif
1503
Ralf Baechlea4282712006-12-14 15:51:23 -08001504 ax25_linkfail_release(&nr_linkfail_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 ax25_protocol_release(AX25_P_NETROM);
1506
1507 unregister_netdevice_notifier(&nr_dev_notifier);
1508
1509 sock_unregister(PF_NETROM);
1510
1511 for (i = 0; i < nr_ndevs; i++) {
1512 struct net_device *dev = dev_nr[i];
1513 if (dev) {
1514 unregister_netdev(dev);
1515 free_netdev(dev);
1516 }
1517 }
1518
1519 kfree(dev_nr);
1520 proto_unregister(&nr_proto);
1521}
1522module_exit(nr_exit);