raw_ip6.c revision 78064
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 78064 2001-06-11 12:39:29Z ume $
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 * 3. All advertising materials mentioning features or use of this software
4553541Sshin *    must display the following acknowledgement:
4653541Sshin *	This product includes software developed by the University of
4753541Sshin *	California, Berkeley and its contributors.
4853541Sshin * 4. Neither the name of the University nor the names of its contributors
4953541Sshin *    may be used to endorse or promote products derived from this software
5053541Sshin *    without specific prior written permission.
5153541Sshin *
5253541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5353541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5453541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5553541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5653541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5753541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5853541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5953541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6053541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6153541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6253541Sshin * SUCH DAMAGE.
6353541Sshin *
6453541Sshin *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
6553541Sshin */
6653541Sshin
6755009Sshin#include "opt_ipsec.h"
6878064Sume#include "opt_inet6.h"
6955009Sshin
7053541Sshin#include <sys/param.h>
7153541Sshin#include <sys/malloc.h>
7253541Sshin#include <sys/proc.h>
7353541Sshin#include <sys/mbuf.h>
7453541Sshin#include <sys/socket.h>
7553541Sshin#include <sys/protosw.h>
7653541Sshin#include <sys/socketvar.h>
7753541Sshin#include <sys/errno.h>
7853541Sshin#include <sys/systm.h>
7953541Sshin
8053541Sshin#include <net/if.h>
8153541Sshin#include <net/route.h>
8253541Sshin#include <net/if_types.h>
8353541Sshin
8453541Sshin#include <netinet/in.h>
8553541Sshin#include <netinet/in_var.h>
8653541Sshin#include <netinet/in_systm.h>
8762587Sitojun#include <netinet/ip6.h>
8853541Sshin#include <netinet6/ip6_var.h>
8956723Sshin#include <netinet6/ip6_mroute.h>
9062587Sitojun#include <netinet/icmp6.h>
9153541Sshin#include <netinet/in_pcb.h>
9253541Sshin#include <netinet6/in6_pcb.h>
9353541Sshin#include <netinet6/nd6.h>
9462587Sitojun#include <netinet6/ip6protosw.h>
9562587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
9662587Sitojun#include <netinet6/scope6_var.h>
9762587Sitojun#endif
9878064Sume#include <netinet6/raw_ip6.h>
9953541Sshin
10053541Sshin#ifdef IPSEC
10153541Sshin#include <netinet6/ipsec.h>
10253541Sshin#include <netinet6/ipsec6.h>
10353541Sshin#endif /*IPSEC*/
10453541Sshin
10553541Sshin#include <machine/stdarg.h>
10653541Sshin
10754263Sshin#include "faith.h"
10878064Sume#if defined(NFAITH) && 0 < NFAITH
10978064Sume#include <net/if_faith.h>
11078064Sume#endif
11153541Sshin
11253541Sshin#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
11353541Sshin#define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
11453541Sshin
11553541Sshin/*
11653541Sshin * Raw interface to IP6 protocol.
11753541Sshin */
11853541Sshin
11953541Sshinextern struct	inpcbhead ripcb;
12053541Sshinextern struct	inpcbinfo ripcbinfo;
12153541Sshinextern u_long	rip_sendspace;
12253541Sshinextern u_long	rip_recvspace;
12353541Sshin
12478064Sumestruct rip6stat rip6stat;
12578064Sume
12653541Sshin/*
12753541Sshin * Setup generic address and protocol structures
12853541Sshin * for raw_input routine, then pass them along with
12953541Sshin * mbuf chain.
13053541Sshin */
13153541Sshinint
13253541Sshinrip6_input(mp, offp, proto)
13353541Sshin	struct	mbuf **mp;
13453541Sshin	int	*offp, proto;
13553541Sshin{
13653541Sshin	struct mbuf *m = *mp;
13753541Sshin	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
13853541Sshin	register struct inpcb *in6p;
13953541Sshin	struct inpcb *last = 0;
14078064Sume	struct mbuf *opts = NULL;
14153541Sshin	struct sockaddr_in6 rip6src;
14253541Sshin
14378064Sume	rip6stat.rip6s_ipackets++;
14478064Sume
14553541Sshin#if defined(NFAITH) && 0 < NFAITH
14678064Sume	if (faithprefix(&ip6->ip6_dst)) {
14778064Sume		/* XXX send icmp6 host/port unreach? */
14878064Sume		m_freem(m);
14978064Sume		return IPPROTO_DONE;
15053541Sshin	}
15153541Sshin#endif
15278064Sume
15353541Sshin	init_sin6(&rip6src, m); /* general init */
15453541Sshin
15553541Sshin	LIST_FOREACH(in6p, &ripcb, inp_list) {
15655009Sshin		if ((in6p->in6p_vflag & INP_IPV6) == 0)
15753541Sshin			continue;
15853541Sshin		if (in6p->in6p_ip6_nxt &&
15953541Sshin		    in6p->in6p_ip6_nxt != proto)
16053541Sshin			continue;
16153541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
16253541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
16353541Sshin			continue;
16453541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
16553541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
16653541Sshin			continue;
16778064Sume		if (in6p->in6p_cksum != -1) {
16878064Sume			rip6stat.rip6s_isum++;
16978064Sume			if (in6_cksum(m, ip6->ip6_nxt, *offp,
17078064Sume			    m->m_pkthdr.len - *offp)) {
17178064Sume				rip6stat.rip6s_badsum++;
17278064Sume				continue;
17378064Sume			}
17453541Sshin		}
17553541Sshin		if (last) {
17653541Sshin			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
17778064Sume
17878064Sume#ifdef IPSEC
17978064Sume			/*
18078064Sume			 * Check AH/ESP integrity.
18178064Sume			 */
18278064Sume			if (n && ipsec6_in_reject_so(n, last->inp_socket)) {
18378064Sume				m_freem(n);
18478064Sume				ipsec6stat.in_polvio++;
18578064Sume				/* do not inject data into pcb */
18678064Sume			} else
18778064Sume#endif /*IPSEC*/
18853541Sshin			if (n) {
18953541Sshin				if (last->in6p_flags & IN6P_CONTROLOPTS ||
19053541Sshin				    last->in6p_socket->so_options & SO_TIMESTAMP)
19153541Sshin					ip6_savecontrol(last, &opts, ip6, n);
19253541Sshin				/* strip intermediate headers */
19353541Sshin				m_adj(n, *offp);
19453541Sshin				if (sbappendaddr(&last->in6p_socket->so_rcv,
19553541Sshin						(struct sockaddr *)&rip6src,
19653541Sshin						 n, opts) == 0) {
19753541Sshin					m_freem(n);
19853541Sshin					if (opts)
19953541Sshin						m_freem(opts);
20078064Sume					rip6stat.rip6s_fullsock++;
20153541Sshin				} else
20253541Sshin					sorwakeup(last->in6p_socket);
20353541Sshin				opts = NULL;
20453541Sshin			}
20553541Sshin		}
20653541Sshin		last = in6p;
20753541Sshin	}
20878064Sume#ifdef IPSEC
20978064Sume	/*
21078064Sume	 * Check AH/ESP integrity.
21178064Sume	 */
21278064Sume	if (last && ipsec6_in_reject_so(m, last->inp_socket)) {
21378064Sume		m_freem(m);
21478064Sume		ipsec6stat.in_polvio++;
21578064Sume		ip6stat.ip6s_delivered--;
21678064Sume		/* do not inject data into pcb */
21778064Sume	} else
21878064Sume#endif /*IPSEC*/
21953541Sshin	if (last) {
22053541Sshin		if (last->in6p_flags & IN6P_CONTROLOPTS ||
22153541Sshin		    last->in6p_socket->so_options & SO_TIMESTAMP)
22253541Sshin			ip6_savecontrol(last, &opts, ip6, m);
22353541Sshin		/* strip intermediate headers */
22453541Sshin		m_adj(m, *offp);
22553541Sshin		if (sbappendaddr(&last->in6p_socket->so_rcv,
22653541Sshin				(struct sockaddr *)&rip6src, m, opts) == 0) {
22753541Sshin			m_freem(m);
22853541Sshin			if (opts)
22953541Sshin				m_freem(opts);
23078064Sume			rip6stat.rip6s_fullsock++;
23153541Sshin		} else
23253541Sshin			sorwakeup(last->in6p_socket);
23353541Sshin	} else {
23478064Sume		rip6stat.rip6s_nosock++;
23578064Sume		if (m->m_flags & M_MCAST)
23678064Sume			rip6stat.rip6s_nosockmcast++;
23753541Sshin		if (proto == IPPROTO_NONE)
23853541Sshin			m_freem(m);
23953541Sshin		else {
24053541Sshin			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
24153541Sshin			icmp6_error(m, ICMP6_PARAM_PROB,
24253541Sshin				    ICMP6_PARAMPROB_NEXTHEADER,
24353541Sshin				    prvnxtp - mtod(m, char *));
24453541Sshin		}
24553541Sshin		ip6stat.ip6s_delivered--;
24653541Sshin	}
24753541Sshin	return IPPROTO_DONE;
24853541Sshin}
24953541Sshin
25062587Sitojunvoid
25162587Sitojunrip6_ctlinput(cmd, sa, d)
25262587Sitojun	int cmd;
25362587Sitojun	struct sockaddr *sa;
25462587Sitojun	void *d;
25562587Sitojun{
25662587Sitojun	struct ip6_hdr *ip6;
25762587Sitojun	struct mbuf *m;
25862587Sitojun	int off = 0;
25978064Sume	struct ip6ctlparam *ip6cp = NULL;
26078064Sume	const struct sockaddr_in6 *sa6_src = NULL;
26162587Sitojun	void (*notify) __P((struct inpcb *, int)) = in6_rtchange;
26262587Sitojun
26362587Sitojun	if (sa->sa_family != AF_INET6 ||
26462587Sitojun	    sa->sa_len != sizeof(struct sockaddr_in6))
26562587Sitojun		return;
26662587Sitojun
26762587Sitojun	if ((unsigned)cmd >= PRC_NCMDS)
26862587Sitojun		return;
26962587Sitojun	if (PRC_IS_REDIRECT(cmd))
27062587Sitojun		notify = in6_rtchange, d = NULL;
27162587Sitojun	else if (cmd == PRC_HOSTDEAD)
27262587Sitojun		d = NULL;
27362587Sitojun	else if (inet6ctlerrmap[cmd] == 0)
27462587Sitojun		return;
27562587Sitojun
27662587Sitojun	/* if the parameter is from icmp6, decode it. */
27762587Sitojun	if (d != NULL) {
27878064Sume		ip6cp = (struct ip6ctlparam *)d;
27962587Sitojun		m = ip6cp->ip6c_m;
28062587Sitojun		ip6 = ip6cp->ip6c_ip6;
28162587Sitojun		off = ip6cp->ip6c_off;
28278064Sume		sa6_src = ip6cp->ip6c_src;
28362587Sitojun	} else {
28462587Sitojun		m = NULL;
28562587Sitojun		ip6 = NULL;
28678064Sume		sa6_src = &sa6_any;
28762587Sitojun	}
28862587Sitojun
28978064Sume	(void) in6_pcbnotify(&ripcb, sa, 0, (struct sockaddr *)sa6_src,
29078064Sume			     0, cmd, notify);
29162587Sitojun}
29262587Sitojun
29353541Sshin/*
29453541Sshin * Generate IPv6 header and pass packet to ip6_output.
29553541Sshin * Tack on options user may have setup with control call.
29653541Sshin */
29753541Sshinint
29853541Sshin#if __STDC__
29953541Sshinrip6_output(struct mbuf *m, ...)
30053541Sshin#else
30153541Sshinrip6_output(m, va_alist)
30253541Sshin	struct mbuf *m;
30353541Sshin	va_dcl
30453541Sshin#endif
30553541Sshin{
30653541Sshin	struct socket *so;
30753541Sshin	struct sockaddr_in6 *dstsock;
30853541Sshin	struct mbuf *control;
30953541Sshin	struct in6_addr *dst;
31053541Sshin	struct ip6_hdr *ip6;
31153541Sshin	struct inpcb *in6p;
31253541Sshin	u_int	plen = m->m_pkthdr.len;
31353541Sshin	int error = 0;
31453541Sshin	struct ip6_pktopts opt, *optp = 0;
31553541Sshin	struct ifnet *oifp = NULL;
31653541Sshin	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
31753541Sshin	int priv = 0;
31853541Sshin	va_list ap;
31953541Sshin
32053541Sshin	va_start(ap, m);
32153541Sshin	so = va_arg(ap, struct socket *);
32253541Sshin	dstsock = va_arg(ap, struct sockaddr_in6 *);
32353541Sshin	control = va_arg(ap, struct mbuf *);
32453541Sshin	va_end(ap);
32553541Sshin
32653541Sshin	in6p = sotoin6pcb(so);
32753541Sshin
32853541Sshin	priv = 0;
32953541Sshin	if (so->so_cred->cr_uid == 0)
33053541Sshin		priv = 1;
33153541Sshin	dst = &dstsock->sin6_addr;
33253541Sshin	if (control) {
33378064Sume		if ((error = ip6_setpktoptions(control, &opt, priv, 0)) != 0)
33453541Sshin			goto bad;
33553541Sshin		optp = &opt;
33653541Sshin	} else
33753541Sshin		optp = in6p->in6p_outputopts;
33853541Sshin
33953541Sshin	/*
34053541Sshin	 * For an ICMPv6 packet, we should know its type and code
34153541Sshin	 * to update statistics.
34253541Sshin	 */
34353541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
34453541Sshin		struct icmp6_hdr *icmp6;
34553541Sshin		if (m->m_len < sizeof(struct icmp6_hdr) &&
34653541Sshin		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
34753541Sshin			error = ENOBUFS;
34853541Sshin			goto bad;
34953541Sshin		}
35053541Sshin		icmp6 = mtod(m, struct icmp6_hdr *);
35153541Sshin		type = icmp6->icmp6_type;
35253541Sshin		code = icmp6->icmp6_code;
35353541Sshin	}
35453541Sshin
35570254Sbmilekic	M_PREPEND(m, sizeof(*ip6), M_TRYWAIT);
35653541Sshin	ip6 = mtod(m, struct ip6_hdr *);
35753541Sshin
35853541Sshin	/*
35953541Sshin	 * Next header might not be ICMP6 but use its pseudo header anyway.
36053541Sshin	 */
36153541Sshin	ip6->ip6_dst = *dst;
36253541Sshin
36353541Sshin	/*
36453541Sshin	 * If the scope of the destination is link-local, embed the interface
36553541Sshin	 * index in the address.
36653541Sshin	 *
36753541Sshin	 * XXX advanced-api value overrides sin6_scope_id
36853541Sshin	 */
36953541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
37053541Sshin		struct in6_pktinfo *pi;
37153541Sshin
37253541Sshin		/*
37353541Sshin		 * XXX Boundary check is assumed to be already done in
37453541Sshin		 * ip6_setpktoptions().
37553541Sshin		 */
37653541Sshin		if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
37753541Sshin			ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
37853541Sshin			oifp = ifindex2ifnet[pi->ipi6_ifindex];
37953541Sshin		} else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
38053541Sshin			 in6p->in6p_moptions &&
38153541Sshin			 in6p->in6p_moptions->im6o_multicast_ifp) {
38253541Sshin			oifp = in6p->in6p_moptions->im6o_multicast_ifp;
38353541Sshin			ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
38453541Sshin		} else if (dstsock->sin6_scope_id) {
38553541Sshin			/* boundary check */
38653541Sshin			if (dstsock->sin6_scope_id < 0
38753541Sshin			 || if_index < dstsock->sin6_scope_id) {
38853541Sshin				error = ENXIO;  /* XXX EINVAL? */
38953541Sshin				goto bad;
39053541Sshin			}
39153541Sshin			ip6->ip6_dst.s6_addr16[1]
39253541Sshin				= htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
39353541Sshin		}
39453541Sshin	}
39553541Sshin
39653541Sshin	/*
39753541Sshin	 * Source address selection.
39853541Sshin	 */
39953541Sshin	{
40053541Sshin		struct in6_addr *in6a;
40153541Sshin
40253541Sshin		if ((in6a = in6_selectsrc(dstsock, optp,
40353541Sshin					  in6p->in6p_moptions,
40453541Sshin					  &in6p->in6p_route,
40553541Sshin					  &in6p->in6p_laddr,
40653541Sshin					  &error)) == 0) {
40753541Sshin			if (error == 0)
40853541Sshin				error = EADDRNOTAVAIL;
40953541Sshin			goto bad;
41053541Sshin		}
41153541Sshin		ip6->ip6_src = *in6a;
41253541Sshin		if (in6p->in6p_route.ro_rt)
41353541Sshin			oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
41453541Sshin	}
41555009Sshin	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
41655009Sshin		(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
41755009Sshin	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
41855009Sshin		(IPV6_VERSION & IPV6_VERSION_MASK);
41953541Sshin	/* ip6_plen will be filled in ip6_output, so not fill it here. */
42053541Sshin	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
42153541Sshin	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
42253541Sshin
42353541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
42453541Sshin	    in6p->in6p_cksum != -1) {
42553541Sshin		struct mbuf *n;
42653541Sshin		int off;
42753541Sshin		u_int16_t *p;
42853541Sshin
42953541Sshin		/* compute checksum */
43053541Sshin		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
43153541Sshin			off = offsetof(struct icmp6_hdr, icmp6_cksum);
43253541Sshin		else
43353541Sshin			off = in6p->in6p_cksum;
43453541Sshin		if (plen < off + 1) {
43553541Sshin			error = EINVAL;
43653541Sshin			goto bad;
43753541Sshin		}
43853541Sshin		off += sizeof(struct ip6_hdr);
43953541Sshin
44053541Sshin		n = m;
44153541Sshin		while (n && n->m_len <= off) {
44253541Sshin			off -= n->m_len;
44353541Sshin			n = n->m_next;
44453541Sshin		}
44553541Sshin		if (!n)
44653541Sshin			goto bad;
44753541Sshin		p = (u_int16_t *)(mtod(n, caddr_t) + off);
44853541Sshin		*p = 0;
44953541Sshin		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
45053541Sshin	}
45153541Sshin
45253541Sshin#ifdef IPSEC
45378064Sume	if (ipsec_setsocket(m, so) != 0) {
45478064Sume		error = ENOBUFS;
45578064Sume		goto bad;
45678064Sume	}
45753541Sshin#endif /*IPSEC*/
45853541Sshin
45962587Sitojun	error = ip6_output(m, optp, &in6p->in6p_route, 0,
46055009Sshin			   in6p->in6p_moptions, &oifp);
46153541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
46253541Sshin		if (oifp)
46353541Sshin			icmp6_ifoutstat_inc(oifp, type, code);
46453541Sshin		icmp6stat.icp6s_outhist[type]++;
46578064Sume	} else
46678064Sume		rip6stat.rip6s_opackets++;
46753541Sshin
46853541Sshin	goto freectl;
46953541Sshin
47053541Sshin bad:
47153541Sshin	if (m)
47253541Sshin		m_freem(m);
47353541Sshin
47453541Sshin freectl:
47553541Sshin	if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
47653541Sshin		RTFREE(optp->ip6po_route.ro_rt);
47778064Sume	if (control) {
47878064Sume		if (optp == &opt)
47978064Sume			ip6_clearpktopts(optp, 0, -1);
48053541Sshin		m_freem(control);
48178064Sume	}
48253541Sshin	return(error);
48353541Sshin}
48453541Sshin
48553541Sshin/*
48653541Sshin * Raw IPv6 socket option processing.
48753541Sshin */
48853541Sshinint
48953541Sshinrip6_ctloutput(so, sopt)
49053541Sshin	struct socket *so;
49153541Sshin	struct sockopt *sopt;
49253541Sshin{
49353541Sshin	int error;
49453541Sshin
49553541Sshin	if (sopt->sopt_level == IPPROTO_ICMPV6)
49653541Sshin		/*
49753541Sshin		 * XXX: is it better to call icmp6_ctloutput() directly
49853541Sshin		 * from protosw?
49953541Sshin		 */
50053541Sshin		return(icmp6_ctloutput(so, sopt));
50153541Sshin	else if (sopt->sopt_level != IPPROTO_IPV6)
50253541Sshin		return (EINVAL);
50353541Sshin
50453541Sshin	error = 0;
50553541Sshin
50653541Sshin	switch (sopt->sopt_dir) {
50753541Sshin	case SOPT_GET:
50853541Sshin		switch (sopt->sopt_name) {
50956723Sshin		case MRT6_INIT:
51056723Sshin		case MRT6_DONE:
51156723Sshin		case MRT6_ADD_MIF:
51256723Sshin		case MRT6_DEL_MIF:
51356723Sshin		case MRT6_ADD_MFC:
51456723Sshin		case MRT6_DEL_MFC:
51556723Sshin		case MRT6_PIM:
51656723Sshin			error = ip6_mrouter_get(so, sopt);
51756723Sshin			break;
51853541Sshin		default:
51953541Sshin			error = ip6_ctloutput(so, sopt);
52053541Sshin			break;
52153541Sshin		}
52253541Sshin		break;
52353541Sshin
52453541Sshin	case SOPT_SET:
52553541Sshin		switch (sopt->sopt_name) {
52656723Sshin		case MRT6_INIT:
52756723Sshin		case MRT6_DONE:
52856723Sshin		case MRT6_ADD_MIF:
52956723Sshin		case MRT6_DEL_MIF:
53056723Sshin		case MRT6_ADD_MFC:
53156723Sshin		case MRT6_DEL_MFC:
53256723Sshin		case MRT6_PIM:
53356723Sshin			error = ip6_mrouter_set(so, sopt);
53456723Sshin			break;
53553541Sshin		default:
53653541Sshin			error = ip6_ctloutput(so, sopt);
53753541Sshin			break;
53853541Sshin		}
53953541Sshin		break;
54053541Sshin	}
54153541Sshin
54253541Sshin	return (error);
54353541Sshin}
54453541Sshin
54553541Sshinstatic int
54653541Sshinrip6_attach(struct socket *so, int proto, struct proc *p)
54753541Sshin{
54853541Sshin	struct inpcb *inp;
54953541Sshin	int error, s;
55053541Sshin
55153541Sshin	inp = sotoinpcb(so);
55253541Sshin	if (inp)
55353541Sshin		panic("rip6_attach");
55453541Sshin	if (p && (error = suser(p)) != 0)
55553541Sshin		return error;
55653541Sshin
55755009Sshin	error = soreserve(so, rip_sendspace, rip_recvspace);
55855009Sshin	if (error)
55955009Sshin		return error;
56053541Sshin	s = splnet();
56153541Sshin	error = in_pcballoc(so, &ripcbinfo, p);
56253541Sshin	splx(s);
56353541Sshin	if (error)
56453541Sshin		return error;
56553541Sshin	inp = (struct inpcb *)so->so_pcb;
56653541Sshin	inp->inp_vflag |= INP_IPV6;
56753541Sshin	inp->in6p_ip6_nxt = (long)proto;
56853541Sshin	inp->in6p_hops = -1;	/* use kernel default */
56953541Sshin	inp->in6p_cksum = -1;
57053541Sshin#ifdef IPSEC
57153541Sshin	error = ipsec_init_policy(so, &inp->in6p_sp);
57253541Sshin	if (error != 0) {
57353541Sshin		in6_pcbdetach(inp);
57453541Sshin		return (error);
57553541Sshin	}
57653541Sshin#endif /*IPSEC*/
57753541Sshin	MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
57853541Sshin	       sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
57953541Sshin	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
58053541Sshin	return 0;
58153541Sshin}
58253541Sshin
58353541Sshinstatic int
58453541Sshinrip6_detach(struct socket *so)
58553541Sshin{
58653541Sshin	struct inpcb *inp;
58753541Sshin
58853541Sshin	inp = sotoinpcb(so);
58953541Sshin	if (inp == 0)
59053541Sshin		panic("rip6_detach");
59153541Sshin	/* xxx: RSVP */
59257535Sshin	if (so == ip6_mrouter)
59357535Sshin		ip6_mrouter_done();
59453541Sshin	if (inp->in6p_icmp6filt) {
59553541Sshin		FREE(inp->in6p_icmp6filt, M_PCB);
59653541Sshin		inp->in6p_icmp6filt = NULL;
59753541Sshin	}
59853541Sshin	in6_pcbdetach(inp);
59953541Sshin	return 0;
60053541Sshin}
60153541Sshin
60253541Sshinstatic int
60353541Sshinrip6_abort(struct socket *so)
60453541Sshin{
60553541Sshin	soisdisconnected(so);
60653541Sshin	return rip6_detach(so);
60753541Sshin}
60853541Sshin
60953541Sshinstatic int
61053541Sshinrip6_disconnect(struct socket *so)
61153541Sshin{
61253541Sshin	struct inpcb *inp = sotoinpcb(so);
61353541Sshin
61453541Sshin	if ((so->so_state & SS_ISCONNECTED) == 0)
61553541Sshin		return ENOTCONN;
61653541Sshin	inp->in6p_faddr = in6addr_any;
61753541Sshin	return rip6_abort(so);
61853541Sshin}
61953541Sshin
62053541Sshinstatic int
62153541Sshinrip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
62253541Sshin{
62353541Sshin	struct inpcb *inp = sotoinpcb(so);
62453541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
62553541Sshin	struct ifaddr *ia = NULL;
62653541Sshin
62753541Sshin	if (nam->sa_len != sizeof(*addr))
62853541Sshin		return EINVAL;
62953541Sshin
63053541Sshin	if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
63153541Sshin		return EADDRNOTAVAIL;
63262587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
63362587Sitojun	if (addr->sin6_scope_id == 0) {	/* not change if specified  */
63462587Sitojun		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
63562587Sitojun	}
63662587Sitojun#endif
63753541Sshin	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
63853541Sshin	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
63953541Sshin		return EADDRNOTAVAIL;
64053541Sshin	if (ia &&
64153541Sshin	    ((struct in6_ifaddr *)ia)->ia6_flags &
64253541Sshin	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
64353541Sshin	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
64453541Sshin		return(EADDRNOTAVAIL);
64553541Sshin	}
64653541Sshin	inp->in6p_laddr = addr->sin6_addr;
64753541Sshin	return 0;
64853541Sshin}
64953541Sshin
65053541Sshinstatic int
65153541Sshinrip6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
65253541Sshin{
65353541Sshin	struct inpcb *inp = sotoinpcb(so);
65453541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
65553541Sshin	struct in6_addr *in6a = NULL;
65653541Sshin	int error = 0;
65762587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
65862587Sitojun	struct sockaddr_in6 tmp;
65962587Sitojun#endif
66053541Sshin
66153541Sshin	if (nam->sa_len != sizeof(*addr))
66253541Sshin		return EINVAL;
66353541Sshin	if (TAILQ_EMPTY(&ifnet))
66453541Sshin		return EADDRNOTAVAIL;
66553541Sshin	if (addr->sin6_family != AF_INET6)
66653541Sshin		return EAFNOSUPPORT;
66762587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
66862587Sitojun	if (addr->sin6_scope_id == 0) {	/* not change if specified  */
66962587Sitojun		/* avoid overwrites */
67062587Sitojun		tmp = *addr;
67162587Sitojun		addr = &tmp;
67262587Sitojun		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
67362587Sitojun	}
67462587Sitojun#endif
67553541Sshin	/* Source address selection. XXX: need pcblookup? */
67653541Sshin	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
67753541Sshin			     inp->in6p_moptions, &inp->in6p_route,
67853541Sshin			     &inp->in6p_laddr, &error);
67953541Sshin	if (in6a == NULL)
68053541Sshin		return (error ? error : EADDRNOTAVAIL);
68153541Sshin	inp->in6p_laddr = *in6a;
68253541Sshin	inp->in6p_faddr = addr->sin6_addr;
68353541Sshin	soisconnected(so);
68453541Sshin	return 0;
68553541Sshin}
68653541Sshin
68753541Sshinstatic int
68853541Sshinrip6_shutdown(struct socket *so)
68953541Sshin{
69053541Sshin	socantsendmore(so);
69153541Sshin	return 0;
69253541Sshin}
69353541Sshin
69453541Sshinstatic int
69553541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
69653541Sshin	 struct mbuf *control, struct proc *p)
69753541Sshin{
69853541Sshin	struct inpcb *inp = sotoinpcb(so);
69953541Sshin	struct sockaddr_in6 tmp;
70053541Sshin	struct sockaddr_in6 *dst;
70153541Sshin
70262587Sitojun	/* always copy sockaddr to avoid overwrites */
70353541Sshin	if (so->so_state & SS_ISCONNECTED) {
70453541Sshin		if (nam) {
70553541Sshin			m_freem(m);
70653541Sshin			return EISCONN;
70753541Sshin		}
70853541Sshin		/* XXX */
70953541Sshin		bzero(&tmp, sizeof(tmp));
71053541Sshin		tmp.sin6_family = AF_INET6;
71153541Sshin		tmp.sin6_len = sizeof(struct sockaddr_in6);
71253541Sshin		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
71353541Sshin		      sizeof(struct in6_addr));
71453541Sshin		dst = &tmp;
71553541Sshin	} else {
71653541Sshin		if (nam == NULL) {
71753541Sshin			m_freem(m);
71853541Sshin			return ENOTCONN;
71953541Sshin		}
72062587Sitojun		tmp = *(struct sockaddr_in6 *)nam;
72162587Sitojun		dst = &tmp;
72253541Sshin	}
72362587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
72462587Sitojun	if (dst->sin6_scope_id == 0) {	/* not change if specified  */
72562587Sitojun		dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
72662587Sitojun	}
72762587Sitojun#endif
72853541Sshin	return rip6_output(m, so, dst, control);
72953541Sshin}
73053541Sshin
73153541Sshinstruct pr_usrreqs rip6_usrreqs = {
73253541Sshin	rip6_abort, pru_accept_notsupp, rip6_attach, rip6_bind, rip6_connect,
73353541Sshin	pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect,
73453541Sshin	pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp,
73553541Sshin	pru_rcvoob_notsupp, rip6_send, pru_sense_null, rip6_shutdown,
73653541Sshin	in6_setsockaddr, sosend, soreceive, sopoll
73753541Sshin};
738