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.
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/string.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
35 #include <linux/inet.h>
36 #include <linux/netdevice.h>
38 #include <linux/if_arp.h>
39 #include <linux/skbuff.h>
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
43 #include <linux/fcntl.h>
44 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
46 #include <linux/interrupt.h>
47 #include <linux/notifier.h>
48 #include <linux/netfilter.h>
49 #include <linux/init.h>
50 #include <net/netrom.h>
52 static unsigned int nr_neigh_no
= 1;
54 static struct nr_node
*nr_node_list
;
55 static struct nr_neigh
*nr_neigh_list
;
57 static void nr_remove_neigh(struct nr_neigh
*);
60 * Add a new route to a node, and in the process add the node and the
61 * neighbour if it is new.
63 static int nr_add_node(ax25_address
*nr
, const char *mnemonic
, ax25_address
*ax25
,
64 ax25_digi
*ax25_digi
, struct net_device
*dev
, int quality
, int obs_count
)
66 struct nr_node
*nr_node
;
67 struct nr_neigh
*nr_neigh
;
68 struct nr_route nr_route
;
72 if (nr_dev_get(nr
) != NULL
) /* Can't add routes to ourself */
75 for (nr_node
= nr_node_list
; nr_node
!= NULL
; nr_node
= nr_node
->next
)
76 if (ax25cmp(nr
, &nr_node
->callsign
) == 0)
79 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
)
80 if (ax25cmp(ax25
, &nr_neigh
->callsign
) == 0 && nr_neigh
->dev
== dev
)
84 * The L2 link to a neighbour has failed in the past
85 * and now a frame comes from this neighbour. We assume
86 * it was a temporary trouble with the link and reset the
87 * routes now (and not wait for a node broadcast).
89 if (nr_neigh
!= NULL
&& nr_neigh
->failed
!= 0 && quality
== 0) {
92 for (node
= nr_node_list
; node
!= NULL
; node
= node
->next
)
93 for (i
= 0; i
< node
->count
; i
++)
94 if (node
->routes
[i
].neighbour
== nr_neigh
)
100 nr_neigh
->failed
= 0;
102 if (quality
== 0 && nr_neigh
!= NULL
&& nr_node
!= NULL
)
105 if (nr_neigh
== NULL
) {
106 if ((nr_neigh
= kmalloc(sizeof(*nr_neigh
), GFP_ATOMIC
)) == NULL
)
109 nr_neigh
->callsign
= *ax25
;
110 nr_neigh
->digipeat
= NULL
;
111 nr_neigh
->ax25
= NULL
;
113 nr_neigh
->quality
= sysctl_netrom_default_path_quality
;
114 nr_neigh
->locked
= 0;
116 nr_neigh
->number
= nr_neigh_no
++;
117 nr_neigh
->failed
= 0;
119 if (ax25_digi
!= NULL
&& ax25_digi
->ndigi
> 0) {
120 if ((nr_neigh
->digipeat
= kmalloc(sizeof(*ax25_digi
), GFP_KERNEL
)) == NULL
) {
124 memcpy(nr_neigh
->digipeat
, ax25_digi
, sizeof(ax25_digi
));
130 nr_neigh
->next
= nr_neigh_list
;
131 nr_neigh_list
= nr_neigh
;
133 restore_flags(flags
);
136 if (quality
!= 0 && ax25cmp(nr
, ax25
) == 0 && !nr_neigh
->locked
)
137 nr_neigh
->quality
= quality
;
139 if (nr_node
== NULL
) {
140 if ((nr_node
= kmalloc(sizeof(*nr_node
), GFP_ATOMIC
)) == NULL
)
143 nr_node
->callsign
= *nr
;
144 strcpy(nr_node
->mnemonic
, mnemonic
);
149 nr_node
->routes
[0].quality
= quality
;
150 nr_node
->routes
[0].obs_count
= obs_count
;
151 nr_node
->routes
[0].neighbour
= nr_neigh
;
156 nr_node
->next
= nr_node_list
;
157 nr_node_list
= nr_node
;
159 restore_flags(flags
);
167 strcpy(nr_node
->mnemonic
, mnemonic
);
169 for (found
= 0, i
= 0; i
< nr_node
->count
; i
++) {
170 if (nr_node
->routes
[i
].neighbour
== nr_neigh
) {
171 nr_node
->routes
[i
].quality
= quality
;
172 nr_node
->routes
[i
].obs_count
= obs_count
;
179 /* We have space at the bottom, slot it in */
180 if (nr_node
->count
< 3) {
181 nr_node
->routes
[2] = nr_node
->routes
[1];
182 nr_node
->routes
[1] = nr_node
->routes
[0];
184 nr_node
->routes
[0].quality
= quality
;
185 nr_node
->routes
[0].obs_count
= obs_count
;
186 nr_node
->routes
[0].neighbour
= nr_neigh
;
192 /* It must be better than the worst */
193 if (quality
> nr_node
->routes
[2].quality
) {
194 nr_node
->routes
[2].neighbour
->count
--;
196 if (nr_node
->routes
[2].neighbour
->count
== 0 && !nr_node
->routes
[2].neighbour
->locked
)
197 nr_remove_neigh(nr_node
->routes
[2].neighbour
);
199 nr_node
->routes
[2].quality
= quality
;
200 nr_node
->routes
[2].obs_count
= obs_count
;
201 nr_node
->routes
[2].neighbour
= nr_neigh
;
208 /* Now re-sort the routes in quality order */
209 switch (nr_node
->count
) {
211 if (nr_node
->routes
[1].quality
> nr_node
->routes
[0].quality
) {
212 switch (nr_node
->which
) {
213 case 0: nr_node
->which
= 1; break;
214 case 1: nr_node
->which
= 0; break;
217 nr_route
= nr_node
->routes
[0];
218 nr_node
->routes
[0] = nr_node
->routes
[1];
219 nr_node
->routes
[1] = nr_route
;
221 if (nr_node
->routes
[2].quality
> nr_node
->routes
[1].quality
) {
222 switch (nr_node
->which
) {
223 case 1: nr_node
->which
= 2; break;
224 case 2: nr_node
->which
= 1; break;
227 nr_route
= nr_node
->routes
[1];
228 nr_node
->routes
[1] = nr_node
->routes
[2];
229 nr_node
->routes
[2] = nr_route
;
232 if (nr_node
->routes
[1].quality
> nr_node
->routes
[0].quality
) {
233 switch (nr_node
->which
) {
234 case 0: nr_node
->which
= 1; break;
235 case 1: nr_node
->which
= 0; break;
238 nr_route
= nr_node
->routes
[0];
239 nr_node
->routes
[0] = nr_node
->routes
[1];
240 nr_node
->routes
[1] = nr_route
;
246 for (i
= 0; i
< nr_node
->count
; i
++) {
247 if (nr_node
->routes
[i
].neighbour
== nr_neigh
) {
248 if (i
< nr_node
->which
)
257 static void nr_remove_node(struct nr_node
*nr_node
)
265 if ((s
= nr_node_list
) == nr_node
) {
266 nr_node_list
= nr_node
->next
;
267 restore_flags(flags
);
272 while (s
!= NULL
&& s
->next
!= NULL
) {
273 if (s
->next
== nr_node
) {
274 s
->next
= nr_node
->next
;
275 restore_flags(flags
);
283 restore_flags(flags
);
286 static void nr_remove_neigh(struct nr_neigh
*nr_neigh
)
294 if ((s
= nr_neigh_list
) == nr_neigh
) {
295 nr_neigh_list
= nr_neigh
->next
;
296 restore_flags(flags
);
297 if (nr_neigh
->digipeat
!= NULL
)
298 kfree(nr_neigh
->digipeat
);
303 while (s
!= NULL
&& s
->next
!= NULL
) {
304 if (s
->next
== nr_neigh
) {
305 s
->next
= nr_neigh
->next
;
306 restore_flags(flags
);
307 if (nr_neigh
->digipeat
!= NULL
)
308 kfree(nr_neigh
->digipeat
);
316 restore_flags(flags
);
320 * "Delete" a node. Strictly speaking remove a route to a node. The node
321 * is only deleted if no routes are left to it.
323 static int nr_del_node(ax25_address
*callsign
, ax25_address
*neighbour
, struct net_device
*dev
)
325 struct nr_node
*nr_node
;
326 struct nr_neigh
*nr_neigh
;
329 for (nr_node
= nr_node_list
; nr_node
!= NULL
; nr_node
= nr_node
->next
)
330 if (ax25cmp(callsign
, &nr_node
->callsign
) == 0)
333 if (nr_node
== NULL
) return -EINVAL
;
335 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
)
336 if (ax25cmp(neighbour
, &nr_neigh
->callsign
) == 0 && nr_neigh
->dev
== dev
)
339 if (nr_neigh
== NULL
) return -EINVAL
;
341 for (i
= 0; i
< nr_node
->count
; i
++) {
342 if (nr_node
->routes
[i
].neighbour
== nr_neigh
) {
345 if (nr_neigh
->count
== 0 && !nr_neigh
->locked
)
346 nr_remove_neigh(nr_neigh
);
350 if (nr_node
->count
== 0) {
351 nr_remove_node(nr_node
);
355 nr_node
->routes
[0] = nr_node
->routes
[1];
357 nr_node
->routes
[1] = nr_node
->routes
[2];
371 * Lock a neighbour with a quality.
373 static int nr_add_neigh(ax25_address
*callsign
, ax25_digi
*ax25_digi
, struct net_device
*dev
, unsigned int quality
)
375 struct nr_neigh
*nr_neigh
;
378 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
) {
379 if (ax25cmp(callsign
, &nr_neigh
->callsign
) == 0 && nr_neigh
->dev
== dev
) {
380 nr_neigh
->quality
= quality
;
381 nr_neigh
->locked
= 1;
386 if ((nr_neigh
= kmalloc(sizeof(*nr_neigh
), GFP_ATOMIC
)) == NULL
)
389 nr_neigh
->callsign
= *callsign
;
390 nr_neigh
->digipeat
= NULL
;
391 nr_neigh
->ax25
= NULL
;
393 nr_neigh
->quality
= quality
;
394 nr_neigh
->locked
= 1;
396 nr_neigh
->number
= nr_neigh_no
++;
397 nr_neigh
->failed
= 0;
399 if (ax25_digi
!= NULL
&& ax25_digi
->ndigi
> 0) {
400 if ((nr_neigh
->digipeat
= kmalloc(sizeof(*ax25_digi
), GFP_KERNEL
)) == NULL
) {
404 memcpy(nr_neigh
->digipeat
, ax25_digi
, sizeof(ax25_digi
));
410 nr_neigh
->next
= nr_neigh_list
;
411 nr_neigh_list
= nr_neigh
;
413 restore_flags(flags
);
419 * "Delete" a neighbour. The neighbour is only removed if the number
420 * of nodes that may use it is zero.
422 static int nr_del_neigh(ax25_address
*callsign
, struct net_device
*dev
, unsigned int quality
)
424 struct nr_neigh
*nr_neigh
;
426 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
)
427 if (ax25cmp(callsign
, &nr_neigh
->callsign
) == 0 && nr_neigh
->dev
== dev
)
430 if (nr_neigh
== NULL
) return -EINVAL
;
432 nr_neigh
->quality
= quality
;
433 nr_neigh
->locked
= 0;
435 if (nr_neigh
->count
== 0)
436 nr_remove_neigh(nr_neigh
);
442 * Decrement the obsolescence count by one. If a route is reduced to a
443 * count of zero, remove it. Also remove any unlocked neighbours with
444 * zero nodes routing via it.
446 static int nr_dec_obs(void)
448 struct nr_neigh
*nr_neigh
;
449 struct nr_node
*s
, *nr_node
;
452 nr_node
= nr_node_list
;
454 while (nr_node
!= NULL
) {
456 nr_node
= nr_node
->next
;
458 for (i
= 0; i
< s
->count
; i
++) {
459 switch (s
->routes
[i
].obs_count
) {
461 case 0: /* A locked entry */
464 case 1: /* From 1 -> 0 */
465 nr_neigh
= s
->routes
[i
].neighbour
;
469 if (nr_neigh
->count
== 0 && !nr_neigh
->locked
)
470 nr_remove_neigh(nr_neigh
);
476 s
->routes
[0] = s
->routes
[1];
478 s
->routes
[1] = s
->routes
[2];
485 s
->routes
[i
].obs_count
--;
499 * A device has been removed. Remove its routes and neighbours.
501 void nr_rt_device_down(struct net_device
*dev
)
503 struct nr_neigh
*s
, *nr_neigh
= nr_neigh_list
;
504 struct nr_node
*t
, *nr_node
;
507 while (nr_neigh
!= NULL
) {
509 nr_neigh
= nr_neigh
->next
;
512 nr_node
= nr_node_list
;
514 while (nr_node
!= NULL
) {
516 nr_node
= nr_node
->next
;
518 for (i
= 0; i
< t
->count
; i
++) {
519 if (t
->routes
[i
].neighbour
== s
) {
524 t
->routes
[0] = t
->routes
[1];
526 t
->routes
[1] = t
->routes
[2];
543 * Check that the device given is a valid AX.25 interface that is "up".
544 * Or a valid ethernet interface with an AX.25 callsign binding.
546 static struct net_device
*nr_ax25_dev_get(char *devname
)
548 struct net_device
*dev
;
550 if ((dev
= dev_get_by_name(devname
)) == NULL
)
553 if ((dev
->flags
& IFF_UP
) && dev
->type
== ARPHRD_AX25
)
561 * Find the first active NET/ROM device, usually "nr0".
563 struct net_device
*nr_dev_first(void)
565 struct net_device
*dev
, *first
= NULL
;
567 read_lock(&dev_base_lock
);
568 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
569 if ((dev
->flags
& IFF_UP
) && dev
->type
== ARPHRD_NETROM
)
570 if (first
== NULL
|| strncmp(dev
->name
, first
->name
, 3) < 0)
573 read_unlock(&dev_base_lock
);
579 * Find the NET/ROM device for the given callsign.
581 struct net_device
*nr_dev_get(ax25_address
*addr
)
583 struct net_device
*dev
;
585 read_lock(&dev_base_lock
);
586 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
587 if ((dev
->flags
& IFF_UP
) && dev
->type
== ARPHRD_NETROM
&& ax25cmp(addr
, (ax25_address
*)dev
->dev_addr
) == 0) {
593 read_unlock(&dev_base_lock
);
597 static ax25_digi
*nr_call_to_digi(int ndigis
, ax25_address
*digipeaters
)
599 static ax25_digi ax25_digi
;
605 for (i
= 0; i
< ndigis
; i
++) {
606 ax25_digi
.calls
[i
] = digipeaters
[i
];
607 ax25_digi
.repeated
[i
] = 0;
610 ax25_digi
.ndigi
= ndigis
;
611 ax25_digi
.lastrepeat
= -1;
617 * Handle the ioctls that control the routing functions.
619 int nr_rt_ioctl(unsigned int cmd
, void *arg
)
621 struct nr_route_struct nr_route
;
622 struct net_device
*dev
;
627 if (copy_from_user(&nr_route
, arg
, sizeof(struct nr_route_struct
)))
629 if ((dev
= nr_ax25_dev_get(nr_route
.device
)) == NULL
)
631 if (nr_route
.ndigis
< 0 || nr_route
.ndigis
> AX25_MAX_DIGIS
)
633 switch (nr_route
.type
) {
635 return nr_add_node(&nr_route
.callsign
,
638 nr_call_to_digi(nr_route
.ndigis
, nr_route
.digipeaters
),
639 dev
, nr_route
.quality
,
642 return nr_add_neigh(&nr_route
.callsign
,
643 nr_call_to_digi(nr_route
.ndigis
, nr_route
.digipeaters
),
644 dev
, nr_route
.quality
);
650 if (copy_from_user(&nr_route
, arg
, sizeof(struct nr_route_struct
)))
652 if ((dev
= nr_ax25_dev_get(nr_route
.device
)) == NULL
)
654 switch (nr_route
.type
) {
656 return nr_del_node(&nr_route
.callsign
,
657 &nr_route
.neighbour
, dev
);
659 return nr_del_neigh(&nr_route
.callsign
,
660 dev
, nr_route
.quality
);
676 * A level 2 link has timed out, therefore it appears to be a poor link,
677 * then don't use that neighbour until it is reset.
679 void nr_link_failed(ax25_cb
*ax25
, int reason
)
681 struct nr_neigh
*nr_neigh
;
682 struct nr_node
*nr_node
;
684 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
)
685 if (nr_neigh
->ax25
== ax25
)
688 if (nr_neigh
== NULL
) return;
690 nr_neigh
->ax25
= NULL
;
692 if (++nr_neigh
->failed
< sysctl_netrom_link_fails_count
) return;
694 for (nr_node
= nr_node_list
; nr_node
!= NULL
; nr_node
= nr_node
->next
)
695 if (nr_node
->which
< nr_node
->count
&& nr_node
->routes
[nr_node
->which
].neighbour
== nr_neigh
)
700 * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
701 * indicates an internally generated frame.
703 int nr_route_frame(struct sk_buff
*skb
, ax25_cb
*ax25
)
705 ax25_address
*nr_src
, *nr_dest
;
706 struct nr_neigh
*nr_neigh
;
707 struct nr_node
*nr_node
;
708 struct net_device
*dev
;
712 nr_src
= (ax25_address
*)(skb
->data
+ 0);
713 nr_dest
= (ax25_address
*)(skb
->data
+ 7);
716 nr_add_node(nr_src
, "", &ax25
->dest_addr
, ax25
->digipeat
,
717 ax25
->ax25_dev
->dev
, 0, sysctl_netrom_obsolescence_count_initialiser
);
719 if ((dev
= nr_dev_get(nr_dest
)) != NULL
) { /* Its for me */
720 if (ax25
== NULL
) /* Its from me */
721 return nr_loopback_queue(skb
);
723 return nr_rx_frame(skb
, dev
);
726 if (!sysctl_netrom_routing_control
&& ax25
!= NULL
)
729 /* Its Time-To-Live has expired */
730 if (--skb
->data
[14] == 0)
733 for (nr_node
= nr_node_list
; nr_node
!= NULL
; nr_node
= nr_node
->next
)
734 if (ax25cmp(nr_dest
, &nr_node
->callsign
) == 0)
737 if (nr_node
== NULL
|| nr_node
->which
>= nr_node
->count
)
740 nr_neigh
= nr_node
->routes
[nr_node
->which
].neighbour
;
742 if ((dev
= nr_dev_first()) == NULL
)
745 dptr
= skb_push(skb
, 1);
746 *dptr
= AX25_P_NETROM
;
748 nr_neigh
->ax25
= ax25_send_frame(skb
, 256, (ax25_address
*)dev
->dev_addr
, &nr_neigh
->callsign
, nr_neigh
->digipeat
, nr_neigh
->dev
);
750 return (nr_neigh
->ax25
!= NULL
);
753 int nr_nodes_get_info(char *buffer
, char **start
, off_t offset
, int length
)
755 struct nr_node
*nr_node
;
763 len
+= sprintf(buffer
, "callsign mnemonic w n qual obs neigh qual obs neigh qual obs neigh\n");
765 for (nr_node
= nr_node_list
; nr_node
!= NULL
; nr_node
= nr_node
->next
) {
766 len
+= sprintf(buffer
+ len
, "%-9s %-7s %d %d",
767 ax2asc(&nr_node
->callsign
),
768 (nr_node
->mnemonic
[0] == '\0') ? "*" : nr_node
->mnemonic
,
772 for (i
= 0; i
< nr_node
->count
; i
++) {
773 len
+= sprintf(buffer
+ len
, " %3d %d %05d",
774 nr_node
->routes
[i
].quality
,
775 nr_node
->routes
[i
].obs_count
,
776 nr_node
->routes
[i
].neighbour
->number
);
779 len
+= sprintf(buffer
+ len
, "\n");
788 if (pos
> offset
+ length
)
794 *start
= buffer
+ (offset
- begin
);
795 len
-= (offset
- begin
);
797 if (len
> length
) len
= length
;
802 int nr_neigh_get_info(char *buffer
, char **start
, off_t offset
, int length
)
804 struct nr_neigh
*nr_neigh
;
812 len
+= sprintf(buffer
, "addr callsign dev qual lock count failed digipeaters\n");
814 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
) {
815 len
+= sprintf(buffer
+ len
, "%05d %-9s %-4s %3d %d %3d %3d",
817 ax2asc(&nr_neigh
->callsign
),
818 nr_neigh
->dev
? nr_neigh
->dev
->name
: "???",
824 if (nr_neigh
->digipeat
!= NULL
) {
825 for (i
= 0; i
< nr_neigh
->digipeat
->ndigi
; i
++)
826 len
+= sprintf(buffer
+ len
, " %s", ax2asc(&nr_neigh
->digipeat
->calls
[i
]));
829 len
+= sprintf(buffer
+ len
, "\n");
838 if (pos
> offset
+ length
)
844 *start
= buffer
+ (offset
- begin
);
845 len
-= (offset
- begin
);
847 if (len
> length
) len
= length
;
853 * Free all memory associated with the nodes and routes lists.
855 void __exit
nr_rt_free(void)
857 struct nr_neigh
*s
, *nr_neigh
= nr_neigh_list
;
858 struct nr_node
*t
, *nr_node
= nr_node_list
;
860 while (nr_node
!= NULL
) {
862 nr_node
= nr_node
->next
;
867 while (nr_neigh
!= NULL
) {
869 nr_neigh
= nr_neigh
->next
;