i2c-algo-pca: Drop duplicate variable
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / rose / rose_route.c
blob70a0b3b4b4d2903b7bd74d1e783e819a561c32ce
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 <net/ax25.h>
20 #include <linux/inet.h>
21 #include <linux/netdevice.h>
22 #include <net/arp.h>
23 #include <linux/if_arp.h>
24 #include <linux/skbuff.h>
25 #include <net/sock.h>
26 #include <net/tcp_states.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <linux/fcntl.h>
30 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
31 #include <linux/mm.h>
32 #include <linux/interrupt.h>
33 #include <linux/notifier.h>
34 #include <linux/netfilter.h>
35 #include <linux/init.h>
36 #include <net/rose.h>
37 #include <linux/seq_file.h>
39 static unsigned int rose_neigh_no = 1;
41 static struct rose_node *rose_node_list;
42 static DEFINE_SPINLOCK(rose_node_list_lock);
43 static struct rose_neigh *rose_neigh_list;
44 static DEFINE_SPINLOCK(rose_neigh_list_lock);
45 static struct rose_route *rose_route_list;
46 static DEFINE_SPINLOCK(rose_route_list_lock);
48 struct rose_neigh *rose_loopback_neigh;
51 * Add a new route to a node, and in the process add the node and the
52 * neighbour if it is new.
54 static int __must_check rose_add_node(struct rose_route_struct *rose_route,
55 struct net_device *dev)
57 struct rose_node *rose_node, *rose_tmpn, *rose_tmpp;
58 struct rose_neigh *rose_neigh;
59 int i, res = 0;
61 spin_lock_bh(&rose_node_list_lock);
62 spin_lock_bh(&rose_neigh_list_lock);
64 rose_node = rose_node_list;
65 while (rose_node != NULL) {
66 if ((rose_node->mask == rose_route->mask) &&
67 (rosecmpm(&rose_route->address, &rose_node->address,
68 rose_route->mask) == 0))
69 break;
70 rose_node = rose_node->next;
73 if (rose_node != NULL && rose_node->loopback) {
74 res = -EINVAL;
75 goto out;
78 rose_neigh = rose_neigh_list;
79 while (rose_neigh != NULL) {
80 if (ax25cmp(&rose_route->neighbour,
81 &rose_neigh->callsign) == 0 &&
82 rose_neigh->dev == dev)
83 break;
84 rose_neigh = rose_neigh->next;
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;
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;
105 skb_queue_head_init(&rose_neigh->queue);
107 init_timer(&rose_neigh->ftimer);
108 init_timer(&rose_neigh->t0timer);
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;
117 rose_neigh->digipeat->ndigi = rose_route->ndigis;
118 rose_neigh->digipeat->lastrepeat = -1;
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;
127 rose_neigh->next = rose_neigh_list;
128 rose_neigh_list = rose_neigh;
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.
138 if (rose_node == NULL) {
139 rose_tmpn = rose_node_list;
140 rose_tmpp = NULL;
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;
151 /* create new node */
152 rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
153 if (rose_node == NULL) {
154 res = -ENOMEM;
155 goto out;
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;
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;
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;
181 rose_neigh->count++;
183 goto out;
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++;
193 out:
194 spin_unlock_bh(&rose_neigh_list_lock);
195 spin_unlock_bh(&rose_node_list_lock);
197 return res;
201 * Caller is holding rose_node_list_lock.
203 static void rose_remove_node(struct rose_node *rose_node)
205 struct rose_node *s;
207 if ((s = rose_node_list) == rose_node) {
208 rose_node_list = rose_node->next;
209 kfree(rose_node);
210 return;
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;
220 s = s->next;
225 * Caller is holding rose_neigh_list_lock.
227 static void rose_remove_neigh(struct rose_neigh *rose_neigh)
229 struct rose_neigh *s;
231 rose_stop_ftimer(rose_neigh);
232 rose_stop_t0timer(rose_neigh);
234 skb_queue_purge(&rose_neigh->queue);
236 if ((s = rose_neigh_list) == rose_neigh) {
237 rose_neigh_list = rose_neigh->next;
238 if (rose_neigh->ax25)
239 ax25_cb_put(rose_neigh->ax25);
240 kfree(rose_neigh->digipeat);
241 kfree(rose_neigh);
242 return;
245 while (s != NULL && s->next != NULL) {
246 if (s->next == rose_neigh) {
247 s->next = rose_neigh->next;
248 if (rose_neigh->ax25)
249 ax25_cb_put(rose_neigh->ax25);
250 kfree(rose_neigh->digipeat);
251 kfree(rose_neigh);
252 return;
255 s = s->next;
260 * Caller is holding rose_route_list_lock.
262 static void rose_remove_route(struct rose_route *rose_route)
264 struct rose_route *s;
266 if (rose_route->neigh1 != NULL)
267 rose_route->neigh1->use--;
269 if (rose_route->neigh2 != NULL)
270 rose_route->neigh2->use--;
272 if ((s = rose_route_list) == rose_route) {
273 rose_route_list = rose_route->next;
274 kfree(rose_route);
275 return;
278 while (s != NULL && s->next != NULL) {
279 if (s->next == rose_route) {
280 s->next = rose_route->next;
281 kfree(rose_route);
282 return;
285 s = s->next;
290 * "Delete" a node. Strictly speaking remove a route to a node. The node
291 * is only deleted if no routes are left to it.
293 static int rose_del_node(struct rose_route_struct *rose_route,
294 struct net_device *dev)
296 struct rose_node *rose_node;
297 struct rose_neigh *rose_neigh;
298 int i, err = 0;
300 spin_lock_bh(&rose_node_list_lock);
301 spin_lock_bh(&rose_neigh_list_lock);
303 rose_node = rose_node_list;
304 while (rose_node != NULL) {
305 if ((rose_node->mask == rose_route->mask) &&
306 (rosecmpm(&rose_route->address, &rose_node->address,
307 rose_route->mask) == 0))
308 break;
309 rose_node = rose_node->next;
312 if (rose_node == NULL || rose_node->loopback) {
313 err = -EINVAL;
314 goto out;
317 rose_neigh = rose_neigh_list;
318 while (rose_neigh != NULL) {
319 if (ax25cmp(&rose_route->neighbour,
320 &rose_neigh->callsign) == 0 &&
321 rose_neigh->dev == dev)
322 break;
323 rose_neigh = rose_neigh->next;
326 if (rose_neigh == NULL) {
327 err = -EINVAL;
328 goto out;
331 for (i = 0; i < rose_node->count; i++) {
332 if (rose_node->neighbour[i] == rose_neigh) {
333 rose_neigh->count--;
335 if (rose_neigh->count == 0 && rose_neigh->use == 0)
336 rose_remove_neigh(rose_neigh);
338 rose_node->count--;
340 if (rose_node->count == 0) {
341 rose_remove_node(rose_node);
342 } else {
343 switch (i) {
344 case 0:
345 rose_node->neighbour[0] =
346 rose_node->neighbour[1];
347 case 1:
348 rose_node->neighbour[1] =
349 rose_node->neighbour[2];
350 case 2:
351 break;
354 goto out;
357 err = -EINVAL;
359 out:
360 spin_unlock_bh(&rose_neigh_list_lock);
361 spin_unlock_bh(&rose_node_list_lock);
363 return err;
367 * Add the loopback neighbour.
369 void rose_add_loopback_neigh(void)
371 struct rose_neigh *sn;
373 rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_KERNEL);
374 if (!rose_loopback_neigh)
375 return;
376 sn = rose_loopback_neigh;
378 sn->callsign = null_ax25_address;
379 sn->digipeat = NULL;
380 sn->ax25 = NULL;
381 sn->dev = NULL;
382 sn->count = 0;
383 sn->use = 0;
384 sn->dce_mode = 1;
385 sn->loopback = 1;
386 sn->number = rose_neigh_no++;
387 sn->restarted = 1;
389 skb_queue_head_init(&sn->queue);
391 init_timer(&sn->ftimer);
392 init_timer(&sn->t0timer);
394 spin_lock_bh(&rose_neigh_list_lock);
395 sn->next = rose_neigh_list;
396 rose_neigh_list = sn;
397 spin_unlock_bh(&rose_neigh_list_lock);
401 * Add a loopback node.
403 int rose_add_loopback_node(rose_address *address)
405 struct rose_node *rose_node;
406 int err = 0;
408 spin_lock_bh(&rose_node_list_lock);
410 rose_node = rose_node_list;
411 while (rose_node != NULL) {
412 if ((rose_node->mask == 10) &&
413 (rosecmpm(address, &rose_node->address, 10) == 0) &&
414 rose_node->loopback)
415 break;
416 rose_node = rose_node->next;
419 if (rose_node != NULL)
420 goto out;
422 if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
423 err = -ENOMEM;
424 goto out;
427 rose_node->address = *address;
428 rose_node->mask = 10;
429 rose_node->count = 1;
430 rose_node->loopback = 1;
431 rose_node->neighbour[0] = rose_loopback_neigh;
433 /* Insert at the head of list. Address is always mask=10 */
434 rose_node->next = rose_node_list;
435 rose_node_list = rose_node;
437 rose_loopback_neigh->count++;
439 out:
440 spin_unlock_bh(&rose_node_list_lock);
442 return err;
446 * Delete a loopback node.
448 void rose_del_loopback_node(rose_address *address)
450 struct rose_node *rose_node;
452 spin_lock_bh(&rose_node_list_lock);
454 rose_node = rose_node_list;
455 while (rose_node != NULL) {
456 if ((rose_node->mask == 10) &&
457 (rosecmpm(address, &rose_node->address, 10) == 0) &&
458 rose_node->loopback)
459 break;
460 rose_node = rose_node->next;
463 if (rose_node == NULL)
464 goto out;
466 rose_remove_node(rose_node);
468 rose_loopback_neigh->count--;
470 out:
471 spin_unlock_bh(&rose_node_list_lock);
475 * A device has been removed. Remove its routes and neighbours.
477 void rose_rt_device_down(struct net_device *dev)
479 struct rose_neigh *s, *rose_neigh;
480 struct rose_node *t, *rose_node;
481 int i;
483 spin_lock_bh(&rose_node_list_lock);
484 spin_lock_bh(&rose_neigh_list_lock);
485 rose_neigh = rose_neigh_list;
486 while (rose_neigh != NULL) {
487 s = rose_neigh;
488 rose_neigh = rose_neigh->next;
490 if (s->dev != dev)
491 continue;
493 rose_node = rose_node_list;
495 while (rose_node != NULL) {
496 t = rose_node;
497 rose_node = rose_node->next;
499 for (i = 0; i < t->count; i++) {
500 if (t->neighbour[i] != s)
501 continue;
503 t->count--;
505 switch (i) {
506 case 0:
507 t->neighbour[0] = t->neighbour[1];
508 case 1:
509 t->neighbour[1] = t->neighbour[2];
510 case 2:
511 break;
515 if (t->count <= 0)
516 rose_remove_node(t);
519 rose_remove_neigh(s);
521 spin_unlock_bh(&rose_neigh_list_lock);
522 spin_unlock_bh(&rose_node_list_lock);
525 #if 0 /* Currently unused */
527 * A device has been removed. Remove its links.
529 void rose_route_device_down(struct net_device *dev)
531 struct rose_route *s, *rose_route;
533 spin_lock_bh(&rose_route_list_lock);
534 rose_route = rose_route_list;
535 while (rose_route != NULL) {
536 s = rose_route;
537 rose_route = rose_route->next;
539 if (s->neigh1->dev == dev || s->neigh2->dev == dev)
540 rose_remove_route(s);
542 spin_unlock_bh(&rose_route_list_lock);
544 #endif
547 * Clear all nodes and neighbours out, except for neighbours with
548 * active connections going through them.
549 * Do not clear loopback neighbour and nodes.
551 static int rose_clear_routes(void)
553 struct rose_neigh *s, *rose_neigh;
554 struct rose_node *t, *rose_node;
556 spin_lock_bh(&rose_node_list_lock);
557 spin_lock_bh(&rose_neigh_list_lock);
559 rose_neigh = rose_neigh_list;
560 rose_node = rose_node_list;
562 while (rose_node != NULL) {
563 t = rose_node;
564 rose_node = rose_node->next;
565 if (!t->loopback)
566 rose_remove_node(t);
569 while (rose_neigh != NULL) {
570 s = rose_neigh;
571 rose_neigh = rose_neigh->next;
573 if (s->use == 0 && !s->loopback) {
574 s->count = 0;
575 rose_remove_neigh(s);
579 spin_unlock_bh(&rose_neigh_list_lock);
580 spin_unlock_bh(&rose_node_list_lock);
582 return 0;
586 * Check that the device given is a valid AX.25 interface that is "up".
587 * called whith RTNL
589 static struct net_device *rose_ax25_dev_find(char *devname)
591 struct net_device *dev;
593 if ((dev = __dev_get_by_name(&init_net, devname)) == NULL)
594 return NULL;
596 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
597 return dev;
599 return NULL;
603 * Find the first active ROSE device, usually "rose0".
605 struct net_device *rose_dev_first(void)
607 struct net_device *dev, *first = NULL;
609 rcu_read_lock();
610 for_each_netdev_rcu(&init_net, dev) {
611 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
612 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
613 first = dev;
615 rcu_read_unlock();
617 return first;
621 * Find the ROSE device for the given address.
623 struct net_device *rose_dev_get(rose_address *addr)
625 struct net_device *dev;
627 rcu_read_lock();
628 for_each_netdev_rcu(&init_net, dev) {
629 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
630 dev_hold(dev);
631 goto out;
634 dev = NULL;
635 out:
636 rcu_read_unlock();
637 return dev;
640 static int rose_dev_exists(rose_address *addr)
642 struct net_device *dev;
644 rcu_read_lock();
645 for_each_netdev_rcu(&init_net, dev) {
646 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
647 goto out;
649 dev = NULL;
650 out:
651 rcu_read_unlock();
652 return dev != NULL;
658 struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
660 struct rose_route *rose_route;
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;
667 return NULL;
671 * Find a neighbour or a route given a ROSE address.
673 struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
674 unsigned char *diagnostic, int new)
676 struct rose_neigh *res = NULL;
677 struct rose_node *node;
678 int failed = 0;
679 int i;
681 if (!new) 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 (new) {
686 if (node->neighbour[i]->restarted) {
687 res = node->neighbour[i];
688 goto out;
691 else {
692 if (!rose_ftimer_running(node->neighbour[i])) {
693 res = node->neighbour[i];
694 goto out;
695 } else
696 failed = 1;
702 if (failed) {
703 *cause = ROSE_OUT_OF_ORDER;
704 *diagnostic = 0;
705 } else {
706 *cause = ROSE_NOT_OBTAINABLE;
707 *diagnostic = 0;
710 out:
711 if (!new) spin_unlock_bh(&rose_node_list_lock);
713 return res;
717 * Handle the ioctls that control the routing functions.
719 int rose_rt_ioctl(unsigned int cmd, void __user *arg)
721 struct rose_route_struct rose_route;
722 struct net_device *dev;
723 int err;
725 switch (cmd) {
726 case SIOCADDRT:
727 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
728 return -EFAULT;
729 if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
730 return -EINVAL;
731 if (rose_dev_exists(&rose_route.address)) /* Can't add routes to ourself */
732 return -EINVAL;
733 if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
734 return -EINVAL;
735 if (rose_route.ndigis > AX25_MAX_DIGIS)
736 return -EINVAL;
737 err = rose_add_node(&rose_route, dev);
738 return err;
740 case SIOCDELRT:
741 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
742 return -EFAULT;
743 if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
744 return -EINVAL;
745 err = rose_del_node(&rose_route, dev);
746 return err;
748 case SIOCRSCLRRT:
749 return rose_clear_routes();
751 default:
752 return -EINVAL;
755 return 0;
758 static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
760 struct rose_route *rose_route, *s;
762 rose_neigh->restarted = 0;
764 rose_stop_t0timer(rose_neigh);
765 rose_start_ftimer(rose_neigh);
767 skb_queue_purge(&rose_neigh->queue);
769 spin_lock_bh(&rose_route_list_lock);
771 rose_route = rose_route_list;
773 while (rose_route != NULL) {
774 if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
775 (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
776 (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
777 s = rose_route->next;
778 rose_remove_route(rose_route);
779 rose_route = s;
780 continue;
783 if (rose_route->neigh1 == rose_neigh) {
784 rose_route->neigh1->use--;
785 rose_route->neigh1 = NULL;
786 rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
789 if (rose_route->neigh2 == rose_neigh) {
790 rose_route->neigh2->use--;
791 rose_route->neigh2 = NULL;
792 rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
795 rose_route = rose_route->next;
797 spin_unlock_bh(&rose_route_list_lock);
801 * A level 2 link has timed out, therefore it appears to be a poor link,
802 * then don't use that neighbour until it is reset. Blow away all through
803 * routes and connections using this route.
805 void rose_link_failed(ax25_cb *ax25, int reason)
807 struct rose_neigh *rose_neigh;
809 spin_lock_bh(&rose_neigh_list_lock);
810 rose_neigh = rose_neigh_list;
811 while (rose_neigh != NULL) {
812 if (rose_neigh->ax25 == ax25)
813 break;
814 rose_neigh = rose_neigh->next;
817 if (rose_neigh != NULL) {
818 rose_neigh->ax25 = NULL;
819 ax25_cb_put(ax25);
821 rose_del_route_by_neigh(rose_neigh);
822 rose_kill_by_neigh(rose_neigh);
824 spin_unlock_bh(&rose_neigh_list_lock);
828 * A device has been "downed" remove its link status. Blow away all
829 * through routes and connections that use this device.
831 void rose_link_device_down(struct net_device *dev)
833 struct rose_neigh *rose_neigh;
835 for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
836 if (rose_neigh->dev == dev) {
837 rose_del_route_by_neigh(rose_neigh);
838 rose_kill_by_neigh(rose_neigh);
844 * Route a frame to an appropriate AX.25 connection.
846 int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
848 struct rose_neigh *rose_neigh, *new_neigh;
849 struct rose_route *rose_route;
850 struct rose_facilities_struct facilities;
851 rose_address *src_addr, *dest_addr;
852 struct sock *sk;
853 unsigned short frametype;
854 unsigned int lci, new_lci;
855 unsigned char cause, diagnostic;
856 struct net_device *dev;
857 int len, res = 0;
858 char buf[11];
860 #if 0
861 if (call_in_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT)
862 return res;
863 #endif
865 frametype = skb->data[2];
866 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
867 src_addr = (rose_address *)(skb->data + 9);
868 dest_addr = (rose_address *)(skb->data + 4);
870 spin_lock_bh(&rose_neigh_list_lock);
871 spin_lock_bh(&rose_route_list_lock);
873 rose_neigh = rose_neigh_list;
874 while (rose_neigh != NULL) {
875 if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
876 ax25->ax25_dev->dev == rose_neigh->dev)
877 break;
878 rose_neigh = rose_neigh->next;
881 if (rose_neigh == NULL) {
882 printk("rose_route : unknown neighbour or device %s\n",
883 ax2asc(buf, &ax25->dest_addr));
884 goto out;
888 * Obviously the link is working, halt the ftimer.
890 rose_stop_ftimer(rose_neigh);
893 * LCI of zero is always for us, and its always a restart
894 * frame.
896 if (lci == 0) {
897 rose_link_rx_restart(skb, rose_neigh, frametype);
898 goto out;
902 * Find an existing socket.
904 if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
905 if (frametype == ROSE_CALL_REQUEST) {
906 struct rose_sock *rose = rose_sk(sk);
908 /* Remove an existing unused socket */
909 rose_clear_queues(sk);
910 rose->cause = ROSE_NETWORK_CONGESTION;
911 rose->diagnostic = 0;
912 rose->neighbour->use--;
913 rose->neighbour = NULL;
914 rose->lci = 0;
915 rose->state = ROSE_STATE_0;
916 sk->sk_state = TCP_CLOSE;
917 sk->sk_err = 0;
918 sk->sk_shutdown |= SEND_SHUTDOWN;
919 if (!sock_flag(sk, SOCK_DEAD)) {
920 sk->sk_state_change(sk);
921 sock_set_flag(sk, SOCK_DEAD);
924 else {
925 skb_reset_transport_header(skb);
926 res = rose_process_rx_frame(sk, skb);
927 goto out;
932 * Is is a Call Request and is it for us ?
934 if (frametype == ROSE_CALL_REQUEST)
935 if ((dev = rose_dev_get(dest_addr)) != NULL) {
936 res = rose_rx_call_request(skb, dev, rose_neigh, lci);
937 dev_put(dev);
938 goto out;
941 if (!sysctl_rose_routing_control) {
942 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
943 goto out;
947 * Route it to the next in line if we have an entry for it.
949 rose_route = rose_route_list;
950 while (rose_route != NULL) {
951 if (rose_route->lci1 == lci &&
952 rose_route->neigh1 == rose_neigh) {
953 if (frametype == ROSE_CALL_REQUEST) {
954 /* F6FBB - Remove an existing unused route */
955 rose_remove_route(rose_route);
956 break;
957 } else if (rose_route->neigh2 != NULL) {
958 skb->data[0] &= 0xF0;
959 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
960 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
961 rose_transmit_link(skb, rose_route->neigh2);
962 if (frametype == ROSE_CLEAR_CONFIRMATION)
963 rose_remove_route(rose_route);
964 res = 1;
965 goto out;
966 } else {
967 if (frametype == ROSE_CLEAR_CONFIRMATION)
968 rose_remove_route(rose_route);
969 goto out;
972 if (rose_route->lci2 == lci &&
973 rose_route->neigh2 == rose_neigh) {
974 if (frametype == ROSE_CALL_REQUEST) {
975 /* F6FBB - Remove an existing unused route */
976 rose_remove_route(rose_route);
977 break;
978 } else if (rose_route->neigh1 != NULL) {
979 skb->data[0] &= 0xF0;
980 skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
981 skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
982 rose_transmit_link(skb, rose_route->neigh1);
983 if (frametype == ROSE_CLEAR_CONFIRMATION)
984 rose_remove_route(rose_route);
985 res = 1;
986 goto out;
987 } else {
988 if (frametype == ROSE_CLEAR_CONFIRMATION)
989 rose_remove_route(rose_route);
990 goto out;
993 rose_route = rose_route->next;
997 * We know that:
998 * 1. The frame isn't for us,
999 * 2. It isn't "owned" by any existing route.
1001 if (frametype != ROSE_CALL_REQUEST) { /* XXX */
1002 res = 0;
1003 goto out;
1006 len = (((skb->data[3] >> 4) & 0x0F) + 1) >> 1;
1007 len += (((skb->data[3] >> 0) & 0x0F) + 1) >> 1;
1009 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1011 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
1012 rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1013 goto out;
1017 * Check for routing loops.
1019 rose_route = rose_route_list;
1020 while (rose_route != NULL) {
1021 if (rose_route->rand == facilities.rand &&
1022 rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1023 ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1024 ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1025 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1026 goto out;
1028 rose_route = rose_route->next;
1031 if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic, 1)) == NULL) {
1032 rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1033 goto out;
1036 if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1037 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1038 goto out;
1041 if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1042 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1043 goto out;
1046 rose_route->lci1 = lci;
1047 rose_route->src_addr = *src_addr;
1048 rose_route->dest_addr = *dest_addr;
1049 rose_route->src_call = facilities.dest_call;
1050 rose_route->dest_call = facilities.source_call;
1051 rose_route->rand = facilities.rand;
1052 rose_route->neigh1 = rose_neigh;
1053 rose_route->lci2 = new_lci;
1054 rose_route->neigh2 = new_neigh;
1056 rose_route->neigh1->use++;
1057 rose_route->neigh2->use++;
1059 rose_route->next = rose_route_list;
1060 rose_route_list = rose_route;
1062 skb->data[0] &= 0xF0;
1063 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1064 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
1066 rose_transmit_link(skb, rose_route->neigh2);
1067 res = 1;
1069 out:
1070 spin_unlock_bh(&rose_route_list_lock);
1071 spin_unlock_bh(&rose_neigh_list_lock);
1073 return res;
1076 #ifdef CONFIG_PROC_FS
1078 static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1079 __acquires(rose_node_list_lock)
1081 struct rose_node *rose_node;
1082 int i = 1;
1084 spin_lock_bh(&rose_node_list_lock);
1085 if (*pos == 0)
1086 return SEQ_START_TOKEN;
1088 for (rose_node = rose_node_list; rose_node && i < *pos;
1089 rose_node = rose_node->next, ++i);
1091 return (i == *pos) ? rose_node : NULL;
1094 static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1096 ++*pos;
1098 return (v == SEQ_START_TOKEN) ? rose_node_list
1099 : ((struct rose_node *)v)->next;
1102 static void rose_node_stop(struct seq_file *seq, void *v)
1103 __releases(rose_node_list_lock)
1105 spin_unlock_bh(&rose_node_list_lock);
1108 static int rose_node_show(struct seq_file *seq, void *v)
1110 char rsbuf[11];
1111 int i;
1113 if (v == SEQ_START_TOKEN)
1114 seq_puts(seq, "address mask n neigh neigh neigh\n");
1115 else {
1116 const struct rose_node *rose_node = v;
1117 /* if (rose_node->loopback) {
1118 seq_printf(seq, "%-10s %04d 1 loopback\n",
1119 rose2asc(rsbuf, &rose_node->address),
1120 rose_node->mask);
1121 } else { */
1122 seq_printf(seq, "%-10s %04d %d",
1123 rose2asc(rsbuf, &rose_node->address),
1124 rose_node->mask,
1125 rose_node->count);
1127 for (i = 0; i < rose_node->count; i++)
1128 seq_printf(seq, " %05d",
1129 rose_node->neighbour[i]->number);
1131 seq_puts(seq, "\n");
1132 /* } */
1134 return 0;
1137 static const struct seq_operations rose_node_seqops = {
1138 .start = rose_node_start,
1139 .next = rose_node_next,
1140 .stop = rose_node_stop,
1141 .show = rose_node_show,
1144 static int rose_nodes_open(struct inode *inode, struct file *file)
1146 return seq_open(file, &rose_node_seqops);
1149 const struct file_operations rose_nodes_fops = {
1150 .owner = THIS_MODULE,
1151 .open = rose_nodes_open,
1152 .read = seq_read,
1153 .llseek = seq_lseek,
1154 .release = seq_release,
1157 static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1158 __acquires(rose_neigh_list_lock)
1160 struct rose_neigh *rose_neigh;
1161 int i = 1;
1163 spin_lock_bh(&rose_neigh_list_lock);
1164 if (*pos == 0)
1165 return SEQ_START_TOKEN;
1167 for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1168 rose_neigh = rose_neigh->next, ++i);
1170 return (i == *pos) ? rose_neigh : NULL;
1173 static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1175 ++*pos;
1177 return (v == SEQ_START_TOKEN) ? rose_neigh_list
1178 : ((struct rose_neigh *)v)->next;
1181 static void rose_neigh_stop(struct seq_file *seq, void *v)
1182 __releases(rose_neigh_list_lock)
1184 spin_unlock_bh(&rose_neigh_list_lock);
1187 static int rose_neigh_show(struct seq_file *seq, void *v)
1189 char buf[11];
1190 int i;
1192 if (v == SEQ_START_TOKEN)
1193 seq_puts(seq,
1194 "addr callsign dev count use mode restart t0 tf digipeaters\n");
1195 else {
1196 struct rose_neigh *rose_neigh = v;
1198 /* if (!rose_neigh->loopback) { */
1199 seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
1200 rose_neigh->number,
1201 (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
1202 rose_neigh->dev ? rose_neigh->dev->name : "???",
1203 rose_neigh->count,
1204 rose_neigh->use,
1205 (rose_neigh->dce_mode) ? "DCE" : "DTE",
1206 (rose_neigh->restarted) ? "yes" : "no",
1207 ax25_display_timer(&rose_neigh->t0timer) / HZ,
1208 ax25_display_timer(&rose_neigh->ftimer) / HZ);
1210 if (rose_neigh->digipeat != NULL) {
1211 for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1212 seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
1215 seq_puts(seq, "\n");
1217 return 0;
1221 static const struct seq_operations rose_neigh_seqops = {
1222 .start = rose_neigh_start,
1223 .next = rose_neigh_next,
1224 .stop = rose_neigh_stop,
1225 .show = rose_neigh_show,
1228 static int rose_neigh_open(struct inode *inode, struct file *file)
1230 return seq_open(file, &rose_neigh_seqops);
1233 const struct file_operations rose_neigh_fops = {
1234 .owner = THIS_MODULE,
1235 .open = rose_neigh_open,
1236 .read = seq_read,
1237 .llseek = seq_lseek,
1238 .release = seq_release,
1242 static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1243 __acquires(rose_route_list_lock)
1245 struct rose_route *rose_route;
1246 int i = 1;
1248 spin_lock_bh(&rose_route_list_lock);
1249 if (*pos == 0)
1250 return SEQ_START_TOKEN;
1252 for (rose_route = rose_route_list; rose_route && i < *pos;
1253 rose_route = rose_route->next, ++i);
1255 return (i == *pos) ? rose_route : NULL;
1258 static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1260 ++*pos;
1262 return (v == SEQ_START_TOKEN) ? rose_route_list
1263 : ((struct rose_route *)v)->next;
1266 static void rose_route_stop(struct seq_file *seq, void *v)
1267 __releases(rose_route_list_lock)
1269 spin_unlock_bh(&rose_route_list_lock);
1272 static int rose_route_show(struct seq_file *seq, void *v)
1274 char buf[11], rsbuf[11];
1276 if (v == SEQ_START_TOKEN)
1277 seq_puts(seq,
1278 "lci address callsign neigh <-> lci address callsign neigh\n");
1279 else {
1280 struct rose_route *rose_route = v;
1282 if (rose_route->neigh1)
1283 seq_printf(seq,
1284 "%3.3X %-10s %-9s %05d ",
1285 rose_route->lci1,
1286 rose2asc(rsbuf, &rose_route->src_addr),
1287 ax2asc(buf, &rose_route->src_call),
1288 rose_route->neigh1->number);
1289 else
1290 seq_puts(seq,
1291 "000 * * 00000 ");
1293 if (rose_route->neigh2)
1294 seq_printf(seq,
1295 "%3.3X %-10s %-9s %05d\n",
1296 rose_route->lci2,
1297 rose2asc(rsbuf, &rose_route->dest_addr),
1298 ax2asc(buf, &rose_route->dest_call),
1299 rose_route->neigh2->number);
1300 else
1301 seq_puts(seq,
1302 "000 * * 00000\n");
1304 return 0;
1307 static const struct seq_operations rose_route_seqops = {
1308 .start = rose_route_start,
1309 .next = rose_route_next,
1310 .stop = rose_route_stop,
1311 .show = rose_route_show,
1314 static int rose_route_open(struct inode *inode, struct file *file)
1316 return seq_open(file, &rose_route_seqops);
1319 const struct file_operations rose_routes_fops = {
1320 .owner = THIS_MODULE,
1321 .open = rose_route_open,
1322 .read = seq_read,
1323 .llseek = seq_lseek,
1324 .release = seq_release,
1327 #endif /* CONFIG_PROC_FS */
1330 * Release all memory associated with ROSE routing structures.
1332 void __exit rose_rt_free(void)
1334 struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1335 struct rose_node *t, *rose_node = rose_node_list;
1336 struct rose_route *u, *rose_route = rose_route_list;
1338 while (rose_neigh != NULL) {
1339 s = rose_neigh;
1340 rose_neigh = rose_neigh->next;
1342 rose_remove_neigh(s);
1345 while (rose_node != NULL) {
1346 t = rose_node;
1347 rose_node = rose_node->next;
1349 rose_remove_node(t);
1352 while (rose_route != NULL) {
1353 u = rose_route;
1354 rose_route = rose_route->next;
1356 rose_remove_route(u);