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
);
649 * We have more bits to go
652 /* Try to walk down on tree. */
653 dir
= addr_bit_set(addr
, fn
->fn_bit
);
656 rcu_dereference_protected(fn
->right
,
657 lockdep_is_held(&table
->tb6_lock
)) :
658 rcu_dereference_protected(fn
->left
,
659 lockdep_is_held(&table
->tb6_lock
));
663 /* We should not create new node because
664 * NLM_F_REPLACE was specified without NLM_F_CREATE
665 * I assume it is safe to require NLM_F_CREATE when
666 * REPLACE flag is used! Later we may want to remove the
667 * check for replace_required, because according
668 * to netlink specification, NLM_F_CREATE
669 * MUST be specified if new route is created.
670 * That would keep IPv6 consistent with IPv4
672 if (replace_required
) {
673 NL_SET_ERR_MSG(extack
,
674 "Can not replace route - no match found");
675 pr_warn("Can't replace route, no match found\n");
676 return ERR_PTR(-ENOENT
);
678 pr_warn("NLM_F_CREATE should be set when creating new route\n");
681 * We walked to the bottom of tree.
682 * Create new leaf node without children.
685 ln
= node_alloc(net
);
688 return ERR_PTR(-ENOMEM
);
690 RCU_INIT_POINTER(ln
->parent
, pn
);
693 rcu_assign_pointer(pn
->right
, ln
);
695 rcu_assign_pointer(pn
->left
, ln
);
702 * split since we don't have a common prefix anymore or
703 * we have a less significant route.
704 * we've to insert an intermediate node on the list
705 * this new node will point to the one we need to create
709 pn
= rcu_dereference_protected(fn
->parent
,
710 lockdep_is_held(&table
->tb6_lock
));
712 /* find 1st bit in difference between the 2 addrs.
714 See comment in __ipv6_addr_diff: bit may be an invalid value,
715 but if it is >= plen, the value is ignored in any case.
718 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
723 * (new leaf node)[ln] (old node)[fn]
726 in
= node_alloc(net
);
727 ln
= node_alloc(net
);
731 node_free_immediate(net
, in
);
733 node_free_immediate(net
, ln
);
734 return ERR_PTR(-ENOMEM
);
738 * new intermediate node.
740 * be off since that an address that chooses one of
741 * the branches would not match less specific routes
742 * in the other branch
747 RCU_INIT_POINTER(in
->parent
, pn
);
749 atomic_inc(&rcu_dereference_protected(in
->leaf
,
750 lockdep_is_held(&table
->tb6_lock
))->rt6i_ref
);
752 /* update parent pointer */
754 rcu_assign_pointer(pn
->right
, in
);
756 rcu_assign_pointer(pn
->left
, in
);
760 RCU_INIT_POINTER(ln
->parent
, in
);
761 rcu_assign_pointer(fn
->parent
, in
);
763 if (addr_bit_set(addr
, bit
)) {
764 rcu_assign_pointer(in
->right
, ln
);
765 rcu_assign_pointer(in
->left
, fn
);
767 rcu_assign_pointer(in
->left
, ln
);
768 rcu_assign_pointer(in
->right
, fn
);
770 } else { /* plen <= bit */
773 * (new leaf node)[ln]
775 * (old node)[fn] NULL
778 ln
= node_alloc(net
);
781 return ERR_PTR(-ENOMEM
);
785 RCU_INIT_POINTER(ln
->parent
, pn
);
787 if (addr_bit_set(&key
->addr
, plen
))
788 RCU_INIT_POINTER(ln
->right
, fn
);
790 RCU_INIT_POINTER(ln
->left
, fn
);
792 rcu_assign_pointer(fn
->parent
, ln
);
795 rcu_assign_pointer(pn
->right
, ln
);
797 rcu_assign_pointer(pn
->left
, ln
);
802 static bool rt6_qualify_for_ecmp(struct rt6_info
*rt
)
804 return (rt
->rt6i_flags
& (RTF_GATEWAY
|RTF_ADDRCONF
|RTF_DYNAMIC
)) ==
808 static void fib6_copy_metrics(u32
*mp
, const struct mx6_config
*mxc
)
812 for (i
= 0; i
< RTAX_MAX
; i
++) {
813 if (test_bit(i
, mxc
->mx_valid
))
818 static int fib6_commit_metrics(struct dst_entry
*dst
, struct mx6_config
*mxc
)
823 if (dst
->flags
& DST_HOST
) {
824 u32
*mp
= dst_metrics_write_ptr(dst
);
829 fib6_copy_metrics(mp
, mxc
);
831 dst_init_metrics(dst
, mxc
->mx
, false);
833 /* We've stolen mx now. */
840 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
843 struct fib6_table
*table
= rt
->rt6i_table
;
845 if (atomic_read(&rt
->rt6i_ref
) != 1) {
846 /* This route is used as dummy address holder in some split
847 * nodes. It is not leaked, but it still holds other resources,
848 * which must be released in time. So, scan ascendant nodes
849 * and replace dummy references to this route with references
850 * to still alive ones.
853 struct rt6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
854 lockdep_is_held(&table
->tb6_lock
));
855 struct rt6_info
*new_leaf
;
856 if (!(fn
->fn_flags
& RTN_RTINFO
) && leaf
== rt
) {
857 new_leaf
= fib6_find_prefix(net
, table
, fn
);
858 atomic_inc(&new_leaf
->rt6i_ref
);
859 rcu_assign_pointer(fn
->leaf
, new_leaf
);
862 fn
= rcu_dereference_protected(fn
->parent
,
863 lockdep_is_held(&table
->tb6_lock
));
869 * Insert routing information in a node.
872 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
873 struct nl_info
*info
, struct mx6_config
*mxc
,
874 struct netlink_ext_ack
*extack
)
876 struct rt6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
877 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
878 struct rt6_info
*iter
= NULL
;
879 struct rt6_info __rcu
**ins
;
880 struct rt6_info __rcu
**fallback_ins
= NULL
;
881 int replace
= (info
->nlh
&&
882 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
883 int add
= (!info
->nlh
||
884 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
886 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
887 u16 nlflags
= NLM_F_EXCL
;
890 if (info
->nlh
&& (info
->nlh
->nlmsg_flags
& NLM_F_APPEND
))
891 nlflags
|= NLM_F_APPEND
;
895 for (iter
= leaf
; iter
;
896 iter
= rcu_dereference_protected(iter
->dst
.rt6_next
,
897 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
))) {
899 * Search for duplicates
902 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
904 * Same priority level
907 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
910 nlflags
&= ~NLM_F_EXCL
;
912 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
917 fallback_ins
= fallback_ins
?: ins
;
921 if (rt6_duplicate_nexthop(iter
, rt
)) {
922 if (rt
->rt6i_nsiblings
)
923 rt
->rt6i_nsiblings
= 0;
924 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
926 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
927 rt6_clean_expires(iter
);
929 rt6_set_expires(iter
, rt
->dst
.expires
);
930 iter
->rt6i_pmtu
= rt
->rt6i_pmtu
;
933 /* If we have the same destination and the same metric,
934 * but not the same gateway, then the route we try to
935 * add is sibling to this route, increment our counter
936 * of siblings, and later we will add our route to the
938 * Only static routes (which don't have flag
939 * RTF_EXPIRES) are used for ECMPv6.
941 * To avoid long list, we only had siblings if the
942 * route have a gateway.
945 rt6_qualify_for_ecmp(iter
))
946 rt
->rt6i_nsiblings
++;
949 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
953 ins
= &iter
->dst
.rt6_next
;
956 if (fallback_ins
&& !found
) {
957 /* No ECMP-able route found, replace first non-ECMP one */
959 iter
= rcu_dereference_protected(*ins
,
960 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
964 /* Reset round-robin state, if necessary */
965 if (ins
== &fn
->leaf
)
968 /* Link this route to others same route. */
969 if (rt
->rt6i_nsiblings
) {
970 unsigned int rt6i_nsiblings
;
971 struct rt6_info
*sibling
, *temp_sibling
;
973 /* Find the first route that have the same metric */
976 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
977 rt6_qualify_for_ecmp(sibling
)) {
978 list_add_tail(&rt
->rt6i_siblings
,
979 &sibling
->rt6i_siblings
);
982 sibling
= rcu_dereference_protected(sibling
->dst
.rt6_next
,
983 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
985 /* For each sibling in the list, increment the counter of
986 * siblings. BUG() if counters does not match, list of siblings
990 list_for_each_entry_safe(sibling
, temp_sibling
,
991 &rt
->rt6i_siblings
, rt6i_siblings
) {
992 sibling
->rt6i_nsiblings
++;
993 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
996 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
1004 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1007 nlflags
|= NLM_F_CREATE
;
1008 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
1012 rcu_assign_pointer(rt
->dst
.rt6_next
, iter
);
1013 atomic_inc(&rt
->rt6i_ref
);
1014 rcu_assign_pointer(rt
->rt6i_node
, fn
);
1015 rcu_assign_pointer(*ins
, rt
);
1016 call_fib6_entry_notifiers(info
->nl_net
, FIB_EVENT_ENTRY_ADD
,
1018 if (!info
->skip_notify
)
1019 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, nlflags
);
1020 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
1022 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
1023 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
1024 fn
->fn_flags
|= RTN_RTINFO
;
1033 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1037 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
1041 atomic_inc(&rt
->rt6i_ref
);
1042 rcu_assign_pointer(rt
->rt6i_node
, fn
);
1043 rt
->dst
.rt6_next
= iter
->dst
.rt6_next
;
1044 rcu_assign_pointer(*ins
, rt
);
1045 call_fib6_entry_notifiers(info
->nl_net
, FIB_EVENT_ENTRY_REPLACE
,
1047 if (!info
->skip_notify
)
1048 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, NLM_F_REPLACE
);
1049 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
1050 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
1051 fn
->fn_flags
|= RTN_RTINFO
;
1053 nsiblings
= iter
->rt6i_nsiblings
;
1054 iter
->rt6i_node
= NULL
;
1055 fib6_purge_rt(iter
, fn
, info
->nl_net
);
1056 if (rcu_access_pointer(fn
->rr_ptr
) == iter
)
1061 /* Replacing an ECMP route, remove all siblings */
1062 ins
= &rt
->dst
.rt6_next
;
1063 iter
= rcu_dereference_protected(*ins
,
1064 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1066 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
1068 if (rt6_qualify_for_ecmp(iter
)) {
1069 *ins
= iter
->dst
.rt6_next
;
1070 iter
->rt6i_node
= NULL
;
1071 fib6_purge_rt(iter
, fn
, info
->nl_net
);
1072 if (rcu_access_pointer(fn
->rr_ptr
) == iter
)
1076 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1078 ins
= &iter
->dst
.rt6_next
;
1080 iter
= rcu_dereference_protected(*ins
,
1081 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1083 WARN_ON(nsiblings
!= 0);
1090 static void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
1092 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
1093 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
1094 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1095 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
1098 void fib6_force_start_gc(struct net
*net
)
1100 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
1101 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1102 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
1105 static void fib6_update_sernum_upto_root(struct rt6_info
*rt
,
1108 struct fib6_node
*fn
= rcu_dereference_protected(rt
->rt6i_node
,
1109 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1111 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1114 fn
->fn_sernum
= sernum
;
1115 fn
= rcu_dereference_protected(fn
->parent
,
1116 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1121 * Add routing information to the routing tree.
1122 * <destination addr>/<source addr>
1123 * with source addr info in sub-trees
1124 * Need to own table->tb6_lock
1127 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
1128 struct nl_info
*info
, struct mx6_config
*mxc
,
1129 struct netlink_ext_ack
*extack
)
1131 struct fib6_table
*table
= rt
->rt6i_table
;
1132 struct fib6_node
*fn
, *pn
= NULL
;
1134 int allow_create
= 1;
1135 int replace_required
= 0;
1136 int sernum
= fib6_new_sernum(info
->nl_net
);
1138 if (WARN_ON_ONCE(!atomic_read(&rt
->dst
.__refcnt
)))
1140 if (WARN_ON_ONCE(rt
->rt6i_flags
& RTF_CACHE
))
1144 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
1146 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1147 replace_required
= 1;
1149 if (!allow_create
&& !replace_required
)
1150 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1152 fn
= fib6_add_1(info
->nl_net
, table
, root
,
1153 &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
,
1154 offsetof(struct rt6_info
, rt6i_dst
), allow_create
,
1155 replace_required
, extack
);
1164 #ifdef CONFIG_IPV6_SUBTREES
1165 if (rt
->rt6i_src
.plen
) {
1166 struct fib6_node
*sn
;
1168 if (!rcu_access_pointer(fn
->subtree
)) {
1169 struct fib6_node
*sfn
;
1181 /* Create subtree root node */
1182 sfn
= node_alloc(info
->nl_net
);
1186 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
1187 rcu_assign_pointer(sfn
->leaf
,
1188 info
->nl_net
->ipv6
.ip6_null_entry
);
1189 sfn
->fn_flags
= RTN_ROOT
;
1191 /* Now add the first leaf node to new subtree */
1193 sn
= fib6_add_1(info
->nl_net
, table
, sfn
,
1194 &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
,
1195 offsetof(struct rt6_info
, rt6i_src
),
1196 allow_create
, replace_required
, extack
);
1199 /* If it is failed, discard just allocated
1200 root, and then (in failure) stale node
1203 node_free_immediate(info
->nl_net
, sfn
);
1208 /* Now link new subtree to main tree */
1209 rcu_assign_pointer(sfn
->parent
, fn
);
1210 rcu_assign_pointer(fn
->subtree
, sfn
);
1212 sn
= fib6_add_1(info
->nl_net
, table
, FIB6_SUBTREE(fn
),
1213 &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
,
1214 offsetof(struct rt6_info
, rt6i_src
),
1215 allow_create
, replace_required
, extack
);
1223 if (!rcu_access_pointer(fn
->leaf
)) {
1224 atomic_inc(&rt
->rt6i_ref
);
1225 rcu_assign_pointer(fn
->leaf
, rt
);
1231 err
= fib6_add_rt2node(fn
, rt
, info
, mxc
, extack
);
1233 fib6_update_sernum_upto_root(rt
, sernum
);
1234 fib6_start_gc(info
->nl_net
, rt
);
1239 #ifdef CONFIG_IPV6_SUBTREES
1241 * If fib6_add_1 has cleared the old leaf pointer in the
1242 * super-tree leaf node we have to find a new one for it.
1244 struct rt6_info
*pn_leaf
= rcu_dereference_protected(pn
->leaf
,
1245 lockdep_is_held(&table
->tb6_lock
));
1246 if (pn
!= fn
&& pn_leaf
== rt
) {
1248 RCU_INIT_POINTER(pn
->leaf
, NULL
);
1249 atomic_dec(&rt
->rt6i_ref
);
1251 if (pn
!= fn
&& !pn_leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
1252 pn_leaf
= fib6_find_prefix(info
->nl_net
, table
, pn
);
1256 pn_leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
1259 atomic_inc(&pn_leaf
->rt6i_ref
);
1260 rcu_assign_pointer(pn
->leaf
, pn_leaf
);
1268 /* fn->leaf could be NULL if fn is an intermediate node and we
1269 * failed to add the new route to it in both subtree creation
1270 * failure and fib6_add_rt2node() failure case.
1271 * In both cases, fib6_repair_tree() should be called to fix
1274 if (fn
&& !(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)))
1275 fib6_repair_tree(info
->nl_net
, table
, fn
);
1276 /* Always release dst as dst->__refcnt is guaranteed
1277 * to be taken before entering this function
1279 dst_release_immediate(&rt
->dst
);
1284 * Routing tree lookup
1288 struct lookup_args
{
1289 int offset
; /* key offset on rt6_info */
1290 const struct in6_addr
*addr
; /* search key */
1293 static struct fib6_node
*fib6_lookup_1(struct fib6_node
*root
,
1294 struct lookup_args
*args
)
1296 struct fib6_node
*fn
;
1299 if (unlikely(args
->offset
== 0))
1309 struct fib6_node
*next
;
1311 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1313 next
= dir
? rcu_dereference(fn
->right
) :
1314 rcu_dereference(fn
->left
);
1324 struct fib6_node
*subtree
= FIB6_SUBTREE(fn
);
1326 if (subtree
|| fn
->fn_flags
& RTN_RTINFO
) {
1327 struct rt6_info
*leaf
= rcu_dereference(fn
->leaf
);
1333 key
= (struct rt6key
*) ((u8
*)leaf
+ args
->offset
);
1335 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1336 #ifdef CONFIG_IPV6_SUBTREES
1338 struct fib6_node
*sfn
;
1339 sfn
= fib6_lookup_1(subtree
, args
+ 1);
1345 if (fn
->fn_flags
& RTN_RTINFO
)
1350 if (fn
->fn_flags
& RTN_ROOT
)
1353 fn
= rcu_dereference(fn
->parent
);
1359 /* called with rcu_read_lock() held
1361 struct fib6_node
*fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1362 const struct in6_addr
*saddr
)
1364 struct fib6_node
*fn
;
1365 struct lookup_args args
[] = {
1367 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1370 #ifdef CONFIG_IPV6_SUBTREES
1372 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1377 .offset
= 0, /* sentinel */
1381 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1382 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1389 * Get node with specified destination prefix (and source prefix,
1390 * if subtrees are used)
1391 * exact_match == true means we try to find fn with exact match of
1392 * the passed in prefix addr
1393 * exact_match == false means we try to find fn with longest prefix
1394 * match of the passed in prefix addr. This is useful for finding fn
1395 * for cached route as it will be stored in the exception table under
1396 * the node with longest prefix length.
1400 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1401 const struct in6_addr
*addr
,
1402 int plen
, int offset
,
1405 struct fib6_node
*fn
, *prev
= NULL
;
1407 for (fn
= root
; fn
; ) {
1408 struct rt6_info
*leaf
= rcu_dereference(fn
->leaf
);
1411 /* This node is being deleted */
1413 if (plen
<= fn
->fn_bit
)
1419 key
= (struct rt6key
*)((u8
*)leaf
+ offset
);
1424 if (plen
< fn
->fn_bit
||
1425 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1428 if (plen
== fn
->fn_bit
)
1435 * We have more bits to go
1437 if (addr_bit_set(addr
, fn
->fn_bit
))
1438 fn
= rcu_dereference(fn
->right
);
1440 fn
= rcu_dereference(fn
->left
);
1449 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1450 const struct in6_addr
*daddr
, int dst_len
,
1451 const struct in6_addr
*saddr
, int src_len
,
1454 struct fib6_node
*fn
;
1456 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1457 offsetof(struct rt6_info
, rt6i_dst
),
1460 #ifdef CONFIG_IPV6_SUBTREES
1462 WARN_ON(saddr
== NULL
);
1464 struct fib6_node
*subtree
= FIB6_SUBTREE(fn
);
1467 fn
= fib6_locate_1(subtree
, saddr
, src_len
,
1468 offsetof(struct rt6_info
, rt6i_src
),
1475 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1487 static struct rt6_info
*fib6_find_prefix(struct net
*net
,
1488 struct fib6_table
*table
,
1489 struct fib6_node
*fn
)
1491 struct fib6_node
*child_left
, *child_right
;
1493 if (fn
->fn_flags
& RTN_ROOT
)
1494 return net
->ipv6
.ip6_null_entry
;
1497 child_left
= rcu_dereference_protected(fn
->left
,
1498 lockdep_is_held(&table
->tb6_lock
));
1499 child_right
= rcu_dereference_protected(fn
->right
,
1500 lockdep_is_held(&table
->tb6_lock
));
1502 return rcu_dereference_protected(child_left
->leaf
,
1503 lockdep_is_held(&table
->tb6_lock
));
1505 return rcu_dereference_protected(child_right
->leaf
,
1506 lockdep_is_held(&table
->tb6_lock
));
1508 fn
= FIB6_SUBTREE(fn
);
1514 * Called to trim the tree of intermediate nodes when possible. "fn"
1515 * is the node we want to try and remove.
1516 * Need to own table->tb6_lock
1519 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1520 struct fib6_table
*table
,
1521 struct fib6_node
*fn
)
1525 struct fib6_node
*child
;
1526 struct fib6_walker
*w
;
1530 struct fib6_node
*fn_r
= rcu_dereference_protected(fn
->right
,
1531 lockdep_is_held(&table
->tb6_lock
));
1532 struct fib6_node
*fn_l
= rcu_dereference_protected(fn
->left
,
1533 lockdep_is_held(&table
->tb6_lock
));
1534 struct fib6_node
*pn
= rcu_dereference_protected(fn
->parent
,
1535 lockdep_is_held(&table
->tb6_lock
));
1536 struct fib6_node
*pn_r
= rcu_dereference_protected(pn
->right
,
1537 lockdep_is_held(&table
->tb6_lock
));
1538 struct fib6_node
*pn_l
= rcu_dereference_protected(pn
->left
,
1539 lockdep_is_held(&table
->tb6_lock
));
1540 struct rt6_info
*fn_leaf
= rcu_dereference_protected(fn
->leaf
,
1541 lockdep_is_held(&table
->tb6_lock
));
1542 struct rt6_info
*pn_leaf
= rcu_dereference_protected(pn
->leaf
,
1543 lockdep_is_held(&table
->tb6_lock
));
1544 struct rt6_info
*new_fn_leaf
;
1546 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1549 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1550 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1556 child
= fn_r
, children
|= 1;
1558 child
= fn_l
, children
|= 2;
1560 if (children
== 3 || FIB6_SUBTREE(fn
)
1561 #ifdef CONFIG_IPV6_SUBTREES
1562 /* Subtree root (i.e. fn) may have one child */
1563 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1566 new_fn_leaf
= fib6_find_prefix(net
, table
, fn
);
1569 WARN_ON(!new_fn_leaf
);
1570 new_fn_leaf
= net
->ipv6
.ip6_null_entry
;
1573 atomic_inc(&new_fn_leaf
->rt6i_ref
);
1574 rcu_assign_pointer(fn
->leaf
, new_fn_leaf
);
1578 #ifdef CONFIG_IPV6_SUBTREES
1579 if (FIB6_SUBTREE(pn
) == fn
) {
1580 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1581 RCU_INIT_POINTER(pn
->subtree
, NULL
);
1584 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1587 rcu_assign_pointer(pn
->right
, child
);
1588 else if (pn_l
== fn
)
1589 rcu_assign_pointer(pn
->left
, child
);
1595 rcu_assign_pointer(child
->parent
, pn
);
1597 #ifdef CONFIG_IPV6_SUBTREES
1601 read_lock(&net
->ipv6
.fib6_walker_lock
);
1602 FOR_WALKERS(net
, w
) {
1604 if (w
->node
== fn
) {
1605 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1610 if (w
->node
== fn
) {
1613 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1614 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1616 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1617 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1622 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1625 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1628 RCU_INIT_POINTER(pn
->leaf
, NULL
);
1629 rt6_release(pn_leaf
);
1634 static void fib6_del_route(struct fib6_table
*table
, struct fib6_node
*fn
,
1635 struct rt6_info __rcu
**rtp
, struct nl_info
*info
)
1637 struct fib6_walker
*w
;
1638 struct rt6_info
*rt
= rcu_dereference_protected(*rtp
,
1639 lockdep_is_held(&table
->tb6_lock
));
1640 struct net
*net
= info
->nl_net
;
1642 RT6_TRACE("fib6_del_route\n");
1644 WARN_ON_ONCE(rt
->rt6i_flags
& RTF_CACHE
);
1647 *rtp
= rt
->dst
.rt6_next
;
1648 rt
->rt6i_node
= NULL
;
1649 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1650 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1652 /* Flush all cached dst in exception table */
1653 rt6_flush_exceptions(rt
);
1655 /* Reset round-robin state, if necessary */
1656 if (rcu_access_pointer(fn
->rr_ptr
) == rt
)
1659 /* Remove this entry from other siblings */
1660 if (rt
->rt6i_nsiblings
) {
1661 struct rt6_info
*sibling
, *next_sibling
;
1663 list_for_each_entry_safe(sibling
, next_sibling
,
1664 &rt
->rt6i_siblings
, rt6i_siblings
)
1665 sibling
->rt6i_nsiblings
--;
1666 rt
->rt6i_nsiblings
= 0;
1667 list_del_init(&rt
->rt6i_siblings
);
1670 /* Adjust walkers */
1671 read_lock(&net
->ipv6
.fib6_walker_lock
);
1672 FOR_WALKERS(net
, w
) {
1673 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1674 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1675 w
->leaf
= rcu_dereference_protected(rt
->dst
.rt6_next
,
1676 lockdep_is_held(&table
->tb6_lock
));
1681 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1683 /* If it was last route, expunge its radix tree node */
1684 if (!rcu_access_pointer(fn
->leaf
)) {
1685 fn
->fn_flags
&= ~RTN_RTINFO
;
1686 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1687 fn
= fib6_repair_tree(net
, table
, fn
);
1690 fib6_purge_rt(rt
, fn
, net
);
1692 call_fib6_entry_notifiers(net
, FIB_EVENT_ENTRY_DEL
, rt
, NULL
);
1693 if (!info
->skip_notify
)
1694 inet6_rt_notify(RTM_DELROUTE
, rt
, info
, 0);
1698 /* Need to own table->tb6_lock */
1699 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1701 struct fib6_node
*fn
= rcu_dereference_protected(rt
->rt6i_node
,
1702 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1703 struct fib6_table
*table
= rt
->rt6i_table
;
1704 struct net
*net
= info
->nl_net
;
1705 struct rt6_info __rcu
**rtp
;
1706 struct rt6_info __rcu
**rtp_next
;
1709 if (rt
->dst
.obsolete
> 0) {
1714 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1717 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1719 /* remove cached dst from exception table */
1720 if (rt
->rt6i_flags
& RTF_CACHE
)
1721 return rt6_remove_exception_rt(rt
);
1724 * Walk the leaf entries looking for ourself
1727 for (rtp
= &fn
->leaf
; *rtp
; rtp
= rtp_next
) {
1728 struct rt6_info
*cur
= rcu_dereference_protected(*rtp
,
1729 lockdep_is_held(&table
->tb6_lock
));
1731 fib6_del_route(table
, fn
, rtp
, info
);
1734 rtp_next
= &cur
->dst
.rt6_next
;
1740 * Tree traversal function.
1742 * Certainly, it is not interrupt safe.
1743 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1744 * It means, that we can modify tree during walking
1745 * and use this function for garbage collection, clone pruning,
1746 * cleaning tree when a device goes down etc. etc.
1748 * It guarantees that every node will be traversed,
1749 * and that it will be traversed only once.
1751 * Callback function w->func may return:
1752 * 0 -> continue walking.
1753 * positive value -> walking is suspended (used by tree dumps,
1754 * and probably by gc, if it will be split to several slices)
1755 * negative value -> terminate walking.
1757 * The function itself returns:
1758 * 0 -> walk is complete.
1759 * >0 -> walk is incomplete (i.e. suspended)
1760 * <0 -> walk is terminated by an error.
1762 * This function is called with tb6_lock held.
1765 static int fib6_walk_continue(struct fib6_walker
*w
)
1767 struct fib6_node
*fn
, *pn
, *left
, *right
;
1769 /* w->root should always be table->tb6_root */
1770 WARN_ON_ONCE(!(w
->root
->fn_flags
& RTN_TL_ROOT
));
1778 #ifdef CONFIG_IPV6_SUBTREES
1780 if (FIB6_SUBTREE(fn
)) {
1781 w
->node
= FIB6_SUBTREE(fn
);
1788 left
= rcu_dereference_protected(fn
->left
, 1);
1791 w
->state
= FWS_INIT
;
1797 right
= rcu_dereference_protected(fn
->right
, 1);
1800 w
->state
= FWS_INIT
;
1804 w
->leaf
= rcu_dereference_protected(fn
->leaf
, 1);
1807 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1828 pn
= rcu_dereference_protected(fn
->parent
, 1);
1829 left
= rcu_dereference_protected(pn
->left
, 1);
1830 right
= rcu_dereference_protected(pn
->right
, 1);
1832 #ifdef CONFIG_IPV6_SUBTREES
1833 if (FIB6_SUBTREE(pn
) == fn
) {
1834 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1845 w
->leaf
= rcu_dereference_protected(w
->node
->leaf
, 1);
1855 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
)
1859 w
->state
= FWS_INIT
;
1862 fib6_walker_link(net
, w
);
1863 res
= fib6_walk_continue(w
);
1865 fib6_walker_unlink(net
, w
);
1869 static int fib6_clean_node(struct fib6_walker
*w
)
1872 struct rt6_info
*rt
;
1873 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1874 struct nl_info info
= {
1878 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1879 w
->node
->fn_sernum
!= c
->sernum
)
1880 w
->node
->fn_sernum
= c
->sernum
;
1883 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1888 for_each_fib6_walker_rt(w
) {
1889 res
= c
->func(rt
, c
->arg
);
1892 res
= fib6_del(rt
, &info
);
1895 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1897 rcu_access_pointer(rt
->rt6i_node
),
1911 * Convenient frontend to tree walker.
1913 * func is called on each route.
1914 * It may return -1 -> delete this route.
1915 * 0 -> continue walking
1918 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1919 int (*func
)(struct rt6_info
*, void *arg
),
1920 int sernum
, void *arg
)
1922 struct fib6_cleaner c
;
1925 c
.w
.func
= fib6_clean_node
;
1933 fib6_walk(net
, &c
.w
);
1936 static void __fib6_clean_all(struct net
*net
,
1937 int (*func
)(struct rt6_info
*, void *),
1938 int sernum
, void *arg
)
1940 struct fib6_table
*table
;
1941 struct hlist_head
*head
;
1945 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1946 head
= &net
->ipv6
.fib_table_hash
[h
];
1947 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1948 spin_lock_bh(&table
->tb6_lock
);
1949 fib6_clean_tree(net
, &table
->tb6_root
,
1951 spin_unlock_bh(&table
->tb6_lock
);
1957 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *),
1960 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
);
1963 static void fib6_flush_trees(struct net
*net
)
1965 int new_sernum
= fib6_new_sernum(net
);
1967 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
);
1971 * Garbage collection
1974 static int fib6_age(struct rt6_info
*rt
, void *arg
)
1976 struct fib6_gc_args
*gc_args
= arg
;
1977 unsigned long now
= jiffies
;
1980 * check addrconf expiration here.
1981 * Routes are expired even if they are in use.
1984 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
1985 if (time_after(now
, rt
->dst
.expires
)) {
1986 RT6_TRACE("expiring %p\n", rt
);
1992 /* Also age clones in the exception table.
1993 * Note, that clones are aged out
1994 * only if they are not in use now.
1996 rt6_age_exceptions(rt
, gc_args
, now
);
2001 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
2003 struct fib6_gc_args gc_args
;
2007 spin_lock_bh(&net
->ipv6
.fib6_gc_lock
);
2008 } else if (!spin_trylock_bh(&net
->ipv6
.fib6_gc_lock
)) {
2009 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
2012 gc_args
.timeout
= expires
? (int)expires
:
2013 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
2016 fib6_clean_all(net
, fib6_age
, &gc_args
);
2018 net
->ipv6
.ip6_rt_last_gc
= now
;
2021 mod_timer(&net
->ipv6
.ip6_fib_timer
,
2023 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
2025 del_timer(&net
->ipv6
.ip6_fib_timer
);
2026 spin_unlock_bh(&net
->ipv6
.fib6_gc_lock
);
2029 static void fib6_gc_timer_cb(struct timer_list
*t
)
2031 struct net
*arg
= from_timer(arg
, t
, ipv6
.ip6_fib_timer
);
2033 fib6_run_gc(0, arg
, true);
2036 static int __net_init
fib6_net_init(struct net
*net
)
2038 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
2041 err
= fib6_notifier_init(net
);
2045 spin_lock_init(&net
->ipv6
.fib6_gc_lock
);
2046 rwlock_init(&net
->ipv6
.fib6_walker_lock
);
2047 INIT_LIST_HEAD(&net
->ipv6
.fib6_walkers
);
2048 timer_setup(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, 0);
2050 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
2051 if (!net
->ipv6
.rt6_stats
)
2054 /* Avoid false sharing : Use at least a full cache line */
2055 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
2057 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
2058 if (!net
->ipv6
.fib_table_hash
)
2061 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
2063 if (!net
->ipv6
.fib6_main_tbl
)
2064 goto out_fib_table_hash
;
2066 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
2067 rcu_assign_pointer(net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
,
2068 net
->ipv6
.ip6_null_entry
);
2069 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
2070 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
2071 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
2073 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2074 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
2076 if (!net
->ipv6
.fib6_local_tbl
)
2077 goto out_fib6_main_tbl
;
2078 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
2079 rcu_assign_pointer(net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
,
2080 net
->ipv6
.ip6_null_entry
);
2081 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
2082 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
2083 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
2085 fib6_tables_init(net
);
2089 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2091 kfree(net
->ipv6
.fib6_main_tbl
);
2094 kfree(net
->ipv6
.fib_table_hash
);
2096 kfree(net
->ipv6
.rt6_stats
);
2098 fib6_notifier_exit(net
);
2102 static void fib6_net_exit(struct net
*net
)
2106 rt6_ifdown(net
, NULL
);
2107 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
2109 for (i
= 0; i
< FIB6_TABLE_HASHSZ
; i
++) {
2110 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[i
];
2111 struct hlist_node
*tmp
;
2112 struct fib6_table
*tb
;
2114 hlist_for_each_entry_safe(tb
, tmp
, head
, tb6_hlist
) {
2115 hlist_del(&tb
->tb6_hlist
);
2116 fib6_free_table(tb
);
2120 kfree(net
->ipv6
.fib_table_hash
);
2121 kfree(net
->ipv6
.rt6_stats
);
2122 fib6_notifier_exit(net
);
2125 static struct pernet_operations fib6_net_ops
= {
2126 .init
= fib6_net_init
,
2127 .exit
= fib6_net_exit
,
2130 int __init
fib6_init(void)
2134 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
2135 sizeof(struct fib6_node
),
2136 0, SLAB_HWCACHE_ALIGN
,
2138 if (!fib6_node_kmem
)
2141 ret
= register_pernet_subsys(&fib6_net_ops
);
2143 goto out_kmem_cache_create
;
2145 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
2148 goto out_unregister_subsys
;
2150 __fib6_flush_trees
= fib6_flush_trees
;
2154 out_unregister_subsys
:
2155 unregister_pernet_subsys(&fib6_net_ops
);
2156 out_kmem_cache_create
:
2157 kmem_cache_destroy(fib6_node_kmem
);
2161 void fib6_gc_cleanup(void)
2163 unregister_pernet_subsys(&fib6_net_ops
);
2164 kmem_cache_destroy(fib6_node_kmem
);
2167 #ifdef CONFIG_PROC_FS
2169 struct ipv6_route_iter
{
2170 struct seq_net_private p
;
2171 struct fib6_walker w
;
2173 struct fib6_table
*tbl
;
2177 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
2179 struct rt6_info
*rt
= v
;
2180 struct ipv6_route_iter
*iter
= seq
->private;
2182 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
2184 #ifdef CONFIG_IPV6_SUBTREES
2185 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
2187 seq_puts(seq
, "00000000000000000000000000000000 00 ");
2189 if (rt
->rt6i_flags
& RTF_GATEWAY
)
2190 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
2192 seq_puts(seq
, "00000000000000000000000000000000");
2194 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
2195 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
2196 rt
->dst
.__use
, rt
->rt6i_flags
,
2197 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
2198 iter
->w
.leaf
= NULL
;
2202 static int ipv6_route_yield(struct fib6_walker
*w
)
2204 struct ipv6_route_iter
*iter
= w
->args
;
2210 iter
->w
.leaf
= rcu_dereference_protected(
2211 iter
->w
.leaf
->dst
.rt6_next
,
2212 lockdep_is_held(&iter
->tbl
->tb6_lock
));
2214 if (!iter
->skip
&& iter
->w
.leaf
)
2216 } while (iter
->w
.leaf
);
2221 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
,
2224 memset(&iter
->w
, 0, sizeof(iter
->w
));
2225 iter
->w
.func
= ipv6_route_yield
;
2226 iter
->w
.root
= &iter
->tbl
->tb6_root
;
2227 iter
->w
.state
= FWS_INIT
;
2228 iter
->w
.node
= iter
->w
.root
;
2229 iter
->w
.args
= iter
;
2230 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2231 INIT_LIST_HEAD(&iter
->w
.lh
);
2232 fib6_walker_link(net
, &iter
->w
);
2235 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
2239 struct hlist_node
*node
;
2242 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
2243 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
2249 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
2250 node
= rcu_dereference_bh(
2251 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
2253 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
2256 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
2258 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
2259 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2260 iter
->w
.state
= FWS_INIT
;
2261 iter
->w
.node
= iter
->w
.root
;
2262 WARN_ON(iter
->w
.skip
);
2263 iter
->w
.skip
= iter
->w
.count
;
2267 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2271 struct net
*net
= seq_file_net(seq
);
2272 struct ipv6_route_iter
*iter
= seq
->private;
2277 n
= rcu_dereference_bh(((struct rt6_info
*)v
)->dst
.rt6_next
);
2284 ipv6_route_check_sernum(iter
);
2285 spin_lock_bh(&iter
->tbl
->tb6_lock
);
2286 r
= fib6_walk_continue(&iter
->w
);
2287 spin_unlock_bh(&iter
->tbl
->tb6_lock
);
2291 return iter
->w
.leaf
;
2293 fib6_walker_unlink(net
, &iter
->w
);
2296 fib6_walker_unlink(net
, &iter
->w
);
2298 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2302 ipv6_route_seq_setup_walk(iter
, net
);
2306 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2309 struct net
*net
= seq_file_net(seq
);
2310 struct ipv6_route_iter
*iter
= seq
->private;
2313 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2317 ipv6_route_seq_setup_walk(iter
, net
);
2318 return ipv6_route_seq_next(seq
, NULL
, pos
);
2324 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2326 struct fib6_walker
*w
= &iter
->w
;
2327 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2330 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2333 struct net
*net
= seq_file_net(seq
);
2334 struct ipv6_route_iter
*iter
= seq
->private;
2336 if (ipv6_route_iter_active(iter
))
2337 fib6_walker_unlink(net
, &iter
->w
);
2339 rcu_read_unlock_bh();
2342 static const struct seq_operations ipv6_route_seq_ops
= {
2343 .start
= ipv6_route_seq_start
,
2344 .next
= ipv6_route_seq_next
,
2345 .stop
= ipv6_route_seq_stop
,
2346 .show
= ipv6_route_seq_show
2349 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2351 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2352 sizeof(struct ipv6_route_iter
));
2355 #endif /* CONFIG_PROC_FS */