raw_ip6.c revision 211301
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: head/sys/netinet6/raw_ip6.c 211301 2010-08-14 14:13:44Z bz $");
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>
7195759Stanimura#include <sys/lock.h>
7253541Sshin#include <sys/malloc.h>
7395759Stanimura#include <sys/mbuf.h>
74170689Srwatson#include <sys/priv.h>
7553541Sshin#include <sys/proc.h>
7695759Stanimura#include <sys/protosw.h>
7795759Stanimura#include <sys/signalvar.h>
7853541Sshin#include <sys/socket.h>
7953541Sshin#include <sys/socketvar.h>
8095759Stanimura#include <sys/sx.h>
81148385Sume#include <sys/syslog.h>
8253541Sshin
8353541Sshin#include <net/if.h>
8495759Stanimura#include <net/if_types.h>
8553541Sshin#include <net/route.h>
86185571Sbz#include <net/vnet.h>
8753541Sshin
8853541Sshin#include <netinet/in.h>
8953541Sshin#include <netinet/in_var.h>
9053541Sshin#include <netinet/in_systm.h>
91185571Sbz#include <netinet/in_pcb.h>
92185571Sbz
9395759Stanimura#include <netinet/icmp6.h>
9462587Sitojun#include <netinet/ip6.h>
9595759Stanimura#include <netinet6/ip6protosw.h>
9656723Sshin#include <netinet6/ip6_mroute.h>
9753541Sshin#include <netinet6/in6_pcb.h>
9895759Stanimura#include <netinet6/ip6_var.h>
9953541Sshin#include <netinet6/nd6.h>
10095759Stanimura#include <netinet6/raw_ip6.h>
10162587Sitojun#include <netinet6/scope6_var.h>
10253541Sshin
103171167Sgnn#ifdef IPSEC
104105199Ssam#include <netipsec/ipsec.h>
105105199Ssam#include <netipsec/ipsec6.h>
106171167Sgnn#endif /* IPSEC */
107105199Ssam
10853541Sshin#include <machine/stdarg.h>
10953541Sshin
11053541Sshin#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
11153541Sshin#define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
11253541Sshin
11353541Sshin/*
11453541Sshin * Raw interface to IP6 protocol.
11553541Sshin */
11653541Sshin
117195699SrwatsonVNET_DECLARE(struct inpcbhead, ripcb);
118195699SrwatsonVNET_DECLARE(struct inpcbinfo, ripcbinfo);
119195727Srwatson#define	V_ripcb				VNET(ripcb)
120195727Srwatson#define	V_ripcbinfo			VNET(ripcbinfo)
121185348Szec
12253541Sshinextern u_long	rip_sendspace;
12353541Sshinextern u_long	rip_recvspace;
12453541Sshin
125207369SbzVNET_DEFINE(struct rip6stat, rip6stat);
126207369Sbz
12753541Sshin/*
128191672Sbms * Hooks for multicast routing. They all default to NULL, so leave them not
129191672Sbms * initialized and rely on BSS being set to 0.
130166938Sbms */
131191672Sbms
132191672Sbms/*
133191672Sbms * The socket used to communicate with the multicast routing daemon.
134191672Sbms */
135195699SrwatsonVNET_DEFINE(struct socket *, ip6_mrouter);
136191672Sbms
137191672Sbms/*
138191672Sbms * The various mrouter functions.
139191672Sbms */
140166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *);
141166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *);
142166938Sbmsint (*ip6_mrouter_done)(void);
143166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
144194581Srdivackyint (*mrt6_ioctl)(u_long, caddr_t);
145166938Sbms
146166938Sbms/*
147180305Srwatson * Setup generic address and protocol structures for raw_input routine, then
148180305Srwatson * pass them along with mbuf chain.
14953541Sshin */
15053541Sshinint
151171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int proto)
15253541Sshin{
153191672Sbms	struct ifnet *ifp;
15453541Sshin	struct mbuf *m = *mp;
15553541Sshin	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
15653541Sshin	register struct inpcb *in6p;
15753541Sshin	struct inpcb *last = 0;
15878064Sume	struct mbuf *opts = NULL;
159121901Sume	struct sockaddr_in6 fromsa;
16053541Sshin
161181803Sbz	V_rip6stat.rip6s_ipackets++;
16278064Sume
16383934Sbrooks	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
164180305Srwatson		/* XXX Send icmp6 host/port unreach? */
16578064Sume		m_freem(m);
166180305Srwatson		return (IPPROTO_DONE);
16753541Sshin	}
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		}
19878064Sume		if (in6p->in6p_cksum != -1) {
199181803Sbz			V_rip6stat.rip6s_isum++;
200151459Ssuz			if (in6_cksum(m, proto, *offp,
20178064Sume			    m->m_pkthdr.len - *offp)) {
202180932Smav				INP_RUNLOCK(in6p);
203181803Sbz				V_rip6stat.rip6s_badsum++;
204180850Smav				continue;
20578064Sume			}
20653541Sshin		}
207191672Sbms		INP_RLOCK(in6p);
208191672Sbms		/*
209191672Sbms		 * If this raw socket has multicast state, and we
210191672Sbms		 * have received a multicast, check if this socket
211191672Sbms		 * should receive it, as multicast filtering is now
212191672Sbms		 * the responsibility of the transport layer.
213191672Sbms		 */
214191672Sbms		if (in6p->in6p_moptions &&
215191672Sbms		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
216199518Sbms			/*
217199518Sbms			 * If the incoming datagram is for MLD, allow it
218199518Sbms			 * through unconditionally to the raw socket.
219199518Sbms			 *
220199518Sbms			 * Use the M_RTALERT_MLD flag to check for MLD
221199518Sbms			 * traffic without having to inspect the mbuf chain
222199518Sbms			 * more deeply, as all MLDv1/v2 host messages MUST
223199518Sbms			 * contain the Router Alert option.
224199518Sbms			 *
225199518Sbms			 * In the case of MLDv1, we may not have explicitly
226199518Sbms			 * joined the group, and may have set IFF_ALLMULTI
227199518Sbms			 * on the interface. im6o_mc_filter() may discard
228199518Sbms			 * control traffic we actually need to see.
229199518Sbms			 *
230199518Sbms			 * Userland multicast routing daemons should continue
231199518Sbms			 * filter the control traffic appropriately.
232199518Sbms			 */
233191672Sbms			int blocked;
234191672Sbms
235199518Sbms			blocked = MCAST_PASS;
236199518Sbms			if ((m->m_flags & M_RTALERT_MLD) == 0) {
237199518Sbms				struct sockaddr_in6 mcaddr;
238191672Sbms
239199518Sbms				bzero(&mcaddr, sizeof(struct sockaddr_in6));
240199518Sbms				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
241199518Sbms				mcaddr.sin6_family = AF_INET6;
242199518Sbms				mcaddr.sin6_addr = ip6->ip6_dst;
243199518Sbms
244199518Sbms				blocked = im6o_mc_filter(in6p->in6p_moptions,
245199518Sbms				    ifp,
246199518Sbms				    (struct sockaddr *)&mcaddr,
247199518Sbms				    (struct sockaddr *)&fromsa);
248199518Sbms			}
249191672Sbms			if (blocked != MCAST_PASS) {
250191672Sbms				IP6STAT_INC(ip6s_notmember);
251211301Sbz				INP_RUNLOCK(in6p);
252191672Sbms				continue;
253191672Sbms			}
254191672Sbms		}
255186163Skmacy		if (last != NULL) {
25653541Sshin			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
25778064Sume
258171167Sgnn#ifdef IPSEC
25978064Sume			/*
26078064Sume			 * Check AH/ESP integrity.
26178064Sume			 */
262125396Sume			if (n && ipsec6_in_reject(n, last)) {
26378064Sume				m_freem(n);
264181803Sbz				V_ipsec6stat.in_polvio++;
265180305Srwatson				/* Do not inject data into pcb. */
266105199Ssam			} else
267171167Sgnn#endif /* IPSEC */
26853541Sshin			if (n) {
269186223Sbz				if (last->inp_flags & INP_CONTROLOPTS ||
270186141Sbz				    last->inp_socket->so_options & SO_TIMESTAMP)
271121674Sume					ip6_savecontrol(last, n, &opts);
27253541Sshin				/* strip intermediate headers */
27353541Sshin				m_adj(n, *offp);
274186141Sbz				if (sbappendaddr(&last->inp_socket->so_rcv,
275121901Sume						(struct sockaddr *)&fromsa,
27653541Sshin						 n, opts) == 0) {
27753541Sshin					m_freem(n);
27853541Sshin					if (opts)
27953541Sshin						m_freem(opts);
280181803Sbz					V_rip6stat.rip6s_fullsock++;
28197658Stanimura				} else
282186141Sbz					sorwakeup(last->inp_socket);
28353541Sshin				opts = NULL;
28453541Sshin			}
285178377Srwatson			INP_RUNLOCK(last);
28653541Sshin		}
28753541Sshin		last = in6p;
28853541Sshin	}
289181803Sbz	INP_INFO_RUNLOCK(&V_ripcbinfo);
290171167Sgnn#ifdef IPSEC
29178064Sume	/*
29278064Sume	 * Check AH/ESP integrity.
29378064Sume	 */
294186170Skmacy	if ((last != NULL) && ipsec6_in_reject(m, last)) {
29578064Sume		m_freem(m);
296181803Sbz		V_ipsec6stat.in_polvio++;
297181803Sbz		V_ip6stat.ip6s_delivered--;
298180305Srwatson		/* Do not inject data into pcb. */
299178377Srwatson		INP_RUNLOCK(last);
300105199Ssam	} else
301171167Sgnn#endif /* IPSEC */
302186163Skmacy	if (last != NULL) {
303186223Sbz		if (last->inp_flags & INP_CONTROLOPTS ||
304186141Sbz		    last->inp_socket->so_options & SO_TIMESTAMP)
305121674Sume			ip6_savecontrol(last, m, &opts);
306180305Srwatson		/* Strip intermediate headers. */
30753541Sshin		m_adj(m, *offp);
308186141Sbz		if (sbappendaddr(&last->inp_socket->so_rcv,
309180305Srwatson		    (struct sockaddr *)&fromsa, m, opts) == 0) {
31053541Sshin			m_freem(m);
31153541Sshin			if (opts)
31253541Sshin				m_freem(opts);
313181803Sbz			V_rip6stat.rip6s_fullsock++;
31497658Stanimura		} else
315186141Sbz			sorwakeup(last->inp_socket);
316178377Srwatson		INP_RUNLOCK(last);
31753541Sshin	} else {
318181803Sbz		V_rip6stat.rip6s_nosock++;
31978064Sume		if (m->m_flags & M_MCAST)
320181803Sbz			V_rip6stat.rip6s_nosockmcast++;
32153541Sshin		if (proto == IPPROTO_NONE)
32253541Sshin			m_freem(m);
32353541Sshin		else {
32453541Sshin			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
32553541Sshin			icmp6_error(m, ICMP6_PARAM_PROB,
326180305Srwatson			    ICMP6_PARAMPROB_NEXTHEADER,
327180305Srwatson			    prvnxtp - mtod(m, char *));
32853541Sshin		}
329181803Sbz		V_ip6stat.ip6s_delivered--;
33053541Sshin	}
331180305Srwatson	return (IPPROTO_DONE);
33253541Sshin}
33353541Sshin
33462587Sitojunvoid
335171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
33662587Sitojun{
33762587Sitojun	struct ip6_hdr *ip6;
33862587Sitojun	struct mbuf *m;
33962587Sitojun	int off = 0;
34078064Sume	struct ip6ctlparam *ip6cp = NULL;
34178064Sume	const struct sockaddr_in6 *sa6_src = NULL;
342125776Sume	void *cmdarg;
343175162Sobrien	struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
34462587Sitojun
34562587Sitojun	if (sa->sa_family != AF_INET6 ||
34662587Sitojun	    sa->sa_len != sizeof(struct sockaddr_in6))
34762587Sitojun		return;
34862587Sitojun
34962587Sitojun	if ((unsigned)cmd >= PRC_NCMDS)
35062587Sitojun		return;
35162587Sitojun	if (PRC_IS_REDIRECT(cmd))
35262587Sitojun		notify = in6_rtchange, d = NULL;
35362587Sitojun	else if (cmd == PRC_HOSTDEAD)
35462587Sitojun		d = NULL;
35562587Sitojun	else if (inet6ctlerrmap[cmd] == 0)
35662587Sitojun		return;
35762587Sitojun
358180305Srwatson	/*
359180305Srwatson	 * If the parameter is from icmp6, decode it.
360180305Srwatson	 */
36162587Sitojun	if (d != NULL) {
36278064Sume		ip6cp = (struct ip6ctlparam *)d;
36362587Sitojun		m = ip6cp->ip6c_m;
36462587Sitojun		ip6 = ip6cp->ip6c_ip6;
36562587Sitojun		off = ip6cp->ip6c_off;
366125776Sume		cmdarg = ip6cp->ip6c_cmdarg;
36778064Sume		sa6_src = ip6cp->ip6c_src;
36862587Sitojun	} else {
36962587Sitojun		m = NULL;
37062587Sitojun		ip6 = NULL;
371125776Sume		cmdarg = NULL;
37278064Sume		sa6_src = &sa6_any;
37362587Sitojun	}
37462587Sitojun
375181803Sbz	(void) in6_pcbnotify(&V_ripcbinfo, sa, 0,
376180305Srwatson	    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
37762587Sitojun}
37862587Sitojun
37953541Sshin/*
380180305Srwatson * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
381180305Srwatson * may have setup with control call.
38253541Sshin */
38353541Sshinint
38453541Sshin#if __STDC__
38553541Sshinrip6_output(struct mbuf *m, ...)
38653541Sshin#else
38753541Sshinrip6_output(m, va_alist)
38853541Sshin	struct mbuf *m;
38953541Sshin	va_dcl
39053541Sshin#endif
39153541Sshin{
392120941Sume	struct mbuf *control;
39353541Sshin	struct socket *so;
39453541Sshin	struct sockaddr_in6 *dstsock;
39553541Sshin	struct in6_addr *dst;
39653541Sshin	struct ip6_hdr *ip6;
39753541Sshin	struct inpcb *in6p;
39853541Sshin	u_int	plen = m->m_pkthdr.len;
39953541Sshin	int error = 0;
400148247Sume	struct ip6_pktopts opt, *optp;
40153541Sshin	struct ifnet *oifp = NULL;
40253541Sshin	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
403148385Sume	int scope_ambiguous = 0;
404194777Sbz	struct in6_addr in6a;
40553541Sshin	va_list ap;
40653541Sshin
40753541Sshin	va_start(ap, m);
40853541Sshin	so = va_arg(ap, struct socket *);
40953541Sshin	dstsock = va_arg(ap, struct sockaddr_in6 *);
41053541Sshin	control = va_arg(ap, struct mbuf *);
41153541Sshin	va_end(ap);
41253541Sshin
413186141Sbz	in6p = sotoinpcb(so);
414178285Srwatson	INP_WLOCK(in6p);
41553541Sshin
41653541Sshin	dst = &dstsock->sin6_addr;
417186170Skmacy	if (control != NULL) {
418148242Sume		if ((error = ip6_setpktopts(control, &opt,
419175630Sbz		    in6p->in6p_outputopts, so->so_cred,
420175630Sbz		    so->so_proto->pr_protocol)) != 0) {
42153541Sshin			goto bad;
422121472Sume		}
423148247Sume		optp = &opt;
424148247Sume	} else
425148247Sume		optp = in6p->in6p_outputopts;
42653541Sshin
42753541Sshin	/*
428148385Sume	 * Check and convert scope zone ID into internal form.
429180305Srwatson	 *
430148385Sume	 * XXX: we may still need to determine the zone later.
431148385Sume	 */
432148385Sume	if (!(so->so_state & SS_ISCONNECTED)) {
433181803Sbz		if (dstsock->sin6_scope_id == 0 && !V_ip6_use_defzone)
434148385Sume			scope_ambiguous = 1;
435181803Sbz		if ((error = sa6_embedscope(dstsock, V_ip6_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
455133592Srwatson	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
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	 */
465194777Sbz	error = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred,
466194777Sbz	    &oifp, &in6a);
467194777Sbz	if (error)
468121472Sume		goto bad;
469207277Sbz	error = prison_check_ip6(in6p->inp_cred, &in6a);
470188144Sjamie	if (error != 0)
471188144Sjamie		goto bad;
472194777Sbz	ip6->ip6_src = in6a;
473148385Sume
474148385Sume	if (oifp && scope_ambiguous) {
475148385Sume		/*
476148385Sume		 * Application should provide a proper zone ID or the use of
477148385Sume		 * default zone IDs should be enabled.  Unfortunately, some
478148385Sume		 * applications do not behave as it should, so we need a
479148385Sume		 * workaround.  Even if an appropriate ID is not determined
480148385Sume		 * (when it's required), if we can determine the outgoing
481148385Sume		 * interface. determine the zone ID based on the interface.
482148385Sume		 */
483148385Sume		error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
484148385Sume		if (error != 0)
485148385Sume			goto bad;
486148385Sume	}
487148385Sume	ip6->ip6_dst = dstsock->sin6_addr;
488148385Sume
489180305Srwatson	/*
490180305Srwatson	 * Fill in the rest of the IPv6 header fields.
491180305Srwatson	 */
49255009Sshin	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
493186141Sbz	    (in6p->inp_flow & IPV6_FLOWINFO_MASK);
49455009Sshin	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
495180305Srwatson	    (IPV6_VERSION & IPV6_VERSION_MASK);
496180305Srwatson
497180305Srwatson	/*
498180305Srwatson	 * ip6_plen will be filled in ip6_output, so not fill it here.
499180305Srwatson	 */
500186141Sbz	ip6->ip6_nxt = in6p->inp_ip_p;
50153541Sshin	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
50253541Sshin
50353541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
50453541Sshin	    in6p->in6p_cksum != -1) {
50553541Sshin		struct mbuf *n;
50653541Sshin		int off;
50753541Sshin		u_int16_t *p;
50853541Sshin
509180305Srwatson		/* Compute checksum. */
51053541Sshin		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
51153541Sshin			off = offsetof(struct icmp6_hdr, icmp6_cksum);
51253541Sshin		else
51353541Sshin			off = in6p->in6p_cksum;
51453541Sshin		if (plen < off + 1) {
51553541Sshin			error = EINVAL;
51653541Sshin			goto bad;
51753541Sshin		}
51853541Sshin		off += sizeof(struct ip6_hdr);
51953541Sshin
52053541Sshin		n = m;
52153541Sshin		while (n && n->m_len <= off) {
52253541Sshin			off -= n->m_len;
52353541Sshin			n = n->m_next;
52453541Sshin		}
52553541Sshin		if (!n)
52653541Sshin			goto bad;
52753541Sshin		p = (u_int16_t *)(mtod(n, caddr_t) + off);
52853541Sshin		*p = 0;
52953541Sshin		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
53053541Sshin	}
53153541Sshin
532148247Sume	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
53353541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
53453541Sshin		if (oifp)
53553541Sshin			icmp6_ifoutstat_inc(oifp, type, code);
536190964Srwatson		ICMP6STAT_INC(icp6s_outhist[type]);
53778064Sume	} else
538181803Sbz		V_rip6stat.rip6s_opackets++;
53953541Sshin
54053541Sshin	goto freectl;
54153541Sshin
54253541Sshin bad:
54353541Sshin	if (m)
54453541Sshin		m_freem(m);
54553541Sshin
54653541Sshin freectl:
547186170Skmacy	if (control != NULL) {
548148247Sume		ip6_clearpktopts(&opt, -1);
54953541Sshin		m_freem(control);
55078064Sume	}
551178285Srwatson	INP_WUNLOCK(in6p);
552120856Sume	return (error);
55353541Sshin}
55453541Sshin
55553541Sshin/*
55653541Sshin * Raw IPv6 socket option processing.
55753541Sshin */
55853541Sshinint
559171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt)
56053541Sshin{
56153541Sshin	int error;
56253541Sshin
56353541Sshin	if (sopt->sopt_level == IPPROTO_ICMPV6)
56453541Sshin		/*
56553541Sshin		 * XXX: is it better to call icmp6_ctloutput() directly
56653541Sshin		 * from protosw?
56753541Sshin		 */
568120856Sume		return (icmp6_ctloutput(so, sopt));
56953541Sshin	else if (sopt->sopt_level != IPPROTO_IPV6)
57053541Sshin		return (EINVAL);
57153541Sshin
57253541Sshin	error = 0;
57353541Sshin
57453541Sshin	switch (sopt->sopt_dir) {
57553541Sshin	case SOPT_GET:
57653541Sshin		switch (sopt->sopt_name) {
57756723Sshin		case MRT6_INIT:
57856723Sshin		case MRT6_DONE:
57956723Sshin		case MRT6_ADD_MIF:
58056723Sshin		case MRT6_DEL_MIF:
58156723Sshin		case MRT6_ADD_MFC:
58256723Sshin		case MRT6_DEL_MFC:
58356723Sshin		case MRT6_PIM:
584166938Sbms			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
585166938Sbms			    EOPNOTSUPP;
58656723Sshin			break;
587121578Sume		case IPV6_CHECKSUM:
588121578Sume			error = ip6_raw_ctloutput(so, sopt);
589121578Sume			break;
59053541Sshin		default:
59153541Sshin			error = ip6_ctloutput(so, sopt);
59253541Sshin			break;
59353541Sshin		}
59453541Sshin		break;
59553541Sshin
59653541Sshin	case SOPT_SET:
59753541Sshin		switch (sopt->sopt_name) {
59856723Sshin		case MRT6_INIT:
59956723Sshin		case MRT6_DONE:
60056723Sshin		case MRT6_ADD_MIF:
60156723Sshin		case MRT6_DEL_MIF:
60256723Sshin		case MRT6_ADD_MFC:
60356723Sshin		case MRT6_DEL_MFC:
60456723Sshin		case MRT6_PIM:
605166938Sbms			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
606166938Sbms			    EOPNOTSUPP;
60756723Sshin			break;
608121578Sume		case IPV6_CHECKSUM:
609121578Sume			error = ip6_raw_ctloutput(so, sopt);
610121578Sume			break;
61153541Sshin		default:
61253541Sshin			error = ip6_ctloutput(so, sopt);
61353541Sshin			break;
61453541Sshin		}
61553541Sshin		break;
61653541Sshin	}
61753541Sshin
61853541Sshin	return (error);
61953541Sshin}
62053541Sshin
62153541Sshinstatic int
62283366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td)
62353541Sshin{
62453541Sshin	struct inpcb *inp;
625144261Ssam	struct icmp6_filter *filter;
626157676Srwatson	int error;
62753541Sshin
62853541Sshin	inp = sotoinpcb(so);
629157374Srwatson	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
630180305Srwatson
631175630Sbz	error = priv_check(td, PRIV_NETINET_RAW);
632175630Sbz	if (error)
633180305Srwatson		return (error);
63455009Sshin	error = soreserve(so, rip_sendspace, rip_recvspace);
635157374Srwatson	if (error)
636180305Srwatson		return (error);
637184214Sdes	filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
638157374Srwatson	if (filter == NULL)
639180305Srwatson		return (ENOMEM);
640181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
641181803Sbz	error = in_pcballoc(so, &V_ripcbinfo);
642132714Srwatson	if (error) {
643181803Sbz		INP_INFO_WUNLOCK(&V_ripcbinfo);
644184205Sdes		free(filter, M_PCB);
645180305Srwatson		return (error);
646132714Srwatson	}
64753541Sshin	inp = (struct inpcb *)so->so_pcb;
648181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
64953541Sshin	inp->inp_vflag |= INP_IPV6;
650186141Sbz	inp->inp_ip_p = (long)proto;
65153541Sshin	inp->in6p_hops = -1;	/* use kernel default */
65253541Sshin	inp->in6p_cksum = -1;
653144261Ssam	inp->in6p_icmp6filt = filter;
65453541Sshin	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
655178285Srwatson	INP_WUNLOCK(inp);
656180305Srwatson	return (0);
65753541Sshin}
65853541Sshin
659157370Srwatsonstatic void
66053541Sshinrip6_detach(struct socket *so)
66153541Sshin{
66253541Sshin	struct inpcb *inp;
66353541Sshin
66453541Sshin	inp = sotoinpcb(so);
665157374Srwatson	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
666160549Srwatson
667191672Sbms	if (so == V_ip6_mrouter && ip6_mrouter_done)
668166938Sbms		ip6_mrouter_done();
66953541Sshin	/* xxx: RSVP */
670181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
671178285Srwatson	INP_WLOCK(inp);
672184205Sdes	free(inp->in6p_icmp6filt, M_PCB);
673185344Sbz	in_pcbdetach(inp);
674185370Sbz	in_pcbfree(inp);
675181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
67653541Sshin}
67753541Sshin
678160549Srwatson/* XXXRW: This can't ever be called. */
679157366Srwatsonstatic void
68053541Sshinrip6_abort(struct socket *so)
68153541Sshin{
682160549Srwatson	struct inpcb *inp;
683160549Srwatson
684160549Srwatson	inp = sotoinpcb(so);
685160549Srwatson	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
686160549Srwatson
68753541Sshin	soisdisconnected(so);
68853541Sshin}
68953541Sshin
690160549Srwatsonstatic void
691160549Srwatsonrip6_close(struct socket *so)
692160549Srwatson{
693160549Srwatson	struct inpcb *inp;
694160549Srwatson
695160549Srwatson	inp = sotoinpcb(so);
696160549Srwatson	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
697160549Srwatson
698160549Srwatson	soisdisconnected(so);
699160549Srwatson}
700160549Srwatson
70153541Sshinstatic int
70253541Sshinrip6_disconnect(struct socket *so)
70353541Sshin{
704180305Srwatson	struct inpcb *inp;
70553541Sshin
706180305Srwatson	inp = sotoinpcb(so);
707180305Srwatson	KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
708180305Srwatson
70997658Stanimura	if ((so->so_state & SS_ISCONNECTED) == 0)
710180305Srwatson		return (ENOTCONN);
71153541Sshin	inp->in6p_faddr = in6addr_any;
712157366Srwatson	rip6_abort(so);
713157374Srwatson	return (0);
71453541Sshin}
71553541Sshin
71653541Sshinstatic int
71783366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
71853541Sshin{
719180305Srwatson	struct inpcb *inp;
72053541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
721194760Srwatson	struct ifaddr *ifa = NULL;
722148385Sume	int error = 0;
72353541Sshin
724180305Srwatson	inp = sotoinpcb(so);
725157374Srwatson	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
726180305Srwatson
72753541Sshin	if (nam->sa_len != sizeof(*addr))
728180305Srwatson		return (EINVAL);
729188144Sjamie	if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
730188144Sjamie		return (error);
731181803Sbz	if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
732180305Srwatson		return (EADDRNOTAVAIL);
733181803Sbz	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
734180305Srwatson		return (error);
735148385Sume
73653541Sshin	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
737194760Srwatson	    (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL)
738180305Srwatson		return (EADDRNOTAVAIL);
739194760Srwatson	if (ifa != NULL &&
740194760Srwatson	    ((struct in6_ifaddr *)ifa)->ia6_flags &
74153541Sshin	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
74253541Sshin	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
743194760Srwatson		ifa_free(ifa);
744120856Sume		return (EADDRNOTAVAIL);
74553541Sshin	}
746194760Srwatson	if (ifa != NULL)
747194760Srwatson		ifa_free(ifa);
748181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
749178285Srwatson	INP_WLOCK(inp);
75053541Sshin	inp->in6p_laddr = addr->sin6_addr;
751178285Srwatson	INP_WUNLOCK(inp);
752181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
753180305Srwatson	return (0);
75453541Sshin}
75553541Sshin
75653541Sshinstatic int
75783366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
75853541Sshin{
759180305Srwatson	struct inpcb *inp;
76053541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
761194777Sbz	struct in6_addr in6a;
762148385Sume	struct ifnet *ifp = NULL;
763148385Sume	int error = 0, scope_ambiguous = 0;
76453541Sshin
765180305Srwatson	inp = sotoinpcb(so);
766157374Srwatson	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
767180305Srwatson
76853541Sshin	if (nam->sa_len != sizeof(*addr))
769180305Srwatson		return (EINVAL);
770181803Sbz	if (TAILQ_EMPTY(&V_ifnet))
771180305Srwatson		return (EADDRNOTAVAIL);
77253541Sshin	if (addr->sin6_family != AF_INET6)
773180305Srwatson		return (EAFNOSUPPORT);
774148385Sume
775148385Sume	/*
776180305Srwatson	 * Application should provide a proper zone ID or the use of default
777180305Srwatson	 * zone IDs should be enabled.  Unfortunately, some applications do
778180305Srwatson	 * not behave as it should, so we need a workaround.  Even if an
779180305Srwatson	 * appropriate ID is not determined, we'll see if we can determine
780180305Srwatson	 * the outgoing interface.  If we can, determine the zone ID based on
781180305Srwatson	 * the interface below.
782148385Sume	 */
783181803Sbz	if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
784148385Sume		scope_ambiguous = 1;
785181803Sbz	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
786180305Srwatson		return (error);
787148385Sume
788181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
789178285Srwatson	INP_WLOCK(inp);
79053541Sshin	/* Source address selection. XXX: need pcblookup? */
791194777Sbz	error = in6_selectsrc(addr, inp->in6p_outputopts,
792194777Sbz	    inp, NULL, so->so_cred, &ifp, &in6a);
793194777Sbz	if (error) {
794178285Srwatson		INP_WUNLOCK(inp);
795181803Sbz		INP_INFO_WUNLOCK(&V_ripcbinfo);
796194777Sbz		return (error);
797132714Srwatson	}
798148385Sume
799148385Sume	/* XXX: see above */
800148385Sume	if (ifp && scope_ambiguous &&
801148385Sume	    (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
802178285Srwatson		INP_WUNLOCK(inp);
803181803Sbz		INP_INFO_WUNLOCK(&V_ripcbinfo);
804180305Srwatson		return (error);
805148385Sume	}
806148385Sume	inp->in6p_faddr = addr->sin6_addr;
807194777Sbz	inp->in6p_laddr = in6a;
80853541Sshin	soisconnected(so);
809178285Srwatson	INP_WUNLOCK(inp);
810181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
811180305Srwatson	return (0);
81253541Sshin}
81353541Sshin
81453541Sshinstatic int
81553541Sshinrip6_shutdown(struct socket *so)
81653541Sshin{
817132714Srwatson	struct inpcb *inp;
818132714Srwatson
819132714Srwatson	inp = sotoinpcb(so);
820157374Srwatson	KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
821180305Srwatson
822178285Srwatson	INP_WLOCK(inp);
82353541Sshin	socantsendmore(so);
824178285Srwatson	INP_WUNLOCK(inp);
825180305Srwatson	return (0);
82653541Sshin}
82753541Sshin
82853541Sshinstatic int
82953541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
830171260Sdelphij    struct mbuf *control, struct thread *td)
83153541Sshin{
832180305Srwatson	struct inpcb *inp;
83353541Sshin	struct sockaddr_in6 tmp;
83453541Sshin	struct sockaddr_in6 *dst;
835132714Srwatson	int ret;
83653541Sshin
837180305Srwatson	inp = sotoinpcb(so);
838157374Srwatson	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
839180305Srwatson
840180305Srwatson	/* Always copy sockaddr to avoid overwrites. */
841132714Srwatson	/* Unlocked read. */
84253541Sshin	if (so->so_state & SS_ISCONNECTED) {
84353541Sshin		if (nam) {
84453541Sshin			m_freem(m);
845180305Srwatson			return (EISCONN);
84653541Sshin		}
84753541Sshin		/* XXX */
84853541Sshin		bzero(&tmp, sizeof(tmp));
84953541Sshin		tmp.sin6_family = AF_INET6;
85053541Sshin		tmp.sin6_len = sizeof(struct sockaddr_in6);
851180990Srwatson		INP_RLOCK(inp);
85253541Sshin		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
853180990Srwatson		    sizeof(struct in6_addr));
854180990Srwatson		INP_RUNLOCK(inp);
85553541Sshin		dst = &tmp;
85653541Sshin	} else {
85753541Sshin		if (nam == NULL) {
85853541Sshin			m_freem(m);
859180305Srwatson			return (ENOTCONN);
86053541Sshin		}
861148385Sume		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
862148385Sume			m_freem(m);
863180305Srwatson			return (EINVAL);
864148385Sume		}
86562587Sitojun		tmp = *(struct sockaddr_in6 *)nam;
86662587Sitojun		dst = &tmp;
867148385Sume
868148385Sume		if (dst->sin6_family == AF_UNSPEC) {
869148385Sume			/*
870148385Sume			 * XXX: we allow this case for backward
871148385Sume			 * compatibility to buggy applications that
872148385Sume			 * rely on old (and wrong) kernel behavior.
873148385Sume			 */
874148385Sume			log(LOG_INFO, "rip6 SEND: address family is "
875148385Sume			    "unspec. Assume AF_INET6\n");
876148385Sume			dst->sin6_family = AF_INET6;
877148385Sume		} else if (dst->sin6_family != AF_INET6) {
878148385Sume			m_freem(m);
879148385Sume			return(EAFNOSUPPORT);
880148385Sume		}
88153541Sshin	}
882132714Srwatson	ret = rip6_output(m, so, dst, control);
883132714Srwatson	return (ret);
88453541Sshin}
88553541Sshin
88653541Sshinstruct pr_usrreqs rip6_usrreqs = {
887137386Sphk	.pru_abort =		rip6_abort,
888137386Sphk	.pru_attach =		rip6_attach,
889137386Sphk	.pru_bind =		rip6_bind,
890137386Sphk	.pru_connect =		rip6_connect,
891137386Sphk	.pru_control =		in6_control,
892137386Sphk	.pru_detach =		rip6_detach,
893137386Sphk	.pru_disconnect =	rip6_disconnect,
894169462Srwatson	.pru_peeraddr =		in6_getpeeraddr,
895137386Sphk	.pru_send =		rip6_send,
896137386Sphk	.pru_shutdown =		rip6_shutdown,
897169462Srwatson	.pru_sockaddr =		in6_getsockaddr,
898160549Srwatson	.pru_close =		rip6_close,
89953541Sshin};
900