2 * Linux INET6 implementation
3 * Forwarding Information Database
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
17 * Ville Nuorvala: Fixed routing subtrees.
20 #define pr_fmt(fmt) "IPv6: " fmt
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/net.h>
25 #include <linux/route.h>
26 #include <linux/netdevice.h>
27 #include <linux/in6.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
33 #include <net/ndisc.h>
34 #include <net/addrconf.h>
36 #include <net/ip6_fib.h>
37 #include <net/ip6_route.h>
42 #define RT6_TRACE(x...) pr_debug(x)
44 #define RT6_TRACE(x...) do { ; } while (0)
47 static struct kmem_cache *fib6_node_kmem __read_mostly;
52 int (*func)(struct rt6_info *, void *arg);
57 static DEFINE_RWLOCK(fib6_walker_lock);
59 #ifdef CONFIG_IPV6_SUBTREES
60 #define FWS_INIT FWS_S
62 #define FWS_INIT FWS_L
65 static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
66 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
67 static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
68 static int fib6_walk(struct fib6_walker *w);
69 static int fib6_walk_continue(struct fib6_walker *w);
72 * A routing update causes an increase of the serial number on the
73 * affected subtree. This allows for cached routes to be asynchronously
74 * tested when modifications are made to the destination cache as a
75 * result of redirects, path MTU changes, etc.
78 static void fib6_gc_timer_cb(unsigned long arg);
80 static LIST_HEAD(fib6_walkers);
81 #define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
83 static void fib6_walker_link(struct fib6_walker *w)
85 write_lock_bh(&fib6_walker_lock);
86 list_add(&w->lh, &fib6_walkers);
87 write_unlock_bh(&fib6_walker_lock);
90 static void fib6_walker_unlink(struct fib6_walker *w)
92 write_lock_bh(&fib6_walker_lock);
94 write_unlock_bh(&fib6_walker_lock);
97 static int fib6_new_sernum(struct net *net)
102 old = atomic_read(&net->ipv6.fib6_sernum);
103 new = old < INT_MAX ? old + 1 : 1;
104 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
110 FIB6_NO_SERNUM_CHANGE = 0,
114 * Auxiliary address test functions for the radix tree.
116 * These assume a 32bit processor (although it will work on
123 #if defined(__LITTLE_ENDIAN)
124 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
126 # define BITOP_BE32_SWIZZLE 0
129 static __be32 addr_bit_set(const void *token, int fn_bit)
131 const __be32 *addr = token;
134 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
135 * is optimized version of
136 * htonl(1 << ((~fn_bit)&0x1F))
137 * See include/asm-generic/bitops/le.h.
139 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
143 static struct fib6_node *node_alloc(void)
145 struct fib6_node *fn;
147 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
152 static void node_free(struct fib6_node *fn)
154 kmem_cache_free(fib6_node_kmem, fn);
157 static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
161 if (!non_pcpu_rt->rt6i_pcpu)
164 for_each_possible_cpu(cpu) {
165 struct rt6_info **ppcpu_rt;
166 struct rt6_info *pcpu_rt;
168 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
171 dst_free(&pcpu_rt->dst);
176 non_pcpu_rt->rt6i_pcpu = NULL;
179 static void rt6_release(struct rt6_info *rt)
181 if (atomic_dec_and_test(&rt->rt6i_ref)) {
187 static void fib6_link_table(struct net *net, struct fib6_table *tb)
192 * Initialize table lock at a single place to give lockdep a key,
193 * tables aren't visible prior to being linked to the list.
195 rwlock_init(&tb->tb6_lock);
197 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
200 * No protection necessary, this is the only list mutatation
201 * operation, tables never disappear once they exist.
203 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
206 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
208 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
210 struct fib6_table *table;
212 table = kzalloc(sizeof(*table), GFP_ATOMIC);
215 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
216 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
217 inet_peer_base_init(&table->tb6_peers);
223 struct fib6_table *fib6_new_table(struct net *net, u32 id)
225 struct fib6_table *tb;
229 tb = fib6_get_table(net, id);
233 tb = fib6_alloc_table(net, id);
235 fib6_link_table(net, tb);
240 struct fib6_table *fib6_get_table(struct net *net, u32 id)
242 struct fib6_table *tb;
243 struct hlist_head *head;
248 h = id & (FIB6_TABLE_HASHSZ - 1);
250 head = &net->ipv6.fib_table_hash[h];
251 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
252 if (tb->tb6_id == id) {
262 static void __net_init fib6_tables_init(struct net *net)
264 fib6_link_table(net, net->ipv6.fib6_main_tbl);
265 fib6_link_table(net, net->ipv6.fib6_local_tbl);
269 struct fib6_table *fib6_new_table(struct net *net, u32 id)
271 return fib6_get_table(net, id);
274 struct fib6_table *fib6_get_table(struct net *net, u32 id)
276 return net->ipv6.fib6_main_tbl;
279 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
280 int flags, pol_lookup_t lookup)
282 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
285 static void __net_init fib6_tables_init(struct net *net)
287 fib6_link_table(net, net->ipv6.fib6_main_tbl);
292 static int fib6_dump_node(struct fib6_walker *w)
297 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
298 res = rt6_dump_route(rt, w->args);
300 /* Frame is full, suspend walking */
309 static void fib6_dump_end(struct netlink_callback *cb)
311 struct fib6_walker *w = (void *)cb->args[2];
316 fib6_walker_unlink(w);
321 cb->done = (void *)cb->args[3];
325 static int fib6_dump_done(struct netlink_callback *cb)
328 return cb->done ? cb->done(cb) : 0;
331 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
332 struct netlink_callback *cb)
334 struct fib6_walker *w;
337 w = (void *)cb->args[2];
338 w->root = &table->tb6_root;
340 if (cb->args[4] == 0) {
344 read_lock_bh(&table->tb6_lock);
346 read_unlock_bh(&table->tb6_lock);
349 cb->args[5] = w->root->fn_sernum;
352 if (cb->args[5] != w->root->fn_sernum) {
353 /* Begin at the root if the tree changed */
354 cb->args[5] = w->root->fn_sernum;
361 read_lock_bh(&table->tb6_lock);
362 res = fib6_walk_continue(w);
363 read_unlock_bh(&table->tb6_lock);
365 fib6_walker_unlink(w);
373 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
375 struct net *net = sock_net(skb->sk);
377 unsigned int e = 0, s_e;
378 struct rt6_rtnl_dump_arg arg;
379 struct fib6_walker *w;
380 struct fib6_table *tb;
381 struct hlist_head *head;
387 w = (void *)cb->args[2];
391 * 1. hook callback destructor.
393 cb->args[3] = (long)cb->done;
394 cb->done = fib6_dump_done;
397 * 2. allocate and initialize walker.
399 w = kzalloc(sizeof(*w), GFP_ATOMIC);
402 w->func = fib6_dump_node;
403 cb->args[2] = (long)w;
412 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
414 head = &net->ipv6.fib_table_hash[h];
415 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
418 res = fib6_dump_table(tb, skb, cb);
430 res = res < 0 ? res : skb->len;
439 * return the appropriate node for a routing tree "add" operation
440 * by either creating and inserting or by returning an existing
444 static struct fib6_node *fib6_add_1(struct fib6_node *root,
445 struct in6_addr *addr, int plen,
446 int offset, int allow_create,
447 int replace_required, int sernum)
449 struct fib6_node *fn, *in, *ln;
450 struct fib6_node *pn = NULL;
455 RT6_TRACE("fib6_add_1\n");
457 /* insert node in tree */
462 key = (struct rt6key *)((u8 *)fn->leaf + offset);
467 if (plen < fn->fn_bit ||
468 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
470 if (replace_required) {
471 pr_warn("Can't replace route, no match found\n");
472 return ERR_PTR(-ENOENT);
474 pr_warn("NLM_F_CREATE should be set when creating new route\n");
483 if (plen == fn->fn_bit) {
484 /* clean up an intermediate node */
485 if (!(fn->fn_flags & RTN_RTINFO)) {
486 rt6_release(fn->leaf);
490 fn->fn_sernum = sernum;
496 * We have more bits to go
499 /* Try to walk down on tree. */
500 fn->fn_sernum = sernum;
501 dir = addr_bit_set(addr, fn->fn_bit);
503 fn = dir ? fn->right : fn->left;
507 /* We should not create new node because
508 * NLM_F_REPLACE was specified without NLM_F_CREATE
509 * I assume it is safe to require NLM_F_CREATE when
510 * REPLACE flag is used! Later we may want to remove the
511 * check for replace_required, because according
512 * to netlink specification, NLM_F_CREATE
513 * MUST be specified if new route is created.
514 * That would keep IPv6 consistent with IPv4
516 if (replace_required) {
517 pr_warn("Can't replace route, no match found\n");
518 return ERR_PTR(-ENOENT);
520 pr_warn("NLM_F_CREATE should be set when creating new route\n");
523 * We walked to the bottom of tree.
524 * Create new leaf node without children.
530 return ERR_PTR(-ENOMEM);
534 ln->fn_sernum = sernum;
546 * split since we don't have a common prefix anymore or
547 * we have a less significant route.
548 * we've to insert an intermediate node on the list
549 * this new node will point to the one we need to create
555 /* find 1st bit in difference between the 2 addrs.
557 See comment in __ipv6_addr_diff: bit may be an invalid value,
558 but if it is >= plen, the value is ignored in any case.
561 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
566 * (new leaf node)[ln] (old node)[fn]
577 return ERR_PTR(-ENOMEM);
581 * new intermediate node.
583 * be off since that an address that chooses one of
584 * the branches would not match less specific routes
585 * in the other branch
592 atomic_inc(&in->leaf->rt6i_ref);
594 in->fn_sernum = sernum;
596 /* update parent pointer */
607 ln->fn_sernum = sernum;
609 if (addr_bit_set(addr, bit)) {
616 } else { /* plen <= bit */
619 * (new leaf node)[ln]
621 * (old node)[fn] NULL
627 return ERR_PTR(-ENOMEM);
633 ln->fn_sernum = sernum;
640 if (addr_bit_set(&key->addr, plen))
650 static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
652 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
656 static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
660 for (i = 0; i < RTAX_MAX; i++) {
661 if (test_bit(i, mxc->mx_valid))
666 static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
671 if (dst->flags & DST_HOST) {
672 u32 *mp = dst_metrics_write_ptr(dst);
677 fib6_copy_metrics(mp, mxc);
679 dst_init_metrics(dst, mxc->mx, false);
681 /* We've stolen mx now. */
688 static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
691 if (atomic_read(&rt->rt6i_ref) != 1) {
692 /* This route is used as dummy address holder in some split
693 * nodes. It is not leaked, but it still holds other resources,
694 * which must be released in time. So, scan ascendant nodes
695 * and replace dummy references to this route with references
696 * to still alive ones.
699 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
700 fn->leaf = fib6_find_prefix(net, fn);
701 atomic_inc(&fn->leaf->rt6i_ref);
706 /* No more references are possible at this point. */
707 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
712 * Insert routing information in a node.
715 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
716 struct nl_info *info, struct mx6_config *mxc)
718 struct rt6_info *iter = NULL;
719 struct rt6_info **ins;
720 struct rt6_info **fallback_ins = NULL;
721 int replace = (info->nlh &&
722 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
723 int add = (!info->nlh ||
724 (info->nlh->nlmsg_flags & NLM_F_CREATE));
726 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
731 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
733 * Search for duplicates
736 if (iter->rt6i_metric == rt->rt6i_metric) {
738 * Same priority level
741 (info->nlh->nlmsg_flags & NLM_F_EXCL))
744 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
749 fallback_ins = fallback_ins ?: ins;
753 if (iter->dst.dev == rt->dst.dev &&
754 iter->rt6i_idev == rt->rt6i_idev &&
755 ipv6_addr_equal(&iter->rt6i_gateway,
756 &rt->rt6i_gateway)) {
757 if (rt->rt6i_nsiblings)
758 rt->rt6i_nsiblings = 0;
759 if (!(iter->rt6i_flags & RTF_EXPIRES))
761 if (!(rt->rt6i_flags & RTF_EXPIRES))
762 rt6_clean_expires(iter);
764 rt6_set_expires(iter, rt->dst.expires);
765 iter->rt6i_pmtu = rt->rt6i_pmtu;
768 /* If we have the same destination and the same metric,
769 * but not the same gateway, then the route we try to
770 * add is sibling to this route, increment our counter
771 * of siblings, and later we will add our route to the
773 * Only static routes (which don't have flag
774 * RTF_EXPIRES) are used for ECMPv6.
776 * To avoid long list, we only had siblings if the
777 * route have a gateway.
780 rt6_qualify_for_ecmp(iter))
781 rt->rt6i_nsiblings++;
784 if (iter->rt6i_metric > rt->rt6i_metric)
788 ins = &iter->dst.rt6_next;
791 if (fallback_ins && !found) {
792 /* No ECMP-able route found, replace first non-ECMP one */
798 /* Reset round-robin state, if necessary */
799 if (ins == &fn->leaf)
802 /* Link this route to others same route. */
803 if (rt->rt6i_nsiblings) {
804 unsigned int rt6i_nsiblings;
805 struct rt6_info *sibling, *temp_sibling;
807 /* Find the first route that have the same metric */
810 if (sibling->rt6i_metric == rt->rt6i_metric &&
811 rt6_qualify_for_ecmp(sibling)) {
812 list_add_tail(&rt->rt6i_siblings,
813 &sibling->rt6i_siblings);
816 sibling = sibling->dst.rt6_next;
818 /* For each sibling in the list, increment the counter of
819 * siblings. BUG() if counters does not match, list of siblings
823 list_for_each_entry_safe(sibling, temp_sibling,
824 &rt->rt6i_siblings, rt6i_siblings) {
825 sibling->rt6i_nsiblings++;
826 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
829 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
837 pr_warn("NLM_F_CREATE should be set when creating new route\n");
840 err = fib6_commit_metrics(&rt->dst, mxc);
844 rt->dst.rt6_next = iter;
847 atomic_inc(&rt->rt6i_ref);
848 inet6_rt_notify(RTM_NEWROUTE, rt, info);
849 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
851 if (!(fn->fn_flags & RTN_RTINFO)) {
852 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
853 fn->fn_flags |= RTN_RTINFO;
862 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
866 err = fib6_commit_metrics(&rt->dst, mxc);
872 rt->dst.rt6_next = iter->dst.rt6_next;
873 atomic_inc(&rt->rt6i_ref);
874 inet6_rt_notify(RTM_NEWROUTE, rt, info);
875 if (!(fn->fn_flags & RTN_RTINFO)) {
876 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
877 fn->fn_flags |= RTN_RTINFO;
879 nsiblings = iter->rt6i_nsiblings;
880 fib6_purge_rt(iter, fn, info->nl_net);
884 /* Replacing an ECMP route, remove all siblings */
885 ins = &rt->dst.rt6_next;
888 if (rt6_qualify_for_ecmp(iter)) {
889 *ins = iter->dst.rt6_next;
890 fib6_purge_rt(iter, fn, info->nl_net);
894 ins = &iter->dst.rt6_next;
898 WARN_ON(nsiblings != 0);
905 static void fib6_start_gc(struct net *net, struct rt6_info *rt)
907 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
908 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
909 mod_timer(&net->ipv6.ip6_fib_timer,
910 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
913 void fib6_force_start_gc(struct net *net)
915 if (!timer_pending(&net->ipv6.ip6_fib_timer))
916 mod_timer(&net->ipv6.ip6_fib_timer,
917 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
921 * Add routing information to the routing tree.
922 * <destination addr>/<source addr>
923 * with source addr info in sub-trees
926 int fib6_add(struct fib6_node *root, struct rt6_info *rt,
927 struct nl_info *info, struct mx6_config *mxc)
929 struct fib6_node *fn, *pn = NULL;
931 int allow_create = 1;
932 int replace_required = 0;
933 int sernum = fib6_new_sernum(info->nl_net);
936 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
938 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
939 replace_required = 1;
941 if (!allow_create && !replace_required)
942 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
944 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
945 offsetof(struct rt6_info, rt6i_dst), allow_create,
946 replace_required, sernum);
955 #ifdef CONFIG_IPV6_SUBTREES
956 if (rt->rt6i_src.plen) {
957 struct fib6_node *sn;
960 struct fib6_node *sfn;
972 /* Create subtree root node */
977 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
978 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
979 sfn->fn_flags = RTN_ROOT;
980 sfn->fn_sernum = sernum;
982 /* Now add the first leaf node to new subtree */
984 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
986 offsetof(struct rt6_info, rt6i_src),
987 allow_create, replace_required, sernum);
990 /* If it is failed, discard just allocated
991 root, and then (in st_failure) stale node
999 /* Now link new subtree to main tree */
1003 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
1005 offsetof(struct rt6_info, rt6i_src),
1006 allow_create, replace_required, sernum);
1016 atomic_inc(&rt->rt6i_ref);
1022 err = fib6_add_rt2node(fn, rt, info, mxc);
1024 fib6_start_gc(info->nl_net, rt);
1025 if (!(rt->rt6i_flags & RTF_CACHE))
1026 fib6_prune_clones(info->nl_net, pn);
1031 #ifdef CONFIG_IPV6_SUBTREES
1033 * If fib6_add_1 has cleared the old leaf pointer in the
1034 * super-tree leaf node we have to find a new one for it.
1036 if (pn != fn && pn->leaf == rt) {
1038 atomic_dec(&rt->rt6i_ref);
1040 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
1041 pn->leaf = fib6_find_prefix(info->nl_net, pn);
1044 WARN_ON(pn->leaf == NULL);
1045 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
1048 atomic_inc(&pn->leaf->rt6i_ref);
1055 #ifdef CONFIG_IPV6_SUBTREES
1056 /* Subtree creation failed, probably main tree node
1057 is orphan. If it is, shoot it.
1060 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
1061 fib6_repair_tree(info->nl_net, fn);
1068 * Routing tree lookup
1072 struct lookup_args {
1073 int offset; /* key offset on rt6_info */
1074 const struct in6_addr *addr; /* search key */
1077 static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1078 struct lookup_args *args)
1080 struct fib6_node *fn;
1083 if (unlikely(args->offset == 0))
1093 struct fib6_node *next;
1095 dir = addr_bit_set(args->addr, fn->fn_bit);
1097 next = dir ? fn->right : fn->left;
1107 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
1110 key = (struct rt6key *) ((u8 *) fn->leaf +
1113 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1114 #ifdef CONFIG_IPV6_SUBTREES
1116 struct fib6_node *sfn;
1117 sfn = fib6_lookup_1(fn->subtree,
1124 if (fn->fn_flags & RTN_RTINFO)
1128 #ifdef CONFIG_IPV6_SUBTREES
1131 if (fn->fn_flags & RTN_ROOT)
1140 struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1141 const struct in6_addr *saddr)
1143 struct fib6_node *fn;
1144 struct lookup_args args[] = {
1146 .offset = offsetof(struct rt6_info, rt6i_dst),
1149 #ifdef CONFIG_IPV6_SUBTREES
1151 .offset = offsetof(struct rt6_info, rt6i_src),
1156 .offset = 0, /* sentinel */
1160 fn = fib6_lookup_1(root, daddr ? args : args + 1);
1161 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1168 * Get node with specified destination prefix (and source prefix,
1169 * if subtrees are used)
1173 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1174 const struct in6_addr *addr,
1175 int plen, int offset)
1177 struct fib6_node *fn;
1179 for (fn = root; fn ; ) {
1180 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1185 if (plen < fn->fn_bit ||
1186 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1189 if (plen == fn->fn_bit)
1193 * We have more bits to go
1195 if (addr_bit_set(addr, fn->fn_bit))
1203 struct fib6_node *fib6_locate(struct fib6_node *root,
1204 const struct in6_addr *daddr, int dst_len,
1205 const struct in6_addr *saddr, int src_len)
1207 struct fib6_node *fn;
1209 fn = fib6_locate_1(root, daddr, dst_len,
1210 offsetof(struct rt6_info, rt6i_dst));
1212 #ifdef CONFIG_IPV6_SUBTREES
1214 WARN_ON(saddr == NULL);
1215 if (fn && fn->subtree)
1216 fn = fib6_locate_1(fn->subtree, saddr, src_len,
1217 offsetof(struct rt6_info, rt6i_src));
1221 if (fn && fn->fn_flags & RTN_RTINFO)
1233 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
1235 if (fn->fn_flags & RTN_ROOT)
1236 return net->ipv6.ip6_null_entry;
1240 return fn->left->leaf;
1242 return fn->right->leaf;
1244 fn = FIB6_SUBTREE(fn);
1250 * Called to trim the tree of intermediate nodes when possible. "fn"
1251 * is the node we want to try and remove.
1254 static struct fib6_node *fib6_repair_tree(struct net *net,
1255 struct fib6_node *fn)
1259 struct fib6_node *child, *pn;
1260 struct fib6_walker *w;
1264 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1267 WARN_ON(fn->fn_flags & RTN_RTINFO);
1268 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1274 child = fn->right, children |= 1;
1276 child = fn->left, children |= 2;
1278 if (children == 3 || FIB6_SUBTREE(fn)
1279 #ifdef CONFIG_IPV6_SUBTREES
1280 /* Subtree root (i.e. fn) may have one child */
1281 || (children && fn->fn_flags & RTN_ROOT)
1284 fn->leaf = fib6_find_prefix(net, fn);
1288 fn->leaf = net->ipv6.ip6_null_entry;
1291 atomic_inc(&fn->leaf->rt6i_ref);
1296 #ifdef CONFIG_IPV6_SUBTREES
1297 if (FIB6_SUBTREE(pn) == fn) {
1298 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1299 FIB6_SUBTREE(pn) = NULL;
1302 WARN_ON(fn->fn_flags & RTN_ROOT);
1304 if (pn->right == fn)
1306 else if (pn->left == fn)
1315 #ifdef CONFIG_IPV6_SUBTREES
1319 read_lock(&fib6_walker_lock);
1322 if (w->root == fn) {
1323 w->root = w->node = NULL;
1324 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1325 } else if (w->node == fn) {
1326 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1331 if (w->root == fn) {
1333 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1335 if (w->node == fn) {
1338 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1339 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1341 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1342 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1347 read_unlock(&fib6_walker_lock);
1350 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1353 rt6_release(pn->leaf);
1359 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
1360 struct nl_info *info)
1362 struct fib6_walker *w;
1363 struct rt6_info *rt = *rtp;
1364 struct net *net = info->nl_net;
1366 RT6_TRACE("fib6_del_route\n");
1369 *rtp = rt->dst.rt6_next;
1370 rt->rt6i_node = NULL;
1371 net->ipv6.rt6_stats->fib_rt_entries--;
1372 net->ipv6.rt6_stats->fib_discarded_routes++;
1374 /* Reset round-robin state, if necessary */
1375 if (fn->rr_ptr == rt)
1378 /* Remove this entry from other siblings */
1379 if (rt->rt6i_nsiblings) {
1380 struct rt6_info *sibling, *next_sibling;
1382 list_for_each_entry_safe(sibling, next_sibling,
1383 &rt->rt6i_siblings, rt6i_siblings)
1384 sibling->rt6i_nsiblings--;
1385 rt->rt6i_nsiblings = 0;
1386 list_del_init(&rt->rt6i_siblings);
1389 /* Adjust walkers */
1390 read_lock(&fib6_walker_lock);
1392 if (w->state == FWS_C && w->leaf == rt) {
1393 RT6_TRACE("walker %p adjusted by delroute\n", w);
1394 w->leaf = rt->dst.rt6_next;
1399 read_unlock(&fib6_walker_lock);
1401 rt->dst.rt6_next = NULL;
1403 /* If it was last route, expunge its radix tree node */
1405 fn->fn_flags &= ~RTN_RTINFO;
1406 net->ipv6.rt6_stats->fib_route_nodes--;
1407 fn = fib6_repair_tree(net, fn);
1410 fib6_purge_rt(rt, fn, net);
1412 inet6_rt_notify(RTM_DELROUTE, rt, info);
1416 int fib6_del(struct rt6_info *rt, struct nl_info *info)
1418 struct net *net = info->nl_net;
1419 struct fib6_node *fn = rt->rt6i_node;
1420 struct rt6_info **rtp;
1423 if (rt->dst.obsolete > 0) {
1428 if (!fn || rt == net->ipv6.ip6_null_entry)
1431 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1433 if (!(rt->rt6i_flags & RTF_CACHE)) {
1434 struct fib6_node *pn = fn;
1435 #ifdef CONFIG_IPV6_SUBTREES
1436 /* clones of this route might be in another subtree */
1437 if (rt->rt6i_src.plen) {
1438 while (!(pn->fn_flags & RTN_ROOT))
1443 fib6_prune_clones(info->nl_net, pn);
1447 * Walk the leaf entries looking for ourself
1450 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
1452 fib6_del_route(fn, rtp, info);
1460 * Tree traversal function.
1462 * Certainly, it is not interrupt safe.
1463 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1464 * It means, that we can modify tree during walking
1465 * and use this function for garbage collection, clone pruning,
1466 * cleaning tree when a device goes down etc. etc.
1468 * It guarantees that every node will be traversed,
1469 * and that it will be traversed only once.
1471 * Callback function w->func may return:
1472 * 0 -> continue walking.
1473 * positive value -> walking is suspended (used by tree dumps,
1474 * and probably by gc, if it will be split to several slices)
1475 * negative value -> terminate walking.
1477 * The function itself returns:
1478 * 0 -> walk is complete.
1479 * >0 -> walk is incomplete (i.e. suspended)
1480 * <0 -> walk is terminated by an error.
1483 static int fib6_walk_continue(struct fib6_walker *w)
1485 struct fib6_node *fn, *pn;
1492 if (w->prune && fn != w->root &&
1493 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
1498 #ifdef CONFIG_IPV6_SUBTREES
1500 if (FIB6_SUBTREE(fn)) {
1501 w->node = FIB6_SUBTREE(fn);
1509 w->state = FWS_INIT;
1515 w->node = fn->right;
1516 w->state = FWS_INIT;
1522 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1544 #ifdef CONFIG_IPV6_SUBTREES
1545 if (FIB6_SUBTREE(pn) == fn) {
1546 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1551 if (pn->left == fn) {
1555 if (pn->right == fn) {
1557 w->leaf = w->node->leaf;
1567 static int fib6_walk(struct fib6_walker *w)
1571 w->state = FWS_INIT;
1574 fib6_walker_link(w);
1575 res = fib6_walk_continue(w);
1577 fib6_walker_unlink(w);
1581 static int fib6_clean_node(struct fib6_walker *w)
1584 struct rt6_info *rt;
1585 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1586 struct nl_info info = {
1590 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1591 w->node->fn_sernum != c->sernum)
1592 w->node->fn_sernum = c->sernum;
1595 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1600 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
1601 res = c->func(rt, c->arg);
1604 res = fib6_del(rt, &info);
1607 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1608 __func__, rt, rt->rt6i_node, res);
1621 * Convenient frontend to tree walker.
1623 * func is called on each route.
1624 * It may return -1 -> delete this route.
1625 * 0 -> continue walking
1627 * prune==1 -> only immediate children of node (certainly,
1628 * ignoring pure split nodes) will be scanned.
1631 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
1632 int (*func)(struct rt6_info *, void *arg),
1633 bool prune, int sernum, void *arg)
1635 struct fib6_cleaner c;
1638 c.w.func = fib6_clean_node;
1650 static void __fib6_clean_all(struct net *net,
1651 int (*func)(struct rt6_info *, void *),
1652 int sernum, void *arg)
1654 struct fib6_table *table;
1655 struct hlist_head *head;
1659 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
1660 head = &net->ipv6.fib_table_hash[h];
1661 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
1662 write_lock_bh(&table->tb6_lock);
1663 fib6_clean_tree(net, &table->tb6_root,
1664 func, false, sernum, arg);
1665 write_unlock_bh(&table->tb6_lock);
1671 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1674 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1677 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1679 if (rt->rt6i_flags & RTF_CACHE) {
1680 RT6_TRACE("pruning clone %p\n", rt);
1687 static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
1689 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1690 FIB6_NO_SERNUM_CHANGE, NULL);
1693 static void fib6_flush_trees(struct net *net)
1695 int new_sernum = fib6_new_sernum(net);
1697 __fib6_clean_all(net, NULL, new_sernum, NULL);
1701 * Garbage collection
1704 static struct fib6_gc_args
1710 static int fib6_age(struct rt6_info *rt, void *arg)
1712 unsigned long now = jiffies;
1715 * check addrconf expiration here.
1716 * Routes are expired even if they are in use.
1718 * Also age clones. Note, that clones are aged out
1719 * only if they are not in use now.
1722 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1723 if (time_after(now, rt->dst.expires)) {
1724 RT6_TRACE("expiring %p\n", rt);
1728 } else if (rt->rt6i_flags & RTF_CACHE) {
1729 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1730 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
1731 RT6_TRACE("aging clone %p\n", rt);
1733 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1734 struct neighbour *neigh;
1735 __u8 neigh_flags = 0;
1737 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1739 neigh_flags = neigh->flags;
1740 neigh_release(neigh);
1742 if (!(neigh_flags & NTF_ROUTER)) {
1743 RT6_TRACE("purging route %p via non-router but gateway\n",
1754 static DEFINE_SPINLOCK(fib6_gc_lock);
1756 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
1761 spin_lock_bh(&fib6_gc_lock);
1762 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1763 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1766 gc_args.timeout = expires ? (int)expires :
1767 net->ipv6.sysctl.ip6_rt_gc_interval;
1769 gc_args.more = icmp6_dst_gc();
1771 fib6_clean_all(net, fib6_age, NULL);
1773 net->ipv6.ip6_rt_last_gc = now;
1776 mod_timer(&net->ipv6.ip6_fib_timer,
1778 + net->ipv6.sysctl.ip6_rt_gc_interval));
1780 del_timer(&net->ipv6.ip6_fib_timer);
1781 spin_unlock_bh(&fib6_gc_lock);
1784 static void fib6_gc_timer_cb(unsigned long arg)
1786 fib6_run_gc(0, (struct net *)arg, true);
1789 static int __net_init fib6_net_init(struct net *net)
1791 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1793 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
1795 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1796 if (!net->ipv6.rt6_stats)
1799 /* Avoid false sharing : Use at least a full cache line */
1800 size = max_t(size_t, size, L1_CACHE_BYTES);
1802 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
1803 if (!net->ipv6.fib_table_hash)
1806 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1808 if (!net->ipv6.fib6_main_tbl)
1809 goto out_fib_table_hash;
1811 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
1812 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1813 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1814 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1815 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
1817 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1818 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1820 if (!net->ipv6.fib6_local_tbl)
1821 goto out_fib6_main_tbl;
1822 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
1823 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1824 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1825 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1826 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
1828 fib6_tables_init(net);
1832 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1834 kfree(net->ipv6.fib6_main_tbl);
1837 kfree(net->ipv6.fib_table_hash);
1839 kfree(net->ipv6.rt6_stats);
1844 static void fib6_net_exit(struct net *net)
1846 rt6_ifdown(net, NULL);
1847 del_timer_sync(&net->ipv6.ip6_fib_timer);
1849 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1850 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
1851 kfree(net->ipv6.fib6_local_tbl);
1853 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
1854 kfree(net->ipv6.fib6_main_tbl);
1855 kfree(net->ipv6.fib_table_hash);
1856 kfree(net->ipv6.rt6_stats);
1859 static struct pernet_operations fib6_net_ops = {
1860 .init = fib6_net_init,
1861 .exit = fib6_net_exit,
1864 int __init fib6_init(void)
1868 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1869 sizeof(struct fib6_node),
1870 0, SLAB_HWCACHE_ALIGN,
1872 if (!fib6_node_kmem)
1875 ret = register_pernet_subsys(&fib6_net_ops);
1877 goto out_kmem_cache_create;
1879 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1882 goto out_unregister_subsys;
1884 __fib6_flush_trees = fib6_flush_trees;
1888 out_unregister_subsys:
1889 unregister_pernet_subsys(&fib6_net_ops);
1890 out_kmem_cache_create:
1891 kmem_cache_destroy(fib6_node_kmem);
1895 void fib6_gc_cleanup(void)
1897 unregister_pernet_subsys(&fib6_net_ops);
1898 kmem_cache_destroy(fib6_node_kmem);
1901 #ifdef CONFIG_PROC_FS
1903 struct ipv6_route_iter {
1904 struct seq_net_private p;
1905 struct fib6_walker w;
1907 struct fib6_table *tbl;
1911 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1913 struct rt6_info *rt = v;
1914 struct ipv6_route_iter *iter = seq->private;
1916 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1918 #ifdef CONFIG_IPV6_SUBTREES
1919 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1921 seq_puts(seq, "00000000000000000000000000000000 00 ");
1923 if (rt->rt6i_flags & RTF_GATEWAY)
1924 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1926 seq_puts(seq, "00000000000000000000000000000000");
1928 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1929 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1930 rt->dst.__use, rt->rt6i_flags,
1931 rt->dst.dev ? rt->dst.dev->name : "");
1932 iter->w.leaf = NULL;
1936 static int ipv6_route_yield(struct fib6_walker *w)
1938 struct ipv6_route_iter *iter = w->args;
1944 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1946 if (!iter->skip && iter->w.leaf)
1948 } while (iter->w.leaf);
1953 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1955 memset(&iter->w, 0, sizeof(iter->w));
1956 iter->w.func = ipv6_route_yield;
1957 iter->w.root = &iter->tbl->tb6_root;
1958 iter->w.state = FWS_INIT;
1959 iter->w.node = iter->w.root;
1960 iter->w.args = iter;
1961 iter->sernum = iter->w.root->fn_sernum;
1962 INIT_LIST_HEAD(&iter->w.lh);
1963 fib6_walker_link(&iter->w);
1966 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1970 struct hlist_node *node;
1973 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1974 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1980 while (!node && h < FIB6_TABLE_HASHSZ) {
1981 node = rcu_dereference_bh(
1982 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1984 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1987 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1989 if (iter->sernum != iter->w.root->fn_sernum) {
1990 iter->sernum = iter->w.root->fn_sernum;
1991 iter->w.state = FWS_INIT;
1992 iter->w.node = iter->w.root;
1993 WARN_ON(iter->w.skip);
1994 iter->w.skip = iter->w.count;
1998 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2002 struct net *net = seq_file_net(seq);
2003 struct ipv6_route_iter *iter = seq->private;
2008 n = ((struct rt6_info *)v)->dst.rt6_next;
2015 ipv6_route_check_sernum(iter);
2016 read_lock(&iter->tbl->tb6_lock);
2017 r = fib6_walk_continue(&iter->w);
2018 read_unlock(&iter->tbl->tb6_lock);
2022 return iter->w.leaf;
2024 fib6_walker_unlink(&iter->w);
2027 fib6_walker_unlink(&iter->w);
2029 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2033 ipv6_route_seq_setup_walk(iter);
2037 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2040 struct net *net = seq_file_net(seq);
2041 struct ipv6_route_iter *iter = seq->private;
2044 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2048 ipv6_route_seq_setup_walk(iter);
2049 return ipv6_route_seq_next(seq, NULL, pos);
2055 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2057 struct fib6_walker *w = &iter->w;
2058 return w->node && !(w->state == FWS_U && w->node == w->root);
2061 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2064 struct ipv6_route_iter *iter = seq->private;
2066 if (ipv6_route_iter_active(iter))
2067 fib6_walker_unlink(&iter->w);
2069 rcu_read_unlock_bh();
2072 static const struct seq_operations ipv6_route_seq_ops = {
2073 .start = ipv6_route_seq_start,
2074 .next = ipv6_route_seq_next,
2075 .stop = ipv6_route_seq_stop,
2076 .show = ipv6_route_seq_show
2079 int ipv6_route_open(struct inode *inode, struct file *file)
2081 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2082 sizeof(struct ipv6_route_iter));
2085 #endif /* CONFIG_PROC_FS */