blob: a22542fa1bc848cedef82a914a49f40ededae771 [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>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070027#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Add a new route to a node, and in the process add the node and the
53 * neighbour if it is new.
54 */
55static int rose_add_node(struct rose_route_struct *rose_route,
56 struct net_device *dev)
57{
58 struct rose_node *rose_node, *rose_tmpn, *rose_tmpp;
59 struct rose_neigh *rose_neigh;
60 int i, res = 0;
61
62 spin_lock_bh(&rose_node_list_lock);
63 spin_lock_bh(&rose_neigh_list_lock);
64
65 rose_node = rose_node_list;
66 while (rose_node != NULL) {
67 if ((rose_node->mask == rose_route->mask) &&
68 (rosecmpm(&rose_route->address, &rose_node->address,
69 rose_route->mask) == 0))
70 break;
71 rose_node = rose_node->next;
72 }
73
74 if (rose_node != NULL && rose_node->loopback) {
75 res = -EINVAL;
76 goto out;
77 }
78
79 rose_neigh = rose_neigh_list;
80 while (rose_neigh != NULL) {
81 if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0
82 && rose_neigh->dev == dev)
83 break;
84 rose_neigh = rose_neigh->next;
85 }
86
87 if (rose_neigh == NULL) {
88 rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
89 if (rose_neigh == NULL) {
90 res = -ENOMEM;
91 goto out;
92 }
93
94 rose_neigh->callsign = rose_route->neighbour;
95 rose_neigh->digipeat = NULL;
96 rose_neigh->ax25 = NULL;
97 rose_neigh->dev = dev;
98 rose_neigh->count = 0;
99 rose_neigh->use = 0;
100 rose_neigh->dce_mode = 0;
101 rose_neigh->loopback = 0;
102 rose_neigh->number = rose_neigh_no++;
103 rose_neigh->restarted = 0;
104
105 skb_queue_head_init(&rose_neigh->queue);
106
107 init_timer(&rose_neigh->ftimer);
108 init_timer(&rose_neigh->t0timer);
109
110 if (rose_route->ndigis != 0) {
111 if ((rose_neigh->digipeat = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) {
112 kfree(rose_neigh);
113 res = -ENOMEM;
114 goto out;
115 }
116
117 rose_neigh->digipeat->ndigi = rose_route->ndigis;
118 rose_neigh->digipeat->lastrepeat = -1;
119
120 for (i = 0; i < rose_route->ndigis; i++) {
121 rose_neigh->digipeat->calls[i] =
122 rose_route->digipeaters[i];
123 rose_neigh->digipeat->repeated[i] = 0;
124 }
125 }
126
127 rose_neigh->next = rose_neigh_list;
128 rose_neigh_list = rose_neigh;
129 }
130
131 /*
132 * This is a new node to be inserted into the list. Find where it needs
133 * to be inserted into the list, and insert it. We want to be sure
134 * to order the list in descending order of mask size to ensure that
135 * later when we are searching this list the first match will be the
136 * best match.
137 */
138 if (rose_node == NULL) {
139 rose_tmpn = rose_node_list;
140 rose_tmpp = NULL;
141
142 while (rose_tmpn != NULL) {
143 if (rose_tmpn->mask > rose_route->mask) {
144 rose_tmpp = rose_tmpn;
145 rose_tmpn = rose_tmpn->next;
146 } else {
147 break;
148 }
149 }
150
151 /* create new node */
152 rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
153 if (rose_node == NULL) {
154 res = -ENOMEM;
155 goto out;
156 }
157
158 rose_node->address = rose_route->address;
159 rose_node->mask = rose_route->mask;
160 rose_node->count = 1;
161 rose_node->loopback = 0;
162 rose_node->neighbour[0] = rose_neigh;
163
164 if (rose_tmpn == NULL) {
165 if (rose_tmpp == NULL) { /* Empty list */
166 rose_node_list = rose_node;
167 rose_node->next = NULL;
168 } else {
169 rose_tmpp->next = rose_node;
170 rose_node->next = NULL;
171 }
172 } else {
173 if (rose_tmpp == NULL) { /* 1st node */
174 rose_node->next = rose_node_list;
175 rose_node_list = rose_node;
176 } else {
177 rose_tmpp->next = rose_node;
178 rose_node->next = rose_tmpn;
179 }
180 }
181 rose_neigh->count++;
182
183 goto out;
184 }
185
186 /* We have space, slot it in */
187 if (rose_node->count < 3) {
188 rose_node->neighbour[rose_node->count] = rose_neigh;
189 rose_node->count++;
190 rose_neigh->count++;
191 }
192
193out:
194 spin_unlock_bh(&rose_neigh_list_lock);
195 spin_unlock_bh(&rose_node_list_lock);
196
197 return res;
198}
199
200/*
201 * Caller is holding rose_node_list_lock.
202 */
203static void rose_remove_node(struct rose_node *rose_node)
204{
205 struct rose_node *s;
206
207 if ((s = rose_node_list) == rose_node) {
208 rose_node_list = rose_node->next;
209 kfree(rose_node);
210 return;
211 }
212
213 while (s != NULL && s->next != NULL) {
214 if (s->next == rose_node) {
215 s->next = rose_node->next;
216 kfree(rose_node);
217 return;
218 }
219
220 s = s->next;
221 }
222}
223
224/*
225 * Caller is holding rose_neigh_list_lock.
226 */
227static void rose_remove_neigh(struct rose_neigh *rose_neigh)
228{
229 struct rose_neigh *s;
230
231 rose_stop_ftimer(rose_neigh);
232 rose_stop_t0timer(rose_neigh);
233
234 skb_queue_purge(&rose_neigh->queue);
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if ((s = rose_neigh_list) == rose_neigh) {
237 rose_neigh_list = rose_neigh->next;
Jesper Juhla51482b2005-11-08 09:41:34 -0800238 kfree(rose_neigh->digipeat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 kfree(rose_neigh);
240 return;
241 }
242
243 while (s != NULL && s->next != NULL) {
244 if (s->next == rose_neigh) {
245 s->next = rose_neigh->next;
Jesper Juhla51482b2005-11-08 09:41:34 -0800246 kfree(rose_neigh->digipeat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 kfree(rose_neigh);
248 return;
249 }
250
251 s = s->next;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/*
256 * Caller is holding rose_route_list_lock.
257 */
258static void rose_remove_route(struct rose_route *rose_route)
259{
260 struct rose_route *s;
261
262 if (rose_route->neigh1 != NULL)
263 rose_route->neigh1->use--;
264
265 if (rose_route->neigh2 != NULL)
266 rose_route->neigh2->use--;
267
268 if ((s = rose_route_list) == rose_route) {
269 rose_route_list = rose_route->next;
270 kfree(rose_route);
271 return;
272 }
273
274 while (s != NULL && s->next != NULL) {
275 if (s->next == rose_route) {
276 s->next = rose_route->next;
277 kfree(rose_route);
278 return;
279 }
280
281 s = s->next;
282 }
283}
284
285/*
286 * "Delete" a node. Strictly speaking remove a route to a node. The node
287 * is only deleted if no routes are left to it.
288 */
289static int rose_del_node(struct rose_route_struct *rose_route,
290 struct net_device *dev)
291{
292 struct rose_node *rose_node;
293 struct rose_neigh *rose_neigh;
294 int i, err = 0;
295
296 spin_lock_bh(&rose_node_list_lock);
297 spin_lock_bh(&rose_neigh_list_lock);
298
299 rose_node = rose_node_list;
300 while (rose_node != NULL) {
301 if ((rose_node->mask == rose_route->mask) &&
302 (rosecmpm(&rose_route->address, &rose_node->address,
303 rose_route->mask) == 0))
304 break;
305 rose_node = rose_node->next;
306 }
307
308 if (rose_node == NULL || rose_node->loopback) {
309 err = -EINVAL;
310 goto out;
311 }
312
313 rose_neigh = rose_neigh_list;
314 while (rose_neigh != NULL) {
315 if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0
316 && rose_neigh->dev == dev)
317 break;
318 rose_neigh = rose_neigh->next;
319 }
320
321 if (rose_neigh == NULL) {
322 err = -EINVAL;
323 goto out;
324 }
325
326 for (i = 0; i < rose_node->count; i++) {
327 if (rose_node->neighbour[i] == rose_neigh) {
328 rose_neigh->count--;
329
330 if (rose_neigh->count == 0 && rose_neigh->use == 0)
331 rose_remove_neigh(rose_neigh);
332
333 rose_node->count--;
334
335 if (rose_node->count == 0) {
336 rose_remove_node(rose_node);
337 } else {
338 switch (i) {
339 case 0:
340 rose_node->neighbour[0] =
341 rose_node->neighbour[1];
342 case 1:
343 rose_node->neighbour[1] =
344 rose_node->neighbour[2];
345 case 2:
346 break;
347 }
348 }
349 goto out;
350 }
351 }
352 err = -EINVAL;
353
354out:
355 spin_unlock_bh(&rose_neigh_list_lock);
356 spin_unlock_bh(&rose_node_list_lock);
357
358 return err;
359}
360
361/*
362 * Add the loopback neighbour.
363 */
364int rose_add_loopback_neigh(void)
365{
366 if ((rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_ATOMIC)) == NULL)
367 return -ENOMEM;
368
369 rose_loopback_neigh->callsign = null_ax25_address;
370 rose_loopback_neigh->digipeat = NULL;
371 rose_loopback_neigh->ax25 = NULL;
372 rose_loopback_neigh->dev = NULL;
373 rose_loopback_neigh->count = 0;
374 rose_loopback_neigh->use = 0;
375 rose_loopback_neigh->dce_mode = 1;
376 rose_loopback_neigh->loopback = 1;
377 rose_loopback_neigh->number = rose_neigh_no++;
378 rose_loopback_neigh->restarted = 1;
379
380 skb_queue_head_init(&rose_loopback_neigh->queue);
381
382 init_timer(&rose_loopback_neigh->ftimer);
383 init_timer(&rose_loopback_neigh->t0timer);
384
385 spin_lock_bh(&rose_neigh_list_lock);
386 rose_loopback_neigh->next = rose_neigh_list;
387 rose_neigh_list = rose_loopback_neigh;
388 spin_unlock_bh(&rose_neigh_list_lock);
389
390 return 0;
391}
392
393/*
394 * Add a loopback node.
395 */
396int rose_add_loopback_node(rose_address *address)
397{
398 struct rose_node *rose_node;
399 unsigned int err = 0;
400
401 spin_lock_bh(&rose_node_list_lock);
402
403 rose_node = rose_node_list;
404 while (rose_node != NULL) {
405 if ((rose_node->mask == 10) &&
406 (rosecmpm(address, &rose_node->address, 10) == 0) &&
407 rose_node->loopback)
408 break;
409 rose_node = rose_node->next;
410 }
411
412 if (rose_node != NULL)
413 goto out;
414
415 if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
416 err = -ENOMEM;
417 goto out;
418 }
419
420 rose_node->address = *address;
421 rose_node->mask = 10;
422 rose_node->count = 1;
423 rose_node->loopback = 1;
424 rose_node->neighbour[0] = rose_loopback_neigh;
425
426 /* Insert at the head of list. Address is always mask=10 */
427 rose_node->next = rose_node_list;
428 rose_node_list = rose_node;
429
430 rose_loopback_neigh->count++;
431
432out:
433 spin_unlock_bh(&rose_node_list_lock);
434
435 return 0;
436}
437
438/*
439 * Delete a loopback node.
440 */
441void rose_del_loopback_node(rose_address *address)
442{
443 struct rose_node *rose_node;
444
445 spin_lock_bh(&rose_node_list_lock);
446
447 rose_node = rose_node_list;
448 while (rose_node != NULL) {
449 if ((rose_node->mask == 10) &&
450 (rosecmpm(address, &rose_node->address, 10) == 0) &&
451 rose_node->loopback)
452 break;
453 rose_node = rose_node->next;
454 }
455
456 if (rose_node == NULL)
457 goto out;
458
459 rose_remove_node(rose_node);
460
461 rose_loopback_neigh->count--;
462
463out:
464 spin_unlock_bh(&rose_node_list_lock);
465}
466
467/*
468 * A device has been removed. Remove its routes and neighbours.
469 */
470void rose_rt_device_down(struct net_device *dev)
471{
472 struct rose_neigh *s, *rose_neigh;
473 struct rose_node *t, *rose_node;
474 int i;
475
476 spin_lock_bh(&rose_node_list_lock);
477 spin_lock_bh(&rose_neigh_list_lock);
478 rose_neigh = rose_neigh_list;
479 while (rose_neigh != NULL) {
480 s = rose_neigh;
481 rose_neigh = rose_neigh->next;
482
483 if (s->dev != dev)
484 continue;
485
486 rose_node = rose_node_list;
487
488 while (rose_node != NULL) {
489 t = rose_node;
490 rose_node = rose_node->next;
491
492 for (i = 0; i < t->count; i++) {
493 if (t->neighbour[i] != s)
494 continue;
495
496 t->count--;
497
498 switch (i) {
499 case 0:
500 t->neighbour[0] = t->neighbour[1];
501 case 1:
502 t->neighbour[1] = t->neighbour[2];
503 case 2:
504 break;
505 }
506 }
507
508 if (t->count <= 0)
509 rose_remove_node(t);
510 }
511
512 rose_remove_neigh(s);
513 }
514 spin_unlock_bh(&rose_neigh_list_lock);
515 spin_unlock_bh(&rose_node_list_lock);
516}
517
518#if 0 /* Currently unused */
519/*
520 * A device has been removed. Remove its links.
521 */
522void rose_route_device_down(struct net_device *dev)
523{
524 struct rose_route *s, *rose_route;
525
526 spin_lock_bh(&rose_route_list_lock);
527 rose_route = rose_route_list;
528 while (rose_route != NULL) {
529 s = rose_route;
530 rose_route = rose_route->next;
531
532 if (s->neigh1->dev == dev || s->neigh2->dev == dev)
533 rose_remove_route(s);
534 }
535 spin_unlock_bh(&rose_route_list_lock);
536}
537#endif
538
539/*
540 * Clear all nodes and neighbours out, except for neighbours with
541 * active connections going through them.
542 * Do not clear loopback neighbour and nodes.
543 */
544static int rose_clear_routes(void)
545{
546 struct rose_neigh *s, *rose_neigh;
547 struct rose_node *t, *rose_node;
548
549 spin_lock_bh(&rose_node_list_lock);
550 spin_lock_bh(&rose_neigh_list_lock);
551
552 rose_neigh = rose_neigh_list;
553 rose_node = rose_node_list;
554
555 while (rose_node != NULL) {
556 t = rose_node;
557 rose_node = rose_node->next;
558 if (!t->loopback)
559 rose_remove_node(t);
560 }
561
562 while (rose_neigh != NULL) {
563 s = rose_neigh;
564 rose_neigh = rose_neigh->next;
565
566 if (s->use == 0 && !s->loopback) {
567 s->count = 0;
568 rose_remove_neigh(s);
569 }
570 }
571
572 spin_unlock_bh(&rose_neigh_list_lock);
573 spin_unlock_bh(&rose_node_list_lock);
574
575 return 0;
576}
577
578/*
579 * Check that the device given is a valid AX.25 interface that is "up".
580 */
581static struct net_device *rose_ax25_dev_get(char *devname)
582{
583 struct net_device *dev;
584
585 if ((dev = dev_get_by_name(devname)) == NULL)
586 return NULL;
587
588 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
589 return dev;
590
591 dev_put(dev);
592 return NULL;
593}
594
595/*
596 * Find the first active ROSE device, usually "rose0".
597 */
598struct net_device *rose_dev_first(void)
599{
600 struct net_device *dev, *first = NULL;
601
602 read_lock(&dev_base_lock);
603 for (dev = dev_base; dev != NULL; dev = dev->next) {
604 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
605 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
606 first = dev;
607 }
608 read_unlock(&dev_base_lock);
609
610 return first;
611}
612
613/*
614 * Find the ROSE device for the given address.
615 */
616struct net_device *rose_dev_get(rose_address *addr)
617{
618 struct net_device *dev;
619
620 read_lock(&dev_base_lock);
621 for (dev = dev_base; dev != NULL; dev = dev->next) {
622 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
623 dev_hold(dev);
624 goto out;
625 }
626 }
627out:
628 read_unlock(&dev_base_lock);
629 return dev;
630}
631
632static int rose_dev_exists(rose_address *addr)
633{
634 struct net_device *dev;
635
636 read_lock(&dev_base_lock);
637 for (dev = dev_base; dev != NULL; dev = dev->next) {
638 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
639 goto out;
640 }
641out:
642 read_unlock(&dev_base_lock);
643 return dev != NULL;
644}
645
646
647
648
649struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
650{
651 struct rose_route *rose_route;
652
653 for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
654 if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
655 (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
656 return rose_route;
657
658 return NULL;
659}
660
661/*
662 * Find a neighbour given a ROSE address.
663 */
664struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
665 unsigned char *diagnostic)
666{
667 struct rose_neigh *res = NULL;
668 struct rose_node *node;
669 int failed = 0;
670 int i;
671
672 spin_lock_bh(&rose_node_list_lock);
673 for (node = rose_node_list; node != NULL; node = node->next) {
674 if (rosecmpm(addr, &node->address, node->mask) == 0) {
675 for (i = 0; i < node->count; i++) {
676 if (!rose_ftimer_running(node->neighbour[i])) {
677 res = node->neighbour[i];
678 goto out;
679 } else
680 failed = 1;
681 }
682 break;
683 }
684 }
685
686 if (failed) {
687 *cause = ROSE_OUT_OF_ORDER;
688 *diagnostic = 0;
689 } else {
690 *cause = ROSE_NOT_OBTAINABLE;
691 *diagnostic = 0;
692 }
693
694out:
695 spin_unlock_bh(&rose_node_list_lock);
696
697 return res;
698}
699
700/*
701 * Handle the ioctls that control the routing functions.
702 */
703int rose_rt_ioctl(unsigned int cmd, void __user *arg)
704{
705 struct rose_route_struct rose_route;
706 struct net_device *dev;
707 int err;
708
709 switch (cmd) {
710 case SIOCADDRT:
711 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
712 return -EFAULT;
713 if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
714 return -EINVAL;
715 if (rose_dev_exists(&rose_route.address)) { /* Can't add routes to ourself */
716 dev_put(dev);
717 return -EINVAL;
718 }
719 if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
720 return -EINVAL;
Ralf Baechle95df1c02005-10-18 21:39:33 +0100721 if (rose_route.ndigis > AX25_MAX_DIGIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -EINVAL;
723 err = rose_add_node(&rose_route, dev);
724 dev_put(dev);
725 return err;
726
727 case SIOCDELRT:
728 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
729 return -EFAULT;
730 if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
731 return -EINVAL;
732 err = rose_del_node(&rose_route, dev);
733 dev_put(dev);
734 return err;
735
736 case SIOCRSCLRRT:
737 return rose_clear_routes();
738
739 default:
740 return -EINVAL;
741 }
742
743 return 0;
744}
745
746static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
747{
748 struct rose_route *rose_route, *s;
749
750 rose_neigh->restarted = 0;
751
752 rose_stop_t0timer(rose_neigh);
753 rose_start_ftimer(rose_neigh);
754
755 skb_queue_purge(&rose_neigh->queue);
756
757 spin_lock_bh(&rose_route_list_lock);
758
759 rose_route = rose_route_list;
760
761 while (rose_route != NULL) {
762 if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
763 (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
764 (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
765 s = rose_route->next;
766 rose_remove_route(rose_route);
767 rose_route = s;
768 continue;
769 }
770
771 if (rose_route->neigh1 == rose_neigh) {
772 rose_route->neigh1->use--;
773 rose_route->neigh1 = NULL;
774 rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
775 }
776
777 if (rose_route->neigh2 == rose_neigh) {
778 rose_route->neigh2->use--;
779 rose_route->neigh2 = NULL;
780 rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
781 }
782
783 rose_route = rose_route->next;
784 }
785 spin_unlock_bh(&rose_route_list_lock);
786}
787
788/*
789 * A level 2 link has timed out, therefore it appears to be a poor link,
790 * then don't use that neighbour until it is reset. Blow away all through
791 * routes and connections using this route.
792 */
793void rose_link_failed(ax25_cb *ax25, int reason)
794{
795 struct rose_neigh *rose_neigh;
796
797 spin_lock_bh(&rose_neigh_list_lock);
798 rose_neigh = rose_neigh_list;
799 while (rose_neigh != NULL) {
800 if (rose_neigh->ax25 == ax25)
801 break;
802 rose_neigh = rose_neigh->next;
803 }
804
805 if (rose_neigh != NULL) {
806 rose_neigh->ax25 = NULL;
807
808 rose_del_route_by_neigh(rose_neigh);
809 rose_kill_by_neigh(rose_neigh);
810 }
811 spin_unlock_bh(&rose_neigh_list_lock);
812}
813
814/*
815 * A device has been "downed" remove its link status. Blow away all
816 * through routes and connections that use this device.
817 */
818void rose_link_device_down(struct net_device *dev)
819{
820 struct rose_neigh *rose_neigh;
821
822 for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
823 if (rose_neigh->dev == dev) {
824 rose_del_route_by_neigh(rose_neigh);
825 rose_kill_by_neigh(rose_neigh);
826 }
827 }
828}
829
830/*
831 * Route a frame to an appropriate AX.25 connection.
832 */
833int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
834{
835 struct rose_neigh *rose_neigh, *new_neigh;
836 struct rose_route *rose_route;
837 struct rose_facilities_struct facilities;
838 rose_address *src_addr, *dest_addr;
839 struct sock *sk;
840 unsigned short frametype;
841 unsigned int lci, new_lci;
842 unsigned char cause, diagnostic;
843 struct net_device *dev;
844 int len, res = 0;
Ralf Baechlef75268c2005-09-06 15:49:39 -0700845 char buf[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847#if 0
848 if (call_in_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT)
849 return res;
850#endif
851
852 frametype = skb->data[2];
853 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
854 src_addr = (rose_address *)(skb->data + 9);
855 dest_addr = (rose_address *)(skb->data + 4);
856
857 spin_lock_bh(&rose_node_list_lock);
858 spin_lock_bh(&rose_neigh_list_lock);
859 spin_lock_bh(&rose_route_list_lock);
860
861 rose_neigh = rose_neigh_list;
862 while (rose_neigh != NULL) {
863 if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
864 ax25->ax25_dev->dev == rose_neigh->dev)
865 break;
866 rose_neigh = rose_neigh->next;
867 }
868
869 if (rose_neigh == NULL) {
870 printk("rose_route : unknown neighbour or device %s\n",
Ralf Baechlef75268c2005-09-06 15:49:39 -0700871 ax2asc(buf, &ax25->dest_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 goto out;
873 }
874
875 /*
876 * Obviously the link is working, halt the ftimer.
877 */
878 rose_stop_ftimer(rose_neigh);
879
880 /*
881 * LCI of zero is always for us, and its always a restart
882 * frame.
883 */
884 if (lci == 0) {
885 rose_link_rx_restart(skb, rose_neigh, frametype);
886 goto out;
887 }
888
889 /*
890 * Find an existing socket.
891 */
892 if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
893 if (frametype == ROSE_CALL_REQUEST) {
894 struct rose_sock *rose = rose_sk(sk);
895
896 /* Remove an existing unused socket */
897 rose_clear_queues(sk);
898 rose->cause = ROSE_NETWORK_CONGESTION;
899 rose->diagnostic = 0;
900 rose->neighbour->use--;
901 rose->neighbour = NULL;
902 rose->lci = 0;
903 rose->state = ROSE_STATE_0;
904 sk->sk_state = TCP_CLOSE;
905 sk->sk_err = 0;
906 sk->sk_shutdown |= SEND_SHUTDOWN;
907 if (!sock_flag(sk, SOCK_DEAD)) {
908 sk->sk_state_change(sk);
909 sock_set_flag(sk, SOCK_DEAD);
910 }
911 }
912 else {
913 skb->h.raw = skb->data;
914 res = rose_process_rx_frame(sk, skb);
915 goto out;
916 }
917 }
918
919 /*
920 * Is is a Call Request and is it for us ?
921 */
922 if (frametype == ROSE_CALL_REQUEST)
923 if ((dev = rose_dev_get(dest_addr)) != NULL) {
924 res = rose_rx_call_request(skb, dev, rose_neigh, lci);
925 dev_put(dev);
926 goto out;
927 }
928
929 if (!sysctl_rose_routing_control) {
930 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
931 goto out;
932 }
933
934 /*
935 * Route it to the next in line if we have an entry for it.
936 */
937 rose_route = rose_route_list;
938 while (rose_route != NULL) {
939 if (rose_route->lci1 == lci &&
940 rose_route->neigh1 == rose_neigh) {
941 if (frametype == ROSE_CALL_REQUEST) {
942 /* F6FBB - Remove an existing unused route */
943 rose_remove_route(rose_route);
944 break;
945 } else if (rose_route->neigh2 != NULL) {
946 skb->data[0] &= 0xF0;
947 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
948 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
949 rose_transmit_link(skb, rose_route->neigh2);
950 if (frametype == ROSE_CLEAR_CONFIRMATION)
951 rose_remove_route(rose_route);
952 res = 1;
953 goto out;
954 } else {
955 if (frametype == ROSE_CLEAR_CONFIRMATION)
956 rose_remove_route(rose_route);
957 goto out;
958 }
959 }
960 if (rose_route->lci2 == lci &&
961 rose_route->neigh2 == rose_neigh) {
962 if (frametype == ROSE_CALL_REQUEST) {
963 /* F6FBB - Remove an existing unused route */
964 rose_remove_route(rose_route);
965 break;
966 } else if (rose_route->neigh1 != NULL) {
967 skb->data[0] &= 0xF0;
968 skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
969 skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
970 rose_transmit_link(skb, rose_route->neigh1);
971 if (frametype == ROSE_CLEAR_CONFIRMATION)
972 rose_remove_route(rose_route);
973 res = 1;
974 goto out;
975 } else {
976 if (frametype == ROSE_CLEAR_CONFIRMATION)
977 rose_remove_route(rose_route);
978 goto out;
979 }
980 }
981 rose_route = rose_route->next;
982 }
983
984 /*
985 * We know that:
986 * 1. The frame isn't for us,
987 * 2. It isn't "owned" by any existing route.
988 */
David S. Millerdc16aaf2005-08-23 10:50:09 -0700989 if (frametype != ROSE_CALL_REQUEST) { /* XXX */
David S. Millerc1cc1682005-08-23 14:55:32 -0700990 res = 0;
David S. Millerdc16aaf2005-08-23 10:50:09 -0700991 goto out;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 len = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
995 len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
996
997 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
998
999 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
1000 rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1001 goto out;
1002 }
1003
1004 /*
1005 * Check for routing loops.
1006 */
1007 rose_route = rose_route_list;
1008 while (rose_route != NULL) {
1009 if (rose_route->rand == facilities.rand &&
1010 rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1011 ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1012 ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1013 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1014 goto out;
1015 }
1016 rose_route = rose_route->next;
1017 }
1018
1019 if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic)) == NULL) {
1020 rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1021 goto out;
1022 }
1023
1024 if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1025 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1026 goto out;
1027 }
1028
1029 if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1030 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1031 goto out;
1032 }
1033
1034 rose_route->lci1 = lci;
1035 rose_route->src_addr = *src_addr;
1036 rose_route->dest_addr = *dest_addr;
1037 rose_route->src_call = facilities.dest_call;
1038 rose_route->dest_call = facilities.source_call;
1039 rose_route->rand = facilities.rand;
1040 rose_route->neigh1 = rose_neigh;
1041 rose_route->lci2 = new_lci;
1042 rose_route->neigh2 = new_neigh;
1043
1044 rose_route->neigh1->use++;
1045 rose_route->neigh2->use++;
1046
1047 rose_route->next = rose_route_list;
1048 rose_route_list = rose_route;
1049
1050 skb->data[0] &= 0xF0;
1051 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1052 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
1053
1054 rose_transmit_link(skb, rose_route->neigh2);
1055 res = 1;
1056
1057out:
1058 spin_unlock_bh(&rose_route_list_lock);
1059 spin_unlock_bh(&rose_neigh_list_lock);
1060 spin_unlock_bh(&rose_node_list_lock);
1061
1062 return res;
1063}
1064
1065#ifdef CONFIG_PROC_FS
1066
1067static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1068{
1069 struct rose_node *rose_node;
1070 int i = 1;
1071
1072 spin_lock_bh(&rose_neigh_list_lock);
1073 if (*pos == 0)
1074 return SEQ_START_TOKEN;
1075
1076 for (rose_node = rose_node_list; rose_node && i < *pos;
1077 rose_node = rose_node->next, ++i);
1078
1079 return (i == *pos) ? rose_node : NULL;
1080}
1081
1082static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1083{
1084 ++*pos;
1085
1086 return (v == SEQ_START_TOKEN) ? rose_node_list
1087 : ((struct rose_node *)v)->next;
1088}
1089
1090static void rose_node_stop(struct seq_file *seq, void *v)
1091{
1092 spin_unlock_bh(&rose_neigh_list_lock);
1093}
1094
1095static int rose_node_show(struct seq_file *seq, void *v)
1096{
1097 int i;
1098
1099 if (v == SEQ_START_TOKEN)
1100 seq_puts(seq, "address mask n neigh neigh neigh\n");
1101 else {
1102 const struct rose_node *rose_node = v;
1103 /* if (rose_node->loopback) {
1104 seq_printf(seq, "%-10s %04d 1 loopback\n",
1105 rose2asc(&rose_node->address),
1106 rose_node->mask);
1107 } else { */
1108 seq_printf(seq, "%-10s %04d %d",
1109 rose2asc(&rose_node->address),
1110 rose_node->mask,
1111 rose_node->count);
1112
1113 for (i = 0; i < rose_node->count; i++)
1114 seq_printf(seq, " %05d",
1115 rose_node->neighbour[i]->number);
1116
1117 seq_puts(seq, "\n");
1118 /* } */
1119 }
1120 return 0;
1121}
1122
1123static struct seq_operations rose_node_seqops = {
1124 .start = rose_node_start,
1125 .next = rose_node_next,
1126 .stop = rose_node_stop,
1127 .show = rose_node_show,
1128};
1129
1130static int rose_nodes_open(struct inode *inode, struct file *file)
1131{
1132 return seq_open(file, &rose_node_seqops);
1133}
1134
1135struct file_operations rose_nodes_fops = {
1136 .owner = THIS_MODULE,
1137 .open = rose_nodes_open,
1138 .read = seq_read,
1139 .llseek = seq_lseek,
1140 .release = seq_release,
1141};
1142
1143static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1144{
1145 struct rose_neigh *rose_neigh;
1146 int i = 1;
1147
1148 spin_lock_bh(&rose_neigh_list_lock);
1149 if (*pos == 0)
1150 return SEQ_START_TOKEN;
1151
1152 for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1153 rose_neigh = rose_neigh->next, ++i);
1154
1155 return (i == *pos) ? rose_neigh : NULL;
1156}
1157
1158static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1159{
1160 ++*pos;
1161
1162 return (v == SEQ_START_TOKEN) ? rose_neigh_list
1163 : ((struct rose_neigh *)v)->next;
1164}
1165
1166static void rose_neigh_stop(struct seq_file *seq, void *v)
1167{
1168 spin_unlock_bh(&rose_neigh_list_lock);
1169}
1170
1171static int rose_neigh_show(struct seq_file *seq, void *v)
1172{
Ralf Baechlef75268c2005-09-06 15:49:39 -07001173 char buf[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int i;
1175
1176 if (v == SEQ_START_TOKEN)
1177 seq_puts(seq,
1178 "addr callsign dev count use mode restart t0 tf digipeaters\n");
1179 else {
1180 struct rose_neigh *rose_neigh = v;
1181
1182 /* if (!rose_neigh->loopback) { */
1183 seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
1184 rose_neigh->number,
Ralf Baechlef75268c2005-09-06 15:49:39 -07001185 (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 rose_neigh->dev ? rose_neigh->dev->name : "???",
1187 rose_neigh->count,
1188 rose_neigh->use,
1189 (rose_neigh->dce_mode) ? "DCE" : "DTE",
1190 (rose_neigh->restarted) ? "yes" : "no",
1191 ax25_display_timer(&rose_neigh->t0timer) / HZ,
1192 ax25_display_timer(&rose_neigh->ftimer) / HZ);
1193
1194 if (rose_neigh->digipeat != NULL) {
1195 for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
Ralf Baechlef75268c2005-09-06 15:49:39 -07001196 seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198
1199 seq_puts(seq, "\n");
1200 }
1201 return 0;
1202}
1203
1204
1205static struct seq_operations rose_neigh_seqops = {
1206 .start = rose_neigh_start,
1207 .next = rose_neigh_next,
1208 .stop = rose_neigh_stop,
1209 .show = rose_neigh_show,
1210};
1211
1212static int rose_neigh_open(struct inode *inode, struct file *file)
1213{
1214 return seq_open(file, &rose_neigh_seqops);
1215}
1216
1217struct file_operations rose_neigh_fops = {
1218 .owner = THIS_MODULE,
1219 .open = rose_neigh_open,
1220 .read = seq_read,
1221 .llseek = seq_lseek,
1222 .release = seq_release,
1223};
1224
1225
1226static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1227{
1228 struct rose_route *rose_route;
1229 int i = 1;
1230
1231 spin_lock_bh(&rose_route_list_lock);
1232 if (*pos == 0)
1233 return SEQ_START_TOKEN;
1234
1235 for (rose_route = rose_route_list; rose_route && i < *pos;
1236 rose_route = rose_route->next, ++i);
1237
1238 return (i == *pos) ? rose_route : NULL;
1239}
1240
1241static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1242{
1243 ++*pos;
1244
1245 return (v == SEQ_START_TOKEN) ? rose_route_list
1246 : ((struct rose_route *)v)->next;
1247}
1248
1249static void rose_route_stop(struct seq_file *seq, void *v)
1250{
1251 spin_unlock_bh(&rose_route_list_lock);
1252}
1253
1254static int rose_route_show(struct seq_file *seq, void *v)
1255{
Ralf Baechlef75268c2005-09-06 15:49:39 -07001256 char buf[11];
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (v == SEQ_START_TOKEN)
1259 seq_puts(seq,
1260 "lci address callsign neigh <-> lci address callsign neigh\n");
1261 else {
1262 struct rose_route *rose_route = v;
1263
1264 if (rose_route->neigh1)
1265 seq_printf(seq,
1266 "%3.3X %-10s %-9s %05d ",
1267 rose_route->lci1,
1268 rose2asc(&rose_route->src_addr),
Ralf Baechlef75268c2005-09-06 15:49:39 -07001269 ax2asc(buf, &rose_route->src_call),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 rose_route->neigh1->number);
1271 else
1272 seq_puts(seq,
1273 "000 * * 00000 ");
1274
1275 if (rose_route->neigh2)
1276 seq_printf(seq,
1277 "%3.3X %-10s %-9s %05d\n",
1278 rose_route->lci2,
1279 rose2asc(&rose_route->dest_addr),
Ralf Baechlef75268c2005-09-06 15:49:39 -07001280 ax2asc(buf, &rose_route->dest_call),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 rose_route->neigh2->number);
1282 else
1283 seq_puts(seq,
1284 "000 * * 00000\n");
1285 }
1286 return 0;
1287}
1288
1289static struct seq_operations rose_route_seqops = {
1290 .start = rose_route_start,
1291 .next = rose_route_next,
1292 .stop = rose_route_stop,
1293 .show = rose_route_show,
1294};
1295
1296static int rose_route_open(struct inode *inode, struct file *file)
1297{
1298 return seq_open(file, &rose_route_seqops);
1299}
1300
1301struct file_operations rose_routes_fops = {
1302 .owner = THIS_MODULE,
1303 .open = rose_route_open,
1304 .read = seq_read,
1305 .llseek = seq_lseek,
1306 .release = seq_release,
1307};
1308
1309#endif /* CONFIG_PROC_FS */
1310
1311/*
1312 * Release all memory associated with ROSE routing structures.
1313 */
1314void __exit rose_rt_free(void)
1315{
1316 struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1317 struct rose_node *t, *rose_node = rose_node_list;
1318 struct rose_route *u, *rose_route = rose_route_list;
1319
1320 while (rose_neigh != NULL) {
1321 s = rose_neigh;
1322 rose_neigh = rose_neigh->next;
1323
1324 rose_remove_neigh(s);
1325 }
1326
1327 while (rose_node != NULL) {
1328 t = rose_node;
1329 rose_node = rose_node->next;
1330
1331 rose_remove_node(t);
1332 }
1333
1334 while (rose_route != NULL) {
1335 u = rose_route;
1336 rose_route = rose_route->next;
1337
1338 rose_remove_route(u);
1339 }
1340}