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.
12 * Other kernels modules in this kit are generally BSD derived. See the copyright headers.
16 * AX.25 020 Jonathan(G4KLX) First go.
17 * AX.25 022 Jonathan(G4KLX) Added the actual meat to this - we now have a nice heard list.
18 * AX.25 025 Alan(GW4PTS) First cut at autobinding by route scan.
19 * AX.25 028b Jonathan(G4KLX) Extracted AX25 control block from the
20 * sock structure. Device removal now
21 * removes the heard structure.
22 * AX.25 029 Steven(GW7RRM) Added /proc information for uid/callsign mapping.
23 * Jonathan(G4KLX) Handling of IP mode in the routing list and /proc entry.
24 * AX.25 030 Jonathan(G4KLX) Added digi-peaters to routing table, and
25 * ioctls to manipulate them. Added port
27 * AX.25 031 Jonathan(G4KLX) Added concept of default route.
28 * Joerg(DL1BKE) ax25_rt_build_path() find digipeater list and device by
29 * destination call. Needed for IP routing via digipeater
30 * Jonathan(G4KLX) Added routing for IP datagram packets.
31 * Joerg(DL1BKE) Changed routing for IP datagram and VC to use a default
32 * route if available. Does not overwrite default routes
33 * on route-table overflow anymore.
34 * Joerg(DL1BKE) Fixed AX.25 routing of IP datagram and VC, new ioctl()
35 * "SIOCAX25OPTRT" to set IP mode and a 'permanent' flag
37 * AX.25 033 Jonathan(G4KLX) Remove auto-router.
38 * Joerg(DL1BKE) Moved BPQ Ethernet driver to separate device.
39 * AX.25 035 Frederic(F1OAT) Support for pseudo-digipeating.
40 * Jonathan(G4KLX) Support for packet forwarding.
43 #include <linux/config.h>
44 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
45 #include <linux/errno.h>
46 #include <linux/types.h>
47 #include <linux/socket.h>
49 #include <linux/kernel.h>
50 #include <linux/sched.h>
51 #include <linux/timer.h>
52 #include <linux/string.h>
53 #include <linux/sockios.h>
54 #include <linux/net.h>
56 #include <linux/inet.h>
57 #include <linux/netdevice.h>
58 #include <linux/if_arp.h>
59 #include <linux/skbuff.h>
61 #include <asm/uaccess.h>
62 #include <asm/system.h>
63 #include <linux/fcntl.h>
65 #include <linux/interrupt.h>
67 static ax25_route
*ax25_route_list
= NULL
;
69 static ax25_route
*ax25_find_route(ax25_address
*, struct net_device
*);
72 * small macro to drop non-digipeated digipeaters and reverse path
74 static inline void ax25_route_invert(ax25_digi
*in
, ax25_digi
*out
)
78 for (k
= 0; k
< in
->ndigi
; k
++)
84 ax25_digi_invert(in
, out
);
87 void ax25_rt_device_down(struct net_device
*dev
)
89 ax25_route
*s
, *t
, *ax25_rt
= ax25_route_list
;
91 while (ax25_rt
!= NULL
) {
93 ax25_rt
= ax25_rt
->next
;
96 if (ax25_route_list
== s
) {
97 ax25_route_list
= s
->next
;
98 if (s
->digipeat
!= NULL
)
102 for (t
= ax25_route_list
; t
!= NULL
; t
= t
->next
) {
105 if (s
->digipeat
!= NULL
)
116 int ax25_rt_ioctl(unsigned int cmd
, void *arg
)
119 ax25_route
*s
, *t
, *ax25_rt
;
120 struct ax25_routes_struct route
;
121 struct ax25_route_opt_struct rt_option
;
127 if (copy_from_user(&route
, arg
, sizeof(route
)))
129 if ((ax25_dev
= ax25_addr_ax25dev(&route
.port_addr
)) == NULL
)
131 if (route
.digi_count
> AX25_MAX_DIGIS
)
133 for (ax25_rt
= ax25_route_list
; ax25_rt
!= NULL
; ax25_rt
= ax25_rt
->next
) {
134 if (ax25cmp(&ax25_rt
->callsign
, &route
.dest_addr
) == 0 && ax25_rt
->dev
== ax25_dev
->dev
) {
135 if (ax25_rt
->digipeat
!= NULL
) {
136 kfree(ax25_rt
->digipeat
);
137 ax25_rt
->digipeat
= NULL
;
139 if (route
.digi_count
!= 0) {
140 if ((ax25_rt
->digipeat
= kmalloc(sizeof(ax25_digi
), GFP_ATOMIC
)) == NULL
)
142 ax25_rt
->digipeat
->lastrepeat
= -1;
143 ax25_rt
->digipeat
->ndigi
= route
.digi_count
;
144 for (i
= 0; i
< route
.digi_count
; i
++) {
145 ax25_rt
->digipeat
->repeated
[i
] = 0;
146 ax25_rt
->digipeat
->calls
[i
] = route
.digi_addr
[i
];
152 if ((ax25_rt
= kmalloc(sizeof(ax25_route
), GFP_ATOMIC
)) == NULL
)
154 ax25_rt
->callsign
= route
.dest_addr
;
155 ax25_rt
->dev
= ax25_dev
->dev
;
156 ax25_rt
->digipeat
= NULL
;
157 ax25_rt
->ip_mode
= ' ';
158 if (route
.digi_count
!= 0) {
159 if ((ax25_rt
->digipeat
= kmalloc(sizeof(ax25_digi
), GFP_ATOMIC
)) == NULL
) {
163 ax25_rt
->digipeat
->lastrepeat
= -1;
164 ax25_rt
->digipeat
->ndigi
= route
.digi_count
;
165 for (i
= 0; i
< route
.digi_count
; i
++) {
166 ax25_rt
->digipeat
->repeated
[i
] = 0;
167 ax25_rt
->digipeat
->calls
[i
] = route
.digi_addr
[i
];
170 save_flags(flags
); cli();
171 ax25_rt
->next
= ax25_route_list
;
172 ax25_route_list
= ax25_rt
;
173 restore_flags(flags
);
177 if (copy_from_user(&route
, arg
, sizeof(route
)))
179 if ((ax25_dev
= ax25_addr_ax25dev(&route
.port_addr
)) == NULL
)
181 ax25_rt
= ax25_route_list
;
182 while (ax25_rt
!= NULL
) {
184 ax25_rt
= ax25_rt
->next
;
185 if (s
->dev
== ax25_dev
->dev
&& ax25cmp(&route
.dest_addr
, &s
->callsign
) == 0) {
186 if (ax25_route_list
== s
) {
187 ax25_route_list
= s
->next
;
188 if (s
->digipeat
!= NULL
)
192 for (t
= ax25_route_list
; t
!= NULL
; t
= t
->next
) {
195 if (s
->digipeat
!= NULL
)
207 if (copy_from_user(&rt_option
, arg
, sizeof(rt_option
)))
209 if ((ax25_dev
= ax25_addr_ax25dev(&rt_option
.port_addr
)) == NULL
)
211 for (ax25_rt
= ax25_route_list
; ax25_rt
!= NULL
; ax25_rt
= ax25_rt
->next
) {
212 if (ax25_rt
->dev
== ax25_dev
->dev
&& ax25cmp(&rt_option
.dest_addr
, &ax25_rt
->callsign
) == 0) {
213 switch (rt_option
.cmd
) {
214 case AX25_SET_RT_IPMODE
:
215 switch (rt_option
.arg
) {
219 ax25_rt
->ip_mode
= rt_option
.arg
;
239 int ax25_rt_get_info(char *buffer
, char **start
, off_t offset
, int length
, int dummy
)
250 len
+= sprintf(buffer
, "callsign dev mode digipeaters\n");
252 for (ax25_rt
= ax25_route_list
; ax25_rt
!= NULL
; ax25_rt
= ax25_rt
->next
) {
253 if (ax25cmp(&ax25_rt
->callsign
, &null_ax25_address
) == 0)
254 callsign
= "default";
256 callsign
= ax2asc(&ax25_rt
->callsign
);
257 len
+= sprintf(buffer
+ len
, "%-9s %-4s",
259 ax25_rt
->dev
? ax25_rt
->dev
->name
: "???");
261 switch (ax25_rt
->ip_mode
) {
263 len
+= sprintf(buffer
+ len
, " vc");
266 len
+= sprintf(buffer
+ len
, " dg");
269 len
+= sprintf(buffer
+ len
, " *");
273 if (ax25_rt
->digipeat
!= NULL
)
274 for (i
= 0; i
< ax25_rt
->digipeat
->ndigi
; i
++)
275 len
+= sprintf(buffer
+ len
, " %s", ax2asc(&ax25_rt
->digipeat
->calls
[i
]));
277 len
+= sprintf(buffer
+ len
, "\n");
286 if (pos
> offset
+ length
)
292 *start
= buffer
+ (offset
- begin
);
293 len
-= (offset
- begin
);
295 if (len
> length
) len
= length
;
303 static ax25_route
*ax25_find_route(ax25_address
*addr
, struct net_device
*dev
)
305 ax25_route
*ax25_spe_rt
= NULL
;
306 ax25_route
*ax25_def_rt
= NULL
;
310 * Bind to the physical interface we heard them on, or the default
311 * route if none is found;
313 for (ax25_rt
= ax25_route_list
; ax25_rt
!= NULL
; ax25_rt
= ax25_rt
->next
) {
315 if (ax25cmp(&ax25_rt
->callsign
, addr
) == 0 && ax25_rt
->dev
!= NULL
)
316 ax25_spe_rt
= ax25_rt
;
317 if (ax25cmp(&ax25_rt
->callsign
, &null_ax25_address
) == 0 && ax25_rt
->dev
!= NULL
)
318 ax25_def_rt
= ax25_rt
;
320 if (ax25cmp(&ax25_rt
->callsign
, addr
) == 0 && ax25_rt
->dev
== dev
)
321 ax25_spe_rt
= ax25_rt
;
322 if (ax25cmp(&ax25_rt
->callsign
, &null_ax25_address
) == 0 && ax25_rt
->dev
== dev
)
323 ax25_def_rt
= ax25_rt
;
327 if (ax25_spe_rt
!= NULL
)
334 * Adjust path: If you specify a default route and want to connect
335 * a target on the digipeater path but w/o having a special route
336 * set before, the path has to be truncated from your target on.
338 static inline void ax25_adjust_path(ax25_address
*addr
, ax25_digi
*digipeat
)
342 for (k
= 0; k
< digipeat
->ndigi
; k
++) {
343 if (ax25cmp(addr
, &digipeat
->calls
[k
]) == 0)
352 * Find which interface to use.
354 int ax25_rt_autobind(ax25_cb
*ax25
, ax25_address
*addr
)
359 if ((ax25_rt
= ax25_find_route(addr
, NULL
)) == NULL
)
360 return -EHOSTUNREACH
;
362 if ((ax25
->ax25_dev
= ax25_dev_ax25dev(ax25_rt
->dev
)) == NULL
)
363 return -EHOSTUNREACH
;
365 if ((call
= ax25_findbyuid(current
->euid
)) == NULL
) {
366 if (ax25_uid_policy
&& !suser())
368 call
= (ax25_address
*)ax25
->ax25_dev
->dev
->dev_addr
;
371 ax25
->source_addr
= *call
;
373 if (ax25_rt
->digipeat
!= NULL
) {
374 if ((ax25
->digipeat
= kmalloc(sizeof(ax25_digi
), GFP_ATOMIC
)) == NULL
)
376 *ax25
->digipeat
= *ax25_rt
->digipeat
;
377 ax25_adjust_path(addr
, ax25
->digipeat
);
380 if (ax25
->sk
!= NULL
)
381 ax25
->sk
->zapped
= 0;
387 * dl1bke 960117: build digipeater path
388 * dl1bke 960301: use the default route if it exists
390 ax25_route
*ax25_rt_find_route(ax25_address
*addr
, struct net_device
*dev
)
392 static ax25_route route
;
395 if ((ax25_rt
= ax25_find_route(addr
, dev
)) == NULL
) {
397 route
.callsign
= *addr
;
399 route
.digipeat
= NULL
;
407 struct sk_buff
*ax25_rt_build_path(struct sk_buff
*skb
, ax25_address
*src
, ax25_address
*dest
, ax25_digi
*digi
)
409 struct sk_buff
*skbn
;
413 len
= digi
->ndigi
* AX25_ADDR_LEN
;
415 if (skb_headroom(skb
) < len
) {
416 if ((skbn
= skb_realloc_headroom(skb
, len
)) == NULL
) {
417 printk(KERN_CRIT
"AX.25: ax25_dg_build_path - out of memory\n");
422 skb_set_owner_w(skbn
, skb
->sk
);
429 bp
= skb_push(skb
, len
);
431 ax25_addr_build(bp
, src
, dest
, digi
, AX25_COMMAND
, AX25_MODULUS
);
439 * Free all memory associated with routing structures.
441 void ax25_rt_free(void)
443 ax25_route
*s
, *ax25_rt
= ax25_route_list
;
445 while (ax25_rt
!= NULL
) {
447 ax25_rt
= ax25_rt
->next
;
449 if (s
->digipeat
!= NULL
)