ax25_route.c 11 KB

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