Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / net / ipv6 / ip6_fib.c
blobbe1d688584faf66e26960d8648c8c4dd8e75e0ea
1 /*
2 * Linux INET6 implementation
3 * Forwarding Information Database
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: ip6_fib.c,v 1.25 2001/10/31 21:55:55 davem Exp $
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.
17 * Changes:
18 * Yuji SEKIYA @USAGI: Support default route on router node;
19 * remove ip6_null_entry from the top of
20 * routing table.
22 #include <linux/config.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/net.h>
26 #include <linux/route.h>
27 #include <linux/netdevice.h>
28 #include <linux/in6.h>
29 #include <linux/init.h>
31 #ifdef CONFIG_PROC_FS
32 #include <linux/proc_fs.h>
33 #endif
35 #include <net/ipv6.h>
36 #include <net/ndisc.h>
37 #include <net/addrconf.h>
39 #include <net/ip6_fib.h>
40 #include <net/ip6_route.h>
42 #define RT6_DEBUG 2
43 #undef CONFIG_IPV6_SUBTREES
45 #if RT6_DEBUG >= 3
46 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
47 #else
48 #define RT6_TRACE(x...) do { ; } while (0)
49 #endif
51 struct rt6_statistics rt6_stats;
53 static kmem_cache_t * fib6_node_kmem;
55 enum fib_walk_state_t
57 #ifdef CONFIG_IPV6_SUBTREES
58 FWS_S,
59 #endif
60 FWS_L,
61 FWS_R,
62 FWS_C,
63 FWS_U
66 struct fib6_cleaner_t
68 struct fib6_walker_t w;
69 int (*func)(struct rt6_info *, void *arg);
70 void *arg;
73 rwlock_t fib6_walker_lock = RW_LOCK_UNLOCKED;
76 #ifdef CONFIG_IPV6_SUBTREES
77 #define FWS_INIT FWS_S
78 #define SUBTREE(fn) ((fn)->subtree)
79 #else
80 #define FWS_INIT FWS_L
81 #define SUBTREE(fn) NULL
82 #endif
84 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
85 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
88 * A routing update causes an increase of the serial number on the
89 * afected subtree. This allows for cached routes to be asynchronously
90 * tested when modifications are made to the destination cache as a
91 * result of redirects, path MTU changes, etc.
94 static __u32 rt_sernum = 0;
96 static struct timer_list ip6_fib_timer = TIMER_INITIALIZER(fib6_run_gc, 0, 0);
98 static struct fib6_walker_t fib6_walker_list = {
99 &fib6_walker_list, &fib6_walker_list,
102 #define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
104 static __inline__ u32 fib6_new_sernum(void)
106 u32 n = ++rt_sernum;
107 if ((__s32)n <= 0)
108 rt_sernum = n = 1;
109 return n;
113 * Auxiliary address test functions for the radix tree.
115 * These assume a 32bit processor (although it will work on
116 * 64bit processors)
120 * compare "prefix length" bits of an address
123 static __inline__ int addr_match(void *token1, void *token2, int prefixlen)
125 __u32 *a1 = token1;
126 __u32 *a2 = token2;
127 int pdw;
128 int pbi;
130 pdw = prefixlen >> 5; /* num of whole __u32 in prefix */
131 pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */
133 if (pdw)
134 if (memcmp(a1, a2, pdw << 2))
135 return 0;
137 if (pbi) {
138 __u32 mask;
140 mask = htonl((0xffffffff) << (32 - pbi));
142 if ((a1[pdw] ^ a2[pdw]) & mask)
143 return 0;
146 return 1;
150 * test bit
153 static __inline__ int addr_bit_set(void *token, int fn_bit)
155 __u32 *addr = token;
157 return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
161 * find the first different bit between two addresses
162 * length of address must be a multiple of 32bits
165 static __inline__ int addr_diff(void *token1, void *token2, int addrlen)
167 __u32 *a1 = token1;
168 __u32 *a2 = token2;
169 int i;
171 addrlen >>= 2;
173 for (i = 0; i < addrlen; i++) {
174 __u32 xb;
176 xb = a1[i] ^ a2[i];
178 if (xb) {
179 int j = 31;
181 xb = ntohl(xb);
183 while ((xb & (1 << j)) == 0)
184 j--;
186 return (i * 32 + 31 - j);
191 * we should *never* get to this point since that
192 * would mean the addrs are equal
194 * However, we do get to it 8) And exacly, when
195 * addresses are equal 8)
197 * ip route add 1111::/128 via ...
198 * ip route add 1111::/64 via ...
199 * and we are here.
201 * Ideally, this function should stop comparison
202 * at prefix length. It does not, but it is still OK,
203 * if returned value is greater than prefix length.
204 * --ANK (980803)
207 return addrlen<<5;
210 static __inline__ struct fib6_node * node_alloc(void)
212 struct fib6_node *fn;
214 if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
215 memset(fn, 0, sizeof(struct fib6_node));
217 return fn;
220 static __inline__ void node_free(struct fib6_node * fn)
222 kmem_cache_free(fib6_node_kmem, fn);
225 static __inline__ void rt6_release(struct rt6_info *rt)
227 if (atomic_dec_and_test(&rt->rt6i_ref))
228 dst_free(&rt->u.dst);
233 * Routing Table
235 * return the apropriate node for a routing tree "add" operation
236 * by either creating and inserting or by returning an existing
237 * node.
240 static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
241 int addrlen, int plen,
242 int offset)
244 struct fib6_node *fn, *in, *ln;
245 struct fib6_node *pn = NULL;
246 struct rt6key *key;
247 int bit;
248 int dir = 0;
249 __u32 sernum = fib6_new_sernum();
251 RT6_TRACE("fib6_add_1\n");
253 /* insert node in tree */
255 fn = root;
257 do {
258 key = (struct rt6key *)((u8 *)fn->leaf + offset);
261 * Prefix match
263 if (plen < fn->fn_bit ||
264 !addr_match(&key->addr, addr, fn->fn_bit))
265 goto insert_above;
268 * Exact match ?
271 if (plen == fn->fn_bit) {
272 /* clean up an intermediate node */
273 if ((fn->fn_flags & RTN_RTINFO) == 0) {
274 rt6_release(fn->leaf);
275 fn->leaf = NULL;
278 fn->fn_sernum = sernum;
280 return fn;
284 * We have more bits to go
287 /* Try to walk down on tree. */
288 fn->fn_sernum = sernum;
289 dir = addr_bit_set(addr, fn->fn_bit);
290 pn = fn;
291 fn = dir ? fn->right: fn->left;
292 } while (fn);
295 * We walked to the bottom of tree.
296 * Create new leaf node without children.
299 ln = node_alloc();
301 if (ln == NULL)
302 return NULL;
303 ln->fn_bit = plen;
305 ln->parent = pn;
306 ln->fn_sernum = sernum;
308 if (dir)
309 pn->right = ln;
310 else
311 pn->left = ln;
313 return ln;
316 insert_above:
318 * split since we don't have a common prefix anymore or
319 * we have a less significant route.
320 * we've to insert an intermediate node on the list
321 * this new node will point to the one we need to create
322 * and the current
325 pn = fn->parent;
327 /* find 1st bit in difference between the 2 addrs.
329 See comment in addr_diff: bit may be an invalid value,
330 but if it is >= plen, the value is ignored in any case.
333 bit = addr_diff(addr, &key->addr, addrlen);
336 * (intermediate)[in]
337 * / \
338 * (new leaf node)[ln] (old node)[fn]
340 if (plen > bit) {
341 in = node_alloc();
342 ln = node_alloc();
344 if (in == NULL || ln == NULL) {
345 if (in)
346 node_free(in);
347 if (ln)
348 node_free(ln);
349 return NULL;
353 * new intermediate node.
354 * RTN_RTINFO will
355 * be off since that an address that chooses one of
356 * the branches would not match less specific routes
357 * in the other branch
360 in->fn_bit = bit;
362 in->parent = pn;
363 in->leaf = fn->leaf;
364 atomic_inc(&in->leaf->rt6i_ref);
366 in->fn_sernum = sernum;
368 /* update parent pointer */
369 if (dir)
370 pn->right = in;
371 else
372 pn->left = in;
374 ln->fn_bit = plen;
376 ln->parent = in;
377 fn->parent = in;
379 ln->fn_sernum = sernum;
381 if (addr_bit_set(addr, bit)) {
382 in->right = ln;
383 in->left = fn;
384 } else {
385 in->left = ln;
386 in->right = fn;
388 } else { /* plen <= bit */
391 * (new leaf node)[ln]
392 * / \
393 * (old node)[fn] NULL
396 ln = node_alloc();
398 if (ln == NULL)
399 return NULL;
401 ln->fn_bit = plen;
403 ln->parent = pn;
405 ln->fn_sernum = sernum;
407 if (dir)
408 pn->right = ln;
409 else
410 pn->left = ln;
412 if (addr_bit_set(&key->addr, plen))
413 ln->right = fn;
414 else
415 ln->left = fn;
417 fn->parent = ln;
419 return ln;
423 * Insert routing information in a node.
426 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt)
428 struct rt6_info *iter = NULL;
429 struct rt6_info **ins;
431 ins = &fn->leaf;
433 if (fn->fn_flags&RTN_TL_ROOT &&
434 fn->leaf == &ip6_null_entry &&
435 !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF | RTF_ALLONLINK)) ){
437 * The top fib of ip6 routing table includes ip6_null_entry.
439 fn->leaf = rt;
440 rt->u.next = NULL;
441 goto out;
444 for (iter = fn->leaf; iter; iter=iter->u.next) {
446 * Search for duplicates
449 if (iter->rt6i_metric == rt->rt6i_metric) {
451 * Same priority level
454 if ((iter->rt6i_dev == rt->rt6i_dev) &&
455 (ipv6_addr_cmp(&iter->rt6i_gateway,
456 &rt->rt6i_gateway) == 0)) {
457 if (!(iter->rt6i_flags&RTF_EXPIRES))
458 return -EEXIST;
459 iter->rt6i_expires = rt->rt6i_expires;
460 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
461 iter->rt6i_flags &= ~RTF_EXPIRES;
462 iter->rt6i_expires = 0;
464 return -EEXIST;
468 if (iter->rt6i_metric > rt->rt6i_metric)
469 break;
471 ins = &iter->u.next;
475 * insert node
478 out:
479 rt->u.next = iter;
480 *ins = rt;
481 rt->rt6i_node = fn;
482 atomic_inc(&rt->rt6i_ref);
483 inet6_rt_notify(RTM_NEWROUTE, rt);
484 rt6_stats.fib_rt_entries++;
486 if ((fn->fn_flags & RTN_RTINFO) == 0) {
487 rt6_stats.fib_route_nodes++;
488 fn->fn_flags |= RTN_RTINFO;
491 return 0;
494 static __inline__ void fib6_start_gc(struct rt6_info *rt)
496 if (ip6_fib_timer.expires == 0 &&
497 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
498 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
502 * Add routing information to the routing tree.
503 * <destination addr>/<source addr>
504 * with source addr info in sub-trees
507 int fib6_add(struct fib6_node *root, struct rt6_info *rt)
509 struct fib6_node *fn;
510 int err = -ENOMEM;
512 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
513 rt->rt6i_dst.plen, (u8*) &rt->rt6i_dst - (u8*) rt);
515 if (fn == NULL)
516 goto out;
518 #ifdef CONFIG_IPV6_SUBTREES
519 if (rt->rt6i_src.plen) {
520 struct fib6_node *sn;
522 if (fn->subtree == NULL) {
523 struct fib6_node *sfn;
526 * Create subtree.
528 * fn[main tree]
530 * sfn[subtree root]
532 * sn[new leaf node]
535 /* Create subtree root node */
536 sfn = node_alloc();
537 if (sfn == NULL)
538 goto st_failure;
540 sfn->leaf = &ip6_null_entry;
541 atomic_inc(&ip6_null_entry.rt6i_ref);
542 sfn->fn_flags = RTN_ROOT;
543 sfn->fn_sernum = fib6_new_sernum();
545 /* Now add the first leaf node to new subtree */
547 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
548 sizeof(struct in6_addr), rt->rt6i_src.plen,
549 (u8*) &rt->rt6i_src - (u8*) rt);
551 if (sn == NULL) {
552 /* If it is failed, discard just allocated
553 root, and then (in st_failure) stale node
554 in main tree.
556 node_free(sfn);
557 goto st_failure;
560 /* Now link new subtree to main tree */
561 sfn->parent = fn;
562 fn->subtree = sfn;
563 if (fn->leaf == NULL) {
564 fn->leaf = rt;
565 atomic_inc(&rt->rt6i_ref);
567 } else {
568 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
569 sizeof(struct in6_addr), rt->rt6i_src.plen,
570 (u8*) &rt->rt6i_src - (u8*) rt);
572 if (sn == NULL)
573 goto st_failure;
576 fn = sn;
578 #endif
580 err = fib6_add_rt2node(fn, rt);
582 if (err == 0) {
583 fib6_start_gc(rt);
584 if (!(rt->rt6i_flags&RTF_CACHE))
585 fib6_prune_clones(fn, rt);
588 out:
589 if (err)
590 dst_free(&rt->u.dst);
591 return err;
593 #ifdef CONFIG_IPV6_SUBTREES
594 /* Subtree creation failed, probably main tree node
595 is orphan. If it is, shot it.
597 st_failure:
598 if (fn && !(fn->fn_flags&RTN_RTINFO|RTN_ROOT))
599 fib_repair_tree(fn);
600 dst_free(&rt->u.dst);
601 return err;
602 #endif
606 * Routing tree lookup
610 struct lookup_args {
611 int offset; /* key offset on rt6_info */
612 struct in6_addr *addr; /* search key */
615 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
616 struct lookup_args *args)
618 struct fib6_node *fn;
619 int dir;
622 * Descend on a tree
625 fn = root;
627 for (;;) {
628 struct fib6_node *next;
630 dir = addr_bit_set(args->addr, fn->fn_bit);
632 next = dir ? fn->right : fn->left;
634 if (next) {
635 fn = next;
636 continue;
639 break;
642 while ((fn->fn_flags & RTN_ROOT) == 0) {
643 #ifdef CONFIG_IPV6_SUBTREES
644 if (fn->subtree) {
645 struct fib6_node *st;
646 struct lookup_args *narg;
648 narg = args + 1;
650 if (narg->addr) {
651 st = fib6_lookup_1(fn->subtree, narg);
653 if (st && !(st->fn_flags & RTN_ROOT))
654 return st;
657 #endif
659 if (fn->fn_flags & RTN_RTINFO) {
660 struct rt6key *key;
662 key = (struct rt6key *) ((u8 *) fn->leaf +
663 args->offset);
665 if (addr_match(&key->addr, args->addr, key->plen))
666 return fn;
669 fn = fn->parent;
672 return NULL;
675 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
676 struct in6_addr *saddr)
678 struct lookup_args args[2];
679 struct rt6_info *rt = NULL;
680 struct fib6_node *fn;
682 args[0].offset = (u8*) &rt->rt6i_dst - (u8*) rt;
683 args[0].addr = daddr;
685 #ifdef CONFIG_IPV6_SUBTREES
686 args[1].offset = (u8*) &rt->rt6i_src - (u8*) rt;
687 args[1].addr = saddr;
688 #endif
690 fn = fib6_lookup_1(root, args);
692 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
693 fn = root;
695 return fn;
699 * Get node with sepciafied destination prefix (and source prefix,
700 * if subtrees are used)
704 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
705 struct in6_addr *addr,
706 int plen, int offset)
708 struct fib6_node *fn;
710 for (fn = root; fn ; ) {
711 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
714 * Prefix match
716 if (plen < fn->fn_bit ||
717 !addr_match(&key->addr, addr, fn->fn_bit))
718 return NULL;
720 if (plen == fn->fn_bit)
721 return fn;
724 * We have more bits to go
726 if (addr_bit_set(addr, fn->fn_bit))
727 fn = fn->right;
728 else
729 fn = fn->left;
731 return NULL;
734 struct fib6_node * fib6_locate(struct fib6_node *root,
735 struct in6_addr *daddr, int dst_len,
736 struct in6_addr *saddr, int src_len)
738 struct rt6_info *rt = NULL;
739 struct fib6_node *fn;
741 fn = fib6_locate_1(root, daddr, dst_len,
742 (u8*) &rt->rt6i_dst - (u8*) rt);
744 #ifdef CONFIG_IPV6_SUBTREES
745 if (src_len) {
746 BUG_TRAP(saddr!=NULL);
747 if (fn == NULL)
748 fn = fn->subtree;
749 if (fn)
750 fn = fib6_locate_1(fn, saddr, src_len,
751 (u8*) &rt->rt6i_src - (u8*) rt);
753 #endif
755 if (fn && fn->fn_flags&RTN_RTINFO)
756 return fn;
758 return NULL;
763 * Deletion
767 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
769 if (fn->fn_flags&RTN_ROOT)
770 return &ip6_null_entry;
772 while(fn) {
773 if(fn->left)
774 return fn->left->leaf;
776 if(fn->right)
777 return fn->right->leaf;
779 fn = SUBTREE(fn);
781 return NULL;
785 * Called to trim the tree of intermediate nodes when possible. "fn"
786 * is the node we want to try and remove.
789 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
791 int children;
792 int nstate;
793 struct fib6_node *child, *pn;
794 struct fib6_walker_t *w;
795 int iter = 0;
797 for (;;) {
798 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
799 iter++;
801 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
802 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
803 BUG_TRAP(fn->leaf==NULL);
805 children = 0;
806 child = NULL;
807 if (fn->right) child = fn->right, children |= 1;
808 if (fn->left) child = fn->left, children |= 2;
810 if (children == 3 || SUBTREE(fn)
811 #ifdef CONFIG_IPV6_SUBTREES
812 /* Subtree root (i.e. fn) may have one child */
813 || (children && fn->fn_flags&RTN_ROOT)
814 #endif
816 fn->leaf = fib6_find_prefix(fn);
817 #if RT6_DEBUG >= 2
818 if (fn->leaf==NULL) {
819 BUG_TRAP(fn->leaf);
820 fn->leaf = &ip6_null_entry;
822 #endif
823 atomic_inc(&fn->leaf->rt6i_ref);
824 return fn->parent;
827 pn = fn->parent;
828 #ifdef CONFIG_IPV6_SUBTREES
829 if (SUBTREE(pn) == fn) {
830 BUG_TRAP(fn->fn_flags&RTN_ROOT);
831 SUBTREE(pn) = NULL;
832 nstate = FWS_L;
833 } else {
834 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
835 #endif
836 if (pn->right == fn) pn->right = child;
837 else if (pn->left == fn) pn->left = child;
838 #if RT6_DEBUG >= 2
839 else BUG_TRAP(0);
840 #endif
841 if (child)
842 child->parent = pn;
843 nstate = FWS_R;
844 #ifdef CONFIG_IPV6_SUBTREES
846 #endif
848 read_lock(&fib6_walker_lock);
849 FOR_WALKERS(w) {
850 if (child == NULL) {
851 if (w->root == fn) {
852 w->root = w->node = NULL;
853 RT6_TRACE("W %p adjusted by delroot 1\n", w);
854 } else if (w->node == fn) {
855 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
856 w->node = pn;
857 w->state = nstate;
859 } else {
860 if (w->root == fn) {
861 w->root = child;
862 RT6_TRACE("W %p adjusted by delroot 2\n", w);
864 if (w->node == fn) {
865 w->node = child;
866 if (children&2) {
867 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
868 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
869 } else {
870 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
871 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
876 read_unlock(&fib6_walker_lock);
878 node_free(fn);
879 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
880 return pn;
882 rt6_release(pn->leaf);
883 pn->leaf = NULL;
884 fn = pn;
888 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp)
890 struct fib6_walker_t *w;
891 struct rt6_info *rt = *rtp;
893 RT6_TRACE("fib6_del_route\n");
895 /* Unlink it */
896 *rtp = rt->u.next;
897 rt->rt6i_node = NULL;
898 rt6_stats.fib_rt_entries--;
900 /* Adjust walkers */
901 read_lock(&fib6_walker_lock);
902 FOR_WALKERS(w) {
903 if (w->state == FWS_C && w->leaf == rt) {
904 RT6_TRACE("walker %p adjusted by delroute\n", w);
905 w->leaf = rt->u.next;
906 if (w->leaf == NULL)
907 w->state = FWS_U;
910 read_unlock(&fib6_walker_lock);
912 rt->u.next = NULL;
914 if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
915 fn->leaf = &ip6_null_entry;
917 /* If it was last route, expunge its radix tree node */
918 if (fn->leaf == NULL) {
919 fn->fn_flags &= ~RTN_RTINFO;
920 rt6_stats.fib_route_nodes--;
921 fn = fib6_repair_tree(fn);
924 if (atomic_read(&rt->rt6i_ref) != 1) {
925 /* This route is used as dummy address holder in some split
926 * nodes. It is not leaked, but it still holds other resources,
927 * which must be released in time. So, scan ascendant nodes
928 * and replace dummy references to this route with references
929 * to still alive ones.
931 while (fn) {
932 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
933 fn->leaf = fib6_find_prefix(fn);
934 atomic_inc(&fn->leaf->rt6i_ref);
935 rt6_release(rt);
937 fn = fn->parent;
939 /* No more references are possiible at this point. */
940 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
943 inet6_rt_notify(RTM_DELROUTE, rt);
944 rt6_release(rt);
947 int fib6_del(struct rt6_info *rt)
949 struct fib6_node *fn = rt->rt6i_node;
950 struct rt6_info **rtp;
952 #if RT6_DEBUG >= 2
953 if (rt->u.dst.obsolete>0) {
954 BUG_TRAP(fn==NULL || rt->u.dst.obsolete<=0);
955 return -ENOENT;
957 #endif
958 if (fn == NULL || rt == &ip6_null_entry)
959 return -ENOENT;
961 BUG_TRAP(fn->fn_flags&RTN_RTINFO);
963 if (!(rt->rt6i_flags&RTF_CACHE))
964 fib6_prune_clones(fn, rt);
967 * Walk the leaf entries looking for ourself
970 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
971 if (*rtp == rt) {
972 fib6_del_route(fn, rtp);
973 return 0;
976 return -ENOENT;
980 * Tree transversal function.
982 * Certainly, it is not interrupt safe.
983 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
984 * It means, that we can modify tree during walking
985 * and use this function for garbage collection, clone pruning,
986 * cleaning tree when a device goes down etc. etc.
988 * It guarantees that every node will be traversed,
989 * and that it will be traversed only once.
991 * Callback function w->func may return:
992 * 0 -> continue walking.
993 * positive value -> walking is suspended (used by tree dumps,
994 * and probably by gc, if it will be split to several slices)
995 * negative value -> terminate walking.
997 * The function itself returns:
998 * 0 -> walk is complete.
999 * >0 -> walk is incomplete (i.e. suspended)
1000 * <0 -> walk is terminated by an error.
1003 int fib6_walk_continue(struct fib6_walker_t *w)
1005 struct fib6_node *fn, *pn;
1007 for (;;) {
1008 fn = w->node;
1009 if (fn == NULL)
1010 return 0;
1012 if (w->prune && fn != w->root &&
1013 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
1014 w->state = FWS_C;
1015 w->leaf = fn->leaf;
1017 switch (w->state) {
1018 #ifdef CONFIG_IPV6_SUBTREES
1019 case FWS_S:
1020 if (SUBTREE(fn)) {
1021 w->node = SUBTREE(fn);
1022 continue;
1024 w->state = FWS_L;
1025 #endif
1026 case FWS_L:
1027 if (fn->left) {
1028 w->node = fn->left;
1029 w->state = FWS_INIT;
1030 continue;
1032 w->state = FWS_R;
1033 case FWS_R:
1034 if (fn->right) {
1035 w->node = fn->right;
1036 w->state = FWS_INIT;
1037 continue;
1039 w->state = FWS_C;
1040 w->leaf = fn->leaf;
1041 case FWS_C:
1042 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1043 int err = w->func(w);
1044 if (err)
1045 return err;
1046 continue;
1048 w->state = FWS_U;
1049 case FWS_U:
1050 if (fn == w->root)
1051 return 0;
1052 pn = fn->parent;
1053 w->node = pn;
1054 #ifdef CONFIG_IPV6_SUBTREES
1055 if (SUBTREE(pn) == fn) {
1056 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1057 w->state = FWS_L;
1058 continue;
1060 #endif
1061 if (pn->left == fn) {
1062 w->state = FWS_R;
1063 continue;
1065 if (pn->right == fn) {
1066 w->state = FWS_C;
1067 w->leaf = w->node->leaf;
1068 continue;
1070 #if RT6_DEBUG >= 2
1071 BUG_TRAP(0);
1072 #endif
1077 int fib6_walk(struct fib6_walker_t *w)
1079 int res;
1081 w->state = FWS_INIT;
1082 w->node = w->root;
1084 fib6_walker_link(w);
1085 res = fib6_walk_continue(w);
1086 if (res <= 0)
1087 fib6_walker_unlink(w);
1088 return res;
1091 static int fib6_clean_node(struct fib6_walker_t *w)
1093 int res;
1094 struct rt6_info *rt;
1095 struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1097 for (rt = w->leaf; rt; rt = rt->u.next) {
1098 res = c->func(rt, c->arg);
1099 if (res < 0) {
1100 w->leaf = rt;
1101 res = fib6_del(rt);
1102 if (res) {
1103 #if RT6_DEBUG >= 2
1104 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1105 #endif
1106 continue;
1108 return 0;
1110 BUG_TRAP(res==0);
1112 w->leaf = rt;
1113 return 0;
1117 * Convenient frontend to tree walker.
1119 * func is called on each route.
1120 * It may return -1 -> delete this route.
1121 * 0 -> continue walking
1123 * prune==1 -> only immediate children of node (certainly,
1124 * ignoring pure split nodes) will be scanned.
1127 void fib6_clean_tree(struct fib6_node *root,
1128 int (*func)(struct rt6_info *, void *arg),
1129 int prune, void *arg)
1131 struct fib6_cleaner_t c;
1133 c.w.root = root;
1134 c.w.func = fib6_clean_node;
1135 c.w.prune = prune;
1136 c.func = func;
1137 c.arg = arg;
1139 fib6_walk(&c.w);
1142 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1144 if (rt->rt6i_flags & RTF_CACHE) {
1145 RT6_TRACE("pruning clone %p\n", rt);
1146 return -1;
1149 return 0;
1152 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1154 fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1158 * Garbage collection
1161 static struct fib6_gc_args
1163 int timeout;
1164 int more;
1165 } gc_args;
1167 static int fib6_age(struct rt6_info *rt, void *arg)
1169 unsigned long now = jiffies;
1172 * check addrconf expiration here.
1173 * Routes are expired even if they are in use.
1175 * Also age clones. Note, that clones are aged out
1176 * only if they are not in use now.
1179 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1180 if ((long)(now - rt->rt6i_expires) > 0) {
1181 RT6_TRACE("expiring %p\n", rt);
1182 return -1;
1184 gc_args.more++;
1185 } else if (rt->rt6i_flags & RTF_CACHE) {
1186 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1187 (long)(now - rt->u.dst.lastuse) >= gc_args.timeout) {
1188 RT6_TRACE("aging clone %p\n", rt);
1189 return -1;
1191 gc_args.more++;
1194 return 0;
1197 static spinlock_t fib6_gc_lock = SPIN_LOCK_UNLOCKED;
1199 void fib6_run_gc(unsigned long dummy)
1201 if (dummy != ~0UL) {
1202 spin_lock_bh(&fib6_gc_lock);
1203 gc_args.timeout = (int)dummy;
1204 } else {
1205 local_bh_disable();
1206 if (!spin_trylock(&fib6_gc_lock)) {
1207 mod_timer(&ip6_fib_timer, jiffies + HZ);
1208 local_bh_enable();
1209 return;
1211 gc_args.timeout = ip6_rt_gc_interval;
1213 gc_args.more = 0;
1216 write_lock_bh(&rt6_lock);
1217 fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
1218 write_unlock_bh(&rt6_lock);
1220 if (gc_args.more)
1221 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1222 else {
1223 del_timer(&ip6_fib_timer);
1224 ip6_fib_timer.expires = 0;
1226 spin_unlock_bh(&fib6_gc_lock);
1229 void __init fib6_init(void)
1231 if (!fib6_node_kmem)
1232 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1233 sizeof(struct fib6_node),
1234 0, SLAB_HWCACHE_ALIGN,
1235 NULL, NULL);
1238 #ifdef MODULE
1239 void fib6_gc_cleanup(void)
1241 del_timer(&ip6_fib_timer);
1243 #endif