Home | History | Annotate | Line # | Download | only in netinet6
raw_ip6.c revision 1.12
      1 /*	$NetBSD: raw_ip6.c,v 1.12 1999/12/13 15:17:24 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1988, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
     65  */
     66 
     67 #ifdef __NetBSD__	/*XXX*/
     68 #include "opt_ipsec.h"
     69 #endif
     70 
     71 #include <sys/param.h>
     72 #include <sys/malloc.h>
     73 #include <sys/mbuf.h>
     74 #include <sys/socket.h>
     75 #include <sys/protosw.h>
     76 #include <sys/socketvar.h>
     77 #include <sys/errno.h>
     78 #include <sys/systm.h>
     79 #ifdef __NetBSD__
     80 #include <sys/proc.h>
     81 #endif
     82 
     83 #include <net/if.h>
     84 #include <net/route.h>
     85 #include <net/if_types.h>
     86 
     87 #include <netinet/in.h>
     88 #include <netinet/in_var.h>
     89 #include <netinet6/ip6.h>
     90 #include <netinet6/ip6_var.h>
     91 #include <netinet6/ip6_mroute.h>
     92 #include <netinet6/icmp6.h>
     93 #include <netinet6/in6_pcb.h>
     94 #include <netinet6/nd6.h>
     95 
     96 #ifdef IPSEC
     97 #include <netinet6/ipsec.h>
     98 #endif /*IPSEC*/
     99 
    100 #include <machine/stdarg.h>
    101 
    102 #include "faith.h"
    103 
    104 struct	in6pcb rawin6pcb;
    105 #define ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
    106 
    107 /*
    108  * Raw interface to IP6 protocol.
    109  */
    110 
    111 /*
    112  * Initialize raw connection block queue.
    113  */
    114 void
    115 rip6_init()
    116 {
    117 	rawin6pcb.in6p_next = rawin6pcb.in6p_prev = &rawin6pcb;
    118 }
    119 
    120 /*
    121  * Setup generic address and protocol structures
    122  * for raw_input routine, then pass them along with
    123  * mbuf chain.
    124  */
    125 int
    126 rip6_input(mp, offp, proto)
    127 	struct	mbuf **mp;
    128 	int	*offp, proto;
    129 {
    130 	struct mbuf *m = *mp;
    131 	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    132 	register struct in6pcb *in6p;
    133 	struct in6pcb *last = NULL;
    134 	struct sockaddr_in6 rip6src;
    135 	struct mbuf *opts = NULL;
    136 
    137 #if defined(NFAITH) && 0 < NFAITH
    138 	if (m->m_pkthdr.rcvif) {
    139 		if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
    140 			/* send icmp6 host unreach? */
    141 			m_freem(m);
    142 			return IPPROTO_DONE;
    143 		}
    144 	}
    145 #endif
    146 	bzero(&rip6src, sizeof(rip6src));
    147 	rip6src.sin6_len = sizeof(struct sockaddr_in6);
    148 	rip6src.sin6_family = AF_INET6;
    149 	rip6src.sin6_addr = ip6->ip6_src;
    150 	if (IN6_IS_SCOPE_LINKLOCAL(&rip6src.sin6_addr))
    151 		rip6src.sin6_addr.s6_addr16[1] = 0;
    152 	if (m->m_pkthdr.rcvif) {
    153 		if (IN6_IS_SCOPE_LINKLOCAL(&rip6src.sin6_addr))
    154 			rip6src.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
    155 		else
    156 			rip6src.sin6_scope_id = 0;
    157 	} else
    158 		rip6src.sin6_scope_id = 0;
    159 
    160 	for (in6p = rawin6pcb.in6p_next;
    161 	     in6p != &rawin6pcb; in6p = in6p->in6p_next) {
    162 		if (in6p->in6p_ip6.ip6_nxt &&
    163 		    in6p->in6p_ip6.ip6_nxt != proto)
    164 			continue;
    165 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
    166 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
    167 			continue;
    168 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
    169 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
    170 			continue;
    171 		if (in6p->in6p_cksum != -1
    172 		 && in6_cksum(m, ip6->ip6_nxt, *offp, m->m_pkthdr.len - *offp))
    173 		{
    174 			/* XXX bark something */
    175 			continue;
    176 		}
    177 		if (last) {
    178 			struct	mbuf *n;
    179 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
    180 				if (last->in6p_flags & IN6P_CONTROLOPTS)
    181 					ip6_savecontrol(last, &opts, ip6, n);
    182 				/* strip intermediate headers */
    183 				m_adj(n, *offp);
    184 				if (sbappendaddr(&last->in6p_socket->so_rcv,
    185 						(struct sockaddr *)&rip6src,
    186 						 n, opts) == 0) {
    187 					/* should notify about lost packet */
    188 					m_freem(n);
    189 					if (opts)
    190 						m_freem(opts);
    191 				} else
    192 					sorwakeup(last->in6p_socket);
    193 				opts = NULL;
    194 			}
    195 		}
    196 		last = in6p;
    197 	}
    198 	if (last) {
    199 		if (last->in6p_flags & IN6P_CONTROLOPTS)
    200 			ip6_savecontrol(last, &opts, ip6, m);
    201 		/* strip intermediate headers */
    202 		m_adj(m, *offp);
    203 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    204 				(struct sockaddr *)&rip6src, m, opts) == 0) {
    205 			m_freem(m);
    206 			if (opts)
    207 				m_freem(opts);
    208 		} else
    209 			sorwakeup(last->in6p_socket);
    210 	} else {
    211 		if (proto == IPPROTO_NONE)
    212 			m_freem(m);
    213 		else {
    214 			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
    215 			icmp6_error(m, ICMP6_PARAM_PROB,
    216 				    ICMP6_PARAMPROB_NEXTHEADER,
    217 				    prvnxtp - mtod(m, char *));
    218 		}
    219 		ip6stat.ip6s_delivered--;
    220 	}
    221 	return IPPROTO_DONE;
    222 }
    223 
    224 /*
    225  * Generate IPv6 header and pass packet to ip6_output.
    226  * Tack on options user may have setup with control call.
    227  */
    228 int
    229 #if __STDC__
    230 rip6_output(struct mbuf *m, ...)
    231 #else
    232 rip6_output(m, va_alist)
    233 	struct mbuf *m;
    234 	va_dcl
    235 #endif
    236 {
    237 	struct socket *so;
    238 	struct sockaddr_in6 *dstsock;
    239 	struct mbuf *control;
    240 	struct in6_addr *dst;
    241 	struct ip6_hdr *ip6;
    242 	struct in6pcb *in6p;
    243 	u_int	plen = m->m_pkthdr.len;
    244 	int error = 0;
    245 	struct ip6_pktopts opt, *optp = NULL;
    246 	struct ifnet *oifp = NULL;
    247 	int type, code;		/* for ICMPv6 output statistics only */
    248 	int priv;
    249 	va_list ap;
    250 
    251 	va_start(ap, m);
    252 	so = va_arg(ap, struct socket *);
    253 	dstsock = va_arg(ap, struct sockaddr_in6 *);
    254 	control = va_arg(ap, struct mbuf *);
    255 	va_end(ap);
    256 
    257 	in6p = sotoin6pcb(so);
    258 
    259 	priv = 0;
    260     {
    261 	struct proc *p = curproc;	/* XXX */
    262 
    263 	if (p && !suser(p->p_ucred, &p->p_acflag))
    264 		priv = 1;
    265     }
    266 	dst = &dstsock->sin6_addr;
    267 	if (control) {
    268 		if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
    269 			goto bad;
    270 		optp = &opt;
    271 	} else
    272 		optp = in6p->in6p_outputopts;
    273 
    274 	/*
    275 	 * For an ICMPv6 packet, we should know its type and code
    276 	 * to update statistics.
    277 	 */
    278 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    279 		struct icmp6_hdr *icmp6;
    280 		if (m->m_len < sizeof(struct icmp6_hdr) &&
    281 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
    282 			error = ENOBUFS;
    283 			goto bad;
    284 		}
    285 		icmp6 = mtod(m, struct icmp6_hdr *);
    286 		type = icmp6->icmp6_type;
    287 		code = icmp6->icmp6_code;
    288 	}
    289 
    290 	M_PREPEND(m, sizeof(*ip6), M_WAIT);
    291 	ip6 = mtod(m, struct ip6_hdr *);
    292 
    293 	/*
    294 	 * Next header might not be ICMP6 but use its pseudo header anyway.
    295 	 */
    296 	ip6->ip6_dst = *dst;
    297 
    298 	/*
    299 	 * If the scope of the destination is link-local, embed the interface
    300 	 * index in the address.
    301 	 *
    302 	 * XXX advanced-api value overrides sin6_scope_id
    303 	 */
    304 	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst) ||
    305 	    IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) {
    306 		struct in6_pktinfo *pi;
    307 
    308 		/*
    309 		 * XXX Boundary check is assumed to be already done in
    310 		 * ip6_setpktoptions().
    311 		 */
    312 		if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
    313 			ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
    314 			oifp = ifindex2ifnet[pi->ipi6_ifindex];
    315 		}
    316 		else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
    317 			 in6p->in6p_moptions &&
    318 			 in6p->in6p_moptions->im6o_multicast_ifp) {
    319 			oifp = in6p->in6p_moptions->im6o_multicast_ifp;
    320 			ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
    321 		} else if (dstsock->sin6_scope_id) {
    322 			/* boundary check */
    323 			if (dstsock->sin6_scope_id < 0
    324 			 || if_index < dstsock->sin6_scope_id) {
    325 				error = ENXIO;  /* XXX EINVAL? */
    326 				goto bad;
    327 			}
    328 			ip6->ip6_dst.s6_addr16[1]
    329 				= htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
    330 		}
    331 	}
    332 
    333 	/*
    334 	 * Source address selection.
    335 	 */
    336 	{
    337 		struct in6_addr *in6a;
    338 
    339 		if ((in6a = in6_selectsrc(dstsock, optp,
    340 					  in6p->in6p_moptions,
    341 					  &in6p->in6p_route,
    342 					  &in6p->in6p_laddr,
    343 					  &error)) == 0) {
    344 			if (error == 0)
    345 				error = EADDRNOTAVAIL;
    346 			goto bad;
    347 		}
    348 		ip6->ip6_src = *in6a;
    349 		if (in6p->in6p_route.ro_rt)
    350 			oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
    351 	}
    352 
    353 	ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    354 	ip6->ip6_vfc  = IPV6_VERSION;
    355 #if 0				/* ip6_plen will be filled in ip6_output. */
    356 	ip6->ip6_plen  = htons((u_short)plen);
    357 #endif
    358 	ip6->ip6_nxt   = in6p->in6p_ip6.ip6_nxt;
    359 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
    360 
    361 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
    362 	    in6p->in6p_cksum != -1) {
    363 		struct mbuf *n;
    364 		int off;
    365 		u_int16_t *p;
    366 
    367 #define	offsetof(type, member)	((size_t)(&((type *)0)->member)) /* XXX */
    368 
    369 		/* compute checksum */
    370 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
    371 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
    372 		else
    373 			off = in6p->in6p_cksum;
    374 		if (plen < off + 1) {
    375 			error = EINVAL;
    376 			goto bad;
    377 		}
    378 		off += sizeof(struct ip6_hdr);
    379 
    380 		n = m;
    381 		while (n && n->m_len <= off) {
    382 			off -= n->m_len;
    383 			n = n->m_next;
    384 		}
    385 		if (!n)
    386 			goto bad;
    387 		p = (u_int16_t *)(mtod(n, caddr_t) + off);
    388 		*p = 0;
    389 		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
    390 	}
    391 
    392 #ifdef IPSEC
    393 	m->m_pkthdr.rcvif = (struct ifnet *)so;
    394 #endif /*IPSEC*/
    395 
    396 	error = ip6_output(m, optp, &in6p->in6p_route, 0, in6p->in6p_moptions,
    397 			   &oifp);
    398 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    399 		if (oifp)
    400 			icmp6_ifoutstat_inc(oifp, type, code);
    401 		icmp6stat.icp6s_outhist[type]++;
    402 	}
    403 
    404 	goto freectl;
    405 
    406  bad:
    407 	if (m)
    408 		m_freem(m);
    409 
    410  freectl:
    411 	if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
    412 		RTFREE(optp->ip6po_route.ro_rt);
    413 	if (control)
    414 		m_freem(control);
    415 	return(error);
    416 }
    417 
    418 /*
    419  * Raw IPv6 socket option processing.
    420  */
    421 int
    422 rip6_ctloutput(op, so, level, optname, m)
    423 	int op;
    424 	struct socket *so;
    425 	int level, optname;
    426 	struct mbuf **m;
    427 {
    428 	int error = 0;
    429 
    430 	switch(level) {
    431 	 case IPPROTO_IPV6:
    432 		 switch(optname) {
    433 		  case MRT6_INIT:
    434 		  case MRT6_DONE:
    435 		  case MRT6_ADD_MIF:
    436 		  case MRT6_DEL_MIF:
    437 		  case MRT6_ADD_MFC:
    438 		  case MRT6_DEL_MFC:
    439 		  case MRT6_PIM:
    440 			  if (op == PRCO_SETOPT) {
    441 				  error = ip6_mrouter_set(optname, so, *m);
    442 				  if (*m)
    443 					  (void)m_free(*m);
    444 			  } else if (op == PRCO_GETOPT) {
    445 				  error = ip6_mrouter_get(optname, so, m);
    446 			  } else
    447 				  error = EINVAL;
    448 			  return (error);
    449 		 }
    450 		 return (ip6_ctloutput(op, so, level, optname, m));
    451 		 /* NOTREACHED */
    452 
    453 	 case IPPROTO_ICMPV6:
    454 		 /*
    455 		  * XXX: is it better to call icmp6_ctloutput() directly
    456 		  * from protosw?
    457 		  */
    458 		 return(icmp6_ctloutput(op, so, level, optname, m));
    459 
    460 	 default:
    461 		 if (op == PRCO_SETOPT && *m)
    462 			 (void)m_free(*m);
    463 		 return(EINVAL);
    464 	}
    465 }
    466 
    467 extern	u_long rip6_sendspace;
    468 extern	u_long rip6_recvspace;
    469 
    470 int
    471 rip6_usrreq(so, req, m, nam, control, p)
    472 	register struct socket *so;
    473 	int req;
    474 	struct mbuf *m, *nam, *control;
    475 	struct proc *p;
    476 {
    477 	register struct in6pcb *in6p = sotoin6pcb(so);
    478 	int s;
    479 	int error = 0;
    480 /*	extern	struct socket *ip6_mrouter; */ /* xxx */
    481 	int priv;
    482 
    483 	priv = 0;
    484 	if (p && !suser(p->p_ucred, &p->p_acflag))
    485 		priv++;
    486 
    487 	if (req == PRU_CONTROL)
    488 		return (in6_control(so, (u_long)m, (caddr_t)nam,
    489 				    (struct ifnet *)control, p));
    490 
    491 	switch (req) {
    492 	case PRU_ATTACH:
    493 		if (in6p)
    494 			panic("rip6_attach");
    495 		if (!priv) {
    496 			error = EACCES;
    497 			break;
    498 		}
    499 		s = splsoftnet();
    500 		if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) ||
    501 		    (error = in6_pcballoc(so, &rawin6pcb))) {
    502 			splx(s);
    503 			break;
    504 		}
    505 		splx(s);
    506 		in6p = sotoin6pcb(so);
    507 		in6p->in6p_ip6.ip6_nxt = (long)nam;
    508 		in6p->in6p_cksum = -1;
    509 #ifdef IPSEC
    510 		error = ipsec_init_policy(&in6p->in6p_sp);
    511 		if (error != 0) {
    512 			in6_pcbdetach(in6p);
    513 			break;
    514 		}
    515 #endif /*IPSEC*/
    516 
    517 		MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
    518 			sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
    519 		if (in6p->in6p_icmp6filt == NULL) {
    520 			in6_pcbdetach(in6p);
    521 			error = ENOMEM;
    522 			break;
    523 		}
    524 		ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
    525 		break;
    526 
    527 	case PRU_DISCONNECT:
    528 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    529 			error = ENOTCONN;
    530 			break;
    531 		}
    532 		in6p->in6p_faddr = in6addr_any;
    533 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
    534 		break;
    535 
    536 	case PRU_ABORT:
    537 		soisdisconnected(so);
    538 		/* Fallthrough */
    539 	case PRU_DETACH:
    540 		if (in6p == 0)
    541 			panic("rip6_detach");
    542 		if (so == ip6_mrouter)
    543 			ip6_mrouter_done();
    544 		/* xxx: RSVP */
    545 		if (in6p->in6p_icmp6filt) {
    546 			FREE(in6p->in6p_icmp6filt, M_PCB);
    547 			in6p->in6p_icmp6filt = NULL;
    548 		}
    549 		in6_pcbdetach(in6p);
    550 		break;
    551 
    552 	case PRU_BIND:
    553 	    {
    554 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    555 		struct ifaddr *ia = NULL;
    556 
    557 		if (nam->m_len != sizeof(*addr)) {
    558 			error = EINVAL;
    559 			break;
    560 		}
    561 		if ((ifnet.tqh_first == 0) ||
    562 		   (addr->sin6_family != AF_INET6) ||
    563 		   (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
    564 		    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)) {
    565 			error = EADDRNOTAVAIL;
    566 			break;
    567 		}
    568 		if (ia &&
    569 		    ((struct in6_ifaddr *)ia)->ia6_flags &
    570 		    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    571 		     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
    572 			error = EADDRNOTAVAIL;
    573 			break;
    574 		}
    575 		in6p->in6p_laddr = addr->sin6_addr;
    576 		break;
    577 	    }
    578 
    579 	case PRU_CONNECT:
    580 	    {
    581 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    582 		struct in6_addr *in6a = NULL;
    583 
    584 		if (nam->m_len != sizeof(*addr)) {
    585 			error = EINVAL;
    586 			break;
    587 		}
    588 		if (ifnet.tqh_first == 0) {
    589 			error = EADDRNOTAVAIL;
    590 			break;
    591 		}
    592 		if (addr->sin6_family != AF_INET6) {
    593 			error = EAFNOSUPPORT;
    594 			break;
    595 		}
    596 
    597 		/* Source address selection. XXX: need pcblookup? */
    598 		in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
    599 				     in6p->in6p_moptions,
    600 				     &in6p->in6p_route,
    601 				     &in6p->in6p_laddr,
    602 				     &error);
    603 		if (in6a == NULL) {
    604 			if (error == 0)
    605 				error = EADDRNOTAVAIL;
    606 			break;
    607 		}
    608 		in6p->in6p_laddr = *in6a;
    609 		in6p->in6p_faddr = addr->sin6_addr;
    610 		soisconnected(so);
    611 		break;
    612 	    }
    613 
    614 	case PRU_CONNECT2:
    615 		error = EOPNOTSUPP;
    616 		break;
    617 
    618 	/*
    619 	 * Mark the connection as being incapable of futther input.
    620 	 */
    621 	case PRU_SHUTDOWN:
    622 		socantsendmore(so);
    623 		break;
    624 	/*
    625 	 * Ship a packet out. The appropriate raw output
    626 	 * routine handles any messaging necessary.
    627 	 */
    628 	case PRU_SEND:
    629 	    {
    630 		struct sockaddr_in6 tmp;
    631 		struct sockaddr_in6 *dst;
    632 
    633 		if (so->so_state & SS_ISCONNECTED) {
    634 			if (nam) {
    635 				error = EISCONN;
    636 				break;
    637 			}
    638 			/* XXX */
    639 			bzero(&tmp, sizeof(tmp));
    640 			tmp.sin6_family = AF_INET6;
    641 			tmp.sin6_len = sizeof(struct sockaddr_in6);
    642 			bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
    643 				sizeof(struct in6_addr));
    644 			dst = &tmp;
    645 		} else {
    646 			if (nam == NULL) {
    647 				error = ENOTCONN;
    648 				break;
    649 			}
    650 			dst = mtod(nam, struct sockaddr_in6 *);
    651 		}
    652 		error = rip6_output(m, so, dst, control);
    653 		m = NULL;
    654 		break;
    655 	    }
    656 
    657 	case PRU_SENSE:
    658 		/*
    659 		 * stat: don't bother with a blocksize
    660 		 */
    661 		return(0);
    662 	/*
    663 	 * Not supported.
    664 	 */
    665 	case PRU_RCVOOB:
    666 	case PRU_RCVD:
    667 	case PRU_LISTEN:
    668 	case PRU_ACCEPT:
    669 	case PRU_SENDOOB:
    670 		error = EOPNOTSUPP;
    671 		break;
    672 
    673 	case PRU_SOCKADDR:
    674 		in6_setsockaddr(in6p, nam);
    675 		break;
    676 
    677 	case PRU_PEERADDR:
    678 		in6_setpeeraddr(in6p, nam);
    679 		break;
    680 
    681 	default:
    682 		panic("rip6_usrreq");
    683 	}
    684 	if (m != NULL)
    685 		m_freem(m);
    686 	return(error);
    687 }
    688