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