]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2011 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
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. | |
8f6c56a5 | 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 | * | |
9bccf70c | 56 | * $FreeBSD: src/sys/netinet/in_rmx.c,v 1.37.2.1 2001/05/14 08:23:49 ru Exp $ |
1c79356b A |
57 | */ |
58 | ||
59 | /* | |
60 | * This code does two things necessary for the enhanced TCP metrics to | |
61 | * function in a useful manner: | |
62 | * 1) It marks all non-host routes as `cloning', thus ensuring that | |
63 | * every actual reference to such a route actually gets turned | |
64 | * into a reference to a host route to the specific destination | |
65 | * requested. | |
66 | * 2) When such routes lose all their references, it arranges for them | |
67 | * to be deleted in some random collection of circumstances, so that | |
68 | * a large quantity of stale routing data is not kept in kernel memory | |
69 | * indefinitely. See in_rtqtimo() below for the exact mechanism. | |
70 | */ | |
71 | ||
72 | #include <sys/param.h> | |
73 | #include <sys/systm.h> | |
74 | #include <sys/kernel.h> | |
75 | #include <sys/sysctl.h> | |
76 | #include <sys/socket.h> | |
77 | #include <sys/mbuf.h> | |
6d2010ae | 78 | #include <sys/protosw.h> |
1c79356b | 79 | #include <sys/syslog.h> |
6d2010ae | 80 | #include <sys/mcache.h> |
91447636 | 81 | #include <kern/lock.h> |
1c79356b A |
82 | |
83 | #include <net/if.h> | |
84 | #include <net/route.h> | |
85 | #include <netinet/in.h> | |
86 | #include <netinet/in_var.h> | |
6d2010ae | 87 | #include <netinet/in_arp.h> |
1c79356b | 88 | |
2d21ac55 | 89 | extern int tvtohz(struct timeval *); |
91447636 | 90 | extern int in_inithead(void **head, int off); |
1c79356b | 91 | |
9bccf70c A |
92 | #ifdef __APPLE__ |
93 | static void in_rtqtimo(void *rock); | |
94 | #endif | |
95 | ||
c910b4d9 A |
96 | static struct radix_node *in_matroute_args(void *, struct radix_node_head *, |
97 | rn_matchf_t *f, void *); | |
98 | ||
1c79356b A |
99 | #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */ |
100 | ||
101 | /* | |
102 | * Do what we need to do when inserting a route. | |
103 | */ | |
104 | static struct radix_node * | |
105 | in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, | |
106 | struct radix_node *treenodes) | |
107 | { | |
108 | struct rtentry *rt = (struct rtentry *)treenodes; | |
109 | struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt); | |
110 | struct radix_node *ret; | |
111 | ||
b0d623f7 A |
112 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); |
113 | RT_LOCK_ASSERT_HELD(rt); | |
114 | ||
1c79356b A |
115 | /* |
116 | * For IP, all unicast non-host routes are automatically cloning. | |
117 | */ | |
b0d623f7 | 118 | if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) |
1c79356b A |
119 | rt->rt_flags |= RTF_MULTICAST; |
120 | ||
b0d623f7 | 121 | if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) { |
1c79356b A |
122 | rt->rt_flags |= RTF_PRCLONING; |
123 | } | |
124 | ||
125 | /* | |
126 | * A little bit of help for both IP output and input: | |
127 | * For host routes, we make sure that RTF_BROADCAST | |
128 | * is set for anything that looks like a broadcast address. | |
129 | * This way, we can avoid an expensive call to in_broadcast() | |
130 | * in ip_output() most of the time (because the route passed | |
131 | * to ip_output() is almost always a host route). | |
132 | * | |
133 | * We also do the same for local addresses, with the thought | |
134 | * that this might one day be used to speed up ip_input(). | |
135 | * | |
136 | * We also mark routes to multicast addresses as such, because | |
137 | * it's easy to do and might be useful (but this is much more | |
138 | * dubious since it's so easy to inspect the address). (This | |
139 | * is done above.) | |
140 | */ | |
141 | if (rt->rt_flags & RTF_HOST) { | |
142 | if (in_broadcast(sin->sin_addr, rt->rt_ifp)) { | |
143 | rt->rt_flags |= RTF_BROADCAST; | |
144 | } else { | |
6d2010ae A |
145 | /* Become a regular mutex */ |
146 | RT_CONVERT_LOCK(rt); | |
147 | IFA_LOCK_SPIN(rt->rt_ifa); | |
1c79356b A |
148 | #define satosin(sa) ((struct sockaddr_in *)sa) |
149 | if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr | |
150 | == sin->sin_addr.s_addr) | |
151 | rt->rt_flags |= RTF_LOCAL; | |
152 | #undef satosin | |
6d2010ae | 153 | IFA_UNLOCK(rt->rt_ifa); |
1c79356b A |
154 | } |
155 | } | |
156 | ||
157 | if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) | |
158 | && rt->rt_ifp) | |
159 | rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; | |
160 | ||
161 | ret = rn_addroute(v_arg, n_arg, head, treenodes); | |
162 | if (ret == NULL && rt->rt_flags & RTF_HOST) { | |
163 | struct rtentry *rt2; | |
164 | /* | |
165 | * We are trying to add a host route, but can't. | |
166 | * Find out if it is because of an | |
167 | * ARP entry and delete it if so. | |
168 | */ | |
c910b4d9 | 169 | rt2 = rtalloc1_scoped_locked(rt_key(rt), 0, |
6d2010ae | 170 | RTF_CLONING | RTF_PRCLONING, sin_get_ifscope(rt_key(rt))); |
1c79356b | 171 | if (rt2) { |
b0d623f7 A |
172 | RT_LOCK(rt2); |
173 | if ((rt2->rt_flags & RTF_LLINFO) && | |
174 | (rt2->rt_flags & RTF_HOST) && | |
175 | rt2->rt_gateway != NULL && | |
176 | rt2->rt_gateway->sa_family == AF_LINK) { | |
177 | /* | |
178 | * Safe to drop rt_lock and use rt_key, | |
179 | * rt_gateway, since holding rnh_lock here | |
180 | * prevents another thread from calling | |
181 | * rt_setgate() on this route. | |
182 | */ | |
183 | RT_UNLOCK(rt2); | |
184 | rtrequest_locked(RTM_DELETE, rt_key(rt2), | |
185 | rt2->rt_gateway, rt_mask(rt2), | |
186 | rt2->rt_flags, 0); | |
1c79356b A |
187 | ret = rn_addroute(v_arg, n_arg, head, |
188 | treenodes); | |
b0d623f7 A |
189 | } else { |
190 | RT_UNLOCK(rt2); | |
1c79356b | 191 | } |
91447636 | 192 | rtfree_locked(rt2); |
1c79356b A |
193 | } |
194 | } | |
195 | return ret; | |
196 | } | |
197 | ||
c910b4d9 A |
198 | /* |
199 | * Validate (unexpire) an expiring AF_INET route. | |
200 | */ | |
201 | struct radix_node * | |
202 | in_validate(struct radix_node *rn) | |
203 | { | |
204 | struct rtentry *rt = (struct rtentry *)rn; | |
205 | ||
b0d623f7 A |
206 | RT_LOCK_ASSERT_HELD(rt); |
207 | ||
c910b4d9 | 208 | /* This is first reference? */ |
6d2010ae A |
209 | if (rt->rt_refcnt == 0) { |
210 | if (rt->rt_flags & RTPRF_OURS) { | |
211 | /* It's one of ours; unexpire it */ | |
212 | rt->rt_flags &= ~RTPRF_OURS; | |
213 | rt_setexpire(rt, 0); | |
214 | } else if ((rt->rt_flags & RTF_LLINFO) && | |
215 | (rt->rt_flags & RTF_HOST) && rt->rt_gateway != NULL && | |
216 | rt->rt_gateway->sa_family == AF_LINK) { | |
217 | /* It's ARP; let it be handled there */ | |
218 | arp_validate(rt); | |
219 | } | |
c910b4d9 A |
220 | } |
221 | return (rn); | |
222 | } | |
223 | ||
224 | /* | |
225 | * Similar to in_matroute_args except without the leaf-matching parameters. | |
226 | */ | |
227 | static struct radix_node * | |
228 | in_matroute(void *v_arg, struct radix_node_head *head) | |
229 | { | |
230 | return (in_matroute_args(v_arg, head, NULL, NULL)); | |
231 | } | |
232 | ||
1c79356b A |
233 | /* |
234 | * This code is the inverse of in_clsroute: on first reference, if we | |
235 | * were managing the route, stop doing so and set the expiration timer | |
236 | * back off again. | |
237 | */ | |
238 | static struct radix_node * | |
c910b4d9 A |
239 | in_matroute_args(void *v_arg, struct radix_node_head *head, |
240 | rn_matchf_t *f, void *w) | |
1c79356b | 241 | { |
c910b4d9 | 242 | struct radix_node *rn = rn_match_args(v_arg, head, f, w); |
1c79356b | 243 | |
b0d623f7 A |
244 | if (rn != NULL) { |
245 | RT_LOCK_SPIN((struct rtentry *)rn); | |
246 | in_validate(rn); | |
247 | RT_UNLOCK((struct rtentry *)rn); | |
248 | } | |
249 | return (rn); | |
1c79356b A |
250 | } |
251 | ||
9bccf70c | 252 | static int rtq_reallyold = 60*60; |
1c79356b | 253 | /* one hour is ``really old'' */ |
6d2010ae | 254 | SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c A |
255 | &rtq_reallyold , 0, |
256 | "Default expiration time on dynamically learned routes"); | |
1c79356b | 257 | |
9bccf70c | 258 | static int rtq_minreallyold = 10; |
1c79356b | 259 | /* never automatically crank down to less */ |
6d2010ae | 260 | SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c A |
261 | &rtq_minreallyold , 0, |
262 | "Minimum time to attempt to hold onto dynamically learned routes"); | |
1c79356b | 263 | |
9bccf70c | 264 | static int rtq_toomany = 128; |
1c79356b | 265 | /* 128 cached routes is ``too many'' */ |
6d2010ae | 266 | SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c A |
267 | &rtq_toomany , 0, "Upper limit on dynamically learned routes"); |
268 | ||
269 | #ifdef __APPLE__ | |
270 | /* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954) | |
271 | * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel | |
272 | * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point. | |
273 | * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS. | |
274 | * That circular route was ignored on previous version of MacOS X because of a routing bug | |
275 | * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED | |
276 | * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is | |
277 | * made in order to provide binary compatibility with AOL. | |
278 | * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of | |
279 | * confusing the routing table with a wrong route to the previous default gateway | |
280 | * If for some reason a circular route is needed, turn this sysctl (net.inet.ip.check_route_selfref) to zero. | |
281 | */ | |
282 | int check_routeselfref = 1; | |
6d2010ae | 283 | SYSCTL_INT(_net_inet_ip, OID_AUTO, check_route_selfref, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c A |
284 | &check_routeselfref , 0, ""); |
285 | #endif | |
286 | ||
b0d623f7 | 287 | int use_routegenid = 1; |
6d2010ae | 288 | SYSCTL_INT(_net_inet_ip, OID_AUTO, use_route_genid, CTLFLAG_RW | CTLFLAG_LOCKED, |
55e303ae | 289 | &use_routegenid , 0, ""); |
1c79356b A |
290 | |
291 | /* | |
292 | * On last reference drop, mark the route as belong to us so that it can be | |
293 | * timed out. | |
294 | */ | |
295 | static void | |
2d21ac55 | 296 | in_clsroute(struct radix_node *rn, __unused struct radix_node_head *head) |
1c79356b A |
297 | { |
298 | struct rtentry *rt = (struct rtentry *)rn; | |
299 | ||
b0d623f7 A |
300 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); |
301 | RT_LOCK_ASSERT_HELD(rt); | |
302 | ||
2d21ac55 | 303 | if (!(rt->rt_flags & RTF_UP)) |
1c79356b A |
304 | return; /* prophylactic measures */ |
305 | ||
2d21ac55 | 306 | if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) |
1c79356b A |
307 | return; |
308 | ||
2d21ac55 | 309 | if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED) |
1c79356b A |
310 | return; |
311 | ||
312 | /* | |
2d21ac55 A |
313 | * Delete the route immediately if RTF_DELCLONE is set or |
314 | * if route caching is disabled (rtq_reallyold set to 0). | |
315 | * Otherwise, let it expire and be deleted by in_rtqkill(). | |
1c79356b | 316 | */ |
2d21ac55 A |
317 | if ((rt->rt_flags & RTF_DELCLONE) || rtq_reallyold == 0) { |
318 | /* | |
319 | * Delete the route from the radix tree but since we are | |
320 | * called when the route's reference count is 0, don't | |
321 | * deallocate it until we return from this routine by | |
322 | * telling rtrequest that we're interested in it. | |
b0d623f7 A |
323 | * Safe to drop rt_lock and use rt_key, rt_gateway since |
324 | * holding rnh_lock here prevents another thread from | |
325 | * calling rt_setgate() on this route. | |
2d21ac55 | 326 | */ |
b0d623f7 | 327 | RT_UNLOCK(rt); |
2d21ac55 A |
328 | if (rtrequest_locked(RTM_DELETE, (struct sockaddr *)rt_key(rt), |
329 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, &rt) == 0) { | |
330 | /* Now let the caller free it */ | |
b0d623f7 A |
331 | RT_LOCK(rt); |
332 | RT_REMREF_LOCKED(rt); | |
333 | } else { | |
334 | RT_LOCK(rt); | |
2d21ac55 A |
335 | } |
336 | } else { | |
6d2010ae | 337 | uint64_t timenow; |
2d21ac55 | 338 | |
6d2010ae | 339 | timenow = net_uptime(); |
1c79356b | 340 | rt->rt_flags |= RTPRF_OURS; |
6d2010ae A |
341 | rt_setexpire(rt, |
342 | rt_expiry(rt, timenow, rtq_reallyold)); | |
1c79356b A |
343 | } |
344 | } | |
345 | ||
346 | struct rtqk_arg { | |
347 | struct radix_node_head *rnh; | |
348 | int draining; | |
349 | int killed; | |
350 | int found; | |
351 | int updating; | |
6d2010ae | 352 | uint64_t nextstop; |
1c79356b A |
353 | }; |
354 | ||
355 | /* | |
356 | * Get rid of old routes. When draining, this deletes everything, even when | |
357 | * the timeout is not expired yet. When updating, this makes sure that | |
358 | * nothing has a timeout longer than the current value of rtq_reallyold. | |
359 | */ | |
360 | static int | |
361 | in_rtqkill(struct radix_node *rn, void *rock) | |
362 | { | |
363 | struct rtqk_arg *ap = rock; | |
364 | struct rtentry *rt = (struct rtentry *)rn; | |
365 | int err; | |
6d2010ae | 366 | uint64_t timenow; |
1c79356b | 367 | |
6d2010ae | 368 | timenow = net_uptime(); |
b0d623f7 | 369 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 370 | |
b0d623f7 | 371 | RT_LOCK(rt); |
2d21ac55 | 372 | if (rt->rt_flags & RTPRF_OURS) { |
1c79356b A |
373 | ap->found++; |
374 | ||
6d2010ae A |
375 | VERIFY(rt->rt_expire == 0 || rt->rt_rmx.rmx_expire != 0); |
376 | VERIFY(rt->rt_expire != 0 || rt->rt_rmx.rmx_expire == 0); | |
377 | if (ap->draining || rt->rt_expire <= timenow) { | |
2d21ac55 | 378 | if (rt->rt_refcnt > 0) |
1c79356b A |
379 | panic("rtqkill route really not free"); |
380 | ||
b0d623f7 A |
381 | /* |
382 | * Delete this route since we're done with it; | |
383 | * the route may be freed afterwards, so we | |
384 | * can no longer refer to 'rt' upon returning | |
385 | * from rtrequest(). Safe to drop rt_lock and | |
386 | * use rt_key, rt_gateway since holding rnh_lock | |
387 | * here prevents another thread from calling | |
388 | * rt_setgate() on this route. | |
389 | */ | |
390 | RT_UNLOCK(rt); | |
391 | err = rtrequest_locked(RTM_DELETE, rt_key(rt), | |
392 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); | |
2d21ac55 | 393 | if (err) { |
1c79356b A |
394 | log(LOG_WARNING, "in_rtqkill: error %d\n", err); |
395 | } else { | |
396 | ap->killed++; | |
397 | } | |
398 | } else { | |
d1ecb069 | 399 | if (ap->updating && |
6d2010ae | 400 | (rt->rt_expire - timenow) > |
d1ecb069 | 401 | rt_expiry(rt, 0, rtq_reallyold)) { |
6d2010ae A |
402 | rt_setexpire(rt, rt_expiry(rt, |
403 | timenow, rtq_reallyold)); | |
1c79356b A |
404 | } |
405 | ap->nextstop = lmin(ap->nextstop, | |
6d2010ae | 406 | rt->rt_expire); |
b0d623f7 | 407 | RT_UNLOCK(rt); |
1c79356b | 408 | } |
b0d623f7 A |
409 | } else { |
410 | RT_UNLOCK(rt); | |
1c79356b A |
411 | } |
412 | ||
413 | return 0; | |
414 | } | |
415 | ||
416 | static void | |
417 | in_rtqtimo_funnel(void *rock) | |
418 | { | |
1c79356b | 419 | in_rtqtimo(rock); |
1c79356b A |
420 | |
421 | } | |
422 | #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ | |
423 | static int rtq_timeout = RTQ_TIMEOUT; | |
424 | ||
425 | static void | |
426 | in_rtqtimo(void *rock) | |
427 | { | |
428 | struct radix_node_head *rnh = rock; | |
429 | struct rtqk_arg arg; | |
430 | struct timeval atv; | |
6d2010ae A |
431 | static uint64_t last_adjusted_timeout = 0; |
432 | uint64_t timenow; | |
9bccf70c | 433 | |
b0d623f7 | 434 | lck_mtx_lock(rnh_lock); |
2d21ac55 | 435 | /* Get the timestamp after we acquire the lock for better accuracy */ |
6d2010ae | 436 | timenow = net_uptime(); |
2d21ac55 | 437 | |
1c79356b A |
438 | arg.found = arg.killed = 0; |
439 | arg.rnh = rnh; | |
6d2010ae | 440 | arg.nextstop = timenow + rtq_timeout; |
1c79356b | 441 | arg.draining = arg.updating = 0; |
1c79356b | 442 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); |
1c79356b A |
443 | |
444 | /* | |
445 | * Attempt to be somewhat dynamic about this: | |
446 | * If there are ``too many'' routes sitting around taking up space, | |
447 | * then crank down the timeout, and see if we can't make some more | |
448 | * go away. However, we make sure that we will never adjust more | |
449 | * than once in rtq_timeout seconds, to keep from cranking down too | |
450 | * hard. | |
451 | */ | |
452 | if((arg.found - arg.killed > rtq_toomany) | |
6d2010ae | 453 | && ((timenow - last_adjusted_timeout) >= (uint64_t)rtq_timeout) |
1c79356b A |
454 | && rtq_reallyold > rtq_minreallyold) { |
455 | rtq_reallyold = 2*rtq_reallyold / 3; | |
456 | if(rtq_reallyold < rtq_minreallyold) { | |
457 | rtq_reallyold = rtq_minreallyold; | |
458 | } | |
459 | ||
6d2010ae | 460 | last_adjusted_timeout = timenow; |
1c79356b A |
461 | #if DIAGNOSTIC |
462 | log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n", | |
463 | rtq_reallyold); | |
464 | #endif | |
465 | arg.found = arg.killed = 0; | |
466 | arg.updating = 1; | |
1c79356b | 467 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); |
1c79356b A |
468 | } |
469 | ||
470 | atv.tv_usec = 0; | |
6d2010ae | 471 | atv.tv_sec = arg.nextstop - timenow; |
b0d623f7 | 472 | lck_mtx_unlock(rnh_lock); |
1c79356b | 473 | timeout(in_rtqtimo_funnel, rock, tvtohz(&atv)); |
1c79356b A |
474 | } |
475 | ||
476 | void | |
477 | in_rtqdrain(void) | |
478 | { | |
479 | struct radix_node_head *rnh = rt_tables[AF_INET]; | |
480 | struct rtqk_arg arg; | |
1c79356b A |
481 | arg.found = arg.killed = 0; |
482 | arg.rnh = rnh; | |
483 | arg.nextstop = 0; | |
484 | arg.draining = 1; | |
485 | arg.updating = 0; | |
b0d623f7 | 486 | lck_mtx_lock(rnh_lock); |
1c79356b | 487 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); |
b0d623f7 | 488 | lck_mtx_unlock(rnh_lock); |
1c79356b A |
489 | } |
490 | ||
491 | /* | |
492 | * Initialize our routing tree. | |
493 | */ | |
494 | int | |
495 | in_inithead(void **head, int off) | |
496 | { | |
497 | struct radix_node_head *rnh; | |
9bccf70c A |
498 | |
499 | #ifdef __APPLE__ | |
1c79356b | 500 | if (*head) |
9bccf70c A |
501 | return 1; |
502 | #endif | |
1c79356b A |
503 | |
504 | if(!rn_inithead(head, off)) | |
505 | return 0; | |
506 | ||
507 | if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */ | |
508 | return 1; /* only do this for the real routing table */ | |
509 | ||
510 | rnh = *head; | |
511 | rnh->rnh_addaddr = in_addroute; | |
512 | rnh->rnh_matchaddr = in_matroute; | |
c910b4d9 | 513 | rnh->rnh_matchaddr_args = in_matroute_args; |
1c79356b A |
514 | rnh->rnh_close = in_clsroute; |
515 | in_rtqtimo(rnh); /* kick off timeout first time */ | |
516 | return 1; | |
517 | } | |
518 | ||
519 | \f | |
520 | /* | |
9bccf70c A |
521 | * This zaps old routes when the interface goes down or interface |
522 | * address is deleted. In the latter case, it deletes static routes | |
523 | * that point to this address. If we don't do this, we may end up | |
524 | * using the old address in the future. The ones we always want to | |
525 | * get rid of are things like ARP entries, since the user might down | |
526 | * the interface, walk over to a completely different network, and | |
527 | * plug back in. | |
1c79356b A |
528 | */ |
529 | struct in_ifadown_arg { | |
530 | struct radix_node_head *rnh; | |
531 | struct ifaddr *ifa; | |
9bccf70c | 532 | int del; |
1c79356b A |
533 | }; |
534 | ||
535 | static int | |
536 | in_ifadownkill(struct radix_node *rn, void *xap) | |
537 | { | |
538 | struct in_ifadown_arg *ap = xap; | |
539 | struct rtentry *rt = (struct rtentry *)rn; | |
540 | int err; | |
541 | ||
b0d623f7 | 542 | RT_LOCK(rt); |
9bccf70c A |
543 | if (rt->rt_ifa == ap->ifa && |
544 | (ap->del || !(rt->rt_flags & RTF_STATIC))) { | |
1c79356b A |
545 | /* |
546 | * We need to disable the automatic prune that happens | |
547 | * in this case in rtrequest() because it will blow | |
548 | * away the pointers that rn_walktree() needs in order | |
549 | * continue our descent. We will end up deleting all | |
550 | * the routes that rtrequest() would have in any case, | |
b0d623f7 A |
551 | * so that behavior is not needed there. Safe to drop |
552 | * rt_lock and use rt_key, rt_gateway, since holding | |
553 | * rnh_lock here prevents another thread from calling | |
554 | * rt_setgate() on this route. | |
1c79356b | 555 | */ |
9bccf70c | 556 | rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING); |
b0d623f7 A |
557 | RT_UNLOCK(rt); |
558 | err = rtrequest_locked(RTM_DELETE, rt_key(rt), | |
559 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); | |
1c79356b A |
560 | if (err) { |
561 | log(LOG_WARNING, "in_ifadownkill: error %d\n", err); | |
562 | } | |
b0d623f7 A |
563 | } else { |
564 | RT_UNLOCK(rt); | |
1c79356b A |
565 | } |
566 | return 0; | |
567 | } | |
568 | ||
569 | int | |
9bccf70c | 570 | in_ifadown(struct ifaddr *ifa, int delete) |
1c79356b A |
571 | { |
572 | struct in_ifadown_arg arg; | |
573 | struct radix_node_head *rnh; | |
574 | ||
b0d623f7 | 575 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); |
91447636 | 576 | |
6d2010ae A |
577 | /* |
578 | * Holding rnh_lock here prevents the possibility of | |
579 | * ifa from changing (e.g. in_ifinit), so it is safe | |
580 | * to access its ifa_addr without locking. | |
581 | */ | |
1c79356b | 582 | if (ifa->ifa_addr->sa_family != AF_INET) |
6d2010ae | 583 | return (1); |
1c79356b | 584 | |
2d21ac55 | 585 | /* trigger route cache reevaluation */ |
b0d623f7 A |
586 | if (use_routegenid) |
587 | routegenid_update(); | |
2d21ac55 | 588 | |
1c79356b A |
589 | arg.rnh = rnh = rt_tables[AF_INET]; |
590 | arg.ifa = ifa; | |
9bccf70c | 591 | arg.del = delete; |
1c79356b | 592 | rnh->rnh_walktree(rnh, in_ifadownkill, &arg); |
6d2010ae | 593 | IFA_LOCK_SPIN(ifa); |
1c79356b | 594 | ifa->ifa_flags &= ~IFA_ROUTE; |
6d2010ae A |
595 | IFA_UNLOCK(ifa); |
596 | return (0); | |
1c79356b | 597 | } |