nr_route.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
  9. * Copyright Tomi Manninen OH2BNS (oh2bns@sral.fi)
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/types.h>
  13. #include <linux/socket.h>
  14. #include <linux/in.h>
  15. #include <linux/kernel.h>
  16. #include <linux/timer.h>
  17. #include <linux/string.h>
  18. #include <linux/sockios.h>
  19. #include <linux/net.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 <asm/uaccess.h>
  28. #include <asm/system.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 <linux/spinlock.h>
  37. #include <net/netrom.h>
  38. #include <linux/seq_file.h>
  39. static unsigned int nr_neigh_no = 1;
  40. static HLIST_HEAD(nr_node_list);
  41. static DEFINE_SPINLOCK(nr_node_list_lock);
  42. static HLIST_HEAD(nr_neigh_list);
  43. static DEFINE_SPINLOCK(nr_neigh_list_lock);
  44. static struct nr_node *nr_node_get(ax25_address *callsign)
  45. {
  46. struct nr_node *found = NULL;
  47. struct nr_node *nr_node;
  48. struct hlist_node *node;
  49. spin_lock_bh(&nr_node_list_lock);
  50. nr_node_for_each(nr_node, node, &nr_node_list)
  51. if (ax25cmp(callsign, &nr_node->callsign) == 0) {
  52. nr_node_hold(nr_node);
  53. found = nr_node;
  54. break;
  55. }
  56. spin_unlock_bh(&nr_node_list_lock);
  57. return found;
  58. }
  59. static struct nr_neigh *nr_neigh_get_dev(ax25_address *callsign,
  60. struct net_device *dev)
  61. {
  62. struct nr_neigh *found = NULL;
  63. struct nr_neigh *nr_neigh;
  64. struct hlist_node *node;
  65. spin_lock_bh(&nr_neigh_list_lock);
  66. nr_neigh_for_each(nr_neigh, node, &nr_neigh_list)
  67. if (ax25cmp(callsign, &nr_neigh->callsign) == 0 &&
  68. nr_neigh->dev == dev) {
  69. nr_neigh_hold(nr_neigh);
  70. found = nr_neigh;
  71. break;
  72. }
  73. spin_unlock_bh(&nr_neigh_list_lock);
  74. return found;
  75. }
  76. static void nr_remove_neigh(struct nr_neigh *);
  77. /*
  78. * Add a new route to a node, and in the process add the node and the
  79. * neighbour if it is new.
  80. */
  81. static int __must_check nr_add_node(ax25_address *nr, const char *mnemonic,
  82. ax25_address *ax25, ax25_digi *ax25_digi, struct net_device *dev,
  83. int quality, int obs_count)
  84. {
  85. struct nr_node *nr_node;
  86. struct nr_neigh *nr_neigh;
  87. struct nr_route nr_route;
  88. int i, found;
  89. struct net_device *odev;
  90. if ((odev=nr_dev_get(nr)) != NULL) { /* Can't add routes to ourself */
  91. dev_put(odev);
  92. return -EINVAL;
  93. }
  94. nr_node = nr_node_get(nr);
  95. nr_neigh = nr_neigh_get_dev(ax25, dev);
  96. /*
  97. * The L2 link to a neighbour has failed in the past
  98. * and now a frame comes from this neighbour. We assume
  99. * it was a temporary trouble with the link and reset the
  100. * routes now (and not wait for a node broadcast).
  101. */
  102. if (nr_neigh != NULL && nr_neigh->failed != 0 && quality == 0) {
  103. struct nr_node *nr_nodet;
  104. struct hlist_node *node;
  105. spin_lock_bh(&nr_node_list_lock);
  106. nr_node_for_each(nr_nodet, node, &nr_node_list) {
  107. nr_node_lock(nr_nodet);
  108. for (i = 0; i < nr_nodet->count; i++)
  109. if (nr_nodet->routes[i].neighbour == nr_neigh)
  110. if (i < nr_nodet->which)
  111. nr_nodet->which = i;
  112. nr_node_unlock(nr_nodet);
  113. }
  114. spin_unlock_bh(&nr_node_list_lock);
  115. }
  116. if (nr_neigh != NULL)
  117. nr_neigh->failed = 0;
  118. if (quality == 0 && nr_neigh != NULL && nr_node != NULL) {
  119. nr_neigh_put(nr_neigh);
  120. nr_node_put(nr_node);
  121. return 0;
  122. }
  123. if (nr_neigh == NULL) {
  124. if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL) {
  125. if (nr_node)
  126. nr_node_put(nr_node);
  127. return -ENOMEM;
  128. }
  129. nr_neigh->callsign = *ax25;
  130. nr_neigh->digipeat = NULL;
  131. nr_neigh->ax25 = NULL;
  132. nr_neigh->dev = dev;
  133. nr_neigh->quality = sysctl_netrom_default_path_quality;
  134. nr_neigh->locked = 0;
  135. nr_neigh->count = 0;
  136. nr_neigh->number = nr_neigh_no++;
  137. nr_neigh->failed = 0;
  138. atomic_set(&nr_neigh->refcount, 1);
  139. if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
  140. nr_neigh->digipeat = kmemdup(ax25_digi,
  141. sizeof(*ax25_digi),
  142. GFP_KERNEL);
  143. if (nr_neigh->digipeat == NULL) {
  144. kfree(nr_neigh);
  145. if (nr_node)
  146. nr_node_put(nr_node);
  147. return -ENOMEM;
  148. }
  149. }
  150. spin_lock_bh(&nr_neigh_list_lock);
  151. hlist_add_head(&nr_neigh->neigh_node, &nr_neigh_list);
  152. nr_neigh_hold(nr_neigh);
  153. spin_unlock_bh(&nr_neigh_list_lock);
  154. }
  155. if (quality != 0 && ax25cmp(nr, ax25) == 0 && !nr_neigh->locked)
  156. nr_neigh->quality = quality;
  157. if (nr_node == NULL) {
  158. if ((nr_node = kmalloc(sizeof(*nr_node), GFP_ATOMIC)) == NULL) {
  159. if (nr_neigh)
  160. nr_neigh_put(nr_neigh);
  161. return -ENOMEM;
  162. }
  163. nr_node->callsign = *nr;
  164. strcpy(nr_node->mnemonic, mnemonic);
  165. nr_node->which = 0;
  166. nr_node->count = 1;
  167. atomic_set(&nr_node->refcount, 1);
  168. spin_lock_init(&nr_node->node_lock);
  169. nr_node->routes[0].quality = quality;
  170. nr_node->routes[0].obs_count = obs_count;
  171. nr_node->routes[0].neighbour = nr_neigh;
  172. nr_neigh_hold(nr_neigh);
  173. nr_neigh->count++;
  174. spin_lock_bh(&nr_node_list_lock);
  175. hlist_add_head(&nr_node->node_node, &nr_node_list);
  176. /* refcount initialized at 1 */
  177. spin_unlock_bh(&nr_node_list_lock);
  178. return 0;
  179. }
  180. nr_node_lock(nr_node);
  181. if (quality != 0)
  182. strcpy(nr_node->mnemonic, mnemonic);
  183. for (found = 0, i = 0; i < nr_node->count; i++) {
  184. if (nr_node->routes[i].neighbour == nr_neigh) {
  185. nr_node->routes[i].quality = quality;
  186. nr_node->routes[i].obs_count = obs_count;
  187. found = 1;
  188. break;
  189. }
  190. }
  191. if (!found) {
  192. /* We have space at the bottom, slot it in */
  193. if (nr_node->count < 3) {
  194. nr_node->routes[2] = nr_node->routes[1];
  195. nr_node->routes[1] = nr_node->routes[0];
  196. nr_node->routes[0].quality = quality;
  197. nr_node->routes[0].obs_count = obs_count;
  198. nr_node->routes[0].neighbour = nr_neigh;
  199. nr_node->which++;
  200. nr_node->count++;
  201. nr_neigh_hold(nr_neigh);
  202. nr_neigh->count++;
  203. } else {
  204. /* It must be better than the worst */
  205. if (quality > nr_node->routes[2].quality) {
  206. nr_node->routes[2].neighbour->count--;
  207. nr_neigh_put(nr_node->routes[2].neighbour);
  208. if (nr_node->routes[2].neighbour->count == 0 && !nr_node->routes[2].neighbour->locked)
  209. nr_remove_neigh(nr_node->routes[2].neighbour);
  210. nr_node->routes[2].quality = quality;
  211. nr_node->routes[2].obs_count = obs_count;
  212. nr_node->routes[2].neighbour = nr_neigh;
  213. nr_neigh_hold(nr_neigh);
  214. nr_neigh->count++;
  215. }
  216. }
  217. }
  218. /* Now re-sort the routes in quality order */
  219. switch (nr_node->count) {
  220. case 3:
  221. if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
  222. switch (nr_node->which) {
  223. case 0: nr_node->which = 1; break;
  224. case 1: nr_node->which = 0; break;
  225. default: break;
  226. }
  227. nr_route = nr_node->routes[0];
  228. nr_node->routes[0] = nr_node->routes[1];
  229. nr_node->routes[1] = nr_route;
  230. }
  231. if (nr_node->routes[2].quality > nr_node->routes[1].quality) {
  232. switch (nr_node->which) {
  233. case 1: nr_node->which = 2;
  234. break;
  235. case 2: nr_node->which = 1;
  236. break;
  237. default:
  238. break;
  239. }
  240. nr_route = nr_node->routes[1];
  241. nr_node->routes[1] = nr_node->routes[2];
  242. nr_node->routes[2] = nr_route;
  243. }
  244. case 2:
  245. if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
  246. switch (nr_node->which) {
  247. case 0: nr_node->which = 1;
  248. break;
  249. case 1: nr_node->which = 0;
  250. break;
  251. default: break;
  252. }
  253. nr_route = nr_node->routes[0];
  254. nr_node->routes[0] = nr_node->routes[1];
  255. nr_node->routes[1] = nr_route;
  256. }
  257. case 1:
  258. break;
  259. }
  260. for (i = 0; i < nr_node->count; i++) {
  261. if (nr_node->routes[i].neighbour == nr_neigh) {
  262. if (i < nr_node->which)
  263. nr_node->which = i;
  264. break;
  265. }
  266. }
  267. nr_neigh_put(nr_neigh);
  268. nr_node_unlock(nr_node);
  269. nr_node_put(nr_node);
  270. return 0;
  271. }
  272. static inline void __nr_remove_node(struct nr_node *nr_node)
  273. {
  274. hlist_del_init(&nr_node->node_node);
  275. nr_node_put(nr_node);
  276. }
  277. #define nr_remove_node_locked(__node) \
  278. __nr_remove_node(__node)
  279. static void nr_remove_node(struct nr_node *nr_node)
  280. {
  281. spin_lock_bh(&nr_node_list_lock);
  282. __nr_remove_node(nr_node);
  283. spin_unlock_bh(&nr_node_list_lock);
  284. }
  285. static inline void __nr_remove_neigh(struct nr_neigh *nr_neigh)
  286. {
  287. hlist_del_init(&nr_neigh->neigh_node);
  288. nr_neigh_put(nr_neigh);
  289. }
  290. #define nr_remove_neigh_locked(__neigh) \
  291. __nr_remove_neigh(__neigh)
  292. static void nr_remove_neigh(struct nr_neigh *nr_neigh)
  293. {
  294. spin_lock_bh(&nr_neigh_list_lock);
  295. __nr_remove_neigh(nr_neigh);
  296. spin_unlock_bh(&nr_neigh_list_lock);
  297. }
  298. /*
  299. * "Delete" a node. Strictly speaking remove a route to a node. The node
  300. * is only deleted if no routes are left to it.
  301. */
  302. static int nr_del_node(ax25_address *callsign, ax25_address *neighbour, struct net_device *dev)
  303. {
  304. struct nr_node *nr_node;
  305. struct nr_neigh *nr_neigh;
  306. int i;
  307. nr_node = nr_node_get(callsign);
  308. if (nr_node == NULL)
  309. return -EINVAL;
  310. nr_neigh = nr_neigh_get_dev(neighbour, dev);
  311. if (nr_neigh == NULL) {
  312. nr_node_put(nr_node);
  313. return -EINVAL;
  314. }
  315. nr_node_lock(nr_node);
  316. for (i = 0; i < nr_node->count; i++) {
  317. if (nr_node->routes[i].neighbour == nr_neigh) {
  318. nr_neigh->count--;
  319. nr_neigh_put(nr_neigh);
  320. if (nr_neigh->count == 0 && !nr_neigh->locked)
  321. nr_remove_neigh(nr_neigh);
  322. nr_neigh_put(nr_neigh);
  323. nr_node->count--;
  324. if (nr_node->count == 0) {
  325. nr_remove_node(nr_node);
  326. } else {
  327. switch (i) {
  328. case 0:
  329. nr_node->routes[0] = nr_node->routes[1];
  330. case 1:
  331. nr_node->routes[1] = nr_node->routes[2];
  332. case 2:
  333. break;
  334. }
  335. nr_node_put(nr_node);
  336. }
  337. nr_node_unlock(nr_node);
  338. return 0;
  339. }
  340. }
  341. nr_neigh_put(nr_neigh);
  342. nr_node_unlock(nr_node);
  343. nr_node_put(nr_node);
  344. return -EINVAL;
  345. }
  346. /*
  347. * Lock a neighbour with a quality.
  348. */
  349. static int __must_check nr_add_neigh(ax25_address *callsign,
  350. ax25_digi *ax25_digi, struct net_device *dev, unsigned int quality)
  351. {
  352. struct nr_neigh *nr_neigh;
  353. nr_neigh = nr_neigh_get_dev(callsign, dev);
  354. if (nr_neigh) {
  355. nr_neigh->quality = quality;
  356. nr_neigh->locked = 1;
  357. nr_neigh_put(nr_neigh);
  358. return 0;
  359. }
  360. if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL)
  361. return -ENOMEM;
  362. nr_neigh->callsign = *callsign;
  363. nr_neigh->digipeat = NULL;
  364. nr_neigh->ax25 = NULL;
  365. nr_neigh->dev = dev;
  366. nr_neigh->quality = quality;
  367. nr_neigh->locked = 1;
  368. nr_neigh->count = 0;
  369. nr_neigh->number = nr_neigh_no++;
  370. nr_neigh->failed = 0;
  371. atomic_set(&nr_neigh->refcount, 1);
  372. if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
  373. nr_neigh->digipeat = kmemdup(ax25_digi, sizeof(*ax25_digi),
  374. GFP_KERNEL);
  375. if (nr_neigh->digipeat == NULL) {
  376. kfree(nr_neigh);
  377. return -ENOMEM;
  378. }
  379. }
  380. spin_lock_bh(&nr_neigh_list_lock);
  381. hlist_add_head(&nr_neigh->neigh_node, &nr_neigh_list);
  382. /* refcount is initialized at 1 */
  383. spin_unlock_bh(&nr_neigh_list_lock);
  384. return 0;
  385. }
  386. /*
  387. * "Delete" a neighbour. The neighbour is only removed if the number
  388. * of nodes that may use it is zero.
  389. */
  390. static int nr_del_neigh(ax25_address *callsign, struct net_device *dev, unsigned int quality)
  391. {
  392. struct nr_neigh *nr_neigh;
  393. nr_neigh = nr_neigh_get_dev(callsign, dev);
  394. if (nr_neigh == NULL) return -EINVAL;
  395. nr_neigh->quality = quality;
  396. nr_neigh->locked = 0;
  397. if (nr_neigh->count == 0)
  398. nr_remove_neigh(nr_neigh);
  399. nr_neigh_put(nr_neigh);
  400. return 0;
  401. }
  402. /*
  403. * Decrement the obsolescence count by one. If a route is reduced to a
  404. * count of zero, remove it. Also remove any unlocked neighbours with
  405. * zero nodes routing via it.
  406. */
  407. static int nr_dec_obs(void)
  408. {
  409. struct nr_neigh *nr_neigh;
  410. struct nr_node *s;
  411. struct hlist_node *node, *nodet;
  412. int i;
  413. spin_lock_bh(&nr_node_list_lock);
  414. nr_node_for_each_safe(s, node, nodet, &nr_node_list) {
  415. nr_node_lock(s);
  416. for (i = 0; i < s->count; i++) {
  417. switch (s->routes[i].obs_count) {
  418. case 0: /* A locked entry */
  419. break;
  420. case 1: /* From 1 -> 0 */
  421. nr_neigh = s->routes[i].neighbour;
  422. nr_neigh->count--;
  423. nr_neigh_put(nr_neigh);
  424. if (nr_neigh->count == 0 && !nr_neigh->locked)
  425. nr_remove_neigh(nr_neigh);
  426. s->count--;
  427. switch (i) {
  428. case 0:
  429. s->routes[0] = s->routes[1];
  430. case 1:
  431. s->routes[1] = s->routes[2];
  432. case 2:
  433. break;
  434. }
  435. break;
  436. default:
  437. s->routes[i].obs_count--;
  438. break;
  439. }
  440. }
  441. if (s->count <= 0)
  442. nr_remove_node_locked(s);
  443. nr_node_unlock(s);
  444. }
  445. spin_unlock_bh(&nr_node_list_lock);
  446. return 0;
  447. }
  448. /*
  449. * A device has been removed. Remove its routes and neighbours.
  450. */
  451. void nr_rt_device_down(struct net_device *dev)
  452. {
  453. struct nr_neigh *s;
  454. struct hlist_node *node, *nodet, *node2, *node2t;
  455. struct nr_node *t;
  456. int i;
  457. spin_lock_bh(&nr_neigh_list_lock);
  458. nr_neigh_for_each_safe(s, node, nodet, &nr_neigh_list) {
  459. if (s->dev == dev) {
  460. spin_lock_bh(&nr_node_list_lock);
  461. nr_node_for_each_safe(t, node2, node2t, &nr_node_list) {
  462. nr_node_lock(t);
  463. for (i = 0; i < t->count; i++) {
  464. if (t->routes[i].neighbour == s) {
  465. t->count--;
  466. switch (i) {
  467. case 0:
  468. t->routes[0] = t->routes[1];
  469. case 1:
  470. t->routes[1] = t->routes[2];
  471. case 2:
  472. break;
  473. }
  474. }
  475. }
  476. if (t->count <= 0)
  477. nr_remove_node_locked(t);
  478. nr_node_unlock(t);
  479. }
  480. spin_unlock_bh(&nr_node_list_lock);
  481. nr_remove_neigh_locked(s);
  482. }
  483. }
  484. spin_unlock_bh(&nr_neigh_list_lock);
  485. }
  486. /*
  487. * Check that the device given is a valid AX.25 interface that is "up".
  488. * Or a valid ethernet interface with an AX.25 callsign binding.
  489. */
  490. static struct net_device *nr_ax25_dev_get(char *devname)
  491. {
  492. struct net_device *dev;
  493. if ((dev = dev_get_by_name(devname)) == NULL)
  494. return NULL;
  495. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
  496. return dev;
  497. dev_put(dev);
  498. return NULL;
  499. }
  500. /*
  501. * Find the first active NET/ROM device, usually "nr0".
  502. */
  503. struct net_device *nr_dev_first(void)
  504. {
  505. struct net_device *dev, *first = NULL;
  506. read_lock(&dev_base_lock);
  507. for (dev = dev_base; dev != NULL; dev = dev->next) {
  508. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM)
  509. if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
  510. first = dev;
  511. }
  512. if (first)
  513. dev_hold(first);
  514. read_unlock(&dev_base_lock);
  515. return first;
  516. }
  517. /*
  518. * Find the NET/ROM device for the given callsign.
  519. */
  520. struct net_device *nr_dev_get(ax25_address *addr)
  521. {
  522. struct net_device *dev;
  523. read_lock(&dev_base_lock);
  524. for (dev = dev_base; dev != NULL; dev = dev->next) {
  525. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM && ax25cmp(addr, (ax25_address *)dev->dev_addr) == 0) {
  526. dev_hold(dev);
  527. goto out;
  528. }
  529. }
  530. out:
  531. read_unlock(&dev_base_lock);
  532. return dev;
  533. }
  534. static ax25_digi *nr_call_to_digi(int ndigis, ax25_address *digipeaters)
  535. {
  536. static ax25_digi ax25_digi;
  537. int i;
  538. if (ndigis == 0)
  539. return NULL;
  540. for (i = 0; i < ndigis; i++) {
  541. ax25_digi.calls[i] = digipeaters[i];
  542. ax25_digi.repeated[i] = 0;
  543. }
  544. ax25_digi.ndigi = ndigis;
  545. ax25_digi.lastrepeat = -1;
  546. return &ax25_digi;
  547. }
  548. /*
  549. * Handle the ioctls that control the routing functions.
  550. */
  551. int nr_rt_ioctl(unsigned int cmd, void __user *arg)
  552. {
  553. struct nr_route_struct nr_route;
  554. struct net_device *dev;
  555. int ret;
  556. switch (cmd) {
  557. case SIOCADDRT:
  558. if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
  559. return -EFAULT;
  560. if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
  561. return -EINVAL;
  562. if (nr_route.ndigis < 0 || nr_route.ndigis > AX25_MAX_DIGIS) {
  563. dev_put(dev);
  564. return -EINVAL;
  565. }
  566. switch (nr_route.type) {
  567. case NETROM_NODE:
  568. ret = nr_add_node(&nr_route.callsign,
  569. nr_route.mnemonic,
  570. &nr_route.neighbour,
  571. nr_call_to_digi(nr_route.ndigis, nr_route.digipeaters),
  572. dev, nr_route.quality,
  573. nr_route.obs_count);
  574. break;
  575. case NETROM_NEIGH:
  576. ret = nr_add_neigh(&nr_route.callsign,
  577. nr_call_to_digi(nr_route.ndigis, nr_route.digipeaters),
  578. dev, nr_route.quality);
  579. break;
  580. default:
  581. ret = -EINVAL;
  582. }
  583. dev_put(dev);
  584. return ret;
  585. case SIOCDELRT:
  586. if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
  587. return -EFAULT;
  588. if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
  589. return -EINVAL;
  590. switch (nr_route.type) {
  591. case NETROM_NODE:
  592. ret = nr_del_node(&nr_route.callsign,
  593. &nr_route.neighbour, dev);
  594. break;
  595. case NETROM_NEIGH:
  596. ret = nr_del_neigh(&nr_route.callsign,
  597. dev, nr_route.quality);
  598. break;
  599. default:
  600. ret = -EINVAL;
  601. }
  602. dev_put(dev);
  603. return ret;
  604. case SIOCNRDECOBS:
  605. return nr_dec_obs();
  606. default:
  607. return -EINVAL;
  608. }
  609. return 0;
  610. }
  611. /*
  612. * A level 2 link has timed out, therefore it appears to be a poor link,
  613. * then don't use that neighbour until it is reset.
  614. */
  615. void nr_link_failed(ax25_cb *ax25, int reason)
  616. {
  617. struct nr_neigh *s, *nr_neigh = NULL;
  618. struct hlist_node *node;
  619. struct nr_node *nr_node = NULL;
  620. spin_lock_bh(&nr_neigh_list_lock);
  621. nr_neigh_for_each(s, node, &nr_neigh_list) {
  622. if (s->ax25 == ax25) {
  623. nr_neigh_hold(s);
  624. nr_neigh = s;
  625. break;
  626. }
  627. }
  628. spin_unlock_bh(&nr_neigh_list_lock);
  629. if (nr_neigh == NULL)
  630. return;
  631. nr_neigh->ax25 = NULL;
  632. ax25_cb_put(ax25);
  633. if (++nr_neigh->failed < sysctl_netrom_link_fails_count) {
  634. nr_neigh_put(nr_neigh);
  635. return;
  636. }
  637. spin_lock_bh(&nr_node_list_lock);
  638. nr_node_for_each(nr_node, node, &nr_node_list) {
  639. nr_node_lock(nr_node);
  640. if (nr_node->which < nr_node->count &&
  641. nr_node->routes[nr_node->which].neighbour == nr_neigh)
  642. nr_node->which++;
  643. nr_node_unlock(nr_node);
  644. }
  645. spin_unlock_bh(&nr_node_list_lock);
  646. nr_neigh_put(nr_neigh);
  647. }
  648. /*
  649. * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
  650. * indicates an internally generated frame.
  651. */
  652. int nr_route_frame(struct sk_buff *skb, ax25_cb *ax25)
  653. {
  654. ax25_address *nr_src, *nr_dest;
  655. struct nr_neigh *nr_neigh;
  656. struct nr_node *nr_node;
  657. struct net_device *dev;
  658. unsigned char *dptr;
  659. ax25_cb *ax25s;
  660. int ret;
  661. struct sk_buff *skbn;
  662. nr_src = (ax25_address *)(skb->data + 0);
  663. nr_dest = (ax25_address *)(skb->data + 7);
  664. if (ax25 != NULL) {
  665. ret = nr_add_node(nr_src, "", &ax25->dest_addr, ax25->digipeat,
  666. ax25->ax25_dev->dev, 0,
  667. sysctl_netrom_obsolescence_count_initialiser);
  668. if (ret)
  669. return ret;
  670. }
  671. if ((dev = nr_dev_get(nr_dest)) != NULL) { /* Its for me */
  672. if (ax25 == NULL) /* Its from me */
  673. ret = nr_loopback_queue(skb);
  674. else
  675. ret = nr_rx_frame(skb, dev);
  676. dev_put(dev);
  677. return ret;
  678. }
  679. if (!sysctl_netrom_routing_control && ax25 != NULL)
  680. return 0;
  681. /* Its Time-To-Live has expired */
  682. if (skb->data[14] == 1) {
  683. return 0;
  684. }
  685. nr_node = nr_node_get(nr_dest);
  686. if (nr_node == NULL)
  687. return 0;
  688. nr_node_lock(nr_node);
  689. if (nr_node->which >= nr_node->count) {
  690. nr_node_unlock(nr_node);
  691. nr_node_put(nr_node);
  692. return 0;
  693. }
  694. nr_neigh = nr_node->routes[nr_node->which].neighbour;
  695. if ((dev = nr_dev_first()) == NULL) {
  696. nr_node_unlock(nr_node);
  697. nr_node_put(nr_node);
  698. return 0;
  699. }
  700. /* We are going to change the netrom headers so we should get our
  701. own skb, we also did not know until now how much header space
  702. we had to reserve... - RXQ */
  703. if ((skbn=skb_copy_expand(skb, dev->hard_header_len, 0, GFP_ATOMIC)) == NULL) {
  704. nr_node_unlock(nr_node);
  705. nr_node_put(nr_node);
  706. dev_put(dev);
  707. return 0;
  708. }
  709. kfree_skb(skb);
  710. skb=skbn;
  711. skb->data[14]--;
  712. dptr = skb_push(skb, 1);
  713. *dptr = AX25_P_NETROM;
  714. ax25s = ax25_send_frame(skb, 256, (ax25_address *)dev->dev_addr, &nr_neigh->callsign, nr_neigh->digipeat, nr_neigh->dev);
  715. if (nr_neigh->ax25 && ax25s) {
  716. /* We were already holding this ax25_cb */
  717. ax25_cb_put(ax25s);
  718. }
  719. nr_neigh->ax25 = ax25s;
  720. dev_put(dev);
  721. ret = (nr_neigh->ax25 != NULL);
  722. nr_node_unlock(nr_node);
  723. nr_node_put(nr_node);
  724. return ret;
  725. }
  726. #ifdef CONFIG_PROC_FS
  727. static void *nr_node_start(struct seq_file *seq, loff_t *pos)
  728. {
  729. struct nr_node *nr_node;
  730. struct hlist_node *node;
  731. int i = 1;
  732. spin_lock_bh(&nr_node_list_lock);
  733. if (*pos == 0)
  734. return SEQ_START_TOKEN;
  735. nr_node_for_each(nr_node, node, &nr_node_list) {
  736. if (i == *pos)
  737. return nr_node;
  738. ++i;
  739. }
  740. return NULL;
  741. }
  742. static void *nr_node_next(struct seq_file *seq, void *v, loff_t *pos)
  743. {
  744. struct hlist_node *node;
  745. ++*pos;
  746. node = (v == SEQ_START_TOKEN)
  747. ? nr_node_list.first
  748. : ((struct nr_node *)v)->node_node.next;
  749. return hlist_entry(node, struct nr_node, node_node);
  750. }
  751. static void nr_node_stop(struct seq_file *seq, void *v)
  752. {
  753. spin_unlock_bh(&nr_node_list_lock);
  754. }
  755. static int nr_node_show(struct seq_file *seq, void *v)
  756. {
  757. char buf[11];
  758. int i;
  759. if (v == SEQ_START_TOKEN)
  760. seq_puts(seq,
  761. "callsign mnemonic w n qual obs neigh qual obs neigh qual obs neigh\n");
  762. else {
  763. struct nr_node *nr_node = v;
  764. nr_node_lock(nr_node);
  765. seq_printf(seq, "%-9s %-7s %d %d",
  766. ax2asc(buf, &nr_node->callsign),
  767. (nr_node->mnemonic[0] == '\0') ? "*" : nr_node->mnemonic,
  768. nr_node->which + 1,
  769. nr_node->count);
  770. for (i = 0; i < nr_node->count; i++) {
  771. seq_printf(seq, " %3d %d %05d",
  772. nr_node->routes[i].quality,
  773. nr_node->routes[i].obs_count,
  774. nr_node->routes[i].neighbour->number);
  775. }
  776. nr_node_unlock(nr_node);
  777. seq_puts(seq, "\n");
  778. }
  779. return 0;
  780. }
  781. static struct seq_operations nr_node_seqops = {
  782. .start = nr_node_start,
  783. .next = nr_node_next,
  784. .stop = nr_node_stop,
  785. .show = nr_node_show,
  786. };
  787. static int nr_node_info_open(struct inode *inode, struct file *file)
  788. {
  789. return seq_open(file, &nr_node_seqops);
  790. }
  791. const struct file_operations nr_nodes_fops = {
  792. .owner = THIS_MODULE,
  793. .open = nr_node_info_open,
  794. .read = seq_read,
  795. .llseek = seq_lseek,
  796. .release = seq_release,
  797. };
  798. static void *nr_neigh_start(struct seq_file *seq, loff_t *pos)
  799. {
  800. struct nr_neigh *nr_neigh;
  801. struct hlist_node *node;
  802. int i = 1;
  803. spin_lock_bh(&nr_neigh_list_lock);
  804. if (*pos == 0)
  805. return SEQ_START_TOKEN;
  806. nr_neigh_for_each(nr_neigh, node, &nr_neigh_list) {
  807. if (i == *pos)
  808. return nr_neigh;
  809. }
  810. return NULL;
  811. }
  812. static void *nr_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
  813. {
  814. struct hlist_node *node;
  815. ++*pos;
  816. node = (v == SEQ_START_TOKEN)
  817. ? nr_neigh_list.first
  818. : ((struct nr_neigh *)v)->neigh_node.next;
  819. return hlist_entry(node, struct nr_neigh, neigh_node);
  820. }
  821. static void nr_neigh_stop(struct seq_file *seq, void *v)
  822. {
  823. spin_unlock_bh(&nr_neigh_list_lock);
  824. }
  825. static int nr_neigh_show(struct seq_file *seq, void *v)
  826. {
  827. char buf[11];
  828. int i;
  829. if (v == SEQ_START_TOKEN)
  830. seq_puts(seq, "addr callsign dev qual lock count failed digipeaters\n");
  831. else {
  832. struct nr_neigh *nr_neigh = v;
  833. seq_printf(seq, "%05d %-9s %-4s %3d %d %3d %3d",
  834. nr_neigh->number,
  835. ax2asc(buf, &nr_neigh->callsign),
  836. nr_neigh->dev ? nr_neigh->dev->name : "???",
  837. nr_neigh->quality,
  838. nr_neigh->locked,
  839. nr_neigh->count,
  840. nr_neigh->failed);
  841. if (nr_neigh->digipeat != NULL) {
  842. for (i = 0; i < nr_neigh->digipeat->ndigi; i++)
  843. seq_printf(seq, " %s",
  844. ax2asc(buf, &nr_neigh->digipeat->calls[i]));
  845. }
  846. seq_puts(seq, "\n");
  847. }
  848. return 0;
  849. }
  850. static struct seq_operations nr_neigh_seqops = {
  851. .start = nr_neigh_start,
  852. .next = nr_neigh_next,
  853. .stop = nr_neigh_stop,
  854. .show = nr_neigh_show,
  855. };
  856. static int nr_neigh_info_open(struct inode *inode, struct file *file)
  857. {
  858. return seq_open(file, &nr_neigh_seqops);
  859. }
  860. const struct file_operations nr_neigh_fops = {
  861. .owner = THIS_MODULE,
  862. .open = nr_neigh_info_open,
  863. .read = seq_read,
  864. .llseek = seq_lseek,
  865. .release = seq_release,
  866. };
  867. #endif
  868. /*
  869. * Free all memory associated with the nodes and routes lists.
  870. */
  871. void __exit nr_rt_free(void)
  872. {
  873. struct nr_neigh *s = NULL;
  874. struct nr_node *t = NULL;
  875. struct hlist_node *node, *nodet;
  876. spin_lock_bh(&nr_neigh_list_lock);
  877. spin_lock_bh(&nr_node_list_lock);
  878. nr_node_for_each_safe(t, node, nodet, &nr_node_list) {
  879. nr_node_lock(t);
  880. nr_remove_node_locked(t);
  881. nr_node_unlock(t);
  882. }
  883. nr_neigh_for_each_safe(s, node, nodet, &nr_neigh_list) {
  884. while(s->count) {
  885. s->count--;
  886. nr_neigh_put(s);
  887. }
  888. nr_remove_neigh_locked(s);
  889. }
  890. spin_unlock_bh(&nr_node_list_lock);
  891. spin_unlock_bh(&nr_neigh_list_lock);
  892. }