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);
56 static DEFINE_RWLOCK(fib6_walker_lock);
58 #ifdef CONFIG_IPV6_SUBTREES
59 #define FWS_INIT FWS_S
61 #define FWS_INIT FWS_L
64 static void fib6_prune_clones(struct net *net, struct fib6_node *fn);
65 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
66 static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
67 static int fib6_walk(struct fib6_walker *w);
68 static int fib6_walk_continue(struct fib6_walker *w);
71 * A routing update causes an increase of the serial number on the
72 * affected subtree. This allows for cached routes to be asynchronously
73 * tested when modifications are made to the destination cache as a
74 * result of redirects, path MTU changes, etc.
77 static void fib6_gc_timer_cb(unsigned long arg);
79 static LIST_HEAD(fib6_walkers);
80 #define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
82 static void fib6_walker_link(struct fib6_walker *w)
84 write_lock_bh(&fib6_walker_lock);
85 list_add(&w->lh, &fib6_walkers);
86 write_unlock_bh(&fib6_walker_lock);
89 static void fib6_walker_unlink(struct fib6_walker *w)
91 write_lock_bh(&fib6_walker_lock);
93 write_unlock_bh(&fib6_walker_lock);
96 static int fib6_new_sernum(struct net *net)
101 old = atomic_read(&net->ipv6.fib6_sernum);
102 new = old < INT_MAX ? old + 1 : 1;
103 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
109 * Auxiliary address test functions for the radix tree.
111 * These assume a 32bit processor (although it will work on
118 #if defined(__LITTLE_ENDIAN)
119 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
121 # define BITOP_BE32_SWIZZLE 0
124 static __be32 addr_bit_set(const void *token, int fn_bit)
126 const __be32 *addr = token;
129 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
130 * is optimized version of
131 * htonl(1 << ((~fn_bit)&0x1F))
132 * See include/asm-generic/bitops/le.h.
134 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
138 static struct fib6_node *node_alloc(void)
140 struct fib6_node *fn;
142 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
147 static void node_free(struct fib6_node *fn)
149 kmem_cache_free(fib6_node_kmem, fn);
152 static void rt6_release(struct rt6_info *rt)
154 if (atomic_dec_and_test(&rt->rt6i_ref))
158 static void fib6_link_table(struct net *net, struct fib6_table *tb)
163 * Initialize table lock at a single place to give lockdep a key,
164 * tables aren't visible prior to being linked to the list.
166 rwlock_init(&tb->tb6_lock);
168 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
171 * No protection necessary, this is the only list mutatation
172 * operation, tables never disappear once they exist.
174 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
177 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
179 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
181 struct fib6_table *table;
183 table = kzalloc(sizeof(*table), GFP_ATOMIC);
186 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
187 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
188 inet_peer_base_init(&table->tb6_peers);
194 struct fib6_table *fib6_new_table(struct net *net, u32 id)
196 struct fib6_table *tb;
200 tb = fib6_get_table(net, id);
204 tb = fib6_alloc_table(net, id);
206 fib6_link_table(net, tb);
211 struct fib6_table *fib6_get_table(struct net *net, u32 id)
213 struct fib6_table *tb;
214 struct hlist_head *head;
219 h = id & (FIB6_TABLE_HASHSZ - 1);
221 head = &net->ipv6.fib_table_hash[h];
222 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
223 if (tb->tb6_id == id) {
233 static void __net_init fib6_tables_init(struct net *net)
235 fib6_link_table(net, net->ipv6.fib6_main_tbl);
236 fib6_link_table(net, net->ipv6.fib6_local_tbl);
240 struct fib6_table *fib6_new_table(struct net *net, u32 id)
242 return fib6_get_table(net, id);
245 struct fib6_table *fib6_get_table(struct net *net, u32 id)
247 return net->ipv6.fib6_main_tbl;
250 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
251 int flags, pol_lookup_t lookup)
253 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
256 static void __net_init fib6_tables_init(struct net *net)
258 fib6_link_table(net, net->ipv6.fib6_main_tbl);
263 static int fib6_dump_node(struct fib6_walker *w)
268 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
269 res = rt6_dump_route(rt, w->args);
271 /* Frame is full, suspend walking */
281 static void fib6_dump_end(struct netlink_callback *cb)
283 struct fib6_walker *w = (void *)cb->args[2];
288 fib6_walker_unlink(w);
293 cb->done = (void *)cb->args[3];
297 static int fib6_dump_done(struct netlink_callback *cb)
300 return cb->done ? cb->done(cb) : 0;
303 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
304 struct netlink_callback *cb)
306 struct fib6_walker *w;
309 w = (void *)cb->args[2];
310 w->root = &table->tb6_root;
312 if (cb->args[4] == 0) {
316 read_lock_bh(&table->tb6_lock);
318 read_unlock_bh(&table->tb6_lock);
321 cb->args[5] = w->root->fn_sernum;
324 if (cb->args[5] != w->root->fn_sernum) {
325 /* Begin at the root if the tree changed */
326 cb->args[5] = w->root->fn_sernum;
333 read_lock_bh(&table->tb6_lock);
334 res = fib6_walk_continue(w);
335 read_unlock_bh(&table->tb6_lock);
337 fib6_walker_unlink(w);
345 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
347 struct net *net = sock_net(skb->sk);
349 unsigned int e = 0, s_e;
350 struct rt6_rtnl_dump_arg arg;
351 struct fib6_walker *w;
352 struct fib6_table *tb;
353 struct hlist_head *head;
359 w = (void *)cb->args[2];
363 * 1. hook callback destructor.
365 cb->args[3] = (long)cb->done;
366 cb->done = fib6_dump_done;
369 * 2. allocate and initialize walker.
371 w = kzalloc(sizeof(*w), GFP_ATOMIC);
374 w->func = fib6_dump_node;
375 cb->args[2] = (long)w;
384 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
386 head = &net->ipv6.fib_table_hash[h];
387 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
390 res = fib6_dump_table(tb, skb, cb);
402 res = res < 0 ? res : skb->len;
411 * return the appropriate node for a routing tree "add" operation
412 * by either creating and inserting or by returning an existing
416 static struct fib6_node *fib6_add_1(struct fib6_node *root,
417 struct in6_addr *addr, int plen,
418 int offset, int allow_create,
419 int replace_required, int sernum)
421 struct fib6_node *fn, *in, *ln;
422 struct fib6_node *pn = NULL;
427 RT6_TRACE("fib6_add_1\n");
429 /* insert node in tree */
434 key = (struct rt6key *)((u8 *)fn->leaf + offset);
439 if (plen < fn->fn_bit ||
440 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
442 if (replace_required) {
443 pr_warn("Can't replace route, no match found\n");
444 return ERR_PTR(-ENOENT);
446 pr_warn("NLM_F_CREATE should be set when creating new route\n");
455 if (plen == fn->fn_bit) {
456 /* clean up an intermediate node */
457 if (!(fn->fn_flags & RTN_RTINFO)) {
458 rt6_release(fn->leaf);
462 fn->fn_sernum = sernum;
468 * We have more bits to go
471 /* Try to walk down on tree. */
472 fn->fn_sernum = sernum;
473 dir = addr_bit_set(addr, fn->fn_bit);
475 fn = dir ? fn->right : fn->left;
479 /* We should not create new node because
480 * NLM_F_REPLACE was specified without NLM_F_CREATE
481 * I assume it is safe to require NLM_F_CREATE when
482 * REPLACE flag is used! Later we may want to remove the
483 * check for replace_required, because according
484 * to netlink specification, NLM_F_CREATE
485 * MUST be specified if new route is created.
486 * That would keep IPv6 consistent with IPv4
488 if (replace_required) {
489 pr_warn("Can't replace route, no match found\n");
490 return ERR_PTR(-ENOENT);
492 pr_warn("NLM_F_CREATE should be set when creating new route\n");
495 * We walked to the bottom of tree.
496 * Create new leaf node without children.
502 return ERR_PTR(-ENOMEM);
506 ln->fn_sernum = sernum;
518 * split since we don't have a common prefix anymore or
519 * we have a less significant route.
520 * we've to insert an intermediate node on the list
521 * this new node will point to the one we need to create
527 /* find 1st bit in difference between the 2 addrs.
529 See comment in __ipv6_addr_diff: bit may be an invalid value,
530 but if it is >= plen, the value is ignored in any case.
533 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
538 * (new leaf node)[ln] (old node)[fn]
549 return ERR_PTR(-ENOMEM);
553 * new intermediate node.
555 * be off since that an address that chooses one of
556 * the branches would not match less specific routes
557 * in the other branch
564 atomic_inc(&in->leaf->rt6i_ref);
566 in->fn_sernum = sernum;
568 /* update parent pointer */
579 ln->fn_sernum = sernum;
581 if (addr_bit_set(addr, bit)) {
588 } else { /* plen <= bit */
591 * (new leaf node)[ln]
593 * (old node)[fn] NULL
599 return ERR_PTR(-ENOMEM);
605 ln->fn_sernum = sernum;
612 if (addr_bit_set(&key->addr, plen))
622 static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
624 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
628 static int fib6_commit_metrics(struct dst_entry *dst,
629 struct nlattr *mx, int mx_len)
635 if (dst->flags & DST_HOST) {
636 mp = dst_metrics_write_ptr(dst);
638 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_ATOMIC);
641 dst_init_metrics(dst, mp, 0);
644 nla_for_each_attr(nla, mx, mx_len, remaining) {
645 int type = nla_type(nla);
651 mp[type - 1] = nla_get_u32(nla);
658 * Insert routing information in a node.
661 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
662 struct nl_info *info, struct nlattr *mx, int mx_len)
664 struct rt6_info *iter = NULL;
665 struct rt6_info **ins;
666 int replace = (info->nlh &&
667 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
668 int add = (!info->nlh ||
669 (info->nlh->nlmsg_flags & NLM_F_CREATE));
671 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
676 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
678 * Search for duplicates
681 if (iter->rt6i_metric == rt->rt6i_metric) {
683 * Same priority level
686 (info->nlh->nlmsg_flags & NLM_F_EXCL))
693 if (iter->dst.dev == rt->dst.dev &&
694 iter->rt6i_idev == rt->rt6i_idev &&
695 ipv6_addr_equal(&iter->rt6i_gateway,
696 &rt->rt6i_gateway)) {
697 if (rt->rt6i_nsiblings)
698 rt->rt6i_nsiblings = 0;
699 if (!(iter->rt6i_flags & RTF_EXPIRES))
701 if (!(rt->rt6i_flags & RTF_EXPIRES))
702 rt6_clean_expires(iter);
704 rt6_set_expires(iter, rt->dst.expires);
707 /* If we have the same destination and the same metric,
708 * but not the same gateway, then the route we try to
709 * add is sibling to this route, increment our counter
710 * of siblings, and later we will add our route to the
712 * Only static routes (which don't have flag
713 * RTF_EXPIRES) are used for ECMPv6.
715 * To avoid long list, we only had siblings if the
716 * route have a gateway.
719 rt6_qualify_for_ecmp(iter))
720 rt->rt6i_nsiblings++;
723 if (iter->rt6i_metric > rt->rt6i_metric)
726 ins = &iter->dst.rt6_next;
729 /* Reset round-robin state, if necessary */
730 if (ins == &fn->leaf)
733 /* Link this route to others same route. */
734 if (rt->rt6i_nsiblings) {
735 unsigned int rt6i_nsiblings;
736 struct rt6_info *sibling, *temp_sibling;
738 /* Find the first route that have the same metric */
741 if (sibling->rt6i_metric == rt->rt6i_metric &&
742 rt6_qualify_for_ecmp(sibling)) {
743 list_add_tail(&rt->rt6i_siblings,
744 &sibling->rt6i_siblings);
747 sibling = sibling->dst.rt6_next;
749 /* For each sibling in the list, increment the counter of
750 * siblings. BUG() if counters does not match, list of siblings
754 list_for_each_entry_safe(sibling, temp_sibling,
755 &rt->rt6i_siblings, rt6i_siblings) {
756 sibling->rt6i_nsiblings++;
757 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
760 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
768 pr_warn("NLM_F_CREATE should be set when creating new route\n");
772 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
776 rt->dst.rt6_next = iter;
779 atomic_inc(&rt->rt6i_ref);
780 inet6_rt_notify(RTM_NEWROUTE, rt, info);
781 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
783 if (!(fn->fn_flags & RTN_RTINFO)) {
784 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
785 fn->fn_flags |= RTN_RTINFO;
792 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
796 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
802 rt->dst.rt6_next = iter->dst.rt6_next;
803 atomic_inc(&rt->rt6i_ref);
804 inet6_rt_notify(RTM_NEWROUTE, rt, info);
806 if (!(fn->fn_flags & RTN_RTINFO)) {
807 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
808 fn->fn_flags |= RTN_RTINFO;
815 static void fib6_start_gc(struct net *net, struct rt6_info *rt)
817 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
818 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
819 mod_timer(&net->ipv6.ip6_fib_timer,
820 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
823 void fib6_force_start_gc(struct net *net)
825 if (!timer_pending(&net->ipv6.ip6_fib_timer))
826 mod_timer(&net->ipv6.ip6_fib_timer,
827 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
831 * Add routing information to the routing tree.
832 * <destination addr>/<source addr>
833 * with source addr info in sub-trees
836 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
837 struct nlattr *mx, int mx_len)
839 struct fib6_node *fn, *pn = NULL;
841 int allow_create = 1;
842 int replace_required = 0;
843 int sernum = fib6_new_sernum(info->nl_net);
846 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
848 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
849 replace_required = 1;
851 if (!allow_create && !replace_required)
852 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
854 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
855 offsetof(struct rt6_info, rt6i_dst), allow_create,
856 replace_required, sernum);
865 #ifdef CONFIG_IPV6_SUBTREES
866 if (rt->rt6i_src.plen) {
867 struct fib6_node *sn;
870 struct fib6_node *sfn;
882 /* Create subtree root node */
887 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
888 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
889 sfn->fn_flags = RTN_ROOT;
890 sfn->fn_sernum = sernum;
892 /* Now add the first leaf node to new subtree */
894 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
896 offsetof(struct rt6_info, rt6i_src),
897 allow_create, replace_required, sernum);
900 /* If it is failed, discard just allocated
901 root, and then (in st_failure) stale node
909 /* Now link new subtree to main tree */
913 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
915 offsetof(struct rt6_info, rt6i_src),
916 allow_create, replace_required, sernum);
926 atomic_inc(&rt->rt6i_ref);
932 err = fib6_add_rt2node(fn, rt, info, mx, mx_len);
934 fib6_start_gc(info->nl_net, rt);
935 if (!(rt->rt6i_flags & RTF_CACHE))
936 fib6_prune_clones(info->nl_net, pn);
941 #ifdef CONFIG_IPV6_SUBTREES
943 * If fib6_add_1 has cleared the old leaf pointer in the
944 * super-tree leaf node we have to find a new one for it.
946 if (pn != fn && pn->leaf == rt) {
948 atomic_dec(&rt->rt6i_ref);
950 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
951 pn->leaf = fib6_find_prefix(info->nl_net, pn);
954 WARN_ON(pn->leaf == NULL);
955 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
958 atomic_inc(&pn->leaf->rt6i_ref);
965 #ifdef CONFIG_IPV6_SUBTREES
966 /* Subtree creation failed, probably main tree node
967 is orphan. If it is, shoot it.
970 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
971 fib6_repair_tree(info->nl_net, fn);
978 * Routing tree lookup
983 int offset; /* key offset on rt6_info */
984 const struct in6_addr *addr; /* search key */
987 static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
988 struct lookup_args *args)
990 struct fib6_node *fn;
993 if (unlikely(args->offset == 0))
1003 struct fib6_node *next;
1005 dir = addr_bit_set(args->addr, fn->fn_bit);
1007 next = dir ? fn->right : fn->left;
1017 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
1020 key = (struct rt6key *) ((u8 *) fn->leaf +
1023 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1024 #ifdef CONFIG_IPV6_SUBTREES
1026 struct fib6_node *sfn;
1027 sfn = fib6_lookup_1(fn->subtree,
1034 if (fn->fn_flags & RTN_RTINFO)
1038 #ifdef CONFIG_IPV6_SUBTREES
1041 if (fn->fn_flags & RTN_ROOT)
1050 struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1051 const struct in6_addr *saddr)
1053 struct fib6_node *fn;
1054 struct lookup_args args[] = {
1056 .offset = offsetof(struct rt6_info, rt6i_dst),
1059 #ifdef CONFIG_IPV6_SUBTREES
1061 .offset = offsetof(struct rt6_info, rt6i_src),
1066 .offset = 0, /* sentinel */
1070 fn = fib6_lookup_1(root, daddr ? args : args + 1);
1071 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1078 * Get node with specified destination prefix (and source prefix,
1079 * if subtrees are used)
1083 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1084 const struct in6_addr *addr,
1085 int plen, int offset)
1087 struct fib6_node *fn;
1089 for (fn = root; fn ; ) {
1090 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1095 if (plen < fn->fn_bit ||
1096 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1099 if (plen == fn->fn_bit)
1103 * We have more bits to go
1105 if (addr_bit_set(addr, fn->fn_bit))
1113 struct fib6_node *fib6_locate(struct fib6_node *root,
1114 const struct in6_addr *daddr, int dst_len,
1115 const struct in6_addr *saddr, int src_len)
1117 struct fib6_node *fn;
1119 fn = fib6_locate_1(root, daddr, dst_len,
1120 offsetof(struct rt6_info, rt6i_dst));
1122 #ifdef CONFIG_IPV6_SUBTREES
1124 WARN_ON(saddr == NULL);
1125 if (fn && fn->subtree)
1126 fn = fib6_locate_1(fn->subtree, saddr, src_len,
1127 offsetof(struct rt6_info, rt6i_src));
1131 if (fn && fn->fn_flags & RTN_RTINFO)
1143 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
1145 if (fn->fn_flags & RTN_ROOT)
1146 return net->ipv6.ip6_null_entry;
1150 return fn->left->leaf;
1152 return fn->right->leaf;
1154 fn = FIB6_SUBTREE(fn);
1160 * Called to trim the tree of intermediate nodes when possible. "fn"
1161 * is the node we want to try and remove.
1164 static struct fib6_node *fib6_repair_tree(struct net *net,
1165 struct fib6_node *fn)
1169 struct fib6_node *child, *pn;
1170 struct fib6_walker *w;
1174 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1177 WARN_ON(fn->fn_flags & RTN_RTINFO);
1178 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1179 WARN_ON(fn->leaf != NULL);
1184 child = fn->right, children |= 1;
1186 child = fn->left, children |= 2;
1188 if (children == 3 || FIB6_SUBTREE(fn)
1189 #ifdef CONFIG_IPV6_SUBTREES
1190 /* Subtree root (i.e. fn) may have one child */
1191 || (children && fn->fn_flags & RTN_ROOT)
1194 fn->leaf = fib6_find_prefix(net, fn);
1198 fn->leaf = net->ipv6.ip6_null_entry;
1201 atomic_inc(&fn->leaf->rt6i_ref);
1206 #ifdef CONFIG_IPV6_SUBTREES
1207 if (FIB6_SUBTREE(pn) == fn) {
1208 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1209 FIB6_SUBTREE(pn) = NULL;
1212 WARN_ON(fn->fn_flags & RTN_ROOT);
1214 if (pn->right == fn)
1216 else if (pn->left == fn)
1225 #ifdef CONFIG_IPV6_SUBTREES
1229 read_lock(&fib6_walker_lock);
1232 if (w->root == fn) {
1233 w->root = w->node = NULL;
1234 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1235 } else if (w->node == fn) {
1236 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1241 if (w->root == fn) {
1243 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1245 if (w->node == fn) {
1248 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1249 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1251 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1252 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1257 read_unlock(&fib6_walker_lock);
1260 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1263 rt6_release(pn->leaf);
1269 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
1270 struct nl_info *info)
1272 struct fib6_walker *w;
1273 struct rt6_info *rt = *rtp;
1274 struct net *net = info->nl_net;
1276 RT6_TRACE("fib6_del_route\n");
1279 *rtp = rt->dst.rt6_next;
1280 rt->rt6i_node = NULL;
1281 net->ipv6.rt6_stats->fib_rt_entries--;
1282 net->ipv6.rt6_stats->fib_discarded_routes++;
1284 /* Reset round-robin state, if necessary */
1285 if (fn->rr_ptr == rt)
1288 /* Remove this entry from other siblings */
1289 if (rt->rt6i_nsiblings) {
1290 struct rt6_info *sibling, *next_sibling;
1292 list_for_each_entry_safe(sibling, next_sibling,
1293 &rt->rt6i_siblings, rt6i_siblings)
1294 sibling->rt6i_nsiblings--;
1295 rt->rt6i_nsiblings = 0;
1296 list_del_init(&rt->rt6i_siblings);
1299 /* Adjust walkers */
1300 read_lock(&fib6_walker_lock);
1302 if (w->state == FWS_C && w->leaf == rt) {
1303 RT6_TRACE("walker %p adjusted by delroute\n", w);
1304 w->leaf = rt->dst.rt6_next;
1309 read_unlock(&fib6_walker_lock);
1311 rt->dst.rt6_next = NULL;
1313 /* If it was last route, expunge its radix tree node */
1315 fn->fn_flags &= ~RTN_RTINFO;
1316 net->ipv6.rt6_stats->fib_route_nodes--;
1317 fn = fib6_repair_tree(net, fn);
1320 if (atomic_read(&rt->rt6i_ref) != 1) {
1321 /* This route is used as dummy address holder in some split
1322 * nodes. It is not leaked, but it still holds other resources,
1323 * which must be released in time. So, scan ascendant nodes
1324 * and replace dummy references to this route with references
1325 * to still alive ones.
1328 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
1329 fn->leaf = fib6_find_prefix(net, fn);
1330 atomic_inc(&fn->leaf->rt6i_ref);
1335 /* No more references are possible at this point. */
1336 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
1339 inet6_rt_notify(RTM_DELROUTE, rt, info);
1343 int fib6_del(struct rt6_info *rt, struct nl_info *info)
1345 struct net *net = info->nl_net;
1346 struct fib6_node *fn = rt->rt6i_node;
1347 struct rt6_info **rtp;
1350 if (rt->dst.obsolete > 0) {
1351 WARN_ON(fn != NULL);
1355 if (!fn || rt == net->ipv6.ip6_null_entry)
1358 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1360 if (!(rt->rt6i_flags & RTF_CACHE)) {
1361 struct fib6_node *pn = fn;
1362 #ifdef CONFIG_IPV6_SUBTREES
1363 /* clones of this route might be in another subtree */
1364 if (rt->rt6i_src.plen) {
1365 while (!(pn->fn_flags & RTN_ROOT))
1370 fib6_prune_clones(info->nl_net, pn);
1374 * Walk the leaf entries looking for ourself
1377 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
1379 fib6_del_route(fn, rtp, info);
1387 * Tree traversal function.
1389 * Certainly, it is not interrupt safe.
1390 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1391 * It means, that we can modify tree during walking
1392 * and use this function for garbage collection, clone pruning,
1393 * cleaning tree when a device goes down etc. etc.
1395 * It guarantees that every node will be traversed,
1396 * and that it will be traversed only once.
1398 * Callback function w->func may return:
1399 * 0 -> continue walking.
1400 * positive value -> walking is suspended (used by tree dumps,
1401 * and probably by gc, if it will be split to several slices)
1402 * negative value -> terminate walking.
1404 * The function itself returns:
1405 * 0 -> walk is complete.
1406 * >0 -> walk is incomplete (i.e. suspended)
1407 * <0 -> walk is terminated by an error.
1410 static int fib6_walk_continue(struct fib6_walker *w)
1412 struct fib6_node *fn, *pn;
1419 if (w->prune && fn != w->root &&
1420 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
1425 #ifdef CONFIG_IPV6_SUBTREES
1427 if (FIB6_SUBTREE(fn)) {
1428 w->node = FIB6_SUBTREE(fn);
1436 w->state = FWS_INIT;
1442 w->node = fn->right;
1443 w->state = FWS_INIT;
1449 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1471 #ifdef CONFIG_IPV6_SUBTREES
1472 if (FIB6_SUBTREE(pn) == fn) {
1473 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1478 if (pn->left == fn) {
1482 if (pn->right == fn) {
1484 w->leaf = w->node->leaf;
1494 static int fib6_walk(struct fib6_walker *w)
1498 w->state = FWS_INIT;
1501 fib6_walker_link(w);
1502 res = fib6_walk_continue(w);
1504 fib6_walker_unlink(w);
1508 static int fib6_clean_node(struct fib6_walker *w)
1511 struct rt6_info *rt;
1512 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1513 struct nl_info info = {
1517 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
1518 res = c->func(rt, c->arg);
1521 res = fib6_del(rt, &info);
1524 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1525 __func__, rt, rt->rt6i_node, res);
1538 * Convenient frontend to tree walker.
1540 * func is called on each route.
1541 * It may return -1 -> delete this route.
1542 * 0 -> continue walking
1544 * prune==1 -> only immediate children of node (certainly,
1545 * ignoring pure split nodes) will be scanned.
1548 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
1549 int (*func)(struct rt6_info *, void *arg),
1550 bool prune, void *arg)
1552 struct fib6_cleaner c;
1555 c.w.func = fib6_clean_node;
1566 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
1569 struct fib6_table *table;
1570 struct hlist_head *head;
1574 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
1575 head = &net->ipv6.fib_table_hash[h];
1576 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
1577 write_lock_bh(&table->tb6_lock);
1578 fib6_clean_tree(net, &table->tb6_root,
1580 write_unlock_bh(&table->tb6_lock);
1586 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1588 if (rt->rt6i_flags & RTF_CACHE) {
1589 RT6_TRACE("pruning clone %p\n", rt);
1596 static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
1598 fib6_clean_tree(net, fn, fib6_prune_clone, true, NULL);
1601 static int fib6_update_sernum(struct rt6_info *rt, void *arg)
1603 int sernum = *(int *)arg;
1605 if (rt->rt6i_node &&
1606 rt->rt6i_node->fn_sernum != sernum)
1607 rt->rt6i_node->fn_sernum = sernum;
1612 static void fib6_flush_trees(struct net *net)
1614 int new_sernum = fib6_new_sernum(net);
1616 fib6_clean_all(net, fib6_update_sernum, &new_sernum);
1620 * Garbage collection
1623 static struct fib6_gc_args
1629 static int fib6_age(struct rt6_info *rt, void *arg)
1631 unsigned long now = jiffies;
1634 * check addrconf expiration here.
1635 * Routes are expired even if they are in use.
1637 * Also age clones. Note, that clones are aged out
1638 * only if they are not in use now.
1641 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1642 if (time_after(now, rt->dst.expires)) {
1643 RT6_TRACE("expiring %p\n", rt);
1647 } else if (rt->rt6i_flags & RTF_CACHE) {
1648 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1649 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
1650 RT6_TRACE("aging clone %p\n", rt);
1652 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1653 struct neighbour *neigh;
1654 __u8 neigh_flags = 0;
1656 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1658 neigh_flags = neigh->flags;
1659 neigh_release(neigh);
1661 if (!(neigh_flags & NTF_ROUTER)) {
1662 RT6_TRACE("purging route %p via non-router but gateway\n",
1673 static DEFINE_SPINLOCK(fib6_gc_lock);
1675 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
1680 spin_lock_bh(&fib6_gc_lock);
1681 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1682 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1685 gc_args.timeout = expires ? (int)expires :
1686 net->ipv6.sysctl.ip6_rt_gc_interval;
1688 gc_args.more = icmp6_dst_gc();
1690 fib6_clean_all(net, fib6_age, NULL);
1692 net->ipv6.ip6_rt_last_gc = now;
1695 mod_timer(&net->ipv6.ip6_fib_timer,
1697 + net->ipv6.sysctl.ip6_rt_gc_interval));
1699 del_timer(&net->ipv6.ip6_fib_timer);
1700 spin_unlock_bh(&fib6_gc_lock);
1703 static void fib6_gc_timer_cb(unsigned long arg)
1705 fib6_run_gc(0, (struct net *)arg, true);
1708 static int __net_init fib6_net_init(struct net *net)
1710 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1712 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
1714 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1715 if (!net->ipv6.rt6_stats)
1718 /* Avoid false sharing : Use at least a full cache line */
1719 size = max_t(size_t, size, L1_CACHE_BYTES);
1721 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
1722 if (!net->ipv6.fib_table_hash)
1725 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1727 if (!net->ipv6.fib6_main_tbl)
1728 goto out_fib_table_hash;
1730 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
1731 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1732 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1733 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1734 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
1736 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1737 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1739 if (!net->ipv6.fib6_local_tbl)
1740 goto out_fib6_main_tbl;
1741 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
1742 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1743 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1744 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1745 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
1747 fib6_tables_init(net);
1751 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1753 kfree(net->ipv6.fib6_main_tbl);
1756 kfree(net->ipv6.fib_table_hash);
1758 kfree(net->ipv6.rt6_stats);
1763 static void fib6_net_exit(struct net *net)
1765 rt6_ifdown(net, NULL);
1766 del_timer_sync(&net->ipv6.ip6_fib_timer);
1768 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1769 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
1770 kfree(net->ipv6.fib6_local_tbl);
1772 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
1773 kfree(net->ipv6.fib6_main_tbl);
1774 kfree(net->ipv6.fib_table_hash);
1775 kfree(net->ipv6.rt6_stats);
1778 static struct pernet_operations fib6_net_ops = {
1779 .init = fib6_net_init,
1780 .exit = fib6_net_exit,
1783 int __init fib6_init(void)
1787 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1788 sizeof(struct fib6_node),
1789 0, SLAB_HWCACHE_ALIGN,
1791 if (!fib6_node_kmem)
1794 ret = register_pernet_subsys(&fib6_net_ops);
1796 goto out_kmem_cache_create;
1798 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1801 goto out_unregister_subsys;
1803 __fib6_flush_trees = fib6_flush_trees;
1807 out_unregister_subsys:
1808 unregister_pernet_subsys(&fib6_net_ops);
1809 out_kmem_cache_create:
1810 kmem_cache_destroy(fib6_node_kmem);
1814 void fib6_gc_cleanup(void)
1816 unregister_pernet_subsys(&fib6_net_ops);
1817 kmem_cache_destroy(fib6_node_kmem);
1820 #ifdef CONFIG_PROC_FS
1822 struct ipv6_route_iter {
1823 struct seq_net_private p;
1824 struct fib6_walker w;
1826 struct fib6_table *tbl;
1830 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1832 struct rt6_info *rt = v;
1833 struct ipv6_route_iter *iter = seq->private;
1835 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1837 #ifdef CONFIG_IPV6_SUBTREES
1838 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1840 seq_puts(seq, "00000000000000000000000000000000 00 ");
1842 if (rt->rt6i_flags & RTF_GATEWAY)
1843 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1845 seq_puts(seq, "00000000000000000000000000000000");
1847 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1848 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1849 rt->dst.__use, rt->rt6i_flags,
1850 rt->dst.dev ? rt->dst.dev->name : "");
1851 iter->w.leaf = NULL;
1855 static int ipv6_route_yield(struct fib6_walker *w)
1857 struct ipv6_route_iter *iter = w->args;
1863 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1865 if (!iter->skip && iter->w.leaf)
1867 } while (iter->w.leaf);
1872 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1874 memset(&iter->w, 0, sizeof(iter->w));
1875 iter->w.func = ipv6_route_yield;
1876 iter->w.root = &iter->tbl->tb6_root;
1877 iter->w.state = FWS_INIT;
1878 iter->w.node = iter->w.root;
1879 iter->w.args = iter;
1880 iter->sernum = iter->w.root->fn_sernum;
1881 INIT_LIST_HEAD(&iter->w.lh);
1882 fib6_walker_link(&iter->w);
1885 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1889 struct hlist_node *node;
1892 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1893 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1899 while (!node && h < FIB6_TABLE_HASHSZ) {
1900 node = rcu_dereference_bh(
1901 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1903 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1906 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1908 if (iter->sernum != iter->w.root->fn_sernum) {
1909 iter->sernum = iter->w.root->fn_sernum;
1910 iter->w.state = FWS_INIT;
1911 iter->w.node = iter->w.root;
1912 WARN_ON(iter->w.skip);
1913 iter->w.skip = iter->w.count;
1917 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1921 struct net *net = seq_file_net(seq);
1922 struct ipv6_route_iter *iter = seq->private;
1927 n = ((struct rt6_info *)v)->dst.rt6_next;
1934 ipv6_route_check_sernum(iter);
1935 read_lock(&iter->tbl->tb6_lock);
1936 r = fib6_walk_continue(&iter->w);
1937 read_unlock(&iter->tbl->tb6_lock);
1941 return iter->w.leaf;
1943 fib6_walker_unlink(&iter->w);
1946 fib6_walker_unlink(&iter->w);
1948 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1952 ipv6_route_seq_setup_walk(iter);
1956 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1959 struct net *net = seq_file_net(seq);
1960 struct ipv6_route_iter *iter = seq->private;
1963 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1967 ipv6_route_seq_setup_walk(iter);
1968 return ipv6_route_seq_next(seq, NULL, pos);
1974 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1976 struct fib6_walker *w = &iter->w;
1977 return w->node && !(w->state == FWS_U && w->node == w->root);
1980 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
1983 struct ipv6_route_iter *iter = seq->private;
1985 if (ipv6_route_iter_active(iter))
1986 fib6_walker_unlink(&iter->w);
1988 rcu_read_unlock_bh();
1991 static const struct seq_operations ipv6_route_seq_ops = {
1992 .start = ipv6_route_seq_start,
1993 .next = ipv6_route_seq_next,
1994 .stop = ipv6_route_seq_stop,
1995 .show = ipv6_route_seq_show
1998 int ipv6_route_open(struct inode *inode, struct file *file)
2000 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2001 sizeof(struct ipv6_route_iter));
2004 #endif /* CONFIG_PROC_FS */