2 /* $KAME: in6_rmx.c,v 1.11 2001/07/26 06:53:16 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright 1994, 1995 Massachusetts Institute of Technology
36 * Permission to use, copy, modify, and distribute this software and
37 * its documentation for any purpose and without fee is hereby
38 * granted, provided that both the above copyright notice and this
39 * permission notice appear in all copies, that both the above
40 * copyright notice and this permission notice appear in all
41 * supporting documentation, and that the name of M.I.T. not be used
42 * in advertising or publicity pertaining to distribution of the
43 * software without specific, written prior permission. M.I.T. makes
44 * no representations about the suitability of this software for any
45 * purpose. It is provided "as is" without express or implied
48 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
49 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
50 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
51 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
52 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
55 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
57 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
58 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * This code does two things necessary for the enhanced TCP metrics to
65 * function in a useful manner:
66 * 1) It marks all non-host routes as `cloning', thus ensuring that
67 * every actual reference to such a route actually gets turned
68 * into a reference to a host route to the specific destination
70 * 2) When such routes lose all their references, it arranges for them
71 * to be deleted in some random collection of circumstances, so that
72 * a large quantity of stale routing data is not kept in kernel memory
73 * indefinitely. See in6_rtqtimo() below for the exact mechanism.
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <sys/queue.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
84 #include <sys/syslog.h>
87 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/in_var.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/ip6_var.h>
95 #include <netinet/icmp6.h>
97 #include <netinet/tcp.h>
98 #include <netinet/tcp_seq.h>
99 #include <netinet/tcp_timer.h>
100 #include <netinet/tcp_var.h>
102 extern int in6_inithead __P((void **head, int off));
104 #define RTPRF_OURS RTF_PROTO3 /* set on routes we manage */
107 * Do what we need to do when inserting a route.
109 static struct radix_node *
110 in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
111 struct radix_node *treenodes)
113 struct rtentry *rt = (struct rtentry *)treenodes;
114 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
115 struct radix_node *ret;
118 * For IPv6, all unicast non-host routes are automatically cloning.
120 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
121 rt->rt_flags |= RTF_MULTICAST;
123 if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
124 rt->rt_flags |= RTF_PRCLONING;
128 * A little bit of help for both IPv6 output and input:
129 * For local addresses, we make sure that RTF_LOCAL is set,
130 * with the thought that this might one day be used to speed up
133 * We also mark routes to multicast addresses as such, because
134 * it's easy to do and might be useful (but this is much more
135 * dubious since it's so easy to inspect the address). (This
139 * should elaborate the code.
141 if (rt->rt_flags & RTF_HOST) {
142 if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
145 rt->rt_flags |= RTF_LOCAL;
149 if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU)
151 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
153 ret = rn_addroute(v_arg, n_arg, head, treenodes);
154 if (ret == NULL && rt->rt_flags & RTF_HOST) {
157 * We are trying to add a host route, but can't.
158 * Find out if it is because of an
159 * ARP entry and delete it if so.
161 rt2 = rtalloc1((struct sockaddr *)sin6, 0,
162 RTF_CLONING | RTF_PRCLONING);
164 if (rt2->rt_flags & RTF_LLINFO &&
165 rt2->rt_flags & RTF_HOST &&
167 rt2->rt_gateway->sa_family == AF_LINK) {
168 rtrequest(RTM_DELETE,
169 (struct sockaddr *)rt_key(rt2),
171 rt_mask(rt2), rt2->rt_flags, 0);
172 ret = rn_addroute(v_arg, n_arg, head,
177 } else if (ret == NULL && rt->rt_flags & RTF_CLONING) {
180 * We are trying to add a net route, but can't.
181 * The following case should be allowed, so we'll make a
182 * special check for this:
183 * Two IPv6 addresses with the same prefix is assigned
184 * to a single interrface.
185 * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
186 * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
187 * In this case, (*1) and (*2) want to add the same
188 * net route entry, 3ffe:0501:: -> if0.
189 * This case should not raise an error.
191 rt2 = rtalloc1((struct sockaddr *)sin6, 0,
192 RTF_CLONING | RTF_PRCLONING);
194 if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY))
197 && rt2->rt_gateway->sa_family == AF_LINK
198 && rt2->rt_ifp == rt->rt_ifp) {
208 * This code is the inverse of in6_clsroute: on first reference, if we
209 * were managing the route, stop doing so and set the expiration timer
212 static struct radix_node *
213 in6_matroute(void *v_arg, struct radix_node_head *head)
215 struct radix_node *rn = rn_match(v_arg, head);
216 struct rtentry *rt = (struct rtentry *)rn;
218 if (rt && rt->rt_refcnt == 0) { /* this is first reference */
219 if (rt->rt_flags & RTPRF_OURS) {
220 rt->rt_flags &= ~RTPRF_OURS;
221 rt->rt_rmx.rmx_expire = 0;
227 SYSCTL_DECL(_net_inet6_ip6);
229 static int rtq_reallyold = 60*60;
230 /* one hour is ``really old'' */
231 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTEXPIRE, rtexpire,
232 CTLFLAG_RW, &rtq_reallyold , 0, "");
234 static int rtq_minreallyold = 10;
235 /* never automatically crank down to less */
236 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMINEXPIRE, rtminexpire,
237 CTLFLAG_RW, &rtq_minreallyold , 0, "");
239 static int rtq_toomany = 128;
240 /* 128 cached routes is ``too many'' */
241 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache,
242 CTLFLAG_RW, &rtq_toomany , 0, "");
246 * On last reference drop, mark the route as belong to us so that it can be
250 in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
252 struct rtentry *rt = (struct rtentry *)rn;
254 if (!(rt->rt_flags & RTF_UP))
255 return; /* prophylactic measures */
257 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
260 if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS))
265 * As requested by David Greenman:
266 * If rtq_reallyold is 0, just delete the route without
267 * waiting for a timeout cycle to kill it.
269 if (rtq_reallyold != 0) {
270 rt->rt_flags |= RTPRF_OURS;
271 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
273 rtrequest(RTM_DELETE,
274 (struct sockaddr *)rt_key(rt),
275 rt->rt_gateway, rt_mask(rt),
281 struct radix_node_head *rnh;
291 * Get rid of old routes. When draining, this deletes everything, even when
292 * the timeout is not expired yet. When updating, this makes sure that
293 * nothing has a timeout longer than the current value of rtq_reallyold.
296 in6_rtqkill(struct radix_node *rn, void *rock)
298 struct rtqk_arg *ap = rock;
299 struct rtentry *rt = (struct rtentry *)rn;
302 if (rt->rt_flags & RTPRF_OURS) {
305 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
306 if (rt->rt_refcnt > 0)
307 panic("rtqkill route really not free");
309 err = rtrequest(RTM_DELETE,
310 (struct sockaddr *)rt_key(rt),
311 rt->rt_gateway, rt_mask(rt),
314 log(LOG_WARNING, "in6_rtqkill: error %d", err);
320 && (rt->rt_rmx.rmx_expire - time_second
322 rt->rt_rmx.rmx_expire = time_second
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 timeout(in6_rtqtimo, rock, tvtohz(&atv));
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 timeout(in6_mtutimo, rock, tvtohz(&atv));
445 struct radix_node_head *rnh = rt_tables[AF_INET6];
448 arg.found = arg.killed = 0;
454 rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
460 * Initialize our routing tree.
463 in6_inithead(void **head, int off)
465 struct radix_node_head *rnh;
467 if (!rn_inithead(head, off))
470 if (head != (void **)&rt_tables[AF_INET6]) /* BOGUS! */
471 return 1; /* only do this for the real routing table */
474 rnh->rnh_addaddr = in6_addroute;
475 rnh->rnh_matchaddr = in6_matroute;
476 rnh->rnh_close = in6_clsroute;
477 in6_rtqtimo(rnh); /* kick off timeout first time */
478 in6_mtutimo(rnh); /* kick off timeout first time */