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>
37 #include <net/ip6_fib.h>
38 #include <net/ip6_route.h>
43 #define RT6_TRACE(x...) pr_debug(x)
45 #define RT6_TRACE(x...) do { ; } while (0)
48 static struct kmem_cache *fib6_node_kmem __read_mostly;
53 int (*func)(struct rt6_info *, void *arg);
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 net *net, struct fib6_walker *w);
68 static int fib6_walk_continue(struct fib6_walker *w);
71 * A routing update causes an increase of the serial number on the
72 * affected subtree. This allows for cached routes to be asynchronously
73 * tested when modifications are made to the destination cache as a
74 * result of redirects, path MTU changes, etc.
77 static void fib6_gc_timer_cb(unsigned long arg);
79 #define FOR_WALKERS(net, w) \
80 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
82 static void fib6_walker_link(struct net *net, struct fib6_walker *w)
84 write_lock_bh(&net->ipv6.fib6_walker_lock);
85 list_add(&w->lh, &net->ipv6.fib6_walkers);
86 write_unlock_bh(&net->ipv6.fib6_walker_lock);
89 static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
91 write_lock_bh(&net->ipv6.fib6_walker_lock);
93 write_unlock_bh(&net->ipv6.fib6_walker_lock);
96 static int fib6_new_sernum(struct net *net)
101 old = atomic_read(&net->ipv6.fib6_sernum);
102 new = old < INT_MAX ? old + 1 : 1;
103 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
109 FIB6_NO_SERNUM_CHANGE = 0,
113 * Auxiliary address test functions for the radix tree.
115 * These assume a 32bit processor (although it will work on
122 #if defined(__LITTLE_ENDIAN)
123 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
125 # define BITOP_BE32_SWIZZLE 0
128 static __be32 addr_bit_set(const void *token, int fn_bit)
130 const __be32 *addr = token;
133 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
134 * is optimized version of
135 * htonl(1 << ((~fn_bit)&0x1F))
136 * See include/asm-generic/bitops/le.h.
138 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
142 static struct fib6_node *node_alloc(void)
144 struct fib6_node *fn;
146 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
151 static void node_free_immediate(struct fib6_node *fn)
153 kmem_cache_free(fib6_node_kmem, fn);
156 static void node_free_rcu(struct rcu_head *head)
158 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
160 kmem_cache_free(fib6_node_kmem, fn);
163 static void node_free(struct fib6_node *fn)
165 call_rcu(&fn->rcu, node_free_rcu);
168 static void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
172 if (!non_pcpu_rt->rt6i_pcpu)
175 for_each_possible_cpu(cpu) {
176 struct rt6_info **ppcpu_rt;
177 struct rt6_info *pcpu_rt;
179 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
182 dst_dev_put(&pcpu_rt->dst);
183 dst_release(&pcpu_rt->dst);
188 free_percpu(non_pcpu_rt->rt6i_pcpu);
189 non_pcpu_rt->rt6i_pcpu = NULL;
192 static void rt6_release(struct rt6_info *rt)
194 if (atomic_dec_and_test(&rt->rt6i_ref)) {
196 dst_dev_put(&rt->dst);
197 dst_release(&rt->dst);
201 static void fib6_link_table(struct net *net, struct fib6_table *tb)
206 * Initialize table lock at a single place to give lockdep a key,
207 * tables aren't visible prior to being linked to the list.
209 rwlock_init(&tb->tb6_lock);
211 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
214 * No protection necessary, this is the only list mutatation
215 * operation, tables never disappear once they exist.
217 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
220 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
222 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
224 struct fib6_table *table;
226 table = kzalloc(sizeof(*table), GFP_ATOMIC);
229 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
230 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
231 inet_peer_base_init(&table->tb6_peers);
237 struct fib6_table *fib6_new_table(struct net *net, u32 id)
239 struct fib6_table *tb;
243 tb = fib6_get_table(net, id);
247 tb = fib6_alloc_table(net, id);
249 fib6_link_table(net, tb);
253 EXPORT_SYMBOL_GPL(fib6_new_table);
255 struct fib6_table *fib6_get_table(struct net *net, u32 id)
257 struct fib6_table *tb;
258 struct hlist_head *head;
263 h = id & (FIB6_TABLE_HASHSZ - 1);
265 head = &net->ipv6.fib_table_hash[h];
266 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
267 if (tb->tb6_id == id) {
276 EXPORT_SYMBOL_GPL(fib6_get_table);
278 static void __net_init fib6_tables_init(struct net *net)
280 fib6_link_table(net, net->ipv6.fib6_main_tbl);
281 fib6_link_table(net, net->ipv6.fib6_local_tbl);
285 struct fib6_table *fib6_new_table(struct net *net, u32 id)
287 return fib6_get_table(net, id);
290 struct fib6_table *fib6_get_table(struct net *net, u32 id)
292 return net->ipv6.fib6_main_tbl;
295 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
296 int flags, pol_lookup_t lookup)
300 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
301 if (rt->dst.error == -EAGAIN) {
303 rt = net->ipv6.ip6_null_entry;
310 static void __net_init fib6_tables_init(struct net *net)
312 fib6_link_table(net, net->ipv6.fib6_main_tbl);
317 static int fib6_dump_node(struct fib6_walker *w)
322 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
323 res = rt6_dump_route(rt, w->args);
325 /* Frame is full, suspend walking */
330 /* Multipath routes are dumped in one route with the
331 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
332 * last sibling of this route (no need to dump the
333 * sibling routes again)
335 if (rt->rt6i_nsiblings)
336 rt = list_last_entry(&rt->rt6i_siblings,
344 static void fib6_dump_end(struct netlink_callback *cb)
346 struct net *net = sock_net(cb->skb->sk);
347 struct fib6_walker *w = (void *)cb->args[2];
352 fib6_walker_unlink(net, w);
357 cb->done = (void *)cb->args[3];
361 static int fib6_dump_done(struct netlink_callback *cb)
364 return cb->done ? cb->done(cb) : 0;
367 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
368 struct netlink_callback *cb)
370 struct net *net = sock_net(skb->sk);
371 struct fib6_walker *w;
374 w = (void *)cb->args[2];
375 w->root = &table->tb6_root;
377 if (cb->args[4] == 0) {
381 read_lock_bh(&table->tb6_lock);
382 res = fib6_walk(net, w);
383 read_unlock_bh(&table->tb6_lock);
386 cb->args[5] = w->root->fn_sernum;
389 if (cb->args[5] != w->root->fn_sernum) {
390 /* Begin at the root if the tree changed */
391 cb->args[5] = w->root->fn_sernum;
398 read_lock_bh(&table->tb6_lock);
399 res = fib6_walk_continue(w);
400 read_unlock_bh(&table->tb6_lock);
402 fib6_walker_unlink(net, w);
410 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
412 struct net *net = sock_net(skb->sk);
414 unsigned int e = 0, s_e;
415 struct rt6_rtnl_dump_arg arg;
416 struct fib6_walker *w;
417 struct fib6_table *tb;
418 struct hlist_head *head;
424 w = (void *)cb->args[2];
428 * 1. hook callback destructor.
430 cb->args[3] = (long)cb->done;
431 cb->done = fib6_dump_done;
434 * 2. allocate and initialize walker.
436 w = kzalloc(sizeof(*w), GFP_ATOMIC);
439 w->func = fib6_dump_node;
440 cb->args[2] = (long)w;
449 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
451 head = &net->ipv6.fib_table_hash[h];
452 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
455 res = fib6_dump_table(tb, skb, cb);
467 res = res < 0 ? res : skb->len;
476 * return the appropriate node for a routing tree "add" operation
477 * by either creating and inserting or by returning an existing
481 static struct fib6_node *fib6_add_1(struct fib6_node *root,
482 struct in6_addr *addr, int plen,
483 int offset, int allow_create,
484 int replace_required, int sernum,
485 struct netlink_ext_ack *extack)
487 struct fib6_node *fn, *in, *ln;
488 struct fib6_node *pn = NULL;
493 RT6_TRACE("fib6_add_1\n");
495 /* insert node in tree */
500 key = (struct rt6key *)((u8 *)fn->leaf + offset);
505 if (plen < fn->fn_bit ||
506 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
508 if (replace_required) {
509 NL_SET_ERR_MSG(extack,
510 "Can not replace route - no match found");
511 pr_warn("Can't replace route, no match found\n");
512 return ERR_PTR(-ENOENT);
514 pr_warn("NLM_F_CREATE should be set when creating new route\n");
523 if (plen == fn->fn_bit) {
524 /* clean up an intermediate node */
525 if (!(fn->fn_flags & RTN_RTINFO)) {
526 rt6_release(fn->leaf);
530 fn->fn_sernum = sernum;
536 * We have more bits to go
539 /* Try to walk down on tree. */
540 fn->fn_sernum = sernum;
541 dir = addr_bit_set(addr, fn->fn_bit);
543 fn = dir ? fn->right : fn->left;
547 /* We should not create new node because
548 * NLM_F_REPLACE was specified without NLM_F_CREATE
549 * I assume it is safe to require NLM_F_CREATE when
550 * REPLACE flag is used! Later we may want to remove the
551 * check for replace_required, because according
552 * to netlink specification, NLM_F_CREATE
553 * MUST be specified if new route is created.
554 * That would keep IPv6 consistent with IPv4
556 if (replace_required) {
557 NL_SET_ERR_MSG(extack,
558 "Can not replace route - no match found");
559 pr_warn("Can't replace route, no match found\n");
560 return ERR_PTR(-ENOENT);
562 pr_warn("NLM_F_CREATE should be set when creating new route\n");
565 * We walked to the bottom of tree.
566 * Create new leaf node without children.
572 return ERR_PTR(-ENOMEM);
576 ln->fn_sernum = sernum;
588 * split since we don't have a common prefix anymore or
589 * we have a less significant route.
590 * we've to insert an intermediate node on the list
591 * this new node will point to the one we need to create
597 /* find 1st bit in difference between the 2 addrs.
599 See comment in __ipv6_addr_diff: bit may be an invalid value,
600 but if it is >= plen, the value is ignored in any case.
603 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
608 * (new leaf node)[ln] (old node)[fn]
616 node_free_immediate(in);
618 node_free_immediate(ln);
619 return ERR_PTR(-ENOMEM);
623 * new intermediate node.
625 * be off since that an address that chooses one of
626 * the branches would not match less specific routes
627 * in the other branch
634 atomic_inc(&in->leaf->rt6i_ref);
636 in->fn_sernum = sernum;
638 /* update parent pointer */
649 ln->fn_sernum = sernum;
651 if (addr_bit_set(addr, bit)) {
658 } else { /* plen <= bit */
661 * (new leaf node)[ln]
663 * (old node)[fn] NULL
669 return ERR_PTR(-ENOMEM);
675 ln->fn_sernum = sernum;
682 if (addr_bit_set(&key->addr, plen))
692 static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
694 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
698 static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
702 for (i = 0; i < RTAX_MAX; i++) {
703 if (test_bit(i, mxc->mx_valid))
708 static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
713 if (dst->flags & DST_HOST) {
714 u32 *mp = dst_metrics_write_ptr(dst);
719 fib6_copy_metrics(mp, mxc);
721 dst_init_metrics(dst, mxc->mx, false);
723 /* We've stolen mx now. */
730 static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
733 if (atomic_read(&rt->rt6i_ref) != 1) {
734 /* This route is used as dummy address holder in some split
735 * nodes. It is not leaked, but it still holds other resources,
736 * which must be released in time. So, scan ascendant nodes
737 * and replace dummy references to this route with references
738 * to still alive ones.
741 if (!(fn->fn_flags & RTN_RTINFO) && fn->leaf == rt) {
742 fn->leaf = fib6_find_prefix(net, fn);
743 atomic_inc(&fn->leaf->rt6i_ref);
748 /* No more references are possible at this point. */
749 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
754 * Insert routing information in a node.
757 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
758 struct nl_info *info, struct mx6_config *mxc)
760 struct rt6_info *iter = NULL;
761 struct rt6_info **ins;
762 struct rt6_info **fallback_ins = NULL;
763 int replace = (info->nlh &&
764 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
765 int add = (!info->nlh ||
766 (info->nlh->nlmsg_flags & NLM_F_CREATE));
768 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
769 u16 nlflags = NLM_F_EXCL;
772 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
773 nlflags |= NLM_F_APPEND;
777 for (iter = fn->leaf; iter; iter = iter->dst.rt6_next) {
779 * Search for duplicates
782 if (iter->rt6i_metric == rt->rt6i_metric) {
784 * Same priority level
787 (info->nlh->nlmsg_flags & NLM_F_EXCL))
790 nlflags &= ~NLM_F_EXCL;
792 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
797 fallback_ins = fallback_ins ?: ins;
801 if (rt6_duplicate_nexthop(iter, rt)) {
802 if (rt->rt6i_nsiblings)
803 rt->rt6i_nsiblings = 0;
804 if (!(iter->rt6i_flags & RTF_EXPIRES))
806 if (!(rt->rt6i_flags & RTF_EXPIRES))
807 rt6_clean_expires(iter);
809 rt6_set_expires(iter, rt->dst.expires);
810 iter->rt6i_pmtu = rt->rt6i_pmtu;
813 /* If we have the same destination and the same metric,
814 * but not the same gateway, then the route we try to
815 * add is sibling to this route, increment our counter
816 * of siblings, and later we will add our route to the
818 * Only static routes (which don't have flag
819 * RTF_EXPIRES) are used for ECMPv6.
821 * To avoid long list, we only had siblings if the
822 * route have a gateway.
825 rt6_qualify_for_ecmp(iter))
826 rt->rt6i_nsiblings++;
829 if (iter->rt6i_metric > rt->rt6i_metric)
833 ins = &iter->dst.rt6_next;
836 if (fallback_ins && !found) {
837 /* No ECMP-able route found, replace first non-ECMP one */
843 /* Reset round-robin state, if necessary */
844 if (ins == &fn->leaf)
847 /* Link this route to others same route. */
848 if (rt->rt6i_nsiblings) {
849 unsigned int rt6i_nsiblings;
850 struct rt6_info *sibling, *temp_sibling;
852 /* Find the first route that have the same metric */
855 if (sibling->rt6i_metric == rt->rt6i_metric &&
856 rt6_qualify_for_ecmp(sibling)) {
857 list_add_tail(&rt->rt6i_siblings,
858 &sibling->rt6i_siblings);
861 sibling = sibling->dst.rt6_next;
863 /* For each sibling in the list, increment the counter of
864 * siblings. BUG() if counters does not match, list of siblings
868 list_for_each_entry_safe(sibling, temp_sibling,
869 &rt->rt6i_siblings, rt6i_siblings) {
870 sibling->rt6i_nsiblings++;
871 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
874 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
882 pr_warn("NLM_F_CREATE should be set when creating new route\n");
885 nlflags |= NLM_F_CREATE;
886 err = fib6_commit_metrics(&rt->dst, mxc);
890 rt->dst.rt6_next = iter;
892 rcu_assign_pointer(rt->rt6i_node, fn);
893 atomic_inc(&rt->rt6i_ref);
894 if (!info->skip_notify)
895 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
896 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
898 if (!(fn->fn_flags & RTN_RTINFO)) {
899 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
900 fn->fn_flags |= RTN_RTINFO;
909 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
913 err = fib6_commit_metrics(&rt->dst, mxc);
918 rcu_assign_pointer(rt->rt6i_node, fn);
919 rt->dst.rt6_next = iter->dst.rt6_next;
920 atomic_inc(&rt->rt6i_ref);
921 if (!info->skip_notify)
922 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
923 if (!(fn->fn_flags & RTN_RTINFO)) {
924 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
925 fn->fn_flags |= RTN_RTINFO;
927 nsiblings = iter->rt6i_nsiblings;
928 fib6_purge_rt(iter, fn, info->nl_net);
929 if (fn->rr_ptr == iter)
934 /* Replacing an ECMP route, remove all siblings */
935 ins = &rt->dst.rt6_next;
938 if (iter->rt6i_metric > rt->rt6i_metric)
940 if (rt6_qualify_for_ecmp(iter)) {
941 *ins = iter->dst.rt6_next;
942 fib6_purge_rt(iter, fn, info->nl_net);
943 if (fn->rr_ptr == iter)
948 ins = &iter->dst.rt6_next;
952 WARN_ON(nsiblings != 0);
959 static void fib6_start_gc(struct net *net, struct rt6_info *rt)
961 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
962 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
963 mod_timer(&net->ipv6.ip6_fib_timer,
964 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
967 void fib6_force_start_gc(struct net *net)
969 if (!timer_pending(&net->ipv6.ip6_fib_timer))
970 mod_timer(&net->ipv6.ip6_fib_timer,
971 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
975 * Add routing information to the routing tree.
976 * <destination addr>/<source addr>
977 * with source addr info in sub-trees
980 int fib6_add(struct fib6_node *root, struct rt6_info *rt,
981 struct nl_info *info, struct mx6_config *mxc,
982 struct netlink_ext_ack *extack)
984 struct fib6_node *fn, *pn = NULL;
986 int allow_create = 1;
987 int replace_required = 0;
988 int sernum = fib6_new_sernum(info->nl_net);
990 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
994 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
996 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
997 replace_required = 1;
999 if (!allow_create && !replace_required)
1000 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1002 fn = fib6_add_1(root, &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
1003 offsetof(struct rt6_info, rt6i_dst), allow_create,
1004 replace_required, sernum, extack);
1013 #ifdef CONFIG_IPV6_SUBTREES
1014 if (rt->rt6i_src.plen) {
1015 struct fib6_node *sn;
1018 struct fib6_node *sfn;
1030 /* Create subtree root node */
1035 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
1036 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
1037 sfn->fn_flags = RTN_ROOT;
1038 sfn->fn_sernum = sernum;
1040 /* Now add the first leaf node to new subtree */
1042 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
1044 offsetof(struct rt6_info, rt6i_src),
1045 allow_create, replace_required, sernum,
1049 /* If it is failed, discard just allocated
1050 root, and then (in failure) stale node
1053 node_free_immediate(sfn);
1058 /* Now link new subtree to main tree */
1062 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
1064 offsetof(struct rt6_info, rt6i_src),
1065 allow_create, replace_required, sernum,
1076 atomic_inc(&rt->rt6i_ref);
1082 err = fib6_add_rt2node(fn, rt, info, mxc);
1084 fib6_start_gc(info->nl_net, rt);
1085 if (!(rt->rt6i_flags & RTF_CACHE))
1086 fib6_prune_clones(info->nl_net, pn);
1091 #ifdef CONFIG_IPV6_SUBTREES
1093 * If fib6_add_1 has cleared the old leaf pointer in the
1094 * super-tree leaf node we have to find a new one for it.
1096 if (pn != fn && pn->leaf == rt) {
1098 atomic_dec(&rt->rt6i_ref);
1100 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
1101 pn->leaf = fib6_find_prefix(info->nl_net, pn);
1104 WARN_ON(pn->leaf == NULL);
1105 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
1108 atomic_inc(&pn->leaf->rt6i_ref);
1116 /* fn->leaf could be NULL if fn is an intermediate node and we
1117 * failed to add the new route to it in both subtree creation
1118 * failure and fib6_add_rt2node() failure case.
1119 * In both cases, fib6_repair_tree() should be called to fix
1122 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
1123 fib6_repair_tree(info->nl_net, fn);
1124 /* Always release dst as dst->__refcnt is guaranteed
1125 * to be taken before entering this function
1127 dst_release_immediate(&rt->dst);
1132 * Routing tree lookup
1136 struct lookup_args {
1137 int offset; /* key offset on rt6_info */
1138 const struct in6_addr *addr; /* search key */
1141 static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1142 struct lookup_args *args)
1144 struct fib6_node *fn;
1147 if (unlikely(args->offset == 0))
1157 struct fib6_node *next;
1159 dir = addr_bit_set(args->addr, fn->fn_bit);
1161 next = dir ? fn->right : fn->left;
1171 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
1174 key = (struct rt6key *) ((u8 *) fn->leaf +
1177 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1178 #ifdef CONFIG_IPV6_SUBTREES
1180 struct fib6_node *sfn;
1181 sfn = fib6_lookup_1(fn->subtree,
1188 if (fn->fn_flags & RTN_RTINFO)
1192 #ifdef CONFIG_IPV6_SUBTREES
1195 if (fn->fn_flags & RTN_ROOT)
1204 struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1205 const struct in6_addr *saddr)
1207 struct fib6_node *fn;
1208 struct lookup_args args[] = {
1210 .offset = offsetof(struct rt6_info, rt6i_dst),
1213 #ifdef CONFIG_IPV6_SUBTREES
1215 .offset = offsetof(struct rt6_info, rt6i_src),
1220 .offset = 0, /* sentinel */
1224 fn = fib6_lookup_1(root, daddr ? args : args + 1);
1225 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1232 * Get node with specified destination prefix (and source prefix,
1233 * if subtrees are used)
1237 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1238 const struct in6_addr *addr,
1239 int plen, int offset)
1241 struct fib6_node *fn;
1243 for (fn = root; fn ; ) {
1244 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
1249 if (plen < fn->fn_bit ||
1250 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1253 if (plen == fn->fn_bit)
1257 * We have more bits to go
1259 if (addr_bit_set(addr, fn->fn_bit))
1267 struct fib6_node *fib6_locate(struct fib6_node *root,
1268 const struct in6_addr *daddr, int dst_len,
1269 const struct in6_addr *saddr, int src_len)
1271 struct fib6_node *fn;
1273 fn = fib6_locate_1(root, daddr, dst_len,
1274 offsetof(struct rt6_info, rt6i_dst));
1276 #ifdef CONFIG_IPV6_SUBTREES
1278 WARN_ON(saddr == NULL);
1279 if (fn && fn->subtree)
1280 fn = fib6_locate_1(fn->subtree, saddr, src_len,
1281 offsetof(struct rt6_info, rt6i_src));
1285 if (fn && fn->fn_flags & RTN_RTINFO)
1297 static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
1299 if (fn->fn_flags & RTN_ROOT)
1300 return net->ipv6.ip6_null_entry;
1304 return fn->left->leaf;
1306 return fn->right->leaf;
1308 fn = FIB6_SUBTREE(fn);
1314 * Called to trim the tree of intermediate nodes when possible. "fn"
1315 * is the node we want to try and remove.
1318 static struct fib6_node *fib6_repair_tree(struct net *net,
1319 struct fib6_node *fn)
1323 struct fib6_node *child, *pn;
1324 struct fib6_walker *w;
1328 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1331 WARN_ON(fn->fn_flags & RTN_RTINFO);
1332 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1338 child = fn->right, children |= 1;
1340 child = fn->left, children |= 2;
1342 if (children == 3 || FIB6_SUBTREE(fn)
1343 #ifdef CONFIG_IPV6_SUBTREES
1344 /* Subtree root (i.e. fn) may have one child */
1345 || (children && fn->fn_flags & RTN_ROOT)
1348 fn->leaf = fib6_find_prefix(net, fn);
1352 fn->leaf = net->ipv6.ip6_null_entry;
1355 atomic_inc(&fn->leaf->rt6i_ref);
1360 #ifdef CONFIG_IPV6_SUBTREES
1361 if (FIB6_SUBTREE(pn) == fn) {
1362 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1363 FIB6_SUBTREE(pn) = NULL;
1366 WARN_ON(fn->fn_flags & RTN_ROOT);
1368 if (pn->right == fn)
1370 else if (pn->left == fn)
1379 #ifdef CONFIG_IPV6_SUBTREES
1383 read_lock(&net->ipv6.fib6_walker_lock);
1384 FOR_WALKERS(net, w) {
1386 if (w->root == fn) {
1387 w->root = w->node = NULL;
1388 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1389 } else if (w->node == fn) {
1390 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1395 if (w->root == fn) {
1397 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1399 if (w->node == fn) {
1402 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1403 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1405 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1406 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1411 read_unlock(&net->ipv6.fib6_walker_lock);
1414 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1417 rt6_release(pn->leaf);
1423 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
1424 struct nl_info *info)
1426 struct fib6_walker *w;
1427 struct rt6_info *rt = *rtp;
1428 struct net *net = info->nl_net;
1430 RT6_TRACE("fib6_del_route\n");
1433 *rtp = rt->dst.rt6_next;
1434 rt->rt6i_node = NULL;
1435 net->ipv6.rt6_stats->fib_rt_entries--;
1436 net->ipv6.rt6_stats->fib_discarded_routes++;
1438 /* Reset round-robin state, if necessary */
1439 if (fn->rr_ptr == rt)
1442 /* Remove this entry from other siblings */
1443 if (rt->rt6i_nsiblings) {
1444 struct rt6_info *sibling, *next_sibling;
1446 list_for_each_entry_safe(sibling, next_sibling,
1447 &rt->rt6i_siblings, rt6i_siblings)
1448 sibling->rt6i_nsiblings--;
1449 rt->rt6i_nsiblings = 0;
1450 list_del_init(&rt->rt6i_siblings);
1453 /* Adjust walkers */
1454 read_lock(&net->ipv6.fib6_walker_lock);
1455 FOR_WALKERS(net, w) {
1456 if (w->state == FWS_C && w->leaf == rt) {
1457 RT6_TRACE("walker %p adjusted by delroute\n", w);
1458 w->leaf = rt->dst.rt6_next;
1463 read_unlock(&net->ipv6.fib6_walker_lock);
1465 rt->dst.rt6_next = NULL;
1467 /* If it was last route, expunge its radix tree node */
1469 fn->fn_flags &= ~RTN_RTINFO;
1470 net->ipv6.rt6_stats->fib_route_nodes--;
1471 fn = fib6_repair_tree(net, fn);
1474 fib6_purge_rt(rt, fn, net);
1476 if (!info->skip_notify)
1477 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
1481 int fib6_del(struct rt6_info *rt, struct nl_info *info)
1483 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1484 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1485 struct net *net = info->nl_net;
1486 struct rt6_info **rtp;
1489 if (rt->dst.obsolete > 0) {
1494 if (!fn || rt == net->ipv6.ip6_null_entry)
1497 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1499 if (!(rt->rt6i_flags & RTF_CACHE)) {
1500 struct fib6_node *pn = fn;
1501 #ifdef CONFIG_IPV6_SUBTREES
1502 /* clones of this route might be in another subtree */
1503 if (rt->rt6i_src.plen) {
1504 while (!(pn->fn_flags & RTN_ROOT))
1509 fib6_prune_clones(info->nl_net, pn);
1513 * Walk the leaf entries looking for ourself
1516 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->dst.rt6_next) {
1518 fib6_del_route(fn, rtp, info);
1526 * Tree traversal function.
1528 * Certainly, it is not interrupt safe.
1529 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1530 * It means, that we can modify tree during walking
1531 * and use this function for garbage collection, clone pruning,
1532 * cleaning tree when a device goes down etc. etc.
1534 * It guarantees that every node will be traversed,
1535 * and that it will be traversed only once.
1537 * Callback function w->func may return:
1538 * 0 -> continue walking.
1539 * positive value -> walking is suspended (used by tree dumps,
1540 * and probably by gc, if it will be split to several slices)
1541 * negative value -> terminate walking.
1543 * The function itself returns:
1544 * 0 -> walk is complete.
1545 * >0 -> walk is incomplete (i.e. suspended)
1546 * <0 -> walk is terminated by an error.
1549 static int fib6_walk_continue(struct fib6_walker *w)
1551 struct fib6_node *fn, *pn;
1558 if (w->prune && fn != w->root &&
1559 fn->fn_flags & RTN_RTINFO && w->state < FWS_C) {
1564 #ifdef CONFIG_IPV6_SUBTREES
1566 if (FIB6_SUBTREE(fn)) {
1567 w->node = FIB6_SUBTREE(fn);
1575 w->state = FWS_INIT;
1581 w->node = fn->right;
1582 w->state = FWS_INIT;
1588 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1610 #ifdef CONFIG_IPV6_SUBTREES
1611 if (FIB6_SUBTREE(pn) == fn) {
1612 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1617 if (pn->left == fn) {
1621 if (pn->right == fn) {
1623 w->leaf = w->node->leaf;
1633 static int fib6_walk(struct net *net, struct fib6_walker *w)
1637 w->state = FWS_INIT;
1640 fib6_walker_link(net, w);
1641 res = fib6_walk_continue(w);
1643 fib6_walker_unlink(net, w);
1647 static int fib6_clean_node(struct fib6_walker *w)
1650 struct rt6_info *rt;
1651 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1652 struct nl_info info = {
1656 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1657 w->node->fn_sernum != c->sernum)
1658 w->node->fn_sernum = c->sernum;
1661 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1666 for (rt = w->leaf; rt; rt = rt->dst.rt6_next) {
1667 res = c->func(rt, c->arg);
1670 res = fib6_del(rt, &info);
1673 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1675 rcu_access_pointer(rt->rt6i_node),
1689 * Convenient frontend to tree walker.
1691 * func is called on each route.
1692 * It may return -1 -> delete this route.
1693 * 0 -> continue walking
1695 * prune==1 -> only immediate children of node (certainly,
1696 * ignoring pure split nodes) will be scanned.
1699 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
1700 int (*func)(struct rt6_info *, void *arg),
1701 bool prune, int sernum, void *arg)
1703 struct fib6_cleaner c;
1706 c.w.func = fib6_clean_node;
1715 fib6_walk(net, &c.w);
1718 static void __fib6_clean_all(struct net *net,
1719 int (*func)(struct rt6_info *, void *),
1720 int sernum, void *arg)
1722 struct fib6_table *table;
1723 struct hlist_head *head;
1727 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
1728 head = &net->ipv6.fib_table_hash[h];
1729 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
1730 write_lock_bh(&table->tb6_lock);
1731 fib6_clean_tree(net, &table->tb6_root,
1732 func, false, sernum, arg);
1733 write_unlock_bh(&table->tb6_lock);
1739 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1742 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1745 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1747 if (rt->rt6i_flags & RTF_CACHE) {
1748 RT6_TRACE("pruning clone %p\n", rt);
1755 static void fib6_prune_clones(struct net *net, struct fib6_node *fn)
1757 fib6_clean_tree(net, fn, fib6_prune_clone, true,
1758 FIB6_NO_SERNUM_CHANGE, NULL);
1761 static void fib6_flush_trees(struct net *net)
1763 int new_sernum = fib6_new_sernum(net);
1765 __fib6_clean_all(net, NULL, new_sernum, NULL);
1769 * Garbage collection
1778 static int fib6_age(struct rt6_info *rt, void *arg)
1780 struct fib6_gc_args *gc_args = arg;
1781 unsigned long now = jiffies;
1784 * check addrconf expiration here.
1785 * Routes are expired even if they are in use.
1787 * Also age clones. Note, that clones are aged out
1788 * only if they are not in use now.
1791 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
1792 if (time_after(now, rt->dst.expires)) {
1793 RT6_TRACE("expiring %p\n", rt);
1797 } else if (rt->rt6i_flags & RTF_CACHE) {
1798 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout))
1799 rt->dst.obsolete = DST_OBSOLETE_KILL;
1800 if (atomic_read(&rt->dst.__refcnt) == 1 &&
1801 rt->dst.obsolete == DST_OBSOLETE_KILL) {
1802 RT6_TRACE("aging clone %p\n", rt);
1804 } else if (rt->rt6i_flags & RTF_GATEWAY) {
1805 struct neighbour *neigh;
1806 __u8 neigh_flags = 0;
1808 neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1810 neigh_flags = neigh->flags;
1811 neigh_release(neigh);
1813 if (!(neigh_flags & NTF_ROUTER)) {
1814 RT6_TRACE("purging route %p via non-router but gateway\n",
1825 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
1827 struct fib6_gc_args gc_args;
1831 spin_lock_bh(&net->ipv6.fib6_gc_lock);
1832 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
1833 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
1836 gc_args.timeout = expires ? (int)expires :
1837 net->ipv6.sysctl.ip6_rt_gc_interval;
1840 fib6_clean_all(net, fib6_age, &gc_args);
1842 net->ipv6.ip6_rt_last_gc = now;
1845 mod_timer(&net->ipv6.ip6_fib_timer,
1847 + net->ipv6.sysctl.ip6_rt_gc_interval));
1849 del_timer(&net->ipv6.ip6_fib_timer);
1850 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
1853 static void fib6_gc_timer_cb(unsigned long arg)
1855 fib6_run_gc(0, (struct net *)arg, true);
1858 static int __net_init fib6_net_init(struct net *net)
1860 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
1862 spin_lock_init(&net->ipv6.fib6_gc_lock);
1863 rwlock_init(&net->ipv6.fib6_walker_lock);
1864 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
1865 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
1867 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1868 if (!net->ipv6.rt6_stats)
1871 /* Avoid false sharing : Use at least a full cache line */
1872 size = max_t(size_t, size, L1_CACHE_BYTES);
1874 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
1875 if (!net->ipv6.fib_table_hash)
1878 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1880 if (!net->ipv6.fib6_main_tbl)
1881 goto out_fib_table_hash;
1883 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
1884 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1885 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1886 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1887 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
1889 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1890 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1892 if (!net->ipv6.fib6_local_tbl)
1893 goto out_fib6_main_tbl;
1894 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
1895 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
1896 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1897 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1898 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
1900 fib6_tables_init(net);
1904 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1906 kfree(net->ipv6.fib6_main_tbl);
1909 kfree(net->ipv6.fib_table_hash);
1911 kfree(net->ipv6.rt6_stats);
1916 static void fib6_net_exit(struct net *net)
1918 rt6_ifdown(net, NULL);
1919 del_timer_sync(&net->ipv6.ip6_fib_timer);
1921 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1922 inetpeer_invalidate_tree(&net->ipv6.fib6_local_tbl->tb6_peers);
1923 kfree(net->ipv6.fib6_local_tbl);
1925 inetpeer_invalidate_tree(&net->ipv6.fib6_main_tbl->tb6_peers);
1926 kfree(net->ipv6.fib6_main_tbl);
1927 kfree(net->ipv6.fib_table_hash);
1928 kfree(net->ipv6.rt6_stats);
1931 static struct pernet_operations fib6_net_ops = {
1932 .init = fib6_net_init,
1933 .exit = fib6_net_exit,
1936 int __init fib6_init(void)
1940 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1941 sizeof(struct fib6_node),
1942 0, SLAB_HWCACHE_ALIGN,
1944 if (!fib6_node_kmem)
1947 ret = register_pernet_subsys(&fib6_net_ops);
1949 goto out_kmem_cache_create;
1951 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
1954 goto out_unregister_subsys;
1956 __fib6_flush_trees = fib6_flush_trees;
1960 out_unregister_subsys:
1961 unregister_pernet_subsys(&fib6_net_ops);
1962 out_kmem_cache_create:
1963 kmem_cache_destroy(fib6_node_kmem);
1967 void fib6_gc_cleanup(void)
1969 unregister_pernet_subsys(&fib6_net_ops);
1970 kmem_cache_destroy(fib6_node_kmem);
1973 #ifdef CONFIG_PROC_FS
1975 struct ipv6_route_iter {
1976 struct seq_net_private p;
1977 struct fib6_walker w;
1979 struct fib6_table *tbl;
1983 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1985 struct rt6_info *rt = v;
1986 struct ipv6_route_iter *iter = seq->private;
1988 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1990 #ifdef CONFIG_IPV6_SUBTREES
1991 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1993 seq_puts(seq, "00000000000000000000000000000000 00 ");
1995 if (rt->rt6i_flags & RTF_GATEWAY)
1996 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1998 seq_puts(seq, "00000000000000000000000000000000");
2000 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2001 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2002 rt->dst.__use, rt->rt6i_flags,
2003 rt->dst.dev ? rt->dst.dev->name : "");
2004 iter->w.leaf = NULL;
2008 static int ipv6_route_yield(struct fib6_walker *w)
2010 struct ipv6_route_iter *iter = w->args;
2016 iter->w.leaf = iter->w.leaf->dst.rt6_next;
2018 if (!iter->skip && iter->w.leaf)
2020 } while (iter->w.leaf);
2025 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2028 memset(&iter->w, 0, sizeof(iter->w));
2029 iter->w.func = ipv6_route_yield;
2030 iter->w.root = &iter->tbl->tb6_root;
2031 iter->w.state = FWS_INIT;
2032 iter->w.node = iter->w.root;
2033 iter->w.args = iter;
2034 iter->sernum = iter->w.root->fn_sernum;
2035 INIT_LIST_HEAD(&iter->w.lh);
2036 fib6_walker_link(net, &iter->w);
2039 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2043 struct hlist_node *node;
2046 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2047 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2053 while (!node && h < FIB6_TABLE_HASHSZ) {
2054 node = rcu_dereference_bh(
2055 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2057 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2060 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2062 if (iter->sernum != iter->w.root->fn_sernum) {
2063 iter->sernum = iter->w.root->fn_sernum;
2064 iter->w.state = FWS_INIT;
2065 iter->w.node = iter->w.root;
2066 WARN_ON(iter->w.skip);
2067 iter->w.skip = iter->w.count;
2071 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2075 struct net *net = seq_file_net(seq);
2076 struct ipv6_route_iter *iter = seq->private;
2081 n = ((struct rt6_info *)v)->dst.rt6_next;
2088 ipv6_route_check_sernum(iter);
2089 read_lock(&iter->tbl->tb6_lock);
2090 r = fib6_walk_continue(&iter->w);
2091 read_unlock(&iter->tbl->tb6_lock);
2095 return iter->w.leaf;
2097 fib6_walker_unlink(net, &iter->w);
2100 fib6_walker_unlink(net, &iter->w);
2102 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2106 ipv6_route_seq_setup_walk(iter, net);
2110 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2113 struct net *net = seq_file_net(seq);
2114 struct ipv6_route_iter *iter = seq->private;
2117 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2121 ipv6_route_seq_setup_walk(iter, net);
2122 return ipv6_route_seq_next(seq, NULL, pos);
2128 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2130 struct fib6_walker *w = &iter->w;
2131 return w->node && !(w->state == FWS_U && w->node == w->root);
2134 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2137 struct net *net = seq_file_net(seq);
2138 struct ipv6_route_iter *iter = seq->private;
2140 if (ipv6_route_iter_active(iter))
2141 fib6_walker_unlink(net, &iter->w);
2143 rcu_read_unlock_bh();
2146 static const struct seq_operations ipv6_route_seq_ops = {
2147 .start = ipv6_route_seq_start,
2148 .next = ipv6_route_seq_next,
2149 .stop = ipv6_route_seq_stop,
2150 .show = ipv6_route_seq_show
2153 int ipv6_route_open(struct inode *inode, struct file *file)
2155 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2156 sizeof(struct ipv6_route_iter));
2159 #endif /* CONFIG_PROC_FS */