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 atomic_t rt_sernum = ATOMIC_INIT(1);
79 static void fib6_gc_timer_cb(unsigned long arg);
81 static LIST_HEAD(fib6_walkers);
82 #define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
84 static void fib6_walker_link(struct fib6_walker *w)
86 write_lock_bh(&fib6_walker_lock);
87 list_add(&w->lh, &fib6_walkers);
88 write_unlock_bh(&fib6_walker_lock);
91 static void fib6_walker_unlink(struct fib6_walker *w)
93 write_lock_bh(&fib6_walker_lock);
95 write_unlock_bh(&fib6_walker_lock);
98 static int fib6_new_sernum(void)
103 old = atomic_read(&rt_sernum);
104 new = old < INT_MAX ? old + 1 : 1;
105 } while (atomic_cmpxchg(&rt_sernum, old, new) != old);
110 * Auxiliary address test functions for the radix tree.
112 * These assume a 32bit processor (although it will work on
119 #if defined(__LITTLE_ENDIAN)
120 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
122 # define BITOP_BE32_SWIZZLE 0
125 static __be32 addr_bit_set(const void *token, int fn_bit)
127 const __be32 *addr = token;
130 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
131 * is optimized version of
132 * htonl(1 << ((~fn_bit)&0x1F))
133 * See include/asm-generic/bitops/le.h.
135 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
139 static struct fib6_node *node_alloc(void)
141 struct fib6_node *fn;
143 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
148 static void node_free(struct fib6_node *fn)
150 kmem_cache_free(fib6_node_kmem, fn);
153 static void rt6_release(struct rt6_info *rt)
155 if (atomic_dec_and_test(&rt->rt6i_ref))
159 static void fib6_link_table(struct net *net, struct fib6_table *tb)
164 * Initialize table lock at a single place to give lockdep a key,
165 * tables aren't visible prior to being linked to the list.
167 rwlock_init(&tb->tb6_lock);
169 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
172 * No protection necessary, this is the only list mutatation
173 * operation, tables never disappear once they exist.
175 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
178 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
180 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
182 struct fib6_table *table;
184 table = kzalloc(sizeof(*table), GFP_ATOMIC);
187 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
188 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
189 inet_peer_base_init(&table->tb6_peers);
195 struct fib6_table *fib6_new_table(struct net *net, u32 id)
197 struct fib6_table *tb;
201 tb = fib6_get_table(net, id);
205 tb = fib6_alloc_table(net, id);
207 fib6_link_table(net, tb);
212 struct fib6_table *fib6_get_table(struct net *net, u32 id)
214 struct fib6_table *tb;
215 struct hlist_head *head;
220 h = id & (FIB6_TABLE_HASHSZ - 1);
222 head = &net->ipv6.fib_table_hash[h];
223 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
224 if (tb->tb6_id == id) {
234 static void __net_init fib6_tables_init(struct net *net)
236 fib6_link_table(net, net->ipv6.fib6_main_tbl);
237 fib6_link_table(net, net->ipv6.fib6_local_tbl);
241 struct fib6_table *fib6_new_table(struct net *net, u32 id)
243 return fib6_get_table(net, id);
246 struct fib6_table *fib6_get_table(struct net *net, u32 id)
248 return net->ipv6.fib6_main_tbl;
251 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
252 int flags, pol_lookup_t lookup)
254 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
257 static void __net_init fib6_tables_init(struct net *net)
259 fib6_link_table(net, net->ipv6.fib6_main_tbl);
264 static int fib6_dump_node(struct fib6_walker *w)
269 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
270 res = rt6_dump_route(rt, w->args);
272 /* Frame is full, suspend walking */
282 static void fib6_dump_end(struct netlink_callback *cb)
284 struct fib6_walker *w = (void *)cb->args[2];
289 fib6_walker_unlink(w);
294 cb->done = (void *)cb->args[3];
298 static int fib6_dump_done(struct netlink_callback *cb)
301 return cb->done ? cb->done(cb) : 0;
304 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
305 struct netlink_callback *cb)
307 struct fib6_walker *w;
310 w = (void *)cb->args[2];
311 w->root = &table->tb6_root;
313 if (cb->args[4] == 0) {
317 read_lock_bh(&table->tb6_lock);
319 read_unlock_bh(&table->tb6_lock);
322 cb->args[5] = w->root->fn_sernum;
325 if (cb->args[5] != w->root->fn_sernum) {
326 /* Begin at the root if the tree changed */
327 cb->args[5] = w->root->fn_sernum;
334 read_lock_bh(&table->tb6_lock);
335 res = fib6_walk_continue(w);
336 read_unlock_bh(&table->tb6_lock);
338 fib6_walker_unlink(w);
346 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
348 struct net *net = sock_net(skb->sk);
350 unsigned int e = 0, s_e;
351 struct rt6_rtnl_dump_arg arg;
352 struct fib6_walker *w;
353 struct fib6_table *tb;
354 struct hlist_head *head;
360 w = (void *)cb->args[2];
364 * 1. hook callback destructor.
366 cb->args[3] = (long)cb->done;
367 cb->done = fib6_dump_done;
370 * 2. allocate and initialize walker.
372 w = kzalloc(sizeof(*w), GFP_ATOMIC);
375 w->func = fib6_dump_node;
376 cb->args[2] = (long)w;
385 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
387 head = &net->ipv6.fib_table_hash[h];
388 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
391 res = fib6_dump_table(tb, skb, cb);
403 res = res < 0 ? res : skb->len;
412 * return the appropriate node for a routing tree "add" operation
413 * by either creating and inserting or by returning an existing
417 static struct fib6_node *fib6_add_1(struct fib6_node *root,
418 struct in6_addr *addr, int plen,
419 int offset, int allow_create,
420 int replace_required, int sernum)
422 struct fib6_node *fn, *in, *ln;
423 struct fib6_node *pn = NULL;
428 RT6_TRACE("fib6_add_1\n");
430 /* insert node in tree */
435 key = (struct rt6key *)((u8 *)fn->leaf + offset);
440 if (plen < fn->fn_bit ||
441 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
443 if (replace_required) {
444 pr_warn("Can't replace route, no match found\n");
445 return ERR_PTR(-ENOENT);
447 pr_warn("NLM_F_CREATE should be set when creating new route\n");
456 if (plen == fn->fn_bit) {
457 /* clean up an intermediate node */
458 if (!(fn->fn_flags & RTN_RTINFO)) {
459 rt6_release(fn->leaf);
463 fn->fn_sernum = sernum;
469 * We have more bits to go
472 /* Try to walk down on tree. */
473 fn->fn_sernum = sernum;
474 dir = addr_bit_set(addr, fn->fn_bit);
476 fn = dir ? fn->right : fn->left;
480 /* We should not create new node because
481 * NLM_F_REPLACE was specified without NLM_F_CREATE
482 * I assume it is safe to require NLM_F_CREATE when
483 * REPLACE flag is used! Later we may want to remove the
484 * check for replace_required, because according
485 * to netlink specification, NLM_F_CREATE
486 * MUST be specified if new route is created.
487 * That would keep IPv6 consistent with IPv4
489 if (replace_required) {
490 pr_warn("Can't replace route, no match found\n");
491 return ERR_PTR(-ENOENT);
493 pr_warn("NLM_F_CREATE should be set when creating new route\n");
496 * We walked to the bottom of tree.
497 * Create new leaf node without children.
503 return ERR_PTR(-ENOMEM);
507 ln->fn_sernum = sernum;
519 * split since we don't have a common prefix anymore or
520 * we have a less significant route.
521 * we've to insert an intermediate node on the list
522 * this new node will point to the one we need to create
528 /* find 1st bit in difference between the 2 addrs.
530 See comment in __ipv6_addr_diff: bit may be an invalid value,
531 but if it is >= plen, the value is ignored in any case.
534 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
539 * (new leaf node)[ln] (old node)[fn]
550 return ERR_PTR(-ENOMEM);
554 * new intermediate node.
556 * be off since that an address that chooses one of
557 * the branches would not match less specific routes
558 * in the other branch
565 atomic_inc(&in->leaf->rt6i_ref);
567 in->fn_sernum = sernum;
569 /* update parent pointer */
580 ln->fn_sernum = sernum;
582 if (addr_bit_set(addr, bit)) {
589 } else { /* plen <= bit */
592 * (new leaf node)[ln]
594 * (old node)[fn] NULL
600 return ERR_PTR(-ENOMEM);
606 ln->fn_sernum = sernum;
613 if (addr_bit_set(&key->addr, plen))
623 static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
625 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
629 static int fib6_commit_metrics(struct dst_entry *dst,
630 struct nlattr *mx, int mx_len)
636 if (dst->flags & DST_HOST) {
637 mp = dst_metrics_write_ptr(dst);
639 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_ATOMIC);
642 dst_init_metrics(dst, mp, 0);
645 nla_for_each_attr(nla, mx, mx_len, remaining) {
646 int type = nla_type(nla);
652 mp[type - 1] = nla_get_u32(nla);
659 * Insert routing information in a node.
662 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
663 struct nl_info *info, struct nlattr *mx, int mx_len)
665 struct rt6_info *iter = NULL;
666 struct rt6_info **ins;
667 int replace = (info->nlh &&
668 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
669 int add = (!info->nlh ||
670 (info->nlh->nlmsg_flags & NLM_F_CREATE));
672 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
677 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
679 * Search for duplicates
682 if (iter->rt6i_metric == rt->rt6i_metric) {
684 * Same priority level
687 (info->nlh->nlmsg_flags & NLM_F_EXCL))
694 if (iter->dst.dev == rt->dst.dev &&
695 iter->rt6i_idev == rt->rt6i_idev &&
696 ipv6_addr_equal(&iter->rt6i_gateway,
697 &rt->rt6i_gateway)) {
698 if (rt->rt6i_nsiblings)
699 rt->rt6i_nsiblings = 0;
700 if (!(iter->rt6i_flags & RTF_EXPIRES))
702 if (!(rt->rt6i_flags & RTF_EXPIRES))
703 rt6_clean_expires(iter);
705 rt6_set_expires(iter, rt->dst.expires);
708 /* If we have the same destination and the same metric,
709 * but not the same gateway, then the route we try to
710 * add is sibling to this route, increment our counter
711 * of siblings, and later we will add our route to the
713 * Only static routes (which don't have flag
714 * RTF_EXPIRES) are used for ECMPv6.
716 * To avoid long list, we only had siblings if the
717 * route have a gateway.
720 rt6_qualify_for_ecmp(iter))
721 rt->rt6i_nsiblings++;
724 if (iter->rt6i_metric > rt->rt6i_metric)
727 ins = &iter->dst.rt6_next;
730 /* Reset round-robin state, if necessary */
731 if (ins == &fn->leaf)
734 /* Link this route to others same route. */
735 if (rt->rt6i_nsiblings) {
736 unsigned int rt6i_nsiblings;
737 struct rt6_info *sibling, *temp_sibling;
739 /* Find the first route that have the same metric */
742 if (sibling->rt6i_metric == rt->rt6i_metric &&
743 rt6_qualify_for_ecmp(sibling)) {
744 list_add_tail(&rt->rt6i_siblings,
745 &sibling->rt6i_siblings);
748 sibling = sibling->dst.rt6_next;
750 /* For each sibling in the list, increment the counter of
751 * siblings. BUG() if counters does not match, list of siblings
755 list_for_each_entry_safe(sibling, temp_sibling,
756 &rt->rt6i_siblings, rt6i_siblings) {
757 sibling->rt6i_nsiblings++;
758 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
761 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
769 pr_warn("NLM_F_CREATE should be set when creating new route\n");
773 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
777 rt->dst.rt6_next = iter;
780 atomic_inc(&rt->rt6i_ref);
781 inet6_rt_notify(RTM_NEWROUTE, rt, info);
782 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
784 if (!(fn->fn_flags & RTN_RTINFO)) {
785 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
786 fn->fn_flags |= RTN_RTINFO;
793 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
797 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
803 rt->dst.rt6_next = iter->dst.rt6_next;
804 atomic_inc(&rt->rt6i_ref);
805 inet6_rt_notify(RTM_NEWROUTE, rt, info);
807 if (!(fn->fn_flags & RTN_RTINFO)) {
808 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
809 fn->fn_flags |= RTN_RTINFO;
816 static void fib6_start_gc(struct net *net, struct rt6_info *rt)
818 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
819 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
820 mod_timer(&net->ipv6.ip6_fib_timer,
821 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
824 void fib6_force_start_gc(struct net *net)
826 if (!timer_pending(&net->ipv6.ip6_fib_timer))
827 mod_timer(&net->ipv6.ip6_fib_timer,
828 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
832 * Add routing information to the routing tree.
833 * <destination addr>/<source addr>
834 * with source addr info in sub-trees
837 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
838 struct nlattr *mx, int mx_len)
840 struct fib6_node *fn, *pn = NULL;
842 int allow_create = 1;
843 int replace_required = 0;
844 int sernum = fib6_new_sernum();
847 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
849 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
850 replace_required = 1;
852 if (!allow_create && !replace_required)
853 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
855 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
856 offsetof(struct rt6_info, rt6i_dst), allow_create,
857 replace_required, sernum);
866 #ifdef CONFIG_IPV6_SUBTREES
867 if (rt->rt6i_src.plen) {
868 struct fib6_node *sn;
871 struct fib6_node *sfn;
883 /* Create subtree root node */
888 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
889 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
890 sfn->fn_flags = RTN_ROOT;
891 sfn->fn_sernum = sernum;
893 /* Now add the first leaf node to new subtree */
895 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
897 offsetof(struct rt6_info, rt6i_src),
898 allow_create, replace_required, sernum);
901 /* If it is failed, discard just allocated
902 root, and then (in st_failure) stale node
910 /* Now link new subtree to main tree */
914 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
916 offsetof(struct rt6_info, rt6i_src),
917 allow_create, replace_required, sernum);
927 atomic_inc(&rt->rt6i_ref);
933 err = fib6_add_rt2node(fn, rt, info, mx, mx_len);
935 fib6_start_gc(info->nl_net, rt);
936 if (!(rt->rt6i_flags & RTF_CACHE))
937 fib6_prune_clones(info->nl_net, pn);
942 #ifdef CONFIG_IPV6_SUBTREES
944 * If fib6_add_1 has cleared the old leaf pointer in the
945 * super-tree leaf node we have to find a new one for it.
947 if (pn != fn && pn->leaf == rt) {
949 atomic_dec(&rt->rt6i_ref);
951 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
952 pn->leaf = fib6_find_prefix(info->nl_net, pn);
955 WARN_ON(pn->leaf == NULL);
956 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
959 atomic_inc(&pn->leaf->rt6i_ref);
966 #ifdef CONFIG_IPV6_SUBTREES
967 /* Subtree creation failed, probably main tree node
968 is orphan. If it is, shoot it.
971 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
972 fib6_repair_tree(info->nl_net, fn);
979 * Routing tree lookup
984 int offset; /* key offset on rt6_info */
985 const struct in6_addr *addr; /* search key */
988 static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
989 struct lookup_args *args)
991 struct fib6_node *fn;
994 if (unlikely(args->offset == 0))
1004 struct fib6_node *next;
1006 dir = addr_bit_set(args->addr, fn->fn_bit);
1008 next = dir ? fn->right : fn->left;
1018 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
1021 key = (struct rt6key *) ((u8 *) fn->leaf +
1024 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1025 #ifdef CONFIG_IPV6_SUBTREES
1027 struct fib6_node *sfn;
1028 sfn = fib6_lookup_1(fn->subtree,
1035 if (fn->fn_flags & RTN_RTINFO)
1039 #ifdef CONFIG_IPV6_SUBTREES
1042 if (fn->fn_flags & RTN_ROOT)
1051 struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1052 const struct in6_addr *saddr)
1054 struct fib6_node *fn;
1055 struct lookup_args args[] = {
1057 .offset = offsetof(struct rt6_info, rt6i_dst),
1060 #ifdef CONFIG_IPV6_SUBTREES
1062 .offset = offsetof(struct rt6_info, rt6i_src),
1067 .offset = 0, /* sentinel */
1071 fn = fib6_lookup_1(root, daddr ? args : args + 1);
1072 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1079 * Get node with specified destination prefix (and source prefix,
1080 * if subtrees are used)
1084 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1085 const struct in6_addr *addr,
1086 int plen, int offset)
1088 struct fib6_node *fn;
1090 for (fn = root; fn ; ) {
1091 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1096 if (plen < fn->fn_bit ||
1097 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1100 if (plen == fn->fn_bit)
1104 * We have more bits to go
1106 if (addr_bit_set(addr, fn->fn_bit))
1114 struct fib6_node *fib6_locate(struct fib6_node *root,
1115 const struct in6_addr *daddr, int dst_len,
1116 const struct in6_addr *saddr, int src_len)
1118 struct fib6_node *fn;
1120 fn = fib6_locate_1(root, daddr, dst_len,
1121 offsetof(struct rt6_info, rt6i_dst));
1123 #ifdef CONFIG_IPV6_SUBTREES
1125 WARN_ON(saddr == NULL);
1126 if (fn && fn->subtree)
1127 fn = fib6_locate_1(fn->subtree, saddr, src_len,
1128 offsetof(struct rt6_info, rt6i_src));
1132 if (fn && fn->fn_flags & RTN_RTINFO)
1144 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
1146 if (fn->fn_flags & RTN_ROOT)
1147 return net->ipv6.ip6_null_entry;
1151 return fn->left->leaf;
1153 return fn->right->leaf;
1155 fn = FIB6_SUBTREE(fn);
1161 * Called to trim the tree of intermediate nodes when possible. "fn"
1162 * is the node we want to try and remove.
1165 static struct fib6_node *fib6_repair_tree(struct net *net,
1166 struct fib6_node *fn)
1170 struct fib6_node *child, *pn;
1171 struct fib6_walker *w;
1175 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1178 WARN_ON(fn->fn_flags & RTN_RTINFO);
1179 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1180 WARN_ON(fn->leaf != NULL);
1185 child = fn->right, children |= 1;
1187 child = fn->left, children |= 2;
1189 if (children == 3 || FIB6_SUBTREE(fn)
1190 #ifdef CONFIG_IPV6_SUBTREES
1191 /* Subtree root (i.e. fn) may have one child */
1192 || (children && fn->fn_flags & RTN_ROOT)
1195 fn->leaf = fib6_find_prefix(net, fn);
1199 fn->leaf = net->ipv6.ip6_null_entry;
1202 atomic_inc(&fn->leaf->rt6i_ref);
1207 #ifdef CONFIG_IPV6_SUBTREES
1208 if (FIB6_SUBTREE(pn) == fn) {
1209 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1210 FIB6_SUBTREE(pn) = NULL;
1213 WARN_ON(fn->fn_flags & RTN_ROOT);
1215 if (pn->right == fn)
1217 else if (pn->left == fn)
1226 #ifdef CONFIG_IPV6_SUBTREES
1230 read_lock(&fib6_walker_lock);
1233 if (w->root == fn) {
1234 w->root = w->node = NULL;
1235 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1236 } else if (w->node == fn) {
1237 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1242 if (w->root == fn) {
1244 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1246 if (w->node == fn) {
1249 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1250 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1252 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1253 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1258 read_unlock(&fib6_walker_lock);
1261 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1264 rt6_release(pn->leaf);
1270 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
1271 struct nl_info *info)
1273 struct fib6_walker *w;
1274 struct rt6_info *rt = *rtp;
1275 struct net *net = info->nl_net;
1277 RT6_TRACE("fib6_del_route\n");
1280 *rtp = rt->dst.rt6_next;
1281 rt->rt6i_node = NULL;
1282 net->ipv6.rt6_stats->fib_rt_entries--;
1283 net->ipv6.rt6_stats->fib_discarded_routes++;
1285 /* Reset round-robin state, if necessary */
1286 if (fn->rr_ptr == rt)
1289 /* Remove this entry from other siblings */
1290 if (rt->rt6i_nsiblings) {
1291 struct rt6_info *sibling, *next_sibling;
1293 list_for_each_entry_safe(sibling, next_sibling,
1294 &rt->rt6i_siblings, rt6i_siblings)
1295 sibling->rt6i_nsiblings--;
1296 rt->rt6i_nsiblings = 0;
1297 list_del_init(&rt->rt6i_siblings);
1300 /* Adjust walkers */
1301 read_lock(&fib6_walker_lock);
1303 if (w->state == FWS_C && w->leaf == rt) {
1304 RT6_TRACE("walker %p adjusted by delroute\n", w);
1305 w->leaf = rt->dst.rt6_next;
1310 read_unlock(&fib6_walker_lock);
1312 rt->dst.rt6_next = NULL;
1314 /* If it was last route, expunge its radix tree node */
1316 fn->fn_flags &= ~RTN_RTINFO;
1317 net->ipv6.rt6_stats->fib_route_nodes--;
1318 fn = fib6_repair_tree(net, fn);
1321 if (atomic_read(&rt->rt6i_ref) != 1) {
1322 /* This route is used as dummy address holder in some split
1323 * nodes. It is not leaked, but it still holds other resources,
1324 * which must be released in time. So, scan ascendant nodes
1325 * and replace dummy references to this route with references
1326 * to still alive ones.
1329 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
1330 fn->leaf = fib6_find_prefix(net, fn);
1331 atomic_inc(&fn->leaf->rt6i_ref);
1336 /* No more references are possible at this point. */
1337 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
1340 inet6_rt_notify(RTM_DELROUTE, rt, info);
1344 int fib6_del(struct rt6_info *rt, struct nl_info *info)
1346 struct net *net = info->nl_net;
1347 struct fib6_node *fn = rt->rt6i_node;
1348 struct rt6_info **rtp;
1351 if (rt->dst.obsolete > 0) {
1352 WARN_ON(fn != NULL);
1356 if (!fn || rt == net->ipv6.ip6_null_entry)
1359 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1361 if (!(rt->rt6i_flags & RTF_CACHE)) {
1362 struct fib6_node *pn = fn;
1363 #ifdef CONFIG_IPV6_SUBTREES
1364 /* clones of this route might be in another subtree */
1365 if (rt->rt6i_src.plen) {
1366 while (!(pn->fn_flags & RTN_ROOT))
1371 fib6_prune_clones(info->nl_net, pn);
1375 * Walk the leaf entries looking for ourself
1378 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
1380 fib6_del_route(fn, rtp, info);
1388 * Tree traversal function.
1390 * Certainly, it is not interrupt safe.
1391 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1392 * It means, that we can modify tree during walking
1393 * and use this function for garbage collection, clone pruning,
1394 * cleaning tree when a device goes down etc. etc.
1396 * It guarantees that every node will be traversed,
1397 * and that it will be traversed only once.
1399 * Callback function w->func may return:
1400 * 0 -> continue walking.
1401 * positive value -> walking is suspended (used by tree dumps,
1402 * and probably by gc, if it will be split to several slices)
1403 * negative value -> terminate walking.
1405 * The function itself returns:
1406 * 0 -> walk is complete.
1407 * >0 -> walk is incomplete (i.e. suspended)
1408 * <0 -> walk is terminated by an error.
1411 static int fib6_walk_continue(struct fib6_walker *w)
1413 struct fib6_node *fn, *pn;
1420 if (w->prune && fn != w->root &&
1421 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
1426 #ifdef CONFIG_IPV6_SUBTREES
1428 if (FIB6_SUBTREE(fn)) {
1429 w->node = FIB6_SUBTREE(fn);
1437 w->state = FWS_INIT;
1443 w->node = fn->right;
1444 w->state = FWS_INIT;
1450 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1472 #ifdef CONFIG_IPV6_SUBTREES
1473 if (FIB6_SUBTREE(pn) == fn) {
1474 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1479 if (pn->left == fn) {
1483 if (pn->right == fn) {
1485 w->leaf = w->node->leaf;
1495 static int fib6_walk(struct fib6_walker *w)
1499 w->state = FWS_INIT;
1502 fib6_walker_link(w);
1503 res = fib6_walk_continue(w);
1505 fib6_walker_unlink(w);
1509 static int fib6_clean_node(struct fib6_walker *w)
1512 struct rt6_info *rt;
1513 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1514 struct nl_info info = {
1518 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
1519 res = c->func(rt, c->arg);
1522 res = fib6_del(rt, &info);
1525 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1526 __func__, rt, rt->rt6i_node, res);
1539 * Convenient frontend to tree walker.
1541 * func is called on each route.
1542 * It may return -1 -> delete this route.
1543 * 0 -> continue walking
1545 * prune==1 -> only immediate children of node (certainly,
1546 * ignoring pure split nodes) will be scanned.
1549 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
1550 int (*func)(struct rt6_info *, void *arg),
1551 bool prune, void *arg)
1553 struct fib6_cleaner c;
1556 c.w.func = fib6_clean_node;
1567 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
1570 struct fib6_table *table;
1571 struct hlist_head *head;
1575 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
1576 head = &net->ipv6.fib_table_hash[h];
1577 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
1578 write_lock_bh(&table->tb6_lock);
1579 fib6_clean_tree(net, &table->tb6_root,
1581 write_unlock_bh(&table->tb6_lock);
1587 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1589 if (rt->rt6i_flags & RTF_CACHE) {
1590 RT6_TRACE("pruning clone %p\n", rt);
1597 static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
1599 fib6_clean_tree(net, fn, fib6_prune_clone, true, NULL);
1602 static int fib6_update_sernum(struct rt6_info *rt, void *arg)
1604 int sernum = *(int *)arg;
1606 if (rt->rt6i_node &&
1607 rt->rt6i_node->fn_sernum != sernum)
1608 rt->rt6i_node->fn_sernum = sernum;
1613 static void fib6_flush_trees(struct net *net)
1615 int new_sernum = fib6_new_sernum();
1617 fib6_clean_all(net, fib6_update_sernum, &new_sernum);
1621 * Garbage collection
1624 static struct fib6_gc_args
1630 static int fib6_age(struct rt6_info *rt, void *arg)
1632 unsigned long now = jiffies;
1635 * check addrconf expiration here.
1636 * Routes are expired even if they are in use.
1638 * Also age clones. Note, that clones are aged out
1639 * only if they are not in use now.
1642 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1643 if (time_after(now, rt->dst.expires)) {
1644 RT6_TRACE("expiring %p\n", rt);
1648 } else if (rt->rt6i_flags & RTF_CACHE) {
1649 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1650 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
1651 RT6_TRACE("aging clone %p\n", rt);
1653 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1654 struct neighbour *neigh;
1655 __u8 neigh_flags = 0;
1657 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1659 neigh_flags = neigh->flags;
1660 neigh_release(neigh);
1662 if (!(neigh_flags & NTF_ROUTER)) {
1663 RT6_TRACE("purging route %p via non-router but gateway\n",
1674 static DEFINE_SPINLOCK(fib6_gc_lock);
1676 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
1681 spin_lock_bh(&fib6_gc_lock);
1682 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1683 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1686 gc_args.timeout = expires ? (int)expires :
1687 net->ipv6.sysctl.ip6_rt_gc_interval;
1689 gc_args.more = icmp6_dst_gc();
1691 fib6_clean_all(net, fib6_age, NULL);
1693 net->ipv6.ip6_rt_last_gc = now;
1696 mod_timer(&net->ipv6.ip6_fib_timer,
1698 + net->ipv6.sysctl.ip6_rt_gc_interval));
1700 del_timer(&net->ipv6.ip6_fib_timer);
1701 spin_unlock_bh(&fib6_gc_lock);
1704 static void fib6_gc_timer_cb(unsigned long arg)
1706 fib6_run_gc(0, (struct net *)arg, true);
1709 static int __net_init fib6_net_init(struct net *net)
1711 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1713 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
1715 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1716 if (!net->ipv6.rt6_stats)
1719 /* Avoid false sharing : Use at least a full cache line */
1720 size = max_t(size_t, size, L1_CACHE_BYTES);
1722 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
1723 if (!net->ipv6.fib_table_hash)
1726 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1728 if (!net->ipv6.fib6_main_tbl)
1729 goto out_fib_table_hash;
1731 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
1732 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1733 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1734 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1735 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
1737 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1738 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1740 if (!net->ipv6.fib6_local_tbl)
1741 goto out_fib6_main_tbl;
1742 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
1743 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1744 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1745 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1746 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
1748 fib6_tables_init(net);
1752 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1754 kfree(net->ipv6.fib6_main_tbl);
1757 kfree(net->ipv6.fib_table_hash);
1759 kfree(net->ipv6.rt6_stats);
1764 static void fib6_net_exit(struct net *net)
1766 rt6_ifdown(net, NULL);
1767 del_timer_sync(&net->ipv6.ip6_fib_timer);
1769 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1770 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
1771 kfree(net->ipv6.fib6_local_tbl);
1773 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
1774 kfree(net->ipv6.fib6_main_tbl);
1775 kfree(net->ipv6.fib_table_hash);
1776 kfree(net->ipv6.rt6_stats);
1779 static struct pernet_operations fib6_net_ops = {
1780 .init = fib6_net_init,
1781 .exit = fib6_net_exit,
1784 int __init fib6_init(void)
1788 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1789 sizeof(struct fib6_node),
1790 0, SLAB_HWCACHE_ALIGN,
1792 if (!fib6_node_kmem)
1795 ret = register_pernet_subsys(&fib6_net_ops);
1797 goto out_kmem_cache_create;
1799 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1802 goto out_unregister_subsys;
1804 __fib6_flush_trees = fib6_flush_trees;
1808 out_unregister_subsys:
1809 unregister_pernet_subsys(&fib6_net_ops);
1810 out_kmem_cache_create:
1811 kmem_cache_destroy(fib6_node_kmem);
1815 void fib6_gc_cleanup(void)
1817 unregister_pernet_subsys(&fib6_net_ops);
1818 kmem_cache_destroy(fib6_node_kmem);
1821 #ifdef CONFIG_PROC_FS
1823 struct ipv6_route_iter {
1824 struct seq_net_private p;
1825 struct fib6_walker w;
1827 struct fib6_table *tbl;
1831 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1833 struct rt6_info *rt = v;
1834 struct ipv6_route_iter *iter = seq->private;
1836 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1838 #ifdef CONFIG_IPV6_SUBTREES
1839 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1841 seq_puts(seq, "00000000000000000000000000000000 00 ");
1843 if (rt->rt6i_flags & RTF_GATEWAY)
1844 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1846 seq_puts(seq, "00000000000000000000000000000000");
1848 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1849 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1850 rt->dst.__use, rt->rt6i_flags,
1851 rt->dst.dev ? rt->dst.dev->name : "");
1852 iter->w.leaf = NULL;
1856 static int ipv6_route_yield(struct fib6_walker *w)
1858 struct ipv6_route_iter *iter = w->args;
1864 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1866 if (!iter->skip && iter->w.leaf)
1868 } while (iter->w.leaf);
1873 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1875 memset(&iter->w, 0, sizeof(iter->w));
1876 iter->w.func = ipv6_route_yield;
1877 iter->w.root = &iter->tbl->tb6_root;
1878 iter->w.state = FWS_INIT;
1879 iter->w.node = iter->w.root;
1880 iter->w.args = iter;
1881 iter->sernum = iter->w.root->fn_sernum;
1882 INIT_LIST_HEAD(&iter->w.lh);
1883 fib6_walker_link(&iter->w);
1886 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1890 struct hlist_node *node;
1893 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1894 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1900 while (!node && h < FIB6_TABLE_HASHSZ) {
1901 node = rcu_dereference_bh(
1902 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1904 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1907 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1909 if (iter->sernum != iter->w.root->fn_sernum) {
1910 iter->sernum = iter->w.root->fn_sernum;
1911 iter->w.state = FWS_INIT;
1912 iter->w.node = iter->w.root;
1913 WARN_ON(iter->w.skip);
1914 iter->w.skip = iter->w.count;
1918 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1922 struct net *net = seq_file_net(seq);
1923 struct ipv6_route_iter *iter = seq->private;
1928 n = ((struct rt6_info *)v)->dst.rt6_next;
1935 ipv6_route_check_sernum(iter);
1936 read_lock(&iter->tbl->tb6_lock);
1937 r = fib6_walk_continue(&iter->w);
1938 read_unlock(&iter->tbl->tb6_lock);
1942 return iter->w.leaf;
1944 fib6_walker_unlink(&iter->w);
1947 fib6_walker_unlink(&iter->w);
1949 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1953 ipv6_route_seq_setup_walk(iter);
1957 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1960 struct net *net = seq_file_net(seq);
1961 struct ipv6_route_iter *iter = seq->private;
1964 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1968 ipv6_route_seq_setup_walk(iter);
1969 return ipv6_route_seq_next(seq, NULL, pos);
1975 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1977 struct fib6_walker *w = &iter->w;
1978 return w->node && !(w->state == FWS_U && w->node == w->root);
1981 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
1984 struct ipv6_route_iter *iter = seq->private;
1986 if (ipv6_route_iter_active(iter))
1987 fib6_walker_unlink(&iter->w);
1989 rcu_read_unlock_bh();
1992 static const struct seq_operations ipv6_route_seq_ops = {
1993 .start = ipv6_route_seq_start,
1994 .next = ipv6_route_seq_next,
1995 .stop = ipv6_route_seq_stop,
1996 .show = ipv6_route_seq_show
1999 int ipv6_route_open(struct inode *inode, struct file *file)
2001 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2002 sizeof(struct ipv6_route_iter));
2005 #endif /* CONFIG_PROC_FS */