]>
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 *);
107 * Do what we need to do when inserting a route.
109 static struct radix_node
*
110 in_addroute(void *v_arg
, void *n_arg
, struct radix_node_head
*head
,
111 struct radix_node
*treenodes
)
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);
120 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
121 RT_LOCK_ASSERT_HELD(rt
);
124 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
127 * For IP, all unicast non-host routes are automatically cloning.
129 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
130 rt
->rt_flags
|= RTF_MULTICAST
;
132 if (!(rt
->rt_flags
& (RTF_HOST
| RTF_CLONING
| RTF_MULTICAST
)))
133 rt
->rt_flags
|= RTF_PRCLONING
;
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).
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().
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
151 if (rt
->rt_flags
& RTF_HOST
) {
152 if (in_broadcast(sin
->sin_addr
, rt
->rt_ifp
)) {
153 rt
->rt_flags
|= RTF_BROADCAST
;
155 /* Become a regular mutex */
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
);
165 if (!rt
->rt_rmx
.rmx_mtu
&& !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) &&
167 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
169 ret
= rn_addroute(v_arg
, n_arg
, head
, treenodes
);
170 if (ret
== NULL
&& (rt
->rt_flags
& RTF_HOST
)) {
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.
177 rt2
= rtalloc1_scoped_locked(rt_key(rt
), 0,
178 RTF_CLONING
| RTF_PRCLONING
, sin_get_ifscope(rt_key(rt
)));
180 char dbufc
[MAX_IPv4_STR_LEN
];
184 rt_str(rt2
, dbufc
, sizeof (dbufc
), NULL
, 0);
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
) {
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",
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
);
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.
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
,
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
);
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
);
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
);
248 static struct radix_node
*
249 in_deleteroute(void *v_arg
, void *netmask_arg
, struct radix_node_head
*head
)
251 struct radix_node
*rn
;
253 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
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
;
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
);
271 * Validate (unexpire) an expiring AF_INET route.
274 in_validate(struct radix_node
*rn
)
276 struct rtentry
*rt
= (struct rtentry
*)rn
;
278 RT_LOCK_ASSERT_HELD(rt
);
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
];
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
);
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.
297 if (rt
->rt_flags
& RTPRF_OURS
) {
298 rt
->rt_flags
&= ~RTPRF_OURS
;
306 * Similar to in_matroute_args except without the leaf-matching parameters.
308 static struct radix_node
*
309 in_matroute(void *v_arg
, struct radix_node_head
*head
)
311 return (in_matroute_args(v_arg
, head
, NULL
, NULL
));
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
319 static struct radix_node
*
320 in_matroute_args(void *v_arg
, struct radix_node_head
*head
,
321 rn_matchf_t
*f
, void *w
)
323 struct radix_node
*rn
= rn_match_args(v_arg
, head
, f
, w
);
326 RT_LOCK_SPIN((struct rtentry
*)rn
);
328 RT_UNLOCK((struct rtentry
*)rn
);
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");
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");
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");
352 * On last reference drop, mark the route as belong to us so that it can be
356 in_clsroute(struct radix_node
*rn
, struct radix_node_head
*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);
363 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
364 RT_LOCK_ASSERT_HELD(rt
);
366 if (!(rt
->rt_flags
& RTF_UP
))
367 return; /* prophylactic measures */
369 if ((rt
->rt_flags
& (RTF_LLINFO
| RTF_HOST
)) != RTF_HOST
)
372 if (rt
->rt_flags
& RTPRF_OURS
)
375 if (!(rt
->rt_flags
& (RTF_WASCLONED
| RTF_DYNAMIC
)))
379 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
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().
386 if ((rt
->rt_flags
& RTF_DELCLONE
) || rtq_reallyold
== 0) {
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
);
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.
405 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
),
406 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, &rt
);
408 /* Now let the caller free it */
410 RT_REMREF_LOCKED(rt
);
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
,
425 timenow
= net_uptime();
426 rt
->rt_flags
|= RTPRF_OURS
;
427 rt_setexpire(rt
, timenow
+ rtq_reallyold
);
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
);
436 /* We have at least one entry; arm the timer if not already */
437 in_sched_rtqtimo(NULL
);
442 struct radix_node_head
*rnh
;
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.
456 in_rtqkill(struct radix_node
*rn
, void *rock
)
458 struct rtqk_arg
*ap
= rock
;
459 struct rtentry
*rt
= (struct rtentry
*)rn
;
460 boolean_t verbose
= (rt_verbose
> 1);
464 timenow
= net_uptime();
465 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
468 if (rt
->rt_flags
& RTPRF_OURS
) {
469 char dbuf
[MAX_IPv4_STR_LEN
], gbuf
[MAX_IPv4_STR_LEN
];
472 rt_str(rt
, dbuf
, sizeof (dbuf
), gbuf
, sizeof (gbuf
));
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__
,
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
);
492 RT_ADDREF_LOCKED(rt
); /* for us to free below */
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.
503 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
),
504 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
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
,
521 uint64_t expire
= (rt
->rt_expire
- timenow
);
523 if (ap
->updating
&& expire
> rtq_reallyold
) {
524 rt_setexpire(rt
, timenow
+ rtq_reallyold
);
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
);
536 ap
->nextstop
= lmin(ap
->nextstop
, rt
->rt_expire
);
546 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
547 static int rtq_timeout
= RTQ_TIMEOUT
;
550 in_rtqtimo(void *targ
)
553 struct radix_node_head
*rnh
;
556 static uint64_t last_adjusted_timeout
= 0;
557 boolean_t verbose
= (rt_verbose
> 1);
561 lck_mtx_lock(rnh_lock
);
562 rnh
= rt_tables
[AF_INET
];
565 /* Get the timestamp after we acquire the lock for better accuracy */
566 timenow
= net_uptime();
568 log(LOG_DEBUG
, "%s: initial nextstop is T+%u seconds\n",
569 __func__
, rtq_timeout
);
571 bzero(&arg
, sizeof (arg
));
573 arg
.nextstop
= timenow
+ rtq_timeout
;
574 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
576 log(LOG_DEBUG
, "%s: found %u, killed %u\n", __func__
,
577 arg
.found
, arg
.killed
);
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
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
;
595 last_adjusted_timeout
= timenow
;
597 log(LOG_DEBUG
, "%s: adjusted rtq_reallyold to %d "
598 "seconds\n", __func__
, rtq_reallyold
);
600 arg
.found
= arg
.killed
= 0;
602 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
606 atv
.tv_sec
= arg
.nextstop
- timenow
;
607 /* re-arm the timer only if there's work to do */
610 in_sched_rtqtimo(&atv
);
612 log(LOG_DEBUG
, "%s: not rescheduling timer\n", __func__
);
613 lck_mtx_unlock(rnh_lock
);
617 in_sched_rtqtimo(struct timeval
*atv
)
619 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
621 if (!in_rtqtimo_run
) {
626 tv
.tv_sec
= MAX(rtq_timeout
/ 10, 1);
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
);
635 timeout(in_rtqtimo
, NULL
, tvtohz(atv
));
642 struct radix_node_head
*rnh
;
646 log(LOG_DEBUG
, "%s: draining routes\n", __func__
);
648 lck_mtx_lock(rnh_lock
);
649 rnh
= rt_tables
[AF_INET
];
651 bzero(&arg
, sizeof (arg
));
654 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
655 lck_mtx_unlock(rnh_lock
);
659 * Initialize our routing tree.
662 in_inithead(void **head
, int off
)
664 struct radix_node_head
*rnh
;
666 /* If called from route_init(), make sure it is exactly once */
667 VERIFY(head
!= (void **)&rt_tables
[AF_INET
] || *head
== NULL
);
669 if (!rn_inithead(head
, off
))
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().
678 if (head
!= (void **)&rt_tables
[AF_INET
])
679 return (1); /* only do this for the real routing table */
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
;
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
699 struct in_ifadown_arg
{
700 struct radix_node_head
*rnh
;
706 in_ifadownkill(struct radix_node
*rn
, void *xap
)
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);
714 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
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
));
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
);
726 RT_ADDREF_LOCKED(rt
); /* for us to free below */
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.
738 rt
->rt_flags
&= ~(RTF_CLONING
| RTF_PRCLONING
);
740 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
),
741 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
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
,
762 in_ifadown(struct ifaddr
*ifa
, int delete)
764 struct in_ifadown_arg arg
;
765 struct radix_node_head
*rnh
;
767 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
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.
774 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
777 /* trigger route cache reevaluation */
778 routegenid_inet_update();
780 arg
.rnh
= rnh
= rt_tables
[AF_INET
];
783 rnh
->rnh_walktree(rnh
, in_ifadownkill
, &arg
);
785 ifa
->ifa_flags
&= ~IFA_ROUTE
;