nr_route.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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 <linux/slab.h>
  21. #include <net/ax25.h>
  22. #include <linux/inet.h>
  23. #include <linux/netdevice.h>
  24. #include <net/arp.h>
  25. #include <linux/if_arp.h>
  26. #include <linux/skbuff.h>
  27. #include <net/sock.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 <linux/spinlock.h>
  36. #include <net/netrom.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/export.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. spin_lock_bh(&nr_node_list_lock);
  49. nr_node_for_each(nr_node, &nr_node_list)
  50. if (ax25cmp(callsign, &nr_node->callsign) == 0) {
  51. nr_node_hold(nr_node);
  52. found = nr_node;
  53. break;
  54. }
  55. spin_unlock_bh(&nr_node_list_lock);
  56. return found;
  57. }
  58. static struct nr_neigh *nr_neigh_get_dev(ax25_address *callsign,
  59. struct net_device *dev)
  60. {
  61. struct nr_neigh *found = NULL;
  62. struct nr_neigh *nr_neigh;
  63. spin_lock_bh(&nr_neigh_list_lock);
  64. nr_neigh_for_each(nr_neigh, &nr_neigh_list)
  65. if (ax25cmp(callsign, &nr_neigh->callsign) == 0 &&
  66. nr_neigh->dev == dev) {
  67. nr_neigh_hold(nr_neigh);
  68. found = nr_neigh;
  69. break;
  70. }
  71. spin_unlock_bh(&nr_neigh_list_lock);
  72. return found;
  73. }
  74. static void nr_remove_neigh(struct nr_neigh *);
  75. /*
  76. * Add a new route to a node, and in the process add the node and the
  77. * neighbour if it is new.
  78. */
  79. static int __must_check nr_add_node(ax25_address *nr, const char *mnemonic,
  80. ax25_address *ax25, ax25_digi *ax25_digi, struct net_device *dev,
  81. int quality, int obs_count)
  82. {
  83. struct nr_node *nr_node;
  84. struct nr_neigh *nr_neigh;
  85. struct nr_route nr_route;
  86. int i, found;
  87. struct net_device *odev;
  88. if ((odev=nr_dev_get(nr)) != NULL) { /* Can't add routes to ourself */
  89. dev_put(odev);
  90. return -EINVAL;
  91. }
  92. nr_node = nr_node_get(nr);
  93. nr_neigh = nr_neigh_get_dev(ax25, dev);
  94. /*
  95. * The L2 link to a neighbour has failed in the past
  96. * and now a frame comes from this neighbour. We assume
  97. * it was a temporary trouble with the link and reset the
  98. * routes now (and not wait for a node broadcast).
  99. */
  100. if (nr_neigh != NULL && nr_neigh->failed != 0 && quality == 0) {
  101. struct nr_node *nr_nodet;
  102. spin_lock_bh(&nr_node_list_lock);
  103. nr_node_for_each(nr_nodet, &nr_node_list) {
  104. nr_node_lock(nr_nodet);
  105. for (i = 0; i < nr_nodet->count; i++)
  106. if (nr_nodet->routes[i].neighbour == nr_neigh)
  107. if (i < nr_nodet->which)
  108. nr_nodet->which = i;
  109. nr_node_unlock(nr_nodet);
  110. }
  111. spin_unlock_bh(&nr_node_list_lock);
  112. }
  113. if (nr_neigh != NULL)
  114. nr_neigh->failed = 0;
  115. if (quality == 0 && nr_neigh != NULL && nr_node != NULL) {
  116. nr_neigh_put(nr_neigh);
  117. nr_node_put(nr_node);
  118. return 0;
  119. }
  120. if (nr_neigh == NULL) {
  121. if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL) {
  122. if (nr_node)
  123. nr_node_put(nr_node);
  124. return -ENOMEM;
  125. }
  126. nr_neigh->callsign = *ax25;
  127. nr_neigh->digipeat = NULL;
  128. nr_neigh->ax25 = NULL;
  129. nr_neigh->dev = dev;
  130. nr_neigh->quality = sysctl_netrom_default_path_quality;
  131. nr_neigh->locked = 0;
  132. nr_neigh->count = 0;
  133. nr_neigh->number = nr_neigh_no++;
  134. nr_neigh->failed = 0;
  135. atomic_set(&nr_neigh->refcount, 1);
  136. if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
  137. nr_neigh->digipeat = kmemdup(ax25_digi,
  138. sizeof(*ax25_digi),
  139. GFP_KERNEL);
  140. if (nr_neigh->digipeat == NULL) {
  141. kfree(nr_neigh);
  142. if (nr_node)
  143. nr_node_put(nr_node);
  144. return -ENOMEM;
  145. }
  146. }
  147. spin_lock_bh(&nr_neigh_list_lock);
  148. hlist_add_head(&nr_neigh->neigh_node, &nr_neigh_list);
  149. nr_neigh_hold(nr_neigh);
  150. spin_unlock_bh(&nr_neigh_list_lock);
  151. }
  152. if (quality != 0 && ax25cmp(nr, ax25) == 0 && !nr_neigh->locked)
  153. nr_neigh->quality = quality;
  154. if (nr_node == NULL) {
  155. if ((nr_node = kmalloc(sizeof(*nr_node), GFP_ATOMIC)) == NULL) {
  156. if (nr_neigh)
  157. nr_neigh_put(nr_neigh);
  158. return -ENOMEM;
  159. }
  160. nr_node->callsign = *nr;
  161. strcpy(nr_node->mnemonic, mnemonic);
  162. nr_node->which = 0;
  163. nr_node->count = 1;
  164. atomic_set(&nr_node->refcount, 1);
  165. spin_lock_init(&nr_node->node_lock);
  166. nr_node->routes[0].quality = quality;
  167. nr_node->routes[0].obs_count = obs_count;
  168. nr_node->routes[0].neighbour = nr_neigh;
  169. nr_neigh_hold(nr_neigh);
  170. nr_neigh->count++;
  171. spin_lock_bh(&nr_node_list_lock);
  172. hlist_add_head(&nr_node->node_node, &nr_node_list);
  173. /* refcount initialized at 1 */
  174. spin_unlock_bh(&nr_node_list_lock);
  175. return 0;
  176. }
  177. nr_node_lock(nr_node);
  178. if (quality != 0)
  179. strcpy(nr_node->mnemonic, mnemonic);
  180. for (found = 0, i = 0; i < nr_node->count; i++) {
  181. if (nr_node->routes[i].neighbour == nr_neigh) {
  182. nr_node->routes[i].quality = quality;
  183. nr_node->routes[i].obs_count = obs_count;
  184. found = 1;
  185. break;
  186. }
  187. }
  188. if (!found) {
  189. /* We have space at the bottom, slot it in */
  190. if (nr_node->count < 3) {
  191. nr_node->routes[2] = nr_node->routes[1];
  192. nr_node->routes[1] = nr_node->routes[0];
  193. nr_node->routes[0].quality = quality;
  194. nr_node->routes[0].obs_count = obs_count;
  195. nr_node->routes[0].neighbour = nr_neigh;
  196. nr_node->which++;
  197. nr_node->count++;
  198. nr_neigh_hold(nr_neigh);
  199. nr_neigh->count++;
  200. } else {
  201. /* It must be better than the worst */
  202. if (quality > nr_node->routes[2].quality) {
  203. nr_node->routes[2].neighbour->count--;
  204. nr_neigh_put(nr_node->routes[2].neighbour);
  205. if (nr_node->routes[2].neighbour->count == 0 && !nr_node->routes[2].neighbour->locked)
  206. nr_remove_neigh(nr_node->routes[2].neighbour);
  207. nr_node->routes[2].quality = quality;
  208. nr_node->routes[2].obs_count = obs_count;
  209. nr_node->routes[2].neighbour = nr_neigh;
  210. nr_neigh_hold(nr_neigh);
  211. nr_neigh->count++;
  212. }
  213. }
  214. }
  215. /* Now re-sort the routes in quality order */
  216. switch (nr_node->count) {
  217. case 3:
  218. if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
  219. switch (nr_node->which) {
  220. case 0:
  221. nr_node->which = 1;
  222. break;
  223. case 1:
  224. nr_node->which = 0;
  225. 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 *nodet;
  412. int i;
  413. spin_lock_bh(&nr_node_list_lock);
  414. nr_node_for_each_safe(s, 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. /* Fallthrough */
  431. case 1:
  432. s->routes[1] = s->routes[2];
  433. case 2:
  434. break;
  435. }
  436. break;
  437. default:
  438. s->routes[i].obs_count--;
  439. break;
  440. }
  441. }
  442. if (s->count <= 0)
  443. nr_remove_node_locked(s);
  444. nr_node_unlock(s);
  445. }
  446. spin_unlock_bh(&nr_node_list_lock);
  447. return 0;
  448. }
  449. /*
  450. * A device has been removed. Remove its routes and neighbours.
  451. */
  452. void nr_rt_device_down(struct net_device *dev)
  453. {
  454. struct nr_neigh *s;
  455. struct hlist_node *nodet, *node2t;
  456. struct nr_node *t;
  457. int i;
  458. spin_lock_bh(&nr_neigh_list_lock);
  459. nr_neigh_for_each_safe(s, nodet, &nr_neigh_list) {
  460. if (s->dev == dev) {
  461. spin_lock_bh(&nr_node_list_lock);
  462. nr_node_for_each_safe(t, node2t, &nr_node_list) {
  463. nr_node_lock(t);
  464. for (i = 0; i < t->count; i++) {
  465. if (t->routes[i].neighbour == s) {
  466. t->count--;
  467. switch (i) {
  468. case 0:
  469. t->routes[0] = t->routes[1];
  470. case 1:
  471. t->routes[1] = t->routes[2];
  472. case 2:
  473. break;
  474. }
  475. }
  476. }
  477. if (t->count <= 0)
  478. nr_remove_node_locked(t);
  479. nr_node_unlock(t);
  480. }
  481. spin_unlock_bh(&nr_node_list_lock);
  482. nr_remove_neigh_locked(s);
  483. }
  484. }
  485. spin_unlock_bh(&nr_neigh_list_lock);
  486. }
  487. /*
  488. * Check that the device given is a valid AX.25 interface that is "up".
  489. * Or a valid ethernet interface with an AX.25 callsign binding.
  490. */
  491. static struct net_device *nr_ax25_dev_get(char *devname)
  492. {
  493. struct net_device *dev;
  494. if ((dev = dev_get_by_name(&init_net, devname)) == NULL)
  495. return NULL;
  496. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
  497. return dev;
  498. dev_put(dev);
  499. return NULL;
  500. }
  501. /*
  502. * Find the first active NET/ROM device, usually "nr0".
  503. */
  504. struct net_device *nr_dev_first(void)
  505. {
  506. struct net_device *dev, *first = NULL;
  507. rcu_read_lock();
  508. for_each_netdev_rcu(&init_net, dev) {
  509. if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM)
  510. if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
  511. first = dev;
  512. }
  513. if (first)
  514. dev_hold(first);
  515. rcu_read_unlock();
  516. return first;
  517. }
  518. /*
  519. * Find the NET/ROM device for the given callsign.
  520. */
  521. struct net_device *nr_dev_get(ax25_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_NETROM &&
  527. ax25cmp(addr, (ax25_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 ax25_digi *nr_call_to_digi(ax25_digi *digi, int ndigis,
  538. ax25_address *digipeaters)
  539. {
  540. int i;
  541. if (ndigis == 0)
  542. return NULL;
  543. for (i = 0; i < ndigis; i++) {
  544. digi->calls[i] = digipeaters[i];
  545. digi->repeated[i] = 0;
  546. }
  547. digi->ndigi = ndigis;
  548. digi->lastrepeat = -1;
  549. return digi;
  550. }
  551. /*
  552. * Handle the ioctls that control the routing functions.
  553. */
  554. int nr_rt_ioctl(unsigned int cmd, void __user *arg)
  555. {
  556. struct nr_route_struct nr_route;
  557. struct net_device *dev;
  558. ax25_digi digi;
  559. int ret;
  560. switch (cmd) {
  561. case SIOCADDRT:
  562. if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
  563. return -EFAULT;
  564. if (nr_route.ndigis > AX25_MAX_DIGIS)
  565. return -EINVAL;
  566. if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
  567. return -EINVAL;
  568. switch (nr_route.type) {
  569. case NETROM_NODE:
  570. if (strnlen(nr_route.mnemonic, 7) == 7) {
  571. ret = -EINVAL;
  572. break;
  573. }
  574. ret = nr_add_node(&nr_route.callsign,
  575. nr_route.mnemonic,
  576. &nr_route.neighbour,
  577. nr_call_to_digi(&digi, nr_route.ndigis,
  578. nr_route.digipeaters),
  579. dev, nr_route.quality,
  580. nr_route.obs_count);
  581. break;
  582. case NETROM_NEIGH:
  583. ret = nr_add_neigh(&nr_route.callsign,
  584. nr_call_to_digi(&digi, nr_route.ndigis,
  585. nr_route.digipeaters),
  586. dev, nr_route.quality);
  587. break;
  588. default:
  589. ret = -EINVAL;
  590. }
  591. dev_put(dev);
  592. return ret;
  593. case SIOCDELRT:
  594. if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
  595. return -EFAULT;
  596. if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
  597. return -EINVAL;
  598. switch (nr_route.type) {
  599. case NETROM_NODE:
  600. ret = nr_del_node(&nr_route.callsign,
  601. &nr_route.neighbour, dev);
  602. break;
  603. case NETROM_NEIGH:
  604. ret = nr_del_neigh(&nr_route.callsign,
  605. dev, nr_route.quality);
  606. break;
  607. default:
  608. ret = -EINVAL;
  609. }
  610. dev_put(dev);
  611. return ret;
  612. case SIOCNRDECOBS:
  613. return nr_dec_obs();
  614. default:
  615. return -EINVAL;
  616. }
  617. return 0;
  618. }
  619. /*
  620. * A level 2 link has timed out, therefore it appears to be a poor link,
  621. * then don't use that neighbour until it is reset.
  622. */
  623. void nr_link_failed(ax25_cb *ax25, int reason)
  624. {
  625. struct nr_neigh *s, *nr_neigh = NULL;
  626. struct nr_node *nr_node = NULL;
  627. spin_lock_bh(&nr_neigh_list_lock);
  628. nr_neigh_for_each(s, &nr_neigh_list) {
  629. if (s->ax25 == ax25) {
  630. nr_neigh_hold(s);
  631. nr_neigh = s;
  632. break;
  633. }
  634. }
  635. spin_unlock_bh(&nr_neigh_list_lock);
  636. if (nr_neigh == NULL)
  637. return;
  638. nr_neigh->ax25 = NULL;
  639. ax25_cb_put(ax25);
  640. if (++nr_neigh->failed < sysctl_netrom_link_fails_count) {
  641. nr_neigh_put(nr_neigh);
  642. return;
  643. }
  644. spin_lock_bh(&nr_node_list_lock);
  645. nr_node_for_each(nr_node, &nr_node_list) {
  646. nr_node_lock(nr_node);
  647. if (nr_node->which < nr_node->count &&
  648. nr_node->routes[nr_node->which].neighbour == nr_neigh)
  649. nr_node->which++;
  650. nr_node_unlock(nr_node);
  651. }
  652. spin_unlock_bh(&nr_node_list_lock);
  653. nr_neigh_put(nr_neigh);
  654. }
  655. /*
  656. * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
  657. * indicates an internally generated frame.
  658. */
  659. int nr_route_frame(struct sk_buff *skb, ax25_cb *ax25)
  660. {
  661. ax25_address *nr_src, *nr_dest;
  662. struct nr_neigh *nr_neigh;
  663. struct nr_node *nr_node;
  664. struct net_device *dev;
  665. unsigned char *dptr;
  666. ax25_cb *ax25s;
  667. int ret;
  668. struct sk_buff *skbn;
  669. nr_src = (ax25_address *)(skb->data + 0);
  670. nr_dest = (ax25_address *)(skb->data + 7);
  671. if (ax25 != NULL) {
  672. ret = nr_add_node(nr_src, "", &ax25->dest_addr, ax25->digipeat,
  673. ax25->ax25_dev->dev, 0,
  674. sysctl_netrom_obsolescence_count_initialiser);
  675. if (ret)
  676. return ret;
  677. }
  678. if ((dev = nr_dev_get(nr_dest)) != NULL) { /* Its for me */
  679. if (ax25 == NULL) /* Its from me */
  680. ret = nr_loopback_queue(skb);
  681. else
  682. ret = nr_rx_frame(skb, dev);
  683. dev_put(dev);
  684. return ret;
  685. }
  686. if (!sysctl_netrom_routing_control && ax25 != NULL)
  687. return 0;
  688. /* Its Time-To-Live has expired */
  689. if (skb->data[14] == 1) {
  690. return 0;
  691. }
  692. nr_node = nr_node_get(nr_dest);
  693. if (nr_node == NULL)
  694. return 0;
  695. nr_node_lock(nr_node);
  696. if (nr_node->which >= nr_node->count) {
  697. nr_node_unlock(nr_node);
  698. nr_node_put(nr_node);
  699. return 0;
  700. }
  701. nr_neigh = nr_node->routes[nr_node->which].neighbour;
  702. if ((dev = nr_dev_first()) == NULL) {
  703. nr_node_unlock(nr_node);
  704. nr_node_put(nr_node);
  705. return 0;
  706. }
  707. /* We are going to change the netrom headers so we should get our
  708. own skb, we also did not know until now how much header space
  709. we had to reserve... - RXQ */
  710. if ((skbn=skb_copy_expand(skb, dev->hard_header_len, 0, GFP_ATOMIC)) == NULL) {
  711. nr_node_unlock(nr_node);
  712. nr_node_put(nr_node);
  713. dev_put(dev);
  714. return 0;
  715. }
  716. kfree_skb(skb);
  717. skb=skbn;
  718. skb->data[14]--;
  719. dptr = skb_push(skb, 1);
  720. *dptr = AX25_P_NETROM;
  721. ax25s = nr_neigh->ax25;
  722. nr_neigh->ax25 = ax25_send_frame(skb, 256,
  723. (ax25_address *)dev->dev_addr,
  724. &nr_neigh->callsign,
  725. nr_neigh->digipeat, nr_neigh->dev);
  726. if (ax25s)
  727. ax25_cb_put(ax25s);
  728. dev_put(dev);
  729. ret = (nr_neigh->ax25 != NULL);
  730. nr_node_unlock(nr_node);
  731. nr_node_put(nr_node);
  732. return ret;
  733. }
  734. #ifdef CONFIG_PROC_FS
  735. static void *nr_node_start(struct seq_file *seq, loff_t *pos)
  736. {
  737. spin_lock_bh(&nr_node_list_lock);
  738. return seq_hlist_start_head(&nr_node_list, *pos);
  739. }
  740. static void *nr_node_next(struct seq_file *seq, void *v, loff_t *pos)
  741. {
  742. return seq_hlist_next(v, &nr_node_list, pos);
  743. }
  744. static void nr_node_stop(struct seq_file *seq, void *v)
  745. {
  746. spin_unlock_bh(&nr_node_list_lock);
  747. }
  748. static int nr_node_show(struct seq_file *seq, void *v)
  749. {
  750. char buf[11];
  751. int i;
  752. if (v == SEQ_START_TOKEN)
  753. seq_puts(seq,
  754. "callsign mnemonic w n qual obs neigh qual obs neigh qual obs neigh\n");
  755. else {
  756. struct nr_node *nr_node = hlist_entry(v, struct nr_node,
  757. node_node);
  758. nr_node_lock(nr_node);
  759. seq_printf(seq, "%-9s %-7s %d %d",
  760. ax2asc(buf, &nr_node->callsign),
  761. (nr_node->mnemonic[0] == '\0') ? "*" : nr_node->mnemonic,
  762. nr_node->which + 1,
  763. nr_node->count);
  764. for (i = 0; i < nr_node->count; i++) {
  765. seq_printf(seq, " %3d %d %05d",
  766. nr_node->routes[i].quality,
  767. nr_node->routes[i].obs_count,
  768. nr_node->routes[i].neighbour->number);
  769. }
  770. nr_node_unlock(nr_node);
  771. seq_puts(seq, "\n");
  772. }
  773. return 0;
  774. }
  775. static const struct seq_operations nr_node_seqops = {
  776. .start = nr_node_start,
  777. .next = nr_node_next,
  778. .stop = nr_node_stop,
  779. .show = nr_node_show,
  780. };
  781. static int nr_node_info_open(struct inode *inode, struct file *file)
  782. {
  783. return seq_open(file, &nr_node_seqops);
  784. }
  785. const struct file_operations nr_nodes_fops = {
  786. .owner = THIS_MODULE,
  787. .open = nr_node_info_open,
  788. .read = seq_read,
  789. .llseek = seq_lseek,
  790. .release = seq_release,
  791. };
  792. static void *nr_neigh_start(struct seq_file *seq, loff_t *pos)
  793. {
  794. spin_lock_bh(&nr_neigh_list_lock);
  795. return seq_hlist_start_head(&nr_neigh_list, *pos);
  796. }
  797. static void *nr_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
  798. {
  799. return seq_hlist_next(v, &nr_neigh_list, pos);
  800. }
  801. static void nr_neigh_stop(struct seq_file *seq, void *v)
  802. {
  803. spin_unlock_bh(&nr_neigh_list_lock);
  804. }
  805. static int nr_neigh_show(struct seq_file *seq, void *v)
  806. {
  807. char buf[11];
  808. int i;
  809. if (v == SEQ_START_TOKEN)
  810. seq_puts(seq, "addr callsign dev qual lock count failed digipeaters\n");
  811. else {
  812. struct nr_neigh *nr_neigh;
  813. nr_neigh = hlist_entry(v, struct nr_neigh, neigh_node);
  814. seq_printf(seq, "%05d %-9s %-4s %3d %d %3d %3d",
  815. nr_neigh->number,
  816. ax2asc(buf, &nr_neigh->callsign),
  817. nr_neigh->dev ? nr_neigh->dev->name : "???",
  818. nr_neigh->quality,
  819. nr_neigh->locked,
  820. nr_neigh->count,
  821. nr_neigh->failed);
  822. if (nr_neigh->digipeat != NULL) {
  823. for (i = 0; i < nr_neigh->digipeat->ndigi; i++)
  824. seq_printf(seq, " %s",
  825. ax2asc(buf, &nr_neigh->digipeat->calls[i]));
  826. }
  827. seq_puts(seq, "\n");
  828. }
  829. return 0;
  830. }
  831. static const struct seq_operations nr_neigh_seqops = {
  832. .start = nr_neigh_start,
  833. .next = nr_neigh_next,
  834. .stop = nr_neigh_stop,
  835. .show = nr_neigh_show,
  836. };
  837. static int nr_neigh_info_open(struct inode *inode, struct file *file)
  838. {
  839. return seq_open(file, &nr_neigh_seqops);
  840. }
  841. const struct file_operations nr_neigh_fops = {
  842. .owner = THIS_MODULE,
  843. .open = nr_neigh_info_open,
  844. .read = seq_read,
  845. .llseek = seq_lseek,
  846. .release = seq_release,
  847. };
  848. #endif
  849. /*
  850. * Free all memory associated with the nodes and routes lists.
  851. */
  852. void __exit nr_rt_free(void)
  853. {
  854. struct nr_neigh *s = NULL;
  855. struct nr_node *t = NULL;
  856. struct hlist_node *nodet;
  857. spin_lock_bh(&nr_neigh_list_lock);
  858. spin_lock_bh(&nr_node_list_lock);
  859. nr_node_for_each_safe(t, nodet, &nr_node_list) {
  860. nr_node_lock(t);
  861. nr_remove_node_locked(t);
  862. nr_node_unlock(t);
  863. }
  864. nr_neigh_for_each_safe(s, nodet, &nr_neigh_list) {
  865. while(s->count) {
  866. s->count--;
  867. nr_neigh_put(s);
  868. }
  869. nr_remove_neigh_locked(s);
  870. }
  871. spin_unlock_bh(&nr_node_list_lock);
  872. spin_unlock_bh(&nr_neigh_list_lock);
  873. }