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.
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
17 * Ville Nuorvala: Fixed routing subtrees.
20 #define pr_fmt(fmt) "IPv6: " fmt
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>
30 #include <linux/slab.h>
33 #include <net/ndisc.h>
34 #include <net/addrconf.h>
35 #include <net/lwtunnel.h>
37 #include <net/ip6_fib.h>
38 #include <net/ip6_route.h>
43 #define RT6_TRACE(x...) pr_debug(x)
45 #define RT6_TRACE(x...) do { ; } while (0)
48 static struct kmem_cache
*fib6_node_kmem __read_mostly
;
53 int (*func
)(struct rt6_info
*, void *arg
);
58 static DEFINE_RWLOCK(fib6_walker_lock
);
60 #ifdef CONFIG_IPV6_SUBTREES
61 #define FWS_INIT FWS_S
63 #define FWS_INIT FWS_L
66 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
);
67 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
);
68 static struct fib6_node
*fib6_repair_tree(struct net
*net
, struct fib6_node
*fn
);
69 static int fib6_walk(struct fib6_walker
*w
);
70 static int fib6_walk_continue(struct fib6_walker
*w
);
73 * A routing update causes an increase of the serial number on the
74 * affected subtree. This allows for cached routes to be asynchronously
75 * tested when modifications are made to the destination cache as a
76 * result of redirects, path MTU changes, etc.
79 static void fib6_gc_timer_cb(unsigned long arg
);
81 static LIST_HEAD(fib6_walkers
);
82 #define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
84 static void fib6_walker_link(struct fib6_walker
*w
)
86 write_lock_bh(&fib6_walker_lock
);
87 list_add(&w
->lh
, &fib6_walkers
);
88 write_unlock_bh(&fib6_walker_lock
);
91 static void fib6_walker_unlink(struct fib6_walker
*w
)
93 write_lock_bh(&fib6_walker_lock
);
95 write_unlock_bh(&fib6_walker_lock
);
98 static int fib6_new_sernum(struct net
*net
)
103 old
= atomic_read(&net
->ipv6
.fib6_sernum
);
104 new = old
< INT_MAX
? old
+ 1 : 1;
105 } while (atomic_cmpxchg(&net
->ipv6
.fib6_sernum
,
111 FIB6_NO_SERNUM_CHANGE
= 0,
115 * Auxiliary address test functions for the radix tree.
117 * These assume a 32bit processor (although it will work on
124 #if defined(__LITTLE_ENDIAN)
125 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
127 # define BITOP_BE32_SWIZZLE 0
130 static __be32
addr_bit_set(const void *token
, int fn_bit
)
132 const __be32
*addr
= token
;
135 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
136 * is optimized version of
137 * htonl(1 << ((~fn_bit)&0x1F))
138 * See include/asm-generic/bitops/le.h.
140 return (__force __be32
)(1 << ((~fn_bit
^ BITOP_BE32_SWIZZLE
) & 0x1f)) &
144 static struct fib6_node
*node_alloc(void)
146 struct fib6_node
*fn
;
148 fn
= kmem_cache_zalloc(fib6_node_kmem
, GFP_ATOMIC
);
153 static void node_free(struct fib6_node
*fn
)
155 kmem_cache_free(fib6_node_kmem
, fn
);
158 static void rt6_rcu_free(struct rt6_info
*rt
)
160 call_rcu(&rt
->dst
.rcu_head
, dst_rcu_free
);
163 static void rt6_free_pcpu(struct rt6_info
*non_pcpu_rt
)
167 if (!non_pcpu_rt
->rt6i_pcpu
)
170 for_each_possible_cpu(cpu
) {
171 struct rt6_info
**ppcpu_rt
;
172 struct rt6_info
*pcpu_rt
;
174 ppcpu_rt
= per_cpu_ptr(non_pcpu_rt
->rt6i_pcpu
, cpu
);
177 rt6_rcu_free(pcpu_rt
);
182 non_pcpu_rt
->rt6i_pcpu
= NULL
;
185 static void rt6_release(struct rt6_info
*rt
)
187 if (atomic_dec_and_test(&rt
->rt6i_ref
)) {
193 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
198 * Initialize table lock at a single place to give lockdep a key,
199 * tables aren't visible prior to being linked to the list.
201 rwlock_init(&tb
->tb6_lock
);
203 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
206 * No protection necessary, this is the only list mutatation
207 * operation, tables never disappear once they exist.
209 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
212 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
214 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
216 struct fib6_table
*table
;
218 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
221 table
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
222 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
223 inet_peer_base_init(&table
->tb6_peers
);
229 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
231 struct fib6_table
*tb
;
235 tb
= fib6_get_table(net
, id
);
239 tb
= fib6_alloc_table(net
, id
);
241 fib6_link_table(net
, tb
);
246 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
248 struct fib6_table
*tb
;
249 struct hlist_head
*head
;
254 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
256 head
= &net
->ipv6
.fib_table_hash
[h
];
257 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
258 if (tb
->tb6_id
== id
) {
268 static void __net_init
fib6_tables_init(struct net
*net
)
270 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
271 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
275 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
277 return fib6_get_table(net
, id
);
280 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
282 return net
->ipv6
.fib6_main_tbl
;
285 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
286 int flags
, pol_lookup_t lookup
)
290 rt
= lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
291 if (rt
->rt6i_flags
& RTF_REJECT
&&
292 rt
->dst
.error
== -EAGAIN
) {
294 rt
= net
->ipv6
.ip6_null_entry
;
301 static void __net_init
fib6_tables_init(struct net
*net
)
303 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
308 static int fib6_dump_node(struct fib6_walker
*w
)
313 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
314 res
= rt6_dump_route(rt
, w
->args
);
316 /* Frame is full, suspend walking */
325 static void fib6_dump_end(struct netlink_callback
*cb
)
327 struct fib6_walker
*w
= (void *)cb
->args
[2];
332 fib6_walker_unlink(w
);
337 cb
->done
= (void *)cb
->args
[3];
341 static int fib6_dump_done(struct netlink_callback
*cb
)
344 return cb
->done
? cb
->done(cb
) : 0;
347 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
348 struct netlink_callback
*cb
)
350 struct fib6_walker
*w
;
353 w
= (void *)cb
->args
[2];
354 w
->root
= &table
->tb6_root
;
356 if (cb
->args
[4] == 0) {
360 read_lock_bh(&table
->tb6_lock
);
362 read_unlock_bh(&table
->tb6_lock
);
365 cb
->args
[5] = w
->root
->fn_sernum
;
368 if (cb
->args
[5] != w
->root
->fn_sernum
) {
369 /* Begin at the root if the tree changed */
370 cb
->args
[5] = w
->root
->fn_sernum
;
377 read_lock_bh(&table
->tb6_lock
);
378 res
= fib6_walk_continue(w
);
379 read_unlock_bh(&table
->tb6_lock
);
381 fib6_walker_unlink(w
);
389 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
391 struct net
*net
= sock_net(skb
->sk
);
393 unsigned int e
= 0, s_e
;
394 struct rt6_rtnl_dump_arg arg
;
395 struct fib6_walker
*w
;
396 struct fib6_table
*tb
;
397 struct hlist_head
*head
;
403 w
= (void *)cb
->args
[2];
407 * 1. hook callback destructor.
409 cb
->args
[3] = (long)cb
->done
;
410 cb
->done
= fib6_dump_done
;
413 * 2. allocate and initialize walker.
415 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
418 w
->func
= fib6_dump_node
;
419 cb
->args
[2] = (long)w
;
428 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
430 head
= &net
->ipv6
.fib_table_hash
[h
];
431 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
434 res
= fib6_dump_table(tb
, skb
, cb
);
446 res
= res
< 0 ? res
: skb
->len
;
455 * return the appropriate node for a routing tree "add" operation
456 * by either creating and inserting or by returning an existing
460 static struct fib6_node
*fib6_add_1(struct fib6_node
*root
,
461 struct in6_addr
*addr
, int plen
,
462 int offset
, int allow_create
,
463 int replace_required
, int sernum
)
465 struct fib6_node
*fn
, *in
, *ln
;
466 struct fib6_node
*pn
= NULL
;
471 RT6_TRACE("fib6_add_1\n");
473 /* insert node in tree */
478 key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
483 if (plen
< fn
->fn_bit
||
484 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
486 if (replace_required
) {
487 pr_warn("Can't replace route, no match found\n");
488 return ERR_PTR(-ENOENT
);
490 pr_warn("NLM_F_CREATE should be set when creating new route\n");
499 if (plen
== fn
->fn_bit
) {
500 /* clean up an intermediate node */
501 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
502 rt6_release(fn
->leaf
);
506 fn
->fn_sernum
= sernum
;
512 * We have more bits to go
515 /* Try to walk down on tree. */
516 fn
->fn_sernum
= sernum
;
517 dir
= addr_bit_set(addr
, fn
->fn_bit
);
519 fn
= dir
? fn
->right
: fn
->left
;
523 /* We should not create new node because
524 * NLM_F_REPLACE was specified without NLM_F_CREATE
525 * I assume it is safe to require NLM_F_CREATE when
526 * REPLACE flag is used! Later we may want to remove the
527 * check for replace_required, because according
528 * to netlink specification, NLM_F_CREATE
529 * MUST be specified if new route is created.
530 * That would keep IPv6 consistent with IPv4
532 if (replace_required
) {
533 pr_warn("Can't replace route, no match found\n");
534 return ERR_PTR(-ENOENT
);
536 pr_warn("NLM_F_CREATE should be set when creating new route\n");
539 * We walked to the bottom of tree.
540 * Create new leaf node without children.
546 return ERR_PTR(-ENOMEM
);
550 ln
->fn_sernum
= sernum
;
562 * split since we don't have a common prefix anymore or
563 * we have a less significant route.
564 * we've to insert an intermediate node on the list
565 * this new node will point to the one we need to create
571 /* find 1st bit in difference between the 2 addrs.
573 See comment in __ipv6_addr_diff: bit may be an invalid value,
574 but if it is >= plen, the value is ignored in any case.
577 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
582 * (new leaf node)[ln] (old node)[fn]
593 return ERR_PTR(-ENOMEM
);
597 * new intermediate node.
599 * be off since that an address that chooses one of
600 * the branches would not match less specific routes
601 * in the other branch
608 atomic_inc(&in
->leaf
->rt6i_ref
);
610 in
->fn_sernum
= sernum
;
612 /* update parent pointer */
623 ln
->fn_sernum
= sernum
;
625 if (addr_bit_set(addr
, bit
)) {
632 } else { /* plen <= bit */
635 * (new leaf node)[ln]
637 * (old node)[fn] NULL
643 return ERR_PTR(-ENOMEM
);
649 ln
->fn_sernum
= sernum
;
656 if (addr_bit_set(&key
->addr
, plen
))
666 static bool rt6_qualify_for_ecmp(struct rt6_info
*rt
)
668 return (rt
->rt6i_flags
& (RTF_GATEWAY
|RTF_ADDRCONF
|RTF_DYNAMIC
)) ==
672 static void fib6_copy_metrics(u32
*mp
, const struct mx6_config
*mxc
)
676 for (i
= 0; i
< RTAX_MAX
; i
++) {
677 if (test_bit(i
, mxc
->mx_valid
))
682 static int fib6_commit_metrics(struct dst_entry
*dst
, struct mx6_config
*mxc
)
687 if (dst
->flags
& DST_HOST
) {
688 u32
*mp
= dst_metrics_write_ptr(dst
);
693 fib6_copy_metrics(mp
, mxc
);
695 dst_init_metrics(dst
, mxc
->mx
, false);
697 /* We've stolen mx now. */
704 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
707 if (atomic_read(&rt
->rt6i_ref
) != 1) {
708 /* This route is used as dummy address holder in some split
709 * nodes. It is not leaked, but it still holds other resources,
710 * which must be released in time. So, scan ascendant nodes
711 * and replace dummy references to this route with references
712 * to still alive ones.
715 if (!(fn
->fn_flags
& RTN_RTINFO
) && fn
->leaf
== rt
) {
716 fn
->leaf
= fib6_find_prefix(net
, fn
);
717 atomic_inc(&fn
->leaf
->rt6i_ref
);
722 /* No more references are possible at this point. */
723 BUG_ON(atomic_read(&rt
->rt6i_ref
) != 1);
728 * Insert routing information in a node.
731 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
732 struct nl_info
*info
, struct mx6_config
*mxc
)
734 struct rt6_info
*iter
= NULL
;
735 struct rt6_info
**ins
;
736 struct rt6_info
**fallback_ins
= NULL
;
737 int replace
= (info
->nlh
&&
738 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
739 int add
= (!info
->nlh
||
740 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
742 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
747 for (iter
= fn
->leaf
; iter
; iter
= iter
->dst
.rt6_next
) {
749 * Search for duplicates
752 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
754 * Same priority level
757 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
760 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
765 fallback_ins
= fallback_ins
?: ins
;
769 if (iter
->dst
.dev
== rt
->dst
.dev
&&
770 iter
->rt6i_idev
== rt
->rt6i_idev
&&
771 ipv6_addr_equal(&iter
->rt6i_gateway
,
772 &rt
->rt6i_gateway
)) {
773 if (rt
->rt6i_nsiblings
)
774 rt
->rt6i_nsiblings
= 0;
775 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
777 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
778 rt6_clean_expires(iter
);
780 rt6_set_expires(iter
, rt
->dst
.expires
);
781 iter
->rt6i_pmtu
= rt
->rt6i_pmtu
;
784 /* If we have the same destination and the same metric,
785 * but not the same gateway, then the route we try to
786 * add is sibling to this route, increment our counter
787 * of siblings, and later we will add our route to the
789 * Only static routes (which don't have flag
790 * RTF_EXPIRES) are used for ECMPv6.
792 * To avoid long list, we only had siblings if the
793 * route have a gateway.
796 rt6_qualify_for_ecmp(iter
))
797 rt
->rt6i_nsiblings
++;
800 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
804 ins
= &iter
->dst
.rt6_next
;
807 if (fallback_ins
&& !found
) {
808 /* No ECMP-able route found, replace first non-ECMP one */
814 /* Reset round-robin state, if necessary */
815 if (ins
== &fn
->leaf
)
818 /* Link this route to others same route. */
819 if (rt
->rt6i_nsiblings
) {
820 unsigned int rt6i_nsiblings
;
821 struct rt6_info
*sibling
, *temp_sibling
;
823 /* Find the first route that have the same metric */
826 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
827 rt6_qualify_for_ecmp(sibling
)) {
828 list_add_tail(&rt
->rt6i_siblings
,
829 &sibling
->rt6i_siblings
);
832 sibling
= sibling
->dst
.rt6_next
;
834 /* For each sibling in the list, increment the counter of
835 * siblings. BUG() if counters does not match, list of siblings
839 list_for_each_entry_safe(sibling
, temp_sibling
,
840 &rt
->rt6i_siblings
, rt6i_siblings
) {
841 sibling
->rt6i_nsiblings
++;
842 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
845 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
853 pr_warn("NLM_F_CREATE should be set when creating new route\n");
856 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
860 rt
->dst
.rt6_next
= iter
;
863 atomic_inc(&rt
->rt6i_ref
);
864 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, 0);
865 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
867 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
868 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
869 fn
->fn_flags
|= RTN_RTINFO
;
878 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
882 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
888 rt
->dst
.rt6_next
= iter
->dst
.rt6_next
;
889 atomic_inc(&rt
->rt6i_ref
);
890 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, NLM_F_REPLACE
);
891 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
892 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
893 fn
->fn_flags
|= RTN_RTINFO
;
895 nsiblings
= iter
->rt6i_nsiblings
;
896 fib6_purge_rt(iter
, fn
, info
->nl_net
);
900 /* Replacing an ECMP route, remove all siblings */
901 ins
= &rt
->dst
.rt6_next
;
904 if (rt6_qualify_for_ecmp(iter
)) {
905 *ins
= iter
->dst
.rt6_next
;
906 fib6_purge_rt(iter
, fn
, info
->nl_net
);
910 ins
= &iter
->dst
.rt6_next
;
914 WARN_ON(nsiblings
!= 0);
921 static void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
923 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
924 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
925 mod_timer(&net
->ipv6
.ip6_fib_timer
,
926 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
929 void fib6_force_start_gc(struct net
*net
)
931 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
932 mod_timer(&net
->ipv6
.ip6_fib_timer
,
933 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
937 * Add routing information to the routing tree.
938 * <destination addr>/<source addr>
939 * with source addr info in sub-trees
942 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
943 struct nl_info
*info
, struct mx6_config
*mxc
)
945 struct fib6_node
*fn
, *pn
= NULL
;
947 int allow_create
= 1;
948 int replace_required
= 0;
949 int sernum
= fib6_new_sernum(info
->nl_net
);
951 if (WARN_ON_ONCE((rt
->dst
.flags
& DST_NOCACHE
) &&
952 !atomic_read(&rt
->dst
.__refcnt
)))
956 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
958 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
959 replace_required
= 1;
961 if (!allow_create
&& !replace_required
)
962 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
964 fn
= fib6_add_1(root
, &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
,
965 offsetof(struct rt6_info
, rt6i_dst
), allow_create
,
966 replace_required
, sernum
);
975 #ifdef CONFIG_IPV6_SUBTREES
976 if (rt
->rt6i_src
.plen
) {
977 struct fib6_node
*sn
;
980 struct fib6_node
*sfn
;
992 /* Create subtree root node */
997 sfn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
998 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
999 sfn
->fn_flags
= RTN_ROOT
;
1000 sfn
->fn_sernum
= sernum
;
1002 /* Now add the first leaf node to new subtree */
1004 sn
= fib6_add_1(sfn
, &rt
->rt6i_src
.addr
,
1006 offsetof(struct rt6_info
, rt6i_src
),
1007 allow_create
, replace_required
, sernum
);
1010 /* If it is failed, discard just allocated
1011 root, and then (in st_failure) stale node
1019 /* Now link new subtree to main tree */
1023 sn
= fib6_add_1(fn
->subtree
, &rt
->rt6i_src
.addr
,
1025 offsetof(struct rt6_info
, rt6i_src
),
1026 allow_create
, replace_required
, sernum
);
1036 atomic_inc(&rt
->rt6i_ref
);
1042 err
= fib6_add_rt2node(fn
, rt
, info
, mxc
);
1044 fib6_start_gc(info
->nl_net
, rt
);
1045 if (!(rt
->rt6i_flags
& RTF_CACHE
))
1046 fib6_prune_clones(info
->nl_net
, pn
);
1047 rt
->dst
.flags
&= ~DST_NOCACHE
;
1052 #ifdef CONFIG_IPV6_SUBTREES
1054 * If fib6_add_1 has cleared the old leaf pointer in the
1055 * super-tree leaf node we have to find a new one for it.
1057 if (pn
!= fn
&& pn
->leaf
== rt
) {
1059 atomic_dec(&rt
->rt6i_ref
);
1061 if (pn
!= fn
&& !pn
->leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
1062 pn
->leaf
= fib6_find_prefix(info
->nl_net
, pn
);
1065 WARN_ON(pn
->leaf
== NULL
);
1066 pn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
1069 atomic_inc(&pn
->leaf
->rt6i_ref
);
1072 if (!(rt
->dst
.flags
& DST_NOCACHE
))
1077 #ifdef CONFIG_IPV6_SUBTREES
1078 /* Subtree creation failed, probably main tree node
1079 is orphan. If it is, shoot it.
1082 if (fn
&& !(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)))
1083 fib6_repair_tree(info
->nl_net
, fn
);
1084 if (!(rt
->dst
.flags
& DST_NOCACHE
))
1091 * Routing tree lookup
1095 struct lookup_args
{
1096 int offset
; /* key offset on rt6_info */
1097 const struct in6_addr
*addr
; /* search key */
1100 static struct fib6_node
*fib6_lookup_1(struct fib6_node
*root
,
1101 struct lookup_args
*args
)
1103 struct fib6_node
*fn
;
1106 if (unlikely(args
->offset
== 0))
1116 struct fib6_node
*next
;
1118 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1120 next
= dir
? fn
->right
: fn
->left
;
1130 if (FIB6_SUBTREE(fn
) || fn
->fn_flags
& RTN_RTINFO
) {
1133 key
= (struct rt6key
*) ((u8
*) fn
->leaf
+
1136 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1137 #ifdef CONFIG_IPV6_SUBTREES
1139 struct fib6_node
*sfn
;
1140 sfn
= fib6_lookup_1(fn
->subtree
,
1147 if (fn
->fn_flags
& RTN_RTINFO
)
1151 #ifdef CONFIG_IPV6_SUBTREES
1154 if (fn
->fn_flags
& RTN_ROOT
)
1163 struct fib6_node
*fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1164 const struct in6_addr
*saddr
)
1166 struct fib6_node
*fn
;
1167 struct lookup_args args
[] = {
1169 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1172 #ifdef CONFIG_IPV6_SUBTREES
1174 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1179 .offset
= 0, /* sentinel */
1183 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1184 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1191 * Get node with specified destination prefix (and source prefix,
1192 * if subtrees are used)
1196 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1197 const struct in6_addr
*addr
,
1198 int plen
, int offset
)
1200 struct fib6_node
*fn
;
1202 for (fn
= root
; fn
; ) {
1203 struct rt6key
*key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
1208 if (plen
< fn
->fn_bit
||
1209 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1212 if (plen
== fn
->fn_bit
)
1216 * We have more bits to go
1218 if (addr_bit_set(addr
, fn
->fn_bit
))
1226 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1227 const struct in6_addr
*daddr
, int dst_len
,
1228 const struct in6_addr
*saddr
, int src_len
)
1230 struct fib6_node
*fn
;
1232 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1233 offsetof(struct rt6_info
, rt6i_dst
));
1235 #ifdef CONFIG_IPV6_SUBTREES
1237 WARN_ON(saddr
== NULL
);
1238 if (fn
&& fn
->subtree
)
1239 fn
= fib6_locate_1(fn
->subtree
, saddr
, src_len
,
1240 offsetof(struct rt6_info
, rt6i_src
));
1244 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1256 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
)
1258 if (fn
->fn_flags
& RTN_ROOT
)
1259 return net
->ipv6
.ip6_null_entry
;
1263 return fn
->left
->leaf
;
1265 return fn
->right
->leaf
;
1267 fn
= FIB6_SUBTREE(fn
);
1273 * Called to trim the tree of intermediate nodes when possible. "fn"
1274 * is the node we want to try and remove.
1277 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1278 struct fib6_node
*fn
)
1282 struct fib6_node
*child
, *pn
;
1283 struct fib6_walker
*w
;
1287 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1290 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1291 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1297 child
= fn
->right
, children
|= 1;
1299 child
= fn
->left
, children
|= 2;
1301 if (children
== 3 || FIB6_SUBTREE(fn
)
1302 #ifdef CONFIG_IPV6_SUBTREES
1303 /* Subtree root (i.e. fn) may have one child */
1304 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1307 fn
->leaf
= fib6_find_prefix(net
, fn
);
1311 fn
->leaf
= net
->ipv6
.ip6_null_entry
;
1314 atomic_inc(&fn
->leaf
->rt6i_ref
);
1319 #ifdef CONFIG_IPV6_SUBTREES
1320 if (FIB6_SUBTREE(pn
) == fn
) {
1321 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1322 FIB6_SUBTREE(pn
) = NULL
;
1325 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1327 if (pn
->right
== fn
)
1329 else if (pn
->left
== fn
)
1338 #ifdef CONFIG_IPV6_SUBTREES
1342 read_lock(&fib6_walker_lock
);
1345 if (w
->root
== fn
) {
1346 w
->root
= w
->node
= NULL
;
1347 RT6_TRACE("W %p adjusted by delroot 1\n", w
);
1348 } else if (w
->node
== fn
) {
1349 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1354 if (w
->root
== fn
) {
1356 RT6_TRACE("W %p adjusted by delroot 2\n", w
);
1358 if (w
->node
== fn
) {
1361 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1362 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1364 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1365 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1370 read_unlock(&fib6_walker_lock
);
1373 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1376 rt6_release(pn
->leaf
);
1382 static void fib6_del_route(struct fib6_node
*fn
, struct rt6_info
**rtp
,
1383 struct nl_info
*info
)
1385 struct fib6_walker
*w
;
1386 struct rt6_info
*rt
= *rtp
;
1387 struct net
*net
= info
->nl_net
;
1389 RT6_TRACE("fib6_del_route\n");
1392 *rtp
= rt
->dst
.rt6_next
;
1393 rt
->rt6i_node
= NULL
;
1394 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1395 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1397 /* Reset round-robin state, if necessary */
1398 if (fn
->rr_ptr
== rt
)
1401 /* Remove this entry from other siblings */
1402 if (rt
->rt6i_nsiblings
) {
1403 struct rt6_info
*sibling
, *next_sibling
;
1405 list_for_each_entry_safe(sibling
, next_sibling
,
1406 &rt
->rt6i_siblings
, rt6i_siblings
)
1407 sibling
->rt6i_nsiblings
--;
1408 rt
->rt6i_nsiblings
= 0;
1409 list_del_init(&rt
->rt6i_siblings
);
1412 /* Adjust walkers */
1413 read_lock(&fib6_walker_lock
);
1415 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1416 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1417 w
->leaf
= rt
->dst
.rt6_next
;
1422 read_unlock(&fib6_walker_lock
);
1424 rt
->dst
.rt6_next
= NULL
;
1426 /* If it was last route, expunge its radix tree node */
1428 fn
->fn_flags
&= ~RTN_RTINFO
;
1429 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1430 fn
= fib6_repair_tree(net
, fn
);
1433 fib6_purge_rt(rt
, fn
, net
);
1435 inet6_rt_notify(RTM_DELROUTE
, rt
, info
, 0);
1439 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1441 struct net
*net
= info
->nl_net
;
1442 struct fib6_node
*fn
= rt
->rt6i_node
;
1443 struct rt6_info
**rtp
;
1446 if (rt
->dst
.obsolete
> 0) {
1451 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1454 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1456 if (!(rt
->rt6i_flags
& RTF_CACHE
)) {
1457 struct fib6_node
*pn
= fn
;
1458 #ifdef CONFIG_IPV6_SUBTREES
1459 /* clones of this route might be in another subtree */
1460 if (rt
->rt6i_src
.plen
) {
1461 while (!(pn
->fn_flags
& RTN_ROOT
))
1466 fib6_prune_clones(info
->nl_net
, pn
);
1470 * Walk the leaf entries looking for ourself
1473 for (rtp
= &fn
->leaf
; *rtp
; rtp
= &(*rtp
)->dst
.rt6_next
) {
1475 fib6_del_route(fn
, rtp
, info
);
1483 * Tree traversal function.
1485 * Certainly, it is not interrupt safe.
1486 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1487 * It means, that we can modify tree during walking
1488 * and use this function for garbage collection, clone pruning,
1489 * cleaning tree when a device goes down etc. etc.
1491 * It guarantees that every node will be traversed,
1492 * and that it will be traversed only once.
1494 * Callback function w->func may return:
1495 * 0 -> continue walking.
1496 * positive value -> walking is suspended (used by tree dumps,
1497 * and probably by gc, if it will be split to several slices)
1498 * negative value -> terminate walking.
1500 * The function itself returns:
1501 * 0 -> walk is complete.
1502 * >0 -> walk is incomplete (i.e. suspended)
1503 * <0 -> walk is terminated by an error.
1506 static int fib6_walk_continue(struct fib6_walker
*w
)
1508 struct fib6_node
*fn
, *pn
;
1515 if (w
->prune
&& fn
!= w
->root
&&
1516 fn
->fn_flags
& RTN_RTINFO
&& w
->state
< FWS_C
) {
1521 #ifdef CONFIG_IPV6_SUBTREES
1523 if (FIB6_SUBTREE(fn
)) {
1524 w
->node
= FIB6_SUBTREE(fn
);
1532 w
->state
= FWS_INIT
;
1538 w
->node
= fn
->right
;
1539 w
->state
= FWS_INIT
;
1545 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1567 #ifdef CONFIG_IPV6_SUBTREES
1568 if (FIB6_SUBTREE(pn
) == fn
) {
1569 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1574 if (pn
->left
== fn
) {
1578 if (pn
->right
== fn
) {
1580 w
->leaf
= w
->node
->leaf
;
1590 static int fib6_walk(struct fib6_walker
*w
)
1594 w
->state
= FWS_INIT
;
1597 fib6_walker_link(w
);
1598 res
= fib6_walk_continue(w
);
1600 fib6_walker_unlink(w
);
1604 static int fib6_clean_node(struct fib6_walker
*w
)
1607 struct rt6_info
*rt
;
1608 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1609 struct nl_info info
= {
1613 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1614 w
->node
->fn_sernum
!= c
->sernum
)
1615 w
->node
->fn_sernum
= c
->sernum
;
1618 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1623 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1624 res
= c
->func(rt
, c
->arg
);
1627 res
= fib6_del(rt
, &info
);
1630 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1631 __func__
, rt
, rt
->rt6i_node
, res
);
1644 * Convenient frontend to tree walker.
1646 * func is called on each route.
1647 * It may return -1 -> delete this route.
1648 * 0 -> continue walking
1650 * prune==1 -> only immediate children of node (certainly,
1651 * ignoring pure split nodes) will be scanned.
1654 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1655 int (*func
)(struct rt6_info
*, void *arg
),
1656 bool prune
, int sernum
, void *arg
)
1658 struct fib6_cleaner c
;
1661 c
.w
.func
= fib6_clean_node
;
1673 static void __fib6_clean_all(struct net
*net
,
1674 int (*func
)(struct rt6_info
*, void *),
1675 int sernum
, void *arg
)
1677 struct fib6_table
*table
;
1678 struct hlist_head
*head
;
1682 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1683 head
= &net
->ipv6
.fib_table_hash
[h
];
1684 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1685 write_lock_bh(&table
->tb6_lock
);
1686 fib6_clean_tree(net
, &table
->tb6_root
,
1687 func
, false, sernum
, arg
);
1688 write_unlock_bh(&table
->tb6_lock
);
1694 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *),
1697 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
);
1700 static int fib6_prune_clone(struct rt6_info
*rt
, void *arg
)
1702 if (rt
->rt6i_flags
& RTF_CACHE
) {
1703 RT6_TRACE("pruning clone %p\n", rt
);
1710 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
)
1712 fib6_clean_tree(net
, fn
, fib6_prune_clone
, true,
1713 FIB6_NO_SERNUM_CHANGE
, NULL
);
1716 static void fib6_flush_trees(struct net
*net
)
1718 int new_sernum
= fib6_new_sernum(net
);
1720 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
);
1724 * Garbage collection
1727 static struct fib6_gc_args
1733 static int fib6_age(struct rt6_info
*rt
, void *arg
)
1735 unsigned long now
= jiffies
;
1738 * check addrconf expiration here.
1739 * Routes are expired even if they are in use.
1741 * Also age clones. Note, that clones are aged out
1742 * only if they are not in use now.
1745 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
1746 if (time_after(now
, rt
->dst
.expires
)) {
1747 RT6_TRACE("expiring %p\n", rt
);
1751 } else if (rt
->rt6i_flags
& RTF_CACHE
) {
1752 if (atomic_read(&rt
->dst
.__refcnt
) == 0 &&
1753 time_after_eq(now
, rt
->dst
.lastuse
+ gc_args
.timeout
)) {
1754 RT6_TRACE("aging clone %p\n", rt
);
1756 } else if (rt
->rt6i_flags
& RTF_GATEWAY
) {
1757 struct neighbour
*neigh
;
1758 __u8 neigh_flags
= 0;
1760 neigh
= dst_neigh_lookup(&rt
->dst
, &rt
->rt6i_gateway
);
1762 neigh_flags
= neigh
->flags
;
1763 neigh_release(neigh
);
1765 if (!(neigh_flags
& NTF_ROUTER
)) {
1766 RT6_TRACE("purging route %p via non-router but gateway\n",
1777 static DEFINE_SPINLOCK(fib6_gc_lock
);
1779 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
1784 spin_lock_bh(&fib6_gc_lock
);
1785 } else if (!spin_trylock_bh(&fib6_gc_lock
)) {
1786 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
1789 gc_args
.timeout
= expires
? (int)expires
:
1790 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
1792 gc_args
.more
= icmp6_dst_gc();
1794 fib6_clean_all(net
, fib6_age
, NULL
);
1796 net
->ipv6
.ip6_rt_last_gc
= now
;
1799 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1801 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
1803 del_timer(&net
->ipv6
.ip6_fib_timer
);
1804 spin_unlock_bh(&fib6_gc_lock
);
1807 static void fib6_gc_timer_cb(unsigned long arg
)
1809 fib6_run_gc(0, (struct net
*)arg
, true);
1812 static int __net_init
fib6_net_init(struct net
*net
)
1814 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
1816 setup_timer(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, (unsigned long)net
);
1818 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
1819 if (!net
->ipv6
.rt6_stats
)
1822 /* Avoid false sharing : Use at least a full cache line */
1823 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
1825 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
1826 if (!net
->ipv6
.fib_table_hash
)
1829 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
1831 if (!net
->ipv6
.fib6_main_tbl
)
1832 goto out_fib_table_hash
;
1834 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
1835 net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1836 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
1837 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1838 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1840 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1841 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
1843 if (!net
->ipv6
.fib6_local_tbl
)
1844 goto out_fib6_main_tbl
;
1845 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
1846 net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1847 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
1848 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1849 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1851 fib6_tables_init(net
);
1855 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1857 kfree(net
->ipv6
.fib6_main_tbl
);
1860 kfree(net
->ipv6
.fib_table_hash
);
1862 kfree(net
->ipv6
.rt6_stats
);
1867 static void fib6_net_exit(struct net
*net
)
1869 rt6_ifdown(net
, NULL
);
1870 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
1872 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1873 inetpeer_invalidate_tree(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1874 kfree(net
->ipv6
.fib6_local_tbl
);
1876 inetpeer_invalidate_tree(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1877 kfree(net
->ipv6
.fib6_main_tbl
);
1878 kfree(net
->ipv6
.fib_table_hash
);
1879 kfree(net
->ipv6
.rt6_stats
);
1882 static struct pernet_operations fib6_net_ops
= {
1883 .init
= fib6_net_init
,
1884 .exit
= fib6_net_exit
,
1887 int __init
fib6_init(void)
1891 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
1892 sizeof(struct fib6_node
),
1893 0, SLAB_HWCACHE_ALIGN
,
1895 if (!fib6_node_kmem
)
1898 ret
= register_pernet_subsys(&fib6_net_ops
);
1900 goto out_kmem_cache_create
;
1902 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
1905 goto out_unregister_subsys
;
1907 __fib6_flush_trees
= fib6_flush_trees
;
1911 out_unregister_subsys
:
1912 unregister_pernet_subsys(&fib6_net_ops
);
1913 out_kmem_cache_create
:
1914 kmem_cache_destroy(fib6_node_kmem
);
1918 void fib6_gc_cleanup(void)
1920 unregister_pernet_subsys(&fib6_net_ops
);
1921 kmem_cache_destroy(fib6_node_kmem
);
1924 #ifdef CONFIG_PROC_FS
1926 struct ipv6_route_iter
{
1927 struct seq_net_private p
;
1928 struct fib6_walker w
;
1930 struct fib6_table
*tbl
;
1934 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
1936 struct rt6_info
*rt
= v
;
1937 struct ipv6_route_iter
*iter
= seq
->private;
1939 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
1941 #ifdef CONFIG_IPV6_SUBTREES
1942 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
1944 seq_puts(seq
, "00000000000000000000000000000000 00 ");
1946 if (rt
->rt6i_flags
& RTF_GATEWAY
)
1947 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
1949 seq_puts(seq
, "00000000000000000000000000000000");
1951 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
1952 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
1953 rt
->dst
.__use
, rt
->rt6i_flags
,
1954 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
1955 iter
->w
.leaf
= NULL
;
1959 static int ipv6_route_yield(struct fib6_walker
*w
)
1961 struct ipv6_route_iter
*iter
= w
->args
;
1967 iter
->w
.leaf
= iter
->w
.leaf
->dst
.rt6_next
;
1969 if (!iter
->skip
&& iter
->w
.leaf
)
1971 } while (iter
->w
.leaf
);
1976 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
)
1978 memset(&iter
->w
, 0, sizeof(iter
->w
));
1979 iter
->w
.func
= ipv6_route_yield
;
1980 iter
->w
.root
= &iter
->tbl
->tb6_root
;
1981 iter
->w
.state
= FWS_INIT
;
1982 iter
->w
.node
= iter
->w
.root
;
1983 iter
->w
.args
= iter
;
1984 iter
->sernum
= iter
->w
.root
->fn_sernum
;
1985 INIT_LIST_HEAD(&iter
->w
.lh
);
1986 fib6_walker_link(&iter
->w
);
1989 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
1993 struct hlist_node
*node
;
1996 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
1997 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
2003 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
2004 node
= rcu_dereference_bh(
2005 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
2007 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
2010 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
2012 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
2013 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2014 iter
->w
.state
= FWS_INIT
;
2015 iter
->w
.node
= iter
->w
.root
;
2016 WARN_ON(iter
->w
.skip
);
2017 iter
->w
.skip
= iter
->w
.count
;
2021 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2025 struct net
*net
= seq_file_net(seq
);
2026 struct ipv6_route_iter
*iter
= seq
->private;
2031 n
= ((struct rt6_info
*)v
)->dst
.rt6_next
;
2038 ipv6_route_check_sernum(iter
);
2039 read_lock(&iter
->tbl
->tb6_lock
);
2040 r
= fib6_walk_continue(&iter
->w
);
2041 read_unlock(&iter
->tbl
->tb6_lock
);
2045 return iter
->w
.leaf
;
2047 fib6_walker_unlink(&iter
->w
);
2050 fib6_walker_unlink(&iter
->w
);
2052 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2056 ipv6_route_seq_setup_walk(iter
);
2060 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2063 struct net
*net
= seq_file_net(seq
);
2064 struct ipv6_route_iter
*iter
= seq
->private;
2067 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2071 ipv6_route_seq_setup_walk(iter
);
2072 return ipv6_route_seq_next(seq
, NULL
, pos
);
2078 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2080 struct fib6_walker
*w
= &iter
->w
;
2081 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2084 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2087 struct ipv6_route_iter
*iter
= seq
->private;
2089 if (ipv6_route_iter_active(iter
))
2090 fib6_walker_unlink(&iter
->w
);
2092 rcu_read_unlock_bh();
2095 static const struct seq_operations ipv6_route_seq_ops
= {
2096 .start
= ipv6_route_seq_start
,
2097 .next
= ipv6_route_seq_next
,
2098 .stop
= ipv6_route_seq_stop
,
2099 .show
= ipv6_route_seq_show
2102 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2104 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2105 sizeof(struct ipv6_route_iter
));
2108 #endif /* CONFIG_PROC_FS */