Home | History | Annotate | Line # | Download | only in netinet6
raw_ip6.c revision 1.87
      1 /*	$NetBSD: raw_ip6.c,v 1.87 2007/09/19 04:33:44 dyoung 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. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  *
     61  *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: raw_ip6.c,v 1.87 2007/09/19 04:33:44 dyoung Exp $");
     66 
     67 #include "opt_ipsec.h"
     68 
     69 #include <sys/param.h>
     70 #include <sys/sysctl.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 #include <sys/kauth.h>
     80 
     81 #include <net/if.h>
     82 #include <net/route.h>
     83 #include <net/if_types.h>
     84 
     85 #include <netinet/in.h>
     86 #include <netinet/in_var.h>
     87 #include <netinet/ip6.h>
     88 #include <netinet6/ip6_var.h>
     89 #include <netinet6/ip6_mroute.h>
     90 #include <netinet/icmp6.h>
     91 #include <netinet6/in6_pcb.h>
     92 #include <netinet6/nd6.h>
     93 #include <netinet6/ip6protosw.h>
     94 #include <netinet6/scope6_var.h>
     95 #include <netinet6/raw_ip6.h>
     96 
     97 #ifdef IPSEC
     98 #include <netinet6/ipsec.h>
     99 #endif /* IPSEC */
    100 
    101 #ifdef FAST_IPSEC
    102 #include <netipsec/ipsec.h>
    103 #include <netipsec/ipsec_var.h> /* XXX ipsecstat namespace */
    104 #include <netipsec/ipsec6.h>
    105 #endif
    106 
    107 #include "faith.h"
    108 #if defined(NFAITH) && 0 < NFAITH
    109 #include <net/if_faith.h>
    110 #endif
    111 
    112 extern struct inpcbtable rawcbtable;
    113 struct	inpcbtable raw6cbtable;
    114 #define ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
    115 
    116 /*
    117  * Raw interface to IP6 protocol.
    118  */
    119 
    120 struct rip6stat rip6stat;
    121 
    122 /*
    123  * Initialize raw connection block queue.
    124  */
    125 void
    126 rip6_init()
    127 {
    128 
    129 	in6_pcbinit(&raw6cbtable, 1, 1);
    130 }
    131 
    132 /*
    133  * Setup generic address and protocol structures
    134  * for raw_input routine, then pass them along with
    135  * mbuf chain.
    136  */
    137 int
    138 rip6_input(struct mbuf **mp, int *offp, int proto)
    139 {
    140 	struct mbuf *m = *mp;
    141 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    142 	struct inpcb_hdr *inph;
    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 	rip6src.sin6_addr = ip6->ip6_src;
    170 	if (sa6_recoverscope(&rip6src) != 0) {
    171 		/* XXX: should be impossible. */
    172 		m_freem(m);
    173 		return IPPROTO_DONE;
    174 	}
    175 
    176 	CIRCLEQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) {
    177 		in6p = (struct in6pcb *)inph;
    178 		if (in6p->in6p_af != AF_INET6)
    179 			continue;
    180 		if (in6p->in6p_ip6.ip6_nxt &&
    181 		    in6p->in6p_ip6.ip6_nxt != proto)
    182 			continue;
    183 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
    184 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
    185 			continue;
    186 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
    187 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
    188 			continue;
    189 		if (in6p->in6p_cksum != -1) {
    190 			rip6stat.rip6s_isum++;
    191 			if (in6_cksum(m, proto, *offp,
    192 			    m->m_pkthdr.len - *offp)) {
    193 				rip6stat.rip6s_badsum++;
    194 				continue;
    195 			}
    196 		}
    197 		if (last) {
    198 			struct	mbuf *n;
    199 
    200 #ifdef IPSEC
    201 			/*
    202 			 * Check AH/ESP integrity.
    203 			 */
    204 			if (ipsec6_in_reject(m, last)) {
    205 				ipsec6stat.in_polvio++;
    206 				/* do not inject data into pcb */
    207 			} else
    208 #endif /* IPSEC */
    209 #ifdef FAST_IPSEC
    210 			/*
    211 			 * Check AH/ESP integrity
    212 			 */
    213 			if (!ipsec6_in_reject(m,last))
    214 #endif /* FAST_IPSEC */
    215 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
    216 				if (last->in6p_flags & IN6P_CONTROLOPTS)
    217 					ip6_savecontrol(last, &opts, ip6, n);
    218 				/* strip intermediate headers */
    219 				m_adj(n, *offp);
    220 				if (sbappendaddr(&last->in6p_socket->so_rcv,
    221 				    (struct sockaddr *)&rip6src, n, opts) == 0) {
    222 					/* should notify about lost packet */
    223 					m_freem(n);
    224 					if (opts)
    225 						m_freem(opts);
    226 					rip6stat.rip6s_fullsock++;
    227 				} else
    228 					sorwakeup(last->in6p_socket);
    229 				opts = NULL;
    230 			}
    231 		}
    232 		last = in6p;
    233 	}
    234 #ifdef IPSEC
    235 	/*
    236 	 * Check AH/ESP integrity.
    237 	 */
    238 	if (last && ipsec6_in_reject(m, last)) {
    239 		m_freem(m);
    240 		ipsec6stat.in_polvio++;
    241 		ip6stat.ip6s_delivered--;
    242 		/* do not inject data into pcb */
    243 	} else
    244 #endif /* IPSEC */
    245 #ifdef FAST_IPSEC
    246 	if (last && ipsec6_in_reject(m, last)) {
    247 		m_freem(m);
    248 		/*
    249 		 * XXX ipsec6_in_reject update stat if there is an error
    250 		 * so we just need to update stats by hand in the case of last is
    251 		 * NULL
    252 		 */
    253 		if (!last)
    254 			ipsec6stat.in_polvio++;
    255 			ip6stat.ip6s_delivered--;
    256 			/* do not inject data into pcb */
    257 		} else
    258 #endif /* FAST_IPSEC */
    259 	if (last) {
    260 		if (last->in6p_flags & IN6P_CONTROLOPTS)
    261 			ip6_savecontrol(last, &opts, ip6, m);
    262 		/* strip intermediate headers */
    263 		m_adj(m, *offp);
    264 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    265 		    (struct sockaddr *)&rip6src, m, opts) == 0) {
    266 			m_freem(m);
    267 			if (opts)
    268 				m_freem(opts);
    269 			rip6stat.rip6s_fullsock++;
    270 		} else
    271 			sorwakeup(last->in6p_socket);
    272 	} else {
    273 		rip6stat.rip6s_nosock++;
    274 		if (m->m_flags & M_MCAST)
    275 			rip6stat.rip6s_nosockmcast++;
    276 		if (proto == IPPROTO_NONE)
    277 			m_freem(m);
    278 		else {
    279 			u_int8_t *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
    280 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
    281 			icmp6_error(m, ICMP6_PARAM_PROB,
    282 			    ICMP6_PARAMPROB_NEXTHEADER,
    283 			    prvnxtp - mtod(m, u_int8_t *));
    284 		}
    285 		ip6stat.ip6s_delivered--;
    286 	}
    287 	return IPPROTO_DONE;
    288 }
    289 
    290 void
    291 rip6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
    292 {
    293 	struct ip6_hdr *ip6;
    294 	struct ip6ctlparam *ip6cp = NULL;
    295 	const struct sockaddr_in6 *sa6_src = NULL;
    296 	void *cmdarg;
    297 	void (*notify) __P((struct in6pcb *, int)) = in6_rtchange;
    298 	int nxt;
    299 
    300 	if (sa->sa_family != AF_INET6 ||
    301 	    sa->sa_len != sizeof(struct sockaddr_in6))
    302 		return;
    303 
    304 	if ((unsigned)cmd >= PRC_NCMDS)
    305 		return;
    306 	if (PRC_IS_REDIRECT(cmd))
    307 		notify = in6_rtchange, d = NULL;
    308 	else if (cmd == PRC_HOSTDEAD)
    309 		d = NULL;
    310 	else if (cmd == PRC_MSGSIZE)
    311 		; /* special code is present, see below */
    312 	else if (inet6ctlerrmap[cmd] == 0)
    313 		return;
    314 
    315 	/* if the parameter is from icmp6, decode it. */
    316 	if (d != NULL) {
    317 		ip6cp = (struct ip6ctlparam *)d;
    318 		ip6 = ip6cp->ip6c_ip6;
    319 		cmdarg = ip6cp->ip6c_cmdarg;
    320 		sa6_src = ip6cp->ip6c_src;
    321 		nxt = ip6cp->ip6c_nxt;
    322 	} else {
    323 		ip6 = NULL;
    324 		cmdarg = NULL;
    325 		sa6_src = &sa6_any;
    326 		nxt = -1;
    327 	}
    328 
    329 	if (ip6 && cmd == PRC_MSGSIZE) {
    330 		const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
    331 		int valid = 0;
    332 		struct in6pcb *in6p;
    333 
    334 		/*
    335 		 * Check to see if we have a valid raw IPv6 socket
    336 		 * corresponding to the address in the ICMPv6 message
    337 		 * payload, and the protocol (ip6_nxt) meets the socket.
    338 		 * XXX chase extension headers, or pass final nxt value
    339 		 * from icmp6_notify_error()
    340 		 */
    341 		in6p = NULL;
    342 		in6p = in6_pcblookup_connect(&raw6cbtable, &sa6->sin6_addr, 0,
    343 		    (const struct in6_addr *)&sa6_src->sin6_addr, 0, 0);
    344 #if 0
    345 		if (!in6p) {
    346 			/*
    347 			 * As the use of sendto(2) is fairly popular,
    348 			 * we may want to allow non-connected pcb too.
    349 			 * But it could be too weak against attacks...
    350 			 * We should at least check if the local
    351 			 * address (= s) is really ours.
    352 			 */
    353 			in6p = in6_pcblookup_bind(&raw6cbtable,
    354 			    &sa6->sin6_addr, 0, 0);
    355 		}
    356 #endif
    357 
    358 		if (in6p && in6p->in6p_ip6.ip6_nxt &&
    359 		    in6p->in6p_ip6.ip6_nxt == nxt)
    360 			valid++;
    361 
    362 		/*
    363 		 * Depending on the value of "valid" and routing table
    364 		 * size (mtudisc_{hi,lo}wat), we will:
    365 		 * - recalculate the new MTU and create the
    366 		 *   corresponding routing entry, or
    367 		 * - ignore the MTU change notification.
    368 		 */
    369 		icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    370 
    371 		/*
    372 		 * regardless of if we called icmp6_mtudisc_update(),
    373 		 * we need to call in6_pcbnotify(), to notify path MTU
    374 		 * change to the userland (RFC3542), because some
    375 		 * unconnected sockets may share the same destination
    376 		 * and want to know the path MTU.
    377 		 */
    378 	}
    379 
    380 	(void) in6_pcbnotify(&raw6cbtable, sa, 0,
    381 	    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
    382 }
    383 
    384 /*
    385  * Generate IPv6 header and pass packet to ip6_output.
    386  * Tack on options user may have setup with control call.
    387  */
    388 int
    389 rip6_output(struct mbuf *m, struct socket *so, struct sockaddr_in6 *dstsock,
    390     struct mbuf *control)
    391 {
    392 	struct in6_addr *dst;
    393 	struct ip6_hdr *ip6;
    394 	struct in6pcb *in6p;
    395 	u_int	plen = m->m_pkthdr.len;
    396 	int error = 0;
    397 	struct ip6_pktopts opt, *optp = NULL;
    398 	struct ifnet *oifp = NULL;
    399 	int type, code;		/* for ICMPv6 output statistics only */
    400 	int priv = 0;
    401 	int scope_ambiguous = 0;
    402 	struct in6_addr *in6a;
    403 
    404 	in6p = sotoin6pcb(so);
    405 
    406 	priv = 0;
    407 	if (curlwp && !kauth_authorize_generic(curlwp->l_cred,
    408 	    KAUTH_GENERIC_ISSUSER, NULL))
    409 		priv = 1;
    410 
    411 	dst = &dstsock->sin6_addr;
    412 	if (control) {
    413 		if ((error = ip6_setpktopts(control, &opt,
    414 		    in6p->in6p_outputopts,
    415 		    priv, so->so_proto->pr_protocol)) != 0) {
    416 			goto bad;
    417 		}
    418 		optp = &opt;
    419 	} else
    420 		optp = in6p->in6p_outputopts;
    421 
    422 	/*
    423 	 * Check and convert scope zone ID into internal form.
    424 	 * XXX: we may still need to determine the zone later.
    425 	 */
    426 	if (!(so->so_state & SS_ISCONNECTED)) {
    427 		if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
    428 			scope_ambiguous = 1;
    429 		if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
    430 			goto bad;
    431 	}
    432 
    433 	/*
    434 	 * For an ICMPv6 packet, we should know its type and code
    435 	 * to update statistics.
    436 	 */
    437 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    438 		struct icmp6_hdr *icmp6;
    439 		if (m->m_len < sizeof(struct icmp6_hdr) &&
    440 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
    441 			error = ENOBUFS;
    442 			goto bad;
    443 		}
    444 		icmp6 = mtod(m, struct icmp6_hdr *);
    445 		type = icmp6->icmp6_type;
    446 		code = icmp6->icmp6_code;
    447 	} else {
    448 		type = 0;
    449 		code = 0;
    450 	}
    451 
    452 	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
    453 	if (!m) {
    454 		error = ENOBUFS;
    455 		goto bad;
    456 	}
    457 	ip6 = mtod(m, struct ip6_hdr *);
    458 
    459 	/*
    460 	 * Next header might not be ICMP6 but use its pseudo header anyway.
    461 	 */
    462 	ip6->ip6_dst = *dst;
    463 
    464 	/*
    465 	 * Source address selection.
    466 	 */
    467 	if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions,
    468 	    (struct route *)&in6p->in6p_route, &in6p->in6p_laddr, &oifp,
    469 	    &error)) == 0) {
    470 		if (error == 0)
    471 			error = EADDRNOTAVAIL;
    472 		goto bad;
    473 	}
    474 	ip6->ip6_src = *in6a;
    475 
    476 	if (oifp && scope_ambiguous) {
    477 		/*
    478 		 * Application should provide a proper zone ID or the use of
    479 		 * default zone IDs should be enabled.  Unfortunately, some
    480 		 * applications do not behave as it should, so we need a
    481 		 * workaround.  Even if an appropriate ID is not determined
    482 		 * (when it's required), if we can determine the outgoing
    483 		 * interface. determine the zone ID based on the interface.
    484 		 */
    485 		error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
    486 		if (error != 0)
    487 			goto bad;
    488 	}
    489 	ip6->ip6_dst = dstsock->sin6_addr;
    490 
    491 	/* fill in the rest of the IPv6 header fields */
    492 	ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    493 	ip6->ip6_vfc  &= ~IPV6_VERSION_MASK;
    494 	ip6->ip6_vfc  |= IPV6_VERSION;
    495 	/* ip6_plen will be filled in ip6_output, so not fill it here. */
    496 	ip6->ip6_nxt   = in6p->in6p_ip6.ip6_nxt;
    497 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
    498 
    499 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
    500 	    in6p->in6p_cksum != -1) {
    501 		int off;
    502 		u_int16_t sum;
    503 
    504 		/* compute checksum */
    505 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
    506 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
    507 		else
    508 			off = in6p->in6p_cksum;
    509 		if (plen < off + 1) {
    510 			error = EINVAL;
    511 			goto bad;
    512 		}
    513 		off += sizeof(struct ip6_hdr);
    514 
    515 		sum = 0;
    516 		m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
    517 		    M_DONTWAIT);
    518 		if (m == NULL) {
    519 			error = ENOBUFS;
    520 			goto bad;
    521 		}
    522 		sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
    523 		m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
    524 		    M_DONTWAIT);
    525 		if (m == NULL) {
    526 			error = ENOBUFS;
    527 			goto bad;
    528 		}
    529 	}
    530 
    531 	error = ip6_output(m, optp, &in6p->in6p_route, 0,
    532 	    in6p->in6p_moptions, so, &oifp);
    533 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    534 		if (oifp)
    535 			icmp6_ifoutstat_inc(oifp, type, code);
    536 		icmp6stat.icp6s_outhist[type]++;
    537 	} else
    538 		rip6stat.rip6s_opackets++;
    539 
    540 	goto freectl;
    541 
    542  bad:
    543 	if (m)
    544 		m_freem(m);
    545 
    546  freectl:
    547 	if (control) {
    548 		ip6_clearpktopts(&opt, -1);
    549 		m_freem(control);
    550 	}
    551 	return error;
    552 }
    553 
    554 /*
    555  * Raw IPv6 socket option processing.
    556  */
    557 int
    558 rip6_ctloutput(int op, struct socket *so, int level, int optname,
    559     struct mbuf **mp)
    560 {
    561 	int error = 0;
    562 
    563 	if (level == SOL_SOCKET && optname == SO_NOHEADER) {
    564 		/* need to fiddle w/ opt(IPPROTO_IPV6, IPV6_CHECKSUM)? */
    565 		if (optname != SO_NOHEADER)
    566 			return ip6_ctloutput(op, so, level, optname, mp);
    567 		else if (op == PRCO_GETOPT) {
    568 			*mp = m_intopt(so, 1);
    569 			return 0;
    570 		} else if (*mp == NULL || (*mp)->m_len < sizeof(int))
    571 			error = EINVAL;
    572 		else if (*mtod(*mp, int *) == 0)
    573 			error = EINVAL;
    574 		goto free_m;
    575 	} else if (level != IPPROTO_IPV6)
    576 		return ip6_ctloutput(op, so, level, optname, mp);
    577 
    578 	switch (optname) {
    579 	case MRT6_INIT:
    580 	case MRT6_DONE:
    581 	case MRT6_ADD_MIF:
    582 	case MRT6_DEL_MIF:
    583 	case MRT6_ADD_MFC:
    584 	case MRT6_DEL_MFC:
    585 	case MRT6_PIM:
    586 		if (op == PRCO_SETOPT)
    587 			error = ip6_mrouter_set(optname, so, *mp);
    588 		else if (op == PRCO_GETOPT)
    589 			error = ip6_mrouter_get(optname, so, mp);
    590 		else
    591 			error = EINVAL;
    592 		break;
    593 	case IPV6_CHECKSUM:
    594 		return ip6_raw_ctloutput(op, so, level, optname, mp);
    595 	default:
    596 		return ip6_ctloutput(op, so, level, optname, mp);
    597 	}
    598 free_m:
    599 	if (op == PRCO_SETOPT && *mp != NULL)
    600 		m_free(*mp);
    601 	return error;
    602 }
    603 
    604 extern	u_long rip6_sendspace;
    605 extern	u_long rip6_recvspace;
    606 
    607 int
    608 rip6_usrreq(struct socket *so, int req, struct mbuf *m,
    609 	struct mbuf *nam, struct mbuf *control, struct lwp *l)
    610 {
    611 	struct in6pcb *in6p = sotoin6pcb(so);
    612 	int s;
    613 	int error = 0;
    614 	int priv;
    615 
    616 	priv = 0;
    617 	if (l && !kauth_authorize_generic(l->l_cred,
    618 	    KAUTH_GENERIC_ISSUSER, NULL))
    619 		priv++;
    620 
    621 	if (req == PRU_CONTROL)
    622 		return in6_control(so, (u_long)m, (void *)nam,
    623 		    (struct ifnet *)control, l);
    624 
    625 	if (req == PRU_PURGEIF) {
    626 		in6_pcbpurgeif0(&raw6cbtable, (struct ifnet *)control);
    627 		in6_purgeif((struct ifnet *)control);
    628 		in6_pcbpurgeif(&raw6cbtable, (struct ifnet *)control);
    629 		return 0;
    630 	}
    631 
    632 	switch (req) {
    633 	case PRU_ATTACH:
    634 		if (in6p != NULL)
    635 			panic("rip6_attach");
    636 		if (!priv) {
    637 			error = EACCES;
    638 			break;
    639 		}
    640 		s = splsoftnet();
    641 		error = soreserve(so, rip6_sendspace, rip6_recvspace);
    642 		if (error != 0) {
    643 			splx(s);
    644 			break;
    645 		}
    646 		if ((error = in6_pcballoc(so, &raw6cbtable)) != 0) {
    647 			splx(s);
    648 			break;
    649 		}
    650 		splx(s);
    651 		in6p = sotoin6pcb(so);
    652 		in6p->in6p_ip6.ip6_nxt = (long)nam;
    653 		in6p->in6p_cksum = -1;
    654 
    655 		MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
    656 		    sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
    657 		if (in6p->in6p_icmp6filt == NULL) {
    658 			in6_pcbdetach(in6p);
    659 			error = ENOMEM;
    660 			break;
    661 		}
    662 		ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
    663 		break;
    664 
    665 	case PRU_DISCONNECT:
    666 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    667 			error = ENOTCONN;
    668 			break;
    669 		}
    670 		in6p->in6p_faddr = in6addr_any;
    671 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
    672 		break;
    673 
    674 	case PRU_ABORT:
    675 		soisdisconnected(so);
    676 		/* Fallthrough */
    677 	case PRU_DETACH:
    678 		if (in6p == NULL)
    679 			panic("rip6_detach");
    680 		if (so == ip6_mrouter)
    681 			ip6_mrouter_done();
    682 		/* xxx: RSVP */
    683 		if (in6p->in6p_icmp6filt != NULL) {
    684 			FREE(in6p->in6p_icmp6filt, M_PCB);
    685 			in6p->in6p_icmp6filt = NULL;
    686 		}
    687 		in6_pcbdetach(in6p);
    688 		break;
    689 
    690 	case PRU_BIND:
    691 	    {
    692 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    693 		struct ifaddr *ia = NULL;
    694 
    695 		if (nam->m_len != sizeof(*addr)) {
    696 			error = EINVAL;
    697 			break;
    698 		}
    699 		if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) {
    700 			error = EADDRNOTAVAIL;
    701 			break;
    702 		}
    703 		if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
    704 			break;
    705 
    706 		/*
    707 		 * we don't support mapped address here, it would confuse
    708 		 * users so reject it
    709 		 */
    710 		if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
    711 			error = EADDRNOTAVAIL;
    712 			break;
    713 		}
    714 		if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
    715 		    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
    716 			error = EADDRNOTAVAIL;
    717 			break;
    718 		}
    719 		if (ia && ((struct in6_ifaddr *)ia)->ia6_flags &
    720 		    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    721 		     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
    722 			error = EADDRNOTAVAIL;
    723 			break;
    724 		}
    725 		in6p->in6p_laddr = addr->sin6_addr;
    726 		break;
    727 	    }
    728 
    729 	case PRU_CONNECT:
    730 	{
    731 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    732 		struct in6_addr *in6a = NULL;
    733 		struct ifnet *ifp = NULL;
    734 		int scope_ambiguous = 0;
    735 
    736 		if (nam->m_len != sizeof(*addr)) {
    737 			error = EINVAL;
    738 			break;
    739 		}
    740 		if (TAILQ_EMPTY(&ifnet)) {
    741 			error = EADDRNOTAVAIL;
    742 			break;
    743 		}
    744 		if (addr->sin6_family != AF_INET6) {
    745 			error = EAFNOSUPPORT;
    746 			break;
    747 		}
    748 
    749 		/*
    750 		 * Application should provide a proper zone ID or the use of
    751 		 * default zone IDs should be enabled.  Unfortunately, some
    752 		 * applications do not behave as it should, so we need a
    753 		 * workaround.  Even if an appropriate ID is not determined,
    754 		 * we'll see if we can determine the outgoing interface.  If we
    755 		 * can, determine the zone ID based on the interface below.
    756 		 */
    757 		if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
    758 			scope_ambiguous = 1;
    759 		if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
    760 			return error;
    761 
    762 		/* Source address selection. XXX: need pcblookup? */
    763 		in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
    764 		    in6p->in6p_moptions, (struct route *)&in6p->in6p_route,
    765 		    &in6p->in6p_laddr, &ifp, &error);
    766 		if (in6a == NULL) {
    767 			if (error == 0)
    768 				error = EADDRNOTAVAIL;
    769 			break;
    770 		}
    771 		/* XXX: see above */
    772 		if (ifp && scope_ambiguous &&
    773 		    (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
    774 			break;
    775 		}
    776 		in6p->in6p_laddr = *in6a;
    777 		in6p->in6p_faddr = addr->sin6_addr;
    778 		soisconnected(so);
    779 		break;
    780 	}
    781 
    782 	case PRU_CONNECT2:
    783 		error = EOPNOTSUPP;
    784 		break;
    785 
    786 	/*
    787 	 * Mark the connection as being incapable of futther input.
    788 	 */
    789 	case PRU_SHUTDOWN:
    790 		socantsendmore(so);
    791 		break;
    792 	/*
    793 	 * Ship a packet out. The appropriate raw output
    794 	 * routine handles any messaging necessary.
    795 	 */
    796 	case PRU_SEND:
    797 	{
    798 		struct sockaddr_in6 tmp;
    799 		struct sockaddr_in6 *dst;
    800 
    801 		/* always copy sockaddr to avoid overwrites */
    802 		if (so->so_state & SS_ISCONNECTED) {
    803 			if (nam) {
    804 				error = EISCONN;
    805 				break;
    806 			}
    807 			/* XXX */
    808 			bzero(&tmp, sizeof(tmp));
    809 			tmp.sin6_family = AF_INET6;
    810 			tmp.sin6_len = sizeof(struct sockaddr_in6);
    811 			bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
    812 			    sizeof(struct in6_addr));
    813 			dst = &tmp;
    814 		} else {
    815 			if (nam == NULL) {
    816 				error = ENOTCONN;
    817 				break;
    818 			}
    819 			if (nam->m_len != sizeof(tmp)) {
    820 				error = EINVAL;
    821 				break;
    822 			}
    823 
    824 			tmp = *mtod(nam, struct sockaddr_in6 *);
    825 			dst = &tmp;
    826 
    827 			if (dst->sin6_family != AF_INET6) {
    828 				error = EAFNOSUPPORT;
    829 				break;
    830 			}
    831 		}
    832 		error = rip6_output(m, so, dst, control);
    833 		m = NULL;
    834 		break;
    835 	}
    836 
    837 	case PRU_SENSE:
    838 		/*
    839 		 * stat: don't bother with a blocksize
    840 		 */
    841 		return 0;
    842 	/*
    843 	 * Not supported.
    844 	 */
    845 	case PRU_RCVOOB:
    846 	case PRU_RCVD:
    847 	case PRU_LISTEN:
    848 	case PRU_ACCEPT:
    849 	case PRU_SENDOOB:
    850 		error = EOPNOTSUPP;
    851 		break;
    852 
    853 	case PRU_SOCKADDR:
    854 		in6_setsockaddr(in6p, nam);
    855 		break;
    856 
    857 	case PRU_PEERADDR:
    858 		in6_setpeeraddr(in6p, nam);
    859 		break;
    860 
    861 	default:
    862 		panic("rip6_usrreq");
    863 	}
    864 	if (m != NULL)
    865 		m_freem(m);
    866 	return error;
    867 }
    868 
    869 SYSCTL_SETUP(sysctl_net_inet6_raw6_setup, "sysctl net.inet6.raw6 subtree setup")
    870 {
    871 
    872 	sysctl_createv(clog, 0, NULL, NULL,
    873 		       CTLFLAG_PERMANENT,
    874 		       CTLTYPE_NODE, "net", NULL,
    875 		       NULL, 0, NULL, 0,
    876 		       CTL_NET, CTL_EOL);
    877 	sysctl_createv(clog, 0, NULL, NULL,
    878 		       CTLFLAG_PERMANENT,
    879 		       CTLTYPE_NODE, "inet6", NULL,
    880 		       NULL, 0, NULL, 0,
    881 		       CTL_NET, PF_INET6, CTL_EOL);
    882 	sysctl_createv(clog, 0, NULL, NULL,
    883 		       CTLFLAG_PERMANENT,
    884 		       CTLTYPE_NODE, "raw6",
    885 		       SYSCTL_DESCR("Raw IPv6 settings"),
    886 		       NULL, 0, NULL, 0,
    887 		       CTL_NET, PF_INET6, IPPROTO_RAW, CTL_EOL);
    888 
    889 	sysctl_createv(clog, 0, NULL, NULL,
    890 		       CTLFLAG_PERMANENT,
    891 		       CTLTYPE_STRUCT, "pcblist",
    892 		       SYSCTL_DESCR("Raw IPv6 control block list"),
    893 		       sysctl_inpcblist, 0, &raw6cbtable, 0,
    894 		       CTL_NET, PF_INET6, IPPROTO_RAW,
    895 		       CTL_CREATE, CTL_EOL);
    896 	sysctl_createv(clog, 0, NULL, NULL,
    897 		       CTLFLAG_PERMANENT,
    898 		       CTLTYPE_STRUCT, "stats",
    899 		       SYSCTL_DESCR("Raw IPv6 statistics"),
    900 		       NULL, 0, &rip6stat, sizeof(rip6stat),
    901 		       CTL_NET, PF_INET6, IPPROTO_RAW, RAW6CTL_STATS,
    902 		       CTL_EOL);
    903 }
    904