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 = { .filter.dump_exceptions = true,
624 .filter.dump_routes = true };
625 const struct nlmsghdr *nlh = cb->nlh;
626 struct net *net = sock_net(skb->sk);
628 unsigned int e = 0, s_e;
629 struct fib6_walker *w;
630 struct fib6_table *tb;
631 struct hlist_head *head;
634 if (cb->strict_check) {
637 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
640 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
641 struct rtmsg *rtm = nlmsg_data(nlh);
643 if (rtm->rtm_flags & RTM_F_PREFIX)
644 arg.filter.flags = RTM_F_PREFIX;
647 w = (void *)cb->args[2];
651 * 1. hook callback destructor.
653 cb->args[3] = (long)cb->done;
654 cb->done = fib6_dump_done;
657 * 2. allocate and initialize walker.
659 w = kzalloc(sizeof(*w), GFP_ATOMIC);
662 w->func = fib6_dump_node;
663 cb->args[2] = (long)w;
671 if (arg.filter.table_id) {
672 tb = fib6_get_table(net, arg.filter.table_id);
674 if (rtnl_msg_family(cb->nlh) != PF_INET6)
677 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
682 res = fib6_dump_table(tb, skb, cb);
693 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
695 head = &net->ipv6.fib_table_hash[h];
696 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
699 res = fib6_dump_table(tb, skb, cb);
711 res = res < 0 ? res : skb->len;
717 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
722 if (f6i->fib6_metrics == &dst_default_metrics) {
723 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
728 refcount_set(&p->refcnt, 1);
729 f6i->fib6_metrics = p;
732 f6i->fib6_metrics->metrics[metric - 1] = val;
738 * return the appropriate node for a routing tree "add" operation
739 * by either creating and inserting or by returning an existing
743 static struct fib6_node *fib6_add_1(struct net *net,
744 struct fib6_table *table,
745 struct fib6_node *root,
746 struct in6_addr *addr, int plen,
747 int offset, int allow_create,
748 int replace_required,
749 struct netlink_ext_ack *extack)
751 struct fib6_node *fn, *in, *ln;
752 struct fib6_node *pn = NULL;
757 RT6_TRACE("fib6_add_1\n");
759 /* insert node in tree */
764 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
765 lockdep_is_held(&table->tb6_lock));
766 key = (struct rt6key *)((u8 *)leaf + offset);
771 if (plen < fn->fn_bit ||
772 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
774 if (replace_required) {
775 NL_SET_ERR_MSG(extack,
776 "Can not replace route - no match found");
777 pr_warn("Can't replace route, no match found\n");
778 return ERR_PTR(-ENOENT);
780 pr_warn("NLM_F_CREATE should be set when creating new route\n");
789 if (plen == fn->fn_bit) {
790 /* clean up an intermediate node */
791 if (!(fn->fn_flags & RTN_RTINFO)) {
792 RCU_INIT_POINTER(fn->leaf, NULL);
793 fib6_info_release(leaf);
794 /* remove null_entry in the root node */
795 } else if (fn->fn_flags & RTN_TL_ROOT &&
796 rcu_access_pointer(fn->leaf) ==
797 net->ipv6.fib6_null_entry) {
798 RCU_INIT_POINTER(fn->leaf, NULL);
805 * We have more bits to go
808 /* Try to walk down on tree. */
809 dir = addr_bit_set(addr, fn->fn_bit);
812 rcu_dereference_protected(fn->right,
813 lockdep_is_held(&table->tb6_lock)) :
814 rcu_dereference_protected(fn->left,
815 lockdep_is_held(&table->tb6_lock));
819 /* We should not create new node because
820 * NLM_F_REPLACE was specified without NLM_F_CREATE
821 * I assume it is safe to require NLM_F_CREATE when
822 * REPLACE flag is used! Later we may want to remove the
823 * check for replace_required, because according
824 * to netlink specification, NLM_F_CREATE
825 * MUST be specified if new route is created.
826 * That would keep IPv6 consistent with IPv4
828 if (replace_required) {
829 NL_SET_ERR_MSG(extack,
830 "Can not replace route - no match found");
831 pr_warn("Can't replace route, no match found\n");
832 return ERR_PTR(-ENOENT);
834 pr_warn("NLM_F_CREATE should be set when creating new route\n");
837 * We walked to the bottom of tree.
838 * Create new leaf node without children.
841 ln = node_alloc(net);
844 return ERR_PTR(-ENOMEM);
846 RCU_INIT_POINTER(ln->parent, pn);
849 rcu_assign_pointer(pn->right, ln);
851 rcu_assign_pointer(pn->left, ln);
858 * split since we don't have a common prefix anymore or
859 * we have a less significant route.
860 * we've to insert an intermediate node on the list
861 * this new node will point to the one we need to create
865 pn = rcu_dereference_protected(fn->parent,
866 lockdep_is_held(&table->tb6_lock));
868 /* find 1st bit in difference between the 2 addrs.
870 See comment in __ipv6_addr_diff: bit may be an invalid value,
871 but if it is >= plen, the value is ignored in any case.
874 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
879 * (new leaf node)[ln] (old node)[fn]
882 in = node_alloc(net);
883 ln = node_alloc(net);
887 node_free_immediate(net, in);
889 node_free_immediate(net, ln);
890 return ERR_PTR(-ENOMEM);
894 * new intermediate node.
896 * be off since that an address that chooses one of
897 * the branches would not match less specific routes
898 * in the other branch
903 RCU_INIT_POINTER(in->parent, pn);
905 fib6_info_hold(rcu_dereference_protected(in->leaf,
906 lockdep_is_held(&table->tb6_lock)));
908 /* update parent pointer */
910 rcu_assign_pointer(pn->right, in);
912 rcu_assign_pointer(pn->left, in);
916 RCU_INIT_POINTER(ln->parent, in);
917 rcu_assign_pointer(fn->parent, in);
919 if (addr_bit_set(addr, bit)) {
920 rcu_assign_pointer(in->right, ln);
921 rcu_assign_pointer(in->left, fn);
923 rcu_assign_pointer(in->left, ln);
924 rcu_assign_pointer(in->right, fn);
926 } else { /* plen <= bit */
929 * (new leaf node)[ln]
931 * (old node)[fn] NULL
934 ln = node_alloc(net);
937 return ERR_PTR(-ENOMEM);
941 RCU_INIT_POINTER(ln->parent, pn);
943 if (addr_bit_set(&key->addr, plen))
944 RCU_INIT_POINTER(ln->right, fn);
946 RCU_INIT_POINTER(ln->left, fn);
948 rcu_assign_pointer(fn->parent, ln);
951 rcu_assign_pointer(pn->right, ln);
953 rcu_assign_pointer(pn->left, ln);
958 static void __fib6_drop_pcpu_from(struct fib6_nh *fib6_nh,
959 const struct fib6_info *match,
960 const struct fib6_table *table)
964 if (!fib6_nh->rt6i_pcpu)
967 /* release the reference to this fib entry from
968 * all of its cached pcpu routes
970 for_each_possible_cpu(cpu) {
971 struct rt6_info **ppcpu_rt;
972 struct rt6_info *pcpu_rt;
974 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
977 /* only dropping the 'from' reference if the cached route
978 * is using 'match'. The cached pcpu_rt->from only changes
979 * from a fib6_info to NULL (ip6_dst_destroy); it can never
980 * change from one fib6_info reference to another
982 if (pcpu_rt && rcu_access_pointer(pcpu_rt->from) == match) {
983 struct fib6_info *from;
985 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
986 fib6_info_release(from);
991 struct fib6_nh_pcpu_arg {
992 struct fib6_info *from;
993 const struct fib6_table *table;
996 static int fib6_nh_drop_pcpu_from(struct fib6_nh *nh, void *_arg)
998 struct fib6_nh_pcpu_arg *arg = _arg;
1000 __fib6_drop_pcpu_from(nh, arg->from, arg->table);
1004 static void fib6_drop_pcpu_from(struct fib6_info *f6i,
1005 const struct fib6_table *table)
1007 /* Make sure rt6_make_pcpu_route() wont add other percpu routes
1008 * while we are cleaning them here.
1010 f6i->fib6_destroying = 1;
1011 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
1014 struct fib6_nh_pcpu_arg arg = {
1019 nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_drop_pcpu_from,
1022 struct fib6_nh *fib6_nh;
1024 fib6_nh = f6i->fib6_nh;
1025 __fib6_drop_pcpu_from(fib6_nh, f6i, table);
1029 static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
1032 struct fib6_table *table = rt->fib6_table;
1034 /* Flush all cached dst in exception table */
1035 rt6_flush_exceptions(rt);
1036 fib6_drop_pcpu_from(rt, table);
1038 if (rt->nh && !list_empty(&rt->nh_list))
1039 list_del_init(&rt->nh_list);
1041 if (refcount_read(&rt->fib6_ref) != 1) {
1042 /* This route is used as dummy address holder in some split
1043 * nodes. It is not leaked, but it still holds other resources,
1044 * which must be released in time. So, scan ascendant nodes
1045 * and replace dummy references to this route with references
1046 * to still alive ones.
1049 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
1050 lockdep_is_held(&table->tb6_lock));
1051 struct fib6_info *new_leaf;
1052 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
1053 new_leaf = fib6_find_prefix(net, table, fn);
1054 fib6_info_hold(new_leaf);
1056 rcu_assign_pointer(fn->leaf, new_leaf);
1057 fib6_info_release(rt);
1059 fn = rcu_dereference_protected(fn->parent,
1060 lockdep_is_held(&table->tb6_lock));
1064 fib6_clean_expires_locked(rt);
1068 * Insert routing information in a node.
1071 static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
1072 struct nl_info *info,
1073 struct netlink_ext_ack *extack)
1075 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
1076 lockdep_is_held(&rt->fib6_table->tb6_lock));
1077 struct fib6_info *iter = NULL;
1078 struct fib6_info __rcu **ins;
1079 struct fib6_info __rcu **fallback_ins = NULL;
1080 int replace = (info->nlh &&
1081 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
1082 int add = (!info->nlh ||
1083 (info->nlh->nlmsg_flags & NLM_F_CREATE));
1085 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
1086 bool notify_sibling_rt = false;
1087 u16 nlflags = NLM_F_EXCL;
1090 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
1091 nlflags |= NLM_F_APPEND;
1095 for (iter = leaf; iter;
1096 iter = rcu_dereference_protected(iter->fib6_next,
1097 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
1099 * Search for duplicates
1102 if (iter->fib6_metric == rt->fib6_metric) {
1104 * Same priority level
1107 (info->nlh->nlmsg_flags & NLM_F_EXCL))
1110 nlflags &= ~NLM_F_EXCL;
1112 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
1116 fallback_ins = fallback_ins ?: ins;
1120 if (rt6_duplicate_nexthop(iter, rt)) {
1121 if (rt->fib6_nsiblings)
1122 rt->fib6_nsiblings = 0;
1123 if (!(iter->fib6_flags & RTF_EXPIRES))
1125 if (!(rt->fib6_flags & RTF_EXPIRES))
1126 fib6_clean_expires_locked(iter);
1128 fib6_set_expires_locked(iter,
1132 fib6_metric_set(iter, RTAX_MTU,
1136 /* If we have the same destination and the same metric,
1137 * but not the same gateway, then the route we try to
1138 * add is sibling to this route, increment our counter
1139 * of siblings, and later we will add our route to the
1141 * Only static routes (which don't have flag
1142 * RTF_EXPIRES) are used for ECMPv6.
1144 * To avoid long list, we only had siblings if the
1145 * route have a gateway.
1148 rt6_qualify_for_ecmp(iter))
1149 rt->fib6_nsiblings++;
1152 if (iter->fib6_metric > rt->fib6_metric)
1156 ins = &iter->fib6_next;
1159 if (fallback_ins && !found) {
1160 /* No matching route with same ecmp-able-ness found, replace
1161 * first matching route
1164 iter = rcu_dereference_protected(*ins,
1165 lockdep_is_held(&rt->fib6_table->tb6_lock));
1169 /* Reset round-robin state, if necessary */
1170 if (ins == &fn->leaf)
1173 /* Link this route to others same route. */
1174 if (rt->fib6_nsiblings) {
1175 unsigned int fib6_nsiblings;
1176 struct fib6_info *sibling, *temp_sibling;
1178 /* Find the first route that have the same metric */
1180 notify_sibling_rt = true;
1182 if (sibling->fib6_metric == rt->fib6_metric &&
1183 rt6_qualify_for_ecmp(sibling)) {
1184 list_add_tail(&rt->fib6_siblings,
1185 &sibling->fib6_siblings);
1188 sibling = rcu_dereference_protected(sibling->fib6_next,
1189 lockdep_is_held(&rt->fib6_table->tb6_lock));
1190 notify_sibling_rt = false;
1192 /* For each sibling in the list, increment the counter of
1193 * siblings. BUG() if counters does not match, list of siblings
1197 list_for_each_entry_safe(sibling, temp_sibling,
1198 &rt->fib6_siblings, fib6_siblings) {
1199 sibling->fib6_nsiblings++;
1200 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1203 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1204 rt6_multipath_rebalance(temp_sibling);
1212 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1215 nlflags |= NLM_F_CREATE;
1217 /* The route should only be notified if it is the first
1218 * route in the node or if it is added as a sibling
1219 * route to the first route in the node.
1221 if (!info->skip_notify_kernel &&
1222 (notify_sibling_rt || ins == &fn->leaf)) {
1223 enum fib_event_type fib_event;
1225 if (notify_sibling_rt)
1226 fib_event = FIB_EVENT_ENTRY_APPEND;
1228 fib_event = FIB_EVENT_ENTRY_REPLACE;
1229 err = call_fib6_entry_notifiers(info->nl_net,
1233 struct fib6_info *sibling, *next_sibling;
1235 /* If the route has siblings, then it first
1236 * needs to be unlinked from them.
1238 if (!rt->fib6_nsiblings)
1241 list_for_each_entry_safe(sibling, next_sibling,
1244 sibling->fib6_nsiblings--;
1245 rt->fib6_nsiblings = 0;
1246 list_del_init(&rt->fib6_siblings);
1247 rt6_multipath_rebalance(next_sibling);
1252 rcu_assign_pointer(rt->fib6_next, iter);
1254 rcu_assign_pointer(rt->fib6_node, fn);
1255 rcu_assign_pointer(*ins, rt);
1256 if (!info->skip_notify)
1257 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1258 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1260 if (!(fn->fn_flags & RTN_RTINFO)) {
1261 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1262 fn->fn_flags |= RTN_RTINFO;
1271 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1275 if (!info->skip_notify_kernel && ins == &fn->leaf) {
1276 err = call_fib6_entry_notifiers(info->nl_net,
1277 FIB_EVENT_ENTRY_REPLACE,
1284 rcu_assign_pointer(rt->fib6_node, fn);
1285 rt->fib6_next = iter->fib6_next;
1286 rcu_assign_pointer(*ins, rt);
1287 if (!info->skip_notify)
1288 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1289 if (!(fn->fn_flags & RTN_RTINFO)) {
1290 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1291 fn->fn_flags |= RTN_RTINFO;
1293 nsiblings = iter->fib6_nsiblings;
1294 iter->fib6_node = NULL;
1295 fib6_purge_rt(iter, fn, info->nl_net);
1296 if (rcu_access_pointer(fn->rr_ptr) == iter)
1298 fib6_info_release(iter);
1301 /* Replacing an ECMP route, remove all siblings */
1302 ins = &rt->fib6_next;
1303 iter = rcu_dereference_protected(*ins,
1304 lockdep_is_held(&rt->fib6_table->tb6_lock));
1306 if (iter->fib6_metric > rt->fib6_metric)
1308 if (rt6_qualify_for_ecmp(iter)) {
1309 *ins = iter->fib6_next;
1310 iter->fib6_node = NULL;
1311 fib6_purge_rt(iter, fn, info->nl_net);
1312 if (rcu_access_pointer(fn->rr_ptr) == iter)
1314 fib6_info_release(iter);
1316 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1318 ins = &iter->fib6_next;
1320 iter = rcu_dereference_protected(*ins,
1321 lockdep_is_held(&rt->fib6_table->tb6_lock));
1323 WARN_ON(nsiblings != 0);
1330 static void fib6_start_gc(struct net *net, struct fib6_info *rt)
1332 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1333 (rt->fib6_flags & RTF_EXPIRES))
1334 mod_timer(&net->ipv6.ip6_fib_timer,
1335 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1338 void fib6_force_start_gc(struct net *net)
1340 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1341 mod_timer(&net->ipv6.ip6_fib_timer,
1342 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1345 static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
1348 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1349 lockdep_is_held(&rt->fib6_table->tb6_lock));
1351 /* paired with smp_rmb() in fib6_get_cookie_safe() */
1354 WRITE_ONCE(fn->fn_sernum, sernum);
1355 fn = rcu_dereference_protected(fn->parent,
1356 lockdep_is_held(&rt->fib6_table->tb6_lock));
1360 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
1362 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1365 /* allow ipv4 to update sernum via ipv6_stub */
1366 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i)
1368 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1369 fib6_update_sernum_upto_root(net, f6i);
1370 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1374 * Add routing information to the routing tree.
1375 * <destination addr>/<source addr>
1376 * with source addr info in sub-trees
1377 * Need to own table->tb6_lock
1380 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
1381 struct nl_info *info, struct netlink_ext_ack *extack)
1383 struct fib6_table *table = rt->fib6_table;
1384 struct fib6_node *fn, *pn = NULL;
1386 int allow_create = 1;
1387 int replace_required = 0;
1390 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1392 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1393 replace_required = 1;
1395 if (!allow_create && !replace_required)
1396 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1398 fn = fib6_add_1(info->nl_net, table, root,
1399 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1400 offsetof(struct fib6_info, fib6_dst), allow_create,
1401 replace_required, extack);
1410 #ifdef CONFIG_IPV6_SUBTREES
1411 if (rt->fib6_src.plen) {
1412 struct fib6_node *sn;
1414 if (!rcu_access_pointer(fn->subtree)) {
1415 struct fib6_node *sfn;
1427 /* Create subtree root node */
1428 sfn = node_alloc(info->nl_net);
1432 fib6_info_hold(info->nl_net->ipv6.fib6_null_entry);
1433 rcu_assign_pointer(sfn->leaf,
1434 info->nl_net->ipv6.fib6_null_entry);
1435 sfn->fn_flags = RTN_ROOT;
1437 /* Now add the first leaf node to new subtree */
1439 sn = fib6_add_1(info->nl_net, table, sfn,
1440 &rt->fib6_src.addr, rt->fib6_src.plen,
1441 offsetof(struct fib6_info, fib6_src),
1442 allow_create, replace_required, extack);
1445 /* If it is failed, discard just allocated
1446 root, and then (in failure) stale node
1449 node_free_immediate(info->nl_net, sfn);
1454 /* Now link new subtree to main tree */
1455 rcu_assign_pointer(sfn->parent, fn);
1456 rcu_assign_pointer(fn->subtree, sfn);
1458 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1459 &rt->fib6_src.addr, rt->fib6_src.plen,
1460 offsetof(struct fib6_info, fib6_src),
1461 allow_create, replace_required, extack);
1469 if (!rcu_access_pointer(fn->leaf)) {
1470 if (fn->fn_flags & RTN_TL_ROOT) {
1471 /* put back null_entry for root node */
1472 rcu_assign_pointer(fn->leaf,
1473 info->nl_net->ipv6.fib6_null_entry);
1476 rcu_assign_pointer(fn->leaf, rt);
1483 err = fib6_add_rt2node(fn, rt, info, extack);
1486 list_add(&rt->nh_list, &rt->nh->f6i_list);
1487 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(info->nl_net));
1489 if (fib6_has_expires(rt))
1490 hlist_add_head(&rt->gc_link, &table->tb6_gc_hlist);
1492 fib6_start_gc(info->nl_net, rt);
1497 #ifdef CONFIG_IPV6_SUBTREES
1499 * If fib6_add_1 has cleared the old leaf pointer in the
1500 * super-tree leaf node we have to find a new one for it.
1503 struct fib6_info *pn_leaf =
1504 rcu_dereference_protected(pn->leaf,
1505 lockdep_is_held(&table->tb6_lock));
1506 if (pn_leaf == rt) {
1508 RCU_INIT_POINTER(pn->leaf, NULL);
1509 fib6_info_release(rt);
1511 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1512 pn_leaf = fib6_find_prefix(info->nl_net, table,
1518 info->nl_net->ipv6.fib6_null_entry;
1521 fib6_info_hold(pn_leaf);
1522 rcu_assign_pointer(pn->leaf, pn_leaf);
1527 } else if (fib6_requires_src(rt)) {
1528 fib6_routes_require_src_inc(info->nl_net);
1533 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1534 * 1. fn is an intermediate node and we failed to add the new
1535 * route to it in both subtree creation failure and fib6_add_rt2node()
1537 * 2. fn is the root node in the table and we fail to add the first
1538 * default route to it.
1541 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1542 (fn->fn_flags & RTN_TL_ROOT &&
1543 !rcu_access_pointer(fn->leaf))))
1544 fib6_repair_tree(info->nl_net, table, fn);
1549 * Routing tree lookup
1553 struct lookup_args {
1554 int offset; /* key offset on fib6_info */
1555 const struct in6_addr *addr; /* search key */
1558 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1559 struct lookup_args *args)
1561 struct fib6_node *fn;
1564 if (unlikely(args->offset == 0))
1574 struct fib6_node *next;
1576 dir = addr_bit_set(args->addr, fn->fn_bit);
1578 next = dir ? rcu_dereference(fn->right) :
1579 rcu_dereference(fn->left);
1589 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1591 if (subtree || fn->fn_flags & RTN_RTINFO) {
1592 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1598 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1600 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1601 #ifdef CONFIG_IPV6_SUBTREES
1603 struct fib6_node *sfn;
1604 sfn = fib6_node_lookup_1(subtree,
1611 if (fn->fn_flags & RTN_RTINFO)
1616 if (fn->fn_flags & RTN_ROOT)
1619 fn = rcu_dereference(fn->parent);
1625 /* called with rcu_read_lock() held
1627 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1628 const struct in6_addr *daddr,
1629 const struct in6_addr *saddr)
1631 struct fib6_node *fn;
1632 struct lookup_args args[] = {
1634 .offset = offsetof(struct fib6_info, fib6_dst),
1637 #ifdef CONFIG_IPV6_SUBTREES
1639 .offset = offsetof(struct fib6_info, fib6_src),
1644 .offset = 0, /* sentinel */
1648 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
1649 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1656 * Get node with specified destination prefix (and source prefix,
1657 * if subtrees are used)
1658 * exact_match == true means we try to find fn with exact match of
1659 * the passed in prefix addr
1660 * exact_match == false means we try to find fn with longest prefix
1661 * match of the passed in prefix addr. This is useful for finding fn
1662 * for cached route as it will be stored in the exception table under
1663 * the node with longest prefix length.
1667 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1668 const struct in6_addr *addr,
1669 int plen, int offset,
1672 struct fib6_node *fn, *prev = NULL;
1674 for (fn = root; fn ; ) {
1675 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1678 /* This node is being deleted */
1680 if (plen <= fn->fn_bit)
1686 key = (struct rt6key *)((u8 *)leaf + offset);
1691 if (plen < fn->fn_bit ||
1692 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1695 if (plen == fn->fn_bit)
1698 if (fn->fn_flags & RTN_RTINFO)
1703 * We have more bits to go
1705 if (addr_bit_set(addr, fn->fn_bit))
1706 fn = rcu_dereference(fn->right);
1708 fn = rcu_dereference(fn->left);
1717 struct fib6_node *fib6_locate(struct fib6_node *root,
1718 const struct in6_addr *daddr, int dst_len,
1719 const struct in6_addr *saddr, int src_len,
1722 struct fib6_node *fn;
1724 fn = fib6_locate_1(root, daddr, dst_len,
1725 offsetof(struct fib6_info, fib6_dst),
1728 #ifdef CONFIG_IPV6_SUBTREES
1730 WARN_ON(saddr == NULL);
1732 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1735 fn = fib6_locate_1(subtree, saddr, src_len,
1736 offsetof(struct fib6_info, fib6_src),
1743 if (fn && fn->fn_flags & RTN_RTINFO)
1755 static struct fib6_info *fib6_find_prefix(struct net *net,
1756 struct fib6_table *table,
1757 struct fib6_node *fn)
1759 struct fib6_node *child_left, *child_right;
1761 if (fn->fn_flags & RTN_ROOT)
1762 return net->ipv6.fib6_null_entry;
1765 child_left = rcu_dereference_protected(fn->left,
1766 lockdep_is_held(&table->tb6_lock));
1767 child_right = rcu_dereference_protected(fn->right,
1768 lockdep_is_held(&table->tb6_lock));
1770 return rcu_dereference_protected(child_left->leaf,
1771 lockdep_is_held(&table->tb6_lock));
1773 return rcu_dereference_protected(child_right->leaf,
1774 lockdep_is_held(&table->tb6_lock));
1776 fn = FIB6_SUBTREE(fn);
1782 * Called to trim the tree of intermediate nodes when possible. "fn"
1783 * is the node we want to try and remove.
1784 * Need to own table->tb6_lock
1787 static struct fib6_node *fib6_repair_tree(struct net *net,
1788 struct fib6_table *table,
1789 struct fib6_node *fn)
1793 struct fib6_node *child;
1794 struct fib6_walker *w;
1797 /* Set fn->leaf to null_entry for root node. */
1798 if (fn->fn_flags & RTN_TL_ROOT) {
1799 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1804 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1805 lockdep_is_held(&table->tb6_lock));
1806 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1807 lockdep_is_held(&table->tb6_lock));
1808 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1809 lockdep_is_held(&table->tb6_lock));
1810 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1811 lockdep_is_held(&table->tb6_lock));
1812 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1813 lockdep_is_held(&table->tb6_lock));
1814 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1815 lockdep_is_held(&table->tb6_lock));
1816 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1817 lockdep_is_held(&table->tb6_lock));
1818 struct fib6_info *new_fn_leaf;
1820 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1823 WARN_ON(fn->fn_flags & RTN_RTINFO);
1824 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1838 if (children == 3 || FIB6_SUBTREE(fn)
1839 #ifdef CONFIG_IPV6_SUBTREES
1840 /* Subtree root (i.e. fn) may have one child */
1841 || (children && fn->fn_flags & RTN_ROOT)
1844 new_fn_leaf = fib6_find_prefix(net, table, fn);
1847 WARN_ON(!new_fn_leaf);
1848 new_fn_leaf = net->ipv6.fib6_null_entry;
1851 fib6_info_hold(new_fn_leaf);
1852 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1856 #ifdef CONFIG_IPV6_SUBTREES
1857 if (FIB6_SUBTREE(pn) == fn) {
1858 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1859 RCU_INIT_POINTER(pn->subtree, NULL);
1862 WARN_ON(fn->fn_flags & RTN_ROOT);
1865 rcu_assign_pointer(pn->right, child);
1866 else if (pn_l == fn)
1867 rcu_assign_pointer(pn->left, child);
1873 rcu_assign_pointer(child->parent, pn);
1875 #ifdef CONFIG_IPV6_SUBTREES
1879 read_lock(&net->ipv6.fib6_walker_lock);
1880 FOR_WALKERS(net, w) {
1882 if (w->node == fn) {
1883 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1888 if (w->node == fn) {
1891 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1892 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1894 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1895 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1900 read_unlock(&net->ipv6.fib6_walker_lock);
1903 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1906 RCU_INIT_POINTER(pn->leaf, NULL);
1907 fib6_info_release(pn_leaf);
1912 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1913 struct fib6_info __rcu **rtp, struct nl_info *info)
1915 struct fib6_info *leaf, *replace_rt = NULL;
1916 struct fib6_walker *w;
1917 struct fib6_info *rt = rcu_dereference_protected(*rtp,
1918 lockdep_is_held(&table->tb6_lock));
1919 struct net *net = info->nl_net;
1920 bool notify_del = false;
1922 RT6_TRACE("fib6_del_route\n");
1924 /* If the deleted route is the first in the node and it is not part of
1925 * a multipath route, then we need to replace it with the next route
1926 * in the node, if exists.
1928 leaf = rcu_dereference_protected(fn->leaf,
1929 lockdep_is_held(&table->tb6_lock));
1930 if (leaf == rt && !rt->fib6_nsiblings) {
1931 if (rcu_access_pointer(rt->fib6_next))
1932 replace_rt = rcu_dereference_protected(rt->fib6_next,
1933 lockdep_is_held(&table->tb6_lock));
1939 *rtp = rt->fib6_next;
1940 rt->fib6_node = NULL;
1941 net->ipv6.rt6_stats->fib_rt_entries--;
1942 net->ipv6.rt6_stats->fib_discarded_routes++;
1944 /* Reset round-robin state, if necessary */
1945 if (rcu_access_pointer(fn->rr_ptr) == rt)
1948 /* Remove this entry from other siblings */
1949 if (rt->fib6_nsiblings) {
1950 struct fib6_info *sibling, *next_sibling;
1952 /* The route is deleted from a multipath route. If this
1953 * multipath route is the first route in the node, then we need
1954 * to emit a delete notification. Otherwise, we need to skip
1957 if (rt->fib6_metric == leaf->fib6_metric &&
1958 rt6_qualify_for_ecmp(leaf))
1960 list_for_each_entry_safe(sibling, next_sibling,
1961 &rt->fib6_siblings, fib6_siblings)
1962 sibling->fib6_nsiblings--;
1963 rt->fib6_nsiblings = 0;
1964 list_del_init(&rt->fib6_siblings);
1965 rt6_multipath_rebalance(next_sibling);
1968 /* Adjust walkers */
1969 read_lock(&net->ipv6.fib6_walker_lock);
1970 FOR_WALKERS(net, w) {
1971 if (w->state == FWS_C && w->leaf == rt) {
1972 RT6_TRACE("walker %p adjusted by delroute\n", w);
1973 w->leaf = rcu_dereference_protected(rt->fib6_next,
1974 lockdep_is_held(&table->tb6_lock));
1979 read_unlock(&net->ipv6.fib6_walker_lock);
1981 /* If it was last route, call fib6_repair_tree() to:
1982 * 1. For root node, put back null_entry as how the table was created.
1983 * 2. For other nodes, expunge its radix tree node.
1985 if (!rcu_access_pointer(fn->leaf)) {
1986 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1987 fn->fn_flags &= ~RTN_RTINFO;
1988 net->ipv6.rt6_stats->fib_route_nodes--;
1990 fn = fib6_repair_tree(net, table, fn);
1993 fib6_purge_rt(rt, fn, net);
1995 if (!info->skip_notify_kernel) {
1997 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL,
1999 else if (replace_rt)
2000 call_fib6_entry_notifiers_replace(net, replace_rt);
2002 if (!info->skip_notify)
2003 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
2005 fib6_info_release(rt);
2008 /* Need to own table->tb6_lock */
2009 int fib6_del(struct fib6_info *rt, struct nl_info *info)
2011 struct net *net = info->nl_net;
2012 struct fib6_info __rcu **rtp;
2013 struct fib6_info __rcu **rtp_next;
2014 struct fib6_table *table;
2015 struct fib6_node *fn;
2017 if (rt == net->ipv6.fib6_null_entry)
2020 table = rt->fib6_table;
2021 fn = rcu_dereference_protected(rt->fib6_node,
2022 lockdep_is_held(&table->tb6_lock));
2026 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
2029 * Walk the leaf entries looking for ourself
2032 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
2033 struct fib6_info *cur = rcu_dereference_protected(*rtp,
2034 lockdep_is_held(&table->tb6_lock));
2036 if (fib6_requires_src(cur))
2037 fib6_routes_require_src_dec(info->nl_net);
2038 fib6_del_route(table, fn, rtp, info);
2041 rtp_next = &cur->fib6_next;
2047 * Tree traversal function.
2049 * Certainly, it is not interrupt safe.
2050 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
2051 * It means, that we can modify tree during walking
2052 * and use this function for garbage collection, clone pruning,
2053 * cleaning tree when a device goes down etc. etc.
2055 * It guarantees that every node will be traversed,
2056 * and that it will be traversed only once.
2058 * Callback function w->func may return:
2059 * 0 -> continue walking.
2060 * positive value -> walking is suspended (used by tree dumps,
2061 * and probably by gc, if it will be split to several slices)
2062 * negative value -> terminate walking.
2064 * The function itself returns:
2065 * 0 -> walk is complete.
2066 * >0 -> walk is incomplete (i.e. suspended)
2067 * <0 -> walk is terminated by an error.
2069 * This function is called with tb6_lock held.
2072 static int fib6_walk_continue(struct fib6_walker *w)
2074 struct fib6_node *fn, *pn, *left, *right;
2076 /* w->root should always be table->tb6_root */
2077 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
2085 #ifdef CONFIG_IPV6_SUBTREES
2087 if (FIB6_SUBTREE(fn)) {
2088 w->node = FIB6_SUBTREE(fn);
2095 left = rcu_dereference_protected(fn->left, 1);
2098 w->state = FWS_INIT;
2104 right = rcu_dereference_protected(fn->right, 1);
2107 w->state = FWS_INIT;
2111 w->leaf = rcu_dereference_protected(fn->leaf, 1);
2114 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
2135 pn = rcu_dereference_protected(fn->parent, 1);
2136 left = rcu_dereference_protected(pn->left, 1);
2137 right = rcu_dereference_protected(pn->right, 1);
2139 #ifdef CONFIG_IPV6_SUBTREES
2140 if (FIB6_SUBTREE(pn) == fn) {
2141 WARN_ON(!(fn->fn_flags & RTN_ROOT));
2152 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
2162 static int fib6_walk(struct net *net, struct fib6_walker *w)
2166 w->state = FWS_INIT;
2169 fib6_walker_link(net, w);
2170 res = fib6_walk_continue(w);
2172 fib6_walker_unlink(net, w);
2176 static int fib6_clean_node(struct fib6_walker *w)
2179 struct fib6_info *rt;
2180 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
2181 struct nl_info info = {
2183 .skip_notify = c->skip_notify,
2186 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
2187 READ_ONCE(w->node->fn_sernum) != c->sernum)
2188 WRITE_ONCE(w->node->fn_sernum, c->sernum);
2191 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
2196 for_each_fib6_walker_rt(w) {
2197 res = c->func(rt, c->arg);
2200 res = fib6_del(rt, &info);
2203 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
2205 rcu_access_pointer(rt->fib6_node),
2211 } else if (res == -2) {
2212 if (WARN_ON(!rt->fib6_nsiblings))
2214 rt = list_last_entry(&rt->fib6_siblings,
2215 struct fib6_info, fib6_siblings);
2225 * Convenient frontend to tree walker.
2227 * func is called on each route.
2228 * It may return -2 -> skip multipath route.
2229 * -1 -> delete this route.
2230 * 0 -> continue walking
2233 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2234 int (*func)(struct fib6_info *, void *arg),
2235 int sernum, void *arg, bool skip_notify)
2237 struct fib6_cleaner c;
2240 c.w.func = fib6_clean_node;
2243 c.w.skip_in_node = 0;
2248 c.skip_notify = skip_notify;
2250 fib6_walk(net, &c.w);
2253 static void __fib6_clean_all(struct net *net,
2254 int (*func)(struct fib6_info *, void *),
2255 int sernum, void *arg, bool skip_notify)
2257 struct fib6_table *table;
2258 struct hlist_head *head;
2262 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2263 head = &net->ipv6.fib_table_hash[h];
2264 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2265 spin_lock_bh(&table->tb6_lock);
2266 fib6_clean_tree(net, &table->tb6_root,
2267 func, sernum, arg, skip_notify);
2268 spin_unlock_bh(&table->tb6_lock);
2274 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
2277 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2280 void fib6_clean_all_skip_notify(struct net *net,
2281 int (*func)(struct fib6_info *, void *),
2284 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
2287 static void fib6_flush_trees(struct net *net)
2289 int new_sernum = fib6_new_sernum(net);
2291 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
2295 * Garbage collection
2298 static int fib6_age(struct fib6_info *rt, struct fib6_gc_args *gc_args)
2300 unsigned long now = jiffies;
2303 * check addrconf expiration here.
2304 * Routes are expired even if they are in use.
2307 if (fib6_has_expires(rt) && rt->expires) {
2308 if (time_after(now, rt->expires)) {
2309 RT6_TRACE("expiring %p\n", rt);
2315 /* Also age clones in the exception table.
2316 * Note, that clones are aged out
2317 * only if they are not in use now.
2319 rt6_age_exceptions(rt, gc_args, now);
2324 static void fib6_gc_table(struct net *net,
2325 struct fib6_table *tb6,
2326 struct fib6_gc_args *gc_args)
2328 struct fib6_info *rt;
2329 struct hlist_node *n;
2330 struct nl_info info = {
2332 .skip_notify = false,
2335 hlist_for_each_entry_safe(rt, n, &tb6->tb6_gc_hlist, gc_link)
2336 if (fib6_age(rt, gc_args) == -1)
2337 fib6_del(rt, &info);
2340 static void fib6_gc_all(struct net *net, struct fib6_gc_args *gc_args)
2342 struct fib6_table *table;
2343 struct hlist_head *head;
2347 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2348 head = &net->ipv6.fib_table_hash[h];
2349 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2350 spin_lock_bh(&table->tb6_lock);
2351 fib6_gc_table(net, table, gc_args);
2352 spin_unlock_bh(&table->tb6_lock);
2358 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2360 struct fib6_gc_args gc_args;
2364 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2365 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2366 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2369 gc_args.timeout = expires ? (int)expires :
2370 net->ipv6.sysctl.ip6_rt_gc_interval;
2373 fib6_gc_all(net, &gc_args);
2375 net->ipv6.ip6_rt_last_gc = now;
2378 mod_timer(&net->ipv6.ip6_fib_timer,
2380 + net->ipv6.sysctl.ip6_rt_gc_interval));
2382 del_timer(&net->ipv6.ip6_fib_timer);
2383 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2386 static void fib6_gc_timer_cb(struct timer_list *t)
2388 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2390 fib6_run_gc(0, arg, true);
2393 static int __net_init fib6_net_init(struct net *net)
2395 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2398 err = fib6_notifier_init(net);
2402 /* Default to 3-tuple */
2403 net->ipv6.sysctl.multipath_hash_fields =
2404 FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK;
2406 spin_lock_init(&net->ipv6.fib6_gc_lock);
2407 rwlock_init(&net->ipv6.fib6_walker_lock);
2408 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2409 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2411 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2412 if (!net->ipv6.rt6_stats)
2415 /* Avoid false sharing : Use at least a full cache line */
2416 size = max_t(size_t, size, L1_CACHE_BYTES);
2418 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2419 if (!net->ipv6.fib_table_hash)
2422 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2424 if (!net->ipv6.fib6_main_tbl)
2425 goto out_fib_table_hash;
2427 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2428 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2429 net->ipv6.fib6_null_entry);
2430 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2431 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2432 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2434 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2435 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2437 if (!net->ipv6.fib6_local_tbl)
2438 goto out_fib6_main_tbl;
2439 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2440 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2441 net->ipv6.fib6_null_entry);
2442 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2443 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2444 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2446 fib6_tables_init(net);
2450 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2452 kfree(net->ipv6.fib6_main_tbl);
2455 kfree(net->ipv6.fib_table_hash);
2457 kfree(net->ipv6.rt6_stats);
2459 fib6_notifier_exit(net);
2463 static void fib6_net_exit(struct net *net)
2467 del_timer_sync(&net->ipv6.ip6_fib_timer);
2469 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2470 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2471 struct hlist_node *tmp;
2472 struct fib6_table *tb;
2474 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2475 hlist_del(&tb->tb6_hlist);
2476 fib6_free_table(tb);
2480 kfree(net->ipv6.fib_table_hash);
2481 kfree(net->ipv6.rt6_stats);
2482 fib6_notifier_exit(net);
2485 static struct pernet_operations fib6_net_ops = {
2486 .init = fib6_net_init,
2487 .exit = fib6_net_exit,
2490 int __init fib6_init(void)
2494 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2495 sizeof(struct fib6_node), 0,
2496 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
2498 if (!fib6_node_kmem)
2501 ret = register_pernet_subsys(&fib6_net_ops);
2503 goto out_kmem_cache_create;
2505 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2508 goto out_unregister_subsys;
2510 __fib6_flush_trees = fib6_flush_trees;
2514 out_unregister_subsys:
2515 unregister_pernet_subsys(&fib6_net_ops);
2516 out_kmem_cache_create:
2517 kmem_cache_destroy(fib6_node_kmem);
2521 void fib6_gc_cleanup(void)
2523 unregister_pernet_subsys(&fib6_net_ops);
2524 kmem_cache_destroy(fib6_node_kmem);
2527 #ifdef CONFIG_PROC_FS
2528 static int ipv6_route_native_seq_show(struct seq_file *seq, void *v)
2530 struct fib6_info *rt = v;
2531 struct ipv6_route_iter *iter = seq->private;
2532 struct fib6_nh *fib6_nh = rt->fib6_nh;
2533 unsigned int flags = rt->fib6_flags;
2534 const struct net_device *dev;
2537 fib6_nh = nexthop_fib6_nh(rt->nh);
2539 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
2541 #ifdef CONFIG_IPV6_SUBTREES
2542 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
2544 seq_puts(seq, "00000000000000000000000000000000 00 ");
2546 if (fib6_nh->fib_nh_gw_family) {
2547 flags |= RTF_GATEWAY;
2548 seq_printf(seq, "%pi6", &fib6_nh->fib_nh_gw6);
2550 seq_puts(seq, "00000000000000000000000000000000");
2553 dev = fib6_nh->fib_nh_dev;
2554 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2555 rt->fib6_metric, refcount_read(&rt->fib6_ref), 0,
2556 flags, dev ? dev->name : "");
2557 iter->w.leaf = NULL;
2561 static int ipv6_route_yield(struct fib6_walker *w)
2563 struct ipv6_route_iter *iter = w->args;
2569 iter->w.leaf = rcu_dereference_protected(
2570 iter->w.leaf->fib6_next,
2571 lockdep_is_held(&iter->tbl->tb6_lock));
2573 if (!iter->skip && iter->w.leaf)
2575 } while (iter->w.leaf);
2580 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2583 memset(&iter->w, 0, sizeof(iter->w));
2584 iter->w.func = ipv6_route_yield;
2585 iter->w.root = &iter->tbl->tb6_root;
2586 iter->w.state = FWS_INIT;
2587 iter->w.node = iter->w.root;
2588 iter->w.args = iter;
2589 iter->sernum = READ_ONCE(iter->w.root->fn_sernum);
2590 INIT_LIST_HEAD(&iter->w.lh);
2591 fib6_walker_link(net, &iter->w);
2594 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2598 struct hlist_node *node;
2601 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2602 node = rcu_dereference(hlist_next_rcu(&tbl->tb6_hlist));
2608 while (!node && h < FIB6_TABLE_HASHSZ) {
2609 node = rcu_dereference(
2610 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2612 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2615 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2617 int sernum = READ_ONCE(iter->w.root->fn_sernum);
2619 if (iter->sernum != sernum) {
2620 iter->sernum = sernum;
2621 iter->w.state = FWS_INIT;
2622 iter->w.node = iter->w.root;
2623 WARN_ON(iter->w.skip);
2624 iter->w.skip = iter->w.count;
2628 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2631 struct fib6_info *n;
2632 struct net *net = seq_file_net(seq);
2633 struct ipv6_route_iter *iter = seq->private;
2639 n = rcu_dereference(((struct fib6_info *)v)->fib6_next);
2644 ipv6_route_check_sernum(iter);
2645 spin_lock_bh(&iter->tbl->tb6_lock);
2646 r = fib6_walk_continue(&iter->w);
2647 spin_unlock_bh(&iter->tbl->tb6_lock);
2649 return iter->w.leaf;
2651 fib6_walker_unlink(net, &iter->w);
2654 fib6_walker_unlink(net, &iter->w);
2656 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2660 ipv6_route_seq_setup_walk(iter, net);
2664 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2667 struct net *net = seq_file_net(seq);
2668 struct ipv6_route_iter *iter = seq->private;
2671 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2677 ipv6_route_seq_setup_walk(iter, net);
2678 return ipv6_route_seq_next(seq, NULL, &p);
2684 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2686 struct fib6_walker *w = &iter->w;
2687 return w->node && !(w->state == FWS_U && w->node == w->root);
2690 static void ipv6_route_native_seq_stop(struct seq_file *seq, void *v)
2693 struct net *net = seq_file_net(seq);
2694 struct ipv6_route_iter *iter = seq->private;
2696 if (ipv6_route_iter_active(iter))
2697 fib6_walker_unlink(net, &iter->w);
2702 #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
2703 static int ipv6_route_prog_seq_show(struct bpf_prog *prog,
2704 struct bpf_iter_meta *meta,
2707 struct bpf_iter__ipv6_route ctx;
2711 return bpf_iter_run_prog(prog, &ctx);
2714 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2716 struct ipv6_route_iter *iter = seq->private;
2717 struct bpf_iter_meta meta;
2718 struct bpf_prog *prog;
2722 prog = bpf_iter_get_info(&meta, false);
2724 return ipv6_route_native_seq_show(seq, v);
2726 ret = ipv6_route_prog_seq_show(prog, &meta, v);
2727 iter->w.leaf = NULL;
2732 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2734 struct bpf_iter_meta meta;
2735 struct bpf_prog *prog;
2739 prog = bpf_iter_get_info(&meta, true);
2741 (void)ipv6_route_prog_seq_show(prog, &meta, v);
2744 ipv6_route_native_seq_stop(seq, v);
2747 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2749 return ipv6_route_native_seq_show(seq, v);
2752 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2754 ipv6_route_native_seq_stop(seq, v);
2758 const struct seq_operations ipv6_route_seq_ops = {
2759 .start = ipv6_route_seq_start,
2760 .next = ipv6_route_seq_next,
2761 .stop = ipv6_route_seq_stop,
2762 .show = ipv6_route_seq_show
2764 #endif /* CONFIG_PROC_FS */