4 * This code REQUIRES 2.1.15 or higher/ NET3.038
7 * This module is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * NET/ROM 001 Jonathan(G4KLX) First attempt.
14 * NET/ROM 003 Jonathan(G4KLX) Use SIOCADDRT/SIOCDELRT ioctl values
16 * Use '*' for a blank mnemonic in /proc/net/nr_nodes.
17 * Change default quality for new neighbour when same
19 * Alan Cox(GW4PTS) Added the firewall hooks.
20 * NET/ROM 006 Jonathan(G4KLX) Added the setting of digipeated neighbours.
21 * Tomi(OH2BNS) Routing quality and link failure changes.
22 * Device refcnt fixes.
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/socket.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/timer.h>
32 #include <linux/string.h>
33 #include <linux/sockios.h>
34 #include <linux/net.h>
36 #include <linux/inet.h>
37 #include <linux/netdevice.h>
39 #include <linux/if_arp.h>
40 #include <linux/skbuff.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
44 #include <linux/fcntl.h>
45 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
47 #include <linux/interrupt.h>
48 #include <linux/notifier.h>
49 #include <linux/netfilter.h>
50 #include <linux/init.h>
51 #include <net/netrom.h>
53 static unsigned int nr_neigh_no = 1;
55 static struct nr_node *nr_node_list;
56 static struct nr_neigh *nr_neigh_list;
58 static void nr_remove_neigh(struct nr_neigh *);
61 * Add a new route to a node, and in the process add the node and the
62 * neighbour if it is new.
64 static int nr_add_node(ax25_address *nr, const char *mnemonic, ax25_address *ax25,
65 ax25_digi *ax25_digi, struct net_device *dev, int quality, int obs_count)
67 struct nr_node *nr_node;
68 struct nr_neigh *nr_neigh;
69 struct nr_route nr_route;
70 struct net_device *tdev;
74 /* Can't add routes to ourself */
75 if ((tdev = nr_dev_get(nr)) != NULL) {
80 for (nr_node = nr_node_list; nr_node != NULL; nr_node = nr_node->next)
81 if (ax25cmp(nr, &nr_node->callsign) == 0)
84 for (nr_neigh = nr_neigh_list; nr_neigh != NULL; nr_neigh = nr_neigh->next)
85 if (ax25cmp(ax25, &nr_neigh->callsign) == 0 && nr_neigh->dev == dev)
89 * The L2 link to a neighbour has failed in the past
90 * and now a frame comes from this neighbour. We assume
91 * it was a temporary trouble with the link and reset the
92 * routes now (and not wait for a node broadcast).
94 if (nr_neigh != NULL && nr_neigh->failed != 0 && quality == 0) {
97 for (node = nr_node_list; node != NULL; node = node->next)
98 for (i = 0; i < node->count; i++)
99 if (node->routes[i].neighbour == nr_neigh)
104 if (nr_neigh != NULL)
105 nr_neigh->failed = 0;
107 if (quality == 0 && nr_neigh != NULL && nr_node != NULL)
110 if (nr_neigh == NULL) {
111 if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL)
114 nr_neigh->callsign = *ax25;
115 nr_neigh->digipeat = NULL;
116 nr_neigh->ax25 = NULL;
118 nr_neigh->quality = sysctl_netrom_default_path_quality;
119 nr_neigh->locked = 0;
121 nr_neigh->number = nr_neigh_no++;
122 nr_neigh->failed = 0;
124 if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
125 if ((nr_neigh->digipeat = kmalloc(sizeof(*ax25_digi), GFP_KERNEL)) == NULL) {
129 memcpy(nr_neigh->digipeat, ax25_digi, sizeof(ax25_digi));
132 dev_hold(nr_neigh->dev);
137 nr_neigh->next = nr_neigh_list;
138 nr_neigh_list = nr_neigh;
140 restore_flags(flags);
143 if (quality != 0 && ax25cmp(nr, ax25) == 0 && !nr_neigh->locked)
144 nr_neigh->quality = quality;
146 if (nr_node == NULL) {
147 if ((nr_node = kmalloc(sizeof(*nr_node), GFP_ATOMIC)) == NULL)
150 nr_node->callsign = *nr;
151 strcpy(nr_node->mnemonic, mnemonic);
156 nr_node->routes[0].quality = quality;
157 nr_node->routes[0].obs_count = obs_count;
158 nr_node->routes[0].neighbour = nr_neigh;
163 nr_node->next = nr_node_list;
164 nr_node_list = nr_node;
166 restore_flags(flags);
174 strcpy(nr_node->mnemonic, mnemonic);
176 for (found = 0, i = 0; i < nr_node->count; i++) {
177 if (nr_node->routes[i].neighbour == nr_neigh) {
178 nr_node->routes[i].quality = quality;
179 nr_node->routes[i].obs_count = obs_count;
186 /* We have space at the bottom, slot it in */
187 if (nr_node->count < 3) {
188 nr_node->routes[2] = nr_node->routes[1];
189 nr_node->routes[1] = nr_node->routes[0];
191 nr_node->routes[0].quality = quality;
192 nr_node->routes[0].obs_count = obs_count;
193 nr_node->routes[0].neighbour = nr_neigh;
199 /* It must be better than the worst */
200 if (quality > nr_node->routes[2].quality) {
201 nr_node->routes[2].neighbour->count--;
203 if (nr_node->routes[2].neighbour->count == 0 && !nr_node->routes[2].neighbour->locked)
204 nr_remove_neigh(nr_node->routes[2].neighbour);
206 nr_node->routes[2].quality = quality;
207 nr_node->routes[2].obs_count = obs_count;
208 nr_node->routes[2].neighbour = nr_neigh;
215 /* Now re-sort the routes in quality order */
216 switch (nr_node->count) {
218 if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
219 switch (nr_node->which) {
220 case 0: nr_node->which = 1; break;
221 case 1: nr_node->which = 0; break;
224 nr_route = nr_node->routes[0];
225 nr_node->routes[0] = nr_node->routes[1];
226 nr_node->routes[1] = nr_route;
228 if (nr_node->routes[2].quality > nr_node->routes[1].quality) {
229 switch (nr_node->which) {
230 case 1: nr_node->which = 2; break;
231 case 2: nr_node->which = 1; break;
234 nr_route = nr_node->routes[1];
235 nr_node->routes[1] = nr_node->routes[2];
236 nr_node->routes[2] = nr_route;
239 if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
240 switch (nr_node->which) {
241 case 0: nr_node->which = 1; break;
242 case 1: nr_node->which = 0; break;
245 nr_route = nr_node->routes[0];
246 nr_node->routes[0] = nr_node->routes[1];
247 nr_node->routes[1] = nr_route;
253 for (i = 0; i < nr_node->count; i++) {
254 if (nr_node->routes[i].neighbour == nr_neigh) {
255 if (i < nr_node->which)
264 static void nr_remove_node(struct nr_node *nr_node)
272 if ((s = nr_node_list) == nr_node) {
273 nr_node_list = nr_node->next;
274 restore_flags(flags);
279 while (s != NULL && s->next != NULL) {
280 if (s->next == nr_node) {
281 s->next = nr_node->next;
282 restore_flags(flags);
290 restore_flags(flags);
293 static void nr_remove_neigh(struct nr_neigh *nr_neigh)
301 if ((s = nr_neigh_list) == nr_neigh) {
302 nr_neigh_list = nr_neigh->next;
303 restore_flags(flags);
304 dev_put(nr_neigh->dev);
305 if (nr_neigh->digipeat != NULL)
306 kfree(nr_neigh->digipeat);
311 while (s != NULL && s->next != NULL) {
312 if (s->next == nr_neigh) {
313 s->next = nr_neigh->next;
314 restore_flags(flags);
315 dev_put(nr_neigh->dev);
316 if (nr_neigh->digipeat != NULL)
317 kfree(nr_neigh->digipeat);
325 restore_flags(flags);
329 * "Delete" a node. Strictly speaking remove a route to a node. The node
330 * is only deleted if no routes are left to it.
332 static int nr_del_node(ax25_address *callsign, ax25_address *neighbour, struct net_device *dev)
334 struct nr_node *nr_node;
335 struct nr_neigh *nr_neigh;
338 for (nr_node = nr_node_list; nr_node != NULL; nr_node = nr_node->next)
339 if (ax25cmp(callsign, &nr_node->callsign) == 0)
342 if (nr_node == NULL) return -EINVAL;
344 for (nr_neigh = nr_neigh_list; nr_neigh != NULL; nr_neigh = nr_neigh->next)
345 if (ax25cmp(neighbour, &nr_neigh->callsign) == 0 && nr_neigh->dev == dev)
348 if (nr_neigh == NULL) return -EINVAL;
350 for (i = 0; i < nr_node->count; i++) {
351 if (nr_node->routes[i].neighbour == nr_neigh) {
354 if (nr_neigh->count == 0 && !nr_neigh->locked)
355 nr_remove_neigh(nr_neigh);
359 if (nr_node->count == 0) {
360 nr_remove_node(nr_node);
364 nr_node->routes[0] = nr_node->routes[1];
366 nr_node->routes[1] = nr_node->routes[2];
380 * Lock a neighbour with a quality.
382 static int nr_add_neigh(ax25_address *callsign, ax25_digi *ax25_digi, struct net_device *dev, unsigned int quality)
384 struct nr_neigh *nr_neigh;
387 for (nr_neigh = nr_neigh_list; nr_neigh != NULL; nr_neigh = nr_neigh->next) {
388 if (ax25cmp(callsign, &nr_neigh->callsign) == 0 && nr_neigh->dev == dev) {
389 nr_neigh->quality = quality;
390 nr_neigh->locked = 1;
395 if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL)
398 nr_neigh->callsign = *callsign;
399 nr_neigh->digipeat = NULL;
400 nr_neigh->ax25 = NULL;
402 nr_neigh->quality = quality;
403 nr_neigh->locked = 1;
405 nr_neigh->number = nr_neigh_no++;
406 nr_neigh->failed = 0;
408 if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
409 if ((nr_neigh->digipeat = kmalloc(sizeof(*ax25_digi), GFP_KERNEL)) == NULL) {
413 memcpy(nr_neigh->digipeat, ax25_digi, sizeof(ax25_digi));
416 dev_hold(nr_neigh->dev);
421 nr_neigh->next = nr_neigh_list;
422 nr_neigh_list = nr_neigh;
424 restore_flags(flags);
430 * "Delete" a neighbour. The neighbour is only removed if the number
431 * of nodes that may use it is zero.
433 static int nr_del_neigh(ax25_address *callsign, struct net_device *dev, unsigned int quality)
435 struct nr_neigh *nr_neigh;
437 for (nr_neigh = nr_neigh_list; nr_neigh != NULL; nr_neigh = nr_neigh->next)
438 if (ax25cmp(callsign, &nr_neigh->callsign) == 0 && nr_neigh->dev == dev)
441 if (nr_neigh == NULL) return -EINVAL;
443 nr_neigh->quality = quality;
444 nr_neigh->locked = 0;
446 if (nr_neigh->count == 0)
447 nr_remove_neigh(nr_neigh);
453 * Decrement the obsolescence count by one. If a route is reduced to a
454 * count of zero, remove it. Also remove any unlocked neighbours with
455 * zero nodes routing via it.
457 static int nr_dec_obs(void)
459 struct nr_neigh *nr_neigh;
460 struct nr_node *s, *nr_node;
463 nr_node = nr_node_list;
465 while (nr_node != NULL) {
467 nr_node = nr_node->next;
469 for (i = 0; i < s->count; i++) {
470 switch (s->routes[i].obs_count) {
472 case 0: /* A locked entry */
475 case 1: /* From 1 -> 0 */
476 nr_neigh = s->routes[i].neighbour;
480 if (nr_neigh->count == 0 && !nr_neigh->locked)
481 nr_remove_neigh(nr_neigh);
487 s->routes[0] = s->routes[1];
489 s->routes[1] = s->routes[2];
496 s->routes[i].obs_count--;
510 * A device has been removed. Remove its routes and neighbours.
512 void nr_rt_device_down(struct net_device *dev)
514 struct nr_neigh *s, *nr_neigh = nr_neigh_list;
515 struct nr_node *t, *nr_node;
518 while (nr_neigh != NULL) {
520 nr_neigh = nr_neigh->next;
523 nr_node = nr_node_list;
525 while (nr_node != NULL) {
527 nr_node = nr_node->next;
529 for (i = 0; i < t->count; i++) {
530 if (t->routes[i].neighbour == s) {
535 t->routes[0] = t->routes[1];
537 t->routes[1] = t->routes[2];
554 * Check that the device given is a valid AX.25 interface that is "up".
555 * Or a valid ethernet interface with an AX.25 callsign binding.
557 static struct net_device *nr_ax25_dev_get(char *devname)
559 struct net_device *dev;
561 if ((dev = dev_get_by_name(devname)) == NULL)
564 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
572 * Find the first active NET/ROM device, usually "nr0".
574 struct net_device *nr_dev_first(void)
576 struct net_device *dev, *first = NULL;
578 read_lock(&dev_base_lock);
579 for (dev = dev_base; dev != NULL; dev = dev->next) {
580 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM)
581 if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
588 read_unlock(&dev_base_lock);
594 * Find the NET/ROM device for the given callsign.
596 struct net_device *nr_dev_get(ax25_address *addr)
598 struct net_device *dev;
600 read_lock(&dev_base_lock);
601 for (dev = dev_base; dev != NULL; dev = dev->next) {
602 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM && ax25cmp(addr, (ax25_address *)dev->dev_addr) == 0) {
608 read_unlock(&dev_base_lock);
612 static ax25_digi *nr_call_to_digi(int ndigis, ax25_address *digipeaters)
614 static ax25_digi ax25_digi;
620 for (i = 0; i < ndigis; i++) {
621 ax25_digi.calls[i] = digipeaters[i];
622 ax25_digi.repeated[i] = 0;
625 ax25_digi.ndigi = ndigis;
626 ax25_digi.lastrepeat = -1;
632 * Handle the ioctls that control the routing functions.
634 int nr_rt_ioctl(unsigned int cmd, void *arg)
636 struct nr_route_struct nr_route;
637 struct net_device *dev;
643 if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
645 if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
647 if (nr_route.ndigis < 0 || nr_route.ndigis > AX25_MAX_DIGIS) {
651 switch (nr_route.type) {
653 ret = nr_add_node(&nr_route.callsign,
656 nr_call_to_digi(nr_route.ndigis, nr_route.digipeaters),
657 dev, nr_route.quality,
661 ret = nr_add_neigh(&nr_route.callsign,
662 nr_call_to_digi(nr_route.ndigis, nr_route.digipeaters),
663 dev, nr_route.quality);
673 if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
675 if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
677 switch (nr_route.type) {
679 ret = nr_del_node(&nr_route.callsign,
680 &nr_route.neighbour, dev);
683 ret = nr_del_neigh(&nr_route.callsign,
684 dev, nr_route.quality);
704 * A level 2 link has timed out, therefore it appears to be a poor link,
705 * then don't use that neighbour until it is reset.
707 void nr_link_failed(ax25_cb *ax25, int reason)
709 struct nr_neigh *nr_neigh;
710 struct nr_node *nr_node;
712 for (nr_neigh = nr_neigh_list; nr_neigh != NULL; nr_neigh = nr_neigh->next)
713 if (nr_neigh->ax25 == ax25)
716 if (nr_neigh == NULL) return;
718 nr_neigh->ax25 = NULL;
720 if (++nr_neigh->failed < sysctl_netrom_link_fails_count) return;
722 for (nr_node = nr_node_list; nr_node != NULL; nr_node = nr_node->next)
723 if (nr_node->which < nr_node->count && nr_node->routes[nr_node->which].neighbour == nr_neigh)
728 * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
729 * indicates an internally generated frame.
731 int nr_route_frame(struct sk_buff *skb, ax25_cb *ax25)
733 ax25_address *nr_src, *nr_dest;
734 struct nr_neigh *nr_neigh;
735 struct nr_node *nr_node;
736 struct net_device *dev;
740 nr_src = (ax25_address *)(skb->data + 0);
741 nr_dest = (ax25_address *)(skb->data + 7);
744 nr_add_node(nr_src, "", &ax25->dest_addr, ax25->digipeat,
745 ax25->ax25_dev->dev, 0, sysctl_netrom_obsolescence_count_initialiser);
747 if ((dev = nr_dev_get(nr_dest)) != NULL) { /* Its for me */
750 if (ax25 == NULL) /* Its from me */
751 ret = nr_loopback_queue(skb);
753 ret = nr_rx_frame(skb, dev);
759 if (!sysctl_netrom_routing_control && ax25 != NULL)
762 /* Its Time-To-Live has expired */
763 if (--skb->data[14] == 0)
766 for (nr_node = nr_node_list; nr_node != NULL; nr_node = nr_node->next)
767 if (ax25cmp(nr_dest, &nr_node->callsign) == 0)
770 if (nr_node == NULL || nr_node->which >= nr_node->count)
773 nr_neigh = nr_node->routes[nr_node->which].neighbour;
775 if ((dev = nr_dev_first()) == NULL)
778 dptr = skb_push(skb, 1);
779 *dptr = AX25_P_NETROM;
781 nr_neigh->ax25 = ax25_send_frame(skb, 256, (ax25_address *)dev->dev_addr, &nr_neigh->callsign, nr_neigh->digipeat, nr_neigh->dev);
785 return (nr_neigh->ax25 != NULL);
788 int nr_nodes_get_info(char *buffer, char **start, off_t offset, int length)
790 struct nr_node *nr_node;
798 len += sprintf(buffer, "callsign mnemonic w n qual obs neigh qual obs neigh qual obs neigh\n");
800 for (nr_node = nr_node_list; nr_node != NULL; nr_node = nr_node->next) {
801 len += sprintf(buffer + len, "%-9s %-7s %d %d",
802 ax2asc(&nr_node->callsign),
803 (nr_node->mnemonic[0] == '\0') ? "*" : nr_node->mnemonic,
807 for (i = 0; i < nr_node->count; i++) {
808 len += sprintf(buffer + len, " %3d %d %05d",
809 nr_node->routes[i].quality,
810 nr_node->routes[i].obs_count,
811 nr_node->routes[i].neighbour->number);
814 len += sprintf(buffer + len, "\n");
823 if (pos > offset + length)
829 *start = buffer + (offset - begin);
830 len -= (offset - begin);
832 if (len > length) len = length;
837 int nr_neigh_get_info(char *buffer, char **start, off_t offset, int length)
839 struct nr_neigh *nr_neigh;
847 len += sprintf(buffer, "addr callsign dev qual lock count failed digipeaters\n");
849 for (nr_neigh = nr_neigh_list; nr_neigh != NULL; nr_neigh = nr_neigh->next) {
850 len += sprintf(buffer + len, "%05d %-9s %-4s %3d %d %3d %3d",
852 ax2asc(&nr_neigh->callsign),
853 nr_neigh->dev ? nr_neigh->dev->name : "???",
859 if (nr_neigh->digipeat != NULL) {
860 for (i = 0; i < nr_neigh->digipeat->ndigi; i++)
861 len += sprintf(buffer + len, " %s", ax2asc(&nr_neigh->digipeat->calls[i]));
864 len += sprintf(buffer + len, "\n");
873 if (pos > offset + length)
879 *start = buffer + (offset - begin);
880 len -= (offset - begin);
882 if (len > length) len = length;
888 * Free all memory associated with the nodes and routes lists.
890 void __exit nr_rt_free(void)
892 struct nr_neigh *s, *nr_neigh = nr_neigh_list;
893 struct nr_node *t, *nr_node = nr_node_list;
895 while (nr_node != NULL) {
897 nr_node = nr_node->next;
902 while (nr_neigh != NULL) {
904 nr_neigh = nr_neigh->next;