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