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) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
8 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
9 * Copyright (C) Steven Whitehouse GW7RRM (stevew@acm.org)
10 * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
11 * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de)
12 * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr)
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/socket.h>
17 #include <linux/timer.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/sockios.h>
23 #include <linux/net.h>
25 #include <linux/inet.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_arp.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include <linux/fcntl.h>
35 #include <linux/interrupt.h>
36 #include <linux/init.h>
37 #include <linux/seq_file.h>
39 static ax25_route *ax25_route_list;
40 static rwlock_t ax25_route_lock = RW_LOCK_UNLOCKED;
42 static ax25_route *ax25_get_route(ax25_address *, struct net_device *);
45 * small macro to drop non-digipeated digipeaters and reverse path
47 static inline void ax25_route_invert(ax25_digi *in, ax25_digi *out)
51 for (k = 0; k < in->ndigi; k++)
57 ax25_digi_invert(in, out);
60 void ax25_rt_device_down(struct net_device *dev)
62 ax25_route *s, *t, *ax25_rt;
64 write_lock(&ax25_route_lock);
65 ax25_rt = ax25_route_list;
66 while (ax25_rt != NULL) {
68 ax25_rt = ax25_rt->next;
71 if (ax25_route_list == s) {
72 ax25_route_list = s->next;
73 if (s->digipeat != NULL)
77 for (t = ax25_route_list; t != NULL; t = t->next) {
80 if (s->digipeat != NULL)
89 write_unlock(&ax25_route_lock);
92 static int ax25_rt_add(struct ax25_routes_struct *route)
98 if ((ax25_dev = ax25_addr_ax25dev(&route->port_addr)) == NULL)
100 if (route->digi_count > AX25_MAX_DIGIS)
103 write_lock(&ax25_route_lock);
105 ax25_rt = ax25_route_list;
106 while (ax25_rt != NULL) {
107 if (ax25cmp(&ax25_rt->callsign, &route->dest_addr) == 0 &&
108 ax25_rt->dev == ax25_dev->dev) {
109 if (ax25_rt->digipeat != NULL) {
110 kfree(ax25_rt->digipeat);
111 ax25_rt->digipeat = NULL;
113 if (route->digi_count != 0) {
114 if ((ax25_rt->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
115 write_unlock(&ax25_route_lock);
118 ax25_rt->digipeat->lastrepeat = -1;
119 ax25_rt->digipeat->ndigi = route->digi_count;
120 for (i = 0; i < route->digi_count; i++) {
121 ax25_rt->digipeat->repeated[i] = 0;
122 ax25_rt->digipeat->calls[i] = route->digi_addr[i];
127 ax25_rt = ax25_rt->next;
130 if ((ax25_rt = kmalloc(sizeof(ax25_route), GFP_ATOMIC)) == NULL) {
131 write_unlock(&ax25_route_lock);
135 atomic_set(&ax25_rt->ref, 0);
136 ax25_rt->callsign = route->dest_addr;
137 ax25_rt->dev = ax25_dev->dev;
138 ax25_rt->digipeat = NULL;
139 ax25_rt->ip_mode = ' ';
140 if (route->digi_count != 0) {
141 if ((ax25_rt->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
142 write_unlock(&ax25_route_lock);
146 ax25_rt->digipeat->lastrepeat = -1;
147 ax25_rt->digipeat->ndigi = route->digi_count;
148 for (i = 0; i < route->digi_count; i++) {
149 ax25_rt->digipeat->repeated[i] = 0;
150 ax25_rt->digipeat->calls[i] = route->digi_addr[i];
153 ax25_rt->next = ax25_route_list;
154 ax25_route_list = ax25_rt;
155 write_unlock(&ax25_route_lock);
160 static void ax25_rt_destroy(ax25_route *ax25_rt)
162 if (atomic_read(&ax25_rt->ref) == 0) {
163 if (ax25_rt->digipeat != NULL)
164 kfree(ax25_rt->digipeat);
170 * Uh... Route is still in use; we can't yet destroy it. Retry later.
172 init_timer(&ax25_rt->timer);
173 ax25_rt->timer.data = (unsigned long) ax25_rt;
174 ax25_rt->timer.function = (void *) ax25_rt_destroy;
175 ax25_rt->timer.expires = jiffies + 5 * HZ;
177 add_timer(&ax25_rt->timer);
180 static int ax25_rt_del(struct ax25_routes_struct *route)
182 ax25_route *s, *t, *ax25_rt;
185 if ((ax25_dev = ax25_addr_ax25dev(&route->port_addr)) == NULL)
188 write_lock(&ax25_route_lock);
190 ax25_rt = ax25_route_list;
191 while (ax25_rt != NULL) {
193 ax25_rt = ax25_rt->next;
194 if (s->dev == ax25_dev->dev &&
195 ax25cmp(&route->dest_addr, &s->callsign) == 0) {
196 if (ax25_route_list == s) {
197 ax25_route_list = s->next;
200 for (t = ax25_route_list; t != NULL; t = t->next) {
210 write_unlock(&ax25_route_lock);
215 static int ax25_rt_opt(struct ax25_route_opt_struct *rt_option)
221 if ((ax25_dev = ax25_addr_ax25dev(&rt_option->port_addr)) == NULL)
224 write_lock(&ax25_route_lock);
226 ax25_rt = ax25_route_list;
227 while (ax25_rt != NULL) {
228 if (ax25_rt->dev == ax25_dev->dev &&
229 ax25cmp(&rt_option->dest_addr, &ax25_rt->callsign) == 0) {
230 switch (rt_option->cmd) {
231 case AX25_SET_RT_IPMODE:
232 switch (rt_option->arg) {
236 ax25_rt->ip_mode = rt_option->arg;
248 ax25_rt = ax25_rt->next;
252 write_unlock(&ax25_route_lock);
256 int ax25_rt_ioctl(unsigned int cmd, void *arg)
258 struct ax25_route_opt_struct rt_option;
259 struct ax25_routes_struct route;
263 if (copy_from_user(&route, arg, sizeof(route)))
265 return ax25_rt_add(&route);
268 if (copy_from_user(&route, arg, sizeof(route)))
270 return ax25_rt_del(&route);
273 if (copy_from_user(&rt_option, arg, sizeof(rt_option)))
275 return ax25_rt_opt(&rt_option);
282 #ifdef CONFIG_PROC_FS
284 static void *ax25_rt_seq_start(struct seq_file *seq, loff_t *pos)
286 struct ax25_route *ax25_rt;
289 read_lock(&ax25_route_lock);
291 return SEQ_START_TOKEN;
293 for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
302 static void *ax25_rt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
305 return (v == SEQ_START_TOKEN) ? ax25_route_list :
306 ((struct ax25_route *) v)->next;
309 static void ax25_rt_seq_stop(struct seq_file *seq, void *v)
311 read_unlock(&ax25_route_lock);
314 static int ax25_rt_seq_show(struct seq_file *seq, void *v)
316 if (v == SEQ_START_TOKEN)
317 seq_puts(seq, "callsign dev mode digipeaters\n");
319 struct ax25_route *ax25_rt = v;
320 const char *callsign;
323 if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
324 callsign = "default";
326 callsign = ax2asc(&ax25_rt->callsign);
328 seq_printf(seq, "%-9s %-4s",
330 ax25_rt->dev ? ax25_rt->dev->name : "???");
332 switch (ax25_rt->ip_mode) {
334 seq_puts(seq, " vc");
337 seq_puts(seq, " dg");
344 if (ax25_rt->digipeat != NULL)
345 for (i = 0; i < ax25_rt->digipeat->ndigi; i++)
346 seq_printf(seq, " %s", ax2asc(&ax25_rt->digipeat->calls[i]));
353 static struct seq_operations ax25_rt_seqops = {
354 .start = ax25_rt_seq_start,
355 .next = ax25_rt_seq_next,
356 .stop = ax25_rt_seq_stop,
357 .show = ax25_rt_seq_show,
360 static int ax25_rt_info_open(struct inode *inode, struct file *file)
362 return seq_open(file, &ax25_rt_seqops);
365 struct file_operations ax25_route_fops = {
366 .owner = THIS_MODULE,
367 .open = ax25_rt_info_open,
370 .release = seq_release,
378 * Only routes with a refernce rout of zero can be destroyed.
380 static ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
382 ax25_route *ax25_spe_rt = NULL;
383 ax25_route *ax25_def_rt = NULL;
386 read_lock(&ax25_route_lock);
388 * Bind to the physical interface we heard them on, or the default
389 * route if none is found;
391 for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
393 if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev != NULL)
394 ax25_spe_rt = ax25_rt;
395 if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev != NULL)
396 ax25_def_rt = ax25_rt;
398 if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev == dev)
399 ax25_spe_rt = ax25_rt;
400 if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
401 ax25_def_rt = ax25_rt;
405 ax25_rt = ax25_def_rt;
406 if (ax25_spe_rt != NULL)
407 ax25_rt = ax25_spe_rt;
410 atomic_inc(&ax25_rt->ref);
412 read_unlock(&ax25_route_lock);
418 * Adjust path: If you specify a default route and want to connect
419 * a target on the digipeater path but w/o having a special route
420 * set before, the path has to be truncated from your target on.
422 static inline void ax25_adjust_path(ax25_address *addr, ax25_digi *digipeat)
426 for (k = 0; k < digipeat->ndigi; k++) {
427 if (ax25cmp(addr, &digipeat->calls[k]) == 0)
436 * Find which interface to use.
438 int ax25_rt_autobind(ax25_cb *ax25, ax25_address *addr)
444 if ((ax25_rt = ax25_get_route(addr, NULL)) == NULL)
445 return -EHOSTUNREACH;
447 if ((ax25->ax25_dev = ax25_dev_ax25dev(ax25_rt->dev)) == NULL) {
452 if ((call = ax25_findbyuid(current->euid)) == NULL) {
453 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
457 call = (ax25_address *)ax25->ax25_dev->dev->dev_addr;
460 ax25->source_addr = *call;
462 if (ax25_rt->digipeat != NULL) {
463 if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
467 memcpy(ax25->digipeat, ax25_rt->digipeat, sizeof(ax25_digi));
468 ax25_adjust_path(addr, ax25->digipeat);
471 if (ax25->sk != NULL) {
472 bh_lock_sock(ax25->sk);
473 ax25->sk->sk_zapped = 0;
474 bh_unlock_sock(ax25->sk);
478 ax25_put_route(ax25_rt);
483 ax25_route *ax25_rt_find_route(ax25_route * route, ax25_address *addr,
484 struct net_device *dev)
488 if ((ax25_rt = ax25_get_route(addr, dev)))
492 atomic_set(&route->ref, 1);
493 route->callsign = *addr;
495 route->digipeat = NULL;
496 route->ip_mode = ' ';
501 struct sk_buff *ax25_rt_build_path(struct sk_buff *skb, ax25_address *src,
502 ax25_address *dest, ax25_digi *digi)
504 struct sk_buff *skbn;
508 len = digi->ndigi * AX25_ADDR_LEN;
510 if (skb_headroom(skb) < len) {
511 if ((skbn = skb_realloc_headroom(skb, len)) == NULL) {
512 printk(KERN_CRIT "AX.25: ax25_dg_build_path - out of memory\n");
517 skb_set_owner_w(skbn, skb->sk);
524 bp = skb_push(skb, len);
526 ax25_addr_build(bp, src, dest, digi, AX25_COMMAND, AX25_MODULUS);
532 * Free all memory associated with routing structures.
534 void __exit ax25_rt_free(void)
536 ax25_route *s, *ax25_rt = ax25_route_list;
538 write_lock(&ax25_route_lock);
539 while (ax25_rt != NULL) {
541 ax25_rt = ax25_rt->next;
543 if (s->digipeat != NULL)
548 write_unlock(&ax25_route_lock);