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];
125 write_unlock(&ax25_route_lock);
128 ax25_rt = ax25_rt->next;
131 if ((ax25_rt = kmalloc(sizeof(ax25_route), GFP_ATOMIC)) == NULL) {
132 write_unlock(&ax25_route_lock);
136 atomic_set(&ax25_rt->ref, 0);
137 ax25_rt->callsign = route->dest_addr;
138 ax25_rt->dev = ax25_dev->dev;
139 ax25_rt->digipeat = NULL;
140 ax25_rt->ip_mode = ' ';
141 if (route->digi_count != 0) {
142 if ((ax25_rt->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
143 write_unlock(&ax25_route_lock);
147 ax25_rt->digipeat->lastrepeat = -1;
148 ax25_rt->digipeat->ndigi = route->digi_count;
149 for (i = 0; i < route->digi_count; i++) {
150 ax25_rt->digipeat->repeated[i] = 0;
151 ax25_rt->digipeat->calls[i] = route->digi_addr[i];
154 ax25_rt->next = ax25_route_list;
155 ax25_route_list = ax25_rt;
156 write_unlock(&ax25_route_lock);
161 static void ax25_rt_destroy(ax25_route *ax25_rt)
163 if (atomic_read(&ax25_rt->ref) == 0) {
164 if (ax25_rt->digipeat != NULL)
165 kfree(ax25_rt->digipeat);
171 * Uh... Route is still in use; we can't yet destroy it. Retry later.
173 init_timer(&ax25_rt->timer);
174 ax25_rt->timer.data = (unsigned long) ax25_rt;
175 ax25_rt->timer.function = (void *) ax25_rt_destroy;
176 ax25_rt->timer.expires = jiffies + 5 * HZ;
178 add_timer(&ax25_rt->timer);
181 static int ax25_rt_del(struct ax25_routes_struct *route)
183 ax25_route *s, *t, *ax25_rt;
186 if ((ax25_dev = ax25_addr_ax25dev(&route->port_addr)) == NULL)
189 write_lock(&ax25_route_lock);
191 ax25_rt = ax25_route_list;
192 while (ax25_rt != NULL) {
194 ax25_rt = ax25_rt->next;
195 if (s->dev == ax25_dev->dev &&
196 ax25cmp(&route->dest_addr, &s->callsign) == 0) {
197 if (ax25_route_list == s) {
198 ax25_route_list = s->next;
201 for (t = ax25_route_list; t != NULL; t = t->next) {
211 write_unlock(&ax25_route_lock);
216 static int ax25_rt_opt(struct ax25_route_opt_struct *rt_option)
222 if ((ax25_dev = ax25_addr_ax25dev(&rt_option->port_addr)) == NULL)
225 write_lock(&ax25_route_lock);
227 ax25_rt = ax25_route_list;
228 while (ax25_rt != NULL) {
229 if (ax25_rt->dev == ax25_dev->dev &&
230 ax25cmp(&rt_option->dest_addr, &ax25_rt->callsign) == 0) {
231 switch (rt_option->cmd) {
232 case AX25_SET_RT_IPMODE:
233 switch (rt_option->arg) {
237 ax25_rt->ip_mode = rt_option->arg;
249 ax25_rt = ax25_rt->next;
253 write_unlock(&ax25_route_lock);
257 int ax25_rt_ioctl(unsigned int cmd, void __user *arg)
259 struct ax25_route_opt_struct rt_option;
260 struct ax25_routes_struct route;
264 if (copy_from_user(&route, arg, sizeof(route)))
266 return ax25_rt_add(&route);
269 if (copy_from_user(&route, arg, sizeof(route)))
271 return ax25_rt_del(&route);
274 if (copy_from_user(&rt_option, arg, sizeof(rt_option)))
276 return ax25_rt_opt(&rt_option);
283 #ifdef CONFIG_PROC_FS
285 static void *ax25_rt_seq_start(struct seq_file *seq, loff_t *pos)
287 struct ax25_route *ax25_rt;
290 read_lock(&ax25_route_lock);
292 return SEQ_START_TOKEN;
294 for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
303 static void *ax25_rt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
306 return (v == SEQ_START_TOKEN) ? ax25_route_list :
307 ((struct ax25_route *) v)->next;
310 static void ax25_rt_seq_stop(struct seq_file *seq, void *v)
312 read_unlock(&ax25_route_lock);
315 static int ax25_rt_seq_show(struct seq_file *seq, void *v)
317 if (v == SEQ_START_TOKEN)
318 seq_puts(seq, "callsign dev mode digipeaters\n");
320 struct ax25_route *ax25_rt = v;
321 const char *callsign;
324 if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
325 callsign = "default";
327 callsign = ax2asc(&ax25_rt->callsign);
329 seq_printf(seq, "%-9s %-4s",
331 ax25_rt->dev ? ax25_rt->dev->name : "???");
333 switch (ax25_rt->ip_mode) {
335 seq_puts(seq, " vc");
338 seq_puts(seq, " dg");
345 if (ax25_rt->digipeat != NULL)
346 for (i = 0; i < ax25_rt->digipeat->ndigi; i++)
347 seq_printf(seq, " %s", ax2asc(&ax25_rt->digipeat->calls[i]));
354 static struct seq_operations ax25_rt_seqops = {
355 .start = ax25_rt_seq_start,
356 .next = ax25_rt_seq_next,
357 .stop = ax25_rt_seq_stop,
358 .show = ax25_rt_seq_show,
361 static int ax25_rt_info_open(struct inode *inode, struct file *file)
363 return seq_open(file, &ax25_rt_seqops);
366 struct file_operations ax25_route_fops = {
367 .owner = THIS_MODULE,
368 .open = ax25_rt_info_open,
371 .release = seq_release,
379 * Only routes with a refernce rout of zero can be destroyed.
381 static ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
383 ax25_route *ax25_spe_rt = NULL;
384 ax25_route *ax25_def_rt = NULL;
387 read_lock(&ax25_route_lock);
389 * Bind to the physical interface we heard them on, or the default
390 * route if none is found;
392 for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
394 if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev != NULL)
395 ax25_spe_rt = ax25_rt;
396 if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev != NULL)
397 ax25_def_rt = ax25_rt;
399 if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev == dev)
400 ax25_spe_rt = ax25_rt;
401 if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
402 ax25_def_rt = ax25_rt;
406 ax25_rt = ax25_def_rt;
407 if (ax25_spe_rt != NULL)
408 ax25_rt = ax25_spe_rt;
411 atomic_inc(&ax25_rt->ref);
413 read_unlock(&ax25_route_lock);
419 * Adjust path: If you specify a default route and want to connect
420 * a target on the digipeater path but w/o having a special route
421 * set before, the path has to be truncated from your target on.
423 static inline void ax25_adjust_path(ax25_address *addr, ax25_digi *digipeat)
427 for (k = 0; k < digipeat->ndigi; k++) {
428 if (ax25cmp(addr, &digipeat->calls[k]) == 0)
437 * Find which interface to use.
439 int ax25_rt_autobind(ax25_cb *ax25, ax25_address *addr)
445 if ((ax25_rt = ax25_get_route(addr, NULL)) == NULL)
446 return -EHOSTUNREACH;
448 if ((ax25->ax25_dev = ax25_dev_ax25dev(ax25_rt->dev)) == NULL) {
453 if ((call = ax25_findbyuid(current->euid)) == NULL) {
454 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
458 call = (ax25_address *)ax25->ax25_dev->dev->dev_addr;
461 ax25->source_addr = *call;
463 if (ax25_rt->digipeat != NULL) {
464 if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
468 memcpy(ax25->digipeat, ax25_rt->digipeat, sizeof(ax25_digi));
469 ax25_adjust_path(addr, ax25->digipeat);
472 if (ax25->sk != NULL) {
473 bh_lock_sock(ax25->sk);
474 ax25->sk->sk_zapped = 0;
475 bh_unlock_sock(ax25->sk);
479 ax25_put_route(ax25_rt);
484 ax25_route *ax25_rt_find_route(ax25_route * route, ax25_address *addr,
485 struct net_device *dev)
489 if ((ax25_rt = ax25_get_route(addr, dev)))
493 atomic_set(&route->ref, 1);
494 route->callsign = *addr;
496 route->digipeat = NULL;
497 route->ip_mode = ' ';
502 struct sk_buff *ax25_rt_build_path(struct sk_buff *skb, ax25_address *src,
503 ax25_address *dest, ax25_digi *digi)
505 struct sk_buff *skbn;
509 len = digi->ndigi * AX25_ADDR_LEN;
511 if (skb_headroom(skb) < len) {
512 if ((skbn = skb_realloc_headroom(skb, len)) == NULL) {
513 printk(KERN_CRIT "AX.25: ax25_dg_build_path - out of memory\n");
518 skb_set_owner_w(skbn, skb->sk);
525 bp = skb_push(skb, len);
527 ax25_addr_build(bp, src, dest, digi, AX25_COMMAND, AX25_MODULUS);
533 * Free all memory associated with routing structures.
535 void __exit ax25_rt_free(void)
537 ax25_route *s, *ax25_rt = ax25_route_list;
539 write_lock(&ax25_route_lock);
540 while (ax25_rt != NULL) {
542 ax25_rt = ax25_rt->next;
544 if (s->digipeat != NULL)
549 write_unlock(&ax25_route_lock);