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