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_free_pcpu(struct rt6_info
*non_pcpu_rt
)
162 if (!non_pcpu_rt
->rt6i_pcpu
)
165 for_each_possible_cpu(cpu
) {
166 struct rt6_info
**ppcpu_rt
;
167 struct rt6_info
*pcpu_rt
;
169 ppcpu_rt
= per_cpu_ptr(non_pcpu_rt
->rt6i_pcpu
, cpu
);
172 dst_free(&pcpu_rt
->dst
);
177 non_pcpu_rt
->rt6i_pcpu
= NULL
;
180 static void rt6_release(struct rt6_info
*rt
)
182 if (atomic_dec_and_test(&rt
->rt6i_ref
)) {
188 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
193 * Initialize table lock at a single place to give lockdep a key,
194 * tables aren't visible prior to being linked to the list.
196 rwlock_init(&tb
->tb6_lock
);
198 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
201 * No protection necessary, this is the only list mutatation
202 * operation, tables never disappear once they exist.
204 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
207 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
209 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
211 struct fib6_table
*table
;
213 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
216 table
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
217 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
218 inet_peer_base_init(&table
->tb6_peers
);
224 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
226 struct fib6_table
*tb
;
230 tb
= fib6_get_table(net
, id
);
234 tb
= fib6_alloc_table(net
, id
);
236 fib6_link_table(net
, tb
);
241 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
243 struct fib6_table
*tb
;
244 struct hlist_head
*head
;
249 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
251 head
= &net
->ipv6
.fib_table_hash
[h
];
252 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
253 if (tb
->tb6_id
== id
) {
263 static void __net_init
fib6_tables_init(struct net
*net
)
265 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
266 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
270 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
272 return fib6_get_table(net
, id
);
275 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
277 return net
->ipv6
.fib6_main_tbl
;
280 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
281 int flags
, pol_lookup_t lookup
)
283 return (struct dst_entry
*) lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
286 static void __net_init
fib6_tables_init(struct net
*net
)
288 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
293 static int fib6_dump_node(struct fib6_walker
*w
)
298 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
299 res
= rt6_dump_route(rt
, w
->args
);
301 /* Frame is full, suspend walking */
310 static void fib6_dump_end(struct netlink_callback
*cb
)
312 struct fib6_walker
*w
= (void *)cb
->args
[2];
317 fib6_walker_unlink(w
);
322 cb
->done
= (void *)cb
->args
[3];
326 static int fib6_dump_done(struct netlink_callback
*cb
)
329 return cb
->done
? cb
->done(cb
) : 0;
332 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
333 struct netlink_callback
*cb
)
335 struct fib6_walker
*w
;
338 w
= (void *)cb
->args
[2];
339 w
->root
= &table
->tb6_root
;
341 if (cb
->args
[4] == 0) {
345 read_lock_bh(&table
->tb6_lock
);
347 read_unlock_bh(&table
->tb6_lock
);
350 cb
->args
[5] = w
->root
->fn_sernum
;
353 if (cb
->args
[5] != w
->root
->fn_sernum
) {
354 /* Begin at the root if the tree changed */
355 cb
->args
[5] = w
->root
->fn_sernum
;
362 read_lock_bh(&table
->tb6_lock
);
363 res
= fib6_walk_continue(w
);
364 read_unlock_bh(&table
->tb6_lock
);
366 fib6_walker_unlink(w
);
374 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
376 struct net
*net
= sock_net(skb
->sk
);
378 unsigned int e
= 0, s_e
;
379 struct rt6_rtnl_dump_arg arg
;
380 struct fib6_walker
*w
;
381 struct fib6_table
*tb
;
382 struct hlist_head
*head
;
388 w
= (void *)cb
->args
[2];
392 * 1. hook callback destructor.
394 cb
->args
[3] = (long)cb
->done
;
395 cb
->done
= fib6_dump_done
;
398 * 2. allocate and initialize walker.
400 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
403 w
->func
= fib6_dump_node
;
404 cb
->args
[2] = (long)w
;
413 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
415 head
= &net
->ipv6
.fib_table_hash
[h
];
416 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
419 res
= fib6_dump_table(tb
, skb
, cb
);
431 res
= res
< 0 ? res
: skb
->len
;
440 * return the appropriate node for a routing tree "add" operation
441 * by either creating and inserting or by returning an existing
445 static struct fib6_node
*fib6_add_1(struct fib6_node
*root
,
446 struct in6_addr
*addr
, int plen
,
447 int offset
, int allow_create
,
448 int replace_required
, int sernum
)
450 struct fib6_node
*fn
, *in
, *ln
;
451 struct fib6_node
*pn
= NULL
;
456 RT6_TRACE("fib6_add_1\n");
458 /* insert node in tree */
463 key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
468 if (plen
< fn
->fn_bit
||
469 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
471 if (replace_required
) {
472 pr_warn("Can't replace route, no match found\n");
473 return ERR_PTR(-ENOENT
);
475 pr_warn("NLM_F_CREATE should be set when creating new route\n");
484 if (plen
== fn
->fn_bit
) {
485 /* clean up an intermediate node */
486 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
487 rt6_release(fn
->leaf
);
491 fn
->fn_sernum
= sernum
;
497 * We have more bits to go
500 /* Try to walk down on tree. */
501 fn
->fn_sernum
= sernum
;
502 dir
= addr_bit_set(addr
, fn
->fn_bit
);
504 fn
= dir
? fn
->right
: fn
->left
;
508 /* We should not create new node because
509 * NLM_F_REPLACE was specified without NLM_F_CREATE
510 * I assume it is safe to require NLM_F_CREATE when
511 * REPLACE flag is used! Later we may want to remove the
512 * check for replace_required, because according
513 * to netlink specification, NLM_F_CREATE
514 * MUST be specified if new route is created.
515 * That would keep IPv6 consistent with IPv4
517 if (replace_required
) {
518 pr_warn("Can't replace route, no match found\n");
519 return ERR_PTR(-ENOENT
);
521 pr_warn("NLM_F_CREATE should be set when creating new route\n");
524 * We walked to the bottom of tree.
525 * Create new leaf node without children.
531 return ERR_PTR(-ENOMEM
);
535 ln
->fn_sernum
= sernum
;
547 * split since we don't have a common prefix anymore or
548 * we have a less significant route.
549 * we've to insert an intermediate node on the list
550 * this new node will point to the one we need to create
556 /* find 1st bit in difference between the 2 addrs.
558 See comment in __ipv6_addr_diff: bit may be an invalid value,
559 but if it is >= plen, the value is ignored in any case.
562 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
567 * (new leaf node)[ln] (old node)[fn]
578 return ERR_PTR(-ENOMEM
);
582 * new intermediate node.
584 * be off since that an address that chooses one of
585 * the branches would not match less specific routes
586 * in the other branch
593 atomic_inc(&in
->leaf
->rt6i_ref
);
595 in
->fn_sernum
= sernum
;
597 /* update parent pointer */
608 ln
->fn_sernum
= sernum
;
610 if (addr_bit_set(addr
, bit
)) {
617 } else { /* plen <= bit */
620 * (new leaf node)[ln]
622 * (old node)[fn] NULL
628 return ERR_PTR(-ENOMEM
);
634 ln
->fn_sernum
= sernum
;
641 if (addr_bit_set(&key
->addr
, plen
))
651 static bool rt6_qualify_for_ecmp(struct rt6_info
*rt
)
653 return (rt
->rt6i_flags
& (RTF_GATEWAY
|RTF_ADDRCONF
|RTF_DYNAMIC
)) ==
657 static void fib6_copy_metrics(u32
*mp
, const struct mx6_config
*mxc
)
661 for (i
= 0; i
< RTAX_MAX
; i
++) {
662 if (test_bit(i
, mxc
->mx_valid
))
667 static int fib6_commit_metrics(struct dst_entry
*dst
, struct mx6_config
*mxc
)
672 if (dst
->flags
& DST_HOST
) {
673 u32
*mp
= dst_metrics_write_ptr(dst
);
678 fib6_copy_metrics(mp
, mxc
);
680 dst_init_metrics(dst
, mxc
->mx
, false);
682 /* We've stolen mx now. */
689 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
692 if (atomic_read(&rt
->rt6i_ref
) != 1) {
693 /* This route is used as dummy address holder in some split
694 * nodes. It is not leaked, but it still holds other resources,
695 * which must be released in time. So, scan ascendant nodes
696 * and replace dummy references to this route with references
697 * to still alive ones.
700 if (!(fn
->fn_flags
& RTN_RTINFO
) && fn
->leaf
== rt
) {
701 fn
->leaf
= fib6_find_prefix(net
, fn
);
702 atomic_inc(&fn
->leaf
->rt6i_ref
);
707 /* No more references are possible at this point. */
708 BUG_ON(atomic_read(&rt
->rt6i_ref
) != 1);
713 * Insert routing information in a node.
716 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
717 struct nl_info
*info
, struct mx6_config
*mxc
)
719 struct rt6_info
*iter
= NULL
;
720 struct rt6_info
**ins
;
721 struct rt6_info
**fallback_ins
= NULL
;
722 int replace
= (info
->nlh
&&
723 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
724 int add
= (!info
->nlh
||
725 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
727 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
732 for (iter
= fn
->leaf
; iter
; iter
= iter
->dst
.rt6_next
) {
734 * Search for duplicates
737 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
739 * Same priority level
742 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
745 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
750 fallback_ins
= fallback_ins
?: ins
;
754 if (iter
->dst
.dev
== rt
->dst
.dev
&&
755 iter
->rt6i_idev
== rt
->rt6i_idev
&&
756 ipv6_addr_equal(&iter
->rt6i_gateway
,
757 &rt
->rt6i_gateway
)) {
758 if (rt
->rt6i_nsiblings
)
759 rt
->rt6i_nsiblings
= 0;
760 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
762 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
763 rt6_clean_expires(iter
);
765 rt6_set_expires(iter
, rt
->dst
.expires
);
766 iter
->rt6i_pmtu
= rt
->rt6i_pmtu
;
769 /* If we have the same destination and the same metric,
770 * but not the same gateway, then the route we try to
771 * add is sibling to this route, increment our counter
772 * of siblings, and later we will add our route to the
774 * Only static routes (which don't have flag
775 * RTF_EXPIRES) are used for ECMPv6.
777 * To avoid long list, we only had siblings if the
778 * route have a gateway.
781 rt6_qualify_for_ecmp(iter
))
782 rt
->rt6i_nsiblings
++;
785 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
789 ins
= &iter
->dst
.rt6_next
;
792 if (fallback_ins
&& !found
) {
793 /* No ECMP-able route found, replace first non-ECMP one */
799 /* Reset round-robin state, if necessary */
800 if (ins
== &fn
->leaf
)
803 /* Link this route to others same route. */
804 if (rt
->rt6i_nsiblings
) {
805 unsigned int rt6i_nsiblings
;
806 struct rt6_info
*sibling
, *temp_sibling
;
808 /* Find the first route that have the same metric */
811 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
812 rt6_qualify_for_ecmp(sibling
)) {
813 list_add_tail(&rt
->rt6i_siblings
,
814 &sibling
->rt6i_siblings
);
817 sibling
= sibling
->dst
.rt6_next
;
819 /* For each sibling in the list, increment the counter of
820 * siblings. BUG() if counters does not match, list of siblings
824 list_for_each_entry_safe(sibling
, temp_sibling
,
825 &rt
->rt6i_siblings
, rt6i_siblings
) {
826 sibling
->rt6i_nsiblings
++;
827 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
830 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
838 pr_warn("NLM_F_CREATE should be set when creating new route\n");
841 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
845 rt
->dst
.rt6_next
= iter
;
848 atomic_inc(&rt
->rt6i_ref
);
849 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
);
850 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
852 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
853 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
854 fn
->fn_flags
|= RTN_RTINFO
;
863 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
867 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
873 rt
->dst
.rt6_next
= iter
->dst
.rt6_next
;
874 atomic_inc(&rt
->rt6i_ref
);
875 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
);
876 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
877 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
878 fn
->fn_flags
|= RTN_RTINFO
;
880 nsiblings
= iter
->rt6i_nsiblings
;
881 fib6_purge_rt(iter
, fn
, info
->nl_net
);
885 /* Replacing an ECMP route, remove all siblings */
886 ins
= &rt
->dst
.rt6_next
;
889 if (rt6_qualify_for_ecmp(iter
)) {
890 *ins
= iter
->dst
.rt6_next
;
891 fib6_purge_rt(iter
, fn
, info
->nl_net
);
895 ins
= &iter
->dst
.rt6_next
;
899 WARN_ON(nsiblings
!= 0);
906 static void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
908 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
909 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
910 mod_timer(&net
->ipv6
.ip6_fib_timer
,
911 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
914 void fib6_force_start_gc(struct net
*net
)
916 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
917 mod_timer(&net
->ipv6
.ip6_fib_timer
,
918 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
922 * Add routing information to the routing tree.
923 * <destination addr>/<source addr>
924 * with source addr info in sub-trees
927 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
928 struct nl_info
*info
, struct mx6_config
*mxc
)
930 struct fib6_node
*fn
, *pn
= NULL
;
932 int allow_create
= 1;
933 int replace_required
= 0;
934 int sernum
= fib6_new_sernum(info
->nl_net
);
937 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
939 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
940 replace_required
= 1;
942 if (!allow_create
&& !replace_required
)
943 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
945 fn
= fib6_add_1(root
, &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
,
946 offsetof(struct rt6_info
, rt6i_dst
), allow_create
,
947 replace_required
, sernum
);
956 #ifdef CONFIG_IPV6_SUBTREES
957 if (rt
->rt6i_src
.plen
) {
958 struct fib6_node
*sn
;
961 struct fib6_node
*sfn
;
973 /* Create subtree root node */
978 sfn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
979 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
980 sfn
->fn_flags
= RTN_ROOT
;
981 sfn
->fn_sernum
= sernum
;
983 /* Now add the first leaf node to new subtree */
985 sn
= fib6_add_1(sfn
, &rt
->rt6i_src
.addr
,
987 offsetof(struct rt6_info
, rt6i_src
),
988 allow_create
, replace_required
, sernum
);
991 /* If it is failed, discard just allocated
992 root, and then (in st_failure) stale node
1000 /* Now link new subtree to main tree */
1004 sn
= fib6_add_1(fn
->subtree
, &rt
->rt6i_src
.addr
,
1006 offsetof(struct rt6_info
, rt6i_src
),
1007 allow_create
, replace_required
, sernum
);
1017 atomic_inc(&rt
->rt6i_ref
);
1023 err
= fib6_add_rt2node(fn
, rt
, info
, mxc
);
1025 fib6_start_gc(info
->nl_net
, rt
);
1026 if (!(rt
->rt6i_flags
& RTF_CACHE
))
1027 fib6_prune_clones(info
->nl_net
, pn
);
1032 #ifdef CONFIG_IPV6_SUBTREES
1034 * If fib6_add_1 has cleared the old leaf pointer in the
1035 * super-tree leaf node we have to find a new one for it.
1037 if (pn
!= fn
&& pn
->leaf
== rt
) {
1039 atomic_dec(&rt
->rt6i_ref
);
1041 if (pn
!= fn
&& !pn
->leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
1042 pn
->leaf
= fib6_find_prefix(info
->nl_net
, pn
);
1045 WARN_ON(pn
->leaf
== NULL
);
1046 pn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
1049 atomic_inc(&pn
->leaf
->rt6i_ref
);
1056 #ifdef CONFIG_IPV6_SUBTREES
1057 /* Subtree creation failed, probably main tree node
1058 is orphan. If it is, shoot it.
1061 if (fn
&& !(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)))
1062 fib6_repair_tree(info
->nl_net
, fn
);
1069 * Routing tree lookup
1073 struct lookup_args
{
1074 int offset
; /* key offset on rt6_info */
1075 const struct in6_addr
*addr
; /* search key */
1078 static struct fib6_node
*fib6_lookup_1(struct fib6_node
*root
,
1079 struct lookup_args
*args
)
1081 struct fib6_node
*fn
;
1084 if (unlikely(args
->offset
== 0))
1094 struct fib6_node
*next
;
1096 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1098 next
= dir
? fn
->right
: fn
->left
;
1108 if (FIB6_SUBTREE(fn
) || fn
->fn_flags
& RTN_RTINFO
) {
1111 key
= (struct rt6key
*) ((u8
*) fn
->leaf
+
1114 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1115 #ifdef CONFIG_IPV6_SUBTREES
1117 struct fib6_node
*sfn
;
1118 sfn
= fib6_lookup_1(fn
->subtree
,
1125 if (fn
->fn_flags
& RTN_RTINFO
)
1129 #ifdef CONFIG_IPV6_SUBTREES
1132 if (fn
->fn_flags
& RTN_ROOT
)
1141 struct fib6_node
*fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1142 const struct in6_addr
*saddr
)
1144 struct fib6_node
*fn
;
1145 struct lookup_args args
[] = {
1147 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1150 #ifdef CONFIG_IPV6_SUBTREES
1152 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1157 .offset
= 0, /* sentinel */
1161 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1162 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1169 * Get node with specified destination prefix (and source prefix,
1170 * if subtrees are used)
1174 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1175 const struct in6_addr
*addr
,
1176 int plen
, int offset
)
1178 struct fib6_node
*fn
;
1180 for (fn
= root
; fn
; ) {
1181 struct rt6key
*key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
1186 if (plen
< fn
->fn_bit
||
1187 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1190 if (plen
== fn
->fn_bit
)
1194 * We have more bits to go
1196 if (addr_bit_set(addr
, fn
->fn_bit
))
1204 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1205 const struct in6_addr
*daddr
, int dst_len
,
1206 const struct in6_addr
*saddr
, int src_len
)
1208 struct fib6_node
*fn
;
1210 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1211 offsetof(struct rt6_info
, rt6i_dst
));
1213 #ifdef CONFIG_IPV6_SUBTREES
1215 WARN_ON(saddr
== NULL
);
1216 if (fn
&& fn
->subtree
)
1217 fn
= fib6_locate_1(fn
->subtree
, saddr
, src_len
,
1218 offsetof(struct rt6_info
, rt6i_src
));
1222 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1234 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
)
1236 if (fn
->fn_flags
& RTN_ROOT
)
1237 return net
->ipv6
.ip6_null_entry
;
1241 return fn
->left
->leaf
;
1243 return fn
->right
->leaf
;
1245 fn
= FIB6_SUBTREE(fn
);
1251 * Called to trim the tree of intermediate nodes when possible. "fn"
1252 * is the node we want to try and remove.
1255 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1256 struct fib6_node
*fn
)
1260 struct fib6_node
*child
, *pn
;
1261 struct fib6_walker
*w
;
1265 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1268 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1269 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1275 child
= fn
->right
, children
|= 1;
1277 child
= fn
->left
, children
|= 2;
1279 if (children
== 3 || FIB6_SUBTREE(fn
)
1280 #ifdef CONFIG_IPV6_SUBTREES
1281 /* Subtree root (i.e. fn) may have one child */
1282 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1285 fn
->leaf
= fib6_find_prefix(net
, fn
);
1289 fn
->leaf
= net
->ipv6
.ip6_null_entry
;
1292 atomic_inc(&fn
->leaf
->rt6i_ref
);
1297 #ifdef CONFIG_IPV6_SUBTREES
1298 if (FIB6_SUBTREE(pn
) == fn
) {
1299 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1300 FIB6_SUBTREE(pn
) = NULL
;
1303 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1305 if (pn
->right
== fn
)
1307 else if (pn
->left
== fn
)
1316 #ifdef CONFIG_IPV6_SUBTREES
1320 read_lock(&fib6_walker_lock
);
1323 if (w
->root
== fn
) {
1324 w
->root
= w
->node
= NULL
;
1325 RT6_TRACE("W %p adjusted by delroot 1\n", w
);
1326 } else if (w
->node
== fn
) {
1327 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1332 if (w
->root
== fn
) {
1334 RT6_TRACE("W %p adjusted by delroot 2\n", w
);
1336 if (w
->node
== fn
) {
1339 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1340 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1342 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1343 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1348 read_unlock(&fib6_walker_lock
);
1351 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1354 rt6_release(pn
->leaf
);
1360 static void fib6_del_route(struct fib6_node
*fn
, struct rt6_info
**rtp
,
1361 struct nl_info
*info
)
1363 struct fib6_walker
*w
;
1364 struct rt6_info
*rt
= *rtp
;
1365 struct net
*net
= info
->nl_net
;
1367 RT6_TRACE("fib6_del_route\n");
1370 *rtp
= rt
->dst
.rt6_next
;
1371 rt
->rt6i_node
= NULL
;
1372 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1373 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1375 /* Reset round-robin state, if necessary */
1376 if (fn
->rr_ptr
== rt
)
1379 /* Remove this entry from other siblings */
1380 if (rt
->rt6i_nsiblings
) {
1381 struct rt6_info
*sibling
, *next_sibling
;
1383 list_for_each_entry_safe(sibling
, next_sibling
,
1384 &rt
->rt6i_siblings
, rt6i_siblings
)
1385 sibling
->rt6i_nsiblings
--;
1386 rt
->rt6i_nsiblings
= 0;
1387 list_del_init(&rt
->rt6i_siblings
);
1390 /* Adjust walkers */
1391 read_lock(&fib6_walker_lock
);
1393 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1394 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1395 w
->leaf
= rt
->dst
.rt6_next
;
1400 read_unlock(&fib6_walker_lock
);
1402 rt
->dst
.rt6_next
= NULL
;
1404 /* If it was last route, expunge its radix tree node */
1406 fn
->fn_flags
&= ~RTN_RTINFO
;
1407 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1408 fn
= fib6_repair_tree(net
, fn
);
1411 fib6_purge_rt(rt
, fn
, net
);
1413 inet6_rt_notify(RTM_DELROUTE
, rt
, info
);
1417 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1419 struct net
*net
= info
->nl_net
;
1420 struct fib6_node
*fn
= rt
->rt6i_node
;
1421 struct rt6_info
**rtp
;
1424 if (rt
->dst
.obsolete
> 0) {
1429 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1432 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1434 if (!(rt
->rt6i_flags
& RTF_CACHE
)) {
1435 struct fib6_node
*pn
= fn
;
1436 #ifdef CONFIG_IPV6_SUBTREES
1437 /* clones of this route might be in another subtree */
1438 if (rt
->rt6i_src
.plen
) {
1439 while (!(pn
->fn_flags
& RTN_ROOT
))
1444 fib6_prune_clones(info
->nl_net
, pn
);
1448 * Walk the leaf entries looking for ourself
1451 for (rtp
= &fn
->leaf
; *rtp
; rtp
= &(*rtp
)->dst
.rt6_next
) {
1453 fib6_del_route(fn
, rtp
, info
);
1461 * Tree traversal function.
1463 * Certainly, it is not interrupt safe.
1464 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1465 * It means, that we can modify tree during walking
1466 * and use this function for garbage collection, clone pruning,
1467 * cleaning tree when a device goes down etc. etc.
1469 * It guarantees that every node will be traversed,
1470 * and that it will be traversed only once.
1472 * Callback function w->func may return:
1473 * 0 -> continue walking.
1474 * positive value -> walking is suspended (used by tree dumps,
1475 * and probably by gc, if it will be split to several slices)
1476 * negative value -> terminate walking.
1478 * The function itself returns:
1479 * 0 -> walk is complete.
1480 * >0 -> walk is incomplete (i.e. suspended)
1481 * <0 -> walk is terminated by an error.
1484 static int fib6_walk_continue(struct fib6_walker
*w
)
1486 struct fib6_node
*fn
, *pn
;
1493 if (w
->prune
&& fn
!= w
->root
&&
1494 fn
->fn_flags
& RTN_RTINFO
&& w
->state
< FWS_C
) {
1499 #ifdef CONFIG_IPV6_SUBTREES
1501 if (FIB6_SUBTREE(fn
)) {
1502 w
->node
= FIB6_SUBTREE(fn
);
1510 w
->state
= FWS_INIT
;
1516 w
->node
= fn
->right
;
1517 w
->state
= FWS_INIT
;
1523 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1545 #ifdef CONFIG_IPV6_SUBTREES
1546 if (FIB6_SUBTREE(pn
) == fn
) {
1547 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1552 if (pn
->left
== fn
) {
1556 if (pn
->right
== fn
) {
1558 w
->leaf
= w
->node
->leaf
;
1568 static int fib6_walk(struct fib6_walker
*w
)
1572 w
->state
= FWS_INIT
;
1575 fib6_walker_link(w
);
1576 res
= fib6_walk_continue(w
);
1578 fib6_walker_unlink(w
);
1582 static int fib6_clean_node(struct fib6_walker
*w
)
1585 struct rt6_info
*rt
;
1586 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1587 struct nl_info info
= {
1591 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1592 w
->node
->fn_sernum
!= c
->sernum
)
1593 w
->node
->fn_sernum
= c
->sernum
;
1596 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1601 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1602 res
= c
->func(rt
, c
->arg
);
1605 res
= fib6_del(rt
, &info
);
1608 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1609 __func__
, rt
, rt
->rt6i_node
, res
);
1622 * Convenient frontend to tree walker.
1624 * func is called on each route.
1625 * It may return -1 -> delete this route.
1626 * 0 -> continue walking
1628 * prune==1 -> only immediate children of node (certainly,
1629 * ignoring pure split nodes) will be scanned.
1632 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1633 int (*func
)(struct rt6_info
*, void *arg
),
1634 bool prune
, int sernum
, void *arg
)
1636 struct fib6_cleaner c
;
1639 c
.w
.func
= fib6_clean_node
;
1651 static void __fib6_clean_all(struct net
*net
,
1652 int (*func
)(struct rt6_info
*, void *),
1653 int sernum
, void *arg
)
1655 struct fib6_table
*table
;
1656 struct hlist_head
*head
;
1660 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1661 head
= &net
->ipv6
.fib_table_hash
[h
];
1662 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1663 write_lock_bh(&table
->tb6_lock
);
1664 fib6_clean_tree(net
, &table
->tb6_root
,
1665 func
, false, sernum
, arg
);
1666 write_unlock_bh(&table
->tb6_lock
);
1672 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *),
1675 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
);
1678 static int fib6_prune_clone(struct rt6_info
*rt
, void *arg
)
1680 if (rt
->rt6i_flags
& RTF_CACHE
) {
1681 RT6_TRACE("pruning clone %p\n", rt
);
1688 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
)
1690 fib6_clean_tree(net
, fn
, fib6_prune_clone
, true,
1691 FIB6_NO_SERNUM_CHANGE
, NULL
);
1694 static void fib6_flush_trees(struct net
*net
)
1696 int new_sernum
= fib6_new_sernum(net
);
1698 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
);
1702 * Garbage collection
1705 static struct fib6_gc_args
1711 static int fib6_age(struct rt6_info
*rt
, void *arg
)
1713 unsigned long now
= jiffies
;
1716 * check addrconf expiration here.
1717 * Routes are expired even if they are in use.
1719 * Also age clones. Note, that clones are aged out
1720 * only if they are not in use now.
1723 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
1724 if (time_after(now
, rt
->dst
.expires
)) {
1725 RT6_TRACE("expiring %p\n", rt
);
1729 } else if (rt
->rt6i_flags
& RTF_CACHE
) {
1730 if (atomic_read(&rt
->dst
.__refcnt
) == 0 &&
1731 time_after_eq(now
, rt
->dst
.lastuse
+ gc_args
.timeout
)) {
1732 RT6_TRACE("aging clone %p\n", rt
);
1734 } else if (rt
->rt6i_flags
& RTF_GATEWAY
) {
1735 struct neighbour
*neigh
;
1736 __u8 neigh_flags
= 0;
1738 neigh
= dst_neigh_lookup(&rt
->dst
, &rt
->rt6i_gateway
);
1740 neigh_flags
= neigh
->flags
;
1741 neigh_release(neigh
);
1743 if (!(neigh_flags
& NTF_ROUTER
)) {
1744 RT6_TRACE("purging route %p via non-router but gateway\n",
1755 static DEFINE_SPINLOCK(fib6_gc_lock
);
1757 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
1762 spin_lock_bh(&fib6_gc_lock
);
1763 } else if (!spin_trylock_bh(&fib6_gc_lock
)) {
1764 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
1767 gc_args
.timeout
= expires
? (int)expires
:
1768 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
1770 gc_args
.more
= icmp6_dst_gc();
1772 fib6_clean_all(net
, fib6_age
, NULL
);
1774 net
->ipv6
.ip6_rt_last_gc
= now
;
1777 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1779 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
1781 del_timer(&net
->ipv6
.ip6_fib_timer
);
1782 spin_unlock_bh(&fib6_gc_lock
);
1785 static void fib6_gc_timer_cb(unsigned long arg
)
1787 fib6_run_gc(0, (struct net
*)arg
, true);
1790 static int __net_init
fib6_net_init(struct net
*net
)
1792 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
1794 setup_timer(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, (unsigned long)net
);
1796 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
1797 if (!net
->ipv6
.rt6_stats
)
1800 /* Avoid false sharing : Use at least a full cache line */
1801 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
1803 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
1804 if (!net
->ipv6
.fib_table_hash
)
1807 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
1809 if (!net
->ipv6
.fib6_main_tbl
)
1810 goto out_fib_table_hash
;
1812 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
1813 net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1814 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
1815 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1816 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1818 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1819 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
1821 if (!net
->ipv6
.fib6_local_tbl
)
1822 goto out_fib6_main_tbl
;
1823 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
1824 net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1825 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
1826 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1827 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1829 fib6_tables_init(net
);
1833 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1835 kfree(net
->ipv6
.fib6_main_tbl
);
1838 kfree(net
->ipv6
.fib_table_hash
);
1840 kfree(net
->ipv6
.rt6_stats
);
1845 static void fib6_net_exit(struct net
*net
)
1847 rt6_ifdown(net
, NULL
);
1848 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
1850 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1851 inetpeer_invalidate_tree(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1852 kfree(net
->ipv6
.fib6_local_tbl
);
1854 inetpeer_invalidate_tree(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1855 kfree(net
->ipv6
.fib6_main_tbl
);
1856 kfree(net
->ipv6
.fib_table_hash
);
1857 kfree(net
->ipv6
.rt6_stats
);
1860 static struct pernet_operations fib6_net_ops
= {
1861 .init
= fib6_net_init
,
1862 .exit
= fib6_net_exit
,
1865 int __init
fib6_init(void)
1869 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
1870 sizeof(struct fib6_node
),
1871 0, SLAB_HWCACHE_ALIGN
,
1873 if (!fib6_node_kmem
)
1876 ret
= register_pernet_subsys(&fib6_net_ops
);
1878 goto out_kmem_cache_create
;
1880 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
1883 goto out_unregister_subsys
;
1885 __fib6_flush_trees
= fib6_flush_trees
;
1889 out_unregister_subsys
:
1890 unregister_pernet_subsys(&fib6_net_ops
);
1891 out_kmem_cache_create
:
1892 kmem_cache_destroy(fib6_node_kmem
);
1896 void fib6_gc_cleanup(void)
1898 unregister_pernet_subsys(&fib6_net_ops
);
1899 kmem_cache_destroy(fib6_node_kmem
);
1902 #ifdef CONFIG_PROC_FS
1904 struct ipv6_route_iter
{
1905 struct seq_net_private p
;
1906 struct fib6_walker w
;
1908 struct fib6_table
*tbl
;
1912 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
1914 struct rt6_info
*rt
= v
;
1915 struct ipv6_route_iter
*iter
= seq
->private;
1917 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
1919 #ifdef CONFIG_IPV6_SUBTREES
1920 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
1922 seq_puts(seq
, "00000000000000000000000000000000 00 ");
1924 if (rt
->rt6i_flags
& RTF_GATEWAY
)
1925 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
1927 seq_puts(seq
, "00000000000000000000000000000000");
1929 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
1930 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
1931 rt
->dst
.__use
, rt
->rt6i_flags
,
1932 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
1933 iter
->w
.leaf
= NULL
;
1937 static int ipv6_route_yield(struct fib6_walker
*w
)
1939 struct ipv6_route_iter
*iter
= w
->args
;
1945 iter
->w
.leaf
= iter
->w
.leaf
->dst
.rt6_next
;
1947 if (!iter
->skip
&& iter
->w
.leaf
)
1949 } while (iter
->w
.leaf
);
1954 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
)
1956 memset(&iter
->w
, 0, sizeof(iter
->w
));
1957 iter
->w
.func
= ipv6_route_yield
;
1958 iter
->w
.root
= &iter
->tbl
->tb6_root
;
1959 iter
->w
.state
= FWS_INIT
;
1960 iter
->w
.node
= iter
->w
.root
;
1961 iter
->w
.args
= iter
;
1962 iter
->sernum
= iter
->w
.root
->fn_sernum
;
1963 INIT_LIST_HEAD(&iter
->w
.lh
);
1964 fib6_walker_link(&iter
->w
);
1967 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
1971 struct hlist_node
*node
;
1974 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
1975 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
1981 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
1982 node
= rcu_dereference_bh(
1983 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
1985 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
1988 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
1990 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
1991 iter
->sernum
= iter
->w
.root
->fn_sernum
;
1992 iter
->w
.state
= FWS_INIT
;
1993 iter
->w
.node
= iter
->w
.root
;
1994 WARN_ON(iter
->w
.skip
);
1995 iter
->w
.skip
= iter
->w
.count
;
1999 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2003 struct net
*net
= seq_file_net(seq
);
2004 struct ipv6_route_iter
*iter
= seq
->private;
2009 n
= ((struct rt6_info
*)v
)->dst
.rt6_next
;
2016 ipv6_route_check_sernum(iter
);
2017 read_lock(&iter
->tbl
->tb6_lock
);
2018 r
= fib6_walk_continue(&iter
->w
);
2019 read_unlock(&iter
->tbl
->tb6_lock
);
2023 return iter
->w
.leaf
;
2025 fib6_walker_unlink(&iter
->w
);
2028 fib6_walker_unlink(&iter
->w
);
2030 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2034 ipv6_route_seq_setup_walk(iter
);
2038 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2041 struct net
*net
= seq_file_net(seq
);
2042 struct ipv6_route_iter
*iter
= seq
->private;
2045 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2049 ipv6_route_seq_setup_walk(iter
);
2050 return ipv6_route_seq_next(seq
, NULL
, pos
);
2056 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2058 struct fib6_walker
*w
= &iter
->w
;
2059 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2062 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2065 struct ipv6_route_iter
*iter
= seq
->private;
2067 if (ipv6_route_iter_active(iter
))
2068 fib6_walker_unlink(&iter
->w
);
2070 rcu_read_unlock_bh();
2073 static const struct seq_operations ipv6_route_seq_ops
= {
2074 .start
= ipv6_route_seq_start
,
2075 .next
= ipv6_route_seq_next
,
2076 .stop
= ipv6_route_seq_stop
,
2077 .show
= ipv6_route_seq_show
2080 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2082 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2083 sizeof(struct ipv6_route_iter
));
2086 #endif /* CONFIG_PROC_FS */