]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
c910b4d9 | 2 | * Copyright (c) 2000-2008 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; |
91447636 | 326 | rt->rt_rmx.rmx_expire = timenow.tv_sec + rtq_reallyold; |
1c79356b A |
327 | } |
328 | } | |
329 | ||
330 | struct rtqk_arg { | |
331 | struct radix_node_head *rnh; | |
332 | int draining; | |
333 | int killed; | |
334 | int found; | |
335 | int updating; | |
336 | time_t nextstop; | |
337 | }; | |
338 | ||
339 | /* | |
340 | * Get rid of old routes. When draining, this deletes everything, even when | |
341 | * the timeout is not expired yet. When updating, this makes sure that | |
342 | * nothing has a timeout longer than the current value of rtq_reallyold. | |
343 | */ | |
344 | static int | |
345 | in_rtqkill(struct radix_node *rn, void *rock) | |
346 | { | |
347 | struct rtqk_arg *ap = rock; | |
348 | struct rtentry *rt = (struct rtentry *)rn; | |
349 | int err; | |
91447636 | 350 | struct timeval timenow; |
1c79356b | 351 | |
91447636 | 352 | getmicrotime(&timenow); |
b0d623f7 | 353 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 354 | |
b0d623f7 | 355 | RT_LOCK(rt); |
2d21ac55 | 356 | if (rt->rt_flags & RTPRF_OURS) { |
1c79356b A |
357 | ap->found++; |
358 | ||
2d21ac55 A |
359 | if (ap->draining || rt->rt_rmx.rmx_expire <= timenow.tv_sec) { |
360 | if (rt->rt_refcnt > 0) | |
1c79356b A |
361 | panic("rtqkill route really not free"); |
362 | ||
b0d623f7 A |
363 | /* |
364 | * Delete this route since we're done with it; | |
365 | * the route may be freed afterwards, so we | |
366 | * can no longer refer to 'rt' upon returning | |
367 | * from rtrequest(). Safe to drop rt_lock and | |
368 | * use rt_key, rt_gateway since holding rnh_lock | |
369 | * here prevents another thread from calling | |
370 | * rt_setgate() on this route. | |
371 | */ | |
372 | RT_UNLOCK(rt); | |
373 | err = rtrequest_locked(RTM_DELETE, rt_key(rt), | |
374 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); | |
2d21ac55 | 375 | if (err) { |
1c79356b A |
376 | log(LOG_WARNING, "in_rtqkill: error %d\n", err); |
377 | } else { | |
378 | ap->killed++; | |
379 | } | |
380 | } else { | |
2d21ac55 | 381 | if (ap->updating |
91447636 | 382 | && (rt->rt_rmx.rmx_expire - timenow.tv_sec |
1c79356b | 383 | > rtq_reallyold)) { |
91447636 | 384 | rt->rt_rmx.rmx_expire = timenow.tv_sec |
1c79356b A |
385 | + rtq_reallyold; |
386 | } | |
387 | ap->nextstop = lmin(ap->nextstop, | |
388 | rt->rt_rmx.rmx_expire); | |
b0d623f7 | 389 | RT_UNLOCK(rt); |
1c79356b | 390 | } |
b0d623f7 A |
391 | } else { |
392 | RT_UNLOCK(rt); | |
1c79356b A |
393 | } |
394 | ||
395 | return 0; | |
396 | } | |
397 | ||
398 | static void | |
399 | in_rtqtimo_funnel(void *rock) | |
400 | { | |
1c79356b | 401 | in_rtqtimo(rock); |
1c79356b A |
402 | |
403 | } | |
404 | #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ | |
405 | static int rtq_timeout = RTQ_TIMEOUT; | |
406 | ||
407 | static void | |
408 | in_rtqtimo(void *rock) | |
409 | { | |
410 | struct radix_node_head *rnh = rock; | |
411 | struct rtqk_arg arg; | |
412 | struct timeval atv; | |
413 | static time_t last_adjusted_timeout = 0; | |
91447636 | 414 | struct timeval timenow; |
9bccf70c | 415 | |
b0d623f7 | 416 | lck_mtx_lock(rnh_lock); |
2d21ac55 | 417 | /* Get the timestamp after we acquire the lock for better accuracy */ |
91447636 | 418 | getmicrotime(&timenow); |
2d21ac55 | 419 | |
1c79356b A |
420 | arg.found = arg.killed = 0; |
421 | arg.rnh = rnh; | |
91447636 | 422 | arg.nextstop = timenow.tv_sec + rtq_timeout; |
1c79356b | 423 | arg.draining = arg.updating = 0; |
1c79356b | 424 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); |
1c79356b A |
425 | |
426 | /* | |
427 | * Attempt to be somewhat dynamic about this: | |
428 | * If there are ``too many'' routes sitting around taking up space, | |
429 | * then crank down the timeout, and see if we can't make some more | |
430 | * go away. However, we make sure that we will never adjust more | |
431 | * than once in rtq_timeout seconds, to keep from cranking down too | |
432 | * hard. | |
433 | */ | |
434 | if((arg.found - arg.killed > rtq_toomany) | |
91447636 | 435 | && (timenow.tv_sec - last_adjusted_timeout >= rtq_timeout) |
1c79356b A |
436 | && rtq_reallyold > rtq_minreallyold) { |
437 | rtq_reallyold = 2*rtq_reallyold / 3; | |
438 | if(rtq_reallyold < rtq_minreallyold) { | |
439 | rtq_reallyold = rtq_minreallyold; | |
440 | } | |
441 | ||
91447636 | 442 | last_adjusted_timeout = timenow.tv_sec; |
1c79356b A |
443 | #if DIAGNOSTIC |
444 | log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n", | |
445 | rtq_reallyold); | |
446 | #endif | |
447 | arg.found = arg.killed = 0; | |
448 | arg.updating = 1; | |
1c79356b | 449 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); |
1c79356b A |
450 | } |
451 | ||
452 | atv.tv_usec = 0; | |
91447636 | 453 | atv.tv_sec = arg.nextstop - timenow.tv_sec; |
b0d623f7 | 454 | lck_mtx_unlock(rnh_lock); |
1c79356b | 455 | timeout(in_rtqtimo_funnel, rock, tvtohz(&atv)); |
1c79356b A |
456 | } |
457 | ||
458 | void | |
459 | in_rtqdrain(void) | |
460 | { | |
461 | struct radix_node_head *rnh = rt_tables[AF_INET]; | |
462 | struct rtqk_arg arg; | |
1c79356b A |
463 | arg.found = arg.killed = 0; |
464 | arg.rnh = rnh; | |
465 | arg.nextstop = 0; | |
466 | arg.draining = 1; | |
467 | arg.updating = 0; | |
b0d623f7 | 468 | lck_mtx_lock(rnh_lock); |
1c79356b | 469 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); |
b0d623f7 | 470 | lck_mtx_unlock(rnh_lock); |
1c79356b A |
471 | } |
472 | ||
473 | /* | |
474 | * Initialize our routing tree. | |
475 | */ | |
476 | int | |
477 | in_inithead(void **head, int off) | |
478 | { | |
479 | struct radix_node_head *rnh; | |
9bccf70c A |
480 | |
481 | #ifdef __APPLE__ | |
1c79356b | 482 | if (*head) |
9bccf70c A |
483 | return 1; |
484 | #endif | |
1c79356b A |
485 | |
486 | if(!rn_inithead(head, off)) | |
487 | return 0; | |
488 | ||
489 | if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */ | |
490 | return 1; /* only do this for the real routing table */ | |
491 | ||
492 | rnh = *head; | |
493 | rnh->rnh_addaddr = in_addroute; | |
494 | rnh->rnh_matchaddr = in_matroute; | |
c910b4d9 | 495 | rnh->rnh_matchaddr_args = in_matroute_args; |
1c79356b A |
496 | rnh->rnh_close = in_clsroute; |
497 | in_rtqtimo(rnh); /* kick off timeout first time */ | |
498 | return 1; | |
499 | } | |
500 | ||
501 | \f | |
502 | /* | |
9bccf70c A |
503 | * This zaps old routes when the interface goes down or interface |
504 | * address is deleted. In the latter case, it deletes static routes | |
505 | * that point to this address. If we don't do this, we may end up | |
506 | * using the old address in the future. The ones we always want to | |
507 | * get rid of are things like ARP entries, since the user might down | |
508 | * the interface, walk over to a completely different network, and | |
509 | * plug back in. | |
1c79356b A |
510 | */ |
511 | struct in_ifadown_arg { | |
512 | struct radix_node_head *rnh; | |
513 | struct ifaddr *ifa; | |
9bccf70c | 514 | int del; |
1c79356b A |
515 | }; |
516 | ||
517 | static int | |
518 | in_ifadownkill(struct radix_node *rn, void *xap) | |
519 | { | |
520 | struct in_ifadown_arg *ap = xap; | |
521 | struct rtentry *rt = (struct rtentry *)rn; | |
522 | int err; | |
523 | ||
b0d623f7 | 524 | RT_LOCK(rt); |
9bccf70c A |
525 | if (rt->rt_ifa == ap->ifa && |
526 | (ap->del || !(rt->rt_flags & RTF_STATIC))) { | |
1c79356b A |
527 | /* |
528 | * We need to disable the automatic prune that happens | |
529 | * in this case in rtrequest() because it will blow | |
530 | * away the pointers that rn_walktree() needs in order | |
531 | * continue our descent. We will end up deleting all | |
532 | * the routes that rtrequest() would have in any case, | |
b0d623f7 A |
533 | * so that behavior is not needed there. Safe to drop |
534 | * rt_lock and use rt_key, rt_gateway, since holding | |
535 | * rnh_lock here prevents another thread from calling | |
536 | * rt_setgate() on this route. | |
1c79356b | 537 | */ |
9bccf70c | 538 | rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING); |
b0d623f7 A |
539 | RT_UNLOCK(rt); |
540 | err = rtrequest_locked(RTM_DELETE, rt_key(rt), | |
541 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); | |
1c79356b A |
542 | if (err) { |
543 | log(LOG_WARNING, "in_ifadownkill: error %d\n", err); | |
544 | } | |
b0d623f7 A |
545 | } else { |
546 | RT_UNLOCK(rt); | |
1c79356b A |
547 | } |
548 | return 0; | |
549 | } | |
550 | ||
551 | int | |
9bccf70c | 552 | in_ifadown(struct ifaddr *ifa, int delete) |
1c79356b A |
553 | { |
554 | struct in_ifadown_arg arg; | |
555 | struct radix_node_head *rnh; | |
556 | ||
b0d623f7 | 557 | lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED); |
91447636 | 558 | |
1c79356b A |
559 | if (ifa->ifa_addr->sa_family != AF_INET) |
560 | return 1; | |
561 | ||
2d21ac55 | 562 | /* trigger route cache reevaluation */ |
b0d623f7 A |
563 | if (use_routegenid) |
564 | routegenid_update(); | |
2d21ac55 | 565 | |
1c79356b A |
566 | arg.rnh = rnh = rt_tables[AF_INET]; |
567 | arg.ifa = ifa; | |
9bccf70c | 568 | arg.del = delete; |
1c79356b A |
569 | rnh->rnh_walktree(rnh, in_ifadownkill, &arg); |
570 | ifa->ifa_flags &= ~IFA_ROUTE; | |
571 | return 0; | |
572 | } |