Home | History | Annotate | Line # | Download | only in netinet6
raw_ip6.c revision 1.96
      1 /*	$NetBSD: raw_ip6.c,v 1.96 2008/04/15 05:23:33 thorpej Exp $	*/
      2 /*	$KAME: raw_ip6.c,v 1.82 2001/07/23 18:57:56 jinmei Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1988, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  *
     61  *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: raw_ip6.c,v 1.96 2008/04/15 05:23:33 thorpej Exp $");
     66 
     67 #include "opt_ipsec.h"
     68 
     69 #include <sys/param.h>
     70 #include <sys/sysctl.h>
     71 #include <sys/malloc.h>
     72 #include <sys/mbuf.h>
     73 #include <sys/socket.h>
     74 #include <sys/protosw.h>
     75 #include <sys/socketvar.h>
     76 #include <sys/errno.h>
     77 #include <sys/systm.h>
     78 #include <sys/proc.h>
     79 #include <sys/kauth.h>
     80 #include <sys/percpu.h>
     81 
     82 #include <net/if.h>
     83 #include <net/route.h>
     84 #include <net/if_types.h>
     85 
     86 #include <netinet/in.h>
     87 #include <netinet/in_var.h>
     88 #include <netinet/ip6.h>
     89 #include <netinet6/ip6_var.h>
     90 #include <netinet6/ip6_private.h>
     91 #include <netinet6/ip6_mroute.h>
     92 #include <netinet/icmp6.h>
     93 #include <netinet6/icmp6_private.h>
     94 #include <netinet6/in6_pcb.h>
     95 #include <netinet6/nd6.h>
     96 #include <netinet6/ip6protosw.h>
     97 #include <netinet6/scope6_var.h>
     98 #include <netinet6/raw_ip6.h>
     99 
    100 #ifdef IPSEC
    101 #include <netinet6/ipsec.h>
    102 #endif /* IPSEC */
    103 
    104 #ifdef FAST_IPSEC
    105 #include <netipsec/ipsec.h>
    106 #include <netipsec/ipsec_var.h> /* XXX ipsecstat namespace */
    107 #include <netipsec/ipsec6.h>
    108 #endif
    109 
    110 #include "faith.h"
    111 #if defined(NFAITH) && 0 < NFAITH
    112 #include <net/if_faith.h>
    113 #endif
    114 
    115 extern struct inpcbtable rawcbtable;
    116 struct	inpcbtable raw6cbtable;
    117 #define ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
    118 
    119 /*
    120  * Raw interface to IP6 protocol.
    121  */
    122 
    123 static percpu_t *rip6stat_percpu;
    124 
    125 #define	RIP6_STATINC(x)							\
    126 do {									\
    127 	uint64_t *_rip6s_ = percpu_getref(rip6stat_percpu);		\
    128 	_rip6s_[x]++;							\
    129 	percpu_putref(rip6stat_percpu);					\
    130 } while (/*CONSTCOND*/0)
    131 
    132 /*
    133  * Initialize raw connection block queue.
    134  */
    135 void
    136 rip6_init()
    137 {
    138 
    139 	in6_pcbinit(&raw6cbtable, 1, 1);
    140 
    141 	rip6stat_percpu = percpu_alloc(sizeof(uint64_t) * RIP6_NSTATS);
    142 }
    143 
    144 /*
    145  * Setup generic address and protocol structures
    146  * for raw_input routine, then pass them along with
    147  * mbuf chain.
    148  */
    149 int
    150 rip6_input(struct mbuf **mp, int *offp, int proto)
    151 {
    152 	struct mbuf *m = *mp;
    153 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    154 	struct inpcb_hdr *inph;
    155 	struct in6pcb *in6p;
    156 	struct in6pcb *last = NULL;
    157 	struct sockaddr_in6 rip6src;
    158 	struct mbuf *opts = NULL;
    159 
    160 	RIP6_STATINC(RIP6_STAT_IPACKETS);
    161 
    162 #if defined(NFAITH) && 0 < NFAITH
    163 	if (faithprefix(&ip6->ip6_dst)) {
    164 		/* send icmp6 host unreach? */
    165 		m_freem(m);
    166 		return IPPROTO_DONE;
    167 	}
    168 #endif
    169 
    170 	/* Be proactive about malicious use of IPv4 mapped address */
    171 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    172 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    173 		/* XXX stat */
    174 		m_freem(m);
    175 		return IPPROTO_DONE;
    176 	}
    177 
    178 	sockaddr_in6_init(&rip6src, &ip6->ip6_src, 0, 0, 0);
    179 	if (sa6_recoverscope(&rip6src) != 0) {
    180 		/* XXX: should be impossible. */
    181 		m_freem(m);
    182 		return IPPROTO_DONE;
    183 	}
    184 
    185 	CIRCLEQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) {
    186 		in6p = (struct in6pcb *)inph;
    187 		if (in6p->in6p_af != AF_INET6)
    188 			continue;
    189 		if (in6p->in6p_ip6.ip6_nxt &&
    190 		    in6p->in6p_ip6.ip6_nxt != proto)
    191 			continue;
    192 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
    193 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
    194 			continue;
    195 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
    196 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
    197 			continue;
    198 		if (in6p->in6p_cksum != -1) {
    199 			RIP6_STATINC(RIP6_STAT_ISUM);
    200 			if (in6_cksum(m, proto, *offp,
    201 			    m->m_pkthdr.len - *offp)) {
    202 				RIP6_STATINC(RIP6_STAT_BADSUM);
    203 				continue;
    204 			}
    205 		}
    206 		if (last) {
    207 			struct	mbuf *n;
    208 
    209 #ifdef IPSEC
    210 			/*
    211 			 * Check AH/ESP integrity.
    212 			 */
    213 			if (ipsec6_in_reject(m, last)) {
    214 				ipsec6stat.in_polvio++;
    215 				/* do not inject data into pcb */
    216 			} else
    217 #endif /* IPSEC */
    218 #ifdef FAST_IPSEC
    219 			/*
    220 			 * Check AH/ESP integrity
    221 			 */
    222 			if (!ipsec6_in_reject(m,last))
    223 #endif /* FAST_IPSEC */
    224 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
    225 				if (last->in6p_flags & IN6P_CONTROLOPTS)
    226 					ip6_savecontrol(last, &opts, ip6, n);
    227 				/* strip intermediate headers */
    228 				m_adj(n, *offp);
    229 				if (sbappendaddr(&last->in6p_socket->so_rcv,
    230 				    (struct sockaddr *)&rip6src, n, opts) == 0) {
    231 					/* should notify about lost packet */
    232 					m_freem(n);
    233 					if (opts)
    234 						m_freem(opts);
    235 					RIP6_STATINC(RIP6_STAT_FULLSOCK);
    236 				} else
    237 					sorwakeup(last->in6p_socket);
    238 				opts = NULL;
    239 			}
    240 		}
    241 		last = in6p;
    242 	}
    243 #ifdef IPSEC
    244 	/*
    245 	 * Check AH/ESP integrity.
    246 	 */
    247 	if (last && ipsec6_in_reject(m, last)) {
    248 		m_freem(m);
    249 		ipsec6stat.in_polvio++;
    250 		IP6_STATDEC(IP6_STAT_DELIVERED);
    251 		/* do not inject data into pcb */
    252 	} else
    253 #endif /* IPSEC */
    254 #ifdef FAST_IPSEC
    255 	if (last && ipsec6_in_reject(m, last)) {
    256 		m_freem(m);
    257 		/*
    258 		 * XXX ipsec6_in_reject update stat if there is an error
    259 		 * so we just need to update stats by hand in the case of last is
    260 		 * NULL
    261 		 */
    262 		if (!last)
    263 			ipsec6stat.in_polvio++;
    264 			IP6_STATDEC(IP6_STAT_DELIVERED);
    265 			/* do not inject data into pcb */
    266 		} else
    267 #endif /* FAST_IPSEC */
    268 	if (last) {
    269 		if (last->in6p_flags & IN6P_CONTROLOPTS)
    270 			ip6_savecontrol(last, &opts, ip6, m);
    271 		/* strip intermediate headers */
    272 		m_adj(m, *offp);
    273 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    274 		    (struct sockaddr *)&rip6src, m, opts) == 0) {
    275 			m_freem(m);
    276 			if (opts)
    277 				m_freem(opts);
    278 			RIP6_STATINC(RIP6_STAT_FULLSOCK);
    279 		} else
    280 			sorwakeup(last->in6p_socket);
    281 	} else {
    282 		RIP6_STATINC(RIP6_STAT_NOSOCK);
    283 		if (m->m_flags & M_MCAST)
    284 			RIP6_STATINC(RIP6_STAT_NOSOCKMCAST);
    285 		if (proto == IPPROTO_NONE)
    286 			m_freem(m);
    287 		else {
    288 			u_int8_t *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
    289 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
    290 			icmp6_error(m, ICMP6_PARAM_PROB,
    291 			    ICMP6_PARAMPROB_NEXTHEADER,
    292 			    prvnxtp - mtod(m, u_int8_t *));
    293 		}
    294 		IP6_STATDEC(IP6_STAT_DELIVERED);
    295 	}
    296 	return IPPROTO_DONE;
    297 }
    298 
    299 void
    300 rip6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
    301 {
    302 	struct ip6_hdr *ip6;
    303 	struct ip6ctlparam *ip6cp = NULL;
    304 	const struct sockaddr_in6 *sa6_src = NULL;
    305 	void *cmdarg;
    306 	void (*notify)(struct in6pcb *, int) = in6_rtchange;
    307 	int nxt;
    308 
    309 	if (sa->sa_family != AF_INET6 ||
    310 	    sa->sa_len != sizeof(struct sockaddr_in6))
    311 		return;
    312 
    313 	if ((unsigned)cmd >= PRC_NCMDS)
    314 		return;
    315 	if (PRC_IS_REDIRECT(cmd))
    316 		notify = in6_rtchange, d = NULL;
    317 	else if (cmd == PRC_HOSTDEAD)
    318 		d = NULL;
    319 	else if (cmd == PRC_MSGSIZE)
    320 		; /* special code is present, see below */
    321 	else if (inet6ctlerrmap[cmd] == 0)
    322 		return;
    323 
    324 	/* if the parameter is from icmp6, decode it. */
    325 	if (d != NULL) {
    326 		ip6cp = (struct ip6ctlparam *)d;
    327 		ip6 = ip6cp->ip6c_ip6;
    328 		cmdarg = ip6cp->ip6c_cmdarg;
    329 		sa6_src = ip6cp->ip6c_src;
    330 		nxt = ip6cp->ip6c_nxt;
    331 	} else {
    332 		ip6 = NULL;
    333 		cmdarg = NULL;
    334 		sa6_src = &sa6_any;
    335 		nxt = -1;
    336 	}
    337 
    338 	if (ip6 && cmd == PRC_MSGSIZE) {
    339 		const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
    340 		int valid = 0;
    341 		struct in6pcb *in6p;
    342 
    343 		/*
    344 		 * Check to see if we have a valid raw IPv6 socket
    345 		 * corresponding to the address in the ICMPv6 message
    346 		 * payload, and the protocol (ip6_nxt) meets the socket.
    347 		 * XXX chase extension headers, or pass final nxt value
    348 		 * from icmp6_notify_error()
    349 		 */
    350 		in6p = NULL;
    351 		in6p = in6_pcblookup_connect(&raw6cbtable, &sa6->sin6_addr, 0,
    352 		    (const struct in6_addr *)&sa6_src->sin6_addr, 0, 0);
    353 #if 0
    354 		if (!in6p) {
    355 			/*
    356 			 * As the use of sendto(2) is fairly popular,
    357 			 * we may want to allow non-connected pcb too.
    358 			 * But it could be too weak against attacks...
    359 			 * We should at least check if the local
    360 			 * address (= s) is really ours.
    361 			 */
    362 			in6p = in6_pcblookup_bind(&raw6cbtable,
    363 			    &sa6->sin6_addr, 0, 0);
    364 		}
    365 #endif
    366 
    367 		if (in6p && in6p->in6p_ip6.ip6_nxt &&
    368 		    in6p->in6p_ip6.ip6_nxt == nxt)
    369 			valid++;
    370 
    371 		/*
    372 		 * Depending on the value of "valid" and routing table
    373 		 * size (mtudisc_{hi,lo}wat), we will:
    374 		 * - recalculate the new MTU and create the
    375 		 *   corresponding routing entry, or
    376 		 * - ignore the MTU change notification.
    377 		 */
    378 		icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    379 
    380 		/*
    381 		 * regardless of if we called icmp6_mtudisc_update(),
    382 		 * we need to call in6_pcbnotify(), to notify path MTU
    383 		 * change to the userland (RFC3542), because some
    384 		 * unconnected sockets may share the same destination
    385 		 * and want to know the path MTU.
    386 		 */
    387 	}
    388 
    389 	(void) in6_pcbnotify(&raw6cbtable, sa, 0,
    390 	    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
    391 }
    392 
    393 /*
    394  * Generate IPv6 header and pass packet to ip6_output.
    395  * Tack on options user may have setup with control call.
    396  */
    397 int
    398 rip6_output(struct mbuf *m, struct socket *so, struct sockaddr_in6 *dstsock,
    399     struct mbuf *control)
    400 {
    401 	struct in6_addr *dst;
    402 	struct ip6_hdr *ip6;
    403 	struct in6pcb *in6p;
    404 	u_int	plen = m->m_pkthdr.len;
    405 	int error = 0;
    406 	struct ip6_pktopts opt, *optp = NULL;
    407 	struct ifnet *oifp = NULL;
    408 	int type, code;		/* for ICMPv6 output statistics only */
    409 	int priv = 0;
    410 	int scope_ambiguous = 0;
    411 	struct in6_addr *in6a;
    412 
    413 	in6p = sotoin6pcb(so);
    414 
    415 	priv = 0;
    416 	if (curlwp && !kauth_authorize_generic(curlwp->l_cred,
    417 	    KAUTH_GENERIC_ISSUSER, NULL))
    418 		priv = 1;
    419 
    420 	dst = &dstsock->sin6_addr;
    421 	if (control) {
    422 		if ((error = ip6_setpktopts(control, &opt,
    423 		    in6p->in6p_outputopts,
    424 		    priv, so->so_proto->pr_protocol)) != 0) {
    425 			goto bad;
    426 		}
    427 		optp = &opt;
    428 	} else
    429 		optp = in6p->in6p_outputopts;
    430 
    431 	/*
    432 	 * Check and convert scope zone ID into internal form.
    433 	 * XXX: we may still need to determine the zone later.
    434 	 */
    435 	if (!(so->so_state & SS_ISCONNECTED)) {
    436 		if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
    437 			scope_ambiguous = 1;
    438 		if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
    439 			goto bad;
    440 	}
    441 
    442 	/*
    443 	 * For an ICMPv6 packet, we should know its type and code
    444 	 * to update statistics.
    445 	 */
    446 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    447 		struct icmp6_hdr *icmp6;
    448 		if (m->m_len < sizeof(struct icmp6_hdr) &&
    449 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
    450 			error = ENOBUFS;
    451 			goto bad;
    452 		}
    453 		icmp6 = mtod(m, struct icmp6_hdr *);
    454 		type = icmp6->icmp6_type;
    455 		code = icmp6->icmp6_code;
    456 	} else {
    457 		type = 0;
    458 		code = 0;
    459 	}
    460 
    461 	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
    462 	if (!m) {
    463 		error = ENOBUFS;
    464 		goto bad;
    465 	}
    466 	ip6 = mtod(m, struct ip6_hdr *);
    467 
    468 	/*
    469 	 * Next header might not be ICMP6 but use its pseudo header anyway.
    470 	 */
    471 	ip6->ip6_dst = *dst;
    472 
    473 	/*
    474 	 * Source address selection.
    475 	 */
    476 	if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions,
    477 	    (struct route *)&in6p->in6p_route, &in6p->in6p_laddr, &oifp,
    478 	    &error)) == 0) {
    479 		if (error == 0)
    480 			error = EADDRNOTAVAIL;
    481 		goto bad;
    482 	}
    483 	ip6->ip6_src = *in6a;
    484 
    485 	if (oifp && scope_ambiguous) {
    486 		/*
    487 		 * Application should provide a proper zone ID or the use of
    488 		 * default zone IDs should be enabled.  Unfortunately, some
    489 		 * applications do not behave as it should, so we need a
    490 		 * workaround.  Even if an appropriate ID is not determined
    491 		 * (when it's required), if we can determine the outgoing
    492 		 * interface. determine the zone ID based on the interface.
    493 		 */
    494 		error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
    495 		if (error != 0)
    496 			goto bad;
    497 	}
    498 	ip6->ip6_dst = dstsock->sin6_addr;
    499 
    500 	/* fill in the rest of the IPv6 header fields */
    501 	ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    502 	ip6->ip6_vfc  &= ~IPV6_VERSION_MASK;
    503 	ip6->ip6_vfc  |= IPV6_VERSION;
    504 	/* ip6_plen will be filled in ip6_output, so not fill it here. */
    505 	ip6->ip6_nxt   = in6p->in6p_ip6.ip6_nxt;
    506 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
    507 
    508 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
    509 	    in6p->in6p_cksum != -1) {
    510 		int off;
    511 		u_int16_t sum;
    512 
    513 		/* compute checksum */
    514 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
    515 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
    516 		else
    517 			off = in6p->in6p_cksum;
    518 		if (plen < off + 1) {
    519 			error = EINVAL;
    520 			goto bad;
    521 		}
    522 		off += sizeof(struct ip6_hdr);
    523 
    524 		sum = 0;
    525 		m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
    526 		    M_DONTWAIT);
    527 		if (m == NULL) {
    528 			error = ENOBUFS;
    529 			goto bad;
    530 		}
    531 		sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
    532 		m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
    533 		    M_DONTWAIT);
    534 		if (m == NULL) {
    535 			error = ENOBUFS;
    536 			goto bad;
    537 		}
    538 	}
    539 
    540 	error = ip6_output(m, optp, &in6p->in6p_route, 0,
    541 	    in6p->in6p_moptions, so, &oifp);
    542 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    543 		if (oifp)
    544 			icmp6_ifoutstat_inc(oifp, type, code);
    545 		ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
    546 	} else
    547 		RIP6_STATINC(RIP6_STAT_OPACKETS);
    548 
    549 	goto freectl;
    550 
    551  bad:
    552 	if (m)
    553 		m_freem(m);
    554 
    555  freectl:
    556 	if (control) {
    557 		ip6_clearpktopts(&opt, -1);
    558 		m_freem(control);
    559 	}
    560 	return error;
    561 }
    562 
    563 /*
    564  * Raw IPv6 socket option processing.
    565  */
    566 int
    567 rip6_ctloutput(int op, struct socket *so, int level, int optname,
    568     struct mbuf **mp)
    569 {
    570 	int error = 0;
    571 
    572 	if (level == SOL_SOCKET && optname == SO_NOHEADER) {
    573 		/* need to fiddle w/ opt(IPPROTO_IPV6, IPV6_CHECKSUM)? */
    574 		if (op == PRCO_GETOPT) {
    575 			*mp = m_intopt(so, 1);
    576 			return 0;
    577 		} else if (*mp == NULL || (*mp)->m_len != sizeof(int))
    578 			error = EINVAL;
    579 		else if (*mtod(*mp, int *) == 0)
    580 			error = EINVAL;
    581 		goto free_m;
    582 	} else if (level != IPPROTO_IPV6)
    583 		return ip6_ctloutput(op, so, level, optname, mp);
    584 
    585 	switch (optname) {
    586 	case MRT6_INIT:
    587 	case MRT6_DONE:
    588 	case MRT6_ADD_MIF:
    589 	case MRT6_DEL_MIF:
    590 	case MRT6_ADD_MFC:
    591 	case MRT6_DEL_MFC:
    592 	case MRT6_PIM:
    593 		if (op == PRCO_SETOPT)
    594 			error = ip6_mrouter_set(optname, so, *mp);
    595 		else if (op == PRCO_GETOPT)
    596 			error = ip6_mrouter_get(optname, so, mp);
    597 		else
    598 			error = EINVAL;
    599 		break;
    600 	case IPV6_CHECKSUM:
    601 		return ip6_raw_ctloutput(op, so, level, optname, mp);
    602 	default:
    603 		return ip6_ctloutput(op, so, level, optname, mp);
    604 	}
    605 free_m:
    606 	if (op == PRCO_SETOPT && *mp != NULL)
    607 		m_free(*mp);
    608 	return error;
    609 }
    610 
    611 extern	u_long rip6_sendspace;
    612 extern	u_long rip6_recvspace;
    613 
    614 int
    615 rip6_usrreq(struct socket *so, int req, struct mbuf *m,
    616 	struct mbuf *nam, struct mbuf *control, struct lwp *l)
    617 {
    618 	struct in6pcb *in6p = sotoin6pcb(so);
    619 	int s;
    620 	int error = 0;
    621 	int priv;
    622 
    623 	priv = 0;
    624 	if (l && !kauth_authorize_generic(l->l_cred,
    625 	    KAUTH_GENERIC_ISSUSER, NULL))
    626 		priv++;
    627 
    628 	if (req == PRU_CONTROL)
    629 		return in6_control(so, (u_long)m, (void *)nam,
    630 		    (struct ifnet *)control, l);
    631 
    632 	if (req == PRU_PURGEIF) {
    633 		in6_pcbpurgeif0(&raw6cbtable, (struct ifnet *)control);
    634 		in6_purgeif((struct ifnet *)control);
    635 		in6_pcbpurgeif(&raw6cbtable, (struct ifnet *)control);
    636 		return 0;
    637 	}
    638 
    639 	switch (req) {
    640 	case PRU_ATTACH:
    641 		if (in6p != NULL)
    642 			panic("rip6_attach");
    643 		if (!priv) {
    644 			error = EACCES;
    645 			break;
    646 		}
    647 		s = splsoftnet();
    648 		error = soreserve(so, rip6_sendspace, rip6_recvspace);
    649 		if (error != 0) {
    650 			splx(s);
    651 			break;
    652 		}
    653 		if ((error = in6_pcballoc(so, &raw6cbtable)) != 0) {
    654 			splx(s);
    655 			break;
    656 		}
    657 		splx(s);
    658 		in6p = sotoin6pcb(so);
    659 		in6p->in6p_ip6.ip6_nxt = (long)nam;
    660 		in6p->in6p_cksum = -1;
    661 
    662 		MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
    663 		    sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
    664 		if (in6p->in6p_icmp6filt == NULL) {
    665 			in6_pcbdetach(in6p);
    666 			error = ENOMEM;
    667 			break;
    668 		}
    669 		ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
    670 		break;
    671 
    672 	case PRU_DISCONNECT:
    673 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    674 			error = ENOTCONN;
    675 			break;
    676 		}
    677 		in6p->in6p_faddr = in6addr_any;
    678 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
    679 		break;
    680 
    681 	case PRU_ABORT:
    682 		soisdisconnected(so);
    683 		/* Fallthrough */
    684 	case PRU_DETACH:
    685 		if (in6p == NULL)
    686 			panic("rip6_detach");
    687 		if (so == ip6_mrouter)
    688 			ip6_mrouter_done();
    689 		/* xxx: RSVP */
    690 		if (in6p->in6p_icmp6filt != NULL) {
    691 			FREE(in6p->in6p_icmp6filt, M_PCB);
    692 			in6p->in6p_icmp6filt = NULL;
    693 		}
    694 		in6_pcbdetach(in6p);
    695 		break;
    696 
    697 	case PRU_BIND:
    698 	    {
    699 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    700 		struct ifaddr *ia = NULL;
    701 
    702 		if (nam->m_len != sizeof(*addr)) {
    703 			error = EINVAL;
    704 			break;
    705 		}
    706 		if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) {
    707 			error = EADDRNOTAVAIL;
    708 			break;
    709 		}
    710 		if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
    711 			break;
    712 
    713 		/*
    714 		 * we don't support mapped address here, it would confuse
    715 		 * users so reject it
    716 		 */
    717 		if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
    718 			error = EADDRNOTAVAIL;
    719 			break;
    720 		}
    721 		if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
    722 		    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
    723 			error = EADDRNOTAVAIL;
    724 			break;
    725 		}
    726 		if (ia && ((struct in6_ifaddr *)ia)->ia6_flags &
    727 		    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    728 		     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
    729 			error = EADDRNOTAVAIL;
    730 			break;
    731 		}
    732 		in6p->in6p_laddr = addr->sin6_addr;
    733 		break;
    734 	    }
    735 
    736 	case PRU_CONNECT:
    737 	{
    738 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    739 		struct in6_addr *in6a = NULL;
    740 		struct ifnet *ifp = NULL;
    741 		int scope_ambiguous = 0;
    742 
    743 		if (nam->m_len != sizeof(*addr)) {
    744 			error = EINVAL;
    745 			break;
    746 		}
    747 		if (TAILQ_EMPTY(&ifnet)) {
    748 			error = EADDRNOTAVAIL;
    749 			break;
    750 		}
    751 		if (addr->sin6_family != AF_INET6) {
    752 			error = EAFNOSUPPORT;
    753 			break;
    754 		}
    755 
    756 		/*
    757 		 * Application should provide a proper zone ID or the use of
    758 		 * default zone IDs should be enabled.  Unfortunately, some
    759 		 * applications do not behave as it should, so we need a
    760 		 * workaround.  Even if an appropriate ID is not determined,
    761 		 * we'll see if we can determine the outgoing interface.  If we
    762 		 * can, determine the zone ID based on the interface below.
    763 		 */
    764 		if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
    765 			scope_ambiguous = 1;
    766 		if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
    767 			return error;
    768 
    769 		/* Source address selection. XXX: need pcblookup? */
    770 		in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
    771 		    in6p->in6p_moptions, (struct route *)&in6p->in6p_route,
    772 		    &in6p->in6p_laddr, &ifp, &error);
    773 		if (in6a == NULL) {
    774 			if (error == 0)
    775 				error = EADDRNOTAVAIL;
    776 			break;
    777 		}
    778 		/* XXX: see above */
    779 		if (ifp && scope_ambiguous &&
    780 		    (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
    781 			break;
    782 		}
    783 		in6p->in6p_laddr = *in6a;
    784 		in6p->in6p_faddr = addr->sin6_addr;
    785 		soisconnected(so);
    786 		break;
    787 	}
    788 
    789 	case PRU_CONNECT2:
    790 		error = EOPNOTSUPP;
    791 		break;
    792 
    793 	/*
    794 	 * Mark the connection as being incapable of futther input.
    795 	 */
    796 	case PRU_SHUTDOWN:
    797 		socantsendmore(so);
    798 		break;
    799 	/*
    800 	 * Ship a packet out. The appropriate raw output
    801 	 * routine handles any messaging necessary.
    802 	 */
    803 	case PRU_SEND:
    804 	{
    805 		struct sockaddr_in6 tmp;
    806 		struct sockaddr_in6 *dst;
    807 
    808 		/* always copy sockaddr to avoid overwrites */
    809 		if (so->so_state & SS_ISCONNECTED) {
    810 			if (nam) {
    811 				error = EISCONN;
    812 				break;
    813 			}
    814 			/* XXX */
    815 			sockaddr_in6_init(&tmp, &in6p->in6p_faddr, 0, 0, 0);
    816 			dst = &tmp;
    817 		} else {
    818 			if (nam == NULL) {
    819 				error = ENOTCONN;
    820 				break;
    821 			}
    822 			if (nam->m_len != sizeof(tmp)) {
    823 				error = EINVAL;
    824 				break;
    825 			}
    826 
    827 			tmp = *mtod(nam, struct sockaddr_in6 *);
    828 			dst = &tmp;
    829 
    830 			if (dst->sin6_family != AF_INET6) {
    831 				error = EAFNOSUPPORT;
    832 				break;
    833 			}
    834 		}
    835 		error = rip6_output(m, so, dst, control);
    836 		m = NULL;
    837 		break;
    838 	}
    839 
    840 	case PRU_SENSE:
    841 		/*
    842 		 * stat: don't bother with a blocksize
    843 		 */
    844 		return 0;
    845 	/*
    846 	 * Not supported.
    847 	 */
    848 	case PRU_RCVOOB:
    849 	case PRU_RCVD:
    850 	case PRU_LISTEN:
    851 	case PRU_ACCEPT:
    852 	case PRU_SENDOOB:
    853 		error = EOPNOTSUPP;
    854 		break;
    855 
    856 	case PRU_SOCKADDR:
    857 		in6_setsockaddr(in6p, nam);
    858 		break;
    859 
    860 	case PRU_PEERADDR:
    861 		in6_setpeeraddr(in6p, nam);
    862 		break;
    863 
    864 	default:
    865 		panic("rip6_usrreq");
    866 	}
    867 	if (m != NULL)
    868 		m_freem(m);
    869 	return error;
    870 }
    871 
    872 static void
    873 rip6stat_convert_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
    874 {
    875 	uint64_t *rip6sc = v1;
    876 	uint64_t *rip6s = v2;
    877 	u_int i;
    878 
    879 	for (i = 0; i < RIP6_NSTATS; i++)
    880 		rip6s[i] += rip6sc[i];
    881 }
    882 
    883 static void
    884 rip6stat_convert_to_user(uint64_t *rip6s)
    885 {
    886 
    887 	memset(rip6s, 0, sizeof(uint64_t) * RIP6_NSTATS);
    888 	percpu_foreach(rip6stat_percpu, rip6stat_convert_to_user_cb, rip6s);
    889 }
    890 
    891 static int
    892 sysctl_net_inet6_raw6_stats(SYSCTLFN_ARGS)
    893 {
    894 	struct sysctlnode node;
    895 	uint64_t rip6s[RIP6_NSTATS];
    896 
    897 	rip6stat_convert_to_user(rip6s);
    898 	node = *rnode;
    899 	node.sysctl_data = rip6s;
    900 	node.sysctl_size = sizeof(rip6s);
    901 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    902 }
    903 
    904 SYSCTL_SETUP(sysctl_net_inet6_raw6_setup, "sysctl net.inet6.raw6 subtree setup")
    905 {
    906 
    907 	sysctl_createv(clog, 0, NULL, NULL,
    908 		       CTLFLAG_PERMANENT,
    909 		       CTLTYPE_NODE, "net", NULL,
    910 		       NULL, 0, NULL, 0,
    911 		       CTL_NET, CTL_EOL);
    912 	sysctl_createv(clog, 0, NULL, NULL,
    913 		       CTLFLAG_PERMANENT,
    914 		       CTLTYPE_NODE, "inet6", NULL,
    915 		       NULL, 0, NULL, 0,
    916 		       CTL_NET, PF_INET6, CTL_EOL);
    917 	sysctl_createv(clog, 0, NULL, NULL,
    918 		       CTLFLAG_PERMANENT,
    919 		       CTLTYPE_NODE, "raw6",
    920 		       SYSCTL_DESCR("Raw IPv6 settings"),
    921 		       NULL, 0, NULL, 0,
    922 		       CTL_NET, PF_INET6, IPPROTO_RAW, CTL_EOL);
    923 
    924 	sysctl_createv(clog, 0, NULL, NULL,
    925 		       CTLFLAG_PERMANENT,
    926 		       CTLTYPE_STRUCT, "pcblist",
    927 		       SYSCTL_DESCR("Raw IPv6 control block list"),
    928 		       sysctl_inpcblist, 0, &raw6cbtable, 0,
    929 		       CTL_NET, PF_INET6, IPPROTO_RAW,
    930 		       CTL_CREATE, CTL_EOL);
    931 	sysctl_createv(clog, 0, NULL, NULL,
    932 		       CTLFLAG_PERMANENT,
    933 		       CTLTYPE_STRUCT, "stats",
    934 		       SYSCTL_DESCR("Raw IPv6 statistics"),
    935 		       sysctl_net_inet6_raw6_stats, 0, NULL, 0,
    936 		       CTL_NET, PF_INET6, IPPROTO_RAW, RAW6CTL_STATS,
    937 		       CTL_EOL);
    938 }
    939