1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux INET6 implementation
4 * Forwarding Information Database
7 * Pedro Roque <roque@di.fc.ul.pt>
10 * Yuji SEKIYA @USAGI: Support default route on router node;
11 * remove ip6_null_entry from the top of
13 * Ville Nuorvala: Fixed routing subtrees.
16 #define pr_fmt(fmt) "IPv6: " fmt
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/net.h>
21 #include <linux/route.h>
22 #include <linux/netdevice.h>
23 #include <linux/in6.h>
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/slab.h>
30 #include <net/ndisc.h>
31 #include <net/addrconf.h>
32 #include <net/lwtunnel.h>
33 #include <net/fib_notifier.h>
35 #include <net/ip6_fib.h>
36 #include <net/ip6_route.h>
38 static struct kmem_cache
*fib6_node_kmem __read_mostly
;
43 int (*func
)(struct fib6_info
*, void *arg
);
49 #ifdef CONFIG_IPV6_SUBTREES
50 #define FWS_INIT FWS_S
52 #define FWS_INIT FWS_L
55 static struct fib6_info
*fib6_find_prefix(struct net
*net
,
56 struct fib6_table
*table
,
57 struct fib6_node
*fn
);
58 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
59 struct fib6_table
*table
,
60 struct fib6_node
*fn
);
61 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
);
62 static int fib6_walk_continue(struct fib6_walker
*w
);
65 * A routing update causes an increase of the serial number on the
66 * affected subtree. This allows for cached routes to be asynchronously
67 * tested when modifications are made to the destination cache as a
68 * result of redirects, path MTU changes, etc.
71 static void fib6_gc_timer_cb(struct timer_list
*t
);
73 #define FOR_WALKERS(net, w) \
74 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
76 static void fib6_walker_link(struct net
*net
, struct fib6_walker
*w
)
78 write_lock_bh(&net
->ipv6
.fib6_walker_lock
);
79 list_add(&w
->lh
, &net
->ipv6
.fib6_walkers
);
80 write_unlock_bh(&net
->ipv6
.fib6_walker_lock
);
83 static void fib6_walker_unlink(struct net
*net
, struct fib6_walker
*w
)
85 write_lock_bh(&net
->ipv6
.fib6_walker_lock
);
87 write_unlock_bh(&net
->ipv6
.fib6_walker_lock
);
90 static int fib6_new_sernum(struct net
*net
)
95 old
= atomic_read(&net
->ipv6
.fib6_sernum
);
96 new = old
< INT_MAX
? old
+ 1 : 1;
97 } while (atomic_cmpxchg(&net
->ipv6
.fib6_sernum
,
103 FIB6_NO_SERNUM_CHANGE
= 0,
106 void fib6_update_sernum(struct net
*net
, struct fib6_info
*f6i
)
108 struct fib6_node
*fn
;
110 fn
= rcu_dereference_protected(f6i
->fib6_node
,
111 lockdep_is_held(&f6i
->fib6_table
->tb6_lock
));
113 fn
->fn_sernum
= fib6_new_sernum(net
);
117 * Auxiliary address test functions for the radix tree.
119 * These assume a 32bit processor (although it will work on
126 #if defined(__LITTLE_ENDIAN)
127 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
129 # define BITOP_BE32_SWIZZLE 0
132 static __be32
addr_bit_set(const void *token
, int fn_bit
)
134 const __be32
*addr
= token
;
137 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
138 * is optimized version of
139 * htonl(1 << ((~fn_bit)&0x1F))
140 * See include/asm-generic/bitops/le.h.
142 return (__force __be32
)(1 << ((~fn_bit
^ BITOP_BE32_SWIZZLE
) & 0x1f)) &
146 struct fib6_info
*fib6_info_alloc(gfp_t gfp_flags
, bool with_fib6_nh
)
148 struct fib6_info
*f6i
;
149 size_t sz
= sizeof(*f6i
);
152 sz
+= sizeof(struct fib6_nh
);
154 f6i
= kzalloc(sz
, gfp_flags
);
158 /* fib6_siblings is a union with nh_list, so this initializes both */
159 INIT_LIST_HEAD(&f6i
->fib6_siblings
);
160 refcount_set(&f6i
->fib6_ref
, 1);
165 void fib6_info_destroy_rcu(struct rcu_head
*head
)
167 struct fib6_info
*f6i
= container_of(head
, struct fib6_info
, rcu
);
169 WARN_ON(f6i
->fib6_node
);
172 nexthop_put(f6i
->nh
);
174 fib6_nh_release(f6i
->fib6_nh
);
176 ip_fib_metrics_put(f6i
->fib6_metrics
);
179 EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu
);
181 static struct fib6_node
*node_alloc(struct net
*net
)
183 struct fib6_node
*fn
;
185 fn
= kmem_cache_zalloc(fib6_node_kmem
, GFP_ATOMIC
);
187 net
->ipv6
.rt6_stats
->fib_nodes
++;
192 static void node_free_immediate(struct net
*net
, struct fib6_node
*fn
)
194 kmem_cache_free(fib6_node_kmem
, fn
);
195 net
->ipv6
.rt6_stats
->fib_nodes
--;
198 static void node_free_rcu(struct rcu_head
*head
)
200 struct fib6_node
*fn
= container_of(head
, struct fib6_node
, rcu
);
202 kmem_cache_free(fib6_node_kmem
, fn
);
205 static void node_free(struct net
*net
, struct fib6_node
*fn
)
207 call_rcu(&fn
->rcu
, node_free_rcu
);
208 net
->ipv6
.rt6_stats
->fib_nodes
--;
211 static void fib6_free_table(struct fib6_table
*table
)
213 inetpeer_invalidate_tree(&table
->tb6_peers
);
217 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
222 * Initialize table lock at a single place to give lockdep a key,
223 * tables aren't visible prior to being linked to the list.
225 spin_lock_init(&tb
->tb6_lock
);
226 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
229 * No protection necessary, this is the only list mutatation
230 * operation, tables never disappear once they exist.
232 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
235 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
237 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
239 struct fib6_table
*table
;
241 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
244 rcu_assign_pointer(table
->tb6_root
.leaf
,
245 net
->ipv6
.fib6_null_entry
);
246 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
247 inet_peer_base_init(&table
->tb6_peers
);
253 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
255 struct fib6_table
*tb
;
259 tb
= fib6_get_table(net
, id
);
263 tb
= fib6_alloc_table(net
, id
);
265 fib6_link_table(net
, tb
);
269 EXPORT_SYMBOL_GPL(fib6_new_table
);
271 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
273 struct fib6_table
*tb
;
274 struct hlist_head
*head
;
279 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
281 head
= &net
->ipv6
.fib_table_hash
[h
];
282 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
283 if (tb
->tb6_id
== id
) {
292 EXPORT_SYMBOL_GPL(fib6_get_table
);
294 static void __net_init
fib6_tables_init(struct net
*net
)
296 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
297 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
301 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
303 return fib6_get_table(net
, id
);
306 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
308 return net
->ipv6
.fib6_main_tbl
;
311 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
312 const struct sk_buff
*skb
,
313 int flags
, pol_lookup_t lookup
)
317 rt
= lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, skb
, flags
);
318 if (rt
->dst
.error
== -EAGAIN
) {
319 ip6_rt_put_flags(rt
, flags
);
320 rt
= net
->ipv6
.ip6_null_entry
;
321 if (!(flags
& RT6_LOOKUP_F_DST_NOREF
))
328 /* called with rcu lock held; no reference taken on fib6_info */
329 int fib6_lookup(struct net
*net
, int oif
, struct flowi6
*fl6
,
330 struct fib6_result
*res
, int flags
)
332 return fib6_table_lookup(net
, net
->ipv6
.fib6_main_tbl
, oif
, fl6
,
336 static void __net_init
fib6_tables_init(struct net
*net
)
338 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
343 unsigned int fib6_tables_seq_read(struct net
*net
)
345 unsigned int h
, fib_seq
= 0;
348 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
349 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[h
];
350 struct fib6_table
*tb
;
352 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
)
353 fib_seq
+= tb
->fib_seq
;
360 static int call_fib6_entry_notifier(struct notifier_block
*nb
,
361 enum fib_event_type event_type
,
362 struct fib6_info
*rt
,
363 struct netlink_ext_ack
*extack
)
365 struct fib6_entry_notifier_info info
= {
366 .info
.extack
= extack
,
370 return call_fib6_notifier(nb
, event_type
, &info
.info
);
373 static int call_fib6_multipath_entry_notifier(struct notifier_block
*nb
,
374 enum fib_event_type event_type
,
375 struct fib6_info
*rt
,
376 unsigned int nsiblings
,
377 struct netlink_ext_ack
*extack
)
379 struct fib6_entry_notifier_info info
= {
380 .info
.extack
= extack
,
382 .nsiblings
= nsiblings
,
385 return call_fib6_notifier(nb
, event_type
, &info
.info
);
388 int call_fib6_entry_notifiers(struct net
*net
,
389 enum fib_event_type event_type
,
390 struct fib6_info
*rt
,
391 struct netlink_ext_ack
*extack
)
393 struct fib6_entry_notifier_info info
= {
394 .info
.extack
= extack
,
398 rt
->fib6_table
->fib_seq
++;
399 return call_fib6_notifiers(net
, event_type
, &info
.info
);
402 int call_fib6_multipath_entry_notifiers(struct net
*net
,
403 enum fib_event_type event_type
,
404 struct fib6_info
*rt
,
405 unsigned int nsiblings
,
406 struct netlink_ext_ack
*extack
)
408 struct fib6_entry_notifier_info info
= {
409 .info
.extack
= extack
,
411 .nsiblings
= nsiblings
,
414 rt
->fib6_table
->fib_seq
++;
415 return call_fib6_notifiers(net
, event_type
, &info
.info
);
418 int call_fib6_entry_notifiers_replace(struct net
*net
, struct fib6_info
*rt
)
420 struct fib6_entry_notifier_info info
= {
422 .nsiblings
= rt
->fib6_nsiblings
,
425 rt
->fib6_table
->fib_seq
++;
426 return call_fib6_notifiers(net
, FIB_EVENT_ENTRY_REPLACE
, &info
.info
);
429 struct fib6_dump_arg
{
431 struct notifier_block
*nb
;
432 struct netlink_ext_ack
*extack
;
435 static int fib6_rt_dump(struct fib6_info
*rt
, struct fib6_dump_arg
*arg
)
437 enum fib_event_type fib_event
= FIB_EVENT_ENTRY_REPLACE
;
440 if (!rt
|| rt
== arg
->net
->ipv6
.fib6_null_entry
)
443 if (rt
->fib6_nsiblings
)
444 err
= call_fib6_multipath_entry_notifier(arg
->nb
, fib_event
,
449 err
= call_fib6_entry_notifier(arg
->nb
, fib_event
, rt
,
455 static int fib6_node_dump(struct fib6_walker
*w
)
459 err
= fib6_rt_dump(w
->leaf
, w
->args
);
464 static int fib6_table_dump(struct net
*net
, struct fib6_table
*tb
,
465 struct fib6_walker
*w
)
469 w
->root
= &tb
->tb6_root
;
470 spin_lock_bh(&tb
->tb6_lock
);
471 err
= fib6_walk(net
, w
);
472 spin_unlock_bh(&tb
->tb6_lock
);
476 /* Called with rcu_read_lock() */
477 int fib6_tables_dump(struct net
*net
, struct notifier_block
*nb
,
478 struct netlink_ext_ack
*extack
)
480 struct fib6_dump_arg arg
;
481 struct fib6_walker
*w
;
485 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
489 w
->func
= fib6_node_dump
;
495 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
496 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[h
];
497 struct fib6_table
*tb
;
499 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
500 err
= fib6_table_dump(net
, tb
, w
);
512 static int fib6_dump_node(struct fib6_walker
*w
)
515 struct fib6_info
*rt
;
517 for_each_fib6_walker_rt(w
) {
518 res
= rt6_dump_route(rt
, w
->args
, w
->skip_in_node
);
520 /* Frame is full, suspend walking */
523 /* We'll restart from this node, so if some routes were
524 * already dumped, skip them next time.
526 w
->skip_in_node
+= res
;
532 /* Multipath routes are dumped in one route with the
533 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
534 * last sibling of this route (no need to dump the
535 * sibling routes again)
537 if (rt
->fib6_nsiblings
)
538 rt
= list_last_entry(&rt
->fib6_siblings
,
546 static void fib6_dump_end(struct netlink_callback
*cb
)
548 struct net
*net
= sock_net(cb
->skb
->sk
);
549 struct fib6_walker
*w
= (void *)cb
->args
[2];
554 fib6_walker_unlink(net
, w
);
559 cb
->done
= (void *)cb
->args
[3];
563 static int fib6_dump_done(struct netlink_callback
*cb
)
566 return cb
->done
? cb
->done(cb
) : 0;
569 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
570 struct netlink_callback
*cb
)
572 struct net
*net
= sock_net(skb
->sk
);
573 struct fib6_walker
*w
;
576 w
= (void *)cb
->args
[2];
577 w
->root
= &table
->tb6_root
;
579 if (cb
->args
[4] == 0) {
584 spin_lock_bh(&table
->tb6_lock
);
585 res
= fib6_walk(net
, w
);
586 spin_unlock_bh(&table
->tb6_lock
);
589 cb
->args
[5] = w
->root
->fn_sernum
;
592 if (cb
->args
[5] != w
->root
->fn_sernum
) {
593 /* Begin at the root if the tree changed */
594 cb
->args
[5] = w
->root
->fn_sernum
;
602 spin_lock_bh(&table
->tb6_lock
);
603 res
= fib6_walk_continue(w
);
604 spin_unlock_bh(&table
->tb6_lock
);
606 fib6_walker_unlink(net
, w
);
614 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
616 struct rt6_rtnl_dump_arg arg
= { .filter
.dump_exceptions
= true,
617 .filter
.dump_routes
= true };
618 const struct nlmsghdr
*nlh
= cb
->nlh
;
619 struct net
*net
= sock_net(skb
->sk
);
621 unsigned int e
= 0, s_e
;
622 struct fib6_walker
*w
;
623 struct fib6_table
*tb
;
624 struct hlist_head
*head
;
627 if (cb
->strict_check
) {
630 err
= ip_valid_fib_dump_req(net
, nlh
, &arg
.filter
, cb
);
633 } else if (nlmsg_len(nlh
) >= sizeof(struct rtmsg
)) {
634 struct rtmsg
*rtm
= nlmsg_data(nlh
);
636 if (rtm
->rtm_flags
& RTM_F_PREFIX
)
637 arg
.filter
.flags
= RTM_F_PREFIX
;
640 w
= (void *)cb
->args
[2];
644 * 1. hook callback destructor.
646 cb
->args
[3] = (long)cb
->done
;
647 cb
->done
= fib6_dump_done
;
650 * 2. allocate and initialize walker.
652 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
655 w
->func
= fib6_dump_node
;
656 cb
->args
[2] = (long)w
;
664 if (arg
.filter
.table_id
) {
665 tb
= fib6_get_table(net
, arg
.filter
.table_id
);
667 if (arg
.filter
.dump_all_families
)
670 NL_SET_ERR_MSG_MOD(cb
->extack
, "FIB table does not exist");
675 res
= fib6_dump_table(tb
, skb
, cb
);
686 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
688 head
= &net
->ipv6
.fib_table_hash
[h
];
689 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
692 res
= fib6_dump_table(tb
, skb
, cb
);
704 res
= res
< 0 ? res
: skb
->len
;
710 void fib6_metric_set(struct fib6_info
*f6i
, int metric
, u32 val
)
715 if (f6i
->fib6_metrics
== &dst_default_metrics
) {
716 struct dst_metrics
*p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
721 refcount_set(&p
->refcnt
, 1);
722 f6i
->fib6_metrics
= p
;
725 f6i
->fib6_metrics
->metrics
[metric
- 1] = val
;
731 * return the appropriate node for a routing tree "add" operation
732 * by either creating and inserting or by returning an existing
736 static struct fib6_node
*fib6_add_1(struct net
*net
,
737 struct fib6_table
*table
,
738 struct fib6_node
*root
,
739 struct in6_addr
*addr
, int plen
,
740 int offset
, int allow_create
,
741 int replace_required
,
742 struct netlink_ext_ack
*extack
)
744 struct fib6_node
*fn
, *in
, *ln
;
745 struct fib6_node
*pn
= NULL
;
750 RT6_TRACE("fib6_add_1\n");
752 /* insert node in tree */
757 struct fib6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
758 lockdep_is_held(&table
->tb6_lock
));
759 key
= (struct rt6key
*)((u8
*)leaf
+ offset
);
764 if (plen
< fn
->fn_bit
||
765 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
767 if (replace_required
) {
768 NL_SET_ERR_MSG(extack
,
769 "Can not replace route - no match found");
770 pr_warn("Can't replace route, no match found\n");
771 return ERR_PTR(-ENOENT
);
773 pr_warn("NLM_F_CREATE should be set when creating new route\n");
782 if (plen
== fn
->fn_bit
) {
783 /* clean up an intermediate node */
784 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
785 RCU_INIT_POINTER(fn
->leaf
, NULL
);
786 fib6_info_release(leaf
);
787 /* remove null_entry in the root node */
788 } else if (fn
->fn_flags
& RTN_TL_ROOT
&&
789 rcu_access_pointer(fn
->leaf
) ==
790 net
->ipv6
.fib6_null_entry
) {
791 RCU_INIT_POINTER(fn
->leaf
, NULL
);
798 * We have more bits to go
801 /* Try to walk down on tree. */
802 dir
= addr_bit_set(addr
, fn
->fn_bit
);
805 rcu_dereference_protected(fn
->right
,
806 lockdep_is_held(&table
->tb6_lock
)) :
807 rcu_dereference_protected(fn
->left
,
808 lockdep_is_held(&table
->tb6_lock
));
812 /* We should not create new node because
813 * NLM_F_REPLACE was specified without NLM_F_CREATE
814 * I assume it is safe to require NLM_F_CREATE when
815 * REPLACE flag is used! Later we may want to remove the
816 * check for replace_required, because according
817 * to netlink specification, NLM_F_CREATE
818 * MUST be specified if new route is created.
819 * That would keep IPv6 consistent with IPv4
821 if (replace_required
) {
822 NL_SET_ERR_MSG(extack
,
823 "Can not replace route - no match found");
824 pr_warn("Can't replace route, no match found\n");
825 return ERR_PTR(-ENOENT
);
827 pr_warn("NLM_F_CREATE should be set when creating new route\n");
830 * We walked to the bottom of tree.
831 * Create new leaf node without children.
834 ln
= node_alloc(net
);
837 return ERR_PTR(-ENOMEM
);
839 RCU_INIT_POINTER(ln
->parent
, pn
);
842 rcu_assign_pointer(pn
->right
, ln
);
844 rcu_assign_pointer(pn
->left
, ln
);
851 * split since we don't have a common prefix anymore or
852 * we have a less significant route.
853 * we've to insert an intermediate node on the list
854 * this new node will point to the one we need to create
858 pn
= rcu_dereference_protected(fn
->parent
,
859 lockdep_is_held(&table
->tb6_lock
));
861 /* find 1st bit in difference between the 2 addrs.
863 See comment in __ipv6_addr_diff: bit may be an invalid value,
864 but if it is >= plen, the value is ignored in any case.
867 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
872 * (new leaf node)[ln] (old node)[fn]
875 in
= node_alloc(net
);
876 ln
= node_alloc(net
);
880 node_free_immediate(net
, in
);
882 node_free_immediate(net
, ln
);
883 return ERR_PTR(-ENOMEM
);
887 * new intermediate node.
889 * be off since that an address that chooses one of
890 * the branches would not match less specific routes
891 * in the other branch
896 RCU_INIT_POINTER(in
->parent
, pn
);
898 fib6_info_hold(rcu_dereference_protected(in
->leaf
,
899 lockdep_is_held(&table
->tb6_lock
)));
901 /* update parent pointer */
903 rcu_assign_pointer(pn
->right
, in
);
905 rcu_assign_pointer(pn
->left
, in
);
909 RCU_INIT_POINTER(ln
->parent
, in
);
910 rcu_assign_pointer(fn
->parent
, in
);
912 if (addr_bit_set(addr
, bit
)) {
913 rcu_assign_pointer(in
->right
, ln
);
914 rcu_assign_pointer(in
->left
, fn
);
916 rcu_assign_pointer(in
->left
, ln
);
917 rcu_assign_pointer(in
->right
, fn
);
919 } else { /* plen <= bit */
922 * (new leaf node)[ln]
924 * (old node)[fn] NULL
927 ln
= node_alloc(net
);
930 return ERR_PTR(-ENOMEM
);
934 RCU_INIT_POINTER(ln
->parent
, pn
);
936 if (addr_bit_set(&key
->addr
, plen
))
937 RCU_INIT_POINTER(ln
->right
, fn
);
939 RCU_INIT_POINTER(ln
->left
, fn
);
941 rcu_assign_pointer(fn
->parent
, ln
);
944 rcu_assign_pointer(pn
->right
, ln
);
946 rcu_assign_pointer(pn
->left
, ln
);
951 static void __fib6_drop_pcpu_from(struct fib6_nh
*fib6_nh
,
952 const struct fib6_info
*match
,
953 const struct fib6_table
*table
)
957 if (!fib6_nh
->rt6i_pcpu
)
960 /* release the reference to this fib entry from
961 * all of its cached pcpu routes
963 for_each_possible_cpu(cpu
) {
964 struct rt6_info
**ppcpu_rt
;
965 struct rt6_info
*pcpu_rt
;
967 ppcpu_rt
= per_cpu_ptr(fib6_nh
->rt6i_pcpu
, cpu
);
970 /* only dropping the 'from' reference if the cached route
971 * is using 'match'. The cached pcpu_rt->from only changes
972 * from a fib6_info to NULL (ip6_dst_destroy); it can never
973 * change from one fib6_info reference to another
975 if (pcpu_rt
&& rcu_access_pointer(pcpu_rt
->from
) == match
) {
976 struct fib6_info
*from
;
978 from
= xchg((__force
struct fib6_info
**)&pcpu_rt
->from
, NULL
);
979 fib6_info_release(from
);
984 struct fib6_nh_pcpu_arg
{
985 struct fib6_info
*from
;
986 const struct fib6_table
*table
;
989 static int fib6_nh_drop_pcpu_from(struct fib6_nh
*nh
, void *_arg
)
991 struct fib6_nh_pcpu_arg
*arg
= _arg
;
993 __fib6_drop_pcpu_from(nh
, arg
->from
, arg
->table
);
997 static void fib6_drop_pcpu_from(struct fib6_info
*f6i
,
998 const struct fib6_table
*table
)
1000 /* Make sure rt6_make_pcpu_route() wont add other percpu routes
1001 * while we are cleaning them here.
1003 f6i
->fib6_destroying
= 1;
1004 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
1007 struct fib6_nh_pcpu_arg arg
= {
1012 nexthop_for_each_fib6_nh(f6i
->nh
, fib6_nh_drop_pcpu_from
,
1015 struct fib6_nh
*fib6_nh
;
1017 fib6_nh
= f6i
->fib6_nh
;
1018 __fib6_drop_pcpu_from(fib6_nh
, f6i
, table
);
1022 static void fib6_purge_rt(struct fib6_info
*rt
, struct fib6_node
*fn
,
1025 struct fib6_table
*table
= rt
->fib6_table
;
1027 fib6_drop_pcpu_from(rt
, table
);
1029 if (rt
->nh
&& !list_empty(&rt
->nh_list
))
1030 list_del_init(&rt
->nh_list
);
1032 if (refcount_read(&rt
->fib6_ref
) != 1) {
1033 /* This route is used as dummy address holder in some split
1034 * nodes. It is not leaked, but it still holds other resources,
1035 * which must be released in time. So, scan ascendant nodes
1036 * and replace dummy references to this route with references
1037 * to still alive ones.
1040 struct fib6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
1041 lockdep_is_held(&table
->tb6_lock
));
1042 struct fib6_info
*new_leaf
;
1043 if (!(fn
->fn_flags
& RTN_RTINFO
) && leaf
== rt
) {
1044 new_leaf
= fib6_find_prefix(net
, table
, fn
);
1045 fib6_info_hold(new_leaf
);
1047 rcu_assign_pointer(fn
->leaf
, new_leaf
);
1048 fib6_info_release(rt
);
1050 fn
= rcu_dereference_protected(fn
->parent
,
1051 lockdep_is_held(&table
->tb6_lock
));
1057 * Insert routing information in a node.
1060 static int fib6_add_rt2node(struct fib6_node
*fn
, struct fib6_info
*rt
,
1061 struct nl_info
*info
,
1062 struct netlink_ext_ack
*extack
)
1064 struct fib6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
1065 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
1066 struct fib6_info
*iter
= NULL
;
1067 struct fib6_info __rcu
**ins
;
1068 struct fib6_info __rcu
**fallback_ins
= NULL
;
1069 int replace
= (info
->nlh
&&
1070 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
1071 int add
= (!info
->nlh
||
1072 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
1074 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
1075 bool notify_sibling_rt
= false;
1076 u16 nlflags
= NLM_F_EXCL
;
1079 if (info
->nlh
&& (info
->nlh
->nlmsg_flags
& NLM_F_APPEND
))
1080 nlflags
|= NLM_F_APPEND
;
1084 for (iter
= leaf
; iter
;
1085 iter
= rcu_dereference_protected(iter
->fib6_next
,
1086 lockdep_is_held(&rt
->fib6_table
->tb6_lock
))) {
1088 * Search for duplicates
1091 if (iter
->fib6_metric
== rt
->fib6_metric
) {
1093 * Same priority level
1096 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
1099 nlflags
&= ~NLM_F_EXCL
;
1101 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
1105 fallback_ins
= fallback_ins
?: ins
;
1109 if (rt6_duplicate_nexthop(iter
, rt
)) {
1110 if (rt
->fib6_nsiblings
)
1111 rt
->fib6_nsiblings
= 0;
1112 if (!(iter
->fib6_flags
& RTF_EXPIRES
))
1114 if (!(rt
->fib6_flags
& RTF_EXPIRES
))
1115 fib6_clean_expires(iter
);
1117 fib6_set_expires(iter
, rt
->expires
);
1120 fib6_metric_set(iter
, RTAX_MTU
,
1124 /* If we have the same destination and the same metric,
1125 * but not the same gateway, then the route we try to
1126 * add is sibling to this route, increment our counter
1127 * of siblings, and later we will add our route to the
1129 * Only static routes (which don't have flag
1130 * RTF_EXPIRES) are used for ECMPv6.
1132 * To avoid long list, we only had siblings if the
1133 * route have a gateway.
1136 rt6_qualify_for_ecmp(iter
))
1137 rt
->fib6_nsiblings
++;
1140 if (iter
->fib6_metric
> rt
->fib6_metric
)
1144 ins
= &iter
->fib6_next
;
1147 if (fallback_ins
&& !found
) {
1148 /* No matching route with same ecmp-able-ness found, replace
1149 * first matching route
1152 iter
= rcu_dereference_protected(*ins
,
1153 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
1157 /* Reset round-robin state, if necessary */
1158 if (ins
== &fn
->leaf
)
1161 /* Link this route to others same route. */
1162 if (rt
->fib6_nsiblings
) {
1163 unsigned int fib6_nsiblings
;
1164 struct fib6_info
*sibling
, *temp_sibling
;
1166 /* Find the first route that have the same metric */
1168 notify_sibling_rt
= true;
1170 if (sibling
->fib6_metric
== rt
->fib6_metric
&&
1171 rt6_qualify_for_ecmp(sibling
)) {
1172 list_add_tail(&rt
->fib6_siblings
,
1173 &sibling
->fib6_siblings
);
1176 sibling
= rcu_dereference_protected(sibling
->fib6_next
,
1177 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
1178 notify_sibling_rt
= false;
1180 /* For each sibling in the list, increment the counter of
1181 * siblings. BUG() if counters does not match, list of siblings
1185 list_for_each_entry_safe(sibling
, temp_sibling
,
1186 &rt
->fib6_siblings
, fib6_siblings
) {
1187 sibling
->fib6_nsiblings
++;
1188 BUG_ON(sibling
->fib6_nsiblings
!= rt
->fib6_nsiblings
);
1191 BUG_ON(fib6_nsiblings
!= rt
->fib6_nsiblings
);
1192 rt6_multipath_rebalance(temp_sibling
);
1200 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1203 nlflags
|= NLM_F_CREATE
;
1205 /* The route should only be notified if it is the first
1206 * route in the node or if it is added as a sibling
1207 * route to the first route in the node.
1209 if (!info
->skip_notify_kernel
&&
1210 (notify_sibling_rt
|| ins
== &fn
->leaf
)) {
1211 enum fib_event_type fib_event
;
1213 if (notify_sibling_rt
)
1214 fib_event
= FIB_EVENT_ENTRY_APPEND
;
1216 fib_event
= FIB_EVENT_ENTRY_REPLACE
;
1217 err
= call_fib6_entry_notifiers(info
->nl_net
,
1221 struct fib6_info
*sibling
, *next_sibling
;
1223 /* If the route has siblings, then it first
1224 * needs to be unlinked from them.
1226 if (!rt
->fib6_nsiblings
)
1229 list_for_each_entry_safe(sibling
, next_sibling
,
1232 sibling
->fib6_nsiblings
--;
1233 rt
->fib6_nsiblings
= 0;
1234 list_del_init(&rt
->fib6_siblings
);
1235 rt6_multipath_rebalance(next_sibling
);
1240 rcu_assign_pointer(rt
->fib6_next
, iter
);
1242 rcu_assign_pointer(rt
->fib6_node
, fn
);
1243 rcu_assign_pointer(*ins
, rt
);
1244 if (!info
->skip_notify
)
1245 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, nlflags
);
1246 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
1248 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
1249 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
1250 fn
->fn_flags
|= RTN_RTINFO
;
1259 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1263 if (!info
->skip_notify_kernel
&& ins
== &fn
->leaf
) {
1264 err
= call_fib6_entry_notifiers(info
->nl_net
,
1265 FIB_EVENT_ENTRY_REPLACE
,
1272 rcu_assign_pointer(rt
->fib6_node
, fn
);
1273 rt
->fib6_next
= iter
->fib6_next
;
1274 rcu_assign_pointer(*ins
, rt
);
1275 if (!info
->skip_notify
)
1276 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, NLM_F_REPLACE
);
1277 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
1278 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
1279 fn
->fn_flags
|= RTN_RTINFO
;
1281 nsiblings
= iter
->fib6_nsiblings
;
1282 iter
->fib6_node
= NULL
;
1283 fib6_purge_rt(iter
, fn
, info
->nl_net
);
1284 if (rcu_access_pointer(fn
->rr_ptr
) == iter
)
1286 fib6_info_release(iter
);
1289 /* Replacing an ECMP route, remove all siblings */
1290 ins
= &rt
->fib6_next
;
1291 iter
= rcu_dereference_protected(*ins
,
1292 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
1294 if (iter
->fib6_metric
> rt
->fib6_metric
)
1296 if (rt6_qualify_for_ecmp(iter
)) {
1297 *ins
= iter
->fib6_next
;
1298 iter
->fib6_node
= NULL
;
1299 fib6_purge_rt(iter
, fn
, info
->nl_net
);
1300 if (rcu_access_pointer(fn
->rr_ptr
) == iter
)
1302 fib6_info_release(iter
);
1304 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1306 ins
= &iter
->fib6_next
;
1308 iter
= rcu_dereference_protected(*ins
,
1309 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
1311 WARN_ON(nsiblings
!= 0);
1318 static void fib6_start_gc(struct net
*net
, struct fib6_info
*rt
)
1320 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
1321 (rt
->fib6_flags
& RTF_EXPIRES
))
1322 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1323 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
1326 void fib6_force_start_gc(struct net
*net
)
1328 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
1329 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1330 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
1333 static void __fib6_update_sernum_upto_root(struct fib6_info
*rt
,
1336 struct fib6_node
*fn
= rcu_dereference_protected(rt
->fib6_node
,
1337 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
1339 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1342 fn
->fn_sernum
= sernum
;
1343 fn
= rcu_dereference_protected(fn
->parent
,
1344 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
1348 void fib6_update_sernum_upto_root(struct net
*net
, struct fib6_info
*rt
)
1350 __fib6_update_sernum_upto_root(rt
, fib6_new_sernum(net
));
1353 /* allow ipv4 to update sernum via ipv6_stub */
1354 void fib6_update_sernum_stub(struct net
*net
, struct fib6_info
*f6i
)
1356 spin_lock_bh(&f6i
->fib6_table
->tb6_lock
);
1357 fib6_update_sernum_upto_root(net
, f6i
);
1358 spin_unlock_bh(&f6i
->fib6_table
->tb6_lock
);
1362 * Add routing information to the routing tree.
1363 * <destination addr>/<source addr>
1364 * with source addr info in sub-trees
1365 * Need to own table->tb6_lock
1368 int fib6_add(struct fib6_node
*root
, struct fib6_info
*rt
,
1369 struct nl_info
*info
, struct netlink_ext_ack
*extack
)
1371 struct fib6_table
*table
= rt
->fib6_table
;
1372 struct fib6_node
*fn
, *pn
= NULL
;
1374 int allow_create
= 1;
1375 int replace_required
= 0;
1376 int sernum
= fib6_new_sernum(info
->nl_net
);
1379 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
1381 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1382 replace_required
= 1;
1384 if (!allow_create
&& !replace_required
)
1385 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1387 fn
= fib6_add_1(info
->nl_net
, table
, root
,
1388 &rt
->fib6_dst
.addr
, rt
->fib6_dst
.plen
,
1389 offsetof(struct fib6_info
, fib6_dst
), allow_create
,
1390 replace_required
, extack
);
1399 #ifdef CONFIG_IPV6_SUBTREES
1400 if (rt
->fib6_src
.plen
) {
1401 struct fib6_node
*sn
;
1403 if (!rcu_access_pointer(fn
->subtree
)) {
1404 struct fib6_node
*sfn
;
1416 /* Create subtree root node */
1417 sfn
= node_alloc(info
->nl_net
);
1421 fib6_info_hold(info
->nl_net
->ipv6
.fib6_null_entry
);
1422 rcu_assign_pointer(sfn
->leaf
,
1423 info
->nl_net
->ipv6
.fib6_null_entry
);
1424 sfn
->fn_flags
= RTN_ROOT
;
1426 /* Now add the first leaf node to new subtree */
1428 sn
= fib6_add_1(info
->nl_net
, table
, sfn
,
1429 &rt
->fib6_src
.addr
, rt
->fib6_src
.plen
,
1430 offsetof(struct fib6_info
, fib6_src
),
1431 allow_create
, replace_required
, extack
);
1434 /* If it is failed, discard just allocated
1435 root, and then (in failure) stale node
1438 node_free_immediate(info
->nl_net
, sfn
);
1443 /* Now link new subtree to main tree */
1444 rcu_assign_pointer(sfn
->parent
, fn
);
1445 rcu_assign_pointer(fn
->subtree
, sfn
);
1447 sn
= fib6_add_1(info
->nl_net
, table
, FIB6_SUBTREE(fn
),
1448 &rt
->fib6_src
.addr
, rt
->fib6_src
.plen
,
1449 offsetof(struct fib6_info
, fib6_src
),
1450 allow_create
, replace_required
, extack
);
1458 if (!rcu_access_pointer(fn
->leaf
)) {
1459 if (fn
->fn_flags
& RTN_TL_ROOT
) {
1460 /* put back null_entry for root node */
1461 rcu_assign_pointer(fn
->leaf
,
1462 info
->nl_net
->ipv6
.fib6_null_entry
);
1465 rcu_assign_pointer(fn
->leaf
, rt
);
1472 err
= fib6_add_rt2node(fn
, rt
, info
, extack
);
1475 list_add(&rt
->nh_list
, &rt
->nh
->f6i_list
);
1476 __fib6_update_sernum_upto_root(rt
, sernum
);
1477 fib6_start_gc(info
->nl_net
, rt
);
1482 #ifdef CONFIG_IPV6_SUBTREES
1484 * If fib6_add_1 has cleared the old leaf pointer in the
1485 * super-tree leaf node we have to find a new one for it.
1488 struct fib6_info
*pn_leaf
=
1489 rcu_dereference_protected(pn
->leaf
,
1490 lockdep_is_held(&table
->tb6_lock
));
1491 if (pn_leaf
== rt
) {
1493 RCU_INIT_POINTER(pn
->leaf
, NULL
);
1494 fib6_info_release(rt
);
1496 if (!pn_leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
1497 pn_leaf
= fib6_find_prefix(info
->nl_net
, table
,
1503 info
->nl_net
->ipv6
.fib6_null_entry
;
1506 fib6_info_hold(pn_leaf
);
1507 rcu_assign_pointer(pn
->leaf
, pn_leaf
);
1512 } else if (fib6_requires_src(rt
)) {
1513 fib6_routes_require_src_inc(info
->nl_net
);
1518 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1519 * 1. fn is an intermediate node and we failed to add the new
1520 * route to it in both subtree creation failure and fib6_add_rt2node()
1522 * 2. fn is the root node in the table and we fail to add the first
1523 * default route to it.
1526 (!(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)) ||
1527 (fn
->fn_flags
& RTN_TL_ROOT
&&
1528 !rcu_access_pointer(fn
->leaf
))))
1529 fib6_repair_tree(info
->nl_net
, table
, fn
);
1534 * Routing tree lookup
1538 struct lookup_args
{
1539 int offset
; /* key offset on fib6_info */
1540 const struct in6_addr
*addr
; /* search key */
1543 static struct fib6_node
*fib6_node_lookup_1(struct fib6_node
*root
,
1544 struct lookup_args
*args
)
1546 struct fib6_node
*fn
;
1549 if (unlikely(args
->offset
== 0))
1559 struct fib6_node
*next
;
1561 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1563 next
= dir
? rcu_dereference(fn
->right
) :
1564 rcu_dereference(fn
->left
);
1574 struct fib6_node
*subtree
= FIB6_SUBTREE(fn
);
1576 if (subtree
|| fn
->fn_flags
& RTN_RTINFO
) {
1577 struct fib6_info
*leaf
= rcu_dereference(fn
->leaf
);
1583 key
= (struct rt6key
*) ((u8
*)leaf
+ args
->offset
);
1585 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1586 #ifdef CONFIG_IPV6_SUBTREES
1588 struct fib6_node
*sfn
;
1589 sfn
= fib6_node_lookup_1(subtree
,
1596 if (fn
->fn_flags
& RTN_RTINFO
)
1601 if (fn
->fn_flags
& RTN_ROOT
)
1604 fn
= rcu_dereference(fn
->parent
);
1610 /* called with rcu_read_lock() held
1612 struct fib6_node
*fib6_node_lookup(struct fib6_node
*root
,
1613 const struct in6_addr
*daddr
,
1614 const struct in6_addr
*saddr
)
1616 struct fib6_node
*fn
;
1617 struct lookup_args args
[] = {
1619 .offset
= offsetof(struct fib6_info
, fib6_dst
),
1622 #ifdef CONFIG_IPV6_SUBTREES
1624 .offset
= offsetof(struct fib6_info
, fib6_src
),
1629 .offset
= 0, /* sentinel */
1633 fn
= fib6_node_lookup_1(root
, daddr
? args
: args
+ 1);
1634 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1641 * Get node with specified destination prefix (and source prefix,
1642 * if subtrees are used)
1643 * exact_match == true means we try to find fn with exact match of
1644 * the passed in prefix addr
1645 * exact_match == false means we try to find fn with longest prefix
1646 * match of the passed in prefix addr. This is useful for finding fn
1647 * for cached route as it will be stored in the exception table under
1648 * the node with longest prefix length.
1652 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1653 const struct in6_addr
*addr
,
1654 int plen
, int offset
,
1657 struct fib6_node
*fn
, *prev
= NULL
;
1659 for (fn
= root
; fn
; ) {
1660 struct fib6_info
*leaf
= rcu_dereference(fn
->leaf
);
1663 /* This node is being deleted */
1665 if (plen
<= fn
->fn_bit
)
1671 key
= (struct rt6key
*)((u8
*)leaf
+ offset
);
1676 if (plen
< fn
->fn_bit
||
1677 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1680 if (plen
== fn
->fn_bit
)
1683 if (fn
->fn_flags
& RTN_RTINFO
)
1688 * We have more bits to go
1690 if (addr_bit_set(addr
, fn
->fn_bit
))
1691 fn
= rcu_dereference(fn
->right
);
1693 fn
= rcu_dereference(fn
->left
);
1702 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1703 const struct in6_addr
*daddr
, int dst_len
,
1704 const struct in6_addr
*saddr
, int src_len
,
1707 struct fib6_node
*fn
;
1709 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1710 offsetof(struct fib6_info
, fib6_dst
),
1713 #ifdef CONFIG_IPV6_SUBTREES
1715 WARN_ON(saddr
== NULL
);
1717 struct fib6_node
*subtree
= FIB6_SUBTREE(fn
);
1720 fn
= fib6_locate_1(subtree
, saddr
, src_len
,
1721 offsetof(struct fib6_info
, fib6_src
),
1728 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1740 static struct fib6_info
*fib6_find_prefix(struct net
*net
,
1741 struct fib6_table
*table
,
1742 struct fib6_node
*fn
)
1744 struct fib6_node
*child_left
, *child_right
;
1746 if (fn
->fn_flags
& RTN_ROOT
)
1747 return net
->ipv6
.fib6_null_entry
;
1750 child_left
= rcu_dereference_protected(fn
->left
,
1751 lockdep_is_held(&table
->tb6_lock
));
1752 child_right
= rcu_dereference_protected(fn
->right
,
1753 lockdep_is_held(&table
->tb6_lock
));
1755 return rcu_dereference_protected(child_left
->leaf
,
1756 lockdep_is_held(&table
->tb6_lock
));
1758 return rcu_dereference_protected(child_right
->leaf
,
1759 lockdep_is_held(&table
->tb6_lock
));
1761 fn
= FIB6_SUBTREE(fn
);
1767 * Called to trim the tree of intermediate nodes when possible. "fn"
1768 * is the node we want to try and remove.
1769 * Need to own table->tb6_lock
1772 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1773 struct fib6_table
*table
,
1774 struct fib6_node
*fn
)
1778 struct fib6_node
*child
;
1779 struct fib6_walker
*w
;
1782 /* Set fn->leaf to null_entry for root node. */
1783 if (fn
->fn_flags
& RTN_TL_ROOT
) {
1784 rcu_assign_pointer(fn
->leaf
, net
->ipv6
.fib6_null_entry
);
1789 struct fib6_node
*fn_r
= rcu_dereference_protected(fn
->right
,
1790 lockdep_is_held(&table
->tb6_lock
));
1791 struct fib6_node
*fn_l
= rcu_dereference_protected(fn
->left
,
1792 lockdep_is_held(&table
->tb6_lock
));
1793 struct fib6_node
*pn
= rcu_dereference_protected(fn
->parent
,
1794 lockdep_is_held(&table
->tb6_lock
));
1795 struct fib6_node
*pn_r
= rcu_dereference_protected(pn
->right
,
1796 lockdep_is_held(&table
->tb6_lock
));
1797 struct fib6_node
*pn_l
= rcu_dereference_protected(pn
->left
,
1798 lockdep_is_held(&table
->tb6_lock
));
1799 struct fib6_info
*fn_leaf
= rcu_dereference_protected(fn
->leaf
,
1800 lockdep_is_held(&table
->tb6_lock
));
1801 struct fib6_info
*pn_leaf
= rcu_dereference_protected(pn
->leaf
,
1802 lockdep_is_held(&table
->tb6_lock
));
1803 struct fib6_info
*new_fn_leaf
;
1805 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1808 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1809 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1815 child
= fn_r
, children
|= 1;
1817 child
= fn_l
, children
|= 2;
1819 if (children
== 3 || FIB6_SUBTREE(fn
)
1820 #ifdef CONFIG_IPV6_SUBTREES
1821 /* Subtree root (i.e. fn) may have one child */
1822 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1825 new_fn_leaf
= fib6_find_prefix(net
, table
, fn
);
1828 WARN_ON(!new_fn_leaf
);
1829 new_fn_leaf
= net
->ipv6
.fib6_null_entry
;
1832 fib6_info_hold(new_fn_leaf
);
1833 rcu_assign_pointer(fn
->leaf
, new_fn_leaf
);
1837 #ifdef CONFIG_IPV6_SUBTREES
1838 if (FIB6_SUBTREE(pn
) == fn
) {
1839 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1840 RCU_INIT_POINTER(pn
->subtree
, NULL
);
1843 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1846 rcu_assign_pointer(pn
->right
, child
);
1847 else if (pn_l
== fn
)
1848 rcu_assign_pointer(pn
->left
, child
);
1854 rcu_assign_pointer(child
->parent
, pn
);
1856 #ifdef CONFIG_IPV6_SUBTREES
1860 read_lock(&net
->ipv6
.fib6_walker_lock
);
1861 FOR_WALKERS(net
, w
) {
1863 if (w
->node
== fn
) {
1864 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1869 if (w
->node
== fn
) {
1872 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1873 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1875 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1876 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1881 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1884 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1887 RCU_INIT_POINTER(pn
->leaf
, NULL
);
1888 fib6_info_release(pn_leaf
);
1893 static void fib6_del_route(struct fib6_table
*table
, struct fib6_node
*fn
,
1894 struct fib6_info __rcu
**rtp
, struct nl_info
*info
)
1896 struct fib6_info
*leaf
, *replace_rt
= NULL
;
1897 struct fib6_walker
*w
;
1898 struct fib6_info
*rt
= rcu_dereference_protected(*rtp
,
1899 lockdep_is_held(&table
->tb6_lock
));
1900 struct net
*net
= info
->nl_net
;
1901 bool notify_del
= false;
1903 RT6_TRACE("fib6_del_route\n");
1905 /* If the deleted route is the first in the node and it is not part of
1906 * a multipath route, then we need to replace it with the next route
1907 * in the node, if exists.
1909 leaf
= rcu_dereference_protected(fn
->leaf
,
1910 lockdep_is_held(&table
->tb6_lock
));
1911 if (leaf
== rt
&& !rt
->fib6_nsiblings
) {
1912 if (rcu_access_pointer(rt
->fib6_next
))
1913 replace_rt
= rcu_dereference_protected(rt
->fib6_next
,
1914 lockdep_is_held(&table
->tb6_lock
));
1920 *rtp
= rt
->fib6_next
;
1921 rt
->fib6_node
= NULL
;
1922 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1923 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1925 /* Flush all cached dst in exception table */
1926 rt6_flush_exceptions(rt
);
1928 /* Reset round-robin state, if necessary */
1929 if (rcu_access_pointer(fn
->rr_ptr
) == rt
)
1932 /* Remove this entry from other siblings */
1933 if (rt
->fib6_nsiblings
) {
1934 struct fib6_info
*sibling
, *next_sibling
;
1936 /* The route is deleted from a multipath route. If this
1937 * multipath route is the first route in the node, then we need
1938 * to emit a delete notification. Otherwise, we need to skip
1941 if (rt
->fib6_metric
== leaf
->fib6_metric
&&
1942 rt6_qualify_for_ecmp(leaf
))
1944 list_for_each_entry_safe(sibling
, next_sibling
,
1945 &rt
->fib6_siblings
, fib6_siblings
)
1946 sibling
->fib6_nsiblings
--;
1947 rt
->fib6_nsiblings
= 0;
1948 list_del_init(&rt
->fib6_siblings
);
1949 rt6_multipath_rebalance(next_sibling
);
1952 /* Adjust walkers */
1953 read_lock(&net
->ipv6
.fib6_walker_lock
);
1954 FOR_WALKERS(net
, w
) {
1955 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1956 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1957 w
->leaf
= rcu_dereference_protected(rt
->fib6_next
,
1958 lockdep_is_held(&table
->tb6_lock
));
1963 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1965 /* If it was last route, call fib6_repair_tree() to:
1966 * 1. For root node, put back null_entry as how the table was created.
1967 * 2. For other nodes, expunge its radix tree node.
1969 if (!rcu_access_pointer(fn
->leaf
)) {
1970 if (!(fn
->fn_flags
& RTN_TL_ROOT
)) {
1971 fn
->fn_flags
&= ~RTN_RTINFO
;
1972 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1974 fn
= fib6_repair_tree(net
, table
, fn
);
1977 fib6_purge_rt(rt
, fn
, net
);
1979 if (!info
->skip_notify_kernel
) {
1981 call_fib6_entry_notifiers(net
, FIB_EVENT_ENTRY_DEL
,
1983 else if (replace_rt
)
1984 call_fib6_entry_notifiers_replace(net
, replace_rt
);
1986 if (!info
->skip_notify
)
1987 inet6_rt_notify(RTM_DELROUTE
, rt
, info
, 0);
1989 fib6_info_release(rt
);
1992 /* Need to own table->tb6_lock */
1993 int fib6_del(struct fib6_info
*rt
, struct nl_info
*info
)
1995 struct fib6_node
*fn
= rcu_dereference_protected(rt
->fib6_node
,
1996 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
1997 struct fib6_table
*table
= rt
->fib6_table
;
1998 struct net
*net
= info
->nl_net
;
1999 struct fib6_info __rcu
**rtp
;
2000 struct fib6_info __rcu
**rtp_next
;
2002 if (!fn
|| rt
== net
->ipv6
.fib6_null_entry
)
2005 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
2008 * Walk the leaf entries looking for ourself
2011 for (rtp
= &fn
->leaf
; *rtp
; rtp
= rtp_next
) {
2012 struct fib6_info
*cur
= rcu_dereference_protected(*rtp
,
2013 lockdep_is_held(&table
->tb6_lock
));
2015 if (fib6_requires_src(cur
))
2016 fib6_routes_require_src_dec(info
->nl_net
);
2017 fib6_del_route(table
, fn
, rtp
, info
);
2020 rtp_next
= &cur
->fib6_next
;
2026 * Tree traversal function.
2028 * Certainly, it is not interrupt safe.
2029 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
2030 * It means, that we can modify tree during walking
2031 * and use this function for garbage collection, clone pruning,
2032 * cleaning tree when a device goes down etc. etc.
2034 * It guarantees that every node will be traversed,
2035 * and that it will be traversed only once.
2037 * Callback function w->func may return:
2038 * 0 -> continue walking.
2039 * positive value -> walking is suspended (used by tree dumps,
2040 * and probably by gc, if it will be split to several slices)
2041 * negative value -> terminate walking.
2043 * The function itself returns:
2044 * 0 -> walk is complete.
2045 * >0 -> walk is incomplete (i.e. suspended)
2046 * <0 -> walk is terminated by an error.
2048 * This function is called with tb6_lock held.
2051 static int fib6_walk_continue(struct fib6_walker
*w
)
2053 struct fib6_node
*fn
, *pn
, *left
, *right
;
2055 /* w->root should always be table->tb6_root */
2056 WARN_ON_ONCE(!(w
->root
->fn_flags
& RTN_TL_ROOT
));
2064 #ifdef CONFIG_IPV6_SUBTREES
2066 if (FIB6_SUBTREE(fn
)) {
2067 w
->node
= FIB6_SUBTREE(fn
);
2074 left
= rcu_dereference_protected(fn
->left
, 1);
2077 w
->state
= FWS_INIT
;
2083 right
= rcu_dereference_protected(fn
->right
, 1);
2086 w
->state
= FWS_INIT
;
2090 w
->leaf
= rcu_dereference_protected(fn
->leaf
, 1);
2093 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
2114 pn
= rcu_dereference_protected(fn
->parent
, 1);
2115 left
= rcu_dereference_protected(pn
->left
, 1);
2116 right
= rcu_dereference_protected(pn
->right
, 1);
2118 #ifdef CONFIG_IPV6_SUBTREES
2119 if (FIB6_SUBTREE(pn
) == fn
) {
2120 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
2131 w
->leaf
= rcu_dereference_protected(w
->node
->leaf
, 1);
2141 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
)
2145 w
->state
= FWS_INIT
;
2148 fib6_walker_link(net
, w
);
2149 res
= fib6_walk_continue(w
);
2151 fib6_walker_unlink(net
, w
);
2155 static int fib6_clean_node(struct fib6_walker
*w
)
2158 struct fib6_info
*rt
;
2159 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
2160 struct nl_info info
= {
2162 .skip_notify
= c
->skip_notify
,
2165 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
2166 w
->node
->fn_sernum
!= c
->sernum
)
2167 w
->node
->fn_sernum
= c
->sernum
;
2170 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
2175 for_each_fib6_walker_rt(w
) {
2176 res
= c
->func(rt
, c
->arg
);
2179 res
= fib6_del(rt
, &info
);
2182 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
2184 rcu_access_pointer(rt
->fib6_node
),
2190 } else if (res
== -2) {
2191 if (WARN_ON(!rt
->fib6_nsiblings
))
2193 rt
= list_last_entry(&rt
->fib6_siblings
,
2194 struct fib6_info
, fib6_siblings
);
2204 * Convenient frontend to tree walker.
2206 * func is called on each route.
2207 * It may return -2 -> skip multipath route.
2208 * -1 -> delete this route.
2209 * 0 -> continue walking
2212 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
2213 int (*func
)(struct fib6_info
*, void *arg
),
2214 int sernum
, void *arg
, bool skip_notify
)
2216 struct fib6_cleaner c
;
2219 c
.w
.func
= fib6_clean_node
;
2222 c
.w
.skip_in_node
= 0;
2227 c
.skip_notify
= skip_notify
;
2229 fib6_walk(net
, &c
.w
);
2232 static void __fib6_clean_all(struct net
*net
,
2233 int (*func
)(struct fib6_info
*, void *),
2234 int sernum
, void *arg
, bool skip_notify
)
2236 struct fib6_table
*table
;
2237 struct hlist_head
*head
;
2241 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
2242 head
= &net
->ipv6
.fib_table_hash
[h
];
2243 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
2244 spin_lock_bh(&table
->tb6_lock
);
2245 fib6_clean_tree(net
, &table
->tb6_root
,
2246 func
, sernum
, arg
, skip_notify
);
2247 spin_unlock_bh(&table
->tb6_lock
);
2253 void fib6_clean_all(struct net
*net
, int (*func
)(struct fib6_info
*, void *),
2256 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
, false);
2259 void fib6_clean_all_skip_notify(struct net
*net
,
2260 int (*func
)(struct fib6_info
*, void *),
2263 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
, true);
2266 static void fib6_flush_trees(struct net
*net
)
2268 int new_sernum
= fib6_new_sernum(net
);
2270 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
, false);
2274 * Garbage collection
2277 static int fib6_age(struct fib6_info
*rt
, void *arg
)
2279 struct fib6_gc_args
*gc_args
= arg
;
2280 unsigned long now
= jiffies
;
2283 * check addrconf expiration here.
2284 * Routes are expired even if they are in use.
2287 if (rt
->fib6_flags
& RTF_EXPIRES
&& rt
->expires
) {
2288 if (time_after(now
, rt
->expires
)) {
2289 RT6_TRACE("expiring %p\n", rt
);
2295 /* Also age clones in the exception table.
2296 * Note, that clones are aged out
2297 * only if they are not in use now.
2299 rt6_age_exceptions(rt
, gc_args
, now
);
2304 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
2306 struct fib6_gc_args gc_args
;
2310 spin_lock_bh(&net
->ipv6
.fib6_gc_lock
);
2311 } else if (!spin_trylock_bh(&net
->ipv6
.fib6_gc_lock
)) {
2312 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
2315 gc_args
.timeout
= expires
? (int)expires
:
2316 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
2319 fib6_clean_all(net
, fib6_age
, &gc_args
);
2321 net
->ipv6
.ip6_rt_last_gc
= now
;
2324 mod_timer(&net
->ipv6
.ip6_fib_timer
,
2326 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
2328 del_timer(&net
->ipv6
.ip6_fib_timer
);
2329 spin_unlock_bh(&net
->ipv6
.fib6_gc_lock
);
2332 static void fib6_gc_timer_cb(struct timer_list
*t
)
2334 struct net
*arg
= from_timer(arg
, t
, ipv6
.ip6_fib_timer
);
2336 fib6_run_gc(0, arg
, true);
2339 static int __net_init
fib6_net_init(struct net
*net
)
2341 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
2344 err
= fib6_notifier_init(net
);
2348 spin_lock_init(&net
->ipv6
.fib6_gc_lock
);
2349 rwlock_init(&net
->ipv6
.fib6_walker_lock
);
2350 INIT_LIST_HEAD(&net
->ipv6
.fib6_walkers
);
2351 timer_setup(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, 0);
2353 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
2354 if (!net
->ipv6
.rt6_stats
)
2357 /* Avoid false sharing : Use at least a full cache line */
2358 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
2360 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
2361 if (!net
->ipv6
.fib_table_hash
)
2364 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
2366 if (!net
->ipv6
.fib6_main_tbl
)
2367 goto out_fib_table_hash
;
2369 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
2370 rcu_assign_pointer(net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
,
2371 net
->ipv6
.fib6_null_entry
);
2372 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
2373 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
2374 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
2376 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2377 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
2379 if (!net
->ipv6
.fib6_local_tbl
)
2380 goto out_fib6_main_tbl
;
2381 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
2382 rcu_assign_pointer(net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
,
2383 net
->ipv6
.fib6_null_entry
);
2384 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
2385 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
2386 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
2388 fib6_tables_init(net
);
2392 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2394 kfree(net
->ipv6
.fib6_main_tbl
);
2397 kfree(net
->ipv6
.fib_table_hash
);
2399 kfree(net
->ipv6
.rt6_stats
);
2401 fib6_notifier_exit(net
);
2405 static void fib6_net_exit(struct net
*net
)
2409 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
2411 for (i
= 0; i
< FIB6_TABLE_HASHSZ
; i
++) {
2412 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[i
];
2413 struct hlist_node
*tmp
;
2414 struct fib6_table
*tb
;
2416 hlist_for_each_entry_safe(tb
, tmp
, head
, tb6_hlist
) {
2417 hlist_del(&tb
->tb6_hlist
);
2418 fib6_free_table(tb
);
2422 kfree(net
->ipv6
.fib_table_hash
);
2423 kfree(net
->ipv6
.rt6_stats
);
2424 fib6_notifier_exit(net
);
2427 static struct pernet_operations fib6_net_ops
= {
2428 .init
= fib6_net_init
,
2429 .exit
= fib6_net_exit
,
2432 int __init
fib6_init(void)
2436 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
2437 sizeof(struct fib6_node
),
2438 0, SLAB_HWCACHE_ALIGN
,
2440 if (!fib6_node_kmem
)
2443 ret
= register_pernet_subsys(&fib6_net_ops
);
2445 goto out_kmem_cache_create
;
2447 ret
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETROUTE
, NULL
,
2450 goto out_unregister_subsys
;
2452 __fib6_flush_trees
= fib6_flush_trees
;
2456 out_unregister_subsys
:
2457 unregister_pernet_subsys(&fib6_net_ops
);
2458 out_kmem_cache_create
:
2459 kmem_cache_destroy(fib6_node_kmem
);
2463 void fib6_gc_cleanup(void)
2465 unregister_pernet_subsys(&fib6_net_ops
);
2466 kmem_cache_destroy(fib6_node_kmem
);
2469 #ifdef CONFIG_PROC_FS
2470 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
2472 struct fib6_info
*rt
= v
;
2473 struct ipv6_route_iter
*iter
= seq
->private;
2474 struct fib6_nh
*fib6_nh
= rt
->fib6_nh
;
2475 unsigned int flags
= rt
->fib6_flags
;
2476 const struct net_device
*dev
;
2479 fib6_nh
= nexthop_fib6_nh(rt
->nh
);
2481 seq_printf(seq
, "%pi6 %02x ", &rt
->fib6_dst
.addr
, rt
->fib6_dst
.plen
);
2483 #ifdef CONFIG_IPV6_SUBTREES
2484 seq_printf(seq
, "%pi6 %02x ", &rt
->fib6_src
.addr
, rt
->fib6_src
.plen
);
2486 seq_puts(seq
, "00000000000000000000000000000000 00 ");
2488 if (fib6_nh
->fib_nh_gw_family
) {
2489 flags
|= RTF_GATEWAY
;
2490 seq_printf(seq
, "%pi6", &fib6_nh
->fib_nh_gw6
);
2492 seq_puts(seq
, "00000000000000000000000000000000");
2495 dev
= fib6_nh
->fib_nh_dev
;
2496 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
2497 rt
->fib6_metric
, refcount_read(&rt
->fib6_ref
), 0,
2498 flags
, dev
? dev
->name
: "");
2499 iter
->w
.leaf
= NULL
;
2503 static int ipv6_route_yield(struct fib6_walker
*w
)
2505 struct ipv6_route_iter
*iter
= w
->args
;
2511 iter
->w
.leaf
= rcu_dereference_protected(
2512 iter
->w
.leaf
->fib6_next
,
2513 lockdep_is_held(&iter
->tbl
->tb6_lock
));
2515 if (!iter
->skip
&& iter
->w
.leaf
)
2517 } while (iter
->w
.leaf
);
2522 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
,
2525 memset(&iter
->w
, 0, sizeof(iter
->w
));
2526 iter
->w
.func
= ipv6_route_yield
;
2527 iter
->w
.root
= &iter
->tbl
->tb6_root
;
2528 iter
->w
.state
= FWS_INIT
;
2529 iter
->w
.node
= iter
->w
.root
;
2530 iter
->w
.args
= iter
;
2531 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2532 INIT_LIST_HEAD(&iter
->w
.lh
);
2533 fib6_walker_link(net
, &iter
->w
);
2536 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
2540 struct hlist_node
*node
;
2543 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
2544 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
2550 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
2551 node
= rcu_dereference_bh(
2552 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
2554 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
2557 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
2559 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
2560 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2561 iter
->w
.state
= FWS_INIT
;
2562 iter
->w
.node
= iter
->w
.root
;
2563 WARN_ON(iter
->w
.skip
);
2564 iter
->w
.skip
= iter
->w
.count
;
2568 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2571 struct fib6_info
*n
;
2572 struct net
*net
= seq_file_net(seq
);
2573 struct ipv6_route_iter
*iter
= seq
->private;
2579 n
= rcu_dereference_bh(((struct fib6_info
*)v
)->fib6_next
);
2584 ipv6_route_check_sernum(iter
);
2585 spin_lock_bh(&iter
->tbl
->tb6_lock
);
2586 r
= fib6_walk_continue(&iter
->w
);
2587 spin_unlock_bh(&iter
->tbl
->tb6_lock
);
2589 return iter
->w
.leaf
;
2591 fib6_walker_unlink(net
, &iter
->w
);
2594 fib6_walker_unlink(net
, &iter
->w
);
2596 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2600 ipv6_route_seq_setup_walk(iter
, net
);
2604 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2607 struct net
*net
= seq_file_net(seq
);
2608 struct ipv6_route_iter
*iter
= seq
->private;
2611 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2615 ipv6_route_seq_setup_walk(iter
, net
);
2616 return ipv6_route_seq_next(seq
, NULL
, pos
);
2622 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2624 struct fib6_walker
*w
= &iter
->w
;
2625 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2628 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2631 struct net
*net
= seq_file_net(seq
);
2632 struct ipv6_route_iter
*iter
= seq
->private;
2634 if (ipv6_route_iter_active(iter
))
2635 fib6_walker_unlink(net
, &iter
->w
);
2637 rcu_read_unlock_bh();
2640 const struct seq_operations ipv6_route_seq_ops
= {
2641 .start
= ipv6_route_seq_start
,
2642 .next
= ipv6_route_seq_next
,
2643 .stop
= ipv6_route_seq_stop
,
2644 .show
= ipv6_route_seq_show
2646 #endif /* CONFIG_PROC_FS */