xref: /freebsd/sys/netinet6/in6_rmx.c (revision 3120b9d4285d2c477a8d7a188597fc7613a66d47)
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>
8182cd038dSYoshinobu Inoue #include <sys/sysctl.h>
8282cd038dSYoshinobu Inoue #include <sys/queue.h>
8382cd038dSYoshinobu Inoue #include <sys/socket.h>
8482cd038dSYoshinobu Inoue #include <sys/socketvar.h>
8582cd038dSYoshinobu Inoue #include <sys/mbuf.h>
863120b9d4SKip Macy #include <sys/rwlock.h>
8782cd038dSYoshinobu Inoue #include <sys/syslog.h>
88d1dd20beSSam Leffler #include <sys/callout.h>
89603724d3SBjoern A. Zeeb #include <sys/vimage.h>
9082cd038dSYoshinobu Inoue 
9182cd038dSYoshinobu Inoue #include <net/if.h>
9282cd038dSYoshinobu Inoue #include <net/route.h>
934b79449eSBjoern A. Zeeb #include <net/vnet.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);
11282cd038dSYoshinobu Inoue 
11382cd038dSYoshinobu Inoue #define RTPRF_OURS		RTF_PROTO3	/* set on routes we manage */
11482cd038dSYoshinobu Inoue 
11582cd038dSYoshinobu Inoue /*
11682cd038dSYoshinobu Inoue  * Do what we need to do when inserting a route.
11782cd038dSYoshinobu Inoue  */
11882cd038dSYoshinobu Inoue static struct radix_node *
11982cd038dSYoshinobu Inoue in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
12082cd038dSYoshinobu Inoue     struct radix_node *treenodes)
12182cd038dSYoshinobu Inoue {
12282cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)treenodes;
12382cd038dSYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
12482cd038dSYoshinobu Inoue 	struct radix_node *ret;
12582cd038dSYoshinobu Inoue 
12682cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
12782cd038dSYoshinobu Inoue 		rt->rt_flags |= RTF_MULTICAST;
12882cd038dSYoshinobu Inoue 
12982cd038dSYoshinobu Inoue 	/*
13082cd038dSYoshinobu Inoue 	 * A little bit of help for both IPv6 output and input:
13182cd038dSYoshinobu Inoue 	 *   For local addresses, we make sure that RTF_LOCAL is set,
13282cd038dSYoshinobu Inoue 	 *   with the thought that this might one day be used to speed up
13382cd038dSYoshinobu Inoue 	 *   ip_input().
13482cd038dSYoshinobu Inoue 	 *
13582cd038dSYoshinobu Inoue 	 * We also mark routes to multicast addresses as such, because
13682cd038dSYoshinobu Inoue 	 * it's easy to do and might be useful (but this is much more
13782cd038dSYoshinobu Inoue 	 * dubious since it's so easy to inspect the address).  (This
13882cd038dSYoshinobu Inoue 	 * is done above.)
13982cd038dSYoshinobu Inoue 	 *
14082cd038dSYoshinobu Inoue 	 * XXX
14182cd038dSYoshinobu Inoue 	 * should elaborate the code.
14282cd038dSYoshinobu Inoue 	 */
14382cd038dSYoshinobu Inoue 	if (rt->rt_flags & RTF_HOST) {
14482cd038dSYoshinobu Inoue 		if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
14582cd038dSYoshinobu Inoue 					->sin6_addr,
14682cd038dSYoshinobu Inoue 				       &sin6->sin6_addr)) {
14782cd038dSYoshinobu Inoue 			rt->rt_flags |= RTF_LOCAL;
14882cd038dSYoshinobu Inoue 		}
14982cd038dSYoshinobu Inoue 	}
15082cd038dSYoshinobu Inoue 
15197d8d152SAndre Oppermann 	if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp)
15231b3783cSHajimu UMEMOTO 		rt->rt_rmx.rmx_mtu = IN6_LINKMTU(rt->rt_ifp);
15382cd038dSYoshinobu Inoue 
15482cd038dSYoshinobu Inoue 	ret = rn_addroute(v_arg, n_arg, head, treenodes);
15582cd038dSYoshinobu Inoue 	if (ret == NULL && rt->rt_flags & RTF_HOST) {
15682cd038dSYoshinobu Inoue 		struct rtentry *rt2;
15782cd038dSYoshinobu Inoue 		/*
15882cd038dSYoshinobu Inoue 		 * We are trying to add a host route, but can't.
15982cd038dSYoshinobu Inoue 		 * Find out if it is because of an
16082cd038dSYoshinobu Inoue 		 * ARP entry and delete it if so.
16182cd038dSYoshinobu Inoue 		 */
16226d02ca7SAndre Oppermann 		rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_CLONING);
16382cd038dSYoshinobu Inoue 		if (rt2) {
16482cd038dSYoshinobu Inoue 			if (rt2->rt_flags & RTF_LLINFO &&
16582cd038dSYoshinobu Inoue 				rt2->rt_flags & RTF_HOST &&
16682cd038dSYoshinobu Inoue 				rt2->rt_gateway &&
16782cd038dSYoshinobu Inoue 				rt2->rt_gateway->sa_family == AF_LINK) {
1689c63e9dbSSam Leffler 				rtexpunge(rt2);
1699c63e9dbSSam Leffler 				RTFREE_LOCKED(rt2);
17082cd038dSYoshinobu Inoue 				ret = rn_addroute(v_arg, n_arg, head,
17182cd038dSYoshinobu Inoue 					treenodes);
1729c63e9dbSSam Leffler 			} else
173d1dd20beSSam Leffler 				RTFREE_LOCKED(rt2);
17482cd038dSYoshinobu Inoue 		}
17582cd038dSYoshinobu Inoue 	} else if (ret == NULL && rt->rt_flags & RTF_CLONING) {
17682cd038dSYoshinobu Inoue 		struct rtentry *rt2;
17782cd038dSYoshinobu Inoue 		/*
17882cd038dSYoshinobu Inoue 		 * We are trying to add a net route, but can't.
17982cd038dSYoshinobu Inoue 		 * The following case should be allowed, so we'll make a
18082cd038dSYoshinobu Inoue 		 * special check for this:
18182cd038dSYoshinobu Inoue 		 *	Two IPv6 addresses with the same prefix is assigned
18282cd038dSYoshinobu Inoue 		 *	to a single interrface.
18382cd038dSYoshinobu Inoue 		 *	# ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
18482cd038dSYoshinobu Inoue 		 *	# ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
18582cd038dSYoshinobu Inoue 		 *	In this case, (*1) and (*2) want to add the same
18682cd038dSYoshinobu Inoue 		 *	net route entry, 3ffe:0501:: -> if0.
18782cd038dSYoshinobu Inoue 		 *	This case should not raise an error.
18882cd038dSYoshinobu Inoue 		 */
18926d02ca7SAndre Oppermann 		rt2 = rtalloc1((struct sockaddr *)sin6, 0, RTF_CLONING);
19082cd038dSYoshinobu Inoue 		if (rt2) {
19182cd038dSYoshinobu Inoue 			if ((rt2->rt_flags & (RTF_CLONING|RTF_HOST|RTF_GATEWAY))
19282cd038dSYoshinobu Inoue 					== RTF_CLONING
19382cd038dSYoshinobu Inoue 			 && rt2->rt_gateway
19482cd038dSYoshinobu Inoue 			 && rt2->rt_gateway->sa_family == AF_LINK
19582cd038dSYoshinobu Inoue 			 && rt2->rt_ifp == rt->rt_ifp) {
19682cd038dSYoshinobu Inoue 				ret = rt2->rt_nodes;
19782cd038dSYoshinobu Inoue 			}
198d1dd20beSSam Leffler 			RTFREE_LOCKED(rt2);
19982cd038dSYoshinobu Inoue 		}
20082cd038dSYoshinobu Inoue 	}
20182cd038dSYoshinobu Inoue 	return ret;
20282cd038dSYoshinobu Inoue }
20382cd038dSYoshinobu Inoue 
20482cd038dSYoshinobu Inoue /*
20582cd038dSYoshinobu Inoue  * This code is the inverse of in6_clsroute: on first reference, if we
20682cd038dSYoshinobu Inoue  * were managing the route, stop doing so and set the expiration timer
20782cd038dSYoshinobu Inoue  * back off again.
20882cd038dSYoshinobu Inoue  */
20982cd038dSYoshinobu Inoue static struct radix_node *
21082cd038dSYoshinobu Inoue in6_matroute(void *v_arg, struct radix_node_head *head)
21182cd038dSYoshinobu Inoue {
21282cd038dSYoshinobu Inoue 	struct radix_node *rn = rn_match(v_arg, head);
21382cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
21482cd038dSYoshinobu Inoue 
21582cd038dSYoshinobu Inoue 	if (rt && rt->rt_refcnt == 0) { /* this is first reference */
21682cd038dSYoshinobu Inoue 		if (rt->rt_flags & RTPRF_OURS) {
21782cd038dSYoshinobu Inoue 			rt->rt_flags &= ~RTPRF_OURS;
21882cd038dSYoshinobu Inoue 			rt->rt_rmx.rmx_expire = 0;
21982cd038dSYoshinobu Inoue 		}
22082cd038dSYoshinobu Inoue 	}
22182cd038dSYoshinobu Inoue 	return rn;
22282cd038dSYoshinobu Inoue }
22382cd038dSYoshinobu Inoue 
2247ee982bcSJun-ichiro itojun Hagino SYSCTL_DECL(_net_inet6_ip6);
2257ee982bcSJun-ichiro itojun Hagino 
22644e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
22744e33a07SMarko Zec static int rtq_reallyold6;
22844e33a07SMarko Zec static int rtq_minreallyold6;
22944e33a07SMarko Zec static int rtq_toomany6;
23044e33a07SMarko Zec #endif
23144e33a07SMarko Zec 
23297021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTEXPIRE,
23397021c24SMarko Zec     rtexpire, CTLFLAG_RW, rtq_reallyold6 , 0, "");
23482cd038dSYoshinobu Inoue 
23597021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTMINEXPIRE,
23697021c24SMarko Zec     rtminexpire, CTLFLAG_RW, rtq_minreallyold6 , 0, "");
23782cd038dSYoshinobu Inoue 
23897021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_ip6, IPV6CTL_RTMAXCACHE,
23997021c24SMarko Zec     rtmaxcache, CTLFLAG_RW, rtq_toomany6 , 0, "");
24082cd038dSYoshinobu Inoue 
24182cd038dSYoshinobu Inoue 
24282cd038dSYoshinobu Inoue /*
24382cd038dSYoshinobu Inoue  * On last reference drop, mark the route as belong to us so that it can be
24482cd038dSYoshinobu Inoue  * timed out.
24582cd038dSYoshinobu Inoue  */
24682cd038dSYoshinobu Inoue static void
24782cd038dSYoshinobu Inoue in6_clsroute(struct radix_node *rn, struct radix_node_head *head)
24882cd038dSYoshinobu Inoue {
2498b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
25082cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
25182cd038dSYoshinobu Inoue 
252d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
253d1dd20beSSam Leffler 
25482cd038dSYoshinobu Inoue 	if (!(rt->rt_flags & RTF_UP))
25582cd038dSYoshinobu Inoue 		return;		/* prophylactic measures */
25682cd038dSYoshinobu Inoue 
25782cd038dSYoshinobu Inoue 	if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
25882cd038dSYoshinobu Inoue 		return;
25982cd038dSYoshinobu Inoue 
260956b0b65SJeffrey Hsu 	if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
26182cd038dSYoshinobu Inoue 		return;
26282cd038dSYoshinobu Inoue 
26382cd038dSYoshinobu Inoue 	/*
26482cd038dSYoshinobu Inoue 	 * As requested by David Greenman:
265f1250445SBjoern A. Zeeb 	 * If rtq_reallyold6 is 0, just delete the route without
26682cd038dSYoshinobu Inoue 	 * waiting for a timeout cycle to kill it.
26782cd038dSYoshinobu Inoue 	 */
268f1250445SBjoern A. Zeeb 	if (V_rtq_reallyold6 != 0) {
26982cd038dSYoshinobu Inoue 		rt->rt_flags |= RTPRF_OURS;
270f1250445SBjoern A. Zeeb 		rt->rt_rmx.rmx_expire = time_uptime + V_rtq_reallyold6;
27182cd038dSYoshinobu Inoue 	} else {
2729c63e9dbSSam Leffler 		rtexpunge(rt);
27382cd038dSYoshinobu Inoue 	}
27482cd038dSYoshinobu Inoue }
27582cd038dSYoshinobu Inoue 
27682cd038dSYoshinobu Inoue struct rtqk_arg {
27782cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
27882cd038dSYoshinobu Inoue 	int mode;
27982cd038dSYoshinobu Inoue 	int updating;
28082cd038dSYoshinobu Inoue 	int draining;
28182cd038dSYoshinobu Inoue 	int killed;
28282cd038dSYoshinobu Inoue 	int found;
28382cd038dSYoshinobu Inoue 	time_t nextstop;
28482cd038dSYoshinobu Inoue };
28582cd038dSYoshinobu Inoue 
28682cd038dSYoshinobu Inoue /*
28782cd038dSYoshinobu Inoue  * Get rid of old routes.  When draining, this deletes everything, even when
28882cd038dSYoshinobu Inoue  * the timeout is not expired yet.  When updating, this makes sure that
289f1250445SBjoern A. Zeeb  * nothing has a timeout longer than the current value of rtq_reallyold6.
29082cd038dSYoshinobu Inoue  */
29182cd038dSYoshinobu Inoue static int
29282cd038dSYoshinobu Inoue in6_rtqkill(struct radix_node *rn, void *rock)
29382cd038dSYoshinobu Inoue {
2948b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
29582cd038dSYoshinobu Inoue 	struct rtqk_arg *ap = rock;
29682cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
29782cd038dSYoshinobu Inoue 	int err;
29882cd038dSYoshinobu Inoue 
29982cd038dSYoshinobu Inoue 	if (rt->rt_flags & RTPRF_OURS) {
30082cd038dSYoshinobu Inoue 		ap->found++;
30182cd038dSYoshinobu Inoue 
302fe53256dSAndre Oppermann 		if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) {
30382cd038dSYoshinobu Inoue 			if (rt->rt_refcnt > 0)
30482cd038dSYoshinobu Inoue 				panic("rtqkill route really not free");
30582cd038dSYoshinobu Inoue 
30682cd038dSYoshinobu Inoue 			err = rtrequest(RTM_DELETE,
30782cd038dSYoshinobu Inoue 					(struct sockaddr *)rt_key(rt),
30882cd038dSYoshinobu Inoue 					rt->rt_gateway, rt_mask(rt),
30982cd038dSYoshinobu Inoue 					rt->rt_flags, 0);
31082cd038dSYoshinobu Inoue 			if (err) {
31182cd038dSYoshinobu Inoue 				log(LOG_WARNING, "in6_rtqkill: error %d", err);
31282cd038dSYoshinobu Inoue 			} else {
31382cd038dSYoshinobu Inoue 				ap->killed++;
31482cd038dSYoshinobu Inoue 			}
31582cd038dSYoshinobu Inoue 		} else {
31682cd038dSYoshinobu Inoue 			if (ap->updating
317fe53256dSAndre Oppermann 			   && (rt->rt_rmx.rmx_expire - time_uptime
318f1250445SBjoern A. Zeeb 			       > V_rtq_reallyold6)) {
319fe53256dSAndre Oppermann 				rt->rt_rmx.rmx_expire = time_uptime
320f1250445SBjoern A. Zeeb 					+ V_rtq_reallyold6;
32182cd038dSYoshinobu Inoue 			}
32282cd038dSYoshinobu Inoue 			ap->nextstop = lmin(ap->nextstop,
32382cd038dSYoshinobu Inoue 					    rt->rt_rmx.rmx_expire);
32482cd038dSYoshinobu Inoue 		}
32582cd038dSYoshinobu Inoue 	}
32682cd038dSYoshinobu Inoue 
32782cd038dSYoshinobu Inoue 	return 0;
32882cd038dSYoshinobu Inoue }
32982cd038dSYoshinobu Inoue 
33082cd038dSYoshinobu Inoue #define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
33144e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
33244e33a07SMarko Zec static int rtq_timeout6;
3332ce7b410SAlexander Kabaev static struct callout rtq_timer6;
33444e33a07SMarko Zec #endif
33582cd038dSYoshinobu Inoue 
33682cd038dSYoshinobu Inoue static void
33782cd038dSYoshinobu Inoue in6_rtqtimo(void *rock)
33882cd038dSYoshinobu Inoue {
3398b615593SMarko Zec 	CURVNET_SET_QUIET((struct vnet *) rock);
3408b615593SMarko Zec 	INIT_VNET_NET((struct vnet *) rock);
3418b615593SMarko Zec 	INIT_VNET_INET6((struct vnet *) rock);
34282cd038dSYoshinobu Inoue 	struct radix_node_head *rnh = rock;
34382cd038dSYoshinobu Inoue 	struct rtqk_arg arg;
34482cd038dSYoshinobu Inoue 	struct timeval atv;
34582cd038dSYoshinobu Inoue 	static time_t last_adjusted_timeout = 0;
34682cd038dSYoshinobu Inoue 
34782cd038dSYoshinobu Inoue 	arg.found = arg.killed = 0;
34882cd038dSYoshinobu Inoue 	arg.rnh = rnh;
349603724d3SBjoern A. Zeeb 	arg.nextstop = time_uptime + V_rtq_timeout6;
35082cd038dSYoshinobu Inoue 	arg.draining = arg.updating = 0;
351956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
35282cd038dSYoshinobu Inoue 	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
353956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
35482cd038dSYoshinobu Inoue 
35582cd038dSYoshinobu Inoue 	/*
35682cd038dSYoshinobu Inoue 	 * Attempt to be somewhat dynamic about this:
35782cd038dSYoshinobu Inoue 	 * If there are ``too many'' routes sitting around taking up space,
35882cd038dSYoshinobu Inoue 	 * then crank down the timeout, and see if we can't make some more
35982cd038dSYoshinobu Inoue 	 * go away.  However, we make sure that we will never adjust more
3605f9a5768SJulian Elischer 	 * than once in rtq_timeout6 seconds, to keep from cranking down too
36182cd038dSYoshinobu Inoue 	 * hard.
36282cd038dSYoshinobu Inoue 	 */
363f1250445SBjoern A. Zeeb 	if ((arg.found - arg.killed > V_rtq_toomany6)
364603724d3SBjoern A. Zeeb 	   && (time_uptime - last_adjusted_timeout >= V_rtq_timeout6)
365f1250445SBjoern A. Zeeb 	   && V_rtq_reallyold6 > V_rtq_minreallyold6) {
366f1250445SBjoern A. Zeeb 		V_rtq_reallyold6 = 2*V_rtq_reallyold6 / 3;
367f1250445SBjoern A. Zeeb 		if (V_rtq_reallyold6 < V_rtq_minreallyold6) {
368f1250445SBjoern A. Zeeb 			V_rtq_reallyold6 = V_rtq_minreallyold6;
36982cd038dSYoshinobu Inoue 		}
37082cd038dSYoshinobu Inoue 
37163721457SOleg Bulyzhin 		last_adjusted_timeout = time_uptime;
37282cd038dSYoshinobu Inoue #ifdef DIAGNOSTIC
373f1250445SBjoern A. Zeeb 		log(LOG_DEBUG, "in6_rtqtimo: adjusted rtq_reallyold6 to %d",
374f1250445SBjoern A. Zeeb 		    V_rtq_reallyold6);
37582cd038dSYoshinobu Inoue #endif
37682cd038dSYoshinobu Inoue 		arg.found = arg.killed = 0;
37782cd038dSYoshinobu Inoue 		arg.updating = 1;
378956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK(rnh);
37982cd038dSYoshinobu Inoue 		rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
380956b0b65SJeffrey Hsu 		RADIX_NODE_HEAD_UNLOCK(rnh);
38182cd038dSYoshinobu Inoue 	}
38282cd038dSYoshinobu Inoue 
38382cd038dSYoshinobu Inoue 	atv.tv_usec = 0;
38463721457SOleg Bulyzhin 	atv.tv_sec = arg.nextstop - time_uptime;
385603724d3SBjoern A. Zeeb 	callout_reset(&V_rtq_timer6, tvtohz(&atv), in6_rtqtimo, rock);
3868b615593SMarko Zec 	CURVNET_RESTORE();
38782cd038dSYoshinobu Inoue }
38882cd038dSYoshinobu Inoue 
38982cd038dSYoshinobu Inoue /*
39082cd038dSYoshinobu Inoue  * Age old PMTUs.
39182cd038dSYoshinobu Inoue  */
39282cd038dSYoshinobu Inoue struct mtuex_arg {
39382cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
39482cd038dSYoshinobu Inoue 	time_t nextstop;
39582cd038dSYoshinobu Inoue };
39644e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
397d1dd20beSSam Leffler static struct callout rtq_mtutimer;
39844e33a07SMarko Zec #endif
39982cd038dSYoshinobu Inoue 
40082cd038dSYoshinobu Inoue static int
40182cd038dSYoshinobu Inoue in6_mtuexpire(struct radix_node *rn, void *rock)
40282cd038dSYoshinobu Inoue {
40382cd038dSYoshinobu Inoue 	struct rtentry *rt = (struct rtentry *)rn;
40482cd038dSYoshinobu Inoue 	struct mtuex_arg *ap = rock;
40582cd038dSYoshinobu Inoue 
40682cd038dSYoshinobu Inoue 	/* sanity */
40782cd038dSYoshinobu Inoue 	if (!rt)
40882cd038dSYoshinobu Inoue 		panic("rt == NULL in in6_mtuexpire");
40982cd038dSYoshinobu Inoue 
41082cd038dSYoshinobu Inoue 	if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
411fe53256dSAndre Oppermann 		if (rt->rt_rmx.rmx_expire <= time_uptime) {
41282cd038dSYoshinobu Inoue 			rt->rt_flags |= RTF_PROBEMTU;
41382cd038dSYoshinobu Inoue 		} else {
41482cd038dSYoshinobu Inoue 			ap->nextstop = lmin(ap->nextstop,
41582cd038dSYoshinobu Inoue 					rt->rt_rmx.rmx_expire);
41682cd038dSYoshinobu Inoue 		}
41782cd038dSYoshinobu Inoue 	}
41882cd038dSYoshinobu Inoue 
41982cd038dSYoshinobu Inoue 	return 0;
42082cd038dSYoshinobu Inoue }
42182cd038dSYoshinobu Inoue 
42282cd038dSYoshinobu Inoue #define	MTUTIMO_DEFAULT	(60*1)
42382cd038dSYoshinobu Inoue 
42482cd038dSYoshinobu Inoue static void
42582cd038dSYoshinobu Inoue in6_mtutimo(void *rock)
42682cd038dSYoshinobu Inoue {
4278b615593SMarko Zec 	CURVNET_SET_QUIET((struct vnet *) rock);
4288b615593SMarko Zec 	INIT_VNET_NET((struct vnet *) rock);
4298b615593SMarko Zec 	INIT_VNET_INET6((struct vnet *) rock);
43082cd038dSYoshinobu Inoue 	struct radix_node_head *rnh = rock;
43182cd038dSYoshinobu Inoue 	struct mtuex_arg arg;
43282cd038dSYoshinobu Inoue 	struct timeval atv;
43382cd038dSYoshinobu Inoue 
43482cd038dSYoshinobu Inoue 	arg.rnh = rnh;
43563721457SOleg Bulyzhin 	arg.nextstop = time_uptime + MTUTIMO_DEFAULT;
436956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
43782cd038dSYoshinobu Inoue 	rnh->rnh_walktree(rnh, in6_mtuexpire, &arg);
438956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
43982cd038dSYoshinobu Inoue 
44082cd038dSYoshinobu Inoue 	atv.tv_usec = 0;
44163721457SOleg Bulyzhin 	atv.tv_sec = arg.nextstop - time_uptime;
4426f9e3ebfSSUZUKI Shinsuke 	if (atv.tv_sec < 0) {
44382cd038dSYoshinobu Inoue 		printf("invalid mtu expiration time on routing table\n");
44463721457SOleg Bulyzhin 		arg.nextstop = time_uptime + 30;	/* last resort */
44563721457SOleg Bulyzhin 		atv.tv_sec = 30;
44682cd038dSYoshinobu Inoue 	}
447603724d3SBjoern A. Zeeb 	callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock);
4488b615593SMarko Zec 	CURVNET_RESTORE();
44982cd038dSYoshinobu Inoue }
45082cd038dSYoshinobu Inoue 
451686cdd19SJun-ichiro itojun Hagino #if 0
452686cdd19SJun-ichiro itojun Hagino void
4531272577eSXin LI in6_rtqdrain(void)
454686cdd19SJun-ichiro itojun Hagino {
4558b615593SMarko Zec 	INIT_VNET_NET(curvnet);
456603724d3SBjoern A. Zeeb 	struct radix_node_head *rnh = V_rt_tables[AF_INET6];
457686cdd19SJun-ichiro itojun Hagino 	struct rtqk_arg arg;
458d1dd20beSSam Leffler 
459686cdd19SJun-ichiro itojun Hagino 	arg.found = arg.killed = 0;
460686cdd19SJun-ichiro itojun Hagino 	arg.rnh = rnh;
461686cdd19SJun-ichiro itojun Hagino 	arg.nextstop = 0;
462686cdd19SJun-ichiro itojun Hagino 	arg.draining = 1;
463686cdd19SJun-ichiro itojun Hagino 	arg.updating = 0;
464956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
465686cdd19SJun-ichiro itojun Hagino 	rnh->rnh_walktree(rnh, in6_rtqkill, &arg);
466956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
467686cdd19SJun-ichiro itojun Hagino }
468686cdd19SJun-ichiro itojun Hagino #endif
469686cdd19SJun-ichiro itojun Hagino 
47082cd038dSYoshinobu Inoue /*
47182cd038dSYoshinobu Inoue  * Initialize our routing tree.
4728b07e49aSJulian Elischer  * XXX MRT When off == 0, we are being called from vfs_export.c
4738b07e49aSJulian Elischer  * so just set up their table and leave. (we know what the correct
4748b07e49aSJulian Elischer  * value should be so just use that).. FIX AFTER RELENG_7 is MFC'd
4758b07e49aSJulian Elischer  * see also comments in in_inithead() vfs_export.c and domain.h
47682cd038dSYoshinobu Inoue  */
47782cd038dSYoshinobu Inoue int
47882cd038dSYoshinobu Inoue in6_inithead(void **head, int off)
47982cd038dSYoshinobu Inoue {
4808b615593SMarko Zec 	INIT_VNET_INET6(curvnet);
48182cd038dSYoshinobu Inoue 	struct radix_node_head *rnh;
48282cd038dSYoshinobu Inoue 
4838b07e49aSJulian Elischer 	if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3))
4848b07e49aSJulian Elischer 		return 0;		/* See above */
48582cd038dSYoshinobu Inoue 
4868b07e49aSJulian Elischer 	if (off == 0)		/* See above */
4878b07e49aSJulian Elischer 		return 1;	/* only do the rest for the real thing */
48882cd038dSYoshinobu Inoue 
48944e33a07SMarko Zec 	V_rtq_reallyold6 = 60*60; /* one hour is ``really old'' */
49044e33a07SMarko Zec 	V_rtq_minreallyold6 = 10; /* never automatically crank down to less */
49144e33a07SMarko Zec 	V_rtq_toomany6 = 128;	  /* 128 cached routes is ``too many'' */
49244e33a07SMarko Zec 	V_rtq_timeout6 = RTQ_TIMEOUT;
49344e33a07SMarko Zec 
49482cd038dSYoshinobu Inoue 	rnh = *head;
49582cd038dSYoshinobu Inoue 	rnh->rnh_addaddr = in6_addroute;
49682cd038dSYoshinobu Inoue 	rnh->rnh_matchaddr = in6_matroute;
49782cd038dSYoshinobu Inoue 	rnh->rnh_close = in6_clsroute;
498603724d3SBjoern A. Zeeb 	callout_init(&V_rtq_timer6, CALLOUT_MPSAFE);
49982cd038dSYoshinobu Inoue 	in6_rtqtimo(rnh);	/* kick off timeout first time */
500603724d3SBjoern A. Zeeb 	callout_init(&V_rtq_mtutimer, CALLOUT_MPSAFE);
50182cd038dSYoshinobu Inoue 	in6_mtutimo(rnh);	/* kick off timeout first time */
50282cd038dSYoshinobu Inoue 	return 1;
50382cd038dSYoshinobu Inoue }
504