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