crypto: pcrypt - Rename pcrypt_instance
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / rose / rose_route.c
blobcbc244a128bd1611829f6e943ae7a669cc81f2d3
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.
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/timer.h>
16 #include <linux/string.h>
17 #include <linux/sockios.h>
18 #include <linux/net.h>
19 #include <linux/slab.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_states.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>
40 static unsigned int rose_neigh_no = 1;
42 static struct rose_node *rose_node_list;
43 static DEFINE_SPINLOCK(rose_node_list_lock);
44 static struct rose_neigh *rose_neigh_list;
45 static DEFINE_SPINLOCK(rose_neigh_list_lock);
46 static struct rose_route *rose_route_list;
47 static DEFINE_SPINLOCK(rose_route_list_lock);
49 struct rose_neigh *rose_loopback_neigh;
52 * Add a new route to a node, and in the process add the node and the
53 * neighbour if it is new.
55 static int __must_check rose_add_node(struct rose_route_struct *rose_route,
56 struct net_device *dev)
58 struct rose_node *rose_node, *rose_tmpn, *rose_tmpp;
59 struct rose_neigh *rose_neigh;
60 int i, res = 0;
62 spin_lock_bh(&rose_node_list_lock);
63 spin_lock_bh(&rose_neigh_list_lock);
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;
74 if (rose_node != NULL && rose_node->loopback) {
75 res = -EINVAL;
76 goto out;
79 rose_neigh = rose_neigh_list;
80 while (rose_neigh != NULL) {
81 if (ax25cmp(&rose_route->neighbour,
82 &rose_neigh->callsign) == 0 &&
83 rose_neigh->dev == dev)
84 break;
85 rose_neigh = rose_neigh->next;
88 if (rose_neigh == NULL) {
89 rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
90 if (rose_neigh == NULL) {
91 res = -ENOMEM;
92 goto out;
95 rose_neigh->callsign = rose_route->neighbour;
96 rose_neigh->digipeat = NULL;
97 rose_neigh->ax25 = NULL;
98 rose_neigh->dev = dev;
99 rose_neigh->count = 0;
100 rose_neigh->use = 0;
101 rose_neigh->dce_mode = 0;
102 rose_neigh->loopback = 0;
103 rose_neigh->number = rose_neigh_no++;
104 rose_neigh->restarted = 0;
106 skb_queue_head_init(&rose_neigh->queue);
108 init_timer(&rose_neigh->ftimer);
109 init_timer(&rose_neigh->t0timer);
111 if (rose_route->ndigis != 0) {
112 if ((rose_neigh->digipeat = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) {
113 kfree(rose_neigh);
114 res = -ENOMEM;
115 goto out;
118 rose_neigh->digipeat->ndigi = rose_route->ndigis;
119 rose_neigh->digipeat->lastrepeat = -1;
121 for (i = 0; i < rose_route->ndigis; i++) {
122 rose_neigh->digipeat->calls[i] =
123 rose_route->digipeaters[i];
124 rose_neigh->digipeat->repeated[i] = 0;
128 rose_neigh->next = rose_neigh_list;
129 rose_neigh_list = rose_neigh;
133 * This is a new node to be inserted into the list. Find where it needs
134 * to be inserted into the list, and insert it. We want to be sure
135 * to order the list in descending order of mask size to ensure that
136 * later when we are searching this list the first match will be the
137 * best match.
139 if (rose_node == NULL) {
140 rose_tmpn = rose_node_list;
141 rose_tmpp = NULL;
143 while (rose_tmpn != NULL) {
144 if (rose_tmpn->mask > rose_route->mask) {
145 rose_tmpp = rose_tmpn;
146 rose_tmpn = rose_tmpn->next;
147 } else {
148 break;
152 /* create new node */
153 rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
154 if (rose_node == NULL) {
155 res = -ENOMEM;
156 goto out;
159 rose_node->address = rose_route->address;
160 rose_node->mask = rose_route->mask;
161 rose_node->count = 1;
162 rose_node->loopback = 0;
163 rose_node->neighbour[0] = rose_neigh;
165 if (rose_tmpn == NULL) {
166 if (rose_tmpp == NULL) { /* Empty list */
167 rose_node_list = rose_node;
168 rose_node->next = NULL;
169 } else {
170 rose_tmpp->next = rose_node;
171 rose_node->next = NULL;
173 } else {
174 if (rose_tmpp == NULL) { /* 1st node */
175 rose_node->next = rose_node_list;
176 rose_node_list = rose_node;
177 } else {
178 rose_tmpp->next = rose_node;
179 rose_node->next = rose_tmpn;
182 rose_neigh->count++;
184 goto out;
187 /* We have space, slot it in */
188 if (rose_node->count < 3) {
189 rose_node->neighbour[rose_node->count] = rose_neigh;
190 rose_node->count++;
191 rose_neigh->count++;
194 out:
195 spin_unlock_bh(&rose_neigh_list_lock);
196 spin_unlock_bh(&rose_node_list_lock);
198 return res;
202 * Caller is holding rose_node_list_lock.
204 static void rose_remove_node(struct rose_node *rose_node)
206 struct rose_node *s;
208 if ((s = rose_node_list) == rose_node) {
209 rose_node_list = rose_node->next;
210 kfree(rose_node);
211 return;
214 while (s != NULL && s->next != NULL) {
215 if (s->next == rose_node) {
216 s->next = rose_node->next;
217 kfree(rose_node);
218 return;
221 s = s->next;
226 * Caller is holding rose_neigh_list_lock.
228 static void rose_remove_neigh(struct rose_neigh *rose_neigh)
230 struct rose_neigh *s;
232 rose_stop_ftimer(rose_neigh);
233 rose_stop_t0timer(rose_neigh);
235 skb_queue_purge(&rose_neigh->queue);
237 if ((s = rose_neigh_list) == rose_neigh) {
238 rose_neigh_list = rose_neigh->next;
239 if (rose_neigh->ax25)
240 ax25_cb_put(rose_neigh->ax25);
241 kfree(rose_neigh->digipeat);
242 kfree(rose_neigh);
243 return;
246 while (s != NULL && s->next != NULL) {
247 if (s->next == rose_neigh) {
248 s->next = rose_neigh->next;
249 if (rose_neigh->ax25)
250 ax25_cb_put(rose_neigh->ax25);
251 kfree(rose_neigh->digipeat);
252 kfree(rose_neigh);
253 return;
256 s = s->next;
261 * Caller is holding rose_route_list_lock.
263 static void rose_remove_route(struct rose_route *rose_route)
265 struct rose_route *s;
267 if (rose_route->neigh1 != NULL)
268 rose_route->neigh1->use--;
270 if (rose_route->neigh2 != NULL)
271 rose_route->neigh2->use--;
273 if ((s = rose_route_list) == rose_route) {
274 rose_route_list = rose_route->next;
275 kfree(rose_route);
276 return;
279 while (s != NULL && s->next != NULL) {
280 if (s->next == rose_route) {
281 s->next = rose_route->next;
282 kfree(rose_route);
283 return;
286 s = s->next;
291 * "Delete" a node. Strictly speaking remove a route to a node. The node
292 * is only deleted if no routes are left to it.
294 static int rose_del_node(struct rose_route_struct *rose_route,
295 struct net_device *dev)
297 struct rose_node *rose_node;
298 struct rose_neigh *rose_neigh;
299 int i, err = 0;
301 spin_lock_bh(&rose_node_list_lock);
302 spin_lock_bh(&rose_neigh_list_lock);
304 rose_node = rose_node_list;
305 while (rose_node != NULL) {
306 if ((rose_node->mask == rose_route->mask) &&
307 (rosecmpm(&rose_route->address, &rose_node->address,
308 rose_route->mask) == 0))
309 break;
310 rose_node = rose_node->next;
313 if (rose_node == NULL || rose_node->loopback) {
314 err = -EINVAL;
315 goto out;
318 rose_neigh = rose_neigh_list;
319 while (rose_neigh != NULL) {
320 if (ax25cmp(&rose_route->neighbour,
321 &rose_neigh->callsign) == 0 &&
322 rose_neigh->dev == dev)
323 break;
324 rose_neigh = rose_neigh->next;
327 if (rose_neigh == NULL) {
328 err = -EINVAL;
329 goto out;
332 for (i = 0; i < rose_node->count; i++) {
333 if (rose_node->neighbour[i] == rose_neigh) {
334 rose_neigh->count--;
336 if (rose_neigh->count == 0 && rose_neigh->use == 0)
337 rose_remove_neigh(rose_neigh);
339 rose_node->count--;
341 if (rose_node->count == 0) {
342 rose_remove_node(rose_node);
343 } else {
344 switch (i) {
345 case 0:
346 rose_node->neighbour[0] =
347 rose_node->neighbour[1];
348 case 1:
349 rose_node->neighbour[1] =
350 rose_node->neighbour[2];
351 case 2:
352 break;
355 goto out;
358 err = -EINVAL;
360 out:
361 spin_unlock_bh(&rose_neigh_list_lock);
362 spin_unlock_bh(&rose_node_list_lock);
364 return err;
368 * Add the loopback neighbour.
370 void rose_add_loopback_neigh(void)
372 struct rose_neigh *sn;
374 rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_KERNEL);
375 if (!rose_loopback_neigh)
376 return;
377 sn = rose_loopback_neigh;
379 sn->callsign = null_ax25_address;
380 sn->digipeat = NULL;
381 sn->ax25 = NULL;
382 sn->dev = NULL;
383 sn->count = 0;
384 sn->use = 0;
385 sn->dce_mode = 1;
386 sn->loopback = 1;
387 sn->number = rose_neigh_no++;
388 sn->restarted = 1;
390 skb_queue_head_init(&sn->queue);
392 init_timer(&sn->ftimer);
393 init_timer(&sn->t0timer);
395 spin_lock_bh(&rose_neigh_list_lock);
396 sn->next = rose_neigh_list;
397 rose_neigh_list = sn;
398 spin_unlock_bh(&rose_neigh_list_lock);
402 * Add a loopback node.
404 int rose_add_loopback_node(rose_address *address)
406 struct rose_node *rose_node;
407 int err = 0;
409 spin_lock_bh(&rose_node_list_lock);
411 rose_node = rose_node_list;
412 while (rose_node != NULL) {
413 if ((rose_node->mask == 10) &&
414 (rosecmpm(address, &rose_node->address, 10) == 0) &&
415 rose_node->loopback)
416 break;
417 rose_node = rose_node->next;
420 if (rose_node != NULL)
421 goto out;
423 if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
424 err = -ENOMEM;
425 goto out;
428 rose_node->address = *address;
429 rose_node->mask = 10;
430 rose_node->count = 1;
431 rose_node->loopback = 1;
432 rose_node->neighbour[0] = rose_loopback_neigh;
434 /* Insert at the head of list. Address is always mask=10 */
435 rose_node->next = rose_node_list;
436 rose_node_list = rose_node;
438 rose_loopback_neigh->count++;
440 out:
441 spin_unlock_bh(&rose_node_list_lock);
443 return err;
447 * Delete a loopback node.
449 void rose_del_loopback_node(rose_address *address)
451 struct rose_node *rose_node;
453 spin_lock_bh(&rose_node_list_lock);
455 rose_node = rose_node_list;
456 while (rose_node != NULL) {
457 if ((rose_node->mask == 10) &&
458 (rosecmpm(address, &rose_node->address, 10) == 0) &&
459 rose_node->loopback)
460 break;
461 rose_node = rose_node->next;
464 if (rose_node == NULL)
465 goto out;
467 rose_remove_node(rose_node);
469 rose_loopback_neigh->count--;
471 out:
472 spin_unlock_bh(&rose_node_list_lock);
476 * A device has been removed. Remove its routes and neighbours.
478 void rose_rt_device_down(struct net_device *dev)
480 struct rose_neigh *s, *rose_neigh;
481 struct rose_node *t, *rose_node;
482 int i;
484 spin_lock_bh(&rose_node_list_lock);
485 spin_lock_bh(&rose_neigh_list_lock);
486 rose_neigh = rose_neigh_list;
487 while (rose_neigh != NULL) {
488 s = rose_neigh;
489 rose_neigh = rose_neigh->next;
491 if (s->dev != dev)
492 continue;
494 rose_node = rose_node_list;
496 while (rose_node != NULL) {
497 t = rose_node;
498 rose_node = rose_node->next;
500 for (i = 0; i < t->count; i++) {
501 if (t->neighbour[i] != s)
502 continue;
504 t->count--;
506 switch (i) {
507 case 0:
508 t->neighbour[0] = t->neighbour[1];
509 case 1:
510 t->neighbour[1] = t->neighbour[2];
511 case 2:
512 break;
516 if (t->count <= 0)
517 rose_remove_node(t);
520 rose_remove_neigh(s);
522 spin_unlock_bh(&rose_neigh_list_lock);
523 spin_unlock_bh(&rose_node_list_lock);
526 #if 0 /* Currently unused */
528 * A device has been removed. Remove its links.
530 void rose_route_device_down(struct net_device *dev)
532 struct rose_route *s, *rose_route;
534 spin_lock_bh(&rose_route_list_lock);
535 rose_route = rose_route_list;
536 while (rose_route != NULL) {
537 s = rose_route;
538 rose_route = rose_route->next;
540 if (s->neigh1->dev == dev || s->neigh2->dev == dev)
541 rose_remove_route(s);
543 spin_unlock_bh(&rose_route_list_lock);
545 #endif
548 * Clear all nodes and neighbours out, except for neighbours with
549 * active connections going through them.
550 * Do not clear loopback neighbour and nodes.
552 static int rose_clear_routes(void)
554 struct rose_neigh *s, *rose_neigh;
555 struct rose_node *t, *rose_node;
557 spin_lock_bh(&rose_node_list_lock);
558 spin_lock_bh(&rose_neigh_list_lock);
560 rose_neigh = rose_neigh_list;
561 rose_node = rose_node_list;
563 while (rose_node != NULL) {
564 t = rose_node;
565 rose_node = rose_node->next;
566 if (!t->loopback)
567 rose_remove_node(t);
570 while (rose_neigh != NULL) {
571 s = rose_neigh;
572 rose_neigh = rose_neigh->next;
574 if (s->use == 0 && !s->loopback) {
575 s->count = 0;
576 rose_remove_neigh(s);
580 spin_unlock_bh(&rose_neigh_list_lock);
581 spin_unlock_bh(&rose_node_list_lock);
583 return 0;
587 * Check that the device given is a valid AX.25 interface that is "up".
588 * called whith RTNL
590 static struct net_device *rose_ax25_dev_find(char *devname)
592 struct net_device *dev;
594 if ((dev = __dev_get_by_name(&init_net, devname)) == NULL)
595 return NULL;
597 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
598 return dev;
600 return NULL;
604 * Find the first active ROSE device, usually "rose0".
606 struct net_device *rose_dev_first(void)
608 struct net_device *dev, *first = NULL;
610 rcu_read_lock();
611 for_each_netdev_rcu(&init_net, dev) {
612 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
613 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
614 first = dev;
616 rcu_read_unlock();
618 return first;
622 * Find the ROSE device for the given address.
624 struct net_device *rose_dev_get(rose_address *addr)
626 struct net_device *dev;
628 rcu_read_lock();
629 for_each_netdev_rcu(&init_net, dev) {
630 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
631 dev_hold(dev);
632 goto out;
635 dev = NULL;
636 out:
637 rcu_read_unlock();
638 return dev;
641 static int rose_dev_exists(rose_address *addr)
643 struct net_device *dev;
645 rcu_read_lock();
646 for_each_netdev_rcu(&init_net, dev) {
647 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
648 goto out;
650 dev = NULL;
651 out:
652 rcu_read_unlock();
653 return dev != NULL;
659 struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
661 struct rose_route *rose_route;
663 for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
664 if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
665 (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
666 return rose_route;
668 return NULL;
672 * Find a neighbour or a route given a ROSE address.
674 struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
675 unsigned char *diagnostic, int new)
677 struct rose_neigh *res = NULL;
678 struct rose_node *node;
679 int failed = 0;
680 int i;
682 if (!new) spin_lock_bh(&rose_node_list_lock);
683 for (node = rose_node_list; node != NULL; node = node->next) {
684 if (rosecmpm(addr, &node->address, node->mask) == 0) {
685 for (i = 0; i < node->count; i++) {
686 if (new) {
687 if (node->neighbour[i]->restarted) {
688 res = node->neighbour[i];
689 goto out;
692 else {
693 if (!rose_ftimer_running(node->neighbour[i])) {
694 res = node->neighbour[i];
695 goto out;
696 } else
697 failed = 1;
703 if (failed) {
704 *cause = ROSE_OUT_OF_ORDER;
705 *diagnostic = 0;
706 } else {
707 *cause = ROSE_NOT_OBTAINABLE;
708 *diagnostic = 0;
711 out:
712 if (!new) spin_unlock_bh(&rose_node_list_lock);
714 return res;
718 * Handle the ioctls that control the routing functions.
720 int rose_rt_ioctl(unsigned int cmd, void __user *arg)
722 struct rose_route_struct rose_route;
723 struct net_device *dev;
724 int err;
726 switch (cmd) {
727 case SIOCADDRT:
728 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
729 return -EFAULT;
730 if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
731 return -EINVAL;
732 if (rose_dev_exists(&rose_route.address)) /* Can't add routes to ourself */
733 return -EINVAL;
734 if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
735 return -EINVAL;
736 if (rose_route.ndigis > AX25_MAX_DIGIS)
737 return -EINVAL;
738 err = rose_add_node(&rose_route, dev);
739 return err;
741 case SIOCDELRT:
742 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
743 return -EFAULT;
744 if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
745 return -EINVAL;
746 err = rose_del_node(&rose_route, dev);
747 return err;
749 case SIOCRSCLRRT:
750 return rose_clear_routes();
752 default:
753 return -EINVAL;
756 return 0;
759 static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
761 struct rose_route *rose_route, *s;
763 rose_neigh->restarted = 0;
765 rose_stop_t0timer(rose_neigh);
766 rose_start_ftimer(rose_neigh);
768 skb_queue_purge(&rose_neigh->queue);
770 spin_lock_bh(&rose_route_list_lock);
772 rose_route = rose_route_list;
774 while (rose_route != NULL) {
775 if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
776 (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
777 (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
778 s = rose_route->next;
779 rose_remove_route(rose_route);
780 rose_route = s;
781 continue;
784 if (rose_route->neigh1 == rose_neigh) {
785 rose_route->neigh1->use--;
786 rose_route->neigh1 = NULL;
787 rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
790 if (rose_route->neigh2 == rose_neigh) {
791 rose_route->neigh2->use--;
792 rose_route->neigh2 = NULL;
793 rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
796 rose_route = rose_route->next;
798 spin_unlock_bh(&rose_route_list_lock);
802 * A level 2 link has timed out, therefore it appears to be a poor link,
803 * then don't use that neighbour until it is reset. Blow away all through
804 * routes and connections using this route.
806 void rose_link_failed(ax25_cb *ax25, int reason)
808 struct rose_neigh *rose_neigh;
810 spin_lock_bh(&rose_neigh_list_lock);
811 rose_neigh = rose_neigh_list;
812 while (rose_neigh != NULL) {
813 if (rose_neigh->ax25 == ax25)
814 break;
815 rose_neigh = rose_neigh->next;
818 if (rose_neigh != NULL) {
819 rose_neigh->ax25 = NULL;
820 ax25_cb_put(ax25);
822 rose_del_route_by_neigh(rose_neigh);
823 rose_kill_by_neigh(rose_neigh);
825 spin_unlock_bh(&rose_neigh_list_lock);
829 * A device has been "downed" remove its link status. Blow away all
830 * through routes and connections that use this device.
832 void rose_link_device_down(struct net_device *dev)
834 struct rose_neigh *rose_neigh;
836 for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
837 if (rose_neigh->dev == dev) {
838 rose_del_route_by_neigh(rose_neigh);
839 rose_kill_by_neigh(rose_neigh);
845 * Route a frame to an appropriate AX.25 connection.
847 int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
849 struct rose_neigh *rose_neigh, *new_neigh;
850 struct rose_route *rose_route;
851 struct rose_facilities_struct facilities;
852 rose_address *src_addr, *dest_addr;
853 struct sock *sk;
854 unsigned short frametype;
855 unsigned int lci, new_lci;
856 unsigned char cause, diagnostic;
857 struct net_device *dev;
858 int len, res = 0;
859 char buf[11];
861 #if 0
862 if (call_in_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT)
863 return res;
864 #endif
866 frametype = skb->data[2];
867 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
868 src_addr = (rose_address *)(skb->data + 9);
869 dest_addr = (rose_address *)(skb->data + 4);
871 spin_lock_bh(&rose_neigh_list_lock);
872 spin_lock_bh(&rose_route_list_lock);
874 rose_neigh = rose_neigh_list;
875 while (rose_neigh != NULL) {
876 if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
877 ax25->ax25_dev->dev == rose_neigh->dev)
878 break;
879 rose_neigh = rose_neigh->next;
882 if (rose_neigh == NULL) {
883 printk("rose_route : unknown neighbour or device %s\n",
884 ax2asc(buf, &ax25->dest_addr));
885 goto out;
889 * Obviously the link is working, halt the ftimer.
891 rose_stop_ftimer(rose_neigh);
894 * LCI of zero is always for us, and its always a restart
895 * frame.
897 if (lci == 0) {
898 rose_link_rx_restart(skb, rose_neigh, frametype);
899 goto out;
903 * Find an existing socket.
905 if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
906 if (frametype == ROSE_CALL_REQUEST) {
907 struct rose_sock *rose = rose_sk(sk);
909 /* Remove an existing unused socket */
910 rose_clear_queues(sk);
911 rose->cause = ROSE_NETWORK_CONGESTION;
912 rose->diagnostic = 0;
913 rose->neighbour->use--;
914 rose->neighbour = NULL;
915 rose->lci = 0;
916 rose->state = ROSE_STATE_0;
917 sk->sk_state = TCP_CLOSE;
918 sk->sk_err = 0;
919 sk->sk_shutdown |= SEND_SHUTDOWN;
920 if (!sock_flag(sk, SOCK_DEAD)) {
921 sk->sk_state_change(sk);
922 sock_set_flag(sk, SOCK_DEAD);
925 else {
926 skb_reset_transport_header(skb);
927 res = rose_process_rx_frame(sk, skb);
928 goto out;
933 * Is is a Call Request and is it for us ?
935 if (frametype == ROSE_CALL_REQUEST)
936 if ((dev = rose_dev_get(dest_addr)) != NULL) {
937 res = rose_rx_call_request(skb, dev, rose_neigh, lci);
938 dev_put(dev);
939 goto out;
942 if (!sysctl_rose_routing_control) {
943 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
944 goto out;
948 * Route it to the next in line if we have an entry for it.
950 rose_route = rose_route_list;
951 while (rose_route != NULL) {
952 if (rose_route->lci1 == lci &&
953 rose_route->neigh1 == rose_neigh) {
954 if (frametype == ROSE_CALL_REQUEST) {
955 /* F6FBB - Remove an existing unused route */
956 rose_remove_route(rose_route);
957 break;
958 } else if (rose_route->neigh2 != NULL) {
959 skb->data[0] &= 0xF0;
960 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
961 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
962 rose_transmit_link(skb, rose_route->neigh2);
963 if (frametype == ROSE_CLEAR_CONFIRMATION)
964 rose_remove_route(rose_route);
965 res = 1;
966 goto out;
967 } else {
968 if (frametype == ROSE_CLEAR_CONFIRMATION)
969 rose_remove_route(rose_route);
970 goto out;
973 if (rose_route->lci2 == lci &&
974 rose_route->neigh2 == rose_neigh) {
975 if (frametype == ROSE_CALL_REQUEST) {
976 /* F6FBB - Remove an existing unused route */
977 rose_remove_route(rose_route);
978 break;
979 } else if (rose_route->neigh1 != NULL) {
980 skb->data[0] &= 0xF0;
981 skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
982 skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
983 rose_transmit_link(skb, rose_route->neigh1);
984 if (frametype == ROSE_CLEAR_CONFIRMATION)
985 rose_remove_route(rose_route);
986 res = 1;
987 goto out;
988 } else {
989 if (frametype == ROSE_CLEAR_CONFIRMATION)
990 rose_remove_route(rose_route);
991 goto out;
994 rose_route = rose_route->next;
998 * We know that:
999 * 1. The frame isn't for us,
1000 * 2. It isn't "owned" by any existing route.
1002 if (frametype != ROSE_CALL_REQUEST) { /* XXX */
1003 res = 0;
1004 goto out;
1007 len = (((skb->data[3] >> 4) & 0x0F) + 1) >> 1;
1008 len += (((skb->data[3] >> 0) & 0x0F) + 1) >> 1;
1010 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1012 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
1013 rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1014 goto out;
1018 * Check for routing loops.
1020 rose_route = rose_route_list;
1021 while (rose_route != NULL) {
1022 if (rose_route->rand == facilities.rand &&
1023 rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1024 ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1025 ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1026 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1027 goto out;
1029 rose_route = rose_route->next;
1032 if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic, 1)) == NULL) {
1033 rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1034 goto out;
1037 if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1038 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1039 goto out;
1042 if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1043 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1044 goto out;
1047 rose_route->lci1 = lci;
1048 rose_route->src_addr = *src_addr;
1049 rose_route->dest_addr = *dest_addr;
1050 rose_route->src_call = facilities.dest_call;
1051 rose_route->dest_call = facilities.source_call;
1052 rose_route->rand = facilities.rand;
1053 rose_route->neigh1 = rose_neigh;
1054 rose_route->lci2 = new_lci;
1055 rose_route->neigh2 = new_neigh;
1057 rose_route->neigh1->use++;
1058 rose_route->neigh2->use++;
1060 rose_route->next = rose_route_list;
1061 rose_route_list = rose_route;
1063 skb->data[0] &= 0xF0;
1064 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1065 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
1067 rose_transmit_link(skb, rose_route->neigh2);
1068 res = 1;
1070 out:
1071 spin_unlock_bh(&rose_route_list_lock);
1072 spin_unlock_bh(&rose_neigh_list_lock);
1074 return res;
1077 #ifdef CONFIG_PROC_FS
1079 static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1080 __acquires(rose_node_list_lock)
1082 struct rose_node *rose_node;
1083 int i = 1;
1085 spin_lock_bh(&rose_node_list_lock);
1086 if (*pos == 0)
1087 return SEQ_START_TOKEN;
1089 for (rose_node = rose_node_list; rose_node && i < *pos;
1090 rose_node = rose_node->next, ++i);
1092 return (i == *pos) ? rose_node : NULL;
1095 static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1097 ++*pos;
1099 return (v == SEQ_START_TOKEN) ? rose_node_list
1100 : ((struct rose_node *)v)->next;
1103 static void rose_node_stop(struct seq_file *seq, void *v)
1104 __releases(rose_node_list_lock)
1106 spin_unlock_bh(&rose_node_list_lock);
1109 static int rose_node_show(struct seq_file *seq, void *v)
1111 char rsbuf[11];
1112 int i;
1114 if (v == SEQ_START_TOKEN)
1115 seq_puts(seq, "address mask n neigh neigh neigh\n");
1116 else {
1117 const struct rose_node *rose_node = v;
1118 /* if (rose_node->loopback) {
1119 seq_printf(seq, "%-10s %04d 1 loopback\n",
1120 rose2asc(rsbuf, &rose_node->address),
1121 rose_node->mask);
1122 } else { */
1123 seq_printf(seq, "%-10s %04d %d",
1124 rose2asc(rsbuf, &rose_node->address),
1125 rose_node->mask,
1126 rose_node->count);
1128 for (i = 0; i < rose_node->count; i++)
1129 seq_printf(seq, " %05d",
1130 rose_node->neighbour[i]->number);
1132 seq_puts(seq, "\n");
1133 /* } */
1135 return 0;
1138 static const struct seq_operations rose_node_seqops = {
1139 .start = rose_node_start,
1140 .next = rose_node_next,
1141 .stop = rose_node_stop,
1142 .show = rose_node_show,
1145 static int rose_nodes_open(struct inode *inode, struct file *file)
1147 return seq_open(file, &rose_node_seqops);
1150 const struct file_operations rose_nodes_fops = {
1151 .owner = THIS_MODULE,
1152 .open = rose_nodes_open,
1153 .read = seq_read,
1154 .llseek = seq_lseek,
1155 .release = seq_release,
1158 static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1159 __acquires(rose_neigh_list_lock)
1161 struct rose_neigh *rose_neigh;
1162 int i = 1;
1164 spin_lock_bh(&rose_neigh_list_lock);
1165 if (*pos == 0)
1166 return SEQ_START_TOKEN;
1168 for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1169 rose_neigh = rose_neigh->next, ++i);
1171 return (i == *pos) ? rose_neigh : NULL;
1174 static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1176 ++*pos;
1178 return (v == SEQ_START_TOKEN) ? rose_neigh_list
1179 : ((struct rose_neigh *)v)->next;
1182 static void rose_neigh_stop(struct seq_file *seq, void *v)
1183 __releases(rose_neigh_list_lock)
1185 spin_unlock_bh(&rose_neigh_list_lock);
1188 static int rose_neigh_show(struct seq_file *seq, void *v)
1190 char buf[11];
1191 int i;
1193 if (v == SEQ_START_TOKEN)
1194 seq_puts(seq,
1195 "addr callsign dev count use mode restart t0 tf digipeaters\n");
1196 else {
1197 struct rose_neigh *rose_neigh = v;
1199 /* if (!rose_neigh->loopback) { */
1200 seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
1201 rose_neigh->number,
1202 (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
1203 rose_neigh->dev ? rose_neigh->dev->name : "???",
1204 rose_neigh->count,
1205 rose_neigh->use,
1206 (rose_neigh->dce_mode) ? "DCE" : "DTE",
1207 (rose_neigh->restarted) ? "yes" : "no",
1208 ax25_display_timer(&rose_neigh->t0timer) / HZ,
1209 ax25_display_timer(&rose_neigh->ftimer) / HZ);
1211 if (rose_neigh->digipeat != NULL) {
1212 for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1213 seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
1216 seq_puts(seq, "\n");
1218 return 0;
1222 static const struct seq_operations rose_neigh_seqops = {
1223 .start = rose_neigh_start,
1224 .next = rose_neigh_next,
1225 .stop = rose_neigh_stop,
1226 .show = rose_neigh_show,
1229 static int rose_neigh_open(struct inode *inode, struct file *file)
1231 return seq_open(file, &rose_neigh_seqops);
1234 const struct file_operations rose_neigh_fops = {
1235 .owner = THIS_MODULE,
1236 .open = rose_neigh_open,
1237 .read = seq_read,
1238 .llseek = seq_lseek,
1239 .release = seq_release,
1243 static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1244 __acquires(rose_route_list_lock)
1246 struct rose_route *rose_route;
1247 int i = 1;
1249 spin_lock_bh(&rose_route_list_lock);
1250 if (*pos == 0)
1251 return SEQ_START_TOKEN;
1253 for (rose_route = rose_route_list; rose_route && i < *pos;
1254 rose_route = rose_route->next, ++i);
1256 return (i == *pos) ? rose_route : NULL;
1259 static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1261 ++*pos;
1263 return (v == SEQ_START_TOKEN) ? rose_route_list
1264 : ((struct rose_route *)v)->next;
1267 static void rose_route_stop(struct seq_file *seq, void *v)
1268 __releases(rose_route_list_lock)
1270 spin_unlock_bh(&rose_route_list_lock);
1273 static int rose_route_show(struct seq_file *seq, void *v)
1275 char buf[11], rsbuf[11];
1277 if (v == SEQ_START_TOKEN)
1278 seq_puts(seq,
1279 "lci address callsign neigh <-> lci address callsign neigh\n");
1280 else {
1281 struct rose_route *rose_route = v;
1283 if (rose_route->neigh1)
1284 seq_printf(seq,
1285 "%3.3X %-10s %-9s %05d ",
1286 rose_route->lci1,
1287 rose2asc(rsbuf, &rose_route->src_addr),
1288 ax2asc(buf, &rose_route->src_call),
1289 rose_route->neigh1->number);
1290 else
1291 seq_puts(seq,
1292 "000 * * 00000 ");
1294 if (rose_route->neigh2)
1295 seq_printf(seq,
1296 "%3.3X %-10s %-9s %05d\n",
1297 rose_route->lci2,
1298 rose2asc(rsbuf, &rose_route->dest_addr),
1299 ax2asc(buf, &rose_route->dest_call),
1300 rose_route->neigh2->number);
1301 else
1302 seq_puts(seq,
1303 "000 * * 00000\n");
1305 return 0;
1308 static const struct seq_operations rose_route_seqops = {
1309 .start = rose_route_start,
1310 .next = rose_route_next,
1311 .stop = rose_route_stop,
1312 .show = rose_route_show,
1315 static int rose_route_open(struct inode *inode, struct file *file)
1317 return seq_open(file, &rose_route_seqops);
1320 const struct file_operations rose_routes_fops = {
1321 .owner = THIS_MODULE,
1322 .open = rose_route_open,
1323 .read = seq_read,
1324 .llseek = seq_lseek,
1325 .release = seq_release,
1328 #endif /* CONFIG_PROC_FS */
1331 * Release all memory associated with ROSE routing structures.
1333 void __exit rose_rt_free(void)
1335 struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1336 struct rose_node *t, *rose_node = rose_node_list;
1337 struct rose_route *u, *rose_route = rose_route_list;
1339 while (rose_neigh != NULL) {
1340 s = rose_neigh;
1341 rose_neigh = rose_neigh->next;
1343 rose_remove_neigh(s);
1346 while (rose_node != NULL) {
1347 t = rose_node;
1348 rose_node = rose_node->next;
1350 rose_remove_node(t);
1353 while (rose_route != NULL) {
1354 u = rose_route;
1355 rose_route = rose_route->next;
1357 rose_remove_route(u);