]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_rmx.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / bsd / netinet / in_rmx.c
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 #include <kern/lock.h>
74
75 #include <net/if.h>
76 #include <net/route.h>
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
79
80 extern int in_inithead(void **head, int off);
81
82 #ifdef __APPLE__
83 static void in_rtqtimo(void *rock);
84 #endif
85
86 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
87 extern lck_mtx_t *rt_mtx;
88
89 /*
90 * Do what we need to do when inserting a route.
91 */
92 static struct radix_node *
93 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
94 struct radix_node *treenodes)
95 {
96 struct rtentry *rt = (struct rtentry *)treenodes;
97 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
98 struct radix_node *ret;
99
100 /*
101 * For IP, all unicast non-host routes are automatically cloning.
102 */
103 if(IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
104 rt->rt_flags |= RTF_MULTICAST;
105
106 if(!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
107 rt->rt_flags |= RTF_PRCLONING;
108 }
109
110 /*
111 * A little bit of help for both IP output and input:
112 * For host routes, we make sure that RTF_BROADCAST
113 * is set for anything that looks like a broadcast address.
114 * This way, we can avoid an expensive call to in_broadcast()
115 * in ip_output() most of the time (because the route passed
116 * to ip_output() is almost always a host route).
117 *
118 * We also do the same for local addresses, with the thought
119 * that this might one day be used to speed up ip_input().
120 *
121 * We also mark routes to multicast addresses as such, because
122 * it's easy to do and might be useful (but this is much more
123 * dubious since it's so easy to inspect the address). (This
124 * is done above.)
125 */
126 if (rt->rt_flags & RTF_HOST) {
127 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
128 rt->rt_flags |= RTF_BROADCAST;
129 } else {
130 #define satosin(sa) ((struct sockaddr_in *)sa)
131 if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr
132 == sin->sin_addr.s_addr)
133 rt->rt_flags |= RTF_LOCAL;
134 #undef satosin
135 }
136 }
137
138 if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU)
139 && rt->rt_ifp)
140 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
141
142 ret = rn_addroute(v_arg, n_arg, head, treenodes);
143 if (ret == NULL && rt->rt_flags & RTF_HOST) {
144 struct rtentry *rt2;
145 /*
146 * We are trying to add a host route, but can't.
147 * Find out if it is because of an
148 * ARP entry and delete it if so.
149 */
150 rt2 = rtalloc1_locked((struct sockaddr *)sin, 0,
151 RTF_CLONING | RTF_PRCLONING);
152 if (rt2) {
153 if (rt2->rt_flags & RTF_LLINFO &&
154 rt2->rt_flags & RTF_HOST &&
155 rt2->rt_gateway &&
156 rt2->rt_gateway->sa_family == AF_LINK) {
157 rtrequest_locked(RTM_DELETE,
158 (struct sockaddr *)rt_key(rt2),
159 rt2->rt_gateway,
160 rt_mask(rt2), rt2->rt_flags, 0);
161 ret = rn_addroute(v_arg, n_arg, head,
162 treenodes);
163 }
164 rtfree_locked(rt2);
165 }
166 }
167 return ret;
168 }
169
170 /*
171 * This code is the inverse of in_clsroute: on first reference, if we
172 * were managing the route, stop doing so and set the expiration timer
173 * back off again.
174 */
175 static struct radix_node *
176 in_matroute(void *v_arg, struct radix_node_head *head)
177 {
178 struct radix_node *rn = rn_match(v_arg, head);
179 struct rtentry *rt = (struct rtentry *)rn;
180
181 if(rt && rt->rt_refcnt == 0) { /* this is first reference */
182 if(rt->rt_flags & RTPRF_OURS) {
183 rt->rt_flags &= ~RTPRF_OURS;
184 rt->rt_rmx.rmx_expire = 0;
185 }
186 }
187 return rn;
188 }
189
190 static int rtq_reallyold = 60*60;
191 /* one hour is ``really old'' */
192 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
193 &rtq_reallyold , 0,
194 "Default expiration time on dynamically learned routes");
195
196 static int rtq_minreallyold = 10;
197 /* never automatically crank down to less */
198 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
199 &rtq_minreallyold , 0,
200 "Minimum time to attempt to hold onto dynamically learned routes");
201
202 static int rtq_toomany = 128;
203 /* 128 cached routes is ``too many'' */
204 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
205 &rtq_toomany , 0, "Upper limit on dynamically learned routes");
206
207 #ifdef __APPLE__
208 /* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954)
209 * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel
210 * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point.
211 * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS.
212 * That circular route was ignored on previous version of MacOS X because of a routing bug
213 * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED
214 * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is
215 * made in order to provide binary compatibility with AOL.
216 * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of
217 * confusing the routing table with a wrong route to the previous default gateway
218 * If for some reason a circular route is needed, turn this sysctl (net.inet.ip.check_route_selfref) to zero.
219 */
220 int check_routeselfref = 1;
221 SYSCTL_INT(_net_inet_ip, OID_AUTO, check_route_selfref, CTLFLAG_RW,
222 &check_routeselfref , 0, "");
223 #endif
224
225 __private_extern__ int use_routegenid = 1;
226 SYSCTL_INT(_net_inet_ip, OID_AUTO, use_route_genid, CTLFLAG_RW,
227 &use_routegenid , 0, "");
228
229 /*
230 * On last reference drop, mark the route as belong to us so that it can be
231 * timed out.
232 */
233 static void
234 in_clsroute(struct radix_node *rn, struct radix_node_head *head)
235 {
236 struct rtentry *rt = (struct rtentry *)rn;
237 struct timeval timenow;
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 getmicrotime(&timenow);
256 rt->rt_flags |= RTPRF_OURS;
257 rt->rt_rmx.rmx_expire = timenow.tv_sec + rtq_reallyold;
258 } else {
259 rtrequest_locked(RTM_DELETE,
260 (struct sockaddr *)rt_key(rt),
261 rt->rt_gateway, rt_mask(rt),
262 rt->rt_flags, 0);
263 }
264 }
265
266 struct rtqk_arg {
267 struct radix_node_head *rnh;
268 int draining;
269 int killed;
270 int found;
271 int updating;
272 time_t nextstop;
273 };
274
275 /*
276 * Get rid of old routes. When draining, this deletes everything, even when
277 * the timeout is not expired yet. When updating, this makes sure that
278 * nothing has a timeout longer than the current value of rtq_reallyold.
279 */
280 static int
281 in_rtqkill(struct radix_node *rn, void *rock)
282 {
283 struct rtqk_arg *ap = rock;
284 struct rtentry *rt = (struct rtentry *)rn;
285 int err;
286 struct timeval timenow;
287
288 getmicrotime(&timenow);
289 lck_mtx_assert(rt_mtx, LCK_MTX_ASSERT_OWNED);
290 if(rt->rt_flags & RTPRF_OURS) {
291 ap->found++;
292
293 if(ap->draining || rt->rt_rmx.rmx_expire <= timenow.tv_sec) {
294 if(rt->rt_refcnt > 0)
295 panic("rtqkill route really not free");
296
297 err = rtrequest_locked(RTM_DELETE,
298 (struct sockaddr *)rt_key(rt),
299 rt->rt_gateway, rt_mask(rt),
300 rt->rt_flags, 0);
301 if(err) {
302 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
303 } else {
304 ap->killed++;
305 }
306 } else {
307 if(ap->updating
308 && (rt->rt_rmx.rmx_expire - timenow.tv_sec
309 > rtq_reallyold)) {
310 rt->rt_rmx.rmx_expire = timenow.tv_sec
311 + rtq_reallyold;
312 }
313 ap->nextstop = lmin(ap->nextstop,
314 rt->rt_rmx.rmx_expire);
315 }
316 }
317
318 return 0;
319 }
320
321 static void
322 in_rtqtimo_funnel(void *rock)
323 {
324 in_rtqtimo(rock);
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 struct timeval timenow;
338
339 getmicrotime(&timenow);
340 arg.found = arg.killed = 0;
341 arg.rnh = rnh;
342 arg.nextstop = timenow.tv_sec + rtq_timeout;
343 arg.draining = arg.updating = 0;
344 lck_mtx_lock(rt_mtx);
345 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
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 && (timenow.tv_sec - 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 = timenow.tv_sec;
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 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
371 }
372
373 atv.tv_usec = 0;
374 atv.tv_sec = arg.nextstop - timenow.tv_sec;
375 lck_mtx_unlock(rt_mtx);
376 timeout(in_rtqtimo_funnel, rock, tvtohz(&atv));
377 }
378
379 void
380 in_rtqdrain(void)
381 {
382 struct radix_node_head *rnh = rt_tables[AF_INET];
383 struct rtqk_arg arg;
384 arg.found = arg.killed = 0;
385 arg.rnh = rnh;
386 arg.nextstop = 0;
387 arg.draining = 1;
388 arg.updating = 0;
389 lck_mtx_lock(rt_mtx);
390 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
391 lck_mtx_unlock(rt_mtx);
392 }
393
394 /*
395 * Initialize our routing tree.
396 */
397 int
398 in_inithead(void **head, int off)
399 {
400 struct radix_node_head *rnh;
401
402 #ifdef __APPLE__
403 if (*head)
404 return 1;
405 #endif
406
407 if(!rn_inithead(head, off))
408 return 0;
409
410 if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
411 return 1; /* only do this for the real routing table */
412
413 rnh = *head;
414 rnh->rnh_addaddr = in_addroute;
415 rnh->rnh_matchaddr = in_matroute;
416 rnh->rnh_close = in_clsroute;
417 in_rtqtimo(rnh); /* kick off timeout first time */
418 return 1;
419 }
420
421 \f
422 /*
423 * This zaps old routes when the interface goes down or interface
424 * address is deleted. In the latter case, it deletes static routes
425 * that point to this address. If we don't do this, we may end up
426 * using the old address in the future. The ones we always want to
427 * get rid of are things like ARP entries, since the user might down
428 * the interface, walk over to a completely different network, and
429 * plug back in.
430 */
431 struct in_ifadown_arg {
432 struct radix_node_head *rnh;
433 struct ifaddr *ifa;
434 int del;
435 };
436
437 static int
438 in_ifadownkill(struct radix_node *rn, void *xap)
439 {
440 struct in_ifadown_arg *ap = xap;
441 struct rtentry *rt = (struct rtentry *)rn;
442 int err;
443
444 if (rt->rt_ifa == ap->ifa &&
445 (ap->del || !(rt->rt_flags & RTF_STATIC))) {
446 /*
447 * We need to disable the automatic prune that happens
448 * in this case in rtrequest() because it will blow
449 * away the pointers that rn_walktree() needs in order
450 * continue our descent. We will end up deleting all
451 * the routes that rtrequest() would have in any case,
452 * so that behavior is not needed there.
453 */
454 rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
455 err = rtrequest_locked(RTM_DELETE, (struct sockaddr *)rt_key(rt),
456 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
457 if (err) {
458 log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
459 }
460 }
461 return 0;
462 }
463
464 int
465 in_ifadown(struct ifaddr *ifa, int delete)
466 {
467 struct in_ifadown_arg arg;
468 struct radix_node_head *rnh;
469
470 lck_mtx_assert(rt_mtx, LCK_MTX_ASSERT_OWNED);
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;
477 arg.del = delete;
478 rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
479 ifa->ifa_flags &= ~IFA_ROUTE;
480 return 0;
481 }