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 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, struct netlink_skb_parms *req)
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, 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;
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;
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));
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;
511 /* Create subtree root node */
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));
528 /* If it is failed, discard just allocated
529 root, and then (in st_failure) stale node
536 /* Now link new subtree to main tree */
539 if (fn->leaf == NULL) {
541 atomic_inc(&rt->rt6i_ref);
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));
556 err = fib6_add_rt2node(fn, rt, nlh, req);
560 if (!(rt->rt6i_flags&RTF_CACHE))
561 fib6_prune_clones(fn, rt);
566 dst_free(&rt->u.dst);
569 #ifdef CONFIG_IPV6_SUBTREES
570 /* Subtree creation failed, probably main tree node
571 is orphan. If it is, shoot it.
574 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
575 fib6_repair_tree(fn);
576 dst_free(&rt->u.dst);
582 * Routing tree lookup
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;
604 struct fib6_node *next;
606 dir = addr_bit_set(args->addr, fn->fn_bit);
608 next = dir ? fn->right : fn->left;
618 while ((fn->fn_flags & RTN_ROOT) == 0) {
619 #ifdef CONFIG_IPV6_SUBTREES
621 struct fib6_node *st;
622 struct lookup_args *narg;
627 st = fib6_lookup_1(fn->subtree, narg);
629 if (st && !(st->fn_flags & RTN_ROOT))
635 if (fn->fn_flags & RTN_RTINFO) {
638 key = (struct rt6key *) ((u8 *) fn->leaf +
641 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
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;
665 fn = fib6_lookup_1(root, args);
667 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
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);
691 if (plen < fn->fn_bit ||
692 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
695 if (plen == fn->fn_bit)
699 * We have more bits to go
701 if (addr_bit_set(addr, fn->fn_bit))
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
720 BUG_TRAP(saddr!=NULL);
724 fn = fib6_locate_1(fn, saddr, src_len,
725 offsetof(struct rt6_info, rt6i_src));
729 if (fn && fn->fn_flags&RTN_RTINFO)
741 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
743 if (fn->fn_flags&RTN_ROOT)
744 return &ip6_null_entry;
748 return fn->left->leaf;
751 return fn->right->leaf;
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)
767 struct fib6_node *child, *pn;
768 struct fib6_walker_t *w;
772 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
775 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
776 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
777 BUG_TRAP(fn->leaf==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)
790 fn->leaf = fib6_find_prefix(fn);
792 if (fn->leaf==NULL) {
794 fn->leaf = &ip6_null_entry;
797 atomic_inc(&fn->leaf->rt6i_ref);
802 #ifdef CONFIG_IPV6_SUBTREES
803 if (SUBTREE(pn) == fn) {
804 BUG_TRAP(fn->fn_flags&RTN_ROOT);
808 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
810 if (pn->right == fn) pn->right = child;
811 else if (pn->left == fn) pn->left = child;
818 #ifdef CONFIG_IPV6_SUBTREES
822 read_lock(&fib6_walker_lock);
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);
836 RT6_TRACE("W %p adjusted by delroot 2\n", w);
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;
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);
853 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
856 rt6_release(pn->leaf);
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");
872 rt->rt6i_node = NULL;
873 rt6_stats.fib_rt_entries--;
874 rt6_stats.fib_discarded_routes++;
877 read_lock(&fib6_walker_lock);
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;
886 read_unlock(&fib6_walker_lock);
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.
908 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
909 fn->leaf = fib6_find_prefix(fn);
910 atomic_inc(&fn->leaf->rt6i_ref);
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);
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;
929 if (rt->u.dst.obsolete>0) {
934 if (fn == NULL || rt == &ip6_null_entry)
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) {
948 fib6_del_route(fn, rtp, nlh, _rtattr, req);
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;
988 if (w->prune && fn != w->root &&
989 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
994 #ifdef CONFIG_IPV6_SUBTREES
997 w->node = SUBTREE(fn);
1005 w->state = FWS_INIT;
1011 w->node = fn->right;
1012 w->state = FWS_INIT;
1018 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1019 int err = w->func(w);
1030 #ifdef CONFIG_IPV6_SUBTREES
1031 if (SUBTREE(pn) == fn) {
1032 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1037 if (pn->left == fn) {
1041 if (pn->right == fn) {
1043 w->leaf = w->node->leaf;
1053 int fib6_walk(struct fib6_walker_t *w)
1057 w->state = FWS_INIT;
1060 fib6_walker_link(w);
1061 res = fib6_walk_continue(w);
1063 fib6_walker_unlink(w);
1067 static int fib6_clean_node(struct fib6_walker_t *w)
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);
1077 res = fib6_del(rt, NULL, NULL, NULL);
1080 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
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;
1110 c.w.func = fib6_clean_node;
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);
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
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);
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);
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",
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;
1188 if (!spin_trylock(&fib6_gc_lock)) {
1189 mod_timer(&ip6_fib_timer, jiffies + HZ);
1193 gc_args.timeout = ip6_rt_gc_interval;
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);
1204 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
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,
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);