xref: /freebsd-src/sys/netinet6/raw_ip6.c (revision 20162e6f1c176c906c6d9d99fc66f6b0ed37dc05)
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.
3082cd038dSYoshinobu Inoue  */
3182cd038dSYoshinobu Inoue 
32caf43b02SWarner Losh /*-
3382cd038dSYoshinobu Inoue  * Copyright (c) 1982, 1986, 1988, 1993
340ae76120SRobert Watson  *	The Regents of the University of California.
350ae76120SRobert Watson  * All rights reserved.
3682cd038dSYoshinobu Inoue  *
3782cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
3882cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
3982cd038dSYoshinobu Inoue  * are met:
4082cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
4182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
4282cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
4382cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
4482cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
45fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
4682cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
4782cd038dSYoshinobu Inoue  *    without specific prior written permission.
4882cd038dSYoshinobu Inoue  *
4982cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5082cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5182cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5282cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5382cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5482cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5582cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5682cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5782cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5882cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5982cd038dSYoshinobu Inoue  * SUCH DAMAGE.
6082cd038dSYoshinobu Inoue  */
6182cd038dSYoshinobu Inoue 
62b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
636a800098SYoshinobu Inoue #include "opt_ipsec.h"
6433841545SHajimu UMEMOTO #include "opt_inet6.h"
650c325f53SAlexander V. Chernikov #include "opt_route.h"
666a800098SYoshinobu Inoue 
6782cd038dSYoshinobu Inoue #include <sys/param.h>
6882cd038dSYoshinobu Inoue #include <sys/errno.h>
69413628a7SBjoern A. Zeeb #include <sys/jail.h>
70a786f679SAndrey V. Elsukov #include <sys/kernel.h>
71960ed29cSSeigo Tanimura #include <sys/lock.h>
72960ed29cSSeigo Tanimura #include <sys/malloc.h>
73960ed29cSSeigo Tanimura #include <sys/mbuf.h>
74c2259ba4SRobert Watson #include <sys/priv.h>
75960ed29cSSeigo Tanimura #include <sys/proc.h>
76960ed29cSSeigo Tanimura #include <sys/protosw.h>
77960ed29cSSeigo Tanimura #include <sys/signalvar.h>
78960ed29cSSeigo Tanimura #include <sys/socket.h>
79960ed29cSSeigo Tanimura #include <sys/socketvar.h>
80960ed29cSSeigo Tanimura #include <sys/sx.h>
81a1f7e5f8SHajimu UMEMOTO #include <sys/syslog.h>
8282cd038dSYoshinobu Inoue 
8382cd038dSYoshinobu Inoue #include <net/if.h>
8476039bc8SGleb Smirnoff #include <net/if_var.h>
853d0d5b21SJustin Hibbits #include <net/if_private.h>
8682cd038dSYoshinobu Inoue #include <net/if_types.h>
87960ed29cSSeigo Tanimura #include <net/route.h>
884b79449eSBjoern A. Zeeb #include <net/vnet.h>
8982cd038dSYoshinobu Inoue 
9082cd038dSYoshinobu Inoue #include <netinet/in.h>
9182cd038dSYoshinobu Inoue #include <netinet/in_var.h>
9282cd038dSYoshinobu Inoue #include <netinet/in_systm.h>
9382cd038dSYoshinobu Inoue #include <netinet/in_pcb.h>
944b79449eSBjoern A. Zeeb 
954b79449eSBjoern A. Zeeb #include <netinet/icmp6.h>
96960ed29cSSeigo Tanimura #include <netinet/ip6.h>
971db8d1f8SAna Kukec #include <netinet/ip_var.h>
98960ed29cSSeigo Tanimura #include <netinet6/ip6_mroute.h>
99960ed29cSSeigo Tanimura #include <netinet6/in6_pcb.h>
100960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
101960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
102960ed29cSSeigo Tanimura #include <netinet6/raw_ip6.h>
1030c325f53SAlexander V. Chernikov #include <netinet6/in6_fib.h>
104686cdd19SJun-ichiro itojun Hagino #include <netinet6/scope6_var.h>
1051db8d1f8SAna Kukec #include <netinet6/send.h>
10682cd038dSYoshinobu Inoue 
107fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
108b9234fafSSam Leffler 
10982cd038dSYoshinobu Inoue #include <machine/stdarg.h>
11082cd038dSYoshinobu Inoue 
11182cd038dSYoshinobu Inoue #define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
11282cd038dSYoshinobu Inoue #define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
11382cd038dSYoshinobu Inoue 
11482cd038dSYoshinobu Inoue /*
11582cd038dSYoshinobu Inoue  * Raw interface to IP6 protocol.
11682cd038dSYoshinobu Inoue  */
11782cd038dSYoshinobu Inoue 
118eddfbb76SRobert Watson VNET_DECLARE(struct inpcbinfo, ripcbinfo);
1191e77c105SRobert Watson #define	V_ripcbinfo			VNET(ripcbinfo)
120eddfbb76SRobert Watson 
12197021c24SMarko Zec extern u_long	rip_sendspace;
12297021c24SMarko Zec extern u_long	rip_recvspace;
12397021c24SMarko Zec 
124a786f679SAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct rip6stat, rip6stat);
125a786f679SAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(rip6stat);
126a786f679SAndrey V. Elsukov 
127a786f679SAndrey V. Elsukov #ifdef VIMAGE
128a786f679SAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(rip6stat);
129a786f679SAndrey V. Elsukov #endif /* VIMAGE */
13082cea7e6SBjoern A. Zeeb 
13182cd038dSYoshinobu Inoue /*
13233cde130SBruce M Simpson  * Hooks for multicast routing. They all default to NULL, so leave them not
13333cde130SBruce M Simpson  * initialized and rely on BSS being set to 0.
1346be2e366SBruce M Simpson  */
13533cde130SBruce M Simpson 
13633cde130SBruce M Simpson /*
13733cde130SBruce M Simpson  * The socket used to communicate with the multicast routing daemon.
13833cde130SBruce M Simpson  */
139eddfbb76SRobert Watson VNET_DEFINE(struct socket *, ip6_mrouter);
14033cde130SBruce M Simpson 
14133cde130SBruce M Simpson /*
14233cde130SBruce M Simpson  * The various mrouter functions.
14333cde130SBruce M Simpson  */
1446be2e366SBruce M Simpson int (*ip6_mrouter_set)(struct socket *, struct sockopt *);
1456be2e366SBruce M Simpson int (*ip6_mrouter_get)(struct socket *, struct sockopt *);
1466be2e366SBruce M Simpson int (*ip6_mrouter_done)(void);
1476be2e366SBruce M Simpson int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
148e40bae9aSRoman Divacky int (*mrt6_ioctl)(u_long, caddr_t);
1496be2e366SBruce M Simpson 
150db0ac6deSCy Schubert struct rip6_inp_match_ctx {
151db0ac6deSCy Schubert 	struct ip6_hdr *ip6;
152db0ac6deSCy Schubert 	int proto;
153db0ac6deSCy Schubert };
154db0ac6deSCy Schubert 
155db0ac6deSCy Schubert static bool
156db0ac6deSCy Schubert rip6_inp_match(const struct inpcb *inp, void *v)
157db0ac6deSCy Schubert {
158db0ac6deSCy Schubert 	struct rip6_inp_match_ctx *c = v;
159db0ac6deSCy Schubert 	struct ip6_hdr *ip6 = c->ip6;
160db0ac6deSCy Schubert 	int proto = c->proto;
161db0ac6deSCy Schubert 
162db0ac6deSCy Schubert 	/* XXX inp locking */
163db0ac6deSCy Schubert 	if ((inp->inp_vflag & INP_IPV6) == 0)
164db0ac6deSCy Schubert 		return (false);
165db0ac6deSCy Schubert 	if (inp->inp_ip_p && inp->inp_ip_p != proto)
166db0ac6deSCy Schubert 		return (false);
167db0ac6deSCy Schubert 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
168db0ac6deSCy Schubert 	    !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst))
169db0ac6deSCy Schubert 		return (false);
170db0ac6deSCy Schubert 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
171db0ac6deSCy Schubert 	    !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src))
172db0ac6deSCy Schubert 		return (false);
173db0ac6deSCy Schubert 
174db0ac6deSCy Schubert 	return (true);
175db0ac6deSCy Schubert }
176db0ac6deSCy Schubert 
1776be2e366SBruce M Simpson /*
1780ae76120SRobert Watson  * Setup generic address and protocol structures for raw_input routine, then
1790ae76120SRobert Watson  * pass them along with mbuf chain.
18082cd038dSYoshinobu Inoue  */
18182cd038dSYoshinobu Inoue int
1821272577eSXin LI rip6_input(struct mbuf **mp, int *offp, int proto)
18382cd038dSYoshinobu Inoue {
18433cde130SBruce M Simpson 	struct ifnet *ifp;
185db0ac6deSCy Schubert 	struct mbuf *n, *m = *mp;
1863e85b721SEd Maste 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1870ecd976eSBjoern A. Zeeb 	struct inpcb *inp;
18833841545SHajimu UMEMOTO 	struct mbuf *opts = NULL;
189af12e09eSHajimu UMEMOTO 	struct sockaddr_in6 fromsa;
190db0ac6deSCy Schubert 	struct rip6_inp_match_ctx ctx = { .ip6 = ip6, .proto = proto };
191db0ac6deSCy Schubert 	struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo,
192db0ac6deSCy Schubert 	    INPLOOKUP_RLOCKPCB, rip6_inp_match, &ctx);
193db0ac6deSCy Schubert 	int delivered = 0;
194f42347c3SGleb Smirnoff 
195f42347c3SGleb Smirnoff 	NET_EPOCH_ASSERT();
19682cd038dSYoshinobu Inoue 
197f1d7ebfeSAndrey V. Elsukov 	RIP6STAT_INC(rip6s_ipackets);
19833841545SHajimu UMEMOTO 
199dce33a45SErmal Luçi 	init_sin6(&fromsa, m, 0); /* general init */
20082cd038dSYoshinobu Inoue 
20133cde130SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
20233cde130SBruce M Simpson 
203db0ac6deSCy Schubert 	while ((inp = inp_next(&inpi)) != NULL) {
204db0ac6deSCy Schubert 		INP_RLOCK_ASSERT(inp);
2051ff6e7a8SBjoern A. Zeeb #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2061ff6e7a8SBjoern A. Zeeb 		/*
2071ff6e7a8SBjoern A. Zeeb 		 * Check AH/ESP integrity.
2081ff6e7a8SBjoern A. Zeeb 		 */
209db0ac6deSCy Schubert 		if (IPSEC_ENABLED(ipv6) &&
210db0ac6deSCy Schubert 		    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
2111ff6e7a8SBjoern A. Zeeb 			/* Do not inject data into pcb. */
212db0ac6deSCy Schubert 			continue;
2131ff6e7a8SBjoern A. Zeeb 		}
2141ff6e7a8SBjoern A. Zeeb #endif /* IPSEC */
215db0ac6deSCy Schubert 		if (jailed_without_vnet(inp->inp_cred) &&
216db0ac6deSCy Schubert 		    !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
217db0ac6deSCy Schubert 		    prison_check_ip6(inp->inp_cred, &ip6->ip6_dst) != 0)
21833cde130SBruce M Simpson 			/*
21933cde130SBruce M Simpson 			 * Allow raw socket in jail to receive multicast;
22033cde130SBruce M Simpson 			 * assume process had PRIV_NETINET_RAW at attach,
22133cde130SBruce M Simpson 			 * and fall through into normal filter path if so.
22233cde130SBruce M Simpson 			 */
223db0ac6deSCy Schubert 			continue;
2240ecd976eSBjoern A. Zeeb 		if (inp->in6p_cksum != -1) {
225f1d7ebfeSAndrey V. Elsukov 			RIP6STAT_INC(rip6s_isum);
2260ecd976eSBjoern A. Zeeb 			if (m->m_pkthdr.len - (*offp + inp->in6p_cksum) < 2 ||
22770a0f3dcSMichael Tuexen 			    in6_cksum(m, proto, *offp,
22882cd038dSYoshinobu Inoue 			    m->m_pkthdr.len - *offp)) {
229f1d7ebfeSAndrey V. Elsukov 				RIP6STAT_INC(rip6s_badsum);
230fb288770SMichael Tuexen 				/*
231fb288770SMichael Tuexen 				 * Drop the received message, don't send an
232fb288770SMichael Tuexen 				 * ICMP6 message. Set proto to IPPROTO_NONE
233fb288770SMichael Tuexen 				 * to achieve that.
234fb288770SMichael Tuexen 				 */
235db0ac6deSCy Schubert 				INP_RUNLOCK(inp);
236fb288770SMichael Tuexen 				proto = IPPROTO_NONE;
237db0ac6deSCy Schubert 				break;
23882cd038dSYoshinobu Inoue 			}
23933841545SHajimu UMEMOTO 		}
24033cde130SBruce M Simpson 		/*
24133cde130SBruce M Simpson 		 * If this raw socket has multicast state, and we
24233cde130SBruce M Simpson 		 * have received a multicast, check if this socket
24333cde130SBruce M Simpson 		 * should receive it, as multicast filtering is now
24433cde130SBruce M Simpson 		 * the responsibility of the transport layer.
24533cde130SBruce M Simpson 		 */
2460ecd976eSBjoern A. Zeeb 		if (inp->in6p_moptions &&
24733cde130SBruce M Simpson 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
2487ab5a5cdSBruce M Simpson 			/*
2497ab5a5cdSBruce M Simpson 			 * If the incoming datagram is for MLD, allow it
2507ab5a5cdSBruce M Simpson 			 * through unconditionally to the raw socket.
2517ab5a5cdSBruce M Simpson 			 *
2527ab5a5cdSBruce M Simpson 			 * Use the M_RTALERT_MLD flag to check for MLD
2537ab5a5cdSBruce M Simpson 			 * traffic without having to inspect the mbuf chain
2547ab5a5cdSBruce M Simpson 			 * more deeply, as all MLDv1/v2 host messages MUST
2557ab5a5cdSBruce M Simpson 			 * contain the Router Alert option.
2567ab5a5cdSBruce M Simpson 			 *
2577ab5a5cdSBruce M Simpson 			 * In the case of MLDv1, we may not have explicitly
2587ab5a5cdSBruce M Simpson 			 * joined the group, and may have set IFF_ALLMULTI
2597ab5a5cdSBruce M Simpson 			 * on the interface. im6o_mc_filter() may discard
2607ab5a5cdSBruce M Simpson 			 * control traffic we actually need to see.
2617ab5a5cdSBruce M Simpson 			 *
2627ab5a5cdSBruce M Simpson 			 * Userland multicast routing daemons should continue
2637ab5a5cdSBruce M Simpson 			 * filter the control traffic appropriately.
2647ab5a5cdSBruce M Simpson 			 */
26533cde130SBruce M Simpson 			int blocked;
26633cde130SBruce M Simpson 
2677ab5a5cdSBruce M Simpson 			blocked = MCAST_PASS;
2687ab5a5cdSBruce M Simpson 			if ((m->m_flags & M_RTALERT_MLD) == 0) {
2697ab5a5cdSBruce M Simpson 				struct sockaddr_in6 mcaddr;
2707ab5a5cdSBruce M Simpson 
27133cde130SBruce M Simpson 				bzero(&mcaddr, sizeof(struct sockaddr_in6));
27233cde130SBruce M Simpson 				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
27333cde130SBruce M Simpson 				mcaddr.sin6_family = AF_INET6;
27433cde130SBruce M Simpson 				mcaddr.sin6_addr = ip6->ip6_dst;
27533cde130SBruce M Simpson 
2760ecd976eSBjoern A. Zeeb 				blocked = im6o_mc_filter(inp->in6p_moptions,
2777ab5a5cdSBruce M Simpson 				    ifp,
27833cde130SBruce M Simpson 				    (struct sockaddr *)&mcaddr,
27933cde130SBruce M Simpson 				    (struct sockaddr *)&fromsa);
2807ab5a5cdSBruce M Simpson 			}
28133cde130SBruce M Simpson 			if (blocked != MCAST_PASS) {
28233cde130SBruce M Simpson 				IP6STAT_INC(ip6s_notmember);
283de2d4784SGleb Smirnoff 				continue;
284266f97b5SCy Schubert 			}
285db0ac6deSCy Schubert 		}
286db0ac6deSCy Schubert 		if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL)
287db0ac6deSCy Schubert 			continue;
288db0ac6deSCy Schubert 		if (inp->inp_flags & INP_CONTROLOPTS ||
289db0ac6deSCy Schubert 		    inp->inp_socket->so_options & SO_TIMESTAMP)
290db0ac6deSCy Schubert 			ip6_savecontrol(inp, n, &opts);
291db0ac6deSCy Schubert 		/* strip intermediate headers */
292db0ac6deSCy Schubert 		m_adj(n, *offp);
293db0ac6deSCy Schubert 		if (sbappendaddr(&inp->inp_socket->so_rcv,
294db0ac6deSCy Schubert 		    (struct sockaddr *)&fromsa, n, opts) == 0) {
295db0ac6deSCy Schubert 			soroverflow(inp->inp_socket);
296db0ac6deSCy Schubert 			m_freem(n);
29782cd038dSYoshinobu Inoue 			if (opts)
29882cd038dSYoshinobu Inoue 				m_freem(opts);
299f1d7ebfeSAndrey V. Elsukov 			RIP6STAT_INC(rip6s_fullsock);
30082cd038dSYoshinobu Inoue 		} else {
301db0ac6deSCy Schubert 			sorwakeup(inp->inp_socket);
302db0ac6deSCy Schubert 			delivered++;
303db0ac6deSCy Schubert 		}
304db0ac6deSCy Schubert 		opts = NULL;
305db0ac6deSCy Schubert 	}
306db0ac6deSCy Schubert 	if (delivered == 0) {
307f1d7ebfeSAndrey V. Elsukov 		RIP6STAT_INC(rip6s_nosock);
30833841545SHajimu UMEMOTO 		if (m->m_flags & M_MCAST)
309f1d7ebfeSAndrey V. Elsukov 			RIP6STAT_INC(rip6s_nosockmcast);
31082cd038dSYoshinobu Inoue 		if (proto == IPPROTO_NONE)
31182cd038dSYoshinobu Inoue 			m_freem(m);
31268e0e5a6SAndrey V. Elsukov 		else
31382cd038dSYoshinobu Inoue 			icmp6_error(m, ICMP6_PARAM_PROB,
31482cd038dSYoshinobu Inoue 			    ICMP6_PARAMPROB_NEXTHEADER,
31568e0e5a6SAndrey V. Elsukov 			    ip6_get_prevhdr(m, *offp));
3169cb8d207SAndrey V. Elsukov 		IP6STAT_DEC(ip6s_delivered);
317db0ac6deSCy Schubert 	} else
318db0ac6deSCy Schubert 		m_freem(m);
3190ae76120SRobert Watson 	return (IPPROTO_DONE);
32082cd038dSYoshinobu Inoue }
32182cd038dSYoshinobu Inoue 
322686cdd19SJun-ichiro itojun Hagino void
323fcb3f813SGleb Smirnoff rip6_ctlinput(struct ip6ctlparam *ip6cp)
324686cdd19SJun-ichiro itojun Hagino {
325fcb3f813SGleb Smirnoff 	int errno;
326686cdd19SJun-ichiro itojun Hagino 
327fcb3f813SGleb Smirnoff 	if ((errno = icmp6_errmap(ip6cp->ip6c_icmp6)) != 0)
328fcb3f813SGleb Smirnoff 		in6_pcbnotify(&V_ripcbinfo, ip6cp->ip6c_finaldst, 0,
329fcb3f813SGleb Smirnoff 		    ip6cp->ip6c_src, 0, errno, ip6cp->ip6c_cmdarg,
330fcb3f813SGleb Smirnoff 		    in6_rtchange);
331686cdd19SJun-ichiro itojun Hagino }
332686cdd19SJun-ichiro itojun Hagino 
33382cd038dSYoshinobu Inoue /*
3340ae76120SRobert Watson  * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
3350ae76120SRobert Watson  * may have setup with control call.
33682cd038dSYoshinobu Inoue  */
337e0b40500SGleb Smirnoff static int
338e0b40500SGleb Smirnoff rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
339e0b40500SGleb Smirnoff     struct mbuf *control, struct thread *td)
34082cd038dSYoshinobu Inoue {
341b9555453SGleb Smirnoff 	struct epoch_tracker et;
3420ecd976eSBjoern A. Zeeb 	struct inpcb *inp;
343e0b40500SGleb Smirnoff 	struct sockaddr_in6 tmp, *dstsock;
344e0b40500SGleb Smirnoff 	struct m_tag *mtag;
345e0b40500SGleb Smirnoff 	struct ip6_hdr *ip6;
34682cd038dSYoshinobu Inoue 	u_int	plen = m->m_pkthdr.len;
347e07db7aaSHajimu UMEMOTO 	struct ip6_pktopts opt, *optp;
34882cd038dSYoshinobu Inoue 	struct ifnet *oifp = NULL;
349e0b40500SGleb Smirnoff 	int error;
35082cd038dSYoshinobu Inoue 	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
351a1f7e5f8SHajimu UMEMOTO 	int scope_ambiguous = 0;
352388288b2SHajimu UMEMOTO 	int use_defzone = 0;
353601c0b8bSAlexander V. Chernikov 	int hlim = 0;
35488d166bfSBjoern A. Zeeb 	struct in6_addr in6a;
35582cd038dSYoshinobu Inoue 
3560ecd976eSBjoern A. Zeeb 	inp = sotoinpcb(so);
357e0b40500SGleb Smirnoff 	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
358e0b40500SGleb Smirnoff 
359e0b40500SGleb Smirnoff 	/* Always copy sockaddr to avoid overwrites. */
360e0b40500SGleb Smirnoff 	/* Unlocked read. */
361e0b40500SGleb Smirnoff 	if (so->so_state & SS_ISCONNECTED) {
362e0b40500SGleb Smirnoff 		if (nam) {
363e0b40500SGleb Smirnoff 			error = EISCONN;
364e0b40500SGleb Smirnoff 			goto release;
365e0b40500SGleb Smirnoff 		}
366e0b40500SGleb Smirnoff 		tmp = (struct sockaddr_in6 ){
367e0b40500SGleb Smirnoff 			.sin6_family = AF_INET6,
368e0b40500SGleb Smirnoff 			.sin6_len = sizeof(struct sockaddr_in6),
369e0b40500SGleb Smirnoff 		};
370e0b40500SGleb Smirnoff 		INP_RLOCK(inp);
371e0b40500SGleb Smirnoff 		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
372e0b40500SGleb Smirnoff 		    sizeof(struct in6_addr));
373e0b40500SGleb Smirnoff 		INP_RUNLOCK(inp);
374e0b40500SGleb Smirnoff 		dstsock = &tmp;
375e0b40500SGleb Smirnoff 	} else {
376e0b40500SGleb Smirnoff 		if (nam == NULL)
377e0b40500SGleb Smirnoff 			error = ENOTCONN;
378e0b40500SGleb Smirnoff 		else if (nam->sa_family != AF_INET6)
379e0b40500SGleb Smirnoff 			error = EAFNOSUPPORT;
380e0b40500SGleb Smirnoff 		else if (nam->sa_len != sizeof(struct sockaddr_in6))
381e0b40500SGleb Smirnoff 			error = EINVAL;
382e0b40500SGleb Smirnoff 		else
383e0b40500SGleb Smirnoff 			error = 0;
384e0b40500SGleb Smirnoff 		if (error != 0)
385e0b40500SGleb Smirnoff 			goto release;
386e0b40500SGleb Smirnoff 		dstsock = (struct sockaddr_in6 *)nam;
387e0b40500SGleb Smirnoff 		if (dstsock->sin6_family != AF_INET6) {
388e0b40500SGleb Smirnoff 			error = EAFNOSUPPORT;
389e0b40500SGleb Smirnoff 			goto release;
390e0b40500SGleb Smirnoff 		}
391e0b40500SGleb Smirnoff 	}
392e0b40500SGleb Smirnoff 
3930ecd976eSBjoern A. Zeeb 	INP_WLOCK(inp);
39482cd038dSYoshinobu Inoue 
39575bab8b8SKip Macy 	if (control != NULL) {
3962290dfb4SRyan Stone 		NET_EPOCH_ENTER(et);
39761f7427fSGleb Smirnoff 		error = ip6_setpktopts(control, &opt, inp->in6p_outputopts,
39861f7427fSGleb Smirnoff 		    so->so_cred, inp->inp_ip_p);
3992290dfb4SRyan Stone 		NET_EPOCH_EXIT(et);
4002290dfb4SRyan Stone 
4012290dfb4SRyan Stone 		if (error != 0) {
40282cd038dSYoshinobu Inoue 			goto bad;
403f95d4633SHajimu UMEMOTO 		}
404e07db7aaSHajimu UMEMOTO 		optp = &opt;
405e07db7aaSHajimu UMEMOTO 	} else
4060ecd976eSBjoern A. Zeeb 		optp = inp->in6p_outputopts;
40782cd038dSYoshinobu Inoue 
40882cd038dSYoshinobu Inoue 	/*
409a1f7e5f8SHajimu UMEMOTO 	 * Check and convert scope zone ID into internal form.
4100ae76120SRobert Watson 	 *
411a1f7e5f8SHajimu UMEMOTO 	 * XXX: we may still need to determine the zone later.
412a1f7e5f8SHajimu UMEMOTO 	 */
413a1f7e5f8SHajimu UMEMOTO 	if (!(so->so_state & SS_ISCONNECTED)) {
414365ccde0SHajimu UMEMOTO 		if (!optp || !optp->ip6po_pktinfo ||
415365ccde0SHajimu UMEMOTO 		    !optp->ip6po_pktinfo->ipi6_ifindex)
416388288b2SHajimu UMEMOTO 			use_defzone = V_ip6_use_defzone;
417388288b2SHajimu UMEMOTO 		if (dstsock->sin6_scope_id == 0 && !use_defzone)
418a1f7e5f8SHajimu UMEMOTO 			scope_ambiguous = 1;
419388288b2SHajimu UMEMOTO 		if ((error = sa6_embedscope(dstsock, use_defzone)) != 0)
420a1f7e5f8SHajimu UMEMOTO 			goto bad;
421a1f7e5f8SHajimu UMEMOTO 	}
422a1f7e5f8SHajimu UMEMOTO 
423a1f7e5f8SHajimu UMEMOTO 	/*
4240ae76120SRobert Watson 	 * For an ICMPv6 packet, we should know its type and code to update
4250ae76120SRobert Watson 	 * statistics.
42682cd038dSYoshinobu Inoue 	 */
42761f7427fSGleb Smirnoff 	if (inp->inp_ip_p == IPPROTO_ICMPV6) {
42882cd038dSYoshinobu Inoue 		struct icmp6_hdr *icmp6;
42982cd038dSYoshinobu Inoue 		if (m->m_len < sizeof(struct icmp6_hdr) &&
43082cd038dSYoshinobu Inoue 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
43182cd038dSYoshinobu Inoue 			error = ENOBUFS;
43282cd038dSYoshinobu Inoue 			goto bad;
43382cd038dSYoshinobu Inoue 		}
43482cd038dSYoshinobu Inoue 		icmp6 = mtod(m, struct icmp6_hdr *);
43582cd038dSYoshinobu Inoue 		type = icmp6->icmp6_type;
43682cd038dSYoshinobu Inoue 		code = icmp6->icmp6_code;
43782cd038dSYoshinobu Inoue 	}
43882cd038dSYoshinobu Inoue 
439eb1b1807SGleb Smirnoff 	M_PREPEND(m, sizeof(*ip6), M_NOWAIT);
4408a0c4da8SRobert Watson 	if (m == NULL) {
4418a0c4da8SRobert Watson 		error = ENOBUFS;
4428a0c4da8SRobert Watson 		goto bad;
4438a0c4da8SRobert Watson 	}
44482cd038dSYoshinobu Inoue 	ip6 = mtod(m, struct ip6_hdr *);
44582cd038dSYoshinobu Inoue 
4460c325f53SAlexander V. Chernikov #ifdef ROUTE_MPATH
4470c325f53SAlexander V. Chernikov 	if (CALC_FLOWID_OUTBOUND) {
4480c325f53SAlexander V. Chernikov 		uint32_t hash_type, hash_val;
4490c325f53SAlexander V. Chernikov 
4500c325f53SAlexander V. Chernikov 		hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
45161f7427fSGleb Smirnoff 		    &dstsock->sin6_addr, 0, 0, inp->inp_ip_p, &hash_type);
4520c325f53SAlexander V. Chernikov 		inp->inp_flowid = hash_val;
4530c325f53SAlexander V. Chernikov 		inp->inp_flowtype = hash_type;
4540c325f53SAlexander V. Chernikov 	}
4550c325f53SAlexander V. Chernikov #endif
45682cd038dSYoshinobu Inoue 	/*
45782cd038dSYoshinobu Inoue 	 * Source address selection.
45882cd038dSYoshinobu Inoue 	 */
459605284b8SAlexander V. Chernikov 	NET_EPOCH_ENTER(et);
4600ecd976eSBjoern A. Zeeb 	error = in6_selectsrc_socket(dstsock, optp, inp, so->so_cred,
461601c0b8bSAlexander V. Chernikov 	    scope_ambiguous, &in6a, &hlim);
462605284b8SAlexander V. Chernikov 	NET_EPOCH_EXIT(et);
463601c0b8bSAlexander V. Chernikov 
46488d166bfSBjoern A. Zeeb 	if (error)
46582cd038dSYoshinobu Inoue 		goto bad;
4660ecd976eSBjoern A. Zeeb 	error = prison_check_ip6(inp->inp_cred, &in6a);
467b89e82ddSJamie Gritton 	if (error != 0)
468413628a7SBjoern A. Zeeb 		goto bad;
46988d166bfSBjoern A. Zeeb 	ip6->ip6_src = in6a;
470a1f7e5f8SHajimu UMEMOTO 
471a1f7e5f8SHajimu UMEMOTO 	ip6->ip6_dst = dstsock->sin6_addr;
472a1f7e5f8SHajimu UMEMOTO 
4730ae76120SRobert Watson 	/*
4740ae76120SRobert Watson 	 * Fill in the rest of the IPv6 header fields.
4750ae76120SRobert Watson 	 */
4766a800098SYoshinobu Inoue 	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
4770ecd976eSBjoern A. Zeeb 	    (inp->inp_flow & IPV6_FLOWINFO_MASK);
4786a800098SYoshinobu Inoue 	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
4796a800098SYoshinobu Inoue 	    (IPV6_VERSION & IPV6_VERSION_MASK);
4800ae76120SRobert Watson 
4810ae76120SRobert Watson 	/*
4820ae76120SRobert Watson 	 * ip6_plen will be filled in ip6_output, so not fill it here.
4830ae76120SRobert Watson 	 */
4840ecd976eSBjoern A. Zeeb 	ip6->ip6_nxt = inp->inp_ip_p;
485601c0b8bSAlexander V. Chernikov 	ip6->ip6_hlim = hlim;
48682cd038dSYoshinobu Inoue 
48761f7427fSGleb Smirnoff 	if (inp->inp_ip_p == IPPROTO_ICMPV6 || inp->in6p_cksum != -1) {
48882cd038dSYoshinobu Inoue 		struct mbuf *n;
48982cd038dSYoshinobu Inoue 		int off;
49082cd038dSYoshinobu Inoue 		u_int16_t *p;
49182cd038dSYoshinobu Inoue 
4920ae76120SRobert Watson 		/* Compute checksum. */
49361f7427fSGleb Smirnoff 		if (inp->inp_ip_p == IPPROTO_ICMPV6)
49482cd038dSYoshinobu Inoue 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
49582cd038dSYoshinobu Inoue 		else
4960ecd976eSBjoern A. Zeeb 			off = inp->in6p_cksum;
497ae7c65b1SMichael Tuexen 		if (plen < off + 2) {
49882cd038dSYoshinobu Inoue 			error = EINVAL;
49982cd038dSYoshinobu Inoue 			goto bad;
50082cd038dSYoshinobu Inoue 		}
50182cd038dSYoshinobu Inoue 		off += sizeof(struct ip6_hdr);
50282cd038dSYoshinobu Inoue 
50382cd038dSYoshinobu Inoue 		n = m;
50482cd038dSYoshinobu Inoue 		while (n && n->m_len <= off) {
50582cd038dSYoshinobu Inoue 			off -= n->m_len;
50682cd038dSYoshinobu Inoue 			n = n->m_next;
50782cd038dSYoshinobu Inoue 		}
50882cd038dSYoshinobu Inoue 		if (!n)
50982cd038dSYoshinobu Inoue 			goto bad;
51082cd038dSYoshinobu Inoue 		p = (u_int16_t *)(mtod(n, caddr_t) + off);
51182cd038dSYoshinobu Inoue 		*p = 0;
51282cd038dSYoshinobu Inoue 		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
51382cd038dSYoshinobu Inoue 	}
51482cd038dSYoshinobu Inoue 
5151db8d1f8SAna Kukec 	/*
5161db8d1f8SAna Kukec 	 * Send RA/RS messages to user land for protection, before sending
5171db8d1f8SAna Kukec 	 * them to rtadvd/rtsol.
5181db8d1f8SAna Kukec 	 */
5191db8d1f8SAna Kukec 	if ((send_sendso_input_hook != NULL) &&
52061f7427fSGleb Smirnoff 	    inp->inp_ip_p == IPPROTO_ICMPV6) {
5211db8d1f8SAna Kukec 		switch (type) {
5221db8d1f8SAna Kukec 		case ND_ROUTER_ADVERT:
5231db8d1f8SAna Kukec 		case ND_ROUTER_SOLICIT:
5241db8d1f8SAna Kukec 			mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
5251db8d1f8SAna Kukec 				sizeof(unsigned short), M_NOWAIT);
5261db8d1f8SAna Kukec 			if (mtag == NULL)
5271db8d1f8SAna Kukec 				goto bad;
5281db8d1f8SAna Kukec 			m_tag_prepend(m, mtag);
5291db8d1f8SAna Kukec 		}
5301db8d1f8SAna Kukec 	}
5311db8d1f8SAna Kukec 
532b9555453SGleb Smirnoff 	NET_EPOCH_ENTER(et);
5330ecd976eSBjoern A. Zeeb 	error = ip6_output(m, optp, NULL, 0, inp->in6p_moptions, &oifp, inp);
534b9555453SGleb Smirnoff 	NET_EPOCH_EXIT(et);
53561f7427fSGleb Smirnoff 	if (inp->inp_ip_p == IPPROTO_ICMPV6) {
53682cd038dSYoshinobu Inoue 		if (oifp)
53782cd038dSYoshinobu Inoue 			icmp6_ifoutstat_inc(oifp, type, code);
538*60d8dbbeSKristof Provost 		ICMP6STAT_INC2(icp6s_outhist, type);
53933841545SHajimu UMEMOTO 	} else
540f1d7ebfeSAndrey V. Elsukov 		RIP6STAT_INC(rip6s_opackets);
54182cd038dSYoshinobu Inoue 
54282cd038dSYoshinobu Inoue 	goto freectl;
54382cd038dSYoshinobu Inoue 
54482cd038dSYoshinobu Inoue  bad:
54582cd038dSYoshinobu Inoue 	if (m)
54682cd038dSYoshinobu Inoue 		m_freem(m);
54782cd038dSYoshinobu Inoue 
54882cd038dSYoshinobu Inoue  freectl:
54975bab8b8SKip Macy 	if (control != NULL) {
550e07db7aaSHajimu UMEMOTO 		ip6_clearpktopts(&opt, -1);
55182cd038dSYoshinobu Inoue 		m_freem(control);
55233841545SHajimu UMEMOTO 	}
5530ecd976eSBjoern A. Zeeb 	INP_WUNLOCK(inp);
55482cd038dSYoshinobu Inoue 	return (error);
555e0b40500SGleb Smirnoff 
556e0b40500SGleb Smirnoff release:
557e0b40500SGleb Smirnoff 	if (control != NULL)
558e0b40500SGleb Smirnoff 		m_freem(control);
559e0b40500SGleb Smirnoff 	m_freem(m);
560e0b40500SGleb Smirnoff 	return (error);
56182cd038dSYoshinobu Inoue }
56282cd038dSYoshinobu Inoue 
56382cd038dSYoshinobu Inoue /*
56482cd038dSYoshinobu Inoue  * Raw IPv6 socket option processing.
56582cd038dSYoshinobu Inoue  */
56682cd038dSYoshinobu Inoue int
5671272577eSXin LI rip6_ctloutput(struct socket *so, struct sockopt *sopt)
56882cd038dSYoshinobu Inoue {
56974ed2e8aSGleb Smirnoff 	struct inpcb *inp = sotoinpcb(so);
57082cd038dSYoshinobu Inoue 	int error;
57182cd038dSYoshinobu Inoue 
57282cd038dSYoshinobu Inoue 	if (sopt->sopt_level == IPPROTO_ICMPV6)
57382cd038dSYoshinobu Inoue 		/*
57482cd038dSYoshinobu Inoue 		 * XXX: is it better to call icmp6_ctloutput() directly
57582cd038dSYoshinobu Inoue 		 * from protosw?
57682cd038dSYoshinobu Inoue 		 */
57782cd038dSYoshinobu Inoue 		return (icmp6_ctloutput(so, sopt));
578ee799639SBjoern A. Zeeb 	else if (sopt->sopt_level != IPPROTO_IPV6) {
579ee799639SBjoern A. Zeeb 		if (sopt->sopt_level == SOL_SOCKET &&
580ee799639SBjoern A. Zeeb 		    sopt->sopt_name == SO_SETFIB) {
581ee799639SBjoern A. Zeeb 			INP_WLOCK(inp);
582ee799639SBjoern A. Zeeb 			inp->inp_inc.inc_fibnum = so->so_fibnum;
583ee799639SBjoern A. Zeeb 			INP_WUNLOCK(inp);
584ee799639SBjoern A. Zeeb 			return (0);
585ee799639SBjoern A. Zeeb 		}
58682cd038dSYoshinobu Inoue 		return (EINVAL);
587ee799639SBjoern A. Zeeb 	}
58882cd038dSYoshinobu Inoue 
58982cd038dSYoshinobu Inoue 	error = 0;
59082cd038dSYoshinobu Inoue 
59182cd038dSYoshinobu Inoue 	switch (sopt->sopt_dir) {
59282cd038dSYoshinobu Inoue 	case SOPT_GET:
59382cd038dSYoshinobu Inoue 		switch (sopt->sopt_name) {
59491ec0a1eSYoshinobu Inoue 		case MRT6_INIT:
59591ec0a1eSYoshinobu Inoue 		case MRT6_DONE:
59691ec0a1eSYoshinobu Inoue 		case MRT6_ADD_MIF:
59791ec0a1eSYoshinobu Inoue 		case MRT6_DEL_MIF:
59891ec0a1eSYoshinobu Inoue 		case MRT6_ADD_MFC:
59991ec0a1eSYoshinobu Inoue 		case MRT6_DEL_MFC:
60091ec0a1eSYoshinobu Inoue 		case MRT6_PIM:
60174ed2e8aSGleb Smirnoff 			if (inp->inp_ip_p != IPPROTO_ICMPV6)
60274ed2e8aSGleb Smirnoff 				return (EOPNOTSUPP);
6036be2e366SBruce M Simpson 			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
6046be2e366SBruce M Simpson 			    EOPNOTSUPP;
60591ec0a1eSYoshinobu Inoue 			break;
60602b9a206SHajimu UMEMOTO 		case IPV6_CHECKSUM:
60702b9a206SHajimu UMEMOTO 			error = ip6_raw_ctloutput(so, sopt);
60802b9a206SHajimu UMEMOTO 			break;
60982cd038dSYoshinobu Inoue 		default:
61082cd038dSYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
61182cd038dSYoshinobu Inoue 			break;
61282cd038dSYoshinobu Inoue 		}
61382cd038dSYoshinobu Inoue 		break;
61482cd038dSYoshinobu Inoue 
61582cd038dSYoshinobu Inoue 	case SOPT_SET:
61682cd038dSYoshinobu Inoue 		switch (sopt->sopt_name) {
61791ec0a1eSYoshinobu Inoue 		case MRT6_INIT:
61891ec0a1eSYoshinobu Inoue 		case MRT6_DONE:
61991ec0a1eSYoshinobu Inoue 		case MRT6_ADD_MIF:
62091ec0a1eSYoshinobu Inoue 		case MRT6_DEL_MIF:
62191ec0a1eSYoshinobu Inoue 		case MRT6_ADD_MFC:
62291ec0a1eSYoshinobu Inoue 		case MRT6_DEL_MFC:
62391ec0a1eSYoshinobu Inoue 		case MRT6_PIM:
62474ed2e8aSGleb Smirnoff 			if (inp->inp_ip_p != IPPROTO_ICMPV6)
62574ed2e8aSGleb Smirnoff 				return (EOPNOTSUPP);
6266be2e366SBruce M Simpson 			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
6276be2e366SBruce M Simpson 			    EOPNOTSUPP;
62891ec0a1eSYoshinobu Inoue 			break;
62902b9a206SHajimu UMEMOTO 		case IPV6_CHECKSUM:
63002b9a206SHajimu UMEMOTO 			error = ip6_raw_ctloutput(so, sopt);
63102b9a206SHajimu UMEMOTO 			break;
63282cd038dSYoshinobu Inoue 		default:
63382cd038dSYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
63482cd038dSYoshinobu Inoue 			break;
63582cd038dSYoshinobu Inoue 		}
63682cd038dSYoshinobu Inoue 		break;
63782cd038dSYoshinobu Inoue 	}
63882cd038dSYoshinobu Inoue 
63982cd038dSYoshinobu Inoue 	return (error);
64082cd038dSYoshinobu Inoue }
64182cd038dSYoshinobu Inoue 
64282cd038dSYoshinobu Inoue static int
643b40ce416SJulian Elischer rip6_attach(struct socket *so, int proto, struct thread *td)
64482cd038dSYoshinobu Inoue {
64582cd038dSYoshinobu Inoue 	struct inpcb *inp;
6468a9d54dfSSam Leffler 	struct icmp6_filter *filter;
647ff7425ceSRobert Watson 	int error;
64882cd038dSYoshinobu Inoue 
64982cd038dSYoshinobu Inoue 	inp = sotoinpcb(so);
65014ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
6510ae76120SRobert Watson 
65279ba3952SBjoern A. Zeeb 	error = priv_check(td, PRIV_NETINET_RAW);
65379ba3952SBjoern A. Zeeb 	if (error)
6540ae76120SRobert Watson 		return (error);
65561f7427fSGleb Smirnoff 	if (proto >= IPPROTO_MAX || proto < 0)
65661f7427fSGleb Smirnoff 		return (EPROTONOSUPPORT);
65782cd038dSYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
65814ba8addSRobert Watson 	if (error)
6590ae76120SRobert Watson 		return (error);
6601ede983cSDag-Erling Smørgrav 	filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
66114ba8addSRobert Watson 	if (filter == NULL)
6620ae76120SRobert Watson 		return (ENOMEM);
663603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
66407385abdSRobert Watson 	if (error) {
6651ede983cSDag-Erling Smørgrav 		free(filter, M_PCB);
6660ae76120SRobert Watson 		return (error);
66707385abdSRobert Watson 	}
66882cd038dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
66961f7427fSGleb Smirnoff 	inp->inp_ip_p = proto;
67082cd038dSYoshinobu Inoue 	inp->in6p_cksum = -1;
6718a9d54dfSSam Leffler 	inp->in6p_icmp6filt = filter;
67282cd038dSYoshinobu Inoue 	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
6738501a69cSRobert Watson 	INP_WUNLOCK(inp);
6740ae76120SRobert Watson 	return (0);
67582cd038dSYoshinobu Inoue }
67682cd038dSYoshinobu Inoue 
677bc725eafSRobert Watson static void
67882cd038dSYoshinobu Inoue rip6_detach(struct socket *so)
67982cd038dSYoshinobu Inoue {
68082cd038dSYoshinobu Inoue 	struct inpcb *inp;
68182cd038dSYoshinobu Inoue 
68282cd038dSYoshinobu Inoue 	inp = sotoinpcb(so);
68314ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
684a152f8a3SRobert Watson 
68533cde130SBruce M Simpson 	if (so == V_ip6_mrouter && ip6_mrouter_done)
686e7e3ecb6SYoshinobu Inoue 		ip6_mrouter_done();
6876be2e366SBruce M Simpson 	/* xxx: RSVP */
6888501a69cSRobert Watson 	INP_WLOCK(inp);
6891ede983cSDag-Erling Smørgrav 	free(inp->in6p_icmp6filt, M_PCB);
6906aee2fc5SBjoern A. Zeeb 	in_pcbfree(inp);
69182cd038dSYoshinobu Inoue }
69282cd038dSYoshinobu Inoue 
693a152f8a3SRobert Watson /* XXXRW: This can't ever be called. */
694ac45e92fSRobert Watson static void
69582cd038dSYoshinobu Inoue rip6_abort(struct socket *so)
69682cd038dSYoshinobu Inoue {
697a98bb75fSJohn Baldwin 	struct inpcb *inp __diagused;
698a152f8a3SRobert Watson 
699a152f8a3SRobert Watson 	inp = sotoinpcb(so);
700a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
701a152f8a3SRobert Watson 
70282cd038dSYoshinobu Inoue 	soisdisconnected(so);
703a152f8a3SRobert Watson }
704a152f8a3SRobert Watson 
705a152f8a3SRobert Watson static void
706a152f8a3SRobert Watson rip6_close(struct socket *so)
707a152f8a3SRobert Watson {
708a98bb75fSJohn Baldwin 	struct inpcb *inp __diagused;
709a152f8a3SRobert Watson 
710a152f8a3SRobert Watson 	inp = sotoinpcb(so);
711a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
712a152f8a3SRobert Watson 
713a152f8a3SRobert Watson 	soisdisconnected(so);
71482cd038dSYoshinobu Inoue }
71582cd038dSYoshinobu Inoue 
71682cd038dSYoshinobu Inoue static int
71782cd038dSYoshinobu Inoue rip6_disconnect(struct socket *so)
71882cd038dSYoshinobu Inoue {
7190ae76120SRobert Watson 	struct inpcb *inp;
7200ae76120SRobert Watson 
7210ae76120SRobert Watson 	inp = sotoinpcb(so);
7220ae76120SRobert Watson 	KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
72382cd038dSYoshinobu Inoue 
7244cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
7250ae76120SRobert Watson 		return (ENOTCONN);
72682cd038dSYoshinobu Inoue 	inp->in6p_faddr = in6addr_any;
727ac45e92fSRobert Watson 	rip6_abort(so);
72814ba8addSRobert Watson 	return (0);
72982cd038dSYoshinobu Inoue }
73082cd038dSYoshinobu Inoue 
73182cd038dSYoshinobu Inoue static int
732b40ce416SJulian Elischer rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
73382cd038dSYoshinobu Inoue {
7343af7f97cSGleb Smirnoff 	struct epoch_tracker et;
7350ae76120SRobert Watson 	struct inpcb *inp;
73682cd038dSYoshinobu Inoue 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
7378c0fec80SRobert Watson 	struct ifaddr *ifa = NULL;
738a1f7e5f8SHajimu UMEMOTO 	int error = 0;
73982cd038dSYoshinobu Inoue 
7400ae76120SRobert Watson 	inp = sotoinpcb(so);
74114ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
7420ae76120SRobert Watson 
743f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
744f161d294SMark Johnston 		return (EAFNOSUPPORT);
74582cd038dSYoshinobu Inoue 	if (nam->sa_len != sizeof(*addr))
7460ae76120SRobert Watson 		return (EINVAL);
747b89e82ddSJamie Gritton 	if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
748b89e82ddSJamie Gritton 		return (error);
7494f6c66ccSMatt Macy 	if (CK_STAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
7500ae76120SRobert Watson 		return (EADDRNOTAVAIL);
751603724d3SBjoern A. Zeeb 	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
752a1f7e5f8SHajimu UMEMOTO 		return (error);
753a1f7e5f8SHajimu UMEMOTO 
7543af7f97cSGleb Smirnoff 	NET_EPOCH_ENTER(et);
75582cd038dSYoshinobu Inoue 	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
7563af7f97cSGleb Smirnoff 	    (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) {
7573af7f97cSGleb Smirnoff 		NET_EPOCH_EXIT(et);
7580ae76120SRobert Watson 		return (EADDRNOTAVAIL);
7593af7f97cSGleb Smirnoff 	}
7608c0fec80SRobert Watson 	if (ifa != NULL &&
7618c0fec80SRobert Watson 	    ((struct in6_ifaddr *)ifa)->ia6_flags &
76282cd038dSYoshinobu Inoue 	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
7633af7f97cSGleb Smirnoff 	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
7643af7f97cSGleb Smirnoff 		NET_EPOCH_EXIT(et);
76582cd038dSYoshinobu Inoue 		return (EADDRNOTAVAIL);
7663af7f97cSGleb Smirnoff 	}
7673af7f97cSGleb Smirnoff 	NET_EPOCH_EXIT(et);
7688501a69cSRobert Watson 	INP_WLOCK(inp);
76982cd038dSYoshinobu Inoue 	inp->in6p_laddr = addr->sin6_addr;
770a14465e1SMark Johnston 	INP_WUNLOCK(inp);
7710ae76120SRobert Watson 	return (0);
77282cd038dSYoshinobu Inoue }
77382cd038dSYoshinobu Inoue 
77482cd038dSYoshinobu Inoue static int
775b40ce416SJulian Elischer rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
77682cd038dSYoshinobu Inoue {
7770ae76120SRobert Watson 	struct inpcb *inp;
77882cd038dSYoshinobu Inoue 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
77988d166bfSBjoern A. Zeeb 	struct in6_addr in6a;
780605284b8SAlexander V. Chernikov 	struct epoch_tracker et;
781a1f7e5f8SHajimu UMEMOTO 	int error = 0, scope_ambiguous = 0;
78282cd038dSYoshinobu Inoue 
7830ae76120SRobert Watson 	inp = sotoinpcb(so);
78414ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
7850ae76120SRobert Watson 
78682cd038dSYoshinobu Inoue 	if (nam->sa_len != sizeof(*addr))
7870ae76120SRobert Watson 		return (EINVAL);
7884f6c66ccSMatt Macy 	if (CK_STAILQ_EMPTY(&V_ifnet))
7890ae76120SRobert Watson 		return (EADDRNOTAVAIL);
79082cd038dSYoshinobu Inoue 	if (addr->sin6_family != AF_INET6)
7910ae76120SRobert Watson 		return (EAFNOSUPPORT);
792a1f7e5f8SHajimu UMEMOTO 
793a1f7e5f8SHajimu UMEMOTO 	/*
7940ae76120SRobert Watson 	 * Application should provide a proper zone ID or the use of default
7950ae76120SRobert Watson 	 * zone IDs should be enabled.  Unfortunately, some applications do
7960ae76120SRobert Watson 	 * not behave as it should, so we need a workaround.  Even if an
7970ae76120SRobert Watson 	 * appropriate ID is not determined, we'll see if we can determine
7980ae76120SRobert Watson 	 * the outgoing interface.  If we can, determine the zone ID based on
7990ae76120SRobert Watson 	 * the interface below.
800a1f7e5f8SHajimu UMEMOTO 	 */
801603724d3SBjoern A. Zeeb 	if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
802a1f7e5f8SHajimu UMEMOTO 		scope_ambiguous = 1;
803603724d3SBjoern A. Zeeb 	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
804a1f7e5f8SHajimu UMEMOTO 		return (error);
805a1f7e5f8SHajimu UMEMOTO 
8068501a69cSRobert Watson 	INP_WLOCK(inp);
80782cd038dSYoshinobu Inoue 	/* Source address selection. XXX: need pcblookup? */
808605284b8SAlexander V. Chernikov 	NET_EPOCH_ENTER(et);
809601c0b8bSAlexander V. Chernikov 	error = in6_selectsrc_socket(addr, inp->in6p_outputopts,
810601c0b8bSAlexander V. Chernikov 	    inp, so->so_cred, scope_ambiguous, &in6a, NULL);
811605284b8SAlexander V. Chernikov 	NET_EPOCH_EXIT(et);
81288d166bfSBjoern A. Zeeb 	if (error) {
81371a1539eSMateusz Guzik 		INP_WUNLOCK(inp);
81488d166bfSBjoern A. Zeeb 		return (error);
81507385abdSRobert Watson 	}
816a1f7e5f8SHajimu UMEMOTO 
81782cd038dSYoshinobu Inoue 	inp->in6p_faddr = addr->sin6_addr;
81888d166bfSBjoern A. Zeeb 	inp->in6p_laddr = in6a;
81982cd038dSYoshinobu Inoue 	soisconnected(so);
82071a1539eSMateusz Guzik 	INP_WUNLOCK(inp);
8210ae76120SRobert Watson 	return (0);
82282cd038dSYoshinobu Inoue }
82382cd038dSYoshinobu Inoue 
82482cd038dSYoshinobu Inoue static int
8255bba2728SGleb Smirnoff rip6_shutdown(struct socket *so, enum shutdown_how how)
82682cd038dSYoshinobu Inoue {
82707385abdSRobert Watson 
8285bba2728SGleb Smirnoff 	SOCK_LOCK(so);
8295bba2728SGleb Smirnoff 	if (!(so->so_state & SS_ISCONNECTED)) {
8305bba2728SGleb Smirnoff 		SOCK_UNLOCK(so);
8315bba2728SGleb Smirnoff 		return (ENOTCONN);
8325bba2728SGleb Smirnoff 	}
8335bba2728SGleb Smirnoff 	SOCK_UNLOCK(so);
8340ae76120SRobert Watson 
8355bba2728SGleb Smirnoff 	switch (how) {
8365bba2728SGleb Smirnoff 	case SHUT_RD:
837ce69e373SGleb Smirnoff 		sorflush(so);
8385bba2728SGleb Smirnoff 		break;
8395bba2728SGleb Smirnoff 	case SHUT_RDWR:
840ce69e373SGleb Smirnoff 		sorflush(so);
8415bba2728SGleb Smirnoff 		/* FALLTHROUGH */
8425bba2728SGleb Smirnoff 	case SHUT_WR:
84382cd038dSYoshinobu Inoue 		socantsendmore(so);
8445bba2728SGleb Smirnoff 	}
8455bba2728SGleb Smirnoff 
8460ae76120SRobert Watson 	return (0);
84782cd038dSYoshinobu Inoue }
84882cd038dSYoshinobu Inoue 
849e7d02be1SGleb Smirnoff struct protosw rip6_protosw = {
85061f7427fSGleb Smirnoff 	.pr_type =		SOCK_RAW,
85161f7427fSGleb Smirnoff 	.pr_flags =		PR_ATOMIC|PR_ADDR,
85261f7427fSGleb Smirnoff 	.pr_ctloutput =		rip6_ctloutput,
85361f7427fSGleb Smirnoff 	.pr_abort =		rip6_abort,
85461f7427fSGleb Smirnoff 	.pr_attach =		rip6_attach,
85561f7427fSGleb Smirnoff 	.pr_bind =		rip6_bind,
85661f7427fSGleb Smirnoff 	.pr_connect =		rip6_connect,
85761f7427fSGleb Smirnoff 	.pr_control =		in6_control,
85861f7427fSGleb Smirnoff 	.pr_detach =		rip6_detach,
85961f7427fSGleb Smirnoff 	.pr_disconnect =	rip6_disconnect,
86061f7427fSGleb Smirnoff 	.pr_peeraddr =		in6_getpeeraddr,
86161f7427fSGleb Smirnoff 	.pr_send =		rip6_send,
86261f7427fSGleb Smirnoff 	.pr_shutdown =		rip6_shutdown,
86361f7427fSGleb Smirnoff 	.pr_sockaddr =		in6_getsockaddr,
86461f7427fSGleb Smirnoff 	.pr_close =		rip6_close
86582cd038dSYoshinobu Inoue };
866