Home | History | Annotate | Line # | Download | only in netinet6
raw_ip6.c revision 1.54
      1 /*	$NetBSD: raw_ip6.c,v 1.54 2003/06/29 22:32:02 fvdl Exp $	*/
      2 /*	$KAME: raw_ip6.c,v 1.82 2001/07/23 18:57:56 jinmei 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 <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: raw_ip6.c,v 1.54 2003/06/29 22:32:02 fvdl Exp $");
     70 
     71 #include "opt_ipsec.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/malloc.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/socket.h>
     77 #include <sys/protosw.h>
     78 #include <sys/socketvar.h>
     79 #include <sys/errno.h>
     80 #include <sys/systm.h>
     81 #include <sys/proc.h>
     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 <netinet/ip6.h>
     90 #include <netinet6/ip6_var.h>
     91 #include <netinet6/ip6_mroute.h>
     92 #include <netinet/icmp6.h>
     93 #include <netinet6/in6_pcb.h>
     94 #include <netinet6/nd6.h>
     95 #include <netinet6/ip6protosw.h>
     96 #ifdef ENABLE_DEFAULT_SCOPE
     97 #include <netinet6/scope6_var.h>
     98 #endif
     99 #include <netinet6/raw_ip6.h>
    100 
    101 #ifdef IPSEC
    102 #include <netinet6/ipsec.h>
    103 #endif /* IPSEC */
    104 
    105 #include <machine/stdarg.h>
    106 
    107 #include "faith.h"
    108 #if defined(NFAITH) && 0 < NFAITH
    109 #include <net/if_faith.h>
    110 #endif
    111 
    112 struct	in6pcb rawin6pcb;
    113 #define ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
    114 
    115 /*
    116  * Raw interface to IP6 protocol.
    117  */
    118 
    119 struct rip6stat rip6stat;
    120 
    121 /*
    122  * Initialize raw connection block queue.
    123  */
    124 void
    125 rip6_init()
    126 {
    127 
    128 	rawin6pcb.in6p_next = rawin6pcb.in6p_prev = &rawin6pcb;
    129 }
    130 
    131 /*
    132  * Setup generic address and protocol structures
    133  * for raw_input routine, then pass them along with
    134  * mbuf chain.
    135  */
    136 int
    137 rip6_input(mp, offp, proto)
    138 	struct	mbuf **mp;
    139 	int	*offp, proto;
    140 {
    141 	struct mbuf *m = *mp;
    142 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    143 	struct in6pcb *in6p;
    144 	struct in6pcb *last = NULL;
    145 	struct sockaddr_in6 rip6src;
    146 	struct mbuf *opts = NULL;
    147 
    148 	rip6stat.rip6s_ipackets++;
    149 
    150 #if defined(NFAITH) && 0 < NFAITH
    151 	if (faithprefix(&ip6->ip6_dst)) {
    152 		/* send icmp6 host unreach? */
    153 		m_freem(m);
    154 		return IPPROTO_DONE;
    155 	}
    156 #endif
    157 
    158 	/* Be proactive about malicious use of IPv4 mapped address */
    159 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    160 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    161 		/* XXX stat */
    162 		m_freem(m);
    163 		return IPPROTO_DONE;
    164 	}
    165 
    166 	bzero(&rip6src, sizeof(rip6src));
    167 	rip6src.sin6_len = sizeof(struct sockaddr_in6);
    168 	rip6src.sin6_family = AF_INET6;
    169 #if 0 /* XXX inbound flowlabel */
    170 	rip6src.sin6_flowinfo = ip6->ip6_flow & IPV6_FLOWINFO_MASK;
    171 #endif
    172 	/* KAME hack: recover scopeid */
    173 	(void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
    174 
    175 	for (in6p = rawin6pcb.in6p_next;
    176 	     in6p != &rawin6pcb; in6p = in6p->in6p_next)
    177 	{
    178 		if (in6p->in6p_ip6.ip6_nxt &&
    179 		    in6p->in6p_ip6.ip6_nxt != proto)
    180 			continue;
    181 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
    182 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
    183 			continue;
    184 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
    185 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
    186 			continue;
    187 		if (in6p->in6p_cksum != -1) {
    188 			rip6stat.rip6s_isum++;
    189 			if (in6_cksum(m, ip6->ip6_nxt, *offp,
    190 			    m->m_pkthdr.len - *offp)) {
    191 				rip6stat.rip6s_badsum++;
    192 				continue;
    193 			}
    194 		}
    195 		if (last) {
    196 			struct	mbuf *n;
    197 
    198 #ifdef IPSEC
    199 			/*
    200 			 * Check AH/ESP integrity.
    201 			 */
    202 			if (ipsec6_in_reject(m, last)) {
    203 				ipsec6stat.in_polvio++;
    204 				/* do not inject data into pcb */
    205 			} else
    206 #endif /* IPSEC */
    207 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
    208 				if (last->in6p_flags & IN6P_CONTROLOPTS)
    209 					ip6_savecontrol(last, &opts, ip6, n);
    210 				/* strip intermediate headers */
    211 				m_adj(n, *offp);
    212 				if (sbappendaddr(&last->in6p_socket->so_rcv,
    213 				    (struct sockaddr *)&rip6src, n, opts) == 0) {
    214 					/* should notify about lost packet */
    215 					m_freem(n);
    216 					if (opts)
    217 						m_freem(opts);
    218 					rip6stat.rip6s_fullsock++;
    219 				} else
    220 					sorwakeup(last->in6p_socket);
    221 				opts = NULL;
    222 			}
    223 		}
    224 		last = in6p;
    225 	}
    226 #ifdef IPSEC
    227 	/*
    228 	 * Check AH/ESP integrity.
    229 	 */
    230 	if (last && ipsec6_in_reject(m, last)) {
    231 		m_freem(m);
    232 		ipsec6stat.in_polvio++;
    233 		ip6stat.ip6s_delivered--;
    234 		/* do not inject data into pcb */
    235 	} else
    236 #endif /* IPSEC */
    237 	if (last) {
    238 		if (last->in6p_flags & IN6P_CONTROLOPTS)
    239 			ip6_savecontrol(last, &opts, ip6, m);
    240 		/* strip intermediate headers */
    241 		m_adj(m, *offp);
    242 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    243 		    (struct sockaddr *)&rip6src, m, opts) == 0) {
    244 			m_freem(m);
    245 			if (opts)
    246 				m_freem(opts);
    247 			rip6stat.rip6s_fullsock++;
    248 		} else
    249 			sorwakeup(last->in6p_socket);
    250 	} else {
    251 		rip6stat.rip6s_nosock++;
    252 		if (m->m_flags & M_MCAST)
    253 			rip6stat.rip6s_nosockmcast++;
    254 		if (proto == IPPROTO_NONE)
    255 			m_freem(m);
    256 		else {
    257 			u_int8_t *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
    258 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
    259 			icmp6_error(m, ICMP6_PARAM_PROB,
    260 			    ICMP6_PARAMPROB_NEXTHEADER,
    261 			    prvnxtp - mtod(m, u_int8_t *));
    262 		}
    263 		ip6stat.ip6s_delivered--;
    264 	}
    265 	return IPPROTO_DONE;
    266 }
    267 
    268 void
    269 rip6_ctlinput(cmd, sa, d)
    270 	int cmd;
    271 	struct sockaddr *sa;
    272 	void *d;
    273 {
    274 	struct ip6_hdr *ip6;
    275 	struct mbuf *m;
    276 	int off;
    277 	struct ip6ctlparam *ip6cp = NULL;
    278 	const struct sockaddr_in6 *sa6_src = NULL;
    279 	void *cmdarg;
    280 	void (*notify) __P((struct in6pcb *, int)) = in6_rtchange;
    281 	int nxt;
    282 
    283 	if (sa->sa_family != AF_INET6 ||
    284 	    sa->sa_len != sizeof(struct sockaddr_in6))
    285 		return;
    286 
    287 	if ((unsigned)cmd >= PRC_NCMDS)
    288 		return;
    289 	if (PRC_IS_REDIRECT(cmd))
    290 		notify = in6_rtchange, d = NULL;
    291 	else if (cmd == PRC_HOSTDEAD)
    292 		d = NULL;
    293 	else if (cmd == PRC_MSGSIZE)
    294 		; /* special code is present, see below */
    295 	else if (inet6ctlerrmap[cmd] == 0)
    296 		return;
    297 
    298 	/* if the parameter is from icmp6, decode it. */
    299 	if (d != NULL) {
    300 		ip6cp = (struct ip6ctlparam *)d;
    301 		m = ip6cp->ip6c_m;
    302 		ip6 = ip6cp->ip6c_ip6;
    303 		off = ip6cp->ip6c_off;
    304 		cmdarg = ip6cp->ip6c_cmdarg;
    305 		sa6_src = ip6cp->ip6c_src;
    306 		nxt = ip6cp->ip6c_nxt;
    307 	} else {
    308 		m = NULL;
    309 		ip6 = NULL;
    310 		cmdarg = NULL;
    311 		sa6_src = &sa6_any;
    312 		nxt = -1;
    313 	}
    314 
    315 	if (ip6 && cmd == PRC_MSGSIZE) {
    316 		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
    317 		int valid = 0;
    318 		struct in6pcb *in6p;
    319 
    320 		/*
    321 		 * Check to see if we have a valid raw IPv6 socket
    322 		 * corresponding to the address in the ICMPv6 message
    323 		 * payload, and the protocol (ip6_nxt) meets the socket.
    324 		 * XXX chase extension headers, or pass final nxt value
    325 		 * from icmp6_notify_error()
    326 		 */
    327 		in6p = NULL;
    328 		in6p = in6_pcblookup_connect(&rawin6pcb, &sa6->sin6_addr, 0,
    329 		    (struct in6_addr *)&sa6_src->sin6_addr, 0, 0);
    330 #if 0
    331 		if (!in6p) {
    332 			/*
    333 			 * As the use of sendto(2) is fairly popular,
    334 			 * we may want to allow non-connected pcb too.
    335 			 * But it could be too weak against attacks...
    336 			 * We should at least check if the local
    337 			 * address (= s) is really ours.
    338 			 */
    339 			in6p = in6_pcblookup_bind(&rawin6pcb,
    340 			    &sa6->sin6_addr, 0, 0))
    341 		}
    342 #endif
    343 
    344 		if (in6p && in6p->in6p_ip6.ip6_nxt &&
    345 		    in6p->in6p_ip6.ip6_nxt == nxt)
    346 			valid++;
    347 
    348 		/*
    349 		 * Depending on the value of "valid" and routing table
    350 		 * size (mtudisc_{hi,lo}wat), we will:
    351 		 * - recalculate the new MTU and create the
    352 		 *   corresponding routing entry, or
    353 		 * - ignore the MTU change notification.
    354 		 */
    355 		icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    356 
    357 		/*
    358 		 * regardless of if we called icmp6_mtudisc_update(),
    359 		 * we need to call in6_pcbnotify(), to notify path
    360 		 * MTU change to the userland (2292bis-02), because
    361 		 * some unconnected sockets may share the same
    362 		 * destination and want to know the path MTU.
    363 		 */
    364 	}
    365 
    366 	(void) in6_pcbnotify(&rawin6pcb, sa, 0,
    367 	    (struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
    368 }
    369 
    370 /*
    371  * Generate IPv6 header and pass packet to ip6_output.
    372  * Tack on options user may have setup with control call.
    373  */
    374 int
    375 #if __STDC__
    376 rip6_output(struct mbuf *m, ...)
    377 #else
    378 rip6_output(m, va_alist)
    379 	struct mbuf *m;
    380 	va_dcl
    381 #endif
    382 {
    383 	struct socket *so;
    384 	struct sockaddr_in6 *dstsock;
    385 	struct mbuf *control;
    386 	struct in6_addr *dst;
    387 	struct ip6_hdr *ip6;
    388 	struct in6pcb *in6p;
    389 	u_int	plen = m->m_pkthdr.len;
    390 	int error = 0;
    391 	struct ip6_pktopts opt, *optp = NULL, *origoptp;
    392 	struct ifnet *oifp = NULL;
    393 	int type, code;		/* for ICMPv6 output statistics only */
    394 	int priv = 0;
    395 	va_list ap;
    396 	int flags;
    397 
    398 	va_start(ap, m);
    399 	so = va_arg(ap, struct socket *);
    400 	dstsock = va_arg(ap, struct sockaddr_in6 *);
    401 	control = va_arg(ap, struct mbuf *);
    402 	va_end(ap);
    403 
    404 	in6p = sotoin6pcb(so);
    405 
    406 	priv = 0;
    407     {
    408 	struct proc *p = curproc;	/* XXX */
    409 
    410 	if (p && !suser(p->p_ucred, &p->p_acflag))
    411 		priv = 1;
    412     }
    413 	dst = &dstsock->sin6_addr;
    414 	if (control) {
    415 		if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
    416 			goto bad;
    417 		optp = &opt;
    418 	} else
    419 		optp = in6p->in6p_outputopts;
    420 
    421 	/*
    422 	 * For an ICMPv6 packet, we should know its type and code
    423 	 * to update statistics.
    424 	 */
    425 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    426 		struct icmp6_hdr *icmp6;
    427 		if (m->m_len < sizeof(struct icmp6_hdr) &&
    428 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
    429 			error = ENOBUFS;
    430 			goto bad;
    431 		}
    432 		icmp6 = mtod(m, struct icmp6_hdr *);
    433 		type = icmp6->icmp6_type;
    434 		code = icmp6->icmp6_code;
    435 	}
    436 
    437 	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
    438 	if (!m) {
    439 		error = ENOBUFS;
    440 		goto bad;
    441 	}
    442 	ip6 = mtod(m, struct ip6_hdr *);
    443 
    444 	/*
    445 	 * Next header might not be ICMP6 but use its pseudo header anyway.
    446 	 */
    447 	ip6->ip6_dst = *dst;
    448 
    449 	/* KAME hack: embed scopeid */
    450 	origoptp = in6p->in6p_outputopts;
    451 	in6p->in6p_outputopts = optp;
    452 	if (in6_embedscope(&ip6->ip6_dst, dstsock, in6p, &oifp) != 0) {
    453 		error = EINVAL;
    454 		goto bad;
    455 	}
    456 	in6p->in6p_outputopts = origoptp;
    457 
    458 	/*
    459 	 * Source address selection.
    460 	 */
    461 	{
    462 		struct in6_addr *in6a;
    463 
    464 		if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions,
    465 		    &in6p->in6p_route, &in6p->in6p_laddr, &error)) == 0) {
    466 			if (error == 0)
    467 				error = EADDRNOTAVAIL;
    468 			goto bad;
    469 		}
    470 		ip6->ip6_src = *in6a;
    471 		if (in6p->in6p_route.ro_rt) {
    472 			/* what if oifp contradicts ? */
    473 			oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
    474 		}
    475 	}
    476 
    477 	ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    478 	ip6->ip6_vfc  &= ~IPV6_VERSION_MASK;
    479 	ip6->ip6_vfc  |= IPV6_VERSION;
    480 #if 0				/* ip6_plen will be filled in ip6_output. */
    481 	ip6->ip6_plen  = htons((u_short)plen);
    482 #endif
    483 	ip6->ip6_nxt   = in6p->in6p_ip6.ip6_nxt;
    484 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
    485 
    486 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
    487 	    in6p->in6p_cksum != -1) {
    488 		int off;
    489 		u_int16_t sum;
    490 
    491 		/* compute checksum */
    492 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
    493 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
    494 		else
    495 			off = in6p->in6p_cksum;
    496 		if (plen < off + 1) {
    497 			error = EINVAL;
    498 			goto bad;
    499 		}
    500 		off += sizeof(struct ip6_hdr);
    501 
    502 		sum = 0;
    503 		m_copyback(m, off, sizeof(sum), (caddr_t)&sum);
    504 		sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
    505 		m_copyback(m, off, sizeof(sum), (caddr_t)&sum);
    506 	}
    507 
    508 #ifdef IPSEC
    509 	if (ipsec_setsocket(m, so) != 0) {
    510 		error = ENOBUFS;
    511 		goto bad;
    512 	}
    513 #endif /* IPSEC */
    514 
    515 	flags = 0;
    516 #ifdef IN6P_MINMTU
    517 	if (in6p->in6p_flags & IN6P_MINMTU)
    518 		flags |= IPV6_MINMTU;
    519 #endif
    520 
    521 	error = ip6_output(m, optp, &in6p->in6p_route, flags,
    522 	    in6p->in6p_moptions, &oifp);
    523 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    524 		if (oifp)
    525 			icmp6_ifoutstat_inc(oifp, type, code);
    526 		icmp6stat.icp6s_outhist[type]++;
    527 	} else
    528 		rip6stat.rip6s_opackets++;
    529 
    530 	goto freectl;
    531 
    532  bad:
    533 	if (m)
    534 		m_freem(m);
    535 
    536  freectl:
    537 	if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
    538 		RTFREE(optp->ip6po_route.ro_rt);
    539 	if (control)
    540 		m_freem(control);
    541 	return (error);
    542 }
    543 
    544 /*
    545  * Raw IPv6 socket option processing.
    546  */
    547 int
    548 rip6_ctloutput(op, so, level, optname, mp)
    549 	int op;
    550 	struct socket *so;
    551 	int level, optname;
    552 	struct mbuf **mp;
    553 {
    554 	int error = 0;
    555 
    556 	switch (level) {
    557 	case IPPROTO_IPV6:
    558 		switch (optname) {
    559 		case MRT6_INIT:
    560 		case MRT6_DONE:
    561 		case MRT6_ADD_MIF:
    562 		case MRT6_DEL_MIF:
    563 		case MRT6_ADD_MFC:
    564 		case MRT6_DEL_MFC:
    565 		case MRT6_PIM:
    566 			if (op == PRCO_SETOPT) {
    567 				error = ip6_mrouter_set(optname, so, *mp);
    568 				if (*mp)
    569 					(void)m_free(*mp);
    570 			} else if (op == PRCO_GETOPT)
    571 				error = ip6_mrouter_get(optname, so, mp);
    572 			else
    573 				error = EINVAL;
    574 			return (error);
    575 		case IPV6_CHECKSUM:
    576 			return (ip6_raw_ctloutput(op, so, level, optname, mp));
    577 		default:
    578 			return (ip6_ctloutput(op, so, level, optname, mp));
    579 		}
    580 
    581 	case IPPROTO_ICMPV6:
    582 		/*
    583 		 * XXX: is it better to call icmp6_ctloutput() directly
    584 		 * from protosw?
    585 		 */
    586 		return (icmp6_ctloutput(op, so, level, optname, mp));
    587 
    588 	default:
    589 		if (op == PRCO_SETOPT && *mp)
    590 			m_free(*mp);
    591 		return EINVAL;
    592 	}
    593 }
    594 
    595 extern	u_long rip6_sendspace;
    596 extern	u_long rip6_recvspace;
    597 
    598 int
    599 rip6_usrreq(so, req, m, nam, control, p)
    600 	struct socket *so;
    601 	int req;
    602 	struct mbuf *m, *nam, *control;
    603 	struct proc *p;
    604 {
    605 	struct in6pcb *in6p = sotoin6pcb(so);
    606 	int s;
    607 	int error = 0;
    608 	int priv;
    609 
    610 	priv = 0;
    611 	if (p && !suser(p->p_ucred, &p->p_acflag))
    612 		priv++;
    613 
    614 	if (req == PRU_CONTROL)
    615 		return (in6_control(so, (u_long)m, (caddr_t)nam,
    616 		    (struct ifnet *)control, p));
    617 
    618 	if (req == PRU_PURGEIF) {
    619 		in6_pcbpurgeif0(&rawin6pcb, (struct ifnet *)control);
    620 		in6_purgeif((struct ifnet *)control);
    621 		in6_pcbpurgeif(&rawin6pcb, (struct ifnet *)control);
    622 		return (0);
    623 	}
    624 
    625 	switch (req) {
    626 	case PRU_ATTACH:
    627 		if (in6p)
    628 			panic("rip6_attach");
    629 		if (!priv) {
    630 			error = EACCES;
    631 			break;
    632 		}
    633 		s = splsoftnet();
    634 		if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) != 0) {
    635 			splx(s);
    636 			break;
    637 		}
    638 		if ((error = in6_pcballoc(so, &rawin6pcb)) != 0)
    639 		{
    640 			splx(s);
    641 			break;
    642 		}
    643 		splx(s);
    644 		in6p = sotoin6pcb(so);
    645 		in6p->in6p_ip6.ip6_nxt = (long)nam;
    646 		in6p->in6p_cksum = -1;
    647 
    648 		MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
    649 		    sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
    650 		if (in6p->in6p_icmp6filt == NULL) {
    651 			in6_pcbdetach(in6p);
    652 			error = ENOMEM;
    653 			break;
    654 		}
    655 		ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
    656 		break;
    657 
    658 	case PRU_DISCONNECT:
    659 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    660 			error = ENOTCONN;
    661 			break;
    662 		}
    663 		in6p->in6p_faddr = in6addr_any;
    664 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
    665 		break;
    666 
    667 	case PRU_ABORT:
    668 		soisdisconnected(so);
    669 		/* Fallthrough */
    670 	case PRU_DETACH:
    671 		if (in6p == 0)
    672 			panic("rip6_detach");
    673 		if (so == ip6_mrouter)
    674 			ip6_mrouter_done();
    675 		/* xxx: RSVP */
    676 		if (in6p->in6p_icmp6filt) {
    677 			FREE(in6p->in6p_icmp6filt, M_PCB);
    678 			in6p->in6p_icmp6filt = NULL;
    679 		}
    680 		in6_pcbdetach(in6p);
    681 		break;
    682 
    683 	case PRU_BIND:
    684 	    {
    685 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    686 		struct ifaddr *ia = NULL;
    687 
    688 		if (nam->m_len != sizeof(*addr)) {
    689 			error = EINVAL;
    690 			break;
    691 		}
    692 		if ((ifnet.tqh_first == 0) || (addr->sin6_family != AF_INET6)) {
    693 			error = EADDRNOTAVAIL;
    694 			break;
    695 		}
    696 #ifdef ENABLE_DEFAULT_SCOPE
    697 		if (addr->sin6_scope_id == 0)	/* not change if specified  */
    698 			addr->sin6_scope_id =
    699 			    scope6_addr2default(&addr->sin6_addr);
    700 #endif
    701 		/* KAME hack: embed scopeid */
    702 		if (in6_embedscope(&addr->sin6_addr, addr, in6p, NULL) != 0)
    703 			return EINVAL;
    704 #ifndef SCOPEDROUTING
    705 		addr->sin6_scope_id = 0; /* for ifa_ifwithaddr */
    706 #endif
    707 
    708 		/*
    709 		 * we don't support mapped address here, it would confuse
    710 		 * users so reject it
    711 		 */
    712 		if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
    713 			error = EADDRNOTAVAIL;
    714 			break;
    715 		}
    716 		if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
    717 		    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
    718 			error = EADDRNOTAVAIL;
    719 			break;
    720 		}
    721 		if (ia && ((struct in6_ifaddr *)ia)->ia6_flags &
    722 		    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    723 		     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
    724 			error = EADDRNOTAVAIL;
    725 			break;
    726 		}
    727 		in6p->in6p_laddr = addr->sin6_addr;
    728 		break;
    729 	    }
    730 
    731 	case PRU_CONNECT:
    732 	{
    733 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    734 		struct in6_addr *in6a = NULL;
    735 #ifdef ENABLE_DEFAULT_SCOPE
    736 		struct sockaddr_in6 sin6;
    737 #endif
    738 
    739 		if (nam->m_len != sizeof(*addr)) {
    740 			error = EINVAL;
    741 			break;
    742 		}
    743 		if (ifnet.tqh_first == 0)
    744 		{
    745 			error = EADDRNOTAVAIL;
    746 			break;
    747 		}
    748 		if (addr->sin6_family != AF_INET6) {
    749 			error = EAFNOSUPPORT;
    750 			break;
    751 		}
    752 
    753 #ifdef ENABLE_DEFAULT_SCOPE
    754 		if (addr->sin6_scope_id == 0) {
    755 			/* protect *addr */
    756 			sin6 = *addr;
    757 			addr = &sin6;
    758 			addr->sin6_scope_id =
    759 			    scope6_addr2default(&addr->sin6_addr);
    760 		}
    761 #endif
    762 
    763 		/* Source address selection. XXX: need pcblookup? */
    764 		in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
    765 		    in6p->in6p_moptions, &in6p->in6p_route,
    766 		    &in6p->in6p_laddr, &error);
    767 		if (in6a == NULL) {
    768 			if (error == 0)
    769 				error = EADDRNOTAVAIL;
    770 			break;
    771 		}
    772 		in6p->in6p_laddr = *in6a;
    773 		in6p->in6p_faddr = addr->sin6_addr;
    774 		soisconnected(so);
    775 		break;
    776 	}
    777 
    778 	case PRU_CONNECT2:
    779 		error = EOPNOTSUPP;
    780 		break;
    781 
    782 	/*
    783 	 * Mark the connection as being incapable of futther input.
    784 	 */
    785 	case PRU_SHUTDOWN:
    786 		socantsendmore(so);
    787 		break;
    788 	/*
    789 	 * Ship a packet out. The appropriate raw output
    790 	 * routine handles any messaging necessary.
    791 	 */
    792 	case PRU_SEND:
    793 	{
    794 		struct sockaddr_in6 tmp;
    795 		struct sockaddr_in6 *dst;
    796 
    797 		/* always copy sockaddr to avoid overwrites */
    798 		if (so->so_state & SS_ISCONNECTED) {
    799 			if (nam) {
    800 				error = EISCONN;
    801 				break;
    802 			}
    803 			/* XXX */
    804 			bzero(&tmp, sizeof(tmp));
    805 			tmp.sin6_family = AF_INET6;
    806 			tmp.sin6_len = sizeof(struct sockaddr_in6);
    807 			bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
    808 			    sizeof(struct in6_addr));
    809 			dst = &tmp;
    810 		} else {
    811 			if (nam == NULL) {
    812 				error = ENOTCONN;
    813 				break;
    814 			}
    815 			if (nam->m_len != sizeof(tmp)) {
    816 				error = EINVAL;
    817 				break;
    818 			}
    819 
    820 			tmp = *mtod(nam, struct sockaddr_in6 *);
    821 			dst = &tmp;
    822 
    823 			if (dst->sin6_family != AF_INET6) {
    824 				error = EAFNOSUPPORT;
    825 				break;
    826 			}
    827 		}
    828 #ifdef ENABLE_DEFAULT_SCOPE
    829 		if (dst->sin6_scope_id == 0) {
    830 			dst->sin6_scope_id =
    831 			    scope6_addr2default(&dst->sin6_addr);
    832 		}
    833 #endif
    834 		error = rip6_output(m, so, dst, control);
    835 		m = NULL;
    836 		break;
    837 	}
    838 
    839 	case PRU_SENSE:
    840 		/*
    841 		 * stat: don't bother with a blocksize
    842 		 */
    843 		return (0);
    844 	/*
    845 	 * Not supported.
    846 	 */
    847 	case PRU_RCVOOB:
    848 	case PRU_RCVD:
    849 	case PRU_LISTEN:
    850 	case PRU_ACCEPT:
    851 	case PRU_SENDOOB:
    852 		error = EOPNOTSUPP;
    853 		break;
    854 
    855 	case PRU_SOCKADDR:
    856 		in6_setsockaddr(in6p, nam);
    857 		break;
    858 
    859 	case PRU_PEERADDR:
    860 		in6_setpeeraddr(in6p, nam);
    861 		break;
    862 
    863 	default:
    864 		panic("rip6_usrreq");
    865 	}
    866 	if (m != NULL)
    867 		m_freem(m);
    868 	return (error);
    869 }
    870