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/timer.h>
16 #include <linux/string.h>
17 #include <linux/sockios.h>
18 #include <linux/net.h>
19 #include <linux/slab.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;
52 * Add a new route to a node, and in the process add the node and the
53 * neighbour if it is new.
55 static int __must_check rose_add_node(struct rose_route_struct *rose_route,
56 struct net_device *dev)
58 struct rose_node *rose_node, *rose_tmpn, *rose_tmpp;
59 struct rose_neigh *rose_neigh;
62 spin_lock_bh(&rose_node_list_lock);
63 spin_lock_bh(&rose_neigh_list_lock);
65 rose_node = rose_node_list;
66 while (rose_node != NULL) {
67 if ((rose_node->mask == rose_route->mask) &&
68 (rosecmpm(&rose_route->address, &rose_node->address,
69 rose_route->mask) == 0))
71 rose_node = rose_node->next;
74 if (rose_node != NULL && rose_node->loopback) {
79 rose_neigh = rose_neigh_list;
80 while (rose_neigh != NULL) {
81 if (ax25cmp(&rose_route->neighbour,
82 &rose_neigh->callsign) == 0 &&
83 rose_neigh->dev == dev)
85 rose_neigh = rose_neigh->next;
88 if (rose_neigh == NULL) {
89 rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
90 if (rose_neigh == NULL) {
95 rose_neigh->callsign = rose_route->neighbour;
96 rose_neigh->digipeat = NULL;
97 rose_neigh->ax25 = NULL;
98 rose_neigh->dev = dev;
99 rose_neigh->count = 0;
101 rose_neigh->dce_mode = 0;
102 rose_neigh->loopback = 0;
103 rose_neigh->number = rose_neigh_no++;
104 rose_neigh->restarted = 0;
106 skb_queue_head_init(&rose_neigh->queue);
108 init_timer(&rose_neigh->ftimer);
109 init_timer(&rose_neigh->t0timer);
111 if (rose_route->ndigis != 0) {
112 if ((rose_neigh->digipeat = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) {
118 rose_neigh->digipeat->ndigi = rose_route->ndigis;
119 rose_neigh->digipeat->lastrepeat = -1;
121 for (i = 0; i < rose_route->ndigis; i++) {
122 rose_neigh->digipeat->calls[i] =
123 rose_route->digipeaters[i];
124 rose_neigh->digipeat->repeated[i] = 0;
128 rose_neigh->next = rose_neigh_list;
129 rose_neigh_list = rose_neigh;
133 * This is a new node to be inserted into the list. Find where it needs
134 * to be inserted into the list, and insert it. We want to be sure
135 * to order the list in descending order of mask size to ensure that
136 * later when we are searching this list the first match will be the
139 if (rose_node == NULL) {
140 rose_tmpn = rose_node_list;
143 while (rose_tmpn != NULL) {
144 if (rose_tmpn->mask > rose_route->mask) {
145 rose_tmpp = rose_tmpn;
146 rose_tmpn = rose_tmpn->next;
152 /* create new node */
153 rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
154 if (rose_node == NULL) {
159 rose_node->address = rose_route->address;
160 rose_node->mask = rose_route->mask;
161 rose_node->count = 1;
162 rose_node->loopback = 0;
163 rose_node->neighbour[0] = rose_neigh;
165 if (rose_tmpn == NULL) {
166 if (rose_tmpp == NULL) { /* Empty list */
167 rose_node_list = rose_node;
168 rose_node->next = NULL;
170 rose_tmpp->next = rose_node;
171 rose_node->next = NULL;
174 if (rose_tmpp == NULL) { /* 1st node */
175 rose_node->next = rose_node_list;
176 rose_node_list = rose_node;
178 rose_tmpp->next = rose_node;
179 rose_node->next = rose_tmpn;
187 /* We have space, slot it in */
188 if (rose_node->count < 3) {
189 rose_node->neighbour[rose_node->count] = rose_neigh;
195 spin_unlock_bh(&rose_neigh_list_lock);
196 spin_unlock_bh(&rose_node_list_lock);
202 * Caller is holding rose_node_list_lock.
204 static void rose_remove_node(struct rose_node *rose_node)
208 if ((s = rose_node_list) == rose_node) {
209 rose_node_list = rose_node->next;
214 while (s != NULL && s->next != NULL) {
215 if (s->next == rose_node) {
216 s->next = rose_node->next;
226 * Caller is holding rose_neigh_list_lock.
228 static void rose_remove_neigh(struct rose_neigh *rose_neigh)
230 struct rose_neigh *s;
232 rose_stop_ftimer(rose_neigh);
233 rose_stop_t0timer(rose_neigh);
235 skb_queue_purge(&rose_neigh->queue);
237 if ((s = rose_neigh_list) == rose_neigh) {
238 rose_neigh_list = rose_neigh->next;
239 if (rose_neigh->ax25)
240 ax25_cb_put(rose_neigh->ax25);
241 kfree(rose_neigh->digipeat);
246 while (s != NULL && s->next != NULL) {
247 if (s->next == rose_neigh) {
248 s->next = rose_neigh->next;
249 if (rose_neigh->ax25)
250 ax25_cb_put(rose_neigh->ax25);
251 kfree(rose_neigh->digipeat);
261 * Caller is holding rose_route_list_lock.
263 static void rose_remove_route(struct rose_route *rose_route)
265 struct rose_route *s;
267 if (rose_route->neigh1 != NULL)
268 rose_route->neigh1->use--;
270 if (rose_route->neigh2 != NULL)
271 rose_route->neigh2->use--;
273 if ((s = rose_route_list) == rose_route) {
274 rose_route_list = rose_route->next;
279 while (s != NULL && s->next != NULL) {
280 if (s->next == rose_route) {
281 s->next = rose_route->next;
291 * "Delete" a node. Strictly speaking remove a route to a node. The node
292 * is only deleted if no routes are left to it.
294 static int rose_del_node(struct rose_route_struct *rose_route,
295 struct net_device *dev)
297 struct rose_node *rose_node;
298 struct rose_neigh *rose_neigh;
301 spin_lock_bh(&rose_node_list_lock);
302 spin_lock_bh(&rose_neigh_list_lock);
304 rose_node = rose_node_list;
305 while (rose_node != NULL) {
306 if ((rose_node->mask == rose_route->mask) &&
307 (rosecmpm(&rose_route->address, &rose_node->address,
308 rose_route->mask) == 0))
310 rose_node = rose_node->next;
313 if (rose_node == NULL || rose_node->loopback) {
318 rose_neigh = rose_neigh_list;
319 while (rose_neigh != NULL) {
320 if (ax25cmp(&rose_route->neighbour,
321 &rose_neigh->callsign) == 0 &&
322 rose_neigh->dev == dev)
324 rose_neigh = rose_neigh->next;
327 if (rose_neigh == NULL) {
332 for (i = 0; i < rose_node->count; i++) {
333 if (rose_node->neighbour[i] == rose_neigh) {
336 if (rose_neigh->count == 0 && rose_neigh->use == 0)
337 rose_remove_neigh(rose_neigh);
341 if (rose_node->count == 0) {
342 rose_remove_node(rose_node);
346 rose_node->neighbour[0] =
347 rose_node->neighbour[1];
349 rose_node->neighbour[1] =
350 rose_node->neighbour[2];
361 spin_unlock_bh(&rose_neigh_list_lock);
362 spin_unlock_bh(&rose_node_list_lock);
368 * Add the loopback neighbour.
370 void rose_add_loopback_neigh(void)
372 struct rose_neigh *sn;
374 rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_KERNEL);
375 if (!rose_loopback_neigh)
377 sn = rose_loopback_neigh;
379 sn->callsign = null_ax25_address;
387 sn->number = rose_neigh_no++;
390 skb_queue_head_init(&sn->queue);
392 init_timer(&sn->ftimer);
393 init_timer(&sn->t0timer);
395 spin_lock_bh(&rose_neigh_list_lock);
396 sn->next = rose_neigh_list;
397 rose_neigh_list = sn;
398 spin_unlock_bh(&rose_neigh_list_lock);
402 * Add a loopback node.
404 int rose_add_loopback_node(rose_address *address)
406 struct rose_node *rose_node;
409 spin_lock_bh(&rose_node_list_lock);
411 rose_node = rose_node_list;
412 while (rose_node != NULL) {
413 if ((rose_node->mask == 10) &&
414 (rosecmpm(address, &rose_node->address, 10) == 0) &&
417 rose_node = rose_node->next;
420 if (rose_node != NULL)
423 if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
428 rose_node->address = *address;
429 rose_node->mask = 10;
430 rose_node->count = 1;
431 rose_node->loopback = 1;
432 rose_node->neighbour[0] = rose_loopback_neigh;
434 /* Insert at the head of list. Address is always mask=10 */
435 rose_node->next = rose_node_list;
436 rose_node_list = rose_node;
438 rose_loopback_neigh->count++;
441 spin_unlock_bh(&rose_node_list_lock);
447 * Delete a loopback node.
449 void rose_del_loopback_node(rose_address *address)
451 struct rose_node *rose_node;
453 spin_lock_bh(&rose_node_list_lock);
455 rose_node = rose_node_list;
456 while (rose_node != NULL) {
457 if ((rose_node->mask == 10) &&
458 (rosecmpm(address, &rose_node->address, 10) == 0) &&
461 rose_node = rose_node->next;
464 if (rose_node == NULL)
467 rose_remove_node(rose_node);
469 rose_loopback_neigh->count--;
472 spin_unlock_bh(&rose_node_list_lock);
476 * A device has been removed. Remove its routes and neighbours.
478 void rose_rt_device_down(struct net_device *dev)
480 struct rose_neigh *s, *rose_neigh;
481 struct rose_node *t, *rose_node;
484 spin_lock_bh(&rose_node_list_lock);
485 spin_lock_bh(&rose_neigh_list_lock);
486 rose_neigh = rose_neigh_list;
487 while (rose_neigh != NULL) {
489 rose_neigh = rose_neigh->next;
494 rose_node = rose_node_list;
496 while (rose_node != NULL) {
498 rose_node = rose_node->next;
500 for (i = 0; i < t->count; i++) {
501 if (t->neighbour[i] != s)
508 t->neighbour[0] = t->neighbour[1];
510 t->neighbour[1] = t->neighbour[2];
520 rose_remove_neigh(s);
522 spin_unlock_bh(&rose_neigh_list_lock);
523 spin_unlock_bh(&rose_node_list_lock);
526 #if 0 /* Currently unused */
528 * A device has been removed. Remove its links.
530 void rose_route_device_down(struct net_device *dev)
532 struct rose_route *s, *rose_route;
534 spin_lock_bh(&rose_route_list_lock);
535 rose_route = rose_route_list;
536 while (rose_route != NULL) {
538 rose_route = rose_route->next;
540 if (s->neigh1->dev == dev || s->neigh2->dev == dev)
541 rose_remove_route(s);
543 spin_unlock_bh(&rose_route_list_lock);
548 * Clear all nodes and neighbours out, except for neighbours with
549 * active connections going through them.
550 * Do not clear loopback neighbour and nodes.
552 static int rose_clear_routes(void)
554 struct rose_neigh *s, *rose_neigh;
555 struct rose_node *t, *rose_node;
557 spin_lock_bh(&rose_node_list_lock);
558 spin_lock_bh(&rose_neigh_list_lock);
560 rose_neigh = rose_neigh_list;
561 rose_node = rose_node_list;
563 while (rose_node != NULL) {
565 rose_node = rose_node->next;
570 while (rose_neigh != NULL) {
572 rose_neigh = rose_neigh->next;
574 if (s->use == 0 && !s->loopback) {
576 rose_remove_neigh(s);
580 spin_unlock_bh(&rose_neigh_list_lock);
581 spin_unlock_bh(&rose_node_list_lock);
587 * Check that the device given is a valid AX.25 interface that is "up".
590 static struct net_device *rose_ax25_dev_find(char *devname)
592 struct net_device *dev;
594 if ((dev = __dev_get_by_name(&init_net, devname)) == NULL)
597 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
604 * Find the first active ROSE device, usually "rose0".
606 struct net_device *rose_dev_first(void)
608 struct net_device *dev, *first = NULL;
611 for_each_netdev_rcu(&init_net, dev) {
612 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
613 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
622 * Find the ROSE device for the given address.
624 struct net_device *rose_dev_get(rose_address *addr)
626 struct net_device *dev;
629 for_each_netdev_rcu(&init_net, dev) {
630 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
641 static int rose_dev_exists(rose_address *addr)
643 struct net_device *dev;
646 for_each_netdev_rcu(&init_net, dev) {
647 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
659 struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
661 struct rose_route *rose_route;
663 for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
664 if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
665 (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
672 * Find a neighbour or a route given a ROSE address.
674 struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
675 unsigned char *diagnostic, int new)
677 struct rose_neigh *res = NULL;
678 struct rose_node *node;
682 if (!new) spin_lock_bh(&rose_node_list_lock);
683 for (node = rose_node_list; node != NULL; node = node->next) {
684 if (rosecmpm(addr, &node->address, node->mask) == 0) {
685 for (i = 0; i < node->count; i++) {
687 if (node->neighbour[i]->restarted) {
688 res = node->neighbour[i];
693 if (!rose_ftimer_running(node->neighbour[i])) {
694 res = node->neighbour[i];
704 *cause = ROSE_OUT_OF_ORDER;
707 *cause = ROSE_NOT_OBTAINABLE;
712 if (!new) spin_unlock_bh(&rose_node_list_lock);
718 * Handle the ioctls that control the routing functions.
720 int rose_rt_ioctl(unsigned int cmd, void __user *arg)
722 struct rose_route_struct rose_route;
723 struct net_device *dev;
728 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
730 if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
732 if (rose_dev_exists(&rose_route.address)) /* Can't add routes to ourself */
734 if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
736 if (rose_route.ndigis > AX25_MAX_DIGIS)
738 err = rose_add_node(&rose_route, dev);
742 if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
744 if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
746 err = rose_del_node(&rose_route, dev);
750 return rose_clear_routes();
759 static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
761 struct rose_route *rose_route, *s;
763 rose_neigh->restarted = 0;
765 rose_stop_t0timer(rose_neigh);
766 rose_start_ftimer(rose_neigh);
768 skb_queue_purge(&rose_neigh->queue);
770 spin_lock_bh(&rose_route_list_lock);
772 rose_route = rose_route_list;
774 while (rose_route != NULL) {
775 if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
776 (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
777 (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
778 s = rose_route->next;
779 rose_remove_route(rose_route);
784 if (rose_route->neigh1 == rose_neigh) {
785 rose_route->neigh1->use--;
786 rose_route->neigh1 = NULL;
787 rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
790 if (rose_route->neigh2 == rose_neigh) {
791 rose_route->neigh2->use--;
792 rose_route->neigh2 = NULL;
793 rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
796 rose_route = rose_route->next;
798 spin_unlock_bh(&rose_route_list_lock);
802 * A level 2 link has timed out, therefore it appears to be a poor link,
803 * then don't use that neighbour until it is reset. Blow away all through
804 * routes and connections using this route.
806 void rose_link_failed(ax25_cb *ax25, int reason)
808 struct rose_neigh *rose_neigh;
810 spin_lock_bh(&rose_neigh_list_lock);
811 rose_neigh = rose_neigh_list;
812 while (rose_neigh != NULL) {
813 if (rose_neigh->ax25 == ax25)
815 rose_neigh = rose_neigh->next;
818 if (rose_neigh != NULL) {
819 rose_neigh->ax25 = NULL;
822 rose_del_route_by_neigh(rose_neigh);
823 rose_kill_by_neigh(rose_neigh);
825 spin_unlock_bh(&rose_neigh_list_lock);
829 * A device has been "downed" remove its link status. Blow away all
830 * through routes and connections that use this device.
832 void rose_link_device_down(struct net_device *dev)
834 struct rose_neigh *rose_neigh;
836 for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
837 if (rose_neigh->dev == dev) {
838 rose_del_route_by_neigh(rose_neigh);
839 rose_kill_by_neigh(rose_neigh);
845 * Route a frame to an appropriate AX.25 connection.
847 int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
849 struct rose_neigh *rose_neigh, *new_neigh;
850 struct rose_route *rose_route;
851 struct rose_facilities_struct facilities;
852 rose_address *src_addr, *dest_addr;
854 unsigned short frametype;
855 unsigned int lci, new_lci;
856 unsigned char cause, diagnostic;
857 struct net_device *dev;
862 if (call_in_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT)
866 frametype = skb->data[2];
867 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
868 src_addr = (rose_address *)(skb->data + 9);
869 dest_addr = (rose_address *)(skb->data + 4);
871 spin_lock_bh(&rose_neigh_list_lock);
872 spin_lock_bh(&rose_route_list_lock);
874 rose_neigh = rose_neigh_list;
875 while (rose_neigh != NULL) {
876 if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
877 ax25->ax25_dev->dev == rose_neigh->dev)
879 rose_neigh = rose_neigh->next;
882 if (rose_neigh == NULL) {
883 printk("rose_route : unknown neighbour or device %s\n",
884 ax2asc(buf, &ax25->dest_addr));
889 * Obviously the link is working, halt the ftimer.
891 rose_stop_ftimer(rose_neigh);
894 * LCI of zero is always for us, and its always a restart
898 rose_link_rx_restart(skb, rose_neigh, frametype);
903 * Find an existing socket.
905 if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
906 if (frametype == ROSE_CALL_REQUEST) {
907 struct rose_sock *rose = rose_sk(sk);
909 /* Remove an existing unused socket */
910 rose_clear_queues(sk);
911 rose->cause = ROSE_NETWORK_CONGESTION;
912 rose->diagnostic = 0;
913 rose->neighbour->use--;
914 rose->neighbour = NULL;
916 rose->state = ROSE_STATE_0;
917 sk->sk_state = TCP_CLOSE;
919 sk->sk_shutdown |= SEND_SHUTDOWN;
920 if (!sock_flag(sk, SOCK_DEAD)) {
921 sk->sk_state_change(sk);
922 sock_set_flag(sk, SOCK_DEAD);
926 skb_reset_transport_header(skb);
927 res = rose_process_rx_frame(sk, skb);
933 * Is is a Call Request and is it for us ?
935 if (frametype == ROSE_CALL_REQUEST)
936 if ((dev = rose_dev_get(dest_addr)) != NULL) {
937 res = rose_rx_call_request(skb, dev, rose_neigh, lci);
942 if (!sysctl_rose_routing_control) {
943 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
948 * Route it to the next in line if we have an entry for it.
950 rose_route = rose_route_list;
951 while (rose_route != NULL) {
952 if (rose_route->lci1 == lci &&
953 rose_route->neigh1 == rose_neigh) {
954 if (frametype == ROSE_CALL_REQUEST) {
955 /* F6FBB - Remove an existing unused route */
956 rose_remove_route(rose_route);
958 } else if (rose_route->neigh2 != NULL) {
959 skb->data[0] &= 0xF0;
960 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
961 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
962 rose_transmit_link(skb, rose_route->neigh2);
963 if (frametype == ROSE_CLEAR_CONFIRMATION)
964 rose_remove_route(rose_route);
968 if (frametype == ROSE_CLEAR_CONFIRMATION)
969 rose_remove_route(rose_route);
973 if (rose_route->lci2 == lci &&
974 rose_route->neigh2 == rose_neigh) {
975 if (frametype == ROSE_CALL_REQUEST) {
976 /* F6FBB - Remove an existing unused route */
977 rose_remove_route(rose_route);
979 } else if (rose_route->neigh1 != NULL) {
980 skb->data[0] &= 0xF0;
981 skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
982 skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
983 rose_transmit_link(skb, rose_route->neigh1);
984 if (frametype == ROSE_CLEAR_CONFIRMATION)
985 rose_remove_route(rose_route);
989 if (frametype == ROSE_CLEAR_CONFIRMATION)
990 rose_remove_route(rose_route);
994 rose_route = rose_route->next;
999 * 1. The frame isn't for us,
1000 * 2. It isn't "owned" by any existing route.
1002 if (frametype != ROSE_CALL_REQUEST) { /* XXX */
1007 len = (((skb->data[3] >> 4) & 0x0F) + 1) >> 1;
1008 len += (((skb->data[3] >> 0) & 0x0F) + 1) >> 1;
1010 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1012 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
1013 rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1018 * Check for routing loops.
1020 rose_route = rose_route_list;
1021 while (rose_route != NULL) {
1022 if (rose_route->rand == facilities.rand &&
1023 rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1024 ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1025 ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1026 rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1029 rose_route = rose_route->next;
1032 if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic, 1)) == NULL) {
1033 rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1037 if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1038 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1042 if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1043 rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1047 rose_route->lci1 = lci;
1048 rose_route->src_addr = *src_addr;
1049 rose_route->dest_addr = *dest_addr;
1050 rose_route->src_call = facilities.dest_call;
1051 rose_route->dest_call = facilities.source_call;
1052 rose_route->rand = facilities.rand;
1053 rose_route->neigh1 = rose_neigh;
1054 rose_route->lci2 = new_lci;
1055 rose_route->neigh2 = new_neigh;
1057 rose_route->neigh1->use++;
1058 rose_route->neigh2->use++;
1060 rose_route->next = rose_route_list;
1061 rose_route_list = rose_route;
1063 skb->data[0] &= 0xF0;
1064 skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1065 skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
1067 rose_transmit_link(skb, rose_route->neigh2);
1071 spin_unlock_bh(&rose_route_list_lock);
1072 spin_unlock_bh(&rose_neigh_list_lock);
1077 #ifdef CONFIG_PROC_FS
1079 static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1080 __acquires(rose_node_list_lock)
1082 struct rose_node *rose_node;
1085 spin_lock_bh(&rose_node_list_lock);
1087 return SEQ_START_TOKEN;
1089 for (rose_node = rose_node_list; rose_node && i < *pos;
1090 rose_node = rose_node->next, ++i);
1092 return (i == *pos) ? rose_node : NULL;
1095 static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1099 return (v == SEQ_START_TOKEN) ? rose_node_list
1100 : ((struct rose_node *)v)->next;
1103 static void rose_node_stop(struct seq_file *seq, void *v)
1104 __releases(rose_node_list_lock)
1106 spin_unlock_bh(&rose_node_list_lock);
1109 static int rose_node_show(struct seq_file *seq, void *v)
1114 if (v == SEQ_START_TOKEN)
1115 seq_puts(seq, "address mask n neigh neigh neigh\n");
1117 const struct rose_node *rose_node = v;
1118 /* if (rose_node->loopback) {
1119 seq_printf(seq, "%-10s %04d 1 loopback\n",
1120 rose2asc(rsbuf, &rose_node->address),
1123 seq_printf(seq, "%-10s %04d %d",
1124 rose2asc(rsbuf, &rose_node->address),
1128 for (i = 0; i < rose_node->count; i++)
1129 seq_printf(seq, " %05d",
1130 rose_node->neighbour[i]->number);
1132 seq_puts(seq, "\n");
1138 static const struct seq_operations rose_node_seqops = {
1139 .start = rose_node_start,
1140 .next = rose_node_next,
1141 .stop = rose_node_stop,
1142 .show = rose_node_show,
1145 static int rose_nodes_open(struct inode *inode, struct file *file)
1147 return seq_open(file, &rose_node_seqops);
1150 const struct file_operations rose_nodes_fops = {
1151 .owner = THIS_MODULE,
1152 .open = rose_nodes_open,
1154 .llseek = seq_lseek,
1155 .release = seq_release,
1158 static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1159 __acquires(rose_neigh_list_lock)
1161 struct rose_neigh *rose_neigh;
1164 spin_lock_bh(&rose_neigh_list_lock);
1166 return SEQ_START_TOKEN;
1168 for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1169 rose_neigh = rose_neigh->next, ++i);
1171 return (i == *pos) ? rose_neigh : NULL;
1174 static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1178 return (v == SEQ_START_TOKEN) ? rose_neigh_list
1179 : ((struct rose_neigh *)v)->next;
1182 static void rose_neigh_stop(struct seq_file *seq, void *v)
1183 __releases(rose_neigh_list_lock)
1185 spin_unlock_bh(&rose_neigh_list_lock);
1188 static int rose_neigh_show(struct seq_file *seq, void *v)
1193 if (v == SEQ_START_TOKEN)
1195 "addr callsign dev count use mode restart t0 tf digipeaters\n");
1197 struct rose_neigh *rose_neigh = v;
1199 /* if (!rose_neigh->loopback) { */
1200 seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
1202 (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
1203 rose_neigh->dev ? rose_neigh->dev->name : "???",
1206 (rose_neigh->dce_mode) ? "DCE" : "DTE",
1207 (rose_neigh->restarted) ? "yes" : "no",
1208 ax25_display_timer(&rose_neigh->t0timer) / HZ,
1209 ax25_display_timer(&rose_neigh->ftimer) / HZ);
1211 if (rose_neigh->digipeat != NULL) {
1212 for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1213 seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
1216 seq_puts(seq, "\n");
1222 static const struct seq_operations rose_neigh_seqops = {
1223 .start = rose_neigh_start,
1224 .next = rose_neigh_next,
1225 .stop = rose_neigh_stop,
1226 .show = rose_neigh_show,
1229 static int rose_neigh_open(struct inode *inode, struct file *file)
1231 return seq_open(file, &rose_neigh_seqops);
1234 const struct file_operations rose_neigh_fops = {
1235 .owner = THIS_MODULE,
1236 .open = rose_neigh_open,
1238 .llseek = seq_lseek,
1239 .release = seq_release,
1243 static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1244 __acquires(rose_route_list_lock)
1246 struct rose_route *rose_route;
1249 spin_lock_bh(&rose_route_list_lock);
1251 return SEQ_START_TOKEN;
1253 for (rose_route = rose_route_list; rose_route && i < *pos;
1254 rose_route = rose_route->next, ++i);
1256 return (i == *pos) ? rose_route : NULL;
1259 static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1263 return (v == SEQ_START_TOKEN) ? rose_route_list
1264 : ((struct rose_route *)v)->next;
1267 static void rose_route_stop(struct seq_file *seq, void *v)
1268 __releases(rose_route_list_lock)
1270 spin_unlock_bh(&rose_route_list_lock);
1273 static int rose_route_show(struct seq_file *seq, void *v)
1275 char buf[11], rsbuf[11];
1277 if (v == SEQ_START_TOKEN)
1279 "lci address callsign neigh <-> lci address callsign neigh\n");
1281 struct rose_route *rose_route = v;
1283 if (rose_route->neigh1)
1285 "%3.3X %-10s %-9s %05d ",
1287 rose2asc(rsbuf, &rose_route->src_addr),
1288 ax2asc(buf, &rose_route->src_call),
1289 rose_route->neigh1->number);
1294 if (rose_route->neigh2)
1296 "%3.3X %-10s %-9s %05d\n",
1298 rose2asc(rsbuf, &rose_route->dest_addr),
1299 ax2asc(buf, &rose_route->dest_call),
1300 rose_route->neigh2->number);
1308 static const struct seq_operations rose_route_seqops = {
1309 .start = rose_route_start,
1310 .next = rose_route_next,
1311 .stop = rose_route_stop,
1312 .show = rose_route_show,
1315 static int rose_route_open(struct inode *inode, struct file *file)
1317 return seq_open(file, &rose_route_seqops);
1320 const struct file_operations rose_routes_fops = {
1321 .owner = THIS_MODULE,
1322 .open = rose_route_open,
1324 .llseek = seq_lseek,
1325 .release = seq_release,
1328 #endif /* CONFIG_PROC_FS */
1331 * Release all memory associated with ROSE routing structures.
1333 void __exit rose_rt_free(void)
1335 struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1336 struct rose_node *t, *rose_node = rose_node_list;
1337 struct rose_route *u, *rose_route = rose_route_list;
1339 while (rose_neigh != NULL) {
1341 rose_neigh = rose_neigh->next;
1343 rose_remove_neigh(s);
1346 while (rose_node != NULL) {
1348 rose_node = rose_node->next;
1350 rose_remove_node(t);
1353 while (rose_route != NULL) {
1355 rose_route = rose_route->next;
1357 rose_remove_route(u);