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_free_pcpu(struct rt6_info
*non_pcpu_rt
)
161 if (!non_pcpu_rt
->rt6i_pcpu
)
164 for_each_possible_cpu(cpu
) {
165 struct rt6_info
**ppcpu_rt
;
166 struct rt6_info
*pcpu_rt
;
168 ppcpu_rt
= per_cpu_ptr(non_pcpu_rt
->rt6i_pcpu
, cpu
);
171 dst_free(&pcpu_rt
->dst
);
177 static void rt6_release(struct rt6_info
*rt
)
179 if (atomic_dec_and_test(&rt
->rt6i_ref
)) {
185 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
190 * Initialize table lock at a single place to give lockdep a key,
191 * tables aren't visible prior to being linked to the list.
193 rwlock_init(&tb
->tb6_lock
);
195 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
198 * No protection necessary, this is the only list mutatation
199 * operation, tables never disappear once they exist.
201 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
204 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
206 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
208 struct fib6_table
*table
;
210 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
213 table
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
214 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
215 inet_peer_base_init(&table
->tb6_peers
);
221 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
223 struct fib6_table
*tb
;
227 tb
= fib6_get_table(net
, id
);
231 tb
= fib6_alloc_table(net
, id
);
233 fib6_link_table(net
, tb
);
238 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
240 struct fib6_table
*tb
;
241 struct hlist_head
*head
;
246 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
248 head
= &net
->ipv6
.fib_table_hash
[h
];
249 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
250 if (tb
->tb6_id
== id
) {
260 static void __net_init
fib6_tables_init(struct net
*net
)
262 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
263 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
267 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
269 return fib6_get_table(net
, id
);
272 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
274 return net
->ipv6
.fib6_main_tbl
;
277 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
278 int flags
, pol_lookup_t lookup
)
280 return (struct dst_entry
*) lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
283 static void __net_init
fib6_tables_init(struct net
*net
)
285 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
290 static int fib6_dump_node(struct fib6_walker
*w
)
295 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
296 res
= rt6_dump_route(rt
, w
->args
);
298 /* Frame is full, suspend walking */
307 static void fib6_dump_end(struct netlink_callback
*cb
)
309 struct fib6_walker
*w
= (void *)cb
->args
[2];
314 fib6_walker_unlink(w
);
319 cb
->done
= (void *)cb
->args
[3];
323 static int fib6_dump_done(struct netlink_callback
*cb
)
326 return cb
->done
? cb
->done(cb
) : 0;
329 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
330 struct netlink_callback
*cb
)
332 struct fib6_walker
*w
;
335 w
= (void *)cb
->args
[2];
336 w
->root
= &table
->tb6_root
;
338 if (cb
->args
[4] == 0) {
342 read_lock_bh(&table
->tb6_lock
);
344 read_unlock_bh(&table
->tb6_lock
);
347 cb
->args
[5] = w
->root
->fn_sernum
;
350 if (cb
->args
[5] != w
->root
->fn_sernum
) {
351 /* Begin at the root if the tree changed */
352 cb
->args
[5] = w
->root
->fn_sernum
;
359 read_lock_bh(&table
->tb6_lock
);
360 res
= fib6_walk_continue(w
);
361 read_unlock_bh(&table
->tb6_lock
);
363 fib6_walker_unlink(w
);
371 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
373 struct net
*net
= sock_net(skb
->sk
);
375 unsigned int e
= 0, s_e
;
376 struct rt6_rtnl_dump_arg arg
;
377 struct fib6_walker
*w
;
378 struct fib6_table
*tb
;
379 struct hlist_head
*head
;
385 w
= (void *)cb
->args
[2];
389 * 1. hook callback destructor.
391 cb
->args
[3] = (long)cb
->done
;
392 cb
->done
= fib6_dump_done
;
395 * 2. allocate and initialize walker.
397 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
400 w
->func
= fib6_dump_node
;
401 cb
->args
[2] = (long)w
;
410 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
412 head
= &net
->ipv6
.fib_table_hash
[h
];
413 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
416 res
= fib6_dump_table(tb
, skb
, cb
);
428 res
= res
< 0 ? res
: skb
->len
;
437 * return the appropriate node for a routing tree "add" operation
438 * by either creating and inserting or by returning an existing
442 static struct fib6_node
*fib6_add_1(struct fib6_node
*root
,
443 struct in6_addr
*addr
, int plen
,
444 int offset
, int allow_create
,
445 int replace_required
, int sernum
)
447 struct fib6_node
*fn
, *in
, *ln
;
448 struct fib6_node
*pn
= NULL
;
453 RT6_TRACE("fib6_add_1\n");
455 /* insert node in tree */
460 key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
465 if (plen
< fn
->fn_bit
||
466 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
468 if (replace_required
) {
469 pr_warn("Can't replace route, no match found\n");
470 return ERR_PTR(-ENOENT
);
472 pr_warn("NLM_F_CREATE should be set when creating new route\n");
481 if (plen
== fn
->fn_bit
) {
482 /* clean up an intermediate node */
483 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
484 rt6_release(fn
->leaf
);
488 fn
->fn_sernum
= sernum
;
494 * We have more bits to go
497 /* Try to walk down on tree. */
498 fn
->fn_sernum
= sernum
;
499 dir
= addr_bit_set(addr
, fn
->fn_bit
);
501 fn
= dir
? fn
->right
: fn
->left
;
505 /* We should not create new node because
506 * NLM_F_REPLACE was specified without NLM_F_CREATE
507 * I assume it is safe to require NLM_F_CREATE when
508 * REPLACE flag is used! Later we may want to remove the
509 * check for replace_required, because according
510 * to netlink specification, NLM_F_CREATE
511 * MUST be specified if new route is created.
512 * That would keep IPv6 consistent with IPv4
514 if (replace_required
) {
515 pr_warn("Can't replace route, no match found\n");
516 return ERR_PTR(-ENOENT
);
518 pr_warn("NLM_F_CREATE should be set when creating new route\n");
521 * We walked to the bottom of tree.
522 * Create new leaf node without children.
528 return ERR_PTR(-ENOMEM
);
532 ln
->fn_sernum
= sernum
;
544 * split since we don't have a common prefix anymore or
545 * we have a less significant route.
546 * we've to insert an intermediate node on the list
547 * this new node will point to the one we need to create
553 /* find 1st bit in difference between the 2 addrs.
555 See comment in __ipv6_addr_diff: bit may be an invalid value,
556 but if it is >= plen, the value is ignored in any case.
559 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
564 * (new leaf node)[ln] (old node)[fn]
575 return ERR_PTR(-ENOMEM
);
579 * new intermediate node.
581 * be off since that an address that chooses one of
582 * the branches would not match less specific routes
583 * in the other branch
590 atomic_inc(&in
->leaf
->rt6i_ref
);
592 in
->fn_sernum
= sernum
;
594 /* update parent pointer */
605 ln
->fn_sernum
= sernum
;
607 if (addr_bit_set(addr
, bit
)) {
614 } else { /* plen <= bit */
617 * (new leaf node)[ln]
619 * (old node)[fn] NULL
625 return ERR_PTR(-ENOMEM
);
631 ln
->fn_sernum
= sernum
;
638 if (addr_bit_set(&key
->addr
, plen
))
648 static bool rt6_qualify_for_ecmp(struct rt6_info
*rt
)
650 return (rt
->rt6i_flags
& (RTF_GATEWAY
|RTF_ADDRCONF
|RTF_DYNAMIC
)) ==
654 static void fib6_copy_metrics(u32
*mp
, const struct mx6_config
*mxc
)
658 for (i
= 0; i
< RTAX_MAX
; i
++) {
659 if (test_bit(i
, mxc
->mx_valid
))
664 static int fib6_commit_metrics(struct dst_entry
*dst
, struct mx6_config
*mxc
)
669 if (dst
->flags
& DST_HOST
) {
670 u32
*mp
= dst_metrics_write_ptr(dst
);
675 fib6_copy_metrics(mp
, mxc
);
677 dst_init_metrics(dst
, mxc
->mx
, false);
679 /* We've stolen mx now. */
686 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
689 if (atomic_read(&rt
->rt6i_ref
) != 1) {
690 /* This route is used as dummy address holder in some split
691 * nodes. It is not leaked, but it still holds other resources,
692 * which must be released in time. So, scan ascendant nodes
693 * and replace dummy references to this route with references
694 * to still alive ones.
697 if (!(fn
->fn_flags
& RTN_RTINFO
) && fn
->leaf
== rt
) {
698 fn
->leaf
= fib6_find_prefix(net
, fn
);
699 atomic_inc(&fn
->leaf
->rt6i_ref
);
704 /* No more references are possible at this point. */
705 BUG_ON(atomic_read(&rt
->rt6i_ref
) != 1);
710 * Insert routing information in a node.
713 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
714 struct nl_info
*info
, struct mx6_config
*mxc
)
716 struct rt6_info
*iter
= NULL
;
717 struct rt6_info
**ins
;
718 struct rt6_info
**fallback_ins
= NULL
;
719 int replace
= (info
->nlh
&&
720 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
721 int add
= (!info
->nlh
||
722 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
724 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
729 for (iter
= fn
->leaf
; iter
; iter
= iter
->dst
.rt6_next
) {
731 * Search for duplicates
734 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
736 * Same priority level
739 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
742 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
747 fallback_ins
= fallback_ins
?: ins
;
751 if (iter
->dst
.dev
== rt
->dst
.dev
&&
752 iter
->rt6i_idev
== rt
->rt6i_idev
&&
753 ipv6_addr_equal(&iter
->rt6i_gateway
,
754 &rt
->rt6i_gateway
)) {
755 if (rt
->rt6i_nsiblings
)
756 rt
->rt6i_nsiblings
= 0;
757 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
759 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
760 rt6_clean_expires(iter
);
762 rt6_set_expires(iter
, rt
->dst
.expires
);
763 iter
->rt6i_pmtu
= rt
->rt6i_pmtu
;
766 /* If we have the same destination and the same metric,
767 * but not the same gateway, then the route we try to
768 * add is sibling to this route, increment our counter
769 * of siblings, and later we will add our route to the
771 * Only static routes (which don't have flag
772 * RTF_EXPIRES) are used for ECMPv6.
774 * To avoid long list, we only had siblings if the
775 * route have a gateway.
778 rt6_qualify_for_ecmp(iter
))
779 rt
->rt6i_nsiblings
++;
782 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
786 ins
= &iter
->dst
.rt6_next
;
789 if (fallback_ins
&& !found
) {
790 /* No ECMP-able route found, replace first non-ECMP one */
796 /* Reset round-robin state, if necessary */
797 if (ins
== &fn
->leaf
)
800 /* Link this route to others same route. */
801 if (rt
->rt6i_nsiblings
) {
802 unsigned int rt6i_nsiblings
;
803 struct rt6_info
*sibling
, *temp_sibling
;
805 /* Find the first route that have the same metric */
808 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
809 rt6_qualify_for_ecmp(sibling
)) {
810 list_add_tail(&rt
->rt6i_siblings
,
811 &sibling
->rt6i_siblings
);
814 sibling
= sibling
->dst
.rt6_next
;
816 /* For each sibling in the list, increment the counter of
817 * siblings. BUG() if counters does not match, list of siblings
821 list_for_each_entry_safe(sibling
, temp_sibling
,
822 &rt
->rt6i_siblings
, rt6i_siblings
) {
823 sibling
->rt6i_nsiblings
++;
824 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
827 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
835 pr_warn("NLM_F_CREATE should be set when creating new route\n");
838 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
842 rt
->dst
.rt6_next
= iter
;
845 atomic_inc(&rt
->rt6i_ref
);
846 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
);
847 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
849 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
850 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
851 fn
->fn_flags
|= RTN_RTINFO
;
860 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
864 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
870 rt
->dst
.rt6_next
= iter
->dst
.rt6_next
;
871 atomic_inc(&rt
->rt6i_ref
);
872 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
);
873 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
874 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
875 fn
->fn_flags
|= RTN_RTINFO
;
877 nsiblings
= iter
->rt6i_nsiblings
;
878 fib6_purge_rt(iter
, fn
, info
->nl_net
);
882 /* Replacing an ECMP route, remove all siblings */
883 ins
= &rt
->dst
.rt6_next
;
886 if (rt6_qualify_for_ecmp(iter
)) {
887 *ins
= iter
->dst
.rt6_next
;
888 fib6_purge_rt(iter
, fn
, info
->nl_net
);
892 ins
= &iter
->dst
.rt6_next
;
896 WARN_ON(nsiblings
!= 0);
903 static void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
905 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
906 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
907 mod_timer(&net
->ipv6
.ip6_fib_timer
,
908 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
911 void fib6_force_start_gc(struct net
*net
)
913 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
914 mod_timer(&net
->ipv6
.ip6_fib_timer
,
915 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
919 * Add routing information to the routing tree.
920 * <destination addr>/<source addr>
921 * with source addr info in sub-trees
924 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
925 struct nl_info
*info
, struct mx6_config
*mxc
)
927 struct fib6_node
*fn
, *pn
= NULL
;
929 int allow_create
= 1;
930 int replace_required
= 0;
931 int sernum
= fib6_new_sernum(info
->nl_net
);
934 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
936 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
937 replace_required
= 1;
939 if (!allow_create
&& !replace_required
)
940 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
942 fn
= fib6_add_1(root
, &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
,
943 offsetof(struct rt6_info
, rt6i_dst
), allow_create
,
944 replace_required
, sernum
);
953 #ifdef CONFIG_IPV6_SUBTREES
954 if (rt
->rt6i_src
.plen
) {
955 struct fib6_node
*sn
;
958 struct fib6_node
*sfn
;
970 /* Create subtree root node */
975 sfn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
976 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
977 sfn
->fn_flags
= RTN_ROOT
;
978 sfn
->fn_sernum
= sernum
;
980 /* Now add the first leaf node to new subtree */
982 sn
= fib6_add_1(sfn
, &rt
->rt6i_src
.addr
,
984 offsetof(struct rt6_info
, rt6i_src
),
985 allow_create
, replace_required
, sernum
);
988 /* If it is failed, discard just allocated
989 root, and then (in st_failure) stale node
997 /* Now link new subtree to main tree */
1001 sn
= fib6_add_1(fn
->subtree
, &rt
->rt6i_src
.addr
,
1003 offsetof(struct rt6_info
, rt6i_src
),
1004 allow_create
, replace_required
, sernum
);
1014 atomic_inc(&rt
->rt6i_ref
);
1020 err
= fib6_add_rt2node(fn
, rt
, info
, mxc
);
1022 fib6_start_gc(info
->nl_net
, rt
);
1023 if (!(rt
->rt6i_flags
& RTF_CACHE
))
1024 fib6_prune_clones(info
->nl_net
, pn
);
1029 #ifdef CONFIG_IPV6_SUBTREES
1031 * If fib6_add_1 has cleared the old leaf pointer in the
1032 * super-tree leaf node we have to find a new one for it.
1034 if (pn
!= fn
&& pn
->leaf
== rt
) {
1036 atomic_dec(&rt
->rt6i_ref
);
1038 if (pn
!= fn
&& !pn
->leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
1039 pn
->leaf
= fib6_find_prefix(info
->nl_net
, pn
);
1042 WARN_ON(pn
->leaf
== NULL
);
1043 pn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
1046 atomic_inc(&pn
->leaf
->rt6i_ref
);
1053 #ifdef CONFIG_IPV6_SUBTREES
1054 /* Subtree creation failed, probably main tree node
1055 is orphan. If it is, shoot it.
1058 if (fn
&& !(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)))
1059 fib6_repair_tree(info
->nl_net
, fn
);
1066 * Routing tree lookup
1070 struct lookup_args
{
1071 int offset
; /* key offset on rt6_info */
1072 const struct in6_addr
*addr
; /* search key */
1075 static struct fib6_node
*fib6_lookup_1(struct fib6_node
*root
,
1076 struct lookup_args
*args
)
1078 struct fib6_node
*fn
;
1081 if (unlikely(args
->offset
== 0))
1091 struct fib6_node
*next
;
1093 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1095 next
= dir
? fn
->right
: fn
->left
;
1105 if (FIB6_SUBTREE(fn
) || fn
->fn_flags
& RTN_RTINFO
) {
1108 key
= (struct rt6key
*) ((u8
*) fn
->leaf
+
1111 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1112 #ifdef CONFIG_IPV6_SUBTREES
1114 struct fib6_node
*sfn
;
1115 sfn
= fib6_lookup_1(fn
->subtree
,
1122 if (fn
->fn_flags
& RTN_RTINFO
)
1126 #ifdef CONFIG_IPV6_SUBTREES
1129 if (fn
->fn_flags
& RTN_ROOT
)
1138 struct fib6_node
*fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1139 const struct in6_addr
*saddr
)
1141 struct fib6_node
*fn
;
1142 struct lookup_args args
[] = {
1144 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1147 #ifdef CONFIG_IPV6_SUBTREES
1149 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1154 .offset
= 0, /* sentinel */
1158 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1159 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1166 * Get node with specified destination prefix (and source prefix,
1167 * if subtrees are used)
1171 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1172 const struct in6_addr
*addr
,
1173 int plen
, int offset
)
1175 struct fib6_node
*fn
;
1177 for (fn
= root
; fn
; ) {
1178 struct rt6key
*key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
1183 if (plen
< fn
->fn_bit
||
1184 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1187 if (plen
== fn
->fn_bit
)
1191 * We have more bits to go
1193 if (addr_bit_set(addr
, fn
->fn_bit
))
1201 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1202 const struct in6_addr
*daddr
, int dst_len
,
1203 const struct in6_addr
*saddr
, int src_len
)
1205 struct fib6_node
*fn
;
1207 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1208 offsetof(struct rt6_info
, rt6i_dst
));
1210 #ifdef CONFIG_IPV6_SUBTREES
1212 WARN_ON(saddr
== NULL
);
1213 if (fn
&& fn
->subtree
)
1214 fn
= fib6_locate_1(fn
->subtree
, saddr
, src_len
,
1215 offsetof(struct rt6_info
, rt6i_src
));
1219 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1231 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
)
1233 if (fn
->fn_flags
& RTN_ROOT
)
1234 return net
->ipv6
.ip6_null_entry
;
1238 return fn
->left
->leaf
;
1240 return fn
->right
->leaf
;
1242 fn
= FIB6_SUBTREE(fn
);
1248 * Called to trim the tree of intermediate nodes when possible. "fn"
1249 * is the node we want to try and remove.
1252 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1253 struct fib6_node
*fn
)
1257 struct fib6_node
*child
, *pn
;
1258 struct fib6_walker
*w
;
1262 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1265 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1266 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1272 child
= fn
->right
, children
|= 1;
1274 child
= fn
->left
, children
|= 2;
1276 if (children
== 3 || FIB6_SUBTREE(fn
)
1277 #ifdef CONFIG_IPV6_SUBTREES
1278 /* Subtree root (i.e. fn) may have one child */
1279 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1282 fn
->leaf
= fib6_find_prefix(net
, fn
);
1286 fn
->leaf
= net
->ipv6
.ip6_null_entry
;
1289 atomic_inc(&fn
->leaf
->rt6i_ref
);
1294 #ifdef CONFIG_IPV6_SUBTREES
1295 if (FIB6_SUBTREE(pn
) == fn
) {
1296 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1297 FIB6_SUBTREE(pn
) = NULL
;
1300 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1302 if (pn
->right
== fn
)
1304 else if (pn
->left
== fn
)
1313 #ifdef CONFIG_IPV6_SUBTREES
1317 read_lock(&fib6_walker_lock
);
1320 if (w
->root
== fn
) {
1321 w
->root
= w
->node
= NULL
;
1322 RT6_TRACE("W %p adjusted by delroot 1\n", w
);
1323 } else if (w
->node
== fn
) {
1324 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1329 if (w
->root
== fn
) {
1331 RT6_TRACE("W %p adjusted by delroot 2\n", w
);
1333 if (w
->node
== fn
) {
1336 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1337 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1339 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1340 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1345 read_unlock(&fib6_walker_lock
);
1348 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1351 rt6_release(pn
->leaf
);
1357 static void fib6_del_route(struct fib6_node
*fn
, struct rt6_info
**rtp
,
1358 struct nl_info
*info
)
1360 struct fib6_walker
*w
;
1361 struct rt6_info
*rt
= *rtp
;
1362 struct net
*net
= info
->nl_net
;
1364 RT6_TRACE("fib6_del_route\n");
1367 *rtp
= rt
->dst
.rt6_next
;
1368 rt
->rt6i_node
= NULL
;
1369 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1370 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1372 /* Reset round-robin state, if necessary */
1373 if (fn
->rr_ptr
== rt
)
1376 /* Remove this entry from other siblings */
1377 if (rt
->rt6i_nsiblings
) {
1378 struct rt6_info
*sibling
, *next_sibling
;
1380 list_for_each_entry_safe(sibling
, next_sibling
,
1381 &rt
->rt6i_siblings
, rt6i_siblings
)
1382 sibling
->rt6i_nsiblings
--;
1383 rt
->rt6i_nsiblings
= 0;
1384 list_del_init(&rt
->rt6i_siblings
);
1387 /* Adjust walkers */
1388 read_lock(&fib6_walker_lock
);
1390 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1391 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1392 w
->leaf
= rt
->dst
.rt6_next
;
1397 read_unlock(&fib6_walker_lock
);
1399 rt
->dst
.rt6_next
= NULL
;
1401 /* If it was last route, expunge its radix tree node */
1403 fn
->fn_flags
&= ~RTN_RTINFO
;
1404 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1405 fn
= fib6_repair_tree(net
, fn
);
1408 fib6_purge_rt(rt
, fn
, net
);
1410 inet6_rt_notify(RTM_DELROUTE
, rt
, info
);
1414 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1416 struct net
*net
= info
->nl_net
;
1417 struct fib6_node
*fn
= rt
->rt6i_node
;
1418 struct rt6_info
**rtp
;
1421 if (rt
->dst
.obsolete
> 0) {
1426 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1429 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1431 if (!(rt
->rt6i_flags
& RTF_CACHE
)) {
1432 struct fib6_node
*pn
= fn
;
1433 #ifdef CONFIG_IPV6_SUBTREES
1434 /* clones of this route might be in another subtree */
1435 if (rt
->rt6i_src
.plen
) {
1436 while (!(pn
->fn_flags
& RTN_ROOT
))
1441 fib6_prune_clones(info
->nl_net
, pn
);
1445 * Walk the leaf entries looking for ourself
1448 for (rtp
= &fn
->leaf
; *rtp
; rtp
= &(*rtp
)->dst
.rt6_next
) {
1450 fib6_del_route(fn
, rtp
, info
);
1458 * Tree traversal function.
1460 * Certainly, it is not interrupt safe.
1461 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1462 * It means, that we can modify tree during walking
1463 * and use this function for garbage collection, clone pruning,
1464 * cleaning tree when a device goes down etc. etc.
1466 * It guarantees that every node will be traversed,
1467 * and that it will be traversed only once.
1469 * Callback function w->func may return:
1470 * 0 -> continue walking.
1471 * positive value -> walking is suspended (used by tree dumps,
1472 * and probably by gc, if it will be split to several slices)
1473 * negative value -> terminate walking.
1475 * The function itself returns:
1476 * 0 -> walk is complete.
1477 * >0 -> walk is incomplete (i.e. suspended)
1478 * <0 -> walk is terminated by an error.
1481 static int fib6_walk_continue(struct fib6_walker
*w
)
1483 struct fib6_node
*fn
, *pn
;
1490 if (w
->prune
&& fn
!= w
->root
&&
1491 fn
->fn_flags
& RTN_RTINFO
&& w
->state
< FWS_C
) {
1496 #ifdef CONFIG_IPV6_SUBTREES
1498 if (FIB6_SUBTREE(fn
)) {
1499 w
->node
= FIB6_SUBTREE(fn
);
1507 w
->state
= FWS_INIT
;
1513 w
->node
= fn
->right
;
1514 w
->state
= FWS_INIT
;
1520 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1542 #ifdef CONFIG_IPV6_SUBTREES
1543 if (FIB6_SUBTREE(pn
) == fn
) {
1544 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1549 if (pn
->left
== fn
) {
1553 if (pn
->right
== fn
) {
1555 w
->leaf
= w
->node
->leaf
;
1565 static int fib6_walk(struct fib6_walker
*w
)
1569 w
->state
= FWS_INIT
;
1572 fib6_walker_link(w
);
1573 res
= fib6_walk_continue(w
);
1575 fib6_walker_unlink(w
);
1579 static int fib6_clean_node(struct fib6_walker
*w
)
1582 struct rt6_info
*rt
;
1583 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1584 struct nl_info info
= {
1588 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1589 w
->node
->fn_sernum
!= c
->sernum
)
1590 w
->node
->fn_sernum
= c
->sernum
;
1593 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1598 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1599 res
= c
->func(rt
, c
->arg
);
1602 res
= fib6_del(rt
, &info
);
1605 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1606 __func__
, rt
, rt
->rt6i_node
, res
);
1619 * Convenient frontend to tree walker.
1621 * func is called on each route.
1622 * It may return -1 -> delete this route.
1623 * 0 -> continue walking
1625 * prune==1 -> only immediate children of node (certainly,
1626 * ignoring pure split nodes) will be scanned.
1629 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1630 int (*func
)(struct rt6_info
*, void *arg
),
1631 bool prune
, int sernum
, void *arg
)
1633 struct fib6_cleaner c
;
1636 c
.w
.func
= fib6_clean_node
;
1648 static void __fib6_clean_all(struct net
*net
,
1649 int (*func
)(struct rt6_info
*, void *),
1650 int sernum
, void *arg
)
1652 struct fib6_table
*table
;
1653 struct hlist_head
*head
;
1657 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1658 head
= &net
->ipv6
.fib_table_hash
[h
];
1659 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1660 write_lock_bh(&table
->tb6_lock
);
1661 fib6_clean_tree(net
, &table
->tb6_root
,
1662 func
, false, sernum
, arg
);
1663 write_unlock_bh(&table
->tb6_lock
);
1669 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *),
1672 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
);
1675 static int fib6_prune_clone(struct rt6_info
*rt
, void *arg
)
1677 if (rt
->rt6i_flags
& RTF_CACHE
) {
1678 RT6_TRACE("pruning clone %p\n", rt
);
1685 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
)
1687 fib6_clean_tree(net
, fn
, fib6_prune_clone
, true,
1688 FIB6_NO_SERNUM_CHANGE
, NULL
);
1691 static void fib6_flush_trees(struct net
*net
)
1693 int new_sernum
= fib6_new_sernum(net
);
1695 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
);
1699 * Garbage collection
1702 static struct fib6_gc_args
1708 static int fib6_age(struct rt6_info
*rt
, void *arg
)
1710 unsigned long now
= jiffies
;
1713 * check addrconf expiration here.
1714 * Routes are expired even if they are in use.
1716 * Also age clones. Note, that clones are aged out
1717 * only if they are not in use now.
1720 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
1721 if (time_after(now
, rt
->dst
.expires
)) {
1722 RT6_TRACE("expiring %p\n", rt
);
1726 } else if (rt
->rt6i_flags
& RTF_CACHE
) {
1727 if (atomic_read(&rt
->dst
.__refcnt
) == 0 &&
1728 time_after_eq(now
, rt
->dst
.lastuse
+ gc_args
.timeout
)) {
1729 RT6_TRACE("aging clone %p\n", rt
);
1731 } else if (rt
->rt6i_flags
& RTF_GATEWAY
) {
1732 struct neighbour
*neigh
;
1733 __u8 neigh_flags
= 0;
1735 neigh
= dst_neigh_lookup(&rt
->dst
, &rt
->rt6i_gateway
);
1737 neigh_flags
= neigh
->flags
;
1738 neigh_release(neigh
);
1740 if (!(neigh_flags
& NTF_ROUTER
)) {
1741 RT6_TRACE("purging route %p via non-router but gateway\n",
1752 static DEFINE_SPINLOCK(fib6_gc_lock
);
1754 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
1759 spin_lock_bh(&fib6_gc_lock
);
1760 } else if (!spin_trylock_bh(&fib6_gc_lock
)) {
1761 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
1764 gc_args
.timeout
= expires
? (int)expires
:
1765 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
1767 gc_args
.more
= icmp6_dst_gc();
1769 fib6_clean_all(net
, fib6_age
, NULL
);
1771 net
->ipv6
.ip6_rt_last_gc
= now
;
1774 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1776 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
1778 del_timer(&net
->ipv6
.ip6_fib_timer
);
1779 spin_unlock_bh(&fib6_gc_lock
);
1782 static void fib6_gc_timer_cb(unsigned long arg
)
1784 fib6_run_gc(0, (struct net
*)arg
, true);
1787 static int __net_init
fib6_net_init(struct net
*net
)
1789 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
1791 setup_timer(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, (unsigned long)net
);
1793 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
1794 if (!net
->ipv6
.rt6_stats
)
1797 /* Avoid false sharing : Use at least a full cache line */
1798 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
1800 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
1801 if (!net
->ipv6
.fib_table_hash
)
1804 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
1806 if (!net
->ipv6
.fib6_main_tbl
)
1807 goto out_fib_table_hash
;
1809 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
1810 net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1811 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
1812 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1813 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1815 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1816 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
1818 if (!net
->ipv6
.fib6_local_tbl
)
1819 goto out_fib6_main_tbl
;
1820 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
1821 net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1822 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
1823 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1824 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1826 fib6_tables_init(net
);
1830 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1832 kfree(net
->ipv6
.fib6_main_tbl
);
1835 kfree(net
->ipv6
.fib_table_hash
);
1837 kfree(net
->ipv6
.rt6_stats
);
1842 static void fib6_net_exit(struct net
*net
)
1844 rt6_ifdown(net
, NULL
);
1845 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
1847 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1848 inetpeer_invalidate_tree(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1849 kfree(net
->ipv6
.fib6_local_tbl
);
1851 inetpeer_invalidate_tree(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1852 kfree(net
->ipv6
.fib6_main_tbl
);
1853 kfree(net
->ipv6
.fib_table_hash
);
1854 kfree(net
->ipv6
.rt6_stats
);
1857 static struct pernet_operations fib6_net_ops
= {
1858 .init
= fib6_net_init
,
1859 .exit
= fib6_net_exit
,
1862 int __init
fib6_init(void)
1866 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
1867 sizeof(struct fib6_node
),
1868 0, SLAB_HWCACHE_ALIGN
,
1870 if (!fib6_node_kmem
)
1873 ret
= register_pernet_subsys(&fib6_net_ops
);
1875 goto out_kmem_cache_create
;
1877 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
1880 goto out_unregister_subsys
;
1882 __fib6_flush_trees
= fib6_flush_trees
;
1886 out_unregister_subsys
:
1887 unregister_pernet_subsys(&fib6_net_ops
);
1888 out_kmem_cache_create
:
1889 kmem_cache_destroy(fib6_node_kmem
);
1893 void fib6_gc_cleanup(void)
1895 unregister_pernet_subsys(&fib6_net_ops
);
1896 kmem_cache_destroy(fib6_node_kmem
);
1899 #ifdef CONFIG_PROC_FS
1901 struct ipv6_route_iter
{
1902 struct seq_net_private p
;
1903 struct fib6_walker w
;
1905 struct fib6_table
*tbl
;
1909 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
1911 struct rt6_info
*rt
= v
;
1912 struct ipv6_route_iter
*iter
= seq
->private;
1914 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
1916 #ifdef CONFIG_IPV6_SUBTREES
1917 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
1919 seq_puts(seq
, "00000000000000000000000000000000 00 ");
1921 if (rt
->rt6i_flags
& RTF_GATEWAY
)
1922 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
1924 seq_puts(seq
, "00000000000000000000000000000000");
1926 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
1927 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
1928 rt
->dst
.__use
, rt
->rt6i_flags
,
1929 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
1930 iter
->w
.leaf
= NULL
;
1934 static int ipv6_route_yield(struct fib6_walker
*w
)
1936 struct ipv6_route_iter
*iter
= w
->args
;
1942 iter
->w
.leaf
= iter
->w
.leaf
->dst
.rt6_next
;
1944 if (!iter
->skip
&& iter
->w
.leaf
)
1946 } while (iter
->w
.leaf
);
1951 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
)
1953 memset(&iter
->w
, 0, sizeof(iter
->w
));
1954 iter
->w
.func
= ipv6_route_yield
;
1955 iter
->w
.root
= &iter
->tbl
->tb6_root
;
1956 iter
->w
.state
= FWS_INIT
;
1957 iter
->w
.node
= iter
->w
.root
;
1958 iter
->w
.args
= iter
;
1959 iter
->sernum
= iter
->w
.root
->fn_sernum
;
1960 INIT_LIST_HEAD(&iter
->w
.lh
);
1961 fib6_walker_link(&iter
->w
);
1964 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
1968 struct hlist_node
*node
;
1971 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
1972 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
1978 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
1979 node
= rcu_dereference_bh(
1980 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
1982 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
1985 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
1987 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
1988 iter
->sernum
= iter
->w
.root
->fn_sernum
;
1989 iter
->w
.state
= FWS_INIT
;
1990 iter
->w
.node
= iter
->w
.root
;
1991 WARN_ON(iter
->w
.skip
);
1992 iter
->w
.skip
= iter
->w
.count
;
1996 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2000 struct net
*net
= seq_file_net(seq
);
2001 struct ipv6_route_iter
*iter
= seq
->private;
2006 n
= ((struct rt6_info
*)v
)->dst
.rt6_next
;
2013 ipv6_route_check_sernum(iter
);
2014 read_lock(&iter
->tbl
->tb6_lock
);
2015 r
= fib6_walk_continue(&iter
->w
);
2016 read_unlock(&iter
->tbl
->tb6_lock
);
2020 return iter
->w
.leaf
;
2022 fib6_walker_unlink(&iter
->w
);
2025 fib6_walker_unlink(&iter
->w
);
2027 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2031 ipv6_route_seq_setup_walk(iter
);
2035 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2038 struct net
*net
= seq_file_net(seq
);
2039 struct ipv6_route_iter
*iter
= seq
->private;
2042 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2046 ipv6_route_seq_setup_walk(iter
);
2047 return ipv6_route_seq_next(seq
, NULL
, pos
);
2053 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2055 struct fib6_walker
*w
= &iter
->w
;
2056 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2059 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2062 struct ipv6_route_iter
*iter
= seq
->private;
2064 if (ipv6_route_iter_active(iter
))
2065 fib6_walker_unlink(&iter
->w
);
2067 rcu_read_unlock_bh();
2070 static const struct seq_operations ipv6_route_seq_ops
= {
2071 .start
= ipv6_route_seq_start
,
2072 .next
= ipv6_route_seq_next
,
2073 .stop
= ipv6_route_seq_stop
,
2074 .show
= ipv6_route_seq_show
2077 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2079 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2080 sizeof(struct ipv6_route_iter
));
2083 #endif /* CONFIG_PROC_FS */