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>
36 #include <net/ip6_fib.h>
37 #include <net/ip6_route.h>
42 #define RT6_TRACE(x...) pr_debug(x)
44 #define RT6_TRACE(x...) do { ; } while (0)
47 static struct kmem_cache
*fib6_node_kmem __read_mostly
;
52 int (*func
)(struct rt6_info
*, void *arg
);
57 static DEFINE_RWLOCK(fib6_walker_lock
);
59 #ifdef CONFIG_IPV6_SUBTREES
60 #define FWS_INIT FWS_S
62 #define FWS_INIT FWS_L
65 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
);
66 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
);
67 static struct fib6_node
*fib6_repair_tree(struct net
*net
, struct fib6_node
*fn
);
68 static int fib6_walk(struct fib6_walker
*w
);
69 static int fib6_walk_continue(struct fib6_walker
*w
);
72 * A routing update causes an increase of the serial number on the
73 * affected subtree. This allows for cached routes to be asynchronously
74 * tested when modifications are made to the destination cache as a
75 * result of redirects, path MTU changes, etc.
78 static void fib6_gc_timer_cb(unsigned long arg
);
80 static LIST_HEAD(fib6_walkers
);
81 #define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
83 static void fib6_walker_link(struct fib6_walker
*w
)
85 write_lock_bh(&fib6_walker_lock
);
86 list_add(&w
->lh
, &fib6_walkers
);
87 write_unlock_bh(&fib6_walker_lock
);
90 static void fib6_walker_unlink(struct fib6_walker
*w
)
92 write_lock_bh(&fib6_walker_lock
);
94 write_unlock_bh(&fib6_walker_lock
);
97 static int fib6_new_sernum(struct net
*net
)
102 old
= atomic_read(&net
->ipv6
.fib6_sernum
);
103 new = old
< INT_MAX
? old
+ 1 : 1;
104 } while (atomic_cmpxchg(&net
->ipv6
.fib6_sernum
,
110 FIB6_NO_SERNUM_CHANGE
= 0,
114 * Auxiliary address test functions for the radix tree.
116 * These assume a 32bit processor (although it will work on
123 #if defined(__LITTLE_ENDIAN)
124 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
126 # define BITOP_BE32_SWIZZLE 0
129 static __be32
addr_bit_set(const void *token
, int fn_bit
)
131 const __be32
*addr
= token
;
134 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
135 * is optimized version of
136 * htonl(1 << ((~fn_bit)&0x1F))
137 * See include/asm-generic/bitops/le.h.
139 return (__force __be32
)(1 << ((~fn_bit
^ BITOP_BE32_SWIZZLE
) & 0x1f)) &
143 static struct fib6_node
*node_alloc(void)
145 struct fib6_node
*fn
;
147 fn
= kmem_cache_zalloc(fib6_node_kmem
, GFP_ATOMIC
);
152 static void node_free(struct fib6_node
*fn
)
154 kmem_cache_free(fib6_node_kmem
, fn
);
157 static void rt6_release(struct rt6_info
*rt
)
159 if (atomic_dec_and_test(&rt
->rt6i_ref
))
163 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
168 * Initialize table lock at a single place to give lockdep a key,
169 * tables aren't visible prior to being linked to the list.
171 rwlock_init(&tb
->tb6_lock
);
173 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
176 * No protection necessary, this is the only list mutatation
177 * operation, tables never disappear once they exist.
179 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
182 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
184 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
186 struct fib6_table
*table
;
188 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
191 table
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
192 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
193 inet_peer_base_init(&table
->tb6_peers
);
199 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
201 struct fib6_table
*tb
;
205 tb
= fib6_get_table(net
, id
);
209 tb
= fib6_alloc_table(net
, id
);
211 fib6_link_table(net
, tb
);
216 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
218 struct fib6_table
*tb
;
219 struct hlist_head
*head
;
224 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
226 head
= &net
->ipv6
.fib_table_hash
[h
];
227 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
228 if (tb
->tb6_id
== id
) {
238 static void __net_init
fib6_tables_init(struct net
*net
)
240 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
241 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
245 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
247 return fib6_get_table(net
, id
);
250 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
252 return net
->ipv6
.fib6_main_tbl
;
255 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
256 int flags
, pol_lookup_t lookup
)
258 return (struct dst_entry
*) lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
261 static void __net_init
fib6_tables_init(struct net
*net
)
263 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
268 static int fib6_dump_node(struct fib6_walker
*w
)
273 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
274 res
= rt6_dump_route(rt
, w
->args
);
276 /* Frame is full, suspend walking */
285 static void fib6_dump_end(struct netlink_callback
*cb
)
287 struct fib6_walker
*w
= (void *)cb
->args
[2];
292 fib6_walker_unlink(w
);
297 cb
->done
= (void *)cb
->args
[3];
301 static int fib6_dump_done(struct netlink_callback
*cb
)
304 return cb
->done
? cb
->done(cb
) : 0;
307 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
308 struct netlink_callback
*cb
)
310 struct fib6_walker
*w
;
313 w
= (void *)cb
->args
[2];
314 w
->root
= &table
->tb6_root
;
316 if (cb
->args
[4] == 0) {
320 read_lock_bh(&table
->tb6_lock
);
322 read_unlock_bh(&table
->tb6_lock
);
325 cb
->args
[5] = w
->root
->fn_sernum
;
328 if (cb
->args
[5] != w
->root
->fn_sernum
) {
329 /* Begin at the root if the tree changed */
330 cb
->args
[5] = w
->root
->fn_sernum
;
337 read_lock_bh(&table
->tb6_lock
);
338 res
= fib6_walk_continue(w
);
339 read_unlock_bh(&table
->tb6_lock
);
341 fib6_walker_unlink(w
);
349 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
351 struct net
*net
= sock_net(skb
->sk
);
353 unsigned int e
= 0, s_e
;
354 struct rt6_rtnl_dump_arg arg
;
355 struct fib6_walker
*w
;
356 struct fib6_table
*tb
;
357 struct hlist_head
*head
;
363 w
= (void *)cb
->args
[2];
367 * 1. hook callback destructor.
369 cb
->args
[3] = (long)cb
->done
;
370 cb
->done
= fib6_dump_done
;
373 * 2. allocate and initialize walker.
375 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
378 w
->func
= fib6_dump_node
;
379 cb
->args
[2] = (long)w
;
388 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
390 head
= &net
->ipv6
.fib_table_hash
[h
];
391 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
394 res
= fib6_dump_table(tb
, skb
, cb
);
406 res
= res
< 0 ? res
: skb
->len
;
415 * return the appropriate node for a routing tree "add" operation
416 * by either creating and inserting or by returning an existing
420 static struct fib6_node
*fib6_add_1(struct fib6_node
*root
,
421 struct in6_addr
*addr
, int plen
,
422 int offset
, int allow_create
,
423 int replace_required
, int sernum
)
425 struct fib6_node
*fn
, *in
, *ln
;
426 struct fib6_node
*pn
= NULL
;
431 RT6_TRACE("fib6_add_1\n");
433 /* insert node in tree */
438 key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
443 if (plen
< fn
->fn_bit
||
444 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
446 if (replace_required
) {
447 pr_warn("Can't replace route, no match found\n");
448 return ERR_PTR(-ENOENT
);
450 pr_warn("NLM_F_CREATE should be set when creating new route\n");
459 if (plen
== fn
->fn_bit
) {
460 /* clean up an intermediate node */
461 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
462 rt6_release(fn
->leaf
);
466 fn
->fn_sernum
= sernum
;
472 * We have more bits to go
475 /* Try to walk down on tree. */
476 fn
->fn_sernum
= sernum
;
477 dir
= addr_bit_set(addr
, fn
->fn_bit
);
479 fn
= dir
? fn
->right
: fn
->left
;
483 /* We should not create new node because
484 * NLM_F_REPLACE was specified without NLM_F_CREATE
485 * I assume it is safe to require NLM_F_CREATE when
486 * REPLACE flag is used! Later we may want to remove the
487 * check for replace_required, because according
488 * to netlink specification, NLM_F_CREATE
489 * MUST be specified if new route is created.
490 * That would keep IPv6 consistent with IPv4
492 if (replace_required
) {
493 pr_warn("Can't replace route, no match found\n");
494 return ERR_PTR(-ENOENT
);
496 pr_warn("NLM_F_CREATE should be set when creating new route\n");
499 * We walked to the bottom of tree.
500 * Create new leaf node without children.
506 return ERR_PTR(-ENOMEM
);
510 ln
->fn_sernum
= sernum
;
522 * split since we don't have a common prefix anymore or
523 * we have a less significant route.
524 * we've to insert an intermediate node on the list
525 * this new node will point to the one we need to create
531 /* find 1st bit in difference between the 2 addrs.
533 See comment in __ipv6_addr_diff: bit may be an invalid value,
534 but if it is >= plen, the value is ignored in any case.
537 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
542 * (new leaf node)[ln] (old node)[fn]
553 return ERR_PTR(-ENOMEM
);
557 * new intermediate node.
559 * be off since that an address that chooses one of
560 * the branches would not match less specific routes
561 * in the other branch
568 atomic_inc(&in
->leaf
->rt6i_ref
);
570 in
->fn_sernum
= sernum
;
572 /* update parent pointer */
583 ln
->fn_sernum
= sernum
;
585 if (addr_bit_set(addr
, bit
)) {
592 } else { /* plen <= bit */
595 * (new leaf node)[ln]
597 * (old node)[fn] NULL
603 return ERR_PTR(-ENOMEM
);
609 ln
->fn_sernum
= sernum
;
616 if (addr_bit_set(&key
->addr
, plen
))
626 static bool rt6_qualify_for_ecmp(struct rt6_info
*rt
)
628 return (rt
->rt6i_flags
& (RTF_GATEWAY
|RTF_ADDRCONF
|RTF_DYNAMIC
)) ==
632 static void fib6_copy_metrics(u32
*mp
, const struct mx6_config
*mxc
)
636 for (i
= 0; i
< RTAX_MAX
; i
++) {
637 if (test_bit(i
, mxc
->mx_valid
))
642 static int fib6_commit_metrics(struct dst_entry
*dst
, struct mx6_config
*mxc
)
647 if (dst
->flags
& DST_HOST
) {
648 u32
*mp
= dst_metrics_write_ptr(dst
);
653 fib6_copy_metrics(mp
, mxc
);
655 dst_init_metrics(dst
, mxc
->mx
, false);
657 /* We've stolen mx now. */
664 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
667 if (atomic_read(&rt
->rt6i_ref
) != 1) {
668 /* This route is used as dummy address holder in some split
669 * nodes. It is not leaked, but it still holds other resources,
670 * which must be released in time. So, scan ascendant nodes
671 * and replace dummy references to this route with references
672 * to still alive ones.
675 if (!(fn
->fn_flags
& RTN_RTINFO
) && fn
->leaf
== rt
) {
676 fn
->leaf
= fib6_find_prefix(net
, fn
);
677 atomic_inc(&fn
->leaf
->rt6i_ref
);
682 /* No more references are possible at this point. */
683 BUG_ON(atomic_read(&rt
->rt6i_ref
) != 1);
688 * Insert routing information in a node.
691 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
692 struct nl_info
*info
, struct mx6_config
*mxc
)
694 struct rt6_info
*iter
= NULL
;
695 struct rt6_info
**ins
;
696 struct rt6_info
**fallback_ins
= NULL
;
697 int replace
= (info
->nlh
&&
698 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
699 int add
= (!info
->nlh
||
700 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
702 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
707 for (iter
= fn
->leaf
; iter
; iter
= iter
->dst
.rt6_next
) {
709 * Search for duplicates
712 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
714 * Same priority level
717 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
720 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
725 fallback_ins
= fallback_ins
?: ins
;
729 if (iter
->dst
.dev
== rt
->dst
.dev
&&
730 iter
->rt6i_idev
== rt
->rt6i_idev
&&
731 ipv6_addr_equal(&iter
->rt6i_gateway
,
732 &rt
->rt6i_gateway
)) {
733 if (rt
->rt6i_nsiblings
)
734 rt
->rt6i_nsiblings
= 0;
735 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
737 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
738 rt6_clean_expires(iter
);
740 rt6_set_expires(iter
, rt
->dst
.expires
);
743 /* If we have the same destination and the same metric,
744 * but not the same gateway, then the route we try to
745 * add is sibling to this route, increment our counter
746 * of siblings, and later we will add our route to the
748 * Only static routes (which don't have flag
749 * RTF_EXPIRES) are used for ECMPv6.
751 * To avoid long list, we only had siblings if the
752 * route have a gateway.
755 rt6_qualify_for_ecmp(iter
))
756 rt
->rt6i_nsiblings
++;
759 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
763 ins
= &iter
->dst
.rt6_next
;
766 if (fallback_ins
&& !found
) {
767 /* No ECMP-able route found, replace first non-ECMP one */
773 /* Reset round-robin state, if necessary */
774 if (ins
== &fn
->leaf
)
777 /* Link this route to others same route. */
778 if (rt
->rt6i_nsiblings
) {
779 unsigned int rt6i_nsiblings
;
780 struct rt6_info
*sibling
, *temp_sibling
;
782 /* Find the first route that have the same metric */
785 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
786 rt6_qualify_for_ecmp(sibling
)) {
787 list_add_tail(&rt
->rt6i_siblings
,
788 &sibling
->rt6i_siblings
);
791 sibling
= sibling
->dst
.rt6_next
;
793 /* For each sibling in the list, increment the counter of
794 * siblings. BUG() if counters does not match, list of siblings
798 list_for_each_entry_safe(sibling
, temp_sibling
,
799 &rt
->rt6i_siblings
, rt6i_siblings
) {
800 sibling
->rt6i_nsiblings
++;
801 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
804 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
812 pr_warn("NLM_F_CREATE should be set when creating new route\n");
815 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
819 rt
->dst
.rt6_next
= iter
;
822 atomic_inc(&rt
->rt6i_ref
);
823 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
);
824 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
826 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
827 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
828 fn
->fn_flags
|= RTN_RTINFO
;
837 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
841 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
847 rt
->dst
.rt6_next
= iter
->dst
.rt6_next
;
848 atomic_inc(&rt
->rt6i_ref
);
849 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
);
850 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
851 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
852 fn
->fn_flags
|= RTN_RTINFO
;
854 nsiblings
= iter
->rt6i_nsiblings
;
855 fib6_purge_rt(iter
, fn
, info
->nl_net
);
859 /* Replacing an ECMP route, remove all siblings */
860 ins
= &rt
->dst
.rt6_next
;
863 if (rt6_qualify_for_ecmp(iter
)) {
864 *ins
= iter
->dst
.rt6_next
;
865 fib6_purge_rt(iter
, fn
, info
->nl_net
);
869 ins
= &iter
->dst
.rt6_next
;
873 WARN_ON(nsiblings
!= 0);
880 static void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
882 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
883 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
884 mod_timer(&net
->ipv6
.ip6_fib_timer
,
885 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
888 void fib6_force_start_gc(struct net
*net
)
890 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
891 mod_timer(&net
->ipv6
.ip6_fib_timer
,
892 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
896 * Add routing information to the routing tree.
897 * <destination addr>/<source addr>
898 * with source addr info in sub-trees
901 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
902 struct nl_info
*info
, struct mx6_config
*mxc
)
904 struct fib6_node
*fn
, *pn
= NULL
;
906 int allow_create
= 1;
907 int replace_required
= 0;
908 int sernum
= fib6_new_sernum(info
->nl_net
);
911 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
913 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
914 replace_required
= 1;
916 if (!allow_create
&& !replace_required
)
917 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
919 fn
= fib6_add_1(root
, &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
,
920 offsetof(struct rt6_info
, rt6i_dst
), allow_create
,
921 replace_required
, sernum
);
930 #ifdef CONFIG_IPV6_SUBTREES
931 if (rt
->rt6i_src
.plen
) {
932 struct fib6_node
*sn
;
935 struct fib6_node
*sfn
;
947 /* Create subtree root node */
952 sfn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
953 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
954 sfn
->fn_flags
= RTN_ROOT
;
955 sfn
->fn_sernum
= sernum
;
957 /* Now add the first leaf node to new subtree */
959 sn
= fib6_add_1(sfn
, &rt
->rt6i_src
.addr
,
961 offsetof(struct rt6_info
, rt6i_src
),
962 allow_create
, replace_required
, sernum
);
965 /* If it is failed, discard just allocated
966 root, and then (in st_failure) stale node
974 /* Now link new subtree to main tree */
978 sn
= fib6_add_1(fn
->subtree
, &rt
->rt6i_src
.addr
,
980 offsetof(struct rt6_info
, rt6i_src
),
981 allow_create
, replace_required
, sernum
);
991 atomic_inc(&rt
->rt6i_ref
);
997 err
= fib6_add_rt2node(fn
, rt
, info
, mxc
);
999 fib6_start_gc(info
->nl_net
, rt
);
1000 if (!(rt
->rt6i_flags
& RTF_CACHE
))
1001 fib6_prune_clones(info
->nl_net
, pn
);
1006 #ifdef CONFIG_IPV6_SUBTREES
1008 * If fib6_add_1 has cleared the old leaf pointer in the
1009 * super-tree leaf node we have to find a new one for it.
1011 if (pn
!= fn
&& pn
->leaf
== rt
) {
1013 atomic_dec(&rt
->rt6i_ref
);
1015 if (pn
!= fn
&& !pn
->leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
1016 pn
->leaf
= fib6_find_prefix(info
->nl_net
, pn
);
1019 WARN_ON(pn
->leaf
== NULL
);
1020 pn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
1023 atomic_inc(&pn
->leaf
->rt6i_ref
);
1030 #ifdef CONFIG_IPV6_SUBTREES
1031 /* Subtree creation failed, probably main tree node
1032 is orphan. If it is, shoot it.
1035 if (fn
&& !(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)))
1036 fib6_repair_tree(info
->nl_net
, fn
);
1043 * Routing tree lookup
1047 struct lookup_args
{
1048 int offset
; /* key offset on rt6_info */
1049 const struct in6_addr
*addr
; /* search key */
1052 static struct fib6_node
*fib6_lookup_1(struct fib6_node
*root
,
1053 struct lookup_args
*args
)
1055 struct fib6_node
*fn
;
1058 if (unlikely(args
->offset
== 0))
1068 struct fib6_node
*next
;
1070 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1072 next
= dir
? fn
->right
: fn
->left
;
1082 if (FIB6_SUBTREE(fn
) || fn
->fn_flags
& RTN_RTINFO
) {
1085 key
= (struct rt6key
*) ((u8
*) fn
->leaf
+
1088 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1089 #ifdef CONFIG_IPV6_SUBTREES
1091 struct fib6_node
*sfn
;
1092 sfn
= fib6_lookup_1(fn
->subtree
,
1099 if (fn
->fn_flags
& RTN_RTINFO
)
1103 #ifdef CONFIG_IPV6_SUBTREES
1106 if (fn
->fn_flags
& RTN_ROOT
)
1115 struct fib6_node
*fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1116 const struct in6_addr
*saddr
)
1118 struct fib6_node
*fn
;
1119 struct lookup_args args
[] = {
1121 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1124 #ifdef CONFIG_IPV6_SUBTREES
1126 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1131 .offset
= 0, /* sentinel */
1135 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1136 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1143 * Get node with specified destination prefix (and source prefix,
1144 * if subtrees are used)
1148 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1149 const struct in6_addr
*addr
,
1150 int plen
, int offset
)
1152 struct fib6_node
*fn
;
1154 for (fn
= root
; fn
; ) {
1155 struct rt6key
*key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
1160 if (plen
< fn
->fn_bit
||
1161 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1164 if (plen
== fn
->fn_bit
)
1168 * We have more bits to go
1170 if (addr_bit_set(addr
, fn
->fn_bit
))
1178 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1179 const struct in6_addr
*daddr
, int dst_len
,
1180 const struct in6_addr
*saddr
, int src_len
)
1182 struct fib6_node
*fn
;
1184 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1185 offsetof(struct rt6_info
, rt6i_dst
));
1187 #ifdef CONFIG_IPV6_SUBTREES
1189 WARN_ON(saddr
== NULL
);
1190 if (fn
&& fn
->subtree
)
1191 fn
= fib6_locate_1(fn
->subtree
, saddr
, src_len
,
1192 offsetof(struct rt6_info
, rt6i_src
));
1196 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1208 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
)
1210 if (fn
->fn_flags
& RTN_ROOT
)
1211 return net
->ipv6
.ip6_null_entry
;
1215 return fn
->left
->leaf
;
1217 return fn
->right
->leaf
;
1219 fn
= FIB6_SUBTREE(fn
);
1225 * Called to trim the tree of intermediate nodes when possible. "fn"
1226 * is the node we want to try and remove.
1229 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1230 struct fib6_node
*fn
)
1234 struct fib6_node
*child
, *pn
;
1235 struct fib6_walker
*w
;
1239 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1242 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1243 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1249 child
= fn
->right
, children
|= 1;
1251 child
= fn
->left
, children
|= 2;
1253 if (children
== 3 || FIB6_SUBTREE(fn
)
1254 #ifdef CONFIG_IPV6_SUBTREES
1255 /* Subtree root (i.e. fn) may have one child */
1256 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1259 fn
->leaf
= fib6_find_prefix(net
, fn
);
1263 fn
->leaf
= net
->ipv6
.ip6_null_entry
;
1266 atomic_inc(&fn
->leaf
->rt6i_ref
);
1271 #ifdef CONFIG_IPV6_SUBTREES
1272 if (FIB6_SUBTREE(pn
) == fn
) {
1273 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1274 FIB6_SUBTREE(pn
) = NULL
;
1277 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1279 if (pn
->right
== fn
)
1281 else if (pn
->left
== fn
)
1290 #ifdef CONFIG_IPV6_SUBTREES
1294 read_lock(&fib6_walker_lock
);
1297 if (w
->root
== fn
) {
1298 w
->root
= w
->node
= NULL
;
1299 RT6_TRACE("W %p adjusted by delroot 1\n", w
);
1300 } else if (w
->node
== fn
) {
1301 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1306 if (w
->root
== fn
) {
1308 RT6_TRACE("W %p adjusted by delroot 2\n", w
);
1310 if (w
->node
== fn
) {
1313 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1314 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1316 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1317 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1322 read_unlock(&fib6_walker_lock
);
1325 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1328 rt6_release(pn
->leaf
);
1334 static void fib6_del_route(struct fib6_node
*fn
, struct rt6_info
**rtp
,
1335 struct nl_info
*info
)
1337 struct fib6_walker
*w
;
1338 struct rt6_info
*rt
= *rtp
;
1339 struct net
*net
= info
->nl_net
;
1341 RT6_TRACE("fib6_del_route\n");
1344 *rtp
= rt
->dst
.rt6_next
;
1345 rt
->rt6i_node
= NULL
;
1346 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1347 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1349 /* Reset round-robin state, if necessary */
1350 if (fn
->rr_ptr
== rt
)
1353 /* Remove this entry from other siblings */
1354 if (rt
->rt6i_nsiblings
) {
1355 struct rt6_info
*sibling
, *next_sibling
;
1357 list_for_each_entry_safe(sibling
, next_sibling
,
1358 &rt
->rt6i_siblings
, rt6i_siblings
)
1359 sibling
->rt6i_nsiblings
--;
1360 rt
->rt6i_nsiblings
= 0;
1361 list_del_init(&rt
->rt6i_siblings
);
1364 /* Adjust walkers */
1365 read_lock(&fib6_walker_lock
);
1367 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1368 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1369 w
->leaf
= rt
->dst
.rt6_next
;
1374 read_unlock(&fib6_walker_lock
);
1376 rt
->dst
.rt6_next
= NULL
;
1378 /* If it was last route, expunge its radix tree node */
1380 fn
->fn_flags
&= ~RTN_RTINFO
;
1381 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1382 fn
= fib6_repair_tree(net
, fn
);
1385 fib6_purge_rt(rt
, fn
, net
);
1387 inet6_rt_notify(RTM_DELROUTE
, rt
, info
);
1391 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1393 struct net
*net
= info
->nl_net
;
1394 struct fib6_node
*fn
= rt
->rt6i_node
;
1395 struct rt6_info
**rtp
;
1398 if (rt
->dst
.obsolete
> 0) {
1403 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1406 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1408 if (!(rt
->rt6i_flags
& RTF_CACHE
)) {
1409 struct fib6_node
*pn
= fn
;
1410 #ifdef CONFIG_IPV6_SUBTREES
1411 /* clones of this route might be in another subtree */
1412 if (rt
->rt6i_src
.plen
) {
1413 while (!(pn
->fn_flags
& RTN_ROOT
))
1418 fib6_prune_clones(info
->nl_net
, pn
);
1422 * Walk the leaf entries looking for ourself
1425 for (rtp
= &fn
->leaf
; *rtp
; rtp
= &(*rtp
)->dst
.rt6_next
) {
1427 fib6_del_route(fn
, rtp
, info
);
1435 * Tree traversal function.
1437 * Certainly, it is not interrupt safe.
1438 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1439 * It means, that we can modify tree during walking
1440 * and use this function for garbage collection, clone pruning,
1441 * cleaning tree when a device goes down etc. etc.
1443 * It guarantees that every node will be traversed,
1444 * and that it will be traversed only once.
1446 * Callback function w->func may return:
1447 * 0 -> continue walking.
1448 * positive value -> walking is suspended (used by tree dumps,
1449 * and probably by gc, if it will be split to several slices)
1450 * negative value -> terminate walking.
1452 * The function itself returns:
1453 * 0 -> walk is complete.
1454 * >0 -> walk is incomplete (i.e. suspended)
1455 * <0 -> walk is terminated by an error.
1458 static int fib6_walk_continue(struct fib6_walker
*w
)
1460 struct fib6_node
*fn
, *pn
;
1467 if (w
->prune
&& fn
!= w
->root
&&
1468 fn
->fn_flags
& RTN_RTINFO
&& w
->state
< FWS_C
) {
1473 #ifdef CONFIG_IPV6_SUBTREES
1475 if (FIB6_SUBTREE(fn
)) {
1476 w
->node
= FIB6_SUBTREE(fn
);
1484 w
->state
= FWS_INIT
;
1490 w
->node
= fn
->right
;
1491 w
->state
= FWS_INIT
;
1497 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1519 #ifdef CONFIG_IPV6_SUBTREES
1520 if (FIB6_SUBTREE(pn
) == fn
) {
1521 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1526 if (pn
->left
== fn
) {
1530 if (pn
->right
== fn
) {
1532 w
->leaf
= w
->node
->leaf
;
1542 static int fib6_walk(struct fib6_walker
*w
)
1546 w
->state
= FWS_INIT
;
1549 fib6_walker_link(w
);
1550 res
= fib6_walk_continue(w
);
1552 fib6_walker_unlink(w
);
1556 static int fib6_clean_node(struct fib6_walker
*w
)
1559 struct rt6_info
*rt
;
1560 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1561 struct nl_info info
= {
1565 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1566 w
->node
->fn_sernum
!= c
->sernum
)
1567 w
->node
->fn_sernum
= c
->sernum
;
1570 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1575 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1576 res
= c
->func(rt
, c
->arg
);
1579 res
= fib6_del(rt
, &info
);
1582 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1583 __func__
, rt
, rt
->rt6i_node
, res
);
1596 * Convenient frontend to tree walker.
1598 * func is called on each route.
1599 * It may return -1 -> delete this route.
1600 * 0 -> continue walking
1602 * prune==1 -> only immediate children of node (certainly,
1603 * ignoring pure split nodes) will be scanned.
1606 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1607 int (*func
)(struct rt6_info
*, void *arg
),
1608 bool prune
, int sernum
, void *arg
)
1610 struct fib6_cleaner c
;
1613 c
.w
.func
= fib6_clean_node
;
1625 static void __fib6_clean_all(struct net
*net
,
1626 int (*func
)(struct rt6_info
*, void *),
1627 int sernum
, void *arg
)
1629 struct fib6_table
*table
;
1630 struct hlist_head
*head
;
1634 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1635 head
= &net
->ipv6
.fib_table_hash
[h
];
1636 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1637 write_lock_bh(&table
->tb6_lock
);
1638 fib6_clean_tree(net
, &table
->tb6_root
,
1639 func
, false, sernum
, arg
);
1640 write_unlock_bh(&table
->tb6_lock
);
1646 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *),
1649 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
);
1652 static int fib6_prune_clone(struct rt6_info
*rt
, void *arg
)
1654 if (rt
->rt6i_flags
& RTF_CACHE
) {
1655 RT6_TRACE("pruning clone %p\n", rt
);
1662 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
)
1664 fib6_clean_tree(net
, fn
, fib6_prune_clone
, true,
1665 FIB6_NO_SERNUM_CHANGE
, NULL
);
1668 static void fib6_flush_trees(struct net
*net
)
1670 int new_sernum
= fib6_new_sernum(net
);
1672 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
);
1676 * Garbage collection
1679 static struct fib6_gc_args
1685 static int fib6_age(struct rt6_info
*rt
, void *arg
)
1687 unsigned long now
= jiffies
;
1690 * check addrconf expiration here.
1691 * Routes are expired even if they are in use.
1693 * Also age clones. Note, that clones are aged out
1694 * only if they are not in use now.
1697 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
1698 if (time_after(now
, rt
->dst
.expires
)) {
1699 RT6_TRACE("expiring %p\n", rt
);
1703 } else if (rt
->rt6i_flags
& RTF_CACHE
) {
1704 if (atomic_read(&rt
->dst
.__refcnt
) == 0 &&
1705 time_after_eq(now
, rt
->dst
.lastuse
+ gc_args
.timeout
)) {
1706 RT6_TRACE("aging clone %p\n", rt
);
1708 } else if (rt
->rt6i_flags
& RTF_GATEWAY
) {
1709 struct neighbour
*neigh
;
1710 __u8 neigh_flags
= 0;
1712 neigh
= dst_neigh_lookup(&rt
->dst
, &rt
->rt6i_gateway
);
1714 neigh_flags
= neigh
->flags
;
1715 neigh_release(neigh
);
1717 if (!(neigh_flags
& NTF_ROUTER
)) {
1718 RT6_TRACE("purging route %p via non-router but gateway\n",
1729 static DEFINE_SPINLOCK(fib6_gc_lock
);
1731 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
1736 spin_lock_bh(&fib6_gc_lock
);
1737 } else if (!spin_trylock_bh(&fib6_gc_lock
)) {
1738 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
1741 gc_args
.timeout
= expires
? (int)expires
:
1742 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
1744 gc_args
.more
= icmp6_dst_gc();
1746 fib6_clean_all(net
, fib6_age
, NULL
);
1748 net
->ipv6
.ip6_rt_last_gc
= now
;
1751 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1753 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
1755 del_timer(&net
->ipv6
.ip6_fib_timer
);
1756 spin_unlock_bh(&fib6_gc_lock
);
1759 static void fib6_gc_timer_cb(unsigned long arg
)
1761 fib6_run_gc(0, (struct net
*)arg
, true);
1764 static int __net_init
fib6_net_init(struct net
*net
)
1766 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
1768 setup_timer(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, (unsigned long)net
);
1770 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
1771 if (!net
->ipv6
.rt6_stats
)
1774 /* Avoid false sharing : Use at least a full cache line */
1775 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
1777 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
1778 if (!net
->ipv6
.fib_table_hash
)
1781 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
1783 if (!net
->ipv6
.fib6_main_tbl
)
1784 goto out_fib_table_hash
;
1786 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
1787 net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1788 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
1789 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1790 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1792 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1793 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
1795 if (!net
->ipv6
.fib6_local_tbl
)
1796 goto out_fib6_main_tbl
;
1797 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
1798 net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1799 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
1800 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1801 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1803 fib6_tables_init(net
);
1807 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1809 kfree(net
->ipv6
.fib6_main_tbl
);
1812 kfree(net
->ipv6
.fib_table_hash
);
1814 kfree(net
->ipv6
.rt6_stats
);
1819 static void fib6_net_exit(struct net
*net
)
1821 rt6_ifdown(net
, NULL
);
1822 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
1824 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1825 inetpeer_invalidate_tree(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1826 kfree(net
->ipv6
.fib6_local_tbl
);
1828 inetpeer_invalidate_tree(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1829 kfree(net
->ipv6
.fib6_main_tbl
);
1830 kfree(net
->ipv6
.fib_table_hash
);
1831 kfree(net
->ipv6
.rt6_stats
);
1834 static struct pernet_operations fib6_net_ops
= {
1835 .init
= fib6_net_init
,
1836 .exit
= fib6_net_exit
,
1839 int __init
fib6_init(void)
1843 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
1844 sizeof(struct fib6_node
),
1845 0, SLAB_HWCACHE_ALIGN
,
1847 if (!fib6_node_kmem
)
1850 ret
= register_pernet_subsys(&fib6_net_ops
);
1852 goto out_kmem_cache_create
;
1854 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
1857 goto out_unregister_subsys
;
1859 __fib6_flush_trees
= fib6_flush_trees
;
1863 out_unregister_subsys
:
1864 unregister_pernet_subsys(&fib6_net_ops
);
1865 out_kmem_cache_create
:
1866 kmem_cache_destroy(fib6_node_kmem
);
1870 void fib6_gc_cleanup(void)
1872 unregister_pernet_subsys(&fib6_net_ops
);
1873 kmem_cache_destroy(fib6_node_kmem
);
1876 #ifdef CONFIG_PROC_FS
1878 struct ipv6_route_iter
{
1879 struct seq_net_private p
;
1880 struct fib6_walker w
;
1882 struct fib6_table
*tbl
;
1886 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
1888 struct rt6_info
*rt
= v
;
1889 struct ipv6_route_iter
*iter
= seq
->private;
1891 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
1893 #ifdef CONFIG_IPV6_SUBTREES
1894 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
1896 seq_puts(seq
, "00000000000000000000000000000000 00 ");
1898 if (rt
->rt6i_flags
& RTF_GATEWAY
)
1899 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
1901 seq_puts(seq
, "00000000000000000000000000000000");
1903 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
1904 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
1905 rt
->dst
.__use
, rt
->rt6i_flags
,
1906 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
1907 iter
->w
.leaf
= NULL
;
1911 static int ipv6_route_yield(struct fib6_walker
*w
)
1913 struct ipv6_route_iter
*iter
= w
->args
;
1919 iter
->w
.leaf
= iter
->w
.leaf
->dst
.rt6_next
;
1921 if (!iter
->skip
&& iter
->w
.leaf
)
1923 } while (iter
->w
.leaf
);
1928 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
)
1930 memset(&iter
->w
, 0, sizeof(iter
->w
));
1931 iter
->w
.func
= ipv6_route_yield
;
1932 iter
->w
.root
= &iter
->tbl
->tb6_root
;
1933 iter
->w
.state
= FWS_INIT
;
1934 iter
->w
.node
= iter
->w
.root
;
1935 iter
->w
.args
= iter
;
1936 iter
->sernum
= iter
->w
.root
->fn_sernum
;
1937 INIT_LIST_HEAD(&iter
->w
.lh
);
1938 fib6_walker_link(&iter
->w
);
1941 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
1945 struct hlist_node
*node
;
1948 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
1949 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
1955 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
1956 node
= rcu_dereference_bh(
1957 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
1959 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
1962 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
1964 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
1965 iter
->sernum
= iter
->w
.root
->fn_sernum
;
1966 iter
->w
.state
= FWS_INIT
;
1967 iter
->w
.node
= iter
->w
.root
;
1968 WARN_ON(iter
->w
.skip
);
1969 iter
->w
.skip
= iter
->w
.count
;
1973 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1977 struct net
*net
= seq_file_net(seq
);
1978 struct ipv6_route_iter
*iter
= seq
->private;
1983 n
= ((struct rt6_info
*)v
)->dst
.rt6_next
;
1990 ipv6_route_check_sernum(iter
);
1991 read_lock(&iter
->tbl
->tb6_lock
);
1992 r
= fib6_walk_continue(&iter
->w
);
1993 read_unlock(&iter
->tbl
->tb6_lock
);
1997 return iter
->w
.leaf
;
1999 fib6_walker_unlink(&iter
->w
);
2002 fib6_walker_unlink(&iter
->w
);
2004 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2008 ipv6_route_seq_setup_walk(iter
);
2012 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2015 struct net
*net
= seq_file_net(seq
);
2016 struct ipv6_route_iter
*iter
= seq
->private;
2019 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2023 ipv6_route_seq_setup_walk(iter
);
2024 return ipv6_route_seq_next(seq
, NULL
, pos
);
2030 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2032 struct fib6_walker
*w
= &iter
->w
;
2033 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2036 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2039 struct ipv6_route_iter
*iter
= seq
->private;
2041 if (ipv6_route_iter_active(iter
))
2042 fib6_walker_unlink(&iter
->w
);
2044 rcu_read_unlock_bh();
2047 static const struct seq_operations ipv6_route_seq_ops
= {
2048 .start
= ipv6_route_seq_start
,
2049 .next
= ipv6_route_seq_next
,
2050 .stop
= ipv6_route_seq_stop
,
2051 .show
= ipv6_route_seq_show
2054 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2056 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2057 sizeof(struct ipv6_route_iter
));
2060 #endif /* CONFIG_PROC_FS */