xref: /freebsd/sys/netinet6/ip6_input.c (revision 40faf87894ff67ffdf8126fce9bb438ddf61a26f)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
482cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
582cd038dSYoshinobu Inoue  * All rights reserved.
682cd038dSYoshinobu Inoue  *
782cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
882cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
982cd038dSYoshinobu Inoue  * are met:
1082cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1282cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1382cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1482cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1582cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1682cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1782cd038dSYoshinobu Inoue  *    without specific prior written permission.
1882cd038dSYoshinobu Inoue  *
1982cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2082cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2182cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2282cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2382cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2482cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2582cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2682cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2782cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2882cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2982cd038dSYoshinobu Inoue  * SUCH DAMAGE.
30b48287a3SDavid E. O'Brien  *
31b48287a3SDavid E. O'Brien  *	$KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $
3282cd038dSYoshinobu Inoue  */
3382cd038dSYoshinobu Inoue 
34caf43b02SWarner Losh /*-
3582cd038dSYoshinobu Inoue  * Copyright (c) 1982, 1986, 1988, 1993
3682cd038dSYoshinobu Inoue  *	The Regents of the University of California.  All rights reserved.
3782cd038dSYoshinobu Inoue  *
3882cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
3982cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
4082cd038dSYoshinobu Inoue  * are met:
4182cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
4282cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
4382cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
4482cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
4582cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
46fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
4782cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
4882cd038dSYoshinobu Inoue  *    without specific prior written permission.
4982cd038dSYoshinobu Inoue  *
5082cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5182cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5282cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5382cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5482cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5582cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5682cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5782cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5882cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5982cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6082cd038dSYoshinobu Inoue  * SUCH DAMAGE.
6182cd038dSYoshinobu Inoue  */
6282cd038dSYoshinobu Inoue 
63b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
64686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
65686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
66686cdd19SJun-ichiro itojun Hagino #include "opt_ipsec.h"
6777931dd5SKip Macy #include "opt_route.h"
680be18915SAdrian Chadd #include "opt_rss.h"
6978b1fc05SGleb Smirnoff #include "opt_sctp.h"
706a800098SYoshinobu Inoue 
7182cd038dSYoshinobu Inoue #include <sys/param.h>
7282cd038dSYoshinobu Inoue #include <sys/systm.h>
73ef91a976SAndrey V. Elsukov #include <sys/hhook.h>
7433841545SHajimu UMEMOTO #include <sys/malloc.h>
7582cd038dSYoshinobu Inoue #include <sys/mbuf.h>
76b40ce416SJulian Elischer #include <sys/proc.h>
7782cd038dSYoshinobu Inoue #include <sys/domain.h>
7882cd038dSYoshinobu Inoue #include <sys/protosw.h>
7957f60867SMark Johnston #include <sys/sdt.h>
8082cd038dSYoshinobu Inoue #include <sys/socket.h>
8182cd038dSYoshinobu Inoue #include <sys/socketvar.h>
8282cd038dSYoshinobu Inoue #include <sys/errno.h>
8382cd038dSYoshinobu Inoue #include <sys/time.h>
8482cd038dSYoshinobu Inoue #include <sys/kernel.h>
85cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
86cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
8782cd038dSYoshinobu Inoue #include <sys/syslog.h>
887aeccebcSAndrey V. Elsukov #include <sys/sysctl.h>
896080e073SGleb Smirnoff #include <sys/eventhandler.h>
9082cd038dSYoshinobu Inoue 
9182cd038dSYoshinobu Inoue #include <net/if.h>
9276039bc8SGleb Smirnoff #include <net/if_var.h>
9382cd038dSYoshinobu Inoue #include <net/if_types.h>
94bb55bb17SJustin Hibbits #include <net/if_private.h>
9582cd038dSYoshinobu Inoue #include <net/if_dl.h>
9682cd038dSYoshinobu Inoue #include <net/route.h>
9782cd038dSYoshinobu Inoue #include <net/netisr.h>
9868bb8d62SAdrian Chadd #include <net/rss_config.h>
99c4ac87eaSDarren Reed #include <net/pfil.h>
1004b79449eSBjoern A. Zeeb #include <net/vnet.h>
10182cd038dSYoshinobu Inoue 
10282cd038dSYoshinobu Inoue #include <netinet/in.h>
10357f60867SMark Johnston #include <netinet/in_kdtrace.h>
1048a006adbSBjoern A. Zeeb #include <netinet/ip_var.h>
10582cd038dSYoshinobu Inoue #include <netinet/in_systm.h>
1066e6b3f7cSQing Li #include <net/if_llatbl.h>
107686cdd19SJun-ichiro itojun Hagino #ifdef INET
10882cd038dSYoshinobu Inoue #include <netinet/ip.h>
10982cd038dSYoshinobu Inoue #include <netinet/ip_icmp.h>
110686cdd19SJun-ichiro itojun Hagino #endif /* INET */
111686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
11282cd038dSYoshinobu Inoue #include <netinet6/in6_var.h>
11382cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
11478b1fc05SGleb Smirnoff #include <netinet/ip_encap.h>
115686cdd19SJun-ichiro itojun Hagino #include <netinet/in_pcb.h>
116686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
11731b1bfe1SHajimu UMEMOTO #include <netinet6/scope6_var.h>
11882cd038dSYoshinobu Inoue #include <netinet6/in6_ifattach.h>
11989856f7eSBjoern A. Zeeb #include <netinet6/mld6_var.h>
12082cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
1210be18915SAdrian Chadd #include <netinet6/in6_rss.h>
12278b1fc05SGleb Smirnoff #ifdef SCTP
12378b1fc05SGleb Smirnoff #include <netinet/sctp_pcb.h>
12478b1fc05SGleb Smirnoff #include <netinet6/sctp6_var.h>
12578b1fc05SGleb Smirnoff #endif
12682cd038dSYoshinobu Inoue 
127fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
128b9234fafSSam Leffler 
12924b96f35SGleb Smirnoff ip6proto_input_t	*ip6_protox[IPPROTO_MAX] = {
13078b1fc05SGleb Smirnoff 			    [0 ... IPPROTO_MAX - 1] = rip6_input };
13124b96f35SGleb Smirnoff ip6proto_ctlinput_t	*ip6_ctlprotox[IPPROTO_MAX] = {
13278b1fc05SGleb Smirnoff 			    [0 ... IPPROTO_MAX - 1] = rip6_ctlinput };
13382cd038dSYoshinobu Inoue 
13482cea7e6SBjoern A. Zeeb VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
13568eba526SAndrey V. Elsukov VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
13668eba526SAndrey V. Elsukov VNET_DEFINE(u_long, in6_ifaddrhmask);
137d4b5cae4SRobert Watson 
138d4b5cae4SRobert Watson static struct netisr_handler ip6_nh = {
139d4b5cae4SRobert Watson 	.nh_name = "ip6",
140d4b5cae4SRobert Watson 	.nh_handler = ip6_input,
141d4b5cae4SRobert Watson 	.nh_proto = NETISR_IPV6,
1420be18915SAdrian Chadd #ifdef RSS
1430be18915SAdrian Chadd 	.nh_m2cpuid = rss_soft_m2cpuid_v6,
1440be18915SAdrian Chadd 	.nh_policy = NETISR_POLICY_CPU,
1450be18915SAdrian Chadd 	.nh_dispatch = NETISR_DISPATCH_HYBRID,
1460be18915SAdrian Chadd #else
147d4b5cae4SRobert Watson 	.nh_policy = NETISR_POLICY_FLOW,
1480be18915SAdrian Chadd #endif
149d4b5cae4SRobert Watson };
15044e33a07SMarko Zec 
1517aeccebcSAndrey V. Elsukov static int
1527aeccebcSAndrey V. Elsukov sysctl_netinet6_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
1537aeccebcSAndrey V. Elsukov {
1547aeccebcSAndrey V. Elsukov 	int error, qlimit;
1557aeccebcSAndrey V. Elsukov 
1567aeccebcSAndrey V. Elsukov 	netisr_getqlimit(&ip6_nh, &qlimit);
1577aeccebcSAndrey V. Elsukov 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
1587aeccebcSAndrey V. Elsukov 	if (error || !req->newptr)
1597aeccebcSAndrey V. Elsukov 		return (error);
1607aeccebcSAndrey V. Elsukov 	if (qlimit < 1)
1617aeccebcSAndrey V. Elsukov 		return (EINVAL);
1627aeccebcSAndrey V. Elsukov 	return (netisr_setqlimit(&ip6_nh, qlimit));
1637aeccebcSAndrey V. Elsukov }
1647aeccebcSAndrey V. Elsukov SYSCTL_DECL(_net_inet6_ip6);
1657aeccebcSAndrey V. Elsukov SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRQMAXLEN, intr_queue_maxlen,
1667029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1677029da5cSPawel Biernacki     0, 0, sysctl_netinet6_intr_queue_maxlen, "I",
1687aeccebcSAndrey V. Elsukov     "Maximum size of the IPv6 input queue");
1697aeccebcSAndrey V. Elsukov 
1701817be48SGleb Smirnoff VNET_DEFINE_STATIC(bool, ip6_sav) = true;
1711817be48SGleb Smirnoff #define	V_ip6_sav	VNET(ip6_sav)
1721817be48SGleb Smirnoff SYSCTL_BOOL(_net_inet6_ip6, OID_AUTO, source_address_validation,
1731817be48SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_sav), true,
1741817be48SGleb Smirnoff     "Drop incoming packets with source address that is a local address");
1751817be48SGleb Smirnoff 
176aaa46574SAdrian Chadd #ifdef RSS
177aaa46574SAdrian Chadd static struct netisr_handler ip6_direct_nh = {
178aaa46574SAdrian Chadd 	.nh_name = "ip6_direct",
179aaa46574SAdrian Chadd 	.nh_handler = ip6_direct_input,
180aaa46574SAdrian Chadd 	.nh_proto = NETISR_IPV6_DIRECT,
181aaa46574SAdrian Chadd 	.nh_m2cpuid = rss_soft_m2cpuid_v6,
182aaa46574SAdrian Chadd 	.nh_policy = NETISR_POLICY_CPU,
183aaa46574SAdrian Chadd 	.nh_dispatch = NETISR_DISPATCH_HYBRID,
184aaa46574SAdrian Chadd };
1857aeccebcSAndrey V. Elsukov 
1867aeccebcSAndrey V. Elsukov static int
1877aeccebcSAndrey V. Elsukov sysctl_netinet6_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
1887aeccebcSAndrey V. Elsukov {
1897aeccebcSAndrey V. Elsukov 	int error, qlimit;
1907aeccebcSAndrey V. Elsukov 
1917aeccebcSAndrey V. Elsukov 	netisr_getqlimit(&ip6_direct_nh, &qlimit);
1927aeccebcSAndrey V. Elsukov 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
1937aeccebcSAndrey V. Elsukov 	if (error || !req->newptr)
1947aeccebcSAndrey V. Elsukov 		return (error);
1957aeccebcSAndrey V. Elsukov 	if (qlimit < 1)
1967aeccebcSAndrey V. Elsukov 		return (EINVAL);
1977aeccebcSAndrey V. Elsukov 	return (netisr_setqlimit(&ip6_direct_nh, qlimit));
1987aeccebcSAndrey V. Elsukov }
1997aeccebcSAndrey V. Elsukov SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
2007029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2017029da5cSPawel Biernacki     0, 0, sysctl_netinet6_intr_direct_queue_maxlen, "I",
2027029da5cSPawel Biernacki     "Maximum size of the IPv6 direct input queue");
2037aeccebcSAndrey V. Elsukov 
204aaa46574SAdrian Chadd #endif
205aaa46574SAdrian Chadd 
206b252313fSGleb Smirnoff VNET_DEFINE(pfil_head_t, inet6_pfil_head);
2075ab15157SDoug Rabson VNET_DEFINE(pfil_head_t, inet6_local_pfil_head);
20882cea7e6SBjoern A. Zeeb 
209a786f679SAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
210a786f679SAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(ip6stat);
211a786f679SAndrey V. Elsukov #ifdef VIMAGE
212a786f679SAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(ip6stat);
213a786f679SAndrey V. Elsukov #endif /* VIMAGE */
21433841545SHajimu UMEMOTO 
215cc0a3c8cSAndrey V. Elsukov struct rmlock in6_ifaddr_lock;
216cc0a3c8cSAndrey V. Elsukov RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
217d1da0a06SRobert Watson 
2189233d8f3SDavid E. O'Brien static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
21982cd038dSYoshinobu Inoue 
22082cd038dSYoshinobu Inoue /*
22182cd038dSYoshinobu Inoue  * IP6 initialization: fill in IP6 protocol switch table.
22282cd038dSYoshinobu Inoue  * All protocols not implemented in kernel go to raw IP6 protocol handler.
22382cd038dSYoshinobu Inoue  */
22489128ff3SGleb Smirnoff static void
22589128ff3SGleb Smirnoff ip6_vnet_init(void *arg __unused)
22682cd038dSYoshinobu Inoue {
227b252313fSGleb Smirnoff 	struct pfil_head_args args;
22882cd038dSYoshinobu Inoue 
229385195c0SMarko Zec 	TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
230385195c0SMarko Zec 	    &V_ip6_auto_linklocal);
231dead1956SHiroki Sato 	TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
232dead1956SHiroki Sato 	TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
23344e33a07SMarko Zec 
234d7c5a620SMatt Macy 	CK_STAILQ_INIT(&V_in6_ifaddrhead);
23568eba526SAndrey V. Elsukov 	V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
23668eba526SAndrey V. Elsukov 	    &V_in6_ifaddrhmask);
23780af0152SRobert Watson 
2380b4b0b0fSJulian Elischer 	/* Initialize packet filter hooks. */
239b252313fSGleb Smirnoff 	args.pa_version = PFIL_VERSION;
240b252313fSGleb Smirnoff 	args.pa_flags = PFIL_IN | PFIL_OUT;
241b252313fSGleb Smirnoff 	args.pa_type = PFIL_TYPE_IP6;
242b252313fSGleb Smirnoff 	args.pa_headname = PFIL_INET6_NAME;
243b252313fSGleb Smirnoff 	V_inet6_pfil_head = pfil_head_register(&args);
2440b4b0b0fSJulian Elischer 
2455ab15157SDoug Rabson 	args.pa_flags = PFIL_OUT;
2465ab15157SDoug Rabson 	args.pa_headname = PFIL_INET6_LOCAL_NAME;
2475ab15157SDoug Rabson 	V_inet6_local_pfil_head = pfil_head_register(&args);
2485ab15157SDoug Rabson 
249ef91a976SAndrey V. Elsukov 	if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6,
250ef91a976SAndrey V. Elsukov 	    &V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
251ef91a976SAndrey V. Elsukov 	    HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
252ef91a976SAndrey V. Elsukov 		printf("%s: WARNING: unable to register input helper hook\n",
253ef91a976SAndrey V. Elsukov 		    __func__);
254ef91a976SAndrey V. Elsukov 	if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6,
255ef91a976SAndrey V. Elsukov 	    &V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
256ef91a976SAndrey V. Elsukov 	    HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
257ef91a976SAndrey V. Elsukov 		printf("%s: WARNING: unable to register output helper hook\n",
258ef91a976SAndrey V. Elsukov 		    __func__);
259ef91a976SAndrey V. Elsukov 
26082cea7e6SBjoern A. Zeeb 	scope6_init();
26182cea7e6SBjoern A. Zeeb 	addrsel_policy_init();
26282cea7e6SBjoern A. Zeeb 	nd6_init();
26382cea7e6SBjoern A. Zeeb 	frag6_init();
26482cea7e6SBjoern A. Zeeb 
26582cea7e6SBjoern A. Zeeb 	V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
26682cea7e6SBjoern A. Zeeb 
2671ed81b73SMarko Zec 	/* Skip global initialization stuff for non-default instances. */
268484149deSBjoern A. Zeeb #ifdef VIMAGE
269484149deSBjoern A. Zeeb 	netisr_register_vnet(&ip6_nh);
270484149deSBjoern A. Zeeb #ifdef RSS
271484149deSBjoern A. Zeeb 	netisr_register_vnet(&ip6_direct_nh);
272484149deSBjoern A. Zeeb #endif
273484149deSBjoern A. Zeeb #endif
27489128ff3SGleb Smirnoff }
27589128ff3SGleb Smirnoff VNET_SYSINIT(ip6_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
27689128ff3SGleb Smirnoff     ip6_vnet_init, NULL);
27789128ff3SGleb Smirnoff 
27889128ff3SGleb Smirnoff static void
27989128ff3SGleb Smirnoff ip6_init(void *arg __unused)
28089128ff3SGleb Smirnoff {
2811ed81b73SMarko Zec 
282f45cd79aSAndre Oppermann 	/*
28378b1fc05SGleb Smirnoff 	 * Register statically those protocols that are unlikely to ever go
28478b1fc05SGleb Smirnoff 	 * dynamic.
285f45cd79aSAndre Oppermann 	 */
28678b1fc05SGleb Smirnoff 	IP6PROTO_REGISTER(IPPROTO_ICMPV6, icmp6_input, rip6_ctlinput);
28778b1fc05SGleb Smirnoff 	IP6PROTO_REGISTER(IPPROTO_DSTOPTS, dest6_input, NULL);
28878b1fc05SGleb Smirnoff 	IP6PROTO_REGISTER(IPPROTO_ROUTING, route6_input, NULL);
28978b1fc05SGleb Smirnoff 	IP6PROTO_REGISTER(IPPROTO_FRAGMENT, frag6_input, NULL);
29078b1fc05SGleb Smirnoff 	IP6PROTO_REGISTER(IPPROTO_IPV4, encap6_input, NULL);
29178b1fc05SGleb Smirnoff 	IP6PROTO_REGISTER(IPPROTO_IPV6, encap6_input, NULL);
29278b1fc05SGleb Smirnoff 	IP6PROTO_REGISTER(IPPROTO_ETHERIP, encap6_input, NULL);
29378b1fc05SGleb Smirnoff 	IP6PROTO_REGISTER(IPPROTO_GRE, encap6_input, NULL);
29478b1fc05SGleb Smirnoff 	IP6PROTO_REGISTER(IPPROTO_PIM, encap6_input, NULL);
29578b1fc05SGleb Smirnoff #ifdef SCTP	/* XXX: has a loadable & static version */
29678b1fc05SGleb Smirnoff 	IP6PROTO_REGISTER(IPPROTO_SCTP, sctp6_input, sctp6_ctlinput);
29778b1fc05SGleb Smirnoff #endif
298c21fd232SAndre Oppermann 
29981a34d37SGleb Smirnoff 	EVENTHANDLER_REGISTER(vm_lowmem, frag6_drain, NULL, LOWMEM_PRI_DEFAULT);
30081a34d37SGleb Smirnoff 	EVENTHANDLER_REGISTER(mbuf_lowmem, frag6_drain, NULL,
30181a34d37SGleb Smirnoff 	    LOWMEM_PRI_DEFAULT);
30281a34d37SGleb Smirnoff 
303d4b5cae4SRobert Watson 	netisr_register(&ip6_nh);
304aaa46574SAdrian Chadd #ifdef RSS
305aaa46574SAdrian Chadd 	netisr_register(&ip6_direct_nh);
306aaa46574SAdrian Chadd #endif
30782cd038dSYoshinobu Inoue }
30889128ff3SGleb Smirnoff SYSINIT(ip6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_init, NULL);
30982cd038dSYoshinobu Inoue 
3101b48d245SBjoern A. Zeeb int
31124b96f35SGleb Smirnoff ip6proto_register(uint8_t proto, ip6proto_input_t input,
31224b96f35SGleb Smirnoff     ip6proto_ctlinput_t ctl)
3131b48d245SBjoern A. Zeeb {
3141b48d245SBjoern A. Zeeb 
31578b1fc05SGleb Smirnoff 	MPASS(proto > 0);
3161b48d245SBjoern A. Zeeb 
31778b1fc05SGleb Smirnoff 	if (ip6_protox[proto] == rip6_input) {
31878b1fc05SGleb Smirnoff 		ip6_protox[proto] = input;
31978b1fc05SGleb Smirnoff 		ip6_ctlprotox[proto] = ctl;
32078b1fc05SGleb Smirnoff 		return (0);
32178b1fc05SGleb Smirnoff 	} else
3221b48d245SBjoern A. Zeeb 		return (EEXIST);
3231b48d245SBjoern A. Zeeb }
3241b48d245SBjoern A. Zeeb 
3251b48d245SBjoern A. Zeeb int
32678b1fc05SGleb Smirnoff ip6proto_unregister(uint8_t proto)
3271b48d245SBjoern A. Zeeb {
3281b48d245SBjoern A. Zeeb 
32978b1fc05SGleb Smirnoff 	MPASS(proto > 0);
3301b48d245SBjoern A. Zeeb 
33178b1fc05SGleb Smirnoff 	if (ip6_protox[proto] != rip6_input) {
33278b1fc05SGleb Smirnoff 		ip6_protox[proto] = rip6_input;
33378b1fc05SGleb Smirnoff 		ip6_ctlprotox[proto] = rip6_ctlinput;
3341b48d245SBjoern A. Zeeb 		return (0);
33578b1fc05SGleb Smirnoff 	} else
33678b1fc05SGleb Smirnoff 		return (ENOENT);
3371b48d245SBjoern A. Zeeb }
3381b48d245SBjoern A. Zeeb 
339bc29160dSMarko Zec #ifdef VIMAGE
3403f58662dSBjoern A. Zeeb static void
3413f58662dSBjoern A. Zeeb ip6_destroy(void *unused __unused)
342bc29160dSMarko Zec {
34389856f7eSBjoern A. Zeeb 	struct ifaddr *ifa, *nifa;
34489856f7eSBjoern A. Zeeb 	struct ifnet *ifp;
345ef91a976SAndrey V. Elsukov 	int error;
346bc29160dSMarko Zec 
347484149deSBjoern A. Zeeb #ifdef RSS
348484149deSBjoern A. Zeeb 	netisr_unregister_vnet(&ip6_direct_nh);
349484149deSBjoern A. Zeeb #endif
350484149deSBjoern A. Zeeb 	netisr_unregister_vnet(&ip6_nh);
351484149deSBjoern A. Zeeb 
352b252313fSGleb Smirnoff 	pfil_head_unregister(V_inet6_pfil_head);
353ef91a976SAndrey V. Elsukov 	error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]);
354ef91a976SAndrey V. Elsukov 	if (error != 0) {
355ef91a976SAndrey V. Elsukov 		printf("%s: WARNING: unable to deregister input helper hook "
356ef91a976SAndrey V. Elsukov 		    "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: "
357ef91a976SAndrey V. Elsukov 		    "error %d returned\n", __func__, error);
358ef91a976SAndrey V. Elsukov 	}
359ef91a976SAndrey V. Elsukov 	error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]);
360ef91a976SAndrey V. Elsukov 	if (error != 0) {
361ef91a976SAndrey V. Elsukov 		printf("%s: WARNING: unable to deregister output helper hook "
362ef91a976SAndrey V. Elsukov 		    "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: "
363ef91a976SAndrey V. Elsukov 		    "error %d returned\n", __func__, error);
364ef91a976SAndrey V. Elsukov 	}
36589856f7eSBjoern A. Zeeb 
36689856f7eSBjoern A. Zeeb 	/* Cleanup addresses. */
36789856f7eSBjoern A. Zeeb 	IFNET_RLOCK();
3684f6c66ccSMatt Macy 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
36989856f7eSBjoern A. Zeeb 		/* Cannot lock here - lock recursion. */
37089856f7eSBjoern A. Zeeb 		/* IF_ADDR_LOCK(ifp); */
371d7c5a620SMatt Macy 		CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
37289856f7eSBjoern A. Zeeb 			if (ifa->ifa_addr->sa_family != AF_INET6)
37389856f7eSBjoern A. Zeeb 				continue;
37489856f7eSBjoern A. Zeeb 			in6_purgeaddr(ifa);
37589856f7eSBjoern A. Zeeb 		}
37689856f7eSBjoern A. Zeeb 		/* IF_ADDR_UNLOCK(ifp); */
37789856f7eSBjoern A. Zeeb 		in6_ifdetach_destroy(ifp);
37889856f7eSBjoern A. Zeeb 		mld_domifdetach(ifp);
37989856f7eSBjoern A. Zeeb 	}
38089856f7eSBjoern A. Zeeb 	IFNET_RUNLOCK();
38189856f7eSBjoern A. Zeeb 
382b1d63265SAlexander V. Chernikov 	/* Make sure any routes are gone as well. */
383b1d63265SAlexander V. Chernikov 	rib_flush_routes_family(AF_INET6);
384b1d63265SAlexander V. Chernikov 
38567a10c46SBjoern A. Zeeb 	frag6_destroy();
386bc29160dSMarko Zec 	nd6_destroy();
3879901091eSBjoern A. Zeeb 	in6_ifattach_destroy();
38889856f7eSBjoern A. Zeeb 
38989856f7eSBjoern A. Zeeb 	hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
390bc29160dSMarko Zec }
3913f58662dSBjoern A. Zeeb 
3923f58662dSBjoern A. Zeeb VNET_SYSUNINIT(inet6, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_destroy, NULL);
393bc29160dSMarko Zec #endif
394bc29160dSMarko Zec 
3951ed81b73SMarko Zec static int
396e7a541b0SMichael Tuexen ip6_input_hbh(struct mbuf **mp, uint32_t *plen, uint32_t *rtalert, int *off,
397ae145050SBjoern A. Zeeb     int *nxt, int *ours)
398ae145050SBjoern A. Zeeb {
399e7a541b0SMichael Tuexen 	struct mbuf *m;
400ae145050SBjoern A. Zeeb 	struct ip6_hdr *ip6;
401ae145050SBjoern A. Zeeb 	struct ip6_hbh *hbh;
402ae145050SBjoern A. Zeeb 
403e7a541b0SMichael Tuexen 	if (ip6_hopopts_input(plen, rtalert, mp, off)) {
404ae145050SBjoern A. Zeeb #if 0	/*touches NULL pointer*/
405e7a541b0SMichael Tuexen 		in6_ifstat_inc((*mp)->m_pkthdr.rcvif, ifs6_in_discard);
406ae145050SBjoern A. Zeeb #endif
407ae145050SBjoern A. Zeeb 		goto out;	/* m have already been freed */
408ae145050SBjoern A. Zeeb 	}
409ae145050SBjoern A. Zeeb 
410ae145050SBjoern A. Zeeb 	/* adjust pointer */
411e7a541b0SMichael Tuexen 	m = *mp;
412ae145050SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
413ae145050SBjoern A. Zeeb 
414ae145050SBjoern A. Zeeb 	/*
415ae145050SBjoern A. Zeeb 	 * if the payload length field is 0 and the next header field
416ae145050SBjoern A. Zeeb 	 * indicates Hop-by-Hop Options header, then a Jumbo Payload
417ae145050SBjoern A. Zeeb 	 * option MUST be included.
418ae145050SBjoern A. Zeeb 	 */
419ae145050SBjoern A. Zeeb 	if (ip6->ip6_plen == 0 && *plen == 0) {
420ae145050SBjoern A. Zeeb 		/*
421ae145050SBjoern A. Zeeb 		 * Note that if a valid jumbo payload option is
422ae145050SBjoern A. Zeeb 		 * contained, ip6_hopopts_input() must set a valid
423ae145050SBjoern A. Zeeb 		 * (non-zero) payload length to the variable plen.
424ae145050SBjoern A. Zeeb 		 */
4259cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badoptions);
426ae145050SBjoern A. Zeeb 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
427ae145050SBjoern A. Zeeb 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
428ae145050SBjoern A. Zeeb 		icmp6_error(m, ICMP6_PARAM_PROB,
429ae145050SBjoern A. Zeeb 			    ICMP6_PARAMPROB_HEADER,
430ae145050SBjoern A. Zeeb 			    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
431ae145050SBjoern A. Zeeb 		goto out;
432ae145050SBjoern A. Zeeb 	}
433ae145050SBjoern A. Zeeb 	/* ip6_hopopts_input() ensures that mbuf is contiguous */
434ae145050SBjoern A. Zeeb 	hbh = (struct ip6_hbh *)(ip6 + 1);
435ae145050SBjoern A. Zeeb 	*nxt = hbh->ip6h_nxt;
436ae145050SBjoern A. Zeeb 
437ae145050SBjoern A. Zeeb 	/*
438ae145050SBjoern A. Zeeb 	 * If we are acting as a router and the packet contains a
439ae145050SBjoern A. Zeeb 	 * router alert option, see if we know the option value.
440ae145050SBjoern A. Zeeb 	 * Currently, we only support the option value for MLD, in which
441ae145050SBjoern A. Zeeb 	 * case we should pass the packet to the multicast routing
442ae145050SBjoern A. Zeeb 	 * daemon.
443ae145050SBjoern A. Zeeb 	 */
444ae145050SBjoern A. Zeeb 	if (*rtalert != ~0) {
445ae145050SBjoern A. Zeeb 		switch (*rtalert) {
446ae145050SBjoern A. Zeeb 		case IP6OPT_RTALERT_MLD:
447ae145050SBjoern A. Zeeb 			if (V_ip6_forwarding)
448ae145050SBjoern A. Zeeb 				*ours = 1;
449ae145050SBjoern A. Zeeb 			break;
450ae145050SBjoern A. Zeeb 		default:
451ae145050SBjoern A. Zeeb 			/*
452ae145050SBjoern A. Zeeb 			 * RFC2711 requires unrecognized values must be
453ae145050SBjoern A. Zeeb 			 * silently ignored.
454ae145050SBjoern A. Zeeb 			 */
455ae145050SBjoern A. Zeeb 			break;
456ae145050SBjoern A. Zeeb 		}
457ae145050SBjoern A. Zeeb 	}
458ae145050SBjoern A. Zeeb 
459ae145050SBjoern A. Zeeb 	return (0);
460ae145050SBjoern A. Zeeb 
461ae145050SBjoern A. Zeeb out:
462ae145050SBjoern A. Zeeb 	return (1);
463ae145050SBjoern A. Zeeb }
464ae145050SBjoern A. Zeeb 
465aaa46574SAdrian Chadd #ifdef RSS
466aaa46574SAdrian Chadd /*
467aaa46574SAdrian Chadd  * IPv6 direct input routine.
468aaa46574SAdrian Chadd  *
469aaa46574SAdrian Chadd  * This is called when reinjecting completed fragments where
470aaa46574SAdrian Chadd  * all of the previous checking and book-keeping has been done.
471aaa46574SAdrian Chadd  */
472aaa46574SAdrian Chadd void
473aaa46574SAdrian Chadd ip6_direct_input(struct mbuf *m)
474aaa46574SAdrian Chadd {
475aaa46574SAdrian Chadd 	int off, nxt;
476aaa46574SAdrian Chadd 	int nest;
477aaa46574SAdrian Chadd 	struct m_tag *mtag;
478aaa46574SAdrian Chadd 	struct ip6_direct_ctx *ip6dc;
479aaa46574SAdrian Chadd 
480aaa46574SAdrian Chadd 	mtag = m_tag_locate(m, MTAG_ABI_IPV6, IPV6_TAG_DIRECT, NULL);
481aaa46574SAdrian Chadd 	KASSERT(mtag != NULL, ("Reinjected packet w/o direct ctx tag!"));
482aaa46574SAdrian Chadd 
483aaa46574SAdrian Chadd 	ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
484aaa46574SAdrian Chadd 	nxt = ip6dc->ip6dc_nxt;
485aaa46574SAdrian Chadd 	off = ip6dc->ip6dc_off;
486aaa46574SAdrian Chadd 
487aaa46574SAdrian Chadd 	nest = 0;
488aaa46574SAdrian Chadd 
489aaa46574SAdrian Chadd 	m_tag_delete(m, mtag);
490aaa46574SAdrian Chadd 
491aaa46574SAdrian Chadd 	while (nxt != IPPROTO_DONE) {
492aaa46574SAdrian Chadd 		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
493aaa46574SAdrian Chadd 			IP6STAT_INC(ip6s_toomanyhdr);
494aaa46574SAdrian Chadd 			goto bad;
495aaa46574SAdrian Chadd 		}
496aaa46574SAdrian Chadd 
497aaa46574SAdrian Chadd 		/*
498aaa46574SAdrian Chadd 		 * protection against faulty packet - there should be
499aaa46574SAdrian Chadd 		 * more sanity checks in header chain processing.
500aaa46574SAdrian Chadd 		 */
501aaa46574SAdrian Chadd 		if (m->m_pkthdr.len < off) {
502aaa46574SAdrian Chadd 			IP6STAT_INC(ip6s_tooshort);
503aaa46574SAdrian Chadd 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
504aaa46574SAdrian Chadd 			goto bad;
505aaa46574SAdrian Chadd 		}
506aaa46574SAdrian Chadd 
507fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
508fcf59617SAndrey V. Elsukov 		if (IPSEC_ENABLED(ipv6)) {
509fcf59617SAndrey V. Elsukov 			if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
510fcf59617SAndrey V. Elsukov 				return;
511fcf59617SAndrey V. Elsukov 		}
512aaa46574SAdrian Chadd #endif /* IPSEC */
513aaa46574SAdrian Chadd 
51478b1fc05SGleb Smirnoff 		nxt = ip6_protox[nxt](&m, &off, nxt);
515aaa46574SAdrian Chadd 	}
516aaa46574SAdrian Chadd 	return;
517aaa46574SAdrian Chadd bad:
518aaa46574SAdrian Chadd 	m_freem(m);
519aaa46574SAdrian Chadd }
520aaa46574SAdrian Chadd #endif
521aaa46574SAdrian Chadd 
52282cd038dSYoshinobu Inoue void
5231272577eSXin LI ip6_input(struct mbuf *m)
52482cd038dSYoshinobu Inoue {
5253e88eb90SAndrey V. Elsukov 	struct in6_addr odst;
52682cd038dSYoshinobu Inoue 	struct ip6_hdr *ip6;
5273e88eb90SAndrey V. Elsukov 	struct in6_ifaddr *ia;
5288a030e9cSAndrey V. Elsukov 	struct ifnet *rcvif;
52982cd038dSYoshinobu Inoue 	u_int32_t plen;
53082cd038dSYoshinobu Inoue 	u_int32_t rtalert = ~0;
5313e88eb90SAndrey V. Elsukov 	int off = sizeof(struct ip6_hdr), nest;
53282cd038dSYoshinobu Inoue 	int nxt, ours = 0;
5333c92002fSSam Leffler 	int srcrt = 0;
534e3124327SSam Leffler 
5355a1842a2SAndrey V. Elsukov 	/*
5365a1842a2SAndrey V. Elsukov 	 * Drop the packet if IPv6 operation is disabled on the interface.
5375a1842a2SAndrey V. Elsukov 	 */
5388a030e9cSAndrey V. Elsukov 	rcvif = m->m_pkthdr.rcvif;
5398a030e9cSAndrey V. Elsukov 	if ((ND_IFINFO(rcvif)->flags & ND6_IFF_IFDISABLED))
5405a1842a2SAndrey V. Elsukov 		goto bad;
5415a1842a2SAndrey V. Elsukov 
542fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
54382cd038dSYoshinobu Inoue 	/*
54482cd038dSYoshinobu Inoue 	 * should the inner packet be considered authentic?
54582cd038dSYoshinobu Inoue 	 * see comment in ah4_input().
5462cb64cb2SGeorge V. Neville-Neil 	 * NB: m cannot be NULL when passed to the input routine
54782cd038dSYoshinobu Inoue 	 */
5482cb64cb2SGeorge V. Neville-Neil 
54982cd038dSYoshinobu Inoue 	m->m_flags &= ~M_AUTHIPHDR;
55082cd038dSYoshinobu Inoue 	m->m_flags &= ~M_AUTHIPDGM;
5512cb64cb2SGeorge V. Neville-Neil 
552b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
55382cd038dSYoshinobu Inoue 
5548a006adbSBjoern A. Zeeb 	if (m->m_flags & M_FASTFWD_OURS) {
5558a006adbSBjoern A. Zeeb 		/*
5568a006adbSBjoern A. Zeeb 		 * Firewall changed destination to local.
5578a006adbSBjoern A. Zeeb 		 */
5588a006adbSBjoern A. Zeeb 		ip6 = mtod(m, struct ip6_hdr *);
5592164def6SAndrey V. Elsukov 		goto passin;
5608a006adbSBjoern A. Zeeb 	}
5618a006adbSBjoern A. Zeeb 
56233841545SHajimu UMEMOTO 	/*
56388ff5695SSUZUKI Shinsuke 	 * mbuf statistics
56482cd038dSYoshinobu Inoue 	 */
56582cd038dSYoshinobu Inoue 	if (m->m_flags & M_EXT) {
56682cd038dSYoshinobu Inoue 		if (m->m_next)
5679cb8d207SAndrey V. Elsukov 			IP6STAT_INC(ip6s_mext2m);
56882cd038dSYoshinobu Inoue 		else
5699cb8d207SAndrey V. Elsukov 			IP6STAT_INC(ip6s_mext1);
57082cd038dSYoshinobu Inoue 	} else {
57182cd038dSYoshinobu Inoue 		if (m->m_next) {
5720ed72537SAlexander V. Chernikov 			struct ifnet *ifp = (m->m_flags & M_LOOP) ? V_loif : rcvif;
5730ed72537SAlexander V. Chernikov 			int ifindex = ifp->if_index;
5740ed72537SAlexander V. Chernikov 			if (ifindex >= IP6S_M2MMAX)
5750ed72537SAlexander V. Chernikov 				ifindex = 0;
57660d8dbbeSKristof Provost 			IP6STAT_INC2(ip6s_m2m, ifindex);
57782cd038dSYoshinobu Inoue 		} else
5789cb8d207SAndrey V. Elsukov 			IP6STAT_INC(ip6s_m1);
57982cd038dSYoshinobu Inoue 	}
58082cd038dSYoshinobu Inoue 
5818a030e9cSAndrey V. Elsukov 	in6_ifstat_inc(rcvif, ifs6_in_receive);
5829cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_total);
58382cd038dSYoshinobu Inoue 
584f13bb832SJun Kuriyama 	/*
585f13bb832SJun Kuriyama 	 * L2 bridge code and some other code can return mbuf chain
586f13bb832SJun Kuriyama 	 * that does not conform to KAME requirement.  too bad.
587f13bb832SJun Kuriyama 	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
588f13bb832SJun Kuriyama 	 */
589f13bb832SJun Kuriyama 	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
590f13bb832SJun Kuriyama 		struct mbuf *n;
591f13bb832SJun Kuriyama 
592aa481811SGleb Smirnoff 		if (m->m_pkthdr.len > MHLEN)
593aa481811SGleb Smirnoff 			n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
594aa481811SGleb Smirnoff 		else
595aa481811SGleb Smirnoff 			n = m_gethdr(M_NOWAIT, MT_DATA);
5968a030e9cSAndrey V. Elsukov 		if (n == NULL)
5978a030e9cSAndrey V. Elsukov 			goto bad;
598f13bb832SJun Kuriyama 
59910e5acc3SGleb Smirnoff 		m_move_pkthdr(n, m);
60053d96a08SSam Leffler 		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
60153d96a08SSam Leffler 		n->m_len = n->m_pkthdr.len;
602f13bb832SJun Kuriyama 		m_freem(m);
603f13bb832SJun Kuriyama 		m = n;
604f13bb832SJun Kuriyama 	}
60582cd038dSYoshinobu Inoue 	if (m->m_len < sizeof(struct ip6_hdr)) {
6067efe5d92SHajimu UMEMOTO 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
6079cb8d207SAndrey V. Elsukov 			IP6STAT_INC(ip6s_toosmall);
6088a030e9cSAndrey V. Elsukov 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
6098a030e9cSAndrey V. Elsukov 			goto bad;
61082cd038dSYoshinobu Inoue 		}
61182cd038dSYoshinobu Inoue 	}
61282cd038dSYoshinobu Inoue 
61382cd038dSYoshinobu Inoue 	ip6 = mtod(m, struct ip6_hdr *);
61482cd038dSYoshinobu Inoue 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
6159cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badvers);
6168a030e9cSAndrey V. Elsukov 		in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
61782cd038dSYoshinobu Inoue 		goto bad;
61882cd038dSYoshinobu Inoue 	}
61982cd038dSYoshinobu Inoue 
62060d8dbbeSKristof Provost 	IP6STAT_INC2(ip6s_nxthist, ip6->ip6_nxt);
6218a030e9cSAndrey V. Elsukov 	IP_PROBE(receive, NULL, NULL, ip6, rcvif, NULL, ip6);
62257f60867SMark Johnston 
62382cd038dSYoshinobu Inoue 	/*
624*40faf878SMark Johnston 	 * Check against address spoofing/corruption.  The unspecified address
625*40faf878SMark Johnston 	 * is checked further below.
62682cd038dSYoshinobu Inoue 	 */
627*40faf878SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
62833841545SHajimu UMEMOTO 		/*
62933841545SHajimu UMEMOTO 		 * XXX: "badscope" is not very suitable for a multicast source.
63033841545SHajimu UMEMOTO 		 */
6319cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope);
6328a030e9cSAndrey V. Elsukov 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
63382cd038dSYoshinobu Inoue 		goto bad;
63482cd038dSYoshinobu Inoue 	}
635cfcea119SHajimu UMEMOTO 	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
636cfcea119SHajimu UMEMOTO 	    !(m->m_flags & M_LOOP)) {
637cfcea119SHajimu UMEMOTO 		/*
638cfcea119SHajimu UMEMOTO 		 * In this case, the packet should come from the loopback
639cfcea119SHajimu UMEMOTO 		 * interface.  However, we cannot just check the if_flags,
640cfcea119SHajimu UMEMOTO 		 * because ip6_mloopback() passes the "actual" interface
641cfcea119SHajimu UMEMOTO 		 * as the outgoing/incoming interface.
642cfcea119SHajimu UMEMOTO 		 */
6439cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope);
6448a030e9cSAndrey V. Elsukov 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
64533841545SHajimu UMEMOTO 		goto bad;
64633841545SHajimu UMEMOTO 	}
647e4c77ca0SAndrey V. Elsukov 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
648e4c77ca0SAndrey V. Elsukov 	    IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
649e4c77ca0SAndrey V. Elsukov 		/*
650e4c77ca0SAndrey V. Elsukov 		 * RFC4291 2.7:
651e4c77ca0SAndrey V. Elsukov 		 * Nodes must not originate a packet to a multicast address
652e4c77ca0SAndrey V. Elsukov 		 * whose scop field contains the reserved value 0; if such
653e4c77ca0SAndrey V. Elsukov 		 * a packet is received, it must be silently dropped.
654e4c77ca0SAndrey V. Elsukov 		 */
655e4c77ca0SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope);
6568a030e9cSAndrey V. Elsukov 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
657e4c77ca0SAndrey V. Elsukov 		goto bad;
658e4c77ca0SAndrey V. Elsukov 	}
659686cdd19SJun-ichiro itojun Hagino 	/*
66033841545SHajimu UMEMOTO 	 * The following check is not documented in specs.  A malicious
66133841545SHajimu UMEMOTO 	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
66233841545SHajimu UMEMOTO 	 * and bypass security checks (act as if it was from 127.0.0.1 by using
66333841545SHajimu UMEMOTO 	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
66433841545SHajimu UMEMOTO 	 *
66574ff87cdSBjoern A. Zeeb 	 * We have supported IPv6-only kernels for a few years and this issue
66674ff87cdSBjoern A. Zeeb 	 * has not come up.  The world seems to move mostly towards not using
66774ff87cdSBjoern A. Zeeb 	 * v4mapped on the wire, so it makes sense for us to keep rejecting
66874ff87cdSBjoern A. Zeeb 	 * any such packets.
669686cdd19SJun-ichiro itojun Hagino 	 */
67033841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
67133841545SHajimu UMEMOTO 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
6729cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope);
6738a030e9cSAndrey V. Elsukov 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
67433841545SHajimu UMEMOTO 		goto bad;
67533841545SHajimu UMEMOTO 	}
676686cdd19SJun-ichiro itojun Hagino #if 0
677686cdd19SJun-ichiro itojun Hagino 	/*
678686cdd19SJun-ichiro itojun Hagino 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
679686cdd19SJun-ichiro itojun Hagino 	 *
680686cdd19SJun-ichiro itojun Hagino 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
681686cdd19SJun-ichiro itojun Hagino 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
682686cdd19SJun-ichiro itojun Hagino 	 * is revised to forbid relaying case.
683686cdd19SJun-ichiro itojun Hagino 	 */
684686cdd19SJun-ichiro itojun Hagino 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
685686cdd19SJun-ichiro itojun Hagino 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
6869cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope);
687686cdd19SJun-ichiro itojun Hagino 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
688686cdd19SJun-ichiro itojun Hagino 		goto bad;
689686cdd19SJun-ichiro itojun Hagino 	}
690686cdd19SJun-ichiro itojun Hagino #endif
691ad9f4d6aSAndrey V. Elsukov 	/*
692ad9f4d6aSAndrey V. Elsukov 	 * Try to forward the packet, but if we fail continue.
69362484790SAndrey V. Elsukov 	 * ip6_tryforward() does not generate redirects, so fall
69462484790SAndrey V. Elsukov 	 * through to normal processing if redirects are required.
695ad9f4d6aSAndrey V. Elsukov 	 * ip6_tryforward() does inbound and outbound packet firewall
696ad9f4d6aSAndrey V. Elsukov 	 * processing. If firewall has decided that destination becomes
697ad9f4d6aSAndrey V. Elsukov 	 * our local address, it sets M_FASTFWD_OURS flag. In this
698ad9f4d6aSAndrey V. Elsukov 	 * case skip another inbound firewall processing and update
699ad9f4d6aSAndrey V. Elsukov 	 * ip6 pointer.
700ad9f4d6aSAndrey V. Elsukov 	 */
70162484790SAndrey V. Elsukov 	if (V_ip6_forwarding != 0 && V_ip6_sendredirects == 0
702fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
703fcf59617SAndrey V. Elsukov 	    && (!IPSEC_ENABLED(ipv6) ||
704fcf59617SAndrey V. Elsukov 	    IPSEC_CAPS(ipv6, m, IPSEC_CAP_OPERABLE) == 0)
705ad9f4d6aSAndrey V. Elsukov #endif
706ad9f4d6aSAndrey V. Elsukov 	    ) {
707ad9f4d6aSAndrey V. Elsukov 		if ((m = ip6_tryforward(m)) == NULL)
7085a1842a2SAndrey V. Elsukov 			return;
709ad9f4d6aSAndrey V. Elsukov 		if (m->m_flags & M_FASTFWD_OURS) {
710ad9f4d6aSAndrey V. Elsukov 			ip6 = mtod(m, struct ip6_hdr *);
7112164def6SAndrey V. Elsukov 			goto passin;
712ad9f4d6aSAndrey V. Elsukov 		}
713ad9f4d6aSAndrey V. Elsukov 	}
714fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
71586905204SBjoern A. Zeeb 	/*
71686905204SBjoern A. Zeeb 	 * Bypass packet filtering for packets previously handled by IPsec.
71786905204SBjoern A. Zeeb 	 */
718fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv6) &&
719fcf59617SAndrey V. Elsukov 	    IPSEC_CAPS(ipv6, m, IPSEC_CAP_BYPASS_FILTER) != 0)
72086905204SBjoern A. Zeeb 			goto passin;
721ad9f4d6aSAndrey V. Elsukov #endif
722cfcea119SHajimu UMEMOTO 	/*
72343eb694aSMax Laier 	 * Run through list of hooks for input packets.
72443eb694aSMax Laier 	 *
72543eb694aSMax Laier 	 * NB: Beware of the destination address changing
72643eb694aSMax Laier 	 *     (e.g. by NAT rewriting).  When this happens,
72743eb694aSMax Laier 	 *     tell ip6_forward to do the right thing.
72843eb694aSMax Laier 	 */
729c21fd232SAndre Oppermann 
730c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
731b252313fSGleb Smirnoff 	if (!PFIL_HOOKED_IN(V_inet6_pfil_head))
732c21fd232SAndre Oppermann 		goto passin;
733c21fd232SAndre Oppermann 
734ad9f4d6aSAndrey V. Elsukov 	odst = ip6->ip6_dst;
735dda6376bSMateusz Guzik 	if (pfil_mbuf_in(V_inet6_pfil_head, &m, m->m_pkthdr.rcvif,
736b252313fSGleb Smirnoff 	    NULL) != PFIL_PASS)
73743eb694aSMax Laier 		return;
73843eb694aSMax Laier 	ip6 = mtod(m, struct ip6_hdr *);
73943eb694aSMax Laier 	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
7402164def6SAndrey V. Elsukov 	if ((m->m_flags & (M_IP6_NEXTHOP | M_FASTFWD_OURS)) == M_IP6_NEXTHOP &&
741ffdbf9daSAndrey V. Elsukov 	    m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
7428a006adbSBjoern A. Zeeb 		/*
7438a006adbSBjoern A. Zeeb 		 * Directly ship the packet on.  This allows forwarding
7448a006adbSBjoern A. Zeeb 		 * packets originally destined to us to some other directly
7458a006adbSBjoern A. Zeeb 		 * connected host.
7468a006adbSBjoern A. Zeeb 		 */
7478a006adbSBjoern A. Zeeb 		ip6_forward(m, 1);
7483e88eb90SAndrey V. Elsukov 		return;
7498a006adbSBjoern A. Zeeb 	}
7508a006adbSBjoern A. Zeeb 
751c21fd232SAndre Oppermann passin:
75243eb694aSMax Laier 	/*
753*40faf878SMark Johnston 	 * The check is deferred to here to give firewalls a chance to block
754*40faf878SMark Johnston 	 * (and log) such packets.  ip6_tryforward() will not process such
755*40faf878SMark Johnston 	 * packets.
756*40faf878SMark Johnston 	 */
757*40faf878SMark Johnston 	if (__predict_false(IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst))) {
758*40faf878SMark Johnston 		IP6STAT_INC(ip6s_badscope);
759*40faf878SMark Johnston 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
760*40faf878SMark Johnston 		goto bad;
761*40faf878SMark Johnston 	}
762*40faf878SMark Johnston 
763*40faf878SMark Johnston 	/*
764411babc6SHajimu UMEMOTO 	 * Disambiguate address scope zones (if there is ambiguity).
765411babc6SHajimu UMEMOTO 	 * We first make sure that the original source or destination address
766411babc6SHajimu UMEMOTO 	 * is not in our internal form for scoped addresses.  Such addresses
767411babc6SHajimu UMEMOTO 	 * are not necessarily invalid spec-wise, but we cannot accept them due
768411babc6SHajimu UMEMOTO 	 * to the usage conflict.
769411babc6SHajimu UMEMOTO 	 * in6_setscope() then also checks and rejects the cases where src or
770411babc6SHajimu UMEMOTO 	 * dst are the loopback address and the receiving interface
771411babc6SHajimu UMEMOTO 	 * is not loopback.
772411babc6SHajimu UMEMOTO 	 */
773411babc6SHajimu UMEMOTO 	if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
7749cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope); /* XXX */
775411babc6SHajimu UMEMOTO 		goto bad;
776411babc6SHajimu UMEMOTO 	}
7778a030e9cSAndrey V. Elsukov 	if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
7788a030e9cSAndrey V. Elsukov 	    in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
7799cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badscope);
780411babc6SHajimu UMEMOTO 		goto bad;
781411babc6SHajimu UMEMOTO 	}
7822164def6SAndrey V. Elsukov 	if (m->m_flags & M_FASTFWD_OURS) {
7832164def6SAndrey V. Elsukov 		m->m_flags &= ~M_FASTFWD_OURS;
7842164def6SAndrey V. Elsukov 		ours = 1;
7852164def6SAndrey V. Elsukov 		goto hbhcheck;
7862164def6SAndrey V. Elsukov 	}
787411babc6SHajimu UMEMOTO 	/*
78833cde130SBruce M Simpson 	 * Multicast check. Assume packet is for us to avoid
78933cde130SBruce M Simpson 	 * prematurely taking locks.
79082cd038dSYoshinobu Inoue 	 */
79182cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
79282cd038dSYoshinobu Inoue 		ours = 1;
7938a030e9cSAndrey V. Elsukov 		in6_ifstat_inc(rcvif, ifs6_in_mcast);
79482cd038dSYoshinobu Inoue 		goto hbhcheck;
79582cd038dSYoshinobu Inoue 	}
79682cd038dSYoshinobu Inoue 	/*
79782cd038dSYoshinobu Inoue 	 * Unicast check
7983e88eb90SAndrey V. Elsukov 	 * XXX: For now we keep link-local IPv6 addresses with embedded
7993e88eb90SAndrey V. Elsukov 	 *      scope zone id, therefore we use zero zoneid here.
80082cd038dSYoshinobu Inoue 	 */
8018268d82cSAlexander V. Chernikov 	ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
8023e88eb90SAndrey V. Elsukov 	if (ia != NULL) {
8033e88eb90SAndrey V. Elsukov 		if (ia->ia6_flags & IN6_IFF_NOTREADY) {
804101235dcSBjoern A. Zeeb 			char ip6bufs[INET6_ADDRSTRLEN];
805101235dcSBjoern A. Zeeb 			char ip6bufd[INET6_ADDRSTRLEN];
806101235dcSBjoern A. Zeeb 			/* address is not ready, so discard the packet. */
807101235dcSBjoern A. Zeeb 			nd6log((LOG_INFO,
808101235dcSBjoern A. Zeeb 			    "ip6_input: packet to an unready address %s->%s\n",
809101235dcSBjoern A. Zeeb 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
810101235dcSBjoern A. Zeeb 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
811101235dcSBjoern A. Zeeb 			goto bad;
8126e6b3f7cSQing Li 		}
81350fa27e7SAlexander V. Chernikov 		if (V_ip6_sav && !(m->m_flags & M_LOOP) &&
8141817be48SGleb Smirnoff 		    __predict_false(in6_localip_fib(&ip6->ip6_src,
8151817be48SGleb Smirnoff 			    rcvif->if_fib))) {
8161817be48SGleb Smirnoff 			IP6STAT_INC(ip6s_badscope); /* XXX */
8171817be48SGleb Smirnoff 			goto bad;
8181817be48SGleb Smirnoff 		}
8195da9f8faSJosef Karthauser 		/* Count the packet in the ip address stats */
8203e88eb90SAndrey V. Elsukov 		counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
8213e88eb90SAndrey V. Elsukov 		counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
82282cd038dSYoshinobu Inoue 		ours = 1;
82382cd038dSYoshinobu Inoue 		goto hbhcheck;
82482cd038dSYoshinobu Inoue 	}
82582cd038dSYoshinobu Inoue 
82682cd038dSYoshinobu Inoue 	/*
82782cd038dSYoshinobu Inoue 	 * Now there is no reason to process the packet if it's not our own
82882cd038dSYoshinobu Inoue 	 * and we're not a router.
82982cd038dSYoshinobu Inoue 	 */
830603724d3SBjoern A. Zeeb 	if (!V_ip6_forwarding) {
8319cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_cantforward);
83282cd038dSYoshinobu Inoue 		goto bad;
83382cd038dSYoshinobu Inoue 	}
83482cd038dSYoshinobu Inoue 
83582cd038dSYoshinobu Inoue   hbhcheck:
83682cd038dSYoshinobu Inoue 	/*
83782cd038dSYoshinobu Inoue 	 * Process Hop-by-Hop options header if it's contained.
83882cd038dSYoshinobu Inoue 	 * m may be modified in ip6_hopopts_input().
83982cd038dSYoshinobu Inoue 	 * If a JumboPayload option is included, plen will also be modified.
84082cd038dSYoshinobu Inoue 	 */
84182cd038dSYoshinobu Inoue 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
84282cd038dSYoshinobu Inoue 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
843e7a541b0SMichael Tuexen 		if (ip6_input_hbh(&m, &plen, &rtalert, &off, &nxt, &ours) != 0)
8443e88eb90SAndrey V. Elsukov 			return;
84582cd038dSYoshinobu Inoue 	} else
84682cd038dSYoshinobu Inoue 		nxt = ip6->ip6_nxt;
84782cd038dSYoshinobu Inoue 
84882cd038dSYoshinobu Inoue 	/*
849aaa46574SAdrian Chadd 	 * Use mbuf flags to propagate Router Alert option to
850aaa46574SAdrian Chadd 	 * ICMPv6 layer, as hop-by-hop options have been stripped.
851aaa46574SAdrian Chadd 	 */
852aaa46574SAdrian Chadd 	if (rtalert != ~0)
853aaa46574SAdrian Chadd 		m->m_flags |= M_RTALERT_MLD;
854aaa46574SAdrian Chadd 
855aaa46574SAdrian Chadd 	/*
85682cd038dSYoshinobu Inoue 	 * Check that the amount of data in the buffers
85782cd038dSYoshinobu Inoue 	 * is as at least much as the IPv6 header would have us expect.
85882cd038dSYoshinobu Inoue 	 * Trim mbufs if longer than we expect.
85982cd038dSYoshinobu Inoue 	 * Drop packet if shorter than we expect.
86082cd038dSYoshinobu Inoue 	 */
86182cd038dSYoshinobu Inoue 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
8629cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_tooshort);
8638a030e9cSAndrey V. Elsukov 		in6_ifstat_inc(rcvif, ifs6_in_truncated);
86482cd038dSYoshinobu Inoue 		goto bad;
86582cd038dSYoshinobu Inoue 	}
86682cd038dSYoshinobu Inoue 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
86782cd038dSYoshinobu Inoue 		if (m->m_len == m->m_pkthdr.len) {
86882cd038dSYoshinobu Inoue 			m->m_len = sizeof(struct ip6_hdr) + plen;
86982cd038dSYoshinobu Inoue 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
87082cd038dSYoshinobu Inoue 		} else
87182cd038dSYoshinobu Inoue 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
87282cd038dSYoshinobu Inoue 	}
87382cd038dSYoshinobu Inoue 
87482cd038dSYoshinobu Inoue 	/*
87582cd038dSYoshinobu Inoue 	 * Forward if desirable.
87682cd038dSYoshinobu Inoue 	 */
87733cde130SBruce M Simpson 	if (V_ip6_mrouter &&
87833cde130SBruce M Simpson 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
87991ec0a1eSYoshinobu Inoue 		/*
88091ec0a1eSYoshinobu Inoue 		 * If we are acting as a multicast router, all
88191ec0a1eSYoshinobu Inoue 		 * incoming multicast packets are passed to the
88291ec0a1eSYoshinobu Inoue 		 * kernel-level multicast forwarding function.
88391ec0a1eSYoshinobu Inoue 		 * The packet is returned (relatively) intact; if
88491ec0a1eSYoshinobu Inoue 		 * ip6_mforward() returns a non-zero value, the packet
88591ec0a1eSYoshinobu Inoue 		 * must be discarded, else it may be accepted below.
88629dc7bc6SBruce M Simpson 		 *
88729dc7bc6SBruce M Simpson 		 * XXX TODO: Check hlim and multicast scope here to avoid
88829dc7bc6SBruce M Simpson 		 * unnecessarily calling into ip6_mforward().
88991ec0a1eSYoshinobu Inoue 		 */
8908a030e9cSAndrey V. Elsukov 		if (ip6_mforward && ip6_mforward(ip6, rcvif, m)) {
89133cde130SBruce M Simpson 			IP6STAT_INC(ip6s_cantforward);
89209f8c3ffSBjoern A. Zeeb 			goto bad;
89391ec0a1eSYoshinobu Inoue 		}
89482cd038dSYoshinobu Inoue 	} else if (!ours) {
8953c92002fSSam Leffler 		ip6_forward(m, srcrt);
8963e88eb90SAndrey V. Elsukov 		return;
89782cd038dSYoshinobu Inoue 	}
89882cd038dSYoshinobu Inoue 
89982cd038dSYoshinobu Inoue 	/*
9005ab15157SDoug Rabson 	 * We are going to ship the packet to the local protocol stack. Call the
9015ab15157SDoug Rabson 	 * filter again for this 'output' action, allowing redirect-like rules
9025ab15157SDoug Rabson 	 * to adjust the source address.
9035ab15157SDoug Rabson 	 */
9045ab15157SDoug Rabson 	if (PFIL_HOOKED_OUT(V_inet6_local_pfil_head)) {
9055ab15157SDoug Rabson 		if (pfil_mbuf_out(V_inet6_local_pfil_head, &m, V_loif, NULL) !=
9065ab15157SDoug Rabson 		    PFIL_PASS)
9075ab15157SDoug Rabson 			return;
9085ab15157SDoug Rabson 		ip6 = mtod(m, struct ip6_hdr *);
9095ab15157SDoug Rabson 	}
9105ab15157SDoug Rabson 
9115ab15157SDoug Rabson 	/*
91282cd038dSYoshinobu Inoue 	 * Tell launch routine the next header
91382cd038dSYoshinobu Inoue 	 */
9149cb8d207SAndrey V. Elsukov 	IP6STAT_INC(ip6s_delivered);
9158a030e9cSAndrey V. Elsukov 	in6_ifstat_inc(rcvif, ifs6_in_deliver);
91682cd038dSYoshinobu Inoue 	nest = 0;
91733841545SHajimu UMEMOTO 
91882cd038dSYoshinobu Inoue 	while (nxt != IPPROTO_DONE) {
919603724d3SBjoern A. Zeeb 		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
9209cb8d207SAndrey V. Elsukov 			IP6STAT_INC(ip6s_toomanyhdr);
92182cd038dSYoshinobu Inoue 			goto bad;
92282cd038dSYoshinobu Inoue 		}
92382cd038dSYoshinobu Inoue 
92482cd038dSYoshinobu Inoue 		/*
92582cd038dSYoshinobu Inoue 		 * protection against faulty packet - there should be
92682cd038dSYoshinobu Inoue 		 * more sanity checks in header chain processing.
92782cd038dSYoshinobu Inoue 		 */
92882cd038dSYoshinobu Inoue 		if (m->m_pkthdr.len < off) {
9299cb8d207SAndrey V. Elsukov 			IP6STAT_INC(ip6s_tooshort);
9308a030e9cSAndrey V. Elsukov 			in6_ifstat_inc(rcvif, ifs6_in_truncated);
93182cd038dSYoshinobu Inoue 			goto bad;
93282cd038dSYoshinobu Inoue 		}
93382cd038dSYoshinobu Inoue 
934fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
935fcf59617SAndrey V. Elsukov 		if (IPSEC_ENABLED(ipv6)) {
936fcf59617SAndrey V. Elsukov 			if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
937fcf59617SAndrey V. Elsukov 				return;
938fcf59617SAndrey V. Elsukov 		}
939b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
94029dc7bc6SBruce M Simpson 
94178b1fc05SGleb Smirnoff 		nxt = ip6_protox[nxt](&m, &off, nxt);
94282cd038dSYoshinobu Inoue 	}
9433e88eb90SAndrey V. Elsukov 	return;
94482cd038dSYoshinobu Inoue bad:
9458a030e9cSAndrey V. Elsukov 	in6_ifstat_inc(rcvif, ifs6_in_discard);
9468a030e9cSAndrey V. Elsukov 	if (m != NULL)
94782cd038dSYoshinobu Inoue 		m_freem(m);
94833841545SHajimu UMEMOTO }
94933841545SHajimu UMEMOTO 
95033841545SHajimu UMEMOTO /*
95182cd038dSYoshinobu Inoue  * Hop-by-Hop options header processing. If a valid jumbo payload option is
95282cd038dSYoshinobu Inoue  * included, the real payload length will be stored in plenp.
9531272577eSXin LI  *
9541272577eSXin LI  * rtalertp - XXX: should be stored more smart way
95582cd038dSYoshinobu Inoue  */
95682cd038dSYoshinobu Inoue static int
9571272577eSXin LI ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
9581272577eSXin LI     struct mbuf **mp, int *offp)
95982cd038dSYoshinobu Inoue {
96033841545SHajimu UMEMOTO 	struct mbuf *m = *mp;
96182cd038dSYoshinobu Inoue 	int off = *offp, hbhlen;
96282cd038dSYoshinobu Inoue 	struct ip6_hbh *hbh;
96382cd038dSYoshinobu Inoue 
96482cd038dSYoshinobu Inoue 	/* validation of the length of the header */
965a4adf6ccSBjoern A. Zeeb 	if (m->m_len < off + sizeof(*hbh)) {
966a61b5cfbSBjoern A. Zeeb 		m = m_pullup(m, off + sizeof(*hbh));
967a61b5cfbSBjoern A. Zeeb 		if (m == NULL) {
968a61b5cfbSBjoern A. Zeeb 			IP6STAT_INC(ip6s_exthdrtoolong);
969a61b5cfbSBjoern A. Zeeb 			*mp = NULL;
970a61b5cfbSBjoern A. Zeeb 			return (-1);
971a61b5cfbSBjoern A. Zeeb 		}
972a4adf6ccSBjoern A. Zeeb 	}
97382cd038dSYoshinobu Inoue 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
97482cd038dSYoshinobu Inoue 	hbhlen = (hbh->ip6h_len + 1) << 3;
97582cd038dSYoshinobu Inoue 
976a4adf6ccSBjoern A. Zeeb 	if (m->m_len < off + hbhlen) {
977a61b5cfbSBjoern A. Zeeb 		m = m_pullup(m, off + hbhlen);
978a61b5cfbSBjoern A. Zeeb 		if (m == NULL) {
979a61b5cfbSBjoern A. Zeeb 			IP6STAT_INC(ip6s_exthdrtoolong);
980a61b5cfbSBjoern A. Zeeb 			*mp = NULL;
981a61b5cfbSBjoern A. Zeeb 			return (-1);
982a61b5cfbSBjoern A. Zeeb 		}
983a4adf6ccSBjoern A. Zeeb 	}
98482cd038dSYoshinobu Inoue 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
98582cd038dSYoshinobu Inoue 	off += hbhlen;
98682cd038dSYoshinobu Inoue 	hbhlen -= sizeof(struct ip6_hbh);
98782cd038dSYoshinobu Inoue 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
988a8fe77d8SBjoern A. Zeeb 				hbhlen, rtalertp, plenp) < 0) {
989a8fe77d8SBjoern A. Zeeb 		*mp = NULL;
99082cd038dSYoshinobu Inoue 		return (-1);
991a8fe77d8SBjoern A. Zeeb 	}
99282cd038dSYoshinobu Inoue 
99382cd038dSYoshinobu Inoue 	*offp = off;
99482cd038dSYoshinobu Inoue 	*mp = m;
99582cd038dSYoshinobu Inoue 	return (0);
99682cd038dSYoshinobu Inoue }
99782cd038dSYoshinobu Inoue 
99882cd038dSYoshinobu Inoue /*
99982cd038dSYoshinobu Inoue  * Search header for all Hop-by-hop options and process each option.
100082cd038dSYoshinobu Inoue  * This function is separate from ip6_hopopts_input() in order to
100182cd038dSYoshinobu Inoue  * handle a case where the sending node itself process its hop-by-hop
100282cd038dSYoshinobu Inoue  * options header. In such a case, the function is called from ip6_output().
100333841545SHajimu UMEMOTO  *
100433841545SHajimu UMEMOTO  * The function assumes that hbh header is located right after the IPv6 header
100533841545SHajimu UMEMOTO  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
10066d79f3f6SRebecca Cran  * opthead + hbhlen is located in contiguous memory region.
100782cd038dSYoshinobu Inoue  */
100882cd038dSYoshinobu Inoue int
10091272577eSXin LI ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
10101272577eSXin LI     u_int32_t *rtalertp, u_int32_t *plenp)
101182cd038dSYoshinobu Inoue {
101282cd038dSYoshinobu Inoue 	struct ip6_hdr *ip6;
101382cd038dSYoshinobu Inoue 	int optlen = 0;
101482cd038dSYoshinobu Inoue 	u_int8_t *opt = opthead;
101582cd038dSYoshinobu Inoue 	u_int16_t rtalert_val;
1016686cdd19SJun-ichiro itojun Hagino 	u_int32_t jumboplen;
101733841545SHajimu UMEMOTO 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
101882cd038dSYoshinobu Inoue 
101982cd038dSYoshinobu Inoue 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
102082cd038dSYoshinobu Inoue 		switch (*opt) {
102182cd038dSYoshinobu Inoue 		case IP6OPT_PAD1:
102282cd038dSYoshinobu Inoue 			optlen = 1;
102382cd038dSYoshinobu Inoue 			break;
102482cd038dSYoshinobu Inoue 		case IP6OPT_PADN:
102582cd038dSYoshinobu Inoue 			if (hbhlen < IP6OPT_MINLEN) {
10269cb8d207SAndrey V. Elsukov 				IP6STAT_INC(ip6s_toosmall);
102782cd038dSYoshinobu Inoue 				goto bad;
102882cd038dSYoshinobu Inoue 			}
102982cd038dSYoshinobu Inoue 			optlen = *(opt + 1) + 2;
103082cd038dSYoshinobu Inoue 			break;
1031f95d4633SHajimu UMEMOTO 		case IP6OPT_ROUTER_ALERT:
103282cd038dSYoshinobu Inoue 			/* XXX may need check for alignment */
103382cd038dSYoshinobu Inoue 			if (hbhlen < IP6OPT_RTALERT_LEN) {
10349cb8d207SAndrey V. Elsukov 				IP6STAT_INC(ip6s_toosmall);
103582cd038dSYoshinobu Inoue 				goto bad;
103682cd038dSYoshinobu Inoue 			}
103733841545SHajimu UMEMOTO 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
103833841545SHajimu UMEMOTO 				/* XXX stat */
103933841545SHajimu UMEMOTO 				icmp6_error(m, ICMP6_PARAM_PROB,
104033841545SHajimu UMEMOTO 				    ICMP6_PARAMPROB_HEADER,
104133841545SHajimu UMEMOTO 				    erroff + opt + 1 - opthead);
104233841545SHajimu UMEMOTO 				return (-1);
104333841545SHajimu UMEMOTO 			}
104482cd038dSYoshinobu Inoue 			optlen = IP6OPT_RTALERT_LEN;
104582cd038dSYoshinobu Inoue 			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
104682cd038dSYoshinobu Inoue 			*rtalertp = ntohs(rtalert_val);
104782cd038dSYoshinobu Inoue 			break;
104882cd038dSYoshinobu Inoue 		case IP6OPT_JUMBO:
104982cd038dSYoshinobu Inoue 			/* XXX may need check for alignment */
105082cd038dSYoshinobu Inoue 			if (hbhlen < IP6OPT_JUMBO_LEN) {
10519cb8d207SAndrey V. Elsukov 				IP6STAT_INC(ip6s_toosmall);
105282cd038dSYoshinobu Inoue 				goto bad;
105382cd038dSYoshinobu Inoue 			}
105433841545SHajimu UMEMOTO 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
105533841545SHajimu UMEMOTO 				/* XXX stat */
105633841545SHajimu UMEMOTO 				icmp6_error(m, ICMP6_PARAM_PROB,
105733841545SHajimu UMEMOTO 				    ICMP6_PARAMPROB_HEADER,
105833841545SHajimu UMEMOTO 				    erroff + opt + 1 - opthead);
105933841545SHajimu UMEMOTO 				return (-1);
106033841545SHajimu UMEMOTO 			}
106182cd038dSYoshinobu Inoue 			optlen = IP6OPT_JUMBO_LEN;
106282cd038dSYoshinobu Inoue 
106382cd038dSYoshinobu Inoue 			/*
106482cd038dSYoshinobu Inoue 			 * IPv6 packets that have non 0 payload length
106533841545SHajimu UMEMOTO 			 * must not contain a jumbo payload option.
106682cd038dSYoshinobu Inoue 			 */
1067686cdd19SJun-ichiro itojun Hagino 			ip6 = mtod(m, struct ip6_hdr *);
1068686cdd19SJun-ichiro itojun Hagino 			if (ip6->ip6_plen) {
10699cb8d207SAndrey V. Elsukov 				IP6STAT_INC(ip6s_badoptions);
107082cd038dSYoshinobu Inoue 				icmp6_error(m, ICMP6_PARAM_PROB,
107182cd038dSYoshinobu Inoue 				    ICMP6_PARAMPROB_HEADER,
107233841545SHajimu UMEMOTO 				    erroff + opt - opthead);
107382cd038dSYoshinobu Inoue 				return (-1);
107482cd038dSYoshinobu Inoue 			}
1075686cdd19SJun-ichiro itojun Hagino 
1076686cdd19SJun-ichiro itojun Hagino 			/*
1077686cdd19SJun-ichiro itojun Hagino 			 * We may see jumbolen in unaligned location, so
1078686cdd19SJun-ichiro itojun Hagino 			 * we'd need to perform bcopy().
1079686cdd19SJun-ichiro itojun Hagino 			 */
1080686cdd19SJun-ichiro itojun Hagino 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1081686cdd19SJun-ichiro itojun Hagino 			jumboplen = (u_int32_t)htonl(jumboplen);
1082686cdd19SJun-ichiro itojun Hagino 
1083686cdd19SJun-ichiro itojun Hagino #if 1
1084686cdd19SJun-ichiro itojun Hagino 			/*
1085686cdd19SJun-ichiro itojun Hagino 			 * if there are multiple jumbo payload options,
1086686cdd19SJun-ichiro itojun Hagino 			 * *plenp will be non-zero and the packet will be
1087686cdd19SJun-ichiro itojun Hagino 			 * rejected.
1088686cdd19SJun-ichiro itojun Hagino 			 * the behavior may need some debate in ipngwg -
1089686cdd19SJun-ichiro itojun Hagino 			 * multiple options does not make sense, however,
1090686cdd19SJun-ichiro itojun Hagino 			 * there's no explicit mention in specification.
1091686cdd19SJun-ichiro itojun Hagino 			 */
1092686cdd19SJun-ichiro itojun Hagino 			if (*plenp != 0) {
10939cb8d207SAndrey V. Elsukov 				IP6STAT_INC(ip6s_badoptions);
1094686cdd19SJun-ichiro itojun Hagino 				icmp6_error(m, ICMP6_PARAM_PROB,
1095686cdd19SJun-ichiro itojun Hagino 				    ICMP6_PARAMPROB_HEADER,
109633841545SHajimu UMEMOTO 				    erroff + opt + 2 - opthead);
1097686cdd19SJun-ichiro itojun Hagino 				return (-1);
1098686cdd19SJun-ichiro itojun Hagino 			}
1099686cdd19SJun-ichiro itojun Hagino #endif
1100686cdd19SJun-ichiro itojun Hagino 
1101686cdd19SJun-ichiro itojun Hagino 			/*
1102686cdd19SJun-ichiro itojun Hagino 			 * jumbo payload length must be larger than 65535.
1103686cdd19SJun-ichiro itojun Hagino 			 */
1104686cdd19SJun-ichiro itojun Hagino 			if (jumboplen <= IPV6_MAXPACKET) {
11059cb8d207SAndrey V. Elsukov 				IP6STAT_INC(ip6s_badoptions);
1106686cdd19SJun-ichiro itojun Hagino 				icmp6_error(m, ICMP6_PARAM_PROB,
1107686cdd19SJun-ichiro itojun Hagino 				    ICMP6_PARAMPROB_HEADER,
110833841545SHajimu UMEMOTO 				    erroff + opt + 2 - opthead);
1109686cdd19SJun-ichiro itojun Hagino 				return (-1);
1110686cdd19SJun-ichiro itojun Hagino 			}
1111686cdd19SJun-ichiro itojun Hagino 			*plenp = jumboplen;
1112686cdd19SJun-ichiro itojun Hagino 
111382cd038dSYoshinobu Inoue 			break;
111482cd038dSYoshinobu Inoue 		default:		/* unknown option */
111582cd038dSYoshinobu Inoue 			if (hbhlen < IP6OPT_MINLEN) {
11169cb8d207SAndrey V. Elsukov 				IP6STAT_INC(ip6s_toosmall);
111782cd038dSYoshinobu Inoue 				goto bad;
111882cd038dSYoshinobu Inoue 			}
111933841545SHajimu UMEMOTO 			optlen = ip6_unknown_opt(opt, m,
112033841545SHajimu UMEMOTO 			    erroff + opt - opthead);
112133841545SHajimu UMEMOTO 			if (optlen == -1)
112282cd038dSYoshinobu Inoue 				return (-1);
112382cd038dSYoshinobu Inoue 			optlen += 2;
112482cd038dSYoshinobu Inoue 			break;
112582cd038dSYoshinobu Inoue 		}
112682cd038dSYoshinobu Inoue 	}
112782cd038dSYoshinobu Inoue 
112882cd038dSYoshinobu Inoue 	return (0);
112982cd038dSYoshinobu Inoue 
113082cd038dSYoshinobu Inoue   bad:
113182cd038dSYoshinobu Inoue 	m_freem(m);
113282cd038dSYoshinobu Inoue 	return (-1);
113382cd038dSYoshinobu Inoue }
113482cd038dSYoshinobu Inoue 
113582cd038dSYoshinobu Inoue /*
113682cd038dSYoshinobu Inoue  * Unknown option processing.
113782cd038dSYoshinobu Inoue  * The third argument `off' is the offset from the IPv6 header to the option,
113882cd038dSYoshinobu Inoue  * which is necessary if the IPv6 header the and option header and IPv6 header
11396d79f3f6SRebecca Cran  * is not contiguous in order to return an ICMPv6 error.
114082cd038dSYoshinobu Inoue  */
114182cd038dSYoshinobu Inoue int
11421272577eSXin LI ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
114382cd038dSYoshinobu Inoue {
114482cd038dSYoshinobu Inoue 	struct ip6_hdr *ip6;
114582cd038dSYoshinobu Inoue 
114682cd038dSYoshinobu Inoue 	switch (IP6OPT_TYPE(*optp)) {
114782cd038dSYoshinobu Inoue 	case IP6OPT_TYPE_SKIP: /* ignore the option */
114882cd038dSYoshinobu Inoue 		return ((int)*(optp + 1));
114982cd038dSYoshinobu Inoue 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
115082cd038dSYoshinobu Inoue 		m_freem(m);
115182cd038dSYoshinobu Inoue 		return (-1);
115282cd038dSYoshinobu Inoue 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
11539cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badoptions);
115482cd038dSYoshinobu Inoue 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
115582cd038dSYoshinobu Inoue 		return (-1);
115682cd038dSYoshinobu Inoue 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
11579cb8d207SAndrey V. Elsukov 		IP6STAT_INC(ip6s_badoptions);
115882cd038dSYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
115982cd038dSYoshinobu Inoue 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
116082cd038dSYoshinobu Inoue 		    (m->m_flags & (M_BCAST|M_MCAST)))
116182cd038dSYoshinobu Inoue 			m_freem(m);
116282cd038dSYoshinobu Inoue 		else
116382cd038dSYoshinobu Inoue 			icmp6_error(m, ICMP6_PARAM_PROB,
116482cd038dSYoshinobu Inoue 				    ICMP6_PARAMPROB_OPTION, off);
116582cd038dSYoshinobu Inoue 		return (-1);
116682cd038dSYoshinobu Inoue 	}
116782cd038dSYoshinobu Inoue 
116882cd038dSYoshinobu Inoue 	m_freem(m);		/* XXX: NOTREACHED */
116982cd038dSYoshinobu Inoue 	return (-1);
117082cd038dSYoshinobu Inoue }
117182cd038dSYoshinobu Inoue 
117282cd038dSYoshinobu Inoue /*
1173686cdd19SJun-ichiro itojun Hagino  * Create the "control" list for this pcb.
11749a38ba81SBjoern A. Zeeb  * These functions will not modify mbuf chain at all.
1175686cdd19SJun-ichiro itojun Hagino  *
1176686cdd19SJun-ichiro itojun Hagino  * The routine will be called from upper layer handlers like tcp6_input().
1177686cdd19SJun-ichiro itojun Hagino  * Thus the routine assumes that the caller (tcp6_input) have already
1178a61b5cfbSBjoern A. Zeeb  * called m_pullup() and all the extension headers are located in the
1179686cdd19SJun-ichiro itojun Hagino  * very first mbuf on the mbuf chain.
11809a38ba81SBjoern A. Zeeb  *
11819a38ba81SBjoern A. Zeeb  * ip6_savecontrol_v4 will handle those options that are possible to be
11829a38ba81SBjoern A. Zeeb  * set on a v4-mapped socket.
11839a38ba81SBjoern A. Zeeb  * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
11849a38ba81SBjoern A. Zeeb  * options and handle the v6-only ones itself.
118582cd038dSYoshinobu Inoue  */
118648d48eb9SBjoern A. Zeeb struct mbuf **
118748d48eb9SBjoern A. Zeeb ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
118848d48eb9SBjoern A. Zeeb     int *v4only)
118982cd038dSYoshinobu Inoue {
119011de19f4SHajimu UMEMOTO 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
119133841545SHajimu UMEMOTO 
119233841545SHajimu UMEMOTO #ifdef SO_TIMESTAMP
11939a38ba81SBjoern A. Zeeb 	if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1194339efd75SMaxim Sobolev 		union {
119582cd038dSYoshinobu Inoue 			struct timeval tv;
1196339efd75SMaxim Sobolev 			struct bintime bt;
1197339efd75SMaxim Sobolev 			struct timespec ts;
1198339efd75SMaxim Sobolev 		} t;
119906193f0bSKonstantin Belousov 		struct bintime boottimebin, bt1;
120006193f0bSKonstantin Belousov 		struct timespec ts1;
120106193f0bSKonstantin Belousov 		bool stamped;
120282cd038dSYoshinobu Inoue 
120306193f0bSKonstantin Belousov 		stamped = false;
1204339efd75SMaxim Sobolev 		switch (inp->inp_socket->so_ts_clock) {
1205339efd75SMaxim Sobolev 		case SO_TS_REALTIME_MICRO:
120606193f0bSKonstantin Belousov 			if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
120706193f0bSKonstantin Belousov 			    M_TSTMP)) {
120806193f0bSKonstantin Belousov 				mbuf_tstmp2timespec(m, &ts1);
120906193f0bSKonstantin Belousov 				timespec2bintime(&ts1, &bt1);
121006193f0bSKonstantin Belousov 				getboottimebin(&boottimebin);
121106193f0bSKonstantin Belousov 				bintime_add(&bt1, &boottimebin);
121206193f0bSKonstantin Belousov 				bintime2timeval(&bt1, &t.tv);
121306193f0bSKonstantin Belousov 			} else {
1214339efd75SMaxim Sobolev 				microtime(&t.tv);
121506193f0bSKonstantin Belousov 			}
1216b46667c6SGleb Smirnoff 			*mp = sbcreatecontrol(&t.tv, sizeof(t.tv),
1217b46667c6SGleb Smirnoff 			    SCM_TIMESTAMP, SOL_SOCKET, M_NOWAIT);
121806193f0bSKonstantin Belousov 			if (*mp != NULL) {
121982cd038dSYoshinobu Inoue 				mp = &(*mp)->m_next;
122006193f0bSKonstantin Belousov 				stamped = true;
122106193f0bSKonstantin Belousov 			}
1222339efd75SMaxim Sobolev 			break;
1223339efd75SMaxim Sobolev 
1224339efd75SMaxim Sobolev 		case SO_TS_BINTIME:
122506193f0bSKonstantin Belousov 			if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
122606193f0bSKonstantin Belousov 			    M_TSTMP)) {
122706193f0bSKonstantin Belousov 				mbuf_tstmp2timespec(m, &ts1);
122806193f0bSKonstantin Belousov 				timespec2bintime(&ts1, &t.bt);
122906193f0bSKonstantin Belousov 				getboottimebin(&boottimebin);
123006193f0bSKonstantin Belousov 				bintime_add(&t.bt, &boottimebin);
123106193f0bSKonstantin Belousov 			} else {
1232339efd75SMaxim Sobolev 				bintime(&t.bt);
123306193f0bSKonstantin Belousov 			}
1234b46667c6SGleb Smirnoff 			*mp = sbcreatecontrol(&t.bt, sizeof(t.bt), SCM_BINTIME,
1235b46667c6SGleb Smirnoff 			    SOL_SOCKET, M_NOWAIT);
123606193f0bSKonstantin Belousov 			if (*mp != NULL) {
1237339efd75SMaxim Sobolev 				mp = &(*mp)->m_next;
123806193f0bSKonstantin Belousov 				stamped = true;
123906193f0bSKonstantin Belousov 			}
1240339efd75SMaxim Sobolev 			break;
1241339efd75SMaxim Sobolev 
1242339efd75SMaxim Sobolev 		case SO_TS_REALTIME:
124306193f0bSKonstantin Belousov 			if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
124406193f0bSKonstantin Belousov 			    M_TSTMP)) {
124506193f0bSKonstantin Belousov 				mbuf_tstmp2timespec(m, &t.ts);
124606193f0bSKonstantin Belousov 				getboottimebin(&boottimebin);
124706193f0bSKonstantin Belousov 				bintime2timespec(&boottimebin, &ts1);
12486040822cSAlan Somers 				timespecadd(&t.ts, &ts1, &t.ts);
124906193f0bSKonstantin Belousov 			} else {
1250339efd75SMaxim Sobolev 				nanotime(&t.ts);
125106193f0bSKonstantin Belousov 			}
1252b46667c6SGleb Smirnoff 			*mp = sbcreatecontrol(&t.ts, sizeof(t.ts),
1253b46667c6SGleb Smirnoff 			    SCM_REALTIME, SOL_SOCKET, M_NOWAIT);
125406193f0bSKonstantin Belousov 			if (*mp != NULL) {
1255339efd75SMaxim Sobolev 				mp = &(*mp)->m_next;
125606193f0bSKonstantin Belousov 				stamped = true;
125706193f0bSKonstantin Belousov 			}
1258339efd75SMaxim Sobolev 			break;
1259339efd75SMaxim Sobolev 
1260339efd75SMaxim Sobolev 		case SO_TS_MONOTONIC:
126106193f0bSKonstantin Belousov 			if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
126206193f0bSKonstantin Belousov 			    M_TSTMP))
126306193f0bSKonstantin Belousov 				mbuf_tstmp2timespec(m, &t.ts);
126406193f0bSKonstantin Belousov 			else
1265339efd75SMaxim Sobolev 				nanouptime(&t.ts);
1266b46667c6SGleb Smirnoff 			*mp = sbcreatecontrol(&t.ts, sizeof(t.ts),
1267b46667c6SGleb Smirnoff 			    SCM_MONOTONIC, SOL_SOCKET, M_NOWAIT);
126806193f0bSKonstantin Belousov 			if (*mp != NULL) {
1269339efd75SMaxim Sobolev 				mp = &(*mp)->m_next;
127006193f0bSKonstantin Belousov 				stamped = true;
127106193f0bSKonstantin Belousov 			}
1272339efd75SMaxim Sobolev 			break;
1273339efd75SMaxim Sobolev 
1274339efd75SMaxim Sobolev 		default:
1275339efd75SMaxim Sobolev 			panic("unknown (corrupted) so_ts_clock");
1276339efd75SMaxim Sobolev 		}
127706193f0bSKonstantin Belousov 		if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) ==
127806193f0bSKonstantin Belousov 		    (M_PKTHDR | M_TSTMP)) {
127906193f0bSKonstantin Belousov 			struct sock_timestamp_info sti;
128006193f0bSKonstantin Belousov 
128106193f0bSKonstantin Belousov 			bzero(&sti, sizeof(sti));
128206193f0bSKonstantin Belousov 			sti.st_info_flags = ST_INFO_HW;
128306193f0bSKonstantin Belousov 			if ((m->m_flags & M_TSTMP_HPREC) != 0)
128406193f0bSKonstantin Belousov 				sti.st_info_flags |= ST_INFO_HW_HPREC;
1285b46667c6SGleb Smirnoff 			*mp = sbcreatecontrol(&sti, sizeof(sti), SCM_TIME_INFO,
1286b46667c6SGleb Smirnoff 			    SOL_SOCKET, M_NOWAIT);
128706193f0bSKonstantin Belousov 			if (*mp != NULL)
128806193f0bSKonstantin Belousov 				mp = &(*mp)->m_next;
128906193f0bSKonstantin Belousov 		}
129082cd038dSYoshinobu Inoue 	}
1291686cdd19SJun-ichiro itojun Hagino #endif
129282cd038dSYoshinobu Inoue 
12939a38ba81SBjoern A. Zeeb #define IS2292(inp, x, y)	(((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
129482cd038dSYoshinobu Inoue 	/* RFC 2292 sec. 5 */
12959a38ba81SBjoern A. Zeeb 	if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
129682cd038dSYoshinobu Inoue 		struct in6_pktinfo pi6;
12977efe5d92SHajimu UMEMOTO 
1298b36dcb9aSMichael Tuexen 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1299b36dcb9aSMichael Tuexen #ifdef INET
1300b36dcb9aSMichael Tuexen 			struct ip *ip;
1301b36dcb9aSMichael Tuexen 
1302b36dcb9aSMichael Tuexen 			ip = mtod(m, struct ip *);
1303b36dcb9aSMichael Tuexen 			pi6.ipi6_addr.s6_addr32[0] = 0;
1304b36dcb9aSMichael Tuexen 			pi6.ipi6_addr.s6_addr32[1] = 0;
1305b36dcb9aSMichael Tuexen 			pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1306b36dcb9aSMichael Tuexen 			pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1307b36dcb9aSMichael Tuexen #else
1308b36dcb9aSMichael Tuexen 			/* We won't hit this code */
1309b36dcb9aSMichael Tuexen 			bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1310b36dcb9aSMichael Tuexen #endif
1311b36dcb9aSMichael Tuexen 		} else {
131282cd038dSYoshinobu Inoue 			bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
13139a4f9608SHajimu UMEMOTO 			in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1314b36dcb9aSMichael Tuexen 		}
13157efe5d92SHajimu UMEMOTO 		pi6.ipi6_ifindex =
13167efe5d92SHajimu UMEMOTO 		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
13177efe5d92SHajimu UMEMOTO 
1318b46667c6SGleb Smirnoff 		*mp = sbcreatecontrol(&pi6, sizeof(struct in6_pktinfo),
1319b46667c6SGleb Smirnoff 		    IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6,
1320b46667c6SGleb Smirnoff 		    M_NOWAIT);
1321f95d4633SHajimu UMEMOTO 		if (*mp)
132282cd038dSYoshinobu Inoue 			mp = &(*mp)->m_next;
132382cd038dSYoshinobu Inoue 	}
132433841545SHajimu UMEMOTO 
13259a38ba81SBjoern A. Zeeb 	if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1326b36dcb9aSMichael Tuexen 		int hlim;
13277efe5d92SHajimu UMEMOTO 
1328b36dcb9aSMichael Tuexen 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1329b36dcb9aSMichael Tuexen #ifdef INET
1330b36dcb9aSMichael Tuexen 			struct ip *ip;
1331b36dcb9aSMichael Tuexen 
1332b36dcb9aSMichael Tuexen 			ip = mtod(m, struct ip *);
1333b36dcb9aSMichael Tuexen 			hlim = ip->ip_ttl;
1334b36dcb9aSMichael Tuexen #else
1335b36dcb9aSMichael Tuexen 			/* We won't hit this code */
1336b36dcb9aSMichael Tuexen 			hlim = 0;
1337b36dcb9aSMichael Tuexen #endif
1338b36dcb9aSMichael Tuexen 		} else {
1339b36dcb9aSMichael Tuexen 			hlim = ip6->ip6_hlim & 0xff;
1340b36dcb9aSMichael Tuexen 		}
1341b46667c6SGleb Smirnoff 		*mp = sbcreatecontrol(&hlim, sizeof(int),
13429a38ba81SBjoern A. Zeeb 		    IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1343b46667c6SGleb Smirnoff 		    IPPROTO_IPV6, M_NOWAIT);
1344f95d4633SHajimu UMEMOTO 		if (*mp)
134582cd038dSYoshinobu Inoue 			mp = &(*mp)->m_next;
134682cd038dSYoshinobu Inoue 	}
1347f95d4633SHajimu UMEMOTO 
1348b36dcb9aSMichael Tuexen 	if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1349b36dcb9aSMichael Tuexen 		int tclass;
1350b36dcb9aSMichael Tuexen 
1351b36dcb9aSMichael Tuexen 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1352b36dcb9aSMichael Tuexen #ifdef INET
1353b36dcb9aSMichael Tuexen 			struct ip *ip;
1354b36dcb9aSMichael Tuexen 
1355b36dcb9aSMichael Tuexen 			ip = mtod(m, struct ip *);
1356b36dcb9aSMichael Tuexen 			tclass = ip->ip_tos;
1357b36dcb9aSMichael Tuexen #else
1358b36dcb9aSMichael Tuexen 			/* We won't hit this code */
1359b36dcb9aSMichael Tuexen 			tclass = 0;
1360b36dcb9aSMichael Tuexen #endif
1361b36dcb9aSMichael Tuexen 		} else {
1362b36dcb9aSMichael Tuexen 			u_int32_t flowinfo;
1363b36dcb9aSMichael Tuexen 
1364b36dcb9aSMichael Tuexen 			flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1365b36dcb9aSMichael Tuexen 			flowinfo >>= 20;
1366b36dcb9aSMichael Tuexen 			tclass = flowinfo & 0xff;
1367b36dcb9aSMichael Tuexen 		}
1368b46667c6SGleb Smirnoff 		*mp = sbcreatecontrol(&tclass, sizeof(int), IPV6_TCLASS,
1369b46667c6SGleb Smirnoff 		    IPPROTO_IPV6, M_NOWAIT);
1370b36dcb9aSMichael Tuexen 		if (*mp)
1371b36dcb9aSMichael Tuexen 			mp = &(*mp)->m_next;
1372b36dcb9aSMichael Tuexen 	}
1373b36dcb9aSMichael Tuexen 
1374b36dcb9aSMichael Tuexen 	if (v4only != NULL) {
1375b36dcb9aSMichael Tuexen 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1376b36dcb9aSMichael Tuexen 			*v4only = 1;
1377b36dcb9aSMichael Tuexen 		} else {
137848d48eb9SBjoern A. Zeeb 			*v4only = 0;
1379b36dcb9aSMichael Tuexen 		}
1380b36dcb9aSMichael Tuexen 	}
1381b36dcb9aSMichael Tuexen 
138248d48eb9SBjoern A. Zeeb 	return (mp);
13839a38ba81SBjoern A. Zeeb }
13849a38ba81SBjoern A. Zeeb 
13859a38ba81SBjoern A. Zeeb void
13860ecd976eSBjoern A. Zeeb ip6_savecontrol(struct inpcb *inp, struct mbuf *m, struct mbuf **mp)
13879a38ba81SBjoern A. Zeeb {
1388503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
138948d48eb9SBjoern A. Zeeb 	int v4only = 0;
13909a38ba81SBjoern A. Zeeb 
13910ecd976eSBjoern A. Zeeb 	mp = ip6_savecontrol_v4(inp, m, mp, &v4only);
139248d48eb9SBjoern A. Zeeb 	if (v4only)
13939a38ba81SBjoern A. Zeeb 		return;
13949a38ba81SBjoern A. Zeeb 
1395503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
139682cd038dSYoshinobu Inoue 	/*
13973c751c1bSHajimu UMEMOTO 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
13983c751c1bSHajimu UMEMOTO 	 * privilege for the option (see ip6_ctloutput), but it might be too
13993c751c1bSHajimu UMEMOTO 	 * strict, since there might be some hop-by-hop options which can be
14003c751c1bSHajimu UMEMOTO 	 * returned to normal user.
14013c751c1bSHajimu UMEMOTO 	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
140282cd038dSYoshinobu Inoue 	 */
14030ecd976eSBjoern A. Zeeb 	if ((inp->inp_flags & IN6P_HOPOPTS) != 0) {
140482cd038dSYoshinobu Inoue 		/*
140582cd038dSYoshinobu Inoue 		 * Check if a hop-by-hop options header is contatined in the
140682cd038dSYoshinobu Inoue 		 * received packet, and if so, store the options as ancillary
140782cd038dSYoshinobu Inoue 		 * data. Note that a hop-by-hop options header must be
14087efe5d92SHajimu UMEMOTO 		 * just after the IPv6 header, which is assured through the
14097efe5d92SHajimu UMEMOTO 		 * IPv6 input processing.
141082cd038dSYoshinobu Inoue 		 */
141182cd038dSYoshinobu Inoue 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1412686cdd19SJun-ichiro itojun Hagino 			struct ip6_hbh *hbh;
14136890b588SGleb Smirnoff 			u_int hbhlen;
1414686cdd19SJun-ichiro itojun Hagino 
1415686cdd19SJun-ichiro itojun Hagino 			hbh = (struct ip6_hbh *)(ip6 + 1);
1416686cdd19SJun-ichiro itojun Hagino 			hbhlen = (hbh->ip6h_len + 1) << 3;
141782cd038dSYoshinobu Inoue 
141882cd038dSYoshinobu Inoue 			/*
14197efe5d92SHajimu UMEMOTO 			 * XXX: We copy the whole header even if a
14207efe5d92SHajimu UMEMOTO 			 * jumbo payload option is included, the option which
14217efe5d92SHajimu UMEMOTO 			 * is to be removed before returning according to
14227efe5d92SHajimu UMEMOTO 			 * RFC2292.
142318b35df8SHajimu UMEMOTO 			 * Note: this constraint is removed in RFC3542
142482cd038dSYoshinobu Inoue 			 */
1425b46667c6SGleb Smirnoff 			*mp = sbcreatecontrol(hbh, hbhlen,
14260ecd976eSBjoern A. Zeeb 			    IS2292(inp, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1427b46667c6SGleb Smirnoff 			    IPPROTO_IPV6, M_NOWAIT);
1428f95d4633SHajimu UMEMOTO 			if (*mp)
142982cd038dSYoshinobu Inoue 				mp = &(*mp)->m_next;
143082cd038dSYoshinobu Inoue 		}
143182cd038dSYoshinobu Inoue 	}
143282cd038dSYoshinobu Inoue 
14330ecd976eSBjoern A. Zeeb 	if ((inp->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
143433841545SHajimu UMEMOTO 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
143582cd038dSYoshinobu Inoue 
143682cd038dSYoshinobu Inoue 		/*
143782cd038dSYoshinobu Inoue 		 * Search for destination options headers or routing
143882cd038dSYoshinobu Inoue 		 * header(s) through the header chain, and stores each
143982cd038dSYoshinobu Inoue 		 * header as ancillary data.
144082cd038dSYoshinobu Inoue 		 * Note that the order of the headers remains in
144182cd038dSYoshinobu Inoue 		 * the chain of ancillary data.
144282cd038dSYoshinobu Inoue 		 */
144382cd038dSYoshinobu Inoue 		while (1) {	/* is explicit loop prevention necessary? */
144433841545SHajimu UMEMOTO 			struct ip6_ext *ip6e = NULL;
14456890b588SGleb Smirnoff 			u_int elen;
144633841545SHajimu UMEMOTO 
144733841545SHajimu UMEMOTO 			/*
144833841545SHajimu UMEMOTO 			 * if it is not an extension header, don't try to
144933841545SHajimu UMEMOTO 			 * pull it from the chain.
145033841545SHajimu UMEMOTO 			 */
145133841545SHajimu UMEMOTO 			switch (nxt) {
145233841545SHajimu UMEMOTO 			case IPPROTO_DSTOPTS:
145333841545SHajimu UMEMOTO 			case IPPROTO_ROUTING:
145433841545SHajimu UMEMOTO 			case IPPROTO_HOPOPTS:
145533841545SHajimu UMEMOTO 			case IPPROTO_AH: /* is it possible? */
145633841545SHajimu UMEMOTO 				break;
145733841545SHajimu UMEMOTO 			default:
145833841545SHajimu UMEMOTO 				goto loopend;
145933841545SHajimu UMEMOTO 			}
1460686cdd19SJun-ichiro itojun Hagino 
146133841545SHajimu UMEMOTO 			if (off + sizeof(*ip6e) > m->m_len)
146233841545SHajimu UMEMOTO 				goto loopend;
1463686cdd19SJun-ichiro itojun Hagino 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1464686cdd19SJun-ichiro itojun Hagino 			if (nxt == IPPROTO_AH)
1465686cdd19SJun-ichiro itojun Hagino 				elen = (ip6e->ip6e_len + 2) << 2;
1466686cdd19SJun-ichiro itojun Hagino 			else
1467686cdd19SJun-ichiro itojun Hagino 				elen = (ip6e->ip6e_len + 1) << 3;
146833841545SHajimu UMEMOTO 			if (off + elen > m->m_len)
146933841545SHajimu UMEMOTO 				goto loopend;
147082cd038dSYoshinobu Inoue 
147182cd038dSYoshinobu Inoue 			switch (nxt) {
147282cd038dSYoshinobu Inoue 			case IPPROTO_DSTOPTS:
14730ecd976eSBjoern A. Zeeb 				if (!(inp->inp_flags & IN6P_DSTOPTS))
147482cd038dSYoshinobu Inoue 					break;
147582cd038dSYoshinobu Inoue 
1476b46667c6SGleb Smirnoff 				*mp = sbcreatecontrol(ip6e, elen,
1477b46667c6SGleb Smirnoff 				    IS2292(inp, IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1478b46667c6SGleb Smirnoff 				    IPPROTO_IPV6, M_NOWAIT);
147982cd038dSYoshinobu Inoue 				if (*mp)
148082cd038dSYoshinobu Inoue 					mp = &(*mp)->m_next;
148182cd038dSYoshinobu Inoue 				break;
148282cd038dSYoshinobu Inoue 			case IPPROTO_ROUTING:
14830ecd976eSBjoern A. Zeeb 				if (!(inp->inp_flags & IN6P_RTHDR))
148482cd038dSYoshinobu Inoue 					break;
148582cd038dSYoshinobu Inoue 
1486b46667c6SGleb Smirnoff 				*mp = sbcreatecontrol(ip6e, elen,
14870ecd976eSBjoern A. Zeeb 				    IS2292(inp, IPV6_2292RTHDR, IPV6_RTHDR),
1488b46667c6SGleb Smirnoff 				    IPPROTO_IPV6, M_NOWAIT);
148982cd038dSYoshinobu Inoue 				if (*mp)
149082cd038dSYoshinobu Inoue 					mp = &(*mp)->m_next;
149182cd038dSYoshinobu Inoue 				break;
149282cd038dSYoshinobu Inoue 			case IPPROTO_HOPOPTS:
149382cd038dSYoshinobu Inoue 			case IPPROTO_AH: /* is it possible? */
149482cd038dSYoshinobu Inoue 				break;
149533841545SHajimu UMEMOTO 
149633841545SHajimu UMEMOTO 			default:
149733841545SHajimu UMEMOTO 				/*
149833841545SHajimu UMEMOTO 				 * other cases have been filtered in the above.
149933841545SHajimu UMEMOTO 				 * none will visit this case.  here we supply
150033841545SHajimu UMEMOTO 				 * the code just in case (nxt overwritten or
150133841545SHajimu UMEMOTO 				 * other cases).
150233841545SHajimu UMEMOTO 				 */
150333841545SHajimu UMEMOTO 				goto loopend;
150482cd038dSYoshinobu Inoue 			}
150582cd038dSYoshinobu Inoue 
150682cd038dSYoshinobu Inoue 			/* proceed with the next header. */
1507686cdd19SJun-ichiro itojun Hagino 			off += elen;
150882cd038dSYoshinobu Inoue 			nxt = ip6e->ip6e_nxt;
150933841545SHajimu UMEMOTO 			ip6e = NULL;
151082cd038dSYoshinobu Inoue 		}
151182cd038dSYoshinobu Inoue 	  loopend:
151233841545SHajimu UMEMOTO 		;
151382cd038dSYoshinobu Inoue 	}
151468bb8d62SAdrian Chadd 
15150ecd976eSBjoern A. Zeeb 	if (inp->inp_flags2 & INP_RECVFLOWID) {
151668bb8d62SAdrian Chadd 		uint32_t flowid, flow_type;
151768bb8d62SAdrian Chadd 
151868bb8d62SAdrian Chadd 		flowid = m->m_pkthdr.flowid;
151968bb8d62SAdrian Chadd 		flow_type = M_HASHTYPE_GET(m);
152068bb8d62SAdrian Chadd 
152168bb8d62SAdrian Chadd 		/*
152268bb8d62SAdrian Chadd 		 * XXX should handle the failure of one or the
152368bb8d62SAdrian Chadd 		 * other - don't populate both?
152468bb8d62SAdrian Chadd 		 */
1525b46667c6SGleb Smirnoff 		*mp = sbcreatecontrol(&flowid, sizeof(uint32_t), IPV6_FLOWID,
1526b46667c6SGleb Smirnoff 		    IPPROTO_IPV6, M_NOWAIT);
152768bb8d62SAdrian Chadd 		if (*mp)
152868bb8d62SAdrian Chadd 			mp = &(*mp)->m_next;
1529b46667c6SGleb Smirnoff 		*mp = sbcreatecontrol(&flow_type, sizeof(uint32_t),
1530b46667c6SGleb Smirnoff 		    IPV6_FLOWTYPE, IPPROTO_IPV6, M_NOWAIT);
153168bb8d62SAdrian Chadd 		if (*mp)
153268bb8d62SAdrian Chadd 			mp = &(*mp)->m_next;
153368bb8d62SAdrian Chadd 	}
153468bb8d62SAdrian Chadd 
153568bb8d62SAdrian Chadd #ifdef	RSS
15360ecd976eSBjoern A. Zeeb 	if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
153768bb8d62SAdrian Chadd 		uint32_t flowid, flow_type;
153868bb8d62SAdrian Chadd 		uint32_t rss_bucketid;
153968bb8d62SAdrian Chadd 
154068bb8d62SAdrian Chadd 		flowid = m->m_pkthdr.flowid;
154168bb8d62SAdrian Chadd 		flow_type = M_HASHTYPE_GET(m);
154268bb8d62SAdrian Chadd 
154368bb8d62SAdrian Chadd 		if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1544b46667c6SGleb Smirnoff 			*mp = sbcreatecontrol(&rss_bucketid, sizeof(uint32_t),
1545b46667c6SGleb Smirnoff 			    IPV6_RSSBUCKETID, IPPROTO_IPV6, M_NOWAIT);
154668bb8d62SAdrian Chadd 			if (*mp)
154768bb8d62SAdrian Chadd 				mp = &(*mp)->m_next;
154868bb8d62SAdrian Chadd 		}
154968bb8d62SAdrian Chadd 	}
155068bb8d62SAdrian Chadd #endif
155168bb8d62SAdrian Chadd 
155282cd038dSYoshinobu Inoue }
15539a38ba81SBjoern A. Zeeb #undef IS2292
155482cd038dSYoshinobu Inoue 
1555efddf5c6SHajimu UMEMOTO void
15568f1beb88SAndrey V. Elsukov ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu)
1557efddf5c6SHajimu UMEMOTO {
1558efddf5c6SHajimu UMEMOTO 	struct socket *so;
1559efddf5c6SHajimu UMEMOTO 	struct mbuf *m_mtu;
1560efddf5c6SHajimu UMEMOTO 	struct ip6_mtuinfo mtuctl;
1561efddf5c6SHajimu UMEMOTO 
15628f1beb88SAndrey V. Elsukov 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
15638f1beb88SAndrey V. Elsukov 	/*
15648f1beb88SAndrey V. Elsukov 	 * Notify the error by sending IPV6_PATHMTU ancillary data if
15658f1beb88SAndrey V. Elsukov 	 * application wanted to know the MTU value.
15668f1beb88SAndrey V. Elsukov 	 * NOTE: we notify disconnected sockets, because some udp
15678f1beb88SAndrey V. Elsukov 	 * applications keep sending sockets disconnected.
15688f1beb88SAndrey V. Elsukov 	 * NOTE: our implementation doesn't notify connected sockets that has
15698f1beb88SAndrey V. Elsukov 	 * foreign address that is different than given destination addresses
15708f1beb88SAndrey V. Elsukov 	 * (this is permitted by RFC 3542).
15718f1beb88SAndrey V. Elsukov 	 */
15728f1beb88SAndrey V. Elsukov 	if ((inp->inp_flags & IN6P_MTU) == 0 || (
15738f1beb88SAndrey V. Elsukov 	    !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
15748f1beb88SAndrey V. Elsukov 	    !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr)))
1575efddf5c6SHajimu UMEMOTO 		return;
1576efddf5c6SHajimu UMEMOTO 
15778f1beb88SAndrey V. Elsukov 	mtuctl.ip6m_mtu = mtu;
1578efddf5c6SHajimu UMEMOTO 	mtuctl.ip6m_addr = *dst;
1579a1f7e5f8SHajimu UMEMOTO 	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1580a1f7e5f8SHajimu UMEMOTO 		return;
1581efddf5c6SHajimu UMEMOTO 
1582b46667c6SGleb Smirnoff 	if ((m_mtu = sbcreatecontrol(&mtuctl, sizeof(mtuctl), IPV6_PATHMTU,
1583b46667c6SGleb Smirnoff 	    IPPROTO_IPV6, M_NOWAIT)) == NULL)
1584efddf5c6SHajimu UMEMOTO 		return;
1585efddf5c6SHajimu UMEMOTO 
15868f1beb88SAndrey V. Elsukov 	so =  inp->inp_socket;
1587efddf5c6SHajimu UMEMOTO 	if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1588efddf5c6SHajimu UMEMOTO 	    == 0) {
15897045b160SRoy Marples 		soroverflow(so);
1590efddf5c6SHajimu UMEMOTO 		m_freem(m_mtu);
1591efddf5c6SHajimu UMEMOTO 		/* XXX: should count statistics */
1592efddf5c6SHajimu UMEMOTO 	} else
1593efddf5c6SHajimu UMEMOTO 		sorwakeup(so);
1594efddf5c6SHajimu UMEMOTO }
1595efddf5c6SHajimu UMEMOTO 
159682cd038dSYoshinobu Inoue /*
159782cd038dSYoshinobu Inoue  * Get pointer to the previous header followed by the header
159882cd038dSYoshinobu Inoue  * currently processed.
159982cd038dSYoshinobu Inoue  */
160068e0e5a6SAndrey V. Elsukov int
16010be18915SAdrian Chadd ip6_get_prevhdr(const struct mbuf *m, int off)
160282cd038dSYoshinobu Inoue {
160368e0e5a6SAndrey V. Elsukov 	struct ip6_ext ip6e;
160468e0e5a6SAndrey V. Elsukov 	struct ip6_hdr *ip6;
160568e0e5a6SAndrey V. Elsukov 	int len, nlen, nxt;
160682cd038dSYoshinobu Inoue 
160782cd038dSYoshinobu Inoue 	if (off == sizeof(struct ip6_hdr))
160868e0e5a6SAndrey V. Elsukov 		return (offsetof(struct ip6_hdr, ip6_nxt));
160968e0e5a6SAndrey V. Elsukov 	if (off < sizeof(struct ip6_hdr))
161068e0e5a6SAndrey V. Elsukov 		panic("%s: off < sizeof(struct ip6_hdr)", __func__);
161182cd038dSYoshinobu Inoue 
161268e0e5a6SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
161382cd038dSYoshinobu Inoue 	nxt = ip6->ip6_nxt;
161482cd038dSYoshinobu Inoue 	len = sizeof(struct ip6_hdr);
161568e0e5a6SAndrey V. Elsukov 	nlen = 0;
161682cd038dSYoshinobu Inoue 	while (len < off) {
161768e0e5a6SAndrey V. Elsukov 		m_copydata(m, len, sizeof(ip6e), (caddr_t)&ip6e);
161882cd038dSYoshinobu Inoue 		switch (nxt) {
161982cd038dSYoshinobu Inoue 		case IPPROTO_FRAGMENT:
162068e0e5a6SAndrey V. Elsukov 			nlen = sizeof(struct ip6_frag);
162182cd038dSYoshinobu Inoue 			break;
162282cd038dSYoshinobu Inoue 		case IPPROTO_AH:
162368e0e5a6SAndrey V. Elsukov 			nlen = (ip6e.ip6e_len + 2) << 2;
162482cd038dSYoshinobu Inoue 			break;
162582cd038dSYoshinobu Inoue 		default:
162668e0e5a6SAndrey V. Elsukov 			nlen = (ip6e.ip6e_len + 1) << 3;
162782cd038dSYoshinobu Inoue 		}
162868e0e5a6SAndrey V. Elsukov 		len += nlen;
162968e0e5a6SAndrey V. Elsukov 		nxt = ip6e.ip6e_nxt;
163082cd038dSYoshinobu Inoue 	}
163168e0e5a6SAndrey V. Elsukov 	return (len - nlen);
163282cd038dSYoshinobu Inoue }
163382cd038dSYoshinobu Inoue 
163482cd038dSYoshinobu Inoue /*
1635686cdd19SJun-ichiro itojun Hagino  * get next header offset.  m will be retained.
1636686cdd19SJun-ichiro itojun Hagino  */
1637686cdd19SJun-ichiro itojun Hagino int
16380be18915SAdrian Chadd ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1639686cdd19SJun-ichiro itojun Hagino {
1640686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr ip6;
1641686cdd19SJun-ichiro itojun Hagino 	struct ip6_ext ip6e;
1642686cdd19SJun-ichiro itojun Hagino 	struct ip6_frag fh;
1643686cdd19SJun-ichiro itojun Hagino 
1644686cdd19SJun-ichiro itojun Hagino 	/* just in case */
1645686cdd19SJun-ichiro itojun Hagino 	if (m == NULL)
1646686cdd19SJun-ichiro itojun Hagino 		panic("ip6_nexthdr: m == NULL");
1647686cdd19SJun-ichiro itojun Hagino 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1648686cdd19SJun-ichiro itojun Hagino 		return -1;
1649686cdd19SJun-ichiro itojun Hagino 
1650686cdd19SJun-ichiro itojun Hagino 	switch (proto) {
1651686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV6:
1652686cdd19SJun-ichiro itojun Hagino 		if (m->m_pkthdr.len < off + sizeof(ip6))
1653686cdd19SJun-ichiro itojun Hagino 			return -1;
1654686cdd19SJun-ichiro itojun Hagino 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1655686cdd19SJun-ichiro itojun Hagino 		if (nxtp)
1656686cdd19SJun-ichiro itojun Hagino 			*nxtp = ip6.ip6_nxt;
1657686cdd19SJun-ichiro itojun Hagino 		off += sizeof(ip6);
1658686cdd19SJun-ichiro itojun Hagino 		return off;
1659686cdd19SJun-ichiro itojun Hagino 
1660686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_FRAGMENT:
1661686cdd19SJun-ichiro itojun Hagino 		/*
1662686cdd19SJun-ichiro itojun Hagino 		 * terminate parsing if it is not the first fragment,
1663686cdd19SJun-ichiro itojun Hagino 		 * it does not make sense to parse through it.
1664686cdd19SJun-ichiro itojun Hagino 		 */
1665686cdd19SJun-ichiro itojun Hagino 		if (m->m_pkthdr.len < off + sizeof(fh))
1666686cdd19SJun-ichiro itojun Hagino 			return -1;
1667686cdd19SJun-ichiro itojun Hagino 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1668020a816fSHajimu UMEMOTO 		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1669020a816fSHajimu UMEMOTO 		if (fh.ip6f_offlg & IP6F_OFF_MASK)
1670686cdd19SJun-ichiro itojun Hagino 			return -1;
1671686cdd19SJun-ichiro itojun Hagino 		if (nxtp)
1672686cdd19SJun-ichiro itojun Hagino 			*nxtp = fh.ip6f_nxt;
1673686cdd19SJun-ichiro itojun Hagino 		off += sizeof(struct ip6_frag);
1674686cdd19SJun-ichiro itojun Hagino 		return off;
1675686cdd19SJun-ichiro itojun Hagino 
1676686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_AH:
1677686cdd19SJun-ichiro itojun Hagino 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1678686cdd19SJun-ichiro itojun Hagino 			return -1;
1679686cdd19SJun-ichiro itojun Hagino 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1680686cdd19SJun-ichiro itojun Hagino 		if (nxtp)
1681686cdd19SJun-ichiro itojun Hagino 			*nxtp = ip6e.ip6e_nxt;
1682686cdd19SJun-ichiro itojun Hagino 		off += (ip6e.ip6e_len + 2) << 2;
1683686cdd19SJun-ichiro itojun Hagino 		return off;
1684686cdd19SJun-ichiro itojun Hagino 
1685686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_HOPOPTS:
1686686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_ROUTING:
1687686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_DSTOPTS:
1688686cdd19SJun-ichiro itojun Hagino 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1689686cdd19SJun-ichiro itojun Hagino 			return -1;
1690686cdd19SJun-ichiro itojun Hagino 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1691686cdd19SJun-ichiro itojun Hagino 		if (nxtp)
1692686cdd19SJun-ichiro itojun Hagino 			*nxtp = ip6e.ip6e_nxt;
1693686cdd19SJun-ichiro itojun Hagino 		off += (ip6e.ip6e_len + 1) << 3;
1694686cdd19SJun-ichiro itojun Hagino 		return off;
1695686cdd19SJun-ichiro itojun Hagino 
1696686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_NONE:
1697686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_ESP:
1698686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPCOMP:
1699686cdd19SJun-ichiro itojun Hagino 		/* give up */
1700686cdd19SJun-ichiro itojun Hagino 		return -1;
1701686cdd19SJun-ichiro itojun Hagino 
1702686cdd19SJun-ichiro itojun Hagino 	default:
1703686cdd19SJun-ichiro itojun Hagino 		return -1;
1704686cdd19SJun-ichiro itojun Hagino 	}
1705686cdd19SJun-ichiro itojun Hagino 
17060be18915SAdrian Chadd 	/* NOTREACHED */
1707686cdd19SJun-ichiro itojun Hagino }
1708686cdd19SJun-ichiro itojun Hagino 
1709686cdd19SJun-ichiro itojun Hagino /*
1710686cdd19SJun-ichiro itojun Hagino  * get offset for the last header in the chain.  m will be kept untainted.
1711686cdd19SJun-ichiro itojun Hagino  */
1712686cdd19SJun-ichiro itojun Hagino int
17130be18915SAdrian Chadd ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1714686cdd19SJun-ichiro itojun Hagino {
1715686cdd19SJun-ichiro itojun Hagino 	int newoff;
1716686cdd19SJun-ichiro itojun Hagino 	int nxt;
1717686cdd19SJun-ichiro itojun Hagino 
1718686cdd19SJun-ichiro itojun Hagino 	if (!nxtp) {
1719686cdd19SJun-ichiro itojun Hagino 		nxt = -1;
1720686cdd19SJun-ichiro itojun Hagino 		nxtp = &nxt;
1721686cdd19SJun-ichiro itojun Hagino 	}
1722686cdd19SJun-ichiro itojun Hagino 	while (1) {
1723686cdd19SJun-ichiro itojun Hagino 		newoff = ip6_nexthdr(m, off, proto, nxtp);
1724686cdd19SJun-ichiro itojun Hagino 		if (newoff < 0)
1725686cdd19SJun-ichiro itojun Hagino 			return off;
1726686cdd19SJun-ichiro itojun Hagino 		else if (newoff < off)
1727686cdd19SJun-ichiro itojun Hagino 			return -1;	/* invalid */
1728686cdd19SJun-ichiro itojun Hagino 		else if (newoff == off)
1729686cdd19SJun-ichiro itojun Hagino 			return newoff;
1730686cdd19SJun-ichiro itojun Hagino 
1731686cdd19SJun-ichiro itojun Hagino 		off = newoff;
1732686cdd19SJun-ichiro itojun Hagino 		proto = *nxtp;
1733686cdd19SJun-ichiro itojun Hagino 	}
1734686cdd19SJun-ichiro itojun Hagino }
1735