aboutsummaryrefslogtreecommitdiffstats
path: root/net/netrom
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /net/netrom
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'net/netrom')
-rw-r--r--net/netrom/Makefile9
-rw-r--r--net/netrom/af_netrom.c1485
-rw-r--r--net/netrom/nr_dev.c220
-rw-r--r--net/netrom/nr_in.c290
-rw-r--r--net/netrom/nr_loopback.c76
-rw-r--r--net/netrom/nr_out.c274
-rw-r--r--net/netrom/nr_route.c1041
-rw-r--r--net/netrom/nr_subr.c283
-rw-r--r--net/netrom/nr_timer.c260
-rw-r--r--net/netrom/sysctl_net_netrom.c189
10 files changed, 4127 insertions, 0 deletions
diff --git a/net/netrom/Makefile b/net/netrom/Makefile
new file mode 100644
index 000000000000..2660f5a16991
--- /dev/null
+++ b/net/netrom/Makefile
@@ -0,0 +1,9 @@
1#
2# Makefile for the Linux NET/ROM layer.
3#
4
5obj-$(CONFIG_NETROM) += netrom.o
6
7netrom-y := af_netrom.o nr_dev.o nr_in.o nr_loopback.o \
8 nr_out.o nr_route.o nr_subr.o nr_timer.o
9netrom-$(CONFIG_SYSCTL) += sysctl_net_netrom.o
diff --git a/net/netrom/af_netrom.c b/net/netrom/af_netrom.c
new file mode 100644
index 000000000000..31ed4a9a1d06
--- /dev/null
+++ b/net/netrom/af_netrom.c
@@ -0,0 +1,1485 @@
1/*
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 */
11#include <linux/config.h>
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#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>
30#include <net/sock.h>
31#include <asm/uaccess.h>
32#include <asm/system.h>
33#include <linux/fcntl.h>
34#include <linux/termios.h> /* For TIOCINQ/OUTQ */
35#include <linux/mm.h>
36#include <linux/interrupt.h>
37#include <linux/notifier.h>
38#include <net/netrom.h>
39#include <linux/proc_fs.h>
40#include <linux/seq_file.h>
41#include <net/ip.h>
42#include <net/tcp.h>
43#include <net/arp.h>
44#include <linux/init.h>
45
46static int nr_ndevs = 4;
47
48int sysctl_netrom_default_path_quality = NR_DEFAULT_QUAL;
49int sysctl_netrom_obsolescence_count_initialiser = NR_DEFAULT_OBS;
50int sysctl_netrom_network_ttl_initialiser = NR_DEFAULT_TTL;
51int sysctl_netrom_transport_timeout = NR_DEFAULT_T1;
52int sysctl_netrom_transport_maximum_tries = NR_DEFAULT_N2;
53int sysctl_netrom_transport_acknowledge_delay = NR_DEFAULT_T2;
54int sysctl_netrom_transport_busy_delay = NR_DEFAULT_T4;
55int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW;
56int sysctl_netrom_transport_no_activity_timeout = NR_DEFAULT_IDLE;
57int sysctl_netrom_routing_control = NR_DEFAULT_ROUTING;
58int sysctl_netrom_link_fails_count = NR_DEFAULT_FAILS;
59
60static unsigned short circuit = 0x101;
61
62static HLIST_HEAD(nr_list);
63static DEFINE_SPINLOCK(nr_list_lock);
64
65static struct proto_ops nr_proto_ops;
66
67/*
68 * Socket removal during an interrupt is now safe.
69 */
70static void nr_remove_socket(struct sock *sk)
71{
72 spin_lock_bh(&nr_list_lock);
73 sk_del_node_init(sk);
74 spin_unlock_bh(&nr_list_lock);
75}
76
77/*
78 * Kill all bound sockets on a dropped device.
79 */
80static void nr_kill_by_device(struct net_device *dev)
81{
82 struct sock *s;
83 struct hlist_node *node;
84
85 spin_lock_bh(&nr_list_lock);
86 sk_for_each(s, node, &nr_list)
87 if (nr_sk(s)->device == dev)
88 nr_disconnect(s, ENETUNREACH);
89 spin_unlock_bh(&nr_list_lock);
90}
91
92/*
93 * Handle device status changes.
94 */
95static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
96{
97 struct net_device *dev = (struct net_device *)ptr;
98
99 if (event != NETDEV_DOWN)
100 return NOTIFY_DONE;
101
102 nr_kill_by_device(dev);
103 nr_rt_device_down(dev);
104
105 return NOTIFY_DONE;
106}
107
108/*
109 * Add a socket to the bound sockets list.
110 */
111static void nr_insert_socket(struct sock *sk)
112{
113 spin_lock_bh(&nr_list_lock);
114 sk_add_node(sk, &nr_list);
115 spin_unlock_bh(&nr_list_lock);
116}
117
118/*
119 * Find a socket that wants to accept the Connect Request we just
120 * received.
121 */
122static struct sock *nr_find_listener(ax25_address *addr)
123{
124 struct sock *s;
125 struct hlist_node *node;
126
127 spin_lock_bh(&nr_list_lock);
128 sk_for_each(s, node, &nr_list)
129 if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
130 s->sk_state == TCP_LISTEN) {
131 bh_lock_sock(s);
132 goto found;
133 }
134 s = NULL;
135found:
136 spin_unlock_bh(&nr_list_lock);
137 return s;
138}
139
140/*
141 * Find a connected NET/ROM socket given my circuit IDs.
142 */
143static struct sock *nr_find_socket(unsigned char index, unsigned char id)
144{
145 struct sock *s;
146 struct hlist_node *node;
147
148 spin_lock_bh(&nr_list_lock);
149 sk_for_each(s, node, &nr_list) {
150 struct nr_sock *nr = nr_sk(s);
151
152 if (nr->my_index == index && nr->my_id == id) {
153 bh_lock_sock(s);
154 goto found;
155 }
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 their circuit IDs.
165 */
166static struct sock *nr_find_peer(unsigned char index, unsigned char id,
167 ax25_address *dest)
168{
169 struct sock *s;
170 struct hlist_node *node;
171
172 spin_lock_bh(&nr_list_lock);
173 sk_for_each(s, node, &nr_list) {
174 struct nr_sock *nr = nr_sk(s);
175
176 if (nr->your_index == index && nr->your_id == id &&
177 !ax25cmp(&nr->dest_addr, dest)) {
178 bh_lock_sock(s);
179 goto found;
180 }
181 }
182 s = NULL;
183found:
184 spin_unlock_bh(&nr_list_lock);
185 return s;
186}
187
188/*
189 * Find next free circuit ID.
190 */
191static unsigned short nr_find_next_circuit(void)
192{
193 unsigned short id = circuit;
194 unsigned char i, j;
195 struct sock *sk;
196
197 for (;;) {
198 i = id / 256;
199 j = id % 256;
200
201 if (i != 0 && j != 0) {
202 if ((sk=nr_find_socket(i, j)) == NULL)
203 break;
204 bh_unlock_sock(sk);
205 }
206
207 id++;
208 }
209
210 return id;
211}
212
213/*
214 * Deferred destroy.
215 */
216void nr_destroy_socket(struct sock *);
217
218/*
219 * Handler for deferred kills.
220 */
221static void nr_destroy_timer(unsigned long data)
222{
223 struct sock *sk=(struct sock *)data;
224 bh_lock_sock(sk);
225 sock_hold(sk);
226 nr_destroy_socket(sk);
227 bh_unlock_sock(sk);
228 sock_put(sk);
229}
230
231/*
232 * This is called from user mode and the timers. Thus it protects itself
233 * against interrupt users but doesn't worry about being called during
234 * work. Once it is removed from the queue no interrupt or bottom half
235 * will touch it and we are (fairly 8-) ) safe.
236 */
237void nr_destroy_socket(struct sock *sk)
238{
239 struct sk_buff *skb;
240
241 nr_remove_socket(sk);
242
243 nr_stop_heartbeat(sk);
244 nr_stop_t1timer(sk);
245 nr_stop_t2timer(sk);
246 nr_stop_t4timer(sk);
247 nr_stop_idletimer(sk);
248
249 nr_clear_queues(sk); /* Flush the queues */
250
251 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
252 if (skb->sk != sk) { /* A pending connection */
253 /* Queue the unaccepted socket for death */
254 sock_set_flag(skb->sk, SOCK_DEAD);
255 nr_start_heartbeat(skb->sk);
256 nr_sk(skb->sk)->state = NR_STATE_0;
257 }
258
259 kfree_skb(skb);
260 }
261
262 if (atomic_read(&sk->sk_wmem_alloc) ||
263 atomic_read(&sk->sk_rmem_alloc)) {
264 /* Defer: outstanding buffers */
265 sk->sk_timer.function = nr_destroy_timer;
266 sk->sk_timer.expires = jiffies + 2 * HZ;
267 add_timer(&sk->sk_timer);
268 } else
269 sock_put(sk);
270}
271
272/*
273 * Handling for system calls applied via the various interfaces to a
274 * NET/ROM socket object.
275 */
276
277static int nr_setsockopt(struct socket *sock, int level, int optname,
278 char __user *optval, int optlen)
279{
280 struct sock *sk = sock->sk;
281 struct nr_sock *nr = nr_sk(sk);
282 int opt;
283
284 if (level != SOL_NETROM)
285 return -ENOPROTOOPT;
286
287 if (optlen < sizeof(int))
288 return -EINVAL;
289
290 if (get_user(opt, (int __user *)optval))
291 return -EFAULT;
292
293 switch (optname) {
294 case NETROM_T1:
295 if (opt < 1)
296 return -EINVAL;
297 nr->t1 = opt * HZ;
298 return 0;
299
300 case NETROM_T2:
301 if (opt < 1)
302 return -EINVAL;
303 nr->t2 = opt * HZ;
304 return 0;
305
306 case NETROM_N2:
307 if (opt < 1 || opt > 31)
308 return -EINVAL;
309 nr->n2 = opt;
310 return 0;
311
312 case NETROM_T4:
313 if (opt < 1)
314 return -EINVAL;
315 nr->t4 = opt * HZ;
316 return 0;
317
318 case NETROM_IDLE:
319 if (opt < 0)
320 return -EINVAL;
321 nr->idle = opt * 60 * HZ;
322 return 0;
323
324 default:
325 return -ENOPROTOOPT;
326 }
327}
328
329static int nr_getsockopt(struct socket *sock, int level, int optname,
330 char __user *optval, int __user *optlen)
331{
332 struct sock *sk = sock->sk;
333 struct nr_sock *nr = nr_sk(sk);
334 int val = 0;
335 int len;
336
337 if (level != SOL_NETROM)
338 return -ENOPROTOOPT;
339
340 if (get_user(len, optlen))
341 return -EFAULT;
342
343 if (len < 0)
344 return -EINVAL;
345
346 switch (optname) {
347 case NETROM_T1:
348 val = nr->t1 / HZ;
349 break;
350
351 case NETROM_T2:
352 val = nr->t2 / HZ;
353 break;
354
355 case NETROM_N2:
356 val = nr->n2;
357 break;
358
359 case NETROM_T4:
360 val = nr->t4 / HZ;
361 break;
362
363 case NETROM_IDLE:
364 val = nr->idle / (60 * HZ);
365 break;
366
367 default:
368 return -ENOPROTOOPT;
369 }
370
371 len = min_t(unsigned int, len, sizeof(int));
372
373 if (put_user(len, optlen))
374 return -EFAULT;
375
376 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
377}
378
379static int nr_listen(struct socket *sock, int backlog)
380{
381 struct sock *sk = sock->sk;
382
383 lock_sock(sk);
384 if (sk->sk_state != TCP_LISTEN) {
385 memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
386 sk->sk_max_ack_backlog = backlog;
387 sk->sk_state = TCP_LISTEN;
388 release_sock(sk);
389 return 0;
390 }
391 release_sock(sk);
392
393 return -EOPNOTSUPP;
394}
395
396static struct proto nr_proto = {
397 .name = "NETROM",
398 .owner = THIS_MODULE,
399 .obj_size = sizeof(struct nr_sock),
400};
401
402static int nr_create(struct socket *sock, int protocol)
403{
404 struct sock *sk;
405 struct nr_sock *nr;
406
407 if (sock->type != SOCK_SEQPACKET || protocol != 0)
408 return -ESOCKTNOSUPPORT;
409
410 if ((sk = sk_alloc(PF_NETROM, GFP_ATOMIC, &nr_proto, 1)) == NULL)
411 return -ENOMEM;
412
413 nr = nr_sk(sk);
414
415 sock_init_data(sock, sk);
416
417 sock->ops = &nr_proto_ops;
418 sk->sk_protocol = protocol;
419
420 skb_queue_head_init(&nr->ack_queue);
421 skb_queue_head_init(&nr->reseq_queue);
422 skb_queue_head_init(&nr->frag_queue);
423
424 nr_init_timers(sk);
425
426 nr->t1 = sysctl_netrom_transport_timeout;
427 nr->t2 = sysctl_netrom_transport_acknowledge_delay;
428 nr->n2 = sysctl_netrom_transport_maximum_tries;
429 nr->t4 = sysctl_netrom_transport_busy_delay;
430 nr->idle = sysctl_netrom_transport_no_activity_timeout;
431 nr->window = sysctl_netrom_transport_requested_window_size;
432
433 nr->bpqext = 1;
434 nr->state = NR_STATE_0;
435
436 return 0;
437}
438
439static struct sock *nr_make_new(struct sock *osk)
440{
441 struct sock *sk;
442 struct nr_sock *nr, *onr;
443
444 if (osk->sk_type != SOCK_SEQPACKET)
445 return NULL;
446
447 if ((sk = sk_alloc(PF_NETROM, GFP_ATOMIC, osk->sk_prot, 1)) == NULL)
448 return NULL;
449
450 nr = nr_sk(sk);
451
452 sock_init_data(NULL, sk);
453
454 sk->sk_type = osk->sk_type;
455 sk->sk_socket = osk->sk_socket;
456 sk->sk_priority = osk->sk_priority;
457 sk->sk_protocol = osk->sk_protocol;
458 sk->sk_rcvbuf = osk->sk_rcvbuf;
459 sk->sk_sndbuf = osk->sk_sndbuf;
460 sk->sk_state = TCP_ESTABLISHED;
461 sk->sk_sleep = osk->sk_sleep;
462
463 if (sock_flag(osk, SOCK_ZAPPED))
464 sock_set_flag(sk, SOCK_ZAPPED);
465
466 if (sock_flag(osk, SOCK_DBG))
467 sock_set_flag(sk, SOCK_DBG);
468
469 skb_queue_head_init(&nr->ack_queue);
470 skb_queue_head_init(&nr->reseq_queue);
471 skb_queue_head_init(&nr->frag_queue);
472
473 nr_init_timers(sk);
474
475 onr = nr_sk(osk);
476
477 nr->t1 = onr->t1;
478 nr->t2 = onr->t2;
479 nr->n2 = onr->n2;
480 nr->t4 = onr->t4;
481 nr->idle = onr->idle;
482 nr->window = onr->window;
483
484 nr->device = onr->device;
485 nr->bpqext = onr->bpqext;
486
487 return sk;
488}
489
490static int nr_release(struct socket *sock)
491{
492 struct sock *sk = sock->sk;
493 struct nr_sock *nr;
494
495 if (sk == NULL) return 0;
496
497 sock_hold(sk);
498 lock_sock(sk);
499 nr = nr_sk(sk);
500
501 switch (nr->state) {
502 case NR_STATE_0:
503 case NR_STATE_1:
504 case NR_STATE_2:
505 nr_disconnect(sk, 0);
506 nr_destroy_socket(sk);
507 break;
508
509 case NR_STATE_3:
510 nr_clear_queues(sk);
511 nr->n2count = 0;
512 nr_write_internal(sk, NR_DISCREQ);
513 nr_start_t1timer(sk);
514 nr_stop_t2timer(sk);
515 nr_stop_t4timer(sk);
516 nr_stop_idletimer(sk);
517 nr->state = NR_STATE_2;
518 sk->sk_state = TCP_CLOSE;
519 sk->sk_shutdown |= SEND_SHUTDOWN;
520 sk->sk_state_change(sk);
521 sock_orphan(sk);
522 sock_set_flag(sk, SOCK_DESTROY);
523 sk->sk_socket = NULL;
524 break;
525
526 default:
527 sk->sk_socket = NULL;
528 break;
529 }
530
531 sock->sk = NULL;
532 release_sock(sk);
533 sock_put(sk);
534
535 return 0;
536}
537
538static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
539{
540 struct sock *sk = sock->sk;
541 struct nr_sock *nr = nr_sk(sk);
542 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
543 struct net_device *dev;
544 ax25_address *user, *source;
545
546 lock_sock(sk);
547 if (!sock_flag(sk, SOCK_ZAPPED)) {
548 release_sock(sk);
549 return -EINVAL;
550 }
551 if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
552 release_sock(sk);
553 return -EINVAL;
554 }
555 if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
556 release_sock(sk);
557 return -EINVAL;
558 }
559 if (addr->fsa_ax25.sax25_family != AF_NETROM) {
560 release_sock(sk);
561 return -EINVAL;
562 }
563 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
564 SOCK_DEBUG(sk, "NET/ROM: bind failed: invalid node callsign\n");
565 release_sock(sk);
566 return -EADDRNOTAVAIL;
567 }
568
569 /*
570 * Only the super user can set an arbitrary user callsign.
571 */
572 if (addr->fsa_ax25.sax25_ndigis == 1) {
573 if (!capable(CAP_NET_BIND_SERVICE)) {
574 dev_put(dev);
575 release_sock(sk);
576 return -EACCES;
577 }
578 nr->user_addr = addr->fsa_digipeater[0];
579 nr->source_addr = addr->fsa_ax25.sax25_call;
580 } else {
581 source = &addr->fsa_ax25.sax25_call;
582
583 if ((user = ax25_findbyuid(current->euid)) == NULL) {
584 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
585 release_sock(sk);
586 dev_put(dev);
587 return -EPERM;
588 }
589 user = source;
590 }
591
592 nr->user_addr = *user;
593 nr->source_addr = *source;
594 }
595
596 nr->device = dev;
597 nr_insert_socket(sk);
598
599 sock_reset_flag(sk, SOCK_ZAPPED);
600 dev_put(dev);
601 release_sock(sk);
602 SOCK_DEBUG(sk, "NET/ROM: socket is bound\n");
603 return 0;
604}
605
606static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
607 int addr_len, int flags)
608{
609 struct sock *sk = sock->sk;
610 struct nr_sock *nr = nr_sk(sk);
611 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
612 ax25_address *user, *source = NULL;
613 struct net_device *dev;
614
615 lock_sock(sk);
616 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
617 sock->state = SS_CONNECTED;
618 release_sock(sk);
619 return 0; /* Connect completed during a ERESTARTSYS event */
620 }
621
622 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
623 sock->state = SS_UNCONNECTED;
624 release_sock(sk);
625 return -ECONNREFUSED;
626 }
627
628 if (sk->sk_state == TCP_ESTABLISHED) {
629 release_sock(sk);
630 return -EISCONN; /* No reconnect on a seqpacket socket */
631 }
632
633 sk->sk_state = TCP_CLOSE;
634 sock->state = SS_UNCONNECTED;
635
636 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
637 release_sock(sk);
638 return -EINVAL;
639 }
640 if (addr->sax25_family != AF_NETROM) {
641 release_sock(sk);
642 return -EINVAL;
643 }
644 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
645 sock_reset_flag(sk, SOCK_ZAPPED);
646
647 if ((dev = nr_dev_first()) == NULL) {
648 release_sock(sk);
649 return -ENETUNREACH;
650 }
651 source = (ax25_address *)dev->dev_addr;
652
653 if ((user = ax25_findbyuid(current->euid)) == NULL) {
654 if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
655 dev_put(dev);
656 release_sock(sk);
657 return -EPERM;
658 }
659 user = source;
660 }
661
662 nr->user_addr = *user;
663 nr->source_addr = *source;
664 nr->device = dev;
665
666 dev_put(dev);
667 nr_insert_socket(sk); /* Finish the bind */
668 }
669
670 nr->dest_addr = addr->sax25_call;
671
672 release_sock(sk);
673 circuit = nr_find_next_circuit();
674 lock_sock(sk);
675
676 nr->my_index = circuit / 256;
677 nr->my_id = circuit % 256;
678
679 circuit++;
680
681 /* Move to connecting socket, start sending Connect Requests */
682 sock->state = SS_CONNECTING;
683 sk->sk_state = TCP_SYN_SENT;
684
685 nr_establish_data_link(sk);
686
687 nr->state = NR_STATE_1;
688
689 nr_start_heartbeat(sk);
690
691 /* Now the loop */
692 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
693 release_sock(sk);
694 return -EINPROGRESS;
695 }
696
697 /*
698 * A Connect Ack with Choke or timeout or failed routing will go to
699 * closed.
700 */
701 if (sk->sk_state == TCP_SYN_SENT) {
702 struct task_struct *tsk = current;
703 DECLARE_WAITQUEUE(wait, tsk);
704
705 add_wait_queue(sk->sk_sleep, &wait);
706 for (;;) {
707 set_current_state(TASK_INTERRUPTIBLE);
708 if (sk->sk_state != TCP_SYN_SENT)
709 break;
710 release_sock(sk);
711 if (!signal_pending(tsk)) {
712 schedule();
713 lock_sock(sk);
714 continue;
715 }
716 current->state = TASK_RUNNING;
717 remove_wait_queue(sk->sk_sleep, &wait);
718 return -ERESTARTSYS;
719 }
720 current->state = TASK_RUNNING;
721 remove_wait_queue(sk->sk_sleep, &wait);
722 }
723
724 if (sk->sk_state != TCP_ESTABLISHED) {
725 sock->state = SS_UNCONNECTED;
726 release_sock(sk);
727 return sock_error(sk); /* Always set at this point */
728 }
729
730 sock->state = SS_CONNECTED;
731 release_sock(sk);
732
733 return 0;
734}
735
736static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
737{
738 struct task_struct *tsk = current;
739 DECLARE_WAITQUEUE(wait, tsk);
740 struct sk_buff *skb;
741 struct sock *newsk;
742 struct sock *sk;
743 int err = 0;
744
745 if ((sk = sock->sk) == NULL)
746 return -EINVAL;
747
748 lock_sock(sk);
749 if (sk->sk_type != SOCK_SEQPACKET) {
750 err = -EOPNOTSUPP;
751 goto out;
752 }
753
754 if (sk->sk_state != TCP_LISTEN) {
755 err = -EINVAL;
756 goto out;
757 }
758
759 /*
760 * The write queue this time is holding sockets ready to use
761 * hooked into the SABM we saved
762 */
763 add_wait_queue(sk->sk_sleep, &wait);
764 for (;;) {
765 skb = skb_dequeue(&sk->sk_receive_queue);
766 if (skb)
767 break;
768
769 current->state = TASK_INTERRUPTIBLE;
770 release_sock(sk);
771 if (flags & O_NONBLOCK) {
772 current->state = TASK_RUNNING;
773 remove_wait_queue(sk->sk_sleep, &wait);
774 return -EWOULDBLOCK;
775 }
776 if (!signal_pending(tsk)) {
777 schedule();
778 lock_sock(sk);
779 continue;
780 }
781 current->state = TASK_RUNNING;
782 remove_wait_queue(sk->sk_sleep, &wait);
783 return -ERESTARTSYS;
784 }
785 current->state = TASK_RUNNING;
786 remove_wait_queue(sk->sk_sleep, &wait);
787
788 newsk = skb->sk;
789 newsk->sk_socket = newsock;
790 newsk->sk_sleep = &newsock->wait;
791
792 /* Now attach up the new socket */
793 kfree_skb(skb);
794 sk->sk_ack_backlog--;
795 newsock->sk = newsk;
796
797out:
798 release_sock(sk);
799 return err;
800}
801
802static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
803 int *uaddr_len, int peer)
804{
805 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
806 struct sock *sk = sock->sk;
807 struct nr_sock *nr = nr_sk(sk);
808
809 lock_sock(sk);
810 if (peer != 0) {
811 if (sk->sk_state != TCP_ESTABLISHED) {
812 release_sock(sk);
813 return -ENOTCONN;
814 }
815 sax->fsa_ax25.sax25_family = AF_NETROM;
816 sax->fsa_ax25.sax25_ndigis = 1;
817 sax->fsa_ax25.sax25_call = nr->user_addr;
818 sax->fsa_digipeater[0] = nr->dest_addr;
819 *uaddr_len = sizeof(struct full_sockaddr_ax25);
820 } else {
821 sax->fsa_ax25.sax25_family = AF_NETROM;
822 sax->fsa_ax25.sax25_ndigis = 0;
823 sax->fsa_ax25.sax25_call = nr->source_addr;
824 *uaddr_len = sizeof(struct sockaddr_ax25);
825 }
826 release_sock(sk);
827
828 return 0;
829}
830
831int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
832{
833 struct sock *sk;
834 struct sock *make;
835 struct nr_sock *nr_make;
836 ax25_address *src, *dest, *user;
837 unsigned short circuit_index, circuit_id;
838 unsigned short peer_circuit_index, peer_circuit_id;
839 unsigned short frametype, flags, window, timeout;
840 int ret;
841
842 skb->sk = NULL; /* Initially we don't know who it's for */
843
844 /*
845 * skb->data points to the netrom frame start
846 */
847
848 src = (ax25_address *)(skb->data + 0);
849 dest = (ax25_address *)(skb->data + 7);
850
851 circuit_index = skb->data[15];
852 circuit_id = skb->data[16];
853 peer_circuit_index = skb->data[17];
854 peer_circuit_id = skb->data[18];
855 frametype = skb->data[19] & 0x0F;
856 flags = skb->data[19] & 0xF0;
857
858#ifdef CONFIG_INET
859 /*
860 * Check for an incoming IP over NET/ROM frame.
861 */
862 if (frametype == NR_PROTOEXT && circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
863 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
864 skb->h.raw = skb->data;
865
866 return nr_rx_ip(skb, dev);
867 }
868#endif
869
870 /*
871 * Find an existing socket connection, based on circuit ID, if it's
872 * a Connect Request base it on their circuit ID.
873 *
874 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
875 * circuit that no longer exists at the other end ...
876 */
877
878 sk = NULL;
879
880 if (circuit_index == 0 && circuit_id == 0) {
881 if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
882 sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
883 } else {
884 if (frametype == NR_CONNREQ)
885 sk = nr_find_peer(circuit_index, circuit_id, src);
886 else
887 sk = nr_find_socket(circuit_index, circuit_id);
888 }
889
890 if (sk != NULL) {
891 skb->h.raw = skb->data;
892
893 if (frametype == NR_CONNACK && skb->len == 22)
894 nr_sk(sk)->bpqext = 1;
895 else
896 nr_sk(sk)->bpqext = 0;
897
898 ret = nr_process_rx_frame(sk, skb);
899 bh_unlock_sock(sk);
900 return ret;
901 }
902
903 /*
904 * Now it should be a CONNREQ.
905 */
906 if (frametype != NR_CONNREQ) {
907 /*
908 * Here it would be nice to be able to send a reset but
909 * NET/ROM doesn't have one. The following hack would
910 * have been a way to extend the protocol but apparently
911 * it kills BPQ boxes... :-(
912 */
913#if 0
914 /*
915 * Never reply to a CONNACK/CHOKE.
916 */
917 if (frametype != NR_CONNACK || flags != NR_CHOKE_FLAG)
918 nr_transmit_refusal(skb, 1);
919#endif
920 return 0;
921 }
922
923 sk = nr_find_listener(dest);
924
925 user = (ax25_address *)(skb->data + 21);
926
927 if (sk == NULL || sk_acceptq_is_full(sk) ||
928 (make = nr_make_new(sk)) == NULL) {
929 nr_transmit_refusal(skb, 0);
930 if (sk)
931 bh_unlock_sock(sk);
932 return 0;
933 }
934
935 window = skb->data[20];
936
937 skb->sk = make;
938 make->sk_state = TCP_ESTABLISHED;
939
940 /* Fill in his circuit details */
941 nr_make = nr_sk(make);
942 nr_make->source_addr = *dest;
943 nr_make->dest_addr = *src;
944 nr_make->user_addr = *user;
945
946 nr_make->your_index = circuit_index;
947 nr_make->your_id = circuit_id;
948
949 bh_unlock_sock(sk);
950 circuit = nr_find_next_circuit();
951 bh_lock_sock(sk);
952
953 nr_make->my_index = circuit / 256;
954 nr_make->my_id = circuit % 256;
955
956 circuit++;
957
958 /* Window negotiation */
959 if (window < nr_make->window)
960 nr_make->window = window;
961
962 /* L4 timeout negotiation */
963 if (skb->len == 37) {
964 timeout = skb->data[36] * 256 + skb->data[35];
965 if (timeout * HZ < nr_make->t1)
966 nr_make->t1 = timeout * HZ;
967 nr_make->bpqext = 1;
968 } else {
969 nr_make->bpqext = 0;
970 }
971
972 nr_write_internal(make, NR_CONNACK);
973
974 nr_make->condition = 0x00;
975 nr_make->vs = 0;
976 nr_make->va = 0;
977 nr_make->vr = 0;
978 nr_make->vl = 0;
979 nr_make->state = NR_STATE_3;
980 sk->sk_ack_backlog++;
981
982 nr_insert_socket(make);
983
984 skb_queue_head(&sk->sk_receive_queue, skb);
985
986 nr_start_heartbeat(make);
987 nr_start_idletimer(make);
988
989 if (!sock_flag(sk, SOCK_DEAD))
990 sk->sk_data_ready(sk, skb->len);
991
992 bh_unlock_sock(sk);
993 return 1;
994}
995
996static int nr_sendmsg(struct kiocb *iocb, struct socket *sock,
997 struct msghdr *msg, size_t len)
998{
999 struct sock *sk = sock->sk;
1000 struct nr_sock *nr = nr_sk(sk);
1001 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1002 int err;
1003 struct sockaddr_ax25 sax;
1004 struct sk_buff *skb;
1005 unsigned char *asmptr;
1006 int size;
1007
1008 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1009 return -EINVAL;
1010
1011 lock_sock(sk);
1012 if (sock_flag(sk, SOCK_ZAPPED)) {
1013 err = -EADDRNOTAVAIL;
1014 goto out;
1015 }
1016
1017 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1018 send_sig(SIGPIPE, current, 0);
1019 err = -EPIPE;
1020 goto out;
1021 }
1022
1023 if (nr->device == NULL) {
1024 err = -ENETUNREACH;
1025 goto out;
1026 }
1027
1028 if (usax) {
1029 if (msg->msg_namelen < sizeof(sax)) {
1030 err = -EINVAL;
1031 goto out;
1032 }
1033 sax = *usax;
1034 if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
1035 err = -EISCONN;
1036 goto out;
1037 }
1038 if (sax.sax25_family != AF_NETROM) {
1039 err = -EINVAL;
1040 goto out;
1041 }
1042 } else {
1043 if (sk->sk_state != TCP_ESTABLISHED) {
1044 err = -ENOTCONN;
1045 goto out;
1046 }
1047 sax.sax25_family = AF_NETROM;
1048 sax.sax25_call = nr->dest_addr;
1049 }
1050
1051 SOCK_DEBUG(sk, "NET/ROM: sendto: Addresses built.\n");
1052
1053 /* Build a packet */
1054 SOCK_DEBUG(sk, "NET/ROM: sendto: building packet.\n");
1055 size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1056
1057 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1058 goto out;
1059
1060 skb_reserve(skb, size - len);
1061
1062 /*
1063 * Push down the NET/ROM header
1064 */
1065
1066 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1067 SOCK_DEBUG(sk, "Building NET/ROM Header.\n");
1068
1069 /* Build a NET/ROM Transport header */
1070
1071 *asmptr++ = nr->your_index;
1072 *asmptr++ = nr->your_id;
1073 *asmptr++ = 0; /* To be filled in later */
1074 *asmptr++ = 0; /* Ditto */
1075 *asmptr++ = NR_INFO;
1076 SOCK_DEBUG(sk, "Built header.\n");
1077
1078 /*
1079 * Put the data on the end
1080 */
1081
1082 skb->h.raw = skb_put(skb, len);
1083
1084 asmptr = skb->h.raw;
1085 SOCK_DEBUG(sk, "NET/ROM: Appending user data\n");
1086
1087 /* User data follows immediately after the NET/ROM transport header */
1088 if (memcpy_fromiovec(asmptr, msg->msg_iov, len)) {
1089 kfree_skb(skb);
1090 err = -EFAULT;
1091 goto out;
1092 }
1093
1094 SOCK_DEBUG(sk, "NET/ROM: Transmitting buffer\n");
1095
1096 if (sk->sk_state != TCP_ESTABLISHED) {
1097 kfree_skb(skb);
1098 err = -ENOTCONN;
1099 goto out;
1100 }
1101
1102 nr_output(sk, skb); /* Shove it onto the queue */
1103
1104 err = len;
1105out:
1106 release_sock(sk);
1107 return err;
1108}
1109
1110static int nr_recvmsg(struct kiocb *iocb, struct socket *sock,
1111 struct msghdr *msg, size_t size, int flags)
1112{
1113 struct sock *sk = sock->sk;
1114 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1115 size_t copied;
1116 struct sk_buff *skb;
1117 int er;
1118
1119 /*
1120 * This works for seqpacket too. The receiver has ordered the queue for
1121 * us! We do one quick check first though
1122 */
1123
1124 lock_sock(sk);
1125 if (sk->sk_state != TCP_ESTABLISHED) {
1126 release_sock(sk);
1127 return -ENOTCONN;
1128 }
1129
1130 /* Now we can treat all alike */
1131 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
1132 release_sock(sk);
1133 return er;
1134 }
1135
1136 skb->h.raw = skb->data;
1137 copied = skb->len;
1138
1139 if (copied > size) {
1140 copied = size;
1141 msg->msg_flags |= MSG_TRUNC;
1142 }
1143
1144 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1145
1146 if (sax != NULL) {
1147 sax->sax25_family = AF_NETROM;
1148 memcpy(sax->sax25_call.ax25_call, skb->data + 7, AX25_ADDR_LEN);
1149 }
1150
1151 msg->msg_namelen = sizeof(*sax);
1152
1153 skb_free_datagram(sk, skb);
1154
1155 release_sock(sk);
1156 return copied;
1157}
1158
1159
1160static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1161{
1162 struct sock *sk = sock->sk;
1163 void __user *argp = (void __user *)arg;
1164 int ret;
1165
1166 lock_sock(sk);
1167 switch (cmd) {
1168 case TIOCOUTQ: {
1169 long amount;
1170 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1171 if (amount < 0)
1172 amount = 0;
1173 release_sock(sk);
1174 return put_user(amount, (int __user *)argp);
1175 }
1176
1177 case TIOCINQ: {
1178 struct sk_buff *skb;
1179 long amount = 0L;
1180 /* These two are safe on a single CPU system as only user tasks fiddle here */
1181 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1182 amount = skb->len;
1183 release_sock(sk);
1184 return put_user(amount, (int __user *)argp);
1185 }
1186
1187 case SIOCGSTAMP:
1188 ret = -EINVAL;
1189 if (sk != NULL)
1190 ret = sock_get_timestamp(sk, argp);
1191 release_sock(sk);
1192 return ret;
1193
1194 case SIOCGIFADDR:
1195 case SIOCSIFADDR:
1196 case SIOCGIFDSTADDR:
1197 case SIOCSIFDSTADDR:
1198 case SIOCGIFBRDADDR:
1199 case SIOCSIFBRDADDR:
1200 case SIOCGIFNETMASK:
1201 case SIOCSIFNETMASK:
1202 case SIOCGIFMETRIC:
1203 case SIOCSIFMETRIC:
1204 release_sock(sk);
1205 return -EINVAL;
1206
1207 case SIOCADDRT:
1208 case SIOCDELRT:
1209 case SIOCNRDECOBS:
1210 release_sock(sk);
1211 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1212 return nr_rt_ioctl(cmd, argp);
1213
1214 default:
1215 release_sock(sk);
1216 return dev_ioctl(cmd, argp);
1217 }
1218 release_sock(sk);
1219
1220 return 0;
1221}
1222
1223#ifdef CONFIG_PROC_FS
1224
1225static void *nr_info_start(struct seq_file *seq, loff_t *pos)
1226{
1227 struct sock *s;
1228 struct hlist_node *node;
1229 int i = 1;
1230
1231 spin_lock_bh(&nr_list_lock);
1232 if (*pos == 0)
1233 return SEQ_START_TOKEN;
1234
1235 sk_for_each(s, node, &nr_list) {
1236 if (i == *pos)
1237 return s;
1238 ++i;
1239 }
1240 return NULL;
1241}
1242
1243static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
1244{
1245 ++*pos;
1246
1247 return (v == SEQ_START_TOKEN) ? sk_head(&nr_list)
1248 : sk_next((struct sock *)v);
1249}
1250
1251static void nr_info_stop(struct seq_file *seq, void *v)
1252{
1253 spin_unlock_bh(&nr_list_lock);
1254}
1255
1256static int nr_info_show(struct seq_file *seq, void *v)
1257{
1258 struct sock *s = v;
1259 struct net_device *dev;
1260 struct nr_sock *nr;
1261 const char *devname;
1262
1263 if (v == SEQ_START_TOKEN)
1264 seq_puts(seq,
1265"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");
1266
1267 else {
1268
1269 bh_lock_sock(s);
1270 nr = nr_sk(s);
1271
1272 if ((dev = nr->device) == NULL)
1273 devname = "???";
1274 else
1275 devname = dev->name;
1276
1277 seq_printf(seq, "%-9s ", ax2asc(&nr->user_addr));
1278 seq_printf(seq, "%-9s ", ax2asc(&nr->dest_addr));
1279 seq_printf(seq,
1280"%-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",
1281 ax2asc(&nr->source_addr),
1282 devname,
1283 nr->my_index,
1284 nr->my_id,
1285 nr->your_index,
1286 nr->your_id,
1287 nr->state,
1288 nr->vs,
1289 nr->vr,
1290 nr->va,
1291 ax25_display_timer(&nr->t1timer) / HZ,
1292 nr->t1 / HZ,
1293 ax25_display_timer(&nr->t2timer) / HZ,
1294 nr->t2 / HZ,
1295 ax25_display_timer(&nr->t4timer) / HZ,
1296 nr->t4 / HZ,
1297 ax25_display_timer(&nr->idletimer) / (60 * HZ),
1298 nr->idle / (60 * HZ),
1299 nr->n2count,
1300 nr->n2,
1301 nr->window,
1302 atomic_read(&s->sk_wmem_alloc),
1303 atomic_read(&s->sk_rmem_alloc),
1304 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1305
1306 bh_unlock_sock(s);
1307 }
1308 return 0;
1309}
1310
1311static struct seq_operations nr_info_seqops = {
1312 .start = nr_info_start,
1313 .next = nr_info_next,
1314 .stop = nr_info_stop,
1315 .show = nr_info_show,
1316};
1317
1318static int nr_info_open(struct inode *inode, struct file *file)
1319{
1320 return seq_open(file, &nr_info_seqops);
1321}
1322
1323static struct file_operations nr_info_fops = {
1324 .owner = THIS_MODULE,
1325 .open = nr_info_open,
1326 .read = seq_read,
1327 .llseek = seq_lseek,
1328 .release = seq_release,
1329};
1330#endif /* CONFIG_PROC_FS */
1331
1332static struct net_proto_family nr_family_ops = {
1333 .family = PF_NETROM,
1334 .create = nr_create,
1335 .owner = THIS_MODULE,
1336};
1337
1338static struct proto_ops nr_proto_ops = {
1339 .family = PF_NETROM,
1340 .owner = THIS_MODULE,
1341 .release = nr_release,
1342 .bind = nr_bind,
1343 .connect = nr_connect,
1344 .socketpair = sock_no_socketpair,
1345 .accept = nr_accept,
1346 .getname = nr_getname,
1347 .poll = datagram_poll,
1348 .ioctl = nr_ioctl,
1349 .listen = nr_listen,
1350 .shutdown = sock_no_shutdown,
1351 .setsockopt = nr_setsockopt,
1352 .getsockopt = nr_getsockopt,
1353 .sendmsg = nr_sendmsg,
1354 .recvmsg = nr_recvmsg,
1355 .mmap = sock_no_mmap,
1356 .sendpage = sock_no_sendpage,
1357};
1358
1359static struct notifier_block nr_dev_notifier = {
1360 .notifier_call = nr_device_event,
1361};
1362
1363static struct net_device **dev_nr;
1364
1365static char banner[] __initdata = KERN_INFO "G4KLX NET/ROM for Linux. Version 0.7 for AX25.037 Linux 2.4\n";
1366
1367static int __init nr_proto_init(void)
1368{
1369 int i;
1370 int rc = proto_register(&nr_proto, 0);
1371
1372 if (rc != 0)
1373 goto out;
1374
1375 if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
1376 printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
1377 return -1;
1378 }
1379
1380 dev_nr = kmalloc(nr_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1381 if (dev_nr == NULL) {
1382 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device array\n");
1383 return -1;
1384 }
1385
1386 memset(dev_nr, 0x00, nr_ndevs * sizeof(struct net_device *));
1387
1388 for (i = 0; i < nr_ndevs; i++) {
1389 char name[IFNAMSIZ];
1390 struct net_device *dev;
1391
1392 sprintf(name, "nr%d", i);
1393 dev = alloc_netdev(sizeof(struct net_device_stats), name,
1394 nr_setup);
1395 if (!dev) {
1396 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
1397 goto fail;
1398 }
1399
1400 dev->base_addr = i;
1401 if (register_netdev(dev)) {
1402 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n");
1403 free_netdev(dev);
1404 goto fail;
1405 }
1406 dev_nr[i] = dev;
1407 }
1408
1409 if (sock_register(&nr_family_ops)) {
1410 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n");
1411 goto fail;
1412 }
1413
1414 register_netdevice_notifier(&nr_dev_notifier);
1415 printk(banner);
1416
1417 ax25_protocol_register(AX25_P_NETROM, nr_route_frame);
1418 ax25_linkfail_register(nr_link_failed);
1419
1420#ifdef CONFIG_SYSCTL
1421 nr_register_sysctl();
1422#endif
1423
1424 nr_loopback_init();
1425
1426 proc_net_fops_create("nr", S_IRUGO, &nr_info_fops);
1427 proc_net_fops_create("nr_neigh", S_IRUGO, &nr_neigh_fops);
1428 proc_net_fops_create("nr_nodes", S_IRUGO, &nr_nodes_fops);
1429out:
1430 return rc;
1431fail:
1432 while (--i >= 0) {
1433 unregister_netdev(dev_nr[i]);
1434 free_netdev(dev_nr[i]);
1435 }
1436 kfree(dev_nr);
1437 proto_unregister(&nr_proto);
1438 rc = -1;
1439 goto out;
1440}
1441
1442module_init(nr_proto_init);
1443
1444module_param(nr_ndevs, int, 0);
1445MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1446
1447MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1448MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1449MODULE_LICENSE("GPL");
1450MODULE_ALIAS_NETPROTO(PF_NETROM);
1451
1452static void __exit nr_exit(void)
1453{
1454 int i;
1455
1456 proc_net_remove("nr");
1457 proc_net_remove("nr_neigh");
1458 proc_net_remove("nr_nodes");
1459 nr_loopback_clear();
1460
1461 nr_rt_free();
1462
1463#ifdef CONFIG_SYSCTL
1464 nr_unregister_sysctl();
1465#endif
1466
1467 ax25_linkfail_release(nr_link_failed);
1468 ax25_protocol_release(AX25_P_NETROM);
1469
1470 unregister_netdevice_notifier(&nr_dev_notifier);
1471
1472 sock_unregister(PF_NETROM);
1473
1474 for (i = 0; i < nr_ndevs; i++) {
1475 struct net_device *dev = dev_nr[i];
1476 if (dev) {
1477 unregister_netdev(dev);
1478 free_netdev(dev);
1479 }
1480 }
1481
1482 kfree(dev_nr);
1483 proto_unregister(&nr_proto);
1484}
1485module_exit(nr_exit);
diff --git a/net/netrom/nr_dev.c b/net/netrom/nr_dev.c
new file mode 100644
index 000000000000..220bf7494f71
--- /dev/null
+++ b/net/netrom/nr_dev.c
@@ -0,0 +1,220 @@
1/*
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 */
9#include <linux/config.h>
10#include <linux/module.h>
11#include <linux/proc_fs.h>
12#include <linux/kernel.h>
13#include <linux/sched.h>
14#include <linux/interrupt.h>
15#include <linux/fs.h>
16#include <linux/types.h>
17#include <linux/sysctl.h>
18#include <linux/string.h>
19#include <linux/socket.h>
20#include <linux/errno.h>
21#include <linux/fcntl.h>
22#include <linux/in.h>
23#include <linux/if_ether.h> /* For the statistics structure. */
24
25#include <asm/system.h>
26#include <asm/uaccess.h>
27#include <asm/io.h>
28
29#include <linux/inet.h>
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/if_arp.h>
33#include <linux/skbuff.h>
34
35#include <net/ip.h>
36#include <net/arp.h>
37
38#include <net/ax25.h>
39#include <net/netrom.h>
40
41#ifdef CONFIG_INET
42
43/*
44 * Only allow IP over NET/ROM frames through if the netrom device is up.
45 */
46
47int nr_rx_ip(struct sk_buff *skb, struct net_device *dev)
48{
49 struct net_device_stats *stats = netdev_priv(dev);
50
51 if (!netif_running(dev)) {
52 stats->rx_errors++;
53 return 0;
54 }
55
56 stats->rx_packets++;
57 stats->rx_bytes += skb->len;
58
59 skb->protocol = htons(ETH_P_IP);
60
61 /* Spoof incoming device */
62 skb->dev = dev;
63 skb->h.raw = skb->data;
64 skb->nh.raw = skb->data;
65 skb->pkt_type = PACKET_HOST;
66
67 ip_rcv(skb, skb->dev, NULL);
68
69 return 1;
70}
71
72
73static int nr_rebuild_header(struct sk_buff *skb)
74{
75 struct net_device *dev = skb->dev;
76 struct net_device_stats *stats = netdev_priv(dev);
77 struct sk_buff *skbn;
78 unsigned char *bp = skb->data;
79 int len;
80
81 if (arp_find(bp + 7, skb)) {
82 return 1;
83 }
84
85 bp[6] &= ~AX25_CBIT;
86 bp[6] &= ~AX25_EBIT;
87 bp[6] |= AX25_SSSID_SPARE;
88 bp += AX25_ADDR_LEN;
89
90 bp[6] &= ~AX25_CBIT;
91 bp[6] |= AX25_EBIT;
92 bp[6] |= AX25_SSSID_SPARE;
93
94 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
95 kfree_skb(skb);
96 return 1;
97 }
98
99 if (skb->sk != NULL)
100 skb_set_owner_w(skbn, skb->sk);
101
102 kfree_skb(skb);
103
104 len = skbn->len;
105
106 if (!nr_route_frame(skbn, NULL)) {
107 kfree_skb(skbn);
108 stats->tx_errors++;
109 }
110
111 stats->tx_packets++;
112 stats->tx_bytes += len;
113
114 return 1;
115}
116
117#else
118
119static int nr_rebuild_header(struct sk_buff *skb)
120{
121 return 1;
122}
123
124#endif
125
126static int nr_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
127 void *daddr, void *saddr, unsigned len)
128{
129 unsigned char *buff = skb_push(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
130
131 memcpy(buff, (saddr != NULL) ? saddr : dev->dev_addr, dev->addr_len);
132 buff[6] &= ~AX25_CBIT;
133 buff[6] &= ~AX25_EBIT;
134 buff[6] |= AX25_SSSID_SPARE;
135 buff += AX25_ADDR_LEN;
136
137 if (daddr != NULL)
138 memcpy(buff, daddr, dev->addr_len);
139 buff[6] &= ~AX25_CBIT;
140 buff[6] |= AX25_EBIT;
141 buff[6] |= AX25_SSSID_SPARE;
142 buff += AX25_ADDR_LEN;
143
144 *buff++ = sysctl_netrom_network_ttl_initialiser;
145
146 *buff++ = NR_PROTO_IP;
147 *buff++ = NR_PROTO_IP;
148 *buff++ = 0;
149 *buff++ = 0;
150 *buff++ = NR_PROTOEXT;
151
152 if (daddr != NULL)
153 return 37;
154
155 return -37;
156}
157
158static int nr_set_mac_address(struct net_device *dev, void *addr)
159{
160 struct sockaddr *sa = addr;
161
162 if (dev->flags & IFF_UP)
163 ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
164
165 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
166
167 if (dev->flags & IFF_UP)
168 ax25_listen_register((ax25_address *)dev->dev_addr, NULL);
169
170 return 0;
171}
172
173static int nr_open(struct net_device *dev)
174{
175 netif_start_queue(dev);
176 ax25_listen_register((ax25_address *)dev->dev_addr, NULL);
177 return 0;
178}
179
180static int nr_close(struct net_device *dev)
181{
182 ax25_listen_release((ax25_address *)dev->dev_addr, NULL);
183 netif_stop_queue(dev);
184 return 0;
185}
186
187static int nr_xmit(struct sk_buff *skb, struct net_device *dev)
188{
189 struct net_device_stats *stats = netdev_priv(dev);
190 dev_kfree_skb(skb);
191 stats->tx_errors++;
192 return 0;
193}
194
195static struct net_device_stats *nr_get_stats(struct net_device *dev)
196{
197 return netdev_priv(dev);
198}
199
200void nr_setup(struct net_device *dev)
201{
202 SET_MODULE_OWNER(dev);
203 dev->mtu = NR_MAX_PACKET_SIZE;
204 dev->hard_start_xmit = nr_xmit;
205 dev->open = nr_open;
206 dev->stop = nr_close;
207
208 dev->hard_header = nr_header;
209 dev->hard_header_len = NR_NETWORK_LEN + NR_TRANSPORT_LEN;
210 dev->addr_len = AX25_ADDR_LEN;
211 dev->type = ARPHRD_NETROM;
212 dev->tx_queue_len = 40;
213 dev->rebuild_header = nr_rebuild_header;
214 dev->set_mac_address = nr_set_mac_address;
215
216 /* New-style flags. */
217 dev->flags = 0;
218
219 dev->get_stats = nr_get_stats;
220}
diff --git a/net/netrom/nr_in.c b/net/netrom/nr_in.c
new file mode 100644
index 000000000000..9c44b3794126
--- /dev/null
+++ b/net/netrom/nr_in.c
@@ -0,0 +1,290 @@
1/*
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 Darryl Miles G7LED (dlm@g7led.demon.co.uk)
9 */
10#include <linux/errno.h>
11#include <linux/types.h>
12#include <linux/socket.h>
13#include <linux/in.h>
14#include <linux/kernel.h>
15#include <linux/sched.h>
16#include <linux/timer.h>
17#include <linux/string.h>
18#include <linux/sockios.h>
19#include <linux/net.h>
20#include <net/ax25.h>
21#include <linux/inet.h>
22#include <linux/netdevice.h>
23#include <linux/skbuff.h>
24#include <net/sock.h>
25#include <net/tcp.h>
26#include <net/ip.h> /* For ip_rcv */
27#include <asm/uaccess.h>
28#include <asm/system.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/interrupt.h>
32#include <net/netrom.h>
33
34static int nr_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
35{
36 struct sk_buff *skbo, *skbn = skb;
37 struct nr_sock *nr = nr_sk(sk);
38
39 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
40
41 nr_start_idletimer(sk);
42
43 if (more) {
44 nr->fraglen += skb->len;
45 skb_queue_tail(&nr->frag_queue, skb);
46 return 0;
47 }
48
49 if (!more && nr->fraglen > 0) { /* End of fragment */
50 nr->fraglen += skb->len;
51 skb_queue_tail(&nr->frag_queue, skb);
52
53 if ((skbn = alloc_skb(nr->fraglen, GFP_ATOMIC)) == NULL)
54 return 1;
55
56 skbn->h.raw = skbn->data;
57
58 while ((skbo = skb_dequeue(&nr->frag_queue)) != NULL) {
59 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
60 kfree_skb(skbo);
61 }
62
63 nr->fraglen = 0;
64 }
65
66 return sock_queue_rcv_skb(sk, skbn);
67}
68
69/*
70 * State machine for state 1, Awaiting Connection State.
71 * The handling of the timer(s) is in file nr_timer.c.
72 * Handling of state 0 and connection release is in netrom.c.
73 */
74static int nr_state1_machine(struct sock *sk, struct sk_buff *skb,
75 int frametype)
76{
77 switch (frametype) {
78 case NR_CONNACK: {
79 struct nr_sock *nr = nr_sk(sk);
80
81 nr_stop_t1timer(sk);
82 nr_start_idletimer(sk);
83 nr->your_index = skb->data[17];
84 nr->your_id = skb->data[18];
85 nr->vs = 0;
86 nr->va = 0;
87 nr->vr = 0;
88 nr->vl = 0;
89 nr->state = NR_STATE_3;
90 nr->n2count = 0;
91 nr->window = skb->data[20];
92 sk->sk_state = TCP_ESTABLISHED;
93 if (!sock_flag(sk, SOCK_DEAD))
94 sk->sk_state_change(sk);
95 break;
96 }
97
98 case NR_CONNACK | NR_CHOKE_FLAG:
99 nr_disconnect(sk, ECONNREFUSED);
100 break;
101
102 default:
103 break;
104 }
105 return 0;
106}
107
108/*
109 * State machine for state 2, Awaiting Release State.
110 * The handling of the timer(s) is in file nr_timer.c
111 * Handling of state 0 and connection release is in netrom.c.
112 */
113static int nr_state2_machine(struct sock *sk, struct sk_buff *skb,
114 int frametype)
115{
116 switch (frametype) {
117 case NR_CONNACK | NR_CHOKE_FLAG:
118 nr_disconnect(sk, ECONNRESET);
119 break;
120
121 case NR_DISCREQ:
122 nr_write_internal(sk, NR_DISCACK);
123
124 case NR_DISCACK:
125 nr_disconnect(sk, 0);
126 break;
127
128 default:
129 break;
130 }
131 return 0;
132}
133
134/*
135 * State machine for state 3, Connected State.
136 * The handling of the timer(s) is in file nr_timer.c
137 * Handling of state 0 and connection release is in netrom.c.
138 */
139static int nr_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype)
140{
141 struct nr_sock *nrom = nr_sk(sk);
142 struct sk_buff_head temp_queue;
143 struct sk_buff *skbn;
144 unsigned short save_vr;
145 unsigned short nr, ns;
146 int queued = 0;
147
148 nr = skb->data[18];
149 ns = skb->data[17];
150
151 switch (frametype) {
152 case NR_CONNREQ:
153 nr_write_internal(sk, NR_CONNACK);
154 break;
155
156 case NR_DISCREQ:
157 nr_write_internal(sk, NR_DISCACK);
158 nr_disconnect(sk, 0);
159 break;
160
161 case NR_CONNACK | NR_CHOKE_FLAG:
162 case NR_DISCACK:
163 nr_disconnect(sk, ECONNRESET);
164 break;
165
166 case NR_INFOACK:
167 case NR_INFOACK | NR_CHOKE_FLAG:
168 case NR_INFOACK | NR_NAK_FLAG:
169 case NR_INFOACK | NR_NAK_FLAG | NR_CHOKE_FLAG:
170 if (frametype & NR_CHOKE_FLAG) {
171 nrom->condition |= NR_COND_PEER_RX_BUSY;
172 nr_start_t4timer(sk);
173 } else {
174 nrom->condition &= ~NR_COND_PEER_RX_BUSY;
175 nr_stop_t4timer(sk);
176 }
177 if (!nr_validate_nr(sk, nr)) {
178 break;
179 }
180 if (frametype & NR_NAK_FLAG) {
181 nr_frames_acked(sk, nr);
182 nr_send_nak_frame(sk);
183 } else {
184 if (nrom->condition & NR_COND_PEER_RX_BUSY) {
185 nr_frames_acked(sk, nr);
186 } else {
187 nr_check_iframes_acked(sk, nr);
188 }
189 }
190 break;
191
192 case NR_INFO:
193 case NR_INFO | NR_NAK_FLAG:
194 case NR_INFO | NR_CHOKE_FLAG:
195 case NR_INFO | NR_MORE_FLAG:
196 case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG:
197 case NR_INFO | NR_CHOKE_FLAG | NR_MORE_FLAG:
198 case NR_INFO | NR_NAK_FLAG | NR_MORE_FLAG:
199 case NR_INFO | NR_NAK_FLAG | NR_CHOKE_FLAG | NR_MORE_FLAG:
200 if (frametype & NR_CHOKE_FLAG) {
201 nrom->condition |= NR_COND_PEER_RX_BUSY;
202 nr_start_t4timer(sk);
203 } else {
204 nrom->condition &= ~NR_COND_PEER_RX_BUSY;
205 nr_stop_t4timer(sk);
206 }
207 if (nr_validate_nr(sk, nr)) {
208 if (frametype & NR_NAK_FLAG) {
209 nr_frames_acked(sk, nr);
210 nr_send_nak_frame(sk);
211 } else {
212 if (nrom->condition & NR_COND_PEER_RX_BUSY) {
213 nr_frames_acked(sk, nr);
214 } else {
215 nr_check_iframes_acked(sk, nr);
216 }
217 }
218 }
219 queued = 1;
220 skb_queue_head(&nrom->reseq_queue, skb);
221 if (nrom->condition & NR_COND_OWN_RX_BUSY)
222 break;
223 skb_queue_head_init(&temp_queue);
224 do {
225 save_vr = nrom->vr;
226 while ((skbn = skb_dequeue(&nrom->reseq_queue)) != NULL) {
227 ns = skbn->data[17];
228 if (ns == nrom->vr) {
229 if (nr_queue_rx_frame(sk, skbn, frametype & NR_MORE_FLAG) == 0) {
230 nrom->vr = (nrom->vr + 1) % NR_MODULUS;
231 } else {
232 nrom->condition |= NR_COND_OWN_RX_BUSY;
233 skb_queue_tail(&temp_queue, skbn);
234 }
235 } else if (nr_in_rx_window(sk, ns)) {
236 skb_queue_tail(&temp_queue, skbn);
237 } else {
238 kfree_skb(skbn);
239 }
240 }
241 while ((skbn = skb_dequeue(&temp_queue)) != NULL) {
242 skb_queue_tail(&nrom->reseq_queue, skbn);
243 }
244 } while (save_vr != nrom->vr);
245 /*
246 * Window is full, ack it immediately.
247 */
248 if (((nrom->vl + nrom->window) % NR_MODULUS) == nrom->vr) {
249 nr_enquiry_response(sk);
250 } else {
251 if (!(nrom->condition & NR_COND_ACK_PENDING)) {
252 nrom->condition |= NR_COND_ACK_PENDING;
253 nr_start_t2timer(sk);
254 }
255 }
256 break;
257
258 default:
259 break;
260 }
261 return queued;
262}
263
264/* Higher level upcall for a LAPB frame - called with sk locked */
265int nr_process_rx_frame(struct sock *sk, struct sk_buff *skb)
266{
267 struct nr_sock *nr = nr_sk(sk);
268 int queued = 0, frametype;
269
270 if (nr->state == NR_STATE_0)
271 return 0;
272
273 frametype = skb->data[19];
274
275 switch (nr->state) {
276 case NR_STATE_1:
277 queued = nr_state1_machine(sk, skb, frametype);
278 break;
279 case NR_STATE_2:
280 queued = nr_state2_machine(sk, skb, frametype);
281 break;
282 case NR_STATE_3:
283 queued = nr_state3_machine(sk, skb, frametype);
284 break;
285 }
286
287 nr_kick(sk);
288
289 return queued;
290}
diff --git a/net/netrom/nr_loopback.c b/net/netrom/nr_loopback.c
new file mode 100644
index 000000000000..165b2abce110
--- /dev/null
+++ b/net/netrom/nr_loopback.c
@@ -0,0 +1,76 @@
1/*
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 Tomi Manninen OH2BNS (oh2bns@sral.fi)
8 */
9#include <linux/types.h>
10#include <linux/socket.h>
11#include <linux/timer.h>
12#include <net/ax25.h>
13#include <linux/skbuff.h>
14#include <net/netrom.h>
15#include <linux/init.h>
16
17static void nr_loopback_timer(unsigned long);
18
19static struct sk_buff_head loopback_queue;
20static struct timer_list loopback_timer = TIMER_INITIALIZER(nr_loopback_timer, 0, 0);
21
22void __init nr_loopback_init(void)
23{
24 skb_queue_head_init(&loopback_queue);
25}
26
27static inline int nr_loopback_running(void)
28{
29 return timer_pending(&loopback_timer);
30}
31
32int nr_loopback_queue(struct sk_buff *skb)
33{
34 struct sk_buff *skbn;
35
36 if ((skbn = alloc_skb(skb->len, GFP_ATOMIC)) != NULL) {
37 memcpy(skb_put(skbn, skb->len), skb->data, skb->len);
38 skbn->h.raw = skbn->data;
39
40 skb_queue_tail(&loopback_queue, skbn);
41
42 if (!nr_loopback_running())
43 mod_timer(&loopback_timer, jiffies + 10);
44 }
45
46 kfree_skb(skb);
47 return 1;
48}
49
50static void nr_loopback_timer(unsigned long param)
51{
52 struct sk_buff *skb;
53 ax25_address *nr_dest;
54 struct net_device *dev;
55
56 if ((skb = skb_dequeue(&loopback_queue)) != NULL) {
57 nr_dest = (ax25_address *)(skb->data + 7);
58
59 dev = nr_dev_get(nr_dest);
60
61 if (dev == NULL || nr_rx_frame(skb, dev) == 0)
62 kfree_skb(skb);
63
64 if (dev != NULL)
65 dev_put(dev);
66
67 if (!skb_queue_empty(&loopback_queue) && !nr_loopback_running())
68 mod_timer(&loopback_timer, jiffies + 10);
69 }
70}
71
72void __exit nr_loopback_clear(void)
73{
74 del_timer_sync(&loopback_timer);
75 skb_queue_purge(&loopback_queue);
76}
diff --git a/net/netrom/nr_out.c b/net/netrom/nr_out.c
new file mode 100644
index 000000000000..7939ded9c98c
--- /dev/null
+++ b/net/netrom/nr_out.c
@@ -0,0 +1,274 @@
1/*
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 Darryl Miles G7LED (dlm@g7led.demon.co.uk)
9 */
10#include <linux/errno.h>
11#include <linux/types.h>
12#include <linux/socket.h>
13#include <linux/in.h>
14#include <linux/kernel.h>
15#include <linux/sched.h>
16#include <linux/timer.h>
17#include <linux/string.h>
18#include <linux/sockios.h>
19#include <linux/net.h>
20#include <net/ax25.h>
21#include <linux/inet.h>
22#include <linux/netdevice.h>
23#include <linux/skbuff.h>
24#include <net/sock.h>
25#include <asm/uaccess.h>
26#include <asm/system.h>
27#include <linux/fcntl.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30#include <net/netrom.h>
31
32/*
33 * This is where all NET/ROM frames pass, except for IP-over-NET/ROM which
34 * cannot be fragmented in this manner.
35 */
36void nr_output(struct sock *sk, struct sk_buff *skb)
37{
38 struct sk_buff *skbn;
39 unsigned char transport[NR_TRANSPORT_LEN];
40 int err, frontlen, len;
41
42 if (skb->len - NR_TRANSPORT_LEN > NR_MAX_PACKET_SIZE) {
43 /* Save a copy of the Transport Header */
44 memcpy(transport, skb->data, NR_TRANSPORT_LEN);
45 skb_pull(skb, NR_TRANSPORT_LEN);
46
47 frontlen = skb_headroom(skb);
48
49 while (skb->len > 0) {
50 if ((skbn = sock_alloc_send_skb(sk, frontlen + NR_MAX_PACKET_SIZE, 0, &err)) == NULL)
51 return;
52
53 skb_reserve(skbn, frontlen);
54
55 len = (NR_MAX_PACKET_SIZE > skb->len) ? skb->len : NR_MAX_PACKET_SIZE;
56
57 /* Copy the user data */
58 memcpy(skb_put(skbn, len), skb->data, len);
59 skb_pull(skb, len);
60
61 /* Duplicate the Transport Header */
62 skb_push(skbn, NR_TRANSPORT_LEN);
63 memcpy(skbn->data, transport, NR_TRANSPORT_LEN);
64
65 if (skb->len > 0)
66 skbn->data[4] |= NR_MORE_FLAG;
67
68 skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
69 }
70
71 kfree_skb(skb);
72 } else {
73 skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
74 }
75
76 nr_kick(sk);
77}
78
79/*
80 * This procedure is passed a buffer descriptor for an iframe. It builds
81 * the rest of the control part of the frame and then writes it out.
82 */
83static void nr_send_iframe(struct sock *sk, struct sk_buff *skb)
84{
85 struct nr_sock *nr = nr_sk(sk);
86
87 if (skb == NULL)
88 return;
89
90 skb->data[2] = nr->vs;
91 skb->data[3] = nr->vr;
92
93 if (nr->condition & NR_COND_OWN_RX_BUSY)
94 skb->data[4] |= NR_CHOKE_FLAG;
95
96 nr_start_idletimer(sk);
97
98 nr_transmit_buffer(sk, skb);
99}
100
101void nr_send_nak_frame(struct sock *sk)
102{
103 struct sk_buff *skb, *skbn;
104 struct nr_sock *nr = nr_sk(sk);
105
106 if ((skb = skb_peek(&nr->ack_queue)) == NULL)
107 return;
108
109 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL)
110 return;
111
112 skbn->data[2] = nr->va;
113 skbn->data[3] = nr->vr;
114
115 if (nr->condition & NR_COND_OWN_RX_BUSY)
116 skbn->data[4] |= NR_CHOKE_FLAG;
117
118 nr_transmit_buffer(sk, skbn);
119
120 nr->condition &= ~NR_COND_ACK_PENDING;
121 nr->vl = nr->vr;
122
123 nr_stop_t1timer(sk);
124}
125
126void nr_kick(struct sock *sk)
127{
128 struct nr_sock *nr = nr_sk(sk);
129 struct sk_buff *skb, *skbn;
130 unsigned short start, end;
131
132 if (nr->state != NR_STATE_3)
133 return;
134
135 if (nr->condition & NR_COND_PEER_RX_BUSY)
136 return;
137
138 if (!skb_peek(&sk->sk_write_queue))
139 return;
140
141 start = (skb_peek(&nr->ack_queue) == NULL) ? nr->va : nr->vs;
142 end = (nr->va + nr->window) % NR_MODULUS;
143
144 if (start == end)
145 return;
146
147 nr->vs = start;
148
149 /*
150 * Transmit data until either we're out of data to send or
151 * the window is full.
152 */
153
154 /*
155 * Dequeue the frame and copy it.
156 */
157 skb = skb_dequeue(&sk->sk_write_queue);
158
159 do {
160 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
161 skb_queue_head(&sk->sk_write_queue, skb);
162 break;
163 }
164
165 skb_set_owner_w(skbn, sk);
166
167 /*
168 * Transmit the frame copy.
169 */
170 nr_send_iframe(sk, skbn);
171
172 nr->vs = (nr->vs + 1) % NR_MODULUS;
173
174 /*
175 * Requeue the original data frame.
176 */
177 skb_queue_tail(&nr->ack_queue, skb);
178
179 } while (nr->vs != end &&
180 (skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
181
182 nr->vl = nr->vr;
183 nr->condition &= ~NR_COND_ACK_PENDING;
184
185 if (!nr_t1timer_running(sk))
186 nr_start_t1timer(sk);
187}
188
189void nr_transmit_buffer(struct sock *sk, struct sk_buff *skb)
190{
191 struct nr_sock *nr = nr_sk(sk);
192 unsigned char *dptr;
193
194 /*
195 * Add the protocol byte and network header.
196 */
197 dptr = skb_push(skb, NR_NETWORK_LEN);
198
199 memcpy(dptr, &nr->source_addr, AX25_ADDR_LEN);
200 dptr[6] &= ~AX25_CBIT;
201 dptr[6] &= ~AX25_EBIT;
202 dptr[6] |= AX25_SSSID_SPARE;
203 dptr += AX25_ADDR_LEN;
204
205 memcpy(dptr, &nr->dest_addr, AX25_ADDR_LEN);
206 dptr[6] &= ~AX25_CBIT;
207 dptr[6] |= AX25_EBIT;
208 dptr[6] |= AX25_SSSID_SPARE;
209 dptr += AX25_ADDR_LEN;
210
211 *dptr++ = sysctl_netrom_network_ttl_initialiser;
212
213 if (!nr_route_frame(skb, NULL)) {
214 kfree_skb(skb);
215 nr_disconnect(sk, ENETUNREACH);
216 }
217}
218
219/*
220 * The following routines are taken from page 170 of the 7th ARRL Computer
221 * Networking Conference paper, as is the whole state machine.
222 */
223
224void nr_establish_data_link(struct sock *sk)
225{
226 struct nr_sock *nr = nr_sk(sk);
227
228 nr->condition = 0x00;
229 nr->n2count = 0;
230
231 nr_write_internal(sk, NR_CONNREQ);
232
233 nr_stop_t2timer(sk);
234 nr_stop_t4timer(sk);
235 nr_stop_idletimer(sk);
236 nr_start_t1timer(sk);
237}
238
239/*
240 * Never send a NAK when we are CHOKEd.
241 */
242void nr_enquiry_response(struct sock *sk)
243{
244 struct nr_sock *nr = nr_sk(sk);
245 int frametype = NR_INFOACK;
246
247 if (nr->condition & NR_COND_OWN_RX_BUSY) {
248 frametype |= NR_CHOKE_FLAG;
249 } else {
250 if (skb_peek(&nr->reseq_queue) != NULL)
251 frametype |= NR_NAK_FLAG;
252 }
253
254 nr_write_internal(sk, frametype);
255
256 nr->vl = nr->vr;
257 nr->condition &= ~NR_COND_ACK_PENDING;
258}
259
260void nr_check_iframes_acked(struct sock *sk, unsigned short nr)
261{
262 struct nr_sock *nrom = nr_sk(sk);
263
264 if (nrom->vs == nr) {
265 nr_frames_acked(sk, nr);
266 nr_stop_t1timer(sk);
267 nrom->n2count = 0;
268 } else {
269 if (nrom->va != nr) {
270 nr_frames_acked(sk, nr);
271 nr_start_t1timer(sk);
272 }
273 }
274}
diff --git a/net/netrom/nr_route.c b/net/netrom/nr_route.c
new file mode 100644
index 000000000000..7a86b36cba50
--- /dev/null
+++ b/net/netrom/nr_route.c
@@ -0,0 +1,1041 @@
1/*
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 Tomi Manninen OH2BNS (oh2bns@sral.fi)
10 */
11#include <linux/errno.h>
12#include <linux/types.h>
13#include <linux/socket.h>
14#include <linux/in.h>
15#include <linux/kernel.h>
16#include <linux/sched.h>
17#include <linux/timer.h>
18#include <linux/string.h>
19#include <linux/sockios.h>
20#include <linux/net.h>
21#include <net/ax25.h>
22#include <linux/inet.h>
23#include <linux/netdevice.h>
24#include <net/arp.h>
25#include <linux/if_arp.h>
26#include <linux/skbuff.h>
27#include <net/sock.h>
28#include <asm/uaccess.h>
29#include <asm/system.h>
30#include <linux/fcntl.h>
31#include <linux/termios.h> /* For TIOCINQ/OUTQ */
32#include <linux/mm.h>
33#include <linux/interrupt.h>
34#include <linux/notifier.h>
35#include <linux/netfilter.h>
36#include <linux/init.h>
37#include <linux/spinlock.h>
38#include <net/netrom.h>
39#include <linux/seq_file.h>
40
41static unsigned int nr_neigh_no = 1;
42
43static HLIST_HEAD(nr_node_list);
44static DEFINE_SPINLOCK(nr_node_list_lock);
45static HLIST_HEAD(nr_neigh_list);
46static DEFINE_SPINLOCK(nr_neigh_list_lock);
47
48static struct nr_node *nr_node_get(ax25_address *callsign)
49{
50 struct nr_node *found = NULL;
51 struct nr_node *nr_node;
52 struct hlist_node *node;
53
54 spin_lock_bh(&nr_node_list_lock);
55 nr_node_for_each(nr_node, node, &nr_node_list)
56 if (ax25cmp(callsign, &nr_node->callsign) == 0) {
57 nr_node_hold(nr_node);
58 found = nr_node;
59 break;
60 }
61 spin_unlock_bh(&nr_node_list_lock);
62 return found;
63}
64
65static struct nr_neigh *nr_neigh_get_dev(ax25_address *callsign,
66 struct net_device *dev)
67{
68 struct nr_neigh *found = NULL;
69 struct nr_neigh *nr_neigh;
70 struct hlist_node *node;
71
72 spin_lock_bh(&nr_neigh_list_lock);
73 nr_neigh_for_each(nr_neigh, node, &nr_neigh_list)
74 if (ax25cmp(callsign, &nr_neigh->callsign) == 0 &&
75 nr_neigh->dev == dev) {
76 nr_neigh_hold(nr_neigh);
77 found = nr_neigh;
78 break;
79 }
80 spin_unlock_bh(&nr_neigh_list_lock);
81 return found;
82}
83
84static void nr_remove_neigh(struct nr_neigh *);
85
86/*
87 * Add a new route to a node, and in the process add the node and the
88 * neighbour if it is new.
89 */
90static int nr_add_node(ax25_address *nr, const char *mnemonic, ax25_address *ax25,
91 ax25_digi *ax25_digi, struct net_device *dev, int quality, int obs_count)
92{
93 struct nr_node *nr_node;
94 struct nr_neigh *nr_neigh;
95 struct nr_route nr_route;
96 int i, found;
97 struct net_device *odev;
98
99 if ((odev=nr_dev_get(nr)) != NULL) { /* Can't add routes to ourself */
100 dev_put(odev);
101 return -EINVAL;
102 }
103
104 nr_node = nr_node_get(nr);
105
106 nr_neigh = nr_neigh_get_dev(ax25, dev);
107
108 /*
109 * The L2 link to a neighbour has failed in the past
110 * and now a frame comes from this neighbour. We assume
111 * it was a temporary trouble with the link and reset the
112 * routes now (and not wait for a node broadcast).
113 */
114 if (nr_neigh != NULL && nr_neigh->failed != 0 && quality == 0) {
115 struct nr_node *nr_nodet;
116 struct hlist_node *node;
117
118 spin_lock_bh(&nr_node_list_lock);
119 nr_node_for_each(nr_nodet, node, &nr_node_list) {
120 nr_node_lock(nr_nodet);
121 for (i = 0; i < nr_nodet->count; i++)
122 if (nr_nodet->routes[i].neighbour == nr_neigh)
123 if (i < nr_nodet->which)
124 nr_nodet->which = i;
125 nr_node_unlock(nr_nodet);
126 }
127 spin_unlock_bh(&nr_node_list_lock);
128 }
129
130 if (nr_neigh != NULL)
131 nr_neigh->failed = 0;
132
133 if (quality == 0 && nr_neigh != NULL && nr_node != NULL) {
134 nr_neigh_put(nr_neigh);
135 nr_node_put(nr_node);
136 return 0;
137 }
138
139 if (nr_neigh == NULL) {
140 if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL) {
141 if (nr_node)
142 nr_node_put(nr_node);
143 return -ENOMEM;
144 }
145
146 nr_neigh->callsign = *ax25;
147 nr_neigh->digipeat = NULL;
148 nr_neigh->ax25 = NULL;
149 nr_neigh->dev = dev;
150 nr_neigh->quality = sysctl_netrom_default_path_quality;
151 nr_neigh->locked = 0;
152 nr_neigh->count = 0;
153 nr_neigh->number = nr_neigh_no++;
154 nr_neigh->failed = 0;
155 atomic_set(&nr_neigh->refcount, 1);
156
157 if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
158 if ((nr_neigh->digipeat = kmalloc(sizeof(*ax25_digi), GFP_KERNEL)) == NULL) {
159 kfree(nr_neigh);
160 if (nr_node)
161 nr_node_put(nr_node);
162 return -ENOMEM;
163 }
164 memcpy(nr_neigh->digipeat, ax25_digi,
165 sizeof(*ax25_digi));
166 }
167
168 spin_lock_bh(&nr_neigh_list_lock);
169 hlist_add_head(&nr_neigh->neigh_node, &nr_neigh_list);
170 nr_neigh_hold(nr_neigh);
171 spin_unlock_bh(&nr_neigh_list_lock);
172 }
173
174 if (quality != 0 && ax25cmp(nr, ax25) == 0 && !nr_neigh->locked)
175 nr_neigh->quality = quality;
176
177 if (nr_node == NULL) {
178 if ((nr_node = kmalloc(sizeof(*nr_node), GFP_ATOMIC)) == NULL) {
179 if (nr_neigh)
180 nr_neigh_put(nr_neigh);
181 return -ENOMEM;
182 }
183
184 nr_node->callsign = *nr;
185 strcpy(nr_node->mnemonic, mnemonic);
186
187 nr_node->which = 0;
188 nr_node->count = 1;
189 atomic_set(&nr_node->refcount, 1);
190 spin_lock_init(&nr_node->node_lock);
191
192 nr_node->routes[0].quality = quality;
193 nr_node->routes[0].obs_count = obs_count;
194 nr_node->routes[0].neighbour = nr_neigh;
195
196 nr_neigh_hold(nr_neigh);
197 nr_neigh->count++;
198
199 spin_lock_bh(&nr_node_list_lock);
200 hlist_add_head(&nr_node->node_node, &nr_node_list);
201 /* refcount initialized at 1 */
202 spin_unlock_bh(&nr_node_list_lock);
203
204 return 0;
205 }
206 nr_node_lock(nr_node);
207
208 if (quality != 0)
209 strcpy(nr_node->mnemonic, mnemonic);
210
211 for (found = 0, i = 0; i < nr_node->count; i++) {
212 if (nr_node->routes[i].neighbour == nr_neigh) {
213 nr_node->routes[i].quality = quality;
214 nr_node->routes[i].obs_count = obs_count;
215 found = 1;
216 break;
217 }
218 }
219
220 if (!found) {
221 /* We have space at the bottom, slot it in */
222 if (nr_node->count < 3) {
223 nr_node->routes[2] = nr_node->routes[1];
224 nr_node->routes[1] = nr_node->routes[0];
225
226 nr_node->routes[0].quality = quality;
227 nr_node->routes[0].obs_count = obs_count;
228 nr_node->routes[0].neighbour = nr_neigh;
229
230 nr_node->which++;
231 nr_node->count++;
232 nr_neigh_hold(nr_neigh);
233 nr_neigh->count++;
234 } else {
235 /* It must be better than the worst */
236 if (quality > nr_node->routes[2].quality) {
237 nr_node->routes[2].neighbour->count--;
238 nr_neigh_put(nr_node->routes[2].neighbour);
239
240 if (nr_node->routes[2].neighbour->count == 0 && !nr_node->routes[2].neighbour->locked)
241 nr_remove_neigh(nr_node->routes[2].neighbour);
242
243 nr_node->routes[2].quality = quality;
244 nr_node->routes[2].obs_count = obs_count;
245 nr_node->routes[2].neighbour = nr_neigh;
246
247 nr_neigh_hold(nr_neigh);
248 nr_neigh->count++;
249 }
250 }
251 }
252
253 /* Now re-sort the routes in quality order */
254 switch (nr_node->count) {
255 case 3:
256 if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
257 switch (nr_node->which) {
258 case 0: nr_node->which = 1; break;
259 case 1: nr_node->which = 0; break;
260 default: break;
261 }
262 nr_route = nr_node->routes[0];
263 nr_node->routes[0] = nr_node->routes[1];
264 nr_node->routes[1] = nr_route;
265 }
266 if (nr_node->routes[2].quality > nr_node->routes[1].quality) {
267 switch (nr_node->which) {
268 case 1: nr_node->which = 2;
269 break;
270
271 case 2: nr_node->which = 1;
272 break;
273
274 default:
275 break;
276 }
277 nr_route = nr_node->routes[1];
278 nr_node->routes[1] = nr_node->routes[2];
279 nr_node->routes[2] = nr_route;
280 }
281 case 2:
282 if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
283 switch (nr_node->which) {
284 case 0: nr_node->which = 1;
285 break;
286
287 case 1: nr_node->which = 0;
288 break;
289
290 default: break;
291 }
292 nr_route = nr_node->routes[0];
293 nr_node->routes[0] = nr_node->routes[1];
294 nr_node->routes[1] = nr_route;
295 }
296 case 1:
297 break;
298 }
299
300 for (i = 0; i < nr_node->count; i++) {
301 if (nr_node->routes[i].neighbour == nr_neigh) {
302 if (i < nr_node->which)
303 nr_node->which = i;
304 break;
305 }
306 }
307
308 nr_neigh_put(nr_neigh);
309 nr_node_unlock(nr_node);
310 nr_node_put(nr_node);
311 return 0;
312}
313
314static inline void __nr_remove_node(struct nr_node *nr_node)
315{
316 hlist_del_init(&nr_node->node_node);
317 nr_node_put(nr_node);
318}
319
320#define nr_remove_node_locked(__node) \
321 __nr_remove_node(__node)
322
323static void nr_remove_node(struct nr_node *nr_node)
324{
325 spin_lock_bh(&nr_node_list_lock);
326 __nr_remove_node(nr_node);
327 spin_unlock_bh(&nr_node_list_lock);
328}
329
330static inline void __nr_remove_neigh(struct nr_neigh *nr_neigh)
331{
332 hlist_del_init(&nr_neigh->neigh_node);
333 nr_neigh_put(nr_neigh);
334}
335
336#define nr_remove_neigh_locked(__neigh) \
337 __nr_remove_neigh(__neigh)
338
339static void nr_remove_neigh(struct nr_neigh *nr_neigh)
340{
341 spin_lock_bh(&nr_neigh_list_lock);
342 __nr_remove_neigh(nr_neigh);
343 spin_unlock_bh(&nr_neigh_list_lock);
344}
345
346/*
347 * "Delete" a node. Strictly speaking remove a route to a node. The node
348 * is only deleted if no routes are left to it.
349 */
350static int nr_del_node(ax25_address *callsign, ax25_address *neighbour, struct net_device *dev)
351{
352 struct nr_node *nr_node;
353 struct nr_neigh *nr_neigh;
354 int i;
355
356 nr_node = nr_node_get(callsign);
357
358 if (nr_node == NULL)
359 return -EINVAL;
360
361 nr_neigh = nr_neigh_get_dev(neighbour, dev);
362
363 if (nr_neigh == NULL) {
364 nr_node_put(nr_node);
365 return -EINVAL;
366 }
367
368 nr_node_lock(nr_node);
369 for (i = 0; i < nr_node->count; i++) {
370 if (nr_node->routes[i].neighbour == nr_neigh) {
371 nr_neigh->count--;
372 nr_neigh_put(nr_neigh);
373
374 if (nr_neigh->count == 0 && !nr_neigh->locked)
375 nr_remove_neigh(nr_neigh);
376 nr_neigh_put(nr_neigh);
377
378 nr_node->count--;
379
380 if (nr_node->count == 0) {
381 nr_remove_node(nr_node);
382 } else {
383 switch (i) {
384 case 0:
385 nr_node->routes[0] = nr_node->routes[1];
386 case 1:
387 nr_node->routes[1] = nr_node->routes[2];
388 case 2:
389 break;
390 }
391 nr_node_put(nr_node);
392 }
393 nr_node_unlock(nr_node);
394
395 return 0;
396 }
397 }
398 nr_neigh_put(nr_neigh);
399 nr_node_unlock(nr_node);
400 nr_node_put(nr_node);
401
402 return -EINVAL;
403}
404
405/*
406 * Lock a neighbour with a quality.
407 */
408static int nr_add_neigh(ax25_address *callsign, ax25_digi *ax25_digi, struct net_device *dev, unsigned int quality)
409{
410 struct nr_neigh *nr_neigh;
411
412 nr_neigh = nr_neigh_get_dev(callsign, dev);
413 if (nr_neigh) {
414 nr_neigh->quality = quality;
415 nr_neigh->locked = 1;
416 nr_neigh_put(nr_neigh);
417 return 0;
418 }
419
420 if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL)
421 return -ENOMEM;
422
423 nr_neigh->callsign = *callsign;
424 nr_neigh->digipeat = NULL;
425 nr_neigh->ax25 = NULL;
426 nr_neigh->dev = dev;
427 nr_neigh->quality = quality;
428 nr_neigh->locked = 1;
429 nr_neigh->count = 0;
430 nr_neigh->number = nr_neigh_no++;
431 nr_neigh->failed = 0;
432 atomic_set(&nr_neigh->refcount, 1);
433
434 if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
435 if ((nr_neigh->digipeat = kmalloc(sizeof(*ax25_digi), GFP_KERNEL)) == NULL) {
436 kfree(nr_neigh);
437 return -ENOMEM;
438 }
439 memcpy(nr_neigh->digipeat, ax25_digi, sizeof(*ax25_digi));
440 }
441
442 spin_lock_bh(&nr_neigh_list_lock);
443 hlist_add_head(&nr_neigh->neigh_node, &nr_neigh_list);
444 /* refcount is initialized at 1 */
445 spin_unlock_bh(&nr_neigh_list_lock);
446
447 return 0;
448}
449
450/*
451 * "Delete" a neighbour. The neighbour is only removed if the number
452 * of nodes that may use it is zero.
453 */
454static int nr_del_neigh(ax25_address *callsign, struct net_device *dev, unsigned int quality)
455{
456 struct nr_neigh *nr_neigh;
457
458 nr_neigh = nr_neigh_get_dev(callsign, dev);
459
460 if (nr_neigh == NULL) return -EINVAL;
461
462 nr_neigh->quality = quality;
463 nr_neigh->locked = 0;
464
465 if (nr_neigh->count == 0)
466 nr_remove_neigh(nr_neigh);
467 nr_neigh_put(nr_neigh);
468
469 return 0;
470}
471
472/*
473 * Decrement the obsolescence count by one. If a route is reduced to a
474 * count of zero, remove it. Also remove any unlocked neighbours with
475 * zero nodes routing via it.
476 */
477static int nr_dec_obs(void)
478{
479 struct nr_neigh *nr_neigh;
480 struct nr_node *s;
481 struct hlist_node *node, *nodet;
482 int i;
483
484 spin_lock_bh(&nr_node_list_lock);
485 nr_node_for_each_safe(s, node, nodet, &nr_node_list) {
486 nr_node_lock(s);
487 for (i = 0; i < s->count; i++) {
488 switch (s->routes[i].obs_count) {
489 case 0: /* A locked entry */
490 break;
491
492 case 1: /* From 1 -> 0 */
493 nr_neigh = s->routes[i].neighbour;
494
495 nr_neigh->count--;
496 nr_neigh_put(nr_neigh);
497
498 if (nr_neigh->count == 0 && !nr_neigh->locked)
499 nr_remove_neigh(nr_neigh);
500
501 s->count--;
502
503 switch (i) {
504 case 0:
505 s->routes[0] = s->routes[1];
506 case 1:
507 s->routes[1] = s->routes[2];
508 case 2:
509 break;
510 }
511 break;
512
513 default:
514 s->routes[i].obs_count--;
515 break;
516
517 }
518 }
519
520 if (s->count <= 0)
521 nr_remove_node_locked(s);
522 nr_node_unlock(s);
523 }
524 spin_unlock_bh(&nr_node_list_lock);
525
526 return 0;
527}
528
529/*
530 * A device has been removed. Remove its routes and neighbours.
531 */
532void nr_rt_device_down(struct net_device *dev)
533{
534 struct nr_neigh *s;
535 struct hlist_node *node, *nodet, *node2, *node2t;
536 struct nr_node *t;
537 int i;
538
539 spin_lock_bh(&nr_neigh_list_lock);
540 nr_neigh_for_each_safe(s, node, nodet, &nr_neigh_list) {
541 if (s->dev == dev) {
542 spin_lock_bh(&nr_node_list_lock);
543 nr_node_for_each_safe(t, node2, node2t, &nr_node_list) {
544 nr_node_lock(t);
545 for (i = 0; i < t->count; i++) {
546 if (t->routes[i].neighbour == s) {
547 t->count--;
548
549 switch (i) {
550 case 0:
551 t->routes[0] = t->routes[1];
552 case 1:
553 t->routes[1] = t->routes[2];
554 case 2:
555 break;
556 }
557 }
558 }
559
560 if (t->count <= 0)
561 nr_remove_node_locked(t);
562 nr_node_unlock(t);
563 }
564 spin_unlock_bh(&nr_node_list_lock);
565
566 nr_remove_neigh_locked(s);
567 }
568 }
569 spin_unlock_bh(&nr_neigh_list_lock);
570}
571
572/*
573 * Check that the device given is a valid AX.25 interface that is "up".
574 * Or a valid ethernet interface with an AX.25 callsign binding.
575 */
576static struct net_device *nr_ax25_dev_get(char *devname)
577{
578 struct net_device *dev;
579
580 if ((dev = dev_get_by_name(devname)) == NULL)
581 return NULL;
582
583 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
584 return dev;
585
586 dev_put(dev);
587 return NULL;
588}
589
590/*
591 * Find the first active NET/ROM device, usually "nr0".
592 */
593struct net_device *nr_dev_first(void)
594{
595 struct net_device *dev, *first = NULL;
596
597 read_lock(&dev_base_lock);
598 for (dev = dev_base; dev != NULL; dev = dev->next) {
599 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM)
600 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
601 first = dev;
602 }
603 if (first)
604 dev_hold(first);
605 read_unlock(&dev_base_lock);
606
607 return first;
608}
609
610/*
611 * Find the NET/ROM device for the given callsign.
612 */
613struct net_device *nr_dev_get(ax25_address *addr)
614{
615 struct net_device *dev;
616
617 read_lock(&dev_base_lock);
618 for (dev = dev_base; dev != NULL; dev = dev->next) {
619 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM && ax25cmp(addr, (ax25_address *)dev->dev_addr) == 0) {
620 dev_hold(dev);
621 goto out;
622 }
623 }
624out:
625 read_unlock(&dev_base_lock);
626 return dev;
627}
628
629static ax25_digi *nr_call_to_digi(int ndigis, ax25_address *digipeaters)
630{
631 static ax25_digi ax25_digi;
632 int i;
633
634 if (ndigis == 0)
635 return NULL;
636
637 for (i = 0; i < ndigis; i++) {
638 ax25_digi.calls[i] = digipeaters[i];
639 ax25_digi.repeated[i] = 0;
640 }
641
642 ax25_digi.ndigi = ndigis;
643 ax25_digi.lastrepeat = -1;
644
645 return &ax25_digi;
646}
647
648/*
649 * Handle the ioctls that control the routing functions.
650 */
651int nr_rt_ioctl(unsigned int cmd, void __user *arg)
652{
653 struct nr_route_struct nr_route;
654 struct net_device *dev;
655 int ret;
656
657 switch (cmd) {
658 case SIOCADDRT:
659 if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
660 return -EFAULT;
661 if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
662 return -EINVAL;
663 if (nr_route.ndigis < 0 || nr_route.ndigis > AX25_MAX_DIGIS) {
664 dev_put(dev);
665 return -EINVAL;
666 }
667 switch (nr_route.type) {
668 case NETROM_NODE:
669 ret = nr_add_node(&nr_route.callsign,
670 nr_route.mnemonic,
671 &nr_route.neighbour,
672 nr_call_to_digi(nr_route.ndigis, nr_route.digipeaters),
673 dev, nr_route.quality,
674 nr_route.obs_count);
675 break;
676 case NETROM_NEIGH:
677 ret = nr_add_neigh(&nr_route.callsign,
678 nr_call_to_digi(nr_route.ndigis, nr_route.digipeaters),
679 dev, nr_route.quality);
680 break;
681 default:
682 ret = -EINVAL;
683 }
684 dev_put(dev);
685 return ret;
686
687 case SIOCDELRT:
688 if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
689 return -EFAULT;
690 if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
691 return -EINVAL;
692 switch (nr_route.type) {
693 case NETROM_NODE:
694 ret = nr_del_node(&nr_route.callsign,
695 &nr_route.neighbour, dev);
696 break;
697 case NETROM_NEIGH:
698 ret = nr_del_neigh(&nr_route.callsign,
699 dev, nr_route.quality);
700 break;
701 default:
702 ret = -EINVAL;
703 }
704 dev_put(dev);
705 return ret;
706
707 case SIOCNRDECOBS:
708 return nr_dec_obs();
709
710 default:
711 return -EINVAL;
712 }
713
714 return 0;
715}
716
717/*
718 * A level 2 link has timed out, therefore it appears to be a poor link,
719 * then don't use that neighbour until it is reset.
720 */
721void nr_link_failed(ax25_cb *ax25, int reason)
722{
723 struct nr_neigh *s, *nr_neigh = NULL;
724 struct hlist_node *node;
725 struct nr_node *nr_node = NULL;
726
727 spin_lock_bh(&nr_neigh_list_lock);
728 nr_neigh_for_each(s, node, &nr_neigh_list)
729 if (s->ax25 == ax25) {
730 nr_neigh_hold(s);
731 nr_neigh = s;
732 break;
733 }
734 spin_unlock_bh(&nr_neigh_list_lock);
735
736 if (nr_neigh == NULL) return;
737
738 nr_neigh->ax25 = NULL;
739 ax25_cb_put(ax25);
740
741 if (++nr_neigh->failed < sysctl_netrom_link_fails_count) {
742 nr_neigh_put(nr_neigh);
743 return;
744 }
745 spin_lock_bh(&nr_node_list_lock);
746 nr_node_for_each(nr_node, node, &nr_node_list)
747 nr_node_lock(nr_node);
748 if (nr_node->which < nr_node->count && nr_node->routes[nr_node->which].neighbour == nr_neigh)
749 nr_node->which++;
750 nr_node_unlock(nr_node);
751 spin_unlock_bh(&nr_node_list_lock);
752 nr_neigh_put(nr_neigh);
753}
754
755/*
756 * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
757 * indicates an internally generated frame.
758 */
759int nr_route_frame(struct sk_buff *skb, ax25_cb *ax25)
760{
761 ax25_address *nr_src, *nr_dest;
762 struct nr_neigh *nr_neigh;
763 struct nr_node *nr_node;
764 struct net_device *dev;
765 unsigned char *dptr;
766 ax25_cb *ax25s;
767 int ret;
768 struct sk_buff *skbn;
769
770
771 nr_src = (ax25_address *)(skb->data + 0);
772 nr_dest = (ax25_address *)(skb->data + 7);
773
774 if (ax25 != NULL)
775 nr_add_node(nr_src, "", &ax25->dest_addr, ax25->digipeat,
776 ax25->ax25_dev->dev, 0, sysctl_netrom_obsolescence_count_initialiser);
777
778 if ((dev = nr_dev_get(nr_dest)) != NULL) { /* Its for me */
779 if (ax25 == NULL) /* Its from me */
780 ret = nr_loopback_queue(skb);
781 else
782 ret = nr_rx_frame(skb, dev);
783 dev_put(dev);
784 return ret;
785 }
786
787 if (!sysctl_netrom_routing_control && ax25 != NULL)
788 return 0;
789
790 /* Its Time-To-Live has expired */
791 if (skb->data[14] == 1) {
792 return 0;
793 }
794
795 nr_node = nr_node_get(nr_dest);
796 if (nr_node == NULL)
797 return 0;
798 nr_node_lock(nr_node);
799
800 if (nr_node->which >= nr_node->count) {
801 nr_node_unlock(nr_node);
802 nr_node_put(nr_node);
803 return 0;
804 }
805
806 nr_neigh = nr_node->routes[nr_node->which].neighbour;
807
808 if ((dev = nr_dev_first()) == NULL) {
809 nr_node_unlock(nr_node);
810 nr_node_put(nr_node);
811 return 0;
812 }
813
814 /* We are going to change the netrom headers so we should get our
815 own skb, we also did not know until now how much header space
816 we had to reserve... - RXQ */
817 if ((skbn=skb_copy_expand(skb, dev->hard_header_len, 0, GFP_ATOMIC)) == NULL) {
818 nr_node_unlock(nr_node);
819 nr_node_put(nr_node);
820 dev_put(dev);
821 return 0;
822 }
823 kfree_skb(skb);
824 skb=skbn;
825 skb->data[14]--;
826
827 dptr = skb_push(skb, 1);
828 *dptr = AX25_P_NETROM;
829
830 ax25s = ax25_send_frame(skb, 256, (ax25_address *)dev->dev_addr, &nr_neigh->callsign, nr_neigh->digipeat, nr_neigh->dev);
831 if (nr_neigh->ax25 && ax25s) {
832 /* We were already holding this ax25_cb */
833 ax25_cb_put(ax25s);
834 }
835 nr_neigh->ax25 = ax25s;
836
837 dev_put(dev);
838 ret = (nr_neigh->ax25 != NULL);
839 nr_node_unlock(nr_node);
840 nr_node_put(nr_node);
841 return ret;
842}
843
844#ifdef CONFIG_PROC_FS
845
846static void *nr_node_start(struct seq_file *seq, loff_t *pos)
847{
848 struct nr_node *nr_node;
849 struct hlist_node *node;
850 int i = 1;
851
852 spin_lock_bh(&nr_node_list_lock);
853 if (*pos == 0)
854 return SEQ_START_TOKEN;
855
856 nr_node_for_each(nr_node, node, &nr_node_list) {
857 if (i == *pos)
858 return nr_node;
859 ++i;
860 }
861
862 return NULL;
863}
864
865static void *nr_node_next(struct seq_file *seq, void *v, loff_t *pos)
866{
867 struct hlist_node *node;
868 ++*pos;
869
870 node = (v == SEQ_START_TOKEN)
871 ? nr_node_list.first
872 : ((struct nr_node *)v)->node_node.next;
873
874 return hlist_entry(node, struct nr_node, node_node);
875}
876
877static void nr_node_stop(struct seq_file *seq, void *v)
878{
879 spin_unlock_bh(&nr_node_list_lock);
880}
881
882static int nr_node_show(struct seq_file *seq, void *v)
883{
884 int i;
885
886 if (v == SEQ_START_TOKEN)
887 seq_puts(seq,
888 "callsign mnemonic w n qual obs neigh qual obs neigh qual obs neigh\n");
889 else {
890 struct nr_node *nr_node = v;
891 nr_node_lock(nr_node);
892 seq_printf(seq, "%-9s %-7s %d %d",
893 ax2asc(&nr_node->callsign),
894 (nr_node->mnemonic[0] == '\0') ? "*" : nr_node->mnemonic,
895 nr_node->which + 1,
896 nr_node->count);
897
898 for (i = 0; i < nr_node->count; i++) {
899 seq_printf(seq, " %3d %d %05d",
900 nr_node->routes[i].quality,
901 nr_node->routes[i].obs_count,
902 nr_node->routes[i].neighbour->number);
903 }
904 nr_node_unlock(nr_node);
905
906 seq_puts(seq, "\n");
907 }
908 return 0;
909}
910
911static struct seq_operations nr_node_seqops = {
912 .start = nr_node_start,
913 .next = nr_node_next,
914 .stop = nr_node_stop,
915 .show = nr_node_show,
916};
917
918static int nr_node_info_open(struct inode *inode, struct file *file)
919{
920 return seq_open(file, &nr_node_seqops);
921}
922
923struct file_operations nr_nodes_fops = {
924 .owner = THIS_MODULE,
925 .open = nr_node_info_open,
926 .read = seq_read,
927 .llseek = seq_lseek,
928 .release = seq_release,
929};
930
931static void *nr_neigh_start(struct seq_file *seq, loff_t *pos)
932{
933 struct nr_neigh *nr_neigh;
934 struct hlist_node *node;
935 int i = 1;
936
937 spin_lock_bh(&nr_neigh_list_lock);
938 if (*pos == 0)
939 return SEQ_START_TOKEN;
940
941 nr_neigh_for_each(nr_neigh, node, &nr_neigh_list) {
942 if (i == *pos)
943 return nr_neigh;
944 }
945 return NULL;
946}
947
948static void *nr_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
949{
950 struct hlist_node *node;
951 ++*pos;
952
953 node = (v == SEQ_START_TOKEN)
954 ? nr_neigh_list.first
955 : ((struct nr_neigh *)v)->neigh_node.next;
956
957 return hlist_entry(node, struct nr_neigh, neigh_node);
958}
959
960static void nr_neigh_stop(struct seq_file *seq, void *v)
961{
962 spin_unlock_bh(&nr_neigh_list_lock);
963}
964
965static int nr_neigh_show(struct seq_file *seq, void *v)
966{
967 int i;
968
969 if (v == SEQ_START_TOKEN)
970 seq_puts(seq, "addr callsign dev qual lock count failed digipeaters\n");
971 else {
972 struct nr_neigh *nr_neigh = v;
973
974 seq_printf(seq, "%05d %-9s %-4s %3d %d %3d %3d",
975 nr_neigh->number,
976 ax2asc(&nr_neigh->callsign),
977 nr_neigh->dev ? nr_neigh->dev->name : "???",
978 nr_neigh->quality,
979 nr_neigh->locked,
980 nr_neigh->count,
981 nr_neigh->failed);
982
983 if (nr_neigh->digipeat != NULL) {
984 for (i = 0; i < nr_neigh->digipeat->ndigi; i++)
985 seq_printf(seq, " %s",
986 ax2asc(&nr_neigh->digipeat->calls[i]));
987 }
988
989 seq_puts(seq, "\n");
990 }
991 return 0;
992}
993
994static struct seq_operations nr_neigh_seqops = {
995 .start = nr_neigh_start,
996 .next = nr_neigh_next,
997 .stop = nr_neigh_stop,
998 .show = nr_neigh_show,
999};
1000
1001static int nr_neigh_info_open(struct inode *inode, struct file *file)
1002{
1003 return seq_open(file, &nr_neigh_seqops);
1004}
1005
1006struct file_operations nr_neigh_fops = {
1007 .owner = THIS_MODULE,
1008 .open = nr_neigh_info_open,
1009 .read = seq_read,
1010 .llseek = seq_lseek,
1011 .release = seq_release,
1012};
1013
1014#endif
1015
1016/*
1017 * Free all memory associated with the nodes and routes lists.
1018 */
1019void __exit nr_rt_free(void)
1020{
1021 struct nr_neigh *s = NULL;
1022 struct nr_node *t = NULL;
1023 struct hlist_node *node, *nodet;
1024
1025 spin_lock_bh(&nr_neigh_list_lock);
1026 spin_lock_bh(&nr_node_list_lock);
1027 nr_node_for_each_safe(t, node, nodet, &nr_node_list) {
1028 nr_node_lock(t);
1029 nr_remove_node_locked(t);
1030 nr_node_unlock(t);
1031 }
1032 nr_neigh_for_each_safe(s, node, nodet, &nr_neigh_list) {
1033 while(s->count) {
1034 s->count--;
1035 nr_neigh_put(s);
1036 }
1037 nr_remove_neigh_locked(s);
1038 }
1039 spin_unlock_bh(&nr_node_list_lock);
1040 spin_unlock_bh(&nr_neigh_list_lock);
1041}
diff --git a/net/netrom/nr_subr.c b/net/netrom/nr_subr.c
new file mode 100644
index 000000000000..0627347b14b8
--- /dev/null
+++ b/net/netrom/nr_subr.c
@@ -0,0 +1,283 @@
1/*
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 */
9#include <linux/errno.h>
10#include <linux/types.h>
11#include <linux/socket.h>
12#include <linux/in.h>
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/timer.h>
16#include <linux/string.h>
17#include <linux/sockios.h>
18#include <linux/net.h>
19#include <net/ax25.h>
20#include <linux/inet.h>
21#include <linux/netdevice.h>
22#include <linux/skbuff.h>
23#include <net/sock.h>
24#include <net/tcp.h>
25#include <asm/uaccess.h>
26#include <asm/system.h>
27#include <linux/fcntl.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30#include <net/netrom.h>
31
32/*
33 * This routine purges all of the queues of frames.
34 */
35void nr_clear_queues(struct sock *sk)
36{
37 struct nr_sock *nr = nr_sk(sk);
38
39 skb_queue_purge(&sk->sk_write_queue);
40 skb_queue_purge(&nr->ack_queue);
41 skb_queue_purge(&nr->reseq_queue);
42 skb_queue_purge(&nr->frag_queue);
43}
44
45/*
46 * This routine purges the input queue of those frames that have been
47 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
48 * SDL diagram.
49 */
50void nr_frames_acked(struct sock *sk, unsigned short nr)
51{
52 struct nr_sock *nrom = nr_sk(sk);
53 struct sk_buff *skb;
54
55 /*
56 * Remove all the ack-ed frames from the ack queue.
57 */
58 if (nrom->va != nr) {
59 while (skb_peek(&nrom->ack_queue) != NULL && nrom->va != nr) {
60 skb = skb_dequeue(&nrom->ack_queue);
61 kfree_skb(skb);
62 nrom->va = (nrom->va + 1) % NR_MODULUS;
63 }
64 }
65}
66
67/*
68 * Requeue all the un-ack-ed frames on the output queue to be picked
69 * up by nr_kick called from the timer. This arrangement handles the
70 * possibility of an empty output queue.
71 */
72void nr_requeue_frames(struct sock *sk)
73{
74 struct sk_buff *skb, *skb_prev = NULL;
75
76 while ((skb = skb_dequeue(&nr_sk(sk)->ack_queue)) != NULL) {
77 if (skb_prev == NULL)
78 skb_queue_head(&sk->sk_write_queue, skb);
79 else
80 skb_append(skb_prev, skb);
81 skb_prev = skb;
82 }
83}
84
85/*
86 * Validate that the value of nr is between va and vs. Return true or
87 * false for testing.
88 */
89int nr_validate_nr(struct sock *sk, unsigned short nr)
90{
91 struct nr_sock *nrom = nr_sk(sk);
92 unsigned short vc = nrom->va;
93
94 while (vc != nrom->vs) {
95 if (nr == vc) return 1;
96 vc = (vc + 1) % NR_MODULUS;
97 }
98
99 return nr == nrom->vs;
100}
101
102/*
103 * Check that ns is within the receive window.
104 */
105int nr_in_rx_window(struct sock *sk, unsigned short ns)
106{
107 struct nr_sock *nr = nr_sk(sk);
108 unsigned short vc = nr->vr;
109 unsigned short vt = (nr->vl + nr->window) % NR_MODULUS;
110
111 while (vc != vt) {
112 if (ns == vc) return 1;
113 vc = (vc + 1) % NR_MODULUS;
114 }
115
116 return 0;
117}
118
119/*
120 * This routine is called when the HDLC layer internally generates a
121 * control frame.
122 */
123void nr_write_internal(struct sock *sk, int frametype)
124{
125 struct nr_sock *nr = nr_sk(sk);
126 struct sk_buff *skb;
127 unsigned char *dptr;
128 int len, timeout;
129
130 len = NR_NETWORK_LEN + NR_TRANSPORT_LEN;
131
132 switch (frametype & 0x0F) {
133 case NR_CONNREQ:
134 len += 17;
135 break;
136 case NR_CONNACK:
137 len += (nr->bpqext) ? 2 : 1;
138 break;
139 case NR_DISCREQ:
140 case NR_DISCACK:
141 case NR_INFOACK:
142 break;
143 default:
144 printk(KERN_ERR "NET/ROM: nr_write_internal - invalid frame type %d\n", frametype);
145 return;
146 }
147
148 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
149 return;
150
151 /*
152 * Space for AX.25 and NET/ROM network header
153 */
154 skb_reserve(skb, NR_NETWORK_LEN);
155
156 dptr = skb_put(skb, skb_tailroom(skb));
157
158 switch (frametype & 0x0F) {
159 case NR_CONNREQ:
160 timeout = nr->t1 / HZ;
161 *dptr++ = nr->my_index;
162 *dptr++ = nr->my_id;
163 *dptr++ = 0;
164 *dptr++ = 0;
165 *dptr++ = frametype;
166 *dptr++ = nr->window;
167 memcpy(dptr, &nr->user_addr, AX25_ADDR_LEN);
168 dptr[6] &= ~AX25_CBIT;
169 dptr[6] &= ~AX25_EBIT;
170 dptr[6] |= AX25_SSSID_SPARE;
171 dptr += AX25_ADDR_LEN;
172 memcpy(dptr, &nr->source_addr, AX25_ADDR_LEN);
173 dptr[6] &= ~AX25_CBIT;
174 dptr[6] &= ~AX25_EBIT;
175 dptr[6] |= AX25_SSSID_SPARE;
176 dptr += AX25_ADDR_LEN;
177 *dptr++ = timeout % 256;
178 *dptr++ = timeout / 256;
179 break;
180
181 case NR_CONNACK:
182 *dptr++ = nr->your_index;
183 *dptr++ = nr->your_id;
184 *dptr++ = nr->my_index;
185 *dptr++ = nr->my_id;
186 *dptr++ = frametype;
187 *dptr++ = nr->window;
188 if (nr->bpqext) *dptr++ = sysctl_netrom_network_ttl_initialiser;
189 break;
190
191 case NR_DISCREQ:
192 case NR_DISCACK:
193 *dptr++ = nr->your_index;
194 *dptr++ = nr->your_id;
195 *dptr++ = 0;
196 *dptr++ = 0;
197 *dptr++ = frametype;
198 break;
199
200 case NR_INFOACK:
201 *dptr++ = nr->your_index;
202 *dptr++ = nr->your_id;
203 *dptr++ = 0;
204 *dptr++ = nr->vr;
205 *dptr++ = frametype;
206 break;
207 }
208
209 nr_transmit_buffer(sk, skb);
210}
211
212/*
213 * This routine is called when a Connect Acknowledge with the Choke Flag
214 * set is needed to refuse a connection.
215 */
216void nr_transmit_refusal(struct sk_buff *skb, int mine)
217{
218 struct sk_buff *skbn;
219 unsigned char *dptr;
220 int len;
221
222 len = NR_NETWORK_LEN + NR_TRANSPORT_LEN + 1;
223
224 if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL)
225 return;
226
227 skb_reserve(skbn, 0);
228
229 dptr = skb_put(skbn, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
230
231 memcpy(dptr, skb->data + 7, AX25_ADDR_LEN);
232 dptr[6] &= ~AX25_CBIT;
233 dptr[6] &= ~AX25_EBIT;
234 dptr[6] |= AX25_SSSID_SPARE;
235 dptr += AX25_ADDR_LEN;
236
237 memcpy(dptr, skb->data + 0, AX25_ADDR_LEN);
238 dptr[6] &= ~AX25_CBIT;
239 dptr[6] |= AX25_EBIT;
240 dptr[6] |= AX25_SSSID_SPARE;
241 dptr += AX25_ADDR_LEN;
242
243 *dptr++ = sysctl_netrom_network_ttl_initialiser;
244
245 if (mine) {
246 *dptr++ = 0;
247 *dptr++ = 0;
248 *dptr++ = skb->data[15];
249 *dptr++ = skb->data[16];
250 } else {
251 *dptr++ = skb->data[15];
252 *dptr++ = skb->data[16];
253 *dptr++ = 0;
254 *dptr++ = 0;
255 }
256
257 *dptr++ = NR_CONNACK | NR_CHOKE_FLAG;
258 *dptr++ = 0;
259
260 if (!nr_route_frame(skbn, NULL))
261 kfree_skb(skbn);
262}
263
264void nr_disconnect(struct sock *sk, int reason)
265{
266 nr_stop_t1timer(sk);
267 nr_stop_t2timer(sk);
268 nr_stop_t4timer(sk);
269 nr_stop_idletimer(sk);
270
271 nr_clear_queues(sk);
272
273 nr_sk(sk)->state = NR_STATE_0;
274
275 sk->sk_state = TCP_CLOSE;
276 sk->sk_err = reason;
277 sk->sk_shutdown |= SEND_SHUTDOWN;
278
279 if (!sock_flag(sk, SOCK_DEAD)) {
280 sk->sk_state_change(sk);
281 sock_set_flag(sk, SOCK_DEAD);
282 }
283}
diff --git a/net/netrom/nr_timer.c b/net/netrom/nr_timer.c
new file mode 100644
index 000000000000..faabda8088be
--- /dev/null
+++ b/net/netrom/nr_timer.c
@@ -0,0 +1,260 @@
1/*
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 (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
9 */
10#include <linux/errno.h>
11#include <linux/types.h>
12#include <linux/socket.h>
13#include <linux/in.h>
14#include <linux/kernel.h>
15#include <linux/jiffies.h>
16#include <linux/timer.h>
17#include <linux/string.h>
18#include <linux/sockios.h>
19#include <linux/net.h>
20#include <net/ax25.h>
21#include <linux/inet.h>
22#include <linux/netdevice.h>
23#include <linux/skbuff.h>
24#include <net/sock.h>
25#include <net/tcp.h>
26#include <asm/uaccess.h>
27#include <asm/system.h>
28#include <linux/fcntl.h>
29#include <linux/mm.h>
30#include <linux/interrupt.h>
31#include <net/netrom.h>
32
33static void nr_heartbeat_expiry(unsigned long);
34static void nr_t1timer_expiry(unsigned long);
35static void nr_t2timer_expiry(unsigned long);
36static void nr_t4timer_expiry(unsigned long);
37static void nr_idletimer_expiry(unsigned long);
38
39void nr_init_timers(struct sock *sk)
40{
41 struct nr_sock *nr = nr_sk(sk);
42
43 init_timer(&nr->t1timer);
44 nr->t1timer.data = (unsigned long)sk;
45 nr->t1timer.function = &nr_t1timer_expiry;
46
47 init_timer(&nr->t2timer);
48 nr->t2timer.data = (unsigned long)sk;
49 nr->t2timer.function = &nr_t2timer_expiry;
50
51 init_timer(&nr->t4timer);
52 nr->t4timer.data = (unsigned long)sk;
53 nr->t4timer.function = &nr_t4timer_expiry;
54
55 init_timer(&nr->idletimer);
56 nr->idletimer.data = (unsigned long)sk;
57 nr->idletimer.function = &nr_idletimer_expiry;
58
59 /* initialized by sock_init_data */
60 sk->sk_timer.data = (unsigned long)sk;
61 sk->sk_timer.function = &nr_heartbeat_expiry;
62}
63
64void nr_start_t1timer(struct sock *sk)
65{
66 struct nr_sock *nr = nr_sk(sk);
67
68 mod_timer(&nr->t1timer, jiffies + nr->t1);
69}
70
71void nr_start_t2timer(struct sock *sk)
72{
73 struct nr_sock *nr = nr_sk(sk);
74
75 mod_timer(&nr->t2timer, jiffies + nr->t2);
76}
77
78void nr_start_t4timer(struct sock *sk)
79{
80 struct nr_sock *nr = nr_sk(sk);
81
82 mod_timer(&nr->t4timer, jiffies + nr->t4);
83}
84
85void nr_start_idletimer(struct sock *sk)
86{
87 struct nr_sock *nr = nr_sk(sk);
88
89 if (nr->idle > 0)
90 mod_timer(&nr->idletimer, jiffies + nr->idle);
91}
92
93void nr_start_heartbeat(struct sock *sk)
94{
95 mod_timer(&sk->sk_timer, jiffies + 5 * HZ);
96}
97
98void nr_stop_t1timer(struct sock *sk)
99{
100 del_timer(&nr_sk(sk)->t1timer);
101}
102
103void nr_stop_t2timer(struct sock *sk)
104{
105 del_timer(&nr_sk(sk)->t2timer);
106}
107
108void nr_stop_t4timer(struct sock *sk)
109{
110 del_timer(&nr_sk(sk)->t4timer);
111}
112
113void nr_stop_idletimer(struct sock *sk)
114{
115 del_timer(&nr_sk(sk)->idletimer);
116}
117
118void nr_stop_heartbeat(struct sock *sk)
119{
120 del_timer(&sk->sk_timer);
121}
122
123int nr_t1timer_running(struct sock *sk)
124{
125 return timer_pending(&nr_sk(sk)->t1timer);
126}
127
128static void nr_heartbeat_expiry(unsigned long param)
129{
130 struct sock *sk = (struct sock *)param;
131 struct nr_sock *nr = nr_sk(sk);
132
133 bh_lock_sock(sk);
134 switch (nr->state) {
135 case NR_STATE_0:
136 /* Magic here: If we listen() and a new link dies before it
137 is accepted() it isn't 'dead' so doesn't get removed. */
138 if (sock_flag(sk, SOCK_DESTROY) ||
139 (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
140 sock_hold(sk);
141 nr_destroy_socket(sk);
142 bh_unlock_sock(sk);
143 sock_put(sk);
144 return;
145 }
146 break;
147
148 case NR_STATE_3:
149 /*
150 * Check for the state of the receive buffer.
151 */
152 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
153 (nr->condition & NR_COND_OWN_RX_BUSY)) {
154 nr->condition &= ~NR_COND_OWN_RX_BUSY;
155 nr->condition &= ~NR_COND_ACK_PENDING;
156 nr->vl = nr->vr;
157 nr_write_internal(sk, NR_INFOACK);
158 break;
159 }
160 break;
161 }
162
163 nr_start_heartbeat(sk);
164 bh_unlock_sock(sk);
165}
166
167static void nr_t2timer_expiry(unsigned long param)
168{
169 struct sock *sk = (struct sock *)param;
170 struct nr_sock *nr = nr_sk(sk);
171
172 bh_lock_sock(sk);
173 if (nr->condition & NR_COND_ACK_PENDING) {
174 nr->condition &= ~NR_COND_ACK_PENDING;
175 nr_enquiry_response(sk);
176 }
177 bh_unlock_sock(sk);
178}
179
180static void nr_t4timer_expiry(unsigned long param)
181{
182 struct sock *sk = (struct sock *)param;
183
184 bh_lock_sock(sk);
185 nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
186 bh_unlock_sock(sk);
187}
188
189static void nr_idletimer_expiry(unsigned long param)
190{
191 struct sock *sk = (struct sock *)param;
192 struct nr_sock *nr = nr_sk(sk);
193
194 bh_lock_sock(sk);
195
196 nr_clear_queues(sk);
197
198 nr->n2count = 0;
199 nr_write_internal(sk, NR_DISCREQ);
200 nr->state = NR_STATE_2;
201
202 nr_start_t1timer(sk);
203 nr_stop_t2timer(sk);
204 nr_stop_t4timer(sk);
205
206 sk->sk_state = TCP_CLOSE;
207 sk->sk_err = 0;
208 sk->sk_shutdown |= SEND_SHUTDOWN;
209
210 if (!sock_flag(sk, SOCK_DEAD)) {
211 sk->sk_state_change(sk);
212 sock_set_flag(sk, SOCK_DEAD);
213 }
214 bh_unlock_sock(sk);
215}
216
217static void nr_t1timer_expiry(unsigned long param)
218{
219 struct sock *sk = (struct sock *)param;
220 struct nr_sock *nr = nr_sk(sk);
221
222 bh_lock_sock(sk);
223 switch (nr->state) {
224 case NR_STATE_1:
225 if (nr->n2count == nr->n2) {
226 nr_disconnect(sk, ETIMEDOUT);
227 bh_unlock_sock(sk);
228 return;
229 } else {
230 nr->n2count++;
231 nr_write_internal(sk, NR_CONNREQ);
232 }
233 break;
234
235 case NR_STATE_2:
236 if (nr->n2count == nr->n2) {
237 nr_disconnect(sk, ETIMEDOUT);
238 bh_unlock_sock(sk);
239 return;
240 } else {
241 nr->n2count++;
242 nr_write_internal(sk, NR_DISCREQ);
243 }
244 break;
245
246 case NR_STATE_3:
247 if (nr->n2count == nr->n2) {
248 nr_disconnect(sk, ETIMEDOUT);
249 bh_unlock_sock(sk);
250 return;
251 } else {
252 nr->n2count++;
253 nr_requeue_frames(sk);
254 }
255 break;
256 }
257
258 nr_start_t1timer(sk);
259 bh_unlock_sock(sk);
260}
diff --git a/net/netrom/sysctl_net_netrom.c b/net/netrom/sysctl_net_netrom.c
new file mode 100644
index 000000000000..c9ed50382ea7
--- /dev/null
+++ b/net/netrom/sysctl_net_netrom.c
@@ -0,0 +1,189 @@
1/*
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 (C) 1996 Mike Shaver (shaver@zeroknowledge.com)
8 */
9#include <linux/mm.h>
10#include <linux/sysctl.h>
11#include <linux/init.h>
12#include <net/ax25.h>
13#include <net/netrom.h>
14
15/*
16 * Values taken from NET/ROM documentation.
17 */
18static int min_quality[] = {0}, max_quality[] = {255};
19static int min_obs[] = {0}, max_obs[] = {255};
20static int min_ttl[] = {0}, max_ttl[] = {255};
21static int min_t1[] = {5 * HZ};
22static int max_t1[] = {600 * HZ};
23static int min_n2[] = {2}, max_n2[] = {127};
24static int min_t2[] = {1 * HZ};
25static int max_t2[] = {60 * HZ};
26static int min_t4[] = {1 * HZ};
27static int max_t4[] = {1000 * HZ};
28static int min_window[] = {1}, max_window[] = {127};
29static int min_idle[] = {0 * HZ};
30static int max_idle[] = {65535 * HZ};
31static int min_route[] = {0}, max_route[] = {1};
32static int min_fails[] = {1}, max_fails[] = {10};
33
34static struct ctl_table_header *nr_table_header;
35
36static ctl_table nr_table[] = {
37 {
38 .ctl_name = NET_NETROM_DEFAULT_PATH_QUALITY,
39 .procname = "default_path_quality",
40 .data = &sysctl_netrom_default_path_quality,
41 .maxlen = sizeof(int),
42 .mode = 0644,
43 .proc_handler = &proc_dointvec_minmax,
44 .strategy = &sysctl_intvec,
45 .extra1 = &min_quality,
46 .extra2 = &max_quality
47 },
48 {
49 .ctl_name = NET_NETROM_OBSOLESCENCE_COUNT_INITIALISER,
50 .procname = "obsolescence_count_initialiser",
51 .data = &sysctl_netrom_obsolescence_count_initialiser,
52 .maxlen = sizeof(int),
53 .mode = 0644,
54 .proc_handler = &proc_dointvec_minmax,
55 .strategy = &sysctl_intvec,
56 .extra1 = &min_obs,
57 .extra2 = &max_obs
58 },
59 {
60 .ctl_name = NET_NETROM_NETWORK_TTL_INITIALISER,
61 .procname = "network_ttl_initialiser",
62 .data = &sysctl_netrom_network_ttl_initialiser,
63 .maxlen = sizeof(int),
64 .mode = 0644,
65 .proc_handler = &proc_dointvec_minmax,
66 .strategy = &sysctl_intvec,
67 .extra1 = &min_ttl,
68 .extra2 = &max_ttl
69 },
70 {
71 .ctl_name = NET_NETROM_TRANSPORT_TIMEOUT,
72 .procname = "transport_timeout",
73 .data = &sysctl_netrom_transport_timeout,
74 .maxlen = sizeof(int),
75 .mode = 0644,
76 .proc_handler = &proc_dointvec_minmax,
77 .strategy = &sysctl_intvec,
78 .extra1 = &min_t1,
79 .extra2 = &max_t1
80 },
81 {
82 .ctl_name = NET_NETROM_TRANSPORT_MAXIMUM_TRIES,
83 .procname = "transport_maximum_tries",
84 .data = &sysctl_netrom_transport_maximum_tries,
85 .maxlen = sizeof(int),
86 .mode = 0644,
87 .proc_handler = &proc_dointvec_minmax,
88 .strategy = &sysctl_intvec,
89 .extra1 = &min_n2,
90 .extra2 = &max_n2
91 },
92 {
93 .ctl_name = NET_NETROM_TRANSPORT_ACKNOWLEDGE_DELAY,
94 .procname = "transport_acknowledge_delay",
95 .data = &sysctl_netrom_transport_acknowledge_delay,
96 .maxlen = sizeof(int),
97 .mode = 0644,
98 .proc_handler = &proc_dointvec_minmax,
99 .strategy = &sysctl_intvec,
100 .extra1 = &min_t2,
101 .extra2 = &max_t2
102 },
103 {
104 .ctl_name = NET_NETROM_TRANSPORT_BUSY_DELAY,
105 .procname = "transport_busy_delay",
106 .data = &sysctl_netrom_transport_busy_delay,
107 .maxlen = sizeof(int),
108 .mode = 0644,
109 .proc_handler = &proc_dointvec_minmax,
110 .strategy = &sysctl_intvec,
111 .extra1 = &min_t4,
112 .extra2 = &max_t4
113 },
114 {
115 .ctl_name = NET_NETROM_TRANSPORT_REQUESTED_WINDOW_SIZE,
116 .procname = "transport_requested_window_size",
117 .data = &sysctl_netrom_transport_requested_window_size,
118 .maxlen = sizeof(int),
119 .mode = 0644,
120 .proc_handler = &proc_dointvec_minmax,
121 .strategy = &sysctl_intvec,
122 .extra1 = &min_window,
123 .extra2 = &max_window
124 },
125 {
126 .ctl_name = NET_NETROM_TRANSPORT_NO_ACTIVITY_TIMEOUT,
127 .procname = "transport_no_activity_timeout",
128 .data = &sysctl_netrom_transport_no_activity_timeout,
129 .maxlen = sizeof(int),
130 .mode = 0644,
131 .proc_handler = &proc_dointvec_minmax,
132 .strategy = &sysctl_intvec,
133 .extra1 = &min_idle,
134 .extra2 = &max_idle
135 },
136 {
137 .ctl_name = NET_NETROM_ROUTING_CONTROL,
138 .procname = "routing_control",
139 .data = &sysctl_netrom_routing_control,
140 .maxlen = sizeof(int),
141 .mode = 0644,
142 .proc_handler = &proc_dointvec_minmax,
143 .strategy = &sysctl_intvec,
144 .extra1 = &min_route,
145 .extra2 = &max_route
146 },
147 {
148 .ctl_name = NET_NETROM_LINK_FAILS_COUNT,
149 .procname = "link_fails_count",
150 .data = &sysctl_netrom_link_fails_count,
151 .maxlen = sizeof(int),
152 .mode = 0644,
153 .proc_handler = &proc_dointvec_minmax,
154 .strategy = &sysctl_intvec,
155 .extra1 = &min_fails,
156 .extra2 = &max_fails
157 },
158 { .ctl_name = 0 }
159};
160
161static ctl_table nr_dir_table[] = {
162 {
163 .ctl_name = NET_NETROM,
164 .procname = "netrom",
165 .mode = 0555,
166 .child = nr_table
167 },
168 { .ctl_name = 0 }
169};
170
171static ctl_table nr_root_table[] = {
172 {
173 .ctl_name = CTL_NET,
174 .procname = "net",
175 .mode = 0555,
176 .child = nr_dir_table
177 },
178 { .ctl_name = 0 }
179};
180
181void __init nr_register_sysctl(void)
182{
183 nr_table_header = register_sysctl_table(nr_root_table, 1);
184}
185
186void nr_unregister_sysctl(void)
187{
188 unregister_sysctl_table(nr_table_header);
189}