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