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_rcu_free(struct rt6_info
*rt
)
170 call_rcu(&rt
->dst
.rcu_head
, dst_rcu_free
);
173 static void rt6_free_pcpu(struct rt6_info
*non_pcpu_rt
)
177 if (!non_pcpu_rt
->rt6i_pcpu
)
180 for_each_possible_cpu(cpu
) {
181 struct rt6_info
**ppcpu_rt
;
182 struct rt6_info
*pcpu_rt
;
184 ppcpu_rt
= per_cpu_ptr(non_pcpu_rt
->rt6i_pcpu
, cpu
);
187 rt6_rcu_free(pcpu_rt
);
192 free_percpu(non_pcpu_rt
->rt6i_pcpu
);
193 non_pcpu_rt
->rt6i_pcpu
= NULL
;
196 static void rt6_release(struct rt6_info
*rt
)
198 if (atomic_dec_and_test(&rt
->rt6i_ref
)) {
204 static void fib6_free_table(struct fib6_table
*table
)
206 inetpeer_invalidate_tree(&table
->tb6_peers
);
210 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
215 * Initialize table lock at a single place to give lockdep a key,
216 * tables aren't visible prior to being linked to the list.
218 rwlock_init(&tb
->tb6_lock
);
220 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
223 * No protection necessary, this is the only list mutatation
224 * operation, tables never disappear once they exist.
226 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
229 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
231 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
233 struct fib6_table
*table
;
235 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
238 table
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
239 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
240 inet_peer_base_init(&table
->tb6_peers
);
246 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
248 struct fib6_table
*tb
;
252 tb
= fib6_get_table(net
, id
);
256 tb
= fib6_alloc_table(net
, id
);
258 fib6_link_table(net
, tb
);
262 EXPORT_SYMBOL_GPL(fib6_new_table
);
264 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
266 struct fib6_table
*tb
;
267 struct hlist_head
*head
;
272 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
274 head
= &net
->ipv6
.fib_table_hash
[h
];
275 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
276 if (tb
->tb6_id
== id
) {
285 EXPORT_SYMBOL_GPL(fib6_get_table
);
287 static void __net_init
fib6_tables_init(struct net
*net
)
289 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
290 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
294 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
296 return fib6_get_table(net
, id
);
299 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
301 return net
->ipv6
.fib6_main_tbl
;
304 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
305 int flags
, pol_lookup_t lookup
)
309 rt
= lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
310 if (rt
->dst
.error
== -EAGAIN
) {
312 rt
= net
->ipv6
.ip6_null_entry
;
319 static void __net_init
fib6_tables_init(struct net
*net
)
321 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
326 static int fib6_dump_node(struct fib6_walker
*w
)
331 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
332 res
= rt6_dump_route(rt
, w
->args
);
334 /* Frame is full, suspend walking */
343 static void fib6_dump_end(struct netlink_callback
*cb
)
345 struct net
*net
= sock_net(cb
->skb
->sk
);
346 struct fib6_walker
*w
= (void *)cb
->args
[2];
351 fib6_walker_unlink(net
, w
);
356 cb
->done
= (void *)cb
->args
[3];
360 static int fib6_dump_done(struct netlink_callback
*cb
)
363 return cb
->done
? cb
->done(cb
) : 0;
366 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
367 struct netlink_callback
*cb
)
369 struct net
*net
= sock_net(skb
->sk
);
370 struct fib6_walker
*w
;
373 w
= (void *)cb
->args
[2];
374 w
->root
= &table
->tb6_root
;
376 if (cb
->args
[4] == 0) {
380 read_lock_bh(&table
->tb6_lock
);
381 res
= fib6_walk(net
, w
);
382 read_unlock_bh(&table
->tb6_lock
);
385 cb
->args
[5] = w
->root
->fn_sernum
;
388 if (cb
->args
[5] != w
->root
->fn_sernum
) {
389 /* Begin at the root if the tree changed */
390 cb
->args
[5] = w
->root
->fn_sernum
;
397 read_lock_bh(&table
->tb6_lock
);
398 res
= fib6_walk_continue(w
);
399 read_unlock_bh(&table
->tb6_lock
);
401 fib6_walker_unlink(net
, w
);
409 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
411 struct net
*net
= sock_net(skb
->sk
);
413 unsigned int e
= 0, s_e
;
414 struct rt6_rtnl_dump_arg arg
;
415 struct fib6_walker
*w
;
416 struct fib6_table
*tb
;
417 struct hlist_head
*head
;
423 w
= (void *)cb
->args
[2];
427 * 1. hook callback destructor.
429 cb
->args
[3] = (long)cb
->done
;
430 cb
->done
= fib6_dump_done
;
433 * 2. allocate and initialize walker.
435 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
438 w
->func
= fib6_dump_node
;
439 cb
->args
[2] = (long)w
;
448 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
450 head
= &net
->ipv6
.fib_table_hash
[h
];
451 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
454 res
= fib6_dump_table(tb
, skb
, cb
);
466 res
= res
< 0 ? res
: skb
->len
;
475 * return the appropriate node for a routing tree "add" operation
476 * by either creating and inserting or by returning an existing
480 static struct fib6_node
*fib6_add_1(struct fib6_node
*root
,
481 struct in6_addr
*addr
, int plen
,
482 int offset
, int allow_create
,
483 int replace_required
, int sernum
)
485 struct fib6_node
*fn
, *in
, *ln
;
486 struct fib6_node
*pn
= NULL
;
491 RT6_TRACE("fib6_add_1\n");
493 /* insert node in tree */
498 key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
503 if (plen
< fn
->fn_bit
||
504 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
506 if (replace_required
) {
507 pr_warn("Can't replace route, no match found\n");
508 return ERR_PTR(-ENOENT
);
510 pr_warn("NLM_F_CREATE should be set when creating new route\n");
519 if (plen
== fn
->fn_bit
) {
520 /* clean up an intermediate node */
521 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
522 rt6_release(fn
->leaf
);
526 fn
->fn_sernum
= sernum
;
532 * We have more bits to go
535 /* Try to walk down on tree. */
536 fn
->fn_sernum
= sernum
;
537 dir
= addr_bit_set(addr
, fn
->fn_bit
);
539 fn
= dir
? fn
->right
: fn
->left
;
543 /* We should not create new node because
544 * NLM_F_REPLACE was specified without NLM_F_CREATE
545 * I assume it is safe to require NLM_F_CREATE when
546 * REPLACE flag is used! Later we may want to remove the
547 * check for replace_required, because according
548 * to netlink specification, NLM_F_CREATE
549 * MUST be specified if new route is created.
550 * That would keep IPv6 consistent with IPv4
552 if (replace_required
) {
553 pr_warn("Can't replace route, no match found\n");
554 return ERR_PTR(-ENOENT
);
556 pr_warn("NLM_F_CREATE should be set when creating new route\n");
559 * We walked to the bottom of tree.
560 * Create new leaf node without children.
566 return ERR_PTR(-ENOMEM
);
570 ln
->fn_sernum
= sernum
;
582 * split since we don't have a common prefix anymore or
583 * we have a less significant route.
584 * we've to insert an intermediate node on the list
585 * this new node will point to the one we need to create
591 /* find 1st bit in difference between the 2 addrs.
593 See comment in __ipv6_addr_diff: bit may be an invalid value,
594 but if it is >= plen, the value is ignored in any case.
597 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
602 * (new leaf node)[ln] (old node)[fn]
610 node_free_immediate(in
);
612 node_free_immediate(ln
);
613 return ERR_PTR(-ENOMEM
);
617 * new intermediate node.
619 * be off since that an address that chooses one of
620 * the branches would not match less specific routes
621 * in the other branch
628 atomic_inc(&in
->leaf
->rt6i_ref
);
630 in
->fn_sernum
= sernum
;
632 /* update parent pointer */
643 ln
->fn_sernum
= sernum
;
645 if (addr_bit_set(addr
, bit
)) {
652 } else { /* plen <= bit */
655 * (new leaf node)[ln]
657 * (old node)[fn] NULL
663 return ERR_PTR(-ENOMEM
);
669 ln
->fn_sernum
= sernum
;
676 if (addr_bit_set(&key
->addr
, plen
))
686 static bool rt6_qualify_for_ecmp(struct rt6_info
*rt
)
688 return (rt
->rt6i_flags
& (RTF_GATEWAY
|RTF_ADDRCONF
|RTF_DYNAMIC
)) ==
692 static void fib6_copy_metrics(u32
*mp
, const struct mx6_config
*mxc
)
696 for (i
= 0; i
< RTAX_MAX
; i
++) {
697 if (test_bit(i
, mxc
->mx_valid
))
702 static int fib6_commit_metrics(struct dst_entry
*dst
, struct mx6_config
*mxc
)
707 if (dst
->flags
& DST_HOST
) {
708 u32
*mp
= dst_metrics_write_ptr(dst
);
713 fib6_copy_metrics(mp
, mxc
);
715 dst_init_metrics(dst
, mxc
->mx
, false);
717 /* We've stolen mx now. */
724 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
727 if (atomic_read(&rt
->rt6i_ref
) != 1) {
728 /* This route is used as dummy address holder in some split
729 * nodes. It is not leaked, but it still holds other resources,
730 * which must be released in time. So, scan ascendant nodes
731 * and replace dummy references to this route with references
732 * to still alive ones.
735 if (!(fn
->fn_flags
& RTN_RTINFO
) && fn
->leaf
== rt
) {
736 fn
->leaf
= fib6_find_prefix(net
, fn
);
737 atomic_inc(&fn
->leaf
->rt6i_ref
);
742 /* No more references are possible at this point. */
743 BUG_ON(atomic_read(&rt
->rt6i_ref
) != 1);
748 * Insert routing information in a node.
751 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
752 struct nl_info
*info
, struct mx6_config
*mxc
)
754 struct rt6_info
*iter
= NULL
;
755 struct rt6_info
**ins
;
756 struct rt6_info
**fallback_ins
= NULL
;
757 int replace
= (info
->nlh
&&
758 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
759 int add
= (!info
->nlh
||
760 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
762 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
763 u16 nlflags
= NLM_F_EXCL
;
768 for (iter
= fn
->leaf
; iter
; iter
= iter
->dst
.rt6_next
) {
770 * Search for duplicates
773 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
775 * Same priority level
778 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
781 nlflags
&= ~NLM_F_EXCL
;
783 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
788 fallback_ins
= fallback_ins
?: ins
;
792 if (rt6_duplicate_nexthop(iter
, rt
)) {
793 if (rt
->rt6i_nsiblings
)
794 rt
->rt6i_nsiblings
= 0;
795 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
797 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
798 rt6_clean_expires(iter
);
800 rt6_set_expires(iter
, rt
->dst
.expires
);
801 iter
->rt6i_pmtu
= rt
->rt6i_pmtu
;
804 /* If we have the same destination and the same metric,
805 * but not the same gateway, then the route we try to
806 * add is sibling to this route, increment our counter
807 * of siblings, and later we will add our route to the
809 * Only static routes (which don't have flag
810 * RTF_EXPIRES) are used for ECMPv6.
812 * To avoid long list, we only had siblings if the
813 * route have a gateway.
816 rt6_qualify_for_ecmp(iter
))
817 rt
->rt6i_nsiblings
++;
820 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
824 ins
= &iter
->dst
.rt6_next
;
827 if (fallback_ins
&& !found
) {
828 /* No ECMP-able route found, replace first non-ECMP one */
834 /* Reset round-robin state, if necessary */
835 if (ins
== &fn
->leaf
)
838 /* Link this route to others same route. */
839 if (rt
->rt6i_nsiblings
) {
840 unsigned int rt6i_nsiblings
;
841 struct rt6_info
*sibling
, *temp_sibling
;
843 /* Find the first route that have the same metric */
846 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
847 rt6_qualify_for_ecmp(sibling
)) {
848 list_add_tail(&rt
->rt6i_siblings
,
849 &sibling
->rt6i_siblings
);
852 sibling
= sibling
->dst
.rt6_next
;
854 /* For each sibling in the list, increment the counter of
855 * siblings. BUG() if counters does not match, list of siblings
859 list_for_each_entry_safe(sibling
, temp_sibling
,
860 &rt
->rt6i_siblings
, rt6i_siblings
) {
861 sibling
->rt6i_nsiblings
++;
862 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
865 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
873 pr_warn("NLM_F_CREATE should be set when creating new route\n");
876 nlflags
|= NLM_F_CREATE
;
877 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
881 rt
->dst
.rt6_next
= iter
;
883 rcu_assign_pointer(rt
->rt6i_node
, fn
);
884 atomic_inc(&rt
->rt6i_ref
);
885 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, nlflags
);
886 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
888 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
889 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
890 fn
->fn_flags
|= RTN_RTINFO
;
899 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
903 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
908 rcu_assign_pointer(rt
->rt6i_node
, fn
);
909 rt
->dst
.rt6_next
= iter
->dst
.rt6_next
;
910 atomic_inc(&rt
->rt6i_ref
);
911 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, NLM_F_REPLACE
);
912 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
913 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
914 fn
->fn_flags
|= RTN_RTINFO
;
916 nsiblings
= iter
->rt6i_nsiblings
;
917 fib6_purge_rt(iter
, fn
, info
->nl_net
);
918 if (fn
->rr_ptr
== iter
)
923 /* Replacing an ECMP route, remove all siblings */
924 ins
= &rt
->dst
.rt6_next
;
927 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
929 if (rt6_qualify_for_ecmp(iter
)) {
930 *ins
= iter
->dst
.rt6_next
;
931 fib6_purge_rt(iter
, fn
, info
->nl_net
);
932 if (fn
->rr_ptr
== iter
)
937 ins
= &iter
->dst
.rt6_next
;
941 WARN_ON(nsiblings
!= 0);
948 static void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
950 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
951 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
952 mod_timer(&net
->ipv6
.ip6_fib_timer
,
953 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
956 void fib6_force_start_gc(struct net
*net
)
958 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
959 mod_timer(&net
->ipv6
.ip6_fib_timer
,
960 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
964 * Add routing information to the routing tree.
965 * <destination addr>/<source addr>
966 * with source addr info in sub-trees
969 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
970 struct nl_info
*info
, struct mx6_config
*mxc
)
972 struct fib6_node
*fn
, *pn
= NULL
;
974 int allow_create
= 1;
975 int replace_required
= 0;
976 int sernum
= fib6_new_sernum(info
->nl_net
);
978 if (WARN_ON_ONCE((rt
->dst
.flags
& DST_NOCACHE
) &&
979 !atomic_read(&rt
->dst
.__refcnt
)))
983 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
985 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
986 replace_required
= 1;
988 if (!allow_create
&& !replace_required
)
989 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
991 fn
= fib6_add_1(root
, &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
,
992 offsetof(struct rt6_info
, rt6i_dst
), allow_create
,
993 replace_required
, sernum
);
1002 #ifdef CONFIG_IPV6_SUBTREES
1003 if (rt
->rt6i_src
.plen
) {
1004 struct fib6_node
*sn
;
1007 struct fib6_node
*sfn
;
1019 /* Create subtree root node */
1024 sfn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
1025 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
1026 sfn
->fn_flags
= RTN_ROOT
;
1027 sfn
->fn_sernum
= sernum
;
1029 /* Now add the first leaf node to new subtree */
1031 sn
= fib6_add_1(sfn
, &rt
->rt6i_src
.addr
,
1033 offsetof(struct rt6_info
, rt6i_src
),
1034 allow_create
, replace_required
, sernum
);
1037 /* If it is failed, discard just allocated
1038 root, and then (in failure) stale node
1041 node_free_immediate(sfn
);
1046 /* Now link new subtree to main tree */
1050 sn
= fib6_add_1(fn
->subtree
, &rt
->rt6i_src
.addr
,
1052 offsetof(struct rt6_info
, rt6i_src
),
1053 allow_create
, replace_required
, sernum
);
1063 atomic_inc(&rt
->rt6i_ref
);
1069 err
= fib6_add_rt2node(fn
, rt
, info
, mxc
);
1071 fib6_start_gc(info
->nl_net
, rt
);
1072 if (!(rt
->rt6i_flags
& RTF_CACHE
))
1073 fib6_prune_clones(info
->nl_net
, pn
);
1074 rt
->dst
.flags
&= ~DST_NOCACHE
;
1079 #ifdef CONFIG_IPV6_SUBTREES
1081 * If fib6_add_1 has cleared the old leaf pointer in the
1082 * super-tree leaf node we have to find a new one for it.
1084 if (pn
!= fn
&& pn
->leaf
== rt
) {
1086 atomic_dec(&rt
->rt6i_ref
);
1088 if (pn
!= fn
&& !pn
->leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
1089 pn
->leaf
= fib6_find_prefix(info
->nl_net
, pn
);
1092 WARN_ON(pn
->leaf
== NULL
);
1093 pn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
1096 atomic_inc(&pn
->leaf
->rt6i_ref
);
1104 /* fn->leaf could be NULL if fn is an intermediate node and we
1105 * failed to add the new route to it in both subtree creation
1106 * failure and fib6_add_rt2node() failure case.
1107 * In both cases, fib6_repair_tree() should be called to fix
1110 if (fn
&& !(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)))
1111 fib6_repair_tree(info
->nl_net
, fn
);
1112 if (!(rt
->dst
.flags
& DST_NOCACHE
))
1118 * Routing tree lookup
1122 struct lookup_args
{
1123 int offset
; /* key offset on rt6_info */
1124 const struct in6_addr
*addr
; /* search key */
1127 static struct fib6_node
*fib6_lookup_1(struct fib6_node
*root
,
1128 struct lookup_args
*args
)
1130 struct fib6_node
*fn
;
1133 if (unlikely(args
->offset
== 0))
1143 struct fib6_node
*next
;
1145 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1147 next
= dir
? fn
->right
: fn
->left
;
1157 if (FIB6_SUBTREE(fn
) || fn
->fn_flags
& RTN_RTINFO
) {
1160 key
= (struct rt6key
*) ((u8
*) fn
->leaf
+
1163 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1164 #ifdef CONFIG_IPV6_SUBTREES
1166 struct fib6_node
*sfn
;
1167 sfn
= fib6_lookup_1(fn
->subtree
,
1174 if (fn
->fn_flags
& RTN_RTINFO
)
1178 #ifdef CONFIG_IPV6_SUBTREES
1181 if (fn
->fn_flags
& RTN_ROOT
)
1190 struct fib6_node
*fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1191 const struct in6_addr
*saddr
)
1193 struct fib6_node
*fn
;
1194 struct lookup_args args
[] = {
1196 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1199 #ifdef CONFIG_IPV6_SUBTREES
1201 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1206 .offset
= 0, /* sentinel */
1210 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1211 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1218 * Get node with specified destination prefix (and source prefix,
1219 * if subtrees are used)
1223 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1224 const struct in6_addr
*addr
,
1225 int plen
, int offset
)
1227 struct fib6_node
*fn
;
1229 for (fn
= root
; fn
; ) {
1230 struct rt6key
*key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
1235 if (plen
< fn
->fn_bit
||
1236 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1239 if (plen
== fn
->fn_bit
)
1243 * We have more bits to go
1245 if (addr_bit_set(addr
, fn
->fn_bit
))
1253 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1254 const struct in6_addr
*daddr
, int dst_len
,
1255 const struct in6_addr
*saddr
, int src_len
)
1257 struct fib6_node
*fn
;
1259 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1260 offsetof(struct rt6_info
, rt6i_dst
));
1262 #ifdef CONFIG_IPV6_SUBTREES
1264 WARN_ON(saddr
== NULL
);
1265 if (fn
&& fn
->subtree
)
1266 fn
= fib6_locate_1(fn
->subtree
, saddr
, src_len
,
1267 offsetof(struct rt6_info
, rt6i_src
));
1271 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1283 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
)
1285 if (fn
->fn_flags
& RTN_ROOT
)
1286 return net
->ipv6
.ip6_null_entry
;
1290 return fn
->left
->leaf
;
1292 return fn
->right
->leaf
;
1294 fn
= FIB6_SUBTREE(fn
);
1300 * Called to trim the tree of intermediate nodes when possible. "fn"
1301 * is the node we want to try and remove.
1304 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1305 struct fib6_node
*fn
)
1309 struct fib6_node
*child
, *pn
;
1310 struct fib6_walker
*w
;
1314 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1317 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1318 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1324 child
= fn
->right
, children
|= 1;
1326 child
= fn
->left
, children
|= 2;
1328 if (children
== 3 || FIB6_SUBTREE(fn
)
1329 #ifdef CONFIG_IPV6_SUBTREES
1330 /* Subtree root (i.e. fn) may have one child */
1331 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1334 fn
->leaf
= fib6_find_prefix(net
, fn
);
1338 fn
->leaf
= net
->ipv6
.ip6_null_entry
;
1341 atomic_inc(&fn
->leaf
->rt6i_ref
);
1346 #ifdef CONFIG_IPV6_SUBTREES
1347 if (FIB6_SUBTREE(pn
) == fn
) {
1348 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1349 FIB6_SUBTREE(pn
) = NULL
;
1352 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1354 if (pn
->right
== fn
)
1356 else if (pn
->left
== fn
)
1365 #ifdef CONFIG_IPV6_SUBTREES
1369 read_lock(&net
->ipv6
.fib6_walker_lock
);
1370 FOR_WALKERS(net
, w
) {
1372 if (w
->root
== fn
) {
1373 w
->root
= w
->node
= NULL
;
1374 RT6_TRACE("W %p adjusted by delroot 1\n", w
);
1375 } else if (w
->node
== fn
) {
1376 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1381 if (w
->root
== fn
) {
1383 RT6_TRACE("W %p adjusted by delroot 2\n", w
);
1385 if (w
->node
== fn
) {
1388 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1389 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1391 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1392 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1397 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1400 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1403 rt6_release(pn
->leaf
);
1409 static void fib6_del_route(struct fib6_node
*fn
, struct rt6_info
**rtp
,
1410 struct nl_info
*info
)
1412 struct fib6_walker
*w
;
1413 struct rt6_info
*rt
= *rtp
;
1414 struct net
*net
= info
->nl_net
;
1416 RT6_TRACE("fib6_del_route\n");
1419 *rtp
= rt
->dst
.rt6_next
;
1420 rt
->rt6i_node
= NULL
;
1421 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1422 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1424 /* Reset round-robin state, if necessary */
1425 if (fn
->rr_ptr
== rt
)
1428 /* Remove this entry from other siblings */
1429 if (rt
->rt6i_nsiblings
) {
1430 struct rt6_info
*sibling
, *next_sibling
;
1432 list_for_each_entry_safe(sibling
, next_sibling
,
1433 &rt
->rt6i_siblings
, rt6i_siblings
)
1434 sibling
->rt6i_nsiblings
--;
1435 rt
->rt6i_nsiblings
= 0;
1436 list_del_init(&rt
->rt6i_siblings
);
1439 /* Adjust walkers */
1440 read_lock(&net
->ipv6
.fib6_walker_lock
);
1441 FOR_WALKERS(net
, w
) {
1442 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1443 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1444 w
->leaf
= rt
->dst
.rt6_next
;
1449 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1451 rt
->dst
.rt6_next
= NULL
;
1453 /* If it was last route, expunge its radix tree node */
1455 fn
->fn_flags
&= ~RTN_RTINFO
;
1456 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1457 fn
= fib6_repair_tree(net
, fn
);
1460 fib6_purge_rt(rt
, fn
, net
);
1462 inet6_rt_notify(RTM_DELROUTE
, rt
, info
, 0);
1466 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1468 struct fib6_node
*fn
= rcu_dereference_protected(rt
->rt6i_node
,
1469 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1470 struct net
*net
= info
->nl_net
;
1471 struct rt6_info
**rtp
;
1474 if (rt
->dst
.obsolete
> 0) {
1479 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1482 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1484 if (!(rt
->rt6i_flags
& RTF_CACHE
)) {
1485 struct fib6_node
*pn
= fn
;
1486 #ifdef CONFIG_IPV6_SUBTREES
1487 /* clones of this route might be in another subtree */
1488 if (rt
->rt6i_src
.plen
) {
1489 while (!(pn
->fn_flags
& RTN_ROOT
))
1494 fib6_prune_clones(info
->nl_net
, pn
);
1498 * Walk the leaf entries looking for ourself
1501 for (rtp
= &fn
->leaf
; *rtp
; rtp
= &(*rtp
)->dst
.rt6_next
) {
1503 fib6_del_route(fn
, rtp
, info
);
1511 * Tree traversal function.
1513 * Certainly, it is not interrupt safe.
1514 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1515 * It means, that we can modify tree during walking
1516 * and use this function for garbage collection, clone pruning,
1517 * cleaning tree when a device goes down etc. etc.
1519 * It guarantees that every node will be traversed,
1520 * and that it will be traversed only once.
1522 * Callback function w->func may return:
1523 * 0 -> continue walking.
1524 * positive value -> walking is suspended (used by tree dumps,
1525 * and probably by gc, if it will be split to several slices)
1526 * negative value -> terminate walking.
1528 * The function itself returns:
1529 * 0 -> walk is complete.
1530 * >0 -> walk is incomplete (i.e. suspended)
1531 * <0 -> walk is terminated by an error.
1534 static int fib6_walk_continue(struct fib6_walker
*w
)
1536 struct fib6_node
*fn
, *pn
;
1543 if (w
->prune
&& fn
!= w
->root
&&
1544 fn
->fn_flags
& RTN_RTINFO
&& w
->state
< FWS_C
) {
1549 #ifdef CONFIG_IPV6_SUBTREES
1551 if (FIB6_SUBTREE(fn
)) {
1552 w
->node
= FIB6_SUBTREE(fn
);
1560 w
->state
= FWS_INIT
;
1566 w
->node
= fn
->right
;
1567 w
->state
= FWS_INIT
;
1573 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1595 #ifdef CONFIG_IPV6_SUBTREES
1596 if (FIB6_SUBTREE(pn
) == fn
) {
1597 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1602 if (pn
->left
== fn
) {
1606 if (pn
->right
== fn
) {
1608 w
->leaf
= w
->node
->leaf
;
1618 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
)
1622 w
->state
= FWS_INIT
;
1625 fib6_walker_link(net
, w
);
1626 res
= fib6_walk_continue(w
);
1628 fib6_walker_unlink(net
, w
);
1632 static int fib6_clean_node(struct fib6_walker
*w
)
1635 struct rt6_info
*rt
;
1636 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1637 struct nl_info info
= {
1641 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1642 w
->node
->fn_sernum
!= c
->sernum
)
1643 w
->node
->fn_sernum
= c
->sernum
;
1646 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1651 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1652 res
= c
->func(rt
, c
->arg
);
1655 res
= fib6_del(rt
, &info
);
1658 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1660 rcu_access_pointer(rt
->rt6i_node
),
1674 * Convenient frontend to tree walker.
1676 * func is called on each route.
1677 * It may return -1 -> delete this route.
1678 * 0 -> continue walking
1680 * prune==1 -> only immediate children of node (certainly,
1681 * ignoring pure split nodes) will be scanned.
1684 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1685 int (*func
)(struct rt6_info
*, void *arg
),
1686 bool prune
, int sernum
, void *arg
)
1688 struct fib6_cleaner c
;
1691 c
.w
.func
= fib6_clean_node
;
1700 fib6_walk(net
, &c
.w
);
1703 static void __fib6_clean_all(struct net
*net
,
1704 int (*func
)(struct rt6_info
*, void *),
1705 int sernum
, void *arg
)
1707 struct fib6_table
*table
;
1708 struct hlist_head
*head
;
1712 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1713 head
= &net
->ipv6
.fib_table_hash
[h
];
1714 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1715 write_lock_bh(&table
->tb6_lock
);
1716 fib6_clean_tree(net
, &table
->tb6_root
,
1717 func
, false, sernum
, arg
);
1718 write_unlock_bh(&table
->tb6_lock
);
1724 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *),
1727 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
);
1730 static int fib6_prune_clone(struct rt6_info
*rt
, void *arg
)
1732 if (rt
->rt6i_flags
& RTF_CACHE
) {
1733 RT6_TRACE("pruning clone %p\n", rt
);
1740 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
)
1742 fib6_clean_tree(net
, fn
, fib6_prune_clone
, true,
1743 FIB6_NO_SERNUM_CHANGE
, NULL
);
1746 static void fib6_flush_trees(struct net
*net
)
1748 int new_sernum
= fib6_new_sernum(net
);
1750 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
);
1754 * Garbage collection
1763 static int fib6_age(struct rt6_info
*rt
, void *arg
)
1765 struct fib6_gc_args
*gc_args
= arg
;
1766 unsigned long now
= jiffies
;
1769 * check addrconf expiration here.
1770 * Routes are expired even if they are in use.
1772 * Also age clones. Note, that clones are aged out
1773 * only if they are not in use now.
1776 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
1777 if (time_after(now
, rt
->dst
.expires
)) {
1778 RT6_TRACE("expiring %p\n", rt
);
1782 } else if (rt
->rt6i_flags
& RTF_CACHE
) {
1783 if (atomic_read(&rt
->dst
.__refcnt
) == 0 &&
1784 time_after_eq(now
, rt
->dst
.lastuse
+ gc_args
->timeout
)) {
1785 RT6_TRACE("aging clone %p\n", rt
);
1787 } else if (rt
->rt6i_flags
& RTF_GATEWAY
) {
1788 struct neighbour
*neigh
;
1789 __u8 neigh_flags
= 0;
1791 neigh
= dst_neigh_lookup(&rt
->dst
, &rt
->rt6i_gateway
);
1793 neigh_flags
= neigh
->flags
;
1794 neigh_release(neigh
);
1796 if (!(neigh_flags
& NTF_ROUTER
)) {
1797 RT6_TRACE("purging route %p via non-router but gateway\n",
1808 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
1810 struct fib6_gc_args gc_args
;
1814 spin_lock_bh(&net
->ipv6
.fib6_gc_lock
);
1815 } else if (!spin_trylock_bh(&net
->ipv6
.fib6_gc_lock
)) {
1816 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
1819 gc_args
.timeout
= expires
? (int)expires
:
1820 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
1822 gc_args
.more
= icmp6_dst_gc();
1824 fib6_clean_all(net
, fib6_age
, &gc_args
);
1826 net
->ipv6
.ip6_rt_last_gc
= now
;
1829 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1831 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
1833 del_timer(&net
->ipv6
.ip6_fib_timer
);
1834 spin_unlock_bh(&net
->ipv6
.fib6_gc_lock
);
1837 static void fib6_gc_timer_cb(unsigned long arg
)
1839 fib6_run_gc(0, (struct net
*)arg
, true);
1842 static int __net_init
fib6_net_init(struct net
*net
)
1844 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
1846 spin_lock_init(&net
->ipv6
.fib6_gc_lock
);
1847 rwlock_init(&net
->ipv6
.fib6_walker_lock
);
1848 INIT_LIST_HEAD(&net
->ipv6
.fib6_walkers
);
1849 setup_timer(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, (unsigned long)net
);
1851 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
1852 if (!net
->ipv6
.rt6_stats
)
1855 /* Avoid false sharing : Use at least a full cache line */
1856 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
1858 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
1859 if (!net
->ipv6
.fib_table_hash
)
1862 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
1864 if (!net
->ipv6
.fib6_main_tbl
)
1865 goto out_fib_table_hash
;
1867 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
1868 net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1869 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
1870 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1871 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1873 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1874 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
1876 if (!net
->ipv6
.fib6_local_tbl
)
1877 goto out_fib6_main_tbl
;
1878 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
1879 net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1880 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
1881 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1882 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1884 fib6_tables_init(net
);
1888 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1890 kfree(net
->ipv6
.fib6_main_tbl
);
1893 kfree(net
->ipv6
.fib_table_hash
);
1895 kfree(net
->ipv6
.rt6_stats
);
1900 static void fib6_net_exit(struct net
*net
)
1904 rt6_ifdown(net
, NULL
);
1905 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
1907 for (i
= 0; i
< FIB6_TABLE_HASHSZ
; i
++) {
1908 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[i
];
1909 struct hlist_node
*tmp
;
1910 struct fib6_table
*tb
;
1912 hlist_for_each_entry_safe(tb
, tmp
, head
, tb6_hlist
) {
1913 hlist_del(&tb
->tb6_hlist
);
1914 fib6_free_table(tb
);
1918 kfree(net
->ipv6
.fib_table_hash
);
1919 kfree(net
->ipv6
.rt6_stats
);
1922 static struct pernet_operations fib6_net_ops
= {
1923 .init
= fib6_net_init
,
1924 .exit
= fib6_net_exit
,
1927 int __init
fib6_init(void)
1931 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
1932 sizeof(struct fib6_node
),
1933 0, SLAB_HWCACHE_ALIGN
,
1935 if (!fib6_node_kmem
)
1938 ret
= register_pernet_subsys(&fib6_net_ops
);
1940 goto out_kmem_cache_create
;
1942 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
1945 goto out_unregister_subsys
;
1947 __fib6_flush_trees
= fib6_flush_trees
;
1951 out_unregister_subsys
:
1952 unregister_pernet_subsys(&fib6_net_ops
);
1953 out_kmem_cache_create
:
1954 kmem_cache_destroy(fib6_node_kmem
);
1958 void fib6_gc_cleanup(void)
1960 unregister_pernet_subsys(&fib6_net_ops
);
1961 kmem_cache_destroy(fib6_node_kmem
);
1964 #ifdef CONFIG_PROC_FS
1966 struct ipv6_route_iter
{
1967 struct seq_net_private p
;
1968 struct fib6_walker w
;
1970 struct fib6_table
*tbl
;
1974 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
1976 struct rt6_info
*rt
= v
;
1977 struct ipv6_route_iter
*iter
= seq
->private;
1979 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
1981 #ifdef CONFIG_IPV6_SUBTREES
1982 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
1984 seq_puts(seq
, "00000000000000000000000000000000 00 ");
1986 if (rt
->rt6i_flags
& RTF_GATEWAY
)
1987 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
1989 seq_puts(seq
, "00000000000000000000000000000000");
1991 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
1992 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
1993 rt
->dst
.__use
, rt
->rt6i_flags
,
1994 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
1995 iter
->w
.leaf
= NULL
;
1999 static int ipv6_route_yield(struct fib6_walker
*w
)
2001 struct ipv6_route_iter
*iter
= w
->args
;
2007 iter
->w
.leaf
= iter
->w
.leaf
->dst
.rt6_next
;
2009 if (!iter
->skip
&& iter
->w
.leaf
)
2011 } while (iter
->w
.leaf
);
2016 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
,
2019 memset(&iter
->w
, 0, sizeof(iter
->w
));
2020 iter
->w
.func
= ipv6_route_yield
;
2021 iter
->w
.root
= &iter
->tbl
->tb6_root
;
2022 iter
->w
.state
= FWS_INIT
;
2023 iter
->w
.node
= iter
->w
.root
;
2024 iter
->w
.args
= iter
;
2025 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2026 INIT_LIST_HEAD(&iter
->w
.lh
);
2027 fib6_walker_link(net
, &iter
->w
);
2030 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
2034 struct hlist_node
*node
;
2037 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
2038 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
2044 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
2045 node
= rcu_dereference_bh(
2046 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
2048 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
2051 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
2053 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
2054 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2055 iter
->w
.state
= FWS_INIT
;
2056 iter
->w
.node
= iter
->w
.root
;
2057 WARN_ON(iter
->w
.skip
);
2058 iter
->w
.skip
= iter
->w
.count
;
2062 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2066 struct net
*net
= seq_file_net(seq
);
2067 struct ipv6_route_iter
*iter
= seq
->private;
2072 n
= ((struct rt6_info
*)v
)->dst
.rt6_next
;
2079 ipv6_route_check_sernum(iter
);
2080 read_lock(&iter
->tbl
->tb6_lock
);
2081 r
= fib6_walk_continue(&iter
->w
);
2082 read_unlock(&iter
->tbl
->tb6_lock
);
2086 return iter
->w
.leaf
;
2088 fib6_walker_unlink(net
, &iter
->w
);
2091 fib6_walker_unlink(net
, &iter
->w
);
2093 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2097 ipv6_route_seq_setup_walk(iter
, net
);
2101 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2104 struct net
*net
= seq_file_net(seq
);
2105 struct ipv6_route_iter
*iter
= seq
->private;
2108 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2112 ipv6_route_seq_setup_walk(iter
, net
);
2113 return ipv6_route_seq_next(seq
, NULL
, pos
);
2119 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2121 struct fib6_walker
*w
= &iter
->w
;
2122 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2125 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2128 struct net
*net
= seq_file_net(seq
);
2129 struct ipv6_route_iter
*iter
= seq
->private;
2131 if (ipv6_route_iter_active(iter
))
2132 fib6_walker_unlink(net
, &iter
->w
);
2134 rcu_read_unlock_bh();
2137 static const struct seq_operations ipv6_route_seq_ops
= {
2138 .start
= ipv6_route_seq_start
,
2139 .next
= ipv6_route_seq_next
,
2140 .stop
= ipv6_route_seq_stop
,
2141 .show
= ipv6_route_seq_show
2144 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2146 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2147 sizeof(struct ipv6_route_iter
));
2150 #endif /* CONFIG_PROC_FS */