Home | History | Annotate | Line # | Download | only in netinet6
raw_ip6.c revision 1.31.2.6
      1 /*	$NetBSD: raw_ip6.c,v 1.31.2.6 2001/11/14 19:18:15 nathanw 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. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by the University of
     48  *	California, Berkeley and its contributors.
     49  * 4. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: raw_ip6.c,v 1.31.2.6 2001/11/14 19:18:15 nathanw Exp $");
     70 
     71 #include "opt_ipsec.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/malloc.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/socket.h>
     77 #include <sys/protosw.h>
     78 #include <sys/socketvar.h>
     79 #include <sys/errno.h>
     80 #include <sys/systm.h>
     81 #include <sys/lwp.h>
     82 #include <sys/proc.h>
     83 
     84 #include <net/if.h>
     85 #include <net/route.h>
     86 #include <net/if_types.h>
     87 
     88 #include <netinet/in.h>
     89 #include <netinet/in_var.h>
     90 #include <netinet/ip6.h>
     91 #include <netinet6/ip6_var.h>
     92 #include <netinet6/ip6_mroute.h>
     93 #include <netinet/icmp6.h>
     94 #include <netinet6/in6_pcb.h>
     95 #include <netinet6/nd6.h>
     96 #include <netinet6/ip6protosw.h>
     97 #ifdef ENABLE_DEFAULT_SCOPE
     98 #include <netinet6/scope6_var.h>
     99 #endif
    100 #include <netinet6/raw_ip6.h>
    101 
    102 #ifdef IPSEC
    103 #include <netinet6/ipsec.h>
    104 #endif /* IPSEC */
    105 
    106 #include <machine/stdarg.h>
    107 
    108 #include "faith.h"
    109 #if defined(NFAITH) && 0 < NFAITH
    110 #include <net/if_faith.h>
    111 #endif
    112 
    113 struct	in6pcb rawin6pcb;
    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 	rawin6pcb.in6p_next = rawin6pcb.in6p_prev = &rawin6pcb;
    129 }
    130 
    131 /*
    132  * Setup generic address and protocol structures
    133  * for raw_input routine, then pass them along with
    134  * mbuf chain.
    135  */
    136 int
    137 rip6_input(mp, offp, proto)
    138 	struct	mbuf **mp;
    139 	int	*offp, proto;
    140 {
    141 	struct mbuf *m = *mp;
    142 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    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 	bzero(&rip6src, sizeof(rip6src));
    167 	rip6src.sin6_len = sizeof(struct sockaddr_in6);
    168 	rip6src.sin6_family = AF_INET6;
    169 #if 0 /* XXX inbound flowlabel */
    170 	rip6src.sin6_flowinfo = ip6->ip6_flow & IPV6_FLOWINFO_MASK;
    171 #endif
    172 	/* KAME hack: recover scopeid */
    173 	(void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
    174 
    175 	for (in6p = rawin6pcb.in6p_next;
    176 	     in6p != &rawin6pcb; in6p = in6p->in6p_next)
    177 	{
    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, ip6->ip6_nxt, *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,
    214 						 n, opts) == 0) {
    215 					/* should notify about lost packet */
    216 					m_freem(n);
    217 					if (opts)
    218 						m_freem(opts);
    219 					rip6stat.rip6s_fullsock++;
    220 				} else
    221 					sorwakeup(last->in6p_socket);
    222 				opts = NULL;
    223 			}
    224 		}
    225 		last = in6p;
    226 	}
    227 #ifdef IPSEC
    228 	/*
    229 	 * Check AH/ESP integrity.
    230 	 */
    231 	if (last && ipsec6_in_reject(m, last)) {
    232 		m_freem(m);
    233 		ipsec6stat.in_polvio++;
    234 		ip6stat.ip6s_delivered--;
    235 		/* do not inject data into pcb */
    236 	} else
    237 #endif /* IPSEC */
    238 	if (last) {
    239 		if (last->in6p_flags & IN6P_CONTROLOPTS)
    240 			ip6_savecontrol(last, &opts, ip6, m);
    241 		/* strip intermediate headers */
    242 		m_adj(m, *offp);
    243 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    244 				(struct sockaddr *)&rip6src, m, opts) == 0) {
    245 			m_freem(m);
    246 			if (opts)
    247 				m_freem(opts);
    248 			rip6stat.rip6s_fullsock++;
    249 		} else
    250 			sorwakeup(last->in6p_socket);
    251 	} else {
    252 		rip6stat.rip6s_nosock++;
    253 		if (m->m_flags & M_MCAST)
    254 			rip6stat.rip6s_nosockmcast++;
    255 		if (proto == IPPROTO_NONE)
    256 			m_freem(m);
    257 		else {
    258 			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
    259 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
    260 			icmp6_error(m, ICMP6_PARAM_PROB,
    261 				    ICMP6_PARAMPROB_NEXTHEADER,
    262 				    prvnxtp - mtod(m, char *));
    263 		}
    264 		ip6stat.ip6s_delivered--;
    265 	}
    266 	return IPPROTO_DONE;
    267 }
    268 
    269 void
    270 rip6_ctlinput(cmd, sa, d)
    271 	int cmd;
    272 	struct sockaddr *sa;
    273 	void *d;
    274 {
    275 	struct ip6_hdr *ip6;
    276 	struct mbuf *m;
    277 	int off;
    278 	struct ip6ctlparam *ip6cp = NULL;
    279 	const struct sockaddr_in6 *sa6_src = NULL;
    280 	void *cmdarg;
    281 	void (*notify) __P((struct in6pcb *, int)) = in6_rtchange;
    282 	int nxt;
    283 
    284 	if (sa->sa_family != AF_INET6 ||
    285 	    sa->sa_len != sizeof(struct sockaddr_in6))
    286 		return;
    287 
    288 	if ((unsigned)cmd >= PRC_NCMDS)
    289 		return;
    290 	if (PRC_IS_REDIRECT(cmd))
    291 		notify = in6_rtchange, d = NULL;
    292 	else if (cmd == PRC_HOSTDEAD)
    293 		d = NULL;
    294 	else if (cmd == PRC_MSGSIZE)
    295 		; /* special code is present, see below */
    296 	else if (inet6ctlerrmap[cmd] == 0)
    297 		return;
    298 
    299 	/* if the parameter is from icmp6, decode it. */
    300 	if (d != NULL) {
    301 		ip6cp = (struct ip6ctlparam *)d;
    302 		m = ip6cp->ip6c_m;
    303 		ip6 = ip6cp->ip6c_ip6;
    304 		off = ip6cp->ip6c_off;
    305 		cmdarg = ip6cp->ip6c_cmdarg;
    306 		sa6_src = ip6cp->ip6c_src;
    307 		nxt = ip6cp->ip6c_nxt;
    308 	} else {
    309 		m = NULL;
    310 		ip6 = NULL;
    311 		cmdarg = NULL;
    312 		sa6_src = &sa6_any;
    313 		nxt = -1;
    314 	}
    315 
    316 	if (ip6 && cmd == PRC_MSGSIZE) {
    317 		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
    318 		int valid = 0;
    319 		struct in6pcb *in6p;
    320 
    321 		/*
    322 		 * Check to see if we have a valid raw IPv6 socket
    323 		 * corresponding to the address in the ICMPv6 message
    324 		 * payload, and the protocol (ip6_nxt) meets the socket.
    325 		 * XXX chase extension headers, or pass final nxt value
    326 		 * from icmp6_notify_error()
    327 		 */
    328 		in6p = NULL;
    329 		in6p = in6_pcblookup_connect(&rawin6pcb,
    330 		    &sa6->sin6_addr, 0,
    331 		    (struct in6_addr *)&sa6_src->sin6_addr, 0, 0);
    332 #if 0
    333 		if (!in6p) {
    334 			/*
    335 			 * As the use of sendto(2) is fairly popular,
    336 			 * we may want to allow non-connected pcb too.
    337 			 * But it could be too weak against attacks...
    338 			 * We should at least check if the local
    339 			 * address (= s) is really ours.
    340 			 */
    341 			in6p = in6_pcblookup_bind(&rawin6pcb,
    342 			    &sa6->sin6_addr, 0, 0))
    343 		}
    344 #endif
    345 
    346 		if (in6p && in6p->in6p_ip6.ip6_nxt &&
    347 		    in6p->in6p_ip6.ip6_nxt == nxt)
    348 			valid++;
    349 
    350 		/*
    351 		 * Depending on the value of "valid" and routing table
    352 		 * size (mtudisc_{hi,lo}wat), we will:
    353 		 * - recalcurate the new MTU and create the
    354 		 *   corresponding routing entry, or
    355 		 * - ignore the MTU change notification.
    356 		 */
    357 		icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    358 
    359 		/*
    360 		 * regardless of if we called icmp6_mtudisc_update(),
    361 		 * we need to call in6_pcbnotify(), to notify path
    362 		 * MTU change to the userland (2292bis-02), because
    363 		 * some unconnected sockets may share the same
    364 		 * destination and want to know the path MTU.
    365 		 */
    366 	}
    367 
    368 	(void) in6_pcbnotify(&rawin6pcb, sa, 0,
    369 	    (struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
    370 }
    371 
    372 /*
    373  * Generate IPv6 header and pass packet to ip6_output.
    374  * Tack on options user may have setup with control call.
    375  */
    376 int
    377 #if __STDC__
    378 rip6_output(struct mbuf *m, ...)
    379 #else
    380 rip6_output(m, va_alist)
    381 	struct mbuf *m;
    382 	va_dcl
    383 #endif
    384 {
    385 	struct socket *so;
    386 	struct sockaddr_in6 *dstsock;
    387 	struct mbuf *control;
    388 	struct in6_addr *dst;
    389 	struct ip6_hdr *ip6;
    390 	struct in6pcb *in6p;
    391 	u_int	plen = m->m_pkthdr.len;
    392 	int error = 0;
    393 	struct ip6_pktopts opt, *optp = NULL, *origoptp;
    394 	struct ifnet *oifp = NULL;
    395 	int type, code;		/* for ICMPv6 output statistics only */
    396 	int priv = 0;
    397 	va_list ap;
    398 	int flags;
    399 
    400 	va_start(ap, m);
    401 	so = va_arg(ap, struct socket *);
    402 	dstsock = va_arg(ap, struct sockaddr_in6 *);
    403 	control = va_arg(ap, struct mbuf *);
    404 	va_end(ap);
    405 
    406 	in6p = sotoin6pcb(so);
    407 
    408 	priv = 0;
    409     {
    410 	struct proc *p = (curproc ? curproc->l_proc : 0);	/* XXX */
    411 
    412 	if (p && !suser(p->p_ucred, &p->p_acflag))
    413 		priv = 1;
    414     }
    415 	dst = &dstsock->sin6_addr;
    416 	if (control) {
    417 		if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
    418 			goto bad;
    419 		optp = &opt;
    420 	} else
    421 		optp = in6p->in6p_outputopts;
    422 
    423 	/*
    424 	 * For an ICMPv6 packet, we should know its type and code
    425 	 * to update statistics.
    426 	 */
    427 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    428 		struct icmp6_hdr *icmp6;
    429 		if (m->m_len < sizeof(struct icmp6_hdr) &&
    430 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
    431 			error = ENOBUFS;
    432 			goto bad;
    433 		}
    434 		icmp6 = mtod(m, struct icmp6_hdr *);
    435 		type = icmp6->icmp6_type;
    436 		code = icmp6->icmp6_code;
    437 	}
    438 
    439 	M_PREPEND(m, sizeof(*ip6), M_WAIT);
    440 	ip6 = mtod(m, struct ip6_hdr *);
    441 
    442 	/*
    443 	 * Next header might not be ICMP6 but use its pseudo header anyway.
    444 	 */
    445 	ip6->ip6_dst = *dst;
    446 
    447 	/* KAME hack: embed scopeid */
    448 	origoptp = in6p->in6p_outputopts;
    449 	in6p->in6p_outputopts = optp;
    450 	if (in6_embedscope(&ip6->ip6_dst, dstsock, in6p, &oifp) != 0) {
    451 		error = EINVAL;
    452 		goto bad;
    453 	}
    454 	in6p->in6p_outputopts = origoptp;
    455 
    456 	/*
    457 	 * Source address selection.
    458 	 */
    459 	{
    460 		struct in6_addr *in6a;
    461 
    462 		if ((in6a = in6_selectsrc(dstsock, optp,
    463 					  in6p->in6p_moptions,
    464 					  &in6p->in6p_route,
    465 					  &in6p->in6p_laddr,
    466 					  &error)) == 0) {
    467 			if (error == 0)
    468 				error = EADDRNOTAVAIL;
    469 			goto bad;
    470 		}
    471 		ip6->ip6_src = *in6a;
    472 		if (in6p->in6p_route.ro_rt) {
    473 			/* what if oifp contradicts ? */
    474 			oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
    475 		}
    476 	}
    477 
    478 	ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    479 	ip6->ip6_vfc  &= ~IPV6_VERSION_MASK;
    480 	ip6->ip6_vfc  |= IPV6_VERSION;
    481 #if 0				/* ip6_plen will be filled in ip6_output. */
    482 	ip6->ip6_plen  = htons((u_short)plen);
    483 #endif
    484 	ip6->ip6_nxt   = in6p->in6p_ip6.ip6_nxt;
    485 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
    486 
    487 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
    488 	    in6p->in6p_cksum != -1) {
    489 		int off;
    490 		u_int16_t sum;
    491 
    492 #define	offsetof(type, member)	((size_t)(&((type *)0)->member)) /* XXX */
    493 
    494 		/* compute checksum */
    495 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
    496 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
    497 		else
    498 			off = in6p->in6p_cksum;
    499 		if (plen < off + 1) {
    500 			error = EINVAL;
    501 			goto bad;
    502 		}
    503 		off += sizeof(struct ip6_hdr);
    504 
    505 		sum = 0;
    506 		m_copyback(m, off, sizeof(sum), (caddr_t)&sum);
    507 		sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
    508 		m_copyback(m, off, sizeof(sum), (caddr_t)&sum);
    509 	}
    510 
    511 #ifdef IPSEC
    512 	if (ipsec_setsocket(m, so) != 0) {
    513 		error = ENOBUFS;
    514 		goto bad;
    515 	}
    516 #endif /* IPSEC */
    517 
    518 	flags = 0;
    519 #ifdef IPV6_MINMTU
    520 	if (in6p->in6p_flags & IN6P_MINMTU)
    521 		flags |= IPV6_MINMTU;
    522 #endif
    523 
    524 	error = ip6_output(m, optp, &in6p->in6p_route, flags,
    525 	    in6p->in6p_moptions, &oifp);
    526 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    527 		if (oifp)
    528 			icmp6_ifoutstat_inc(oifp, type, code);
    529 		icmp6stat.icp6s_outhist[type]++;
    530 	} else
    531 		rip6stat.rip6s_opackets++;
    532 
    533 	goto freectl;
    534 
    535  bad:
    536 	if (m)
    537 		m_freem(m);
    538 
    539  freectl:
    540 	if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
    541 		RTFREE(optp->ip6po_route.ro_rt);
    542 	if (control)
    543 		m_freem(control);
    544 	return(error);
    545 }
    546 
    547 /*
    548  * Raw IPv6 socket option processing.
    549   */
    550 int
    551 rip6_ctloutput(op, so, level, optname, mp)
    552 	int op;
    553 	struct socket *so;
    554 	int level, optname;
    555 	struct mbuf **mp;
    556 {
    557 	int error = 0;
    558 	int optval;
    559 	struct in6pcb *in6p;
    560 	struct mbuf *m;
    561 	const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
    562 
    563 	switch (level) {
    564 	case IPPROTO_IPV6:
    565 		switch (optname) {
    566 		case MRT6_INIT:
    567 		case MRT6_DONE:
    568 		case MRT6_ADD_MIF:
    569 		case MRT6_DEL_MIF:
    570 		case MRT6_ADD_MFC:
    571 		case MRT6_DEL_MFC:
    572 		case MRT6_PIM:
    573 			if (op == PRCO_SETOPT) {
    574 				error = ip6_mrouter_set(optname, so, *mp);
    575 				if (*mp)
    576 					(void)m_free(*mp);
    577 			} else if (op == PRCO_GETOPT)
    578 				error = ip6_mrouter_get(optname, so, mp);
    579 			else
    580 				error = EINVAL;
    581 			return (error);
    582 		case IPV6_CHECKSUM:
    583 			/*
    584 			 * for ICMPv6 sockets, no modification allowed for
    585 			 * checksum offset, permit "no change" values to
    586 			 * help existing apps.
    587 			 */
    588 			in6p = sotoin6pcb(so);
    589 			error = 0;
    590 			if (op == PRCO_SETOPT) {
    591 				m = *mp;
    592 				if (m && m->m_len == sizeof(int)) {
    593 					optval = *mtod(m, int *);
    594 					if (so->so_proto->pr_protocol ==
    595 					    IPPROTO_ICMPV6) {
    596 						if (optval != icmp6off)
    597 							error = EINVAL;
    598 					} else
    599 						in6p->in6p_cksum = optval;
    600 				} else
    601 					error = EINVAL;
    602 				if (m)
    603 					m_free(m);
    604 			} else if (op == PRCO_GETOPT) {
    605 				if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
    606 					optval = icmp6off;
    607 				else
    608 					optval = in6p->in6p_cksum;
    609 				*mp = m = m_get(M_WAIT, MT_SOOPTS);
    610 				m->m_len = sizeof(int);
    611 				*mtod(m, int *) = optval;
    612 			} else
    613 				error = EINVAL;
    614 			return error;
    615 		default:
    616 			return (ip6_ctloutput(op, so, level, optname, mp));
    617 		}
    618 
    619 	case IPPROTO_ICMPV6:
    620 		/*
    621 		 * XXX: is it better to call icmp6_ctloutput() directly
    622 		 * from protosw?
    623 		 */
    624 		return(icmp6_ctloutput(op, so, level, optname, mp));
    625 
    626 	default:
    627 		if (op == PRCO_SETOPT && *mp)
    628 			m_free(*mp);
    629 		return EINVAL;
    630 	}
    631 }
    632 
    633 extern	u_long rip6_sendspace;
    634 extern	u_long rip6_recvspace;
    635 
    636 int
    637 rip6_usrreq(so, req, m, nam, control, p)
    638 	struct socket *so;
    639 	int req;
    640 	struct mbuf *m, *nam, *control;
    641 	struct proc *p;
    642 {
    643 	struct in6pcb *in6p = sotoin6pcb(so);
    644 	int s;
    645 	int error = 0;
    646 /*	extern	struct socket *ip6_mrouter; */ /* xxx */
    647 	int priv;
    648 
    649 	priv = 0;
    650 	if (p && !suser(p->p_ucred, &p->p_acflag))
    651 		priv++;
    652 
    653 	if (req == PRU_CONTROL)
    654 		return (in6_control(so, (u_long)m, (caddr_t)nam,
    655 				    (struct ifnet *)control, p));
    656 
    657 	if (req == PRU_PURGEIF) {
    658 		in6_pcbpurgeif0(&rawin6pcb, (struct ifnet *)control);
    659 		in6_purgeif((struct ifnet *)control);
    660 		in6_pcbpurgeif(&rawin6pcb, (struct ifnet *)control);
    661 		return (0);
    662 	}
    663 
    664 	switch (req) {
    665 	case PRU_ATTACH:
    666 		if (in6p)
    667 			panic("rip6_attach");
    668 		if (!priv) {
    669 			error = EACCES;
    670 			break;
    671 		}
    672 		s = splsoftnet();
    673 		if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) != 0) {
    674 			splx(s);
    675 			break;
    676 		}
    677 		if ((error = in6_pcballoc(so, &rawin6pcb)) != 0)
    678 		{
    679 			splx(s);
    680 			break;
    681 		}
    682 		splx(s);
    683 		in6p = sotoin6pcb(so);
    684 		in6p->in6p_ip6.ip6_nxt = (long)nam;
    685 		in6p->in6p_cksum = -1;
    686 
    687 		MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
    688 			sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
    689 		if (in6p->in6p_icmp6filt == NULL) {
    690 			in6_pcbdetach(in6p);
    691 			error = ENOMEM;
    692 			break;
    693 		}
    694 		ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
    695 		break;
    696 
    697 	case PRU_DISCONNECT:
    698 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    699 			error = ENOTCONN;
    700 			break;
    701 		}
    702 		in6p->in6p_faddr = in6addr_any;
    703 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
    704 		break;
    705 
    706 	case PRU_ABORT:
    707 		soisdisconnected(so);
    708 		/* Fallthrough */
    709 	case PRU_DETACH:
    710 		if (in6p == 0)
    711 			panic("rip6_detach");
    712 		if (so == ip6_mrouter)
    713 			ip6_mrouter_done();
    714 		/* xxx: RSVP */
    715 		if (in6p->in6p_icmp6filt) {
    716 			FREE(in6p->in6p_icmp6filt, M_PCB);
    717 			in6p->in6p_icmp6filt = NULL;
    718 		}
    719 		in6_pcbdetach(in6p);
    720 		break;
    721 
    722 	case PRU_BIND:
    723 	    {
    724 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    725 		struct ifaddr *ia = NULL;
    726 
    727 		if (nam->m_len != sizeof(*addr)) {
    728 			error = EINVAL;
    729 			break;
    730 		}
    731 		if ((ifnet.tqh_first == 0) || (addr->sin6_family != AF_INET6)) {
    732 			error = EADDRNOTAVAIL;
    733 			break;
    734 		}
    735 #ifdef ENABLE_DEFAULT_SCOPE
    736 		if (addr->sin6_scope_id == 0)	/* not change if specified  */
    737 			addr->sin6_scope_id =
    738 				scope6_addr2default(&addr->sin6_addr);
    739 #endif
    740 		/* KAME hack: embed scopeid */
    741 		if (in6_embedscope(&addr->sin6_addr, addr, in6p, NULL) != 0)
    742 			return EINVAL;
    743 #ifndef SCOPEDROUTING
    744 		addr->sin6_scope_id = 0; /* for ifa_ifwithaddr */
    745 #endif
    746 
    747 		/*
    748 		 * we don't support mapped address here, it would confuse
    749 		 * users so reject it
    750 		 */
    751 		if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
    752 			error = EADDRNOTAVAIL;
    753 			break;
    754 		}
    755 		if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
    756 		    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
    757 			error = EADDRNOTAVAIL;
    758 			break;
    759 		}
    760 		if (ia &&
    761 		    ((struct in6_ifaddr *)ia)->ia6_flags &
    762 		    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    763 		     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
    764 			error = EADDRNOTAVAIL;
    765 			break;
    766 		}
    767 		in6p->in6p_laddr = addr->sin6_addr;
    768 		break;
    769 	    }
    770 
    771 	case PRU_CONNECT:
    772 	    {
    773 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    774 		struct in6_addr *in6a = NULL;
    775 #ifdef ENABLE_DEFAULT_SCOPE
    776 		struct sockaddr_in6 sin6;
    777 #endif
    778 
    779 		if (nam->m_len != sizeof(*addr)) {
    780 			error = EINVAL;
    781 			break;
    782 		}
    783 		if (ifnet.tqh_first == 0)
    784 		{
    785 			error = EADDRNOTAVAIL;
    786 			break;
    787 		}
    788 		if (addr->sin6_family != AF_INET6) {
    789 			error = EAFNOSUPPORT;
    790 			break;
    791 		}
    792 
    793 #ifdef ENABLE_DEFAULT_SCOPE
    794 		if (addr->sin6_scope_id == 0) {
    795 			/* protect *addr */
    796 			sin6 = *addr;
    797 			addr = &sin6;
    798 			addr->sin6_scope_id =
    799 				scope6_addr2default(&addr->sin6_addr);
    800 		}
    801 #endif
    802 
    803 		/* Source address selection. XXX: need pcblookup? */
    804 		in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
    805 				     in6p->in6p_moptions,
    806 				     &in6p->in6p_route,
    807 				     &in6p->in6p_laddr,
    808 				     &error);
    809 		if (in6a == NULL) {
    810 			if (error == 0)
    811 				error = EADDRNOTAVAIL;
    812 			break;
    813 		}
    814 		in6p->in6p_laddr = *in6a;
    815 		in6p->in6p_faddr = addr->sin6_addr;
    816 		soisconnected(so);
    817 		break;
    818 	    }
    819 
    820 	case PRU_CONNECT2:
    821 		error = EOPNOTSUPP;
    822 		break;
    823 
    824 	/*
    825 	 * Mark the connection as being incapable of futther input.
    826 	 */
    827 	case PRU_SHUTDOWN:
    828 		socantsendmore(so);
    829 		break;
    830 	/*
    831 	 * Ship a packet out. The appropriate raw output
    832 	 * routine handles any messaging necessary.
    833 	 */
    834 	case PRU_SEND:
    835 	    {
    836 		struct sockaddr_in6 tmp;
    837 		struct sockaddr_in6 *dst;
    838 
    839 		/* always copy sockaddr to avoid overwrites */
    840 		if (so->so_state & SS_ISCONNECTED) {
    841 			if (nam) {
    842 				error = EISCONN;
    843 				break;
    844 			}
    845 			/* XXX */
    846 			bzero(&tmp, sizeof(tmp));
    847 			tmp.sin6_family = AF_INET6;
    848 			tmp.sin6_len = sizeof(struct sockaddr_in6);
    849 			bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
    850 				sizeof(struct in6_addr));
    851 			dst = &tmp;
    852 		} else {
    853 			if (nam == NULL) {
    854 				error = ENOTCONN;
    855 				break;
    856 			}
    857 			tmp = *mtod(nam, struct sockaddr_in6 *);
    858 			dst = &tmp;
    859 		}
    860 #ifdef ENABLE_DEFAULT_SCOPE
    861 		if (dst->sin6_scope_id == 0) {
    862 			dst->sin6_scope_id =
    863 				scope6_addr2default(&dst->sin6_addr);
    864 		}
    865 #endif
    866 		error = rip6_output(m, so, dst, control);
    867 		m = NULL;
    868 		break;
    869 	    }
    870 
    871 	case PRU_SENSE:
    872 		/*
    873 		 * stat: don't bother with a blocksize
    874 		 */
    875 		return(0);
    876 	/*
    877 	 * Not supported.
    878 	 */
    879 	case PRU_RCVOOB:
    880 	case PRU_RCVD:
    881 	case PRU_LISTEN:
    882 	case PRU_ACCEPT:
    883 	case PRU_SENDOOB:
    884 		error = EOPNOTSUPP;
    885 		break;
    886 
    887 	case PRU_SOCKADDR:
    888 		in6_setsockaddr(in6p, nam);
    889 		break;
    890 
    891 	case PRU_PEERADDR:
    892 		in6_setpeeraddr(in6p, nam);
    893 		break;
    894 
    895 	default:
    896 		panic("rip6_usrreq");
    897 	}
    898 	if (m != NULL)
    899 		m_freem(m);
    900 	return(error);
    901 }
    902