rose_route.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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 <linux/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. timer_setup(&rose_neigh->ftimer, NULL, 0);
  96. timer_setup(&rose_neigh->t0timer, NULL, 0);
  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. /* fall through */
  299. case 1:
  300. rose_node->neighbour[1] =
  301. rose_node->neighbour[2];
  302. case 2:
  303. break;
  304. }
  305. }
  306. goto out;
  307. }
  308. }
  309. err = -EINVAL;
  310. out:
  311. spin_unlock_bh(&rose_neigh_list_lock);
  312. spin_unlock_bh(&rose_node_list_lock);
  313. return err;
  314. }
  315. /*
  316. * Add the loopback neighbour.
  317. */
  318. void rose_add_loopback_neigh(void)
  319. {
  320. struct rose_neigh *sn;
  321. rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_KERNEL);
  322. if (!rose_loopback_neigh)
  323. return;
  324. sn = rose_loopback_neigh;
  325. sn->callsign = null_ax25_address;
  326. sn->digipeat = NULL;
  327. sn->ax25 = NULL;
  328. sn->dev = NULL;
  329. sn->count = 0;
  330. sn->use = 0;
  331. sn->dce_mode = 1;
  332. sn->loopback = 1;
  333. sn->number = rose_neigh_no++;
  334. sn->restarted = 1;
  335. skb_queue_head_init(&sn->queue);
  336. timer_setup(&sn->ftimer, NULL, 0);
  337. timer_setup(&sn->t0timer, NULL, 0);
  338. spin_lock_bh(&rose_neigh_list_lock);
  339. sn->next = rose_neigh_list;
  340. rose_neigh_list = sn;
  341. spin_unlock_bh(&rose_neigh_list_lock);
  342. }
  343. /*
  344. * Add a loopback node.
  345. */
  346. int rose_add_loopback_node(rose_address *address)
  347. {
  348. struct rose_node *rose_node;
  349. int err = 0;
  350. spin_lock_bh(&rose_node_list_lock);
  351. rose_node = rose_node_list;
  352. while (rose_node != NULL) {
  353. if ((rose_node->mask == 10) &&
  354. (rosecmpm(address, &rose_node->address, 10) == 0) &&
  355. rose_node->loopback)
  356. break;
  357. rose_node = rose_node->next;
  358. }
  359. if (rose_node != NULL)
  360. goto out;
  361. if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
  362. err = -ENOMEM;
  363. goto out;
  364. }
  365. rose_node->address = *address;
  366. rose_node->mask = 10;
  367. rose_node->count = 1;
  368. rose_node->loopback = 1;
  369. rose_node->neighbour[0] = rose_loopback_neigh;
  370. /* Insert at the head of list. Address is always mask=10 */
  371. rose_node->next = rose_node_list;
  372. rose_node_list = rose_node;
  373. rose_loopback_neigh->count++;
  374. out:
  375. spin_unlock_bh(&rose_node_list_lock);
  376. return err;
  377. }
  378. /*
  379. * Delete a loopback node.
  380. */
  381. void rose_del_loopback_node(rose_address *address)
  382. {
  383. struct rose_node *rose_node;
  384. spin_lock_bh(&rose_node_list_lock);
  385. rose_node = rose_node_list;
  386. while (rose_node != NULL) {
  387. if ((rose_node->mask == 10) &&
  388. (rosecmpm(address, &rose_node->address, 10) == 0) &&
  389. rose_node->loopback)
  390. break;
  391. rose_node = rose_node->next;
  392. }
  393. if (rose_node == NULL)
  394. goto out;
  395. rose_remove_node(rose_node);
  396. rose_loopback_neigh->count--;
  397. out:
  398. spin_unlock_bh(&rose_node_list_lock);
  399. }
  400. /*
  401. * A device has been removed. Remove its routes and neighbours.
  402. */
  403. void rose_rt_device_down(struct net_device *dev)
  404. {
  405. struct rose_neigh *s, *rose_neigh;
  406. struct rose_node *t, *rose_node;
  407. int i;
  408. spin_lock_bh(&rose_node_list_lock);
  409. spin_lock_bh(&rose_neigh_list_lock);
  410. rose_neigh = rose_neigh_list;
  411. while (rose_neigh != NULL) {
  412. s = rose_neigh;
  413. rose_neigh = rose_neigh->next;
  414. if (s->dev != dev)
  415. continue;
  416. rose_node = rose_node_list;
  417. while (rose_node != NULL) {
  418. t = rose_node;
  419. rose_node = rose_node->next;
  420. for (i = 0; i < t->count; i++) {
  421. if (t->neighbour[i] != s)
  422. continue;
  423. t->count--;
  424. switch (i) {
  425. case 0:
  426. t->neighbour[0] = t->neighbour[1];
  427. /* fall through */
  428. case 1:
  429. t->neighbour[1] = t->neighbour[2];
  430. case 2:
  431. break;
  432. }
  433. }
  434. if (t->count <= 0)
  435. rose_remove_node(t);
  436. }
  437. rose_remove_neigh(s);
  438. }
  439. spin_unlock_bh(&rose_neigh_list_lock);
  440. spin_unlock_bh(&rose_node_list_lock);
  441. }
  442. #if 0 /* Currently unused */
  443. /*
  444. * A device has been removed. Remove its links.
  445. */
  446. void rose_route_device_down(struct net_device *dev)
  447. {
  448. struct rose_route *s, *rose_route;
  449. spin_lock_bh(&rose_route_list_lock);
  450. rose_route = rose_route_list;
  451. while (rose_route != NULL) {
  452. s = rose_route;
  453. rose_route = rose_route->next;
  454. if (s->neigh1->dev == dev || s->neigh2->dev == dev)
  455. rose_remove_route(s);
  456. }
  457. spin_unlock_bh(&rose_route_list_lock);
  458. }
  459. #endif
  460. /*
  461. * Clear all nodes and neighbours out, except for neighbours with
  462. * active connections going through them.
  463. * Do not clear loopback neighbour and nodes.
  464. */
  465. static int rose_clear_routes(void)
  466. {
  467. struct rose_neigh *s, *rose_neigh;
  468. struct rose_node *t, *rose_node;
  469. spin_lock_bh(&rose_node_list_lock);
  470. spin_lock_bh(&rose_neigh_list_lock);
  471. rose_neigh = rose_neigh_list;
  472. rose_node = rose_node_list;
  473. while (rose_node != NULL) {
  474. t = rose_node;
  475. rose_node = rose_node->next;
  476. if (!t->loopback)
  477. rose_remove_node(t);
  478. }
  479. while (rose_neigh != NULL) {
  480. s = rose_neigh;
  481. rose_neigh = rose_neigh->next;
  482. if (s->use == 0 && !s->loopback) {
  483. s->count = 0;
  484. rose_remove_neigh(s);
  485. }
  486. }
  487. spin_unlock_bh(&rose_neigh_list_lock);
  488. spin_unlock_bh(&rose_node_list_lock);
  489. return 0;
  490. }
  491. /*
  492. * Check that the device given is a valid AX.25 interface that is "up".
  493. * called with RTNL
  494. */
  495. static struct net_device *rose_ax25_dev_find(char *devname)
  496. {
  497. struct net_device *dev;
  498. if ((dev = __dev_get_by_name(&init_net, devname)) == NULL)
  499. return NULL;
  500. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
  501. return dev;
  502. return NULL;
  503. }
  504. /*
  505. * Find the first active ROSE device, usually "rose0".
  506. */
  507. struct net_device *rose_dev_first(void)
  508. {
  509. struct net_device *dev, *first = NULL;
  510. rcu_read_lock();
  511. for_each_netdev_rcu(&init_net, dev) {
  512. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
  513. if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
  514. first = dev;
  515. }
  516. rcu_read_unlock();
  517. return first;
  518. }
  519. /*
  520. * Find the ROSE device for the given address.
  521. */
  522. struct net_device *rose_dev_get(rose_address *addr)
  523. {
  524. struct net_device *dev;
  525. rcu_read_lock();
  526. for_each_netdev_rcu(&init_net, dev) {
  527. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
  528. dev_hold(dev);
  529. goto out;
  530. }
  531. }
  532. dev = NULL;
  533. out:
  534. rcu_read_unlock();
  535. return dev;
  536. }
  537. static int rose_dev_exists(rose_address *addr)
  538. {
  539. struct net_device *dev;
  540. rcu_read_lock();
  541. for_each_netdev_rcu(&init_net, dev) {
  542. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
  543. goto out;
  544. }
  545. dev = NULL;
  546. out:
  547. rcu_read_unlock();
  548. return dev != NULL;
  549. }
  550. struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
  551. {
  552. struct rose_route *rose_route;
  553. for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
  554. if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
  555. (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
  556. return rose_route;
  557. return NULL;
  558. }
  559. /*
  560. * Find a neighbour or a route given a ROSE address.
  561. */
  562. struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
  563. unsigned char *diagnostic, int route_frame)
  564. {
  565. struct rose_neigh *res = NULL;
  566. struct rose_node *node;
  567. int failed = 0;
  568. int i;
  569. if (!route_frame) spin_lock_bh(&rose_node_list_lock);
  570. for (node = rose_node_list; node != NULL; node = node->next) {
  571. if (rosecmpm(addr, &node->address, node->mask) == 0) {
  572. for (i = 0; i < node->count; i++) {
  573. if (node->neighbour[i]->restarted) {
  574. res = node->neighbour[i];
  575. goto out;
  576. }
  577. }
  578. }
  579. }
  580. if (!route_frame) { /* connect request */
  581. for (node = rose_node_list; node != NULL; node = node->next) {
  582. if (rosecmpm(addr, &node->address, node->mask) == 0) {
  583. for (i = 0; i < node->count; i++) {
  584. if (!rose_ftimer_running(node->neighbour[i])) {
  585. res = node->neighbour[i];
  586. failed = 0;
  587. goto out;
  588. }
  589. failed = 1;
  590. }
  591. }
  592. }
  593. }
  594. if (failed) {
  595. *cause = ROSE_OUT_OF_ORDER;
  596. *diagnostic = 0;
  597. } else {
  598. *cause = ROSE_NOT_OBTAINABLE;
  599. *diagnostic = 0;
  600. }
  601. out:
  602. if (!route_frame) spin_unlock_bh(&rose_node_list_lock);
  603. return res;
  604. }
  605. /*
  606. * Handle the ioctls that control the routing functions.
  607. */
  608. int rose_rt_ioctl(unsigned int cmd, void __user *arg)
  609. {
  610. struct rose_route_struct rose_route;
  611. struct net_device *dev;
  612. int err;
  613. switch (cmd) {
  614. case SIOCADDRT:
  615. if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
  616. return -EFAULT;
  617. if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
  618. return -EINVAL;
  619. if (rose_dev_exists(&rose_route.address)) /* Can't add routes to ourself */
  620. return -EINVAL;
  621. if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
  622. return -EINVAL;
  623. if (rose_route.ndigis > AX25_MAX_DIGIS)
  624. return -EINVAL;
  625. err = rose_add_node(&rose_route, dev);
  626. return err;
  627. case SIOCDELRT:
  628. if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
  629. return -EFAULT;
  630. if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
  631. return -EINVAL;
  632. err = rose_del_node(&rose_route, dev);
  633. return err;
  634. case SIOCRSCLRRT:
  635. return rose_clear_routes();
  636. default:
  637. return -EINVAL;
  638. }
  639. return 0;
  640. }
  641. static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
  642. {
  643. struct rose_route *rose_route, *s;
  644. rose_neigh->restarted = 0;
  645. rose_stop_t0timer(rose_neigh);
  646. rose_start_ftimer(rose_neigh);
  647. skb_queue_purge(&rose_neigh->queue);
  648. spin_lock_bh(&rose_route_list_lock);
  649. rose_route = rose_route_list;
  650. while (rose_route != NULL) {
  651. if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
  652. (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL) ||
  653. (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
  654. s = rose_route->next;
  655. rose_remove_route(rose_route);
  656. rose_route = s;
  657. continue;
  658. }
  659. if (rose_route->neigh1 == rose_neigh) {
  660. rose_route->neigh1->use--;
  661. rose_route->neigh1 = NULL;
  662. rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
  663. }
  664. if (rose_route->neigh2 == rose_neigh) {
  665. rose_route->neigh2->use--;
  666. rose_route->neigh2 = NULL;
  667. rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
  668. }
  669. rose_route = rose_route->next;
  670. }
  671. spin_unlock_bh(&rose_route_list_lock);
  672. }
  673. /*
  674. * A level 2 link has timed out, therefore it appears to be a poor link,
  675. * then don't use that neighbour until it is reset. Blow away all through
  676. * routes and connections using this route.
  677. */
  678. void rose_link_failed(ax25_cb *ax25, int reason)
  679. {
  680. struct rose_neigh *rose_neigh;
  681. spin_lock_bh(&rose_neigh_list_lock);
  682. rose_neigh = rose_neigh_list;
  683. while (rose_neigh != NULL) {
  684. if (rose_neigh->ax25 == ax25)
  685. break;
  686. rose_neigh = rose_neigh->next;
  687. }
  688. if (rose_neigh != NULL) {
  689. rose_neigh->ax25 = NULL;
  690. ax25_cb_put(ax25);
  691. rose_del_route_by_neigh(rose_neigh);
  692. rose_kill_by_neigh(rose_neigh);
  693. }
  694. spin_unlock_bh(&rose_neigh_list_lock);
  695. }
  696. /*
  697. * A device has been "downed" remove its link status. Blow away all
  698. * through routes and connections that use this device.
  699. */
  700. void rose_link_device_down(struct net_device *dev)
  701. {
  702. struct rose_neigh *rose_neigh;
  703. for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
  704. if (rose_neigh->dev == dev) {
  705. rose_del_route_by_neigh(rose_neigh);
  706. rose_kill_by_neigh(rose_neigh);
  707. }
  708. }
  709. }
  710. /*
  711. * Route a frame to an appropriate AX.25 connection.
  712. * A NULL ax25_cb indicates an internally generated frame.
  713. */
  714. int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
  715. {
  716. struct rose_neigh *rose_neigh, *new_neigh;
  717. struct rose_route *rose_route;
  718. struct rose_facilities_struct facilities;
  719. rose_address *src_addr, *dest_addr;
  720. struct sock *sk;
  721. unsigned short frametype;
  722. unsigned int lci, new_lci;
  723. unsigned char cause, diagnostic;
  724. struct net_device *dev;
  725. int res = 0;
  726. char buf[11];
  727. if (skb->len < ROSE_MIN_LEN)
  728. return res;
  729. if (!ax25)
  730. return rose_loopback_queue(skb, NULL);
  731. frametype = skb->data[2];
  732. lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
  733. if (frametype == ROSE_CALL_REQUEST &&
  734. (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
  735. skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
  736. ROSE_CALL_REQ_ADDR_LEN_VAL))
  737. return res;
  738. src_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_SRC_ADDR_OFF);
  739. dest_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
  740. spin_lock_bh(&rose_neigh_list_lock);
  741. spin_lock_bh(&rose_route_list_lock);
  742. rose_neigh = rose_neigh_list;
  743. while (rose_neigh != NULL) {
  744. if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
  745. ax25->ax25_dev->dev == rose_neigh->dev)
  746. break;
  747. rose_neigh = rose_neigh->next;
  748. }
  749. if (rose_neigh == NULL) {
  750. printk("rose_route : unknown neighbour or device %s\n",
  751. ax2asc(buf, &ax25->dest_addr));
  752. goto out;
  753. }
  754. /*
  755. * Obviously the link is working, halt the ftimer.
  756. */
  757. rose_stop_ftimer(rose_neigh);
  758. /*
  759. * LCI of zero is always for us, and its always a restart
  760. * frame.
  761. */
  762. if (lci == 0) {
  763. rose_link_rx_restart(skb, rose_neigh, frametype);
  764. goto out;
  765. }
  766. /*
  767. * Find an existing socket.
  768. */
  769. if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
  770. if (frametype == ROSE_CALL_REQUEST) {
  771. struct rose_sock *rose = rose_sk(sk);
  772. /* Remove an existing unused socket */
  773. rose_clear_queues(sk);
  774. rose->cause = ROSE_NETWORK_CONGESTION;
  775. rose->diagnostic = 0;
  776. rose->neighbour->use--;
  777. rose->neighbour = NULL;
  778. rose->lci = 0;
  779. rose->state = ROSE_STATE_0;
  780. sk->sk_state = TCP_CLOSE;
  781. sk->sk_err = 0;
  782. sk->sk_shutdown |= SEND_SHUTDOWN;
  783. if (!sock_flag(sk, SOCK_DEAD)) {
  784. sk->sk_state_change(sk);
  785. sock_set_flag(sk, SOCK_DEAD);
  786. }
  787. }
  788. else {
  789. skb_reset_transport_header(skb);
  790. res = rose_process_rx_frame(sk, skb);
  791. goto out;
  792. }
  793. }
  794. /*
  795. * Is is a Call Request and is it for us ?
  796. */
  797. if (frametype == ROSE_CALL_REQUEST)
  798. if ((dev = rose_dev_get(dest_addr)) != NULL) {
  799. res = rose_rx_call_request(skb, dev, rose_neigh, lci);
  800. dev_put(dev);
  801. goto out;
  802. }
  803. if (!sysctl_rose_routing_control) {
  804. rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
  805. goto out;
  806. }
  807. /*
  808. * Route it to the next in line if we have an entry for it.
  809. */
  810. rose_route = rose_route_list;
  811. while (rose_route != NULL) {
  812. if (rose_route->lci1 == lci &&
  813. rose_route->neigh1 == rose_neigh) {
  814. if (frametype == ROSE_CALL_REQUEST) {
  815. /* F6FBB - Remove an existing unused route */
  816. rose_remove_route(rose_route);
  817. break;
  818. } else if (rose_route->neigh2 != NULL) {
  819. skb->data[0] &= 0xF0;
  820. skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
  821. skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
  822. rose_transmit_link(skb, rose_route->neigh2);
  823. if (frametype == ROSE_CLEAR_CONFIRMATION)
  824. rose_remove_route(rose_route);
  825. res = 1;
  826. goto out;
  827. } else {
  828. if (frametype == ROSE_CLEAR_CONFIRMATION)
  829. rose_remove_route(rose_route);
  830. goto out;
  831. }
  832. }
  833. if (rose_route->lci2 == lci &&
  834. rose_route->neigh2 == rose_neigh) {
  835. if (frametype == ROSE_CALL_REQUEST) {
  836. /* F6FBB - Remove an existing unused route */
  837. rose_remove_route(rose_route);
  838. break;
  839. } else if (rose_route->neigh1 != NULL) {
  840. skb->data[0] &= 0xF0;
  841. skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
  842. skb->data[1] = (rose_route->lci1 >> 0) & 0xFF;
  843. rose_transmit_link(skb, rose_route->neigh1);
  844. if (frametype == ROSE_CLEAR_CONFIRMATION)
  845. rose_remove_route(rose_route);
  846. res = 1;
  847. goto out;
  848. } else {
  849. if (frametype == ROSE_CLEAR_CONFIRMATION)
  850. rose_remove_route(rose_route);
  851. goto out;
  852. }
  853. }
  854. rose_route = rose_route->next;
  855. }
  856. /*
  857. * We know that:
  858. * 1. The frame isn't for us,
  859. * 2. It isn't "owned" by any existing route.
  860. */
  861. if (frametype != ROSE_CALL_REQUEST) { /* XXX */
  862. res = 0;
  863. goto out;
  864. }
  865. memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
  866. if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
  867. skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
  868. &facilities)) {
  869. rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
  870. goto out;
  871. }
  872. /*
  873. * Check for routing loops.
  874. */
  875. rose_route = rose_route_list;
  876. while (rose_route != NULL) {
  877. if (rose_route->rand == facilities.rand &&
  878. rosecmp(src_addr, &rose_route->src_addr) == 0 &&
  879. ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
  880. ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
  881. rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
  882. goto out;
  883. }
  884. rose_route = rose_route->next;
  885. }
  886. if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic, 1)) == NULL) {
  887. rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
  888. goto out;
  889. }
  890. if ((new_lci = rose_new_lci(new_neigh)) == 0) {
  891. rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
  892. goto out;
  893. }
  894. if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
  895. rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
  896. goto out;
  897. }
  898. rose_route->lci1 = lci;
  899. rose_route->src_addr = *src_addr;
  900. rose_route->dest_addr = *dest_addr;
  901. rose_route->src_call = facilities.dest_call;
  902. rose_route->dest_call = facilities.source_call;
  903. rose_route->rand = facilities.rand;
  904. rose_route->neigh1 = rose_neigh;
  905. rose_route->lci2 = new_lci;
  906. rose_route->neigh2 = new_neigh;
  907. rose_route->neigh1->use++;
  908. rose_route->neigh2->use++;
  909. rose_route->next = rose_route_list;
  910. rose_route_list = rose_route;
  911. skb->data[0] &= 0xF0;
  912. skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
  913. skb->data[1] = (rose_route->lci2 >> 0) & 0xFF;
  914. rose_transmit_link(skb, rose_route->neigh2);
  915. res = 1;
  916. out:
  917. spin_unlock_bh(&rose_route_list_lock);
  918. spin_unlock_bh(&rose_neigh_list_lock);
  919. return res;
  920. }
  921. #ifdef CONFIG_PROC_FS
  922. static void *rose_node_start(struct seq_file *seq, loff_t *pos)
  923. __acquires(rose_node_list_lock)
  924. {
  925. struct rose_node *rose_node;
  926. int i = 1;
  927. spin_lock_bh(&rose_node_list_lock);
  928. if (*pos == 0)
  929. return SEQ_START_TOKEN;
  930. for (rose_node = rose_node_list; rose_node && i < *pos;
  931. rose_node = rose_node->next, ++i);
  932. return (i == *pos) ? rose_node : NULL;
  933. }
  934. static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
  935. {
  936. ++*pos;
  937. return (v == SEQ_START_TOKEN) ? rose_node_list
  938. : ((struct rose_node *)v)->next;
  939. }
  940. static void rose_node_stop(struct seq_file *seq, void *v)
  941. __releases(rose_node_list_lock)
  942. {
  943. spin_unlock_bh(&rose_node_list_lock);
  944. }
  945. static int rose_node_show(struct seq_file *seq, void *v)
  946. {
  947. char rsbuf[11];
  948. int i;
  949. if (v == SEQ_START_TOKEN)
  950. seq_puts(seq, "address mask n neigh neigh neigh\n");
  951. else {
  952. const struct rose_node *rose_node = v;
  953. /* if (rose_node->loopback) {
  954. seq_printf(seq, "%-10s %04d 1 loopback\n",
  955. rose2asc(rsbuf, &rose_node->address),
  956. rose_node->mask);
  957. } else { */
  958. seq_printf(seq, "%-10s %04d %d",
  959. rose2asc(rsbuf, &rose_node->address),
  960. rose_node->mask,
  961. rose_node->count);
  962. for (i = 0; i < rose_node->count; i++)
  963. seq_printf(seq, " %05d",
  964. rose_node->neighbour[i]->number);
  965. seq_puts(seq, "\n");
  966. /* } */
  967. }
  968. return 0;
  969. }
  970. const struct seq_operations rose_node_seqops = {
  971. .start = rose_node_start,
  972. .next = rose_node_next,
  973. .stop = rose_node_stop,
  974. .show = rose_node_show,
  975. };
  976. static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
  977. __acquires(rose_neigh_list_lock)
  978. {
  979. struct rose_neigh *rose_neigh;
  980. int i = 1;
  981. spin_lock_bh(&rose_neigh_list_lock);
  982. if (*pos == 0)
  983. return SEQ_START_TOKEN;
  984. for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
  985. rose_neigh = rose_neigh->next, ++i);
  986. return (i == *pos) ? rose_neigh : NULL;
  987. }
  988. static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
  989. {
  990. ++*pos;
  991. return (v == SEQ_START_TOKEN) ? rose_neigh_list
  992. : ((struct rose_neigh *)v)->next;
  993. }
  994. static void rose_neigh_stop(struct seq_file *seq, void *v)
  995. __releases(rose_neigh_list_lock)
  996. {
  997. spin_unlock_bh(&rose_neigh_list_lock);
  998. }
  999. static int rose_neigh_show(struct seq_file *seq, void *v)
  1000. {
  1001. char buf[11];
  1002. int i;
  1003. if (v == SEQ_START_TOKEN)
  1004. seq_puts(seq,
  1005. "addr callsign dev count use mode restart t0 tf digipeaters\n");
  1006. else {
  1007. struct rose_neigh *rose_neigh = v;
  1008. /* if (!rose_neigh->loopback) { */
  1009. seq_printf(seq, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
  1010. rose_neigh->number,
  1011. (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
  1012. rose_neigh->dev ? rose_neigh->dev->name : "???",
  1013. rose_neigh->count,
  1014. rose_neigh->use,
  1015. (rose_neigh->dce_mode) ? "DCE" : "DTE",
  1016. (rose_neigh->restarted) ? "yes" : "no",
  1017. ax25_display_timer(&rose_neigh->t0timer) / HZ,
  1018. ax25_display_timer(&rose_neigh->ftimer) / HZ);
  1019. if (rose_neigh->digipeat != NULL) {
  1020. for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
  1021. seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
  1022. }
  1023. seq_puts(seq, "\n");
  1024. }
  1025. return 0;
  1026. }
  1027. const struct seq_operations rose_neigh_seqops = {
  1028. .start = rose_neigh_start,
  1029. .next = rose_neigh_next,
  1030. .stop = rose_neigh_stop,
  1031. .show = rose_neigh_show,
  1032. };
  1033. static void *rose_route_start(struct seq_file *seq, loff_t *pos)
  1034. __acquires(rose_route_list_lock)
  1035. {
  1036. struct rose_route *rose_route;
  1037. int i = 1;
  1038. spin_lock_bh(&rose_route_list_lock);
  1039. if (*pos == 0)
  1040. return SEQ_START_TOKEN;
  1041. for (rose_route = rose_route_list; rose_route && i < *pos;
  1042. rose_route = rose_route->next, ++i);
  1043. return (i == *pos) ? rose_route : NULL;
  1044. }
  1045. static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
  1046. {
  1047. ++*pos;
  1048. return (v == SEQ_START_TOKEN) ? rose_route_list
  1049. : ((struct rose_route *)v)->next;
  1050. }
  1051. static void rose_route_stop(struct seq_file *seq, void *v)
  1052. __releases(rose_route_list_lock)
  1053. {
  1054. spin_unlock_bh(&rose_route_list_lock);
  1055. }
  1056. static int rose_route_show(struct seq_file *seq, void *v)
  1057. {
  1058. char buf[11], rsbuf[11];
  1059. if (v == SEQ_START_TOKEN)
  1060. seq_puts(seq,
  1061. "lci address callsign neigh <-> lci address callsign neigh\n");
  1062. else {
  1063. struct rose_route *rose_route = v;
  1064. if (rose_route->neigh1)
  1065. seq_printf(seq,
  1066. "%3.3X %-10s %-9s %05d ",
  1067. rose_route->lci1,
  1068. rose2asc(rsbuf, &rose_route->src_addr),
  1069. ax2asc(buf, &rose_route->src_call),
  1070. rose_route->neigh1->number);
  1071. else
  1072. seq_puts(seq,
  1073. "000 * * 00000 ");
  1074. if (rose_route->neigh2)
  1075. seq_printf(seq,
  1076. "%3.3X %-10s %-9s %05d\n",
  1077. rose_route->lci2,
  1078. rose2asc(rsbuf, &rose_route->dest_addr),
  1079. ax2asc(buf, &rose_route->dest_call),
  1080. rose_route->neigh2->number);
  1081. else
  1082. seq_puts(seq,
  1083. "000 * * 00000\n");
  1084. }
  1085. return 0;
  1086. }
  1087. struct seq_operations rose_route_seqops = {
  1088. .start = rose_route_start,
  1089. .next = rose_route_next,
  1090. .stop = rose_route_stop,
  1091. .show = rose_route_show,
  1092. };
  1093. #endif /* CONFIG_PROC_FS */
  1094. /*
  1095. * Release all memory associated with ROSE routing structures.
  1096. */
  1097. void __exit rose_rt_free(void)
  1098. {
  1099. struct rose_neigh *s, *rose_neigh = rose_neigh_list;
  1100. struct rose_node *t, *rose_node = rose_node_list;
  1101. struct rose_route *u, *rose_route = rose_route_list;
  1102. while (rose_neigh != NULL) {
  1103. s = rose_neigh;
  1104. rose_neigh = rose_neigh->next;
  1105. rose_remove_neigh(s);
  1106. }
  1107. while (rose_node != NULL) {
  1108. t = rose_node;
  1109. rose_node = rose_node->next;
  1110. rose_remove_node(t);
  1111. }
  1112. while (rose_route != NULL) {
  1113. u = rose_route;
  1114. rose_route = rose_route->next;
  1115. rose_remove_route(u);
  1116. }
  1117. }