]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_rmx.c
f1a41c2f6e1c8dc80ee910b5a64f5f9f48740df1
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
53 * This code does two things necessary for the enhanced TCP metrics to
54 * function in a useful manner:
55 * 1) It marks all non-host routes as `cloning', thus ensuring that
56 * every actual reference to such a route actually gets turned
57 * into a reference to a host route to the specific destination
59 * 2) When such routes lose all their references, it arranges for them
60 * to be deleted in some random collection of circumstances, so that
61 * a large quantity of stale routing data is not kept in kernel memory
62 * indefinitely. See in_rtqtimo() below for the exact mechanism.
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/sysctl.h>
69 #include <sys/socket.h>
71 #include <sys/syslog.h>
74 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
78 extern int in_inithead
__P((void **head
, int off
));
80 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
83 * Do what we need to do when inserting a route.
85 static struct radix_node
*
86 in_addroute(void *v_arg
, void *n_arg
, struct radix_node_head
*head
,
87 struct radix_node
*treenodes
)
89 struct rtentry
*rt
= (struct rtentry
*)treenodes
;
90 struct sockaddr_in
*sin
= (struct sockaddr_in
*)rt_key(rt
);
91 struct radix_node
*ret
;
94 * For IP, all unicast non-host routes are automatically cloning.
96 if(IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
97 rt
->rt_flags
|= RTF_MULTICAST
;
99 if(!(rt
->rt_flags
& (RTF_HOST
| RTF_CLONING
| RTF_MULTICAST
))) {
100 rt
->rt_flags
|= RTF_PRCLONING
;
104 * A little bit of help for both IP output and input:
105 * For host routes, we make sure that RTF_BROADCAST
106 * is set for anything that looks like a broadcast address.
107 * This way, we can avoid an expensive call to in_broadcast()
108 * in ip_output() most of the time (because the route passed
109 * to ip_output() is almost always a host route).
111 * We also do the same for local addresses, with the thought
112 * that this might one day be used to speed up ip_input().
114 * We also mark routes to multicast addresses as such, because
115 * it's easy to do and might be useful (but this is much more
116 * dubious since it's so easy to inspect the address). (This
119 if (rt
->rt_flags
& RTF_HOST
) {
120 if (in_broadcast(sin
->sin_addr
, rt
->rt_ifp
)) {
121 rt
->rt_flags
|= RTF_BROADCAST
;
123 #define satosin(sa) ((struct sockaddr_in *)sa)
124 if (satosin(rt
->rt_ifa
->ifa_addr
)->sin_addr
.s_addr
125 == sin
->sin_addr
.s_addr
)
126 rt
->rt_flags
|= RTF_LOCAL
;
131 if (!rt
->rt_rmx
.rmx_mtu
&& !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
133 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
135 ret
= rn_addroute(v_arg
, n_arg
, head
, treenodes
);
136 if (ret
== NULL
&& rt
->rt_flags
& RTF_HOST
) {
139 * We are trying to add a host route, but can't.
140 * Find out if it is because of an
141 * ARP entry and delete it if so.
143 rt2
= rtalloc1((struct sockaddr
*)sin
, 0,
144 RTF_CLONING
| RTF_PRCLONING
);
146 if (rt2
->rt_flags
& RTF_LLINFO
&&
147 rt2
->rt_flags
& RTF_HOST
&&
149 rt2
->rt_gateway
->sa_family
== AF_LINK
) {
150 rtrequest(RTM_DELETE
,
151 (struct sockaddr
*)rt_key(rt2
),
153 rt_mask(rt2
), rt2
->rt_flags
, 0);
154 ret
= rn_addroute(v_arg
, n_arg
, head
,
164 * This code is the inverse of in_clsroute: on first reference, if we
165 * were managing the route, stop doing so and set the expiration timer
168 static struct radix_node
*
169 in_matroute(void *v_arg
, struct radix_node_head
*head
)
171 struct radix_node
*rn
= rn_match(v_arg
, head
);
172 struct rtentry
*rt
= (struct rtentry
*)rn
;
174 if(rt
&& rt
->rt_refcnt
== 0) { /* this is first reference */
175 if(rt
->rt_flags
& RTPRF_OURS
) {
176 rt
->rt_flags
&= ~RTPRF_OURS
;
177 rt
->rt_rmx
.rmx_expire
= 0;
183 int rtq_reallyold
= 60*60;
184 /* one hour is ``really old'' */
185 SYSCTL_INT(_net_inet_ip
, IPCTL_RTEXPIRE
, rtexpire
,
186 CTLFLAG_RW
, &rtq_reallyold
, 0, "");
188 int rtq_minreallyold
= 10;
189 /* never automatically crank down to less */
190 SYSCTL_INT(_net_inet_ip
, IPCTL_RTMINEXPIRE
, rtminexpire
,
191 CTLFLAG_RW
, &rtq_minreallyold
, 0, "");
193 int rtq_toomany
= 128;
194 /* 128 cached routes is ``too many'' */
195 SYSCTL_INT(_net_inet_ip
, IPCTL_RTMAXCACHE
, rtmaxcache
,
196 CTLFLAG_RW
, &rtq_toomany
, 0, "");
200 * On last reference drop, mark the route as belong to us so that it can be
204 in_clsroute(struct radix_node
*rn
, struct radix_node_head
*head
)
206 struct rtentry
*rt
= (struct rtentry
*)rn
;
208 if(!(rt
->rt_flags
& RTF_UP
))
209 return; /* prophylactic measures */
211 if((rt
->rt_flags
& (RTF_LLINFO
| RTF_HOST
)) != RTF_HOST
)
214 if((rt
->rt_flags
& (RTF_WASCLONED
| RTPRF_OURS
))
219 * As requested by David Greenman:
220 * If rtq_reallyold is 0, just delete the route without
221 * waiting for a timeout cycle to kill it.
223 if(rtq_reallyold
!= 0) {
224 rt
->rt_flags
|= RTPRF_OURS
;
225 rt
->rt_rmx
.rmx_expire
= time_second
+ rtq_reallyold
;
227 rtrequest(RTM_DELETE
,
228 (struct sockaddr
*)rt_key(rt
),
229 rt
->rt_gateway
, rt_mask(rt
),
235 struct radix_node_head
*rnh
;
244 * Get rid of old routes. When draining, this deletes everything, even when
245 * the timeout is not expired yet. When updating, this makes sure that
246 * nothing has a timeout longer than the current value of rtq_reallyold.
249 in_rtqkill(struct radix_node
*rn
, void *rock
)
251 struct rtqk_arg
*ap
= rock
;
252 struct rtentry
*rt
= (struct rtentry
*)rn
;
255 if(rt
->rt_flags
& RTPRF_OURS
) {
258 if(ap
->draining
|| rt
->rt_rmx
.rmx_expire
<= time_second
) {
259 if(rt
->rt_refcnt
> 0)
260 panic("rtqkill route really not free");
262 err
= rtrequest(RTM_DELETE
,
263 (struct sockaddr
*)rt_key(rt
),
264 rt
->rt_gateway
, rt_mask(rt
),
267 log(LOG_WARNING
, "in_rtqkill: error %d\n", err
);
273 && (rt
->rt_rmx
.rmx_expire
- time_second
275 rt
->rt_rmx
.rmx_expire
= time_second
278 ap
->nextstop
= lmin(ap
->nextstop
,
279 rt
->rt_rmx
.rmx_expire
);
287 in_rtqtimo_funnel(void *rock
)
289 boolean_t funnel_state
;
291 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
293 (void) thread_funnel_set(network_flock
, FALSE
);
296 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
297 static int rtq_timeout
= RTQ_TIMEOUT
;
300 in_rtqtimo(void *rock
)
302 struct radix_node_head
*rnh
= rock
;
305 static time_t last_adjusted_timeout
= 0;
308 arg
.found
= arg
.killed
= 0;
310 arg
.nextstop
= time_second
+ rtq_timeout
;
311 arg
.draining
= arg
.updating
= 0;
313 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
317 * Attempt to be somewhat dynamic about this:
318 * If there are ``too many'' routes sitting around taking up space,
319 * then crank down the timeout, and see if we can't make some more
320 * go away. However, we make sure that we will never adjust more
321 * than once in rtq_timeout seconds, to keep from cranking down too
324 if((arg
.found
- arg
.killed
> rtq_toomany
)
325 && (time_second
- last_adjusted_timeout
>= rtq_timeout
)
326 && rtq_reallyold
> rtq_minreallyold
) {
327 rtq_reallyold
= 2*rtq_reallyold
/ 3;
328 if(rtq_reallyold
< rtq_minreallyold
) {
329 rtq_reallyold
= rtq_minreallyold
;
332 last_adjusted_timeout
= time_second
;
334 log(LOG_DEBUG
, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
337 arg
.found
= arg
.killed
= 0;
340 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
345 atv
.tv_sec
= arg
.nextstop
- time_second
;
346 timeout(in_rtqtimo_funnel
, rock
, tvtohz(&atv
));
353 struct radix_node_head
*rnh
= rt_tables
[AF_INET
];
356 arg
.found
= arg
.killed
= 0;
362 rnh
->rnh_walktree(rnh
, in_rtqkill
, &arg
);
367 * Initialize our routing tree.
370 in_inithead(void **head
, int off
)
372 struct radix_node_head
*rnh
;
377 if(!rn_inithead(head
, off
))
380 if(head
!= (void **)&rt_tables
[AF_INET
]) /* BOGUS! */
381 return 1; /* only do this for the real routing table */
384 rnh
->rnh_addaddr
= in_addroute
;
385 rnh
->rnh_matchaddr
= in_matroute
;
386 rnh
->rnh_close
= in_clsroute
;
387 in_rtqtimo(rnh
); /* kick off timeout first time */
393 * This zaps old routes when the interface goes down.
394 * Currently it doesn't delete static routes; there are
395 * arguments one could make for both behaviors. For the moment,
396 * we will adopt the Principle of Least Surprise and leave them
397 * alone (with the knowledge that this will not be enough for some
398 * people). The ones we really want to get rid of are things like ARP
399 * entries, since the user might down the interface, walk over to a completely
400 * different network, and plug back in.
402 struct in_ifadown_arg
{
403 struct radix_node_head
*rnh
;
408 in_ifadownkill(struct radix_node
*rn
, void *xap
)
410 struct in_ifadown_arg
*ap
= xap
;
411 struct rtentry
*rt
= (struct rtentry
*)rn
;
414 if (rt
->rt_ifa
== ap
->ifa
&& !(rt
->rt_flags
& RTF_STATIC
)) {
416 * We need to disable the automatic prune that happens
417 * in this case in rtrequest() because it will blow
418 * away the pointers that rn_walktree() needs in order
419 * continue our descent. We will end up deleting all
420 * the routes that rtrequest() would have in any case,
421 * so that behavior is not needed there.
423 rt
->rt_flags
&= ~RTF_PRCLONING
;
424 err
= rtrequest(RTM_DELETE
, (struct sockaddr
*)rt_key(rt
),
425 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, 0);
427 log(LOG_WARNING
, "in_ifadownkill: error %d\n", err
);
434 in_ifadown(struct ifaddr
*ifa
)
436 struct in_ifadown_arg arg
;
437 struct radix_node_head
*rnh
;
439 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
442 arg
.rnh
= rnh
= rt_tables
[AF_INET
];
444 rnh
->rnh_walktree(rnh
, in_ifadownkill
, &arg
);
445 ifa
->ifa_flags
&= ~IFA_ROUTE
;