ax25_route.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. char buf[11];
  264. if (v == SEQ_START_TOKEN)
  265. seq_puts(seq, "callsign dev mode digipeaters\n");
  266. else {
  267. struct ax25_route *ax25_rt = v;
  268. const char *callsign;
  269. int i;
  270. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
  271. callsign = "default";
  272. else
  273. callsign = ax2asc(buf, &ax25_rt->callsign);
  274. seq_printf(seq, "%-9s %-4s",
  275. callsign,
  276. ax25_rt->dev ? ax25_rt->dev->name : "???");
  277. switch (ax25_rt->ip_mode) {
  278. case 'V':
  279. seq_puts(seq, " vc");
  280. break;
  281. case 'D':
  282. seq_puts(seq, " dg");
  283. break;
  284. default:
  285. seq_puts(seq, " *");
  286. break;
  287. }
  288. if (ax25_rt->digipeat != NULL)
  289. for (i = 0; i < ax25_rt->digipeat->ndigi; i++)
  290. seq_printf(seq, " %s",
  291. ax2asc(buf, &ax25_rt->digipeat->calls[i]));
  292. seq_puts(seq, "\n");
  293. }
  294. return 0;
  295. }
  296. static struct seq_operations ax25_rt_seqops = {
  297. .start = ax25_rt_seq_start,
  298. .next = ax25_rt_seq_next,
  299. .stop = ax25_rt_seq_stop,
  300. .show = ax25_rt_seq_show,
  301. };
  302. static int ax25_rt_info_open(struct inode *inode, struct file *file)
  303. {
  304. return seq_open(file, &ax25_rt_seqops);
  305. }
  306. struct file_operations ax25_route_fops = {
  307. .owner = THIS_MODULE,
  308. .open = ax25_rt_info_open,
  309. .read = seq_read,
  310. .llseek = seq_lseek,
  311. .release = seq_release,
  312. };
  313. #endif
  314. /*
  315. * Find AX.25 route
  316. *
  317. * Only routes with a refernce rout of zero can be destroyed.
  318. */
  319. static ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
  320. {
  321. ax25_route *ax25_spe_rt = NULL;
  322. ax25_route *ax25_def_rt = NULL;
  323. ax25_route *ax25_rt;
  324. read_lock(&ax25_route_lock);
  325. /*
  326. * Bind to the physical interface we heard them on, or the default
  327. * route if none is found;
  328. */
  329. for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
  330. if (dev == NULL) {
  331. if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev != NULL)
  332. ax25_spe_rt = ax25_rt;
  333. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev != NULL)
  334. ax25_def_rt = ax25_rt;
  335. } else {
  336. if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev == dev)
  337. ax25_spe_rt = ax25_rt;
  338. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
  339. ax25_def_rt = ax25_rt;
  340. }
  341. }
  342. ax25_rt = ax25_def_rt;
  343. if (ax25_spe_rt != NULL)
  344. ax25_rt = ax25_spe_rt;
  345. if (ax25_rt != NULL)
  346. atomic_inc(&ax25_rt->ref);
  347. read_unlock(&ax25_route_lock);
  348. return ax25_rt;
  349. }
  350. /*
  351. * Adjust path: If you specify a default route and want to connect
  352. * a target on the digipeater path but w/o having a special route
  353. * set before, the path has to be truncated from your target on.
  354. */
  355. static inline void ax25_adjust_path(ax25_address *addr, ax25_digi *digipeat)
  356. {
  357. int k;
  358. for (k = 0; k < digipeat->ndigi; k++) {
  359. if (ax25cmp(addr, &digipeat->calls[k]) == 0)
  360. break;
  361. }
  362. digipeat->ndigi = k;
  363. }
  364. /*
  365. * Find which interface to use.
  366. */
  367. int ax25_rt_autobind(ax25_cb *ax25, ax25_address *addr)
  368. {
  369. ax25_uid_assoc *user;
  370. ax25_route *ax25_rt;
  371. int err;
  372. if ((ax25_rt = ax25_get_route(addr, NULL)) == NULL)
  373. return -EHOSTUNREACH;
  374. if ((ax25->ax25_dev = ax25_dev_ax25dev(ax25_rt->dev)) == NULL) {
  375. err = -EHOSTUNREACH;
  376. goto put;
  377. }
  378. user = ax25_findbyuid(current->euid);
  379. if (user) {
  380. ax25->source_addr = user->call;
  381. ax25_uid_put(user);
  382. } else {
  383. if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
  384. err = -EPERM;
  385. goto put;
  386. }
  387. ax25->source_addr = *(ax25_address *)ax25->ax25_dev->dev->dev_addr;
  388. }
  389. if (ax25_rt->digipeat != NULL) {
  390. if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
  391. err = -ENOMEM;
  392. goto put;
  393. }
  394. memcpy(ax25->digipeat, ax25_rt->digipeat, sizeof(ax25_digi));
  395. ax25_adjust_path(addr, ax25->digipeat);
  396. }
  397. if (ax25->sk != NULL) {
  398. bh_lock_sock(ax25->sk);
  399. sock_reset_flag(ax25->sk, SOCK_ZAPPED);
  400. bh_unlock_sock(ax25->sk);
  401. }
  402. put:
  403. ax25_put_route(ax25_rt);
  404. return 0;
  405. }
  406. ax25_route *ax25_rt_find_route(ax25_route * route, ax25_address *addr,
  407. struct net_device *dev)
  408. {
  409. ax25_route *ax25_rt;
  410. if ((ax25_rt = ax25_get_route(addr, dev)))
  411. return ax25_rt;
  412. route->next = NULL;
  413. atomic_set(&route->ref, 1);
  414. route->callsign = *addr;
  415. route->dev = dev;
  416. route->digipeat = NULL;
  417. route->ip_mode = ' ';
  418. return route;
  419. }
  420. struct sk_buff *ax25_rt_build_path(struct sk_buff *skb, ax25_address *src,
  421. ax25_address *dest, ax25_digi *digi)
  422. {
  423. struct sk_buff *skbn;
  424. unsigned char *bp;
  425. int len;
  426. len = digi->ndigi * AX25_ADDR_LEN;
  427. if (skb_headroom(skb) < len) {
  428. if ((skbn = skb_realloc_headroom(skb, len)) == NULL) {
  429. printk(KERN_CRIT "AX.25: ax25_dg_build_path - out of memory\n");
  430. return NULL;
  431. }
  432. if (skb->sk != NULL)
  433. skb_set_owner_w(skbn, skb->sk);
  434. kfree_skb(skb);
  435. skb = skbn;
  436. }
  437. bp = skb_push(skb, len);
  438. ax25_addr_build(bp, src, dest, digi, AX25_COMMAND, AX25_MODULUS);
  439. return skb;
  440. }
  441. /*
  442. * Free all memory associated with routing structures.
  443. */
  444. void __exit ax25_rt_free(void)
  445. {
  446. ax25_route *s, *ax25_rt = ax25_route_list;
  447. write_lock(&ax25_route_lock);
  448. while (ax25_rt != NULL) {
  449. s = ax25_rt;
  450. ax25_rt = ax25_rt->next;
  451. if (s->digipeat != NULL)
  452. kfree(s->digipeat);
  453. kfree(s);
  454. }
  455. write_unlock(&ax25_route_lock);
  456. }