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