patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / net / ax25 / ax25_route.c
1 /*
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.
6  *
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)
13  */
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/socket.h>
17 #include <linux/timer.h>
18 #include <linux/in.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>
24 #include <net/ax25.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>
30 #include <net/sock.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include <linux/fcntl.h>
34 #include <linux/mm.h>
35 #include <linux/interrupt.h>
36 #include <linux/init.h>
37 #include <linux/seq_file.h>
38
39 static ax25_route *ax25_route_list;
40 static rwlock_t ax25_route_lock = RW_LOCK_UNLOCKED;
41
42 static ax25_route *ax25_get_route(ax25_address *, struct net_device *);
43
44 /*
45  * small macro to drop non-digipeated digipeaters and reverse path
46  */
47 static inline void ax25_route_invert(ax25_digi *in, ax25_digi *out)
48 {
49         int k;
50
51         for (k = 0; k < in->ndigi; k++)
52                 if (!in->repeated[k])
53                         break;
54
55         in->ndigi = k;
56
57         ax25_digi_invert(in, out);
58 }
59
60 void ax25_rt_device_down(struct net_device *dev)
61 {
62         ax25_route *s, *t, *ax25_rt;
63
64         write_lock(&ax25_route_lock);
65         ax25_rt = ax25_route_list;
66         while (ax25_rt != NULL) {
67                 s       = ax25_rt;
68                 ax25_rt = ax25_rt->next;
69
70                 if (s->dev == dev) {
71                         if (ax25_route_list == s) {
72                                 ax25_route_list = s->next;
73                                 if (s->digipeat != NULL)
74                                         kfree(s->digipeat);
75                                 kfree(s);
76                         } else {
77                                 for (t = ax25_route_list; t != NULL; t = t->next) {
78                                         if (t->next == s) {
79                                                 t->next = s->next;
80                                                 if (s->digipeat != NULL)
81                                                         kfree(s->digipeat);
82                                                 kfree(s);
83                                                 break;
84                                         }
85                                 }
86                         }
87                 }
88         }
89         write_unlock(&ax25_route_lock);
90 }
91
92 static int ax25_rt_add(struct ax25_routes_struct *route)
93 {
94         ax25_route *ax25_rt;
95         ax25_dev *ax25_dev;
96         int i;
97
98         if ((ax25_dev = ax25_addr_ax25dev(&route->port_addr)) == NULL)
99                 return -EINVAL;
100         if (route->digi_count > AX25_MAX_DIGIS)
101                 return -EINVAL;
102
103         write_lock(&ax25_route_lock);
104
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;
112                         }
113                         if (route->digi_count != 0) {
114                                 if ((ax25_rt->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
115                                         write_unlock(&ax25_route_lock);
116                                         return -ENOMEM;
117                                 }
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];
123                                 }
124                         }
125                         return 0;
126                 }
127                 ax25_rt = ax25_rt->next;
128         }
129
130         if ((ax25_rt = kmalloc(sizeof(ax25_route), GFP_ATOMIC)) == NULL) {
131                 write_unlock(&ax25_route_lock);
132                 return -ENOMEM;
133         }
134
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);
143                         kfree(ax25_rt);
144                         return -ENOMEM;
145                 }
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];
151                 }
152         }
153         ax25_rt->next   = ax25_route_list;
154         ax25_route_list = ax25_rt;
155         write_unlock(&ax25_route_lock);
156
157         return 0;
158 }
159
160 static void ax25_rt_destroy(ax25_route *ax25_rt)
161 {
162         if (atomic_read(&ax25_rt->ref) == 0) {
163                 if (ax25_rt->digipeat != NULL)
164                         kfree(ax25_rt->digipeat);
165                 kfree(ax25_rt);
166                 return;
167         }
168
169         /*
170          * Uh...  Route is still in use; we can't yet destroy it.  Retry later.
171          */
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;
176
177         add_timer(&ax25_rt->timer);
178 }
179
180 static int ax25_rt_del(struct ax25_routes_struct *route)
181 {
182         ax25_route *s, *t, *ax25_rt;
183         ax25_dev *ax25_dev;
184
185         if ((ax25_dev = ax25_addr_ax25dev(&route->port_addr)) == NULL)
186                 return -EINVAL;
187
188         write_lock(&ax25_route_lock);
189
190         ax25_rt = ax25_route_list;
191         while (ax25_rt != NULL) {
192                 s       = ax25_rt;
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;
198                                 ax25_rt_destroy(s);
199                         } else {
200                                 for (t = ax25_route_list; t != NULL; t = t->next) {
201                                         if (t->next == s) {
202                                                 t->next = s->next;
203                                                 ax25_rt_destroy(s);
204                                                 break;
205                                         }
206                                 }
207                         }
208                 }
209         }
210         write_unlock(&ax25_route_lock);
211
212         return 0;
213 }
214
215 static int ax25_rt_opt(struct ax25_route_opt_struct *rt_option)
216 {
217         ax25_route *ax25_rt;
218         ax25_dev *ax25_dev;
219         int err = 0;
220
221         if ((ax25_dev = ax25_addr_ax25dev(&rt_option->port_addr)) == NULL)
222                 return -EINVAL;
223
224         write_lock(&ax25_route_lock);
225
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) {
233                                 case ' ':
234                                 case 'D':
235                                 case 'V':
236                                         ax25_rt->ip_mode = rt_option->arg;
237                                         break;
238                                 default:
239                                         err = -EINVAL;
240                                         goto out;
241                                 }
242                                 break;
243                         default:
244                                 err = -EINVAL;
245                                 goto out;
246                         }
247                 }
248                 ax25_rt = ax25_rt->next;
249         }
250
251 out:
252         write_unlock(&ax25_route_lock);
253         return err;
254 }
255
256 int ax25_rt_ioctl(unsigned int cmd, void __user *arg)
257 {
258         struct ax25_route_opt_struct rt_option;
259         struct ax25_routes_struct route;
260
261         switch (cmd) {
262         case SIOCADDRT:
263                 if (copy_from_user(&route, arg, sizeof(route)))
264                         return -EFAULT;
265                 return ax25_rt_add(&route);
266
267         case SIOCDELRT:
268                 if (copy_from_user(&route, arg, sizeof(route)))
269                         return -EFAULT;
270                 return ax25_rt_del(&route);
271
272         case SIOCAX25OPTRT:
273                 if (copy_from_user(&rt_option, arg, sizeof(rt_option)))
274                         return -EFAULT;
275                 return ax25_rt_opt(&rt_option);
276
277         default:
278                 return -EINVAL;
279         }
280 }
281
282 #ifdef CONFIG_PROC_FS
283
284 static void *ax25_rt_seq_start(struct seq_file *seq, loff_t *pos)
285 {
286         struct ax25_route *ax25_rt;
287         int i = 1;
288  
289         read_lock(&ax25_route_lock);
290         if (*pos == 0)
291                 return SEQ_START_TOKEN;
292
293         for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
294                 if (i == *pos)
295                         return ax25_rt;
296                 ++i;
297         }
298
299         return NULL;
300 }
301
302 static void *ax25_rt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
303 {
304         ++*pos;
305         return (v == SEQ_START_TOKEN) ? ax25_route_list : 
306                 ((struct ax25_route *) v)->next;
307 }
308
309 static void ax25_rt_seq_stop(struct seq_file *seq, void *v)
310 {
311         read_unlock(&ax25_route_lock);
312 }
313
314 static int ax25_rt_seq_show(struct seq_file *seq, void *v)
315 {
316         if (v == SEQ_START_TOKEN)
317                 seq_puts(seq, "callsign  dev  mode digipeaters\n");
318         else {
319                 struct ax25_route *ax25_rt = v;
320                 const char *callsign;
321                 int i;
322
323                 if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
324                         callsign = "default";
325                 else
326                         callsign = ax2asc(&ax25_rt->callsign);
327
328                 seq_printf(seq, "%-9s %-4s",
329                         callsign,
330                         ax25_rt->dev ? ax25_rt->dev->name : "???");
331
332                 switch (ax25_rt->ip_mode) {
333                 case 'V':
334                         seq_puts(seq, "   vc");
335                         break;
336                 case 'D':
337                         seq_puts(seq, "   dg");
338                         break;
339                 default:
340                         seq_puts(seq, "    *");
341                         break;
342                 }
343
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]));
347
348                 seq_puts(seq, "\n");
349         }
350         return 0;
351 }
352
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,
358 };
359
360 static int ax25_rt_info_open(struct inode *inode, struct file *file)
361 {
362         return seq_open(file, &ax25_rt_seqops);
363 }
364
365 struct file_operations ax25_route_fops = {
366         .owner = THIS_MODULE,
367         .open = ax25_rt_info_open,
368         .read = seq_read,
369         .llseek = seq_lseek,
370         .release = seq_release,
371 };
372
373 #endif
374
375 /*
376  *      Find AX.25 route
377  *
378  *      Only routes with a refernce rout of zero can be destroyed.
379  */
380 static ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
381 {
382         ax25_route *ax25_spe_rt = NULL;
383         ax25_route *ax25_def_rt = NULL;
384         ax25_route *ax25_rt;
385
386         read_lock(&ax25_route_lock);
387         /*
388          *      Bind to the physical interface we heard them on, or the default
389          *      route if none is found;
390          */
391         for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
392                 if (dev == NULL) {
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;
397                 } else {
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;
402                 }
403         }
404
405         ax25_rt = ax25_def_rt;
406         if (ax25_spe_rt != NULL)
407                 ax25_rt = ax25_spe_rt;
408
409         if (ax25_rt != NULL)
410                 atomic_inc(&ax25_rt->ref);
411
412         read_unlock(&ax25_route_lock);
413
414         return ax25_rt;
415 }
416
417 /*
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.
421  */
422 static inline void ax25_adjust_path(ax25_address *addr, ax25_digi *digipeat)
423 {
424         int k;
425
426         for (k = 0; k < digipeat->ndigi; k++) {
427                 if (ax25cmp(addr, &digipeat->calls[k]) == 0)
428                         break;
429         }
430
431         digipeat->ndigi = k;
432 }
433
434
435 /*
436  *      Find which interface to use.
437  */
438 int ax25_rt_autobind(ax25_cb *ax25, ax25_address *addr)
439 {
440         ax25_route *ax25_rt;
441         ax25_address *call;
442         int err;
443
444         if ((ax25_rt = ax25_get_route(addr, NULL)) == NULL)
445                 return -EHOSTUNREACH;
446
447         if ((ax25->ax25_dev = ax25_dev_ax25dev(ax25_rt->dev)) == NULL) {
448                 err = -EHOSTUNREACH;
449                 goto put;
450         }
451
452         if ((call = ax25_findbyuid(current->euid)) == NULL) {
453                 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
454                         err = -EPERM;
455                         goto put;
456                 }
457                 call = (ax25_address *)ax25->ax25_dev->dev->dev_addr;
458         }
459
460         ax25->source_addr = *call;
461
462         if (ax25_rt->digipeat != NULL) {
463                 if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
464                         err = -ENOMEM;
465                         goto put;
466                 }
467                 memcpy(ax25->digipeat, ax25_rt->digipeat, sizeof(ax25_digi));
468                 ax25_adjust_path(addr, ax25->digipeat);
469         }
470
471         if (ax25->sk != NULL) {
472                 bh_lock_sock(ax25->sk);
473                 ax25->sk->sk_zapped = 0;
474                 bh_unlock_sock(ax25->sk);
475         }
476
477 put:
478         ax25_put_route(ax25_rt);
479
480         return 0;
481 }
482
483 ax25_route *ax25_rt_find_route(ax25_route * route, ax25_address *addr,
484         struct net_device *dev)
485 {
486         ax25_route *ax25_rt;
487
488         if ((ax25_rt = ax25_get_route(addr, dev)))
489                 return ax25_rt;
490
491         route->next     = NULL;
492         atomic_set(&route->ref, 1);
493         route->callsign = *addr;
494         route->dev      = dev;
495         route->digipeat = NULL;
496         route->ip_mode  = ' ';
497
498         return route;
499 }
500
501 struct sk_buff *ax25_rt_build_path(struct sk_buff *skb, ax25_address *src,
502         ax25_address *dest, ax25_digi *digi)
503 {
504         struct sk_buff *skbn;
505         unsigned char *bp;
506         int len;
507
508         len = digi->ndigi * AX25_ADDR_LEN;
509
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");
513                         return NULL;
514                 }
515
516                 if (skb->sk != NULL)
517                         skb_set_owner_w(skbn, skb->sk);
518
519                 kfree_skb(skb);
520
521                 skb = skbn;
522         }
523
524         bp = skb_push(skb, len);
525
526         ax25_addr_build(bp, src, dest, digi, AX25_COMMAND, AX25_MODULUS);
527
528         return skb;
529 }
530
531 /*
532  *      Free all memory associated with routing structures.
533  */
534 void __exit ax25_rt_free(void)
535 {
536         ax25_route *s, *ax25_rt = ax25_route_list;
537
538         write_lock(&ax25_route_lock);
539         while (ax25_rt != NULL) {
540                 s       = ax25_rt;
541                 ax25_rt = ax25_rt->next;
542
543                 if (s->digipeat != NULL)
544                         kfree(s->digipeat);
545
546                 kfree(s);
547         }
548         write_unlock(&ax25_route_lock);
549 }