Home | History | Annotate | Line # | Download | only in netinet6
raw_ip6.c revision 1.46
      1 /*	$NetBSD: raw_ip6.c,v 1.46 2002/06/07 22:07:38 itojun 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.46 2002/06/07 22:07:38 itojun 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/proc.h>
     82 
     83 #include <net/if.h>
     84 #include <net/route.h>
     85 #include <net/if_types.h>
     86 
     87 #include <netinet/in.h>
     88 #include <netinet/in_var.h>
     89 #include <netinet/ip6.h>
     90 #include <netinet6/ip6_var.h>
     91 #include <netinet6/ip6_mroute.h>
     92 #include <netinet/icmp6.h>
     93 #include <netinet6/in6_pcb.h>
     94 #include <netinet6/nd6.h>
     95 #include <netinet6/ip6protosw.h>
     96 #ifdef ENABLE_DEFAULT_SCOPE
     97 #include <netinet6/scope6_var.h>
     98 #endif
     99 #include <netinet6/raw_ip6.h>
    100 
    101 #ifdef IPSEC
    102 #include <netinet6/ipsec.h>
    103 #endif /* IPSEC */
    104 
    105 #include <machine/stdarg.h>
    106 
    107 #include "faith.h"
    108 #if defined(NFAITH) && 0 < NFAITH
    109 #include <net/if_faith.h>
    110 #endif
    111 
    112 struct	in6pcb rawin6pcb;
    113 #define ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
    114 
    115 /*
    116  * Raw interface to IP6 protocol.
    117  */
    118 
    119 struct rip6stat rip6stat;
    120 
    121 /*
    122  * Initialize raw connection block queue.
    123  */
    124 void
    125 rip6_init()
    126 {
    127 	rawin6pcb.in6p_next = rawin6pcb.in6p_prev = &rawin6pcb;
    128 }
    129 
    130 /*
    131  * Setup generic address and protocol structures
    132  * for raw_input routine, then pass them along with
    133  * mbuf chain.
    134  */
    135 int
    136 rip6_input(mp, offp, proto)
    137 	struct	mbuf **mp;
    138 	int	*offp, proto;
    139 {
    140 	struct mbuf *m = *mp;
    141 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    142 	struct in6pcb *in6p;
    143 	struct in6pcb *last = NULL;
    144 	struct sockaddr_in6 rip6src;
    145 	struct mbuf *opts = NULL;
    146 
    147 	rip6stat.rip6s_ipackets++;
    148 
    149 #if defined(NFAITH) && 0 < NFAITH
    150 	if (faithprefix(&ip6->ip6_dst)) {
    151 		/* send icmp6 host unreach? */
    152 		m_freem(m);
    153 		return IPPROTO_DONE;
    154 	}
    155 #endif
    156 
    157 	/* Be proactive about malicious use of IPv4 mapped address */
    158 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    159 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    160 		/* XXX stat */
    161 		m_freem(m);
    162 		return IPPROTO_DONE;
    163 	}
    164 
    165 	bzero(&rip6src, sizeof(rip6src));
    166 	rip6src.sin6_len = sizeof(struct sockaddr_in6);
    167 	rip6src.sin6_family = AF_INET6;
    168 #if 0 /* XXX inbound flowlabel */
    169 	rip6src.sin6_flowinfo = ip6->ip6_flow & IPV6_FLOWINFO_MASK;
    170 #endif
    171 	/* KAME hack: recover scopeid */
    172 	(void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
    173 
    174 	for (in6p = rawin6pcb.in6p_next;
    175 	     in6p != &rawin6pcb; in6p = in6p->in6p_next)
    176 	{
    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, ip6->ip6_nxt, *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 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
    207 				if (last->in6p_flags & IN6P_CONTROLOPTS)
    208 					ip6_savecontrol(last, &opts, ip6, n);
    209 				/* strip intermediate headers */
    210 				m_adj(n, *offp);
    211 				if (sbappendaddr(&last->in6p_socket->so_rcv,
    212 				    (struct sockaddr *)&rip6src, n, opts) == 0) {
    213 					/* should notify about lost packet */
    214 					m_freem(n);
    215 					if (opts)
    216 						m_freem(opts);
    217 					rip6stat.rip6s_fullsock++;
    218 				} else
    219 					sorwakeup(last->in6p_socket);
    220 				opts = NULL;
    221 			}
    222 		}
    223 		last = in6p;
    224 	}
    225 #ifdef IPSEC
    226 	/*
    227 	 * Check AH/ESP integrity.
    228 	 */
    229 	if (last && ipsec6_in_reject(m, last)) {
    230 		m_freem(m);
    231 		ipsec6stat.in_polvio++;
    232 		ip6stat.ip6s_delivered--;
    233 		/* do not inject data into pcb */
    234 	} else
    235 #endif /* IPSEC */
    236 	if (last) {
    237 		if (last->in6p_flags & IN6P_CONTROLOPTS)
    238 			ip6_savecontrol(last, &opts, ip6, m);
    239 		/* strip intermediate headers */
    240 		m_adj(m, *offp);
    241 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    242 		    (struct sockaddr *)&rip6src, m, opts) == 0) {
    243 			m_freem(m);
    244 			if (opts)
    245 				m_freem(opts);
    246 			rip6stat.rip6s_fullsock++;
    247 		} else
    248 			sorwakeup(last->in6p_socket);
    249 	} else {
    250 		rip6stat.rip6s_nosock++;
    251 		if (m->m_flags & M_MCAST)
    252 			rip6stat.rip6s_nosockmcast++;
    253 		if (proto == IPPROTO_NONE)
    254 			m_freem(m);
    255 		else {
    256 			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
    257 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
    258 			icmp6_error(m, ICMP6_PARAM_PROB,
    259 			    ICMP6_PARAMPROB_NEXTHEADER,
    260 			    prvnxtp - mtod(m, char *));
    261 		}
    262 		ip6stat.ip6s_delivered--;
    263 	}
    264 	return IPPROTO_DONE;
    265 }
    266 
    267 void
    268 rip6_ctlinput(cmd, sa, d)
    269 	int cmd;
    270 	struct sockaddr *sa;
    271 	void *d;
    272 {
    273 	struct ip6_hdr *ip6;
    274 	struct mbuf *m;
    275 	int off;
    276 	struct ip6ctlparam *ip6cp = NULL;
    277 	const struct sockaddr_in6 *sa6_src = NULL;
    278 	void *cmdarg;
    279 	void (*notify) __P((struct in6pcb *, int)) = in6_rtchange;
    280 	int nxt;
    281 
    282 	if (sa->sa_family != AF_INET6 ||
    283 	    sa->sa_len != sizeof(struct sockaddr_in6))
    284 		return;
    285 
    286 	if ((unsigned)cmd >= PRC_NCMDS)
    287 		return;
    288 	if (PRC_IS_REDIRECT(cmd))
    289 		notify = in6_rtchange, d = NULL;
    290 	else if (cmd == PRC_HOSTDEAD)
    291 		d = NULL;
    292 	else if (cmd == PRC_MSGSIZE)
    293 		; /* special code is present, see below */
    294 	else if (inet6ctlerrmap[cmd] == 0)
    295 		return;
    296 
    297 	/* if the parameter is from icmp6, decode it. */
    298 	if (d != NULL) {
    299 		ip6cp = (struct ip6ctlparam *)d;
    300 		m = ip6cp->ip6c_m;
    301 		ip6 = ip6cp->ip6c_ip6;
    302 		off = ip6cp->ip6c_off;
    303 		cmdarg = ip6cp->ip6c_cmdarg;
    304 		sa6_src = ip6cp->ip6c_src;
    305 		nxt = ip6cp->ip6c_nxt;
    306 	} else {
    307 		m = NULL;
    308 		ip6 = NULL;
    309 		cmdarg = NULL;
    310 		sa6_src = &sa6_any;
    311 		nxt = -1;
    312 	}
    313 
    314 	if (ip6 && cmd == PRC_MSGSIZE) {
    315 		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
    316 		int valid = 0;
    317 		struct in6pcb *in6p;
    318 
    319 		/*
    320 		 * Check to see if we have a valid raw IPv6 socket
    321 		 * corresponding to the address in the ICMPv6 message
    322 		 * payload, and the protocol (ip6_nxt) meets the socket.
    323 		 * XXX chase extension headers, or pass final nxt value
    324 		 * from icmp6_notify_error()
    325 		 */
    326 		in6p = NULL;
    327 		in6p = in6_pcblookup_connect(&rawin6pcb, &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 		 * - recalculate 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;	/* 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, in6p->in6p_moptions,
    460 		    &in6p->in6p_route, &in6p->in6p_laddr, &error)) == 0) {
    461 			if (error == 0)
    462 				error = EADDRNOTAVAIL;
    463 			goto bad;
    464 		}
    465 		ip6->ip6_src = *in6a;
    466 		if (in6p->in6p_route.ro_rt) {
    467 			/* what if oifp contradicts ? */
    468 			oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
    469 		}
    470 	}
    471 
    472 	ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    473 	ip6->ip6_vfc  &= ~IPV6_VERSION_MASK;
    474 	ip6->ip6_vfc  |= IPV6_VERSION;
    475 #if 0				/* ip6_plen will be filled in ip6_output. */
    476 	ip6->ip6_plen  = htons((u_short)plen);
    477 #endif
    478 	ip6->ip6_nxt   = in6p->in6p_ip6.ip6_nxt;
    479 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
    480 
    481 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
    482 	    in6p->in6p_cksum != -1) {
    483 		int off;
    484 		u_int16_t sum;
    485 
    486 		/* compute checksum */
    487 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
    488 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
    489 		else
    490 			off = in6p->in6p_cksum;
    491 		if (plen < off + 1) {
    492 			error = EINVAL;
    493 			goto bad;
    494 		}
    495 		off += sizeof(struct ip6_hdr);
    496 
    497 		sum = 0;
    498 		m_copyback(m, off, sizeof(sum), (caddr_t)&sum);
    499 		sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
    500 		m_copyback(m, off, sizeof(sum), (caddr_t)&sum);
    501 	}
    502 
    503 #ifdef IPSEC
    504 	if (ipsec_setsocket(m, so) != 0) {
    505 		error = ENOBUFS;
    506 		goto bad;
    507 	}
    508 #endif /* IPSEC */
    509 
    510 	flags = 0;
    511 #ifdef IN6P_MINMTU
    512 	if (in6p->in6p_flags & IN6P_MINMTU)
    513 		flags |= IPV6_MINMTU;
    514 #endif
    515 
    516 	error = ip6_output(m, optp, &in6p->in6p_route, flags,
    517 	    in6p->in6p_moptions, &oifp);
    518 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    519 		if (oifp)
    520 			icmp6_ifoutstat_inc(oifp, type, code);
    521 		icmp6stat.icp6s_outhist[type]++;
    522 	} else
    523 		rip6stat.rip6s_opackets++;
    524 
    525 	goto freectl;
    526 
    527  bad:
    528 	if (m)
    529 		m_freem(m);
    530 
    531  freectl:
    532 	if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
    533 		RTFREE(optp->ip6po_route.ro_rt);
    534 	if (control)
    535 		m_freem(control);
    536 	return(error);
    537 }
    538 
    539 /*
    540  * Raw IPv6 socket option processing.
    541  */
    542 int
    543 rip6_ctloutput(op, so, level, optname, mp)
    544 	int op;
    545 	struct socket *so;
    546 	int level, optname;
    547 	struct mbuf **mp;
    548 {
    549 	int error = 0;
    550 
    551 	switch (level) {
    552 	case IPPROTO_IPV6:
    553 		switch (optname) {
    554 		case MRT6_INIT:
    555 		case MRT6_DONE:
    556 		case MRT6_ADD_MIF:
    557 		case MRT6_DEL_MIF:
    558 		case MRT6_ADD_MFC:
    559 		case MRT6_DEL_MFC:
    560 		case MRT6_PIM:
    561 			if (op == PRCO_SETOPT) {
    562 				error = ip6_mrouter_set(optname, so, *mp);
    563 				if (*mp)
    564 					(void)m_free(*mp);
    565 			} else if (op == PRCO_GETOPT)
    566 				error = ip6_mrouter_get(optname, so, mp);
    567 			else
    568 				error = EINVAL;
    569 			return (error);
    570 		case IPV6_CHECKSUM:
    571 			return (ip6_raw_ctloutput(op, so, level, optname, mp));
    572 		default:
    573 			return (ip6_ctloutput(op, so, level, optname, mp));
    574 		}
    575 
    576 	case IPPROTO_ICMPV6:
    577 		/*
    578 		 * XXX: is it better to call icmp6_ctloutput() directly
    579 		 * from protosw?
    580 		 */
    581 		return (icmp6_ctloutput(op, so, level, optname, mp));
    582 
    583 	default:
    584 		if (op == PRCO_SETOPT && *mp)
    585 			m_free(*mp);
    586 		return EINVAL;
    587 	}
    588 }
    589 
    590 extern	u_long rip6_sendspace;
    591 extern	u_long rip6_recvspace;
    592 
    593 int
    594 rip6_usrreq(so, req, m, nam, control, p)
    595 	struct socket *so;
    596 	int req;
    597 	struct mbuf *m, *nam, *control;
    598 	struct proc *p;
    599 {
    600 	struct in6pcb *in6p = sotoin6pcb(so);
    601 	int s;
    602 	int error = 0;
    603 /*	extern	struct socket *ip6_mrouter; */ /* xxx */
    604 	int priv;
    605 
    606 	priv = 0;
    607 	if (p && !suser(p->p_ucred, &p->p_acflag))
    608 		priv++;
    609 
    610 	if (req == PRU_CONTROL)
    611 		return (in6_control(so, (u_long)m, (caddr_t)nam,
    612 		    (struct ifnet *)control, p));
    613 
    614 	if (req == PRU_PURGEIF) {
    615 		in6_pcbpurgeif0(&rawin6pcb, (struct ifnet *)control);
    616 		in6_purgeif((struct ifnet *)control);
    617 		in6_pcbpurgeif(&rawin6pcb, (struct ifnet *)control);
    618 		return (0);
    619 	}
    620 
    621 	switch (req) {
    622 	case PRU_ATTACH:
    623 		if (in6p)
    624 			panic("rip6_attach");
    625 		if (!priv) {
    626 			error = EACCES;
    627 			break;
    628 		}
    629 		s = splsoftnet();
    630 		if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) != 0) {
    631 			splx(s);
    632 			break;
    633 		}
    634 		if ((error = in6_pcballoc(so, &rawin6pcb)) != 0)
    635 		{
    636 			splx(s);
    637 			break;
    638 		}
    639 		splx(s);
    640 		in6p = sotoin6pcb(so);
    641 		in6p->in6p_ip6.ip6_nxt = (long)nam;
    642 		in6p->in6p_cksum = -1;
    643 
    644 		MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
    645 		    sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
    646 		if (in6p->in6p_icmp6filt == NULL) {
    647 			in6_pcbdetach(in6p);
    648 			error = ENOMEM;
    649 			break;
    650 		}
    651 		ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
    652 		break;
    653 
    654 	case PRU_DISCONNECT:
    655 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    656 			error = ENOTCONN;
    657 			break;
    658 		}
    659 		in6p->in6p_faddr = in6addr_any;
    660 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
    661 		break;
    662 
    663 	case PRU_ABORT:
    664 		soisdisconnected(so);
    665 		/* Fallthrough */
    666 	case PRU_DETACH:
    667 		if (in6p == 0)
    668 			panic("rip6_detach");
    669 		if (so == ip6_mrouter)
    670 			ip6_mrouter_done();
    671 		/* xxx: RSVP */
    672 		if (in6p->in6p_icmp6filt) {
    673 			FREE(in6p->in6p_icmp6filt, M_PCB);
    674 			in6p->in6p_icmp6filt = NULL;
    675 		}
    676 		in6_pcbdetach(in6p);
    677 		break;
    678 
    679 	case PRU_BIND:
    680 	    {
    681 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    682 		struct ifaddr *ia = NULL;
    683 
    684 		if (nam->m_len != sizeof(*addr)) {
    685 			error = EINVAL;
    686 			break;
    687 		}
    688 		if ((ifnet.tqh_first == 0) || (addr->sin6_family != AF_INET6)) {
    689 			error = EADDRNOTAVAIL;
    690 			break;
    691 		}
    692 #ifdef ENABLE_DEFAULT_SCOPE
    693 		if (addr->sin6_scope_id == 0)	/* not change if specified  */
    694 			addr->sin6_scope_id =
    695 			    scope6_addr2default(&addr->sin6_addr);
    696 #endif
    697 		/* KAME hack: embed scopeid */
    698 		if (in6_embedscope(&addr->sin6_addr, addr, in6p, NULL) != 0)
    699 			return EINVAL;
    700 #ifndef SCOPEDROUTING
    701 		addr->sin6_scope_id = 0; /* for ifa_ifwithaddr */
    702 #endif
    703 
    704 		/*
    705 		 * we don't support mapped address here, it would confuse
    706 		 * users so reject it
    707 		 */
    708 		if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
    709 			error = EADDRNOTAVAIL;
    710 			break;
    711 		}
    712 		if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
    713 		    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
    714 			error = EADDRNOTAVAIL;
    715 			break;
    716 		}
    717 		if (ia && ((struct in6_ifaddr *)ia)->ia6_flags &
    718 		    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    719 		     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
    720 			error = EADDRNOTAVAIL;
    721 			break;
    722 		}
    723 		in6p->in6p_laddr = addr->sin6_addr;
    724 		break;
    725 	    }
    726 
    727 	case PRU_CONNECT:
    728 	{
    729 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    730 		struct in6_addr *in6a = NULL;
    731 #ifdef ENABLE_DEFAULT_SCOPE
    732 		struct sockaddr_in6 sin6;
    733 #endif
    734 
    735 		if (nam->m_len != sizeof(*addr)) {
    736 			error = EINVAL;
    737 			break;
    738 		}
    739 		if (ifnet.tqh_first == 0)
    740 		{
    741 			error = EADDRNOTAVAIL;
    742 			break;
    743 		}
    744 		if (addr->sin6_family != AF_INET6) {
    745 			error = EAFNOSUPPORT;
    746 			break;
    747 		}
    748 
    749 #ifdef ENABLE_DEFAULT_SCOPE
    750 		if (addr->sin6_scope_id == 0) {
    751 			/* protect *addr */
    752 			sin6 = *addr;
    753 			addr = &sin6;
    754 			addr->sin6_scope_id =
    755 			    scope6_addr2default(&addr->sin6_addr);
    756 		}
    757 #endif
    758 
    759 		/* Source address selection. XXX: need pcblookup? */
    760 		in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
    761 		    in6p->in6p_moptions, &in6p->in6p_route,
    762 		    &in6p->in6p_laddr, &error);
    763 		if (in6a == NULL) {
    764 			if (error == 0)
    765 				error = EADDRNOTAVAIL;
    766 			break;
    767 		}
    768 		in6p->in6p_laddr = *in6a;
    769 		in6p->in6p_faddr = addr->sin6_addr;
    770 		soisconnected(so);
    771 		break;
    772 	}
    773 
    774 	case PRU_CONNECT2:
    775 		error = EOPNOTSUPP;
    776 		break;
    777 
    778 	/*
    779 	 * Mark the connection as being incapable of futther input.
    780 	 */
    781 	case PRU_SHUTDOWN:
    782 		socantsendmore(so);
    783 		break;
    784 	/*
    785 	 * Ship a packet out. The appropriate raw output
    786 	 * routine handles any messaging necessary.
    787 	 */
    788 	case PRU_SEND:
    789 	{
    790 		struct sockaddr_in6 tmp;
    791 		struct sockaddr_in6 *dst;
    792 
    793 		/* always copy sockaddr to avoid overwrites */
    794 		if (so->so_state & SS_ISCONNECTED) {
    795 			if (nam) {
    796 				error = EISCONN;
    797 				break;
    798 			}
    799 			/* XXX */
    800 			bzero(&tmp, sizeof(tmp));
    801 			tmp.sin6_family = AF_INET6;
    802 			tmp.sin6_len = sizeof(struct sockaddr_in6);
    803 			bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
    804 			    sizeof(struct in6_addr));
    805 			dst = &tmp;
    806 		} else {
    807 			if (nam == NULL) {
    808 				error = ENOTCONN;
    809 				break;
    810 			}
    811 			if (nam->m_len != sizeof(tmp)) {
    812 				error = EINVAL;
    813 				break;
    814 			}
    815 
    816 			tmp = *mtod(nam, struct sockaddr_in6 *);
    817 			dst = &tmp;
    818 
    819 			if (dst->sin6_family != AF_INET6) {
    820 				error = EAFNOSUPPORT;
    821 				break;
    822 			}
    823 		}
    824 #ifdef ENABLE_DEFAULT_SCOPE
    825 		if (dst->sin6_scope_id == 0) {
    826 			dst->sin6_scope_id =
    827 			    scope6_addr2default(&dst->sin6_addr);
    828 		}
    829 #endif
    830 		error = rip6_output(m, so, dst, control);
    831 		m = NULL;
    832 		break;
    833 	}
    834 
    835 	case PRU_SENSE:
    836 		/*
    837 		 * stat: don't bother with a blocksize
    838 		 */
    839 		return(0);
    840 	/*
    841 	 * Not supported.
    842 	 */
    843 	case PRU_RCVOOB:
    844 	case PRU_RCVD:
    845 	case PRU_LISTEN:
    846 	case PRU_ACCEPT:
    847 	case PRU_SENDOOB:
    848 		error = EOPNOTSUPP;
    849 		break;
    850 
    851 	case PRU_SOCKADDR:
    852 		in6_setsockaddr(in6p, nam);
    853 		break;
    854 
    855 	case PRU_PEERADDR:
    856 		in6_setpeeraddr(in6p, nam);
    857 		break;
    858 
    859 	default:
    860 		panic("rip6_usrreq");
    861 	}
    862 	if (m != NULL)
    863 		m_freem(m);
    864 	return(error);
    865 }
    866