ax25_route.c 12 KB

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