]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_rmx.c
3a7afc3cdacd500dae06a7a59840b551158bd606
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright 1994, 1995 Massachusetts Institute of Technology
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
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
50 * $FreeBSD: src/sys/netinet/in_rmx.c,v 1.37.2.1 2001/05/14 08:23:49 ru Exp $
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
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.
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>
72 #include <sys/syslog.h>
73 #include <kern/lock.h>
76 #include <net/route.h>
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
80 extern int in_inithead(void **head
, int off
);
83 static void in_rtqtimo(void *rock
);
86 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
87 extern lck_mtx_t
*rt_mtx
;
90 * Do what we need to do when inserting a route.
92 static struct radix_node
*
93 in_addroute(void *v_arg
, void *n_arg
, struct radix_node_head
*head
,
94 struct radix_node
*treenodes
)
96 struct rtentry
*rt
= (struct rtentry
*)treenodes
;
97 struct sockaddr_in
*sin
= (struct sockaddr_in
*)rt_key(rt
);
98 struct radix_node
*ret
;
101 * For IP, all unicast non-host routes are automatically cloning.
103 if(IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
104 rt
->rt_flags
|= RTF_MULTICAST
;
106 if(!(rt
->rt_flags
& (RTF_HOST
| RTF_CLONING
| RTF_MULTICAST
))) {
107 rt
->rt_flags
|= RTF_PRCLONING
;
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).
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().
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
126 if (rt
->rt_flags
& RTF_HOST
) {
127 if (in_broadcast(sin
->sin_addr
, rt
->rt_ifp
)) {
128 rt
->rt_flags
|= RTF_BROADCAST
;
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
;
138 if (!rt
->rt_rmx
.rmx_mtu
&& !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
140 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
142 ret
= rn_addroute(v_arg
, n_arg
, head
, treenodes
);
143 if (ret
== NULL
&& rt
->rt_flags
& RTF_HOST
) {
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.
150 rt2
= rtalloc1_locked((struct sockaddr
*)sin
, 0,
151 RTF_CLONING
| RTF_PRCLONING
);
153 if (rt2
->rt_flags
& RTF_LLINFO
&&
154 rt2
->rt_flags
& RTF_HOST
&&
156 rt2
->rt_gateway
->sa_family
== AF_LINK
) {
157 rtrequest_locked(RTM_DELETE
,
158 (struct sockaddr
*)rt_key(rt2
),
160 rt_mask(rt2
), rt2
->rt_flags
, 0);
161 ret
= rn_addroute(v_arg
, n_arg
, head
,
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
175 static struct radix_node
*
176 in_matroute(void *v_arg
, struct radix_node_head
*head
)
178 struct radix_node
*rn
= rn_match(v_arg
, head
);
179 struct rtentry
*rt
= (struct rtentry
*)rn
;
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;
190 static int rtq_reallyold
= 60*60;
191 /* one hour is ``really old'' */
192 SYSCTL_INT(_net_inet_ip
, IPCTL_RTEXPIRE
, rtexpire
, CTLFLAG_RW
,
194 "Default expiration time on dynamically learned routes");
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");
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");
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.
220 int check_routeselfref
= 1;
221 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_route_selfref
, CTLFLAG_RW
,
222 &check_routeselfref
, 0, "");
225 __private_extern__
int use_routegenid
= 1;
226 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, use_route_genid
, CTLFLAG_RW
,
227 &use_routegenid
, 0, "");
230 * On last reference drop, mark the route as belong to us so that it can be
234 in_clsroute(struct radix_node
*rn
, struct radix_node_head
*head
)
236 struct rtentry
*rt
= (struct rtentry
*)rn
;
237 struct timeval timenow
;
239 if(!(rt
->rt_flags
& RTF_UP
))
240 return; /* prophylactic measures */
242 if((rt
->rt_flags
& (RTF_LLINFO
| RTF_HOST
)) != RTF_HOST
)
245 if((rt
->rt_flags
& (RTF_WASCLONED
| RTPRF_OURS
))
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.
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
;
259 rtrequest_locked(RTM_DELETE
,
260 (struct sockaddr
*)rt_key(rt
),
261 rt
->rt_gateway
, rt_mask(rt
),
267 struct radix_node_head
*rnh
;
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.
281 in_rtqkill(struct radix_node
*rn
, void *rock
)
283 struct rtqk_arg
*ap
= rock
;
284 struct rtentry
*rt
= (struct rtentry
*)rn
;
286 struct timeval timenow
;
288 getmicrotime(&timenow
);
289 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
290 if(rt
->rt_flags
& RTPRF_OURS
) {
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");
297 err
= rtrequest_locked(RTM_DELETE
,
298 (struct sockaddr
*)rt_key(rt
),
299 rt
->rt_gateway
, rt_mask(rt
),
302 log(LOG_WARNING
, "in_rtqkill: error %d\n", err
);
308 && (rt
->rt_rmx
.rmx_expire
- timenow
.tv_sec
310 rt
->rt_rmx
.rmx_expire
= timenow
.tv_sec
313 ap
->nextstop
= lmin(ap
->nextstop
,
314 rt
->rt_rmx
.rmx_expire
);
322 in_rtqtimo_funnel(void *rock
)
327 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
328 static int rtq_timeout
= RTQ_TIMEOUT
;
331 in_rtqtimo(void *rock
)
333 struct radix_node_head
*rnh
= rock
;
336 static time_t last_adjusted_timeout
= 0;
337 struct timeval timenow
;
339 getmicrotime(&timenow
);
340 arg
.found
= arg
.killed
= 0;
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
);
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
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
;
363 last_adjusted_timeout
= timenow
.tv_sec
;
365 log(LOG_DEBUG
, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
368 arg
.found
= arg
.killed
= 0;
370 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
374 atv
.tv_sec
= arg
.nextstop
- timenow
.tv_sec
;
375 lck_mtx_unlock(rt_mtx
);
376 timeout(in_rtqtimo_funnel
, rock
, tvtohz(&atv
));
382 struct radix_node_head
*rnh
= rt_tables
[AF_INET
];
384 arg
.found
= arg
.killed
= 0;
389 lck_mtx_lock(rt_mtx
);
390 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
391 lck_mtx_unlock(rt_mtx
);
395 * Initialize our routing tree.
398 in_inithead(void **head
, int off
)
400 struct radix_node_head
*rnh
;
407 if(!rn_inithead(head
, off
))
410 if(head
!= (void **)&rt_tables
[AF_INET
]) /* BOGUS! */
411 return 1; /* only do this for the real routing table */
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 */
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
431 struct in_ifadown_arg
{
432 struct radix_node_head
*rnh
;
438 in_ifadownkill(struct radix_node
*rn
, void *xap
)
440 struct in_ifadown_arg
*ap
= xap
;
441 struct rtentry
*rt
= (struct rtentry
*)rn
;
444 if (rt
->rt_ifa
== ap
->ifa
&&
445 (ap
->del
|| !(rt
->rt_flags
& RTF_STATIC
))) {
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.
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);
458 log(LOG_WARNING
, "in_ifadownkill: error %d\n", err
);
465 in_ifadown(struct ifaddr
*ifa
, int delete)
467 struct in_ifadown_arg arg
;
468 struct radix_node_head
*rnh
;
470 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
472 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
475 arg
.rnh
= rnh
= rt_tables
[AF_INET
];
478 rnh
->rnh_walktree(rnh
, in_ifadownkill
, &arg
);
479 ifa
->ifa_flags
&= ~IFA_ROUTE
;