rose_route.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/types.h>
  12. #include <linux/socket.h>
  13. #include <linux/in.h>
  14. #include <linux/kernel.h>
  15. #include <linux/timer.h>
  16. #include <linux/string.h>
  17. #include <linux/sockios.h>
  18. #include <linux/net.h>
  19. #include <linux/slab.h>
  20. #include <net/ax25.h>
  21. #include <linux/inet.h>
  22. #include <linux/netdevice.h>
  23. #include <net/arp.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/skbuff.h>
  26. #include <net/sock.h>
  27. #include <net/tcp_states.h>
  28. #include <asm/uaccess.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/termios.h> /* For TIOCINQ/OUTQ */
  31. #include <linux/mm.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/notifier.h>
  34. #include <linux/init.h>
  35. #include <net/rose.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/export.h>
  38. static unsigned int rose_neigh_no = 1;
  39. static struct rose_node *rose_node_list;
  40. static DEFINE_SPINLOCK(rose_node_list_lock);
  41. static struct rose_neigh *rose_neigh_list;
  42. static DEFINE_SPINLOCK(rose_neigh_list_lock);
  43. static struct rose_route *rose_route_list;
  44. static DEFINE_SPINLOCK(rose_route_list_lock);
  45. struct rose_neigh *rose_loopback_neigh;
  46. /*
  47. * Add a new route to a node, and in the process add the node and the
  48. * neighbour if it is new.
  49. */
  50. static int __must_check rose_add_node(struct rose_route_struct *rose_route,
  51. struct net_device *dev)
  52. {
  53. struct rose_node *rose_node, *rose_tmpn, *rose_tmpp;
  54. struct rose_neigh *rose_neigh;
  55. int i, res = 0;
  56. spin_lock_bh(&rose_node_list_lock);
  57. spin_lock_bh(&rose_neigh_list_lock);
  58. rose_node = rose_node_list;
  59. while (rose_node != NULL) {
  60. if ((rose_node->mask == rose_route->mask) &&
  61. (rosecmpm(&rose_route->address, &rose_node->address,
  62. rose_route->mask) == 0))
  63. break;
  64. rose_node = rose_node->next;
  65. }
  66. if (rose_node != NULL && rose_node->loopback) {
  67. res = -EINVAL;
  68. goto out;
  69. }
  70. rose_neigh = rose_neigh_list;
  71. while (rose_neigh != NULL) {
  72. if (ax25cmp(&rose_route->neighbour,
  73. &rose_neigh->callsign) == 0 &&
  74. rose_neigh->dev == dev)
  75. break;
  76. rose_neigh = rose_neigh->next;
  77. }
  78. if (rose_neigh == NULL) {
  79. rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
  80. if (rose_neigh == NULL) {
  81. res = -ENOMEM;
  82. goto out;
  83. }
  84. rose_neigh->callsign = rose_route->neighbour;
  85. rose_neigh->digipeat = NULL;
  86. rose_neigh->ax25 = NULL;
  87. rose_neigh->dev = dev;
  88. rose_neigh->count = 0;
  89. rose_neigh->use = 0;
  90. rose_neigh->dce_mode = 0;
  91. rose_neigh->loopback = 0;
  92. rose_neigh->number = rose_neigh_no++;
  93. rose_neigh->restarted = 0;
  94. skb_queue_head_init(&rose_neigh->queue);
  95. init_timer(&rose_neigh->ftimer);
  96. init_timer(&rose_neigh->t0timer);
  97. if (rose_route->ndigis != 0) {
  98. rose_neigh->digipeat =
  99. kmalloc(sizeof(ax25_digi), GFP_ATOMIC);
  100. if (rose_neigh->digipeat == NULL) {
  101. kfree(rose_neigh);
  102. res = -ENOMEM;
  103. goto out;
  104. }
  105. rose_neigh->digipeat->ndigi = rose_route->ndigis;
  106. rose_neigh->digipeat->lastrepeat = -1;
  107. for (i = 0; i < rose_route->ndigis; i++) {
  108. rose_neigh->digipeat->calls[i] =
  109. rose_route->digipeaters[i];
  110. rose_neigh->digipeat->repeated[i] = 0;
  111. }
  112. }
  113. rose_neigh->next = rose_neigh_list;
  114. rose_neigh_list = rose_neigh;
  115. }
  116. /*
  117. * This is a new node to be inserted into the list. Find where it needs
  118. * to be inserted into the list, and insert it. We want to be sure
  119. * to order the list in descending order of mask size to ensure that
  120. * later when we are searching this list the first match will be the
  121. * best match.
  122. */
  123. if (rose_node == NULL) {
  124. rose_tmpn = rose_node_list;
  125. rose_tmpp = NULL;
  126. while (rose_tmpn != NULL) {
  127. if (rose_tmpn->mask > rose_route->mask) {
  128. rose_tmpp = rose_tmpn;
  129. rose_tmpn = rose_tmpn->next;
  130. } else {
  131. break;
  132. }
  133. }
  134. /* create new node */
  135. rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
  136. if (rose_node == NULL) {
  137. res = -ENOMEM;
  138. goto out;
  139. }
  140. rose_node->address = rose_route->address;
  141. rose_node->mask = rose_route->mask;
  142. rose_node->count = 1;
  143. rose_node->loopback = 0;
  144. rose_node->neighbour[0] = rose_neigh;
  145. if (rose_tmpn == NULL) {
  146. if (rose_tmpp == NULL) { /* Empty list */
  147. rose_node_list = rose_node;
  148. rose_node->next = NULL;
  149. } else {
  150. rose_tmpp->next = rose_node;
  151. rose_node->next = NULL;
  152. }
  153. } else {
  154. if (rose_tmpp == NULL) { /* 1st node */
  155. rose_node->next = rose_node_list;
  156. rose_node_list = rose_node;
  157. } else {
  158. rose_tmpp->next = rose_node;
  159. rose_node->next = rose_tmpn;
  160. }
  161. }
  162. rose_neigh->count++;
  163. goto out;
  164. }
  165. /* We have space, slot it in */
  166. if (rose_node->count < 3) {
  167. rose_node->neighbour[rose_node->count] = rose_neigh;
  168. rose_node->count++;
  169. rose_neigh->count++;
  170. }
  171. out:
  172. spin_unlock_bh(&rose_neigh_list_lock);
  173. spin_unlock_bh(&rose_node_list_lock);
  174. return res;
  175. }
  176. /*
  177. * Caller is holding rose_node_list_lock.
  178. */
  179. static void rose_remove_node(struct rose_node *rose_node)
  180. {
  181. struct rose_node *s;
  182. if ((s = rose_node_list) == rose_node) {
  183. rose_node_list = rose_node->next;
  184. kfree(rose_node);
  185. return;
  186. }
  187. while (s != NULL && s->next != NULL) {
  188. if (s->next == rose_node) {
  189. s->next = rose_node->next;
  190. kfree(rose_node);
  191. return;
  192. }
  193. s = s->next;
  194. }
  195. }
  196. /*
  197. * Caller is holding rose_neigh_list_lock.
  198. */
  199. static void rose_remove_neigh(struct rose_neigh *rose_neigh)
  200. {
  201. struct rose_neigh *s;
  202. rose_stop_ftimer(rose_neigh);
  203. rose_stop_t0timer(rose_neigh);
  204. skb_queue_purge(&rose_neigh->queue);
  205. if ((s = rose_neigh_list) == rose_neigh) {
  206. rose_neigh_list = rose_neigh->next;
  207. if (rose_neigh->ax25)
  208. ax25_cb_put(rose_neigh->ax25);
  209. kfree(rose_neigh->digipeat);
  210. kfree(rose_neigh);
  211. return;
  212. }
  213. while (s != NULL && s->next != NULL) {
  214. if (s->next == rose_neigh) {
  215. s->next = rose_neigh->next;
  216. if (rose_neigh->ax25)
  217. ax25_cb_put(rose_neigh->ax25);
  218. kfree(rose_neigh->digipeat);
  219. kfree(rose_neigh);
  220. return;
  221. }
  222. s = s->next;
  223. }
  224. }
  225. /*
  226. * Caller is holding rose_route_list_lock.
  227. */
  228. static void rose_remove_route(struct rose_route *rose_route)
  229. {
  230. struct rose_route *s;
  231. if (rose_route->neigh1 != NULL)
  232. rose_route->neigh1->use--;
  233. if (rose_route->neigh2 != NULL)
  234. rose_route->neigh2->use--;
  235. if ((s = rose_route_list) == rose_route) {
  236. rose_route_list = rose_route->next;
  237. kfree(rose_route);
  238. return;
  239. }
  240. while (s != NULL && s->next != NULL) {
  241. if (s->next == rose_route) {
  242. s->next = rose_route->next;
  243. kfree(rose_route);
  244. return;
  245. }
  246. s = s->next;
  247. }
  248. }
  249. /*
  250. * "Delete" a node. Strictly speaking remove a route to a node. The node
  251. * is only deleted if no routes are left to it.
  252. */
  253. static int rose_del_node(struct rose_route_struct *rose_route,
  254. struct net_device *dev)
  255. {
  256. struct rose_node *rose_node;
  257. struct rose_neigh *rose_neigh;
  258. int i, err = 0;
  259. spin_lock_bh(&rose_node_list_lock);
  260. spin_lock_bh(&rose_neigh_list_lock);
  261. rose_node = rose_node_list;
  262. while (rose_node != NULL) {
  263. if ((rose_node->mask == rose_route->mask) &&
  264. (rosecmpm(&rose_route->address, &rose_node->address,
  265. rose_route->mask) == 0))
  266. break;
  267. rose_node = rose_node->next;
  268. }
  269. if (rose_node == NULL || rose_node->loopback) {
  270. err = -EINVAL;
  271. goto out;
  272. }
  273. rose_neigh = rose_neigh_list;
  274. while (rose_neigh != NULL) {
  275. if (ax25cmp(&rose_route->neighbour,
  276. &rose_neigh->callsign) == 0 &&
  277. rose_neigh->dev == dev)
  278. break;
  279. rose_neigh = rose_neigh->next;
  280. }
  281. if (rose_neigh == NULL) {
  282. err = -EINVAL;
  283. goto out;
  284. }
  285. for (i = 0; i < rose_node->count; i++) {
  286. if (rose_node->neighbour[i] == rose_neigh) {
  287. rose_neigh->count--;
  288. if (rose_neigh->count == 0 && rose_neigh->use == 0)
  289. rose_remove_neigh(rose_neigh);
  290. rose_node->count--;
  291. if (rose_node->count == 0) {
  292. rose_remove_node(rose_node);
  293. } else {
  294. switch (i) {
  295. case 0:
  296. rose_node->neighbour[0] =
  297. rose_node->neighbour[1];
  298. case 1:
  299. rose_node->neighbour[1] =
  300. rose_node->neighbour[2];
  301. case 2:
  302. break;
  303. }
  304. }
  305. goto out;
  306. }
  307. }
  308. err = -EINVAL;
  309. out:
  310. spin_unlock_bh(&rose_neigh_list_lock);
  311. spin_unlock_bh(&rose_node_list_lock);
  312. return err;
  313. }
  314. /*
  315. * Add the loopback neighbour.
  316. */
  317. void rose_add_loopback_neigh(void)
  318. {
  319. struct rose_neigh *sn;
  320. rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_KERNEL);
  321. if (!rose_loopback_neigh)
  322. return;
  323. sn = rose_loopback_neigh;
  324. sn->callsign = null_ax25_address;
  325. sn->digipeat = NULL;
  326. sn->ax25 = NULL;
  327. sn->dev = NULL;
  328. sn->count = 0;
  329. sn->use = 0;
  330. sn->dce_mode = 1;
  331. sn->loopback = 1;
  332. sn->number = rose_neigh_no++;
  333. sn->restarted = 1;
  334. skb_queue_head_init(&sn->queue);
  335. init_timer(&sn->ftimer);
  336. init_timer(&sn->t0timer);
  337. spin_lock_bh(&rose_neigh_list_lock);
  338. sn->next = rose_neigh_list;
  339. rose_neigh_list = sn;
  340. spin_unlock_bh(&rose_neigh_list_lock);
  341. }
  342. /*
  343. * Add a loopback node.
  344. */
  345. int rose_add_loopback_node(rose_address *address)
  346. {
  347. struct rose_node *rose_node;
  348. int err = 0;
  349. spin_lock_bh(&rose_node_list_lock);
  350. rose_node = rose_node_list;
  351. while (rose_node != NULL) {
  352. if ((rose_node->mask == 10) &&
  353. (rosecmpm(address, &rose_node->address, 10) == 0) &&
  354. rose_node->loopback)
  355. break;
  356. rose_node = rose_node->next;
  357. }
  358. if (rose_node != NULL)
  359. goto out;
  360. if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
  361. err = -ENOMEM;
  362. goto out;
  363. }
  364. rose_node->address = *address;
  365. rose_node->mask = 10;
  366. rose_node->count = 1;
  367. rose_node->loopback = 1;
  368. rose_node->neighbour[0] = rose_loopback_neigh;
  369. /* Insert at the head of list. Address is always mask=10 */
  370. rose_node->next = rose_node_list;
  371. rose_node_list = rose_node;
  372. rose_loopback_neigh->count++;
  373. out:
  374. spin_unlock_bh(&rose_node_list_lock);
  375. return err;
  376. }
  377. /*
  378. * Delete a loopback node.
  379. */
  380. void rose_del_loopback_node(rose_address *address)
  381. {
  382. struct rose_node *rose_node;
  383. spin_lock_bh(&rose_node_list_lock);
  384. rose_node = rose_node_list;
  385. while (rose_node != NULL) {
  386. if ((rose_node->mask == 10) &&
  387. (rosecmpm(address, &rose_node->address, 10) == 0) &&
  388. rose_node->loopback)
  389. break;
  390. rose_node = rose_node->next;
  391. }
  392. if (rose_node == NULL)
  393. goto out;
  394. rose_remove_node(rose_node);
  395. rose_loopback_neigh->count--;
  396. out:
  397. spin_unlock_bh(&rose_node_list_lock);
  398. }
  399. /*
  400. * A device has been removed. Remove its routes and neighbours.
  401. */
  402. void rose_rt_device_down(struct net_device *dev)
  403. {
  404. struct rose_neigh *s, *rose_neigh;
  405. struct rose_node *t, *rose_node;
  406. int i;
  407. spin_lock_bh(&rose_node_list_lock);
  408. spin_lock_bh(&rose_neigh_list_lock);
  409. rose_neigh = rose_neigh_list;
  410. while (rose_neigh != NULL) {
  411. s = rose_neigh;
  412. rose_neigh = rose_neigh->next;
  413. if (s->dev != dev)
  414. continue;
  415. rose_node = rose_node_list;
  416. while (rose_node != NULL) {
  417. t = rose_node;
  418. rose_node = rose_node->next;
  419. for (i = 0; i < t->count; i++) {
  420. if (t->neighbour[i] != s)
  421. continue;
  422. t->count--;
  423. switch (i) {
  424. case 0:
  425. t->neighbour[0] = t->neighbour[1];
  426. case 1:
  427. t->neighbour[1] = t->neighbour[2];
  428. case 2:
  429. break;
  430. }
  431. }
  432. if (t->count <= 0)
  433. rose_remove_node(t);
  434. }
  435. rose_remove_neigh(s);
  436. }
  437. spin_unlock_bh(&rose_neigh_list_lock);
  438. spin_unlock_bh(&rose_node_list_lock);
  439. }
  440. #if 0 /* Currently unused */
  441. /*
  442. * A device has been removed. Remove its links.
  443. */
  444. void rose_route_device_down(struct net_device *dev)
  445. {
  446. struct rose_route *s, *rose_route;
  447. spin_lock_bh(&rose_route_list_lock);
  448. rose_route = rose_route_list;
  449. while (rose_route != NULL) {
  450. s = rose_route;
  451. rose_route = rose_route->next;
  452. if (s->neigh1->dev == dev || s->neigh2->dev == dev)
  453. rose_remove_route(s);
  454. }
  455. spin_unlock_bh(&rose_route_list_lock);
  456. }
  457. #endif
  458. /*
  459. * Clear all nodes and neighbours out, except for neighbours with
  460. * active connections going through them.
  461. * Do not clear loopback neighbour and nodes.
  462. */
  463. static int rose_clear_routes(void)
  464. {
  465. struct rose_neigh *s, *rose_neigh;
  466. struct rose_node *t, *rose_node;
  467. spin_lock_bh(&rose_node_list_lock);
  468. spin_lock_bh(&rose_neigh_list_lock);
  469. rose_neigh = rose_neigh_list;
  470. rose_node = rose_node_list;
  471. while (rose_node != NULL) {
  472. t = rose_node;
  473. rose_node = rose_node->next;
  474. if (!t->loopback)
  475. rose_remove_node(t);
  476. }
  477. while (rose_neigh != NULL) {
  478. s = rose_neigh;
  479. rose_neigh = rose_neigh->next;
  480. if (s->use == 0 && !s->loopback) {
  481. s->count = 0;
  482. rose_remove_neigh(s);
  483. }
  484. }
  485. spin_unlock_bh(&rose_neigh_list_lock);
  486. spin_unlock_bh(&rose_node_list_lock);
  487. return 0;
  488. }
  489. /*
  490. * Check that the device given is a valid AX.25 interface that is "up".
  491. * called with RTNL
  492. */
  493. static struct net_device *rose_ax25_dev_find(char *devname)
  494. {
  495. struct net_device *dev;
  496. if ((dev = __dev_get_by_name(&init_net, devname)) == NULL)
  497. return NULL;
  498. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
  499. return dev;
  500. return NULL;
  501. }
  502. /*
  503. * Find the first active ROSE device, usually "rose0".
  504. */
  505. struct net_device *rose_dev_first(void)
  506. {
  507. struct net_device *dev, *first = NULL;
  508. rcu_read_lock();
  509. for_each_netdev_rcu(&init_net, dev) {
  510. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
  511. if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
  512. first = dev;
  513. }
  514. rcu_read_unlock();
  515. return first;
  516. }
  517. /*
  518. * Find the ROSE device for the given address.
  519. */
  520. struct net_device *rose_dev_get(rose_address *addr)
  521. {
  522. struct net_device *dev;
  523. rcu_read_lock();
  524. for_each_netdev_rcu(&init_net, dev) {
  525. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
  526. dev_hold(dev);
  527. goto out;
  528. }
  529. }
  530. dev = NULL;
  531. out:
  532. rcu_read_unlock();
  533. return dev;
  534. }
  535. static int rose_dev_exists(rose_address *addr)
  536. {
  537. struct net_device *dev;
  538. rcu_read_lock();
  539. for_each_netdev_rcu(&init_net, dev) {
  540. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
  541. goto out;
  542. }
  543. dev = NULL;
  544. out:
  545. rcu_read_unlock();
  546. return dev != NULL;
  547. }
  548. struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
  549. {
  550. struct rose_route *rose_route;
  551. for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
  552. if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
  553. (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
  554. return rose_route;
  555. return NULL;
  556. }
  557. /*
  558. * Find a neighbour or a route given a ROSE address.
  559. */
  560. struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
  561. unsigned char *diagnostic, int route_frame)
  562. {
  563. struct rose_neigh *res = NULL;
  564. struct rose_node *node;
  565. int failed = 0;
  566. int i;
  567. if (!route_frame) spin_lock_bh(&rose_node_list_lock);
  568. for (node = rose_node_list; node != NULL; node = node->next) {
  569. if (rosecmpm(addr, &node->address, node->mask) == 0) {
  570. for (i = 0; i < node->count; i++) {
  571. if (node->neighbour[i]->restarted) {
  572. res = node->neighbour[i];
  573. goto out;
  574. }
  575. }
  576. }
  577. }
  578. if (!route_frame) { /* connect request */
  579. for (node = rose_node_list; node != NULL; node = node->next) {
  580. if (rosecmpm(addr, &node->address, node->mask) == 0) {
  581. for (i = 0; i < node->count; i++) {
  582. if (!rose_ftimer_running(node->neighbour[i])) {
  583. res = node->neighbour[i];
  584. failed = 0;
  585. goto out;
  586. }
  587. failed = 1;
  588. }
  589. }
  590. }
  591. }
  592. if (failed) {
  593. *cause = ROSE_OUT_OF_ORDER;
  594. *diagnostic = 0;
  595. } else {
  596. *cause = ROSE_NOT_OBTAINABLE;
  597. *diagnostic = 0;
  598. }
  599. out:
  600. if (!route_frame) spin_unlock_bh(&rose_node_list_lock);
  601. return res;
  602. }
  603. /*
  604. * Handle the ioctls that control the routing functions.
  605. */
  606. int rose_rt_ioctl(unsigned int cmd, void __user *arg)
  607. {
  608. struct rose_route_struct rose_route;
  609. struct net_device *dev;
  610. int err;
  611. switch (cmd) {
  612. case SIOCADDRT:
  613. if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
  614. return -EFAULT;
  615. if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
  616. return -EINVAL;
  617. if (rose_dev_exists(&rose_route.address)) /* Can't add routes to ourself */
  618. return -EINVAL;
  619. if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
  620. return -EINVAL;
  621. if (rose_route.ndigis > AX25_MAX_DIGIS)
  622. return -EINVAL;
  623. err = rose_add_node(&rose_route, dev);
  624. return err;
  625. case SIOCDELRT:
  626. if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
  627. return -EFAULT;
  628. if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
  629. return -EINVAL;
  630. err = rose_del_node(&rose_route, dev);
  631. return err;
  632. case SIOCRSCLRRT:
  633. return rose_clear_routes();
  634. default:
  635. return -EINVAL;
  636. }
  637. return 0;
  638. }
  639. static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
  640. {
  641. struct rose_route *rose_route, *s;
  642. rose_neigh->restarted = 0;
  643. rose_stop_t0timer(rose_neigh);
  644. rose_start_ftimer(rose_neigh);
  645. skb_queue_purge(&rose_neigh->queue);
  646. spin_lock_bh(&rose_route_list_lock);
  647. rose_route = rose_route_list;
  648. while (rose_route != NULL) {
  649. if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
  650. (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
  651. (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
  652. s = rose_route->next;
  653. rose_remove_route(rose_route);
  654. rose_route = s;
  655. continue;
  656. }
  657. if (rose_route->neigh1 == rose_neigh) {
  658. rose_route->neigh1->use--;
  659. rose_route->neigh1 = NULL;
  660. rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
  661. }
  662. if (rose_route->neigh2 == rose_neigh) {
  663. rose_route->neigh2->use--;
  664. rose_route->neigh2 = NULL;
  665. rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
  666. }
  667. rose_route = rose_route->next;
  668. }
  669. spin_unlock_bh(&rose_route_list_lock);
  670. }
  671. /*
  672. * A level 2 link has timed out, therefore it appears to be a poor link,
  673. * then don't use that neighbour until it is reset. Blow away all through
  674. * routes and connections using this route.
  675. */
  676. void rose_link_failed(ax25_cb *ax25, int reason)
  677. {
  678. struct rose_neigh *rose_neigh;
  679. spin_lock_bh(&rose_neigh_list_lock);
  680. rose_neigh = rose_neigh_list;
  681. while (rose_neigh != NULL) {
  682. if (rose_neigh->ax25 == ax25)
  683. break;
  684. rose_neigh = rose_neigh->next;
  685. }
  686. if (rose_neigh != NULL) {
  687. rose_neigh->ax25 = NULL;
  688. ax25_cb_put(ax25);
  689. rose_del_route_by_neigh(rose_neigh);
  690. rose_kill_by_neigh(rose_neigh);
  691. }
  692. spin_unlock_bh(&rose_neigh_list_lock);
  693. }
  694. /*
  695. * A device has been "downed" remove its link status. Blow away all
  696. * through routes and connections that use this device.
  697. */
  698. void rose_link_device_down(struct net_device *dev)
  699. {
  700. struct rose_neigh *rose_neigh;
  701. for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
  702. if (rose_neigh->dev == dev) {
  703. rose_del_route_by_neigh(rose_neigh);
  704. rose_kill_by_neigh(rose_neigh);
  705. }
  706. }
  707. }
  708. /*
  709. * Route a frame to an appropriate AX.25 connection.
  710. */
  711. int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
  712. {
  713. struct rose_neigh *rose_neigh, *new_neigh;
  714. struct rose_route *rose_route;
  715. struct rose_facilities_struct facilities;
  716. rose_address *src_addr, *dest_addr;
  717. struct sock *sk;
  718. unsigned short frametype;
  719. unsigned int lci, new_lci;
  720. unsigned char cause, diagnostic;
  721. struct net_device *dev;
  722. int res = 0;
  723. char buf[11];
  724. if (skb->len < ROSE_MIN_LEN)
  725. return res;
  726. frametype = skb->data[2];
  727. lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
  728. if (frametype == ROSE_CALL_REQUEST &&
  729. (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
  730. skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
  731. ROSE_CALL_REQ_ADDR_LEN_VAL))
  732. return res;
  733. src_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_SRC_ADDR_OFF);
  734. dest_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
  735. spin_lock_bh(&rose_neigh_list_lock);
  736. spin_lock_bh(&rose_route_list_lock);
  737. rose_neigh = rose_neigh_list;
  738. while (rose_neigh != NULL) {
  739. if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
  740. ax25->ax25_dev->dev == rose_neigh->dev)
  741. break;
  742. rose_neigh = rose_neigh->next;
  743. }
  744. if (rose_neigh == NULL) {
  745. printk("rose_route : unknown neighbour or device %s\n",
  746. ax2asc(buf, &ax25->dest_addr));
  747. goto out;
  748. }
  749. /*
  750. * Obviously the link is working, halt the ftimer.
  751. */
  752. rose_stop_ftimer(rose_neigh);
  753. /*
  754. * LCI of zero is always for us, and its always a restart
  755. * frame.
  756. */
  757. if (lci == 0) {
  758. rose_link_rx_restart(skb, rose_neigh, frametype);
  759. goto out;
  760. }
  761. /*
  762. * Find an existing socket.
  763. */
  764. if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
  765. if (frametype == ROSE_CALL_REQUEST) {
  766. struct rose_sock *rose = rose_sk(sk);
  767. /* Remove an existing unused socket */
  768. rose_clear_queues(sk);
  769. rose->cause = ROSE_NETWORK_CONGESTION;
  770. rose->diagnostic = 0;
  771. rose->neighbour->use--;
  772. rose->neighbour = NULL;
  773. rose->lci = 0;
  774. rose->state = ROSE_STATE_0;
  775. sk->sk_state = TCP_CLOSE;
  776. sk->sk_err = 0;
  777. sk->sk_shutdown |= SEND_SHUTDOWN;
  778. if (!sock_flag(sk, SOCK_DEAD)) {
  779. sk->sk_state_change(sk);
  780. sock_set_flag(sk, SOCK_DEAD);
  781. }
  782. }
  783. else {
  784. skb_reset_transport_header(skb);
  785. res = rose_process_rx_frame(sk, skb);
  786. goto out;
  787. }
  788. }
  789. /*
  790. * Is is a Call Request and is it for us ?
  791. */
  792. if (frametype == ROSE_CALL_REQUEST)
  793. if ((dev = rose_dev_get(dest_addr)) != NULL) {
  794. res = rose_rx_call_request(skb, dev, rose_neigh, lci);
  795. dev_put(dev);
  796. goto out;
  797. }
  798. if (!sysctl_rose_routing_control) {
  799. rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
  800. goto out;
  801. }
  802. /*
  803. * Route it to the next in line if we have an entry for it.
  804. */
  805. rose_route = rose_route_list;
  806. while (rose_route != NULL) {
  807. if (rose_route->lci1 == lci &&
  808. rose_route->neigh1 == rose_neigh) {
  809. if (frametype == ROSE_CALL_REQUEST) {
  810. /* F6FBB - Remove an existing unused route */
  811. rose_remove_route(rose_route);
  812. break;
  813. } else if (rose_route->neigh2 != NULL) {
  814. skb->data[0] &= 0xF0;
  815. skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
  816. skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
  817. rose_transmit_link(skb, rose_route->neigh2);
  818. if (frametype == ROSE_CLEAR_CONFIRMATION)
  819. rose_remove_route(rose_route);
  820. res = 1;
  821. goto out;
  822. } else {
  823. if (frametype == ROSE_CLEAR_CONFIRMATION)
  824. rose_remove_route(rose_route);
  825. goto out;
  826. }
  827. }
  828. if (rose_route->lci2 == lci &&
  829. rose_route->neigh2 == rose_neigh) {
  830. if (frametype == ROSE_CALL_REQUEST) {
  831. /* F6FBB - Remove an existing unused route */
  832. rose_remove_route(rose_route);
  833. break;
  834. } else if (rose_route->neigh1 != NULL) {
  835. skb->data[0] &= 0xF0;
  836. skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
  837. skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
  838. rose_transmit_link(skb, rose_route->neigh1);
  839. if (frametype == ROSE_CLEAR_CONFIRMATION)
  840. rose_remove_route(rose_route);
  841. res = 1;
  842. goto out;
  843. } else {
  844. if (frametype == ROSE_CLEAR_CONFIRMATION)
  845. rose_remove_route(rose_route);
  846. goto out;
  847. }
  848. }
  849. rose_route = rose_route->next;
  850. }
  851. /*
  852. * We know that:
  853. * 1. The frame isn't for us,
  854. * 2. It isn't "owned" by any existing route.
  855. */
  856. if (frametype != ROSE_CALL_REQUEST) { /* XXX */
  857. res = 0;
  858. goto out;
  859. }
  860. memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
  861. if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
  862. skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
  863. &facilities)) {
  864. rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
  865. goto out;
  866. }
  867. /*
  868. * Check for routing loops.
  869. */
  870. rose_route = rose_route_list;
  871. while (rose_route != NULL) {
  872. if (rose_route->rand == facilities.rand &&
  873. rosecmp(src_addr, &rose_route->src_addr) == 0 &&
  874. ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
  875. ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
  876. rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
  877. goto out;
  878. }
  879. rose_route = rose_route->next;
  880. }
  881. if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic, 1)) == NULL) {
  882. rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
  883. goto out;
  884. }
  885. if ((new_lci = rose_new_lci(new_neigh)) == 0) {
  886. rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
  887. goto out;
  888. }
  889. if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
  890. rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
  891. goto out;
  892. }
  893. rose_route->lci1 = lci;
  894. rose_route->src_addr = *src_addr;
  895. rose_route->dest_addr = *dest_addr;
  896. rose_route->src_call = facilities.dest_call;
  897. rose_route->dest_call = facilities.source_call;
  898. rose_route->rand = facilities.rand;
  899. rose_route->neigh1 = rose_neigh;
  900. rose_route->lci2 = new_lci;
  901. rose_route->neigh2 = new_neigh;
  902. rose_route->neigh1->use++;
  903. rose_route->neigh2->use++;
  904. rose_route->next = rose_route_list;
  905. rose_route_list = rose_route;
  906. skb->data[0] &= 0xF0;
  907. skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
  908. skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
  909. rose_transmit_link(skb, rose_route->neigh2);
  910. res = 1;
  911. out:
  912. spin_unlock_bh(&rose_route_list_lock);
  913. spin_unlock_bh(&rose_neigh_list_lock);
  914. return res;
  915. }
  916. #ifdef CONFIG_PROC_FS
  917. static void *rose_node_start(struct seq_file *seq, loff_t *pos)
  918. __acquires(rose_node_list_lock)
  919. {
  920. struct rose_node *rose_node;
  921. int i = 1;
  922. spin_lock_bh(&rose_node_list_lock);
  923. if (*pos == 0)
  924. return SEQ_START_TOKEN;
  925. for (rose_node = rose_node_list; rose_node && i < *pos;
  926. rose_node = rose_node->next, ++i);
  927. return (i == *pos) ? rose_node : NULL;
  928. }
  929. static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
  930. {
  931. ++*pos;
  932. return (v == SEQ_START_TOKEN) ? rose_node_list
  933. : ((struct rose_node *)v)->next;
  934. }
  935. static void rose_node_stop(struct seq_file *seq, void *v)
  936. __releases(rose_node_list_lock)
  937. {
  938. spin_unlock_bh(&rose_node_list_lock);
  939. }
  940. static int rose_node_show(struct seq_file *seq, void *v)
  941. {
  942. char rsbuf[11];
  943. int i;
  944. if (v == SEQ_START_TOKEN)
  945. seq_puts(seq, "address mask n neigh neigh neigh\n");
  946. else {
  947. const struct rose_node *rose_node = v;
  948. /* if (rose_node->loopback) {
  949. seq_printf(seq, "%-10s %04d 1 loopback\n",
  950. rose2asc(rsbuf, &rose_node->address),
  951. rose_node->mask);
  952. } else { */
  953. seq_printf(seq, "%-10s %04d %d",
  954. rose2asc(rsbuf, &rose_node->address),
  955. rose_node->mask,
  956. rose_node->count);
  957. for (i = 0; i < rose_node->count; i++)
  958. seq_printf(seq, " %05d",
  959. rose_node->neighbour[i]->number);
  960. seq_puts(seq, "\n");
  961. /* } */
  962. }
  963. return 0;
  964. }
  965. static const struct seq_operations rose_node_seqops = {
  966. .start = rose_node_start,
  967. .next = rose_node_next,
  968. .stop = rose_node_stop,
  969. .show = rose_node_show,
  970. };
  971. static int rose_nodes_open(struct inode *inode, struct file *file)
  972. {
  973. return seq_open(file, &rose_node_seqops);
  974. }
  975. const struct file_operations rose_nodes_fops = {
  976. .owner = THIS_MODULE,
  977. .open = rose_nodes_open,
  978. .read = seq_read,
  979. .llseek = seq_lseek,
  980. .release = seq_release,
  981. };
  982. static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
  983. __acquires(rose_neigh_list_lock)
  984. {
  985. struct rose_neigh *rose_neigh;
  986. int i = 1;
  987. spin_lock_bh(&rose_neigh_list_lock);
  988. if (*pos == 0)
  989. return SEQ_START_TOKEN;
  990. for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
  991. rose_neigh = rose_neigh->next, ++i);
  992. return (i == *pos) ? rose_neigh : NULL;
  993. }
  994. static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
  995. {
  996. ++*pos;
  997. return (v == SEQ_START_TOKEN) ? rose_neigh_list
  998. : ((struct rose_neigh *)v)->next;
  999. }
  1000. static void rose_neigh_stop(struct seq_file *seq, void *v)
  1001. __releases(rose_neigh_list_lock)
  1002. {
  1003. spin_unlock_bh(&rose_neigh_list_lock);
  1004. }
  1005. static int rose_neigh_show(struct seq_file *seq, void *v)
  1006. {
  1007. char buf[11];
  1008. int i;
  1009. if (v == SEQ_START_TOKEN)
  1010. seq_puts(seq,
  1011. "addr callsign dev count use mode restart t0 tf digipeaters\n");
  1012. else {
  1013. struct rose_neigh *rose_neigh = v;
  1014. /* if (!rose_neigh->loopback) { */
  1015. seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
  1016. rose_neigh->number,
  1017. (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
  1018. rose_neigh->dev ? rose_neigh->dev->name : "???",
  1019. rose_neigh->count,
  1020. rose_neigh->use,
  1021. (rose_neigh->dce_mode) ? "DCE" : "DTE",
  1022. (rose_neigh->restarted) ? "yes" : "no",
  1023. ax25_display_timer(&rose_neigh->t0timer) / HZ,
  1024. ax25_display_timer(&rose_neigh->ftimer) / HZ);
  1025. if (rose_neigh->digipeat != NULL) {
  1026. for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
  1027. seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
  1028. }
  1029. seq_puts(seq, "\n");
  1030. }
  1031. return 0;
  1032. }
  1033. static const struct seq_operations rose_neigh_seqops = {
  1034. .start = rose_neigh_start,
  1035. .next = rose_neigh_next,
  1036. .stop = rose_neigh_stop,
  1037. .show = rose_neigh_show,
  1038. };
  1039. static int rose_neigh_open(struct inode *inode, struct file *file)
  1040. {
  1041. return seq_open(file, &rose_neigh_seqops);
  1042. }
  1043. const struct file_operations rose_neigh_fops = {
  1044. .owner = THIS_MODULE,
  1045. .open = rose_neigh_open,
  1046. .read = seq_read,
  1047. .llseek = seq_lseek,
  1048. .release = seq_release,
  1049. };
  1050. static void *rose_route_start(struct seq_file *seq, loff_t *pos)
  1051. __acquires(rose_route_list_lock)
  1052. {
  1053. struct rose_route *rose_route;
  1054. int i = 1;
  1055. spin_lock_bh(&rose_route_list_lock);
  1056. if (*pos == 0)
  1057. return SEQ_START_TOKEN;
  1058. for (rose_route = rose_route_list; rose_route && i < *pos;
  1059. rose_route = rose_route->next, ++i);
  1060. return (i == *pos) ? rose_route : NULL;
  1061. }
  1062. static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
  1063. {
  1064. ++*pos;
  1065. return (v == SEQ_START_TOKEN) ? rose_route_list
  1066. : ((struct rose_route *)v)->next;
  1067. }
  1068. static void rose_route_stop(struct seq_file *seq, void *v)
  1069. __releases(rose_route_list_lock)
  1070. {
  1071. spin_unlock_bh(&rose_route_list_lock);
  1072. }
  1073. static int rose_route_show(struct seq_file *seq, void *v)
  1074. {
  1075. char buf[11], rsbuf[11];
  1076. if (v == SEQ_START_TOKEN)
  1077. seq_puts(seq,
  1078. "lci address callsign neigh <-> lci address callsign neigh\n");
  1079. else {
  1080. struct rose_route *rose_route = v;
  1081. if (rose_route->neigh1)
  1082. seq_printf(seq,
  1083. "%3.3X %-10s %-9s %05d ",
  1084. rose_route->lci1,
  1085. rose2asc(rsbuf, &rose_route->src_addr),
  1086. ax2asc(buf, &rose_route->src_call),
  1087. rose_route->neigh1->number);
  1088. else
  1089. seq_puts(seq,
  1090. "000 * * 00000 ");
  1091. if (rose_route->neigh2)
  1092. seq_printf(seq,
  1093. "%3.3X %-10s %-9s %05d\n",
  1094. rose_route->lci2,
  1095. rose2asc(rsbuf, &rose_route->dest_addr),
  1096. ax2asc(buf, &rose_route->dest_call),
  1097. rose_route->neigh2->number);
  1098. else
  1099. seq_puts(seq,
  1100. "000 * * 00000\n");
  1101. }
  1102. return 0;
  1103. }
  1104. static const struct seq_operations rose_route_seqops = {
  1105. .start = rose_route_start,
  1106. .next = rose_route_next,
  1107. .stop = rose_route_stop,
  1108. .show = rose_route_show,
  1109. };
  1110. static int rose_route_open(struct inode *inode, struct file *file)
  1111. {
  1112. return seq_open(file, &rose_route_seqops);
  1113. }
  1114. const struct file_operations rose_routes_fops = {
  1115. .owner = THIS_MODULE,
  1116. .open = rose_route_open,
  1117. .read = seq_read,
  1118. .llseek = seq_lseek,
  1119. .release = seq_release,
  1120. };
  1121. #endif /* CONFIG_PROC_FS */
  1122. /*
  1123. * Release all memory associated with ROSE routing structures.
  1124. */
  1125. void __exit rose_rt_free(void)
  1126. {
  1127. struct rose_neigh *s, *rose_neigh = rose_neigh_list;
  1128. struct rose_node *t, *rose_node = rose_node_list;
  1129. struct rose_route *u, *rose_route = rose_route_list;
  1130. while (rose_neigh != NULL) {
  1131. s = rose_neigh;
  1132. rose_neigh = rose_neigh->next;
  1133. rose_remove_neigh(s);
  1134. }
  1135. while (rose_node != NULL) {
  1136. t = rose_node;
  1137. rose_node = rose_node->next;
  1138. rose_remove_node(t);
  1139. }
  1140. while (rose_route != NULL) {
  1141. u = rose_route;
  1142. rose_route = rose_route->next;
  1143. rose_remove_route(u);
  1144. }
  1145. }