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