]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_rmx.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1994, 1995 Massachusetts Institute of Technology
26 * Permission to use, copy, modify, and distribute this software and
27 * its documentation for any purpose and without fee is hereby
28 * granted, provided that both the above copyright notice and this
29 * permission notice appear in all copies, that both the above
30 * copyright notice and this permission notice appear in all
31 * supporting documentation, and that the name of M.I.T. not be used
32 * in advertising or publicity pertaining to distribution of the
33 * software without specific, written prior permission. M.I.T. makes
34 * no representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied
38 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
39 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
40 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
42 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * $FreeBSD: src/sys/netinet/in_rmx.c,v 1.37.2.1 2001/05/14 08:23:49 ru Exp $
55 * This code does two things necessary for the enhanced TCP metrics to
56 * function in a useful manner:
57 * 1) It marks all non-host routes as `cloning', thus ensuring that
58 * every actual reference to such a route actually gets turned
59 * into a reference to a host route to the specific destination
61 * 2) When such routes lose all their references, it arranges for them
62 * to be deleted in some random collection of circumstances, so that
63 * a large quantity of stale routing data is not kept in kernel memory
64 * indefinitely. See in_rtqtimo() below for the exact mechanism.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/sysctl.h>
71 #include <sys/socket.h>
73 #include <sys/syslog.h>
74 #include <kern/lock.h>
77 #include <net/route.h>
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
81 extern int in_inithead(void **head
, int off
);
84 static void in_rtqtimo(void *rock
);
87 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
88 extern lck_mtx_t
*rt_mtx
;
91 * Do what we need to do when inserting a route.
93 static struct radix_node
*
94 in_addroute(void *v_arg
, void *n_arg
, struct radix_node_head
*head
,
95 struct radix_node
*treenodes
)
97 struct rtentry
*rt
= (struct rtentry
*)treenodes
;
98 struct sockaddr_in
*sin
= (struct sockaddr_in
*)rt_key(rt
);
99 struct radix_node
*ret
;
102 * For IP, all unicast non-host routes are automatically cloning.
104 if(IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
105 rt
->rt_flags
|= RTF_MULTICAST
;
107 if(!(rt
->rt_flags
& (RTF_HOST
| RTF_CLONING
| RTF_MULTICAST
))) {
108 rt
->rt_flags
|= RTF_PRCLONING
;
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).
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().
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
127 if (rt
->rt_flags
& RTF_HOST
) {
128 if (in_broadcast(sin
->sin_addr
, rt
->rt_ifp
)) {
129 rt
->rt_flags
|= RTF_BROADCAST
;
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
;
139 if (!rt
->rt_rmx
.rmx_mtu
&& !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
141 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
143 ret
= rn_addroute(v_arg
, n_arg
, head
, treenodes
);
144 if (ret
== NULL
&& rt
->rt_flags
& RTF_HOST
) {
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.
151 rt2
= rtalloc1_locked((struct sockaddr
*)sin
, 0,
152 RTF_CLONING
| RTF_PRCLONING
);
154 if (rt2
->rt_flags
& RTF_LLINFO
&&
155 rt2
->rt_flags
& RTF_HOST
&&
157 rt2
->rt_gateway
->sa_family
== AF_LINK
) {
158 rtrequest_locked(RTM_DELETE
,
159 (struct sockaddr
*)rt_key(rt2
),
161 rt_mask(rt2
), rt2
->rt_flags
, 0);
162 ret
= rn_addroute(v_arg
, n_arg
, head
,
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
176 static struct radix_node
*
177 in_matroute(void *v_arg
, struct radix_node_head
*head
)
179 struct radix_node
*rn
= rn_match(v_arg
, head
);
180 struct rtentry
*rt
= (struct rtentry
*)rn
;
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;
191 static int rtq_reallyold
= 60*60;
192 /* one hour is ``really old'' */
193 SYSCTL_INT(_net_inet_ip
, IPCTL_RTEXPIRE
, rtexpire
, CTLFLAG_RW
,
195 "Default expiration time on dynamically learned routes");
197 static int rtq_minreallyold
= 10;
198 /* never automatically crank down to less */
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");
203 static int rtq_toomany
= 128;
204 /* 128 cached routes is ``too many'' */
205 SYSCTL_INT(_net_inet_ip
, IPCTL_RTMAXCACHE
, rtmaxcache
, CTLFLAG_RW
,
206 &rtq_toomany
, 0, "Upper limit on dynamically learned routes");
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.
221 int check_routeselfref
= 1;
222 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_route_selfref
, CTLFLAG_RW
,
223 &check_routeselfref
, 0, "");
226 __private_extern__
int use_routegenid
= 1;
227 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, use_route_genid
, CTLFLAG_RW
,
228 &use_routegenid
, 0, "");
231 * On last reference drop, mark the route as belong to us so that it can be
235 in_clsroute(struct radix_node
*rn
, struct radix_node_head
*head
)
237 struct rtentry
*rt
= (struct rtentry
*)rn
;
238 struct timeval timenow
;
240 if(!(rt
->rt_flags
& RTF_UP
))
241 return; /* prophylactic measures */
243 if((rt
->rt_flags
& (RTF_LLINFO
| RTF_HOST
)) != RTF_HOST
)
246 if((rt
->rt_flags
& (RTF_WASCLONED
| RTPRF_OURS
))
251 * As requested by David Greenman:
252 * If rtq_reallyold is 0, just delete the route without
253 * waiting for a timeout cycle to kill it.
255 if(rtq_reallyold
!= 0) {
256 getmicrotime(&timenow
);
257 rt
->rt_flags
|= RTPRF_OURS
;
258 rt
->rt_rmx
.rmx_expire
= timenow
.tv_sec
+ rtq_reallyold
;
260 rtrequest_locked(RTM_DELETE
,
261 (struct sockaddr
*)rt_key(rt
),
262 rt
->rt_gateway
, rt_mask(rt
),
268 struct radix_node_head
*rnh
;
277 * Get rid of old routes. When draining, this deletes everything, even when
278 * the timeout is not expired yet. When updating, this makes sure that
279 * nothing has a timeout longer than the current value of rtq_reallyold.
282 in_rtqkill(struct radix_node
*rn
, void *rock
)
284 struct rtqk_arg
*ap
= rock
;
285 struct rtentry
*rt
= (struct rtentry
*)rn
;
287 struct timeval timenow
;
289 getmicrotime(&timenow
);
290 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
291 if(rt
->rt_flags
& RTPRF_OURS
) {
294 if(ap
->draining
|| rt
->rt_rmx
.rmx_expire
<= timenow
.tv_sec
) {
295 if(rt
->rt_refcnt
> 0)
296 panic("rtqkill route really not free");
298 err
= rtrequest_locked(RTM_DELETE
,
299 (struct sockaddr
*)rt_key(rt
),
300 rt
->rt_gateway
, rt_mask(rt
),
303 log(LOG_WARNING
, "in_rtqkill: error %d\n", err
);
309 && (rt
->rt_rmx
.rmx_expire
- timenow
.tv_sec
311 rt
->rt_rmx
.rmx_expire
= timenow
.tv_sec
314 ap
->nextstop
= lmin(ap
->nextstop
,
315 rt
->rt_rmx
.rmx_expire
);
323 in_rtqtimo_funnel(void *rock
)
328 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
329 static int rtq_timeout
= RTQ_TIMEOUT
;
332 in_rtqtimo(void *rock
)
334 struct radix_node_head
*rnh
= rock
;
337 static time_t last_adjusted_timeout
= 0;
338 struct timeval timenow
;
340 getmicrotime(&timenow
);
341 arg
.found
= arg
.killed
= 0;
343 arg
.nextstop
= timenow
.tv_sec
+ rtq_timeout
;
344 arg
.draining
= arg
.updating
= 0;
345 lck_mtx_lock(rt_mtx
);
346 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
349 * Attempt to be somewhat dynamic about this:
350 * If there are ``too many'' routes sitting around taking up space,
351 * then crank down the timeout, and see if we can't make some more
352 * go away. However, we make sure that we will never adjust more
353 * than once in rtq_timeout seconds, to keep from cranking down too
356 if((arg
.found
- arg
.killed
> rtq_toomany
)
357 && (timenow
.tv_sec
- last_adjusted_timeout
>= rtq_timeout
)
358 && rtq_reallyold
> rtq_minreallyold
) {
359 rtq_reallyold
= 2*rtq_reallyold
/ 3;
360 if(rtq_reallyold
< rtq_minreallyold
) {
361 rtq_reallyold
= rtq_minreallyold
;
364 last_adjusted_timeout
= timenow
.tv_sec
;
366 log(LOG_DEBUG
, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
369 arg
.found
= arg
.killed
= 0;
371 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
375 atv
.tv_sec
= arg
.nextstop
- timenow
.tv_sec
;
376 lck_mtx_unlock(rt_mtx
);
377 timeout(in_rtqtimo_funnel
, rock
, tvtohz(&atv
));
383 struct radix_node_head
*rnh
= rt_tables
[AF_INET
];
385 arg
.found
= arg
.killed
= 0;
390 lck_mtx_lock(rt_mtx
);
391 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
392 lck_mtx_unlock(rt_mtx
);
396 * Initialize our routing tree.
399 in_inithead(void **head
, int off
)
401 struct radix_node_head
*rnh
;
408 if(!rn_inithead(head
, off
))
411 if(head
!= (void **)&rt_tables
[AF_INET
]) /* BOGUS! */
412 return 1; /* only do this for the real routing table */
415 rnh
->rnh_addaddr
= in_addroute
;
416 rnh
->rnh_matchaddr
= in_matroute
;
417 rnh
->rnh_close
= in_clsroute
;
418 in_rtqtimo(rnh
); /* kick off timeout first time */
424 * This zaps old routes when the interface goes down or interface
425 * address is deleted. In the latter case, it deletes static routes
426 * that point to this address. If we don't do this, we may end up
427 * using the old address in the future. The ones we always want to
428 * get rid of are things like ARP entries, since the user might down
429 * the interface, walk over to a completely different network, and
432 struct in_ifadown_arg
{
433 struct radix_node_head
*rnh
;
439 in_ifadownkill(struct radix_node
*rn
, void *xap
)
441 struct in_ifadown_arg
*ap
= xap
;
442 struct rtentry
*rt
= (struct rtentry
*)rn
;
445 if (rt
->rt_ifa
== ap
->ifa
&&
446 (ap
->del
|| !(rt
->rt_flags
& RTF_STATIC
))) {
448 * We need to disable the automatic prune that happens
449 * in this case in rtrequest() because it will blow
450 * away the pointers that rn_walktree() needs in order
451 * continue our descent. We will end up deleting all
452 * the routes that rtrequest() would have in any case,
453 * so that behavior is not needed there.
455 rt
->rt_flags
&= ~(RTF_CLONING
| RTF_PRCLONING
);
456 err
= rtrequest_locked(RTM_DELETE
, (struct sockaddr
*)rt_key(rt
),
457 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, 0);
459 log(LOG_WARNING
, "in_ifadownkill: error %d\n", err
);
466 in_ifadown(struct ifaddr
*ifa
, int delete)
468 struct in_ifadown_arg arg
;
469 struct radix_node_head
*rnh
;
471 lck_mtx_assert(rt_mtx
, LCK_MTX_ASSERT_OWNED
);
473 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
476 arg
.rnh
= rnh
= rt_tables
[AF_INET
];
479 rnh
->rnh_walktree(rnh
, in_ifadownkill
, &arg
);
480 ifa
->ifa_flags
&= ~IFA_ROUTE
;