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 atomic_inc(&f6i->fib6_ref);
170 void fib6_info_destroy_rcu(struct rcu_head *head)
172 struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
173 struct rt6_exception_bucket *bucket;
175 WARN_ON(f6i->fib6_node);
177 bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
179 f6i->rt6i_exception_bucket = NULL;
183 if (f6i->rt6i_pcpu) {
186 for_each_possible_cpu(cpu) {
187 struct rt6_info **ppcpu_rt;
188 struct rt6_info *pcpu_rt;
190 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
193 dst_dev_put(&pcpu_rt->dst);
194 dst_release(&pcpu_rt->dst);
199 free_percpu(f6i->rt6i_pcpu);
202 lwtstate_put(f6i->fib6_nh.nh_lwtstate);
204 if (f6i->fib6_nh.nh_dev)
205 dev_put(f6i->fib6_nh.nh_dev);
207 ip_fib_metrics_put(f6i->fib6_metrics);
211 EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
213 static struct fib6_node *node_alloc(struct net *net)
215 struct fib6_node *fn;
217 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
219 net->ipv6.rt6_stats->fib_nodes++;
224 static void node_free_immediate(struct net *net, struct fib6_node *fn)
226 kmem_cache_free(fib6_node_kmem, fn);
227 net->ipv6.rt6_stats->fib_nodes--;
230 static void node_free_rcu(struct rcu_head *head)
232 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
234 kmem_cache_free(fib6_node_kmem, fn);
237 static void node_free(struct net *net, struct fib6_node *fn)
239 call_rcu(&fn->rcu, node_free_rcu);
240 net->ipv6.rt6_stats->fib_nodes--;
243 static void fib6_free_table(struct fib6_table *table)
245 inetpeer_invalidate_tree(&table->tb6_peers);
249 static void fib6_link_table(struct net *net, struct fib6_table *tb)
254 * Initialize table lock at a single place to give lockdep a key,
255 * tables aren't visible prior to being linked to the list.
257 spin_lock_init(&tb->tb6_lock);
258 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
261 * No protection necessary, this is the only list mutatation
262 * operation, tables never disappear once they exist.
264 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
267 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
269 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
271 struct fib6_table *table;
273 table = kzalloc(sizeof(*table), GFP_ATOMIC);
276 rcu_assign_pointer(table->tb6_root.leaf,
277 net->ipv6.fib6_null_entry);
278 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
279 inet_peer_base_init(&table->tb6_peers);
285 struct fib6_table *fib6_new_table(struct net *net, u32 id)
287 struct fib6_table *tb;
291 tb = fib6_get_table(net, id);
295 tb = fib6_alloc_table(net, id);
297 fib6_link_table(net, tb);
301 EXPORT_SYMBOL_GPL(fib6_new_table);
303 struct fib6_table *fib6_get_table(struct net *net, u32 id)
305 struct fib6_table *tb;
306 struct hlist_head *head;
311 h = id & (FIB6_TABLE_HASHSZ - 1);
313 head = &net->ipv6.fib_table_hash[h];
314 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
315 if (tb->tb6_id == id) {
324 EXPORT_SYMBOL_GPL(fib6_get_table);
326 static void __net_init fib6_tables_init(struct net *net)
328 fib6_link_table(net, net->ipv6.fib6_main_tbl);
329 fib6_link_table(net, net->ipv6.fib6_local_tbl);
333 struct fib6_table *fib6_new_table(struct net *net, u32 id)
335 return fib6_get_table(net, id);
338 struct fib6_table *fib6_get_table(struct net *net, u32 id)
340 return net->ipv6.fib6_main_tbl;
343 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
344 const struct sk_buff *skb,
345 int flags, pol_lookup_t lookup)
349 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
350 if (rt->dst.error == -EAGAIN) {
352 rt = net->ipv6.ip6_null_entry;
359 /* called with rcu lock held; no reference taken on fib6_info */
360 struct fib6_info *fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
363 return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6, flags);
366 static void __net_init fib6_tables_init(struct net *net)
368 fib6_link_table(net, net->ipv6.fib6_main_tbl);
373 unsigned int fib6_tables_seq_read(struct net *net)
375 unsigned int h, fib_seq = 0;
378 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
379 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
380 struct fib6_table *tb;
382 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
383 fib_seq += tb->fib_seq;
390 static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
391 enum fib_event_type event_type,
392 struct fib6_info *rt)
394 struct fib6_entry_notifier_info info = {
398 return call_fib6_notifier(nb, net, event_type, &info.info);
401 static int call_fib6_entry_notifiers(struct net *net,
402 enum fib_event_type event_type,
403 struct fib6_info *rt,
404 struct netlink_ext_ack *extack)
406 struct fib6_entry_notifier_info info = {
407 .info.extack = extack,
411 rt->fib6_table->fib_seq++;
412 return call_fib6_notifiers(net, event_type, &info.info);
415 struct fib6_dump_arg {
417 struct notifier_block *nb;
420 static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
422 if (rt == arg->net->ipv6.fib6_null_entry)
424 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
427 static int fib6_node_dump(struct fib6_walker *w)
429 struct fib6_info *rt;
431 for_each_fib6_walker_rt(w)
432 fib6_rt_dump(rt, w->args);
437 static void fib6_table_dump(struct net *net, struct fib6_table *tb,
438 struct fib6_walker *w)
440 w->root = &tb->tb6_root;
441 spin_lock_bh(&tb->tb6_lock);
443 spin_unlock_bh(&tb->tb6_lock);
446 /* Called with rcu_read_lock() */
447 int fib6_tables_dump(struct net *net, struct notifier_block *nb)
449 struct fib6_dump_arg arg;
450 struct fib6_walker *w;
453 w = kzalloc(sizeof(*w), GFP_ATOMIC);
457 w->func = fib6_node_dump;
462 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
463 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
464 struct fib6_table *tb;
466 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
467 fib6_table_dump(net, tb, w);
475 static int fib6_dump_node(struct fib6_walker *w)
478 struct fib6_info *rt;
480 for_each_fib6_walker_rt(w) {
481 res = rt6_dump_route(rt, w->args);
483 /* Frame is full, suspend walking */
488 /* Multipath routes are dumped in one route with the
489 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
490 * last sibling of this route (no need to dump the
491 * sibling routes again)
493 if (rt->fib6_nsiblings)
494 rt = list_last_entry(&rt->fib6_siblings,
502 static void fib6_dump_end(struct netlink_callback *cb)
504 struct net *net = sock_net(cb->skb->sk);
505 struct fib6_walker *w = (void *)cb->args[2];
510 fib6_walker_unlink(net, w);
515 cb->done = (void *)cb->args[3];
519 static int fib6_dump_done(struct netlink_callback *cb)
522 return cb->done ? cb->done(cb) : 0;
525 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
526 struct netlink_callback *cb)
528 struct net *net = sock_net(skb->sk);
529 struct fib6_walker *w;
532 w = (void *)cb->args[2];
533 w->root = &table->tb6_root;
535 if (cb->args[4] == 0) {
539 spin_lock_bh(&table->tb6_lock);
540 res = fib6_walk(net, w);
541 spin_unlock_bh(&table->tb6_lock);
544 cb->args[5] = w->root->fn_sernum;
547 if (cb->args[5] != w->root->fn_sernum) {
548 /* Begin at the root if the tree changed */
549 cb->args[5] = w->root->fn_sernum;
556 spin_lock_bh(&table->tb6_lock);
557 res = fib6_walk_continue(w);
558 spin_unlock_bh(&table->tb6_lock);
560 fib6_walker_unlink(net, w);
568 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
570 const struct nlmsghdr *nlh = cb->nlh;
571 struct net *net = sock_net(skb->sk);
572 struct rt6_rtnl_dump_arg arg = {};
574 unsigned int e = 0, s_e;
575 struct fib6_walker *w;
576 struct fib6_table *tb;
577 struct hlist_head *head;
580 if (cb->strict_check) {
583 err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
586 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
587 struct rtmsg *rtm = nlmsg_data(nlh);
589 arg.filter.flags = rtm->rtm_flags & (RTM_F_PREFIX|RTM_F_CLONED);
592 /* fib entries are never clones */
593 if (arg.filter.flags & RTM_F_CLONED)
596 w = (void *)cb->args[2];
600 * 1. hook callback destructor.
602 cb->args[3] = (long)cb->done;
603 cb->done = fib6_dump_done;
606 * 2. allocate and initialize walker.
608 w = kzalloc(sizeof(*w), GFP_ATOMIC);
611 w->func = fib6_dump_node;
612 cb->args[2] = (long)w;
620 if (arg.filter.table_id) {
621 tb = fib6_get_table(net, arg.filter.table_id);
623 if (arg.filter.dump_all_families)
626 NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
631 res = fib6_dump_table(tb, skb, cb);
642 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
644 head = &net->ipv6.fib_table_hash[h];
645 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
648 res = fib6_dump_table(tb, skb, cb);
660 res = res < 0 ? res : skb->len;
666 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
671 if (f6i->fib6_metrics == &dst_default_metrics) {
672 struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
677 refcount_set(&p->refcnt, 1);
678 f6i->fib6_metrics = p;
681 f6i->fib6_metrics->metrics[metric - 1] = val;
687 * return the appropriate node for a routing tree "add" operation
688 * by either creating and inserting or by returning an existing
692 static struct fib6_node *fib6_add_1(struct net *net,
693 struct fib6_table *table,
694 struct fib6_node *root,
695 struct in6_addr *addr, int plen,
696 int offset, int allow_create,
697 int replace_required,
698 struct netlink_ext_ack *extack)
700 struct fib6_node *fn, *in, *ln;
701 struct fib6_node *pn = NULL;
706 RT6_TRACE("fib6_add_1\n");
708 /* insert node in tree */
713 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
714 lockdep_is_held(&table->tb6_lock));
715 key = (struct rt6key *)((u8 *)leaf + offset);
720 if (plen < fn->fn_bit ||
721 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
723 if (replace_required) {
724 NL_SET_ERR_MSG(extack,
725 "Can not replace route - no match found");
726 pr_warn("Can't replace route, no match found\n");
727 return ERR_PTR(-ENOENT);
729 pr_warn("NLM_F_CREATE should be set when creating new route\n");
738 if (plen == fn->fn_bit) {
739 /* clean up an intermediate node */
740 if (!(fn->fn_flags & RTN_RTINFO)) {
741 RCU_INIT_POINTER(fn->leaf, NULL);
742 fib6_info_release(leaf);
743 /* remove null_entry in the root node */
744 } else if (fn->fn_flags & RTN_TL_ROOT &&
745 rcu_access_pointer(fn->leaf) ==
746 net->ipv6.fib6_null_entry) {
747 RCU_INIT_POINTER(fn->leaf, NULL);
754 * We have more bits to go
757 /* Try to walk down on tree. */
758 dir = addr_bit_set(addr, fn->fn_bit);
761 rcu_dereference_protected(fn->right,
762 lockdep_is_held(&table->tb6_lock)) :
763 rcu_dereference_protected(fn->left,
764 lockdep_is_held(&table->tb6_lock));
768 /* We should not create new node because
769 * NLM_F_REPLACE was specified without NLM_F_CREATE
770 * I assume it is safe to require NLM_F_CREATE when
771 * REPLACE flag is used! Later we may want to remove the
772 * check for replace_required, because according
773 * to netlink specification, NLM_F_CREATE
774 * MUST be specified if new route is created.
775 * That would keep IPv6 consistent with IPv4
777 if (replace_required) {
778 NL_SET_ERR_MSG(extack,
779 "Can not replace route - no match found");
780 pr_warn("Can't replace route, no match found\n");
781 return ERR_PTR(-ENOENT);
783 pr_warn("NLM_F_CREATE should be set when creating new route\n");
786 * We walked to the bottom of tree.
787 * Create new leaf node without children.
790 ln = node_alloc(net);
793 return ERR_PTR(-ENOMEM);
795 RCU_INIT_POINTER(ln->parent, pn);
798 rcu_assign_pointer(pn->right, ln);
800 rcu_assign_pointer(pn->left, ln);
807 * split since we don't have a common prefix anymore or
808 * we have a less significant route.
809 * we've to insert an intermediate node on the list
810 * this new node will point to the one we need to create
814 pn = rcu_dereference_protected(fn->parent,
815 lockdep_is_held(&table->tb6_lock));
817 /* find 1st bit in difference between the 2 addrs.
819 See comment in __ipv6_addr_diff: bit may be an invalid value,
820 but if it is >= plen, the value is ignored in any case.
823 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
828 * (new leaf node)[ln] (old node)[fn]
831 in = node_alloc(net);
832 ln = node_alloc(net);
836 node_free_immediate(net, in);
838 node_free_immediate(net, ln);
839 return ERR_PTR(-ENOMEM);
843 * new intermediate node.
845 * be off since that an address that chooses one of
846 * the branches would not match less specific routes
847 * in the other branch
852 RCU_INIT_POINTER(in->parent, pn);
854 atomic_inc(&rcu_dereference_protected(in->leaf,
855 lockdep_is_held(&table->tb6_lock))->fib6_ref);
857 /* update parent pointer */
859 rcu_assign_pointer(pn->right, in);
861 rcu_assign_pointer(pn->left, in);
865 RCU_INIT_POINTER(ln->parent, in);
866 rcu_assign_pointer(fn->parent, in);
868 if (addr_bit_set(addr, bit)) {
869 rcu_assign_pointer(in->right, ln);
870 rcu_assign_pointer(in->left, fn);
872 rcu_assign_pointer(in->left, ln);
873 rcu_assign_pointer(in->right, fn);
875 } else { /* plen <= bit */
878 * (new leaf node)[ln]
880 * (old node)[fn] NULL
883 ln = node_alloc(net);
886 return ERR_PTR(-ENOMEM);
890 RCU_INIT_POINTER(ln->parent, pn);
892 if (addr_bit_set(&key->addr, plen))
893 RCU_INIT_POINTER(ln->right, fn);
895 RCU_INIT_POINTER(ln->left, fn);
897 rcu_assign_pointer(fn->parent, ln);
900 rcu_assign_pointer(pn->right, ln);
902 rcu_assign_pointer(pn->left, ln);
907 static void fib6_drop_pcpu_from(struct fib6_info *f6i,
908 const struct fib6_table *table)
912 /* release the reference to this fib entry from
913 * all of its cached pcpu routes
915 for_each_possible_cpu(cpu) {
916 struct rt6_info **ppcpu_rt;
917 struct rt6_info *pcpu_rt;
919 ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
922 struct fib6_info *from;
924 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
925 fib6_info_release(from);
930 static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
933 struct fib6_table *table = rt->fib6_table;
935 if (atomic_read(&rt->fib6_ref) != 1) {
936 /* This route is used as dummy address holder in some split
937 * nodes. It is not leaked, but it still holds other resources,
938 * which must be released in time. So, scan ascendant nodes
939 * and replace dummy references to this route with references
940 * to still alive ones.
943 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
944 lockdep_is_held(&table->tb6_lock));
945 struct fib6_info *new_leaf;
946 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
947 new_leaf = fib6_find_prefix(net, table, fn);
948 atomic_inc(&new_leaf->fib6_ref);
950 rcu_assign_pointer(fn->leaf, new_leaf);
951 fib6_info_release(rt);
953 fn = rcu_dereference_protected(fn->parent,
954 lockdep_is_held(&table->tb6_lock));
958 fib6_drop_pcpu_from(rt, table);
963 * Insert routing information in a node.
966 static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
967 struct nl_info *info,
968 struct netlink_ext_ack *extack)
970 struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
971 lockdep_is_held(&rt->fib6_table->tb6_lock));
972 struct fib6_info *iter = NULL;
973 struct fib6_info __rcu **ins;
974 struct fib6_info __rcu **fallback_ins = NULL;
975 int replace = (info->nlh &&
976 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
977 int add = (!info->nlh ||
978 (info->nlh->nlmsg_flags & NLM_F_CREATE));
980 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
981 u16 nlflags = NLM_F_EXCL;
984 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
985 nlflags |= NLM_F_APPEND;
989 for (iter = leaf; iter;
990 iter = rcu_dereference_protected(iter->fib6_next,
991 lockdep_is_held(&rt->fib6_table->tb6_lock))) {
993 * Search for duplicates
996 if (iter->fib6_metric == rt->fib6_metric) {
998 * Same priority level
1001 (info->nlh->nlmsg_flags & NLM_F_EXCL))
1004 nlflags &= ~NLM_F_EXCL;
1006 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
1011 fallback_ins = fallback_ins ?: ins;
1015 if (rt6_duplicate_nexthop(iter, rt)) {
1016 if (rt->fib6_nsiblings)
1017 rt->fib6_nsiblings = 0;
1018 if (!(iter->fib6_flags & RTF_EXPIRES))
1020 if (!(rt->fib6_flags & RTF_EXPIRES))
1021 fib6_clean_expires(iter);
1023 fib6_set_expires(iter, rt->expires);
1026 fib6_metric_set(iter, RTAX_MTU,
1030 /* If we have the same destination and the same metric,
1031 * but not the same gateway, then the route we try to
1032 * add is sibling to this route, increment our counter
1033 * of siblings, and later we will add our route to the
1035 * Only static routes (which don't have flag
1036 * RTF_EXPIRES) are used for ECMPv6.
1038 * To avoid long list, we only had siblings if the
1039 * route have a gateway.
1042 rt6_qualify_for_ecmp(iter))
1043 rt->fib6_nsiblings++;
1046 if (iter->fib6_metric > rt->fib6_metric)
1050 ins = &iter->fib6_next;
1053 if (fallback_ins && !found) {
1054 /* No ECMP-able route found, replace first non-ECMP one */
1056 iter = rcu_dereference_protected(*ins,
1057 lockdep_is_held(&rt->fib6_table->tb6_lock));
1061 /* Reset round-robin state, if necessary */
1062 if (ins == &fn->leaf)
1065 /* Link this route to others same route. */
1066 if (rt->fib6_nsiblings) {
1067 unsigned int fib6_nsiblings;
1068 struct fib6_info *sibling, *temp_sibling;
1070 /* Find the first route that have the same metric */
1073 if (sibling->fib6_metric == rt->fib6_metric &&
1074 rt6_qualify_for_ecmp(sibling)) {
1075 list_add_tail(&rt->fib6_siblings,
1076 &sibling->fib6_siblings);
1079 sibling = rcu_dereference_protected(sibling->fib6_next,
1080 lockdep_is_held(&rt->fib6_table->tb6_lock));
1082 /* For each sibling in the list, increment the counter of
1083 * siblings. BUG() if counters does not match, list of siblings
1087 list_for_each_entry_safe(sibling, temp_sibling,
1088 &rt->fib6_siblings, fib6_siblings) {
1089 sibling->fib6_nsiblings++;
1090 BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1093 BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1094 rt6_multipath_rebalance(temp_sibling);
1102 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1105 nlflags |= NLM_F_CREATE;
1107 err = call_fib6_entry_notifiers(info->nl_net,
1108 FIB_EVENT_ENTRY_ADD,
1113 rcu_assign_pointer(rt->fib6_next, iter);
1114 atomic_inc(&rt->fib6_ref);
1115 rcu_assign_pointer(rt->fib6_node, fn);
1116 rcu_assign_pointer(*ins, rt);
1117 if (!info->skip_notify)
1118 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1119 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1121 if (!(fn->fn_flags & RTN_RTINFO)) {
1122 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1123 fn->fn_flags |= RTN_RTINFO;
1132 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1136 err = call_fib6_entry_notifiers(info->nl_net,
1137 FIB_EVENT_ENTRY_REPLACE,
1142 atomic_inc(&rt->fib6_ref);
1143 rcu_assign_pointer(rt->fib6_node, fn);
1144 rt->fib6_next = iter->fib6_next;
1145 rcu_assign_pointer(*ins, rt);
1146 if (!info->skip_notify)
1147 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1148 if (!(fn->fn_flags & RTN_RTINFO)) {
1149 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1150 fn->fn_flags |= RTN_RTINFO;
1152 nsiblings = iter->fib6_nsiblings;
1153 iter->fib6_node = NULL;
1154 fib6_purge_rt(iter, fn, info->nl_net);
1155 if (rcu_access_pointer(fn->rr_ptr) == iter)
1157 fib6_info_release(iter);
1160 /* Replacing an ECMP route, remove all siblings */
1161 ins = &rt->fib6_next;
1162 iter = rcu_dereference_protected(*ins,
1163 lockdep_is_held(&rt->fib6_table->tb6_lock));
1165 if (iter->fib6_metric > rt->fib6_metric)
1167 if (rt6_qualify_for_ecmp(iter)) {
1168 *ins = iter->fib6_next;
1169 iter->fib6_node = NULL;
1170 fib6_purge_rt(iter, fn, info->nl_net);
1171 if (rcu_access_pointer(fn->rr_ptr) == iter)
1173 fib6_info_release(iter);
1175 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1177 ins = &iter->fib6_next;
1179 iter = rcu_dereference_protected(*ins,
1180 lockdep_is_held(&rt->fib6_table->tb6_lock));
1182 WARN_ON(nsiblings != 0);
1189 static void fib6_start_gc(struct net *net, struct fib6_info *rt)
1191 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1192 (rt->fib6_flags & RTF_EXPIRES))
1193 mod_timer(&net->ipv6.ip6_fib_timer,
1194 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1197 void fib6_force_start_gc(struct net *net)
1199 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1200 mod_timer(&net->ipv6.ip6_fib_timer,
1201 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1204 static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
1207 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1208 lockdep_is_held(&rt->fib6_table->tb6_lock));
1210 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1213 fn->fn_sernum = sernum;
1214 fn = rcu_dereference_protected(fn->parent,
1215 lockdep_is_held(&rt->fib6_table->tb6_lock));
1219 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
1221 __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1225 * Add routing information to the routing tree.
1226 * <destination addr>/<source addr>
1227 * with source addr info in sub-trees
1228 * Need to own table->tb6_lock
1231 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
1232 struct nl_info *info, struct netlink_ext_ack *extack)
1234 struct fib6_table *table = rt->fib6_table;
1235 struct fib6_node *fn, *pn = NULL;
1237 int allow_create = 1;
1238 int replace_required = 0;
1239 int sernum = fib6_new_sernum(info->nl_net);
1242 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1244 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1245 replace_required = 1;
1247 if (!allow_create && !replace_required)
1248 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1250 fn = fib6_add_1(info->nl_net, table, root,
1251 &rt->fib6_dst.addr, rt->fib6_dst.plen,
1252 offsetof(struct fib6_info, fib6_dst), allow_create,
1253 replace_required, extack);
1262 #ifdef CONFIG_IPV6_SUBTREES
1263 if (rt->fib6_src.plen) {
1264 struct fib6_node *sn;
1266 if (!rcu_access_pointer(fn->subtree)) {
1267 struct fib6_node *sfn;
1279 /* Create subtree root node */
1280 sfn = node_alloc(info->nl_net);
1284 atomic_inc(&info->nl_net->ipv6.fib6_null_entry->fib6_ref);
1285 rcu_assign_pointer(sfn->leaf,
1286 info->nl_net->ipv6.fib6_null_entry);
1287 sfn->fn_flags = RTN_ROOT;
1289 /* Now add the first leaf node to new subtree */
1291 sn = fib6_add_1(info->nl_net, table, sfn,
1292 &rt->fib6_src.addr, rt->fib6_src.plen,
1293 offsetof(struct fib6_info, fib6_src),
1294 allow_create, replace_required, extack);
1297 /* If it is failed, discard just allocated
1298 root, and then (in failure) stale node
1301 node_free_immediate(info->nl_net, sfn);
1306 /* Now link new subtree to main tree */
1307 rcu_assign_pointer(sfn->parent, fn);
1308 rcu_assign_pointer(fn->subtree, sfn);
1310 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1311 &rt->fib6_src.addr, rt->fib6_src.plen,
1312 offsetof(struct fib6_info, fib6_src),
1313 allow_create, replace_required, extack);
1321 if (!rcu_access_pointer(fn->leaf)) {
1322 if (fn->fn_flags & RTN_TL_ROOT) {
1323 /* put back null_entry for root node */
1324 rcu_assign_pointer(fn->leaf,
1325 info->nl_net->ipv6.fib6_null_entry);
1327 atomic_inc(&rt->fib6_ref);
1328 rcu_assign_pointer(fn->leaf, rt);
1335 err = fib6_add_rt2node(fn, rt, info, extack);
1337 __fib6_update_sernum_upto_root(rt, sernum);
1338 fib6_start_gc(info->nl_net, rt);
1343 #ifdef CONFIG_IPV6_SUBTREES
1345 * If fib6_add_1 has cleared the old leaf pointer in the
1346 * super-tree leaf node we have to find a new one for it.
1349 struct fib6_info *pn_leaf =
1350 rcu_dereference_protected(pn->leaf,
1351 lockdep_is_held(&table->tb6_lock));
1352 if (pn_leaf == rt) {
1354 RCU_INIT_POINTER(pn->leaf, NULL);
1355 fib6_info_release(rt);
1357 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1358 pn_leaf = fib6_find_prefix(info->nl_net, table,
1364 info->nl_net->ipv6.fib6_null_entry;
1367 fib6_info_hold(pn_leaf);
1368 rcu_assign_pointer(pn->leaf, pn_leaf);
1377 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1378 * 1. fn is an intermediate node and we failed to add the new
1379 * route to it in both subtree creation failure and fib6_add_rt2node()
1381 * 2. fn is the root node in the table and we fail to add the first
1382 * default route to it.
1385 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1386 (fn->fn_flags & RTN_TL_ROOT &&
1387 !rcu_access_pointer(fn->leaf))))
1388 fib6_repair_tree(info->nl_net, table, fn);
1393 * Routing tree lookup
1397 struct lookup_args {
1398 int offset; /* key offset on fib6_info */
1399 const struct in6_addr *addr; /* search key */
1402 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1403 struct lookup_args *args)
1405 struct fib6_node *fn;
1408 if (unlikely(args->offset == 0))
1418 struct fib6_node *next;
1420 dir = addr_bit_set(args->addr, fn->fn_bit);
1422 next = dir ? rcu_dereference(fn->right) :
1423 rcu_dereference(fn->left);
1433 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1435 if (subtree || fn->fn_flags & RTN_RTINFO) {
1436 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1442 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1444 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1445 #ifdef CONFIG_IPV6_SUBTREES
1447 struct fib6_node *sfn;
1448 sfn = fib6_node_lookup_1(subtree,
1455 if (fn->fn_flags & RTN_RTINFO)
1460 if (fn->fn_flags & RTN_ROOT)
1463 fn = rcu_dereference(fn->parent);
1469 /* called with rcu_read_lock() held
1471 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1472 const struct in6_addr *daddr,
1473 const struct in6_addr *saddr)
1475 struct fib6_node *fn;
1476 struct lookup_args args[] = {
1478 .offset = offsetof(struct fib6_info, fib6_dst),
1481 #ifdef CONFIG_IPV6_SUBTREES
1483 .offset = offsetof(struct fib6_info, fib6_src),
1488 .offset = 0, /* sentinel */
1492 fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
1493 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1500 * Get node with specified destination prefix (and source prefix,
1501 * if subtrees are used)
1502 * exact_match == true means we try to find fn with exact match of
1503 * the passed in prefix addr
1504 * exact_match == false means we try to find fn with longest prefix
1505 * match of the passed in prefix addr. This is useful for finding fn
1506 * for cached route as it will be stored in the exception table under
1507 * the node with longest prefix length.
1511 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1512 const struct in6_addr *addr,
1513 int plen, int offset,
1516 struct fib6_node *fn, *prev = NULL;
1518 for (fn = root; fn ; ) {
1519 struct fib6_info *leaf = rcu_dereference(fn->leaf);
1522 /* This node is being deleted */
1524 if (plen <= fn->fn_bit)
1530 key = (struct rt6key *)((u8 *)leaf + offset);
1535 if (plen < fn->fn_bit ||
1536 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1539 if (plen == fn->fn_bit)
1546 * We have more bits to go
1548 if (addr_bit_set(addr, fn->fn_bit))
1549 fn = rcu_dereference(fn->right);
1551 fn = rcu_dereference(fn->left);
1560 struct fib6_node *fib6_locate(struct fib6_node *root,
1561 const struct in6_addr *daddr, int dst_len,
1562 const struct in6_addr *saddr, int src_len,
1565 struct fib6_node *fn;
1567 fn = fib6_locate_1(root, daddr, dst_len,
1568 offsetof(struct fib6_info, fib6_dst),
1571 #ifdef CONFIG_IPV6_SUBTREES
1573 WARN_ON(saddr == NULL);
1575 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1578 fn = fib6_locate_1(subtree, saddr, src_len,
1579 offsetof(struct fib6_info, fib6_src),
1586 if (fn && fn->fn_flags & RTN_RTINFO)
1598 static struct fib6_info *fib6_find_prefix(struct net *net,
1599 struct fib6_table *table,
1600 struct fib6_node *fn)
1602 struct fib6_node *child_left, *child_right;
1604 if (fn->fn_flags & RTN_ROOT)
1605 return net->ipv6.fib6_null_entry;
1608 child_left = rcu_dereference_protected(fn->left,
1609 lockdep_is_held(&table->tb6_lock));
1610 child_right = rcu_dereference_protected(fn->right,
1611 lockdep_is_held(&table->tb6_lock));
1613 return rcu_dereference_protected(child_left->leaf,
1614 lockdep_is_held(&table->tb6_lock));
1616 return rcu_dereference_protected(child_right->leaf,
1617 lockdep_is_held(&table->tb6_lock));
1619 fn = FIB6_SUBTREE(fn);
1625 * Called to trim the tree of intermediate nodes when possible. "fn"
1626 * is the node we want to try and remove.
1627 * Need to own table->tb6_lock
1630 static struct fib6_node *fib6_repair_tree(struct net *net,
1631 struct fib6_table *table,
1632 struct fib6_node *fn)
1636 struct fib6_node *child;
1637 struct fib6_walker *w;
1640 /* Set fn->leaf to null_entry for root node. */
1641 if (fn->fn_flags & RTN_TL_ROOT) {
1642 rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1647 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1648 lockdep_is_held(&table->tb6_lock));
1649 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1650 lockdep_is_held(&table->tb6_lock));
1651 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1652 lockdep_is_held(&table->tb6_lock));
1653 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1654 lockdep_is_held(&table->tb6_lock));
1655 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1656 lockdep_is_held(&table->tb6_lock));
1657 struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1658 lockdep_is_held(&table->tb6_lock));
1659 struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1660 lockdep_is_held(&table->tb6_lock));
1661 struct fib6_info *new_fn_leaf;
1663 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1666 WARN_ON(fn->fn_flags & RTN_RTINFO);
1667 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1673 child = fn_r, children |= 1;
1675 child = fn_l, children |= 2;
1677 if (children == 3 || FIB6_SUBTREE(fn)
1678 #ifdef CONFIG_IPV6_SUBTREES
1679 /* Subtree root (i.e. fn) may have one child */
1680 || (children && fn->fn_flags & RTN_ROOT)
1683 new_fn_leaf = fib6_find_prefix(net, table, fn);
1686 WARN_ON(!new_fn_leaf);
1687 new_fn_leaf = net->ipv6.fib6_null_entry;
1690 fib6_info_hold(new_fn_leaf);
1691 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1695 #ifdef CONFIG_IPV6_SUBTREES
1696 if (FIB6_SUBTREE(pn) == fn) {
1697 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1698 RCU_INIT_POINTER(pn->subtree, NULL);
1701 WARN_ON(fn->fn_flags & RTN_ROOT);
1704 rcu_assign_pointer(pn->right, child);
1705 else if (pn_l == fn)
1706 rcu_assign_pointer(pn->left, child);
1712 rcu_assign_pointer(child->parent, pn);
1714 #ifdef CONFIG_IPV6_SUBTREES
1718 read_lock(&net->ipv6.fib6_walker_lock);
1719 FOR_WALKERS(net, w) {
1721 if (w->node == fn) {
1722 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1727 if (w->node == fn) {
1730 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1731 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1733 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1734 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1739 read_unlock(&net->ipv6.fib6_walker_lock);
1742 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1745 RCU_INIT_POINTER(pn->leaf, NULL);
1746 fib6_info_release(pn_leaf);
1751 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1752 struct fib6_info __rcu **rtp, struct nl_info *info)
1754 struct fib6_walker *w;
1755 struct fib6_info *rt = rcu_dereference_protected(*rtp,
1756 lockdep_is_held(&table->tb6_lock));
1757 struct net *net = info->nl_net;
1759 RT6_TRACE("fib6_del_route\n");
1762 *rtp = rt->fib6_next;
1763 rt->fib6_node = NULL;
1764 net->ipv6.rt6_stats->fib_rt_entries--;
1765 net->ipv6.rt6_stats->fib_discarded_routes++;
1767 /* Flush all cached dst in exception table */
1768 rt6_flush_exceptions(rt);
1770 /* Reset round-robin state, if necessary */
1771 if (rcu_access_pointer(fn->rr_ptr) == rt)
1774 /* Remove this entry from other siblings */
1775 if (rt->fib6_nsiblings) {
1776 struct fib6_info *sibling, *next_sibling;
1778 list_for_each_entry_safe(sibling, next_sibling,
1779 &rt->fib6_siblings, fib6_siblings)
1780 sibling->fib6_nsiblings--;
1781 rt->fib6_nsiblings = 0;
1782 list_del_init(&rt->fib6_siblings);
1783 rt6_multipath_rebalance(next_sibling);
1786 /* Adjust walkers */
1787 read_lock(&net->ipv6.fib6_walker_lock);
1788 FOR_WALKERS(net, w) {
1789 if (w->state == FWS_C && w->leaf == rt) {
1790 RT6_TRACE("walker %p adjusted by delroute\n", w);
1791 w->leaf = rcu_dereference_protected(rt->fib6_next,
1792 lockdep_is_held(&table->tb6_lock));
1797 read_unlock(&net->ipv6.fib6_walker_lock);
1799 /* If it was last route, call fib6_repair_tree() to:
1800 * 1. For root node, put back null_entry as how the table was created.
1801 * 2. For other nodes, expunge its radix tree node.
1803 if (!rcu_access_pointer(fn->leaf)) {
1804 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1805 fn->fn_flags &= ~RTN_RTINFO;
1806 net->ipv6.rt6_stats->fib_route_nodes--;
1808 fn = fib6_repair_tree(net, table, fn);
1811 fib6_purge_rt(rt, fn, net);
1813 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
1814 if (!info->skip_notify)
1815 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
1816 fib6_info_release(rt);
1819 /* Need to own table->tb6_lock */
1820 int fib6_del(struct fib6_info *rt, struct nl_info *info)
1822 struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1823 lockdep_is_held(&rt->fib6_table->tb6_lock));
1824 struct fib6_table *table = rt->fib6_table;
1825 struct net *net = info->nl_net;
1826 struct fib6_info __rcu **rtp;
1827 struct fib6_info __rcu **rtp_next;
1829 if (!fn || rt == net->ipv6.fib6_null_entry)
1832 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1835 * Walk the leaf entries looking for ourself
1838 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1839 struct fib6_info *cur = rcu_dereference_protected(*rtp,
1840 lockdep_is_held(&table->tb6_lock));
1842 fib6_del_route(table, fn, rtp, info);
1845 rtp_next = &cur->fib6_next;
1851 * Tree traversal function.
1853 * Certainly, it is not interrupt safe.
1854 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1855 * It means, that we can modify tree during walking
1856 * and use this function for garbage collection, clone pruning,
1857 * cleaning tree when a device goes down etc. etc.
1859 * It guarantees that every node will be traversed,
1860 * and that it will be traversed only once.
1862 * Callback function w->func may return:
1863 * 0 -> continue walking.
1864 * positive value -> walking is suspended (used by tree dumps,
1865 * and probably by gc, if it will be split to several slices)
1866 * negative value -> terminate walking.
1868 * The function itself returns:
1869 * 0 -> walk is complete.
1870 * >0 -> walk is incomplete (i.e. suspended)
1871 * <0 -> walk is terminated by an error.
1873 * This function is called with tb6_lock held.
1876 static int fib6_walk_continue(struct fib6_walker *w)
1878 struct fib6_node *fn, *pn, *left, *right;
1880 /* w->root should always be table->tb6_root */
1881 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1889 #ifdef CONFIG_IPV6_SUBTREES
1891 if (FIB6_SUBTREE(fn)) {
1892 w->node = FIB6_SUBTREE(fn);
1899 left = rcu_dereference_protected(fn->left, 1);
1902 w->state = FWS_INIT;
1908 right = rcu_dereference_protected(fn->right, 1);
1911 w->state = FWS_INIT;
1915 w->leaf = rcu_dereference_protected(fn->leaf, 1);
1918 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1939 pn = rcu_dereference_protected(fn->parent, 1);
1940 left = rcu_dereference_protected(pn->left, 1);
1941 right = rcu_dereference_protected(pn->right, 1);
1943 #ifdef CONFIG_IPV6_SUBTREES
1944 if (FIB6_SUBTREE(pn) == fn) {
1945 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1956 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
1966 static int fib6_walk(struct net *net, struct fib6_walker *w)
1970 w->state = FWS_INIT;
1973 fib6_walker_link(net, w);
1974 res = fib6_walk_continue(w);
1976 fib6_walker_unlink(net, w);
1980 static int fib6_clean_node(struct fib6_walker *w)
1983 struct fib6_info *rt;
1984 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1985 struct nl_info info = {
1987 .skip_notify = c->skip_notify,
1990 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1991 w->node->fn_sernum != c->sernum)
1992 w->node->fn_sernum = c->sernum;
1995 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
2000 for_each_fib6_walker_rt(w) {
2001 res = c->func(rt, c->arg);
2004 res = fib6_del(rt, &info);
2007 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
2009 rcu_access_pointer(rt->fib6_node),
2015 } else if (res == -2) {
2016 if (WARN_ON(!rt->fib6_nsiblings))
2018 rt = list_last_entry(&rt->fib6_siblings,
2019 struct fib6_info, fib6_siblings);
2029 * Convenient frontend to tree walker.
2031 * func is called on each route.
2032 * It may return -2 -> skip multipath route.
2033 * -1 -> delete this route.
2034 * 0 -> continue walking
2037 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2038 int (*func)(struct fib6_info *, void *arg),
2039 int sernum, void *arg, bool skip_notify)
2041 struct fib6_cleaner c;
2044 c.w.func = fib6_clean_node;
2051 c.skip_notify = skip_notify;
2053 fib6_walk(net, &c.w);
2056 static void __fib6_clean_all(struct net *net,
2057 int (*func)(struct fib6_info *, void *),
2058 int sernum, void *arg, bool skip_notify)
2060 struct fib6_table *table;
2061 struct hlist_head *head;
2065 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2066 head = &net->ipv6.fib_table_hash[h];
2067 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2068 spin_lock_bh(&table->tb6_lock);
2069 fib6_clean_tree(net, &table->tb6_root,
2070 func, sernum, arg, skip_notify);
2071 spin_unlock_bh(&table->tb6_lock);
2077 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
2080 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2083 void fib6_clean_all_skip_notify(struct net *net,
2084 int (*func)(struct fib6_info *, void *),
2087 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
2090 static void fib6_flush_trees(struct net *net)
2092 int new_sernum = fib6_new_sernum(net);
2094 __fib6_clean_all(net, NULL, new_sernum, NULL, false);
2098 * Garbage collection
2101 static int fib6_age(struct fib6_info *rt, void *arg)
2103 struct fib6_gc_args *gc_args = arg;
2104 unsigned long now = jiffies;
2107 * check addrconf expiration here.
2108 * Routes are expired even if they are in use.
2111 if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
2112 if (time_after(now, rt->expires)) {
2113 RT6_TRACE("expiring %p\n", rt);
2119 /* Also age clones in the exception table.
2120 * Note, that clones are aged out
2121 * only if they are not in use now.
2123 rt6_age_exceptions(rt, gc_args, now);
2128 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2130 struct fib6_gc_args gc_args;
2134 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2135 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2136 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2139 gc_args.timeout = expires ? (int)expires :
2140 net->ipv6.sysctl.ip6_rt_gc_interval;
2143 fib6_clean_all(net, fib6_age, &gc_args);
2145 net->ipv6.ip6_rt_last_gc = now;
2148 mod_timer(&net->ipv6.ip6_fib_timer,
2150 + net->ipv6.sysctl.ip6_rt_gc_interval));
2152 del_timer(&net->ipv6.ip6_fib_timer);
2153 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2156 static void fib6_gc_timer_cb(struct timer_list *t)
2158 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2160 fib6_run_gc(0, arg, true);
2163 static int __net_init fib6_net_init(struct net *net)
2165 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2168 err = fib6_notifier_init(net);
2172 spin_lock_init(&net->ipv6.fib6_gc_lock);
2173 rwlock_init(&net->ipv6.fib6_walker_lock);
2174 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2175 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2177 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2178 if (!net->ipv6.rt6_stats)
2181 /* Avoid false sharing : Use at least a full cache line */
2182 size = max_t(size_t, size, L1_CACHE_BYTES);
2184 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2185 if (!net->ipv6.fib_table_hash)
2188 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2190 if (!net->ipv6.fib6_main_tbl)
2191 goto out_fib_table_hash;
2193 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2194 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2195 net->ipv6.fib6_null_entry);
2196 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2197 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2198 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2200 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2201 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2203 if (!net->ipv6.fib6_local_tbl)
2204 goto out_fib6_main_tbl;
2205 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2206 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2207 net->ipv6.fib6_null_entry);
2208 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2209 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2210 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2212 fib6_tables_init(net);
2216 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2218 kfree(net->ipv6.fib6_main_tbl);
2221 kfree(net->ipv6.fib_table_hash);
2223 kfree(net->ipv6.rt6_stats);
2225 fib6_notifier_exit(net);
2229 static void fib6_net_exit(struct net *net)
2233 del_timer_sync(&net->ipv6.ip6_fib_timer);
2235 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2236 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2237 struct hlist_node *tmp;
2238 struct fib6_table *tb;
2240 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2241 hlist_del(&tb->tb6_hlist);
2242 fib6_free_table(tb);
2246 kfree(net->ipv6.fib_table_hash);
2247 kfree(net->ipv6.rt6_stats);
2248 fib6_notifier_exit(net);
2251 static struct pernet_operations fib6_net_ops = {
2252 .init = fib6_net_init,
2253 .exit = fib6_net_exit,
2256 int __init fib6_init(void)
2260 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2261 sizeof(struct fib6_node),
2262 0, SLAB_HWCACHE_ALIGN,
2264 if (!fib6_node_kmem)
2267 ret = register_pernet_subsys(&fib6_net_ops);
2269 goto out_kmem_cache_create;
2271 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
2274 goto out_unregister_subsys;
2276 __fib6_flush_trees = fib6_flush_trees;
2280 out_unregister_subsys:
2281 unregister_pernet_subsys(&fib6_net_ops);
2282 out_kmem_cache_create:
2283 kmem_cache_destroy(fib6_node_kmem);
2287 void fib6_gc_cleanup(void)
2289 unregister_pernet_subsys(&fib6_net_ops);
2290 kmem_cache_destroy(fib6_node_kmem);
2293 #ifdef CONFIG_PROC_FS
2294 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2296 struct fib6_info *rt = v;
2297 struct ipv6_route_iter *iter = seq->private;
2298 const struct net_device *dev;
2300 seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
2302 #ifdef CONFIG_IPV6_SUBTREES
2303 seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
2305 seq_puts(seq, "00000000000000000000000000000000 00 ");
2307 if (rt->fib6_flags & RTF_GATEWAY)
2308 seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
2310 seq_puts(seq, "00000000000000000000000000000000");
2312 dev = rt->fib6_nh.nh_dev;
2313 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2314 rt->fib6_metric, atomic_read(&rt->fib6_ref), 0,
2315 rt->fib6_flags, dev ? dev->name : "");
2316 iter->w.leaf = NULL;
2320 static int ipv6_route_yield(struct fib6_walker *w)
2322 struct ipv6_route_iter *iter = w->args;
2328 iter->w.leaf = rcu_dereference_protected(
2329 iter->w.leaf->fib6_next,
2330 lockdep_is_held(&iter->tbl->tb6_lock));
2332 if (!iter->skip && iter->w.leaf)
2334 } while (iter->w.leaf);
2339 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2342 memset(&iter->w, 0, sizeof(iter->w));
2343 iter->w.func = ipv6_route_yield;
2344 iter->w.root = &iter->tbl->tb6_root;
2345 iter->w.state = FWS_INIT;
2346 iter->w.node = iter->w.root;
2347 iter->w.args = iter;
2348 iter->sernum = iter->w.root->fn_sernum;
2349 INIT_LIST_HEAD(&iter->w.lh);
2350 fib6_walker_link(net, &iter->w);
2353 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2357 struct hlist_node *node;
2360 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2361 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2367 while (!node && h < FIB6_TABLE_HASHSZ) {
2368 node = rcu_dereference_bh(
2369 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2371 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2374 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2376 if (iter->sernum != iter->w.root->fn_sernum) {
2377 iter->sernum = iter->w.root->fn_sernum;
2378 iter->w.state = FWS_INIT;
2379 iter->w.node = iter->w.root;
2380 WARN_ON(iter->w.skip);
2381 iter->w.skip = iter->w.count;
2385 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2388 struct fib6_info *n;
2389 struct net *net = seq_file_net(seq);
2390 struct ipv6_route_iter *iter = seq->private;
2395 n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
2402 ipv6_route_check_sernum(iter);
2403 spin_lock_bh(&iter->tbl->tb6_lock);
2404 r = fib6_walk_continue(&iter->w);
2405 spin_unlock_bh(&iter->tbl->tb6_lock);
2409 return iter->w.leaf;
2411 fib6_walker_unlink(net, &iter->w);
2414 fib6_walker_unlink(net, &iter->w);
2416 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2420 ipv6_route_seq_setup_walk(iter, net);
2424 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2427 struct net *net = seq_file_net(seq);
2428 struct ipv6_route_iter *iter = seq->private;
2431 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2435 ipv6_route_seq_setup_walk(iter, net);
2436 return ipv6_route_seq_next(seq, NULL, pos);
2442 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2444 struct fib6_walker *w = &iter->w;
2445 return w->node && !(w->state == FWS_U && w->node == w->root);
2448 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2451 struct net *net = seq_file_net(seq);
2452 struct ipv6_route_iter *iter = seq->private;
2454 if (ipv6_route_iter_active(iter))
2455 fib6_walker_unlink(net, &iter->w);
2457 rcu_read_unlock_bh();
2460 const struct seq_operations ipv6_route_seq_ops = {
2461 .start = ipv6_route_seq_start,
2462 .next = ipv6_route_seq_next,
2463 .stop = ipv6_route_seq_stop,
2464 .show = ipv6_route_seq_show
2466 #endif /* CONFIG_PROC_FS */