2 * Linux INET6 implementation
3 * Forwarding Information Database
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.
18 * Yuji SEKIYA @USAGI: Support default route on router node;
19 * remove ip6_null_entry from the top of
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>
32 #include <linux/proc_fs.h>
36 #include <net/ndisc.h>
37 #include <net/addrconf.h>
39 #include <net/ip6_fib.h>
40 #include <net/ip6_route.h>
45 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
47 #define RT6_TRACE(x...) do { ; } while (0)
50 struct rt6_statistics rt6_stats;
52 static kmem_cache_t * fib6_node_kmem __read_mostly;
56 #ifdef CONFIG_IPV6_SUBTREES
67 struct fib6_walker_t w;
68 int (*func)(struct rt6_info *, 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)
79 #define FWS_INIT FWS_L
80 #define SUBTREE(fn) NULL
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)
113 * Auxiliary address test functions for the radix tree.
115 * These assume a 32bit processor (although it will work on
123 static __inline__ int addr_bit_set(void *token, int fn_bit)
127 return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
130 static __inline__ struct fib6_node * node_alloc(void)
132 struct fib6_node *fn;
134 if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
135 memset(fn, 0, sizeof(struct fib6_node));
140 static __inline__ void node_free(struct fib6_node * fn)
142 kmem_cache_free(fib6_node_kmem, fn);
145 static __inline__ void rt6_release(struct rt6_info *rt)
147 if (atomic_dec_and_test(&rt->rt6i_ref))
148 dst_free(&rt->u.dst);
155 * return the appropriate node for a routing tree "add" operation
156 * by either creating and inserting or by returning an existing
160 static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
161 int addrlen, int plen,
164 struct fib6_node *fn, *in, *ln;
165 struct fib6_node *pn = NULL;
169 __u32 sernum = fib6_new_sernum();
171 RT6_TRACE("fib6_add_1\n");
173 /* insert node in tree */
178 key = (struct rt6key *)((u8 *)fn->leaf + offset);
183 if (plen < fn->fn_bit ||
184 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
191 if (plen == fn->fn_bit) {
192 /* clean up an intermediate node */
193 if ((fn->fn_flags & RTN_RTINFO) == 0) {
194 rt6_release(fn->leaf);
198 fn->fn_sernum = sernum;
204 * We have more bits to go
207 /* Try to walk down on tree. */
208 fn->fn_sernum = sernum;
209 dir = addr_bit_set(addr, fn->fn_bit);
211 fn = dir ? fn->right: fn->left;
215 * We walked to the bottom of tree.
216 * Create new leaf node without children.
226 ln->fn_sernum = sernum;
238 * split since we don't have a common prefix anymore or
239 * we have a less significant route.
240 * we've to insert an intermediate node on the list
241 * this new node will point to the one we need to create
247 /* find 1st bit in difference between the 2 addrs.
249 See comment in __ipv6_addr_diff: bit may be an invalid value,
250 but if it is >= plen, the value is ignored in any case.
253 bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
258 * (new leaf node)[ln] (old node)[fn]
264 if (in == NULL || ln == NULL) {
273 * new intermediate node.
275 * be off since that an address that chooses one of
276 * the branches would not match less specific routes
277 * in the other branch
284 atomic_inc(&in->leaf->rt6i_ref);
286 in->fn_sernum = sernum;
288 /* update parent pointer */
299 ln->fn_sernum = sernum;
301 if (addr_bit_set(addr, bit)) {
308 } else { /* plen <= bit */
311 * (new leaf node)[ln]
313 * (old node)[fn] NULL
325 ln->fn_sernum = sernum;
332 if (addr_bit_set(&key->addr, plen))
343 * Insert routing information in a node.
346 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
347 struct nlmsghdr *nlh, struct netlink_skb_parms *req)
349 struct rt6_info *iter = NULL;
350 struct rt6_info **ins;
354 if (fn->fn_flags&RTN_TL_ROOT &&
355 fn->leaf == &ip6_null_entry &&
356 !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
362 for (iter = fn->leaf; iter; iter=iter->u.next) {
364 * Search for duplicates
367 if (iter->rt6i_metric == rt->rt6i_metric) {
369 * Same priority level
372 if (iter->rt6i_dev == rt->rt6i_dev &&
373 iter->rt6i_idev == rt->rt6i_idev &&
374 ipv6_addr_equal(&iter->rt6i_gateway,
375 &rt->rt6i_gateway)) {
376 if (!(iter->rt6i_flags&RTF_EXPIRES))
378 iter->rt6i_expires = rt->rt6i_expires;
379 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
380 iter->rt6i_flags &= ~RTF_EXPIRES;
381 iter->rt6i_expires = 0;
387 if (iter->rt6i_metric > rt->rt6i_metric)
401 atomic_inc(&rt->rt6i_ref);
402 inet6_rt_notify(RTM_NEWROUTE, rt, nlh, req);
403 rt6_stats.fib_rt_entries++;
405 if ((fn->fn_flags & RTN_RTINFO) == 0) {
406 rt6_stats.fib_route_nodes++;
407 fn->fn_flags |= RTN_RTINFO;
413 static __inline__ void fib6_start_gc(struct rt6_info *rt)
415 if (ip6_fib_timer.expires == 0 &&
416 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
417 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
420 void fib6_force_start_gc(void)
422 if (ip6_fib_timer.expires == 0)
423 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
427 * Add routing information to the routing tree.
428 * <destination addr>/<source addr>
429 * with source addr info in sub-trees
432 int fib6_add(struct fib6_node *root, struct rt6_info *rt,
433 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
435 struct fib6_node *fn;
438 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
439 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
444 #ifdef CONFIG_IPV6_SUBTREES
445 if (rt->rt6i_src.plen) {
446 struct fib6_node *sn;
448 if (fn->subtree == NULL) {
449 struct fib6_node *sfn;
461 /* Create subtree root node */
466 sfn->leaf = &ip6_null_entry;
467 atomic_inc(&ip6_null_entry.rt6i_ref);
468 sfn->fn_flags = RTN_ROOT;
469 sfn->fn_sernum = fib6_new_sernum();
471 /* Now add the first leaf node to new subtree */
473 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
474 sizeof(struct in6_addr), rt->rt6i_src.plen,
475 offsetof(struct rt6_info, rt6i_src));
478 /* If it is failed, discard just allocated
479 root, and then (in st_failure) stale node
486 /* Now link new subtree to main tree */
489 if (fn->leaf == NULL) {
491 atomic_inc(&rt->rt6i_ref);
494 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
495 sizeof(struct in6_addr), rt->rt6i_src.plen,
496 offsetof(struct rt6_info, rt6i_src));
506 err = fib6_add_rt2node(fn, rt, nlh, req);
510 if (!(rt->rt6i_flags&RTF_CACHE))
511 fib6_prune_clones(fn, rt);
516 dst_free(&rt->u.dst);
519 #ifdef CONFIG_IPV6_SUBTREES
520 /* Subtree creation failed, probably main tree node
521 is orphan. If it is, shoot it.
524 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
525 fib6_repair_tree(fn);
526 dst_free(&rt->u.dst);
532 * Routing tree lookup
537 int offset; /* key offset on rt6_info */
538 struct in6_addr *addr; /* search key */
541 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
542 struct lookup_args *args)
544 struct fib6_node *fn;
554 struct fib6_node *next;
556 dir = addr_bit_set(args->addr, fn->fn_bit);
558 next = dir ? fn->right : fn->left;
568 while ((fn->fn_flags & RTN_ROOT) == 0) {
569 #ifdef CONFIG_IPV6_SUBTREES
571 struct fib6_node *st;
572 struct lookup_args *narg;
577 st = fib6_lookup_1(fn->subtree, narg);
579 if (st && !(st->fn_flags & RTN_ROOT))
585 if (fn->fn_flags & RTN_RTINFO) {
588 key = (struct rt6key *) ((u8 *) fn->leaf +
591 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
601 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
602 struct in6_addr *saddr)
604 struct lookup_args args[2];
605 struct fib6_node *fn;
607 args[0].offset = offsetof(struct rt6_info, rt6i_dst);
608 args[0].addr = daddr;
610 #ifdef CONFIG_IPV6_SUBTREES
611 args[1].offset = offsetof(struct rt6_info, rt6i_src);
612 args[1].addr = saddr;
615 fn = fib6_lookup_1(root, args);
617 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
624 * Get node with specified destination prefix (and source prefix,
625 * if subtrees are used)
629 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
630 struct in6_addr *addr,
631 int plen, int offset)
633 struct fib6_node *fn;
635 for (fn = root; fn ; ) {
636 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
641 if (plen < fn->fn_bit ||
642 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
645 if (plen == fn->fn_bit)
649 * We have more bits to go
651 if (addr_bit_set(addr, fn->fn_bit))
659 struct fib6_node * fib6_locate(struct fib6_node *root,
660 struct in6_addr *daddr, int dst_len,
661 struct in6_addr *saddr, int src_len)
663 struct fib6_node *fn;
665 fn = fib6_locate_1(root, daddr, dst_len,
666 offsetof(struct rt6_info, rt6i_dst));
668 #ifdef CONFIG_IPV6_SUBTREES
670 BUG_TRAP(saddr!=NULL);
674 fn = fib6_locate_1(fn, saddr, src_len,
675 offsetof(struct rt6_info, rt6i_src));
679 if (fn && fn->fn_flags&RTN_RTINFO)
691 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
693 if (fn->fn_flags&RTN_ROOT)
694 return &ip6_null_entry;
698 return fn->left->leaf;
701 return fn->right->leaf;
709 * Called to trim the tree of intermediate nodes when possible. "fn"
710 * is the node we want to try and remove.
713 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
717 struct fib6_node *child, *pn;
718 struct fib6_walker_t *w;
722 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
725 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
726 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
727 BUG_TRAP(fn->leaf==NULL);
731 if (fn->right) child = fn->right, children |= 1;
732 if (fn->left) child = fn->left, children |= 2;
734 if (children == 3 || SUBTREE(fn)
735 #ifdef CONFIG_IPV6_SUBTREES
736 /* Subtree root (i.e. fn) may have one child */
737 || (children && fn->fn_flags&RTN_ROOT)
740 fn->leaf = fib6_find_prefix(fn);
742 if (fn->leaf==NULL) {
744 fn->leaf = &ip6_null_entry;
747 atomic_inc(&fn->leaf->rt6i_ref);
752 #ifdef CONFIG_IPV6_SUBTREES
753 if (SUBTREE(pn) == fn) {
754 BUG_TRAP(fn->fn_flags&RTN_ROOT);
758 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
760 if (pn->right == fn) pn->right = child;
761 else if (pn->left == fn) pn->left = child;
768 #ifdef CONFIG_IPV6_SUBTREES
772 read_lock(&fib6_walker_lock);
776 w->root = w->node = NULL;
777 RT6_TRACE("W %p adjusted by delroot 1\n", w);
778 } else if (w->node == fn) {
779 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
786 RT6_TRACE("W %p adjusted by delroot 2\n", w);
791 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
792 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
794 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
795 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
800 read_unlock(&fib6_walker_lock);
803 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
806 rt6_release(pn->leaf);
812 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
813 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
815 struct fib6_walker_t *w;
816 struct rt6_info *rt = *rtp;
818 RT6_TRACE("fib6_del_route\n");
822 rt->rt6i_node = NULL;
823 rt6_stats.fib_rt_entries--;
824 rt6_stats.fib_discarded_routes++;
827 read_lock(&fib6_walker_lock);
829 if (w->state == FWS_C && w->leaf == rt) {
830 RT6_TRACE("walker %p adjusted by delroute\n", w);
831 w->leaf = rt->u.next;
836 read_unlock(&fib6_walker_lock);
840 if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
841 fn->leaf = &ip6_null_entry;
843 /* If it was last route, expunge its radix tree node */
844 if (fn->leaf == NULL) {
845 fn->fn_flags &= ~RTN_RTINFO;
846 rt6_stats.fib_route_nodes--;
847 fn = fib6_repair_tree(fn);
850 if (atomic_read(&rt->rt6i_ref) != 1) {
851 /* This route is used as dummy address holder in some split
852 * nodes. It is not leaked, but it still holds other resources,
853 * which must be released in time. So, scan ascendant nodes
854 * and replace dummy references to this route with references
855 * to still alive ones.
858 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
859 fn->leaf = fib6_find_prefix(fn);
860 atomic_inc(&fn->leaf->rt6i_ref);
865 /* No more references are possible at this point. */
866 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
869 inet6_rt_notify(RTM_DELROUTE, rt, nlh, req);
873 int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
875 struct fib6_node *fn = rt->rt6i_node;
876 struct rt6_info **rtp;
879 if (rt->u.dst.obsolete>0) {
884 if (fn == NULL || rt == &ip6_null_entry)
887 BUG_TRAP(fn->fn_flags&RTN_RTINFO);
889 if (!(rt->rt6i_flags&RTF_CACHE))
890 fib6_prune_clones(fn, rt);
893 * Walk the leaf entries looking for ourself
896 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
898 fib6_del_route(fn, rtp, nlh, _rtattr, req);
906 * Tree traversal function.
908 * Certainly, it is not interrupt safe.
909 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
910 * It means, that we can modify tree during walking
911 * and use this function for garbage collection, clone pruning,
912 * cleaning tree when a device goes down etc. etc.
914 * It guarantees that every node will be traversed,
915 * and that it will be traversed only once.
917 * Callback function w->func may return:
918 * 0 -> continue walking.
919 * positive value -> walking is suspended (used by tree dumps,
920 * and probably by gc, if it will be split to several slices)
921 * negative value -> terminate walking.
923 * The function itself returns:
924 * 0 -> walk is complete.
925 * >0 -> walk is incomplete (i.e. suspended)
926 * <0 -> walk is terminated by an error.
929 int fib6_walk_continue(struct fib6_walker_t *w)
931 struct fib6_node *fn, *pn;
938 if (w->prune && fn != w->root &&
939 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
944 #ifdef CONFIG_IPV6_SUBTREES
947 w->node = SUBTREE(fn);
968 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
969 int err = w->func(w);
980 #ifdef CONFIG_IPV6_SUBTREES
981 if (SUBTREE(pn) == fn) {
982 BUG_TRAP(fn->fn_flags&RTN_ROOT);
987 if (pn->left == fn) {
991 if (pn->right == fn) {
993 w->leaf = w->node->leaf;
1003 int fib6_walk(struct fib6_walker_t *w)
1007 w->state = FWS_INIT;
1010 fib6_walker_link(w);
1011 res = fib6_walk_continue(w);
1013 fib6_walker_unlink(w);
1017 static int fib6_clean_node(struct fib6_walker_t *w)
1020 struct rt6_info *rt;
1021 struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1023 for (rt = w->leaf; rt; rt = rt->u.next) {
1024 res = c->func(rt, c->arg);
1027 res = fib6_del(rt, NULL, NULL, NULL);
1030 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1043 * Convenient frontend to tree walker.
1045 * func is called on each route.
1046 * It may return -1 -> delete this route.
1047 * 0 -> continue walking
1049 * prune==1 -> only immediate children of node (certainly,
1050 * ignoring pure split nodes) will be scanned.
1053 void fib6_clean_tree(struct fib6_node *root,
1054 int (*func)(struct rt6_info *, void *arg),
1055 int prune, void *arg)
1057 struct fib6_cleaner_t c;
1060 c.w.func = fib6_clean_node;
1068 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1070 if (rt->rt6i_flags & RTF_CACHE) {
1071 RT6_TRACE("pruning clone %p\n", rt);
1078 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1080 fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1084 * Garbage collection
1087 static struct fib6_gc_args
1093 static int fib6_age(struct rt6_info *rt, void *arg)
1095 unsigned long now = jiffies;
1098 * check addrconf expiration here.
1099 * Routes are expired even if they are in use.
1101 * Also age clones. Note, that clones are aged out
1102 * only if they are not in use now.
1105 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1106 if (time_after(now, rt->rt6i_expires)) {
1107 RT6_TRACE("expiring %p\n", rt);
1111 } else if (rt->rt6i_flags & RTF_CACHE) {
1112 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1113 time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1114 RT6_TRACE("aging clone %p\n", rt);
1116 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1117 (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1118 RT6_TRACE("purging route %p via non-router but gateway\n",
1128 static DEFINE_SPINLOCK(fib6_gc_lock);
1130 void fib6_run_gc(unsigned long dummy)
1132 if (dummy != ~0UL) {
1133 spin_lock_bh(&fib6_gc_lock);
1134 gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
1137 if (!spin_trylock(&fib6_gc_lock)) {
1138 mod_timer(&ip6_fib_timer, jiffies + HZ);
1142 gc_args.timeout = ip6_rt_gc_interval;
1147 write_lock_bh(&rt6_lock);
1148 ndisc_dst_gc(&gc_args.more);
1149 fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
1150 write_unlock_bh(&rt6_lock);
1153 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1155 del_timer(&ip6_fib_timer);
1156 ip6_fib_timer.expires = 0;
1158 spin_unlock_bh(&fib6_gc_lock);
1161 void __init fib6_init(void)
1163 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1164 sizeof(struct fib6_node),
1165 0, SLAB_HWCACHE_ALIGN,
1167 if (!fib6_node_kmem)
1168 panic("cannot create fib6_nodes cache");
1171 void fib6_gc_cleanup(void)
1173 del_timer(&ip6_fib_timer);
1174 kmem_cache_destroy(fib6_node_kmem);