raw_ip6.c revision 180850
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
30139826Simp/*-
3153541Sshin * Copyright (c) 1982, 1986, 1988, 1993
32180305Srwatson *	The Regents of the University of California.
33180305Srwatson * All rights reserved.
3453541Sshin *
3553541Sshin * Redistribution and use in source and binary forms, with or without
3653541Sshin * modification, are permitted provided that the following conditions
3753541Sshin * are met:
3853541Sshin * 1. Redistributions of source code must retain the above copyright
3953541Sshin *    notice, this list of conditions and the following disclaimer.
4053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4153541Sshin *    notice, this list of conditions and the following disclaimer in the
4253541Sshin *    documentation and/or other materials provided with the distribution.
4353541Sshin * 4. Neither the name of the University nor the names of its contributors
4453541Sshin *    may be used to endorse or promote products derived from this software
4553541Sshin *    without specific prior written permission.
4653541Sshin *
4753541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5753541Sshin * SUCH DAMAGE.
5853541Sshin *
5953541Sshin *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
6053541Sshin */
6153541Sshin
62174510Sobrien#include <sys/cdefs.h>
63174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/raw_ip6.c 180850 2008-07-27 20:30:34Z mav $");
64174510Sobrien
6555009Sshin#include "opt_ipsec.h"
6678064Sume#include "opt_inet6.h"
6755009Sshin
6853541Sshin#include <sys/param.h>
6995759Stanimura#include <sys/errno.h>
7095759Stanimura#include <sys/lock.h>
7153541Sshin#include <sys/malloc.h>
7295759Stanimura#include <sys/mbuf.h>
73170689Srwatson#include <sys/priv.h>
7453541Sshin#include <sys/proc.h>
7595759Stanimura#include <sys/protosw.h>
7695759Stanimura#include <sys/signalvar.h>
7753541Sshin#include <sys/socket.h>
7853541Sshin#include <sys/socketvar.h>
7995759Stanimura#include <sys/sx.h>
80148385Sume#include <sys/syslog.h>
8153541Sshin
8253541Sshin#include <net/if.h>
8395759Stanimura#include <net/if_types.h>
8453541Sshin#include <net/route.h>
8553541Sshin
8653541Sshin#include <netinet/in.h>
8753541Sshin#include <netinet/in_var.h>
8853541Sshin#include <netinet/in_systm.h>
8995759Stanimura#include <netinet/icmp6.h>
9095759Stanimura#include <netinet/in_pcb.h>
9162587Sitojun#include <netinet/ip6.h>
9295759Stanimura#include <netinet6/ip6protosw.h>
9356723Sshin#include <netinet6/ip6_mroute.h>
9453541Sshin#include <netinet6/in6_pcb.h>
9595759Stanimura#include <netinet6/ip6_var.h>
9653541Sshin#include <netinet6/nd6.h>
9795759Stanimura#include <netinet6/raw_ip6.h>
9862587Sitojun#include <netinet6/scope6_var.h>
9953541Sshin
100171167Sgnn#ifdef IPSEC
101105199Ssam#include <netipsec/ipsec.h>
102105199Ssam#include <netipsec/ipsec6.h>
103171167Sgnn#endif /* IPSEC */
104105199Ssam
10553541Sshin#include <machine/stdarg.h>
10653541Sshin
10753541Sshin#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
10853541Sshin#define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
10953541Sshin
11053541Sshin/*
11153541Sshin * Raw interface to IP6 protocol.
11253541Sshin */
11353541Sshin
11453541Sshinextern struct	inpcbhead ripcb;
11553541Sshinextern struct	inpcbinfo ripcbinfo;
11653541Sshinextern u_long	rip_sendspace;
11753541Sshinextern u_long	rip_recvspace;
11853541Sshin
11978064Sumestruct rip6stat rip6stat;
12078064Sume
12153541Sshin/*
122166938Sbms * Hooks for multicast forwarding.
123166938Sbms */
124166948Sbmsstruct socket *ip6_mrouter = NULL;
125166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *);
126166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *);
127166938Sbmsint (*ip6_mrouter_done)(void);
128166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
129166938Sbmsint (*mrt6_ioctl)(int, caddr_t);
130166938Sbms
131166938Sbms/*
132180305Srwatson * Setup generic address and protocol structures for raw_input routine, then
133180305Srwatson * pass them along with mbuf chain.
13453541Sshin */
13553541Sshinint
136171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int proto)
13753541Sshin{
13853541Sshin	struct mbuf *m = *mp;
13953541Sshin	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
14053541Sshin	register struct inpcb *in6p;
14153541Sshin	struct inpcb *last = 0;
14278064Sume	struct mbuf *opts = NULL;
143121901Sume	struct sockaddr_in6 fromsa;
14453541Sshin
14578064Sume	rip6stat.rip6s_ipackets++;
14678064Sume
14783934Sbrooks	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
148180305Srwatson		/* XXX Send icmp6 host/port unreach? */
14978064Sume		m_freem(m);
150180305Srwatson		return (IPPROTO_DONE);
15153541Sshin	}
15278064Sume
153121901Sume	init_sin6(&fromsa, m); /* general init */
15453541Sshin
155132714Srwatson	INP_INFO_RLOCK(&ripcbinfo);
15653541Sshin	LIST_FOREACH(in6p, &ripcb, inp_list) {
157180850Smav		if ((in6p->in6p_vflag & INP_IPV6) == 0)
15853541Sshin			continue;
15953541Sshin		if (in6p->in6p_ip6_nxt &&
16053541Sshin		    in6p->in6p_ip6_nxt != proto)
161180850Smav			continue;
16253541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
16353541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
164180850Smav			continue;
16553541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
16653541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
167180850Smav			continue;
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++;
173180850Smav				continue;
17478064Sume			}
17553541Sshin		}
17653541Sshin		if (last) {
17753541Sshin			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
17878064Sume
179171167Sgnn#ifdef IPSEC
18078064Sume			/*
18178064Sume			 * Check AH/ESP integrity.
18278064Sume			 */
183125396Sume			if (n && ipsec6_in_reject(n, last)) {
18478064Sume				m_freem(n);
18578064Sume				ipsec6stat.in_polvio++;
186180305Srwatson				/* Do not inject data into pcb. */
187105199Ssam			} else
188171167Sgnn#endif /* IPSEC */
18953541Sshin			if (n) {
19097658Stanimura				if (last->in6p_flags & IN6P_CONTROLOPTS ||
19197658Stanimura				    last->in6p_socket->so_options & SO_TIMESTAMP)
192121674Sume					ip6_savecontrol(last, n, &opts);
19353541Sshin				/* strip intermediate headers */
19453541Sshin				m_adj(n, *offp);
19553541Sshin				if (sbappendaddr(&last->in6p_socket->so_rcv,
196121901Sume						(struct sockaddr *)&fromsa,
19753541Sshin						 n, opts) == 0) {
19853541Sshin					m_freem(n);
19953541Sshin					if (opts)
20053541Sshin						m_freem(opts);
20178064Sume					rip6stat.rip6s_fullsock++;
20297658Stanimura				} else
20353541Sshin					sorwakeup(last->in6p_socket);
20453541Sshin				opts = NULL;
20553541Sshin			}
206178377Srwatson			INP_RUNLOCK(last);
20753541Sshin		}
20853541Sshin		last = in6p;
209180850Smav		INP_RLOCK(last);
21053541Sshin	}
211180850Smav	INP_INFO_RUNLOCK(&ripcbinfo);
212171167Sgnn#ifdef IPSEC
21378064Sume	/*
21478064Sume	 * Check AH/ESP integrity.
21578064Sume	 */
216125396Sume	if (last && ipsec6_in_reject(m, last)) {
21778064Sume		m_freem(m);
21878064Sume		ipsec6stat.in_polvio++;
219105199Ssam		ip6stat.ip6s_delivered--;
220180305Srwatson		/* Do not inject data into pcb. */
221178377Srwatson		INP_RUNLOCK(last);
222105199Ssam	} else
223171167Sgnn#endif /* IPSEC */
22453541Sshin	if (last) {
22597658Stanimura		if (last->in6p_flags & IN6P_CONTROLOPTS ||
22697658Stanimura		    last->in6p_socket->so_options & SO_TIMESTAMP)
227121674Sume			ip6_savecontrol(last, m, &opts);
228180305Srwatson		/* Strip intermediate headers. */
22953541Sshin		m_adj(m, *offp);
23053541Sshin		if (sbappendaddr(&last->in6p_socket->so_rcv,
231180305Srwatson		    (struct sockaddr *)&fromsa, m, opts) == 0) {
23253541Sshin			m_freem(m);
23353541Sshin			if (opts)
23453541Sshin				m_freem(opts);
23578064Sume			rip6stat.rip6s_fullsock++;
23697658Stanimura		} else
23753541Sshin			sorwakeup(last->in6p_socket);
238178377Srwatson		INP_RUNLOCK(last);
23953541Sshin	} else {
24078064Sume		rip6stat.rip6s_nosock++;
24178064Sume		if (m->m_flags & M_MCAST)
24278064Sume			rip6stat.rip6s_nosockmcast++;
24353541Sshin		if (proto == IPPROTO_NONE)
24453541Sshin			m_freem(m);
24553541Sshin		else {
24653541Sshin			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
24753541Sshin			icmp6_error(m, ICMP6_PARAM_PROB,
248180305Srwatson			    ICMP6_PARAMPROB_NEXTHEADER,
249180305Srwatson			    prvnxtp - mtod(m, char *));
25053541Sshin		}
25153541Sshin		ip6stat.ip6s_delivered--;
25253541Sshin	}
253180305Srwatson	return (IPPROTO_DONE);
25453541Sshin}
25553541Sshin
25662587Sitojunvoid
257171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
25862587Sitojun{
25962587Sitojun	struct ip6_hdr *ip6;
26062587Sitojun	struct mbuf *m;
26162587Sitojun	int off = 0;
26278064Sume	struct ip6ctlparam *ip6cp = NULL;
26378064Sume	const struct sockaddr_in6 *sa6_src = NULL;
264125776Sume	void *cmdarg;
265175162Sobrien	struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
26662587Sitojun
26762587Sitojun	if (sa->sa_family != AF_INET6 ||
26862587Sitojun	    sa->sa_len != sizeof(struct sockaddr_in6))
26962587Sitojun		return;
27062587Sitojun
27162587Sitojun	if ((unsigned)cmd >= PRC_NCMDS)
27262587Sitojun		return;
27362587Sitojun	if (PRC_IS_REDIRECT(cmd))
27462587Sitojun		notify = in6_rtchange, d = NULL;
27562587Sitojun	else if (cmd == PRC_HOSTDEAD)
27662587Sitojun		d = NULL;
27762587Sitojun	else if (inet6ctlerrmap[cmd] == 0)
27862587Sitojun		return;
27962587Sitojun
280180305Srwatson	/*
281180305Srwatson	 * If the parameter is from icmp6, decode it.
282180305Srwatson	 */
28362587Sitojun	if (d != NULL) {
28478064Sume		ip6cp = (struct ip6ctlparam *)d;
28562587Sitojun		m = ip6cp->ip6c_m;
28662587Sitojun		ip6 = ip6cp->ip6c_ip6;
28762587Sitojun		off = ip6cp->ip6c_off;
288125776Sume		cmdarg = ip6cp->ip6c_cmdarg;
28978064Sume		sa6_src = ip6cp->ip6c_src;
29062587Sitojun	} else {
29162587Sitojun		m = NULL;
29262587Sitojun		ip6 = NULL;
293125776Sume		cmdarg = NULL;
29478064Sume		sa6_src = &sa6_any;
29562587Sitojun	}
29662587Sitojun
297133192Srwatson	(void) in6_pcbnotify(&ripcbinfo, sa, 0,
298180305Srwatson	    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
29962587Sitojun}
30062587Sitojun
30153541Sshin/*
302180305Srwatson * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
303180305Srwatson * may have setup with control call.
30453541Sshin */
30553541Sshinint
30653541Sshin#if __STDC__
30753541Sshinrip6_output(struct mbuf *m, ...)
30853541Sshin#else
30953541Sshinrip6_output(m, va_alist)
31053541Sshin	struct mbuf *m;
31153541Sshin	va_dcl
31253541Sshin#endif
31353541Sshin{
314120941Sume	struct mbuf *control;
31553541Sshin	struct socket *so;
31653541Sshin	struct sockaddr_in6 *dstsock;
31753541Sshin	struct in6_addr *dst;
31853541Sshin	struct ip6_hdr *ip6;
31953541Sshin	struct inpcb *in6p;
32053541Sshin	u_int	plen = m->m_pkthdr.len;
32153541Sshin	int error = 0;
322148247Sume	struct ip6_pktopts opt, *optp;
32353541Sshin	struct ifnet *oifp = NULL;
32453541Sshin	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
325148385Sume	int scope_ambiguous = 0;
326121472Sume	struct in6_addr *in6a;
32753541Sshin	va_list ap;
32853541Sshin
32953541Sshin	va_start(ap, m);
33053541Sshin	so = va_arg(ap, struct socket *);
33153541Sshin	dstsock = va_arg(ap, struct sockaddr_in6 *);
33253541Sshin	control = va_arg(ap, struct mbuf *);
33353541Sshin	va_end(ap);
33453541Sshin
33553541Sshin	in6p = sotoin6pcb(so);
336178285Srwatson	INP_WLOCK(in6p);
33753541Sshin
33853541Sshin	dst = &dstsock->sin6_addr;
33953541Sshin	if (control) {
340148242Sume		if ((error = ip6_setpktopts(control, &opt,
341175630Sbz		    in6p->in6p_outputopts, so->so_cred,
342175630Sbz		    so->so_proto->pr_protocol)) != 0) {
34353541Sshin			goto bad;
344121472Sume		}
345148247Sume		optp = &opt;
346148247Sume	} else
347148247Sume		optp = in6p->in6p_outputopts;
34853541Sshin
34953541Sshin	/*
350148385Sume	 * Check and convert scope zone ID into internal form.
351180305Srwatson	 *
352148385Sume	 * XXX: we may still need to determine the zone later.
353148385Sume	 */
354148385Sume	if (!(so->so_state & SS_ISCONNECTED)) {
355148385Sume		if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
356148385Sume			scope_ambiguous = 1;
357148385Sume		if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
358148385Sume			goto bad;
359148385Sume	}
360148385Sume
361148385Sume	/*
362180305Srwatson	 * For an ICMPv6 packet, we should know its type and code to update
363180305Srwatson	 * statistics.
36453541Sshin	 */
36553541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
36653541Sshin		struct icmp6_hdr *icmp6;
36753541Sshin		if (m->m_len < sizeof(struct icmp6_hdr) &&
36853541Sshin		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
36953541Sshin			error = ENOBUFS;
37053541Sshin			goto bad;
37153541Sshin		}
37253541Sshin		icmp6 = mtod(m, struct icmp6_hdr *);
37353541Sshin		type = icmp6->icmp6_type;
37453541Sshin		code = icmp6->icmp6_code;
37553541Sshin	}
37653541Sshin
377133592Srwatson	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
378133592Srwatson	if (m == NULL) {
379133592Srwatson		error = ENOBUFS;
380133592Srwatson		goto bad;
381133592Srwatson	}
38253541Sshin	ip6 = mtod(m, struct ip6_hdr *);
38353541Sshin
38453541Sshin	/*
38553541Sshin	 * Source address selection.
38653541Sshin	 */
387180371Sbz	if ((in6a = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred,
388180371Sbz	    &oifp, &error)) == NULL) {
389121472Sume		if (error == 0)
390121472Sume			error = EADDRNOTAVAIL;
391121472Sume		goto bad;
39253541Sshin	}
393121472Sume	ip6->ip6_src = *in6a;
394148385Sume
395148385Sume	if (oifp && scope_ambiguous) {
396148385Sume		/*
397148385Sume		 * Application should provide a proper zone ID or the use of
398148385Sume		 * default zone IDs should be enabled.  Unfortunately, some
399148385Sume		 * applications do not behave as it should, so we need a
400148385Sume		 * workaround.  Even if an appropriate ID is not determined
401148385Sume		 * (when it's required), if we can determine the outgoing
402148385Sume		 * interface. determine the zone ID based on the interface.
403148385Sume		 */
404148385Sume		error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
405148385Sume		if (error != 0)
406148385Sume			goto bad;
407148385Sume	}
408148385Sume	ip6->ip6_dst = dstsock->sin6_addr;
409148385Sume
410180305Srwatson	/*
411180305Srwatson	 * Fill in the rest of the IPv6 header fields.
412180305Srwatson	 */
41355009Sshin	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
414180305Srwatson	    (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
41555009Sshin	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
416180305Srwatson	    (IPV6_VERSION & IPV6_VERSION_MASK);
417180305Srwatson
418180305Srwatson	/*
419180305Srwatson	 * ip6_plen will be filled in ip6_output, so not fill it here.
420180305Srwatson	 */
42153541Sshin	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
42253541Sshin	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
42353541Sshin
42453541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
42553541Sshin	    in6p->in6p_cksum != -1) {
42653541Sshin		struct mbuf *n;
42753541Sshin		int off;
42853541Sshin		u_int16_t *p;
42953541Sshin
430180305Srwatson		/* Compute checksum. */
43153541Sshin		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
43253541Sshin			off = offsetof(struct icmp6_hdr, icmp6_cksum);
43353541Sshin		else
43453541Sshin			off = in6p->in6p_cksum;
43553541Sshin		if (plen < off + 1) {
43653541Sshin			error = EINVAL;
43753541Sshin			goto bad;
43853541Sshin		}
43953541Sshin		off += sizeof(struct ip6_hdr);
44053541Sshin
44153541Sshin		n = m;
44253541Sshin		while (n && n->m_len <= off) {
44353541Sshin			off -= n->m_len;
44453541Sshin			n = n->m_next;
44553541Sshin		}
44653541Sshin		if (!n)
44753541Sshin			goto bad;
44853541Sshin		p = (u_int16_t *)(mtod(n, caddr_t) + off);
44953541Sshin		*p = 0;
45053541Sshin		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
45153541Sshin	}
45253541Sshin
453148247Sume	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
45453541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
45553541Sshin		if (oifp)
45653541Sshin			icmp6_ifoutstat_inc(oifp, type, code);
45753541Sshin		icmp6stat.icp6s_outhist[type]++;
45878064Sume	} else
45978064Sume		rip6stat.rip6s_opackets++;
46053541Sshin
46153541Sshin	goto freectl;
46253541Sshin
46353541Sshin bad:
46453541Sshin	if (m)
46553541Sshin		m_freem(m);
46653541Sshin
46753541Sshin freectl:
46878064Sume	if (control) {
469148247Sume		ip6_clearpktopts(&opt, -1);
47053541Sshin		m_freem(control);
47178064Sume	}
472178285Srwatson	INP_WUNLOCK(in6p);
473120856Sume	return (error);
47453541Sshin}
47553541Sshin
47653541Sshin/*
47753541Sshin * Raw IPv6 socket option processing.
47853541Sshin */
47953541Sshinint
480171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt)
48153541Sshin{
48253541Sshin	int error;
48353541Sshin
48453541Sshin	if (sopt->sopt_level == IPPROTO_ICMPV6)
48553541Sshin		/*
48653541Sshin		 * XXX: is it better to call icmp6_ctloutput() directly
48753541Sshin		 * from protosw?
48853541Sshin		 */
489120856Sume		return (icmp6_ctloutput(so, sopt));
49053541Sshin	else if (sopt->sopt_level != IPPROTO_IPV6)
49153541Sshin		return (EINVAL);
49253541Sshin
49353541Sshin	error = 0;
49453541Sshin
49553541Sshin	switch (sopt->sopt_dir) {
49653541Sshin	case SOPT_GET:
49753541Sshin		switch (sopt->sopt_name) {
49856723Sshin		case MRT6_INIT:
49956723Sshin		case MRT6_DONE:
50056723Sshin		case MRT6_ADD_MIF:
50156723Sshin		case MRT6_DEL_MIF:
50256723Sshin		case MRT6_ADD_MFC:
50356723Sshin		case MRT6_DEL_MFC:
50456723Sshin		case MRT6_PIM:
505166938Sbms			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
506166938Sbms			    EOPNOTSUPP;
50756723Sshin			break;
508121578Sume		case IPV6_CHECKSUM:
509121578Sume			error = ip6_raw_ctloutput(so, sopt);
510121578Sume			break;
51153541Sshin		default:
51253541Sshin			error = ip6_ctloutput(so, sopt);
51353541Sshin			break;
51453541Sshin		}
51553541Sshin		break;
51653541Sshin
51753541Sshin	case SOPT_SET:
51853541Sshin		switch (sopt->sopt_name) {
51956723Sshin		case MRT6_INIT:
52056723Sshin		case MRT6_DONE:
52156723Sshin		case MRT6_ADD_MIF:
52256723Sshin		case MRT6_DEL_MIF:
52356723Sshin		case MRT6_ADD_MFC:
52456723Sshin		case MRT6_DEL_MFC:
52556723Sshin		case MRT6_PIM:
526166938Sbms			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
527166938Sbms			    EOPNOTSUPP;
52856723Sshin			break;
529121578Sume		case IPV6_CHECKSUM:
530121578Sume			error = ip6_raw_ctloutput(so, sopt);
531121578Sume			break;
53253541Sshin		default:
53353541Sshin			error = ip6_ctloutput(so, sopt);
53453541Sshin			break;
53553541Sshin		}
53653541Sshin		break;
53753541Sshin	}
53853541Sshin
53953541Sshin	return (error);
54053541Sshin}
54153541Sshin
54253541Sshinstatic int
54383366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td)
54453541Sshin{
54553541Sshin	struct inpcb *inp;
546144261Ssam	struct icmp6_filter *filter;
547157676Srwatson	int error;
54853541Sshin
54953541Sshin	inp = sotoinpcb(so);
550157374Srwatson	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
551180305Srwatson
552175630Sbz	error = priv_check(td, PRIV_NETINET_RAW);
553175630Sbz	if (error)
554180305Srwatson		return (error);
55555009Sshin	error = soreserve(so, rip_sendspace, rip_recvspace);
556157374Srwatson	if (error)
557180305Srwatson		return (error);
558144261Ssam	MALLOC(filter, struct icmp6_filter *,
559144261Ssam	       sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
560157374Srwatson	if (filter == NULL)
561180305Srwatson		return (ENOMEM);
562157374Srwatson	INP_INFO_WLOCK(&ripcbinfo);
563160491Sups	error = in_pcballoc(so, &ripcbinfo);
564132714Srwatson	if (error) {
565132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
566144261Ssam		FREE(filter, M_PCB);
567180305Srwatson		return (error);
568132714Srwatson	}
56953541Sshin	inp = (struct inpcb *)so->so_pcb;
570132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
57153541Sshin	inp->inp_vflag |= INP_IPV6;
57253541Sshin	inp->in6p_ip6_nxt = (long)proto;
57353541Sshin	inp->in6p_hops = -1;	/* use kernel default */
57453541Sshin	inp->in6p_cksum = -1;
575144261Ssam	inp->in6p_icmp6filt = filter;
57653541Sshin	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
577178285Srwatson	INP_WUNLOCK(inp);
578180305Srwatson	return (0);
57953541Sshin}
58053541Sshin
581157370Srwatsonstatic void
58253541Sshinrip6_detach(struct socket *so)
58353541Sshin{
58453541Sshin	struct inpcb *inp;
58553541Sshin
58653541Sshin	inp = sotoinpcb(so);
587157374Srwatson	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
588160549Srwatson
589166938Sbms	if (so == ip6_mrouter && ip6_mrouter_done)
590166938Sbms		ip6_mrouter_done();
59153541Sshin	/* xxx: RSVP */
592160549Srwatson	INP_INFO_WLOCK(&ripcbinfo);
593178285Srwatson	INP_WLOCK(inp);
59453541Sshin	if (inp->in6p_icmp6filt) {
59553541Sshin		FREE(inp->in6p_icmp6filt, M_PCB);
59653541Sshin		inp->in6p_icmp6filt = NULL;
59753541Sshin	}
59853541Sshin	in6_pcbdetach(inp);
599157374Srwatson	in6_pcbfree(inp);
600132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
60153541Sshin}
60253541Sshin
603160549Srwatson/* XXXRW: This can't ever be called. */
604157366Srwatsonstatic void
60553541Sshinrip6_abort(struct socket *so)
60653541Sshin{
607160549Srwatson	struct inpcb *inp;
608160549Srwatson
609160549Srwatson	inp = sotoinpcb(so);
610160549Srwatson	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
611160549Srwatson
61253541Sshin	soisdisconnected(so);
61353541Sshin}
61453541Sshin
615160549Srwatsonstatic void
616160549Srwatsonrip6_close(struct socket *so)
617160549Srwatson{
618160549Srwatson	struct inpcb *inp;
619160549Srwatson
620160549Srwatson	inp = sotoinpcb(so);
621160549Srwatson	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
622160549Srwatson
623160549Srwatson	soisdisconnected(so);
624160549Srwatson}
625160549Srwatson
62653541Sshinstatic int
62753541Sshinrip6_disconnect(struct socket *so)
62853541Sshin{
629180305Srwatson	struct inpcb *inp;
63053541Sshin
631180305Srwatson	inp = sotoinpcb(so);
632180305Srwatson	KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
633180305Srwatson
63497658Stanimura	if ((so->so_state & SS_ISCONNECTED) == 0)
635180305Srwatson		return (ENOTCONN);
63653541Sshin	inp->in6p_faddr = in6addr_any;
637157366Srwatson	rip6_abort(so);
638157374Srwatson	return (0);
63953541Sshin}
64053541Sshin
64153541Sshinstatic int
64283366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
64353541Sshin{
644180305Srwatson	struct inpcb *inp;
64553541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
64653541Sshin	struct ifaddr *ia = NULL;
647148385Sume	int error = 0;
64853541Sshin
649180305Srwatson	inp = sotoinpcb(so);
650157374Srwatson	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
651180305Srwatson
65253541Sshin	if (nam->sa_len != sizeof(*addr))
653180305Srwatson		return (EINVAL);
65453541Sshin	if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
655180305Srwatson		return (EADDRNOTAVAIL);
656148385Sume	if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
657180305Srwatson		return (error);
658148385Sume
65953541Sshin	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
66053541Sshin	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
661180305Srwatson		return (EADDRNOTAVAIL);
66253541Sshin	if (ia &&
66353541Sshin	    ((struct in6_ifaddr *)ia)->ia6_flags &
66453541Sshin	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
66553541Sshin	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
666120856Sume		return (EADDRNOTAVAIL);
66753541Sshin	}
668132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
669178285Srwatson	INP_WLOCK(inp);
67053541Sshin	inp->in6p_laddr = addr->sin6_addr;
671178285Srwatson	INP_WUNLOCK(inp);
672132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
673180305Srwatson	return (0);
67453541Sshin}
67553541Sshin
67653541Sshinstatic int
67783366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
67853541Sshin{
679180305Srwatson	struct inpcb *inp;
68053541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
68153541Sshin	struct in6_addr *in6a = NULL;
682148385Sume	struct ifnet *ifp = NULL;
683148385Sume	int error = 0, scope_ambiguous = 0;
68453541Sshin
685180305Srwatson	inp = sotoinpcb(so);
686157374Srwatson	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
687180305Srwatson
68853541Sshin	if (nam->sa_len != sizeof(*addr))
689180305Srwatson		return (EINVAL);
69053541Sshin	if (TAILQ_EMPTY(&ifnet))
691180305Srwatson		return (EADDRNOTAVAIL);
69253541Sshin	if (addr->sin6_family != AF_INET6)
693180305Srwatson		return (EAFNOSUPPORT);
694148385Sume
695148385Sume	/*
696180305Srwatson	 * Application should provide a proper zone ID or the use of default
697180305Srwatson	 * zone IDs should be enabled.  Unfortunately, some applications do
698180305Srwatson	 * not behave as it should, so we need a workaround.  Even if an
699180305Srwatson	 * appropriate ID is not determined, we'll see if we can determine
700180305Srwatson	 * the outgoing interface.  If we can, determine the zone ID based on
701180305Srwatson	 * the interface below.
702148385Sume	 */
703148385Sume	if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
704148385Sume		scope_ambiguous = 1;
705148385Sume	if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
706180305Srwatson		return (error);
707148385Sume
708132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
709178285Srwatson	INP_WLOCK(inp);
71053541Sshin	/* Source address selection. XXX: need pcblookup? */
71153541Sshin	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
712180371Sbz			     inp, NULL, so->so_cred,
713180371Sbz			     &ifp, &error);
714132714Srwatson	if (in6a == NULL) {
715178285Srwatson		INP_WUNLOCK(inp);
716132714Srwatson		INP_INFO_WUNLOCK(&ripcbinfo);
71753541Sshin		return (error ? error : EADDRNOTAVAIL);
718132714Srwatson	}
719148385Sume
720148385Sume	/* XXX: see above */
721148385Sume	if (ifp && scope_ambiguous &&
722148385Sume	    (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
723178285Srwatson		INP_WUNLOCK(inp);
724148385Sume		INP_INFO_WUNLOCK(&ripcbinfo);
725180305Srwatson		return (error);
726148385Sume	}
727148385Sume	inp->in6p_faddr = addr->sin6_addr;
72853541Sshin	inp->in6p_laddr = *in6a;
72953541Sshin	soisconnected(so);
730178285Srwatson	INP_WUNLOCK(inp);
731132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
732180305Srwatson	return (0);
73353541Sshin}
73453541Sshin
73553541Sshinstatic int
73653541Sshinrip6_shutdown(struct socket *so)
73753541Sshin{
738132714Srwatson	struct inpcb *inp;
739132714Srwatson
740132714Srwatson	inp = sotoinpcb(so);
741157374Srwatson	KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
742180305Srwatson
743178285Srwatson	INP_WLOCK(inp);
74453541Sshin	socantsendmore(so);
745178285Srwatson	INP_WUNLOCK(inp);
746180305Srwatson	return (0);
74753541Sshin}
74853541Sshin
74953541Sshinstatic int
75053541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
751171260Sdelphij    struct mbuf *control, struct thread *td)
75253541Sshin{
753180305Srwatson	struct inpcb *inp;
75453541Sshin	struct sockaddr_in6 tmp;
75553541Sshin	struct sockaddr_in6 *dst;
756132714Srwatson	int ret;
75753541Sshin
758180305Srwatson	inp = sotoinpcb(so);
759157374Srwatson	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
760180305Srwatson
761132714Srwatson	INP_INFO_WLOCK(&ripcbinfo);
762180305Srwatson	/* Always copy sockaddr to avoid overwrites. */
763132714Srwatson	/* Unlocked read. */
76453541Sshin	if (so->so_state & SS_ISCONNECTED) {
76553541Sshin		if (nam) {
766132714Srwatson			INP_INFO_WUNLOCK(&ripcbinfo);
76753541Sshin			m_freem(m);
768180305Srwatson			return (EISCONN);
76953541Sshin		}
77053541Sshin		/* XXX */
77153541Sshin		bzero(&tmp, sizeof(tmp));
77253541Sshin		tmp.sin6_family = AF_INET6;
77353541Sshin		tmp.sin6_len = sizeof(struct sockaddr_in6);
77453541Sshin		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
77553541Sshin		      sizeof(struct in6_addr));
77653541Sshin		dst = &tmp;
77753541Sshin	} else {
77853541Sshin		if (nam == NULL) {
779132714Srwatson			INP_INFO_WUNLOCK(&ripcbinfo);
78053541Sshin			m_freem(m);
781180305Srwatson			return (ENOTCONN);
78253541Sshin		}
783148385Sume		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
784148385Sume			INP_INFO_WUNLOCK(&ripcbinfo);
785148385Sume			m_freem(m);
786180305Srwatson			return (EINVAL);
787148385Sume		}
78862587Sitojun		tmp = *(struct sockaddr_in6 *)nam;
78962587Sitojun		dst = &tmp;
790148385Sume
791148385Sume		if (dst->sin6_family == AF_UNSPEC) {
792148385Sume			/*
793148385Sume			 * XXX: we allow this case for backward
794148385Sume			 * compatibility to buggy applications that
795148385Sume			 * rely on old (and wrong) kernel behavior.
796148385Sume			 */
797148385Sume			log(LOG_INFO, "rip6 SEND: address family is "
798148385Sume			    "unspec. Assume AF_INET6\n");
799148385Sume			dst->sin6_family = AF_INET6;
800148385Sume		} else if (dst->sin6_family != AF_INET6) {
801148385Sume			INP_INFO_WUNLOCK(&ripcbinfo);
802148385Sume			m_freem(m);
803148385Sume			return(EAFNOSUPPORT);
804148385Sume		}
80553541Sshin	}
806132714Srwatson	ret = rip6_output(m, so, dst, control);
807132714Srwatson	INP_INFO_WUNLOCK(&ripcbinfo);
808132714Srwatson	return (ret);
80953541Sshin}
81053541Sshin
81153541Sshinstruct pr_usrreqs rip6_usrreqs = {
812137386Sphk	.pru_abort =		rip6_abort,
813137386Sphk	.pru_attach =		rip6_attach,
814137386Sphk	.pru_bind =		rip6_bind,
815137386Sphk	.pru_connect =		rip6_connect,
816137386Sphk	.pru_control =		in6_control,
817137386Sphk	.pru_detach =		rip6_detach,
818137386Sphk	.pru_disconnect =	rip6_disconnect,
819169462Srwatson	.pru_peeraddr =		in6_getpeeraddr,
820137386Sphk	.pru_send =		rip6_send,
821137386Sphk	.pru_shutdown =		rip6_shutdown,
822169462Srwatson	.pru_sockaddr =		in6_getsockaddr,
823160549Srwatson	.pru_close =		rip6_close,
82453541Sshin};
825