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