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