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.12 2005/03/04 03:48:25 hsu 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>
88 #include <net/route.h>
89 #include <netinet/in.h>
90 #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 #include <netinet/tcp.h>
99 #include <netinet/tcp_seq.h>
100 #include <netinet/tcp_timer.h>
101 #include <netinet/tcp_var.h>
103 static struct callout in6_rtqtimo_ch;
104 static struct callout in6_mtutimo_ch;
106 extern int in6_inithead (void **head, int off);
108 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
111 * Do what we need to do when inserting a route.
113 static struct radix_node *
114 in6_addroute(char *key, char *mask, struct radix_node_head *head,
115 struct radix_node *treenodes)
117 struct rtentry *rt = (struct rtentry *)treenodes;
118 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
119 struct radix_node *ret;
122 * For IPv6, all unicast non-host routes are automatically cloning.
124 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
125 rt->rt_flags |= RTF_MULTICAST;
127 if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
128 rt->rt_flags |= RTF_PRCLONING;
132 * A little bit of help for both IPv6 output and input:
133 * For local addresses, we make sure that RTF_LOCAL is set,
134 * with the thought that this might one day be used to speed up
137 * We also mark routes to multicast addresses as such, because
138 * it's easy to do and might be useful (but this is much more
139 * dubious since it's so easy to inspect the address). (This
143 * should elaborate the code.
145 if (rt->rt_flags & RTF_HOST) {
146 if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
149 rt->rt_flags |= RTF_LOCAL;
153 if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
155 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
157 ret = rn_addroute(key, mask, head, treenodes);
158 if (ret == NULL && rt->rt_flags & RTF_HOST) {
162 * We are trying to add a host route, but can't.
163 * Find out if it is because of an
164 * ARP entry and delete it if so.
166 rt2 = rtpurelookup((struct sockaddr *)sin6);
169 if (rt2->rt_flags & RTF_LLINFO &&
170 rt2->rt_flags & RTF_HOST &&
172 rt2->rt_gateway->sa_family == AF_LINK) {
173 rtrequest(RTM_DELETE, rt_key(rt2),
174 rt2->rt_gateway, rt_mask(rt2),
175 rt2->rt_flags, NULL);
176 ret = rn_addroute(key, mask, head, treenodes);
179 } else if (ret == NULL && rt->rt_flags & RTF_CLONING) {
183 * We are trying to add a net route, but can't.
184 * The following case should be allowed, so we'll make a
185 * special check for this:
186 * Two IPv6 addresses with the same prefix is assigned
187 * to a single interrface.
188 * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
189 * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
190 * In this case, (*1) and (*2) want to add the same
191 * net route entry, 3ffe:0501:: -> if0.
192 * This case should not raise an error.
194 rt2 = rtpurelookup((struct sockaddr *)sin6);
196 if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY))
199 rt2->rt_gateway->sa_family == AF_LINK &&
200 rt2->rt_ifp == rt->rt_ifp) {
210 * This code is the inverse of in6_clsroute: on first reference, if we
211 * were managing the route, stop doing so and set the expiration timer
214 static struct radix_node *
215 in6_matchroute(char *key, struct radix_node_head *head)
217 struct radix_node *rn = rn_match(key, head);
218 struct rtentry *rt = (struct rtentry *)rn;
220 if (rt != NULL && rt->rt_refcnt == 0) { /* this is first reference */
221 if (rt->rt_flags & RTPRF_OURS) {
222 rt->rt_flags &= ~RTPRF_OURS;
223 rt->rt_rmx.rmx_expire = 0;
229 SYSCTL_DECL(_net_inet6_ip6);
231 static int rtq_reallyold = 60*60;
232 /* one hour is ``really old'' */
233 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire,
234 CTLFLAG_RW, &rtq_reallyold , 0, "");
236 static int rtq_minreallyold = 10;
237 /* never automatically crank down to less */
238 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire,
239 CTLFLAG_RW, &rtq_minreallyold , 0, "");
241 static int rtq_toomany = 128;
242 /* 128 cached routes is ``too many'' */
243 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache,
244 CTLFLAG_RW, &rtq_toomany , 0, "");
248 * On last reference drop, mark the route as belong to us so that it can be
252 in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
254 struct rtentry *rt = (struct rtentry *)rn;
256 if (!(rt->rt_flags & RTF_UP))
257 return; /* prophylactic measures */
259 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
262 if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
266 * As requested by David Greenman:
267 * If rtq_reallyold is 0, just delete the route without
268 * waiting for a timeout cycle to kill it.
270 if (rtq_reallyold != 0) {
271 rt->rt_flags |= RTPRF_OURS;
272 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
275 * Remove route from the radix tree, but defer deallocation
276 * until we return to rtfree().
278 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, rt_mask(rt),
284 struct radix_node_head *rnh;
294 * Get rid of old routes. When draining, this deletes everything, even when
295 * the timeout is not expired yet. When updating, this makes sure that
296 * nothing has a timeout longer than the current value of rtq_reallyold.
299 in6_rtqkill(struct radix_node *rn, void *rock)
301 struct rtqk_arg *ap = rock;
302 struct rtentry *rt = (struct rtentry *)rn;
305 if (rt->rt_flags & RTPRF_OURS) {
308 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
309 if (rt->rt_refcnt > 0)
310 panic("rtqkill route really not free");
312 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
313 rt_mask(rt), rt->rt_flags, NULL);
315 log(LOG_WARNING, "in6_rtqkill: error %d", err);
320 (rt->rt_rmx.rmx_expire - time_second >
322 rt->rt_rmx.rmx_expire =
323 time_second + rtq_reallyold;
325 ap->nextstop = lmin(ap->nextstop,
326 rt->rt_rmx.rmx_expire);
333 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
334 static int rtq_timeout = RTQ_TIMEOUT;
337 in6_rtqtimo(void *rock)
339 struct radix_node_head *rnh = rock;
342 static time_t last_adjusted_timeout = 0;
345 arg.found = arg.killed = 0;
347 arg.nextstop = time_second + rtq_timeout;
348 arg.draining = arg.updating = 0;
350 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
354 * Attempt to be somewhat dynamic about this:
355 * If there are ``too many'' routes sitting around taking up space,
356 * then crank down the timeout, and see if we can't make some more
357 * go away. However, we make sure that we will never adjust more
358 * than once in rtq_timeout seconds, to keep from cranking down too
361 if ((arg.found - arg.killed > rtq_toomany)
362 && (time_second - last_adjusted_timeout >= rtq_timeout)
363 && rtq_reallyold > rtq_minreallyold) {
364 rtq_reallyold = 2*rtq_reallyold / 3;
365 if (rtq_reallyold < rtq_minreallyold) {
366 rtq_reallyold = rtq_minreallyold;
369 last_adjusted_timeout = time_second;
371 log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold to %d",
374 arg.found = arg.killed = 0;
377 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
382 atv.tv_sec = arg.nextstop;
383 callout_reset(&in6_rtqtimo_ch, tvtohz_high(&atv), in6_rtqtimo, rock);
390 struct radix_node_head *rnh;
395 in6_mtuexpire(struct radix_node *rn, void *rock)
397 struct rtentry *rt = (struct rtentry *)rn;
398 struct mtuex_arg *ap = rock;
402 panic("rt == NULL in in6_mtuexpire");
404 if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
405 if (rt->rt_rmx.rmx_expire <= time_second) {
406 rt->rt_flags |= RTF_PROBEMTU;
408 ap->nextstop = lmin(ap->nextstop,
409 rt->rt_rmx.rmx_expire);
416 #define MTUTIMO_DEFAULT (60*1)
419 in6_mtutimo(void *rock)
421 struct radix_node_head *rnh = rock;
422 struct mtuex_arg arg;
427 arg.nextstop = time_second + MTUTIMO_DEFAULT;
429 rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
433 atv.tv_sec = arg.nextstop;
434 if (atv.tv_sec < time_second) {
435 printf("invalid mtu expiration time on routing table\n");
436 arg.nextstop = time_second + 30; /* last resort */
438 callout_reset(&in6_mtutimo_ch, tvtohz_high(&atv), in6_mtutimo, rock);
445 struct radix_node_head *rnh = rt_tables[AF_INET6];
449 arg.found = arg.killed = 0;
455 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
461 * Initialize our routing tree.
464 in6_inithead(void **head, int off)
466 struct radix_node_head *rnh;
468 if (!rn_inithead(head, off))
471 if (head != (void **)&rt_tables[AF_INET6]) /* BOGUS! */
472 return 1; /* only do this for the real routing table */
475 rnh->rnh_addaddr = in6_addroute;
476 rnh->rnh_matchaddr = in6_matchroute;
477 rnh->rnh_close = in6_clsroute;
478 callout_init(&in6_mtutimo_ch);
479 callout_init(&in6_rtqtimo_ch);
480 in6_rtqtimo(rnh); /* kick off timeout first time */
481 in6_mtutimo(rnh); /* kick off timeout first time */