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
;
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
);
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 net
*net
= info
->nl_net
;
1484 struct fib6_node
*fn
= rt
->rt6i_node
;
1485 struct rt6_info
**rtp
;
1488 if (rt
->dst
.obsolete
> 0) {
1493 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1496 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1498 if (!(rt
->rt6i_flags
& RTF_CACHE
)) {
1499 struct fib6_node
*pn
= fn
;
1500 #ifdef CONFIG_IPV6_SUBTREES
1501 /* clones of this route might be in another subtree */
1502 if (rt
->rt6i_src
.plen
) {
1503 while (!(pn
->fn_flags
& RTN_ROOT
))
1508 fib6_prune_clones(info
->nl_net
, pn
);
1512 * Walk the leaf entries looking for ourself
1515 for (rtp
= &fn
->leaf
; *rtp
; rtp
= &(*rtp
)->dst
.rt6_next
) {
1517 fib6_del_route(fn
, rtp
, info
);
1525 * Tree traversal function.
1527 * Certainly, it is not interrupt safe.
1528 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1529 * It means, that we can modify tree during walking
1530 * and use this function for garbage collection, clone pruning,
1531 * cleaning tree when a device goes down etc. etc.
1533 * It guarantees that every node will be traversed,
1534 * and that it will be traversed only once.
1536 * Callback function w->func may return:
1537 * 0 -> continue walking.
1538 * positive value -> walking is suspended (used by tree dumps,
1539 * and probably by gc, if it will be split to several slices)
1540 * negative value -> terminate walking.
1542 * The function itself returns:
1543 * 0 -> walk is complete.
1544 * >0 -> walk is incomplete (i.e. suspended)
1545 * <0 -> walk is terminated by an error.
1548 static int fib6_walk_continue(struct fib6_walker
*w
)
1550 struct fib6_node
*fn
, *pn
;
1557 if (w
->prune
&& fn
!= w
->root
&&
1558 fn
->fn_flags
& RTN_RTINFO
&& w
->state
< FWS_C
) {
1563 #ifdef CONFIG_IPV6_SUBTREES
1565 if (FIB6_SUBTREE(fn
)) {
1566 w
->node
= FIB6_SUBTREE(fn
);
1574 w
->state
= FWS_INIT
;
1580 w
->node
= fn
->right
;
1581 w
->state
= FWS_INIT
;
1587 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1609 #ifdef CONFIG_IPV6_SUBTREES
1610 if (FIB6_SUBTREE(pn
) == fn
) {
1611 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1616 if (pn
->left
== fn
) {
1620 if (pn
->right
== fn
) {
1622 w
->leaf
= w
->node
->leaf
;
1632 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
)
1636 w
->state
= FWS_INIT
;
1639 fib6_walker_link(net
, w
);
1640 res
= fib6_walk_continue(w
);
1642 fib6_walker_unlink(net
, w
);
1646 static int fib6_clean_node(struct fib6_walker
*w
)
1649 struct rt6_info
*rt
;
1650 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1651 struct nl_info info
= {
1655 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1656 w
->node
->fn_sernum
!= c
->sernum
)
1657 w
->node
->fn_sernum
= c
->sernum
;
1660 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1665 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1666 res
= c
->func(rt
, c
->arg
);
1669 res
= fib6_del(rt
, &info
);
1672 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1673 __func__
, rt
, rt
->rt6i_node
, res
);
1686 * Convenient frontend to tree walker.
1688 * func is called on each route.
1689 * It may return -1 -> delete this route.
1690 * 0 -> continue walking
1692 * prune==1 -> only immediate children of node (certainly,
1693 * ignoring pure split nodes) will be scanned.
1696 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1697 int (*func
)(struct rt6_info
*, void *arg
),
1698 bool prune
, int sernum
, void *arg
)
1700 struct fib6_cleaner c
;
1703 c
.w
.func
= fib6_clean_node
;
1712 fib6_walk(net
, &c
.w
);
1715 static void __fib6_clean_all(struct net
*net
,
1716 int (*func
)(struct rt6_info
*, void *),
1717 int sernum
, void *arg
)
1719 struct fib6_table
*table
;
1720 struct hlist_head
*head
;
1724 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1725 head
= &net
->ipv6
.fib_table_hash
[h
];
1726 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1727 write_lock_bh(&table
->tb6_lock
);
1728 fib6_clean_tree(net
, &table
->tb6_root
,
1729 func
, false, sernum
, arg
);
1730 write_unlock_bh(&table
->tb6_lock
);
1736 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *),
1739 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
);
1742 static int fib6_prune_clone(struct rt6_info
*rt
, void *arg
)
1744 if (rt
->rt6i_flags
& RTF_CACHE
) {
1745 RT6_TRACE("pruning clone %p\n", rt
);
1752 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
)
1754 fib6_clean_tree(net
, fn
, fib6_prune_clone
, true,
1755 FIB6_NO_SERNUM_CHANGE
, NULL
);
1758 static void fib6_flush_trees(struct net
*net
)
1760 int new_sernum
= fib6_new_sernum(net
);
1762 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
);
1766 * Garbage collection
1775 static int fib6_age(struct rt6_info
*rt
, void *arg
)
1777 struct fib6_gc_args
*gc_args
= arg
;
1778 unsigned long now
= jiffies
;
1781 * check addrconf expiration here.
1782 * Routes are expired even if they are in use.
1784 * Also age clones. Note, that clones are aged out
1785 * only if they are not in use now.
1788 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
1789 if (time_after(now
, rt
->dst
.expires
)) {
1790 RT6_TRACE("expiring %p\n", rt
);
1794 } else if (rt
->rt6i_flags
& RTF_CACHE
) {
1795 if (atomic_read(&rt
->dst
.__refcnt
) == 1 &&
1796 time_after_eq(now
, rt
->dst
.lastuse
+ gc_args
->timeout
)) {
1797 RT6_TRACE("aging clone %p\n", rt
);
1799 } else if (rt
->rt6i_flags
& RTF_GATEWAY
) {
1800 struct neighbour
*neigh
;
1801 __u8 neigh_flags
= 0;
1803 neigh
= dst_neigh_lookup(&rt
->dst
, &rt
->rt6i_gateway
);
1805 neigh_flags
= neigh
->flags
;
1806 neigh_release(neigh
);
1808 if (!(neigh_flags
& NTF_ROUTER
)) {
1809 RT6_TRACE("purging route %p via non-router but gateway\n",
1820 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
1822 struct fib6_gc_args gc_args
;
1826 spin_lock_bh(&net
->ipv6
.fib6_gc_lock
);
1827 } else if (!spin_trylock_bh(&net
->ipv6
.fib6_gc_lock
)) {
1828 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
1831 gc_args
.timeout
= expires
? (int)expires
:
1832 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
1835 fib6_clean_all(net
, fib6_age
, &gc_args
);
1837 net
->ipv6
.ip6_rt_last_gc
= now
;
1840 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1842 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
1844 del_timer(&net
->ipv6
.ip6_fib_timer
);
1845 spin_unlock_bh(&net
->ipv6
.fib6_gc_lock
);
1848 static void fib6_gc_timer_cb(unsigned long arg
)
1850 fib6_run_gc(0, (struct net
*)arg
, true);
1853 static int __net_init
fib6_net_init(struct net
*net
)
1855 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
1857 spin_lock_init(&net
->ipv6
.fib6_gc_lock
);
1858 rwlock_init(&net
->ipv6
.fib6_walker_lock
);
1859 INIT_LIST_HEAD(&net
->ipv6
.fib6_walkers
);
1860 setup_timer(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, (unsigned long)net
);
1862 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
1863 if (!net
->ipv6
.rt6_stats
)
1866 /* Avoid false sharing : Use at least a full cache line */
1867 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
1869 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
1870 if (!net
->ipv6
.fib_table_hash
)
1873 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
1875 if (!net
->ipv6
.fib6_main_tbl
)
1876 goto out_fib_table_hash
;
1878 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
1879 net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1880 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
1881 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1882 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1884 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1885 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
1887 if (!net
->ipv6
.fib6_local_tbl
)
1888 goto out_fib6_main_tbl
;
1889 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
1890 net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1891 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
1892 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1893 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1895 fib6_tables_init(net
);
1899 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1901 kfree(net
->ipv6
.fib6_main_tbl
);
1904 kfree(net
->ipv6
.fib_table_hash
);
1906 kfree(net
->ipv6
.rt6_stats
);
1911 static void fib6_net_exit(struct net
*net
)
1913 rt6_ifdown(net
, NULL
);
1914 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
1916 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1917 inetpeer_invalidate_tree(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
1918 kfree(net
->ipv6
.fib6_local_tbl
);
1920 inetpeer_invalidate_tree(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
1921 kfree(net
->ipv6
.fib6_main_tbl
);
1922 kfree(net
->ipv6
.fib_table_hash
);
1923 kfree(net
->ipv6
.rt6_stats
);
1926 static struct pernet_operations fib6_net_ops
= {
1927 .init
= fib6_net_init
,
1928 .exit
= fib6_net_exit
,
1931 int __init
fib6_init(void)
1935 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
1936 sizeof(struct fib6_node
),
1937 0, SLAB_HWCACHE_ALIGN
,
1939 if (!fib6_node_kmem
)
1942 ret
= register_pernet_subsys(&fib6_net_ops
);
1944 goto out_kmem_cache_create
;
1946 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
1949 goto out_unregister_subsys
;
1951 __fib6_flush_trees
= fib6_flush_trees
;
1955 out_unregister_subsys
:
1956 unregister_pernet_subsys(&fib6_net_ops
);
1957 out_kmem_cache_create
:
1958 kmem_cache_destroy(fib6_node_kmem
);
1962 void fib6_gc_cleanup(void)
1964 unregister_pernet_subsys(&fib6_net_ops
);
1965 kmem_cache_destroy(fib6_node_kmem
);
1968 #ifdef CONFIG_PROC_FS
1970 struct ipv6_route_iter
{
1971 struct seq_net_private p
;
1972 struct fib6_walker w
;
1974 struct fib6_table
*tbl
;
1978 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
1980 struct rt6_info
*rt
= v
;
1981 struct ipv6_route_iter
*iter
= seq
->private;
1983 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
1985 #ifdef CONFIG_IPV6_SUBTREES
1986 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
1988 seq_puts(seq
, "00000000000000000000000000000000 00 ");
1990 if (rt
->rt6i_flags
& RTF_GATEWAY
)
1991 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
1993 seq_puts(seq
, "00000000000000000000000000000000");
1995 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
1996 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
1997 rt
->dst
.__use
, rt
->rt6i_flags
,
1998 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
1999 iter
->w
.leaf
= NULL
;
2003 static int ipv6_route_yield(struct fib6_walker
*w
)
2005 struct ipv6_route_iter
*iter
= w
->args
;
2011 iter
->w
.leaf
= iter
->w
.leaf
->dst
.rt6_next
;
2013 if (!iter
->skip
&& iter
->w
.leaf
)
2015 } while (iter
->w
.leaf
);
2020 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
,
2023 memset(&iter
->w
, 0, sizeof(iter
->w
));
2024 iter
->w
.func
= ipv6_route_yield
;
2025 iter
->w
.root
= &iter
->tbl
->tb6_root
;
2026 iter
->w
.state
= FWS_INIT
;
2027 iter
->w
.node
= iter
->w
.root
;
2028 iter
->w
.args
= iter
;
2029 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2030 INIT_LIST_HEAD(&iter
->w
.lh
);
2031 fib6_walker_link(net
, &iter
->w
);
2034 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
2038 struct hlist_node
*node
;
2041 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
2042 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
2048 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
2049 node
= rcu_dereference_bh(
2050 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
2052 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
2055 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
2057 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
2058 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2059 iter
->w
.state
= FWS_INIT
;
2060 iter
->w
.node
= iter
->w
.root
;
2061 WARN_ON(iter
->w
.skip
);
2062 iter
->w
.skip
= iter
->w
.count
;
2066 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2070 struct net
*net
= seq_file_net(seq
);
2071 struct ipv6_route_iter
*iter
= seq
->private;
2076 n
= ((struct rt6_info
*)v
)->dst
.rt6_next
;
2083 ipv6_route_check_sernum(iter
);
2084 read_lock(&iter
->tbl
->tb6_lock
);
2085 r
= fib6_walk_continue(&iter
->w
);
2086 read_unlock(&iter
->tbl
->tb6_lock
);
2090 return iter
->w
.leaf
;
2092 fib6_walker_unlink(net
, &iter
->w
);
2095 fib6_walker_unlink(net
, &iter
->w
);
2097 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2101 ipv6_route_seq_setup_walk(iter
, net
);
2105 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2108 struct net
*net
= seq_file_net(seq
);
2109 struct ipv6_route_iter
*iter
= seq
->private;
2112 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2116 ipv6_route_seq_setup_walk(iter
, net
);
2117 return ipv6_route_seq_next(seq
, NULL
, pos
);
2123 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2125 struct fib6_walker
*w
= &iter
->w
;
2126 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2129 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2132 struct net
*net
= seq_file_net(seq
);
2133 struct ipv6_route_iter
*iter
= seq
->private;
2135 if (ipv6_route_iter_active(iter
))
2136 fib6_walker_unlink(net
, &iter
->w
);
2138 rcu_read_unlock_bh();
2141 static const struct seq_operations ipv6_route_seq_ops
= {
2142 .start
= ipv6_route_seq_start
,
2143 .next
= ipv6_route_seq_next
,
2144 .stop
= ipv6_route_seq_stop
,
2145 .show
= ipv6_route_seq_show
2148 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2150 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2151 sizeof(struct ipv6_route_iter
));
2154 #endif /* CONFIG_PROC_FS */