ax25_route.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. 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 __must_check 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->refcount, 1);
  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. void __ax25_put_route(ax25_route *ax25_rt)
  129. {
  130. kfree(ax25_rt->digipeat);
  131. kfree(ax25_rt);
  132. }
  133. static int ax25_rt_del(struct ax25_routes_struct *route)
  134. {
  135. ax25_route *s, *t, *ax25_rt;
  136. ax25_dev *ax25_dev;
  137. if ((ax25_dev = ax25_addr_ax25dev(&route->port_addr)) == NULL)
  138. return -EINVAL;
  139. write_lock(&ax25_route_lock);
  140. ax25_rt = ax25_route_list;
  141. while (ax25_rt != NULL) {
  142. s = ax25_rt;
  143. ax25_rt = ax25_rt->next;
  144. if (s->dev == ax25_dev->dev &&
  145. ax25cmp(&route->dest_addr, &s->callsign) == 0) {
  146. if (ax25_route_list == s) {
  147. ax25_route_list = s->next;
  148. ax25_put_route(s);
  149. } else {
  150. for (t = ax25_route_list; t != NULL; t = t->next) {
  151. if (t->next == s) {
  152. t->next = s->next;
  153. ax25_put_route(s);
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. write_unlock(&ax25_route_lock);
  161. return 0;
  162. }
  163. static int ax25_rt_opt(struct ax25_route_opt_struct *rt_option)
  164. {
  165. ax25_route *ax25_rt;
  166. ax25_dev *ax25_dev;
  167. int err = 0;
  168. if ((ax25_dev = ax25_addr_ax25dev(&rt_option->port_addr)) == NULL)
  169. return -EINVAL;
  170. write_lock(&ax25_route_lock);
  171. ax25_rt = ax25_route_list;
  172. while (ax25_rt != NULL) {
  173. if (ax25_rt->dev == ax25_dev->dev &&
  174. ax25cmp(&rt_option->dest_addr, &ax25_rt->callsign) == 0) {
  175. switch (rt_option->cmd) {
  176. case AX25_SET_RT_IPMODE:
  177. switch (rt_option->arg) {
  178. case ' ':
  179. case 'D':
  180. case 'V':
  181. ax25_rt->ip_mode = rt_option->arg;
  182. break;
  183. default:
  184. err = -EINVAL;
  185. goto out;
  186. }
  187. break;
  188. default:
  189. err = -EINVAL;
  190. goto out;
  191. }
  192. }
  193. ax25_rt = ax25_rt->next;
  194. }
  195. out:
  196. write_unlock(&ax25_route_lock);
  197. return err;
  198. }
  199. int ax25_rt_ioctl(unsigned int cmd, void __user *arg)
  200. {
  201. struct ax25_route_opt_struct rt_option;
  202. struct ax25_routes_struct route;
  203. switch (cmd) {
  204. case SIOCADDRT:
  205. if (copy_from_user(&route, arg, sizeof(route)))
  206. return -EFAULT;
  207. return ax25_rt_add(&route);
  208. case SIOCDELRT:
  209. if (copy_from_user(&route, arg, sizeof(route)))
  210. return -EFAULT;
  211. return ax25_rt_del(&route);
  212. case SIOCAX25OPTRT:
  213. if (copy_from_user(&rt_option, arg, sizeof(rt_option)))
  214. return -EFAULT;
  215. return ax25_rt_opt(&rt_option);
  216. default:
  217. return -EINVAL;
  218. }
  219. }
  220. #ifdef CONFIG_PROC_FS
  221. static void *ax25_rt_seq_start(struct seq_file *seq, loff_t *pos)
  222. {
  223. struct ax25_route *ax25_rt;
  224. int i = 1;
  225. read_lock(&ax25_route_lock);
  226. if (*pos == 0)
  227. return SEQ_START_TOKEN;
  228. for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
  229. if (i == *pos)
  230. return ax25_rt;
  231. ++i;
  232. }
  233. return NULL;
  234. }
  235. static void *ax25_rt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  236. {
  237. ++*pos;
  238. return (v == SEQ_START_TOKEN) ? ax25_route_list :
  239. ((struct ax25_route *) v)->next;
  240. }
  241. static void ax25_rt_seq_stop(struct seq_file *seq, void *v)
  242. {
  243. read_unlock(&ax25_route_lock);
  244. }
  245. static int ax25_rt_seq_show(struct seq_file *seq, void *v)
  246. {
  247. char buf[11];
  248. if (v == SEQ_START_TOKEN)
  249. seq_puts(seq, "callsign dev mode digipeaters\n");
  250. else {
  251. struct ax25_route *ax25_rt = v;
  252. const char *callsign;
  253. int i;
  254. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
  255. callsign = "default";
  256. else
  257. callsign = ax2asc(buf, &ax25_rt->callsign);
  258. seq_printf(seq, "%-9s %-4s",
  259. callsign,
  260. ax25_rt->dev ? ax25_rt->dev->name : "???");
  261. switch (ax25_rt->ip_mode) {
  262. case 'V':
  263. seq_puts(seq, " vc");
  264. break;
  265. case 'D':
  266. seq_puts(seq, " dg");
  267. break;
  268. default:
  269. seq_puts(seq, " *");
  270. break;
  271. }
  272. if (ax25_rt->digipeat != NULL)
  273. for (i = 0; i < ax25_rt->digipeat->ndigi; i++)
  274. seq_printf(seq, " %s",
  275. ax2asc(buf, &ax25_rt->digipeat->calls[i]));
  276. seq_puts(seq, "\n");
  277. }
  278. return 0;
  279. }
  280. static struct seq_operations ax25_rt_seqops = {
  281. .start = ax25_rt_seq_start,
  282. .next = ax25_rt_seq_next,
  283. .stop = ax25_rt_seq_stop,
  284. .show = ax25_rt_seq_show,
  285. };
  286. static int ax25_rt_info_open(struct inode *inode, struct file *file)
  287. {
  288. return seq_open(file, &ax25_rt_seqops);
  289. }
  290. const struct file_operations ax25_route_fops = {
  291. .owner = THIS_MODULE,
  292. .open = ax25_rt_info_open,
  293. .read = seq_read,
  294. .llseek = seq_lseek,
  295. .release = seq_release,
  296. };
  297. #endif
  298. /*
  299. * Find AX.25 route
  300. *
  301. * Only routes with a reference count of zero can be destroyed.
  302. */
  303. ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
  304. {
  305. ax25_route *ax25_spe_rt = NULL;
  306. ax25_route *ax25_def_rt = NULL;
  307. ax25_route *ax25_rt;
  308. read_lock(&ax25_route_lock);
  309. /*
  310. * Bind to the physical interface we heard them on, or the default
  311. * route if none is found;
  312. */
  313. for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
  314. if (dev == NULL) {
  315. if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev != NULL)
  316. ax25_spe_rt = ax25_rt;
  317. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev != NULL)
  318. ax25_def_rt = ax25_rt;
  319. } else {
  320. if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev == dev)
  321. ax25_spe_rt = ax25_rt;
  322. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
  323. ax25_def_rt = ax25_rt;
  324. }
  325. }
  326. ax25_rt = ax25_def_rt;
  327. if (ax25_spe_rt != NULL)
  328. ax25_rt = ax25_spe_rt;
  329. if (ax25_rt != NULL)
  330. ax25_hold_route(ax25_rt);
  331. read_unlock(&ax25_route_lock);
  332. return ax25_rt;
  333. }
  334. /*
  335. * Adjust path: If you specify a default route and want to connect
  336. * a target on the digipeater path but w/o having a special route
  337. * set before, the path has to be truncated from your target on.
  338. */
  339. static inline void ax25_adjust_path(ax25_address *addr, ax25_digi *digipeat)
  340. {
  341. int k;
  342. for (k = 0; k < digipeat->ndigi; k++) {
  343. if (ax25cmp(addr, &digipeat->calls[k]) == 0)
  344. break;
  345. }
  346. digipeat->ndigi = k;
  347. }
  348. /*
  349. * Find which interface to use.
  350. */
  351. int ax25_rt_autobind(ax25_cb *ax25, ax25_address *addr)
  352. {
  353. ax25_uid_assoc *user;
  354. ax25_route *ax25_rt;
  355. int err;
  356. if ((ax25_rt = ax25_get_route(addr, NULL)) == NULL)
  357. return -EHOSTUNREACH;
  358. if ((ax25->ax25_dev = ax25_dev_ax25dev(ax25_rt->dev)) == NULL) {
  359. err = -EHOSTUNREACH;
  360. goto put;
  361. }
  362. user = ax25_findbyuid(current->euid);
  363. if (user) {
  364. ax25->source_addr = user->call;
  365. ax25_uid_put(user);
  366. } else {
  367. if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
  368. err = -EPERM;
  369. goto put;
  370. }
  371. ax25->source_addr = *(ax25_address *)ax25->ax25_dev->dev->dev_addr;
  372. }
  373. if (ax25_rt->digipeat != NULL) {
  374. ax25->digipeat = kmemdup(ax25_rt->digipeat, sizeof(ax25_digi),
  375. GFP_ATOMIC);
  376. if (ax25->digipeat == NULL) {
  377. err = -ENOMEM;
  378. goto put;
  379. }
  380. ax25_adjust_path(addr, ax25->digipeat);
  381. }
  382. if (ax25->sk != NULL) {
  383. bh_lock_sock(ax25->sk);
  384. sock_reset_flag(ax25->sk, SOCK_ZAPPED);
  385. bh_unlock_sock(ax25->sk);
  386. }
  387. put:
  388. ax25_put_route(ax25_rt);
  389. return 0;
  390. }
  391. struct sk_buff *ax25_rt_build_path(struct sk_buff *skb, ax25_address *src,
  392. ax25_address *dest, ax25_digi *digi)
  393. {
  394. struct sk_buff *skbn;
  395. unsigned char *bp;
  396. int len;
  397. len = digi->ndigi * AX25_ADDR_LEN;
  398. if (skb_headroom(skb) < len) {
  399. if ((skbn = skb_realloc_headroom(skb, len)) == NULL) {
  400. printk(KERN_CRIT "AX.25: ax25_dg_build_path - out of memory\n");
  401. return NULL;
  402. }
  403. if (skb->sk != NULL)
  404. skb_set_owner_w(skbn, skb->sk);
  405. kfree_skb(skb);
  406. skb = skbn;
  407. }
  408. bp = skb_push(skb, len);
  409. ax25_addr_build(bp, src, dest, digi, AX25_COMMAND, AX25_MODULUS);
  410. return skb;
  411. }
  412. /*
  413. * Free all memory associated with routing structures.
  414. */
  415. void __exit ax25_rt_free(void)
  416. {
  417. ax25_route *s, *ax25_rt = ax25_route_list;
  418. write_lock(&ax25_route_lock);
  419. while (ax25_rt != NULL) {
  420. s = ax25_rt;
  421. ax25_rt = ax25_rt->next;
  422. kfree(s->digipeat);
  423. kfree(s);
  424. }
  425. write_unlock(&ax25_route_lock);
  426. }