[PATCH] Fix cpu timers expiration time
[linux-2.6/mini2440.git] / net / ipv6 / ip6_fib.c
blob4fcc5a7acf6e446ce2715df397d37d78b3b34354
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 __read_mostly;
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 DEFINE_TIMER(ip6_fib_timer, 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, struct netlink_skb_parms *req)
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, req);
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,
483 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
485 struct fib6_node *fn;
486 int err = -ENOMEM;
488 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
489 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
491 if (fn == NULL)
492 goto out;
494 #ifdef CONFIG_IPV6_SUBTREES
495 if (rt->rt6i_src.plen) {
496 struct fib6_node *sn;
498 if (fn->subtree == NULL) {
499 struct fib6_node *sfn;
502 * Create subtree.
504 * fn[main tree]
506 * sfn[subtree root]
508 * sn[new leaf node]
511 /* Create subtree root node */
512 sfn = node_alloc();
513 if (sfn == NULL)
514 goto st_failure;
516 sfn->leaf = &ip6_null_entry;
517 atomic_inc(&ip6_null_entry.rt6i_ref);
518 sfn->fn_flags = RTN_ROOT;
519 sfn->fn_sernum = fib6_new_sernum();
521 /* Now add the first leaf node to new subtree */
523 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
524 sizeof(struct in6_addr), rt->rt6i_src.plen,
525 offsetof(struct rt6_info, rt6i_src));
527 if (sn == NULL) {
528 /* If it is failed, discard just allocated
529 root, and then (in st_failure) stale node
530 in main tree.
532 node_free(sfn);
533 goto st_failure;
536 /* Now link new subtree to main tree */
537 sfn->parent = fn;
538 fn->subtree = sfn;
539 if (fn->leaf == NULL) {
540 fn->leaf = rt;
541 atomic_inc(&rt->rt6i_ref);
543 } else {
544 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
545 sizeof(struct in6_addr), rt->rt6i_src.plen,
546 offsetof(struct rt6_info, rt6i_src));
548 if (sn == NULL)
549 goto st_failure;
552 fn = sn;
554 #endif
556 err = fib6_add_rt2node(fn, rt, nlh, req);
558 if (err == 0) {
559 fib6_start_gc(rt);
560 if (!(rt->rt6i_flags&RTF_CACHE))
561 fib6_prune_clones(fn, rt);
564 out:
565 if (err)
566 dst_free(&rt->u.dst);
567 return err;
569 #ifdef CONFIG_IPV6_SUBTREES
570 /* Subtree creation failed, probably main tree node
571 is orphan. If it is, shoot it.
573 st_failure:
574 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
575 fib6_repair_tree(fn);
576 dst_free(&rt->u.dst);
577 return err;
578 #endif
582 * Routing tree lookup
586 struct lookup_args {
587 int offset; /* key offset on rt6_info */
588 struct in6_addr *addr; /* search key */
591 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
592 struct lookup_args *args)
594 struct fib6_node *fn;
595 int dir;
598 * Descend on a tree
601 fn = root;
603 for (;;) {
604 struct fib6_node *next;
606 dir = addr_bit_set(args->addr, fn->fn_bit);
608 next = dir ? fn->right : fn->left;
610 if (next) {
611 fn = next;
612 continue;
615 break;
618 while ((fn->fn_flags & RTN_ROOT) == 0) {
619 #ifdef CONFIG_IPV6_SUBTREES
620 if (fn->subtree) {
621 struct fib6_node *st;
622 struct lookup_args *narg;
624 narg = args + 1;
626 if (narg->addr) {
627 st = fib6_lookup_1(fn->subtree, narg);
629 if (st && !(st->fn_flags & RTN_ROOT))
630 return st;
633 #endif
635 if (fn->fn_flags & RTN_RTINFO) {
636 struct rt6key *key;
638 key = (struct rt6key *) ((u8 *) fn->leaf +
639 args->offset);
641 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
642 return fn;
645 fn = fn->parent;
648 return NULL;
651 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
652 struct in6_addr *saddr)
654 struct lookup_args args[2];
655 struct fib6_node *fn;
657 args[0].offset = offsetof(struct rt6_info, rt6i_dst);
658 args[0].addr = daddr;
660 #ifdef CONFIG_IPV6_SUBTREES
661 args[1].offset = offsetof(struct rt6_info, rt6i_src);
662 args[1].addr = saddr;
663 #endif
665 fn = fib6_lookup_1(root, args);
667 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
668 fn = root;
670 return fn;
674 * Get node with specified destination prefix (and source prefix,
675 * if subtrees are used)
679 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
680 struct in6_addr *addr,
681 int plen, int offset)
683 struct fib6_node *fn;
685 for (fn = root; fn ; ) {
686 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
689 * Prefix match
691 if (plen < fn->fn_bit ||
692 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
693 return NULL;
695 if (plen == fn->fn_bit)
696 return fn;
699 * We have more bits to go
701 if (addr_bit_set(addr, fn->fn_bit))
702 fn = fn->right;
703 else
704 fn = fn->left;
706 return NULL;
709 struct fib6_node * fib6_locate(struct fib6_node *root,
710 struct in6_addr *daddr, int dst_len,
711 struct in6_addr *saddr, int src_len)
713 struct fib6_node *fn;
715 fn = fib6_locate_1(root, daddr, dst_len,
716 offsetof(struct rt6_info, rt6i_dst));
718 #ifdef CONFIG_IPV6_SUBTREES
719 if (src_len) {
720 BUG_TRAP(saddr!=NULL);
721 if (fn == NULL)
722 fn = fn->subtree;
723 if (fn)
724 fn = fib6_locate_1(fn, saddr, src_len,
725 offsetof(struct rt6_info, rt6i_src));
727 #endif
729 if (fn && fn->fn_flags&RTN_RTINFO)
730 return fn;
732 return NULL;
737 * Deletion
741 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
743 if (fn->fn_flags&RTN_ROOT)
744 return &ip6_null_entry;
746 while(fn) {
747 if(fn->left)
748 return fn->left->leaf;
750 if(fn->right)
751 return fn->right->leaf;
753 fn = SUBTREE(fn);
755 return NULL;
759 * Called to trim the tree of intermediate nodes when possible. "fn"
760 * is the node we want to try and remove.
763 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
765 int children;
766 int nstate;
767 struct fib6_node *child, *pn;
768 struct fib6_walker_t *w;
769 int iter = 0;
771 for (;;) {
772 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
773 iter++;
775 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
776 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
777 BUG_TRAP(fn->leaf==NULL);
779 children = 0;
780 child = NULL;
781 if (fn->right) child = fn->right, children |= 1;
782 if (fn->left) child = fn->left, children |= 2;
784 if (children == 3 || SUBTREE(fn)
785 #ifdef CONFIG_IPV6_SUBTREES
786 /* Subtree root (i.e. fn) may have one child */
787 || (children && fn->fn_flags&RTN_ROOT)
788 #endif
790 fn->leaf = fib6_find_prefix(fn);
791 #if RT6_DEBUG >= 2
792 if (fn->leaf==NULL) {
793 BUG_TRAP(fn->leaf);
794 fn->leaf = &ip6_null_entry;
796 #endif
797 atomic_inc(&fn->leaf->rt6i_ref);
798 return fn->parent;
801 pn = fn->parent;
802 #ifdef CONFIG_IPV6_SUBTREES
803 if (SUBTREE(pn) == fn) {
804 BUG_TRAP(fn->fn_flags&RTN_ROOT);
805 SUBTREE(pn) = NULL;
806 nstate = FWS_L;
807 } else {
808 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
809 #endif
810 if (pn->right == fn) pn->right = child;
811 else if (pn->left == fn) pn->left = child;
812 #if RT6_DEBUG >= 2
813 else BUG_TRAP(0);
814 #endif
815 if (child)
816 child->parent = pn;
817 nstate = FWS_R;
818 #ifdef CONFIG_IPV6_SUBTREES
820 #endif
822 read_lock(&fib6_walker_lock);
823 FOR_WALKERS(w) {
824 if (child == NULL) {
825 if (w->root == fn) {
826 w->root = w->node = NULL;
827 RT6_TRACE("W %p adjusted by delroot 1\n", w);
828 } else if (w->node == fn) {
829 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
830 w->node = pn;
831 w->state = nstate;
833 } else {
834 if (w->root == fn) {
835 w->root = child;
836 RT6_TRACE("W %p adjusted by delroot 2\n", w);
838 if (w->node == fn) {
839 w->node = child;
840 if (children&2) {
841 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
842 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
843 } else {
844 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
845 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
850 read_unlock(&fib6_walker_lock);
852 node_free(fn);
853 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
854 return pn;
856 rt6_release(pn->leaf);
857 pn->leaf = NULL;
858 fn = pn;
862 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
863 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
865 struct fib6_walker_t *w;
866 struct rt6_info *rt = *rtp;
868 RT6_TRACE("fib6_del_route\n");
870 /* Unlink it */
871 *rtp = rt->u.next;
872 rt->rt6i_node = NULL;
873 rt6_stats.fib_rt_entries--;
874 rt6_stats.fib_discarded_routes++;
876 /* Adjust walkers */
877 read_lock(&fib6_walker_lock);
878 FOR_WALKERS(w) {
879 if (w->state == FWS_C && w->leaf == rt) {
880 RT6_TRACE("walker %p adjusted by delroute\n", w);
881 w->leaf = rt->u.next;
882 if (w->leaf == NULL)
883 w->state = FWS_U;
886 read_unlock(&fib6_walker_lock);
888 rt->u.next = NULL;
890 if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
891 fn->leaf = &ip6_null_entry;
893 /* If it was last route, expunge its radix tree node */
894 if (fn->leaf == NULL) {
895 fn->fn_flags &= ~RTN_RTINFO;
896 rt6_stats.fib_route_nodes--;
897 fn = fib6_repair_tree(fn);
900 if (atomic_read(&rt->rt6i_ref) != 1) {
901 /* This route is used as dummy address holder in some split
902 * nodes. It is not leaked, but it still holds other resources,
903 * which must be released in time. So, scan ascendant nodes
904 * and replace dummy references to this route with references
905 * to still alive ones.
907 while (fn) {
908 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
909 fn->leaf = fib6_find_prefix(fn);
910 atomic_inc(&fn->leaf->rt6i_ref);
911 rt6_release(rt);
913 fn = fn->parent;
915 /* No more references are possible at this point. */
916 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
919 inet6_rt_notify(RTM_DELROUTE, rt, nlh, req);
920 rt6_release(rt);
923 int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
925 struct fib6_node *fn = rt->rt6i_node;
926 struct rt6_info **rtp;
928 #if RT6_DEBUG >= 2
929 if (rt->u.dst.obsolete>0) {
930 BUG_TRAP(fn==NULL);
931 return -ENOENT;
933 #endif
934 if (fn == NULL || rt == &ip6_null_entry)
935 return -ENOENT;
937 BUG_TRAP(fn->fn_flags&RTN_RTINFO);
939 if (!(rt->rt6i_flags&RTF_CACHE))
940 fib6_prune_clones(fn, rt);
943 * Walk the leaf entries looking for ourself
946 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
947 if (*rtp == rt) {
948 fib6_del_route(fn, rtp, nlh, _rtattr, req);
949 return 0;
952 return -ENOENT;
956 * Tree traversal function.
958 * Certainly, it is not interrupt safe.
959 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
960 * It means, that we can modify tree during walking
961 * and use this function for garbage collection, clone pruning,
962 * cleaning tree when a device goes down etc. etc.
964 * It guarantees that every node will be traversed,
965 * and that it will be traversed only once.
967 * Callback function w->func may return:
968 * 0 -> continue walking.
969 * positive value -> walking is suspended (used by tree dumps,
970 * and probably by gc, if it will be split to several slices)
971 * negative value -> terminate walking.
973 * The function itself returns:
974 * 0 -> walk is complete.
975 * >0 -> walk is incomplete (i.e. suspended)
976 * <0 -> walk is terminated by an error.
979 int fib6_walk_continue(struct fib6_walker_t *w)
981 struct fib6_node *fn, *pn;
983 for (;;) {
984 fn = w->node;
985 if (fn == NULL)
986 return 0;
988 if (w->prune && fn != w->root &&
989 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
990 w->state = FWS_C;
991 w->leaf = fn->leaf;
993 switch (w->state) {
994 #ifdef CONFIG_IPV6_SUBTREES
995 case FWS_S:
996 if (SUBTREE(fn)) {
997 w->node = SUBTREE(fn);
998 continue;
1000 w->state = FWS_L;
1001 #endif
1002 case FWS_L:
1003 if (fn->left) {
1004 w->node = fn->left;
1005 w->state = FWS_INIT;
1006 continue;
1008 w->state = FWS_R;
1009 case FWS_R:
1010 if (fn->right) {
1011 w->node = fn->right;
1012 w->state = FWS_INIT;
1013 continue;
1015 w->state = FWS_C;
1016 w->leaf = fn->leaf;
1017 case FWS_C:
1018 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1019 int err = w->func(w);
1020 if (err)
1021 return err;
1022 continue;
1024 w->state = FWS_U;
1025 case FWS_U:
1026 if (fn == w->root)
1027 return 0;
1028 pn = fn->parent;
1029 w->node = pn;
1030 #ifdef CONFIG_IPV6_SUBTREES
1031 if (SUBTREE(pn) == fn) {
1032 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1033 w->state = FWS_L;
1034 continue;
1036 #endif
1037 if (pn->left == fn) {
1038 w->state = FWS_R;
1039 continue;
1041 if (pn->right == fn) {
1042 w->state = FWS_C;
1043 w->leaf = w->node->leaf;
1044 continue;
1046 #if RT6_DEBUG >= 2
1047 BUG_TRAP(0);
1048 #endif
1053 int fib6_walk(struct fib6_walker_t *w)
1055 int res;
1057 w->state = FWS_INIT;
1058 w->node = w->root;
1060 fib6_walker_link(w);
1061 res = fib6_walk_continue(w);
1062 if (res <= 0)
1063 fib6_walker_unlink(w);
1064 return res;
1067 static int fib6_clean_node(struct fib6_walker_t *w)
1069 int res;
1070 struct rt6_info *rt;
1071 struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1073 for (rt = w->leaf; rt; rt = rt->u.next) {
1074 res = c->func(rt, c->arg);
1075 if (res < 0) {
1076 w->leaf = rt;
1077 res = fib6_del(rt, NULL, NULL, NULL);
1078 if (res) {
1079 #if RT6_DEBUG >= 2
1080 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1081 #endif
1082 continue;
1084 return 0;
1086 BUG_TRAP(res==0);
1088 w->leaf = rt;
1089 return 0;
1093 * Convenient frontend to tree walker.
1095 * func is called on each route.
1096 * It may return -1 -> delete this route.
1097 * 0 -> continue walking
1099 * prune==1 -> only immediate children of node (certainly,
1100 * ignoring pure split nodes) will be scanned.
1103 void fib6_clean_tree(struct fib6_node *root,
1104 int (*func)(struct rt6_info *, void *arg),
1105 int prune, void *arg)
1107 struct fib6_cleaner_t c;
1109 c.w.root = root;
1110 c.w.func = fib6_clean_node;
1111 c.w.prune = prune;
1112 c.func = func;
1113 c.arg = arg;
1115 fib6_walk(&c.w);
1118 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1120 if (rt->rt6i_flags & RTF_CACHE) {
1121 RT6_TRACE("pruning clone %p\n", rt);
1122 return -1;
1125 return 0;
1128 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1130 fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1134 * Garbage collection
1137 static struct fib6_gc_args
1139 int timeout;
1140 int more;
1141 } gc_args;
1143 static int fib6_age(struct rt6_info *rt, void *arg)
1145 unsigned long now = jiffies;
1148 * check addrconf expiration here.
1149 * Routes are expired even if they are in use.
1151 * Also age clones. Note, that clones are aged out
1152 * only if they are not in use now.
1155 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1156 if (time_after(now, rt->rt6i_expires)) {
1157 RT6_TRACE("expiring %p\n", rt);
1158 rt6_reset_dflt_pointer(rt);
1159 return -1;
1161 gc_args.more++;
1162 } else if (rt->rt6i_flags & RTF_CACHE) {
1163 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1164 time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1165 RT6_TRACE("aging clone %p\n", rt);
1166 return -1;
1167 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1168 (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1169 RT6_TRACE("purging route %p via non-router but gateway\n",
1170 rt);
1171 return -1;
1173 gc_args.more++;
1176 return 0;
1179 static DEFINE_SPINLOCK(fib6_gc_lock);
1181 void fib6_run_gc(unsigned long dummy)
1183 if (dummy != ~0UL) {
1184 spin_lock_bh(&fib6_gc_lock);
1185 gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
1186 } else {
1187 local_bh_disable();
1188 if (!spin_trylock(&fib6_gc_lock)) {
1189 mod_timer(&ip6_fib_timer, jiffies + HZ);
1190 local_bh_enable();
1191 return;
1193 gc_args.timeout = ip6_rt_gc_interval;
1195 gc_args.more = 0;
1198 write_lock_bh(&rt6_lock);
1199 ndisc_dst_gc(&gc_args.more);
1200 fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
1201 write_unlock_bh(&rt6_lock);
1203 if (gc_args.more)
1204 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1205 else {
1206 del_timer(&ip6_fib_timer);
1207 ip6_fib_timer.expires = 0;
1209 spin_unlock_bh(&fib6_gc_lock);
1212 void __init fib6_init(void)
1214 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1215 sizeof(struct fib6_node),
1216 0, SLAB_HWCACHE_ALIGN,
1217 NULL, NULL);
1218 if (!fib6_node_kmem)
1219 panic("cannot create fib6_nodes cache");
1222 void fib6_gc_cleanup(void)
1224 del_timer(&ip6_fib_timer);
1225 kmem_cache_destroy(fib6_node_kmem);