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