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