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,
1516 info->nl_net->ipv6.fib6_null_entry;
1517 fib6_info_hold(pn_leaf);
1518 rcu_assign_pointer(pn->leaf, pn_leaf);
1523 } else if (fib6_requires_src(rt)) {
1524 fib6_routes_require_src_inc(info->nl_net);
1529 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1530 * 1. fn is an intermediate node and we failed to add the new
1531 * route to it in both subtree creation failure and fib6_add_rt2node()
1533 * 2. fn is the root node in the table and we fail to add the first
1534 * default route to it.
1537 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1538 (fn->fn_flags & RTN_TL_ROOT &&
1539 !rcu_access_pointer(fn->leaf))))
1540 fib6_repair_tree(info->nl_net, table, fn);
1545 * Routing tree lookup
1549 struct lookup_args {
1550 int offset; /* key offset on fib6_info */
1551 const struct in6_addr *addr; /* search key */
1554 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1555 struct lookup_args *args)
1557 struct fib6_node *fn;
1560 if (unlikely(args->offset == 0))
1570 struct fib6_node *next;
1572 dir = addr_bit_set(args->addr, fn->fn_bit);
1574 next = dir ? rcu_dereference(fn->right) :
1575 rcu_dereference(fn->left);
1585 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1587 if (subtree || fn->fn_flags & RTN_RTINFO) {
1588 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1594 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1596 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1597 #ifdef CONFIG_IPV6_SUBTREES
1599 struct fib6_node *sfn;
1600 sfn = fib6_node_lookup_1(subtree,
1607 if (fn->fn_flags & RTN_RTINFO)
1612 if (fn->fn_flags & RTN_ROOT)
1615 fn = rcu_dereference(fn->parent);
1621 /* called with rcu_read_lock() held
1623 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1624 const struct in6_addr *daddr,
1625 const struct in6_addr *saddr)
1627 struct fib6_node *fn;
1628 struct lookup_args args[] = {
1630 .offset = offsetof(struct fib6_info, fib6_dst),
1633 #ifdef CONFIG_IPV6_SUBTREES
1635 .offset = offsetof(struct fib6_info, fib6_src),
1640 .offset = 0, /* sentinel */
1644 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
1645 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1652 * Get node with specified destination prefix (and source prefix,
1653 * if subtrees are used)
1654 * exact_match == true means we try to find fn with exact match of
1655 * the passed in prefix addr
1656 * exact_match == false means we try to find fn with longest prefix
1657 * match of the passed in prefix addr. This is useful for finding fn
1658 * for cached route as it will be stored in the exception table under
1659 * the node with longest prefix length.
1663 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1664 const struct in6_addr *addr,
1665 int plen, int offset,
1668 struct fib6_node *fn, *prev = NULL;
1670 for (fn = root; fn ; ) {
1671 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1674 /* This node is being deleted */
1676 if (plen <= fn->fn_bit)
1682 key = (struct rt6key *)((u8 *)leaf + offset);
1687 if (plen < fn->fn_bit ||
1688 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1691 if (plen == fn->fn_bit)
1694 if (fn->fn_flags & RTN_RTINFO)
1699 * We have more bits to go
1701 if (addr_bit_set(addr, fn->fn_bit))
1702 fn = rcu_dereference(fn->right);
1704 fn = rcu_dereference(fn->left);
1713 struct fib6_node *fib6_locate(struct fib6_node *root,
1714 const struct in6_addr *daddr, int dst_len,
1715 const struct in6_addr *saddr, int src_len,
1718 struct fib6_node *fn;
1720 fn = fib6_locate_1(root, daddr, dst_len,
1721 offsetof(struct fib6_info, fib6_dst),
1724 #ifdef CONFIG_IPV6_SUBTREES
1726 WARN_ON(saddr == NULL);
1728 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1731 fn = fib6_locate_1(subtree, saddr, src_len,
1732 offsetof(struct fib6_info, fib6_src),
1739 if (fn && fn->fn_flags & RTN_RTINFO)
1751 static struct fib6_info *fib6_find_prefix(struct net *net,
1752 struct fib6_table *table,
1753 struct fib6_node *fn)
1755 struct fib6_node *child_left, *child_right;
1757 if (fn->fn_flags & RTN_ROOT)
1758 return net->ipv6.fib6_null_entry;
1761 child_left = rcu_dereference_protected(fn->left,
1762 lockdep_is_held(&table->tb6_lock));
1763 child_right = rcu_dereference_protected(fn->right,
1764 lockdep_is_held(&table->tb6_lock));
1766 return rcu_dereference_protected(child_left->leaf,
1767 lockdep_is_held(&table->tb6_lock));
1769 return rcu_dereference_protected(child_right->leaf,
1770 lockdep_is_held(&table->tb6_lock));
1772 fn = FIB6_SUBTREE(fn);
1778 * Called to trim the tree of intermediate nodes when possible. "fn"
1779 * is the node we want to try and remove.
1780 * Need to own table->tb6_lock
1783 static struct fib6_node *fib6_repair_tree(struct net *net,
1784 struct fib6_table *table,
1785 struct fib6_node *fn)
1789 struct fib6_node *child;
1790 struct fib6_walker *w;
1793 /* Set fn->leaf to null_entry for root node. */
1794 if (fn->fn_flags & RTN_TL_ROOT) {
1795 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1800 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1801 lockdep_is_held(&table->tb6_lock));
1802 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1803 lockdep_is_held(&table->tb6_lock));
1804 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1805 lockdep_is_held(&table->tb6_lock));
1806 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1807 lockdep_is_held(&table->tb6_lock));
1808 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1809 lockdep_is_held(&table->tb6_lock));
1810 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1811 lockdep_is_held(&table->tb6_lock));
1812 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1813 lockdep_is_held(&table->tb6_lock));
1814 struct fib6_info *new_fn_leaf;
1816 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1819 WARN_ON(fn->fn_flags & RTN_RTINFO);
1820 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1834 if (children == 3 || FIB6_SUBTREE(fn)
1835 #ifdef CONFIG_IPV6_SUBTREES
1836 /* Subtree root (i.e. fn) may have one child */
1837 || (children && fn->fn_flags & RTN_ROOT)
1840 new_fn_leaf = fib6_find_prefix(net, table, fn);
1843 WARN_ON(!new_fn_leaf);
1844 new_fn_leaf = net->ipv6.fib6_null_entry;
1847 fib6_info_hold(new_fn_leaf);
1848 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1852 #ifdef CONFIG_IPV6_SUBTREES
1853 if (FIB6_SUBTREE(pn) == fn) {
1854 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1855 RCU_INIT_POINTER(pn->subtree, NULL);
1858 WARN_ON(fn->fn_flags & RTN_ROOT);
1861 rcu_assign_pointer(pn->right, child);
1862 else if (pn_l == fn)
1863 rcu_assign_pointer(pn->left, child);
1869 rcu_assign_pointer(child->parent, pn);
1871 #ifdef CONFIG_IPV6_SUBTREES
1875 read_lock(&net->ipv6.fib6_walker_lock);
1876 FOR_WALKERS(net, w) {
1878 if (w->node == fn) {
1879 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1884 if (w->node == fn) {
1887 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1888 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1890 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1891 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1896 read_unlock(&net->ipv6.fib6_walker_lock);
1899 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1902 RCU_INIT_POINTER(pn->leaf, NULL);
1903 fib6_info_release(pn_leaf);
1908 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1909 struct fib6_info __rcu **rtp, struct nl_info *info)
1911 struct fib6_info *leaf, *replace_rt = NULL;
1912 struct fib6_walker *w;
1913 struct fib6_info *rt = rcu_dereference_protected(*rtp,
1914 lockdep_is_held(&table->tb6_lock));
1915 struct net *net = info->nl_net;
1916 bool notify_del = false;
1918 RT6_TRACE("fib6_del_route\n");
1920 /* If the deleted route is the first in the node and it is not part of
1921 * a multipath route, then we need to replace it with the next route
1922 * in the node, if exists.
1924 leaf = rcu_dereference_protected(fn->leaf,
1925 lockdep_is_held(&table->tb6_lock));
1926 if (leaf == rt && !rt->fib6_nsiblings) {
1927 if (rcu_access_pointer(rt->fib6_next))
1928 replace_rt = rcu_dereference_protected(rt->fib6_next,
1929 lockdep_is_held(&table->tb6_lock));
1935 *rtp = rt->fib6_next;
1936 rt->fib6_node = NULL;
1937 net->ipv6.rt6_stats->fib_rt_entries--;
1938 net->ipv6.rt6_stats->fib_discarded_routes++;
1940 /* Reset round-robin state, if necessary */
1941 if (rcu_access_pointer(fn->rr_ptr) == rt)
1944 /* Remove this entry from other siblings */
1945 if (rt->fib6_nsiblings) {
1946 struct fib6_info *sibling, *next_sibling;
1948 /* The route is deleted from a multipath route. If this
1949 * multipath route is the first route in the node, then we need
1950 * to emit a delete notification. Otherwise, we need to skip
1953 if (rt->fib6_metric == leaf->fib6_metric &&
1954 rt6_qualify_for_ecmp(leaf))
1956 list_for_each_entry_safe(sibling, next_sibling,
1957 &rt->fib6_siblings, fib6_siblings)
1958 sibling->fib6_nsiblings--;
1959 rt->fib6_nsiblings = 0;
1960 list_del_init(&rt->fib6_siblings);
1961 rt6_multipath_rebalance(next_sibling);
1964 /* Adjust walkers */
1965 read_lock(&net->ipv6.fib6_walker_lock);
1966 FOR_WALKERS(net, w) {
1967 if (w->state == FWS_C && w->leaf == rt) {
1968 RT6_TRACE("walker %p adjusted by delroute\n", w);
1969 w->leaf = rcu_dereference_protected(rt->fib6_next,
1970 lockdep_is_held(&table->tb6_lock));
1975 read_unlock(&net->ipv6.fib6_walker_lock);
1977 /* If it was last route, call fib6_repair_tree() to:
1978 * 1. For root node, put back null_entry as how the table was created.
1979 * 2. For other nodes, expunge its radix tree node.
1981 if (!rcu_access_pointer(fn->leaf)) {
1982 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1983 fn->fn_flags &= ~RTN_RTINFO;
1984 net->ipv6.rt6_stats->fib_route_nodes--;
1986 fn = fib6_repair_tree(net, table, fn);
1989 fib6_purge_rt(rt, fn, net);
1991 if (!info->skip_notify_kernel) {
1993 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL,
1995 else if (replace_rt)
1996 call_fib6_entry_notifiers_replace(net, replace_rt);
1998 if (!info->skip_notify)
1999 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
2001 fib6_info_release(rt);
2004 /* Need to own table->tb6_lock */
2005 int fib6_del(struct fib6_info *rt, struct nl_info *info)
2007 struct net *net = info->nl_net;
2008 struct fib6_info __rcu **rtp;
2009 struct fib6_info __rcu **rtp_next;
2010 struct fib6_table *table;
2011 struct fib6_node *fn;
2013 if (rt == net->ipv6.fib6_null_entry)
2016 table = rt->fib6_table;
2017 fn = rcu_dereference_protected(rt->fib6_node,
2018 lockdep_is_held(&table->tb6_lock));
2022 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
2025 * Walk the leaf entries looking for ourself
2028 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
2029 struct fib6_info *cur = rcu_dereference_protected(*rtp,
2030 lockdep_is_held(&table->tb6_lock));
2032 if (fib6_requires_src(cur))
2033 fib6_routes_require_src_dec(info->nl_net);
2034 fib6_del_route(table, fn, rtp, info);
2037 rtp_next = &cur->fib6_next;
2043 * Tree traversal function.
2045 * Certainly, it is not interrupt safe.
2046 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
2047 * It means, that we can modify tree during walking
2048 * and use this function for garbage collection, clone pruning,
2049 * cleaning tree when a device goes down etc. etc.
2051 * It guarantees that every node will be traversed,
2052 * and that it will be traversed only once.
2054 * Callback function w->func may return:
2055 * 0 -> continue walking.
2056 * positive value -> walking is suspended (used by tree dumps,
2057 * and probably by gc, if it will be split to several slices)
2058 * negative value -> terminate walking.
2060 * The function itself returns:
2061 * 0 -> walk is complete.
2062 * >0 -> walk is incomplete (i.e. suspended)
2063 * <0 -> walk is terminated by an error.
2065 * This function is called with tb6_lock held.
2068 static int fib6_walk_continue(struct fib6_walker *w)
2070 struct fib6_node *fn, *pn, *left, *right;
2072 /* w->root should always be table->tb6_root */
2073 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
2081 #ifdef CONFIG_IPV6_SUBTREES
2083 if (FIB6_SUBTREE(fn)) {
2084 w->node = FIB6_SUBTREE(fn);
2091 left = rcu_dereference_protected(fn->left, 1);
2094 w->state = FWS_INIT;
2100 right = rcu_dereference_protected(fn->right, 1);
2103 w->state = FWS_INIT;
2107 w->leaf = rcu_dereference_protected(fn->leaf, 1);
2110 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
2131 pn = rcu_dereference_protected(fn->parent, 1);
2132 left = rcu_dereference_protected(pn->left, 1);
2133 right = rcu_dereference_protected(pn->right, 1);
2135 #ifdef CONFIG_IPV6_SUBTREES
2136 if (FIB6_SUBTREE(pn) == fn) {
2137 WARN_ON(!(fn->fn_flags & RTN_ROOT));
2148 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
2158 static int fib6_walk(struct net *net, struct fib6_walker *w)
2162 w->state = FWS_INIT;
2165 fib6_walker_link(net, w);
2166 res = fib6_walk_continue(w);
2168 fib6_walker_unlink(net, w);
2172 static int fib6_clean_node(struct fib6_walker *w)
2175 struct fib6_info *rt;
2176 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
2177 struct nl_info info = {
2179 .skip_notify = c->skip_notify,
2182 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
2183 READ_ONCE(w->node->fn_sernum) != c->sernum)
2184 WRITE_ONCE(w->node->fn_sernum, c->sernum);
2187 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
2192 for_each_fib6_walker_rt(w) {
2193 res = c->func(rt, c->arg);
2196 res = fib6_del(rt, &info);
2199 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
2201 rcu_access_pointer(rt->fib6_node),
2207 } else if (res == -2) {
2208 if (WARN_ON(!rt->fib6_nsiblings))
2210 rt = list_last_entry(&rt->fib6_siblings,
2211 struct fib6_info, fib6_siblings);
2221 * Convenient frontend to tree walker.
2223 * func is called on each route.
2224 * It may return -2 -> skip multipath route.
2225 * -1 -> delete this route.
2226 * 0 -> continue walking
2229 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2230 int (*func)(struct fib6_info *, void *arg),
2231 int sernum, void *arg, bool skip_notify)
2233 struct fib6_cleaner c;
2236 c.w.func = fib6_clean_node;
2239 c.w.skip_in_node = 0;
2244 c.skip_notify = skip_notify;
2246 fib6_walk(net, &c.w);
2249 static void __fib6_clean_all(struct net *net,
2250 int (*func)(struct fib6_info *, void *),
2251 int sernum, void *arg, bool skip_notify)
2253 struct fib6_table *table;
2254 struct hlist_head *head;
2258 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2259 head = &net->ipv6.fib_table_hash[h];
2260 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2261 spin_lock_bh(&table->tb6_lock);
2262 fib6_clean_tree(net, &table->tb6_root,
2263 func, sernum, arg, skip_notify);
2264 spin_unlock_bh(&table->tb6_lock);
2270 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
2273 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2276 void fib6_clean_all_skip_notify(struct net *net,
2277 int (*func)(struct fib6_info *, void *),
2280 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
2283 static void fib6_flush_trees(struct net *net)
2285 int new_sernum = fib6_new_sernum(net);
2287 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
2291 * Garbage collection
2294 static int fib6_age(struct fib6_info *rt, struct fib6_gc_args *gc_args)
2296 unsigned long now = jiffies;
2299 * check addrconf expiration here.
2300 * Routes are expired even if they are in use.
2303 if (fib6_has_expires(rt) && rt->expires) {
2304 if (time_after(now, rt->expires)) {
2305 RT6_TRACE("expiring %p\n", rt);
2311 /* Also age clones in the exception table.
2312 * Note, that clones are aged out
2313 * only if they are not in use now.
2315 rt6_age_exceptions(rt, gc_args, now);
2320 static void fib6_gc_table(struct net *net,
2321 struct fib6_table *tb6,
2322 struct fib6_gc_args *gc_args)
2324 struct fib6_info *rt;
2325 struct hlist_node *n;
2326 struct nl_info info = {
2328 .skip_notify = false,
2331 hlist_for_each_entry_safe(rt, n, &tb6->tb6_gc_hlist, gc_link)
2332 if (fib6_age(rt, gc_args) == -1)
2333 fib6_del(rt, &info);
2336 static void fib6_gc_all(struct net *net, struct fib6_gc_args *gc_args)
2338 struct fib6_table *table;
2339 struct hlist_head *head;
2343 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2344 head = &net->ipv6.fib_table_hash[h];
2345 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2346 spin_lock_bh(&table->tb6_lock);
2347 fib6_gc_table(net, table, gc_args);
2348 spin_unlock_bh(&table->tb6_lock);
2354 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2356 struct fib6_gc_args gc_args;
2360 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2361 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2362 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2365 gc_args.timeout = expires ? (int)expires :
2366 net->ipv6.sysctl.ip6_rt_gc_interval;
2369 fib6_gc_all(net, &gc_args);
2371 net->ipv6.ip6_rt_last_gc = now;
2374 mod_timer(&net->ipv6.ip6_fib_timer,
2376 + net->ipv6.sysctl.ip6_rt_gc_interval));
2378 del_timer(&net->ipv6.ip6_fib_timer);
2379 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2382 static void fib6_gc_timer_cb(struct timer_list *t)
2384 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2386 fib6_run_gc(0, arg, true);
2389 static int __net_init fib6_net_init(struct net *net)
2391 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2394 err = fib6_notifier_init(net);
2398 /* Default to 3-tuple */
2399 net->ipv6.sysctl.multipath_hash_fields =
2400 FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK;
2402 spin_lock_init(&net->ipv6.fib6_gc_lock);
2403 rwlock_init(&net->ipv6.fib6_walker_lock);
2404 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2405 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2407 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2408 if (!net->ipv6.rt6_stats)
2411 /* Avoid false sharing : Use at least a full cache line */
2412 size = max_t(size_t, size, L1_CACHE_BYTES);
2414 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2415 if (!net->ipv6.fib_table_hash)
2418 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2420 if (!net->ipv6.fib6_main_tbl)
2421 goto out_fib_table_hash;
2423 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2424 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2425 net->ipv6.fib6_null_entry);
2426 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2427 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2428 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2430 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2431 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2433 if (!net->ipv6.fib6_local_tbl)
2434 goto out_fib6_main_tbl;
2435 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2436 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2437 net->ipv6.fib6_null_entry);
2438 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2439 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2440 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2442 fib6_tables_init(net);
2446 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2448 kfree(net->ipv6.fib6_main_tbl);
2451 kfree(net->ipv6.fib_table_hash);
2453 kfree(net->ipv6.rt6_stats);
2455 fib6_notifier_exit(net);
2459 static void fib6_net_exit(struct net *net)
2463 del_timer_sync(&net->ipv6.ip6_fib_timer);
2465 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2466 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2467 struct hlist_node *tmp;
2468 struct fib6_table *tb;
2470 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2471 hlist_del(&tb->tb6_hlist);
2472 fib6_free_table(tb);
2476 kfree(net->ipv6.fib_table_hash);
2477 kfree(net->ipv6.rt6_stats);
2478 fib6_notifier_exit(net);
2481 static struct pernet_operations fib6_net_ops = {
2482 .init = fib6_net_init,
2483 .exit = fib6_net_exit,
2486 int __init fib6_init(void)
2490 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2491 sizeof(struct fib6_node), 0,
2492 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
2494 if (!fib6_node_kmem)
2497 ret = register_pernet_subsys(&fib6_net_ops);
2499 goto out_kmem_cache_create;
2501 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2504 goto out_unregister_subsys;
2506 __fib6_flush_trees = fib6_flush_trees;
2510 out_unregister_subsys:
2511 unregister_pernet_subsys(&fib6_net_ops);
2512 out_kmem_cache_create:
2513 kmem_cache_destroy(fib6_node_kmem);
2517 void fib6_gc_cleanup(void)
2519 unregister_pernet_subsys(&fib6_net_ops);
2520 kmem_cache_destroy(fib6_node_kmem);
2523 #ifdef CONFIG_PROC_FS
2524 static int ipv6_route_native_seq_show(struct seq_file *seq, void *v)
2526 struct fib6_info *rt = v;
2527 struct ipv6_route_iter *iter = seq->private;
2528 struct fib6_nh *fib6_nh = rt->fib6_nh;
2529 unsigned int flags = rt->fib6_flags;
2530 const struct net_device *dev;
2533 fib6_nh = nexthop_fib6_nh(rt->nh);
2535 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
2537 #ifdef CONFIG_IPV6_SUBTREES
2538 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
2540 seq_puts(seq, "00000000000000000000000000000000 00 ");
2542 if (fib6_nh->fib_nh_gw_family) {
2543 flags |= RTF_GATEWAY;
2544 seq_printf(seq, "%pi6", &fib6_nh->fib_nh_gw6);
2546 seq_puts(seq, "00000000000000000000000000000000");
2549 dev = fib6_nh->fib_nh_dev;
2550 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2551 rt->fib6_metric, refcount_read(&rt->fib6_ref), 0,
2552 flags, dev ? dev->name : "");
2553 iter->w.leaf = NULL;
2557 static int ipv6_route_yield(struct fib6_walker *w)
2559 struct ipv6_route_iter *iter = w->args;
2565 iter->w.leaf = rcu_dereference_protected(
2566 iter->w.leaf->fib6_next,
2567 lockdep_is_held(&iter->tbl->tb6_lock));
2569 if (!iter->skip && iter->w.leaf)
2571 } while (iter->w.leaf);
2576 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2579 memset(&iter->w, 0, sizeof(iter->w));
2580 iter->w.func = ipv6_route_yield;
2581 iter->w.root = &iter->tbl->tb6_root;
2582 iter->w.state = FWS_INIT;
2583 iter->w.node = iter->w.root;
2584 iter->w.args = iter;
2585 iter->sernum = READ_ONCE(iter->w.root->fn_sernum);
2586 INIT_LIST_HEAD(&iter->w.lh);
2587 fib6_walker_link(net, &iter->w);
2590 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2594 struct hlist_node *node;
2597 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2598 node = rcu_dereference(hlist_next_rcu(&tbl->tb6_hlist));
2604 while (!node && h < FIB6_TABLE_HASHSZ) {
2605 node = rcu_dereference(
2606 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2608 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2611 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2613 int sernum = READ_ONCE(iter->w.root->fn_sernum);
2615 if (iter->sernum != sernum) {
2616 iter->sernum = sernum;
2617 iter->w.state = FWS_INIT;
2618 iter->w.node = iter->w.root;
2619 WARN_ON(iter->w.skip);
2620 iter->w.skip = iter->w.count;
2624 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2627 struct fib6_info *n;
2628 struct net *net = seq_file_net(seq);
2629 struct ipv6_route_iter *iter = seq->private;
2635 n = rcu_dereference(((struct fib6_info *)v)->fib6_next);
2640 ipv6_route_check_sernum(iter);
2641 spin_lock_bh(&iter->tbl->tb6_lock);
2642 r = fib6_walk_continue(&iter->w);
2643 spin_unlock_bh(&iter->tbl->tb6_lock);
2645 return iter->w.leaf;
2647 fib6_walker_unlink(net, &iter->w);
2650 fib6_walker_unlink(net, &iter->w);
2652 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2656 ipv6_route_seq_setup_walk(iter, net);
2660 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2663 struct net *net = seq_file_net(seq);
2664 struct ipv6_route_iter *iter = seq->private;
2667 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2673 ipv6_route_seq_setup_walk(iter, net);
2674 return ipv6_route_seq_next(seq, NULL, &p);
2680 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2682 struct fib6_walker *w = &iter->w;
2683 return w->node && !(w->state == FWS_U && w->node == w->root);
2686 static void ipv6_route_native_seq_stop(struct seq_file *seq, void *v)
2689 struct net *net = seq_file_net(seq);
2690 struct ipv6_route_iter *iter = seq->private;
2692 if (ipv6_route_iter_active(iter))
2693 fib6_walker_unlink(net, &iter->w);
2698 #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
2699 static int ipv6_route_prog_seq_show(struct bpf_prog *prog,
2700 struct bpf_iter_meta *meta,
2703 struct bpf_iter__ipv6_route ctx;
2707 return bpf_iter_run_prog(prog, &ctx);
2710 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2712 struct ipv6_route_iter *iter = seq->private;
2713 struct bpf_iter_meta meta;
2714 struct bpf_prog *prog;
2718 prog = bpf_iter_get_info(&meta, false);
2720 return ipv6_route_native_seq_show(seq, v);
2722 ret = ipv6_route_prog_seq_show(prog, &meta, v);
2723 iter->w.leaf = NULL;
2728 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2730 struct bpf_iter_meta meta;
2731 struct bpf_prog *prog;
2735 prog = bpf_iter_get_info(&meta, true);
2737 (void)ipv6_route_prog_seq_show(prog, &meta, v);
2740 ipv6_route_native_seq_stop(seq, v);
2743 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2745 return ipv6_route_native_seq_show(seq, v);
2748 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2750 ipv6_route_native_seq_stop(seq, v);
2754 const struct seq_operations ipv6_route_seq_ops = {
2755 .start = ipv6_route_seq_start,
2756 .next = ipv6_route_seq_next,
2757 .stop = ipv6_route_seq_stop,
2758 .show = ipv6_route_seq_show
2760 #endif /* CONFIG_PROC_FS */