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