xref: /xnu-8796.121.2/bsd/netinet6/in6_rmx.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1 /*
2  * Copyright (c) 2003-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*
30  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. Neither the name of the project nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 /*
59  * Copyright 1994, 1995 Massachusetts Institute of Technology
60  *
61  * Permission to use, copy, modify, and distribute this software and
62  * its documentation for any purpose and without fee is hereby
63  * granted, provided that both the above copyright notice and this
64  * permission notice appear in all copies, that both the above
65  * copyright notice and this permission notice appear in all
66  * supporting documentation, and that the name of M.I.T. not be used
67  * in advertising or publicity pertaining to distribution of the
68  * software without specific, written prior permission.  M.I.T. makes
69  * no representations about the suitability of this software for any
70  * purpose.  It is provided "as is" without express or implied
71  * warranty.
72  *
73  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
74  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
75  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
76  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
77  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
78  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
79  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
80  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
81  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
82  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
83  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84  * SUCH DAMAGE.
85  *
86  */
87 
88 /*
89  * This code does two things necessary for the enhanced TCP metrics to
90  * function in a useful manner:
91  *  1) It marks all non-host routes as `cloning', thus ensuring that
92  *     every actual reference to such a route actually gets turned
93  *     into a reference to a host route to the specific destination
94  *     requested.
95  *  2) When such routes lose all their references, it arranges for them
96  *     to be deleted in some random collection of circumstances, so that
97  *     a large quantity of stale routing data is not kept in kernel memory
98  *     indefinitely.  See in6_rtqtimo() below for the exact mechanism.
99  */
100 
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/kernel.h>
104 #include <sys/sysctl.h>
105 #include <kern/queue.h>
106 #include <sys/socket.h>
107 #include <sys/socketvar.h>
108 #include <sys/protosw.h>
109 #include <sys/mbuf.h>
110 #include <sys/syslog.h>
111 #include <sys/mcache.h>
112 #include <kern/locks.h>
113 
114 #include <net/if.h>
115 #include <net/route.h>
116 #include <netinet/in.h>
117 #include <netinet/ip_var.h>
118 #include <netinet/in_var.h>
119 
120 #include <netinet/ip6.h>
121 #include <netinet6/ip6_var.h>
122 
123 #include <netinet/icmp6.h>
124 
125 #include <netinet/tcp.h>
126 #include <netinet/tcp_seq.h>
127 #include <netinet/tcp_timer.h>
128 #include <netinet/tcp_var.h>
129 
130 extern int      tvtohz(struct timeval *);
131 
132 static int in6_rtqtimo_run;             /* in6_rtqtimo is scheduled to run */
133 static void in6_rtqtimo(void *);
134 static void in6_sched_rtqtimo(struct timeval *);
135 
136 static struct radix_node *in6_addroute(void *, void *, struct radix_node_head *,
137     struct radix_node *);
138 static struct radix_node *in6_deleteroute(void *, void *,
139     struct radix_node_head *);
140 static struct radix_node *in6_matroute(void *, struct radix_node_head *);
141 static struct radix_node *in6_matroute_args(void *, struct radix_node_head *,
142     rn_matchf_t *, void *);
143 static void in6_clsroute(struct radix_node *, struct radix_node_head *);
144 static int in6_rtqkill(struct radix_node *, void *);
145 
146 /*
147  * Accessed by in6_addroute(), in6_deleteroute() and in6_rtqkill(), during
148  * which the routing lock (rnh_lock) is held and thus protects the variable.
149  */
150 static int in6dynroutes;
151 
152 /*
153  * Do what we need to do when inserting a route.
154  */
155 static struct radix_node *
in6_addroute(void * v_arg,void * n_arg,struct radix_node_head * head,struct radix_node * treenodes)156 in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
157     struct radix_node *treenodes)
158 {
159 	struct rtentry *rt = (struct rtentry *)treenodes;
160 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(void *)rt_key(rt);
161 	struct radix_node *ret;
162 	char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN];
163 	uint32_t flags = rt->rt_flags;
164 	boolean_t verbose = (rt_verbose > 1);
165 
166 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
167 	RT_LOCK_ASSERT_HELD(rt);
168 
169 	if (verbose) {
170 		rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
171 	}
172 
173 	/*
174 	 * If this is a dynamic route (which is created via Redirect) and
175 	 * we already have the maximum acceptable number of such route entries,
176 	 * reject creating a new one.  We could initiate garbage collection to
177 	 * make available space right now, but the benefit would probably not
178 	 * be worth the cleaning overhead; we only have to endure a slightly
179 	 * suboptimal path even without the redirected route.
180 	 */
181 	if ((rt->rt_flags & RTF_DYNAMIC) &&
182 	    ip6_maxdynroutes >= 0 && in6dynroutes >= ip6_maxdynroutes) {
183 		return NULL;
184 	}
185 
186 	/*
187 	 * For IPv6, all unicast non-host routes are automatically cloning.
188 	 */
189 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
190 		rt->rt_flags |= RTF_MULTICAST;
191 	}
192 
193 	if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
194 		rt->rt_flags |= RTF_PRCLONING;
195 	}
196 
197 	/*
198 	 * A little bit of help for both IPv6 output and input:
199 	 *   For local addresses, we make sure that RTF_LOCAL is set,
200 	 *   with the thought that this might one day be used to speed up
201 	 *   ip_input().
202 	 *
203 	 * We also mark routes to multicast addresses as such, because
204 	 * it's easy to do and might be useful (but this is much more
205 	 * dubious since it's so easy to inspect the address).  (This
206 	 * is done above.)
207 	 *
208 	 * XXX
209 	 * should elaborate the code.
210 	 */
211 	if (rt->rt_flags & RTF_HOST) {
212 		IFA_LOCK_SPIN(rt->rt_ifa);
213 		if (in6_are_addr_equal_scoped(&satosin6(rt->rt_ifa->ifa_addr)->
214 		    sin6_addr, &sin6->sin6_addr, satosin6(rt->rt_ifa->ifa_addr)->sin6_scope_id, sin6->sin6_scope_id)) {
215 			rt->rt_flags |= RTF_LOCAL;
216 		}
217 		IFA_UNLOCK(rt->rt_ifa);
218 	}
219 
220 	if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
221 	    rt->rt_ifp) {
222 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
223 	}
224 
225 	/*
226 	 * rn_addroute route returns NULL if an exact entry is being attempted
227 	 * to be added again.
228 	 */
229 	ret = rn_addroute(v_arg, n_arg, head, treenodes);
230 	if (ret == NULL && (rt->rt_flags & RTF_HOST)) {
231 		struct rtentry *rt2;
232 		/*
233 		 * We are trying to add a host route, but can't.
234 		 * Find out if it is because of an
235 		 * ND6 entry and delete it if so.
236 		 */
237 		rt2 = rtalloc1_scoped_locked((struct sockaddr *)sin6, 0,
238 		    RTF_CLONING | RTF_PRCLONING, sin6_get_ifscope(rt_key(rt)));
239 		if (rt2 != NULL) {
240 			char dbufc[MAX_IPv6_STR_LEN];
241 
242 			RT_LOCK(rt2);
243 			if (verbose) {
244 				rt_str(rt2, dbufc, sizeof(dbufc), NULL, 0);
245 			}
246 
247 			if ((rt2->rt_flags & RTF_LLINFO) &&
248 			    (rt2->rt_flags & RTF_HOST) &&
249 			    rt2->rt_gateway != NULL &&
250 			    rt2->rt_gateway->sa_family == AF_LINK) {
251 				if (verbose) {
252 					os_log_debug(OS_LOG_DEFAULT, "%s: unable to insert "
253 					    "route to %s:%s, flags=%b, due to "
254 					    "existing ND6 route %s->%s "
255 					    "flags=%b, attempting to delete\n",
256 					    __func__, dbuf,
257 					    (rt->rt_ifp != NULL) ?
258 					    rt->rt_ifp->if_xname : "",
259 					    rt->rt_flags, RTF_BITS,
260 					    dbufc, (rt2->rt_ifp != NULL) ?
261 					    rt2->rt_ifp->if_xname : "",
262 					    rt2->rt_flags, RTF_BITS);
263 				}
264 				/*
265 				 * Safe to drop rt_lock and use rt_key,
266 				 * rt_gateway, since holding rnh_lock here
267 				 * prevents another thread from calling
268 				 * rt_setgate() on this route.
269 				 */
270 				RT_UNLOCK(rt2);
271 				(void) rtrequest_locked(RTM_DELETE, rt_key(rt2),
272 				    rt2->rt_gateway, rt_mask(rt2),
273 				    rt2->rt_flags, NULL);
274 				ret = rn_addroute(v_arg, n_arg, head,
275 				    treenodes);
276 			} else {
277 				RT_UNLOCK(rt2);
278 			}
279 			rtfree_locked(rt2);
280 		}
281 	} else if (ret == NULL && (rt->rt_flags & RTF_CLONING)) {
282 		struct rtentry *rt2;
283 		/*
284 		 * We are trying to add a net route, but can't.
285 		 * The following case should be allowed, so we'll make a
286 		 * special check for this:
287 		 *	Two IPv6 addresses with the same prefix is assigned
288 		 *	to a single interrface.
289 		 *	# ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
290 		 *	# ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
291 		 *	In this case, (*1) and (*2) want to add the same
292 		 *	net route entry, 3ffe:0501:: -> if0.
293 		 *	This case should not raise an error.
294 		 */
295 		rt2 = rtalloc1_scoped_locked((struct sockaddr *)sin6, 0,
296 		    RTF_CLONING | RTF_PRCLONING, sin6_get_ifscope(rt_key(rt)));
297 		if (rt2 != NULL) {
298 			RT_LOCK(rt2);
299 			if ((rt2->rt_flags & (RTF_CLONING | RTF_HOST |
300 			    RTF_GATEWAY)) == RTF_CLONING &&
301 			    rt2->rt_gateway &&
302 			    rt2->rt_gateway->sa_family == AF_LINK &&
303 			    rt2->rt_ifp == rt->rt_ifp) {
304 				ret = rt2->rt_nodes;
305 			}
306 			RT_UNLOCK(rt2);
307 			rtfree_locked(rt2);
308 		}
309 	}
310 
311 	if (ret != NULL && (rt->rt_flags & RTF_DYNAMIC)) {
312 		in6dynroutes++;
313 	}
314 
315 	if (!verbose) {
316 		goto done;
317 	}
318 
319 	if (ret != NULL) {
320 		if (flags != rt->rt_flags) {
321 			os_log_debug(OS_LOG_DEFAULT, "%s: route to %s->%s->%s inserted, "
322 			    "oflags=%b, flags=%b\n", __func__,
323 			    dbuf, gbuf, (rt->rt_ifp != NULL) ?
324 			    rt->rt_ifp->if_xname : "", flags, RTF_BITS,
325 			    rt->rt_flags, RTF_BITS);
326 		} else {
327 			os_log_debug(OS_LOG_DEFAULT, "%s: route to %s->%s->%s inserted, "
328 			    "flags=%b\n", __func__, dbuf, gbuf,
329 			    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
330 			    rt->rt_flags, RTF_BITS);
331 		}
332 	} else {
333 		os_log_debug(OS_LOG_DEFAULT, "%s: unable to insert route to %s->%s->%s, "
334 		    "flags=%b, already exists\n", __func__, dbuf, gbuf,
335 		    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
336 		    rt->rt_flags, RTF_BITS);
337 	}
338 done:
339 	return ret;
340 }
341 
342 static struct radix_node *
in6_deleteroute(void * v_arg,void * netmask_arg,struct radix_node_head * head)343 in6_deleteroute(void *v_arg, void *netmask_arg, struct radix_node_head *head)
344 {
345 	struct radix_node *rn;
346 
347 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
348 
349 	rn = rn_delete(v_arg, netmask_arg, head);
350 	if (rn != NULL) {
351 		struct rtentry *rt = (struct rtentry *)rn;
352 
353 		RT_LOCK(rt);
354 		if (rt->rt_flags & RTF_DYNAMIC) {
355 			in6dynroutes--;
356 		}
357 		if (rt_verbose > 1) {
358 			char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN];
359 
360 			rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
361 			os_log_debug(OS_LOG_DEFAULT, "%s: route to %s->%s->%s deleted, "
362 			    "flags=%b\n", __func__, dbuf, gbuf,
363 			    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
364 			    rt->rt_flags, RTF_BITS);
365 		}
366 		RT_UNLOCK(rt);
367 	}
368 	return rn;
369 }
370 
371 /*
372  * Validate (unexpire) an expiring AF_INET6 route.
373  */
374 struct radix_node *
in6_validate(struct radix_node * rn)375 in6_validate(struct radix_node *rn)
376 {
377 	struct rtentry *rt = (struct rtentry *)rn;
378 
379 	RT_LOCK_ASSERT_HELD(rt);
380 
381 	/* This is first reference? */
382 	if (rt->rt_refcnt == 0) {
383 		if (rt_verbose > 2) {
384 			char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN];
385 
386 			rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
387 			os_log_debug(OS_LOG_DEFAULT, "%s: route to %s->%s->%s validated, "
388 			    "flags=%b\n", __func__, dbuf, gbuf,
389 			    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
390 			    rt->rt_flags, RTF_BITS);
391 		}
392 
393 		/*
394 		 * It's one of ours; unexpire it.  If the timer is already
395 		 * scheduled, let it run later as it won't re-arm itself
396 		 * if there's nothing to do.
397 		 */
398 		if (rt->rt_flags & RTPRF_OURS) {
399 			rt->rt_flags &= ~RTPRF_OURS;
400 			rt_setexpire(rt, 0);
401 		}
402 	}
403 	return rn;
404 }
405 
406 /*
407  * Similar to in6_matroute_args except without the leaf-matching parameters.
408  */
409 static struct radix_node *
in6_matroute(void * v_arg,struct radix_node_head * head)410 in6_matroute(void *v_arg, struct radix_node_head *head)
411 {
412 	return in6_matroute_args(v_arg, head, NULL, NULL);
413 }
414 
415 /*
416  * This code is the inverse of in6_clsroute: on first reference, if we
417  * were managing the route, stop doing so and set the expiration timer
418  * back off again.
419  */
420 static struct radix_node *
in6_matroute_args(void * v_arg,struct radix_node_head * head,rn_matchf_t * f,void * w)421 in6_matroute_args(void *v_arg, struct radix_node_head *head,
422     rn_matchf_t *f, void *w)
423 {
424 	struct radix_node *rn = rn_match_args(v_arg, head, f, w);
425 
426 	if (rn != NULL) {
427 		RT_LOCK_SPIN((struct rtentry *)rn);
428 		in6_validate(rn);
429 		RT_UNLOCK((struct rtentry *)rn);
430 	}
431 	return rn;
432 }
433 
434 SYSCTL_DECL(_net_inet6_ip6);
435 
436 /* one hour is ``really old'' */
437 static uint32_t rtq_reallyold = 60 * 60;
438 SYSCTL_UINT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire,
439     CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_reallyold, 0, "");
440 
441 /* never automatically crank down to less */
442 static uint32_t rtq_minreallyold = 10;
443 SYSCTL_UINT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire,
444     CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_minreallyold, 0, "");
445 
446 /* 128 cached routes is ``too many'' */
447 static uint32_t rtq_toomany = 128;
448 SYSCTL_UINT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache,
449     CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_toomany, 0, "");
450 
451 /*
452  * On last reference drop, mark the route as belong to us so that it can be
453  * timed out.
454  */
455 static void
in6_clsroute(struct radix_node * rn,struct radix_node_head * head)456 in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
457 {
458 #pragma unused(head)
459 	char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN];
460 	struct rtentry *rt = (struct rtentry *)rn;
461 	boolean_t verbose = (rt_verbose > 1);
462 
463 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
464 	RT_LOCK_ASSERT_HELD(rt);
465 
466 	if (!(rt->rt_flags & RTF_UP)) {
467 		return;         /* prophylactic measures */
468 	}
469 	if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) {
470 		return;
471 	}
472 
473 	if (rt->rt_flags & RTPRF_OURS) {
474 		return;
475 	}
476 
477 	if (!(rt->rt_flags & (RTF_WASCLONED | RTF_DYNAMIC))) {
478 		return;
479 	}
480 
481 	if (verbose) {
482 		rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
483 	}
484 
485 	/*
486 	 * Delete the route immediately if RTF_DELCLONE is set or
487 	 * if route caching is disabled (rtq_reallyold set to 0).
488 	 * Otherwise, let it expire and be deleted by in6_rtqkill().
489 	 */
490 	if ((rt->rt_flags & RTF_DELCLONE) || rtq_reallyold == 0) {
491 		int err;
492 
493 		if (verbose) {
494 			os_log_debug(OS_LOG_DEFAULT, "%s: deleting route to %s->%s->%s, "
495 			    "flags=%b\n", __func__, dbuf, gbuf,
496 			    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
497 			    rt->rt_flags, RTF_BITS);
498 		}
499 		/*
500 		 * Delete the route from the radix tree but since we are
501 		 * called when the route's reference count is 0, don't
502 		 * deallocate it until we return from this routine by
503 		 * telling rtrequest that we're interested in it.
504 		 * Safe to drop rt_lock and use rt_key, rt_gateway,
505 		 * since holding rnh_lock here prevents another thread
506 		 * from calling rt_setgate() on this route.
507 		 */
508 		RT_UNLOCK(rt);
509 		err = rtrequest_locked(RTM_DELETE, rt_key(rt),
510 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, &rt);
511 		if (err == 0) {
512 			/* Now let the caller free it */
513 			RT_LOCK(rt);
514 			RT_REMREF_LOCKED(rt);
515 		} else {
516 			RT_LOCK(rt);
517 			if (!verbose) {
518 				rt_str(rt, dbuf, sizeof(dbuf),
519 				    gbuf, sizeof(gbuf));
520 			}
521 			os_log_error(OS_LOG_DEFAULT, "%s: error deleting route to "
522 			    "%s->%s->%s, flags=%b, err=%d\n", __func__,
523 			    dbuf, gbuf, (rt->rt_ifp != NULL) ?
524 			    rt->rt_ifp->if_xname : "", rt->rt_flags,
525 			    RTF_BITS, err);
526 		}
527 	} else {
528 		uint64_t timenow;
529 
530 		timenow = net_uptime();
531 		rt->rt_flags |= RTPRF_OURS;
532 		rt_setexpire(rt, timenow + rtq_reallyold);
533 
534 		if (verbose) {
535 			os_log_debug(OS_LOG_DEFAULT, "%s: route to %s->%s->%s invalidated, "
536 			    "flags=%b, expire=T+%u\n", __func__, dbuf, gbuf,
537 			    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
538 			    rt->rt_flags, RTF_BITS, rt->rt_expire - timenow);
539 		}
540 
541 		/* We have at least one entry; arm the timer if not already */
542 		in6_sched_rtqtimo(NULL);
543 	}
544 }
545 
546 struct rtqk_arg {
547 	struct radix_node_head *rnh;
548 	int updating;
549 	int draining;
550 	uint32_t killed;
551 	uint32_t found;
552 	uint64_t nextstop;
553 };
554 
555 /*
556  * Get rid of old routes.  When draining, this deletes everything, even when
557  * the timeout is not expired yet.  This also applies if the route is dynamic
558  * and there are sufficiently large number of such routes (more than a half of
559  * maximum).  When updating, this makes sure that nothing has a timeout longer
560  * than the current value of rtq_reallyold.
561  */
562 static int
in6_rtqkill(struct radix_node * rn,void * rock)563 in6_rtqkill(struct radix_node *rn, void *rock)
564 {
565 	struct rtqk_arg *ap = rock;
566 	struct rtentry *rt = (struct rtentry *)rn;
567 	boolean_t verbose = (rt_verbose > 1);
568 	uint64_t timenow;
569 	int err;
570 
571 	timenow = net_uptime();
572 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
573 
574 	RT_LOCK(rt);
575 	if (rt->rt_flags & RTPRF_OURS) {
576 		char dbuf[MAX_IPv6_STR_LEN], gbuf[MAX_IPv6_STR_LEN];
577 
578 		if (verbose) {
579 			rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
580 		}
581 
582 		ap->found++;
583 		VERIFY(rt->rt_expire == 0 || rt->rt_rmx.rmx_expire != 0);
584 		VERIFY(rt->rt_expire != 0 || rt->rt_rmx.rmx_expire == 0);
585 		if (ap->draining || rt->rt_expire <= timenow ||
586 		    ((rt->rt_flags & RTF_DYNAMIC) && ip6_maxdynroutes >= 0 &&
587 		    in6dynroutes > ip6_maxdynroutes / 2)) {
588 			if (rt->rt_refcnt > 0) {
589 				panic("%s: route %p marked with RTPRF_OURS "
590 				    "with non-zero refcnt (%u)", __func__,
591 				    rt, rt->rt_refcnt);
592 				/* NOTREACHED */
593 			}
594 
595 			if (verbose) {
596 				os_log_debug(OS_LOG_DEFAULT, "%s: deleting route to "
597 				    "%s->%s->%s, flags=%b, draining=%d\n",
598 				    __func__, dbuf, gbuf, (rt->rt_ifp != NULL) ?
599 				    rt->rt_ifp->if_xname : "", rt->rt_flags,
600 				    RTF_BITS, ap->draining);
601 			}
602 			RT_ADDREF_LOCKED(rt);   /* for us to free below */
603 			/*
604 			 * Delete this route since we're done with it;
605 			 * the route may be freed afterwards, so we
606 			 * can no longer refer to 'rt' upon returning
607 			 * from rtrequest().  Safe to drop rt_lock and
608 			 * use rt_key, rt_gateway, since holding rnh_lock
609 			 * here prevents another thread from calling
610 			 * rt_setgate() on this route.
611 			 */
612 			RT_UNLOCK(rt);
613 			err = rtrequest_locked(RTM_DELETE, rt_key(rt),
614 			    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
615 			if (err != 0) {
616 				RT_LOCK(rt);
617 				if (!verbose) {
618 					rt_str(rt, dbuf, sizeof(dbuf),
619 					    gbuf, sizeof(gbuf));
620 				}
621 				os_log_error(OS_LOG_DEFAULT, "%s: error deleting route to "
622 				    "%s->%s->%s, flags=%b, err=%d\n", __func__,
623 				    dbuf, gbuf, (rt->rt_ifp != NULL) ?
624 				    rt->rt_ifp->if_xname : "", rt->rt_flags,
625 				    RTF_BITS, err);
626 				RT_UNLOCK(rt);
627 			} else {
628 				ap->killed++;
629 			}
630 			rtfree_locked(rt);
631 		} else {
632 			uint64_t expire = (rt->rt_expire - timenow);
633 
634 			if (ap->updating && expire > rtq_reallyold) {
635 				rt_setexpire(rt, timenow + rtq_reallyold);
636 				if (verbose) {
637 					os_log_debug(OS_LOG_DEFAULT, "%s: route to "
638 					    "%s->%s->%s, flags=%b, adjusted "
639 					    "expire=T+%u (was T+%u)\n",
640 					    __func__, dbuf, gbuf,
641 					    (rt->rt_ifp != NULL) ?
642 					    rt->rt_ifp->if_xname : "",
643 					    rt->rt_flags, RTF_BITS,
644 					    (rt->rt_expire - timenow), expire);
645 				}
646 			}
647 			ap->nextstop = lmin(ap->nextstop, rt->rt_expire);
648 			RT_UNLOCK(rt);
649 		}
650 	} else {
651 		RT_UNLOCK(rt);
652 	}
653 
654 	return 0;
655 }
656 
657 #define RTQ_TIMEOUT     60*10   /* run no less than once every ten minutes */
658 static int rtq_timeout = RTQ_TIMEOUT;
659 
660 static void
in6_rtqtimo(void * targ)661 in6_rtqtimo(void *targ)
662 {
663 #pragma unused(targ)
664 	struct radix_node_head *rnh;
665 	struct rtqk_arg arg;
666 	struct timeval atv;
667 	static uint64_t last_adjusted_timeout = 0;
668 	boolean_t verbose = (rt_verbose > 1);
669 	uint64_t timenow;
670 	uint32_t ours;
671 
672 	lck_mtx_lock(rnh_lock);
673 	rnh = rt_tables[AF_INET6];
674 	VERIFY(rnh != NULL);
675 
676 	/* Get the timestamp after we acquire the lock for better accuracy */
677 	timenow = net_uptime();
678 	if (verbose) {
679 		os_log_debug(OS_LOG_DEFAULT, "%s: initial nextstop is T+%u seconds\n",
680 		    __func__, rtq_timeout);
681 	}
682 	bzero(&arg, sizeof(arg));
683 	arg.rnh = rnh;
684 	arg.nextstop = timenow + rtq_timeout;
685 	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
686 	if (verbose) {
687 		os_log_debug(OS_LOG_DEFAULT, "%s: found %u, killed %u\n", __func__,
688 		    arg.found, arg.killed);
689 	}
690 	/*
691 	 * Attempt to be somewhat dynamic about this:
692 	 * If there are ``too many'' routes sitting around taking up space,
693 	 * then crank down the timeout, and see if we can't make some more
694 	 * go away.  However, we make sure that we will never adjust more
695 	 * than once in rtq_timeout seconds, to keep from cranking down too
696 	 * hard.
697 	 */
698 	ours = (arg.found - arg.killed);
699 	if (ours > rtq_toomany &&
700 	    ((timenow - last_adjusted_timeout) >= (uint64_t)rtq_timeout) &&
701 	    rtq_reallyold > rtq_minreallyold) {
702 		rtq_reallyold = 2 * rtq_reallyold / 3;
703 		if (rtq_reallyold < rtq_minreallyold) {
704 			rtq_reallyold = rtq_minreallyold;
705 		}
706 
707 		last_adjusted_timeout = timenow;
708 		if (verbose) {
709 			os_log_debug(OS_LOG_DEFAULT, "%s: adjusted rtq_reallyold to %d "
710 			    "seconds\n", __func__, rtq_reallyold);
711 		}
712 		arg.found = arg.killed = 0;
713 		arg.updating = 1;
714 		rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
715 	}
716 
717 	atv.tv_usec = 0;
718 	atv.tv_sec = arg.nextstop - timenow;
719 	/* re-arm the timer only if there's work to do */
720 	in6_rtqtimo_run = 0;
721 	if (ours > 0) {
722 		in6_sched_rtqtimo(&atv);
723 	} else if (verbose) {
724 		os_log_debug(OS_LOG_DEFAULT, "%s: not rescheduling timer\n", __func__);
725 	}
726 	lck_mtx_unlock(rnh_lock);
727 }
728 
729 static void
in6_sched_rtqtimo(struct timeval * atv)730 in6_sched_rtqtimo(struct timeval *atv)
731 {
732 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
733 
734 	if (!in6_rtqtimo_run) {
735 		struct timeval tv;
736 
737 		if (atv == NULL) {
738 			tv.tv_usec = 0;
739 			tv.tv_sec = MAX(rtq_timeout / 10, 1);
740 			atv = &tv;
741 		}
742 		if (rt_verbose > 1) {
743 			os_log_debug(OS_LOG_DEFAULT, "%s: timer scheduled in "
744 			    "T+%llus.%lluu\n", __func__,
745 			    (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec);
746 		}
747 		in6_rtqtimo_run = 1;
748 		timeout(in6_rtqtimo, NULL, tvtohz(atv));
749 	}
750 }
751 
752 void
in6_rtqdrain(void)753 in6_rtqdrain(void)
754 {
755 	struct radix_node_head *rnh;
756 	struct rtqk_arg arg;
757 
758 	if (rt_verbose > 1) {
759 		os_log_debug(OS_LOG_DEFAULT, "%s: draining routes\n", __func__);
760 	}
761 
762 	lck_mtx_lock(rnh_lock);
763 	rnh = rt_tables[AF_INET6];
764 	VERIFY(rnh != NULL);
765 	bzero(&arg, sizeof(arg));
766 	arg.rnh = rnh;
767 	arg.draining = 1;
768 	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
769 	lck_mtx_unlock(rnh_lock);
770 }
771 
772 /*
773  * Initialize our routing tree.
774  */
775 int
in6_inithead(void ** head,int off)776 in6_inithead(void **head, int off)
777 {
778 	struct radix_node_head *rnh;
779 
780 	/* If called from route_init(), make sure it is exactly once */
781 	VERIFY(head != (void **)&rt_tables[AF_INET6] || *head == NULL);
782 
783 	if (!rn_inithead(head, off)) {
784 		return 0;
785 	}
786 
787 	/*
788 	 * We can get here from nfs_subs.c as well, in which case this
789 	 * won't be for the real routing table and thus we're done;
790 	 * this also takes care of the case when we're called more than
791 	 * once from anywhere but route_init().
792 	 */
793 	if (head != (void **)&rt_tables[AF_INET6]) {
794 		return 1;     /* only do this for the real routing table */
795 	}
796 	rnh = *head;
797 	rnh->rnh_addaddr = in6_addroute;
798 	rnh->rnh_deladdr = in6_deleteroute;
799 	rnh->rnh_matchaddr = in6_matroute;
800 	rnh->rnh_matchaddr_args = in6_matroute_args;
801 	rnh->rnh_close = in6_clsroute;
802 	return 1;
803 }
804