raw_ip6.c revision 170689
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 170689 2007-06-13 22:42:43Z 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>
71170689Srwatson#include <sys/priv.h>
7253541Sshin#include <sys/proc.h>
7395759Stanimura#include <sys/protosw.h>
7495759Stanimura#include <sys/signalvar.h>
7553541Sshin#include <sys/socket.h>
7653541Sshin#include <sys/socketvar.h>
7795759Stanimura#include <sys/sx.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/*
125166938Sbms * Hooks for multicast forwarding.
126166938Sbms */
127166948Sbmsstruct socket *ip6_mrouter = NULL;
128166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *);
129166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *);
130166938Sbmsint (*ip6_mrouter_done)(void);
131166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
132166938Sbmsint (*mrt6_ioctl)(int, caddr_t);
133166938Sbms
134166938Sbms/*
13553541Sshin * Setup generic address and protocol structures
13653541Sshin * for raw_input routine, then pass them along with
13753541Sshin * mbuf chain.
13853541Sshin */
13953541Sshinint
14053541Sshinrip6_input(mp, offp, proto)
14153541Sshin	struct	mbuf **mp;
14253541Sshin	int	*offp, proto;
14353541Sshin{
14453541Sshin	struct mbuf *m = *mp;
14553541Sshin	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
14653541Sshin	register struct inpcb *in6p;
14753541Sshin	struct inpcb *last = 0;
14878064Sume	struct mbuf *opts = NULL;
149121901Sume	struct sockaddr_in6 fromsa;
15053541Sshin
15178064Sume	rip6stat.rip6s_ipackets++;
15278064Sume
15383934Sbrooks	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
15478064Sume		/* XXX send icmp6 host/port unreach? */
15578064Sume		m_freem(m);
15678064Sume		return IPPROTO_DONE;
15753541Sshin	}
15878064Sume
159121901Sume	init_sin6(&fromsa, m); /* general init */
16053541Sshin
161132714Srwatson	INP_INFO_RLOCK(&ripcbinfo);
16253541Sshin	LIST_FOREACH(in6p, &ripcb, inp_list) {
163132714Srwatson		INP_LOCK(in6p);
164132714Srwatson		if ((in6p->in6p_vflag & INP_IPV6) == 0) {
165132714Srwatsondocontinue:
166132714Srwatson			INP_UNLOCK(in6p);
16753541Sshin			continue;
168132714Srwatson		}
16953541Sshin		if (in6p->in6p_ip6_nxt &&
17053541Sshin		    in6p->in6p_ip6_nxt != proto)
171132714Srwatson			goto docontinue;
17253541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
17353541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
174132714Srwatson			goto docontinue;
17553541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
17653541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
177132714Srwatson			goto docontinue;
17878064Sume		if (in6p->in6p_cksum != -1) {
17978064Sume			rip6stat.rip6s_isum++;
180151459Ssuz			if (in6_cksum(m, proto, *offp,
18178064Sume			    m->m_pkthdr.len - *offp)) {
18278064Sume				rip6stat.rip6s_badsum++;
183132714Srwatson				goto docontinue;
18478064Sume			}
18553541Sshin		}
18653541Sshin		if (last) {
18753541Sshin			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
18878064Sume
189125941Sume#if defined(IPSEC) || defined(FAST_IPSEC)
19078064Sume			/*
19178064Sume			 * Check AH/ESP integrity.
19278064Sume			 */
193125396Sume			if (n && ipsec6_in_reject(n, last)) {
19478064Sume				m_freem(n);
195125941Sume#ifdef IPSEC
19678064Sume				ipsec6stat.in_polvio++;
19778064Sume#endif /*IPSEC*/
198105199Ssam				/* do not inject data into pcb */
199105199Ssam			} else
200125941Sume#endif /*IPSEC || FAST_IPSEC*/
20153541Sshin			if (n) {
20297658Stanimura				if (last->in6p_flags & IN6P_CONTROLOPTS ||
20397658Stanimura				    last->in6p_socket->so_options & SO_TIMESTAMP)
204121674Sume					ip6_savecontrol(last, n, &opts);
20553541Sshin				/* strip intermediate headers */
20653541Sshin				m_adj(n, *offp);
20753541Sshin				if (sbappendaddr(&last->in6p_socket->so_rcv,
208121901Sume						(struct sockaddr *)&fromsa,
20953541Sshin						 n, opts) == 0) {
21053541Sshin					m_freem(n);
21153541Sshin					if (opts)
21253541Sshin						m_freem(opts);
21378064Sume					rip6stat.rip6s_fullsock++;
21497658Stanimura				} else
21553541Sshin					sorwakeup(last->in6p_socket);
21653541Sshin				opts = NULL;
21753541Sshin			}
218132714Srwatson			INP_UNLOCK(last);
21953541Sshin		}
22053541Sshin		last = in6p;
22153541Sshin	}
222125941Sume#if defined(IPSEC) || defined(FAST_IPSEC)
22378064Sume	/*
22478064Sume	 * Check AH/ESP integrity.
22578064Sume	 */
226125396Sume	if (last && ipsec6_in_reject(m, last)) {
22778064Sume		m_freem(m);
228125941Sume#ifdef IPSEC
22978064Sume		ipsec6stat.in_polvio++;
23078064Sume#endif /*IPSEC*/
231105199Ssam		ip6stat.ip6s_delivered--;
232105199Ssam		/* do not inject data into pcb */
233149224Ssuz		INP_UNLOCK(last);
234105199Ssam	} else
235125941Sume#endif /*IPSEC || FAST_IPSEC*/
23653541Sshin	if (last) {
23797658Stanimura		if (last->in6p_flags & IN6P_CONTROLOPTS ||
23897658Stanimura		    last->in6p_socket->so_options & SO_TIMESTAMP)
239121674Sume			ip6_savecontrol(last, m, &opts);
24053541Sshin		/* strip intermediate headers */
24153541Sshin		m_adj(m, *offp);
24253541Sshin		if (sbappendaddr(&last->in6p_socket->so_rcv,
243121901Sume				(struct sockaddr *)&fromsa, m, opts) == 0) {
24453541Sshin			m_freem(m);
24553541Sshin			if (opts)
24653541Sshin				m_freem(opts);
24778064Sume			rip6stat.rip6s_fullsock++;
24897658Stanimura		} else
24953541Sshin			sorwakeup(last->in6p_socket);
250132714Srwatson		INP_UNLOCK(last);
25153541Sshin	} else {
25278064Sume		rip6stat.rip6s_nosock++;
25378064Sume		if (m->m_flags & M_MCAST)
25478064Sume			rip6stat.rip6s_nosockmcast++;
25553541Sshin		if (proto == IPPROTO_NONE)
25653541Sshin			m_freem(m);
25753541Sshin		else {
25853541Sshin			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
25953541Sshin			icmp6_error(m, ICMP6_PARAM_PROB,
26053541Sshin				    ICMP6_PARAMPROB_NEXTHEADER,
26153541Sshin				    prvnxtp - mtod(m, char *));
26253541Sshin		}
26353541Sshin		ip6stat.ip6s_delivered--;
26453541Sshin	}
265134655Srwatson	INP_INFO_RUNLOCK(&ripcbinfo);
26653541Sshin	return IPPROTO_DONE;
26753541Sshin}
26853541Sshin
26962587Sitojunvoid
27062587Sitojunrip6_ctlinput(cmd, sa, d)
27162587Sitojun	int cmd;
27262587Sitojun	struct sockaddr *sa;
27362587Sitojun	void *d;
27462587Sitojun{
27562587Sitojun	struct ip6_hdr *ip6;
27662587Sitojun	struct mbuf *m;
27762587Sitojun	int off = 0;
27878064Sume	struct ip6ctlparam *ip6cp = NULL;
27978064Sume	const struct sockaddr_in6 *sa6_src = NULL;
280125776Sume	void *cmdarg;
28198211Shsu	struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange;
28262587Sitojun
28362587Sitojun	if (sa->sa_family != AF_INET6 ||
28462587Sitojun	    sa->sa_len != sizeof(struct sockaddr_in6))
28562587Sitojun		return;
28662587Sitojun
28762587Sitojun	if ((unsigned)cmd >= PRC_NCMDS)
28862587Sitojun		return;
28962587Sitojun	if (PRC_IS_REDIRECT(cmd))
29062587Sitojun		notify = in6_rtchange, d = NULL;
29162587Sitojun	else if (cmd == PRC_HOSTDEAD)
29262587Sitojun		d = NULL;
29362587Sitojun	else if (inet6ctlerrmap[cmd] == 0)
29462587Sitojun		return;
29562587Sitojun
29662587Sitojun	/* if the parameter is from icmp6, decode it. */
29762587Sitojun	if (d != NULL) {
29878064Sume		ip6cp = (struct ip6ctlparam *)d;
29962587Sitojun		m = ip6cp->ip6c_m;
30062587Sitojun		ip6 = ip6cp->ip6c_ip6;
30162587Sitojun		off = ip6cp->ip6c_off;
302125776Sume		cmdarg = ip6cp->ip6c_cmdarg;
30378064Sume		sa6_src = ip6cp->ip6c_src;
30462587Sitojun	} else {
30562587Sitojun		m = NULL;
30662587Sitojun		ip6 = NULL;
307125776Sume		cmdarg = NULL;
30878064Sume		sa6_src = &sa6_any;
30962587Sitojun	}
31062587Sitojun
311133192Srwatson	(void) in6_pcbnotify(&ripcbinfo, sa, 0,
312133192Srwatson			     (const struct sockaddr *)sa6_src,
313125776Sume			     0, cmd, cmdarg, notify);
31462587Sitojun}
31562587Sitojun
31653541Sshin/*
31753541Sshin * Generate IPv6 header and pass packet to ip6_output.
31853541Sshin * Tack on options user may have setup with control call.
31953541Sshin */
32053541Sshinint
32153541Sshin#if __STDC__
32253541Sshinrip6_output(struct mbuf *m, ...)
32353541Sshin#else
32453541Sshinrip6_output(m, va_alist)
32553541Sshin	struct mbuf *m;
32653541Sshin	va_dcl
32753541Sshin#endif
32853541Sshin{
329120941Sume	struct mbuf *control;
33053541Sshin	struct socket *so;
33153541Sshin	struct sockaddr_in6 *dstsock;
33253541Sshin	struct in6_addr *dst;
33353541Sshin	struct ip6_hdr *ip6;
33453541Sshin	struct inpcb *in6p;
33553541Sshin	u_int	plen = m->m_pkthdr.len;
33653541Sshin	int error = 0;
337148247Sume	struct ip6_pktopts opt, *optp;
33853541Sshin	struct ifnet *oifp = NULL;
33953541Sshin	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
34053541Sshin	int priv = 0;
341148385Sume	int scope_ambiguous = 0;
342121472Sume	struct in6_addr *in6a;
34353541Sshin	va_list ap;
34453541Sshin
34553541Sshin	va_start(ap, m);
34653541Sshin	so = va_arg(ap, struct socket *);
34753541Sshin	dstsock = va_arg(ap, struct sockaddr_in6 *);
34853541Sshin	control = va_arg(ap, struct mbuf *);
34953541Sshin	va_end(ap);
35053541Sshin
35153541Sshin	in6p = sotoin6pcb(so);
352132714Srwatson	INP_LOCK(in6p);
35353541Sshin
35453541Sshin	priv = 0;
355170587Srwatson	if (suser_cred(so->so_cred, 0) == 0)
35653541Sshin		priv = 1;
35753541Sshin	dst = &dstsock->sin6_addr;
35853541Sshin	if (control) {
359148242Sume		if ((error = ip6_setpktopts(control, &opt,
360148250Sume		    in6p->in6p_outputopts, priv, so->so_proto->pr_protocol))
361121472Sume		    != 0) {
36253541Sshin			goto bad;
363121472Sume		}
364148247Sume		optp = &opt;
365148247Sume	} else
366148247Sume		optp = in6p->in6p_outputopts;
36753541Sshin
36853541Sshin	/*
369148385Sume	 * Check and convert scope zone ID into internal form.
370148385Sume	 * XXX: we may still need to determine the zone later.
371148385Sume	 */
372148385Sume	if (!(so->so_state & SS_ISCONNECTED)) {
373148385Sume		if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
374148385Sume			scope_ambiguous = 1;
375148385Sume		if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
376148385Sume			goto bad;
377148385Sume	}
378148385Sume
379148385Sume	/*
38053541Sshin	 * For an ICMPv6 packet, we should know its type and code
38153541Sshin	 * to update statistics.
38253541Sshin	 */
38353541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
38453541Sshin		struct icmp6_hdr *icmp6;
38553541Sshin		if (m->m_len < sizeof(struct icmp6_hdr) &&
38653541Sshin		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
38753541Sshin			error = ENOBUFS;
38853541Sshin			goto bad;
38953541Sshin		}
39053541Sshin		icmp6 = mtod(m, struct icmp6_hdr *);
39153541Sshin		type = icmp6->icmp6_type;
39253541Sshin		code = icmp6->icmp6_code;
39353541Sshin	}
39453541Sshin
395133592Srwatson	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
396133592Srwatson	if (m == NULL) {
397133592Srwatson		error = ENOBUFS;
398133592Srwatson		goto bad;
399133592Srwatson	}
40053541Sshin	ip6 = mtod(m, struct ip6_hdr *);
40153541Sshin
40253541Sshin	/*
40353541Sshin	 * Source address selection.
40453541Sshin	 */
405148247Sume	if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, NULL,
406148385Sume	    &in6p->in6p_laddr, &oifp, &error)) == NULL) {
407121472Sume		if (error == 0)
408121472Sume			error = EADDRNOTAVAIL;
409121472Sume		goto bad;
41053541Sshin	}
411121472Sume	ip6->ip6_src = *in6a;
412148385Sume
413148385Sume	if (oifp && scope_ambiguous) {
414148385Sume		/*
415148385Sume		 * Application should provide a proper zone ID or the use of
416148385Sume		 * default zone IDs should be enabled.  Unfortunately, some
417148385Sume		 * applications do not behave as it should, so we need a
418148385Sume		 * workaround.  Even if an appropriate ID is not determined
419148385Sume		 * (when it's required), if we can determine the outgoing
420148385Sume		 * interface. determine the zone ID based on the interface.
421148385Sume		 */
422148385Sume		error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
423148385Sume		if (error != 0)
424148385Sume			goto bad;
425148385Sume	}
426148385Sume	ip6->ip6_dst = dstsock->sin6_addr;
427148385Sume
428148385Sume	/* fill in the rest of the IPv6 header fields */
42955009Sshin	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
43055009Sshin		(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
43155009Sshin	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
43255009Sshin		(IPV6_VERSION & IPV6_VERSION_MASK);
43353541Sshin	/* ip6_plen will be filled in ip6_output, so not fill it here. */
43453541Sshin	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
43553541Sshin	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
43653541Sshin
43753541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
43853541Sshin	    in6p->in6p_cksum != -1) {
43953541Sshin		struct mbuf *n;
44053541Sshin		int off;
44153541Sshin		u_int16_t *p;
44253541Sshin
44353541Sshin		/* compute checksum */
44453541Sshin		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
44553541Sshin			off = offsetof(struct icmp6_hdr, icmp6_cksum);
44653541Sshin		else
44753541Sshin			off = in6p->in6p_cksum;
44853541Sshin		if (plen < off + 1) {
44953541Sshin			error = EINVAL;
45053541Sshin			goto bad;
45153541Sshin		}
45253541Sshin		off += sizeof(struct ip6_hdr);
45353541Sshin
45453541Sshin		n = m;
45553541Sshin		while (n && n->m_len <= off) {
45653541Sshin			off -= n->m_len;
45753541Sshin			n = n->m_next;
45853541Sshin		}
45953541Sshin		if (!n)
46053541Sshin			goto bad;
46153541Sshin		p = (u_int16_t *)(mtod(n, caddr_t) + off);
46253541Sshin		*p = 0;
46353541Sshin		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
46453541Sshin	}
46553541Sshin
466148247Sume	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
46753541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
46853541Sshin		if (oifp)
46953541Sshin			icmp6_ifoutstat_inc(oifp, type, code);
47053541Sshin		icmp6stat.icp6s_outhist[type]++;
47178064Sume	} else
47278064Sume		rip6stat.rip6s_opackets++;
47353541Sshin
47453541Sshin	goto freectl;
47553541Sshin
47653541Sshin bad:
47753541Sshin	if (m)
47853541Sshin		m_freem(m);
47953541Sshin
48053541Sshin freectl:
48178064Sume	if (control) {
482148247Sume		ip6_clearpktopts(&opt, -1);
48353541Sshin		m_freem(control);
48478064Sume	}
485132714Srwatson	INP_UNLOCK(in6p);
486120856Sume	return (error);
48753541Sshin}
48853541Sshin
48953541Sshin/*
49053541Sshin * Raw IPv6 socket option processing.
49153541Sshin */
49253541Sshinint
49353541Sshinrip6_ctloutput(so, sopt)
49453541Sshin	struct socket *so;
49553541Sshin	struct sockopt *sopt;
49653541Sshin{
49753541Sshin	int error;
49853541Sshin
49953541Sshin	if (sopt->sopt_level == IPPROTO_ICMPV6)
50053541Sshin		/*
50153541Sshin		 * XXX: is it better to call icmp6_ctloutput() directly
50253541Sshin		 * from protosw?
50353541Sshin		 */
504120856Sume		return (icmp6_ctloutput(so, sopt));
50553541Sshin	else if (sopt->sopt_level != IPPROTO_IPV6)
50653541Sshin		return (EINVAL);
50753541Sshin
50853541Sshin	error = 0;
50953541Sshin
51053541Sshin	switch (sopt->sopt_dir) {
51153541Sshin	case SOPT_GET:
51253541Sshin		switch (sopt->sopt_name) {
51356723Sshin		case MRT6_INIT:
51456723Sshin		case MRT6_DONE:
51556723Sshin		case MRT6_ADD_MIF:
51656723Sshin		case MRT6_DEL_MIF:
51756723Sshin		case MRT6_ADD_MFC:
51856723Sshin		case MRT6_DEL_MFC:
51956723Sshin		case MRT6_PIM:
520166938Sbms			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
521166938Sbms			    EOPNOTSUPP;
52256723Sshin			break;
523121578Sume		case IPV6_CHECKSUM:
524121578Sume			error = ip6_raw_ctloutput(so, sopt);
525121578Sume			break;
52653541Sshin		default:
52753541Sshin			error = ip6_ctloutput(so, sopt);
52853541Sshin			break;
52953541Sshin		}
53053541Sshin		break;
53153541Sshin
53253541Sshin	case SOPT_SET:
53353541Sshin		switch (sopt->sopt_name) {
53456723Sshin		case MRT6_INIT:
53556723Sshin		case MRT6_DONE:
53656723Sshin		case MRT6_ADD_MIF:
53756723Sshin		case MRT6_DEL_MIF:
53856723Sshin		case MRT6_ADD_MFC:
53956723Sshin		case MRT6_DEL_MFC:
54056723Sshin		case MRT6_PIM:
541166938Sbms			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
542166938Sbms			    EOPNOTSUPP;
54356723Sshin			break;
544121578Sume		case IPV6_CHECKSUM:
545121578Sume			error = ip6_raw_ctloutput(so, sopt);
546121578Sume			break;
54753541Sshin		default:
54853541Sshin			error = ip6_ctloutput(so, sopt);
54953541Sshin			break;
55053541Sshin		}
55153541Sshin		break;
55253541Sshin	}
55353541Sshin
55453541Sshin	return (error);
55553541Sshin}
55653541Sshin
55753541Sshinstatic int
55883366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td)
55953541Sshin{
56053541Sshin	struct inpcb *inp;
561144261Ssam	struct icmp6_filter *filter;
562157676Srwatson	int error;
56353541Sshin
56453541Sshin	inp = sotoinpcb(so);
565157374Srwatson	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
566157374Srwatson	if (td && (error = suser(td)) != 0)
56753541Sshin		return error;
56855009Sshin	error = soreserve(so, rip_sendspace, rip_recvspace);
569157374Srwatson	if (error)
57055009Sshin		return error;
571144261Ssam	MALLOC(filter, struct icmp6_filter *,
572144261Ssam	       sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
573157374Srwatson	if (filter == NULL)
574144261Ssam		return ENOMEM;
575157374Srwatson	INP_INFO_WLOCK(&ripcbinfo);
576160491Sups	error = in_pcballoc(so, &ripcbinfo);
577132714Srwatson	if (error) {
578132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
579144261Ssam		FREE(filter, M_PCB);
58053541Sshin		return error;
581132714Srwatson	}
58253541Sshin	inp = (struct inpcb *)so->so_pcb;
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
59953541Sshin	inp = sotoinpcb(so);
600157374Srwatson	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
601160549Srwatson
602166938Sbms	if (so == ip6_mrouter && ip6_mrouter_done)
603166938Sbms		ip6_mrouter_done();
60453541Sshin	/* xxx: RSVP */
605160549Srwatson	INP_INFO_WLOCK(&ripcbinfo);
606160549Srwatson	INP_LOCK(inp);
60753541Sshin	if (inp->in6p_icmp6filt) {
60853541Sshin		FREE(inp->in6p_icmp6filt, M_PCB);
60953541Sshin		inp->in6p_icmp6filt = NULL;
61053541Sshin	}
61153541Sshin	in6_pcbdetach(inp);
612157374Srwatson	in6_pcbfree(inp);
613132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
61453541Sshin}
61553541Sshin
616160549Srwatson/* XXXRW: This can't ever be called. */
617157366Srwatsonstatic void
61853541Sshinrip6_abort(struct socket *so)
61953541Sshin{
620160549Srwatson	struct inpcb *inp;
621160549Srwatson
622160549Srwatson	inp = sotoinpcb(so);
623160549Srwatson	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
624160549Srwatson
62553541Sshin	soisdisconnected(so);
62653541Sshin}
62753541Sshin
628160549Srwatsonstatic void
629160549Srwatsonrip6_close(struct socket *so)
630160549Srwatson{
631160549Srwatson	struct inpcb *inp;
632160549Srwatson
633160549Srwatson	inp = sotoinpcb(so);
634160549Srwatson	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
635160549Srwatson
636160549Srwatson	soisdisconnected(so);
637160549Srwatson}
638160549Srwatson
63953541Sshinstatic int
64053541Sshinrip6_disconnect(struct socket *so)
64153541Sshin{
64253541Sshin	struct inpcb *inp = sotoinpcb(so);
64353541Sshin
64497658Stanimura	if ((so->so_state & SS_ISCONNECTED) == 0)
64553541Sshin		return ENOTCONN;
64653541Sshin	inp->in6p_faddr = in6addr_any;
647157366Srwatson	rip6_abort(so);
648157374Srwatson	return (0);
64953541Sshin}
65053541Sshin
65153541Sshinstatic int
65283366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
65353541Sshin{
65453541Sshin	struct inpcb *inp = sotoinpcb(so);
65553541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
65653541Sshin	struct ifaddr *ia = NULL;
657148385Sume	int error = 0;
65853541Sshin
659157374Srwatson	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
66053541Sshin	if (nam->sa_len != sizeof(*addr))
66153541Sshin		return EINVAL;
66253541Sshin	if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
66353541Sshin		return EADDRNOTAVAIL;
664148385Sume	if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
665148385Sume		return(error);
666148385Sume
66753541Sshin	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
66853541Sshin	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
66953541Sshin		return EADDRNOTAVAIL;
67053541Sshin	if (ia &&
67153541Sshin	    ((struct in6_ifaddr *)ia)->ia6_flags &
67253541Sshin	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
67353541Sshin	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
674120856Sume		return (EADDRNOTAVAIL);
67553541Sshin	}
676132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
677132714Srwatson	INP_LOCK(inp);
67853541Sshin	inp->in6p_laddr = addr->sin6_addr;
679132714Srwatson	INP_UNLOCK(inp);
680132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
68153541Sshin	return 0;
68253541Sshin}
68353541Sshin
68453541Sshinstatic int
68583366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
68653541Sshin{
68753541Sshin	struct inpcb *inp = sotoinpcb(so);
68853541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
68953541Sshin	struct in6_addr *in6a = NULL;
690148385Sume	struct ifnet *ifp = NULL;
691148385Sume	int error = 0, scope_ambiguous = 0;
69253541Sshin
693157374Srwatson	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
69453541Sshin	if (nam->sa_len != sizeof(*addr))
69553541Sshin		return EINVAL;
69653541Sshin	if (TAILQ_EMPTY(&ifnet))
69753541Sshin		return EADDRNOTAVAIL;
69853541Sshin	if (addr->sin6_family != AF_INET6)
69953541Sshin		return EAFNOSUPPORT;
700148385Sume
701148385Sume	/*
702148385Sume	 * Application should provide a proper zone ID or the use of
703148385Sume	 * default zone IDs should be enabled.  Unfortunately, some
704148385Sume	 * applications do not behave as it should, so we need a
705148385Sume	 * workaround.  Even if an appropriate ID is not determined,
706148385Sume	 * we'll see if we can determine the outgoing interface.  If we
707148385Sume	 * can, determine the zone ID based on the interface below.
708148385Sume	 */
709148385Sume	if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
710148385Sume		scope_ambiguous = 1;
711148385Sume	if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
712148385Sume		return(error);
713148385Sume
714132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
715132714Srwatson	INP_LOCK(inp);
71653541Sshin	/* Source address selection. XXX: need pcblookup? */
71753541Sshin	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
718122927Sandre			     inp->in6p_moptions, NULL,
719148385Sume			     &inp->in6p_laddr, &ifp, &error);
720132714Srwatson	if (in6a == NULL) {
721132714Srwatson		INP_UNLOCK(inp);
722132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
72353541Sshin		return (error ? error : EADDRNOTAVAIL);
724132714Srwatson	}
725148385Sume
726148385Sume	/* XXX: see above */
727148385Sume	if (ifp && scope_ambiguous &&
728148385Sume	    (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
729148385Sume		INP_UNLOCK(inp);
730148385Sume		INP_INFO_WUNLOCK(&ripcbinfo);
731148385Sume		return(error);
732148385Sume	}
733148385Sume	inp->in6p_faddr = addr->sin6_addr;
73453541Sshin	inp->in6p_laddr = *in6a;
73553541Sshin	soisconnected(so);
736132714Srwatson	INP_UNLOCK(inp);
737132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
73853541Sshin	return 0;
73953541Sshin}
74053541Sshin
74153541Sshinstatic int
74253541Sshinrip6_shutdown(struct socket *so)
74353541Sshin{
744132714Srwatson	struct inpcb *inp;
745132714Srwatson
746132714Srwatson	inp = sotoinpcb(so);
747157374Srwatson	KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
748132714Srwatson	INP_LOCK(inp);
74953541Sshin	socantsendmore(so);
750132714Srwatson	INP_UNLOCK(inp);
75153541Sshin	return 0;
75253541Sshin}
75353541Sshin
75453541Sshinstatic int
75553541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
75683366Sjulian	 struct mbuf *control, struct thread *td)
75753541Sshin{
75853541Sshin	struct inpcb *inp = sotoinpcb(so);
75953541Sshin	struct sockaddr_in6 tmp;
76053541Sshin	struct sockaddr_in6 *dst;
761132714Srwatson	int ret;
76253541Sshin
763157374Srwatson	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
764132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
76562587Sitojun	/* always copy sockaddr to avoid overwrites */
766132714Srwatson	/* Unlocked read. */
76753541Sshin	if (so->so_state & SS_ISCONNECTED) {
76853541Sshin		if (nam) {
769132714Srwatson			INP_INFO_WUNLOCK(&ripcbinfo);
77053541Sshin			m_freem(m);
77153541Sshin			return EISCONN;
77253541Sshin		}
77353541Sshin		/* XXX */
77453541Sshin		bzero(&tmp, sizeof(tmp));
77553541Sshin		tmp.sin6_family = AF_INET6;
77653541Sshin		tmp.sin6_len = sizeof(struct sockaddr_in6);
77753541Sshin		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
77853541Sshin		      sizeof(struct in6_addr));
77953541Sshin		dst = &tmp;
78053541Sshin	} else {
78153541Sshin		if (nam == NULL) {
782132714Srwatson			INP_INFO_WUNLOCK(&ripcbinfo);
78353541Sshin			m_freem(m);
78453541Sshin			return ENOTCONN;
78553541Sshin		}
786148385Sume		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
787148385Sume			INP_INFO_WUNLOCK(&ripcbinfo);
788148385Sume			m_freem(m);
789148385Sume			return(EINVAL);
790148385Sume		}
79162587Sitojun		tmp = *(struct sockaddr_in6 *)nam;
79262587Sitojun		dst = &tmp;
793148385Sume
794148385Sume		if (dst->sin6_family == AF_UNSPEC) {
795148385Sume			/*
796148385Sume			 * XXX: we allow this case for backward
797148385Sume			 * compatibility to buggy applications that
798148385Sume			 * rely on old (and wrong) kernel behavior.
799148385Sume			 */
800148385Sume			log(LOG_INFO, "rip6 SEND: address family is "
801148385Sume			    "unspec. Assume AF_INET6\n");
802148385Sume			dst->sin6_family = AF_INET6;
803148385Sume		} else if (dst->sin6_family != AF_INET6) {
804148385Sume			INP_INFO_WUNLOCK(&ripcbinfo);
805148385Sume			m_freem(m);
806148385Sume			return(EAFNOSUPPORT);
807148385Sume		}
80853541Sshin	}
809132714Srwatson	ret = rip6_output(m, so, dst, control);
810132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
811132714Srwatson	return (ret);
81253541Sshin}
81353541Sshin
81453541Sshinstruct pr_usrreqs rip6_usrreqs = {
815137386Sphk	.pru_abort =		rip6_abort,
816137386Sphk	.pru_attach =		rip6_attach,
817137386Sphk	.pru_bind =		rip6_bind,
818137386Sphk	.pru_connect =		rip6_connect,
819137386Sphk	.pru_control =		in6_control,
820137386Sphk	.pru_detach =		rip6_detach,
821137386Sphk	.pru_disconnect =	rip6_disconnect,
822169462Srwatson	.pru_peeraddr =		in6_getpeeraddr,
823137386Sphk	.pru_send =		rip6_send,
824137386Sphk	.pru_shutdown =		rip6_shutdown,
825169462Srwatson	.pru_sockaddr =		in6_getsockaddr,
826160549Srwatson	.pru_close =		rip6_close,
82753541Sshin};
828