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