raw_ip6.c revision 62587
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 62587 2000-07-04 16:35:15Z itojun $
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"
6855009Sshin
6953541Sshin#include <stddef.h>
7053541Sshin
7153541Sshin#include <sys/param.h>
7253541Sshin#include <sys/malloc.h>
7353541Sshin#include <sys/proc.h>
7453541Sshin#include <sys/mbuf.h>
7553541Sshin#include <sys/socket.h>
7653541Sshin#include <sys/protosw.h>
7753541Sshin#include <sys/socketvar.h>
7853541Sshin#include <sys/errno.h>
7953541Sshin#include <sys/systm.h>
8053541Sshin
8153541Sshin#include <net/if.h>
8253541Sshin#include <net/route.h>
8353541Sshin#include <net/if_types.h>
8453541Sshin
8553541Sshin#include <netinet/in.h>
8653541Sshin#include <netinet/in_var.h>
8753541Sshin#include <netinet/in_systm.h>
8862587Sitojun#include <netinet/ip6.h>
8953541Sshin#include <netinet6/ip6_var.h>
9056723Sshin#include <netinet6/ip6_mroute.h>
9162587Sitojun#include <netinet/icmp6.h>
9253541Sshin#include <netinet/in_pcb.h>
9353541Sshin#include <netinet6/in6_pcb.h>
9453541Sshin#include <netinet6/nd6.h>
9562587Sitojun#include <netinet6/ip6protosw.h>
9662587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
9762587Sitojun#include <netinet6/scope6_var.h>
9862587Sitojun#endif
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"
10853541Sshin
10953541Sshin#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
11053541Sshin#define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
11153541Sshin
11253541Sshin/*
11353541Sshin * Raw interface to IP6 protocol.
11453541Sshin */
11553541Sshin
11653541Sshinextern struct	inpcbhead ripcb;
11753541Sshinextern struct	inpcbinfo ripcbinfo;
11853541Sshinextern u_long	rip_sendspace;
11953541Sshinextern u_long	rip_recvspace;
12053541Sshin
12153541Sshin/*
12253541Sshin * Setup generic address and protocol structures
12353541Sshin * for raw_input routine, then pass them along with
12453541Sshin * mbuf chain.
12553541Sshin */
12653541Sshinint
12753541Sshinrip6_input(mp, offp, proto)
12853541Sshin	struct	mbuf **mp;
12953541Sshin	int	*offp, proto;
13053541Sshin{
13153541Sshin	struct mbuf *m = *mp;
13253541Sshin	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
13353541Sshin	register struct inpcb *in6p;
13453541Sshin	struct inpcb *last = 0;
13553541Sshin	struct mbuf *opts = 0;
13653541Sshin	struct sockaddr_in6 rip6src;
13753541Sshin
13853541Sshin#if defined(NFAITH) && 0 < NFAITH
13953541Sshin	if (m->m_pkthdr.rcvif) {
14053541Sshin		if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
14153541Sshin			/* XXX send icmp6 host/port unreach? */
14253541Sshin			m_freem(m);
14353541Sshin			return IPPROTO_DONE;
14453541Sshin		}
14553541Sshin	}
14653541Sshin#endif
14753541Sshin	init_sin6(&rip6src, m); /* general init */
14853541Sshin
14953541Sshin	LIST_FOREACH(in6p, &ripcb, inp_list) {
15055009Sshin		if ((in6p->in6p_vflag & INP_IPV6) == 0)
15153541Sshin			continue;
15253541Sshin		if (in6p->in6p_ip6_nxt &&
15353541Sshin		    in6p->in6p_ip6_nxt != proto)
15453541Sshin			continue;
15553541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
15653541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
15753541Sshin			continue;
15853541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
15953541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
16053541Sshin			continue;
16153541Sshin		if (in6p->in6p_cksum != -1
16253541Sshin		    && in6_cksum(m, ip6->ip6_nxt, *offp,
16353541Sshin				 m->m_pkthdr.len - *offp)) {
16453541Sshin			/* XXX bark something */
16553541Sshin			continue;
16653541Sshin		}
16753541Sshin		if (last) {
16853541Sshin			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
16953541Sshin			if (n) {
17053541Sshin				if (last->in6p_flags & IN6P_CONTROLOPTS ||
17153541Sshin				    last->in6p_socket->so_options & SO_TIMESTAMP)
17253541Sshin					ip6_savecontrol(last, &opts, ip6, n);
17353541Sshin				/* strip intermediate headers */
17453541Sshin				m_adj(n, *offp);
17553541Sshin				if (sbappendaddr(&last->in6p_socket->so_rcv,
17653541Sshin						(struct sockaddr *)&rip6src,
17753541Sshin						 n, opts) == 0) {
17853541Sshin					/* should notify about lost packet */
17953541Sshin					m_freem(n);
18053541Sshin					if (opts)
18153541Sshin						m_freem(opts);
18253541Sshin				} else
18353541Sshin					sorwakeup(last->in6p_socket);
18453541Sshin				opts = NULL;
18553541Sshin			}
18653541Sshin		}
18753541Sshin		last = in6p;
18853541Sshin	}
18953541Sshin	if (last) {
19053541Sshin		if (last->in6p_flags & IN6P_CONTROLOPTS ||
19153541Sshin		    last->in6p_socket->so_options & SO_TIMESTAMP)
19253541Sshin			ip6_savecontrol(last, &opts, ip6, m);
19353541Sshin		/* strip intermediate headers */
19453541Sshin		m_adj(m, *offp);
19553541Sshin		if (sbappendaddr(&last->in6p_socket->so_rcv,
19653541Sshin				(struct sockaddr *)&rip6src, m, opts) == 0) {
19753541Sshin			m_freem(m);
19853541Sshin			if (opts)
19953541Sshin				m_freem(opts);
20053541Sshin		} else
20153541Sshin			sorwakeup(last->in6p_socket);
20253541Sshin	} else {
20353541Sshin		if (proto == IPPROTO_NONE)
20453541Sshin			m_freem(m);
20553541Sshin		else {
20653541Sshin			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
20753541Sshin			icmp6_error(m, ICMP6_PARAM_PROB,
20853541Sshin				    ICMP6_PARAMPROB_NEXTHEADER,
20953541Sshin				    prvnxtp - mtod(m, char *));
21053541Sshin		}
21153541Sshin		ip6stat.ip6s_delivered--;
21253541Sshin	}
21353541Sshin	return IPPROTO_DONE;
21453541Sshin}
21553541Sshin
21662587Sitojunvoid
21762587Sitojunrip6_ctlinput(cmd, sa, d)
21862587Sitojun	int cmd;
21962587Sitojun	struct sockaddr *sa;
22062587Sitojun	void *d;
22162587Sitojun{
22262587Sitojun	struct sockaddr_in6 sa6;
22362587Sitojun	struct ip6_hdr *ip6;
22462587Sitojun	struct mbuf *m;
22562587Sitojun	int off = 0;
22662587Sitojun	void (*notify) __P((struct inpcb *, int)) = in6_rtchange;
22762587Sitojun
22862587Sitojun	if (sa->sa_family != AF_INET6 ||
22962587Sitojun	    sa->sa_len != sizeof(struct sockaddr_in6))
23062587Sitojun		return;
23162587Sitojun
23262587Sitojun	if ((unsigned)cmd >= PRC_NCMDS)
23362587Sitojun		return;
23462587Sitojun	if (PRC_IS_REDIRECT(cmd))
23562587Sitojun		notify = in6_rtchange, d = NULL;
23662587Sitojun	else if (cmd == PRC_HOSTDEAD)
23762587Sitojun		d = NULL;
23862587Sitojun	else if (inet6ctlerrmap[cmd] == 0)
23962587Sitojun		return;
24062587Sitojun
24162587Sitojun	/* if the parameter is from icmp6, decode it. */
24262587Sitojun	if (d != NULL) {
24362587Sitojun		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
24462587Sitojun		m = ip6cp->ip6c_m;
24562587Sitojun		ip6 = ip6cp->ip6c_ip6;
24662587Sitojun		off = ip6cp->ip6c_off;
24762587Sitojun	} else {
24862587Sitojun		m = NULL;
24962587Sitojun		ip6 = NULL;
25062587Sitojun	}
25162587Sitojun
25262587Sitojun	/* translate addresses into internal form */
25362587Sitojun	sa6 = *(struct sockaddr_in6 *)sa;
25462587Sitojun	if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) && m && m->m_pkthdr.rcvif)
25562587Sitojun		sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
25662587Sitojun
25762587Sitojun	if (ip6) {
25862587Sitojun		/*
25962587Sitojun		 * XXX: We assume that when IPV6 is non NULL,
26062587Sitojun		 * M and OFF are valid.
26162587Sitojun		 */
26262587Sitojun		struct in6_addr s;
26362587Sitojun
26462587Sitojun		/* translate addresses into internal form */
26562587Sitojun		memcpy(&s, &ip6->ip6_src, sizeof(s));
26662587Sitojun		if (IN6_IS_ADDR_LINKLOCAL(&s))
26762587Sitojun			s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
26862587Sitojun
26962587Sitojun		(void) in6_pcbnotify(&ripcb, (struct sockaddr *)&sa6,
27062587Sitojun				     0, &s, 0, cmd, notify);
27162587Sitojun	} else
27262587Sitojun		(void) in6_pcbnotify(&ripcb, (struct sockaddr *)&sa6, 0,
27362587Sitojun				     &zeroin6_addr, 0, cmd, notify);
27462587Sitojun}
27562587Sitojun
27653541Sshin/*
27753541Sshin * Generate IPv6 header and pass packet to ip6_output.
27853541Sshin * Tack on options user may have setup with control call.
27953541Sshin */
28053541Sshinint
28153541Sshin#if __STDC__
28253541Sshinrip6_output(struct mbuf *m, ...)
28353541Sshin#else
28453541Sshinrip6_output(m, va_alist)
28553541Sshin	struct mbuf *m;
28653541Sshin	va_dcl
28753541Sshin#endif
28853541Sshin{
28953541Sshin	struct socket *so;
29053541Sshin	struct sockaddr_in6 *dstsock;
29153541Sshin	struct mbuf *control;
29253541Sshin	struct in6_addr *dst;
29353541Sshin	struct ip6_hdr *ip6;
29453541Sshin	struct inpcb *in6p;
29553541Sshin	u_int	plen = m->m_pkthdr.len;
29653541Sshin	int error = 0;
29753541Sshin	struct ip6_pktopts opt, *optp = 0;
29853541Sshin	struct ifnet *oifp = NULL;
29953541Sshin	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
30053541Sshin	int priv = 0;
30153541Sshin	va_list ap;
30253541Sshin
30353541Sshin	va_start(ap, m);
30453541Sshin	so = va_arg(ap, struct socket *);
30553541Sshin	dstsock = va_arg(ap, struct sockaddr_in6 *);
30653541Sshin	control = va_arg(ap, struct mbuf *);
30753541Sshin	va_end(ap);
30853541Sshin
30953541Sshin	in6p = sotoin6pcb(so);
31053541Sshin
31153541Sshin	priv = 0;
31253541Sshin	if (so->so_cred->cr_uid == 0)
31353541Sshin		priv = 1;
31453541Sshin	dst = &dstsock->sin6_addr;
31553541Sshin	if (control) {
31653541Sshin		if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
31753541Sshin			goto bad;
31853541Sshin		optp = &opt;
31953541Sshin	} else
32053541Sshin		optp = in6p->in6p_outputopts;
32153541Sshin
32253541Sshin	/*
32353541Sshin	 * For an ICMPv6 packet, we should know its type and code
32453541Sshin	 * to update statistics.
32553541Sshin	 */
32653541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
32753541Sshin		struct icmp6_hdr *icmp6;
32853541Sshin		if (m->m_len < sizeof(struct icmp6_hdr) &&
32953541Sshin		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
33053541Sshin			error = ENOBUFS;
33153541Sshin			goto bad;
33253541Sshin		}
33353541Sshin		icmp6 = mtod(m, struct icmp6_hdr *);
33453541Sshin		type = icmp6->icmp6_type;
33553541Sshin		code = icmp6->icmp6_code;
33653541Sshin	}
33753541Sshin
33853541Sshin	M_PREPEND(m, sizeof(*ip6), M_WAIT);
33953541Sshin	ip6 = mtod(m, struct ip6_hdr *);
34053541Sshin
34153541Sshin	/*
34253541Sshin	 * Next header might not be ICMP6 but use its pseudo header anyway.
34353541Sshin	 */
34453541Sshin	ip6->ip6_dst = *dst;
34553541Sshin
34653541Sshin	/*
34753541Sshin	 * If the scope of the destination is link-local, embed the interface
34853541Sshin	 * index in the address.
34953541Sshin	 *
35053541Sshin	 * XXX advanced-api value overrides sin6_scope_id
35153541Sshin	 */
35253541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
35353541Sshin		struct in6_pktinfo *pi;
35453541Sshin
35553541Sshin		/*
35653541Sshin		 * XXX Boundary check is assumed to be already done in
35753541Sshin		 * ip6_setpktoptions().
35853541Sshin		 */
35953541Sshin		if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
36053541Sshin			ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
36153541Sshin			oifp = ifindex2ifnet[pi->ipi6_ifindex];
36253541Sshin		} else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
36353541Sshin			 in6p->in6p_moptions &&
36453541Sshin			 in6p->in6p_moptions->im6o_multicast_ifp) {
36553541Sshin			oifp = in6p->in6p_moptions->im6o_multicast_ifp;
36653541Sshin			ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
36753541Sshin		} else if (dstsock->sin6_scope_id) {
36853541Sshin			/* boundary check */
36953541Sshin			if (dstsock->sin6_scope_id < 0
37053541Sshin			 || if_index < dstsock->sin6_scope_id) {
37153541Sshin				error = ENXIO;  /* XXX EINVAL? */
37253541Sshin				goto bad;
37353541Sshin			}
37453541Sshin			ip6->ip6_dst.s6_addr16[1]
37553541Sshin				= htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
37653541Sshin		}
37753541Sshin	}
37853541Sshin
37953541Sshin	/*
38053541Sshin	 * Source address selection.
38153541Sshin	 */
38253541Sshin	{
38353541Sshin		struct in6_addr *in6a;
38453541Sshin
38553541Sshin		if ((in6a = in6_selectsrc(dstsock, optp,
38653541Sshin					  in6p->in6p_moptions,
38753541Sshin					  &in6p->in6p_route,
38853541Sshin					  &in6p->in6p_laddr,
38953541Sshin					  &error)) == 0) {
39053541Sshin			if (error == 0)
39153541Sshin				error = EADDRNOTAVAIL;
39253541Sshin			goto bad;
39353541Sshin		}
39453541Sshin		ip6->ip6_src = *in6a;
39553541Sshin		if (in6p->in6p_route.ro_rt)
39653541Sshin			oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
39753541Sshin	}
39855009Sshin	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
39955009Sshin		(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
40055009Sshin	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
40155009Sshin		(IPV6_VERSION & IPV6_VERSION_MASK);
40253541Sshin	/* ip6_plen will be filled in ip6_output, so not fill it here. */
40353541Sshin	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
40453541Sshin	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
40553541Sshin
40653541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
40753541Sshin	    in6p->in6p_cksum != -1) {
40853541Sshin		struct mbuf *n;
40953541Sshin		int off;
41053541Sshin		u_int16_t *p;
41153541Sshin
41253541Sshin#define	offsetof(type, member)	((size_t)(&((type *)0)->member)) /* XXX */
41353541Sshin
41453541Sshin		/* compute checksum */
41553541Sshin		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
41653541Sshin			off = offsetof(struct icmp6_hdr, icmp6_cksum);
41753541Sshin		else
41853541Sshin			off = in6p->in6p_cksum;
41953541Sshin		if (plen < off + 1) {
42053541Sshin			error = EINVAL;
42153541Sshin			goto bad;
42253541Sshin		}
42353541Sshin		off += sizeof(struct ip6_hdr);
42453541Sshin
42553541Sshin		n = m;
42653541Sshin		while (n && n->m_len <= off) {
42753541Sshin			off -= n->m_len;
42853541Sshin			n = n->m_next;
42953541Sshin		}
43053541Sshin		if (!n)
43153541Sshin			goto bad;
43253541Sshin		p = (u_int16_t *)(mtod(n, caddr_t) + off);
43353541Sshin		*p = 0;
43453541Sshin		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
43553541Sshin	}
43653541Sshin
43753541Sshin#ifdef IPSEC
43862587Sitojun	ipsec_setsocket(m, so);
43953541Sshin#endif /*IPSEC*/
44053541Sshin
44162587Sitojun	error = ip6_output(m, optp, &in6p->in6p_route, 0,
44255009Sshin			   in6p->in6p_moptions, &oifp);
44353541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
44453541Sshin		if (oifp)
44553541Sshin			icmp6_ifoutstat_inc(oifp, type, code);
44653541Sshin		icmp6stat.icp6s_outhist[type]++;
44753541Sshin	}
44853541Sshin
44953541Sshin	goto freectl;
45053541Sshin
45153541Sshin bad:
45253541Sshin	if (m)
45353541Sshin		m_freem(m);
45453541Sshin
45553541Sshin freectl:
45653541Sshin	if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
45753541Sshin		RTFREE(optp->ip6po_route.ro_rt);
45853541Sshin	if (control)
45953541Sshin		m_freem(control);
46053541Sshin	return(error);
46153541Sshin}
46253541Sshin
46353541Sshin/*
46453541Sshin * Raw IPv6 socket option processing.
46553541Sshin */
46653541Sshinint
46753541Sshinrip6_ctloutput(so, sopt)
46853541Sshin	struct socket *so;
46953541Sshin	struct sockopt *sopt;
47053541Sshin{
47153541Sshin	int error;
47253541Sshin
47353541Sshin	if (sopt->sopt_level == IPPROTO_ICMPV6)
47453541Sshin		/*
47553541Sshin		 * XXX: is it better to call icmp6_ctloutput() directly
47653541Sshin		 * from protosw?
47753541Sshin		 */
47853541Sshin		return(icmp6_ctloutput(so, sopt));
47953541Sshin	else if (sopt->sopt_level != IPPROTO_IPV6)
48053541Sshin		return (EINVAL);
48153541Sshin
48253541Sshin	error = 0;
48353541Sshin
48453541Sshin	switch (sopt->sopt_dir) {
48553541Sshin	case SOPT_GET:
48653541Sshin		switch (sopt->sopt_name) {
48756723Sshin		case MRT6_INIT:
48856723Sshin		case MRT6_DONE:
48956723Sshin		case MRT6_ADD_MIF:
49056723Sshin		case MRT6_DEL_MIF:
49156723Sshin		case MRT6_ADD_MFC:
49256723Sshin		case MRT6_DEL_MFC:
49356723Sshin		case MRT6_PIM:
49456723Sshin			error = ip6_mrouter_get(so, sopt);
49556723Sshin			break;
49653541Sshin		default:
49753541Sshin			error = ip6_ctloutput(so, sopt);
49853541Sshin			break;
49953541Sshin		}
50053541Sshin		break;
50153541Sshin
50253541Sshin	case SOPT_SET:
50353541Sshin		switch (sopt->sopt_name) {
50456723Sshin		case MRT6_INIT:
50556723Sshin		case MRT6_DONE:
50656723Sshin		case MRT6_ADD_MIF:
50756723Sshin		case MRT6_DEL_MIF:
50856723Sshin		case MRT6_ADD_MFC:
50956723Sshin		case MRT6_DEL_MFC:
51056723Sshin		case MRT6_PIM:
51156723Sshin			error = ip6_mrouter_set(so, sopt);
51256723Sshin			break;
51353541Sshin		default:
51453541Sshin			error = ip6_ctloutput(so, sopt);
51553541Sshin			break;
51653541Sshin		}
51753541Sshin		break;
51853541Sshin	}
51953541Sshin
52053541Sshin	return (error);
52153541Sshin}
52253541Sshin
52353541Sshinstatic int
52453541Sshinrip6_attach(struct socket *so, int proto, struct proc *p)
52553541Sshin{
52653541Sshin	struct inpcb *inp;
52753541Sshin	int error, s;
52853541Sshin
52953541Sshin	inp = sotoinpcb(so);
53053541Sshin	if (inp)
53153541Sshin		panic("rip6_attach");
53253541Sshin	if (p && (error = suser(p)) != 0)
53353541Sshin		return error;
53453541Sshin
53555009Sshin	error = soreserve(so, rip_sendspace, rip_recvspace);
53655009Sshin	if (error)
53755009Sshin		return error;
53853541Sshin	s = splnet();
53953541Sshin	error = in_pcballoc(so, &ripcbinfo, p);
54053541Sshin	splx(s);
54153541Sshin	if (error)
54253541Sshin		return error;
54353541Sshin	inp = (struct inpcb *)so->so_pcb;
54453541Sshin	inp->inp_vflag |= INP_IPV6;
54553541Sshin	inp->in6p_ip6_nxt = (long)proto;
54653541Sshin	inp->in6p_hops = -1;	/* use kernel default */
54753541Sshin	inp->in6p_cksum = -1;
54853541Sshin#ifdef IPSEC
54953541Sshin	error = ipsec_init_policy(so, &inp->in6p_sp);
55053541Sshin	if (error != 0) {
55153541Sshin		in6_pcbdetach(inp);
55253541Sshin		return (error);
55353541Sshin	}
55453541Sshin#endif /*IPSEC*/
55553541Sshin	MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
55653541Sshin	       sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
55753541Sshin	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
55853541Sshin	return 0;
55953541Sshin}
56053541Sshin
56153541Sshinstatic int
56253541Sshinrip6_detach(struct socket *so)
56353541Sshin{
56453541Sshin	struct inpcb *inp;
56553541Sshin
56653541Sshin	inp = sotoinpcb(so);
56753541Sshin	if (inp == 0)
56853541Sshin		panic("rip6_detach");
56953541Sshin	/* xxx: RSVP */
57057535Sshin	if (so == ip6_mrouter)
57157535Sshin		ip6_mrouter_done();
57253541Sshin	if (inp->in6p_icmp6filt) {
57353541Sshin		FREE(inp->in6p_icmp6filt, M_PCB);
57453541Sshin		inp->in6p_icmp6filt = NULL;
57553541Sshin	}
57653541Sshin	in6_pcbdetach(inp);
57753541Sshin	return 0;
57853541Sshin}
57953541Sshin
58053541Sshinstatic int
58153541Sshinrip6_abort(struct socket *so)
58253541Sshin{
58353541Sshin	soisdisconnected(so);
58453541Sshin	return rip6_detach(so);
58553541Sshin}
58653541Sshin
58753541Sshinstatic int
58853541Sshinrip6_disconnect(struct socket *so)
58953541Sshin{
59053541Sshin	struct inpcb *inp = sotoinpcb(so);
59153541Sshin
59253541Sshin	if ((so->so_state & SS_ISCONNECTED) == 0)
59353541Sshin		return ENOTCONN;
59453541Sshin	inp->in6p_faddr = in6addr_any;
59553541Sshin	return rip6_abort(so);
59653541Sshin}
59753541Sshin
59853541Sshinstatic int
59953541Sshinrip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
60053541Sshin{
60153541Sshin	struct inpcb *inp = sotoinpcb(so);
60253541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
60353541Sshin	struct ifaddr *ia = NULL;
60453541Sshin
60553541Sshin	if (nam->sa_len != sizeof(*addr))
60653541Sshin		return EINVAL;
60753541Sshin
60853541Sshin	if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
60953541Sshin		return EADDRNOTAVAIL;
61062587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
61162587Sitojun	if (addr->sin6_scope_id == 0) {	/* not change if specified  */
61262587Sitojun		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
61362587Sitojun	}
61462587Sitojun#endif
61553541Sshin	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
61653541Sshin	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
61753541Sshin		return EADDRNOTAVAIL;
61853541Sshin	if (ia &&
61953541Sshin	    ((struct in6_ifaddr *)ia)->ia6_flags &
62053541Sshin	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
62153541Sshin	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
62253541Sshin		return(EADDRNOTAVAIL);
62353541Sshin	}
62453541Sshin	inp->in6p_laddr = addr->sin6_addr;
62553541Sshin	return 0;
62653541Sshin}
62753541Sshin
62853541Sshinstatic int
62953541Sshinrip6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
63053541Sshin{
63153541Sshin	struct inpcb *inp = sotoinpcb(so);
63253541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
63353541Sshin	struct in6_addr *in6a = NULL;
63453541Sshin	int error = 0;
63562587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
63662587Sitojun	struct sockaddr_in6 tmp;
63762587Sitojun#endif
63853541Sshin
63953541Sshin	if (nam->sa_len != sizeof(*addr))
64053541Sshin		return EINVAL;
64153541Sshin	if (TAILQ_EMPTY(&ifnet))
64253541Sshin		return EADDRNOTAVAIL;
64353541Sshin	if (addr->sin6_family != AF_INET6)
64453541Sshin		return EAFNOSUPPORT;
64562587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
64662587Sitojun	if (addr->sin6_scope_id == 0) {	/* not change if specified  */
64762587Sitojun		/* avoid overwrites */
64862587Sitojun		tmp = *addr;
64962587Sitojun		addr = &tmp;
65062587Sitojun		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
65162587Sitojun	}
65262587Sitojun#endif
65353541Sshin	/* Source address selection. XXX: need pcblookup? */
65453541Sshin	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
65553541Sshin			     inp->in6p_moptions, &inp->in6p_route,
65653541Sshin			     &inp->in6p_laddr, &error);
65753541Sshin	if (in6a == NULL)
65853541Sshin		return (error ? error : EADDRNOTAVAIL);
65953541Sshin	inp->in6p_laddr = *in6a;
66053541Sshin	inp->in6p_faddr = addr->sin6_addr;
66153541Sshin	soisconnected(so);
66253541Sshin	return 0;
66353541Sshin}
66453541Sshin
66553541Sshinstatic int
66653541Sshinrip6_shutdown(struct socket *so)
66753541Sshin{
66853541Sshin	socantsendmore(so);
66953541Sshin	return 0;
67053541Sshin}
67153541Sshin
67253541Sshinstatic int
67353541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
67453541Sshin	 struct mbuf *control, struct proc *p)
67553541Sshin{
67653541Sshin	struct inpcb *inp = sotoinpcb(so);
67753541Sshin	struct sockaddr_in6 tmp;
67853541Sshin	struct sockaddr_in6 *dst;
67953541Sshin
68062587Sitojun	/* always copy sockaddr to avoid overwrites */
68153541Sshin	if (so->so_state & SS_ISCONNECTED) {
68253541Sshin		if (nam) {
68353541Sshin			m_freem(m);
68453541Sshin			return EISCONN;
68553541Sshin		}
68653541Sshin		/* XXX */
68753541Sshin		bzero(&tmp, sizeof(tmp));
68853541Sshin		tmp.sin6_family = AF_INET6;
68953541Sshin		tmp.sin6_len = sizeof(struct sockaddr_in6);
69053541Sshin		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
69153541Sshin		      sizeof(struct in6_addr));
69253541Sshin		dst = &tmp;
69353541Sshin	} else {
69453541Sshin		if (nam == NULL) {
69553541Sshin			m_freem(m);
69653541Sshin			return ENOTCONN;
69753541Sshin		}
69862587Sitojun		tmp = *(struct sockaddr_in6 *)nam;
69962587Sitojun		dst = &tmp;
70053541Sshin	}
70162587Sitojun#ifdef ENABLE_DEFAULT_SCOPE
70262587Sitojun	if (dst->sin6_scope_id == 0) {	/* not change if specified  */
70362587Sitojun		dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
70462587Sitojun	}
70562587Sitojun#endif
70653541Sshin	return rip6_output(m, so, dst, control);
70753541Sshin}
70853541Sshin
70953541Sshinstruct pr_usrreqs rip6_usrreqs = {
71053541Sshin	rip6_abort, pru_accept_notsupp, rip6_attach, rip6_bind, rip6_connect,
71153541Sshin	pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect,
71253541Sshin	pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp,
71353541Sshin	pru_rcvoob_notsupp, rip6_send, pru_sense_null, rip6_shutdown,
71453541Sshin	in6_setsockaddr, sosend, soreceive, sopoll
71553541Sshin};
716