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