2 * Linux INET6 implementation
3 * Forwarding Information Database
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
17 * Ville Nuorvala: Fixed routing subtrees.
20 #define pr_fmt(fmt) "IPv6: " fmt
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/net.h>
25 #include <linux/route.h>
26 #include <linux/netdevice.h>
27 #include <linux/in6.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
33 #include <net/ndisc.h>
34 #include <net/addrconf.h>
35 #include <net/lwtunnel.h>
36 #include <net/fib_notifier.h>
38 #include <net/ip6_fib.h>
39 #include <net/ip6_route.h>
41 static struct kmem_cache
*fib6_node_kmem __read_mostly
;
46 int (*func
)(struct rt6_info
*, void *arg
);
51 #ifdef CONFIG_IPV6_SUBTREES
52 #define FWS_INIT FWS_S
54 #define FWS_INIT FWS_L
57 static struct rt6_info
*fib6_find_prefix(struct net
*net
,
58 struct fib6_table
*table
,
59 struct fib6_node
*fn
);
60 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
61 struct fib6_table
*table
,
62 struct fib6_node
*fn
);
63 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
);
64 static int fib6_walk_continue(struct fib6_walker
*w
);
67 * A routing update causes an increase of the serial number on the
68 * affected subtree. This allows for cached routes to be asynchronously
69 * tested when modifications are made to the destination cache as a
70 * result of redirects, path MTU changes, etc.
73 static void fib6_gc_timer_cb(struct timer_list
*t
);
75 #define FOR_WALKERS(net, w) \
76 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
78 static void fib6_walker_link(struct net
*net
, struct fib6_walker
*w
)
80 write_lock_bh(&net
->ipv6
.fib6_walker_lock
);
81 list_add(&w
->lh
, &net
->ipv6
.fib6_walkers
);
82 write_unlock_bh(&net
->ipv6
.fib6_walker_lock
);
85 static void fib6_walker_unlink(struct net
*net
, struct fib6_walker
*w
)
87 write_lock_bh(&net
->ipv6
.fib6_walker_lock
);
89 write_unlock_bh(&net
->ipv6
.fib6_walker_lock
);
92 static int fib6_new_sernum(struct net
*net
)
97 old
= atomic_read(&net
->ipv6
.fib6_sernum
);
98 new = old
< INT_MAX
? old
+ 1 : 1;
99 } while (atomic_cmpxchg(&net
->ipv6
.fib6_sernum
,
105 FIB6_NO_SERNUM_CHANGE
= 0,
108 void fib6_update_sernum(struct rt6_info
*rt
)
110 struct fib6_table
*table
= rt
->rt6i_table
;
111 struct net
*net
= dev_net(rt
->dst
.dev
);
112 struct fib6_node
*fn
;
114 spin_lock_bh(&table
->tb6_lock
);
115 fn
= rcu_dereference_protected(rt
->rt6i_node
,
116 lockdep_is_held(&table
->tb6_lock
));
118 fn
->fn_sernum
= fib6_new_sernum(net
);
119 spin_unlock_bh(&table
->tb6_lock
);
123 * Auxiliary address test functions for the radix tree.
125 * These assume a 32bit processor (although it will work on
132 #if defined(__LITTLE_ENDIAN)
133 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
135 # define BITOP_BE32_SWIZZLE 0
138 static __be32
addr_bit_set(const void *token
, int fn_bit
)
140 const __be32
*addr
= token
;
143 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
144 * is optimized version of
145 * htonl(1 << ((~fn_bit)&0x1F))
146 * See include/asm-generic/bitops/le.h.
148 return (__force __be32
)(1 << ((~fn_bit
^ BITOP_BE32_SWIZZLE
) & 0x1f)) &
152 static struct fib6_node
*node_alloc(struct net
*net
)
154 struct fib6_node
*fn
;
156 fn
= kmem_cache_zalloc(fib6_node_kmem
, GFP_ATOMIC
);
158 net
->ipv6
.rt6_stats
->fib_nodes
++;
163 static void node_free_immediate(struct net
*net
, struct fib6_node
*fn
)
165 kmem_cache_free(fib6_node_kmem
, fn
);
166 net
->ipv6
.rt6_stats
->fib_nodes
--;
169 static void node_free_rcu(struct rcu_head
*head
)
171 struct fib6_node
*fn
= container_of(head
, struct fib6_node
, rcu
);
173 kmem_cache_free(fib6_node_kmem
, fn
);
176 static void node_free(struct net
*net
, struct fib6_node
*fn
)
178 call_rcu(&fn
->rcu
, node_free_rcu
);
179 net
->ipv6
.rt6_stats
->fib_nodes
--;
182 void rt6_free_pcpu(struct rt6_info
*non_pcpu_rt
)
186 if (!non_pcpu_rt
->rt6i_pcpu
)
189 for_each_possible_cpu(cpu
) {
190 struct rt6_info
**ppcpu_rt
;
191 struct rt6_info
*pcpu_rt
;
193 ppcpu_rt
= per_cpu_ptr(non_pcpu_rt
->rt6i_pcpu
, cpu
);
196 dst_dev_put(&pcpu_rt
->dst
);
197 dst_release(&pcpu_rt
->dst
);
202 EXPORT_SYMBOL_GPL(rt6_free_pcpu
);
204 static void fib6_free_table(struct fib6_table
*table
)
206 inetpeer_invalidate_tree(&table
->tb6_peers
);
210 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
215 * Initialize table lock at a single place to give lockdep a key,
216 * tables aren't visible prior to being linked to the list.
218 spin_lock_init(&tb
->tb6_lock
);
219 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
222 * No protection necessary, this is the only list mutatation
223 * operation, tables never disappear once they exist.
225 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
228 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
230 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
232 struct fib6_table
*table
;
234 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
237 rcu_assign_pointer(table
->tb6_root
.leaf
,
238 net
->ipv6
.ip6_null_entry
);
239 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
240 inet_peer_base_init(&table
->tb6_peers
);
246 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
248 struct fib6_table
*tb
;
252 tb
= fib6_get_table(net
, id
);
256 tb
= fib6_alloc_table(net
, id
);
258 fib6_link_table(net
, tb
);
262 EXPORT_SYMBOL_GPL(fib6_new_table
);
264 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
266 struct fib6_table
*tb
;
267 struct hlist_head
*head
;
272 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
274 head
= &net
->ipv6
.fib_table_hash
[h
];
275 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
276 if (tb
->tb6_id
== id
) {
285 EXPORT_SYMBOL_GPL(fib6_get_table
);
287 static void __net_init
fib6_tables_init(struct net
*net
)
289 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
290 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
294 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
296 return fib6_get_table(net
, id
);
299 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
301 return net
->ipv6
.fib6_main_tbl
;
304 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
305 int flags
, pol_lookup_t lookup
)
309 rt
= lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
310 if (rt
->dst
.error
== -EAGAIN
) {
312 rt
= net
->ipv6
.ip6_null_entry
;
319 static void __net_init
fib6_tables_init(struct net
*net
)
321 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
326 unsigned int fib6_tables_seq_read(struct net
*net
)
328 unsigned int h
, fib_seq
= 0;
331 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
332 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[h
];
333 struct fib6_table
*tb
;
335 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
)
336 fib_seq
+= tb
->fib_seq
;
343 static int call_fib6_entry_notifier(struct notifier_block
*nb
, struct net
*net
,
344 enum fib_event_type event_type
,
347 struct fib6_entry_notifier_info info
= {
351 return call_fib6_notifier(nb
, net
, event_type
, &info
.info
);
354 static int call_fib6_entry_notifiers(struct net
*net
,
355 enum fib_event_type event_type
,
357 struct netlink_ext_ack
*extack
)
359 struct fib6_entry_notifier_info info
= {
360 .info
.extack
= extack
,
364 rt
->rt6i_table
->fib_seq
++;
365 return call_fib6_notifiers(net
, event_type
, &info
.info
);
368 struct fib6_dump_arg
{
370 struct notifier_block
*nb
;
373 static void fib6_rt_dump(struct rt6_info
*rt
, struct fib6_dump_arg
*arg
)
375 if (rt
== arg
->net
->ipv6
.ip6_null_entry
)
377 call_fib6_entry_notifier(arg
->nb
, arg
->net
, FIB_EVENT_ENTRY_ADD
, rt
);
380 static int fib6_node_dump(struct fib6_walker
*w
)
384 for_each_fib6_walker_rt(w
)
385 fib6_rt_dump(rt
, w
->args
);
390 static void fib6_table_dump(struct net
*net
, struct fib6_table
*tb
,
391 struct fib6_walker
*w
)
393 w
->root
= &tb
->tb6_root
;
394 spin_lock_bh(&tb
->tb6_lock
);
396 spin_unlock_bh(&tb
->tb6_lock
);
399 /* Called with rcu_read_lock() */
400 int fib6_tables_dump(struct net
*net
, struct notifier_block
*nb
)
402 struct fib6_dump_arg arg
;
403 struct fib6_walker
*w
;
406 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
410 w
->func
= fib6_node_dump
;
415 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
416 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[h
];
417 struct fib6_table
*tb
;
419 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
)
420 fib6_table_dump(net
, tb
, w
);
428 static int fib6_dump_node(struct fib6_walker
*w
)
433 for_each_fib6_walker_rt(w
) {
434 res
= rt6_dump_route(rt
, w
->args
);
436 /* Frame is full, suspend walking */
441 /* Multipath routes are dumped in one route with the
442 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
443 * last sibling of this route (no need to dump the
444 * sibling routes again)
446 if (rt
->rt6i_nsiblings
)
447 rt
= list_last_entry(&rt
->rt6i_siblings
,
455 static void fib6_dump_end(struct netlink_callback
*cb
)
457 struct net
*net
= sock_net(cb
->skb
->sk
);
458 struct fib6_walker
*w
= (void *)cb
->args
[2];
463 fib6_walker_unlink(net
, w
);
468 cb
->done
= (void *)cb
->args
[3];
472 static int fib6_dump_done(struct netlink_callback
*cb
)
475 return cb
->done
? cb
->done(cb
) : 0;
478 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
479 struct netlink_callback
*cb
)
481 struct net
*net
= sock_net(skb
->sk
);
482 struct fib6_walker
*w
;
485 w
= (void *)cb
->args
[2];
486 w
->root
= &table
->tb6_root
;
488 if (cb
->args
[4] == 0) {
492 spin_lock_bh(&table
->tb6_lock
);
493 res
= fib6_walk(net
, w
);
494 spin_unlock_bh(&table
->tb6_lock
);
497 cb
->args
[5] = w
->root
->fn_sernum
;
500 if (cb
->args
[5] != w
->root
->fn_sernum
) {
501 /* Begin at the root if the tree changed */
502 cb
->args
[5] = w
->root
->fn_sernum
;
509 spin_lock_bh(&table
->tb6_lock
);
510 res
= fib6_walk_continue(w
);
511 spin_unlock_bh(&table
->tb6_lock
);
513 fib6_walker_unlink(net
, w
);
521 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
523 struct net
*net
= sock_net(skb
->sk
);
525 unsigned int e
= 0, s_e
;
526 struct rt6_rtnl_dump_arg arg
;
527 struct fib6_walker
*w
;
528 struct fib6_table
*tb
;
529 struct hlist_head
*head
;
535 w
= (void *)cb
->args
[2];
539 * 1. hook callback destructor.
541 cb
->args
[3] = (long)cb
->done
;
542 cb
->done
= fib6_dump_done
;
545 * 2. allocate and initialize walker.
547 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
550 w
->func
= fib6_dump_node
;
551 cb
->args
[2] = (long)w
;
560 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
562 head
= &net
->ipv6
.fib_table_hash
[h
];
563 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
566 res
= fib6_dump_table(tb
, skb
, cb
);
578 res
= res
< 0 ? res
: skb
->len
;
587 * return the appropriate node for a routing tree "add" operation
588 * by either creating and inserting or by returning an existing
592 static struct fib6_node
*fib6_add_1(struct net
*net
,
593 struct fib6_table
*table
,
594 struct fib6_node
*root
,
595 struct in6_addr
*addr
, int plen
,
596 int offset
, int allow_create
,
597 int replace_required
,
598 struct netlink_ext_ack
*extack
)
600 struct fib6_node
*fn
, *in
, *ln
;
601 struct fib6_node
*pn
= NULL
;
606 RT6_TRACE("fib6_add_1\n");
608 /* insert node in tree */
613 struct rt6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
614 lockdep_is_held(&table
->tb6_lock
));
615 key
= (struct rt6key
*)((u8
*)leaf
+ offset
);
620 if (plen
< fn
->fn_bit
||
621 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
623 if (replace_required
) {
624 NL_SET_ERR_MSG(extack
,
625 "Can not replace route - no match found");
626 pr_warn("Can't replace route, no match found\n");
627 return ERR_PTR(-ENOENT
);
629 pr_warn("NLM_F_CREATE should be set when creating new route\n");
638 if (plen
== fn
->fn_bit
) {
639 /* clean up an intermediate node */
640 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
641 RCU_INIT_POINTER(fn
->leaf
, NULL
);
643 /* remove null_entry in the root node */
644 } else if (fn
->fn_flags
& RTN_TL_ROOT
&&
645 rcu_access_pointer(fn
->leaf
) ==
646 net
->ipv6
.ip6_null_entry
) {
647 RCU_INIT_POINTER(fn
->leaf
, NULL
);
654 * We have more bits to go
657 /* Try to walk down on tree. */
658 dir
= addr_bit_set(addr
, fn
->fn_bit
);
661 rcu_dereference_protected(fn
->right
,
662 lockdep_is_held(&table
->tb6_lock
)) :
663 rcu_dereference_protected(fn
->left
,
664 lockdep_is_held(&table
->tb6_lock
));
668 /* We should not create new node because
669 * NLM_F_REPLACE was specified without NLM_F_CREATE
670 * I assume it is safe to require NLM_F_CREATE when
671 * REPLACE flag is used! Later we may want to remove the
672 * check for replace_required, because according
673 * to netlink specification, NLM_F_CREATE
674 * MUST be specified if new route is created.
675 * That would keep IPv6 consistent with IPv4
677 if (replace_required
) {
678 NL_SET_ERR_MSG(extack
,
679 "Can not replace route - no match found");
680 pr_warn("Can't replace route, no match found\n");
681 return ERR_PTR(-ENOENT
);
683 pr_warn("NLM_F_CREATE should be set when creating new route\n");
686 * We walked to the bottom of tree.
687 * Create new leaf node without children.
690 ln
= node_alloc(net
);
693 return ERR_PTR(-ENOMEM
);
695 RCU_INIT_POINTER(ln
->parent
, pn
);
698 rcu_assign_pointer(pn
->right
, ln
);
700 rcu_assign_pointer(pn
->left
, ln
);
707 * split since we don't have a common prefix anymore or
708 * we have a less significant route.
709 * we've to insert an intermediate node on the list
710 * this new node will point to the one we need to create
714 pn
= rcu_dereference_protected(fn
->parent
,
715 lockdep_is_held(&table
->tb6_lock
));
717 /* find 1st bit in difference between the 2 addrs.
719 See comment in __ipv6_addr_diff: bit may be an invalid value,
720 but if it is >= plen, the value is ignored in any case.
723 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
728 * (new leaf node)[ln] (old node)[fn]
731 in
= node_alloc(net
);
732 ln
= node_alloc(net
);
736 node_free_immediate(net
, in
);
738 node_free_immediate(net
, ln
);
739 return ERR_PTR(-ENOMEM
);
743 * new intermediate node.
745 * be off since that an address that chooses one of
746 * the branches would not match less specific routes
747 * in the other branch
752 RCU_INIT_POINTER(in
->parent
, pn
);
754 atomic_inc(&rcu_dereference_protected(in
->leaf
,
755 lockdep_is_held(&table
->tb6_lock
))->rt6i_ref
);
757 /* update parent pointer */
759 rcu_assign_pointer(pn
->right
, in
);
761 rcu_assign_pointer(pn
->left
, in
);
765 RCU_INIT_POINTER(ln
->parent
, in
);
766 rcu_assign_pointer(fn
->parent
, in
);
768 if (addr_bit_set(addr
, bit
)) {
769 rcu_assign_pointer(in
->right
, ln
);
770 rcu_assign_pointer(in
->left
, fn
);
772 rcu_assign_pointer(in
->left
, ln
);
773 rcu_assign_pointer(in
->right
, fn
);
775 } else { /* plen <= bit */
778 * (new leaf node)[ln]
780 * (old node)[fn] NULL
783 ln
= node_alloc(net
);
786 return ERR_PTR(-ENOMEM
);
790 RCU_INIT_POINTER(ln
->parent
, pn
);
792 if (addr_bit_set(&key
->addr
, plen
))
793 RCU_INIT_POINTER(ln
->right
, fn
);
795 RCU_INIT_POINTER(ln
->left
, fn
);
797 rcu_assign_pointer(fn
->parent
, ln
);
800 rcu_assign_pointer(pn
->right
, ln
);
802 rcu_assign_pointer(pn
->left
, ln
);
807 static bool rt6_qualify_for_ecmp(struct rt6_info
*rt
)
809 return (rt
->rt6i_flags
& (RTF_GATEWAY
|RTF_ADDRCONF
|RTF_DYNAMIC
)) ==
813 static void fib6_copy_metrics(u32
*mp
, const struct mx6_config
*mxc
)
817 for (i
= 0; i
< RTAX_MAX
; i
++) {
818 if (test_bit(i
, mxc
->mx_valid
))
823 static int fib6_commit_metrics(struct dst_entry
*dst
, struct mx6_config
*mxc
)
828 if (dst
->flags
& DST_HOST
) {
829 u32
*mp
= dst_metrics_write_ptr(dst
);
834 fib6_copy_metrics(mp
, mxc
);
836 dst_init_metrics(dst
, mxc
->mx
, false);
838 /* We've stolen mx now. */
845 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
848 struct fib6_table
*table
= rt
->rt6i_table
;
850 if (atomic_read(&rt
->rt6i_ref
) != 1) {
851 /* This route is used as dummy address holder in some split
852 * nodes. It is not leaked, but it still holds other resources,
853 * which must be released in time. So, scan ascendant nodes
854 * and replace dummy references to this route with references
855 * to still alive ones.
858 struct rt6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
859 lockdep_is_held(&table
->tb6_lock
));
860 struct rt6_info
*new_leaf
;
861 if (!(fn
->fn_flags
& RTN_RTINFO
) && leaf
== rt
) {
862 new_leaf
= fib6_find_prefix(net
, table
, fn
);
863 atomic_inc(&new_leaf
->rt6i_ref
);
864 rcu_assign_pointer(fn
->leaf
, new_leaf
);
867 fn
= rcu_dereference_protected(fn
->parent
,
868 lockdep_is_held(&table
->tb6_lock
));
874 * Insert routing information in a node.
877 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
878 struct nl_info
*info
, struct mx6_config
*mxc
,
879 struct netlink_ext_ack
*extack
)
881 struct rt6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
882 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
883 struct rt6_info
*iter
= NULL
;
884 struct rt6_info __rcu
**ins
;
885 struct rt6_info __rcu
**fallback_ins
= NULL
;
886 int replace
= (info
->nlh
&&
887 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
888 int add
= (!info
->nlh
||
889 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
891 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
892 u16 nlflags
= NLM_F_EXCL
;
895 if (info
->nlh
&& (info
->nlh
->nlmsg_flags
& NLM_F_APPEND
))
896 nlflags
|= NLM_F_APPEND
;
900 for (iter
= leaf
; iter
;
901 iter
= rcu_dereference_protected(iter
->dst
.rt6_next
,
902 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
))) {
904 * Search for duplicates
907 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
909 * Same priority level
912 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
915 nlflags
&= ~NLM_F_EXCL
;
917 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
922 fallback_ins
= fallback_ins
?: ins
;
926 if (rt6_duplicate_nexthop(iter
, rt
)) {
927 if (rt
->rt6i_nsiblings
)
928 rt
->rt6i_nsiblings
= 0;
929 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
931 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
932 rt6_clean_expires(iter
);
934 rt6_set_expires(iter
, rt
->dst
.expires
);
935 iter
->rt6i_pmtu
= rt
->rt6i_pmtu
;
938 /* If we have the same destination and the same metric,
939 * but not the same gateway, then the route we try to
940 * add is sibling to this route, increment our counter
941 * of siblings, and later we will add our route to the
943 * Only static routes (which don't have flag
944 * RTF_EXPIRES) are used for ECMPv6.
946 * To avoid long list, we only had siblings if the
947 * route have a gateway.
950 rt6_qualify_for_ecmp(iter
))
951 rt
->rt6i_nsiblings
++;
954 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
958 ins
= &iter
->dst
.rt6_next
;
961 if (fallback_ins
&& !found
) {
962 /* No ECMP-able route found, replace first non-ECMP one */
964 iter
= rcu_dereference_protected(*ins
,
965 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
969 /* Reset round-robin state, if necessary */
970 if (ins
== &fn
->leaf
)
973 /* Link this route to others same route. */
974 if (rt
->rt6i_nsiblings
) {
975 unsigned int rt6i_nsiblings
;
976 struct rt6_info
*sibling
, *temp_sibling
;
978 /* Find the first route that have the same metric */
981 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
982 rt6_qualify_for_ecmp(sibling
)) {
983 list_add_tail(&rt
->rt6i_siblings
,
984 &sibling
->rt6i_siblings
);
987 sibling
= rcu_dereference_protected(sibling
->dst
.rt6_next
,
988 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
990 /* For each sibling in the list, increment the counter of
991 * siblings. BUG() if counters does not match, list of siblings
995 list_for_each_entry_safe(sibling
, temp_sibling
,
996 &rt
->rt6i_siblings
, rt6i_siblings
) {
997 sibling
->rt6i_nsiblings
++;
998 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
1001 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
1009 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1012 nlflags
|= NLM_F_CREATE
;
1013 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
1017 rcu_assign_pointer(rt
->dst
.rt6_next
, iter
);
1018 atomic_inc(&rt
->rt6i_ref
);
1019 rcu_assign_pointer(rt
->rt6i_node
, fn
);
1020 rcu_assign_pointer(*ins
, rt
);
1021 call_fib6_entry_notifiers(info
->nl_net
, FIB_EVENT_ENTRY_ADD
,
1023 if (!info
->skip_notify
)
1024 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, nlflags
);
1025 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
1027 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
1028 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
1029 fn
->fn_flags
|= RTN_RTINFO
;
1038 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1042 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
1046 atomic_inc(&rt
->rt6i_ref
);
1047 rcu_assign_pointer(rt
->rt6i_node
, fn
);
1048 rt
->dst
.rt6_next
= iter
->dst
.rt6_next
;
1049 rcu_assign_pointer(*ins
, rt
);
1050 call_fib6_entry_notifiers(info
->nl_net
, FIB_EVENT_ENTRY_REPLACE
,
1052 if (!info
->skip_notify
)
1053 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, NLM_F_REPLACE
);
1054 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
1055 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
1056 fn
->fn_flags
|= RTN_RTINFO
;
1058 nsiblings
= iter
->rt6i_nsiblings
;
1059 iter
->rt6i_node
= NULL
;
1060 fib6_purge_rt(iter
, fn
, info
->nl_net
);
1061 if (rcu_access_pointer(fn
->rr_ptr
) == iter
)
1066 /* Replacing an ECMP route, remove all siblings */
1067 ins
= &rt
->dst
.rt6_next
;
1068 iter
= rcu_dereference_protected(*ins
,
1069 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1071 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
1073 if (rt6_qualify_for_ecmp(iter
)) {
1074 *ins
= iter
->dst
.rt6_next
;
1075 iter
->rt6i_node
= NULL
;
1076 fib6_purge_rt(iter
, fn
, info
->nl_net
);
1077 if (rcu_access_pointer(fn
->rr_ptr
) == iter
)
1081 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1083 ins
= &iter
->dst
.rt6_next
;
1085 iter
= rcu_dereference_protected(*ins
,
1086 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1088 WARN_ON(nsiblings
!= 0);
1095 static void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
1097 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
1098 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
1099 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1100 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
1103 void fib6_force_start_gc(struct net
*net
)
1105 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
1106 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1107 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
1110 static void fib6_update_sernum_upto_root(struct rt6_info
*rt
,
1113 struct fib6_node
*fn
= rcu_dereference_protected(rt
->rt6i_node
,
1114 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1116 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1119 fn
->fn_sernum
= sernum
;
1120 fn
= rcu_dereference_protected(fn
->parent
,
1121 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1126 * Add routing information to the routing tree.
1127 * <destination addr>/<source addr>
1128 * with source addr info in sub-trees
1129 * Need to own table->tb6_lock
1132 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
1133 struct nl_info
*info
, struct mx6_config
*mxc
,
1134 struct netlink_ext_ack
*extack
)
1136 struct fib6_table
*table
= rt
->rt6i_table
;
1137 struct fib6_node
*fn
, *pn
= NULL
;
1139 int allow_create
= 1;
1140 int replace_required
= 0;
1141 int sernum
= fib6_new_sernum(info
->nl_net
);
1143 if (WARN_ON_ONCE(!atomic_read(&rt
->dst
.__refcnt
)))
1145 if (WARN_ON_ONCE(rt
->rt6i_flags
& RTF_CACHE
))
1149 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
1151 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1152 replace_required
= 1;
1154 if (!allow_create
&& !replace_required
)
1155 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1157 fn
= fib6_add_1(info
->nl_net
, table
, root
,
1158 &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
,
1159 offsetof(struct rt6_info
, rt6i_dst
), allow_create
,
1160 replace_required
, extack
);
1169 #ifdef CONFIG_IPV6_SUBTREES
1170 if (rt
->rt6i_src
.plen
) {
1171 struct fib6_node
*sn
;
1173 if (!rcu_access_pointer(fn
->subtree
)) {
1174 struct fib6_node
*sfn
;
1186 /* Create subtree root node */
1187 sfn
= node_alloc(info
->nl_net
);
1191 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
1192 rcu_assign_pointer(sfn
->leaf
,
1193 info
->nl_net
->ipv6
.ip6_null_entry
);
1194 sfn
->fn_flags
= RTN_ROOT
;
1196 /* Now add the first leaf node to new subtree */
1198 sn
= fib6_add_1(info
->nl_net
, table
, sfn
,
1199 &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
,
1200 offsetof(struct rt6_info
, rt6i_src
),
1201 allow_create
, replace_required
, extack
);
1204 /* If it is failed, discard just allocated
1205 root, and then (in failure) stale node
1208 node_free_immediate(info
->nl_net
, sfn
);
1213 /* Now link new subtree to main tree */
1214 rcu_assign_pointer(sfn
->parent
, fn
);
1215 rcu_assign_pointer(fn
->subtree
, sfn
);
1217 sn
= fib6_add_1(info
->nl_net
, table
, FIB6_SUBTREE(fn
),
1218 &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
,
1219 offsetof(struct rt6_info
, rt6i_src
),
1220 allow_create
, replace_required
, extack
);
1228 if (!rcu_access_pointer(fn
->leaf
)) {
1229 if (fn
->fn_flags
& RTN_TL_ROOT
) {
1230 /* put back null_entry for root node */
1231 rcu_assign_pointer(fn
->leaf
,
1232 info
->nl_net
->ipv6
.ip6_null_entry
);
1234 atomic_inc(&rt
->rt6i_ref
);
1235 rcu_assign_pointer(fn
->leaf
, rt
);
1242 err
= fib6_add_rt2node(fn
, rt
, info
, mxc
, extack
);
1244 fib6_update_sernum_upto_root(rt
, sernum
);
1245 fib6_start_gc(info
->nl_net
, rt
);
1250 #ifdef CONFIG_IPV6_SUBTREES
1252 * If fib6_add_1 has cleared the old leaf pointer in the
1253 * super-tree leaf node we have to find a new one for it.
1256 struct rt6_info
*pn_leaf
=
1257 rcu_dereference_protected(pn
->leaf
,
1258 lockdep_is_held(&table
->tb6_lock
));
1259 if (pn_leaf
== rt
) {
1261 RCU_INIT_POINTER(pn
->leaf
, NULL
);
1262 atomic_dec(&rt
->rt6i_ref
);
1264 if (!pn_leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
1265 pn_leaf
= fib6_find_prefix(info
->nl_net
, table
,
1271 info
->nl_net
->ipv6
.ip6_null_entry
;
1274 atomic_inc(&pn_leaf
->rt6i_ref
);
1275 rcu_assign_pointer(pn
->leaf
, pn_leaf
);
1284 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1285 * 1. fn is an intermediate node and we failed to add the new
1286 * route to it in both subtree creation failure and fib6_add_rt2node()
1288 * 2. fn is the root node in the table and we fail to add the first
1289 * default route to it.
1292 (!(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)) ||
1293 (fn
->fn_flags
& RTN_TL_ROOT
&&
1294 !rcu_access_pointer(fn
->leaf
))))
1295 fib6_repair_tree(info
->nl_net
, table
, fn
);
1296 /* Always release dst as dst->__refcnt is guaranteed
1297 * to be taken before entering this function
1299 dst_release_immediate(&rt
->dst
);
1304 * Routing tree lookup
1308 struct lookup_args
{
1309 int offset
; /* key offset on rt6_info */
1310 const struct in6_addr
*addr
; /* search key */
1313 static struct fib6_node
*fib6_lookup_1(struct fib6_node
*root
,
1314 struct lookup_args
*args
)
1316 struct fib6_node
*fn
;
1319 if (unlikely(args
->offset
== 0))
1329 struct fib6_node
*next
;
1331 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1333 next
= dir
? rcu_dereference(fn
->right
) :
1334 rcu_dereference(fn
->left
);
1344 struct fib6_node
*subtree
= FIB6_SUBTREE(fn
);
1346 if (subtree
|| fn
->fn_flags
& RTN_RTINFO
) {
1347 struct rt6_info
*leaf
= rcu_dereference(fn
->leaf
);
1353 key
= (struct rt6key
*) ((u8
*)leaf
+ args
->offset
);
1355 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1356 #ifdef CONFIG_IPV6_SUBTREES
1358 struct fib6_node
*sfn
;
1359 sfn
= fib6_lookup_1(subtree
, args
+ 1);
1365 if (fn
->fn_flags
& RTN_RTINFO
)
1370 if (fn
->fn_flags
& RTN_ROOT
)
1373 fn
= rcu_dereference(fn
->parent
);
1379 /* called with rcu_read_lock() held
1381 struct fib6_node
*fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1382 const struct in6_addr
*saddr
)
1384 struct fib6_node
*fn
;
1385 struct lookup_args args
[] = {
1387 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1390 #ifdef CONFIG_IPV6_SUBTREES
1392 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1397 .offset
= 0, /* sentinel */
1401 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1402 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1409 * Get node with specified destination prefix (and source prefix,
1410 * if subtrees are used)
1411 * exact_match == true means we try to find fn with exact match of
1412 * the passed in prefix addr
1413 * exact_match == false means we try to find fn with longest prefix
1414 * match of the passed in prefix addr. This is useful for finding fn
1415 * for cached route as it will be stored in the exception table under
1416 * the node with longest prefix length.
1420 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1421 const struct in6_addr
*addr
,
1422 int plen
, int offset
,
1425 struct fib6_node
*fn
, *prev
= NULL
;
1427 for (fn
= root
; fn
; ) {
1428 struct rt6_info
*leaf
= rcu_dereference(fn
->leaf
);
1431 /* This node is being deleted */
1433 if (plen
<= fn
->fn_bit
)
1439 key
= (struct rt6key
*)((u8
*)leaf
+ offset
);
1444 if (plen
< fn
->fn_bit
||
1445 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1448 if (plen
== fn
->fn_bit
)
1455 * We have more bits to go
1457 if (addr_bit_set(addr
, fn
->fn_bit
))
1458 fn
= rcu_dereference(fn
->right
);
1460 fn
= rcu_dereference(fn
->left
);
1469 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1470 const struct in6_addr
*daddr
, int dst_len
,
1471 const struct in6_addr
*saddr
, int src_len
,
1474 struct fib6_node
*fn
;
1476 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1477 offsetof(struct rt6_info
, rt6i_dst
),
1480 #ifdef CONFIG_IPV6_SUBTREES
1482 WARN_ON(saddr
== NULL
);
1484 struct fib6_node
*subtree
= FIB6_SUBTREE(fn
);
1487 fn
= fib6_locate_1(subtree
, saddr
, src_len
,
1488 offsetof(struct rt6_info
, rt6i_src
),
1495 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1507 static struct rt6_info
*fib6_find_prefix(struct net
*net
,
1508 struct fib6_table
*table
,
1509 struct fib6_node
*fn
)
1511 struct fib6_node
*child_left
, *child_right
;
1513 if (fn
->fn_flags
& RTN_ROOT
)
1514 return net
->ipv6
.ip6_null_entry
;
1517 child_left
= rcu_dereference_protected(fn
->left
,
1518 lockdep_is_held(&table
->tb6_lock
));
1519 child_right
= rcu_dereference_protected(fn
->right
,
1520 lockdep_is_held(&table
->tb6_lock
));
1522 return rcu_dereference_protected(child_left
->leaf
,
1523 lockdep_is_held(&table
->tb6_lock
));
1525 return rcu_dereference_protected(child_right
->leaf
,
1526 lockdep_is_held(&table
->tb6_lock
));
1528 fn
= FIB6_SUBTREE(fn
);
1534 * Called to trim the tree of intermediate nodes when possible. "fn"
1535 * is the node we want to try and remove.
1536 * Need to own table->tb6_lock
1539 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1540 struct fib6_table
*table
,
1541 struct fib6_node
*fn
)
1545 struct fib6_node
*child
;
1546 struct fib6_walker
*w
;
1549 /* Set fn->leaf to null_entry for root node. */
1550 if (fn
->fn_flags
& RTN_TL_ROOT
) {
1551 rcu_assign_pointer(fn
->leaf
, net
->ipv6
.ip6_null_entry
);
1556 struct fib6_node
*fn_r
= rcu_dereference_protected(fn
->right
,
1557 lockdep_is_held(&table
->tb6_lock
));
1558 struct fib6_node
*fn_l
= rcu_dereference_protected(fn
->left
,
1559 lockdep_is_held(&table
->tb6_lock
));
1560 struct fib6_node
*pn
= rcu_dereference_protected(fn
->parent
,
1561 lockdep_is_held(&table
->tb6_lock
));
1562 struct fib6_node
*pn_r
= rcu_dereference_protected(pn
->right
,
1563 lockdep_is_held(&table
->tb6_lock
));
1564 struct fib6_node
*pn_l
= rcu_dereference_protected(pn
->left
,
1565 lockdep_is_held(&table
->tb6_lock
));
1566 struct rt6_info
*fn_leaf
= rcu_dereference_protected(fn
->leaf
,
1567 lockdep_is_held(&table
->tb6_lock
));
1568 struct rt6_info
*pn_leaf
= rcu_dereference_protected(pn
->leaf
,
1569 lockdep_is_held(&table
->tb6_lock
));
1570 struct rt6_info
*new_fn_leaf
;
1572 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1575 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1576 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1582 child
= fn_r
, children
|= 1;
1584 child
= fn_l
, children
|= 2;
1586 if (children
== 3 || FIB6_SUBTREE(fn
)
1587 #ifdef CONFIG_IPV6_SUBTREES
1588 /* Subtree root (i.e. fn) may have one child */
1589 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1592 new_fn_leaf
= fib6_find_prefix(net
, table
, fn
);
1595 WARN_ON(!new_fn_leaf
);
1596 new_fn_leaf
= net
->ipv6
.ip6_null_entry
;
1599 atomic_inc(&new_fn_leaf
->rt6i_ref
);
1600 rcu_assign_pointer(fn
->leaf
, new_fn_leaf
);
1604 #ifdef CONFIG_IPV6_SUBTREES
1605 if (FIB6_SUBTREE(pn
) == fn
) {
1606 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1607 RCU_INIT_POINTER(pn
->subtree
, NULL
);
1610 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1613 rcu_assign_pointer(pn
->right
, child
);
1614 else if (pn_l
== fn
)
1615 rcu_assign_pointer(pn
->left
, child
);
1621 rcu_assign_pointer(child
->parent
, pn
);
1623 #ifdef CONFIG_IPV6_SUBTREES
1627 read_lock(&net
->ipv6
.fib6_walker_lock
);
1628 FOR_WALKERS(net
, w
) {
1630 if (w
->node
== fn
) {
1631 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1636 if (w
->node
== fn
) {
1639 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1640 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1642 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1643 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1648 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1651 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1654 RCU_INIT_POINTER(pn
->leaf
, NULL
);
1655 rt6_release(pn_leaf
);
1660 static void fib6_del_route(struct fib6_table
*table
, struct fib6_node
*fn
,
1661 struct rt6_info __rcu
**rtp
, struct nl_info
*info
)
1663 struct fib6_walker
*w
;
1664 struct rt6_info
*rt
= rcu_dereference_protected(*rtp
,
1665 lockdep_is_held(&table
->tb6_lock
));
1666 struct net
*net
= info
->nl_net
;
1668 RT6_TRACE("fib6_del_route\n");
1670 WARN_ON_ONCE(rt
->rt6i_flags
& RTF_CACHE
);
1673 *rtp
= rt
->dst
.rt6_next
;
1674 rt
->rt6i_node
= NULL
;
1675 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1676 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1678 /* Flush all cached dst in exception table */
1679 rt6_flush_exceptions(rt
);
1681 /* Reset round-robin state, if necessary */
1682 if (rcu_access_pointer(fn
->rr_ptr
) == rt
)
1685 /* Remove this entry from other siblings */
1686 if (rt
->rt6i_nsiblings
) {
1687 struct rt6_info
*sibling
, *next_sibling
;
1689 list_for_each_entry_safe(sibling
, next_sibling
,
1690 &rt
->rt6i_siblings
, rt6i_siblings
)
1691 sibling
->rt6i_nsiblings
--;
1692 rt
->rt6i_nsiblings
= 0;
1693 list_del_init(&rt
->rt6i_siblings
);
1696 /* Adjust walkers */
1697 read_lock(&net
->ipv6
.fib6_walker_lock
);
1698 FOR_WALKERS(net
, w
) {
1699 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1700 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1701 w
->leaf
= rcu_dereference_protected(rt
->dst
.rt6_next
,
1702 lockdep_is_held(&table
->tb6_lock
));
1707 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1709 /* If it was last route, call fib6_repair_tree() to:
1710 * 1. For root node, put back null_entry as how the table was created.
1711 * 2. For other nodes, expunge its radix tree node.
1713 if (!rcu_access_pointer(fn
->leaf
)) {
1714 if (!(fn
->fn_flags
& RTN_TL_ROOT
)) {
1715 fn
->fn_flags
&= ~RTN_RTINFO
;
1716 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1718 fn
= fib6_repair_tree(net
, table
, fn
);
1721 fib6_purge_rt(rt
, fn
, net
);
1723 call_fib6_entry_notifiers(net
, FIB_EVENT_ENTRY_DEL
, rt
, NULL
);
1724 if (!info
->skip_notify
)
1725 inet6_rt_notify(RTM_DELROUTE
, rt
, info
, 0);
1729 /* Need to own table->tb6_lock */
1730 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1732 struct fib6_node
*fn
= rcu_dereference_protected(rt
->rt6i_node
,
1733 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1734 struct fib6_table
*table
= rt
->rt6i_table
;
1735 struct net
*net
= info
->nl_net
;
1736 struct rt6_info __rcu
**rtp
;
1737 struct rt6_info __rcu
**rtp_next
;
1740 if (rt
->dst
.obsolete
> 0) {
1745 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1748 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1750 /* remove cached dst from exception table */
1751 if (rt
->rt6i_flags
& RTF_CACHE
)
1752 return rt6_remove_exception_rt(rt
);
1755 * Walk the leaf entries looking for ourself
1758 for (rtp
= &fn
->leaf
; *rtp
; rtp
= rtp_next
) {
1759 struct rt6_info
*cur
= rcu_dereference_protected(*rtp
,
1760 lockdep_is_held(&table
->tb6_lock
));
1762 fib6_del_route(table
, fn
, rtp
, info
);
1765 rtp_next
= &cur
->dst
.rt6_next
;
1771 * Tree traversal function.
1773 * Certainly, it is not interrupt safe.
1774 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1775 * It means, that we can modify tree during walking
1776 * and use this function for garbage collection, clone pruning,
1777 * cleaning tree when a device goes down etc. etc.
1779 * It guarantees that every node will be traversed,
1780 * and that it will be traversed only once.
1782 * Callback function w->func may return:
1783 * 0 -> continue walking.
1784 * positive value -> walking is suspended (used by tree dumps,
1785 * and probably by gc, if it will be split to several slices)
1786 * negative value -> terminate walking.
1788 * The function itself returns:
1789 * 0 -> walk is complete.
1790 * >0 -> walk is incomplete (i.e. suspended)
1791 * <0 -> walk is terminated by an error.
1793 * This function is called with tb6_lock held.
1796 static int fib6_walk_continue(struct fib6_walker
*w
)
1798 struct fib6_node
*fn
, *pn
, *left
, *right
;
1800 /* w->root should always be table->tb6_root */
1801 WARN_ON_ONCE(!(w
->root
->fn_flags
& RTN_TL_ROOT
));
1809 #ifdef CONFIG_IPV6_SUBTREES
1811 if (FIB6_SUBTREE(fn
)) {
1812 w
->node
= FIB6_SUBTREE(fn
);
1819 left
= rcu_dereference_protected(fn
->left
, 1);
1822 w
->state
= FWS_INIT
;
1828 right
= rcu_dereference_protected(fn
->right
, 1);
1831 w
->state
= FWS_INIT
;
1835 w
->leaf
= rcu_dereference_protected(fn
->leaf
, 1);
1838 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1859 pn
= rcu_dereference_protected(fn
->parent
, 1);
1860 left
= rcu_dereference_protected(pn
->left
, 1);
1861 right
= rcu_dereference_protected(pn
->right
, 1);
1863 #ifdef CONFIG_IPV6_SUBTREES
1864 if (FIB6_SUBTREE(pn
) == fn
) {
1865 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1876 w
->leaf
= rcu_dereference_protected(w
->node
->leaf
, 1);
1886 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
)
1890 w
->state
= FWS_INIT
;
1893 fib6_walker_link(net
, w
);
1894 res
= fib6_walk_continue(w
);
1896 fib6_walker_unlink(net
, w
);
1900 static int fib6_clean_node(struct fib6_walker
*w
)
1903 struct rt6_info
*rt
;
1904 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1905 struct nl_info info
= {
1909 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1910 w
->node
->fn_sernum
!= c
->sernum
)
1911 w
->node
->fn_sernum
= c
->sernum
;
1914 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1919 for_each_fib6_walker_rt(w
) {
1920 res
= c
->func(rt
, c
->arg
);
1923 res
= fib6_del(rt
, &info
);
1926 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1928 rcu_access_pointer(rt
->rt6i_node
),
1942 * Convenient frontend to tree walker.
1944 * func is called on each route.
1945 * It may return -1 -> delete this route.
1946 * 0 -> continue walking
1949 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1950 int (*func
)(struct rt6_info
*, void *arg
),
1951 int sernum
, void *arg
)
1953 struct fib6_cleaner c
;
1956 c
.w
.func
= fib6_clean_node
;
1964 fib6_walk(net
, &c
.w
);
1967 static void __fib6_clean_all(struct net
*net
,
1968 int (*func
)(struct rt6_info
*, void *),
1969 int sernum
, void *arg
)
1971 struct fib6_table
*table
;
1972 struct hlist_head
*head
;
1976 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1977 head
= &net
->ipv6
.fib_table_hash
[h
];
1978 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1979 spin_lock_bh(&table
->tb6_lock
);
1980 fib6_clean_tree(net
, &table
->tb6_root
,
1982 spin_unlock_bh(&table
->tb6_lock
);
1988 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *),
1991 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
);
1994 static void fib6_flush_trees(struct net
*net
)
1996 int new_sernum
= fib6_new_sernum(net
);
1998 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
);
2002 * Garbage collection
2005 static int fib6_age(struct rt6_info
*rt
, void *arg
)
2007 struct fib6_gc_args
*gc_args
= arg
;
2008 unsigned long now
= jiffies
;
2011 * check addrconf expiration here.
2012 * Routes are expired even if they are in use.
2015 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
2016 if (time_after(now
, rt
->dst
.expires
)) {
2017 RT6_TRACE("expiring %p\n", rt
);
2023 /* Also age clones in the exception table.
2024 * Note, that clones are aged out
2025 * only if they are not in use now.
2027 rt6_age_exceptions(rt
, gc_args
, now
);
2032 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
2034 struct fib6_gc_args gc_args
;
2038 spin_lock_bh(&net
->ipv6
.fib6_gc_lock
);
2039 } else if (!spin_trylock_bh(&net
->ipv6
.fib6_gc_lock
)) {
2040 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
2043 gc_args
.timeout
= expires
? (int)expires
:
2044 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
2047 fib6_clean_all(net
, fib6_age
, &gc_args
);
2049 net
->ipv6
.ip6_rt_last_gc
= now
;
2052 mod_timer(&net
->ipv6
.ip6_fib_timer
,
2054 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
2056 del_timer(&net
->ipv6
.ip6_fib_timer
);
2057 spin_unlock_bh(&net
->ipv6
.fib6_gc_lock
);
2060 static void fib6_gc_timer_cb(struct timer_list
*t
)
2062 struct net
*arg
= from_timer(arg
, t
, ipv6
.ip6_fib_timer
);
2064 fib6_run_gc(0, arg
, true);
2067 static int __net_init
fib6_net_init(struct net
*net
)
2069 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
2072 err
= fib6_notifier_init(net
);
2076 spin_lock_init(&net
->ipv6
.fib6_gc_lock
);
2077 rwlock_init(&net
->ipv6
.fib6_walker_lock
);
2078 INIT_LIST_HEAD(&net
->ipv6
.fib6_walkers
);
2079 timer_setup(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, 0);
2081 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
2082 if (!net
->ipv6
.rt6_stats
)
2085 /* Avoid false sharing : Use at least a full cache line */
2086 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
2088 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
2089 if (!net
->ipv6
.fib_table_hash
)
2092 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
2094 if (!net
->ipv6
.fib6_main_tbl
)
2095 goto out_fib_table_hash
;
2097 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
2098 rcu_assign_pointer(net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
,
2099 net
->ipv6
.ip6_null_entry
);
2100 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
2101 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
2102 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
2104 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2105 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
2107 if (!net
->ipv6
.fib6_local_tbl
)
2108 goto out_fib6_main_tbl
;
2109 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
2110 rcu_assign_pointer(net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
,
2111 net
->ipv6
.ip6_null_entry
);
2112 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
2113 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
2114 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
2116 fib6_tables_init(net
);
2120 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2122 kfree(net
->ipv6
.fib6_main_tbl
);
2125 kfree(net
->ipv6
.fib_table_hash
);
2127 kfree(net
->ipv6
.rt6_stats
);
2129 fib6_notifier_exit(net
);
2133 static void fib6_net_exit(struct net
*net
)
2137 rt6_ifdown(net
, NULL
);
2138 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
2140 for (i
= 0; i
< FIB6_TABLE_HASHSZ
; i
++) {
2141 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[i
];
2142 struct hlist_node
*tmp
;
2143 struct fib6_table
*tb
;
2145 hlist_for_each_entry_safe(tb
, tmp
, head
, tb6_hlist
) {
2146 hlist_del(&tb
->tb6_hlist
);
2147 fib6_free_table(tb
);
2151 kfree(net
->ipv6
.fib_table_hash
);
2152 kfree(net
->ipv6
.rt6_stats
);
2153 fib6_notifier_exit(net
);
2156 static struct pernet_operations fib6_net_ops
= {
2157 .init
= fib6_net_init
,
2158 .exit
= fib6_net_exit
,
2161 int __init
fib6_init(void)
2165 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
2166 sizeof(struct fib6_node
),
2167 0, SLAB_HWCACHE_ALIGN
,
2169 if (!fib6_node_kmem
)
2172 ret
= register_pernet_subsys(&fib6_net_ops
);
2174 goto out_kmem_cache_create
;
2176 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
2179 goto out_unregister_subsys
;
2181 __fib6_flush_trees
= fib6_flush_trees
;
2185 out_unregister_subsys
:
2186 unregister_pernet_subsys(&fib6_net_ops
);
2187 out_kmem_cache_create
:
2188 kmem_cache_destroy(fib6_node_kmem
);
2192 void fib6_gc_cleanup(void)
2194 unregister_pernet_subsys(&fib6_net_ops
);
2195 kmem_cache_destroy(fib6_node_kmem
);
2198 #ifdef CONFIG_PROC_FS
2200 struct ipv6_route_iter
{
2201 struct seq_net_private p
;
2202 struct fib6_walker w
;
2204 struct fib6_table
*tbl
;
2208 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
2210 struct rt6_info
*rt
= v
;
2211 struct ipv6_route_iter
*iter
= seq
->private;
2213 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
2215 #ifdef CONFIG_IPV6_SUBTREES
2216 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
2218 seq_puts(seq
, "00000000000000000000000000000000 00 ");
2220 if (rt
->rt6i_flags
& RTF_GATEWAY
)
2221 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
2223 seq_puts(seq
, "00000000000000000000000000000000");
2225 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
2226 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
2227 rt
->dst
.__use
, rt
->rt6i_flags
,
2228 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
2229 iter
->w
.leaf
= NULL
;
2233 static int ipv6_route_yield(struct fib6_walker
*w
)
2235 struct ipv6_route_iter
*iter
= w
->args
;
2241 iter
->w
.leaf
= rcu_dereference_protected(
2242 iter
->w
.leaf
->dst
.rt6_next
,
2243 lockdep_is_held(&iter
->tbl
->tb6_lock
));
2245 if (!iter
->skip
&& iter
->w
.leaf
)
2247 } while (iter
->w
.leaf
);
2252 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
,
2255 memset(&iter
->w
, 0, sizeof(iter
->w
));
2256 iter
->w
.func
= ipv6_route_yield
;
2257 iter
->w
.root
= &iter
->tbl
->tb6_root
;
2258 iter
->w
.state
= FWS_INIT
;
2259 iter
->w
.node
= iter
->w
.root
;
2260 iter
->w
.args
= iter
;
2261 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2262 INIT_LIST_HEAD(&iter
->w
.lh
);
2263 fib6_walker_link(net
, &iter
->w
);
2266 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
2270 struct hlist_node
*node
;
2273 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
2274 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
2280 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
2281 node
= rcu_dereference_bh(
2282 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
2284 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
2287 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
2289 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
2290 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2291 iter
->w
.state
= FWS_INIT
;
2292 iter
->w
.node
= iter
->w
.root
;
2293 WARN_ON(iter
->w
.skip
);
2294 iter
->w
.skip
= iter
->w
.count
;
2298 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2302 struct net
*net
= seq_file_net(seq
);
2303 struct ipv6_route_iter
*iter
= seq
->private;
2308 n
= rcu_dereference_bh(((struct rt6_info
*)v
)->dst
.rt6_next
);
2315 ipv6_route_check_sernum(iter
);
2316 spin_lock_bh(&iter
->tbl
->tb6_lock
);
2317 r
= fib6_walk_continue(&iter
->w
);
2318 spin_unlock_bh(&iter
->tbl
->tb6_lock
);
2322 return iter
->w
.leaf
;
2324 fib6_walker_unlink(net
, &iter
->w
);
2327 fib6_walker_unlink(net
, &iter
->w
);
2329 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2333 ipv6_route_seq_setup_walk(iter
, net
);
2337 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2340 struct net
*net
= seq_file_net(seq
);
2341 struct ipv6_route_iter
*iter
= seq
->private;
2344 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2348 ipv6_route_seq_setup_walk(iter
, net
);
2349 return ipv6_route_seq_next(seq
, NULL
, pos
);
2355 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2357 struct fib6_walker
*w
= &iter
->w
;
2358 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2361 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2364 struct net
*net
= seq_file_net(seq
);
2365 struct ipv6_route_iter
*iter
= seq
->private;
2367 if (ipv6_route_iter_active(iter
))
2368 fib6_walker_unlink(net
, &iter
->w
);
2370 rcu_read_unlock_bh();
2373 static const struct seq_operations ipv6_route_seq_ops
= {
2374 .start
= ipv6_route_seq_start
,
2375 .next
= ipv6_route_seq_next
,
2376 .stop
= ipv6_route_seq_stop
,
2377 .show
= ipv6_route_seq_show
2380 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2382 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2383 sizeof(struct ipv6_route_iter
));
2386 #endif /* CONFIG_PROC_FS */