2 * Linux INET6 implementation
3 * Forwarding Information Database
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
16 * Yuji SEKIYA @USAGI: Support default route on router node;
17 * remove ip6_null_entry from the top of
19 * Ville Nuorvala: Fixed routing subtrees.
22 #define pr_fmt(fmt) "IPv6: " fmt
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/net.h>
27 #include <linux/route.h>
28 #include <linux/netdevice.h>
29 #include <linux/in6.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/slab.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...) pr_debug(x)
46 #define RT6_TRACE(x...) do { ; } while (0)
49 static struct kmem_cache
* fib6_node_kmem __read_mostly
;
53 #ifdef CONFIG_IPV6_SUBTREES
64 struct fib6_walker_t w
;
66 int (*func
)(struct rt6_info
*, void *arg
);
70 static DEFINE_RWLOCK(fib6_walker_lock
);
72 #ifdef CONFIG_IPV6_SUBTREES
73 #define FWS_INIT FWS_S
75 #define FWS_INIT FWS_L
78 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
,
80 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
);
81 static struct fib6_node
*fib6_repair_tree(struct net
*net
, struct fib6_node
*fn
);
82 static int fib6_walk(struct fib6_walker_t
*w
);
83 static int fib6_walk_continue(struct fib6_walker_t
*w
);
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 void fib6_gc_timer_cb(unsigned long arg
);
96 static LIST_HEAD(fib6_walkers
);
97 #define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
99 static inline void fib6_walker_link(struct fib6_walker_t
*w
)
101 write_lock_bh(&fib6_walker_lock
);
102 list_add(&w
->lh
, &fib6_walkers
);
103 write_unlock_bh(&fib6_walker_lock
);
106 static inline void fib6_walker_unlink(struct fib6_walker_t
*w
)
108 write_lock_bh(&fib6_walker_lock
);
110 write_unlock_bh(&fib6_walker_lock
);
112 static __inline__ u32
fib6_new_sernum(void)
121 * Auxiliary address test functions for the radix tree.
123 * These assume a 32bit processor (although it will work on
130 #if defined(__LITTLE_ENDIAN)
131 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
133 # define BITOP_BE32_SWIZZLE 0
136 static __inline__ __be32
addr_bit_set(const void *token
, int fn_bit
)
138 const __be32
*addr
= token
;
141 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
142 * is optimized version of
143 * htonl(1 << ((~fn_bit)&0x1F))
144 * See include/asm-generic/bitops/le.h.
146 return (__force __be32
)(1 << ((~fn_bit
^ BITOP_BE32_SWIZZLE
) & 0x1f)) &
150 static __inline__
struct fib6_node
* node_alloc(void)
152 struct fib6_node
*fn
;
154 fn
= kmem_cache_zalloc(fib6_node_kmem
, GFP_ATOMIC
);
159 static __inline__
void node_free(struct fib6_node
* fn
)
161 kmem_cache_free(fib6_node_kmem
, fn
);
164 static __inline__
void rt6_release(struct rt6_info
*rt
)
166 if (atomic_dec_and_test(&rt
->rt6i_ref
))
170 static void fib6_free_table(struct fib6_table
*table
)
172 inetpeer_invalidate_tree(&table
->tb6_peers
);
176 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
181 * Initialize table lock at a single place to give lockdep a key,
182 * tables aren't visible prior to being linked to the list.
184 rwlock_init(&tb
->tb6_lock
);
186 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
189 * No protection necessary, this is the only list mutatation
190 * operation, tables never disappear once they exist.
192 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
195 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
197 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
199 struct fib6_table
*table
;
201 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
204 table
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
205 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
206 inet_peer_base_init(&table
->tb6_peers
);
212 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
214 struct fib6_table
*tb
;
218 tb
= fib6_get_table(net
, id
);
222 tb
= fib6_alloc_table(net
, id
);
224 fib6_link_table(net
, tb
);
229 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
231 struct fib6_table
*tb
;
232 struct hlist_head
*head
;
237 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
239 head
= &net
->ipv6
.fib_table_hash
[h
];
240 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
241 if (tb
->tb6_id
== id
) {
251 static void __net_init
fib6_tables_init(struct net
*net
)
253 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
254 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
258 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
260 return fib6_get_table(net
, id
);
263 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
265 return net
->ipv6
.fib6_main_tbl
;
268 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
269 int flags
, pol_lookup_t lookup
)
271 return (struct dst_entry
*) lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
274 static void __net_init
fib6_tables_init(struct net
*net
)
276 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
281 static int fib6_dump_node(struct fib6_walker_t
*w
)
286 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
287 res
= rt6_dump_route(rt
, w
->args
);
289 /* Frame is full, suspend walking */
299 static void fib6_dump_end(struct netlink_callback
*cb
)
301 struct fib6_walker_t
*w
= (void*)cb
->args
[2];
306 fib6_walker_unlink(w
);
311 cb
->done
= (void*)cb
->args
[3];
315 static int fib6_dump_done(struct netlink_callback
*cb
)
318 return cb
->done
? cb
->done(cb
) : 0;
321 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
322 struct netlink_callback
*cb
)
324 struct fib6_walker_t
*w
;
327 w
= (void *)cb
->args
[2];
328 w
->root
= &table
->tb6_root
;
330 if (cb
->args
[4] == 0) {
334 read_lock_bh(&table
->tb6_lock
);
336 read_unlock_bh(&table
->tb6_lock
);
339 cb
->args
[5] = w
->root
->fn_sernum
;
342 if (cb
->args
[5] != w
->root
->fn_sernum
) {
343 /* Begin at the root if the tree changed */
344 cb
->args
[5] = w
->root
->fn_sernum
;
351 read_lock_bh(&table
->tb6_lock
);
352 res
= fib6_walk_continue(w
);
353 read_unlock_bh(&table
->tb6_lock
);
355 fib6_walker_unlink(w
);
363 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
365 struct net
*net
= sock_net(skb
->sk
);
367 unsigned int e
= 0, s_e
;
368 struct rt6_rtnl_dump_arg arg
;
369 struct fib6_walker_t
*w
;
370 struct fib6_table
*tb
;
371 struct hlist_head
*head
;
377 w
= (void *)cb
->args
[2];
381 * 1. hook callback destructor.
383 cb
->args
[3] = (long)cb
->done
;
384 cb
->done
= fib6_dump_done
;
387 * 2. allocate and initialize walker.
389 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
392 w
->func
= fib6_dump_node
;
393 cb
->args
[2] = (long)w
;
402 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
404 head
= &net
->ipv6
.fib_table_hash
[h
];
405 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
408 res
= fib6_dump_table(tb
, skb
, cb
);
420 res
= res
< 0 ? res
: skb
->len
;
429 * return the appropriate node for a routing tree "add" operation
430 * by either creating and inserting or by returning an existing
434 static struct fib6_node
* fib6_add_1(struct fib6_node
*root
, void *addr
,
435 int addrlen
, int plen
,
436 int offset
, int allow_create
,
437 int replace_required
)
439 struct fib6_node
*fn
, *in
, *ln
;
440 struct fib6_node
*pn
= NULL
;
444 __u32 sernum
= fib6_new_sernum();
446 RT6_TRACE("fib6_add_1\n");
448 /* insert node in tree */
453 key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
458 if (plen
< fn
->fn_bit
||
459 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
461 if (replace_required
) {
462 pr_warn("Can't replace route, no match found\n");
463 return ERR_PTR(-ENOENT
);
465 pr_warn("NLM_F_CREATE should be set when creating new route\n");
474 if (plen
== fn
->fn_bit
) {
475 /* clean up an intermediate node */
476 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
477 rt6_release(fn
->leaf
);
481 fn
->fn_sernum
= sernum
;
487 * We have more bits to go
490 /* Try to walk down on tree. */
491 fn
->fn_sernum
= sernum
;
492 dir
= addr_bit_set(addr
, fn
->fn_bit
);
494 fn
= dir
? fn
->right
: fn
->left
;
498 /* We should not create new node because
499 * NLM_F_REPLACE was specified without NLM_F_CREATE
500 * I assume it is safe to require NLM_F_CREATE when
501 * REPLACE flag is used! Later we may want to remove the
502 * check for replace_required, because according
503 * to netlink specification, NLM_F_CREATE
504 * MUST be specified if new route is created.
505 * That would keep IPv6 consistent with IPv4
507 if (replace_required
) {
508 pr_warn("Can't replace route, no match found\n");
509 return ERR_PTR(-ENOENT
);
511 pr_warn("NLM_F_CREATE should be set when creating new route\n");
514 * We walked to the bottom of tree.
515 * Create new leaf node without children.
521 return ERR_PTR(-ENOMEM
);
525 ln
->fn_sernum
= sernum
;
537 * split since we don't have a common prefix anymore or
538 * we have a less significant route.
539 * we've to insert an intermediate node on the list
540 * this new node will point to the one we need to create
546 /* find 1st bit in difference between the 2 addrs.
548 See comment in __ipv6_addr_diff: bit may be an invalid value,
549 but if it is >= plen, the value is ignored in any case.
552 bit
= __ipv6_addr_diff(addr
, &key
->addr
, addrlen
);
557 * (new leaf node)[ln] (old node)[fn]
568 return ERR_PTR(-ENOMEM
);
572 * new intermediate node.
574 * be off since that an address that chooses one of
575 * the branches would not match less specific routes
576 * in the other branch
583 atomic_inc(&in
->leaf
->rt6i_ref
);
585 in
->fn_sernum
= sernum
;
587 /* update parent pointer */
598 ln
->fn_sernum
= sernum
;
600 if (addr_bit_set(addr
, bit
)) {
607 } else { /* plen <= bit */
610 * (new leaf node)[ln]
612 * (old node)[fn] NULL
618 return ERR_PTR(-ENOMEM
);
624 ln
->fn_sernum
= sernum
;
631 if (addr_bit_set(&key
->addr
, plen
))
641 static inline bool rt6_qualify_for_ecmp(struct rt6_info
*rt
)
643 return (rt
->rt6i_flags
& (RTF_GATEWAY
|RTF_ADDRCONF
|RTF_DYNAMIC
)) ==
647 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
650 if (atomic_read(&rt
->rt6i_ref
) != 1) {
651 /* This route is used as dummy address holder in some split
652 * nodes. It is not leaked, but it still holds other resources,
653 * which must be released in time. So, scan ascendant nodes
654 * and replace dummy references to this route with references
655 * to still alive ones.
658 if (!(fn
->fn_flags
& RTN_RTINFO
) && fn
->leaf
== rt
) {
659 fn
->leaf
= fib6_find_prefix(net
, fn
);
660 atomic_inc(&fn
->leaf
->rt6i_ref
);
665 /* No more references are possible at this point. */
666 BUG_ON(atomic_read(&rt
->rt6i_ref
) != 1);
671 * Insert routing information in a node.
674 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
675 struct nl_info
*info
)
677 struct rt6_info
*iter
= NULL
;
678 struct rt6_info
**ins
;
679 int replace
= (info
->nlh
&&
680 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
681 int add
= (!info
->nlh
||
682 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
684 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
688 for (iter
= fn
->leaf
; iter
; iter
= iter
->dst
.rt6_next
) {
690 * Search for duplicates
693 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
695 * Same priority level
698 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
705 if (iter
->dst
.dev
== rt
->dst
.dev
&&
706 iter
->rt6i_idev
== rt
->rt6i_idev
&&
707 ipv6_addr_equal(&iter
->rt6i_gateway
,
708 &rt
->rt6i_gateway
)) {
709 if (rt
->rt6i_nsiblings
)
710 rt
->rt6i_nsiblings
= 0;
711 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
713 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
714 rt6_clean_expires(iter
);
716 rt6_set_expires(iter
, rt
->dst
.expires
);
719 /* If we have the same destination and the same metric,
720 * but not the same gateway, then the route we try to
721 * add is sibling to this route, increment our counter
722 * of siblings, and later we will add our route to the
724 * Only static routes (which don't have flag
725 * RTF_EXPIRES) are used for ECMPv6.
727 * To avoid long list, we only had siblings if the
728 * route have a gateway.
731 rt6_qualify_for_ecmp(iter
))
732 rt
->rt6i_nsiblings
++;
735 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
738 ins
= &iter
->dst
.rt6_next
;
741 /* Reset round-robin state, if necessary */
742 if (ins
== &fn
->leaf
)
745 /* Link this route to others same route. */
746 if (rt
->rt6i_nsiblings
) {
747 unsigned int rt6i_nsiblings
;
748 struct rt6_info
*sibling
, *temp_sibling
;
750 /* Find the first route that have the same metric */
753 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
754 rt6_qualify_for_ecmp(sibling
)) {
755 list_add_tail(&rt
->rt6i_siblings
,
756 &sibling
->rt6i_siblings
);
759 sibling
= sibling
->dst
.rt6_next
;
761 /* For each sibling in the list, increment the counter of
762 * siblings. BUG() if counters does not match, list of siblings
766 list_for_each_entry_safe(sibling
, temp_sibling
,
767 &rt
->rt6i_siblings
, rt6i_siblings
) {
768 sibling
->rt6i_nsiblings
++;
769 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
772 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
780 pr_warn("NLM_F_CREATE should be set when creating new route\n");
783 rt
->dst
.rt6_next
= iter
;
786 atomic_inc(&rt
->rt6i_ref
);
787 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
);
788 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
790 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
791 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
792 fn
->fn_flags
|= RTN_RTINFO
;
799 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
804 rt
->dst
.rt6_next
= iter
->dst
.rt6_next
;
805 atomic_inc(&rt
->rt6i_ref
);
806 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
);
807 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
808 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
809 fn
->fn_flags
|= RTN_RTINFO
;
811 fib6_purge_rt(iter
, fn
, info
->nl_net
);
818 static __inline__
void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
820 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
821 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
822 mod_timer(&net
->ipv6
.ip6_fib_timer
,
823 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
826 void fib6_force_start_gc(struct net
*net
)
828 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
829 mod_timer(&net
->ipv6
.ip6_fib_timer
,
830 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
834 * Add routing information to the routing tree.
835 * <destination addr>/<source addr>
836 * with source addr info in sub-trees
839 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
, struct nl_info
*info
)
841 struct fib6_node
*fn
, *pn
= NULL
;
843 int allow_create
= 1;
844 int replace_required
= 0;
847 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
849 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
850 replace_required
= 1;
852 if (!allow_create
&& !replace_required
)
853 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
855 fn
= fib6_add_1(root
, &rt
->rt6i_dst
.addr
, sizeof(struct in6_addr
),
856 rt
->rt6i_dst
.plen
, offsetof(struct rt6_info
, rt6i_dst
),
857 allow_create
, replace_required
);
866 #ifdef CONFIG_IPV6_SUBTREES
867 if (rt
->rt6i_src
.plen
) {
868 struct fib6_node
*sn
;
871 struct fib6_node
*sfn
;
883 /* Create subtree root node */
888 sfn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
889 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
890 sfn
->fn_flags
= RTN_ROOT
;
891 sfn
->fn_sernum
= fib6_new_sernum();
893 /* Now add the first leaf node to new subtree */
895 sn
= fib6_add_1(sfn
, &rt
->rt6i_src
.addr
,
896 sizeof(struct in6_addr
), rt
->rt6i_src
.plen
,
897 offsetof(struct rt6_info
, rt6i_src
),
898 allow_create
, replace_required
);
901 /* If it is failed, discard just allocated
902 root, and then (in st_failure) stale node
910 /* Now link new subtree to main tree */
914 sn
= fib6_add_1(fn
->subtree
, &rt
->rt6i_src
.addr
,
915 sizeof(struct in6_addr
), rt
->rt6i_src
.plen
,
916 offsetof(struct rt6_info
, rt6i_src
),
917 allow_create
, replace_required
);
927 atomic_inc(&rt
->rt6i_ref
);
933 err
= fib6_add_rt2node(fn
, rt
, info
);
935 fib6_start_gc(info
->nl_net
, rt
);
936 if (!(rt
->rt6i_flags
& RTF_CACHE
))
937 fib6_prune_clones(info
->nl_net
, pn
, rt
);
942 #ifdef CONFIG_IPV6_SUBTREES
944 * If fib6_add_1 has cleared the old leaf pointer in the
945 * super-tree leaf node we have to find a new one for it.
947 if (pn
!= fn
&& pn
->leaf
== rt
) {
949 atomic_dec(&rt
->rt6i_ref
);
951 if (pn
!= fn
&& !pn
->leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
952 pn
->leaf
= fib6_find_prefix(info
->nl_net
, pn
);
955 WARN_ON(pn
->leaf
== NULL
);
956 pn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
959 atomic_inc(&pn
->leaf
->rt6i_ref
);
966 #ifdef CONFIG_IPV6_SUBTREES
967 /* Subtree creation failed, probably main tree node
968 is orphan. If it is, shoot it.
971 if (fn
&& !(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)))
972 fib6_repair_tree(info
->nl_net
, fn
);
979 * Routing tree lookup
984 int offset
; /* key offset on rt6_info */
985 const struct in6_addr
*addr
; /* search key */
988 static struct fib6_node
* fib6_lookup_1(struct fib6_node
*root
,
989 struct lookup_args
*args
)
991 struct fib6_node
*fn
;
994 if (unlikely(args
->offset
== 0))
1004 struct fib6_node
*next
;
1006 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1008 next
= dir
? fn
->right
: fn
->left
;
1018 if (FIB6_SUBTREE(fn
) || fn
->fn_flags
& RTN_RTINFO
) {
1021 key
= (struct rt6key
*) ((u8
*) fn
->leaf
+
1024 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1025 #ifdef CONFIG_IPV6_SUBTREES
1027 struct fib6_node
*sfn
;
1028 sfn
= fib6_lookup_1(fn
->subtree
,
1035 if (fn
->fn_flags
& RTN_RTINFO
)
1039 #ifdef CONFIG_IPV6_SUBTREES
1042 if (fn
->fn_flags
& RTN_ROOT
)
1051 struct fib6_node
* fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1052 const struct in6_addr
*saddr
)
1054 struct fib6_node
*fn
;
1055 struct lookup_args args
[] = {
1057 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1060 #ifdef CONFIG_IPV6_SUBTREES
1062 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1067 .offset
= 0, /* sentinel */
1071 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1072 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1079 * Get node with specified destination prefix (and source prefix,
1080 * if subtrees are used)
1084 static struct fib6_node
* fib6_locate_1(struct fib6_node
*root
,
1085 const struct in6_addr
*addr
,
1086 int plen
, int offset
)
1088 struct fib6_node
*fn
;
1090 for (fn
= root
; fn
; ) {
1091 struct rt6key
*key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
1096 if (plen
< fn
->fn_bit
||
1097 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1100 if (plen
== fn
->fn_bit
)
1104 * We have more bits to go
1106 if (addr_bit_set(addr
, fn
->fn_bit
))
1114 struct fib6_node
* fib6_locate(struct fib6_node
*root
,
1115 const struct in6_addr
*daddr
, int dst_len
,
1116 const struct in6_addr
*saddr
, int src_len
)
1118 struct fib6_node
*fn
;
1120 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1121 offsetof(struct rt6_info
, rt6i_dst
));
1123 #ifdef CONFIG_IPV6_SUBTREES
1125 WARN_ON(saddr
== NULL
);
1126 if (fn
&& fn
->subtree
)
1127 fn
= fib6_locate_1(fn
->subtree
, saddr
, src_len
,
1128 offsetof(struct rt6_info
, rt6i_src
));
1132 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1144 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
)
1146 if (fn
->fn_flags
& RTN_ROOT
)
1147 return net
->ipv6
.ip6_null_entry
;
1151 return fn
->left
->leaf
;
1153 return fn
->right
->leaf
;
1155 fn
= FIB6_SUBTREE(fn
);
1161 * Called to trim the tree of intermediate nodes when possible. "fn"
1162 * is the node we want to try and remove.
1165 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1166 struct fib6_node
*fn
)
1170 struct fib6_node
*child
, *pn
;
1171 struct fib6_walker_t
*w
;
1175 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1178 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1179 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1180 WARN_ON(fn
->leaf
!= NULL
);
1184 if (fn
->right
) child
= fn
->right
, children
|= 1;
1185 if (fn
->left
) child
= fn
->left
, children
|= 2;
1187 if (children
== 3 || FIB6_SUBTREE(fn
)
1188 #ifdef CONFIG_IPV6_SUBTREES
1189 /* Subtree root (i.e. fn) may have one child */
1190 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1193 fn
->leaf
= fib6_find_prefix(net
, fn
);
1197 fn
->leaf
= net
->ipv6
.ip6_null_entry
;
1200 atomic_inc(&fn
->leaf
->rt6i_ref
);
1205 #ifdef CONFIG_IPV6_SUBTREES
1206 if (FIB6_SUBTREE(pn
) == fn
) {
1207 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1208 FIB6_SUBTREE(pn
) = NULL
;
1211 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1213 if (pn
->right
== fn
) pn
->right
= child
;
1214 else if (pn
->left
== fn
) pn
->left
= child
;
1222 #ifdef CONFIG_IPV6_SUBTREES
1226 read_lock(&fib6_walker_lock
);
1229 if (w
->root
== fn
) {
1230 w
->root
= w
->node
= NULL
;
1231 RT6_TRACE("W %p adjusted by delroot 1\n", w
);
1232 } else if (w
->node
== fn
) {
1233 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1238 if (w
->root
== fn
) {
1240 RT6_TRACE("W %p adjusted by delroot 2\n", w
);
1242 if (w
->node
== fn
) {
1245 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1246 w
->state
= w
->state
>=FWS_R
? FWS_U
: FWS_INIT
;
1248 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1249 w
->state
= w
->state
>=FWS_C
? FWS_U
: FWS_INIT
;
1254 read_unlock(&fib6_walker_lock
);
1257 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1260 rt6_release(pn
->leaf
);
1266 static void fib6_del_route(struct fib6_node
*fn
, struct rt6_info
**rtp
,
1267 struct nl_info
*info
)
1269 struct fib6_walker_t
*w
;
1270 struct rt6_info
*rt
= *rtp
;
1271 struct net
*net
= info
->nl_net
;
1273 RT6_TRACE("fib6_del_route\n");
1276 *rtp
= rt
->dst
.rt6_next
;
1277 rt
->rt6i_node
= NULL
;
1278 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1279 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1281 /* Reset round-robin state, if necessary */
1282 if (fn
->rr_ptr
== rt
)
1285 /* Remove this entry from other siblings */
1286 if (rt
->rt6i_nsiblings
) {
1287 struct rt6_info
*sibling
, *next_sibling
;
1289 list_for_each_entry_safe(sibling
, next_sibling
,
1290 &rt
->rt6i_siblings
, rt6i_siblings
)
1291 sibling
->rt6i_nsiblings
--;
1292 rt
->rt6i_nsiblings
= 0;
1293 list_del_init(&rt
->rt6i_siblings
);
1296 /* Adjust walkers */
1297 read_lock(&fib6_walker_lock
);
1299 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1300 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1301 w
->leaf
= rt
->dst
.rt6_next
;
1306 read_unlock(&fib6_walker_lock
);
1308 rt
->dst
.rt6_next
= NULL
;
1310 /* If it was last route, expunge its radix tree node */
1312 fn
->fn_flags
&= ~RTN_RTINFO
;
1313 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1314 fn
= fib6_repair_tree(net
, fn
);
1317 fib6_purge_rt(rt
, fn
, net
);
1319 inet6_rt_notify(RTM_DELROUTE
, rt
, info
);
1323 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1325 struct net
*net
= info
->nl_net
;
1326 struct fib6_node
*fn
= rt
->rt6i_node
;
1327 struct rt6_info
**rtp
;
1330 if (rt
->dst
.obsolete
>0) {
1331 WARN_ON(fn
!= NULL
);
1335 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1338 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1340 if (!(rt
->rt6i_flags
& RTF_CACHE
)) {
1341 struct fib6_node
*pn
= fn
;
1342 #ifdef CONFIG_IPV6_SUBTREES
1343 /* clones of this route might be in another subtree */
1344 if (rt
->rt6i_src
.plen
) {
1345 while (!(pn
->fn_flags
& RTN_ROOT
))
1350 fib6_prune_clones(info
->nl_net
, pn
, rt
);
1354 * Walk the leaf entries looking for ourself
1357 for (rtp
= &fn
->leaf
; *rtp
; rtp
= &(*rtp
)->dst
.rt6_next
) {
1359 fib6_del_route(fn
, rtp
, info
);
1367 * Tree traversal function.
1369 * Certainly, it is not interrupt safe.
1370 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1371 * It means, that we can modify tree during walking
1372 * and use this function for garbage collection, clone pruning,
1373 * cleaning tree when a device goes down etc. etc.
1375 * It guarantees that every node will be traversed,
1376 * and that it will be traversed only once.
1378 * Callback function w->func may return:
1379 * 0 -> continue walking.
1380 * positive value -> walking is suspended (used by tree dumps,
1381 * and probably by gc, if it will be split to several slices)
1382 * negative value -> terminate walking.
1384 * The function itself returns:
1385 * 0 -> walk is complete.
1386 * >0 -> walk is incomplete (i.e. suspended)
1387 * <0 -> walk is terminated by an error.
1390 static int fib6_walk_continue(struct fib6_walker_t
*w
)
1392 struct fib6_node
*fn
, *pn
;
1399 if (w
->prune
&& fn
!= w
->root
&&
1400 fn
->fn_flags
& RTN_RTINFO
&& w
->state
< FWS_C
) {
1405 #ifdef CONFIG_IPV6_SUBTREES
1407 if (FIB6_SUBTREE(fn
)) {
1408 w
->node
= FIB6_SUBTREE(fn
);
1416 w
->state
= FWS_INIT
;
1422 w
->node
= fn
->right
;
1423 w
->state
= FWS_INIT
;
1429 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1451 #ifdef CONFIG_IPV6_SUBTREES
1452 if (FIB6_SUBTREE(pn
) == fn
) {
1453 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1458 if (pn
->left
== fn
) {
1462 if (pn
->right
== fn
) {
1464 w
->leaf
= w
->node
->leaf
;
1474 static int fib6_walk(struct fib6_walker_t
*w
)
1478 w
->state
= FWS_INIT
;
1481 fib6_walker_link(w
);
1482 res
= fib6_walk_continue(w
);
1484 fib6_walker_unlink(w
);
1488 static int fib6_clean_node(struct fib6_walker_t
*w
)
1491 struct rt6_info
*rt
;
1492 struct fib6_cleaner_t
*c
= container_of(w
, struct fib6_cleaner_t
, w
);
1493 struct nl_info info
= {
1497 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1498 res
= c
->func(rt
, c
->arg
);
1501 res
= fib6_del(rt
, &info
);
1504 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1505 __func__
, rt
, rt
->rt6i_node
, res
);
1518 * Convenient frontend to tree walker.
1520 * func is called on each route.
1521 * It may return -1 -> delete this route.
1522 * 0 -> continue walking
1524 * prune==1 -> only immediate children of node (certainly,
1525 * ignoring pure split nodes) will be scanned.
1528 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1529 int (*func
)(struct rt6_info
*, void *arg
),
1530 int prune
, void *arg
)
1532 struct fib6_cleaner_t c
;
1535 c
.w
.func
= fib6_clean_node
;
1546 void fib6_clean_all_ro(struct net
*net
, int (*func
)(struct rt6_info
*, void *arg
),
1547 int prune
, void *arg
)
1549 struct fib6_table
*table
;
1550 struct hlist_head
*head
;
1554 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1555 head
= &net
->ipv6
.fib_table_hash
[h
];
1556 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1557 read_lock_bh(&table
->tb6_lock
);
1558 fib6_clean_tree(net
, &table
->tb6_root
,
1560 read_unlock_bh(&table
->tb6_lock
);
1565 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *arg
),
1566 int prune
, void *arg
)
1568 struct fib6_table
*table
;
1569 struct hlist_head
*head
;
1573 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1574 head
= &net
->ipv6
.fib_table_hash
[h
];
1575 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1576 write_lock_bh(&table
->tb6_lock
);
1577 fib6_clean_tree(net
, &table
->tb6_root
,
1579 write_unlock_bh(&table
->tb6_lock
);
1585 static int fib6_prune_clone(struct rt6_info
*rt
, void *arg
)
1587 if (rt
->rt6i_flags
& RTF_CACHE
) {
1588 RT6_TRACE("pruning clone %p\n", rt
);
1595 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
,
1596 struct rt6_info
*rt
)
1598 fib6_clean_tree(net
, fn
, fib6_prune_clone
, 1, rt
);
1602 * Garbage collection
1605 static struct fib6_gc_args
1611 static int fib6_age(struct rt6_info
*rt
, void *arg
)
1613 unsigned long now
= jiffies
;
1616 * check addrconf expiration here.
1617 * Routes are expired even if they are in use.
1619 * Also age clones. Note, that clones are aged out
1620 * only if they are not in use now.
1623 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
1624 if (time_after(now
, rt
->dst
.expires
)) {
1625 RT6_TRACE("expiring %p\n", rt
);
1629 } else if (rt
->rt6i_flags
& RTF_CACHE
) {
1630 if (atomic_read(&rt
->dst
.__refcnt
) == 0 &&
1631 time_after_eq(now
, rt
->dst
.lastuse
+ gc_args
.timeout
)) {
1632 RT6_TRACE("aging clone %p\n", rt
);
1634 } else if (rt
->rt6i_flags
& RTF_GATEWAY
) {
1635 struct neighbour
*neigh
;
1636 __u8 neigh_flags
= 0;
1638 neigh
= dst_neigh_lookup(&rt
->dst
, &rt
->rt6i_gateway
);
1640 neigh_flags
= neigh
->flags
;
1641 neigh_release(neigh
);
1643 if (!(neigh_flags
& NTF_ROUTER
)) {
1644 RT6_TRACE("purging route %p via non-router but gateway\n",
1655 static DEFINE_SPINLOCK(fib6_gc_lock
);
1657 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
1662 spin_lock_bh(&fib6_gc_lock
);
1663 } else if (!spin_trylock_bh(&fib6_gc_lock
)) {
1664 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
1667 gc_args
.timeout
= expires
? (int)expires
:
1668 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
1670 gc_args
.more
= icmp6_dst_gc();
1672 fib6_clean_all(net
, fib6_age
, 0, NULL
);
1674 net
->ipv6
.ip6_rt_last_gc
= now
;
1677 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1679 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
1681 del_timer(&net
->ipv6
.ip6_fib_timer
);
1682 spin_unlock_bh(&fib6_gc_lock
);
1685 static void fib6_gc_timer_cb(unsigned long arg
)
1687 fib6_run_gc(0, (struct net
*)arg
, true);
1690 static int __net_init
fib6_net_init(struct net
*net
)
1692 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
1694 setup_timer(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, (unsigned long)net
);
1696 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
1697 if (!net
->ipv6
.rt6_stats
)
1700 /* Avoid false sharing : Use at least a full cache line */
1701 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
1703 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
1704 if (!net
->ipv6
.fib_table_hash
)
1707 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
1709 if (!net
->ipv6
.fib6_main_tbl
)
1710 goto out_fib_table_hash
;
1712 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
1713 net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1714 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
1715 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1716 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1718 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1719 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
1721 if (!net
->ipv6
.fib6_local_tbl
)
1722 goto out_fib6_main_tbl
;
1723 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
1724 net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1725 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
1726 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1727 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1729 fib6_tables_init(net
);
1733 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1735 kfree(net
->ipv6
.fib6_main_tbl
);
1738 kfree(net
->ipv6
.fib_table_hash
);
1740 kfree(net
->ipv6
.rt6_stats
);
1745 static void fib6_net_exit(struct net
*net
)
1749 rt6_ifdown(net
, NULL
);
1750 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
1752 for (i
= 0; i
< FIB6_TABLE_HASHSZ
; i
++) {
1753 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[i
];
1754 struct hlist_node
*tmp
;
1755 struct fib6_table
*tb
;
1757 hlist_for_each_entry_safe(tb
, tmp
, head
, tb6_hlist
) {
1758 hlist_del(&tb
->tb6_hlist
);
1759 fib6_free_table(tb
);
1763 kfree(net
->ipv6
.fib_table_hash
);
1764 kfree(net
->ipv6
.rt6_stats
);
1767 static struct pernet_operations fib6_net_ops
= {
1768 .init
= fib6_net_init
,
1769 .exit
= fib6_net_exit
,
1772 int __init
fib6_init(void)
1776 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
1777 sizeof(struct fib6_node
),
1778 0, SLAB_HWCACHE_ALIGN
,
1780 if (!fib6_node_kmem
)
1783 ret
= register_pernet_subsys(&fib6_net_ops
);
1785 goto out_kmem_cache_create
;
1787 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
1790 goto out_unregister_subsys
;
1794 out_unregister_subsys
:
1795 unregister_pernet_subsys(&fib6_net_ops
);
1796 out_kmem_cache_create
:
1797 kmem_cache_destroy(fib6_node_kmem
);
1801 void fib6_gc_cleanup(void)
1803 unregister_pernet_subsys(&fib6_net_ops
);
1804 kmem_cache_destroy(fib6_node_kmem
);