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