ax25_route.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. static ax25_route *ax25_route_list;
  39. static DEFINE_RWLOCK(ax25_route_lock);
  40. static ax25_route *ax25_get_route(ax25_address *, struct net_device *);
  41. void ax25_rt_device_down(struct net_device *dev)
  42. {
  43. ax25_route *s, *t, *ax25_rt;
  44. write_lock(&ax25_route_lock);
  45. ax25_rt = ax25_route_list;
  46. while (ax25_rt != NULL) {
  47. s = ax25_rt;
  48. ax25_rt = ax25_rt->next;
  49. if (s->dev == dev) {
  50. if (ax25_route_list == s) {
  51. ax25_route_list = s->next;
  52. if (s->digipeat != NULL)
  53. kfree(s->digipeat);
  54. kfree(s);
  55. } else {
  56. for (t = ax25_route_list; t != NULL; t = t->next) {
  57. if (t->next == s) {
  58. t->next = s->next;
  59. if (s->digipeat != NULL)
  60. kfree(s->digipeat);
  61. kfree(s);
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. write_unlock(&ax25_route_lock);
  69. }
  70. static int ax25_rt_add(struct ax25_routes_struct *route)
  71. {
  72. ax25_route *ax25_rt;
  73. ax25_dev *ax25_dev;
  74. int i;
  75. if ((ax25_dev = ax25_addr_ax25dev(&route->port_addr)) == NULL)
  76. return -EINVAL;
  77. if (route->digi_count > AX25_MAX_DIGIS)
  78. return -EINVAL;
  79. write_lock(&ax25_route_lock);
  80. ax25_rt = ax25_route_list;
  81. while (ax25_rt != NULL) {
  82. if (ax25cmp(&ax25_rt->callsign, &route->dest_addr) == 0 &&
  83. ax25_rt->dev == ax25_dev->dev) {
  84. if (ax25_rt->digipeat != NULL) {
  85. kfree(ax25_rt->digipeat);
  86. ax25_rt->digipeat = NULL;
  87. }
  88. if (route->digi_count != 0) {
  89. if ((ax25_rt->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
  90. write_unlock(&ax25_route_lock);
  91. return -ENOMEM;
  92. }
  93. ax25_rt->digipeat->lastrepeat = -1;
  94. ax25_rt->digipeat->ndigi = route->digi_count;
  95. for (i = 0; i < route->digi_count; i++) {
  96. ax25_rt->digipeat->repeated[i] = 0;
  97. ax25_rt->digipeat->calls[i] = route->digi_addr[i];
  98. }
  99. }
  100. write_unlock(&ax25_route_lock);
  101. return 0;
  102. }
  103. ax25_rt = ax25_rt->next;
  104. }
  105. if ((ax25_rt = kmalloc(sizeof(ax25_route), GFP_ATOMIC)) == NULL) {
  106. write_unlock(&ax25_route_lock);
  107. return -ENOMEM;
  108. }
  109. atomic_set(&ax25_rt->ref, 0);
  110. ax25_rt->callsign = route->dest_addr;
  111. ax25_rt->dev = ax25_dev->dev;
  112. ax25_rt->digipeat = NULL;
  113. ax25_rt->ip_mode = ' ';
  114. if (route->digi_count != 0) {
  115. if ((ax25_rt->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
  116. write_unlock(&ax25_route_lock);
  117. kfree(ax25_rt);
  118. return -ENOMEM;
  119. }
  120. ax25_rt->digipeat->lastrepeat = -1;
  121. ax25_rt->digipeat->ndigi = route->digi_count;
  122. for (i = 0; i < route->digi_count; i++) {
  123. ax25_rt->digipeat->repeated[i] = 0;
  124. ax25_rt->digipeat->calls[i] = route->digi_addr[i];
  125. }
  126. }
  127. ax25_rt->next = ax25_route_list;
  128. ax25_route_list = ax25_rt;
  129. write_unlock(&ax25_route_lock);
  130. return 0;
  131. }
  132. static void ax25_rt_destroy(ax25_route *ax25_rt)
  133. {
  134. if (atomic_read(&ax25_rt->ref) == 0) {
  135. if (ax25_rt->digipeat != NULL)
  136. kfree(ax25_rt->digipeat);
  137. kfree(ax25_rt);
  138. return;
  139. }
  140. /*
  141. * Uh... Route is still in use; we can't yet destroy it. Retry later.
  142. */
  143. init_timer(&ax25_rt->timer);
  144. ax25_rt->timer.data = (unsigned long) ax25_rt;
  145. ax25_rt->timer.function = (void *) ax25_rt_destroy;
  146. ax25_rt->timer.expires = jiffies + 5 * HZ;
  147. add_timer(&ax25_rt->timer);
  148. }
  149. static int ax25_rt_del(struct ax25_routes_struct *route)
  150. {
  151. ax25_route *s, *t, *ax25_rt;
  152. ax25_dev *ax25_dev;
  153. if ((ax25_dev = ax25_addr_ax25dev(&route->port_addr)) == NULL)
  154. return -EINVAL;
  155. write_lock(&ax25_route_lock);
  156. ax25_rt = ax25_route_list;
  157. while (ax25_rt != NULL) {
  158. s = ax25_rt;
  159. ax25_rt = ax25_rt->next;
  160. if (s->dev == ax25_dev->dev &&
  161. ax25cmp(&route->dest_addr, &s->callsign) == 0) {
  162. if (ax25_route_list == s) {
  163. ax25_route_list = s->next;
  164. ax25_rt_destroy(s);
  165. } else {
  166. for (t = ax25_route_list; t != NULL; t = t->next) {
  167. if (t->next == s) {
  168. t->next = s->next;
  169. ax25_rt_destroy(s);
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. write_unlock(&ax25_route_lock);
  177. return 0;
  178. }
  179. static int ax25_rt_opt(struct ax25_route_opt_struct *rt_option)
  180. {
  181. ax25_route *ax25_rt;
  182. ax25_dev *ax25_dev;
  183. int err = 0;
  184. if ((ax25_dev = ax25_addr_ax25dev(&rt_option->port_addr)) == NULL)
  185. return -EINVAL;
  186. write_lock(&ax25_route_lock);
  187. ax25_rt = ax25_route_list;
  188. while (ax25_rt != NULL) {
  189. if (ax25_rt->dev == ax25_dev->dev &&
  190. ax25cmp(&rt_option->dest_addr, &ax25_rt->callsign) == 0) {
  191. switch (rt_option->cmd) {
  192. case AX25_SET_RT_IPMODE:
  193. switch (rt_option->arg) {
  194. case ' ':
  195. case 'D':
  196. case 'V':
  197. ax25_rt->ip_mode = rt_option->arg;
  198. break;
  199. default:
  200. err = -EINVAL;
  201. goto out;
  202. }
  203. break;
  204. default:
  205. err = -EINVAL;
  206. goto out;
  207. }
  208. }
  209. ax25_rt = ax25_rt->next;
  210. }
  211. out:
  212. write_unlock(&ax25_route_lock);
  213. return err;
  214. }
  215. int ax25_rt_ioctl(unsigned int cmd, void __user *arg)
  216. {
  217. struct ax25_route_opt_struct rt_option;
  218. struct ax25_routes_struct route;
  219. switch (cmd) {
  220. case SIOCADDRT:
  221. if (copy_from_user(&route, arg, sizeof(route)))
  222. return -EFAULT;
  223. return ax25_rt_add(&route);
  224. case SIOCDELRT:
  225. if (copy_from_user(&route, arg, sizeof(route)))
  226. return -EFAULT;
  227. return ax25_rt_del(&route);
  228. case SIOCAX25OPTRT:
  229. if (copy_from_user(&rt_option, arg, sizeof(rt_option)))
  230. return -EFAULT;
  231. return ax25_rt_opt(&rt_option);
  232. default:
  233. return -EINVAL;
  234. }
  235. }
  236. #ifdef CONFIG_PROC_FS
  237. static void *ax25_rt_seq_start(struct seq_file *seq, loff_t *pos)
  238. {
  239. struct ax25_route *ax25_rt;
  240. int i = 1;
  241. read_lock(&ax25_route_lock);
  242. if (*pos == 0)
  243. return SEQ_START_TOKEN;
  244. for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
  245. if (i == *pos)
  246. return ax25_rt;
  247. ++i;
  248. }
  249. return NULL;
  250. }
  251. static void *ax25_rt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  252. {
  253. ++*pos;
  254. return (v == SEQ_START_TOKEN) ? ax25_route_list :
  255. ((struct ax25_route *) v)->next;
  256. }
  257. static void ax25_rt_seq_stop(struct seq_file *seq, void *v)
  258. {
  259. read_unlock(&ax25_route_lock);
  260. }
  261. static int ax25_rt_seq_show(struct seq_file *seq, void *v)
  262. {
  263. if (v == SEQ_START_TOKEN)
  264. seq_puts(seq, "callsign dev mode digipeaters\n");
  265. else {
  266. struct ax25_route *ax25_rt = v;
  267. const char *callsign;
  268. int i;
  269. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
  270. callsign = "default";
  271. else
  272. callsign = ax2asc(&ax25_rt->callsign);
  273. seq_printf(seq, "%-9s %-4s",
  274. callsign,
  275. ax25_rt->dev ? ax25_rt->dev->name : "???");
  276. switch (ax25_rt->ip_mode) {
  277. case 'V':
  278. seq_puts(seq, " vc");
  279. break;
  280. case 'D':
  281. seq_puts(seq, " dg");
  282. break;
  283. default:
  284. seq_puts(seq, " *");
  285. break;
  286. }
  287. if (ax25_rt->digipeat != NULL)
  288. for (i = 0; i < ax25_rt->digipeat->ndigi; i++)
  289. seq_printf(seq, " %s", ax2asc(&ax25_rt->digipeat->calls[i]));
  290. seq_puts(seq, "\n");
  291. }
  292. return 0;
  293. }
  294. static struct seq_operations ax25_rt_seqops = {
  295. .start = ax25_rt_seq_start,
  296. .next = ax25_rt_seq_next,
  297. .stop = ax25_rt_seq_stop,
  298. .show = ax25_rt_seq_show,
  299. };
  300. static int ax25_rt_info_open(struct inode *inode, struct file *file)
  301. {
  302. return seq_open(file, &ax25_rt_seqops);
  303. }
  304. struct file_operations ax25_route_fops = {
  305. .owner = THIS_MODULE,
  306. .open = ax25_rt_info_open,
  307. .read = seq_read,
  308. .llseek = seq_lseek,
  309. .release = seq_release,
  310. };
  311. #endif
  312. /*
  313. * Find AX.25 route
  314. *
  315. * Only routes with a refernce rout of zero can be destroyed.
  316. */
  317. static ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
  318. {
  319. ax25_route *ax25_spe_rt = NULL;
  320. ax25_route *ax25_def_rt = NULL;
  321. ax25_route *ax25_rt;
  322. read_lock(&ax25_route_lock);
  323. /*
  324. * Bind to the physical interface we heard them on, or the default
  325. * route if none is found;
  326. */
  327. for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
  328. if (dev == NULL) {
  329. if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev != NULL)
  330. ax25_spe_rt = ax25_rt;
  331. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev != NULL)
  332. ax25_def_rt = ax25_rt;
  333. } else {
  334. if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev == dev)
  335. ax25_spe_rt = ax25_rt;
  336. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
  337. ax25_def_rt = ax25_rt;
  338. }
  339. }
  340. ax25_rt = ax25_def_rt;
  341. if (ax25_spe_rt != NULL)
  342. ax25_rt = ax25_spe_rt;
  343. if (ax25_rt != NULL)
  344. atomic_inc(&ax25_rt->ref);
  345. read_unlock(&ax25_route_lock);
  346. return ax25_rt;
  347. }
  348. /*
  349. * Adjust path: If you specify a default route and want to connect
  350. * a target on the digipeater path but w/o having a special route
  351. * set before, the path has to be truncated from your target on.
  352. */
  353. static inline void ax25_adjust_path(ax25_address *addr, ax25_digi *digipeat)
  354. {
  355. int k;
  356. for (k = 0; k < digipeat->ndigi; k++) {
  357. if (ax25cmp(addr, &digipeat->calls[k]) == 0)
  358. break;
  359. }
  360. digipeat->ndigi = k;
  361. }
  362. /*
  363. * Find which interface to use.
  364. */
  365. int ax25_rt_autobind(ax25_cb *ax25, ax25_address *addr)
  366. {
  367. ax25_route *ax25_rt;
  368. ax25_address *call;
  369. int err;
  370. if ((ax25_rt = ax25_get_route(addr, NULL)) == NULL)
  371. return -EHOSTUNREACH;
  372. if ((ax25->ax25_dev = ax25_dev_ax25dev(ax25_rt->dev)) == NULL) {
  373. err = -EHOSTUNREACH;
  374. goto put;
  375. }
  376. if ((call = ax25_findbyuid(current->euid)) == NULL) {
  377. if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
  378. err = -EPERM;
  379. goto put;
  380. }
  381. call = (ax25_address *)ax25->ax25_dev->dev->dev_addr;
  382. }
  383. ax25->source_addr = *call;
  384. if (ax25_rt->digipeat != NULL) {
  385. if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
  386. err = -ENOMEM;
  387. goto put;
  388. }
  389. memcpy(ax25->digipeat, ax25_rt->digipeat, sizeof(ax25_digi));
  390. ax25_adjust_path(addr, ax25->digipeat);
  391. }
  392. if (ax25->sk != NULL) {
  393. bh_lock_sock(ax25->sk);
  394. sock_reset_flag(ax25->sk, SOCK_ZAPPED);
  395. bh_unlock_sock(ax25->sk);
  396. }
  397. put:
  398. ax25_put_route(ax25_rt);
  399. return 0;
  400. }
  401. ax25_route *ax25_rt_find_route(ax25_route * route, ax25_address *addr,
  402. struct net_device *dev)
  403. {
  404. ax25_route *ax25_rt;
  405. if ((ax25_rt = ax25_get_route(addr, dev)))
  406. return ax25_rt;
  407. route->next = NULL;
  408. atomic_set(&route->ref, 1);
  409. route->callsign = *addr;
  410. route->dev = dev;
  411. route->digipeat = NULL;
  412. route->ip_mode = ' ';
  413. return route;
  414. }
  415. struct sk_buff *ax25_rt_build_path(struct sk_buff *skb, ax25_address *src,
  416. ax25_address *dest, ax25_digi *digi)
  417. {
  418. struct sk_buff *skbn;
  419. unsigned char *bp;
  420. int len;
  421. len = digi->ndigi * AX25_ADDR_LEN;
  422. if (skb_headroom(skb) < len) {
  423. if ((skbn = skb_realloc_headroom(skb, len)) == NULL) {
  424. printk(KERN_CRIT "AX.25: ax25_dg_build_path - out of memory\n");
  425. return NULL;
  426. }
  427. if (skb->sk != NULL)
  428. skb_set_owner_w(skbn, skb->sk);
  429. kfree_skb(skb);
  430. skb = skbn;
  431. }
  432. bp = skb_push(skb, len);
  433. ax25_addr_build(bp, src, dest, digi, AX25_COMMAND, AX25_MODULUS);
  434. return skb;
  435. }
  436. /*
  437. * Free all memory associated with routing structures.
  438. */
  439. void __exit ax25_rt_free(void)
  440. {
  441. ax25_route *s, *ax25_rt = ax25_route_list;
  442. write_lock(&ax25_route_lock);
  443. while (ax25_rt != NULL) {
  444. s = ax25_rt;
  445. ax25_rt = ax25_rt->next;
  446. if (s->digipeat != NULL)
  447. kfree(s->digipeat);
  448. kfree(s);
  449. }
  450. write_unlock(&ax25_route_lock);
  451. }