xref: /freebsd/sys/netinet6/in6_rmx.c (revision 609ff41f16fa1c27e0c10c9465a74d2ebe306827)
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>
9382cd038dSYoshinobu Inoue #include <net/route.h>
944b79449eSBjoern A. Zeeb #include <net/vnet.h>
954b79449eSBjoern A. Zeeb 
9682cd038dSYoshinobu Inoue #include <netinet/in.h>
9782cd038dSYoshinobu Inoue #include <netinet/ip_var.h>
9882cd038dSYoshinobu Inoue #include <netinet/in_var.h>
9982cd038dSYoshinobu Inoue 
100686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
10182cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
10282cd038dSYoshinobu Inoue 
103686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
10431b3783cSHajimu UMEMOTO #include <netinet6/nd6.h>
1054b79449eSBjoern A. Zeeb #include <netinet6/vinet6.h>
10682cd038dSYoshinobu Inoue 
10782cd038dSYoshinobu Inoue #include <netinet/tcp.h>
10882cd038dSYoshinobu Inoue #include <netinet/tcp_seq.h>
10982cd038dSYoshinobu Inoue #include <netinet/tcp_timer.h>
11082cd038dSYoshinobu Inoue #include <netinet/tcp_var.h>
11182cd038dSYoshinobu Inoue 
1129233d8f3SDavid E. O'Brien extern int	in6_inithead(void **head, int off);
11382cd038dSYoshinobu Inoue 
11482cd038dSYoshinobu Inoue #define RTPRF_OURS		RTF_PROTO3	/* set on routes we manage */
11582cd038dSYoshinobu Inoue 
11682cd038dSYoshinobu Inoue /*
11782cd038dSYoshinobu Inoue  * Do what we need to do when inserting a route.
11882cd038dSYoshinobu Inoue  */
11982cd038dSYoshinobu Inoue static struct radix_node *
12082cd038dSYoshinobu Inoue in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
12182cd038dSYoshinobu Inoue     struct radix_node *treenodes)
12282cd038dSYoshinobu Inoue {
12382cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)treenodes;
12482cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
12582cd038dSYoshinobu Inoue 	struct radix_node *ret;
12682cd038dSYoshinobu Inoue 
12782cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
12882cd038dSYoshinobu Inoue 		rt->rt_flags |= RTF_MULTICAST;
12982cd038dSYoshinobu Inoue 
13082cd038dSYoshinobu Inoue 	/*
13182cd038dSYoshinobu Inoue 	 * A little bit of help for both IPv6 output and input:
13282cd038dSYoshinobu Inoue 	 *   For local addresses, we make sure that RTF_LOCAL is set,
13382cd038dSYoshinobu Inoue 	 *   with the thought that this might one day be used to speed up
13482cd038dSYoshinobu Inoue 	 *   ip_input().
13582cd038dSYoshinobu Inoue 	 *
13682cd038dSYoshinobu Inoue 	 * We also mark routes to multicast addresses as such, because
13782cd038dSYoshinobu Inoue 	 * it's easy to do and might be useful (but this is much more
13882cd038dSYoshinobu Inoue 	 * dubious since it's so easy to inspect the address).  (This
13982cd038dSYoshinobu Inoue 	 * is done above.)
14082cd038dSYoshinobu Inoue 	 *
14182cd038dSYoshinobu Inoue 	 * XXX
14282cd038dSYoshinobu Inoue 	 * should elaborate the code.
14382cd038dSYoshinobu Inoue 	 */
14482cd038dSYoshinobu Inoue 	if (rt->rt_flags & RTF_HOST) {
14582cd038dSYoshinobu Inoue 		if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
14682cd038dSYoshinobu Inoue 					->sin6_addr,
14782cd038dSYoshinobu Inoue 				       &sin6->sin6_addr)) {
14882cd038dSYoshinobu Inoue 			rt->rt_flags |= RTF_LOCAL;
14982cd038dSYoshinobu Inoue 		}
15082cd038dSYoshinobu Inoue 	}
15182cd038dSYoshinobu Inoue 
15297d8d152SAndre Oppermann 	if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp)
15331b3783cSHajimu UMEMOTO 		rt->rt_rmx.rmx_mtu = IN6_LINKMTU(rt->rt_ifp);
15482cd038dSYoshinobu Inoue 
15582cd038dSYoshinobu Inoue 	ret = rn_addroute(v_arg, n_arg, head, treenodes);
15682cd038dSYoshinobu Inoue 	if (ret == NULL && rt->rt_flags & RTF_HOST) {
15782cd038dSYoshinobu Inoue 		struct rtentry *rt2;
15882cd038dSYoshinobu Inoue 		/*
15982cd038dSYoshinobu Inoue 		 * We are trying to add a host route, but can't.
16082cd038dSYoshinobu Inoue 		 * Find out if it is because of an
16182cd038dSYoshinobu Inoue 		 * ARP entry and delete it if so.
16282cd038dSYoshinobu Inoue 		 */
16326d02ca7SAndre Oppermann 		rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_CLONING);
16482cd038dSYoshinobu Inoue 		if (rt2) {
16582cd038dSYoshinobu Inoue 			if (rt2->rt_flags & RTF_LLINFO &&
16682cd038dSYoshinobu Inoue 				rt2->rt_flags & RTF_HOST &&
16782cd038dSYoshinobu Inoue 				rt2->rt_gateway &&
16882cd038dSYoshinobu Inoue 				rt2->rt_gateway->sa_family == AF_LINK) {
1699c63e9dbSSam Leffler 				rtexpunge(rt2);
1709c63e9dbSSam Leffler 				RTFREE_LOCKED(rt2);
17182cd038dSYoshinobu Inoue 				ret = rn_addroute(v_arg, n_arg, head,
17282cd038dSYoshinobu Inoue 					treenodes);
1739c63e9dbSSam Leffler 			} else
174d1dd20beSSam Leffler 				RTFREE_LOCKED(rt2);
17582cd038dSYoshinobu Inoue 		}
17682cd038dSYoshinobu Inoue 	} else if (ret == NULL && rt->rt_flags & RTF_CLONING) {
17782cd038dSYoshinobu Inoue 		struct rtentry *rt2;
17882cd038dSYoshinobu Inoue 		/*
17982cd038dSYoshinobu Inoue 		 * We are trying to add a net route, but can't.
18082cd038dSYoshinobu Inoue 		 * The following case should be allowed, so we'll make a
18182cd038dSYoshinobu Inoue 		 * special check for this:
18282cd038dSYoshinobu Inoue 		 *	Two IPv6 addresses with the same prefix is assigned
18382cd038dSYoshinobu Inoue 		 *	to a single interrface.
18482cd038dSYoshinobu Inoue 		 *	# ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
18582cd038dSYoshinobu Inoue 		 *	# ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
18682cd038dSYoshinobu Inoue 		 *	In this case, (*1) and (*2) want to add the same
18782cd038dSYoshinobu Inoue 		 *	net route entry, 3ffe:0501:: -> if0.
18882cd038dSYoshinobu Inoue 		 *	This case should not raise an error.
18982cd038dSYoshinobu Inoue 		 */
19026d02ca7SAndre Oppermann 		rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_CLONING);
19182cd038dSYoshinobu Inoue 		if (rt2) {
19282cd038dSYoshinobu Inoue 			if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY))
19382cd038dSYoshinobu Inoue 					== RTF_CLONING
19482cd038dSYoshinobu Inoue 			 && rt2->rt_gateway
19582cd038dSYoshinobu Inoue 			 && rt2->rt_gateway->sa_family == AF_LINK
19682cd038dSYoshinobu Inoue 			 && rt2->rt_ifp == rt->rt_ifp) {
19782cd038dSYoshinobu Inoue 				ret = rt2->rt_nodes;
19882cd038dSYoshinobu Inoue 			}
199d1dd20beSSam Leffler 			RTFREE_LOCKED(rt2);
20082cd038dSYoshinobu Inoue 		}
20182cd038dSYoshinobu Inoue 	}
20282cd038dSYoshinobu Inoue 	return ret;
20382cd038dSYoshinobu Inoue }
20482cd038dSYoshinobu Inoue 
20582cd038dSYoshinobu Inoue /*
20682cd038dSYoshinobu Inoue  * This code is the inverse of in6_clsroute: on first reference, if we
20782cd038dSYoshinobu Inoue  * were managing the route, stop doing so and set the expiration timer
20882cd038dSYoshinobu Inoue  * back off again.
20982cd038dSYoshinobu Inoue  */
21082cd038dSYoshinobu Inoue static struct radix_node *
21182cd038dSYoshinobu Inoue in6_matroute(void *v_arg, struct radix_node_head *head)
21282cd038dSYoshinobu Inoue {
21382cd038dSYoshinobu Inoue 	struct radix_node *rn = rn_match(v_arg, head);
21482cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
21582cd038dSYoshinobu Inoue 
21682cd038dSYoshinobu Inoue 	if (rt && rt->rt_refcnt == 0) { /* this is first reference */
21782cd038dSYoshinobu Inoue 		if (rt->rt_flags & RTPRF_OURS) {
21882cd038dSYoshinobu Inoue 			rt->rt_flags &= ~RTPRF_OURS;
21982cd038dSYoshinobu Inoue 			rt->rt_rmx.rmx_expire = 0;
22082cd038dSYoshinobu Inoue 		}
22182cd038dSYoshinobu Inoue 	}
22282cd038dSYoshinobu Inoue 	return rn;
22382cd038dSYoshinobu Inoue }
22482cd038dSYoshinobu Inoue 
2257ee982bcSJun-ichiro itojun Hagino SYSCTL_DECL(_net_inet6_ip6);
2267ee982bcSJun-ichiro itojun Hagino 
22744e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
22844e33a07SMarko Zec static int rtq_reallyold6;
22944e33a07SMarko Zec static int rtq_minreallyold6;
23044e33a07SMarko Zec static int rtq_toomany6;
23144e33a07SMarko Zec #endif
23244e33a07SMarko Zec 
23397021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTEXPIRE,
23497021c24SMarko Zec     rtexpire, CTLFLAG_RW, rtq_reallyold6 , 0, "");
23582cd038dSYoshinobu Inoue 
23697021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTMINEXPIRE,
23797021c24SMarko Zec     rtminexpire, CTLFLAG_RW, rtq_minreallyold6 , 0, "");
23882cd038dSYoshinobu Inoue 
23997021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTMAXCACHE,
24097021c24SMarko Zec     rtmaxcache, CTLFLAG_RW, rtq_toomany6 , 0, "");
24182cd038dSYoshinobu Inoue 
24282cd038dSYoshinobu Inoue 
24382cd038dSYoshinobu Inoue /*
24482cd038dSYoshinobu Inoue  * On last reference drop, mark the route as belong to us so that it can be
24582cd038dSYoshinobu Inoue  * timed out.
24682cd038dSYoshinobu Inoue  */
24782cd038dSYoshinobu Inoue static void
24882cd038dSYoshinobu Inoue in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
24982cd038dSYoshinobu Inoue {
2508b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
25182cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
25282cd038dSYoshinobu Inoue 
253d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
254d1dd20beSSam Leffler 
25582cd038dSYoshinobu Inoue 	if (!(rt->rt_flags & RTF_UP))
25682cd038dSYoshinobu Inoue 		return;		/* prophylactic measures */
25782cd038dSYoshinobu Inoue 
25882cd038dSYoshinobu Inoue 	if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
25982cd038dSYoshinobu Inoue 		return;
26082cd038dSYoshinobu Inoue 
261956b0b65SJeffrey Hsu 	if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
26282cd038dSYoshinobu Inoue 		return;
26382cd038dSYoshinobu Inoue 
26482cd038dSYoshinobu Inoue 	/*
26582cd038dSYoshinobu Inoue 	 * As requested by David Greenman:
266f1250445SBjoern A. Zeeb 	 * If rtq_reallyold6 is 0, just delete the route without
26782cd038dSYoshinobu Inoue 	 * waiting for a timeout cycle to kill it.
26882cd038dSYoshinobu Inoue 	 */
269f1250445SBjoern A. Zeeb 	if (V_rtq_reallyold6 != 0) {
27082cd038dSYoshinobu Inoue 		rt->rt_flags |= RTPRF_OURS;
271f1250445SBjoern A. Zeeb 		rt->rt_rmx.rmx_expire = time_uptime + V_rtq_reallyold6;
27282cd038dSYoshinobu Inoue 	} else {
2739c63e9dbSSam Leffler 		rtexpunge(rt);
27482cd038dSYoshinobu Inoue 	}
27582cd038dSYoshinobu Inoue }
27682cd038dSYoshinobu Inoue 
27782cd038dSYoshinobu Inoue struct rtqk_arg {
27882cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
27982cd038dSYoshinobu Inoue 	int mode;
28082cd038dSYoshinobu Inoue 	int updating;
28182cd038dSYoshinobu Inoue 	int draining;
28282cd038dSYoshinobu Inoue 	int killed;
28382cd038dSYoshinobu Inoue 	int found;
28482cd038dSYoshinobu Inoue 	time_t nextstop;
28582cd038dSYoshinobu Inoue };
28682cd038dSYoshinobu Inoue 
28782cd038dSYoshinobu Inoue /*
28882cd038dSYoshinobu Inoue  * Get rid of old routes.  When draining, this deletes everything, even when
28982cd038dSYoshinobu Inoue  * the timeout is not expired yet.  When updating, this makes sure that
290f1250445SBjoern A. Zeeb  * nothing has a timeout longer than the current value of rtq_reallyold6.
29182cd038dSYoshinobu Inoue  */
29282cd038dSYoshinobu Inoue static int
29382cd038dSYoshinobu Inoue in6_rtqkill(struct radix_node *rn, void *rock)
29482cd038dSYoshinobu Inoue {
2958b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
29682cd038dSYoshinobu Inoue 	struct rtqk_arg *ap = rock;
29782cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
29882cd038dSYoshinobu Inoue 	int err;
29982cd038dSYoshinobu Inoue 
30082cd038dSYoshinobu Inoue 	if (rt->rt_flags & RTPRF_OURS) {
30182cd038dSYoshinobu Inoue 		ap->found++;
30282cd038dSYoshinobu Inoue 
303fe53256dSAndre Oppermann 		if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) {
30482cd038dSYoshinobu Inoue 			if (rt->rt_refcnt > 0)
30582cd038dSYoshinobu Inoue 				panic("rtqkill route really not free");
30682cd038dSYoshinobu Inoue 
30782cd038dSYoshinobu Inoue 			err = rtrequest(RTM_DELETE,
30882cd038dSYoshinobu Inoue 					(struct sockaddr *)rt_key(rt),
30982cd038dSYoshinobu Inoue 					rt->rt_gateway, rt_mask(rt),
31082cd038dSYoshinobu Inoue 					rt->rt_flags, 0);
31182cd038dSYoshinobu Inoue 			if (err) {
31282cd038dSYoshinobu Inoue 				log(LOG_WARNING, "in6_rtqkill: error %d", err);
31382cd038dSYoshinobu Inoue 			} else {
31482cd038dSYoshinobu Inoue 				ap->killed++;
31582cd038dSYoshinobu Inoue 			}
31682cd038dSYoshinobu Inoue 		} else {
31782cd038dSYoshinobu Inoue 			if (ap->updating
318fe53256dSAndre Oppermann 			   && (rt->rt_rmx.rmx_expire - time_uptime
319f1250445SBjoern A. Zeeb 			       > V_rtq_reallyold6)) {
320fe53256dSAndre Oppermann 				rt->rt_rmx.rmx_expire = time_uptime
321f1250445SBjoern A. Zeeb 					+ V_rtq_reallyold6;
32282cd038dSYoshinobu Inoue 			}
32382cd038dSYoshinobu Inoue 			ap->nextstop = lmin(ap->nextstop,
32482cd038dSYoshinobu Inoue 					    rt->rt_rmx.rmx_expire);
32582cd038dSYoshinobu Inoue 		}
32682cd038dSYoshinobu Inoue 	}
32782cd038dSYoshinobu Inoue 
32882cd038dSYoshinobu Inoue 	return 0;
32982cd038dSYoshinobu Inoue }
33082cd038dSYoshinobu Inoue 
33182cd038dSYoshinobu Inoue #define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
33244e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
33344e33a07SMarko Zec static int rtq_timeout6;
3342ce7b410SAlexander Kabaev static struct callout rtq_timer6;
33544e33a07SMarko Zec #endif
33682cd038dSYoshinobu Inoue 
33782cd038dSYoshinobu Inoue static void
33882cd038dSYoshinobu Inoue in6_rtqtimo(void *rock)
33982cd038dSYoshinobu Inoue {
3408b615593SMarko Zec 	CURVNET_SET_QUIET((struct vnet *) rock);
3418b615593SMarko Zec 	INIT_VNET_NET((struct vnet *) rock);
3428b615593SMarko Zec 	INIT_VNET_INET6((struct vnet *) rock);
34382cd038dSYoshinobu Inoue 	struct radix_node_head *rnh = rock;
34482cd038dSYoshinobu Inoue 	struct rtqk_arg arg;
34582cd038dSYoshinobu Inoue 	struct timeval atv;
34682cd038dSYoshinobu Inoue 	static time_t last_adjusted_timeout = 0;
34782cd038dSYoshinobu Inoue 
34882cd038dSYoshinobu Inoue 	arg.found = arg.killed = 0;
34982cd038dSYoshinobu Inoue 	arg.rnh = rnh;
350603724d3SBjoern A. Zeeb 	arg.nextstop = time_uptime + V_rtq_timeout6;
35182cd038dSYoshinobu Inoue 	arg.draining = arg.updating = 0;
352956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
35382cd038dSYoshinobu Inoue 	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
354956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
35582cd038dSYoshinobu Inoue 
35682cd038dSYoshinobu Inoue 	/*
35782cd038dSYoshinobu Inoue 	 * Attempt to be somewhat dynamic about this:
35882cd038dSYoshinobu Inoue 	 * If there are ``too many'' routes sitting around taking up space,
35982cd038dSYoshinobu Inoue 	 * then crank down the timeout, and see if we can't make some more
36082cd038dSYoshinobu Inoue 	 * go away.  However, we make sure that we will never adjust more
3615f9a5768SJulian Elischer 	 * than once in rtq_timeout6 seconds, to keep from cranking down too
36282cd038dSYoshinobu Inoue 	 * hard.
36382cd038dSYoshinobu Inoue 	 */
364f1250445SBjoern A. Zeeb 	if ((arg.found - arg.killed > V_rtq_toomany6)
365603724d3SBjoern A. Zeeb 	   && (time_uptime - last_adjusted_timeout >= V_rtq_timeout6)
366f1250445SBjoern A. Zeeb 	   && V_rtq_reallyold6 > V_rtq_minreallyold6) {
367f1250445SBjoern A. Zeeb 		V_rtq_reallyold6 = 2*V_rtq_reallyold6 / 3;
368f1250445SBjoern A. Zeeb 		if (V_rtq_reallyold6 < V_rtq_minreallyold6) {
369f1250445SBjoern A. Zeeb 			V_rtq_reallyold6 = V_rtq_minreallyold6;
37082cd038dSYoshinobu Inoue 		}
37182cd038dSYoshinobu Inoue 
37263721457SOleg Bulyzhin 		last_adjusted_timeout = time_uptime;
37382cd038dSYoshinobu Inoue #ifdef DIAGNOSTIC
374f1250445SBjoern A. Zeeb 		log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold6 to %d",
375f1250445SBjoern A. Zeeb 		    V_rtq_reallyold6);
37682cd038dSYoshinobu Inoue #endif
37782cd038dSYoshinobu Inoue 		arg.found = arg.killed = 0;
37882cd038dSYoshinobu Inoue 		arg.updating = 1;
379956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK(rnh);
38082cd038dSYoshinobu Inoue 		rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
381956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_UNLOCK(rnh);
38282cd038dSYoshinobu Inoue 	}
38382cd038dSYoshinobu Inoue 
38482cd038dSYoshinobu Inoue 	atv.tv_usec = 0;
38563721457SOleg Bulyzhin 	atv.tv_sec = arg.nextstop - time_uptime;
386603724d3SBjoern A. Zeeb 	callout_reset(&V_rtq_timer6, tvtohz(&atv), in6_rtqtimo, rock);
3878b615593SMarko Zec 	CURVNET_RESTORE();
38882cd038dSYoshinobu Inoue }
38982cd038dSYoshinobu Inoue 
39082cd038dSYoshinobu Inoue /*
39182cd038dSYoshinobu Inoue  * Age old PMTUs.
39282cd038dSYoshinobu Inoue  */
39382cd038dSYoshinobu Inoue struct mtuex_arg {
39482cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
39582cd038dSYoshinobu Inoue 	time_t nextstop;
39682cd038dSYoshinobu Inoue };
39744e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
398d1dd20beSSam Leffler static struct callout rtq_mtutimer;
39944e33a07SMarko Zec #endif
40082cd038dSYoshinobu Inoue 
40182cd038dSYoshinobu Inoue static int
40282cd038dSYoshinobu Inoue in6_mtuexpire(struct radix_node *rn, void *rock)
40382cd038dSYoshinobu Inoue {
40482cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
40582cd038dSYoshinobu Inoue 	struct mtuex_arg *ap = rock;
40682cd038dSYoshinobu Inoue 
40782cd038dSYoshinobu Inoue 	/* sanity */
40882cd038dSYoshinobu Inoue 	if (!rt)
40982cd038dSYoshinobu Inoue 		panic("rt == NULL in in6_mtuexpire");
41082cd038dSYoshinobu Inoue 
41182cd038dSYoshinobu Inoue 	if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
412fe53256dSAndre Oppermann 		if (rt->rt_rmx.rmx_expire <= time_uptime) {
41382cd038dSYoshinobu Inoue 			rt->rt_flags |= RTF_PROBEMTU;
41482cd038dSYoshinobu Inoue 		} else {
41582cd038dSYoshinobu Inoue 			ap->nextstop = lmin(ap->nextstop,
41682cd038dSYoshinobu Inoue 					rt->rt_rmx.rmx_expire);
41782cd038dSYoshinobu Inoue 		}
41882cd038dSYoshinobu Inoue 	}
41982cd038dSYoshinobu Inoue 
42082cd038dSYoshinobu Inoue 	return 0;
42182cd038dSYoshinobu Inoue }
42282cd038dSYoshinobu Inoue 
42382cd038dSYoshinobu Inoue #define	MTUTIMO_DEFAULT	(60*1)
42482cd038dSYoshinobu Inoue 
42582cd038dSYoshinobu Inoue static void
42682cd038dSYoshinobu Inoue in6_mtutimo(void *rock)
42782cd038dSYoshinobu Inoue {
4288b615593SMarko Zec 	CURVNET_SET_QUIET((struct vnet *) rock);
4298b615593SMarko Zec 	INIT_VNET_NET((struct vnet *) rock);
4308b615593SMarko Zec 	INIT_VNET_INET6((struct vnet *) rock);
43182cd038dSYoshinobu Inoue 	struct radix_node_head *rnh = rock;
43282cd038dSYoshinobu Inoue 	struct mtuex_arg arg;
43382cd038dSYoshinobu Inoue 	struct timeval atv;
43482cd038dSYoshinobu Inoue 
43582cd038dSYoshinobu Inoue 	arg.rnh = rnh;
43663721457SOleg Bulyzhin 	arg.nextstop = time_uptime + MTUTIMO_DEFAULT;
437956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
43882cd038dSYoshinobu Inoue 	rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
439956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
44082cd038dSYoshinobu Inoue 
44182cd038dSYoshinobu Inoue 	atv.tv_usec = 0;
44263721457SOleg Bulyzhin 	atv.tv_sec = arg.nextstop - time_uptime;
4436f9e3ebfSSUZUKI Shinsuke 	if (atv.tv_sec < 0) {
44482cd038dSYoshinobu Inoue 		printf("invalid mtu expiration time on routing table\n");
44563721457SOleg Bulyzhin 		arg.nextstop = time_uptime + 30;	/* last resort */
44663721457SOleg Bulyzhin 		atv.tv_sec = 30;
44782cd038dSYoshinobu Inoue 	}
448603724d3SBjoern A. Zeeb 	callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock);
4498b615593SMarko Zec 	CURVNET_RESTORE();
45082cd038dSYoshinobu Inoue }
45182cd038dSYoshinobu Inoue 
452686cdd19SJun-ichiro itojun Hagino #if 0
453686cdd19SJun-ichiro itojun Hagino void
4541272577eSXin LI in6_rtqdrain(void)
455686cdd19SJun-ichiro itojun Hagino {
4568b615593SMarko Zec 	INIT_VNET_NET(curvnet);
457603724d3SBjoern A. Zeeb 	struct radix_node_head *rnh = V_rt_tables[AF_INET6];
458686cdd19SJun-ichiro itojun Hagino 	struct rtqk_arg arg;
459d1dd20beSSam Leffler 
460686cdd19SJun-ichiro itojun Hagino 	arg.found = arg.killed = 0;
461686cdd19SJun-ichiro itojun Hagino 	arg.rnh = rnh;
462686cdd19SJun-ichiro itojun Hagino 	arg.nextstop = 0;
463686cdd19SJun-ichiro itojun Hagino 	arg.draining = 1;
464686cdd19SJun-ichiro itojun Hagino 	arg.updating = 0;
465956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
466686cdd19SJun-ichiro itojun Hagino 	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
467956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
468686cdd19SJun-ichiro itojun Hagino }
469686cdd19SJun-ichiro itojun Hagino #endif
470686cdd19SJun-ichiro itojun Hagino 
47182cd038dSYoshinobu Inoue /*
47282cd038dSYoshinobu Inoue  * Initialize our routing tree.
4738b07e49aSJulian Elischer  * XXX MRT When off == 0, we are being called from vfs_export.c
4748b07e49aSJulian Elischer  * so just set up their table and leave. (we know what the correct
4758b07e49aSJulian Elischer  * value should be so just use that).. FIX AFTER RELENG_7 is MFC'd
4768b07e49aSJulian Elischer  * see also comments in in_inithead() vfs_export.c and domain.h
47782cd038dSYoshinobu Inoue  */
47882cd038dSYoshinobu Inoue int
47982cd038dSYoshinobu Inoue in6_inithead(void **head, int off)
48082cd038dSYoshinobu Inoue {
4818b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
48282cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
48382cd038dSYoshinobu Inoue 
4848b07e49aSJulian Elischer 	if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3))
4858b07e49aSJulian Elischer 		return 0;		/* See above */
48682cd038dSYoshinobu Inoue 
4878b07e49aSJulian Elischer 	if (off == 0)		/* See above */
4888b07e49aSJulian Elischer 		return 1;	/* only do the rest for the real thing */
48982cd038dSYoshinobu Inoue 
49044e33a07SMarko Zec 	V_rtq_reallyold6 = 60*60; /* one hour is ``really old'' */
49144e33a07SMarko Zec 	V_rtq_minreallyold6 = 10; /* never automatically crank down to less */
49244e33a07SMarko Zec 	V_rtq_toomany6 = 128;	  /* 128 cached routes is ``too many'' */
49344e33a07SMarko Zec 	V_rtq_timeout6 = RTQ_TIMEOUT;
49444e33a07SMarko Zec 
49582cd038dSYoshinobu Inoue 	rnh = *head;
49682cd038dSYoshinobu Inoue 	rnh->rnh_addaddr = in6_addroute;
49782cd038dSYoshinobu Inoue 	rnh->rnh_matchaddr = in6_matroute;
49882cd038dSYoshinobu Inoue 	rnh->rnh_close = in6_clsroute;
499603724d3SBjoern A. Zeeb 	callout_init(&V_rtq_timer6, CALLOUT_MPSAFE);
50082cd038dSYoshinobu Inoue 	in6_rtqtimo(rnh);	/* kick off timeout first time */
501603724d3SBjoern A. Zeeb 	callout_init(&V_rtq_mtutimer, CALLOUT_MPSAFE);
50282cd038dSYoshinobu Inoue 	in6_mtutimo(rnh);	/* kick off timeout first time */
50382cd038dSYoshinobu Inoue 	return 1;
50482cd038dSYoshinobu Inoue }
505