2 * Linux INET6 implementation
3 * Forwarding Information Database
6 * Pedro Roque <pedro_m@yahoo.com>
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>
43 #undef CONFIG_IPV6_SUBTREES
46 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
48 #define RT6_TRACE(x...) do { ; } while (0)
51 struct rt6_statistics rt6_stats;
53 static kmem_cache_t * fib6_node_kmem;
57 #ifdef CONFIG_IPV6_SUBTREES
68 struct fib6_walker_t w;
69 int (*func)(struct rt6_info *, void *arg);
73 rwlock_t fib6_walker_lock = RW_LOCK_UNLOCKED;
76 #ifdef CONFIG_IPV6_SUBTREES
77 #define FWS_INIT FWS_S
78 #define SUBTREE(fn) ((fn)->subtree)
80 #define FWS_INIT FWS_L
81 #define SUBTREE(fn) NULL
84 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
85 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
88 * A routing update causes an increase of the serial number on the
89 * afected subtree. This allows for cached routes to be asynchronously
90 * tested when modifications are made to the destination cache as a
91 * result of redirects, path MTU changes, etc.
94 static __u32 rt_sernum = 0;
96 static struct timer_list ip6_fib_timer = { function: fib6_run_gc };
98 static struct fib6_walker_t fib6_walker_list = {
99 &fib6_walker_list, &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
120 * compare "prefix length" bits of an address
123 static __inline__ int addr_match(void *token1, void *token2, int prefixlen)
130 pdw = prefixlen >> 5; /* num of whole __u32 in prefix */
131 pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */
134 if (memcmp(a1, a2, pdw << 2))
140 mask = htonl((0xffffffff) << (32 - pbi));
142 if ((a1[pdw] ^ a2[pdw]) & mask)
153 static __inline__ int addr_bit_set(void *token, int fn_bit)
157 return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
161 * find the first different bit between two addresses
162 * length of address must be a multiple of 32bits
165 static __inline__ int addr_diff(void *token1, void *token2, int addrlen)
173 for (i = 0; i < addrlen; i++) {
183 while ((xb & (1 << j)) == 0)
186 return (i * 32 + 31 - j);
191 * we should *never* get to this point since that
192 * would mean the addrs are equal
194 * However, we do get to it 8) And exacly, when
195 * addresses are equal 8)
197 * ip route add 1111::/128 via ...
198 * ip route add 1111::/64 via ...
201 * Ideally, this function should stop comparison
202 * at prefix length. It does not, but it is still OK,
203 * if returned value is greater than prefix length.
210 static __inline__ struct fib6_node * node_alloc(void)
212 struct fib6_node *fn;
214 if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
215 memset(fn, 0, sizeof(struct fib6_node));
220 static __inline__ void node_free(struct fib6_node * fn)
222 kmem_cache_free(fib6_node_kmem, fn);
225 static __inline__ void rt6_release(struct rt6_info *rt)
227 if (atomic_dec_and_test(&rt->rt6i_ref))
228 dst_free(&rt->u.dst);
235 * return the apropriate node for a routing tree "add" operation
236 * by either creating and inserting or by returning an existing
240 static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
241 int addrlen, int plen,
244 struct fib6_node *fn, *in, *ln;
245 struct fib6_node *pn = NULL;
249 __u32 sernum = fib6_new_sernum();
251 RT6_TRACE("fib6_add_1\n");
253 /* insert node in tree */
258 key = (struct rt6key *)((u8 *)fn->leaf + offset);
263 if (plen < fn->fn_bit ||
264 !addr_match(&key->addr, addr, fn->fn_bit))
271 if (plen == fn->fn_bit) {
272 /* clean up an intermediate node */
273 if ((fn->fn_flags & RTN_RTINFO) == 0) {
274 rt6_release(fn->leaf);
278 fn->fn_sernum = sernum;
284 * We have more bits to go
287 /* Try to walk down on tree. */
288 fn->fn_sernum = sernum;
289 dir = addr_bit_set(addr, fn->fn_bit);
291 fn = dir ? fn->right: fn->left;
295 * We walked to the bottom of tree.
296 * Create new leaf node without children.
306 ln->fn_sernum = sernum;
318 * split since we don't have a common prefix anymore or
319 * we have a less significant route.
320 * we've to insert an intermediate node on the list
321 * this new node will point to the one we need to create
327 /* find 1st bit in difference between the 2 addrs.
329 See comment in addr_diff: bit may be an invalid value,
330 but if it is >= plen, the value is ignored in any case.
333 bit = addr_diff(addr, &key->addr, addrlen);
338 * (new leaf node)[ln] (old node)[fn]
344 if (in == NULL || ln == NULL) {
353 * new intermediate node.
355 * be off since that an address that chooses one of
356 * the branches would not match less specific routes
357 * in the other branch
364 atomic_inc(&in->leaf->rt6i_ref);
366 in->fn_sernum = sernum;
368 /* update parent pointer */
379 ln->fn_sernum = sernum;
381 if (addr_bit_set(addr, bit)) {
388 } else { /* plen <= bit */
391 * (new leaf node)[ln]
393 * (old node)[fn] NULL
405 ln->fn_sernum = sernum;
412 if (addr_bit_set(&key->addr, plen))
423 * Insert routing information in a node.
426 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
427 struct nlmsghdr *nlh)
429 struct rt6_info *iter = NULL;
430 struct rt6_info **ins;
434 if (fn->fn_flags&RTN_TL_ROOT &&
435 fn->leaf == &ip6_null_entry &&
436 !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF | RTF_ALLONLINK)) ){
438 * The top fib of ip6 routing table includes ip6_null_entry.
445 for (iter = fn->leaf; iter; iter=iter->u.next) {
447 * Search for duplicates
450 if (iter->rt6i_metric == rt->rt6i_metric) {
452 * Same priority level
455 if ((iter->rt6i_dev == rt->rt6i_dev) &&
456 (iter->rt6i_flowr == rt->rt6i_flowr) &&
457 (ipv6_addr_cmp(&iter->rt6i_gateway,
458 &rt->rt6i_gateway) == 0)) {
459 if (!(iter->rt6i_flags&RTF_EXPIRES))
461 iter->rt6i_expires = rt->rt6i_expires;
462 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
463 iter->rt6i_flags &= ~RTF_EXPIRES;
464 iter->rt6i_expires = 0;
470 if (iter->rt6i_metric > rt->rt6i_metric)
484 atomic_inc(&rt->rt6i_ref);
485 inet6_rt_notify(RTM_NEWROUTE, rt, nlh);
486 rt6_stats.fib_rt_entries++;
488 if ((fn->fn_flags & RTN_RTINFO) == 0) {
489 rt6_stats.fib_route_nodes++;
490 fn->fn_flags |= RTN_RTINFO;
496 static __inline__ void fib6_start_gc(struct rt6_info *rt)
498 if (ip6_fib_timer.expires == 0 &&
499 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
500 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
504 * Add routing information to the routing tree.
505 * <destination addr>/<source addr>
506 * with source addr info in sub-trees
509 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nlmsghdr *nlh)
511 struct fib6_node *fn;
514 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
515 rt->rt6i_dst.plen, (u8*) &rt->rt6i_dst - (u8*) rt);
520 #ifdef CONFIG_IPV6_SUBTREES
521 if (rt->rt6i_src.plen) {
522 struct fib6_node *sn;
524 if (fn->subtree == NULL) {
525 struct fib6_node *sfn;
537 /* Create subtree root node */
542 sfn->leaf = &ip6_null_entry;
543 atomic_inc(&ip6_null_entry.rt6i_ref);
544 sfn->fn_flags = RTN_ROOT;
545 sfn->fn_sernum = fib6_new_sernum();
547 /* Now add the first leaf node to new subtree */
549 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
550 sizeof(struct in6_addr), rt->rt6i_src.plen,
551 (u8*) &rt->rt6i_src - (u8*) rt);
554 /* If it is failed, discard just allocated
555 root, and then (in st_failure) stale node
562 /* Now link new subtree to main tree */
565 if (fn->leaf == NULL) {
567 atomic_inc(&rt->rt6i_ref);
570 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
571 sizeof(struct in6_addr), rt->rt6i_src.plen,
572 (u8*) &rt->rt6i_src - (u8*) rt);
582 err = fib6_add_rt2node(fn, rt, nlh);
586 if (!(rt->rt6i_flags&RTF_CACHE))
587 fib6_prune_clones(fn, rt);
592 dst_free(&rt->u.dst);
595 #ifdef CONFIG_IPV6_SUBTREES
596 /* Subtree creation failed, probably main tree node
597 is orphan. If it is, shoot it.
600 if (fn && !(fn->fn_flags&RTN_RTINFO|RTN_ROOT))
602 dst_free(&rt->u.dst);
608 * Routing tree lookup
613 int offset; /* key offset on rt6_info */
614 struct in6_addr *addr; /* search key */
617 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
618 struct lookup_args *args)
620 struct fib6_node *fn;
630 struct fib6_node *next;
632 dir = addr_bit_set(args->addr, fn->fn_bit);
634 next = dir ? fn->right : fn->left;
644 while ((fn->fn_flags & RTN_ROOT) == 0) {
645 #ifdef CONFIG_IPV6_SUBTREES
647 struct fib6_node *st;
648 struct lookup_args *narg;
653 st = fib6_lookup_1(fn->subtree, narg);
655 if (st && !(st->fn_flags & RTN_ROOT))
661 if (fn->fn_flags & RTN_RTINFO) {
664 key = (struct rt6key *) ((u8 *) fn->leaf +
667 if (addr_match(&key->addr, args->addr, key->plen))
677 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
678 struct in6_addr *saddr)
680 struct lookup_args args[2];
681 struct rt6_info *rt = NULL;
682 struct fib6_node *fn;
684 args[0].offset = (u8*) &rt->rt6i_dst - (u8*) rt;
685 args[0].addr = daddr;
687 #ifdef CONFIG_IPV6_SUBTREES
688 args[1].offset = (u8*) &rt->rt6i_src - (u8*) rt;
689 args[1].addr = saddr;
692 fn = fib6_lookup_1(root, args);
694 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
701 * Get node with sepciafied destination prefix (and source prefix,
702 * if subtrees are used)
706 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
707 struct in6_addr *addr,
708 int plen, int offset)
710 struct fib6_node *fn;
712 for (fn = root; fn ; ) {
713 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
718 if (plen < fn->fn_bit ||
719 !addr_match(&key->addr, addr, fn->fn_bit))
722 if (plen == fn->fn_bit)
726 * We have more bits to go
728 if (addr_bit_set(addr, fn->fn_bit))
736 struct fib6_node * fib6_locate(struct fib6_node *root,
737 struct in6_addr *daddr, int dst_len,
738 struct in6_addr *saddr, int src_len)
740 struct rt6_info *rt = NULL;
741 struct fib6_node *fn;
743 fn = fib6_locate_1(root, daddr, dst_len,
744 (u8*) &rt->rt6i_dst - (u8*) rt);
746 #ifdef CONFIG_IPV6_SUBTREES
748 BUG_TRAP(saddr!=NULL);
752 fn = fib6_locate_1(fn, saddr, src_len,
753 (u8*) &rt->rt6i_src - (u8*) rt);
757 if (fn && fn->fn_flags&RTN_RTINFO)
769 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
771 if (fn->fn_flags&RTN_ROOT)
772 return &ip6_null_entry;
776 return fn->left->leaf;
779 return fn->right->leaf;
787 * Called to trim the tree of intermediate nodes when possible. "fn"
788 * is the node we want to try and remove.
791 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
795 struct fib6_node *child, *pn;
796 struct fib6_walker_t *w;
800 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
803 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
804 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
805 BUG_TRAP(fn->leaf==NULL);
809 if (fn->right) child = fn->right, children |= 1;
810 if (fn->left) child = fn->left, children |= 2;
812 if (children == 3 || SUBTREE(fn)
813 #ifdef CONFIG_IPV6_SUBTREES
814 /* Subtree root (i.e. fn) may have one child */
815 || (children && fn->fn_flags&RTN_ROOT)
818 fn->leaf = fib6_find_prefix(fn);
820 if (fn->leaf==NULL) {
822 fn->leaf = &ip6_null_entry;
825 atomic_inc(&fn->leaf->rt6i_ref);
830 #ifdef CONFIG_IPV6_SUBTREES
831 if (SUBTREE(pn) == fn) {
832 BUG_TRAP(fn->fn_flags&RTN_ROOT);
836 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
838 if (pn->right == fn) pn->right = child;
839 else if (pn->left == fn) pn->left = child;
846 #ifdef CONFIG_IPV6_SUBTREES
850 read_lock(&fib6_walker_lock);
854 w->root = w->node = NULL;
855 RT6_TRACE("W %p adjusted by delroot 1\n", w);
856 } else if (w->node == fn) {
857 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
864 RT6_TRACE("W %p adjusted by delroot 2\n", w);
869 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
870 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
872 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
873 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
878 read_unlock(&fib6_walker_lock);
881 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
884 rt6_release(pn->leaf);
890 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
891 struct nlmsghdr *nlh)
893 struct fib6_walker_t *w;
894 struct rt6_info *rt = *rtp;
896 RT6_TRACE("fib6_del_route\n");
900 rt->rt6i_node = NULL;
901 rt6_stats.fib_rt_entries--;
904 read_lock(&fib6_walker_lock);
906 if (w->state == FWS_C && w->leaf == rt) {
907 RT6_TRACE("walker %p adjusted by delroute\n", w);
908 w->leaf = rt->u.next;
913 read_unlock(&fib6_walker_lock);
917 if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
918 fn->leaf = &ip6_null_entry;
920 /* If it was last route, expunge its radix tree node */
921 if (fn->leaf == NULL) {
922 fn->fn_flags &= ~RTN_RTINFO;
923 rt6_stats.fib_route_nodes--;
924 fn = fib6_repair_tree(fn);
927 if (atomic_read(&rt->rt6i_ref) != 1) {
928 /* This route is used as dummy address holder in some split
929 * nodes. It is not leaked, but it still holds other resources,
930 * which must be released in time. So, scan ascendant nodes
931 * and replace dummy references to this route with references
932 * to still alive ones.
935 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
936 fn->leaf = fib6_find_prefix(fn);
937 atomic_inc(&fn->leaf->rt6i_ref);
942 /* No more references are possiible at this point. */
943 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
946 inet6_rt_notify(RTM_DELROUTE, rt, nlh);
950 int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh)
952 struct fib6_node *fn = rt->rt6i_node;
953 struct rt6_info **rtp;
956 if (rt->u.dst.obsolete>0) {
961 if (fn == NULL || rt == &ip6_null_entry)
964 BUG_TRAP(fn->fn_flags&RTN_RTINFO);
966 if (!(rt->rt6i_flags&RTF_CACHE))
967 fib6_prune_clones(fn, rt);
970 * Walk the leaf entries looking for ourself
973 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
975 fib6_del_route(fn, rtp, nlh);
983 * Tree traversal function.
985 * Certainly, it is not interrupt safe.
986 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
987 * It means, that we can modify tree during walking
988 * and use this function for garbage collection, clone pruning,
989 * cleaning tree when a device goes down etc. etc.
991 * It guarantees that every node will be traversed,
992 * and that it will be traversed only once.
994 * Callback function w->func may return:
995 * 0 -> continue walking.
996 * positive value -> walking is suspended (used by tree dumps,
997 * and probably by gc, if it will be split to several slices)
998 * negative value -> terminate walking.
1000 * The function itself returns:
1001 * 0 -> walk is complete.
1002 * >0 -> walk is incomplete (i.e. suspended)
1003 * <0 -> walk is terminated by an error.
1006 int fib6_walk_continue(struct fib6_walker_t *w)
1008 struct fib6_node *fn, *pn;
1015 if (w->prune && fn != w->root &&
1016 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
1021 #ifdef CONFIG_IPV6_SUBTREES
1024 w->node = SUBTREE(fn);
1032 w->state = FWS_INIT;
1038 w->node = fn->right;
1039 w->state = FWS_INIT;
1045 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1046 int err = w->func(w);
1057 #ifdef CONFIG_IPV6_SUBTREES
1058 if (SUBTREE(pn) == fn) {
1059 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1064 if (pn->left == fn) {
1068 if (pn->right == fn) {
1070 w->leaf = w->node->leaf;
1080 int fib6_walk(struct fib6_walker_t *w)
1084 w->state = FWS_INIT;
1087 fib6_walker_link(w);
1088 res = fib6_walk_continue(w);
1090 fib6_walker_unlink(w);
1094 static int fib6_clean_node(struct fib6_walker_t *w)
1097 struct rt6_info *rt;
1098 struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1100 for (rt = w->leaf; rt; rt = rt->u.next) {
1101 res = c->func(rt, c->arg);
1104 res = fib6_del(rt, NULL);
1107 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1120 * Convenient frontend to tree walker.
1122 * func is called on each route.
1123 * It may return -1 -> delete this route.
1124 * 0 -> continue walking
1126 * prune==1 -> only immediate children of node (certainly,
1127 * ignoring pure split nodes) will be scanned.
1130 void fib6_clean_tree(struct fib6_node *root,
1131 int (*func)(struct rt6_info *, void *arg),
1132 int prune, void *arg)
1134 struct fib6_cleaner_t c;
1137 c.w.func = fib6_clean_node;
1145 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1147 if (rt->rt6i_flags & RTF_CACHE) {
1148 RT6_TRACE("pruning clone %p\n", rt);
1155 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1157 fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1161 * Garbage collection
1164 static struct fib6_gc_args
1170 static int fib6_age(struct rt6_info *rt, void *arg)
1172 unsigned long now = jiffies;
1174 /* Age clones. Note, that clones are aged out
1175 only if they are not in use now.
1179 * check addrconf expiration here.
1180 * They are expired even if they are in use.
1183 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1184 if (time_after(now, rt->rt6i_expires)) {
1185 RT6_TRACE("expiring %p\n", rt);
1189 } else if (rt->rt6i_flags & RTF_CACHE) {
1190 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1191 time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1192 RT6_TRACE("aging clone %p\n", rt);
1201 static spinlock_t fib6_gc_lock = SPIN_LOCK_UNLOCKED;
1203 void fib6_run_gc(unsigned long dummy)
1205 if (dummy != ~0UL) {
1206 spin_lock_bh(&fib6_gc_lock);
1207 gc_args.timeout = (int)dummy;
1210 if (!spin_trylock(&fib6_gc_lock)) {
1211 mod_timer(&ip6_fib_timer, jiffies + HZ);
1215 gc_args.timeout = ip6_rt_gc_interval;
1220 write_lock_bh(&rt6_lock);
1221 fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
1222 write_unlock_bh(&rt6_lock);
1225 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1227 del_timer(&ip6_fib_timer);
1228 ip6_fib_timer.expires = 0;
1230 spin_unlock_bh(&fib6_gc_lock);
1233 void __init fib6_init(void)
1235 if (!fib6_node_kmem)
1236 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1237 sizeof(struct fib6_node),
1238 0, SLAB_HWCACHE_ALIGN,
1243 void fib6_gc_cleanup(void)
1245 del_timer(&ip6_fib_timer);