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 f6i->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
159 if (!f6i->rt6i_pcpu) {
164 INIT_LIST_HEAD(&f6i->fib6_siblings);
165 refcount_set(&f6i->fib6_ref, 1);
170 void fib6_info_destroy_rcu(struct rcu_head *head)
172 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
173 struct rt6_exception_bucket *bucket;
175 WARN_ON(f6i->fib6_node);
177 bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
180 if (f6i->rt6i_pcpu) {
183 for_each_possible_cpu(cpu) {
184 struct rt6_info **ppcpu_rt;
185 struct rt6_info *pcpu_rt;
187 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
190 dst_dev_put(&pcpu_rt->dst);
191 dst_release(&pcpu_rt->dst);
196 free_percpu(f6i->rt6i_pcpu);
199 fib6_nh_release(&f6i->fib6_nh);
201 ip_fib_metrics_put(f6i->fib6_metrics);
205 EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
207 static struct fib6_node *node_alloc(struct net *net)
209 struct fib6_node *fn;
211 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
213 net->ipv6.rt6_stats->fib_nodes++;
218 static void node_free_immediate(struct net *net, struct fib6_node *fn)
220 kmem_cache_free(fib6_node_kmem, fn);
221 net->ipv6.rt6_stats->fib_nodes--;
224 static void node_free_rcu(struct rcu_head *head)
226 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
228 kmem_cache_free(fib6_node_kmem, fn);
231 static void node_free(struct net *net, struct fib6_node *fn)
233 call_rcu(&fn->rcu, node_free_rcu);
234 net->ipv6.rt6_stats->fib_nodes--;
237 static void fib6_free_table(struct fib6_table *table)
239 inetpeer_invalidate_tree(&table->tb6_peers);
243 static void fib6_link_table(struct net *net, struct fib6_table *tb)
248 * Initialize table lock at a single place to give lockdep a key,
249 * tables aren't visible prior to being linked to the list.
251 spin_lock_init(&tb->tb6_lock);
252 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
255 * No protection necessary, this is the only list mutatation
256 * operation, tables never disappear once they exist.
258 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
261 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
263 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
265 struct fib6_table *table;
267 table = kzalloc(sizeof(*table), GFP_ATOMIC);
270 rcu_assign_pointer(table->tb6_root.leaf,
271 net->ipv6.fib6_null_entry);
272 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
273 inet_peer_base_init(&table->tb6_peers);
279 struct fib6_table *fib6_new_table(struct net *net, u32 id)
281 struct fib6_table *tb;
285 tb = fib6_get_table(net, id);
289 tb = fib6_alloc_table(net, id);
291 fib6_link_table(net, tb);
295 EXPORT_SYMBOL_GPL(fib6_new_table);
297 struct fib6_table *fib6_get_table(struct net *net, u32 id)
299 struct fib6_table *tb;
300 struct hlist_head *head;
305 h = id & (FIB6_TABLE_HASHSZ - 1);
307 head = &net->ipv6.fib_table_hash[h];
308 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
309 if (tb->tb6_id == id) {
318 EXPORT_SYMBOL_GPL(fib6_get_table);
320 static void __net_init fib6_tables_init(struct net *net)
322 fib6_link_table(net, net->ipv6.fib6_main_tbl);
323 fib6_link_table(net, net->ipv6.fib6_local_tbl);
327 struct fib6_table *fib6_new_table(struct net *net, u32 id)
329 return fib6_get_table(net, id);
332 struct fib6_table *fib6_get_table(struct net *net, u32 id)
334 return net->ipv6.fib6_main_tbl;
337 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
338 const struct sk_buff *skb,
339 int flags, pol_lookup_t lookup)
343 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
344 if (rt->dst.error == -EAGAIN) {
346 rt = net->ipv6.ip6_null_entry;
353 /* called with rcu lock held; no reference taken on fib6_info */
354 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
355 struct fib6_result *res, int flags)
357 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6,
361 static void __net_init fib6_tables_init(struct net *net)
363 fib6_link_table(net, net->ipv6.fib6_main_tbl);
368 unsigned int fib6_tables_seq_read(struct net *net)
370 unsigned int h, fib_seq = 0;
373 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
374 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
375 struct fib6_table *tb;
377 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
378 fib_seq += tb->fib_seq;
385 static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
386 enum fib_event_type event_type,
387 struct fib6_info *rt)
389 struct fib6_entry_notifier_info info = {
393 return call_fib6_notifier(nb, net, event_type, &info.info);
396 static int call_fib6_entry_notifiers(struct net *net,
397 enum fib_event_type event_type,
398 struct fib6_info *rt,
399 struct netlink_ext_ack *extack)
401 struct fib6_entry_notifier_info info = {
402 .info.extack = extack,
406 rt->fib6_table->fib_seq++;
407 return call_fib6_notifiers(net, event_type, &info.info);
410 struct fib6_dump_arg {
412 struct notifier_block *nb;
415 static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
417 if (rt == arg->net->ipv6.fib6_null_entry)
419 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
422 static int fib6_node_dump(struct fib6_walker *w)
424 struct fib6_info *rt;
426 for_each_fib6_walker_rt(w)
427 fib6_rt_dump(rt, w->args);
432 static void fib6_table_dump(struct net *net, struct fib6_table *tb,
433 struct fib6_walker *w)
435 w->root = &tb->tb6_root;
436 spin_lock_bh(&tb->tb6_lock);
438 spin_unlock_bh(&tb->tb6_lock);
441 /* Called with rcu_read_lock() */
442 int fib6_tables_dump(struct net *net, struct notifier_block *nb)
444 struct fib6_dump_arg arg;
445 struct fib6_walker *w;
448 w = kzalloc(sizeof(*w), GFP_ATOMIC);
452 w->func = fib6_node_dump;
457 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
458 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
459 struct fib6_table *tb;
461 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
462 fib6_table_dump(net, tb, w);
470 static int fib6_dump_node(struct fib6_walker *w)
473 struct fib6_info *rt;
475 for_each_fib6_walker_rt(w) {
476 res = rt6_dump_route(rt, w->args);
478 /* Frame is full, suspend walking */
483 /* Multipath routes are dumped in one route with the
484 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
485 * last sibling of this route (no need to dump the
486 * sibling routes again)
488 if (rt->fib6_nsiblings)
489 rt = list_last_entry(&rt->fib6_siblings,
497 static void fib6_dump_end(struct netlink_callback *cb)
499 struct net *net = sock_net(cb->skb->sk);
500 struct fib6_walker *w = (void *)cb->args[2];
505 fib6_walker_unlink(net, w);
510 cb->done = (void *)cb->args[3];
514 static int fib6_dump_done(struct netlink_callback *cb)
517 return cb->done ? cb->done(cb) : 0;
520 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
521 struct netlink_callback *cb)
523 struct net *net = sock_net(skb->sk);
524 struct fib6_walker *w;
527 w = (void *)cb->args[2];
528 w->root = &table->tb6_root;
530 if (cb->args[4] == 0) {
534 spin_lock_bh(&table->tb6_lock);
535 res = fib6_walk(net, w);
536 spin_unlock_bh(&table->tb6_lock);
539 cb->args[5] = w->root->fn_sernum;
542 if (cb->args[5] != w->root->fn_sernum) {
543 /* Begin at the root if the tree changed */
544 cb->args[5] = w->root->fn_sernum;
551 spin_lock_bh(&table->tb6_lock);
552 res = fib6_walk_continue(w);
553 spin_unlock_bh(&table->tb6_lock);
555 fib6_walker_unlink(net, w);
563 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
565 const struct nlmsghdr *nlh = cb->nlh;
566 struct net *net = sock_net(skb->sk);
567 struct rt6_rtnl_dump_arg arg = {};
569 unsigned int e = 0, s_e;
570 struct fib6_walker *w;
571 struct fib6_table *tb;
572 struct hlist_head *head;
575 if (cb->strict_check) {
578 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
581 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
582 struct rtmsg *rtm = nlmsg_data(nlh);
584 arg.filter.flags = rtm->rtm_flags & (RTM_F_PREFIX|RTM_F_CLONED);
587 /* fib entries are never clones */
588 if (arg.filter.flags & RTM_F_CLONED)
591 w = (void *)cb->args[2];
595 * 1. hook callback destructor.
597 cb->args[3] = (long)cb->done;
598 cb->done = fib6_dump_done;
601 * 2. allocate and initialize walker.
603 w = kzalloc(sizeof(*w), GFP_ATOMIC);
606 w->func = fib6_dump_node;
607 cb->args[2] = (long)w;
615 if (arg.filter.table_id) {
616 tb = fib6_get_table(net, arg.filter.table_id);
618 if (arg.filter.dump_all_families)
621 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
626 res = fib6_dump_table(tb, skb, cb);
637 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
639 head = &net->ipv6.fib_table_hash[h];
640 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
643 res = fib6_dump_table(tb, skb, cb);
655 res = res < 0 ? res : skb->len;
661 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
666 if (f6i->fib6_metrics == &dst_default_metrics) {
667 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
672 refcount_set(&p->refcnt, 1);
673 f6i->fib6_metrics = p;
676 f6i->fib6_metrics->metrics[metric - 1] = val;
682 * return the appropriate node for a routing tree "add" operation
683 * by either creating and inserting or by returning an existing
687 static struct fib6_node *fib6_add_1(struct net *net,
688 struct fib6_table *table,
689 struct fib6_node *root,
690 struct in6_addr *addr, int plen,
691 int offset, int allow_create,
692 int replace_required,
693 struct netlink_ext_ack *extack)
695 struct fib6_node *fn, *in, *ln;
696 struct fib6_node *pn = NULL;
701 RT6_TRACE("fib6_add_1\n");
703 /* insert node in tree */
708 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
709 lockdep_is_held(&table->tb6_lock));
710 key = (struct rt6key *)((u8 *)leaf + offset);
715 if (plen < fn->fn_bit ||
716 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
718 if (replace_required) {
719 NL_SET_ERR_MSG(extack,
720 "Can not replace route - no match found");
721 pr_warn("Can't replace route, no match found\n");
722 return ERR_PTR(-ENOENT);
724 pr_warn("NLM_F_CREATE should be set when creating new route\n");
733 if (plen == fn->fn_bit) {
734 /* clean up an intermediate node */
735 if (!(fn->fn_flags & RTN_RTINFO)) {
736 RCU_INIT_POINTER(fn->leaf, NULL);
737 fib6_info_release(leaf);
738 /* remove null_entry in the root node */
739 } else if (fn->fn_flags & RTN_TL_ROOT &&
740 rcu_access_pointer(fn->leaf) ==
741 net->ipv6.fib6_null_entry) {
742 RCU_INIT_POINTER(fn->leaf, NULL);
749 * We have more bits to go
752 /* Try to walk down on tree. */
753 dir = addr_bit_set(addr, fn->fn_bit);
756 rcu_dereference_protected(fn->right,
757 lockdep_is_held(&table->tb6_lock)) :
758 rcu_dereference_protected(fn->left,
759 lockdep_is_held(&table->tb6_lock));
763 /* We should not create new node because
764 * NLM_F_REPLACE was specified without NLM_F_CREATE
765 * I assume it is safe to require NLM_F_CREATE when
766 * REPLACE flag is used! Later we may want to remove the
767 * check for replace_required, because according
768 * to netlink specification, NLM_F_CREATE
769 * MUST be specified if new route is created.
770 * That would keep IPv6 consistent with IPv4
772 if (replace_required) {
773 NL_SET_ERR_MSG(extack,
774 "Can not replace route - no match found");
775 pr_warn("Can't replace route, no match found\n");
776 return ERR_PTR(-ENOENT);
778 pr_warn("NLM_F_CREATE should be set when creating new route\n");
781 * We walked to the bottom of tree.
782 * Create new leaf node without children.
785 ln = node_alloc(net);
788 return ERR_PTR(-ENOMEM);
790 RCU_INIT_POINTER(ln->parent, pn);
793 rcu_assign_pointer(pn->right, ln);
795 rcu_assign_pointer(pn->left, ln);
802 * split since we don't have a common prefix anymore or
803 * we have a less significant route.
804 * we've to insert an intermediate node on the list
805 * this new node will point to the one we need to create
809 pn = rcu_dereference_protected(fn->parent,
810 lockdep_is_held(&table->tb6_lock));
812 /* find 1st bit in difference between the 2 addrs.
814 See comment in __ipv6_addr_diff: bit may be an invalid value,
815 but if it is >= plen, the value is ignored in any case.
818 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
823 * (new leaf node)[ln] (old node)[fn]
826 in = node_alloc(net);
827 ln = node_alloc(net);
831 node_free_immediate(net, in);
833 node_free_immediate(net, ln);
834 return ERR_PTR(-ENOMEM);
838 * new intermediate node.
840 * be off since that an address that chooses one of
841 * the branches would not match less specific routes
842 * in the other branch
847 RCU_INIT_POINTER(in->parent, pn);
849 fib6_info_hold(rcu_dereference_protected(in->leaf,
850 lockdep_is_held(&table->tb6_lock)));
852 /* update parent pointer */
854 rcu_assign_pointer(pn->right, in);
856 rcu_assign_pointer(pn->left, in);
860 RCU_INIT_POINTER(ln->parent, in);
861 rcu_assign_pointer(fn->parent, in);
863 if (addr_bit_set(addr, bit)) {
864 rcu_assign_pointer(in->right, ln);
865 rcu_assign_pointer(in->left, fn);
867 rcu_assign_pointer(in->left, ln);
868 rcu_assign_pointer(in->right, fn);
870 } else { /* plen <= bit */
873 * (new leaf node)[ln]
875 * (old node)[fn] NULL
878 ln = node_alloc(net);
881 return ERR_PTR(-ENOMEM);
885 RCU_INIT_POINTER(ln->parent, pn);
887 if (addr_bit_set(&key->addr, plen))
888 RCU_INIT_POINTER(ln->right, fn);
890 RCU_INIT_POINTER(ln->left, fn);
892 rcu_assign_pointer(fn->parent, ln);
895 rcu_assign_pointer(pn->right, ln);
897 rcu_assign_pointer(pn->left, ln);
902 static void fib6_drop_pcpu_from(struct fib6_info *f6i,
903 const struct fib6_table *table)
907 /* Make sure rt6_make_pcpu_route() wont add other percpu routes
908 * while we are cleaning them here.
910 f6i->fib6_destroying = 1;
911 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
913 /* release the reference to this fib entry from
914 * all of its cached pcpu routes
916 for_each_possible_cpu(cpu) {
917 struct rt6_info **ppcpu_rt;
918 struct rt6_info *pcpu_rt;
920 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
923 struct fib6_info *from;
925 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
926 fib6_info_release(from);
931 static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
934 struct fib6_table *table = rt->fib6_table;
937 fib6_drop_pcpu_from(rt, table);
939 if (refcount_read(&rt->fib6_ref) != 1) {
940 /* This route is used as dummy address holder in some split
941 * nodes. It is not leaked, but it still holds other resources,
942 * which must be released in time. So, scan ascendant nodes
943 * and replace dummy references to this route with references
944 * to still alive ones.
947 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
948 lockdep_is_held(&table->tb6_lock));
949 struct fib6_info *new_leaf;
950 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
951 new_leaf = fib6_find_prefix(net, table, fn);
952 fib6_info_hold(new_leaf);
954 rcu_assign_pointer(fn->leaf, new_leaf);
955 fib6_info_release(rt);
957 fn = rcu_dereference_protected(fn->parent,
958 lockdep_is_held(&table->tb6_lock));
964 * Insert routing information in a node.
967 static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
968 struct nl_info *info,
969 struct netlink_ext_ack *extack)
971 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
972 lockdep_is_held(&rt->fib6_table->tb6_lock));
973 struct fib6_info *iter = NULL;
974 struct fib6_info __rcu **ins;
975 struct fib6_info __rcu **fallback_ins = NULL;
976 int replace = (info->nlh &&
977 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
978 int add = (!info->nlh ||
979 (info->nlh->nlmsg_flags & NLM_F_CREATE));
981 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
982 u16 nlflags = NLM_F_EXCL;
985 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
986 nlflags |= NLM_F_APPEND;
990 for (iter = leaf; iter;
991 iter = rcu_dereference_protected(iter->fib6_next,
992 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
994 * Search for duplicates
997 if (iter->fib6_metric == rt->fib6_metric) {
999 * Same priority level
1002 (info->nlh->nlmsg_flags & NLM_F_EXCL))
1005 nlflags &= ~NLM_F_EXCL;
1007 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
1012 fallback_ins = fallback_ins ?: ins;
1016 if (rt6_duplicate_nexthop(iter, rt)) {
1017 if (rt->fib6_nsiblings)
1018 rt->fib6_nsiblings = 0;
1019 if (!(iter->fib6_flags & RTF_EXPIRES))
1021 if (!(rt->fib6_flags & RTF_EXPIRES))
1022 fib6_clean_expires(iter);
1024 fib6_set_expires(iter, rt->expires);
1027 fib6_metric_set(iter, RTAX_MTU,
1031 /* If we have the same destination and the same metric,
1032 * but not the same gateway, then the route we try to
1033 * add is sibling to this route, increment our counter
1034 * of siblings, and later we will add our route to the
1036 * Only static routes (which don't have flag
1037 * RTF_EXPIRES) are used for ECMPv6.
1039 * To avoid long list, we only had siblings if the
1040 * route have a gateway.
1043 rt6_qualify_for_ecmp(iter))
1044 rt->fib6_nsiblings++;
1047 if (iter->fib6_metric > rt->fib6_metric)
1051 ins = &iter->fib6_next;
1054 if (fallback_ins && !found) {
1055 /* No ECMP-able route found, replace first non-ECMP one */
1057 iter = rcu_dereference_protected(*ins,
1058 lockdep_is_held(&rt->fib6_table->tb6_lock));
1062 /* Reset round-robin state, if necessary */
1063 if (ins == &fn->leaf)
1066 /* Link this route to others same route. */
1067 if (rt->fib6_nsiblings) {
1068 unsigned int fib6_nsiblings;
1069 struct fib6_info *sibling, *temp_sibling;
1071 /* Find the first route that have the same metric */
1074 if (sibling->fib6_metric == rt->fib6_metric &&
1075 rt6_qualify_for_ecmp(sibling)) {
1076 list_add_tail(&rt->fib6_siblings,
1077 &sibling->fib6_siblings);
1080 sibling = rcu_dereference_protected(sibling->fib6_next,
1081 lockdep_is_held(&rt->fib6_table->tb6_lock));
1083 /* For each sibling in the list, increment the counter of
1084 * siblings. BUG() if counters does not match, list of siblings
1088 list_for_each_entry_safe(sibling, temp_sibling,
1089 &rt->fib6_siblings, fib6_siblings) {
1090 sibling->fib6_nsiblings++;
1091 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1094 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1095 rt6_multipath_rebalance(temp_sibling);
1103 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1106 nlflags |= NLM_F_CREATE;
1108 err = call_fib6_entry_notifiers(info->nl_net,
1109 FIB_EVENT_ENTRY_ADD,
1114 rcu_assign_pointer(rt->fib6_next, iter);
1116 rcu_assign_pointer(rt->fib6_node, fn);
1117 rcu_assign_pointer(*ins, rt);
1118 if (!info->skip_notify)
1119 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1120 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1122 if (!(fn->fn_flags & RTN_RTINFO)) {
1123 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1124 fn->fn_flags |= RTN_RTINFO;
1133 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1137 err = call_fib6_entry_notifiers(info->nl_net,
1138 FIB_EVENT_ENTRY_REPLACE,
1144 rcu_assign_pointer(rt->fib6_node, fn);
1145 rt->fib6_next = iter->fib6_next;
1146 rcu_assign_pointer(*ins, rt);
1147 if (!info->skip_notify)
1148 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1149 if (!(fn->fn_flags & RTN_RTINFO)) {
1150 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1151 fn->fn_flags |= RTN_RTINFO;
1153 nsiblings = iter->fib6_nsiblings;
1154 iter->fib6_node = NULL;
1155 fib6_purge_rt(iter, fn, info->nl_net);
1156 if (rcu_access_pointer(fn->rr_ptr) == iter)
1158 fib6_info_release(iter);
1161 /* Replacing an ECMP route, remove all siblings */
1162 ins = &rt->fib6_next;
1163 iter = rcu_dereference_protected(*ins,
1164 lockdep_is_held(&rt->fib6_table->tb6_lock));
1166 if (iter->fib6_metric > rt->fib6_metric)
1168 if (rt6_qualify_for_ecmp(iter)) {
1169 *ins = iter->fib6_next;
1170 iter->fib6_node = NULL;
1171 fib6_purge_rt(iter, fn, info->nl_net);
1172 if (rcu_access_pointer(fn->rr_ptr) == iter)
1174 fib6_info_release(iter);
1176 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1178 ins = &iter->fib6_next;
1180 iter = rcu_dereference_protected(*ins,
1181 lockdep_is_held(&rt->fib6_table->tb6_lock));
1183 WARN_ON(nsiblings != 0);
1190 static void fib6_start_gc(struct net *net, struct fib6_info *rt)
1192 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1193 (rt->fib6_flags & RTF_EXPIRES))
1194 mod_timer(&net->ipv6.ip6_fib_timer,
1195 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1198 void fib6_force_start_gc(struct net *net)
1200 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1201 mod_timer(&net->ipv6.ip6_fib_timer,
1202 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1205 static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
1208 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1209 lockdep_is_held(&rt->fib6_table->tb6_lock));
1211 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1214 fn->fn_sernum = sernum;
1215 fn = rcu_dereference_protected(fn->parent,
1216 lockdep_is_held(&rt->fib6_table->tb6_lock));
1220 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
1222 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1226 * Add routing information to the routing tree.
1227 * <destination addr>/<source addr>
1228 * with source addr info in sub-trees
1229 * Need to own table->tb6_lock
1232 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
1233 struct nl_info *info, struct netlink_ext_ack *extack)
1235 struct fib6_table *table = rt->fib6_table;
1236 struct fib6_node *fn, *pn = NULL;
1238 int allow_create = 1;
1239 int replace_required = 0;
1240 int sernum = fib6_new_sernum(info->nl_net);
1243 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1245 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1246 replace_required = 1;
1248 if (!allow_create && !replace_required)
1249 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1251 fn = fib6_add_1(info->nl_net, table, root,
1252 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1253 offsetof(struct fib6_info, fib6_dst), allow_create,
1254 replace_required, extack);
1263 #ifdef CONFIG_IPV6_SUBTREES
1264 if (rt->fib6_src.plen) {
1265 struct fib6_node *sn;
1267 if (!rcu_access_pointer(fn->subtree)) {
1268 struct fib6_node *sfn;
1280 /* Create subtree root node */
1281 sfn = node_alloc(info->nl_net);
1285 fib6_info_hold(info->nl_net->ipv6.fib6_null_entry);
1286 rcu_assign_pointer(sfn->leaf,
1287 info->nl_net->ipv6.fib6_null_entry);
1288 sfn->fn_flags = RTN_ROOT;
1290 /* Now add the first leaf node to new subtree */
1292 sn = fib6_add_1(info->nl_net, table, sfn,
1293 &rt->fib6_src.addr, rt->fib6_src.plen,
1294 offsetof(struct fib6_info, fib6_src),
1295 allow_create, replace_required, extack);
1298 /* If it is failed, discard just allocated
1299 root, and then (in failure) stale node
1302 node_free_immediate(info->nl_net, sfn);
1307 /* Now link new subtree to main tree */
1308 rcu_assign_pointer(sfn->parent, fn);
1309 rcu_assign_pointer(fn->subtree, sfn);
1311 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1312 &rt->fib6_src.addr, rt->fib6_src.plen,
1313 offsetof(struct fib6_info, fib6_src),
1314 allow_create, replace_required, extack);
1322 if (!rcu_access_pointer(fn->leaf)) {
1323 if (fn->fn_flags & RTN_TL_ROOT) {
1324 /* put back null_entry for root node */
1325 rcu_assign_pointer(fn->leaf,
1326 info->nl_net->ipv6.fib6_null_entry);
1329 rcu_assign_pointer(fn->leaf, rt);
1336 err = fib6_add_rt2node(fn, rt, info, extack);
1338 __fib6_update_sernum_upto_root(rt, sernum);
1339 fib6_start_gc(info->nl_net, rt);
1344 #ifdef CONFIG_IPV6_SUBTREES
1346 * If fib6_add_1 has cleared the old leaf pointer in the
1347 * super-tree leaf node we have to find a new one for it.
1350 struct fib6_info *pn_leaf =
1351 rcu_dereference_protected(pn->leaf,
1352 lockdep_is_held(&table->tb6_lock));
1353 if (pn_leaf == rt) {
1355 RCU_INIT_POINTER(pn->leaf, NULL);
1356 fib6_info_release(rt);
1358 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1359 pn_leaf = fib6_find_prefix(info->nl_net, table,
1365 info->nl_net->ipv6.fib6_null_entry;
1368 fib6_info_hold(pn_leaf);
1369 rcu_assign_pointer(pn->leaf, pn_leaf);
1378 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1379 * 1. fn is an intermediate node and we failed to add the new
1380 * route to it in both subtree creation failure and fib6_add_rt2node()
1382 * 2. fn is the root node in the table and we fail to add the first
1383 * default route to it.
1386 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1387 (fn->fn_flags & RTN_TL_ROOT &&
1388 !rcu_access_pointer(fn->leaf))))
1389 fib6_repair_tree(info->nl_net, table, fn);
1394 * Routing tree lookup
1398 struct lookup_args {
1399 int offset; /* key offset on fib6_info */
1400 const struct in6_addr *addr; /* search key */
1403 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1404 struct lookup_args *args)
1406 struct fib6_node *fn;
1409 if (unlikely(args->offset == 0))
1419 struct fib6_node *next;
1421 dir = addr_bit_set(args->addr, fn->fn_bit);
1423 next = dir ? rcu_dereference(fn->right) :
1424 rcu_dereference(fn->left);
1434 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1436 if (subtree || fn->fn_flags & RTN_RTINFO) {
1437 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1443 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1445 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1446 #ifdef CONFIG_IPV6_SUBTREES
1448 struct fib6_node *sfn;
1449 sfn = fib6_node_lookup_1(subtree,
1456 if (fn->fn_flags & RTN_RTINFO)
1461 if (fn->fn_flags & RTN_ROOT)
1464 fn = rcu_dereference(fn->parent);
1470 /* called with rcu_read_lock() held
1472 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1473 const struct in6_addr *daddr,
1474 const struct in6_addr *saddr)
1476 struct fib6_node *fn;
1477 struct lookup_args args[] = {
1479 .offset = offsetof(struct fib6_info, fib6_dst),
1482 #ifdef CONFIG_IPV6_SUBTREES
1484 .offset = offsetof(struct fib6_info, fib6_src),
1489 .offset = 0, /* sentinel */
1493 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
1494 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1501 * Get node with specified destination prefix (and source prefix,
1502 * if subtrees are used)
1503 * exact_match == true means we try to find fn with exact match of
1504 * the passed in prefix addr
1505 * exact_match == false means we try to find fn with longest prefix
1506 * match of the passed in prefix addr. This is useful for finding fn
1507 * for cached route as it will be stored in the exception table under
1508 * the node with longest prefix length.
1512 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1513 const struct in6_addr *addr,
1514 int plen, int offset,
1517 struct fib6_node *fn, *prev = NULL;
1519 for (fn = root; fn ; ) {
1520 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1523 /* This node is being deleted */
1525 if (plen <= fn->fn_bit)
1531 key = (struct rt6key *)((u8 *)leaf + offset);
1536 if (plen < fn->fn_bit ||
1537 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1540 if (plen == fn->fn_bit)
1547 * We have more bits to go
1549 if (addr_bit_set(addr, fn->fn_bit))
1550 fn = rcu_dereference(fn->right);
1552 fn = rcu_dereference(fn->left);
1561 struct fib6_node *fib6_locate(struct fib6_node *root,
1562 const struct in6_addr *daddr, int dst_len,
1563 const struct in6_addr *saddr, int src_len,
1566 struct fib6_node *fn;
1568 fn = fib6_locate_1(root, daddr, dst_len,
1569 offsetof(struct fib6_info, fib6_dst),
1572 #ifdef CONFIG_IPV6_SUBTREES
1574 WARN_ON(saddr == NULL);
1576 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1579 fn = fib6_locate_1(subtree, saddr, src_len,
1580 offsetof(struct fib6_info, fib6_src),
1587 if (fn && fn->fn_flags & RTN_RTINFO)
1599 static struct fib6_info *fib6_find_prefix(struct net *net,
1600 struct fib6_table *table,
1601 struct fib6_node *fn)
1603 struct fib6_node *child_left, *child_right;
1605 if (fn->fn_flags & RTN_ROOT)
1606 return net->ipv6.fib6_null_entry;
1609 child_left = rcu_dereference_protected(fn->left,
1610 lockdep_is_held(&table->tb6_lock));
1611 child_right = rcu_dereference_protected(fn->right,
1612 lockdep_is_held(&table->tb6_lock));
1614 return rcu_dereference_protected(child_left->leaf,
1615 lockdep_is_held(&table->tb6_lock));
1617 return rcu_dereference_protected(child_right->leaf,
1618 lockdep_is_held(&table->tb6_lock));
1620 fn = FIB6_SUBTREE(fn);
1626 * Called to trim the tree of intermediate nodes when possible. "fn"
1627 * is the node we want to try and remove.
1628 * Need to own table->tb6_lock
1631 static struct fib6_node *fib6_repair_tree(struct net *net,
1632 struct fib6_table *table,
1633 struct fib6_node *fn)
1637 struct fib6_node *child;
1638 struct fib6_walker *w;
1641 /* Set fn->leaf to null_entry for root node. */
1642 if (fn->fn_flags & RTN_TL_ROOT) {
1643 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1648 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1649 lockdep_is_held(&table->tb6_lock));
1650 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1651 lockdep_is_held(&table->tb6_lock));
1652 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1653 lockdep_is_held(&table->tb6_lock));
1654 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1655 lockdep_is_held(&table->tb6_lock));
1656 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1657 lockdep_is_held(&table->tb6_lock));
1658 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1659 lockdep_is_held(&table->tb6_lock));
1660 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1661 lockdep_is_held(&table->tb6_lock));
1662 struct fib6_info *new_fn_leaf;
1664 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1667 WARN_ON(fn->fn_flags & RTN_RTINFO);
1668 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1674 child = fn_r, children |= 1;
1676 child = fn_l, children |= 2;
1678 if (children == 3 || FIB6_SUBTREE(fn)
1679 #ifdef CONFIG_IPV6_SUBTREES
1680 /* Subtree root (i.e. fn) may have one child */
1681 || (children && fn->fn_flags & RTN_ROOT)
1684 new_fn_leaf = fib6_find_prefix(net, table, fn);
1687 WARN_ON(!new_fn_leaf);
1688 new_fn_leaf = net->ipv6.fib6_null_entry;
1691 fib6_info_hold(new_fn_leaf);
1692 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1696 #ifdef CONFIG_IPV6_SUBTREES
1697 if (FIB6_SUBTREE(pn) == fn) {
1698 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1699 RCU_INIT_POINTER(pn->subtree, NULL);
1702 WARN_ON(fn->fn_flags & RTN_ROOT);
1705 rcu_assign_pointer(pn->right, child);
1706 else if (pn_l == fn)
1707 rcu_assign_pointer(pn->left, child);
1713 rcu_assign_pointer(child->parent, pn);
1715 #ifdef CONFIG_IPV6_SUBTREES
1719 read_lock(&net->ipv6.fib6_walker_lock);
1720 FOR_WALKERS(net, w) {
1722 if (w->node == fn) {
1723 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1728 if (w->node == fn) {
1731 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1732 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1734 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1735 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1740 read_unlock(&net->ipv6.fib6_walker_lock);
1743 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1746 RCU_INIT_POINTER(pn->leaf, NULL);
1747 fib6_info_release(pn_leaf);
1752 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1753 struct fib6_info __rcu **rtp, struct nl_info *info)
1755 struct fib6_walker *w;
1756 struct fib6_info *rt = rcu_dereference_protected(*rtp,
1757 lockdep_is_held(&table->tb6_lock));
1758 struct net *net = info->nl_net;
1760 RT6_TRACE("fib6_del_route\n");
1763 *rtp = rt->fib6_next;
1764 rt->fib6_node = NULL;
1765 net->ipv6.rt6_stats->fib_rt_entries--;
1766 net->ipv6.rt6_stats->fib_discarded_routes++;
1768 /* Flush all cached dst in exception table */
1769 rt6_flush_exceptions(rt);
1771 /* Reset round-robin state, if necessary */
1772 if (rcu_access_pointer(fn->rr_ptr) == rt)
1775 /* Remove this entry from other siblings */
1776 if (rt->fib6_nsiblings) {
1777 struct fib6_info *sibling, *next_sibling;
1779 list_for_each_entry_safe(sibling, next_sibling,
1780 &rt->fib6_siblings, fib6_siblings)
1781 sibling->fib6_nsiblings--;
1782 rt->fib6_nsiblings = 0;
1783 list_del_init(&rt->fib6_siblings);
1784 rt6_multipath_rebalance(next_sibling);
1787 /* Adjust walkers */
1788 read_lock(&net->ipv6.fib6_walker_lock);
1789 FOR_WALKERS(net, w) {
1790 if (w->state == FWS_C && w->leaf == rt) {
1791 RT6_TRACE("walker %p adjusted by delroute\n", w);
1792 w->leaf = rcu_dereference_protected(rt->fib6_next,
1793 lockdep_is_held(&table->tb6_lock));
1798 read_unlock(&net->ipv6.fib6_walker_lock);
1800 /* If it was last route, call fib6_repair_tree() to:
1801 * 1. For root node, put back null_entry as how the table was created.
1802 * 2. For other nodes, expunge its radix tree node.
1804 if (!rcu_access_pointer(fn->leaf)) {
1805 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1806 fn->fn_flags &= ~RTN_RTINFO;
1807 net->ipv6.rt6_stats->fib_route_nodes--;
1809 fn = fib6_repair_tree(net, table, fn);
1812 fib6_purge_rt(rt, fn, net);
1814 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
1815 if (!info->skip_notify)
1816 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
1817 fib6_info_release(rt);
1820 /* Need to own table->tb6_lock */
1821 int fib6_del(struct fib6_info *rt, struct nl_info *info)
1823 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1824 lockdep_is_held(&rt->fib6_table->tb6_lock));
1825 struct fib6_table *table = rt->fib6_table;
1826 struct net *net = info->nl_net;
1827 struct fib6_info __rcu **rtp;
1828 struct fib6_info __rcu **rtp_next;
1830 if (!fn || rt == net->ipv6.fib6_null_entry)
1833 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1836 * Walk the leaf entries looking for ourself
1839 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1840 struct fib6_info *cur = rcu_dereference_protected(*rtp,
1841 lockdep_is_held(&table->tb6_lock));
1843 fib6_del_route(table, fn, rtp, info);
1846 rtp_next = &cur->fib6_next;
1852 * Tree traversal function.
1854 * Certainly, it is not interrupt safe.
1855 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1856 * It means, that we can modify tree during walking
1857 * and use this function for garbage collection, clone pruning,
1858 * cleaning tree when a device goes down etc. etc.
1860 * It guarantees that every node will be traversed,
1861 * and that it will be traversed only once.
1863 * Callback function w->func may return:
1864 * 0 -> continue walking.
1865 * positive value -> walking is suspended (used by tree dumps,
1866 * and probably by gc, if it will be split to several slices)
1867 * negative value -> terminate walking.
1869 * The function itself returns:
1870 * 0 -> walk is complete.
1871 * >0 -> walk is incomplete (i.e. suspended)
1872 * <0 -> walk is terminated by an error.
1874 * This function is called with tb6_lock held.
1877 static int fib6_walk_continue(struct fib6_walker *w)
1879 struct fib6_node *fn, *pn, *left, *right;
1881 /* w->root should always be table->tb6_root */
1882 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1890 #ifdef CONFIG_IPV6_SUBTREES
1892 if (FIB6_SUBTREE(fn)) {
1893 w->node = FIB6_SUBTREE(fn);
1900 left = rcu_dereference_protected(fn->left, 1);
1903 w->state = FWS_INIT;
1909 right = rcu_dereference_protected(fn->right, 1);
1912 w->state = FWS_INIT;
1916 w->leaf = rcu_dereference_protected(fn->leaf, 1);
1919 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1940 pn = rcu_dereference_protected(fn->parent, 1);
1941 left = rcu_dereference_protected(pn->left, 1);
1942 right = rcu_dereference_protected(pn->right, 1);
1944 #ifdef CONFIG_IPV6_SUBTREES
1945 if (FIB6_SUBTREE(pn) == fn) {
1946 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1957 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
1967 static int fib6_walk(struct net *net, struct fib6_walker *w)
1971 w->state = FWS_INIT;
1974 fib6_walker_link(net, w);
1975 res = fib6_walk_continue(w);
1977 fib6_walker_unlink(net, w);
1981 static int fib6_clean_node(struct fib6_walker *w)
1984 struct fib6_info *rt;
1985 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1986 struct nl_info info = {
1988 .skip_notify = c->skip_notify,
1991 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1992 w->node->fn_sernum != c->sernum)
1993 w->node->fn_sernum = c->sernum;
1996 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
2001 for_each_fib6_walker_rt(w) {
2002 res = c->func(rt, c->arg);
2005 res = fib6_del(rt, &info);
2008 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
2010 rcu_access_pointer(rt->fib6_node),
2016 } else if (res == -2) {
2017 if (WARN_ON(!rt->fib6_nsiblings))
2019 rt = list_last_entry(&rt->fib6_siblings,
2020 struct fib6_info, fib6_siblings);
2030 * Convenient frontend to tree walker.
2032 * func is called on each route.
2033 * It may return -2 -> skip multipath route.
2034 * -1 -> delete this route.
2035 * 0 -> continue walking
2038 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2039 int (*func)(struct fib6_info *, void *arg),
2040 int sernum, void *arg, bool skip_notify)
2042 struct fib6_cleaner c;
2045 c.w.func = fib6_clean_node;
2052 c.skip_notify = skip_notify;
2054 fib6_walk(net, &c.w);
2057 static void __fib6_clean_all(struct net *net,
2058 int (*func)(struct fib6_info *, void *),
2059 int sernum, void *arg, bool skip_notify)
2061 struct fib6_table *table;
2062 struct hlist_head *head;
2066 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2067 head = &net->ipv6.fib_table_hash[h];
2068 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2069 spin_lock_bh(&table->tb6_lock);
2070 fib6_clean_tree(net, &table->tb6_root,
2071 func, sernum, arg, skip_notify);
2072 spin_unlock_bh(&table->tb6_lock);
2078 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
2081 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2084 void fib6_clean_all_skip_notify(struct net *net,
2085 int (*func)(struct fib6_info *, void *),
2088 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
2091 static void fib6_flush_trees(struct net *net)
2093 int new_sernum = fib6_new_sernum(net);
2095 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
2099 * Garbage collection
2102 static int fib6_age(struct fib6_info *rt, void *arg)
2104 struct fib6_gc_args *gc_args = arg;
2105 unsigned long now = jiffies;
2108 * check addrconf expiration here.
2109 * Routes are expired even if they are in use.
2112 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
2113 if (time_after(now, rt->expires)) {
2114 RT6_TRACE("expiring %p\n", rt);
2120 /* Also age clones in the exception table.
2121 * Note, that clones are aged out
2122 * only if they are not in use now.
2124 rt6_age_exceptions(rt, gc_args, now);
2129 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2131 struct fib6_gc_args gc_args;
2135 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2136 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2137 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2140 gc_args.timeout = expires ? (int)expires :
2141 net->ipv6.sysctl.ip6_rt_gc_interval;
2144 fib6_clean_all(net, fib6_age, &gc_args);
2146 net->ipv6.ip6_rt_last_gc = now;
2149 mod_timer(&net->ipv6.ip6_fib_timer,
2151 + net->ipv6.sysctl.ip6_rt_gc_interval));
2153 del_timer(&net->ipv6.ip6_fib_timer);
2154 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2157 static void fib6_gc_timer_cb(struct timer_list *t)
2159 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2161 fib6_run_gc(0, arg, true);
2164 static int __net_init fib6_net_init(struct net *net)
2166 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2169 err = fib6_notifier_init(net);
2173 spin_lock_init(&net->ipv6.fib6_gc_lock);
2174 rwlock_init(&net->ipv6.fib6_walker_lock);
2175 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2176 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2178 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2179 if (!net->ipv6.rt6_stats)
2182 /* Avoid false sharing : Use at least a full cache line */
2183 size = max_t(size_t, size, L1_CACHE_BYTES);
2185 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2186 if (!net->ipv6.fib_table_hash)
2189 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2191 if (!net->ipv6.fib6_main_tbl)
2192 goto out_fib_table_hash;
2194 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2195 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2196 net->ipv6.fib6_null_entry);
2197 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2198 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2199 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2201 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2202 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2204 if (!net->ipv6.fib6_local_tbl)
2205 goto out_fib6_main_tbl;
2206 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2207 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2208 net->ipv6.fib6_null_entry);
2209 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2210 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2211 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2213 fib6_tables_init(net);
2217 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2219 kfree(net->ipv6.fib6_main_tbl);
2222 kfree(net->ipv6.fib_table_hash);
2224 kfree(net->ipv6.rt6_stats);
2226 fib6_notifier_exit(net);
2230 static void fib6_net_exit(struct net *net)
2234 del_timer_sync(&net->ipv6.ip6_fib_timer);
2236 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2237 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2238 struct hlist_node *tmp;
2239 struct fib6_table *tb;
2241 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2242 hlist_del(&tb->tb6_hlist);
2243 fib6_free_table(tb);
2247 kfree(net->ipv6.fib_table_hash);
2248 kfree(net->ipv6.rt6_stats);
2249 fib6_notifier_exit(net);
2252 static struct pernet_operations fib6_net_ops = {
2253 .init = fib6_net_init,
2254 .exit = fib6_net_exit,
2257 int __init fib6_init(void)
2261 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2262 sizeof(struct fib6_node),
2263 0, SLAB_HWCACHE_ALIGN,
2265 if (!fib6_node_kmem)
2268 ret = register_pernet_subsys(&fib6_net_ops);
2270 goto out_kmem_cache_create;
2272 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2275 goto out_unregister_subsys;
2277 __fib6_flush_trees = fib6_flush_trees;
2281 out_unregister_subsys:
2282 unregister_pernet_subsys(&fib6_net_ops);
2283 out_kmem_cache_create:
2284 kmem_cache_destroy(fib6_node_kmem);
2288 void fib6_gc_cleanup(void)
2290 unregister_pernet_subsys(&fib6_net_ops);
2291 kmem_cache_destroy(fib6_node_kmem);
2294 #ifdef CONFIG_PROC_FS
2295 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2297 struct fib6_info *rt = v;
2298 struct ipv6_route_iter *iter = seq->private;
2299 unsigned int flags = rt->fib6_flags;
2300 const struct net_device *dev;
2302 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
2304 #ifdef CONFIG_IPV6_SUBTREES
2305 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
2307 seq_puts(seq, "00000000000000000000000000000000 00 ");
2309 if (rt->fib6_nh.fib_nh_gw_family) {
2310 flags |= RTF_GATEWAY;
2311 seq_printf(seq, "%pi6", &rt->fib6_nh.fib_nh_gw6);
2313 seq_puts(seq, "00000000000000000000000000000000");
2316 dev = rt->fib6_nh.fib_nh_dev;
2317 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2318 rt->fib6_metric, refcount_read(&rt->fib6_ref), 0,
2319 flags, dev ? dev->name : "");
2320 iter->w.leaf = NULL;
2324 static int ipv6_route_yield(struct fib6_walker *w)
2326 struct ipv6_route_iter *iter = w->args;
2332 iter->w.leaf = rcu_dereference_protected(
2333 iter->w.leaf->fib6_next,
2334 lockdep_is_held(&iter->tbl->tb6_lock));
2336 if (!iter->skip && iter->w.leaf)
2338 } while (iter->w.leaf);
2343 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2346 memset(&iter->w, 0, sizeof(iter->w));
2347 iter->w.func = ipv6_route_yield;
2348 iter->w.root = &iter->tbl->tb6_root;
2349 iter->w.state = FWS_INIT;
2350 iter->w.node = iter->w.root;
2351 iter->w.args = iter;
2352 iter->sernum = iter->w.root->fn_sernum;
2353 INIT_LIST_HEAD(&iter->w.lh);
2354 fib6_walker_link(net, &iter->w);
2357 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2361 struct hlist_node *node;
2364 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2365 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2371 while (!node && h < FIB6_TABLE_HASHSZ) {
2372 node = rcu_dereference_bh(
2373 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2375 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2378 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2380 if (iter->sernum != iter->w.root->fn_sernum) {
2381 iter->sernum = iter->w.root->fn_sernum;
2382 iter->w.state = FWS_INIT;
2383 iter->w.node = iter->w.root;
2384 WARN_ON(iter->w.skip);
2385 iter->w.skip = iter->w.count;
2389 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2392 struct fib6_info *n;
2393 struct net *net = seq_file_net(seq);
2394 struct ipv6_route_iter *iter = seq->private;
2399 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
2406 ipv6_route_check_sernum(iter);
2407 spin_lock_bh(&iter->tbl->tb6_lock);
2408 r = fib6_walk_continue(&iter->w);
2409 spin_unlock_bh(&iter->tbl->tb6_lock);
2413 return iter->w.leaf;
2415 fib6_walker_unlink(net, &iter->w);
2418 fib6_walker_unlink(net, &iter->w);
2420 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2424 ipv6_route_seq_setup_walk(iter, net);
2428 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2431 struct net *net = seq_file_net(seq);
2432 struct ipv6_route_iter *iter = seq->private;
2435 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2439 ipv6_route_seq_setup_walk(iter, net);
2440 return ipv6_route_seq_next(seq, NULL, pos);
2446 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2448 struct fib6_walker *w = &iter->w;
2449 return w->node && !(w->state == FWS_U && w->node == w->root);
2452 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2455 struct net *net = seq_file_net(seq);
2456 struct ipv6_route_iter *iter = seq->private;
2458 if (ipv6_route_iter_active(iter))
2459 fib6_walker_unlink(net, &iter->w);
2461 rcu_read_unlock_bh();
2464 const struct seq_operations ipv6_route_seq_ops = {
2465 .start = ipv6_route_seq_start,
2466 .next = ipv6_route_seq_next,
2467 .stop = ipv6_route_seq_stop,
2468 .show = ipv6_route_seq_show
2470 #endif /* CONFIG_PROC_FS */