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)
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/socket.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/timer.h>
17 #include <linux/string.h>
18 #include <linux/sockios.h>
19 #include <linux/net.h>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
24 #include <linux/if_arp.h>
25 #include <linux/skbuff.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 */
33 #include <linux/interrupt.h>
34 #include <linux/notifier.h>
35 #include <linux/netfilter.h>
36 #include <linux/init.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;
51 static void rose_remove_neigh(struct rose_neigh *);
54 * Add a new route to a node, and in the process add the node and the
55 * neighbour if it is new.
57 static int rose_add_node(struct rose_route_struct *rose_route,
58 struct net_device *dev)
60 struct rose_node *rose_node, *rose_tmpn, *rose_tmpp;
61 struct rose_neigh *rose_neigh;
64 spin_lock_bh(&rose_node_list_lock);
65 spin_lock_bh(&rose_neigh_list_lock);
67 rose_node = rose_node_list;
68 while (rose_node != NULL) {
69 if ((rose_node->mask == rose_route->mask) &&
70 (rosecmpm(&rose_route->address, &rose_node->address,
71 rose_route->mask) == 0))
73 rose_node = rose_node->next;
76 if (rose_node != NULL && rose_node->loopback) {
81 rose_neigh = rose_neigh_list;
82 while (rose_neigh != NULL) {
83 if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0
84 && rose_neigh->dev == dev)
86 rose_neigh = rose_neigh->next;
89 if (rose_neigh == NULL) {
90 rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
91 if (rose_neigh == NULL) {
96 rose_neigh->callsign = rose_route->neighbour;
97 rose_neigh->digipeat = NULL;
98 rose_neigh->ax25 = NULL;
99 rose_neigh->dev = dev;
100 rose_neigh->count = 0;
102 rose_neigh->dce_mode = 0;
103 rose_neigh->loopback = 0;
104 rose_neigh->number = rose_neigh_no++;
105 rose_neigh->restarted = 0;
107 skb_queue_head_init(&rose_neigh->queue);
109 init_timer(&rose_neigh->ftimer);
110 init_timer(&rose_neigh->t0timer);
112 if (rose_route->ndigis != 0) {
113 if ((rose_neigh->digipeat = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) {
119 rose_neigh->digipeat->ndigi = rose_route->ndigis;
120 rose_neigh->digipeat->lastrepeat = -1;
122 for (i = 0; i < rose_route->ndigis; i++) {
123 rose_neigh->digipeat->calls[i] =
124 rose_route->digipeaters[i];
125 rose_neigh->digipeat->repeated[i] = 0;
129 rose_neigh->next = rose_neigh_list;
130 rose_neigh_list = rose_neigh;
134 * This is a new node to be inserted into the list. Find where it needs
135 * to be inserted into the list, and insert it. We want to be sure
136 * to order the list in descending order of mask size to ensure that
137 * later when we are searching this list the first match will be the
140 if (rose_node == NULL) {
141 rose_tmpn = rose_node_list;
144 while (rose_tmpn != NULL) {
145 if (rose_tmpn->mask > rose_route->mask) {
146 rose_tmpp = rose_tmpn;
147 rose_tmpn = rose_tmpn->next;
153 /* create new node */
154 rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
155 if (rose_node == NULL) {
160 rose_node->address = rose_route->address;
161 rose_node->mask = rose_route->mask;
162 rose_node->count = 1;
163 rose_node->loopback = 0;
164 rose_node->neighbour[0] = rose_neigh;
166 if (rose_tmpn == NULL) {
167 if (rose_tmpp == NULL) { /* Empty list */
168 rose_node_list = rose_node;
169 rose_node->next = NULL;
171 rose_tmpp->next = rose_node;
172 rose_node->next = NULL;
175 if (rose_tmpp == NULL) { /* 1st node */
176 rose_node->next = rose_node_list;
177 rose_node_list = rose_node;
179 rose_tmpp->next = rose_node;
180 rose_node->next = rose_tmpn;
188 /* We have space, slot it in */
189 if (rose_node->count < 3) {
190 rose_node->neighbour[rose_node->count] = rose_neigh;
196 spin_unlock_bh(&rose_neigh_list_lock);
197 spin_unlock_bh(&rose_node_list_lock);
203 * Caller is holding rose_node_list_lock.
205 static void rose_remove_node(struct rose_node *rose_node)
209 if ((s = rose_node_list) == rose_node) {
210 rose_node_list = rose_node->next;
215 while (s != NULL && s->next != NULL) {
216 if (s->next == rose_node) {
217 s->next = rose_node->next;
227 * Caller is holding rose_neigh_list_lock.
229 static void rose_remove_neigh(struct rose_neigh *rose_neigh)
231 struct rose_neigh *s;
233 rose_stop_ftimer(rose_neigh);
234 rose_stop_t0timer(rose_neigh);
236 skb_queue_purge(&rose_neigh->queue);
238 spin_lock_bh(&rose_neigh_list_lock);
240 if ((s = rose_neigh_list) == rose_neigh) {
241 rose_neigh_list = rose_neigh->next;
242 spin_unlock_bh(&rose_neigh_list_lock);
243 if (rose_neigh->digipeat != NULL)
244 kfree(rose_neigh->digipeat);
249 while (s != NULL && s->next != NULL) {
250 if (s->next == rose_neigh) {
251 s->next = rose_neigh->next;
252 spin_unlock_bh(&rose_neigh_list_lock);
253 if (rose_neigh->digipeat != NULL)
254 kfree(rose_neigh->digipeat);
261 spin_unlock_bh(&rose_neigh_list_lock);
265 * Caller is holding rose_route_list_lock.
267 static void rose_remove_route(struct rose_route *rose_route)
269 struct rose_route *s;
271 if (rose_route->neigh1 != NULL)
272 rose_route->neigh1->use--;
274 if (rose_route->neigh2 != NULL)
275 rose_route->neigh2->use--;
277 if ((s = rose_route_list) == rose_route) {
278 rose_route_list = rose_route->next;
283 while (s != NULL && s->next != NULL) {
284 if (s->next == rose_route) {
285 s->next = rose_route->next;
295 * "Delete" a node. Strictly speaking remove a route to a node. The node
296 * is only deleted if no routes are left to it.
298 static int rose_del_node(struct rose_route_struct *rose_route,
299 struct net_device *dev)
301 struct rose_node *rose_node;
302 struct rose_neigh *rose_neigh;
305 spin_lock_bh(&rose_node_list_lock);
306 spin_lock_bh(&rose_neigh_list_lock);
308 rose_node = rose_node_list;
309 while (rose_node != NULL) {
310 if ((rose_node->mask == rose_route->mask) &&
311 (rosecmpm(&rose_route->address, &rose_node->address,
312 rose_route->mask) == 0))
314 rose_node = rose_node->next;
317 if (rose_node == NULL || rose_node->loopback) {
322 rose_neigh = rose_neigh_list;
323 while (rose_neigh != NULL) {
324 if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0
325 && rose_neigh->dev == dev)
327 rose_neigh = rose_neigh->next;
330 if (rose_neigh == NULL) {
335 for (i = 0; i < rose_node->count; i++) {
336 if (rose_node->neighbour[i] == rose_neigh) {
339 if (rose_neigh->count == 0 && rose_neigh->use == 0)
340 rose_remove_neigh(rose_neigh);
344 if (rose_node->count == 0) {
345 rose_remove_node(rose_node);
349 rose_node->neighbour[0] =
350 rose_node->neighbour[1];
352 rose_node->neighbour[1] =
353 rose_node->neighbour[2];
364 spin_unlock_bh(&rose_neigh_list_lock);
365 spin_unlock_bh(&rose_node_list_lock);
371 * Add the loopback neighbour.
373 int rose_add_loopback_neigh(void)
375 if ((rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_ATOMIC)) == NULL)
378 rose_loopback_neigh->callsign = null_ax25_address;
379 rose_loopback_neigh->digipeat = NULL;
380 rose_loopback_neigh->ax25 = NULL;
381 rose_loopback_neigh->dev = NULL;
382 rose_loopback_neigh->count = 0;
383 rose_loopback_neigh->use = 0;
384 rose_loopback_neigh->dce_mode = 1;
385 rose_loopback_neigh->loopback = 1;
386 rose_loopback_neigh->number = rose_neigh_no++;
387 rose_loopback_neigh->restarted = 1;
389 skb_queue_head_init(&rose_loopback_neigh->queue);
391 init_timer(&rose_loopback_neigh->ftimer);
392 init_timer(&rose_loopback_neigh->t0timer);
394 spin_lock_bh(&rose_neigh_list_lock);
395 rose_loopback_neigh->next = rose_neigh_list;
396 rose_neigh_list = rose_loopback_neigh;
397 spin_unlock_bh(&rose_neigh_list_lock);
403 * Add a loopback node.
405 int rose_add_loopback_node(rose_address *address)
407 struct rose_node *rose_node;
408 unsigned int err = 0;
410 spin_lock_bh(&rose_node_list_lock);
412 rose_node = rose_node_list;
413 while (rose_node != NULL) {
414 if ((rose_node->mask == 10) &&
415 (rosecmpm(address, &rose_node->address, 10) == 0) &&
418 rose_node = rose_node->next;
421 if (rose_node != NULL)
424 if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
429 rose_node->address = *address;
430 rose_node->mask = 10;
431 rose_node->count = 1;
432 rose_node->loopback = 1;
433 rose_node->neighbour[0] = rose_loopback_neigh;
435 /* Insert at the head of list. Address is always mask=10 */
436 rose_node->next = rose_node_list;
437 rose_node_list = rose_node;
439 rose_loopback_neigh->count++;
442 spin_unlock_bh(&rose_node_list_lock);
448 * Delete a loopback node.
450 void rose_del_loopback_node(rose_address *address)
452 struct rose_node *rose_node;
454 spin_lock_bh(&rose_node_list_lock);
456 rose_node = rose_node_list;
457 while (rose_node != NULL) {
458 if ((rose_node->mask == 10) &&
459 (rosecmpm(address, &rose_node->address, 10) == 0) &&
462 rose_node = rose_node->next;
465 if (rose_node == NULL)
468 rose_remove_node(rose_node);
470 rose_loopback_neigh->count--;
473 spin_unlock_bh(&rose_node_list_lock);
477 * A device has been removed. Remove its routes and neighbours.
479 void rose_rt_device_down(struct net_device *dev)
481 struct rose_neigh *s, *rose_neigh;
482 struct rose_node *t, *rose_node;
485 spin_lock_bh(&rose_node_list_lock);
486 spin_lock_bh(&rose_neigh_list_lock);
487 rose_neigh = rose_neigh_list;
488 while (rose_neigh != NULL) {
490 rose_neigh = rose_neigh->next;
495 rose_node = rose_node_list;
497 while (rose_node != NULL) {
499 rose_node = rose_node->next;
501 for (i = 0; i < t->count; i++) {
502 if (t->neighbour[i] != s)
509 t->neighbour[0] = t->neighbour[1];
511 t->neighbour[1] = t->neighbour[2];
521 rose_remove_neigh(s);
523 spin_unlock_bh(&rose_neigh_list_lock);
524 spin_unlock_bh(&rose_node_list_lock);
527 #if 0 /* Currently unused */
529 * A device has been removed. Remove its links.
531 void rose_route_device_down(struct net_device *dev)
533 struct rose_route *s, *rose_route;
535 spin_lock_bh(&rose_route_list_lock);
536 rose_route = rose_route_list;
537 while (rose_route != NULL) {
539 rose_route = rose_route->next;
541 if (s->neigh1->dev == dev || s->neigh2->dev == dev)
542 rose_remove_route(s);
544 spin_unlock_bh(&rose_route_list_lock);
549 * Clear all nodes and neighbours out, except for neighbours with
550 * active connections going through them.
551 * Do not clear loopback neighbour and nodes.
553 static int rose_clear_routes(void)
555 struct rose_neigh *s, *rose_neigh;
556 struct rose_node *t, *rose_node;
558 spin_lock_bh(&rose_node_list_lock);
559 spin_lock_bh(&rose_neigh_list_lock);
561 rose_neigh = rose_neigh_list;
562 rose_node = rose_node_list;
564 while (rose_node != NULL) {
566 rose_node = rose_node->next;
571 while (rose_neigh != NULL) {
573 rose_neigh = rose_neigh->next;
575 if (s->use == 0 && !s->loopback) {
577 rose_remove_neigh(s);
581 spin_unlock_bh(&rose_neigh_list_lock);
582 spin_unlock_bh(&rose_node_list_lock);
588 * Check that the device given is a valid AX.25 interface that is "up".
590 static struct net_device *rose_ax25_dev_get(char *devname)
592 struct net_device *dev;
594 if ((dev = dev_get_by_name(devname)) == NULL)
597 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
605 * Find the first active ROSE device, usually "rose0".
607 struct net_device *rose_dev_first(void)
609 struct net_device *dev, *first = NULL;
611 read_lock(&dev_base_lock);
612 for (dev = dev_base; dev != NULL; dev = dev->next) {
613 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
614 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
617 read_unlock(&dev_base_lock);
623 * Find the ROSE device for the given address.
625 struct net_device *rose_dev_get(rose_address *addr)
627 struct net_device *dev;
629 read_lock(&dev_base_lock);
630 for (dev = dev_base; dev != NULL; dev = dev->next) {
631 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
637 read_unlock(&dev_base_lock);
641 static int rose_dev_exists(rose_address *addr)
643 struct net_device *dev;
645 read_lock(&dev_base_lock);
646 for (dev = dev_base; dev != NULL; dev = dev->next) {
647 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
651 read_unlock(&dev_base_lock);
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))
671 * Find a neighbour given a ROSE address.
673 struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
674 unsigned char *diagnostic)
676 struct rose_neigh *res = NULL;
677 struct rose_node *node;
681 spin_lock_bh(&rose_node_list_lock);
682 for (node = rose_node_list; node != NULL; node = node->next) {
683 if (rosecmpm(addr, &node->address, node->mask) == 0) {
684 for (i = 0; i < node->count; i++) {
685 if (!rose_ftimer_running(node->neighbour[i])) {
686 res = node->neighbour[i];
696 *cause = ROSE_OUT_OF_ORDER;
699 *cause = ROSE_NOT_OBTAINABLE;
704 spin_unlock_bh(&rose_node_list_lock);
710 * Handle the ioctls that control the routing functions.
712 int rose_rt_ioctl(unsigned int cmd, void __user *arg)
714 struct rose_route_struct rose_route;
715 struct net_device *dev;
720 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
722 if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
724 if (rose_dev_exists(&rose_route.address)) { /* Can't add routes to ourself */
728 if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
730 if (rose_route.ndigis > 8) /* No more than 8 digipeats */
732 err = rose_add_node(&rose_route, dev);
737 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
739 if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
741 err = rose_del_node(&rose_route, dev);
746 return rose_clear_routes();
755 static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
757 struct rose_route *rose_route, *s;
759 rose_neigh->restarted = 0;
761 rose_stop_t0timer(rose_neigh);
762 rose_start_ftimer(rose_neigh);
764 skb_queue_purge(&rose_neigh->queue);
766 spin_lock_bh(&rose_route_list_lock);
768 rose_route = rose_route_list;
770 while (rose_route != NULL) {
771 if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
772 (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
773 (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
774 s = rose_route->next;
775 rose_remove_route(rose_route);
780 if (rose_route->neigh1 == rose_neigh) {
781 rose_route->neigh1->use--;
782 rose_route->neigh1 = NULL;
783 rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
786 if (rose_route->neigh2 == rose_neigh) {
787 rose_route->neigh2->use--;
788 rose_route->neigh2 = NULL;
789 rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
792 rose_route = rose_route->next;
794 spin_unlock_bh(&rose_route_list_lock);
798 * A level 2 link has timed out, therefore it appears to be a poor link,
799 * then don't use that neighbour until it is reset. Blow away all through
800 * routes and connections using this route.
802 void rose_link_failed(ax25_cb *ax25, int reason)
804 struct rose_neigh *rose_neigh;
806 spin_lock_bh(&rose_neigh_list_lock);
807 rose_neigh = rose_neigh_list;
808 while (rose_neigh != NULL) {
809 if (rose_neigh->ax25 == ax25)
811 rose_neigh = rose_neigh->next;
814 if (rose_neigh != NULL) {
815 rose_neigh->ax25 = NULL;
817 rose_del_route_by_neigh(rose_neigh);
818 rose_kill_by_neigh(rose_neigh);
820 spin_unlock_bh(&rose_neigh_list_lock);
824 * A device has been "downed" remove its link status. Blow away all
825 * through routes and connections that use this device.
827 void rose_link_device_down(struct net_device *dev)
829 struct rose_neigh *rose_neigh;
831 for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
832 if (rose_neigh->dev == dev) {
833 rose_del_route_by_neigh(rose_neigh);
834 rose_kill_by_neigh(rose_neigh);
840 * Route a frame to an appropriate AX.25 connection.
842 int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
844 struct rose_neigh *rose_neigh, *new_neigh;
845 struct rose_route *rose_route;
846 struct rose_facilities_struct facilities;
847 rose_address *src_addr, *dest_addr;
849 unsigned short frametype;
850 unsigned int lci, new_lci;
851 unsigned char cause, diagnostic;
852 struct net_device *dev;
857 if (call_in_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT)
861 frametype = skb->data[2];
862 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
863 src_addr = (rose_address *)(skb->data + 9);
864 dest_addr = (rose_address *)(skb->data + 4);
866 spin_lock_bh(&rose_node_list_lock);
867 spin_lock_bh(&rose_neigh_list_lock);
868 spin_lock_bh(&rose_route_list_lock);
870 rose_neigh = rose_neigh_list;
871 while (rose_neigh != NULL) {
872 if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
873 ax25->ax25_dev->dev == rose_neigh->dev)
875 rose_neigh = rose_neigh->next;
878 if (rose_neigh == NULL) {
879 printk("rose_route : unknown neighbour or device %s\n",
880 ax2asc(buf, &ax25->dest_addr));
885 * Obviously the link is working, halt the ftimer.
887 rose_stop_ftimer(rose_neigh);
890 * LCI of zero is always for us, and its always a restart
894 rose_link_rx_restart(skb, rose_neigh, frametype);
899 * Find an existing socket.
901 if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
902 if (frametype == ROSE_CALL_REQUEST) {
903 struct rose_sock *rose = rose_sk(sk);
905 /* Remove an existing unused socket */
906 rose_clear_queues(sk);
907 rose->cause = ROSE_NETWORK_CONGESTION;
908 rose->diagnostic = 0;
909 rose->neighbour->use--;
910 rose->neighbour = NULL;
912 rose->state = ROSE_STATE_0;
913 sk->sk_state = TCP_CLOSE;
915 sk->sk_shutdown |= SEND_SHUTDOWN;
916 if (!sock_flag(sk, SOCK_DEAD)) {
917 sk->sk_state_change(sk);
918 sock_set_flag(sk, SOCK_DEAD);
922 skb->h.raw = skb->data;
923 res = rose_process_rx_frame(sk, skb);
929 * Is is a Call Request and is it for us ?
931 if (frametype == ROSE_CALL_REQUEST)
932 if ((dev = rose_dev_get(dest_addr)) != NULL) {
933 res = rose_rx_call_request(skb, dev, rose_neigh, lci);
938 if (!sysctl_rose_routing_control) {
939 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
944 * Route it to the next in line if we have an entry for it.
946 rose_route = rose_route_list;
947 while (rose_route != NULL) {
948 if (rose_route->lci1 == lci &&
949 rose_route->neigh1 == rose_neigh) {
950 if (frametype == ROSE_CALL_REQUEST) {
951 /* F6FBB - Remove an existing unused route */
952 rose_remove_route(rose_route);
954 } else if (rose_route->neigh2 != NULL) {
955 skb->data[0] &= 0xF0;
956 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
957 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
958 rose_transmit_link(skb, rose_route->neigh2);
959 if (frametype == ROSE_CLEAR_CONFIRMATION)
960 rose_remove_route(rose_route);
964 if (frametype == ROSE_CLEAR_CONFIRMATION)
965 rose_remove_route(rose_route);
969 if (rose_route->lci2 == lci &&
970 rose_route->neigh2 == rose_neigh) {
971 if (frametype == ROSE_CALL_REQUEST) {
972 /* F6FBB - Remove an existing unused route */
973 rose_remove_route(rose_route);
975 } else if (rose_route->neigh1 != NULL) {
976 skb->data[0] &= 0xF0;
977 skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
978 skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
979 rose_transmit_link(skb, rose_route->neigh1);
980 if (frametype == ROSE_CLEAR_CONFIRMATION)
981 rose_remove_route(rose_route);
985 if (frametype == ROSE_CLEAR_CONFIRMATION)
986 rose_remove_route(rose_route);
990 rose_route = rose_route->next;
995 * 1. The frame isn't for us,
996 * 2. It isn't "owned" by any existing route.
998 if (frametype != ROSE_CALL_REQUEST) { /* XXX */
1003 len = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
1004 len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
1006 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1008 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
1009 rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1014 * Check for routing loops.
1016 rose_route = rose_route_list;
1017 while (rose_route != NULL) {
1018 if (rose_route->rand == facilities.rand &&
1019 rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1020 ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1021 ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1022 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1025 rose_route = rose_route->next;
1028 if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic)) == NULL) {
1029 rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1033 if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1034 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1038 if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1039 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1043 rose_route->lci1 = lci;
1044 rose_route->src_addr = *src_addr;
1045 rose_route->dest_addr = *dest_addr;
1046 rose_route->src_call = facilities.dest_call;
1047 rose_route->dest_call = facilities.source_call;
1048 rose_route->rand = facilities.rand;
1049 rose_route->neigh1 = rose_neigh;
1050 rose_route->lci2 = new_lci;
1051 rose_route->neigh2 = new_neigh;
1053 rose_route->neigh1->use++;
1054 rose_route->neigh2->use++;
1056 rose_route->next = rose_route_list;
1057 rose_route_list = rose_route;
1059 skb->data[0] &= 0xF0;
1060 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1061 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
1063 rose_transmit_link(skb, rose_route->neigh2);
1067 spin_unlock_bh(&rose_route_list_lock);
1068 spin_unlock_bh(&rose_neigh_list_lock);
1069 spin_unlock_bh(&rose_node_list_lock);
1074 #ifdef CONFIG_PROC_FS
1076 static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1078 struct rose_node *rose_node;
1081 spin_lock_bh(&rose_neigh_list_lock);
1083 return SEQ_START_TOKEN;
1085 for (rose_node = rose_node_list; rose_node && i < *pos;
1086 rose_node = rose_node->next, ++i);
1088 return (i == *pos) ? rose_node : NULL;
1091 static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1095 return (v == SEQ_START_TOKEN) ? rose_node_list
1096 : ((struct rose_node *)v)->next;
1099 static void rose_node_stop(struct seq_file *seq, void *v)
1101 spin_unlock_bh(&rose_neigh_list_lock);
1104 static int rose_node_show(struct seq_file *seq, void *v)
1108 if (v == SEQ_START_TOKEN)
1109 seq_puts(seq, "address mask n neigh neigh neigh\n");
1111 const struct rose_node *rose_node = v;
1112 /* if (rose_node->loopback) {
1113 seq_printf(seq, "%-10s %04d 1 loopback\n",
1114 rose2asc(&rose_node->address),
1117 seq_printf(seq, "%-10s %04d %d",
1118 rose2asc(&rose_node->address),
1122 for (i = 0; i < rose_node->count; i++)
1123 seq_printf(seq, " %05d",
1124 rose_node->neighbour[i]->number);
1126 seq_puts(seq, "\n");
1132 static struct seq_operations rose_node_seqops = {
1133 .start = rose_node_start,
1134 .next = rose_node_next,
1135 .stop = rose_node_stop,
1136 .show = rose_node_show,
1139 static int rose_nodes_open(struct inode *inode, struct file *file)
1141 return seq_open(file, &rose_node_seqops);
1144 struct file_operations rose_nodes_fops = {
1145 .owner = THIS_MODULE,
1146 .open = rose_nodes_open,
1148 .llseek = seq_lseek,
1149 .release = seq_release,
1152 static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1154 struct rose_neigh *rose_neigh;
1157 spin_lock_bh(&rose_neigh_list_lock);
1159 return SEQ_START_TOKEN;
1161 for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1162 rose_neigh = rose_neigh->next, ++i);
1164 return (i == *pos) ? rose_neigh : NULL;
1167 static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1171 return (v == SEQ_START_TOKEN) ? rose_neigh_list
1172 : ((struct rose_neigh *)v)->next;
1175 static void rose_neigh_stop(struct seq_file *seq, void *v)
1177 spin_unlock_bh(&rose_neigh_list_lock);
1180 static int rose_neigh_show(struct seq_file *seq, void *v)
1185 if (v == SEQ_START_TOKEN)
1187 "addr callsign dev count use mode restart t0 tf digipeaters\n");
1189 struct rose_neigh *rose_neigh = v;
1191 /* if (!rose_neigh->loopback) { */
1192 seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
1194 (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
1195 rose_neigh->dev ? rose_neigh->dev->name : "???",
1198 (rose_neigh->dce_mode) ? "DCE" : "DTE",
1199 (rose_neigh->restarted) ? "yes" : "no",
1200 ax25_display_timer(&rose_neigh->t0timer) / HZ,
1201 ax25_display_timer(&rose_neigh->ftimer) / HZ);
1203 if (rose_neigh->digipeat != NULL) {
1204 for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1205 seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
1208 seq_puts(seq, "\n");
1214 static struct seq_operations rose_neigh_seqops = {
1215 .start = rose_neigh_start,
1216 .next = rose_neigh_next,
1217 .stop = rose_neigh_stop,
1218 .show = rose_neigh_show,
1221 static int rose_neigh_open(struct inode *inode, struct file *file)
1223 return seq_open(file, &rose_neigh_seqops);
1226 struct file_operations rose_neigh_fops = {
1227 .owner = THIS_MODULE,
1228 .open = rose_neigh_open,
1230 .llseek = seq_lseek,
1231 .release = seq_release,
1235 static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1237 struct rose_route *rose_route;
1240 spin_lock_bh(&rose_route_list_lock);
1242 return SEQ_START_TOKEN;
1244 for (rose_route = rose_route_list; rose_route && i < *pos;
1245 rose_route = rose_route->next, ++i);
1247 return (i == *pos) ? rose_route : NULL;
1250 static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1254 return (v == SEQ_START_TOKEN) ? rose_route_list
1255 : ((struct rose_route *)v)->next;
1258 static void rose_route_stop(struct seq_file *seq, void *v)
1260 spin_unlock_bh(&rose_route_list_lock);
1263 static int rose_route_show(struct seq_file *seq, void *v)
1267 if (v == SEQ_START_TOKEN)
1269 "lci address callsign neigh <-> lci address callsign neigh\n");
1271 struct rose_route *rose_route = v;
1273 if (rose_route->neigh1)
1275 "%3.3X %-10s %-9s %05d ",
1277 rose2asc(&rose_route->src_addr),
1278 ax2asc(buf, &rose_route->src_call),
1279 rose_route->neigh1->number);
1284 if (rose_route->neigh2)
1286 "%3.3X %-10s %-9s %05d\n",
1288 rose2asc(&rose_route->dest_addr),
1289 ax2asc(buf, &rose_route->dest_call),
1290 rose_route->neigh2->number);
1298 static struct seq_operations rose_route_seqops = {
1299 .start = rose_route_start,
1300 .next = rose_route_next,
1301 .stop = rose_route_stop,
1302 .show = rose_route_show,
1305 static int rose_route_open(struct inode *inode, struct file *file)
1307 return seq_open(file, &rose_route_seqops);
1310 struct file_operations rose_routes_fops = {
1311 .owner = THIS_MODULE,
1312 .open = rose_route_open,
1314 .llseek = seq_lseek,
1315 .release = seq_release,
1318 #endif /* CONFIG_PROC_FS */
1321 * Release all memory associated with ROSE routing structures.
1323 void __exit rose_rt_free(void)
1325 struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1326 struct rose_node *t, *rose_node = rose_node_list;
1327 struct rose_route *u, *rose_route = rose_route_list;
1329 while (rose_neigh != NULL) {
1331 rose_neigh = rose_neigh->next;
1333 rose_remove_neigh(s);
1336 while (rose_node != NULL) {
1338 rose_node = rose_node->next;
1340 rose_remove_node(t);
1343 while (rose_route != NULL) {
1345 rose_route = rose_route->next;
1347 rose_remove_route(u);