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>
29 #include <linux/list.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 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)
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];
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
));
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
,
155 .leaf
= &ip6_null_entry
,
156 .fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
,
160 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
162 static struct fib6_table fib6_local_tbl
= {
163 .tb6_id
= RT6_TABLE_LOCAL
,
164 .tb6_lock
= RW_LOCK_UNLOCKED
,
166 .leaf
= &ip6_null_entry
,
167 .fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
,
171 #define FIB_TABLE_HASHSZ 256
172 static struct hlist_head fib_table_hash
[FIB_TABLE_HASHSZ
];
174 static struct fib6_table
*fib6_alloc_table(u32 id
)
176 struct fib6_table
*table
;
178 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
181 table
->tb6_lock
= RW_LOCK_UNLOCKED
;
182 table
->tb6_root
.leaf
= &ip6_null_entry
;
183 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
189 static void fib6_link_table(struct fib6_table
*tb
)
193 h
= tb
->tb6_id
& (FIB_TABLE_HASHSZ
- 1);
196 * No protection necessary, this is the only list mutatation
197 * operation, tables never disappear once they exist.
199 hlist_add_head_rcu(&tb
->tb6_hlist
, &fib_table_hash
[h
]);
202 struct fib6_table
*fib6_new_table(u32 id
)
204 struct fib6_table
*tb
;
208 tb
= fib6_get_table(id
);
212 tb
= fib6_alloc_table(id
);
219 struct fib6_table
*fib6_get_table(u32 id
)
221 struct fib6_table
*tb
;
222 struct hlist_node
*node
;
227 h
= id
& (FIB_TABLE_HASHSZ
- 1);
229 hlist_for_each_entry_rcu(tb
, node
, &fib_table_hash
[h
], tb6_hlist
) {
230 if (tb
->tb6_id
== id
) {
240 static void __init
fib6_tables_init(void)
242 fib6_link_table(&fib6_main_tbl
);
243 fib6_link_table(&fib6_local_tbl
);
248 struct fib6_table
*fib6_new_table(u32 id
)
250 return fib6_get_table(id
);
253 struct fib6_table
*fib6_get_table(u32 id
)
255 return &fib6_main_tbl
;
258 struct dst_entry
*fib6_rule_lookup(struct flowi
*fl
, int flags
,
261 return (struct dst_entry
*) lookup(&fib6_main_tbl
, fl
, flags
);
264 static void __init
fib6_tables_init(void)
274 * return the appropriate node for a routing tree "add" operation
275 * by either creating and inserting or by returning an existing
279 static struct fib6_node
* fib6_add_1(struct fib6_node
*root
, void *addr
,
280 int addrlen
, int plen
,
283 struct fib6_node
*fn
, *in
, *ln
;
284 struct fib6_node
*pn
= NULL
;
288 __u32 sernum
= fib6_new_sernum();
290 RT6_TRACE("fib6_add_1\n");
292 /* insert node in tree */
297 key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
302 if (plen
< fn
->fn_bit
||
303 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
310 if (plen
== fn
->fn_bit
) {
311 /* clean up an intermediate node */
312 if ((fn
->fn_flags
& RTN_RTINFO
) == 0) {
313 rt6_release(fn
->leaf
);
317 fn
->fn_sernum
= sernum
;
323 * We have more bits to go
326 /* Try to walk down on tree. */
327 fn
->fn_sernum
= sernum
;
328 dir
= addr_bit_set(addr
, fn
->fn_bit
);
330 fn
= dir
? fn
->right
: fn
->left
;
334 * We walked to the bottom of tree.
335 * Create new leaf node without children.
345 ln
->fn_sernum
= sernum
;
357 * split since we don't have a common prefix anymore or
358 * we have a less significant route.
359 * we've to insert an intermediate node on the list
360 * this new node will point to the one we need to create
366 /* find 1st bit in difference between the 2 addrs.
368 See comment in __ipv6_addr_diff: bit may be an invalid value,
369 but if it is >= plen, the value is ignored in any case.
372 bit
= __ipv6_addr_diff(addr
, &key
->addr
, addrlen
);
377 * (new leaf node)[ln] (old node)[fn]
383 if (in
== NULL
|| ln
== NULL
) {
392 * new intermediate node.
394 * be off since that an address that chooses one of
395 * the branches would not match less specific routes
396 * in the other branch
403 atomic_inc(&in
->leaf
->rt6i_ref
);
405 in
->fn_sernum
= sernum
;
407 /* update parent pointer */
418 ln
->fn_sernum
= sernum
;
420 if (addr_bit_set(addr
, bit
)) {
427 } else { /* plen <= bit */
430 * (new leaf node)[ln]
432 * (old node)[fn] NULL
444 ln
->fn_sernum
= sernum
;
451 if (addr_bit_set(&key
->addr
, plen
))
462 * Insert routing information in a node.
465 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
466 struct nlmsghdr
*nlh
, struct netlink_skb_parms
*req
)
468 struct rt6_info
*iter
= NULL
;
469 struct rt6_info
**ins
;
473 if (fn
->fn_flags
&RTN_TL_ROOT
&&
474 fn
->leaf
== &ip6_null_entry
&&
475 !(rt
->rt6i_flags
& (RTF_DEFAULT
| RTF_ADDRCONF
)) ){
481 for (iter
= fn
->leaf
; iter
; iter
=iter
->u
.next
) {
483 * Search for duplicates
486 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
488 * Same priority level
491 if (iter
->rt6i_dev
== rt
->rt6i_dev
&&
492 iter
->rt6i_idev
== rt
->rt6i_idev
&&
493 ipv6_addr_equal(&iter
->rt6i_gateway
,
494 &rt
->rt6i_gateway
)) {
495 if (!(iter
->rt6i_flags
&RTF_EXPIRES
))
497 iter
->rt6i_expires
= rt
->rt6i_expires
;
498 if (!(rt
->rt6i_flags
&RTF_EXPIRES
)) {
499 iter
->rt6i_flags
&= ~RTF_EXPIRES
;
500 iter
->rt6i_expires
= 0;
506 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
520 atomic_inc(&rt
->rt6i_ref
);
521 inet6_rt_notify(RTM_NEWROUTE
, rt
, nlh
, req
);
522 rt6_stats
.fib_rt_entries
++;
524 if ((fn
->fn_flags
& RTN_RTINFO
) == 0) {
525 rt6_stats
.fib_route_nodes
++;
526 fn
->fn_flags
|= RTN_RTINFO
;
532 static __inline__
void fib6_start_gc(struct rt6_info
*rt
)
534 if (ip6_fib_timer
.expires
== 0 &&
535 (rt
->rt6i_flags
& (RTF_EXPIRES
|RTF_CACHE
)))
536 mod_timer(&ip6_fib_timer
, jiffies
+ ip6_rt_gc_interval
);
539 void fib6_force_start_gc(void)
541 if (ip6_fib_timer
.expires
== 0)
542 mod_timer(&ip6_fib_timer
, jiffies
+ ip6_rt_gc_interval
);
546 * Add routing information to the routing tree.
547 * <destination addr>/<source addr>
548 * with source addr info in sub-trees
551 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
552 struct nlmsghdr
*nlh
, void *_rtattr
, struct netlink_skb_parms
*req
)
554 struct fib6_node
*fn
;
557 fn
= fib6_add_1(root
, &rt
->rt6i_dst
.addr
, sizeof(struct in6_addr
),
558 rt
->rt6i_dst
.plen
, offsetof(struct rt6_info
, rt6i_dst
));
563 #ifdef CONFIG_IPV6_SUBTREES
564 if (rt
->rt6i_src
.plen
) {
565 struct fib6_node
*sn
;
567 if (fn
->subtree
== NULL
) {
568 struct fib6_node
*sfn
;
580 /* Create subtree root node */
585 sfn
->leaf
= &ip6_null_entry
;
586 atomic_inc(&ip6_null_entry
.rt6i_ref
);
587 sfn
->fn_flags
= RTN_ROOT
;
588 sfn
->fn_sernum
= fib6_new_sernum();
590 /* Now add the first leaf node to new subtree */
592 sn
= fib6_add_1(sfn
, &rt
->rt6i_src
.addr
,
593 sizeof(struct in6_addr
), rt
->rt6i_src
.plen
,
594 offsetof(struct rt6_info
, rt6i_src
));
597 /* If it is failed, discard just allocated
598 root, and then (in st_failure) stale node
605 /* Now link new subtree to main tree */
608 if (fn
->leaf
== NULL
) {
610 atomic_inc(&rt
->rt6i_ref
);
613 sn
= fib6_add_1(fn
->subtree
, &rt
->rt6i_src
.addr
,
614 sizeof(struct in6_addr
), rt
->rt6i_src
.plen
,
615 offsetof(struct rt6_info
, rt6i_src
));
625 err
= fib6_add_rt2node(fn
, rt
, nlh
, req
);
629 if (!(rt
->rt6i_flags
&RTF_CACHE
))
630 fib6_prune_clones(fn
, rt
);
635 dst_free(&rt
->u
.dst
);
638 #ifdef CONFIG_IPV6_SUBTREES
639 /* Subtree creation failed, probably main tree node
640 is orphan. If it is, shoot it.
643 if (fn
&& !(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)))
644 fib6_repair_tree(fn
);
645 dst_free(&rt
->u
.dst
);
651 * Routing tree lookup
656 int offset
; /* key offset on rt6_info */
657 struct in6_addr
*addr
; /* search key */
660 static struct fib6_node
* fib6_lookup_1(struct fib6_node
*root
,
661 struct lookup_args
*args
)
663 struct fib6_node
*fn
;
673 struct fib6_node
*next
;
675 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
677 next
= dir
? fn
->right
: fn
->left
;
687 while ((fn
->fn_flags
& RTN_ROOT
) == 0) {
688 #ifdef CONFIG_IPV6_SUBTREES
690 struct fib6_node
*st
;
691 struct lookup_args
*narg
;
696 st
= fib6_lookup_1(fn
->subtree
, narg
);
698 if (st
&& !(st
->fn_flags
& RTN_ROOT
))
704 if (fn
->fn_flags
& RTN_RTINFO
) {
707 key
= (struct rt6key
*) ((u8
*) fn
->leaf
+
710 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
))
720 struct fib6_node
* fib6_lookup(struct fib6_node
*root
, struct in6_addr
*daddr
,
721 struct in6_addr
*saddr
)
723 struct lookup_args args
[2];
724 struct fib6_node
*fn
;
726 args
[0].offset
= offsetof(struct rt6_info
, rt6i_dst
);
727 args
[0].addr
= daddr
;
729 #ifdef CONFIG_IPV6_SUBTREES
730 args
[1].offset
= offsetof(struct rt6_info
, rt6i_src
);
731 args
[1].addr
= saddr
;
734 fn
= fib6_lookup_1(root
, args
);
736 if (fn
== NULL
|| fn
->fn_flags
& RTN_TL_ROOT
)
743 * Get node with specified destination prefix (and source prefix,
744 * if subtrees are used)
748 static struct fib6_node
* fib6_locate_1(struct fib6_node
*root
,
749 struct in6_addr
*addr
,
750 int plen
, int offset
)
752 struct fib6_node
*fn
;
754 for (fn
= root
; fn
; ) {
755 struct rt6key
*key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
760 if (plen
< fn
->fn_bit
||
761 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
764 if (plen
== fn
->fn_bit
)
768 * We have more bits to go
770 if (addr_bit_set(addr
, fn
->fn_bit
))
778 struct fib6_node
* fib6_locate(struct fib6_node
*root
,
779 struct in6_addr
*daddr
, int dst_len
,
780 struct in6_addr
*saddr
, int src_len
)
782 struct fib6_node
*fn
;
784 fn
= fib6_locate_1(root
, daddr
, dst_len
,
785 offsetof(struct rt6_info
, rt6i_dst
));
787 #ifdef CONFIG_IPV6_SUBTREES
789 BUG_TRAP(saddr
!=NULL
);
793 fn
= fib6_locate_1(fn
, saddr
, src_len
,
794 offsetof(struct rt6_info
, rt6i_src
));
798 if (fn
&& fn
->fn_flags
&RTN_RTINFO
)
810 static struct rt6_info
* fib6_find_prefix(struct fib6_node
*fn
)
812 if (fn
->fn_flags
&RTN_ROOT
)
813 return &ip6_null_entry
;
817 return fn
->left
->leaf
;
820 return fn
->right
->leaf
;
828 * Called to trim the tree of intermediate nodes when possible. "fn"
829 * is the node we want to try and remove.
832 static struct fib6_node
* fib6_repair_tree(struct fib6_node
*fn
)
836 struct fib6_node
*child
, *pn
;
837 struct fib6_walker_t
*w
;
841 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
844 BUG_TRAP(!(fn
->fn_flags
&RTN_RTINFO
));
845 BUG_TRAP(!(fn
->fn_flags
&RTN_TL_ROOT
));
846 BUG_TRAP(fn
->leaf
==NULL
);
850 if (fn
->right
) child
= fn
->right
, children
|= 1;
851 if (fn
->left
) child
= fn
->left
, children
|= 2;
853 if (children
== 3 || SUBTREE(fn
)
854 #ifdef CONFIG_IPV6_SUBTREES
855 /* Subtree root (i.e. fn) may have one child */
856 || (children
&& fn
->fn_flags
&RTN_ROOT
)
859 fn
->leaf
= fib6_find_prefix(fn
);
861 if (fn
->leaf
==NULL
) {
863 fn
->leaf
= &ip6_null_entry
;
866 atomic_inc(&fn
->leaf
->rt6i_ref
);
871 #ifdef CONFIG_IPV6_SUBTREES
872 if (SUBTREE(pn
) == fn
) {
873 BUG_TRAP(fn
->fn_flags
&RTN_ROOT
);
877 BUG_TRAP(!(fn
->fn_flags
&RTN_ROOT
));
879 if (pn
->right
== fn
) pn
->right
= child
;
880 else if (pn
->left
== fn
) pn
->left
= child
;
887 #ifdef CONFIG_IPV6_SUBTREES
891 read_lock(&fib6_walker_lock
);
895 w
->root
= w
->node
= NULL
;
896 RT6_TRACE("W %p adjusted by delroot 1\n", w
);
897 } else if (w
->node
== fn
) {
898 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
905 RT6_TRACE("W %p adjusted by delroot 2\n", w
);
910 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
911 w
->state
= w
->state
>=FWS_R
? FWS_U
: FWS_INIT
;
913 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
914 w
->state
= w
->state
>=FWS_C
? FWS_U
: FWS_INIT
;
919 read_unlock(&fib6_walker_lock
);
922 if (pn
->fn_flags
&RTN_RTINFO
|| SUBTREE(pn
))
925 rt6_release(pn
->leaf
);
931 static void fib6_del_route(struct fib6_node
*fn
, struct rt6_info
**rtp
,
932 struct nlmsghdr
*nlh
, void *_rtattr
, struct netlink_skb_parms
*req
)
934 struct fib6_walker_t
*w
;
935 struct rt6_info
*rt
= *rtp
;
937 RT6_TRACE("fib6_del_route\n");
941 rt
->rt6i_node
= NULL
;
942 rt6_stats
.fib_rt_entries
--;
943 rt6_stats
.fib_discarded_routes
++;
946 read_lock(&fib6_walker_lock
);
948 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
949 RT6_TRACE("walker %p adjusted by delroute\n", w
);
950 w
->leaf
= rt
->u
.next
;
955 read_unlock(&fib6_walker_lock
);
959 if (fn
->leaf
== NULL
&& fn
->fn_flags
&RTN_TL_ROOT
)
960 fn
->leaf
= &ip6_null_entry
;
962 /* If it was last route, expunge its radix tree node */
963 if (fn
->leaf
== NULL
) {
964 fn
->fn_flags
&= ~RTN_RTINFO
;
965 rt6_stats
.fib_route_nodes
--;
966 fn
= fib6_repair_tree(fn
);
969 if (atomic_read(&rt
->rt6i_ref
) != 1) {
970 /* This route is used as dummy address holder in some split
971 * nodes. It is not leaked, but it still holds other resources,
972 * which must be released in time. So, scan ascendant nodes
973 * and replace dummy references to this route with references
974 * to still alive ones.
977 if (!(fn
->fn_flags
&RTN_RTINFO
) && fn
->leaf
== rt
) {
978 fn
->leaf
= fib6_find_prefix(fn
);
979 atomic_inc(&fn
->leaf
->rt6i_ref
);
984 /* No more references are possible at this point. */
985 if (atomic_read(&rt
->rt6i_ref
) != 1) BUG();
988 inet6_rt_notify(RTM_DELROUTE
, rt
, nlh
, req
);
992 int fib6_del(struct rt6_info
*rt
, struct nlmsghdr
*nlh
, void *_rtattr
, struct netlink_skb_parms
*req
)
994 struct fib6_node
*fn
= rt
->rt6i_node
;
995 struct rt6_info
**rtp
;
998 if (rt
->u
.dst
.obsolete
>0) {
1003 if (fn
== NULL
|| rt
== &ip6_null_entry
)
1006 BUG_TRAP(fn
->fn_flags
&RTN_RTINFO
);
1008 if (!(rt
->rt6i_flags
&RTF_CACHE
))
1009 fib6_prune_clones(fn
, rt
);
1012 * Walk the leaf entries looking for ourself
1015 for (rtp
= &fn
->leaf
; *rtp
; rtp
= &(*rtp
)->u
.next
) {
1017 fib6_del_route(fn
, rtp
, nlh
, _rtattr
, req
);
1025 * Tree traversal function.
1027 * Certainly, it is not interrupt safe.
1028 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1029 * It means, that we can modify tree during walking
1030 * and use this function for garbage collection, clone pruning,
1031 * cleaning tree when a device goes down etc. etc.
1033 * It guarantees that every node will be traversed,
1034 * and that it will be traversed only once.
1036 * Callback function w->func may return:
1037 * 0 -> continue walking.
1038 * positive value -> walking is suspended (used by tree dumps,
1039 * and probably by gc, if it will be split to several slices)
1040 * negative value -> terminate walking.
1042 * The function itself returns:
1043 * 0 -> walk is complete.
1044 * >0 -> walk is incomplete (i.e. suspended)
1045 * <0 -> walk is terminated by an error.
1048 int fib6_walk_continue(struct fib6_walker_t
*w
)
1050 struct fib6_node
*fn
, *pn
;
1057 if (w
->prune
&& fn
!= w
->root
&&
1058 fn
->fn_flags
&RTN_RTINFO
&& w
->state
< FWS_C
) {
1063 #ifdef CONFIG_IPV6_SUBTREES
1066 w
->node
= SUBTREE(fn
);
1074 w
->state
= FWS_INIT
;
1080 w
->node
= fn
->right
;
1081 w
->state
= FWS_INIT
;
1087 if (w
->leaf
&& fn
->fn_flags
&RTN_RTINFO
) {
1088 int err
= w
->func(w
);
1099 #ifdef CONFIG_IPV6_SUBTREES
1100 if (SUBTREE(pn
) == fn
) {
1101 BUG_TRAP(fn
->fn_flags
&RTN_ROOT
);
1106 if (pn
->left
== fn
) {
1110 if (pn
->right
== fn
) {
1112 w
->leaf
= w
->node
->leaf
;
1122 int fib6_walk(struct fib6_walker_t
*w
)
1126 w
->state
= FWS_INIT
;
1129 fib6_walker_link(w
);
1130 res
= fib6_walk_continue(w
);
1132 fib6_walker_unlink(w
);
1136 static int fib6_clean_node(struct fib6_walker_t
*w
)
1139 struct rt6_info
*rt
;
1140 struct fib6_cleaner_t
*c
= (struct fib6_cleaner_t
*)w
;
1142 for (rt
= w
->leaf
; rt
; rt
= rt
->u
.next
) {
1143 res
= c
->func(rt
, c
->arg
);
1146 res
= fib6_del(rt
, NULL
, NULL
, NULL
);
1149 printk(KERN_DEBUG
"fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt
, rt
->rt6i_node
, res
);
1162 * Convenient frontend to tree walker.
1164 * func is called on each route.
1165 * It may return -1 -> delete this route.
1166 * 0 -> continue walking
1168 * prune==1 -> only immediate children of node (certainly,
1169 * ignoring pure split nodes) will be scanned.
1172 void fib6_clean_tree(struct fib6_node
*root
,
1173 int (*func
)(struct rt6_info
*, void *arg
),
1174 int prune
, void *arg
)
1176 struct fib6_cleaner_t c
;
1179 c
.w
.func
= fib6_clean_node
;
1187 void fib6_clean_all(int (*func
)(struct rt6_info
*, void *arg
),
1188 int prune
, void *arg
)
1191 struct fib6_table
*table
;
1193 for (i
= FIB6_TABLE_MIN
; i
<= FIB6_TABLE_MAX
; i
++) {
1194 table
= fib6_get_table(i
);
1195 if (table
!= NULL
) {
1196 write_lock_bh(&table
->tb6_lock
);
1197 fib6_clean_tree(&table
->tb6_root
, func
, prune
, arg
);
1198 write_unlock_bh(&table
->tb6_lock
);
1203 static int fib6_prune_clone(struct rt6_info
*rt
, void *arg
)
1205 if (rt
->rt6i_flags
& RTF_CACHE
) {
1206 RT6_TRACE("pruning clone %p\n", rt
);
1213 static void fib6_prune_clones(struct fib6_node
*fn
, struct rt6_info
*rt
)
1215 fib6_clean_tree(fn
, fib6_prune_clone
, 1, rt
);
1219 * Garbage collection
1222 static struct fib6_gc_args
1228 static int fib6_age(struct rt6_info
*rt
, void *arg
)
1230 unsigned long now
= jiffies
;
1233 * check addrconf expiration here.
1234 * Routes are expired even if they are in use.
1236 * Also age clones. Note, that clones are aged out
1237 * only if they are not in use now.
1240 if (rt
->rt6i_flags
&RTF_EXPIRES
&& rt
->rt6i_expires
) {
1241 if (time_after(now
, rt
->rt6i_expires
)) {
1242 RT6_TRACE("expiring %p\n", rt
);
1246 } else if (rt
->rt6i_flags
& RTF_CACHE
) {
1247 if (atomic_read(&rt
->u
.dst
.__refcnt
) == 0 &&
1248 time_after_eq(now
, rt
->u
.dst
.lastuse
+ gc_args
.timeout
)) {
1249 RT6_TRACE("aging clone %p\n", rt
);
1251 } else if ((rt
->rt6i_flags
& RTF_GATEWAY
) &&
1252 (!(rt
->rt6i_nexthop
->flags
& NTF_ROUTER
))) {
1253 RT6_TRACE("purging route %p via non-router but gateway\n",
1263 static DEFINE_SPINLOCK(fib6_gc_lock
);
1265 void fib6_run_gc(unsigned long dummy
)
1267 if (dummy
!= ~0UL) {
1268 spin_lock_bh(&fib6_gc_lock
);
1269 gc_args
.timeout
= dummy
? (int)dummy
: ip6_rt_gc_interval
;
1272 if (!spin_trylock(&fib6_gc_lock
)) {
1273 mod_timer(&ip6_fib_timer
, jiffies
+ HZ
);
1277 gc_args
.timeout
= ip6_rt_gc_interval
;
1281 ndisc_dst_gc(&gc_args
.more
);
1282 fib6_clean_all(fib6_age
, 0, NULL
);
1285 mod_timer(&ip6_fib_timer
, jiffies
+ ip6_rt_gc_interval
);
1287 del_timer(&ip6_fib_timer
);
1288 ip6_fib_timer
.expires
= 0;
1290 spin_unlock_bh(&fib6_gc_lock
);
1293 void __init
fib6_init(void)
1295 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
1296 sizeof(struct fib6_node
),
1297 0, SLAB_HWCACHE_ALIGN
,
1299 if (!fib6_node_kmem
)
1300 panic("cannot create fib6_nodes cache");
1305 void fib6_gc_cleanup(void)
1307 del_timer(&ip6_fib_timer
);
1308 kmem_cache_destroy(fib6_node_kmem
);