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_release(struct rt6_info *rt)
159 if (atomic_dec_and_test(&rt->rt6i_ref))
163 static void fib6_link_table(struct net *net, struct fib6_table *tb)
168 * Initialize table lock at a single place to give lockdep a key,
169 * tables aren't visible prior to being linked to the list.
171 rwlock_init(&tb->tb6_lock);
173 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
176 * No protection necessary, this is the only list mutatation
177 * operation, tables never disappear once they exist.
179 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
182 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
184 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
186 struct fib6_table *table;
188 table = kzalloc(sizeof(*table), GFP_ATOMIC);
191 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
192 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
193 inet_peer_base_init(&table->tb6_peers);
199 struct fib6_table *fib6_new_table(struct net *net, u32 id)
201 struct fib6_table *tb;
205 tb = fib6_get_table(net, id);
209 tb = fib6_alloc_table(net, id);
211 fib6_link_table(net, tb);
216 struct fib6_table *fib6_get_table(struct net *net, u32 id)
218 struct fib6_table *tb;
219 struct hlist_head *head;
224 h = id & (FIB6_TABLE_HASHSZ - 1);
226 head = &net->ipv6.fib_table_hash[h];
227 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
228 if (tb->tb6_id == id) {
238 static void __net_init fib6_tables_init(struct net *net)
240 fib6_link_table(net, net->ipv6.fib6_main_tbl);
241 fib6_link_table(net, net->ipv6.fib6_local_tbl);
245 struct fib6_table *fib6_new_table(struct net *net, u32 id)
247 return fib6_get_table(net, id);
250 struct fib6_table *fib6_get_table(struct net *net, u32 id)
252 return net->ipv6.fib6_main_tbl;
255 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
256 int flags, pol_lookup_t lookup)
258 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
261 static void __net_init fib6_tables_init(struct net *net)
263 fib6_link_table(net, net->ipv6.fib6_main_tbl);
268 static int fib6_dump_node(struct fib6_walker *w)
273 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
274 res = rt6_dump_route(rt, w->args);
276 /* Frame is full, suspend walking */
286 static void fib6_dump_end(struct netlink_callback *cb)
288 struct fib6_walker *w = (void *)cb->args[2];
293 fib6_walker_unlink(w);
298 cb->done = (void *)cb->args[3];
302 static int fib6_dump_done(struct netlink_callback *cb)
305 return cb->done ? cb->done(cb) : 0;
308 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
309 struct netlink_callback *cb)
311 struct fib6_walker *w;
314 w = (void *)cb->args[2];
315 w->root = &table->tb6_root;
317 if (cb->args[4] == 0) {
321 read_lock_bh(&table->tb6_lock);
323 read_unlock_bh(&table->tb6_lock);
326 cb->args[5] = w->root->fn_sernum;
329 if (cb->args[5] != w->root->fn_sernum) {
330 /* Begin at the root if the tree changed */
331 cb->args[5] = w->root->fn_sernum;
338 read_lock_bh(&table->tb6_lock);
339 res = fib6_walk_continue(w);
340 read_unlock_bh(&table->tb6_lock);
342 fib6_walker_unlink(w);
350 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
352 struct net *net = sock_net(skb->sk);
354 unsigned int e = 0, s_e;
355 struct rt6_rtnl_dump_arg arg;
356 struct fib6_walker *w;
357 struct fib6_table *tb;
358 struct hlist_head *head;
364 w = (void *)cb->args[2];
368 * 1. hook callback destructor.
370 cb->args[3] = (long)cb->done;
371 cb->done = fib6_dump_done;
374 * 2. allocate and initialize walker.
376 w = kzalloc(sizeof(*w), GFP_ATOMIC);
379 w->func = fib6_dump_node;
380 cb->args[2] = (long)w;
389 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
391 head = &net->ipv6.fib_table_hash[h];
392 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
395 res = fib6_dump_table(tb, skb, cb);
407 res = res < 0 ? res : skb->len;
416 * return the appropriate node for a routing tree "add" operation
417 * by either creating and inserting or by returning an existing
421 static struct fib6_node *fib6_add_1(struct fib6_node *root,
422 struct in6_addr *addr, int plen,
423 int offset, int allow_create,
424 int replace_required, int sernum)
426 struct fib6_node *fn, *in, *ln;
427 struct fib6_node *pn = NULL;
432 RT6_TRACE("fib6_add_1\n");
434 /* insert node in tree */
439 key = (struct rt6key *)((u8 *)fn->leaf + offset);
444 if (plen < fn->fn_bit ||
445 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
447 if (replace_required) {
448 pr_warn("Can't replace route, no match found\n");
449 return ERR_PTR(-ENOENT);
451 pr_warn("NLM_F_CREATE should be set when creating new route\n");
460 if (plen == fn->fn_bit) {
461 /* clean up an intermediate node */
462 if (!(fn->fn_flags & RTN_RTINFO)) {
463 rt6_release(fn->leaf);
467 fn->fn_sernum = sernum;
473 * We have more bits to go
476 /* Try to walk down on tree. */
477 fn->fn_sernum = sernum;
478 dir = addr_bit_set(addr, fn->fn_bit);
480 fn = dir ? fn->right : fn->left;
484 /* We should not create new node because
485 * NLM_F_REPLACE was specified without NLM_F_CREATE
486 * I assume it is safe to require NLM_F_CREATE when
487 * REPLACE flag is used! Later we may want to remove the
488 * check for replace_required, because according
489 * to netlink specification, NLM_F_CREATE
490 * MUST be specified if new route is created.
491 * That would keep IPv6 consistent with IPv4
493 if (replace_required) {
494 pr_warn("Can't replace route, no match found\n");
495 return ERR_PTR(-ENOENT);
497 pr_warn("NLM_F_CREATE should be set when creating new route\n");
500 * We walked to the bottom of tree.
501 * Create new leaf node without children.
507 return ERR_PTR(-ENOMEM);
511 ln->fn_sernum = sernum;
523 * split since we don't have a common prefix anymore or
524 * we have a less significant route.
525 * we've to insert an intermediate node on the list
526 * this new node will point to the one we need to create
532 /* find 1st bit in difference between the 2 addrs.
534 See comment in __ipv6_addr_diff: bit may be an invalid value,
535 but if it is >= plen, the value is ignored in any case.
538 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
543 * (new leaf node)[ln] (old node)[fn]
554 return ERR_PTR(-ENOMEM);
558 * new intermediate node.
560 * be off since that an address that chooses one of
561 * the branches would not match less specific routes
562 * in the other branch
569 atomic_inc(&in->leaf->rt6i_ref);
571 in->fn_sernum = sernum;
573 /* update parent pointer */
584 ln->fn_sernum = sernum;
586 if (addr_bit_set(addr, bit)) {
593 } else { /* plen <= bit */
596 * (new leaf node)[ln]
598 * (old node)[fn] NULL
604 return ERR_PTR(-ENOMEM);
610 ln->fn_sernum = sernum;
617 if (addr_bit_set(&key->addr, plen))
627 static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
629 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
633 static int fib6_commit_metrics(struct dst_entry *dst,
634 struct nlattr *mx, int mx_len)
640 if (dst->flags & DST_HOST) {
641 mp = dst_metrics_write_ptr(dst);
643 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_ATOMIC);
646 dst_init_metrics(dst, mp, 0);
649 nla_for_each_attr(nla, mx, mx_len, remaining) {
650 int type = nla_type(nla);
656 mp[type - 1] = nla_get_u32(nla);
662 static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
665 if (atomic_read(&rt->rt6i_ref) != 1) {
666 /* This route is used as dummy address holder in some split
667 * nodes. It is not leaked, but it still holds other resources,
668 * which must be released in time. So, scan ascendant nodes
669 * and replace dummy references to this route with references
670 * to still alive ones.
673 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
674 fn->leaf = fib6_find_prefix(net, fn);
675 atomic_inc(&fn->leaf->rt6i_ref);
680 /* No more references are possible at this point. */
681 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
686 * Insert routing information in a node.
689 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
690 struct nl_info *info, struct nlattr *mx, int mx_len)
692 struct rt6_info *iter = NULL;
693 struct rt6_info **ins;
694 int replace = (info->nlh &&
695 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
696 int add = (!info->nlh ||
697 (info->nlh->nlmsg_flags & NLM_F_CREATE));
699 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
704 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
706 * Search for duplicates
709 if (iter->rt6i_metric == rt->rt6i_metric) {
711 * Same priority level
714 (info->nlh->nlmsg_flags & NLM_F_EXCL))
721 if (iter->dst.dev == rt->dst.dev &&
722 iter->rt6i_idev == rt->rt6i_idev &&
723 ipv6_addr_equal(&iter->rt6i_gateway,
724 &rt->rt6i_gateway)) {
725 if (rt->rt6i_nsiblings)
726 rt->rt6i_nsiblings = 0;
727 if (!(iter->rt6i_flags & RTF_EXPIRES))
729 if (!(rt->rt6i_flags & RTF_EXPIRES))
730 rt6_clean_expires(iter);
732 rt6_set_expires(iter, rt->dst.expires);
735 /* If we have the same destination and the same metric,
736 * but not the same gateway, then the route we try to
737 * add is sibling to this route, increment our counter
738 * of siblings, and later we will add our route to the
740 * Only static routes (which don't have flag
741 * RTF_EXPIRES) are used for ECMPv6.
743 * To avoid long list, we only had siblings if the
744 * route have a gateway.
747 rt6_qualify_for_ecmp(iter))
748 rt->rt6i_nsiblings++;
751 if (iter->rt6i_metric > rt->rt6i_metric)
754 ins = &iter->dst.rt6_next;
757 /* Reset round-robin state, if necessary */
758 if (ins == &fn->leaf)
761 /* Link this route to others same route. */
762 if (rt->rt6i_nsiblings) {
763 unsigned int rt6i_nsiblings;
764 struct rt6_info *sibling, *temp_sibling;
766 /* Find the first route that have the same metric */
769 if (sibling->rt6i_metric == rt->rt6i_metric &&
770 rt6_qualify_for_ecmp(sibling)) {
771 list_add_tail(&rt->rt6i_siblings,
772 &sibling->rt6i_siblings);
775 sibling = sibling->dst.rt6_next;
777 /* For each sibling in the list, increment the counter of
778 * siblings. BUG() if counters does not match, list of siblings
782 list_for_each_entry_safe(sibling, temp_sibling,
783 &rt->rt6i_siblings, rt6i_siblings) {
784 sibling->rt6i_nsiblings++;
785 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
788 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
796 pr_warn("NLM_F_CREATE should be set when creating new route\n");
800 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
804 rt->dst.rt6_next = iter;
807 atomic_inc(&rt->rt6i_ref);
808 inet6_rt_notify(RTM_NEWROUTE, rt, info);
809 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
811 if (!(fn->fn_flags & RTN_RTINFO)) {
812 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
813 fn->fn_flags |= RTN_RTINFO;
820 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
824 err = fib6_commit_metrics(&rt->dst, mx, mx_len);
830 rt->dst.rt6_next = iter->dst.rt6_next;
831 atomic_inc(&rt->rt6i_ref);
832 inet6_rt_notify(RTM_NEWROUTE, rt, info);
833 if (!(fn->fn_flags & RTN_RTINFO)) {
834 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
835 fn->fn_flags |= RTN_RTINFO;
837 fib6_purge_rt(iter, fn, info->nl_net);
844 static void fib6_start_gc(struct net *net, struct rt6_info *rt)
846 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
847 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
848 mod_timer(&net->ipv6.ip6_fib_timer,
849 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
852 void fib6_force_start_gc(struct net *net)
854 if (!timer_pending(&net->ipv6.ip6_fib_timer))
855 mod_timer(&net->ipv6.ip6_fib_timer,
856 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
860 * Add routing information to the routing tree.
861 * <destination addr>/<source addr>
862 * with source addr info in sub-trees
865 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info,
866 struct nlattr *mx, int mx_len)
868 struct fib6_node *fn, *pn = NULL;
870 int allow_create = 1;
871 int replace_required = 0;
872 int sernum = fib6_new_sernum(info->nl_net);
875 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
877 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
878 replace_required = 1;
880 if (!allow_create && !replace_required)
881 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
883 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
884 offsetof(struct rt6_info, rt6i_dst), allow_create,
885 replace_required, sernum);
894 #ifdef CONFIG_IPV6_SUBTREES
895 if (rt->rt6i_src.plen) {
896 struct fib6_node *sn;
899 struct fib6_node *sfn;
911 /* Create subtree root node */
916 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
917 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
918 sfn->fn_flags = RTN_ROOT;
919 sfn->fn_sernum = sernum;
921 /* Now add the first leaf node to new subtree */
923 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
925 offsetof(struct rt6_info, rt6i_src),
926 allow_create, replace_required, sernum);
929 /* If it is failed, discard just allocated
930 root, and then (in st_failure) stale node
938 /* Now link new subtree to main tree */
942 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
944 offsetof(struct rt6_info, rt6i_src),
945 allow_create, replace_required, sernum);
955 atomic_inc(&rt->rt6i_ref);
961 err = fib6_add_rt2node(fn, rt, info, mx, mx_len);
963 fib6_start_gc(info->nl_net, rt);
964 if (!(rt->rt6i_flags & RTF_CACHE))
965 fib6_prune_clones(info->nl_net, pn);
970 #ifdef CONFIG_IPV6_SUBTREES
972 * If fib6_add_1 has cleared the old leaf pointer in the
973 * super-tree leaf node we have to find a new one for it.
975 if (pn != fn && pn->leaf == rt) {
977 atomic_dec(&rt->rt6i_ref);
979 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
980 pn->leaf = fib6_find_prefix(info->nl_net, pn);
983 WARN_ON(pn->leaf == NULL);
984 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
987 atomic_inc(&pn->leaf->rt6i_ref);
994 #ifdef CONFIG_IPV6_SUBTREES
995 /* Subtree creation failed, probably main tree node
996 is orphan. If it is, shoot it.
999 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
1000 fib6_repair_tree(info->nl_net, fn);
1007 * Routing tree lookup
1011 struct lookup_args {
1012 int offset; /* key offset on rt6_info */
1013 const struct in6_addr *addr; /* search key */
1016 static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1017 struct lookup_args *args)
1019 struct fib6_node *fn;
1022 if (unlikely(args->offset == 0))
1032 struct fib6_node *next;
1034 dir = addr_bit_set(args->addr, fn->fn_bit);
1036 next = dir ? fn->right : fn->left;
1046 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
1049 key = (struct rt6key *) ((u8 *) fn->leaf +
1052 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1053 #ifdef CONFIG_IPV6_SUBTREES
1055 struct fib6_node *sfn;
1056 sfn = fib6_lookup_1(fn->subtree,
1063 if (fn->fn_flags & RTN_RTINFO)
1067 #ifdef CONFIG_IPV6_SUBTREES
1070 if (fn->fn_flags & RTN_ROOT)
1079 struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1080 const struct in6_addr *saddr)
1082 struct fib6_node *fn;
1083 struct lookup_args args[] = {
1085 .offset = offsetof(struct rt6_info, rt6i_dst),
1088 #ifdef CONFIG_IPV6_SUBTREES
1090 .offset = offsetof(struct rt6_info, rt6i_src),
1095 .offset = 0, /* sentinel */
1099 fn = fib6_lookup_1(root, daddr ? args : args + 1);
1100 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1107 * Get node with specified destination prefix (and source prefix,
1108 * if subtrees are used)
1112 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1113 const struct in6_addr *addr,
1114 int plen, int offset)
1116 struct fib6_node *fn;
1118 for (fn = root; fn ; ) {
1119 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1124 if (plen < fn->fn_bit ||
1125 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1128 if (plen == fn->fn_bit)
1132 * We have more bits to go
1134 if (addr_bit_set(addr, fn->fn_bit))
1142 struct fib6_node *fib6_locate(struct fib6_node *root,
1143 const struct in6_addr *daddr, int dst_len,
1144 const struct in6_addr *saddr, int src_len)
1146 struct fib6_node *fn;
1148 fn = fib6_locate_1(root, daddr, dst_len,
1149 offsetof(struct rt6_info, rt6i_dst));
1151 #ifdef CONFIG_IPV6_SUBTREES
1153 WARN_ON(saddr == NULL);
1154 if (fn && fn->subtree)
1155 fn = fib6_locate_1(fn->subtree, saddr, src_len,
1156 offsetof(struct rt6_info, rt6i_src));
1160 if (fn && fn->fn_flags & RTN_RTINFO)
1172 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
1174 if (fn->fn_flags & RTN_ROOT)
1175 return net->ipv6.ip6_null_entry;
1179 return fn->left->leaf;
1181 return fn->right->leaf;
1183 fn = FIB6_SUBTREE(fn);
1189 * Called to trim the tree of intermediate nodes when possible. "fn"
1190 * is the node we want to try and remove.
1193 static struct fib6_node *fib6_repair_tree(struct net *net,
1194 struct fib6_node *fn)
1198 struct fib6_node *child, *pn;
1199 struct fib6_walker *w;
1203 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1206 WARN_ON(fn->fn_flags & RTN_RTINFO);
1207 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1208 WARN_ON(fn->leaf != NULL);
1213 child = fn->right, children |= 1;
1215 child = fn->left, children |= 2;
1217 if (children == 3 || FIB6_SUBTREE(fn)
1218 #ifdef CONFIG_IPV6_SUBTREES
1219 /* Subtree root (i.e. fn) may have one child */
1220 || (children && fn->fn_flags & RTN_ROOT)
1223 fn->leaf = fib6_find_prefix(net, fn);
1227 fn->leaf = net->ipv6.ip6_null_entry;
1230 atomic_inc(&fn->leaf->rt6i_ref);
1235 #ifdef CONFIG_IPV6_SUBTREES
1236 if (FIB6_SUBTREE(pn) == fn) {
1237 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1238 FIB6_SUBTREE(pn) = NULL;
1241 WARN_ON(fn->fn_flags & RTN_ROOT);
1243 if (pn->right == fn)
1245 else if (pn->left == fn)
1254 #ifdef CONFIG_IPV6_SUBTREES
1258 read_lock(&fib6_walker_lock);
1261 if (w->root == fn) {
1262 w->root = w->node = NULL;
1263 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1264 } else if (w->node == fn) {
1265 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1270 if (w->root == fn) {
1272 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1274 if (w->node == fn) {
1277 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1278 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1280 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1281 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1286 read_unlock(&fib6_walker_lock);
1289 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1292 rt6_release(pn->leaf);
1298 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
1299 struct nl_info *info)
1301 struct fib6_walker *w;
1302 struct rt6_info *rt = *rtp;
1303 struct net *net = info->nl_net;
1305 RT6_TRACE("fib6_del_route\n");
1308 *rtp = rt->dst.rt6_next;
1309 rt->rt6i_node = NULL;
1310 net->ipv6.rt6_stats->fib_rt_entries--;
1311 net->ipv6.rt6_stats->fib_discarded_routes++;
1313 /* Reset round-robin state, if necessary */
1314 if (fn->rr_ptr == rt)
1317 /* Remove this entry from other siblings */
1318 if (rt->rt6i_nsiblings) {
1319 struct rt6_info *sibling, *next_sibling;
1321 list_for_each_entry_safe(sibling, next_sibling,
1322 &rt->rt6i_siblings, rt6i_siblings)
1323 sibling->rt6i_nsiblings--;
1324 rt->rt6i_nsiblings = 0;
1325 list_del_init(&rt->rt6i_siblings);
1328 /* Adjust walkers */
1329 read_lock(&fib6_walker_lock);
1331 if (w->state == FWS_C && w->leaf == rt) {
1332 RT6_TRACE("walker %p adjusted by delroute\n", w);
1333 w->leaf = rt->dst.rt6_next;
1338 read_unlock(&fib6_walker_lock);
1340 rt->dst.rt6_next = NULL;
1342 /* If it was last route, expunge its radix tree node */
1344 fn->fn_flags &= ~RTN_RTINFO;
1345 net->ipv6.rt6_stats->fib_route_nodes--;
1346 fn = fib6_repair_tree(net, fn);
1349 fib6_purge_rt(rt, fn, net);
1351 inet6_rt_notify(RTM_DELROUTE, rt, info);
1355 int fib6_del(struct rt6_info *rt, struct nl_info *info)
1357 struct net *net = info->nl_net;
1358 struct fib6_node *fn = rt->rt6i_node;
1359 struct rt6_info **rtp;
1362 if (rt->dst.obsolete > 0) {
1363 WARN_ON(fn != NULL);
1367 if (!fn || rt == net->ipv6.ip6_null_entry)
1370 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1372 if (!(rt->rt6i_flags & RTF_CACHE)) {
1373 struct fib6_node *pn = fn;
1374 #ifdef CONFIG_IPV6_SUBTREES
1375 /* clones of this route might be in another subtree */
1376 if (rt->rt6i_src.plen) {
1377 while (!(pn->fn_flags & RTN_ROOT))
1382 fib6_prune_clones(info->nl_net, pn);
1386 * Walk the leaf entries looking for ourself
1389 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
1391 fib6_del_route(fn, rtp, info);
1399 * Tree traversal function.
1401 * Certainly, it is not interrupt safe.
1402 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1403 * It means, that we can modify tree during walking
1404 * and use this function for garbage collection, clone pruning,
1405 * cleaning tree when a device goes down etc. etc.
1407 * It guarantees that every node will be traversed,
1408 * and that it will be traversed only once.
1410 * Callback function w->func may return:
1411 * 0 -> continue walking.
1412 * positive value -> walking is suspended (used by tree dumps,
1413 * and probably by gc, if it will be split to several slices)
1414 * negative value -> terminate walking.
1416 * The function itself returns:
1417 * 0 -> walk is complete.
1418 * >0 -> walk is incomplete (i.e. suspended)
1419 * <0 -> walk is terminated by an error.
1422 static int fib6_walk_continue(struct fib6_walker *w)
1424 struct fib6_node *fn, *pn;
1431 if (w->prune && fn != w->root &&
1432 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
1437 #ifdef CONFIG_IPV6_SUBTREES
1439 if (FIB6_SUBTREE(fn)) {
1440 w->node = FIB6_SUBTREE(fn);
1448 w->state = FWS_INIT;
1454 w->node = fn->right;
1455 w->state = FWS_INIT;
1461 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1483 #ifdef CONFIG_IPV6_SUBTREES
1484 if (FIB6_SUBTREE(pn) == fn) {
1485 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1490 if (pn->left == fn) {
1494 if (pn->right == fn) {
1496 w->leaf = w->node->leaf;
1506 static int fib6_walk(struct fib6_walker *w)
1510 w->state = FWS_INIT;
1513 fib6_walker_link(w);
1514 res = fib6_walk_continue(w);
1516 fib6_walker_unlink(w);
1520 static int fib6_clean_node(struct fib6_walker *w)
1523 struct rt6_info *rt;
1524 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1525 struct nl_info info = {
1529 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1530 w->node->fn_sernum != c->sernum)
1531 w->node->fn_sernum = c->sernum;
1534 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1539 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
1540 res = c->func(rt, c->arg);
1543 res = fib6_del(rt, &info);
1546 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1547 __func__, rt, rt->rt6i_node, res);
1560 * Convenient frontend to tree walker.
1562 * func is called on each route.
1563 * It may return -1 -> delete this route.
1564 * 0 -> continue walking
1566 * prune==1 -> only immediate children of node (certainly,
1567 * ignoring pure split nodes) will be scanned.
1570 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
1571 int (*func)(struct rt6_info *, void *arg),
1572 bool prune, int sernum, void *arg)
1574 struct fib6_cleaner c;
1577 c.w.func = fib6_clean_node;
1589 static void __fib6_clean_all(struct net *net,
1590 int (*func)(struct rt6_info *, void *),
1591 int sernum, void *arg)
1593 struct fib6_table *table;
1594 struct hlist_head *head;
1598 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
1599 head = &net->ipv6.fib_table_hash[h];
1600 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
1601 write_lock_bh(&table->tb6_lock);
1602 fib6_clean_tree(net, &table->tb6_root,
1603 func, false, sernum, arg);
1604 write_unlock_bh(&table->tb6_lock);
1610 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1613 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1616 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1618 if (rt->rt6i_flags & RTF_CACHE) {
1619 RT6_TRACE("pruning clone %p\n", rt);
1626 static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
1628 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1629 FIB6_NO_SERNUM_CHANGE, NULL);
1632 static void fib6_flush_trees(struct net *net)
1634 int new_sernum = fib6_new_sernum(net);
1636 __fib6_clean_all(net, NULL, new_sernum, NULL);
1640 * Garbage collection
1643 static struct fib6_gc_args
1649 static int fib6_age(struct rt6_info *rt, void *arg)
1651 unsigned long now = jiffies;
1654 * check addrconf expiration here.
1655 * Routes are expired even if they are in use.
1657 * Also age clones. Note, that clones are aged out
1658 * only if they are not in use now.
1661 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1662 if (time_after(now, rt->dst.expires)) {
1663 RT6_TRACE("expiring %p\n", rt);
1667 } else if (rt->rt6i_flags & RTF_CACHE) {
1668 if (atomic_read(&rt->dst.__refcnt) == 0 &&
1669 time_after_eq(now, rt->dst.lastuse + gc_args.timeout)) {
1670 RT6_TRACE("aging clone %p\n", rt);
1672 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1673 struct neighbour *neigh;
1674 __u8 neigh_flags = 0;
1676 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1678 neigh_flags = neigh->flags;
1679 neigh_release(neigh);
1681 if (!(neigh_flags & NTF_ROUTER)) {
1682 RT6_TRACE("purging route %p via non-router but gateway\n",
1693 static DEFINE_SPINLOCK(fib6_gc_lock);
1695 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
1700 spin_lock_bh(&fib6_gc_lock);
1701 } else if (!spin_trylock_bh(&fib6_gc_lock)) {
1702 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1705 gc_args.timeout = expires ? (int)expires :
1706 net->ipv6.sysctl.ip6_rt_gc_interval;
1708 gc_args.more = icmp6_dst_gc();
1710 fib6_clean_all(net, fib6_age, NULL);
1712 net->ipv6.ip6_rt_last_gc = now;
1715 mod_timer(&net->ipv6.ip6_fib_timer,
1717 + net->ipv6.sysctl.ip6_rt_gc_interval));
1719 del_timer(&net->ipv6.ip6_fib_timer);
1720 spin_unlock_bh(&fib6_gc_lock);
1723 static void fib6_gc_timer_cb(unsigned long arg)
1725 fib6_run_gc(0, (struct net *)arg, true);
1728 static int __net_init fib6_net_init(struct net *net)
1730 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1732 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
1734 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1735 if (!net->ipv6.rt6_stats)
1738 /* Avoid false sharing : Use at least a full cache line */
1739 size = max_t(size_t, size, L1_CACHE_BYTES);
1741 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
1742 if (!net->ipv6.fib_table_hash)
1745 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1747 if (!net->ipv6.fib6_main_tbl)
1748 goto out_fib_table_hash;
1750 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
1751 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1752 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1753 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1754 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
1756 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1757 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1759 if (!net->ipv6.fib6_local_tbl)
1760 goto out_fib6_main_tbl;
1761 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
1762 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1763 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1764 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1765 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
1767 fib6_tables_init(net);
1771 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1773 kfree(net->ipv6.fib6_main_tbl);
1776 kfree(net->ipv6.fib_table_hash);
1778 kfree(net->ipv6.rt6_stats);
1783 static void fib6_net_exit(struct net *net)
1785 rt6_ifdown(net, NULL);
1786 del_timer_sync(&net->ipv6.ip6_fib_timer);
1788 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1789 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
1790 kfree(net->ipv6.fib6_local_tbl);
1792 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
1793 kfree(net->ipv6.fib6_main_tbl);
1794 kfree(net->ipv6.fib_table_hash);
1795 kfree(net->ipv6.rt6_stats);
1798 static struct pernet_operations fib6_net_ops = {
1799 .init = fib6_net_init,
1800 .exit = fib6_net_exit,
1803 int __init fib6_init(void)
1807 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1808 sizeof(struct fib6_node),
1809 0, SLAB_HWCACHE_ALIGN,
1811 if (!fib6_node_kmem)
1814 ret = register_pernet_subsys(&fib6_net_ops);
1816 goto out_kmem_cache_create;
1818 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1821 goto out_unregister_subsys;
1823 __fib6_flush_trees = fib6_flush_trees;
1827 out_unregister_subsys:
1828 unregister_pernet_subsys(&fib6_net_ops);
1829 out_kmem_cache_create:
1830 kmem_cache_destroy(fib6_node_kmem);
1834 void fib6_gc_cleanup(void)
1836 unregister_pernet_subsys(&fib6_net_ops);
1837 kmem_cache_destroy(fib6_node_kmem);
1840 #ifdef CONFIG_PROC_FS
1842 struct ipv6_route_iter {
1843 struct seq_net_private p;
1844 struct fib6_walker w;
1846 struct fib6_table *tbl;
1850 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1852 struct rt6_info *rt = v;
1853 struct ipv6_route_iter *iter = seq->private;
1855 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1857 #ifdef CONFIG_IPV6_SUBTREES
1858 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1860 seq_puts(seq, "00000000000000000000000000000000 00 ");
1862 if (rt->rt6i_flags & RTF_GATEWAY)
1863 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1865 seq_puts(seq, "00000000000000000000000000000000");
1867 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1868 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1869 rt->dst.__use, rt->rt6i_flags,
1870 rt->dst.dev ? rt->dst.dev->name : "");
1871 iter->w.leaf = NULL;
1875 static int ipv6_route_yield(struct fib6_walker *w)
1877 struct ipv6_route_iter *iter = w->args;
1883 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1885 if (!iter->skip && iter->w.leaf)
1887 } while (iter->w.leaf);
1892 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1894 memset(&iter->w, 0, sizeof(iter->w));
1895 iter->w.func = ipv6_route_yield;
1896 iter->w.root = &iter->tbl->tb6_root;
1897 iter->w.state = FWS_INIT;
1898 iter->w.node = iter->w.root;
1899 iter->w.args = iter;
1900 iter->sernum = iter->w.root->fn_sernum;
1901 INIT_LIST_HEAD(&iter->w.lh);
1902 fib6_walker_link(&iter->w);
1905 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1909 struct hlist_node *node;
1912 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1913 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1919 while (!node && h < FIB6_TABLE_HASHSZ) {
1920 node = rcu_dereference_bh(
1921 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1923 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1926 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1928 if (iter->sernum != iter->w.root->fn_sernum) {
1929 iter->sernum = iter->w.root->fn_sernum;
1930 iter->w.state = FWS_INIT;
1931 iter->w.node = iter->w.root;
1932 WARN_ON(iter->w.skip);
1933 iter->w.skip = iter->w.count;
1937 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1941 struct net *net = seq_file_net(seq);
1942 struct ipv6_route_iter *iter = seq->private;
1947 n = ((struct rt6_info *)v)->dst.rt6_next;
1954 ipv6_route_check_sernum(iter);
1955 read_lock(&iter->tbl->tb6_lock);
1956 r = fib6_walk_continue(&iter->w);
1957 read_unlock(&iter->tbl->tb6_lock);
1961 return iter->w.leaf;
1963 fib6_walker_unlink(&iter->w);
1966 fib6_walker_unlink(&iter->w);
1968 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1972 ipv6_route_seq_setup_walk(iter);
1976 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1979 struct net *net = seq_file_net(seq);
1980 struct ipv6_route_iter *iter = seq->private;
1983 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1987 ipv6_route_seq_setup_walk(iter);
1988 return ipv6_route_seq_next(seq, NULL, pos);
1994 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1996 struct fib6_walker *w = &iter->w;
1997 return w->node && !(w->state == FWS_U && w->node == w->root);
2000 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2003 struct ipv6_route_iter *iter = seq->private;
2005 if (ipv6_route_iter_active(iter))
2006 fib6_walker_unlink(&iter->w);
2008 rcu_read_unlock_bh();
2011 static const struct seq_operations ipv6_route_seq_ops = {
2012 .start = ipv6_route_seq_start,
2013 .next = ipv6_route_seq_next,
2014 .stop = ipv6_route_seq_stop,
2015 .show = ipv6_route_seq_show
2018 int ipv6_route_open(struct inode *inode, struct file *file)
2020 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2021 sizeof(struct ipv6_route_iter));
2024 #endif /* CONFIG_PROC_FS */