xref: /xnu-11417.121.6/bsd/netinet/in_rmx.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1 /*
2  * Copyright (c) 2000-2017 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  * Copyright 1994, 1995 Massachusetts Institute of Technology
30  *
31  * Permission to use, copy, modify, and distribute this software and
32  * its documentation for any purpose and without fee is hereby
33  * granted, provided that both the above copyright notice and this
34  * permission notice appear in all copies, that both the above
35  * copyright notice and this permission notice appear in all
36  * supporting documentation, and that the name of M.I.T. not be used
37  * in advertising or publicity pertaining to distribution of the
38  * software without specific, written prior permission.  M.I.T. makes
39  * no representations about the suitability of this software for any
40  * purpose.  It is provided "as is" without express or implied
41  * warranty.
42  *
43  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
44  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
45  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
46  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
47  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
50  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54  * SUCH DAMAGE.
55  *
56  */
57 
58 /*
59  * This code does two things necessary for the enhanced TCP metrics to
60  * function in a useful manner:
61  *  1) It marks all non-host routes as `cloning', thus ensuring that
62  *     every actual reference to such a route actually gets turned
63  *     into a reference to a host route to the specific destination
64  *     requested.
65  *  2) When such routes lose all their references, it arranges for them
66  *     to be deleted in some random collection of circumstances, so that
67  *     a large quantity of stale routing data is not kept in kernel memory
68  *     indefinitely.  See in_rtqtimo() below for the exact mechanism.
69  */
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
75 #include <sys/socket.h>
76 #include <sys/mbuf.h>
77 #include <sys/protosw.h>
78 #include <sys/syslog.h>
79 #include <sys/mcache.h>
80 #include <kern/locks.h>
81 
82 #include <net/if.h>
83 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/in_arp.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/nd6.h>
90 
91 #include <net/sockaddr_utils.h>
92 
93 extern int tvtohz(struct timeval *);
94 
95 static int in_rtqtimo_run;              /* in_rtqtimo is scheduled to run */
96 static void in_rtqtimo(void *);
97 static void in_sched_rtqtimo(struct timeval *);
98 
99 static struct radix_node *in_addroute(void *, void *, struct radix_node_head *,
100     struct radix_node *);
101 static struct radix_node *in_deleteroute(void *, void *,
102     struct radix_node_head *);
103 static struct radix_node *in_matroute(void *, struct radix_node_head *);
104 static struct radix_node *in_matroute_args(void *, struct radix_node_head *,
105     rn_matchf_t *f, void *);
106 static void in_clsroute(struct radix_node *, struct radix_node_head *);
107 static int in_rtqkill(struct radix_node *, void *);
108 
109 static int in_ifadownkill(struct radix_node *, void *);
110 
111 /*
112  * Do what we need to do when inserting a route.
113  */
114 static struct radix_node *
in_addroute(void * v_arg,void * n_arg,struct radix_node_head * head,struct radix_node * rn)115 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
116     struct radix_node *rn)
117 {
118 	rtentry_ref_t rt = RT(rn);
119 	struct sockaddr_in *sin = SIN(rt_key(rt));
120 	struct radix_node *ret;
121 	char dbuf[MAX_IPv4_STR_LEN], gbuf[MAX_IPv4_STR_LEN];
122 	uint32_t flags = rt->rt_flags;
123 	boolean_t verbose = (rt_verbose > 0);
124 
125 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
126 	RT_LOCK_ASSERT_HELD(rt);
127 
128 	if (verbose) {
129 		rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
130 	}
131 
132 	/*
133 	 * For IP, all unicast non-host routes are automatically cloning.
134 	 */
135 	if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
136 		rt->rt_flags |= RTF_MULTICAST;
137 	}
138 
139 	if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
140 		rt->rt_flags |= RTF_PRCLONING;
141 	}
142 
143 	/*
144 	 * A little bit of help for both IP output and input:
145 	 *   For host routes, we make sure that RTF_BROADCAST
146 	 *   is set for anything that looks like a broadcast address.
147 	 *   This way, we can avoid an expensive call to in_broadcast()
148 	 *   in ip_output() most of the time (because the route passed
149 	 *   to ip_output() is almost always a host route).
150 	 *
151 	 *   We also do the same for local addresses, with the thought
152 	 *   that this might one day be used to speed up ip_input().
153 	 *
154 	 * We also mark routes to multicast addresses as such, because
155 	 * it's easy to do and might be useful (but this is much more
156 	 * dubious since it's so easy to inspect the address).  (This
157 	 * is done above.)
158 	 */
159 	if (rt->rt_flags & RTF_HOST) {
160 		if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
161 			rt->rt_flags |= RTF_BROADCAST;
162 		} else {
163 			/* Become a regular mutex */
164 			RT_CONVERT_LOCK(rt);
165 			IFA_LOCK_SPIN(rt->rt_ifa);
166 			if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr ==
167 			    sin->sin_addr.s_addr) {
168 				rt->rt_flags |= RTF_LOCAL;
169 			}
170 			IFA_UNLOCK(rt->rt_ifa);
171 		}
172 	}
173 
174 	if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
175 	    rt->rt_ifp) {
176 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
177 		if (INTF_ADJUST_MTU_FOR_CLAT46(rt->rt_ifp)) {
178 			rt->rt_rmx.rmx_mtu = IN6_LINKMTU(rt->rt_ifp);
179 			/* Further adjust the size for CLAT46 expansion */
180 			rt->rt_rmx.rmx_mtu -= CLAT46_HDR_EXPANSION_OVERHD;
181 		}
182 	}
183 
184 	ret = rn_addroute(v_arg, n_arg, head, rt->rt_nodes);
185 	if (ret == NULL && (rt->rt_flags & RTF_HOST)) {
186 		rtentry_ref_t rt2;
187 		/*
188 		 * We are trying to add a host route, but can't.
189 		 * Find out if it is because of an
190 		 * ARP entry and delete it if so.
191 		 */
192 		rt2 = rtalloc1_scoped_locked(rt_key(rt), 0,
193 		    RTF_CLONING | RTF_PRCLONING, sin_get_ifscope(rt_key(rt)));
194 		if (rt2 != NULL) {
195 			char dbufc[MAX_IPv4_STR_LEN];
196 
197 			RT_LOCK(rt2);
198 			if (verbose) {
199 				rt_str(rt2, dbufc, sizeof(dbufc), NULL, 0);
200 			}
201 
202 			if ((rt2->rt_flags & RTF_LLINFO) &&
203 			    (rt2->rt_flags & RTF_HOST) &&
204 			    rt2->rt_gateway != NULL &&
205 			    rt2->rt_gateway->sa_family == AF_LINK) {
206 				if (verbose) {
207 					os_log_debug(OS_LOG_DEFAULT, "%s: unable to insert "
208 					    "route to %s;%s, flags=0x%x, due to "
209 					    "existing ARP route %s->%s "
210 					    "flags=0x%x, attempting to delete\n",
211 					    __func__, dbuf,
212 					    (rt->rt_ifp != NULL) ?
213 					    rt->rt_ifp->if_xname : "",
214 					    rt->rt_flags, dbufc,
215 					    (rt2->rt_ifp != NULL) ?
216 					    rt2->rt_ifp->if_xname : "",
217 					    rt2->rt_flags);
218 				}
219 				/*
220 				 * Safe to drop rt_lock and use rt_key,
221 				 * rt_gateway, since holding rnh_lock here
222 				 * prevents another thread from calling
223 				 * rt_setgate() on this route.
224 				 */
225 				RT_UNLOCK(rt2);
226 				(void) rtrequest_locked(RTM_DELETE, rt_key(rt2),
227 				    rt2->rt_gateway, rt_mask(rt2),
228 				    rt2->rt_flags, NULL);
229 				ret = rn_addroute(v_arg, n_arg, head, rt->rt_nodes);
230 			} else {
231 				RT_UNLOCK(rt2);
232 			}
233 			rtfree_locked(rt2);
234 		}
235 	}
236 
237 	if (!verbose) {
238 		goto done;
239 	}
240 
241 	if (ret != NULL) {
242 		if (flags != rt->rt_flags) {
243 			os_log_debug(OS_LOG_DEFAULT, "%s: route to %s->%s->%s inserted, "
244 			    "oflags=0x%x, flags=0x%x\n", __func__,
245 			    dbuf, gbuf, (rt->rt_ifp != NULL) ?
246 			    rt->rt_ifp->if_xname : "", flags,
247 			    rt->rt_flags);
248 		} else {
249 			os_log_debug(OS_LOG_DEFAULT, "%s: route to %s->%s->%s inserted, "
250 			    "flags=0x%x\n", __func__, dbuf, gbuf,
251 			    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
252 			    rt->rt_flags);
253 		}
254 	} else {
255 		os_log_debug(OS_LOG_DEFAULT, "%s: unable to insert route to %s->%s->%s, "
256 		    "flags=0x%x, already exists\n", __func__, dbuf, gbuf,
257 		    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
258 		    rt->rt_flags);
259 	}
260 done:
261 	return ret;
262 }
263 
264 static struct radix_node *
in_deleteroute(void * v_arg,void * netmask_arg,struct radix_node_head * head)265 in_deleteroute(void *v_arg, void *netmask_arg, struct radix_node_head *head)
266 {
267 	struct radix_node *rn;
268 
269 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
270 
271 	rn = rn_delete(v_arg, netmask_arg, head);
272 	if (rt_verbose > 0 && rn != NULL) {
273 		char dbuf[MAX_IPv4_STR_LEN], gbuf[MAX_IPv4_STR_LEN];
274 		rtentry_ref_t rt = RT(rn);
275 
276 		RT_LOCK(rt);
277 		rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
278 		os_log_debug(OS_LOG_DEFAULT, "%s: route to %s->%s->%s deleted, "
279 		    "flags=0x%x\n", __func__, dbuf, gbuf, (rt->rt_ifp != NULL) ?
280 		    rt->rt_ifp->if_xname : "", rt->rt_flags);
281 		RT_UNLOCK(rt);
282 	}
283 	return rn;
284 }
285 
286 /*
287  * Validate (unexpire) an expiring AF_INET route.
288  */
289 struct radix_node *
in_validate(struct radix_node * rn)290 in_validate(struct radix_node *rn)
291 {
292 	rtentry_ref_t rt = RT(rn);
293 
294 	RT_LOCK_ASSERT_HELD(rt);
295 
296 	/* This is first reference? */
297 	if (rt->rt_refcnt == 0) {
298 		if (rt_verbose > 2) {
299 			char dbuf[MAX_IPv4_STR_LEN], gbuf[MAX_IPv4_STR_LEN];
300 
301 			rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
302 			os_log_debug(OS_LOG_DEFAULT, "%s: route to %s->%s->%s validated, "
303 			    "flags=0x%x\n", __func__, dbuf, gbuf,
304 			    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
305 			    rt->rt_flags);
306 		}
307 
308 		/*
309 		 * It's one of ours; unexpire it.  If the timer is already
310 		 * scheduled, let it run later as it won't re-arm itself
311 		 * if there's nothing to do.
312 		 */
313 		if (rt->rt_flags & RTPRF_OURS) {
314 			rt->rt_flags &= ~RTPRF_OURS;
315 			rt_setexpire(rt, 0);
316 		}
317 	}
318 	return rn;
319 }
320 
321 /*
322  * Similar to in_matroute_args except without the leaf-matching parameters.
323  */
324 static struct radix_node *
in_matroute(void * v_arg,struct radix_node_head * head)325 in_matroute(void *v_arg, struct radix_node_head *head)
326 {
327 	return in_matroute_args(v_arg, head, NULL, NULL);
328 }
329 
330 /*
331  * This code is the inverse of in_clsroute: on first reference, if we
332  * were managing the route, stop doing so and set the expiration timer
333  * back off again.
334  */
335 static struct radix_node *
in_matroute_args(void * v_arg,struct radix_node_head * head,rn_matchf_t * f,void * w)336 in_matroute_args(void *v_arg, struct radix_node_head *head,
337     rn_matchf_t *f, void *w)
338 {
339 	struct radix_node *rn = rn_match_args(v_arg, head, f, w);
340 
341 	if (rn != NULL) {
342 		rtentry_ref_t rt = RT(rn);
343 		RT_LOCK_SPIN(rt);
344 		in_validate(rn);
345 		RT_UNLOCK(rt);
346 	}
347 	return rn;
348 }
349 
350 /* one hour is ``really old'' */
351 static uint32_t rtq_reallyold = 60 * 60;
352 SYSCTL_UINT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire,
353     CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_reallyold, 0,
354     "Default expiration time on dynamically learned routes");
355 
356 /* never automatically crank down to less */
357 static uint32_t rtq_minreallyold = 10;
358 SYSCTL_UINT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire,
359     CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_minreallyold, 0,
360     "Minimum time to attempt to hold onto dynamically learned routes");
361 
362 /* 128 cached routes is ``too many'' */
363 static uint32_t rtq_toomany = 128;
364 SYSCTL_UINT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache,
365     CTLFLAG_RW | CTLFLAG_LOCKED, &rtq_toomany, 0,
366     "Upper limit on dynamically learned routes");
367 
368 /*
369  * On last reference drop, mark the route as belong to us so that it can be
370  * timed out.
371  */
372 static void
in_clsroute(struct radix_node * rn,struct radix_node_head * head)373 in_clsroute(struct radix_node *rn, struct radix_node_head *head)
374 {
375 #pragma unused(head)
376 	char dbuf[MAX_IPv4_STR_LEN], gbuf[MAX_IPv4_STR_LEN];
377 	rtentry_ref_t rt = RT(rn);
378 	boolean_t verbose = (rt_verbose > 1);
379 
380 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
381 	RT_LOCK_ASSERT_HELD(rt);
382 
383 	if (!(rt->rt_flags & RTF_UP)) {
384 		return;         /* prophylactic measures */
385 	}
386 	if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) {
387 		return;
388 	}
389 
390 	if (rt->rt_flags & RTPRF_OURS) {
391 		return;
392 	}
393 
394 	if (!(rt->rt_flags & (RTF_WASCLONED | RTF_DYNAMIC))) {
395 		return;
396 	}
397 
398 	if (verbose) {
399 		rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
400 	}
401 
402 	/*
403 	 * Delete the route immediately if RTF_DELCLONE is set or
404 	 * if route caching is disabled (rtq_reallyold set to 0).
405 	 * Otherwise, let it expire and be deleted by in_rtqkill().
406 	 */
407 	if ((rt->rt_flags & RTF_DELCLONE) || rtq_reallyold == 0) {
408 		int err;
409 
410 		if (verbose) {
411 			os_log_debug(OS_LOG_DEFAULT, "%s: deleting route to %s->%s->%s, "
412 			    "flags=0x%x\n", __func__, dbuf, gbuf,
413 			    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
414 			    rt->rt_flags);
415 		}
416 		/*
417 		 * Delete the route from the radix tree but since we are
418 		 * called when the route's reference count is 0, don't
419 		 * deallocate it until we return from this routine by
420 		 * telling rtrequest that we're interested in it.
421 		 * Safe to drop rt_lock and use rt_key, rt_gateway since
422 		 * holding rnh_lock here prevents another thread from
423 		 * calling rt_setgate() on this route.
424 		 */
425 		RT_UNLOCK(rt);
426 		err = rtrequest_locked(RTM_DELETE, rt_key(rt),
427 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, &rt);
428 		if (err == 0) {
429 			/* Now let the caller free it */
430 			RT_LOCK(rt);
431 			RT_REMREF_LOCKED(rt);
432 		} else {
433 			RT_LOCK(rt);
434 			rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
435 			os_log_error(OS_LOG_DEFAULT, "%s: error deleting route to "
436 			    "%s->%s->%s, flags=0x%x, err=%d\n", __func__,
437 			    dbuf, gbuf, (rt->rt_ifp != NULL) ?
438 			    rt->rt_ifp->if_xname : "", rt->rt_flags,
439 			    err);
440 		}
441 	} else {
442 		uint64_t timenow;
443 
444 		timenow = net_uptime();
445 		rt->rt_flags |= RTPRF_OURS;
446 		rt_setexpire(rt, timenow + rtq_reallyold);
447 
448 		if (rt_verbose > 2) {
449 			os_log_debug(OS_LOG_DEFAULT, "%s: route to %s->%s->%s invalidated, "
450 			    "flags=0x%x, expire=T+%u\n", __func__, dbuf, gbuf,
451 			    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
452 			    rt->rt_flags, rt->rt_expire - timenow);
453 		}
454 
455 		/* We have at least one entry; arm the timer if not already */
456 		in_sched_rtqtimo(NULL);
457 	}
458 }
459 
460 struct rtqk_arg {
461 	struct radix_node_head *rnh;
462 	int updating;
463 	int draining;
464 	uint32_t killed;
465 	uint32_t found;
466 	uint64_t nextstop;
467 };
468 __CCT_DECLARE_CONSTRAINED_PTR_TYPE(struct rtqk_arg, rtqk_arg, __CCT_REF);
469 
470 /*
471  * Get rid of old routes.  When draining, this deletes everything, even when
472  * the timeout is not expired yet.  When updating, this makes sure that
473  * nothing has a timeout longer than the current value of rtq_reallyold.
474  */
475 static int
in_rtqkill(struct radix_node * rn,void * rock)476 in_rtqkill(struct radix_node *rn, void *rock)
477 {
478 	rtqk_arg_ref_t ap = rock;
479 	rtentry_ref_t rt = RT(rn);
480 	boolean_t verbose = (rt_verbose > 1);
481 	uint64_t timenow;
482 	int err;
483 
484 	timenow = net_uptime();
485 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
486 
487 	RT_LOCK(rt);
488 	if (rt->rt_flags & RTPRF_OURS) {
489 		char dbuf[MAX_IPv4_STR_LEN], gbuf[MAX_IPv4_STR_LEN];
490 
491 		if (verbose) {
492 			rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
493 		}
494 
495 		ap->found++;
496 		VERIFY(rt->rt_expire == 0 || rt->rt_rmx.rmx_expire != 0);
497 		VERIFY(rt->rt_expire != 0 || rt->rt_rmx.rmx_expire == 0);
498 		if (ap->draining || rt->rt_expire <= timenow) {
499 			if (rt->rt_refcnt > 0) {
500 				panic("%s: route %p marked with RTPRF_OURS "
501 				    "with non-zero refcnt (%u)", __func__,
502 				    rt, rt->rt_refcnt);
503 				/* NOTREACHED */
504 			}
505 
506 			if (verbose) {
507 				os_log_debug(OS_LOG_DEFAULT, "%s: deleting route to "
508 				    "%s->%s->%s, flags=0x%x, draining=%d\n",
509 				    __func__, dbuf, gbuf, (rt->rt_ifp != NULL) ?
510 				    rt->rt_ifp->if_xname : "", rt->rt_flags,
511 				    ap->draining);
512 			}
513 			RT_ADDREF_LOCKED(rt);   /* for us to free below */
514 			/*
515 			 * Delete this route since we're done with it;
516 			 * the route may be freed afterwards, so we
517 			 * can no longer refer to 'rt' upon returning
518 			 * from rtrequest().  Safe to drop rt_lock and
519 			 * use rt_key, rt_gateway since holding rnh_lock
520 			 * here prevents another thread from calling
521 			 * rt_setgate() on this route.
522 			 */
523 			RT_UNLOCK(rt);
524 			err = rtrequest_locked(RTM_DELETE, rt_key(rt),
525 			    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
526 			if (err != 0) {
527 				RT_LOCK(rt);
528 				if (!verbose) {
529 					rt_str(rt, dbuf, sizeof(dbuf),
530 					    gbuf, sizeof(gbuf));
531 				}
532 				os_log_error(OS_LOG_DEFAULT, "%s: error deleting route to "
533 				    "%s->%s->%s, flags=0x%x, err=%d\n", __func__,
534 				    dbuf, gbuf, (rt->rt_ifp != NULL) ?
535 				    rt->rt_ifp->if_xname : "", rt->rt_flags,
536 				    err);
537 				RT_UNLOCK(rt);
538 			} else {
539 				ap->killed++;
540 			}
541 			rtfree_locked(rt);
542 		} else {
543 			uint64_t expire = (rt->rt_expire - timenow);
544 
545 			if (ap->updating && expire > rtq_reallyold) {
546 				rt_setexpire(rt, timenow + rtq_reallyold);
547 				if (verbose) {
548 					os_log_debug(OS_LOG_DEFAULT, "%s: route to "
549 					    "%s->%s->%s, flags=0x%x, adjusted "
550 					    "expire=T+%u (was T+%u)\n",
551 					    __func__, dbuf, gbuf,
552 					    (rt->rt_ifp != NULL) ?
553 					    rt->rt_ifp->if_xname : "",
554 					    rt->rt_flags,
555 					    (rt->rt_expire - timenow), expire);
556 				}
557 			}
558 			ap->nextstop = lmin(ap->nextstop, rt->rt_expire);
559 			RT_UNLOCK(rt);
560 		}
561 	} else {
562 		RT_UNLOCK(rt);
563 	}
564 
565 	return 0;
566 }
567 
568 #define RTQ_TIMEOUT     60*10   /* run no less than once every ten minutes */
569 static int rtq_timeout = RTQ_TIMEOUT;
570 
571 static void
in_rtqtimo(void * targ)572 in_rtqtimo(void *targ)
573 {
574 #pragma unused(targ)
575 	struct radix_node_head *rnh;
576 	struct rtqk_arg arg;
577 	struct timeval atv;
578 	static uint64_t last_adjusted_timeout = 0;
579 	boolean_t verbose = (rt_verbose > 1);
580 	uint64_t timenow;
581 	uint32_t ours;
582 
583 	lck_mtx_lock(rnh_lock);
584 	rnh = rt_tables[AF_INET];
585 	VERIFY(rnh != NULL);
586 
587 	/* Get the timestamp after we acquire the lock for better accuracy */
588 	timenow = net_uptime();
589 	if (verbose) {
590 		os_log_debug(OS_LOG_DEFAULT, "%s: initial nextstop is T+%u seconds\n",
591 		    __func__, rtq_timeout);
592 	}
593 	bzero(&arg, sizeof(arg));
594 	arg.rnh = rnh;
595 	arg.nextstop = timenow + rtq_timeout;
596 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
597 	if (verbose) {
598 		os_log_debug(OS_LOG_DEFAULT, "%s: found %u, killed %u\n", __func__,
599 		    arg.found, arg.killed);
600 	}
601 	/*
602 	 * Attempt to be somewhat dynamic about this:
603 	 * If there are ``too many'' routes sitting around taking up space,
604 	 * then crank down the timeout, and see if we can't make some more
605 	 * go away.  However, we make sure that we will never adjust more
606 	 * than once in rtq_timeout seconds, to keep from cranking down too
607 	 * hard.
608 	 */
609 	ours = (arg.found - arg.killed);
610 	if (ours > rtq_toomany &&
611 	    ((timenow - last_adjusted_timeout) >= (uint64_t)rtq_timeout) &&
612 	    rtq_reallyold > rtq_minreallyold) {
613 		rtq_reallyold = 2 * rtq_reallyold / 3;
614 		if (rtq_reallyold < rtq_minreallyold) {
615 			rtq_reallyold = rtq_minreallyold;
616 		}
617 
618 		last_adjusted_timeout = timenow;
619 		if (verbose) {
620 			os_log_debug(OS_LOG_DEFAULT, "%s: adjusted rtq_reallyold to %d "
621 			    "seconds\n", __func__, rtq_reallyold);
622 		}
623 		arg.found = arg.killed = 0;
624 		arg.updating = 1;
625 		rnh->rnh_walktree(rnh, in_rtqkill, &arg);
626 	}
627 
628 	atv.tv_usec = 0;
629 	atv.tv_sec = arg.nextstop - timenow;
630 	/* re-arm the timer only if there's work to do */
631 	in_rtqtimo_run = 0;
632 	if (ours > 0) {
633 		in_sched_rtqtimo(&atv);
634 	} else if (verbose) {
635 		os_log_debug(OS_LOG_DEFAULT, "%s: not rescheduling timer\n", __func__);
636 	}
637 	lck_mtx_unlock(rnh_lock);
638 }
639 
640 static void
in_sched_rtqtimo(struct timeval * atv)641 in_sched_rtqtimo(struct timeval *atv)
642 {
643 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
644 
645 	if (!in_rtqtimo_run) {
646 		struct timeval tv;
647 
648 		if (atv == NULL) {
649 			tv.tv_usec = 0;
650 			tv.tv_sec = MAX(rtq_timeout / 10, 1);
651 			atv = &tv;
652 		}
653 		if (rt_verbose > 1) {
654 			os_log_debug(OS_LOG_DEFAULT, "%s: timer scheduled in "
655 			    "T+%llus.%lluu\n", __func__,
656 			    (uint64_t)atv->tv_sec, (uint64_t)atv->tv_usec);
657 		}
658 		in_rtqtimo_run = 1;
659 		timeout(in_rtqtimo, NULL, tvtohz(atv));
660 	}
661 }
662 
663 void
in_rtqdrain(void)664 in_rtqdrain(void)
665 {
666 	struct radix_node_head *rnh;
667 	struct rtqk_arg arg;
668 
669 	if (rt_verbose > 1) {
670 		os_log_debug(OS_LOG_DEFAULT, "%s: draining routes\n", __func__);
671 	}
672 
673 	lck_mtx_lock(rnh_lock);
674 	rnh = rt_tables[AF_INET];
675 	VERIFY(rnh != NULL);
676 	bzero(&arg, sizeof(arg));
677 	arg.rnh = rnh;
678 	arg.draining = 1;
679 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
680 	lck_mtx_unlock(rnh_lock);
681 }
682 
683 /*
684  * Initialize our routing tree.
685  */
686 int
in_inithead(void ** head,int off)687 in_inithead(void **head, int off)
688 {
689 	radix_node_head_ref_t rnh;
690 
691 	void *__single *__single inet_rt_table = (void *__single *__single)&rt_tables[AF_INET];
692 
693 	/* If called from route_init(), make sure it is exactly once */
694 	VERIFY(head != inet_rt_table || *head == NULL);
695 
696 	if (!rn_inithead(head, off)) {
697 		return 0;
698 	}
699 
700 	/*
701 	 * We can get here from nfs_subs.c as well, in which case this
702 	 * won't be for the real routing table and thus we're done;
703 	 * this also takes care of the case when we're called more than
704 	 * once from anywhere but route_init().
705 	 */
706 	if (head != inet_rt_table) {
707 		return 1;     /* only do this for the real routing table */
708 	}
709 	rnh = *(void * __single * __single)head;
710 	rnh->rnh_addaddr = in_addroute;
711 	rnh->rnh_deladdr = in_deleteroute;
712 	rnh->rnh_matchaddr = in_matroute;
713 	rnh->rnh_matchaddr_args = in_matroute_args;
714 	rnh->rnh_close = in_clsroute;
715 	return 1;
716 }
717 
718 /*
719  * This zaps old routes when the interface goes down or interface
720  * address is deleted.  In the latter case, it deletes static routes
721  * that point to this address.  If we don't do this, we may end up
722  * using the old address in the future.  The ones we always want to
723  * get rid of are things like ARP entries, since the user might down
724  * the interface, walk over to a completely different network, and
725  * plug back in.
726  */
727 struct in_ifadown_arg {
728 	struct radix_node_head *rnh;
729 	struct ifaddr *ifa;
730 	int del;
731 };
732 __CCT_DECLARE_CONSTRAINED_PTR_TYPE(struct in_ifadown_arg, in_ifadown_arg, __CCT_REF);
733 
734 static int
in_ifadownkill(struct radix_node * rn,void * xap)735 in_ifadownkill(struct radix_node *rn, void *xap)
736 {
737 	char dbuf[MAX_IPv4_STR_LEN], gbuf[MAX_IPv4_STR_LEN];
738 	in_ifadown_arg_ref_t ap = xap;
739 	rtentry_ref_t rt = RT(rn);
740 	boolean_t verbose = (rt_verbose > 1);
741 	int err;
742 
743 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
744 
745 	RT_LOCK(rt);
746 	if (rt->rt_ifa == ap->ifa &&
747 	    (ap->del || !(rt->rt_flags & RTF_STATIC))) {
748 		rt_str(rt, dbuf, sizeof(dbuf), gbuf, sizeof(gbuf));
749 		if (verbose) {
750 			os_log_debug(OS_LOG_DEFAULT, "%s: deleting route to %s->%s->%s, "
751 			    "flags=0x%x\n", __func__, dbuf, gbuf,
752 			    (rt->rt_ifp != NULL) ? rt->rt_ifp->if_xname : "",
753 			    rt->rt_flags);
754 		}
755 
756 		RT_ADDREF_LOCKED(rt);   /* for us to free below */
757 		/*
758 		 * We need to disable the automatic prune that happens
759 		 * in this case in rtrequest() because it will blow
760 		 * away the pointers that rn_walktree() needs in order
761 		 * continue our descent.  We will end up deleting all
762 		 * the routes that rtrequest() would have in any case,
763 		 * so that behavior is not needed there.  Safe to drop
764 		 * rt_lock and use rt_key, rt_gateway, since holding
765 		 * rnh_lock here prevents another thread from calling
766 		 * rt_setgate() on this route.
767 		 */
768 		rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
769 		RT_UNLOCK(rt);
770 		err = rtrequest_locked(RTM_DELETE, rt_key(rt),
771 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
772 		if (err != 0) {
773 			RT_LOCK(rt);
774 			if (!verbose) {
775 				rt_str(rt, dbuf, sizeof(dbuf),
776 				    gbuf, sizeof(gbuf));
777 			}
778 			os_log_error(OS_LOG_DEFAULT, "%s: error deleting route to "
779 			    "%s->%s->%s, flags=0x%x, err=%d\n", __func__,
780 			    dbuf, gbuf, (rt->rt_ifp != NULL) ?
781 			    rt->rt_ifp->if_xname : "", rt->rt_flags,
782 			    err);
783 			RT_UNLOCK(rt);
784 		}
785 		rtfree_locked(rt);
786 	} else {
787 		RT_UNLOCK(rt);
788 	}
789 	return 0;
790 }
791 
792 int
in_ifadown(struct ifaddr * ifa,int delete)793 in_ifadown(struct ifaddr *ifa, int delete)
794 {
795 	struct in_ifadown_arg arg;
796 	struct radix_node_head *rnh;
797 
798 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
799 
800 	/*
801 	 * Holding rnh_lock here prevents the possibility of
802 	 * ifa from changing (e.g. in_ifinit), so it is safe
803 	 * to access its ifa_addr without locking.
804 	 */
805 	if (ifa->ifa_addr->sa_family != AF_INET) {
806 		return 1;
807 	}
808 
809 	/* trigger route cache reevaluation */
810 	routegenid_inet_update();
811 
812 	arg.rnh = rnh = rt_tables[AF_INET];
813 	arg.ifa = ifa;
814 	arg.del = delete;
815 	rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
816 	IFA_LOCK_SPIN(ifa);
817 	ifa->ifa_flags &= ~IFA_ROUTE;
818 	IFA_UNLOCK(ifa);
819 	return 0;
820 }
821