]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_rmx.c
2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright 1994, 1995 Massachusetts Institute of Technology
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
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
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
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.
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>
77 #include <sys/protosw.h>
78 #include <sys/syslog.h>
79 #include <sys/mcache.h>
80 #include <kern/locks.h>
83 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/in_arp.h>
88 extern int tvtohz(struct timeval
*);
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
*);
94 static struct radix_node
*in_addroute(void *, void *, struct radix_node_head
*,
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 *);
104 static int in_ifadownkill(struct radix_node
*, void *);
106 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
109 * Do what we need to do when inserting a route.
111 static struct radix_node
*
112 in_addroute(void *v_arg
, void *n_arg
, struct radix_node_head
*head
,
113 struct radix_node
*treenodes
)
115 struct rtentry
*rt
= (struct rtentry
*)treenodes
;
116 struct sockaddr_in
*sin
= (struct sockaddr_in
*)(void *)rt_key(rt
);
117 struct radix_node
*ret
;
118 char dbuf
[MAX_IPv4_STR_LEN
], gbuf
[MAX_IPv4_STR_LEN
];
119 uint32_t flags
= rt
->rt_flags
;
120 boolean_t verbose
= (rt_verbose
> 1);
122 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
123 RT_LOCK_ASSERT_HELD(rt
);
126 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
129 * For IP, all unicast non-host routes are automatically cloning.
131 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
132 rt
->rt_flags
|= RTF_MULTICAST
;
134 if (!(rt
->rt_flags
& (RTF_HOST
| RTF_CLONING
| RTF_MULTICAST
)))
135 rt
->rt_flags
|= RTF_PRCLONING
;
138 * A little bit of help for both IP output and input:
139 * For host routes, we make sure that RTF_BROADCAST
140 * is set for anything that looks like a broadcast address.
141 * This way, we can avoid an expensive call to in_broadcast()
142 * in ip_output() most of the time (because the route passed
143 * to ip_output() is almost always a host route).
145 * We also do the same for local addresses, with the thought
146 * that this might one day be used to speed up ip_input().
148 * We also mark routes to multicast addresses as such, because
149 * it's easy to do and might be useful (but this is much more
150 * dubious since it's so easy to inspect the address). (This
153 if (rt
->rt_flags
& RTF_HOST
) {
154 if (in_broadcast(sin
->sin_addr
, rt
->rt_ifp
)) {
155 rt
->rt_flags
|= RTF_BROADCAST
;
157 /* Become a regular mutex */
159 IFA_LOCK_SPIN(rt
->rt_ifa
);
160 if (satosin(rt
->rt_ifa
->ifa_addr
)->sin_addr
.s_addr
==
161 sin
->sin_addr
.s_addr
)
162 rt
->rt_flags
|= RTF_LOCAL
;
163 IFA_UNLOCK(rt
->rt_ifa
);
167 if (!rt
->rt_rmx
.rmx_mtu
&& !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) &&
169 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
171 ret
= rn_addroute(v_arg
, n_arg
, head
, treenodes
);
172 if (ret
== NULL
&& (rt
->rt_flags
& RTF_HOST
)) {
175 * We are trying to add a host route, but can't.
176 * Find out if it is because of an
177 * ARP entry and delete it if so.
179 rt2
= rtalloc1_scoped_locked(rt_key(rt
), 0,
180 RTF_CLONING
| RTF_PRCLONING
, sin_get_ifscope(rt_key(rt
)));
182 char dbufc
[MAX_IPv4_STR_LEN
];
186 rt_str(rt2
, dbufc
, sizeof (dbufc
), NULL
, 0);
188 if ((rt2
->rt_flags
& RTF_LLINFO
) &&
189 (rt2
->rt_flags
& RTF_HOST
) &&
190 rt2
->rt_gateway
!= NULL
&&
191 rt2
->rt_gateway
->sa_family
== AF_LINK
) {
193 log(LOG_DEBUG
, "%s: unable to insert "
194 "route to %s;%s, flags=%b, due to "
195 "existing ARP route %s->%s "
196 "flags=%b, attempting to delete\n",
198 (rt
->rt_ifp
!= NULL
) ?
199 rt
->rt_ifp
->if_xname
: "",
200 rt
->rt_flags
, RTF_BITS
, dbufc
,
201 (rt2
->rt_ifp
!= NULL
) ?
202 rt2
->rt_ifp
->if_xname
: "",
203 rt2
->rt_flags
, RTF_BITS
);
206 * Safe to drop rt_lock and use rt_key,
207 * rt_gateway, since holding rnh_lock here
208 * prevents another thread from calling
209 * rt_setgate() on this route.
212 (void) rtrequest_locked(RTM_DELETE
, rt_key(rt2
),
213 rt2
->rt_gateway
, rt_mask(rt2
),
214 rt2
->rt_flags
, NULL
);
215 ret
= rn_addroute(v_arg
, n_arg
, head
,
228 if (flags
!= rt
->rt_flags
) {
229 log(LOG_DEBUG
, "%s: route to %s->%s->%s inserted, "
230 "oflags=%b, flags=%b\n", __func__
,
231 dbuf
, gbuf
, (rt
->rt_ifp
!= NULL
) ?
232 rt
->rt_ifp
->if_xname
: "", flags
, RTF_BITS
,
233 rt
->rt_flags
, RTF_BITS
);
235 log(LOG_DEBUG
, "%s: route to %s->%s->%s inserted, "
236 "flags=%b\n", __func__
, dbuf
, gbuf
,
237 (rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: "",
238 rt
->rt_flags
, RTF_BITS
);
241 log(LOG_DEBUG
, "%s: unable to insert route to %s->%s->%s, "
242 "flags=%b, already exists\n", __func__
, dbuf
, gbuf
,
243 (rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: "",
244 rt
->rt_flags
, RTF_BITS
);
250 static struct radix_node
*
251 in_deleteroute(void *v_arg
, void *netmask_arg
, struct radix_node_head
*head
)
253 struct radix_node
*rn
;
255 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
257 rn
= rn_delete(v_arg
, netmask_arg
, head
);
258 if (rt_verbose
> 1 && rn
!= NULL
) {
259 char dbuf
[MAX_IPv4_STR_LEN
], gbuf
[MAX_IPv4_STR_LEN
];
260 struct rtentry
*rt
= (struct rtentry
*)rn
;
263 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
264 log(LOG_DEBUG
, "%s: route to %s->%s->%s deleted, "
265 "flags=%b\n", __func__
, dbuf
, gbuf
, (rt
->rt_ifp
!= NULL
) ?
266 rt
->rt_ifp
->if_xname
: "", rt
->rt_flags
, RTF_BITS
);
273 * Validate (unexpire) an expiring AF_INET route.
276 in_validate(struct radix_node
*rn
)
278 struct rtentry
*rt
= (struct rtentry
*)rn
;
280 RT_LOCK_ASSERT_HELD(rt
);
282 /* This is first reference? */
283 if (rt
->rt_refcnt
== 0) {
284 if (rt_verbose
> 2) {
285 char dbuf
[MAX_IPv4_STR_LEN
], gbuf
[MAX_IPv4_STR_LEN
];
287 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
288 log(LOG_DEBUG
, "%s: route to %s->%s->%s validated, "
289 "flags=%b\n", __func__
, dbuf
, gbuf
,
290 (rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: "",
291 rt
->rt_flags
, RTF_BITS
);
295 * It's one of ours; unexpire it. If the timer is already
296 * scheduled, let it run later as it won't re-arm itself
297 * if there's nothing to do.
299 if (rt
->rt_flags
& RTPRF_OURS
) {
300 rt
->rt_flags
&= ~RTPRF_OURS
;
308 * Similar to in_matroute_args except without the leaf-matching parameters.
310 static struct radix_node
*
311 in_matroute(void *v_arg
, struct radix_node_head
*head
)
313 return (in_matroute_args(v_arg
, head
, NULL
, NULL
));
317 * This code is the inverse of in_clsroute: on first reference, if we
318 * were managing the route, stop doing so and set the expiration timer
321 static struct radix_node
*
322 in_matroute_args(void *v_arg
, struct radix_node_head
*head
,
323 rn_matchf_t
*f
, void *w
)
325 struct radix_node
*rn
= rn_match_args(v_arg
, head
, f
, w
);
328 RT_LOCK_SPIN((struct rtentry
*)rn
);
330 RT_UNLOCK((struct rtentry
*)rn
);
335 /* one hour is ``really old'' */
336 static uint32_t rtq_reallyold
= 60*60;
337 SYSCTL_UINT(_net_inet_ip
, IPCTL_RTEXPIRE
, rtexpire
,
338 CTLFLAG_RW
| CTLFLAG_LOCKED
, &rtq_reallyold
, 0,
339 "Default expiration time on dynamically learned routes");
341 /* never automatically crank down to less */
342 static uint32_t rtq_minreallyold
= 10;
343 SYSCTL_UINT(_net_inet_ip
, IPCTL_RTMINEXPIRE
, rtminexpire
,
344 CTLFLAG_RW
| CTLFLAG_LOCKED
, &rtq_minreallyold
, 0,
345 "Minimum time to attempt to hold onto dynamically learned routes");
347 /* 128 cached routes is ``too many'' */
348 static uint32_t rtq_toomany
= 128;
349 SYSCTL_UINT(_net_inet_ip
, IPCTL_RTMAXCACHE
, rtmaxcache
,
350 CTLFLAG_RW
| CTLFLAG_LOCKED
, &rtq_toomany
, 0,
351 "Upper limit on dynamically learned routes");
354 * On last reference drop, mark the route as belong to us so that it can be
358 in_clsroute(struct radix_node
*rn
, struct radix_node_head
*head
)
361 char dbuf
[MAX_IPv4_STR_LEN
], gbuf
[MAX_IPv4_STR_LEN
];
362 struct rtentry
*rt
= (struct rtentry
*)rn
;
363 boolean_t verbose
= (rt_verbose
> 1);
365 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
366 RT_LOCK_ASSERT_HELD(rt
);
368 if (!(rt
->rt_flags
& RTF_UP
))
369 return; /* prophylactic measures */
371 if ((rt
->rt_flags
& (RTF_LLINFO
| RTF_HOST
)) != RTF_HOST
)
374 if (rt
->rt_flags
& RTPRF_OURS
)
377 if (!(rt
->rt_flags
& (RTF_WASCLONED
| RTF_DYNAMIC
)))
381 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
384 * Delete the route immediately if RTF_DELCLONE is set or
385 * if route caching is disabled (rtq_reallyold set to 0).
386 * Otherwise, let it expire and be deleted by in_rtqkill().
388 if ((rt
->rt_flags
& RTF_DELCLONE
) || rtq_reallyold
== 0) {
392 log(LOG_DEBUG
, "%s: deleting route to %s->%s->%s, "
393 "flags=%b\n", __func__
, dbuf
, gbuf
,
394 (rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: "",
395 rt
->rt_flags
, RTF_BITS
);
398 * Delete the route from the radix tree but since we are
399 * called when the route's reference count is 0, don't
400 * deallocate it until we return from this routine by
401 * telling rtrequest that we're interested in it.
402 * Safe to drop rt_lock and use rt_key, rt_gateway since
403 * holding rnh_lock here prevents another thread from
404 * calling rt_setgate() on this route.
407 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
),
408 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, &rt
);
410 /* Now let the caller free it */
412 RT_REMREF_LOCKED(rt
);
416 rt_str(rt
, dbuf
, sizeof (dbuf
),
417 gbuf
, sizeof (gbuf
));
418 log(LOG_ERR
, "%s: error deleting route to "
419 "%s->%s->%s, flags=%b, err=%d\n", __func__
,
420 dbuf
, gbuf
, (rt
->rt_ifp
!= NULL
) ?
421 rt
->rt_ifp
->if_xname
: "", rt
->rt_flags
,
427 timenow
= net_uptime();
428 rt
->rt_flags
|= RTPRF_OURS
;
429 rt_setexpire(rt
, timenow
+ rtq_reallyold
);
432 log(LOG_DEBUG
, "%s: route to %s->%s->%s invalidated, "
433 "flags=%b, expire=T+%u\n", __func__
, dbuf
, gbuf
,
434 (rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: "",
435 rt
->rt_flags
, RTF_BITS
, rt
->rt_expire
- timenow
);
438 /* We have at least one entry; arm the timer if not already */
439 in_sched_rtqtimo(NULL
);
444 struct radix_node_head
*rnh
;
453 * Get rid of old routes. When draining, this deletes everything, even when
454 * the timeout is not expired yet. When updating, this makes sure that
455 * nothing has a timeout longer than the current value of rtq_reallyold.
458 in_rtqkill(struct radix_node
*rn
, void *rock
)
460 struct rtqk_arg
*ap
= rock
;
461 struct rtentry
*rt
= (struct rtentry
*)rn
;
462 boolean_t verbose
= (rt_verbose
> 1);
466 timenow
= net_uptime();
467 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
470 if (rt
->rt_flags
& RTPRF_OURS
) {
471 char dbuf
[MAX_IPv4_STR_LEN
], gbuf
[MAX_IPv4_STR_LEN
];
474 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
477 VERIFY(rt
->rt_expire
== 0 || rt
->rt_rmx
.rmx_expire
!= 0);
478 VERIFY(rt
->rt_expire
!= 0 || rt
->rt_rmx
.rmx_expire
== 0);
479 if (ap
->draining
|| rt
->rt_expire
<= timenow
) {
480 if (rt
->rt_refcnt
> 0) {
481 panic("%s: route %p marked with RTPRF_OURS "
482 "with non-zero refcnt (%u)", __func__
,
487 log(LOG_DEBUG
, "%s: deleting route to "
488 "%s->%s->%s, flags=%b, draining=%d\n",
489 __func__
, dbuf
, gbuf
, (rt
->rt_ifp
!= NULL
) ?
490 rt
->rt_ifp
->if_xname
: "", rt
->rt_flags
,
491 RTF_BITS
, ap
->draining
);
493 RT_ADDREF_LOCKED(rt
); /* for us to free below */
495 * Delete this route since we're done with it;
496 * the route may be freed afterwards, so we
497 * can no longer refer to 'rt' upon returning
498 * from rtrequest(). Safe to drop rt_lock and
499 * use rt_key, rt_gateway since holding rnh_lock
500 * here prevents another thread from calling
501 * rt_setgate() on this route.
504 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
),
505 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
509 rt_str(rt
, dbuf
, sizeof (dbuf
),
510 gbuf
, sizeof (gbuf
));
511 log(LOG_ERR
, "%s: error deleting route to "
512 "%s->%s->%s, flags=%b, err=%d\n", __func__
,
513 dbuf
, gbuf
, (rt
->rt_ifp
!= NULL
) ?
514 rt
->rt_ifp
->if_xname
: "", rt
->rt_flags
,
522 uint64_t expire
= (rt
->rt_expire
- timenow
);
524 if (ap
->updating
&& expire
> rtq_reallyold
) {
525 rt_setexpire(rt
, timenow
+ rtq_reallyold
);
527 log(LOG_DEBUG
, "%s: route to "
528 "%s->%s->%s, flags=%b, adjusted "
529 "expire=T+%u (was T+%u)\n",
530 __func__
, dbuf
, gbuf
,
531 (rt
->rt_ifp
!= NULL
) ?
532 rt
->rt_ifp
->if_xname
: "",
533 rt
->rt_flags
, RTF_BITS
,
534 (rt
->rt_expire
- timenow
), expire
);
537 ap
->nextstop
= lmin(ap
->nextstop
, rt
->rt_expire
);
547 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
548 static int rtq_timeout
= RTQ_TIMEOUT
;
551 in_rtqtimo(void *targ
)
554 struct radix_node_head
*rnh
;
557 static uint64_t last_adjusted_timeout
= 0;
558 boolean_t verbose
= (rt_verbose
> 1);
562 lck_mtx_lock(rnh_lock
);
563 rnh
= rt_tables
[AF_INET
];
566 /* Get the timestamp after we acquire the lock for better accuracy */
567 timenow
= net_uptime();
569 log(LOG_DEBUG
, "%s: initial nextstop is T+%u seconds\n",
570 __func__
, rtq_timeout
);
572 bzero(&arg
, sizeof (arg
));
574 arg
.nextstop
= timenow
+ rtq_timeout
;
575 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
577 log(LOG_DEBUG
, "%s: found %u, killed %u\n", __func__
,
578 arg
.found
, arg
.killed
);
581 * Attempt to be somewhat dynamic about this:
582 * If there are ``too many'' routes sitting around taking up space,
583 * then crank down the timeout, and see if we can't make some more
584 * go away. However, we make sure that we will never adjust more
585 * than once in rtq_timeout seconds, to keep from cranking down too
588 ours
= (arg
.found
- arg
.killed
);
589 if (ours
> rtq_toomany
&&
590 ((timenow
- last_adjusted_timeout
) >= (uint64_t)rtq_timeout
) &&
591 rtq_reallyold
> rtq_minreallyold
) {
592 rtq_reallyold
= 2 * rtq_reallyold
/ 3;
593 if (rtq_reallyold
< rtq_minreallyold
)
594 rtq_reallyold
= rtq_minreallyold
;
596 last_adjusted_timeout
= timenow
;
598 log(LOG_DEBUG
, "%s: adjusted rtq_reallyold to %d "
599 "seconds\n", __func__
, rtq_reallyold
);
601 arg
.found
= arg
.killed
= 0;
603 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
607 atv
.tv_sec
= arg
.nextstop
- timenow
;
608 /* re-arm the timer only if there's work to do */
611 in_sched_rtqtimo(&atv
);
613 log(LOG_DEBUG
, "%s: not rescheduling timer\n", __func__
);
614 lck_mtx_unlock(rnh_lock
);
618 in_sched_rtqtimo(struct timeval
*atv
)
620 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
622 if (!in_rtqtimo_run
) {
627 tv
.tv_sec
= MAX(rtq_timeout
/ 10, 1);
630 if (rt_verbose
> 1) {
631 log(LOG_DEBUG
, "%s: timer scheduled in "
632 "T+%llus.%lluu\n", __func__
,
633 (uint64_t)atv
->tv_sec
, (uint64_t)atv
->tv_usec
);
636 timeout(in_rtqtimo
, NULL
, tvtohz(atv
));
643 struct radix_node_head
*rnh
;
647 log(LOG_DEBUG
, "%s: draining routes\n", __func__
);
649 lck_mtx_lock(rnh_lock
);
650 rnh
= rt_tables
[AF_INET
];
652 bzero(&arg
, sizeof (arg
));
655 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
656 lck_mtx_unlock(rnh_lock
);
660 * Initialize our routing tree.
663 in_inithead(void **head
, int off
)
665 struct radix_node_head
*rnh
;
667 /* If called from route_init(), make sure it is exactly once */
668 VERIFY(head
!= (void **)&rt_tables
[AF_INET
] || *head
== NULL
);
670 if (!rn_inithead(head
, off
))
674 * We can get here from nfs_subs.c as well, in which case this
675 * won't be for the real routing table and thus we're done;
676 * this also takes care of the case when we're called more than
677 * once from anywhere but route_init().
679 if (head
!= (void **)&rt_tables
[AF_INET
])
680 return (1); /* only do this for the real routing table */
683 rnh
->rnh_addaddr
= in_addroute
;
684 rnh
->rnh_deladdr
= in_deleteroute
;
685 rnh
->rnh_matchaddr
= in_matroute
;
686 rnh
->rnh_matchaddr_args
= in_matroute_args
;
687 rnh
->rnh_close
= in_clsroute
;
692 * This zaps old routes when the interface goes down or interface
693 * address is deleted. In the latter case, it deletes static routes
694 * that point to this address. If we don't do this, we may end up
695 * using the old address in the future. The ones we always want to
696 * get rid of are things like ARP entries, since the user might down
697 * the interface, walk over to a completely different network, and
700 struct in_ifadown_arg
{
701 struct radix_node_head
*rnh
;
707 in_ifadownkill(struct radix_node
*rn
, void *xap
)
709 char dbuf
[MAX_IPv4_STR_LEN
], gbuf
[MAX_IPv4_STR_LEN
];
710 struct in_ifadown_arg
*ap
= xap
;
711 struct rtentry
*rt
= (struct rtentry
*)rn
;
712 boolean_t verbose
= (rt_verbose
!= 0);
715 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
718 if (rt
->rt_ifa
== ap
->ifa
&&
719 (ap
->del
|| !(rt
->rt_flags
& RTF_STATIC
))) {
720 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
722 log(LOG_DEBUG
, "%s: deleting route to %s->%s->%s, "
723 "flags=%b\n", __func__
, dbuf
, gbuf
,
724 (rt
->rt_ifp
!= NULL
) ? rt
->rt_ifp
->if_xname
: "",
725 rt
->rt_flags
, RTF_BITS
);
727 RT_ADDREF_LOCKED(rt
); /* for us to free below */
729 * We need to disable the automatic prune that happens
730 * in this case in rtrequest() because it will blow
731 * away the pointers that rn_walktree() needs in order
732 * continue our descent. We will end up deleting all
733 * the routes that rtrequest() would have in any case,
734 * so that behavior is not needed there. Safe to drop
735 * rt_lock and use rt_key, rt_gateway, since holding
736 * rnh_lock here prevents another thread from calling
737 * rt_setgate() on this route.
739 rt
->rt_flags
&= ~(RTF_CLONING
| RTF_PRCLONING
);
741 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
),
742 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
746 rt_str(rt
, dbuf
, sizeof (dbuf
),
747 gbuf
, sizeof (gbuf
));
748 log(LOG_ERR
, "%s: error deleting route to "
749 "%s->%s->%s, flags=%b, err=%d\n", __func__
,
750 dbuf
, gbuf
, (rt
->rt_ifp
!= NULL
) ?
751 rt
->rt_ifp
->if_xname
: "", rt
->rt_flags
,
763 in_ifadown(struct ifaddr
*ifa
, int delete)
765 struct in_ifadown_arg arg
;
766 struct radix_node_head
*rnh
;
768 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
771 * Holding rnh_lock here prevents the possibility of
772 * ifa from changing (e.g. in_ifinit), so it is safe
773 * to access its ifa_addr without locking.
775 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
778 /* trigger route cache reevaluation */
779 routegenid_inet_update();
781 arg
.rnh
= rnh
= rt_tables
[AF_INET
];
784 rnh
->rnh_walktree(rnh
, in_ifadownkill
, &arg
);
786 ifa
->ifa_flags
&= ~IFA_ROUTE
;