ip6_fib.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:24k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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