raw_ip6.c revision 132714
153541Sshin/*
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 132714 2004-07-27 23:44:03Z rwatson $
3053541Sshin */
3153541Sshin
3253541Sshin/*
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	}
255132714Srwatson	INP_INFO_RLOCK(&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
30191346Salfred	(void) in6_pcbnotify(&ripcb, sa, 0, (const struct sockaddr *)sa6_src,
302125776Sume			     0, cmd, cmdarg, notify);
30362587Sitojun}
30462587Sitojun
30553541Sshin/*
30653541Sshin * Generate IPv6 header and pass packet to ip6_output.
30753541Sshin * Tack on options user may have setup with control call.
30853541Sshin */
30953541Sshinint
31053541Sshin#if __STDC__
31153541Sshinrip6_output(struct mbuf *m, ...)
31253541Sshin#else
31353541Sshinrip6_output(m, va_alist)
31453541Sshin	struct mbuf *m;
31553541Sshin	va_dcl
31653541Sshin#endif
31753541Sshin{
318120941Sume	struct mbuf *control;
31953541Sshin	struct socket *so;
32053541Sshin	struct sockaddr_in6 *dstsock;
32153541Sshin	struct in6_addr *dst;
32253541Sshin	struct ip6_hdr *ip6;
32353541Sshin	struct inpcb *in6p;
32453541Sshin	u_int	plen = m->m_pkthdr.len;
32553541Sshin	int error = 0;
326121472Sume	struct ip6_pktopts opt, *stickyopt = NULL;
32753541Sshin	struct ifnet *oifp = NULL;
32853541Sshin	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
32953541Sshin	int priv = 0;
330121472Sume	struct in6_addr *in6a;
33153541Sshin	va_list ap;
33253541Sshin
33353541Sshin	va_start(ap, m);
33453541Sshin	so = va_arg(ap, struct socket *);
33553541Sshin	dstsock = va_arg(ap, struct sockaddr_in6 *);
33653541Sshin	control = va_arg(ap, struct mbuf *);
33753541Sshin	va_end(ap);
33853541Sshin
33953541Sshin	in6p = sotoin6pcb(so);
340132714Srwatson	/*
341132714Srwatson	 * XXXRW: In IPv6, we don't start referencing the contents of the
342132714Srwatson	 * inpcb until after all M_TRYWAIT allocations have finished.  We may
343132714Srwatson	 * want to reorder this function to provide similar guarantees here,
344132714Srwatson	 * so as to avoid holding a mutex over M_TRYWAIT.
345132714Srwatson	 */
346132714Srwatson	INP_LOCK(in6p);
347121472Sume	stickyopt = in6p->in6p_outputopts;
34853541Sshin
34953541Sshin	priv = 0;
35053541Sshin	if (so->so_cred->cr_uid == 0)
35153541Sshin		priv = 1;
35253541Sshin	dst = &dstsock->sin6_addr;
35353541Sshin	if (control) {
354121472Sume		if ((error = ip6_setpktoptions(control, &opt,
355121472Sume					       stickyopt, priv, 0,
356121472Sume					       so->so_proto->pr_protocol))
357121472Sume		    != 0) {
35853541Sshin			goto bad;
359121472Sume		}
360121472Sume		in6p->in6p_outputopts = &opt;
361121472Sume	}
36253541Sshin
36353541Sshin	/*
36453541Sshin	 * For an ICMPv6 packet, we should know its type and code
36553541Sshin	 * to update statistics.
36653541Sshin	 */
36753541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
36853541Sshin		struct icmp6_hdr *icmp6;
36953541Sshin		if (m->m_len < sizeof(struct icmp6_hdr) &&
37053541Sshin		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
37153541Sshin			error = ENOBUFS;
37253541Sshin			goto bad;
37353541Sshin		}
37453541Sshin		icmp6 = mtod(m, struct icmp6_hdr *);
37553541Sshin		type = icmp6->icmp6_type;
37653541Sshin		code = icmp6->icmp6_code;
37753541Sshin	}
37853541Sshin
379111119Simp	M_PREPEND(m, sizeof(*ip6), M_TRYWAIT);
38053541Sshin	ip6 = mtod(m, struct ip6_hdr *);
38153541Sshin
38253541Sshin	/*
38353541Sshin	 * Next header might not be ICMP6 but use its pseudo header anyway.
38453541Sshin	 */
38553541Sshin	ip6->ip6_dst = *dst;
38653541Sshin
38753541Sshin	/*
38853541Sshin	 * If the scope of the destination is link-local, embed the interface
38953541Sshin	 * index in the address.
39053541Sshin	 *
39153541Sshin	 * XXX advanced-api value overrides sin6_scope_id
39253541Sshin	 */
39353541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
39453541Sshin		struct in6_pktinfo *pi;
39553541Sshin
39653541Sshin		/*
39753541Sshin		 * XXX Boundary check is assumed to be already done in
39853541Sshin		 * ip6_setpktoptions().
39953541Sshin		 */
400121472Sume		if (in6p->in6p_outputopts &&
401121472Sume		    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
402121472Sume		    pi->ipi6_ifindex) {
40353541Sshin			ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
40483130Sjlemon			oifp = ifnet_byindex(pi->ipi6_ifindex);
40553541Sshin		} else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
40653541Sshin			 in6p->in6p_moptions &&
40753541Sshin			 in6p->in6p_moptions->im6o_multicast_ifp) {
40853541Sshin			oifp = in6p->in6p_moptions->im6o_multicast_ifp;
40953541Sshin			ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
41053541Sshin		} else if (dstsock->sin6_scope_id) {
41153541Sshin			/* boundary check */
412120941Sume			if (dstsock->sin6_scope_id < 0 ||
413120941Sume			    if_index < dstsock->sin6_scope_id) {
41453541Sshin				error = ENXIO;  /* XXX EINVAL? */
41553541Sshin				goto bad;
41653541Sshin			}
417120941Sume			ip6->ip6_dst.s6_addr16[1] =
418120941Sume			    htons(dstsock->sin6_scope_id & 0xffff); /* XXX */
41953541Sshin		}
42053541Sshin	}
42153541Sshin
42253541Sshin	/*
42353541Sshin	 * Source address selection.
42453541Sshin	 */
425121472Sume	if ((in6a = in6_selectsrc(dstsock, in6p->in6p_outputopts,
426122927Sandre	    in6p->in6p_moptions, NULL, &in6p->in6p_laddr, &error)) == 0) {
427121472Sume		if (error == 0)
428121472Sume			error = EADDRNOTAVAIL;
429121472Sume		goto bad;
43053541Sshin	}
431121472Sume	ip6->ip6_src = *in6a;
43255009Sshin	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
43355009Sshin		(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
43455009Sshin	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
43555009Sshin		(IPV6_VERSION & IPV6_VERSION_MASK);
43653541Sshin	/* ip6_plen will be filled in ip6_output, so not fill it here. */
43753541Sshin	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
43853541Sshin	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
43953541Sshin
44053541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
44153541Sshin	    in6p->in6p_cksum != -1) {
44253541Sshin		struct mbuf *n;
44353541Sshin		int off;
44453541Sshin		u_int16_t *p;
44553541Sshin
44653541Sshin		/* compute checksum */
44753541Sshin		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
44853541Sshin			off = offsetof(struct icmp6_hdr, icmp6_cksum);
44953541Sshin		else
45053541Sshin			off = in6p->in6p_cksum;
45153541Sshin		if (plen < off + 1) {
45253541Sshin			error = EINVAL;
45353541Sshin			goto bad;
45453541Sshin		}
45553541Sshin		off += sizeof(struct ip6_hdr);
45653541Sshin
45753541Sshin		n = m;
45853541Sshin		while (n && n->m_len <= off) {
45953541Sshin			off -= n->m_len;
46053541Sshin			n = n->m_next;
46153541Sshin		}
46253541Sshin		if (!n)
46353541Sshin			goto bad;
46453541Sshin		p = (u_int16_t *)(mtod(n, caddr_t) + off);
46553541Sshin		*p = 0;
46653541Sshin		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
46753541Sshin	}
46853541Sshin
469122927Sandre	error = ip6_output(m, in6p->in6p_outputopts, NULL, 0,
470105194Ssam			   in6p->in6p_moptions, &oifp, in6p);
47153541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
47253541Sshin		if (oifp)
47353541Sshin			icmp6_ifoutstat_inc(oifp, type, code);
47453541Sshin		icmp6stat.icp6s_outhist[type]++;
47578064Sume	} else
47678064Sume		rip6stat.rip6s_opackets++;
47753541Sshin
47853541Sshin	goto freectl;
47953541Sshin
48053541Sshin bad:
48153541Sshin	if (m)
48253541Sshin		m_freem(m);
48353541Sshin
48453541Sshin freectl:
48578064Sume	if (control) {
486121472Sume		ip6_clearpktopts(in6p->in6p_outputopts, -1);
487121472Sume		in6p->in6p_outputopts = stickyopt;
48853541Sshin		m_freem(control);
48978064Sume	}
490132714Srwatson	INP_UNLOCK(in6p);
491120856Sume	return (error);
49253541Sshin}
49353541Sshin
49453541Sshin/*
49553541Sshin * Raw IPv6 socket option processing.
49653541Sshin */
49753541Sshinint
49853541Sshinrip6_ctloutput(so, sopt)
49953541Sshin	struct socket *so;
50053541Sshin	struct sockopt *sopt;
50153541Sshin{
50253541Sshin	int error;
50353541Sshin
50453541Sshin	if (sopt->sopt_level == IPPROTO_ICMPV6)
50553541Sshin		/*
50653541Sshin		 * XXX: is it better to call icmp6_ctloutput() directly
50753541Sshin		 * from protosw?
50853541Sshin		 */
509120856Sume		return (icmp6_ctloutput(so, sopt));
51053541Sshin	else if (sopt->sopt_level != IPPROTO_IPV6)
51153541Sshin		return (EINVAL);
51253541Sshin
51353541Sshin	error = 0;
51453541Sshin
51553541Sshin	switch (sopt->sopt_dir) {
51653541Sshin	case SOPT_GET:
51753541Sshin		switch (sopt->sopt_name) {
51856723Sshin		case MRT6_INIT:
51956723Sshin		case MRT6_DONE:
52056723Sshin		case MRT6_ADD_MIF:
52156723Sshin		case MRT6_DEL_MIF:
52256723Sshin		case MRT6_ADD_MFC:
52356723Sshin		case MRT6_DEL_MFC:
52456723Sshin		case MRT6_PIM:
52556723Sshin			error = ip6_mrouter_get(so, sopt);
52656723Sshin			break;
527121578Sume		case IPV6_CHECKSUM:
528121578Sume			error = ip6_raw_ctloutput(so, sopt);
529121578Sume			break;
53053541Sshin		default:
53153541Sshin			error = ip6_ctloutput(so, sopt);
53253541Sshin			break;
53353541Sshin		}
53453541Sshin		break;
53553541Sshin
53653541Sshin	case SOPT_SET:
53753541Sshin		switch (sopt->sopt_name) {
53856723Sshin		case MRT6_INIT:
53956723Sshin		case MRT6_DONE:
54056723Sshin		case MRT6_ADD_MIF:
54156723Sshin		case MRT6_DEL_MIF:
54256723Sshin		case MRT6_ADD_MFC:
54356723Sshin		case MRT6_DEL_MFC:
54456723Sshin		case MRT6_PIM:
54556723Sshin			error = ip6_mrouter_set(so, sopt);
54656723Sshin			break;
547121578Sume		case IPV6_CHECKSUM:
548121578Sume			error = ip6_raw_ctloutput(so, sopt);
549121578Sume			break;
55053541Sshin		default:
55153541Sshin			error = ip6_ctloutput(so, sopt);
55253541Sshin			break;
55353541Sshin		}
55453541Sshin		break;
55553541Sshin	}
55653541Sshin
55753541Sshin	return (error);
55853541Sshin}
55953541Sshin
56053541Sshinstatic int
56183366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td)
56253541Sshin{
56353541Sshin	struct inpcb *inp;
56453541Sshin	int error, s;
56553541Sshin
566132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
56753541Sshin	inp = sotoinpcb(so);
568132714Srwatson	if (inp) {
569132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
57053541Sshin		panic("rip6_attach");
571132714Srwatson	}
572132714Srwatson	if (td && (error = suser(td)) != 0) {
573132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
57453541Sshin		return error;
575132714Srwatson	}
57655009Sshin	error = soreserve(so, rip_sendspace, rip_recvspace);
577132714Srwatson	if (error) {
578132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
57955009Sshin		return error;
580132714Srwatson	}
58153541Sshin	s = splnet();
582127504Spjd	error = in_pcballoc(so, &ripcbinfo, "raw6inp");
58353541Sshin	splx(s);
584132714Srwatson	if (error) {
585132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
58653541Sshin		return error;
587132714Srwatson	}
58853541Sshin	inp = (struct inpcb *)so->so_pcb;
589132714Srwatson	INP_LOCK(inp);
590132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
59153541Sshin	inp->inp_vflag |= INP_IPV6;
59253541Sshin	inp->in6p_ip6_nxt = (long)proto;
59353541Sshin	inp->in6p_hops = -1;	/* use kernel default */
59453541Sshin	inp->in6p_cksum = -1;
59553541Sshin	MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
59653541Sshin	       sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
59753541Sshin	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
598132714Srwatson	INP_UNLOCK(inp);
59953541Sshin	return 0;
60053541Sshin}
60153541Sshin
60253541Sshinstatic int
60353541Sshinrip6_detach(struct socket *so)
60453541Sshin{
60553541Sshin	struct inpcb *inp;
60653541Sshin
607132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
60853541Sshin	inp = sotoinpcb(so);
609132714Srwatson	if (inp == 0) {
610132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
61153541Sshin		panic("rip6_detach");
612132714Srwatson	}
61353541Sshin	/* xxx: RSVP */
61457535Sshin	if (so == ip6_mrouter)
61557535Sshin		ip6_mrouter_done();
61653541Sshin	if (inp->in6p_icmp6filt) {
61753541Sshin		FREE(inp->in6p_icmp6filt, M_PCB);
61853541Sshin		inp->in6p_icmp6filt = NULL;
61953541Sshin	}
620132714Srwatson	INP_LOCK(inp);
62153541Sshin	in6_pcbdetach(inp);
622132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
62353541Sshin	return 0;
62453541Sshin}
62553541Sshin
62653541Sshinstatic int
62753541Sshinrip6_abort(struct socket *so)
62853541Sshin{
62953541Sshin	soisdisconnected(so);
63053541Sshin	return rip6_detach(so);
63153541Sshin}
63253541Sshin
63353541Sshinstatic int
63453541Sshinrip6_disconnect(struct socket *so)
63553541Sshin{
63653541Sshin	struct inpcb *inp = sotoinpcb(so);
63753541Sshin
63897658Stanimura	if ((so->so_state & SS_ISCONNECTED) == 0)
63953541Sshin		return ENOTCONN;
64053541Sshin	inp->in6p_faddr = in6addr_any;
64153541Sshin	return rip6_abort(so);
64253541Sshin}
64353541Sshin
64453541Sshinstatic int
64583366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
64653541Sshin{
64753541Sshin	struct inpcb *inp = sotoinpcb(so);
64853541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
64953541Sshin	struct ifaddr *ia = NULL;
65053541Sshin
65153541Sshin	if (nam->sa_len != sizeof(*addr))
65253541Sshin		return EINVAL;
65353541Sshin	if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
65453541Sshin		return EADDRNOTAVAIL;
65562587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
65662587Sitojun	if (addr->sin6_scope_id == 0) {	/* not change if specified  */
65762587Sitojun		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
65862587Sitojun	}
65962587Sitojun#endif
66053541Sshin	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
66153541Sshin	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
66253541Sshin		return EADDRNOTAVAIL;
66353541Sshin	if (ia &&
66453541Sshin	    ((struct in6_ifaddr *)ia)->ia6_flags &
66553541Sshin	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
66653541Sshin	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
667120856Sume		return (EADDRNOTAVAIL);
66853541Sshin	}
669132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
670132714Srwatson	INP_LOCK(inp);
67153541Sshin	inp->in6p_laddr = addr->sin6_addr;
672132714Srwatson	INP_UNLOCK(inp);
673132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
67453541Sshin	return 0;
67553541Sshin}
67653541Sshin
67753541Sshinstatic int
67883366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
67953541Sshin{
68053541Sshin	struct inpcb *inp = sotoinpcb(so);
68153541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
68253541Sshin	struct in6_addr *in6a = NULL;
68353541Sshin	int error = 0;
68462587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
68562587Sitojun	struct sockaddr_in6 tmp;
68662587Sitojun#endif
68753541Sshin
68853541Sshin	if (nam->sa_len != sizeof(*addr))
68953541Sshin		return EINVAL;
69053541Sshin	if (TAILQ_EMPTY(&ifnet))
69153541Sshin		return EADDRNOTAVAIL;
69253541Sshin	if (addr->sin6_family != AF_INET6)
69353541Sshin		return EAFNOSUPPORT;
69462587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
69562587Sitojun	if (addr->sin6_scope_id == 0) {	/* not change if specified  */
69662587Sitojun		/* avoid overwrites */
69762587Sitojun		tmp = *addr;
69862587Sitojun		addr = &tmp;
69962587Sitojun		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
70062587Sitojun	}
70162587Sitojun#endif
702132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
703132714Srwatson	INP_LOCK(inp);
70453541Sshin	/* Source address selection. XXX: need pcblookup? */
70553541Sshin	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
706122927Sandre			     inp->in6p_moptions, NULL,
70753541Sshin			     &inp->in6p_laddr, &error);
708132714Srwatson	if (in6a == NULL) {
709132714Srwatson		INP_UNLOCK(inp);
710132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
71153541Sshin		return (error ? error : EADDRNOTAVAIL);
712132714Srwatson	}
71353541Sshin	inp->in6p_laddr = *in6a;
71453541Sshin	inp->in6p_faddr = addr->sin6_addr;
71553541Sshin	soisconnected(so);
716132714Srwatson	INP_UNLOCK(inp);
717132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
71853541Sshin	return 0;
71953541Sshin}
72053541Sshin
72153541Sshinstatic int
72253541Sshinrip6_shutdown(struct socket *so)
72353541Sshin{
724132714Srwatson	struct inpcb *inp;
725132714Srwatson
726132714Srwatson	INP_INFO_RLOCK(&ripcbinfo);
727132714Srwatson	inp = sotoinpcb(so);
728132714Srwatson	INP_LOCK(inp);
729132714Srwatson	INP_INFO_RUNLOCK(&ripcbinfo);
73053541Sshin	socantsendmore(so);
731132714Srwatson	INP_UNLOCK(inp);
73253541Sshin	return 0;
73353541Sshin}
73453541Sshin
73553541Sshinstatic int
73653541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
73783366Sjulian	 struct mbuf *control, struct thread *td)
73853541Sshin{
73953541Sshin	struct inpcb *inp = sotoinpcb(so);
74053541Sshin	struct sockaddr_in6 tmp;
74153541Sshin	struct sockaddr_in6 *dst;
742132714Srwatson	int ret;
74353541Sshin
744132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
74562587Sitojun	/* always copy sockaddr to avoid overwrites */
746132714Srwatson	/* Unlocked read. */
74753541Sshin	if (so->so_state & SS_ISCONNECTED) {
74853541Sshin		if (nam) {
749132714Srwatson			INP_INFO_WUNLOCK(&ripcbinfo);
75053541Sshin			m_freem(m);
75153541Sshin			return EISCONN;
75253541Sshin		}
75353541Sshin		/* XXX */
75453541Sshin		bzero(&tmp, sizeof(tmp));
75553541Sshin		tmp.sin6_family = AF_INET6;
75653541Sshin		tmp.sin6_len = sizeof(struct sockaddr_in6);
75753541Sshin		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
75853541Sshin		      sizeof(struct in6_addr));
75953541Sshin		dst = &tmp;
76053541Sshin	} else {
76153541Sshin		if (nam == NULL) {
762132714Srwatson			INP_INFO_WUNLOCK(&ripcbinfo);
76353541Sshin			m_freem(m);
76453541Sshin			return ENOTCONN;
76553541Sshin		}
76662587Sitojun		tmp = *(struct sockaddr_in6 *)nam;
76762587Sitojun		dst = &tmp;
76853541Sshin	}
76962587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
77062587Sitojun	if (dst->sin6_scope_id == 0) {	/* not change if specified  */
77162587Sitojun		dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
77262587Sitojun	}
77362587Sitojun#endif
774132714Srwatson	ret = rip6_output(m, so, dst, control);
775132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
776132714Srwatson	return (ret);
77753541Sshin}
77853541Sshin
77953541Sshinstruct pr_usrreqs rip6_usrreqs = {
78053541Sshin	rip6_abort, pru_accept_notsupp, rip6_attach, rip6_bind, rip6_connect,
78153541Sshin	pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect,
78253541Sshin	pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp,
78353541Sshin	pru_rcvoob_notsupp, rip6_send, pru_sense_null, rip6_shutdown,
784122875Srwatson	in6_setsockaddr, sosend, soreceive, sopoll, pru_sosetlabel_null
78553541Sshin};
786