raw_ip6.c revision 157370
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 157370 2006-04-01 15:42:02Z rwatson $
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>
78148385Sume#include <sys/syslog.h>
7953541Sshin
8053541Sshin#include <net/if.h>
8195759Stanimura#include <net/if_types.h>
8253541Sshin#include <net/route.h>
8353541Sshin
8453541Sshin#include <netinet/in.h>
8553541Sshin#include <netinet/in_var.h>
8653541Sshin#include <netinet/in_systm.h>
8795759Stanimura#include <netinet/icmp6.h>
8895759Stanimura#include <netinet/in_pcb.h>
8962587Sitojun#include <netinet/ip6.h>
9095759Stanimura#include <netinet6/ip6protosw.h>
9156723Sshin#include <netinet6/ip6_mroute.h>
9253541Sshin#include <netinet6/in6_pcb.h>
9395759Stanimura#include <netinet6/ip6_var.h>
9453541Sshin#include <netinet6/nd6.h>
9595759Stanimura#include <netinet6/raw_ip6.h>
9662587Sitojun#include <netinet6/scope6_var.h>
9753541Sshin
9853541Sshin#ifdef IPSEC
9953541Sshin#include <netinet6/ipsec.h>
10053541Sshin#include <netinet6/ipsec6.h>
10153541Sshin#endif /*IPSEC*/
10253541Sshin
103105199Ssam#ifdef FAST_IPSEC
104105199Ssam#include <netipsec/ipsec.h>
105105199Ssam#include <netipsec/ipsec6.h>
106105199Ssam#endif /* FAST_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
11753541Sshinextern struct	inpcbhead ripcb;
11853541Sshinextern struct	inpcbinfo ripcbinfo;
11953541Sshinextern u_long	rip_sendspace;
12053541Sshinextern u_long	rip_recvspace;
12153541Sshin
12278064Sumestruct rip6stat rip6stat;
12378064Sume
12453541Sshin/*
12553541Sshin * Setup generic address and protocol structures
12653541Sshin * for raw_input routine, then pass them along with
12753541Sshin * mbuf chain.
12853541Sshin */
12953541Sshinint
13053541Sshinrip6_input(mp, offp, proto)
13153541Sshin	struct	mbuf **mp;
13253541Sshin	int	*offp, proto;
13353541Sshin{
13453541Sshin	struct mbuf *m = *mp;
13553541Sshin	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
13653541Sshin	register struct inpcb *in6p;
13753541Sshin	struct inpcb *last = 0;
13878064Sume	struct mbuf *opts = NULL;
139121901Sume	struct sockaddr_in6 fromsa;
14053541Sshin
14178064Sume	rip6stat.rip6s_ipackets++;
14278064Sume
14383934Sbrooks	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
14478064Sume		/* XXX send icmp6 host/port unreach? */
14578064Sume		m_freem(m);
14678064Sume		return IPPROTO_DONE;
14753541Sshin	}
14878064Sume
149121901Sume	init_sin6(&fromsa, m); /* general init */
15053541Sshin
151132714Srwatson	INP_INFO_RLOCK(&ripcbinfo);
15253541Sshin	LIST_FOREACH(in6p, &ripcb, inp_list) {
153132714Srwatson		INP_LOCK(in6p);
154132714Srwatson		if ((in6p->in6p_vflag & INP_IPV6) == 0) {
155132714Srwatsondocontinue:
156132714Srwatson			INP_UNLOCK(in6p);
15753541Sshin			continue;
158132714Srwatson		}
15953541Sshin		if (in6p->in6p_ip6_nxt &&
16053541Sshin		    in6p->in6p_ip6_nxt != proto)
161132714Srwatson			goto docontinue;
16253541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
16353541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
164132714Srwatson			goto docontinue;
16553541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
16653541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
167132714Srwatson			goto docontinue;
16878064Sume		if (in6p->in6p_cksum != -1) {
16978064Sume			rip6stat.rip6s_isum++;
170151459Ssuz			if (in6_cksum(m, proto, *offp,
17178064Sume			    m->m_pkthdr.len - *offp)) {
17278064Sume				rip6stat.rip6s_badsum++;
173132714Srwatson				goto docontinue;
17478064Sume			}
17553541Sshin		}
17653541Sshin		if (last) {
17753541Sshin			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
17878064Sume
179125941Sume#if defined(IPSEC) || defined(FAST_IPSEC)
18078064Sume			/*
18178064Sume			 * Check AH/ESP integrity.
18278064Sume			 */
183125396Sume			if (n && ipsec6_in_reject(n, last)) {
18478064Sume				m_freem(n);
185125941Sume#ifdef IPSEC
18678064Sume				ipsec6stat.in_polvio++;
18778064Sume#endif /*IPSEC*/
188105199Ssam				/* do not inject data into pcb */
189105199Ssam			} else
190125941Sume#endif /*IPSEC || FAST_IPSEC*/
19153541Sshin			if (n) {
19297658Stanimura				if (last->in6p_flags & IN6P_CONTROLOPTS ||
19397658Stanimura				    last->in6p_socket->so_options & SO_TIMESTAMP)
194121674Sume					ip6_savecontrol(last, n, &opts);
19553541Sshin				/* strip intermediate headers */
19653541Sshin				m_adj(n, *offp);
19753541Sshin				if (sbappendaddr(&last->in6p_socket->so_rcv,
198121901Sume						(struct sockaddr *)&fromsa,
19953541Sshin						 n, opts) == 0) {
20053541Sshin					m_freem(n);
20153541Sshin					if (opts)
20253541Sshin						m_freem(opts);
20378064Sume					rip6stat.rip6s_fullsock++;
20497658Stanimura				} else
20553541Sshin					sorwakeup(last->in6p_socket);
20653541Sshin				opts = NULL;
20753541Sshin			}
208132714Srwatson			INP_UNLOCK(last);
20953541Sshin		}
21053541Sshin		last = in6p;
21153541Sshin	}
212125941Sume#if defined(IPSEC) || defined(FAST_IPSEC)
21378064Sume	/*
21478064Sume	 * Check AH/ESP integrity.
21578064Sume	 */
216125396Sume	if (last && ipsec6_in_reject(m, last)) {
21778064Sume		m_freem(m);
218125941Sume#ifdef IPSEC
21978064Sume		ipsec6stat.in_polvio++;
22078064Sume#endif /*IPSEC*/
221105199Ssam		ip6stat.ip6s_delivered--;
222105199Ssam		/* do not inject data into pcb */
223149224Ssuz		INP_UNLOCK(last);
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;
327148247Sume	struct ip6_pktopts opt, *optp;
32853541Sshin	struct ifnet *oifp = NULL;
32953541Sshin	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
33053541Sshin	int priv = 0;
331148385Sume	int scope_ambiguous = 0;
332121472Sume	struct in6_addr *in6a;
33353541Sshin	va_list ap;
33453541Sshin
33553541Sshin	va_start(ap, m);
33653541Sshin	so = va_arg(ap, struct socket *);
33753541Sshin	dstsock = va_arg(ap, struct sockaddr_in6 *);
33853541Sshin	control = va_arg(ap, struct mbuf *);
33953541Sshin	va_end(ap);
34053541Sshin
34153541Sshin	in6p = sotoin6pcb(so);
342132714Srwatson	INP_LOCK(in6p);
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,
350148250Sume		    in6p->in6p_outputopts, priv, so->so_proto->pr_protocol))
351121472Sume		    != 0) {
35253541Sshin			goto bad;
353121472Sume		}
354148247Sume		optp = &opt;
355148247Sume	} else
356148247Sume		optp = in6p->in6p_outputopts;
35753541Sshin
35853541Sshin	/*
359148385Sume	 * Check and convert scope zone ID into internal form.
360148385Sume	 * XXX: we may still need to determine the zone later.
361148385Sume	 */
362148385Sume	if (!(so->so_state & SS_ISCONNECTED)) {
363148385Sume		if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
364148385Sume			scope_ambiguous = 1;
365148385Sume		if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
366148385Sume			goto bad;
367148385Sume	}
368148385Sume
369148385Sume	/*
37053541Sshin	 * For an ICMPv6 packet, we should know its type and code
37153541Sshin	 * to update statistics.
37253541Sshin	 */
37353541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
37453541Sshin		struct icmp6_hdr *icmp6;
37553541Sshin		if (m->m_len < sizeof(struct icmp6_hdr) &&
37653541Sshin		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
37753541Sshin			error = ENOBUFS;
37853541Sshin			goto bad;
37953541Sshin		}
38053541Sshin		icmp6 = mtod(m, struct icmp6_hdr *);
38153541Sshin		type = icmp6->icmp6_type;
38253541Sshin		code = icmp6->icmp6_code;
38353541Sshin	}
38453541Sshin
385133592Srwatson	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
386133592Srwatson	if (m == NULL) {
387133592Srwatson		error = ENOBUFS;
388133592Srwatson		goto bad;
389133592Srwatson	}
39053541Sshin	ip6 = mtod(m, struct ip6_hdr *);
39153541Sshin
39253541Sshin	/*
39353541Sshin	 * Source address selection.
39453541Sshin	 */
395148247Sume	if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, NULL,
396148385Sume	    &in6p->in6p_laddr, &oifp, &error)) == NULL) {
397121472Sume		if (error == 0)
398121472Sume			error = EADDRNOTAVAIL;
399121472Sume		goto bad;
40053541Sshin	}
401121472Sume	ip6->ip6_src = *in6a;
402148385Sume
403148385Sume	if (oifp && scope_ambiguous) {
404148385Sume		/*
405148385Sume		 * Application should provide a proper zone ID or the use of
406148385Sume		 * default zone IDs should be enabled.  Unfortunately, some
407148385Sume		 * applications do not behave as it should, so we need a
408148385Sume		 * workaround.  Even if an appropriate ID is not determined
409148385Sume		 * (when it's required), if we can determine the outgoing
410148385Sume		 * interface. determine the zone ID based on the interface.
411148385Sume		 */
412148385Sume		error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
413148385Sume		if (error != 0)
414148385Sume			goto bad;
415148385Sume	}
416148385Sume	ip6->ip6_dst = dstsock->sin6_addr;
417148385Sume
418148385Sume	/* fill in the rest of the IPv6 header fields */
41955009Sshin	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
42055009Sshin		(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
42155009Sshin	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
42255009Sshin		(IPV6_VERSION & IPV6_VERSION_MASK);
42353541Sshin	/* ip6_plen will be filled in ip6_output, so not fill it here. */
42453541Sshin	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
42553541Sshin	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
42653541Sshin
42753541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
42853541Sshin	    in6p->in6p_cksum != -1) {
42953541Sshin		struct mbuf *n;
43053541Sshin		int off;
43153541Sshin		u_int16_t *p;
43253541Sshin
43353541Sshin		/* compute checksum */
43453541Sshin		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
43553541Sshin			off = offsetof(struct icmp6_hdr, icmp6_cksum);
43653541Sshin		else
43753541Sshin			off = in6p->in6p_cksum;
43853541Sshin		if (plen < off + 1) {
43953541Sshin			error = EINVAL;
44053541Sshin			goto bad;
44153541Sshin		}
44253541Sshin		off += sizeof(struct ip6_hdr);
44353541Sshin
44453541Sshin		n = m;
44553541Sshin		while (n && n->m_len <= off) {
44653541Sshin			off -= n->m_len;
44753541Sshin			n = n->m_next;
44853541Sshin		}
44953541Sshin		if (!n)
45053541Sshin			goto bad;
45153541Sshin		p = (u_int16_t *)(mtod(n, caddr_t) + off);
45253541Sshin		*p = 0;
45353541Sshin		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
45453541Sshin	}
45553541Sshin
456148247Sume	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
45753541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
45853541Sshin		if (oifp)
45953541Sshin			icmp6_ifoutstat_inc(oifp, type, code);
46053541Sshin		icmp6stat.icp6s_outhist[type]++;
46178064Sume	} else
46278064Sume		rip6stat.rip6s_opackets++;
46353541Sshin
46453541Sshin	goto freectl;
46553541Sshin
46653541Sshin bad:
46753541Sshin	if (m)
46853541Sshin		m_freem(m);
46953541Sshin
47053541Sshin freectl:
47178064Sume	if (control) {
472148247Sume		ip6_clearpktopts(&opt, -1);
47353541Sshin		m_freem(control);
47478064Sume	}
475132714Srwatson	INP_UNLOCK(in6p);
476120856Sume	return (error);
47753541Sshin}
47853541Sshin
47953541Sshin/*
48053541Sshin * Raw IPv6 socket option processing.
48153541Sshin */
48253541Sshinint
48353541Sshinrip6_ctloutput(so, sopt)
48453541Sshin	struct socket *so;
48553541Sshin	struct sockopt *sopt;
48653541Sshin{
48753541Sshin	int error;
48853541Sshin
48953541Sshin	if (sopt->sopt_level == IPPROTO_ICMPV6)
49053541Sshin		/*
49153541Sshin		 * XXX: is it better to call icmp6_ctloutput() directly
49253541Sshin		 * from protosw?
49353541Sshin		 */
494120856Sume		return (icmp6_ctloutput(so, sopt));
49553541Sshin	else if (sopt->sopt_level != IPPROTO_IPV6)
49653541Sshin		return (EINVAL);
49753541Sshin
49853541Sshin	error = 0;
49953541Sshin
50053541Sshin	switch (sopt->sopt_dir) {
50153541Sshin	case SOPT_GET:
50253541Sshin		switch (sopt->sopt_name) {
50356723Sshin		case MRT6_INIT:
50456723Sshin		case MRT6_DONE:
50556723Sshin		case MRT6_ADD_MIF:
50656723Sshin		case MRT6_DEL_MIF:
50756723Sshin		case MRT6_ADD_MFC:
50856723Sshin		case MRT6_DEL_MFC:
50956723Sshin		case MRT6_PIM:
51056723Sshin			error = ip6_mrouter_get(so, sopt);
51156723Sshin			break;
512121578Sume		case IPV6_CHECKSUM:
513121578Sume			error = ip6_raw_ctloutput(so, sopt);
514121578Sume			break;
51553541Sshin		default:
51653541Sshin			error = ip6_ctloutput(so, sopt);
51753541Sshin			break;
51853541Sshin		}
51953541Sshin		break;
52053541Sshin
52153541Sshin	case SOPT_SET:
52253541Sshin		switch (sopt->sopt_name) {
52356723Sshin		case MRT6_INIT:
52456723Sshin		case MRT6_DONE:
52556723Sshin		case MRT6_ADD_MIF:
52656723Sshin		case MRT6_DEL_MIF:
52756723Sshin		case MRT6_ADD_MFC:
52856723Sshin		case MRT6_DEL_MFC:
52956723Sshin		case MRT6_PIM:
53056723Sshin			error = ip6_mrouter_set(so, sopt);
53156723Sshin			break;
532121578Sume		case IPV6_CHECKSUM:
533121578Sume			error = ip6_raw_ctloutput(so, sopt);
534121578Sume			break;
53553541Sshin		default:
53653541Sshin			error = ip6_ctloutput(so, sopt);
53753541Sshin			break;
53853541Sshin		}
53953541Sshin		break;
54053541Sshin	}
54153541Sshin
54253541Sshin	return (error);
54353541Sshin}
54453541Sshin
54553541Sshinstatic int
54683366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td)
54753541Sshin{
54853541Sshin	struct inpcb *inp;
549144261Ssam	struct icmp6_filter *filter;
55053541Sshin	int error, s;
55153541Sshin
552132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
55353541Sshin	inp = sotoinpcb(so);
554132714Srwatson	if (inp) {
555132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
55653541Sshin		panic("rip6_attach");
557132714Srwatson	}
558132714Srwatson	if (td && (error = suser(td)) != 0) {
559132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
56053541Sshin		return error;
561132714Srwatson	}
56255009Sshin	error = soreserve(so, rip_sendspace, rip_recvspace);
563132714Srwatson	if (error) {
564132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
56555009Sshin		return error;
566132714Srwatson	}
567144261Ssam	MALLOC(filter, struct icmp6_filter *,
568144261Ssam	       sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
569151362Ssuz	if (filter == NULL) {
570151362Ssuz		INP_INFO_WUNLOCK(&ripcbinfo);
571144261Ssam		return ENOMEM;
572151362Ssuz	}
57353541Sshin	s = splnet();
574127504Spjd	error = in_pcballoc(so, &ripcbinfo, "raw6inp");
57553541Sshin	splx(s);
576132714Srwatson	if (error) {
577132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
578144261Ssam		FREE(filter, M_PCB);
57953541Sshin		return error;
580132714Srwatson	}
58153541Sshin	inp = (struct inpcb *)so->so_pcb;
582132714Srwatson	INP_LOCK(inp);
583132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
58453541Sshin	inp->inp_vflag |= INP_IPV6;
58553541Sshin	inp->in6p_ip6_nxt = (long)proto;
58653541Sshin	inp->in6p_hops = -1;	/* use kernel default */
58753541Sshin	inp->in6p_cksum = -1;
588144261Ssam	inp->in6p_icmp6filt = filter;
58953541Sshin	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
590132714Srwatson	INP_UNLOCK(inp);
59153541Sshin	return 0;
59253541Sshin}
59353541Sshin
594157370Srwatsonstatic void
59553541Sshinrip6_detach(struct socket *so)
59653541Sshin{
59753541Sshin	struct inpcb *inp;
59853541Sshin
599132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
60053541Sshin	inp = sotoinpcb(so);
601132714Srwatson	if (inp == 0) {
602132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
60353541Sshin		panic("rip6_detach");
604132714Srwatson	}
60553541Sshin	/* xxx: RSVP */
60657535Sshin	if (so == ip6_mrouter)
60757535Sshin		ip6_mrouter_done();
60853541Sshin	if (inp->in6p_icmp6filt) {
60953541Sshin		FREE(inp->in6p_icmp6filt, M_PCB);
61053541Sshin		inp->in6p_icmp6filt = NULL;
61153541Sshin	}
612132714Srwatson	INP_LOCK(inp);
61353541Sshin	in6_pcbdetach(inp);
614132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
61553541Sshin}
61653541Sshin
617157366Srwatsonstatic void
61853541Sshinrip6_abort(struct socket *so)
61953541Sshin{
62053541Sshin	soisdisconnected(so);
621157366Srwatson	rip6_detach(so);
62253541Sshin}
62353541Sshin
62453541Sshinstatic int
62553541Sshinrip6_disconnect(struct socket *so)
62653541Sshin{
62753541Sshin	struct inpcb *inp = sotoinpcb(so);
62853541Sshin
62997658Stanimura	if ((so->so_state & SS_ISCONNECTED) == 0)
63053541Sshin		return ENOTCONN;
63153541Sshin	inp->in6p_faddr = in6addr_any;
632157366Srwatson	rip6_abort(so);
633157366Srwatson	return 0;
63453541Sshin}
63553541Sshin
63653541Sshinstatic int
63783366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
63853541Sshin{
63953541Sshin	struct inpcb *inp = sotoinpcb(so);
64053541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
64153541Sshin	struct ifaddr *ia = NULL;
642148385Sume	int error = 0;
64353541Sshin
64453541Sshin	if (nam->sa_len != sizeof(*addr))
64553541Sshin		return EINVAL;
64653541Sshin	if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
64753541Sshin		return EADDRNOTAVAIL;
648148385Sume	if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
649148385Sume		return(error);
650148385Sume
65153541Sshin	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
65253541Sshin	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
65353541Sshin		return EADDRNOTAVAIL;
65453541Sshin	if (ia &&
65553541Sshin	    ((struct in6_ifaddr *)ia)->ia6_flags &
65653541Sshin	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
65753541Sshin	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
658120856Sume		return (EADDRNOTAVAIL);
65953541Sshin	}
660132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
661132714Srwatson	INP_LOCK(inp);
66253541Sshin	inp->in6p_laddr = addr->sin6_addr;
663132714Srwatson	INP_UNLOCK(inp);
664132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
66553541Sshin	return 0;
66653541Sshin}
66753541Sshin
66853541Sshinstatic int
66983366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
67053541Sshin{
67153541Sshin	struct inpcb *inp = sotoinpcb(so);
67253541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
67353541Sshin	struct in6_addr *in6a = NULL;
674148385Sume	struct ifnet *ifp = NULL;
675148385Sume	int error = 0, scope_ambiguous = 0;
67653541Sshin
67753541Sshin	if (nam->sa_len != sizeof(*addr))
67853541Sshin		return EINVAL;
67953541Sshin	if (TAILQ_EMPTY(&ifnet))
68053541Sshin		return EADDRNOTAVAIL;
68153541Sshin	if (addr->sin6_family != AF_INET6)
68253541Sshin		return EAFNOSUPPORT;
683148385Sume
684148385Sume	/*
685148385Sume	 * Application should provide a proper zone ID or the use of
686148385Sume	 * default zone IDs should be enabled.  Unfortunately, some
687148385Sume	 * applications do not behave as it should, so we need a
688148385Sume	 * workaround.  Even if an appropriate ID is not determined,
689148385Sume	 * we'll see if we can determine the outgoing interface.  If we
690148385Sume	 * can, determine the zone ID based on the interface below.
691148385Sume	 */
692148385Sume	if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
693148385Sume		scope_ambiguous = 1;
694148385Sume	if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
695148385Sume		return(error);
696148385Sume
697132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
698132714Srwatson	INP_LOCK(inp);
69953541Sshin	/* Source address selection. XXX: need pcblookup? */
70053541Sshin	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
701122927Sandre			     inp->in6p_moptions, NULL,
702148385Sume			     &inp->in6p_laddr, &ifp, &error);
703132714Srwatson	if (in6a == NULL) {
704132714Srwatson		INP_UNLOCK(inp);
705132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
70653541Sshin		return (error ? error : EADDRNOTAVAIL);
707132714Srwatson	}
708148385Sume
709148385Sume	/* XXX: see above */
710148385Sume	if (ifp && scope_ambiguous &&
711148385Sume	    (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
712148385Sume		INP_UNLOCK(inp);
713148385Sume		INP_INFO_WUNLOCK(&ripcbinfo);
714148385Sume		return(error);
715148385Sume	}
716148385Sume	inp->in6p_faddr = addr->sin6_addr;
71753541Sshin	inp->in6p_laddr = *in6a;
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		}
769148385Sume		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
770148385Sume			INP_INFO_WUNLOCK(&ripcbinfo);
771148385Sume			m_freem(m);
772148385Sume			return(EINVAL);
773148385Sume		}
77462587Sitojun		tmp = *(struct sockaddr_in6 *)nam;
77562587Sitojun		dst = &tmp;
776148385Sume
777148385Sume		if (dst->sin6_family == AF_UNSPEC) {
778148385Sume			/*
779148385Sume			 * XXX: we allow this case for backward
780148385Sume			 * compatibility to buggy applications that
781148385Sume			 * rely on old (and wrong) kernel behavior.
782148385Sume			 */
783148385Sume			log(LOG_INFO, "rip6 SEND: address family is "
784148385Sume			    "unspec. Assume AF_INET6\n");
785148385Sume			dst->sin6_family = AF_INET6;
786148385Sume		} else if (dst->sin6_family != AF_INET6) {
787148385Sume			INP_INFO_WUNLOCK(&ripcbinfo);
788148385Sume			m_freem(m);
789148385Sume			return(EAFNOSUPPORT);
790148385Sume		}
79153541Sshin	}
792132714Srwatson	ret = rip6_output(m, so, dst, control);
793132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
794132714Srwatson	return (ret);
79553541Sshin}
79653541Sshin
79753541Sshinstruct pr_usrreqs rip6_usrreqs = {
798137386Sphk	.pru_abort =		rip6_abort,
799137386Sphk	.pru_attach =		rip6_attach,
800137386Sphk	.pru_bind =		rip6_bind,
801137386Sphk	.pru_connect =		rip6_connect,
802137386Sphk	.pru_control =		in6_control,
803137386Sphk	.pru_detach =		rip6_detach,
804137386Sphk	.pru_disconnect =	rip6_disconnect,
805137386Sphk	.pru_peeraddr =		in6_setpeeraddr,
806137386Sphk	.pru_send =		rip6_send,
807137386Sphk	.pru_shutdown =		rip6_shutdown,
808137386Sphk	.pru_sockaddr =		in6_setsockaddr,
80953541Sshin};
810