ax25_route.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 <linux/uaccess.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. #include <linux/export.h>
  40. static ax25_route *ax25_route_list;
  41. 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. refcount_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. 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. #endif
  290. /*
  291. * Find AX.25 route
  292. *
  293. * Only routes with a reference count of zero can be destroyed.
  294. * Must be called with ax25_route_lock read locked.
  295. */
  296. ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
  297. {
  298. ax25_route *ax25_spe_rt = NULL;
  299. ax25_route *ax25_def_rt = NULL;
  300. ax25_route *ax25_rt;
  301. /*
  302. * Bind to the physical interface we heard them on, or the default
  303. * route if none is found;
  304. */
  305. for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
  306. if (dev == NULL) {
  307. if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev != NULL)
  308. ax25_spe_rt = ax25_rt;
  309. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev != NULL)
  310. ax25_def_rt = ax25_rt;
  311. } else {
  312. if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev == dev)
  313. ax25_spe_rt = ax25_rt;
  314. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
  315. ax25_def_rt = ax25_rt;
  316. }
  317. }
  318. ax25_rt = ax25_def_rt;
  319. if (ax25_spe_rt != NULL)
  320. ax25_rt = ax25_spe_rt;
  321. return ax25_rt;
  322. }
  323. /*
  324. * Adjust path: If you specify a default route and want to connect
  325. * a target on the digipeater path but w/o having a special route
  326. * set before, the path has to be truncated from your target on.
  327. */
  328. static inline void ax25_adjust_path(ax25_address *addr, ax25_digi *digipeat)
  329. {
  330. int k;
  331. for (k = 0; k < digipeat->ndigi; k++) {
  332. if (ax25cmp(addr, &digipeat->calls[k]) == 0)
  333. break;
  334. }
  335. digipeat->ndigi = k;
  336. }
  337. /*
  338. * Find which interface to use.
  339. */
  340. int ax25_rt_autobind(ax25_cb *ax25, ax25_address *addr)
  341. {
  342. ax25_uid_assoc *user;
  343. ax25_route *ax25_rt;
  344. int err = 0;
  345. ax25_route_lock_use();
  346. ax25_rt = ax25_get_route(addr, NULL);
  347. if (!ax25_rt) {
  348. ax25_route_lock_unuse();
  349. return -EHOSTUNREACH;
  350. }
  351. if ((ax25->ax25_dev = ax25_dev_ax25dev(ax25_rt->dev)) == NULL) {
  352. err = -EHOSTUNREACH;
  353. goto put;
  354. }
  355. user = ax25_findbyuid(current_euid());
  356. if (user) {
  357. ax25->source_addr = user->call;
  358. ax25_uid_put(user);
  359. } else {
  360. if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
  361. err = -EPERM;
  362. goto put;
  363. }
  364. ax25->source_addr = *(ax25_address *)ax25->ax25_dev->dev->dev_addr;
  365. }
  366. if (ax25_rt->digipeat != NULL) {
  367. ax25->digipeat = kmemdup(ax25_rt->digipeat, sizeof(ax25_digi),
  368. GFP_ATOMIC);
  369. if (ax25->digipeat == NULL) {
  370. err = -ENOMEM;
  371. goto put;
  372. }
  373. ax25_adjust_path(addr, ax25->digipeat);
  374. }
  375. if (ax25->sk != NULL) {
  376. local_bh_disable();
  377. bh_lock_sock(ax25->sk);
  378. sock_reset_flag(ax25->sk, SOCK_ZAPPED);
  379. bh_unlock_sock(ax25->sk);
  380. local_bh_enable();
  381. }
  382. put:
  383. ax25_route_lock_unuse();
  384. return err;
  385. }
  386. struct sk_buff *ax25_rt_build_path(struct sk_buff *skb, ax25_address *src,
  387. ax25_address *dest, ax25_digi *digi)
  388. {
  389. struct sk_buff *skbn;
  390. unsigned char *bp;
  391. int len;
  392. len = digi->ndigi * AX25_ADDR_LEN;
  393. if (skb_headroom(skb) < len) {
  394. if ((skbn = skb_realloc_headroom(skb, len)) == NULL) {
  395. printk(KERN_CRIT "AX.25: ax25_dg_build_path - out of memory\n");
  396. return NULL;
  397. }
  398. if (skb->sk != NULL)
  399. skb_set_owner_w(skbn, skb->sk);
  400. consume_skb(skb);
  401. skb = skbn;
  402. }
  403. bp = skb_push(skb, len);
  404. ax25_addr_build(bp, src, dest, digi, AX25_COMMAND, AX25_MODULUS);
  405. return skb;
  406. }
  407. /*
  408. * Free all memory associated with routing structures.
  409. */
  410. void __exit ax25_rt_free(void)
  411. {
  412. ax25_route *s, *ax25_rt = ax25_route_list;
  413. write_lock_bh(&ax25_route_lock);
  414. while (ax25_rt != NULL) {
  415. s = ax25_rt;
  416. ax25_rt = ax25_rt->next;
  417. kfree(s->digipeat);
  418. kfree(s);
  419. }
  420. write_unlock_bh(&ax25_route_lock);
  421. }