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