raw_ip6.c revision 184214
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 184214 2008-10-23 20:26:15Z des $");
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>
81181803Sbz#include <sys/vimage.h>
8253541Sshin
8353541Sshin#include <net/if.h>
8495759Stanimura#include <net/if_types.h>
8553541Sshin#include <net/route.h>
8653541Sshin
8753541Sshin#include <netinet/in.h>
8853541Sshin#include <netinet/in_var.h>
8953541Sshin#include <netinet/in_systm.h>
9095759Stanimura#include <netinet/icmp6.h>
9195759Stanimura#include <netinet/in_pcb.h>
9262587Sitojun#include <netinet/ip6.h>
9395759Stanimura#include <netinet6/ip6protosw.h>
9456723Sshin#include <netinet6/ip6_mroute.h>
9553541Sshin#include <netinet6/in6_pcb.h>
9695759Stanimura#include <netinet6/ip6_var.h>
9753541Sshin#include <netinet6/nd6.h>
9895759Stanimura#include <netinet6/raw_ip6.h>
9962587Sitojun#include <netinet6/scope6_var.h>
10053541Sshin
101171167Sgnn#ifdef IPSEC
102105199Ssam#include <netipsec/ipsec.h>
103105199Ssam#include <netipsec/ipsec6.h>
104171167Sgnn#endif /* IPSEC */
105105199Ssam
10653541Sshin#include <machine/stdarg.h>
10753541Sshin
10853541Sshin#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
10953541Sshin#define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
11053541Sshin
11153541Sshin/*
11253541Sshin * Raw interface to IP6 protocol.
11353541Sshin */
11453541Sshin
11553541Sshinextern struct	inpcbhead ripcb;
11653541Sshinextern struct	inpcbinfo ripcbinfo;
11753541Sshinextern u_long	rip_sendspace;
11853541Sshinextern u_long	rip_recvspace;
11953541Sshin
12078064Sumestruct rip6stat rip6stat;
12178064Sume
12253541Sshin/*
123166938Sbms * Hooks for multicast forwarding.
124166938Sbms */
125166948Sbmsstruct socket *ip6_mrouter = NULL;
126166938Sbmsint (*ip6_mrouter_set)(struct socket *, struct sockopt *);
127166938Sbmsint (*ip6_mrouter_get)(struct socket *, struct sockopt *);
128166938Sbmsint (*ip6_mrouter_done)(void);
129166938Sbmsint (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
130166938Sbmsint (*mrt6_ioctl)(int, caddr_t);
131166938Sbms
132166938Sbms/*
133180305Srwatson * Setup generic address and protocol structures for raw_input routine, then
134180305Srwatson * pass them along with mbuf chain.
13553541Sshin */
13653541Sshinint
137171259Sdelphijrip6_input(struct mbuf **mp, int *offp, int proto)
13853541Sshin{
139183550Szec	INIT_VNET_INET(curvnet);
140183550Szec	INIT_VNET_INET6(curvnet);
141183550Szec#ifdef IPSEC
142183550Szec	INIT_VNET_IPSEC(curvnet);
143183550Szec#endif
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
151181803Sbz	V_rip6stat.rip6s_ipackets++;
15278064Sume
15383934Sbrooks	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
154180305Srwatson		/* XXX Send icmp6 host/port unreach? */
15578064Sume		m_freem(m);
156180305Srwatson		return (IPPROTO_DONE);
15753541Sshin	}
15878064Sume
159121901Sume	init_sin6(&fromsa, m); /* general init */
16053541Sshin
161181803Sbz	INP_INFO_RLOCK(&V_ripcbinfo);
162181803Sbz	LIST_FOREACH(in6p, &V_ripcb, inp_list) {
163180850Smav		if ((in6p->in6p_vflag & INP_IPV6) == 0)
16453541Sshin			continue;
16553541Sshin		if (in6p->in6p_ip6_nxt &&
16653541Sshin		    in6p->in6p_ip6_nxt != proto)
167180850Smav			continue;
16853541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
16953541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
170180850Smav			continue;
17153541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
17253541Sshin		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
173180850Smav			continue;
174180932Smav		INP_RLOCK(in6p);
17578064Sume		if (in6p->in6p_cksum != -1) {
176181803Sbz			V_rip6stat.rip6s_isum++;
177151459Ssuz			if (in6_cksum(m, proto, *offp,
17878064Sume			    m->m_pkthdr.len - *offp)) {
179180932Smav				INP_RUNLOCK(in6p);
180181803Sbz				V_rip6stat.rip6s_badsum++;
181180850Smav				continue;
18278064Sume			}
18353541Sshin		}
18453541Sshin		if (last) {
18553541Sshin			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
18678064Sume
187171167Sgnn#ifdef IPSEC
18878064Sume			/*
18978064Sume			 * Check AH/ESP integrity.
19078064Sume			 */
191125396Sume			if (n && ipsec6_in_reject(n, last)) {
19278064Sume				m_freem(n);
193181803Sbz				V_ipsec6stat.in_polvio++;
194180305Srwatson				/* Do not inject data into pcb. */
195105199Ssam			} else
196171167Sgnn#endif /* IPSEC */
19753541Sshin			if (n) {
19897658Stanimura				if (last->in6p_flags & IN6P_CONTROLOPTS ||
19997658Stanimura				    last->in6p_socket->so_options & SO_TIMESTAMP)
200121674Sume					ip6_savecontrol(last, n, &opts);
20153541Sshin				/* strip intermediate headers */
20253541Sshin				m_adj(n, *offp);
20353541Sshin				if (sbappendaddr(&last->in6p_socket->so_rcv,
204121901Sume						(struct sockaddr *)&fromsa,
20553541Sshin						 n, opts) == 0) {
20653541Sshin					m_freem(n);
20753541Sshin					if (opts)
20853541Sshin						m_freem(opts);
209181803Sbz					V_rip6stat.rip6s_fullsock++;
21097658Stanimura				} else
21153541Sshin					sorwakeup(last->in6p_socket);
21253541Sshin				opts = NULL;
21353541Sshin			}
214178377Srwatson			INP_RUNLOCK(last);
21553541Sshin		}
21653541Sshin		last = in6p;
21753541Sshin	}
218181803Sbz	INP_INFO_RUNLOCK(&V_ripcbinfo);
219171167Sgnn#ifdef IPSEC
22078064Sume	/*
22178064Sume	 * Check AH/ESP integrity.
22278064Sume	 */
223125396Sume	if (last && ipsec6_in_reject(m, last)) {
22478064Sume		m_freem(m);
225181803Sbz		V_ipsec6stat.in_polvio++;
226181803Sbz		V_ip6stat.ip6s_delivered--;
227180305Srwatson		/* Do not inject data into pcb. */
228178377Srwatson		INP_RUNLOCK(last);
229105199Ssam	} else
230171167Sgnn#endif /* IPSEC */
23153541Sshin	if (last) {
23297658Stanimura		if (last->in6p_flags & IN6P_CONTROLOPTS ||
23397658Stanimura		    last->in6p_socket->so_options & SO_TIMESTAMP)
234121674Sume			ip6_savecontrol(last, m, &opts);
235180305Srwatson		/* Strip intermediate headers. */
23653541Sshin		m_adj(m, *offp);
23753541Sshin		if (sbappendaddr(&last->in6p_socket->so_rcv,
238180305Srwatson		    (struct sockaddr *)&fromsa, m, opts) == 0) {
23953541Sshin			m_freem(m);
24053541Sshin			if (opts)
24153541Sshin				m_freem(opts);
242181803Sbz			V_rip6stat.rip6s_fullsock++;
24397658Stanimura		} else
24453541Sshin			sorwakeup(last->in6p_socket);
245178377Srwatson		INP_RUNLOCK(last);
24653541Sshin	} else {
247181803Sbz		V_rip6stat.rip6s_nosock++;
24878064Sume		if (m->m_flags & M_MCAST)
249181803Sbz			V_rip6stat.rip6s_nosockmcast++;
25053541Sshin		if (proto == IPPROTO_NONE)
25153541Sshin			m_freem(m);
25253541Sshin		else {
25353541Sshin			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
25453541Sshin			icmp6_error(m, ICMP6_PARAM_PROB,
255180305Srwatson			    ICMP6_PARAMPROB_NEXTHEADER,
256180305Srwatson			    prvnxtp - mtod(m, char *));
25753541Sshin		}
258181803Sbz		V_ip6stat.ip6s_delivered--;
25953541Sshin	}
260180305Srwatson	return (IPPROTO_DONE);
26153541Sshin}
26253541Sshin
26362587Sitojunvoid
264171259Sdelphijrip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
26562587Sitojun{
266183550Szec	INIT_VNET_INET(curvnet);
26762587Sitojun	struct ip6_hdr *ip6;
26862587Sitojun	struct mbuf *m;
26962587Sitojun	int off = 0;
27078064Sume	struct ip6ctlparam *ip6cp = NULL;
27178064Sume	const struct sockaddr_in6 *sa6_src = NULL;
272125776Sume	void *cmdarg;
273175162Sobrien	struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
27462587Sitojun
27562587Sitojun	if (sa->sa_family != AF_INET6 ||
27662587Sitojun	    sa->sa_len != sizeof(struct sockaddr_in6))
27762587Sitojun		return;
27862587Sitojun
27962587Sitojun	if ((unsigned)cmd >= PRC_NCMDS)
28062587Sitojun		return;
28162587Sitojun	if (PRC_IS_REDIRECT(cmd))
28262587Sitojun		notify = in6_rtchange, d = NULL;
28362587Sitojun	else if (cmd == PRC_HOSTDEAD)
28462587Sitojun		d = NULL;
28562587Sitojun	else if (inet6ctlerrmap[cmd] == 0)
28662587Sitojun		return;
28762587Sitojun
288180305Srwatson	/*
289180305Srwatson	 * If the parameter is from icmp6, decode it.
290180305Srwatson	 */
29162587Sitojun	if (d != NULL) {
29278064Sume		ip6cp = (struct ip6ctlparam *)d;
29362587Sitojun		m = ip6cp->ip6c_m;
29462587Sitojun		ip6 = ip6cp->ip6c_ip6;
29562587Sitojun		off = ip6cp->ip6c_off;
296125776Sume		cmdarg = ip6cp->ip6c_cmdarg;
29778064Sume		sa6_src = ip6cp->ip6c_src;
29862587Sitojun	} else {
29962587Sitojun		m = NULL;
30062587Sitojun		ip6 = NULL;
301125776Sume		cmdarg = NULL;
30278064Sume		sa6_src = &sa6_any;
30362587Sitojun	}
30462587Sitojun
305181803Sbz	(void) in6_pcbnotify(&V_ripcbinfo, sa, 0,
306180305Srwatson	    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
30762587Sitojun}
30862587Sitojun
30953541Sshin/*
310180305Srwatson * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
311180305Srwatson * may have setup with control call.
31253541Sshin */
31353541Sshinint
31453541Sshin#if __STDC__
31553541Sshinrip6_output(struct mbuf *m, ...)
31653541Sshin#else
31753541Sshinrip6_output(m, va_alist)
31853541Sshin	struct mbuf *m;
31953541Sshin	va_dcl
32053541Sshin#endif
32153541Sshin{
322183550Szec	INIT_VNET_INET6(curvnet);
323120941Sume	struct mbuf *control;
32453541Sshin	struct socket *so;
32553541Sshin	struct sockaddr_in6 *dstsock;
32653541Sshin	struct in6_addr *dst;
32753541Sshin	struct ip6_hdr *ip6;
32853541Sshin	struct inpcb *in6p;
32953541Sshin	u_int	plen = m->m_pkthdr.len;
33053541Sshin	int error = 0;
331148247Sume	struct ip6_pktopts opt, *optp;
33253541Sshin	struct ifnet *oifp = NULL;
33353541Sshin	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
334148385Sume	int scope_ambiguous = 0;
335121472Sume	struct in6_addr *in6a;
33653541Sshin	va_list ap;
33753541Sshin
33853541Sshin	va_start(ap, m);
33953541Sshin	so = va_arg(ap, struct socket *);
34053541Sshin	dstsock = va_arg(ap, struct sockaddr_in6 *);
34153541Sshin	control = va_arg(ap, struct mbuf *);
34253541Sshin	va_end(ap);
34353541Sshin
34453541Sshin	in6p = sotoin6pcb(so);
345178285Srwatson	INP_WLOCK(in6p);
34653541Sshin
34753541Sshin	dst = &dstsock->sin6_addr;
34853541Sshin	if (control) {
349148242Sume		if ((error = ip6_setpktopts(control, &opt,
350175630Sbz		    in6p->in6p_outputopts, so->so_cred,
351175630Sbz		    so->so_proto->pr_protocol)) != 0) {
35253541Sshin			goto bad;
353121472Sume		}
354148247Sume		optp = &opt;
355148247Sume	} else
356148247Sume		optp = in6p->in6p_outputopts;
35753541Sshin
35853541Sshin	/*
359148385Sume	 * Check and convert scope zone ID into internal form.
360180305Srwatson	 *
361148385Sume	 * XXX: we may still need to determine the zone later.
362148385Sume	 */
363148385Sume	if (!(so->so_state & SS_ISCONNECTED)) {
364181803Sbz		if (dstsock->sin6_scope_id == 0 && !V_ip6_use_defzone)
365148385Sume			scope_ambiguous = 1;
366181803Sbz		if ((error = sa6_embedscope(dstsock, V_ip6_use_defzone)) != 0)
367148385Sume			goto bad;
368148385Sume	}
369148385Sume
370148385Sume	/*
371180305Srwatson	 * For an ICMPv6 packet, we should know its type and code to update
372180305Srwatson	 * statistics.
37353541Sshin	 */
37453541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
37553541Sshin		struct icmp6_hdr *icmp6;
37653541Sshin		if (m->m_len < sizeof(struct icmp6_hdr) &&
37753541Sshin		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
37853541Sshin			error = ENOBUFS;
37953541Sshin			goto bad;
38053541Sshin		}
38153541Sshin		icmp6 = mtod(m, struct icmp6_hdr *);
38253541Sshin		type = icmp6->icmp6_type;
38353541Sshin		code = icmp6->icmp6_code;
38453541Sshin	}
38553541Sshin
386133592Srwatson	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
387133592Srwatson	if (m == NULL) {
388133592Srwatson		error = ENOBUFS;
389133592Srwatson		goto bad;
390133592Srwatson	}
39153541Sshin	ip6 = mtod(m, struct ip6_hdr *);
39253541Sshin
39353541Sshin	/*
39453541Sshin	 * Source address selection.
39553541Sshin	 */
396180371Sbz	if ((in6a = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred,
397180371Sbz	    &oifp, &error)) == NULL) {
398121472Sume		if (error == 0)
399121472Sume			error = EADDRNOTAVAIL;
400121472Sume		goto bad;
40153541Sshin	}
402121472Sume	ip6->ip6_src = *in6a;
403148385Sume
404148385Sume	if (oifp && scope_ambiguous) {
405148385Sume		/*
406148385Sume		 * Application should provide a proper zone ID or the use of
407148385Sume		 * default zone IDs should be enabled.  Unfortunately, some
408148385Sume		 * applications do not behave as it should, so we need a
409148385Sume		 * workaround.  Even if an appropriate ID is not determined
410148385Sume		 * (when it's required), if we can determine the outgoing
411148385Sume		 * interface. determine the zone ID based on the interface.
412148385Sume		 */
413148385Sume		error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
414148385Sume		if (error != 0)
415148385Sume			goto bad;
416148385Sume	}
417148385Sume	ip6->ip6_dst = dstsock->sin6_addr;
418148385Sume
419180305Srwatson	/*
420180305Srwatson	 * Fill in the rest of the IPv6 header fields.
421180305Srwatson	 */
42255009Sshin	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
423180305Srwatson	    (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
42455009Sshin	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
425180305Srwatson	    (IPV6_VERSION & IPV6_VERSION_MASK);
426180305Srwatson
427180305Srwatson	/*
428180305Srwatson	 * ip6_plen will be filled in ip6_output, so not fill it here.
429180305Srwatson	 */
43053541Sshin	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
43153541Sshin	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
43253541Sshin
43353541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
43453541Sshin	    in6p->in6p_cksum != -1) {
43553541Sshin		struct mbuf *n;
43653541Sshin		int off;
43753541Sshin		u_int16_t *p;
43853541Sshin
439180305Srwatson		/* Compute checksum. */
44053541Sshin		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
44153541Sshin			off = offsetof(struct icmp6_hdr, icmp6_cksum);
44253541Sshin		else
44353541Sshin			off = in6p->in6p_cksum;
44453541Sshin		if (plen < off + 1) {
44553541Sshin			error = EINVAL;
44653541Sshin			goto bad;
44753541Sshin		}
44853541Sshin		off += sizeof(struct ip6_hdr);
44953541Sshin
45053541Sshin		n = m;
45153541Sshin		while (n && n->m_len <= off) {
45253541Sshin			off -= n->m_len;
45353541Sshin			n = n->m_next;
45453541Sshin		}
45553541Sshin		if (!n)
45653541Sshin			goto bad;
45753541Sshin		p = (u_int16_t *)(mtod(n, caddr_t) + off);
45853541Sshin		*p = 0;
45953541Sshin		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
46053541Sshin	}
46153541Sshin
462148247Sume	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
46353541Sshin	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
46453541Sshin		if (oifp)
46553541Sshin			icmp6_ifoutstat_inc(oifp, type, code);
466181803Sbz		V_icmp6stat.icp6s_outhist[type]++;
46778064Sume	} else
468181803Sbz		V_rip6stat.rip6s_opackets++;
46953541Sshin
47053541Sshin	goto freectl;
47153541Sshin
47253541Sshin bad:
47353541Sshin	if (m)
47453541Sshin		m_freem(m);
47553541Sshin
47653541Sshin freectl:
47778064Sume	if (control) {
478148247Sume		ip6_clearpktopts(&opt, -1);
47953541Sshin		m_freem(control);
48078064Sume	}
481178285Srwatson	INP_WUNLOCK(in6p);
482120856Sume	return (error);
48353541Sshin}
48453541Sshin
48553541Sshin/*
48653541Sshin * Raw IPv6 socket option processing.
48753541Sshin */
48853541Sshinint
489171259Sdelphijrip6_ctloutput(struct socket *so, struct sockopt *sopt)
49053541Sshin{
49153541Sshin	int error;
49253541Sshin
49353541Sshin	if (sopt->sopt_level == IPPROTO_ICMPV6)
49453541Sshin		/*
49553541Sshin		 * XXX: is it better to call icmp6_ctloutput() directly
49653541Sshin		 * from protosw?
49753541Sshin		 */
498120856Sume		return (icmp6_ctloutput(so, sopt));
49953541Sshin	else if (sopt->sopt_level != IPPROTO_IPV6)
50053541Sshin		return (EINVAL);
50153541Sshin
50253541Sshin	error = 0;
50353541Sshin
50453541Sshin	switch (sopt->sopt_dir) {
50553541Sshin	case SOPT_GET:
50653541Sshin		switch (sopt->sopt_name) {
50756723Sshin		case MRT6_INIT:
50856723Sshin		case MRT6_DONE:
50956723Sshin		case MRT6_ADD_MIF:
51056723Sshin		case MRT6_DEL_MIF:
51156723Sshin		case MRT6_ADD_MFC:
51256723Sshin		case MRT6_DEL_MFC:
51356723Sshin		case MRT6_PIM:
514166938Sbms			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
515166938Sbms			    EOPNOTSUPP;
51656723Sshin			break;
517121578Sume		case IPV6_CHECKSUM:
518121578Sume			error = ip6_raw_ctloutput(so, sopt);
519121578Sume			break;
52053541Sshin		default:
52153541Sshin			error = ip6_ctloutput(so, sopt);
52253541Sshin			break;
52353541Sshin		}
52453541Sshin		break;
52553541Sshin
52653541Sshin	case SOPT_SET:
52753541Sshin		switch (sopt->sopt_name) {
52856723Sshin		case MRT6_INIT:
52956723Sshin		case MRT6_DONE:
53056723Sshin		case MRT6_ADD_MIF:
53156723Sshin		case MRT6_DEL_MIF:
53256723Sshin		case MRT6_ADD_MFC:
53356723Sshin		case MRT6_DEL_MFC:
53456723Sshin		case MRT6_PIM:
535166938Sbms			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
536166938Sbms			    EOPNOTSUPP;
53756723Sshin			break;
538121578Sume		case IPV6_CHECKSUM:
539121578Sume			error = ip6_raw_ctloutput(so, sopt);
540121578Sume			break;
54153541Sshin		default:
54253541Sshin			error = ip6_ctloutput(so, sopt);
54353541Sshin			break;
54453541Sshin		}
54553541Sshin		break;
54653541Sshin	}
54753541Sshin
54853541Sshin	return (error);
54953541Sshin}
55053541Sshin
55153541Sshinstatic int
55283366Sjulianrip6_attach(struct socket *so, int proto, struct thread *td)
55353541Sshin{
554183550Szec	INIT_VNET_INET(so->so_vnet);
55553541Sshin	struct inpcb *inp;
556144261Ssam	struct icmp6_filter *filter;
557157676Srwatson	int error;
55853541Sshin
55953541Sshin	inp = sotoinpcb(so);
560157374Srwatson	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
561180305Srwatson
562175630Sbz	error = priv_check(td, PRIV_NETINET_RAW);
563175630Sbz	if (error)
564180305Srwatson		return (error);
56555009Sshin	error = soreserve(so, rip_sendspace, rip_recvspace);
566157374Srwatson	if (error)
567180305Srwatson		return (error);
568184214Sdes	filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
569157374Srwatson	if (filter == NULL)
570180305Srwatson		return (ENOMEM);
571181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
572181803Sbz	error = in_pcballoc(so, &V_ripcbinfo);
573132714Srwatson	if (error) {
574181803Sbz		INP_INFO_WUNLOCK(&V_ripcbinfo);
575184205Sdes		free(filter, M_PCB);
576180305Srwatson		return (error);
577132714Srwatson	}
57853541Sshin	inp = (struct inpcb *)so->so_pcb;
579181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
58053541Sshin	inp->inp_vflag |= INP_IPV6;
58153541Sshin	inp->in6p_ip6_nxt = (long)proto;
58253541Sshin	inp->in6p_hops = -1;	/* use kernel default */
58353541Sshin	inp->in6p_cksum = -1;
584144261Ssam	inp->in6p_icmp6filt = filter;
58553541Sshin	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
586178285Srwatson	INP_WUNLOCK(inp);
587180305Srwatson	return (0);
58853541Sshin}
58953541Sshin
590157370Srwatsonstatic void
59153541Sshinrip6_detach(struct socket *so)
59253541Sshin{
593183550Szec	INIT_VNET_INET(so->so_vnet);
59453541Sshin	struct inpcb *inp;
59553541Sshin
59653541Sshin	inp = sotoinpcb(so);
597157374Srwatson	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
598160549Srwatson
599166938Sbms	if (so == ip6_mrouter && ip6_mrouter_done)
600166938Sbms		ip6_mrouter_done();
60153541Sshin	/* xxx: RSVP */
602181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
603178285Srwatson	INP_WLOCK(inp);
604184205Sdes	free(inp->in6p_icmp6filt, M_PCB);
60553541Sshin	in6_pcbdetach(inp);
606157374Srwatson	in6_pcbfree(inp);
607181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
60853541Sshin}
60953541Sshin
610160549Srwatson/* XXXRW: This can't ever be called. */
611157366Srwatsonstatic void
61253541Sshinrip6_abort(struct socket *so)
61353541Sshin{
614160549Srwatson	struct inpcb *inp;
615160549Srwatson
616160549Srwatson	inp = sotoinpcb(so);
617160549Srwatson	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
618160549Srwatson
61953541Sshin	soisdisconnected(so);
62053541Sshin}
62153541Sshin
622160549Srwatsonstatic void
623160549Srwatsonrip6_close(struct socket *so)
624160549Srwatson{
625160549Srwatson	struct inpcb *inp;
626160549Srwatson
627160549Srwatson	inp = sotoinpcb(so);
628160549Srwatson	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
629160549Srwatson
630160549Srwatson	soisdisconnected(so);
631160549Srwatson}
632160549Srwatson
63353541Sshinstatic int
63453541Sshinrip6_disconnect(struct socket *so)
63553541Sshin{
636180305Srwatson	struct inpcb *inp;
63753541Sshin
638180305Srwatson	inp = sotoinpcb(so);
639180305Srwatson	KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
640180305Srwatson
64197658Stanimura	if ((so->so_state & SS_ISCONNECTED) == 0)
642180305Srwatson		return (ENOTCONN);
64353541Sshin	inp->in6p_faddr = in6addr_any;
644157366Srwatson	rip6_abort(so);
645157374Srwatson	return (0);
64653541Sshin}
64753541Sshin
64853541Sshinstatic int
64983366Sjulianrip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
65053541Sshin{
651183550Szec	INIT_VNET_NET(so->so_vnet);
652183550Szec	INIT_VNET_INET(so->so_vnet);
653183550Szec	INIT_VNET_INET6(so->so_vnet);
654180305Srwatson	struct inpcb *inp;
65553541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
65653541Sshin	struct ifaddr *ia = NULL;
657148385Sume	int error = 0;
65853541Sshin
659180305Srwatson	inp = sotoinpcb(so);
660157374Srwatson	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
661180305Srwatson
66253541Sshin	if (nam->sa_len != sizeof(*addr))
663180305Srwatson		return (EINVAL);
664181803Sbz	if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
665180305Srwatson		return (EADDRNOTAVAIL);
666181803Sbz	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
667180305Srwatson		return (error);
668148385Sume
66953541Sshin	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
67053541Sshin	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
671180305Srwatson		return (EADDRNOTAVAIL);
67253541Sshin	if (ia &&
67353541Sshin	    ((struct in6_ifaddr *)ia)->ia6_flags &
67453541Sshin	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
67553541Sshin	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
676120856Sume		return (EADDRNOTAVAIL);
67753541Sshin	}
678181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
679178285Srwatson	INP_WLOCK(inp);
68053541Sshin	inp->in6p_laddr = addr->sin6_addr;
681178285Srwatson	INP_WUNLOCK(inp);
682181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
683180305Srwatson	return (0);
68453541Sshin}
68553541Sshin
68653541Sshinstatic int
68783366Sjulianrip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
68853541Sshin{
689183550Szec	INIT_VNET_NET(so->so_vnet);
690183550Szec	INIT_VNET_INET(so->so_vnet);
691183550Szec	INIT_VNET_INET6(so->so_vnet);
692180305Srwatson	struct inpcb *inp;
69353541Sshin	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
69453541Sshin	struct in6_addr *in6a = NULL;
695148385Sume	struct ifnet *ifp = NULL;
696148385Sume	int error = 0, scope_ambiguous = 0;
69753541Sshin
698180305Srwatson	inp = sotoinpcb(so);
699157374Srwatson	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
700180305Srwatson
70153541Sshin	if (nam->sa_len != sizeof(*addr))
702180305Srwatson		return (EINVAL);
703181803Sbz	if (TAILQ_EMPTY(&V_ifnet))
704180305Srwatson		return (EADDRNOTAVAIL);
70553541Sshin	if (addr->sin6_family != AF_INET6)
706180305Srwatson		return (EAFNOSUPPORT);
707148385Sume
708148385Sume	/*
709180305Srwatson	 * Application should provide a proper zone ID or the use of default
710180305Srwatson	 * zone IDs should be enabled.  Unfortunately, some applications do
711180305Srwatson	 * not behave as it should, so we need a workaround.  Even if an
712180305Srwatson	 * appropriate ID is not determined, we'll see if we can determine
713180305Srwatson	 * the outgoing interface.  If we can, determine the zone ID based on
714180305Srwatson	 * the interface below.
715148385Sume	 */
716181803Sbz	if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
717148385Sume		scope_ambiguous = 1;
718181803Sbz	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
719180305Srwatson		return (error);
720148385Sume
721181803Sbz	INP_INFO_WLOCK(&V_ripcbinfo);
722178285Srwatson	INP_WLOCK(inp);
72353541Sshin	/* Source address selection. XXX: need pcblookup? */
72453541Sshin	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
725180371Sbz			     inp, NULL, so->so_cred,
726180371Sbz			     &ifp, &error);
727132714Srwatson	if (in6a == NULL) {
728178285Srwatson		INP_WUNLOCK(inp);
729181803Sbz		INP_INFO_WUNLOCK(&V_ripcbinfo);
73053541Sshin		return (error ? error : EADDRNOTAVAIL);
731132714Srwatson	}
732148385Sume
733148385Sume	/* XXX: see above */
734148385Sume	if (ifp && scope_ambiguous &&
735148385Sume	    (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
736178285Srwatson		INP_WUNLOCK(inp);
737181803Sbz		INP_INFO_WUNLOCK(&V_ripcbinfo);
738180305Srwatson		return (error);
739148385Sume	}
740148385Sume	inp->in6p_faddr = addr->sin6_addr;
74153541Sshin	inp->in6p_laddr = *in6a;
74253541Sshin	soisconnected(so);
743178285Srwatson	INP_WUNLOCK(inp);
744181803Sbz	INP_INFO_WUNLOCK(&V_ripcbinfo);
745180305Srwatson	return (0);
74653541Sshin}
74753541Sshin
74853541Sshinstatic int
74953541Sshinrip6_shutdown(struct socket *so)
75053541Sshin{
751132714Srwatson	struct inpcb *inp;
752132714Srwatson
753132714Srwatson	inp = sotoinpcb(so);
754157374Srwatson	KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
755180305Srwatson
756178285Srwatson	INP_WLOCK(inp);
75753541Sshin	socantsendmore(so);
758178285Srwatson	INP_WUNLOCK(inp);
759180305Srwatson	return (0);
76053541Sshin}
76153541Sshin
76253541Sshinstatic int
76353541Sshinrip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
764171260Sdelphij    struct mbuf *control, struct thread *td)
76553541Sshin{
766183550Szec	INIT_VNET_INET(so->so_vnet);
767180305Srwatson	struct inpcb *inp;
76853541Sshin	struct sockaddr_in6 tmp;
76953541Sshin	struct sockaddr_in6 *dst;
770132714Srwatson	int ret;
77153541Sshin
772180305Srwatson	inp = sotoinpcb(so);
773157374Srwatson	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
774180305Srwatson
775180305Srwatson	/* Always copy sockaddr to avoid overwrites. */
776132714Srwatson	/* Unlocked read. */
77753541Sshin	if (so->so_state & SS_ISCONNECTED) {
77853541Sshin		if (nam) {
77953541Sshin			m_freem(m);
780180305Srwatson			return (EISCONN);
78153541Sshin		}
78253541Sshin		/* XXX */
78353541Sshin		bzero(&tmp, sizeof(tmp));
78453541Sshin		tmp.sin6_family = AF_INET6;
78553541Sshin		tmp.sin6_len = sizeof(struct sockaddr_in6);
786180990Srwatson		INP_RLOCK(inp);
78753541Sshin		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
788180990Srwatson		    sizeof(struct in6_addr));
789180990Srwatson		INP_RUNLOCK(inp);
79053541Sshin		dst = &tmp;
79153541Sshin	} else {
79253541Sshin		if (nam == NULL) {
79353541Sshin			m_freem(m);
794180305Srwatson			return (ENOTCONN);
79553541Sshin		}
796148385Sume		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
797148385Sume			m_freem(m);
798180305Srwatson			return (EINVAL);
799148385Sume		}
80062587Sitojun		tmp = *(struct sockaddr_in6 *)nam;
80162587Sitojun		dst = &tmp;
802148385Sume
803148385Sume		if (dst->sin6_family == AF_UNSPEC) {
804148385Sume			/*
805148385Sume			 * XXX: we allow this case for backward
806148385Sume			 * compatibility to buggy applications that
807148385Sume			 * rely on old (and wrong) kernel behavior.
808148385Sume			 */
809148385Sume			log(LOG_INFO, "rip6 SEND: address family is "
810148385Sume			    "unspec. Assume AF_INET6\n");
811148385Sume			dst->sin6_family = AF_INET6;
812148385Sume		} else if (dst->sin6_family != AF_INET6) {
813148385Sume			m_freem(m);
814148385Sume			return(EAFNOSUPPORT);
815148385Sume		}
81653541Sshin	}
817132714Srwatson	ret = rip6_output(m, so, dst, control);
818132714Srwatson	return (ret);
81953541Sshin}
82053541Sshin
82153541Sshinstruct pr_usrreqs rip6_usrreqs = {
822137386Sphk	.pru_abort =		rip6_abort,
823137386Sphk	.pru_attach =		rip6_attach,
824137386Sphk	.pru_bind =		rip6_bind,
825137386Sphk	.pru_connect =		rip6_connect,
826137386Sphk	.pru_control =		in6_control,
827137386Sphk	.pru_detach =		rip6_detach,
828137386Sphk	.pru_disconnect =	rip6_disconnect,
829169462Srwatson	.pru_peeraddr =		in6_getpeeraddr,
830137386Sphk	.pru_send =		rip6_send,
831137386Sphk	.pru_shutdown =		rip6_shutdown,
832169462Srwatson	.pru_sockaddr =		in6_getsockaddr,
833160549Srwatson	.pru_close =		rip6_close,
83453541Sshin};
835