0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/capability.h>
0013 #include <linux/errno.h>
0014 #include <linux/types.h>
0015 #include <linux/socket.h>
0016 #include <linux/timer.h>
0017 #include <linux/in.h>
0018 #include <linux/kernel.h>
0019 #include <linux/sched.h>
0020 #include <linux/string.h>
0021 #include <linux/sockios.h>
0022 #include <linux/net.h>
0023 #include <linux/slab.h>
0024 #include <net/ax25.h>
0025 #include <linux/inet.h>
0026 #include <linux/netdevice.h>
0027 #include <linux/if_arp.h>
0028 #include <linux/skbuff.h>
0029 #include <linux/spinlock.h>
0030 #include <net/sock.h>
0031 #include <linux/uaccess.h>
0032 #include <linux/fcntl.h>
0033 #include <linux/mm.h>
0034 #include <linux/interrupt.h>
0035 #include <linux/init.h>
0036 #include <linux/seq_file.h>
0037 #include <linux/export.h>
0038
0039 static ax25_route *ax25_route_list;
0040 DEFINE_RWLOCK(ax25_route_lock);
0041
0042 void ax25_rt_device_down(struct net_device *dev)
0043 {
0044 ax25_route *s, *t, *ax25_rt;
0045
0046 write_lock_bh(&ax25_route_lock);
0047 ax25_rt = ax25_route_list;
0048 while (ax25_rt != NULL) {
0049 s = ax25_rt;
0050 ax25_rt = ax25_rt->next;
0051
0052 if (s->dev == dev) {
0053 if (ax25_route_list == s) {
0054 ax25_route_list = s->next;
0055 kfree(s->digipeat);
0056 kfree(s);
0057 } else {
0058 for (t = ax25_route_list; t != NULL; t = t->next) {
0059 if (t->next == s) {
0060 t->next = s->next;
0061 kfree(s->digipeat);
0062 kfree(s);
0063 break;
0064 }
0065 }
0066 }
0067 }
0068 }
0069 write_unlock_bh(&ax25_route_lock);
0070 }
0071
0072 static int __must_check ax25_rt_add(struct ax25_routes_struct *route)
0073 {
0074 ax25_route *ax25_rt;
0075 ax25_dev *ax25_dev;
0076 int i;
0077
0078 if (route->digi_count > AX25_MAX_DIGIS)
0079 return -EINVAL;
0080
0081 ax25_dev = ax25_addr_ax25dev(&route->port_addr);
0082 if (!ax25_dev)
0083 return -EINVAL;
0084
0085 write_lock_bh(&ax25_route_lock);
0086
0087 ax25_rt = ax25_route_list;
0088 while (ax25_rt != NULL) {
0089 if (ax25cmp(&ax25_rt->callsign, &route->dest_addr) == 0 &&
0090 ax25_rt->dev == ax25_dev->dev) {
0091 kfree(ax25_rt->digipeat);
0092 ax25_rt->digipeat = NULL;
0093 if (route->digi_count != 0) {
0094 if ((ax25_rt->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
0095 write_unlock_bh(&ax25_route_lock);
0096 ax25_dev_put(ax25_dev);
0097 return -ENOMEM;
0098 }
0099 ax25_rt->digipeat->lastrepeat = -1;
0100 ax25_rt->digipeat->ndigi = route->digi_count;
0101 for (i = 0; i < route->digi_count; i++) {
0102 ax25_rt->digipeat->repeated[i] = 0;
0103 ax25_rt->digipeat->calls[i] = route->digi_addr[i];
0104 }
0105 }
0106 write_unlock_bh(&ax25_route_lock);
0107 ax25_dev_put(ax25_dev);
0108 return 0;
0109 }
0110 ax25_rt = ax25_rt->next;
0111 }
0112
0113 if ((ax25_rt = kmalloc(sizeof(ax25_route), GFP_ATOMIC)) == NULL) {
0114 write_unlock_bh(&ax25_route_lock);
0115 ax25_dev_put(ax25_dev);
0116 return -ENOMEM;
0117 }
0118
0119 ax25_rt->callsign = route->dest_addr;
0120 ax25_rt->dev = ax25_dev->dev;
0121 ax25_rt->digipeat = NULL;
0122 ax25_rt->ip_mode = ' ';
0123 if (route->digi_count != 0) {
0124 if ((ax25_rt->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
0125 write_unlock_bh(&ax25_route_lock);
0126 kfree(ax25_rt);
0127 ax25_dev_put(ax25_dev);
0128 return -ENOMEM;
0129 }
0130 ax25_rt->digipeat->lastrepeat = -1;
0131 ax25_rt->digipeat->ndigi = route->digi_count;
0132 for (i = 0; i < route->digi_count; i++) {
0133 ax25_rt->digipeat->repeated[i] = 0;
0134 ax25_rt->digipeat->calls[i] = route->digi_addr[i];
0135 }
0136 }
0137 ax25_rt->next = ax25_route_list;
0138 ax25_route_list = ax25_rt;
0139 write_unlock_bh(&ax25_route_lock);
0140 ax25_dev_put(ax25_dev);
0141
0142 return 0;
0143 }
0144
0145 void __ax25_put_route(ax25_route *ax25_rt)
0146 {
0147 kfree(ax25_rt->digipeat);
0148 kfree(ax25_rt);
0149 }
0150
0151 static int ax25_rt_del(struct ax25_routes_struct *route)
0152 {
0153 ax25_route *s, *t, *ax25_rt;
0154 ax25_dev *ax25_dev;
0155
0156 if ((ax25_dev = ax25_addr_ax25dev(&route->port_addr)) == NULL)
0157 return -EINVAL;
0158
0159 write_lock_bh(&ax25_route_lock);
0160
0161 ax25_rt = ax25_route_list;
0162 while (ax25_rt != NULL) {
0163 s = ax25_rt;
0164 ax25_rt = ax25_rt->next;
0165 if (s->dev == ax25_dev->dev &&
0166 ax25cmp(&route->dest_addr, &s->callsign) == 0) {
0167 if (ax25_route_list == s) {
0168 ax25_route_list = s->next;
0169 __ax25_put_route(s);
0170 } else {
0171 for (t = ax25_route_list; t != NULL; t = t->next) {
0172 if (t->next == s) {
0173 t->next = s->next;
0174 __ax25_put_route(s);
0175 break;
0176 }
0177 }
0178 }
0179 }
0180 }
0181 write_unlock_bh(&ax25_route_lock);
0182 ax25_dev_put(ax25_dev);
0183
0184 return 0;
0185 }
0186
0187 static int ax25_rt_opt(struct ax25_route_opt_struct *rt_option)
0188 {
0189 ax25_route *ax25_rt;
0190 ax25_dev *ax25_dev;
0191 int err = 0;
0192
0193 if ((ax25_dev = ax25_addr_ax25dev(&rt_option->port_addr)) == NULL)
0194 return -EINVAL;
0195
0196 write_lock_bh(&ax25_route_lock);
0197
0198 ax25_rt = ax25_route_list;
0199 while (ax25_rt != NULL) {
0200 if (ax25_rt->dev == ax25_dev->dev &&
0201 ax25cmp(&rt_option->dest_addr, &ax25_rt->callsign) == 0) {
0202 switch (rt_option->cmd) {
0203 case AX25_SET_RT_IPMODE:
0204 switch (rt_option->arg) {
0205 case ' ':
0206 case 'D':
0207 case 'V':
0208 ax25_rt->ip_mode = rt_option->arg;
0209 break;
0210 default:
0211 err = -EINVAL;
0212 goto out;
0213 }
0214 break;
0215 default:
0216 err = -EINVAL;
0217 goto out;
0218 }
0219 }
0220 ax25_rt = ax25_rt->next;
0221 }
0222
0223 out:
0224 write_unlock_bh(&ax25_route_lock);
0225 ax25_dev_put(ax25_dev);
0226 return err;
0227 }
0228
0229 int ax25_rt_ioctl(unsigned int cmd, void __user *arg)
0230 {
0231 struct ax25_route_opt_struct rt_option;
0232 struct ax25_routes_struct route;
0233
0234 switch (cmd) {
0235 case SIOCADDRT:
0236 if (copy_from_user(&route, arg, sizeof(route)))
0237 return -EFAULT;
0238 return ax25_rt_add(&route);
0239
0240 case SIOCDELRT:
0241 if (copy_from_user(&route, arg, sizeof(route)))
0242 return -EFAULT;
0243 return ax25_rt_del(&route);
0244
0245 case SIOCAX25OPTRT:
0246 if (copy_from_user(&rt_option, arg, sizeof(rt_option)))
0247 return -EFAULT;
0248 return ax25_rt_opt(&rt_option);
0249
0250 default:
0251 return -EINVAL;
0252 }
0253 }
0254
0255 #ifdef CONFIG_PROC_FS
0256
0257 static void *ax25_rt_seq_start(struct seq_file *seq, loff_t *pos)
0258 __acquires(ax25_route_lock)
0259 {
0260 struct ax25_route *ax25_rt;
0261 int i = 1;
0262
0263 read_lock(&ax25_route_lock);
0264 if (*pos == 0)
0265 return SEQ_START_TOKEN;
0266
0267 for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
0268 if (i == *pos)
0269 return ax25_rt;
0270 ++i;
0271 }
0272
0273 return NULL;
0274 }
0275
0276 static void *ax25_rt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
0277 {
0278 ++*pos;
0279 return (v == SEQ_START_TOKEN) ? ax25_route_list :
0280 ((struct ax25_route *) v)->next;
0281 }
0282
0283 static void ax25_rt_seq_stop(struct seq_file *seq, void *v)
0284 __releases(ax25_route_lock)
0285 {
0286 read_unlock(&ax25_route_lock);
0287 }
0288
0289 static int ax25_rt_seq_show(struct seq_file *seq, void *v)
0290 {
0291 char buf[11];
0292
0293 if (v == SEQ_START_TOKEN)
0294 seq_puts(seq, "callsign dev mode digipeaters\n");
0295 else {
0296 struct ax25_route *ax25_rt = v;
0297 const char *callsign;
0298 int i;
0299
0300 if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
0301 callsign = "default";
0302 else
0303 callsign = ax2asc(buf, &ax25_rt->callsign);
0304
0305 seq_printf(seq, "%-9s %-4s",
0306 callsign,
0307 ax25_rt->dev ? ax25_rt->dev->name : "???");
0308
0309 switch (ax25_rt->ip_mode) {
0310 case 'V':
0311 seq_puts(seq, " vc");
0312 break;
0313 case 'D':
0314 seq_puts(seq, " dg");
0315 break;
0316 default:
0317 seq_puts(seq, " *");
0318 break;
0319 }
0320
0321 if (ax25_rt->digipeat != NULL)
0322 for (i = 0; i < ax25_rt->digipeat->ndigi; i++)
0323 seq_printf(seq, " %s",
0324 ax2asc(buf, &ax25_rt->digipeat->calls[i]));
0325
0326 seq_puts(seq, "\n");
0327 }
0328 return 0;
0329 }
0330
0331 const struct seq_operations ax25_rt_seqops = {
0332 .start = ax25_rt_seq_start,
0333 .next = ax25_rt_seq_next,
0334 .stop = ax25_rt_seq_stop,
0335 .show = ax25_rt_seq_show,
0336 };
0337 #endif
0338
0339
0340
0341
0342
0343
0344
0345 ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
0346 {
0347 ax25_route *ax25_spe_rt = NULL;
0348 ax25_route *ax25_def_rt = NULL;
0349 ax25_route *ax25_rt;
0350
0351
0352
0353
0354
0355 for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
0356 if (dev == NULL) {
0357 if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev != NULL)
0358 ax25_spe_rt = ax25_rt;
0359 if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev != NULL)
0360 ax25_def_rt = ax25_rt;
0361 } else {
0362 if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev == dev)
0363 ax25_spe_rt = ax25_rt;
0364 if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
0365 ax25_def_rt = ax25_rt;
0366 }
0367 }
0368
0369 ax25_rt = ax25_def_rt;
0370 if (ax25_spe_rt != NULL)
0371 ax25_rt = ax25_spe_rt;
0372
0373 return ax25_rt;
0374 }
0375
0376
0377
0378
0379
0380
0381 static inline void ax25_adjust_path(ax25_address *addr, ax25_digi *digipeat)
0382 {
0383 int k;
0384
0385 for (k = 0; k < digipeat->ndigi; k++) {
0386 if (ax25cmp(addr, &digipeat->calls[k]) == 0)
0387 break;
0388 }
0389
0390 digipeat->ndigi = k;
0391 }
0392
0393
0394
0395
0396
0397 int ax25_rt_autobind(ax25_cb *ax25, ax25_address *addr)
0398 {
0399 ax25_uid_assoc *user;
0400 ax25_route *ax25_rt;
0401 int err = 0;
0402
0403 ax25_route_lock_use();
0404 ax25_rt = ax25_get_route(addr, NULL);
0405 if (!ax25_rt) {
0406 ax25_route_lock_unuse();
0407 return -EHOSTUNREACH;
0408 }
0409 if ((ax25->ax25_dev = ax25_dev_ax25dev(ax25_rt->dev)) == NULL) {
0410 err = -EHOSTUNREACH;
0411 goto put;
0412 }
0413
0414 user = ax25_findbyuid(current_euid());
0415 if (user) {
0416 ax25->source_addr = user->call;
0417 ax25_uid_put(user);
0418 } else {
0419 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
0420 err = -EPERM;
0421 goto put;
0422 }
0423 ax25->source_addr = *(ax25_address *)ax25->ax25_dev->dev->dev_addr;
0424 }
0425
0426 if (ax25_rt->digipeat != NULL) {
0427 ax25->digipeat = kmemdup(ax25_rt->digipeat, sizeof(ax25_digi),
0428 GFP_ATOMIC);
0429 if (ax25->digipeat == NULL) {
0430 err = -ENOMEM;
0431 goto put;
0432 }
0433 ax25_adjust_path(addr, ax25->digipeat);
0434 }
0435
0436 if (ax25->sk != NULL) {
0437 local_bh_disable();
0438 bh_lock_sock(ax25->sk);
0439 sock_reset_flag(ax25->sk, SOCK_ZAPPED);
0440 bh_unlock_sock(ax25->sk);
0441 local_bh_enable();
0442 }
0443
0444 put:
0445 ax25_route_lock_unuse();
0446 return err;
0447 }
0448
0449 struct sk_buff *ax25_rt_build_path(struct sk_buff *skb, ax25_address *src,
0450 ax25_address *dest, ax25_digi *digi)
0451 {
0452 unsigned char *bp;
0453 int len;
0454
0455 len = digi->ndigi * AX25_ADDR_LEN;
0456
0457 if (unlikely(skb_headroom(skb) < len)) {
0458 skb = skb_expand_head(skb, len);
0459 if (!skb) {
0460 printk(KERN_CRIT "AX.25: ax25_dg_build_path - out of memory\n");
0461 return NULL;
0462 }
0463 }
0464
0465 bp = skb_push(skb, len);
0466
0467 ax25_addr_build(bp, src, dest, digi, AX25_COMMAND, AX25_MODULUS);
0468
0469 return skb;
0470 }
0471
0472
0473
0474
0475 void __exit ax25_rt_free(void)
0476 {
0477 ax25_route *s, *ax25_rt = ax25_route_list;
0478
0479 write_lock_bh(&ax25_route_lock);
0480 while (ax25_rt != NULL) {
0481 s = ax25_rt;
0482 ax25_rt = ax25_rt->next;
0483
0484 kfree(s->digipeat);
0485 kfree(s);
0486 }
0487 write_unlock_bh(&ax25_route_lock);
0488 }