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