]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
43866e37 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
1c79356b | 7 | * |
43866e37 A |
8 | * This file contains Original Code and/or Modifications of Original Code |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
43866e37 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * Copyright 1994, 1995 Massachusetts Institute of Technology | |
27 | * | |
28 | * Permission to use, copy, modify, and distribute this software and | |
29 | * its documentation for any purpose and without fee is hereby | |
30 | * granted, provided that both the above copyright notice and this | |
31 | * permission notice appear in all copies, that both the above | |
32 | * copyright notice and this permission notice appear in all | |
33 | * supporting documentation, and that the name of M.I.T. not be used | |
34 | * in advertising or publicity pertaining to distribution of the | |
35 | * software without specific, written prior permission. M.I.T. makes | |
36 | * no representations about the suitability of this software for any | |
37 | * purpose. It is provided "as is" without express or implied | |
38 | * warranty. | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS | |
41 | * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
42 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
43 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT | |
44 | * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
46 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |
47 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
48 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
49 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
50 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
52 | * | |
9bccf70c | 53 | * $FreeBSD: src/sys/netinet/in_rmx.c,v 1.37.2.1 2001/05/14 08:23:49 ru Exp $ |
1c79356b A |
54 | */ |
55 | ||
56 | /* | |
57 | * This code does two things necessary for the enhanced TCP metrics to | |
58 | * function in a useful manner: | |
59 | * 1) It marks all non-host routes as `cloning', thus ensuring that | |
60 | * every actual reference to such a route actually gets turned | |
61 | * into a reference to a host route to the specific destination | |
62 | * requested. | |
63 | * 2) When such routes lose all their references, it arranges for them | |
64 | * to be deleted in some random collection of circumstances, so that | |
65 | * a large quantity of stale routing data is not kept in kernel memory | |
66 | * indefinitely. See in_rtqtimo() below for the exact mechanism. | |
67 | */ | |
68 | ||
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
71 | #include <sys/kernel.h> | |
72 | #include <sys/sysctl.h> | |
73 | #include <sys/socket.h> | |
74 | #include <sys/mbuf.h> | |
75 | #include <sys/syslog.h> | |
76 | ||
77 | #include <net/if.h> | |
78 | #include <net/route.h> | |
79 | #include <netinet/in.h> | |
80 | #include <netinet/in_var.h> | |
81 | ||
82 | extern int in_inithead __P((void **head, int off)); | |
83 | ||
9bccf70c A |
84 | #ifdef __APPLE__ |
85 | static void in_rtqtimo(void *rock); | |
86 | #endif | |
87 | ||
1c79356b A |
88 | #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */ |
89 | ||
90 | /* | |
91 | * Do what we need to do when inserting a route. | |
92 | */ | |
93 | static struct radix_node * | |
94 | in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, | |
95 | struct radix_node *treenodes) | |
96 | { | |
97 | struct rtentry *rt = (struct rtentry *)treenodes; | |
98 | struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt); | |
99 | struct radix_node *ret; | |
100 | ||
101 | /* | |
102 | * For IP, all unicast non-host routes are automatically cloning. | |
103 | */ | |
104 | if(IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) | |
105 | rt->rt_flags |= RTF_MULTICAST; | |
106 | ||
107 | if(!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) { | |
108 | rt->rt_flags |= RTF_PRCLONING; | |
109 | } | |
110 | ||
111 | /* | |
112 | * A little bit of help for both IP output and input: | |
113 | * For host routes, we make sure that RTF_BROADCAST | |
114 | * is set for anything that looks like a broadcast address. | |
115 | * This way, we can avoid an expensive call to in_broadcast() | |
116 | * in ip_output() most of the time (because the route passed | |
117 | * to ip_output() is almost always a host route). | |
118 | * | |
119 | * We also do the same for local addresses, with the thought | |
120 | * that this might one day be used to speed up ip_input(). | |
121 | * | |
122 | * We also mark routes to multicast addresses as such, because | |
123 | * it's easy to do and might be useful (but this is much more | |
124 | * dubious since it's so easy to inspect the address). (This | |
125 | * is done above.) | |
126 | */ | |
127 | if (rt->rt_flags & RTF_HOST) { | |
128 | if (in_broadcast(sin->sin_addr, rt->rt_ifp)) { | |
129 | rt->rt_flags |= RTF_BROADCAST; | |
130 | } else { | |
131 | #define satosin(sa) ((struct sockaddr_in *)sa) | |
132 | if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr | |
133 | == sin->sin_addr.s_addr) | |
134 | rt->rt_flags |= RTF_LOCAL; | |
135 | #undef satosin | |
136 | } | |
137 | } | |
138 | ||
139 | if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) | |
140 | && rt->rt_ifp) | |
141 | rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; | |
142 | ||
143 | ret = rn_addroute(v_arg, n_arg, head, treenodes); | |
144 | if (ret == NULL && rt->rt_flags & RTF_HOST) { | |
145 | struct rtentry *rt2; | |
146 | /* | |
147 | * We are trying to add a host route, but can't. | |
148 | * Find out if it is because of an | |
149 | * ARP entry and delete it if so. | |
150 | */ | |
151 | rt2 = rtalloc1((struct sockaddr *)sin, 0, | |
152 | RTF_CLONING | RTF_PRCLONING); | |
153 | if (rt2) { | |
154 | if (rt2->rt_flags & RTF_LLINFO && | |
155 | rt2->rt_flags & RTF_HOST && | |
156 | rt2->rt_gateway && | |
157 | rt2->rt_gateway->sa_family == AF_LINK) { | |
158 | rtrequest(RTM_DELETE, | |
159 | (struct sockaddr *)rt_key(rt2), | |
160 | rt2->rt_gateway, | |
161 | rt_mask(rt2), rt2->rt_flags, 0); | |
162 | ret = rn_addroute(v_arg, n_arg, head, | |
163 | treenodes); | |
164 | } | |
9bccf70c | 165 | rtfree(rt2); |
1c79356b A |
166 | } |
167 | } | |
168 | return ret; | |
169 | } | |
170 | ||
171 | /* | |
172 | * This code is the inverse of in_clsroute: on first reference, if we | |
173 | * were managing the route, stop doing so and set the expiration timer | |
174 | * back off again. | |
175 | */ | |
176 | static struct radix_node * | |
177 | in_matroute(void *v_arg, struct radix_node_head *head) | |
178 | { | |
179 | struct radix_node *rn = rn_match(v_arg, head); | |
180 | struct rtentry *rt = (struct rtentry *)rn; | |
181 | ||
182 | if(rt && rt->rt_refcnt == 0) { /* this is first reference */ | |
183 | if(rt->rt_flags & RTPRF_OURS) { | |
184 | rt->rt_flags &= ~RTPRF_OURS; | |
185 | rt->rt_rmx.rmx_expire = 0; | |
186 | } | |
187 | } | |
188 | return rn; | |
189 | } | |
190 | ||
9bccf70c | 191 | static int rtq_reallyold = 60*60; |
1c79356b | 192 | /* one hour is ``really old'' */ |
9bccf70c A |
193 | SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW, |
194 | &rtq_reallyold , 0, | |
195 | "Default expiration time on dynamically learned routes"); | |
1c79356b | 196 | |
9bccf70c | 197 | static int rtq_minreallyold = 10; |
1c79356b | 198 | /* never automatically crank down to less */ |
9bccf70c A |
199 | SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW, |
200 | &rtq_minreallyold , 0, | |
201 | "Minimum time to attempt to hold onto dynamically learned routes"); | |
1c79356b | 202 | |
9bccf70c | 203 | static int rtq_toomany = 128; |
1c79356b | 204 | /* 128 cached routes is ``too many'' */ |
9bccf70c A |
205 | SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW, |
206 | &rtq_toomany , 0, "Upper limit on dynamically learned routes"); | |
207 | ||
208 | #ifdef __APPLE__ | |
209 | /* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954) | |
210 | * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel | |
211 | * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point. | |
212 | * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS. | |
213 | * That circular route was ignored on previous version of MacOS X because of a routing bug | |
214 | * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED | |
215 | * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is | |
216 | * made in order to provide binary compatibility with AOL. | |
217 | * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of | |
218 | * confusing the routing table with a wrong route to the previous default gateway | |
219 | * If for some reason a circular route is needed, turn this sysctl (net.inet.ip.check_route_selfref) to zero. | |
220 | */ | |
221 | int check_routeselfref = 1; | |
222 | SYSCTL_INT(_net_inet_ip, OID_AUTO, check_route_selfref, CTLFLAG_RW, | |
223 | &check_routeselfref , 0, ""); | |
224 | #endif | |
225 | ||
55e303ae A |
226 | __private_extern__ int use_routegenid = 1; |
227 | SYSCTL_INT(_net_inet_ip, OID_AUTO, use_route_genid, CTLFLAG_RW, | |
228 | &use_routegenid , 0, ""); | |
1c79356b A |
229 | |
230 | /* | |
231 | * On last reference drop, mark the route as belong to us so that it can be | |
232 | * timed out. | |
233 | */ | |
234 | static void | |
235 | in_clsroute(struct radix_node *rn, struct radix_node_head *head) | |
236 | { | |
237 | struct rtentry *rt = (struct rtentry *)rn; | |
238 | ||
239 | if(!(rt->rt_flags & RTF_UP)) | |
240 | return; /* prophylactic measures */ | |
241 | ||
242 | if((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST) | |
243 | return; | |
244 | ||
245 | if((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) | |
246 | != RTF_WASCLONED) | |
247 | return; | |
248 | ||
249 | /* | |
250 | * As requested by David Greenman: | |
251 | * If rtq_reallyold is 0, just delete the route without | |
252 | * waiting for a timeout cycle to kill it. | |
253 | */ | |
254 | if(rtq_reallyold != 0) { | |
255 | rt->rt_flags |= RTPRF_OURS; | |
256 | rt->rt_rmx.rmx_expire = time_second + rtq_reallyold; | |
257 | } else { | |
258 | rtrequest(RTM_DELETE, | |
259 | (struct sockaddr *)rt_key(rt), | |
260 | rt->rt_gateway, rt_mask(rt), | |
261 | rt->rt_flags, 0); | |
262 | } | |
263 | } | |
264 | ||
265 | struct rtqk_arg { | |
266 | struct radix_node_head *rnh; | |
267 | int draining; | |
268 | int killed; | |
269 | int found; | |
270 | int updating; | |
271 | time_t nextstop; | |
272 | }; | |
273 | ||
274 | /* | |
275 | * Get rid of old routes. When draining, this deletes everything, even when | |
276 | * the timeout is not expired yet. When updating, this makes sure that | |
277 | * nothing has a timeout longer than the current value of rtq_reallyold. | |
278 | */ | |
279 | static int | |
280 | in_rtqkill(struct radix_node *rn, void *rock) | |
281 | { | |
282 | struct rtqk_arg *ap = rock; | |
283 | struct rtentry *rt = (struct rtentry *)rn; | |
284 | int err; | |
285 | ||
286 | if(rt->rt_flags & RTPRF_OURS) { | |
287 | ap->found++; | |
288 | ||
289 | if(ap->draining || rt->rt_rmx.rmx_expire <= time_second) { | |
290 | if(rt->rt_refcnt > 0) | |
291 | panic("rtqkill route really not free"); | |
292 | ||
293 | err = rtrequest(RTM_DELETE, | |
294 | (struct sockaddr *)rt_key(rt), | |
295 | rt->rt_gateway, rt_mask(rt), | |
296 | rt->rt_flags, 0); | |
297 | if(err) { | |
298 | log(LOG_WARNING, "in_rtqkill: error %d\n", err); | |
299 | } else { | |
300 | ap->killed++; | |
301 | } | |
302 | } else { | |
303 | if(ap->updating | |
304 | && (rt->rt_rmx.rmx_expire - time_second | |
305 | > rtq_reallyold)) { | |
306 | rt->rt_rmx.rmx_expire = time_second | |
307 | + rtq_reallyold; | |
308 | } | |
309 | ap->nextstop = lmin(ap->nextstop, | |
310 | rt->rt_rmx.rmx_expire); | |
311 | } | |
312 | } | |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
317 | static void | |
318 | in_rtqtimo_funnel(void *rock) | |
319 | { | |
320 | boolean_t funnel_state; | |
321 | ||
322 | funnel_state = thread_funnel_set(network_flock, TRUE); | |
323 | in_rtqtimo(rock); | |
324 | (void) thread_funnel_set(network_flock, FALSE); | |
325 | ||
326 | } | |
327 | #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */ | |
328 | static int rtq_timeout = RTQ_TIMEOUT; | |
329 | ||
330 | static void | |
331 | in_rtqtimo(void *rock) | |
332 | { | |
333 | struct radix_node_head *rnh = rock; | |
334 | struct rtqk_arg arg; | |
335 | struct timeval atv; | |
336 | static time_t last_adjusted_timeout = 0; | |
337 | int s; | |
9bccf70c | 338 | |
1c79356b A |
339 | arg.found = arg.killed = 0; |
340 | arg.rnh = rnh; | |
341 | arg.nextstop = time_second + rtq_timeout; | |
342 | arg.draining = arg.updating = 0; | |
343 | s = splnet(); | |
344 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); | |
345 | splx(s); | |
346 | ||
347 | /* | |
348 | * Attempt to be somewhat dynamic about this: | |
349 | * If there are ``too many'' routes sitting around taking up space, | |
350 | * then crank down the timeout, and see if we can't make some more | |
351 | * go away. However, we make sure that we will never adjust more | |
352 | * than once in rtq_timeout seconds, to keep from cranking down too | |
353 | * hard. | |
354 | */ | |
355 | if((arg.found - arg.killed > rtq_toomany) | |
356 | && (time_second - last_adjusted_timeout >= rtq_timeout) | |
357 | && rtq_reallyold > rtq_minreallyold) { | |
358 | rtq_reallyold = 2*rtq_reallyold / 3; | |
359 | if(rtq_reallyold < rtq_minreallyold) { | |
360 | rtq_reallyold = rtq_minreallyold; | |
361 | } | |
362 | ||
363 | last_adjusted_timeout = time_second; | |
364 | #if DIAGNOSTIC | |
365 | log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n", | |
366 | rtq_reallyold); | |
367 | #endif | |
368 | arg.found = arg.killed = 0; | |
369 | arg.updating = 1; | |
370 | s = splnet(); | |
371 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); | |
372 | splx(s); | |
373 | } | |
374 | ||
375 | atv.tv_usec = 0; | |
376 | atv.tv_sec = arg.nextstop - time_second; | |
377 | timeout(in_rtqtimo_funnel, rock, tvtohz(&atv)); | |
1c79356b A |
378 | } |
379 | ||
380 | void | |
381 | in_rtqdrain(void) | |
382 | { | |
383 | struct radix_node_head *rnh = rt_tables[AF_INET]; | |
384 | struct rtqk_arg arg; | |
385 | int s; | |
386 | arg.found = arg.killed = 0; | |
387 | arg.rnh = rnh; | |
388 | arg.nextstop = 0; | |
389 | arg.draining = 1; | |
390 | arg.updating = 0; | |
391 | s = splnet(); | |
392 | rnh->rnh_walktree(rnh, in_rtqkill, &arg); | |
393 | splx(s); | |
394 | } | |
395 | ||
396 | /* | |
397 | * Initialize our routing tree. | |
398 | */ | |
399 | int | |
400 | in_inithead(void **head, int off) | |
401 | { | |
402 | struct radix_node_head *rnh; | |
9bccf70c A |
403 | |
404 | #ifdef __APPLE__ | |
1c79356b | 405 | if (*head) |
9bccf70c A |
406 | return 1; |
407 | #endif | |
1c79356b A |
408 | |
409 | if(!rn_inithead(head, off)) | |
410 | return 0; | |
411 | ||
412 | if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */ | |
413 | return 1; /* only do this for the real routing table */ | |
414 | ||
415 | rnh = *head; | |
416 | rnh->rnh_addaddr = in_addroute; | |
417 | rnh->rnh_matchaddr = in_matroute; | |
418 | rnh->rnh_close = in_clsroute; | |
419 | in_rtqtimo(rnh); /* kick off timeout first time */ | |
420 | return 1; | |
421 | } | |
422 | ||
423 | \f | |
424 | /* | |
9bccf70c A |
425 | * This zaps old routes when the interface goes down or interface |
426 | * address is deleted. In the latter case, it deletes static routes | |
427 | * that point to this address. If we don't do this, we may end up | |
428 | * using the old address in the future. The ones we always want to | |
429 | * get rid of are things like ARP entries, since the user might down | |
430 | * the interface, walk over to a completely different network, and | |
431 | * plug back in. | |
1c79356b A |
432 | */ |
433 | struct in_ifadown_arg { | |
434 | struct radix_node_head *rnh; | |
435 | struct ifaddr *ifa; | |
9bccf70c | 436 | int del; |
1c79356b A |
437 | }; |
438 | ||
439 | static int | |
440 | in_ifadownkill(struct radix_node *rn, void *xap) | |
441 | { | |
442 | struct in_ifadown_arg *ap = xap; | |
443 | struct rtentry *rt = (struct rtentry *)rn; | |
444 | int err; | |
445 | ||
9bccf70c A |
446 | if (rt->rt_ifa == ap->ifa && |
447 | (ap->del || !(rt->rt_flags & RTF_STATIC))) { | |
1c79356b A |
448 | /* |
449 | * We need to disable the automatic prune that happens | |
450 | * in this case in rtrequest() because it will blow | |
451 | * away the pointers that rn_walktree() needs in order | |
452 | * continue our descent. We will end up deleting all | |
453 | * the routes that rtrequest() would have in any case, | |
454 | * so that behavior is not needed there. | |
455 | */ | |
9bccf70c | 456 | rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING); |
1c79356b A |
457 | err = rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt), |
458 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); | |
459 | if (err) { | |
460 | log(LOG_WARNING, "in_ifadownkill: error %d\n", err); | |
461 | } | |
462 | } | |
463 | return 0; | |
464 | } | |
465 | ||
466 | int | |
9bccf70c | 467 | in_ifadown(struct ifaddr *ifa, int delete) |
1c79356b A |
468 | { |
469 | struct in_ifadown_arg arg; | |
470 | struct radix_node_head *rnh; | |
471 | ||
472 | if (ifa->ifa_addr->sa_family != AF_INET) | |
473 | return 1; | |
474 | ||
475 | arg.rnh = rnh = rt_tables[AF_INET]; | |
476 | arg.ifa = ifa; | |
9bccf70c | 477 | arg.del = delete; |
1c79356b A |
478 | rnh->rnh_walktree(rnh, in_ifadownkill, &arg); |
479 | ifa->ifa_flags &= ~IFA_ROUTE; | |
480 | return 0; | |
481 | } |