1 /* $FreeBSD: src/sys/netinet6/in6_rmx.c,v 1.1.2.4 2004/10/06 02:35:17 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/in6_rmx.c,v 1.15 2006/12/22 23:57:53 swildner Exp $ */
3 /* $KAME: in6_rmx.c,v 1.11 2001/07/26 06:53:16 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright 1994, 1995 Massachusetts Institute of Technology
37 * Permission to use, copy, modify, and distribute this software and
38 * its documentation for any purpose and without fee is hereby
39 * granted, provided that both the above copyright notice and this
40 * permission notice appear in all copies, that both the above
41 * copyright notice and this permission notice appear in all
42 * supporting documentation, and that the name of M.I.T. not be used
43 * in advertising or publicity pertaining to distribution of the
44 * software without specific, written prior permission. M.I.T. makes
45 * no representations about the suitability of this software for any
46 * purpose. It is provided "as is" without express or implied
49 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
50 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
51 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
52 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
53 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
56 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
59 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * This code does two things necessary for the enhanced TCP metrics to
66 * function in a useful manner:
67 * 1) It marks all non-host routes as `cloning', thus ensuring that
68 * every actual reference to such a route actually gets turned
69 * into a reference to a host route to the specific destination
71 * 2) When such routes lose all their references, it arranges for them
72 * to be deleted in some random collection of circumstances, so that
73 * a large quantity of stale routing data is not kept in kernel memory
74 * indefinitely. See in6_rtqtimo() below for the exact mechanism.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
81 #include <sys/queue.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
85 #include <sys/syslog.h>
86 #include <sys/globaldata.h>
87 #include <sys/thread2.h>
90 #include <net/route.h>
91 #include <net/netisr2.h>
92 #include <net/netmsg2.h>
93 #include <netinet/in.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/in_var.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
100 #include <netinet/icmp6.h>
101 #include <netinet6/nd6.h>
103 #include <netinet/tcp.h>
104 #include <netinet/tcp_seq.h>
105 #include <netinet/tcp_timer.h>
106 #include <netinet/tcp_var.h>
108 struct in6_rttimo_ctx
{
109 struct callout timo_ch
;
110 struct netmsg_base timo_nmsg
;
111 struct radix_node_head
*timo_rnh
;
114 static struct in6_rttimo_ctx in6_rtqtimo_ctx
[MAXCPU
];
115 static struct in6_rttimo_ctx in6_mtutimo_ctx
[MAXCPU
];
117 extern int in6_inithead (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(char *key
, char *mask
, 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
;
164 if (!rt
->rt_rmx
.rmx_mtu
&& !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) &&
166 rt
->rt_rmx
.rmx_mtu
= IN6_LINKMTU(rt
->rt_ifp
);
168 ret
= rn_addroute(key
, mask
, head
, treenodes
);
169 if (ret
== NULL
&& rt
->rt_flags
& RTF_HOST
) {
173 * We are trying to add a host route, but can't.
174 * Find out if it is because of an
175 * ARP entry and delete it if so.
177 rt2
= rtpurelookup((struct sockaddr
*)sin6
);
180 if (rt2
->rt_flags
& RTF_LLINFO
&&
181 rt2
->rt_flags
& RTF_HOST
&&
183 rt2
->rt_gateway
->sa_family
== AF_LINK
) {
184 rtrequest(RTM_DELETE
, rt_key(rt2
),
185 rt2
->rt_gateway
, rt_mask(rt2
),
186 rt2
->rt_flags
, NULL
);
187 ret
= rn_addroute(key
, mask
, head
, treenodes
);
190 } else if (ret
== NULL
&& rt
->rt_flags
& RTF_CLONING
) {
194 * We are trying to add a net route, but can't.
195 * The following case should be allowed, so we'll make a
196 * special check for this:
197 * Two IPv6 addresses with the same prefix is assigned
198 * to a single interrface.
199 * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
200 * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
201 * In this case, (*1) and (*2) want to add the same
202 * net route entry, 3ffe:0501:: -> if0.
203 * This case should not raise an error.
205 rt2
= rtpurelookup((struct sockaddr
*)sin6
);
207 if ((rt2
->rt_flags
& (RTF_CLONING
|RTF_HOST
|RTF_GATEWAY
))
210 rt2
->rt_gateway
->sa_family
== AF_LINK
&&
211 rt2
->rt_ifp
== rt
->rt_ifp
) {
221 * This code is the inverse of in6_clsroute: on first reference, if we
222 * were managing the route, stop doing so and set the expiration timer
225 static struct radix_node
*
226 in6_matchroute(char *key
, struct radix_node_head
*head
)
228 struct radix_node
*rn
= rn_match(key
, head
);
229 struct rtentry
*rt
= (struct rtentry
*)rn
;
231 if (rt
!= NULL
&& rt
->rt_refcnt
== 0) { /* this is first reference */
232 if (rt
->rt_flags
& RTPRF_OURS
) {
233 rt
->rt_flags
&= ~RTPRF_OURS
;
234 rt
->rt_rmx
.rmx_expire
= 0;
240 SYSCTL_DECL(_net_inet6_ip6
);
242 static int rtq_reallyold
= 60*60;
243 /* one hour is ``really old'' */
244 SYSCTL_INT(_net_inet6_ip6
, IPV6CTL_RTEXPIRE
, rtexpire
,
245 CTLFLAG_RW
, &rtq_reallyold
, 0, "Default expiration time on cloned routes");
247 static int rtq_minreallyold
= 10;
248 /* never automatically crank down to less */
249 SYSCTL_INT(_net_inet6_ip6
, IPV6CTL_RTMINEXPIRE
, rtminexpire
, CTLFLAG_RW
,
250 &rtq_minreallyold
, 0, "Minimum time to attempt to hold onto cloned routes");
252 static int rtq_toomany
= 128;
253 /* 128 cached routes is ``too many'' */
254 SYSCTL_INT(_net_inet6_ip6
, IPV6CTL_RTMAXCACHE
, rtmaxcache
,
255 CTLFLAG_RW
, &rtq_toomany
, 0, "Upper limit on cloned routes");
259 * On last reference drop, mark the route as belong to us so that it can be
263 in6_clsroute(struct radix_node
*rn
, struct radix_node_head
*head
)
265 struct rtentry
*rt
= (struct rtentry
*)rn
;
267 if (!(rt
->rt_flags
& RTF_UP
))
268 return; /* prophylactic measures */
270 if ((rt
->rt_flags
& (RTF_LLINFO
| RTF_HOST
)) != RTF_HOST
)
273 if ((rt
->rt_flags
& (RTF_WASCLONED
| RTPRF_OURS
)) != RTF_WASCLONED
)
277 * As requested by David Greenman:
278 * If rtq_reallyold is 0, just delete the route without
279 * waiting for a timeout cycle to kill it.
281 if (rtq_reallyold
!= 0) {
282 rt
->rt_flags
|= RTPRF_OURS
;
283 rt
->rt_rmx
.rmx_expire
= time_uptime
+ rtq_reallyold
;
286 * Remove route from the radix tree, but defer deallocation
287 * until we return to rtfree().
289 rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
, rt_mask(rt
),
295 struct radix_node_head
*rnh
;
305 * Get rid of old routes. When draining, this deletes everything, even when
306 * the timeout is not expired yet. When updating, this makes sure that
307 * nothing has a timeout longer than the current value of rtq_reallyold.
310 in6_rtqkill(struct radix_node
*rn
, void *rock
)
312 struct rtqk_arg
*ap
= rock
;
313 struct rtentry
*rt
= (struct rtentry
*)rn
;
316 if (rt
->rt_flags
& RTPRF_OURS
) {
319 if (ap
->draining
|| rt
->rt_rmx
.rmx_expire
<= time_uptime
) {
320 if (rt
->rt_refcnt
> 0)
321 panic("rtqkill route really not free");
323 err
= rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
324 rt_mask(rt
), rt
->rt_flags
, NULL
);
326 log(LOG_WARNING
, "in6_rtqkill: error %d", err
);
331 (rt
->rt_rmx
.rmx_expire
- time_uptime
>
333 rt
->rt_rmx
.rmx_expire
=
334 time_uptime
+ rtq_reallyold
;
336 ap
->nextstop
= lmin(ap
->nextstop
,
337 rt
->rt_rmx
.rmx_expire
);
344 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
345 static int rtq_timeout
= RTQ_TIMEOUT
;
348 in6_rtqtimo(void *arg __unused
)
351 struct lwkt_msg
*lmsg
= &in6_rtqtimo_ctx
[cpuid
].timo_nmsg
.lmsg
;
354 if (lmsg
->ms_flags
& MSGF_DONE
)
355 lwkt_sendmsg_oncpu(netisr_cpuport(cpuid
), lmsg
);
360 in6_rtqtimo_dispatch(netmsg_t nmsg
)
362 struct in6_rttimo_ctx
*ctx
= &in6_rtqtimo_ctx
[mycpuid
];
363 struct radix_node_head
*rnh
= ctx
->timo_rnh
;
366 static time_t last_adjusted_timeout
= 0;
368 ASSERT_NETISR_NCPUS(mycpuid
);
372 lwkt_replymsg(&nmsg
->lmsg
, 0);
375 arg
.found
= arg
.killed
= 0;
377 arg
.nextstop
= time_uptime
+ rtq_timeout
;
378 arg
.draining
= arg
.updating
= 0;
379 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 && (int)(time_uptime
- 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_uptime
;
399 log(LOG_DEBUG
, "in6_rtqtimo: adjusted rtq_reallyold to %d",
402 arg
.found
= arg
.killed
= 0;
404 rnh
->rnh_walktree(rnh
, in6_rtqkill
, &arg
);
408 atv
.tv_sec
= arg
.nextstop
- time_uptime
;
409 if ((int)atv
.tv_sec
< 1) { /* time shift safety */
411 arg
.nextstop
= time_uptime
+ atv
.tv_sec
;
413 if ((int)atv
.tv_sec
> rtq_timeout
) { /* time shift safety */
414 atv
.tv_sec
= rtq_timeout
;
415 arg
.nextstop
= time_uptime
+ atv
.tv_sec
;
417 callout_reset(&ctx
->timo_ch
, tvtohz_high(&atv
), in6_rtqtimo
, NULL
);
424 struct radix_node_head
*rnh
;
429 in6_mtuexpire(struct radix_node
*rn
, void *rock
)
431 struct rtentry
*rt
= (struct rtentry
*)rn
;
432 struct mtuex_arg
*ap
= rock
;
436 panic("rt == NULL in in6_mtuexpire");
438 if (rt
->rt_rmx
.rmx_expire
&& !(rt
->rt_flags
& RTF_PROBEMTU
)) {
439 if (rt
->rt_rmx
.rmx_expire
<= time_uptime
) {
440 rt
->rt_flags
|= RTF_PROBEMTU
;
442 ap
->nextstop
= lmin(ap
->nextstop
,
443 rt
->rt_rmx
.rmx_expire
);
450 #define MTUTIMO_DEFAULT (60*1)
453 in6_mtutimo(void *arg __unused
)
456 struct lwkt_msg
*lmsg
= &in6_mtutimo_ctx
[cpuid
].timo_nmsg
.lmsg
;
459 if (lmsg
->ms_flags
& MSGF_DONE
)
460 lwkt_sendmsg_oncpu(netisr_cpuport(cpuid
), lmsg
);
465 in6_mtutimo_dispatch(netmsg_t nmsg
)
467 struct in6_rttimo_ctx
*ctx
= &in6_mtutimo_ctx
[mycpuid
];
468 struct radix_node_head
*rnh
= ctx
->timo_rnh
;
469 struct mtuex_arg arg
;
472 ASSERT_NETISR_NCPUS(mycpuid
);
476 lwkt_replymsg(&nmsg
->lmsg
, 0);
480 arg
.nextstop
= time_uptime
+ MTUTIMO_DEFAULT
;
481 rnh
->rnh_walktree(rnh
, in6_mtuexpire
, &arg
);
484 atv
.tv_sec
= arg
.nextstop
- time_uptime
;
485 if ((int)atv
.tv_sec
< 1) { /* time shift safety */
487 arg
.nextstop
= time_uptime
+ atv
.tv_sec
;
489 if ((int)atv
.tv_sec
> rtq_timeout
) { /* time shift safety */
490 atv
.tv_sec
= rtq_timeout
;
491 arg
.nextstop
= time_uptime
+ atv
.tv_sec
;
493 callout_reset(&ctx
->timo_ch
, tvtohz_high(&atv
), in6_mtutimo
, NULL
);
500 struct radix_node_head
*rnh
= rt_tables
[mycpuid
][AF_INET6
];
503 arg
.found
= arg
.killed
= 0;
509 rnh
->rnh_walktree(rnh
, in6_rtqkill
, &arg
);
515 * Initialize our routing tree.
518 in6_inithead(void **head
, int off
)
520 struct radix_node_head
*rnh
;
521 struct in6_rttimo_ctx
*ctx
;
524 KKASSERT(head
== (void **)&rt_tables
[cpuid
][AF_INET6
]);
526 if (!rn_inithead(head
, rn_cpumaskhead(cpuid
), off
))
530 rnh
->rnh_addaddr
= in6_addroute
;
531 rnh
->rnh_matchaddr
= in6_matchroute
;
532 rnh
->rnh_close
= in6_clsroute
;
534 ctx
= &in6_rtqtimo_ctx
[cpuid
];
536 callout_init_mp(&ctx
->timo_ch
);
537 netmsg_init(&ctx
->timo_nmsg
, NULL
, &netisr_adone_rport
, MSGF_PRIORITY
,
538 in6_rtqtimo_dispatch
);
540 ctx
= &in6_mtutimo_ctx
[cpuid
];
542 callout_init_mp(&ctx
->timo_ch
);
543 netmsg_init(&ctx
->timo_nmsg
, NULL
, &netisr_adone_rport
, MSGF_PRIORITY
,
544 in6_mtutimo_dispatch
);
546 in6_rtqtimo(NULL
); /* kick off timeout first time */
547 in6_mtutimo(NULL
); /* kick off timeout first time */