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