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 rt6_info *, void *arg);
51 #ifdef CONFIG_IPV6_SUBTREES
52 #define FWS_INIT FWS_S
54 #define FWS_INIT FWS_L
57 static struct rt6_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 rt6_info *rt)
110 struct fib6_node *fn;
112 fn = rcu_dereference_protected(rt->rt6i_node,
113 lockdep_is_held(&rt->rt6i_table->tb6_lock));
115 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 rt6_info *fib6_info_alloc(gfp_t gfp_flags)
150 struct rt6_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->rt6i_siblings);
163 f6i->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
165 atomic_inc(&f6i->rt6i_ref);
170 void fib6_info_destroy(struct rt6_info *f6i)
172 struct rt6_exception_bucket *bucket;
173 struct dst_metrics *m;
175 WARN_ON(f6i->rt6i_node);
177 bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
179 f6i->rt6i_exception_bucket = NULL;
183 if (f6i->rt6i_pcpu) {
186 for_each_possible_cpu(cpu) {
187 struct rt6_info **ppcpu_rt;
188 struct rt6_info *pcpu_rt;
190 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
193 dst_dev_put(&pcpu_rt->dst);
194 dst_release(&pcpu_rt->dst);
201 in6_dev_put(f6i->rt6i_idev);
202 if (f6i->fib6_nh.nh_dev)
203 dev_put(f6i->fib6_nh.nh_dev);
205 m = f6i->fib6_metrics;
206 if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
211 EXPORT_SYMBOL_GPL(fib6_info_destroy);
213 static struct fib6_node *node_alloc(struct net *net)
215 struct fib6_node *fn;
217 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
219 net->ipv6.rt6_stats->fib_nodes++;
224 static void node_free_immediate(struct net *net, struct fib6_node *fn)
226 kmem_cache_free(fib6_node_kmem, fn);
227 net->ipv6.rt6_stats->fib_nodes--;
230 static void node_free_rcu(struct rcu_head *head)
232 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
234 kmem_cache_free(fib6_node_kmem, fn);
237 static void node_free(struct net *net, struct fib6_node *fn)
239 call_rcu(&fn->rcu, node_free_rcu);
240 net->ipv6.rt6_stats->fib_nodes--;
243 void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
247 if (!non_pcpu_rt->rt6i_pcpu)
250 for_each_possible_cpu(cpu) {
251 struct rt6_info **ppcpu_rt;
252 struct rt6_info *pcpu_rt;
254 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
257 dst_dev_put(&pcpu_rt->dst);
258 dst_release(&pcpu_rt->dst);
263 EXPORT_SYMBOL_GPL(rt6_free_pcpu);
265 static void fib6_free_table(struct fib6_table *table)
267 inetpeer_invalidate_tree(&table->tb6_peers);
271 static void fib6_link_table(struct net *net, struct fib6_table *tb)
276 * Initialize table lock at a single place to give lockdep a key,
277 * tables aren't visible prior to being linked to the list.
279 spin_lock_init(&tb->tb6_lock);
280 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
283 * No protection necessary, this is the only list mutatation
284 * operation, tables never disappear once they exist.
286 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
289 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
291 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
293 struct fib6_table *table;
295 table = kzalloc(sizeof(*table), GFP_ATOMIC);
298 rcu_assign_pointer(table->tb6_root.leaf,
299 net->ipv6.fib6_null_entry);
300 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
301 inet_peer_base_init(&table->tb6_peers);
307 struct fib6_table *fib6_new_table(struct net *net, u32 id)
309 struct fib6_table *tb;
313 tb = fib6_get_table(net, id);
317 tb = fib6_alloc_table(net, id);
319 fib6_link_table(net, tb);
323 EXPORT_SYMBOL_GPL(fib6_new_table);
325 struct fib6_table *fib6_get_table(struct net *net, u32 id)
327 struct fib6_table *tb;
328 struct hlist_head *head;
333 h = id & (FIB6_TABLE_HASHSZ - 1);
335 head = &net->ipv6.fib_table_hash[h];
336 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
337 if (tb->tb6_id == id) {
346 EXPORT_SYMBOL_GPL(fib6_get_table);
348 static void __net_init fib6_tables_init(struct net *net)
350 fib6_link_table(net, net->ipv6.fib6_main_tbl);
351 fib6_link_table(net, net->ipv6.fib6_local_tbl);
355 struct fib6_table *fib6_new_table(struct net *net, u32 id)
357 return fib6_get_table(net, id);
360 struct fib6_table *fib6_get_table(struct net *net, u32 id)
362 return net->ipv6.fib6_main_tbl;
365 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
366 const struct sk_buff *skb,
367 int flags, pol_lookup_t lookup)
371 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
372 if (rt->dst.error == -EAGAIN) {
374 rt = net->ipv6.ip6_null_entry;
381 static void __net_init fib6_tables_init(struct net *net)
383 fib6_link_table(net, net->ipv6.fib6_main_tbl);
388 unsigned int fib6_tables_seq_read(struct net *net)
390 unsigned int h, fib_seq = 0;
393 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
394 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
395 struct fib6_table *tb;
397 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
398 fib_seq += tb->fib_seq;
405 static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
406 enum fib_event_type event_type,
409 struct fib6_entry_notifier_info info = {
413 return call_fib6_notifier(nb, net, event_type, &info.info);
416 static int call_fib6_entry_notifiers(struct net *net,
417 enum fib_event_type event_type,
419 struct netlink_ext_ack *extack)
421 struct fib6_entry_notifier_info info = {
422 .info.extack = extack,
426 rt->rt6i_table->fib_seq++;
427 return call_fib6_notifiers(net, event_type, &info.info);
430 struct fib6_dump_arg {
432 struct notifier_block *nb;
435 static void fib6_rt_dump(struct rt6_info *rt, struct fib6_dump_arg *arg)
437 if (rt == arg->net->ipv6.fib6_null_entry)
439 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
442 static int fib6_node_dump(struct fib6_walker *w)
446 for_each_fib6_walker_rt(w)
447 fib6_rt_dump(rt, w->args);
452 static void fib6_table_dump(struct net *net, struct fib6_table *tb,
453 struct fib6_walker *w)
455 w->root = &tb->tb6_root;
456 spin_lock_bh(&tb->tb6_lock);
458 spin_unlock_bh(&tb->tb6_lock);
461 /* Called with rcu_read_lock() */
462 int fib6_tables_dump(struct net *net, struct notifier_block *nb)
464 struct fib6_dump_arg arg;
465 struct fib6_walker *w;
468 w = kzalloc(sizeof(*w), GFP_ATOMIC);
472 w->func = fib6_node_dump;
477 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
478 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
479 struct fib6_table *tb;
481 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
482 fib6_table_dump(net, tb, w);
490 static int fib6_dump_node(struct fib6_walker *w)
495 for_each_fib6_walker_rt(w) {
496 res = rt6_dump_route(rt, w->args);
498 /* Frame is full, suspend walking */
503 /* Multipath routes are dumped in one route with the
504 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
505 * last sibling of this route (no need to dump the
506 * sibling routes again)
508 if (rt->rt6i_nsiblings)
509 rt = list_last_entry(&rt->rt6i_siblings,
517 static void fib6_dump_end(struct netlink_callback *cb)
519 struct net *net = sock_net(cb->skb->sk);
520 struct fib6_walker *w = (void *)cb->args[2];
525 fib6_walker_unlink(net, w);
530 cb->done = (void *)cb->args[3];
534 static int fib6_dump_done(struct netlink_callback *cb)
537 return cb->done ? cb->done(cb) : 0;
540 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
541 struct netlink_callback *cb)
543 struct net *net = sock_net(skb->sk);
544 struct fib6_walker *w;
547 w = (void *)cb->args[2];
548 w->root = &table->tb6_root;
550 if (cb->args[4] == 0) {
554 spin_lock_bh(&table->tb6_lock);
555 res = fib6_walk(net, w);
556 spin_unlock_bh(&table->tb6_lock);
559 cb->args[5] = w->root->fn_sernum;
562 if (cb->args[5] != w->root->fn_sernum) {
563 /* Begin at the root if the tree changed */
564 cb->args[5] = w->root->fn_sernum;
571 spin_lock_bh(&table->tb6_lock);
572 res = fib6_walk_continue(w);
573 spin_unlock_bh(&table->tb6_lock);
575 fib6_walker_unlink(net, w);
583 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
585 struct net *net = sock_net(skb->sk);
587 unsigned int e = 0, s_e;
588 struct rt6_rtnl_dump_arg arg;
589 struct fib6_walker *w;
590 struct fib6_table *tb;
591 struct hlist_head *head;
597 w = (void *)cb->args[2];
601 * 1. hook callback destructor.
603 cb->args[3] = (long)cb->done;
604 cb->done = fib6_dump_done;
607 * 2. allocate and initialize walker.
609 w = kzalloc(sizeof(*w), GFP_ATOMIC);
612 w->func = fib6_dump_node;
613 cb->args[2] = (long)w;
622 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
624 head = &net->ipv6.fib_table_hash[h];
625 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
628 res = fib6_dump_table(tb, skb, cb);
640 res = res < 0 ? res : skb->len;
646 void fib6_metric_set(struct rt6_info *f6i, int metric, u32 val)
651 if (f6i->fib6_metrics == &dst_default_metrics) {
652 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
657 refcount_set(&p->refcnt, 1);
658 f6i->fib6_metrics = p;
661 f6i->fib6_metrics->metrics[metric - 1] = val;
667 * return the appropriate node for a routing tree "add" operation
668 * by either creating and inserting or by returning an existing
672 static struct fib6_node *fib6_add_1(struct net *net,
673 struct fib6_table *table,
674 struct fib6_node *root,
675 struct in6_addr *addr, int plen,
676 int offset, int allow_create,
677 int replace_required,
678 struct netlink_ext_ack *extack)
680 struct fib6_node *fn, *in, *ln;
681 struct fib6_node *pn = NULL;
686 RT6_TRACE("fib6_add_1\n");
688 /* insert node in tree */
693 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
694 lockdep_is_held(&table->tb6_lock));
695 key = (struct rt6key *)((u8 *)leaf + offset);
700 if (plen < fn->fn_bit ||
701 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
703 if (replace_required) {
704 NL_SET_ERR_MSG(extack,
705 "Can not replace route - no match found");
706 pr_warn("Can't replace route, no match found\n");
707 return ERR_PTR(-ENOENT);
709 pr_warn("NLM_F_CREATE should be set when creating new route\n");
718 if (plen == fn->fn_bit) {
719 /* clean up an intermediate node */
720 if (!(fn->fn_flags & RTN_RTINFO)) {
721 RCU_INIT_POINTER(fn->leaf, NULL);
722 fib6_info_release(leaf);
723 /* remove null_entry in the root node */
724 } else if (fn->fn_flags & RTN_TL_ROOT &&
725 rcu_access_pointer(fn->leaf) ==
726 net->ipv6.fib6_null_entry) {
727 RCU_INIT_POINTER(fn->leaf, NULL);
734 * We have more bits to go
737 /* Try to walk down on tree. */
738 dir = addr_bit_set(addr, fn->fn_bit);
741 rcu_dereference_protected(fn->right,
742 lockdep_is_held(&table->tb6_lock)) :
743 rcu_dereference_protected(fn->left,
744 lockdep_is_held(&table->tb6_lock));
748 /* We should not create new node because
749 * NLM_F_REPLACE was specified without NLM_F_CREATE
750 * I assume it is safe to require NLM_F_CREATE when
751 * REPLACE flag is used! Later we may want to remove the
752 * check for replace_required, because according
753 * to netlink specification, NLM_F_CREATE
754 * MUST be specified if new route is created.
755 * That would keep IPv6 consistent with IPv4
757 if (replace_required) {
758 NL_SET_ERR_MSG(extack,
759 "Can not replace route - no match found");
760 pr_warn("Can't replace route, no match found\n");
761 return ERR_PTR(-ENOENT);
763 pr_warn("NLM_F_CREATE should be set when creating new route\n");
766 * We walked to the bottom of tree.
767 * Create new leaf node without children.
770 ln = node_alloc(net);
773 return ERR_PTR(-ENOMEM);
775 RCU_INIT_POINTER(ln->parent, pn);
778 rcu_assign_pointer(pn->right, ln);
780 rcu_assign_pointer(pn->left, ln);
787 * split since we don't have a common prefix anymore or
788 * we have a less significant route.
789 * we've to insert an intermediate node on the list
790 * this new node will point to the one we need to create
794 pn = rcu_dereference_protected(fn->parent,
795 lockdep_is_held(&table->tb6_lock));
797 /* find 1st bit in difference between the 2 addrs.
799 See comment in __ipv6_addr_diff: bit may be an invalid value,
800 but if it is >= plen, the value is ignored in any case.
803 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
808 * (new leaf node)[ln] (old node)[fn]
811 in = node_alloc(net);
812 ln = node_alloc(net);
816 node_free_immediate(net, in);
818 node_free_immediate(net, ln);
819 return ERR_PTR(-ENOMEM);
823 * new intermediate node.
825 * be off since that an address that chooses one of
826 * the branches would not match less specific routes
827 * in the other branch
832 RCU_INIT_POINTER(in->parent, pn);
834 atomic_inc(&rcu_dereference_protected(in->leaf,
835 lockdep_is_held(&table->tb6_lock))->rt6i_ref);
837 /* update parent pointer */
839 rcu_assign_pointer(pn->right, in);
841 rcu_assign_pointer(pn->left, in);
845 RCU_INIT_POINTER(ln->parent, in);
846 rcu_assign_pointer(fn->parent, in);
848 if (addr_bit_set(addr, bit)) {
849 rcu_assign_pointer(in->right, ln);
850 rcu_assign_pointer(in->left, fn);
852 rcu_assign_pointer(in->left, ln);
853 rcu_assign_pointer(in->right, fn);
855 } else { /* plen <= bit */
858 * (new leaf node)[ln]
860 * (old node)[fn] NULL
863 ln = node_alloc(net);
866 return ERR_PTR(-ENOMEM);
870 RCU_INIT_POINTER(ln->parent, pn);
872 if (addr_bit_set(&key->addr, plen))
873 RCU_INIT_POINTER(ln->right, fn);
875 RCU_INIT_POINTER(ln->left, fn);
877 rcu_assign_pointer(fn->parent, ln);
880 rcu_assign_pointer(pn->right, ln);
882 rcu_assign_pointer(pn->left, ln);
887 static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
890 struct fib6_table *table = rt->rt6i_table;
892 if (atomic_read(&rt->rt6i_ref) != 1) {
893 /* This route is used as dummy address holder in some split
894 * nodes. It is not leaked, but it still holds other resources,
895 * which must be released in time. So, scan ascendant nodes
896 * and replace dummy references to this route with references
897 * to still alive ones.
900 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
901 lockdep_is_held(&table->tb6_lock));
902 struct rt6_info *new_leaf;
903 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
904 new_leaf = fib6_find_prefix(net, table, fn);
905 atomic_inc(&new_leaf->rt6i_ref);
907 rcu_assign_pointer(fn->leaf, new_leaf);
908 fib6_info_release(rt);
910 fn = rcu_dereference_protected(fn->parent,
911 lockdep_is_held(&table->tb6_lock));
917 /* release the reference to this fib entry from
918 * all of its cached pcpu routes
920 for_each_possible_cpu(cpu) {
921 struct rt6_info **ppcpu_rt;
922 struct rt6_info *pcpu_rt;
924 ppcpu_rt = per_cpu_ptr(rt->rt6i_pcpu, cpu);
927 fib6_info_release(pcpu_rt->from);
928 pcpu_rt->from = NULL;
936 * Insert routing information in a node.
939 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
940 struct nl_info *info,
941 struct netlink_ext_ack *extack)
943 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
944 lockdep_is_held(&rt->rt6i_table->tb6_lock));
945 struct rt6_info *iter = NULL;
946 struct rt6_info __rcu **ins;
947 struct rt6_info __rcu **fallback_ins = NULL;
948 int replace = (info->nlh &&
949 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
950 int add = (!info->nlh ||
951 (info->nlh->nlmsg_flags & NLM_F_CREATE));
953 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
954 u16 nlflags = NLM_F_EXCL;
957 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
958 nlflags |= NLM_F_APPEND;
962 for (iter = leaf; iter;
963 iter = rcu_dereference_protected(iter->rt6_next,
964 lockdep_is_held(&rt->rt6i_table->tb6_lock))) {
966 * Search for duplicates
969 if (iter->rt6i_metric == rt->rt6i_metric) {
971 * Same priority level
974 (info->nlh->nlmsg_flags & NLM_F_EXCL))
977 nlflags &= ~NLM_F_EXCL;
979 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
984 fallback_ins = fallback_ins ?: ins;
988 if (rt6_duplicate_nexthop(iter, rt)) {
989 if (rt->rt6i_nsiblings)
990 rt->rt6i_nsiblings = 0;
991 if (!(iter->rt6i_flags & RTF_EXPIRES))
993 if (!(rt->rt6i_flags & RTF_EXPIRES))
994 fib6_clean_expires(iter);
996 fib6_set_expires(iter, rt->expires);
997 fib6_metric_set(iter, RTAX_MTU, rt->fib6_pmtu);
1000 /* If we have the same destination and the same metric,
1001 * but not the same gateway, then the route we try to
1002 * add is sibling to this route, increment our counter
1003 * of siblings, and later we will add our route to the
1005 * Only static routes (which don't have flag
1006 * RTF_EXPIRES) are used for ECMPv6.
1008 * To avoid long list, we only had siblings if the
1009 * route have a gateway.
1012 rt6_qualify_for_ecmp(iter))
1013 rt->rt6i_nsiblings++;
1016 if (iter->rt6i_metric > rt->rt6i_metric)
1020 ins = &iter->rt6_next;
1023 if (fallback_ins && !found) {
1024 /* No ECMP-able route found, replace first non-ECMP one */
1026 iter = rcu_dereference_protected(*ins,
1027 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1031 /* Reset round-robin state, if necessary */
1032 if (ins == &fn->leaf)
1035 /* Link this route to others same route. */
1036 if (rt->rt6i_nsiblings) {
1037 unsigned int rt6i_nsiblings;
1038 struct rt6_info *sibling, *temp_sibling;
1040 /* Find the first route that have the same metric */
1043 if (sibling->rt6i_metric == rt->rt6i_metric &&
1044 rt6_qualify_for_ecmp(sibling)) {
1045 list_add_tail(&rt->rt6i_siblings,
1046 &sibling->rt6i_siblings);
1049 sibling = rcu_dereference_protected(sibling->rt6_next,
1050 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1052 /* For each sibling in the list, increment the counter of
1053 * siblings. BUG() if counters does not match, list of siblings
1057 list_for_each_entry_safe(sibling, temp_sibling,
1058 &rt->rt6i_siblings, rt6i_siblings) {
1059 sibling->rt6i_nsiblings++;
1060 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
1063 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
1064 rt6_multipath_rebalance(temp_sibling);
1072 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1075 nlflags |= NLM_F_CREATE;
1077 err = call_fib6_entry_notifiers(info->nl_net,
1078 FIB_EVENT_ENTRY_ADD,
1083 rcu_assign_pointer(rt->rt6_next, iter);
1084 atomic_inc(&rt->rt6i_ref);
1085 rcu_assign_pointer(rt->rt6i_node, fn);
1086 rcu_assign_pointer(*ins, rt);
1087 if (!info->skip_notify)
1088 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1089 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1091 if (!(fn->fn_flags & RTN_RTINFO)) {
1092 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1093 fn->fn_flags |= RTN_RTINFO;
1102 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1106 err = call_fib6_entry_notifiers(info->nl_net,
1107 FIB_EVENT_ENTRY_REPLACE,
1112 atomic_inc(&rt->rt6i_ref);
1113 rcu_assign_pointer(rt->rt6i_node, fn);
1114 rt->rt6_next = iter->rt6_next;
1115 rcu_assign_pointer(*ins, rt);
1116 if (!info->skip_notify)
1117 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1118 if (!(fn->fn_flags & RTN_RTINFO)) {
1119 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1120 fn->fn_flags |= RTN_RTINFO;
1122 nsiblings = iter->rt6i_nsiblings;
1123 iter->rt6i_node = NULL;
1124 fib6_purge_rt(iter, fn, info->nl_net);
1125 if (rcu_access_pointer(fn->rr_ptr) == iter)
1127 fib6_info_release(iter);
1130 /* Replacing an ECMP route, remove all siblings */
1131 ins = &rt->rt6_next;
1132 iter = rcu_dereference_protected(*ins,
1133 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1135 if (iter->rt6i_metric > rt->rt6i_metric)
1137 if (rt6_qualify_for_ecmp(iter)) {
1138 *ins = iter->rt6_next;
1139 iter->rt6i_node = NULL;
1140 fib6_purge_rt(iter, fn, info->nl_net);
1141 if (rcu_access_pointer(fn->rr_ptr) == iter)
1143 fib6_info_release(iter);
1145 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1147 ins = &iter->rt6_next;
1149 iter = rcu_dereference_protected(*ins,
1150 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1152 WARN_ON(nsiblings != 0);
1159 static void fib6_start_gc(struct net *net, struct rt6_info *rt)
1161 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1162 (rt->rt6i_flags & RTF_EXPIRES))
1163 mod_timer(&net->ipv6.ip6_fib_timer,
1164 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1167 void fib6_force_start_gc(struct net *net)
1169 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1170 mod_timer(&net->ipv6.ip6_fib_timer,
1171 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1174 static void __fib6_update_sernum_upto_root(struct rt6_info *rt,
1177 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1178 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1180 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1183 fn->fn_sernum = sernum;
1184 fn = rcu_dereference_protected(fn->parent,
1185 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1189 void fib6_update_sernum_upto_root(struct net *net, struct rt6_info *rt)
1191 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1195 * Add routing information to the routing tree.
1196 * <destination addr>/<source addr>
1197 * with source addr info in sub-trees
1198 * Need to own table->tb6_lock
1201 int fib6_add(struct fib6_node *root, struct rt6_info *rt,
1202 struct nl_info *info, struct netlink_ext_ack *extack)
1204 struct fib6_table *table = rt->rt6i_table;
1205 struct fib6_node *fn, *pn = NULL;
1207 int allow_create = 1;
1208 int replace_required = 0;
1209 int sernum = fib6_new_sernum(info->nl_net);
1212 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1214 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1215 replace_required = 1;
1217 if (!allow_create && !replace_required)
1218 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1220 fn = fib6_add_1(info->nl_net, table, root,
1221 &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
1222 offsetof(struct rt6_info, rt6i_dst), allow_create,
1223 replace_required, extack);
1232 #ifdef CONFIG_IPV6_SUBTREES
1233 if (rt->rt6i_src.plen) {
1234 struct fib6_node *sn;
1236 if (!rcu_access_pointer(fn->subtree)) {
1237 struct fib6_node *sfn;
1249 /* Create subtree root node */
1250 sfn = node_alloc(info->nl_net);
1254 atomic_inc(&info->nl_net->ipv6.fib6_null_entry->rt6i_ref);
1255 rcu_assign_pointer(sfn->leaf,
1256 info->nl_net->ipv6.fib6_null_entry);
1257 sfn->fn_flags = RTN_ROOT;
1259 /* Now add the first leaf node to new subtree */
1261 sn = fib6_add_1(info->nl_net, table, sfn,
1262 &rt->rt6i_src.addr, rt->rt6i_src.plen,
1263 offsetof(struct rt6_info, rt6i_src),
1264 allow_create, replace_required, extack);
1267 /* If it is failed, discard just allocated
1268 root, and then (in failure) stale node
1271 node_free_immediate(info->nl_net, sfn);
1276 /* Now link new subtree to main tree */
1277 rcu_assign_pointer(sfn->parent, fn);
1278 rcu_assign_pointer(fn->subtree, sfn);
1280 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1281 &rt->rt6i_src.addr, rt->rt6i_src.plen,
1282 offsetof(struct rt6_info, rt6i_src),
1283 allow_create, replace_required, extack);
1291 if (!rcu_access_pointer(fn->leaf)) {
1292 if (fn->fn_flags & RTN_TL_ROOT) {
1293 /* put back null_entry for root node */
1294 rcu_assign_pointer(fn->leaf,
1295 info->nl_net->ipv6.fib6_null_entry);
1297 atomic_inc(&rt->rt6i_ref);
1298 rcu_assign_pointer(fn->leaf, rt);
1305 err = fib6_add_rt2node(fn, rt, info, extack);
1307 __fib6_update_sernum_upto_root(rt, sernum);
1308 fib6_start_gc(info->nl_net, rt);
1313 #ifdef CONFIG_IPV6_SUBTREES
1315 * If fib6_add_1 has cleared the old leaf pointer in the
1316 * super-tree leaf node we have to find a new one for it.
1319 struct rt6_info *pn_leaf =
1320 rcu_dereference_protected(pn->leaf,
1321 lockdep_is_held(&table->tb6_lock));
1322 if (pn_leaf == rt) {
1324 RCU_INIT_POINTER(pn->leaf, NULL);
1325 fib6_info_release(rt);
1327 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1328 pn_leaf = fib6_find_prefix(info->nl_net, table,
1334 info->nl_net->ipv6.fib6_null_entry;
1337 fib6_info_hold(pn_leaf);
1338 rcu_assign_pointer(pn->leaf, pn_leaf);
1347 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1348 * 1. fn is an intermediate node and we failed to add the new
1349 * route to it in both subtree creation failure and fib6_add_rt2node()
1351 * 2. fn is the root node in the table and we fail to add the first
1352 * default route to it.
1355 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1356 (fn->fn_flags & RTN_TL_ROOT &&
1357 !rcu_access_pointer(fn->leaf))))
1358 fib6_repair_tree(info->nl_net, table, fn);
1363 * Routing tree lookup
1367 struct lookup_args {
1368 int offset; /* key offset on rt6_info */
1369 const struct in6_addr *addr; /* search key */
1372 static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1373 struct lookup_args *args)
1375 struct fib6_node *fn;
1378 if (unlikely(args->offset == 0))
1388 struct fib6_node *next;
1390 dir = addr_bit_set(args->addr, fn->fn_bit);
1392 next = dir ? rcu_dereference(fn->right) :
1393 rcu_dereference(fn->left);
1403 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1405 if (subtree || fn->fn_flags & RTN_RTINFO) {
1406 struct rt6_info *leaf = rcu_dereference(fn->leaf);
1412 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1414 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1415 #ifdef CONFIG_IPV6_SUBTREES
1417 struct fib6_node *sfn;
1418 sfn = fib6_lookup_1(subtree, args + 1);
1424 if (fn->fn_flags & RTN_RTINFO)
1429 if (fn->fn_flags & RTN_ROOT)
1432 fn = rcu_dereference(fn->parent);
1438 /* called with rcu_read_lock() held
1440 struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1441 const struct in6_addr *saddr)
1443 struct fib6_node *fn;
1444 struct lookup_args args[] = {
1446 .offset = offsetof(struct rt6_info, rt6i_dst),
1449 #ifdef CONFIG_IPV6_SUBTREES
1451 .offset = offsetof(struct rt6_info, rt6i_src),
1456 .offset = 0, /* sentinel */
1460 fn = fib6_lookup_1(root, daddr ? args : args + 1);
1461 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1468 * Get node with specified destination prefix (and source prefix,
1469 * if subtrees are used)
1470 * exact_match == true means we try to find fn with exact match of
1471 * the passed in prefix addr
1472 * exact_match == false means we try to find fn with longest prefix
1473 * match of the passed in prefix addr. This is useful for finding fn
1474 * for cached route as it will be stored in the exception table under
1475 * the node with longest prefix length.
1479 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1480 const struct in6_addr *addr,
1481 int plen, int offset,
1484 struct fib6_node *fn, *prev = NULL;
1486 for (fn = root; fn ; ) {
1487 struct rt6_info *leaf = rcu_dereference(fn->leaf);
1490 /* This node is being deleted */
1492 if (plen <= fn->fn_bit)
1498 key = (struct rt6key *)((u8 *)leaf + offset);
1503 if (plen < fn->fn_bit ||
1504 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1507 if (plen == fn->fn_bit)
1514 * We have more bits to go
1516 if (addr_bit_set(addr, fn->fn_bit))
1517 fn = rcu_dereference(fn->right);
1519 fn = rcu_dereference(fn->left);
1528 struct fib6_node *fib6_locate(struct fib6_node *root,
1529 const struct in6_addr *daddr, int dst_len,
1530 const struct in6_addr *saddr, int src_len,
1533 struct fib6_node *fn;
1535 fn = fib6_locate_1(root, daddr, dst_len,
1536 offsetof(struct rt6_info, rt6i_dst),
1539 #ifdef CONFIG_IPV6_SUBTREES
1541 WARN_ON(saddr == NULL);
1543 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1546 fn = fib6_locate_1(subtree, saddr, src_len,
1547 offsetof(struct rt6_info, rt6i_src),
1554 if (fn && fn->fn_flags & RTN_RTINFO)
1566 static struct rt6_info *fib6_find_prefix(struct net *net,
1567 struct fib6_table *table,
1568 struct fib6_node *fn)
1570 struct fib6_node *child_left, *child_right;
1572 if (fn->fn_flags & RTN_ROOT)
1573 return net->ipv6.fib6_null_entry;
1576 child_left = rcu_dereference_protected(fn->left,
1577 lockdep_is_held(&table->tb6_lock));
1578 child_right = rcu_dereference_protected(fn->right,
1579 lockdep_is_held(&table->tb6_lock));
1581 return rcu_dereference_protected(child_left->leaf,
1582 lockdep_is_held(&table->tb6_lock));
1584 return rcu_dereference_protected(child_right->leaf,
1585 lockdep_is_held(&table->tb6_lock));
1587 fn = FIB6_SUBTREE(fn);
1593 * Called to trim the tree of intermediate nodes when possible. "fn"
1594 * is the node we want to try and remove.
1595 * Need to own table->tb6_lock
1598 static struct fib6_node *fib6_repair_tree(struct net *net,
1599 struct fib6_table *table,
1600 struct fib6_node *fn)
1604 struct fib6_node *child;
1605 struct fib6_walker *w;
1608 /* Set fn->leaf to null_entry for root node. */
1609 if (fn->fn_flags & RTN_TL_ROOT) {
1610 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1615 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1616 lockdep_is_held(&table->tb6_lock));
1617 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1618 lockdep_is_held(&table->tb6_lock));
1619 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1620 lockdep_is_held(&table->tb6_lock));
1621 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1622 lockdep_is_held(&table->tb6_lock));
1623 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1624 lockdep_is_held(&table->tb6_lock));
1625 struct rt6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1626 lockdep_is_held(&table->tb6_lock));
1627 struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1628 lockdep_is_held(&table->tb6_lock));
1629 struct rt6_info *new_fn_leaf;
1631 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1634 WARN_ON(fn->fn_flags & RTN_RTINFO);
1635 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1641 child = fn_r, children |= 1;
1643 child = fn_l, children |= 2;
1645 if (children == 3 || FIB6_SUBTREE(fn)
1646 #ifdef CONFIG_IPV6_SUBTREES
1647 /* Subtree root (i.e. fn) may have one child */
1648 || (children && fn->fn_flags & RTN_ROOT)
1651 new_fn_leaf = fib6_find_prefix(net, table, fn);
1654 WARN_ON(!new_fn_leaf);
1655 new_fn_leaf = net->ipv6.fib6_null_entry;
1658 fib6_info_hold(new_fn_leaf);
1659 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1663 #ifdef CONFIG_IPV6_SUBTREES
1664 if (FIB6_SUBTREE(pn) == fn) {
1665 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1666 RCU_INIT_POINTER(pn->subtree, NULL);
1669 WARN_ON(fn->fn_flags & RTN_ROOT);
1672 rcu_assign_pointer(pn->right, child);
1673 else if (pn_l == fn)
1674 rcu_assign_pointer(pn->left, child);
1680 rcu_assign_pointer(child->parent, pn);
1682 #ifdef CONFIG_IPV6_SUBTREES
1686 read_lock(&net->ipv6.fib6_walker_lock);
1687 FOR_WALKERS(net, w) {
1689 if (w->node == fn) {
1690 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1695 if (w->node == fn) {
1698 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1699 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1701 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1702 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1707 read_unlock(&net->ipv6.fib6_walker_lock);
1710 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1713 RCU_INIT_POINTER(pn->leaf, NULL);
1714 fib6_info_release(pn_leaf);
1719 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1720 struct rt6_info __rcu **rtp, struct nl_info *info)
1722 struct fib6_walker *w;
1723 struct rt6_info *rt = rcu_dereference_protected(*rtp,
1724 lockdep_is_held(&table->tb6_lock));
1725 struct net *net = info->nl_net;
1727 RT6_TRACE("fib6_del_route\n");
1730 *rtp = rt->rt6_next;
1731 rt->rt6i_node = NULL;
1732 net->ipv6.rt6_stats->fib_rt_entries--;
1733 net->ipv6.rt6_stats->fib_discarded_routes++;
1735 /* Flush all cached dst in exception table */
1736 rt6_flush_exceptions(rt);
1738 /* Reset round-robin state, if necessary */
1739 if (rcu_access_pointer(fn->rr_ptr) == rt)
1742 /* Remove this entry from other siblings */
1743 if (rt->rt6i_nsiblings) {
1744 struct rt6_info *sibling, *next_sibling;
1746 list_for_each_entry_safe(sibling, next_sibling,
1747 &rt->rt6i_siblings, rt6i_siblings)
1748 sibling->rt6i_nsiblings--;
1749 rt->rt6i_nsiblings = 0;
1750 list_del_init(&rt->rt6i_siblings);
1751 rt6_multipath_rebalance(next_sibling);
1754 /* Adjust walkers */
1755 read_lock(&net->ipv6.fib6_walker_lock);
1756 FOR_WALKERS(net, w) {
1757 if (w->state == FWS_C && w->leaf == rt) {
1758 RT6_TRACE("walker %p adjusted by delroute\n", w);
1759 w->leaf = rcu_dereference_protected(rt->rt6_next,
1760 lockdep_is_held(&table->tb6_lock));
1765 read_unlock(&net->ipv6.fib6_walker_lock);
1767 /* If it was last route, call fib6_repair_tree() to:
1768 * 1. For root node, put back null_entry as how the table was created.
1769 * 2. For other nodes, expunge its radix tree node.
1771 if (!rcu_access_pointer(fn->leaf)) {
1772 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1773 fn->fn_flags &= ~RTN_RTINFO;
1774 net->ipv6.rt6_stats->fib_route_nodes--;
1776 fn = fib6_repair_tree(net, table, fn);
1779 fib6_purge_rt(rt, fn, net);
1781 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
1782 if (!info->skip_notify)
1783 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
1784 fib6_info_release(rt);
1787 /* Need to own table->tb6_lock */
1788 int fib6_del(struct rt6_info *rt, struct nl_info *info)
1790 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1791 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1792 struct fib6_table *table = rt->rt6i_table;
1793 struct net *net = info->nl_net;
1794 struct rt6_info __rcu **rtp;
1795 struct rt6_info __rcu **rtp_next;
1797 if (!fn || rt == net->ipv6.fib6_null_entry)
1800 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1803 * Walk the leaf entries looking for ourself
1806 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1807 struct rt6_info *cur = rcu_dereference_protected(*rtp,
1808 lockdep_is_held(&table->tb6_lock));
1810 fib6_del_route(table, fn, rtp, info);
1813 rtp_next = &cur->rt6_next;
1819 * Tree traversal function.
1821 * Certainly, it is not interrupt safe.
1822 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1823 * It means, that we can modify tree during walking
1824 * and use this function for garbage collection, clone pruning,
1825 * cleaning tree when a device goes down etc. etc.
1827 * It guarantees that every node will be traversed,
1828 * and that it will be traversed only once.
1830 * Callback function w->func may return:
1831 * 0 -> continue walking.
1832 * positive value -> walking is suspended (used by tree dumps,
1833 * and probably by gc, if it will be split to several slices)
1834 * negative value -> terminate walking.
1836 * The function itself returns:
1837 * 0 -> walk is complete.
1838 * >0 -> walk is incomplete (i.e. suspended)
1839 * <0 -> walk is terminated by an error.
1841 * This function is called with tb6_lock held.
1844 static int fib6_walk_continue(struct fib6_walker *w)
1846 struct fib6_node *fn, *pn, *left, *right;
1848 /* w->root should always be table->tb6_root */
1849 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1857 #ifdef CONFIG_IPV6_SUBTREES
1859 if (FIB6_SUBTREE(fn)) {
1860 w->node = FIB6_SUBTREE(fn);
1867 left = rcu_dereference_protected(fn->left, 1);
1870 w->state = FWS_INIT;
1876 right = rcu_dereference_protected(fn->right, 1);
1879 w->state = FWS_INIT;
1883 w->leaf = rcu_dereference_protected(fn->leaf, 1);
1886 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1907 pn = rcu_dereference_protected(fn->parent, 1);
1908 left = rcu_dereference_protected(pn->left, 1);
1909 right = rcu_dereference_protected(pn->right, 1);
1911 #ifdef CONFIG_IPV6_SUBTREES
1912 if (FIB6_SUBTREE(pn) == fn) {
1913 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1924 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
1934 static int fib6_walk(struct net *net, struct fib6_walker *w)
1938 w->state = FWS_INIT;
1941 fib6_walker_link(net, w);
1942 res = fib6_walk_continue(w);
1944 fib6_walker_unlink(net, w);
1948 static int fib6_clean_node(struct fib6_walker *w)
1951 struct rt6_info *rt;
1952 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1953 struct nl_info info = {
1957 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1958 w->node->fn_sernum != c->sernum)
1959 w->node->fn_sernum = c->sernum;
1962 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1967 for_each_fib6_walker_rt(w) {
1968 res = c->func(rt, c->arg);
1971 res = fib6_del(rt, &info);
1974 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1976 rcu_access_pointer(rt->rt6i_node),
1982 } else if (res == -2) {
1983 if (WARN_ON(!rt->rt6i_nsiblings))
1985 rt = list_last_entry(&rt->rt6i_siblings,
1986 struct rt6_info, rt6i_siblings);
1996 * Convenient frontend to tree walker.
1998 * func is called on each route.
1999 * It may return -2 -> skip multipath route.
2000 * -1 -> delete this route.
2001 * 0 -> continue walking
2004 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2005 int (*func)(struct rt6_info *, void *arg),
2006 int sernum, void *arg)
2008 struct fib6_cleaner c;
2011 c.w.func = fib6_clean_node;
2019 fib6_walk(net, &c.w);
2022 static void __fib6_clean_all(struct net *net,
2023 int (*func)(struct rt6_info *, void *),
2024 int sernum, void *arg)
2026 struct fib6_table *table;
2027 struct hlist_head *head;
2031 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2032 head = &net->ipv6.fib_table_hash[h];
2033 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2034 spin_lock_bh(&table->tb6_lock);
2035 fib6_clean_tree(net, &table->tb6_root,
2037 spin_unlock_bh(&table->tb6_lock);
2043 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
2046 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
2049 static void fib6_flush_trees(struct net *net)
2051 int new_sernum = fib6_new_sernum(net);
2053 __fib6_clean_all(net, NULL, new_sernum, NULL);
2057 * Garbage collection
2060 static int fib6_age(struct rt6_info *rt, void *arg)
2062 struct fib6_gc_args *gc_args = arg;
2063 unsigned long now = jiffies;
2066 * check addrconf expiration here.
2067 * Routes are expired even if they are in use.
2070 if (rt->rt6i_flags & RTF_EXPIRES && rt->expires) {
2071 if (time_after(now, rt->expires)) {
2072 RT6_TRACE("expiring %p\n", rt);
2078 /* Also age clones in the exception table.
2079 * Note, that clones are aged out
2080 * only if they are not in use now.
2082 rt6_age_exceptions(rt, gc_args, now);
2087 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2089 struct fib6_gc_args gc_args;
2093 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2094 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2095 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2098 gc_args.timeout = expires ? (int)expires :
2099 net->ipv6.sysctl.ip6_rt_gc_interval;
2102 fib6_clean_all(net, fib6_age, &gc_args);
2104 net->ipv6.ip6_rt_last_gc = now;
2107 mod_timer(&net->ipv6.ip6_fib_timer,
2109 + net->ipv6.sysctl.ip6_rt_gc_interval));
2111 del_timer(&net->ipv6.ip6_fib_timer);
2112 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2115 static void fib6_gc_timer_cb(struct timer_list *t)
2117 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2119 fib6_run_gc(0, arg, true);
2122 static int __net_init fib6_net_init(struct net *net)
2124 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2127 err = fib6_notifier_init(net);
2131 spin_lock_init(&net->ipv6.fib6_gc_lock);
2132 rwlock_init(&net->ipv6.fib6_walker_lock);
2133 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2134 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2136 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2137 if (!net->ipv6.rt6_stats)
2140 /* Avoid false sharing : Use at least a full cache line */
2141 size = max_t(size_t, size, L1_CACHE_BYTES);
2143 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2144 if (!net->ipv6.fib_table_hash)
2147 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2149 if (!net->ipv6.fib6_main_tbl)
2150 goto out_fib_table_hash;
2152 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2153 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2154 net->ipv6.fib6_null_entry);
2155 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2156 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2157 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2159 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2160 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2162 if (!net->ipv6.fib6_local_tbl)
2163 goto out_fib6_main_tbl;
2164 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2165 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2166 net->ipv6.fib6_null_entry);
2167 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2168 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2169 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2171 fib6_tables_init(net);
2175 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2177 kfree(net->ipv6.fib6_main_tbl);
2180 kfree(net->ipv6.fib_table_hash);
2182 kfree(net->ipv6.rt6_stats);
2184 fib6_notifier_exit(net);
2188 static void fib6_net_exit(struct net *net)
2192 del_timer_sync(&net->ipv6.ip6_fib_timer);
2194 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2195 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2196 struct hlist_node *tmp;
2197 struct fib6_table *tb;
2199 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2200 hlist_del(&tb->tb6_hlist);
2201 fib6_free_table(tb);
2205 kfree(net->ipv6.fib_table_hash);
2206 kfree(net->ipv6.rt6_stats);
2207 fib6_notifier_exit(net);
2210 static struct pernet_operations fib6_net_ops = {
2211 .init = fib6_net_init,
2212 .exit = fib6_net_exit,
2215 int __init fib6_init(void)
2219 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2220 sizeof(struct fib6_node),
2221 0, SLAB_HWCACHE_ALIGN,
2223 if (!fib6_node_kmem)
2226 ret = register_pernet_subsys(&fib6_net_ops);
2228 goto out_kmem_cache_create;
2230 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2233 goto out_unregister_subsys;
2235 __fib6_flush_trees = fib6_flush_trees;
2239 out_unregister_subsys:
2240 unregister_pernet_subsys(&fib6_net_ops);
2241 out_kmem_cache_create:
2242 kmem_cache_destroy(fib6_node_kmem);
2246 void fib6_gc_cleanup(void)
2248 unregister_pernet_subsys(&fib6_net_ops);
2249 kmem_cache_destroy(fib6_node_kmem);
2252 #ifdef CONFIG_PROC_FS
2254 struct ipv6_route_iter {
2255 struct seq_net_private p;
2256 struct fib6_walker w;
2258 struct fib6_table *tbl;
2262 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2264 struct rt6_info *rt = v;
2265 struct ipv6_route_iter *iter = seq->private;
2266 const struct net_device *dev;
2268 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2270 #ifdef CONFIG_IPV6_SUBTREES
2271 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2273 seq_puts(seq, "00000000000000000000000000000000 00 ");
2275 if (rt->rt6i_flags & RTF_GATEWAY)
2276 seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
2278 seq_puts(seq, "00000000000000000000000000000000");
2280 dev = rt->fib6_nh.nh_dev;
2281 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2282 rt->rt6i_metric, atomic_read(&rt->rt6i_ref), 0,
2283 rt->rt6i_flags, dev ? dev->name : "");
2284 iter->w.leaf = NULL;
2288 static int ipv6_route_yield(struct fib6_walker *w)
2290 struct ipv6_route_iter *iter = w->args;
2296 iter->w.leaf = rcu_dereference_protected(
2297 iter->w.leaf->rt6_next,
2298 lockdep_is_held(&iter->tbl->tb6_lock));
2300 if (!iter->skip && iter->w.leaf)
2302 } while (iter->w.leaf);
2307 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2310 memset(&iter->w, 0, sizeof(iter->w));
2311 iter->w.func = ipv6_route_yield;
2312 iter->w.root = &iter->tbl->tb6_root;
2313 iter->w.state = FWS_INIT;
2314 iter->w.node = iter->w.root;
2315 iter->w.args = iter;
2316 iter->sernum = iter->w.root->fn_sernum;
2317 INIT_LIST_HEAD(&iter->w.lh);
2318 fib6_walker_link(net, &iter->w);
2321 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2325 struct hlist_node *node;
2328 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2329 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2335 while (!node && h < FIB6_TABLE_HASHSZ) {
2336 node = rcu_dereference_bh(
2337 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2339 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2342 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2344 if (iter->sernum != iter->w.root->fn_sernum) {
2345 iter->sernum = iter->w.root->fn_sernum;
2346 iter->w.state = FWS_INIT;
2347 iter->w.node = iter->w.root;
2348 WARN_ON(iter->w.skip);
2349 iter->w.skip = iter->w.count;
2353 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2357 struct net *net = seq_file_net(seq);
2358 struct ipv6_route_iter *iter = seq->private;
2363 n = rcu_dereference_bh(((struct rt6_info *)v)->rt6_next);
2370 ipv6_route_check_sernum(iter);
2371 spin_lock_bh(&iter->tbl->tb6_lock);
2372 r = fib6_walk_continue(&iter->w);
2373 spin_unlock_bh(&iter->tbl->tb6_lock);
2377 return iter->w.leaf;
2379 fib6_walker_unlink(net, &iter->w);
2382 fib6_walker_unlink(net, &iter->w);
2384 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2388 ipv6_route_seq_setup_walk(iter, net);
2392 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2395 struct net *net = seq_file_net(seq);
2396 struct ipv6_route_iter *iter = seq->private;
2399 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2403 ipv6_route_seq_setup_walk(iter, net);
2404 return ipv6_route_seq_next(seq, NULL, pos);
2410 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2412 struct fib6_walker *w = &iter->w;
2413 return w->node && !(w->state == FWS_U && w->node == w->root);
2416 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2419 struct net *net = seq_file_net(seq);
2420 struct ipv6_route_iter *iter = seq->private;
2422 if (ipv6_route_iter_active(iter))
2423 fib6_walker_unlink(net, &iter->w);
2425 rcu_read_unlock_bh();
2428 static const struct seq_operations ipv6_route_seq_ops = {
2429 .start = ipv6_route_seq_start,
2430 .next = ipv6_route_seq_next,
2431 .stop = ipv6_route_seq_stop,
2432 .show = ipv6_route_seq_show
2435 int ipv6_route_open(struct inode *inode, struct file *file)
2437 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2438 sizeof(struct ipv6_route_iter));
2441 #endif /* CONFIG_PROC_FS */