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