[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / net / ipv6 / ip6_fib.c
blob405740b75abb7f693d66285815ffdd24a9f17740
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
44 #if RT6_DEBUG >= 3
45 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
46 #else
47 #define RT6_TRACE(x...) do { ; } while (0)
48 #endif
50 struct rt6_statistics rt6_stats;
52 static kmem_cache_t * fib6_node_kmem;
54 enum fib_walk_state_t
56 #ifdef CONFIG_IPV6_SUBTREES
57 FWS_S,
58 #endif
59 FWS_L,
60 FWS_R,
61 FWS_C,
62 FWS_U
65 struct fib6_cleaner_t
67 struct fib6_walker_t w;
68 int (*func)(struct rt6_info *, void *arg);
69 void *arg;
72 DEFINE_RWLOCK(fib6_walker_lock);
75 #ifdef CONFIG_IPV6_SUBTREES
76 #define FWS_INIT FWS_S
77 #define SUBTREE(fn) ((fn)->subtree)
78 #else
79 #define FWS_INIT FWS_L
80 #define SUBTREE(fn) NULL
81 #endif
83 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
84 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
87 * A routing update causes an increase of the serial number on the
88 * affected subtree. This allows for cached routes to be asynchronously
89 * tested when modifications are made to the destination cache as a
90 * result of redirects, path MTU changes, etc.
93 static __u32 rt_sernum;
95 static struct timer_list ip6_fib_timer = TIMER_INITIALIZER(fib6_run_gc, 0, 0);
97 struct fib6_walker_t fib6_walker_list = {
98 .prev = &fib6_walker_list,
99 .next = &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 * test bit
123 static __inline__ int addr_bit_set(void *token, int fn_bit)
125 __u32 *addr = token;
127 return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
131 * find the first different bit between two addresses
132 * length of address must be a multiple of 32bits
135 static __inline__ int addr_diff(void *token1, void *token2, int addrlen)
137 __u32 *a1 = token1;
138 __u32 *a2 = token2;
139 int i;
141 addrlen >>= 2;
143 for (i = 0; i < addrlen; i++) {
144 __u32 xb;
146 xb = a1[i] ^ a2[i];
148 if (xb) {
149 int j = 31;
151 xb = ntohl(xb);
153 while ((xb & (1 << j)) == 0)
154 j--;
156 return (i * 32 + 31 - j);
161 * we should *never* get to this point since that
162 * would mean the addrs are equal
164 * However, we do get to it 8) And exacly, when
165 * addresses are equal 8)
167 * ip route add 1111::/128 via ...
168 * ip route add 1111::/64 via ...
169 * and we are here.
171 * Ideally, this function should stop comparison
172 * at prefix length. It does not, but it is still OK,
173 * if returned value is greater than prefix length.
174 * --ANK (980803)
177 return addrlen<<5;
180 static __inline__ struct fib6_node * node_alloc(void)
182 struct fib6_node *fn;
184 if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
185 memset(fn, 0, sizeof(struct fib6_node));
187 return fn;
190 static __inline__ void node_free(struct fib6_node * fn)
192 kmem_cache_free(fib6_node_kmem, fn);
195 static __inline__ void rt6_release(struct rt6_info *rt)
197 if (atomic_dec_and_test(&rt->rt6i_ref))
198 dst_free(&rt->u.dst);
203 * Routing Table
205 * return the appropriate node for a routing tree "add" operation
206 * by either creating and inserting or by returning an existing
207 * node.
210 static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
211 int addrlen, int plen,
212 int offset)
214 struct fib6_node *fn, *in, *ln;
215 struct fib6_node *pn = NULL;
216 struct rt6key *key;
217 int bit;
218 int dir = 0;
219 __u32 sernum = fib6_new_sernum();
221 RT6_TRACE("fib6_add_1\n");
223 /* insert node in tree */
225 fn = root;
227 do {
228 key = (struct rt6key *)((u8 *)fn->leaf + offset);
231 * Prefix match
233 if (plen < fn->fn_bit ||
234 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
235 goto insert_above;
238 * Exact match ?
241 if (plen == fn->fn_bit) {
242 /* clean up an intermediate node */
243 if ((fn->fn_flags & RTN_RTINFO) == 0) {
244 rt6_release(fn->leaf);
245 fn->leaf = NULL;
248 fn->fn_sernum = sernum;
250 return fn;
254 * We have more bits to go
257 /* Try to walk down on tree. */
258 fn->fn_sernum = sernum;
259 dir = addr_bit_set(addr, fn->fn_bit);
260 pn = fn;
261 fn = dir ? fn->right: fn->left;
262 } while (fn);
265 * We walked to the bottom of tree.
266 * Create new leaf node without children.
269 ln = node_alloc();
271 if (ln == NULL)
272 return NULL;
273 ln->fn_bit = plen;
275 ln->parent = pn;
276 ln->fn_sernum = sernum;
278 if (dir)
279 pn->right = ln;
280 else
281 pn->left = ln;
283 return ln;
286 insert_above:
288 * split since we don't have a common prefix anymore or
289 * we have a less significant route.
290 * we've to insert an intermediate node on the list
291 * this new node will point to the one we need to create
292 * and the current
295 pn = fn->parent;
297 /* find 1st bit in difference between the 2 addrs.
299 See comment in addr_diff: bit may be an invalid value,
300 but if it is >= plen, the value is ignored in any case.
303 bit = addr_diff(addr, &key->addr, addrlen);
306 * (intermediate)[in]
307 * / \
308 * (new leaf node)[ln] (old node)[fn]
310 if (plen > bit) {
311 in = node_alloc();
312 ln = node_alloc();
314 if (in == NULL || ln == NULL) {
315 if (in)
316 node_free(in);
317 if (ln)
318 node_free(ln);
319 return NULL;
323 * new intermediate node.
324 * RTN_RTINFO will
325 * be off since that an address that chooses one of
326 * the branches would not match less specific routes
327 * in the other branch
330 in->fn_bit = bit;
332 in->parent = pn;
333 in->leaf = fn->leaf;
334 atomic_inc(&in->leaf->rt6i_ref);
336 in->fn_sernum = sernum;
338 /* update parent pointer */
339 if (dir)
340 pn->right = in;
341 else
342 pn->left = in;
344 ln->fn_bit = plen;
346 ln->parent = in;
347 fn->parent = in;
349 ln->fn_sernum = sernum;
351 if (addr_bit_set(addr, bit)) {
352 in->right = ln;
353 in->left = fn;
354 } else {
355 in->left = ln;
356 in->right = fn;
358 } else { /* plen <= bit */
361 * (new leaf node)[ln]
362 * / \
363 * (old node)[fn] NULL
366 ln = node_alloc();
368 if (ln == NULL)
369 return NULL;
371 ln->fn_bit = plen;
373 ln->parent = pn;
375 ln->fn_sernum = sernum;
377 if (dir)
378 pn->right = ln;
379 else
380 pn->left = ln;
382 if (addr_bit_set(&key->addr, plen))
383 ln->right = fn;
384 else
385 ln->left = fn;
387 fn->parent = ln;
389 return ln;
393 * Insert routing information in a node.
396 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
397 struct nlmsghdr *nlh)
399 struct rt6_info *iter = NULL;
400 struct rt6_info **ins;
402 ins = &fn->leaf;
404 if (fn->fn_flags&RTN_TL_ROOT &&
405 fn->leaf == &ip6_null_entry &&
406 !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
407 fn->leaf = rt;
408 rt->u.next = NULL;
409 goto out;
412 for (iter = fn->leaf; iter; iter=iter->u.next) {
414 * Search for duplicates
417 if (iter->rt6i_metric == rt->rt6i_metric) {
419 * Same priority level
422 if (iter->rt6i_dev == rt->rt6i_dev &&
423 iter->rt6i_idev == rt->rt6i_idev &&
424 ipv6_addr_equal(&iter->rt6i_gateway,
425 &rt->rt6i_gateway)) {
426 if (!(iter->rt6i_flags&RTF_EXPIRES))
427 return -EEXIST;
428 iter->rt6i_expires = rt->rt6i_expires;
429 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
430 iter->rt6i_flags &= ~RTF_EXPIRES;
431 iter->rt6i_expires = 0;
433 return -EEXIST;
437 if (iter->rt6i_metric > rt->rt6i_metric)
438 break;
440 ins = &iter->u.next;
444 * insert node
447 out:
448 rt->u.next = iter;
449 *ins = rt;
450 rt->rt6i_node = fn;
451 atomic_inc(&rt->rt6i_ref);
452 inet6_rt_notify(RTM_NEWROUTE, rt, nlh);
453 rt6_stats.fib_rt_entries++;
455 if ((fn->fn_flags & RTN_RTINFO) == 0) {
456 rt6_stats.fib_route_nodes++;
457 fn->fn_flags |= RTN_RTINFO;
460 return 0;
463 static __inline__ void fib6_start_gc(struct rt6_info *rt)
465 if (ip6_fib_timer.expires == 0 &&
466 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
467 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
470 void fib6_force_start_gc(void)
472 if (ip6_fib_timer.expires == 0)
473 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
477 * Add routing information to the routing tree.
478 * <destination addr>/<source addr>
479 * with source addr info in sub-trees
482 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr)
484 struct fib6_node *fn;
485 int err = -ENOMEM;
487 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
488 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
490 if (fn == NULL)
491 goto out;
493 #ifdef CONFIG_IPV6_SUBTREES
494 if (rt->rt6i_src.plen) {
495 struct fib6_node *sn;
497 if (fn->subtree == NULL) {
498 struct fib6_node *sfn;
501 * Create subtree.
503 * fn[main tree]
505 * sfn[subtree root]
507 * sn[new leaf node]
510 /* Create subtree root node */
511 sfn = node_alloc();
512 if (sfn == NULL)
513 goto st_failure;
515 sfn->leaf = &ip6_null_entry;
516 atomic_inc(&ip6_null_entry.rt6i_ref);
517 sfn->fn_flags = RTN_ROOT;
518 sfn->fn_sernum = fib6_new_sernum();
520 /* Now add the first leaf node to new subtree */
522 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
523 sizeof(struct in6_addr), rt->rt6i_src.plen,
524 offsetof(struct rt6_info, rt6i_src));
526 if (sn == NULL) {
527 /* If it is failed, discard just allocated
528 root, and then (in st_failure) stale node
529 in main tree.
531 node_free(sfn);
532 goto st_failure;
535 /* Now link new subtree to main tree */
536 sfn->parent = fn;
537 fn->subtree = sfn;
538 if (fn->leaf == NULL) {
539 fn->leaf = rt;
540 atomic_inc(&rt->rt6i_ref);
542 } else {
543 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
544 sizeof(struct in6_addr), rt->rt6i_src.plen,
545 offsetof(struct rt6_info, rt6i_src));
547 if (sn == NULL)
548 goto st_failure;
551 fn = sn;
553 #endif
555 err = fib6_add_rt2node(fn, rt, nlh);
557 if (err == 0) {
558 fib6_start_gc(rt);
559 if (!(rt->rt6i_flags&RTF_CACHE))
560 fib6_prune_clones(fn, rt);
563 out:
564 if (err)
565 dst_free(&rt->u.dst);
566 return err;
568 #ifdef CONFIG_IPV6_SUBTREES
569 /* Subtree creation failed, probably main tree node
570 is orphan. If it is, shoot it.
572 st_failure:
573 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
574 fib6_repair_tree(fn);
575 dst_free(&rt->u.dst);
576 return err;
577 #endif
581 * Routing tree lookup
585 struct lookup_args {
586 int offset; /* key offset on rt6_info */
587 struct in6_addr *addr; /* search key */
590 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
591 struct lookup_args *args)
593 struct fib6_node *fn;
594 int dir;
597 * Descend on a tree
600 fn = root;
602 for (;;) {
603 struct fib6_node *next;
605 dir = addr_bit_set(args->addr, fn->fn_bit);
607 next = dir ? fn->right : fn->left;
609 if (next) {
610 fn = next;
611 continue;
614 break;
617 while ((fn->fn_flags & RTN_ROOT) == 0) {
618 #ifdef CONFIG_IPV6_SUBTREES
619 if (fn->subtree) {
620 struct fib6_node *st;
621 struct lookup_args *narg;
623 narg = args + 1;
625 if (narg->addr) {
626 st = fib6_lookup_1(fn->subtree, narg);
628 if (st && !(st->fn_flags & RTN_ROOT))
629 return st;
632 #endif
634 if (fn->fn_flags & RTN_RTINFO) {
635 struct rt6key *key;
637 key = (struct rt6key *) ((u8 *) fn->leaf +
638 args->offset);
640 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
641 return fn;
644 fn = fn->parent;
647 return NULL;
650 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
651 struct in6_addr *saddr)
653 struct lookup_args args[2];
654 struct fib6_node *fn;
656 args[0].offset = offsetof(struct rt6_info, rt6i_dst);
657 args[0].addr = daddr;
659 #ifdef CONFIG_IPV6_SUBTREES
660 args[1].offset = offsetof(struct rt6_info, rt6i_src);
661 args[1].addr = saddr;
662 #endif
664 fn = fib6_lookup_1(root, args);
666 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
667 fn = root;
669 return fn;
673 * Get node with specified destination prefix (and source prefix,
674 * if subtrees are used)
678 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
679 struct in6_addr *addr,
680 int plen, int offset)
682 struct fib6_node *fn;
684 for (fn = root; fn ; ) {
685 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
688 * Prefix match
690 if (plen < fn->fn_bit ||
691 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
692 return NULL;
694 if (plen == fn->fn_bit)
695 return fn;
698 * We have more bits to go
700 if (addr_bit_set(addr, fn->fn_bit))
701 fn = fn->right;
702 else
703 fn = fn->left;
705 return NULL;
708 struct fib6_node * fib6_locate(struct fib6_node *root,
709 struct in6_addr *daddr, int dst_len,
710 struct in6_addr *saddr, int src_len)
712 struct fib6_node *fn;
714 fn = fib6_locate_1(root, daddr, dst_len,
715 offsetof(struct rt6_info, rt6i_dst));
717 #ifdef CONFIG_IPV6_SUBTREES
718 if (src_len) {
719 BUG_TRAP(saddr!=NULL);
720 if (fn == NULL)
721 fn = fn->subtree;
722 if (fn)
723 fn = fib6_locate_1(fn, saddr, src_len,
724 offsetof(struct rt6_info, rt6i_src));
726 #endif
728 if (fn && fn->fn_flags&RTN_RTINFO)
729 return fn;
731 return NULL;
736 * Deletion
740 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
742 if (fn->fn_flags&RTN_ROOT)
743 return &ip6_null_entry;
745 while(fn) {
746 if(fn->left)
747 return fn->left->leaf;
749 if(fn->right)
750 return fn->right->leaf;
752 fn = SUBTREE(fn);
754 return NULL;
758 * Called to trim the tree of intermediate nodes when possible. "fn"
759 * is the node we want to try and remove.
762 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
764 int children;
765 int nstate;
766 struct fib6_node *child, *pn;
767 struct fib6_walker_t *w;
768 int iter = 0;
770 for (;;) {
771 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
772 iter++;
774 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
775 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
776 BUG_TRAP(fn->leaf==NULL);
778 children = 0;
779 child = NULL;
780 if (fn->right) child = fn->right, children |= 1;
781 if (fn->left) child = fn->left, children |= 2;
783 if (children == 3 || SUBTREE(fn)
784 #ifdef CONFIG_IPV6_SUBTREES
785 /* Subtree root (i.e. fn) may have one child */
786 || (children && fn->fn_flags&RTN_ROOT)
787 #endif
789 fn->leaf = fib6_find_prefix(fn);
790 #if RT6_DEBUG >= 2
791 if (fn->leaf==NULL) {
792 BUG_TRAP(fn->leaf);
793 fn->leaf = &ip6_null_entry;
795 #endif
796 atomic_inc(&fn->leaf->rt6i_ref);
797 return fn->parent;
800 pn = fn->parent;
801 #ifdef CONFIG_IPV6_SUBTREES
802 if (SUBTREE(pn) == fn) {
803 BUG_TRAP(fn->fn_flags&RTN_ROOT);
804 SUBTREE(pn) = NULL;
805 nstate = FWS_L;
806 } else {
807 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
808 #endif
809 if (pn->right == fn) pn->right = child;
810 else if (pn->left == fn) pn->left = child;
811 #if RT6_DEBUG >= 2
812 else BUG_TRAP(0);
813 #endif
814 if (child)
815 child->parent = pn;
816 nstate = FWS_R;
817 #ifdef CONFIG_IPV6_SUBTREES
819 #endif
821 read_lock(&fib6_walker_lock);
822 FOR_WALKERS(w) {
823 if (child == NULL) {
824 if (w->root == fn) {
825 w->root = w->node = NULL;
826 RT6_TRACE("W %p adjusted by delroot 1\n", w);
827 } else if (w->node == fn) {
828 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
829 w->node = pn;
830 w->state = nstate;
832 } else {
833 if (w->root == fn) {
834 w->root = child;
835 RT6_TRACE("W %p adjusted by delroot 2\n", w);
837 if (w->node == fn) {
838 w->node = child;
839 if (children&2) {
840 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
841 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
842 } else {
843 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
844 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
849 read_unlock(&fib6_walker_lock);
851 node_free(fn);
852 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
853 return pn;
855 rt6_release(pn->leaf);
856 pn->leaf = NULL;
857 fn = pn;
861 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
862 struct nlmsghdr *nlh, void *_rtattr)
864 struct fib6_walker_t *w;
865 struct rt6_info *rt = *rtp;
867 RT6_TRACE("fib6_del_route\n");
869 /* Unlink it */
870 *rtp = rt->u.next;
871 rt->rt6i_node = NULL;
872 rt6_stats.fib_rt_entries--;
873 rt6_stats.fib_discarded_routes++;
875 /* Adjust walkers */
876 read_lock(&fib6_walker_lock);
877 FOR_WALKERS(w) {
878 if (w->state == FWS_C && w->leaf == rt) {
879 RT6_TRACE("walker %p adjusted by delroute\n", w);
880 w->leaf = rt->u.next;
881 if (w->leaf == NULL)
882 w->state = FWS_U;
885 read_unlock(&fib6_walker_lock);
887 rt->u.next = NULL;
889 if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
890 fn->leaf = &ip6_null_entry;
892 /* If it was last route, expunge its radix tree node */
893 if (fn->leaf == NULL) {
894 fn->fn_flags &= ~RTN_RTINFO;
895 rt6_stats.fib_route_nodes--;
896 fn = fib6_repair_tree(fn);
899 if (atomic_read(&rt->rt6i_ref) != 1) {
900 /* This route is used as dummy address holder in some split
901 * nodes. It is not leaked, but it still holds other resources,
902 * which must be released in time. So, scan ascendant nodes
903 * and replace dummy references to this route with references
904 * to still alive ones.
906 while (fn) {
907 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
908 fn->leaf = fib6_find_prefix(fn);
909 atomic_inc(&fn->leaf->rt6i_ref);
910 rt6_release(rt);
912 fn = fn->parent;
914 /* No more references are possible at this point. */
915 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
918 inet6_rt_notify(RTM_DELROUTE, rt, nlh);
919 rt6_release(rt);
922 int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr)
924 struct fib6_node *fn = rt->rt6i_node;
925 struct rt6_info **rtp;
927 #if RT6_DEBUG >= 2
928 if (rt->u.dst.obsolete>0) {
929 BUG_TRAP(fn==NULL);
930 return -ENOENT;
932 #endif
933 if (fn == NULL || rt == &ip6_null_entry)
934 return -ENOENT;
936 BUG_TRAP(fn->fn_flags&RTN_RTINFO);
938 if (!(rt->rt6i_flags&RTF_CACHE))
939 fib6_prune_clones(fn, rt);
942 * Walk the leaf entries looking for ourself
945 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
946 if (*rtp == rt) {
947 fib6_del_route(fn, rtp, nlh, _rtattr);
948 return 0;
951 return -ENOENT;
955 * Tree traversal function.
957 * Certainly, it is not interrupt safe.
958 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
959 * It means, that we can modify tree during walking
960 * and use this function for garbage collection, clone pruning,
961 * cleaning tree when a device goes down etc. etc.
963 * It guarantees that every node will be traversed,
964 * and that it will be traversed only once.
966 * Callback function w->func may return:
967 * 0 -> continue walking.
968 * positive value -> walking is suspended (used by tree dumps,
969 * and probably by gc, if it will be split to several slices)
970 * negative value -> terminate walking.
972 * The function itself returns:
973 * 0 -> walk is complete.
974 * >0 -> walk is incomplete (i.e. suspended)
975 * <0 -> walk is terminated by an error.
978 int fib6_walk_continue(struct fib6_walker_t *w)
980 struct fib6_node *fn, *pn;
982 for (;;) {
983 fn = w->node;
984 if (fn == NULL)
985 return 0;
987 if (w->prune && fn != w->root &&
988 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
989 w->state = FWS_C;
990 w->leaf = fn->leaf;
992 switch (w->state) {
993 #ifdef CONFIG_IPV6_SUBTREES
994 case FWS_S:
995 if (SUBTREE(fn)) {
996 w->node = SUBTREE(fn);
997 continue;
999 w->state = FWS_L;
1000 #endif
1001 case FWS_L:
1002 if (fn->left) {
1003 w->node = fn->left;
1004 w->state = FWS_INIT;
1005 continue;
1007 w->state = FWS_R;
1008 case FWS_R:
1009 if (fn->right) {
1010 w->node = fn->right;
1011 w->state = FWS_INIT;
1012 continue;
1014 w->state = FWS_C;
1015 w->leaf = fn->leaf;
1016 case FWS_C:
1017 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1018 int err = w->func(w);
1019 if (err)
1020 return err;
1021 continue;
1023 w->state = FWS_U;
1024 case FWS_U:
1025 if (fn == w->root)
1026 return 0;
1027 pn = fn->parent;
1028 w->node = pn;
1029 #ifdef CONFIG_IPV6_SUBTREES
1030 if (SUBTREE(pn) == fn) {
1031 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1032 w->state = FWS_L;
1033 continue;
1035 #endif
1036 if (pn->left == fn) {
1037 w->state = FWS_R;
1038 continue;
1040 if (pn->right == fn) {
1041 w->state = FWS_C;
1042 w->leaf = w->node->leaf;
1043 continue;
1045 #if RT6_DEBUG >= 2
1046 BUG_TRAP(0);
1047 #endif
1052 int fib6_walk(struct fib6_walker_t *w)
1054 int res;
1056 w->state = FWS_INIT;
1057 w->node = w->root;
1059 fib6_walker_link(w);
1060 res = fib6_walk_continue(w);
1061 if (res <= 0)
1062 fib6_walker_unlink(w);
1063 return res;
1066 static int fib6_clean_node(struct fib6_walker_t *w)
1068 int res;
1069 struct rt6_info *rt;
1070 struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1072 for (rt = w->leaf; rt; rt = rt->u.next) {
1073 res = c->func(rt, c->arg);
1074 if (res < 0) {
1075 w->leaf = rt;
1076 res = fib6_del(rt, NULL, NULL);
1077 if (res) {
1078 #if RT6_DEBUG >= 2
1079 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1080 #endif
1081 continue;
1083 return 0;
1085 BUG_TRAP(res==0);
1087 w->leaf = rt;
1088 return 0;
1092 * Convenient frontend to tree walker.
1094 * func is called on each route.
1095 * It may return -1 -> delete this route.
1096 * 0 -> continue walking
1098 * prune==1 -> only immediate children of node (certainly,
1099 * ignoring pure split nodes) will be scanned.
1102 void fib6_clean_tree(struct fib6_node *root,
1103 int (*func)(struct rt6_info *, void *arg),
1104 int prune, void *arg)
1106 struct fib6_cleaner_t c;
1108 c.w.root = root;
1109 c.w.func = fib6_clean_node;
1110 c.w.prune = prune;
1111 c.func = func;
1112 c.arg = arg;
1114 fib6_walk(&c.w);
1117 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1119 if (rt->rt6i_flags & RTF_CACHE) {
1120 RT6_TRACE("pruning clone %p\n", rt);
1121 return -1;
1124 return 0;
1127 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1129 fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1133 * Garbage collection
1136 static struct fib6_gc_args
1138 int timeout;
1139 int more;
1140 } gc_args;
1142 static int fib6_age(struct rt6_info *rt, void *arg)
1144 unsigned long now = jiffies;
1147 * check addrconf expiration here.
1148 * Routes are expired even if they are in use.
1150 * Also age clones. Note, that clones are aged out
1151 * only if they are not in use now.
1154 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1155 if (time_after(now, rt->rt6i_expires)) {
1156 RT6_TRACE("expiring %p\n", rt);
1157 rt6_reset_dflt_pointer(rt);
1158 return -1;
1160 gc_args.more++;
1161 } else if (rt->rt6i_flags & RTF_CACHE) {
1162 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1163 time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1164 RT6_TRACE("aging clone %p\n", rt);
1165 return -1;
1166 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1167 (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1168 RT6_TRACE("purging route %p via non-router but gateway\n",
1169 rt);
1170 return -1;
1172 gc_args.more++;
1175 return 0;
1178 static DEFINE_SPINLOCK(fib6_gc_lock);
1180 void fib6_run_gc(unsigned long dummy)
1182 if (dummy != ~0UL) {
1183 spin_lock_bh(&fib6_gc_lock);
1184 gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
1185 } else {
1186 local_bh_disable();
1187 if (!spin_trylock(&fib6_gc_lock)) {
1188 mod_timer(&ip6_fib_timer, jiffies + HZ);
1189 local_bh_enable();
1190 return;
1192 gc_args.timeout = ip6_rt_gc_interval;
1194 gc_args.more = 0;
1197 write_lock_bh(&rt6_lock);
1198 ndisc_dst_gc(&gc_args.more);
1199 fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
1200 write_unlock_bh(&rt6_lock);
1202 if (gc_args.more)
1203 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1204 else {
1205 del_timer(&ip6_fib_timer);
1206 ip6_fib_timer.expires = 0;
1208 spin_unlock_bh(&fib6_gc_lock);
1211 void __init fib6_init(void)
1213 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1214 sizeof(struct fib6_node),
1215 0, SLAB_HWCACHE_ALIGN,
1216 NULL, NULL);
1217 if (!fib6_node_kmem)
1218 panic("cannot create fib6_nodes cache");
1221 void fib6_gc_cleanup(void)
1223 del_timer(&ip6_fib_timer);
1224 kmem_cache_destroy(fib6_node_kmem);