blob: ff73ebb912b8ebd0f6887bdb776caa25922d9b5a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * Copyright (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}