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