2 * Linux INET6 implementation
3 * Forwarding Information Database
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
17 * Ville Nuorvala: Fixed routing subtrees.
20 #define pr_fmt(fmt) "IPv6: " fmt
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/net.h>
25 #include <linux/route.h>
26 #include <linux/netdevice.h>
27 #include <linux/in6.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
33 #include <net/ndisc.h>
34 #include <net/addrconf.h>
35 #include <net/lwtunnel.h>
36 #include <net/fib_notifier.h>
38 #include <net/ip6_fib.h>
39 #include <net/ip6_route.h>
41 static struct kmem_cache *fib6_node_kmem __read_mostly;
46 int (*func)(struct 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)
97 old = atomic_read(&net->ipv6.fib6_sernum);
98 new = old < INT_MAX ? old + 1 : 1;
99 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
105 FIB6_NO_SERNUM_CHANGE = 0,
108 void fib6_update_sernum(struct net *net, struct fib6_info *f6i)
110 struct fib6_node *fn;
112 fn = rcu_dereference_protected(f6i->fib6_node,
113 lockdep_is_held(&f6i->fib6_table->tb6_lock));
115 WRITE_ONCE(fn->fn_sernum, fib6_new_sernum(net));
119 * Auxiliary address test functions for the radix tree.
121 * These assume a 32bit processor (although it will work on
128 #if defined(__LITTLE_ENDIAN)
129 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
131 # define BITOP_BE32_SWIZZLE 0
134 static __be32 addr_bit_set(const void *token, int fn_bit)
136 const __be32 *addr = token;
139 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
140 * is optimized version of
141 * htonl(1 << ((~fn_bit)&0x1F))
142 * See include/asm-generic/bitops/le.h.
144 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
148 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags)
150 struct fib6_info *f6i;
152 f6i = kzalloc(sizeof(*f6i), gfp_flags);
156 f6i->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
157 if (!f6i->rt6i_pcpu) {
162 INIT_LIST_HEAD(&f6i->fib6_siblings);
163 f6i->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
165 atomic_inc(&f6i->fib6_ref);
170 void fib6_info_destroy_rcu(struct rcu_head *head)
172 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
173 struct rt6_exception_bucket *bucket;
174 struct dst_metrics *m;
176 WARN_ON(f6i->fib6_node);
178 bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
180 f6i->rt6i_exception_bucket = NULL;
184 if (f6i->rt6i_pcpu) {
187 for_each_possible_cpu(cpu) {
188 struct rt6_info **ppcpu_rt;
189 struct rt6_info *pcpu_rt;
191 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
194 dst_dev_put(&pcpu_rt->dst);
195 dst_release(&pcpu_rt->dst);
200 free_percpu(f6i->rt6i_pcpu);
203 lwtstate_put(f6i->fib6_nh.nh_lwtstate);
205 if (f6i->fib6_nh.nh_dev)
206 dev_put(f6i->fib6_nh.nh_dev);
208 m = f6i->fib6_metrics;
209 if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
214 EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
216 static struct fib6_node *node_alloc(struct net *net)
218 struct fib6_node *fn;
220 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
222 net->ipv6.rt6_stats->fib_nodes++;
227 static void node_free_immediate(struct net *net, struct fib6_node *fn)
229 kmem_cache_free(fib6_node_kmem, fn);
230 net->ipv6.rt6_stats->fib_nodes--;
233 static void node_free_rcu(struct rcu_head *head)
235 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
237 kmem_cache_free(fib6_node_kmem, fn);
240 static void node_free(struct net *net, struct fib6_node *fn)
242 call_rcu(&fn->rcu, node_free_rcu);
243 net->ipv6.rt6_stats->fib_nodes--;
246 static void fib6_free_table(struct fib6_table *table)
248 inetpeer_invalidate_tree(&table->tb6_peers);
252 static void fib6_link_table(struct net *net, struct fib6_table *tb)
257 * Initialize table lock at a single place to give lockdep a key,
258 * tables aren't visible prior to being linked to the list.
260 spin_lock_init(&tb->tb6_lock);
261 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
264 * No protection necessary, this is the only list mutatation
265 * operation, tables never disappear once they exist.
267 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
270 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
272 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
274 struct fib6_table *table;
276 table = kzalloc(sizeof(*table), GFP_ATOMIC);
279 rcu_assign_pointer(table->tb6_root.leaf,
280 net->ipv6.fib6_null_entry);
281 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
282 inet_peer_base_init(&table->tb6_peers);
288 struct fib6_table *fib6_new_table(struct net *net, u32 id)
290 struct fib6_table *tb;
294 tb = fib6_get_table(net, id);
298 tb = fib6_alloc_table(net, id);
300 fib6_link_table(net, tb);
304 EXPORT_SYMBOL_GPL(fib6_new_table);
306 struct fib6_table *fib6_get_table(struct net *net, u32 id)
308 struct fib6_table *tb;
309 struct hlist_head *head;
314 h = id & (FIB6_TABLE_HASHSZ - 1);
316 head = &net->ipv6.fib_table_hash[h];
317 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
318 if (tb->tb6_id == id) {
327 EXPORT_SYMBOL_GPL(fib6_get_table);
329 static void __net_init fib6_tables_init(struct net *net)
331 fib6_link_table(net, net->ipv6.fib6_main_tbl);
332 fib6_link_table(net, net->ipv6.fib6_local_tbl);
336 struct fib6_table *fib6_new_table(struct net *net, u32 id)
338 return fib6_get_table(net, id);
341 struct fib6_table *fib6_get_table(struct net *net, u32 id)
343 return net->ipv6.fib6_main_tbl;
346 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
347 const struct sk_buff *skb,
348 int flags, pol_lookup_t lookup)
352 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
353 if (rt->dst.error == -EAGAIN) {
355 rt = net->ipv6.ip6_null_entry;
362 /* called with rcu lock held; no reference taken on fib6_info */
363 struct fib6_info *fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
366 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6, flags);
369 static void __net_init fib6_tables_init(struct net *net)
371 fib6_link_table(net, net->ipv6.fib6_main_tbl);
376 unsigned int fib6_tables_seq_read(struct net *net)
378 unsigned int h, fib_seq = 0;
381 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
382 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
383 struct fib6_table *tb;
385 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
386 fib_seq += tb->fib_seq;
393 static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
394 enum fib_event_type event_type,
395 struct fib6_info *rt)
397 struct fib6_entry_notifier_info info = {
401 return call_fib6_notifier(nb, net, event_type, &info.info);
404 static int call_fib6_entry_notifiers(struct net *net,
405 enum fib_event_type event_type,
406 struct fib6_info *rt,
407 struct netlink_ext_ack *extack)
409 struct fib6_entry_notifier_info info = {
410 .info.extack = extack,
414 rt->fib6_table->fib_seq++;
415 return call_fib6_notifiers(net, event_type, &info.info);
418 struct fib6_dump_arg {
420 struct notifier_block *nb;
423 static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
425 if (rt == arg->net->ipv6.fib6_null_entry)
427 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
430 static int fib6_node_dump(struct fib6_walker *w)
432 struct fib6_info *rt;
434 for_each_fib6_walker_rt(w)
435 fib6_rt_dump(rt, w->args);
440 static void fib6_table_dump(struct net *net, struct fib6_table *tb,
441 struct fib6_walker *w)
443 w->root = &tb->tb6_root;
444 spin_lock_bh(&tb->tb6_lock);
446 spin_unlock_bh(&tb->tb6_lock);
449 /* Called with rcu_read_lock() */
450 int fib6_tables_dump(struct net *net, struct notifier_block *nb)
452 struct fib6_dump_arg arg;
453 struct fib6_walker *w;
456 w = kzalloc(sizeof(*w), GFP_ATOMIC);
460 w->func = fib6_node_dump;
465 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
466 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
467 struct fib6_table *tb;
469 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
470 fib6_table_dump(net, tb, w);
478 static int fib6_dump_node(struct fib6_walker *w)
481 struct fib6_info *rt;
483 for_each_fib6_walker_rt(w) {
484 res = rt6_dump_route(rt, w->args);
486 /* Frame is full, suspend walking */
491 /* Multipath routes are dumped in one route with the
492 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
493 * last sibling of this route (no need to dump the
494 * sibling routes again)
496 if (rt->fib6_nsiblings)
497 rt = list_last_entry(&rt->fib6_siblings,
505 static void fib6_dump_end(struct netlink_callback *cb)
507 struct net *net = sock_net(cb->skb->sk);
508 struct fib6_walker *w = (void *)cb->args[2];
513 fib6_walker_unlink(net, w);
518 cb->done = (void *)cb->args[3];
522 static int fib6_dump_done(struct netlink_callback *cb)
525 return cb->done ? cb->done(cb) : 0;
528 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
529 struct netlink_callback *cb)
531 struct net *net = sock_net(skb->sk);
532 struct fib6_walker *w;
535 w = (void *)cb->args[2];
536 w->root = &table->tb6_root;
538 if (cb->args[4] == 0) {
542 spin_lock_bh(&table->tb6_lock);
543 res = fib6_walk(net, w);
544 spin_unlock_bh(&table->tb6_lock);
547 cb->args[5] = READ_ONCE(w->root->fn_sernum);
550 int sernum = READ_ONCE(w->root->fn_sernum);
551 if (cb->args[5] != sernum) {
552 /* Begin at the root if the tree changed */
553 cb->args[5] = sernum;
560 spin_lock_bh(&table->tb6_lock);
561 res = fib6_walk_continue(w);
562 spin_unlock_bh(&table->tb6_lock);
564 fib6_walker_unlink(net, w);
572 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
574 struct net *net = sock_net(skb->sk);
576 unsigned int e = 0, s_e;
577 struct rt6_rtnl_dump_arg arg;
578 struct fib6_walker *w;
579 struct fib6_table *tb;
580 struct hlist_head *head;
586 w = (void *)cb->args[2];
590 * 1. hook callback destructor.
592 cb->args[3] = (long)cb->done;
593 cb->done = fib6_dump_done;
596 * 2. allocate and initialize walker.
598 w = kzalloc(sizeof(*w), GFP_ATOMIC);
601 w->func = fib6_dump_node;
602 cb->args[2] = (long)w;
611 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
613 head = &net->ipv6.fib_table_hash[h];
614 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
617 res = fib6_dump_table(tb, skb, cb);
629 res = res < 0 ? res : skb->len;
635 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
640 if (f6i->fib6_metrics == &dst_default_metrics) {
641 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
646 refcount_set(&p->refcnt, 1);
647 f6i->fib6_metrics = p;
650 f6i->fib6_metrics->metrics[metric - 1] = val;
656 * return the appropriate node for a routing tree "add" operation
657 * by either creating and inserting or by returning an existing
661 static struct fib6_node *fib6_add_1(struct net *net,
662 struct fib6_table *table,
663 struct fib6_node *root,
664 struct in6_addr *addr, int plen,
665 int offset, int allow_create,
666 int replace_required,
667 struct netlink_ext_ack *extack)
669 struct fib6_node *fn, *in, *ln;
670 struct fib6_node *pn = NULL;
675 RT6_TRACE("fib6_add_1\n");
677 /* insert node in tree */
682 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
683 lockdep_is_held(&table->tb6_lock));
684 key = (struct rt6key *)((u8 *)leaf + offset);
689 if (plen < fn->fn_bit ||
690 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
692 if (replace_required) {
693 NL_SET_ERR_MSG(extack,
694 "Can not replace route - no match found");
695 pr_warn("Can't replace route, no match found\n");
696 return ERR_PTR(-ENOENT);
698 pr_warn("NLM_F_CREATE should be set when creating new route\n");
707 if (plen == fn->fn_bit) {
708 /* clean up an intermediate node */
709 if (!(fn->fn_flags & RTN_RTINFO)) {
710 RCU_INIT_POINTER(fn->leaf, NULL);
711 fib6_info_release(leaf);
712 /* remove null_entry in the root node */
713 } else if (fn->fn_flags & RTN_TL_ROOT &&
714 rcu_access_pointer(fn->leaf) ==
715 net->ipv6.fib6_null_entry) {
716 RCU_INIT_POINTER(fn->leaf, NULL);
723 * We have more bits to go
726 /* Try to walk down on tree. */
727 dir = addr_bit_set(addr, fn->fn_bit);
730 rcu_dereference_protected(fn->right,
731 lockdep_is_held(&table->tb6_lock)) :
732 rcu_dereference_protected(fn->left,
733 lockdep_is_held(&table->tb6_lock));
737 /* We should not create new node because
738 * NLM_F_REPLACE was specified without NLM_F_CREATE
739 * I assume it is safe to require NLM_F_CREATE when
740 * REPLACE flag is used! Later we may want to remove the
741 * check for replace_required, because according
742 * to netlink specification, NLM_F_CREATE
743 * MUST be specified if new route is created.
744 * That would keep IPv6 consistent with IPv4
746 if (replace_required) {
747 NL_SET_ERR_MSG(extack,
748 "Can not replace route - no match found");
749 pr_warn("Can't replace route, no match found\n");
750 return ERR_PTR(-ENOENT);
752 pr_warn("NLM_F_CREATE should be set when creating new route\n");
755 * We walked to the bottom of tree.
756 * Create new leaf node without children.
759 ln = node_alloc(net);
762 return ERR_PTR(-ENOMEM);
764 RCU_INIT_POINTER(ln->parent, pn);
767 rcu_assign_pointer(pn->right, ln);
769 rcu_assign_pointer(pn->left, ln);
776 * split since we don't have a common prefix anymore or
777 * we have a less significant route.
778 * we've to insert an intermediate node on the list
779 * this new node will point to the one we need to create
783 pn = rcu_dereference_protected(fn->parent,
784 lockdep_is_held(&table->tb6_lock));
786 /* find 1st bit in difference between the 2 addrs.
788 See comment in __ipv6_addr_diff: bit may be an invalid value,
789 but if it is >= plen, the value is ignored in any case.
792 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
797 * (new leaf node)[ln] (old node)[fn]
800 in = node_alloc(net);
801 ln = node_alloc(net);
805 node_free_immediate(net, in);
807 node_free_immediate(net, ln);
808 return ERR_PTR(-ENOMEM);
812 * new intermediate node.
814 * be off since that an address that chooses one of
815 * the branches would not match less specific routes
816 * in the other branch
821 RCU_INIT_POINTER(in->parent, pn);
823 atomic_inc(&rcu_dereference_protected(in->leaf,
824 lockdep_is_held(&table->tb6_lock))->fib6_ref);
826 /* update parent pointer */
828 rcu_assign_pointer(pn->right, in);
830 rcu_assign_pointer(pn->left, in);
834 RCU_INIT_POINTER(ln->parent, in);
835 rcu_assign_pointer(fn->parent, in);
837 if (addr_bit_set(addr, bit)) {
838 rcu_assign_pointer(in->right, ln);
839 rcu_assign_pointer(in->left, fn);
841 rcu_assign_pointer(in->left, ln);
842 rcu_assign_pointer(in->right, fn);
844 } else { /* plen <= bit */
847 * (new leaf node)[ln]
849 * (old node)[fn] NULL
852 ln = node_alloc(net);
855 return ERR_PTR(-ENOMEM);
859 RCU_INIT_POINTER(ln->parent, pn);
861 if (addr_bit_set(&key->addr, plen))
862 RCU_INIT_POINTER(ln->right, fn);
864 RCU_INIT_POINTER(ln->left, fn);
866 rcu_assign_pointer(fn->parent, ln);
869 rcu_assign_pointer(pn->right, ln);
871 rcu_assign_pointer(pn->left, ln);
876 static void fib6_drop_pcpu_from(struct fib6_info *f6i,
877 const struct fib6_table *table)
881 /* Make sure rt6_make_pcpu_route() wont add other percpu routes
882 * while we are cleaning them here.
884 f6i->fib6_destroying = 1;
885 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
887 /* release the reference to this fib entry from
888 * all of its cached pcpu routes
890 for_each_possible_cpu(cpu) {
891 struct rt6_info **ppcpu_rt;
892 struct rt6_info *pcpu_rt;
894 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
897 struct fib6_info *from;
899 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
900 fib6_info_release(from);
905 static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
908 struct fib6_table *table = rt->fib6_table;
910 /* Flush all cached dst in exception table */
911 rt6_flush_exceptions(rt);
913 fib6_drop_pcpu_from(rt, table);
915 if (atomic_read(&rt->fib6_ref) != 1) {
916 /* This route is used as dummy address holder in some split
917 * nodes. It is not leaked, but it still holds other resources,
918 * which must be released in time. So, scan ascendant nodes
919 * and replace dummy references to this route with references
920 * to still alive ones.
923 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
924 lockdep_is_held(&table->tb6_lock));
925 struct fib6_info *new_leaf;
926 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
927 new_leaf = fib6_find_prefix(net, table, fn);
928 atomic_inc(&new_leaf->fib6_ref);
930 rcu_assign_pointer(fn->leaf, new_leaf);
931 fib6_info_release(rt);
933 fn = rcu_dereference_protected(fn->parent,
934 lockdep_is_held(&table->tb6_lock));
940 * Insert routing information in a node.
943 static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
944 struct nl_info *info,
945 struct netlink_ext_ack *extack)
947 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
948 lockdep_is_held(&rt->fib6_table->tb6_lock));
949 struct fib6_info *iter = NULL;
950 struct fib6_info __rcu **ins;
951 struct fib6_info __rcu **fallback_ins = NULL;
952 int replace = (info->nlh &&
953 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
954 int add = (!info->nlh ||
955 (info->nlh->nlmsg_flags & NLM_F_CREATE));
957 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
958 u16 nlflags = NLM_F_EXCL;
961 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
962 nlflags |= NLM_F_APPEND;
966 for (iter = leaf; iter;
967 iter = rcu_dereference_protected(iter->fib6_next,
968 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
970 * Search for duplicates
973 if (iter->fib6_metric == rt->fib6_metric) {
975 * Same priority level
978 (info->nlh->nlmsg_flags & NLM_F_EXCL))
981 nlflags &= ~NLM_F_EXCL;
983 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
987 fallback_ins = fallback_ins ?: ins;
991 if (rt6_duplicate_nexthop(iter, rt)) {
992 if (rt->fib6_nsiblings)
993 rt->fib6_nsiblings = 0;
994 if (!(iter->fib6_flags & RTF_EXPIRES))
996 if (!(rt->fib6_flags & RTF_EXPIRES))
997 fib6_clean_expires(iter);
999 fib6_set_expires(iter, rt->expires);
1002 fib6_metric_set(iter, RTAX_MTU,
1006 /* If we have the same destination and the same metric,
1007 * but not the same gateway, then the route we try to
1008 * add is sibling to this route, increment our counter
1009 * of siblings, and later we will add our route to the
1011 * Only static routes (which don't have flag
1012 * RTF_EXPIRES) are used for ECMPv6.
1014 * To avoid long list, we only had siblings if the
1015 * route have a gateway.
1018 rt6_qualify_for_ecmp(iter))
1019 rt->fib6_nsiblings++;
1022 if (iter->fib6_metric > rt->fib6_metric)
1026 ins = &iter->fib6_next;
1029 if (fallback_ins && !found) {
1030 /* No matching route with same ecmp-able-ness found, replace
1031 * first matching route
1034 iter = rcu_dereference_protected(*ins,
1035 lockdep_is_held(&rt->fib6_table->tb6_lock));
1039 /* Reset round-robin state, if necessary */
1040 if (ins == &fn->leaf)
1043 /* Link this route to others same route. */
1044 if (rt->fib6_nsiblings) {
1045 unsigned int fib6_nsiblings;
1046 struct fib6_info *sibling, *temp_sibling;
1048 /* Find the first route that have the same metric */
1051 if (sibling->fib6_metric == rt->fib6_metric &&
1052 rt6_qualify_for_ecmp(sibling)) {
1053 list_add_tail(&rt->fib6_siblings,
1054 &sibling->fib6_siblings);
1057 sibling = rcu_dereference_protected(sibling->fib6_next,
1058 lockdep_is_held(&rt->fib6_table->tb6_lock));
1060 /* For each sibling in the list, increment the counter of
1061 * siblings. BUG() if counters does not match, list of siblings
1065 list_for_each_entry_safe(sibling, temp_sibling,
1066 &rt->fib6_siblings, fib6_siblings) {
1067 sibling->fib6_nsiblings++;
1068 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1071 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1072 rt6_multipath_rebalance(temp_sibling);
1080 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1083 nlflags |= NLM_F_CREATE;
1085 err = call_fib6_entry_notifiers(info->nl_net,
1086 FIB_EVENT_ENTRY_ADD,
1089 struct fib6_info *sibling, *next_sibling;
1091 /* If the route has siblings, then it first
1092 * needs to be unlinked from them.
1094 if (!rt->fib6_nsiblings)
1097 list_for_each_entry_safe(sibling, next_sibling,
1100 sibling->fib6_nsiblings--;
1101 rt->fib6_nsiblings = 0;
1102 list_del_init(&rt->fib6_siblings);
1103 rt6_multipath_rebalance(next_sibling);
1107 rcu_assign_pointer(rt->fib6_next, iter);
1108 atomic_inc(&rt->fib6_ref);
1109 rcu_assign_pointer(rt->fib6_node, fn);
1110 rcu_assign_pointer(*ins, rt);
1111 if (!info->skip_notify)
1112 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1113 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1115 if (!(fn->fn_flags & RTN_RTINFO)) {
1116 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1117 fn->fn_flags |= RTN_RTINFO;
1126 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1130 err = call_fib6_entry_notifiers(info->nl_net,
1131 FIB_EVENT_ENTRY_REPLACE,
1136 atomic_inc(&rt->fib6_ref);
1137 rcu_assign_pointer(rt->fib6_node, fn);
1138 rt->fib6_next = iter->fib6_next;
1139 rcu_assign_pointer(*ins, rt);
1140 if (!info->skip_notify)
1141 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1142 if (!(fn->fn_flags & RTN_RTINFO)) {
1143 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1144 fn->fn_flags |= RTN_RTINFO;
1146 nsiblings = iter->fib6_nsiblings;
1147 iter->fib6_node = NULL;
1148 fib6_purge_rt(iter, fn, info->nl_net);
1149 if (rcu_access_pointer(fn->rr_ptr) == iter)
1151 fib6_info_release(iter);
1154 /* Replacing an ECMP route, remove all siblings */
1155 ins = &rt->fib6_next;
1156 iter = rcu_dereference_protected(*ins,
1157 lockdep_is_held(&rt->fib6_table->tb6_lock));
1159 if (iter->fib6_metric > rt->fib6_metric)
1161 if (rt6_qualify_for_ecmp(iter)) {
1162 *ins = iter->fib6_next;
1163 iter->fib6_node = NULL;
1164 fib6_purge_rt(iter, fn, info->nl_net);
1165 if (rcu_access_pointer(fn->rr_ptr) == iter)
1167 fib6_info_release(iter);
1169 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1171 ins = &iter->fib6_next;
1173 iter = rcu_dereference_protected(*ins,
1174 lockdep_is_held(&rt->fib6_table->tb6_lock));
1176 WARN_ON(nsiblings != 0);
1183 static void fib6_start_gc(struct net *net, struct fib6_info *rt)
1185 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1186 (rt->fib6_flags & RTF_EXPIRES))
1187 mod_timer(&net->ipv6.ip6_fib_timer,
1188 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1191 void fib6_force_start_gc(struct net *net)
1193 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1194 mod_timer(&net->ipv6.ip6_fib_timer,
1195 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1198 static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
1201 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1202 lockdep_is_held(&rt->fib6_table->tb6_lock));
1204 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1207 WRITE_ONCE(fn->fn_sernum, sernum);
1208 fn = rcu_dereference_protected(fn->parent,
1209 lockdep_is_held(&rt->fib6_table->tb6_lock));
1213 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
1215 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1219 * Add routing information to the routing tree.
1220 * <destination addr>/<source addr>
1221 * with source addr info in sub-trees
1222 * Need to own table->tb6_lock
1225 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
1226 struct nl_info *info, struct netlink_ext_ack *extack)
1228 struct fib6_table *table = rt->fib6_table;
1229 struct fib6_node *fn, *pn = NULL;
1231 int allow_create = 1;
1232 int replace_required = 0;
1233 int sernum = fib6_new_sernum(info->nl_net);
1236 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1238 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1239 replace_required = 1;
1241 if (!allow_create && !replace_required)
1242 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1244 fn = fib6_add_1(info->nl_net, table, root,
1245 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1246 offsetof(struct fib6_info, fib6_dst), allow_create,
1247 replace_required, extack);
1256 #ifdef CONFIG_IPV6_SUBTREES
1257 if (rt->fib6_src.plen) {
1258 struct fib6_node *sn;
1260 if (!rcu_access_pointer(fn->subtree)) {
1261 struct fib6_node *sfn;
1273 /* Create subtree root node */
1274 sfn = node_alloc(info->nl_net);
1278 atomic_inc(&info->nl_net->ipv6.fib6_null_entry->fib6_ref);
1279 rcu_assign_pointer(sfn->leaf,
1280 info->nl_net->ipv6.fib6_null_entry);
1281 sfn->fn_flags = RTN_ROOT;
1283 /* Now add the first leaf node to new subtree */
1285 sn = fib6_add_1(info->nl_net, table, sfn,
1286 &rt->fib6_src.addr, rt->fib6_src.plen,
1287 offsetof(struct fib6_info, fib6_src),
1288 allow_create, replace_required, extack);
1291 /* If it is failed, discard just allocated
1292 root, and then (in failure) stale node
1295 node_free_immediate(info->nl_net, sfn);
1300 /* Now link new subtree to main tree */
1301 rcu_assign_pointer(sfn->parent, fn);
1302 rcu_assign_pointer(fn->subtree, sfn);
1304 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1305 &rt->fib6_src.addr, rt->fib6_src.plen,
1306 offsetof(struct fib6_info, fib6_src),
1307 allow_create, replace_required, extack);
1315 if (!rcu_access_pointer(fn->leaf)) {
1316 if (fn->fn_flags & RTN_TL_ROOT) {
1317 /* put back null_entry for root node */
1318 rcu_assign_pointer(fn->leaf,
1319 info->nl_net->ipv6.fib6_null_entry);
1321 atomic_inc(&rt->fib6_ref);
1322 rcu_assign_pointer(fn->leaf, rt);
1329 err = fib6_add_rt2node(fn, rt, info, extack);
1331 __fib6_update_sernum_upto_root(rt, sernum);
1332 fib6_start_gc(info->nl_net, rt);
1337 #ifdef CONFIG_IPV6_SUBTREES
1339 * If fib6_add_1 has cleared the old leaf pointer in the
1340 * super-tree leaf node we have to find a new one for it.
1343 struct fib6_info *pn_leaf =
1344 rcu_dereference_protected(pn->leaf,
1345 lockdep_is_held(&table->tb6_lock));
1346 if (pn_leaf == rt) {
1348 RCU_INIT_POINTER(pn->leaf, NULL);
1349 fib6_info_release(rt);
1351 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1352 pn_leaf = fib6_find_prefix(info->nl_net, table,
1358 info->nl_net->ipv6.fib6_null_entry;
1361 fib6_info_hold(pn_leaf);
1362 rcu_assign_pointer(pn->leaf, pn_leaf);
1371 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1372 * 1. fn is an intermediate node and we failed to add the new
1373 * route to it in both subtree creation failure and fib6_add_rt2node()
1375 * 2. fn is the root node in the table and we fail to add the first
1376 * default route to it.
1379 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1380 (fn->fn_flags & RTN_TL_ROOT &&
1381 !rcu_access_pointer(fn->leaf))))
1382 fib6_repair_tree(info->nl_net, table, fn);
1387 * Routing tree lookup
1391 struct lookup_args {
1392 int offset; /* key offset on fib6_info */
1393 const struct in6_addr *addr; /* search key */
1396 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1397 struct lookup_args *args)
1399 struct fib6_node *fn;
1402 if (unlikely(args->offset == 0))
1412 struct fib6_node *next;
1414 dir = addr_bit_set(args->addr, fn->fn_bit);
1416 next = dir ? rcu_dereference(fn->right) :
1417 rcu_dereference(fn->left);
1427 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1429 if (subtree || fn->fn_flags & RTN_RTINFO) {
1430 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1436 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1438 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1439 #ifdef CONFIG_IPV6_SUBTREES
1441 struct fib6_node *sfn;
1442 sfn = fib6_node_lookup_1(subtree,
1449 if (fn->fn_flags & RTN_RTINFO)
1454 if (fn->fn_flags & RTN_ROOT)
1457 fn = rcu_dereference(fn->parent);
1463 /* called with rcu_read_lock() held
1465 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1466 const struct in6_addr *daddr,
1467 const struct in6_addr *saddr)
1469 struct fib6_node *fn;
1470 struct lookup_args args[] = {
1472 .offset = offsetof(struct fib6_info, fib6_dst),
1475 #ifdef CONFIG_IPV6_SUBTREES
1477 .offset = offsetof(struct fib6_info, fib6_src),
1482 .offset = 0, /* sentinel */
1486 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
1487 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1494 * Get node with specified destination prefix (and source prefix,
1495 * if subtrees are used)
1496 * exact_match == true means we try to find fn with exact match of
1497 * the passed in prefix addr
1498 * exact_match == false means we try to find fn with longest prefix
1499 * match of the passed in prefix addr. This is useful for finding fn
1500 * for cached route as it will be stored in the exception table under
1501 * the node with longest prefix length.
1505 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1506 const struct in6_addr *addr,
1507 int plen, int offset,
1510 struct fib6_node *fn, *prev = NULL;
1512 for (fn = root; fn ; ) {
1513 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1516 /* This node is being deleted */
1518 if (plen <= fn->fn_bit)
1524 key = (struct rt6key *)((u8 *)leaf + offset);
1529 if (plen < fn->fn_bit ||
1530 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1533 if (plen == fn->fn_bit)
1536 if (fn->fn_flags & RTN_RTINFO)
1541 * We have more bits to go
1543 if (addr_bit_set(addr, fn->fn_bit))
1544 fn = rcu_dereference(fn->right);
1546 fn = rcu_dereference(fn->left);
1555 struct fib6_node *fib6_locate(struct fib6_node *root,
1556 const struct in6_addr *daddr, int dst_len,
1557 const struct in6_addr *saddr, int src_len,
1560 struct fib6_node *fn;
1562 fn = fib6_locate_1(root, daddr, dst_len,
1563 offsetof(struct fib6_info, fib6_dst),
1566 #ifdef CONFIG_IPV6_SUBTREES
1568 WARN_ON(saddr == NULL);
1570 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1573 fn = fib6_locate_1(subtree, saddr, src_len,
1574 offsetof(struct fib6_info, fib6_src),
1581 if (fn && fn->fn_flags & RTN_RTINFO)
1593 static struct fib6_info *fib6_find_prefix(struct net *net,
1594 struct fib6_table *table,
1595 struct fib6_node *fn)
1597 struct fib6_node *child_left, *child_right;
1599 if (fn->fn_flags & RTN_ROOT)
1600 return net->ipv6.fib6_null_entry;
1603 child_left = rcu_dereference_protected(fn->left,
1604 lockdep_is_held(&table->tb6_lock));
1605 child_right = rcu_dereference_protected(fn->right,
1606 lockdep_is_held(&table->tb6_lock));
1608 return rcu_dereference_protected(child_left->leaf,
1609 lockdep_is_held(&table->tb6_lock));
1611 return rcu_dereference_protected(child_right->leaf,
1612 lockdep_is_held(&table->tb6_lock));
1614 fn = FIB6_SUBTREE(fn);
1620 * Called to trim the tree of intermediate nodes when possible. "fn"
1621 * is the node we want to try and remove.
1622 * Need to own table->tb6_lock
1625 static struct fib6_node *fib6_repair_tree(struct net *net,
1626 struct fib6_table *table,
1627 struct fib6_node *fn)
1631 struct fib6_node *child;
1632 struct fib6_walker *w;
1635 /* Set fn->leaf to null_entry for root node. */
1636 if (fn->fn_flags & RTN_TL_ROOT) {
1637 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1642 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1643 lockdep_is_held(&table->tb6_lock));
1644 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1645 lockdep_is_held(&table->tb6_lock));
1646 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1647 lockdep_is_held(&table->tb6_lock));
1648 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1649 lockdep_is_held(&table->tb6_lock));
1650 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1651 lockdep_is_held(&table->tb6_lock));
1652 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1653 lockdep_is_held(&table->tb6_lock));
1654 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1655 lockdep_is_held(&table->tb6_lock));
1656 struct fib6_info *new_fn_leaf;
1658 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1661 WARN_ON(fn->fn_flags & RTN_RTINFO);
1662 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1668 child = fn_r, children |= 1;
1670 child = fn_l, children |= 2;
1672 if (children == 3 || FIB6_SUBTREE(fn)
1673 #ifdef CONFIG_IPV6_SUBTREES
1674 /* Subtree root (i.e. fn) may have one child */
1675 || (children && fn->fn_flags & RTN_ROOT)
1678 new_fn_leaf = fib6_find_prefix(net, table, fn);
1681 WARN_ON(!new_fn_leaf);
1682 new_fn_leaf = net->ipv6.fib6_null_entry;
1685 fib6_info_hold(new_fn_leaf);
1686 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1690 #ifdef CONFIG_IPV6_SUBTREES
1691 if (FIB6_SUBTREE(pn) == fn) {
1692 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1693 RCU_INIT_POINTER(pn->subtree, NULL);
1696 WARN_ON(fn->fn_flags & RTN_ROOT);
1699 rcu_assign_pointer(pn->right, child);
1700 else if (pn_l == fn)
1701 rcu_assign_pointer(pn->left, child);
1707 rcu_assign_pointer(child->parent, pn);
1709 #ifdef CONFIG_IPV6_SUBTREES
1713 read_lock(&net->ipv6.fib6_walker_lock);
1714 FOR_WALKERS(net, w) {
1716 if (w->node == fn) {
1717 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1722 if (w->node == fn) {
1725 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1726 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1728 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1729 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1734 read_unlock(&net->ipv6.fib6_walker_lock);
1737 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1740 RCU_INIT_POINTER(pn->leaf, NULL);
1741 fib6_info_release(pn_leaf);
1746 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1747 struct fib6_info __rcu **rtp, struct nl_info *info)
1749 struct fib6_walker *w;
1750 struct fib6_info *rt = rcu_dereference_protected(*rtp,
1751 lockdep_is_held(&table->tb6_lock));
1752 struct net *net = info->nl_net;
1754 RT6_TRACE("fib6_del_route\n");
1757 *rtp = rt->fib6_next;
1758 rt->fib6_node = NULL;
1759 net->ipv6.rt6_stats->fib_rt_entries--;
1760 net->ipv6.rt6_stats->fib_discarded_routes++;
1762 /* Reset round-robin state, if necessary */
1763 if (rcu_access_pointer(fn->rr_ptr) == rt)
1766 /* Remove this entry from other siblings */
1767 if (rt->fib6_nsiblings) {
1768 struct fib6_info *sibling, *next_sibling;
1770 list_for_each_entry_safe(sibling, next_sibling,
1771 &rt->fib6_siblings, fib6_siblings)
1772 sibling->fib6_nsiblings--;
1773 rt->fib6_nsiblings = 0;
1774 list_del_init(&rt->fib6_siblings);
1775 rt6_multipath_rebalance(next_sibling);
1778 /* Adjust walkers */
1779 read_lock(&net->ipv6.fib6_walker_lock);
1780 FOR_WALKERS(net, w) {
1781 if (w->state == FWS_C && w->leaf == rt) {
1782 RT6_TRACE("walker %p adjusted by delroute\n", w);
1783 w->leaf = rcu_dereference_protected(rt->fib6_next,
1784 lockdep_is_held(&table->tb6_lock));
1789 read_unlock(&net->ipv6.fib6_walker_lock);
1791 /* If it was last route, call fib6_repair_tree() to:
1792 * 1. For root node, put back null_entry as how the table was created.
1793 * 2. For other nodes, expunge its radix tree node.
1795 if (!rcu_access_pointer(fn->leaf)) {
1796 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1797 fn->fn_flags &= ~RTN_RTINFO;
1798 net->ipv6.rt6_stats->fib_route_nodes--;
1800 fn = fib6_repair_tree(net, table, fn);
1803 fib6_purge_rt(rt, fn, net);
1805 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
1806 if (!info->skip_notify)
1807 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
1808 fib6_info_release(rt);
1811 /* Need to own table->tb6_lock */
1812 int fib6_del(struct fib6_info *rt, struct nl_info *info)
1814 struct net *net = info->nl_net;
1815 struct fib6_info __rcu **rtp;
1816 struct fib6_info __rcu **rtp_next;
1817 struct fib6_table *table;
1818 struct fib6_node *fn;
1820 if (rt == net->ipv6.fib6_null_entry)
1823 table = rt->fib6_table;
1824 fn = rcu_dereference_protected(rt->fib6_node,
1825 lockdep_is_held(&table->tb6_lock));
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 = {
1986 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1987 READ_ONCE(w->node->fn_sernum) != c->sernum)
1988 WRITE_ONCE(w->node->fn_sernum, c->sernum);
1991 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1996 for_each_fib6_walker_rt(w) {
1997 res = c->func(rt, c->arg);
2000 res = fib6_del(rt, &info);
2003 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
2005 rcu_access_pointer(rt->fib6_node),
2011 } else if (res == -2) {
2012 if (WARN_ON(!rt->fib6_nsiblings))
2014 rt = list_last_entry(&rt->fib6_siblings,
2015 struct fib6_info, fib6_siblings);
2025 * Convenient frontend to tree walker.
2027 * func is called on each route.
2028 * It may return -2 -> skip multipath route.
2029 * -1 -> delete this route.
2030 * 0 -> continue walking
2033 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2034 int (*func)(struct fib6_info *, void *arg),
2035 int sernum, void *arg)
2037 struct fib6_cleaner c;
2040 c.w.func = fib6_clean_node;
2048 fib6_walk(net, &c.w);
2051 static void __fib6_clean_all(struct net *net,
2052 int (*func)(struct fib6_info *, void *),
2053 int sernum, void *arg)
2055 struct fib6_table *table;
2056 struct hlist_head *head;
2060 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2061 head = &net->ipv6.fib_table_hash[h];
2062 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2063 spin_lock_bh(&table->tb6_lock);
2064 fib6_clean_tree(net, &table->tb6_root,
2066 spin_unlock_bh(&table->tb6_lock);
2072 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
2075 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
2078 static void fib6_flush_trees(struct net *net)
2080 int new_sernum = fib6_new_sernum(net);
2082 __fib6_clean_all(net, NULL, new_sernum, NULL);
2086 * Garbage collection
2089 static int fib6_age(struct fib6_info *rt, void *arg)
2091 struct fib6_gc_args *gc_args = arg;
2092 unsigned long now = jiffies;
2095 * check addrconf expiration here.
2096 * Routes are expired even if they are in use.
2099 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
2100 if (time_after(now, rt->expires)) {
2101 RT6_TRACE("expiring %p\n", rt);
2107 /* Also age clones in the exception table.
2108 * Note, that clones are aged out
2109 * only if they are not in use now.
2111 rt6_age_exceptions(rt, gc_args, now);
2116 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2118 struct fib6_gc_args gc_args;
2122 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2123 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2124 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2127 gc_args.timeout = expires ? (int)expires :
2128 net->ipv6.sysctl.ip6_rt_gc_interval;
2131 fib6_clean_all(net, fib6_age, &gc_args);
2133 net->ipv6.ip6_rt_last_gc = now;
2136 mod_timer(&net->ipv6.ip6_fib_timer,
2138 + net->ipv6.sysctl.ip6_rt_gc_interval));
2140 del_timer(&net->ipv6.ip6_fib_timer);
2141 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2144 static void fib6_gc_timer_cb(struct timer_list *t)
2146 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2148 fib6_run_gc(0, arg, true);
2151 static int __net_init fib6_net_init(struct net *net)
2153 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2156 err = fib6_notifier_init(net);
2160 spin_lock_init(&net->ipv6.fib6_gc_lock);
2161 rwlock_init(&net->ipv6.fib6_walker_lock);
2162 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2163 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2165 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2166 if (!net->ipv6.rt6_stats)
2169 /* Avoid false sharing : Use at least a full cache line */
2170 size = max_t(size_t, size, L1_CACHE_BYTES);
2172 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2173 if (!net->ipv6.fib_table_hash)
2176 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2178 if (!net->ipv6.fib6_main_tbl)
2179 goto out_fib_table_hash;
2181 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2182 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2183 net->ipv6.fib6_null_entry);
2184 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2185 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2186 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2188 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2189 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2191 if (!net->ipv6.fib6_local_tbl)
2192 goto out_fib6_main_tbl;
2193 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2194 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2195 net->ipv6.fib6_null_entry);
2196 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2197 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2198 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2200 fib6_tables_init(net);
2204 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2206 kfree(net->ipv6.fib6_main_tbl);
2209 kfree(net->ipv6.fib_table_hash);
2211 kfree(net->ipv6.rt6_stats);
2213 fib6_notifier_exit(net);
2217 static void fib6_net_exit(struct net *net)
2221 del_timer_sync(&net->ipv6.ip6_fib_timer);
2223 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2224 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2225 struct hlist_node *tmp;
2226 struct fib6_table *tb;
2228 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2229 hlist_del(&tb->tb6_hlist);
2230 fib6_free_table(tb);
2234 kfree(net->ipv6.fib_table_hash);
2235 kfree(net->ipv6.rt6_stats);
2236 fib6_notifier_exit(net);
2239 static struct pernet_operations fib6_net_ops = {
2240 .init = fib6_net_init,
2241 .exit = fib6_net_exit,
2244 int __init fib6_init(void)
2248 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2249 sizeof(struct fib6_node),
2250 0, SLAB_HWCACHE_ALIGN,
2252 if (!fib6_node_kmem)
2255 ret = register_pernet_subsys(&fib6_net_ops);
2257 goto out_kmem_cache_create;
2259 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2262 goto out_unregister_subsys;
2264 __fib6_flush_trees = fib6_flush_trees;
2268 out_unregister_subsys:
2269 unregister_pernet_subsys(&fib6_net_ops);
2270 out_kmem_cache_create:
2271 kmem_cache_destroy(fib6_node_kmem);
2275 void fib6_gc_cleanup(void)
2277 unregister_pernet_subsys(&fib6_net_ops);
2278 kmem_cache_destroy(fib6_node_kmem);
2281 #ifdef CONFIG_PROC_FS
2282 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2284 struct fib6_info *rt = v;
2285 struct ipv6_route_iter *iter = seq->private;
2286 const struct net_device *dev;
2288 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
2290 #ifdef CONFIG_IPV6_SUBTREES
2291 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
2293 seq_puts(seq, "00000000000000000000000000000000 00 ");
2295 if (rt->fib6_flags & RTF_GATEWAY)
2296 seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
2298 seq_puts(seq, "00000000000000000000000000000000");
2300 dev = rt->fib6_nh.nh_dev;
2301 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2302 rt->fib6_metric, atomic_read(&rt->fib6_ref), 0,
2303 rt->fib6_flags, dev ? dev->name : "");
2304 iter->w.leaf = NULL;
2308 static int ipv6_route_yield(struct fib6_walker *w)
2310 struct ipv6_route_iter *iter = w->args;
2316 iter->w.leaf = rcu_dereference_protected(
2317 iter->w.leaf->fib6_next,
2318 lockdep_is_held(&iter->tbl->tb6_lock));
2320 if (!iter->skip && iter->w.leaf)
2322 } while (iter->w.leaf);
2327 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2330 memset(&iter->w, 0, sizeof(iter->w));
2331 iter->w.func = ipv6_route_yield;
2332 iter->w.root = &iter->tbl->tb6_root;
2333 iter->w.state = FWS_INIT;
2334 iter->w.node = iter->w.root;
2335 iter->w.args = iter;
2336 iter->sernum = READ_ONCE(iter->w.root->fn_sernum);
2337 INIT_LIST_HEAD(&iter->w.lh);
2338 fib6_walker_link(net, &iter->w);
2341 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2345 struct hlist_node *node;
2348 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2349 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2355 while (!node && h < FIB6_TABLE_HASHSZ) {
2356 node = rcu_dereference_bh(
2357 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2359 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2362 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2364 int sernum = READ_ONCE(iter->w.root->fn_sernum);
2366 if (iter->sernum != sernum) {
2367 iter->sernum = sernum;
2368 iter->w.state = FWS_INIT;
2369 iter->w.node = iter->w.root;
2370 WARN_ON(iter->w.skip);
2371 iter->w.skip = iter->w.count;
2375 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2378 struct fib6_info *n;
2379 struct net *net = seq_file_net(seq);
2380 struct ipv6_route_iter *iter = seq->private;
2386 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
2391 ipv6_route_check_sernum(iter);
2392 spin_lock_bh(&iter->tbl->tb6_lock);
2393 r = fib6_walk_continue(&iter->w);
2394 spin_unlock_bh(&iter->tbl->tb6_lock);
2396 return iter->w.leaf;
2398 fib6_walker_unlink(net, &iter->w);
2401 fib6_walker_unlink(net, &iter->w);
2403 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2407 ipv6_route_seq_setup_walk(iter, net);
2411 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2414 struct net *net = seq_file_net(seq);
2415 struct ipv6_route_iter *iter = seq->private;
2418 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2424 ipv6_route_seq_setup_walk(iter, net);
2425 return ipv6_route_seq_next(seq, NULL, &p);
2431 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2433 struct fib6_walker *w = &iter->w;
2434 return w->node && !(w->state == FWS_U && w->node == w->root);
2437 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2440 struct net *net = seq_file_net(seq);
2441 struct ipv6_route_iter *iter = seq->private;
2443 if (ipv6_route_iter_active(iter))
2444 fib6_walker_unlink(net, &iter->w);
2446 rcu_read_unlock_bh();
2449 const struct seq_operations ipv6_route_seq_ops = {
2450 .start = ipv6_route_seq_start,
2451 .next = ipv6_route_seq_next,
2452 .stop = ipv6_route_seq_stop,
2453 .show = ipv6_route_seq_show
2455 #endif /* CONFIG_PROC_FS */