xref: /linux/net/ipv6/ip6_fib.c (revision 2ed4b46b4fc77749cb0f8dd31a01441b82c8dbaa)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Linux INET6 implementation
4  *	Forwarding Information Database
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *
9  *	Changes:
10  *	Yuji SEKIYA @USAGI:	Support default route on router node;
11  *				remove ip6_null_entry from the top of
12  *				routing table.
13  *	Ville Nuorvala:		Fixed routing subtrees.
14  */
15 
16 #define pr_fmt(fmt) "IPv6: " fmt
17 
18 #include <linux/bpf.h>
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/net.h>
22 #include <linux/route.h>
23 #include <linux/netdevice.h>
24 #include <linux/in6.h>
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/slab.h>
28 
29 #include <net/ip.h>
30 #include <net/ipv6.h>
31 #include <net/ndisc.h>
32 #include <net/addrconf.h>
33 #include <net/lwtunnel.h>
34 #include <net/fib_notifier.h>
35 
36 #include <net/ip_fib.h>
37 #include <net/ip6_fib.h>
38 #include <net/ip6_route.h>
39 
40 static struct kmem_cache *fib6_node_kmem __read_mostly;
41 
42 struct fib6_cleaner {
43 	struct fib6_walker w;
44 	struct net *net;
45 	int (*func)(struct fib6_info *, void *arg);
46 	int sernum;
47 	void *arg;
48 	bool skip_notify;
49 };
50 
51 #ifdef CONFIG_IPV6_SUBTREES
52 #define FWS_INIT FWS_S
53 #else
54 #define FWS_INIT FWS_L
55 #endif
56 
57 static struct fib6_info *fib6_find_prefix(struct net *net,
58 					 struct fib6_table *table,
59 					 struct fib6_node *fn);
60 static struct fib6_node *fib6_repair_tree(struct net *net,
61 					  struct fib6_table *table,
62 					  struct fib6_node *fn);
63 static int fib6_walk(struct net *net, struct fib6_walker *w);
64 static int fib6_walk_continue(struct fib6_walker *w);
65 
66 /*
67  *	A routing update causes an increase of the serial number on the
68  *	affected subtree. This allows for cached routes to be asynchronously
69  *	tested when modifications are made to the destination cache as a
70  *	result of redirects, path MTU changes, etc.
71  */
72 
73 static void fib6_gc_timer_cb(struct timer_list *t);
74 
75 #define FOR_WALKERS(net, w) \
76 	list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
77 
78 static void fib6_walker_link(struct net *net, struct fib6_walker *w)
79 {
80 	write_lock_bh(&net->ipv6.fib6_walker_lock);
81 	list_add(&w->lh, &net->ipv6.fib6_walkers);
82 	write_unlock_bh(&net->ipv6.fib6_walker_lock);
83 }
84 
85 static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
86 {
87 	write_lock_bh(&net->ipv6.fib6_walker_lock);
88 	list_del(&w->lh);
89 	write_unlock_bh(&net->ipv6.fib6_walker_lock);
90 }
91 
92 static int fib6_new_sernum(struct net *net)
93 {
94 	int new, old = atomic_read(&net->ipv6.fib6_sernum);
95 
96 	do {
97 		new = old < INT_MAX ? old + 1 : 1;
98 	} while (!atomic_try_cmpxchg(&net->ipv6.fib6_sernum, &old, new));
99 
100 	return new;
101 }
102 
103 enum {
104 	FIB6_NO_SERNUM_CHANGE = 0,
105 };
106 
107 void fib6_update_sernum(struct net *net, struct fib6_info *f6i)
108 {
109 	struct fib6_node *fn;
110 
111 	fn = rcu_dereference_protected(f6i->fib6_node,
112 			lockdep_is_held(&f6i->fib6_table->tb6_lock));
113 	if (fn)
114 		WRITE_ONCE(fn->fn_sernum, fib6_new_sernum(net));
115 }
116 
117 /*
118  *	Auxiliary address test functions for the radix tree.
119  *
120  *	These assume a 32bit processor (although it will work on
121  *	64bit processors)
122  */
123 
124 /*
125  *	test bit
126  */
127 #if defined(__LITTLE_ENDIAN)
128 # define BITOP_BE32_SWIZZLE	(0x1F & ~7)
129 #else
130 # define BITOP_BE32_SWIZZLE	0
131 #endif
132 
133 static __be32 addr_bit_set(const void *token, int fn_bit)
134 {
135 	const __be32 *addr = token;
136 	/*
137 	 * Here,
138 	 *	1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
139 	 * is optimized version of
140 	 *	htonl(1 << ((~fn_bit)&0x1F))
141 	 * See include/asm-generic/bitops/le.h.
142 	 */
143 	return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
144 	       addr[fn_bit >> 5];
145 }
146 
147 struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh)
148 {
149 	struct fib6_info *f6i;
150 	size_t sz = sizeof(*f6i);
151 
152 	if (with_fib6_nh)
153 		sz += sizeof(struct fib6_nh);
154 
155 	f6i = kzalloc(sz, gfp_flags);
156 	if (!f6i)
157 		return NULL;
158 
159 	/* fib6_siblings is a union with nh_list, so this initializes both */
160 	INIT_LIST_HEAD(&f6i->fib6_siblings);
161 	refcount_set(&f6i->fib6_ref, 1);
162 
163 	INIT_HLIST_NODE(&f6i->gc_link);
164 
165 	return f6i;
166 }
167 
168 void fib6_info_destroy_rcu(struct rcu_head *head)
169 {
170 	struct fib6_info *f6i = container_of(head, struct fib6_info, rcu);
171 
172 	WARN_ON(f6i->fib6_node);
173 
174 	if (f6i->nh)
175 		nexthop_put(f6i->nh);
176 	else
177 		fib6_nh_release(f6i->fib6_nh);
178 
179 	ip_fib_metrics_put(f6i->fib6_metrics);
180 	kfree(f6i);
181 }
182 EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu);
183 
184 static struct fib6_node *node_alloc(struct net *net)
185 {
186 	struct fib6_node *fn;
187 
188 	fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
189 	if (fn)
190 		net->ipv6.rt6_stats->fib_nodes++;
191 
192 	return fn;
193 }
194 
195 static void node_free_immediate(struct net *net, struct fib6_node *fn)
196 {
197 	kmem_cache_free(fib6_node_kmem, fn);
198 	net->ipv6.rt6_stats->fib_nodes--;
199 }
200 
201 static void node_free(struct net *net, struct fib6_node *fn)
202 {
203 	kfree_rcu(fn, rcu);
204 	net->ipv6.rt6_stats->fib_nodes--;
205 }
206 
207 static void fib6_free_table(struct fib6_table *table)
208 {
209 	inetpeer_invalidate_tree(&table->tb6_peers);
210 	kfree(table);
211 }
212 
213 static void fib6_link_table(struct net *net, struct fib6_table *tb)
214 {
215 	unsigned int h;
216 
217 	/*
218 	 * Initialize table lock at a single place to give lockdep a key,
219 	 * tables aren't visible prior to being linked to the list.
220 	 */
221 	spin_lock_init(&tb->tb6_lock);
222 	h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
223 
224 	/*
225 	 * No protection necessary, this is the only list mutatation
226 	 * operation, tables never disappear once they exist.
227 	 */
228 	hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
229 }
230 
231 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
232 
233 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
234 {
235 	struct fib6_table *table;
236 
237 	table = kzalloc_obj(*table, GFP_ATOMIC);
238 	if (table) {
239 		table->tb6_id = id;
240 		rcu_assign_pointer(table->tb6_root.leaf,
241 				   net->ipv6.fib6_null_entry);
242 		table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
243 		inet_peer_base_init(&table->tb6_peers);
244 		INIT_HLIST_HEAD(&table->tb6_gc_hlist);
245 	}
246 
247 	return table;
248 }
249 
250 struct fib6_table *fib6_new_table(struct net *net, u32 id)
251 {
252 	struct fib6_table *tb, *new_tb;
253 
254 	if (id == 0)
255 		id = RT6_TABLE_MAIN;
256 
257 	tb = fib6_get_table(net, id);
258 	if (tb)
259 		return tb;
260 
261 	new_tb = fib6_alloc_table(net, id);
262 	if (!new_tb)
263 		return NULL;
264 
265 	spin_lock_bh(&net->ipv6.fib_table_hash_lock);
266 
267 	tb = fib6_get_table(net, id);
268 	if (unlikely(tb)) {
269 		spin_unlock_bh(&net->ipv6.fib_table_hash_lock);
270 		kfree(new_tb);
271 		return tb;
272 	}
273 
274 	fib6_link_table(net, new_tb);
275 
276 	spin_unlock_bh(&net->ipv6.fib_table_hash_lock);
277 
278 	return new_tb;
279 }
280 EXPORT_SYMBOL_GPL(fib6_new_table);
281 
282 struct fib6_table *fib6_get_table(struct net *net, u32 id)
283 {
284 	struct hlist_head *head;
285 	struct fib6_table *tb;
286 
287 	if (!id)
288 		id = RT6_TABLE_MAIN;
289 
290 	head = &net->ipv6.fib_table_hash[id & (FIB6_TABLE_HASHSZ - 1)];
291 
292 	/* See comment in fib6_link_table().  RCU is not required,
293 	 * but rcu_dereference_raw() is used to avoid data-race.
294 	 */
295 	hlist_for_each_entry_rcu(tb, head, tb6_hlist, true)
296 		if (tb->tb6_id == id)
297 			return tb;
298 
299 	return NULL;
300 }
301 EXPORT_SYMBOL_GPL(fib6_get_table);
302 
303 static void __net_init fib6_tables_init(struct net *net)
304 {
305 	fib6_link_table(net, net->ipv6.fib6_main_tbl);
306 	fib6_link_table(net, net->ipv6.fib6_local_tbl);
307 }
308 #else
309 
310 struct fib6_table *fib6_new_table(struct net *net, u32 id)
311 {
312 	return fib6_get_table(net, id);
313 }
314 
315 struct fib6_table *fib6_get_table(struct net *net, u32 id)
316 {
317 	  return net->ipv6.fib6_main_tbl;
318 }
319 
320 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
321 				   const struct sk_buff *skb,
322 				   int flags, pol_lookup_t lookup)
323 {
324 	struct rt6_info *rt;
325 
326 	rt = pol_lookup_func(lookup,
327 			net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
328 	if (rt->dst.error == -EAGAIN) {
329 		ip6_rt_put_flags(rt, flags);
330 		rt = net->ipv6.ip6_null_entry;
331 		if (!(flags & RT6_LOOKUP_F_DST_NOREF))
332 			dst_hold(&rt->dst);
333 	}
334 
335 	return &rt->dst;
336 }
337 
338 /* called with rcu lock held; no reference taken on fib6_info */
339 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
340 		struct fib6_result *res, int flags)
341 {
342 	return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6,
343 				 res, flags);
344 }
345 #if IS_MODULE(CONFIG_NFT_FIB_IPV6)
346 EXPORT_SYMBOL_GPL(fib6_lookup);
347 #endif
348 
349 static void __net_init fib6_tables_init(struct net *net)
350 {
351 	fib6_link_table(net, net->ipv6.fib6_main_tbl);
352 }
353 
354 #endif
355 
356 unsigned int fib6_tables_seq_read(const struct net *net)
357 {
358 	unsigned int h, fib_seq = 0;
359 
360 	rcu_read_lock();
361 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
362 		const struct hlist_head *head = &net->ipv6.fib_table_hash[h];
363 		const struct fib6_table *tb;
364 
365 		hlist_for_each_entry_rcu(tb, head, tb6_hlist)
366 			fib_seq += READ_ONCE(tb->fib_seq);
367 	}
368 	rcu_read_unlock();
369 
370 	return fib_seq;
371 }
372 
373 static int call_fib6_entry_notifier(struct notifier_block *nb,
374 				    enum fib_event_type event_type,
375 				    struct fib6_info *rt,
376 				    struct netlink_ext_ack *extack)
377 {
378 	struct fib6_entry_notifier_info info = {
379 		.info.extack = extack,
380 		.rt = rt,
381 	};
382 
383 	return call_fib6_notifier(nb, event_type, &info.info);
384 }
385 
386 static int call_fib6_multipath_entry_notifier(struct notifier_block *nb,
387 					      enum fib_event_type event_type,
388 					      struct fib6_info *rt,
389 					      unsigned int nsiblings,
390 					      struct netlink_ext_ack *extack)
391 {
392 	struct fib6_entry_notifier_info info = {
393 		.info.extack = extack,
394 		.rt = rt,
395 		.nsiblings = nsiblings,
396 	};
397 
398 	return call_fib6_notifier(nb, event_type, &info.info);
399 }
400 
401 int call_fib6_entry_notifiers(struct net *net,
402 			      enum fib_event_type event_type,
403 			      struct fib6_info *rt,
404 			      struct netlink_ext_ack *extack)
405 {
406 	struct fib6_entry_notifier_info info = {
407 		.info.extack = extack,
408 		.rt = rt,
409 	};
410 
411 	WRITE_ONCE(rt->fib6_table->fib_seq, rt->fib6_table->fib_seq + 1);
412 	return call_fib6_notifiers(net, event_type, &info.info);
413 }
414 
415 int call_fib6_multipath_entry_notifiers(struct net *net,
416 					enum fib_event_type event_type,
417 					struct fib6_info *rt,
418 					unsigned int nsiblings,
419 					struct netlink_ext_ack *extack)
420 {
421 	struct fib6_entry_notifier_info info = {
422 		.info.extack = extack,
423 		.rt = rt,
424 		.nsiblings = nsiblings,
425 	};
426 
427 	WRITE_ONCE(rt->fib6_table->fib_seq, rt->fib6_table->fib_seq + 1);
428 	return call_fib6_notifiers(net, event_type, &info.info);
429 }
430 
431 int call_fib6_entry_notifiers_replace(struct net *net, struct fib6_info *rt)
432 {
433 	struct fib6_entry_notifier_info info = {
434 		.rt = rt,
435 		.nsiblings = rt->fib6_nsiblings,
436 	};
437 
438 	WRITE_ONCE(rt->fib6_table->fib_seq, rt->fib6_table->fib_seq + 1);
439 	return call_fib6_notifiers(net, FIB_EVENT_ENTRY_REPLACE, &info.info);
440 }
441 
442 struct fib6_dump_arg {
443 	struct net *net;
444 	struct notifier_block *nb;
445 	struct netlink_ext_ack *extack;
446 };
447 
448 static int fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
449 {
450 	enum fib_event_type fib_event = FIB_EVENT_ENTRY_REPLACE;
451 	unsigned int nsiblings;
452 	int err;
453 
454 	if (!rt || rt == arg->net->ipv6.fib6_null_entry)
455 		return 0;
456 
457 	nsiblings = READ_ONCE(rt->fib6_nsiblings);
458 	if (nsiblings)
459 		err = call_fib6_multipath_entry_notifier(arg->nb, fib_event,
460 							 rt,
461 							 nsiblings,
462 							 arg->extack);
463 	else
464 		err = call_fib6_entry_notifier(arg->nb, fib_event, rt,
465 					       arg->extack);
466 
467 	return err;
468 }
469 
470 static int fib6_node_dump(struct fib6_walker *w)
471 {
472 	int err;
473 
474 	err = fib6_rt_dump(w->leaf, w->args);
475 	w->leaf = NULL;
476 	return err;
477 }
478 
479 static int fib6_table_dump(struct net *net, struct fib6_table *tb,
480 			   struct fib6_walker *w)
481 {
482 	int err;
483 
484 	w->root = &tb->tb6_root;
485 	spin_lock_bh(&tb->tb6_lock);
486 	err = fib6_walk(net, w);
487 	spin_unlock_bh(&tb->tb6_lock);
488 	return err;
489 }
490 
491 /* Called with rcu_read_lock() */
492 int fib6_tables_dump(struct net *net, struct notifier_block *nb,
493 		     struct netlink_ext_ack *extack)
494 {
495 	struct fib6_dump_arg arg;
496 	struct fib6_walker *w;
497 	unsigned int h;
498 	int err = 0;
499 
500 	w = kzalloc_obj(*w, GFP_ATOMIC);
501 	if (!w)
502 		return -ENOMEM;
503 
504 	w->func = fib6_node_dump;
505 	arg.net = net;
506 	arg.nb = nb;
507 	arg.extack = extack;
508 	w->args = &arg;
509 
510 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
511 		struct hlist_head *head = &net->ipv6.fib_table_hash[h];
512 		struct fib6_table *tb;
513 
514 		hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
515 			err = fib6_table_dump(net, tb, w);
516 			if (err)
517 				goto out;
518 		}
519 	}
520 
521 out:
522 	kfree(w);
523 
524 	/* The tree traversal function should never return a positive value. */
525 	return err > 0 ? -EINVAL : err;
526 }
527 
528 static int fib6_dump_node(struct fib6_walker *w)
529 {
530 	int res;
531 	struct fib6_info *rt;
532 
533 	for_each_fib6_walker_rt(w) {
534 		res = rt6_dump_route(rt, w->args, w->skip_in_node);
535 		if (res >= 0) {
536 			/* Frame is full, suspend walking */
537 			w->leaf = rt;
538 
539 			/* We'll restart from this node, so if some routes were
540 			 * already dumped, skip them next time.
541 			 */
542 			w->skip_in_node += res;
543 
544 			return 1;
545 		}
546 		w->skip_in_node = 0;
547 
548 		/* Multipath routes are dumped in one route with the
549 		 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
550 		 * last sibling of this route (no need to dump the
551 		 * sibling routes again)
552 		 */
553 		if (rt->fib6_nsiblings)
554 			rt = list_last_entry(&rt->fib6_siblings,
555 					     struct fib6_info,
556 					     fib6_siblings);
557 	}
558 	w->leaf = NULL;
559 	return 0;
560 }
561 
562 static void fib6_dump_end(struct netlink_callback *cb)
563 {
564 	struct net *net = sock_net(cb->skb->sk);
565 	struct fib6_walker *w = (void *)cb->args[2];
566 
567 	if (w) {
568 		if (cb->args[4]) {
569 			cb->args[4] = 0;
570 			fib6_walker_unlink(net, w);
571 		}
572 		cb->args[2] = 0;
573 		kfree(w);
574 	}
575 	cb->done = (void *)cb->args[3];
576 	cb->args[1] = 3;
577 }
578 
579 static int fib6_dump_done(struct netlink_callback *cb)
580 {
581 	fib6_dump_end(cb);
582 	return cb->done ? cb->done(cb) : 0;
583 }
584 
585 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
586 			   struct netlink_callback *cb)
587 {
588 	struct net *net = sock_net(skb->sk);
589 	struct fib6_walker *w;
590 	int res;
591 
592 	w = (void *)cb->args[2];
593 	w->root = &table->tb6_root;
594 
595 	if (cb->args[4] == 0) {
596 		w->count = 0;
597 		w->skip = 0;
598 		w->skip_in_node = 0;
599 
600 		spin_lock_bh(&table->tb6_lock);
601 		res = fib6_walk(net, w);
602 		spin_unlock_bh(&table->tb6_lock);
603 		if (res > 0) {
604 			cb->args[4] = 1;
605 			cb->args[5] = READ_ONCE(w->root->fn_sernum);
606 		}
607 	} else {
608 		int sernum = READ_ONCE(w->root->fn_sernum);
609 		if (cb->args[5] != sernum) {
610 			/* Begin at the root if the tree changed */
611 			cb->args[5] = sernum;
612 			w->state = FWS_INIT;
613 			w->node = w->root;
614 			w->skip = w->count;
615 			w->skip_in_node = 0;
616 		} else
617 			w->skip = 0;
618 
619 		spin_lock_bh(&table->tb6_lock);
620 		res = fib6_walk_continue(w);
621 		spin_unlock_bh(&table->tb6_lock);
622 		if (res <= 0) {
623 			fib6_walker_unlink(net, w);
624 			cb->args[4] = 0;
625 		}
626 	}
627 
628 	return res;
629 }
630 
631 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
632 {
633 	struct rt6_rtnl_dump_arg arg = {
634 		.filter.dump_exceptions = true,
635 		.filter.dump_routes = true,
636 		.filter.rtnl_held = false,
637 	};
638 	const struct nlmsghdr *nlh = cb->nlh;
639 	struct net *net = sock_net(skb->sk);
640 	unsigned int e = 0, s_e;
641 	struct hlist_head *head;
642 	struct fib6_walker *w;
643 	struct fib6_table *tb;
644 	unsigned int h, s_h;
645 	int err = 0;
646 
647 	rcu_read_lock();
648 	if (cb->strict_check) {
649 		err = ip_valid_fib_dump_req(net, nlh, &arg.filter, cb);
650 		if (err < 0)
651 			goto unlock;
652 	} else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
653 		struct rtmsg *rtm = nlmsg_data(nlh);
654 
655 		if (rtm->rtm_flags & RTM_F_PREFIX)
656 			arg.filter.flags = RTM_F_PREFIX;
657 	}
658 
659 	w = (void *)cb->args[2];
660 	if (!w) {
661 		/* New dump:
662 		 *
663 		 * 1. allocate and initialize walker.
664 		 */
665 		w = kzalloc_obj(*w, GFP_ATOMIC);
666 		if (!w) {
667 			err = -ENOMEM;
668 			goto unlock;
669 		}
670 		w->func = fib6_dump_node;
671 		cb->args[2] = (long)w;
672 
673 		/* 2. hook callback destructor.
674 		 */
675 		cb->args[3] = (long)cb->done;
676 		cb->done = fib6_dump_done;
677 
678 	}
679 
680 	arg.skb = skb;
681 	arg.cb = cb;
682 	arg.net = net;
683 	w->args = &arg;
684 
685 	if (arg.filter.table_id) {
686 		tb = fib6_get_table(net, arg.filter.table_id);
687 		if (!tb) {
688 			if (rtnl_msg_family(cb->nlh) != PF_INET6)
689 				goto unlock;
690 
691 			NL_SET_ERR_MSG_MOD(cb->extack, "FIB table does not exist");
692 			err = -ENOENT;
693 			goto unlock;
694 		}
695 
696 		if (!cb->args[0]) {
697 			err = fib6_dump_table(tb, skb, cb);
698 			if (!err)
699 				cb->args[0] = 1;
700 		}
701 		goto unlock;
702 	}
703 
704 	s_h = cb->args[0];
705 	s_e = cb->args[1];
706 
707 	for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
708 		e = 0;
709 		head = &net->ipv6.fib_table_hash[h];
710 		hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
711 			if (e < s_e)
712 				goto next;
713 			err = fib6_dump_table(tb, skb, cb);
714 			if (err != 0)
715 				goto out;
716 next:
717 			e++;
718 		}
719 	}
720 out:
721 	cb->args[1] = e;
722 	cb->args[0] = h;
723 
724 unlock:
725 	rcu_read_unlock();
726 	if (err <= 0)
727 		fib6_dump_end(cb);
728 	return err;
729 }
730 
731 void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
732 {
733 	if (!f6i)
734 		return;
735 
736 	if (f6i->fib6_metrics == &dst_default_metrics) {
737 		struct dst_metrics *p = kzalloc_obj(*p, GFP_ATOMIC);
738 
739 		if (!p)
740 			return;
741 
742 		refcount_set(&p->refcnt, 1);
743 		f6i->fib6_metrics = p;
744 	}
745 
746 	f6i->fib6_metrics->metrics[metric - 1] = val;
747 }
748 
749 /*
750  *	Routing Table
751  *
752  *	return the appropriate node for a routing tree "add" operation
753  *	by either creating and inserting or by returning an existing
754  *	node.
755  */
756 
757 static struct fib6_node *fib6_add_1(struct net *net,
758 				    struct fib6_table *table,
759 				    struct fib6_node *root,
760 				    struct in6_addr *addr, int plen,
761 				    int offset, int allow_create,
762 				    int replace_required,
763 				    struct netlink_ext_ack *extack)
764 {
765 	struct fib6_node *fn, *in, *ln;
766 	struct fib6_node *pn = NULL;
767 	struct rt6key *key;
768 	int	bit;
769 	__be32	dir = 0;
770 
771 	/* insert node in tree */
772 
773 	fn = root;
774 
775 	do {
776 		struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
777 					    lockdep_is_held(&table->tb6_lock));
778 		key = (struct rt6key *)((u8 *)leaf + offset);
779 
780 		/*
781 		 *	Prefix match
782 		 */
783 		if (plen < fn->fn_bit ||
784 		    !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
785 			if (!allow_create) {
786 				if (replace_required) {
787 					NL_SET_ERR_MSG(extack,
788 						       "Can not replace route - no match found");
789 					pr_warn("Can't replace route, no match found\n");
790 					return ERR_PTR(-ENOENT);
791 				}
792 				pr_warn("NLM_F_CREATE should be set when creating new route\n");
793 			}
794 			goto insert_above;
795 		}
796 
797 		/*
798 		 *	Exact match ?
799 		 */
800 
801 		if (plen == fn->fn_bit) {
802 			/* clean up an intermediate node */
803 			if (!(fn->fn_flags & RTN_RTINFO)) {
804 				RCU_INIT_POINTER(fn->leaf, NULL);
805 				fib6_info_release(leaf);
806 			/* remove null_entry in the root node */
807 			} else if (fn->fn_flags & RTN_TL_ROOT &&
808 				   rcu_access_pointer(fn->leaf) ==
809 				   net->ipv6.fib6_null_entry) {
810 				RCU_INIT_POINTER(fn->leaf, NULL);
811 			}
812 
813 			return fn;
814 		}
815 
816 		/*
817 		 *	We have more bits to go
818 		 */
819 
820 		/* Try to walk down on tree. */
821 		dir = addr_bit_set(addr, fn->fn_bit);
822 		pn = fn;
823 		fn = dir ?
824 		     rcu_dereference_protected(fn->right,
825 					lockdep_is_held(&table->tb6_lock)) :
826 		     rcu_dereference_protected(fn->left,
827 					lockdep_is_held(&table->tb6_lock));
828 	} while (fn);
829 
830 	if (!allow_create) {
831 		/* We should not create new node because
832 		 * NLM_F_REPLACE was specified without NLM_F_CREATE
833 		 * I assume it is safe to require NLM_F_CREATE when
834 		 * REPLACE flag is used! Later we may want to remove the
835 		 * check for replace_required, because according
836 		 * to netlink specification, NLM_F_CREATE
837 		 * MUST be specified if new route is created.
838 		 * That would keep IPv6 consistent with IPv4
839 		 */
840 		if (replace_required) {
841 			NL_SET_ERR_MSG(extack,
842 				       "Can not replace route - no match found");
843 			pr_warn("Can't replace route, no match found\n");
844 			return ERR_PTR(-ENOENT);
845 		}
846 		pr_warn("NLM_F_CREATE should be set when creating new route\n");
847 	}
848 	/*
849 	 *	We walked to the bottom of tree.
850 	 *	Create new leaf node without children.
851 	 */
852 
853 	ln = node_alloc(net);
854 
855 	if (!ln)
856 		return ERR_PTR(-ENOMEM);
857 	ln->fn_bit = plen;
858 	RCU_INIT_POINTER(ln->parent, pn);
859 
860 	if (dir)
861 		rcu_assign_pointer(pn->right, ln);
862 	else
863 		rcu_assign_pointer(pn->left, ln);
864 
865 	return ln;
866 
867 
868 insert_above:
869 	/*
870 	 * split since we don't have a common prefix anymore or
871 	 * we have a less significant route.
872 	 * we've to insert an intermediate node on the list
873 	 * this new node will point to the one we need to create
874 	 * and the current
875 	 */
876 
877 	pn = rcu_dereference_protected(fn->parent,
878 				       lockdep_is_held(&table->tb6_lock));
879 
880 	/* find 1st bit in difference between the 2 addrs.
881 
882 	   See comment in __ipv6_addr_diff: bit may be an invalid value,
883 	   but if it is >= plen, the value is ignored in any case.
884 	 */
885 
886 	bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
887 
888 	/*
889 	 *		(intermediate)[in]
890 	 *	          /	   \
891 	 *	(new leaf node)[ln] (old node)[fn]
892 	 */
893 	if (plen > bit) {
894 		in = node_alloc(net);
895 		ln = node_alloc(net);
896 
897 		if (!in || !ln) {
898 			if (in)
899 				node_free_immediate(net, in);
900 			if (ln)
901 				node_free_immediate(net, ln);
902 			return ERR_PTR(-ENOMEM);
903 		}
904 
905 		/*
906 		 * new intermediate node.
907 		 * RTN_RTINFO will
908 		 * be off since that an address that chooses one of
909 		 * the branches would not match less specific routes
910 		 * in the other branch
911 		 */
912 
913 		in->fn_bit = bit;
914 
915 		RCU_INIT_POINTER(in->parent, pn);
916 		in->leaf = fn->leaf;
917 		fib6_info_hold(rcu_dereference_protected(in->leaf,
918 				lockdep_is_held(&table->tb6_lock)));
919 
920 		/* update parent pointer */
921 		if (dir)
922 			rcu_assign_pointer(pn->right, in);
923 		else
924 			rcu_assign_pointer(pn->left, in);
925 
926 		ln->fn_bit = plen;
927 
928 		RCU_INIT_POINTER(ln->parent, in);
929 		rcu_assign_pointer(fn->parent, in);
930 
931 		if (addr_bit_set(addr, bit)) {
932 			rcu_assign_pointer(in->right, ln);
933 			rcu_assign_pointer(in->left, fn);
934 		} else {
935 			rcu_assign_pointer(in->left, ln);
936 			rcu_assign_pointer(in->right, fn);
937 		}
938 	} else { /* plen <= bit */
939 
940 		/*
941 		 *		(new leaf node)[ln]
942 		 *	          /	   \
943 		 *	     (old node)[fn] NULL
944 		 */
945 
946 		ln = node_alloc(net);
947 
948 		if (!ln)
949 			return ERR_PTR(-ENOMEM);
950 
951 		ln->fn_bit = plen;
952 
953 		RCU_INIT_POINTER(ln->parent, pn);
954 
955 		if (addr_bit_set(&key->addr, plen))
956 			RCU_INIT_POINTER(ln->right, fn);
957 		else
958 			RCU_INIT_POINTER(ln->left, fn);
959 
960 		rcu_assign_pointer(fn->parent, ln);
961 
962 		if (dir)
963 			rcu_assign_pointer(pn->right, ln);
964 		else
965 			rcu_assign_pointer(pn->left, ln);
966 	}
967 	return ln;
968 }
969 
970 static void __fib6_drop_pcpu_from(struct fib6_nh *fib6_nh,
971 				  const struct fib6_info *match)
972 {
973 	int cpu;
974 
975 	if (!fib6_nh->rt6i_pcpu)
976 		return;
977 
978 	rcu_read_lock();
979 	/* release the reference to this fib entry from
980 	 * all of its cached pcpu routes
981 	 */
982 	for_each_possible_cpu(cpu) {
983 		struct rt6_info **ppcpu_rt;
984 		struct rt6_info *pcpu_rt;
985 
986 		ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
987 
988 		/* Paired with xchg() in rt6_get_pcpu_route() */
989 		pcpu_rt = READ_ONCE(*ppcpu_rt);
990 
991 		/* only dropping the 'from' reference if the cached route
992 		 * is using 'match'. The cached pcpu_rt->from only changes
993 		 * from a fib6_info to NULL (ip6_dst_destroy); it can never
994 		 * change from one fib6_info reference to another
995 		 */
996 		if (pcpu_rt && rcu_access_pointer(pcpu_rt->from) == match) {
997 			struct fib6_info *from;
998 
999 			from = unrcu_pointer(xchg(&pcpu_rt->from, NULL));
1000 			fib6_info_release(from);
1001 		}
1002 	}
1003 	rcu_read_unlock();
1004 }
1005 
1006 static int fib6_nh_drop_pcpu_from(struct fib6_nh *nh, void *_arg)
1007 {
1008 	struct fib6_info *arg = _arg;
1009 
1010 	__fib6_drop_pcpu_from(nh, arg);
1011 	return 0;
1012 }
1013 
1014 static void fib6_drop_pcpu_from(struct fib6_info *f6i)
1015 {
1016 	/* Make sure rt6_make_pcpu_route() wont add other percpu routes
1017 	 * while we are cleaning them here.
1018 	 */
1019 	f6i->fib6_destroying = 1;
1020 	mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
1021 
1022 	if (f6i->nh) {
1023 		rcu_read_lock();
1024 		nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_drop_pcpu_from, f6i);
1025 		rcu_read_unlock();
1026 	} else {
1027 		struct fib6_nh *fib6_nh;
1028 
1029 		fib6_nh = f6i->fib6_nh;
1030 		__fib6_drop_pcpu_from(fib6_nh, f6i);
1031 	}
1032 }
1033 
1034 static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
1035 			  struct net *net)
1036 {
1037 	struct fib6_table *table = rt->fib6_table;
1038 
1039 	/* Flush all cached dst in exception table */
1040 	rt6_flush_exceptions(rt);
1041 	fib6_drop_pcpu_from(rt);
1042 
1043 	if (rt->nh) {
1044 		spin_lock(&rt->nh->lock);
1045 
1046 		if (!list_empty(&rt->nh_list))
1047 			list_del_init(&rt->nh_list);
1048 
1049 		spin_unlock(&rt->nh->lock);
1050 	}
1051 
1052 	if (refcount_read(&rt->fib6_ref) != 1) {
1053 		/* This route is used as dummy address holder in some split
1054 		 * nodes. It is not leaked, but it still holds other resources,
1055 		 * which must be released in time. So, scan ascendant nodes
1056 		 * and replace dummy references to this route with references
1057 		 * to still alive ones.
1058 		 */
1059 		while (fn) {
1060 			struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
1061 					    lockdep_is_held(&table->tb6_lock));
1062 			struct fib6_info *new_leaf;
1063 			if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
1064 				new_leaf = fib6_find_prefix(net, table, fn);
1065 				fib6_info_hold(new_leaf);
1066 
1067 				rcu_assign_pointer(fn->leaf, new_leaf);
1068 				fib6_info_release(rt);
1069 			}
1070 			fn = rcu_dereference_protected(fn->parent,
1071 				    lockdep_is_held(&table->tb6_lock));
1072 		}
1073 	}
1074 
1075 	fib6_clean_expires(rt);
1076 	fib6_remove_gc_list(rt);
1077 }
1078 
1079 /*
1080  *	Insert routing information in a node.
1081  */
1082 
1083 static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
1084 			    struct nl_info *info, struct netlink_ext_ack *extack,
1085 			    struct list_head *purge_list)
1086 {
1087 	struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
1088 				    lockdep_is_held(&rt->fib6_table->tb6_lock));
1089 	struct fib6_info *iter = NULL;
1090 	struct fib6_info __rcu **ins;
1091 	struct fib6_info __rcu **fallback_ins = NULL;
1092 	int replace = (info->nlh &&
1093 		       (info->nlh->nlmsg_flags & NLM_F_REPLACE));
1094 	int add = (!info->nlh ||
1095 		   (info->nlh->nlmsg_flags & NLM_F_CREATE));
1096 	int found = 0;
1097 	bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
1098 	bool notify_sibling_rt = false;
1099 	u16 nlflags = NLM_F_EXCL;
1100 	int err;
1101 
1102 	if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
1103 		nlflags |= NLM_F_APPEND;
1104 
1105 	ins = &fn->leaf;
1106 
1107 	for (iter = leaf; iter;
1108 	     iter = rcu_dereference_protected(iter->fib6_next,
1109 				lockdep_is_held(&rt->fib6_table->tb6_lock))) {
1110 		/*
1111 		 *	Search for duplicates
1112 		 */
1113 
1114 		if (iter->fib6_metric == rt->fib6_metric) {
1115 			/*
1116 			 *	Same priority level
1117 			 */
1118 			if (info->nlh &&
1119 			    (info->nlh->nlmsg_flags & NLM_F_EXCL))
1120 				return -EEXIST;
1121 
1122 			nlflags &= ~NLM_F_EXCL;
1123 			if (replace) {
1124 				if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
1125 					found++;
1126 					break;
1127 				}
1128 				fallback_ins = fallback_ins ?: ins;
1129 				goto next_iter;
1130 			}
1131 
1132 			if (rt6_duplicate_nexthop(iter, rt)) {
1133 				if (rt->fib6_nsiblings)
1134 					WRITE_ONCE(rt->fib6_nsiblings, 0);
1135 				if (!(iter->fib6_flags & RTF_EXPIRES))
1136 					return -EEXIST;
1137 				if (!(rt->fib6_flags & RTF_EXPIRES)) {
1138 					fib6_clean_expires(iter);
1139 					fib6_remove_gc_list(iter);
1140 				} else {
1141 					fib6_set_expires(iter, rt->expires);
1142 					fib6_add_gc_list(iter);
1143 				}
1144 				if (!(rt->fib6_flags & (RTF_ADDRCONF | RTF_PREFIX_RT)) &&
1145 				    (iter->nh || !iter->fib6_nh->fib_nh_gw_family)) {
1146 					iter->fib6_flags &= ~RTF_ADDRCONF;
1147 					iter->fib6_flags &= ~RTF_PREFIX_RT;
1148 				}
1149 
1150 				if (rt->fib6_pmtu)
1151 					fib6_metric_set(iter, RTAX_MTU,
1152 							rt->fib6_pmtu);
1153 				return -EEXIST;
1154 			}
1155 			/* If we have the same destination and the same metric,
1156 			 * but not the same gateway, then the route we try to
1157 			 * add is sibling to this route, increment our counter
1158 			 * of siblings, and later we will add our route to the
1159 			 * list.
1160 			 * Only static routes (which don't have flag
1161 			 * RTF_EXPIRES) are used for ECMPv6.
1162 			 *
1163 			 * To avoid long list, we only had siblings if the
1164 			 * route have a gateway.
1165 			 */
1166 			if (rt_can_ecmp &&
1167 			    rt6_qualify_for_ecmp(iter))
1168 				WRITE_ONCE(rt->fib6_nsiblings,
1169 					   rt->fib6_nsiblings + 1);
1170 		}
1171 
1172 		if (iter->fib6_metric > rt->fib6_metric)
1173 			break;
1174 
1175 next_iter:
1176 		ins = &iter->fib6_next;
1177 	}
1178 
1179 	if (fallback_ins && !found) {
1180 		/* No matching route with same ecmp-able-ness found, replace
1181 		 * first matching route
1182 		 */
1183 		ins = fallback_ins;
1184 		iter = rcu_dereference_protected(*ins,
1185 				    lockdep_is_held(&rt->fib6_table->tb6_lock));
1186 		found++;
1187 	}
1188 
1189 	/* Reset round-robin state, if necessary */
1190 	if (ins == &fn->leaf)
1191 		fn->rr_ptr = NULL;
1192 
1193 	/* Link this route to others same route. */
1194 	if (rt->fib6_nsiblings) {
1195 		unsigned int fib6_nsiblings;
1196 		struct fib6_info *sibling, *temp_sibling;
1197 
1198 		/* Find the first route that have the same metric */
1199 		sibling = leaf;
1200 		notify_sibling_rt = true;
1201 		while (sibling) {
1202 			if (sibling->fib6_metric == rt->fib6_metric &&
1203 			    rt6_qualify_for_ecmp(sibling)) {
1204 				list_add_tail_rcu(&rt->fib6_siblings,
1205 						  &sibling->fib6_siblings);
1206 				break;
1207 			}
1208 			sibling = rcu_dereference_protected(sibling->fib6_next,
1209 				    lockdep_is_held(&rt->fib6_table->tb6_lock));
1210 			notify_sibling_rt = false;
1211 		}
1212 		/* For each sibling in the list, increment the counter of
1213 		 * siblings. BUG() if counters does not match, list of siblings
1214 		 * is broken!
1215 		 */
1216 		fib6_nsiblings = 0;
1217 		list_for_each_entry_safe(sibling, temp_sibling,
1218 					 &rt->fib6_siblings, fib6_siblings) {
1219 			WRITE_ONCE(sibling->fib6_nsiblings,
1220 				   sibling->fib6_nsiblings + 1);
1221 			BUG_ON(sibling->fib6_nsiblings != rt->fib6_nsiblings);
1222 			fib6_nsiblings++;
1223 		}
1224 		BUG_ON(fib6_nsiblings != rt->fib6_nsiblings);
1225 		rcu_read_lock();
1226 		rt6_multipath_rebalance(temp_sibling);
1227 		rcu_read_unlock();
1228 	}
1229 
1230 	/*
1231 	 *	insert node
1232 	 */
1233 	if (!replace) {
1234 		if (!add)
1235 			pr_warn("NLM_F_CREATE should be set when creating new route\n");
1236 
1237 add:
1238 		nlflags |= NLM_F_CREATE;
1239 
1240 		/* The route should only be notified if it is the first
1241 		 * route in the node or if it is added as a sibling
1242 		 * route to the first route in the node.
1243 		 */
1244 		if (!info->skip_notify_kernel &&
1245 		    (notify_sibling_rt || ins == &fn->leaf)) {
1246 			enum fib_event_type fib_event;
1247 
1248 			if (notify_sibling_rt)
1249 				fib_event = FIB_EVENT_ENTRY_APPEND;
1250 			else
1251 				fib_event = FIB_EVENT_ENTRY_REPLACE;
1252 			err = call_fib6_entry_notifiers(info->nl_net,
1253 							fib_event, rt,
1254 							extack);
1255 			if (err) {
1256 				struct fib6_info *sibling, *next_sibling;
1257 
1258 				/* If the route has siblings, then it first
1259 				 * needs to be unlinked from them.
1260 				 */
1261 				if (!rt->fib6_nsiblings)
1262 					return err;
1263 
1264 				list_for_each_entry_safe(sibling, next_sibling,
1265 							 &rt->fib6_siblings,
1266 							 fib6_siblings)
1267 					WRITE_ONCE(sibling->fib6_nsiblings,
1268 						   sibling->fib6_nsiblings - 1);
1269 				WRITE_ONCE(rt->fib6_nsiblings, 0);
1270 				list_del_rcu(&rt->fib6_siblings);
1271 				rcu_read_lock();
1272 				rt6_multipath_rebalance(next_sibling);
1273 				rcu_read_unlock();
1274 				return err;
1275 			}
1276 		}
1277 
1278 		rcu_assign_pointer(rt->fib6_next, iter);
1279 		fib6_info_hold(rt);
1280 		rcu_assign_pointer(rt->fib6_node, fn);
1281 		rcu_assign_pointer(*ins, rt);
1282 		if (!info->skip_notify)
1283 			inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1284 		info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1285 
1286 		if (!(fn->fn_flags & RTN_RTINFO)) {
1287 			info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1288 			fn->fn_flags |= RTN_RTINFO;
1289 		}
1290 
1291 	} else {
1292 		int nsiblings;
1293 
1294 		if (!found) {
1295 			if (add)
1296 				goto add;
1297 			pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1298 			return -ENOENT;
1299 		}
1300 
1301 		if (!info->skip_notify_kernel && ins == &fn->leaf) {
1302 			err = call_fib6_entry_notifiers(info->nl_net,
1303 							FIB_EVENT_ENTRY_REPLACE,
1304 							rt, extack);
1305 			if (err)
1306 				return err;
1307 		}
1308 
1309 		fib6_info_hold(rt);
1310 		rcu_assign_pointer(rt->fib6_node, fn);
1311 		rt->fib6_next = iter->fib6_next;
1312 		rcu_assign_pointer(*ins, rt);
1313 		if (!info->skip_notify)
1314 			inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1315 		if (!(fn->fn_flags & RTN_RTINFO)) {
1316 			info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1317 			fn->fn_flags |= RTN_RTINFO;
1318 		}
1319 		nsiblings = iter->fib6_nsiblings;
1320 		iter->fib6_node = NULL;
1321 		list_add(&iter->purge_link, purge_list);
1322 		if (rcu_access_pointer(fn->rr_ptr) == iter)
1323 			fn->rr_ptr = NULL;
1324 
1325 		if (nsiblings) {
1326 			/* Replacing an ECMP route, remove all siblings */
1327 			ins = &rt->fib6_next;
1328 			iter = rcu_dereference_protected(*ins,
1329 				    lockdep_is_held(&rt->fib6_table->tb6_lock));
1330 			while (iter) {
1331 				if (iter->fib6_metric > rt->fib6_metric)
1332 					break;
1333 				if (rt6_qualify_for_ecmp(iter)) {
1334 					*ins = iter->fib6_next;
1335 					iter->fib6_node = NULL;
1336 					list_add(&iter->purge_link, purge_list);
1337 					if (rcu_access_pointer(fn->rr_ptr) == iter)
1338 						fn->rr_ptr = NULL;
1339 					nsiblings--;
1340 					info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1341 				} else {
1342 					ins = &iter->fib6_next;
1343 				}
1344 				iter = rcu_dereference_protected(*ins,
1345 					lockdep_is_held(&rt->fib6_table->tb6_lock));
1346 			}
1347 			WARN_ON(nsiblings != 0);
1348 		}
1349 	}
1350 
1351 	return 0;
1352 }
1353 
1354 static int fib6_add_rt2node_nh(struct fib6_node *fn, struct fib6_info *rt,
1355 			       struct nl_info *info, struct netlink_ext_ack *extack,
1356 			       struct list_head *purge_list)
1357 {
1358 	int err;
1359 
1360 	spin_lock(&rt->nh->lock);
1361 
1362 	if (rt->nh->dead) {
1363 		NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
1364 		err = -EINVAL;
1365 	} else {
1366 		err = fib6_add_rt2node(fn, rt, info, extack, purge_list);
1367 		if (!err)
1368 			list_add(&rt->nh_list, &rt->nh->f6i_list);
1369 	}
1370 
1371 	spin_unlock(&rt->nh->lock);
1372 
1373 	return err;
1374 }
1375 
1376 static void fib6_start_gc(struct net *net, struct fib6_info *rt)
1377 {
1378 	if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1379 	    (rt->fib6_flags & RTF_EXPIRES))
1380 		mod_timer(&net->ipv6.ip6_fib_timer,
1381 			  jiffies + READ_ONCE(net->ipv6.sysctl.ip6_rt_gc_interval));
1382 }
1383 
1384 void fib6_force_start_gc(struct net *net)
1385 {
1386 	if (!timer_pending(&net->ipv6.ip6_fib_timer))
1387 		mod_timer(&net->ipv6.ip6_fib_timer,
1388 			  jiffies + READ_ONCE(net->ipv6.sysctl.ip6_rt_gc_interval));
1389 }
1390 
1391 static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
1392 					   int sernum)
1393 {
1394 	struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
1395 				lockdep_is_held(&rt->fib6_table->tb6_lock));
1396 
1397 	/* paired with smp_rmb() in fib6_get_cookie_safe() */
1398 	smp_wmb();
1399 	while (fn) {
1400 		WRITE_ONCE(fn->fn_sernum, sernum);
1401 		fn = rcu_dereference_protected(fn->parent,
1402 				lockdep_is_held(&rt->fib6_table->tb6_lock));
1403 	}
1404 }
1405 
1406 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
1407 {
1408 	__fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
1409 }
1410 
1411 /* allow ipv4 to update sernum via ipv6_stub */
1412 void fib6_update_sernum_stub(struct net *net, struct fib6_info *f6i)
1413 {
1414 	spin_lock_bh(&f6i->fib6_table->tb6_lock);
1415 	fib6_update_sernum_upto_root(net, f6i);
1416 	spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1417 }
1418 
1419 /*
1420  *	Add routing information to the routing tree.
1421  *	<destination addr>/<source addr>
1422  *	with source addr info in sub-trees
1423  *	Need to own table->tb6_lock
1424  */
1425 
1426 int fib6_add(struct fib6_node *root, struct fib6_info *rt,
1427 	     struct nl_info *info, struct netlink_ext_ack *extack)
1428 {
1429 	struct fib6_table *table = rt->fib6_table;
1430 	LIST_HEAD(purge_list);
1431 	struct fib6_node *fn;
1432 #ifdef CONFIG_IPV6_SUBTREES
1433 	struct fib6_node *pn = NULL;
1434 #endif
1435 	int err = -ENOMEM;
1436 	int allow_create = 1;
1437 	int replace_required = 0;
1438 
1439 	if (info->nlh) {
1440 		if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1441 			allow_create = 0;
1442 		if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1443 			replace_required = 1;
1444 	}
1445 	if (!allow_create && !replace_required)
1446 		pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1447 
1448 	fn = fib6_add_1(info->nl_net, table, root,
1449 			&rt->fib6_dst.addr, rt->fib6_dst.plen,
1450 			offsetof(struct fib6_info, fib6_dst), allow_create,
1451 			replace_required, extack);
1452 	if (IS_ERR(fn)) {
1453 		err = PTR_ERR(fn);
1454 		fn = NULL;
1455 		goto out;
1456 	}
1457 
1458 #ifdef CONFIG_IPV6_SUBTREES
1459 	pn = fn;
1460 
1461 	if (rt->fib6_src.plen) {
1462 		struct fib6_node *sn;
1463 
1464 		if (!rcu_access_pointer(fn->subtree)) {
1465 			struct fib6_node *sfn;
1466 
1467 			/*
1468 			 * Create subtree.
1469 			 *
1470 			 *		fn[main tree]
1471 			 *		|
1472 			 *		sfn[subtree root]
1473 			 *		   \
1474 			 *		    sn[new leaf node]
1475 			 */
1476 
1477 			/* Create subtree root node */
1478 			sfn = node_alloc(info->nl_net);
1479 			if (!sfn)
1480 				goto failure;
1481 
1482 			fib6_info_hold(info->nl_net->ipv6.fib6_null_entry);
1483 			rcu_assign_pointer(sfn->leaf,
1484 					   info->nl_net->ipv6.fib6_null_entry);
1485 			sfn->fn_flags = RTN_ROOT;
1486 
1487 			/* Now add the first leaf node to new subtree */
1488 
1489 			sn = fib6_add_1(info->nl_net, table, sfn,
1490 					&rt->fib6_src.addr, rt->fib6_src.plen,
1491 					offsetof(struct fib6_info, fib6_src),
1492 					allow_create, replace_required, extack);
1493 
1494 			if (IS_ERR(sn)) {
1495 				/* If it is failed, discard just allocated
1496 				   root, and then (in failure) stale node
1497 				   in main tree.
1498 				 */
1499 				node_free_immediate(info->nl_net, sfn);
1500 				err = PTR_ERR(sn);
1501 				goto failure;
1502 			}
1503 
1504 			/* Now link new subtree to main tree */
1505 			rcu_assign_pointer(sfn->parent, fn);
1506 			rcu_assign_pointer(fn->subtree, sfn);
1507 		} else {
1508 			sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1509 					&rt->fib6_src.addr, rt->fib6_src.plen,
1510 					offsetof(struct fib6_info, fib6_src),
1511 					allow_create, replace_required, extack);
1512 
1513 			if (IS_ERR(sn)) {
1514 				err = PTR_ERR(sn);
1515 				goto failure;
1516 			}
1517 		}
1518 
1519 		if (!rcu_access_pointer(fn->leaf)) {
1520 			if (fn->fn_flags & RTN_TL_ROOT) {
1521 				/* put back null_entry for root node */
1522 				rcu_assign_pointer(fn->leaf,
1523 					    info->nl_net->ipv6.fib6_null_entry);
1524 			} else {
1525 				fib6_info_hold(rt);
1526 				rcu_assign_pointer(fn->leaf, rt);
1527 			}
1528 		}
1529 		fn = sn;
1530 	}
1531 #endif
1532 
1533 	if (rt->nh)
1534 		err = fib6_add_rt2node_nh(fn, rt, info, extack, &purge_list);
1535 	else
1536 		err = fib6_add_rt2node(fn, rt, info, extack, &purge_list);
1537 	if (!err) {
1538 		struct fib6_info *iter, *next;
1539 
1540 		list_for_each_entry_safe(iter, next, &purge_list, purge_link) {
1541 			list_del(&iter->purge_link);
1542 			fib6_purge_rt(iter, fn, info->nl_net);
1543 			fib6_info_release(iter);
1544 		}
1545 
1546 		__fib6_update_sernum_upto_root(rt, fib6_new_sernum(info->nl_net));
1547 
1548 		if (rt->fib6_flags & RTF_EXPIRES)
1549 			fib6_add_gc_list(rt);
1550 
1551 		fib6_start_gc(info->nl_net, rt);
1552 	}
1553 
1554 out:
1555 	if (err) {
1556 #ifdef CONFIG_IPV6_SUBTREES
1557 		/*
1558 		 * If fib6_add_1 has cleared the old leaf pointer in the
1559 		 * super-tree leaf node we have to find a new one for it.
1560 		 */
1561 		if (pn != fn) {
1562 			struct fib6_info *pn_leaf =
1563 				rcu_dereference_protected(pn->leaf,
1564 				    lockdep_is_held(&table->tb6_lock));
1565 			if (pn_leaf == rt) {
1566 				pn_leaf = NULL;
1567 				RCU_INIT_POINTER(pn->leaf, NULL);
1568 				fib6_info_release(rt);
1569 			}
1570 			if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1571 				pn_leaf = fib6_find_prefix(info->nl_net, table,
1572 							   pn);
1573 				if (!pn_leaf)
1574 					pn_leaf =
1575 					    info->nl_net->ipv6.fib6_null_entry;
1576 				fib6_info_hold(pn_leaf);
1577 				rcu_assign_pointer(pn->leaf, pn_leaf);
1578 			}
1579 		}
1580 #endif
1581 		goto failure;
1582 	} else if (fib6_requires_src(rt)) {
1583 		fib6_routes_require_src_inc(info->nl_net);
1584 	}
1585 	return err;
1586 
1587 failure:
1588 	/* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1589 	 * 1. fn is an intermediate node and we failed to add the new
1590 	 * route to it in both subtree creation failure and fib6_add_rt2node()
1591 	 * failure case.
1592 	 * 2. fn is the root node in the table and we fail to add the first
1593 	 * default route to it.
1594 	 */
1595 	if (fn &&
1596 	    (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1597 	     (fn->fn_flags & RTN_TL_ROOT &&
1598 	      !rcu_access_pointer(fn->leaf))))
1599 		fib6_repair_tree(info->nl_net, table, fn);
1600 	return err;
1601 }
1602 
1603 /*
1604  *	Routing tree lookup
1605  *
1606  */
1607 
1608 struct lookup_args {
1609 	int			offset;		/* key offset on fib6_info */
1610 	const struct in6_addr	*addr;		/* search key			*/
1611 };
1612 
1613 static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
1614 					    struct lookup_args *args)
1615 {
1616 	struct fib6_node *fn;
1617 	__be32 dir;
1618 
1619 	if (unlikely(args->offset == 0))
1620 		return NULL;
1621 
1622 	/*
1623 	 *	Descend on a tree
1624 	 */
1625 
1626 	fn = root;
1627 
1628 	for (;;) {
1629 		struct fib6_node *next;
1630 
1631 		dir = addr_bit_set(args->addr, fn->fn_bit);
1632 
1633 		next = dir ? rcu_dereference(fn->right) :
1634 			     rcu_dereference(fn->left);
1635 
1636 		if (next) {
1637 			fn = next;
1638 			continue;
1639 		}
1640 		break;
1641 	}
1642 
1643 	while (fn) {
1644 		struct fib6_node *subtree = FIB6_SUBTREE(fn);
1645 
1646 		if (subtree || fn->fn_flags & RTN_RTINFO) {
1647 			struct fib6_info *leaf = rcu_dereference(fn->leaf);
1648 			struct rt6key *key;
1649 
1650 			if (!leaf)
1651 				goto backtrack;
1652 
1653 			key = (struct rt6key *) ((u8 *)leaf + args->offset);
1654 
1655 			if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1656 #ifdef CONFIG_IPV6_SUBTREES
1657 				if (subtree) {
1658 					struct fib6_node *sfn;
1659 					sfn = fib6_node_lookup_1(subtree,
1660 								 args + 1);
1661 					if (!sfn)
1662 						goto backtrack;
1663 					fn = sfn;
1664 				}
1665 #endif
1666 				if (fn->fn_flags & RTN_RTINFO)
1667 					return fn;
1668 			}
1669 		}
1670 backtrack:
1671 		if (fn->fn_flags & RTN_ROOT)
1672 			break;
1673 
1674 		fn = rcu_dereference(fn->parent);
1675 	}
1676 
1677 	return NULL;
1678 }
1679 
1680 /* called with rcu_read_lock() held
1681  */
1682 struct fib6_node *fib6_node_lookup(struct fib6_node *root,
1683 				   const struct in6_addr *daddr,
1684 				   const struct in6_addr *saddr)
1685 {
1686 	struct fib6_node *fn;
1687 	struct lookup_args args[] = {
1688 		{
1689 			.offset = offsetof(struct fib6_info, fib6_dst),
1690 			.addr = daddr,
1691 		},
1692 #ifdef CONFIG_IPV6_SUBTREES
1693 		{
1694 			.offset = offsetof(struct fib6_info, fib6_src),
1695 			.addr = saddr,
1696 		},
1697 #endif
1698 		{
1699 			.offset = 0,	/* sentinel */
1700 		}
1701 	};
1702 
1703 	fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
1704 	if (!fn || fn->fn_flags & RTN_TL_ROOT)
1705 		fn = root;
1706 
1707 	return fn;
1708 }
1709 
1710 /*
1711  *	Get node with specified destination prefix (and source prefix,
1712  *	if subtrees are used)
1713  *	exact_match == true means we try to find fn with exact match of
1714  *	the passed in prefix addr
1715  *	exact_match == false means we try to find fn with longest prefix
1716  *	match of the passed in prefix addr. This is useful for finding fn
1717  *	for cached route as it will be stored in the exception table under
1718  *	the node with longest prefix length.
1719  */
1720 
1721 
1722 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1723 				       const struct in6_addr *addr,
1724 				       int plen, int offset,
1725 				       bool exact_match)
1726 {
1727 	struct fib6_node *fn, *prev = NULL;
1728 
1729 	for (fn = root; fn ; ) {
1730 		struct fib6_info *leaf = rcu_dereference(fn->leaf);
1731 		struct rt6key *key;
1732 
1733 		/* This node is being deleted */
1734 		if (!leaf) {
1735 			if (plen <= fn->fn_bit)
1736 				goto out;
1737 			else
1738 				goto next;
1739 		}
1740 
1741 		key = (struct rt6key *)((u8 *)leaf + offset);
1742 
1743 		/*
1744 		 *	Prefix match
1745 		 */
1746 		if (plen < fn->fn_bit ||
1747 		    !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1748 			goto out;
1749 
1750 		if (plen == fn->fn_bit)
1751 			return fn;
1752 
1753 		if (fn->fn_flags & RTN_RTINFO)
1754 			prev = fn;
1755 
1756 next:
1757 		/*
1758 		 *	We have more bits to go
1759 		 */
1760 		if (addr_bit_set(addr, fn->fn_bit))
1761 			fn = rcu_dereference(fn->right);
1762 		else
1763 			fn = rcu_dereference(fn->left);
1764 	}
1765 out:
1766 	if (exact_match)
1767 		return NULL;
1768 	else
1769 		return prev;
1770 }
1771 
1772 struct fib6_node *fib6_locate(struct fib6_node *root,
1773 			      const struct in6_addr *daddr, int dst_len,
1774 			      const struct in6_addr *saddr, int src_len,
1775 			      bool exact_match)
1776 {
1777 	struct fib6_node *fn;
1778 
1779 	fn = fib6_locate_1(root, daddr, dst_len,
1780 			   offsetof(struct fib6_info, fib6_dst),
1781 			   exact_match);
1782 
1783 #ifdef CONFIG_IPV6_SUBTREES
1784 	if (src_len) {
1785 		WARN_ON(saddr == NULL);
1786 		if (fn) {
1787 			struct fib6_node *subtree = FIB6_SUBTREE(fn);
1788 
1789 			if (subtree) {
1790 				fn = fib6_locate_1(subtree, saddr, src_len,
1791 					   offsetof(struct fib6_info, fib6_src),
1792 					   exact_match);
1793 			}
1794 		}
1795 	}
1796 #endif
1797 
1798 	if (fn && fn->fn_flags & RTN_RTINFO)
1799 		return fn;
1800 
1801 	return NULL;
1802 }
1803 
1804 
1805 /*
1806  *	Deletion
1807  *
1808  */
1809 
1810 static struct fib6_info *fib6_find_prefix(struct net *net,
1811 					 struct fib6_table *table,
1812 					 struct fib6_node *fn)
1813 {
1814 	struct fib6_node *child_left, *child_right;
1815 
1816 	if (fn->fn_flags & RTN_ROOT)
1817 		return net->ipv6.fib6_null_entry;
1818 
1819 	while (fn) {
1820 		child_left = rcu_dereference_protected(fn->left,
1821 				    lockdep_is_held(&table->tb6_lock));
1822 		child_right = rcu_dereference_protected(fn->right,
1823 				    lockdep_is_held(&table->tb6_lock));
1824 		if (child_left)
1825 			return rcu_dereference_protected(child_left->leaf,
1826 					lockdep_is_held(&table->tb6_lock));
1827 		if (child_right)
1828 			return rcu_dereference_protected(child_right->leaf,
1829 					lockdep_is_held(&table->tb6_lock));
1830 
1831 		fn = FIB6_SUBTREE(fn);
1832 	}
1833 	return NULL;
1834 }
1835 
1836 /*
1837  *	Called to trim the tree of intermediate nodes when possible. "fn"
1838  *	is the node we want to try and remove.
1839  *	Need to own table->tb6_lock
1840  */
1841 
1842 static struct fib6_node *fib6_repair_tree(struct net *net,
1843 					  struct fib6_table *table,
1844 					  struct fib6_node *fn)
1845 {
1846 	int children;
1847 	int nstate;
1848 	struct fib6_node *child;
1849 	struct fib6_walker *w;
1850 	int iter = 0;
1851 
1852 	/* Set fn->leaf to null_entry for root node. */
1853 	if (fn->fn_flags & RTN_TL_ROOT) {
1854 		rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
1855 		return fn;
1856 	}
1857 
1858 	for (;;) {
1859 		struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1860 					    lockdep_is_held(&table->tb6_lock));
1861 		struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1862 					    lockdep_is_held(&table->tb6_lock));
1863 		struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1864 					    lockdep_is_held(&table->tb6_lock));
1865 		struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1866 					    lockdep_is_held(&table->tb6_lock));
1867 		struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1868 					    lockdep_is_held(&table->tb6_lock));
1869 		struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1870 					    lockdep_is_held(&table->tb6_lock));
1871 		struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1872 					    lockdep_is_held(&table->tb6_lock));
1873 		struct fib6_info *new_fn_leaf;
1874 
1875 		pr_debug("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1876 		iter++;
1877 
1878 		WARN_ON(fn->fn_flags & RTN_RTINFO);
1879 		WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1880 		WARN_ON(fn_leaf);
1881 
1882 		children = 0;
1883 		child = NULL;
1884 		if (fn_r) {
1885 			child = fn_r;
1886 			children |= 1;
1887 		}
1888 		if (fn_l) {
1889 			child = fn_l;
1890 			children |= 2;
1891 		}
1892 
1893 		if (children == 3 || FIB6_SUBTREE(fn)
1894 #ifdef CONFIG_IPV6_SUBTREES
1895 		    /* Subtree root (i.e. fn) may have one child */
1896 		    || (children && fn->fn_flags & RTN_ROOT)
1897 #endif
1898 		    ) {
1899 			new_fn_leaf = fib6_find_prefix(net, table, fn);
1900 #if RT6_DEBUG >= 2
1901 			if (!new_fn_leaf) {
1902 				WARN_ON(!new_fn_leaf);
1903 				new_fn_leaf = net->ipv6.fib6_null_entry;
1904 			}
1905 #endif
1906 			fib6_info_hold(new_fn_leaf);
1907 			rcu_assign_pointer(fn->leaf, new_fn_leaf);
1908 			return pn;
1909 		}
1910 
1911 #ifdef CONFIG_IPV6_SUBTREES
1912 		if (FIB6_SUBTREE(pn) == fn) {
1913 			WARN_ON(!(fn->fn_flags & RTN_ROOT));
1914 			RCU_INIT_POINTER(pn->subtree, NULL);
1915 			nstate = FWS_L;
1916 		} else {
1917 			WARN_ON(fn->fn_flags & RTN_ROOT);
1918 #endif
1919 			if (pn_r == fn)
1920 				rcu_assign_pointer(pn->right, child);
1921 			else if (pn_l == fn)
1922 				rcu_assign_pointer(pn->left, child);
1923 #if RT6_DEBUG >= 2
1924 			else
1925 				WARN_ON(1);
1926 #endif
1927 			if (child)
1928 				rcu_assign_pointer(child->parent, pn);
1929 			nstate = FWS_R;
1930 #ifdef CONFIG_IPV6_SUBTREES
1931 		}
1932 #endif
1933 
1934 		read_lock(&net->ipv6.fib6_walker_lock);
1935 		FOR_WALKERS(net, w) {
1936 			if (!child) {
1937 				if (w->node == fn) {
1938 					pr_debug("W %p adjusted by delnode 1, s=%d/%d\n",
1939 						 w, w->state, nstate);
1940 					w->node = pn;
1941 					w->state = nstate;
1942 				}
1943 			} else {
1944 				if (w->node == fn) {
1945 					w->node = child;
1946 					if (children&2) {
1947 						pr_debug("W %p adjusted by delnode 2, s=%d\n",
1948 							 w, w->state);
1949 						w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1950 					} else {
1951 						pr_debug("W %p adjusted by delnode 2, s=%d\n",
1952 							 w, w->state);
1953 						w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1954 					}
1955 				}
1956 			}
1957 		}
1958 		read_unlock(&net->ipv6.fib6_walker_lock);
1959 
1960 		node_free(net, fn);
1961 		if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1962 			return pn;
1963 
1964 		RCU_INIT_POINTER(pn->leaf, NULL);
1965 		fib6_info_release(pn_leaf);
1966 		fn = pn;
1967 	}
1968 }
1969 
1970 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1971 			   struct fib6_info __rcu **rtp, struct nl_info *info)
1972 {
1973 	struct fib6_info *leaf, *replace_rt = NULL;
1974 	struct fib6_walker *w;
1975 	struct fib6_info *rt = rcu_dereference_protected(*rtp,
1976 				    lockdep_is_held(&table->tb6_lock));
1977 	struct net *net = info->nl_net;
1978 	bool notify_del = false;
1979 
1980 	/* If the deleted route is the first in the node and it is not part of
1981 	 * a multipath route, then we need to replace it with the next route
1982 	 * in the node, if exists.
1983 	 */
1984 	leaf = rcu_dereference_protected(fn->leaf,
1985 					 lockdep_is_held(&table->tb6_lock));
1986 	if (leaf == rt && !rt->fib6_nsiblings) {
1987 		if (rcu_access_pointer(rt->fib6_next))
1988 			replace_rt = rcu_dereference_protected(rt->fib6_next,
1989 					    lockdep_is_held(&table->tb6_lock));
1990 		else
1991 			notify_del = true;
1992 	}
1993 
1994 	/* Unlink it */
1995 	*rtp = rt->fib6_next;
1996 	rt->fib6_node = NULL;
1997 	net->ipv6.rt6_stats->fib_rt_entries--;
1998 	net->ipv6.rt6_stats->fib_discarded_routes++;
1999 
2000 	/* Reset round-robin state, if necessary */
2001 	if (rcu_access_pointer(fn->rr_ptr) == rt)
2002 		fn->rr_ptr = NULL;
2003 
2004 	/* Remove this entry from other siblings */
2005 	if (rt->fib6_nsiblings) {
2006 		struct fib6_info *sibling, *next_sibling;
2007 
2008 		/* The route is deleted from a multipath route. If this
2009 		 * multipath route is the first route in the node, then we need
2010 		 * to emit a delete notification. Otherwise, we need to skip
2011 		 * the notification.
2012 		 */
2013 		if (rt->fib6_metric == leaf->fib6_metric &&
2014 		    rt6_qualify_for_ecmp(leaf))
2015 			notify_del = true;
2016 		list_for_each_entry_safe(sibling, next_sibling,
2017 					 &rt->fib6_siblings, fib6_siblings)
2018 			WRITE_ONCE(sibling->fib6_nsiblings,
2019 				   sibling->fib6_nsiblings - 1);
2020 		WRITE_ONCE(rt->fib6_nsiblings, 0);
2021 		list_del_rcu(&rt->fib6_siblings);
2022 		rt6_multipath_rebalance(next_sibling);
2023 	}
2024 
2025 	/* Adjust walkers */
2026 	read_lock(&net->ipv6.fib6_walker_lock);
2027 	FOR_WALKERS(net, w) {
2028 		if (w->state == FWS_C && w->leaf == rt) {
2029 			pr_debug("walker %p adjusted by delroute\n", w);
2030 			w->leaf = rcu_dereference_protected(rt->fib6_next,
2031 					    lockdep_is_held(&table->tb6_lock));
2032 			if (!w->leaf)
2033 				w->state = FWS_U;
2034 		}
2035 	}
2036 	read_unlock(&net->ipv6.fib6_walker_lock);
2037 
2038 	/* If it was last route, call fib6_repair_tree() to:
2039 	 * 1. For root node, put back null_entry as how the table was created.
2040 	 * 2. For other nodes, expunge its radix tree node.
2041 	 */
2042 	if (!rcu_access_pointer(fn->leaf)) {
2043 		if (!(fn->fn_flags & RTN_TL_ROOT)) {
2044 			fn->fn_flags &= ~RTN_RTINFO;
2045 			net->ipv6.rt6_stats->fib_route_nodes--;
2046 		}
2047 		fn = fib6_repair_tree(net, table, fn);
2048 	}
2049 
2050 	fib6_purge_rt(rt, fn, net);
2051 
2052 	if (!info->skip_notify_kernel) {
2053 		if (notify_del)
2054 			call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL,
2055 						  rt, NULL);
2056 		else if (replace_rt)
2057 			call_fib6_entry_notifiers_replace(net, replace_rt);
2058 	}
2059 	if (!info->skip_notify)
2060 		inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
2061 
2062 	fib6_info_release(rt);
2063 }
2064 
2065 /* Need to own table->tb6_lock */
2066 int fib6_del(struct fib6_info *rt, struct nl_info *info)
2067 {
2068 	struct net *net = info->nl_net;
2069 	struct fib6_info __rcu **rtp;
2070 	struct fib6_info __rcu **rtp_next;
2071 	struct fib6_table *table;
2072 	struct fib6_node *fn;
2073 
2074 	if (rt == net->ipv6.fib6_null_entry)
2075 		return -ENOENT;
2076 
2077 	table = rt->fib6_table;
2078 	fn = rcu_dereference_protected(rt->fib6_node,
2079 				       lockdep_is_held(&table->tb6_lock));
2080 	if (!fn)
2081 		return -ENOENT;
2082 
2083 	WARN_ON(!(fn->fn_flags & RTN_RTINFO));
2084 
2085 	/*
2086 	 *	Walk the leaf entries looking for ourself
2087 	 */
2088 
2089 	for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
2090 		struct fib6_info *cur = rcu_dereference_protected(*rtp,
2091 					lockdep_is_held(&table->tb6_lock));
2092 		if (rt == cur) {
2093 			if (fib6_requires_src(cur))
2094 				fib6_routes_require_src_dec(info->nl_net);
2095 			fib6_del_route(table, fn, rtp, info);
2096 			return 0;
2097 		}
2098 		rtp_next = &cur->fib6_next;
2099 	}
2100 	return -ENOENT;
2101 }
2102 
2103 /*
2104  *	Tree traversal function.
2105  *
2106  *	Certainly, it is not interrupt safe.
2107  *	However, it is internally reenterable wrt itself and fib6_add/fib6_del.
2108  *	It means, that we can modify tree during walking
2109  *	and use this function for garbage collection, clone pruning,
2110  *	cleaning tree when a device goes down etc. etc.
2111  *
2112  *	It guarantees that every node will be traversed,
2113  *	and that it will be traversed only once.
2114  *
2115  *	Callback function w->func may return:
2116  *	0 -> continue walking.
2117  *	positive value -> walking is suspended (used by tree dumps,
2118  *	and probably by gc, if it will be split to several slices)
2119  *	negative value -> terminate walking.
2120  *
2121  *	The function itself returns:
2122  *	0   -> walk is complete.
2123  *	>0  -> walk is incomplete (i.e. suspended)
2124  *	<0  -> walk is terminated by an error.
2125  *
2126  *	This function is called with tb6_lock held.
2127  */
2128 
2129 static int fib6_walk_continue(struct fib6_walker *w)
2130 {
2131 	struct fib6_node *fn, *pn, *left, *right;
2132 
2133 	/* w->root should always be table->tb6_root */
2134 	WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
2135 
2136 	for (;;) {
2137 		fn = w->node;
2138 		if (!fn)
2139 			return 0;
2140 
2141 		switch (w->state) {
2142 #ifdef CONFIG_IPV6_SUBTREES
2143 		case FWS_S:
2144 			if (FIB6_SUBTREE(fn)) {
2145 				w->node = FIB6_SUBTREE(fn);
2146 				continue;
2147 			}
2148 			w->state = FWS_L;
2149 			fallthrough;
2150 #endif
2151 		case FWS_L:
2152 			left = rcu_dereference_protected(fn->left, 1);
2153 			if (left) {
2154 				w->node = left;
2155 				w->state = FWS_INIT;
2156 				continue;
2157 			}
2158 			w->state = FWS_R;
2159 			fallthrough;
2160 		case FWS_R:
2161 			right = rcu_dereference_protected(fn->right, 1);
2162 			if (right) {
2163 				w->node = right;
2164 				w->state = FWS_INIT;
2165 				continue;
2166 			}
2167 			w->state = FWS_C;
2168 			w->leaf = rcu_dereference_protected(fn->leaf, 1);
2169 			fallthrough;
2170 		case FWS_C:
2171 			if (w->leaf && fn->fn_flags & RTN_RTINFO) {
2172 				int err;
2173 
2174 				if (w->skip) {
2175 					w->skip--;
2176 					goto skip;
2177 				}
2178 
2179 				err = w->func(w);
2180 				if (err)
2181 					return err;
2182 
2183 				w->count++;
2184 				continue;
2185 			}
2186 skip:
2187 			w->state = FWS_U;
2188 			fallthrough;
2189 		case FWS_U:
2190 			if (fn == w->root)
2191 				return 0;
2192 			pn = rcu_dereference_protected(fn->parent, 1);
2193 			left = rcu_dereference_protected(pn->left, 1);
2194 			right = rcu_dereference_protected(pn->right, 1);
2195 			w->node = pn;
2196 #ifdef CONFIG_IPV6_SUBTREES
2197 			if (FIB6_SUBTREE(pn) == fn) {
2198 				WARN_ON(!(fn->fn_flags & RTN_ROOT));
2199 				w->state = FWS_L;
2200 				continue;
2201 			}
2202 #endif
2203 			if (left == fn) {
2204 				w->state = FWS_R;
2205 				continue;
2206 			}
2207 			if (right == fn) {
2208 				w->state = FWS_C;
2209 				w->leaf = rcu_dereference_protected(w->node->leaf, 1);
2210 				continue;
2211 			}
2212 #if RT6_DEBUG >= 2
2213 			WARN_ON(1);
2214 #endif
2215 		}
2216 	}
2217 }
2218 
2219 static int fib6_walk(struct net *net, struct fib6_walker *w)
2220 {
2221 	int res;
2222 
2223 	w->state = FWS_INIT;
2224 	w->node = w->root;
2225 
2226 	fib6_walker_link(net, w);
2227 	res = fib6_walk_continue(w);
2228 	if (res <= 0)
2229 		fib6_walker_unlink(net, w);
2230 	return res;
2231 }
2232 
2233 static int fib6_clean_node(struct fib6_walker *w)
2234 {
2235 	int res;
2236 	struct fib6_info *rt;
2237 	struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
2238 	struct nl_info info = {
2239 		.nl_net = c->net,
2240 		.skip_notify = c->skip_notify,
2241 	};
2242 
2243 	if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
2244 	    READ_ONCE(w->node->fn_sernum) != c->sernum)
2245 		WRITE_ONCE(w->node->fn_sernum, c->sernum);
2246 
2247 	if (!c->func) {
2248 		WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
2249 		w->leaf = NULL;
2250 		return 0;
2251 	}
2252 
2253 	for_each_fib6_walker_rt(w) {
2254 		res = c->func(rt, c->arg);
2255 		if (res == -1) {
2256 			w->leaf = rt;
2257 			res = fib6_del(rt, &info);
2258 			if (res) {
2259 #if RT6_DEBUG >= 2
2260 				pr_debug("%s: del failed: rt=%p@%p err=%d\n",
2261 					 __func__, rt,
2262 					 rcu_access_pointer(rt->fib6_node),
2263 					 res);
2264 #endif
2265 				continue;
2266 			}
2267 			return 0;
2268 		} else if (res == -2) {
2269 			if (WARN_ON(!rt->fib6_nsiblings))
2270 				continue;
2271 			rt = list_last_entry(&rt->fib6_siblings,
2272 					     struct fib6_info, fib6_siblings);
2273 			continue;
2274 		}
2275 		WARN_ON(res != 0);
2276 	}
2277 	w->leaf = rt;
2278 	return 0;
2279 }
2280 
2281 /*
2282  *	Convenient frontend to tree walker.
2283  *
2284  *	func is called on each route.
2285  *		It may return -2 -> skip multipath route.
2286  *			      -1 -> delete this route.
2287  *		              0  -> continue walking
2288  */
2289 
2290 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
2291 			    int (*func)(struct fib6_info *, void *arg),
2292 			    int sernum, void *arg, bool skip_notify)
2293 {
2294 	struct fib6_cleaner c;
2295 
2296 	c.w.root = root;
2297 	c.w.func = fib6_clean_node;
2298 	c.w.count = 0;
2299 	c.w.skip = 0;
2300 	c.w.skip_in_node = 0;
2301 	c.func = func;
2302 	c.sernum = sernum;
2303 	c.arg = arg;
2304 	c.net = net;
2305 	c.skip_notify = skip_notify;
2306 
2307 	fib6_walk(net, &c.w);
2308 }
2309 
2310 static void __fib6_clean_all(struct net *net,
2311 			     int (*func)(struct fib6_info *, void *),
2312 			     int sernum, void *arg, bool skip_notify)
2313 {
2314 	struct fib6_table *table;
2315 	struct hlist_head *head;
2316 	unsigned int h;
2317 
2318 	rcu_read_lock();
2319 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2320 		head = &net->ipv6.fib_table_hash[h];
2321 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2322 			spin_lock_bh(&table->tb6_lock);
2323 			fib6_clean_tree(net, &table->tb6_root,
2324 					func, sernum, arg, skip_notify);
2325 			spin_unlock_bh(&table->tb6_lock);
2326 		}
2327 	}
2328 	rcu_read_unlock();
2329 }
2330 
2331 void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
2332 		    void *arg)
2333 {
2334 	__fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, false);
2335 }
2336 
2337 void fib6_clean_all_skip_notify(struct net *net,
2338 				int (*func)(struct fib6_info *, void *),
2339 				void *arg)
2340 {
2341 	__fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg, true);
2342 }
2343 
2344 static void fib6_flush_trees(struct net *net)
2345 {
2346 	int new_sernum = fib6_new_sernum(net);
2347 
2348 	__fib6_clean_all(net, NULL, new_sernum, NULL, false);
2349 }
2350 
2351 /*
2352  *	Garbage collection
2353  */
2354 
2355 static int fib6_age(struct fib6_info *rt, struct fib6_gc_args *gc_args)
2356 {
2357 	unsigned long now = jiffies;
2358 
2359 	/*
2360 	 *	check addrconf expiration here.
2361 	 *	Routes are expired even if they are in use.
2362 	 */
2363 
2364 	if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
2365 		if (time_after(now, rt->expires)) {
2366 			pr_debug("expiring %p\n", rt);
2367 			return -1;
2368 		}
2369 		gc_args->more++;
2370 	}
2371 
2372 	/*	Also age clones in the exception table.
2373 	 *	Note, that clones are aged out
2374 	 *	only if they are not in use now.
2375 	 */
2376 	rt6_age_exceptions(rt, gc_args, now);
2377 
2378 	return 0;
2379 }
2380 
2381 static void fib6_gc_table(struct net *net,
2382 			  struct fib6_table *tb6,
2383 			  struct fib6_gc_args *gc_args)
2384 {
2385 	struct fib6_info *rt;
2386 	struct hlist_node *n;
2387 	struct nl_info info = {
2388 		.nl_net = net,
2389 		.skip_notify = false,
2390 	};
2391 
2392 	hlist_for_each_entry_safe(rt, n, &tb6->tb6_gc_hlist, gc_link)
2393 		if (fib6_age(rt, gc_args) == -1)
2394 			fib6_del(rt, &info);
2395 }
2396 
2397 static void fib6_gc_all(struct net *net, struct fib6_gc_args *gc_args)
2398 {
2399 	struct fib6_table *table;
2400 	struct hlist_head *head;
2401 	unsigned int h;
2402 
2403 	rcu_read_lock();
2404 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
2405 		head = &net->ipv6.fib_table_hash[h];
2406 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
2407 			spin_lock_bh(&table->tb6_lock);
2408 
2409 			fib6_gc_table(net, table, gc_args);
2410 
2411 			spin_unlock_bh(&table->tb6_lock);
2412 		}
2413 	}
2414 	rcu_read_unlock();
2415 }
2416 
2417 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2418 {
2419 	struct fib6_gc_args gc_args;
2420 	int ip6_rt_gc_interval;
2421 	unsigned long now;
2422 
2423 	if (force) {
2424 		spin_lock_bh(&net->ipv6.fib6_gc_lock);
2425 	} else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2426 		mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2427 		return;
2428 	}
2429 	ip6_rt_gc_interval = READ_ONCE(net->ipv6.sysctl.ip6_rt_gc_interval);
2430 	gc_args.timeout = expires ? (int)expires : ip6_rt_gc_interval;
2431 	gc_args.more = 0;
2432 
2433 	fib6_gc_all(net, &gc_args);
2434 	now = jiffies;
2435 	net->ipv6.ip6_rt_last_gc = now;
2436 
2437 	if (gc_args.more)
2438 		mod_timer(&net->ipv6.ip6_fib_timer,
2439 			  round_jiffies(now + ip6_rt_gc_interval));
2440 	else
2441 		timer_delete(&net->ipv6.ip6_fib_timer);
2442 	spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2443 }
2444 
2445 static void fib6_gc_timer_cb(struct timer_list *t)
2446 {
2447 	struct net *arg = timer_container_of(arg, t, ipv6.ip6_fib_timer);
2448 
2449 	fib6_run_gc(0, arg, true);
2450 }
2451 
2452 static int __net_init fib6_net_init(struct net *net)
2453 {
2454 	size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2455 	int err;
2456 
2457 	err = fib6_notifier_init(net);
2458 	if (err)
2459 		return err;
2460 
2461 	/* Default to 3-tuple */
2462 	net->ipv6.sysctl.multipath_hash_fields =
2463 		FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK;
2464 
2465 	spin_lock_init(&net->ipv6.fib6_gc_lock);
2466 	rwlock_init(&net->ipv6.fib6_walker_lock);
2467 	INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2468 	timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2469 
2470 	net->ipv6.rt6_stats = kzalloc_obj(*net->ipv6.rt6_stats);
2471 	if (!net->ipv6.rt6_stats)
2472 		goto out_notifier;
2473 
2474 	/* Avoid false sharing : Use at least a full cache line */
2475 	size = max_t(size_t, size, L1_CACHE_BYTES);
2476 
2477 	net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2478 	if (!net->ipv6.fib_table_hash)
2479 		goto out_rt6_stats;
2480 
2481 	spin_lock_init(&net->ipv6.fib_table_hash_lock);
2482 
2483 	net->ipv6.fib6_main_tbl = kzalloc_obj(*net->ipv6.fib6_main_tbl);
2484 	if (!net->ipv6.fib6_main_tbl)
2485 		goto out_fib_table_hash;
2486 
2487 	net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2488 	rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2489 			   net->ipv6.fib6_null_entry);
2490 	net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2491 		RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2492 	inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2493 	INIT_HLIST_HEAD(&net->ipv6.fib6_main_tbl->tb6_gc_hlist);
2494 
2495 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2496 	net->ipv6.fib6_local_tbl = kzalloc_obj(*net->ipv6.fib6_local_tbl);
2497 	if (!net->ipv6.fib6_local_tbl)
2498 		goto out_fib6_main_tbl;
2499 	net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2500 	rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2501 			   net->ipv6.fib6_null_entry);
2502 	net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2503 		RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2504 	inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2505 	INIT_HLIST_HEAD(&net->ipv6.fib6_local_tbl->tb6_gc_hlist);
2506 #endif
2507 	fib6_tables_init(net);
2508 
2509 	return 0;
2510 
2511 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2512 out_fib6_main_tbl:
2513 	kfree(net->ipv6.fib6_main_tbl);
2514 #endif
2515 out_fib_table_hash:
2516 	kfree(net->ipv6.fib_table_hash);
2517 out_rt6_stats:
2518 	kfree(net->ipv6.rt6_stats);
2519 out_notifier:
2520 	fib6_notifier_exit(net);
2521 	return -ENOMEM;
2522 }
2523 
2524 static void fib6_net_exit(struct net *net)
2525 {
2526 	unsigned int i;
2527 
2528 	timer_delete_sync(&net->ipv6.ip6_fib_timer);
2529 
2530 	for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2531 		struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2532 		struct hlist_node *tmp;
2533 		struct fib6_table *tb;
2534 
2535 		hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2536 			hlist_del(&tb->tb6_hlist);
2537 			fib6_free_table(tb);
2538 		}
2539 	}
2540 
2541 	kfree(net->ipv6.fib_table_hash);
2542 	kfree(net->ipv6.rt6_stats);
2543 	fib6_notifier_exit(net);
2544 }
2545 
2546 static struct pernet_operations fib6_net_ops = {
2547 	.init = fib6_net_init,
2548 	.exit = fib6_net_exit,
2549 };
2550 
2551 static const struct rtnl_msg_handler fib6_rtnl_msg_handlers[] __initconst_or_module = {
2552 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETROUTE,
2553 	 .dumpit = inet6_dump_fib,
2554 	 .flags = RTNL_FLAG_DUMP_UNLOCKED | RTNL_FLAG_DUMP_SPLIT_NLM_DONE},
2555 };
2556 
2557 int __init fib6_init(void)
2558 {
2559 	int ret = -ENOMEM;
2560 
2561 	fib6_node_kmem = KMEM_CACHE(fib6_node,
2562 				    SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT);
2563 	if (!fib6_node_kmem)
2564 		goto out;
2565 
2566 	ret = register_pernet_subsys(&fib6_net_ops);
2567 	if (ret)
2568 		goto out_kmem_cache_create;
2569 
2570 	ret = rtnl_register_many(fib6_rtnl_msg_handlers);
2571 	if (ret)
2572 		goto out_unregister_subsys;
2573 
2574 	__fib6_flush_trees = fib6_flush_trees;
2575 out:
2576 	return ret;
2577 
2578 out_unregister_subsys:
2579 	unregister_pernet_subsys(&fib6_net_ops);
2580 out_kmem_cache_create:
2581 	kmem_cache_destroy(fib6_node_kmem);
2582 	goto out;
2583 }
2584 
2585 void fib6_gc_cleanup(void)
2586 {
2587 	unregister_pernet_subsys(&fib6_net_ops);
2588 	kmem_cache_destroy(fib6_node_kmem);
2589 }
2590 
2591 #ifdef CONFIG_PROC_FS
2592 static int ipv6_route_native_seq_show(struct seq_file *seq, void *v)
2593 {
2594 	struct fib6_info *rt = v;
2595 	struct ipv6_route_iter *iter = seq->private;
2596 	struct fib6_nh *fib6_nh = rt->fib6_nh;
2597 	unsigned int flags = rt->fib6_flags;
2598 	const struct net_device *dev;
2599 
2600 	if (rt->nh)
2601 		fib6_nh = nexthop_fib6_nh(rt->nh);
2602 
2603 	seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
2604 
2605 #ifdef CONFIG_IPV6_SUBTREES
2606 	seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
2607 #else
2608 	seq_puts(seq, "00000000000000000000000000000000 00 ");
2609 #endif
2610 	if (fib6_nh->fib_nh_gw_family) {
2611 		flags |= RTF_GATEWAY;
2612 		seq_printf(seq, "%pi6", &fib6_nh->fib_nh_gw6);
2613 	} else {
2614 		seq_puts(seq, "00000000000000000000000000000000");
2615 	}
2616 
2617 	dev = fib6_nh->fib_nh_dev;
2618 	seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2619 		   rt->fib6_metric, refcount_read(&rt->fib6_ref), 0,
2620 		   flags, dev ? dev->name : "");
2621 	iter->w.leaf = NULL;
2622 	return 0;
2623 }
2624 
2625 static int ipv6_route_yield(struct fib6_walker *w)
2626 {
2627 	struct ipv6_route_iter *iter = w->args;
2628 
2629 	if (!iter->skip)
2630 		return 1;
2631 
2632 	do {
2633 		iter->w.leaf = rcu_dereference_protected(
2634 				iter->w.leaf->fib6_next,
2635 				lockdep_is_held(&iter->tbl->tb6_lock));
2636 		iter->skip--;
2637 		if (!iter->skip && iter->w.leaf)
2638 			return 1;
2639 	} while (iter->w.leaf);
2640 
2641 	return 0;
2642 }
2643 
2644 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2645 				      struct net *net)
2646 {
2647 	memset(&iter->w, 0, sizeof(iter->w));
2648 	iter->w.func = ipv6_route_yield;
2649 	iter->w.root = &iter->tbl->tb6_root;
2650 	iter->w.state = FWS_INIT;
2651 	iter->w.node = iter->w.root;
2652 	iter->w.args = iter;
2653 	iter->sernum = READ_ONCE(iter->w.root->fn_sernum);
2654 	INIT_LIST_HEAD(&iter->w.lh);
2655 	fib6_walker_link(net, &iter->w);
2656 }
2657 
2658 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2659 						    struct net *net)
2660 {
2661 	unsigned int h;
2662 	struct hlist_node *node;
2663 
2664 	if (tbl) {
2665 		h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2666 		node = rcu_dereference(hlist_next_rcu(&tbl->tb6_hlist));
2667 	} else {
2668 		h = 0;
2669 		node = NULL;
2670 	}
2671 
2672 	while (!node && h < FIB6_TABLE_HASHSZ) {
2673 		node = rcu_dereference(
2674 			hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2675 	}
2676 	return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2677 }
2678 
2679 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2680 {
2681 	int sernum = READ_ONCE(iter->w.root->fn_sernum);
2682 
2683 	if (iter->sernum != sernum) {
2684 		iter->sernum = sernum;
2685 		iter->w.state = FWS_INIT;
2686 		iter->w.node = iter->w.root;
2687 		WARN_ON(iter->w.skip);
2688 		iter->w.skip = iter->w.count;
2689 	}
2690 }
2691 
2692 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2693 {
2694 	int r;
2695 	struct fib6_info *n;
2696 	struct net *net = seq_file_net(seq);
2697 	struct ipv6_route_iter *iter = seq->private;
2698 
2699 	++(*pos);
2700 	if (!v)
2701 		goto iter_table;
2702 
2703 	n = rcu_dereference(((struct fib6_info *)v)->fib6_next);
2704 	if (n)
2705 		return n;
2706 
2707 iter_table:
2708 	ipv6_route_check_sernum(iter);
2709 	spin_lock_bh(&iter->tbl->tb6_lock);
2710 	r = fib6_walk_continue(&iter->w);
2711 	spin_unlock_bh(&iter->tbl->tb6_lock);
2712 	if (r > 0) {
2713 		return iter->w.leaf;
2714 	} else if (r < 0) {
2715 		fib6_walker_unlink(net, &iter->w);
2716 		return NULL;
2717 	}
2718 	fib6_walker_unlink(net, &iter->w);
2719 
2720 	iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2721 	if (!iter->tbl)
2722 		return NULL;
2723 
2724 	ipv6_route_seq_setup_walk(iter, net);
2725 	goto iter_table;
2726 }
2727 
2728 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2729 	__acquires(RCU)
2730 {
2731 	struct net *net = seq_file_net(seq);
2732 	struct ipv6_route_iter *iter = seq->private;
2733 
2734 	rcu_read_lock();
2735 	iter->tbl = ipv6_route_seq_next_table(NULL, net);
2736 	iter->skip = *pos;
2737 
2738 	if (iter->tbl) {
2739 		loff_t p = 0;
2740 
2741 		ipv6_route_seq_setup_walk(iter, net);
2742 		return ipv6_route_seq_next(seq, NULL, &p);
2743 	} else {
2744 		return NULL;
2745 	}
2746 }
2747 
2748 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2749 {
2750 	struct fib6_walker *w = &iter->w;
2751 	return w->node && !(w->state == FWS_U && w->node == w->root);
2752 }
2753 
2754 static void ipv6_route_native_seq_stop(struct seq_file *seq, void *v)
2755 	__releases(RCU)
2756 {
2757 	struct net *net = seq_file_net(seq);
2758 	struct ipv6_route_iter *iter = seq->private;
2759 
2760 	if (ipv6_route_iter_active(iter))
2761 		fib6_walker_unlink(net, &iter->w);
2762 
2763 	rcu_read_unlock();
2764 }
2765 
2766 #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
2767 static int ipv6_route_prog_seq_show(struct bpf_prog *prog,
2768 				    struct bpf_iter_meta *meta,
2769 				    void *v)
2770 {
2771 	struct bpf_iter__ipv6_route ctx;
2772 
2773 	ctx.meta = meta;
2774 	ctx.rt = v;
2775 	return bpf_iter_run_prog(prog, &ctx);
2776 }
2777 
2778 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2779 {
2780 	struct ipv6_route_iter *iter = seq->private;
2781 	struct bpf_iter_meta meta;
2782 	struct bpf_prog *prog;
2783 	int ret;
2784 
2785 	meta.seq = seq;
2786 	prog = bpf_iter_get_info(&meta, false);
2787 	if (!prog)
2788 		return ipv6_route_native_seq_show(seq, v);
2789 
2790 	ret = ipv6_route_prog_seq_show(prog, &meta, v);
2791 	iter->w.leaf = NULL;
2792 
2793 	return ret;
2794 }
2795 
2796 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2797 {
2798 	struct bpf_iter_meta meta;
2799 	struct bpf_prog *prog;
2800 
2801 	if (!v) {
2802 		meta.seq = seq;
2803 		prog = bpf_iter_get_info(&meta, true);
2804 		if (prog)
2805 			(void)ipv6_route_prog_seq_show(prog, &meta, v);
2806 	}
2807 
2808 	ipv6_route_native_seq_stop(seq, v);
2809 }
2810 #else
2811 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2812 {
2813 	return ipv6_route_native_seq_show(seq, v);
2814 }
2815 
2816 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2817 {
2818 	ipv6_route_native_seq_stop(seq, v);
2819 }
2820 #endif
2821 
2822 const struct seq_operations ipv6_route_seq_ops = {
2823 	.start	= ipv6_route_seq_start,
2824 	.next	= ipv6_route_seq_next,
2825 	.stop	= ipv6_route_seq_stop,
2826 	.show	= ipv6_route_seq_show
2827 };
2828 #endif /* CONFIG_PROC_FS */
2829