ax25_route.c 11 KB

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