2 * Linux INET6 implementation
3 * Forwarding Information Database
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
17 * Ville Nuorvala: Fixed routing subtrees.
20 #define pr_fmt(fmt) "IPv6: " fmt
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/net.h>
25 #include <linux/route.h>
26 #include <linux/netdevice.h>
27 #include <linux/in6.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
33 #include <net/ndisc.h>
34 #include <net/addrconf.h>
35 #include <net/lwtunnel.h>
36 #include <net/fib_notifier.h>
38 #include <net/ip6_fib.h>
39 #include <net/ip6_route.h>
44 #define RT6_TRACE(x...) pr_debug(x)
46 #define RT6_TRACE(x...) do { ; } while (0)
49 static struct kmem_cache
*fib6_node_kmem __read_mostly
;
54 int (*func
)(struct rt6_info
*, void *arg
);
59 #ifdef CONFIG_IPV6_SUBTREES
60 #define FWS_INIT FWS_S
62 #define FWS_INIT FWS_L
65 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
);
66 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
);
67 static struct fib6_node
*fib6_repair_tree(struct net
*net
, struct fib6_node
*fn
);
68 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
);
69 static int fib6_walk_continue(struct fib6_walker
*w
);
72 * A routing update causes an increase of the serial number on the
73 * affected subtree. This allows for cached routes to be asynchronously
74 * tested when modifications are made to the destination cache as a
75 * result of redirects, path MTU changes, etc.
78 static void fib6_gc_timer_cb(unsigned long arg
);
80 #define FOR_WALKERS(net, w) \
81 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
83 static void fib6_walker_link(struct net
*net
, struct fib6_walker
*w
)
85 write_lock_bh(&net
->ipv6
.fib6_walker_lock
);
86 list_add(&w
->lh
, &net
->ipv6
.fib6_walkers
);
87 write_unlock_bh(&net
->ipv6
.fib6_walker_lock
);
90 static void fib6_walker_unlink(struct net
*net
, struct fib6_walker
*w
)
92 write_lock_bh(&net
->ipv6
.fib6_walker_lock
);
94 write_unlock_bh(&net
->ipv6
.fib6_walker_lock
);
97 static int fib6_new_sernum(struct net
*net
)
102 old
= atomic_read(&net
->ipv6
.fib6_sernum
);
103 new = old
< INT_MAX
? old
+ 1 : 1;
104 } while (atomic_cmpxchg(&net
->ipv6
.fib6_sernum
,
110 FIB6_NO_SERNUM_CHANGE
= 0,
114 * Auxiliary address test functions for the radix tree.
116 * These assume a 32bit processor (although it will work on
123 #if defined(__LITTLE_ENDIAN)
124 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
126 # define BITOP_BE32_SWIZZLE 0
129 static __be32
addr_bit_set(const void *token
, int fn_bit
)
131 const __be32
*addr
= token
;
134 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
135 * is optimized version of
136 * htonl(1 << ((~fn_bit)&0x1F))
137 * See include/asm-generic/bitops/le.h.
139 return (__force __be32
)(1 << ((~fn_bit
^ BITOP_BE32_SWIZZLE
) & 0x1f)) &
143 static struct fib6_node
*node_alloc(void)
145 struct fib6_node
*fn
;
147 fn
= kmem_cache_zalloc(fib6_node_kmem
, GFP_ATOMIC
);
152 static void node_free_immediate(struct fib6_node
*fn
)
154 kmem_cache_free(fib6_node_kmem
, fn
);
157 static void node_free_rcu(struct rcu_head
*head
)
159 struct fib6_node
*fn
= container_of(head
, struct fib6_node
, rcu
);
161 kmem_cache_free(fib6_node_kmem
, fn
);
164 static void node_free(struct fib6_node
*fn
)
166 call_rcu(&fn
->rcu
, node_free_rcu
);
169 void rt6_free_pcpu(struct rt6_info
*non_pcpu_rt
)
173 if (!non_pcpu_rt
->rt6i_pcpu
)
176 for_each_possible_cpu(cpu
) {
177 struct rt6_info
**ppcpu_rt
;
178 struct rt6_info
*pcpu_rt
;
180 ppcpu_rt
= per_cpu_ptr(non_pcpu_rt
->rt6i_pcpu
, cpu
);
183 dst_dev_put(&pcpu_rt
->dst
);
184 dst_release(&pcpu_rt
->dst
);
189 free_percpu(non_pcpu_rt
->rt6i_pcpu
);
190 non_pcpu_rt
->rt6i_pcpu
= NULL
;
192 EXPORT_SYMBOL_GPL(rt6_free_pcpu
);
194 static void fib6_free_table(struct fib6_table
*table
)
196 inetpeer_invalidate_tree(&table
->tb6_peers
);
200 static void fib6_link_table(struct net
*net
, struct fib6_table
*tb
)
205 * Initialize table lock at a single place to give lockdep a key,
206 * tables aren't visible prior to being linked to the list.
208 rwlock_init(&tb
->tb6_lock
);
210 h
= tb
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1);
213 * No protection necessary, this is the only list mutatation
214 * operation, tables never disappear once they exist.
216 hlist_add_head_rcu(&tb
->tb6_hlist
, &net
->ipv6
.fib_table_hash
[h
]);
219 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
221 static struct fib6_table
*fib6_alloc_table(struct net
*net
, u32 id
)
223 struct fib6_table
*table
;
225 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
228 table
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
229 table
->tb6_root
.fn_flags
= RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
230 inet_peer_base_init(&table
->tb6_peers
);
236 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
238 struct fib6_table
*tb
;
242 tb
= fib6_get_table(net
, id
);
246 tb
= fib6_alloc_table(net
, id
);
248 fib6_link_table(net
, tb
);
252 EXPORT_SYMBOL_GPL(fib6_new_table
);
254 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
256 struct fib6_table
*tb
;
257 struct hlist_head
*head
;
262 h
= id
& (FIB6_TABLE_HASHSZ
- 1);
264 head
= &net
->ipv6
.fib_table_hash
[h
];
265 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
266 if (tb
->tb6_id
== id
) {
275 EXPORT_SYMBOL_GPL(fib6_get_table
);
277 static void __net_init
fib6_tables_init(struct net
*net
)
279 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
280 fib6_link_table(net
, net
->ipv6
.fib6_local_tbl
);
284 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
)
286 return fib6_get_table(net
, id
);
289 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
)
291 return net
->ipv6
.fib6_main_tbl
;
294 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
295 int flags
, pol_lookup_t lookup
)
299 rt
= lookup(net
, net
->ipv6
.fib6_main_tbl
, fl6
, flags
);
300 if (rt
->dst
.error
== -EAGAIN
) {
302 rt
= net
->ipv6
.ip6_null_entry
;
309 static void __net_init
fib6_tables_init(struct net
*net
)
311 fib6_link_table(net
, net
->ipv6
.fib6_main_tbl
);
316 unsigned int fib6_tables_seq_read(struct net
*net
)
318 unsigned int h
, fib_seq
= 0;
321 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
322 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[h
];
323 struct fib6_table
*tb
;
325 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
326 read_lock_bh(&tb
->tb6_lock
);
327 fib_seq
+= tb
->fib_seq
;
328 read_unlock_bh(&tb
->tb6_lock
);
336 static int call_fib6_entry_notifier(struct notifier_block
*nb
, struct net
*net
,
337 enum fib_event_type event_type
,
340 struct fib6_entry_notifier_info info
= {
344 return call_fib6_notifier(nb
, net
, event_type
, &info
.info
);
347 static int call_fib6_entry_notifiers(struct net
*net
,
348 enum fib_event_type event_type
,
351 struct fib6_entry_notifier_info info
= {
355 rt
->rt6i_table
->fib_seq
++;
356 return call_fib6_notifiers(net
, event_type
, &info
.info
);
359 struct fib6_dump_arg
{
361 struct notifier_block
*nb
;
364 static void fib6_rt_dump(struct rt6_info
*rt
, struct fib6_dump_arg
*arg
)
366 if (rt
== arg
->net
->ipv6
.ip6_null_entry
)
368 call_fib6_entry_notifier(arg
->nb
, arg
->net
, FIB_EVENT_ENTRY_ADD
, rt
);
371 static int fib6_node_dump(struct fib6_walker
*w
)
375 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
)
376 fib6_rt_dump(rt
, w
->args
);
381 static void fib6_table_dump(struct net
*net
, struct fib6_table
*tb
,
382 struct fib6_walker
*w
)
384 w
->root
= &tb
->tb6_root
;
385 read_lock_bh(&tb
->tb6_lock
);
387 read_unlock_bh(&tb
->tb6_lock
);
390 /* Called with rcu_read_lock() */
391 int fib6_tables_dump(struct net
*net
, struct notifier_block
*nb
)
393 struct fib6_dump_arg arg
;
394 struct fib6_walker
*w
;
397 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
401 w
->func
= fib6_node_dump
;
406 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
407 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[h
];
408 struct fib6_table
*tb
;
410 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
)
411 fib6_table_dump(net
, tb
, w
);
419 static int fib6_dump_node(struct fib6_walker
*w
)
424 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
425 res
= rt6_dump_route(rt
, w
->args
);
427 /* Frame is full, suspend walking */
432 /* Multipath routes are dumped in one route with the
433 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
434 * last sibling of this route (no need to dump the
435 * sibling routes again)
437 if (rt
->rt6i_nsiblings
)
438 rt
= list_last_entry(&rt
->rt6i_siblings
,
446 static void fib6_dump_end(struct netlink_callback
*cb
)
448 struct net
*net
= sock_net(cb
->skb
->sk
);
449 struct fib6_walker
*w
= (void *)cb
->args
[2];
454 fib6_walker_unlink(net
, w
);
459 cb
->done
= (void *)cb
->args
[3];
463 static int fib6_dump_done(struct netlink_callback
*cb
)
466 return cb
->done
? cb
->done(cb
) : 0;
469 static int fib6_dump_table(struct fib6_table
*table
, struct sk_buff
*skb
,
470 struct netlink_callback
*cb
)
472 struct net
*net
= sock_net(skb
->sk
);
473 struct fib6_walker
*w
;
476 w
= (void *)cb
->args
[2];
477 w
->root
= &table
->tb6_root
;
479 if (cb
->args
[4] == 0) {
483 read_lock_bh(&table
->tb6_lock
);
484 res
= fib6_walk(net
, w
);
485 read_unlock_bh(&table
->tb6_lock
);
488 cb
->args
[5] = w
->root
->fn_sernum
;
491 if (cb
->args
[5] != w
->root
->fn_sernum
) {
492 /* Begin at the root if the tree changed */
493 cb
->args
[5] = w
->root
->fn_sernum
;
500 read_lock_bh(&table
->tb6_lock
);
501 res
= fib6_walk_continue(w
);
502 read_unlock_bh(&table
->tb6_lock
);
504 fib6_walker_unlink(net
, w
);
512 static int inet6_dump_fib(struct sk_buff
*skb
, struct netlink_callback
*cb
)
514 struct net
*net
= sock_net(skb
->sk
);
516 unsigned int e
= 0, s_e
;
517 struct rt6_rtnl_dump_arg arg
;
518 struct fib6_walker
*w
;
519 struct fib6_table
*tb
;
520 struct hlist_head
*head
;
526 w
= (void *)cb
->args
[2];
530 * 1. hook callback destructor.
532 cb
->args
[3] = (long)cb
->done
;
533 cb
->done
= fib6_dump_done
;
536 * 2. allocate and initialize walker.
538 w
= kzalloc(sizeof(*w
), GFP_ATOMIC
);
541 w
->func
= fib6_dump_node
;
542 cb
->args
[2] = (long)w
;
551 for (h
= s_h
; h
< FIB6_TABLE_HASHSZ
; h
++, s_e
= 0) {
553 head
= &net
->ipv6
.fib_table_hash
[h
];
554 hlist_for_each_entry_rcu(tb
, head
, tb6_hlist
) {
557 res
= fib6_dump_table(tb
, skb
, cb
);
569 res
= res
< 0 ? res
: skb
->len
;
578 * return the appropriate node for a routing tree "add" operation
579 * by either creating and inserting or by returning an existing
583 static struct fib6_node
*fib6_add_1(struct fib6_node
*root
,
584 struct in6_addr
*addr
, int plen
,
585 int offset
, int allow_create
,
586 int replace_required
, int sernum
,
587 struct netlink_ext_ack
*extack
)
589 struct fib6_node
*fn
, *in
, *ln
;
590 struct fib6_node
*pn
= NULL
;
595 RT6_TRACE("fib6_add_1\n");
597 /* insert node in tree */
602 key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
607 if (plen
< fn
->fn_bit
||
608 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
)) {
610 if (replace_required
) {
611 NL_SET_ERR_MSG(extack
,
612 "Can not replace route - no match found");
613 pr_warn("Can't replace route, no match found\n");
614 return ERR_PTR(-ENOENT
);
616 pr_warn("NLM_F_CREATE should be set when creating new route\n");
625 if (plen
== fn
->fn_bit
) {
626 /* clean up an intermediate node */
627 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
628 rt6_release(fn
->leaf
);
632 fn
->fn_sernum
= sernum
;
638 * We have more bits to go
641 /* Try to walk down on tree. */
642 fn
->fn_sernum
= sernum
;
643 dir
= addr_bit_set(addr
, fn
->fn_bit
);
645 fn
= dir
? fn
->right
: fn
->left
;
649 /* We should not create new node because
650 * NLM_F_REPLACE was specified without NLM_F_CREATE
651 * I assume it is safe to require NLM_F_CREATE when
652 * REPLACE flag is used! Later we may want to remove the
653 * check for replace_required, because according
654 * to netlink specification, NLM_F_CREATE
655 * MUST be specified if new route is created.
656 * That would keep IPv6 consistent with IPv4
658 if (replace_required
) {
659 NL_SET_ERR_MSG(extack
,
660 "Can not replace route - no match found");
661 pr_warn("Can't replace route, no match found\n");
662 return ERR_PTR(-ENOENT
);
664 pr_warn("NLM_F_CREATE should be set when creating new route\n");
667 * We walked to the bottom of tree.
668 * Create new leaf node without children.
674 return ERR_PTR(-ENOMEM
);
678 ln
->fn_sernum
= sernum
;
690 * split since we don't have a common prefix anymore or
691 * we have a less significant route.
692 * we've to insert an intermediate node on the list
693 * this new node will point to the one we need to create
699 /* find 1st bit in difference between the 2 addrs.
701 See comment in __ipv6_addr_diff: bit may be an invalid value,
702 but if it is >= plen, the value is ignored in any case.
705 bit
= __ipv6_addr_diff(addr
, &key
->addr
, sizeof(*addr
));
710 * (new leaf node)[ln] (old node)[fn]
718 node_free_immediate(in
);
720 node_free_immediate(ln
);
721 return ERR_PTR(-ENOMEM
);
725 * new intermediate node.
727 * be off since that an address that chooses one of
728 * the branches would not match less specific routes
729 * in the other branch
736 atomic_inc(&in
->leaf
->rt6i_ref
);
738 in
->fn_sernum
= sernum
;
740 /* update parent pointer */
751 ln
->fn_sernum
= sernum
;
753 if (addr_bit_set(addr
, bit
)) {
760 } else { /* plen <= bit */
763 * (new leaf node)[ln]
765 * (old node)[fn] NULL
771 return ERR_PTR(-ENOMEM
);
777 ln
->fn_sernum
= sernum
;
784 if (addr_bit_set(&key
->addr
, plen
))
794 static bool rt6_qualify_for_ecmp(struct rt6_info
*rt
)
796 return (rt
->rt6i_flags
& (RTF_GATEWAY
|RTF_ADDRCONF
|RTF_DYNAMIC
)) ==
800 static void fib6_copy_metrics(u32
*mp
, const struct mx6_config
*mxc
)
804 for (i
= 0; i
< RTAX_MAX
; i
++) {
805 if (test_bit(i
, mxc
->mx_valid
))
810 static int fib6_commit_metrics(struct dst_entry
*dst
, struct mx6_config
*mxc
)
815 if (dst
->flags
& DST_HOST
) {
816 u32
*mp
= dst_metrics_write_ptr(dst
);
821 fib6_copy_metrics(mp
, mxc
);
823 dst_init_metrics(dst
, mxc
->mx
, false);
825 /* We've stolen mx now. */
832 static void fib6_purge_rt(struct rt6_info
*rt
, struct fib6_node
*fn
,
835 if (atomic_read(&rt
->rt6i_ref
) != 1) {
836 /* This route is used as dummy address holder in some split
837 * nodes. It is not leaked, but it still holds other resources,
838 * which must be released in time. So, scan ascendant nodes
839 * and replace dummy references to this route with references
840 * to still alive ones.
843 if (!(fn
->fn_flags
& RTN_RTINFO
) && fn
->leaf
== rt
) {
844 fn
->leaf
= fib6_find_prefix(net
, fn
);
845 atomic_inc(&fn
->leaf
->rt6i_ref
);
854 * Insert routing information in a node.
857 static int fib6_add_rt2node(struct fib6_node
*fn
, struct rt6_info
*rt
,
858 struct nl_info
*info
, struct mx6_config
*mxc
)
860 struct rt6_info
*iter
= NULL
;
861 struct rt6_info
**ins
;
862 struct rt6_info
**fallback_ins
= NULL
;
863 int replace
= (info
->nlh
&&
864 (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
));
865 int add
= (!info
->nlh
||
866 (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
));
868 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
869 u16 nlflags
= NLM_F_EXCL
;
872 if (info
->nlh
&& (info
->nlh
->nlmsg_flags
& NLM_F_APPEND
))
873 nlflags
|= NLM_F_APPEND
;
877 for (iter
= fn
->leaf
; iter
; iter
= iter
->dst
.rt6_next
) {
879 * Search for duplicates
882 if (iter
->rt6i_metric
== rt
->rt6i_metric
) {
884 * Same priority level
887 (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
890 nlflags
&= ~NLM_F_EXCL
;
892 if (rt_can_ecmp
== rt6_qualify_for_ecmp(iter
)) {
897 fallback_ins
= fallback_ins
?: ins
;
901 if (rt6_duplicate_nexthop(iter
, rt
)) {
902 if (rt
->rt6i_nsiblings
)
903 rt
->rt6i_nsiblings
= 0;
904 if (!(iter
->rt6i_flags
& RTF_EXPIRES
))
906 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
907 rt6_clean_expires(iter
);
909 rt6_set_expires(iter
, rt
->dst
.expires
);
910 iter
->rt6i_pmtu
= rt
->rt6i_pmtu
;
913 /* If we have the same destination and the same metric,
914 * but not the same gateway, then the route we try to
915 * add is sibling to this route, increment our counter
916 * of siblings, and later we will add our route to the
918 * Only static routes (which don't have flag
919 * RTF_EXPIRES) are used for ECMPv6.
921 * To avoid long list, we only had siblings if the
922 * route have a gateway.
925 rt6_qualify_for_ecmp(iter
))
926 rt
->rt6i_nsiblings
++;
929 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
933 ins
= &iter
->dst
.rt6_next
;
936 if (fallback_ins
&& !found
) {
937 /* No ECMP-able route found, replace first non-ECMP one */
943 /* Reset round-robin state, if necessary */
944 if (ins
== &fn
->leaf
)
947 /* Link this route to others same route. */
948 if (rt
->rt6i_nsiblings
) {
949 unsigned int rt6i_nsiblings
;
950 struct rt6_info
*sibling
, *temp_sibling
;
952 /* Find the first route that have the same metric */
955 if (sibling
->rt6i_metric
== rt
->rt6i_metric
&&
956 rt6_qualify_for_ecmp(sibling
)) {
957 list_add_tail(&rt
->rt6i_siblings
,
958 &sibling
->rt6i_siblings
);
961 sibling
= sibling
->dst
.rt6_next
;
963 /* For each sibling in the list, increment the counter of
964 * siblings. BUG() if counters does not match, list of siblings
968 list_for_each_entry_safe(sibling
, temp_sibling
,
969 &rt
->rt6i_siblings
, rt6i_siblings
) {
970 sibling
->rt6i_nsiblings
++;
971 BUG_ON(sibling
->rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
974 BUG_ON(rt6i_nsiblings
!= rt
->rt6i_nsiblings
);
982 pr_warn("NLM_F_CREATE should be set when creating new route\n");
985 nlflags
|= NLM_F_CREATE
;
986 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
990 rt
->dst
.rt6_next
= iter
;
992 rcu_assign_pointer(rt
->rt6i_node
, fn
);
993 atomic_inc(&rt
->rt6i_ref
);
994 call_fib6_entry_notifiers(info
->nl_net
, FIB_EVENT_ENTRY_ADD
,
996 if (!info
->skip_notify
)
997 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, nlflags
);
998 info
->nl_net
->ipv6
.rt6_stats
->fib_rt_entries
++;
1000 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
1001 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
1002 fn
->fn_flags
|= RTN_RTINFO
;
1011 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1015 err
= fib6_commit_metrics(&rt
->dst
, mxc
);
1020 rcu_assign_pointer(rt
->rt6i_node
, fn
);
1021 rt
->dst
.rt6_next
= iter
->dst
.rt6_next
;
1022 atomic_inc(&rt
->rt6i_ref
);
1023 call_fib6_entry_notifiers(info
->nl_net
, FIB_EVENT_ENTRY_REPLACE
,
1025 if (!info
->skip_notify
)
1026 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, NLM_F_REPLACE
);
1027 if (!(fn
->fn_flags
& RTN_RTINFO
)) {
1028 info
->nl_net
->ipv6
.rt6_stats
->fib_route_nodes
++;
1029 fn
->fn_flags
|= RTN_RTINFO
;
1031 nsiblings
= iter
->rt6i_nsiblings
;
1032 iter
->rt6i_node
= NULL
;
1033 fib6_purge_rt(iter
, fn
, info
->nl_net
);
1034 if (fn
->rr_ptr
== iter
)
1039 /* Replacing an ECMP route, remove all siblings */
1040 ins
= &rt
->dst
.rt6_next
;
1043 if (iter
->rt6i_metric
> rt
->rt6i_metric
)
1045 if (rt6_qualify_for_ecmp(iter
)) {
1046 *ins
= iter
->dst
.rt6_next
;
1047 iter
->rt6i_node
= NULL
;
1048 fib6_purge_rt(iter
, fn
, info
->nl_net
);
1049 if (fn
->rr_ptr
== iter
)
1054 ins
= &iter
->dst
.rt6_next
;
1058 WARN_ON(nsiblings
!= 0);
1065 static void fib6_start_gc(struct net
*net
, struct rt6_info
*rt
)
1067 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
) &&
1068 (rt
->rt6i_flags
& (RTF_EXPIRES
| RTF_CACHE
)))
1069 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1070 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
1073 void fib6_force_start_gc(struct net
*net
)
1075 if (!timer_pending(&net
->ipv6
.ip6_fib_timer
))
1076 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1077 jiffies
+ net
->ipv6
.sysctl
.ip6_rt_gc_interval
);
1081 * Add routing information to the routing tree.
1082 * <destination addr>/<source addr>
1083 * with source addr info in sub-trees
1086 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
1087 struct nl_info
*info
, struct mx6_config
*mxc
,
1088 struct netlink_ext_ack
*extack
)
1090 struct fib6_node
*fn
, *pn
= NULL
;
1092 int allow_create
= 1;
1093 int replace_required
= 0;
1094 int sernum
= fib6_new_sernum(info
->nl_net
);
1096 if (WARN_ON_ONCE(!atomic_read(&rt
->dst
.__refcnt
)))
1100 if (!(info
->nlh
->nlmsg_flags
& NLM_F_CREATE
))
1102 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1103 replace_required
= 1;
1105 if (!allow_create
&& !replace_required
)
1106 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1108 fn
= fib6_add_1(root
, &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
,
1109 offsetof(struct rt6_info
, rt6i_dst
), allow_create
,
1110 replace_required
, sernum
, extack
);
1119 #ifdef CONFIG_IPV6_SUBTREES
1120 if (rt
->rt6i_src
.plen
) {
1121 struct fib6_node
*sn
;
1124 struct fib6_node
*sfn
;
1136 /* Create subtree root node */
1141 sfn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
1142 atomic_inc(&info
->nl_net
->ipv6
.ip6_null_entry
->rt6i_ref
);
1143 sfn
->fn_flags
= RTN_ROOT
;
1144 sfn
->fn_sernum
= sernum
;
1146 /* Now add the first leaf node to new subtree */
1148 sn
= fib6_add_1(sfn
, &rt
->rt6i_src
.addr
,
1150 offsetof(struct rt6_info
, rt6i_src
),
1151 allow_create
, replace_required
, sernum
,
1155 /* If it is failed, discard just allocated
1156 root, and then (in failure) stale node
1159 node_free_immediate(sfn
);
1164 /* Now link new subtree to main tree */
1168 sn
= fib6_add_1(fn
->subtree
, &rt
->rt6i_src
.addr
,
1170 offsetof(struct rt6_info
, rt6i_src
),
1171 allow_create
, replace_required
, sernum
,
1182 atomic_inc(&rt
->rt6i_ref
);
1188 err
= fib6_add_rt2node(fn
, rt
, info
, mxc
);
1190 fib6_start_gc(info
->nl_net
, rt
);
1191 if (!(rt
->rt6i_flags
& RTF_CACHE
))
1192 fib6_prune_clones(info
->nl_net
, pn
);
1197 #ifdef CONFIG_IPV6_SUBTREES
1199 * If fib6_add_1 has cleared the old leaf pointer in the
1200 * super-tree leaf node we have to find a new one for it.
1202 if (pn
!= fn
&& pn
->leaf
== rt
) {
1204 atomic_dec(&rt
->rt6i_ref
);
1206 if (pn
!= fn
&& !pn
->leaf
&& !(pn
->fn_flags
& RTN_RTINFO
)) {
1207 pn
->leaf
= fib6_find_prefix(info
->nl_net
, pn
);
1210 WARN_ON(pn
->leaf
== NULL
);
1211 pn
->leaf
= info
->nl_net
->ipv6
.ip6_null_entry
;
1214 atomic_inc(&pn
->leaf
->rt6i_ref
);
1222 /* fn->leaf could be NULL if fn is an intermediate node and we
1223 * failed to add the new route to it in both subtree creation
1224 * failure and fib6_add_rt2node() failure case.
1225 * In both cases, fib6_repair_tree() should be called to fix
1228 if (fn
&& !(fn
->fn_flags
& (RTN_RTINFO
|RTN_ROOT
)))
1229 fib6_repair_tree(info
->nl_net
, fn
);
1230 /* Always release dst as dst->__refcnt is guaranteed
1231 * to be taken before entering this function
1233 dst_release_immediate(&rt
->dst
);
1238 * Routing tree lookup
1242 struct lookup_args
{
1243 int offset
; /* key offset on rt6_info */
1244 const struct in6_addr
*addr
; /* search key */
1247 static struct fib6_node
*fib6_lookup_1(struct fib6_node
*root
,
1248 struct lookup_args
*args
)
1250 struct fib6_node
*fn
;
1253 if (unlikely(args
->offset
== 0))
1263 struct fib6_node
*next
;
1265 dir
= addr_bit_set(args
->addr
, fn
->fn_bit
);
1267 next
= dir
? fn
->right
: fn
->left
;
1277 if (FIB6_SUBTREE(fn
) || fn
->fn_flags
& RTN_RTINFO
) {
1280 key
= (struct rt6key
*) ((u8
*) fn
->leaf
+
1283 if (ipv6_prefix_equal(&key
->addr
, args
->addr
, key
->plen
)) {
1284 #ifdef CONFIG_IPV6_SUBTREES
1286 struct fib6_node
*sfn
;
1287 sfn
= fib6_lookup_1(fn
->subtree
,
1294 if (fn
->fn_flags
& RTN_RTINFO
)
1298 #ifdef CONFIG_IPV6_SUBTREES
1301 if (fn
->fn_flags
& RTN_ROOT
)
1310 struct fib6_node
*fib6_lookup(struct fib6_node
*root
, const struct in6_addr
*daddr
,
1311 const struct in6_addr
*saddr
)
1313 struct fib6_node
*fn
;
1314 struct lookup_args args
[] = {
1316 .offset
= offsetof(struct rt6_info
, rt6i_dst
),
1319 #ifdef CONFIG_IPV6_SUBTREES
1321 .offset
= offsetof(struct rt6_info
, rt6i_src
),
1326 .offset
= 0, /* sentinel */
1330 fn
= fib6_lookup_1(root
, daddr
? args
: args
+ 1);
1331 if (!fn
|| fn
->fn_flags
& RTN_TL_ROOT
)
1338 * Get node with specified destination prefix (and source prefix,
1339 * if subtrees are used)
1343 static struct fib6_node
*fib6_locate_1(struct fib6_node
*root
,
1344 const struct in6_addr
*addr
,
1345 int plen
, int offset
)
1347 struct fib6_node
*fn
;
1349 for (fn
= root
; fn
; ) {
1350 struct rt6key
*key
= (struct rt6key
*)((u8
*)fn
->leaf
+ offset
);
1355 if (plen
< fn
->fn_bit
||
1356 !ipv6_prefix_equal(&key
->addr
, addr
, fn
->fn_bit
))
1359 if (plen
== fn
->fn_bit
)
1363 * We have more bits to go
1365 if (addr_bit_set(addr
, fn
->fn_bit
))
1373 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
1374 const struct in6_addr
*daddr
, int dst_len
,
1375 const struct in6_addr
*saddr
, int src_len
)
1377 struct fib6_node
*fn
;
1379 fn
= fib6_locate_1(root
, daddr
, dst_len
,
1380 offsetof(struct rt6_info
, rt6i_dst
));
1382 #ifdef CONFIG_IPV6_SUBTREES
1384 WARN_ON(saddr
== NULL
);
1385 if (fn
&& fn
->subtree
)
1386 fn
= fib6_locate_1(fn
->subtree
, saddr
, src_len
,
1387 offsetof(struct rt6_info
, rt6i_src
));
1391 if (fn
&& fn
->fn_flags
& RTN_RTINFO
)
1403 static struct rt6_info
*fib6_find_prefix(struct net
*net
, struct fib6_node
*fn
)
1405 if (fn
->fn_flags
& RTN_ROOT
)
1406 return net
->ipv6
.ip6_null_entry
;
1410 return fn
->left
->leaf
;
1412 return fn
->right
->leaf
;
1414 fn
= FIB6_SUBTREE(fn
);
1420 * Called to trim the tree of intermediate nodes when possible. "fn"
1421 * is the node we want to try and remove.
1424 static struct fib6_node
*fib6_repair_tree(struct net
*net
,
1425 struct fib6_node
*fn
)
1429 struct fib6_node
*child
, *pn
;
1430 struct fib6_walker
*w
;
1434 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn
->fn_bit
, iter
);
1437 WARN_ON(fn
->fn_flags
& RTN_RTINFO
);
1438 WARN_ON(fn
->fn_flags
& RTN_TL_ROOT
);
1444 child
= fn
->right
, children
|= 1;
1446 child
= fn
->left
, children
|= 2;
1448 if (children
== 3 || FIB6_SUBTREE(fn
)
1449 #ifdef CONFIG_IPV6_SUBTREES
1450 /* Subtree root (i.e. fn) may have one child */
1451 || (children
&& fn
->fn_flags
& RTN_ROOT
)
1454 fn
->leaf
= fib6_find_prefix(net
, fn
);
1458 fn
->leaf
= net
->ipv6
.ip6_null_entry
;
1461 atomic_inc(&fn
->leaf
->rt6i_ref
);
1466 #ifdef CONFIG_IPV6_SUBTREES
1467 if (FIB6_SUBTREE(pn
) == fn
) {
1468 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1469 FIB6_SUBTREE(pn
) = NULL
;
1472 WARN_ON(fn
->fn_flags
& RTN_ROOT
);
1474 if (pn
->right
== fn
)
1476 else if (pn
->left
== fn
)
1485 #ifdef CONFIG_IPV6_SUBTREES
1489 read_lock(&net
->ipv6
.fib6_walker_lock
);
1490 FOR_WALKERS(net
, w
) {
1492 if (w
->root
== fn
) {
1493 w
->root
= w
->node
= NULL
;
1494 RT6_TRACE("W %p adjusted by delroot 1\n", w
);
1495 } else if (w
->node
== fn
) {
1496 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w
, w
->state
, nstate
);
1501 if (w
->root
== fn
) {
1503 RT6_TRACE("W %p adjusted by delroot 2\n", w
);
1505 if (w
->node
== fn
) {
1508 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1509 w
->state
= w
->state
>= FWS_R
? FWS_U
: FWS_INIT
;
1511 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w
, w
->state
);
1512 w
->state
= w
->state
>= FWS_C
? FWS_U
: FWS_INIT
;
1517 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1520 if (pn
->fn_flags
& RTN_RTINFO
|| FIB6_SUBTREE(pn
))
1523 rt6_release(pn
->leaf
);
1529 static void fib6_del_route(struct fib6_node
*fn
, struct rt6_info
**rtp
,
1530 struct nl_info
*info
)
1532 struct fib6_walker
*w
;
1533 struct rt6_info
*rt
= *rtp
;
1534 struct net
*net
= info
->nl_net
;
1536 RT6_TRACE("fib6_del_route\n");
1539 *rtp
= rt
->dst
.rt6_next
;
1540 rt
->rt6i_node
= NULL
;
1541 net
->ipv6
.rt6_stats
->fib_rt_entries
--;
1542 net
->ipv6
.rt6_stats
->fib_discarded_routes
++;
1544 /* Reset round-robin state, if necessary */
1545 if (fn
->rr_ptr
== rt
)
1548 /* Remove this entry from other siblings */
1549 if (rt
->rt6i_nsiblings
) {
1550 struct rt6_info
*sibling
, *next_sibling
;
1552 list_for_each_entry_safe(sibling
, next_sibling
,
1553 &rt
->rt6i_siblings
, rt6i_siblings
)
1554 sibling
->rt6i_nsiblings
--;
1555 rt
->rt6i_nsiblings
= 0;
1556 list_del_init(&rt
->rt6i_siblings
);
1559 /* Adjust walkers */
1560 read_lock(&net
->ipv6
.fib6_walker_lock
);
1561 FOR_WALKERS(net
, w
) {
1562 if (w
->state
== FWS_C
&& w
->leaf
== rt
) {
1563 RT6_TRACE("walker %p adjusted by delroute\n", w
);
1564 w
->leaf
= rt
->dst
.rt6_next
;
1569 read_unlock(&net
->ipv6
.fib6_walker_lock
);
1571 rt
->dst
.rt6_next
= NULL
;
1573 /* If it was last route, expunge its radix tree node */
1575 fn
->fn_flags
&= ~RTN_RTINFO
;
1576 net
->ipv6
.rt6_stats
->fib_route_nodes
--;
1577 fn
= fib6_repair_tree(net
, fn
);
1580 fib6_purge_rt(rt
, fn
, net
);
1582 call_fib6_entry_notifiers(net
, FIB_EVENT_ENTRY_DEL
, rt
);
1583 if (!info
->skip_notify
)
1584 inet6_rt_notify(RTM_DELROUTE
, rt
, info
, 0);
1588 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
)
1590 struct fib6_node
*fn
= rcu_dereference_protected(rt
->rt6i_node
,
1591 lockdep_is_held(&rt
->rt6i_table
->tb6_lock
));
1592 struct net
*net
= info
->nl_net
;
1593 struct rt6_info
**rtp
;
1596 if (rt
->dst
.obsolete
> 0) {
1601 if (!fn
|| rt
== net
->ipv6
.ip6_null_entry
)
1604 WARN_ON(!(fn
->fn_flags
& RTN_RTINFO
));
1606 if (!(rt
->rt6i_flags
& RTF_CACHE
)) {
1607 struct fib6_node
*pn
= fn
;
1608 #ifdef CONFIG_IPV6_SUBTREES
1609 /* clones of this route might be in another subtree */
1610 if (rt
->rt6i_src
.plen
) {
1611 while (!(pn
->fn_flags
& RTN_ROOT
))
1616 fib6_prune_clones(info
->nl_net
, pn
);
1620 * Walk the leaf entries looking for ourself
1623 for (rtp
= &fn
->leaf
; *rtp
; rtp
= &(*rtp
)->dst
.rt6_next
) {
1625 fib6_del_route(fn
, rtp
, info
);
1633 * Tree traversal function.
1635 * Certainly, it is not interrupt safe.
1636 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1637 * It means, that we can modify tree during walking
1638 * and use this function for garbage collection, clone pruning,
1639 * cleaning tree when a device goes down etc. etc.
1641 * It guarantees that every node will be traversed,
1642 * and that it will be traversed only once.
1644 * Callback function w->func may return:
1645 * 0 -> continue walking.
1646 * positive value -> walking is suspended (used by tree dumps,
1647 * and probably by gc, if it will be split to several slices)
1648 * negative value -> terminate walking.
1650 * The function itself returns:
1651 * 0 -> walk is complete.
1652 * >0 -> walk is incomplete (i.e. suspended)
1653 * <0 -> walk is terminated by an error.
1656 static int fib6_walk_continue(struct fib6_walker
*w
)
1658 struct fib6_node
*fn
, *pn
;
1665 if (w
->prune
&& fn
!= w
->root
&&
1666 fn
->fn_flags
& RTN_RTINFO
&& w
->state
< FWS_C
) {
1671 #ifdef CONFIG_IPV6_SUBTREES
1673 if (FIB6_SUBTREE(fn
)) {
1674 w
->node
= FIB6_SUBTREE(fn
);
1682 w
->state
= FWS_INIT
;
1688 w
->node
= fn
->right
;
1689 w
->state
= FWS_INIT
;
1695 if (w
->leaf
&& fn
->fn_flags
& RTN_RTINFO
) {
1717 #ifdef CONFIG_IPV6_SUBTREES
1718 if (FIB6_SUBTREE(pn
) == fn
) {
1719 WARN_ON(!(fn
->fn_flags
& RTN_ROOT
));
1724 if (pn
->left
== fn
) {
1728 if (pn
->right
== fn
) {
1730 w
->leaf
= w
->node
->leaf
;
1740 static int fib6_walk(struct net
*net
, struct fib6_walker
*w
)
1744 w
->state
= FWS_INIT
;
1747 fib6_walker_link(net
, w
);
1748 res
= fib6_walk_continue(w
);
1750 fib6_walker_unlink(net
, w
);
1754 static int fib6_clean_node(struct fib6_walker
*w
)
1757 struct rt6_info
*rt
;
1758 struct fib6_cleaner
*c
= container_of(w
, struct fib6_cleaner
, w
);
1759 struct nl_info info
= {
1763 if (c
->sernum
!= FIB6_NO_SERNUM_CHANGE
&&
1764 w
->node
->fn_sernum
!= c
->sernum
)
1765 w
->node
->fn_sernum
= c
->sernum
;
1768 WARN_ON_ONCE(c
->sernum
== FIB6_NO_SERNUM_CHANGE
);
1773 for (rt
= w
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1774 res
= c
->func(rt
, c
->arg
);
1777 res
= fib6_del(rt
, &info
);
1780 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1782 rcu_access_pointer(rt
->rt6i_node
),
1796 * Convenient frontend to tree walker.
1798 * func is called on each route.
1799 * It may return -1 -> delete this route.
1800 * 0 -> continue walking
1802 * prune==1 -> only immediate children of node (certainly,
1803 * ignoring pure split nodes) will be scanned.
1806 static void fib6_clean_tree(struct net
*net
, struct fib6_node
*root
,
1807 int (*func
)(struct rt6_info
*, void *arg
),
1808 bool prune
, int sernum
, void *arg
)
1810 struct fib6_cleaner c
;
1813 c
.w
.func
= fib6_clean_node
;
1822 fib6_walk(net
, &c
.w
);
1825 static void __fib6_clean_all(struct net
*net
,
1826 int (*func
)(struct rt6_info
*, void *),
1827 int sernum
, void *arg
)
1829 struct fib6_table
*table
;
1830 struct hlist_head
*head
;
1834 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
1835 head
= &net
->ipv6
.fib_table_hash
[h
];
1836 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
1837 write_lock_bh(&table
->tb6_lock
);
1838 fib6_clean_tree(net
, &table
->tb6_root
,
1839 func
, false, sernum
, arg
);
1840 write_unlock_bh(&table
->tb6_lock
);
1846 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *),
1849 __fib6_clean_all(net
, func
, FIB6_NO_SERNUM_CHANGE
, arg
);
1852 static int fib6_prune_clone(struct rt6_info
*rt
, void *arg
)
1854 if (rt
->rt6i_flags
& RTF_CACHE
) {
1855 RT6_TRACE("pruning clone %p\n", rt
);
1862 static void fib6_prune_clones(struct net
*net
, struct fib6_node
*fn
)
1864 fib6_clean_tree(net
, fn
, fib6_prune_clone
, true,
1865 FIB6_NO_SERNUM_CHANGE
, NULL
);
1868 static void fib6_flush_trees(struct net
*net
)
1870 int new_sernum
= fib6_new_sernum(net
);
1872 __fib6_clean_all(net
, NULL
, new_sernum
, NULL
);
1876 * Garbage collection
1885 static int fib6_age(struct rt6_info
*rt
, void *arg
)
1887 struct fib6_gc_args
*gc_args
= arg
;
1888 unsigned long now
= jiffies
;
1891 * check addrconf expiration here.
1892 * Routes are expired even if they are in use.
1894 * Also age clones. Note, that clones are aged out
1895 * only if they are not in use now.
1898 if (rt
->rt6i_flags
& RTF_EXPIRES
&& rt
->dst
.expires
) {
1899 if (time_after(now
, rt
->dst
.expires
)) {
1900 RT6_TRACE("expiring %p\n", rt
);
1904 } else if (rt
->rt6i_flags
& RTF_CACHE
) {
1905 if (time_after_eq(now
, rt
->dst
.lastuse
+ gc_args
->timeout
))
1906 rt
->dst
.obsolete
= DST_OBSOLETE_KILL
;
1907 if (atomic_read(&rt
->dst
.__refcnt
) == 1 &&
1908 rt
->dst
.obsolete
== DST_OBSOLETE_KILL
) {
1909 RT6_TRACE("aging clone %p\n", rt
);
1911 } else if (rt
->rt6i_flags
& RTF_GATEWAY
) {
1912 struct neighbour
*neigh
;
1913 __u8 neigh_flags
= 0;
1915 neigh
= dst_neigh_lookup(&rt
->dst
, &rt
->rt6i_gateway
);
1917 neigh_flags
= neigh
->flags
;
1918 neigh_release(neigh
);
1920 if (!(neigh_flags
& NTF_ROUTER
)) {
1921 RT6_TRACE("purging route %p via non-router but gateway\n",
1932 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
)
1934 struct fib6_gc_args gc_args
;
1938 spin_lock_bh(&net
->ipv6
.fib6_gc_lock
);
1939 } else if (!spin_trylock_bh(&net
->ipv6
.fib6_gc_lock
)) {
1940 mod_timer(&net
->ipv6
.ip6_fib_timer
, jiffies
+ HZ
);
1943 gc_args
.timeout
= expires
? (int)expires
:
1944 net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
1947 fib6_clean_all(net
, fib6_age
, &gc_args
);
1949 net
->ipv6
.ip6_rt_last_gc
= now
;
1952 mod_timer(&net
->ipv6
.ip6_fib_timer
,
1954 + net
->ipv6
.sysctl
.ip6_rt_gc_interval
));
1956 del_timer(&net
->ipv6
.ip6_fib_timer
);
1957 spin_unlock_bh(&net
->ipv6
.fib6_gc_lock
);
1960 static void fib6_gc_timer_cb(unsigned long arg
)
1962 fib6_run_gc(0, (struct net
*)arg
, true);
1965 static int __net_init
fib6_net_init(struct net
*net
)
1967 size_t size
= sizeof(struct hlist_head
) * FIB6_TABLE_HASHSZ
;
1970 err
= fib6_notifier_init(net
);
1974 spin_lock_init(&net
->ipv6
.fib6_gc_lock
);
1975 rwlock_init(&net
->ipv6
.fib6_walker_lock
);
1976 INIT_LIST_HEAD(&net
->ipv6
.fib6_walkers
);
1977 setup_timer(&net
->ipv6
.ip6_fib_timer
, fib6_gc_timer_cb
, (unsigned long)net
);
1979 net
->ipv6
.rt6_stats
= kzalloc(sizeof(*net
->ipv6
.rt6_stats
), GFP_KERNEL
);
1980 if (!net
->ipv6
.rt6_stats
)
1983 /* Avoid false sharing : Use at least a full cache line */
1984 size
= max_t(size_t, size
, L1_CACHE_BYTES
);
1986 net
->ipv6
.fib_table_hash
= kzalloc(size
, GFP_KERNEL
);
1987 if (!net
->ipv6
.fib_table_hash
)
1990 net
->ipv6
.fib6_main_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_main_tbl
),
1992 if (!net
->ipv6
.fib6_main_tbl
)
1993 goto out_fib_table_hash
;
1995 net
->ipv6
.fib6_main_tbl
->tb6_id
= RT6_TABLE_MAIN
;
1996 net
->ipv6
.fib6_main_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
1997 net
->ipv6
.fib6_main_tbl
->tb6_root
.fn_flags
=
1998 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
1999 inet_peer_base_init(&net
->ipv6
.fib6_main_tbl
->tb6_peers
);
2001 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2002 net
->ipv6
.fib6_local_tbl
= kzalloc(sizeof(*net
->ipv6
.fib6_local_tbl
),
2004 if (!net
->ipv6
.fib6_local_tbl
)
2005 goto out_fib6_main_tbl
;
2006 net
->ipv6
.fib6_local_tbl
->tb6_id
= RT6_TABLE_LOCAL
;
2007 net
->ipv6
.fib6_local_tbl
->tb6_root
.leaf
= net
->ipv6
.ip6_null_entry
;
2008 net
->ipv6
.fib6_local_tbl
->tb6_root
.fn_flags
=
2009 RTN_ROOT
| RTN_TL_ROOT
| RTN_RTINFO
;
2010 inet_peer_base_init(&net
->ipv6
.fib6_local_tbl
->tb6_peers
);
2012 fib6_tables_init(net
);
2016 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2018 kfree(net
->ipv6
.fib6_main_tbl
);
2021 kfree(net
->ipv6
.fib_table_hash
);
2023 kfree(net
->ipv6
.rt6_stats
);
2025 fib6_notifier_exit(net
);
2029 static void fib6_net_exit(struct net
*net
)
2033 rt6_ifdown(net
, NULL
);
2034 del_timer_sync(&net
->ipv6
.ip6_fib_timer
);
2036 for (i
= 0; i
< FIB6_TABLE_HASHSZ
; i
++) {
2037 struct hlist_head
*head
= &net
->ipv6
.fib_table_hash
[i
];
2038 struct hlist_node
*tmp
;
2039 struct fib6_table
*tb
;
2041 hlist_for_each_entry_safe(tb
, tmp
, head
, tb6_hlist
) {
2042 hlist_del(&tb
->tb6_hlist
);
2043 fib6_free_table(tb
);
2047 kfree(net
->ipv6
.fib_table_hash
);
2048 kfree(net
->ipv6
.rt6_stats
);
2049 fib6_notifier_exit(net
);
2052 static struct pernet_operations fib6_net_ops
= {
2053 .init
= fib6_net_init
,
2054 .exit
= fib6_net_exit
,
2057 int __init
fib6_init(void)
2061 fib6_node_kmem
= kmem_cache_create("fib6_nodes",
2062 sizeof(struct fib6_node
),
2063 0, SLAB_HWCACHE_ALIGN
,
2065 if (!fib6_node_kmem
)
2068 ret
= register_pernet_subsys(&fib6_net_ops
);
2070 goto out_kmem_cache_create
;
2072 ret
= __rtnl_register(PF_INET6
, RTM_GETROUTE
, NULL
, inet6_dump_fib
,
2075 goto out_unregister_subsys
;
2077 __fib6_flush_trees
= fib6_flush_trees
;
2081 out_unregister_subsys
:
2082 unregister_pernet_subsys(&fib6_net_ops
);
2083 out_kmem_cache_create
:
2084 kmem_cache_destroy(fib6_node_kmem
);
2088 void fib6_gc_cleanup(void)
2090 unregister_pernet_subsys(&fib6_net_ops
);
2091 kmem_cache_destroy(fib6_node_kmem
);
2094 #ifdef CONFIG_PROC_FS
2096 struct ipv6_route_iter
{
2097 struct seq_net_private p
;
2098 struct fib6_walker w
;
2100 struct fib6_table
*tbl
;
2104 static int ipv6_route_seq_show(struct seq_file
*seq
, void *v
)
2106 struct rt6_info
*rt
= v
;
2107 struct ipv6_route_iter
*iter
= seq
->private;
2109 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
2111 #ifdef CONFIG_IPV6_SUBTREES
2112 seq_printf(seq
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
2114 seq_puts(seq
, "00000000000000000000000000000000 00 ");
2116 if (rt
->rt6i_flags
& RTF_GATEWAY
)
2117 seq_printf(seq
, "%pi6", &rt
->rt6i_gateway
);
2119 seq_puts(seq
, "00000000000000000000000000000000");
2121 seq_printf(seq
, " %08x %08x %08x %08x %8s\n",
2122 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
2123 rt
->dst
.__use
, rt
->rt6i_flags
,
2124 rt
->dst
.dev
? rt
->dst
.dev
->name
: "");
2125 iter
->w
.leaf
= NULL
;
2129 static int ipv6_route_yield(struct fib6_walker
*w
)
2131 struct ipv6_route_iter
*iter
= w
->args
;
2137 iter
->w
.leaf
= iter
->w
.leaf
->dst
.rt6_next
;
2139 if (!iter
->skip
&& iter
->w
.leaf
)
2141 } while (iter
->w
.leaf
);
2146 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter
*iter
,
2149 memset(&iter
->w
, 0, sizeof(iter
->w
));
2150 iter
->w
.func
= ipv6_route_yield
;
2151 iter
->w
.root
= &iter
->tbl
->tb6_root
;
2152 iter
->w
.state
= FWS_INIT
;
2153 iter
->w
.node
= iter
->w
.root
;
2154 iter
->w
.args
= iter
;
2155 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2156 INIT_LIST_HEAD(&iter
->w
.lh
);
2157 fib6_walker_link(net
, &iter
->w
);
2160 static struct fib6_table
*ipv6_route_seq_next_table(struct fib6_table
*tbl
,
2164 struct hlist_node
*node
;
2167 h
= (tbl
->tb6_id
& (FIB6_TABLE_HASHSZ
- 1)) + 1;
2168 node
= rcu_dereference_bh(hlist_next_rcu(&tbl
->tb6_hlist
));
2174 while (!node
&& h
< FIB6_TABLE_HASHSZ
) {
2175 node
= rcu_dereference_bh(
2176 hlist_first_rcu(&net
->ipv6
.fib_table_hash
[h
++]));
2178 return hlist_entry_safe(node
, struct fib6_table
, tb6_hlist
);
2181 static void ipv6_route_check_sernum(struct ipv6_route_iter
*iter
)
2183 if (iter
->sernum
!= iter
->w
.root
->fn_sernum
) {
2184 iter
->sernum
= iter
->w
.root
->fn_sernum
;
2185 iter
->w
.state
= FWS_INIT
;
2186 iter
->w
.node
= iter
->w
.root
;
2187 WARN_ON(iter
->w
.skip
);
2188 iter
->w
.skip
= iter
->w
.count
;
2192 static void *ipv6_route_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2196 struct net
*net
= seq_file_net(seq
);
2197 struct ipv6_route_iter
*iter
= seq
->private;
2202 n
= ((struct rt6_info
*)v
)->dst
.rt6_next
;
2209 ipv6_route_check_sernum(iter
);
2210 read_lock(&iter
->tbl
->tb6_lock
);
2211 r
= fib6_walk_continue(&iter
->w
);
2212 read_unlock(&iter
->tbl
->tb6_lock
);
2216 return iter
->w
.leaf
;
2218 fib6_walker_unlink(net
, &iter
->w
);
2221 fib6_walker_unlink(net
, &iter
->w
);
2223 iter
->tbl
= ipv6_route_seq_next_table(iter
->tbl
, net
);
2227 ipv6_route_seq_setup_walk(iter
, net
);
2231 static void *ipv6_route_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2234 struct net
*net
= seq_file_net(seq
);
2235 struct ipv6_route_iter
*iter
= seq
->private;
2238 iter
->tbl
= ipv6_route_seq_next_table(NULL
, net
);
2242 ipv6_route_seq_setup_walk(iter
, net
);
2243 return ipv6_route_seq_next(seq
, NULL
, pos
);
2249 static bool ipv6_route_iter_active(struct ipv6_route_iter
*iter
)
2251 struct fib6_walker
*w
= &iter
->w
;
2252 return w
->node
&& !(w
->state
== FWS_U
&& w
->node
== w
->root
);
2255 static void ipv6_route_seq_stop(struct seq_file
*seq
, void *v
)
2258 struct net
*net
= seq_file_net(seq
);
2259 struct ipv6_route_iter
*iter
= seq
->private;
2261 if (ipv6_route_iter_active(iter
))
2262 fib6_walker_unlink(net
, &iter
->w
);
2264 rcu_read_unlock_bh();
2267 static const struct seq_operations ipv6_route_seq_ops
= {
2268 .start
= ipv6_route_seq_start
,
2269 .next
= ipv6_route_seq_next
,
2270 .stop
= ipv6_route_seq_stop
,
2271 .show
= ipv6_route_seq_show
2274 int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2276 return seq_open_net(inode
, file
, &ipv6_route_seq_ops
,
2277 sizeof(struct ipv6_route_iter
));
2280 #endif /* CONFIG_PROC_FS */