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/errno.h>
19 #include <linux/types.h>
20 #include <linux/net.h>
21 #include <linux/route.h>
22 #include <linux/netdevice.h>
23 #include <linux/in6.h>
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/slab.h>
30 #include <net/ndisc.h>
31 #include <net/addrconf.h>
32 #include <net/lwtunnel.h>
33 #include <net/fib_notifier.h>
35 #include <net/ip6_fib.h>
36 #include <net/ip6_route.h>
38 static struct kmem_cache *fib6_node_kmem __read_mostly;
43 int (*func)(struct fib6_info *, void *arg);
49 #ifdef CONFIG_IPV6_SUBTREES
50 #define FWS_INIT FWS_S
52 #define FWS_INIT FWS_L
55 static struct fib6_info *fib6_find_prefix(struct net *net,
56 struct fib6_table *table,
57 struct fib6_node *fn);
58 static struct fib6_node *fib6_repair_tree(struct net *net,
59 struct fib6_table *table,
60 struct fib6_node *fn);
61 static int fib6_walk(struct net *net, struct fib6_walker *w);
62 static int fib6_walk_continue(struct fib6_walker *w);
65 * A routing update causes an increase of the serial number on the
66 * affected subtree. This allows for cached routes to be asynchronously
67 * tested when modifications are made to the destination cache as a
68 * result of redirects, path MTU changes, etc.
71 static void fib6_gc_timer_cb(struct timer_list *t);
73 #define FOR_WALKERS(net, w) \
74 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
76 static void fib6_walker_link(struct net *net, struct fib6_walker *w)
78 write_lock_bh(&net->ipv6.fib6_walker_lock);
79 list_add(&w->lh, &net->ipv6.fib6_walkers);
80 write_unlock_bh(&net->ipv6.fib6_walker_lock);
83 static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
85 write_lock_bh(&net->ipv6.fib6_walker_lock);
87 write_unlock_bh(&net->ipv6.fib6_walker_lock);
90 static int fib6_new_sernum(struct net *net)
95 old = atomic_read(&net->ipv6.fib6_sernum);
96 new = old < INT_MAX ? old + 1 : 1;
97 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
103 FIB6_NO_SERNUM_CHANGE = 0,
106 void fib6_update_sernum(struct net *net, struct fib6_info *f6i)
108 struct fib6_node *fn;
110 fn = rcu_dereference_protected(f6i->fib6_node,
111 lockdep_is_held(&f6i->fib6_table->tb6_lock));
113 fn->fn_sernum = fib6_new_sernum(net);
117 * Auxiliary address test functions for the radix tree.
119 * These assume a 32bit processor (although it will work on
126 #if defined(__LITTLE_ENDIAN)
127 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
129 # define BITOP_BE32_SWIZZLE 0
132 static __be32 addr_bit_set(const void *token, int fn_bit)
134 const __be32 *addr = token;
137 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
138 * is optimized version of
139 * htonl(1 << ((~fn_bit)&0x1F))
140 * See include/asm-generic/bitops/le.h.
142 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
146 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags)
148 struct fib6_info *f6i;
150 f6i = kzalloc(sizeof(*f6i), gfp_flags);
154 f6i->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
155 if (!f6i->rt6i_pcpu) {
160 INIT_LIST_HEAD(&f6i->fib6_siblings);
161 refcount_set(&f6i->fib6_ref, 1);
166 void fib6_info_destroy_rcu(struct rcu_head *head)
168 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
169 struct rt6_exception_bucket *bucket;
171 WARN_ON(f6i->fib6_node);
173 bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
176 if (f6i->rt6i_pcpu) {
179 for_each_possible_cpu(cpu) {
180 struct rt6_info **ppcpu_rt;
181 struct rt6_info *pcpu_rt;
183 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
186 dst_dev_put(&pcpu_rt->dst);
187 dst_release(&pcpu_rt->dst);
192 free_percpu(f6i->rt6i_pcpu);
195 fib6_nh_release(&f6i->fib6_nh);
197 ip_fib_metrics_put(f6i->fib6_metrics);
201 EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
203 static struct fib6_node *node_alloc(struct net *net)
205 struct fib6_node *fn;
207 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
209 net->ipv6.rt6_stats->fib_nodes++;
214 static void node_free_immediate(struct net *net, struct fib6_node *fn)
216 kmem_cache_free(fib6_node_kmem, fn);
217 net->ipv6.rt6_stats->fib_nodes--;
220 static void node_free_rcu(struct rcu_head *head)
222 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
224 kmem_cache_free(fib6_node_kmem, fn);
227 static void node_free(struct net *net, struct fib6_node *fn)
229 call_rcu(&fn->rcu, node_free_rcu);
230 net->ipv6.rt6_stats->fib_nodes--;
233 static void fib6_free_table(struct fib6_table *table)
235 inetpeer_invalidate_tree(&table->tb6_peers);
239 static void fib6_link_table(struct net *net, struct fib6_table *tb)
244 * Initialize table lock at a single place to give lockdep a key,
245 * tables aren't visible prior to being linked to the list.
247 spin_lock_init(&tb->tb6_lock);
248 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
251 * No protection necessary, this is the only list mutatation
252 * operation, tables never disappear once they exist.
254 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
257 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
259 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
261 struct fib6_table *table;
263 table = kzalloc(sizeof(*table), GFP_ATOMIC);
266 rcu_assign_pointer(table->tb6_root.leaf,
267 net->ipv6.fib6_null_entry);
268 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
269 inet_peer_base_init(&table->tb6_peers);
275 struct fib6_table *fib6_new_table(struct net *net, u32 id)
277 struct fib6_table *tb;
281 tb = fib6_get_table(net, id);
285 tb = fib6_alloc_table(net, id);
287 fib6_link_table(net, tb);
291 EXPORT_SYMBOL_GPL(fib6_new_table);
293 struct fib6_table *fib6_get_table(struct net *net, u32 id)
295 struct fib6_table *tb;
296 struct hlist_head *head;
301 h = id & (FIB6_TABLE_HASHSZ - 1);
303 head = &net->ipv6.fib_table_hash[h];
304 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
305 if (tb->tb6_id == id) {
314 EXPORT_SYMBOL_GPL(fib6_get_table);
316 static void __net_init fib6_tables_init(struct net *net)
318 fib6_link_table(net, net->ipv6.fib6_main_tbl);
319 fib6_link_table(net, net->ipv6.fib6_local_tbl);
323 struct fib6_table *fib6_new_table(struct net *net, u32 id)
325 return fib6_get_table(net, id);
328 struct fib6_table *fib6_get_table(struct net *net, u32 id)
330 return net->ipv6.fib6_main_tbl;
333 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
334 const struct sk_buff *skb,
335 int flags, pol_lookup_t lookup)
339 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
340 if (rt->dst.error == -EAGAIN) {
342 rt = net->ipv6.ip6_null_entry;
349 /* called with rcu lock held; no reference taken on fib6_info */
350 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
351 struct fib6_result *res, int flags)
353 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6,
357 static void __net_init fib6_tables_init(struct net *net)
359 fib6_link_table(net, net->ipv6.fib6_main_tbl);
364 unsigned int fib6_tables_seq_read(struct net *net)
366 unsigned int h, fib_seq = 0;
369 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
370 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
371 struct fib6_table *tb;
373 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
374 fib_seq += tb->fib_seq;
381 static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
382 enum fib_event_type event_type,
383 struct fib6_info *rt)
385 struct fib6_entry_notifier_info info = {
389 return call_fib6_notifier(nb, net, event_type, &info.info);
392 static int call_fib6_entry_notifiers(struct net *net,
393 enum fib_event_type event_type,
394 struct fib6_info *rt,
395 struct netlink_ext_ack *extack)
397 struct fib6_entry_notifier_info info = {
398 .info.extack = extack,
402 rt->fib6_table->fib_seq++;
403 return call_fib6_notifiers(net, event_type, &info.info);
406 struct fib6_dump_arg {
408 struct notifier_block *nb;
411 static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
413 if (rt == arg->net->ipv6.fib6_null_entry)
415 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
418 static int fib6_node_dump(struct fib6_walker *w)
420 struct fib6_info *rt;
422 for_each_fib6_walker_rt(w)
423 fib6_rt_dump(rt, w->args);
428 static void fib6_table_dump(struct net *net, struct fib6_table *tb,
429 struct fib6_walker *w)
431 w->root = &tb->tb6_root;
432 spin_lock_bh(&tb->tb6_lock);
434 spin_unlock_bh(&tb->tb6_lock);
437 /* Called with rcu_read_lock() */
438 int fib6_tables_dump(struct net *net, struct notifier_block *nb)
440 struct fib6_dump_arg arg;
441 struct fib6_walker *w;
444 w = kzalloc(sizeof(*w), GFP_ATOMIC);
448 w->func = fib6_node_dump;
453 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
454 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
455 struct fib6_table *tb;
457 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
458 fib6_table_dump(net, tb, w);
466 static int fib6_dump_node(struct fib6_walker *w)
469 struct fib6_info *rt;
471 for_each_fib6_walker_rt(w) {
472 res = rt6_dump_route(rt, w->args);
474 /* Frame is full, suspend walking */
479 /* Multipath routes are dumped in one route with the
480 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
481 * last sibling of this route (no need to dump the
482 * sibling routes again)
484 if (rt->fib6_nsiblings)
485 rt = list_last_entry(&rt->fib6_siblings,
493 static void fib6_dump_end(struct netlink_callback *cb)
495 struct net *net = sock_net(cb->skb->sk);
496 struct fib6_walker *w = (void *)cb->args[2];
501 fib6_walker_unlink(net, w);
506 cb->done = (void *)cb->args[3];
510 static int fib6_dump_done(struct netlink_callback *cb)
513 return cb->done ? cb->done(cb) : 0;
516 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
517 struct netlink_callback *cb)
519 struct net *net = sock_net(skb->sk);
520 struct fib6_walker *w;
523 w = (void *)cb->args[2];
524 w->root = &table->tb6_root;
526 if (cb->args[4] == 0) {
530 spin_lock_bh(&table->tb6_lock);
531 res = fib6_walk(net, w);
532 spin_unlock_bh(&table->tb6_lock);
535 cb->args[5] = w->root->fn_sernum;
538 if (cb->args[5] != w->root->fn_sernum) {
539 /* Begin at the root if the tree changed */
540 cb->args[5] = w->root->fn_sernum;
547 spin_lock_bh(&table->tb6_lock);
548 res = fib6_walk_continue(w);
549 spin_unlock_bh(&table->tb6_lock);
551 fib6_walker_unlink(net, w);
559 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
561 const struct nlmsghdr *nlh = cb->nlh;
562 struct net *net = sock_net(skb->sk);
563 struct rt6_rtnl_dump_arg arg = {};
565 unsigned int e = 0, s_e;
566 struct fib6_walker *w;
567 struct fib6_table *tb;
568 struct hlist_head *head;
571 if (cb->strict_check) {
574 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
577 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
578 struct rtmsg *rtm = nlmsg_data(nlh);
580 arg.filter.flags = rtm->rtm_flags & (RTM_F_PREFIX|RTM_F_CLONED);
583 /* fib entries are never clones */
584 if (arg.filter.flags & RTM_F_CLONED)
587 w = (void *)cb->args[2];
591 * 1. hook callback destructor.
593 cb->args[3] = (long)cb->done;
594 cb->done = fib6_dump_done;
597 * 2. allocate and initialize walker.
599 w = kzalloc(sizeof(*w), GFP_ATOMIC);
602 w->func = fib6_dump_node;
603 cb->args[2] = (long)w;
611 if (arg.filter.table_id) {
612 tb = fib6_get_table(net, arg.filter.table_id);
614 if (arg.filter.dump_all_families)
617 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
622 res = fib6_dump_table(tb, skb, cb);
633 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
635 head = &net->ipv6.fib_table_hash[h];
636 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
639 res = fib6_dump_table(tb, skb, cb);
651 res = res < 0 ? res : skb->len;
657 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
662 if (f6i->fib6_metrics == &dst_default_metrics) {
663 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
668 refcount_set(&p->refcnt, 1);
669 f6i->fib6_metrics = p;
672 f6i->fib6_metrics->metrics[metric - 1] = val;
678 * return the appropriate node for a routing tree "add" operation
679 * by either creating and inserting or by returning an existing
683 static struct fib6_node *fib6_add_1(struct net *net,
684 struct fib6_table *table,
685 struct fib6_node *root,
686 struct in6_addr *addr, int plen,
687 int offset, int allow_create,
688 int replace_required,
689 struct netlink_ext_ack *extack)
691 struct fib6_node *fn, *in, *ln;
692 struct fib6_node *pn = NULL;
697 RT6_TRACE("fib6_add_1\n");
699 /* insert node in tree */
704 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
705 lockdep_is_held(&table->tb6_lock));
706 key = (struct rt6key *)((u8 *)leaf + offset);
711 if (plen < fn->fn_bit ||
712 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
714 if (replace_required) {
715 NL_SET_ERR_MSG(extack,
716 "Can not replace route - no match found");
717 pr_warn("Can't replace route, no match found\n");
718 return ERR_PTR(-ENOENT);
720 pr_warn("NLM_F_CREATE should be set when creating new route\n");
729 if (plen == fn->fn_bit) {
730 /* clean up an intermediate node */
731 if (!(fn->fn_flags & RTN_RTINFO)) {
732 RCU_INIT_POINTER(fn->leaf, NULL);
733 fib6_info_release(leaf);
734 /* remove null_entry in the root node */
735 } else if (fn->fn_flags & RTN_TL_ROOT &&
736 rcu_access_pointer(fn->leaf) ==
737 net->ipv6.fib6_null_entry) {
738 RCU_INIT_POINTER(fn->leaf, NULL);
745 * We have more bits to go
748 /* Try to walk down on tree. */
749 dir = addr_bit_set(addr, fn->fn_bit);
752 rcu_dereference_protected(fn->right,
753 lockdep_is_held(&table->tb6_lock)) :
754 rcu_dereference_protected(fn->left,
755 lockdep_is_held(&table->tb6_lock));
759 /* We should not create new node because
760 * NLM_F_REPLACE was specified without NLM_F_CREATE
761 * I assume it is safe to require NLM_F_CREATE when
762 * REPLACE flag is used! Later we may want to remove the
763 * check for replace_required, because according
764 * to netlink specification, NLM_F_CREATE
765 * MUST be specified if new route is created.
766 * That would keep IPv6 consistent with IPv4
768 if (replace_required) {
769 NL_SET_ERR_MSG(extack,
770 "Can not replace route - no match found");
771 pr_warn("Can't replace route, no match found\n");
772 return ERR_PTR(-ENOENT);
774 pr_warn("NLM_F_CREATE should be set when creating new route\n");
777 * We walked to the bottom of tree.
778 * Create new leaf node without children.
781 ln = node_alloc(net);
784 return ERR_PTR(-ENOMEM);
786 RCU_INIT_POINTER(ln->parent, pn);
789 rcu_assign_pointer(pn->right, ln);
791 rcu_assign_pointer(pn->left, ln);
798 * split since we don't have a common prefix anymore or
799 * we have a less significant route.
800 * we've to insert an intermediate node on the list
801 * this new node will point to the one we need to create
805 pn = rcu_dereference_protected(fn->parent,
806 lockdep_is_held(&table->tb6_lock));
808 /* find 1st bit in difference between the 2 addrs.
810 See comment in __ipv6_addr_diff: bit may be an invalid value,
811 but if it is >= plen, the value is ignored in any case.
814 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
819 * (new leaf node)[ln] (old node)[fn]
822 in = node_alloc(net);
823 ln = node_alloc(net);
827 node_free_immediate(net, in);
829 node_free_immediate(net, ln);
830 return ERR_PTR(-ENOMEM);
834 * new intermediate node.
836 * be off since that an address that chooses one of
837 * the branches would not match less specific routes
838 * in the other branch
843 RCU_INIT_POINTER(in->parent, pn);
845 fib6_info_hold(rcu_dereference_protected(in->leaf,
846 lockdep_is_held(&table->tb6_lock)));
848 /* update parent pointer */
850 rcu_assign_pointer(pn->right, in);
852 rcu_assign_pointer(pn->left, in);
856 RCU_INIT_POINTER(ln->parent, in);
857 rcu_assign_pointer(fn->parent, in);
859 if (addr_bit_set(addr, bit)) {
860 rcu_assign_pointer(in->right, ln);
861 rcu_assign_pointer(in->left, fn);
863 rcu_assign_pointer(in->left, ln);
864 rcu_assign_pointer(in->right, fn);
866 } else { /* plen <= bit */
869 * (new leaf node)[ln]
871 * (old node)[fn] NULL
874 ln = node_alloc(net);
877 return ERR_PTR(-ENOMEM);
881 RCU_INIT_POINTER(ln->parent, pn);
883 if (addr_bit_set(&key->addr, plen))
884 RCU_INIT_POINTER(ln->right, fn);
886 RCU_INIT_POINTER(ln->left, fn);
888 rcu_assign_pointer(fn->parent, ln);
891 rcu_assign_pointer(pn->right, ln);
893 rcu_assign_pointer(pn->left, ln);
898 static void fib6_drop_pcpu_from(struct fib6_info *f6i,
899 const struct fib6_table *table)
903 /* Make sure rt6_make_pcpu_route() wont add other percpu routes
904 * while we are cleaning them here.
906 f6i->fib6_destroying = 1;
907 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
909 /* release the reference to this fib entry from
910 * all of its cached pcpu routes
912 for_each_possible_cpu(cpu) {
913 struct rt6_info **ppcpu_rt;
914 struct rt6_info *pcpu_rt;
916 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
919 struct fib6_info *from;
921 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
922 fib6_info_release(from);
927 static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
930 struct fib6_table *table = rt->fib6_table;
933 fib6_drop_pcpu_from(rt, table);
935 if (refcount_read(&rt->fib6_ref) != 1) {
936 /* This route is used as dummy address holder in some split
937 * nodes. It is not leaked, but it still holds other resources,
938 * which must be released in time. So, scan ascendant nodes
939 * and replace dummy references to this route with references
940 * to still alive ones.
943 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
944 lockdep_is_held(&table->tb6_lock));
945 struct fib6_info *new_leaf;
946 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
947 new_leaf = fib6_find_prefix(net, table, fn);
948 fib6_info_hold(new_leaf);
950 rcu_assign_pointer(fn->leaf, new_leaf);
951 fib6_info_release(rt);
953 fn = rcu_dereference_protected(fn->parent,
954 lockdep_is_held(&table->tb6_lock));
960 * Insert routing information in a node.
963 static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
964 struct nl_info *info,
965 struct netlink_ext_ack *extack)
967 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
968 lockdep_is_held(&rt->fib6_table->tb6_lock));
969 struct fib6_info *iter = NULL;
970 struct fib6_info __rcu **ins;
971 struct fib6_info __rcu **fallback_ins = NULL;
972 int replace = (info->nlh &&
973 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
974 int add = (!info->nlh ||
975 (info->nlh->nlmsg_flags & NLM_F_CREATE));
977 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
978 u16 nlflags = NLM_F_EXCL;
981 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
982 nlflags |= NLM_F_APPEND;
986 for (iter = leaf; iter;
987 iter = rcu_dereference_protected(iter->fib6_next,
988 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
990 * Search for duplicates
993 if (iter->fib6_metric == rt->fib6_metric) {
995 * Same priority level
998 (info->nlh->nlmsg_flags & NLM_F_EXCL))
1001 nlflags &= ~NLM_F_EXCL;
1003 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
1008 fallback_ins = fallback_ins ?: ins;
1012 if (rt6_duplicate_nexthop(iter, rt)) {
1013 if (rt->fib6_nsiblings)
1014 rt->fib6_nsiblings = 0;
1015 if (!(iter->fib6_flags & RTF_EXPIRES))
1017 if (!(rt->fib6_flags & RTF_EXPIRES))
1018 fib6_clean_expires(iter);
1020 fib6_set_expires(iter, rt->expires);
1023 fib6_metric_set(iter, RTAX_MTU,
1027 /* If we have the same destination and the same metric,
1028 * but not the same gateway, then the route we try to
1029 * add is sibling to this route, increment our counter
1030 * of siblings, and later we will add our route to the
1032 * Only static routes (which don't have flag
1033 * RTF_EXPIRES) are used for ECMPv6.
1035 * To avoid long list, we only had siblings if the
1036 * route have a gateway.
1039 rt6_qualify_for_ecmp(iter))
1040 rt->fib6_nsiblings++;
1043 if (iter->fib6_metric > rt->fib6_metric)
1047 ins = &iter->fib6_next;
1050 if (fallback_ins && !found) {
1051 /* No ECMP-able route found, replace first non-ECMP one */
1053 iter = rcu_dereference_protected(*ins,
1054 lockdep_is_held(&rt->fib6_table->tb6_lock));
1058 /* Reset round-robin state, if necessary */
1059 if (ins == &fn->leaf)
1062 /* Link this route to others same route. */
1063 if (rt->fib6_nsiblings) {
1064 unsigned int fib6_nsiblings;
1065 struct fib6_info *sibling, *temp_sibling;
1067 /* Find the first route that have the same metric */
1070 if (sibling->fib6_metric == rt->fib6_metric &&
1071 rt6_qualify_for_ecmp(sibling)) {
1072 list_add_tail(&rt->fib6_siblings,
1073 &sibling->fib6_siblings);
1076 sibling = rcu_dereference_protected(sibling->fib6_next,
1077 lockdep_is_held(&rt->fib6_table->tb6_lock));
1079 /* For each sibling in the list, increment the counter of
1080 * siblings. BUG() if counters does not match, list of siblings
1084 list_for_each_entry_safe(sibling, temp_sibling,
1085 &rt->fib6_siblings, fib6_siblings) {
1086 sibling->fib6_nsiblings++;
1087 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1090 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1091 rt6_multipath_rebalance(temp_sibling);
1099 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1102 nlflags |= NLM_F_CREATE;
1104 err = call_fib6_entry_notifiers(info->nl_net,
1105 FIB_EVENT_ENTRY_ADD,
1110 rcu_assign_pointer(rt->fib6_next, iter);
1112 rcu_assign_pointer(rt->fib6_node, fn);
1113 rcu_assign_pointer(*ins, rt);
1114 if (!info->skip_notify)
1115 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1116 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1118 if (!(fn->fn_flags & RTN_RTINFO)) {
1119 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1120 fn->fn_flags |= RTN_RTINFO;
1129 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1133 err = call_fib6_entry_notifiers(info->nl_net,
1134 FIB_EVENT_ENTRY_REPLACE,
1140 rcu_assign_pointer(rt->fib6_node, fn);
1141 rt->fib6_next = iter->fib6_next;
1142 rcu_assign_pointer(*ins, rt);
1143 if (!info->skip_notify)
1144 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1145 if (!(fn->fn_flags & RTN_RTINFO)) {
1146 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1147 fn->fn_flags |= RTN_RTINFO;
1149 nsiblings = iter->fib6_nsiblings;
1150 iter->fib6_node = NULL;
1151 fib6_purge_rt(iter, fn, info->nl_net);
1152 if (rcu_access_pointer(fn->rr_ptr) == iter)
1154 fib6_info_release(iter);
1157 /* Replacing an ECMP route, remove all siblings */
1158 ins = &rt->fib6_next;
1159 iter = rcu_dereference_protected(*ins,
1160 lockdep_is_held(&rt->fib6_table->tb6_lock));
1162 if (iter->fib6_metric > rt->fib6_metric)
1164 if (rt6_qualify_for_ecmp(iter)) {
1165 *ins = iter->fib6_next;
1166 iter->fib6_node = NULL;
1167 fib6_purge_rt(iter, fn, info->nl_net);
1168 if (rcu_access_pointer(fn->rr_ptr) == iter)
1170 fib6_info_release(iter);
1172 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1174 ins = &iter->fib6_next;
1176 iter = rcu_dereference_protected(*ins,
1177 lockdep_is_held(&rt->fib6_table->tb6_lock));
1179 WARN_ON(nsiblings != 0);
1186 static void fib6_start_gc(struct net *net, struct fib6_info *rt)
1188 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1189 (rt->fib6_flags & RTF_EXPIRES))
1190 mod_timer(&net->ipv6.ip6_fib_timer,
1191 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1194 void fib6_force_start_gc(struct net *net)
1196 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1197 mod_timer(&net->ipv6.ip6_fib_timer,
1198 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1201 static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
1204 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1205 lockdep_is_held(&rt->fib6_table->tb6_lock));
1207 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1210 fn->fn_sernum = sernum;
1211 fn = rcu_dereference_protected(fn->parent,
1212 lockdep_is_held(&rt->fib6_table->tb6_lock));
1216 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
1218 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1222 * Add routing information to the routing tree.
1223 * <destination addr>/<source addr>
1224 * with source addr info in sub-trees
1225 * Need to own table->tb6_lock
1228 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
1229 struct nl_info *info, struct netlink_ext_ack *extack)
1231 struct fib6_table *table = rt->fib6_table;
1232 struct fib6_node *fn, *pn = NULL;
1234 int allow_create = 1;
1235 int replace_required = 0;
1236 int sernum = fib6_new_sernum(info->nl_net);
1239 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1241 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1242 replace_required = 1;
1244 if (!allow_create && !replace_required)
1245 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1247 fn = fib6_add_1(info->nl_net, table, root,
1248 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1249 offsetof(struct fib6_info, fib6_dst), allow_create,
1250 replace_required, extack);
1259 #ifdef CONFIG_IPV6_SUBTREES
1260 if (rt->fib6_src.plen) {
1261 struct fib6_node *sn;
1263 if (!rcu_access_pointer(fn->subtree)) {
1264 struct fib6_node *sfn;
1276 /* Create subtree root node */
1277 sfn = node_alloc(info->nl_net);
1281 fib6_info_hold(info->nl_net->ipv6.fib6_null_entry);
1282 rcu_assign_pointer(sfn->leaf,
1283 info->nl_net->ipv6.fib6_null_entry);
1284 sfn->fn_flags = RTN_ROOT;
1286 /* Now add the first leaf node to new subtree */
1288 sn = fib6_add_1(info->nl_net, table, sfn,
1289 &rt->fib6_src.addr, rt->fib6_src.plen,
1290 offsetof(struct fib6_info, fib6_src),
1291 allow_create, replace_required, extack);
1294 /* If it is failed, discard just allocated
1295 root, and then (in failure) stale node
1298 node_free_immediate(info->nl_net, sfn);
1303 /* Now link new subtree to main tree */
1304 rcu_assign_pointer(sfn->parent, fn);
1305 rcu_assign_pointer(fn->subtree, sfn);
1307 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1308 &rt->fib6_src.addr, rt->fib6_src.plen,
1309 offsetof(struct fib6_info, fib6_src),
1310 allow_create, replace_required, extack);
1318 if (!rcu_access_pointer(fn->leaf)) {
1319 if (fn->fn_flags & RTN_TL_ROOT) {
1320 /* put back null_entry for root node */
1321 rcu_assign_pointer(fn->leaf,
1322 info->nl_net->ipv6.fib6_null_entry);
1325 rcu_assign_pointer(fn->leaf, rt);
1332 err = fib6_add_rt2node(fn, rt, info, extack);
1334 __fib6_update_sernum_upto_root(rt, sernum);
1335 fib6_start_gc(info->nl_net, rt);
1340 #ifdef CONFIG_IPV6_SUBTREES
1342 * If fib6_add_1 has cleared the old leaf pointer in the
1343 * super-tree leaf node we have to find a new one for it.
1346 struct fib6_info *pn_leaf =
1347 rcu_dereference_protected(pn->leaf,
1348 lockdep_is_held(&table->tb6_lock));
1349 if (pn_leaf == rt) {
1351 RCU_INIT_POINTER(pn->leaf, NULL);
1352 fib6_info_release(rt);
1354 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1355 pn_leaf = fib6_find_prefix(info->nl_net, table,
1361 info->nl_net->ipv6.fib6_null_entry;
1364 fib6_info_hold(pn_leaf);
1365 rcu_assign_pointer(pn->leaf, pn_leaf);
1374 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1375 * 1. fn is an intermediate node and we failed to add the new
1376 * route to it in both subtree creation failure and fib6_add_rt2node()
1378 * 2. fn is the root node in the table and we fail to add the first
1379 * default route to it.
1382 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1383 (fn->fn_flags & RTN_TL_ROOT &&
1384 !rcu_access_pointer(fn->leaf))))
1385 fib6_repair_tree(info->nl_net, table, fn);
1390 * Routing tree lookup
1394 struct lookup_args {
1395 int offset; /* key offset on fib6_info */
1396 const struct in6_addr *addr; /* search key */
1399 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1400 struct lookup_args *args)
1402 struct fib6_node *fn;
1405 if (unlikely(args->offset == 0))
1415 struct fib6_node *next;
1417 dir = addr_bit_set(args->addr, fn->fn_bit);
1419 next = dir ? rcu_dereference(fn->right) :
1420 rcu_dereference(fn->left);
1430 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1432 if (subtree || fn->fn_flags & RTN_RTINFO) {
1433 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1439 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1441 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1442 #ifdef CONFIG_IPV6_SUBTREES
1444 struct fib6_node *sfn;
1445 sfn = fib6_node_lookup_1(subtree,
1452 if (fn->fn_flags & RTN_RTINFO)
1457 if (fn->fn_flags & RTN_ROOT)
1460 fn = rcu_dereference(fn->parent);
1466 /* called with rcu_read_lock() held
1468 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1469 const struct in6_addr *daddr,
1470 const struct in6_addr *saddr)
1472 struct fib6_node *fn;
1473 struct lookup_args args[] = {
1475 .offset = offsetof(struct fib6_info, fib6_dst),
1478 #ifdef CONFIG_IPV6_SUBTREES
1480 .offset = offsetof(struct fib6_info, fib6_src),
1485 .offset = 0, /* sentinel */
1489 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
1490 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1497 * Get node with specified destination prefix (and source prefix,
1498 * if subtrees are used)
1499 * exact_match == true means we try to find fn with exact match of
1500 * the passed in prefix addr
1501 * exact_match == false means we try to find fn with longest prefix
1502 * match of the passed in prefix addr. This is useful for finding fn
1503 * for cached route as it will be stored in the exception table under
1504 * the node with longest prefix length.
1508 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1509 const struct in6_addr *addr,
1510 int plen, int offset,
1513 struct fib6_node *fn, *prev = NULL;
1515 for (fn = root; fn ; ) {
1516 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1519 /* This node is being deleted */
1521 if (plen <= fn->fn_bit)
1527 key = (struct rt6key *)((u8 *)leaf + offset);
1532 if (plen < fn->fn_bit ||
1533 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1536 if (plen == fn->fn_bit)
1543 * We have more bits to go
1545 if (addr_bit_set(addr, fn->fn_bit))
1546 fn = rcu_dereference(fn->right);
1548 fn = rcu_dereference(fn->left);
1557 struct fib6_node *fib6_locate(struct fib6_node *root,
1558 const struct in6_addr *daddr, int dst_len,
1559 const struct in6_addr *saddr, int src_len,
1562 struct fib6_node *fn;
1564 fn = fib6_locate_1(root, daddr, dst_len,
1565 offsetof(struct fib6_info, fib6_dst),
1568 #ifdef CONFIG_IPV6_SUBTREES
1570 WARN_ON(saddr == NULL);
1572 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1575 fn = fib6_locate_1(subtree, saddr, src_len,
1576 offsetof(struct fib6_info, fib6_src),
1583 if (fn && fn->fn_flags & RTN_RTINFO)
1595 static struct fib6_info *fib6_find_prefix(struct net *net,
1596 struct fib6_table *table,
1597 struct fib6_node *fn)
1599 struct fib6_node *child_left, *child_right;
1601 if (fn->fn_flags & RTN_ROOT)
1602 return net->ipv6.fib6_null_entry;
1605 child_left = rcu_dereference_protected(fn->left,
1606 lockdep_is_held(&table->tb6_lock));
1607 child_right = rcu_dereference_protected(fn->right,
1608 lockdep_is_held(&table->tb6_lock));
1610 return rcu_dereference_protected(child_left->leaf,
1611 lockdep_is_held(&table->tb6_lock));
1613 return rcu_dereference_protected(child_right->leaf,
1614 lockdep_is_held(&table->tb6_lock));
1616 fn = FIB6_SUBTREE(fn);
1622 * Called to trim the tree of intermediate nodes when possible. "fn"
1623 * is the node we want to try and remove.
1624 * Need to own table->tb6_lock
1627 static struct fib6_node *fib6_repair_tree(struct net *net,
1628 struct fib6_table *table,
1629 struct fib6_node *fn)
1633 struct fib6_node *child;
1634 struct fib6_walker *w;
1637 /* Set fn->leaf to null_entry for root node. */
1638 if (fn->fn_flags & RTN_TL_ROOT) {
1639 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1644 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1645 lockdep_is_held(&table->tb6_lock));
1646 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1647 lockdep_is_held(&table->tb6_lock));
1648 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1649 lockdep_is_held(&table->tb6_lock));
1650 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1651 lockdep_is_held(&table->tb6_lock));
1652 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1653 lockdep_is_held(&table->tb6_lock));
1654 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1655 lockdep_is_held(&table->tb6_lock));
1656 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1657 lockdep_is_held(&table->tb6_lock));
1658 struct fib6_info *new_fn_leaf;
1660 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1663 WARN_ON(fn->fn_flags & RTN_RTINFO);
1664 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1670 child = fn_r, children |= 1;
1672 child = fn_l, children |= 2;
1674 if (children == 3 || FIB6_SUBTREE(fn)
1675 #ifdef CONFIG_IPV6_SUBTREES
1676 /* Subtree root (i.e. fn) may have one child */
1677 || (children && fn->fn_flags & RTN_ROOT)
1680 new_fn_leaf = fib6_find_prefix(net, table, fn);
1683 WARN_ON(!new_fn_leaf);
1684 new_fn_leaf = net->ipv6.fib6_null_entry;
1687 fib6_info_hold(new_fn_leaf);
1688 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1692 #ifdef CONFIG_IPV6_SUBTREES
1693 if (FIB6_SUBTREE(pn) == fn) {
1694 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1695 RCU_INIT_POINTER(pn->subtree, NULL);
1698 WARN_ON(fn->fn_flags & RTN_ROOT);
1701 rcu_assign_pointer(pn->right, child);
1702 else if (pn_l == fn)
1703 rcu_assign_pointer(pn->left, child);
1709 rcu_assign_pointer(child->parent, pn);
1711 #ifdef CONFIG_IPV6_SUBTREES
1715 read_lock(&net->ipv6.fib6_walker_lock);
1716 FOR_WALKERS(net, w) {
1718 if (w->node == fn) {
1719 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1724 if (w->node == fn) {
1727 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1728 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1730 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1731 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1736 read_unlock(&net->ipv6.fib6_walker_lock);
1739 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1742 RCU_INIT_POINTER(pn->leaf, NULL);
1743 fib6_info_release(pn_leaf);
1748 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1749 struct fib6_info __rcu **rtp, struct nl_info *info)
1751 struct fib6_walker *w;
1752 struct fib6_info *rt = rcu_dereference_protected(*rtp,
1753 lockdep_is_held(&table->tb6_lock));
1754 struct net *net = info->nl_net;
1756 RT6_TRACE("fib6_del_route\n");
1759 *rtp = rt->fib6_next;
1760 rt->fib6_node = NULL;
1761 net->ipv6.rt6_stats->fib_rt_entries--;
1762 net->ipv6.rt6_stats->fib_discarded_routes++;
1764 /* Flush all cached dst in exception table */
1765 rt6_flush_exceptions(rt);
1767 /* Reset round-robin state, if necessary */
1768 if (rcu_access_pointer(fn->rr_ptr) == rt)
1771 /* Remove this entry from other siblings */
1772 if (rt->fib6_nsiblings) {
1773 struct fib6_info *sibling, *next_sibling;
1775 list_for_each_entry_safe(sibling, next_sibling,
1776 &rt->fib6_siblings, fib6_siblings)
1777 sibling->fib6_nsiblings--;
1778 rt->fib6_nsiblings = 0;
1779 list_del_init(&rt->fib6_siblings);
1780 rt6_multipath_rebalance(next_sibling);
1783 /* Adjust walkers */
1784 read_lock(&net->ipv6.fib6_walker_lock);
1785 FOR_WALKERS(net, w) {
1786 if (w->state == FWS_C && w->leaf == rt) {
1787 RT6_TRACE("walker %p adjusted by delroute\n", w);
1788 w->leaf = rcu_dereference_protected(rt->fib6_next,
1789 lockdep_is_held(&table->tb6_lock));
1794 read_unlock(&net->ipv6.fib6_walker_lock);
1796 /* If it was last route, call fib6_repair_tree() to:
1797 * 1. For root node, put back null_entry as how the table was created.
1798 * 2. For other nodes, expunge its radix tree node.
1800 if (!rcu_access_pointer(fn->leaf)) {
1801 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1802 fn->fn_flags &= ~RTN_RTINFO;
1803 net->ipv6.rt6_stats->fib_route_nodes--;
1805 fn = fib6_repair_tree(net, table, fn);
1808 fib6_purge_rt(rt, fn, net);
1810 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
1811 if (!info->skip_notify)
1812 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
1813 fib6_info_release(rt);
1816 /* Need to own table->tb6_lock */
1817 int fib6_del(struct fib6_info *rt, struct nl_info *info)
1819 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1820 lockdep_is_held(&rt->fib6_table->tb6_lock));
1821 struct fib6_table *table = rt->fib6_table;
1822 struct net *net = info->nl_net;
1823 struct fib6_info __rcu **rtp;
1824 struct fib6_info __rcu **rtp_next;
1826 if (!fn || rt == net->ipv6.fib6_null_entry)
1829 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1832 * Walk the leaf entries looking for ourself
1835 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1836 struct fib6_info *cur = rcu_dereference_protected(*rtp,
1837 lockdep_is_held(&table->tb6_lock));
1839 fib6_del_route(table, fn, rtp, info);
1842 rtp_next = &cur->fib6_next;
1848 * Tree traversal function.
1850 * Certainly, it is not interrupt safe.
1851 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1852 * It means, that we can modify tree during walking
1853 * and use this function for garbage collection, clone pruning,
1854 * cleaning tree when a device goes down etc. etc.
1856 * It guarantees that every node will be traversed,
1857 * and that it will be traversed only once.
1859 * Callback function w->func may return:
1860 * 0 -> continue walking.
1861 * positive value -> walking is suspended (used by tree dumps,
1862 * and probably by gc, if it will be split to several slices)
1863 * negative value -> terminate walking.
1865 * The function itself returns:
1866 * 0 -> walk is complete.
1867 * >0 -> walk is incomplete (i.e. suspended)
1868 * <0 -> walk is terminated by an error.
1870 * This function is called with tb6_lock held.
1873 static int fib6_walk_continue(struct fib6_walker *w)
1875 struct fib6_node *fn, *pn, *left, *right;
1877 /* w->root should always be table->tb6_root */
1878 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1886 #ifdef CONFIG_IPV6_SUBTREES
1888 if (FIB6_SUBTREE(fn)) {
1889 w->node = FIB6_SUBTREE(fn);
1896 left = rcu_dereference_protected(fn->left, 1);
1899 w->state = FWS_INIT;
1905 right = rcu_dereference_protected(fn->right, 1);
1908 w->state = FWS_INIT;
1912 w->leaf = rcu_dereference_protected(fn->leaf, 1);
1915 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1936 pn = rcu_dereference_protected(fn->parent, 1);
1937 left = rcu_dereference_protected(pn->left, 1);
1938 right = rcu_dereference_protected(pn->right, 1);
1940 #ifdef CONFIG_IPV6_SUBTREES
1941 if (FIB6_SUBTREE(pn) == fn) {
1942 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1953 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
1963 static int fib6_walk(struct net *net, struct fib6_walker *w)
1967 w->state = FWS_INIT;
1970 fib6_walker_link(net, w);
1971 res = fib6_walk_continue(w);
1973 fib6_walker_unlink(net, w);
1977 static int fib6_clean_node(struct fib6_walker *w)
1980 struct fib6_info *rt;
1981 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1982 struct nl_info info = {
1984 .skip_notify = c->skip_notify,
1987 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1988 w->node->fn_sernum != c->sernum)
1989 w->node->fn_sernum = c->sernum;
1992 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1997 for_each_fib6_walker_rt(w) {
1998 res = c->func(rt, c->arg);
2001 res = fib6_del(rt, &info);
2004 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
2006 rcu_access_pointer(rt->fib6_node),
2012 } else if (res == -2) {
2013 if (WARN_ON(!rt->fib6_nsiblings))
2015 rt = list_last_entry(&rt->fib6_siblings,
2016 struct fib6_info, fib6_siblings);
2026 * Convenient frontend to tree walker.
2028 * func is called on each route.
2029 * It may return -2 -> skip multipath route.
2030 * -1 -> delete this route.
2031 * 0 -> continue walking
2034 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2035 int (*func)(struct fib6_info *, void *arg),
2036 int sernum, void *arg, bool skip_notify)
2038 struct fib6_cleaner c;
2041 c.w.func = fib6_clean_node;
2048 c.skip_notify = skip_notify;
2050 fib6_walk(net, &c.w);
2053 static void __fib6_clean_all(struct net *net,
2054 int (*func)(struct fib6_info *, void *),
2055 int sernum, void *arg, bool skip_notify)
2057 struct fib6_table *table;
2058 struct hlist_head *head;
2062 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2063 head = &net->ipv6.fib_table_hash[h];
2064 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2065 spin_lock_bh(&table->tb6_lock);
2066 fib6_clean_tree(net, &table->tb6_root,
2067 func, sernum, arg, skip_notify);
2068 spin_unlock_bh(&table->tb6_lock);
2074 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
2077 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2080 void fib6_clean_all_skip_notify(struct net *net,
2081 int (*func)(struct fib6_info *, void *),
2084 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
2087 static void fib6_flush_trees(struct net *net)
2089 int new_sernum = fib6_new_sernum(net);
2091 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
2095 * Garbage collection
2098 static int fib6_age(struct fib6_info *rt, void *arg)
2100 struct fib6_gc_args *gc_args = arg;
2101 unsigned long now = jiffies;
2104 * check addrconf expiration here.
2105 * Routes are expired even if they are in use.
2108 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
2109 if (time_after(now, rt->expires)) {
2110 RT6_TRACE("expiring %p\n", rt);
2116 /* Also age clones in the exception table.
2117 * Note, that clones are aged out
2118 * only if they are not in use now.
2120 rt6_age_exceptions(rt, gc_args, now);
2125 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2127 struct fib6_gc_args gc_args;
2131 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2132 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2133 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2136 gc_args.timeout = expires ? (int)expires :
2137 net->ipv6.sysctl.ip6_rt_gc_interval;
2140 fib6_clean_all(net, fib6_age, &gc_args);
2142 net->ipv6.ip6_rt_last_gc = now;
2145 mod_timer(&net->ipv6.ip6_fib_timer,
2147 + net->ipv6.sysctl.ip6_rt_gc_interval));
2149 del_timer(&net->ipv6.ip6_fib_timer);
2150 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2153 static void fib6_gc_timer_cb(struct timer_list *t)
2155 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2157 fib6_run_gc(0, arg, true);
2160 static int __net_init fib6_net_init(struct net *net)
2162 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2165 err = fib6_notifier_init(net);
2169 spin_lock_init(&net->ipv6.fib6_gc_lock);
2170 rwlock_init(&net->ipv6.fib6_walker_lock);
2171 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2172 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2174 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2175 if (!net->ipv6.rt6_stats)
2178 /* Avoid false sharing : Use at least a full cache line */
2179 size = max_t(size_t, size, L1_CACHE_BYTES);
2181 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2182 if (!net->ipv6.fib_table_hash)
2185 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2187 if (!net->ipv6.fib6_main_tbl)
2188 goto out_fib_table_hash;
2190 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2191 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2192 net->ipv6.fib6_null_entry);
2193 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2194 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2195 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2197 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2198 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2200 if (!net->ipv6.fib6_local_tbl)
2201 goto out_fib6_main_tbl;
2202 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2203 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2204 net->ipv6.fib6_null_entry);
2205 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2206 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2207 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2209 fib6_tables_init(net);
2213 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2215 kfree(net->ipv6.fib6_main_tbl);
2218 kfree(net->ipv6.fib_table_hash);
2220 kfree(net->ipv6.rt6_stats);
2222 fib6_notifier_exit(net);
2226 static void fib6_net_exit(struct net *net)
2230 del_timer_sync(&net->ipv6.ip6_fib_timer);
2232 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2233 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2234 struct hlist_node *tmp;
2235 struct fib6_table *tb;
2237 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2238 hlist_del(&tb->tb6_hlist);
2239 fib6_free_table(tb);
2243 kfree(net->ipv6.fib_table_hash);
2244 kfree(net->ipv6.rt6_stats);
2245 fib6_notifier_exit(net);
2248 static struct pernet_operations fib6_net_ops = {
2249 .init = fib6_net_init,
2250 .exit = fib6_net_exit,
2253 int __init fib6_init(void)
2257 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2258 sizeof(struct fib6_node),
2259 0, SLAB_HWCACHE_ALIGN,
2261 if (!fib6_node_kmem)
2264 ret = register_pernet_subsys(&fib6_net_ops);
2266 goto out_kmem_cache_create;
2268 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2271 goto out_unregister_subsys;
2273 __fib6_flush_trees = fib6_flush_trees;
2277 out_unregister_subsys:
2278 unregister_pernet_subsys(&fib6_net_ops);
2279 out_kmem_cache_create:
2280 kmem_cache_destroy(fib6_node_kmem);
2284 void fib6_gc_cleanup(void)
2286 unregister_pernet_subsys(&fib6_net_ops);
2287 kmem_cache_destroy(fib6_node_kmem);
2290 #ifdef CONFIG_PROC_FS
2291 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2293 struct fib6_info *rt = v;
2294 struct ipv6_route_iter *iter = seq->private;
2295 unsigned int flags = rt->fib6_flags;
2296 const struct net_device *dev;
2298 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
2300 #ifdef CONFIG_IPV6_SUBTREES
2301 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
2303 seq_puts(seq, "00000000000000000000000000000000 00 ");
2305 if (rt->fib6_nh.fib_nh_gw_family) {
2306 flags |= RTF_GATEWAY;
2307 seq_printf(seq, "%pi6", &rt->fib6_nh.fib_nh_gw6);
2309 seq_puts(seq, "00000000000000000000000000000000");
2312 dev = rt->fib6_nh.fib_nh_dev;
2313 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2314 rt->fib6_metric, refcount_read(&rt->fib6_ref), 0,
2315 flags, dev ? dev->name : "");
2316 iter->w.leaf = NULL;
2320 static int ipv6_route_yield(struct fib6_walker *w)
2322 struct ipv6_route_iter *iter = w->args;
2328 iter->w.leaf = rcu_dereference_protected(
2329 iter->w.leaf->fib6_next,
2330 lockdep_is_held(&iter->tbl->tb6_lock));
2332 if (!iter->skip && iter->w.leaf)
2334 } while (iter->w.leaf);
2339 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2342 memset(&iter->w, 0, sizeof(iter->w));
2343 iter->w.func = ipv6_route_yield;
2344 iter->w.root = &iter->tbl->tb6_root;
2345 iter->w.state = FWS_INIT;
2346 iter->w.node = iter->w.root;
2347 iter->w.args = iter;
2348 iter->sernum = iter->w.root->fn_sernum;
2349 INIT_LIST_HEAD(&iter->w.lh);
2350 fib6_walker_link(net, &iter->w);
2353 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2357 struct hlist_node *node;
2360 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2361 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2367 while (!node && h < FIB6_TABLE_HASHSZ) {
2368 node = rcu_dereference_bh(
2369 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2371 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2374 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2376 if (iter->sernum != iter->w.root->fn_sernum) {
2377 iter->sernum = iter->w.root->fn_sernum;
2378 iter->w.state = FWS_INIT;
2379 iter->w.node = iter->w.root;
2380 WARN_ON(iter->w.skip);
2381 iter->w.skip = iter->w.count;
2385 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2388 struct fib6_info *n;
2389 struct net *net = seq_file_net(seq);
2390 struct ipv6_route_iter *iter = seq->private;
2395 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
2402 ipv6_route_check_sernum(iter);
2403 spin_lock_bh(&iter->tbl->tb6_lock);
2404 r = fib6_walk_continue(&iter->w);
2405 spin_unlock_bh(&iter->tbl->tb6_lock);
2409 return iter->w.leaf;
2411 fib6_walker_unlink(net, &iter->w);
2414 fib6_walker_unlink(net, &iter->w);
2416 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2420 ipv6_route_seq_setup_walk(iter, net);
2424 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2427 struct net *net = seq_file_net(seq);
2428 struct ipv6_route_iter *iter = seq->private;
2431 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2435 ipv6_route_seq_setup_walk(iter, net);
2436 return ipv6_route_seq_next(seq, NULL, pos);
2442 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2444 struct fib6_walker *w = &iter->w;
2445 return w->node && !(w->state == FWS_U && w->node == w->root);
2448 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2451 struct net *net = seq_file_net(seq);
2452 struct ipv6_route_iter *iter = seq->private;
2454 if (ipv6_route_iter_active(iter))
2455 fib6_walker_unlink(net, &iter->w);
2457 rcu_read_unlock_bh();
2460 const struct seq_operations ipv6_route_seq_ops = {
2461 .start = ipv6_route_seq_start,
2462 .next = ipv6_route_seq_next,
2463 .stop = ipv6_route_seq_stop,
2464 .show = ipv6_route_seq_show
2466 #endif /* CONFIG_PROC_FS */