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