xref: /freebsd/sys/netinet6/in6_rmx.c (revision bc29160df3d0b3a65d9b85a63a4589f1f9652e6b)
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 
7833553d6eSBjoern A. Zeeb #include "opt_route.h"
7933553d6eSBjoern A. Zeeb 
8082cd038dSYoshinobu Inoue #include <sys/param.h>
8182cd038dSYoshinobu Inoue #include <sys/systm.h>
8282cd038dSYoshinobu Inoue #include <sys/kernel.h>
83609ff41fSWarner Losh #include <sys/lock.h>
8482cd038dSYoshinobu Inoue #include <sys/sysctl.h>
8582cd038dSYoshinobu Inoue #include <sys/queue.h>
8682cd038dSYoshinobu Inoue #include <sys/socket.h>
8782cd038dSYoshinobu Inoue #include <sys/socketvar.h>
8882cd038dSYoshinobu Inoue #include <sys/mbuf.h>
893120b9d4SKip Macy #include <sys/rwlock.h>
9082cd038dSYoshinobu Inoue #include <sys/syslog.h>
91d1dd20beSSam Leffler #include <sys/callout.h>
92603724d3SBjoern A. Zeeb #include <sys/vimage.h>
9382cd038dSYoshinobu Inoue 
9482cd038dSYoshinobu Inoue #include <net/if.h>
9582cd038dSYoshinobu Inoue #include <net/route.h>
964b79449eSBjoern A. Zeeb #include <net/vnet.h>
974b79449eSBjoern A. Zeeb 
9882cd038dSYoshinobu Inoue #include <netinet/in.h>
9982cd038dSYoshinobu Inoue #include <netinet/ip_var.h>
10082cd038dSYoshinobu Inoue #include <netinet/in_var.h>
10182cd038dSYoshinobu Inoue 
102686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
10382cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
10482cd038dSYoshinobu Inoue 
105686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
10631b3783cSHajimu UMEMOTO #include <netinet6/nd6.h>
1074b79449eSBjoern A. Zeeb #include <netinet6/vinet6.h>
10882cd038dSYoshinobu Inoue 
10982cd038dSYoshinobu Inoue #include <netinet/tcp.h>
11082cd038dSYoshinobu Inoue #include <netinet/tcp_seq.h>
11182cd038dSYoshinobu Inoue #include <netinet/tcp_timer.h>
11282cd038dSYoshinobu Inoue #include <netinet/tcp_var.h>
11382cd038dSYoshinobu Inoue 
1149233d8f3SDavid E. O'Brien extern int	in6_inithead(void **head, int off);
115bc29160dSMarko Zec #ifdef VIMAGE
116bc29160dSMarko Zec extern int	in6_detachhead(void **head, int off);
117bc29160dSMarko Zec #endif
11882cd038dSYoshinobu Inoue 
11982cd038dSYoshinobu Inoue #define RTPRF_OURS		RTF_PROTO3	/* set on routes we manage */
12082cd038dSYoshinobu Inoue 
12182cd038dSYoshinobu Inoue /*
12282cd038dSYoshinobu Inoue  * Do what we need to do when inserting a route.
12382cd038dSYoshinobu Inoue  */
12482cd038dSYoshinobu Inoue static struct radix_node *
12582cd038dSYoshinobu Inoue in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
12682cd038dSYoshinobu Inoue     struct radix_node *treenodes)
12782cd038dSYoshinobu Inoue {
12882cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)treenodes;
12982cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
13082cd038dSYoshinobu Inoue 	struct radix_node *ret;
13182cd038dSYoshinobu Inoue 
1326e6b3f7cSQing Li 	RADIX_NODE_HEAD_WLOCK_ASSERT(head);
13382cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
13482cd038dSYoshinobu Inoue 		rt->rt_flags |= RTF_MULTICAST;
13582cd038dSYoshinobu Inoue 
13682cd038dSYoshinobu Inoue 	/*
13782cd038dSYoshinobu Inoue 	 * A little bit of help for both IPv6 output and input:
13882cd038dSYoshinobu Inoue 	 *   For local addresses, we make sure that RTF_LOCAL is set,
13982cd038dSYoshinobu Inoue 	 *   with the thought that this might one day be used to speed up
14082cd038dSYoshinobu Inoue 	 *   ip_input().
14182cd038dSYoshinobu Inoue 	 *
14282cd038dSYoshinobu Inoue 	 * We also mark routes to multicast addresses as such, because
14382cd038dSYoshinobu Inoue 	 * it's easy to do and might be useful (but this is much more
14482cd038dSYoshinobu Inoue 	 * dubious since it's so easy to inspect the address).  (This
14582cd038dSYoshinobu Inoue 	 * is done above.)
14682cd038dSYoshinobu Inoue 	 *
14782cd038dSYoshinobu Inoue 	 * XXX
14882cd038dSYoshinobu Inoue 	 * should elaborate the code.
14982cd038dSYoshinobu Inoue 	 */
15082cd038dSYoshinobu Inoue 	if (rt->rt_flags & RTF_HOST) {
15182cd038dSYoshinobu Inoue 		if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
15282cd038dSYoshinobu Inoue 					->sin6_addr,
15382cd038dSYoshinobu Inoue 				       &sin6->sin6_addr)) {
15482cd038dSYoshinobu Inoue 			rt->rt_flags |= RTF_LOCAL;
15582cd038dSYoshinobu Inoue 		}
15682cd038dSYoshinobu Inoue 	}
15782cd038dSYoshinobu Inoue 
15897d8d152SAndre Oppermann 	if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp)
15931b3783cSHajimu UMEMOTO 		rt->rt_rmx.rmx_mtu = IN6_LINKMTU(rt->rt_ifp);
16082cd038dSYoshinobu Inoue 
16182cd038dSYoshinobu Inoue 	ret = rn_addroute(v_arg, n_arg, head, treenodes);
1626e6b3f7cSQing Li 	if (ret == NULL) {
16382cd038dSYoshinobu Inoue 		struct rtentry *rt2;
16482cd038dSYoshinobu Inoue 		/*
16582cd038dSYoshinobu Inoue 		 * We are trying to add a net route, but can't.
16682cd038dSYoshinobu Inoue 		 * The following case should be allowed, so we'll make a
16782cd038dSYoshinobu Inoue 		 * special check for this:
16882cd038dSYoshinobu Inoue 		 *	Two IPv6 addresses with the same prefix is assigned
16982cd038dSYoshinobu Inoue 		 *	to a single interrface.
17082cd038dSYoshinobu Inoue 		 *	# ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
17182cd038dSYoshinobu Inoue 		 *	# ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
17282cd038dSYoshinobu Inoue 		 *	In this case, (*1) and (*2) want to add the same
17382cd038dSYoshinobu Inoue 		 *	net route entry, 3ffe:0501:: -> if0.
17482cd038dSYoshinobu Inoue 		 *	This case should not raise an error.
17582cd038dSYoshinobu Inoue 		 */
1766e6b3f7cSQing Li 		rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_RNH_LOCKED);
17782cd038dSYoshinobu Inoue 		if (rt2) {
1786e6b3f7cSQing Li 			if (((rt2->rt_flags & (RTF_HOST|RTF_GATEWAY)) == 0)
17982cd038dSYoshinobu Inoue 			 && rt2->rt_gateway
18082cd038dSYoshinobu Inoue 			 && rt2->rt_gateway->sa_family == AF_LINK
18182cd038dSYoshinobu Inoue 			 && rt2->rt_ifp == rt->rt_ifp) {
18282cd038dSYoshinobu Inoue 				ret = rt2->rt_nodes;
18382cd038dSYoshinobu Inoue 			}
184d1dd20beSSam Leffler 			RTFREE_LOCKED(rt2);
18582cd038dSYoshinobu Inoue 		}
18682cd038dSYoshinobu Inoue 	}
1876e6b3f7cSQing Li 	return (ret);
18882cd038dSYoshinobu Inoue }
18982cd038dSYoshinobu Inoue 
19082cd038dSYoshinobu Inoue /*
19182cd038dSYoshinobu Inoue  * This code is the inverse of in6_clsroute: on first reference, if we
19282cd038dSYoshinobu Inoue  * were managing the route, stop doing so and set the expiration timer
19382cd038dSYoshinobu Inoue  * back off again.
19482cd038dSYoshinobu Inoue  */
19582cd038dSYoshinobu Inoue static struct radix_node *
19682cd038dSYoshinobu Inoue in6_matroute(void *v_arg, struct radix_node_head *head)
19782cd038dSYoshinobu Inoue {
19882cd038dSYoshinobu Inoue 	struct radix_node *rn = rn_match(v_arg, head);
19982cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
20082cd038dSYoshinobu Inoue 
20182cd038dSYoshinobu Inoue 	if (rt && rt->rt_refcnt == 0) { /* this is first reference */
20282cd038dSYoshinobu Inoue 		if (rt->rt_flags & RTPRF_OURS) {
20382cd038dSYoshinobu Inoue 			rt->rt_flags &= ~RTPRF_OURS;
20482cd038dSYoshinobu Inoue 			rt->rt_rmx.rmx_expire = 0;
20582cd038dSYoshinobu Inoue 		}
20682cd038dSYoshinobu Inoue 	}
20782cd038dSYoshinobu Inoue 	return rn;
20882cd038dSYoshinobu Inoue }
20982cd038dSYoshinobu Inoue 
2107ee982bcSJun-ichiro itojun Hagino SYSCTL_DECL(_net_inet6_ip6);
2117ee982bcSJun-ichiro itojun Hagino 
21244e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
21344e33a07SMarko Zec static int rtq_reallyold6;
21444e33a07SMarko Zec static int rtq_minreallyold6;
21544e33a07SMarko Zec static int rtq_toomany6;
21644e33a07SMarko Zec #endif
21744e33a07SMarko Zec 
21897021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTEXPIRE,
21997021c24SMarko Zec     rtexpire, CTLFLAG_RW, rtq_reallyold6 , 0, "");
22082cd038dSYoshinobu Inoue 
22197021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTMINEXPIRE,
22297021c24SMarko Zec     rtminexpire, CTLFLAG_RW, rtq_minreallyold6 , 0, "");
22382cd038dSYoshinobu Inoue 
22497021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTMAXCACHE,
22597021c24SMarko Zec     rtmaxcache, CTLFLAG_RW, rtq_toomany6 , 0, "");
22682cd038dSYoshinobu Inoue 
22782cd038dSYoshinobu Inoue 
22882cd038dSYoshinobu Inoue 
22982cd038dSYoshinobu Inoue struct rtqk_arg {
23082cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
23182cd038dSYoshinobu Inoue 	int mode;
23282cd038dSYoshinobu Inoue 	int updating;
23382cd038dSYoshinobu Inoue 	int draining;
23482cd038dSYoshinobu Inoue 	int killed;
23582cd038dSYoshinobu Inoue 	int found;
23682cd038dSYoshinobu Inoue 	time_t nextstop;
23782cd038dSYoshinobu Inoue };
23882cd038dSYoshinobu Inoue 
23982cd038dSYoshinobu Inoue /*
24082cd038dSYoshinobu Inoue  * Get rid of old routes.  When draining, this deletes everything, even when
24182cd038dSYoshinobu Inoue  * the timeout is not expired yet.  When updating, this makes sure that
242f1250445SBjoern A. Zeeb  * nothing has a timeout longer than the current value of rtq_reallyold6.
24382cd038dSYoshinobu Inoue  */
24482cd038dSYoshinobu Inoue static int
24582cd038dSYoshinobu Inoue in6_rtqkill(struct radix_node *rn, void *rock)
24682cd038dSYoshinobu Inoue {
2478b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
24882cd038dSYoshinobu Inoue 	struct rtqk_arg *ap = rock;
24982cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
25082cd038dSYoshinobu Inoue 	int err;
25182cd038dSYoshinobu Inoue 
252a714e55fSRobert Watson 	RADIX_NODE_HEAD_WLOCK_ASSERT(ap->rnh);
253a714e55fSRobert Watson 
25482cd038dSYoshinobu Inoue 	if (rt->rt_flags & RTPRF_OURS) {
25582cd038dSYoshinobu Inoue 		ap->found++;
25682cd038dSYoshinobu Inoue 
257fe53256dSAndre Oppermann 		if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) {
25882cd038dSYoshinobu Inoue 			if (rt->rt_refcnt > 0)
25982cd038dSYoshinobu Inoue 				panic("rtqkill route really not free");
26082cd038dSYoshinobu Inoue 
26182cd038dSYoshinobu Inoue 			err = rtrequest(RTM_DELETE,
26282cd038dSYoshinobu Inoue 					(struct sockaddr *)rt_key(rt),
26382cd038dSYoshinobu Inoue 					rt->rt_gateway, rt_mask(rt),
2646e6b3f7cSQing Li 					rt->rt_flags|RTF_RNH_LOCKED, 0);
26582cd038dSYoshinobu Inoue 			if (err) {
26682cd038dSYoshinobu Inoue 				log(LOG_WARNING, "in6_rtqkill: error %d", err);
26782cd038dSYoshinobu Inoue 			} else {
26882cd038dSYoshinobu Inoue 				ap->killed++;
26982cd038dSYoshinobu Inoue 			}
27082cd038dSYoshinobu Inoue 		} else {
27182cd038dSYoshinobu Inoue 			if (ap->updating
272fe53256dSAndre Oppermann 			   && (rt->rt_rmx.rmx_expire - time_uptime
273f1250445SBjoern A. Zeeb 			       > V_rtq_reallyold6)) {
274fe53256dSAndre Oppermann 				rt->rt_rmx.rmx_expire = time_uptime
275f1250445SBjoern A. Zeeb 					+ V_rtq_reallyold6;
27682cd038dSYoshinobu Inoue 			}
27782cd038dSYoshinobu Inoue 			ap->nextstop = lmin(ap->nextstop,
27882cd038dSYoshinobu Inoue 					    rt->rt_rmx.rmx_expire);
27982cd038dSYoshinobu Inoue 		}
28082cd038dSYoshinobu Inoue 	}
28182cd038dSYoshinobu Inoue 
28282cd038dSYoshinobu Inoue 	return 0;
28382cd038dSYoshinobu Inoue }
28482cd038dSYoshinobu Inoue 
28582cd038dSYoshinobu Inoue #define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
28644e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
28744e33a07SMarko Zec static int rtq_timeout6;
2882ce7b410SAlexander Kabaev static struct callout rtq_timer6;
28944e33a07SMarko Zec #endif
29082cd038dSYoshinobu Inoue 
29182cd038dSYoshinobu Inoue static void
29282cd038dSYoshinobu Inoue in6_rtqtimo(void *rock)
29382cd038dSYoshinobu Inoue {
2948b615593SMarko Zec 	CURVNET_SET_QUIET((struct vnet *) rock);
295093f25f8SMarko Zec 	INIT_VNET_INET6(curvnet);
296c2c2a7c1SBjoern A. Zeeb 	struct radix_node_head *rnh;
29782cd038dSYoshinobu Inoue 	struct rtqk_arg arg;
29882cd038dSYoshinobu Inoue 	struct timeval atv;
29982cd038dSYoshinobu Inoue 	static time_t last_adjusted_timeout = 0;
30082cd038dSYoshinobu Inoue 
301c2c2a7c1SBjoern A. Zeeb 	rnh = rt_tables_get_rnh(0, AF_INET6);
302c2c2a7c1SBjoern A. Zeeb 	if (rnh == NULL) {
303c2c2a7c1SBjoern A. Zeeb 		CURVNET_RESTORE();
304c2c2a7c1SBjoern A. Zeeb 		return;
305c2c2a7c1SBjoern A. Zeeb 	}
30682cd038dSYoshinobu Inoue 	arg.found = arg.killed = 0;
30782cd038dSYoshinobu Inoue 	arg.rnh = rnh;
308603724d3SBjoern A. Zeeb 	arg.nextstop = time_uptime + V_rtq_timeout6;
30982cd038dSYoshinobu Inoue 	arg.draining = arg.updating = 0;
310956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
31182cd038dSYoshinobu Inoue 	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
312956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
31382cd038dSYoshinobu Inoue 
31482cd038dSYoshinobu Inoue 	/*
31582cd038dSYoshinobu Inoue 	 * Attempt to be somewhat dynamic about this:
31682cd038dSYoshinobu Inoue 	 * If there are ``too many'' routes sitting around taking up space,
31782cd038dSYoshinobu Inoue 	 * then crank down the timeout, and see if we can't make some more
31882cd038dSYoshinobu Inoue 	 * go away.  However, we make sure that we will never adjust more
3195f9a5768SJulian Elischer 	 * than once in rtq_timeout6 seconds, to keep from cranking down too
32082cd038dSYoshinobu Inoue 	 * hard.
32182cd038dSYoshinobu Inoue 	 */
322f1250445SBjoern A. Zeeb 	if ((arg.found - arg.killed > V_rtq_toomany6)
323603724d3SBjoern A. Zeeb 	   && (time_uptime - last_adjusted_timeout >= V_rtq_timeout6)
324f1250445SBjoern A. Zeeb 	   && V_rtq_reallyold6 > V_rtq_minreallyold6) {
325f1250445SBjoern A. Zeeb 		V_rtq_reallyold6 = 2*V_rtq_reallyold6 / 3;
326f1250445SBjoern A. Zeeb 		if (V_rtq_reallyold6 < V_rtq_minreallyold6) {
327f1250445SBjoern A. Zeeb 			V_rtq_reallyold6 = V_rtq_minreallyold6;
32882cd038dSYoshinobu Inoue 		}
32982cd038dSYoshinobu Inoue 
33063721457SOleg Bulyzhin 		last_adjusted_timeout = time_uptime;
33182cd038dSYoshinobu Inoue #ifdef DIAGNOSTIC
332f1250445SBjoern A. Zeeb 		log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold6 to %d",
333f1250445SBjoern A. Zeeb 		    V_rtq_reallyold6);
33482cd038dSYoshinobu Inoue #endif
33582cd038dSYoshinobu Inoue 		arg.found = arg.killed = 0;
33682cd038dSYoshinobu Inoue 		arg.updating = 1;
337956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK(rnh);
33882cd038dSYoshinobu Inoue 		rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
339956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_UNLOCK(rnh);
34082cd038dSYoshinobu Inoue 	}
34182cd038dSYoshinobu Inoue 
34282cd038dSYoshinobu Inoue 	atv.tv_usec = 0;
34363721457SOleg Bulyzhin 	atv.tv_sec = arg.nextstop - time_uptime;
344603724d3SBjoern A. Zeeb 	callout_reset(&V_rtq_timer6, tvtohz(&atv), in6_rtqtimo, rock);
3458b615593SMarko Zec 	CURVNET_RESTORE();
34682cd038dSYoshinobu Inoue }
34782cd038dSYoshinobu Inoue 
34882cd038dSYoshinobu Inoue /*
34982cd038dSYoshinobu Inoue  * Age old PMTUs.
35082cd038dSYoshinobu Inoue  */
35182cd038dSYoshinobu Inoue struct mtuex_arg {
35282cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
35382cd038dSYoshinobu Inoue 	time_t nextstop;
35482cd038dSYoshinobu Inoue };
35544e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
356d1dd20beSSam Leffler static struct callout rtq_mtutimer;
35744e33a07SMarko Zec #endif
35882cd038dSYoshinobu Inoue 
35982cd038dSYoshinobu Inoue static int
36082cd038dSYoshinobu Inoue in6_mtuexpire(struct radix_node *rn, void *rock)
36182cd038dSYoshinobu Inoue {
36282cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
36382cd038dSYoshinobu Inoue 	struct mtuex_arg *ap = rock;
36482cd038dSYoshinobu Inoue 
36582cd038dSYoshinobu Inoue 	/* sanity */
36682cd038dSYoshinobu Inoue 	if (!rt)
36782cd038dSYoshinobu Inoue 		panic("rt == NULL in in6_mtuexpire");
36882cd038dSYoshinobu Inoue 
36982cd038dSYoshinobu Inoue 	if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
370fe53256dSAndre Oppermann 		if (rt->rt_rmx.rmx_expire <= time_uptime) {
37182cd038dSYoshinobu Inoue 			rt->rt_flags |= RTF_PROBEMTU;
37282cd038dSYoshinobu Inoue 		} else {
37382cd038dSYoshinobu Inoue 			ap->nextstop = lmin(ap->nextstop,
37482cd038dSYoshinobu Inoue 					rt->rt_rmx.rmx_expire);
37582cd038dSYoshinobu Inoue 		}
37682cd038dSYoshinobu Inoue 	}
37782cd038dSYoshinobu Inoue 
37882cd038dSYoshinobu Inoue 	return 0;
37982cd038dSYoshinobu Inoue }
38082cd038dSYoshinobu Inoue 
38182cd038dSYoshinobu Inoue #define	MTUTIMO_DEFAULT	(60*1)
38282cd038dSYoshinobu Inoue 
38382cd038dSYoshinobu Inoue static void
38482cd038dSYoshinobu Inoue in6_mtutimo(void *rock)
38582cd038dSYoshinobu Inoue {
3868b615593SMarko Zec 	CURVNET_SET_QUIET((struct vnet *) rock);
387093f25f8SMarko Zec 	INIT_VNET_INET6(curvnet);
388c2c2a7c1SBjoern A. Zeeb 	struct radix_node_head *rnh;
38982cd038dSYoshinobu Inoue 	struct mtuex_arg arg;
39082cd038dSYoshinobu Inoue 	struct timeval atv;
39182cd038dSYoshinobu Inoue 
392c2c2a7c1SBjoern A. Zeeb 	rnh = rt_tables_get_rnh(0, AF_INET6);
393c2c2a7c1SBjoern A. Zeeb 	if (rnh == NULL) {
394c2c2a7c1SBjoern A. Zeeb 		CURVNET_RESTORE();
395c2c2a7c1SBjoern A. Zeeb 		return;
396c2c2a7c1SBjoern A. Zeeb 	}
39782cd038dSYoshinobu Inoue 	arg.rnh = rnh;
39863721457SOleg Bulyzhin 	arg.nextstop = time_uptime + MTUTIMO_DEFAULT;
399956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
40082cd038dSYoshinobu Inoue 	rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
401956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
40282cd038dSYoshinobu Inoue 
40382cd038dSYoshinobu Inoue 	atv.tv_usec = 0;
40463721457SOleg Bulyzhin 	atv.tv_sec = arg.nextstop - time_uptime;
4056f9e3ebfSSUZUKI Shinsuke 	if (atv.tv_sec < 0) {
40682cd038dSYoshinobu Inoue 		printf("invalid mtu expiration time on routing table\n");
40763721457SOleg Bulyzhin 		arg.nextstop = time_uptime + 30;	/* last resort */
40863721457SOleg Bulyzhin 		atv.tv_sec = 30;
40982cd038dSYoshinobu Inoue 	}
410603724d3SBjoern A. Zeeb 	callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock);
4118b615593SMarko Zec 	CURVNET_RESTORE();
41282cd038dSYoshinobu Inoue }
41382cd038dSYoshinobu Inoue 
414686cdd19SJun-ichiro itojun Hagino #if 0
415686cdd19SJun-ichiro itojun Hagino void
4161272577eSXin LI in6_rtqdrain(void)
417686cdd19SJun-ichiro itojun Hagino {
4188b615593SMarko Zec 	INIT_VNET_NET(curvnet);
419c2c2a7c1SBjoern A. Zeeb 	struct radix_node_head *rnh;
420686cdd19SJun-ichiro itojun Hagino 	struct rtqk_arg arg;
421d1dd20beSSam Leffler 
422c2c2a7c1SBjoern A. Zeeb 	rnh = rt_tables_get_rnh(0, AF_INET6);
423c2c2a7c1SBjoern A. Zeeb 	if (rnh == NULL)
424c2c2a7c1SBjoern A. Zeeb 		panic("%s: rnh == NULL", __func__);
425686cdd19SJun-ichiro itojun Hagino 	arg.found = arg.killed = 0;
426686cdd19SJun-ichiro itojun Hagino 	arg.rnh = rnh;
427686cdd19SJun-ichiro itojun Hagino 	arg.nextstop = 0;
428686cdd19SJun-ichiro itojun Hagino 	arg.draining = 1;
429686cdd19SJun-ichiro itojun Hagino 	arg.updating = 0;
430956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
431686cdd19SJun-ichiro itojun Hagino 	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
432956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
433686cdd19SJun-ichiro itojun Hagino }
434686cdd19SJun-ichiro itojun Hagino #endif
435686cdd19SJun-ichiro itojun Hagino 
43682cd038dSYoshinobu Inoue /*
43782cd038dSYoshinobu Inoue  * Initialize our routing tree.
4388b07e49aSJulian Elischer  * XXX MRT When off == 0, we are being called from vfs_export.c
4398b07e49aSJulian Elischer  * so just set up their table and leave. (we know what the correct
4408b07e49aSJulian Elischer  * value should be so just use that).. FIX AFTER RELENG_7 is MFC'd
4418b07e49aSJulian Elischer  * see also comments in in_inithead() vfs_export.c and domain.h
44282cd038dSYoshinobu Inoue  */
44382cd038dSYoshinobu Inoue int
44482cd038dSYoshinobu Inoue in6_inithead(void **head, int off)
44582cd038dSYoshinobu Inoue {
4468b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
44782cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
44882cd038dSYoshinobu Inoue 
4498b07e49aSJulian Elischer 	if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3))
4508b07e49aSJulian Elischer 		return 0;		/* See above */
45182cd038dSYoshinobu Inoue 
4528b07e49aSJulian Elischer 	if (off == 0)		/* See above */
4538b07e49aSJulian Elischer 		return 1;	/* only do the rest for the real thing */
45482cd038dSYoshinobu Inoue 
45544e33a07SMarko Zec 	V_rtq_reallyold6 = 60*60; /* one hour is ``really old'' */
45644e33a07SMarko Zec 	V_rtq_minreallyold6 = 10; /* never automatically crank down to less */
45744e33a07SMarko Zec 	V_rtq_toomany6 = 128;	  /* 128 cached routes is ``too many'' */
45844e33a07SMarko Zec 	V_rtq_timeout6 = RTQ_TIMEOUT;
45944e33a07SMarko Zec 
46082cd038dSYoshinobu Inoue 	rnh = *head;
461c2c2a7c1SBjoern A. Zeeb 	KASSERT(rnh == rt_tables_get_rnh(0, AF_INET6), ("rnh?"));
46282cd038dSYoshinobu Inoue 	rnh->rnh_addaddr = in6_addroute;
46382cd038dSYoshinobu Inoue 	rnh->rnh_matchaddr = in6_matroute;
464603724d3SBjoern A. Zeeb 	callout_init(&V_rtq_timer6, CALLOUT_MPSAFE);
465603724d3SBjoern A. Zeeb 	callout_init(&V_rtq_mtutimer, CALLOUT_MPSAFE);
46621ca7b57SMarko Zec 	in6_rtqtimo(curvnet);	/* kick off timeout first time */
46721ca7b57SMarko Zec 	in6_mtutimo(curvnet);	/* kick off timeout first time */
46882cd038dSYoshinobu Inoue 	return 1;
46982cd038dSYoshinobu Inoue }
470bc29160dSMarko Zec 
471bc29160dSMarko Zec #ifdef VIMAGE
472bc29160dSMarko Zec int
473bc29160dSMarko Zec in6_detachhead(void **head, int off)
474bc29160dSMarko Zec {
475bc29160dSMarko Zec 	INIT_VNET_INET6(curvnet);
476bc29160dSMarko Zec 
477bc29160dSMarko Zec 	callout_drain(&V_rtq_timer6);
478bc29160dSMarko Zec 	callout_drain(&V_rtq_mtutimer);
479bc29160dSMarko Zec 	return (1);
480bc29160dSMarko Zec }
481bc29160dSMarko Zec #endif
482