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