ip6_fib.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. * Linux INET6 implementation
  3. * Forwarding Information Database
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * $Id: ip6_fib.c,v 1.25 2001/10/31 21:55:55 davem Exp $
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /*
  16. * Changes:
  17. * Yuji SEKIYA @USAGI: Support default route on router node;
  18. * remove ip6_null_entry from the top of
  19. * routing table.
  20. */
  21. #include <linux/errno.h>
  22. #include <linux/types.h>
  23. #include <linux/net.h>
  24. #include <linux/route.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/in6.h>
  27. #include <linux/init.h>
  28. #ifdef CONFIG_PROC_FS
  29. #include <linux/proc_fs.h>
  30. #endif
  31. #include <net/ipv6.h>
  32. #include <net/ndisc.h>
  33. #include <net/addrconf.h>
  34. #include <net/ip6_fib.h>
  35. #include <net/ip6_route.h>
  36. #define RT6_DEBUG 2
  37. #if RT6_DEBUG >= 3
  38. #define RT6_TRACE(x...) printk(KERN_DEBUG x)
  39. #else
  40. #define RT6_TRACE(x...) do { ; } while (0)
  41. #endif
  42. struct rt6_statistics rt6_stats;
  43. static kmem_cache_t * fib6_node_kmem __read_mostly;
  44. enum fib_walk_state_t
  45. {
  46. #ifdef CONFIG_IPV6_SUBTREES
  47. FWS_S,
  48. #endif
  49. FWS_L,
  50. FWS_R,
  51. FWS_C,
  52. FWS_U
  53. };
  54. struct fib6_cleaner_t
  55. {
  56. struct fib6_walker_t w;
  57. int (*func)(struct rt6_info *, void *arg);
  58. void *arg;
  59. };
  60. DEFINE_RWLOCK(fib6_walker_lock);
  61. #ifdef CONFIG_IPV6_SUBTREES
  62. #define FWS_INIT FWS_S
  63. #define SUBTREE(fn) ((fn)->subtree)
  64. #else
  65. #define FWS_INIT FWS_L
  66. #define SUBTREE(fn) NULL
  67. #endif
  68. static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
  69. static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
  70. /*
  71. * A routing update causes an increase of the serial number on the
  72. * affected subtree. This allows for cached routes to be asynchronously
  73. * tested when modifications are made to the destination cache as a
  74. * result of redirects, path MTU changes, etc.
  75. */
  76. static __u32 rt_sernum;
  77. static DEFINE_TIMER(ip6_fib_timer, fib6_run_gc, 0, 0);
  78. struct fib6_walker_t fib6_walker_list = {
  79. .prev = &fib6_walker_list,
  80. .next = &fib6_walker_list,
  81. };
  82. #define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
  83. static __inline__ u32 fib6_new_sernum(void)
  84. {
  85. u32 n = ++rt_sernum;
  86. if ((__s32)n <= 0)
  87. rt_sernum = n = 1;
  88. return n;
  89. }
  90. /*
  91. * Auxiliary address test functions for the radix tree.
  92. *
  93. * These assume a 32bit processor (although it will work on
  94. * 64bit processors)
  95. */
  96. /*
  97. * test bit
  98. */
  99. static __inline__ int addr_bit_set(void *token, int fn_bit)
  100. {
  101. __u32 *addr = token;
  102. return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
  103. }
  104. static __inline__ struct fib6_node * node_alloc(void)
  105. {
  106. struct fib6_node *fn;
  107. if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
  108. memset(fn, 0, sizeof(struct fib6_node));
  109. return fn;
  110. }
  111. static __inline__ void node_free(struct fib6_node * fn)
  112. {
  113. kmem_cache_free(fib6_node_kmem, fn);
  114. }
  115. static __inline__ void rt6_release(struct rt6_info *rt)
  116. {
  117. if (atomic_dec_and_test(&rt->rt6i_ref))
  118. dst_free(&rt->u.dst);
  119. }
  120. /*
  121. * Routing Table
  122. *
  123. * return the appropriate node for a routing tree "add" operation
  124. * by either creating and inserting or by returning an existing
  125. * node.
  126. */
  127. static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
  128. int addrlen, int plen,
  129. int offset)
  130. {
  131. struct fib6_node *fn, *in, *ln;
  132. struct fib6_node *pn = NULL;
  133. struct rt6key *key;
  134. int bit;
  135. int dir = 0;
  136. __u32 sernum = fib6_new_sernum();
  137. RT6_TRACE("fib6_add_1\n");
  138. /* insert node in tree */
  139. fn = root;
  140. do {
  141. key = (struct rt6key *)((u8 *)fn->leaf + offset);
  142. /*
  143. * Prefix match
  144. */
  145. if (plen < fn->fn_bit ||
  146. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  147. goto insert_above;
  148. /*
  149. * Exact match ?
  150. */
  151. if (plen == fn->fn_bit) {
  152. /* clean up an intermediate node */
  153. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  154. rt6_release(fn->leaf);
  155. fn->leaf = NULL;
  156. }
  157. fn->fn_sernum = sernum;
  158. return fn;
  159. }
  160. /*
  161. * We have more bits to go
  162. */
  163. /* Try to walk down on tree. */
  164. fn->fn_sernum = sernum;
  165. dir = addr_bit_set(addr, fn->fn_bit);
  166. pn = fn;
  167. fn = dir ? fn->right: fn->left;
  168. } while (fn);
  169. /*
  170. * We walked to the bottom of tree.
  171. * Create new leaf node without children.
  172. */
  173. ln = node_alloc();
  174. if (ln == NULL)
  175. return NULL;
  176. ln->fn_bit = plen;
  177. ln->parent = pn;
  178. ln->fn_sernum = sernum;
  179. if (dir)
  180. pn->right = ln;
  181. else
  182. pn->left = ln;
  183. return ln;
  184. insert_above:
  185. /*
  186. * split since we don't have a common prefix anymore or
  187. * we have a less significant route.
  188. * we've to insert an intermediate node on the list
  189. * this new node will point to the one we need to create
  190. * and the current
  191. */
  192. pn = fn->parent;
  193. /* find 1st bit in difference between the 2 addrs.
  194. See comment in __ipv6_addr_diff: bit may be an invalid value,
  195. but if it is >= plen, the value is ignored in any case.
  196. */
  197. bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
  198. /*
  199. * (intermediate)[in]
  200. * / \
  201. * (new leaf node)[ln] (old node)[fn]
  202. */
  203. if (plen > bit) {
  204. in = node_alloc();
  205. ln = node_alloc();
  206. if (in == NULL || ln == NULL) {
  207. if (in)
  208. node_free(in);
  209. if (ln)
  210. node_free(ln);
  211. return NULL;
  212. }
  213. /*
  214. * new intermediate node.
  215. * RTN_RTINFO will
  216. * be off since that an address that chooses one of
  217. * the branches would not match less specific routes
  218. * in the other branch
  219. */
  220. in->fn_bit = bit;
  221. in->parent = pn;
  222. in->leaf = fn->leaf;
  223. atomic_inc(&in->leaf->rt6i_ref);
  224. in->fn_sernum = sernum;
  225. /* update parent pointer */
  226. if (dir)
  227. pn->right = in;
  228. else
  229. pn->left = in;
  230. ln->fn_bit = plen;
  231. ln->parent = in;
  232. fn->parent = in;
  233. ln->fn_sernum = sernum;
  234. if (addr_bit_set(addr, bit)) {
  235. in->right = ln;
  236. in->left = fn;
  237. } else {
  238. in->left = ln;
  239. in->right = fn;
  240. }
  241. } else { /* plen <= bit */
  242. /*
  243. * (new leaf node)[ln]
  244. * / \
  245. * (old node)[fn] NULL
  246. */
  247. ln = node_alloc();
  248. if (ln == NULL)
  249. return NULL;
  250. ln->fn_bit = plen;
  251. ln->parent = pn;
  252. ln->fn_sernum = sernum;
  253. if (dir)
  254. pn->right = ln;
  255. else
  256. pn->left = ln;
  257. if (addr_bit_set(&key->addr, plen))
  258. ln->right = fn;
  259. else
  260. ln->left = fn;
  261. fn->parent = ln;
  262. }
  263. return ln;
  264. }
  265. /*
  266. * Insert routing information in a node.
  267. */
  268. static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
  269. struct nlmsghdr *nlh, struct netlink_skb_parms *req)
  270. {
  271. struct rt6_info *iter = NULL;
  272. struct rt6_info **ins;
  273. ins = &fn->leaf;
  274. if (fn->fn_flags&RTN_TL_ROOT &&
  275. fn->leaf == &ip6_null_entry &&
  276. !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
  277. fn->leaf = rt;
  278. rt->u.next = NULL;
  279. goto out;
  280. }
  281. for (iter = fn->leaf; iter; iter=iter->u.next) {
  282. /*
  283. * Search for duplicates
  284. */
  285. if (iter->rt6i_metric == rt->rt6i_metric) {
  286. /*
  287. * Same priority level
  288. */
  289. if (iter->rt6i_dev == rt->rt6i_dev &&
  290. iter->rt6i_idev == rt->rt6i_idev &&
  291. ipv6_addr_equal(&iter->rt6i_gateway,
  292. &rt->rt6i_gateway)) {
  293. if (!(iter->rt6i_flags&RTF_EXPIRES))
  294. return -EEXIST;
  295. iter->rt6i_expires = rt->rt6i_expires;
  296. if (!(rt->rt6i_flags&RTF_EXPIRES)) {
  297. iter->rt6i_flags &= ~RTF_EXPIRES;
  298. iter->rt6i_expires = 0;
  299. }
  300. return -EEXIST;
  301. }
  302. }
  303. if (iter->rt6i_metric > rt->rt6i_metric)
  304. break;
  305. ins = &iter->u.next;
  306. }
  307. /*
  308. * insert node
  309. */
  310. out:
  311. rt->u.next = iter;
  312. *ins = rt;
  313. rt->rt6i_node = fn;
  314. atomic_inc(&rt->rt6i_ref);
  315. inet6_rt_notify(RTM_NEWROUTE, rt, nlh, req);
  316. rt6_stats.fib_rt_entries++;
  317. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  318. rt6_stats.fib_route_nodes++;
  319. fn->fn_flags |= RTN_RTINFO;
  320. }
  321. return 0;
  322. }
  323. static __inline__ void fib6_start_gc(struct rt6_info *rt)
  324. {
  325. if (ip6_fib_timer.expires == 0 &&
  326. (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
  327. mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
  328. }
  329. void fib6_force_start_gc(void)
  330. {
  331. if (ip6_fib_timer.expires == 0)
  332. mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
  333. }
  334. /*
  335. * Add routing information to the routing tree.
  336. * <destination addr>/<source addr>
  337. * with source addr info in sub-trees
  338. */
  339. int fib6_add(struct fib6_node *root, struct rt6_info *rt,
  340. struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
  341. {
  342. struct fib6_node *fn;
  343. int err = -ENOMEM;
  344. fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
  345. rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
  346. if (fn == NULL)
  347. goto out;
  348. #ifdef CONFIG_IPV6_SUBTREES
  349. if (rt->rt6i_src.plen) {
  350. struct fib6_node *sn;
  351. if (fn->subtree == NULL) {
  352. struct fib6_node *sfn;
  353. /*
  354. * Create subtree.
  355. *
  356. * fn[main tree]
  357. * |
  358. * sfn[subtree root]
  359. * \
  360. * sn[new leaf node]
  361. */
  362. /* Create subtree root node */
  363. sfn = node_alloc();
  364. if (sfn == NULL)
  365. goto st_failure;
  366. sfn->leaf = &ip6_null_entry;
  367. atomic_inc(&ip6_null_entry.rt6i_ref);
  368. sfn->fn_flags = RTN_ROOT;
  369. sfn->fn_sernum = fib6_new_sernum();
  370. /* Now add the first leaf node to new subtree */
  371. sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
  372. sizeof(struct in6_addr), rt->rt6i_src.plen,
  373. offsetof(struct rt6_info, rt6i_src));
  374. if (sn == NULL) {
  375. /* If it is failed, discard just allocated
  376. root, and then (in st_failure) stale node
  377. in main tree.
  378. */
  379. node_free(sfn);
  380. goto st_failure;
  381. }
  382. /* Now link new subtree to main tree */
  383. sfn->parent = fn;
  384. fn->subtree = sfn;
  385. if (fn->leaf == NULL) {
  386. fn->leaf = rt;
  387. atomic_inc(&rt->rt6i_ref);
  388. }
  389. } else {
  390. sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
  391. sizeof(struct in6_addr), rt->rt6i_src.plen,
  392. offsetof(struct rt6_info, rt6i_src));
  393. if (sn == NULL)
  394. goto st_failure;
  395. }
  396. fn = sn;
  397. }
  398. #endif
  399. err = fib6_add_rt2node(fn, rt, nlh, req);
  400. if (err == 0) {
  401. fib6_start_gc(rt);
  402. if (!(rt->rt6i_flags&RTF_CACHE))
  403. fib6_prune_clones(fn, rt);
  404. }
  405. out:
  406. if (err)
  407. dst_free(&rt->u.dst);
  408. return err;
  409. #ifdef CONFIG_IPV6_SUBTREES
  410. /* Subtree creation failed, probably main tree node
  411. is orphan. If it is, shoot it.
  412. */
  413. st_failure:
  414. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  415. fib6_repair_tree(fn);
  416. dst_free(&rt->u.dst);
  417. return err;
  418. #endif
  419. }
  420. /*
  421. * Routing tree lookup
  422. *
  423. */
  424. struct lookup_args {
  425. int offset; /* key offset on rt6_info */
  426. struct in6_addr *addr; /* search key */
  427. };
  428. static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
  429. struct lookup_args *args)
  430. {
  431. struct fib6_node *fn;
  432. int dir;
  433. /*
  434. * Descend on a tree
  435. */
  436. fn = root;
  437. for (;;) {
  438. struct fib6_node *next;
  439. dir = addr_bit_set(args->addr, fn->fn_bit);
  440. next = dir ? fn->right : fn->left;
  441. if (next) {
  442. fn = next;
  443. continue;
  444. }
  445. break;
  446. }
  447. while ((fn->fn_flags & RTN_ROOT) == 0) {
  448. #ifdef CONFIG_IPV6_SUBTREES
  449. if (fn->subtree) {
  450. struct fib6_node *st;
  451. struct lookup_args *narg;
  452. narg = args + 1;
  453. if (narg->addr) {
  454. st = fib6_lookup_1(fn->subtree, narg);
  455. if (st && !(st->fn_flags & RTN_ROOT))
  456. return st;
  457. }
  458. }
  459. #endif
  460. if (fn->fn_flags & RTN_RTINFO) {
  461. struct rt6key *key;
  462. key = (struct rt6key *) ((u8 *) fn->leaf +
  463. args->offset);
  464. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
  465. return fn;
  466. }
  467. fn = fn->parent;
  468. }
  469. return NULL;
  470. }
  471. struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
  472. struct in6_addr *saddr)
  473. {
  474. struct lookup_args args[2];
  475. struct fib6_node *fn;
  476. args[0].offset = offsetof(struct rt6_info, rt6i_dst);
  477. args[0].addr = daddr;
  478. #ifdef CONFIG_IPV6_SUBTREES
  479. args[1].offset = offsetof(struct rt6_info, rt6i_src);
  480. args[1].addr = saddr;
  481. #endif
  482. fn = fib6_lookup_1(root, args);
  483. if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
  484. fn = root;
  485. return fn;
  486. }
  487. /*
  488. * Get node with specified destination prefix (and source prefix,
  489. * if subtrees are used)
  490. */
  491. static struct fib6_node * fib6_locate_1(struct fib6_node *root,
  492. struct in6_addr *addr,
  493. int plen, int offset)
  494. {
  495. struct fib6_node *fn;
  496. for (fn = root; fn ; ) {
  497. struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
  498. /*
  499. * Prefix match
  500. */
  501. if (plen < fn->fn_bit ||
  502. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  503. return NULL;
  504. if (plen == fn->fn_bit)
  505. return fn;
  506. /*
  507. * We have more bits to go
  508. */
  509. if (addr_bit_set(addr, fn->fn_bit))
  510. fn = fn->right;
  511. else
  512. fn = fn->left;
  513. }
  514. return NULL;
  515. }
  516. struct fib6_node * fib6_locate(struct fib6_node *root,
  517. struct in6_addr *daddr, int dst_len,
  518. struct in6_addr *saddr, int src_len)
  519. {
  520. struct fib6_node *fn;
  521. fn = fib6_locate_1(root, daddr, dst_len,
  522. offsetof(struct rt6_info, rt6i_dst));
  523. #ifdef CONFIG_IPV6_SUBTREES
  524. if (src_len) {
  525. BUG_TRAP(saddr!=NULL);
  526. if (fn == NULL)
  527. fn = fn->subtree;
  528. if (fn)
  529. fn = fib6_locate_1(fn, saddr, src_len,
  530. offsetof(struct rt6_info, rt6i_src));
  531. }
  532. #endif
  533. if (fn && fn->fn_flags&RTN_RTINFO)
  534. return fn;
  535. return NULL;
  536. }
  537. /*
  538. * Deletion
  539. *
  540. */
  541. static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
  542. {
  543. if (fn->fn_flags&RTN_ROOT)
  544. return &ip6_null_entry;
  545. while(fn) {
  546. if(fn->left)
  547. return fn->left->leaf;
  548. if(fn->right)
  549. return fn->right->leaf;
  550. fn = SUBTREE(fn);
  551. }
  552. return NULL;
  553. }
  554. /*
  555. * Called to trim the tree of intermediate nodes when possible. "fn"
  556. * is the node we want to try and remove.
  557. */
  558. static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
  559. {
  560. int children;
  561. int nstate;
  562. struct fib6_node *child, *pn;
  563. struct fib6_walker_t *w;
  564. int iter = 0;
  565. for (;;) {
  566. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  567. iter++;
  568. BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
  569. BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
  570. BUG_TRAP(fn->leaf==NULL);
  571. children = 0;
  572. child = NULL;
  573. if (fn->right) child = fn->right, children |= 1;
  574. if (fn->left) child = fn->left, children |= 2;
  575. if (children == 3 || SUBTREE(fn)
  576. #ifdef CONFIG_IPV6_SUBTREES
  577. /* Subtree root (i.e. fn) may have one child */
  578. || (children && fn->fn_flags&RTN_ROOT)
  579. #endif
  580. ) {
  581. fn->leaf = fib6_find_prefix(fn);
  582. #if RT6_DEBUG >= 2
  583. if (fn->leaf==NULL) {
  584. BUG_TRAP(fn->leaf);
  585. fn->leaf = &ip6_null_entry;
  586. }
  587. #endif
  588. atomic_inc(&fn->leaf->rt6i_ref);
  589. return fn->parent;
  590. }
  591. pn = fn->parent;
  592. #ifdef CONFIG_IPV6_SUBTREES
  593. if (SUBTREE(pn) == fn) {
  594. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  595. SUBTREE(pn) = NULL;
  596. nstate = FWS_L;
  597. } else {
  598. BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
  599. #endif
  600. if (pn->right == fn) pn->right = child;
  601. else if (pn->left == fn) pn->left = child;
  602. #if RT6_DEBUG >= 2
  603. else BUG_TRAP(0);
  604. #endif
  605. if (child)
  606. child->parent = pn;
  607. nstate = FWS_R;
  608. #ifdef CONFIG_IPV6_SUBTREES
  609. }
  610. #endif
  611. read_lock(&fib6_walker_lock);
  612. FOR_WALKERS(w) {
  613. if (child == NULL) {
  614. if (w->root == fn) {
  615. w->root = w->node = NULL;
  616. RT6_TRACE("W %p adjusted by delroot 1\n", w);
  617. } else if (w->node == fn) {
  618. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  619. w->node = pn;
  620. w->state = nstate;
  621. }
  622. } else {
  623. if (w->root == fn) {
  624. w->root = child;
  625. RT6_TRACE("W %p adjusted by delroot 2\n", w);
  626. }
  627. if (w->node == fn) {
  628. w->node = child;
  629. if (children&2) {
  630. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  631. w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
  632. } else {
  633. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  634. w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
  635. }
  636. }
  637. }
  638. }
  639. read_unlock(&fib6_walker_lock);
  640. node_free(fn);
  641. if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
  642. return pn;
  643. rt6_release(pn->leaf);
  644. pn->leaf = NULL;
  645. fn = pn;
  646. }
  647. }
  648. static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
  649. struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
  650. {
  651. struct fib6_walker_t *w;
  652. struct rt6_info *rt = *rtp;
  653. RT6_TRACE("fib6_del_route\n");
  654. /* Unlink it */
  655. *rtp = rt->u.next;
  656. rt->rt6i_node = NULL;
  657. rt6_stats.fib_rt_entries--;
  658. rt6_stats.fib_discarded_routes++;
  659. /* Adjust walkers */
  660. read_lock(&fib6_walker_lock);
  661. FOR_WALKERS(w) {
  662. if (w->state == FWS_C && w->leaf == rt) {
  663. RT6_TRACE("walker %p adjusted by delroute\n", w);
  664. w->leaf = rt->u.next;
  665. if (w->leaf == NULL)
  666. w->state = FWS_U;
  667. }
  668. }
  669. read_unlock(&fib6_walker_lock);
  670. rt->u.next = NULL;
  671. if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
  672. fn->leaf = &ip6_null_entry;
  673. /* If it was last route, expunge its radix tree node */
  674. if (fn->leaf == NULL) {
  675. fn->fn_flags &= ~RTN_RTINFO;
  676. rt6_stats.fib_route_nodes--;
  677. fn = fib6_repair_tree(fn);
  678. }
  679. if (atomic_read(&rt->rt6i_ref) != 1) {
  680. /* This route is used as dummy address holder in some split
  681. * nodes. It is not leaked, but it still holds other resources,
  682. * which must be released in time. So, scan ascendant nodes
  683. * and replace dummy references to this route with references
  684. * to still alive ones.
  685. */
  686. while (fn) {
  687. if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
  688. fn->leaf = fib6_find_prefix(fn);
  689. atomic_inc(&fn->leaf->rt6i_ref);
  690. rt6_release(rt);
  691. }
  692. fn = fn->parent;
  693. }
  694. /* No more references are possible at this point. */
  695. if (atomic_read(&rt->rt6i_ref) != 1) BUG();
  696. }
  697. inet6_rt_notify(RTM_DELROUTE, rt, nlh, req);
  698. rt6_release(rt);
  699. }
  700. int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
  701. {
  702. struct fib6_node *fn = rt->rt6i_node;
  703. struct rt6_info **rtp;
  704. #if RT6_DEBUG >= 2
  705. if (rt->u.dst.obsolete>0) {
  706. BUG_TRAP(fn==NULL);
  707. return -ENOENT;
  708. }
  709. #endif
  710. if (fn == NULL || rt == &ip6_null_entry)
  711. return -ENOENT;
  712. BUG_TRAP(fn->fn_flags&RTN_RTINFO);
  713. if (!(rt->rt6i_flags&RTF_CACHE))
  714. fib6_prune_clones(fn, rt);
  715. /*
  716. * Walk the leaf entries looking for ourself
  717. */
  718. for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
  719. if (*rtp == rt) {
  720. fib6_del_route(fn, rtp, nlh, _rtattr, req);
  721. return 0;
  722. }
  723. }
  724. return -ENOENT;
  725. }
  726. /*
  727. * Tree traversal function.
  728. *
  729. * Certainly, it is not interrupt safe.
  730. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  731. * It means, that we can modify tree during walking
  732. * and use this function for garbage collection, clone pruning,
  733. * cleaning tree when a device goes down etc. etc.
  734. *
  735. * It guarantees that every node will be traversed,
  736. * and that it will be traversed only once.
  737. *
  738. * Callback function w->func may return:
  739. * 0 -> continue walking.
  740. * positive value -> walking is suspended (used by tree dumps,
  741. * and probably by gc, if it will be split to several slices)
  742. * negative value -> terminate walking.
  743. *
  744. * The function itself returns:
  745. * 0 -> walk is complete.
  746. * >0 -> walk is incomplete (i.e. suspended)
  747. * <0 -> walk is terminated by an error.
  748. */
  749. int fib6_walk_continue(struct fib6_walker_t *w)
  750. {
  751. struct fib6_node *fn, *pn;
  752. for (;;) {
  753. fn = w->node;
  754. if (fn == NULL)
  755. return 0;
  756. if (w->prune && fn != w->root &&
  757. fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
  758. w->state = FWS_C;
  759. w->leaf = fn->leaf;
  760. }
  761. switch (w->state) {
  762. #ifdef CONFIG_IPV6_SUBTREES
  763. case FWS_S:
  764. if (SUBTREE(fn)) {
  765. w->node = SUBTREE(fn);
  766. continue;
  767. }
  768. w->state = FWS_L;
  769. #endif
  770. case FWS_L:
  771. if (fn->left) {
  772. w->node = fn->left;
  773. w->state = FWS_INIT;
  774. continue;
  775. }
  776. w->state = FWS_R;
  777. case FWS_R:
  778. if (fn->right) {
  779. w->node = fn->right;
  780. w->state = FWS_INIT;
  781. continue;
  782. }
  783. w->state = FWS_C;
  784. w->leaf = fn->leaf;
  785. case FWS_C:
  786. if (w->leaf && fn->fn_flags&RTN_RTINFO) {
  787. int err = w->func(w);
  788. if (err)
  789. return err;
  790. continue;
  791. }
  792. w->state = FWS_U;
  793. case FWS_U:
  794. if (fn == w->root)
  795. return 0;
  796. pn = fn->parent;
  797. w->node = pn;
  798. #ifdef CONFIG_IPV6_SUBTREES
  799. if (SUBTREE(pn) == fn) {
  800. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  801. w->state = FWS_L;
  802. continue;
  803. }
  804. #endif
  805. if (pn->left == fn) {
  806. w->state = FWS_R;
  807. continue;
  808. }
  809. if (pn->right == fn) {
  810. w->state = FWS_C;
  811. w->leaf = w->node->leaf;
  812. continue;
  813. }
  814. #if RT6_DEBUG >= 2
  815. BUG_TRAP(0);
  816. #endif
  817. }
  818. }
  819. }
  820. int fib6_walk(struct fib6_walker_t *w)
  821. {
  822. int res;
  823. w->state = FWS_INIT;
  824. w->node = w->root;
  825. fib6_walker_link(w);
  826. res = fib6_walk_continue(w);
  827. if (res <= 0)
  828. fib6_walker_unlink(w);
  829. return res;
  830. }
  831. static int fib6_clean_node(struct fib6_walker_t *w)
  832. {
  833. int res;
  834. struct rt6_info *rt;
  835. struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
  836. for (rt = w->leaf; rt; rt = rt->u.next) {
  837. res = c->func(rt, c->arg);
  838. if (res < 0) {
  839. w->leaf = rt;
  840. res = fib6_del(rt, NULL, NULL, NULL);
  841. if (res) {
  842. #if RT6_DEBUG >= 2
  843. printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
  844. #endif
  845. continue;
  846. }
  847. return 0;
  848. }
  849. BUG_TRAP(res==0);
  850. }
  851. w->leaf = rt;
  852. return 0;
  853. }
  854. /*
  855. * Convenient frontend to tree walker.
  856. *
  857. * func is called on each route.
  858. * It may return -1 -> delete this route.
  859. * 0 -> continue walking
  860. *
  861. * prune==1 -> only immediate children of node (certainly,
  862. * ignoring pure split nodes) will be scanned.
  863. */
  864. void fib6_clean_tree(struct fib6_node *root,
  865. int (*func)(struct rt6_info *, void *arg),
  866. int prune, void *arg)
  867. {
  868. struct fib6_cleaner_t c;
  869. c.w.root = root;
  870. c.w.func = fib6_clean_node;
  871. c.w.prune = prune;
  872. c.func = func;
  873. c.arg = arg;
  874. fib6_walk(&c.w);
  875. }
  876. static int fib6_prune_clone(struct rt6_info *rt, void *arg)
  877. {
  878. if (rt->rt6i_flags & RTF_CACHE) {
  879. RT6_TRACE("pruning clone %p\n", rt);
  880. return -1;
  881. }
  882. return 0;
  883. }
  884. static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
  885. {
  886. fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
  887. }
  888. /*
  889. * Garbage collection
  890. */
  891. static struct fib6_gc_args
  892. {
  893. int timeout;
  894. int more;
  895. } gc_args;
  896. static int fib6_age(struct rt6_info *rt, void *arg)
  897. {
  898. unsigned long now = jiffies;
  899. /*
  900. * check addrconf expiration here.
  901. * Routes are expired even if they are in use.
  902. *
  903. * Also age clones. Note, that clones are aged out
  904. * only if they are not in use now.
  905. */
  906. if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
  907. if (time_after(now, rt->rt6i_expires)) {
  908. RT6_TRACE("expiring %p\n", rt);
  909. return -1;
  910. }
  911. gc_args.more++;
  912. } else if (rt->rt6i_flags & RTF_CACHE) {
  913. if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
  914. time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
  915. RT6_TRACE("aging clone %p\n", rt);
  916. return -1;
  917. } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
  918. (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
  919. RT6_TRACE("purging route %p via non-router but gateway\n",
  920. rt);
  921. return -1;
  922. }
  923. gc_args.more++;
  924. }
  925. return 0;
  926. }
  927. static DEFINE_SPINLOCK(fib6_gc_lock);
  928. void fib6_run_gc(unsigned long dummy)
  929. {
  930. if (dummy != ~0UL) {
  931. spin_lock_bh(&fib6_gc_lock);
  932. gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
  933. } else {
  934. local_bh_disable();
  935. if (!spin_trylock(&fib6_gc_lock)) {
  936. mod_timer(&ip6_fib_timer, jiffies + HZ);
  937. local_bh_enable();
  938. return;
  939. }
  940. gc_args.timeout = ip6_rt_gc_interval;
  941. }
  942. gc_args.more = 0;
  943. write_lock_bh(&rt6_lock);
  944. ndisc_dst_gc(&gc_args.more);
  945. fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
  946. write_unlock_bh(&rt6_lock);
  947. if (gc_args.more)
  948. mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
  949. else {
  950. del_timer(&ip6_fib_timer);
  951. ip6_fib_timer.expires = 0;
  952. }
  953. spin_unlock_bh(&fib6_gc_lock);
  954. }
  955. void __init fib6_init(void)
  956. {
  957. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  958. sizeof(struct fib6_node),
  959. 0, SLAB_HWCACHE_ALIGN,
  960. NULL, NULL);
  961. if (!fib6_node_kmem)
  962. panic("cannot create fib6_nodes cache");
  963. }
  964. void fib6_gc_cleanup(void)
  965. {
  966. del_timer(&ip6_fib_timer);
  967. kmem_cache_destroy(fib6_node_kmem);
  968. }