in6_rmx.c revision 286594
1139826Simp/*-
253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
353541Sshin * All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer.
1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1153541Sshin *    notice, this list of conditions and the following disclaimer in the
1253541Sshin *    documentation and/or other materials provided with the distribution.
1353541Sshin * 3. Neither the name of the project nor the names of its contributors
1453541Sshin *    may be used to endorse or promote products derived from this software
1553541Sshin *    without specific prior written permission.
1653541Sshin *
1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2753541Sshin * SUCH DAMAGE.
28174510Sobrien *
29174510Sobrien *	$KAME: in6_rmx.c,v 1.11 2001/07/26 06:53:16 jinmei Exp $
3053541Sshin */
3153541Sshin
32139826Simp/*-
3353541Sshin * Copyright 1994, 1995 Massachusetts Institute of Technology
3453541Sshin *
3553541Sshin * Permission to use, copy, modify, and distribute this software and
3653541Sshin * its documentation for any purpose and without fee is hereby
3753541Sshin * granted, provided that both the above copyright notice and this
3853541Sshin * permission notice appear in all copies, that both the above
3953541Sshin * copyright notice and this permission notice appear in all
4053541Sshin * supporting documentation, and that the name of M.I.T. not be used
4153541Sshin * in advertising or publicity pertaining to distribution of the
4253541Sshin * software without specific, written prior permission.  M.I.T. makes
4353541Sshin * no representations about the suitability of this software for any
4453541Sshin * purpose.  It is provided "as is" without express or implied
4553541Sshin * warranty.
4653541Sshin *
4753541Sshin * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
4853541Sshin * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
4953541Sshin * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
5053541Sshin * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
5153541Sshin * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
5253541Sshin * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
5353541Sshin * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
5453541Sshin * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5553541Sshin * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5653541Sshin * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
5753541Sshin * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5853541Sshin * SUCH DAMAGE.
5953541Sshin *
6053541Sshin */
6153541Sshin
62174510Sobrien#include <sys/cdefs.h>
63174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/in6_rmx.c 286594 2015-08-10 20:50:31Z melifaro $");
64174510Sobrien
6553541Sshin#include <sys/param.h>
6653541Sshin#include <sys/systm.h>
6753541Sshin#include <sys/kernel.h>
68185751Simp#include <sys/lock.h>
6953541Sshin#include <sys/queue.h>
7053541Sshin#include <sys/socket.h>
7153541Sshin#include <sys/socketvar.h>
7253541Sshin#include <sys/mbuf.h>
73185747Skmacy#include <sys/rwlock.h>
7453541Sshin#include <sys/syslog.h>
75120727Ssam#include <sys/callout.h>
7653541Sshin
7753541Sshin#include <net/if.h>
78257176Sglebius#include <net/if_var.h>
79194714Sbz#include <net/route.h>
80185571Sbz
8153541Sshin#include <netinet/in.h>
8253541Sshin#include <netinet/ip_var.h>
8353541Sshin#include <netinet/in_var.h>
8453541Sshin
8562587Sitojun#include <netinet/ip6.h>
8653541Sshin#include <netinet6/ip6_var.h>
8753541Sshin
8862587Sitojun#include <netinet/icmp6.h>
89121283Sume#include <netinet6/nd6.h>
9053541Sshin
9153541Sshin#include <netinet/tcp.h>
9253541Sshin#include <netinet/tcp_seq.h>
9353541Sshin#include <netinet/tcp_timer.h>
9453541Sshin#include <netinet/tcp_var.h>
9553541Sshin
96175162Sobrienextern int	in6_inithead(void **head, int off);
97193731Szec#ifdef VIMAGE
98193731Szecextern int	in6_detachhead(void **head, int off);
99193731Szec#endif
10053541Sshin
10153541Sshin/*
10253541Sshin * Do what we need to do when inserting a route.
10353541Sshin */
10453541Sshinstatic struct radix_node *
10553541Sshinin6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
106171260Sdelphij    struct radix_node *treenodes)
10753541Sshin{
10853541Sshin	struct rtentry *rt = (struct rtentry *)treenodes;
10953541Sshin	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
11053541Sshin	struct radix_node *ret;
11153541Sshin
112186119Sqingli	RADIX_NODE_HEAD_WLOCK_ASSERT(head);
11353541Sshin	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
11453541Sshin		rt->rt_flags |= RTF_MULTICAST;
11553541Sshin
11653541Sshin	/*
11753541Sshin	 * A little bit of help for both IPv6 output and input:
11853541Sshin	 *   For local addresses, we make sure that RTF_LOCAL is set,
11953541Sshin	 *   with the thought that this might one day be used to speed up
12053541Sshin	 *   ip_input().
12153541Sshin	 *
12253541Sshin	 * We also mark routes to multicast addresses as such, because
12353541Sshin	 * it's easy to do and might be useful (but this is much more
12453541Sshin	 * dubious since it's so easy to inspect the address).  (This
12553541Sshin	 * is done above.)
12653541Sshin	 *
12753541Sshin	 * XXX
12853541Sshin	 * should elaborate the code.
12953541Sshin	 */
13053541Sshin	if (rt->rt_flags & RTF_HOST) {
13153541Sshin		if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
13253541Sshin					->sin6_addr,
13353541Sshin				       &sin6->sin6_addr)) {
13453541Sshin			rt->rt_flags |= RTF_LOCAL;
13553541Sshin		}
13653541Sshin	}
13753541Sshin
138274175Smelifaro	if (rt->rt_ifp != NULL) {
13953541Sshin
140274175Smelifaro		/*
141274175Smelifaro		 * Check route MTU:
142274175Smelifaro		 * inherit interface MTU if not set or
143274175Smelifaro		 * check if MTU is too large.
144274175Smelifaro		 */
145274175Smelifaro		if (rt->rt_mtu == 0) {
146274175Smelifaro			rt->rt_mtu = IN6_LINKMTU(rt->rt_ifp);
147274175Smelifaro		} else if (rt->rt_mtu > IN6_LINKMTU(rt->rt_ifp))
148274175Smelifaro			rt->rt_mtu = IN6_LINKMTU(rt->rt_ifp);
149274175Smelifaro	}
150274175Smelifaro
15153541Sshin	ret = rn_addroute(v_arg, n_arg, head, treenodes);
152186119Sqingli	if (ret == NULL) {
15353541Sshin		struct rtentry *rt2;
15453541Sshin		/*
15553541Sshin		 * We are trying to add a net route, but can't.
15653541Sshin		 * The following case should be allowed, so we'll make a
15753541Sshin		 * special check for this:
15853541Sshin		 *	Two IPv6 addresses with the same prefix is assigned
15953541Sshin		 *	to a single interrface.
16053541Sshin		 *	# ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1)
16153541Sshin		 *	# ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2)
16253541Sshin		 *	In this case, (*1) and (*2) want to add the same
16353541Sshin		 *	net route entry, 3ffe:0501:: -> if0.
16453541Sshin		 *	This case should not raise an error.
16553541Sshin		 */
166231852Sbz		rt2 = in6_rtalloc1((struct sockaddr *)sin6, 0, RTF_RNH_LOCKED,
167231852Sbz		    rt->rt_fibnum);
16853541Sshin		if (rt2) {
169186119Sqingli			if (((rt2->rt_flags & (RTF_HOST|RTF_GATEWAY)) == 0)
17053541Sshin			 && rt2->rt_gateway
17153541Sshin			 && rt2->rt_gateway->sa_family == AF_LINK
17253541Sshin			 && rt2->rt_ifp == rt->rt_ifp) {
17353541Sshin				ret = rt2->rt_nodes;
17453541Sshin			}
175120727Ssam			RTFREE_LOCKED(rt2);
17653541Sshin		}
17753541Sshin	}
178186119Sqingli	return (ret);
17953541Sshin}
18053541Sshin
18153541Sshin/*
18253541Sshin * Age old PMTUs.
18353541Sshin */
18453541Sshinstruct mtuex_arg {
18553541Sshin	struct radix_node_head *rnh;
18653541Sshin	time_t nextstop;
18753541Sshin};
188215701Sdimstatic VNET_DEFINE(struct callout, rtq_mtutimer);
189195727Srwatson#define	V_rtq_mtutimer			VNET(rtq_mtutimer)
190195699Srwatson
19153541Sshinstatic int
192286458Smelifaroin6_mtuexpire(struct rtentry *rt, void *rock)
19353541Sshin{
19453541Sshin	struct mtuex_arg *ap = rock;
19553541Sshin
196262763Sglebius	if (rt->rt_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
197262763Sglebius		if (rt->rt_expire <= time_uptime) {
19853541Sshin			rt->rt_flags |= RTF_PROBEMTU;
19953541Sshin		} else {
200262763Sglebius			ap->nextstop = lmin(ap->nextstop, rt->rt_expire);
20153541Sshin		}
20253541Sshin	}
20353541Sshin
204286458Smelifaro	return (0);
20553541Sshin}
20653541Sshin
20753541Sshin#define	MTUTIMO_DEFAULT	(60*1)
20853541Sshin
20953541Sshinstatic void
210286594Smelifaroin6_mtutimo_setwa(struct radix_node_head *rnh, uint32_t fibum, int af,
211286594Smelifaro    void *_arg)
21253541Sshin{
213286458Smelifaro	struct mtuex_arg *arg;
21453541Sshin
215286458Smelifaro	arg = (struct mtuex_arg *)_arg;
216286458Smelifaro
217286458Smelifaro	arg->rnh = rnh;
218231852Sbz}
21953541Sshin
220231852Sbzstatic void
221231852Sbzin6_mtutimo(void *rock)
222231852Sbz{
223231852Sbz	CURVNET_SET_QUIET((struct vnet *) rock);
224231852Sbz	struct timeval atv;
225286458Smelifaro	struct mtuex_arg arg;
226231852Sbz
227286594Smelifaro	rt_foreach_fib_walk(AF_INET6, in6_mtutimo_setwa, in6_mtuexpire, &arg);
228231852Sbz
229231852Sbz	atv.tv_sec = MTUTIMO_DEFAULT;
23053541Sshin	atv.tv_usec = 0;
231181803Sbz	callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock);
232183550Szec	CURVNET_RESTORE();
23353541Sshin}
23453541Sshin
23553541Sshin/*
23653541Sshin * Initialize our routing tree.
23753541Sshin */
238231852Sbzstatic VNET_DEFINE(int, _in6_rt_was_here);
239231852Sbz#define	V__in6_rt_was_here	VNET(_in6_rt_was_here)
240231852Sbz
24153541Sshinint
24253541Sshinin6_inithead(void **head, int off)
24353541Sshin{
24453541Sshin	struct radix_node_head *rnh;
24553541Sshin
246178888Sjulian	if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3))
247274118Smelifaro		return (0);
24853541Sshin
249272361Smelifaro	rnh = *head;
250272361Smelifaro	RADIX_NODE_HEAD_LOCK_INIT(rnh);
251272361Smelifaro
25253541Sshin	rnh->rnh_addaddr = in6_addroute;
253231852Sbz
254231852Sbz	if (V__in6_rt_was_here == 0) {
255283291Sjkim		callout_init(&V_rtq_mtutimer, 1);
256231852Sbz		in6_mtutimo(curvnet);	/* kick off timeout first time */
257231852Sbz		V__in6_rt_was_here = 1;
258231852Sbz	}
259231852Sbz
260274118Smelifaro	return (1);
26153541Sshin}
262193731Szec
263193731Szec#ifdef VIMAGE
264193731Szecint
265193731Szecin6_detachhead(void **head, int off)
266193731Szec{
267193731Szec
268193731Szec	callout_drain(&V_rtq_mtutimer);
269193731Szec	return (1);
270193731Szec}
271193731Szec#endif
272231852Sbz
273231852Sbz/*
274231852Sbz * Extended API for IPv6 FIB support.
275231852Sbz */
276231852Sbzvoid
277231852Sbzin6_rtredirect(struct sockaddr *dst, struct sockaddr *gw, struct sockaddr *nm,
278231852Sbz    int flags, struct sockaddr *src, u_int fibnum)
279231852Sbz{
280231852Sbz
281231852Sbz	rtredirect_fib(dst, gw, nm, flags, src, fibnum);
282231852Sbz}
283231852Sbz
284231852Sbzint
285231852Sbzin6_rtrequest(int req, struct sockaddr *dst, struct sockaddr *gw,
286231852Sbz    struct sockaddr *mask, int flags, struct rtentry **ret_nrt, u_int fibnum)
287231852Sbz{
288231852Sbz
289231852Sbz	return (rtrequest_fib(req, dst, gw, mask, flags, ret_nrt, fibnum));
290231852Sbz}
291231852Sbz
292231852Sbzvoid
293231852Sbzin6_rtalloc(struct route_in6 *ro, u_int fibnum)
294231852Sbz{
295231852Sbz
296231852Sbz	rtalloc_ign_fib((struct route *)ro, 0ul, fibnum);
297231852Sbz}
298231852Sbz
299231852Sbzvoid
300231852Sbzin6_rtalloc_ign(struct route_in6 *ro, u_long ignflags, u_int fibnum)
301231852Sbz{
302231852Sbz
303231852Sbz	rtalloc_ign_fib((struct route *)ro, ignflags, fibnum);
304231852Sbz}
305231852Sbz
306231852Sbzstruct rtentry *
307231852Sbzin6_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum)
308231852Sbz{
309231852Sbz
310231852Sbz	return (rtalloc1_fib(dst, report, ignflags, fibnum));
311231852Sbz}
312