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>
34 #include <net/ndisc.h>
35 #include <net/addrconf.h>
36 #include <net/lwtunnel.h>
37 #include <net/fib_notifier.h>
39 #include <net/ip6_fib.h>
40 #include <net/ip6_route.h>
42 static struct kmem_cache *fib6_node_kmem __read_mostly;
47 int (*func)(struct fib6_info *, void *arg);
53 #ifdef CONFIG_IPV6_SUBTREES
54 #define FWS_INIT FWS_S
56 #define FWS_INIT FWS_L
59 static struct fib6_info *fib6_find_prefix(struct net *net,
60 struct fib6_table *table,
61 struct fib6_node *fn);
62 static struct fib6_node *fib6_repair_tree(struct net *net,
63 struct fib6_table *table,
64 struct fib6_node *fn);
65 static int fib6_walk(struct net *net, struct fib6_walker *w);
66 static int fib6_walk_continue(struct fib6_walker *w);
69 * A routing update causes an increase of the serial number on the
70 * affected subtree. This allows for cached routes to be asynchronously
71 * tested when modifications are made to the destination cache as a
72 * result of redirects, path MTU changes, etc.
75 static void fib6_gc_timer_cb(struct timer_list *t);
77 #define FOR_WALKERS(net, w) \
78 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
80 static void fib6_walker_link(struct net *net, struct fib6_walker *w)
82 write_lock_bh(&net->ipv6.fib6_walker_lock);
83 list_add(&w->lh, &net->ipv6.fib6_walkers);
84 write_unlock_bh(&net->ipv6.fib6_walker_lock);
87 static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
89 write_lock_bh(&net->ipv6.fib6_walker_lock);
91 write_unlock_bh(&net->ipv6.fib6_walker_lock);
94 static int fib6_new_sernum(struct net *net)
99 old = atomic_read(&net->ipv6.fib6_sernum);
100 new = old < INT_MAX ? old + 1 : 1;
101 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
107 FIB6_NO_SERNUM_CHANGE = 0,
110 void fib6_update_sernum(struct net *net, struct fib6_info *f6i)
112 struct fib6_node *fn;
114 fn = rcu_dereference_protected(f6i->fib6_node,
115 lockdep_is_held(&f6i->fib6_table->tb6_lock));
117 fn->fn_sernum = fib6_new_sernum(net);
121 * Auxiliary address test functions for the radix tree.
123 * These assume a 32bit processor (although it will work on
130 #if defined(__LITTLE_ENDIAN)
131 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
133 # define BITOP_BE32_SWIZZLE 0
136 static __be32 addr_bit_set(const void *token, int fn_bit)
138 const __be32 *addr = token;
141 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
142 * is optimized version of
143 * htonl(1 << ((~fn_bit)&0x1F))
144 * See include/asm-generic/bitops/le.h.
146 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
150 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags)
152 struct fib6_info *f6i;
154 f6i = kzalloc(sizeof(*f6i), gfp_flags);
158 INIT_LIST_HEAD(&f6i->fib6_siblings);
159 refcount_set(&f6i->fib6_ref, 1);
164 void fib6_info_destroy_rcu(struct rcu_head *head)
166 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
167 struct rt6_exception_bucket *bucket;
169 WARN_ON(f6i->fib6_node);
171 bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
174 fib6_nh_release(&f6i->fib6_nh);
176 ip_fib_metrics_put(f6i->fib6_metrics);
180 EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
182 static struct fib6_node *node_alloc(struct net *net)
184 struct fib6_node *fn;
186 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
188 net->ipv6.rt6_stats->fib_nodes++;
193 static void node_free_immediate(struct net *net, struct fib6_node *fn)
195 kmem_cache_free(fib6_node_kmem, fn);
196 net->ipv6.rt6_stats->fib_nodes--;
199 static void node_free_rcu(struct rcu_head *head)
201 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
203 kmem_cache_free(fib6_node_kmem, fn);
206 static void node_free(struct net *net, struct fib6_node *fn)
208 call_rcu(&fn->rcu, node_free_rcu);
209 net->ipv6.rt6_stats->fib_nodes--;
212 static void fib6_free_table(struct fib6_table *table)
214 inetpeer_invalidate_tree(&table->tb6_peers);
218 static void fib6_link_table(struct net *net, struct fib6_table *tb)
223 * Initialize table lock at a single place to give lockdep a key,
224 * tables aren't visible prior to being linked to the list.
226 spin_lock_init(&tb->tb6_lock);
227 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
230 * No protection necessary, this is the only list mutatation
231 * operation, tables never disappear once they exist.
233 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
236 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
238 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
240 struct fib6_table *table;
242 table = kzalloc(sizeof(*table), GFP_ATOMIC);
245 rcu_assign_pointer(table->tb6_root.leaf,
246 net->ipv6.fib6_null_entry);
247 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
248 inet_peer_base_init(&table->tb6_peers);
254 struct fib6_table *fib6_new_table(struct net *net, u32 id)
256 struct fib6_table *tb;
260 tb = fib6_get_table(net, id);
264 tb = fib6_alloc_table(net, id);
266 fib6_link_table(net, tb);
270 EXPORT_SYMBOL_GPL(fib6_new_table);
272 struct fib6_table *fib6_get_table(struct net *net, u32 id)
274 struct fib6_table *tb;
275 struct hlist_head *head;
280 h = id & (FIB6_TABLE_HASHSZ - 1);
282 head = &net->ipv6.fib_table_hash[h];
283 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
284 if (tb->tb6_id == id) {
293 EXPORT_SYMBOL_GPL(fib6_get_table);
295 static void __net_init fib6_tables_init(struct net *net)
297 fib6_link_table(net, net->ipv6.fib6_main_tbl);
298 fib6_link_table(net, net->ipv6.fib6_local_tbl);
302 struct fib6_table *fib6_new_table(struct net *net, u32 id)
304 return fib6_get_table(net, id);
307 struct fib6_table *fib6_get_table(struct net *net, u32 id)
309 return net->ipv6.fib6_main_tbl;
312 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
313 const struct sk_buff *skb,
314 int flags, pol_lookup_t lookup)
318 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
319 if (rt->dst.error == -EAGAIN) {
321 rt = net->ipv6.ip6_null_entry;
328 /* called with rcu lock held; no reference taken on fib6_info */
329 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
330 struct fib6_result *res, int flags)
332 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6,
336 static void __net_init fib6_tables_init(struct net *net)
338 fib6_link_table(net, net->ipv6.fib6_main_tbl);
343 unsigned int fib6_tables_seq_read(struct net *net)
345 unsigned int h, fib_seq = 0;
348 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
349 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
350 struct fib6_table *tb;
352 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
353 fib_seq += tb->fib_seq;
360 static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
361 enum fib_event_type event_type,
362 struct fib6_info *rt)
364 struct fib6_entry_notifier_info info = {
368 return call_fib6_notifier(nb, net, event_type, &info.info);
371 int call_fib6_entry_notifiers(struct net *net,
372 enum fib_event_type event_type,
373 struct fib6_info *rt,
374 struct netlink_ext_ack *extack)
376 struct fib6_entry_notifier_info info = {
377 .info.extack = extack,
381 rt->fib6_table->fib_seq++;
382 return call_fib6_notifiers(net, event_type, &info.info);
385 struct fib6_dump_arg {
387 struct notifier_block *nb;
390 static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
392 if (rt == arg->net->ipv6.fib6_null_entry)
394 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
397 static int fib6_node_dump(struct fib6_walker *w)
399 struct fib6_info *rt;
401 for_each_fib6_walker_rt(w)
402 fib6_rt_dump(rt, w->args);
407 static void fib6_table_dump(struct net *net, struct fib6_table *tb,
408 struct fib6_walker *w)
410 w->root = &tb->tb6_root;
411 spin_lock_bh(&tb->tb6_lock);
413 spin_unlock_bh(&tb->tb6_lock);
416 /* Called with rcu_read_lock() */
417 int fib6_tables_dump(struct net *net, struct notifier_block *nb)
419 struct fib6_dump_arg arg;
420 struct fib6_walker *w;
423 w = kzalloc(sizeof(*w), GFP_ATOMIC);
427 w->func = fib6_node_dump;
432 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
433 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
434 struct fib6_table *tb;
436 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
437 fib6_table_dump(net, tb, w);
445 static int fib6_dump_node(struct fib6_walker *w)
448 struct fib6_info *rt;
450 for_each_fib6_walker_rt(w) {
451 res = rt6_dump_route(rt, w->args);
453 /* Frame is full, suspend walking */
458 /* Multipath routes are dumped in one route with the
459 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
460 * last sibling of this route (no need to dump the
461 * sibling routes again)
463 if (rt->fib6_nsiblings)
464 rt = list_last_entry(&rt->fib6_siblings,
472 static void fib6_dump_end(struct netlink_callback *cb)
474 struct net *net = sock_net(cb->skb->sk);
475 struct fib6_walker *w = (void *)cb->args[2];
480 fib6_walker_unlink(net, w);
485 cb->done = (void *)cb->args[3];
489 static int fib6_dump_done(struct netlink_callback *cb)
492 return cb->done ? cb->done(cb) : 0;
495 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
496 struct netlink_callback *cb)
498 struct net *net = sock_net(skb->sk);
499 struct fib6_walker *w;
502 w = (void *)cb->args[2];
503 w->root = &table->tb6_root;
505 if (cb->args[4] == 0) {
509 spin_lock_bh(&table->tb6_lock);
510 res = fib6_walk(net, w);
511 spin_unlock_bh(&table->tb6_lock);
514 cb->args[5] = w->root->fn_sernum;
517 if (cb->args[5] != w->root->fn_sernum) {
518 /* Begin at the root if the tree changed */
519 cb->args[5] = w->root->fn_sernum;
526 spin_lock_bh(&table->tb6_lock);
527 res = fib6_walk_continue(w);
528 spin_unlock_bh(&table->tb6_lock);
530 fib6_walker_unlink(net, w);
538 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
540 const struct nlmsghdr *nlh = cb->nlh;
541 struct net *net = sock_net(skb->sk);
542 struct rt6_rtnl_dump_arg arg = {};
544 unsigned int e = 0, s_e;
545 struct fib6_walker *w;
546 struct fib6_table *tb;
547 struct hlist_head *head;
550 if (cb->strict_check) {
553 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
556 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
557 struct rtmsg *rtm = nlmsg_data(nlh);
559 arg.filter.flags = rtm->rtm_flags & (RTM_F_PREFIX|RTM_F_CLONED);
562 /* fib entries are never clones */
563 if (arg.filter.flags & RTM_F_CLONED)
566 w = (void *)cb->args[2];
570 * 1. hook callback destructor.
572 cb->args[3] = (long)cb->done;
573 cb->done = fib6_dump_done;
576 * 2. allocate and initialize walker.
578 w = kzalloc(sizeof(*w), GFP_ATOMIC);
581 w->func = fib6_dump_node;
582 cb->args[2] = (long)w;
590 if (arg.filter.table_id) {
591 tb = fib6_get_table(net, arg.filter.table_id);
593 if (arg.filter.dump_all_families)
596 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
601 res = fib6_dump_table(tb, skb, cb);
612 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
614 head = &net->ipv6.fib_table_hash[h];
615 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
618 res = fib6_dump_table(tb, skb, cb);
630 res = res < 0 ? res : skb->len;
636 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
641 if (f6i->fib6_metrics == &dst_default_metrics) {
642 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
647 refcount_set(&p->refcnt, 1);
648 f6i->fib6_metrics = p;
651 f6i->fib6_metrics->metrics[metric - 1] = val;
657 * return the appropriate node for a routing tree "add" operation
658 * by either creating and inserting or by returning an existing
662 static struct fib6_node *fib6_add_1(struct net *net,
663 struct fib6_table *table,
664 struct fib6_node *root,
665 struct in6_addr *addr, int plen,
666 int offset, int allow_create,
667 int replace_required,
668 struct netlink_ext_ack *extack)
670 struct fib6_node *fn, *in, *ln;
671 struct fib6_node *pn = NULL;
676 RT6_TRACE("fib6_add_1\n");
678 /* insert node in tree */
683 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
684 lockdep_is_held(&table->tb6_lock));
685 key = (struct rt6key *)((u8 *)leaf + offset);
690 if (plen < fn->fn_bit ||
691 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
693 if (replace_required) {
694 NL_SET_ERR_MSG(extack,
695 "Can not replace route - no match found");
696 pr_warn("Can't replace route, no match found\n");
697 return ERR_PTR(-ENOENT);
699 pr_warn("NLM_F_CREATE should be set when creating new route\n");
708 if (plen == fn->fn_bit) {
709 /* clean up an intermediate node */
710 if (!(fn->fn_flags & RTN_RTINFO)) {
711 RCU_INIT_POINTER(fn->leaf, NULL);
712 fib6_info_release(leaf);
713 /* remove null_entry in the root node */
714 } else if (fn->fn_flags & RTN_TL_ROOT &&
715 rcu_access_pointer(fn->leaf) ==
716 net->ipv6.fib6_null_entry) {
717 RCU_INIT_POINTER(fn->leaf, NULL);
724 * We have more bits to go
727 /* Try to walk down on tree. */
728 dir = addr_bit_set(addr, fn->fn_bit);
731 rcu_dereference_protected(fn->right,
732 lockdep_is_held(&table->tb6_lock)) :
733 rcu_dereference_protected(fn->left,
734 lockdep_is_held(&table->tb6_lock));
738 /* We should not create new node because
739 * NLM_F_REPLACE was specified without NLM_F_CREATE
740 * I assume it is safe to require NLM_F_CREATE when
741 * REPLACE flag is used! Later we may want to remove the
742 * check for replace_required, because according
743 * to netlink specification, NLM_F_CREATE
744 * MUST be specified if new route is created.
745 * That would keep IPv6 consistent with IPv4
747 if (replace_required) {
748 NL_SET_ERR_MSG(extack,
749 "Can not replace route - no match found");
750 pr_warn("Can't replace route, no match found\n");
751 return ERR_PTR(-ENOENT);
753 pr_warn("NLM_F_CREATE should be set when creating new route\n");
756 * We walked to the bottom of tree.
757 * Create new leaf node without children.
760 ln = node_alloc(net);
763 return ERR_PTR(-ENOMEM);
765 RCU_INIT_POINTER(ln->parent, pn);
768 rcu_assign_pointer(pn->right, ln);
770 rcu_assign_pointer(pn->left, ln);
777 * split since we don't have a common prefix anymore or
778 * we have a less significant route.
779 * we've to insert an intermediate node on the list
780 * this new node will point to the one we need to create
784 pn = rcu_dereference_protected(fn->parent,
785 lockdep_is_held(&table->tb6_lock));
787 /* find 1st bit in difference between the 2 addrs.
789 See comment in __ipv6_addr_diff: bit may be an invalid value,
790 but if it is >= plen, the value is ignored in any case.
793 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
798 * (new leaf node)[ln] (old node)[fn]
801 in = node_alloc(net);
802 ln = node_alloc(net);
806 node_free_immediate(net, in);
808 node_free_immediate(net, ln);
809 return ERR_PTR(-ENOMEM);
813 * new intermediate node.
815 * be off since that an address that chooses one of
816 * the branches would not match less specific routes
817 * in the other branch
822 RCU_INIT_POINTER(in->parent, pn);
824 fib6_info_hold(rcu_dereference_protected(in->leaf,
825 lockdep_is_held(&table->tb6_lock)));
827 /* update parent pointer */
829 rcu_assign_pointer(pn->right, in);
831 rcu_assign_pointer(pn->left, in);
835 RCU_INIT_POINTER(ln->parent, in);
836 rcu_assign_pointer(fn->parent, in);
838 if (addr_bit_set(addr, bit)) {
839 rcu_assign_pointer(in->right, ln);
840 rcu_assign_pointer(in->left, fn);
842 rcu_assign_pointer(in->left, ln);
843 rcu_assign_pointer(in->right, fn);
845 } else { /* plen <= bit */
848 * (new leaf node)[ln]
850 * (old node)[fn] NULL
853 ln = node_alloc(net);
856 return ERR_PTR(-ENOMEM);
860 RCU_INIT_POINTER(ln->parent, pn);
862 if (addr_bit_set(&key->addr, plen))
863 RCU_INIT_POINTER(ln->right, fn);
865 RCU_INIT_POINTER(ln->left, fn);
867 rcu_assign_pointer(fn->parent, ln);
870 rcu_assign_pointer(pn->right, ln);
872 rcu_assign_pointer(pn->left, ln);
877 static void fib6_drop_pcpu_from(struct fib6_info *f6i,
878 const struct fib6_table *table)
880 struct fib6_nh *fib6_nh = &f6i->fib6_nh;
883 if (!fib6_nh->rt6i_pcpu)
886 /* Make sure rt6_make_pcpu_route() wont add other percpu routes
887 * while we are cleaning them here.
889 f6i->fib6_destroying = 1;
890 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
892 /* release the reference to this fib entry from
893 * all of its cached pcpu routes
895 for_each_possible_cpu(cpu) {
896 struct rt6_info **ppcpu_rt;
897 struct rt6_info *pcpu_rt;
899 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
902 struct fib6_info *from;
904 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
905 fib6_info_release(from);
910 static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
913 struct fib6_table *table = rt->fib6_table;
915 fib6_drop_pcpu_from(rt, table);
917 if (refcount_read(&rt->fib6_ref) != 1) {
918 /* This route is used as dummy address holder in some split
919 * nodes. It is not leaked, but it still holds other resources,
920 * which must be released in time. So, scan ascendant nodes
921 * and replace dummy references to this route with references
922 * to still alive ones.
925 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
926 lockdep_is_held(&table->tb6_lock));
927 struct fib6_info *new_leaf;
928 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
929 new_leaf = fib6_find_prefix(net, table, fn);
930 fib6_info_hold(new_leaf);
932 rcu_assign_pointer(fn->leaf, new_leaf);
933 fib6_info_release(rt);
935 fn = rcu_dereference_protected(fn->parent,
936 lockdep_is_held(&table->tb6_lock));
942 * Insert routing information in a node.
945 static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
946 struct nl_info *info,
947 struct netlink_ext_ack *extack)
949 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
950 lockdep_is_held(&rt->fib6_table->tb6_lock));
951 struct fib6_info *iter = NULL;
952 struct fib6_info __rcu **ins;
953 struct fib6_info __rcu **fallback_ins = NULL;
954 int replace = (info->nlh &&
955 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
956 int add = (!info->nlh ||
957 (info->nlh->nlmsg_flags & NLM_F_CREATE));
959 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
960 u16 nlflags = NLM_F_EXCL;
963 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
964 nlflags |= NLM_F_APPEND;
968 for (iter = leaf; iter;
969 iter = rcu_dereference_protected(iter->fib6_next,
970 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
972 * Search for duplicates
975 if (iter->fib6_metric == rt->fib6_metric) {
977 * Same priority level
980 (info->nlh->nlmsg_flags & NLM_F_EXCL))
983 nlflags &= ~NLM_F_EXCL;
985 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
990 fallback_ins = fallback_ins ?: ins;
994 if (rt6_duplicate_nexthop(iter, rt)) {
995 if (rt->fib6_nsiblings)
996 rt->fib6_nsiblings = 0;
997 if (!(iter->fib6_flags & RTF_EXPIRES))
999 if (!(rt->fib6_flags & RTF_EXPIRES))
1000 fib6_clean_expires(iter);
1002 fib6_set_expires(iter, rt->expires);
1005 fib6_metric_set(iter, RTAX_MTU,
1009 /* If we have the same destination and the same metric,
1010 * but not the same gateway, then the route we try to
1011 * add is sibling to this route, increment our counter
1012 * of siblings, and later we will add our route to the
1014 * Only static routes (which don't have flag
1015 * RTF_EXPIRES) are used for ECMPv6.
1017 * To avoid long list, we only had siblings if the
1018 * route have a gateway.
1021 rt6_qualify_for_ecmp(iter))
1022 rt->fib6_nsiblings++;
1025 if (iter->fib6_metric > rt->fib6_metric)
1029 ins = &iter->fib6_next;
1032 if (fallback_ins && !found) {
1033 /* No ECMP-able route found, replace first non-ECMP one */
1035 iter = rcu_dereference_protected(*ins,
1036 lockdep_is_held(&rt->fib6_table->tb6_lock));
1040 /* Reset round-robin state, if necessary */
1041 if (ins == &fn->leaf)
1044 /* Link this route to others same route. */
1045 if (rt->fib6_nsiblings) {
1046 unsigned int fib6_nsiblings;
1047 struct fib6_info *sibling, *temp_sibling;
1049 /* Find the first route that have the same metric */
1052 if (sibling->fib6_metric == rt->fib6_metric &&
1053 rt6_qualify_for_ecmp(sibling)) {
1054 list_add_tail(&rt->fib6_siblings,
1055 &sibling->fib6_siblings);
1058 sibling = rcu_dereference_protected(sibling->fib6_next,
1059 lockdep_is_held(&rt->fib6_table->tb6_lock));
1061 /* For each sibling in the list, increment the counter of
1062 * siblings. BUG() if counters does not match, list of siblings
1066 list_for_each_entry_safe(sibling, temp_sibling,
1067 &rt->fib6_siblings, fib6_siblings) {
1068 sibling->fib6_nsiblings++;
1069 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1072 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1073 rt6_multipath_rebalance(temp_sibling);
1081 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1084 nlflags |= NLM_F_CREATE;
1086 err = call_fib6_entry_notifiers(info->nl_net,
1087 FIB_EVENT_ENTRY_ADD,
1092 rcu_assign_pointer(rt->fib6_next, iter);
1094 rcu_assign_pointer(rt->fib6_node, fn);
1095 rcu_assign_pointer(*ins, rt);
1096 if (!info->skip_notify)
1097 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1098 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1100 if (!(fn->fn_flags & RTN_RTINFO)) {
1101 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1102 fn->fn_flags |= RTN_RTINFO;
1111 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1115 err = call_fib6_entry_notifiers(info->nl_net,
1116 FIB_EVENT_ENTRY_REPLACE,
1122 rcu_assign_pointer(rt->fib6_node, fn);
1123 rt->fib6_next = iter->fib6_next;
1124 rcu_assign_pointer(*ins, rt);
1125 if (!info->skip_notify)
1126 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1127 if (!(fn->fn_flags & RTN_RTINFO)) {
1128 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1129 fn->fn_flags |= RTN_RTINFO;
1131 nsiblings = iter->fib6_nsiblings;
1132 iter->fib6_node = NULL;
1133 fib6_purge_rt(iter, fn, info->nl_net);
1134 if (rcu_access_pointer(fn->rr_ptr) == iter)
1136 fib6_info_release(iter);
1139 /* Replacing an ECMP route, remove all siblings */
1140 ins = &rt->fib6_next;
1141 iter = rcu_dereference_protected(*ins,
1142 lockdep_is_held(&rt->fib6_table->tb6_lock));
1144 if (iter->fib6_metric > rt->fib6_metric)
1146 if (rt6_qualify_for_ecmp(iter)) {
1147 *ins = iter->fib6_next;
1148 iter->fib6_node = NULL;
1149 fib6_purge_rt(iter, fn, info->nl_net);
1150 if (rcu_access_pointer(fn->rr_ptr) == iter)
1152 fib6_info_release(iter);
1154 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1156 ins = &iter->fib6_next;
1158 iter = rcu_dereference_protected(*ins,
1159 lockdep_is_held(&rt->fib6_table->tb6_lock));
1161 WARN_ON(nsiblings != 0);
1168 static void fib6_start_gc(struct net *net, struct fib6_info *rt)
1170 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1171 (rt->fib6_flags & RTF_EXPIRES))
1172 mod_timer(&net->ipv6.ip6_fib_timer,
1173 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1176 void fib6_force_start_gc(struct net *net)
1178 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1179 mod_timer(&net->ipv6.ip6_fib_timer,
1180 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1183 static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
1186 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1187 lockdep_is_held(&rt->fib6_table->tb6_lock));
1189 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1192 fn->fn_sernum = sernum;
1193 fn = rcu_dereference_protected(fn->parent,
1194 lockdep_is_held(&rt->fib6_table->tb6_lock));
1198 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
1200 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1203 /* allow ipv4 to update sernum via ipv6_stub */
1204 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i)
1206 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1207 fib6_update_sernum_upto_root(net, f6i);
1208 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1212 * Add routing information to the routing tree.
1213 * <destination addr>/<source addr>
1214 * with source addr info in sub-trees
1215 * Need to own table->tb6_lock
1218 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
1219 struct nl_info *info, struct netlink_ext_ack *extack)
1221 struct fib6_table *table = rt->fib6_table;
1222 struct fib6_node *fn, *pn = NULL;
1224 int allow_create = 1;
1225 int replace_required = 0;
1226 int sernum = fib6_new_sernum(info->nl_net);
1229 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1231 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1232 replace_required = 1;
1234 if (!allow_create && !replace_required)
1235 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1237 fn = fib6_add_1(info->nl_net, table, root,
1238 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1239 offsetof(struct fib6_info, fib6_dst), allow_create,
1240 replace_required, extack);
1249 #ifdef CONFIG_IPV6_SUBTREES
1250 if (rt->fib6_src.plen) {
1251 struct fib6_node *sn;
1253 if (!rcu_access_pointer(fn->subtree)) {
1254 struct fib6_node *sfn;
1266 /* Create subtree root node */
1267 sfn = node_alloc(info->nl_net);
1271 fib6_info_hold(info->nl_net->ipv6.fib6_null_entry);
1272 rcu_assign_pointer(sfn->leaf,
1273 info->nl_net->ipv6.fib6_null_entry);
1274 sfn->fn_flags = RTN_ROOT;
1276 /* Now add the first leaf node to new subtree */
1278 sn = fib6_add_1(info->nl_net, table, sfn,
1279 &rt->fib6_src.addr, rt->fib6_src.plen,
1280 offsetof(struct fib6_info, fib6_src),
1281 allow_create, replace_required, extack);
1284 /* If it is failed, discard just allocated
1285 root, and then (in failure) stale node
1288 node_free_immediate(info->nl_net, sfn);
1293 /* Now link new subtree to main tree */
1294 rcu_assign_pointer(sfn->parent, fn);
1295 rcu_assign_pointer(fn->subtree, sfn);
1297 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1298 &rt->fib6_src.addr, rt->fib6_src.plen,
1299 offsetof(struct fib6_info, fib6_src),
1300 allow_create, replace_required, extack);
1308 if (!rcu_access_pointer(fn->leaf)) {
1309 if (fn->fn_flags & RTN_TL_ROOT) {
1310 /* put back null_entry for root node */
1311 rcu_assign_pointer(fn->leaf,
1312 info->nl_net->ipv6.fib6_null_entry);
1315 rcu_assign_pointer(fn->leaf, rt);
1322 err = fib6_add_rt2node(fn, rt, info, extack);
1324 __fib6_update_sernum_upto_root(rt, sernum);
1325 fib6_start_gc(info->nl_net, rt);
1330 #ifdef CONFIG_IPV6_SUBTREES
1332 * If fib6_add_1 has cleared the old leaf pointer in the
1333 * super-tree leaf node we have to find a new one for it.
1336 struct fib6_info *pn_leaf =
1337 rcu_dereference_protected(pn->leaf,
1338 lockdep_is_held(&table->tb6_lock));
1339 if (pn_leaf == rt) {
1341 RCU_INIT_POINTER(pn->leaf, NULL);
1342 fib6_info_release(rt);
1344 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1345 pn_leaf = fib6_find_prefix(info->nl_net, table,
1351 info->nl_net->ipv6.fib6_null_entry;
1354 fib6_info_hold(pn_leaf);
1355 rcu_assign_pointer(pn->leaf, pn_leaf);
1364 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1365 * 1. fn is an intermediate node and we failed to add the new
1366 * route to it in both subtree creation failure and fib6_add_rt2node()
1368 * 2. fn is the root node in the table and we fail to add the first
1369 * default route to it.
1372 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1373 (fn->fn_flags & RTN_TL_ROOT &&
1374 !rcu_access_pointer(fn->leaf))))
1375 fib6_repair_tree(info->nl_net, table, fn);
1380 * Routing tree lookup
1384 struct lookup_args {
1385 int offset; /* key offset on fib6_info */
1386 const struct in6_addr *addr; /* search key */
1389 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1390 struct lookup_args *args)
1392 struct fib6_node *fn;
1395 if (unlikely(args->offset == 0))
1405 struct fib6_node *next;
1407 dir = addr_bit_set(args->addr, fn->fn_bit);
1409 next = dir ? rcu_dereference(fn->right) :
1410 rcu_dereference(fn->left);
1420 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1422 if (subtree || fn->fn_flags & RTN_RTINFO) {
1423 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1429 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1431 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1432 #ifdef CONFIG_IPV6_SUBTREES
1434 struct fib6_node *sfn;
1435 sfn = fib6_node_lookup_1(subtree,
1442 if (fn->fn_flags & RTN_RTINFO)
1447 if (fn->fn_flags & RTN_ROOT)
1450 fn = rcu_dereference(fn->parent);
1456 /* called with rcu_read_lock() held
1458 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1459 const struct in6_addr *daddr,
1460 const struct in6_addr *saddr)
1462 struct fib6_node *fn;
1463 struct lookup_args args[] = {
1465 .offset = offsetof(struct fib6_info, fib6_dst),
1468 #ifdef CONFIG_IPV6_SUBTREES
1470 .offset = offsetof(struct fib6_info, fib6_src),
1475 .offset = 0, /* sentinel */
1479 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
1480 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1487 * Get node with specified destination prefix (and source prefix,
1488 * if subtrees are used)
1489 * exact_match == true means we try to find fn with exact match of
1490 * the passed in prefix addr
1491 * exact_match == false means we try to find fn with longest prefix
1492 * match of the passed in prefix addr. This is useful for finding fn
1493 * for cached route as it will be stored in the exception table under
1494 * the node with longest prefix length.
1498 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1499 const struct in6_addr *addr,
1500 int plen, int offset,
1503 struct fib6_node *fn, *prev = NULL;
1505 for (fn = root; fn ; ) {
1506 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1509 /* This node is being deleted */
1511 if (plen <= fn->fn_bit)
1517 key = (struct rt6key *)((u8 *)leaf + offset);
1522 if (plen < fn->fn_bit ||
1523 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1526 if (plen == fn->fn_bit)
1533 * We have more bits to go
1535 if (addr_bit_set(addr, fn->fn_bit))
1536 fn = rcu_dereference(fn->right);
1538 fn = rcu_dereference(fn->left);
1547 struct fib6_node *fib6_locate(struct fib6_node *root,
1548 const struct in6_addr *daddr, int dst_len,
1549 const struct in6_addr *saddr, int src_len,
1552 struct fib6_node *fn;
1554 fn = fib6_locate_1(root, daddr, dst_len,
1555 offsetof(struct fib6_info, fib6_dst),
1558 #ifdef CONFIG_IPV6_SUBTREES
1560 WARN_ON(saddr == NULL);
1562 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1565 fn = fib6_locate_1(subtree, saddr, src_len,
1566 offsetof(struct fib6_info, fib6_src),
1573 if (fn && fn->fn_flags & RTN_RTINFO)
1585 static struct fib6_info *fib6_find_prefix(struct net *net,
1586 struct fib6_table *table,
1587 struct fib6_node *fn)
1589 struct fib6_node *child_left, *child_right;
1591 if (fn->fn_flags & RTN_ROOT)
1592 return net->ipv6.fib6_null_entry;
1595 child_left = rcu_dereference_protected(fn->left,
1596 lockdep_is_held(&table->tb6_lock));
1597 child_right = rcu_dereference_protected(fn->right,
1598 lockdep_is_held(&table->tb6_lock));
1600 return rcu_dereference_protected(child_left->leaf,
1601 lockdep_is_held(&table->tb6_lock));
1603 return rcu_dereference_protected(child_right->leaf,
1604 lockdep_is_held(&table->tb6_lock));
1606 fn = FIB6_SUBTREE(fn);
1612 * Called to trim the tree of intermediate nodes when possible. "fn"
1613 * is the node we want to try and remove.
1614 * Need to own table->tb6_lock
1617 static struct fib6_node *fib6_repair_tree(struct net *net,
1618 struct fib6_table *table,
1619 struct fib6_node *fn)
1623 struct fib6_node *child;
1624 struct fib6_walker *w;
1627 /* Set fn->leaf to null_entry for root node. */
1628 if (fn->fn_flags & RTN_TL_ROOT) {
1629 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1634 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1635 lockdep_is_held(&table->tb6_lock));
1636 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1637 lockdep_is_held(&table->tb6_lock));
1638 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1639 lockdep_is_held(&table->tb6_lock));
1640 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1641 lockdep_is_held(&table->tb6_lock));
1642 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1643 lockdep_is_held(&table->tb6_lock));
1644 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1645 lockdep_is_held(&table->tb6_lock));
1646 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1647 lockdep_is_held(&table->tb6_lock));
1648 struct fib6_info *new_fn_leaf;
1650 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1653 WARN_ON(fn->fn_flags & RTN_RTINFO);
1654 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1660 child = fn_r, children |= 1;
1662 child = fn_l, children |= 2;
1664 if (children == 3 || FIB6_SUBTREE(fn)
1665 #ifdef CONFIG_IPV6_SUBTREES
1666 /* Subtree root (i.e. fn) may have one child */
1667 || (children && fn->fn_flags & RTN_ROOT)
1670 new_fn_leaf = fib6_find_prefix(net, table, fn);
1673 WARN_ON(!new_fn_leaf);
1674 new_fn_leaf = net->ipv6.fib6_null_entry;
1677 fib6_info_hold(new_fn_leaf);
1678 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1682 #ifdef CONFIG_IPV6_SUBTREES
1683 if (FIB6_SUBTREE(pn) == fn) {
1684 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1685 RCU_INIT_POINTER(pn->subtree, NULL);
1688 WARN_ON(fn->fn_flags & RTN_ROOT);
1691 rcu_assign_pointer(pn->right, child);
1692 else if (pn_l == fn)
1693 rcu_assign_pointer(pn->left, child);
1699 rcu_assign_pointer(child->parent, pn);
1701 #ifdef CONFIG_IPV6_SUBTREES
1705 read_lock(&net->ipv6.fib6_walker_lock);
1706 FOR_WALKERS(net, w) {
1708 if (w->node == fn) {
1709 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1714 if (w->node == fn) {
1717 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1718 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1720 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1721 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1726 read_unlock(&net->ipv6.fib6_walker_lock);
1729 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1732 RCU_INIT_POINTER(pn->leaf, NULL);
1733 fib6_info_release(pn_leaf);
1738 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1739 struct fib6_info __rcu **rtp, struct nl_info *info)
1741 struct fib6_walker *w;
1742 struct fib6_info *rt = rcu_dereference_protected(*rtp,
1743 lockdep_is_held(&table->tb6_lock));
1744 struct net *net = info->nl_net;
1746 RT6_TRACE("fib6_del_route\n");
1749 *rtp = rt->fib6_next;
1750 rt->fib6_node = NULL;
1751 net->ipv6.rt6_stats->fib_rt_entries--;
1752 net->ipv6.rt6_stats->fib_discarded_routes++;
1754 /* Flush all cached dst in exception table */
1755 rt6_flush_exceptions(rt);
1757 /* Reset round-robin state, if necessary */
1758 if (rcu_access_pointer(fn->rr_ptr) == rt)
1761 /* Remove this entry from other siblings */
1762 if (rt->fib6_nsiblings) {
1763 struct fib6_info *sibling, *next_sibling;
1765 list_for_each_entry_safe(sibling, next_sibling,
1766 &rt->fib6_siblings, fib6_siblings)
1767 sibling->fib6_nsiblings--;
1768 rt->fib6_nsiblings = 0;
1769 list_del_init(&rt->fib6_siblings);
1770 rt6_multipath_rebalance(next_sibling);
1773 /* Adjust walkers */
1774 read_lock(&net->ipv6.fib6_walker_lock);
1775 FOR_WALKERS(net, w) {
1776 if (w->state == FWS_C && w->leaf == rt) {
1777 RT6_TRACE("walker %p adjusted by delroute\n", w);
1778 w->leaf = rcu_dereference_protected(rt->fib6_next,
1779 lockdep_is_held(&table->tb6_lock));
1784 read_unlock(&net->ipv6.fib6_walker_lock);
1786 /* If it was last route, call fib6_repair_tree() to:
1787 * 1. For root node, put back null_entry as how the table was created.
1788 * 2. For other nodes, expunge its radix tree node.
1790 if (!rcu_access_pointer(fn->leaf)) {
1791 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1792 fn->fn_flags &= ~RTN_RTINFO;
1793 net->ipv6.rt6_stats->fib_route_nodes--;
1795 fn = fib6_repair_tree(net, table, fn);
1798 fib6_purge_rt(rt, fn, net);
1800 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
1801 if (!info->skip_notify)
1802 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
1803 fib6_info_release(rt);
1806 /* Need to own table->tb6_lock */
1807 int fib6_del(struct fib6_info *rt, struct nl_info *info)
1809 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1810 lockdep_is_held(&rt->fib6_table->tb6_lock));
1811 struct fib6_table *table = rt->fib6_table;
1812 struct net *net = info->nl_net;
1813 struct fib6_info __rcu **rtp;
1814 struct fib6_info __rcu **rtp_next;
1816 if (!fn || rt == net->ipv6.fib6_null_entry)
1819 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1822 * Walk the leaf entries looking for ourself
1825 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1826 struct fib6_info *cur = rcu_dereference_protected(*rtp,
1827 lockdep_is_held(&table->tb6_lock));
1829 fib6_del_route(table, fn, rtp, info);
1832 rtp_next = &cur->fib6_next;
1838 * Tree traversal function.
1840 * Certainly, it is not interrupt safe.
1841 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1842 * It means, that we can modify tree during walking
1843 * and use this function for garbage collection, clone pruning,
1844 * cleaning tree when a device goes down etc. etc.
1846 * It guarantees that every node will be traversed,
1847 * and that it will be traversed only once.
1849 * Callback function w->func may return:
1850 * 0 -> continue walking.
1851 * positive value -> walking is suspended (used by tree dumps,
1852 * and probably by gc, if it will be split to several slices)
1853 * negative value -> terminate walking.
1855 * The function itself returns:
1856 * 0 -> walk is complete.
1857 * >0 -> walk is incomplete (i.e. suspended)
1858 * <0 -> walk is terminated by an error.
1860 * This function is called with tb6_lock held.
1863 static int fib6_walk_continue(struct fib6_walker *w)
1865 struct fib6_node *fn, *pn, *left, *right;
1867 /* w->root should always be table->tb6_root */
1868 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1876 #ifdef CONFIG_IPV6_SUBTREES
1878 if (FIB6_SUBTREE(fn)) {
1879 w->node = FIB6_SUBTREE(fn);
1886 left = rcu_dereference_protected(fn->left, 1);
1889 w->state = FWS_INIT;
1895 right = rcu_dereference_protected(fn->right, 1);
1898 w->state = FWS_INIT;
1902 w->leaf = rcu_dereference_protected(fn->leaf, 1);
1905 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1926 pn = rcu_dereference_protected(fn->parent, 1);
1927 left = rcu_dereference_protected(pn->left, 1);
1928 right = rcu_dereference_protected(pn->right, 1);
1930 #ifdef CONFIG_IPV6_SUBTREES
1931 if (FIB6_SUBTREE(pn) == fn) {
1932 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1943 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
1953 static int fib6_walk(struct net *net, struct fib6_walker *w)
1957 w->state = FWS_INIT;
1960 fib6_walker_link(net, w);
1961 res = fib6_walk_continue(w);
1963 fib6_walker_unlink(net, w);
1967 static int fib6_clean_node(struct fib6_walker *w)
1970 struct fib6_info *rt;
1971 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1972 struct nl_info info = {
1974 .skip_notify = c->skip_notify,
1977 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1978 w->node->fn_sernum != c->sernum)
1979 w->node->fn_sernum = c->sernum;
1982 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1987 for_each_fib6_walker_rt(w) {
1988 res = c->func(rt, c->arg);
1991 res = fib6_del(rt, &info);
1994 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1996 rcu_access_pointer(rt->fib6_node),
2002 } else if (res == -2) {
2003 if (WARN_ON(!rt->fib6_nsiblings))
2005 rt = list_last_entry(&rt->fib6_siblings,
2006 struct fib6_info, fib6_siblings);
2016 * Convenient frontend to tree walker.
2018 * func is called on each route.
2019 * It may return -2 -> skip multipath route.
2020 * -1 -> delete this route.
2021 * 0 -> continue walking
2024 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2025 int (*func)(struct fib6_info *, void *arg),
2026 int sernum, void *arg, bool skip_notify)
2028 struct fib6_cleaner c;
2031 c.w.func = fib6_clean_node;
2038 c.skip_notify = skip_notify;
2040 fib6_walk(net, &c.w);
2043 static void __fib6_clean_all(struct net *net,
2044 int (*func)(struct fib6_info *, void *),
2045 int sernum, void *arg, bool skip_notify)
2047 struct fib6_table *table;
2048 struct hlist_head *head;
2052 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2053 head = &net->ipv6.fib_table_hash[h];
2054 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2055 spin_lock_bh(&table->tb6_lock);
2056 fib6_clean_tree(net, &table->tb6_root,
2057 func, sernum, arg, skip_notify);
2058 spin_unlock_bh(&table->tb6_lock);
2064 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
2067 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2070 void fib6_clean_all_skip_notify(struct net *net,
2071 int (*func)(struct fib6_info *, void *),
2074 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
2077 static void fib6_flush_trees(struct net *net)
2079 int new_sernum = fib6_new_sernum(net);
2081 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
2085 * Garbage collection
2088 static int fib6_age(struct fib6_info *rt, void *arg)
2090 struct fib6_gc_args *gc_args = arg;
2091 unsigned long now = jiffies;
2094 * check addrconf expiration here.
2095 * Routes are expired even if they are in use.
2098 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
2099 if (time_after(now, rt->expires)) {
2100 RT6_TRACE("expiring %p\n", rt);
2106 /* Also age clones in the exception table.
2107 * Note, that clones are aged out
2108 * only if they are not in use now.
2110 rt6_age_exceptions(rt, gc_args, now);
2115 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2117 struct fib6_gc_args gc_args;
2121 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2122 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2123 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2126 gc_args.timeout = expires ? (int)expires :
2127 net->ipv6.sysctl.ip6_rt_gc_interval;
2130 fib6_clean_all(net, fib6_age, &gc_args);
2132 net->ipv6.ip6_rt_last_gc = now;
2135 mod_timer(&net->ipv6.ip6_fib_timer,
2137 + net->ipv6.sysctl.ip6_rt_gc_interval));
2139 del_timer(&net->ipv6.ip6_fib_timer);
2140 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2143 static void fib6_gc_timer_cb(struct timer_list *t)
2145 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2147 fib6_run_gc(0, arg, true);
2150 static int __net_init fib6_net_init(struct net *net)
2152 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2155 err = fib6_notifier_init(net);
2159 spin_lock_init(&net->ipv6.fib6_gc_lock);
2160 rwlock_init(&net->ipv6.fib6_walker_lock);
2161 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2162 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2164 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2165 if (!net->ipv6.rt6_stats)
2168 /* Avoid false sharing : Use at least a full cache line */
2169 size = max_t(size_t, size, L1_CACHE_BYTES);
2171 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2172 if (!net->ipv6.fib_table_hash)
2175 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2177 if (!net->ipv6.fib6_main_tbl)
2178 goto out_fib_table_hash;
2180 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2181 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2182 net->ipv6.fib6_null_entry);
2183 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2184 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2185 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2187 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2188 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2190 if (!net->ipv6.fib6_local_tbl)
2191 goto out_fib6_main_tbl;
2192 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2193 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2194 net->ipv6.fib6_null_entry);
2195 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2196 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2197 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2199 fib6_tables_init(net);
2203 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2205 kfree(net->ipv6.fib6_main_tbl);
2208 kfree(net->ipv6.fib_table_hash);
2210 kfree(net->ipv6.rt6_stats);
2212 fib6_notifier_exit(net);
2216 static void fib6_net_exit(struct net *net)
2220 del_timer_sync(&net->ipv6.ip6_fib_timer);
2222 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2223 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2224 struct hlist_node *tmp;
2225 struct fib6_table *tb;
2227 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2228 hlist_del(&tb->tb6_hlist);
2229 fib6_free_table(tb);
2233 kfree(net->ipv6.fib_table_hash);
2234 kfree(net->ipv6.rt6_stats);
2235 fib6_notifier_exit(net);
2238 static struct pernet_operations fib6_net_ops = {
2239 .init = fib6_net_init,
2240 .exit = fib6_net_exit,
2243 int __init fib6_init(void)
2247 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2248 sizeof(struct fib6_node),
2249 0, SLAB_HWCACHE_ALIGN,
2251 if (!fib6_node_kmem)
2254 ret = register_pernet_subsys(&fib6_net_ops);
2256 goto out_kmem_cache_create;
2258 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2261 goto out_unregister_subsys;
2263 __fib6_flush_trees = fib6_flush_trees;
2267 out_unregister_subsys:
2268 unregister_pernet_subsys(&fib6_net_ops);
2269 out_kmem_cache_create:
2270 kmem_cache_destroy(fib6_node_kmem);
2274 void fib6_gc_cleanup(void)
2276 unregister_pernet_subsys(&fib6_net_ops);
2277 kmem_cache_destroy(fib6_node_kmem);
2280 #ifdef CONFIG_PROC_FS
2281 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2283 struct fib6_info *rt = v;
2284 struct ipv6_route_iter *iter = seq->private;
2285 unsigned int flags = rt->fib6_flags;
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_nh.fib_nh_gw_family) {
2296 flags |= RTF_GATEWAY;
2297 seq_printf(seq, "%pi6", &rt->fib6_nh.fib_nh_gw6);
2299 seq_puts(seq, "00000000000000000000000000000000");
2302 dev = rt->fib6_nh.fib_nh_dev;
2303 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2304 rt->fib6_metric, refcount_read(&rt->fib6_ref), 0,
2305 flags, dev ? dev->name : "");
2306 iter->w.leaf = NULL;
2310 static int ipv6_route_yield(struct fib6_walker *w)
2312 struct ipv6_route_iter *iter = w->args;
2318 iter->w.leaf = rcu_dereference_protected(
2319 iter->w.leaf->fib6_next,
2320 lockdep_is_held(&iter->tbl->tb6_lock));
2322 if (!iter->skip && iter->w.leaf)
2324 } while (iter->w.leaf);
2329 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2332 memset(&iter->w, 0, sizeof(iter->w));
2333 iter->w.func = ipv6_route_yield;
2334 iter->w.root = &iter->tbl->tb6_root;
2335 iter->w.state = FWS_INIT;
2336 iter->w.node = iter->w.root;
2337 iter->w.args = iter;
2338 iter->sernum = iter->w.root->fn_sernum;
2339 INIT_LIST_HEAD(&iter->w.lh);
2340 fib6_walker_link(net, &iter->w);
2343 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2347 struct hlist_node *node;
2350 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2351 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2357 while (!node && h < FIB6_TABLE_HASHSZ) {
2358 node = rcu_dereference_bh(
2359 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2361 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2364 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2366 if (iter->sernum != iter->w.root->fn_sernum) {
2367 iter->sernum = iter->w.root->fn_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;
2385 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
2392 ipv6_route_check_sernum(iter);
2393 spin_lock_bh(&iter->tbl->tb6_lock);
2394 r = fib6_walk_continue(&iter->w);
2395 spin_unlock_bh(&iter->tbl->tb6_lock);
2399 return iter->w.leaf;
2401 fib6_walker_unlink(net, &iter->w);
2404 fib6_walker_unlink(net, &iter->w);
2406 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2410 ipv6_route_seq_setup_walk(iter, net);
2414 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2417 struct net *net = seq_file_net(seq);
2418 struct ipv6_route_iter *iter = seq->private;
2421 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2425 ipv6_route_seq_setup_walk(iter, net);
2426 return ipv6_route_seq_next(seq, NULL, pos);
2432 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2434 struct fib6_walker *w = &iter->w;
2435 return w->node && !(w->state == FWS_U && w->node == w->root);
2438 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2441 struct net *net = seq_file_net(seq);
2442 struct ipv6_route_iter *iter = seq->private;
2444 if (ipv6_route_iter_active(iter))
2445 fib6_walker_unlink(net, &iter->w);
2447 rcu_read_unlock_bh();
2450 const struct seq_operations ipv6_route_seq_ops = {
2451 .start = ipv6_route_seq_start,
2452 .next = ipv6_route_seq_next,
2453 .stop = ipv6_route_seq_stop,
2454 .show = ipv6_route_seq_show
2456 #endif /* CONFIG_PROC_FS */