aboutsummaryrefslogtreecommitdiffstats
path: root/net/rose
diff options
context:
space:
mode:
Diffstat (limited to 'net/rose')
-rw-r--r--net/rose/Makefile9
-rw-r--r--net/rose/af_rose.c1589
-rw-r--r--net/rose/rose_dev.c154
-rw-r--r--net/rose/rose_in.c297
-rw-r--r--net/rose/rose_link.c288
-rw-r--r--net/rose/rose_loopback.c111
-rw-r--r--net/rose/rose_out.c126
-rw-r--r--net/rose/rose_route.c1343
-rw-r--r--net/rose/rose_subr.c519
-rw-r--r--net/rose/rose_timer.c216
-rw-r--r--net/rose/sysctl_net_rose.c169
11 files changed, 4821 insertions, 0 deletions
diff --git a/net/rose/Makefile b/net/rose/Makefile
new file mode 100644
index 000000000000..fa248116fd5b
--- /dev/null
+++ b/net/rose/Makefile
@@ -0,0 +1,9 @@
1#
2# Makefile for the Linux Rose (X.25 PLP) layer.
3#
4
5obj-$(CONFIG_ROSE) += rose.o
6
7rose-y := af_rose.o rose_dev.o rose_in.o rose_link.o rose_loopback.o \
8 rose_out.o rose_route.o rose_subr.o rose_timer.o
9rose-$(CONFIG_SYSCTL) += sysctl_net_rose.o
diff --git a/net/rose/af_rose.c b/net/rose/af_rose.c
new file mode 100644
index 000000000000..7eb6a5bf93ea
--- /dev/null
+++ b/net/rose/af_rose.c
@@ -0,0 +1,1589 @@
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) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
9 * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
10 * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi)
11 */
12#include <linux/config.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/init.h>
16#include <linux/errno.h>
17#include <linux/types.h>
18#include <linux/socket.h>
19#include <linux/in.h>
20#include <linux/kernel.h>
21#include <linux/sched.h>
22#include <linux/spinlock.h>
23#include <linux/timer.h>
24#include <linux/string.h>
25#include <linux/sockios.h>
26#include <linux/net.h>
27#include <linux/stat.h>
28#include <net/ax25.h>
29#include <linux/inet.h>
30#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/skbuff.h>
33#include <net/sock.h>
34#include <asm/system.h>
35#include <asm/uaccess.h>
36#include <linux/fcntl.h>
37#include <linux/termios.h>
38#include <linux/mm.h>
39#include <linux/interrupt.h>
40#include <linux/notifier.h>
41#include <net/rose.h>
42#include <linux/proc_fs.h>
43#include <linux/seq_file.h>
44#include <net/tcp.h>
45#include <net/ip.h>
46#include <net/arp.h>
47
48static int rose_ndevs = 10;
49
50int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0;
51int sysctl_rose_call_request_timeout = ROSE_DEFAULT_T1;
52int sysctl_rose_reset_request_timeout = ROSE_DEFAULT_T2;
53int sysctl_rose_clear_request_timeout = ROSE_DEFAULT_T3;
54int sysctl_rose_no_activity_timeout = ROSE_DEFAULT_IDLE;
55int sysctl_rose_ack_hold_back_timeout = ROSE_DEFAULT_HB;
56int sysctl_rose_routing_control = ROSE_DEFAULT_ROUTING;
57int sysctl_rose_link_fail_timeout = ROSE_DEFAULT_FAIL_TIMEOUT;
58int sysctl_rose_maximum_vcs = ROSE_DEFAULT_MAXVC;
59int sysctl_rose_window_size = ROSE_DEFAULT_WINDOW_SIZE;
60
61static HLIST_HEAD(rose_list);
62static DEFINE_SPINLOCK(rose_list_lock);
63
64static struct proto_ops rose_proto_ops;
65
66ax25_address rose_callsign;
67
68/*
69 * Convert a ROSE address into text.
70 */
71const char *rose2asc(const rose_address *addr)
72{
73 static char buffer[11];
74
75 if (addr->rose_addr[0] == 0x00 && addr->rose_addr[1] == 0x00 &&
76 addr->rose_addr[2] == 0x00 && addr->rose_addr[3] == 0x00 &&
77 addr->rose_addr[4] == 0x00) {
78 strcpy(buffer, "*");
79 } else {
80 sprintf(buffer, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF,
81 addr->rose_addr[1] & 0xFF,
82 addr->rose_addr[2] & 0xFF,
83 addr->rose_addr[3] & 0xFF,
84 addr->rose_addr[4] & 0xFF);
85 }
86
87 return buffer;
88}
89
90/*
91 * Compare two ROSE addresses, 0 == equal.
92 */
93int rosecmp(rose_address *addr1, rose_address *addr2)
94{
95 int i;
96
97 for (i = 0; i < 5; i++)
98 if (addr1->rose_addr[i] != addr2->rose_addr[i])
99 return 1;
100
101 return 0;
102}
103
104/*
105 * Compare two ROSE addresses for only mask digits, 0 == equal.
106 */
107int rosecmpm(rose_address *addr1, rose_address *addr2, unsigned short mask)
108{
109 int i, j;
110
111 if (mask > 10)
112 return 1;
113
114 for (i = 0; i < mask; i++) {
115 j = i / 2;
116
117 if ((i % 2) != 0) {
118 if ((addr1->rose_addr[j] & 0x0F) != (addr2->rose_addr[j] & 0x0F))
119 return 1;
120 } else {
121 if ((addr1->rose_addr[j] & 0xF0) != (addr2->rose_addr[j] & 0xF0))
122 return 1;
123 }
124 }
125
126 return 0;
127}
128
129/*
130 * Socket removal during an interrupt is now safe.
131 */
132static void rose_remove_socket(struct sock *sk)
133{
134 spin_lock_bh(&rose_list_lock);
135 sk_del_node_init(sk);
136 spin_unlock_bh(&rose_list_lock);
137}
138
139/*
140 * Kill all bound sockets on a broken link layer connection to a
141 * particular neighbour.
142 */
143void rose_kill_by_neigh(struct rose_neigh *neigh)
144{
145 struct sock *s;
146 struct hlist_node *node;
147
148 spin_lock_bh(&rose_list_lock);
149 sk_for_each(s, node, &rose_list) {
150 struct rose_sock *rose = rose_sk(s);
151
152 if (rose->neighbour == neigh) {
153 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
154 rose->neighbour->use--;
155 rose->neighbour = NULL;
156 }
157 }
158 spin_unlock_bh(&rose_list_lock);
159}
160
161/*
162 * Kill all bound sockets on a dropped device.
163 */
164static void rose_kill_by_device(struct net_device *dev)
165{
166 struct sock *s;
167 struct hlist_node *node;
168
169 spin_lock_bh(&rose_list_lock);
170 sk_for_each(s, node, &rose_list) {
171 struct rose_sock *rose = rose_sk(s);
172
173 if (rose->device == dev) {
174 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
175 rose->neighbour->use--;
176 rose->device = NULL;
177 }
178 }
179 spin_unlock_bh(&rose_list_lock);
180}
181
182/*
183 * Handle device status changes.
184 */
185static int rose_device_event(struct notifier_block *this, unsigned long event,
186 void *ptr)
187{
188 struct net_device *dev = (struct net_device *)ptr;
189
190 if (event != NETDEV_DOWN)
191 return NOTIFY_DONE;
192
193 switch (dev->type) {
194 case ARPHRD_ROSE:
195 rose_kill_by_device(dev);
196 break;
197 case ARPHRD_AX25:
198 rose_link_device_down(dev);
199 rose_rt_device_down(dev);
200 break;
201 }
202
203 return NOTIFY_DONE;
204}
205
206/*
207 * Add a socket to the bound sockets list.
208 */
209static void rose_insert_socket(struct sock *sk)
210{
211
212 spin_lock_bh(&rose_list_lock);
213 sk_add_node(sk, &rose_list);
214 spin_unlock_bh(&rose_list_lock);
215}
216
217/*
218 * Find a socket that wants to accept the Call Request we just
219 * received.
220 */
221static struct sock *rose_find_listener(rose_address *addr, ax25_address *call)
222{
223 struct sock *s;
224 struct hlist_node *node;
225
226 spin_lock_bh(&rose_list_lock);
227 sk_for_each(s, node, &rose_list) {
228 struct rose_sock *rose = rose_sk(s);
229
230 if (!rosecmp(&rose->source_addr, addr) &&
231 !ax25cmp(&rose->source_call, call) &&
232 !rose->source_ndigis && s->sk_state == TCP_LISTEN)
233 goto found;
234 }
235
236 sk_for_each(s, node, &rose_list) {
237 struct rose_sock *rose = rose_sk(s);
238
239 if (!rosecmp(&rose->source_addr, addr) &&
240 !ax25cmp(&rose->source_call, &null_ax25_address) &&
241 s->sk_state == TCP_LISTEN)
242 goto found;
243 }
244 s = NULL;
245found:
246 spin_unlock_bh(&rose_list_lock);
247 return s;
248}
249
250/*
251 * Find a connected ROSE socket given my LCI and device.
252 */
253struct sock *rose_find_socket(unsigned int lci, struct rose_neigh *neigh)
254{
255 struct sock *s;
256 struct hlist_node *node;
257
258 spin_lock_bh(&rose_list_lock);
259 sk_for_each(s, node, &rose_list) {
260 struct rose_sock *rose = rose_sk(s);
261
262 if (rose->lci == lci && rose->neighbour == neigh)
263 goto found;
264 }
265 s = NULL;
266found:
267 spin_unlock_bh(&rose_list_lock);
268 return s;
269}
270
271/*
272 * Find a unique LCI for a given device.
273 */
274unsigned int rose_new_lci(struct rose_neigh *neigh)
275{
276 int lci;
277
278 if (neigh->dce_mode) {
279 for (lci = 1; lci <= sysctl_rose_maximum_vcs; lci++)
280 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
281 return lci;
282 } else {
283 for (lci = sysctl_rose_maximum_vcs; lci > 0; lci--)
284 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
285 return lci;
286 }
287
288 return 0;
289}
290
291/*
292 * Deferred destroy.
293 */
294void rose_destroy_socket(struct sock *);
295
296/*
297 * Handler for deferred kills.
298 */
299static void rose_destroy_timer(unsigned long data)
300{
301 rose_destroy_socket((struct sock *)data);
302}
303
304/*
305 * This is called from user mode and the timers. Thus it protects itself
306 * against interrupt users but doesn't worry about being called during
307 * work. Once it is removed from the queue no interrupt or bottom half
308 * will touch it and we are (fairly 8-) ) safe.
309 */
310void rose_destroy_socket(struct sock *sk)
311{
312 struct sk_buff *skb;
313
314 rose_remove_socket(sk);
315 rose_stop_heartbeat(sk);
316 rose_stop_idletimer(sk);
317 rose_stop_timer(sk);
318
319 rose_clear_queues(sk); /* Flush the queues */
320
321 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
322 if (skb->sk != sk) { /* A pending connection */
323 /* Queue the unaccepted socket for death */
324 sock_set_flag(skb->sk, SOCK_DEAD);
325 rose_start_heartbeat(skb->sk);
326 rose_sk(skb->sk)->state = ROSE_STATE_0;
327 }
328
329 kfree_skb(skb);
330 }
331
332 if (atomic_read(&sk->sk_wmem_alloc) ||
333 atomic_read(&sk->sk_rmem_alloc)) {
334 /* Defer: outstanding buffers */
335 init_timer(&sk->sk_timer);
336 sk->sk_timer.expires = jiffies + 10 * HZ;
337 sk->sk_timer.function = rose_destroy_timer;
338 sk->sk_timer.data = (unsigned long)sk;
339 add_timer(&sk->sk_timer);
340 } else
341 sock_put(sk);
342}
343
344/*
345 * Handling for system calls applied via the various interfaces to a
346 * ROSE socket object.
347 */
348
349static int rose_setsockopt(struct socket *sock, int level, int optname,
350 char __user *optval, int optlen)
351{
352 struct sock *sk = sock->sk;
353 struct rose_sock *rose = rose_sk(sk);
354 int opt;
355
356 if (level != SOL_ROSE)
357 return -ENOPROTOOPT;
358
359 if (optlen < sizeof(int))
360 return -EINVAL;
361
362 if (get_user(opt, (int __user *)optval))
363 return -EFAULT;
364
365 switch (optname) {
366 case ROSE_DEFER:
367 rose->defer = opt ? 1 : 0;
368 return 0;
369
370 case ROSE_T1:
371 if (opt < 1)
372 return -EINVAL;
373 rose->t1 = opt * HZ;
374 return 0;
375
376 case ROSE_T2:
377 if (opt < 1)
378 return -EINVAL;
379 rose->t2 = opt * HZ;
380 return 0;
381
382 case ROSE_T3:
383 if (opt < 1)
384 return -EINVAL;
385 rose->t3 = opt * HZ;
386 return 0;
387
388 case ROSE_HOLDBACK:
389 if (opt < 1)
390 return -EINVAL;
391 rose->hb = opt * HZ;
392 return 0;
393
394 case ROSE_IDLE:
395 if (opt < 0)
396 return -EINVAL;
397 rose->idle = opt * 60 * HZ;
398 return 0;
399
400 case ROSE_QBITINCL:
401 rose->qbitincl = opt ? 1 : 0;
402 return 0;
403
404 default:
405 return -ENOPROTOOPT;
406 }
407}
408
409static int rose_getsockopt(struct socket *sock, int level, int optname,
410 char __user *optval, int __user *optlen)
411{
412 struct sock *sk = sock->sk;
413 struct rose_sock *rose = rose_sk(sk);
414 int val = 0;
415 int len;
416
417 if (level != SOL_ROSE)
418 return -ENOPROTOOPT;
419
420 if (get_user(len, optlen))
421 return -EFAULT;
422
423 if (len < 0)
424 return -EINVAL;
425
426 switch (optname) {
427 case ROSE_DEFER:
428 val = rose->defer;
429 break;
430
431 case ROSE_T1:
432 val = rose->t1 / HZ;
433 break;
434
435 case ROSE_T2:
436 val = rose->t2 / HZ;
437 break;
438
439 case ROSE_T3:
440 val = rose->t3 / HZ;
441 break;
442
443 case ROSE_HOLDBACK:
444 val = rose->hb / HZ;
445 break;
446
447 case ROSE_IDLE:
448 val = rose->idle / (60 * HZ);
449 break;
450
451 case ROSE_QBITINCL:
452 val = rose->qbitincl;
453 break;
454
455 default:
456 return -ENOPROTOOPT;
457 }
458
459 len = min_t(unsigned int, len, sizeof(int));
460
461 if (put_user(len, optlen))
462 return -EFAULT;
463
464 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
465}
466
467static int rose_listen(struct socket *sock, int backlog)
468{
469 struct sock *sk = sock->sk;
470
471 if (sk->sk_state != TCP_LISTEN) {
472 struct rose_sock *rose = rose_sk(sk);
473
474 rose->dest_ndigis = 0;
475 memset(&rose->dest_addr, 0, ROSE_ADDR_LEN);
476 memset(&rose->dest_call, 0, AX25_ADDR_LEN);
477 memset(rose->dest_digis, 0, AX25_ADDR_LEN * ROSE_MAX_DIGIS);
478 sk->sk_max_ack_backlog = backlog;
479 sk->sk_state = TCP_LISTEN;
480 return 0;
481 }
482
483 return -EOPNOTSUPP;
484}
485
486static struct proto rose_proto = {
487 .name = "ROSE",
488 .owner = THIS_MODULE,
489 .obj_size = sizeof(struct rose_sock),
490};
491
492static int rose_create(struct socket *sock, int protocol)
493{
494 struct sock *sk;
495 struct rose_sock *rose;
496
497 if (sock->type != SOCK_SEQPACKET || protocol != 0)
498 return -ESOCKTNOSUPPORT;
499
500 if ((sk = sk_alloc(PF_ROSE, GFP_ATOMIC, &rose_proto, 1)) == NULL)
501 return -ENOMEM;
502
503 rose = rose_sk(sk);
504
505 sock_init_data(sock, sk);
506
507 skb_queue_head_init(&rose->ack_queue);
508#ifdef M_BIT
509 skb_queue_head_init(&rose->frag_queue);
510 rose->fraglen = 0;
511#endif
512
513 sock->ops = &rose_proto_ops;
514 sk->sk_protocol = protocol;
515
516 init_timer(&rose->timer);
517 init_timer(&rose->idletimer);
518
519 rose->t1 = sysctl_rose_call_request_timeout;
520 rose->t2 = sysctl_rose_reset_request_timeout;
521 rose->t3 = sysctl_rose_clear_request_timeout;
522 rose->hb = sysctl_rose_ack_hold_back_timeout;
523 rose->idle = sysctl_rose_no_activity_timeout;
524
525 rose->state = ROSE_STATE_0;
526
527 return 0;
528}
529
530static struct sock *rose_make_new(struct sock *osk)
531{
532 struct sock *sk;
533 struct rose_sock *rose, *orose;
534
535 if (osk->sk_type != SOCK_SEQPACKET)
536 return NULL;
537
538 if ((sk = sk_alloc(PF_ROSE, GFP_ATOMIC, &rose_proto, 1)) == NULL)
539 return NULL;
540
541 rose = rose_sk(sk);
542
543 sock_init_data(NULL, sk);
544
545 skb_queue_head_init(&rose->ack_queue);
546#ifdef M_BIT
547 skb_queue_head_init(&rose->frag_queue);
548 rose->fraglen = 0;
549#endif
550
551 sk->sk_type = osk->sk_type;
552 sk->sk_socket = osk->sk_socket;
553 sk->sk_priority = osk->sk_priority;
554 sk->sk_protocol = osk->sk_protocol;
555 sk->sk_rcvbuf = osk->sk_rcvbuf;
556 sk->sk_sndbuf = osk->sk_sndbuf;
557 sk->sk_state = TCP_ESTABLISHED;
558 sk->sk_sleep = osk->sk_sleep;
559
560 if (sock_flag(osk, SOCK_ZAPPED))
561 sock_set_flag(sk, SOCK_ZAPPED);
562
563 if (sock_flag(osk, SOCK_DBG))
564 sock_set_flag(sk, SOCK_DBG);
565
566 init_timer(&rose->timer);
567 init_timer(&rose->idletimer);
568
569 orose = rose_sk(osk);
570 rose->t1 = orose->t1;
571 rose->t2 = orose->t2;
572 rose->t3 = orose->t3;
573 rose->hb = orose->hb;
574 rose->idle = orose->idle;
575 rose->defer = orose->defer;
576 rose->device = orose->device;
577 rose->qbitincl = orose->qbitincl;
578
579 return sk;
580}
581
582static int rose_release(struct socket *sock)
583{
584 struct sock *sk = sock->sk;
585 struct rose_sock *rose;
586
587 if (sk == NULL) return 0;
588
589 rose = rose_sk(sk);
590
591 switch (rose->state) {
592 case ROSE_STATE_0:
593 rose_disconnect(sk, 0, -1, -1);
594 rose_destroy_socket(sk);
595 break;
596
597 case ROSE_STATE_2:
598 rose->neighbour->use--;
599 rose_disconnect(sk, 0, -1, -1);
600 rose_destroy_socket(sk);
601 break;
602
603 case ROSE_STATE_1:
604 case ROSE_STATE_3:
605 case ROSE_STATE_4:
606 case ROSE_STATE_5:
607 rose_clear_queues(sk);
608 rose_stop_idletimer(sk);
609 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
610 rose_start_t3timer(sk);
611 rose->state = ROSE_STATE_2;
612 sk->sk_state = TCP_CLOSE;
613 sk->sk_shutdown |= SEND_SHUTDOWN;
614 sk->sk_state_change(sk);
615 sock_set_flag(sk, SOCK_DEAD);
616 sock_set_flag(sk, SOCK_DESTROY);
617 break;
618
619 default:
620 break;
621 }
622
623 sock->sk = NULL;
624
625 return 0;
626}
627
628static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
629{
630 struct sock *sk = sock->sk;
631 struct rose_sock *rose = rose_sk(sk);
632 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
633 struct net_device *dev;
634 ax25_address *user, *source;
635 int n;
636
637 if (!sock_flag(sk, SOCK_ZAPPED))
638 return -EINVAL;
639
640 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
641 return -EINVAL;
642
643 if (addr->srose_family != AF_ROSE)
644 return -EINVAL;
645
646 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
647 return -EINVAL;
648
649 if (addr->srose_ndigis > ROSE_MAX_DIGIS)
650 return -EINVAL;
651
652 if ((dev = rose_dev_get(&addr->srose_addr)) == NULL) {
653 SOCK_DEBUG(sk, "ROSE: bind failed: invalid address\n");
654 return -EADDRNOTAVAIL;
655 }
656
657 source = &addr->srose_call;
658
659 if ((user = ax25_findbyuid(current->euid)) == NULL) {
660 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE))
661 return -EACCES;
662 user = source;
663 }
664
665 rose->source_addr = addr->srose_addr;
666 rose->source_call = *user;
667 rose->device = dev;
668 rose->source_ndigis = addr->srose_ndigis;
669
670 if (addr_len == sizeof(struct full_sockaddr_rose)) {
671 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
672 for (n = 0 ; n < addr->srose_ndigis ; n++)
673 rose->source_digis[n] = full_addr->srose_digis[n];
674 } else {
675 if (rose->source_ndigis == 1) {
676 rose->source_digis[0] = addr->srose_digi;
677 }
678 }
679
680 rose_insert_socket(sk);
681
682 sock_reset_flag(sk, SOCK_ZAPPED);
683 SOCK_DEBUG(sk, "ROSE: socket is bound\n");
684 return 0;
685}
686
687static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
688{
689 struct sock *sk = sock->sk;
690 struct rose_sock *rose = rose_sk(sk);
691 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
692 unsigned char cause, diagnostic;
693 ax25_address *user;
694 struct net_device *dev;
695 int n;
696
697 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
698 sock->state = SS_CONNECTED;
699 return 0; /* Connect completed during a ERESTARTSYS event */
700 }
701
702 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
703 sock->state = SS_UNCONNECTED;
704 return -ECONNREFUSED;
705 }
706
707 if (sk->sk_state == TCP_ESTABLISHED)
708 return -EISCONN; /* No reconnect on a seqpacket socket */
709
710 sk->sk_state = TCP_CLOSE;
711 sock->state = SS_UNCONNECTED;
712
713 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
714 return -EINVAL;
715
716 if (addr->srose_family != AF_ROSE)
717 return -EINVAL;
718
719 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
720 return -EINVAL;
721
722 if (addr->srose_ndigis > ROSE_MAX_DIGIS)
723 return -EINVAL;
724
725 /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
726 if ((rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
727 return -EINVAL;
728
729 rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause,
730 &diagnostic);
731 if (!rose->neighbour)
732 return -ENETUNREACH;
733
734 rose->lci = rose_new_lci(rose->neighbour);
735 if (!rose->lci)
736 return -ENETUNREACH;
737
738 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
739 sock_reset_flag(sk, SOCK_ZAPPED);
740
741 if ((dev = rose_dev_first()) == NULL)
742 return -ENETUNREACH;
743
744 if ((user = ax25_findbyuid(current->euid)) == NULL)
745 return -EINVAL;
746
747 memcpy(&rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
748 rose->source_call = *user;
749 rose->device = dev;
750
751 rose_insert_socket(sk); /* Finish the bind */
752 }
753
754 rose->dest_addr = addr->srose_addr;
755 rose->dest_call = addr->srose_call;
756 rose->rand = ((long)rose & 0xFFFF) + rose->lci;
757 rose->dest_ndigis = addr->srose_ndigis;
758
759 if (addr_len == sizeof(struct full_sockaddr_rose)) {
760 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
761 for (n = 0 ; n < addr->srose_ndigis ; n++)
762 rose->dest_digis[n] = full_addr->srose_digis[n];
763 } else {
764 if (rose->dest_ndigis == 1) {
765 rose->dest_digis[0] = addr->srose_digi;
766 }
767 }
768
769 /* Move to connecting socket, start sending Connect Requests */
770 sock->state = SS_CONNECTING;
771 sk->sk_state = TCP_SYN_SENT;
772
773 rose->state = ROSE_STATE_1;
774
775 rose->neighbour->use++;
776
777 rose_write_internal(sk, ROSE_CALL_REQUEST);
778 rose_start_heartbeat(sk);
779 rose_start_t1timer(sk);
780
781 /* Now the loop */
782 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
783 return -EINPROGRESS;
784
785 /*
786 * A Connect Ack with Choke or timeout or failed routing will go to
787 * closed.
788 */
789 if (sk->sk_state == TCP_SYN_SENT) {
790 struct task_struct *tsk = current;
791 DECLARE_WAITQUEUE(wait, tsk);
792
793 add_wait_queue(sk->sk_sleep, &wait);
794 for (;;) {
795 set_current_state(TASK_INTERRUPTIBLE);
796 if (sk->sk_state != TCP_SYN_SENT)
797 break;
798 if (!signal_pending(tsk)) {
799 schedule();
800 continue;
801 }
802 current->state = TASK_RUNNING;
803 remove_wait_queue(sk->sk_sleep, &wait);
804 return -ERESTARTSYS;
805 }
806 current->state = TASK_RUNNING;
807 remove_wait_queue(sk->sk_sleep, &wait);
808 }
809
810 if (sk->sk_state != TCP_ESTABLISHED) {
811 sock->state = SS_UNCONNECTED;
812 return sock_error(sk); /* Always set at this point */
813 }
814
815 sock->state = SS_CONNECTED;
816
817 return 0;
818}
819
820static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
821{
822 struct task_struct *tsk = current;
823 DECLARE_WAITQUEUE(wait, tsk);
824 struct sk_buff *skb;
825 struct sock *newsk;
826 struct sock *sk;
827 int err = 0;
828
829 if ((sk = sock->sk) == NULL)
830 return -EINVAL;
831
832 lock_sock(sk);
833 if (sk->sk_type != SOCK_SEQPACKET) {
834 err = -EOPNOTSUPP;
835 goto out;
836 }
837
838 if (sk->sk_state != TCP_LISTEN) {
839 err = -EINVAL;
840 goto out;
841 }
842
843 /*
844 * The write queue this time is holding sockets ready to use
845 * hooked into the SABM we saved
846 */
847 add_wait_queue(sk->sk_sleep, &wait);
848 for (;;) {
849 skb = skb_dequeue(&sk->sk_receive_queue);
850 if (skb)
851 break;
852
853 current->state = TASK_INTERRUPTIBLE;
854 release_sock(sk);
855 if (flags & O_NONBLOCK) {
856 current->state = TASK_RUNNING;
857 remove_wait_queue(sk->sk_sleep, &wait);
858 return -EWOULDBLOCK;
859 }
860 if (!signal_pending(tsk)) {
861 schedule();
862 lock_sock(sk);
863 continue;
864 }
865 return -ERESTARTSYS;
866 }
867 current->state = TASK_RUNNING;
868 remove_wait_queue(sk->sk_sleep, &wait);
869
870 newsk = skb->sk;
871 newsk->sk_socket = newsock;
872 newsk->sk_sleep = &newsock->wait;
873
874 /* Now attach up the new socket */
875 skb->sk = NULL;
876 kfree_skb(skb);
877 sk->sk_ack_backlog--;
878 newsock->sk = newsk;
879
880out:
881 release_sock(sk);
882
883 return err;
884}
885
886static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
887 int *uaddr_len, int peer)
888{
889 struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
890 struct sock *sk = sock->sk;
891 struct rose_sock *rose = rose_sk(sk);
892 int n;
893
894 if (peer != 0) {
895 if (sk->sk_state != TCP_ESTABLISHED)
896 return -ENOTCONN;
897 srose->srose_family = AF_ROSE;
898 srose->srose_addr = rose->dest_addr;
899 srose->srose_call = rose->dest_call;
900 srose->srose_ndigis = rose->dest_ndigis;
901 for (n = 0; n < rose->dest_ndigis; n++)
902 srose->srose_digis[n] = rose->dest_digis[n];
903 } else {
904 srose->srose_family = AF_ROSE;
905 srose->srose_addr = rose->source_addr;
906 srose->srose_call = rose->source_call;
907 srose->srose_ndigis = rose->source_ndigis;
908 for (n = 0; n < rose->source_ndigis; n++)
909 srose->srose_digis[n] = rose->source_digis[n];
910 }
911
912 *uaddr_len = sizeof(struct full_sockaddr_rose);
913 return 0;
914}
915
916int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct rose_neigh *neigh, unsigned int lci)
917{
918 struct sock *sk;
919 struct sock *make;
920 struct rose_sock *make_rose;
921 struct rose_facilities_struct facilities;
922 int n, len;
923
924 skb->sk = NULL; /* Initially we don't know who it's for */
925
926 /*
927 * skb->data points to the rose frame start
928 */
929 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
930
931 len = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
932 len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
933 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
934 rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
935 return 0;
936 }
937
938 sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
939
940 /*
941 * We can't accept the Call Request.
942 */
943 if (sk == NULL || sk_acceptq_is_full(sk) ||
944 (make = rose_make_new(sk)) == NULL) {
945 rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
946 return 0;
947 }
948
949 skb->sk = make;
950 make->sk_state = TCP_ESTABLISHED;
951 make_rose = rose_sk(make);
952
953 make_rose->lci = lci;
954 make_rose->dest_addr = facilities.dest_addr;
955 make_rose->dest_call = facilities.dest_call;
956 make_rose->dest_ndigis = facilities.dest_ndigis;
957 for (n = 0 ; n < facilities.dest_ndigis ; n++)
958 make_rose->dest_digis[n] = facilities.dest_digis[n];
959 make_rose->source_addr = facilities.source_addr;
960 make_rose->source_call = facilities.source_call;
961 make_rose->source_ndigis = facilities.source_ndigis;
962 for (n = 0 ; n < facilities.source_ndigis ; n++)
963 make_rose->source_digis[n]= facilities.source_digis[n];
964 make_rose->neighbour = neigh;
965 make_rose->device = dev;
966 make_rose->facilities = facilities;
967
968 make_rose->neighbour->use++;
969
970 if (rose_sk(sk)->defer) {
971 make_rose->state = ROSE_STATE_5;
972 } else {
973 rose_write_internal(make, ROSE_CALL_ACCEPTED);
974 make_rose->state = ROSE_STATE_3;
975 rose_start_idletimer(make);
976 }
977
978 make_rose->condition = 0x00;
979 make_rose->vs = 0;
980 make_rose->va = 0;
981 make_rose->vr = 0;
982 make_rose->vl = 0;
983 sk->sk_ack_backlog++;
984
985 rose_insert_socket(make);
986
987 skb_queue_head(&sk->sk_receive_queue, skb);
988
989 rose_start_heartbeat(make);
990
991 if (!sock_flag(sk, SOCK_DEAD))
992 sk->sk_data_ready(sk, skb->len);
993
994 return 1;
995}
996
997static int rose_sendmsg(struct kiocb *iocb, struct socket *sock,
998 struct msghdr *msg, size_t len)
999{
1000 struct sock *sk = sock->sk;
1001 struct rose_sock *rose = rose_sk(sk);
1002 struct sockaddr_rose *usrose = (struct sockaddr_rose *)msg->msg_name;
1003 int err;
1004 struct full_sockaddr_rose srose;
1005 struct sk_buff *skb;
1006 unsigned char *asmptr;
1007 int n, size, qbit = 0;
1008
1009 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1010 return -EINVAL;
1011
1012 if (sock_flag(sk, SOCK_ZAPPED))
1013 return -EADDRNOTAVAIL;
1014
1015 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1016 send_sig(SIGPIPE, current, 0);
1017 return -EPIPE;
1018 }
1019
1020 if (rose->neighbour == NULL || rose->device == NULL)
1021 return -ENETUNREACH;
1022
1023 if (usrose != NULL) {
1024 if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
1025 return -EINVAL;
1026 memset(&srose, 0, sizeof(struct full_sockaddr_rose));
1027 memcpy(&srose, usrose, msg->msg_namelen);
1028 if (rosecmp(&rose->dest_addr, &srose.srose_addr) != 0 ||
1029 ax25cmp(&rose->dest_call, &srose.srose_call) != 0)
1030 return -EISCONN;
1031 if (srose.srose_ndigis != rose->dest_ndigis)
1032 return -EISCONN;
1033 if (srose.srose_ndigis == rose->dest_ndigis) {
1034 for (n = 0 ; n < srose.srose_ndigis ; n++)
1035 if (ax25cmp(&rose->dest_digis[n],
1036 &srose.srose_digis[n]))
1037 return -EISCONN;
1038 }
1039 if (srose.srose_family != AF_ROSE)
1040 return -EINVAL;
1041 } else {
1042 if (sk->sk_state != TCP_ESTABLISHED)
1043 return -ENOTCONN;
1044
1045 srose.srose_family = AF_ROSE;
1046 srose.srose_addr = rose->dest_addr;
1047 srose.srose_call = rose->dest_call;
1048 srose.srose_ndigis = rose->dest_ndigis;
1049 for (n = 0 ; n < rose->dest_ndigis ; n++)
1050 srose.srose_digis[n] = rose->dest_digis[n];
1051 }
1052
1053 SOCK_DEBUG(sk, "ROSE: sendto: Addresses built.\n");
1054
1055 /* Build a packet */
1056 SOCK_DEBUG(sk, "ROSE: sendto: building packet.\n");
1057 size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
1058
1059 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1060 return err;
1061
1062 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
1063
1064 /*
1065 * Put the data on the end
1066 */
1067 SOCK_DEBUG(sk, "ROSE: Appending user data\n");
1068
1069 asmptr = skb->h.raw = skb_put(skb, len);
1070
1071 err = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1072 if (err) {
1073 kfree_skb(skb);
1074 return err;
1075 }
1076
1077 /*
1078 * If the Q BIT Include socket option is in force, the first
1079 * byte of the user data is the logical value of the Q Bit.
1080 */
1081 if (rose->qbitincl) {
1082 qbit = skb->data[0];
1083 skb_pull(skb, 1);
1084 }
1085
1086 /*
1087 * Push down the ROSE header
1088 */
1089 asmptr = skb_push(skb, ROSE_MIN_LEN);
1090
1091 SOCK_DEBUG(sk, "ROSE: Building Network Header.\n");
1092
1093 /* Build a ROSE Network header */
1094 asmptr[0] = ((rose->lci >> 8) & 0x0F) | ROSE_GFI;
1095 asmptr[1] = (rose->lci >> 0) & 0xFF;
1096 asmptr[2] = ROSE_DATA;
1097
1098 if (qbit)
1099 asmptr[0] |= ROSE_Q_BIT;
1100
1101 SOCK_DEBUG(sk, "ROSE: Built header.\n");
1102
1103 SOCK_DEBUG(sk, "ROSE: Transmitting buffer\n");
1104
1105 if (sk->sk_state != TCP_ESTABLISHED) {
1106 kfree_skb(skb);
1107 return -ENOTCONN;
1108 }
1109
1110#ifdef M_BIT
1111#define ROSE_PACLEN (256-ROSE_MIN_LEN)
1112 if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
1113 unsigned char header[ROSE_MIN_LEN];
1114 struct sk_buff *skbn;
1115 int frontlen;
1116 int lg;
1117
1118 /* Save a copy of the Header */
1119 memcpy(header, skb->data, ROSE_MIN_LEN);
1120 skb_pull(skb, ROSE_MIN_LEN);
1121
1122 frontlen = skb_headroom(skb);
1123
1124 while (skb->len > 0) {
1125 if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, &err)) == NULL) {
1126 kfree_skb(skb);
1127 return err;
1128 }
1129
1130 skbn->sk = sk;
1131 skbn->free = 1;
1132 skbn->arp = 1;
1133
1134 skb_reserve(skbn, frontlen);
1135
1136 lg = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN;
1137
1138 /* Copy the user data */
1139 memcpy(skb_put(skbn, lg), skb->data, lg);
1140 skb_pull(skb, lg);
1141
1142 /* Duplicate the Header */
1143 skb_push(skbn, ROSE_MIN_LEN);
1144 memcpy(skbn->data, header, ROSE_MIN_LEN);
1145
1146 if (skb->len > 0)
1147 skbn->data[2] |= M_BIT;
1148
1149 skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
1150 }
1151
1152 skb->free = 1;
1153 kfree_skb(skb);
1154 } else {
1155 skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
1156 }
1157#else
1158 skb_queue_tail(&sk->sk_write_queue, skb); /* Shove it onto the queue */
1159#endif
1160
1161 rose_kick(sk);
1162
1163 return len;
1164}
1165
1166
1167static int rose_recvmsg(struct kiocb *iocb, struct socket *sock,
1168 struct msghdr *msg, size_t size, int flags)
1169{
1170 struct sock *sk = sock->sk;
1171 struct rose_sock *rose = rose_sk(sk);
1172 struct sockaddr_rose *srose = (struct sockaddr_rose *)msg->msg_name;
1173 size_t copied;
1174 unsigned char *asmptr;
1175 struct sk_buff *skb;
1176 int n, er, qbit;
1177
1178 /*
1179 * This works for seqpacket too. The receiver has ordered the queue for
1180 * us! We do one quick check first though
1181 */
1182 if (sk->sk_state != TCP_ESTABLISHED)
1183 return -ENOTCONN;
1184
1185 /* Now we can treat all alike */
1186 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1187 return er;
1188
1189 qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
1190
1191 skb_pull(skb, ROSE_MIN_LEN);
1192
1193 if (rose->qbitincl) {
1194 asmptr = skb_push(skb, 1);
1195 *asmptr = qbit;
1196 }
1197
1198 skb->h.raw = skb->data;
1199 copied = skb->len;
1200
1201 if (copied > size) {
1202 copied = size;
1203 msg->msg_flags |= MSG_TRUNC;
1204 }
1205
1206 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1207
1208 if (srose != NULL) {
1209 srose->srose_family = AF_ROSE;
1210 srose->srose_addr = rose->dest_addr;
1211 srose->srose_call = rose->dest_call;
1212 srose->srose_ndigis = rose->dest_ndigis;
1213 if (msg->msg_namelen >= sizeof(struct full_sockaddr_rose)) {
1214 struct full_sockaddr_rose *full_srose = (struct full_sockaddr_rose *)msg->msg_name;
1215 for (n = 0 ; n < rose->dest_ndigis ; n++)
1216 full_srose->srose_digis[n] = rose->dest_digis[n];
1217 msg->msg_namelen = sizeof(struct full_sockaddr_rose);
1218 } else {
1219 if (rose->dest_ndigis >= 1) {
1220 srose->srose_ndigis = 1;
1221 srose->srose_digi = rose->dest_digis[0];
1222 }
1223 msg->msg_namelen = sizeof(struct sockaddr_rose);
1224 }
1225 }
1226
1227 skb_free_datagram(sk, skb);
1228
1229 return copied;
1230}
1231
1232
1233static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1234{
1235 struct sock *sk = sock->sk;
1236 struct rose_sock *rose = rose_sk(sk);
1237 void __user *argp = (void __user *)arg;
1238
1239 switch (cmd) {
1240 case TIOCOUTQ: {
1241 long amount;
1242 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1243 if (amount < 0)
1244 amount = 0;
1245 return put_user(amount, (unsigned int __user *)argp);
1246 }
1247
1248 case TIOCINQ: {
1249 struct sk_buff *skb;
1250 long amount = 0L;
1251 /* These two are safe on a single CPU system as only user tasks fiddle here */
1252 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1253 amount = skb->len;
1254 return put_user(amount, (unsigned int __user *)argp);
1255 }
1256
1257 case SIOCGSTAMP:
1258 if (sk != NULL)
1259 return sock_get_timestamp(sk, (struct timeval __user *)argp);
1260 return -EINVAL;
1261
1262 case SIOCGIFADDR:
1263 case SIOCSIFADDR:
1264 case SIOCGIFDSTADDR:
1265 case SIOCSIFDSTADDR:
1266 case SIOCGIFBRDADDR:
1267 case SIOCSIFBRDADDR:
1268 case SIOCGIFNETMASK:
1269 case SIOCSIFNETMASK:
1270 case SIOCGIFMETRIC:
1271 case SIOCSIFMETRIC:
1272 return -EINVAL;
1273
1274 case SIOCADDRT:
1275 case SIOCDELRT:
1276 case SIOCRSCLRRT:
1277 if (!capable(CAP_NET_ADMIN))
1278 return -EPERM;
1279 return rose_rt_ioctl(cmd, argp);
1280
1281 case SIOCRSGCAUSE: {
1282 struct rose_cause_struct rose_cause;
1283 rose_cause.cause = rose->cause;
1284 rose_cause.diagnostic = rose->diagnostic;
1285 return copy_to_user(argp, &rose_cause, sizeof(struct rose_cause_struct)) ? -EFAULT : 0;
1286 }
1287
1288 case SIOCRSSCAUSE: {
1289 struct rose_cause_struct rose_cause;
1290 if (copy_from_user(&rose_cause, argp, sizeof(struct rose_cause_struct)))
1291 return -EFAULT;
1292 rose->cause = rose_cause.cause;
1293 rose->diagnostic = rose_cause.diagnostic;
1294 return 0;
1295 }
1296
1297 case SIOCRSSL2CALL:
1298 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1299 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1300 ax25_listen_release(&rose_callsign, NULL);
1301 if (copy_from_user(&rose_callsign, argp, sizeof(ax25_address)))
1302 return -EFAULT;
1303 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1304 ax25_listen_register(&rose_callsign, NULL);
1305 return 0;
1306
1307 case SIOCRSGL2CALL:
1308 return copy_to_user(argp, &rose_callsign, sizeof(ax25_address)) ? -EFAULT : 0;
1309
1310 case SIOCRSACCEPT:
1311 if (rose->state == ROSE_STATE_5) {
1312 rose_write_internal(sk, ROSE_CALL_ACCEPTED);
1313 rose_start_idletimer(sk);
1314 rose->condition = 0x00;
1315 rose->vs = 0;
1316 rose->va = 0;
1317 rose->vr = 0;
1318 rose->vl = 0;
1319 rose->state = ROSE_STATE_3;
1320 }
1321 return 0;
1322
1323 default:
1324 return dev_ioctl(cmd, argp);
1325 }
1326
1327 return 0;
1328}
1329
1330#ifdef CONFIG_PROC_FS
1331static void *rose_info_start(struct seq_file *seq, loff_t *pos)
1332{
1333 int i;
1334 struct sock *s;
1335 struct hlist_node *node;
1336
1337 spin_lock_bh(&rose_list_lock);
1338 if (*pos == 0)
1339 return SEQ_START_TOKEN;
1340
1341 i = 1;
1342 sk_for_each(s, node, &rose_list) {
1343 if (i == *pos)
1344 return s;
1345 ++i;
1346 }
1347 return NULL;
1348}
1349
1350static void *rose_info_next(struct seq_file *seq, void *v, loff_t *pos)
1351{
1352 ++*pos;
1353
1354 return (v == SEQ_START_TOKEN) ? sk_head(&rose_list)
1355 : sk_next((struct sock *)v);
1356}
1357
1358static void rose_info_stop(struct seq_file *seq, void *v)
1359{
1360 spin_unlock_bh(&rose_list_lock);
1361}
1362
1363static int rose_info_show(struct seq_file *seq, void *v)
1364{
1365 if (v == SEQ_START_TOKEN)
1366 seq_puts(seq,
1367 "dest_addr dest_call src_addr src_call dev lci neigh st vs vr va t t1 t2 t3 hb idle Snd-Q Rcv-Q inode\n");
1368
1369 else {
1370 struct sock *s = v;
1371 struct rose_sock *rose = rose_sk(s);
1372 const char *devname, *callsign;
1373 const struct net_device *dev = rose->device;
1374
1375 if (!dev)
1376 devname = "???";
1377 else
1378 devname = dev->name;
1379
1380 seq_printf(seq, "%-10s %-9s ",
1381 rose2asc(&rose->dest_addr),
1382 ax2asc(&rose->dest_call));
1383
1384 if (ax25cmp(&rose->source_call, &null_ax25_address) == 0)
1385 callsign = "??????-?";
1386 else
1387 callsign = ax2asc(&rose->source_call);
1388
1389 seq_printf(seq,
1390 "%-10s %-9s %-5s %3.3X %05d %d %d %d %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
1391 rose2asc(&rose->source_addr),
1392 callsign,
1393 devname,
1394 rose->lci & 0x0FFF,
1395 (rose->neighbour) ? rose->neighbour->number : 0,
1396 rose->state,
1397 rose->vs,
1398 rose->vr,
1399 rose->va,
1400 ax25_display_timer(&rose->timer) / HZ,
1401 rose->t1 / HZ,
1402 rose->t2 / HZ,
1403 rose->t3 / HZ,
1404 rose->hb / HZ,
1405 ax25_display_timer(&rose->idletimer) / (60 * HZ),
1406 rose->idle / (60 * HZ),
1407 atomic_read(&s->sk_wmem_alloc),
1408 atomic_read(&s->sk_rmem_alloc),
1409 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1410 }
1411
1412 return 0;
1413}
1414
1415static struct seq_operations rose_info_seqops = {
1416 .start = rose_info_start,
1417 .next = rose_info_next,
1418 .stop = rose_info_stop,
1419 .show = rose_info_show,
1420};
1421
1422static int rose_info_open(struct inode *inode, struct file *file)
1423{
1424 return seq_open(file, &rose_info_seqops);
1425}
1426
1427static struct file_operations rose_info_fops = {
1428 .owner = THIS_MODULE,
1429 .open = rose_info_open,
1430 .read = seq_read,
1431 .llseek = seq_lseek,
1432 .release = seq_release,
1433};
1434#endif /* CONFIG_PROC_FS */
1435
1436static struct net_proto_family rose_family_ops = {
1437 .family = PF_ROSE,
1438 .create = rose_create,
1439 .owner = THIS_MODULE,
1440};
1441
1442static struct proto_ops rose_proto_ops = {
1443 .family = PF_ROSE,
1444 .owner = THIS_MODULE,
1445 .release = rose_release,
1446 .bind = rose_bind,
1447 .connect = rose_connect,
1448 .socketpair = sock_no_socketpair,
1449 .accept = rose_accept,
1450 .getname = rose_getname,
1451 .poll = datagram_poll,
1452 .ioctl = rose_ioctl,
1453 .listen = rose_listen,
1454 .shutdown = sock_no_shutdown,
1455 .setsockopt = rose_setsockopt,
1456 .getsockopt = rose_getsockopt,
1457 .sendmsg = rose_sendmsg,
1458 .recvmsg = rose_recvmsg,
1459 .mmap = sock_no_mmap,
1460 .sendpage = sock_no_sendpage,
1461};
1462
1463static struct notifier_block rose_dev_notifier = {
1464 .notifier_call = rose_device_event,
1465};
1466
1467static struct net_device **dev_rose;
1468
1469static const char banner[] = KERN_INFO "F6FBB/G4KLX ROSE for Linux. Version 0.62 for AX25.037 Linux 2.4\n";
1470
1471static int __init rose_proto_init(void)
1472{
1473 int i;
1474 int rc = proto_register(&rose_proto, 0);
1475
1476 if (rc != 0)
1477 goto out;
1478
1479 rose_callsign = null_ax25_address;
1480
1481 if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
1482 printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
1483 return -1;
1484 }
1485
1486 dev_rose = kmalloc(rose_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1487 if (dev_rose == NULL) {
1488 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
1489 return -1;
1490 }
1491
1492 memset(dev_rose, 0x00, rose_ndevs * sizeof(struct net_device*));
1493 for (i = 0; i < rose_ndevs; i++) {
1494 struct net_device *dev;
1495 char name[IFNAMSIZ];
1496
1497 sprintf(name, "rose%d", i);
1498 dev = alloc_netdev(sizeof(struct net_device_stats),
1499 name, rose_setup);
1500 if (!dev) {
1501 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
1502 goto fail;
1503 }
1504 if (register_netdev(dev)) {
1505 printk(KERN_ERR "ROSE: netdevice regeistration failed\n");
1506 free_netdev(dev);
1507 goto fail;
1508 }
1509 dev_rose[i] = dev;
1510 }
1511
1512 sock_register(&rose_family_ops);
1513 register_netdevice_notifier(&rose_dev_notifier);
1514 printk(banner);
1515
1516 ax25_protocol_register(AX25_P_ROSE, rose_route_frame);
1517 ax25_linkfail_register(rose_link_failed);
1518
1519#ifdef CONFIG_SYSCTL
1520 rose_register_sysctl();
1521#endif
1522 rose_loopback_init();
1523
1524 rose_add_loopback_neigh();
1525
1526 proc_net_fops_create("rose", S_IRUGO, &rose_info_fops);
1527 proc_net_fops_create("rose_neigh", S_IRUGO, &rose_neigh_fops);
1528 proc_net_fops_create("rose_nodes", S_IRUGO, &rose_nodes_fops);
1529 proc_net_fops_create("rose_routes", S_IRUGO, &rose_routes_fops);
1530out:
1531 return rc;
1532fail:
1533 while (--i >= 0) {
1534 unregister_netdev(dev_rose[i]);
1535 free_netdev(dev_rose[i]);
1536 }
1537 kfree(dev_rose);
1538 proto_unregister(&rose_proto);
1539 return -ENOMEM;
1540}
1541module_init(rose_proto_init);
1542
1543module_param(rose_ndevs, int, 0);
1544MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
1545
1546MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1547MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1548MODULE_LICENSE("GPL");
1549MODULE_ALIAS_NETPROTO(PF_ROSE);
1550
1551static void __exit rose_exit(void)
1552{
1553 int i;
1554
1555 proc_net_remove("rose");
1556 proc_net_remove("rose_neigh");
1557 proc_net_remove("rose_nodes");
1558 proc_net_remove("rose_routes");
1559 rose_loopback_clear();
1560
1561 rose_rt_free();
1562
1563 ax25_protocol_release(AX25_P_ROSE);
1564 ax25_linkfail_release(rose_link_failed);
1565
1566 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1567 ax25_listen_release(&rose_callsign, NULL);
1568
1569#ifdef CONFIG_SYSCTL
1570 rose_unregister_sysctl();
1571#endif
1572 unregister_netdevice_notifier(&rose_dev_notifier);
1573
1574 sock_unregister(PF_ROSE);
1575
1576 for (i = 0; i < rose_ndevs; i++) {
1577 struct net_device *dev = dev_rose[i];
1578
1579 if (dev) {
1580 unregister_netdev(dev);
1581 free_netdev(dev);
1582 }
1583 }
1584
1585 kfree(dev_rose);
1586 proto_unregister(&rose_proto);
1587}
1588
1589module_exit(rose_exit);
diff --git a/net/rose/rose_dev.c b/net/rose/rose_dev.c
new file mode 100644
index 000000000000..a8ed9a1d09f9
--- /dev/null
+++ b/net/rose/rose_dev.c
@@ -0,0 +1,154 @@
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 */
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>
24
25#include <asm/system.h>
26#include <asm/io.h>
27
28#include <linux/inet.h>
29#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/if_arp.h>
32#include <linux/skbuff.h>
33
34#include <net/ip.h>
35#include <net/arp.h>
36
37#include <net/ax25.h>
38#include <net/rose.h>
39
40static int rose_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
41 void *daddr, void *saddr, unsigned len)
42{
43 unsigned char *buff = skb_push(skb, ROSE_MIN_LEN + 2);
44
45 *buff++ = ROSE_GFI | ROSE_Q_BIT;
46 *buff++ = 0x00;
47 *buff++ = ROSE_DATA;
48 *buff++ = 0x7F;
49 *buff++ = AX25_P_IP;
50
51 if (daddr != NULL)
52 return 37;
53
54 return -37;
55}
56
57static int rose_rebuild_header(struct sk_buff *skb)
58{
59 struct net_device *dev = skb->dev;
60 struct net_device_stats *stats = netdev_priv(dev);
61 unsigned char *bp = (unsigned char *)skb->data;
62 struct sk_buff *skbn;
63
64#ifdef CONFIG_INET
65 if (arp_find(bp + 7, skb)) {
66 return 1;
67 }
68
69 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
70 kfree_skb(skb);
71 return 1;
72 }
73
74 if (skb->sk != NULL)
75 skb_set_owner_w(skbn, skb->sk);
76
77 kfree_skb(skb);
78
79 if (!rose_route_frame(skbn, NULL)) {
80 kfree_skb(skbn);
81 stats->tx_errors++;
82 return 1;
83 }
84
85 stats->tx_packets++;
86 stats->tx_bytes += skbn->len;
87#endif
88 return 1;
89}
90
91static int rose_set_mac_address(struct net_device *dev, void *addr)
92{
93 struct sockaddr *sa = addr;
94
95 rose_del_loopback_node((rose_address *)dev->dev_addr);
96
97 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
98
99 rose_add_loopback_node((rose_address *)dev->dev_addr);
100
101 return 0;
102}
103
104static int rose_open(struct net_device *dev)
105{
106 netif_start_queue(dev);
107 rose_add_loopback_node((rose_address *)dev->dev_addr);
108 return 0;
109}
110
111static int rose_close(struct net_device *dev)
112{
113 netif_stop_queue(dev);
114 rose_del_loopback_node((rose_address *)dev->dev_addr);
115 return 0;
116}
117
118static int rose_xmit(struct sk_buff *skb, struct net_device *dev)
119{
120 struct net_device_stats *stats = netdev_priv(dev);
121
122 if (!netif_running(dev)) {
123 printk(KERN_ERR "ROSE: rose_xmit - called when iface is down\n");
124 return 1;
125 }
126 dev_kfree_skb(skb);
127 stats->tx_errors++;
128 return 0;
129}
130
131static struct net_device_stats *rose_get_stats(struct net_device *dev)
132{
133 return netdev_priv(dev);
134}
135
136void rose_setup(struct net_device *dev)
137{
138 SET_MODULE_OWNER(dev);
139 dev->mtu = ROSE_MAX_PACKET_SIZE - 2;
140 dev->hard_start_xmit = rose_xmit;
141 dev->open = rose_open;
142 dev->stop = rose_close;
143
144 dev->hard_header = rose_header;
145 dev->hard_header_len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
146 dev->addr_len = ROSE_ADDR_LEN;
147 dev->type = ARPHRD_ROSE;
148 dev->rebuild_header = rose_rebuild_header;
149 dev->set_mac_address = rose_set_mac_address;
150
151 /* New-style flags. */
152 dev->flags = 0;
153 dev->get_stats = rose_get_stats;
154}
diff --git a/net/rose/rose_in.c b/net/rose/rose_in.c
new file mode 100644
index 000000000000..ef475a1bb1ba
--- /dev/null
+++ b/net/rose/rose_in.c
@@ -0,0 +1,297 @@
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 *
9 * Most of this code is based on the SDL diagrams published in the 7th ARRL
10 * Computer Networking Conference papers. The diagrams have mistakes in them,
11 * but are mostly correct. Before you modify the code could you read the SDL
12 * diagrams as the code is not obvious and probably very easy to break.
13 */
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 <net/ax25.h>
25#include <linux/inet.h>
26#include <linux/netdevice.h>
27#include <linux/skbuff.h>
28#include <net/sock.h>
29#include <net/ip.h> /* For ip_rcv */
30#include <net/tcp.h>
31#include <asm/system.h>
32#include <linux/fcntl.h>
33#include <linux/mm.h>
34#include <linux/interrupt.h>
35#include <net/rose.h>
36
37/*
38 * State machine for state 1, Awaiting Call Accepted State.
39 * The handling of the timer(s) is in file rose_timer.c.
40 * Handling of state 0 and connection release is in af_rose.c.
41 */
42static int rose_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
43{
44 struct rose_sock *rose = rose_sk(sk);
45
46 switch (frametype) {
47 case ROSE_CALL_ACCEPTED:
48 rose_stop_timer(sk);
49 rose_start_idletimer(sk);
50 rose->condition = 0x00;
51 rose->vs = 0;
52 rose->va = 0;
53 rose->vr = 0;
54 rose->vl = 0;
55 rose->state = ROSE_STATE_3;
56 sk->sk_state = TCP_ESTABLISHED;
57 if (!sock_flag(sk, SOCK_DEAD))
58 sk->sk_state_change(sk);
59 break;
60
61 case ROSE_CLEAR_REQUEST:
62 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
63 rose_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
64 rose->neighbour->use--;
65 break;
66
67 default:
68 break;
69 }
70
71 return 0;
72}
73
74/*
75 * State machine for state 2, Awaiting Clear Confirmation State.
76 * The handling of the timer(s) is in file rose_timer.c
77 * Handling of state 0 and connection release is in af_rose.c.
78 */
79static int rose_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
80{
81 struct rose_sock *rose = rose_sk(sk);
82
83 switch (frametype) {
84 case ROSE_CLEAR_REQUEST:
85 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
86 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
87 rose->neighbour->use--;
88 break;
89
90 case ROSE_CLEAR_CONFIRMATION:
91 rose_disconnect(sk, 0, -1, -1);
92 rose->neighbour->use--;
93 break;
94
95 default:
96 break;
97 }
98
99 return 0;
100}
101
102/*
103 * State machine for state 3, Connected State.
104 * The handling of the timer(s) is in file rose_timer.c
105 * Handling of state 0 and connection release is in af_rose.c.
106 */
107static int rose_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
108{
109 struct rose_sock *rose = rose_sk(sk);
110 int queued = 0;
111
112 switch (frametype) {
113 case ROSE_RESET_REQUEST:
114 rose_stop_timer(sk);
115 rose_start_idletimer(sk);
116 rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
117 rose->condition = 0x00;
118 rose->vs = 0;
119 rose->vr = 0;
120 rose->va = 0;
121 rose->vl = 0;
122 rose_requeue_frames(sk);
123 break;
124
125 case ROSE_CLEAR_REQUEST:
126 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
127 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
128 rose->neighbour->use--;
129 break;
130
131 case ROSE_RR:
132 case ROSE_RNR:
133 if (!rose_validate_nr(sk, nr)) {
134 rose_write_internal(sk, ROSE_RESET_REQUEST);
135 rose->condition = 0x00;
136 rose->vs = 0;
137 rose->vr = 0;
138 rose->va = 0;
139 rose->vl = 0;
140 rose->state = ROSE_STATE_4;
141 rose_start_t2timer(sk);
142 rose_stop_idletimer(sk);
143 } else {
144 rose_frames_acked(sk, nr);
145 if (frametype == ROSE_RNR) {
146 rose->condition |= ROSE_COND_PEER_RX_BUSY;
147 } else {
148 rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
149 }
150 }
151 break;
152
153 case ROSE_DATA: /* XXX */
154 rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
155 if (!rose_validate_nr(sk, nr)) {
156 rose_write_internal(sk, ROSE_RESET_REQUEST);
157 rose->condition = 0x00;
158 rose->vs = 0;
159 rose->vr = 0;
160 rose->va = 0;
161 rose->vl = 0;
162 rose->state = ROSE_STATE_4;
163 rose_start_t2timer(sk);
164 rose_stop_idletimer(sk);
165 break;
166 }
167 rose_frames_acked(sk, nr);
168 if (ns == rose->vr) {
169 rose_start_idletimer(sk);
170 if (sock_queue_rcv_skb(sk, skb) == 0) {
171 rose->vr = (rose->vr + 1) % ROSE_MODULUS;
172 queued = 1;
173 } else {
174 /* Should never happen ! */
175 rose_write_internal(sk, ROSE_RESET_REQUEST);
176 rose->condition = 0x00;
177 rose->vs = 0;
178 rose->vr = 0;
179 rose->va = 0;
180 rose->vl = 0;
181 rose->state = ROSE_STATE_4;
182 rose_start_t2timer(sk);
183 rose_stop_idletimer(sk);
184 break;
185 }
186 if (atomic_read(&sk->sk_rmem_alloc) >
187 (sk->sk_rcvbuf / 2))
188 rose->condition |= ROSE_COND_OWN_RX_BUSY;
189 }
190 /*
191 * If the window is full, ack the frame, else start the
192 * acknowledge hold back timer.
193 */
194 if (((rose->vl + sysctl_rose_window_size) % ROSE_MODULUS) == rose->vr) {
195 rose->condition &= ~ROSE_COND_ACK_PENDING;
196 rose_stop_timer(sk);
197 rose_enquiry_response(sk);
198 } else {
199 rose->condition |= ROSE_COND_ACK_PENDING;
200 rose_start_hbtimer(sk);
201 }
202 break;
203
204 default:
205 printk(KERN_WARNING "ROSE: unknown %02X in state 3\n", frametype);
206 break;
207 }
208
209 return queued;
210}
211
212/*
213 * State machine for state 4, Awaiting Reset Confirmation State.
214 * The handling of the timer(s) is in file rose_timer.c
215 * Handling of state 0 and connection release is in af_rose.c.
216 */
217static int rose_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
218{
219 struct rose_sock *rose = rose_sk(sk);
220
221 switch (frametype) {
222 case ROSE_RESET_REQUEST:
223 rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
224 case ROSE_RESET_CONFIRMATION:
225 rose_stop_timer(sk);
226 rose_start_idletimer(sk);
227 rose->condition = 0x00;
228 rose->va = 0;
229 rose->vr = 0;
230 rose->vs = 0;
231 rose->vl = 0;
232 rose->state = ROSE_STATE_3;
233 rose_requeue_frames(sk);
234 break;
235
236 case ROSE_CLEAR_REQUEST:
237 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
238 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
239 rose->neighbour->use--;
240 break;
241
242 default:
243 break;
244 }
245
246 return 0;
247}
248
249/*
250 * State machine for state 5, Awaiting Call Acceptance State.
251 * The handling of the timer(s) is in file rose_timer.c
252 * Handling of state 0 and connection release is in af_rose.c.
253 */
254static int rose_state5_machine(struct sock *sk, struct sk_buff *skb, int frametype)
255{
256 if (frametype == ROSE_CLEAR_REQUEST) {
257 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
258 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
259 rose_sk(sk)->neighbour->use--;
260 }
261
262 return 0;
263}
264
265/* Higher level upcall for a LAPB frame */
266int rose_process_rx_frame(struct sock *sk, struct sk_buff *skb)
267{
268 struct rose_sock *rose = rose_sk(sk);
269 int queued = 0, frametype, ns, nr, q, d, m;
270
271 if (rose->state == ROSE_STATE_0)
272 return 0;
273
274 frametype = rose_decode(skb, &ns, &nr, &q, &d, &m);
275
276 switch (rose->state) {
277 case ROSE_STATE_1:
278 queued = rose_state1_machine(sk, skb, frametype);
279 break;
280 case ROSE_STATE_2:
281 queued = rose_state2_machine(sk, skb, frametype);
282 break;
283 case ROSE_STATE_3:
284 queued = rose_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
285 break;
286 case ROSE_STATE_4:
287 queued = rose_state4_machine(sk, skb, frametype);
288 break;
289 case ROSE_STATE_5:
290 queued = rose_state5_machine(sk, skb, frametype);
291 break;
292 }
293
294 rose_kick(sk);
295
296 return queued;
297}
diff --git a/net/rose/rose_link.c b/net/rose/rose_link.c
new file mode 100644
index 000000000000..09e9e9d04d92
--- /dev/null
+++ b/net/rose/rose_link.c
@@ -0,0 +1,288 @@
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 */
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/jiffies.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 <asm/system.h>
25#include <linux/fcntl.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
28#include <linux/netfilter.h>
29#include <net/rose.h>
30
31static void rose_ftimer_expiry(unsigned long);
32static void rose_t0timer_expiry(unsigned long);
33
34static void rose_transmit_restart_confirmation(struct rose_neigh *neigh);
35static void rose_transmit_restart_request(struct rose_neigh *neigh);
36
37void rose_start_ftimer(struct rose_neigh *neigh)
38{
39 del_timer(&neigh->ftimer);
40
41 neigh->ftimer.data = (unsigned long)neigh;
42 neigh->ftimer.function = &rose_ftimer_expiry;
43 neigh->ftimer.expires = jiffies + sysctl_rose_link_fail_timeout;
44
45 add_timer(&neigh->ftimer);
46}
47
48static void rose_start_t0timer(struct rose_neigh *neigh)
49{
50 del_timer(&neigh->t0timer);
51
52 neigh->t0timer.data = (unsigned long)neigh;
53 neigh->t0timer.function = &rose_t0timer_expiry;
54 neigh->t0timer.expires = jiffies + sysctl_rose_restart_request_timeout;
55
56 add_timer(&neigh->t0timer);
57}
58
59void rose_stop_ftimer(struct rose_neigh *neigh)
60{
61 del_timer(&neigh->ftimer);
62}
63
64void rose_stop_t0timer(struct rose_neigh *neigh)
65{
66 del_timer(&neigh->t0timer);
67}
68
69int rose_ftimer_running(struct rose_neigh *neigh)
70{
71 return timer_pending(&neigh->ftimer);
72}
73
74static int rose_t0timer_running(struct rose_neigh *neigh)
75{
76 return timer_pending(&neigh->t0timer);
77}
78
79static void rose_ftimer_expiry(unsigned long param)
80{
81}
82
83static void rose_t0timer_expiry(unsigned long param)
84{
85 struct rose_neigh *neigh = (struct rose_neigh *)param;
86
87 rose_transmit_restart_request(neigh);
88
89 neigh->dce_mode = 0;
90
91 rose_start_t0timer(neigh);
92}
93
94/*
95 * Interface to ax25_send_frame. Changes my level 2 callsign depending
96 * on whether we have a global ROSE callsign or use the default port
97 * callsign.
98 */
99static int rose_send_frame(struct sk_buff *skb, struct rose_neigh *neigh)
100{
101 ax25_address *rose_call;
102
103 if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
104 rose_call = (ax25_address *)neigh->dev->dev_addr;
105 else
106 rose_call = &rose_callsign;
107
108 neigh->ax25 = ax25_send_frame(skb, 260, rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
109
110 return (neigh->ax25 != NULL);
111}
112
113/*
114 * Interface to ax25_link_up. Changes my level 2 callsign depending
115 * on whether we have a global ROSE callsign or use the default port
116 * callsign.
117 */
118static int rose_link_up(struct rose_neigh *neigh)
119{
120 ax25_address *rose_call;
121
122 if (ax25cmp(&rose_callsign, &null_ax25_address) == 0)
123 rose_call = (ax25_address *)neigh->dev->dev_addr;
124 else
125 rose_call = &rose_callsign;
126
127 neigh->ax25 = ax25_find_cb(rose_call, &neigh->callsign, neigh->digipeat, neigh->dev);
128
129 return (neigh->ax25 != NULL);
130}
131
132/*
133 * This handles all restart and diagnostic frames.
134 */
135void rose_link_rx_restart(struct sk_buff *skb, struct rose_neigh *neigh, unsigned short frametype)
136{
137 struct sk_buff *skbn;
138
139 switch (frametype) {
140 case ROSE_RESTART_REQUEST:
141 rose_stop_t0timer(neigh);
142 neigh->restarted = 1;
143 neigh->dce_mode = (skb->data[3] == ROSE_DTE_ORIGINATED);
144 rose_transmit_restart_confirmation(neigh);
145 break;
146
147 case ROSE_RESTART_CONFIRMATION:
148 rose_stop_t0timer(neigh);
149 neigh->restarted = 1;
150 break;
151
152 case ROSE_DIAGNOSTIC:
153 printk(KERN_WARNING "ROSE: received diagnostic #%d - %02X %02X %02X\n", skb->data[3], skb->data[4], skb->data[5], skb->data[6]);
154 break;
155
156 default:
157 printk(KERN_WARNING "ROSE: received unknown %02X with LCI 000\n", frametype);
158 break;
159 }
160
161 if (neigh->restarted) {
162 while ((skbn = skb_dequeue(&neigh->queue)) != NULL)
163 if (!rose_send_frame(skbn, neigh))
164 kfree_skb(skbn);
165 }
166}
167
168/*
169 * This routine is called when a Restart Request is needed
170 */
171static void rose_transmit_restart_request(struct rose_neigh *neigh)
172{
173 struct sk_buff *skb;
174 unsigned char *dptr;
175 int len;
176
177 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
178
179 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
180 return;
181
182 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
183
184 dptr = skb_put(skb, ROSE_MIN_LEN + 3);
185
186 *dptr++ = AX25_P_ROSE;
187 *dptr++ = ROSE_GFI;
188 *dptr++ = 0x00;
189 *dptr++ = ROSE_RESTART_REQUEST;
190 *dptr++ = ROSE_DTE_ORIGINATED;
191 *dptr++ = 0;
192
193 if (!rose_send_frame(skb, neigh))
194 kfree_skb(skb);
195}
196
197/*
198 * This routine is called when a Restart Confirmation is needed
199 */
200static void rose_transmit_restart_confirmation(struct rose_neigh *neigh)
201{
202 struct sk_buff *skb;
203 unsigned char *dptr;
204 int len;
205
206 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
207
208 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
209 return;
210
211 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
212
213 dptr = skb_put(skb, ROSE_MIN_LEN + 1);
214
215 *dptr++ = AX25_P_ROSE;
216 *dptr++ = ROSE_GFI;
217 *dptr++ = 0x00;
218 *dptr++ = ROSE_RESTART_CONFIRMATION;
219
220 if (!rose_send_frame(skb, neigh))
221 kfree_skb(skb);
222}
223
224/*
225 * This routine is called when a Clear Request is needed outside of the context
226 * of a connected socket.
227 */
228void rose_transmit_clear_request(struct rose_neigh *neigh, unsigned int lci, unsigned char cause, unsigned char diagnostic)
229{
230 struct sk_buff *skb;
231 unsigned char *dptr;
232 int len;
233
234 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 3;
235
236 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
237 return;
238
239 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN);
240
241 dptr = skb_put(skb, ROSE_MIN_LEN + 3);
242
243 *dptr++ = AX25_P_ROSE;
244 *dptr++ = ((lci >> 8) & 0x0F) | ROSE_GFI;
245 *dptr++ = ((lci >> 0) & 0xFF);
246 *dptr++ = ROSE_CLEAR_REQUEST;
247 *dptr++ = cause;
248 *dptr++ = diagnostic;
249
250 if (!rose_send_frame(skb, neigh))
251 kfree_skb(skb);
252}
253
254void rose_transmit_link(struct sk_buff *skb, struct rose_neigh *neigh)
255{
256 unsigned char *dptr;
257
258#if 0
259 if (call_fw_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT) {
260 kfree_skb(skb);
261 return;
262 }
263#endif
264
265 if (neigh->loopback) {
266 rose_loopback_queue(skb, neigh);
267 return;
268 }
269
270 if (!rose_link_up(neigh))
271 neigh->restarted = 0;
272
273 dptr = skb_push(skb, 1);
274 *dptr++ = AX25_P_ROSE;
275
276 if (neigh->restarted) {
277 if (!rose_send_frame(skb, neigh))
278 kfree_skb(skb);
279 } else {
280 skb_queue_tail(&neigh->queue, skb);
281
282 if (!rose_t0timer_running(neigh)) {
283 rose_transmit_restart_request(neigh);
284 neigh->dce_mode = 0;
285 rose_start_t0timer(neigh);
286 }
287 }
288}
diff --git a/net/rose/rose_loopback.c b/net/rose/rose_loopback.c
new file mode 100644
index 000000000000..103b4d38f88a
--- /dev/null
+++ b/net/rose/rose_loopback.c
@@ -0,0 +1,111 @@
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 */
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/rose.h>
15#include <linux/init.h>
16
17static struct sk_buff_head loopback_queue;
18static struct timer_list loopback_timer;
19
20static void rose_set_loopback_timer(void);
21
22void rose_loopback_init(void)
23{
24 skb_queue_head_init(&loopback_queue);
25
26 init_timer(&loopback_timer);
27}
28
29static int rose_loopback_running(void)
30{
31 return timer_pending(&loopback_timer);
32}
33
34int rose_loopback_queue(struct sk_buff *skb, struct rose_neigh *neigh)
35{
36 struct sk_buff *skbn;
37
38 skbn = skb_clone(skb, GFP_ATOMIC);
39
40 kfree_skb(skb);
41
42 if (skbn != NULL) {
43 skb_queue_tail(&loopback_queue, skbn);
44
45 if (!rose_loopback_running())
46 rose_set_loopback_timer();
47 }
48
49 return 1;
50}
51
52static void rose_loopback_timer(unsigned long);
53
54static void rose_set_loopback_timer(void)
55{
56 del_timer(&loopback_timer);
57
58 loopback_timer.data = 0;
59 loopback_timer.function = &rose_loopback_timer;
60 loopback_timer.expires = jiffies + 10;
61
62 add_timer(&loopback_timer);
63}
64
65static void rose_loopback_timer(unsigned long param)
66{
67 struct sk_buff *skb;
68 struct net_device *dev;
69 rose_address *dest;
70 struct sock *sk;
71 unsigned short frametype;
72 unsigned int lci_i, lci_o;
73
74 while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
75 lci_i = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
76 frametype = skb->data[2];
77 dest = (rose_address *)(skb->data + 4);
78 lci_o = 0xFFF - lci_i;
79
80 skb->h.raw = skb->data;
81
82 if ((sk = rose_find_socket(lci_o, rose_loopback_neigh)) != NULL) {
83 if (rose_process_rx_frame(sk, skb) == 0)
84 kfree_skb(skb);
85 continue;
86 }
87
88 if (frametype == ROSE_CALL_REQUEST) {
89 if ((dev = rose_dev_get(dest)) != NULL) {
90 if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
91 kfree_skb(skb);
92 } else {
93 kfree_skb(skb);
94 }
95 } else {
96 kfree_skb(skb);
97 }
98 }
99}
100
101void __exit rose_loopback_clear(void)
102{
103 struct sk_buff *skb;
104
105 del_timer(&loopback_timer);
106
107 while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
108 skb->sk = NULL;
109 kfree_skb(skb);
110 }
111}
diff --git a/net/rose/rose_out.c b/net/rose/rose_out.c
new file mode 100644
index 000000000000..2965ffc83b9b
--- /dev/null
+++ b/net/rose/rose_out.c
@@ -0,0 +1,126 @@
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 */
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 <asm/system.h>
25#include <linux/fcntl.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
28#include <net/rose.h>
29
30/*
31 * This procedure is passed a buffer descriptor for an iframe. It builds
32 * the rest of the control part of the frame and then writes it out.
33 */
34static void rose_send_iframe(struct sock *sk, struct sk_buff *skb)
35{
36 struct rose_sock *rose = rose_sk(sk);
37
38 if (skb == NULL)
39 return;
40
41 skb->data[2] |= (rose->vr << 5) & 0xE0;
42 skb->data[2] |= (rose->vs << 1) & 0x0E;
43
44 rose_start_idletimer(sk);
45
46 rose_transmit_link(skb, rose->neighbour);
47}
48
49void rose_kick(struct sock *sk)
50{
51 struct rose_sock *rose = rose_sk(sk);
52 struct sk_buff *skb, *skbn;
53 unsigned short start, end;
54
55 if (rose->state != ROSE_STATE_3)
56 return;
57
58 if (rose->condition & ROSE_COND_PEER_RX_BUSY)
59 return;
60
61 if (!skb_peek(&sk->sk_write_queue))
62 return;
63
64 start = (skb_peek(&rose->ack_queue) == NULL) ? rose->va : rose->vs;
65 end = (rose->va + sysctl_rose_window_size) % ROSE_MODULUS;
66
67 if (start == end)
68 return;
69
70 rose->vs = start;
71
72 /*
73 * Transmit data until either we're out of data to send or
74 * the window is full.
75 */
76
77 skb = skb_dequeue(&sk->sk_write_queue);
78
79 do {
80 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
81 skb_queue_head(&sk->sk_write_queue, skb);
82 break;
83 }
84
85 skb_set_owner_w(skbn, sk);
86
87 /*
88 * Transmit the frame copy.
89 */
90 rose_send_iframe(sk, skbn);
91
92 rose->vs = (rose->vs + 1) % ROSE_MODULUS;
93
94 /*
95 * Requeue the original data frame.
96 */
97 skb_queue_tail(&rose->ack_queue, skb);
98
99 } while (rose->vs != end &&
100 (skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
101
102 rose->vl = rose->vr;
103 rose->condition &= ~ROSE_COND_ACK_PENDING;
104
105 rose_stop_timer(sk);
106}
107
108/*
109 * The following routines are taken from page 170 of the 7th ARRL Computer
110 * Networking Conference paper, as is the whole state machine.
111 */
112
113void rose_enquiry_response(struct sock *sk)
114{
115 struct rose_sock *rose = rose_sk(sk);
116
117 if (rose->condition & ROSE_COND_OWN_RX_BUSY)
118 rose_write_internal(sk, ROSE_RNR);
119 else
120 rose_write_internal(sk, ROSE_RR);
121
122 rose->vl = rose->vr;
123 rose->condition &= ~ROSE_COND_ACK_PENDING;
124
125 rose_stop_timer(sk);
126}
diff --git a/net/rose/rose_route.c b/net/rose/rose_route.c
new file mode 100644
index 000000000000..ff73ebb912b8
--- /dev/null
+++ b/net/rose/rose_route.c
@@ -0,0 +1,1343 @@
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) Terry Dawson VK2KTJ (terry@animats.net)
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 <net/arp.h>
24#include <linux/if_arp.h>
25#include <linux/skbuff.h>
26#include <net/sock.h>
27#include <net/tcp.h>
28#include <asm/system.h>
29#include <asm/uaccess.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 <net/rose.h>
38#include <linux/seq_file.h>
39
40static unsigned int rose_neigh_no = 1;
41
42static struct rose_node *rose_node_list;
43static DEFINE_SPINLOCK(rose_node_list_lock);
44static struct rose_neigh *rose_neigh_list;
45static DEFINE_SPINLOCK(rose_neigh_list_lock);
46static struct rose_route *rose_route_list;
47static DEFINE_SPINLOCK(rose_route_list_lock);
48
49struct rose_neigh *rose_loopback_neigh;
50
51static void rose_remove_neigh(struct rose_neigh *);
52
53/*
54 * Add a new route to a node, and in the process add the node and the
55 * neighbour if it is new.
56 */
57static int rose_add_node(struct rose_route_struct *rose_route,
58 struct net_device *dev)
59{
60 struct rose_node *rose_node, *rose_tmpn, *rose_tmpp;
61 struct rose_neigh *rose_neigh;
62 int i, res = 0;
63
64 spin_lock_bh(&rose_node_list_lock);
65 spin_lock_bh(&rose_neigh_list_lock);
66
67 rose_node = rose_node_list;
68 while (rose_node != NULL) {
69 if ((rose_node->mask == rose_route->mask) &&
70 (rosecmpm(&rose_route->address, &rose_node->address,
71 rose_route->mask) == 0))
72 break;
73 rose_node = rose_node->next;
74 }
75
76 if (rose_node != NULL && rose_node->loopback) {
77 res = -EINVAL;
78 goto out;
79 }
80
81 rose_neigh = rose_neigh_list;
82 while (rose_neigh != NULL) {
83 if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0
84 && rose_neigh->dev == dev)
85 break;
86 rose_neigh = rose_neigh->next;
87 }
88
89 if (rose_neigh == NULL) {
90 rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
91 if (rose_neigh == NULL) {
92 res = -ENOMEM;
93 goto out;
94 }
95
96 rose_neigh->callsign = rose_route->neighbour;
97 rose_neigh->digipeat = NULL;
98 rose_neigh->ax25 = NULL;
99 rose_neigh->dev = dev;
100 rose_neigh->count = 0;
101 rose_neigh->use = 0;
102 rose_neigh->dce_mode = 0;
103 rose_neigh->loopback = 0;
104 rose_neigh->number = rose_neigh_no++;
105 rose_neigh->restarted = 0;
106
107 skb_queue_head_init(&rose_neigh->queue);
108
109 init_timer(&rose_neigh->ftimer);
110 init_timer(&rose_neigh->t0timer);
111
112 if (rose_route->ndigis != 0) {
113 if ((rose_neigh->digipeat = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) {
114 kfree(rose_neigh);
115 res = -ENOMEM;
116 goto out;
117 }
118
119 rose_neigh->digipeat->ndigi = rose_route->ndigis;
120 rose_neigh->digipeat->lastrepeat = -1;
121
122 for (i = 0; i < rose_route->ndigis; i++) {
123 rose_neigh->digipeat->calls[i] =
124 rose_route->digipeaters[i];
125 rose_neigh->digipeat->repeated[i] = 0;
126 }
127 }
128
129 rose_neigh->next = rose_neigh_list;
130 rose_neigh_list = rose_neigh;
131 }
132
133 /*
134 * This is a new node to be inserted into the list. Find where it needs
135 * to be inserted into the list, and insert it. We want to be sure
136 * to order the list in descending order of mask size to ensure that
137 * later when we are searching this list the first match will be the
138 * best match.
139 */
140 if (rose_node == NULL) {
141 rose_tmpn = rose_node_list;
142 rose_tmpp = NULL;
143
144 while (rose_tmpn != NULL) {
145 if (rose_tmpn->mask > rose_route->mask) {
146 rose_tmpp = rose_tmpn;
147 rose_tmpn = rose_tmpn->next;
148 } else {
149 break;
150 }
151 }
152
153 /* create new node */
154 rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
155 if (rose_node == NULL) {
156 res = -ENOMEM;
157 goto out;
158 }
159
160 rose_node->address = rose_route->address;
161 rose_node->mask = rose_route->mask;
162 rose_node->count = 1;
163 rose_node->loopback = 0;
164 rose_node->neighbour[0] = rose_neigh;
165
166 if (rose_tmpn == NULL) {
167 if (rose_tmpp == NULL) { /* Empty list */
168 rose_node_list = rose_node;
169 rose_node->next = NULL;
170 } else {
171 rose_tmpp->next = rose_node;
172 rose_node->next = NULL;
173 }
174 } else {
175 if (rose_tmpp == NULL) { /* 1st node */
176 rose_node->next = rose_node_list;
177 rose_node_list = rose_node;
178 } else {
179 rose_tmpp->next = rose_node;
180 rose_node->next = rose_tmpn;
181 }
182 }
183 rose_neigh->count++;
184
185 goto out;
186 }
187
188 /* We have space, slot it in */
189 if (rose_node->count < 3) {
190 rose_node->neighbour[rose_node->count] = rose_neigh;
191 rose_node->count++;
192 rose_neigh->count++;
193 }
194
195out:
196 spin_unlock_bh(&rose_neigh_list_lock);
197 spin_unlock_bh(&rose_node_list_lock);
198
199 return res;
200}
201
202/*
203 * Caller is holding rose_node_list_lock.
204 */
205static void rose_remove_node(struct rose_node *rose_node)
206{
207 struct rose_node *s;
208
209 if ((s = rose_node_list) == rose_node) {
210 rose_node_list = rose_node->next;
211 kfree(rose_node);
212 return;
213 }
214
215 while (s != NULL && s->next != NULL) {
216 if (s->next == rose_node) {
217 s->next = rose_node->next;
218 kfree(rose_node);
219 return;
220 }
221
222 s = s->next;
223 }
224}
225
226/*
227 * Caller is holding rose_neigh_list_lock.
228 */
229static void rose_remove_neigh(struct rose_neigh *rose_neigh)
230{
231 struct rose_neigh *s;
232
233 rose_stop_ftimer(rose_neigh);
234 rose_stop_t0timer(rose_neigh);
235
236 skb_queue_purge(&rose_neigh->queue);
237
238 spin_lock_bh(&rose_neigh_list_lock);
239
240 if ((s = rose_neigh_list) == rose_neigh) {
241 rose_neigh_list = rose_neigh->next;
242 spin_unlock_bh(&rose_neigh_list_lock);
243 if (rose_neigh->digipeat != NULL)
244 kfree(rose_neigh->digipeat);
245 kfree(rose_neigh);
246 return;
247 }
248
249 while (s != NULL && s->next != NULL) {
250 if (s->next == rose_neigh) {
251 s->next = rose_neigh->next;
252 spin_unlock_bh(&rose_neigh_list_lock);
253 if (rose_neigh->digipeat != NULL)
254 kfree(rose_neigh->digipeat);
255 kfree(rose_neigh);
256 return;
257 }
258
259 s = s->next;
260 }
261 spin_unlock_bh(&rose_neigh_list_lock);
262}
263
264/*
265 * Caller is holding rose_route_list_lock.
266 */
267static void rose_remove_route(struct rose_route *rose_route)
268{
269 struct rose_route *s;
270
271 if (rose_route->neigh1 != NULL)
272 rose_route->neigh1->use--;
273
274 if (rose_route->neigh2 != NULL)
275 rose_route->neigh2->use--;
276
277 if ((s = rose_route_list) == rose_route) {
278 rose_route_list = rose_route->next;
279 kfree(rose_route);
280 return;
281 }
282
283 while (s != NULL && s->next != NULL) {
284 if (s->next == rose_route) {
285 s->next = rose_route->next;
286 kfree(rose_route);
287 return;
288 }
289
290 s = s->next;
291 }
292}
293
294/*
295 * "Delete" a node. Strictly speaking remove a route to a node. The node
296 * is only deleted if no routes are left to it.
297 */
298static int rose_del_node(struct rose_route_struct *rose_route,
299 struct net_device *dev)
300{
301 struct rose_node *rose_node;
302 struct rose_neigh *rose_neigh;
303 int i, err = 0;
304
305 spin_lock_bh(&rose_node_list_lock);
306 spin_lock_bh(&rose_neigh_list_lock);
307
308 rose_node = rose_node_list;
309 while (rose_node != NULL) {
310 if ((rose_node->mask == rose_route->mask) &&
311 (rosecmpm(&rose_route->address, &rose_node->address,
312 rose_route->mask) == 0))
313 break;
314 rose_node = rose_node->next;
315 }
316
317 if (rose_node == NULL || rose_node->loopback) {
318 err = -EINVAL;
319 goto out;
320 }
321
322 rose_neigh = rose_neigh_list;
323 while (rose_neigh != NULL) {
324 if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0
325 && rose_neigh->dev == dev)
326 break;
327 rose_neigh = rose_neigh->next;
328 }
329
330 if (rose_neigh == NULL) {
331 err = -EINVAL;
332 goto out;
333 }
334
335 for (i = 0; i < rose_node->count; i++) {
336 if (rose_node->neighbour[i] == rose_neigh) {
337 rose_neigh->count--;
338
339 if (rose_neigh->count == 0 && rose_neigh->use == 0)
340 rose_remove_neigh(rose_neigh);
341
342 rose_node->count--;
343
344 if (rose_node->count == 0) {
345 rose_remove_node(rose_node);
346 } else {
347 switch (i) {
348 case 0:
349 rose_node->neighbour[0] =
350 rose_node->neighbour[1];
351 case 1:
352 rose_node->neighbour[1] =
353 rose_node->neighbour[2];
354 case 2:
355 break;
356 }
357 }
358 goto out;
359 }
360 }
361 err = -EINVAL;
362
363out:
364 spin_unlock_bh(&rose_neigh_list_lock);
365 spin_unlock_bh(&rose_node_list_lock);
366
367 return err;
368}
369
370/*
371 * Add the loopback neighbour.
372 */
373int rose_add_loopback_neigh(void)
374{
375 if ((rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_ATOMIC)) == NULL)
376 return -ENOMEM;
377
378 rose_loopback_neigh->callsign = null_ax25_address;
379 rose_loopback_neigh->digipeat = NULL;
380 rose_loopback_neigh->ax25 = NULL;
381 rose_loopback_neigh->dev = NULL;
382 rose_loopback_neigh->count = 0;
383 rose_loopback_neigh->use = 0;
384 rose_loopback_neigh->dce_mode = 1;
385 rose_loopback_neigh->loopback = 1;
386 rose_loopback_neigh->number = rose_neigh_no++;
387 rose_loopback_neigh->restarted = 1;
388
389 skb_queue_head_init(&rose_loopback_neigh->queue);
390
391 init_timer(&rose_loopback_neigh->ftimer);
392 init_timer(&rose_loopback_neigh->t0timer);
393
394 spin_lock_bh(&rose_neigh_list_lock);
395 rose_loopback_neigh->next = rose_neigh_list;
396 rose_neigh_list = rose_loopback_neigh;
397 spin_unlock_bh(&rose_neigh_list_lock);
398
399 return 0;
400}
401
402/*
403 * Add a loopback node.
404 */
405int rose_add_loopback_node(rose_address *address)
406{
407 struct rose_node *rose_node;
408 unsigned int err = 0;
409
410 spin_lock_bh(&rose_node_list_lock);
411
412 rose_node = rose_node_list;
413 while (rose_node != NULL) {
414 if ((rose_node->mask == 10) &&
415 (rosecmpm(address, &rose_node->address, 10) == 0) &&
416 rose_node->loopback)
417 break;
418 rose_node = rose_node->next;
419 }
420
421 if (rose_node != NULL)
422 goto out;
423
424 if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
425 err = -ENOMEM;
426 goto out;
427 }
428
429 rose_node->address = *address;
430 rose_node->mask = 10;
431 rose_node->count = 1;
432 rose_node->loopback = 1;
433 rose_node->neighbour[0] = rose_loopback_neigh;
434
435 /* Insert at the head of list. Address is always mask=10 */
436 rose_node->next = rose_node_list;
437 rose_node_list = rose_node;
438
439 rose_loopback_neigh->count++;
440
441out:
442 spin_unlock_bh(&rose_node_list_lock);
443
444 return 0;
445}
446
447/*
448 * Delete a loopback node.
449 */
450void rose_del_loopback_node(rose_address *address)
451{
452 struct rose_node *rose_node;
453
454 spin_lock_bh(&rose_node_list_lock);
455
456 rose_node = rose_node_list;
457 while (rose_node != NULL) {
458 if ((rose_node->mask == 10) &&
459 (rosecmpm(address, &rose_node->address, 10) == 0) &&
460 rose_node->loopback)
461 break;
462 rose_node = rose_node->next;
463 }
464
465 if (rose_node == NULL)
466 goto out;
467
468 rose_remove_node(rose_node);
469
470 rose_loopback_neigh->count--;
471
472out:
473 spin_unlock_bh(&rose_node_list_lock);
474}
475
476/*
477 * A device has been removed. Remove its routes and neighbours.
478 */
479void rose_rt_device_down(struct net_device *dev)
480{
481 struct rose_neigh *s, *rose_neigh;
482 struct rose_node *t, *rose_node;
483 int i;
484
485 spin_lock_bh(&rose_node_list_lock);
486 spin_lock_bh(&rose_neigh_list_lock);
487 rose_neigh = rose_neigh_list;
488 while (rose_neigh != NULL) {
489 s = rose_neigh;
490 rose_neigh = rose_neigh->next;
491
492 if (s->dev != dev)
493 continue;
494
495 rose_node = rose_node_list;
496
497 while (rose_node != NULL) {
498 t = rose_node;
499 rose_node = rose_node->next;
500
501 for (i = 0; i < t->count; i++) {
502 if (t->neighbour[i] != s)
503 continue;
504
505 t->count--;
506
507 switch (i) {
508 case 0:
509 t->neighbour[0] = t->neighbour[1];
510 case 1:
511 t->neighbour[1] = t->neighbour[2];
512 case 2:
513 break;
514 }
515 }
516
517 if (t->count <= 0)
518 rose_remove_node(t);
519 }
520
521 rose_remove_neigh(s);
522 }
523 spin_unlock_bh(&rose_neigh_list_lock);
524 spin_unlock_bh(&rose_node_list_lock);
525}
526
527#if 0 /* Currently unused */
528/*
529 * A device has been removed. Remove its links.
530 */
531void rose_route_device_down(struct net_device *dev)
532{
533 struct rose_route *s, *rose_route;
534
535 spin_lock_bh(&rose_route_list_lock);
536 rose_route = rose_route_list;
537 while (rose_route != NULL) {
538 s = rose_route;
539 rose_route = rose_route->next;
540
541 if (s->neigh1->dev == dev || s->neigh2->dev == dev)
542 rose_remove_route(s);
543 }
544 spin_unlock_bh(&rose_route_list_lock);
545}
546#endif
547
548/*
549 * Clear all nodes and neighbours out, except for neighbours with
550 * active connections going through them.
551 * Do not clear loopback neighbour and nodes.
552 */
553static int rose_clear_routes(void)
554{
555 struct rose_neigh *s, *rose_neigh;
556 struct rose_node *t, *rose_node;
557
558 spin_lock_bh(&rose_node_list_lock);
559 spin_lock_bh(&rose_neigh_list_lock);
560
561 rose_neigh = rose_neigh_list;
562 rose_node = rose_node_list;
563
564 while (rose_node != NULL) {
565 t = rose_node;
566 rose_node = rose_node->next;
567 if (!t->loopback)
568 rose_remove_node(t);
569 }
570
571 while (rose_neigh != NULL) {
572 s = rose_neigh;
573 rose_neigh = rose_neigh->next;
574
575 if (s->use == 0 && !s->loopback) {
576 s->count = 0;
577 rose_remove_neigh(s);
578 }
579 }
580
581 spin_unlock_bh(&rose_neigh_list_lock);
582 spin_unlock_bh(&rose_node_list_lock);
583
584 return 0;
585}
586
587/*
588 * Check that the device given is a valid AX.25 interface that is "up".
589 */
590static struct net_device *rose_ax25_dev_get(char *devname)
591{
592 struct net_device *dev;
593
594 if ((dev = dev_get_by_name(devname)) == NULL)
595 return NULL;
596
597 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
598 return dev;
599
600 dev_put(dev);
601 return NULL;
602}
603
604/*
605 * Find the first active ROSE device, usually "rose0".
606 */
607struct net_device *rose_dev_first(void)
608{
609 struct net_device *dev, *first = NULL;
610
611 read_lock(&dev_base_lock);
612 for (dev = dev_base; dev != NULL; dev = dev->next) {
613 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
614 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
615 first = dev;
616 }
617 read_unlock(&dev_base_lock);
618
619 return first;
620}
621
622/*
623 * Find the ROSE device for the given address.
624 */
625struct net_device *rose_dev_get(rose_address *addr)
626{
627 struct net_device *dev;
628
629 read_lock(&dev_base_lock);
630 for (dev = dev_base; dev != NULL; dev = dev->next) {
631 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
632 dev_hold(dev);
633 goto out;
634 }
635 }
636out:
637 read_unlock(&dev_base_lock);
638 return dev;
639}
640
641static int rose_dev_exists(rose_address *addr)
642{
643 struct net_device *dev;
644
645 read_lock(&dev_base_lock);
646 for (dev = dev_base; dev != NULL; dev = dev->next) {
647 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
648 goto out;
649 }
650out:
651 read_unlock(&dev_base_lock);
652 return dev != NULL;
653}
654
655
656
657
658struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
659{
660 struct rose_route *rose_route;
661
662 for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
663 if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
664 (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
665 return rose_route;
666
667 return NULL;
668}
669
670/*
671 * Find a neighbour given a ROSE address.
672 */
673struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
674 unsigned char *diagnostic)
675{
676 struct rose_neigh *res = NULL;
677 struct rose_node *node;
678 int failed = 0;
679 int i;
680
681 spin_lock_bh(&rose_node_list_lock);
682 for (node = rose_node_list; node != NULL; node = node->next) {
683 if (rosecmpm(addr, &node->address, node->mask) == 0) {
684 for (i = 0; i < node->count; i++) {
685 if (!rose_ftimer_running(node->neighbour[i])) {
686 res = node->neighbour[i];
687 goto out;
688 } else
689 failed = 1;
690 }
691 break;
692 }
693 }
694
695 if (failed) {
696 *cause = ROSE_OUT_OF_ORDER;
697 *diagnostic = 0;
698 } else {
699 *cause = ROSE_NOT_OBTAINABLE;
700 *diagnostic = 0;
701 }
702
703out:
704 spin_unlock_bh(&rose_node_list_lock);
705
706 return res;
707}
708
709/*
710 * Handle the ioctls that control the routing functions.
711 */
712int rose_rt_ioctl(unsigned int cmd, void __user *arg)
713{
714 struct rose_route_struct rose_route;
715 struct net_device *dev;
716 int err;
717
718 switch (cmd) {
719 case SIOCADDRT:
720 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
721 return -EFAULT;
722 if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
723 return -EINVAL;
724 if (rose_dev_exists(&rose_route.address)) { /* Can't add routes to ourself */
725 dev_put(dev);
726 return -EINVAL;
727 }
728 if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
729 return -EINVAL;
730 if (rose_route.ndigis > 8) /* No more than 8 digipeats */
731 return -EINVAL;
732 err = rose_add_node(&rose_route, dev);
733 dev_put(dev);
734 return err;
735
736 case SIOCDELRT:
737 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
738 return -EFAULT;
739 if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
740 return -EINVAL;
741 err = rose_del_node(&rose_route, dev);
742 dev_put(dev);
743 return err;
744
745 case SIOCRSCLRRT:
746 return rose_clear_routes();
747
748 default:
749 return -EINVAL;
750 }
751
752 return 0;
753}
754
755static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
756{
757 struct rose_route *rose_route, *s;
758
759 rose_neigh->restarted = 0;
760
761 rose_stop_t0timer(rose_neigh);
762 rose_start_ftimer(rose_neigh);
763
764 skb_queue_purge(&rose_neigh->queue);
765
766 spin_lock_bh(&rose_route_list_lock);
767
768 rose_route = rose_route_list;
769
770 while (rose_route != NULL) {
771 if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
772 (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
773 (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
774 s = rose_route->next;
775 rose_remove_route(rose_route);
776 rose_route = s;
777 continue;
778 }
779
780 if (rose_route->neigh1 == rose_neigh) {
781 rose_route->neigh1->use--;
782 rose_route->neigh1 = NULL;
783 rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
784 }
785
786 if (rose_route->neigh2 == rose_neigh) {
787 rose_route->neigh2->use--;
788 rose_route->neigh2 = NULL;
789 rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
790 }
791
792 rose_route = rose_route->next;
793 }
794 spin_unlock_bh(&rose_route_list_lock);
795}
796
797/*
798 * A level 2 link has timed out, therefore it appears to be a poor link,
799 * then don't use that neighbour until it is reset. Blow away all through
800 * routes and connections using this route.
801 */
802void rose_link_failed(ax25_cb *ax25, int reason)
803{
804 struct rose_neigh *rose_neigh;
805
806 spin_lock_bh(&rose_neigh_list_lock);
807 rose_neigh = rose_neigh_list;
808 while (rose_neigh != NULL) {
809 if (rose_neigh->ax25 == ax25)
810 break;
811 rose_neigh = rose_neigh->next;
812 }
813
814 if (rose_neigh != NULL) {
815 rose_neigh->ax25 = NULL;
816
817 rose_del_route_by_neigh(rose_neigh);
818 rose_kill_by_neigh(rose_neigh);
819 }
820 spin_unlock_bh(&rose_neigh_list_lock);
821}
822
823/*
824 * A device has been "downed" remove its link status. Blow away all
825 * through routes and connections that use this device.
826 */
827void rose_link_device_down(struct net_device *dev)
828{
829 struct rose_neigh *rose_neigh;
830
831 for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
832 if (rose_neigh->dev == dev) {
833 rose_del_route_by_neigh(rose_neigh);
834 rose_kill_by_neigh(rose_neigh);
835 }
836 }
837}
838
839/*
840 * Route a frame to an appropriate AX.25 connection.
841 */
842int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
843{
844 struct rose_neigh *rose_neigh, *new_neigh;
845 struct rose_route *rose_route;
846 struct rose_facilities_struct facilities;
847 rose_address *src_addr, *dest_addr;
848 struct sock *sk;
849 unsigned short frametype;
850 unsigned int lci, new_lci;
851 unsigned char cause, diagnostic;
852 struct net_device *dev;
853 int len, res = 0;
854
855#if 0
856 if (call_in_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT)
857 return res;
858#endif
859
860 frametype = skb->data[2];
861 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
862 src_addr = (rose_address *)(skb->data + 9);
863 dest_addr = (rose_address *)(skb->data + 4);
864
865 spin_lock_bh(&rose_node_list_lock);
866 spin_lock_bh(&rose_neigh_list_lock);
867 spin_lock_bh(&rose_route_list_lock);
868
869 rose_neigh = rose_neigh_list;
870 while (rose_neigh != NULL) {
871 if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
872 ax25->ax25_dev->dev == rose_neigh->dev)
873 break;
874 rose_neigh = rose_neigh->next;
875 }
876
877 if (rose_neigh == NULL) {
878 printk("rose_route : unknown neighbour or device %s\n",
879 ax2asc(&ax25->dest_addr));
880 goto out;
881 }
882
883 /*
884 * Obviously the link is working, halt the ftimer.
885 */
886 rose_stop_ftimer(rose_neigh);
887
888 /*
889 * LCI of zero is always for us, and its always a restart
890 * frame.
891 */
892 if (lci == 0) {
893 rose_link_rx_restart(skb, rose_neigh, frametype);
894 goto out;
895 }
896
897 /*
898 * Find an existing socket.
899 */
900 if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
901 if (frametype == ROSE_CALL_REQUEST) {
902 struct rose_sock *rose = rose_sk(sk);
903
904 /* Remove an existing unused socket */
905 rose_clear_queues(sk);
906 rose->cause = ROSE_NETWORK_CONGESTION;
907 rose->diagnostic = 0;
908 rose->neighbour->use--;
909 rose->neighbour = NULL;
910 rose->lci = 0;
911 rose->state = ROSE_STATE_0;
912 sk->sk_state = TCP_CLOSE;
913 sk->sk_err = 0;
914 sk->sk_shutdown |= SEND_SHUTDOWN;
915 if (!sock_flag(sk, SOCK_DEAD)) {
916 sk->sk_state_change(sk);
917 sock_set_flag(sk, SOCK_DEAD);
918 }
919 }
920 else {
921 skb->h.raw = skb->data;
922 res = rose_process_rx_frame(sk, skb);
923 goto out;
924 }
925 }
926
927 /*
928 * Is is a Call Request and is it for us ?
929 */
930 if (frametype == ROSE_CALL_REQUEST)
931 if ((dev = rose_dev_get(dest_addr)) != NULL) {
932 res = rose_rx_call_request(skb, dev, rose_neigh, lci);
933 dev_put(dev);
934 goto out;
935 }
936
937 if (!sysctl_rose_routing_control) {
938 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
939 goto out;
940 }
941
942 /*
943 * Route it to the next in line if we have an entry for it.
944 */
945 rose_route = rose_route_list;
946 while (rose_route != NULL) {
947 if (rose_route->lci1 == lci &&
948 rose_route->neigh1 == rose_neigh) {
949 if (frametype == ROSE_CALL_REQUEST) {
950 /* F6FBB - Remove an existing unused route */
951 rose_remove_route(rose_route);
952 break;
953 } else if (rose_route->neigh2 != NULL) {
954 skb->data[0] &= 0xF0;
955 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
956 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
957 rose_transmit_link(skb, rose_route->neigh2);
958 if (frametype == ROSE_CLEAR_CONFIRMATION)
959 rose_remove_route(rose_route);
960 res = 1;
961 goto out;
962 } else {
963 if (frametype == ROSE_CLEAR_CONFIRMATION)
964 rose_remove_route(rose_route);
965 goto out;
966 }
967 }
968 if (rose_route->lci2 == lci &&
969 rose_route->neigh2 == rose_neigh) {
970 if (frametype == ROSE_CALL_REQUEST) {
971 /* F6FBB - Remove an existing unused route */
972 rose_remove_route(rose_route);
973 break;
974 } else if (rose_route->neigh1 != NULL) {
975 skb->data[0] &= 0xF0;
976 skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
977 skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
978 rose_transmit_link(skb, rose_route->neigh1);
979 if (frametype == ROSE_CLEAR_CONFIRMATION)
980 rose_remove_route(rose_route);
981 res = 1;
982 goto out;
983 } else {
984 if (frametype == ROSE_CLEAR_CONFIRMATION)
985 rose_remove_route(rose_route);
986 goto out;
987 }
988 }
989 rose_route = rose_route->next;
990 }
991
992 /*
993 * We know that:
994 * 1. The frame isn't for us,
995 * 2. It isn't "owned" by any existing route.
996 */
997 if (frametype != ROSE_CALL_REQUEST) /* XXX */
998 return 0;
999
1000 len = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
1001 len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
1002
1003 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1004
1005 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
1006 rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1007 goto out;
1008 }
1009
1010 /*
1011 * Check for routing loops.
1012 */
1013 rose_route = rose_route_list;
1014 while (rose_route != NULL) {
1015 if (rose_route->rand == facilities.rand &&
1016 rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1017 ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1018 ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1019 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1020 goto out;
1021 }
1022 rose_route = rose_route->next;
1023 }
1024
1025 if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic)) == NULL) {
1026 rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1027 goto out;
1028 }
1029
1030 if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1031 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1032 goto out;
1033 }
1034
1035 if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1036 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1037 goto out;
1038 }
1039
1040 rose_route->lci1 = lci;
1041 rose_route->src_addr = *src_addr;
1042 rose_route->dest_addr = *dest_addr;
1043 rose_route->src_call = facilities.dest_call;
1044 rose_route->dest_call = facilities.source_call;
1045 rose_route->rand = facilities.rand;
1046 rose_route->neigh1 = rose_neigh;
1047 rose_route->lci2 = new_lci;
1048 rose_route->neigh2 = new_neigh;
1049
1050 rose_route->neigh1->use++;
1051 rose_route->neigh2->use++;
1052
1053 rose_route->next = rose_route_list;
1054 rose_route_list = rose_route;
1055
1056 skb->data[0] &= 0xF0;
1057 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1058 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
1059
1060 rose_transmit_link(skb, rose_route->neigh2);
1061 res = 1;
1062
1063out:
1064 spin_unlock_bh(&rose_route_list_lock);
1065 spin_unlock_bh(&rose_neigh_list_lock);
1066 spin_unlock_bh(&rose_node_list_lock);
1067
1068 return res;
1069}
1070
1071#ifdef CONFIG_PROC_FS
1072
1073static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1074{
1075 struct rose_node *rose_node;
1076 int i = 1;
1077
1078 spin_lock_bh(&rose_neigh_list_lock);
1079 if (*pos == 0)
1080 return SEQ_START_TOKEN;
1081
1082 for (rose_node = rose_node_list; rose_node && i < *pos;
1083 rose_node = rose_node->next, ++i);
1084
1085 return (i == *pos) ? rose_node : NULL;
1086}
1087
1088static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1089{
1090 ++*pos;
1091
1092 return (v == SEQ_START_TOKEN) ? rose_node_list
1093 : ((struct rose_node *)v)->next;
1094}
1095
1096static void rose_node_stop(struct seq_file *seq, void *v)
1097{
1098 spin_unlock_bh(&rose_neigh_list_lock);
1099}
1100
1101static int rose_node_show(struct seq_file *seq, void *v)
1102{
1103 int i;
1104
1105 if (v == SEQ_START_TOKEN)
1106 seq_puts(seq, "address mask n neigh neigh neigh\n");
1107 else {
1108 const struct rose_node *rose_node = v;
1109 /* if (rose_node->loopback) {
1110 seq_printf(seq, "%-10s %04d 1 loopback\n",
1111 rose2asc(&rose_node->address),
1112 rose_node->mask);
1113 } else { */
1114 seq_printf(seq, "%-10s %04d %d",
1115 rose2asc(&rose_node->address),
1116 rose_node->mask,
1117 rose_node->count);
1118
1119 for (i = 0; i < rose_node->count; i++)
1120 seq_printf(seq, " %05d",
1121 rose_node->neighbour[i]->number);
1122
1123 seq_puts(seq, "\n");
1124 /* } */
1125 }
1126 return 0;
1127}
1128
1129static struct seq_operations rose_node_seqops = {
1130 .start = rose_node_start,
1131 .next = rose_node_next,
1132 .stop = rose_node_stop,
1133 .show = rose_node_show,
1134};
1135
1136static int rose_nodes_open(struct inode *inode, struct file *file)
1137{
1138 return seq_open(file, &rose_node_seqops);
1139}
1140
1141struct file_operations rose_nodes_fops = {
1142 .owner = THIS_MODULE,
1143 .open = rose_nodes_open,
1144 .read = seq_read,
1145 .llseek = seq_lseek,
1146 .release = seq_release,
1147};
1148
1149static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1150{
1151 struct rose_neigh *rose_neigh;
1152 int i = 1;
1153
1154 spin_lock_bh(&rose_neigh_list_lock);
1155 if (*pos == 0)
1156 return SEQ_START_TOKEN;
1157
1158 for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1159 rose_neigh = rose_neigh->next, ++i);
1160
1161 return (i == *pos) ? rose_neigh : NULL;
1162}
1163
1164static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1165{
1166 ++*pos;
1167
1168 return (v == SEQ_START_TOKEN) ? rose_neigh_list
1169 : ((struct rose_neigh *)v)->next;
1170}
1171
1172static void rose_neigh_stop(struct seq_file *seq, void *v)
1173{
1174 spin_unlock_bh(&rose_neigh_list_lock);
1175}
1176
1177static int rose_neigh_show(struct seq_file *seq, void *v)
1178{
1179 int i;
1180
1181 if (v == SEQ_START_TOKEN)
1182 seq_puts(seq,
1183 "addr callsign dev count use mode restart t0 tf digipeaters\n");
1184 else {
1185 struct rose_neigh *rose_neigh = v;
1186
1187 /* if (!rose_neigh->loopback) { */
1188 seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
1189 rose_neigh->number,
1190 (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(&rose_neigh->callsign),
1191 rose_neigh->dev ? rose_neigh->dev->name : "???",
1192 rose_neigh->count,
1193 rose_neigh->use,
1194 (rose_neigh->dce_mode) ? "DCE" : "DTE",
1195 (rose_neigh->restarted) ? "yes" : "no",
1196 ax25_display_timer(&rose_neigh->t0timer) / HZ,
1197 ax25_display_timer(&rose_neigh->ftimer) / HZ);
1198
1199 if (rose_neigh->digipeat != NULL) {
1200 for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1201 seq_printf(seq, " %s", ax2asc(&rose_neigh->digipeat->calls[i]));
1202 }
1203
1204 seq_puts(seq, "\n");
1205 }
1206 return 0;
1207}
1208
1209
1210static struct seq_operations rose_neigh_seqops = {
1211 .start = rose_neigh_start,
1212 .next = rose_neigh_next,
1213 .stop = rose_neigh_stop,
1214 .show = rose_neigh_show,
1215};
1216
1217static int rose_neigh_open(struct inode *inode, struct file *file)
1218{
1219 return seq_open(file, &rose_neigh_seqops);
1220}
1221
1222struct file_operations rose_neigh_fops = {
1223 .owner = THIS_MODULE,
1224 .open = rose_neigh_open,
1225 .read = seq_read,
1226 .llseek = seq_lseek,
1227 .release = seq_release,
1228};
1229
1230
1231static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1232{
1233 struct rose_route *rose_route;
1234 int i = 1;
1235
1236 spin_lock_bh(&rose_route_list_lock);
1237 if (*pos == 0)
1238 return SEQ_START_TOKEN;
1239
1240 for (rose_route = rose_route_list; rose_route && i < *pos;
1241 rose_route = rose_route->next, ++i);
1242
1243 return (i == *pos) ? rose_route : NULL;
1244}
1245
1246static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1247{
1248 ++*pos;
1249
1250 return (v == SEQ_START_TOKEN) ? rose_route_list
1251 : ((struct rose_route *)v)->next;
1252}
1253
1254static void rose_route_stop(struct seq_file *seq, void *v)
1255{
1256 spin_unlock_bh(&rose_route_list_lock);
1257}
1258
1259static int rose_route_show(struct seq_file *seq, void *v)
1260{
1261 if (v == SEQ_START_TOKEN)
1262 seq_puts(seq,
1263 "lci address callsign neigh <-> lci address callsign neigh\n");
1264 else {
1265 struct rose_route *rose_route = v;
1266
1267 if (rose_route->neigh1)
1268 seq_printf(seq,
1269 "%3.3X %-10s %-9s %05d ",
1270 rose_route->lci1,
1271 rose2asc(&rose_route->src_addr),
1272 ax2asc(&rose_route->src_call),
1273 rose_route->neigh1->number);
1274 else
1275 seq_puts(seq,
1276 "000 * * 00000 ");
1277
1278 if (rose_route->neigh2)
1279 seq_printf(seq,
1280 "%3.3X %-10s %-9s %05d\n",
1281 rose_route->lci2,
1282 rose2asc(&rose_route->dest_addr),
1283 ax2asc(&rose_route->dest_call),
1284 rose_route->neigh2->number);
1285 else
1286 seq_puts(seq,
1287 "000 * * 00000\n");
1288 }
1289 return 0;
1290}
1291
1292static struct seq_operations rose_route_seqops = {
1293 .start = rose_route_start,
1294 .next = rose_route_next,
1295 .stop = rose_route_stop,
1296 .show = rose_route_show,
1297};
1298
1299static int rose_route_open(struct inode *inode, struct file *file)
1300{
1301 return seq_open(file, &rose_route_seqops);
1302}
1303
1304struct file_operations rose_routes_fops = {
1305 .owner = THIS_MODULE,
1306 .open = rose_route_open,
1307 .read = seq_read,
1308 .llseek = seq_lseek,
1309 .release = seq_release,
1310};
1311
1312#endif /* CONFIG_PROC_FS */
1313
1314/*
1315 * Release all memory associated with ROSE routing structures.
1316 */
1317void __exit rose_rt_free(void)
1318{
1319 struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1320 struct rose_node *t, *rose_node = rose_node_list;
1321 struct rose_route *u, *rose_route = rose_route_list;
1322
1323 while (rose_neigh != NULL) {
1324 s = rose_neigh;
1325 rose_neigh = rose_neigh->next;
1326
1327 rose_remove_neigh(s);
1328 }
1329
1330 while (rose_node != NULL) {
1331 t = rose_node;
1332 rose_node = rose_node->next;
1333
1334 rose_remove_node(t);
1335 }
1336
1337 while (rose_route != NULL) {
1338 u = rose_route;
1339 rose_route = rose_route->next;
1340
1341 rose_remove_route(u);
1342 }
1343}
diff --git a/net/rose/rose_subr.c b/net/rose/rose_subr.c
new file mode 100644
index 000000000000..7db7e1cedc3a
--- /dev/null
+++ b/net/rose/rose_subr.c
@@ -0,0 +1,519 @@
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 */
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/system.h>
26#include <linux/fcntl.h>
27#include <linux/mm.h>
28#include <linux/interrupt.h>
29#include <net/rose.h>
30
31static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose);
32
33/*
34 * This routine purges all of the queues of frames.
35 */
36void rose_clear_queues(struct sock *sk)
37{
38 skb_queue_purge(&sk->sk_write_queue);
39 skb_queue_purge(&rose_sk(sk)->ack_queue);
40}
41
42/*
43 * This routine purges the input queue of those frames that have been
44 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
45 * SDL diagram.
46 */
47void rose_frames_acked(struct sock *sk, unsigned short nr)
48{
49 struct sk_buff *skb;
50 struct rose_sock *rose = rose_sk(sk);
51
52 /*
53 * Remove all the ack-ed frames from the ack queue.
54 */
55 if (rose->va != nr) {
56 while (skb_peek(&rose->ack_queue) != NULL && rose->va != nr) {
57 skb = skb_dequeue(&rose->ack_queue);
58 kfree_skb(skb);
59 rose->va = (rose->va + 1) % ROSE_MODULUS;
60 }
61 }
62}
63
64void rose_requeue_frames(struct sock *sk)
65{
66 struct sk_buff *skb, *skb_prev = NULL;
67
68 /*
69 * Requeue all the un-ack-ed frames on the output queue to be picked
70 * up by rose_kick. This arrangement handles the possibility of an
71 * empty output queue.
72 */
73 while ((skb = skb_dequeue(&rose_sk(sk)->ack_queue)) != NULL) {
74 if (skb_prev == NULL)
75 skb_queue_head(&sk->sk_write_queue, skb);
76 else
77 skb_append(skb_prev, skb);
78 skb_prev = skb;
79 }
80}
81
82/*
83 * Validate that the value of nr is between va and vs. Return true or
84 * false for testing.
85 */
86int rose_validate_nr(struct sock *sk, unsigned short nr)
87{
88 struct rose_sock *rose = rose_sk(sk);
89 unsigned short vc = rose->va;
90
91 while (vc != rose->vs) {
92 if (nr == vc) return 1;
93 vc = (vc + 1) % ROSE_MODULUS;
94 }
95
96 return nr == rose->vs;
97}
98
99/*
100 * This routine is called when the packet layer internally generates a
101 * control frame.
102 */
103void rose_write_internal(struct sock *sk, int frametype)
104{
105 struct rose_sock *rose = rose_sk(sk);
106 struct sk_buff *skb;
107 unsigned char *dptr;
108 unsigned char lci1, lci2;
109 char buffer[100];
110 int len, faclen = 0;
111
112 len = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN + 1;
113
114 switch (frametype) {
115 case ROSE_CALL_REQUEST:
116 len += 1 + ROSE_ADDR_LEN + ROSE_ADDR_LEN;
117 faclen = rose_create_facilities(buffer, rose);
118 len += faclen;
119 break;
120 case ROSE_CALL_ACCEPTED:
121 case ROSE_CLEAR_REQUEST:
122 case ROSE_RESET_REQUEST:
123 len += 2;
124 break;
125 }
126
127 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
128 return;
129
130 /*
131 * Space for AX.25 header and PID.
132 */
133 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + 1);
134
135 dptr = skb_put(skb, skb_tailroom(skb));
136
137 lci1 = (rose->lci >> 8) & 0x0F;
138 lci2 = (rose->lci >> 0) & 0xFF;
139
140 switch (frametype) {
141 case ROSE_CALL_REQUEST:
142 *dptr++ = ROSE_GFI | lci1;
143 *dptr++ = lci2;
144 *dptr++ = frametype;
145 *dptr++ = 0xAA;
146 memcpy(dptr, &rose->dest_addr, ROSE_ADDR_LEN);
147 dptr += ROSE_ADDR_LEN;
148 memcpy(dptr, &rose->source_addr, ROSE_ADDR_LEN);
149 dptr += ROSE_ADDR_LEN;
150 memcpy(dptr, buffer, faclen);
151 dptr += faclen;
152 break;
153
154 case ROSE_CALL_ACCEPTED:
155 *dptr++ = ROSE_GFI | lci1;
156 *dptr++ = lci2;
157 *dptr++ = frametype;
158 *dptr++ = 0x00; /* Address length */
159 *dptr++ = 0; /* Facilities length */
160 break;
161
162 case ROSE_CLEAR_REQUEST:
163 *dptr++ = ROSE_GFI | lci1;
164 *dptr++ = lci2;
165 *dptr++ = frametype;
166 *dptr++ = rose->cause;
167 *dptr++ = rose->diagnostic;
168 break;
169
170 case ROSE_RESET_REQUEST:
171 *dptr++ = ROSE_GFI | lci1;
172 *dptr++ = lci2;
173 *dptr++ = frametype;
174 *dptr++ = ROSE_DTE_ORIGINATED;
175 *dptr++ = 0;
176 break;
177
178 case ROSE_RR:
179 case ROSE_RNR:
180 *dptr++ = ROSE_GFI | lci1;
181 *dptr++ = lci2;
182 *dptr = frametype;
183 *dptr++ |= (rose->vr << 5) & 0xE0;
184 break;
185
186 case ROSE_CLEAR_CONFIRMATION:
187 case ROSE_RESET_CONFIRMATION:
188 *dptr++ = ROSE_GFI | lci1;
189 *dptr++ = lci2;
190 *dptr++ = frametype;
191 break;
192
193 default:
194 printk(KERN_ERR "ROSE: rose_write_internal - invalid frametype %02X\n", frametype);
195 kfree_skb(skb);
196 return;
197 }
198
199 rose_transmit_link(skb, rose->neighbour);
200}
201
202int rose_decode(struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m)
203{
204 unsigned char *frame;
205
206 frame = skb->data;
207
208 *ns = *nr = *q = *d = *m = 0;
209
210 switch (frame[2]) {
211 case ROSE_CALL_REQUEST:
212 case ROSE_CALL_ACCEPTED:
213 case ROSE_CLEAR_REQUEST:
214 case ROSE_CLEAR_CONFIRMATION:
215 case ROSE_RESET_REQUEST:
216 case ROSE_RESET_CONFIRMATION:
217 return frame[2];
218 default:
219 break;
220 }
221
222 if ((frame[2] & 0x1F) == ROSE_RR ||
223 (frame[2] & 0x1F) == ROSE_RNR) {
224 *nr = (frame[2] >> 5) & 0x07;
225 return frame[2] & 0x1F;
226 }
227
228 if ((frame[2] & 0x01) == ROSE_DATA) {
229 *q = (frame[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
230 *d = (frame[0] & ROSE_D_BIT) == ROSE_D_BIT;
231 *m = (frame[2] & ROSE_M_BIT) == ROSE_M_BIT;
232 *nr = (frame[2] >> 5) & 0x07;
233 *ns = (frame[2] >> 1) & 0x07;
234 return ROSE_DATA;
235 }
236
237 return ROSE_ILLEGAL;
238}
239
240static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *facilities, int len)
241{
242 unsigned char *pt;
243 unsigned char l, lg, n = 0;
244 int fac_national_digis_received = 0;
245
246 do {
247 switch (*p & 0xC0) {
248 case 0x00:
249 p += 2;
250 n += 2;
251 len -= 2;
252 break;
253
254 case 0x40:
255 if (*p == FAC_NATIONAL_RAND)
256 facilities->rand = ((p[1] << 8) & 0xFF00) + ((p[2] << 0) & 0x00FF);
257 p += 3;
258 n += 3;
259 len -= 3;
260 break;
261
262 case 0x80:
263 p += 4;
264 n += 4;
265 len -= 4;
266 break;
267
268 case 0xC0:
269 l = p[1];
270 if (*p == FAC_NATIONAL_DEST_DIGI) {
271 if (!fac_national_digis_received) {
272 memcpy(&facilities->source_digis[0], p + 2, AX25_ADDR_LEN);
273 facilities->source_ndigis = 1;
274 }
275 }
276 else if (*p == FAC_NATIONAL_SRC_DIGI) {
277 if (!fac_national_digis_received) {
278 memcpy(&facilities->dest_digis[0], p + 2, AX25_ADDR_LEN);
279 facilities->dest_ndigis = 1;
280 }
281 }
282 else if (*p == FAC_NATIONAL_FAIL_CALL) {
283 memcpy(&facilities->fail_call, p + 2, AX25_ADDR_LEN);
284 }
285 else if (*p == FAC_NATIONAL_FAIL_ADD) {
286 memcpy(&facilities->fail_addr, p + 3, ROSE_ADDR_LEN);
287 }
288 else if (*p == FAC_NATIONAL_DIGIS) {
289 fac_national_digis_received = 1;
290 facilities->source_ndigis = 0;
291 facilities->dest_ndigis = 0;
292 for (pt = p + 2, lg = 0 ; lg < l ; pt += AX25_ADDR_LEN, lg += AX25_ADDR_LEN) {
293 if (pt[6] & AX25_HBIT)
294 memcpy(&facilities->dest_digis[facilities->dest_ndigis++], pt, AX25_ADDR_LEN);
295 else
296 memcpy(&facilities->source_digis[facilities->source_ndigis++], pt, AX25_ADDR_LEN);
297 }
298 }
299 p += l + 2;
300 n += l + 2;
301 len -= l + 2;
302 break;
303 }
304 } while (*p != 0x00 && len > 0);
305
306 return n;
307}
308
309static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *facilities, int len)
310{
311 unsigned char l, n = 0;
312 char callsign[11];
313
314 do {
315 switch (*p & 0xC0) {
316 case 0x00:
317 p += 2;
318 n += 2;
319 len -= 2;
320 break;
321
322 case 0x40:
323 p += 3;
324 n += 3;
325 len -= 3;
326 break;
327
328 case 0x80:
329 p += 4;
330 n += 4;
331 len -= 4;
332 break;
333
334 case 0xC0:
335 l = p[1];
336 if (*p == FAC_CCITT_DEST_NSAP) {
337 memcpy(&facilities->source_addr, p + 7, ROSE_ADDR_LEN);
338 memcpy(callsign, p + 12, l - 10);
339 callsign[l - 10] = '\0';
340 facilities->source_call = *asc2ax(callsign);
341 }
342 if (*p == FAC_CCITT_SRC_NSAP) {
343 memcpy(&facilities->dest_addr, p + 7, ROSE_ADDR_LEN);
344 memcpy(callsign, p + 12, l - 10);
345 callsign[l - 10] = '\0';
346 facilities->dest_call = *asc2ax(callsign);
347 }
348 p += l + 2;
349 n += l + 2;
350 len -= l + 2;
351 break;
352 }
353 } while (*p != 0x00 && len > 0);
354
355 return n;
356}
357
358int rose_parse_facilities(unsigned char *p,
359 struct rose_facilities_struct *facilities)
360{
361 int facilities_len, len;
362
363 facilities_len = *p++;
364
365 if (facilities_len == 0)
366 return 0;
367
368 while (facilities_len > 0) {
369 if (*p == 0x00) {
370 facilities_len--;
371 p++;
372
373 switch (*p) {
374 case FAC_NATIONAL: /* National */
375 len = rose_parse_national(p + 1, facilities, facilities_len - 1);
376 facilities_len -= len + 1;
377 p += len + 1;
378 break;
379
380 case FAC_CCITT: /* CCITT */
381 len = rose_parse_ccitt(p + 1, facilities, facilities_len - 1);
382 facilities_len -= len + 1;
383 p += len + 1;
384 break;
385
386 default:
387 printk(KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X\n", *p);
388 facilities_len--;
389 p++;
390 break;
391 }
392 } else
393 break; /* Error in facilities format */
394 }
395
396 return 1;
397}
398
399static int rose_create_facilities(unsigned char *buffer, struct rose_sock *rose)
400{
401 unsigned char *p = buffer + 1;
402 char *callsign;
403 int len, nb;
404
405 /* National Facilities */
406 if (rose->rand != 0 || rose->source_ndigis == 1 || rose->dest_ndigis == 1) {
407 *p++ = 0x00;
408 *p++ = FAC_NATIONAL;
409
410 if (rose->rand != 0) {
411 *p++ = FAC_NATIONAL_RAND;
412 *p++ = (rose->rand >> 8) & 0xFF;
413 *p++ = (rose->rand >> 0) & 0xFF;
414 }
415
416 /* Sent before older facilities */
417 if ((rose->source_ndigis > 0) || (rose->dest_ndigis > 0)) {
418 int maxdigi = 0;
419 *p++ = FAC_NATIONAL_DIGIS;
420 *p++ = AX25_ADDR_LEN * (rose->source_ndigis + rose->dest_ndigis);
421 for (nb = 0 ; nb < rose->source_ndigis ; nb++) {
422 if (++maxdigi >= ROSE_MAX_DIGIS)
423 break;
424 memcpy(p, &rose->source_digis[nb], AX25_ADDR_LEN);
425 p[6] |= AX25_HBIT;
426 p += AX25_ADDR_LEN;
427 }
428 for (nb = 0 ; nb < rose->dest_ndigis ; nb++) {
429 if (++maxdigi >= ROSE_MAX_DIGIS)
430 break;
431 memcpy(p, &rose->dest_digis[nb], AX25_ADDR_LEN);
432 p[6] &= ~AX25_HBIT;
433 p += AX25_ADDR_LEN;
434 }
435 }
436
437 /* For compatibility */
438 if (rose->source_ndigis > 0) {
439 *p++ = FAC_NATIONAL_SRC_DIGI;
440 *p++ = AX25_ADDR_LEN;
441 memcpy(p, &rose->source_digis[0], AX25_ADDR_LEN);
442 p += AX25_ADDR_LEN;
443 }
444
445 /* For compatibility */
446 if (rose->dest_ndigis > 0) {
447 *p++ = FAC_NATIONAL_DEST_DIGI;
448 *p++ = AX25_ADDR_LEN;
449 memcpy(p, &rose->dest_digis[0], AX25_ADDR_LEN);
450 p += AX25_ADDR_LEN;
451 }
452 }
453
454 *p++ = 0x00;
455 *p++ = FAC_CCITT;
456
457 *p++ = FAC_CCITT_DEST_NSAP;
458
459 callsign = ax2asc(&rose->dest_call);
460
461 *p++ = strlen(callsign) + 10;
462 *p++ = (strlen(callsign) + 9) * 2; /* ??? */
463
464 *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
465 *p++ = ROSE_ADDR_LEN * 2;
466 memcpy(p, &rose->dest_addr, ROSE_ADDR_LEN);
467 p += ROSE_ADDR_LEN;
468
469 memcpy(p, callsign, strlen(callsign));
470 p += strlen(callsign);
471
472 *p++ = FAC_CCITT_SRC_NSAP;
473
474 callsign = ax2asc(&rose->source_call);
475
476 *p++ = strlen(callsign) + 10;
477 *p++ = (strlen(callsign) + 9) * 2; /* ??? */
478
479 *p++ = 0x47; *p++ = 0x00; *p++ = 0x11;
480 *p++ = ROSE_ADDR_LEN * 2;
481 memcpy(p, &rose->source_addr, ROSE_ADDR_LEN);
482 p += ROSE_ADDR_LEN;
483
484 memcpy(p, callsign, strlen(callsign));
485 p += strlen(callsign);
486
487 len = p - buffer;
488 buffer[0] = len - 1;
489
490 return len;
491}
492
493void rose_disconnect(struct sock *sk, int reason, int cause, int diagnostic)
494{
495 struct rose_sock *rose = rose_sk(sk);
496
497 rose_stop_timer(sk);
498 rose_stop_idletimer(sk);
499
500 rose_clear_queues(sk);
501
502 rose->lci = 0;
503 rose->state = ROSE_STATE_0;
504
505 if (cause != -1)
506 rose->cause = cause;
507
508 if (diagnostic != -1)
509 rose->diagnostic = diagnostic;
510
511 sk->sk_state = TCP_CLOSE;
512 sk->sk_err = reason;
513 sk->sk_shutdown |= SEND_SHUTDOWN;
514
515 if (!sock_flag(sk, SOCK_DEAD)) {
516 sk->sk_state_change(sk);
517 sock_set_flag(sk, SOCK_DEAD);
518 }
519}
diff --git a/net/rose/rose_timer.c b/net/rose/rose_timer.c
new file mode 100644
index 000000000000..84dd4403f792
--- /dev/null
+++ b/net/rose/rose_timer.c
@@ -0,0 +1,216 @@
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/system.h>
27#include <linux/fcntl.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30#include <net/rose.h>
31
32static void rose_heartbeat_expiry(unsigned long);
33static void rose_timer_expiry(unsigned long);
34static void rose_idletimer_expiry(unsigned long);
35
36void rose_start_heartbeat(struct sock *sk)
37{
38 del_timer(&sk->sk_timer);
39
40 sk->sk_timer.data = (unsigned long)sk;
41 sk->sk_timer.function = &rose_heartbeat_expiry;
42 sk->sk_timer.expires = jiffies + 5 * HZ;
43
44 add_timer(&sk->sk_timer);
45}
46
47void rose_start_t1timer(struct sock *sk)
48{
49 struct rose_sock *rose = rose_sk(sk);
50
51 del_timer(&rose->timer);
52
53 rose->timer.data = (unsigned long)sk;
54 rose->timer.function = &rose_timer_expiry;
55 rose->timer.expires = jiffies + rose->t1;
56
57 add_timer(&rose->timer);
58}
59
60void rose_start_t2timer(struct sock *sk)
61{
62 struct rose_sock *rose = rose_sk(sk);
63
64 del_timer(&rose->timer);
65
66 rose->timer.data = (unsigned long)sk;
67 rose->timer.function = &rose_timer_expiry;
68 rose->timer.expires = jiffies + rose->t2;
69
70 add_timer(&rose->timer);
71}
72
73void rose_start_t3timer(struct sock *sk)
74{
75 struct rose_sock *rose = rose_sk(sk);
76
77 del_timer(&rose->timer);
78
79 rose->timer.data = (unsigned long)sk;
80 rose->timer.function = &rose_timer_expiry;
81 rose->timer.expires = jiffies + rose->t3;
82
83 add_timer(&rose->timer);
84}
85
86void rose_start_hbtimer(struct sock *sk)
87{
88 struct rose_sock *rose = rose_sk(sk);
89
90 del_timer(&rose->timer);
91
92 rose->timer.data = (unsigned long)sk;
93 rose->timer.function = &rose_timer_expiry;
94 rose->timer.expires = jiffies + rose->hb;
95
96 add_timer(&rose->timer);
97}
98
99void rose_start_idletimer(struct sock *sk)
100{
101 struct rose_sock *rose = rose_sk(sk);
102
103 del_timer(&rose->idletimer);
104
105 if (rose->idle > 0) {
106 rose->idletimer.data = (unsigned long)sk;
107 rose->idletimer.function = &rose_idletimer_expiry;
108 rose->idletimer.expires = jiffies + rose->idle;
109
110 add_timer(&rose->idletimer);
111 }
112}
113
114void rose_stop_heartbeat(struct sock *sk)
115{
116 del_timer(&sk->sk_timer);
117}
118
119void rose_stop_timer(struct sock *sk)
120{
121 del_timer(&rose_sk(sk)->timer);
122}
123
124void rose_stop_idletimer(struct sock *sk)
125{
126 del_timer(&rose_sk(sk)->idletimer);
127}
128
129static void rose_heartbeat_expiry(unsigned long param)
130{
131 struct sock *sk = (struct sock *)param;
132 struct rose_sock *rose = rose_sk(sk);
133
134 bh_lock_sock(sk);
135 switch (rose->state) {
136 case ROSE_STATE_0:
137 /* Magic here: If we listen() and a new link dies before it
138 is accepted() it isn't 'dead' so doesn't get removed. */
139 if (sock_flag(sk, SOCK_DESTROY) ||
140 (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
141 rose_destroy_socket(sk);
142 return;
143 }
144 break;
145
146 case ROSE_STATE_3:
147 /*
148 * Check for the state of the receive buffer.
149 */
150 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
151 (rose->condition & ROSE_COND_OWN_RX_BUSY)) {
152 rose->condition &= ~ROSE_COND_OWN_RX_BUSY;
153 rose->condition &= ~ROSE_COND_ACK_PENDING;
154 rose->vl = rose->vr;
155 rose_write_internal(sk, ROSE_RR);
156 rose_stop_timer(sk); /* HB */
157 break;
158 }
159 break;
160 }
161
162 rose_start_heartbeat(sk);
163 bh_unlock_sock(sk);
164}
165
166static void rose_timer_expiry(unsigned long param)
167{
168 struct sock *sk = (struct sock *)param;
169 struct rose_sock *rose = rose_sk(sk);
170
171 bh_lock_sock(sk);
172 switch (rose->state) {
173 case ROSE_STATE_1: /* T1 */
174 case ROSE_STATE_4: /* T2 */
175 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
176 rose->state = ROSE_STATE_2;
177 rose_start_t3timer(sk);
178 break;
179
180 case ROSE_STATE_2: /* T3 */
181 rose->neighbour->use--;
182 rose_disconnect(sk, ETIMEDOUT, -1, -1);
183 break;
184
185 case ROSE_STATE_3: /* HB */
186 if (rose->condition & ROSE_COND_ACK_PENDING) {
187 rose->condition &= ~ROSE_COND_ACK_PENDING;
188 rose_enquiry_response(sk);
189 }
190 break;
191 }
192 bh_unlock_sock(sk);
193}
194
195static void rose_idletimer_expiry(unsigned long param)
196{
197 struct sock *sk = (struct sock *)param;
198
199 bh_lock_sock(sk);
200 rose_clear_queues(sk);
201
202 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
203 rose_sk(sk)->state = ROSE_STATE_2;
204
205 rose_start_t3timer(sk);
206
207 sk->sk_state = TCP_CLOSE;
208 sk->sk_err = 0;
209 sk->sk_shutdown |= SEND_SHUTDOWN;
210
211 if (!sock_flag(sk, SOCK_DEAD)) {
212 sk->sk_state_change(sk);
213 sock_set_flag(sk, SOCK_DEAD);
214 }
215 bh_unlock_sock(sk);
216}
diff --git a/net/rose/sysctl_net_rose.c b/net/rose/sysctl_net_rose.c
new file mode 100644
index 000000000000..8548c7cf5643
--- /dev/null
+++ b/net/rose/sysctl_net_rose.c
@@ -0,0 +1,169 @@
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/rose.h>
14
15static int min_timer[] = {1 * HZ};
16static int max_timer[] = {300 * HZ};
17static int min_idle[] = {0 * HZ};
18static int max_idle[] = {65535 * HZ};
19static int min_route[1], max_route[] = {1};
20static int min_ftimer[] = {60 * HZ};
21static int max_ftimer[] = {600 * HZ};
22static int min_maxvcs[] = {1}, max_maxvcs[] = {254};
23static int min_window[] = {1}, max_window[] = {7};
24
25static struct ctl_table_header *rose_table_header;
26
27static ctl_table rose_table[] = {
28 {
29 .ctl_name = NET_ROSE_RESTART_REQUEST_TIMEOUT,
30 .procname = "restart_request_timeout",
31 .data = &sysctl_rose_restart_request_timeout,
32 .maxlen = sizeof(int),
33 .mode = 0644,
34 .proc_handler = &proc_dointvec_minmax,
35 .strategy = &sysctl_intvec,
36 .extra1 = &min_timer,
37 .extra2 = &max_timer
38 },
39 {
40 .ctl_name = NET_ROSE_CALL_REQUEST_TIMEOUT,
41 .procname = "call_request_timeout",
42 .data = &sysctl_rose_call_request_timeout,
43 .maxlen = sizeof(int),
44 .mode = 0644,
45 .proc_handler = &proc_dointvec_minmax,
46 .strategy = &sysctl_intvec,
47 .extra1 = &min_timer,
48 .extra2 = &max_timer
49 },
50 {
51 .ctl_name = NET_ROSE_RESET_REQUEST_TIMEOUT,
52 .procname = "reset_request_timeout",
53 .data = &sysctl_rose_reset_request_timeout,
54 .maxlen = sizeof(int),
55 .mode = 0644,
56 .proc_handler = &proc_dointvec_minmax,
57 .strategy = &sysctl_intvec,
58 .extra1 = &min_timer,
59 .extra2 = &max_timer
60 },
61 {
62 .ctl_name = NET_ROSE_CLEAR_REQUEST_TIMEOUT,
63 .procname = "clear_request_timeout",
64 .data = &sysctl_rose_clear_request_timeout,
65 .maxlen = sizeof(int),
66 .mode = 0644,
67 .proc_handler = &proc_dointvec_minmax,
68 .strategy = &sysctl_intvec,
69 .extra1 = &min_timer,
70 .extra2 = &max_timer
71 },
72 {
73 .ctl_name = NET_ROSE_NO_ACTIVITY_TIMEOUT,
74 .procname = "no_activity_timeout",
75 .data = &sysctl_rose_no_activity_timeout,
76 .maxlen = sizeof(int),
77 .mode = 0644,
78 .proc_handler = &proc_dointvec_minmax,
79 .strategy = &sysctl_intvec,
80 .extra1 = &min_idle,
81 .extra2 = &max_idle
82 },
83 {
84 .ctl_name = NET_ROSE_ACK_HOLD_BACK_TIMEOUT,
85 .procname = "acknowledge_hold_back_timeout",
86 .data = &sysctl_rose_ack_hold_back_timeout,
87 .maxlen = sizeof(int),
88 .mode = 0644,
89 .proc_handler = &proc_dointvec_minmax,
90 .strategy = &sysctl_intvec,
91 .extra1 = &min_timer,
92 .extra2 = &max_timer
93 },
94 {
95 .ctl_name = NET_ROSE_ROUTING_CONTROL,
96 .procname = "routing_control",
97 .data = &sysctl_rose_routing_control,
98 .maxlen = sizeof(int),
99 .mode = 0644,
100 .proc_handler = &proc_dointvec_minmax,
101 .strategy = &sysctl_intvec,
102 .extra1 = &min_route,
103 .extra2 = &max_route
104 },
105 {
106 .ctl_name = NET_ROSE_LINK_FAIL_TIMEOUT,
107 .procname = "link_fail_timeout",
108 .data = &sysctl_rose_link_fail_timeout,
109 .maxlen = sizeof(int),
110 .mode = 0644,
111 .proc_handler = &proc_dointvec_minmax,
112 .strategy = &sysctl_intvec,
113 .extra1 = &min_ftimer,
114 .extra2 = &max_ftimer
115 },
116 {
117 .ctl_name = NET_ROSE_MAX_VCS,
118 .procname = "maximum_virtual_circuits",
119 .data = &sysctl_rose_maximum_vcs,
120 .maxlen = sizeof(int),
121 .mode = 0644,
122 .proc_handler = &proc_dointvec_minmax,
123 .strategy = &sysctl_intvec,
124 .extra1 = &min_maxvcs,
125 .extra2 = &max_maxvcs
126 },
127 {
128 .ctl_name = NET_ROSE_WINDOW_SIZE,
129 .procname = "window_size",
130 .data = &sysctl_rose_window_size,
131 .maxlen = sizeof(int),
132 .mode = 0644,
133 .proc_handler = &proc_dointvec_minmax,
134 .strategy = &sysctl_intvec,
135 .extra1 = &min_window,
136 .extra2 = &max_window
137 },
138 { .ctl_name = 0 }
139};
140
141static ctl_table rose_dir_table[] = {
142 {
143 .ctl_name = NET_ROSE,
144 .procname = "rose",
145 .mode = 0555,
146 .child = rose_table
147 },
148 { .ctl_name = 0 }
149};
150
151static ctl_table rose_root_table[] = {
152 {
153 .ctl_name = CTL_NET,
154 .procname = "net",
155 .mode = 0555,
156 .child = rose_dir_table
157 },
158 { .ctl_name = 0 }
159};
160
161void __init rose_register_sysctl(void)
162{
163 rose_table_header = register_sysctl_table(rose_root_table, 1);
164}
165
166void rose_unregister_sysctl(void)
167{
168 unregister_sysctl_table(rose_table_header);
169}