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);
168 WARN_ON(f6i->fib6_node);
170 fib6_nh_release(&f6i->fib6_nh);
171 ip_fib_metrics_put(f6i->fib6_metrics);
174 EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
176 static struct fib6_node *node_alloc(struct net *net)
178 struct fib6_node *fn;
180 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
182 net->ipv6.rt6_stats->fib_nodes++;
187 static void node_free_immediate(struct net *net, struct fib6_node *fn)
189 kmem_cache_free(fib6_node_kmem, fn);
190 net->ipv6.rt6_stats->fib_nodes--;
193 static void node_free_rcu(struct rcu_head *head)
195 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
197 kmem_cache_free(fib6_node_kmem, fn);
200 static void node_free(struct net *net, struct fib6_node *fn)
202 call_rcu(&fn->rcu, node_free_rcu);
203 net->ipv6.rt6_stats->fib_nodes--;
206 static void fib6_free_table(struct fib6_table *table)
208 inetpeer_invalidate_tree(&table->tb6_peers);
212 static void fib6_link_table(struct net *net, struct fib6_table *tb)
217 * Initialize table lock at a single place to give lockdep a key,
218 * tables aren't visible prior to being linked to the list.
220 spin_lock_init(&tb->tb6_lock);
221 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
224 * No protection necessary, this is the only list mutatation
225 * operation, tables never disappear once they exist.
227 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
230 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
232 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
234 struct fib6_table *table;
236 table = kzalloc(sizeof(*table), GFP_ATOMIC);
239 rcu_assign_pointer(table->tb6_root.leaf,
240 net->ipv6.fib6_null_entry);
241 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
242 inet_peer_base_init(&table->tb6_peers);
248 struct fib6_table *fib6_new_table(struct net *net, u32 id)
250 struct fib6_table *tb;
254 tb = fib6_get_table(net, id);
258 tb = fib6_alloc_table(net, id);
260 fib6_link_table(net, tb);
264 EXPORT_SYMBOL_GPL(fib6_new_table);
266 struct fib6_table *fib6_get_table(struct net *net, u32 id)
268 struct fib6_table *tb;
269 struct hlist_head *head;
274 h = id & (FIB6_TABLE_HASHSZ - 1);
276 head = &net->ipv6.fib_table_hash[h];
277 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
278 if (tb->tb6_id == id) {
287 EXPORT_SYMBOL_GPL(fib6_get_table);
289 static void __net_init fib6_tables_init(struct net *net)
291 fib6_link_table(net, net->ipv6.fib6_main_tbl);
292 fib6_link_table(net, net->ipv6.fib6_local_tbl);
296 struct fib6_table *fib6_new_table(struct net *net, u32 id)
298 return fib6_get_table(net, id);
301 struct fib6_table *fib6_get_table(struct net *net, u32 id)
303 return net->ipv6.fib6_main_tbl;
306 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
307 const struct sk_buff *skb,
308 int flags, pol_lookup_t lookup)
312 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
313 if (rt->dst.error == -EAGAIN) {
315 rt = net->ipv6.ip6_null_entry;
322 /* called with rcu lock held; no reference taken on fib6_info */
323 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
324 struct fib6_result *res, int flags)
326 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6,
330 static void __net_init fib6_tables_init(struct net *net)
332 fib6_link_table(net, net->ipv6.fib6_main_tbl);
337 unsigned int fib6_tables_seq_read(struct net *net)
339 unsigned int h, fib_seq = 0;
342 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
343 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
344 struct fib6_table *tb;
346 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
347 fib_seq += tb->fib_seq;
354 static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
355 enum fib_event_type event_type,
356 struct fib6_info *rt)
358 struct fib6_entry_notifier_info info = {
362 return call_fib6_notifier(nb, net, event_type, &info.info);
365 int call_fib6_entry_notifiers(struct net *net,
366 enum fib_event_type event_type,
367 struct fib6_info *rt,
368 struct netlink_ext_ack *extack)
370 struct fib6_entry_notifier_info info = {
371 .info.extack = extack,
375 rt->fib6_table->fib_seq++;
376 return call_fib6_notifiers(net, event_type, &info.info);
379 struct fib6_dump_arg {
381 struct notifier_block *nb;
384 static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
386 if (rt == arg->net->ipv6.fib6_null_entry)
388 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
391 static int fib6_node_dump(struct fib6_walker *w)
393 struct fib6_info *rt;
395 for_each_fib6_walker_rt(w)
396 fib6_rt_dump(rt, w->args);
401 static void fib6_table_dump(struct net *net, struct fib6_table *tb,
402 struct fib6_walker *w)
404 w->root = &tb->tb6_root;
405 spin_lock_bh(&tb->tb6_lock);
407 spin_unlock_bh(&tb->tb6_lock);
410 /* Called with rcu_read_lock() */
411 int fib6_tables_dump(struct net *net, struct notifier_block *nb)
413 struct fib6_dump_arg arg;
414 struct fib6_walker *w;
417 w = kzalloc(sizeof(*w), GFP_ATOMIC);
421 w->func = fib6_node_dump;
426 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
427 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
428 struct fib6_table *tb;
430 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
431 fib6_table_dump(net, tb, w);
439 static int fib6_dump_node(struct fib6_walker *w)
442 struct fib6_info *rt;
444 for_each_fib6_walker_rt(w) {
445 res = rt6_dump_route(rt, w->args);
447 /* Frame is full, suspend walking */
452 /* Multipath routes are dumped in one route with the
453 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
454 * last sibling of this route (no need to dump the
455 * sibling routes again)
457 if (rt->fib6_nsiblings)
458 rt = list_last_entry(&rt->fib6_siblings,
466 static void fib6_dump_end(struct netlink_callback *cb)
468 struct net *net = sock_net(cb->skb->sk);
469 struct fib6_walker *w = (void *)cb->args[2];
474 fib6_walker_unlink(net, w);
479 cb->done = (void *)cb->args[3];
483 static int fib6_dump_done(struct netlink_callback *cb)
486 return cb->done ? cb->done(cb) : 0;
489 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
490 struct netlink_callback *cb)
492 struct net *net = sock_net(skb->sk);
493 struct fib6_walker *w;
496 w = (void *)cb->args[2];
497 w->root = &table->tb6_root;
499 if (cb->args[4] == 0) {
503 spin_lock_bh(&table->tb6_lock);
504 res = fib6_walk(net, w);
505 spin_unlock_bh(&table->tb6_lock);
508 cb->args[5] = w->root->fn_sernum;
511 if (cb->args[5] != w->root->fn_sernum) {
512 /* Begin at the root if the tree changed */
513 cb->args[5] = w->root->fn_sernum;
520 spin_lock_bh(&table->tb6_lock);
521 res = fib6_walk_continue(w);
522 spin_unlock_bh(&table->tb6_lock);
524 fib6_walker_unlink(net, w);
532 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
534 const struct nlmsghdr *nlh = cb->nlh;
535 struct net *net = sock_net(skb->sk);
536 struct rt6_rtnl_dump_arg arg = {};
538 unsigned int e = 0, s_e;
539 struct fib6_walker *w;
540 struct fib6_table *tb;
541 struct hlist_head *head;
544 if (cb->strict_check) {
547 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
550 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
551 struct rtmsg *rtm = nlmsg_data(nlh);
553 arg.filter.flags = rtm->rtm_flags & (RTM_F_PREFIX|RTM_F_CLONED);
556 /* fib entries are never clones */
557 if (arg.filter.flags & RTM_F_CLONED)
560 w = (void *)cb->args[2];
564 * 1. hook callback destructor.
566 cb->args[3] = (long)cb->done;
567 cb->done = fib6_dump_done;
570 * 2. allocate and initialize walker.
572 w = kzalloc(sizeof(*w), GFP_ATOMIC);
575 w->func = fib6_dump_node;
576 cb->args[2] = (long)w;
584 if (arg.filter.table_id) {
585 tb = fib6_get_table(net, arg.filter.table_id);
587 if (arg.filter.dump_all_families)
590 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
595 res = fib6_dump_table(tb, skb, cb);
606 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
608 head = &net->ipv6.fib_table_hash[h];
609 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
612 res = fib6_dump_table(tb, skb, cb);
624 res = res < 0 ? res : skb->len;
630 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
635 if (f6i->fib6_metrics == &dst_default_metrics) {
636 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
641 refcount_set(&p->refcnt, 1);
642 f6i->fib6_metrics = p;
645 f6i->fib6_metrics->metrics[metric - 1] = val;
651 * return the appropriate node for a routing tree "add" operation
652 * by either creating and inserting or by returning an existing
656 static struct fib6_node *fib6_add_1(struct net *net,
657 struct fib6_table *table,
658 struct fib6_node *root,
659 struct in6_addr *addr, int plen,
660 int offset, int allow_create,
661 int replace_required,
662 struct netlink_ext_ack *extack)
664 struct fib6_node *fn, *in, *ln;
665 struct fib6_node *pn = NULL;
670 RT6_TRACE("fib6_add_1\n");
672 /* insert node in tree */
677 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
678 lockdep_is_held(&table->tb6_lock));
679 key = (struct rt6key *)((u8 *)leaf + offset);
684 if (plen < fn->fn_bit ||
685 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
687 if (replace_required) {
688 NL_SET_ERR_MSG(extack,
689 "Can not replace route - no match found");
690 pr_warn("Can't replace route, no match found\n");
691 return ERR_PTR(-ENOENT);
693 pr_warn("NLM_F_CREATE should be set when creating new route\n");
702 if (plen == fn->fn_bit) {
703 /* clean up an intermediate node */
704 if (!(fn->fn_flags & RTN_RTINFO)) {
705 RCU_INIT_POINTER(fn->leaf, NULL);
706 fib6_info_release(leaf);
707 /* remove null_entry in the root node */
708 } else if (fn->fn_flags & RTN_TL_ROOT &&
709 rcu_access_pointer(fn->leaf) ==
710 net->ipv6.fib6_null_entry) {
711 RCU_INIT_POINTER(fn->leaf, NULL);
718 * We have more bits to go
721 /* Try to walk down on tree. */
722 dir = addr_bit_set(addr, fn->fn_bit);
725 rcu_dereference_protected(fn->right,
726 lockdep_is_held(&table->tb6_lock)) :
727 rcu_dereference_protected(fn->left,
728 lockdep_is_held(&table->tb6_lock));
732 /* We should not create new node because
733 * NLM_F_REPLACE was specified without NLM_F_CREATE
734 * I assume it is safe to require NLM_F_CREATE when
735 * REPLACE flag is used! Later we may want to remove the
736 * check for replace_required, because according
737 * to netlink specification, NLM_F_CREATE
738 * MUST be specified if new route is created.
739 * That would keep IPv6 consistent with IPv4
741 if (replace_required) {
742 NL_SET_ERR_MSG(extack,
743 "Can not replace route - no match found");
744 pr_warn("Can't replace route, no match found\n");
745 return ERR_PTR(-ENOENT);
747 pr_warn("NLM_F_CREATE should be set when creating new route\n");
750 * We walked to the bottom of tree.
751 * Create new leaf node without children.
754 ln = node_alloc(net);
757 return ERR_PTR(-ENOMEM);
759 RCU_INIT_POINTER(ln->parent, pn);
762 rcu_assign_pointer(pn->right, ln);
764 rcu_assign_pointer(pn->left, ln);
771 * split since we don't have a common prefix anymore or
772 * we have a less significant route.
773 * we've to insert an intermediate node on the list
774 * this new node will point to the one we need to create
778 pn = rcu_dereference_protected(fn->parent,
779 lockdep_is_held(&table->tb6_lock));
781 /* find 1st bit in difference between the 2 addrs.
783 See comment in __ipv6_addr_diff: bit may be an invalid value,
784 but if it is >= plen, the value is ignored in any case.
787 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
792 * (new leaf node)[ln] (old node)[fn]
795 in = node_alloc(net);
796 ln = node_alloc(net);
800 node_free_immediate(net, in);
802 node_free_immediate(net, ln);
803 return ERR_PTR(-ENOMEM);
807 * new intermediate node.
809 * be off since that an address that chooses one of
810 * the branches would not match less specific routes
811 * in the other branch
816 RCU_INIT_POINTER(in->parent, pn);
818 fib6_info_hold(rcu_dereference_protected(in->leaf,
819 lockdep_is_held(&table->tb6_lock)));
821 /* update parent pointer */
823 rcu_assign_pointer(pn->right, in);
825 rcu_assign_pointer(pn->left, in);
829 RCU_INIT_POINTER(ln->parent, in);
830 rcu_assign_pointer(fn->parent, in);
832 if (addr_bit_set(addr, bit)) {
833 rcu_assign_pointer(in->right, ln);
834 rcu_assign_pointer(in->left, fn);
836 rcu_assign_pointer(in->left, ln);
837 rcu_assign_pointer(in->right, fn);
839 } else { /* plen <= bit */
842 * (new leaf node)[ln]
844 * (old node)[fn] NULL
847 ln = node_alloc(net);
850 return ERR_PTR(-ENOMEM);
854 RCU_INIT_POINTER(ln->parent, pn);
856 if (addr_bit_set(&key->addr, plen))
857 RCU_INIT_POINTER(ln->right, fn);
859 RCU_INIT_POINTER(ln->left, fn);
861 rcu_assign_pointer(fn->parent, ln);
864 rcu_assign_pointer(pn->right, ln);
866 rcu_assign_pointer(pn->left, ln);
871 static void __fib6_drop_pcpu_from(struct fib6_nh *fib6_nh,
872 const struct fib6_info *match,
873 const struct fib6_table *table)
877 if (!fib6_nh->rt6i_pcpu)
880 /* release the reference to this fib entry from
881 * all of its cached pcpu routes
883 for_each_possible_cpu(cpu) {
884 struct rt6_info **ppcpu_rt;
885 struct rt6_info *pcpu_rt;
887 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
890 /* only dropping the 'from' reference if the cached route
891 * is using 'match'. The cached pcpu_rt->from only changes
892 * from a fib6_info to NULL (ip6_dst_destroy); it can never
893 * change from one fib6_info reference to another
895 if (pcpu_rt && rcu_access_pointer(pcpu_rt->from) == match) {
896 struct fib6_info *from;
898 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
899 fib6_info_release(from);
904 static void fib6_drop_pcpu_from(struct fib6_info *f6i,
905 const struct fib6_table *table)
907 struct fib6_nh *fib6_nh;
909 /* Make sure rt6_make_pcpu_route() wont add other percpu routes
910 * while we are cleaning them here.
912 f6i->fib6_destroying = 1;
913 mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
915 fib6_nh = &f6i->fib6_nh;
916 __fib6_drop_pcpu_from(fib6_nh, f6i, table);
919 static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
922 struct fib6_table *table = rt->fib6_table;
924 fib6_drop_pcpu_from(rt, table);
926 if (refcount_read(&rt->fib6_ref) != 1) {
927 /* This route is used as dummy address holder in some split
928 * nodes. It is not leaked, but it still holds other resources,
929 * which must be released in time. So, scan ascendant nodes
930 * and replace dummy references to this route with references
931 * to still alive ones.
934 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
935 lockdep_is_held(&table->tb6_lock));
936 struct fib6_info *new_leaf;
937 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
938 new_leaf = fib6_find_prefix(net, table, fn);
939 fib6_info_hold(new_leaf);
941 rcu_assign_pointer(fn->leaf, new_leaf);
942 fib6_info_release(rt);
944 fn = rcu_dereference_protected(fn->parent,
945 lockdep_is_held(&table->tb6_lock));
951 * Insert routing information in a node.
954 static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
955 struct nl_info *info,
956 struct netlink_ext_ack *extack)
958 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
959 lockdep_is_held(&rt->fib6_table->tb6_lock));
960 struct fib6_info *iter = NULL;
961 struct fib6_info __rcu **ins;
962 struct fib6_info __rcu **fallback_ins = NULL;
963 int replace = (info->nlh &&
964 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
965 int add = (!info->nlh ||
966 (info->nlh->nlmsg_flags & NLM_F_CREATE));
968 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
969 u16 nlflags = NLM_F_EXCL;
972 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
973 nlflags |= NLM_F_APPEND;
977 for (iter = leaf; iter;
978 iter = rcu_dereference_protected(iter->fib6_next,
979 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
981 * Search for duplicates
984 if (iter->fib6_metric == rt->fib6_metric) {
986 * Same priority level
989 (info->nlh->nlmsg_flags & NLM_F_EXCL))
992 nlflags &= ~NLM_F_EXCL;
994 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
999 fallback_ins = fallback_ins ?: ins;
1003 if (rt6_duplicate_nexthop(iter, rt)) {
1004 if (rt->fib6_nsiblings)
1005 rt->fib6_nsiblings = 0;
1006 if (!(iter->fib6_flags & RTF_EXPIRES))
1008 if (!(rt->fib6_flags & RTF_EXPIRES))
1009 fib6_clean_expires(iter);
1011 fib6_set_expires(iter, rt->expires);
1014 fib6_metric_set(iter, RTAX_MTU,
1018 /* If we have the same destination and the same metric,
1019 * but not the same gateway, then the route we try to
1020 * add is sibling to this route, increment our counter
1021 * of siblings, and later we will add our route to the
1023 * Only static routes (which don't have flag
1024 * RTF_EXPIRES) are used for ECMPv6.
1026 * To avoid long list, we only had siblings if the
1027 * route have a gateway.
1030 rt6_qualify_for_ecmp(iter))
1031 rt->fib6_nsiblings++;
1034 if (iter->fib6_metric > rt->fib6_metric)
1038 ins = &iter->fib6_next;
1041 if (fallback_ins && !found) {
1042 /* No ECMP-able route found, replace first non-ECMP one */
1044 iter = rcu_dereference_protected(*ins,
1045 lockdep_is_held(&rt->fib6_table->tb6_lock));
1049 /* Reset round-robin state, if necessary */
1050 if (ins == &fn->leaf)
1053 /* Link this route to others same route. */
1054 if (rt->fib6_nsiblings) {
1055 unsigned int fib6_nsiblings;
1056 struct fib6_info *sibling, *temp_sibling;
1058 /* Find the first route that have the same metric */
1061 if (sibling->fib6_metric == rt->fib6_metric &&
1062 rt6_qualify_for_ecmp(sibling)) {
1063 list_add_tail(&rt->fib6_siblings,
1064 &sibling->fib6_siblings);
1067 sibling = rcu_dereference_protected(sibling->fib6_next,
1068 lockdep_is_held(&rt->fib6_table->tb6_lock));
1070 /* For each sibling in the list, increment the counter of
1071 * siblings. BUG() if counters does not match, list of siblings
1075 list_for_each_entry_safe(sibling, temp_sibling,
1076 &rt->fib6_siblings, fib6_siblings) {
1077 sibling->fib6_nsiblings++;
1078 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1081 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1082 rt6_multipath_rebalance(temp_sibling);
1090 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1093 nlflags |= NLM_F_CREATE;
1095 err = call_fib6_entry_notifiers(info->nl_net,
1096 FIB_EVENT_ENTRY_ADD,
1101 rcu_assign_pointer(rt->fib6_next, iter);
1103 rcu_assign_pointer(rt->fib6_node, fn);
1104 rcu_assign_pointer(*ins, rt);
1105 if (!info->skip_notify)
1106 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1107 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1109 if (!(fn->fn_flags & RTN_RTINFO)) {
1110 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1111 fn->fn_flags |= RTN_RTINFO;
1120 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1124 err = call_fib6_entry_notifiers(info->nl_net,
1125 FIB_EVENT_ENTRY_REPLACE,
1131 rcu_assign_pointer(rt->fib6_node, fn);
1132 rt->fib6_next = iter->fib6_next;
1133 rcu_assign_pointer(*ins, rt);
1134 if (!info->skip_notify)
1135 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1136 if (!(fn->fn_flags & RTN_RTINFO)) {
1137 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1138 fn->fn_flags |= RTN_RTINFO;
1140 nsiblings = iter->fib6_nsiblings;
1141 iter->fib6_node = NULL;
1142 fib6_purge_rt(iter, fn, info->nl_net);
1143 if (rcu_access_pointer(fn->rr_ptr) == iter)
1145 fib6_info_release(iter);
1148 /* Replacing an ECMP route, remove all siblings */
1149 ins = &rt->fib6_next;
1150 iter = rcu_dereference_protected(*ins,
1151 lockdep_is_held(&rt->fib6_table->tb6_lock));
1153 if (iter->fib6_metric > rt->fib6_metric)
1155 if (rt6_qualify_for_ecmp(iter)) {
1156 *ins = iter->fib6_next;
1157 iter->fib6_node = NULL;
1158 fib6_purge_rt(iter, fn, info->nl_net);
1159 if (rcu_access_pointer(fn->rr_ptr) == iter)
1161 fib6_info_release(iter);
1163 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1165 ins = &iter->fib6_next;
1167 iter = rcu_dereference_protected(*ins,
1168 lockdep_is_held(&rt->fib6_table->tb6_lock));
1170 WARN_ON(nsiblings != 0);
1177 static void fib6_start_gc(struct net *net, struct fib6_info *rt)
1179 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1180 (rt->fib6_flags & RTF_EXPIRES))
1181 mod_timer(&net->ipv6.ip6_fib_timer,
1182 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1185 void fib6_force_start_gc(struct net *net)
1187 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1188 mod_timer(&net->ipv6.ip6_fib_timer,
1189 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1192 static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
1195 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1196 lockdep_is_held(&rt->fib6_table->tb6_lock));
1198 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1201 fn->fn_sernum = sernum;
1202 fn = rcu_dereference_protected(fn->parent,
1203 lockdep_is_held(&rt->fib6_table->tb6_lock));
1207 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
1209 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1212 /* allow ipv4 to update sernum via ipv6_stub */
1213 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i)
1215 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1216 fib6_update_sernum_upto_root(net, f6i);
1217 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1221 * Add routing information to the routing tree.
1222 * <destination addr>/<source addr>
1223 * with source addr info in sub-trees
1224 * Need to own table->tb6_lock
1227 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
1228 struct nl_info *info, struct netlink_ext_ack *extack)
1230 struct fib6_table *table = rt->fib6_table;
1231 struct fib6_node *fn, *pn = NULL;
1233 int allow_create = 1;
1234 int replace_required = 0;
1235 int sernum = fib6_new_sernum(info->nl_net);
1238 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1240 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1241 replace_required = 1;
1243 if (!allow_create && !replace_required)
1244 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1246 fn = fib6_add_1(info->nl_net, table, root,
1247 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1248 offsetof(struct fib6_info, fib6_dst), allow_create,
1249 replace_required, extack);
1258 #ifdef CONFIG_IPV6_SUBTREES
1259 if (rt->fib6_src.plen) {
1260 struct fib6_node *sn;
1262 if (!rcu_access_pointer(fn->subtree)) {
1263 struct fib6_node *sfn;
1275 /* Create subtree root node */
1276 sfn = node_alloc(info->nl_net);
1280 fib6_info_hold(info->nl_net->ipv6.fib6_null_entry);
1281 rcu_assign_pointer(sfn->leaf,
1282 info->nl_net->ipv6.fib6_null_entry);
1283 sfn->fn_flags = RTN_ROOT;
1285 /* Now add the first leaf node to new subtree */
1287 sn = fib6_add_1(info->nl_net, table, sfn,
1288 &rt->fib6_src.addr, rt->fib6_src.plen,
1289 offsetof(struct fib6_info, fib6_src),
1290 allow_create, replace_required, extack);
1293 /* If it is failed, discard just allocated
1294 root, and then (in failure) stale node
1297 node_free_immediate(info->nl_net, sfn);
1302 /* Now link new subtree to main tree */
1303 rcu_assign_pointer(sfn->parent, fn);
1304 rcu_assign_pointer(fn->subtree, sfn);
1306 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1307 &rt->fib6_src.addr, rt->fib6_src.plen,
1308 offsetof(struct fib6_info, fib6_src),
1309 allow_create, replace_required, extack);
1317 if (!rcu_access_pointer(fn->leaf)) {
1318 if (fn->fn_flags & RTN_TL_ROOT) {
1319 /* put back null_entry for root node */
1320 rcu_assign_pointer(fn->leaf,
1321 info->nl_net->ipv6.fib6_null_entry);
1324 rcu_assign_pointer(fn->leaf, rt);
1331 err = fib6_add_rt2node(fn, rt, info, extack);
1333 __fib6_update_sernum_upto_root(rt, sernum);
1334 fib6_start_gc(info->nl_net, rt);
1339 #ifdef CONFIG_IPV6_SUBTREES
1341 * If fib6_add_1 has cleared the old leaf pointer in the
1342 * super-tree leaf node we have to find a new one for it.
1345 struct fib6_info *pn_leaf =
1346 rcu_dereference_protected(pn->leaf,
1347 lockdep_is_held(&table->tb6_lock));
1348 if (pn_leaf == rt) {
1350 RCU_INIT_POINTER(pn->leaf, NULL);
1351 fib6_info_release(rt);
1353 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1354 pn_leaf = fib6_find_prefix(info->nl_net, table,
1360 info->nl_net->ipv6.fib6_null_entry;
1363 fib6_info_hold(pn_leaf);
1364 rcu_assign_pointer(pn->leaf, pn_leaf);
1373 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1374 * 1. fn is an intermediate node and we failed to add the new
1375 * route to it in both subtree creation failure and fib6_add_rt2node()
1377 * 2. fn is the root node in the table and we fail to add the first
1378 * default route to it.
1381 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1382 (fn->fn_flags & RTN_TL_ROOT &&
1383 !rcu_access_pointer(fn->leaf))))
1384 fib6_repair_tree(info->nl_net, table, fn);
1389 * Routing tree lookup
1393 struct lookup_args {
1394 int offset; /* key offset on fib6_info */
1395 const struct in6_addr *addr; /* search key */
1398 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1399 struct lookup_args *args)
1401 struct fib6_node *fn;
1404 if (unlikely(args->offset == 0))
1414 struct fib6_node *next;
1416 dir = addr_bit_set(args->addr, fn->fn_bit);
1418 next = dir ? rcu_dereference(fn->right) :
1419 rcu_dereference(fn->left);
1429 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1431 if (subtree || fn->fn_flags & RTN_RTINFO) {
1432 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1438 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1440 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1441 #ifdef CONFIG_IPV6_SUBTREES
1443 struct fib6_node *sfn;
1444 sfn = fib6_node_lookup_1(subtree,
1451 if (fn->fn_flags & RTN_RTINFO)
1456 if (fn->fn_flags & RTN_ROOT)
1459 fn = rcu_dereference(fn->parent);
1465 /* called with rcu_read_lock() held
1467 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1468 const struct in6_addr *daddr,
1469 const struct in6_addr *saddr)
1471 struct fib6_node *fn;
1472 struct lookup_args args[] = {
1474 .offset = offsetof(struct fib6_info, fib6_dst),
1477 #ifdef CONFIG_IPV6_SUBTREES
1479 .offset = offsetof(struct fib6_info, fib6_src),
1484 .offset = 0, /* sentinel */
1488 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
1489 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1496 * Get node with specified destination prefix (and source prefix,
1497 * if subtrees are used)
1498 * exact_match == true means we try to find fn with exact match of
1499 * the passed in prefix addr
1500 * exact_match == false means we try to find fn with longest prefix
1501 * match of the passed in prefix addr. This is useful for finding fn
1502 * for cached route as it will be stored in the exception table under
1503 * the node with longest prefix length.
1507 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1508 const struct in6_addr *addr,
1509 int plen, int offset,
1512 struct fib6_node *fn, *prev = NULL;
1514 for (fn = root; fn ; ) {
1515 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1518 /* This node is being deleted */
1520 if (plen <= fn->fn_bit)
1526 key = (struct rt6key *)((u8 *)leaf + offset);
1531 if (plen < fn->fn_bit ||
1532 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1535 if (plen == fn->fn_bit)
1542 * We have more bits to go
1544 if (addr_bit_set(addr, fn->fn_bit))
1545 fn = rcu_dereference(fn->right);
1547 fn = rcu_dereference(fn->left);
1556 struct fib6_node *fib6_locate(struct fib6_node *root,
1557 const struct in6_addr *daddr, int dst_len,
1558 const struct in6_addr *saddr, int src_len,
1561 struct fib6_node *fn;
1563 fn = fib6_locate_1(root, daddr, dst_len,
1564 offsetof(struct fib6_info, fib6_dst),
1567 #ifdef CONFIG_IPV6_SUBTREES
1569 WARN_ON(saddr == NULL);
1571 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1574 fn = fib6_locate_1(subtree, saddr, src_len,
1575 offsetof(struct fib6_info, fib6_src),
1582 if (fn && fn->fn_flags & RTN_RTINFO)
1594 static struct fib6_info *fib6_find_prefix(struct net *net,
1595 struct fib6_table *table,
1596 struct fib6_node *fn)
1598 struct fib6_node *child_left, *child_right;
1600 if (fn->fn_flags & RTN_ROOT)
1601 return net->ipv6.fib6_null_entry;
1604 child_left = rcu_dereference_protected(fn->left,
1605 lockdep_is_held(&table->tb6_lock));
1606 child_right = rcu_dereference_protected(fn->right,
1607 lockdep_is_held(&table->tb6_lock));
1609 return rcu_dereference_protected(child_left->leaf,
1610 lockdep_is_held(&table->tb6_lock));
1612 return rcu_dereference_protected(child_right->leaf,
1613 lockdep_is_held(&table->tb6_lock));
1615 fn = FIB6_SUBTREE(fn);
1621 * Called to trim the tree of intermediate nodes when possible. "fn"
1622 * is the node we want to try and remove.
1623 * Need to own table->tb6_lock
1626 static struct fib6_node *fib6_repair_tree(struct net *net,
1627 struct fib6_table *table,
1628 struct fib6_node *fn)
1632 struct fib6_node *child;
1633 struct fib6_walker *w;
1636 /* Set fn->leaf to null_entry for root node. */
1637 if (fn->fn_flags & RTN_TL_ROOT) {
1638 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1643 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1644 lockdep_is_held(&table->tb6_lock));
1645 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1646 lockdep_is_held(&table->tb6_lock));
1647 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1648 lockdep_is_held(&table->tb6_lock));
1649 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1650 lockdep_is_held(&table->tb6_lock));
1651 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1652 lockdep_is_held(&table->tb6_lock));
1653 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1654 lockdep_is_held(&table->tb6_lock));
1655 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1656 lockdep_is_held(&table->tb6_lock));
1657 struct fib6_info *new_fn_leaf;
1659 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1662 WARN_ON(fn->fn_flags & RTN_RTINFO);
1663 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1669 child = fn_r, children |= 1;
1671 child = fn_l, children |= 2;
1673 if (children == 3 || FIB6_SUBTREE(fn)
1674 #ifdef CONFIG_IPV6_SUBTREES
1675 /* Subtree root (i.e. fn) may have one child */
1676 || (children && fn->fn_flags & RTN_ROOT)
1679 new_fn_leaf = fib6_find_prefix(net, table, fn);
1682 WARN_ON(!new_fn_leaf);
1683 new_fn_leaf = net->ipv6.fib6_null_entry;
1686 fib6_info_hold(new_fn_leaf);
1687 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1691 #ifdef CONFIG_IPV6_SUBTREES
1692 if (FIB6_SUBTREE(pn) == fn) {
1693 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1694 RCU_INIT_POINTER(pn->subtree, NULL);
1697 WARN_ON(fn->fn_flags & RTN_ROOT);
1700 rcu_assign_pointer(pn->right, child);
1701 else if (pn_l == fn)
1702 rcu_assign_pointer(pn->left, child);
1708 rcu_assign_pointer(child->parent, pn);
1710 #ifdef CONFIG_IPV6_SUBTREES
1714 read_lock(&net->ipv6.fib6_walker_lock);
1715 FOR_WALKERS(net, w) {
1717 if (w->node == fn) {
1718 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1723 if (w->node == fn) {
1726 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1727 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1729 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1730 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1735 read_unlock(&net->ipv6.fib6_walker_lock);
1738 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1741 RCU_INIT_POINTER(pn->leaf, NULL);
1742 fib6_info_release(pn_leaf);
1747 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1748 struct fib6_info __rcu **rtp, struct nl_info *info)
1750 struct fib6_walker *w;
1751 struct fib6_info *rt = rcu_dereference_protected(*rtp,
1752 lockdep_is_held(&table->tb6_lock));
1753 struct net *net = info->nl_net;
1755 RT6_TRACE("fib6_del_route\n");
1758 *rtp = rt->fib6_next;
1759 rt->fib6_node = NULL;
1760 net->ipv6.rt6_stats->fib_rt_entries--;
1761 net->ipv6.rt6_stats->fib_discarded_routes++;
1763 /* Flush all cached dst in exception table */
1764 rt6_flush_exceptions(rt);
1766 /* Reset round-robin state, if necessary */
1767 if (rcu_access_pointer(fn->rr_ptr) == rt)
1770 /* Remove this entry from other siblings */
1771 if (rt->fib6_nsiblings) {
1772 struct fib6_info *sibling, *next_sibling;
1774 list_for_each_entry_safe(sibling, next_sibling,
1775 &rt->fib6_siblings, fib6_siblings)
1776 sibling->fib6_nsiblings--;
1777 rt->fib6_nsiblings = 0;
1778 list_del_init(&rt->fib6_siblings);
1779 rt6_multipath_rebalance(next_sibling);
1782 /* Adjust walkers */
1783 read_lock(&net->ipv6.fib6_walker_lock);
1784 FOR_WALKERS(net, w) {
1785 if (w->state == FWS_C && w->leaf == rt) {
1786 RT6_TRACE("walker %p adjusted by delroute\n", w);
1787 w->leaf = rcu_dereference_protected(rt->fib6_next,
1788 lockdep_is_held(&table->tb6_lock));
1793 read_unlock(&net->ipv6.fib6_walker_lock);
1795 /* If it was last route, call fib6_repair_tree() to:
1796 * 1. For root node, put back null_entry as how the table was created.
1797 * 2. For other nodes, expunge its radix tree node.
1799 if (!rcu_access_pointer(fn->leaf)) {
1800 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1801 fn->fn_flags &= ~RTN_RTINFO;
1802 net->ipv6.rt6_stats->fib_route_nodes--;
1804 fn = fib6_repair_tree(net, table, fn);
1807 fib6_purge_rt(rt, fn, net);
1809 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
1810 if (!info->skip_notify)
1811 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
1812 fib6_info_release(rt);
1815 /* Need to own table->tb6_lock */
1816 int fib6_del(struct fib6_info *rt, struct nl_info *info)
1818 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1819 lockdep_is_held(&rt->fib6_table->tb6_lock));
1820 struct fib6_table *table = rt->fib6_table;
1821 struct net *net = info->nl_net;
1822 struct fib6_info __rcu **rtp;
1823 struct fib6_info __rcu **rtp_next;
1825 if (!fn || rt == net->ipv6.fib6_null_entry)
1828 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1831 * Walk the leaf entries looking for ourself
1834 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1835 struct fib6_info *cur = rcu_dereference_protected(*rtp,
1836 lockdep_is_held(&table->tb6_lock));
1838 fib6_del_route(table, fn, rtp, info);
1841 rtp_next = &cur->fib6_next;
1847 * Tree traversal function.
1849 * Certainly, it is not interrupt safe.
1850 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1851 * It means, that we can modify tree during walking
1852 * and use this function for garbage collection, clone pruning,
1853 * cleaning tree when a device goes down etc. etc.
1855 * It guarantees that every node will be traversed,
1856 * and that it will be traversed only once.
1858 * Callback function w->func may return:
1859 * 0 -> continue walking.
1860 * positive value -> walking is suspended (used by tree dumps,
1861 * and probably by gc, if it will be split to several slices)
1862 * negative value -> terminate walking.
1864 * The function itself returns:
1865 * 0 -> walk is complete.
1866 * >0 -> walk is incomplete (i.e. suspended)
1867 * <0 -> walk is terminated by an error.
1869 * This function is called with tb6_lock held.
1872 static int fib6_walk_continue(struct fib6_walker *w)
1874 struct fib6_node *fn, *pn, *left, *right;
1876 /* w->root should always be table->tb6_root */
1877 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1885 #ifdef CONFIG_IPV6_SUBTREES
1887 if (FIB6_SUBTREE(fn)) {
1888 w->node = FIB6_SUBTREE(fn);
1895 left = rcu_dereference_protected(fn->left, 1);
1898 w->state = FWS_INIT;
1904 right = rcu_dereference_protected(fn->right, 1);
1907 w->state = FWS_INIT;
1911 w->leaf = rcu_dereference_protected(fn->leaf, 1);
1914 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1935 pn = rcu_dereference_protected(fn->parent, 1);
1936 left = rcu_dereference_protected(pn->left, 1);
1937 right = rcu_dereference_protected(pn->right, 1);
1939 #ifdef CONFIG_IPV6_SUBTREES
1940 if (FIB6_SUBTREE(pn) == fn) {
1941 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1952 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
1962 static int fib6_walk(struct net *net, struct fib6_walker *w)
1966 w->state = FWS_INIT;
1969 fib6_walker_link(net, w);
1970 res = fib6_walk_continue(w);
1972 fib6_walker_unlink(net, w);
1976 static int fib6_clean_node(struct fib6_walker *w)
1979 struct fib6_info *rt;
1980 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1981 struct nl_info info = {
1983 .skip_notify = c->skip_notify,
1986 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1987 w->node->fn_sernum != c->sernum)
1988 w->node->fn_sernum = c->sernum;
1991 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1996 for_each_fib6_walker_rt(w) {
1997 res = c->func(rt, c->arg);
2000 res = fib6_del(rt, &info);
2003 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
2005 rcu_access_pointer(rt->fib6_node),
2011 } else if (res == -2) {
2012 if (WARN_ON(!rt->fib6_nsiblings))
2014 rt = list_last_entry(&rt->fib6_siblings,
2015 struct fib6_info, fib6_siblings);
2025 * Convenient frontend to tree walker.
2027 * func is called on each route.
2028 * It may return -2 -> skip multipath route.
2029 * -1 -> delete this route.
2030 * 0 -> continue walking
2033 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2034 int (*func)(struct fib6_info *, void *arg),
2035 int sernum, void *arg, bool skip_notify)
2037 struct fib6_cleaner c;
2040 c.w.func = fib6_clean_node;
2047 c.skip_notify = skip_notify;
2049 fib6_walk(net, &c.w);
2052 static void __fib6_clean_all(struct net *net,
2053 int (*func)(struct fib6_info *, void *),
2054 int sernum, void *arg, bool skip_notify)
2056 struct fib6_table *table;
2057 struct hlist_head *head;
2061 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2062 head = &net->ipv6.fib_table_hash[h];
2063 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2064 spin_lock_bh(&table->tb6_lock);
2065 fib6_clean_tree(net, &table->tb6_root,
2066 func, sernum, arg, skip_notify);
2067 spin_unlock_bh(&table->tb6_lock);
2073 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
2076 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2079 void fib6_clean_all_skip_notify(struct net *net,
2080 int (*func)(struct fib6_info *, void *),
2083 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
2086 static void fib6_flush_trees(struct net *net)
2088 int new_sernum = fib6_new_sernum(net);
2090 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
2094 * Garbage collection
2097 static int fib6_age(struct fib6_info *rt, void *arg)
2099 struct fib6_gc_args *gc_args = arg;
2100 unsigned long now = jiffies;
2103 * check addrconf expiration here.
2104 * Routes are expired even if they are in use.
2107 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
2108 if (time_after(now, rt->expires)) {
2109 RT6_TRACE("expiring %p\n", rt);
2115 /* Also age clones in the exception table.
2116 * Note, that clones are aged out
2117 * only if they are not in use now.
2119 rt6_age_exceptions(rt, gc_args, now);
2124 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2126 struct fib6_gc_args gc_args;
2130 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2131 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2132 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2135 gc_args.timeout = expires ? (int)expires :
2136 net->ipv6.sysctl.ip6_rt_gc_interval;
2139 fib6_clean_all(net, fib6_age, &gc_args);
2141 net->ipv6.ip6_rt_last_gc = now;
2144 mod_timer(&net->ipv6.ip6_fib_timer,
2146 + net->ipv6.sysctl.ip6_rt_gc_interval));
2148 del_timer(&net->ipv6.ip6_fib_timer);
2149 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2152 static void fib6_gc_timer_cb(struct timer_list *t)
2154 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2156 fib6_run_gc(0, arg, true);
2159 static int __net_init fib6_net_init(struct net *net)
2161 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2164 err = fib6_notifier_init(net);
2168 spin_lock_init(&net->ipv6.fib6_gc_lock);
2169 rwlock_init(&net->ipv6.fib6_walker_lock);
2170 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2171 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2173 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2174 if (!net->ipv6.rt6_stats)
2177 /* Avoid false sharing : Use at least a full cache line */
2178 size = max_t(size_t, size, L1_CACHE_BYTES);
2180 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2181 if (!net->ipv6.fib_table_hash)
2184 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2186 if (!net->ipv6.fib6_main_tbl)
2187 goto out_fib_table_hash;
2189 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2190 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2191 net->ipv6.fib6_null_entry);
2192 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2193 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2194 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2196 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2197 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2199 if (!net->ipv6.fib6_local_tbl)
2200 goto out_fib6_main_tbl;
2201 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2202 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2203 net->ipv6.fib6_null_entry);
2204 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2205 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2206 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2208 fib6_tables_init(net);
2212 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2214 kfree(net->ipv6.fib6_main_tbl);
2217 kfree(net->ipv6.fib_table_hash);
2219 kfree(net->ipv6.rt6_stats);
2221 fib6_notifier_exit(net);
2225 static void fib6_net_exit(struct net *net)
2229 del_timer_sync(&net->ipv6.ip6_fib_timer);
2231 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2232 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2233 struct hlist_node *tmp;
2234 struct fib6_table *tb;
2236 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2237 hlist_del(&tb->tb6_hlist);
2238 fib6_free_table(tb);
2242 kfree(net->ipv6.fib_table_hash);
2243 kfree(net->ipv6.rt6_stats);
2244 fib6_notifier_exit(net);
2247 static struct pernet_operations fib6_net_ops = {
2248 .init = fib6_net_init,
2249 .exit = fib6_net_exit,
2252 int __init fib6_init(void)
2256 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2257 sizeof(struct fib6_node),
2258 0, SLAB_HWCACHE_ALIGN,
2260 if (!fib6_node_kmem)
2263 ret = register_pernet_subsys(&fib6_net_ops);
2265 goto out_kmem_cache_create;
2267 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2270 goto out_unregister_subsys;
2272 __fib6_flush_trees = fib6_flush_trees;
2276 out_unregister_subsys:
2277 unregister_pernet_subsys(&fib6_net_ops);
2278 out_kmem_cache_create:
2279 kmem_cache_destroy(fib6_node_kmem);
2283 void fib6_gc_cleanup(void)
2285 unregister_pernet_subsys(&fib6_net_ops);
2286 kmem_cache_destroy(fib6_node_kmem);
2289 #ifdef CONFIG_PROC_FS
2290 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2292 struct fib6_info *rt = v;
2293 struct ipv6_route_iter *iter = seq->private;
2294 unsigned int flags = rt->fib6_flags;
2295 const struct net_device *dev;
2297 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
2299 #ifdef CONFIG_IPV6_SUBTREES
2300 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
2302 seq_puts(seq, "00000000000000000000000000000000 00 ");
2304 if (rt->fib6_nh.fib_nh_gw_family) {
2305 flags |= RTF_GATEWAY;
2306 seq_printf(seq, "%pi6", &rt->fib6_nh.fib_nh_gw6);
2308 seq_puts(seq, "00000000000000000000000000000000");
2311 dev = rt->fib6_nh.fib_nh_dev;
2312 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2313 rt->fib6_metric, refcount_read(&rt->fib6_ref), 0,
2314 flags, dev ? dev->name : "");
2315 iter->w.leaf = NULL;
2319 static int ipv6_route_yield(struct fib6_walker *w)
2321 struct ipv6_route_iter *iter = w->args;
2327 iter->w.leaf = rcu_dereference_protected(
2328 iter->w.leaf->fib6_next,
2329 lockdep_is_held(&iter->tbl->tb6_lock));
2331 if (!iter->skip && iter->w.leaf)
2333 } while (iter->w.leaf);
2338 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2341 memset(&iter->w, 0, sizeof(iter->w));
2342 iter->w.func = ipv6_route_yield;
2343 iter->w.root = &iter->tbl->tb6_root;
2344 iter->w.state = FWS_INIT;
2345 iter->w.node = iter->w.root;
2346 iter->w.args = iter;
2347 iter->sernum = iter->w.root->fn_sernum;
2348 INIT_LIST_HEAD(&iter->w.lh);
2349 fib6_walker_link(net, &iter->w);
2352 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2356 struct hlist_node *node;
2359 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2360 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2366 while (!node && h < FIB6_TABLE_HASHSZ) {
2367 node = rcu_dereference_bh(
2368 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2370 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2373 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2375 if (iter->sernum != iter->w.root->fn_sernum) {
2376 iter->sernum = iter->w.root->fn_sernum;
2377 iter->w.state = FWS_INIT;
2378 iter->w.node = iter->w.root;
2379 WARN_ON(iter->w.skip);
2380 iter->w.skip = iter->w.count;
2384 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2387 struct fib6_info *n;
2388 struct net *net = seq_file_net(seq);
2389 struct ipv6_route_iter *iter = seq->private;
2394 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
2401 ipv6_route_check_sernum(iter);
2402 spin_lock_bh(&iter->tbl->tb6_lock);
2403 r = fib6_walk_continue(&iter->w);
2404 spin_unlock_bh(&iter->tbl->tb6_lock);
2408 return iter->w.leaf;
2410 fib6_walker_unlink(net, &iter->w);
2413 fib6_walker_unlink(net, &iter->w);
2415 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2419 ipv6_route_seq_setup_walk(iter, net);
2423 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2426 struct net *net = seq_file_net(seq);
2427 struct ipv6_route_iter *iter = seq->private;
2430 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2434 ipv6_route_seq_setup_walk(iter, net);
2435 return ipv6_route_seq_next(seq, NULL, pos);
2441 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2443 struct fib6_walker *w = &iter->w;
2444 return w->node && !(w->state == FWS_U && w->node == w->root);
2447 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2450 struct net *net = seq_file_net(seq);
2451 struct ipv6_route_iter *iter = seq->private;
2453 if (ipv6_route_iter_active(iter))
2454 fib6_walker_unlink(net, &iter->w);
2456 rcu_read_unlock_bh();
2459 const struct seq_operations ipv6_route_seq_ops = {
2460 .start = ipv6_route_seq_start,
2461 .next = ipv6_route_seq_next,
2462 .stop = ipv6_route_seq_stop,
2463 .show = ipv6_route_seq_show
2465 #endif /* CONFIG_PROC_FS */