1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux INET6 implementation
4 * Forwarding Information Database
7 * Pedro Roque <roque@di.fc.ul.pt>
10 * Yuji SEKIYA @USAGI: Support default route on router node;
11 * remove ip6_null_entry from the top of
13 * Ville Nuorvala: Fixed routing subtrees.
16 #define pr_fmt(fmt) "IPv6: " fmt
18 #include <linux/bpf.h>
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/net.h>
22 #include <linux/route.h>
23 #include <linux/netdevice.h>
24 #include <linux/in6.h>
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/slab.h>
31 #include <net/ndisc.h>
32 #include <net/addrconf.h>
33 #include <net/lwtunnel.h>
34 #include <net/fib_notifier.h>
36 #include <net/ip_fib.h>
37 #include <net/ip6_fib.h>
38 #include <net/ip6_route.h>
40 static struct kmem_cache *fib6_node_kmem __read_mostly;
45 int (*func)(struct fib6_info *, void *arg);
51 #ifdef CONFIG_IPV6_SUBTREES
52 #define FWS_INIT FWS_S
54 #define FWS_INIT FWS_L
57 static struct fib6_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)
94 int new, old = atomic_read(&net->ipv6.fib6_sernum);
97 new = old < INT_MAX ? old + 1 : 1;
98 } while (!atomic_try_cmpxchg(&net->ipv6.fib6_sernum, &old, new));
104 FIB6_NO_SERNUM_CHANGE = 0,
107 void fib6_update_sernum(struct net *net, struct fib6_info *f6i)
109 struct fib6_node *fn;
111 fn = rcu_dereference_protected(f6i->fib6_node,
112 lockdep_is_held(&f6i->fib6_table->tb6_lock));
114 WRITE_ONCE(fn->fn_sernum, fib6_new_sernum(net));
118 * Auxiliary address test functions for the radix tree.
120 * These assume a 32bit processor (although it will work on
127 #if defined(__LITTLE_ENDIAN)
128 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
130 # define BITOP_BE32_SWIZZLE 0
133 static __be32 addr_bit_set(const void *token, int fn_bit)
135 const __be32 *addr = token;
138 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
139 * is optimized version of
140 * htonl(1 << ((~fn_bit)&0x1F))
141 * See include/asm-generic/bitops/le.h.
143 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
147 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh)
149 struct fib6_info *f6i;
150 size_t sz = sizeof(*f6i);
153 sz += sizeof(struct fib6_nh);
155 f6i = kzalloc(sz, gfp_flags);
159 /* fib6_siblings is a union with nh_list, so this initializes both */
160 INIT_LIST_HEAD(&f6i->fib6_siblings);
161 refcount_set(&f6i->fib6_ref, 1);
163 INIT_HLIST_NODE(&f6i->gc_link);
168 void fib6_info_destroy_rcu(struct rcu_head *head)
170 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
172 WARN_ON(f6i->fib6_node);
175 nexthop_put(f6i->nh);
177 fib6_nh_release(f6i->fib6_nh);
179 ip_fib_metrics_put(f6i->fib6_metrics);
182 EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
184 static struct fib6_node *node_alloc(struct net *net)
186 struct fib6_node *fn;
188 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
190 net->ipv6.rt6_stats->fib_nodes++;
195 static void node_free_immediate(struct net *net, struct fib6_node *fn)
197 kmem_cache_free(fib6_node_kmem, fn);
198 net->ipv6.rt6_stats->fib_nodes--;
201 static void node_free_rcu(struct rcu_head *head)
203 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
205 kmem_cache_free(fib6_node_kmem, fn);
208 static void node_free(struct net *net, struct fib6_node *fn)
210 call_rcu(&fn->rcu, node_free_rcu);
211 net->ipv6.rt6_stats->fib_nodes--;
214 static void fib6_free_table(struct fib6_table *table)
216 inetpeer_invalidate_tree(&table->tb6_peers);
220 static void fib6_link_table(struct net *net, struct fib6_table *tb)
225 * Initialize table lock at a single place to give lockdep a key,
226 * tables aren't visible prior to being linked to the list.
228 spin_lock_init(&tb->tb6_lock);
229 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
232 * No protection necessary, this is the only list mutatation
233 * operation, tables never disappear once they exist.
235 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
238 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
240 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
242 struct fib6_table *table;
244 table = kzalloc(sizeof(*table), GFP_ATOMIC);
247 rcu_assign_pointer(table->tb6_root.leaf,
248 net->ipv6.fib6_null_entry);
249 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
250 inet_peer_base_init(&table->tb6_peers);
251 INIT_HLIST_HEAD(&table->tb6_gc_hlist);
257 struct fib6_table *fib6_new_table(struct net *net, u32 id)
259 struct fib6_table *tb;
263 tb = fib6_get_table(net, id);
267 tb = fib6_alloc_table(net, id);
269 fib6_link_table(net, tb);
273 EXPORT_SYMBOL_GPL(fib6_new_table);
275 struct fib6_table *fib6_get_table(struct net *net, u32 id)
277 struct fib6_table *tb;
278 struct hlist_head *head;
283 h = id & (FIB6_TABLE_HASHSZ - 1);
285 head = &net->ipv6.fib_table_hash[h];
286 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
287 if (tb->tb6_id == id) {
296 EXPORT_SYMBOL_GPL(fib6_get_table);
298 static void __net_init fib6_tables_init(struct net *net)
300 fib6_link_table(net, net->ipv6.fib6_main_tbl);
301 fib6_link_table(net, net->ipv6.fib6_local_tbl);
305 struct fib6_table *fib6_new_table(struct net *net, u32 id)
307 return fib6_get_table(net, id);
310 struct fib6_table *fib6_get_table(struct net *net, u32 id)
312 return net->ipv6.fib6_main_tbl;
315 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
316 const struct sk_buff *skb,
317 int flags, pol_lookup_t lookup)
321 rt = pol_lookup_func(lookup,
322 net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
323 if (rt->dst.error == -EAGAIN) {
324 ip6_rt_put_flags(rt, flags);
325 rt = net->ipv6.ip6_null_entry;
326 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
333 /* called with rcu lock held; no reference taken on fib6_info */
334 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
335 struct fib6_result *res, int flags)
337 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6,
341 static void __net_init fib6_tables_init(struct net *net)
343 fib6_link_table(net, net->ipv6.fib6_main_tbl);
348 unsigned int fib6_tables_seq_read(struct net *net)
350 unsigned int h, fib_seq = 0;
353 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
354 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
355 struct fib6_table *tb;
357 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
358 fib_seq += tb->fib_seq;
365 static int call_fib6_entry_notifier(struct notifier_block *nb,
366 enum fib_event_type event_type,
367 struct fib6_info *rt,
368 struct netlink_ext_ack *extack)
370 struct fib6_entry_notifier_info info = {
371 .info.extack = extack,
375 return call_fib6_notifier(nb, event_type, &info.info);
378 static int call_fib6_multipath_entry_notifier(struct notifier_block *nb,
379 enum fib_event_type event_type,
380 struct fib6_info *rt,
381 unsigned int nsiblings,
382 struct netlink_ext_ack *extack)
384 struct fib6_entry_notifier_info info = {
385 .info.extack = extack,
387 .nsiblings = nsiblings,
390 return call_fib6_notifier(nb, event_type, &info.info);
393 int call_fib6_entry_notifiers(struct net *net,
394 enum fib_event_type event_type,
395 struct fib6_info *rt,
396 struct netlink_ext_ack *extack)
398 struct fib6_entry_notifier_info info = {
399 .info.extack = extack,
403 rt->fib6_table->fib_seq++;
404 return call_fib6_notifiers(net, event_type, &info.info);
407 int call_fib6_multipath_entry_notifiers(struct net *net,
408 enum fib_event_type event_type,
409 struct fib6_info *rt,
410 unsigned int nsiblings,
411 struct netlink_ext_ack *extack)
413 struct fib6_entry_notifier_info info = {
414 .info.extack = extack,
416 .nsiblings = nsiblings,
419 rt->fib6_table->fib_seq++;
420 return call_fib6_notifiers(net, event_type, &info.info);
423 int call_fib6_entry_notifiers_replace(struct net *net, struct fib6_info *rt)
425 struct fib6_entry_notifier_info info = {
427 .nsiblings = rt->fib6_nsiblings,
430 rt->fib6_table->fib_seq++;
431 return call_fib6_notifiers(net, FIB_EVENT_ENTRY_REPLACE, &info.info);
434 struct fib6_dump_arg {
436 struct notifier_block *nb;
437 struct netlink_ext_ack *extack;
440 static int fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
442 enum fib_event_type fib_event = FIB_EVENT_ENTRY_REPLACE;
445 if (!rt || rt == arg->net->ipv6.fib6_null_entry)
448 if (rt->fib6_nsiblings)
449 err = call_fib6_multipath_entry_notifier(arg->nb, fib_event,
454 err = call_fib6_entry_notifier(arg->nb, fib_event, rt,
460 static int fib6_node_dump(struct fib6_walker *w)
464 err = fib6_rt_dump(w->leaf, w->args);
469 static int fib6_table_dump(struct net *net, struct fib6_table *tb,
470 struct fib6_walker *w)
474 w->root = &tb->tb6_root;
475 spin_lock_bh(&tb->tb6_lock);
476 err = fib6_walk(net, w);
477 spin_unlock_bh(&tb->tb6_lock);
481 /* Called with rcu_read_lock() */
482 int fib6_tables_dump(struct net *net, struct notifier_block *nb,
483 struct netlink_ext_ack *extack)
485 struct fib6_dump_arg arg;
486 struct fib6_walker *w;
490 w = kzalloc(sizeof(*w), GFP_ATOMIC);
494 w->func = fib6_node_dump;
500 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
501 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
502 struct fib6_table *tb;
504 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
505 err = fib6_table_dump(net, tb, w);
514 /* The tree traversal function should never return a positive value. */
515 return err > 0 ? -EINVAL : err;
518 static int fib6_dump_node(struct fib6_walker *w)
521 struct fib6_info *rt;
523 for_each_fib6_walker_rt(w) {
524 res = rt6_dump_route(rt, w->args, w->skip_in_node);
526 /* Frame is full, suspend walking */
529 /* We'll restart from this node, so if some routes were
530 * already dumped, skip them next time.
532 w->skip_in_node += res;
538 /* Multipath routes are dumped in one route with the
539 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
540 * last sibling of this route (no need to dump the
541 * sibling routes again)
543 if (rt->fib6_nsiblings)
544 rt = list_last_entry(&rt->fib6_siblings,
552 static void fib6_dump_end(struct netlink_callback *cb)
554 struct net *net = sock_net(cb->skb->sk);
555 struct fib6_walker *w = (void *)cb->args[2];
560 fib6_walker_unlink(net, w);
565 cb->done = (void *)cb->args[3];
569 static int fib6_dump_done(struct netlink_callback *cb)
572 return cb->done ? cb->done(cb) : 0;
575 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
576 struct netlink_callback *cb)
578 struct net *net = sock_net(skb->sk);
579 struct fib6_walker *w;
582 w = (void *)cb->args[2];
583 w->root = &table->tb6_root;
585 if (cb->args[4] == 0) {
590 spin_lock_bh(&table->tb6_lock);
591 res = fib6_walk(net, w);
592 spin_unlock_bh(&table->tb6_lock);
595 cb->args[5] = READ_ONCE(w->root->fn_sernum);
598 int sernum = READ_ONCE(w->root->fn_sernum);
599 if (cb->args[5] != sernum) {
600 /* Begin at the root if the tree changed */
601 cb->args[5] = sernum;
609 spin_lock_bh(&table->tb6_lock);
610 res = fib6_walk_continue(w);
611 spin_unlock_bh(&table->tb6_lock);
613 fib6_walker_unlink(net, w);
621 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
623 struct rt6_rtnl_dump_arg arg = {
624 .filter.dump_exceptions = true,
625 .filter.dump_routes = true,
626 .filter.rtnl_held = false,
628 const struct nlmsghdr *nlh = cb->nlh;
629 struct net *net = sock_net(skb->sk);
630 unsigned int e = 0, s_e;
631 struct hlist_head *head;
632 struct fib6_walker *w;
633 struct fib6_table *tb;
638 if (cb->strict_check) {
639 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
642 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
643 struct rtmsg *rtm = nlmsg_data(nlh);
645 if (rtm->rtm_flags & RTM_F_PREFIX)
646 arg.filter.flags = RTM_F_PREFIX;
649 w = (void *)cb->args[2];
653 * 1. allocate and initialize walker.
655 w = kzalloc(sizeof(*w), GFP_ATOMIC);
660 w->func = fib6_dump_node;
661 cb->args[2] = (long)w;
663 /* 2. hook callback destructor.
665 cb->args[3] = (long)cb->done;
666 cb->done = fib6_dump_done;
675 if (arg.filter.table_id) {
676 tb = fib6_get_table(net, arg.filter.table_id);
678 if (rtnl_msg_family(cb->nlh) != PF_INET6)
681 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
687 err = fib6_dump_table(tb, skb, cb);
697 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
699 head = &net->ipv6.fib_table_hash[h];
700 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
703 err = fib6_dump_table(tb, skb, cb);
721 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
726 if (f6i->fib6_metrics == &dst_default_metrics) {
727 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
732 refcount_set(&p->refcnt, 1);
733 f6i->fib6_metrics = p;
736 f6i->fib6_metrics->metrics[metric - 1] = val;
742 * return the appropriate node for a routing tree "add" operation
743 * by either creating and inserting or by returning an existing
747 static struct fib6_node *fib6_add_1(struct net *net,
748 struct fib6_table *table,
749 struct fib6_node *root,
750 struct in6_addr *addr, int plen,
751 int offset, int allow_create,
752 int replace_required,
753 struct netlink_ext_ack *extack)
755 struct fib6_node *fn, *in, *ln;
756 struct fib6_node *pn = NULL;
761 /* insert node in tree */
766 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
767 lockdep_is_held(&table->tb6_lock));
768 key = (struct rt6key *)((u8 *)leaf + offset);
773 if (plen < fn->fn_bit ||
774 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
776 if (replace_required) {
777 NL_SET_ERR_MSG(extack,
778 "Can not replace route - no match found");
779 pr_warn("Can't replace route, no match found\n");
780 return ERR_PTR(-ENOENT);
782 pr_warn("NLM_F_CREATE should be set when creating new route\n");
791 if (plen == fn->fn_bit) {
792 /* clean up an intermediate node */
793 if (!(fn->fn_flags & RTN_RTINFO)) {
794 RCU_INIT_POINTER(fn->leaf, NULL);
795 fib6_info_release(leaf);
796 /* remove null_entry in the root node */
797 } else if (fn->fn_flags & RTN_TL_ROOT &&
798 rcu_access_pointer(fn->leaf) ==
799 net->ipv6.fib6_null_entry) {
800 RCU_INIT_POINTER(fn->leaf, NULL);
807 * We have more bits to go
810 /* Try to walk down on tree. */
811 dir = addr_bit_set(addr, fn->fn_bit);
814 rcu_dereference_protected(fn->right,
815 lockdep_is_held(&table->tb6_lock)) :
816 rcu_dereference_protected(fn->left,
817 lockdep_is_held(&table->tb6_lock));
821 /* We should not create new node because
822 * NLM_F_REPLACE was specified without NLM_F_CREATE
823 * I assume it is safe to require NLM_F_CREATE when
824 * REPLACE flag is used! Later we may want to remove the
825 * check for replace_required, because according
826 * to netlink specification, NLM_F_CREATE
827 * MUST be specified if new route is created.
828 * That would keep IPv6 consistent with IPv4
830 if (replace_required) {
831 NL_SET_ERR_MSG(extack,
832 "Can not replace route - no match found");
833 pr_warn("Can't replace route, no match found\n");
834 return ERR_PTR(-ENOENT);
836 pr_warn("NLM_F_CREATE should be set when creating new route\n");
839 * We walked to the bottom of tree.
840 * Create new leaf node without children.
843 ln = node_alloc(net);
846 return ERR_PTR(-ENOMEM);
848 RCU_INIT_POINTER(ln->parent, pn);
851 rcu_assign_pointer(pn->right, ln);
853 rcu_assign_pointer(pn->left, ln);
860 * split since we don't have a common prefix anymore or
861 * we have a less significant route.
862 * we've to insert an intermediate node on the list
863 * this new node will point to the one we need to create
867 pn = rcu_dereference_protected(fn->parent,
868 lockdep_is_held(&table->tb6_lock));
870 /* find 1st bit in difference between the 2 addrs.
872 See comment in __ipv6_addr_diff: bit may be an invalid value,
873 but if it is >= plen, the value is ignored in any case.
876 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
881 * (new leaf node)[ln] (old node)[fn]
884 in = node_alloc(net);
885 ln = node_alloc(net);
889 node_free_immediate(net, in);
891 node_free_immediate(net, ln);
892 return ERR_PTR(-ENOMEM);
896 * new intermediate node.
898 * be off since that an address that chooses one of
899 * the branches would not match less specific routes
900 * in the other branch
905 RCU_INIT_POINTER(in->parent, pn);
907 fib6_info_hold(rcu_dereference_protected(in->leaf,
908 lockdep_is_held(&table->tb6_lock)));
910 /* update parent pointer */
912 rcu_assign_pointer(pn->right, in);
914 rcu_assign_pointer(pn->left, in);
918 RCU_INIT_POINTER(ln->parent, in);
919 rcu_assign_pointer(fn->parent, in);
921 if (addr_bit_set(addr, bit)) {
922 rcu_assign_pointer(in->right, ln);
923 rcu_assign_pointer(in->left, fn);
925 rcu_assign_pointer(in->left, ln);
926 rcu_assign_pointer(in->right, fn);
928 } else { /* plen <= bit */
931 * (new leaf node)[ln]
933 * (old node)[fn] NULL
936 ln = node_alloc(net);
939 return ERR_PTR(-ENOMEM);
943 RCU_INIT_POINTER(ln->parent, pn);
945 if (addr_bit_set(&key->addr, plen))
946 RCU_INIT_POINTER(ln->right, fn);
948 RCU_INIT_POINTER(ln->left, fn);
950 rcu_assign_pointer(fn->parent, ln);
953 rcu_assign_pointer(pn->right, ln);
955 rcu_assign_pointer(pn->left, ln);
960 static void __fib6_drop_pcpu_from(struct fib6_nh *fib6_nh,
961 const struct fib6_info *match,
962 const struct fib6_table *table)
966 if (!fib6_nh->rt6i_pcpu)
969 /* release the reference to this fib entry from
970 * all of its cached pcpu routes
972 for_each_possible_cpu(cpu) {
973 struct rt6_info **ppcpu_rt;
974 struct rt6_info *pcpu_rt;
976 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
979 /* only dropping the 'from' reference if the cached route
980 * is using 'match'. The cached pcpu_rt->from only changes
981 * from a fib6_info to NULL (ip6_dst_destroy); it can never
982 * change from one fib6_info reference to another
984 if (pcpu_rt && rcu_access_pointer(pcpu_rt->from) == match) {
985 struct fib6_info *from;
987 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
988 fib6_info_release(from);
993 struct fib6_nh_pcpu_arg {
994 struct fib6_info *from;
995 const struct fib6_table *table;
998 static int fib6_nh_drop_pcpu_from(struct fib6_nh *nh, void *_arg)
1000 struct fib6_nh_pcpu_arg *arg = _arg;
1002 __fib6_drop_pcpu_from(nh, arg->from, arg->table);
1006 static void fib6_drop_pcpu_from(struct fib6_info *f6i,
1007 const struct fib6_table *table)
1009 /* Make sure rt6_make_pcpu_route() wont add other percpu routes
1010 * while we are cleaning them here.
1012 f6i->fib6_destroying = 1;
1013 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
1016 struct fib6_nh_pcpu_arg arg = {
1021 nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_drop_pcpu_from,
1024 struct fib6_nh *fib6_nh;
1026 fib6_nh = f6i->fib6_nh;
1027 __fib6_drop_pcpu_from(fib6_nh, f6i, table);
1031 static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
1034 struct fib6_table *table = rt->fib6_table;
1036 /* Flush all cached dst in exception table */
1037 rt6_flush_exceptions(rt);
1038 fib6_drop_pcpu_from(rt, table);
1040 if (rt->nh && !list_empty(&rt->nh_list))
1041 list_del_init(&rt->nh_list);
1043 if (refcount_read(&rt->fib6_ref) != 1) {
1044 /* This route is used as dummy address holder in some split
1045 * nodes. It is not leaked, but it still holds other resources,
1046 * which must be released in time. So, scan ascendant nodes
1047 * and replace dummy references to this route with references
1048 * to still alive ones.
1051 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
1052 lockdep_is_held(&table->tb6_lock));
1053 struct fib6_info *new_leaf;
1054 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
1055 new_leaf = fib6_find_prefix(net, table, fn);
1056 fib6_info_hold(new_leaf);
1058 rcu_assign_pointer(fn->leaf, new_leaf);
1059 fib6_info_release(rt);
1061 fn = rcu_dereference_protected(fn->parent,
1062 lockdep_is_held(&table->tb6_lock));
1066 fib6_clean_expires(rt);
1067 fib6_remove_gc_list(rt);
1071 * Insert routing information in a node.
1074 static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
1075 struct nl_info *info,
1076 struct netlink_ext_ack *extack)
1078 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
1079 lockdep_is_held(&rt->fib6_table->tb6_lock));
1080 struct fib6_info *iter = NULL;
1081 struct fib6_info __rcu **ins;
1082 struct fib6_info __rcu **fallback_ins = NULL;
1083 int replace = (info->nlh &&
1084 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
1085 int add = (!info->nlh ||
1086 (info->nlh->nlmsg_flags & NLM_F_CREATE));
1088 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
1089 bool notify_sibling_rt = false;
1090 u16 nlflags = NLM_F_EXCL;
1093 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
1094 nlflags |= NLM_F_APPEND;
1098 for (iter = leaf; iter;
1099 iter = rcu_dereference_protected(iter->fib6_next,
1100 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
1102 * Search for duplicates
1105 if (iter->fib6_metric == rt->fib6_metric) {
1107 * Same priority level
1110 (info->nlh->nlmsg_flags & NLM_F_EXCL))
1113 nlflags &= ~NLM_F_EXCL;
1115 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
1119 fallback_ins = fallback_ins ?: ins;
1123 if (rt6_duplicate_nexthop(iter, rt)) {
1124 if (rt->fib6_nsiblings)
1125 rt->fib6_nsiblings = 0;
1126 if (!(iter->fib6_flags & RTF_EXPIRES))
1128 if (!(rt->fib6_flags & RTF_EXPIRES)) {
1129 fib6_clean_expires(iter);
1130 fib6_remove_gc_list(iter);
1132 fib6_set_expires(iter, rt->expires);
1133 fib6_add_gc_list(iter);
1137 fib6_metric_set(iter, RTAX_MTU,
1141 /* If we have the same destination and the same metric,
1142 * but not the same gateway, then the route we try to
1143 * add is sibling to this route, increment our counter
1144 * of siblings, and later we will add our route to the
1146 * Only static routes (which don't have flag
1147 * RTF_EXPIRES) are used for ECMPv6.
1149 * To avoid long list, we only had siblings if the
1150 * route have a gateway.
1153 rt6_qualify_for_ecmp(iter))
1154 rt->fib6_nsiblings++;
1157 if (iter->fib6_metric > rt->fib6_metric)
1161 ins = &iter->fib6_next;
1164 if (fallback_ins && !found) {
1165 /* No matching route with same ecmp-able-ness found, replace
1166 * first matching route
1169 iter = rcu_dereference_protected(*ins,
1170 lockdep_is_held(&rt->fib6_table->tb6_lock));
1174 /* Reset round-robin state, if necessary */
1175 if (ins == &fn->leaf)
1178 /* Link this route to others same route. */
1179 if (rt->fib6_nsiblings) {
1180 unsigned int fib6_nsiblings;
1181 struct fib6_info *sibling, *temp_sibling;
1183 /* Find the first route that have the same metric */
1185 notify_sibling_rt = true;
1187 if (sibling->fib6_metric == rt->fib6_metric &&
1188 rt6_qualify_for_ecmp(sibling)) {
1189 list_add_tail(&rt->fib6_siblings,
1190 &sibling->fib6_siblings);
1193 sibling = rcu_dereference_protected(sibling->fib6_next,
1194 lockdep_is_held(&rt->fib6_table->tb6_lock));
1195 notify_sibling_rt = false;
1197 /* For each sibling in the list, increment the counter of
1198 * siblings. BUG() if counters does not match, list of siblings
1202 list_for_each_entry_safe(sibling, temp_sibling,
1203 &rt->fib6_siblings, fib6_siblings) {
1204 sibling->fib6_nsiblings++;
1205 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1208 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1209 rt6_multipath_rebalance(temp_sibling);
1217 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1220 nlflags |= NLM_F_CREATE;
1222 /* The route should only be notified if it is the first
1223 * route in the node or if it is added as a sibling
1224 * route to the first route in the node.
1226 if (!info->skip_notify_kernel &&
1227 (notify_sibling_rt || ins == &fn->leaf)) {
1228 enum fib_event_type fib_event;
1230 if (notify_sibling_rt)
1231 fib_event = FIB_EVENT_ENTRY_APPEND;
1233 fib_event = FIB_EVENT_ENTRY_REPLACE;
1234 err = call_fib6_entry_notifiers(info->nl_net,
1238 struct fib6_info *sibling, *next_sibling;
1240 /* If the route has siblings, then it first
1241 * needs to be unlinked from them.
1243 if (!rt->fib6_nsiblings)
1246 list_for_each_entry_safe(sibling, next_sibling,
1249 sibling->fib6_nsiblings--;
1250 rt->fib6_nsiblings = 0;
1251 list_del_init(&rt->fib6_siblings);
1252 rt6_multipath_rebalance(next_sibling);
1257 rcu_assign_pointer(rt->fib6_next, iter);
1259 rcu_assign_pointer(rt->fib6_node, fn);
1260 rcu_assign_pointer(*ins, rt);
1261 if (!info->skip_notify)
1262 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1263 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1265 if (!(fn->fn_flags & RTN_RTINFO)) {
1266 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1267 fn->fn_flags |= RTN_RTINFO;
1276 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1280 if (!info->skip_notify_kernel && ins == &fn->leaf) {
1281 err = call_fib6_entry_notifiers(info->nl_net,
1282 FIB_EVENT_ENTRY_REPLACE,
1289 rcu_assign_pointer(rt->fib6_node, fn);
1290 rt->fib6_next = iter->fib6_next;
1291 rcu_assign_pointer(*ins, rt);
1292 if (!info->skip_notify)
1293 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1294 if (!(fn->fn_flags & RTN_RTINFO)) {
1295 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1296 fn->fn_flags |= RTN_RTINFO;
1298 nsiblings = iter->fib6_nsiblings;
1299 iter->fib6_node = NULL;
1300 fib6_purge_rt(iter, fn, info->nl_net);
1301 if (rcu_access_pointer(fn->rr_ptr) == iter)
1303 fib6_info_release(iter);
1306 /* Replacing an ECMP route, remove all siblings */
1307 ins = &rt->fib6_next;
1308 iter = rcu_dereference_protected(*ins,
1309 lockdep_is_held(&rt->fib6_table->tb6_lock));
1311 if (iter->fib6_metric > rt->fib6_metric)
1313 if (rt6_qualify_for_ecmp(iter)) {
1314 *ins = iter->fib6_next;
1315 iter->fib6_node = NULL;
1316 fib6_purge_rt(iter, fn, info->nl_net);
1317 if (rcu_access_pointer(fn->rr_ptr) == iter)
1319 fib6_info_release(iter);
1321 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1323 ins = &iter->fib6_next;
1325 iter = rcu_dereference_protected(*ins,
1326 lockdep_is_held(&rt->fib6_table->tb6_lock));
1328 WARN_ON(nsiblings != 0);
1335 static void fib6_start_gc(struct net *net, struct fib6_info *rt)
1337 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1338 (rt->fib6_flags & RTF_EXPIRES))
1339 mod_timer(&net->ipv6.ip6_fib_timer,
1340 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1343 void fib6_force_start_gc(struct net *net)
1345 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1346 mod_timer(&net->ipv6.ip6_fib_timer,
1347 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1350 static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
1353 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1354 lockdep_is_held(&rt->fib6_table->tb6_lock));
1356 /* paired with smp_rmb() in fib6_get_cookie_safe() */
1359 WRITE_ONCE(fn->fn_sernum, sernum);
1360 fn = rcu_dereference_protected(fn->parent,
1361 lockdep_is_held(&rt->fib6_table->tb6_lock));
1365 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
1367 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1370 /* allow ipv4 to update sernum via ipv6_stub */
1371 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i)
1373 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1374 fib6_update_sernum_upto_root(net, f6i);
1375 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1379 * Add routing information to the routing tree.
1380 * <destination addr>/<source addr>
1381 * with source addr info in sub-trees
1382 * Need to own table->tb6_lock
1385 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
1386 struct nl_info *info, struct netlink_ext_ack *extack)
1388 struct fib6_table *table = rt->fib6_table;
1389 struct fib6_node *fn;
1390 #ifdef CONFIG_IPV6_SUBTREES
1391 struct fib6_node *pn = NULL;
1394 int allow_create = 1;
1395 int replace_required = 0;
1398 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1400 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1401 replace_required = 1;
1403 if (!allow_create && !replace_required)
1404 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1406 fn = fib6_add_1(info->nl_net, table, root,
1407 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1408 offsetof(struct fib6_info, fib6_dst), allow_create,
1409 replace_required, extack);
1416 #ifdef CONFIG_IPV6_SUBTREES
1419 if (rt->fib6_src.plen) {
1420 struct fib6_node *sn;
1422 if (!rcu_access_pointer(fn->subtree)) {
1423 struct fib6_node *sfn;
1435 /* Create subtree root node */
1436 sfn = node_alloc(info->nl_net);
1440 fib6_info_hold(info->nl_net->ipv6.fib6_null_entry);
1441 rcu_assign_pointer(sfn->leaf,
1442 info->nl_net->ipv6.fib6_null_entry);
1443 sfn->fn_flags = RTN_ROOT;
1445 /* Now add the first leaf node to new subtree */
1447 sn = fib6_add_1(info->nl_net, table, sfn,
1448 &rt->fib6_src.addr, rt->fib6_src.plen,
1449 offsetof(struct fib6_info, fib6_src),
1450 allow_create, replace_required, extack);
1453 /* If it is failed, discard just allocated
1454 root, and then (in failure) stale node
1457 node_free_immediate(info->nl_net, sfn);
1462 /* Now link new subtree to main tree */
1463 rcu_assign_pointer(sfn->parent, fn);
1464 rcu_assign_pointer(fn->subtree, sfn);
1466 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1467 &rt->fib6_src.addr, rt->fib6_src.plen,
1468 offsetof(struct fib6_info, fib6_src),
1469 allow_create, replace_required, extack);
1477 if (!rcu_access_pointer(fn->leaf)) {
1478 if (fn->fn_flags & RTN_TL_ROOT) {
1479 /* put back null_entry for root node */
1480 rcu_assign_pointer(fn->leaf,
1481 info->nl_net->ipv6.fib6_null_entry);
1484 rcu_assign_pointer(fn->leaf, rt);
1491 err = fib6_add_rt2node(fn, rt, info, extack);
1494 list_add(&rt->nh_list, &rt->nh->f6i_list);
1495 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(info->nl_net));
1497 if (rt->fib6_flags & RTF_EXPIRES)
1498 fib6_add_gc_list(rt);
1500 fib6_start_gc(info->nl_net, rt);
1505 #ifdef CONFIG_IPV6_SUBTREES
1507 * If fib6_add_1 has cleared the old leaf pointer in the
1508 * super-tree leaf node we have to find a new one for it.
1511 struct fib6_info *pn_leaf =
1512 rcu_dereference_protected(pn->leaf,
1513 lockdep_is_held(&table->tb6_lock));
1514 if (pn_leaf == rt) {
1516 RCU_INIT_POINTER(pn->leaf, NULL);
1517 fib6_info_release(rt);
1519 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1520 pn_leaf = fib6_find_prefix(info->nl_net, table,
1524 info->nl_net->ipv6.fib6_null_entry;
1525 fib6_info_hold(pn_leaf);
1526 rcu_assign_pointer(pn->leaf, pn_leaf);
1531 } else if (fib6_requires_src(rt)) {
1532 fib6_routes_require_src_inc(info->nl_net);
1537 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1538 * 1. fn is an intermediate node and we failed to add the new
1539 * route to it in both subtree creation failure and fib6_add_rt2node()
1541 * 2. fn is the root node in the table and we fail to add the first
1542 * default route to it.
1545 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1546 (fn->fn_flags & RTN_TL_ROOT &&
1547 !rcu_access_pointer(fn->leaf))))
1548 fib6_repair_tree(info->nl_net, table, fn);
1553 * Routing tree lookup
1557 struct lookup_args {
1558 int offset; /* key offset on fib6_info */
1559 const struct in6_addr *addr; /* search key */
1562 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1563 struct lookup_args *args)
1565 struct fib6_node *fn;
1568 if (unlikely(args->offset == 0))
1578 struct fib6_node *next;
1580 dir = addr_bit_set(args->addr, fn->fn_bit);
1582 next = dir ? rcu_dereference(fn->right) :
1583 rcu_dereference(fn->left);
1593 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1595 if (subtree || fn->fn_flags & RTN_RTINFO) {
1596 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1602 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1604 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1605 #ifdef CONFIG_IPV6_SUBTREES
1607 struct fib6_node *sfn;
1608 sfn = fib6_node_lookup_1(subtree,
1615 if (fn->fn_flags & RTN_RTINFO)
1620 if (fn->fn_flags & RTN_ROOT)
1623 fn = rcu_dereference(fn->parent);
1629 /* called with rcu_read_lock() held
1631 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1632 const struct in6_addr *daddr,
1633 const struct in6_addr *saddr)
1635 struct fib6_node *fn;
1636 struct lookup_args args[] = {
1638 .offset = offsetof(struct fib6_info, fib6_dst),
1641 #ifdef CONFIG_IPV6_SUBTREES
1643 .offset = offsetof(struct fib6_info, fib6_src),
1648 .offset = 0, /* sentinel */
1652 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
1653 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1660 * Get node with specified destination prefix (and source prefix,
1661 * if subtrees are used)
1662 * exact_match == true means we try to find fn with exact match of
1663 * the passed in prefix addr
1664 * exact_match == false means we try to find fn with longest prefix
1665 * match of the passed in prefix addr. This is useful for finding fn
1666 * for cached route as it will be stored in the exception table under
1667 * the node with longest prefix length.
1671 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1672 const struct in6_addr *addr,
1673 int plen, int offset,
1676 struct fib6_node *fn, *prev = NULL;
1678 for (fn = root; fn ; ) {
1679 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1682 /* This node is being deleted */
1684 if (plen <= fn->fn_bit)
1690 key = (struct rt6key *)((u8 *)leaf + offset);
1695 if (plen < fn->fn_bit ||
1696 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1699 if (plen == fn->fn_bit)
1702 if (fn->fn_flags & RTN_RTINFO)
1707 * We have more bits to go
1709 if (addr_bit_set(addr, fn->fn_bit))
1710 fn = rcu_dereference(fn->right);
1712 fn = rcu_dereference(fn->left);
1721 struct fib6_node *fib6_locate(struct fib6_node *root,
1722 const struct in6_addr *daddr, int dst_len,
1723 const struct in6_addr *saddr, int src_len,
1726 struct fib6_node *fn;
1728 fn = fib6_locate_1(root, daddr, dst_len,
1729 offsetof(struct fib6_info, fib6_dst),
1732 #ifdef CONFIG_IPV6_SUBTREES
1734 WARN_ON(saddr == NULL);
1736 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1739 fn = fib6_locate_1(subtree, saddr, src_len,
1740 offsetof(struct fib6_info, fib6_src),
1747 if (fn && fn->fn_flags & RTN_RTINFO)
1759 static struct fib6_info *fib6_find_prefix(struct net *net,
1760 struct fib6_table *table,
1761 struct fib6_node *fn)
1763 struct fib6_node *child_left, *child_right;
1765 if (fn->fn_flags & RTN_ROOT)
1766 return net->ipv6.fib6_null_entry;
1769 child_left = rcu_dereference_protected(fn->left,
1770 lockdep_is_held(&table->tb6_lock));
1771 child_right = rcu_dereference_protected(fn->right,
1772 lockdep_is_held(&table->tb6_lock));
1774 return rcu_dereference_protected(child_left->leaf,
1775 lockdep_is_held(&table->tb6_lock));
1777 return rcu_dereference_protected(child_right->leaf,
1778 lockdep_is_held(&table->tb6_lock));
1780 fn = FIB6_SUBTREE(fn);
1786 * Called to trim the tree of intermediate nodes when possible. "fn"
1787 * is the node we want to try and remove.
1788 * Need to own table->tb6_lock
1791 static struct fib6_node *fib6_repair_tree(struct net *net,
1792 struct fib6_table *table,
1793 struct fib6_node *fn)
1797 struct fib6_node *child;
1798 struct fib6_walker *w;
1801 /* Set fn->leaf to null_entry for root node. */
1802 if (fn->fn_flags & RTN_TL_ROOT) {
1803 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1808 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1809 lockdep_is_held(&table->tb6_lock));
1810 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1811 lockdep_is_held(&table->tb6_lock));
1812 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1813 lockdep_is_held(&table->tb6_lock));
1814 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1815 lockdep_is_held(&table->tb6_lock));
1816 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1817 lockdep_is_held(&table->tb6_lock));
1818 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1819 lockdep_is_held(&table->tb6_lock));
1820 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1821 lockdep_is_held(&table->tb6_lock));
1822 struct fib6_info *new_fn_leaf;
1824 pr_debug("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1827 WARN_ON(fn->fn_flags & RTN_RTINFO);
1828 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1842 if (children == 3 || FIB6_SUBTREE(fn)
1843 #ifdef CONFIG_IPV6_SUBTREES
1844 /* Subtree root (i.e. fn) may have one child */
1845 || (children && fn->fn_flags & RTN_ROOT)
1848 new_fn_leaf = fib6_find_prefix(net, table, fn);
1851 WARN_ON(!new_fn_leaf);
1852 new_fn_leaf = net->ipv6.fib6_null_entry;
1855 fib6_info_hold(new_fn_leaf);
1856 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1860 #ifdef CONFIG_IPV6_SUBTREES
1861 if (FIB6_SUBTREE(pn) == fn) {
1862 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1863 RCU_INIT_POINTER(pn->subtree, NULL);
1866 WARN_ON(fn->fn_flags & RTN_ROOT);
1869 rcu_assign_pointer(pn->right, child);
1870 else if (pn_l == fn)
1871 rcu_assign_pointer(pn->left, child);
1877 rcu_assign_pointer(child->parent, pn);
1879 #ifdef CONFIG_IPV6_SUBTREES
1883 read_lock(&net->ipv6.fib6_walker_lock);
1884 FOR_WALKERS(net, w) {
1886 if (w->node == fn) {
1887 pr_debug("W %p adjusted by delnode 1, s=%d/%d\n",
1888 w, w->state, nstate);
1893 if (w->node == fn) {
1896 pr_debug("W %p adjusted by delnode 2, s=%d\n",
1898 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1900 pr_debug("W %p adjusted by delnode 2, s=%d\n",
1902 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1907 read_unlock(&net->ipv6.fib6_walker_lock);
1910 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1913 RCU_INIT_POINTER(pn->leaf, NULL);
1914 fib6_info_release(pn_leaf);
1919 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1920 struct fib6_info __rcu **rtp, struct nl_info *info)
1922 struct fib6_info *leaf, *replace_rt = NULL;
1923 struct fib6_walker *w;
1924 struct fib6_info *rt = rcu_dereference_protected(*rtp,
1925 lockdep_is_held(&table->tb6_lock));
1926 struct net *net = info->nl_net;
1927 bool notify_del = false;
1929 /* If the deleted route is the first in the node and it is not part of
1930 * a multipath route, then we need to replace it with the next route
1931 * in the node, if exists.
1933 leaf = rcu_dereference_protected(fn->leaf,
1934 lockdep_is_held(&table->tb6_lock));
1935 if (leaf == rt && !rt->fib6_nsiblings) {
1936 if (rcu_access_pointer(rt->fib6_next))
1937 replace_rt = rcu_dereference_protected(rt->fib6_next,
1938 lockdep_is_held(&table->tb6_lock));
1944 *rtp = rt->fib6_next;
1945 rt->fib6_node = NULL;
1946 net->ipv6.rt6_stats->fib_rt_entries--;
1947 net->ipv6.rt6_stats->fib_discarded_routes++;
1949 /* Reset round-robin state, if necessary */
1950 if (rcu_access_pointer(fn->rr_ptr) == rt)
1953 /* Remove this entry from other siblings */
1954 if (rt->fib6_nsiblings) {
1955 struct fib6_info *sibling, *next_sibling;
1957 /* The route is deleted from a multipath route. If this
1958 * multipath route is the first route in the node, then we need
1959 * to emit a delete notification. Otherwise, we need to skip
1962 if (rt->fib6_metric == leaf->fib6_metric &&
1963 rt6_qualify_for_ecmp(leaf))
1965 list_for_each_entry_safe(sibling, next_sibling,
1966 &rt->fib6_siblings, fib6_siblings)
1967 sibling->fib6_nsiblings--;
1968 rt->fib6_nsiblings = 0;
1969 list_del_init(&rt->fib6_siblings);
1970 rt6_multipath_rebalance(next_sibling);
1973 /* Adjust walkers */
1974 read_lock(&net->ipv6.fib6_walker_lock);
1975 FOR_WALKERS(net, w) {
1976 if (w->state == FWS_C && w->leaf == rt) {
1977 pr_debug("walker %p adjusted by delroute\n", w);
1978 w->leaf = rcu_dereference_protected(rt->fib6_next,
1979 lockdep_is_held(&table->tb6_lock));
1984 read_unlock(&net->ipv6.fib6_walker_lock);
1986 /* If it was last route, call fib6_repair_tree() to:
1987 * 1. For root node, put back null_entry as how the table was created.
1988 * 2. For other nodes, expunge its radix tree node.
1990 if (!rcu_access_pointer(fn->leaf)) {
1991 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1992 fn->fn_flags &= ~RTN_RTINFO;
1993 net->ipv6.rt6_stats->fib_route_nodes--;
1995 fn = fib6_repair_tree(net, table, fn);
1998 fib6_purge_rt(rt, fn, net);
2000 if (!info->skip_notify_kernel) {
2002 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL,
2004 else if (replace_rt)
2005 call_fib6_entry_notifiers_replace(net, replace_rt);
2007 if (!info->skip_notify)
2008 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
2010 fib6_info_release(rt);
2013 /* Need to own table->tb6_lock */
2014 int fib6_del(struct fib6_info *rt, struct nl_info *info)
2016 struct net *net = info->nl_net;
2017 struct fib6_info __rcu **rtp;
2018 struct fib6_info __rcu **rtp_next;
2019 struct fib6_table *table;
2020 struct fib6_node *fn;
2022 if (rt == net->ipv6.fib6_null_entry)
2025 table = rt->fib6_table;
2026 fn = rcu_dereference_protected(rt->fib6_node,
2027 lockdep_is_held(&table->tb6_lock));
2031 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
2034 * Walk the leaf entries looking for ourself
2037 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
2038 struct fib6_info *cur = rcu_dereference_protected(*rtp,
2039 lockdep_is_held(&table->tb6_lock));
2041 if (fib6_requires_src(cur))
2042 fib6_routes_require_src_dec(info->nl_net);
2043 fib6_del_route(table, fn, rtp, info);
2046 rtp_next = &cur->fib6_next;
2052 * Tree traversal function.
2054 * Certainly, it is not interrupt safe.
2055 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
2056 * It means, that we can modify tree during walking
2057 * and use this function for garbage collection, clone pruning,
2058 * cleaning tree when a device goes down etc. etc.
2060 * It guarantees that every node will be traversed,
2061 * and that it will be traversed only once.
2063 * Callback function w->func may return:
2064 * 0 -> continue walking.
2065 * positive value -> walking is suspended (used by tree dumps,
2066 * and probably by gc, if it will be split to several slices)
2067 * negative value -> terminate walking.
2069 * The function itself returns:
2070 * 0 -> walk is complete.
2071 * >0 -> walk is incomplete (i.e. suspended)
2072 * <0 -> walk is terminated by an error.
2074 * This function is called with tb6_lock held.
2077 static int fib6_walk_continue(struct fib6_walker *w)
2079 struct fib6_node *fn, *pn, *left, *right;
2081 /* w->root should always be table->tb6_root */
2082 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
2090 #ifdef CONFIG_IPV6_SUBTREES
2092 if (FIB6_SUBTREE(fn)) {
2093 w->node = FIB6_SUBTREE(fn);
2100 left = rcu_dereference_protected(fn->left, 1);
2103 w->state = FWS_INIT;
2109 right = rcu_dereference_protected(fn->right, 1);
2112 w->state = FWS_INIT;
2116 w->leaf = rcu_dereference_protected(fn->leaf, 1);
2119 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
2140 pn = rcu_dereference_protected(fn->parent, 1);
2141 left = rcu_dereference_protected(pn->left, 1);
2142 right = rcu_dereference_protected(pn->right, 1);
2144 #ifdef CONFIG_IPV6_SUBTREES
2145 if (FIB6_SUBTREE(pn) == fn) {
2146 WARN_ON(!(fn->fn_flags & RTN_ROOT));
2157 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
2167 static int fib6_walk(struct net *net, struct fib6_walker *w)
2171 w->state = FWS_INIT;
2174 fib6_walker_link(net, w);
2175 res = fib6_walk_continue(w);
2177 fib6_walker_unlink(net, w);
2181 static int fib6_clean_node(struct fib6_walker *w)
2184 struct fib6_info *rt;
2185 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
2186 struct nl_info info = {
2188 .skip_notify = c->skip_notify,
2191 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
2192 READ_ONCE(w->node->fn_sernum) != c->sernum)
2193 WRITE_ONCE(w->node->fn_sernum, c->sernum);
2196 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
2201 for_each_fib6_walker_rt(w) {
2202 res = c->func(rt, c->arg);
2205 res = fib6_del(rt, &info);
2208 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
2210 rcu_access_pointer(rt->fib6_node),
2216 } else if (res == -2) {
2217 if (WARN_ON(!rt->fib6_nsiblings))
2219 rt = list_last_entry(&rt->fib6_siblings,
2220 struct fib6_info, fib6_siblings);
2230 * Convenient frontend to tree walker.
2232 * func is called on each route.
2233 * It may return -2 -> skip multipath route.
2234 * -1 -> delete this route.
2235 * 0 -> continue walking
2238 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2239 int (*func)(struct fib6_info *, void *arg),
2240 int sernum, void *arg, bool skip_notify)
2242 struct fib6_cleaner c;
2245 c.w.func = fib6_clean_node;
2248 c.w.skip_in_node = 0;
2253 c.skip_notify = skip_notify;
2255 fib6_walk(net, &c.w);
2258 static void __fib6_clean_all(struct net *net,
2259 int (*func)(struct fib6_info *, void *),
2260 int sernum, void *arg, bool skip_notify)
2262 struct fib6_table *table;
2263 struct hlist_head *head;
2267 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2268 head = &net->ipv6.fib_table_hash[h];
2269 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2270 spin_lock_bh(&table->tb6_lock);
2271 fib6_clean_tree(net, &table->tb6_root,
2272 func, sernum, arg, skip_notify);
2273 spin_unlock_bh(&table->tb6_lock);
2279 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
2282 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2285 void fib6_clean_all_skip_notify(struct net *net,
2286 int (*func)(struct fib6_info *, void *),
2289 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
2292 static void fib6_flush_trees(struct net *net)
2294 int new_sernum = fib6_new_sernum(net);
2296 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
2300 * Garbage collection
2303 static int fib6_age(struct fib6_info *rt, struct fib6_gc_args *gc_args)
2305 unsigned long now = jiffies;
2308 * check addrconf expiration here.
2309 * Routes are expired even if they are in use.
2312 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
2313 if (time_after(now, rt->expires)) {
2314 pr_debug("expiring %p\n", rt);
2320 /* Also age clones in the exception table.
2321 * Note, that clones are aged out
2322 * only if they are not in use now.
2324 rt6_age_exceptions(rt, gc_args, now);
2329 static void fib6_gc_table(struct net *net,
2330 struct fib6_table *tb6,
2331 struct fib6_gc_args *gc_args)
2333 struct fib6_info *rt;
2334 struct hlist_node *n;
2335 struct nl_info info = {
2337 .skip_notify = false,
2340 hlist_for_each_entry_safe(rt, n, &tb6->tb6_gc_hlist, gc_link)
2341 if (fib6_age(rt, gc_args) == -1)
2342 fib6_del(rt, &info);
2345 static void fib6_gc_all(struct net *net, struct fib6_gc_args *gc_args)
2347 struct fib6_table *table;
2348 struct hlist_head *head;
2352 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2353 head = &net->ipv6.fib_table_hash[h];
2354 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2355 spin_lock_bh(&table->tb6_lock);
2357 fib6_gc_table(net, table, gc_args);
2359 spin_unlock_bh(&table->tb6_lock);
2365 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2367 struct fib6_gc_args gc_args;
2371 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2372 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2373 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2376 gc_args.timeout = expires ? (int)expires :
2377 net->ipv6.sysctl.ip6_rt_gc_interval;
2380 fib6_gc_all(net, &gc_args);
2382 net->ipv6.ip6_rt_last_gc = now;
2385 mod_timer(&net->ipv6.ip6_fib_timer,
2387 + net->ipv6.sysctl.ip6_rt_gc_interval));
2389 del_timer(&net->ipv6.ip6_fib_timer);
2390 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2393 static void fib6_gc_timer_cb(struct timer_list *t)
2395 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2397 fib6_run_gc(0, arg, true);
2400 static int __net_init fib6_net_init(struct net *net)
2402 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2405 err = fib6_notifier_init(net);
2409 /* Default to 3-tuple */
2410 net->ipv6.sysctl.multipath_hash_fields =
2411 FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK;
2413 spin_lock_init(&net->ipv6.fib6_gc_lock);
2414 rwlock_init(&net->ipv6.fib6_walker_lock);
2415 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2416 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2418 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2419 if (!net->ipv6.rt6_stats)
2422 /* Avoid false sharing : Use at least a full cache line */
2423 size = max_t(size_t, size, L1_CACHE_BYTES);
2425 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2426 if (!net->ipv6.fib_table_hash)
2429 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2431 if (!net->ipv6.fib6_main_tbl)
2432 goto out_fib_table_hash;
2434 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2435 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2436 net->ipv6.fib6_null_entry);
2437 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2438 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2439 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2440 INIT_HLIST_HEAD(&net->ipv6.fib6_main_tbl->tb6_gc_hlist);
2442 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2443 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2445 if (!net->ipv6.fib6_local_tbl)
2446 goto out_fib6_main_tbl;
2447 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2448 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2449 net->ipv6.fib6_null_entry);
2450 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2451 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2452 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2453 INIT_HLIST_HEAD(&net->ipv6.fib6_local_tbl->tb6_gc_hlist);
2455 fib6_tables_init(net);
2459 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2461 kfree(net->ipv6.fib6_main_tbl);
2464 kfree(net->ipv6.fib_table_hash);
2466 kfree(net->ipv6.rt6_stats);
2468 fib6_notifier_exit(net);
2472 static void fib6_net_exit(struct net *net)
2476 del_timer_sync(&net->ipv6.ip6_fib_timer);
2478 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2479 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2480 struct hlist_node *tmp;
2481 struct fib6_table *tb;
2483 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2484 hlist_del(&tb->tb6_hlist);
2485 fib6_free_table(tb);
2489 kfree(net->ipv6.fib_table_hash);
2490 kfree(net->ipv6.rt6_stats);
2491 fib6_notifier_exit(net);
2494 static struct pernet_operations fib6_net_ops = {
2495 .init = fib6_net_init,
2496 .exit = fib6_net_exit,
2499 int __init fib6_init(void)
2503 fib6_node_kmem = KMEM_CACHE(fib6_node,
2504 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT);
2505 if (!fib6_node_kmem)
2508 ret = register_pernet_subsys(&fib6_net_ops);
2510 goto out_kmem_cache_create;
2512 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2513 inet6_dump_fib, RTNL_FLAG_DUMP_UNLOCKED);
2515 goto out_unregister_subsys;
2517 __fib6_flush_trees = fib6_flush_trees;
2521 out_unregister_subsys:
2522 unregister_pernet_subsys(&fib6_net_ops);
2523 out_kmem_cache_create:
2524 kmem_cache_destroy(fib6_node_kmem);
2528 void fib6_gc_cleanup(void)
2530 unregister_pernet_subsys(&fib6_net_ops);
2531 kmem_cache_destroy(fib6_node_kmem);
2534 #ifdef CONFIG_PROC_FS
2535 static int ipv6_route_native_seq_show(struct seq_file *seq, void *v)
2537 struct fib6_info *rt = v;
2538 struct ipv6_route_iter *iter = seq->private;
2539 struct fib6_nh *fib6_nh = rt->fib6_nh;
2540 unsigned int flags = rt->fib6_flags;
2541 const struct net_device *dev;
2544 fib6_nh = nexthop_fib6_nh(rt->nh);
2546 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
2548 #ifdef CONFIG_IPV6_SUBTREES
2549 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
2551 seq_puts(seq, "00000000000000000000000000000000 00 ");
2553 if (fib6_nh->fib_nh_gw_family) {
2554 flags |= RTF_GATEWAY;
2555 seq_printf(seq, "%pi6", &fib6_nh->fib_nh_gw6);
2557 seq_puts(seq, "00000000000000000000000000000000");
2560 dev = fib6_nh->fib_nh_dev;
2561 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2562 rt->fib6_metric, refcount_read(&rt->fib6_ref), 0,
2563 flags, dev ? dev->name : "");
2564 iter->w.leaf = NULL;
2568 static int ipv6_route_yield(struct fib6_walker *w)
2570 struct ipv6_route_iter *iter = w->args;
2576 iter->w.leaf = rcu_dereference_protected(
2577 iter->w.leaf->fib6_next,
2578 lockdep_is_held(&iter->tbl->tb6_lock));
2580 if (!iter->skip && iter->w.leaf)
2582 } while (iter->w.leaf);
2587 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2590 memset(&iter->w, 0, sizeof(iter->w));
2591 iter->w.func = ipv6_route_yield;
2592 iter->w.root = &iter->tbl->tb6_root;
2593 iter->w.state = FWS_INIT;
2594 iter->w.node = iter->w.root;
2595 iter->w.args = iter;
2596 iter->sernum = READ_ONCE(iter->w.root->fn_sernum);
2597 INIT_LIST_HEAD(&iter->w.lh);
2598 fib6_walker_link(net, &iter->w);
2601 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2605 struct hlist_node *node;
2608 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2609 node = rcu_dereference(hlist_next_rcu(&tbl->tb6_hlist));
2615 while (!node && h < FIB6_TABLE_HASHSZ) {
2616 node = rcu_dereference(
2617 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2619 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2622 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2624 int sernum = READ_ONCE(iter->w.root->fn_sernum);
2626 if (iter->sernum != sernum) {
2627 iter->sernum = sernum;
2628 iter->w.state = FWS_INIT;
2629 iter->w.node = iter->w.root;
2630 WARN_ON(iter->w.skip);
2631 iter->w.skip = iter->w.count;
2635 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2638 struct fib6_info *n;
2639 struct net *net = seq_file_net(seq);
2640 struct ipv6_route_iter *iter = seq->private;
2646 n = rcu_dereference(((struct fib6_info *)v)->fib6_next);
2651 ipv6_route_check_sernum(iter);
2652 spin_lock_bh(&iter->tbl->tb6_lock);
2653 r = fib6_walk_continue(&iter->w);
2654 spin_unlock_bh(&iter->tbl->tb6_lock);
2656 return iter->w.leaf;
2658 fib6_walker_unlink(net, &iter->w);
2661 fib6_walker_unlink(net, &iter->w);
2663 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2667 ipv6_route_seq_setup_walk(iter, net);
2671 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2674 struct net *net = seq_file_net(seq);
2675 struct ipv6_route_iter *iter = seq->private;
2678 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2684 ipv6_route_seq_setup_walk(iter, net);
2685 return ipv6_route_seq_next(seq, NULL, &p);
2691 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2693 struct fib6_walker *w = &iter->w;
2694 return w->node && !(w->state == FWS_U && w->node == w->root);
2697 static void ipv6_route_native_seq_stop(struct seq_file *seq, void *v)
2700 struct net *net = seq_file_net(seq);
2701 struct ipv6_route_iter *iter = seq->private;
2703 if (ipv6_route_iter_active(iter))
2704 fib6_walker_unlink(net, &iter->w);
2709 #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
2710 static int ipv6_route_prog_seq_show(struct bpf_prog *prog,
2711 struct bpf_iter_meta *meta,
2714 struct bpf_iter__ipv6_route ctx;
2718 return bpf_iter_run_prog(prog, &ctx);
2721 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2723 struct ipv6_route_iter *iter = seq->private;
2724 struct bpf_iter_meta meta;
2725 struct bpf_prog *prog;
2729 prog = bpf_iter_get_info(&meta, false);
2731 return ipv6_route_native_seq_show(seq, v);
2733 ret = ipv6_route_prog_seq_show(prog, &meta, v);
2734 iter->w.leaf = NULL;
2739 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2741 struct bpf_iter_meta meta;
2742 struct bpf_prog *prog;
2746 prog = bpf_iter_get_info(&meta, true);
2748 (void)ipv6_route_prog_seq_show(prog, &meta, v);
2751 ipv6_route_native_seq_stop(seq, v);
2754 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2756 return ipv6_route_native_seq_show(seq, v);
2759 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2761 ipv6_route_native_seq_stop(seq, v);
2765 const struct seq_operations ipv6_route_seq_ops = {
2766 .start = ipv6_route_seq_start,
2767 .next = ipv6_route_seq_next,
2768 .stop = ipv6_route_seq_stop,
2769 .show = ipv6_route_seq_show
2771 #endif /* CONFIG_PROC_FS */