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