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/config.h>
25 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
26 #include <linux/errno.h>
27 #include <linux/types.h>
28 #include <linux/socket.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/timer.h>
33 #include <linux/string.h>
34 #include <linux/sockios.h>
35 #include <linux/net.h>
37 #include <linux/inet.h>
38 #include <linux/netdevice.h>
40 #include <linux/if_arp.h>
41 #include <linux/skbuff.h>
43 #include <asm/uaccess.h>
44 #include <asm/system.h>
45 #include <linux/fcntl.h>
46 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
48 #include <linux/interrupt.h>
49 #include <linux/notifier.h>
50 #include <linux/netfilter.h>
51 #include <net/netrom.h>
53 static unsigned int nr_neigh_no
= 1;
55 static struct nr_node
*nr_node_list
= NULL
;
56 static struct nr_neigh
*nr_neigh_list
= NULL
;
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
;
73 if (nr_dev_get(nr
) != NULL
) /* Can't add routes to ourself */
76 for (nr_node
= nr_node_list
; nr_node
!= NULL
; nr_node
= nr_node
->next
)
77 if (ax25cmp(nr
, &nr_node
->callsign
) == 0)
80 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
)
81 if (ax25cmp(ax25
, &nr_neigh
->callsign
) == 0 && nr_neigh
->dev
== dev
)
85 * The L2 link to a neighbour has failed in the past
86 * and now a frame comes from this neighbour. We assume
87 * it was a temporary trouble with the link and reset the
88 * routes now (and not wait for a node broadcast).
90 if (nr_neigh
!= NULL
&& nr_neigh
->failed
!= 0 && quality
== 0) {
93 for (node
= nr_node_list
; node
!= NULL
; node
= node
->next
)
94 for (i
= 0; i
< node
->count
; i
++)
95 if (node
->routes
[i
].neighbour
== nr_neigh
)
100 if (nr_neigh
!= NULL
)
101 nr_neigh
->failed
= 0;
103 if (quality
== 0 && nr_neigh
!= NULL
&& nr_node
!= NULL
)
106 if (nr_neigh
== NULL
) {
107 if ((nr_neigh
= kmalloc(sizeof(*nr_neigh
), GFP_ATOMIC
)) == NULL
)
110 nr_neigh
->callsign
= *ax25
;
111 nr_neigh
->digipeat
= NULL
;
112 nr_neigh
->ax25
= NULL
;
114 nr_neigh
->quality
= sysctl_netrom_default_path_quality
;
115 nr_neigh
->locked
= 0;
117 nr_neigh
->number
= nr_neigh_no
++;
118 nr_neigh
->failed
= 0;
120 if (ax25_digi
!= NULL
&& ax25_digi
->ndigi
> 0) {
121 if ((nr_neigh
->digipeat
= kmalloc(sizeof(*ax25_digi
), GFP_KERNEL
)) == NULL
) {
125 memcpy(nr_neigh
->digipeat
, ax25_digi
, sizeof(ax25_digi
));
131 nr_neigh
->next
= nr_neigh_list
;
132 nr_neigh_list
= nr_neigh
;
134 restore_flags(flags
);
137 if (quality
!= 0 && ax25cmp(nr
, ax25
) == 0 && !nr_neigh
->locked
)
138 nr_neigh
->quality
= quality
;
140 if (nr_node
== NULL
) {
141 if ((nr_node
= kmalloc(sizeof(*nr_node
), GFP_ATOMIC
)) == NULL
)
144 nr_node
->callsign
= *nr
;
145 strcpy(nr_node
->mnemonic
, mnemonic
);
150 nr_node
->routes
[0].quality
= quality
;
151 nr_node
->routes
[0].obs_count
= obs_count
;
152 nr_node
->routes
[0].neighbour
= nr_neigh
;
157 nr_node
->next
= nr_node_list
;
158 nr_node_list
= nr_node
;
160 restore_flags(flags
);
168 strcpy(nr_node
->mnemonic
, mnemonic
);
170 for (found
= 0, i
= 0; i
< nr_node
->count
; i
++) {
171 if (nr_node
->routes
[i
].neighbour
== nr_neigh
) {
172 nr_node
->routes
[i
].quality
= quality
;
173 nr_node
->routes
[i
].obs_count
= obs_count
;
180 /* We have space at the bottom, slot it in */
181 if (nr_node
->count
< 3) {
182 nr_node
->routes
[2] = nr_node
->routes
[1];
183 nr_node
->routes
[1] = nr_node
->routes
[0];
185 nr_node
->routes
[0].quality
= quality
;
186 nr_node
->routes
[0].obs_count
= obs_count
;
187 nr_node
->routes
[0].neighbour
= nr_neigh
;
193 /* It must be better than the worst */
194 if (quality
> nr_node
->routes
[2].quality
) {
195 nr_node
->routes
[2].neighbour
->count
--;
197 if (nr_node
->routes
[2].neighbour
->count
== 0 && !nr_node
->routes
[2].neighbour
->locked
)
198 nr_remove_neigh(nr_node
->routes
[2].neighbour
);
200 nr_node
->routes
[2].quality
= quality
;
201 nr_node
->routes
[2].obs_count
= obs_count
;
202 nr_node
->routes
[2].neighbour
= nr_neigh
;
209 /* Now re-sort the routes in quality order */
210 switch (nr_node
->count
) {
212 if (nr_node
->routes
[1].quality
> nr_node
->routes
[0].quality
) {
213 switch (nr_node
->which
) {
214 case 0: nr_node
->which
= 1; break;
215 case 1: nr_node
->which
= 0; break;
218 nr_route
= nr_node
->routes
[0];
219 nr_node
->routes
[0] = nr_node
->routes
[1];
220 nr_node
->routes
[1] = nr_route
;
222 if (nr_node
->routes
[2].quality
> nr_node
->routes
[1].quality
) {
223 switch (nr_node
->which
) {
224 case 1: nr_node
->which
= 2; break;
225 case 2: nr_node
->which
= 1; break;
228 nr_route
= nr_node
->routes
[1];
229 nr_node
->routes
[1] = nr_node
->routes
[2];
230 nr_node
->routes
[2] = nr_route
;
233 if (nr_node
->routes
[1].quality
> nr_node
->routes
[0].quality
) {
234 switch (nr_node
->which
) {
235 case 0: nr_node
->which
= 1; break;
236 case 1: nr_node
->which
= 0; break;
239 nr_route
= nr_node
->routes
[0];
240 nr_node
->routes
[0] = nr_node
->routes
[1];
241 nr_node
->routes
[1] = nr_route
;
247 for (i
= 0; i
< nr_node
->count
; i
++) {
248 if (nr_node
->routes
[i
].neighbour
== nr_neigh
) {
249 if (i
< nr_node
->which
)
258 static void nr_remove_node(struct nr_node
*nr_node
)
266 if ((s
= nr_node_list
) == nr_node
) {
267 nr_node_list
= nr_node
->next
;
268 restore_flags(flags
);
273 while (s
!= NULL
&& s
->next
!= NULL
) {
274 if (s
->next
== nr_node
) {
275 s
->next
= nr_node
->next
;
276 restore_flags(flags
);
284 restore_flags(flags
);
287 static void nr_remove_neigh(struct nr_neigh
*nr_neigh
)
295 if ((s
= nr_neigh_list
) == nr_neigh
) {
296 nr_neigh_list
= nr_neigh
->next
;
297 restore_flags(flags
);
298 if (nr_neigh
->digipeat
!= NULL
)
299 kfree(nr_neigh
->digipeat
);
304 while (s
!= NULL
&& s
->next
!= NULL
) {
305 if (s
->next
== nr_neigh
) {
306 s
->next
= nr_neigh
->next
;
307 restore_flags(flags
);
308 if (nr_neigh
->digipeat
!= NULL
)
309 kfree(nr_neigh
->digipeat
);
317 restore_flags(flags
);
321 * "Delete" a node. Strictly speaking remove a route to a node. The node
322 * is only deleted if no routes are left to it.
324 static int nr_del_node(ax25_address
*callsign
, ax25_address
*neighbour
, struct net_device
*dev
)
326 struct nr_node
*nr_node
;
327 struct nr_neigh
*nr_neigh
;
330 for (nr_node
= nr_node_list
; nr_node
!= NULL
; nr_node
= nr_node
->next
)
331 if (ax25cmp(callsign
, &nr_node
->callsign
) == 0)
334 if (nr_node
== NULL
) return -EINVAL
;
336 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
)
337 if (ax25cmp(neighbour
, &nr_neigh
->callsign
) == 0 && nr_neigh
->dev
== dev
)
340 if (nr_neigh
== NULL
) return -EINVAL
;
342 for (i
= 0; i
< nr_node
->count
; i
++) {
343 if (nr_node
->routes
[i
].neighbour
== nr_neigh
) {
346 if (nr_neigh
->count
== 0 && !nr_neigh
->locked
)
347 nr_remove_neigh(nr_neigh
);
351 if (nr_node
->count
== 0) {
352 nr_remove_node(nr_node
);
356 nr_node
->routes
[0] = nr_node
->routes
[1];
358 nr_node
->routes
[1] = nr_node
->routes
[2];
372 * Lock a neighbour with a quality.
374 static int nr_add_neigh(ax25_address
*callsign
, ax25_digi
*ax25_digi
, struct net_device
*dev
, unsigned int quality
)
376 struct nr_neigh
*nr_neigh
;
379 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
) {
380 if (ax25cmp(callsign
, &nr_neigh
->callsign
) == 0 && nr_neigh
->dev
== dev
) {
381 nr_neigh
->quality
= quality
;
382 nr_neigh
->locked
= 1;
387 if ((nr_neigh
= kmalloc(sizeof(*nr_neigh
), GFP_ATOMIC
)) == NULL
)
390 nr_neigh
->callsign
= *callsign
;
391 nr_neigh
->digipeat
= NULL
;
392 nr_neigh
->ax25
= NULL
;
394 nr_neigh
->quality
= quality
;
395 nr_neigh
->locked
= 1;
397 nr_neigh
->number
= nr_neigh_no
++;
398 nr_neigh
->failed
= 0;
400 if (ax25_digi
!= NULL
&& ax25_digi
->ndigi
> 0) {
401 if ((nr_neigh
->digipeat
= kmalloc(sizeof(*ax25_digi
), GFP_KERNEL
)) == NULL
) {
405 memcpy(nr_neigh
->digipeat
, ax25_digi
, sizeof(ax25_digi
));
411 nr_neigh
->next
= nr_neigh_list
;
412 nr_neigh_list
= nr_neigh
;
414 restore_flags(flags
);
420 * "Delete" a neighbour. The neighbour is only removed if the number
421 * of nodes that may use it is zero.
423 static int nr_del_neigh(ax25_address
*callsign
, struct net_device
*dev
, unsigned int quality
)
425 struct nr_neigh
*nr_neigh
;
427 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
)
428 if (ax25cmp(callsign
, &nr_neigh
->callsign
) == 0 && nr_neigh
->dev
== dev
)
431 if (nr_neigh
== NULL
) return -EINVAL
;
433 nr_neigh
->quality
= quality
;
434 nr_neigh
->locked
= 0;
436 if (nr_neigh
->count
== 0)
437 nr_remove_neigh(nr_neigh
);
443 * Decrement the obsolescence count by one. If a route is reduced to a
444 * count of zero, remove it. Also remove any unlocked neighbours with
445 * zero nodes routing via it.
447 static int nr_dec_obs(void)
449 struct nr_neigh
*nr_neigh
;
450 struct nr_node
*s
, *nr_node
;
453 nr_node
= nr_node_list
;
455 while (nr_node
!= NULL
) {
457 nr_node
= nr_node
->next
;
459 for (i
= 0; i
< s
->count
; i
++) {
460 switch (s
->routes
[i
].obs_count
) {
462 case 0: /* A locked entry */
465 case 1: /* From 1 -> 0 */
466 nr_neigh
= s
->routes
[i
].neighbour
;
470 if (nr_neigh
->count
== 0 && !nr_neigh
->locked
)
471 nr_remove_neigh(nr_neigh
);
477 s
->routes
[0] = s
->routes
[1];
479 s
->routes
[1] = s
->routes
[2];
486 s
->routes
[i
].obs_count
--;
500 * A device has been removed. Remove its routes and neighbours.
502 void nr_rt_device_down(struct net_device
*dev
)
504 struct nr_neigh
*s
, *nr_neigh
= nr_neigh_list
;
505 struct nr_node
*t
, *nr_node
;
508 while (nr_neigh
!= NULL
) {
510 nr_neigh
= nr_neigh
->next
;
513 nr_node
= nr_node_list
;
515 while (nr_node
!= NULL
) {
517 nr_node
= nr_node
->next
;
519 for (i
= 0; i
< t
->count
; i
++) {
520 if (t
->routes
[i
].neighbour
== s
) {
525 t
->routes
[0] = t
->routes
[1];
527 t
->routes
[1] = t
->routes
[2];
544 * Check that the device given is a valid AX.25 interface that is "up".
545 * Or a valid ethernet interface with an AX.25 callsign binding.
547 static struct net_device
*nr_ax25_dev_get(char *devname
)
549 struct net_device
*dev
;
551 if ((dev
= dev_get_by_name(devname
)) == NULL
)
554 if ((dev
->flags
& IFF_UP
) && dev
->type
== ARPHRD_AX25
)
562 * Find the first active NET/ROM device, usually "nr0".
564 struct net_device
*nr_dev_first(void)
566 struct net_device
*dev
, *first
= NULL
;
568 read_lock(&dev_base_lock
);
569 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
570 if ((dev
->flags
& IFF_UP
) && dev
->type
== ARPHRD_NETROM
)
571 if (first
== NULL
|| strncmp(dev
->name
, first
->name
, 3) < 0)
574 read_unlock(&dev_base_lock
);
580 * Find the NET/ROM device for the given callsign.
582 struct net_device
*nr_dev_get(ax25_address
*addr
)
584 struct net_device
*dev
;
586 read_lock(&dev_base_lock
);
587 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
588 if ((dev
->flags
& IFF_UP
) && dev
->type
== ARPHRD_NETROM
&& ax25cmp(addr
, (ax25_address
*)dev
->dev_addr
) == 0) {
594 read_unlock(&dev_base_lock
);
598 static ax25_digi
*nr_call_to_digi(int ndigis
, ax25_address
*digipeaters
)
600 static ax25_digi ax25_digi
;
606 for (i
= 0; i
< ndigis
; i
++) {
607 ax25_digi
.calls
[i
] = digipeaters
[i
];
608 ax25_digi
.repeated
[i
] = 0;
611 ax25_digi
.ndigi
= ndigis
;
612 ax25_digi
.lastrepeat
= -1;
618 * Handle the ioctls that control the routing functions.
620 int nr_rt_ioctl(unsigned int cmd
, void *arg
)
622 struct nr_route_struct nr_route
;
623 struct net_device
*dev
;
628 if (copy_from_user(&nr_route
, arg
, sizeof(struct nr_route_struct
)))
630 if ((dev
= nr_ax25_dev_get(nr_route
.device
)) == NULL
)
632 if (nr_route
.ndigis
< 0 || nr_route
.ndigis
> AX25_MAX_DIGIS
)
634 switch (nr_route
.type
) {
636 return nr_add_node(&nr_route
.callsign
,
639 nr_call_to_digi(nr_route
.ndigis
, nr_route
.digipeaters
),
640 dev
, nr_route
.quality
,
643 return nr_add_neigh(&nr_route
.callsign
,
644 nr_call_to_digi(nr_route
.ndigis
, nr_route
.digipeaters
),
645 dev
, nr_route
.quality
);
651 if (copy_from_user(&nr_route
, arg
, sizeof(struct nr_route_struct
)))
653 if ((dev
= nr_ax25_dev_get(nr_route
.device
)) == NULL
)
655 switch (nr_route
.type
) {
657 return nr_del_node(&nr_route
.callsign
,
658 &nr_route
.neighbour
, dev
);
660 return nr_del_neigh(&nr_route
.callsign
,
661 dev
, nr_route
.quality
);
677 * A level 2 link has timed out, therefore it appears to be a poor link,
678 * then don't use that neighbour until it is reset.
680 void nr_link_failed(ax25_cb
*ax25
, int reason
)
682 struct nr_neigh
*nr_neigh
;
683 struct nr_node
*nr_node
;
685 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
)
686 if (nr_neigh
->ax25
== ax25
)
689 if (nr_neigh
== NULL
) return;
691 nr_neigh
->ax25
= NULL
;
693 if (++nr_neigh
->failed
< sysctl_netrom_link_fails_count
) return;
695 for (nr_node
= nr_node_list
; nr_node
!= NULL
; nr_node
= nr_node
->next
)
696 if (nr_node
->which
< nr_node
->count
&& nr_node
->routes
[nr_node
->which
].neighbour
== nr_neigh
)
701 * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
702 * indicates an internally generated frame.
704 int nr_route_frame(struct sk_buff
*skb
, ax25_cb
*ax25
)
706 ax25_address
*nr_src
, *nr_dest
;
707 struct nr_neigh
*nr_neigh
;
708 struct nr_node
*nr_node
;
709 struct net_device
*dev
;
713 nr_src
= (ax25_address
*)(skb
->data
+ 0);
714 nr_dest
= (ax25_address
*)(skb
->data
+ 7);
717 nr_add_node(nr_src
, "", &ax25
->dest_addr
, ax25
->digipeat
,
718 ax25
->ax25_dev
->dev
, 0, sysctl_netrom_obsolescence_count_initialiser
);
720 if ((dev
= nr_dev_get(nr_dest
)) != NULL
) { /* Its for me */
721 if (ax25
== NULL
) /* Its from me */
722 return nr_loopback_queue(skb
);
724 return nr_rx_frame(skb
, dev
);
727 if (!sysctl_netrom_routing_control
&& ax25
!= NULL
)
730 /* Its Time-To-Live has expired */
731 if (--skb
->data
[14] == 0)
734 for (nr_node
= nr_node_list
; nr_node
!= NULL
; nr_node
= nr_node
->next
)
735 if (ax25cmp(nr_dest
, &nr_node
->callsign
) == 0)
738 if (nr_node
== NULL
|| nr_node
->which
>= nr_node
->count
)
741 nr_neigh
= nr_node
->routes
[nr_node
->which
].neighbour
;
743 if ((dev
= nr_dev_first()) == NULL
)
746 dptr
= skb_push(skb
, 1);
747 *dptr
= AX25_P_NETROM
;
749 nr_neigh
->ax25
= ax25_send_frame(skb
, 256, (ax25_address
*)dev
->dev_addr
, &nr_neigh
->callsign
, nr_neigh
->digipeat
, nr_neigh
->dev
);
751 return (nr_neigh
->ax25
!= NULL
);
754 int nr_nodes_get_info(char *buffer
, char **start
, off_t offset
, int length
)
756 struct nr_node
*nr_node
;
764 len
+= sprintf(buffer
, "callsign mnemonic w n qual obs neigh qual obs neigh qual obs neigh\n");
766 for (nr_node
= nr_node_list
; nr_node
!= NULL
; nr_node
= nr_node
->next
) {
767 len
+= sprintf(buffer
+ len
, "%-9s %-7s %d %d",
768 ax2asc(&nr_node
->callsign
),
769 (nr_node
->mnemonic
[0] == '\0') ? "*" : nr_node
->mnemonic
,
773 for (i
= 0; i
< nr_node
->count
; i
++) {
774 len
+= sprintf(buffer
+ len
, " %3d %d %05d",
775 nr_node
->routes
[i
].quality
,
776 nr_node
->routes
[i
].obs_count
,
777 nr_node
->routes
[i
].neighbour
->number
);
780 len
+= sprintf(buffer
+ len
, "\n");
789 if (pos
> offset
+ length
)
795 *start
= buffer
+ (offset
- begin
);
796 len
-= (offset
- begin
);
798 if (len
> length
) len
= length
;
803 int nr_neigh_get_info(char *buffer
, char **start
, off_t offset
, int length
)
805 struct nr_neigh
*nr_neigh
;
813 len
+= sprintf(buffer
, "addr callsign dev qual lock count failed digipeaters\n");
815 for (nr_neigh
= nr_neigh_list
; nr_neigh
!= NULL
; nr_neigh
= nr_neigh
->next
) {
816 len
+= sprintf(buffer
+ len
, "%05d %-9s %-4s %3d %d %3d %3d",
818 ax2asc(&nr_neigh
->callsign
),
819 nr_neigh
->dev
? nr_neigh
->dev
->name
: "???",
825 if (nr_neigh
->digipeat
!= NULL
) {
826 for (i
= 0; i
< nr_neigh
->digipeat
->ndigi
; i
++)
827 len
+= sprintf(buffer
+ len
, " %s", ax2asc(&nr_neigh
->digipeat
->calls
[i
]));
830 len
+= sprintf(buffer
+ len
, "\n");
839 if (pos
> offset
+ length
)
845 *start
= buffer
+ (offset
- begin
);
846 len
-= (offset
- begin
);
848 if (len
> length
) len
= length
;
856 * Free all memory associated with the nodes and routes lists.
858 void nr_rt_free(void)
860 struct nr_neigh
*s
, *nr_neigh
= nr_neigh_list
;
861 struct nr_node
*t
, *nr_node
= nr_node_list
;
863 while (nr_node
!= NULL
) {
865 nr_node
= nr_node
->next
;
870 while (nr_neigh
!= NULL
) {
872 nr_neigh
= nr_neigh
->next
;