1139826Simp/*-
253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
353541Sshin * All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer.
1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1153541Sshin *    notice, this list of conditions and the following disclaimer in the
1253541Sshin *    documentation and/or other materials provided with the distribution.
1353541Sshin * 3. Neither the name of the project nor the names of its contributors
1453541Sshin *    may be used to endorse or promote products derived from this software
1553541Sshin *    without specific prior written permission.
1653541Sshin *
1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2753541Sshin * SUCH DAMAGE.
2853541Sshin */
2953541Sshin
30139826Simp/*-
3153541Sshin * Copyright (c) 1982, 1986, 1988, 1993
32180305Srwatson *	The Regents of the University of California.
33180305Srwatson * All rights reserved.
3453541Sshin *
3553541Sshin * Redistribution and use in source and binary forms, with or without
3653541Sshin * modification, are permitted provided that the following conditions
3753541Sshin * are met:
3853541Sshin * 1. Redistributions of source code must retain the above copyright
3953541Sshin *    notice, this list of conditions and the following disclaimer.
4053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4153541Sshin *    notice, this list of conditions and the following disclaimer in the
4253541Sshin *    documentation and/or other materials provided with the distribution.
4353541Sshin * 4. Neither the name of the University nor the names of its contributors
4453541Sshin *    may be used to endorse or promote products derived from this software
4553541Sshin *    without specific prior written permission.
4653541Sshin *
4753541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5753541Sshin * SUCH DAMAGE.
5853541Sshin *
5953541Sshin *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
6053541Sshin */
6153541Sshin
62174510Sobrien#include <sys/cdefs.h>
63174510Sobrien__FBSDID("$FreeBSD: stable/11/sys/netinet6/raw_ip6.c 347688 2019-05-16 11:20:02Z tuexen $");
64174510Sobrien
6555009Sshin#include "opt_ipsec.h"
6678064Sume#include "opt_inet6.h"
6755009Sshin
6853541Sshin#include <sys/param.h>
6995759Stanimura#include <sys/errno.h>
70185435Sbz#include <sys/jail.h>
71253085Sae#include <sys/kernel.h>
7295759Stanimura#include <sys/lock.h>
7353541Sshin#include <sys/malloc.h>
7495759Stanimura#include <sys/mbuf.h>
75170689Srwatson#include <sys/priv.h>
7653541Sshin#include <sys/proc.h>
7795759Stanimura#include <sys/protosw.h>
7895759Stanimura#include <sys/signalvar.h>
7953541Sshin#include <sys/socket.h>
8053541Sshin#include <sys/socketvar.h>
8195759Stanimura#include <sys/sx.h>
82148385Sume#include <sys/syslog.h>
8353541Sshin
8453541Sshin#include <net/if.h>
85257176Sglebius#include <net/if_var.h>
8695759Stanimura#include <net/if_types.h>
8753541Sshin#include <net/route.h>
88185571Sbz#include <net/vnet.h>
8953541Sshin
9053541Sshin#include <netinet/in.h>
9153541Sshin#include <netinet/in_var.h>
9253541Sshin#include <netinet/in_systm.h>
93185571Sbz#include <netinet/in_pcb.h>
94185571Sbz
9595759Stanimura#include <netinet/icmp6.h>
9662587Sitojun#include <netinet/ip6.h>
97211501Sanchie#include <netinet/ip_var.h>
9895759Stanimura#include <netinet6/ip6protosw.h>
9956723Sshin#include <netinet6/ip6_mroute.h>
10053541Sshin#include <netinet6/in6_pcb.h>
10195759Stanimura#include <netinet6/ip6_var.h>
10253541Sshin#include <netinet6/nd6.h>
10395759Stanimura#include <netinet6/raw_ip6.h>
10462587Sitojun#include <netinet6/scope6_var.h>
105211501Sanchie#include <netinet6/send.h>
10653541Sshin
107315514Sae#include <netipsec/ipsec_support.h>
108105199Ssam
10953541Sshin#include <machine/stdarg.h>
11053541Sshin
11153541Sshin#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
11253541Sshin#define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
11353541Sshin
11453541Sshin/*
11553541Sshin * Raw interface to IP6 protocol.
11653541Sshin */
11753541Sshin
118195699SrwatsonVNET_DECLARE(struct inpcbhead, ripcb);
119195699SrwatsonVNET_DECLARE(struct inpcbinfo, ripcbinfo);
120195727Srwatson#define	V_ripcb				VNET(ripcb)
121195727Srwatson#define	V_ripcbinfo			VNET(ripcbinfo)
122185348Szec
12353541Sshinextern u_long	rip_sendspace;
12453541Sshinextern u_long	rip_recvspace;
12553541Sshin
126253085SaeVNET_PCPUSTAT_DEFINE(struct rip6stat, rip6stat);
127253085SaeVNET_PCPUSTAT_SYSINIT(rip6stat);
128207369Sbz
129253085Sae#ifdef VIMAGE
130253085SaeVNET_PCPUSTAT_SYSUNINIT(rip6stat);
131253085Sae#endif /* VIMAGE */
132253085Sae
13353541Sshin/*
134191672Sbms * Hooks for multicast routing. They all default to NULL, so leave them not
135191672Sbms * initialized and rely on BSS being set to 0.
136166938Sbms */
137191672Sbms
138191672Sbms/*
139191672Sbms * The socket used to communicate with the multicast routing daemon.
140191672Sbms */
141195699SrwatsonVNET_DEFINE(struct socket *, ip6_mrouter);
142191672Sbms
143191672Sbms/*
144191672Sbms * The various mrouter functions.
145191672Sbms */
146166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *);
147166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *);
148166938Sbmsint (*ip6_mrouter_done)(void);
149166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
150194581Srdivackyint (*mrt6_ioctl)(u_long, caddr_t);
151166938Sbms
152166938Sbms/*
153180305Srwatson * Setup generic address and protocol structures for raw_input routine, then
154180305Srwatson * pass them along with mbuf chain.
15553541Sshin */
15653541Sshinint
157171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int proto)
15853541Sshin{
159191672Sbms	struct ifnet *ifp;
16053541Sshin	struct mbuf *m = *mp;
161331643Sdim	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
162331643Sdim	struct inpcb *in6p;
163298075Spfg	struct inpcb *last = NULL;
16478064Sume	struct mbuf *opts = NULL;
165121901Sume	struct sockaddr_in6 fromsa;
16653541Sshin
167252007Sae	RIP6STAT_INC(rip6s_ipackets);
16878064Sume
169121901Sume	init_sin6(&fromsa, m); /* general init */
17053541Sshin
171191672Sbms	ifp = m->m_pkthdr.rcvif;
172191672Sbms
173181803Sbz	INP_INFO_RLOCK(&V_ripcbinfo);
174181803Sbz	LIST_FOREACH(in6p, &V_ripcb, inp_list) {
175185435Sbz		/* XXX inp locking */
176186141Sbz		if ((in6p->inp_vflag & INP_IPV6) == 0)
17753541Sshin			continue;
178186141Sbz		if (in6p->inp_ip_p &&
179186141Sbz		    in6p->inp_ip_p != proto)
180180850Smav			continue;
18153541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
18253541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
183180850Smav			continue;
18453541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
18553541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
186180850Smav			continue;
187200473Sbz		if (jailed_without_vnet(in6p->inp_cred)) {
188191672Sbms			/*
189191672Sbms			 * Allow raw socket in jail to receive multicast;
190191672Sbms			 * assume process had PRIV_NETINET_RAW at attach,
191191672Sbms			 * and fall through into normal filter path if so.
192191672Sbms			 */
193191672Sbms			if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
194191672Sbms			    prison_check_ip6(in6p->inp_cred,
195191672Sbms			    &ip6->ip6_dst) != 0)
196191672Sbms				continue;
197191672Sbms		}
198248180Sae		INP_RLOCK(in6p);
19978064Sume		if (in6p->in6p_cksum != -1) {
200252007Sae			RIP6STAT_INC(rip6s_isum);
201347688Stuexen			if (m->m_pkthdr.len - (*offp + in6p->in6p_cksum) < 2 ||
202347688Stuexen			    in6_cksum(m, proto, *offp,
20378064Sume			    m->m_pkthdr.len - *offp)) {
204180932Smav				INP_RUNLOCK(in6p);
205252007Sae				RIP6STAT_INC(rip6s_badsum);
206180850Smav				continue;
20778064Sume			}
20853541Sshin		}
209191672Sbms		/*
210191672Sbms		 * If this raw socket has multicast state, and we
211191672Sbms		 * have received a multicast, check if this socket
212191672Sbms		 * should receive it, as multicast filtering is now
213191672Sbms		 * the responsibility of the transport layer.
214191672Sbms		 */
215191672Sbms		if (in6p->in6p_moptions &&
216191672Sbms		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
217199518Sbms			/*
218199518Sbms			 * If the incoming datagram is for MLD, allow it
219199518Sbms			 * through unconditionally to the raw socket.
220199518Sbms			 *
221199518Sbms			 * Use the M_RTALERT_MLD flag to check for MLD
222199518Sbms			 * traffic without having to inspect the mbuf chain
223199518Sbms			 * more deeply, as all MLDv1/v2 host messages MUST
224199518Sbms			 * contain the Router Alert option.
225199518Sbms			 *
226199518Sbms			 * In the case of MLDv1, we may not have explicitly
227199518Sbms			 * joined the group, and may have set IFF_ALLMULTI
228199518Sbms			 * on the interface. im6o_mc_filter() may discard
229199518Sbms			 * control traffic we actually need to see.
230199518Sbms			 *
231199518Sbms			 * Userland multicast routing daemons should continue
232199518Sbms			 * filter the control traffic appropriately.
233199518Sbms			 */
234191672Sbms			int blocked;
235191672Sbms
236199518Sbms			blocked = MCAST_PASS;
237199518Sbms			if ((m->m_flags & M_RTALERT_MLD) == 0) {
238199518Sbms				struct sockaddr_in6 mcaddr;
239191672Sbms
240199518Sbms				bzero(&mcaddr, sizeof(struct sockaddr_in6));
241199518Sbms				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
242199518Sbms				mcaddr.sin6_family = AF_INET6;
243199518Sbms				mcaddr.sin6_addr = ip6->ip6_dst;
244199518Sbms
245199518Sbms				blocked = im6o_mc_filter(in6p->in6p_moptions,
246199518Sbms				    ifp,
247199518Sbms				    (struct sockaddr *)&mcaddr,
248199518Sbms				    (struct sockaddr *)&fromsa);
249199518Sbms			}
250191672Sbms			if (blocked != MCAST_PASS) {
251191672Sbms				IP6STAT_INC(ip6s_notmember);
252211301Sbz				INP_RUNLOCK(in6p);
253191672Sbms				continue;
254191672Sbms			}
255191672Sbms		}
256186163Skmacy		if (last != NULL) {
25753541Sshin			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
25878064Sume
259315514Sae#if defined(IPSEC) || defined(IPSEC_SUPPORT)
26078064Sume			/*
26178064Sume			 * Check AH/ESP integrity.
26278064Sume			 */
263315514Sae			if (IPSEC_ENABLED(ipv6)) {
264315514Sae				if (n != NULL &&
265315514Sae				    IPSEC_CHECK_POLICY(ipv6, n, last) != 0) {
266315514Sae					m_freem(n);
267315514Sae					/* Do not inject data into pcb. */
268315514Sae					n = NULL;
269315514Sae				}
270315514Sae			}
271171167Sgnn#endif /* IPSEC */
27253541Sshin			if (n) {
273186223Sbz				if (last->inp_flags & INP_CONTROLOPTS ||
274186141Sbz				    last->inp_socket->so_options & SO_TIMESTAMP)
275121674Sume					ip6_savecontrol(last, n, &opts);
27653541Sshin				/* strip intermediate headers */
27753541Sshin				m_adj(n, *offp);
278186141Sbz				if (sbappendaddr(&last->inp_socket->so_rcv,
279121901Sume						(struct sockaddr *)&fromsa,
28053541Sshin						 n, opts) == 0) {
28153541Sshin					m_freem(n);
28253541Sshin					if (opts)
28353541Sshin						m_freem(opts);
284252007Sae					RIP6STAT_INC(rip6s_fullsock);
28597658Stanimura				} else
286186141Sbz					sorwakeup(last->inp_socket);
28753541Sshin				opts = NULL;
28853541Sshin			}
289178377Srwatson			INP_RUNLOCK(last);
29053541Sshin		}
29153541Sshin		last = in6p;
29253541Sshin	}
293181803Sbz	INP_INFO_RUNLOCK(&V_ripcbinfo);
294315514Sae#if defined(IPSEC) || defined(IPSEC_SUPPORT)
29578064Sume	/*
29678064Sume	 * Check AH/ESP integrity.
29778064Sume	 */
298315514Sae	if (IPSEC_ENABLED(ipv6) && last != NULL &&
299315514Sae	    IPSEC_CHECK_POLICY(ipv6, m, last) != 0) {
30078064Sume		m_freem(m);
301249294Sae		IP6STAT_DEC(ip6s_delivered);
302180305Srwatson		/* Do not inject data into pcb. */
303178377Srwatson		INP_RUNLOCK(last);
304105199Ssam	} else
305171167Sgnn#endif /* IPSEC */
306186163Skmacy	if (last != NULL) {
307186223Sbz		if (last->inp_flags & INP_CONTROLOPTS ||
308186141Sbz		    last->inp_socket->so_options & SO_TIMESTAMP)
309121674Sume			ip6_savecontrol(last, m, &opts);
310180305Srwatson		/* Strip intermediate headers. */
31153541Sshin		m_adj(m, *offp);
312186141Sbz		if (sbappendaddr(&last->inp_socket->so_rcv,
313180305Srwatson		    (struct sockaddr *)&fromsa, m, opts) == 0) {
31453541Sshin			m_freem(m);
31553541Sshin			if (opts)
31653541Sshin				m_freem(opts);
317252007Sae			RIP6STAT_INC(rip6s_fullsock);
31897658Stanimura		} else
319186141Sbz			sorwakeup(last->inp_socket);
320178377Srwatson		INP_RUNLOCK(last);
32153541Sshin	} else {
322252007Sae		RIP6STAT_INC(rip6s_nosock);
32378064Sume		if (m->m_flags & M_MCAST)
324252007Sae			RIP6STAT_INC(rip6s_nosockmcast);
32553541Sshin		if (proto == IPPROTO_NONE)
32653541Sshin			m_freem(m);
327329157Sae		else
32853541Sshin			icmp6_error(m, ICMP6_PARAM_PROB,
329180305Srwatson			    ICMP6_PARAMPROB_NEXTHEADER,
330329157Sae			    ip6_get_prevhdr(m, *offp));
331249294Sae		IP6STAT_DEC(ip6s_delivered);
33253541Sshin	}
333180305Srwatson	return (IPPROTO_DONE);
33453541Sshin}
33553541Sshin
33662587Sitojunvoid
337171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
33862587Sitojun{
33962587Sitojun	struct ip6_hdr *ip6;
34062587Sitojun	struct mbuf *m;
34162587Sitojun	int off = 0;
34278064Sume	struct ip6ctlparam *ip6cp = NULL;
34378064Sume	const struct sockaddr_in6 *sa6_src = NULL;
344125776Sume	void *cmdarg;
345175162Sobrien	struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
34662587Sitojun
34762587Sitojun	if (sa->sa_family != AF_INET6 ||
34862587Sitojun	    sa->sa_len != sizeof(struct sockaddr_in6))
34962587Sitojun		return;
35062587Sitojun
35162587Sitojun	if ((unsigned)cmd >= PRC_NCMDS)
35262587Sitojun		return;
35362587Sitojun	if (PRC_IS_REDIRECT(cmd))
35462587Sitojun		notify = in6_rtchange, d = NULL;
35562587Sitojun	else if (cmd == PRC_HOSTDEAD)
35662587Sitojun		d = NULL;
35762587Sitojun	else if (inet6ctlerrmap[cmd] == 0)
35862587Sitojun		return;
35962587Sitojun
360180305Srwatson	/*
361180305Srwatson	 * If the parameter is from icmp6, decode it.
362180305Srwatson	 */
36362587Sitojun	if (d != NULL) {
36478064Sume		ip6cp = (struct ip6ctlparam *)d;
36562587Sitojun		m = ip6cp->ip6c_m;
36662587Sitojun		ip6 = ip6cp->ip6c_ip6;
36762587Sitojun		off = ip6cp->ip6c_off;
368125776Sume		cmdarg = ip6cp->ip6c_cmdarg;
36978064Sume		sa6_src = ip6cp->ip6c_src;
37062587Sitojun	} else {
37162587Sitojun		m = NULL;
37262587Sitojun		ip6 = NULL;
373125776Sume		cmdarg = NULL;
37478064Sume		sa6_src = &sa6_any;
37562587Sitojun	}
37662587Sitojun
377181803Sbz	(void) in6_pcbnotify(&V_ripcbinfo, sa, 0,
378180305Srwatson	    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
37962587Sitojun}
38062587Sitojun
38153541Sshin/*
382180305Srwatson * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
383180305Srwatson * may have setup with control call.
38453541Sshin */
38553541Sshinint
386269699Skevlorip6_output(struct mbuf *m, struct socket *so, ...)
38753541Sshin{
388120941Sume	struct mbuf *control;
389211501Sanchie	struct m_tag *mtag;
39053541Sshin	struct sockaddr_in6 *dstsock;
39153541Sshin	struct in6_addr *dst;
39253541Sshin	struct ip6_hdr *ip6;
39353541Sshin	struct inpcb *in6p;
39453541Sshin	u_int	plen = m->m_pkthdr.len;
39553541Sshin	int error = 0;
396148247Sume	struct ip6_pktopts opt, *optp;
39753541Sshin	struct ifnet *oifp = NULL;
39853541Sshin	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
399148385Sume	int scope_ambiguous = 0;
400211435Sume	int use_defzone = 0;
401293633Smelifaro	int hlim = 0;
402194777Sbz	struct in6_addr in6a;
40353541Sshin	va_list ap;
40453541Sshin
405269699Skevlo	va_start(ap, so);
40653541Sshin	dstsock = va_arg(ap, struct sockaddr_in6 *);
40753541Sshin	control = va_arg(ap, struct mbuf *);
40853541Sshin	va_end(ap);
40953541Sshin
410186141Sbz	in6p = sotoinpcb(so);
411178285Srwatson	INP_WLOCK(in6p);
41253541Sshin
41353541Sshin	dst = &dstsock->sin6_addr;
414186170Skmacy	if (control != NULL) {
415148242Sume		if ((error = ip6_setpktopts(control, &opt,
416175630Sbz		    in6p->in6p_outputopts, so->so_cred,
417175630Sbz		    so->so_proto->pr_protocol)) != 0) {
41853541Sshin			goto bad;
419121472Sume		}
420148247Sume		optp = &opt;
421148247Sume	} else
422148247Sume		optp = in6p->in6p_outputopts;
42353541Sshin
42453541Sshin	/*
425148385Sume	 * Check and convert scope zone ID into internal form.
426180305Srwatson	 *
427148385Sume	 * XXX: we may still need to determine the zone later.
428148385Sume	 */
429148385Sume	if (!(so->so_state & SS_ISCONNECTED)) {
430211530Sume		if (!optp || !optp->ip6po_pktinfo ||
431211530Sume		    !optp->ip6po_pktinfo->ipi6_ifindex)
432211435Sume			use_defzone = V_ip6_use_defzone;
433211435Sume		if (dstsock->sin6_scope_id == 0 && !use_defzone)
434148385Sume			scope_ambiguous = 1;
435211435Sume		if ((error = sa6_embedscope(dstsock, use_defzone)) != 0)
436148385Sume			goto bad;
437148385Sume	}
438148385Sume
439148385Sume	/*
440180305Srwatson	 * For an ICMPv6 packet, we should know its type and code to update
441180305Srwatson	 * statistics.
44253541Sshin	 */
44353541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
44453541Sshin		struct icmp6_hdr *icmp6;
44553541Sshin		if (m->m_len < sizeof(struct icmp6_hdr) &&
44653541Sshin		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
44753541Sshin			error = ENOBUFS;
44853541Sshin			goto bad;
44953541Sshin		}
45053541Sshin		icmp6 = mtod(m, struct icmp6_hdr *);
45153541Sshin		type = icmp6->icmp6_type;
45253541Sshin		code = icmp6->icmp6_code;
45353541Sshin	}
45453541Sshin
455243882Sglebius	M_PREPEND(m, sizeof(*ip6), M_NOWAIT);
456133592Srwatson	if (m == NULL) {
457133592Srwatson		error = ENOBUFS;
458133592Srwatson		goto bad;
459133592Srwatson	}
46053541Sshin	ip6 = mtod(m, struct ip6_hdr *);
46153541Sshin
46253541Sshin	/*
46353541Sshin	 * Source address selection.
46453541Sshin	 */
465293633Smelifaro	error = in6_selectsrc_socket(dstsock, optp, in6p, so->so_cred,
466293633Smelifaro	    scope_ambiguous, &in6a, &hlim);
467293633Smelifaro
468194777Sbz	if (error)
469121472Sume		goto bad;
470207277Sbz	error = prison_check_ip6(in6p->inp_cred, &in6a);
471188144Sjamie	if (error != 0)
472188144Sjamie		goto bad;
473194777Sbz	ip6->ip6_src = in6a;
474148385Sume
475148385Sume	ip6->ip6_dst = dstsock->sin6_addr;
476148385Sume
477180305Srwatson	/*
478180305Srwatson	 * Fill in the rest of the IPv6 header fields.
479180305Srwatson	 */
48055009Sshin	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
481186141Sbz	    (in6p->inp_flow & IPV6_FLOWINFO_MASK);
48255009Sshin	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
483180305Srwatson	    (IPV6_VERSION & IPV6_VERSION_MASK);
484180305Srwatson
485180305Srwatson	/*
486180305Srwatson	 * ip6_plen will be filled in ip6_output, so not fill it here.
487180305Srwatson	 */
488186141Sbz	ip6->ip6_nxt = in6p->inp_ip_p;
489293633Smelifaro	ip6->ip6_hlim = hlim;
49053541Sshin
49153541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
49253541Sshin	    in6p->in6p_cksum != -1) {
49353541Sshin		struct mbuf *n;
49453541Sshin		int off;
49553541Sshin		u_int16_t *p;
49653541Sshin
497180305Srwatson		/* Compute checksum. */
49853541Sshin		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
49953541Sshin			off = offsetof(struct icmp6_hdr, icmp6_cksum);
50053541Sshin		else
50153541Sshin			off = in6p->in6p_cksum;
502347687Stuexen		if (plen < off + 2) {
50353541Sshin			error = EINVAL;
50453541Sshin			goto bad;
50553541Sshin		}
50653541Sshin		off += sizeof(struct ip6_hdr);
50753541Sshin
50853541Sshin		n = m;
50953541Sshin		while (n && n->m_len <= off) {
51053541Sshin			off -= n->m_len;
51153541Sshin			n = n->m_next;
51253541Sshin		}
51353541Sshin		if (!n)
51453541Sshin			goto bad;
51553541Sshin		p = (u_int16_t *)(mtod(n, caddr_t) + off);
51653541Sshin		*p = 0;
51753541Sshin		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
51853541Sshin	}
51953541Sshin
520211501Sanchie	/*
521211501Sanchie	 * Send RA/RS messages to user land for protection, before sending
522211501Sanchie	 * them to rtadvd/rtsol.
523211501Sanchie	 */
524211501Sanchie	if ((send_sendso_input_hook != NULL) &&
525211501Sanchie	    so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
526211501Sanchie		switch (type) {
527211501Sanchie		case ND_ROUTER_ADVERT:
528211501Sanchie		case ND_ROUTER_SOLICIT:
529211501Sanchie			mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
530211501Sanchie				sizeof(unsigned short), M_NOWAIT);
531211501Sanchie			if (mtag == NULL)
532211501Sanchie				goto bad;
533211501Sanchie			m_tag_prepend(m, mtag);
534211501Sanchie		}
535211501Sanchie	}
536211501Sanchie
537148247Sume	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
53853541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
53953541Sshin		if (oifp)
54053541Sshin			icmp6_ifoutstat_inc(oifp, type, code);
541190964Srwatson		ICMP6STAT_INC(icp6s_outhist[type]);
54278064Sume	} else
543252007Sae		RIP6STAT_INC(rip6s_opackets);
54453541Sshin
54553541Sshin	goto freectl;
54653541Sshin
54753541Sshin bad:
54853541Sshin	if (m)
54953541Sshin		m_freem(m);
55053541Sshin
55153541Sshin freectl:
552186170Skmacy	if (control != NULL) {
553148247Sume		ip6_clearpktopts(&opt, -1);
55453541Sshin		m_freem(control);
55578064Sume	}
556178285Srwatson	INP_WUNLOCK(in6p);
557120856Sume	return (error);
55853541Sshin}
55953541Sshin
56053541Sshin/*
56153541Sshin * Raw IPv6 socket option processing.
56253541Sshin */
56353541Sshinint
564171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt)
56553541Sshin{
566231852Sbz	struct inpcb *inp;
56753541Sshin	int error;
56853541Sshin
56953541Sshin	if (sopt->sopt_level == IPPROTO_ICMPV6)
57053541Sshin		/*
57153541Sshin		 * XXX: is it better to call icmp6_ctloutput() directly
57253541Sshin		 * from protosw?
57353541Sshin		 */
574120856Sume		return (icmp6_ctloutput(so, sopt));
575231852Sbz	else if (sopt->sopt_level != IPPROTO_IPV6) {
576231852Sbz		if (sopt->sopt_level == SOL_SOCKET &&
577231852Sbz		    sopt->sopt_name == SO_SETFIB) {
578231852Sbz			inp = sotoinpcb(so);
579231852Sbz			INP_WLOCK(inp);
580231852Sbz			inp->inp_inc.inc_fibnum = so->so_fibnum;
581231852Sbz			INP_WUNLOCK(inp);
582231852Sbz			return (0);
583231852Sbz		}
58453541Sshin		return (EINVAL);
585231852Sbz	}
58653541Sshin
58753541Sshin	error = 0;
58853541Sshin
58953541Sshin	switch (sopt->sopt_dir) {
59053541Sshin	case SOPT_GET:
59153541Sshin		switch (sopt->sopt_name) {
59256723Sshin		case MRT6_INIT:
59356723Sshin		case MRT6_DONE:
59456723Sshin		case MRT6_ADD_MIF:
59556723Sshin		case MRT6_DEL_MIF:
59656723Sshin		case MRT6_ADD_MFC:
59756723Sshin		case MRT6_DEL_MFC:
59856723Sshin		case MRT6_PIM:
599166938Sbms			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
600166938Sbms			    EOPNOTSUPP;
60156723Sshin			break;
602121578Sume		case IPV6_CHECKSUM:
603121578Sume			error = ip6_raw_ctloutput(so, sopt);
604121578Sume			break;
60553541Sshin		default:
60653541Sshin			error = ip6_ctloutput(so, sopt);
60753541Sshin			break;
60853541Sshin		}
60953541Sshin		break;
61053541Sshin
61153541Sshin	case SOPT_SET:
61253541Sshin		switch (sopt->sopt_name) {
61356723Sshin		case MRT6_INIT:
61456723Sshin		case MRT6_DONE:
61556723Sshin		case MRT6_ADD_MIF:
61656723Sshin		case MRT6_DEL_MIF:
61756723Sshin		case MRT6_ADD_MFC:
61856723Sshin		case MRT6_DEL_MFC:
61956723Sshin		case MRT6_PIM:
620166938Sbms			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
621166938Sbms			    EOPNOTSUPP;
62256723Sshin			break;
623121578Sume		case IPV6_CHECKSUM:
624121578Sume			error = ip6_raw_ctloutput(so, sopt);
625121578Sume			break;
62653541Sshin		default:
62753541Sshin			error = ip6_ctloutput(so, sopt);
62853541Sshin			break;
62953541Sshin		}
63053541Sshin		break;
63153541Sshin	}
63253541Sshin
63353541Sshin	return (error);
63453541Sshin}
63553541Sshin
63653541Sshinstatic int
63783366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td)
63853541Sshin{
63953541Sshin	struct inpcb *inp;
640144261Ssam	struct icmp6_filter *filter;
641157676Srwatson	int error;
64253541Sshin
64353541Sshin	inp = sotoinpcb(so);
644157374Srwatson	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
645180305Srwatson
646175630Sbz	error = priv_check(td, PRIV_NETINET_RAW);
647175630Sbz	if (error)
648180305Srwatson		return (error);
64955009Sshin	error = soreserve(so, rip_sendspace, rip_recvspace);
650157374Srwatson	if (error)
651180305Srwatson		return (error);
652184214Sdes	filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
653157374Srwatson	if (filter == NULL)
654180305Srwatson		return (ENOMEM);
655181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
656181803Sbz	error = in_pcballoc(so, &V_ripcbinfo);
657132714Srwatson	if (error) {
658181803Sbz		INP_INFO_WUNLOCK(&V_ripcbinfo);
659184205Sdes		free(filter, M_PCB);
660180305Srwatson		return (error);
661132714Srwatson	}
66253541Sshin	inp = (struct inpcb *)so->so_pcb;
663181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
66453541Sshin	inp->inp_vflag |= INP_IPV6;
665186141Sbz	inp->inp_ip_p = (long)proto;
66653541Sshin	inp->in6p_hops = -1;	/* use kernel default */
66753541Sshin	inp->in6p_cksum = -1;
668144261Ssam	inp->in6p_icmp6filt = filter;
66953541Sshin	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
670178285Srwatson	INP_WUNLOCK(inp);
671180305Srwatson	return (0);
67253541Sshin}
67353541Sshin
674157370Srwatsonstatic void
67553541Sshinrip6_detach(struct socket *so)
67653541Sshin{
67753541Sshin	struct inpcb *inp;
67853541Sshin
67953541Sshin	inp = sotoinpcb(so);
680157374Srwatson	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
681160549Srwatson
682191672Sbms	if (so == V_ip6_mrouter && ip6_mrouter_done)
683166938Sbms		ip6_mrouter_done();
68453541Sshin	/* xxx: RSVP */
685181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
686178285Srwatson	INP_WLOCK(inp);
687184205Sdes	free(inp->in6p_icmp6filt, M_PCB);
688185344Sbz	in_pcbdetach(inp);
689185370Sbz	in_pcbfree(inp);
690181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
69153541Sshin}
69253541Sshin
693160549Srwatson/* XXXRW: This can't ever be called. */
694157366Srwatsonstatic void
69553541Sshinrip6_abort(struct socket *so)
69653541Sshin{
697160549Srwatson	struct inpcb *inp;
698160549Srwatson
699160549Srwatson	inp = sotoinpcb(so);
700160549Srwatson	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
701160549Srwatson
70253541Sshin	soisdisconnected(so);
70353541Sshin}
70453541Sshin
705160549Srwatsonstatic void
706160549Srwatsonrip6_close(struct socket *so)
707160549Srwatson{
708160549Srwatson	struct inpcb *inp;
709160549Srwatson
710160549Srwatson	inp = sotoinpcb(so);
711160549Srwatson	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
712160549Srwatson
713160549Srwatson	soisdisconnected(so);
714160549Srwatson}
715160549Srwatson
71653541Sshinstatic int
71753541Sshinrip6_disconnect(struct socket *so)
71853541Sshin{
719180305Srwatson	struct inpcb *inp;
72053541Sshin
721180305Srwatson	inp = sotoinpcb(so);
722180305Srwatson	KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
723180305Srwatson
72497658Stanimura	if ((so->so_state & SS_ISCONNECTED) == 0)
725180305Srwatson		return (ENOTCONN);
72653541Sshin	inp->in6p_faddr = in6addr_any;
727157366Srwatson	rip6_abort(so);
728157374Srwatson	return (0);
72953541Sshin}
73053541Sshin
73153541Sshinstatic int
73283366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
73353541Sshin{
734180305Srwatson	struct inpcb *inp;
73553541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
736194760Srwatson	struct ifaddr *ifa = NULL;
737148385Sume	int error = 0;
73853541Sshin
739180305Srwatson	inp = sotoinpcb(so);
740157374Srwatson	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
741180305Srwatson
74253541Sshin	if (nam->sa_len != sizeof(*addr))
743180305Srwatson		return (EINVAL);
744188144Sjamie	if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
745188144Sjamie		return (error);
746181803Sbz	if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
747180305Srwatson		return (EADDRNOTAVAIL);
748181803Sbz	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
749180305Srwatson		return (error);
750148385Sume
75153541Sshin	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
752194760Srwatson	    (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL)
753180305Srwatson		return (EADDRNOTAVAIL);
754194760Srwatson	if (ifa != NULL &&
755194760Srwatson	    ((struct in6_ifaddr *)ifa)->ia6_flags &
75653541Sshin	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
75753541Sshin	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
758194760Srwatson		ifa_free(ifa);
759120856Sume		return (EADDRNOTAVAIL);
76053541Sshin	}
761194760Srwatson	if (ifa != NULL)
762194760Srwatson		ifa_free(ifa);
763181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
764178285Srwatson	INP_WLOCK(inp);
76553541Sshin	inp->in6p_laddr = addr->sin6_addr;
766178285Srwatson	INP_WUNLOCK(inp);
767181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
768180305Srwatson	return (0);
76953541Sshin}
77053541Sshin
77153541Sshinstatic int
77283366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
77353541Sshin{
774180305Srwatson	struct inpcb *inp;
77553541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
776194777Sbz	struct in6_addr in6a;
777148385Sume	int error = 0, scope_ambiguous = 0;
77853541Sshin
779180305Srwatson	inp = sotoinpcb(so);
780157374Srwatson	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
781180305Srwatson
78253541Sshin	if (nam->sa_len != sizeof(*addr))
783180305Srwatson		return (EINVAL);
784181803Sbz	if (TAILQ_EMPTY(&V_ifnet))
785180305Srwatson		return (EADDRNOTAVAIL);
78653541Sshin	if (addr->sin6_family != AF_INET6)
787180305Srwatson		return (EAFNOSUPPORT);
788148385Sume
789148385Sume	/*
790180305Srwatson	 * Application should provide a proper zone ID or the use of default
791180305Srwatson	 * zone IDs should be enabled.  Unfortunately, some applications do
792180305Srwatson	 * not behave as it should, so we need a workaround.  Even if an
793180305Srwatson	 * appropriate ID is not determined, we'll see if we can determine
794180305Srwatson	 * the outgoing interface.  If we can, determine the zone ID based on
795180305Srwatson	 * the interface below.
796148385Sume	 */
797181803Sbz	if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
798148385Sume		scope_ambiguous = 1;
799181803Sbz	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
800180305Srwatson		return (error);
801148385Sume
802181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
803178285Srwatson	INP_WLOCK(inp);
80453541Sshin	/* Source address selection. XXX: need pcblookup? */
805293633Smelifaro	error = in6_selectsrc_socket(addr, inp->in6p_outputopts,
806293633Smelifaro	    inp, so->so_cred, scope_ambiguous, &in6a, NULL);
807194777Sbz	if (error) {
808178285Srwatson		INP_WUNLOCK(inp);
809181803Sbz		INP_INFO_WUNLOCK(&V_ripcbinfo);
810194777Sbz		return (error);
811132714Srwatson	}
812148385Sume
813148385Sume	inp->in6p_faddr = addr->sin6_addr;
814194777Sbz	inp->in6p_laddr = in6a;
81553541Sshin	soisconnected(so);
816178285Srwatson	INP_WUNLOCK(inp);
817181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
818180305Srwatson	return (0);
81953541Sshin}
82053541Sshin
82153541Sshinstatic int
82253541Sshinrip6_shutdown(struct socket *so)
82353541Sshin{
824132714Srwatson	struct inpcb *inp;
825132714Srwatson
826132714Srwatson	inp = sotoinpcb(so);
827157374Srwatson	KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
828180305Srwatson
829178285Srwatson	INP_WLOCK(inp);
83053541Sshin	socantsendmore(so);
831178285Srwatson	INP_WUNLOCK(inp);
832180305Srwatson	return (0);
83353541Sshin}
83453541Sshin
83553541Sshinstatic int
83653541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
837171260Sdelphij    struct mbuf *control, struct thread *td)
83853541Sshin{
839180305Srwatson	struct inpcb *inp;
84053541Sshin	struct sockaddr_in6 tmp;
84153541Sshin	struct sockaddr_in6 *dst;
842132714Srwatson	int ret;
84353541Sshin
844180305Srwatson	inp = sotoinpcb(so);
845157374Srwatson	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
846180305Srwatson
847180305Srwatson	/* Always copy sockaddr to avoid overwrites. */
848132714Srwatson	/* Unlocked read. */
84953541Sshin	if (so->so_state & SS_ISCONNECTED) {
85053541Sshin		if (nam) {
85153541Sshin			m_freem(m);
852180305Srwatson			return (EISCONN);
85353541Sshin		}
85453541Sshin		/* XXX */
85553541Sshin		bzero(&tmp, sizeof(tmp));
85653541Sshin		tmp.sin6_family = AF_INET6;
85753541Sshin		tmp.sin6_len = sizeof(struct sockaddr_in6);
858180990Srwatson		INP_RLOCK(inp);
85953541Sshin		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
860180990Srwatson		    sizeof(struct in6_addr));
861180990Srwatson		INP_RUNLOCK(inp);
86253541Sshin		dst = &tmp;
86353541Sshin	} else {
86453541Sshin		if (nam == NULL) {
86553541Sshin			m_freem(m);
866180305Srwatson			return (ENOTCONN);
86753541Sshin		}
868148385Sume		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
869148385Sume			m_freem(m);
870180305Srwatson			return (EINVAL);
871148385Sume		}
87262587Sitojun		tmp = *(struct sockaddr_in6 *)nam;
87362587Sitojun		dst = &tmp;
874148385Sume
875148385Sume		if (dst->sin6_family == AF_UNSPEC) {
876148385Sume			/*
877148385Sume			 * XXX: we allow this case for backward
878148385Sume			 * compatibility to buggy applications that
879148385Sume			 * rely on old (and wrong) kernel behavior.
880148385Sume			 */
881148385Sume			log(LOG_INFO, "rip6 SEND: address family is "
882148385Sume			    "unspec. Assume AF_INET6\n");
883148385Sume			dst->sin6_family = AF_INET6;
884148385Sume		} else if (dst->sin6_family != AF_INET6) {
885148385Sume			m_freem(m);
886148385Sume			return(EAFNOSUPPORT);
887148385Sume		}
88853541Sshin	}
889132714Srwatson	ret = rip6_output(m, so, dst, control);
890132714Srwatson	return (ret);
89153541Sshin}
89253541Sshin
89353541Sshinstruct pr_usrreqs rip6_usrreqs = {
894137386Sphk	.pru_abort =		rip6_abort,
895137386Sphk	.pru_attach =		rip6_attach,
896137386Sphk	.pru_bind =		rip6_bind,
897137386Sphk	.pru_connect =		rip6_connect,
898137386Sphk	.pru_control =		in6_control,
899137386Sphk	.pru_detach =		rip6_detach,
900137386Sphk	.pru_disconnect =	rip6_disconnect,
901169462Srwatson	.pru_peeraddr =		in6_getpeeraddr,
902137386Sphk	.pru_send =		rip6_send,
903137386Sphk	.pru_shutdown =		rip6_shutdown,
904169462Srwatson	.pru_sockaddr =		in6_getsockaddr,
905160549Srwatson	.pru_close =		rip6_close,
90653541Sshin};
907