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(struct fib6_node
*fn
)
153 kmem_cache_free(fib6_node_kmem
, fn
);
156 static void rt6_free_pcpu(struct rt6_info
*non_pcpu_rt
)
160 if (!non_pcpu_rt
->rt6i_pcpu
)
163 for_each_possible_cpu(cpu
) {
164 struct rt6_info
**ppcpu_rt
;
165 struct rt6_info
*pcpu_rt
;
167 ppcpu_rt
= per_cpu_ptr(non_pcpu_rt
->rt6i_pcpu
, cpu
);
170 dst_dev_put(&pcpu_rt
->dst
);
171 dst_release(&pcpu_rt
->dst
);
176 free_percpu(non_pcpu_rt
->rt6i_pcpu
);
177 non_pcpu_rt
->rt6i_pcpu
= NULL
;
180 static void rt6_release(struct rt6_info
*rt
)
182 if (atomic_dec_and_test(&rt
->rt6i_ref
)) {
184 dst_dev_put(&rt
->dst
);
185 dst_release(&rt
->dst
);
189 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
194 * Initialize table lock at a single place to give lockdep a key,
195 * tables aren't visible prior to being linked to the list.
197 rwlock_init(&tb
->tb6_lock
);
199 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
202 * No protection necessary, this is the only list mutatation
203 * operation, tables never disappear once they exist.
205 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
208 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
210 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
212 struct fib6_table
*table
;
214 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
217 table
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
218 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
219 inet_peer_base_init(&table
->tb6_peers
);
225 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
227 struct fib6_table
*tb
;
231 tb
= fib6_get_table(net
, id
);
235 tb
= fib6_alloc_table(net
, id
);
237 fib6_link_table(net
, tb
);
241 EXPORT_SYMBOL_GPL(fib6_new_table
);
243 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
245 struct fib6_table
*tb
;
246 struct hlist_head
*head
;
251 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
253 head
= &net
->ipv6
.fib_table_hash
[h
];
254 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
255 if (tb
->tb6_id
== id
) {
264 EXPORT_SYMBOL_GPL(fib6_get_table
);
266 static void __net_init
fib6_tables_init(struct net
*net
)
268 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
269 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
273 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
275 return fib6_get_table(net
, id
);
278 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
280 return net
->ipv6
.fib6_main_tbl
;
283 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
284 int flags
, pol_lookup_t lookup
)
288 rt
= lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
289 if (rt
->dst
.error
== -EAGAIN
) {
291 rt
= net
->ipv6
.ip6_null_entry
;
298 static void __net_init
fib6_tables_init(struct net
*net
)
300 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
305 static int fib6_dump_node(struct fib6_walker
*w
)
310 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
311 res
= rt6_dump_route(rt
, w
->args
);
313 /* Frame is full, suspend walking */
318 /* Multipath routes are dumped in one route with the
319 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
320 * last sibling of this route (no need to dump the
321 * sibling routes again)
323 if (rt
->rt6i_nsiblings
)
324 rt
= list_last_entry(&rt
->rt6i_siblings
,
332 static void fib6_dump_end(struct netlink_callback
*cb
)
334 struct net
*net
= sock_net(cb
->skb
->sk
);
335 struct fib6_walker
*w
= (void *)cb
->args
[2];
340 fib6_walker_unlink(net
, w
);
345 cb
->done
= (void *)cb
->args
[3];
349 static int fib6_dump_done(struct netlink_callback
*cb
)
352 return cb
->done
? cb
->done(cb
) : 0;
355 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
356 struct netlink_callback
*cb
)
358 struct net
*net
= sock_net(skb
->sk
);
359 struct fib6_walker
*w
;
362 w
= (void *)cb
->args
[2];
363 w
->root
= &table
->tb6_root
;
365 if (cb
->args
[4] == 0) {
369 read_lock_bh(&table
->tb6_lock
);
370 res
= fib6_walk(net
, w
);
371 read_unlock_bh(&table
->tb6_lock
);
374 cb
->args
[5] = w
->root
->fn_sernum
;
377 if (cb
->args
[5] != w
->root
->fn_sernum
) {
378 /* Begin at the root if the tree changed */
379 cb
->args
[5] = w
->root
->fn_sernum
;
386 read_lock_bh(&table
->tb6_lock
);
387 res
= fib6_walk_continue(w
);
388 read_unlock_bh(&table
->tb6_lock
);
390 fib6_walker_unlink(net
, w
);
398 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
400 struct net
*net
= sock_net(skb
->sk
);
402 unsigned int e
= 0, s_e
;
403 struct rt6_rtnl_dump_arg arg
;
404 struct fib6_walker
*w
;
405 struct fib6_table
*tb
;
406 struct hlist_head
*head
;
412 w
= (void *)cb
->args
[2];
416 * 1. hook callback destructor.
418 cb
->args
[3] = (long)cb
->done
;
419 cb
->done
= fib6_dump_done
;
422 * 2. allocate and initialize walker.
424 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
427 w
->func
= fib6_dump_node
;
428 cb
->args
[2] = (long)w
;
437 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
439 head
= &net
->ipv6
.fib_table_hash
[h
];
440 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
443 res
= fib6_dump_table(tb
, skb
, cb
);
455 res
= res
< 0 ? res
: skb
->len
;
464 * return the appropriate node for a routing tree "add" operation
465 * by either creating and inserting or by returning an existing
469 static struct fib6_node
*fib6_add_1(struct fib6_node
*root
,
470 struct in6_addr
*addr
, int plen
,
471 int offset
, int allow_create
,
472 int replace_required
, int sernum
,
473 struct netlink_ext_ack
*extack
)
475 struct fib6_node
*fn
, *in
, *ln
;
476 struct fib6_node
*pn
= NULL
;
481 RT6_TRACE("fib6_add_1\n");
483 /* insert node in tree */
488 key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
493 if (plen
< fn
->fn_bit
||
494 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
496 if (replace_required
) {
497 NL_SET_ERR_MSG(extack
,
498 "Can not replace route - no match found");
499 pr_warn("Can't replace route, no match found\n");
500 return ERR_PTR(-ENOENT
);
502 pr_warn("NLM_F_CREATE should be set when creating new route\n");
511 if (plen
== fn
->fn_bit
) {
512 /* clean up an intermediate node */
513 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
514 rt6_release(fn
->leaf
);
518 fn
->fn_sernum
= sernum
;
524 * We have more bits to go
527 /* Try to walk down on tree. */
528 fn
->fn_sernum
= sernum
;
529 dir
= addr_bit_set(addr
, fn
->fn_bit
);
531 fn
= dir
? fn
->right
: fn
->left
;
535 /* We should not create new node because
536 * NLM_F_REPLACE was specified without NLM_F_CREATE
537 * I assume it is safe to require NLM_F_CREATE when
538 * REPLACE flag is used! Later we may want to remove the
539 * check for replace_required, because according
540 * to netlink specification, NLM_F_CREATE
541 * MUST be specified if new route is created.
542 * That would keep IPv6 consistent with IPv4
544 if (replace_required
) {
545 NL_SET_ERR_MSG(extack
,
546 "Can not replace route - no match found");
547 pr_warn("Can't replace route, no match found\n");
548 return ERR_PTR(-ENOENT
);
550 pr_warn("NLM_F_CREATE should be set when creating new route\n");
553 * We walked to the bottom of tree.
554 * Create new leaf node without children.
560 return ERR_PTR(-ENOMEM
);
564 ln
->fn_sernum
= sernum
;
576 * split since we don't have a common prefix anymore or
577 * we have a less significant route.
578 * we've to insert an intermediate node on the list
579 * this new node will point to the one we need to create
585 /* find 1st bit in difference between the 2 addrs.
587 See comment in __ipv6_addr_diff: bit may be an invalid value,
588 but if it is >= plen, the value is ignored in any case.
591 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
596 * (new leaf node)[ln] (old node)[fn]
607 return ERR_PTR(-ENOMEM
);
611 * new intermediate node.
613 * be off since that an address that chooses one of
614 * the branches would not match less specific routes
615 * in the other branch
622 atomic_inc(&in
->leaf
->rt6i_ref
);
624 in
->fn_sernum
= sernum
;
626 /* update parent pointer */
637 ln
->fn_sernum
= sernum
;
639 if (addr_bit_set(addr
, bit
)) {
646 } else { /* plen <= bit */
649 * (new leaf node)[ln]
651 * (old node)[fn] NULL
657 return ERR_PTR(-ENOMEM
);
663 ln
->fn_sernum
= sernum
;
670 if (addr_bit_set(&key
->addr
, plen
))
680 static bool rt6_qualify_for_ecmp(struct rt6_info
*rt
)
682 return (rt
->rt6i_flags
& (RTF_GATEWAY
|RTF_ADDRCONF
|RTF_DYNAMIC
)) ==
686 static void fib6_copy_metrics(u32
*mp
, const struct mx6_config
*mxc
)
690 for (i
= 0; i
< RTAX_MAX
; i
++) {
691 if (test_bit(i
, mxc
->mx_valid
))
696 static int fib6_commit_metrics(struct dst_entry
*dst
, struct mx6_config
*mxc
)
701 if (dst
->flags
& DST_HOST
) {
702 u32
*mp
= dst_metrics_write_ptr(dst
);
707 fib6_copy_metrics(mp
, mxc
);
709 dst_init_metrics(dst
, mxc
->mx
, false);
711 /* We've stolen mx now. */
718 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
721 if (atomic_read(&rt
->rt6i_ref
) != 1) {
722 /* This route is used as dummy address holder in some split
723 * nodes. It is not leaked, but it still holds other resources,
724 * which must be released in time. So, scan ascendant nodes
725 * and replace dummy references to this route with references
726 * to still alive ones.
729 if (!(fn
->fn_flags
& RTN_RTINFO
) && fn
->leaf
== rt
) {
730 fn
->leaf
= fib6_find_prefix(net
, fn
);
731 atomic_inc(&fn
->leaf
->rt6i_ref
);
736 /* No more references are possible at this point. */
737 BUG_ON(atomic_read(&rt
->rt6i_ref
) != 1);
742 * Insert routing information in a node.
745 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
746 struct nl_info
*info
, struct mx6_config
*mxc
)
748 struct rt6_info
*iter
= NULL
;
749 struct rt6_info
**ins
;
750 struct rt6_info
**fallback_ins
= NULL
;
751 int replace
= (info
->nlh
&&
752 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
753 int add
= (!info
->nlh
||
754 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
756 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
757 u16 nlflags
= NLM_F_EXCL
;
760 if (info
->nlh
&& (info
->nlh
->nlmsg_flags
& NLM_F_APPEND
))
761 nlflags
|= NLM_F_APPEND
;
765 for (iter
= fn
->leaf
; iter
; iter
= iter
->dst
.rt6_next
) {
767 * Search for duplicates
770 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
772 * Same priority level
775 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
778 nlflags
&= ~NLM_F_EXCL
;
780 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
785 fallback_ins
= fallback_ins
?: ins
;
789 if (rt6_duplicate_nexthop(iter
, rt
)) {
790 if (rt
->rt6i_nsiblings
)
791 rt
->rt6i_nsiblings
= 0;
792 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
794 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
795 rt6_clean_expires(iter
);
797 rt6_set_expires(iter
, rt
->dst
.expires
);
798 iter
->rt6i_pmtu
= rt
->rt6i_pmtu
;
801 /* If we have the same destination and the same metric,
802 * but not the same gateway, then the route we try to
803 * add is sibling to this route, increment our counter
804 * of siblings, and later we will add our route to the
806 * Only static routes (which don't have flag
807 * RTF_EXPIRES) are used for ECMPv6.
809 * To avoid long list, we only had siblings if the
810 * route have a gateway.
813 rt6_qualify_for_ecmp(iter
))
814 rt
->rt6i_nsiblings
++;
817 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
821 ins
= &iter
->dst
.rt6_next
;
824 if (fallback_ins
&& !found
) {
825 /* No ECMP-able route found, replace first non-ECMP one */
831 /* Reset round-robin state, if necessary */
832 if (ins
== &fn
->leaf
)
835 /* Link this route to others same route. */
836 if (rt
->rt6i_nsiblings
) {
837 unsigned int rt6i_nsiblings
;
838 struct rt6_info
*sibling
, *temp_sibling
;
840 /* Find the first route that have the same metric */
843 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
844 rt6_qualify_for_ecmp(sibling
)) {
845 list_add_tail(&rt
->rt6i_siblings
,
846 &sibling
->rt6i_siblings
);
849 sibling
= sibling
->dst
.rt6_next
;
851 /* For each sibling in the list, increment the counter of
852 * siblings. BUG() if counters does not match, list of siblings
856 list_for_each_entry_safe(sibling
, temp_sibling
,
857 &rt
->rt6i_siblings
, rt6i_siblings
) {
858 sibling
->rt6i_nsiblings
++;
859 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
862 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
870 pr_warn("NLM_F_CREATE should be set when creating new route\n");
873 nlflags
|= NLM_F_CREATE
;
874 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
878 rt
->dst
.rt6_next
= iter
;
881 atomic_inc(&rt
->rt6i_ref
);
882 if (!info
->skip_notify
)
883 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, nlflags
);
884 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
886 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
887 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
888 fn
->fn_flags
|= RTN_RTINFO
;
897 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
901 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
907 rt
->dst
.rt6_next
= iter
->dst
.rt6_next
;
908 atomic_inc(&rt
->rt6i_ref
);
909 if (!info
->skip_notify
)
910 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, NLM_F_REPLACE
);
911 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
912 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
913 fn
->fn_flags
|= RTN_RTINFO
;
915 nsiblings
= iter
->rt6i_nsiblings
;
916 fib6_purge_rt(iter
, fn
, info
->nl_net
);
917 if (fn
->rr_ptr
== iter
)
922 /* Replacing an ECMP route, remove all siblings */
923 ins
= &rt
->dst
.rt6_next
;
926 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
928 if (rt6_qualify_for_ecmp(iter
)) {
929 *ins
= iter
->dst
.rt6_next
;
930 fib6_purge_rt(iter
, fn
, info
->nl_net
);
931 if (fn
->rr_ptr
== iter
)
936 ins
= &iter
->dst
.rt6_next
;
940 WARN_ON(nsiblings
!= 0);
947 static void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
949 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
950 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
951 mod_timer(&net
->ipv6
.ip6_fib_timer
,
952 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
955 void fib6_force_start_gc(struct net
*net
)
957 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
958 mod_timer(&net
->ipv6
.ip6_fib_timer
,
959 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
963 * Add routing information to the routing tree.
964 * <destination addr>/<source addr>
965 * with source addr info in sub-trees
968 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
969 struct nl_info
*info
, struct mx6_config
*mxc
,
970 struct netlink_ext_ack
*extack
)
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(!atomic_read(&rt
->dst
.__refcnt
)))
982 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
984 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
985 replace_required
= 1;
987 if (!allow_create
&& !replace_required
)
988 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
990 fn
= fib6_add_1(root
, &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
,
991 offsetof(struct rt6_info
, rt6i_dst
), allow_create
,
992 replace_required
, sernum
, extack
);
1001 #ifdef CONFIG_IPV6_SUBTREES
1002 if (rt
->rt6i_src
.plen
) {
1003 struct fib6_node
*sn
;
1006 struct fib6_node
*sfn
;
1018 /* Create subtree root node */
1023 sfn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
1024 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
1025 sfn
->fn_flags
= RTN_ROOT
;
1026 sfn
->fn_sernum
= sernum
;
1028 /* Now add the first leaf node to new subtree */
1030 sn
= fib6_add_1(sfn
, &rt
->rt6i_src
.addr
,
1032 offsetof(struct rt6_info
, rt6i_src
),
1033 allow_create
, replace_required
, sernum
,
1037 /* If it is failed, discard just allocated
1038 root, and then (in failure) stale node
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
,
1064 atomic_inc(&rt
->rt6i_ref
);
1070 err
= fib6_add_rt2node(fn
, rt
, info
, mxc
);
1072 fib6_start_gc(info
->nl_net
, rt
);
1073 if (!(rt
->rt6i_flags
& RTF_CACHE
))
1074 fib6_prune_clones(info
->nl_net
, pn
);
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 /* Always release dst as dst->__refcnt is guaranteed
1113 * to be taken before entering this function
1115 dst_release_immediate(&rt
->dst
);
1120 * Routing tree lookup
1124 struct lookup_args
{
1125 int offset
; /* key offset on rt6_info */
1126 const struct in6_addr
*addr
; /* search key */
1129 static struct fib6_node
*fib6_lookup_1(struct fib6_node
*root
,
1130 struct lookup_args
*args
)
1132 struct fib6_node
*fn
;
1135 if (unlikely(args
->offset
== 0))
1145 struct fib6_node
*next
;
1147 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1149 next
= dir
? fn
->right
: fn
->left
;
1159 if (FIB6_SUBTREE(fn
) || fn
->fn_flags
& RTN_RTINFO
) {
1162 key
= (struct rt6key
*) ((u8
*) fn
->leaf
+
1165 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1166 #ifdef CONFIG_IPV6_SUBTREES
1168 struct fib6_node
*sfn
;
1169 sfn
= fib6_lookup_1(fn
->subtree
,
1176 if (fn
->fn_flags
& RTN_RTINFO
)
1180 #ifdef CONFIG_IPV6_SUBTREES
1183 if (fn
->fn_flags
& RTN_ROOT
)
1192 struct fib6_node
*fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1193 const struct in6_addr
*saddr
)
1195 struct fib6_node
*fn
;
1196 struct lookup_args args
[] = {
1198 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1201 #ifdef CONFIG_IPV6_SUBTREES
1203 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1208 .offset
= 0, /* sentinel */
1212 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1213 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1220 * Get node with specified destination prefix (and source prefix,
1221 * if subtrees are used)
1225 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1226 const struct in6_addr
*addr
,
1227 int plen
, int offset
)
1229 struct fib6_node
*fn
;
1231 for (fn
= root
; fn
; ) {
1232 struct rt6key
*key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
1237 if (plen
< fn
->fn_bit
||
1238 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1241 if (plen
== fn
->fn_bit
)
1245 * We have more bits to go
1247 if (addr_bit_set(addr
, fn
->fn_bit
))
1255 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1256 const struct in6_addr
*daddr
, int dst_len
,
1257 const struct in6_addr
*saddr
, int src_len
)
1259 struct fib6_node
*fn
;
1261 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1262 offsetof(struct rt6_info
, rt6i_dst
));
1264 #ifdef CONFIG_IPV6_SUBTREES
1266 WARN_ON(saddr
== NULL
);
1267 if (fn
&& fn
->subtree
)
1268 fn
= fib6_locate_1(fn
->subtree
, saddr
, src_len
,
1269 offsetof(struct rt6_info
, rt6i_src
));
1273 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1285 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
)
1287 if (fn
->fn_flags
& RTN_ROOT
)
1288 return net
->ipv6
.ip6_null_entry
;
1292 return fn
->left
->leaf
;
1294 return fn
->right
->leaf
;
1296 fn
= FIB6_SUBTREE(fn
);
1302 * Called to trim the tree of intermediate nodes when possible. "fn"
1303 * is the node we want to try and remove.
1306 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1307 struct fib6_node
*fn
)
1311 struct fib6_node
*child
, *pn
;
1312 struct fib6_walker
*w
;
1316 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1319 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1320 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1326 child
= fn
->right
, children
|= 1;
1328 child
= fn
->left
, children
|= 2;
1330 if (children
== 3 || FIB6_SUBTREE(fn
)
1331 #ifdef CONFIG_IPV6_SUBTREES
1332 /* Subtree root (i.e. fn) may have one child */
1333 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1336 fn
->leaf
= fib6_find_prefix(net
, fn
);
1340 fn
->leaf
= net
->ipv6
.ip6_null_entry
;
1343 atomic_inc(&fn
->leaf
->rt6i_ref
);
1348 #ifdef CONFIG_IPV6_SUBTREES
1349 if (FIB6_SUBTREE(pn
) == fn
) {
1350 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1351 FIB6_SUBTREE(pn
) = NULL
;
1354 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1356 if (pn
->right
== fn
)
1358 else if (pn
->left
== fn
)
1367 #ifdef CONFIG_IPV6_SUBTREES
1371 read_lock(&net
->ipv6
.fib6_walker_lock
);
1372 FOR_WALKERS(net
, w
) {
1374 if (w
->root
== fn
) {
1375 w
->root
= w
->node
= NULL
;
1376 RT6_TRACE("W %p adjusted by delroot 1\n", w
);
1377 } else if (w
->node
== fn
) {
1378 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1383 if (w
->root
== fn
) {
1385 RT6_TRACE("W %p adjusted by delroot 2\n", w
);
1387 if (w
->node
== fn
) {
1390 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1391 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1393 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1394 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1399 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1402 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1405 rt6_release(pn
->leaf
);
1411 static void fib6_del_route(struct fib6_node
*fn
, struct rt6_info
**rtp
,
1412 struct nl_info
*info
)
1414 struct fib6_walker
*w
;
1415 struct rt6_info
*rt
= *rtp
;
1416 struct net
*net
= info
->nl_net
;
1418 RT6_TRACE("fib6_del_route\n");
1421 *rtp
= rt
->dst
.rt6_next
;
1422 rt
->rt6i_node
= NULL
;
1423 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1424 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1426 /* Reset round-robin state, if necessary */
1427 if (fn
->rr_ptr
== rt
)
1430 /* Remove this entry from other siblings */
1431 if (rt
->rt6i_nsiblings
) {
1432 struct rt6_info
*sibling
, *next_sibling
;
1434 list_for_each_entry_safe(sibling
, next_sibling
,
1435 &rt
->rt6i_siblings
, rt6i_siblings
)
1436 sibling
->rt6i_nsiblings
--;
1437 rt
->rt6i_nsiblings
= 0;
1438 list_del_init(&rt
->rt6i_siblings
);
1441 /* Adjust walkers */
1442 read_lock(&net
->ipv6
.fib6_walker_lock
);
1443 FOR_WALKERS(net
, w
) {
1444 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1445 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1446 w
->leaf
= rt
->dst
.rt6_next
;
1451 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1453 rt
->dst
.rt6_next
= NULL
;
1455 /* If it was last route, expunge its radix tree node */
1457 fn
->fn_flags
&= ~RTN_RTINFO
;
1458 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1459 fn
= fib6_repair_tree(net
, fn
);
1462 fib6_purge_rt(rt
, fn
, net
);
1464 if (!info
->skip_notify
)
1465 inet6_rt_notify(RTM_DELROUTE
, rt
, info
, 0);
1469 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1471 struct net
*net
= info
->nl_net
;
1472 struct fib6_node
*fn
= rt
->rt6i_node
;
1473 struct rt6_info
**rtp
;
1476 if (rt
->dst
.obsolete
> 0) {
1481 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1484 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1486 if (!(rt
->rt6i_flags
& RTF_CACHE
)) {
1487 struct fib6_node
*pn
= fn
;
1488 #ifdef CONFIG_IPV6_SUBTREES
1489 /* clones of this route might be in another subtree */
1490 if (rt
->rt6i_src
.plen
) {
1491 while (!(pn
->fn_flags
& RTN_ROOT
))
1496 fib6_prune_clones(info
->nl_net
, pn
);
1500 * Walk the leaf entries looking for ourself
1503 for (rtp
= &fn
->leaf
; *rtp
; rtp
= &(*rtp
)->dst
.rt6_next
) {
1505 fib6_del_route(fn
, rtp
, info
);
1513 * Tree traversal function.
1515 * Certainly, it is not interrupt safe.
1516 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1517 * It means, that we can modify tree during walking
1518 * and use this function for garbage collection, clone pruning,
1519 * cleaning tree when a device goes down etc. etc.
1521 * It guarantees that every node will be traversed,
1522 * and that it will be traversed only once.
1524 * Callback function w->func may return:
1525 * 0 -> continue walking.
1526 * positive value -> walking is suspended (used by tree dumps,
1527 * and probably by gc, if it will be split to several slices)
1528 * negative value -> terminate walking.
1530 * The function itself returns:
1531 * 0 -> walk is complete.
1532 * >0 -> walk is incomplete (i.e. suspended)
1533 * <0 -> walk is terminated by an error.
1536 static int fib6_walk_continue(struct fib6_walker
*w
)
1538 struct fib6_node
*fn
, *pn
;
1545 if (w
->prune
&& fn
!= w
->root
&&
1546 fn
->fn_flags
& RTN_RTINFO
&& w
->state
< FWS_C
) {
1551 #ifdef CONFIG_IPV6_SUBTREES
1553 if (FIB6_SUBTREE(fn
)) {
1554 w
->node
= FIB6_SUBTREE(fn
);
1562 w
->state
= FWS_INIT
;
1568 w
->node
= fn
->right
;
1569 w
->state
= FWS_INIT
;
1575 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1597 #ifdef CONFIG_IPV6_SUBTREES
1598 if (FIB6_SUBTREE(pn
) == fn
) {
1599 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1604 if (pn
->left
== fn
) {
1608 if (pn
->right
== fn
) {
1610 w
->leaf
= w
->node
->leaf
;
1620 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
)
1624 w
->state
= FWS_INIT
;
1627 fib6_walker_link(net
, w
);
1628 res
= fib6_walk_continue(w
);
1630 fib6_walker_unlink(net
, w
);
1634 static int fib6_clean_node(struct fib6_walker
*w
)
1637 struct rt6_info
*rt
;
1638 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1639 struct nl_info info
= {
1643 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1644 w
->node
->fn_sernum
!= c
->sernum
)
1645 w
->node
->fn_sernum
= c
->sernum
;
1648 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1653 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1654 res
= c
->func(rt
, c
->arg
);
1657 res
= fib6_del(rt
, &info
);
1660 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1661 __func__
, rt
, rt
->rt6i_node
, res
);
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
) == 1 &&
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
;
1823 fib6_clean_all(net
, fib6_age
, &gc_args
);
1825 net
->ipv6
.ip6_rt_last_gc
= now
;
1828 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1830 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
1832 del_timer(&net
->ipv6
.ip6_fib_timer
);
1833 spin_unlock_bh(&net
->ipv6
.fib6_gc_lock
);
1836 static void fib6_gc_timer_cb(unsigned long arg
)
1838 fib6_run_gc(0, (struct net
*)arg
, true);
1841 static int __net_init
fib6_net_init(struct net
*net
)
1843 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
1845 spin_lock_init(&net
->ipv6
.fib6_gc_lock
);
1846 rwlock_init(&net
->ipv6
.fib6_walker_lock
);
1847 INIT_LIST_HEAD(&net
->ipv6
.fib6_walkers
);
1848 setup_timer(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, (unsigned long)net
);
1850 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
1851 if (!net
->ipv6
.rt6_stats
)
1854 /* Avoid false sharing : Use at least a full cache line */
1855 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
1857 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
1858 if (!net
->ipv6
.fib_table_hash
)
1861 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
1863 if (!net
->ipv6
.fib6_main_tbl
)
1864 goto out_fib_table_hash
;
1866 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
1867 net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1868 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
1869 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1870 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1872 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1873 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
1875 if (!net
->ipv6
.fib6_local_tbl
)
1876 goto out_fib6_main_tbl
;
1877 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
1878 net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1879 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
1880 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1881 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1883 fib6_tables_init(net
);
1887 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1889 kfree(net
->ipv6
.fib6_main_tbl
);
1892 kfree(net
->ipv6
.fib_table_hash
);
1894 kfree(net
->ipv6
.rt6_stats
);
1899 static void fib6_net_exit(struct net
*net
)
1901 rt6_ifdown(net
, NULL
);
1902 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
1904 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1905 inetpeer_invalidate_tree(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1906 kfree(net
->ipv6
.fib6_local_tbl
);
1908 inetpeer_invalidate_tree(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1909 kfree(net
->ipv6
.fib6_main_tbl
);
1910 kfree(net
->ipv6
.fib_table_hash
);
1911 kfree(net
->ipv6
.rt6_stats
);
1914 static struct pernet_operations fib6_net_ops
= {
1915 .init
= fib6_net_init
,
1916 .exit
= fib6_net_exit
,
1919 int __init
fib6_init(void)
1923 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
1924 sizeof(struct fib6_node
),
1925 0, SLAB_HWCACHE_ALIGN
,
1927 if (!fib6_node_kmem
)
1930 ret
= register_pernet_subsys(&fib6_net_ops
);
1932 goto out_kmem_cache_create
;
1934 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
1937 goto out_unregister_subsys
;
1939 __fib6_flush_trees
= fib6_flush_trees
;
1943 out_unregister_subsys
:
1944 unregister_pernet_subsys(&fib6_net_ops
);
1945 out_kmem_cache_create
:
1946 kmem_cache_destroy(fib6_node_kmem
);
1950 void fib6_gc_cleanup(void)
1952 unregister_pernet_subsys(&fib6_net_ops
);
1953 kmem_cache_destroy(fib6_node_kmem
);
1956 #ifdef CONFIG_PROC_FS
1958 struct ipv6_route_iter
{
1959 struct seq_net_private p
;
1960 struct fib6_walker w
;
1962 struct fib6_table
*tbl
;
1966 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
1968 struct rt6_info
*rt
= v
;
1969 struct ipv6_route_iter
*iter
= seq
->private;
1971 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
1973 #ifdef CONFIG_IPV6_SUBTREES
1974 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
1976 seq_puts(seq
, "00000000000000000000000000000000 00 ");
1978 if (rt
->rt6i_flags
& RTF_GATEWAY
)
1979 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
1981 seq_puts(seq
, "00000000000000000000000000000000");
1983 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
1984 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
1985 rt
->dst
.__use
, rt
->rt6i_flags
,
1986 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
1987 iter
->w
.leaf
= NULL
;
1991 static int ipv6_route_yield(struct fib6_walker
*w
)
1993 struct ipv6_route_iter
*iter
= w
->args
;
1999 iter
->w
.leaf
= iter
->w
.leaf
->dst
.rt6_next
;
2001 if (!iter
->skip
&& iter
->w
.leaf
)
2003 } while (iter
->w
.leaf
);
2008 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
,
2011 memset(&iter
->w
, 0, sizeof(iter
->w
));
2012 iter
->w
.func
= ipv6_route_yield
;
2013 iter
->w
.root
= &iter
->tbl
->tb6_root
;
2014 iter
->w
.state
= FWS_INIT
;
2015 iter
->w
.node
= iter
->w
.root
;
2016 iter
->w
.args
= iter
;
2017 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2018 INIT_LIST_HEAD(&iter
->w
.lh
);
2019 fib6_walker_link(net
, &iter
->w
);
2022 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
2026 struct hlist_node
*node
;
2029 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
2030 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
2036 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
2037 node
= rcu_dereference_bh(
2038 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
2040 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
2043 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
2045 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
2046 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2047 iter
->w
.state
= FWS_INIT
;
2048 iter
->w
.node
= iter
->w
.root
;
2049 WARN_ON(iter
->w
.skip
);
2050 iter
->w
.skip
= iter
->w
.count
;
2054 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2058 struct net
*net
= seq_file_net(seq
);
2059 struct ipv6_route_iter
*iter
= seq
->private;
2064 n
= ((struct rt6_info
*)v
)->dst
.rt6_next
;
2071 ipv6_route_check_sernum(iter
);
2072 read_lock(&iter
->tbl
->tb6_lock
);
2073 r
= fib6_walk_continue(&iter
->w
);
2074 read_unlock(&iter
->tbl
->tb6_lock
);
2078 return iter
->w
.leaf
;
2080 fib6_walker_unlink(net
, &iter
->w
);
2083 fib6_walker_unlink(net
, &iter
->w
);
2085 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2089 ipv6_route_seq_setup_walk(iter
, net
);
2093 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2096 struct net
*net
= seq_file_net(seq
);
2097 struct ipv6_route_iter
*iter
= seq
->private;
2100 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2104 ipv6_route_seq_setup_walk(iter
, net
);
2105 return ipv6_route_seq_next(seq
, NULL
, pos
);
2111 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2113 struct fib6_walker
*w
= &iter
->w
;
2114 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2117 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2120 struct net
*net
= seq_file_net(seq
);
2121 struct ipv6_route_iter
*iter
= seq
->private;
2123 if (ipv6_route_iter_active(iter
))
2124 fib6_walker_unlink(net
, &iter
->w
);
2126 rcu_read_unlock_bh();
2129 static const struct seq_operations ipv6_route_seq_ops
= {
2130 .start
= ipv6_route_seq_start
,
2131 .next
= ipv6_route_seq_next
,
2132 .stop
= ipv6_route_seq_stop
,
2133 .show
= ipv6_route_seq_show
2136 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2138 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2139 sizeof(struct ipv6_route_iter
));
2142 #endif /* CONFIG_PROC_FS */