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 net
*net
= dev_net(rt
->dst
.dev
);
111 struct fib6_node
*fn
;
113 fn
= rcu_dereference_protected(rt
->rt6i_node
,
114 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
116 fn
->fn_sernum
= fib6_new_sernum(net
);
120 * Auxiliary address test functions for the radix tree.
122 * These assume a 32bit processor (although it will work on
129 #if defined(__LITTLE_ENDIAN)
130 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
132 # define BITOP_BE32_SWIZZLE 0
135 static __be32
addr_bit_set(const void *token
, int fn_bit
)
137 const __be32
*addr
= token
;
140 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
141 * is optimized version of
142 * htonl(1 << ((~fn_bit)&0x1F))
143 * See include/asm-generic/bitops/le.h.
145 return (__force __be32
)(1 << ((~fn_bit
^ BITOP_BE32_SWIZZLE
) & 0x1f)) &
149 static struct fib6_node
*node_alloc(struct net
*net
)
151 struct fib6_node
*fn
;
153 fn
= kmem_cache_zalloc(fib6_node_kmem
, GFP_ATOMIC
);
155 net
->ipv6
.rt6_stats
->fib_nodes
++;
160 static void node_free_immediate(struct net
*net
, struct fib6_node
*fn
)
162 kmem_cache_free(fib6_node_kmem
, fn
);
163 net
->ipv6
.rt6_stats
->fib_nodes
--;
166 static void node_free_rcu(struct rcu_head
*head
)
168 struct fib6_node
*fn
= container_of(head
, struct fib6_node
, rcu
);
170 kmem_cache_free(fib6_node_kmem
, fn
);
173 static void node_free(struct net
*net
, struct fib6_node
*fn
)
175 call_rcu(&fn
->rcu
, node_free_rcu
);
176 net
->ipv6
.rt6_stats
->fib_nodes
--;
179 void rt6_free_pcpu(struct rt6_info
*non_pcpu_rt
)
183 if (!non_pcpu_rt
->rt6i_pcpu
)
186 for_each_possible_cpu(cpu
) {
187 struct rt6_info
**ppcpu_rt
;
188 struct rt6_info
*pcpu_rt
;
190 ppcpu_rt
= per_cpu_ptr(non_pcpu_rt
->rt6i_pcpu
, cpu
);
193 dst_dev_put(&pcpu_rt
->dst
);
194 dst_release(&pcpu_rt
->dst
);
199 EXPORT_SYMBOL_GPL(rt6_free_pcpu
);
201 static void fib6_free_table(struct fib6_table
*table
)
203 inetpeer_invalidate_tree(&table
->tb6_peers
);
207 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
212 * Initialize table lock at a single place to give lockdep a key,
213 * tables aren't visible prior to being linked to the list.
215 spin_lock_init(&tb
->tb6_lock
);
216 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
219 * No protection necessary, this is the only list mutatation
220 * operation, tables never disappear once they exist.
222 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
225 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
227 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
229 struct fib6_table
*table
;
231 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
234 rcu_assign_pointer(table
->tb6_root
.leaf
,
235 net
->ipv6
.ip6_null_entry
);
236 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
237 inet_peer_base_init(&table
->tb6_peers
);
243 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
245 struct fib6_table
*tb
;
249 tb
= fib6_get_table(net
, id
);
253 tb
= fib6_alloc_table(net
, id
);
255 fib6_link_table(net
, tb
);
259 EXPORT_SYMBOL_GPL(fib6_new_table
);
261 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
263 struct fib6_table
*tb
;
264 struct hlist_head
*head
;
269 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
271 head
= &net
->ipv6
.fib_table_hash
[h
];
272 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
273 if (tb
->tb6_id
== id
) {
282 EXPORT_SYMBOL_GPL(fib6_get_table
);
284 static void __net_init
fib6_tables_init(struct net
*net
)
286 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
287 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
291 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
293 return fib6_get_table(net
, id
);
296 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
298 return net
->ipv6
.fib6_main_tbl
;
301 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
302 int flags
, pol_lookup_t lookup
)
306 rt
= lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
307 if (rt
->dst
.error
== -EAGAIN
) {
309 rt
= net
->ipv6
.ip6_null_entry
;
316 static void __net_init
fib6_tables_init(struct net
*net
)
318 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
323 unsigned int fib6_tables_seq_read(struct net
*net
)
325 unsigned int h
, fib_seq
= 0;
328 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
329 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[h
];
330 struct fib6_table
*tb
;
332 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
)
333 fib_seq
+= tb
->fib_seq
;
340 static int call_fib6_entry_notifier(struct notifier_block
*nb
, struct net
*net
,
341 enum fib_event_type event_type
,
344 struct fib6_entry_notifier_info info
= {
348 return call_fib6_notifier(nb
, net
, event_type
, &info
.info
);
351 static int call_fib6_entry_notifiers(struct net
*net
,
352 enum fib_event_type event_type
,
354 struct netlink_ext_ack
*extack
)
356 struct fib6_entry_notifier_info info
= {
357 .info
.extack
= extack
,
361 rt
->rt6i_table
->fib_seq
++;
362 return call_fib6_notifiers(net
, event_type
, &info
.info
);
365 struct fib6_dump_arg
{
367 struct notifier_block
*nb
;
370 static void fib6_rt_dump(struct rt6_info
*rt
, struct fib6_dump_arg
*arg
)
372 if (rt
== arg
->net
->ipv6
.ip6_null_entry
)
374 call_fib6_entry_notifier(arg
->nb
, arg
->net
, FIB_EVENT_ENTRY_ADD
, rt
);
377 static int fib6_node_dump(struct fib6_walker
*w
)
381 for_each_fib6_walker_rt(w
)
382 fib6_rt_dump(rt
, w
->args
);
387 static void fib6_table_dump(struct net
*net
, struct fib6_table
*tb
,
388 struct fib6_walker
*w
)
390 w
->root
= &tb
->tb6_root
;
391 spin_lock_bh(&tb
->tb6_lock
);
393 spin_unlock_bh(&tb
->tb6_lock
);
396 /* Called with rcu_read_lock() */
397 int fib6_tables_dump(struct net
*net
, struct notifier_block
*nb
)
399 struct fib6_dump_arg arg
;
400 struct fib6_walker
*w
;
403 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
407 w
->func
= fib6_node_dump
;
412 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
413 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[h
];
414 struct fib6_table
*tb
;
416 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
)
417 fib6_table_dump(net
, tb
, w
);
425 static int fib6_dump_node(struct fib6_walker
*w
)
430 for_each_fib6_walker_rt(w
) {
431 res
= rt6_dump_route(rt
, w
->args
);
433 /* Frame is full, suspend walking */
438 /* Multipath routes are dumped in one route with the
439 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
440 * last sibling of this route (no need to dump the
441 * sibling routes again)
443 if (rt
->rt6i_nsiblings
)
444 rt
= list_last_entry(&rt
->rt6i_siblings
,
452 static void fib6_dump_end(struct netlink_callback
*cb
)
454 struct net
*net
= sock_net(cb
->skb
->sk
);
455 struct fib6_walker
*w
= (void *)cb
->args
[2];
460 fib6_walker_unlink(net
, w
);
465 cb
->done
= (void *)cb
->args
[3];
469 static int fib6_dump_done(struct netlink_callback
*cb
)
472 return cb
->done
? cb
->done(cb
) : 0;
475 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
476 struct netlink_callback
*cb
)
478 struct net
*net
= sock_net(skb
->sk
);
479 struct fib6_walker
*w
;
482 w
= (void *)cb
->args
[2];
483 w
->root
= &table
->tb6_root
;
485 if (cb
->args
[4] == 0) {
489 spin_lock_bh(&table
->tb6_lock
);
490 res
= fib6_walk(net
, w
);
491 spin_unlock_bh(&table
->tb6_lock
);
494 cb
->args
[5] = w
->root
->fn_sernum
;
497 if (cb
->args
[5] != w
->root
->fn_sernum
) {
498 /* Begin at the root if the tree changed */
499 cb
->args
[5] = w
->root
->fn_sernum
;
506 spin_lock_bh(&table
->tb6_lock
);
507 res
= fib6_walk_continue(w
);
508 spin_unlock_bh(&table
->tb6_lock
);
510 fib6_walker_unlink(net
, w
);
518 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
520 struct net
*net
= sock_net(skb
->sk
);
522 unsigned int e
= 0, s_e
;
523 struct rt6_rtnl_dump_arg arg
;
524 struct fib6_walker
*w
;
525 struct fib6_table
*tb
;
526 struct hlist_head
*head
;
532 w
= (void *)cb
->args
[2];
536 * 1. hook callback destructor.
538 cb
->args
[3] = (long)cb
->done
;
539 cb
->done
= fib6_dump_done
;
542 * 2. allocate and initialize walker.
544 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
547 w
->func
= fib6_dump_node
;
548 cb
->args
[2] = (long)w
;
557 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
559 head
= &net
->ipv6
.fib_table_hash
[h
];
560 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
563 res
= fib6_dump_table(tb
, skb
, cb
);
575 res
= res
< 0 ? res
: skb
->len
;
584 * return the appropriate node for a routing tree "add" operation
585 * by either creating and inserting or by returning an existing
589 static struct fib6_node
*fib6_add_1(struct net
*net
,
590 struct fib6_table
*table
,
591 struct fib6_node
*root
,
592 struct in6_addr
*addr
, int plen
,
593 int offset
, int allow_create
,
594 int replace_required
,
595 struct netlink_ext_ack
*extack
)
597 struct fib6_node
*fn
, *in
, *ln
;
598 struct fib6_node
*pn
= NULL
;
603 RT6_TRACE("fib6_add_1\n");
605 /* insert node in tree */
610 struct rt6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
611 lockdep_is_held(&table
->tb6_lock
));
612 key
= (struct rt6key
*)((u8
*)leaf
+ offset
);
617 if (plen
< fn
->fn_bit
||
618 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
620 if (replace_required
) {
621 NL_SET_ERR_MSG(extack
,
622 "Can not replace route - no match found");
623 pr_warn("Can't replace route, no match found\n");
624 return ERR_PTR(-ENOENT
);
626 pr_warn("NLM_F_CREATE should be set when creating new route\n");
635 if (plen
== fn
->fn_bit
) {
636 /* clean up an intermediate node */
637 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
638 RCU_INIT_POINTER(fn
->leaf
, NULL
);
640 /* remove null_entry in the root node */
641 } else if (fn
->fn_flags
& RTN_TL_ROOT
&&
642 rcu_access_pointer(fn
->leaf
) ==
643 net
->ipv6
.ip6_null_entry
) {
644 RCU_INIT_POINTER(fn
->leaf
, NULL
);
651 * We have more bits to go
654 /* Try to walk down on tree. */
655 dir
= addr_bit_set(addr
, fn
->fn_bit
);
658 rcu_dereference_protected(fn
->right
,
659 lockdep_is_held(&table
->tb6_lock
)) :
660 rcu_dereference_protected(fn
->left
,
661 lockdep_is_held(&table
->tb6_lock
));
665 /* We should not create new node because
666 * NLM_F_REPLACE was specified without NLM_F_CREATE
667 * I assume it is safe to require NLM_F_CREATE when
668 * REPLACE flag is used! Later we may want to remove the
669 * check for replace_required, because according
670 * to netlink specification, NLM_F_CREATE
671 * MUST be specified if new route is created.
672 * That would keep IPv6 consistent with IPv4
674 if (replace_required
) {
675 NL_SET_ERR_MSG(extack
,
676 "Can not replace route - no match found");
677 pr_warn("Can't replace route, no match found\n");
678 return ERR_PTR(-ENOENT
);
680 pr_warn("NLM_F_CREATE should be set when creating new route\n");
683 * We walked to the bottom of tree.
684 * Create new leaf node without children.
687 ln
= node_alloc(net
);
690 return ERR_PTR(-ENOMEM
);
692 RCU_INIT_POINTER(ln
->parent
, pn
);
695 rcu_assign_pointer(pn
->right
, ln
);
697 rcu_assign_pointer(pn
->left
, ln
);
704 * split since we don't have a common prefix anymore or
705 * we have a less significant route.
706 * we've to insert an intermediate node on the list
707 * this new node will point to the one we need to create
711 pn
= rcu_dereference_protected(fn
->parent
,
712 lockdep_is_held(&table
->tb6_lock
));
714 /* find 1st bit in difference between the 2 addrs.
716 See comment in __ipv6_addr_diff: bit may be an invalid value,
717 but if it is >= plen, the value is ignored in any case.
720 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
725 * (new leaf node)[ln] (old node)[fn]
728 in
= node_alloc(net
);
729 ln
= node_alloc(net
);
733 node_free_immediate(net
, in
);
735 node_free_immediate(net
, ln
);
736 return ERR_PTR(-ENOMEM
);
740 * new intermediate node.
742 * be off since that an address that chooses one of
743 * the branches would not match less specific routes
744 * in the other branch
749 RCU_INIT_POINTER(in
->parent
, pn
);
751 atomic_inc(&rcu_dereference_protected(in
->leaf
,
752 lockdep_is_held(&table
->tb6_lock
))->rt6i_ref
);
754 /* update parent pointer */
756 rcu_assign_pointer(pn
->right
, in
);
758 rcu_assign_pointer(pn
->left
, in
);
762 RCU_INIT_POINTER(ln
->parent
, in
);
763 rcu_assign_pointer(fn
->parent
, in
);
765 if (addr_bit_set(addr
, bit
)) {
766 rcu_assign_pointer(in
->right
, ln
);
767 rcu_assign_pointer(in
->left
, fn
);
769 rcu_assign_pointer(in
->left
, ln
);
770 rcu_assign_pointer(in
->right
, fn
);
772 } else { /* plen <= bit */
775 * (new leaf node)[ln]
777 * (old node)[fn] NULL
780 ln
= node_alloc(net
);
783 return ERR_PTR(-ENOMEM
);
787 RCU_INIT_POINTER(ln
->parent
, pn
);
789 if (addr_bit_set(&key
->addr
, plen
))
790 RCU_INIT_POINTER(ln
->right
, fn
);
792 RCU_INIT_POINTER(ln
->left
, fn
);
794 rcu_assign_pointer(fn
->parent
, ln
);
797 rcu_assign_pointer(pn
->right
, ln
);
799 rcu_assign_pointer(pn
->left
, ln
);
804 static void fib6_copy_metrics(u32
*mp
, const struct mx6_config
*mxc
)
808 for (i
= 0; i
< RTAX_MAX
; i
++) {
809 if (test_bit(i
, mxc
->mx_valid
))
814 static int fib6_commit_metrics(struct dst_entry
*dst
, struct mx6_config
*mxc
)
819 if (dst
->flags
& DST_HOST
) {
820 u32
*mp
= dst_metrics_write_ptr(dst
);
825 fib6_copy_metrics(mp
, mxc
);
827 dst_init_metrics(dst
, mxc
->mx
, false);
829 /* We've stolen mx now. */
836 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
839 struct fib6_table
*table
= rt
->rt6i_table
;
841 if (atomic_read(&rt
->rt6i_ref
) != 1) {
842 /* This route is used as dummy address holder in some split
843 * nodes. It is not leaked, but it still holds other resources,
844 * which must be released in time. So, scan ascendant nodes
845 * and replace dummy references to this route with references
846 * to still alive ones.
849 struct rt6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
850 lockdep_is_held(&table
->tb6_lock
));
851 struct rt6_info
*new_leaf
;
852 if (!(fn
->fn_flags
& RTN_RTINFO
) && leaf
== rt
) {
853 new_leaf
= fib6_find_prefix(net
, table
, fn
);
854 atomic_inc(&new_leaf
->rt6i_ref
);
855 rcu_assign_pointer(fn
->leaf
, new_leaf
);
858 fn
= rcu_dereference_protected(fn
->parent
,
859 lockdep_is_held(&table
->tb6_lock
));
865 * Insert routing information in a node.
868 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
869 struct nl_info
*info
, struct mx6_config
*mxc
,
870 struct netlink_ext_ack
*extack
)
872 struct rt6_info
*leaf
= rcu_dereference_protected(fn
->leaf
,
873 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
874 struct rt6_info
*iter
= NULL
;
875 struct rt6_info __rcu
**ins
;
876 struct rt6_info __rcu
**fallback_ins
= NULL
;
877 int replace
= (info
->nlh
&&
878 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
879 int add
= (!info
->nlh
||
880 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
882 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
883 u16 nlflags
= NLM_F_EXCL
;
886 if (info
->nlh
&& (info
->nlh
->nlmsg_flags
& NLM_F_APPEND
))
887 nlflags
|= NLM_F_APPEND
;
891 for (iter
= leaf
; iter
;
892 iter
= rcu_dereference_protected(iter
->rt6_next
,
893 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
))) {
895 * Search for duplicates
898 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
900 * Same priority level
903 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
906 nlflags
&= ~NLM_F_EXCL
;
908 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
913 fallback_ins
= fallback_ins
?: ins
;
917 if (rt6_duplicate_nexthop(iter
, rt
)) {
918 if (rt
->rt6i_nsiblings
)
919 rt
->rt6i_nsiblings
= 0;
920 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
922 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
923 rt6_clean_expires(iter
);
925 rt6_set_expires(iter
, rt
->dst
.expires
);
926 iter
->rt6i_pmtu
= rt
->rt6i_pmtu
;
929 /* If we have the same destination and the same metric,
930 * but not the same gateway, then the route we try to
931 * add is sibling to this route, increment our counter
932 * of siblings, and later we will add our route to the
934 * Only static routes (which don't have flag
935 * RTF_EXPIRES) are used for ECMPv6.
937 * To avoid long list, we only had siblings if the
938 * route have a gateway.
941 rt6_qualify_for_ecmp(iter
))
942 rt
->rt6i_nsiblings
++;
945 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
949 ins
= &iter
->rt6_next
;
952 if (fallback_ins
&& !found
) {
953 /* No ECMP-able route found, replace first non-ECMP one */
955 iter
= rcu_dereference_protected(*ins
,
956 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
960 /* Reset round-robin state, if necessary */
961 if (ins
== &fn
->leaf
)
964 /* Link this route to others same route. */
965 if (rt
->rt6i_nsiblings
) {
966 unsigned int rt6i_nsiblings
;
967 struct rt6_info
*sibling
, *temp_sibling
;
969 /* Find the first route that have the same metric */
972 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
973 rt6_qualify_for_ecmp(sibling
)) {
974 list_add_tail(&rt
->rt6i_siblings
,
975 &sibling
->rt6i_siblings
);
978 sibling
= rcu_dereference_protected(sibling
->rt6_next
,
979 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
981 /* For each sibling in the list, increment the counter of
982 * siblings. BUG() if counters does not match, list of siblings
986 list_for_each_entry_safe(sibling
, temp_sibling
,
987 &rt
->rt6i_siblings
, rt6i_siblings
) {
988 sibling
->rt6i_nsiblings
++;
989 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
992 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
993 rt6_multipath_rebalance(temp_sibling
);
1001 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1004 nlflags
|= NLM_F_CREATE
;
1005 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
1009 rcu_assign_pointer(rt
->rt6_next
, iter
);
1010 atomic_inc(&rt
->rt6i_ref
);
1011 rcu_assign_pointer(rt
->rt6i_node
, fn
);
1012 rcu_assign_pointer(*ins
, rt
);
1013 call_fib6_entry_notifiers(info
->nl_net
, FIB_EVENT_ENTRY_ADD
,
1015 if (!info
->skip_notify
)
1016 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, nlflags
);
1017 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
1019 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
1020 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
1021 fn
->fn_flags
|= RTN_RTINFO
;
1030 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1034 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
1038 atomic_inc(&rt
->rt6i_ref
);
1039 rcu_assign_pointer(rt
->rt6i_node
, fn
);
1040 rt
->rt6_next
= iter
->rt6_next
;
1041 rcu_assign_pointer(*ins
, rt
);
1042 call_fib6_entry_notifiers(info
->nl_net
, FIB_EVENT_ENTRY_REPLACE
,
1044 if (!info
->skip_notify
)
1045 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, NLM_F_REPLACE
);
1046 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
1047 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
1048 fn
->fn_flags
|= RTN_RTINFO
;
1050 nsiblings
= iter
->rt6i_nsiblings
;
1051 iter
->rt6i_node
= NULL
;
1052 fib6_purge_rt(iter
, fn
, info
->nl_net
);
1053 if (rcu_access_pointer(fn
->rr_ptr
) == iter
)
1058 /* Replacing an ECMP route, remove all siblings */
1059 ins
= &rt
->rt6_next
;
1060 iter
= rcu_dereference_protected(*ins
,
1061 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1063 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
1065 if (rt6_qualify_for_ecmp(iter
)) {
1066 *ins
= iter
->rt6_next
;
1067 iter
->rt6i_node
= NULL
;
1068 fib6_purge_rt(iter
, fn
, info
->nl_net
);
1069 if (rcu_access_pointer(fn
->rr_ptr
) == iter
)
1073 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1075 ins
= &iter
->rt6_next
;
1077 iter
= rcu_dereference_protected(*ins
,
1078 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1080 WARN_ON(nsiblings
!= 0);
1087 static void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
1089 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
1090 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
1091 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1092 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
1095 void fib6_force_start_gc(struct net
*net
)
1097 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
1098 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1099 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
1102 static void __fib6_update_sernum_upto_root(struct rt6_info
*rt
,
1105 struct fib6_node
*fn
= rcu_dereference_protected(rt
->rt6i_node
,
1106 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1108 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1111 fn
->fn_sernum
= sernum
;
1112 fn
= rcu_dereference_protected(fn
->parent
,
1113 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1117 void fib6_update_sernum_upto_root(struct net
*net
, struct rt6_info
*rt
)
1119 __fib6_update_sernum_upto_root(rt
, fib6_new_sernum(net
));
1123 * Add routing information to the routing tree.
1124 * <destination addr>/<source addr>
1125 * with source addr info in sub-trees
1126 * Need to own table->tb6_lock
1129 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
1130 struct nl_info
*info
, struct mx6_config
*mxc
,
1131 struct netlink_ext_ack
*extack
)
1133 struct fib6_table
*table
= rt
->rt6i_table
;
1134 struct fib6_node
*fn
, *pn
= NULL
;
1136 int allow_create
= 1;
1137 int replace_required
= 0;
1138 int sernum
= fib6_new_sernum(info
->nl_net
);
1140 if (WARN_ON_ONCE(!atomic_read(&rt
->dst
.__refcnt
)))
1142 if (WARN_ON_ONCE(rt
->rt6i_flags
& RTF_CACHE
))
1146 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
1148 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1149 replace_required
= 1;
1151 if (!allow_create
&& !replace_required
)
1152 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1154 fn
= fib6_add_1(info
->nl_net
, table
, root
,
1155 &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
,
1156 offsetof(struct rt6_info
, rt6i_dst
), allow_create
,
1157 replace_required
, extack
);
1166 #ifdef CONFIG_IPV6_SUBTREES
1167 if (rt
->rt6i_src
.plen
) {
1168 struct fib6_node
*sn
;
1170 if (!rcu_access_pointer(fn
->subtree
)) {
1171 struct fib6_node
*sfn
;
1183 /* Create subtree root node */
1184 sfn
= node_alloc(info
->nl_net
);
1188 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
1189 rcu_assign_pointer(sfn
->leaf
,
1190 info
->nl_net
->ipv6
.ip6_null_entry
);
1191 sfn
->fn_flags
= RTN_ROOT
;
1193 /* Now add the first leaf node to new subtree */
1195 sn
= fib6_add_1(info
->nl_net
, table
, sfn
,
1196 &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
,
1197 offsetof(struct rt6_info
, rt6i_src
),
1198 allow_create
, replace_required
, extack
);
1201 /* If it is failed, discard just allocated
1202 root, and then (in failure) stale node
1205 node_free_immediate(info
->nl_net
, sfn
);
1210 /* Now link new subtree to main tree */
1211 rcu_assign_pointer(sfn
->parent
, fn
);
1212 rcu_assign_pointer(fn
->subtree
, sfn
);
1214 sn
= fib6_add_1(info
->nl_net
, table
, FIB6_SUBTREE(fn
),
1215 &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
,
1216 offsetof(struct rt6_info
, rt6i_src
),
1217 allow_create
, replace_required
, extack
);
1225 if (!rcu_access_pointer(fn
->leaf
)) {
1226 if (fn
->fn_flags
& RTN_TL_ROOT
) {
1227 /* put back null_entry for root node */
1228 rcu_assign_pointer(fn
->leaf
,
1229 info
->nl_net
->ipv6
.ip6_null_entry
);
1231 atomic_inc(&rt
->rt6i_ref
);
1232 rcu_assign_pointer(fn
->leaf
, rt
);
1239 err
= fib6_add_rt2node(fn
, rt
, info
, mxc
, extack
);
1241 __fib6_update_sernum_upto_root(rt
, sernum
);
1242 fib6_start_gc(info
->nl_net
, rt
);
1247 #ifdef CONFIG_IPV6_SUBTREES
1249 * If fib6_add_1 has cleared the old leaf pointer in the
1250 * super-tree leaf node we have to find a new one for it.
1253 struct rt6_info
*pn_leaf
=
1254 rcu_dereference_protected(pn
->leaf
,
1255 lockdep_is_held(&table
->tb6_lock
));
1256 if (pn_leaf
== rt
) {
1258 RCU_INIT_POINTER(pn
->leaf
, NULL
);
1259 atomic_dec(&rt
->rt6i_ref
);
1261 if (!pn_leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
1262 pn_leaf
= fib6_find_prefix(info
->nl_net
, table
,
1268 info
->nl_net
->ipv6
.ip6_null_entry
;
1271 atomic_inc(&pn_leaf
->rt6i_ref
);
1272 rcu_assign_pointer(pn
->leaf
, pn_leaf
);
1281 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1282 * 1. fn is an intermediate node and we failed to add the new
1283 * route to it in both subtree creation failure and fib6_add_rt2node()
1285 * 2. fn is the root node in the table and we fail to add the first
1286 * default route to it.
1289 (!(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)) ||
1290 (fn
->fn_flags
& RTN_TL_ROOT
&&
1291 !rcu_access_pointer(fn
->leaf
))))
1292 fib6_repair_tree(info
->nl_net
, table
, fn
);
1293 /* Always release dst as dst->__refcnt is guaranteed
1294 * to be taken before entering this function
1296 dst_release_immediate(&rt
->dst
);
1301 * Routing tree lookup
1305 struct lookup_args
{
1306 int offset
; /* key offset on rt6_info */
1307 const struct in6_addr
*addr
; /* search key */
1310 static struct fib6_node
*fib6_lookup_1(struct fib6_node
*root
,
1311 struct lookup_args
*args
)
1313 struct fib6_node
*fn
;
1316 if (unlikely(args
->offset
== 0))
1326 struct fib6_node
*next
;
1328 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1330 next
= dir
? rcu_dereference(fn
->right
) :
1331 rcu_dereference(fn
->left
);
1341 struct fib6_node
*subtree
= FIB6_SUBTREE(fn
);
1343 if (subtree
|| fn
->fn_flags
& RTN_RTINFO
) {
1344 struct rt6_info
*leaf
= rcu_dereference(fn
->leaf
);
1350 key
= (struct rt6key
*) ((u8
*)leaf
+ args
->offset
);
1352 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1353 #ifdef CONFIG_IPV6_SUBTREES
1355 struct fib6_node
*sfn
;
1356 sfn
= fib6_lookup_1(subtree
, args
+ 1);
1362 if (fn
->fn_flags
& RTN_RTINFO
)
1367 if (fn
->fn_flags
& RTN_ROOT
)
1370 fn
= rcu_dereference(fn
->parent
);
1376 /* called with rcu_read_lock() held
1378 struct fib6_node
*fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1379 const struct in6_addr
*saddr
)
1381 struct fib6_node
*fn
;
1382 struct lookup_args args
[] = {
1384 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1387 #ifdef CONFIG_IPV6_SUBTREES
1389 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1394 .offset
= 0, /* sentinel */
1398 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1399 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1406 * Get node with specified destination prefix (and source prefix,
1407 * if subtrees are used)
1408 * exact_match == true means we try to find fn with exact match of
1409 * the passed in prefix addr
1410 * exact_match == false means we try to find fn with longest prefix
1411 * match of the passed in prefix addr. This is useful for finding fn
1412 * for cached route as it will be stored in the exception table under
1413 * the node with longest prefix length.
1417 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1418 const struct in6_addr
*addr
,
1419 int plen
, int offset
,
1422 struct fib6_node
*fn
, *prev
= NULL
;
1424 for (fn
= root
; fn
; ) {
1425 struct rt6_info
*leaf
= rcu_dereference(fn
->leaf
);
1428 /* This node is being deleted */
1430 if (plen
<= fn
->fn_bit
)
1436 key
= (struct rt6key
*)((u8
*)leaf
+ offset
);
1441 if (plen
< fn
->fn_bit
||
1442 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1445 if (plen
== fn
->fn_bit
)
1452 * We have more bits to go
1454 if (addr_bit_set(addr
, fn
->fn_bit
))
1455 fn
= rcu_dereference(fn
->right
);
1457 fn
= rcu_dereference(fn
->left
);
1466 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1467 const struct in6_addr
*daddr
, int dst_len
,
1468 const struct in6_addr
*saddr
, int src_len
,
1471 struct fib6_node
*fn
;
1473 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1474 offsetof(struct rt6_info
, rt6i_dst
),
1477 #ifdef CONFIG_IPV6_SUBTREES
1479 WARN_ON(saddr
== NULL
);
1481 struct fib6_node
*subtree
= FIB6_SUBTREE(fn
);
1484 fn
= fib6_locate_1(subtree
, saddr
, src_len
,
1485 offsetof(struct rt6_info
, rt6i_src
),
1492 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1504 static struct rt6_info
*fib6_find_prefix(struct net
*net
,
1505 struct fib6_table
*table
,
1506 struct fib6_node
*fn
)
1508 struct fib6_node
*child_left
, *child_right
;
1510 if (fn
->fn_flags
& RTN_ROOT
)
1511 return net
->ipv6
.ip6_null_entry
;
1514 child_left
= rcu_dereference_protected(fn
->left
,
1515 lockdep_is_held(&table
->tb6_lock
));
1516 child_right
= rcu_dereference_protected(fn
->right
,
1517 lockdep_is_held(&table
->tb6_lock
));
1519 return rcu_dereference_protected(child_left
->leaf
,
1520 lockdep_is_held(&table
->tb6_lock
));
1522 return rcu_dereference_protected(child_right
->leaf
,
1523 lockdep_is_held(&table
->tb6_lock
));
1525 fn
= FIB6_SUBTREE(fn
);
1531 * Called to trim the tree of intermediate nodes when possible. "fn"
1532 * is the node we want to try and remove.
1533 * Need to own table->tb6_lock
1536 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1537 struct fib6_table
*table
,
1538 struct fib6_node
*fn
)
1542 struct fib6_node
*child
;
1543 struct fib6_walker
*w
;
1546 /* Set fn->leaf to null_entry for root node. */
1547 if (fn
->fn_flags
& RTN_TL_ROOT
) {
1548 rcu_assign_pointer(fn
->leaf
, net
->ipv6
.ip6_null_entry
);
1553 struct fib6_node
*fn_r
= rcu_dereference_protected(fn
->right
,
1554 lockdep_is_held(&table
->tb6_lock
));
1555 struct fib6_node
*fn_l
= rcu_dereference_protected(fn
->left
,
1556 lockdep_is_held(&table
->tb6_lock
));
1557 struct fib6_node
*pn
= rcu_dereference_protected(fn
->parent
,
1558 lockdep_is_held(&table
->tb6_lock
));
1559 struct fib6_node
*pn_r
= rcu_dereference_protected(pn
->right
,
1560 lockdep_is_held(&table
->tb6_lock
));
1561 struct fib6_node
*pn_l
= rcu_dereference_protected(pn
->left
,
1562 lockdep_is_held(&table
->tb6_lock
));
1563 struct rt6_info
*fn_leaf
= rcu_dereference_protected(fn
->leaf
,
1564 lockdep_is_held(&table
->tb6_lock
));
1565 struct rt6_info
*pn_leaf
= rcu_dereference_protected(pn
->leaf
,
1566 lockdep_is_held(&table
->tb6_lock
));
1567 struct rt6_info
*new_fn_leaf
;
1569 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1572 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1573 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1579 child
= fn_r
, children
|= 1;
1581 child
= fn_l
, children
|= 2;
1583 if (children
== 3 || FIB6_SUBTREE(fn
)
1584 #ifdef CONFIG_IPV6_SUBTREES
1585 /* Subtree root (i.e. fn) may have one child */
1586 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1589 new_fn_leaf
= fib6_find_prefix(net
, table
, fn
);
1592 WARN_ON(!new_fn_leaf
);
1593 new_fn_leaf
= net
->ipv6
.ip6_null_entry
;
1596 atomic_inc(&new_fn_leaf
->rt6i_ref
);
1597 rcu_assign_pointer(fn
->leaf
, new_fn_leaf
);
1601 #ifdef CONFIG_IPV6_SUBTREES
1602 if (FIB6_SUBTREE(pn
) == fn
) {
1603 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1604 RCU_INIT_POINTER(pn
->subtree
, NULL
);
1607 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1610 rcu_assign_pointer(pn
->right
, child
);
1611 else if (pn_l
== fn
)
1612 rcu_assign_pointer(pn
->left
, child
);
1618 rcu_assign_pointer(child
->parent
, pn
);
1620 #ifdef CONFIG_IPV6_SUBTREES
1624 read_lock(&net
->ipv6
.fib6_walker_lock
);
1625 FOR_WALKERS(net
, w
) {
1627 if (w
->node
== fn
) {
1628 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1633 if (w
->node
== fn
) {
1636 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1637 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1639 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1640 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1645 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1648 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1651 RCU_INIT_POINTER(pn
->leaf
, NULL
);
1652 rt6_release(pn_leaf
);
1657 static void fib6_del_route(struct fib6_table
*table
, struct fib6_node
*fn
,
1658 struct rt6_info __rcu
**rtp
, struct nl_info
*info
)
1660 struct fib6_walker
*w
;
1661 struct rt6_info
*rt
= rcu_dereference_protected(*rtp
,
1662 lockdep_is_held(&table
->tb6_lock
));
1663 struct net
*net
= info
->nl_net
;
1665 RT6_TRACE("fib6_del_route\n");
1667 WARN_ON_ONCE(rt
->rt6i_flags
& RTF_CACHE
);
1670 *rtp
= rt
->rt6_next
;
1671 rt
->rt6i_node
= NULL
;
1672 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1673 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1675 /* Flush all cached dst in exception table */
1676 rt6_flush_exceptions(rt
);
1678 /* Reset round-robin state, if necessary */
1679 if (rcu_access_pointer(fn
->rr_ptr
) == rt
)
1682 /* Remove this entry from other siblings */
1683 if (rt
->rt6i_nsiblings
) {
1684 struct rt6_info
*sibling
, *next_sibling
;
1686 list_for_each_entry_safe(sibling
, next_sibling
,
1687 &rt
->rt6i_siblings
, rt6i_siblings
)
1688 sibling
->rt6i_nsiblings
--;
1689 rt
->rt6i_nsiblings
= 0;
1690 list_del_init(&rt
->rt6i_siblings
);
1691 rt6_multipath_rebalance(next_sibling
);
1694 /* Adjust walkers */
1695 read_lock(&net
->ipv6
.fib6_walker_lock
);
1696 FOR_WALKERS(net
, w
) {
1697 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1698 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1699 w
->leaf
= rcu_dereference_protected(rt
->rt6_next
,
1700 lockdep_is_held(&table
->tb6_lock
));
1705 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1707 /* If it was last route, call fib6_repair_tree() to:
1708 * 1. For root node, put back null_entry as how the table was created.
1709 * 2. For other nodes, expunge its radix tree node.
1711 if (!rcu_access_pointer(fn
->leaf
)) {
1712 if (!(fn
->fn_flags
& RTN_TL_ROOT
)) {
1713 fn
->fn_flags
&= ~RTN_RTINFO
;
1714 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1716 fn
= fib6_repair_tree(net
, table
, fn
);
1719 fib6_purge_rt(rt
, fn
, net
);
1721 call_fib6_entry_notifiers(net
, FIB_EVENT_ENTRY_DEL
, rt
, NULL
);
1722 if (!info
->skip_notify
)
1723 inet6_rt_notify(RTM_DELROUTE
, rt
, info
, 0);
1727 /* Need to own table->tb6_lock */
1728 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1730 struct fib6_node
*fn
= rcu_dereference_protected(rt
->rt6i_node
,
1731 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1732 struct fib6_table
*table
= rt
->rt6i_table
;
1733 struct net
*net
= info
->nl_net
;
1734 struct rt6_info __rcu
**rtp
;
1735 struct rt6_info __rcu
**rtp_next
;
1738 if (rt
->dst
.obsolete
> 0) {
1743 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1746 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1748 /* remove cached dst from exception table */
1749 if (rt
->rt6i_flags
& RTF_CACHE
)
1750 return rt6_remove_exception_rt(rt
);
1753 * Walk the leaf entries looking for ourself
1756 for (rtp
= &fn
->leaf
; *rtp
; rtp
= rtp_next
) {
1757 struct rt6_info
*cur
= rcu_dereference_protected(*rtp
,
1758 lockdep_is_held(&table
->tb6_lock
));
1760 fib6_del_route(table
, fn
, rtp
, info
);
1763 rtp_next
= &cur
->rt6_next
;
1769 * Tree traversal function.
1771 * Certainly, it is not interrupt safe.
1772 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1773 * It means, that we can modify tree during walking
1774 * and use this function for garbage collection, clone pruning,
1775 * cleaning tree when a device goes down etc. etc.
1777 * It guarantees that every node will be traversed,
1778 * and that it will be traversed only once.
1780 * Callback function w->func may return:
1781 * 0 -> continue walking.
1782 * positive value -> walking is suspended (used by tree dumps,
1783 * and probably by gc, if it will be split to several slices)
1784 * negative value -> terminate walking.
1786 * The function itself returns:
1787 * 0 -> walk is complete.
1788 * >0 -> walk is incomplete (i.e. suspended)
1789 * <0 -> walk is terminated by an error.
1791 * This function is called with tb6_lock held.
1794 static int fib6_walk_continue(struct fib6_walker
*w
)
1796 struct fib6_node
*fn
, *pn
, *left
, *right
;
1798 /* w->root should always be table->tb6_root */
1799 WARN_ON_ONCE(!(w
->root
->fn_flags
& RTN_TL_ROOT
));
1807 #ifdef CONFIG_IPV6_SUBTREES
1809 if (FIB6_SUBTREE(fn
)) {
1810 w
->node
= FIB6_SUBTREE(fn
);
1817 left
= rcu_dereference_protected(fn
->left
, 1);
1820 w
->state
= FWS_INIT
;
1826 right
= rcu_dereference_protected(fn
->right
, 1);
1829 w
->state
= FWS_INIT
;
1833 w
->leaf
= rcu_dereference_protected(fn
->leaf
, 1);
1836 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1857 pn
= rcu_dereference_protected(fn
->parent
, 1);
1858 left
= rcu_dereference_protected(pn
->left
, 1);
1859 right
= rcu_dereference_protected(pn
->right
, 1);
1861 #ifdef CONFIG_IPV6_SUBTREES
1862 if (FIB6_SUBTREE(pn
) == fn
) {
1863 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1874 w
->leaf
= rcu_dereference_protected(w
->node
->leaf
, 1);
1884 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
)
1888 w
->state
= FWS_INIT
;
1891 fib6_walker_link(net
, w
);
1892 res
= fib6_walk_continue(w
);
1894 fib6_walker_unlink(net
, w
);
1898 static int fib6_clean_node(struct fib6_walker
*w
)
1901 struct rt6_info
*rt
;
1902 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1903 struct nl_info info
= {
1907 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1908 w
->node
->fn_sernum
!= c
->sernum
)
1909 w
->node
->fn_sernum
= c
->sernum
;
1912 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1917 for_each_fib6_walker_rt(w
) {
1918 res
= c
->func(rt
, c
->arg
);
1921 res
= fib6_del(rt
, &info
);
1924 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1926 rcu_access_pointer(rt
->rt6i_node
),
1932 } else if (res
== -2) {
1933 if (WARN_ON(!rt
->rt6i_nsiblings
))
1935 rt
= list_last_entry(&rt
->rt6i_siblings
,
1936 struct rt6_info
, rt6i_siblings
);
1946 * Convenient frontend to tree walker.
1948 * func is called on each route.
1949 * It may return -2 -> skip multipath route.
1950 * -1 -> delete this route.
1951 * 0 -> continue walking
1954 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1955 int (*func
)(struct rt6_info
*, void *arg
),
1956 int sernum
, void *arg
)
1958 struct fib6_cleaner c
;
1961 c
.w
.func
= fib6_clean_node
;
1969 fib6_walk(net
, &c
.w
);
1972 static void __fib6_clean_all(struct net
*net
,
1973 int (*func
)(struct rt6_info
*, void *),
1974 int sernum
, void *arg
)
1976 struct fib6_table
*table
;
1977 struct hlist_head
*head
;
1981 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1982 head
= &net
->ipv6
.fib_table_hash
[h
];
1983 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1984 spin_lock_bh(&table
->tb6_lock
);
1985 fib6_clean_tree(net
, &table
->tb6_root
,
1987 spin_unlock_bh(&table
->tb6_lock
);
1993 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *),
1996 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
);
1999 static void fib6_flush_trees(struct net
*net
)
2001 int new_sernum
= fib6_new_sernum(net
);
2003 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
);
2007 * Garbage collection
2010 static int fib6_age(struct rt6_info
*rt
, void *arg
)
2012 struct fib6_gc_args
*gc_args
= arg
;
2013 unsigned long now
= jiffies
;
2016 * check addrconf expiration here.
2017 * Routes are expired even if they are in use.
2020 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
2021 if (time_after(now
, rt
->dst
.expires
)) {
2022 RT6_TRACE("expiring %p\n", rt
);
2028 /* Also age clones in the exception table.
2029 * Note, that clones are aged out
2030 * only if they are not in use now.
2032 rt6_age_exceptions(rt
, gc_args
, now
);
2037 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
2039 struct fib6_gc_args gc_args
;
2043 spin_lock_bh(&net
->ipv6
.fib6_gc_lock
);
2044 } else if (!spin_trylock_bh(&net
->ipv6
.fib6_gc_lock
)) {
2045 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
2048 gc_args
.timeout
= expires
? (int)expires
:
2049 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
2052 fib6_clean_all(net
, fib6_age
, &gc_args
);
2054 net
->ipv6
.ip6_rt_last_gc
= now
;
2057 mod_timer(&net
->ipv6
.ip6_fib_timer
,
2059 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
2061 del_timer(&net
->ipv6
.ip6_fib_timer
);
2062 spin_unlock_bh(&net
->ipv6
.fib6_gc_lock
);
2065 static void fib6_gc_timer_cb(struct timer_list
*t
)
2067 struct net
*arg
= from_timer(arg
, t
, ipv6
.ip6_fib_timer
);
2069 fib6_run_gc(0, arg
, true);
2072 static int __net_init
fib6_net_init(struct net
*net
)
2074 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
2077 err
= fib6_notifier_init(net
);
2081 spin_lock_init(&net
->ipv6
.fib6_gc_lock
);
2082 rwlock_init(&net
->ipv6
.fib6_walker_lock
);
2083 INIT_LIST_HEAD(&net
->ipv6
.fib6_walkers
);
2084 timer_setup(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, 0);
2086 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
2087 if (!net
->ipv6
.rt6_stats
)
2090 /* Avoid false sharing : Use at least a full cache line */
2091 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
2093 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
2094 if (!net
->ipv6
.fib_table_hash
)
2097 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
2099 if (!net
->ipv6
.fib6_main_tbl
)
2100 goto out_fib_table_hash
;
2102 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
2103 rcu_assign_pointer(net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
,
2104 net
->ipv6
.ip6_null_entry
);
2105 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
2106 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
2107 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
2109 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2110 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
2112 if (!net
->ipv6
.fib6_local_tbl
)
2113 goto out_fib6_main_tbl
;
2114 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
2115 rcu_assign_pointer(net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
,
2116 net
->ipv6
.ip6_null_entry
);
2117 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
2118 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
2119 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
2121 fib6_tables_init(net
);
2125 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2127 kfree(net
->ipv6
.fib6_main_tbl
);
2130 kfree(net
->ipv6
.fib_table_hash
);
2132 kfree(net
->ipv6
.rt6_stats
);
2134 fib6_notifier_exit(net
);
2138 static void fib6_net_exit(struct net
*net
)
2142 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
2144 for (i
= 0; i
< FIB6_TABLE_HASHSZ
; i
++) {
2145 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[i
];
2146 struct hlist_node
*tmp
;
2147 struct fib6_table
*tb
;
2149 hlist_for_each_entry_safe(tb
, tmp
, head
, tb6_hlist
) {
2150 hlist_del(&tb
->tb6_hlist
);
2151 fib6_free_table(tb
);
2155 kfree(net
->ipv6
.fib_table_hash
);
2156 kfree(net
->ipv6
.rt6_stats
);
2157 fib6_notifier_exit(net
);
2160 static struct pernet_operations fib6_net_ops
= {
2161 .init
= fib6_net_init
,
2162 .exit
= fib6_net_exit
,
2165 int __init
fib6_init(void)
2169 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
2170 sizeof(struct fib6_node
),
2171 0, SLAB_HWCACHE_ALIGN
,
2173 if (!fib6_node_kmem
)
2176 ret
= register_pernet_subsys(&fib6_net_ops
);
2178 goto out_kmem_cache_create
;
2180 ret
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETROUTE
, NULL
,
2183 goto out_unregister_subsys
;
2185 __fib6_flush_trees
= fib6_flush_trees
;
2189 out_unregister_subsys
:
2190 unregister_pernet_subsys(&fib6_net_ops
);
2191 out_kmem_cache_create
:
2192 kmem_cache_destroy(fib6_node_kmem
);
2196 void fib6_gc_cleanup(void)
2198 unregister_pernet_subsys(&fib6_net_ops
);
2199 kmem_cache_destroy(fib6_node_kmem
);
2202 #ifdef CONFIG_PROC_FS
2204 struct ipv6_route_iter
{
2205 struct seq_net_private p
;
2206 struct fib6_walker w
;
2208 struct fib6_table
*tbl
;
2212 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
2214 struct rt6_info
*rt
= v
;
2215 struct ipv6_route_iter
*iter
= seq
->private;
2217 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
2219 #ifdef CONFIG_IPV6_SUBTREES
2220 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
2222 seq_puts(seq
, "00000000000000000000000000000000 00 ");
2224 if (rt
->rt6i_flags
& RTF_GATEWAY
)
2225 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
2227 seq_puts(seq
, "00000000000000000000000000000000");
2229 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
2230 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
2231 rt
->dst
.__use
, rt
->rt6i_flags
,
2232 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
2233 iter
->w
.leaf
= NULL
;
2237 static int ipv6_route_yield(struct fib6_walker
*w
)
2239 struct ipv6_route_iter
*iter
= w
->args
;
2245 iter
->w
.leaf
= rcu_dereference_protected(
2246 iter
->w
.leaf
->rt6_next
,
2247 lockdep_is_held(&iter
->tbl
->tb6_lock
));
2249 if (!iter
->skip
&& iter
->w
.leaf
)
2251 } while (iter
->w
.leaf
);
2256 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
,
2259 memset(&iter
->w
, 0, sizeof(iter
->w
));
2260 iter
->w
.func
= ipv6_route_yield
;
2261 iter
->w
.root
= &iter
->tbl
->tb6_root
;
2262 iter
->w
.state
= FWS_INIT
;
2263 iter
->w
.node
= iter
->w
.root
;
2264 iter
->w
.args
= iter
;
2265 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2266 INIT_LIST_HEAD(&iter
->w
.lh
);
2267 fib6_walker_link(net
, &iter
->w
);
2270 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
2274 struct hlist_node
*node
;
2277 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
2278 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
2284 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
2285 node
= rcu_dereference_bh(
2286 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
2288 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
2291 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
2293 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
2294 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2295 iter
->w
.state
= FWS_INIT
;
2296 iter
->w
.node
= iter
->w
.root
;
2297 WARN_ON(iter
->w
.skip
);
2298 iter
->w
.skip
= iter
->w
.count
;
2302 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2306 struct net
*net
= seq_file_net(seq
);
2307 struct ipv6_route_iter
*iter
= seq
->private;
2312 n
= rcu_dereference_bh(((struct rt6_info
*)v
)->rt6_next
);
2319 ipv6_route_check_sernum(iter
);
2320 spin_lock_bh(&iter
->tbl
->tb6_lock
);
2321 r
= fib6_walk_continue(&iter
->w
);
2322 spin_unlock_bh(&iter
->tbl
->tb6_lock
);
2326 return iter
->w
.leaf
;
2328 fib6_walker_unlink(net
, &iter
->w
);
2331 fib6_walker_unlink(net
, &iter
->w
);
2333 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2337 ipv6_route_seq_setup_walk(iter
, net
);
2341 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2344 struct net
*net
= seq_file_net(seq
);
2345 struct ipv6_route_iter
*iter
= seq
->private;
2348 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2352 ipv6_route_seq_setup_walk(iter
, net
);
2353 return ipv6_route_seq_next(seq
, NULL
, pos
);
2359 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2361 struct fib6_walker
*w
= &iter
->w
;
2362 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2365 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2368 struct net
*net
= seq_file_net(seq
);
2369 struct ipv6_route_iter
*iter
= seq
->private;
2371 if (ipv6_route_iter_active(iter
))
2372 fib6_walker_unlink(net
, &iter
->w
);
2374 rcu_read_unlock_bh();
2377 static const struct seq_operations ipv6_route_seq_ops
= {
2378 .start
= ipv6_route_seq_start
,
2379 .next
= ipv6_route_seq_next
,
2380 .stop
= ipv6_route_seq_stop
,
2381 .show
= ipv6_route_seq_show
2384 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2386 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2387 sizeof(struct ipv6_route_iter
));
2390 #endif /* CONFIG_PROC_FS */