]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_rmx.c
f94f673dd3a58d27127979a60a39e065eb2c091c
1 /* $KAME: in6_rmx.c,v 1.6 2000/03/25 07:23:45 sumikawa Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright 1994, 1995 Massachusetts Institute of Technology
35 * Permission to use, copy, modify, and distribute this software and
36 * its documentation for any purpose and without fee is hereby
37 * granted, provided that both the above copyright notice and this
38 * permission notice appear in all copies, that both the above
39 * copyright notice and this permission notice appear in all
40 * supporting documentation, and that the name of M.I.T. not be used
41 * in advertising or publicity pertaining to distribution of the
42 * software without specific, written prior permission. M.I.T. makes
43 * no representations about the suitability of this software for any
44 * purpose. It is provided "as is" without express or implied
47 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
48 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
49 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
51 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * This code does two things necessary for the enhanced TCP metrics to
64 * function in a useful manner:
65 * 1) It marks all non-host routes as `cloning', thus ensuring that
66 * every actual reference to such a route actually gets turned
67 * into a reference to a host route to the specific destination
69 * 2) When such routes lose all their references, it arranges for them
70 * to be deleted in some random collection of circumstances, so that
71 * a large quantity of stale routing data is not kept in kernel memory
72 * indefinitely. See in6_rtqtimo() below for the exact mechanism.
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/sysctl.h>
79 #include <kern/queue.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
83 #include <sys/syslog.h>
86 #include <net/route.h>
87 #include <netinet/in.h>
88 #if defined(__APPLE__)
89 #include <netinet/ip_var.h>
91 #include <netinet/in_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
96 #include <netinet/icmp6.h>
98 #if !defined(__APPLE__)
99 #include <netinet6/tcp6.h>
100 #include <netinet6/tcp6_seq.h>
101 #include <netinet6/tcp6_timer.h>
102 #include <netinet6/tcp6_var.h>
104 #include <netinet/tcp.h>
105 #include <netinet/tcp_seq.h>
106 #include <netinet/tcp_timer.h>
107 #include <netinet/tcp_var.h>
110 #if !defined(__APPLE__)
111 #define tcp_sendspace tcp6_sendspace
112 #define tcp_recvspace tcp6_recvspace
113 #define time_second time.tv_sec
117 extern int in6_inithead
__P((void **head
, int off
));
119 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
122 * Do what we need to do when inserting a route.
124 static struct radix_node
*
125 in6_addroute(void *v_arg
, void *n_arg
, struct radix_node_head
*head
,
126 struct radix_node
*treenodes
)
128 struct rtentry
*rt
= (struct rtentry
*)treenodes
;
129 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)rt_key(rt
);
130 struct radix_node
*ret
;
133 * For IPv6, all unicast non-host routes are automatically cloning.
135 if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
136 rt
->rt_flags
|= RTF_MULTICAST
;
138 if (!(rt
->rt_flags
& (RTF_HOST
| RTF_CLONING
| RTF_MULTICAST
))) {
139 rt
->rt_flags
|= RTF_PRCLONING
;
143 * A little bit of help for both IPv6 output and input:
144 * For local addresses, we make sure that RTF_LOCAL is set,
145 * with the thought that this might one day be used to speed up
148 * We also mark routes to multicast addresses as such, because
149 * it's easy to do and might be useful (but this is much more
150 * dubious since it's so easy to inspect the address). (This
154 * should elaborate the code.
156 if (rt
->rt_flags
& RTF_HOST
) {
157 if (IN6_ARE_ADDR_EQUAL(&satosin6(rt
->rt_ifa
->ifa_addr
)
160 rt
->rt_flags
|= RTF_LOCAL
;
165 * We also specify a send and receive pipe size for every
166 * route added, to help TCP a bit. TCP doesn't actually
167 * want a true pipe size, which would be prohibitive in memory
168 * costs and is hard to compute anyway; it simply uses these
169 * values to size its buffers. So, we fill them in with the
170 * same values that TCP would have used anyway, and allow the
171 * installing program or the link layer to override these values
172 * as it sees fit. This will hopefully allow TCP more
173 * opportunities to save its ssthresh value.
175 if (!rt
->rt_rmx
.rmx_sendpipe
&& !(rt
->rt_rmx
.rmx_locks
& RTV_SPIPE
))
176 rt
->rt_rmx
.rmx_sendpipe
= tcp_sendspace
;
178 if (!rt
->rt_rmx
.rmx_recvpipe
&& !(rt
->rt_rmx
.rmx_locks
& RTV_RPIPE
))
179 rt
->rt_rmx
.rmx_recvpipe
= tcp_recvspace
;
181 if (!rt
->rt_rmx
.rmx_mtu
&& !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
183 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
;
185 ret
= rn_addroute(v_arg
, n_arg
, head
, treenodes
);
186 if (ret
== NULL
&& rt
->rt_flags
& RTF_HOST
) {
189 * We are trying to add a host route, but can't.
190 * Find out if it is because of an
191 * ARP entry and delete it if so.
193 rt2
= rtalloc1((struct sockaddr
*)sin6
, 0,
194 RTF_CLONING
| RTF_PRCLONING
);
196 if (rt2
->rt_flags
& RTF_LLINFO
&&
197 rt2
->rt_flags
& RTF_HOST
&&
199 rt2
->rt_gateway
->sa_family
== AF_LINK
) {
200 rtrequest(RTM_DELETE
,
201 (struct sockaddr
*)rt_key(rt2
),
203 rt_mask(rt2
), rt2
->rt_flags
, 0);
204 ret
= rn_addroute(v_arg
, n_arg
, head
,
209 } else if (ret
== NULL
&& rt
->rt_flags
& RTF_CLONING
) {
212 * We are trying to add a net route, but can't.
213 * The following case should be allowed, so we'll make a
214 * special check for this:
215 * Two IPv6 addresses with the same prefix is assigned
216 * to a single interrface.
217 * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
218 * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
219 * In this case, (*1) and (*2) want to add the same
220 * net route entry, 3ffe:0501:: -> if0.
221 * This case should not raise an error.
223 rt2
= rtalloc1((struct sockaddr
*)sin6
, 0,
224 RTF_CLONING
| RTF_PRCLONING
);
226 if ((rt2
->rt_flags
& (RTF_CLONING
|RTF_HOST
|RTF_GATEWAY
))
229 && rt2
->rt_gateway
->sa_family
== AF_LINK
230 && rt2
->rt_ifp
== rt
->rt_ifp
) {
240 * This code is the inverse of in6_clsroute: on first reference, if we
241 * were managing the route, stop doing so and set the expiration timer
244 static struct radix_node
*
245 in6_matroute(void *v_arg
, struct radix_node_head
*head
)
247 struct radix_node
*rn
= rn_match(v_arg
, head
);
248 struct rtentry
*rt
= (struct rtentry
*)rn
;
250 if (rt
&& rt
->rt_refcnt
== 0) { /* this is first reference */
251 if (rt
->rt_flags
& RTPRF_OURS
) {
252 rt
->rt_flags
&= ~RTPRF_OURS
;
253 rt
->rt_rmx
.rmx_expire
= 0;
259 static int rtq_reallyold
= 60*60;
260 /* one hour is ``really old'' */
262 static int rtq_minreallyold
= 10;
263 /* never automatically crank down to less */
265 static int rtq_toomany
= 128;
266 /* 128 cached routes is ``too many'' */
270 * On last reference drop, mark the route as belong to us so that it can be
274 in6_clsroute(struct radix_node
*rn
, struct radix_node_head
*head
)
276 struct rtentry
*rt
= (struct rtentry
*)rn
;
278 if (!(rt
->rt_flags
& RTF_UP
))
279 return; /* prophylactic measures */
281 if ((rt
->rt_flags
& (RTF_LLINFO
| RTF_HOST
)) != RTF_HOST
)
284 if ((rt
->rt_flags
& (RTF_WASCLONED
| RTPRF_OURS
))
289 * As requested by David Greenman:
290 * If rtq_reallyold is 0, just delete the route without
291 * waiting for a timeout cycle to kill it.
293 if (rtq_reallyold
!= 0) {
294 rt
->rt_flags
|= RTPRF_OURS
;
295 rt
->rt_rmx
.rmx_expire
= time_second
+ rtq_reallyold
;
297 rtrequest(RTM_DELETE
,
298 (struct sockaddr
*)rt_key(rt
),
299 rt
->rt_gateway
, rt_mask(rt
),
305 struct radix_node_head
*rnh
;
315 * Get rid of old routes. When draining, this deletes everything, even when
316 * the timeout is not expired yet. When updating, this makes sure that
317 * nothing has a timeout longer than the current value of rtq_reallyold.
320 in6_rtqkill(struct radix_node
*rn
, void *rock
)
322 struct rtqk_arg
*ap
= rock
;
323 struct rtentry
*rt
= (struct rtentry
*)rn
;
326 if (rt
->rt_flags
& RTPRF_OURS
) {
329 if (ap
->draining
|| rt
->rt_rmx
.rmx_expire
<= time_second
) {
330 if (rt
->rt_refcnt
> 0)
331 panic("rtqkill route really not free");
333 err
= rtrequest(RTM_DELETE
,
334 (struct sockaddr
*)rt_key(rt
),
335 rt
->rt_gateway
, rt_mask(rt
),
338 log(LOG_WARNING
, "in6_rtqkill: error %d", err
);
344 && (rt
->rt_rmx
.rmx_expire
- time_second
346 rt
->rt_rmx
.rmx_expire
= time_second
349 ap
->nextstop
= lmin(ap
->nextstop
,
350 rt
->rt_rmx
.rmx_expire
);
357 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
358 static int rtq_timeout
= RTQ_TIMEOUT
;
361 in6_rtqtimo(void *rock
)
363 struct radix_node_head
*rnh
= rock
;
366 static time_t last_adjusted_timeout
= 0;
369 boolean_t funnel_state
;
370 funnel_state
= thread_set_funneled(TRUE
);
373 arg
.found
= arg
.killed
= 0;
375 arg
.nextstop
= time_second
+ rtq_timeout
;
376 arg
.draining
= arg
.updating
= 0;
378 rnh
->rnh_walktree(rnh
, in6_rtqkill
, &arg
);
382 * Attempt to be somewhat dynamic about this:
383 * If there are ``too many'' routes sitting around taking up space,
384 * then crank down the timeout, and see if we can't make some more
385 * go away. However, we make sure that we will never adjust more
386 * than once in rtq_timeout seconds, to keep from cranking down too
389 if ((arg
.found
- arg
.killed
> rtq_toomany
)
390 && (time_second
- last_adjusted_timeout
>= rtq_timeout
)
391 && rtq_reallyold
> rtq_minreallyold
) {
392 rtq_reallyold
= 2*rtq_reallyold
/ 3;
393 if (rtq_reallyold
< rtq_minreallyold
) {
394 rtq_reallyold
= rtq_minreallyold
;
397 last_adjusted_timeout
= time_second
;
399 log(LOG_DEBUG
, "in6_rtqtimo: adjusted rtq_reallyold to %d",
402 arg
.found
= arg
.killed
= 0;
405 rnh
->rnh_walktree(rnh
, in6_rtqkill
, &arg
);
410 atv
.tv_sec
= arg
.nextstop
;
411 timeout(in6_rtqtimo
, rock
, tvtohz(&atv
));
413 (void) thread_set_funneled(funnel_state
);
421 struct radix_node_head
*rnh
;
426 in6_mtuexpire(struct radix_node
*rn
, void *rock
)
428 struct rtentry
*rt
= (struct rtentry
*)rn
;
429 struct mtuex_arg
*ap
= rock
;
433 panic("rt == NULL in in6_mtuexpire");
435 if (rt
->rt_rmx
.rmx_expire
&& !(rt
->rt_flags
& RTF_PROBEMTU
)) {
436 if (rt
->rt_rmx
.rmx_expire
<= time_second
) {
437 rt
->rt_flags
|= RTF_PROBEMTU
;
439 ap
->nextstop
= lmin(ap
->nextstop
,
440 rt
->rt_rmx
.rmx_expire
);
447 #define MTUTIMO_DEFAULT (60*1)
450 in6_mtutimo(void *rock
)
452 struct radix_node_head
*rnh
= rock
;
453 struct mtuex_arg arg
;
457 boolean_t funnel_state
;
458 funnel_state
= thread_set_funneled(TRUE
);
462 arg
.nextstop
= time_second
+ MTUTIMO_DEFAULT
;
464 rnh
->rnh_walktree(rnh
, in6_mtuexpire
, &arg
);
468 atv
.tv_sec
= arg
.nextstop
;
469 if (atv
.tv_sec
< time_second
) {
470 printf("invalid mtu expiration time on routing table\n");
471 arg
.nextstop
= time_second
+ 30; /*last resort*/
473 timeout(in6_mtutimo
, rock
, tvtohz(&atv
));
475 (void) thread_set_funneled(funnel_state
);
483 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
486 arg
.found
= arg
.killed
= 0;
492 rnh
->rnh_walktree(rnh
, in6_rtqkill
, &arg
);
498 * Initialize our routing tree.
501 in6_inithead(void **head
, int off
)
503 struct radix_node_head
*rnh
;
505 if (!rn_inithead(head
, off
))
508 if (head
!= (void **)&rt_tables
[AF_INET6
]) /* BOGUS! */
509 return 1; /* only do this for the real routing table */
512 rnh
->rnh_addaddr
= in6_addroute
;
513 rnh
->rnh_matchaddr
= in6_matroute
;
514 rnh
->rnh_close
= in6_clsroute
;
515 in6_rtqtimo(rnh
); /* kick off timeout first time */
516 in6_mtutimo(rnh
); /* kick off timeout first time */