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;
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 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)
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];
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)
143 for (i = 0; i < addrlen; i++) {
153 while ((xb & (1 << j)) == 0)
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 ...
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.
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));
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);
205 * return the appropriate node for a routing tree "add" operation
206 * by either creating and inserting or by returning an existing
210 static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
211 int addrlen, int plen,
214 struct fib6_node *fn, *in, *ln;
215 struct fib6_node *pn = NULL;
219 __u32 sernum = fib6_new_sernum();
221 RT6_TRACE("fib6_add_1\n");
223 /* insert node in tree */
228 key = (struct rt6key *)((u8 *)fn->leaf + offset);
233 if (plen < fn->fn_bit ||
234 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
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);
248 fn->fn_sernum = sernum;
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);
261 fn = dir ? fn->right: fn->left;
265 * We walked to the bottom of tree.
266 * Create new leaf node without children.
276 ln->fn_sernum = sernum;
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
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);
308 * (new leaf node)[ln] (old node)[fn]
314 if (in == NULL || ln == NULL) {
323 * new intermediate node.
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
334 atomic_inc(&in->leaf->rt6i_ref);
336 in->fn_sernum = sernum;
338 /* update parent pointer */
349 ln->fn_sernum = sernum;
351 if (addr_bit_set(addr, bit)) {
358 } else { /* plen <= bit */
361 * (new leaf node)[ln]
363 * (old node)[fn] NULL
375 ln->fn_sernum = sernum;
382 if (addr_bit_set(&key->addr, plen))
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;
404 if (fn->fn_flags&RTN_TL_ROOT &&
405 fn->leaf == &ip6_null_entry &&
406 !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
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))
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;
437 if (iter->rt6i_metric > rt->rt6i_metric)
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;
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;
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));
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;
510 /* Create subtree root node */
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));
527 /* If it is failed, discard just allocated
528 root, and then (in st_failure) stale node
535 /* Now link new subtree to main tree */
538 if (fn->leaf == NULL) {
540 atomic_inc(&rt->rt6i_ref);
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));
555 err = fib6_add_rt2node(fn, rt, nlh);
559 if (!(rt->rt6i_flags&RTF_CACHE))
560 fib6_prune_clones(fn, rt);
565 dst_free(&rt->u.dst);
568 #ifdef CONFIG_IPV6_SUBTREES
569 /* Subtree creation failed, probably main tree node
570 is orphan. If it is, shoot it.
573 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
574 fib6_repair_tree(fn);
575 dst_free(&rt->u.dst);
581 * Routing tree lookup
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;
603 struct fib6_node *next;
605 dir = addr_bit_set(args->addr, fn->fn_bit);
607 next = dir ? fn->right : fn->left;
617 while ((fn->fn_flags & RTN_ROOT) == 0) {
618 #ifdef CONFIG_IPV6_SUBTREES
620 struct fib6_node *st;
621 struct lookup_args *narg;
626 st = fib6_lookup_1(fn->subtree, narg);
628 if (st && !(st->fn_flags & RTN_ROOT))
634 if (fn->fn_flags & RTN_RTINFO) {
637 key = (struct rt6key *) ((u8 *) fn->leaf +
640 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
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;
664 fn = fib6_lookup_1(root, args);
666 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
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);
690 if (plen < fn->fn_bit ||
691 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
694 if (plen == fn->fn_bit)
698 * We have more bits to go
700 if (addr_bit_set(addr, fn->fn_bit))
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
719 BUG_TRAP(saddr!=NULL);
723 fn = fib6_locate_1(fn, saddr, src_len,
724 offsetof(struct rt6_info, rt6i_src));
728 if (fn && fn->fn_flags&RTN_RTINFO)
740 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
742 if (fn->fn_flags&RTN_ROOT)
743 return &ip6_null_entry;
747 return fn->left->leaf;
750 return fn->right->leaf;
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)
766 struct fib6_node *child, *pn;
767 struct fib6_walker_t *w;
771 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
774 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
775 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
776 BUG_TRAP(fn->leaf==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)
789 fn->leaf = fib6_find_prefix(fn);
791 if (fn->leaf==NULL) {
793 fn->leaf = &ip6_null_entry;
796 atomic_inc(&fn->leaf->rt6i_ref);
801 #ifdef CONFIG_IPV6_SUBTREES
802 if (SUBTREE(pn) == fn) {
803 BUG_TRAP(fn->fn_flags&RTN_ROOT);
807 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
809 if (pn->right == fn) pn->right = child;
810 else if (pn->left == fn) pn->left = child;
817 #ifdef CONFIG_IPV6_SUBTREES
821 read_lock(&fib6_walker_lock);
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);
835 RT6_TRACE("W %p adjusted by delroot 2\n", w);
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;
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);
852 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
855 rt6_release(pn->leaf);
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");
871 rt->rt6i_node = NULL;
872 rt6_stats.fib_rt_entries--;
873 rt6_stats.fib_discarded_routes++;
876 read_lock(&fib6_walker_lock);
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;
885 read_unlock(&fib6_walker_lock);
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.
907 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
908 fn->leaf = fib6_find_prefix(fn);
909 atomic_inc(&fn->leaf->rt6i_ref);
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);
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;
928 if (rt->u.dst.obsolete>0) {
933 if (fn == NULL || rt == &ip6_null_entry)
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) {
947 fib6_del_route(fn, rtp, nlh, _rtattr);
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;
987 if (w->prune && fn != w->root &&
988 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
993 #ifdef CONFIG_IPV6_SUBTREES
996 w->node = SUBTREE(fn);
1004 w->state = FWS_INIT;
1010 w->node = fn->right;
1011 w->state = FWS_INIT;
1017 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1018 int err = w->func(w);
1029 #ifdef CONFIG_IPV6_SUBTREES
1030 if (SUBTREE(pn) == fn) {
1031 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1036 if (pn->left == fn) {
1040 if (pn->right == fn) {
1042 w->leaf = w->node->leaf;
1052 int fib6_walk(struct fib6_walker_t *w)
1056 w->state = FWS_INIT;
1059 fib6_walker_link(w);
1060 res = fib6_walk_continue(w);
1062 fib6_walker_unlink(w);
1066 static int fib6_clean_node(struct fib6_walker_t *w)
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);
1076 res = fib6_del(rt, NULL, NULL);
1079 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
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;
1109 c.w.func = fib6_clean_node;
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);
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
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);
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);
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",
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;
1187 if (!spin_trylock(&fib6_gc_lock)) {
1188 mod_timer(&ip6_fib_timer, jiffies + HZ);
1192 gc_args.timeout = ip6_rt_gc_interval;
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);
1203 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
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,
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);