rose_route.c 31 KB

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