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