xref: /freebsd/sys/netinet6/in6_rmx.c (revision 0c88be04990b5c60f88d533caea397b02e2a0660)
1caf43b02SWarner Losh /*-
282cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
382cd038dSYoshinobu Inoue  * All rights reserved.
482cd038dSYoshinobu Inoue  *
582cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
682cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
782cd038dSYoshinobu Inoue  * are met:
882cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
982cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1082cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1282cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1382cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1482cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1582cd038dSYoshinobu Inoue  *    without specific prior written permission.
1682cd038dSYoshinobu Inoue  *
1782cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1882cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1982cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2082cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2182cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2282cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2382cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2482cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2582cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2682cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2782cd038dSYoshinobu Inoue  * SUCH DAMAGE.
28b48287a3SDavid E. O'Brien  *
29b48287a3SDavid E. O'Brien  *	$KAME: in6_rmx.c,v 1.11 2001/07/26 06:53:16 jinmei Exp $
3082cd038dSYoshinobu Inoue  */
3182cd038dSYoshinobu Inoue 
32caf43b02SWarner Losh /*-
3382cd038dSYoshinobu Inoue  * Copyright 1994, 1995 Massachusetts Institute of Technology
3482cd038dSYoshinobu Inoue  *
3582cd038dSYoshinobu Inoue  * Permission to use, copy, modify, and distribute this software and
3682cd038dSYoshinobu Inoue  * its documentation for any purpose and without fee is hereby
3782cd038dSYoshinobu Inoue  * granted, provided that both the above copyright notice and this
3882cd038dSYoshinobu Inoue  * permission notice appear in all copies, that both the above
3982cd038dSYoshinobu Inoue  * copyright notice and this permission notice appear in all
4082cd038dSYoshinobu Inoue  * supporting documentation, and that the name of M.I.T. not be used
4182cd038dSYoshinobu Inoue  * in advertising or publicity pertaining to distribution of the
4282cd038dSYoshinobu Inoue  * software without specific, written prior permission.  M.I.T. makes
4382cd038dSYoshinobu Inoue  * no representations about the suitability of this software for any
4482cd038dSYoshinobu Inoue  * purpose.  It is provided "as is" without express or implied
4582cd038dSYoshinobu Inoue  * warranty.
4682cd038dSYoshinobu Inoue  *
4782cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
4882cd038dSYoshinobu Inoue  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
4982cd038dSYoshinobu Inoue  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
5082cd038dSYoshinobu Inoue  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
5182cd038dSYoshinobu Inoue  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
5282cd038dSYoshinobu Inoue  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
5382cd038dSYoshinobu Inoue  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
5482cd038dSYoshinobu Inoue  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5582cd038dSYoshinobu Inoue  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5682cd038dSYoshinobu Inoue  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
5782cd038dSYoshinobu Inoue  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5882cd038dSYoshinobu Inoue  * SUCH DAMAGE.
5982cd038dSYoshinobu Inoue  *
6082cd038dSYoshinobu Inoue  */
6182cd038dSYoshinobu Inoue 
6282cd038dSYoshinobu Inoue /*
6382cd038dSYoshinobu Inoue  * This code does two things necessary for the enhanced TCP metrics to
6482cd038dSYoshinobu Inoue  * function in a useful manner:
6582cd038dSYoshinobu Inoue  *  1) It marks all non-host routes as `cloning', thus ensuring that
6682cd038dSYoshinobu Inoue  *     every actual reference to such a route actually gets turned
6782cd038dSYoshinobu Inoue  *     into a reference to a host route to the specific destination
6882cd038dSYoshinobu Inoue  *     requested.
6982cd038dSYoshinobu Inoue  *  2) When such routes lose all their references, it arranges for them
7082cd038dSYoshinobu Inoue  *     to be deleted in some random collection of circumstances, so that
7182cd038dSYoshinobu Inoue  *     a large quantity of stale routing data is not kept in kernel memory
7282cd038dSYoshinobu Inoue  *     indefinitely.  See in6_rtqtimo() below for the exact mechanism.
7382cd038dSYoshinobu Inoue  */
7482cd038dSYoshinobu Inoue 
75b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
76b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$");
77b48287a3SDavid E. O'Brien 
7882cd038dSYoshinobu Inoue #include <sys/param.h>
7982cd038dSYoshinobu Inoue #include <sys/systm.h>
8082cd038dSYoshinobu Inoue #include <sys/kernel.h>
81609ff41fSWarner Losh #include <sys/lock.h>
8282cd038dSYoshinobu Inoue #include <sys/sysctl.h>
8382cd038dSYoshinobu Inoue #include <sys/queue.h>
8482cd038dSYoshinobu Inoue #include <sys/socket.h>
8582cd038dSYoshinobu Inoue #include <sys/socketvar.h>
8682cd038dSYoshinobu Inoue #include <sys/mbuf.h>
873120b9d4SKip Macy #include <sys/rwlock.h>
8882cd038dSYoshinobu Inoue #include <sys/syslog.h>
89d1dd20beSSam Leffler #include <sys/callout.h>
90603724d3SBjoern A. Zeeb #include <sys/vimage.h>
9182cd038dSYoshinobu Inoue 
9282cd038dSYoshinobu Inoue #include <net/if.h>
930c88be04SBjoern A. Zeeb #include <net/route.h>
944b79449eSBjoern A. Zeeb 
9582cd038dSYoshinobu Inoue #include <netinet/in.h>
9682cd038dSYoshinobu Inoue #include <netinet/ip_var.h>
9782cd038dSYoshinobu Inoue #include <netinet/in_var.h>
9882cd038dSYoshinobu Inoue 
99686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
10082cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
10182cd038dSYoshinobu Inoue 
102686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
10331b3783cSHajimu UMEMOTO #include <netinet6/nd6.h>
1044b79449eSBjoern A. Zeeb #include <netinet6/vinet6.h>
10582cd038dSYoshinobu Inoue 
10682cd038dSYoshinobu Inoue #include <netinet/tcp.h>
10782cd038dSYoshinobu Inoue #include <netinet/tcp_seq.h>
10882cd038dSYoshinobu Inoue #include <netinet/tcp_timer.h>
10982cd038dSYoshinobu Inoue #include <netinet/tcp_var.h>
11082cd038dSYoshinobu Inoue 
1119233d8f3SDavid E. O'Brien extern int	in6_inithead(void **head, int off);
112bc29160dSMarko Zec #ifdef VIMAGE
113bc29160dSMarko Zec extern int	in6_detachhead(void **head, int off);
114bc29160dSMarko Zec #endif
11582cd038dSYoshinobu Inoue 
11682cd038dSYoshinobu Inoue #define RTPRF_OURS		RTF_PROTO3	/* set on routes we manage */
11782cd038dSYoshinobu Inoue 
11882cd038dSYoshinobu Inoue /*
11982cd038dSYoshinobu Inoue  * Do what we need to do when inserting a route.
12082cd038dSYoshinobu Inoue  */
12182cd038dSYoshinobu Inoue static struct radix_node *
12282cd038dSYoshinobu Inoue in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
12382cd038dSYoshinobu Inoue     struct radix_node *treenodes)
12482cd038dSYoshinobu Inoue {
12582cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)treenodes;
12682cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
12782cd038dSYoshinobu Inoue 	struct radix_node *ret;
12882cd038dSYoshinobu Inoue 
1296e6b3f7cSQing Li 	RADIX_NODE_HEAD_WLOCK_ASSERT(head);
13082cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
13182cd038dSYoshinobu Inoue 		rt->rt_flags |= RTF_MULTICAST;
13282cd038dSYoshinobu Inoue 
13382cd038dSYoshinobu Inoue 	/*
13482cd038dSYoshinobu Inoue 	 * A little bit of help for both IPv6 output and input:
13582cd038dSYoshinobu Inoue 	 *   For local addresses, we make sure that RTF_LOCAL is set,
13682cd038dSYoshinobu Inoue 	 *   with the thought that this might one day be used to speed up
13782cd038dSYoshinobu Inoue 	 *   ip_input().
13882cd038dSYoshinobu Inoue 	 *
13982cd038dSYoshinobu Inoue 	 * We also mark routes to multicast addresses as such, because
14082cd038dSYoshinobu Inoue 	 * it's easy to do and might be useful (but this is much more
14182cd038dSYoshinobu Inoue 	 * dubious since it's so easy to inspect the address).  (This
14282cd038dSYoshinobu Inoue 	 * is done above.)
14382cd038dSYoshinobu Inoue 	 *
14482cd038dSYoshinobu Inoue 	 * XXX
14582cd038dSYoshinobu Inoue 	 * should elaborate the code.
14682cd038dSYoshinobu Inoue 	 */
14782cd038dSYoshinobu Inoue 	if (rt->rt_flags & RTF_HOST) {
14882cd038dSYoshinobu Inoue 		if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
14982cd038dSYoshinobu Inoue 					->sin6_addr,
15082cd038dSYoshinobu Inoue 				       &sin6->sin6_addr)) {
15182cd038dSYoshinobu Inoue 			rt->rt_flags |= RTF_LOCAL;
15282cd038dSYoshinobu Inoue 		}
15382cd038dSYoshinobu Inoue 	}
15482cd038dSYoshinobu Inoue 
15597d8d152SAndre Oppermann 	if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp)
15631b3783cSHajimu UMEMOTO 		rt->rt_rmx.rmx_mtu = IN6_LINKMTU(rt->rt_ifp);
15782cd038dSYoshinobu Inoue 
15882cd038dSYoshinobu Inoue 	ret = rn_addroute(v_arg, n_arg, head, treenodes);
1596e6b3f7cSQing Li 	if (ret == NULL) {
16082cd038dSYoshinobu Inoue 		struct rtentry *rt2;
16182cd038dSYoshinobu Inoue 		/*
16282cd038dSYoshinobu Inoue 		 * We are trying to add a net route, but can't.
16382cd038dSYoshinobu Inoue 		 * The following case should be allowed, so we'll make a
16482cd038dSYoshinobu Inoue 		 * special check for this:
16582cd038dSYoshinobu Inoue 		 *	Two IPv6 addresses with the same prefix is assigned
16682cd038dSYoshinobu Inoue 		 *	to a single interrface.
16782cd038dSYoshinobu Inoue 		 *	# ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
16882cd038dSYoshinobu Inoue 		 *	# ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
16982cd038dSYoshinobu Inoue 		 *	In this case, (*1) and (*2) want to add the same
17082cd038dSYoshinobu Inoue 		 *	net route entry, 3ffe:0501:: -> if0.
17182cd038dSYoshinobu Inoue 		 *	This case should not raise an error.
17282cd038dSYoshinobu Inoue 		 */
1736e6b3f7cSQing Li 		rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_RNH_LOCKED);
17482cd038dSYoshinobu Inoue 		if (rt2) {
1756e6b3f7cSQing Li 			if (((rt2->rt_flags & (RTF_HOST|RTF_GATEWAY)) == 0)
17682cd038dSYoshinobu Inoue 			 && rt2->rt_gateway
17782cd038dSYoshinobu Inoue 			 && rt2->rt_gateway->sa_family == AF_LINK
17882cd038dSYoshinobu Inoue 			 && rt2->rt_ifp == rt->rt_ifp) {
17982cd038dSYoshinobu Inoue 				ret = rt2->rt_nodes;
18082cd038dSYoshinobu Inoue 			}
181d1dd20beSSam Leffler 			RTFREE_LOCKED(rt2);
18282cd038dSYoshinobu Inoue 		}
18382cd038dSYoshinobu Inoue 	}
1846e6b3f7cSQing Li 	return (ret);
18582cd038dSYoshinobu Inoue }
18682cd038dSYoshinobu Inoue 
18782cd038dSYoshinobu Inoue /*
18882cd038dSYoshinobu Inoue  * This code is the inverse of in6_clsroute: on first reference, if we
18982cd038dSYoshinobu Inoue  * were managing the route, stop doing so and set the expiration timer
19082cd038dSYoshinobu Inoue  * back off again.
19182cd038dSYoshinobu Inoue  */
19282cd038dSYoshinobu Inoue static struct radix_node *
19382cd038dSYoshinobu Inoue in6_matroute(void *v_arg, struct radix_node_head *head)
19482cd038dSYoshinobu Inoue {
19582cd038dSYoshinobu Inoue 	struct radix_node *rn = rn_match(v_arg, head);
19682cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
19782cd038dSYoshinobu Inoue 
19882cd038dSYoshinobu Inoue 	if (rt && rt->rt_refcnt == 0) { /* this is first reference */
19982cd038dSYoshinobu Inoue 		if (rt->rt_flags & RTPRF_OURS) {
20082cd038dSYoshinobu Inoue 			rt->rt_flags &= ~RTPRF_OURS;
20182cd038dSYoshinobu Inoue 			rt->rt_rmx.rmx_expire = 0;
20282cd038dSYoshinobu Inoue 		}
20382cd038dSYoshinobu Inoue 	}
20482cd038dSYoshinobu Inoue 	return rn;
20582cd038dSYoshinobu Inoue }
20682cd038dSYoshinobu Inoue 
2077ee982bcSJun-ichiro itojun Hagino SYSCTL_DECL(_net_inet6_ip6);
2087ee982bcSJun-ichiro itojun Hagino 
20944e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
21044e33a07SMarko Zec static int rtq_reallyold6;
21144e33a07SMarko Zec static int rtq_minreallyold6;
21244e33a07SMarko Zec static int rtq_toomany6;
21344e33a07SMarko Zec #endif
21444e33a07SMarko Zec 
21597021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTEXPIRE,
21697021c24SMarko Zec     rtexpire, CTLFLAG_RW, rtq_reallyold6 , 0, "");
21782cd038dSYoshinobu Inoue 
21897021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTMINEXPIRE,
21997021c24SMarko Zec     rtminexpire, CTLFLAG_RW, rtq_minreallyold6 , 0, "");
22082cd038dSYoshinobu Inoue 
22197021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTMAXCACHE,
22297021c24SMarko Zec     rtmaxcache, CTLFLAG_RW, rtq_toomany6 , 0, "");
22382cd038dSYoshinobu Inoue 
22482cd038dSYoshinobu Inoue 
22582cd038dSYoshinobu Inoue 
22682cd038dSYoshinobu Inoue struct rtqk_arg {
22782cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
22882cd038dSYoshinobu Inoue 	int mode;
22982cd038dSYoshinobu Inoue 	int updating;
23082cd038dSYoshinobu Inoue 	int draining;
23182cd038dSYoshinobu Inoue 	int killed;
23282cd038dSYoshinobu Inoue 	int found;
23382cd038dSYoshinobu Inoue 	time_t nextstop;
23482cd038dSYoshinobu Inoue };
23582cd038dSYoshinobu Inoue 
23682cd038dSYoshinobu Inoue /*
23782cd038dSYoshinobu Inoue  * Get rid of old routes.  When draining, this deletes everything, even when
23882cd038dSYoshinobu Inoue  * the timeout is not expired yet.  When updating, this makes sure that
239f1250445SBjoern A. Zeeb  * nothing has a timeout longer than the current value of rtq_reallyold6.
24082cd038dSYoshinobu Inoue  */
24182cd038dSYoshinobu Inoue static int
24282cd038dSYoshinobu Inoue in6_rtqkill(struct radix_node *rn, void *rock)
24382cd038dSYoshinobu Inoue {
2448b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
24582cd038dSYoshinobu Inoue 	struct rtqk_arg *ap = rock;
24682cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
24782cd038dSYoshinobu Inoue 	int err;
24882cd038dSYoshinobu Inoue 
249a714e55fSRobert Watson 	RADIX_NODE_HEAD_WLOCK_ASSERT(ap->rnh);
250a714e55fSRobert Watson 
25182cd038dSYoshinobu Inoue 	if (rt->rt_flags & RTPRF_OURS) {
25282cd038dSYoshinobu Inoue 		ap->found++;
25382cd038dSYoshinobu Inoue 
254fe53256dSAndre Oppermann 		if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) {
25582cd038dSYoshinobu Inoue 			if (rt->rt_refcnt > 0)
25682cd038dSYoshinobu Inoue 				panic("rtqkill route really not free");
25782cd038dSYoshinobu Inoue 
25882cd038dSYoshinobu Inoue 			err = rtrequest(RTM_DELETE,
25982cd038dSYoshinobu Inoue 					(struct sockaddr *)rt_key(rt),
26082cd038dSYoshinobu Inoue 					rt->rt_gateway, rt_mask(rt),
2616e6b3f7cSQing Li 					rt->rt_flags|RTF_RNH_LOCKED, 0);
26282cd038dSYoshinobu Inoue 			if (err) {
26382cd038dSYoshinobu Inoue 				log(LOG_WARNING, "in6_rtqkill: error %d", err);
26482cd038dSYoshinobu Inoue 			} else {
26582cd038dSYoshinobu Inoue 				ap->killed++;
26682cd038dSYoshinobu Inoue 			}
26782cd038dSYoshinobu Inoue 		} else {
26882cd038dSYoshinobu Inoue 			if (ap->updating
269fe53256dSAndre Oppermann 			   && (rt->rt_rmx.rmx_expire - time_uptime
270f1250445SBjoern A. Zeeb 			       > V_rtq_reallyold6)) {
271fe53256dSAndre Oppermann 				rt->rt_rmx.rmx_expire = time_uptime
272f1250445SBjoern A. Zeeb 					+ V_rtq_reallyold6;
27382cd038dSYoshinobu Inoue 			}
27482cd038dSYoshinobu Inoue 			ap->nextstop = lmin(ap->nextstop,
27582cd038dSYoshinobu Inoue 					    rt->rt_rmx.rmx_expire);
27682cd038dSYoshinobu Inoue 		}
27782cd038dSYoshinobu Inoue 	}
27882cd038dSYoshinobu Inoue 
27982cd038dSYoshinobu Inoue 	return 0;
28082cd038dSYoshinobu Inoue }
28182cd038dSYoshinobu Inoue 
28282cd038dSYoshinobu Inoue #define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
28344e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
28444e33a07SMarko Zec static int rtq_timeout6;
2852ce7b410SAlexander Kabaev static struct callout rtq_timer6;
28644e33a07SMarko Zec #endif
28782cd038dSYoshinobu Inoue 
28882cd038dSYoshinobu Inoue static void
28982cd038dSYoshinobu Inoue in6_rtqtimo(void *rock)
29082cd038dSYoshinobu Inoue {
2918b615593SMarko Zec 	CURVNET_SET_QUIET((struct vnet *) rock);
292093f25f8SMarko Zec 	INIT_VNET_INET6(curvnet);
293c2c2a7c1SBjoern A. Zeeb 	struct radix_node_head *rnh;
29482cd038dSYoshinobu Inoue 	struct rtqk_arg arg;
29582cd038dSYoshinobu Inoue 	struct timeval atv;
29682cd038dSYoshinobu Inoue 	static time_t last_adjusted_timeout = 0;
29782cd038dSYoshinobu Inoue 
298c2c2a7c1SBjoern A. Zeeb 	rnh = rt_tables_get_rnh(0, AF_INET6);
299c2c2a7c1SBjoern A. Zeeb 	if (rnh == NULL) {
300c2c2a7c1SBjoern A. Zeeb 		CURVNET_RESTORE();
301c2c2a7c1SBjoern A. Zeeb 		return;
302c2c2a7c1SBjoern A. Zeeb 	}
30382cd038dSYoshinobu Inoue 	arg.found = arg.killed = 0;
30482cd038dSYoshinobu Inoue 	arg.rnh = rnh;
305603724d3SBjoern A. Zeeb 	arg.nextstop = time_uptime + V_rtq_timeout6;
30682cd038dSYoshinobu Inoue 	arg.draining = arg.updating = 0;
307956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
30882cd038dSYoshinobu Inoue 	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
309956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
31082cd038dSYoshinobu Inoue 
31182cd038dSYoshinobu Inoue 	/*
31282cd038dSYoshinobu Inoue 	 * Attempt to be somewhat dynamic about this:
31382cd038dSYoshinobu Inoue 	 * If there are ``too many'' routes sitting around taking up space,
31482cd038dSYoshinobu Inoue 	 * then crank down the timeout, and see if we can't make some more
31582cd038dSYoshinobu Inoue 	 * go away.  However, we make sure that we will never adjust more
3165f9a5768SJulian Elischer 	 * than once in rtq_timeout6 seconds, to keep from cranking down too
31782cd038dSYoshinobu Inoue 	 * hard.
31882cd038dSYoshinobu Inoue 	 */
319f1250445SBjoern A. Zeeb 	if ((arg.found - arg.killed > V_rtq_toomany6)
320603724d3SBjoern A. Zeeb 	   && (time_uptime - last_adjusted_timeout >= V_rtq_timeout6)
321f1250445SBjoern A. Zeeb 	   && V_rtq_reallyold6 > V_rtq_minreallyold6) {
322f1250445SBjoern A. Zeeb 		V_rtq_reallyold6 = 2*V_rtq_reallyold6 / 3;
323f1250445SBjoern A. Zeeb 		if (V_rtq_reallyold6 < V_rtq_minreallyold6) {
324f1250445SBjoern A. Zeeb 			V_rtq_reallyold6 = V_rtq_minreallyold6;
32582cd038dSYoshinobu Inoue 		}
32682cd038dSYoshinobu Inoue 
32763721457SOleg Bulyzhin 		last_adjusted_timeout = time_uptime;
32882cd038dSYoshinobu Inoue #ifdef DIAGNOSTIC
329f1250445SBjoern A. Zeeb 		log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold6 to %d",
330f1250445SBjoern A. Zeeb 		    V_rtq_reallyold6);
33182cd038dSYoshinobu Inoue #endif
33282cd038dSYoshinobu Inoue 		arg.found = arg.killed = 0;
33382cd038dSYoshinobu Inoue 		arg.updating = 1;
334956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK(rnh);
33582cd038dSYoshinobu Inoue 		rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
336956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_UNLOCK(rnh);
33782cd038dSYoshinobu Inoue 	}
33882cd038dSYoshinobu Inoue 
33982cd038dSYoshinobu Inoue 	atv.tv_usec = 0;
34063721457SOleg Bulyzhin 	atv.tv_sec = arg.nextstop - time_uptime;
341603724d3SBjoern A. Zeeb 	callout_reset(&V_rtq_timer6, tvtohz(&atv), in6_rtqtimo, rock);
3428b615593SMarko Zec 	CURVNET_RESTORE();
34382cd038dSYoshinobu Inoue }
34482cd038dSYoshinobu Inoue 
34582cd038dSYoshinobu Inoue /*
34682cd038dSYoshinobu Inoue  * Age old PMTUs.
34782cd038dSYoshinobu Inoue  */
34882cd038dSYoshinobu Inoue struct mtuex_arg {
34982cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
35082cd038dSYoshinobu Inoue 	time_t nextstop;
35182cd038dSYoshinobu Inoue };
35244e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
353d1dd20beSSam Leffler static struct callout rtq_mtutimer;
35444e33a07SMarko Zec #endif
35582cd038dSYoshinobu Inoue 
35682cd038dSYoshinobu Inoue static int
35782cd038dSYoshinobu Inoue in6_mtuexpire(struct radix_node *rn, void *rock)
35882cd038dSYoshinobu Inoue {
35982cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
36082cd038dSYoshinobu Inoue 	struct mtuex_arg *ap = rock;
36182cd038dSYoshinobu Inoue 
36282cd038dSYoshinobu Inoue 	/* sanity */
36382cd038dSYoshinobu Inoue 	if (!rt)
36482cd038dSYoshinobu Inoue 		panic("rt == NULL in in6_mtuexpire");
36582cd038dSYoshinobu Inoue 
36682cd038dSYoshinobu Inoue 	if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
367fe53256dSAndre Oppermann 		if (rt->rt_rmx.rmx_expire <= time_uptime) {
36882cd038dSYoshinobu Inoue 			rt->rt_flags |= RTF_PROBEMTU;
36982cd038dSYoshinobu Inoue 		} else {
37082cd038dSYoshinobu Inoue 			ap->nextstop = lmin(ap->nextstop,
37182cd038dSYoshinobu Inoue 					rt->rt_rmx.rmx_expire);
37282cd038dSYoshinobu Inoue 		}
37382cd038dSYoshinobu Inoue 	}
37482cd038dSYoshinobu Inoue 
37582cd038dSYoshinobu Inoue 	return 0;
37682cd038dSYoshinobu Inoue }
37782cd038dSYoshinobu Inoue 
37882cd038dSYoshinobu Inoue #define	MTUTIMO_DEFAULT	(60*1)
37982cd038dSYoshinobu Inoue 
38082cd038dSYoshinobu Inoue static void
38182cd038dSYoshinobu Inoue in6_mtutimo(void *rock)
38282cd038dSYoshinobu Inoue {
3838b615593SMarko Zec 	CURVNET_SET_QUIET((struct vnet *) rock);
384093f25f8SMarko Zec 	INIT_VNET_INET6(curvnet);
385c2c2a7c1SBjoern A. Zeeb 	struct radix_node_head *rnh;
38682cd038dSYoshinobu Inoue 	struct mtuex_arg arg;
38782cd038dSYoshinobu Inoue 	struct timeval atv;
38882cd038dSYoshinobu Inoue 
389c2c2a7c1SBjoern A. Zeeb 	rnh = rt_tables_get_rnh(0, AF_INET6);
390c2c2a7c1SBjoern A. Zeeb 	if (rnh == NULL) {
391c2c2a7c1SBjoern A. Zeeb 		CURVNET_RESTORE();
392c2c2a7c1SBjoern A. Zeeb 		return;
393c2c2a7c1SBjoern A. Zeeb 	}
39482cd038dSYoshinobu Inoue 	arg.rnh = rnh;
39563721457SOleg Bulyzhin 	arg.nextstop = time_uptime + MTUTIMO_DEFAULT;
396956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
39782cd038dSYoshinobu Inoue 	rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
398956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
39982cd038dSYoshinobu Inoue 
40082cd038dSYoshinobu Inoue 	atv.tv_usec = 0;
40163721457SOleg Bulyzhin 	atv.tv_sec = arg.nextstop - time_uptime;
4026f9e3ebfSSUZUKI Shinsuke 	if (atv.tv_sec < 0) {
40382cd038dSYoshinobu Inoue 		printf("invalid mtu expiration time on routing table\n");
40463721457SOleg Bulyzhin 		arg.nextstop = time_uptime + 30;	/* last resort */
40563721457SOleg Bulyzhin 		atv.tv_sec = 30;
40682cd038dSYoshinobu Inoue 	}
407603724d3SBjoern A. Zeeb 	callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock);
4088b615593SMarko Zec 	CURVNET_RESTORE();
40982cd038dSYoshinobu Inoue }
41082cd038dSYoshinobu Inoue 
41182cd038dSYoshinobu Inoue /*
41282cd038dSYoshinobu Inoue  * Initialize our routing tree.
4138b07e49aSJulian Elischer  * XXX MRT When off == 0, we are being called from vfs_export.c
4148b07e49aSJulian Elischer  * so just set up their table and leave. (we know what the correct
4158b07e49aSJulian Elischer  * value should be so just use that).. FIX AFTER RELENG_7 is MFC'd
4168b07e49aSJulian Elischer  * see also comments in in_inithead() vfs_export.c and domain.h
41782cd038dSYoshinobu Inoue  */
41882cd038dSYoshinobu Inoue int
41982cd038dSYoshinobu Inoue in6_inithead(void **head, int off)
42082cd038dSYoshinobu Inoue {
4218b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
42282cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
42382cd038dSYoshinobu Inoue 
4248b07e49aSJulian Elischer 	if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3))
4258b07e49aSJulian Elischer 		return 0;		/* See above */
42682cd038dSYoshinobu Inoue 
4278b07e49aSJulian Elischer 	if (off == 0)		/* See above */
4288b07e49aSJulian Elischer 		return 1;	/* only do the rest for the real thing */
42982cd038dSYoshinobu Inoue 
43044e33a07SMarko Zec 	V_rtq_reallyold6 = 60*60; /* one hour is ``really old'' */
43144e33a07SMarko Zec 	V_rtq_minreallyold6 = 10; /* never automatically crank down to less */
43244e33a07SMarko Zec 	V_rtq_toomany6 = 128;	  /* 128 cached routes is ``too many'' */
43344e33a07SMarko Zec 	V_rtq_timeout6 = RTQ_TIMEOUT;
43444e33a07SMarko Zec 
43582cd038dSYoshinobu Inoue 	rnh = *head;
436c2c2a7c1SBjoern A. Zeeb 	KASSERT(rnh == rt_tables_get_rnh(0, AF_INET6), ("rnh?"));
43782cd038dSYoshinobu Inoue 	rnh->rnh_addaddr = in6_addroute;
43882cd038dSYoshinobu Inoue 	rnh->rnh_matchaddr = in6_matroute;
439603724d3SBjoern A. Zeeb 	callout_init(&V_rtq_timer6, CALLOUT_MPSAFE);
440603724d3SBjoern A. Zeeb 	callout_init(&V_rtq_mtutimer, CALLOUT_MPSAFE);
44121ca7b57SMarko Zec 	in6_rtqtimo(curvnet);	/* kick off timeout first time */
44221ca7b57SMarko Zec 	in6_mtutimo(curvnet);	/* kick off timeout first time */
44382cd038dSYoshinobu Inoue 	return 1;
44482cd038dSYoshinobu Inoue }
445bc29160dSMarko Zec 
446bc29160dSMarko Zec #ifdef VIMAGE
447bc29160dSMarko Zec int
448bc29160dSMarko Zec in6_detachhead(void **head, int off)
449bc29160dSMarko Zec {
450bc29160dSMarko Zec 	INIT_VNET_INET6(curvnet);
451bc29160dSMarko Zec 
452bc29160dSMarko Zec 	callout_drain(&V_rtq_timer6);
453bc29160dSMarko Zec 	callout_drain(&V_rtq_mtutimer);
454bc29160dSMarko Zec 	return (1);
455bc29160dSMarko Zec }
456bc29160dSMarko Zec #endif
457