[IPV6]: Increase number of possible routing tables to 2^32
[linux-2.6.22.y-op.git] / net / ipv6 / ip6_fib.c
blobbececbe9dd2cc197b93da57108aa1aa7f9ee83a8
1 /*
2 * Linux INET6 implementation
3 * Forwarding Information Database
5 * Authors:
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.
17 * Changes:
18 * Yuji SEKIYA @USAGI: Support default route on router node;
19 * remove ip6_null_entry from the top of
20 * routing table.
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>
29 #include <linux/list.h>
31 #ifdef CONFIG_PROC_FS
32 #include <linux/proc_fs.h>
33 #endif
35 #include <net/ipv6.h>
36 #include <net/ndisc.h>
37 #include <net/addrconf.h>
39 #include <net/ip6_fib.h>
40 #include <net/ip6_route.h>
42 #define RT6_DEBUG 2
44 #if RT6_DEBUG >= 3
45 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
46 #else
47 #define RT6_TRACE(x...) do { ; } while (0)
48 #endif
50 struct rt6_statistics rt6_stats;
52 static kmem_cache_t * fib6_node_kmem __read_mostly;
54 enum fib_walk_state_t
56 #ifdef CONFIG_IPV6_SUBTREES
57 FWS_S,
58 #endif
59 FWS_L,
60 FWS_R,
61 FWS_C,
62 FWS_U
65 struct fib6_cleaner_t
67 struct fib6_walker_t w;
68 int (*func)(struct rt6_info *, void *arg);
69 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)
78 #else
79 #define FWS_INIT FWS_L
80 #define SUBTREE(fn) NULL
81 #endif
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)
106 u32 n = ++rt_sernum;
107 if ((__s32)n <= 0)
108 rt_sernum = n = 1;
109 return n;
113 * Auxiliary address test functions for the radix tree.
115 * These assume a 32bit processor (although it will work on
116 * 64bit processors)
120 * test bit
123 static __inline__ int addr_bit_set(void *token, int fn_bit)
125 __u32 *addr = token;
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));
137 return fn;
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);
151 static struct fib6_table fib6_main_tbl = {
152 .tb6_id = RT6_TABLE_MAIN,
153 .tb6_lock = RW_LOCK_UNLOCKED,
154 .tb6_root = {
155 .leaf = &ip6_null_entry,
156 .fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
160 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
161 #define FIB_TABLE_HASHSZ 256
162 #else
163 #define FIB_TABLE_HASHSZ 1
164 #endif
165 static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
167 static void fib6_link_table(struct fib6_table *tb)
169 unsigned int h;
171 h = tb->tb6_id & (FIB_TABLE_HASHSZ - 1);
174 * No protection necessary, this is the only list mutatation
175 * operation, tables never disappear once they exist.
177 hlist_add_head_rcu(&tb->tb6_hlist, &fib_table_hash[h]);
180 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
181 static struct fib6_table fib6_local_tbl = {
182 .tb6_id = RT6_TABLE_LOCAL,
183 .tb6_lock = RW_LOCK_UNLOCKED,
184 .tb6_root = {
185 .leaf = &ip6_null_entry,
186 .fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
190 static struct fib6_table *fib6_alloc_table(u32 id)
192 struct fib6_table *table;
194 table = kzalloc(sizeof(*table), GFP_ATOMIC);
195 if (table != NULL) {
196 table->tb6_id = id;
197 table->tb6_lock = RW_LOCK_UNLOCKED;
198 table->tb6_root.leaf = &ip6_null_entry;
199 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
202 return table;
205 struct fib6_table *fib6_new_table(u32 id)
207 struct fib6_table *tb;
209 if (id == 0)
210 id = RT6_TABLE_MAIN;
211 tb = fib6_get_table(id);
212 if (tb)
213 return tb;
215 tb = fib6_alloc_table(id);
216 if (tb != NULL)
217 fib6_link_table(tb);
219 return tb;
222 struct fib6_table *fib6_get_table(u32 id)
224 struct fib6_table *tb;
225 struct hlist_node *node;
226 unsigned int h;
228 if (id == 0)
229 id = RT6_TABLE_MAIN;
230 h = id & (FIB_TABLE_HASHSZ - 1);
231 rcu_read_lock();
232 hlist_for_each_entry_rcu(tb, node, &fib_table_hash[h], tb6_hlist) {
233 if (tb->tb6_id == id) {
234 rcu_read_unlock();
235 return tb;
238 rcu_read_unlock();
240 return NULL;
243 static void __init fib6_tables_init(void)
245 fib6_link_table(&fib6_main_tbl);
246 fib6_link_table(&fib6_local_tbl);
249 #else
251 struct fib6_table *fib6_new_table(u32 id)
253 return fib6_get_table(id);
256 struct fib6_table *fib6_get_table(u32 id)
258 return &fib6_main_tbl;
261 struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
262 pol_lookup_t lookup)
264 return (struct dst_entry *) lookup(&fib6_main_tbl, fl, flags);
267 static void __init fib6_tables_init(void)
269 fib6_link_table(&fib6_main_tbl);
272 #endif
274 static int fib6_dump_node(struct fib6_walker_t *w)
276 int res;
277 struct rt6_info *rt;
279 for (rt = w->leaf; rt; rt = rt->u.next) {
280 res = rt6_dump_route(rt, w->args);
281 if (res < 0) {
282 /* Frame is full, suspend walking */
283 w->leaf = rt;
284 return 1;
286 BUG_TRAP(res!=0);
288 w->leaf = NULL;
289 return 0;
292 static void fib6_dump_end(struct netlink_callback *cb)
294 struct fib6_walker_t *w = (void*)cb->args[2];
296 if (w) {
297 cb->args[2] = 0;
298 kfree(w);
300 cb->done = (void*)cb->args[3];
301 cb->args[1] = 3;
304 static int fib6_dump_done(struct netlink_callback *cb)
306 fib6_dump_end(cb);
307 return cb->done ? cb->done(cb) : 0;
310 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
311 struct netlink_callback *cb)
313 struct fib6_walker_t *w;
314 int res;
316 w = (void *)cb->args[2];
317 w->root = &table->tb6_root;
319 if (cb->args[4] == 0) {
320 read_lock_bh(&table->tb6_lock);
321 res = fib6_walk(w);
322 read_unlock_bh(&table->tb6_lock);
323 if (res > 0)
324 cb->args[4] = 1;
325 } else {
326 read_lock_bh(&table->tb6_lock);
327 res = fib6_walk_continue(w);
328 read_unlock_bh(&table->tb6_lock);
329 if (res != 0) {
330 if (res < 0)
331 fib6_walker_unlink(w);
332 goto end;
334 fib6_walker_unlink(w);
335 cb->args[4] = 0;
337 end:
338 return res;
341 int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
343 unsigned int h, s_h;
344 unsigned int e = 0, s_e;
345 struct rt6_rtnl_dump_arg arg;
346 struct fib6_walker_t *w;
347 struct fib6_table *tb;
348 struct hlist_node *node;
349 int res = 0;
351 s_h = cb->args[0];
352 s_e = cb->args[1];
354 w = (void *)cb->args[2];
355 if (w == NULL) {
356 /* New dump:
358 * 1. hook callback destructor.
360 cb->args[3] = (long)cb->done;
361 cb->done = fib6_dump_done;
364 * 2. allocate and initialize walker.
366 w = kzalloc(sizeof(*w), GFP_ATOMIC);
367 if (w == NULL)
368 return -ENOMEM;
369 w->func = fib6_dump_node;
370 cb->args[2] = (long)w;
373 arg.skb = skb;
374 arg.cb = cb;
375 w->args = &arg;
377 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
378 e = 0;
379 hlist_for_each_entry(tb, node, &fib_table_hash[h], tb6_hlist) {
380 if (e < s_e)
381 goto next;
382 res = fib6_dump_table(tb, skb, cb);
383 if (res != 0)
384 goto out;
385 next:
386 e++;
389 out:
390 cb->args[1] = e;
391 cb->args[0] = h;
393 res = res < 0 ? res : skb->len;
394 if (res <= 0)
395 fib6_dump_end(cb);
396 return res;
400 * Routing Table
402 * return the appropriate node for a routing tree "add" operation
403 * by either creating and inserting or by returning an existing
404 * node.
407 static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
408 int addrlen, int plen,
409 int offset)
411 struct fib6_node *fn, *in, *ln;
412 struct fib6_node *pn = NULL;
413 struct rt6key *key;
414 int bit;
415 int dir = 0;
416 __u32 sernum = fib6_new_sernum();
418 RT6_TRACE("fib6_add_1\n");
420 /* insert node in tree */
422 fn = root;
424 do {
425 key = (struct rt6key *)((u8 *)fn->leaf + offset);
428 * Prefix match
430 if (plen < fn->fn_bit ||
431 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
432 goto insert_above;
435 * Exact match ?
438 if (plen == fn->fn_bit) {
439 /* clean up an intermediate node */
440 if ((fn->fn_flags & RTN_RTINFO) == 0) {
441 rt6_release(fn->leaf);
442 fn->leaf = NULL;
445 fn->fn_sernum = sernum;
447 return fn;
451 * We have more bits to go
454 /* Try to walk down on tree. */
455 fn->fn_sernum = sernum;
456 dir = addr_bit_set(addr, fn->fn_bit);
457 pn = fn;
458 fn = dir ? fn->right: fn->left;
459 } while (fn);
462 * We walked to the bottom of tree.
463 * Create new leaf node without children.
466 ln = node_alloc();
468 if (ln == NULL)
469 return NULL;
470 ln->fn_bit = plen;
472 ln->parent = pn;
473 ln->fn_sernum = sernum;
475 if (dir)
476 pn->right = ln;
477 else
478 pn->left = ln;
480 return ln;
483 insert_above:
485 * split since we don't have a common prefix anymore or
486 * we have a less significant route.
487 * we've to insert an intermediate node on the list
488 * this new node will point to the one we need to create
489 * and the current
492 pn = fn->parent;
494 /* find 1st bit in difference between the 2 addrs.
496 See comment in __ipv6_addr_diff: bit may be an invalid value,
497 but if it is >= plen, the value is ignored in any case.
500 bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
503 * (intermediate)[in]
504 * / \
505 * (new leaf node)[ln] (old node)[fn]
507 if (plen > bit) {
508 in = node_alloc();
509 ln = node_alloc();
511 if (in == NULL || ln == NULL) {
512 if (in)
513 node_free(in);
514 if (ln)
515 node_free(ln);
516 return NULL;
520 * new intermediate node.
521 * RTN_RTINFO will
522 * be off since that an address that chooses one of
523 * the branches would not match less specific routes
524 * in the other branch
527 in->fn_bit = bit;
529 in->parent = pn;
530 in->leaf = fn->leaf;
531 atomic_inc(&in->leaf->rt6i_ref);
533 in->fn_sernum = sernum;
535 /* update parent pointer */
536 if (dir)
537 pn->right = in;
538 else
539 pn->left = in;
541 ln->fn_bit = plen;
543 ln->parent = in;
544 fn->parent = in;
546 ln->fn_sernum = sernum;
548 if (addr_bit_set(addr, bit)) {
549 in->right = ln;
550 in->left = fn;
551 } else {
552 in->left = ln;
553 in->right = fn;
555 } else { /* plen <= bit */
558 * (new leaf node)[ln]
559 * / \
560 * (old node)[fn] NULL
563 ln = node_alloc();
565 if (ln == NULL)
566 return NULL;
568 ln->fn_bit = plen;
570 ln->parent = pn;
572 ln->fn_sernum = sernum;
574 if (dir)
575 pn->right = ln;
576 else
577 pn->left = ln;
579 if (addr_bit_set(&key->addr, plen))
580 ln->right = fn;
581 else
582 ln->left = fn;
584 fn->parent = ln;
586 return ln;
590 * Insert routing information in a node.
593 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
594 struct nlmsghdr *nlh, struct netlink_skb_parms *req)
596 struct rt6_info *iter = NULL;
597 struct rt6_info **ins;
599 ins = &fn->leaf;
601 if (fn->fn_flags&RTN_TL_ROOT &&
602 fn->leaf == &ip6_null_entry &&
603 !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
604 fn->leaf = rt;
605 rt->u.next = NULL;
606 goto out;
609 for (iter = fn->leaf; iter; iter=iter->u.next) {
611 * Search for duplicates
614 if (iter->rt6i_metric == rt->rt6i_metric) {
616 * Same priority level
619 if (iter->rt6i_dev == rt->rt6i_dev &&
620 iter->rt6i_idev == rt->rt6i_idev &&
621 ipv6_addr_equal(&iter->rt6i_gateway,
622 &rt->rt6i_gateway)) {
623 if (!(iter->rt6i_flags&RTF_EXPIRES))
624 return -EEXIST;
625 iter->rt6i_expires = rt->rt6i_expires;
626 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
627 iter->rt6i_flags &= ~RTF_EXPIRES;
628 iter->rt6i_expires = 0;
630 return -EEXIST;
634 if (iter->rt6i_metric > rt->rt6i_metric)
635 break;
637 ins = &iter->u.next;
641 * insert node
644 out:
645 rt->u.next = iter;
646 *ins = rt;
647 rt->rt6i_node = fn;
648 atomic_inc(&rt->rt6i_ref);
649 inet6_rt_notify(RTM_NEWROUTE, rt, nlh, req);
650 rt6_stats.fib_rt_entries++;
652 if ((fn->fn_flags & RTN_RTINFO) == 0) {
653 rt6_stats.fib_route_nodes++;
654 fn->fn_flags |= RTN_RTINFO;
657 return 0;
660 static __inline__ void fib6_start_gc(struct rt6_info *rt)
662 if (ip6_fib_timer.expires == 0 &&
663 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
664 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
667 void fib6_force_start_gc(void)
669 if (ip6_fib_timer.expires == 0)
670 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
674 * Add routing information to the routing tree.
675 * <destination addr>/<source addr>
676 * with source addr info in sub-trees
679 int fib6_add(struct fib6_node *root, struct rt6_info *rt,
680 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
682 struct fib6_node *fn;
683 int err = -ENOMEM;
685 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
686 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
688 if (fn == NULL)
689 goto out;
691 #ifdef CONFIG_IPV6_SUBTREES
692 if (rt->rt6i_src.plen) {
693 struct fib6_node *sn;
695 if (fn->subtree == NULL) {
696 struct fib6_node *sfn;
699 * Create subtree.
701 * fn[main tree]
703 * sfn[subtree root]
705 * sn[new leaf node]
708 /* Create subtree root node */
709 sfn = node_alloc();
710 if (sfn == NULL)
711 goto st_failure;
713 sfn->leaf = &ip6_null_entry;
714 atomic_inc(&ip6_null_entry.rt6i_ref);
715 sfn->fn_flags = RTN_ROOT;
716 sfn->fn_sernum = fib6_new_sernum();
718 /* Now add the first leaf node to new subtree */
720 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
721 sizeof(struct in6_addr), rt->rt6i_src.plen,
722 offsetof(struct rt6_info, rt6i_src));
724 if (sn == NULL) {
725 /* If it is failed, discard just allocated
726 root, and then (in st_failure) stale node
727 in main tree.
729 node_free(sfn);
730 goto st_failure;
733 /* Now link new subtree to main tree */
734 sfn->parent = fn;
735 fn->subtree = sfn;
736 if (fn->leaf == NULL) {
737 fn->leaf = rt;
738 atomic_inc(&rt->rt6i_ref);
740 } else {
741 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
742 sizeof(struct in6_addr), rt->rt6i_src.plen,
743 offsetof(struct rt6_info, rt6i_src));
745 if (sn == NULL)
746 goto st_failure;
749 fn = sn;
751 #endif
753 err = fib6_add_rt2node(fn, rt, nlh, req);
755 if (err == 0) {
756 fib6_start_gc(rt);
757 if (!(rt->rt6i_flags&RTF_CACHE))
758 fib6_prune_clones(fn, rt);
761 out:
762 if (err)
763 dst_free(&rt->u.dst);
764 return err;
766 #ifdef CONFIG_IPV6_SUBTREES
767 /* Subtree creation failed, probably main tree node
768 is orphan. If it is, shoot it.
770 st_failure:
771 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
772 fib6_repair_tree(fn);
773 dst_free(&rt->u.dst);
774 return err;
775 #endif
779 * Routing tree lookup
783 struct lookup_args {
784 int offset; /* key offset on rt6_info */
785 struct in6_addr *addr; /* search key */
788 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
789 struct lookup_args *args)
791 struct fib6_node *fn;
792 int dir;
795 * Descend on a tree
798 fn = root;
800 for (;;) {
801 struct fib6_node *next;
803 dir = addr_bit_set(args->addr, fn->fn_bit);
805 next = dir ? fn->right : fn->left;
807 if (next) {
808 fn = next;
809 continue;
812 break;
815 while ((fn->fn_flags & RTN_ROOT) == 0) {
816 #ifdef CONFIG_IPV6_SUBTREES
817 if (fn->subtree) {
818 struct fib6_node *st;
819 struct lookup_args *narg;
821 narg = args + 1;
823 if (narg->addr) {
824 st = fib6_lookup_1(fn->subtree, narg);
826 if (st && !(st->fn_flags & RTN_ROOT))
827 return st;
830 #endif
832 if (fn->fn_flags & RTN_RTINFO) {
833 struct rt6key *key;
835 key = (struct rt6key *) ((u8 *) fn->leaf +
836 args->offset);
838 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
839 return fn;
842 fn = fn->parent;
845 return NULL;
848 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
849 struct in6_addr *saddr)
851 struct lookup_args args[2];
852 struct fib6_node *fn;
854 args[0].offset = offsetof(struct rt6_info, rt6i_dst);
855 args[0].addr = daddr;
857 #ifdef CONFIG_IPV6_SUBTREES
858 args[1].offset = offsetof(struct rt6_info, rt6i_src);
859 args[1].addr = saddr;
860 #endif
862 fn = fib6_lookup_1(root, args);
864 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
865 fn = root;
867 return fn;
871 * Get node with specified destination prefix (and source prefix,
872 * if subtrees are used)
876 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
877 struct in6_addr *addr,
878 int plen, int offset)
880 struct fib6_node *fn;
882 for (fn = root; fn ; ) {
883 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
886 * Prefix match
888 if (plen < fn->fn_bit ||
889 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
890 return NULL;
892 if (plen == fn->fn_bit)
893 return fn;
896 * We have more bits to go
898 if (addr_bit_set(addr, fn->fn_bit))
899 fn = fn->right;
900 else
901 fn = fn->left;
903 return NULL;
906 struct fib6_node * fib6_locate(struct fib6_node *root,
907 struct in6_addr *daddr, int dst_len,
908 struct in6_addr *saddr, int src_len)
910 struct fib6_node *fn;
912 fn = fib6_locate_1(root, daddr, dst_len,
913 offsetof(struct rt6_info, rt6i_dst));
915 #ifdef CONFIG_IPV6_SUBTREES
916 if (src_len) {
917 BUG_TRAP(saddr!=NULL);
918 if (fn == NULL)
919 fn = fn->subtree;
920 if (fn)
921 fn = fib6_locate_1(fn, saddr, src_len,
922 offsetof(struct rt6_info, rt6i_src));
924 #endif
926 if (fn && fn->fn_flags&RTN_RTINFO)
927 return fn;
929 return NULL;
934 * Deletion
938 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
940 if (fn->fn_flags&RTN_ROOT)
941 return &ip6_null_entry;
943 while(fn) {
944 if(fn->left)
945 return fn->left->leaf;
947 if(fn->right)
948 return fn->right->leaf;
950 fn = SUBTREE(fn);
952 return NULL;
956 * Called to trim the tree of intermediate nodes when possible. "fn"
957 * is the node we want to try and remove.
960 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
962 int children;
963 int nstate;
964 struct fib6_node *child, *pn;
965 struct fib6_walker_t *w;
966 int iter = 0;
968 for (;;) {
969 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
970 iter++;
972 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
973 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
974 BUG_TRAP(fn->leaf==NULL);
976 children = 0;
977 child = NULL;
978 if (fn->right) child = fn->right, children |= 1;
979 if (fn->left) child = fn->left, children |= 2;
981 if (children == 3 || SUBTREE(fn)
982 #ifdef CONFIG_IPV6_SUBTREES
983 /* Subtree root (i.e. fn) may have one child */
984 || (children && fn->fn_flags&RTN_ROOT)
985 #endif
987 fn->leaf = fib6_find_prefix(fn);
988 #if RT6_DEBUG >= 2
989 if (fn->leaf==NULL) {
990 BUG_TRAP(fn->leaf);
991 fn->leaf = &ip6_null_entry;
993 #endif
994 atomic_inc(&fn->leaf->rt6i_ref);
995 return fn->parent;
998 pn = fn->parent;
999 #ifdef CONFIG_IPV6_SUBTREES
1000 if (SUBTREE(pn) == fn) {
1001 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1002 SUBTREE(pn) = NULL;
1003 nstate = FWS_L;
1004 } else {
1005 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
1006 #endif
1007 if (pn->right == fn) pn->right = child;
1008 else if (pn->left == fn) pn->left = child;
1009 #if RT6_DEBUG >= 2
1010 else BUG_TRAP(0);
1011 #endif
1012 if (child)
1013 child->parent = pn;
1014 nstate = FWS_R;
1015 #ifdef CONFIG_IPV6_SUBTREES
1017 #endif
1019 read_lock(&fib6_walker_lock);
1020 FOR_WALKERS(w) {
1021 if (child == NULL) {
1022 if (w->root == fn) {
1023 w->root = w->node = NULL;
1024 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1025 } else if (w->node == fn) {
1026 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1027 w->node = pn;
1028 w->state = nstate;
1030 } else {
1031 if (w->root == fn) {
1032 w->root = child;
1033 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1035 if (w->node == fn) {
1036 w->node = child;
1037 if (children&2) {
1038 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1039 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
1040 } else {
1041 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1042 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
1047 read_unlock(&fib6_walker_lock);
1049 node_free(fn);
1050 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
1051 return pn;
1053 rt6_release(pn->leaf);
1054 pn->leaf = NULL;
1055 fn = pn;
1059 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
1060 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
1062 struct fib6_walker_t *w;
1063 struct rt6_info *rt = *rtp;
1065 RT6_TRACE("fib6_del_route\n");
1067 /* Unlink it */
1068 *rtp = rt->u.next;
1069 rt->rt6i_node = NULL;
1070 rt6_stats.fib_rt_entries--;
1071 rt6_stats.fib_discarded_routes++;
1073 /* Adjust walkers */
1074 read_lock(&fib6_walker_lock);
1075 FOR_WALKERS(w) {
1076 if (w->state == FWS_C && w->leaf == rt) {
1077 RT6_TRACE("walker %p adjusted by delroute\n", w);
1078 w->leaf = rt->u.next;
1079 if (w->leaf == NULL)
1080 w->state = FWS_U;
1083 read_unlock(&fib6_walker_lock);
1085 rt->u.next = NULL;
1087 if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
1088 fn->leaf = &ip6_null_entry;
1090 /* If it was last route, expunge its radix tree node */
1091 if (fn->leaf == NULL) {
1092 fn->fn_flags &= ~RTN_RTINFO;
1093 rt6_stats.fib_route_nodes--;
1094 fn = fib6_repair_tree(fn);
1097 if (atomic_read(&rt->rt6i_ref) != 1) {
1098 /* This route is used as dummy address holder in some split
1099 * nodes. It is not leaked, but it still holds other resources,
1100 * which must be released in time. So, scan ascendant nodes
1101 * and replace dummy references to this route with references
1102 * to still alive ones.
1104 while (fn) {
1105 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
1106 fn->leaf = fib6_find_prefix(fn);
1107 atomic_inc(&fn->leaf->rt6i_ref);
1108 rt6_release(rt);
1110 fn = fn->parent;
1112 /* No more references are possible at this point. */
1113 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
1116 inet6_rt_notify(RTM_DELROUTE, rt, nlh, req);
1117 rt6_release(rt);
1120 int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
1122 struct fib6_node *fn = rt->rt6i_node;
1123 struct rt6_info **rtp;
1125 #if RT6_DEBUG >= 2
1126 if (rt->u.dst.obsolete>0) {
1127 BUG_TRAP(fn==NULL);
1128 return -ENOENT;
1130 #endif
1131 if (fn == NULL || rt == &ip6_null_entry)
1132 return -ENOENT;
1134 BUG_TRAP(fn->fn_flags&RTN_RTINFO);
1136 if (!(rt->rt6i_flags&RTF_CACHE))
1137 fib6_prune_clones(fn, rt);
1140 * Walk the leaf entries looking for ourself
1143 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
1144 if (*rtp == rt) {
1145 fib6_del_route(fn, rtp, nlh, _rtattr, req);
1146 return 0;
1149 return -ENOENT;
1153 * Tree traversal function.
1155 * Certainly, it is not interrupt safe.
1156 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1157 * It means, that we can modify tree during walking
1158 * and use this function for garbage collection, clone pruning,
1159 * cleaning tree when a device goes down etc. etc.
1161 * It guarantees that every node will be traversed,
1162 * and that it will be traversed only once.
1164 * Callback function w->func may return:
1165 * 0 -> continue walking.
1166 * positive value -> walking is suspended (used by tree dumps,
1167 * and probably by gc, if it will be split to several slices)
1168 * negative value -> terminate walking.
1170 * The function itself returns:
1171 * 0 -> walk is complete.
1172 * >0 -> walk is incomplete (i.e. suspended)
1173 * <0 -> walk is terminated by an error.
1176 int fib6_walk_continue(struct fib6_walker_t *w)
1178 struct fib6_node *fn, *pn;
1180 for (;;) {
1181 fn = w->node;
1182 if (fn == NULL)
1183 return 0;
1185 if (w->prune && fn != w->root &&
1186 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
1187 w->state = FWS_C;
1188 w->leaf = fn->leaf;
1190 switch (w->state) {
1191 #ifdef CONFIG_IPV6_SUBTREES
1192 case FWS_S:
1193 if (SUBTREE(fn)) {
1194 w->node = SUBTREE(fn);
1195 continue;
1197 w->state = FWS_L;
1198 #endif
1199 case FWS_L:
1200 if (fn->left) {
1201 w->node = fn->left;
1202 w->state = FWS_INIT;
1203 continue;
1205 w->state = FWS_R;
1206 case FWS_R:
1207 if (fn->right) {
1208 w->node = fn->right;
1209 w->state = FWS_INIT;
1210 continue;
1212 w->state = FWS_C;
1213 w->leaf = fn->leaf;
1214 case FWS_C:
1215 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1216 int err = w->func(w);
1217 if (err)
1218 return err;
1219 continue;
1221 w->state = FWS_U;
1222 case FWS_U:
1223 if (fn == w->root)
1224 return 0;
1225 pn = fn->parent;
1226 w->node = pn;
1227 #ifdef CONFIG_IPV6_SUBTREES
1228 if (SUBTREE(pn) == fn) {
1229 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1230 w->state = FWS_L;
1231 continue;
1233 #endif
1234 if (pn->left == fn) {
1235 w->state = FWS_R;
1236 continue;
1238 if (pn->right == fn) {
1239 w->state = FWS_C;
1240 w->leaf = w->node->leaf;
1241 continue;
1243 #if RT6_DEBUG >= 2
1244 BUG_TRAP(0);
1245 #endif
1250 int fib6_walk(struct fib6_walker_t *w)
1252 int res;
1254 w->state = FWS_INIT;
1255 w->node = w->root;
1257 fib6_walker_link(w);
1258 res = fib6_walk_continue(w);
1259 if (res <= 0)
1260 fib6_walker_unlink(w);
1261 return res;
1264 static int fib6_clean_node(struct fib6_walker_t *w)
1266 int res;
1267 struct rt6_info *rt;
1268 struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1270 for (rt = w->leaf; rt; rt = rt->u.next) {
1271 res = c->func(rt, c->arg);
1272 if (res < 0) {
1273 w->leaf = rt;
1274 res = fib6_del(rt, NULL, NULL, NULL);
1275 if (res) {
1276 #if RT6_DEBUG >= 2
1277 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1278 #endif
1279 continue;
1281 return 0;
1283 BUG_TRAP(res==0);
1285 w->leaf = rt;
1286 return 0;
1290 * Convenient frontend to tree walker.
1292 * func is called on each route.
1293 * It may return -1 -> delete this route.
1294 * 0 -> continue walking
1296 * prune==1 -> only immediate children of node (certainly,
1297 * ignoring pure split nodes) will be scanned.
1300 static void fib6_clean_tree(struct fib6_node *root,
1301 int (*func)(struct rt6_info *, void *arg),
1302 int prune, void *arg)
1304 struct fib6_cleaner_t c;
1306 c.w.root = root;
1307 c.w.func = fib6_clean_node;
1308 c.w.prune = prune;
1309 c.func = func;
1310 c.arg = arg;
1312 fib6_walk(&c.w);
1315 void fib6_clean_all(int (*func)(struct rt6_info *, void *arg),
1316 int prune, void *arg)
1318 struct fib6_table *table;
1319 struct hlist_node *node;
1320 unsigned int h;
1322 rcu_read_lock();
1323 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
1324 hlist_for_each_entry_rcu(table, node, &fib_table_hash[h],
1325 tb6_hlist) {
1326 write_lock_bh(&table->tb6_lock);
1327 fib6_clean_tree(&table->tb6_root, func, prune, arg);
1328 write_unlock_bh(&table->tb6_lock);
1331 rcu_read_unlock();
1334 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1336 if (rt->rt6i_flags & RTF_CACHE) {
1337 RT6_TRACE("pruning clone %p\n", rt);
1338 return -1;
1341 return 0;
1344 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1346 fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1350 * Garbage collection
1353 static struct fib6_gc_args
1355 int timeout;
1356 int more;
1357 } gc_args;
1359 static int fib6_age(struct rt6_info *rt, void *arg)
1361 unsigned long now = jiffies;
1364 * check addrconf expiration here.
1365 * Routes are expired even if they are in use.
1367 * Also age clones. Note, that clones are aged out
1368 * only if they are not in use now.
1371 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1372 if (time_after(now, rt->rt6i_expires)) {
1373 RT6_TRACE("expiring %p\n", rt);
1374 return -1;
1376 gc_args.more++;
1377 } else if (rt->rt6i_flags & RTF_CACHE) {
1378 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1379 time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1380 RT6_TRACE("aging clone %p\n", rt);
1381 return -1;
1382 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1383 (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1384 RT6_TRACE("purging route %p via non-router but gateway\n",
1385 rt);
1386 return -1;
1388 gc_args.more++;
1391 return 0;
1394 static DEFINE_SPINLOCK(fib6_gc_lock);
1396 void fib6_run_gc(unsigned long dummy)
1398 if (dummy != ~0UL) {
1399 spin_lock_bh(&fib6_gc_lock);
1400 gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
1401 } else {
1402 local_bh_disable();
1403 if (!spin_trylock(&fib6_gc_lock)) {
1404 mod_timer(&ip6_fib_timer, jiffies + HZ);
1405 local_bh_enable();
1406 return;
1408 gc_args.timeout = ip6_rt_gc_interval;
1410 gc_args.more = 0;
1412 ndisc_dst_gc(&gc_args.more);
1413 fib6_clean_all(fib6_age, 0, NULL);
1415 if (gc_args.more)
1416 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1417 else {
1418 del_timer(&ip6_fib_timer);
1419 ip6_fib_timer.expires = 0;
1421 spin_unlock_bh(&fib6_gc_lock);
1424 void __init fib6_init(void)
1426 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1427 sizeof(struct fib6_node),
1428 0, SLAB_HWCACHE_ALIGN,
1429 NULL, NULL);
1430 if (!fib6_node_kmem)
1431 panic("cannot create fib6_nodes cache");
1433 fib6_tables_init();
1436 void fib6_gc_cleanup(void)
1438 del_timer(&ip6_fib_timer);
1439 kmem_cache_destroy(fib6_node_kmem);