xref: /freebsd/sys/netinet6/raw_ip6.c (revision 4009a98fe80b8a51837d471076152e6ff505b675)
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 
626a800098SYoshinobu Inoue #include "opt_ipsec.h"
6333841545SHajimu UMEMOTO #include "opt_inet6.h"
640c325f53SAlexander V. Chernikov #include "opt_route.h"
656a800098SYoshinobu Inoue 
6682cd038dSYoshinobu Inoue #include <sys/param.h>
6782cd038dSYoshinobu Inoue #include <sys/errno.h>
68413628a7SBjoern A. Zeeb #include <sys/jail.h>
69a786f679SAndrey V. Elsukov #include <sys/kernel.h>
70960ed29cSSeigo Tanimura #include <sys/lock.h>
71960ed29cSSeigo Tanimura #include <sys/malloc.h>
72960ed29cSSeigo Tanimura #include <sys/mbuf.h>
73c2259ba4SRobert Watson #include <sys/priv.h>
74960ed29cSSeigo Tanimura #include <sys/proc.h>
75960ed29cSSeigo Tanimura #include <sys/protosw.h>
76960ed29cSSeigo Tanimura #include <sys/signalvar.h>
77960ed29cSSeigo Tanimura #include <sys/socket.h>
78960ed29cSSeigo Tanimura #include <sys/socketvar.h>
79960ed29cSSeigo Tanimura #include <sys/sx.h>
80a1f7e5f8SHajimu UMEMOTO #include <sys/syslog.h>
8182cd038dSYoshinobu Inoue 
8282cd038dSYoshinobu Inoue #include <net/if.h>
8376039bc8SGleb Smirnoff #include <net/if_var.h>
843d0d5b21SJustin Hibbits #include <net/if_private.h>
8582cd038dSYoshinobu Inoue #include <net/if_types.h>
86960ed29cSSeigo Tanimura #include <net/route.h>
874b79449eSBjoern A. Zeeb #include <net/vnet.h>
8882cd038dSYoshinobu Inoue 
8982cd038dSYoshinobu Inoue #include <netinet/in.h>
9082cd038dSYoshinobu Inoue #include <netinet/in_var.h>
9182cd038dSYoshinobu Inoue #include <netinet/in_systm.h>
9282cd038dSYoshinobu Inoue #include <netinet/in_pcb.h>
934b79449eSBjoern A. Zeeb 
944b79449eSBjoern A. Zeeb #include <netinet/icmp6.h>
95960ed29cSSeigo Tanimura #include <netinet/ip6.h>
961db8d1f8SAna Kukec #include <netinet/ip_var.h>
97960ed29cSSeigo Tanimura #include <netinet6/ip6_mroute.h>
98960ed29cSSeigo Tanimura #include <netinet6/in6_pcb.h>
99960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
100960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
101960ed29cSSeigo Tanimura #include <netinet6/raw_ip6.h>
1020c325f53SAlexander V. Chernikov #include <netinet6/in6_fib.h>
103686cdd19SJun-ichiro itojun Hagino #include <netinet6/scope6_var.h>
1041db8d1f8SAna Kukec #include <netinet6/send.h>
10582cd038dSYoshinobu Inoue 
106fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
107b9234fafSSam Leffler 
10882cd038dSYoshinobu Inoue #include <machine/stdarg.h>
10982cd038dSYoshinobu Inoue 
11082cd038dSYoshinobu Inoue #define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
11182cd038dSYoshinobu Inoue #define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
11282cd038dSYoshinobu Inoue 
11382cd038dSYoshinobu Inoue /*
11482cd038dSYoshinobu Inoue  * Raw interface to IP6 protocol.
11582cd038dSYoshinobu Inoue  */
11682cd038dSYoshinobu Inoue 
117eddfbb76SRobert Watson VNET_DECLARE(struct inpcbinfo, ripcbinfo);
1181e77c105SRobert Watson #define	V_ripcbinfo			VNET(ripcbinfo)
119eddfbb76SRobert Watson 
120*4009a98fSMark Johnston VNET_DECLARE(int, rip_bind_all_fibs);
121*4009a98fSMark Johnston #define	V_rip_bind_all_fibs	VNET(rip_bind_all_fibs)
122*4009a98fSMark Johnston 
12397021c24SMarko Zec extern u_long	rip_sendspace;
12497021c24SMarko Zec extern u_long	rip_recvspace;
12597021c24SMarko Zec 
126a786f679SAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct rip6stat, rip6stat);
127a786f679SAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(rip6stat);
128a786f679SAndrey V. Elsukov 
129a786f679SAndrey V. Elsukov #ifdef VIMAGE
130a786f679SAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(rip6stat);
131a786f679SAndrey V. Elsukov #endif /* VIMAGE */
13282cea7e6SBjoern A. Zeeb 
13382cd038dSYoshinobu Inoue /*
13433cde130SBruce M Simpson  * Hooks for multicast routing. They all default to NULL, so leave them not
13533cde130SBruce M Simpson  * initialized and rely on BSS being set to 0.
1366be2e366SBruce M Simpson  */
13733cde130SBruce M Simpson 
13833cde130SBruce M Simpson /*
13933cde130SBruce M Simpson  * The socket used to communicate with the multicast routing daemon.
14033cde130SBruce M Simpson  */
141eddfbb76SRobert Watson VNET_DEFINE(struct socket *, ip6_mrouter);
14233cde130SBruce M Simpson 
14333cde130SBruce M Simpson /*
14433cde130SBruce M Simpson  * The various mrouter functions.
14533cde130SBruce M Simpson  */
1466be2e366SBruce M Simpson int (*ip6_mrouter_set)(struct socket *, struct sockopt *);
1476be2e366SBruce M Simpson int (*ip6_mrouter_get)(struct socket *, struct sockopt *);
1486be2e366SBruce M Simpson int (*ip6_mrouter_done)(void);
1496be2e366SBruce M Simpson int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
150e40bae9aSRoman Divacky int (*mrt6_ioctl)(u_long, caddr_t);
1516be2e366SBruce M Simpson 
152db0ac6deSCy Schubert struct rip6_inp_match_ctx {
153db0ac6deSCy Schubert 	struct ip6_hdr *ip6;
154db0ac6deSCy Schubert 	int proto;
155db0ac6deSCy Schubert };
156db0ac6deSCy Schubert 
157db0ac6deSCy Schubert static bool
rip6_inp_match(const struct inpcb * inp,void * v)158db0ac6deSCy Schubert rip6_inp_match(const struct inpcb *inp, void *v)
159db0ac6deSCy Schubert {
160db0ac6deSCy Schubert 	struct rip6_inp_match_ctx *c = v;
161db0ac6deSCy Schubert 	struct ip6_hdr *ip6 = c->ip6;
162db0ac6deSCy Schubert 	int proto = c->proto;
163db0ac6deSCy Schubert 
164db0ac6deSCy Schubert 	/* XXX inp locking */
165db0ac6deSCy Schubert 	if ((inp->inp_vflag & INP_IPV6) == 0)
166db0ac6deSCy Schubert 		return (false);
167db0ac6deSCy Schubert 	if (inp->inp_ip_p && inp->inp_ip_p != proto)
168db0ac6deSCy Schubert 		return (false);
169db0ac6deSCy Schubert 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
170db0ac6deSCy Schubert 	    !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst))
171db0ac6deSCy Schubert 		return (false);
172db0ac6deSCy Schubert 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
173db0ac6deSCy Schubert 	    !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src))
174db0ac6deSCy Schubert 		return (false);
175db0ac6deSCy Schubert 
176db0ac6deSCy Schubert 	return (true);
177db0ac6deSCy Schubert }
178db0ac6deSCy Schubert 
1796be2e366SBruce M Simpson /*
1800ae76120SRobert Watson  * Setup generic address and protocol structures for raw_input routine, then
1810ae76120SRobert Watson  * pass them along with mbuf chain.
18282cd038dSYoshinobu Inoue  */
18382cd038dSYoshinobu Inoue int
rip6_input(struct mbuf ** mp,int * offp,int proto)1841272577eSXin LI rip6_input(struct mbuf **mp, int *offp, int proto)
18582cd038dSYoshinobu Inoue {
18633cde130SBruce M Simpson 	struct ifnet *ifp;
187db0ac6deSCy Schubert 	struct mbuf *n, *m = *mp;
1883e85b721SEd Maste 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1890ecd976eSBjoern A. Zeeb 	struct inpcb *inp;
19033841545SHajimu UMEMOTO 	struct mbuf *opts = NULL;
191af12e09eSHajimu UMEMOTO 	struct sockaddr_in6 fromsa;
192db0ac6deSCy Schubert 	struct rip6_inp_match_ctx ctx = { .ip6 = ip6, .proto = proto };
193db0ac6deSCy Schubert 	struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo,
194db0ac6deSCy Schubert 	    INPLOOKUP_RLOCKPCB, rip6_inp_match, &ctx);
195*4009a98fSMark Johnston 	int delivered = 0, fib;
196f42347c3SGleb Smirnoff 
197*4009a98fSMark Johnston 	M_ASSERTPKTHDR(m);
198f42347c3SGleb Smirnoff 	NET_EPOCH_ASSERT();
19982cd038dSYoshinobu Inoue 
200f1d7ebfeSAndrey V. Elsukov 	RIP6STAT_INC(rip6s_ipackets);
20133841545SHajimu UMEMOTO 
202dce33a45SErmal Luçi 	init_sin6(&fromsa, m, 0); /* general init */
20382cd038dSYoshinobu Inoue 
204*4009a98fSMark Johnston 	fib = M_GETFIB(m);
20533cde130SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
20633cde130SBruce M Simpson 
207db0ac6deSCy Schubert 	while ((inp = inp_next(&inpi)) != NULL) {
208db0ac6deSCy Schubert 		INP_RLOCK_ASSERT(inp);
2091ff6e7a8SBjoern A. Zeeb #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2101ff6e7a8SBjoern A. Zeeb 		/*
2111ff6e7a8SBjoern A. Zeeb 		 * Check AH/ESP integrity.
2121ff6e7a8SBjoern A. Zeeb 		 */
213db0ac6deSCy Schubert 		if (IPSEC_ENABLED(ipv6) &&
214db0ac6deSCy Schubert 		    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
2151ff6e7a8SBjoern A. Zeeb 			/* Do not inject data into pcb. */
216db0ac6deSCy Schubert 			continue;
2171ff6e7a8SBjoern A. Zeeb 		}
2181ff6e7a8SBjoern A. Zeeb #endif /* IPSEC */
219db0ac6deSCy Schubert 		if (jailed_without_vnet(inp->inp_cred) &&
220db0ac6deSCy Schubert 		    !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
221db0ac6deSCy Schubert 		    prison_check_ip6(inp->inp_cred, &ip6->ip6_dst) != 0)
22233cde130SBruce M Simpson 			/*
22333cde130SBruce M Simpson 			 * Allow raw socket in jail to receive multicast;
22433cde130SBruce M Simpson 			 * assume process had PRIV_NETINET_RAW at attach,
22533cde130SBruce M Simpson 			 * and fall through into normal filter path if so.
22633cde130SBruce M Simpson 			 */
227db0ac6deSCy Schubert 			continue;
228*4009a98fSMark Johnston 		if (V_rip_bind_all_fibs == 0 && fib != inp->inp_inc.inc_fibnum)
229*4009a98fSMark Johnston 			/*
230*4009a98fSMark Johnston 			 * Sockets bound to a specific FIB can only receive
231*4009a98fSMark Johnston 			 * packets from that FIB.
232*4009a98fSMark Johnston 			 */
233*4009a98fSMark Johnston 			continue;
2340ecd976eSBjoern A. Zeeb 		if (inp->in6p_cksum != -1) {
235f1d7ebfeSAndrey V. Elsukov 			RIP6STAT_INC(rip6s_isum);
2360ecd976eSBjoern A. Zeeb 			if (m->m_pkthdr.len - (*offp + inp->in6p_cksum) < 2 ||
23770a0f3dcSMichael Tuexen 			    in6_cksum(m, proto, *offp,
23882cd038dSYoshinobu Inoue 			    m->m_pkthdr.len - *offp)) {
239f1d7ebfeSAndrey V. Elsukov 				RIP6STAT_INC(rip6s_badsum);
240fb288770SMichael Tuexen 				/*
241fb288770SMichael Tuexen 				 * Drop the received message, don't send an
242fb288770SMichael Tuexen 				 * ICMP6 message. Set proto to IPPROTO_NONE
243fb288770SMichael Tuexen 				 * to achieve that.
244fb288770SMichael Tuexen 				 */
245db0ac6deSCy Schubert 				INP_RUNLOCK(inp);
246fb288770SMichael Tuexen 				proto = IPPROTO_NONE;
247db0ac6deSCy Schubert 				break;
24882cd038dSYoshinobu Inoue 			}
24933841545SHajimu UMEMOTO 		}
25033cde130SBruce M Simpson 		/*
25133cde130SBruce M Simpson 		 * If this raw socket has multicast state, and we
25233cde130SBruce M Simpson 		 * have received a multicast, check if this socket
25333cde130SBruce M Simpson 		 * should receive it, as multicast filtering is now
25433cde130SBruce M Simpson 		 * the responsibility of the transport layer.
25533cde130SBruce M Simpson 		 */
2560ecd976eSBjoern A. Zeeb 		if (inp->in6p_moptions &&
25733cde130SBruce M Simpson 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
2587ab5a5cdSBruce M Simpson 			/*
2597ab5a5cdSBruce M Simpson 			 * If the incoming datagram is for MLD, allow it
2607ab5a5cdSBruce M Simpson 			 * through unconditionally to the raw socket.
2617ab5a5cdSBruce M Simpson 			 *
2627ab5a5cdSBruce M Simpson 			 * Use the M_RTALERT_MLD flag to check for MLD
2637ab5a5cdSBruce M Simpson 			 * traffic without having to inspect the mbuf chain
2647ab5a5cdSBruce M Simpson 			 * more deeply, as all MLDv1/v2 host messages MUST
2657ab5a5cdSBruce M Simpson 			 * contain the Router Alert option.
2667ab5a5cdSBruce M Simpson 			 *
2677ab5a5cdSBruce M Simpson 			 * In the case of MLDv1, we may not have explicitly
2687ab5a5cdSBruce M Simpson 			 * joined the group, and may have set IFF_ALLMULTI
2697ab5a5cdSBruce M Simpson 			 * on the interface. im6o_mc_filter() may discard
2707ab5a5cdSBruce M Simpson 			 * control traffic we actually need to see.
2717ab5a5cdSBruce M Simpson 			 *
2727ab5a5cdSBruce M Simpson 			 * Userland multicast routing daemons should continue
2737ab5a5cdSBruce M Simpson 			 * filter the control traffic appropriately.
2747ab5a5cdSBruce M Simpson 			 */
27533cde130SBruce M Simpson 			int blocked;
27633cde130SBruce M Simpson 
2777ab5a5cdSBruce M Simpson 			blocked = MCAST_PASS;
2787ab5a5cdSBruce M Simpson 			if ((m->m_flags & M_RTALERT_MLD) == 0) {
2797ab5a5cdSBruce M Simpson 				struct sockaddr_in6 mcaddr;
2807ab5a5cdSBruce M Simpson 
28133cde130SBruce M Simpson 				bzero(&mcaddr, sizeof(struct sockaddr_in6));
28233cde130SBruce M Simpson 				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
28333cde130SBruce M Simpson 				mcaddr.sin6_family = AF_INET6;
28433cde130SBruce M Simpson 				mcaddr.sin6_addr = ip6->ip6_dst;
28533cde130SBruce M Simpson 
2860ecd976eSBjoern A. Zeeb 				blocked = im6o_mc_filter(inp->in6p_moptions,
2877ab5a5cdSBruce M Simpson 				    ifp,
28833cde130SBruce M Simpson 				    (struct sockaddr *)&mcaddr,
28933cde130SBruce M Simpson 				    (struct sockaddr *)&fromsa);
2907ab5a5cdSBruce M Simpson 			}
29133cde130SBruce M Simpson 			if (blocked != MCAST_PASS) {
29233cde130SBruce M Simpson 				IP6STAT_INC(ip6s_notmember);
293de2d4784SGleb Smirnoff 				continue;
294266f97b5SCy Schubert 			}
295db0ac6deSCy Schubert 		}
296db0ac6deSCy Schubert 		if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL)
297db0ac6deSCy Schubert 			continue;
298db0ac6deSCy Schubert 		if (inp->inp_flags & INP_CONTROLOPTS ||
299db0ac6deSCy Schubert 		    inp->inp_socket->so_options & SO_TIMESTAMP)
300db0ac6deSCy Schubert 			ip6_savecontrol(inp, n, &opts);
301db0ac6deSCy Schubert 		/* strip intermediate headers */
302db0ac6deSCy Schubert 		m_adj(n, *offp);
303db0ac6deSCy Schubert 		if (sbappendaddr(&inp->inp_socket->so_rcv,
304db0ac6deSCy Schubert 		    (struct sockaddr *)&fromsa, n, opts) == 0) {
305db0ac6deSCy Schubert 			soroverflow(inp->inp_socket);
306db0ac6deSCy Schubert 			m_freem(n);
30782cd038dSYoshinobu Inoue 			if (opts)
30882cd038dSYoshinobu Inoue 				m_freem(opts);
309f1d7ebfeSAndrey V. Elsukov 			RIP6STAT_INC(rip6s_fullsock);
31082cd038dSYoshinobu Inoue 		} else {
311db0ac6deSCy Schubert 			sorwakeup(inp->inp_socket);
312db0ac6deSCy Schubert 			delivered++;
313db0ac6deSCy Schubert 		}
314db0ac6deSCy Schubert 		opts = NULL;
315db0ac6deSCy Schubert 	}
316db0ac6deSCy Schubert 	if (delivered == 0) {
317f1d7ebfeSAndrey V. Elsukov 		RIP6STAT_INC(rip6s_nosock);
31833841545SHajimu UMEMOTO 		if (m->m_flags & M_MCAST)
319f1d7ebfeSAndrey V. Elsukov 			RIP6STAT_INC(rip6s_nosockmcast);
32082cd038dSYoshinobu Inoue 		if (proto == IPPROTO_NONE)
32182cd038dSYoshinobu Inoue 			m_freem(m);
32268e0e5a6SAndrey V. Elsukov 		else
32382cd038dSYoshinobu Inoue 			icmp6_error(m, ICMP6_PARAM_PROB,
32482cd038dSYoshinobu Inoue 			    ICMP6_PARAMPROB_NEXTHEADER,
32568e0e5a6SAndrey V. Elsukov 			    ip6_get_prevhdr(m, *offp));
3269cb8d207SAndrey V. Elsukov 		IP6STAT_DEC(ip6s_delivered);
327db0ac6deSCy Schubert 	} else
328db0ac6deSCy Schubert 		m_freem(m);
3290ae76120SRobert Watson 	return (IPPROTO_DONE);
33082cd038dSYoshinobu Inoue }
33182cd038dSYoshinobu Inoue 
332686cdd19SJun-ichiro itojun Hagino void
rip6_ctlinput(struct ip6ctlparam * ip6cp)333fcb3f813SGleb Smirnoff rip6_ctlinput(struct ip6ctlparam *ip6cp)
334686cdd19SJun-ichiro itojun Hagino {
335fcb3f813SGleb Smirnoff 	int errno;
336686cdd19SJun-ichiro itojun Hagino 
337fcb3f813SGleb Smirnoff 	if ((errno = icmp6_errmap(ip6cp->ip6c_icmp6)) != 0)
338fcb3f813SGleb Smirnoff 		in6_pcbnotify(&V_ripcbinfo, ip6cp->ip6c_finaldst, 0,
339fcb3f813SGleb Smirnoff 		    ip6cp->ip6c_src, 0, errno, ip6cp->ip6c_cmdarg,
340fcb3f813SGleb Smirnoff 		    in6_rtchange);
341686cdd19SJun-ichiro itojun Hagino }
342686cdd19SJun-ichiro itojun Hagino 
34382cd038dSYoshinobu Inoue /*
3440ae76120SRobert Watson  * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
3450ae76120SRobert Watson  * may have setup with control call.
34682cd038dSYoshinobu Inoue  */
347e0b40500SGleb Smirnoff static int
rip6_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)348e0b40500SGleb Smirnoff rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
349e0b40500SGleb Smirnoff     struct mbuf *control, struct thread *td)
35082cd038dSYoshinobu Inoue {
351b9555453SGleb Smirnoff 	struct epoch_tracker et;
3520ecd976eSBjoern A. Zeeb 	struct inpcb *inp;
353e0b40500SGleb Smirnoff 	struct sockaddr_in6 tmp, *dstsock;
354e0b40500SGleb Smirnoff 	struct m_tag *mtag;
355e0b40500SGleb Smirnoff 	struct ip6_hdr *ip6;
35682cd038dSYoshinobu Inoue 	u_int	plen = m->m_pkthdr.len;
357e07db7aaSHajimu UMEMOTO 	struct ip6_pktopts opt, *optp;
35882cd038dSYoshinobu Inoue 	struct ifnet *oifp = NULL;
359e0b40500SGleb Smirnoff 	int error;
36082cd038dSYoshinobu Inoue 	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
361a1f7e5f8SHajimu UMEMOTO 	int scope_ambiguous = 0;
362388288b2SHajimu UMEMOTO 	int use_defzone = 0;
363601c0b8bSAlexander V. Chernikov 	int hlim = 0;
36488d166bfSBjoern A. Zeeb 	struct in6_addr in6a;
36582cd038dSYoshinobu Inoue 
3660ecd976eSBjoern A. Zeeb 	inp = sotoinpcb(so);
367e0b40500SGleb Smirnoff 	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
368e0b40500SGleb Smirnoff 
369e0b40500SGleb Smirnoff 	/* Always copy sockaddr to avoid overwrites. */
370e0b40500SGleb Smirnoff 	/* Unlocked read. */
371e0b40500SGleb Smirnoff 	if (so->so_state & SS_ISCONNECTED) {
372e0b40500SGleb Smirnoff 		if (nam) {
373e0b40500SGleb Smirnoff 			error = EISCONN;
374e0b40500SGleb Smirnoff 			goto release;
375e0b40500SGleb Smirnoff 		}
376e0b40500SGleb Smirnoff 		tmp = (struct sockaddr_in6 ){
377e0b40500SGleb Smirnoff 			.sin6_family = AF_INET6,
378e0b40500SGleb Smirnoff 			.sin6_len = sizeof(struct sockaddr_in6),
379e0b40500SGleb Smirnoff 		};
380e0b40500SGleb Smirnoff 		INP_RLOCK(inp);
381e0b40500SGleb Smirnoff 		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
382e0b40500SGleb Smirnoff 		    sizeof(struct in6_addr));
383e0b40500SGleb Smirnoff 		INP_RUNLOCK(inp);
384e0b40500SGleb Smirnoff 		dstsock = &tmp;
385e0b40500SGleb Smirnoff 	} else {
386e0b40500SGleb Smirnoff 		if (nam == NULL)
387e0b40500SGleb Smirnoff 			error = ENOTCONN;
388e0b40500SGleb Smirnoff 		else if (nam->sa_family != AF_INET6)
389e0b40500SGleb Smirnoff 			error = EAFNOSUPPORT;
390e0b40500SGleb Smirnoff 		else if (nam->sa_len != sizeof(struct sockaddr_in6))
391e0b40500SGleb Smirnoff 			error = EINVAL;
392e0b40500SGleb Smirnoff 		else
393e0b40500SGleb Smirnoff 			error = 0;
394e0b40500SGleb Smirnoff 		if (error != 0)
395e0b40500SGleb Smirnoff 			goto release;
396e0b40500SGleb Smirnoff 		dstsock = (struct sockaddr_in6 *)nam;
397e0b40500SGleb Smirnoff 		if (dstsock->sin6_family != AF_INET6) {
398e0b40500SGleb Smirnoff 			error = EAFNOSUPPORT;
399e0b40500SGleb Smirnoff 			goto release;
400e0b40500SGleb Smirnoff 		}
401e0b40500SGleb Smirnoff 	}
402e0b40500SGleb Smirnoff 
4030ecd976eSBjoern A. Zeeb 	INP_WLOCK(inp);
40482cd038dSYoshinobu Inoue 
40575bab8b8SKip Macy 	if (control != NULL) {
4062290dfb4SRyan Stone 		NET_EPOCH_ENTER(et);
40761f7427fSGleb Smirnoff 		error = ip6_setpktopts(control, &opt, inp->in6p_outputopts,
40861f7427fSGleb Smirnoff 		    so->so_cred, inp->inp_ip_p);
4092290dfb4SRyan Stone 		NET_EPOCH_EXIT(et);
4102290dfb4SRyan Stone 
4112290dfb4SRyan Stone 		if (error != 0) {
41282cd038dSYoshinobu Inoue 			goto bad;
413f95d4633SHajimu UMEMOTO 		}
414e07db7aaSHajimu UMEMOTO 		optp = &opt;
415e07db7aaSHajimu UMEMOTO 	} else
4160ecd976eSBjoern A. Zeeb 		optp = inp->in6p_outputopts;
41782cd038dSYoshinobu Inoue 
41882cd038dSYoshinobu Inoue 	/*
419a1f7e5f8SHajimu UMEMOTO 	 * Check and convert scope zone ID into internal form.
4200ae76120SRobert Watson 	 *
421a1f7e5f8SHajimu UMEMOTO 	 * XXX: we may still need to determine the zone later.
422a1f7e5f8SHajimu UMEMOTO 	 */
423a1f7e5f8SHajimu UMEMOTO 	if (!(so->so_state & SS_ISCONNECTED)) {
424365ccde0SHajimu UMEMOTO 		if (!optp || !optp->ip6po_pktinfo ||
425365ccde0SHajimu UMEMOTO 		    !optp->ip6po_pktinfo->ipi6_ifindex)
426388288b2SHajimu UMEMOTO 			use_defzone = V_ip6_use_defzone;
427388288b2SHajimu UMEMOTO 		if (dstsock->sin6_scope_id == 0 && !use_defzone)
428a1f7e5f8SHajimu UMEMOTO 			scope_ambiguous = 1;
429388288b2SHajimu UMEMOTO 		if ((error = sa6_embedscope(dstsock, use_defzone)) != 0)
430a1f7e5f8SHajimu UMEMOTO 			goto bad;
431a1f7e5f8SHajimu UMEMOTO 	}
432a1f7e5f8SHajimu UMEMOTO 
433a1f7e5f8SHajimu UMEMOTO 	/*
4340ae76120SRobert Watson 	 * For an ICMPv6 packet, we should know its type and code to update
4350ae76120SRobert Watson 	 * statistics.
43682cd038dSYoshinobu Inoue 	 */
43761f7427fSGleb Smirnoff 	if (inp->inp_ip_p == IPPROTO_ICMPV6) {
43882cd038dSYoshinobu Inoue 		struct icmp6_hdr *icmp6;
43982cd038dSYoshinobu Inoue 		if (m->m_len < sizeof(struct icmp6_hdr) &&
44082cd038dSYoshinobu Inoue 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
44182cd038dSYoshinobu Inoue 			error = ENOBUFS;
44282cd038dSYoshinobu Inoue 			goto bad;
44382cd038dSYoshinobu Inoue 		}
44482cd038dSYoshinobu Inoue 		icmp6 = mtod(m, struct icmp6_hdr *);
44582cd038dSYoshinobu Inoue 		type = icmp6->icmp6_type;
44682cd038dSYoshinobu Inoue 		code = icmp6->icmp6_code;
44782cd038dSYoshinobu Inoue 	}
44882cd038dSYoshinobu Inoue 
449eb1b1807SGleb Smirnoff 	M_PREPEND(m, sizeof(*ip6), M_NOWAIT);
4508a0c4da8SRobert Watson 	if (m == NULL) {
4518a0c4da8SRobert Watson 		error = ENOBUFS;
4528a0c4da8SRobert Watson 		goto bad;
4538a0c4da8SRobert Watson 	}
45482cd038dSYoshinobu Inoue 	ip6 = mtod(m, struct ip6_hdr *);
45582cd038dSYoshinobu Inoue 
4560c325f53SAlexander V. Chernikov #ifdef ROUTE_MPATH
4570c325f53SAlexander V. Chernikov 	if (CALC_FLOWID_OUTBOUND) {
4580c325f53SAlexander V. Chernikov 		uint32_t hash_type, hash_val;
4590c325f53SAlexander V. Chernikov 
4600c325f53SAlexander V. Chernikov 		hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
46161f7427fSGleb Smirnoff 		    &dstsock->sin6_addr, 0, 0, inp->inp_ip_p, &hash_type);
4620c325f53SAlexander V. Chernikov 		inp->inp_flowid = hash_val;
4630c325f53SAlexander V. Chernikov 		inp->inp_flowtype = hash_type;
4640c325f53SAlexander V. Chernikov 	}
4650c325f53SAlexander V. Chernikov #endif
46682cd038dSYoshinobu Inoue 	/*
46782cd038dSYoshinobu Inoue 	 * Source address selection.
46882cd038dSYoshinobu Inoue 	 */
469605284b8SAlexander V. Chernikov 	NET_EPOCH_ENTER(et);
4700ecd976eSBjoern A. Zeeb 	error = in6_selectsrc_socket(dstsock, optp, inp, so->so_cred,
471601c0b8bSAlexander V. Chernikov 	    scope_ambiguous, &in6a, &hlim);
472605284b8SAlexander V. Chernikov 	NET_EPOCH_EXIT(et);
473601c0b8bSAlexander V. Chernikov 
47488d166bfSBjoern A. Zeeb 	if (error)
47582cd038dSYoshinobu Inoue 		goto bad;
4760ecd976eSBjoern A. Zeeb 	error = prison_check_ip6(inp->inp_cred, &in6a);
477b89e82ddSJamie Gritton 	if (error != 0)
478413628a7SBjoern A. Zeeb 		goto bad;
47988d166bfSBjoern A. Zeeb 	ip6->ip6_src = in6a;
480a1f7e5f8SHajimu UMEMOTO 
481a1f7e5f8SHajimu UMEMOTO 	ip6->ip6_dst = dstsock->sin6_addr;
482a1f7e5f8SHajimu UMEMOTO 
4830ae76120SRobert Watson 	/*
4840ae76120SRobert Watson 	 * Fill in the rest of the IPv6 header fields.
4850ae76120SRobert Watson 	 */
4866a800098SYoshinobu Inoue 	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
4870ecd976eSBjoern A. Zeeb 	    (inp->inp_flow & IPV6_FLOWINFO_MASK);
4886a800098SYoshinobu Inoue 	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
4896a800098SYoshinobu Inoue 	    (IPV6_VERSION & IPV6_VERSION_MASK);
4900ae76120SRobert Watson 
4910ae76120SRobert Watson 	/*
4920ae76120SRobert Watson 	 * ip6_plen will be filled in ip6_output, so not fill it here.
4930ae76120SRobert Watson 	 */
4940ecd976eSBjoern A. Zeeb 	ip6->ip6_nxt = inp->inp_ip_p;
495601c0b8bSAlexander V. Chernikov 	ip6->ip6_hlim = hlim;
49682cd038dSYoshinobu Inoue 
49761f7427fSGleb Smirnoff 	if (inp->inp_ip_p == IPPROTO_ICMPV6 || inp->in6p_cksum != -1) {
49882cd038dSYoshinobu Inoue 		struct mbuf *n;
49982cd038dSYoshinobu Inoue 		int off;
50082cd038dSYoshinobu Inoue 		u_int16_t *p;
50182cd038dSYoshinobu Inoue 
5020ae76120SRobert Watson 		/* Compute checksum. */
50361f7427fSGleb Smirnoff 		if (inp->inp_ip_p == IPPROTO_ICMPV6)
50482cd038dSYoshinobu Inoue 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
50582cd038dSYoshinobu Inoue 		else
5060ecd976eSBjoern A. Zeeb 			off = inp->in6p_cksum;
507ae7c65b1SMichael Tuexen 		if (plen < off + 2) {
50882cd038dSYoshinobu Inoue 			error = EINVAL;
50982cd038dSYoshinobu Inoue 			goto bad;
51082cd038dSYoshinobu Inoue 		}
51182cd038dSYoshinobu Inoue 		off += sizeof(struct ip6_hdr);
51282cd038dSYoshinobu Inoue 
51382cd038dSYoshinobu Inoue 		n = m;
51482cd038dSYoshinobu Inoue 		while (n && n->m_len <= off) {
51582cd038dSYoshinobu Inoue 			off -= n->m_len;
51682cd038dSYoshinobu Inoue 			n = n->m_next;
51782cd038dSYoshinobu Inoue 		}
51882cd038dSYoshinobu Inoue 		if (!n)
51982cd038dSYoshinobu Inoue 			goto bad;
52082cd038dSYoshinobu Inoue 		p = (u_int16_t *)(mtod(n, caddr_t) + off);
52182cd038dSYoshinobu Inoue 		*p = 0;
52282cd038dSYoshinobu Inoue 		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
52382cd038dSYoshinobu Inoue 	}
52482cd038dSYoshinobu Inoue 
5251db8d1f8SAna Kukec 	/*
5261db8d1f8SAna Kukec 	 * Send RA/RS messages to user land for protection, before sending
5271db8d1f8SAna Kukec 	 * them to rtadvd/rtsol.
5281db8d1f8SAna Kukec 	 */
5291db8d1f8SAna Kukec 	if ((send_sendso_input_hook != NULL) &&
53061f7427fSGleb Smirnoff 	    inp->inp_ip_p == IPPROTO_ICMPV6) {
5311db8d1f8SAna Kukec 		switch (type) {
5321db8d1f8SAna Kukec 		case ND_ROUTER_ADVERT:
5331db8d1f8SAna Kukec 		case ND_ROUTER_SOLICIT:
5341db8d1f8SAna Kukec 			mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
5351db8d1f8SAna Kukec 				sizeof(unsigned short), M_NOWAIT);
5361db8d1f8SAna Kukec 			if (mtag == NULL)
5371db8d1f8SAna Kukec 				goto bad;
5381db8d1f8SAna Kukec 			m_tag_prepend(m, mtag);
5391db8d1f8SAna Kukec 		}
5401db8d1f8SAna Kukec 	}
5411db8d1f8SAna Kukec 
542b9555453SGleb Smirnoff 	NET_EPOCH_ENTER(et);
5430ecd976eSBjoern A. Zeeb 	error = ip6_output(m, optp, NULL, 0, inp->in6p_moptions, &oifp, inp);
544b9555453SGleb Smirnoff 	NET_EPOCH_EXIT(et);
54561f7427fSGleb Smirnoff 	if (inp->inp_ip_p == IPPROTO_ICMPV6) {
54682cd038dSYoshinobu Inoue 		if (oifp)
54782cd038dSYoshinobu Inoue 			icmp6_ifoutstat_inc(oifp, type, code);
54860d8dbbeSKristof Provost 		ICMP6STAT_INC2(icp6s_outhist, type);
54933841545SHajimu UMEMOTO 	} else
550f1d7ebfeSAndrey V. Elsukov 		RIP6STAT_INC(rip6s_opackets);
55182cd038dSYoshinobu Inoue 
55282cd038dSYoshinobu Inoue 	goto freectl;
55382cd038dSYoshinobu Inoue 
55482cd038dSYoshinobu Inoue  bad:
55582cd038dSYoshinobu Inoue 	if (m)
55682cd038dSYoshinobu Inoue 		m_freem(m);
55782cd038dSYoshinobu Inoue 
55882cd038dSYoshinobu Inoue  freectl:
55975bab8b8SKip Macy 	if (control != NULL) {
560e07db7aaSHajimu UMEMOTO 		ip6_clearpktopts(&opt, -1);
56182cd038dSYoshinobu Inoue 		m_freem(control);
56233841545SHajimu UMEMOTO 	}
5630ecd976eSBjoern A. Zeeb 	INP_WUNLOCK(inp);
56482cd038dSYoshinobu Inoue 	return (error);
565e0b40500SGleb Smirnoff 
566e0b40500SGleb Smirnoff release:
567e0b40500SGleb Smirnoff 	if (control != NULL)
568e0b40500SGleb Smirnoff 		m_freem(control);
569e0b40500SGleb Smirnoff 	m_freem(m);
570e0b40500SGleb Smirnoff 	return (error);
57182cd038dSYoshinobu Inoue }
57282cd038dSYoshinobu Inoue 
57382cd038dSYoshinobu Inoue /*
57482cd038dSYoshinobu Inoue  * Raw IPv6 socket option processing.
57582cd038dSYoshinobu Inoue  */
57682cd038dSYoshinobu Inoue int
rip6_ctloutput(struct socket * so,struct sockopt * sopt)5771272577eSXin LI rip6_ctloutput(struct socket *so, struct sockopt *sopt)
57882cd038dSYoshinobu Inoue {
57974ed2e8aSGleb Smirnoff 	struct inpcb *inp = sotoinpcb(so);
58082cd038dSYoshinobu Inoue 	int error;
58182cd038dSYoshinobu Inoue 
58282cd038dSYoshinobu Inoue 	if (sopt->sopt_level == IPPROTO_ICMPV6)
58382cd038dSYoshinobu Inoue 		/*
58482cd038dSYoshinobu Inoue 		 * XXX: is it better to call icmp6_ctloutput() directly
58582cd038dSYoshinobu Inoue 		 * from protosw?
58682cd038dSYoshinobu Inoue 		 */
58782cd038dSYoshinobu Inoue 		return (icmp6_ctloutput(so, sopt));
588ee799639SBjoern A. Zeeb 	else if (sopt->sopt_level != IPPROTO_IPV6) {
589caccbaefSMark Johnston 		if (sopt->sopt_dir == SOPT_SET &&
590caccbaefSMark Johnston 		    sopt->sopt_level == SOL_SOCKET &&
591caccbaefSMark Johnston 		    sopt->sopt_name == SO_SETFIB)
592caccbaefSMark Johnston 			return (ip6_ctloutput(so, sopt));
59382cd038dSYoshinobu Inoue 		return (EINVAL);
594ee799639SBjoern A. Zeeb 	}
59582cd038dSYoshinobu Inoue 
59682cd038dSYoshinobu Inoue 	error = 0;
59782cd038dSYoshinobu Inoue 
59882cd038dSYoshinobu Inoue 	switch (sopt->sopt_dir) {
59982cd038dSYoshinobu Inoue 	case SOPT_GET:
60082cd038dSYoshinobu Inoue 		switch (sopt->sopt_name) {
60191ec0a1eSYoshinobu Inoue 		case MRT6_INIT:
60291ec0a1eSYoshinobu Inoue 		case MRT6_DONE:
60391ec0a1eSYoshinobu Inoue 		case MRT6_ADD_MIF:
60491ec0a1eSYoshinobu Inoue 		case MRT6_DEL_MIF:
60591ec0a1eSYoshinobu Inoue 		case MRT6_ADD_MFC:
60691ec0a1eSYoshinobu Inoue 		case MRT6_DEL_MFC:
60791ec0a1eSYoshinobu Inoue 		case MRT6_PIM:
60874ed2e8aSGleb Smirnoff 			if (inp->inp_ip_p != IPPROTO_ICMPV6)
60974ed2e8aSGleb Smirnoff 				return (EOPNOTSUPP);
6106be2e366SBruce M Simpson 			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
6116be2e366SBruce M Simpson 			    EOPNOTSUPP;
61291ec0a1eSYoshinobu Inoue 			break;
61302b9a206SHajimu UMEMOTO 		case IPV6_CHECKSUM:
61402b9a206SHajimu UMEMOTO 			error = ip6_raw_ctloutput(so, sopt);
61502b9a206SHajimu UMEMOTO 			break;
61682cd038dSYoshinobu Inoue 		default:
61782cd038dSYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
61882cd038dSYoshinobu Inoue 			break;
61982cd038dSYoshinobu Inoue 		}
62082cd038dSYoshinobu Inoue 		break;
62182cd038dSYoshinobu Inoue 
62282cd038dSYoshinobu Inoue 	case SOPT_SET:
62382cd038dSYoshinobu Inoue 		switch (sopt->sopt_name) {
62491ec0a1eSYoshinobu Inoue 		case MRT6_INIT:
62591ec0a1eSYoshinobu Inoue 		case MRT6_DONE:
62691ec0a1eSYoshinobu Inoue 		case MRT6_ADD_MIF:
62791ec0a1eSYoshinobu Inoue 		case MRT6_DEL_MIF:
62891ec0a1eSYoshinobu Inoue 		case MRT6_ADD_MFC:
62991ec0a1eSYoshinobu Inoue 		case MRT6_DEL_MFC:
63091ec0a1eSYoshinobu Inoue 		case MRT6_PIM:
63174ed2e8aSGleb Smirnoff 			if (inp->inp_ip_p != IPPROTO_ICMPV6)
63274ed2e8aSGleb Smirnoff 				return (EOPNOTSUPP);
6336be2e366SBruce M Simpson 			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
6346be2e366SBruce M Simpson 			    EOPNOTSUPP;
63591ec0a1eSYoshinobu Inoue 			break;
63602b9a206SHajimu UMEMOTO 		case IPV6_CHECKSUM:
63702b9a206SHajimu UMEMOTO 			error = ip6_raw_ctloutput(so, sopt);
63802b9a206SHajimu UMEMOTO 			break;
63982cd038dSYoshinobu Inoue 		default:
64082cd038dSYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
64182cd038dSYoshinobu Inoue 			break;
64282cd038dSYoshinobu Inoue 		}
64382cd038dSYoshinobu Inoue 		break;
64482cd038dSYoshinobu Inoue 	}
64582cd038dSYoshinobu Inoue 
64682cd038dSYoshinobu Inoue 	return (error);
64782cd038dSYoshinobu Inoue }
64882cd038dSYoshinobu Inoue 
64982cd038dSYoshinobu Inoue static int
rip6_attach(struct socket * so,int proto,struct thread * td)650b40ce416SJulian Elischer rip6_attach(struct socket *so, int proto, struct thread *td)
65182cd038dSYoshinobu Inoue {
65282cd038dSYoshinobu Inoue 	struct inpcb *inp;
6538a9d54dfSSam Leffler 	struct icmp6_filter *filter;
654ff7425ceSRobert Watson 	int error;
65582cd038dSYoshinobu Inoue 
65682cd038dSYoshinobu Inoue 	inp = sotoinpcb(so);
65714ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
6580ae76120SRobert Watson 
65979ba3952SBjoern A. Zeeb 	error = priv_check(td, PRIV_NETINET_RAW);
66079ba3952SBjoern A. Zeeb 	if (error)
6610ae76120SRobert Watson 		return (error);
66261f7427fSGleb Smirnoff 	if (proto >= IPPROTO_MAX || proto < 0)
66361f7427fSGleb Smirnoff 		return (EPROTONOSUPPORT);
66482cd038dSYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
66514ba8addSRobert Watson 	if (error)
6660ae76120SRobert Watson 		return (error);
6671ede983cSDag-Erling Smørgrav 	filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
66814ba8addSRobert Watson 	if (filter == NULL)
6690ae76120SRobert Watson 		return (ENOMEM);
670603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
67107385abdSRobert Watson 	if (error) {
6721ede983cSDag-Erling Smørgrav 		free(filter, M_PCB);
6730ae76120SRobert Watson 		return (error);
67407385abdSRobert Watson 	}
67582cd038dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
67661f7427fSGleb Smirnoff 	inp->inp_ip_p = proto;
67782cd038dSYoshinobu Inoue 	inp->in6p_cksum = -1;
6788a9d54dfSSam Leffler 	inp->in6p_icmp6filt = filter;
67982cd038dSYoshinobu Inoue 	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
6808501a69cSRobert Watson 	INP_WUNLOCK(inp);
6810ae76120SRobert Watson 	return (0);
68282cd038dSYoshinobu Inoue }
68382cd038dSYoshinobu Inoue 
684bc725eafSRobert Watson static void
rip6_detach(struct socket * so)68582cd038dSYoshinobu Inoue rip6_detach(struct socket *so)
68682cd038dSYoshinobu Inoue {
68782cd038dSYoshinobu Inoue 	struct inpcb *inp;
68882cd038dSYoshinobu Inoue 
68982cd038dSYoshinobu Inoue 	inp = sotoinpcb(so);
69014ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
691a152f8a3SRobert Watson 
69233cde130SBruce M Simpson 	if (so == V_ip6_mrouter && ip6_mrouter_done)
693e7e3ecb6SYoshinobu Inoue 		ip6_mrouter_done();
6946be2e366SBruce M Simpson 	/* xxx: RSVP */
6958501a69cSRobert Watson 	INP_WLOCK(inp);
6961ede983cSDag-Erling Smørgrav 	free(inp->in6p_icmp6filt, M_PCB);
6976aee2fc5SBjoern A. Zeeb 	in_pcbfree(inp);
69882cd038dSYoshinobu Inoue }
69982cd038dSYoshinobu Inoue 
700a152f8a3SRobert Watson /* XXXRW: This can't ever be called. */
701ac45e92fSRobert Watson static void
rip6_abort(struct socket * so)70282cd038dSYoshinobu Inoue rip6_abort(struct socket *so)
70382cd038dSYoshinobu Inoue {
704a98bb75fSJohn Baldwin 	struct inpcb *inp __diagused;
705a152f8a3SRobert Watson 
706a152f8a3SRobert Watson 	inp = sotoinpcb(so);
707a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
708a152f8a3SRobert Watson 
70982cd038dSYoshinobu Inoue 	soisdisconnected(so);
710a152f8a3SRobert Watson }
711a152f8a3SRobert Watson 
712a152f8a3SRobert Watson static void
rip6_close(struct socket * so)713a152f8a3SRobert Watson rip6_close(struct socket *so)
714a152f8a3SRobert Watson {
715a98bb75fSJohn Baldwin 	struct inpcb *inp __diagused;
716a152f8a3SRobert Watson 
717a152f8a3SRobert Watson 	inp = sotoinpcb(so);
718a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
719a152f8a3SRobert Watson 
720a152f8a3SRobert Watson 	soisdisconnected(so);
72182cd038dSYoshinobu Inoue }
72282cd038dSYoshinobu Inoue 
72382cd038dSYoshinobu Inoue static int
rip6_disconnect(struct socket * so)72482cd038dSYoshinobu Inoue rip6_disconnect(struct socket *so)
72582cd038dSYoshinobu Inoue {
7260ae76120SRobert Watson 	struct inpcb *inp;
7270ae76120SRobert Watson 
7280ae76120SRobert Watson 	inp = sotoinpcb(so);
7290ae76120SRobert Watson 	KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
73082cd038dSYoshinobu Inoue 
7314cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
7320ae76120SRobert Watson 		return (ENOTCONN);
73382cd038dSYoshinobu Inoue 	inp->in6p_faddr = in6addr_any;
734ac45e92fSRobert Watson 	rip6_abort(so);
73514ba8addSRobert Watson 	return (0);
73682cd038dSYoshinobu Inoue }
73782cd038dSYoshinobu Inoue 
73882cd038dSYoshinobu Inoue static int
rip6_bind(struct socket * so,struct sockaddr * nam,struct thread * td)739b40ce416SJulian Elischer rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
74082cd038dSYoshinobu Inoue {
7413af7f97cSGleb Smirnoff 	struct epoch_tracker et;
7420ae76120SRobert Watson 	struct inpcb *inp;
74382cd038dSYoshinobu Inoue 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
7448c0fec80SRobert Watson 	struct ifaddr *ifa = NULL;
745a1f7e5f8SHajimu UMEMOTO 	int error = 0;
74682cd038dSYoshinobu Inoue 
7470ae76120SRobert Watson 	inp = sotoinpcb(so);
74814ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
7490ae76120SRobert Watson 
750f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
751f161d294SMark Johnston 		return (EAFNOSUPPORT);
75282cd038dSYoshinobu Inoue 	if (nam->sa_len != sizeof(*addr))
7530ae76120SRobert Watson 		return (EINVAL);
754b89e82ddSJamie Gritton 	if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
755b89e82ddSJamie Gritton 		return (error);
7564f6c66ccSMatt Macy 	if (CK_STAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
7570ae76120SRobert Watson 		return (EADDRNOTAVAIL);
758603724d3SBjoern A. Zeeb 	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
759a1f7e5f8SHajimu UMEMOTO 		return (error);
760a1f7e5f8SHajimu UMEMOTO 
7613af7f97cSGleb Smirnoff 	NET_EPOCH_ENTER(et);
76282cd038dSYoshinobu Inoue 	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
7633af7f97cSGleb Smirnoff 	    (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) {
7643af7f97cSGleb Smirnoff 		NET_EPOCH_EXIT(et);
7650ae76120SRobert Watson 		return (EADDRNOTAVAIL);
7663af7f97cSGleb Smirnoff 	}
7678c0fec80SRobert Watson 	if (ifa != NULL &&
7688c0fec80SRobert Watson 	    ((struct in6_ifaddr *)ifa)->ia6_flags &
76982cd038dSYoshinobu Inoue 	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
7703af7f97cSGleb Smirnoff 	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
7713af7f97cSGleb Smirnoff 		NET_EPOCH_EXIT(et);
77282cd038dSYoshinobu Inoue 		return (EADDRNOTAVAIL);
7733af7f97cSGleb Smirnoff 	}
7743af7f97cSGleb Smirnoff 	NET_EPOCH_EXIT(et);
7758501a69cSRobert Watson 	INP_WLOCK(inp);
77682cd038dSYoshinobu Inoue 	inp->in6p_laddr = addr->sin6_addr;
777a14465e1SMark Johnston 	INP_WUNLOCK(inp);
7780ae76120SRobert Watson 	return (0);
77982cd038dSYoshinobu Inoue }
78082cd038dSYoshinobu Inoue 
78182cd038dSYoshinobu Inoue static int
rip6_connect(struct socket * so,struct sockaddr * nam,struct thread * td)782b40ce416SJulian Elischer rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
78382cd038dSYoshinobu Inoue {
7840ae76120SRobert Watson 	struct inpcb *inp;
78582cd038dSYoshinobu Inoue 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
78688d166bfSBjoern A. Zeeb 	struct in6_addr in6a;
787605284b8SAlexander V. Chernikov 	struct epoch_tracker et;
788a1f7e5f8SHajimu UMEMOTO 	int error = 0, scope_ambiguous = 0;
78982cd038dSYoshinobu Inoue 
7900ae76120SRobert Watson 	inp = sotoinpcb(so);
79114ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
7920ae76120SRobert Watson 
79382cd038dSYoshinobu Inoue 	if (nam->sa_len != sizeof(*addr))
7940ae76120SRobert Watson 		return (EINVAL);
7954f6c66ccSMatt Macy 	if (CK_STAILQ_EMPTY(&V_ifnet))
7960ae76120SRobert Watson 		return (EADDRNOTAVAIL);
79782cd038dSYoshinobu Inoue 	if (addr->sin6_family != AF_INET6)
7980ae76120SRobert Watson 		return (EAFNOSUPPORT);
799a1f7e5f8SHajimu UMEMOTO 
800a1f7e5f8SHajimu UMEMOTO 	/*
8010ae76120SRobert Watson 	 * Application should provide a proper zone ID or the use of default
8020ae76120SRobert Watson 	 * zone IDs should be enabled.  Unfortunately, some applications do
8030ae76120SRobert Watson 	 * not behave as it should, so we need a workaround.  Even if an
8040ae76120SRobert Watson 	 * appropriate ID is not determined, we'll see if we can determine
8050ae76120SRobert Watson 	 * the outgoing interface.  If we can, determine the zone ID based on
8060ae76120SRobert Watson 	 * the interface below.
807a1f7e5f8SHajimu UMEMOTO 	 */
808603724d3SBjoern A. Zeeb 	if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
809a1f7e5f8SHajimu UMEMOTO 		scope_ambiguous = 1;
810603724d3SBjoern A. Zeeb 	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
811a1f7e5f8SHajimu UMEMOTO 		return (error);
812a1f7e5f8SHajimu UMEMOTO 
8138501a69cSRobert Watson 	INP_WLOCK(inp);
81482cd038dSYoshinobu Inoue 	/* Source address selection. XXX: need pcblookup? */
815605284b8SAlexander V. Chernikov 	NET_EPOCH_ENTER(et);
816601c0b8bSAlexander V. Chernikov 	error = in6_selectsrc_socket(addr, inp->in6p_outputopts,
817601c0b8bSAlexander V. Chernikov 	    inp, so->so_cred, scope_ambiguous, &in6a, NULL);
818605284b8SAlexander V. Chernikov 	NET_EPOCH_EXIT(et);
81988d166bfSBjoern A. Zeeb 	if (error) {
82071a1539eSMateusz Guzik 		INP_WUNLOCK(inp);
82188d166bfSBjoern A. Zeeb 		return (error);
82207385abdSRobert Watson 	}
823a1f7e5f8SHajimu UMEMOTO 
82482cd038dSYoshinobu Inoue 	inp->in6p_faddr = addr->sin6_addr;
82588d166bfSBjoern A. Zeeb 	inp->in6p_laddr = in6a;
82682cd038dSYoshinobu Inoue 	soisconnected(so);
82771a1539eSMateusz Guzik 	INP_WUNLOCK(inp);
8280ae76120SRobert Watson 	return (0);
82982cd038dSYoshinobu Inoue }
83082cd038dSYoshinobu Inoue 
83182cd038dSYoshinobu Inoue static int
rip6_shutdown(struct socket * so,enum shutdown_how how)8325bba2728SGleb Smirnoff rip6_shutdown(struct socket *so, enum shutdown_how how)
83382cd038dSYoshinobu Inoue {
83407385abdSRobert Watson 
8355bba2728SGleb Smirnoff 	SOCK_LOCK(so);
8365bba2728SGleb Smirnoff 	if (!(so->so_state & SS_ISCONNECTED)) {
8375bba2728SGleb Smirnoff 		SOCK_UNLOCK(so);
8385bba2728SGleb Smirnoff 		return (ENOTCONN);
8395bba2728SGleb Smirnoff 	}
8405bba2728SGleb Smirnoff 	SOCK_UNLOCK(so);
8410ae76120SRobert Watson 
8425bba2728SGleb Smirnoff 	switch (how) {
8435bba2728SGleb Smirnoff 	case SHUT_RD:
844ce69e373SGleb Smirnoff 		sorflush(so);
8455bba2728SGleb Smirnoff 		break;
8465bba2728SGleb Smirnoff 	case SHUT_RDWR:
847ce69e373SGleb Smirnoff 		sorflush(so);
8485bba2728SGleb Smirnoff 		/* FALLTHROUGH */
8495bba2728SGleb Smirnoff 	case SHUT_WR:
85082cd038dSYoshinobu Inoue 		socantsendmore(so);
8515bba2728SGleb Smirnoff 	}
8525bba2728SGleb Smirnoff 
8530ae76120SRobert Watson 	return (0);
85482cd038dSYoshinobu Inoue }
85582cd038dSYoshinobu Inoue 
856e7d02be1SGleb Smirnoff struct protosw rip6_protosw = {
85761f7427fSGleb Smirnoff 	.pr_type =		SOCK_RAW,
85861f7427fSGleb Smirnoff 	.pr_flags =		PR_ATOMIC|PR_ADDR,
85961f7427fSGleb Smirnoff 	.pr_ctloutput =		rip6_ctloutput,
86061f7427fSGleb Smirnoff 	.pr_abort =		rip6_abort,
86161f7427fSGleb Smirnoff 	.pr_attach =		rip6_attach,
86261f7427fSGleb Smirnoff 	.pr_bind =		rip6_bind,
86361f7427fSGleb Smirnoff 	.pr_connect =		rip6_connect,
86461f7427fSGleb Smirnoff 	.pr_control =		in6_control,
86561f7427fSGleb Smirnoff 	.pr_detach =		rip6_detach,
86661f7427fSGleb Smirnoff 	.pr_disconnect =	rip6_disconnect,
86761f7427fSGleb Smirnoff 	.pr_peeraddr =		in6_getpeeraddr,
86861f7427fSGleb Smirnoff 	.pr_send =		rip6_send,
86961f7427fSGleb Smirnoff 	.pr_shutdown =		rip6_shutdown,
87061f7427fSGleb Smirnoff 	.pr_sockaddr =		in6_getsockaddr,
87161f7427fSGleb Smirnoff 	.pr_close =		rip6_close
87282cd038dSYoshinobu Inoue };
873