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/errno.h>
23 #include <linux/types.h>
24 #include <linux/net.h>
25 #include <linux/route.h>
26 #include <linux/netdevice.h>
27 #include <linux/in6.h>
28 #include <linux/init.h>
31 #include <linux/proc_fs.h>
35 #include <net/ndisc.h>
36 #include <net/addrconf.h>
38 #include <net/ip6_fib.h>
39 #include <net/ip6_route.h>
44 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
46 #define RT6_TRACE(x...) do { ; } while (0)
49 struct rt6_statistics rt6_stats;
51 static kmem_cache_t * fib6_node_kmem __read_mostly;
55 #ifdef CONFIG_IPV6_SUBTREES
66 struct fib6_walker_t w;
67 int (*func)(struct rt6_info *, void *arg);
71 DEFINE_RWLOCK(fib6_walker_lock);
74 #ifdef CONFIG_IPV6_SUBTREES
75 #define FWS_INIT FWS_S
76 #define SUBTREE(fn) ((fn)->subtree)
78 #define FWS_INIT FWS_L
79 #define SUBTREE(fn) NULL
82 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
83 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
86 * A routing update causes an increase of the serial number on the
87 * affected subtree. This allows for cached routes to be asynchronously
88 * tested when modifications are made to the destination cache as a
89 * result of redirects, path MTU changes, etc.
92 static __u32 rt_sernum;
94 static DEFINE_TIMER(ip6_fib_timer, fib6_run_gc, 0, 0);
96 struct fib6_walker_t fib6_walker_list = {
97 .prev = &fib6_walker_list,
98 .next = &fib6_walker_list,
101 #define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
103 static __inline__ u32 fib6_new_sernum(void)
112 * Auxiliary address test functions for the radix tree.
114 * These assume a 32bit processor (although it will work on
122 static __inline__ int addr_bit_set(void *token, int fn_bit)
126 return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
129 static __inline__ struct fib6_node * node_alloc(void)
131 struct fib6_node *fn;
133 if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
134 memset(fn, 0, sizeof(struct fib6_node));
139 static __inline__ void node_free(struct fib6_node * fn)
141 kmem_cache_free(fib6_node_kmem, fn);
144 static __inline__ void rt6_release(struct rt6_info *rt)
146 if (atomic_dec_and_test(&rt->rt6i_ref))
147 dst_free(&rt->u.dst);
154 * return the appropriate node for a routing tree "add" operation
155 * by either creating and inserting or by returning an existing
159 static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
160 int addrlen, int plen,
163 struct fib6_node *fn, *in, *ln;
164 struct fib6_node *pn = NULL;
168 __u32 sernum = fib6_new_sernum();
170 RT6_TRACE("fib6_add_1\n");
172 /* insert node in tree */
177 key = (struct rt6key *)((u8 *)fn->leaf + offset);
182 if (plen < fn->fn_bit ||
183 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
190 if (plen == fn->fn_bit) {
191 /* clean up an intermediate node */
192 if ((fn->fn_flags & RTN_RTINFO) == 0) {
193 rt6_release(fn->leaf);
197 fn->fn_sernum = sernum;
203 * We have more bits to go
206 /* Try to walk down on tree. */
207 fn->fn_sernum = sernum;
208 dir = addr_bit_set(addr, fn->fn_bit);
210 fn = dir ? fn->right: fn->left;
214 * We walked to the bottom of tree.
215 * Create new leaf node without children.
225 ln->fn_sernum = sernum;
237 * split since we don't have a common prefix anymore or
238 * we have a less significant route.
239 * we've to insert an intermediate node on the list
240 * this new node will point to the one we need to create
246 /* find 1st bit in difference between the 2 addrs.
248 See comment in __ipv6_addr_diff: bit may be an invalid value,
249 but if it is >= plen, the value is ignored in any case.
252 bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
257 * (new leaf node)[ln] (old node)[fn]
263 if (in == NULL || ln == NULL) {
272 * new intermediate node.
274 * be off since that an address that chooses one of
275 * the branches would not match less specific routes
276 * in the other branch
283 atomic_inc(&in->leaf->rt6i_ref);
285 in->fn_sernum = sernum;
287 /* update parent pointer */
298 ln->fn_sernum = sernum;
300 if (addr_bit_set(addr, bit)) {
307 } else { /* plen <= bit */
310 * (new leaf node)[ln]
312 * (old node)[fn] NULL
324 ln->fn_sernum = sernum;
331 if (addr_bit_set(&key->addr, plen))
342 * Insert routing information in a node.
345 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
346 struct nlmsghdr *nlh, struct netlink_skb_parms *req)
348 struct rt6_info *iter = NULL;
349 struct rt6_info **ins;
353 if (fn->fn_flags&RTN_TL_ROOT &&
354 fn->leaf == &ip6_null_entry &&
355 !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
361 for (iter = fn->leaf; iter; iter=iter->u.next) {
363 * Search for duplicates
366 if (iter->rt6i_metric == rt->rt6i_metric) {
368 * Same priority level
371 if (iter->rt6i_dev == rt->rt6i_dev &&
372 iter->rt6i_idev == rt->rt6i_idev &&
373 ipv6_addr_equal(&iter->rt6i_gateway,
374 &rt->rt6i_gateway)) {
375 if (!(iter->rt6i_flags&RTF_EXPIRES))
377 iter->rt6i_expires = rt->rt6i_expires;
378 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
379 iter->rt6i_flags &= ~RTF_EXPIRES;
380 iter->rt6i_expires = 0;
386 if (iter->rt6i_metric > rt->rt6i_metric)
400 atomic_inc(&rt->rt6i_ref);
401 inet6_rt_notify(RTM_NEWROUTE, rt, nlh, req);
402 rt6_stats.fib_rt_entries++;
404 if ((fn->fn_flags & RTN_RTINFO) == 0) {
405 rt6_stats.fib_route_nodes++;
406 fn->fn_flags |= RTN_RTINFO;
412 static __inline__ void fib6_start_gc(struct rt6_info *rt)
414 if (ip6_fib_timer.expires == 0 &&
415 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
416 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
419 void fib6_force_start_gc(void)
421 if (ip6_fib_timer.expires == 0)
422 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
426 * Add routing information to the routing tree.
427 * <destination addr>/<source addr>
428 * with source addr info in sub-trees
431 int fib6_add(struct fib6_node *root, struct rt6_info *rt,
432 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
434 struct fib6_node *fn;
437 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
438 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
443 #ifdef CONFIG_IPV6_SUBTREES
444 if (rt->rt6i_src.plen) {
445 struct fib6_node *sn;
447 if (fn->subtree == NULL) {
448 struct fib6_node *sfn;
460 /* Create subtree root node */
465 sfn->leaf = &ip6_null_entry;
466 atomic_inc(&ip6_null_entry.rt6i_ref);
467 sfn->fn_flags = RTN_ROOT;
468 sfn->fn_sernum = fib6_new_sernum();
470 /* Now add the first leaf node to new subtree */
472 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
473 sizeof(struct in6_addr), rt->rt6i_src.plen,
474 offsetof(struct rt6_info, rt6i_src));
477 /* If it is failed, discard just allocated
478 root, and then (in st_failure) stale node
485 /* Now link new subtree to main tree */
488 if (fn->leaf == NULL) {
490 atomic_inc(&rt->rt6i_ref);
493 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
494 sizeof(struct in6_addr), rt->rt6i_src.plen,
495 offsetof(struct rt6_info, rt6i_src));
505 err = fib6_add_rt2node(fn, rt, nlh, req);
509 if (!(rt->rt6i_flags&RTF_CACHE))
510 fib6_prune_clones(fn, rt);
515 dst_free(&rt->u.dst);
518 #ifdef CONFIG_IPV6_SUBTREES
519 /* Subtree creation failed, probably main tree node
520 is orphan. If it is, shoot it.
523 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
524 fib6_repair_tree(fn);
525 dst_free(&rt->u.dst);
531 * Routing tree lookup
536 int offset; /* key offset on rt6_info */
537 struct in6_addr *addr; /* search key */
540 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
541 struct lookup_args *args)
543 struct fib6_node *fn;
553 struct fib6_node *next;
555 dir = addr_bit_set(args->addr, fn->fn_bit);
557 next = dir ? fn->right : fn->left;
567 while ((fn->fn_flags & RTN_ROOT) == 0) {
568 #ifdef CONFIG_IPV6_SUBTREES
570 struct fib6_node *st;
571 struct lookup_args *narg;
576 st = fib6_lookup_1(fn->subtree, narg);
578 if (st && !(st->fn_flags & RTN_ROOT))
584 if (fn->fn_flags & RTN_RTINFO) {
587 key = (struct rt6key *) ((u8 *) fn->leaf +
590 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
600 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
601 struct in6_addr *saddr)
603 struct lookup_args args[2];
604 struct fib6_node *fn;
606 args[0].offset = offsetof(struct rt6_info, rt6i_dst);
607 args[0].addr = daddr;
609 #ifdef CONFIG_IPV6_SUBTREES
610 args[1].offset = offsetof(struct rt6_info, rt6i_src);
611 args[1].addr = saddr;
614 fn = fib6_lookup_1(root, args);
616 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
623 * Get node with specified destination prefix (and source prefix,
624 * if subtrees are used)
628 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
629 struct in6_addr *addr,
630 int plen, int offset)
632 struct fib6_node *fn;
634 for (fn = root; fn ; ) {
635 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
640 if (plen < fn->fn_bit ||
641 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
644 if (plen == fn->fn_bit)
648 * We have more bits to go
650 if (addr_bit_set(addr, fn->fn_bit))
658 struct fib6_node * fib6_locate(struct fib6_node *root,
659 struct in6_addr *daddr, int dst_len,
660 struct in6_addr *saddr, int src_len)
662 struct fib6_node *fn;
664 fn = fib6_locate_1(root, daddr, dst_len,
665 offsetof(struct rt6_info, rt6i_dst));
667 #ifdef CONFIG_IPV6_SUBTREES
669 BUG_TRAP(saddr!=NULL);
673 fn = fib6_locate_1(fn, saddr, src_len,
674 offsetof(struct rt6_info, rt6i_src));
678 if (fn && fn->fn_flags&RTN_RTINFO)
690 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
692 if (fn->fn_flags&RTN_ROOT)
693 return &ip6_null_entry;
697 return fn->left->leaf;
700 return fn->right->leaf;
708 * Called to trim the tree of intermediate nodes when possible. "fn"
709 * is the node we want to try and remove.
712 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
716 struct fib6_node *child, *pn;
717 struct fib6_walker_t *w;
721 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
724 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
725 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
726 BUG_TRAP(fn->leaf==NULL);
730 if (fn->right) child = fn->right, children |= 1;
731 if (fn->left) child = fn->left, children |= 2;
733 if (children == 3 || SUBTREE(fn)
734 #ifdef CONFIG_IPV6_SUBTREES
735 /* Subtree root (i.e. fn) may have one child */
736 || (children && fn->fn_flags&RTN_ROOT)
739 fn->leaf = fib6_find_prefix(fn);
741 if (fn->leaf==NULL) {
743 fn->leaf = &ip6_null_entry;
746 atomic_inc(&fn->leaf->rt6i_ref);
751 #ifdef CONFIG_IPV6_SUBTREES
752 if (SUBTREE(pn) == fn) {
753 BUG_TRAP(fn->fn_flags&RTN_ROOT);
757 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
759 if (pn->right == fn) pn->right = child;
760 else if (pn->left == fn) pn->left = child;
767 #ifdef CONFIG_IPV6_SUBTREES
771 read_lock(&fib6_walker_lock);
775 w->root = w->node = NULL;
776 RT6_TRACE("W %p adjusted by delroot 1\n", w);
777 } else if (w->node == fn) {
778 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
785 RT6_TRACE("W %p adjusted by delroot 2\n", w);
790 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
791 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
793 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
794 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
799 read_unlock(&fib6_walker_lock);
802 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
805 rt6_release(pn->leaf);
811 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
812 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
814 struct fib6_walker_t *w;
815 struct rt6_info *rt = *rtp;
817 RT6_TRACE("fib6_del_route\n");
821 rt->rt6i_node = NULL;
822 rt6_stats.fib_rt_entries--;
823 rt6_stats.fib_discarded_routes++;
826 read_lock(&fib6_walker_lock);
828 if (w->state == FWS_C && w->leaf == rt) {
829 RT6_TRACE("walker %p adjusted by delroute\n", w);
830 w->leaf = rt->u.next;
835 read_unlock(&fib6_walker_lock);
839 if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
840 fn->leaf = &ip6_null_entry;
842 /* If it was last route, expunge its radix tree node */
843 if (fn->leaf == NULL) {
844 fn->fn_flags &= ~RTN_RTINFO;
845 rt6_stats.fib_route_nodes--;
846 fn = fib6_repair_tree(fn);
849 if (atomic_read(&rt->rt6i_ref) != 1) {
850 /* This route is used as dummy address holder in some split
851 * nodes. It is not leaked, but it still holds other resources,
852 * which must be released in time. So, scan ascendant nodes
853 * and replace dummy references to this route with references
854 * to still alive ones.
857 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
858 fn->leaf = fib6_find_prefix(fn);
859 atomic_inc(&fn->leaf->rt6i_ref);
864 /* No more references are possible at this point. */
865 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
868 inet6_rt_notify(RTM_DELROUTE, rt, nlh, req);
872 int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
874 struct fib6_node *fn = rt->rt6i_node;
875 struct rt6_info **rtp;
878 if (rt->u.dst.obsolete>0) {
883 if (fn == NULL || rt == &ip6_null_entry)
886 BUG_TRAP(fn->fn_flags&RTN_RTINFO);
888 if (!(rt->rt6i_flags&RTF_CACHE))
889 fib6_prune_clones(fn, rt);
892 * Walk the leaf entries looking for ourself
895 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
897 fib6_del_route(fn, rtp, nlh, _rtattr, req);
905 * Tree traversal function.
907 * Certainly, it is not interrupt safe.
908 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
909 * It means, that we can modify tree during walking
910 * and use this function for garbage collection, clone pruning,
911 * cleaning tree when a device goes down etc. etc.
913 * It guarantees that every node will be traversed,
914 * and that it will be traversed only once.
916 * Callback function w->func may return:
917 * 0 -> continue walking.
918 * positive value -> walking is suspended (used by tree dumps,
919 * and probably by gc, if it will be split to several slices)
920 * negative value -> terminate walking.
922 * The function itself returns:
923 * 0 -> walk is complete.
924 * >0 -> walk is incomplete (i.e. suspended)
925 * <0 -> walk is terminated by an error.
928 int fib6_walk_continue(struct fib6_walker_t *w)
930 struct fib6_node *fn, *pn;
937 if (w->prune && fn != w->root &&
938 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
943 #ifdef CONFIG_IPV6_SUBTREES
946 w->node = SUBTREE(fn);
967 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
968 int err = w->func(w);
979 #ifdef CONFIG_IPV6_SUBTREES
980 if (SUBTREE(pn) == fn) {
981 BUG_TRAP(fn->fn_flags&RTN_ROOT);
986 if (pn->left == fn) {
990 if (pn->right == fn) {
992 w->leaf = w->node->leaf;
1002 int fib6_walk(struct fib6_walker_t *w)
1006 w->state = FWS_INIT;
1009 fib6_walker_link(w);
1010 res = fib6_walk_continue(w);
1012 fib6_walker_unlink(w);
1016 static int fib6_clean_node(struct fib6_walker_t *w)
1019 struct rt6_info *rt;
1020 struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1022 for (rt = w->leaf; rt; rt = rt->u.next) {
1023 res = c->func(rt, c->arg);
1026 res = fib6_del(rt, NULL, NULL, NULL);
1029 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1042 * Convenient frontend to tree walker.
1044 * func is called on each route.
1045 * It may return -1 -> delete this route.
1046 * 0 -> continue walking
1048 * prune==1 -> only immediate children of node (certainly,
1049 * ignoring pure split nodes) will be scanned.
1052 void fib6_clean_tree(struct fib6_node *root,
1053 int (*func)(struct rt6_info *, void *arg),
1054 int prune, void *arg)
1056 struct fib6_cleaner_t c;
1059 c.w.func = fib6_clean_node;
1067 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1069 if (rt->rt6i_flags & RTF_CACHE) {
1070 RT6_TRACE("pruning clone %p\n", rt);
1077 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1079 fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1083 * Garbage collection
1086 static struct fib6_gc_args
1092 static int fib6_age(struct rt6_info *rt, void *arg)
1094 unsigned long now = jiffies;
1097 * check addrconf expiration here.
1098 * Routes are expired even if they are in use.
1100 * Also age clones. Note, that clones are aged out
1101 * only if they are not in use now.
1104 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1105 if (time_after(now, rt->rt6i_expires)) {
1106 RT6_TRACE("expiring %p\n", rt);
1110 } else if (rt->rt6i_flags & RTF_CACHE) {
1111 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1112 time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1113 RT6_TRACE("aging clone %p\n", rt);
1115 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1116 (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1117 RT6_TRACE("purging route %p via non-router but gateway\n",
1127 static DEFINE_SPINLOCK(fib6_gc_lock);
1129 void fib6_run_gc(unsigned long dummy)
1131 if (dummy != ~0UL) {
1132 spin_lock_bh(&fib6_gc_lock);
1133 gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
1136 if (!spin_trylock(&fib6_gc_lock)) {
1137 mod_timer(&ip6_fib_timer, jiffies + HZ);
1141 gc_args.timeout = ip6_rt_gc_interval;
1146 write_lock_bh(&rt6_lock);
1147 ndisc_dst_gc(&gc_args.more);
1148 fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
1149 write_unlock_bh(&rt6_lock);
1152 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1154 del_timer(&ip6_fib_timer);
1155 ip6_fib_timer.expires = 0;
1157 spin_unlock_bh(&fib6_gc_lock);
1160 void __init fib6_init(void)
1162 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1163 sizeof(struct fib6_node),
1164 0, SLAB_HWCACHE_ALIGN,
1166 if (!fib6_node_kmem)
1167 panic("cannot create fib6_nodes cache");
1170 void fib6_gc_cleanup(void)
1172 del_timer(&ip6_fib_timer);
1173 kmem_cache_destroy(fib6_node_kmem);