]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_rmx.c
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_funneled(void *rock
)
364 boolean_t funnel_state
;
365 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
369 (void) thread_funnel_set(network_flock
, FALSE
);
374 in6_rtqtimo(void *rock
)
376 struct radix_node_head
*rnh
= rock
;
379 static time_t last_adjusted_timeout
= 0;
382 arg
.found
= arg
.killed
= 0;
384 arg
.nextstop
= time_second
+ rtq_timeout
;
385 arg
.draining
= arg
.updating
= 0;
387 rnh
->rnh_walktree(rnh
, in6_rtqkill
, &arg
);
391 * Attempt to be somewhat dynamic about this:
392 * If there are ``too many'' routes sitting around taking up space,
393 * then crank down the timeout, and see if we can't make some more
394 * go away. However, we make sure that we will never adjust more
395 * than once in rtq_timeout seconds, to keep from cranking down too
398 if ((arg
.found
- arg
.killed
> rtq_toomany
)
399 && (time_second
- last_adjusted_timeout
>= rtq_timeout
)
400 && rtq_reallyold
> rtq_minreallyold
) {
401 rtq_reallyold
= 2*rtq_reallyold
/ 3;
402 if (rtq_reallyold
< rtq_minreallyold
) {
403 rtq_reallyold
= rtq_minreallyold
;
406 last_adjusted_timeout
= time_second
;
408 log(LOG_DEBUG
, "in6_rtqtimo: adjusted rtq_reallyold to %d",
411 arg
.found
= arg
.killed
= 0;
414 rnh
->rnh_walktree(rnh
, in6_rtqkill
, &arg
);
419 atv
.tv_sec
= arg
.nextstop
;
420 timeout(in6_rtqtimo_funneled
, rock
, tvtohz(&atv
));
427 struct radix_node_head
*rnh
;
432 in6_mtuexpire(struct radix_node
*rn
, void *rock
)
434 struct rtentry
*rt
= (struct rtentry
*)rn
;
435 struct mtuex_arg
*ap
= rock
;
439 panic("rt == NULL in in6_mtuexpire");
441 if (rt
->rt_rmx
.rmx_expire
&& !(rt
->rt_flags
& RTF_PROBEMTU
)) {
442 if (rt
->rt_rmx
.rmx_expire
<= time_second
) {
443 rt
->rt_flags
|= RTF_PROBEMTU
;
445 ap
->nextstop
= lmin(ap
->nextstop
,
446 rt
->rt_rmx
.rmx_expire
);
453 #define MTUTIMO_DEFAULT (60*1)
456 in6_mtutimo_funneled(void *rock
)
459 boolean_t funnel_state
;
460 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
464 (void) thread_funnel_set(network_flock
, FALSE
);
469 in6_mtutimo(void *rock
)
471 struct radix_node_head
*rnh
= rock
;
472 struct mtuex_arg arg
;
477 arg
.nextstop
= time_second
+ MTUTIMO_DEFAULT
;
479 rnh
->rnh_walktree(rnh
, in6_mtuexpire
, &arg
);
483 atv
.tv_sec
= arg
.nextstop
;
484 if (atv
.tv_sec
< time_second
) {
485 printf("invalid mtu expiration time on routing table\n");
486 arg
.nextstop
= time_second
+ 30; /*last resort*/
488 timeout(in6_mtutimo_funneled
, rock
, tvtohz(&atv
));
495 struct radix_node_head
*rnh
= rt_tables
[AF_INET6
];
498 arg
.found
= arg
.killed
= 0;
504 rnh
->rnh_walktree(rnh
, in6_rtqkill
, &arg
);
510 * Initialize our routing tree.
513 in6_inithead(void **head
, int off
)
515 struct radix_node_head
*rnh
;
517 if (!rn_inithead(head
, off
))
520 if (head
!= (void **)&rt_tables
[AF_INET6
]) /* BOGUS! */
521 return 1; /* only do this for the real routing table */
524 rnh
->rnh_addaddr
= in6_addroute
;
525 rnh
->rnh_matchaddr
= in6_matroute
;
526 rnh
->rnh_close
= in6_clsroute
;
527 in6_rtqtimo(rnh
); /* kick off timeout first time */
528 in6_mtutimo(rnh
); /* kick off timeout first time */