ax25_route.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
  5. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  6. * Copyright (C) Steven Whitehouse GW7RRM (stevew@acm.org)
  7. * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
  8. * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de)
  9. * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr)
  10. */
  11. #include <linux/capability.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/socket.h>
  15. #include <linux/timer.h>
  16. #include <linux/in.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/sockios.h>
  21. #include <linux/net.h>
  22. #include <linux/slab.h>
  23. #include <net/ax25.h>
  24. #include <linux/inet.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/spinlock.h>
  29. #include <net/sock.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/mm.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/init.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/export.h>
  37. static ax25_route *ax25_route_list;
  38. DEFINE_RWLOCK(ax25_route_lock);
  39. void ax25_rt_device_down(struct net_device *dev)
  40. {
  41. ax25_route *s, *t, *ax25_rt;
  42. write_lock_bh(&ax25_route_lock);
  43. ax25_rt = ax25_route_list;
  44. while (ax25_rt != NULL) {
  45. s = ax25_rt;
  46. ax25_rt = ax25_rt->next;
  47. if (s->dev == dev) {
  48. if (ax25_route_list == s) {
  49. ax25_route_list = s->next;
  50. kfree(s->digipeat);
  51. kfree(s);
  52. } else {
  53. for (t = ax25_route_list; t != NULL; t = t->next) {
  54. if (t->next == s) {
  55. t->next = s->next;
  56. kfree(s->digipeat);
  57. kfree(s);
  58. break;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. write_unlock_bh(&ax25_route_lock);
  65. }
  66. static int __must_check ax25_rt_add(struct ax25_routes_struct *route)
  67. {
  68. ax25_route *ax25_rt;
  69. ax25_dev *ax25_dev;
  70. int i;
  71. if (route->digi_count > AX25_MAX_DIGIS)
  72. return -EINVAL;
  73. ax25_dev = ax25_addr_ax25dev(&route->port_addr);
  74. if (!ax25_dev)
  75. return -EINVAL;
  76. write_lock_bh(&ax25_route_lock);
  77. ax25_rt = ax25_route_list;
  78. while (ax25_rt != NULL) {
  79. if (ax25cmp(&ax25_rt->callsign, &route->dest_addr) == 0 &&
  80. ax25_rt->dev == ax25_dev->dev) {
  81. kfree(ax25_rt->digipeat);
  82. ax25_rt->digipeat = NULL;
  83. if (route->digi_count != 0) {
  84. if ((ax25_rt->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
  85. write_unlock_bh(&ax25_route_lock);
  86. ax25_dev_put(ax25_dev);
  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_bh(&ax25_route_lock);
  97. ax25_dev_put(ax25_dev);
  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. ax25_dev_put(ax25_dev);
  105. return -ENOMEM;
  106. }
  107. refcount_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. ax25_dev_put(ax25_dev);
  117. return -ENOMEM;
  118. }
  119. ax25_rt->digipeat->lastrepeat = -1;
  120. ax25_rt->digipeat->ndigi = route->digi_count;
  121. for (i = 0; i < route->digi_count; i++) {
  122. ax25_rt->digipeat->repeated[i] = 0;
  123. ax25_rt->digipeat->calls[i] = route->digi_addr[i];
  124. }
  125. }
  126. ax25_rt->next = ax25_route_list;
  127. ax25_route_list = ax25_rt;
  128. write_unlock_bh(&ax25_route_lock);
  129. ax25_dev_put(ax25_dev);
  130. return 0;
  131. }
  132. void __ax25_put_route(ax25_route *ax25_rt)
  133. {
  134. kfree(ax25_rt->digipeat);
  135. kfree(ax25_rt);
  136. }
  137. static int ax25_rt_del(struct ax25_routes_struct *route)
  138. {
  139. ax25_route *s, *t, *ax25_rt;
  140. ax25_dev *ax25_dev;
  141. if ((ax25_dev = ax25_addr_ax25dev(&route->port_addr)) == NULL)
  142. return -EINVAL;
  143. write_lock_bh(&ax25_route_lock);
  144. ax25_rt = ax25_route_list;
  145. while (ax25_rt != NULL) {
  146. s = ax25_rt;
  147. ax25_rt = ax25_rt->next;
  148. if (s->dev == ax25_dev->dev &&
  149. ax25cmp(&route->dest_addr, &s->callsign) == 0) {
  150. if (ax25_route_list == s) {
  151. ax25_route_list = s->next;
  152. ax25_put_route(s);
  153. } else {
  154. for (t = ax25_route_list; t != NULL; t = t->next) {
  155. if (t->next == s) {
  156. t->next = s->next;
  157. ax25_put_route(s);
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. }
  164. write_unlock_bh(&ax25_route_lock);
  165. ax25_dev_put(ax25_dev);
  166. return 0;
  167. }
  168. static int ax25_rt_opt(struct ax25_route_opt_struct *rt_option)
  169. {
  170. ax25_route *ax25_rt;
  171. ax25_dev *ax25_dev;
  172. int err = 0;
  173. if ((ax25_dev = ax25_addr_ax25dev(&rt_option->port_addr)) == NULL)
  174. return -EINVAL;
  175. write_lock_bh(&ax25_route_lock);
  176. ax25_rt = ax25_route_list;
  177. while (ax25_rt != NULL) {
  178. if (ax25_rt->dev == ax25_dev->dev &&
  179. ax25cmp(&rt_option->dest_addr, &ax25_rt->callsign) == 0) {
  180. switch (rt_option->cmd) {
  181. case AX25_SET_RT_IPMODE:
  182. switch (rt_option->arg) {
  183. case ' ':
  184. case 'D':
  185. case 'V':
  186. ax25_rt->ip_mode = rt_option->arg;
  187. break;
  188. default:
  189. err = -EINVAL;
  190. goto out;
  191. }
  192. break;
  193. default:
  194. err = -EINVAL;
  195. goto out;
  196. }
  197. }
  198. ax25_rt = ax25_rt->next;
  199. }
  200. out:
  201. write_unlock_bh(&ax25_route_lock);
  202. ax25_dev_put(ax25_dev);
  203. return err;
  204. }
  205. int ax25_rt_ioctl(unsigned int cmd, void __user *arg)
  206. {
  207. struct ax25_route_opt_struct rt_option;
  208. struct ax25_routes_struct route;
  209. switch (cmd) {
  210. case SIOCADDRT:
  211. if (copy_from_user(&route, arg, sizeof(route)))
  212. return -EFAULT;
  213. return ax25_rt_add(&route);
  214. case SIOCDELRT:
  215. if (copy_from_user(&route, arg, sizeof(route)))
  216. return -EFAULT;
  217. return ax25_rt_del(&route);
  218. case SIOCAX25OPTRT:
  219. if (copy_from_user(&rt_option, arg, sizeof(rt_option)))
  220. return -EFAULT;
  221. return ax25_rt_opt(&rt_option);
  222. default:
  223. return -EINVAL;
  224. }
  225. }
  226. #ifdef CONFIG_PROC_FS
  227. static void *ax25_rt_seq_start(struct seq_file *seq, loff_t *pos)
  228. __acquires(ax25_route_lock)
  229. {
  230. struct ax25_route *ax25_rt;
  231. int i = 1;
  232. read_lock(&ax25_route_lock);
  233. if (*pos == 0)
  234. return SEQ_START_TOKEN;
  235. for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
  236. if (i == *pos)
  237. return ax25_rt;
  238. ++i;
  239. }
  240. return NULL;
  241. }
  242. static void *ax25_rt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  243. {
  244. ++*pos;
  245. return (v == SEQ_START_TOKEN) ? ax25_route_list :
  246. ((struct ax25_route *) v)->next;
  247. }
  248. static void ax25_rt_seq_stop(struct seq_file *seq, void *v)
  249. __releases(ax25_route_lock)
  250. {
  251. read_unlock(&ax25_route_lock);
  252. }
  253. static int ax25_rt_seq_show(struct seq_file *seq, void *v)
  254. {
  255. char buf[11];
  256. if (v == SEQ_START_TOKEN)
  257. seq_puts(seq, "callsign dev mode digipeaters\n");
  258. else {
  259. struct ax25_route *ax25_rt = v;
  260. const char *callsign;
  261. int i;
  262. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
  263. callsign = "default";
  264. else
  265. callsign = ax2asc(buf, &ax25_rt->callsign);
  266. seq_printf(seq, "%-9s %-4s",
  267. callsign,
  268. ax25_rt->dev ? ax25_rt->dev->name : "???");
  269. switch (ax25_rt->ip_mode) {
  270. case 'V':
  271. seq_puts(seq, " vc");
  272. break;
  273. case 'D':
  274. seq_puts(seq, " dg");
  275. break;
  276. default:
  277. seq_puts(seq, " *");
  278. break;
  279. }
  280. if (ax25_rt->digipeat != NULL)
  281. for (i = 0; i < ax25_rt->digipeat->ndigi; i++)
  282. seq_printf(seq, " %s",
  283. ax2asc(buf, &ax25_rt->digipeat->calls[i]));
  284. seq_puts(seq, "\n");
  285. }
  286. return 0;
  287. }
  288. const struct seq_operations ax25_rt_seqops = {
  289. .start = ax25_rt_seq_start,
  290. .next = ax25_rt_seq_next,
  291. .stop = ax25_rt_seq_stop,
  292. .show = ax25_rt_seq_show,
  293. };
  294. #endif
  295. /*
  296. * Find AX.25 route
  297. *
  298. * Only routes with a reference count of zero can be destroyed.
  299. * Must be called with ax25_route_lock read locked.
  300. */
  301. ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
  302. {
  303. ax25_route *ax25_spe_rt = NULL;
  304. ax25_route *ax25_def_rt = NULL;
  305. ax25_route *ax25_rt;
  306. /*
  307. * Bind to the physical interface we heard them on, or the default
  308. * route if none is found;
  309. */
  310. for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
  311. if (dev == NULL) {
  312. if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev != NULL)
  313. ax25_spe_rt = ax25_rt;
  314. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev != NULL)
  315. ax25_def_rt = ax25_rt;
  316. } else {
  317. if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev == dev)
  318. ax25_spe_rt = ax25_rt;
  319. if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
  320. ax25_def_rt = ax25_rt;
  321. }
  322. }
  323. ax25_rt = ax25_def_rt;
  324. if (ax25_spe_rt != NULL)
  325. ax25_rt = ax25_spe_rt;
  326. return ax25_rt;
  327. }
  328. /*
  329. * Adjust path: If you specify a default route and want to connect
  330. * a target on the digipeater path but w/o having a special route
  331. * set before, the path has to be truncated from your target on.
  332. */
  333. static inline void ax25_adjust_path(ax25_address *addr, ax25_digi *digipeat)
  334. {
  335. int k;
  336. for (k = 0; k < digipeat->ndigi; k++) {
  337. if (ax25cmp(addr, &digipeat->calls[k]) == 0)
  338. break;
  339. }
  340. digipeat->ndigi = k;
  341. }
  342. /*
  343. * Find which interface to use.
  344. */
  345. int ax25_rt_autobind(ax25_cb *ax25, ax25_address *addr)
  346. {
  347. ax25_uid_assoc *user;
  348. ax25_route *ax25_rt;
  349. int err = 0;
  350. ax25_route_lock_use();
  351. ax25_rt = ax25_get_route(addr, NULL);
  352. if (!ax25_rt) {
  353. ax25_route_lock_unuse();
  354. return -EHOSTUNREACH;
  355. }
  356. if ((ax25->ax25_dev = ax25_dev_ax25dev(ax25_rt->dev)) == NULL) {
  357. err = -EHOSTUNREACH;
  358. goto put;
  359. }
  360. user = ax25_findbyuid(current_euid());
  361. if (user) {
  362. ax25->source_addr = user->call;
  363. ax25_uid_put(user);
  364. } else {
  365. if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
  366. err = -EPERM;
  367. goto put;
  368. }
  369. ax25->source_addr = *(ax25_address *)ax25->ax25_dev->dev->dev_addr;
  370. }
  371. if (ax25_rt->digipeat != NULL) {
  372. ax25->digipeat = kmemdup(ax25_rt->digipeat, sizeof(ax25_digi),
  373. GFP_ATOMIC);
  374. if (ax25->digipeat == NULL) {
  375. err = -ENOMEM;
  376. goto put;
  377. }
  378. ax25_adjust_path(addr, ax25->digipeat);
  379. }
  380. if (ax25->sk != NULL) {
  381. local_bh_disable();
  382. bh_lock_sock(ax25->sk);
  383. sock_reset_flag(ax25->sk, SOCK_ZAPPED);
  384. bh_unlock_sock(ax25->sk);
  385. local_bh_enable();
  386. }
  387. put:
  388. ax25_route_lock_unuse();
  389. return err;
  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. consume_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_bh(&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_bh(&ax25_route_lock);
  426. }