Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39037602 | 2 | * Copyright (c) 2000-2016 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> |
1c79356b | 87 | |
2d21ac55 | 88 | extern int tvtohz(struct timeval *); |
1c79356b | 89 | |
39236c6e A |
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 *); | |
9bccf70c | 93 | |
39236c6e A |
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 *); | |
c910b4d9 A |
99 | static struct radix_node *in_matroute_args(void *, struct radix_node_head *, |
100 | rn_matchf_t *f, void *); | |
39236c6e A |
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 *); | |
c910b4d9 | 105 | |
1c79356b A |
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, | |
39236c6e | 111 | struct radix_node *treenodes) |
1c79356b A |
112 | { |
113 | struct rtentry *rt = (struct rtentry *)treenodes; | |
316670eb | 114 | struct sockaddr_in *sin = (struct sockaddr_in *)(void *)rt_key(rt); |
1c79356b | 115 | struct radix_node *ret; |
39236c6e A |
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); | |
1c79356b | 119 | |
5ba3f43e | 120 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
b0d623f7 A |
121 | RT_LOCK_ASSERT_HELD(rt); |
122 | ||
39236c6e A |
123 | if (verbose) |
124 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
125 | ||
1c79356b A |
126 | /* |
127 | * For IP, all unicast non-host routes are automatically cloning. | |
128 | */ | |
b0d623f7 | 129 | if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) |
1c79356b A |
130 | rt->rt_flags |= RTF_MULTICAST; |
131 | ||
39236c6e | 132 | if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) |
1c79356b | 133 | rt->rt_flags |= RTF_PRCLONING; |
1c79356b A |
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 { | |
6d2010ae A |
155 | /* Become a regular mutex */ |
156 | RT_CONVERT_LOCK(rt); | |
157 | IFA_LOCK_SPIN(rt->rt_ifa); | |
39236c6e A |
158 | if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr == |
159 | sin->sin_addr.s_addr) | |
1c79356b | 160 | rt->rt_flags |= RTF_LOCAL; |
6d2010ae | 161 | IFA_UNLOCK(rt->rt_ifa); |
1c79356b A |
162 | } |
163 | } | |
164 | ||
39236c6e A |
165 | if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) && |
166 | rt->rt_ifp) | |
1c79356b A |
167 | rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; |
168 | ||
169 | ret = rn_addroute(v_arg, n_arg, head, treenodes); | |
39236c6e | 170 | if (ret == NULL && (rt->rt_flags & RTF_HOST)) { |
1c79356b A |
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 | */ | |
c910b4d9 | 177 | rt2 = rtalloc1_scoped_locked(rt_key(rt), 0, |
6d2010ae | 178 | RTF_CLONING | RTF_PRCLONING, sin_get_ifscope(rt_key(rt))); |
39236c6e A |
179 | if (rt2 != NULL) { |
180 | char dbufc[MAX_IPv4_STR_LEN]; | |
181 | ||
b0d623f7 | 182 | RT_LOCK(rt2); |
39236c6e A |
183 | if (verbose) |
184 | rt_str(rt2, dbufc, sizeof (dbufc), NULL, 0); | |
185 | ||
b0d623f7 A |
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) { | |
39236c6e A |
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 | } | |
b0d623f7 A |
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); | |
39236c6e | 210 | (void) rtrequest_locked(RTM_DELETE, rt_key(rt2), |
b0d623f7 | 211 | rt2->rt_gateway, rt_mask(rt2), |
39236c6e | 212 | rt2->rt_flags, NULL); |
1c79356b | 213 | ret = rn_addroute(v_arg, n_arg, head, |
39236c6e | 214 | treenodes); |
b0d623f7 A |
215 | } else { |
216 | RT_UNLOCK(rt2); | |
1c79356b | 217 | } |
91447636 | 218 | rtfree_locked(rt2); |
1c79356b A |
219 | } |
220 | } | |
39236c6e A |
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 | ||
5ba3f43e | 253 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
39236c6e A |
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); | |
1c79356b A |
268 | } |
269 | ||
c910b4d9 A |
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 | ||
b0d623f7 A |
278 | RT_LOCK_ASSERT_HELD(rt); |
279 | ||
c910b4d9 | 280 | /* This is first reference? */ |
6d2010ae | 281 | if (rt->rt_refcnt == 0) { |
39236c6e A |
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 | */ | |
6d2010ae | 297 | if (rt->rt_flags & RTPRF_OURS) { |
6d2010ae A |
298 | rt->rt_flags &= ~RTPRF_OURS; |
299 | rt_setexpire(rt, 0); | |
6d2010ae | 300 | } |
c910b4d9 A |
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 | ||
1c79356b A |
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 * | |
c910b4d9 A |
320 | in_matroute_args(void *v_arg, struct radix_node_head *head, |
321 | rn_matchf_t *f, void *w) | |
1c79356b | 322 | { |
c910b4d9 | 323 | struct radix_node *rn = rn_match_args(v_arg, head, f, w); |
1c79356b | 324 | |
b0d623f7 A |
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); | |
1c79356b A |
331 | } |
332 | ||
39236c6e A |
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"); | |
9bccf70c | 338 | |
39236c6e A |
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"); | |
1c79356b A |
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 | |
39236c6e | 356 | in_clsroute(struct radix_node *rn, struct radix_node_head *head) |
1c79356b | 357 | { |
39236c6e A |
358 | #pragma unused(head) |
359 | char dbuf[MAX_IPv4_STR_LEN], gbuf[MAX_IPv4_STR_LEN]; | |
1c79356b | 360 | struct rtentry *rt = (struct rtentry *)rn; |
39236c6e | 361 | boolean_t verbose = (rt_verbose > 1); |
1c79356b | 362 | |
5ba3f43e | 363 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
b0d623f7 A |
364 | RT_LOCK_ASSERT_HELD(rt); |
365 | ||
2d21ac55 | 366 | if (!(rt->rt_flags & RTF_UP)) |
5ba3f43e | 367 | return; /* prophylactic measures */ |
1c79356b | 368 | |
2d21ac55 | 369 | if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) |
1c79356b A |
370 | return; |
371 | ||
39236c6e A |
372 | if (rt->rt_flags & RTPRF_OURS) |
373 | return; | |
374 | ||
375 | if (!(rt->rt_flags & (RTF_WASCLONED | RTF_DYNAMIC))) | |
1c79356b A |
376 | return; |
377 | ||
39236c6e A |
378 | if (verbose) |
379 | rt_str(rt, dbuf, sizeof (dbuf), gbuf, sizeof (gbuf)); | |
380 | ||
1c79356b | 381 | /* |
2d21ac55 A |
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(). | |
1c79356b | 385 | */ |
2d21ac55 | 386 | if ((rt->rt_flags & RTF_DELCLONE) || rtq_reallyold == 0) { |
39236c6e A |
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 | } | |
2d21ac55 A |
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. | |
b0d623f7 A |
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. | |
2d21ac55 | 403 | */ |
b0d623f7 | 404 | RT_UNLOCK(rt); |
39236c6e A |
405 | err = rtrequest_locked(RTM_DELETE, rt_key(rt), |
406 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, &rt); | |
407 | if (err == 0) { | |
2d21ac55 | 408 | /* Now let the caller free it */ |
b0d623f7 A |
409 | RT_LOCK(rt); |
410 | RT_REMREF_LOCKED(rt); | |
411 | } else { | |
412 | RT_LOCK(rt); | |
39236c6e A |
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); | |
2d21ac55 A |
421 | } |
422 | } else { | |
6d2010ae | 423 | uint64_t timenow; |
2d21ac55 | 424 | |
6d2010ae | 425 | timenow = net_uptime(); |
1c79356b | 426 | rt->rt_flags |= RTPRF_OURS; |
39236c6e A |
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); | |
1c79356b A |
438 | } |
439 | } | |
440 | ||
441 | struct rtqk_arg { | |
442 | struct radix_node_head *rnh; | |
1c79356b | 443 | int updating; |
39236c6e A |
444 | int draining; |
445 | uint32_t killed; | |
446 | uint32_t found; | |
6d2010ae | 447 | uint64_t nextstop; |
1c79356b A |
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; | |
39236c6e | 460 | boolean_t verbose = (rt_verbose > 1); |
6d2010ae | 461 | uint64_t timenow; |
39236c6e | 462 | int err; |
1c79356b | 463 | |
6d2010ae | 464 | timenow = net_uptime(); |
5ba3f43e | 465 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 466 | |
b0d623f7 | 467 | RT_LOCK(rt); |
2d21ac55 | 468 | if (rt->rt_flags & RTPRF_OURS) { |
39236c6e A |
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)); | |
1c79356b | 473 | |
39236c6e | 474 | ap->found++; |
6d2010ae A |
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) { | |
39236c6e A |
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 | } | |
5ba3f43e | 484 | |
39236c6e A |
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 */ | |
b0d623f7 A |
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), | |
39236c6e A |
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); | |
1c79356b A |
516 | } else { |
517 | ap->killed++; | |
518 | } | |
39236c6e | 519 | rtfree_locked(rt); |
1c79356b | 520 | } else { |
39236c6e A |
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 | } | |
1c79356b | 535 | } |
39236c6e | 536 | ap->nextstop = lmin(ap->nextstop, rt->rt_expire); |
b0d623f7 | 537 | RT_UNLOCK(rt); |
1c79356b | 538 | } |
b0d623f7 A |
539 | } else { |
540 | RT_UNLOCK(rt); | |
1c79356b A |
541 | } |
542 | ||
39236c6e | 543 | return (0); |
1c79356b A |
544 | } |
545 | ||
39236c6e | 546 | #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ |
1c79356b A |
547 | static int rtq_timeout = RTQ_TIMEOUT; |
548 | ||
549 | static void | |
39236c6e | 550 | in_rtqtimo(void *targ) |
1c79356b | 551 | { |
39236c6e A |
552 | #pragma unused(targ) |
553 | struct radix_node_head *rnh; | |
1c79356b A |
554 | struct rtqk_arg arg; |
555 | struct timeval atv; | |
6d2010ae | 556 | static uint64_t last_adjusted_timeout = 0; |
39236c6e | 557 | boolean_t verbose = (rt_verbose > 1); |
6d2010ae | 558 | uint64_t timenow; |
39236c6e | 559 | uint32_t ours; |
9bccf70c | 560 | |
b0d623f7 | 561 | lck_mtx_lock(rnh_lock); |
39236c6e A |
562 | rnh = rt_tables[AF_INET]; |
563 | VERIFY(rnh != NULL); | |
2d21ac55 | 564 | |
39236c6e A |
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)); | |
1c79356b | 572 | arg.rnh = rnh; |
6d2010ae | 573 | arg.nextstop = timenow + rtq_timeout; |
1c79356b | 574 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); |
39236c6e A |
575 | if (verbose) { |
576 | log(LOG_DEBUG, "%s: found %u, killed %u\n", __func__, | |
577 | arg.found, arg.killed); | |
578 | } | |
1c79356b A |
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 | */ | |
39236c6e A |
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) | |
1c79356b | 593 | rtq_reallyold = rtq_minreallyold; |
1c79356b | 594 | |
6d2010ae | 595 | last_adjusted_timeout = timenow; |
39236c6e A |
596 | if (verbose) { |
597 | log(LOG_DEBUG, "%s: adjusted rtq_reallyold to %d " | |
598 | "seconds\n", __func__, rtq_reallyold); | |
599 | } | |
1c79356b A |
600 | arg.found = arg.killed = 0; |
601 | arg.updating = 1; | |
1c79356b | 602 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); |
1c79356b A |
603 | } |
604 | ||
605 | atv.tv_usec = 0; | |
6d2010ae | 606 | atv.tv_sec = arg.nextstop - timenow; |
39236c6e A |
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__); | |
b0d623f7 | 613 | lck_mtx_unlock(rnh_lock); |
39236c6e A |
614 | } |
615 | ||
616 | static void | |
617 | in_sched_rtqtimo(struct timeval *atv) | |
618 | { | |
5ba3f43e | 619 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
39236c6e A |
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 | } | |
1c79356b A |
637 | } |
638 | ||
639 | void | |
640 | in_rtqdrain(void) | |
641 | { | |
39236c6e | 642 | struct radix_node_head *rnh; |
1c79356b | 643 | struct rtqk_arg arg; |
39236c6e A |
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)); | |
1c79356b | 652 | arg.rnh = rnh; |
1c79356b | 653 | arg.draining = 1; |
1c79356b | 654 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); |
b0d623f7 | 655 | lck_mtx_unlock(rnh_lock); |
1c79356b A |
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; | |
9bccf70c | 665 | |
39236c6e A |
666 | /* If called from route_init(), make sure it is exactly once */ |
667 | VERIFY(head != (void **)&rt_tables[AF_INET] || *head == NULL); | |
1c79356b | 668 | |
39236c6e A |
669 | if (!rn_inithead(head, off)) |
670 | return (0); | |
1c79356b | 671 | |
39236c6e A |
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 */ | |
1c79356b A |
680 | |
681 | rnh = *head; | |
682 | rnh->rnh_addaddr = in_addroute; | |
39236c6e | 683 | rnh->rnh_deladdr = in_deleteroute; |
1c79356b | 684 | rnh->rnh_matchaddr = in_matroute; |
c910b4d9 | 685 | rnh->rnh_matchaddr_args = in_matroute_args; |
1c79356b | 686 | rnh->rnh_close = in_clsroute; |
39236c6e | 687 | return (1); |
1c79356b A |
688 | } |
689 | ||
1c79356b | 690 | /* |
9bccf70c A |
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. | |
1c79356b A |
698 | */ |
699 | struct in_ifadown_arg { | |
700 | struct radix_node_head *rnh; | |
701 | struct ifaddr *ifa; | |
9bccf70c | 702 | int del; |
1c79356b A |
703 | }; |
704 | ||
705 | static int | |
706 | in_ifadownkill(struct radix_node *rn, void *xap) | |
707 | { | |
39236c6e | 708 | char dbuf[MAX_IPv4_STR_LEN], gbuf[MAX_IPv4_STR_LEN]; |
1c79356b A |
709 | struct in_ifadown_arg *ap = xap; |
710 | struct rtentry *rt = (struct rtentry *)rn; | |
39236c6e | 711 | boolean_t verbose = (rt_verbose != 0); |
1c79356b A |
712 | int err; |
713 | ||
5ba3f43e | 714 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
39236c6e | 715 | |
b0d623f7 | 716 | RT_LOCK(rt); |
9bccf70c A |
717 | if (rt->rt_ifa == ap->ifa && |
718 | (ap->del || !(rt->rt_flags & RTF_STATIC))) { | |
39236c6e A |
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 */ | |
1c79356b A |
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, | |
b0d623f7 A |
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. | |
1c79356b | 737 | */ |
9bccf70c | 738 | rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING); |
b0d623f7 A |
739 | RT_UNLOCK(rt); |
740 | err = rtrequest_locked(RTM_DELETE, rt_key(rt), | |
39236c6e A |
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); | |
1c79356b | 753 | } |
39236c6e | 754 | rtfree_locked(rt); |
b0d623f7 A |
755 | } else { |
756 | RT_UNLOCK(rt); | |
1c79356b | 757 | } |
39236c6e | 758 | return (0); |
1c79356b A |
759 | } |
760 | ||
761 | int | |
9bccf70c | 762 | in_ifadown(struct ifaddr *ifa, int delete) |
1c79356b A |
763 | { |
764 | struct in_ifadown_arg arg; | |
765 | struct radix_node_head *rnh; | |
766 | ||
5ba3f43e | 767 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED); |
91447636 | 768 | |
6d2010ae A |
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 | */ | |
1c79356b | 774 | if (ifa->ifa_addr->sa_family != AF_INET) |
6d2010ae | 775 | return (1); |
1c79356b | 776 | |
2d21ac55 | 777 | /* trigger route cache reevaluation */ |
39236c6e | 778 | routegenid_inet_update(); |
2d21ac55 | 779 | |
1c79356b A |
780 | arg.rnh = rnh = rt_tables[AF_INET]; |
781 | arg.ifa = ifa; | |
9bccf70c | 782 | arg.del = delete; |
1c79356b | 783 | rnh->rnh_walktree(rnh, in_ifadownkill, &arg); |
6d2010ae | 784 | IFA_LOCK_SPIN(ifa); |
1c79356b | 785 | ifa->ifa_flags &= ~IFA_ROUTE; |
6d2010ae A |
786 | IFA_UNLOCK(ifa); |
787 | return (0); | |
1c79356b | 788 | } |