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>
35 #include <net/lwtunnel.h>
36 #include <net/fib_notifier.h>
38 #include <net/ip6_fib.h>
39 #include <net/ip6_route.h>
44 #define RT6_TRACE(x...) pr_debug(x)
46 #define RT6_TRACE(x...) do { ; } while (0)
49 static struct kmem_cache *fib6_node_kmem __read_mostly;
54 int (*func)(struct rt6_info *, void *arg);
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 net *net, 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 #define FOR_WALKERS(net, w) \
81 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
83 static void fib6_walker_link(struct net *net, struct fib6_walker *w)
85 write_lock_bh(&net->ipv6.fib6_walker_lock);
86 list_add(&w->lh, &net->ipv6.fib6_walkers);
87 write_unlock_bh(&net->ipv6.fib6_walker_lock);
90 static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
92 write_lock_bh(&net->ipv6.fib6_walker_lock);
94 write_unlock_bh(&net->ipv6.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_immediate(struct fib6_node *fn)
154 kmem_cache_free(fib6_node_kmem, fn);
157 static void node_free_rcu(struct rcu_head *head)
159 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
161 kmem_cache_free(fib6_node_kmem, fn);
164 static void node_free(struct fib6_node *fn)
166 call_rcu(&fn->rcu, node_free_rcu);
169 void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
173 if (!non_pcpu_rt->rt6i_pcpu)
176 for_each_possible_cpu(cpu) {
177 struct rt6_info **ppcpu_rt;
178 struct rt6_info *pcpu_rt;
180 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
183 dst_dev_put(&pcpu_rt->dst);
184 dst_release(&pcpu_rt->dst);
189 free_percpu(non_pcpu_rt->rt6i_pcpu);
190 non_pcpu_rt->rt6i_pcpu = NULL;
192 EXPORT_SYMBOL_GPL(rt6_free_pcpu);
194 static void fib6_free_table(struct fib6_table *table)
196 inetpeer_invalidate_tree(&table->tb6_peers);
200 static void fib6_link_table(struct net *net, struct fib6_table *tb)
205 * Initialize table lock at a single place to give lockdep a key,
206 * tables aren't visible prior to being linked to the list.
208 rwlock_init(&tb->tb6_lock);
210 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
213 * No protection necessary, this is the only list mutatation
214 * operation, tables never disappear once they exist.
216 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
219 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
221 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
223 struct fib6_table *table;
225 table = kzalloc(sizeof(*table), GFP_ATOMIC);
228 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
229 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
230 inet_peer_base_init(&table->tb6_peers);
236 struct fib6_table *fib6_new_table(struct net *net, u32 id)
238 struct fib6_table *tb;
242 tb = fib6_get_table(net, id);
246 tb = fib6_alloc_table(net, id);
248 fib6_link_table(net, tb);
252 EXPORT_SYMBOL_GPL(fib6_new_table);
254 struct fib6_table *fib6_get_table(struct net *net, u32 id)
256 struct fib6_table *tb;
257 struct hlist_head *head;
262 h = id & (FIB6_TABLE_HASHSZ - 1);
264 head = &net->ipv6.fib_table_hash[h];
265 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
266 if (tb->tb6_id == id) {
275 EXPORT_SYMBOL_GPL(fib6_get_table);
277 static void __net_init fib6_tables_init(struct net *net)
279 fib6_link_table(net, net->ipv6.fib6_main_tbl);
280 fib6_link_table(net, net->ipv6.fib6_local_tbl);
284 struct fib6_table *fib6_new_table(struct net *net, u32 id)
286 return fib6_get_table(net, id);
289 struct fib6_table *fib6_get_table(struct net *net, u32 id)
291 return net->ipv6.fib6_main_tbl;
294 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
295 int flags, pol_lookup_t lookup)
299 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
300 if (rt->dst.error == -EAGAIN) {
302 rt = net->ipv6.ip6_null_entry;
309 static void __net_init fib6_tables_init(struct net *net)
311 fib6_link_table(net, net->ipv6.fib6_main_tbl);
316 unsigned int fib6_tables_seq_read(struct net *net)
318 unsigned int h, fib_seq = 0;
321 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
322 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
323 struct fib6_table *tb;
325 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
326 read_lock_bh(&tb->tb6_lock);
327 fib_seq += tb->fib_seq;
328 read_unlock_bh(&tb->tb6_lock);
336 static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
337 enum fib_event_type event_type,
340 struct fib6_entry_notifier_info info = {
344 return call_fib6_notifier(nb, net, event_type, &info.info);
347 static int call_fib6_entry_notifiers(struct net *net,
348 enum fib_event_type event_type,
351 struct fib6_entry_notifier_info info = {
355 rt->rt6i_table->fib_seq++;
356 return call_fib6_notifiers(net, event_type, &info.info);
359 struct fib6_dump_arg {
361 struct notifier_block *nb;
364 static void fib6_rt_dump(struct rt6_info *rt, struct fib6_dump_arg *arg)
366 if (rt == arg->net->ipv6.ip6_null_entry)
368 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
371 static int fib6_node_dump(struct fib6_walker *w)
375 for (rt = w->leaf; rt; rt = rt->dst.rt6_next)
376 fib6_rt_dump(rt, w->args);
381 static void fib6_table_dump(struct net *net, struct fib6_table *tb,
382 struct fib6_walker *w)
384 w->root = &tb->tb6_root;
385 read_lock_bh(&tb->tb6_lock);
387 read_unlock_bh(&tb->tb6_lock);
390 /* Called with rcu_read_lock() */
391 int fib6_tables_dump(struct net *net, struct notifier_block *nb)
393 struct fib6_dump_arg arg;
394 struct fib6_walker *w;
397 w = kzalloc(sizeof(*w), GFP_ATOMIC);
401 w->func = fib6_node_dump;
406 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
407 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
408 struct fib6_table *tb;
410 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
411 fib6_table_dump(net, tb, w);
419 static int fib6_dump_node(struct fib6_walker *w)
424 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
425 res = rt6_dump_route(rt, w->args);
427 /* Frame is full, suspend walking */
432 /* Multipath routes are dumped in one route with the
433 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
434 * last sibling of this route (no need to dump the
435 * sibling routes again)
437 if (rt->rt6i_nsiblings)
438 rt = list_last_entry(&rt->rt6i_siblings,
446 static void fib6_dump_end(struct netlink_callback *cb)
448 struct net *net = sock_net(cb->skb->sk);
449 struct fib6_walker *w = (void *)cb->args[2];
454 fib6_walker_unlink(net, w);
459 cb->done = (void *)cb->args[3];
463 static int fib6_dump_done(struct netlink_callback *cb)
466 return cb->done ? cb->done(cb) : 0;
469 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
470 struct netlink_callback *cb)
472 struct net *net = sock_net(skb->sk);
473 struct fib6_walker *w;
476 w = (void *)cb->args[2];
477 w->root = &table->tb6_root;
479 if (cb->args[4] == 0) {
483 read_lock_bh(&table->tb6_lock);
484 res = fib6_walk(net, w);
485 read_unlock_bh(&table->tb6_lock);
488 cb->args[5] = w->root->fn_sernum;
491 if (cb->args[5] != w->root->fn_sernum) {
492 /* Begin at the root if the tree changed */
493 cb->args[5] = w->root->fn_sernum;
500 read_lock_bh(&table->tb6_lock);
501 res = fib6_walk_continue(w);
502 read_unlock_bh(&table->tb6_lock);
504 fib6_walker_unlink(net, w);
512 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
514 struct net *net = sock_net(skb->sk);
516 unsigned int e = 0, s_e;
517 struct rt6_rtnl_dump_arg arg;
518 struct fib6_walker *w;
519 struct fib6_table *tb;
520 struct hlist_head *head;
526 w = (void *)cb->args[2];
530 * 1. hook callback destructor.
532 cb->args[3] = (long)cb->done;
533 cb->done = fib6_dump_done;
536 * 2. allocate and initialize walker.
538 w = kzalloc(sizeof(*w), GFP_ATOMIC);
541 w->func = fib6_dump_node;
542 cb->args[2] = (long)w;
551 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
553 head = &net->ipv6.fib_table_hash[h];
554 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
557 res = fib6_dump_table(tb, skb, cb);
569 res = res < 0 ? res : skb->len;
578 * return the appropriate node for a routing tree "add" operation
579 * by either creating and inserting or by returning an existing
583 static struct fib6_node *fib6_add_1(struct fib6_node *root,
584 struct in6_addr *addr, int plen,
585 int offset, int allow_create,
586 int replace_required, int sernum,
587 struct netlink_ext_ack *extack)
589 struct fib6_node *fn, *in, *ln;
590 struct fib6_node *pn = NULL;
595 RT6_TRACE("fib6_add_1\n");
597 /* insert node in tree */
602 key = (struct rt6key *)((u8 *)fn->leaf + offset);
607 if (plen < fn->fn_bit ||
608 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
610 if (replace_required) {
611 NL_SET_ERR_MSG(extack,
612 "Can not replace route - no match found");
613 pr_warn("Can't replace route, no match found\n");
614 return ERR_PTR(-ENOENT);
616 pr_warn("NLM_F_CREATE should be set when creating new route\n");
625 if (plen == fn->fn_bit) {
626 /* clean up an intermediate node */
627 if (!(fn->fn_flags & RTN_RTINFO)) {
628 rt6_release(fn->leaf);
632 fn->fn_sernum = sernum;
638 * We have more bits to go
641 /* Try to walk down on tree. */
642 fn->fn_sernum = sernum;
643 dir = addr_bit_set(addr, fn->fn_bit);
645 fn = dir ? fn->right : fn->left;
649 /* We should not create new node because
650 * NLM_F_REPLACE was specified without NLM_F_CREATE
651 * I assume it is safe to require NLM_F_CREATE when
652 * REPLACE flag is used! Later we may want to remove the
653 * check for replace_required, because according
654 * to netlink specification, NLM_F_CREATE
655 * MUST be specified if new route is created.
656 * That would keep IPv6 consistent with IPv4
658 if (replace_required) {
659 NL_SET_ERR_MSG(extack,
660 "Can not replace route - no match found");
661 pr_warn("Can't replace route, no match found\n");
662 return ERR_PTR(-ENOENT);
664 pr_warn("NLM_F_CREATE should be set when creating new route\n");
667 * We walked to the bottom of tree.
668 * Create new leaf node without children.
674 return ERR_PTR(-ENOMEM);
678 ln->fn_sernum = sernum;
690 * split since we don't have a common prefix anymore or
691 * we have a less significant route.
692 * we've to insert an intermediate node on the list
693 * this new node will point to the one we need to create
699 /* find 1st bit in difference between the 2 addrs.
701 See comment in __ipv6_addr_diff: bit may be an invalid value,
702 but if it is >= plen, the value is ignored in any case.
705 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
710 * (new leaf node)[ln] (old node)[fn]
718 node_free_immediate(in);
720 node_free_immediate(ln);
721 return ERR_PTR(-ENOMEM);
725 * new intermediate node.
727 * be off since that an address that chooses one of
728 * the branches would not match less specific routes
729 * in the other branch
736 atomic_inc(&in->leaf->rt6i_ref);
738 in->fn_sernum = sernum;
740 /* update parent pointer */
751 ln->fn_sernum = sernum;
753 if (addr_bit_set(addr, bit)) {
760 } else { /* plen <= bit */
763 * (new leaf node)[ln]
765 * (old node)[fn] NULL
771 return ERR_PTR(-ENOMEM);
777 ln->fn_sernum = sernum;
784 if (addr_bit_set(&key->addr, plen))
794 static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
796 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
800 static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
804 for (i = 0; i < RTAX_MAX; i++) {
805 if (test_bit(i, mxc->mx_valid))
810 static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
815 if (dst->flags & DST_HOST) {
816 u32 *mp = dst_metrics_write_ptr(dst);
821 fib6_copy_metrics(mp, mxc);
823 dst_init_metrics(dst, mxc->mx, false);
825 /* We've stolen mx now. */
832 static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
835 if (atomic_read(&rt->rt6i_ref) != 1) {
836 /* This route is used as dummy address holder in some split
837 * nodes. It is not leaked, but it still holds other resources,
838 * which must be released in time. So, scan ascendant nodes
839 * and replace dummy references to this route with references
840 * to still alive ones.
843 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
844 fn->leaf = fib6_find_prefix(net, fn);
845 atomic_inc(&fn->leaf->rt6i_ref);
854 * Insert routing information in a node.
857 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
858 struct nl_info *info, struct mx6_config *mxc)
860 struct rt6_info *iter = NULL;
861 struct rt6_info **ins;
862 struct rt6_info **fallback_ins = NULL;
863 int replace = (info->nlh &&
864 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
865 int add = (!info->nlh ||
866 (info->nlh->nlmsg_flags & NLM_F_CREATE));
868 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
869 u16 nlflags = NLM_F_EXCL;
872 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
873 nlflags |= NLM_F_APPEND;
877 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
879 * Search for duplicates
882 if (iter->rt6i_metric == rt->rt6i_metric) {
884 * Same priority level
887 (info->nlh->nlmsg_flags & NLM_F_EXCL))
890 nlflags &= ~NLM_F_EXCL;
892 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
896 fallback_ins = fallback_ins ?: ins;
900 if (rt6_duplicate_nexthop(iter, rt)) {
901 if (rt->rt6i_nsiblings)
902 rt->rt6i_nsiblings = 0;
903 if (!(iter->rt6i_flags & RTF_EXPIRES))
905 if (!(rt->rt6i_flags & RTF_EXPIRES))
906 rt6_clean_expires(iter);
908 rt6_set_expires(iter, rt->dst.expires);
909 iter->rt6i_pmtu = rt->rt6i_pmtu;
912 /* If we have the same destination and the same metric,
913 * but not the same gateway, then the route we try to
914 * add is sibling to this route, increment our counter
915 * of siblings, and later we will add our route to the
917 * Only static routes (which don't have flag
918 * RTF_EXPIRES) are used for ECMPv6.
920 * To avoid long list, we only had siblings if the
921 * route have a gateway.
924 rt6_qualify_for_ecmp(iter))
925 rt->rt6i_nsiblings++;
928 if (iter->rt6i_metric > rt->rt6i_metric)
932 ins = &iter->dst.rt6_next;
935 if (fallback_ins && !found) {
936 /* No matching route with same ecmp-able-ness found, replace
937 * first matching route
944 /* Reset round-robin state, if necessary */
945 if (ins == &fn->leaf)
948 /* Link this route to others same route. */
949 if (rt->rt6i_nsiblings) {
950 unsigned int rt6i_nsiblings;
951 struct rt6_info *sibling, *temp_sibling;
953 /* Find the first route that have the same metric */
956 if (sibling->rt6i_metric == rt->rt6i_metric &&
957 rt6_qualify_for_ecmp(sibling)) {
958 list_add_tail(&rt->rt6i_siblings,
959 &sibling->rt6i_siblings);
962 sibling = sibling->dst.rt6_next;
964 /* For each sibling in the list, increment the counter of
965 * siblings. BUG() if counters does not match, list of siblings
969 list_for_each_entry_safe(sibling, temp_sibling,
970 &rt->rt6i_siblings, rt6i_siblings) {
971 sibling->rt6i_nsiblings++;
972 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
975 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
983 pr_warn("NLM_F_CREATE should be set when creating new route\n");
986 nlflags |= NLM_F_CREATE;
987 err = fib6_commit_metrics(&rt->dst, mxc);
991 rt->dst.rt6_next = iter;
993 rcu_assign_pointer(rt->rt6i_node, fn);
994 atomic_inc(&rt->rt6i_ref);
995 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
997 if (!info->skip_notify)
998 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
999 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1001 if (!(fn->fn_flags & RTN_RTINFO)) {
1002 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1003 fn->fn_flags |= RTN_RTINFO;
1012 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1016 err = fib6_commit_metrics(&rt->dst, mxc);
1021 rcu_assign_pointer(rt->rt6i_node, fn);
1022 rt->dst.rt6_next = iter->dst.rt6_next;
1023 atomic_inc(&rt->rt6i_ref);
1024 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
1026 if (!info->skip_notify)
1027 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1028 if (!(fn->fn_flags & RTN_RTINFO)) {
1029 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1030 fn->fn_flags |= RTN_RTINFO;
1032 nsiblings = iter->rt6i_nsiblings;
1033 iter->rt6i_node = NULL;
1034 fib6_purge_rt(iter, fn, info->nl_net);
1035 if (fn->rr_ptr == iter)
1040 /* Replacing an ECMP route, remove all siblings */
1041 ins = &rt->dst.rt6_next;
1044 if (iter->rt6i_metric > rt->rt6i_metric)
1046 if (rt6_qualify_for_ecmp(iter)) {
1047 *ins = iter->dst.rt6_next;
1048 iter->rt6i_node = NULL;
1049 fib6_purge_rt(iter, fn, info->nl_net);
1050 if (fn->rr_ptr == iter)
1055 ins = &iter->dst.rt6_next;
1059 WARN_ON(nsiblings != 0);
1066 static void fib6_start_gc(struct net *net, struct rt6_info *rt)
1068 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1069 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
1070 mod_timer(&net->ipv6.ip6_fib_timer,
1071 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1074 void fib6_force_start_gc(struct net *net)
1076 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1077 mod_timer(&net->ipv6.ip6_fib_timer,
1078 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1082 * Add routing information to the routing tree.
1083 * <destination addr>/<source addr>
1084 * with source addr info in sub-trees
1087 int fib6_add(struct fib6_node *root, struct rt6_info *rt,
1088 struct nl_info *info, struct mx6_config *mxc,
1089 struct netlink_ext_ack *extack)
1091 struct fib6_node *fn, *pn = NULL;
1093 int allow_create = 1;
1094 int replace_required = 0;
1095 int sernum = fib6_new_sernum(info->nl_net);
1097 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
1101 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1103 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1104 replace_required = 1;
1106 if (!allow_create && !replace_required)
1107 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1109 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
1110 offsetof(struct rt6_info, rt6i_dst), allow_create,
1111 replace_required, sernum, extack);
1120 #ifdef CONFIG_IPV6_SUBTREES
1121 if (rt->rt6i_src.plen) {
1122 struct fib6_node *sn;
1125 struct fib6_node *sfn;
1137 /* Create subtree root node */
1142 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1143 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
1144 sfn->fn_flags = RTN_ROOT;
1145 sfn->fn_sernum = sernum;
1147 /* Now add the first leaf node to new subtree */
1149 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
1151 offsetof(struct rt6_info, rt6i_src),
1152 allow_create, replace_required, sernum,
1156 /* If it is failed, discard just allocated
1157 root, and then (in failure) stale node
1160 node_free_immediate(sfn);
1165 /* Now link new subtree to main tree */
1169 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
1171 offsetof(struct rt6_info, rt6i_src),
1172 allow_create, replace_required, sernum,
1183 atomic_inc(&rt->rt6i_ref);
1189 err = fib6_add_rt2node(fn, rt, info, mxc);
1191 fib6_start_gc(info->nl_net, rt);
1192 if (!(rt->rt6i_flags & RTF_CACHE))
1193 fib6_prune_clones(info->nl_net, pn);
1198 #ifdef CONFIG_IPV6_SUBTREES
1200 * If fib6_add_1 has cleared the old leaf pointer in the
1201 * super-tree leaf node we have to find a new one for it.
1203 if (pn != fn && pn->leaf == rt) {
1205 atomic_dec(&rt->rt6i_ref);
1207 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
1208 pn->leaf = fib6_find_prefix(info->nl_net, pn);
1211 WARN_ON(pn->leaf == NULL);
1212 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
1215 atomic_inc(&pn->leaf->rt6i_ref);
1223 /* fn->leaf could be NULL if fn is an intermediate node and we
1224 * failed to add the new route to it in both subtree creation
1225 * failure and fib6_add_rt2node() failure case.
1226 * In both cases, fib6_repair_tree() should be called to fix
1229 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
1230 fib6_repair_tree(info->nl_net, fn);
1231 /* Always release dst as dst->__refcnt is guaranteed
1232 * to be taken before entering this function
1234 dst_release_immediate(&rt->dst);
1239 * Routing tree lookup
1243 struct lookup_args {
1244 int offset; /* key offset on rt6_info */
1245 const struct in6_addr *addr; /* search key */
1248 static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1249 struct lookup_args *args)
1251 struct fib6_node *fn;
1254 if (unlikely(args->offset == 0))
1264 struct fib6_node *next;
1266 dir = addr_bit_set(args->addr, fn->fn_bit);
1268 next = dir ? fn->right : fn->left;
1278 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
1281 key = (struct rt6key *) ((u8 *) fn->leaf +
1284 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1285 #ifdef CONFIG_IPV6_SUBTREES
1287 struct fib6_node *sfn;
1288 sfn = fib6_lookup_1(fn->subtree,
1295 if (fn->fn_flags & RTN_RTINFO)
1299 #ifdef CONFIG_IPV6_SUBTREES
1302 if (fn->fn_flags & RTN_ROOT)
1311 struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1312 const struct in6_addr *saddr)
1314 struct fib6_node *fn;
1315 struct lookup_args args[] = {
1317 .offset = offsetof(struct rt6_info, rt6i_dst),
1320 #ifdef CONFIG_IPV6_SUBTREES
1322 .offset = offsetof(struct rt6_info, rt6i_src),
1327 .offset = 0, /* sentinel */
1331 fn = fib6_lookup_1(root, daddr ? args : args + 1);
1332 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1339 * Get node with specified destination prefix (and source prefix,
1340 * if subtrees are used)
1344 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1345 const struct in6_addr *addr,
1346 int plen, int offset)
1348 struct fib6_node *fn;
1350 for (fn = root; fn ; ) {
1351 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1356 if (plen < fn->fn_bit ||
1357 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1360 if (plen == fn->fn_bit)
1364 * We have more bits to go
1366 if (addr_bit_set(addr, fn->fn_bit))
1374 struct fib6_node *fib6_locate(struct fib6_node *root,
1375 const struct in6_addr *daddr, int dst_len,
1376 const struct in6_addr *saddr, int src_len)
1378 struct fib6_node *fn;
1380 fn = fib6_locate_1(root, daddr, dst_len,
1381 offsetof(struct rt6_info, rt6i_dst));
1383 #ifdef CONFIG_IPV6_SUBTREES
1385 WARN_ON(saddr == NULL);
1386 if (fn && fn->subtree)
1387 fn = fib6_locate_1(fn->subtree, saddr, src_len,
1388 offsetof(struct rt6_info, rt6i_src));
1392 if (fn && fn->fn_flags & RTN_RTINFO)
1404 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
1406 if (fn->fn_flags & RTN_ROOT)
1407 return net->ipv6.ip6_null_entry;
1411 return fn->left->leaf;
1413 return fn->right->leaf;
1415 fn = FIB6_SUBTREE(fn);
1421 * Called to trim the tree of intermediate nodes when possible. "fn"
1422 * is the node we want to try and remove.
1425 static struct fib6_node *fib6_repair_tree(struct net *net,
1426 struct fib6_node *fn)
1430 struct fib6_node *child, *pn;
1431 struct fib6_walker *w;
1435 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1438 WARN_ON(fn->fn_flags & RTN_RTINFO);
1439 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1445 child = fn->right, children |= 1;
1447 child = fn->left, children |= 2;
1449 if (children == 3 || FIB6_SUBTREE(fn)
1450 #ifdef CONFIG_IPV6_SUBTREES
1451 /* Subtree root (i.e. fn) may have one child */
1452 || (children && fn->fn_flags & RTN_ROOT)
1455 fn->leaf = fib6_find_prefix(net, fn);
1459 fn->leaf = net->ipv6.ip6_null_entry;
1462 atomic_inc(&fn->leaf->rt6i_ref);
1467 #ifdef CONFIG_IPV6_SUBTREES
1468 if (FIB6_SUBTREE(pn) == fn) {
1469 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1470 FIB6_SUBTREE(pn) = NULL;
1473 WARN_ON(fn->fn_flags & RTN_ROOT);
1475 if (pn->right == fn)
1477 else if (pn->left == fn)
1486 #ifdef CONFIG_IPV6_SUBTREES
1490 read_lock(&net->ipv6.fib6_walker_lock);
1491 FOR_WALKERS(net, w) {
1493 if (w->root == fn) {
1494 w->root = w->node = NULL;
1495 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1496 } else if (w->node == fn) {
1497 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1502 if (w->root == fn) {
1504 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1506 if (w->node == fn) {
1509 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1510 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1512 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1513 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1518 read_unlock(&net->ipv6.fib6_walker_lock);
1521 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1524 rt6_release(pn->leaf);
1530 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
1531 struct nl_info *info)
1533 struct fib6_walker *w;
1534 struct rt6_info *rt = *rtp;
1535 struct net *net = info->nl_net;
1537 RT6_TRACE("fib6_del_route\n");
1540 *rtp = rt->dst.rt6_next;
1541 rt->rt6i_node = NULL;
1542 net->ipv6.rt6_stats->fib_rt_entries--;
1543 net->ipv6.rt6_stats->fib_discarded_routes++;
1545 /* Reset round-robin state, if necessary */
1546 if (fn->rr_ptr == rt)
1549 /* Remove this entry from other siblings */
1550 if (rt->rt6i_nsiblings) {
1551 struct rt6_info *sibling, *next_sibling;
1553 list_for_each_entry_safe(sibling, next_sibling,
1554 &rt->rt6i_siblings, rt6i_siblings)
1555 sibling->rt6i_nsiblings--;
1556 rt->rt6i_nsiblings = 0;
1557 list_del_init(&rt->rt6i_siblings);
1560 /* Adjust walkers */
1561 read_lock(&net->ipv6.fib6_walker_lock);
1562 FOR_WALKERS(net, w) {
1563 if (w->state == FWS_C && w->leaf == rt) {
1564 RT6_TRACE("walker %p adjusted by delroute\n", w);
1565 w->leaf = rt->dst.rt6_next;
1570 read_unlock(&net->ipv6.fib6_walker_lock);
1572 rt->dst.rt6_next = NULL;
1574 /* If it was last route, expunge its radix tree node */
1576 fn->fn_flags &= ~RTN_RTINFO;
1577 net->ipv6.rt6_stats->fib_route_nodes--;
1578 fn = fib6_repair_tree(net, fn);
1581 fib6_purge_rt(rt, fn, net);
1583 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt);
1584 if (!info->skip_notify)
1585 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
1589 int fib6_del(struct rt6_info *rt, struct nl_info *info)
1591 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1592 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1593 struct net *net = info->nl_net;
1594 struct rt6_info **rtp;
1597 if (rt->dst.obsolete > 0) {
1602 if (!fn || rt == net->ipv6.ip6_null_entry)
1605 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1607 if (!(rt->rt6i_flags & RTF_CACHE)) {
1608 struct fib6_node *pn = fn;
1609 #ifdef CONFIG_IPV6_SUBTREES
1610 /* clones of this route might be in another subtree */
1611 if (rt->rt6i_src.plen) {
1612 while (!(pn->fn_flags & RTN_ROOT))
1617 fib6_prune_clones(info->nl_net, pn);
1621 * Walk the leaf entries looking for ourself
1624 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
1626 fib6_del_route(fn, rtp, info);
1634 * Tree traversal function.
1636 * Certainly, it is not interrupt safe.
1637 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1638 * It means, that we can modify tree during walking
1639 * and use this function for garbage collection, clone pruning,
1640 * cleaning tree when a device goes down etc. etc.
1642 * It guarantees that every node will be traversed,
1643 * and that it will be traversed only once.
1645 * Callback function w->func may return:
1646 * 0 -> continue walking.
1647 * positive value -> walking is suspended (used by tree dumps,
1648 * and probably by gc, if it will be split to several slices)
1649 * negative value -> terminate walking.
1651 * The function itself returns:
1652 * 0 -> walk is complete.
1653 * >0 -> walk is incomplete (i.e. suspended)
1654 * <0 -> walk is terminated by an error.
1657 static int fib6_walk_continue(struct fib6_walker *w)
1659 struct fib6_node *fn, *pn;
1666 if (w->prune && fn != w->root &&
1667 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
1672 #ifdef CONFIG_IPV6_SUBTREES
1674 if (FIB6_SUBTREE(fn)) {
1675 w->node = FIB6_SUBTREE(fn);
1683 w->state = FWS_INIT;
1689 w->node = fn->right;
1690 w->state = FWS_INIT;
1696 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1718 #ifdef CONFIG_IPV6_SUBTREES
1719 if (FIB6_SUBTREE(pn) == fn) {
1720 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1725 if (pn->left == fn) {
1729 if (pn->right == fn) {
1731 w->leaf = w->node->leaf;
1741 static int fib6_walk(struct net *net, struct fib6_walker *w)
1745 w->state = FWS_INIT;
1748 fib6_walker_link(net, w);
1749 res = fib6_walk_continue(w);
1751 fib6_walker_unlink(net, w);
1755 static int fib6_clean_node(struct fib6_walker *w)
1758 struct rt6_info *rt;
1759 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1760 struct nl_info info = {
1764 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1765 w->node->fn_sernum != c->sernum)
1766 w->node->fn_sernum = c->sernum;
1769 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1774 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
1775 res = c->func(rt, c->arg);
1778 res = fib6_del(rt, &info);
1781 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1783 rcu_access_pointer(rt->rt6i_node),
1797 * Convenient frontend to tree walker.
1799 * func is called on each route.
1800 * It may return -1 -> delete this route.
1801 * 0 -> continue walking
1803 * prune==1 -> only immediate children of node (certainly,
1804 * ignoring pure split nodes) will be scanned.
1807 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
1808 int (*func)(struct rt6_info *, void *arg),
1809 bool prune, int sernum, void *arg)
1811 struct fib6_cleaner c;
1814 c.w.func = fib6_clean_node;
1823 fib6_walk(net, &c.w);
1826 static void __fib6_clean_all(struct net *net,
1827 int (*func)(struct rt6_info *, void *),
1828 int sernum, void *arg)
1830 struct fib6_table *table;
1831 struct hlist_head *head;
1835 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
1836 head = &net->ipv6.fib_table_hash[h];
1837 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
1838 write_lock_bh(&table->tb6_lock);
1839 fib6_clean_tree(net, &table->tb6_root,
1840 func, false, sernum, arg);
1841 write_unlock_bh(&table->tb6_lock);
1847 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1850 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1853 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1855 if (rt->rt6i_flags & RTF_CACHE) {
1856 RT6_TRACE("pruning clone %p\n", rt);
1863 static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
1865 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1866 FIB6_NO_SERNUM_CHANGE, NULL);
1869 static void fib6_flush_trees(struct net *net)
1871 int new_sernum = fib6_new_sernum(net);
1873 __fib6_clean_all(net, NULL, new_sernum, NULL);
1877 * Garbage collection
1886 static int fib6_age(struct rt6_info *rt, void *arg)
1888 struct fib6_gc_args *gc_args = arg;
1889 unsigned long now = jiffies;
1892 * check addrconf expiration here.
1893 * Routes are expired even if they are in use.
1895 * Also age clones. Note, that clones are aged out
1896 * only if they are not in use now.
1899 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1900 if (time_after(now, rt->dst.expires)) {
1901 RT6_TRACE("expiring %p\n", rt);
1905 } else if (rt->rt6i_flags & RTF_CACHE) {
1906 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout))
1907 rt->dst.obsolete = DST_OBSOLETE_KILL;
1908 if (atomic_read(&rt->dst.__refcnt) == 1 &&
1909 rt->dst.obsolete == DST_OBSOLETE_KILL) {
1910 RT6_TRACE("aging clone %p\n", rt);
1912 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1913 struct neighbour *neigh;
1914 __u8 neigh_flags = 0;
1916 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1918 neigh_flags = neigh->flags;
1919 neigh_release(neigh);
1921 if (!(neigh_flags & NTF_ROUTER)) {
1922 RT6_TRACE("purging route %p via non-router but gateway\n",
1933 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
1935 struct fib6_gc_args gc_args;
1939 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1940 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
1941 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1944 gc_args.timeout = expires ? (int)expires :
1945 net->ipv6.sysctl.ip6_rt_gc_interval;
1948 fib6_clean_all(net, fib6_age, &gc_args);
1950 net->ipv6.ip6_rt_last_gc = now;
1953 mod_timer(&net->ipv6.ip6_fib_timer,
1955 + net->ipv6.sysctl.ip6_rt_gc_interval));
1957 del_timer(&net->ipv6.ip6_fib_timer);
1958 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
1961 static void fib6_gc_timer_cb(unsigned long arg)
1963 fib6_run_gc(0, (struct net *)arg, true);
1966 static int __net_init fib6_net_init(struct net *net)
1968 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1971 err = fib6_notifier_init(net);
1975 spin_lock_init(&net->ipv6.fib6_gc_lock);
1976 rwlock_init(&net->ipv6.fib6_walker_lock);
1977 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
1978 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
1980 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1981 if (!net->ipv6.rt6_stats)
1984 /* Avoid false sharing : Use at least a full cache line */
1985 size = max_t(size_t, size, L1_CACHE_BYTES);
1987 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
1988 if (!net->ipv6.fib_table_hash)
1991 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1993 if (!net->ipv6.fib6_main_tbl)
1994 goto out_fib_table_hash;
1996 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
1997 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1998 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1999 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2000 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2002 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2003 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2005 if (!net->ipv6.fib6_local_tbl)
2006 goto out_fib6_main_tbl;
2007 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2008 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
2009 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2010 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2011 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2013 fib6_tables_init(net);
2017 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2019 kfree(net->ipv6.fib6_main_tbl);
2022 kfree(net->ipv6.fib_table_hash);
2024 kfree(net->ipv6.rt6_stats);
2026 fib6_notifier_exit(net);
2030 static void fib6_net_exit(struct net *net)
2034 rt6_ifdown(net, NULL);
2035 del_timer_sync(&net->ipv6.ip6_fib_timer);
2037 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2038 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2039 struct hlist_node *tmp;
2040 struct fib6_table *tb;
2042 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2043 hlist_del(&tb->tb6_hlist);
2044 fib6_free_table(tb);
2048 kfree(net->ipv6.fib_table_hash);
2049 kfree(net->ipv6.rt6_stats);
2050 fib6_notifier_exit(net);
2053 static struct pernet_operations fib6_net_ops = {
2054 .init = fib6_net_init,
2055 .exit = fib6_net_exit,
2058 int __init fib6_init(void)
2062 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2063 sizeof(struct fib6_node),
2064 0, SLAB_HWCACHE_ALIGN,
2066 if (!fib6_node_kmem)
2069 ret = register_pernet_subsys(&fib6_net_ops);
2071 goto out_kmem_cache_create;
2073 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
2076 goto out_unregister_subsys;
2078 __fib6_flush_trees = fib6_flush_trees;
2082 out_unregister_subsys:
2083 unregister_pernet_subsys(&fib6_net_ops);
2084 out_kmem_cache_create:
2085 kmem_cache_destroy(fib6_node_kmem);
2089 void fib6_gc_cleanup(void)
2091 unregister_pernet_subsys(&fib6_net_ops);
2092 kmem_cache_destroy(fib6_node_kmem);
2095 #ifdef CONFIG_PROC_FS
2097 struct ipv6_route_iter {
2098 struct seq_net_private p;
2099 struct fib6_walker w;
2101 struct fib6_table *tbl;
2105 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2107 struct rt6_info *rt = v;
2108 struct ipv6_route_iter *iter = seq->private;
2110 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2112 #ifdef CONFIG_IPV6_SUBTREES
2113 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2115 seq_puts(seq, "00000000000000000000000000000000 00 ");
2117 if (rt->rt6i_flags & RTF_GATEWAY)
2118 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
2120 seq_puts(seq, "00000000000000000000000000000000");
2122 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2123 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2124 rt->dst.__use, rt->rt6i_flags,
2125 rt->dst.dev ? rt->dst.dev->name : "");
2126 iter->w.leaf = NULL;
2130 static int ipv6_route_yield(struct fib6_walker *w)
2132 struct ipv6_route_iter *iter = w->args;
2138 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2140 if (!iter->skip && iter->w.leaf)
2142 } while (iter->w.leaf);
2147 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2150 memset(&iter->w, 0, sizeof(iter->w));
2151 iter->w.func = ipv6_route_yield;
2152 iter->w.root = &iter->tbl->tb6_root;
2153 iter->w.state = FWS_INIT;
2154 iter->w.node = iter->w.root;
2155 iter->w.args = iter;
2156 iter->sernum = iter->w.root->fn_sernum;
2157 INIT_LIST_HEAD(&iter->w.lh);
2158 fib6_walker_link(net, &iter->w);
2161 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2165 struct hlist_node *node;
2168 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2169 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2175 while (!node && h < FIB6_TABLE_HASHSZ) {
2176 node = rcu_dereference_bh(
2177 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2179 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2182 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2184 if (iter->sernum != iter->w.root->fn_sernum) {
2185 iter->sernum = iter->w.root->fn_sernum;
2186 iter->w.state = FWS_INIT;
2187 iter->w.node = iter->w.root;
2188 WARN_ON(iter->w.skip);
2189 iter->w.skip = iter->w.count;
2193 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2197 struct net *net = seq_file_net(seq);
2198 struct ipv6_route_iter *iter = seq->private;
2203 n = ((struct rt6_info *)v)->dst.rt6_next;
2210 ipv6_route_check_sernum(iter);
2211 read_lock(&iter->tbl->tb6_lock);
2212 r = fib6_walk_continue(&iter->w);
2213 read_unlock(&iter->tbl->tb6_lock);
2217 return iter->w.leaf;
2219 fib6_walker_unlink(net, &iter->w);
2222 fib6_walker_unlink(net, &iter->w);
2224 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2228 ipv6_route_seq_setup_walk(iter, net);
2232 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2235 struct net *net = seq_file_net(seq);
2236 struct ipv6_route_iter *iter = seq->private;
2239 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2243 ipv6_route_seq_setup_walk(iter, net);
2244 return ipv6_route_seq_next(seq, NULL, pos);
2250 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2252 struct fib6_walker *w = &iter->w;
2253 return w->node && !(w->state == FWS_U && w->node == w->root);
2256 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2259 struct net *net = seq_file_net(seq);
2260 struct ipv6_route_iter *iter = seq->private;
2262 if (ipv6_route_iter_active(iter))
2263 fib6_walker_unlink(net, &iter->w);
2265 rcu_read_unlock_bh();
2268 static const struct seq_operations ipv6_route_seq_ops = {
2269 .start = ipv6_route_seq_start,
2270 .next = ipv6_route_seq_next,
2271 .stop = ipv6_route_seq_stop,
2272 .show = ipv6_route_seq_show
2275 int ipv6_route_open(struct inode *inode, struct file *file)
2277 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2278 sizeof(struct ipv6_route_iter));
2281 #endif /* CONFIG_PROC_FS */