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