]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_rmx.c
c7410a0a357e373d58be6b689da654b924d9e1c2
2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright 1994, 1995 Massachusetts Institute of Technology
31 * Permission to use, copy, modify, and distribute this software and
32 * its documentation for any purpose and without fee is hereby
33 * granted, provided that both the above copyright notice and this
34 * permission notice appear in all copies, that both the above
35 * copyright notice and this permission notice appear in all
36 * supporting documentation, and that the name of M.I.T. not be used
37 * in advertising or publicity pertaining to distribution of the
38 * software without specific, written prior permission. M.I.T. makes
39 * no representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied
43 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
44 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
45 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
46 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
47 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
50 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * $FreeBSD: src/sys/netinet/in_rmx.c,v 1.37.2.1 2001/05/14 08:23:49 ru Exp $
60 * This code does two things necessary for the enhanced TCP metrics to
61 * function in a useful manner:
62 * 1) It marks all non-host routes as `cloning', thus ensuring that
63 * every actual reference to such a route actually gets turned
64 * into a reference to a host route to the specific destination
66 * 2) When such routes lose all their references, it arranges for them
67 * to be deleted in some random collection of circumstances, so that
68 * a large quantity of stale routing data is not kept in kernel memory
69 * indefinitely. See in_rtqtimo() below for the exact mechanism.
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
76 #include <sys/socket.h>
78 #include <sys/syslog.h>
79 #include <kern/lock.h>
82 #include <net/route.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
86 extern int tvtohz(struct timeval
*);
87 extern int in_inithead(void **head
, int off
);
90 static void in_rtqtimo(void *rock
);
93 static struct radix_node
*in_matroute_args(void *, struct radix_node_head
*,
94 rn_matchf_t
*f
, void *);
96 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
99 * Do what we need to do when inserting a route.
101 static struct radix_node
*
102 in_addroute(void *v_arg
, void *n_arg
, struct radix_node_head
*head
,
103 struct radix_node
*treenodes
)
105 struct rtentry
*rt
= (struct rtentry
*)treenodes
;
106 struct sockaddr_in
*sin
= (struct sockaddr_in
*)rt_key(rt
);
107 struct radix_node
*ret
;
109 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
110 RT_LOCK_ASSERT_HELD(rt
);
113 * For IP, all unicast non-host routes are automatically cloning.
115 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
116 rt
->rt_flags
|= RTF_MULTICAST
;
118 if (!(rt
->rt_flags
& (RTF_HOST
| RTF_CLONING
| RTF_MULTICAST
))) {
119 rt
->rt_flags
|= RTF_PRCLONING
;
123 * A little bit of help for both IP output and input:
124 * For host routes, we make sure that RTF_BROADCAST
125 * is set for anything that looks like a broadcast address.
126 * This way, we can avoid an expensive call to in_broadcast()
127 * in ip_output() most of the time (because the route passed
128 * to ip_output() is almost always a host route).
130 * We also do the same for local addresses, with the thought
131 * that this might one day be used to speed up ip_input().
133 * We also mark routes to multicast addresses as such, because
134 * it's easy to do and might be useful (but this is much more
135 * dubious since it's so easy to inspect the address). (This
138 if (rt
->rt_flags
& RTF_HOST
) {
139 if (in_broadcast(sin
->sin_addr
, rt
->rt_ifp
)) {
140 rt
->rt_flags
|= RTF_BROADCAST
;
142 #define satosin(sa) ((struct sockaddr_in *)sa)
143 if (satosin(rt
->rt_ifa
->ifa_addr
)->sin_addr
.s_addr
144 == sin
->sin_addr
.s_addr
)
145 rt
->rt_flags
|= RTF_LOCAL
;
150 if (!rt
->rt_rmx
.rmx_mtu
&& !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
152 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
154 ret
= rn_addroute(v_arg
, n_arg
, head
, treenodes
);
155 if (ret
== NULL
&& rt
->rt_flags
& RTF_HOST
) {
158 * We are trying to add a host route, but can't.
159 * Find out if it is because of an
160 * ARP entry and delete it if so.
162 rt2
= rtalloc1_scoped_locked(rt_key(rt
), 0,
163 RTF_CLONING
| RTF_PRCLONING
, sa_get_ifscope(rt_key(rt
)));
166 if ((rt2
->rt_flags
& RTF_LLINFO
) &&
167 (rt2
->rt_flags
& RTF_HOST
) &&
168 rt2
->rt_gateway
!= NULL
&&
169 rt2
->rt_gateway
->sa_family
== AF_LINK
) {
171 * Safe to drop rt_lock and use rt_key,
172 * rt_gateway, since holding rnh_lock here
173 * prevents another thread from calling
174 * rt_setgate() on this route.
177 rtrequest_locked(RTM_DELETE
, rt_key(rt2
),
178 rt2
->rt_gateway
, rt_mask(rt2
),
180 ret
= rn_addroute(v_arg
, n_arg
, head
,
192 * Validate (unexpire) an expiring AF_INET route.
195 in_validate(struct radix_node
*rn
)
197 struct rtentry
*rt
= (struct rtentry
*)rn
;
199 RT_LOCK_ASSERT_HELD(rt
);
201 /* This is first reference? */
202 if (rt
->rt_refcnt
== 0 && (rt
->rt_flags
& RTPRF_OURS
)) {
203 rt
->rt_flags
&= ~RTPRF_OURS
;
204 rt
->rt_rmx
.rmx_expire
= 0;
210 * Similar to in_matroute_args except without the leaf-matching parameters.
212 static struct radix_node
*
213 in_matroute(void *v_arg
, struct radix_node_head
*head
)
215 return (in_matroute_args(v_arg
, head
, NULL
, NULL
));
219 * This code is the inverse of in_clsroute: on first reference, if we
220 * were managing the route, stop doing so and set the expiration timer
223 static struct radix_node
*
224 in_matroute_args(void *v_arg
, struct radix_node_head
*head
,
225 rn_matchf_t
*f
, void *w
)
227 struct radix_node
*rn
= rn_match_args(v_arg
, head
, f
, w
);
230 RT_LOCK_SPIN((struct rtentry
*)rn
);
232 RT_UNLOCK((struct rtentry
*)rn
);
237 static int rtq_reallyold
= 60*60;
238 /* one hour is ``really old'' */
239 SYSCTL_INT(_net_inet_ip
, IPCTL_RTEXPIRE
, rtexpire
, CTLFLAG_RW
,
241 "Default expiration time on dynamically learned routes");
243 static int rtq_minreallyold
= 10;
244 /* never automatically crank down to less */
245 SYSCTL_INT(_net_inet_ip
, IPCTL_RTMINEXPIRE
, rtminexpire
, CTLFLAG_RW
,
246 &rtq_minreallyold
, 0,
247 "Minimum time to attempt to hold onto dynamically learned routes");
249 static int rtq_toomany
= 128;
250 /* 128 cached routes is ``too many'' */
251 SYSCTL_INT(_net_inet_ip
, IPCTL_RTMAXCACHE
, rtmaxcache
, CTLFLAG_RW
,
252 &rtq_toomany
, 0, "Upper limit on dynamically learned routes");
255 /* XXX LD11JUL02 Special case for AOL 5.1.2 connectivity issue to AirPort BS (Radar 2969954)
256 * AOL is adding a circular route ("10.0.1.1/32 10.0.1.1") when establishing its ppp tunnel
257 * to the AP BaseStation by removing the default gateway and replacing it with their tunnel entry point.
258 * There is no apparent reason to add this route as there is a valid 10.0.1.1/24 route to the BS.
259 * That circular route was ignored on previous version of MacOS X because of a routing bug
260 * corrected with the merge to FreeBSD4.4 (a route generated from an RTF_CLONING route had the RTF_WASCLONED
261 * flag set but did not have a reference to the parent route) and that entry was left in the RT. This workaround is
262 * made in order to provide binary compatibility with AOL.
263 * If we catch a process adding a circular route with a /32 from the routing socket, we error it out instead of
264 * confusing the routing table with a wrong route to the previous default gateway
265 * If for some reason a circular route is needed, turn this sysctl (net.inet.ip.check_route_selfref) to zero.
267 int check_routeselfref
= 1;
268 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_route_selfref
, CTLFLAG_RW
,
269 &check_routeselfref
, 0, "");
272 int use_routegenid
= 1;
273 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, use_route_genid
, CTLFLAG_RW
,
274 &use_routegenid
, 0, "");
277 * On last reference drop, mark the route as belong to us so that it can be
281 in_clsroute(struct radix_node
*rn
, __unused
struct radix_node_head
*head
)
283 struct rtentry
*rt
= (struct rtentry
*)rn
;
285 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
286 RT_LOCK_ASSERT_HELD(rt
);
288 if (!(rt
->rt_flags
& RTF_UP
))
289 return; /* prophylactic measures */
291 if ((rt
->rt_flags
& (RTF_LLINFO
| RTF_HOST
)) != RTF_HOST
)
294 if ((rt
->rt_flags
& (RTF_WASCLONED
| RTPRF_OURS
)) != RTF_WASCLONED
)
298 * Delete the route immediately if RTF_DELCLONE is set or
299 * if route caching is disabled (rtq_reallyold set to 0).
300 * Otherwise, let it expire and be deleted by in_rtqkill().
302 if ((rt
->rt_flags
& RTF_DELCLONE
) || rtq_reallyold
== 0) {
304 * Delete the route from the radix tree but since we are
305 * called when the route's reference count is 0, don't
306 * deallocate it until we return from this routine by
307 * telling rtrequest that we're interested in it.
308 * Safe to drop rt_lock and use rt_key, rt_gateway since
309 * holding rnh_lock here prevents another thread from
310 * calling rt_setgate() on this route.
313 if (rtrequest_locked(RTM_DELETE
, (struct sockaddr
*)rt_key(rt
),
314 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, &rt
) == 0) {
315 /* Now let the caller free it */
317 RT_REMREF_LOCKED(rt
);
322 struct timeval timenow
;
324 getmicrotime(&timenow
);
325 rt
->rt_flags
|= RTPRF_OURS
;
326 rt
->rt_rmx
.rmx_expire
= timenow
.tv_sec
+ rtq_reallyold
;
331 struct radix_node_head
*rnh
;
340 * Get rid of old routes. When draining, this deletes everything, even when
341 * the timeout is not expired yet. When updating, this makes sure that
342 * nothing has a timeout longer than the current value of rtq_reallyold.
345 in_rtqkill(struct radix_node
*rn
, void *rock
)
347 struct rtqk_arg
*ap
= rock
;
348 struct rtentry
*rt
= (struct rtentry
*)rn
;
350 struct timeval timenow
;
352 getmicrotime(&timenow
);
353 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
356 if (rt
->rt_flags
& RTPRF_OURS
) {
359 if (ap
->draining
|| rt
->rt_rmx
.rmx_expire
<= timenow
.tv_sec
) {
360 if (rt
->rt_refcnt
> 0)
361 panic("rtqkill route really not free");
364 * Delete this route since we're done with it;
365 * the route may be freed afterwards, so we
366 * can no longer refer to 'rt' upon returning
367 * from rtrequest(). Safe to drop rt_lock and
368 * use rt_key, rt_gateway since holding rnh_lock
369 * here prevents another thread from calling
370 * rt_setgate() on this route.
373 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
),
374 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, 0);
376 log(LOG_WARNING
, "in_rtqkill: error %d\n", err
);
382 && (rt
->rt_rmx
.rmx_expire
- timenow
.tv_sec
384 rt
->rt_rmx
.rmx_expire
= timenow
.tv_sec
387 ap
->nextstop
= lmin(ap
->nextstop
,
388 rt
->rt_rmx
.rmx_expire
);
399 in_rtqtimo_funnel(void *rock
)
404 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
405 static int rtq_timeout
= RTQ_TIMEOUT
;
408 in_rtqtimo(void *rock
)
410 struct radix_node_head
*rnh
= rock
;
413 static time_t last_adjusted_timeout
= 0;
414 struct timeval timenow
;
416 lck_mtx_lock(rnh_lock
);
417 /* Get the timestamp after we acquire the lock for better accuracy */
418 getmicrotime(&timenow
);
420 arg
.found
= arg
.killed
= 0;
422 arg
.nextstop
= timenow
.tv_sec
+ rtq_timeout
;
423 arg
.draining
= arg
.updating
= 0;
424 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
427 * Attempt to be somewhat dynamic about this:
428 * If there are ``too many'' routes sitting around taking up space,
429 * then crank down the timeout, and see if we can't make some more
430 * go away. However, we make sure that we will never adjust more
431 * than once in rtq_timeout seconds, to keep from cranking down too
434 if((arg
.found
- arg
.killed
> rtq_toomany
)
435 && (timenow
.tv_sec
- last_adjusted_timeout
>= rtq_timeout
)
436 && rtq_reallyold
> rtq_minreallyold
) {
437 rtq_reallyold
= 2*rtq_reallyold
/ 3;
438 if(rtq_reallyold
< rtq_minreallyold
) {
439 rtq_reallyold
= rtq_minreallyold
;
442 last_adjusted_timeout
= timenow
.tv_sec
;
444 log(LOG_DEBUG
, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
447 arg
.found
= arg
.killed
= 0;
449 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
453 atv
.tv_sec
= arg
.nextstop
- timenow
.tv_sec
;
454 lck_mtx_unlock(rnh_lock
);
455 timeout(in_rtqtimo_funnel
, rock
, tvtohz(&atv
));
461 struct radix_node_head
*rnh
= rt_tables
[AF_INET
];
463 arg
.found
= arg
.killed
= 0;
468 lck_mtx_lock(rnh_lock
);
469 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
470 lck_mtx_unlock(rnh_lock
);
474 * Initialize our routing tree.
477 in_inithead(void **head
, int off
)
479 struct radix_node_head
*rnh
;
486 if(!rn_inithead(head
, off
))
489 if(head
!= (void **)&rt_tables
[AF_INET
]) /* BOGUS! */
490 return 1; /* only do this for the real routing table */
493 rnh
->rnh_addaddr
= in_addroute
;
494 rnh
->rnh_matchaddr
= in_matroute
;
495 rnh
->rnh_matchaddr_args
= in_matroute_args
;
496 rnh
->rnh_close
= in_clsroute
;
497 in_rtqtimo(rnh
); /* kick off timeout first time */
503 * This zaps old routes when the interface goes down or interface
504 * address is deleted. In the latter case, it deletes static routes
505 * that point to this address. If we don't do this, we may end up
506 * using the old address in the future. The ones we always want to
507 * get rid of are things like ARP entries, since the user might down
508 * the interface, walk over to a completely different network, and
511 struct in_ifadown_arg
{
512 struct radix_node_head
*rnh
;
518 in_ifadownkill(struct radix_node
*rn
, void *xap
)
520 struct in_ifadown_arg
*ap
= xap
;
521 struct rtentry
*rt
= (struct rtentry
*)rn
;
525 if (rt
->rt_ifa
== ap
->ifa
&&
526 (ap
->del
|| !(rt
->rt_flags
& RTF_STATIC
))) {
528 * We need to disable the automatic prune that happens
529 * in this case in rtrequest() because it will blow
530 * away the pointers that rn_walktree() needs in order
531 * continue our descent. We will end up deleting all
532 * the routes that rtrequest() would have in any case,
533 * so that behavior is not needed there. Safe to drop
534 * rt_lock and use rt_key, rt_gateway, since holding
535 * rnh_lock here prevents another thread from calling
536 * rt_setgate() on this route.
538 rt
->rt_flags
&= ~(RTF_CLONING
| RTF_PRCLONING
);
540 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
),
541 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, 0);
543 log(LOG_WARNING
, "in_ifadownkill: error %d\n", err
);
552 in_ifadown(struct ifaddr
*ifa
, int delete)
554 struct in_ifadown_arg arg
;
555 struct radix_node_head
*rnh
;
557 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
559 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
562 /* trigger route cache reevaluation */
566 arg
.rnh
= rnh
= rt_tables
[AF_INET
];
569 rnh
->rnh_walktree(rnh
, in_ifadownkill
, &arg
);
570 ifa
->ifa_flags
&= ~IFA_ROUTE
;