raw_ip6.c revision 148242
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 * $FreeBSD: head/sys/netinet6/raw_ip6.c 148242 2005-07-21 15:06:32Z ume $
3053541Sshin */
3153541Sshin
32139826Simp/*-
3353541Sshin * Copyright (c) 1982, 1986, 1988, 1993
3453541Sshin *	The Regents of the University of California.  All rights reserved.
3553541Sshin *
3653541Sshin * Redistribution and use in source and binary forms, with or without
3753541Sshin * modification, are permitted provided that the following conditions
3853541Sshin * are met:
3953541Sshin * 1. Redistributions of source code must retain the above copyright
4053541Sshin *    notice, this list of conditions and the following disclaimer.
4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4253541Sshin *    notice, this list of conditions and the following disclaimer in the
4353541Sshin *    documentation and/or other materials provided with the distribution.
4453541Sshin * 4. Neither the name of the University nor the names of its contributors
4553541Sshin *    may be used to endorse or promote products derived from this software
4653541Sshin *    without specific prior written permission.
4753541Sshin *
4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5153541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5853541Sshin * SUCH DAMAGE.
5953541Sshin *
6053541Sshin *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
6153541Sshin */
6253541Sshin
6355009Sshin#include "opt_ipsec.h"
6478064Sume#include "opt_inet6.h"
6555009Sshin
6653541Sshin#include <sys/param.h>
6795759Stanimura#include <sys/errno.h>
6895759Stanimura#include <sys/lock.h>
6953541Sshin#include <sys/malloc.h>
7095759Stanimura#include <sys/mbuf.h>
7153541Sshin#include <sys/proc.h>
7295759Stanimura#include <sys/protosw.h>
7395759Stanimura#include <sys/signalvar.h>
7453541Sshin#include <sys/socket.h>
7553541Sshin#include <sys/socketvar.h>
7695759Stanimura#include <sys/sx.h>
7753541Sshin#include <sys/systm.h>
7853541Sshin
7953541Sshin#include <net/if.h>
8095759Stanimura#include <net/if_types.h>
8153541Sshin#include <net/route.h>
8253541Sshin
8353541Sshin#include <netinet/in.h>
8453541Sshin#include <netinet/in_var.h>
8553541Sshin#include <netinet/in_systm.h>
8695759Stanimura#include <netinet/icmp6.h>
8795759Stanimura#include <netinet/in_pcb.h>
8862587Sitojun#include <netinet/ip6.h>
8995759Stanimura#include <netinet6/ip6protosw.h>
9056723Sshin#include <netinet6/ip6_mroute.h>
9153541Sshin#include <netinet6/in6_pcb.h>
9295759Stanimura#include <netinet6/ip6_var.h>
9353541Sshin#include <netinet6/nd6.h>
9495759Stanimura#include <netinet6/raw_ip6.h>
9562587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
9662587Sitojun#include <netinet6/scope6_var.h>
9762587Sitojun#endif
9853541Sshin
9953541Sshin#ifdef IPSEC
10053541Sshin#include <netinet6/ipsec.h>
10153541Sshin#include <netinet6/ipsec6.h>
10253541Sshin#endif /*IPSEC*/
10353541Sshin
104105199Ssam#ifdef FAST_IPSEC
105105199Ssam#include <netipsec/ipsec.h>
106105199Ssam#include <netipsec/ipsec6.h>
107105199Ssam#endif /* FAST_IPSEC */
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
11853541Sshinextern struct	inpcbhead ripcb;
11953541Sshinextern struct	inpcbinfo ripcbinfo;
12053541Sshinextern u_long	rip_sendspace;
12153541Sshinextern u_long	rip_recvspace;
12253541Sshin
12378064Sumestruct rip6stat rip6stat;
12478064Sume
12553541Sshin/*
12653541Sshin * Setup generic address and protocol structures
12753541Sshin * for raw_input routine, then pass them along with
12853541Sshin * mbuf chain.
12953541Sshin */
13053541Sshinint
13153541Sshinrip6_input(mp, offp, proto)
13253541Sshin	struct	mbuf **mp;
13353541Sshin	int	*offp, proto;
13453541Sshin{
13553541Sshin	struct mbuf *m = *mp;
13653541Sshin	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
13753541Sshin	register struct inpcb *in6p;
13853541Sshin	struct inpcb *last = 0;
13978064Sume	struct mbuf *opts = NULL;
140121901Sume	struct sockaddr_in6 fromsa;
14153541Sshin
14278064Sume	rip6stat.rip6s_ipackets++;
14378064Sume
14483934Sbrooks	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
14578064Sume		/* XXX send icmp6 host/port unreach? */
14678064Sume		m_freem(m);
14778064Sume		return IPPROTO_DONE;
14853541Sshin	}
14978064Sume
150121901Sume	init_sin6(&fromsa, m); /* general init */
15153541Sshin
152132714Srwatson	INP_INFO_RLOCK(&ripcbinfo);
15353541Sshin	LIST_FOREACH(in6p, &ripcb, inp_list) {
154132714Srwatson		INP_LOCK(in6p);
155132714Srwatson		if ((in6p->in6p_vflag & INP_IPV6) == 0) {
156132714Srwatsondocontinue:
157132714Srwatson			INP_UNLOCK(in6p);
15853541Sshin			continue;
159132714Srwatson		}
16053541Sshin		if (in6p->in6p_ip6_nxt &&
16153541Sshin		    in6p->in6p_ip6_nxt != proto)
162132714Srwatson			goto docontinue;
16353541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
16453541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
165132714Srwatson			goto docontinue;
16653541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
16753541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
168132714Srwatson			goto docontinue;
16978064Sume		if (in6p->in6p_cksum != -1) {
17078064Sume			rip6stat.rip6s_isum++;
17178064Sume			if (in6_cksum(m, ip6->ip6_nxt, *offp,
17278064Sume			    m->m_pkthdr.len - *offp)) {
17378064Sume				rip6stat.rip6s_badsum++;
174132714Srwatson				goto docontinue;
17578064Sume			}
17653541Sshin		}
17753541Sshin		if (last) {
17853541Sshin			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
17978064Sume
180125941Sume#if defined(IPSEC) || defined(FAST_IPSEC)
18178064Sume			/*
18278064Sume			 * Check AH/ESP integrity.
18378064Sume			 */
184125396Sume			if (n && ipsec6_in_reject(n, last)) {
18578064Sume				m_freem(n);
186125941Sume#ifdef IPSEC
18778064Sume				ipsec6stat.in_polvio++;
18878064Sume#endif /*IPSEC*/
189105199Ssam				/* do not inject data into pcb */
190105199Ssam			} else
191125941Sume#endif /*IPSEC || FAST_IPSEC*/
19253541Sshin			if (n) {
19397658Stanimura				if (last->in6p_flags & IN6P_CONTROLOPTS ||
19497658Stanimura				    last->in6p_socket->so_options & SO_TIMESTAMP)
195121674Sume					ip6_savecontrol(last, n, &opts);
19653541Sshin				/* strip intermediate headers */
19753541Sshin				m_adj(n, *offp);
19853541Sshin				if (sbappendaddr(&last->in6p_socket->so_rcv,
199121901Sume						(struct sockaddr *)&fromsa,
20053541Sshin						 n, opts) == 0) {
20153541Sshin					m_freem(n);
20253541Sshin					if (opts)
20353541Sshin						m_freem(opts);
20478064Sume					rip6stat.rip6s_fullsock++;
20597658Stanimura				} else
20653541Sshin					sorwakeup(last->in6p_socket);
20753541Sshin				opts = NULL;
20853541Sshin			}
209132714Srwatson			INP_UNLOCK(last);
21053541Sshin		}
21153541Sshin		last = in6p;
21253541Sshin	}
213125941Sume#if defined(IPSEC) || defined(FAST_IPSEC)
21478064Sume	/*
21578064Sume	 * Check AH/ESP integrity.
21678064Sume	 */
217125396Sume	if (last && ipsec6_in_reject(m, last)) {
21878064Sume		m_freem(m);
219125941Sume#ifdef IPSEC
22078064Sume		ipsec6stat.in_polvio++;
22178064Sume#endif /*IPSEC*/
222105199Ssam		ip6stat.ip6s_delivered--;
223105199Ssam		/* do not inject data into pcb */
224105199Ssam	} else
225125941Sume#endif /*IPSEC || FAST_IPSEC*/
22653541Sshin	if (last) {
22797658Stanimura		if (last->in6p_flags & IN6P_CONTROLOPTS ||
22897658Stanimura		    last->in6p_socket->so_options & SO_TIMESTAMP)
229121674Sume			ip6_savecontrol(last, m, &opts);
23053541Sshin		/* strip intermediate headers */
23153541Sshin		m_adj(m, *offp);
23253541Sshin		if (sbappendaddr(&last->in6p_socket->so_rcv,
233121901Sume				(struct sockaddr *)&fromsa, m, opts) == 0) {
23453541Sshin			m_freem(m);
23553541Sshin			if (opts)
23653541Sshin				m_freem(opts);
23778064Sume			rip6stat.rip6s_fullsock++;
23897658Stanimura		} else
23953541Sshin			sorwakeup(last->in6p_socket);
240132714Srwatson		INP_UNLOCK(last);
24153541Sshin	} else {
24278064Sume		rip6stat.rip6s_nosock++;
24378064Sume		if (m->m_flags & M_MCAST)
24478064Sume			rip6stat.rip6s_nosockmcast++;
24553541Sshin		if (proto == IPPROTO_NONE)
24653541Sshin			m_freem(m);
24753541Sshin		else {
24853541Sshin			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
24953541Sshin			icmp6_error(m, ICMP6_PARAM_PROB,
25053541Sshin				    ICMP6_PARAMPROB_NEXTHEADER,
25153541Sshin				    prvnxtp - mtod(m, char *));
25253541Sshin		}
25353541Sshin		ip6stat.ip6s_delivered--;
25453541Sshin	}
255134655Srwatson	INP_INFO_RUNLOCK(&ripcbinfo);
25653541Sshin	return IPPROTO_DONE;
25753541Sshin}
25853541Sshin
25962587Sitojunvoid
26062587Sitojunrip6_ctlinput(cmd, sa, d)
26162587Sitojun	int cmd;
26262587Sitojun	struct sockaddr *sa;
26362587Sitojun	void *d;
26462587Sitojun{
26562587Sitojun	struct ip6_hdr *ip6;
26662587Sitojun	struct mbuf *m;
26762587Sitojun	int off = 0;
26878064Sume	struct ip6ctlparam *ip6cp = NULL;
26978064Sume	const struct sockaddr_in6 *sa6_src = NULL;
270125776Sume	void *cmdarg;
27198211Shsu	struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange;
27262587Sitojun
27362587Sitojun	if (sa->sa_family != AF_INET6 ||
27462587Sitojun	    sa->sa_len != sizeof(struct sockaddr_in6))
27562587Sitojun		return;
27662587Sitojun
27762587Sitojun	if ((unsigned)cmd >= PRC_NCMDS)
27862587Sitojun		return;
27962587Sitojun	if (PRC_IS_REDIRECT(cmd))
28062587Sitojun		notify = in6_rtchange, d = NULL;
28162587Sitojun	else if (cmd == PRC_HOSTDEAD)
28262587Sitojun		d = NULL;
28362587Sitojun	else if (inet6ctlerrmap[cmd] == 0)
28462587Sitojun		return;
28562587Sitojun
28662587Sitojun	/* if the parameter is from icmp6, decode it. */
28762587Sitojun	if (d != NULL) {
28878064Sume		ip6cp = (struct ip6ctlparam *)d;
28962587Sitojun		m = ip6cp->ip6c_m;
29062587Sitojun		ip6 = ip6cp->ip6c_ip6;
29162587Sitojun		off = ip6cp->ip6c_off;
292125776Sume		cmdarg = ip6cp->ip6c_cmdarg;
29378064Sume		sa6_src = ip6cp->ip6c_src;
29462587Sitojun	} else {
29562587Sitojun		m = NULL;
29662587Sitojun		ip6 = NULL;
297125776Sume		cmdarg = NULL;
29878064Sume		sa6_src = &sa6_any;
29962587Sitojun	}
30062587Sitojun
301133192Srwatson	(void) in6_pcbnotify(&ripcbinfo, sa, 0,
302133192Srwatson			     (const struct sockaddr *)sa6_src,
303125776Sume			     0, cmd, cmdarg, notify);
30462587Sitojun}
30562587Sitojun
30653541Sshin/*
30753541Sshin * Generate IPv6 header and pass packet to ip6_output.
30853541Sshin * Tack on options user may have setup with control call.
30953541Sshin */
31053541Sshinint
31153541Sshin#if __STDC__
31253541Sshinrip6_output(struct mbuf *m, ...)
31353541Sshin#else
31453541Sshinrip6_output(m, va_alist)
31553541Sshin	struct mbuf *m;
31653541Sshin	va_dcl
31753541Sshin#endif
31853541Sshin{
319120941Sume	struct mbuf *control;
32053541Sshin	struct socket *so;
32153541Sshin	struct sockaddr_in6 *dstsock;
32253541Sshin	struct in6_addr *dst;
32353541Sshin	struct ip6_hdr *ip6;
32453541Sshin	struct inpcb *in6p;
32553541Sshin	u_int	plen = m->m_pkthdr.len;
32653541Sshin	int error = 0;
327121472Sume	struct ip6_pktopts opt, *stickyopt = NULL;
32853541Sshin	struct ifnet *oifp = NULL;
32953541Sshin	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
33053541Sshin	int priv = 0;
331121472Sume	struct in6_addr *in6a;
33253541Sshin	va_list ap;
33353541Sshin
33453541Sshin	va_start(ap, m);
33553541Sshin	so = va_arg(ap, struct socket *);
33653541Sshin	dstsock = va_arg(ap, struct sockaddr_in6 *);
33753541Sshin	control = va_arg(ap, struct mbuf *);
33853541Sshin	va_end(ap);
33953541Sshin
34053541Sshin	in6p = sotoin6pcb(so);
341132714Srwatson	INP_LOCK(in6p);
342121472Sume	stickyopt = in6p->in6p_outputopts;
34353541Sshin
34453541Sshin	priv = 0;
34553541Sshin	if (so->so_cred->cr_uid == 0)
34653541Sshin		priv = 1;
34753541Sshin	dst = &dstsock->sin6_addr;
34853541Sshin	if (control) {
349148242Sume		if ((error = ip6_setpktopts(control, &opt,
350148242Sume		    stickyopt, priv, 0, so->so_proto->pr_protocol))
351121472Sume		    != 0) {
35253541Sshin			goto bad;
353121472Sume		}
354121472Sume		in6p->in6p_outputopts = &opt;
355121472Sume	}
35653541Sshin
35753541Sshin	/*
35853541Sshin	 * For an ICMPv6 packet, we should know its type and code
35953541Sshin	 * to update statistics.
36053541Sshin	 */
36153541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
36253541Sshin		struct icmp6_hdr *icmp6;
36353541Sshin		if (m->m_len < sizeof(struct icmp6_hdr) &&
36453541Sshin		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
36553541Sshin			error = ENOBUFS;
36653541Sshin			goto bad;
36753541Sshin		}
36853541Sshin		icmp6 = mtod(m, struct icmp6_hdr *);
36953541Sshin		type = icmp6->icmp6_type;
37053541Sshin		code = icmp6->icmp6_code;
37153541Sshin	}
37253541Sshin
373133592Srwatson	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
374133592Srwatson	if (m == NULL) {
375133592Srwatson		error = ENOBUFS;
376133592Srwatson		goto bad;
377133592Srwatson	}
37853541Sshin	ip6 = mtod(m, struct ip6_hdr *);
37953541Sshin
38053541Sshin	/*
38153541Sshin	 * Next header might not be ICMP6 but use its pseudo header anyway.
38253541Sshin	 */
38353541Sshin	ip6->ip6_dst = *dst;
38453541Sshin
38553541Sshin	/*
38653541Sshin	 * If the scope of the destination is link-local, embed the interface
38753541Sshin	 * index in the address.
38853541Sshin	 *
38953541Sshin	 * XXX advanced-api value overrides sin6_scope_id
39053541Sshin	 */
39153541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
39253541Sshin		struct in6_pktinfo *pi;
39353541Sshin
39453541Sshin		/*
39553541Sshin		 * XXX Boundary check is assumed to be already done in
396148242Sume		 * ip6_setpktopts().
39753541Sshin		 */
398121472Sume		if (in6p->in6p_outputopts &&
399121472Sume		    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
400121472Sume		    pi->ipi6_ifindex) {
40153541Sshin			ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
40283130Sjlemon			oifp = ifnet_byindex(pi->ipi6_ifindex);
40353541Sshin		} else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
40453541Sshin			 in6p->in6p_moptions &&
40553541Sshin			 in6p->in6p_moptions->im6o_multicast_ifp) {
40653541Sshin			oifp = in6p->in6p_moptions->im6o_multicast_ifp;
40753541Sshin			ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
40853541Sshin		} else if (dstsock->sin6_scope_id) {
40953541Sshin			/* boundary check */
410120941Sume			if (dstsock->sin6_scope_id < 0 ||
411120941Sume			    if_index < dstsock->sin6_scope_id) {
41253541Sshin				error = ENXIO;  /* XXX EINVAL? */
41353541Sshin				goto bad;
41453541Sshin			}
415120941Sume			ip6->ip6_dst.s6_addr16[1] =
416120941Sume			    htons(dstsock->sin6_scope_id & 0xffff); /* XXX */
41753541Sshin		}
41853541Sshin	}
41953541Sshin
42053541Sshin	/*
42153541Sshin	 * Source address selection.
42253541Sshin	 */
423121472Sume	if ((in6a = in6_selectsrc(dstsock, in6p->in6p_outputopts,
424122927Sandre	    in6p->in6p_moptions, NULL, &in6p->in6p_laddr, &error)) == 0) {
425121472Sume		if (error == 0)
426121472Sume			error = EADDRNOTAVAIL;
427121472Sume		goto bad;
42853541Sshin	}
429121472Sume	ip6->ip6_src = *in6a;
43055009Sshin	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
43155009Sshin		(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
43255009Sshin	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
43355009Sshin		(IPV6_VERSION & IPV6_VERSION_MASK);
43453541Sshin	/* ip6_plen will be filled in ip6_output, so not fill it here. */
43553541Sshin	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
43653541Sshin	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
43753541Sshin
43853541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
43953541Sshin	    in6p->in6p_cksum != -1) {
44053541Sshin		struct mbuf *n;
44153541Sshin		int off;
44253541Sshin		u_int16_t *p;
44353541Sshin
44453541Sshin		/* compute checksum */
44553541Sshin		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
44653541Sshin			off = offsetof(struct icmp6_hdr, icmp6_cksum);
44753541Sshin		else
44853541Sshin			off = in6p->in6p_cksum;
44953541Sshin		if (plen < off + 1) {
45053541Sshin			error = EINVAL;
45153541Sshin			goto bad;
45253541Sshin		}
45353541Sshin		off += sizeof(struct ip6_hdr);
45453541Sshin
45553541Sshin		n = m;
45653541Sshin		while (n && n->m_len <= off) {
45753541Sshin			off -= n->m_len;
45853541Sshin			n = n->m_next;
45953541Sshin		}
46053541Sshin		if (!n)
46153541Sshin			goto bad;
46253541Sshin		p = (u_int16_t *)(mtod(n, caddr_t) + off);
46353541Sshin		*p = 0;
46453541Sshin		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
46553541Sshin	}
46653541Sshin
467122927Sandre	error = ip6_output(m, in6p->in6p_outputopts, NULL, 0,
468105194Ssam			   in6p->in6p_moptions, &oifp, in6p);
46953541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
47053541Sshin		if (oifp)
47153541Sshin			icmp6_ifoutstat_inc(oifp, type, code);
47253541Sshin		icmp6stat.icp6s_outhist[type]++;
47378064Sume	} else
47478064Sume		rip6stat.rip6s_opackets++;
47553541Sshin
47653541Sshin	goto freectl;
47753541Sshin
47853541Sshin bad:
47953541Sshin	if (m)
48053541Sshin		m_freem(m);
48153541Sshin
48253541Sshin freectl:
48378064Sume	if (control) {
484121472Sume		ip6_clearpktopts(in6p->in6p_outputopts, -1);
485121472Sume		in6p->in6p_outputopts = stickyopt;
48653541Sshin		m_freem(control);
48778064Sume	}
488132714Srwatson	INP_UNLOCK(in6p);
489120856Sume	return (error);
49053541Sshin}
49153541Sshin
49253541Sshin/*
49353541Sshin * Raw IPv6 socket option processing.
49453541Sshin */
49553541Sshinint
49653541Sshinrip6_ctloutput(so, sopt)
49753541Sshin	struct socket *so;
49853541Sshin	struct sockopt *sopt;
49953541Sshin{
50053541Sshin	int error;
50153541Sshin
50253541Sshin	if (sopt->sopt_level == IPPROTO_ICMPV6)
50353541Sshin		/*
50453541Sshin		 * XXX: is it better to call icmp6_ctloutput() directly
50553541Sshin		 * from protosw?
50653541Sshin		 */
507120856Sume		return (icmp6_ctloutput(so, sopt));
50853541Sshin	else if (sopt->sopt_level != IPPROTO_IPV6)
50953541Sshin		return (EINVAL);
51053541Sshin
51153541Sshin	error = 0;
51253541Sshin
51353541Sshin	switch (sopt->sopt_dir) {
51453541Sshin	case SOPT_GET:
51553541Sshin		switch (sopt->sopt_name) {
51656723Sshin		case MRT6_INIT:
51756723Sshin		case MRT6_DONE:
51856723Sshin		case MRT6_ADD_MIF:
51956723Sshin		case MRT6_DEL_MIF:
52056723Sshin		case MRT6_ADD_MFC:
52156723Sshin		case MRT6_DEL_MFC:
52256723Sshin		case MRT6_PIM:
52356723Sshin			error = ip6_mrouter_get(so, sopt);
52456723Sshin			break;
525121578Sume		case IPV6_CHECKSUM:
526121578Sume			error = ip6_raw_ctloutput(so, sopt);
527121578Sume			break;
52853541Sshin		default:
52953541Sshin			error = ip6_ctloutput(so, sopt);
53053541Sshin			break;
53153541Sshin		}
53253541Sshin		break;
53353541Sshin
53453541Sshin	case SOPT_SET:
53553541Sshin		switch (sopt->sopt_name) {
53656723Sshin		case MRT6_INIT:
53756723Sshin		case MRT6_DONE:
53856723Sshin		case MRT6_ADD_MIF:
53956723Sshin		case MRT6_DEL_MIF:
54056723Sshin		case MRT6_ADD_MFC:
54156723Sshin		case MRT6_DEL_MFC:
54256723Sshin		case MRT6_PIM:
54356723Sshin			error = ip6_mrouter_set(so, sopt);
54456723Sshin			break;
545121578Sume		case IPV6_CHECKSUM:
546121578Sume			error = ip6_raw_ctloutput(so, sopt);
547121578Sume			break;
54853541Sshin		default:
54953541Sshin			error = ip6_ctloutput(so, sopt);
55053541Sshin			break;
55153541Sshin		}
55253541Sshin		break;
55353541Sshin	}
55453541Sshin
55553541Sshin	return (error);
55653541Sshin}
55753541Sshin
55853541Sshinstatic int
55983366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td)
56053541Sshin{
56153541Sshin	struct inpcb *inp;
562144261Ssam	struct icmp6_filter *filter;
56353541Sshin	int error, s;
56453541Sshin
565132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
56653541Sshin	inp = sotoinpcb(so);
567132714Srwatson	if (inp) {
568132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
56953541Sshin		panic("rip6_attach");
570132714Srwatson	}
571132714Srwatson	if (td && (error = suser(td)) != 0) {
572132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
57353541Sshin		return error;
574132714Srwatson	}
57555009Sshin	error = soreserve(so, rip_sendspace, rip_recvspace);
576132714Srwatson	if (error) {
577132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
57855009Sshin		return error;
579132714Srwatson	}
580144261Ssam	MALLOC(filter, struct icmp6_filter *,
581144261Ssam	       sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
582144261Ssam	if (filter == NULL)
583144261Ssam		return ENOMEM;
58453541Sshin	s = splnet();
585127504Spjd	error = in_pcballoc(so, &ripcbinfo, "raw6inp");
58653541Sshin	splx(s);
587132714Srwatson	if (error) {
588132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
589144261Ssam		FREE(filter, M_PCB);
59053541Sshin		return error;
591132714Srwatson	}
59253541Sshin	inp = (struct inpcb *)so->so_pcb;
593132714Srwatson	INP_LOCK(inp);
594132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
59553541Sshin	inp->inp_vflag |= INP_IPV6;
59653541Sshin	inp->in6p_ip6_nxt = (long)proto;
59753541Sshin	inp->in6p_hops = -1;	/* use kernel default */
59853541Sshin	inp->in6p_cksum = -1;
599144261Ssam	inp->in6p_icmp6filt = filter;
60053541Sshin	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
601132714Srwatson	INP_UNLOCK(inp);
60253541Sshin	return 0;
60353541Sshin}
60453541Sshin
60553541Sshinstatic int
60653541Sshinrip6_detach(struct socket *so)
60753541Sshin{
60853541Sshin	struct inpcb *inp;
60953541Sshin
610132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
61153541Sshin	inp = sotoinpcb(so);
612132714Srwatson	if (inp == 0) {
613132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
61453541Sshin		panic("rip6_detach");
615132714Srwatson	}
61653541Sshin	/* xxx: RSVP */
61757535Sshin	if (so == ip6_mrouter)
61857535Sshin		ip6_mrouter_done();
61953541Sshin	if (inp->in6p_icmp6filt) {
62053541Sshin		FREE(inp->in6p_icmp6filt, M_PCB);
62153541Sshin		inp->in6p_icmp6filt = NULL;
62253541Sshin	}
623132714Srwatson	INP_LOCK(inp);
62453541Sshin	in6_pcbdetach(inp);
625132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
62653541Sshin	return 0;
62753541Sshin}
62853541Sshin
62953541Sshinstatic int
63053541Sshinrip6_abort(struct socket *so)
63153541Sshin{
63253541Sshin	soisdisconnected(so);
63353541Sshin	return rip6_detach(so);
63453541Sshin}
63553541Sshin
63653541Sshinstatic int
63753541Sshinrip6_disconnect(struct socket *so)
63853541Sshin{
63953541Sshin	struct inpcb *inp = sotoinpcb(so);
64053541Sshin
64197658Stanimura	if ((so->so_state & SS_ISCONNECTED) == 0)
64253541Sshin		return ENOTCONN;
64353541Sshin	inp->in6p_faddr = in6addr_any;
64453541Sshin	return rip6_abort(so);
64553541Sshin}
64653541Sshin
64753541Sshinstatic int
64883366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
64953541Sshin{
65053541Sshin	struct inpcb *inp = sotoinpcb(so);
65153541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
65253541Sshin	struct ifaddr *ia = NULL;
65353541Sshin
65453541Sshin	if (nam->sa_len != sizeof(*addr))
65553541Sshin		return EINVAL;
65653541Sshin	if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
65753541Sshin		return EADDRNOTAVAIL;
65862587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
65962587Sitojun	if (addr->sin6_scope_id == 0) {	/* not change if specified  */
66062587Sitojun		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
66162587Sitojun	}
66262587Sitojun#endif
66353541Sshin	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
66453541Sshin	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
66553541Sshin		return EADDRNOTAVAIL;
66653541Sshin	if (ia &&
66753541Sshin	    ((struct in6_ifaddr *)ia)->ia6_flags &
66853541Sshin	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
66953541Sshin	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
670120856Sume		return (EADDRNOTAVAIL);
67153541Sshin	}
672132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
673132714Srwatson	INP_LOCK(inp);
67453541Sshin	inp->in6p_laddr = addr->sin6_addr;
675132714Srwatson	INP_UNLOCK(inp);
676132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
67753541Sshin	return 0;
67853541Sshin}
67953541Sshin
68053541Sshinstatic int
68183366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
68253541Sshin{
68353541Sshin	struct inpcb *inp = sotoinpcb(so);
68453541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
68553541Sshin	struct in6_addr *in6a = NULL;
68653541Sshin	int error = 0;
68762587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
68862587Sitojun	struct sockaddr_in6 tmp;
68962587Sitojun#endif
69053541Sshin
69153541Sshin	if (nam->sa_len != sizeof(*addr))
69253541Sshin		return EINVAL;
69353541Sshin	if (TAILQ_EMPTY(&ifnet))
69453541Sshin		return EADDRNOTAVAIL;
69553541Sshin	if (addr->sin6_family != AF_INET6)
69653541Sshin		return EAFNOSUPPORT;
69762587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
69862587Sitojun	if (addr->sin6_scope_id == 0) {	/* not change if specified  */
69962587Sitojun		/* avoid overwrites */
70062587Sitojun		tmp = *addr;
70162587Sitojun		addr = &tmp;
70262587Sitojun		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
70362587Sitojun	}
70462587Sitojun#endif
705132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
706132714Srwatson	INP_LOCK(inp);
70753541Sshin	/* Source address selection. XXX: need pcblookup? */
70853541Sshin	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
709122927Sandre			     inp->in6p_moptions, NULL,
71053541Sshin			     &inp->in6p_laddr, &error);
711132714Srwatson	if (in6a == NULL) {
712132714Srwatson		INP_UNLOCK(inp);
713132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
71453541Sshin		return (error ? error : EADDRNOTAVAIL);
715132714Srwatson	}
71653541Sshin	inp->in6p_laddr = *in6a;
71753541Sshin	inp->in6p_faddr = addr->sin6_addr;
71853541Sshin	soisconnected(so);
719132714Srwatson	INP_UNLOCK(inp);
720132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
72153541Sshin	return 0;
72253541Sshin}
72353541Sshin
72453541Sshinstatic int
72553541Sshinrip6_shutdown(struct socket *so)
72653541Sshin{
727132714Srwatson	struct inpcb *inp;
728132714Srwatson
729132714Srwatson	INP_INFO_RLOCK(&ripcbinfo);
730132714Srwatson	inp = sotoinpcb(so);
731132714Srwatson	INP_LOCK(inp);
732132714Srwatson	INP_INFO_RUNLOCK(&ripcbinfo);
73353541Sshin	socantsendmore(so);
734132714Srwatson	INP_UNLOCK(inp);
73553541Sshin	return 0;
73653541Sshin}
73753541Sshin
73853541Sshinstatic int
73953541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
74083366Sjulian	 struct mbuf *control, struct thread *td)
74153541Sshin{
74253541Sshin	struct inpcb *inp = sotoinpcb(so);
74353541Sshin	struct sockaddr_in6 tmp;
74453541Sshin	struct sockaddr_in6 *dst;
745132714Srwatson	int ret;
74653541Sshin
747132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
74862587Sitojun	/* always copy sockaddr to avoid overwrites */
749132714Srwatson	/* Unlocked read. */
75053541Sshin	if (so->so_state & SS_ISCONNECTED) {
75153541Sshin		if (nam) {
752132714Srwatson			INP_INFO_WUNLOCK(&ripcbinfo);
75353541Sshin			m_freem(m);
75453541Sshin			return EISCONN;
75553541Sshin		}
75653541Sshin		/* XXX */
75753541Sshin		bzero(&tmp, sizeof(tmp));
75853541Sshin		tmp.sin6_family = AF_INET6;
75953541Sshin		tmp.sin6_len = sizeof(struct sockaddr_in6);
76053541Sshin		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
76153541Sshin		      sizeof(struct in6_addr));
76253541Sshin		dst = &tmp;
76353541Sshin	} else {
76453541Sshin		if (nam == NULL) {
765132714Srwatson			INP_INFO_WUNLOCK(&ripcbinfo);
76653541Sshin			m_freem(m);
76753541Sshin			return ENOTCONN;
76853541Sshin		}
76962587Sitojun		tmp = *(struct sockaddr_in6 *)nam;
77062587Sitojun		dst = &tmp;
77153541Sshin	}
77262587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
77362587Sitojun	if (dst->sin6_scope_id == 0) {	/* not change if specified  */
77462587Sitojun		dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
77562587Sitojun	}
77662587Sitojun#endif
777132714Srwatson	ret = rip6_output(m, so, dst, control);
778132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
779132714Srwatson	return (ret);
78053541Sshin}
78153541Sshin
78253541Sshinstruct pr_usrreqs rip6_usrreqs = {
783137386Sphk	.pru_abort =		rip6_abort,
784137386Sphk	.pru_attach =		rip6_attach,
785137386Sphk	.pru_bind =		rip6_bind,
786137386Sphk	.pru_connect =		rip6_connect,
787137386Sphk	.pru_control =		in6_control,
788137386Sphk	.pru_detach =		rip6_detach,
789137386Sphk	.pru_disconnect =	rip6_disconnect,
790137386Sphk	.pru_peeraddr =		in6_setpeeraddr,
791137386Sphk	.pru_send =		rip6_send,
792137386Sphk	.pru_shutdown =		rip6_shutdown,
793137386Sphk	.pru_sockaddr =		in6_setsockaddr,
79453541Sshin};
795