[SCSI] dpt_i2o: convert to use the data buffer accessors
[wrt350n-kernel.git] / net / rose / rose_route.c
blob929a784a86d72cef17e10bf98bca7a178a474eae
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, &rose_neigh->callsign) == 0
81 && rose_neigh->dev == dev)
82 break;
83 rose_neigh = rose_neigh->next;
86 if (rose_neigh == NULL) {
87 rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
88 if (rose_neigh == NULL) {
89 res = -ENOMEM;
90 goto out;
93 rose_neigh->callsign = rose_route->neighbour;
94 rose_neigh->digipeat = NULL;
95 rose_neigh->ax25 = NULL;
96 rose_neigh->dev = dev;
97 rose_neigh->count = 0;
98 rose_neigh->use = 0;
99 rose_neigh->dce_mode = 0;
100 rose_neigh->loopback = 0;
101 rose_neigh->number = rose_neigh_no++;
102 rose_neigh->restarted = 0;
104 skb_queue_head_init(&rose_neigh->queue);
106 init_timer(&rose_neigh->ftimer);
107 init_timer(&rose_neigh->t0timer);
109 if (rose_route->ndigis != 0) {
110 if ((rose_neigh->digipeat = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) {
111 kfree(rose_neigh);
112 res = -ENOMEM;
113 goto out;
116 rose_neigh->digipeat->ndigi = rose_route->ndigis;
117 rose_neigh->digipeat->lastrepeat = -1;
119 for (i = 0; i < rose_route->ndigis; i++) {
120 rose_neigh->digipeat->calls[i] =
121 rose_route->digipeaters[i];
122 rose_neigh->digipeat->repeated[i] = 0;
126 rose_neigh->next = rose_neigh_list;
127 rose_neigh_list = rose_neigh;
131 * This is a new node to be inserted into the list. Find where it needs
132 * to be inserted into the list, and insert it. We want to be sure
133 * to order the list in descending order of mask size to ensure that
134 * later when we are searching this list the first match will be the
135 * best match.
137 if (rose_node == NULL) {
138 rose_tmpn = rose_node_list;
139 rose_tmpp = NULL;
141 while (rose_tmpn != NULL) {
142 if (rose_tmpn->mask > rose_route->mask) {
143 rose_tmpp = rose_tmpn;
144 rose_tmpn = rose_tmpn->next;
145 } else {
146 break;
150 /* create new node */
151 rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
152 if (rose_node == NULL) {
153 res = -ENOMEM;
154 goto out;
157 rose_node->address = rose_route->address;
158 rose_node->mask = rose_route->mask;
159 rose_node->count = 1;
160 rose_node->loopback = 0;
161 rose_node->neighbour[0] = rose_neigh;
163 if (rose_tmpn == NULL) {
164 if (rose_tmpp == NULL) { /* Empty list */
165 rose_node_list = rose_node;
166 rose_node->next = NULL;
167 } else {
168 rose_tmpp->next = rose_node;
169 rose_node->next = NULL;
171 } else {
172 if (rose_tmpp == NULL) { /* 1st node */
173 rose_node->next = rose_node_list;
174 rose_node_list = rose_node;
175 } else {
176 rose_tmpp->next = rose_node;
177 rose_node->next = rose_tmpn;
180 rose_neigh->count++;
182 goto out;
185 /* We have space, slot it in */
186 if (rose_node->count < 3) {
187 rose_node->neighbour[rose_node->count] = rose_neigh;
188 rose_node->count++;
189 rose_neigh->count++;
192 out:
193 spin_unlock_bh(&rose_neigh_list_lock);
194 spin_unlock_bh(&rose_node_list_lock);
196 return res;
200 * Caller is holding rose_node_list_lock.
202 static void rose_remove_node(struct rose_node *rose_node)
204 struct rose_node *s;
206 if ((s = rose_node_list) == rose_node) {
207 rose_node_list = rose_node->next;
208 kfree(rose_node);
209 return;
212 while (s != NULL && s->next != NULL) {
213 if (s->next == rose_node) {
214 s->next = rose_node->next;
215 kfree(rose_node);
216 return;
219 s = s->next;
224 * Caller is holding rose_neigh_list_lock.
226 static void rose_remove_neigh(struct rose_neigh *rose_neigh)
228 struct rose_neigh *s;
230 rose_stop_ftimer(rose_neigh);
231 rose_stop_t0timer(rose_neigh);
233 skb_queue_purge(&rose_neigh->queue);
235 if ((s = rose_neigh_list) == rose_neigh) {
236 rose_neigh_list = rose_neigh->next;
237 kfree(rose_neigh->digipeat);
238 kfree(rose_neigh);
239 return;
242 while (s != NULL && s->next != NULL) {
243 if (s->next == rose_neigh) {
244 s->next = rose_neigh->next;
245 kfree(rose_neigh->digipeat);
246 kfree(rose_neigh);
247 return;
250 s = s->next;
255 * Caller is holding rose_route_list_lock.
257 static void rose_remove_route(struct rose_route *rose_route)
259 struct rose_route *s;
261 if (rose_route->neigh1 != NULL)
262 rose_route->neigh1->use--;
264 if (rose_route->neigh2 != NULL)
265 rose_route->neigh2->use--;
267 if ((s = rose_route_list) == rose_route) {
268 rose_route_list = rose_route->next;
269 kfree(rose_route);
270 return;
273 while (s != NULL && s->next != NULL) {
274 if (s->next == rose_route) {
275 s->next = rose_route->next;
276 kfree(rose_route);
277 return;
280 s = s->next;
285 * "Delete" a node. Strictly speaking remove a route to a node. The node
286 * is only deleted if no routes are left to it.
288 static int rose_del_node(struct rose_route_struct *rose_route,
289 struct net_device *dev)
291 struct rose_node *rose_node;
292 struct rose_neigh *rose_neigh;
293 int i, err = 0;
295 spin_lock_bh(&rose_node_list_lock);
296 spin_lock_bh(&rose_neigh_list_lock);
298 rose_node = rose_node_list;
299 while (rose_node != NULL) {
300 if ((rose_node->mask == rose_route->mask) &&
301 (rosecmpm(&rose_route->address, &rose_node->address,
302 rose_route->mask) == 0))
303 break;
304 rose_node = rose_node->next;
307 if (rose_node == NULL || rose_node->loopback) {
308 err = -EINVAL;
309 goto out;
312 rose_neigh = rose_neigh_list;
313 while (rose_neigh != NULL) {
314 if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0
315 && rose_neigh->dev == dev)
316 break;
317 rose_neigh = rose_neigh->next;
320 if (rose_neigh == NULL) {
321 err = -EINVAL;
322 goto out;
325 for (i = 0; i < rose_node->count; i++) {
326 if (rose_node->neighbour[i] == rose_neigh) {
327 rose_neigh->count--;
329 if (rose_neigh->count == 0 && rose_neigh->use == 0)
330 rose_remove_neigh(rose_neigh);
332 rose_node->count--;
334 if (rose_node->count == 0) {
335 rose_remove_node(rose_node);
336 } else {
337 switch (i) {
338 case 0:
339 rose_node->neighbour[0] =
340 rose_node->neighbour[1];
341 case 1:
342 rose_node->neighbour[1] =
343 rose_node->neighbour[2];
344 case 2:
345 break;
348 goto out;
351 err = -EINVAL;
353 out:
354 spin_unlock_bh(&rose_neigh_list_lock);
355 spin_unlock_bh(&rose_node_list_lock);
357 return err;
361 * Add the loopback neighbour.
363 void rose_add_loopback_neigh(void)
365 struct rose_neigh *sn = &rose_loopback_neigh;
367 sn->callsign = null_ax25_address;
368 sn->digipeat = NULL;
369 sn->ax25 = NULL;
370 sn->dev = NULL;
371 sn->count = 0;
372 sn->use = 0;
373 sn->dce_mode = 1;
374 sn->loopback = 1;
375 sn->number = rose_neigh_no++;
376 sn->restarted = 1;
378 skb_queue_head_init(&sn->queue);
380 init_timer(&sn->ftimer);
381 init_timer(&sn->t0timer);
383 spin_lock_bh(&rose_neigh_list_lock);
384 sn->next = rose_neigh_list;
385 rose_neigh_list = sn;
386 spin_unlock_bh(&rose_neigh_list_lock);
390 * Add a loopback node.
392 int rose_add_loopback_node(rose_address *address)
394 struct rose_node *rose_node;
395 int err = 0;
397 spin_lock_bh(&rose_node_list_lock);
399 rose_node = rose_node_list;
400 while (rose_node != NULL) {
401 if ((rose_node->mask == 10) &&
402 (rosecmpm(address, &rose_node->address, 10) == 0) &&
403 rose_node->loopback)
404 break;
405 rose_node = rose_node->next;
408 if (rose_node != NULL)
409 goto out;
411 if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
412 err = -ENOMEM;
413 goto out;
416 rose_node->address = *address;
417 rose_node->mask = 10;
418 rose_node->count = 1;
419 rose_node->loopback = 1;
420 rose_node->neighbour[0] = &rose_loopback_neigh;
422 /* Insert at the head of list. Address is always mask=10 */
423 rose_node->next = rose_node_list;
424 rose_node_list = rose_node;
426 rose_loopback_neigh.count++;
428 out:
429 spin_unlock_bh(&rose_node_list_lock);
431 return err;
435 * Delete a loopback node.
437 void rose_del_loopback_node(rose_address *address)
439 struct rose_node *rose_node;
441 spin_lock_bh(&rose_node_list_lock);
443 rose_node = rose_node_list;
444 while (rose_node != NULL) {
445 if ((rose_node->mask == 10) &&
446 (rosecmpm(address, &rose_node->address, 10) == 0) &&
447 rose_node->loopback)
448 break;
449 rose_node = rose_node->next;
452 if (rose_node == NULL)
453 goto out;
455 rose_remove_node(rose_node);
457 rose_loopback_neigh.count--;
459 out:
460 spin_unlock_bh(&rose_node_list_lock);
464 * A device has been removed. Remove its routes and neighbours.
466 void rose_rt_device_down(struct net_device *dev)
468 struct rose_neigh *s, *rose_neigh;
469 struct rose_node *t, *rose_node;
470 int i;
472 spin_lock_bh(&rose_node_list_lock);
473 spin_lock_bh(&rose_neigh_list_lock);
474 rose_neigh = rose_neigh_list;
475 while (rose_neigh != NULL) {
476 s = rose_neigh;
477 rose_neigh = rose_neigh->next;
479 if (s->dev != dev)
480 continue;
482 rose_node = rose_node_list;
484 while (rose_node != NULL) {
485 t = rose_node;
486 rose_node = rose_node->next;
488 for (i = 0; i < t->count; i++) {
489 if (t->neighbour[i] != s)
490 continue;
492 t->count--;
494 switch (i) {
495 case 0:
496 t->neighbour[0] = t->neighbour[1];
497 case 1:
498 t->neighbour[1] = t->neighbour[2];
499 case 2:
500 break;
504 if (t->count <= 0)
505 rose_remove_node(t);
508 rose_remove_neigh(s);
510 spin_unlock_bh(&rose_neigh_list_lock);
511 spin_unlock_bh(&rose_node_list_lock);
514 #if 0 /* Currently unused */
516 * A device has been removed. Remove its links.
518 void rose_route_device_down(struct net_device *dev)
520 struct rose_route *s, *rose_route;
522 spin_lock_bh(&rose_route_list_lock);
523 rose_route = rose_route_list;
524 while (rose_route != NULL) {
525 s = rose_route;
526 rose_route = rose_route->next;
528 if (s->neigh1->dev == dev || s->neigh2->dev == dev)
529 rose_remove_route(s);
531 spin_unlock_bh(&rose_route_list_lock);
533 #endif
536 * Clear all nodes and neighbours out, except for neighbours with
537 * active connections going through them.
538 * Do not clear loopback neighbour and nodes.
540 static int rose_clear_routes(void)
542 struct rose_neigh *s, *rose_neigh;
543 struct rose_node *t, *rose_node;
545 spin_lock_bh(&rose_node_list_lock);
546 spin_lock_bh(&rose_neigh_list_lock);
548 rose_neigh = rose_neigh_list;
549 rose_node = rose_node_list;
551 while (rose_node != NULL) {
552 t = rose_node;
553 rose_node = rose_node->next;
554 if (!t->loopback)
555 rose_remove_node(t);
558 while (rose_neigh != NULL) {
559 s = rose_neigh;
560 rose_neigh = rose_neigh->next;
562 if (s->use == 0 && !s->loopback) {
563 s->count = 0;
564 rose_remove_neigh(s);
568 spin_unlock_bh(&rose_neigh_list_lock);
569 spin_unlock_bh(&rose_node_list_lock);
571 return 0;
575 * Check that the device given is a valid AX.25 interface that is "up".
577 static struct net_device *rose_ax25_dev_get(char *devname)
579 struct net_device *dev;
581 if ((dev = dev_get_by_name(devname)) == NULL)
582 return NULL;
584 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
585 return dev;
587 dev_put(dev);
588 return NULL;
592 * Find the first active ROSE device, usually "rose0".
594 struct net_device *rose_dev_first(void)
596 struct net_device *dev, *first = NULL;
598 read_lock(&dev_base_lock);
599 for_each_netdev(dev) {
600 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
601 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
602 first = dev;
604 read_unlock(&dev_base_lock);
606 return first;
610 * Find the ROSE device for the given address.
612 struct net_device *rose_dev_get(rose_address *addr)
614 struct net_device *dev;
616 read_lock(&dev_base_lock);
617 for_each_netdev(dev) {
618 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
619 dev_hold(dev);
620 goto out;
623 dev = NULL;
624 out:
625 read_unlock(&dev_base_lock);
626 return dev;
629 static int rose_dev_exists(rose_address *addr)
631 struct net_device *dev;
633 read_lock(&dev_base_lock);
634 for_each_netdev(dev) {
635 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
636 goto out;
638 dev = NULL;
639 out:
640 read_unlock(&dev_base_lock);
641 return dev != NULL;
647 struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
649 struct rose_route *rose_route;
651 for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
652 if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
653 (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
654 return rose_route;
656 return NULL;
660 * Find a neighbour given a ROSE address.
662 struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
663 unsigned char *diagnostic)
665 struct rose_neigh *res = NULL;
666 struct rose_node *node;
667 int failed = 0;
668 int i;
670 spin_lock_bh(&rose_node_list_lock);
671 for (node = rose_node_list; node != NULL; node = node->next) {
672 if (rosecmpm(addr, &node->address, node->mask) == 0) {
673 for (i = 0; i < node->count; i++) {
674 if (!rose_ftimer_running(node->neighbour[i])) {
675 res = node->neighbour[i];
676 goto out;
677 } else
678 failed = 1;
680 break;
684 if (failed) {
685 *cause = ROSE_OUT_OF_ORDER;
686 *diagnostic = 0;
687 } else {
688 *cause = ROSE_NOT_OBTAINABLE;
689 *diagnostic = 0;
692 out:
693 spin_unlock_bh(&rose_node_list_lock);
695 return res;
699 * Handle the ioctls that control the routing functions.
701 int rose_rt_ioctl(unsigned int cmd, void __user *arg)
703 struct rose_route_struct rose_route;
704 struct net_device *dev;
705 int err;
707 switch (cmd) {
708 case SIOCADDRT:
709 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
710 return -EFAULT;
711 if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
712 return -EINVAL;
713 if (rose_dev_exists(&rose_route.address)) { /* Can't add routes to ourself */
714 dev_put(dev);
715 return -EINVAL;
717 if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
718 return -EINVAL;
719 if (rose_route.ndigis > AX25_MAX_DIGIS)
720 return -EINVAL;
721 err = rose_add_node(&rose_route, dev);
722 dev_put(dev);
723 return err;
725 case SIOCDELRT:
726 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
727 return -EFAULT;
728 if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
729 return -EINVAL;
730 err = rose_del_node(&rose_route, dev);
731 dev_put(dev);
732 return err;
734 case SIOCRSCLRRT:
735 return rose_clear_routes();
737 default:
738 return -EINVAL;
741 return 0;
744 static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
746 struct rose_route *rose_route, *s;
748 rose_neigh->restarted = 0;
750 rose_stop_t0timer(rose_neigh);
751 rose_start_ftimer(rose_neigh);
753 skb_queue_purge(&rose_neigh->queue);
755 spin_lock_bh(&rose_route_list_lock);
757 rose_route = rose_route_list;
759 while (rose_route != NULL) {
760 if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
761 (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
762 (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
763 s = rose_route->next;
764 rose_remove_route(rose_route);
765 rose_route = s;
766 continue;
769 if (rose_route->neigh1 == rose_neigh) {
770 rose_route->neigh1->use--;
771 rose_route->neigh1 = NULL;
772 rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
775 if (rose_route->neigh2 == rose_neigh) {
776 rose_route->neigh2->use--;
777 rose_route->neigh2 = NULL;
778 rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
781 rose_route = rose_route->next;
783 spin_unlock_bh(&rose_route_list_lock);
787 * A level 2 link has timed out, therefore it appears to be a poor link,
788 * then don't use that neighbour until it is reset. Blow away all through
789 * routes and connections using this route.
791 void rose_link_failed(ax25_cb *ax25, int reason)
793 struct rose_neigh *rose_neigh;
795 spin_lock_bh(&rose_neigh_list_lock);
796 rose_neigh = rose_neigh_list;
797 while (rose_neigh != NULL) {
798 if (rose_neigh->ax25 == ax25)
799 break;
800 rose_neigh = rose_neigh->next;
803 if (rose_neigh != NULL) {
804 rose_neigh->ax25 = NULL;
806 rose_del_route_by_neigh(rose_neigh);
807 rose_kill_by_neigh(rose_neigh);
809 spin_unlock_bh(&rose_neigh_list_lock);
813 * A device has been "downed" remove its link status. Blow away all
814 * through routes and connections that use this device.
816 void rose_link_device_down(struct net_device *dev)
818 struct rose_neigh *rose_neigh;
820 for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
821 if (rose_neigh->dev == dev) {
822 rose_del_route_by_neigh(rose_neigh);
823 rose_kill_by_neigh(rose_neigh);
829 * Route a frame to an appropriate AX.25 connection.
831 int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
833 struct rose_neigh *rose_neigh, *new_neigh;
834 struct rose_route *rose_route;
835 struct rose_facilities_struct facilities;
836 rose_address *src_addr, *dest_addr;
837 struct sock *sk;
838 unsigned short frametype;
839 unsigned int lci, new_lci;
840 unsigned char cause, diagnostic;
841 struct net_device *dev;
842 int len, res = 0;
843 char buf[11];
845 #if 0
846 if (call_in_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT)
847 return res;
848 #endif
850 frametype = skb->data[2];
851 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
852 src_addr = (rose_address *)(skb->data + 9);
853 dest_addr = (rose_address *)(skb->data + 4);
855 spin_lock_bh(&rose_node_list_lock);
856 spin_lock_bh(&rose_neigh_list_lock);
857 spin_lock_bh(&rose_route_list_lock);
859 rose_neigh = rose_neigh_list;
860 while (rose_neigh != NULL) {
861 if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
862 ax25->ax25_dev->dev == rose_neigh->dev)
863 break;
864 rose_neigh = rose_neigh->next;
867 if (rose_neigh == NULL) {
868 printk("rose_route : unknown neighbour or device %s\n",
869 ax2asc(buf, &ax25->dest_addr));
870 goto out;
874 * Obviously the link is working, halt the ftimer.
876 rose_stop_ftimer(rose_neigh);
879 * LCI of zero is always for us, and its always a restart
880 * frame.
882 if (lci == 0) {
883 rose_link_rx_restart(skb, rose_neigh, frametype);
884 goto out;
888 * Find an existing socket.
890 if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
891 if (frametype == ROSE_CALL_REQUEST) {
892 struct rose_sock *rose = rose_sk(sk);
894 /* Remove an existing unused socket */
895 rose_clear_queues(sk);
896 rose->cause = ROSE_NETWORK_CONGESTION;
897 rose->diagnostic = 0;
898 rose->neighbour->use--;
899 rose->neighbour = NULL;
900 rose->lci = 0;
901 rose->state = ROSE_STATE_0;
902 sk->sk_state = TCP_CLOSE;
903 sk->sk_err = 0;
904 sk->sk_shutdown |= SEND_SHUTDOWN;
905 if (!sock_flag(sk, SOCK_DEAD)) {
906 sk->sk_state_change(sk);
907 sock_set_flag(sk, SOCK_DEAD);
910 else {
911 skb_reset_transport_header(skb);
912 res = rose_process_rx_frame(sk, skb);
913 goto out;
918 * Is is a Call Request and is it for us ?
920 if (frametype == ROSE_CALL_REQUEST)
921 if ((dev = rose_dev_get(dest_addr)) != NULL) {
922 res = rose_rx_call_request(skb, dev, rose_neigh, lci);
923 dev_put(dev);
924 goto out;
927 if (!sysctl_rose_routing_control) {
928 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
929 goto out;
933 * Route it to the next in line if we have an entry for it.
935 rose_route = rose_route_list;
936 while (rose_route != NULL) {
937 if (rose_route->lci1 == lci &&
938 rose_route->neigh1 == rose_neigh) {
939 if (frametype == ROSE_CALL_REQUEST) {
940 /* F6FBB - Remove an existing unused route */
941 rose_remove_route(rose_route);
942 break;
943 } else if (rose_route->neigh2 != NULL) {
944 skb->data[0] &= 0xF0;
945 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
946 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
947 rose_transmit_link(skb, rose_route->neigh2);
948 if (frametype == ROSE_CLEAR_CONFIRMATION)
949 rose_remove_route(rose_route);
950 res = 1;
951 goto out;
952 } else {
953 if (frametype == ROSE_CLEAR_CONFIRMATION)
954 rose_remove_route(rose_route);
955 goto out;
958 if (rose_route->lci2 == lci &&
959 rose_route->neigh2 == rose_neigh) {
960 if (frametype == ROSE_CALL_REQUEST) {
961 /* F6FBB - Remove an existing unused route */
962 rose_remove_route(rose_route);
963 break;
964 } else if (rose_route->neigh1 != NULL) {
965 skb->data[0] &= 0xF0;
966 skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
967 skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
968 rose_transmit_link(skb, rose_route->neigh1);
969 if (frametype == ROSE_CLEAR_CONFIRMATION)
970 rose_remove_route(rose_route);
971 res = 1;
972 goto out;
973 } else {
974 if (frametype == ROSE_CLEAR_CONFIRMATION)
975 rose_remove_route(rose_route);
976 goto out;
979 rose_route = rose_route->next;
983 * We know that:
984 * 1. The frame isn't for us,
985 * 2. It isn't "owned" by any existing route.
987 if (frametype != ROSE_CALL_REQUEST) { /* XXX */
988 res = 0;
989 goto out;
992 len = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
993 len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
995 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
997 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
998 rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
999 goto out;
1003 * Check for routing loops.
1005 rose_route = rose_route_list;
1006 while (rose_route != NULL) {
1007 if (rose_route->rand == facilities.rand &&
1008 rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1009 ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1010 ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1011 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1012 goto out;
1014 rose_route = rose_route->next;
1017 if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic)) == NULL) {
1018 rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1019 goto out;
1022 if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1023 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1024 goto out;
1027 if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1028 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1029 goto out;
1032 rose_route->lci1 = lci;
1033 rose_route->src_addr = *src_addr;
1034 rose_route->dest_addr = *dest_addr;
1035 rose_route->src_call = facilities.dest_call;
1036 rose_route->dest_call = facilities.source_call;
1037 rose_route->rand = facilities.rand;
1038 rose_route->neigh1 = rose_neigh;
1039 rose_route->lci2 = new_lci;
1040 rose_route->neigh2 = new_neigh;
1042 rose_route->neigh1->use++;
1043 rose_route->neigh2->use++;
1045 rose_route->next = rose_route_list;
1046 rose_route_list = rose_route;
1048 skb->data[0] &= 0xF0;
1049 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1050 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
1052 rose_transmit_link(skb, rose_route->neigh2);
1053 res = 1;
1055 out:
1056 spin_unlock_bh(&rose_route_list_lock);
1057 spin_unlock_bh(&rose_neigh_list_lock);
1058 spin_unlock_bh(&rose_node_list_lock);
1060 return res;
1063 #ifdef CONFIG_PROC_FS
1065 static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1067 struct rose_node *rose_node;
1068 int i = 1;
1070 spin_lock_bh(&rose_neigh_list_lock);
1071 if (*pos == 0)
1072 return SEQ_START_TOKEN;
1074 for (rose_node = rose_node_list; rose_node && i < *pos;
1075 rose_node = rose_node->next, ++i);
1077 return (i == *pos) ? rose_node : NULL;
1080 static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1082 ++*pos;
1084 return (v == SEQ_START_TOKEN) ? rose_node_list
1085 : ((struct rose_node *)v)->next;
1088 static void rose_node_stop(struct seq_file *seq, void *v)
1090 spin_unlock_bh(&rose_neigh_list_lock);
1093 static int rose_node_show(struct seq_file *seq, void *v)
1095 int i;
1097 if (v == SEQ_START_TOKEN)
1098 seq_puts(seq, "address mask n neigh neigh neigh\n");
1099 else {
1100 const struct rose_node *rose_node = v;
1101 /* if (rose_node->loopback) {
1102 seq_printf(seq, "%-10s %04d 1 loopback\n",
1103 rose2asc(&rose_node->address),
1104 rose_node->mask);
1105 } else { */
1106 seq_printf(seq, "%-10s %04d %d",
1107 rose2asc(&rose_node->address),
1108 rose_node->mask,
1109 rose_node->count);
1111 for (i = 0; i < rose_node->count; i++)
1112 seq_printf(seq, " %05d",
1113 rose_node->neighbour[i]->number);
1115 seq_puts(seq, "\n");
1116 /* } */
1118 return 0;
1121 static struct seq_operations rose_node_seqops = {
1122 .start = rose_node_start,
1123 .next = rose_node_next,
1124 .stop = rose_node_stop,
1125 .show = rose_node_show,
1128 static int rose_nodes_open(struct inode *inode, struct file *file)
1130 return seq_open(file, &rose_node_seqops);
1133 const struct file_operations rose_nodes_fops = {
1134 .owner = THIS_MODULE,
1135 .open = rose_nodes_open,
1136 .read = seq_read,
1137 .llseek = seq_lseek,
1138 .release = seq_release,
1141 static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1143 struct rose_neigh *rose_neigh;
1144 int i = 1;
1146 spin_lock_bh(&rose_neigh_list_lock);
1147 if (*pos == 0)
1148 return SEQ_START_TOKEN;
1150 for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1151 rose_neigh = rose_neigh->next, ++i);
1153 return (i == *pos) ? rose_neigh : NULL;
1156 static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1158 ++*pos;
1160 return (v == SEQ_START_TOKEN) ? rose_neigh_list
1161 : ((struct rose_neigh *)v)->next;
1164 static void rose_neigh_stop(struct seq_file *seq, void *v)
1166 spin_unlock_bh(&rose_neigh_list_lock);
1169 static int rose_neigh_show(struct seq_file *seq, void *v)
1171 char buf[11];
1172 int i;
1174 if (v == SEQ_START_TOKEN)
1175 seq_puts(seq,
1176 "addr callsign dev count use mode restart t0 tf digipeaters\n");
1177 else {
1178 struct rose_neigh *rose_neigh = v;
1180 /* if (!rose_neigh->loopback) { */
1181 seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
1182 rose_neigh->number,
1183 (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
1184 rose_neigh->dev ? rose_neigh->dev->name : "???",
1185 rose_neigh->count,
1186 rose_neigh->use,
1187 (rose_neigh->dce_mode) ? "DCE" : "DTE",
1188 (rose_neigh->restarted) ? "yes" : "no",
1189 ax25_display_timer(&rose_neigh->t0timer) / HZ,
1190 ax25_display_timer(&rose_neigh->ftimer) / HZ);
1192 if (rose_neigh->digipeat != NULL) {
1193 for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1194 seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
1197 seq_puts(seq, "\n");
1199 return 0;
1203 static struct seq_operations rose_neigh_seqops = {
1204 .start = rose_neigh_start,
1205 .next = rose_neigh_next,
1206 .stop = rose_neigh_stop,
1207 .show = rose_neigh_show,
1210 static int rose_neigh_open(struct inode *inode, struct file *file)
1212 return seq_open(file, &rose_neigh_seqops);
1215 const struct file_operations rose_neigh_fops = {
1216 .owner = THIS_MODULE,
1217 .open = rose_neigh_open,
1218 .read = seq_read,
1219 .llseek = seq_lseek,
1220 .release = seq_release,
1224 static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1226 struct rose_route *rose_route;
1227 int i = 1;
1229 spin_lock_bh(&rose_route_list_lock);
1230 if (*pos == 0)
1231 return SEQ_START_TOKEN;
1233 for (rose_route = rose_route_list; rose_route && i < *pos;
1234 rose_route = rose_route->next, ++i);
1236 return (i == *pos) ? rose_route : NULL;
1239 static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1241 ++*pos;
1243 return (v == SEQ_START_TOKEN) ? rose_route_list
1244 : ((struct rose_route *)v)->next;
1247 static void rose_route_stop(struct seq_file *seq, void *v)
1249 spin_unlock_bh(&rose_route_list_lock);
1252 static int rose_route_show(struct seq_file *seq, void *v)
1254 char buf[11];
1256 if (v == SEQ_START_TOKEN)
1257 seq_puts(seq,
1258 "lci address callsign neigh <-> lci address callsign neigh\n");
1259 else {
1260 struct rose_route *rose_route = v;
1262 if (rose_route->neigh1)
1263 seq_printf(seq,
1264 "%3.3X %-10s %-9s %05d ",
1265 rose_route->lci1,
1266 rose2asc(&rose_route->src_addr),
1267 ax2asc(buf, &rose_route->src_call),
1268 rose_route->neigh1->number);
1269 else
1270 seq_puts(seq,
1271 "000 * * 00000 ");
1273 if (rose_route->neigh2)
1274 seq_printf(seq,
1275 "%3.3X %-10s %-9s %05d\n",
1276 rose_route->lci2,
1277 rose2asc(&rose_route->dest_addr),
1278 ax2asc(buf, &rose_route->dest_call),
1279 rose_route->neigh2->number);
1280 else
1281 seq_puts(seq,
1282 "000 * * 00000\n");
1284 return 0;
1287 static struct seq_operations rose_route_seqops = {
1288 .start = rose_route_start,
1289 .next = rose_route_next,
1290 .stop = rose_route_stop,
1291 .show = rose_route_show,
1294 static int rose_route_open(struct inode *inode, struct file *file)
1296 return seq_open(file, &rose_route_seqops);
1299 const struct file_operations rose_routes_fops = {
1300 .owner = THIS_MODULE,
1301 .open = rose_route_open,
1302 .read = seq_read,
1303 .llseek = seq_lseek,
1304 .release = seq_release,
1307 #endif /* CONFIG_PROC_FS */
1310 * Release all memory associated with ROSE routing structures.
1312 void __exit rose_rt_free(void)
1314 struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1315 struct rose_node *t, *rose_node = rose_node_list;
1316 struct rose_route *u, *rose_route = rose_route_list;
1318 while (rose_neigh != NULL) {
1319 s = rose_neigh;
1320 rose_neigh = rose_neigh->next;
1322 rose_remove_neigh(s);
1325 while (rose_node != NULL) {
1326 t = rose_node;
1327 rose_node = rose_node->next;
1329 rose_remove_node(t);
1332 while (rose_route != NULL) {
1333 u = rose_route;
1334 rose_route = rose_route->next;
1336 rose_remove_route(u);