Home | History | Annotate | Line # | Download | only in netinet6
ip6_output.c revision 1.91
      1 /*	$NetBSD: ip6_output.c,v 1.91 2005/08/18 00:30:59 yamt Exp $	*/
      2 /*	$KAME: ip6_output.c,v 1.172 2001/03/25 09:55:56 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, 1990, 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  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: ip6_output.c,v 1.91 2005/08/18 00:30:59 yamt Exp $");
     66 
     67 #include "opt_inet.h"
     68 #include "opt_ipsec.h"
     69 #include "opt_pfil_hooks.h"
     70 
     71 #include <sys/param.h>
     72 #include <sys/malloc.h>
     73 #include <sys/mbuf.h>
     74 #include <sys/errno.h>
     75 #include <sys/protosw.h>
     76 #include <sys/socket.h>
     77 #include <sys/socketvar.h>
     78 #include <sys/systm.h>
     79 #include <sys/proc.h>
     80 
     81 #include <net/if.h>
     82 #include <net/route.h>
     83 #ifdef PFIL_HOOKS
     84 #include <net/pfil.h>
     85 #endif
     86 
     87 #include <netinet/in.h>
     88 #include <netinet/in_var.h>
     89 #include <netinet/ip6.h>
     90 #include <netinet/icmp6.h>
     91 #include <netinet/in_offload.h>
     92 #include <netinet6/ip6_var.h>
     93 #include <netinet6/in6_pcb.h>
     94 #include <netinet6/nd6.h>
     95 #include <netinet6/ip6protosw.h>
     96 
     97 #ifdef IPSEC
     98 #include <netinet6/ipsec.h>
     99 #include <netkey/key.h>
    100 #endif /* IPSEC */
    101 
    102 #include <net/net_osdep.h>
    103 
    104 #ifdef PFIL_HOOKS
    105 extern struct pfil_head inet6_pfil_hook;	/* XXX */
    106 #endif
    107 
    108 struct ip6_exthdrs {
    109 	struct mbuf *ip6e_ip6;
    110 	struct mbuf *ip6e_hbh;
    111 	struct mbuf *ip6e_dest1;
    112 	struct mbuf *ip6e_rthdr;
    113 	struct mbuf *ip6e_dest2;
    114 };
    115 
    116 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
    117 	struct socket *));
    118 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
    119 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
    120 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
    121 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
    122 	struct ip6_frag **));
    123 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
    124 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
    125 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
    126 	struct ifnet *, struct in6_addr *, u_long *, int *));
    127 
    128 #define	IN6_NEED_CHECKSUM(ifp, csum_flags) \
    129 	(__predict_true(((ifp)->if_flags & IFF_LOOPBACK) == 0 || \
    130 	(((csum_flags) & M_CSUM_UDPv6) != 0 && udp_do_loopback_cksum) || \
    131 	(((csum_flags) & M_CSUM_TCPv6) != 0 && tcp_do_loopback_cksum)))
    132 
    133 /*
    134  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
    135  * header (with pri, len, nxt, hlim, src, dst).
    136  * This function may modify ver and hlim only.
    137  * The mbuf chain containing the packet will be freed.
    138  * The mbuf opt, if present, will not be freed.
    139  *
    140  * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
    141  * nd_ifinfo.linkmtu is u_int32_t.  so we use u_long to hold largest one,
    142  * which is rt_rmx.rmx_mtu.
    143  */
    144 int
    145 ip6_output(m0, opt, ro, flags, im6o, so, ifpp)
    146 	struct mbuf *m0;
    147 	struct ip6_pktopts *opt;
    148 	struct route_in6 *ro;
    149 	int flags;
    150 	struct ip6_moptions *im6o;
    151 	struct socket *so;
    152 	struct ifnet **ifpp;		/* XXX: just for statistics */
    153 {
    154 	struct ip6_hdr *ip6, *mhip6;
    155 	struct ifnet *ifp, *origifp;
    156 	struct mbuf *m = m0;
    157 	int hlen, tlen, len, off;
    158 	struct route_in6 ip6route;
    159 	struct sockaddr_in6 *dst;
    160 	int error = 0;
    161 	u_long mtu;
    162 	int alwaysfrag, dontfrag;
    163 	u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
    164 	struct ip6_exthdrs exthdrs;
    165 	struct in6_addr finaldst;
    166 	struct route_in6 *ro_pmtu = NULL;
    167 	int hdrsplit = 0;
    168 	int needipsec = 0;
    169 #ifdef IPSEC
    170 	int needipsectun = 0;
    171 	struct secpolicy *sp = NULL;
    172 
    173 	ip6 = mtod(m, struct ip6_hdr *);
    174 #endif /* IPSEC */
    175 
    176 	M_CSUM_DATA_IPv6_HL_SET(m->m_pkthdr.csum_data, sizeof(struct ip6_hdr));
    177 
    178 #define MAKE_EXTHDR(hp, mp)						\
    179     do {								\
    180 	if (hp) {							\
    181 		struct ip6_ext *eh = (struct ip6_ext *)(hp);		\
    182 		error = ip6_copyexthdr((mp), (caddr_t)(hp), 		\
    183 		    ((eh)->ip6e_len + 1) << 3);				\
    184 		if (error)						\
    185 			goto freehdrs;					\
    186 	}								\
    187     } while (/*CONSTCOND*/ 0)
    188 
    189 	bzero(&exthdrs, sizeof(exthdrs));
    190 	if (opt) {
    191 		/* Hop-by-Hop options header */
    192 		MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
    193 		/* Destination options header(1st part) */
    194 		MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
    195 		/* Routing header */
    196 		MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
    197 		/* Destination options header(2nd part) */
    198 		MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
    199 	}
    200 
    201 #ifdef IPSEC
    202 	if ((flags & IPV6_FORWARDING) != 0) {
    203 		needipsec = 0;
    204 		goto skippolicycheck;
    205 	}
    206 
    207 	/* get a security policy for this packet */
    208 	if (so == NULL)
    209 		sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
    210 	else {
    211 		if (IPSEC_PCB_SKIP_IPSEC(sotoinpcb_hdr(so)->inph_sp,
    212 					 IPSEC_DIR_OUTBOUND)) {
    213 			needipsec = 0;
    214 			goto skippolicycheck;
    215 		}
    216 		sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
    217 	}
    218 
    219 	if (sp == NULL) {
    220 		ipsec6stat.out_inval++;
    221 		goto freehdrs;
    222 	}
    223 
    224 	error = 0;
    225 
    226 	/* check policy */
    227 	switch (sp->policy) {
    228 	case IPSEC_POLICY_DISCARD:
    229 		/*
    230 		 * This packet is just discarded.
    231 		 */
    232 		ipsec6stat.out_polvio++;
    233 		goto freehdrs;
    234 
    235 	case IPSEC_POLICY_BYPASS:
    236 	case IPSEC_POLICY_NONE:
    237 		/* no need to do IPsec. */
    238 		needipsec = 0;
    239 		break;
    240 
    241 	case IPSEC_POLICY_IPSEC:
    242 		if (sp->req == NULL) {
    243 			/* XXX should be panic ? */
    244 			printf("ip6_output: No IPsec request specified.\n");
    245 			error = EINVAL;
    246 			goto freehdrs;
    247 		}
    248 		needipsec = 1;
    249 		break;
    250 
    251 	case IPSEC_POLICY_ENTRUST:
    252 	default:
    253 		printf("ip6_output: Invalid policy found. %d\n", sp->policy);
    254 	}
    255 
    256   skippolicycheck:;
    257 #endif /* IPSEC */
    258 
    259 	if (needipsec &&
    260 	    (m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) {
    261 		in6_delayed_cksum(m);
    262 		m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
    263 	}
    264 
    265 	/*
    266 	 * Calculate the total length of the extension header chain.
    267 	 * Keep the length of the unfragmentable part for fragmentation.
    268 	 */
    269 	optlen = 0;
    270 	if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
    271 	if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
    272 	if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
    273 	unfragpartlen = optlen + sizeof(struct ip6_hdr);
    274 	/* NOTE: we don't add AH/ESP length here. do that later. */
    275 	if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
    276 
    277 	/*
    278 	 * If we need IPsec, or there is at least one extension header,
    279 	 * separate IP6 header from the payload.
    280 	 */
    281 	if ((needipsec || optlen) && !hdrsplit) {
    282 		if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
    283 			m = NULL;
    284 			goto freehdrs;
    285 		}
    286 		m = exthdrs.ip6e_ip6;
    287 		hdrsplit++;
    288 	}
    289 
    290 	/* adjust pointer */
    291 	ip6 = mtod(m, struct ip6_hdr *);
    292 
    293 	/* adjust mbuf packet header length */
    294 	m->m_pkthdr.len += optlen;
    295 	plen = m->m_pkthdr.len - sizeof(*ip6);
    296 
    297 	/* If this is a jumbo payload, insert a jumbo payload option. */
    298 	if (plen > IPV6_MAXPACKET) {
    299 		if (!hdrsplit) {
    300 			if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
    301 				m = NULL;
    302 				goto freehdrs;
    303 			}
    304 			m = exthdrs.ip6e_ip6;
    305 			hdrsplit++;
    306 		}
    307 		/* adjust pointer */
    308 		ip6 = mtod(m, struct ip6_hdr *);
    309 		if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
    310 			goto freehdrs;
    311 		optlen += 8; /* XXX JUMBOOPTLEN */
    312 		ip6->ip6_plen = 0;
    313 	} else
    314 		ip6->ip6_plen = htons(plen);
    315 
    316 	/*
    317 	 * Concatenate headers and fill in next header fields.
    318 	 * Here we have, on "m"
    319 	 *	IPv6 payload
    320 	 * and we insert headers accordingly.  Finally, we should be getting:
    321 	 *	IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
    322 	 *
    323 	 * during the header composing process, "m" points to IPv6 header.
    324 	 * "mprev" points to an extension header prior to esp.
    325 	 */
    326 	{
    327 		u_char *nexthdrp = &ip6->ip6_nxt;
    328 		struct mbuf *mprev = m;
    329 
    330 		/*
    331 		 * we treat dest2 specially.  this makes IPsec processing
    332 		 * much easier.  the goal here is to make mprev point the
    333 		 * mbuf prior to dest2.
    334 		 *
    335 		 * result: IPv6 dest2 payload
    336 		 * m and mprev will point to IPv6 header.
    337 		 */
    338 		if (exthdrs.ip6e_dest2) {
    339 			if (!hdrsplit)
    340 				panic("assumption failed: hdr not split");
    341 			exthdrs.ip6e_dest2->m_next = m->m_next;
    342 			m->m_next = exthdrs.ip6e_dest2;
    343 			*mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
    344 			ip6->ip6_nxt = IPPROTO_DSTOPTS;
    345 		}
    346 
    347 #define MAKE_CHAIN(m, mp, p, i)\
    348     do {\
    349 	if (m) {\
    350 		if (!hdrsplit) \
    351 			panic("assumption failed: hdr not split"); \
    352 		*mtod((m), u_char *) = *(p);\
    353 		*(p) = (i);\
    354 		p = mtod((m), u_char *);\
    355 		(m)->m_next = (mp)->m_next;\
    356 		(mp)->m_next = (m);\
    357 		(mp) = (m);\
    358 	}\
    359     } while (/*CONSTCOND*/ 0)
    360 		/*
    361 		 * result: IPv6 hbh dest1 rthdr dest2 payload
    362 		 * m will point to IPv6 header.  mprev will point to the
    363 		 * extension header prior to dest2 (rthdr in the above case).
    364 		 */
    365 		MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
    366 		MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
    367 		    IPPROTO_DSTOPTS);
    368 		MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
    369 		    IPPROTO_ROUTING);
    370 
    371 		M_CSUM_DATA_IPv6_HL_SET(m->m_pkthdr.csum_data,
    372 		    sizeof(struct ip6_hdr) + optlen);
    373 
    374 #ifdef IPSEC
    375 		if (!needipsec)
    376 			goto skip_ipsec2;
    377 
    378 		/*
    379 		 * pointers after IPsec headers are not valid any more.
    380 		 * other pointers need a great care too.
    381 		 * (IPsec routines should not mangle mbufs prior to AH/ESP)
    382 		 */
    383 		exthdrs.ip6e_dest2 = NULL;
    384 
    385 	    {
    386 		struct ip6_rthdr *rh = NULL;
    387 		int segleft_org = 0;
    388 		struct ipsec_output_state state;
    389 
    390 		if (exthdrs.ip6e_rthdr) {
    391 			rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
    392 			segleft_org = rh->ip6r_segleft;
    393 			rh->ip6r_segleft = 0;
    394 		}
    395 
    396 		bzero(&state, sizeof(state));
    397 		state.m = m;
    398 		error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
    399 		    &needipsectun);
    400 		m = state.m;
    401 		if (error) {
    402 			/* mbuf is already reclaimed in ipsec6_output_trans. */
    403 			m = NULL;
    404 			switch (error) {
    405 			case EHOSTUNREACH:
    406 			case ENETUNREACH:
    407 			case EMSGSIZE:
    408 			case ENOBUFS:
    409 			case ENOMEM:
    410 				break;
    411 			default:
    412 				printf("ip6_output (ipsec): error code %d\n", error);
    413 				/* FALLTHROUGH */
    414 			case ENOENT:
    415 				/* don't show these error codes to the user */
    416 				error = 0;
    417 				break;
    418 			}
    419 			goto bad;
    420 		}
    421 		if (exthdrs.ip6e_rthdr) {
    422 			/* ah6_output doesn't modify mbuf chain */
    423 			rh->ip6r_segleft = segleft_org;
    424 		}
    425 	    }
    426 skip_ipsec2:;
    427 #endif
    428 	}
    429 
    430 	/*
    431 	 * If there is a routing header, replace destination address field
    432 	 * with the first hop of the routing header.
    433 	 */
    434 	if (exthdrs.ip6e_rthdr) {
    435 		struct ip6_rthdr *rh;
    436 		struct ip6_rthdr0 *rh0;
    437 		struct in6_addr *addr;
    438 
    439 		rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
    440 		    struct ip6_rthdr *));
    441 		finaldst = ip6->ip6_dst;
    442 		switch (rh->ip6r_type) {
    443 		case IPV6_RTHDR_TYPE_0:
    444 			 rh0 = (struct ip6_rthdr0 *)rh;
    445 			 addr = (struct in6_addr *)(rh0 + 1);
    446 			 ip6->ip6_dst = addr[0];
    447 			 bcopy(&addr[1], &addr[0],
    448 			     sizeof(struct in6_addr) * (rh0->ip6r0_segleft - 1));
    449 			 addr[rh0->ip6r0_segleft - 1] = finaldst;
    450 			 break;
    451 		default:	/* is it possible? */
    452 			 error = EINVAL;
    453 			 goto bad;
    454 		}
    455 	}
    456 
    457 	/* Source address validation */
    458 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
    459 	    (flags & IPV6_UNSPECSRC) == 0) {
    460 		error = EOPNOTSUPP;
    461 		ip6stat.ip6s_badscope++;
    462 		goto bad;
    463 	}
    464 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
    465 		error = EOPNOTSUPP;
    466 		ip6stat.ip6s_badscope++;
    467 		goto bad;
    468 	}
    469 
    470 	ip6stat.ip6s_localout++;
    471 
    472 	/*
    473 	 * Route packet.
    474 	 */
    475 	/* initialize cached route */
    476 	if (ro == 0) {
    477 		ro = &ip6route;
    478 		bzero((caddr_t)ro, sizeof(*ro));
    479 	}
    480 	ro_pmtu = ro;
    481 	if (opt && opt->ip6po_rthdr)
    482 		ro = &opt->ip6po_route;
    483 	dst = (struct sockaddr_in6 *)&ro->ro_dst;
    484 	/*
    485 	 * If there is a cached route,
    486 	 * check that it is to the same destination
    487 	 * and is still up. If not, free it and try again.
    488 	 */
    489 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
    490 	    dst->sin6_family != AF_INET6 ||
    491 	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
    492 		RTFREE(ro->ro_rt);
    493 		ro->ro_rt = (struct rtentry *)0;
    494 	}
    495 	if (ro->ro_rt == 0) {
    496 		bzero(dst, sizeof(*dst));
    497 		dst->sin6_family = AF_INET6;
    498 		dst->sin6_len = sizeof(struct sockaddr_in6);
    499 		dst->sin6_addr = ip6->ip6_dst;
    500 	}
    501 #ifdef IPSEC
    502 	if (needipsec && needipsectun) {
    503 		struct ipsec_output_state state;
    504 
    505 		/*
    506 		 * All the extension headers will become inaccessible
    507 		 * (since they can be encrypted).
    508 		 * Don't panic, we need no more updates to extension headers
    509 		 * on inner IPv6 packet (since they are now encapsulated).
    510 		 *
    511 		 * IPv6 [ESP|AH] IPv6 [extension headers] payload
    512 		 */
    513 		bzero(&exthdrs, sizeof(exthdrs));
    514 		exthdrs.ip6e_ip6 = m;
    515 
    516 		bzero(&state, sizeof(state));
    517 		state.m = m;
    518 		state.ro = (struct route *)ro;
    519 		state.dst = (struct sockaddr *)dst;
    520 
    521 		error = ipsec6_output_tunnel(&state, sp, flags);
    522 
    523 		m = state.m;
    524 		ro_pmtu = ro = (struct route_in6 *)state.ro;
    525 		dst = (struct sockaddr_in6 *)state.dst;
    526 		if (error) {
    527 			/* mbuf is already reclaimed in ipsec6_output_tunnel. */
    528 			m0 = m = NULL;
    529 			m = NULL;
    530 			switch (error) {
    531 			case EHOSTUNREACH:
    532 			case ENETUNREACH:
    533 			case EMSGSIZE:
    534 			case ENOBUFS:
    535 			case ENOMEM:
    536 				break;
    537 			default:
    538 				printf("ip6_output (ipsec): error code %d\n", error);
    539 				/* FALLTHROUGH */
    540 			case ENOENT:
    541 				/* don't show these error codes to the user */
    542 				error = 0;
    543 				break;
    544 			}
    545 			goto bad;
    546 		}
    547 
    548 		exthdrs.ip6e_ip6 = m;
    549 	}
    550 #endif /* IPSEC */
    551 
    552 	if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    553 		/* Unicast */
    554 
    555 #define ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
    556 #define sin6tosa(sin6)	((struct sockaddr *)(sin6))
    557 		/* xxx
    558 		 * interface selection comes here
    559 		 * if an interface is specified from an upper layer,
    560 		 * ifp must point it.
    561 		 */
    562 		if (ro->ro_rt == 0) {
    563 			/*
    564 			 * non-bsdi always clone routes, if parent is
    565 			 * PRF_CLONING.
    566 			 */
    567 			rtalloc((struct route *)ro);
    568 		}
    569 		if (ro->ro_rt == 0) {
    570 			ip6stat.ip6s_noroute++;
    571 			error = EHOSTUNREACH;
    572 			/* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
    573 			goto bad;
    574 		}
    575 		ifp = ro->ro_rt->rt_ifp;
    576 		ro->ro_rt->rt_use++;
    577 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
    578 			dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
    579 		m->m_flags &= ~(M_BCAST | M_MCAST);	/* just in case */
    580 
    581 		in6_ifstat_inc(ifp, ifs6_out_request);
    582 
    583 		/*
    584 		 * Check if the outgoing interface conflicts with
    585 		 * the interface specified by ifi6_ifindex (if specified).
    586 		 * Note that loopback interface is always okay.
    587 		 * (this may happen when we are sending a packet to one of
    588 		 *  our own addresses.)
    589 		 */
    590 		if (opt && opt->ip6po_pktinfo &&
    591 		    opt->ip6po_pktinfo->ipi6_ifindex) {
    592 			if (!(ifp->if_flags & IFF_LOOPBACK) &&
    593 			    ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
    594 				ip6stat.ip6s_noroute++;
    595 				in6_ifstat_inc(ifp, ifs6_out_discard);
    596 				error = EHOSTUNREACH;
    597 				goto bad;
    598 			}
    599 		}
    600 
    601 		if (opt && opt->ip6po_hlim != -1)
    602 			ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
    603 	} else {
    604 		/* Multicast */
    605 		struct	in6_multi *in6m;
    606 
    607 		m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
    608 
    609 		/*
    610 		 * See if the caller provided any multicast options
    611 		 */
    612 		ifp = NULL;
    613 		if (im6o != NULL) {
    614 			ip6->ip6_hlim = im6o->im6o_multicast_hlim;
    615 			if (im6o->im6o_multicast_ifp != NULL)
    616 				ifp = im6o->im6o_multicast_ifp;
    617 		} else
    618 			ip6->ip6_hlim = ip6_defmcasthlim;
    619 
    620 		/*
    621 		 * See if the caller provided the outgoing interface
    622 		 * as an ancillary data.
    623 		 * Boundary check for ifindex is assumed to be already done.
    624 		 */
    625 		if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
    626 			ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
    627 
    628 		/*
    629 		 * If the destination is a node-local scope multicast,
    630 		 * the packet should be loop-backed only.
    631 		 */
    632 		if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
    633 			/*
    634 			 * If the outgoing interface is already specified,
    635 			 * it should be a loopback interface.
    636 			 */
    637 			if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
    638 				ip6stat.ip6s_badscope++;
    639 				error = ENETUNREACH; /* XXX: better error? */
    640 				/* XXX correct ifp? */
    641 				in6_ifstat_inc(ifp, ifs6_out_discard);
    642 				goto bad;
    643 			} else
    644 				ifp = lo0ifp;
    645 		}
    646 
    647 		if (opt && opt->ip6po_hlim != -1)
    648 			ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
    649 
    650 		/*
    651 		 * If caller did not provide an interface lookup a
    652 		 * default in the routing table.  This is either a
    653 		 * default for the speicfied group (i.e. a host
    654 		 * route), or a multicast default (a route for the
    655 		 * ``net'' ff00::/8).
    656 		 */
    657 		if (ifp == NULL) {
    658 			if (ro->ro_rt == 0) {
    659 				ro->ro_rt = rtalloc1((struct sockaddr *)
    660 				    &ro->ro_dst, 0);
    661 			}
    662 			if (ro->ro_rt == 0) {
    663 				ip6stat.ip6s_noroute++;
    664 				error = EHOSTUNREACH;
    665 				/* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
    666 				goto bad;
    667 			}
    668 			ifp = ro->ro_rt->rt_ifp;
    669 			ro->ro_rt->rt_use++;
    670 		}
    671 
    672 		if ((flags & IPV6_FORWARDING) == 0)
    673 			in6_ifstat_inc(ifp, ifs6_out_request);
    674 		in6_ifstat_inc(ifp, ifs6_out_mcast);
    675 
    676 		/*
    677 		 * Confirm that the outgoing interface supports multicast.
    678 		 */
    679 		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    680 			ip6stat.ip6s_noroute++;
    681 			in6_ifstat_inc(ifp, ifs6_out_discard);
    682 			error = ENETUNREACH;
    683 			goto bad;
    684 		}
    685 		IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
    686 		if (in6m != NULL &&
    687 		   (im6o == NULL || im6o->im6o_multicast_loop)) {
    688 			/*
    689 			 * If we belong to the destination multicast group
    690 			 * on the outgoing interface, and the caller did not
    691 			 * forbid loopback, loop back a copy.
    692 			 */
    693 			ip6_mloopback(ifp, m, dst);
    694 		} else {
    695 			/*
    696 			 * If we are acting as a multicast router, perform
    697 			 * multicast forwarding as if the packet had just
    698 			 * arrived on the interface to which we are about
    699 			 * to send.  The multicast forwarding function
    700 			 * recursively calls this function, using the
    701 			 * IPV6_FORWARDING flag to prevent infinite recursion.
    702 			 *
    703 			 * Multicasts that are looped back by ip6_mloopback(),
    704 			 * above, will be forwarded by the ip6_input() routine,
    705 			 * if necessary.
    706 			 */
    707 			if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
    708 				if (ip6_mforward(ip6, ifp, m) != 0) {
    709 					m_freem(m);
    710 					goto done;
    711 				}
    712 			}
    713 		}
    714 		/*
    715 		 * Multicasts with a hoplimit of zero may be looped back,
    716 		 * above, but must not be transmitted on a network.
    717 		 * Also, multicasts addressed to the loopback interface
    718 		 * are not sent -- the above call to ip6_mloopback() will
    719 		 * loop back a copy if this host actually belongs to the
    720 		 * destination group on the loopback interface.
    721 		 */
    722 		if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
    723 			m_freem(m);
    724 			goto done;
    725 		}
    726 	}
    727 
    728 	/*
    729 	 * Fill the outgoing inteface to tell the upper layer
    730 	 * to increment per-interface statistics.
    731 	 */
    732 	if (ifpp)
    733 		*ifpp = ifp;
    734 
    735 	/* Determine path MTU. */
    736 	if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
    737 	    &alwaysfrag)) != 0)
    738 		goto bad;
    739 #ifdef IPSEC
    740 	if (needipsectun)
    741 		mtu = IPV6_MMTU;
    742 #endif
    743 
    744 	/*
    745 	 * The caller of this function may specify to use the minimum MTU
    746 	 * in some cases.
    747 	 */
    748 	if (mtu > IPV6_MMTU) {
    749 		if ((flags & IPV6_MINMTU))
    750 			mtu = IPV6_MMTU;
    751 	}
    752 
    753 	/* Fake scoped addresses */
    754 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    755 		/*
    756 		 * If source or destination address is a scoped address, and
    757 		 * the packet is going to be sent to a loopback interface,
    758 		 * we should keep the original interface.
    759 		 */
    760 
    761 		/*
    762 		 * XXX: this is a very experimental and temporary solution.
    763 		 * We eventually have sockaddr_in6 and use the sin6_scope_id
    764 		 * field of the structure here.
    765 		 * We rely on the consistency between two scope zone ids
    766 		 * of source add destination, which should already be assured
    767 		 * Larger scopes than link will be supported in the near
    768 		 * future.
    769 		 */
    770 		origifp = NULL;
    771 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    772 			origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
    773 		else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    774 			origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
    775 		/*
    776 		 * XXX: origifp can be NULL even in those two cases above.
    777 		 * For example, if we remove the (only) link-local address
    778 		 * from the loopback interface, and try to send a link-local
    779 		 * address without link-id information.  Then the source
    780 		 * address is ::1, and the destination address is the
    781 		 * link-local address with its s6_addr16[1] being zero.
    782 		 * What is worse, if the packet goes to the loopback interface
    783 		 * by a default rejected route, the null pointer would be
    784 		 * passed to looutput, and the kernel would hang.
    785 		 * The following last resort would prevent such disaster.
    786 		 */
    787 		if (origifp == NULL)
    788 			origifp = ifp;
    789 	} else
    790 		origifp = ifp;
    791 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    792 		ip6->ip6_src.s6_addr16[1] = 0;
    793 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    794 		ip6->ip6_dst.s6_addr16[1] = 0;
    795 
    796 	/*
    797 	 * If the outgoing packet contains a hop-by-hop options header,
    798 	 * it must be examined and processed even by the source node.
    799 	 * (RFC 2460, section 4.)
    800 	 */
    801 	if (exthdrs.ip6e_hbh) {
    802 		struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
    803 		u_int32_t dummy1; /* XXX unused */
    804 		u_int32_t dummy2; /* XXX unused */
    805 
    806 		/*
    807 		 *  XXX: if we have to send an ICMPv6 error to the sender,
    808 		 *       we need the M_LOOP flag since icmp6_error() expects
    809 		 *       the IPv6 and the hop-by-hop options header are
    810 		 *       continuous unless the flag is set.
    811 		 */
    812 		m->m_flags |= M_LOOP;
    813 		m->m_pkthdr.rcvif = ifp;
    814 		if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
    815 		    ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
    816 		    &dummy1, &dummy2) < 0) {
    817 			/* m was already freed at this point */
    818 			error = EINVAL;/* better error? */
    819 			goto done;
    820 		}
    821 		m->m_flags &= ~M_LOOP; /* XXX */
    822 		m->m_pkthdr.rcvif = NULL;
    823 	}
    824 
    825 #ifdef PFIL_HOOKS
    826 	/*
    827 	 * Run through list of hooks for output packets.
    828 	 */
    829 	if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
    830 		goto done;
    831 	if (m == NULL)
    832 		goto done;
    833 	ip6 = mtod(m, struct ip6_hdr *);
    834 #endif /* PFIL_HOOKS */
    835 	/*
    836 	 * Send the packet to the outgoing interface.
    837 	 * If necessary, do IPv6 fragmentation before sending.
    838 	 *
    839 	 * the logic here is rather complex:
    840 	 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
    841 	 * 1-a:	send as is if tlen <= path mtu
    842 	 * 1-b:	fragment if tlen > path mtu
    843 	 *
    844 	 * 2: if user asks us not to fragment (dontfrag == 1)
    845 	 * 2-a:	send as is if tlen <= interface mtu
    846 	 * 2-b:	error if tlen > interface mtu
    847 	 *
    848 	 * 3: if we always need to attach fragment header (alwaysfrag == 1)
    849 	 *	always fragment
    850 	 *
    851 	 * 4: if dontfrag == 1 && alwaysfrag == 1
    852 	 *	error, as we cannot handle this conflicting request
    853 	 */
    854 	tlen = m->m_pkthdr.len;
    855 
    856 	dontfrag = 0;
    857 	if (dontfrag && alwaysfrag) {	/* case 4 */
    858 		/* conflicting request - can't transmit */
    859 		error = EMSGSIZE;
    860 		goto bad;
    861 	}
    862 	if (dontfrag && tlen > IN6_LINKMTU(ifp)) {	/* case 2-b */
    863 		/*
    864 		 * Even if the DONTFRAG option is specified, we cannot send the
    865 		 * packet when the data length is larger than the MTU of the
    866 		 * outgoing interface.
    867 		 * Notify the error by sending IPV6_PATHMTU ancillary data as
    868 		 * well as returning an error code (the latter is not described
    869 		 * in the API spec.)
    870 		 */
    871 		u_int32_t mtu32;
    872 		struct ip6ctlparam ip6cp;
    873 
    874 		mtu32 = (u_int32_t)mtu;
    875 		bzero(&ip6cp, sizeof(ip6cp));
    876 		ip6cp.ip6c_cmdarg = (void *)&mtu32;
    877 		pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
    878 		    (void *)&ip6cp);
    879 
    880 		error = EMSGSIZE;
    881 		goto bad;
    882 	}
    883 
    884 	/*
    885 	 * transmit packet without fragmentation
    886 	 */
    887 	if (dontfrag || (!alwaysfrag && tlen <= mtu)) {	/* case 1-a and 2-a */
    888 		struct in6_ifaddr *ia6;
    889 		int sw_csum;
    890 
    891 		ip6 = mtod(m, struct ip6_hdr *);
    892 		ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
    893 		if (ia6) {
    894 			/* Record statistics for this interface address. */
    895 			ia6->ia_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len;
    896 		}
    897 #ifdef IPSEC
    898 		/* clean ipsec history once it goes out of the node */
    899 		ipsec_delaux(m);
    900 #endif
    901 
    902 		sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
    903 		if ((sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) {
    904 			if (IN6_NEED_CHECKSUM(ifp,
    905 			    sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6))) {
    906 				in6_delayed_cksum(m);
    907 			}
    908 			m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
    909 		}
    910 
    911 		error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
    912 		goto done;
    913 	}
    914 
    915 	/*
    916 	 * try to fragment the packet.  case 1-b and 3
    917 	 */
    918 	if (mtu < IPV6_MMTU) {
    919 		/* path MTU cannot be less than IPV6_MMTU */
    920 		error = EMSGSIZE;
    921 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
    922 		goto bad;
    923 	} else if (ip6->ip6_plen == 0) {
    924 		/* jumbo payload cannot be fragmented */
    925 		error = EMSGSIZE;
    926 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
    927 		goto bad;
    928 	} else {
    929 		struct mbuf **mnext, *m_frgpart;
    930 		struct ip6_frag *ip6f;
    931 		u_int32_t id = htonl(ip6_randomid());
    932 		u_char nextproto;
    933 		struct ip6ctlparam ip6cp;
    934 		u_int32_t mtu32;
    935 
    936 		/*
    937 		 * Too large for the destination or interface;
    938 		 * fragment if possible.
    939 		 * Must be able to put at least 8 bytes per fragment.
    940 		 */
    941 		hlen = unfragpartlen;
    942 		if (mtu > IPV6_MAXPACKET)
    943 			mtu = IPV6_MAXPACKET;
    944 
    945 		/* Notify a proper path MTU to applications. */
    946 		mtu32 = (u_int32_t)mtu;
    947 		bzero(&ip6cp, sizeof(ip6cp));
    948 		ip6cp.ip6c_cmdarg = (void *)&mtu32;
    949 		pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
    950 		    (void *)&ip6cp);
    951 
    952 		len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
    953 		if (len < 8) {
    954 			error = EMSGSIZE;
    955 			in6_ifstat_inc(ifp, ifs6_out_fragfail);
    956 			goto bad;
    957 		}
    958 
    959 		mnext = &m->m_nextpkt;
    960 
    961 		/*
    962 		 * Change the next header field of the last header in the
    963 		 * unfragmentable part.
    964 		 */
    965 		if (exthdrs.ip6e_rthdr) {
    966 			nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
    967 			*mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
    968 		} else if (exthdrs.ip6e_dest1) {
    969 			nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
    970 			*mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
    971 		} else if (exthdrs.ip6e_hbh) {
    972 			nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
    973 			*mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
    974 		} else {
    975 			nextproto = ip6->ip6_nxt;
    976 			ip6->ip6_nxt = IPPROTO_FRAGMENT;
    977 		}
    978 
    979 		if ((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6))
    980 		    != 0) {
    981 			if (IN6_NEED_CHECKSUM(ifp,
    982 			    m->m_pkthdr.csum_flags &
    983 			    (M_CSUM_UDPv6|M_CSUM_TCPv6))) {
    984 				in6_delayed_cksum(m);
    985 			}
    986 			m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
    987 		}
    988 
    989 		/*
    990 		 * Loop through length of segment after first fragment,
    991 		 * make new header and copy data of each part and link onto
    992 		 * chain.
    993 		 */
    994 		m0 = m;
    995 		for (off = hlen; off < tlen; off += len) {
    996 			struct mbuf *mlast;
    997 
    998 			MGETHDR(m, M_DONTWAIT, MT_HEADER);
    999 			if (!m) {
   1000 				error = ENOBUFS;
   1001 				ip6stat.ip6s_odropped++;
   1002 				goto sendorfree;
   1003 			}
   1004 			m->m_pkthdr.rcvif = NULL;
   1005 			m->m_flags = m0->m_flags & M_COPYFLAGS;
   1006 			*mnext = m;
   1007 			mnext = &m->m_nextpkt;
   1008 			m->m_data += max_linkhdr;
   1009 			mhip6 = mtod(m, struct ip6_hdr *);
   1010 			*mhip6 = *ip6;
   1011 			m->m_len = sizeof(*mhip6);
   1012 			error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
   1013 			if (error) {
   1014 				ip6stat.ip6s_odropped++;
   1015 				goto sendorfree;
   1016 			}
   1017 			ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7));
   1018 			if (off + len >= tlen)
   1019 				len = tlen - off;
   1020 			else
   1021 				ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
   1022 			mhip6->ip6_plen = htons((u_int16_t)(len + hlen +
   1023 			    sizeof(*ip6f) - sizeof(struct ip6_hdr)));
   1024 			if ((m_frgpart = m_copy(m0, off, len)) == 0) {
   1025 				error = ENOBUFS;
   1026 				ip6stat.ip6s_odropped++;
   1027 				goto sendorfree;
   1028 			}
   1029 			for (mlast = m; mlast->m_next; mlast = mlast->m_next)
   1030 				;
   1031 			mlast->m_next = m_frgpart;
   1032 			m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
   1033 			m->m_pkthdr.rcvif = (struct ifnet *)0;
   1034 			ip6f->ip6f_reserved = 0;
   1035 			ip6f->ip6f_ident = id;
   1036 			ip6f->ip6f_nxt = nextproto;
   1037 			ip6stat.ip6s_ofragments++;
   1038 			in6_ifstat_inc(ifp, ifs6_out_fragcreat);
   1039 		}
   1040 
   1041 		in6_ifstat_inc(ifp, ifs6_out_fragok);
   1042 	}
   1043 
   1044 	/*
   1045 	 * Remove leading garbages.
   1046 	 */
   1047 sendorfree:
   1048 	m = m0->m_nextpkt;
   1049 	m0->m_nextpkt = 0;
   1050 	m_freem(m0);
   1051 	for (m0 = m; m; m = m0) {
   1052 		m0 = m->m_nextpkt;
   1053 		m->m_nextpkt = 0;
   1054 		if (error == 0) {
   1055 			struct in6_ifaddr *ia6;
   1056 			ip6 = mtod(m, struct ip6_hdr *);
   1057 			ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
   1058 			if (ia6) {
   1059 				/*
   1060 				 * Record statistics for this interface
   1061 				 * address.
   1062 				 */
   1063 				ia6->ia_ifa.ifa_data.ifad_outbytes +=
   1064 				    m->m_pkthdr.len;
   1065 			}
   1066 #ifdef IPSEC
   1067 			/* clean ipsec history once it goes out of the node */
   1068 			ipsec_delaux(m);
   1069 #endif
   1070 			error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
   1071 		} else
   1072 			m_freem(m);
   1073 	}
   1074 
   1075 	if (error == 0)
   1076 		ip6stat.ip6s_fragmented++;
   1077 
   1078 done:
   1079 	if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
   1080 		RTFREE(ro->ro_rt);
   1081 	} else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
   1082 		RTFREE(ro_pmtu->ro_rt);
   1083 	}
   1084 
   1085 #ifdef IPSEC
   1086 	if (sp != NULL)
   1087 		key_freesp(sp);
   1088 #endif /* IPSEC */
   1089 
   1090 	return (error);
   1091 
   1092 freehdrs:
   1093 	m_freem(exthdrs.ip6e_hbh);	/* m_freem will check if mbuf is 0 */
   1094 	m_freem(exthdrs.ip6e_dest1);
   1095 	m_freem(exthdrs.ip6e_rthdr);
   1096 	m_freem(exthdrs.ip6e_dest2);
   1097 	/* FALLTHROUGH */
   1098 bad:
   1099 	m_freem(m);
   1100 	goto done;
   1101 }
   1102 
   1103 static int
   1104 ip6_copyexthdr(mp, hdr, hlen)
   1105 	struct mbuf **mp;
   1106 	caddr_t hdr;
   1107 	int hlen;
   1108 {
   1109 	struct mbuf *m;
   1110 
   1111 	if (hlen > MCLBYTES)
   1112 		return (ENOBUFS); /* XXX */
   1113 
   1114 	MGET(m, M_DONTWAIT, MT_DATA);
   1115 	if (!m)
   1116 		return (ENOBUFS);
   1117 
   1118 	if (hlen > MLEN) {
   1119 		MCLGET(m, M_DONTWAIT);
   1120 		if ((m->m_flags & M_EXT) == 0) {
   1121 			m_free(m);
   1122 			return (ENOBUFS);
   1123 		}
   1124 	}
   1125 	m->m_len = hlen;
   1126 	if (hdr)
   1127 		bcopy(hdr, mtod(m, caddr_t), hlen);
   1128 
   1129 	*mp = m;
   1130 	return (0);
   1131 }
   1132 
   1133 /*
   1134  * Process a delayed payload checksum calculation.
   1135  */
   1136 void
   1137 in6_delayed_cksum(struct mbuf *m)
   1138 {
   1139 	uint16_t csum, offset;
   1140 
   1141 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
   1142 	KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
   1143 	KASSERT((m->m_pkthdr.csum_flags
   1144 	    & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0);
   1145 
   1146 	offset = M_CSUM_DATA_IPv6_HL(m->m_pkthdr.csum_data);
   1147 	csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset);
   1148 	if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) {
   1149 		csum = 0xffff;
   1150 	}
   1151 
   1152 	offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
   1153 	if ((offset + sizeof(csum)) > m->m_len) {
   1154 		m_copyback(m, offset, sizeof(csum), &csum);
   1155 	} else {
   1156 		*(uint16_t *)(mtod(m, caddr_t) + offset) = csum;
   1157 	}
   1158 }
   1159 
   1160 /*
   1161  * Insert jumbo payload option.
   1162  */
   1163 static int
   1164 ip6_insert_jumboopt(exthdrs, plen)
   1165 	struct ip6_exthdrs *exthdrs;
   1166 	u_int32_t plen;
   1167 {
   1168 	struct mbuf *mopt;
   1169 	u_int8_t *optbuf;
   1170 	u_int32_t v;
   1171 
   1172 #define JUMBOOPTLEN	8	/* length of jumbo payload option and padding */
   1173 
   1174 	/*
   1175 	 * If there is no hop-by-hop options header, allocate new one.
   1176 	 * If there is one but it doesn't have enough space to store the
   1177 	 * jumbo payload option, allocate a cluster to store the whole options.
   1178 	 * Otherwise, use it to store the options.
   1179 	 */
   1180 	if (exthdrs->ip6e_hbh == 0) {
   1181 		MGET(mopt, M_DONTWAIT, MT_DATA);
   1182 		if (mopt == 0)
   1183 			return (ENOBUFS);
   1184 		mopt->m_len = JUMBOOPTLEN;
   1185 		optbuf = mtod(mopt, u_int8_t *);
   1186 		optbuf[1] = 0;	/* = ((JUMBOOPTLEN) >> 3) - 1 */
   1187 		exthdrs->ip6e_hbh = mopt;
   1188 	} else {
   1189 		struct ip6_hbh *hbh;
   1190 
   1191 		mopt = exthdrs->ip6e_hbh;
   1192 		if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
   1193 			/*
   1194 			 * XXX assumption:
   1195 			 * - exthdrs->ip6e_hbh is not referenced from places
   1196 			 *   other than exthdrs.
   1197 			 * - exthdrs->ip6e_hbh is not an mbuf chain.
   1198 			 */
   1199 			int oldoptlen = mopt->m_len;
   1200 			struct mbuf *n;
   1201 
   1202 			/*
   1203 			 * XXX: give up if the whole (new) hbh header does
   1204 			 * not fit even in an mbuf cluster.
   1205 			 */
   1206 			if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
   1207 				return (ENOBUFS);
   1208 
   1209 			/*
   1210 			 * As a consequence, we must always prepare a cluster
   1211 			 * at this point.
   1212 			 */
   1213 			MGET(n, M_DONTWAIT, MT_DATA);
   1214 			if (n) {
   1215 				MCLGET(n, M_DONTWAIT);
   1216 				if ((n->m_flags & M_EXT) == 0) {
   1217 					m_freem(n);
   1218 					n = NULL;
   1219 				}
   1220 			}
   1221 			if (!n)
   1222 				return (ENOBUFS);
   1223 			n->m_len = oldoptlen + JUMBOOPTLEN;
   1224 			bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
   1225 			    oldoptlen);
   1226 			optbuf = mtod(n, u_int8_t *) + oldoptlen;
   1227 			m_freem(mopt);
   1228 			mopt = exthdrs->ip6e_hbh = n;
   1229 		} else {
   1230 			optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
   1231 			mopt->m_len += JUMBOOPTLEN;
   1232 		}
   1233 		optbuf[0] = IP6OPT_PADN;
   1234 		optbuf[1] = 0;
   1235 
   1236 		/*
   1237 		 * Adjust the header length according to the pad and
   1238 		 * the jumbo payload option.
   1239 		 */
   1240 		hbh = mtod(mopt, struct ip6_hbh *);
   1241 		hbh->ip6h_len += (JUMBOOPTLEN >> 3);
   1242 	}
   1243 
   1244 	/* fill in the option. */
   1245 	optbuf[2] = IP6OPT_JUMBO;
   1246 	optbuf[3] = 4;
   1247 	v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
   1248 	bcopy(&v, &optbuf[4], sizeof(u_int32_t));
   1249 
   1250 	/* finally, adjust the packet header length */
   1251 	exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
   1252 
   1253 	return (0);
   1254 #undef JUMBOOPTLEN
   1255 }
   1256 
   1257 /*
   1258  * Insert fragment header and copy unfragmentable header portions.
   1259  */
   1260 static int
   1261 ip6_insertfraghdr(m0, m, hlen, frghdrp)
   1262 	struct mbuf *m0, *m;
   1263 	int hlen;
   1264 	struct ip6_frag **frghdrp;
   1265 {
   1266 	struct mbuf *n, *mlast;
   1267 
   1268 	if (hlen > sizeof(struct ip6_hdr)) {
   1269 		n = m_copym(m0, sizeof(struct ip6_hdr),
   1270 		    hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
   1271 		if (n == 0)
   1272 			return (ENOBUFS);
   1273 		m->m_next = n;
   1274 	} else
   1275 		n = m;
   1276 
   1277 	/* Search for the last mbuf of unfragmentable part. */
   1278 	for (mlast = n; mlast->m_next; mlast = mlast->m_next)
   1279 		;
   1280 
   1281 	if ((mlast->m_flags & M_EXT) == 0 &&
   1282 	    M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
   1283 		/* use the trailing space of the last mbuf for the fragment hdr */
   1284 		*frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
   1285 		    mlast->m_len);
   1286 		mlast->m_len += sizeof(struct ip6_frag);
   1287 		m->m_pkthdr.len += sizeof(struct ip6_frag);
   1288 	} else {
   1289 		/* allocate a new mbuf for the fragment header */
   1290 		struct mbuf *mfrg;
   1291 
   1292 		MGET(mfrg, M_DONTWAIT, MT_DATA);
   1293 		if (mfrg == 0)
   1294 			return (ENOBUFS);
   1295 		mfrg->m_len = sizeof(struct ip6_frag);
   1296 		*frghdrp = mtod(mfrg, struct ip6_frag *);
   1297 		mlast->m_next = mfrg;
   1298 	}
   1299 
   1300 	return (0);
   1301 }
   1302 
   1303 static int
   1304 ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup, alwaysfragp)
   1305 	struct route_in6 *ro_pmtu, *ro;
   1306 	struct ifnet *ifp;
   1307 	struct in6_addr *dst;
   1308 	u_long *mtup;
   1309 	int *alwaysfragp;
   1310 {
   1311 	u_int32_t mtu = 0;
   1312 	int alwaysfrag = 0;
   1313 	int error = 0;
   1314 
   1315 	if (ro_pmtu != ro) {
   1316 		/* The first hop and the final destination may differ. */
   1317 		struct sockaddr_in6 *sa6_dst =
   1318 		    (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
   1319 		if (ro_pmtu->ro_rt &&
   1320 		    ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
   1321 		      !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
   1322 			RTFREE(ro_pmtu->ro_rt);
   1323 			ro_pmtu->ro_rt = (struct rtentry *)NULL;
   1324 		}
   1325 		if (ro_pmtu->ro_rt == NULL) {
   1326 			bzero(sa6_dst, sizeof(*sa6_dst)); /* for safety */
   1327 			sa6_dst->sin6_family = AF_INET6;
   1328 			sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
   1329 			sa6_dst->sin6_addr = *dst;
   1330 
   1331 			rtalloc((struct route *)ro_pmtu);
   1332 		}
   1333 	}
   1334 	if (ro_pmtu->ro_rt) {
   1335 		u_int32_t ifmtu;
   1336 
   1337 		if (ifp == NULL)
   1338 			ifp = ro_pmtu->ro_rt->rt_ifp;
   1339 		ifmtu = IN6_LINKMTU(ifp);
   1340 		mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
   1341 		if (mtu == 0)
   1342 			mtu = ifmtu;
   1343 		else if (mtu < IPV6_MMTU) {
   1344 			/*
   1345 			 * RFC2460 section 5, last paragraph:
   1346 			 * if we record ICMPv6 too big message with
   1347 			 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
   1348 			 * or smaller, with fragment header attached.
   1349 			 * (fragment header is needed regardless from the
   1350 			 * packet size, for translators to identify packets)
   1351 			 */
   1352 			alwaysfrag = 1;
   1353 			mtu = IPV6_MMTU;
   1354 		} else if (mtu > ifmtu) {
   1355 			/*
   1356 			 * The MTU on the route is larger than the MTU on
   1357 			 * the interface!  This shouldn't happen, unless the
   1358 			 * MTU of the interface has been changed after the
   1359 			 * interface was brought up.  Change the MTU in the
   1360 			 * route to match the interface MTU (as long as the
   1361 			 * field isn't locked).
   1362 			 */
   1363 			mtu = ifmtu;
   1364 			if (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU))
   1365 				ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
   1366 		}
   1367 	} else if (ifp) {
   1368 		mtu = IN6_LINKMTU(ifp);
   1369 	} else
   1370 		error = EHOSTUNREACH; /* XXX */
   1371 
   1372 	*mtup = mtu;
   1373 	if (alwaysfragp)
   1374 		*alwaysfragp = alwaysfrag;
   1375 	return (error);
   1376 }
   1377 
   1378 /*
   1379  * IP6 socket option processing.
   1380  */
   1381 int
   1382 ip6_ctloutput(op, so, level, optname, mp)
   1383 	int op;
   1384 	struct socket *so;
   1385 	int level, optname;
   1386 	struct mbuf **mp;
   1387 {
   1388 	struct in6pcb *in6p = sotoin6pcb(so);
   1389 	struct mbuf *m = *mp;
   1390 	int optval = 0;
   1391 	int error = 0;
   1392 	struct proc *p = curproc;	/* XXX */
   1393 
   1394 	if (level == IPPROTO_IPV6) {
   1395 		switch (op) {
   1396 		case PRCO_SETOPT:
   1397 			switch (optname) {
   1398 			case IPV6_PKTOPTIONS:
   1399 				/* m is freed in ip6_pcbopts */
   1400 				return (ip6_pcbopts(&in6p->in6p_outputopts,
   1401 				    m, so));
   1402 			case IPV6_HOPOPTS:
   1403 			case IPV6_DSTOPTS:
   1404 				if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
   1405 					error = EPERM;
   1406 					break;
   1407 				}
   1408 				/* FALLTHROUGH */
   1409 			case IPV6_UNICAST_HOPS:
   1410 			case IPV6_RECVOPTS:
   1411 			case IPV6_RECVRETOPTS:
   1412 			case IPV6_RECVDSTADDR:
   1413 			case IPV6_PKTINFO:
   1414 			case IPV6_HOPLIMIT:
   1415 			case IPV6_RTHDR:
   1416 			case IPV6_FAITH:
   1417 			case IPV6_V6ONLY:
   1418 			case IPV6_USE_MIN_MTU:
   1419 				if (!m || m->m_len != sizeof(int)) {
   1420 					error = EINVAL;
   1421 					break;
   1422 				}
   1423 				optval = *mtod(m, int *);
   1424 				switch (optname) {
   1425 
   1426 				case IPV6_UNICAST_HOPS:
   1427 					if (optval < -1 || optval >= 256)
   1428 						error = EINVAL;
   1429 					else {
   1430 						/* -1 = kernel default */
   1431 						in6p->in6p_hops = optval;
   1432 					}
   1433 					break;
   1434 #define OPTSET(bit) \
   1435 do { \
   1436 	if (optval) \
   1437 		in6p->in6p_flags |= (bit); \
   1438 	else \
   1439 		in6p->in6p_flags &= ~(bit); \
   1440 } while (/*CONSTCOND*/ 0)
   1441 
   1442 				case IPV6_RECVOPTS:
   1443 					OPTSET(IN6P_RECVOPTS);
   1444 					break;
   1445 
   1446 				case IPV6_RECVRETOPTS:
   1447 					OPTSET(IN6P_RECVRETOPTS);
   1448 					break;
   1449 
   1450 				case IPV6_RECVDSTADDR:
   1451 					OPTSET(IN6P_RECVDSTADDR);
   1452 					break;
   1453 
   1454 				case IPV6_PKTINFO:
   1455 					OPTSET(IN6P_PKTINFO);
   1456 					break;
   1457 
   1458 				case IPV6_HOPLIMIT:
   1459 					OPTSET(IN6P_HOPLIMIT);
   1460 					break;
   1461 
   1462 				case IPV6_HOPOPTS:
   1463 					OPTSET(IN6P_HOPOPTS);
   1464 					break;
   1465 
   1466 				case IPV6_DSTOPTS:
   1467 					OPTSET(IN6P_DSTOPTS);
   1468 					break;
   1469 
   1470 				case IPV6_RTHDR:
   1471 					OPTSET(IN6P_RTHDR);
   1472 					break;
   1473 
   1474 				case IPV6_FAITH:
   1475 					OPTSET(IN6P_FAITH);
   1476 					break;
   1477 
   1478 				case IPV6_USE_MIN_MTU:
   1479 					OPTSET(IN6P_MINMTU);
   1480 					break;
   1481 
   1482 				case IPV6_V6ONLY:
   1483 					/*
   1484 					 * make setsockopt(IPV6_V6ONLY)
   1485 					 * available only prior to bind(2).
   1486 					 * see ipng mailing list, Jun 22 2001.
   1487 					 */
   1488 					if (in6p->in6p_lport ||
   1489 					    !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
   1490 						error = EINVAL;
   1491 						break;
   1492 					}
   1493 #ifdef INET6_BINDV6ONLY
   1494 					if (!optval)
   1495 						error = EINVAL;
   1496 #else
   1497 					OPTSET(IN6P_IPV6_V6ONLY);
   1498 #endif
   1499 					break;
   1500 				}
   1501 				break;
   1502 #undef OPTSET
   1503 
   1504 			case IPV6_MULTICAST_IF:
   1505 			case IPV6_MULTICAST_HOPS:
   1506 			case IPV6_MULTICAST_LOOP:
   1507 			case IPV6_JOIN_GROUP:
   1508 			case IPV6_LEAVE_GROUP:
   1509 				error =	ip6_setmoptions(optname,
   1510 				    &in6p->in6p_moptions, m);
   1511 				break;
   1512 
   1513 			case IPV6_PORTRANGE:
   1514 				optval = *mtod(m, int *);
   1515 
   1516 				switch (optval) {
   1517 				case IPV6_PORTRANGE_DEFAULT:
   1518 					in6p->in6p_flags &= ~(IN6P_LOWPORT);
   1519 					in6p->in6p_flags &= ~(IN6P_HIGHPORT);
   1520 					break;
   1521 
   1522 				case IPV6_PORTRANGE_HIGH:
   1523 					in6p->in6p_flags &= ~(IN6P_LOWPORT);
   1524 					in6p->in6p_flags |= IN6P_HIGHPORT;
   1525 					break;
   1526 
   1527 				case IPV6_PORTRANGE_LOW:
   1528 					in6p->in6p_flags &= ~(IN6P_HIGHPORT);
   1529 					in6p->in6p_flags |= IN6P_LOWPORT;
   1530 					break;
   1531 
   1532 				default:
   1533 					error = EINVAL;
   1534 					break;
   1535 				}
   1536 				break;
   1537 
   1538 #ifdef IPSEC
   1539 			case IPV6_IPSEC_POLICY:
   1540 			    {
   1541 				caddr_t req = NULL;
   1542 				size_t len = 0;
   1543 
   1544 				int priv = 0;
   1545 				if (p == 0 || suser(p->p_ucred, &p->p_acflag))
   1546 					priv = 0;
   1547 				else
   1548 					priv = 1;
   1549 				if (m) {
   1550 					req = mtod(m, caddr_t);
   1551 					len = m->m_len;
   1552 				}
   1553 				error = ipsec6_set_policy(in6p,
   1554 				                   optname, req, len, priv);
   1555 			    }
   1556 				break;
   1557 #endif /* IPSEC */
   1558 
   1559 			default:
   1560 				error = ENOPROTOOPT;
   1561 				break;
   1562 			}
   1563 			if (m)
   1564 				(void)m_free(m);
   1565 			break;
   1566 
   1567 		case PRCO_GETOPT:
   1568 			switch (optname) {
   1569 
   1570 			case IPV6_OPTIONS:
   1571 			case IPV6_RETOPTS:
   1572 				error = ENOPROTOOPT;
   1573 				break;
   1574 
   1575 			case IPV6_PKTOPTIONS:
   1576 				if (in6p->in6p_options) {
   1577 					*mp = m_copym(in6p->in6p_options, 0,
   1578 					    M_COPYALL, M_WAIT);
   1579 				} else {
   1580 					*mp = m_get(M_WAIT, MT_SOOPTS);
   1581 					(*mp)->m_len = 0;
   1582 				}
   1583 				break;
   1584 
   1585 			case IPV6_HOPOPTS:
   1586 			case IPV6_DSTOPTS:
   1587 				if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
   1588 					error = EPERM;
   1589 					break;
   1590 				}
   1591 				/* FALLTHROUGH */
   1592 			case IPV6_UNICAST_HOPS:
   1593 			case IPV6_RECVOPTS:
   1594 			case IPV6_RECVRETOPTS:
   1595 			case IPV6_RECVDSTADDR:
   1596 			case IPV6_PORTRANGE:
   1597 			case IPV6_PKTINFO:
   1598 			case IPV6_HOPLIMIT:
   1599 			case IPV6_RTHDR:
   1600 			case IPV6_FAITH:
   1601 			case IPV6_V6ONLY:
   1602 			case IPV6_USE_MIN_MTU:
   1603 				*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1604 				m->m_len = sizeof(int);
   1605 				switch (optname) {
   1606 
   1607 				case IPV6_UNICAST_HOPS:
   1608 					optval = in6p->in6p_hops;
   1609 					break;
   1610 
   1611 #define OPTBIT(bit) (in6p->in6p_flags & bit ? 1 : 0)
   1612 
   1613 				case IPV6_RECVOPTS:
   1614 					optval = OPTBIT(IN6P_RECVOPTS);
   1615 					break;
   1616 
   1617 				case IPV6_RECVRETOPTS:
   1618 					optval = OPTBIT(IN6P_RECVRETOPTS);
   1619 					break;
   1620 
   1621 				case IPV6_RECVDSTADDR:
   1622 					optval = OPTBIT(IN6P_RECVDSTADDR);
   1623 					break;
   1624 
   1625 				case IPV6_PORTRANGE:
   1626 				    {
   1627 					int flags;
   1628 					flags = in6p->in6p_flags;
   1629 					if (flags & IN6P_HIGHPORT)
   1630 						optval = IPV6_PORTRANGE_HIGH;
   1631 					else if (flags & IN6P_LOWPORT)
   1632 						optval = IPV6_PORTRANGE_LOW;
   1633 					else
   1634 						optval = 0;
   1635 					break;
   1636 				    }
   1637 
   1638 				case IPV6_PKTINFO:
   1639 					optval = OPTBIT(IN6P_PKTINFO);
   1640 					break;
   1641 
   1642 				case IPV6_HOPLIMIT:
   1643 					optval = OPTBIT(IN6P_HOPLIMIT);
   1644 					break;
   1645 
   1646 				case IPV6_HOPOPTS:
   1647 					optval = OPTBIT(IN6P_HOPOPTS);
   1648 					break;
   1649 
   1650 				case IPV6_DSTOPTS:
   1651 					optval = OPTBIT(IN6P_DSTOPTS);
   1652 					break;
   1653 
   1654 				case IPV6_RTHDR:
   1655 					optval = OPTBIT(IN6P_RTHDR);
   1656 					break;
   1657 
   1658 				case IPV6_FAITH:
   1659 					optval = OPTBIT(IN6P_FAITH);
   1660 					break;
   1661 
   1662 				case IPV6_V6ONLY:
   1663 					optval = OPTBIT(IN6P_IPV6_V6ONLY);
   1664 					break;
   1665 
   1666 				case IPV6_USE_MIN_MTU:
   1667 					optval = OPTBIT(IN6P_MINMTU);
   1668 					break;
   1669 				}
   1670 				*mtod(m, int *) = optval;
   1671 				break;
   1672 
   1673 			case IPV6_MULTICAST_IF:
   1674 			case IPV6_MULTICAST_HOPS:
   1675 			case IPV6_MULTICAST_LOOP:
   1676 			case IPV6_JOIN_GROUP:
   1677 			case IPV6_LEAVE_GROUP:
   1678 				error = ip6_getmoptions(optname, in6p->in6p_moptions, mp);
   1679 				break;
   1680 
   1681 #if 0	/* defined(IPSEC) */
   1682 			/* XXX: code broken */
   1683 			case IPV6_IPSEC_POLICY:
   1684 			{
   1685 				caddr_t req = NULL;
   1686 				size_t len = 0;
   1687 
   1688 				if (m) {
   1689 					req = mtod(m, caddr_t);
   1690 					len = m->m_len;
   1691 				}
   1692 				error = ipsec6_get_policy(in6p, req, len, mp);
   1693 				break;
   1694 			}
   1695 #endif /* IPSEC */
   1696 
   1697 			default:
   1698 				error = ENOPROTOOPT;
   1699 				break;
   1700 			}
   1701 			break;
   1702 		}
   1703 	} else {
   1704 		error = EINVAL;
   1705 		if (op == PRCO_SETOPT && *mp)
   1706 			(void)m_free(*mp);
   1707 	}
   1708 	return (error);
   1709 }
   1710 
   1711 int
   1712 ip6_raw_ctloutput(op, so, level, optname, mp)
   1713 	int op;
   1714 	struct socket *so;
   1715 	int level, optname;
   1716 	struct mbuf **mp;
   1717 {
   1718 	int error = 0, optval, optlen;
   1719 	const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
   1720 	struct in6pcb *in6p = sotoin6pcb(so);
   1721 	struct mbuf *m = *mp;
   1722 
   1723 	optlen = m ? m->m_len : 0;
   1724 
   1725 	if (level != IPPROTO_IPV6) {
   1726 		if (op == PRCO_SETOPT && *mp)
   1727 			(void)m_free(*mp);
   1728 		return (EINVAL);
   1729 	}
   1730 
   1731 	switch (optname) {
   1732 	case IPV6_CHECKSUM:
   1733 		/*
   1734 		 * For ICMPv6 sockets, no modification allowed for checksum
   1735 		 * offset, permit "no change" values to help existing apps.
   1736 		 *
   1737 		 * XXX 2292bis says: "An attempt to set IPV6_CHECKSUM
   1738 		 * for an ICMPv6 socket will fail."
   1739 		 * The current behavior does not meet 2292bis.
   1740 		 */
   1741 		switch (op) {
   1742 		case PRCO_SETOPT:
   1743 			if (optlen != sizeof(int)) {
   1744 				error = EINVAL;
   1745 				break;
   1746 			}
   1747 			optval = *mtod(m, int *);
   1748 			if ((optval % 2) != 0) {
   1749 				/* the API assumes even offset values */
   1750 				error = EINVAL;
   1751 			} else if (so->so_proto->pr_protocol ==
   1752 			    IPPROTO_ICMPV6) {
   1753 				if (optval != icmp6off)
   1754 					error = EINVAL;
   1755 			} else
   1756 				in6p->in6p_cksum = optval;
   1757 			break;
   1758 
   1759 		case PRCO_GETOPT:
   1760 			if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
   1761 				optval = icmp6off;
   1762 			else
   1763 				optval = in6p->in6p_cksum;
   1764 
   1765 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1766 			m->m_len = sizeof(int);
   1767 			*mtod(m, int *) = optval;
   1768 			break;
   1769 
   1770 		default:
   1771 			error = EINVAL;
   1772 			break;
   1773 		}
   1774 		break;
   1775 
   1776 	default:
   1777 		error = ENOPROTOOPT;
   1778 		break;
   1779 	}
   1780 
   1781 	if (op == PRCO_SETOPT && m)
   1782 		(void)m_free(m);
   1783 
   1784 	return (error);
   1785 }
   1786 
   1787 /*
   1788  * Set up IP6 options in pcb for insertion in output packets.
   1789  * Store in mbuf with pointer in pcbopt, adding pseudo-option
   1790  * with destination address if source routed.
   1791  */
   1792 static int
   1793 ip6_pcbopts(pktopt, m, so)
   1794 	struct ip6_pktopts **pktopt;
   1795 	struct mbuf *m;
   1796 	struct socket *so;
   1797 {
   1798 	struct ip6_pktopts *opt = *pktopt;
   1799 	int error = 0;
   1800 	struct proc *p = curproc;	/* XXX */
   1801 	int priv = 0;
   1802 
   1803 	/* turn off any old options. */
   1804 	if (opt) {
   1805 		if (opt->ip6po_m)
   1806 			(void)m_free(opt->ip6po_m);
   1807 	} else
   1808 		opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
   1809 	*pktopt = 0;
   1810 
   1811 	if (!m || m->m_len == 0) {
   1812 		/*
   1813 		 * Only turning off any previous options.
   1814 		 */
   1815 		free(opt, M_IP6OPT);
   1816 		if (m)
   1817 			(void)m_free(m);
   1818 		return (0);
   1819 	}
   1820 
   1821 	/*  set options specified by user. */
   1822 	if (p && !suser(p->p_ucred, &p->p_acflag))
   1823 		priv = 1;
   1824 	if ((error = ip6_setpktoptions(m, opt, priv)) != 0) {
   1825 		(void)m_free(m);
   1826 		free(opt, M_IP6OPT);
   1827 		return (error);
   1828 	}
   1829 	*pktopt = opt;
   1830 	return (0);
   1831 }
   1832 
   1833 /*
   1834  * Set the IP6 multicast options in response to user setsockopt().
   1835  */
   1836 static int
   1837 ip6_setmoptions(optname, im6op, m)
   1838 	int optname;
   1839 	struct ip6_moptions **im6op;
   1840 	struct mbuf *m;
   1841 {
   1842 	int error = 0;
   1843 	u_int loop, ifindex;
   1844 	struct ipv6_mreq *mreq;
   1845 	struct ifnet *ifp;
   1846 	struct ip6_moptions *im6o = *im6op;
   1847 	struct route_in6 ro;
   1848 	struct sockaddr_in6 *dst;
   1849 	struct in6_multi_mship *imm;
   1850 	struct proc *p = curproc;	/* XXX */
   1851 
   1852 	if (im6o == NULL) {
   1853 		/*
   1854 		 * No multicast option buffer attached to the pcb;
   1855 		 * allocate one and initialize to default values.
   1856 		 */
   1857 		im6o = (struct ip6_moptions *)
   1858 			malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
   1859 
   1860 		if (im6o == NULL)
   1861 			return (ENOBUFS);
   1862 		*im6op = im6o;
   1863 		im6o->im6o_multicast_ifp = NULL;
   1864 		im6o->im6o_multicast_hlim = ip6_defmcasthlim;
   1865 		im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
   1866 		LIST_INIT(&im6o->im6o_memberships);
   1867 	}
   1868 
   1869 	switch (optname) {
   1870 
   1871 	case IPV6_MULTICAST_IF:
   1872 		/*
   1873 		 * Select the interface for outgoing multicast packets.
   1874 		 */
   1875 		if (m == NULL || m->m_len != sizeof(u_int)) {
   1876 			error = EINVAL;
   1877 			break;
   1878 		}
   1879 		bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
   1880 		if (ifindex != 0) {
   1881 			if (ifindex < 0 || if_indexlim <= ifindex ||
   1882 			    !ifindex2ifnet[ifindex]) {
   1883 				error = ENXIO;	/* XXX EINVAL? */
   1884 				break;
   1885 			}
   1886 			ifp = ifindex2ifnet[ifindex];
   1887 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
   1888 				error = EADDRNOTAVAIL;
   1889 				break;
   1890 			}
   1891 		} else
   1892 			ifp = NULL;
   1893 		im6o->im6o_multicast_ifp = ifp;
   1894 		break;
   1895 
   1896 	case IPV6_MULTICAST_HOPS:
   1897 	    {
   1898 		/*
   1899 		 * Set the IP6 hoplimit for outgoing multicast packets.
   1900 		 */
   1901 		int optval;
   1902 		if (m == NULL || m->m_len != sizeof(int)) {
   1903 			error = EINVAL;
   1904 			break;
   1905 		}
   1906 		bcopy(mtod(m, u_int *), &optval, sizeof(optval));
   1907 		if (optval < -1 || optval >= 256)
   1908 			error = EINVAL;
   1909 		else if (optval == -1)
   1910 			im6o->im6o_multicast_hlim = ip6_defmcasthlim;
   1911 		else
   1912 			im6o->im6o_multicast_hlim = optval;
   1913 		break;
   1914 	    }
   1915 
   1916 	case IPV6_MULTICAST_LOOP:
   1917 		/*
   1918 		 * Set the loopback flag for outgoing multicast packets.
   1919 		 * Must be zero or one.
   1920 		 */
   1921 		if (m == NULL || m->m_len != sizeof(u_int)) {
   1922 			error = EINVAL;
   1923 			break;
   1924 		}
   1925 		bcopy(mtod(m, u_int *), &loop, sizeof(loop));
   1926 		if (loop > 1) {
   1927 			error = EINVAL;
   1928 			break;
   1929 		}
   1930 		im6o->im6o_multicast_loop = loop;
   1931 		break;
   1932 
   1933 	case IPV6_JOIN_GROUP:
   1934 		/*
   1935 		 * Add a multicast group membership.
   1936 		 * Group must be a valid IP6 multicast address.
   1937 		 */
   1938 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
   1939 			error = EINVAL;
   1940 			break;
   1941 		}
   1942 		mreq = mtod(m, struct ipv6_mreq *);
   1943 		if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
   1944 			/*
   1945 			 * We use the unspecified address to specify to accept
   1946 			 * all multicast addresses. Only super user is allowed
   1947 			 * to do this.
   1948 			 */
   1949 			if (suser(p->p_ucred, &p->p_acflag))
   1950 			{
   1951 				error = EACCES;
   1952 				break;
   1953 			}
   1954 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
   1955 			error = EINVAL;
   1956 			break;
   1957 		}
   1958 
   1959 		/*
   1960 		 * If the interface is specified, validate it.
   1961 		 * If no interface was explicitly specified, choose an
   1962 		 * appropriate one according to the given multicast address.
   1963 		 */
   1964 		if (mreq->ipv6mr_interface != 0) {
   1965 			if (mreq->ipv6mr_interface < 0 ||
   1966 			    if_indexlim <= mreq->ipv6mr_interface ||
   1967 			    !ifindex2ifnet[mreq->ipv6mr_interface]) {
   1968 				error = ENXIO;	/* XXX EINVAL? */
   1969 				break;
   1970 			}
   1971 			ifp = ifindex2ifnet[mreq->ipv6mr_interface];
   1972 		} else {
   1973 			/*
   1974 			 * If the multicast address is in node-local scope,
   1975 			 * the interface should be a loopback interface.
   1976 			 * Otherwise, look up the routing table for the
   1977 			 * address, and choose the outgoing interface.
   1978 			 *   XXX: is it a good approach?
   1979 			 */
   1980 			if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
   1981 				ifp = lo0ifp;
   1982 			} else {
   1983 				ro.ro_rt = NULL;
   1984 				dst = (struct sockaddr_in6 *)&ro.ro_dst;
   1985 				bzero(dst, sizeof(*dst));
   1986 				dst->sin6_len = sizeof(struct sockaddr_in6);
   1987 				dst->sin6_family = AF_INET6;
   1988 				dst->sin6_addr = mreq->ipv6mr_multiaddr;
   1989 				rtalloc((struct route *)&ro);
   1990 				if (ro.ro_rt == NULL) {
   1991 					error = EADDRNOTAVAIL;
   1992 					break;
   1993 				}
   1994 				ifp = ro.ro_rt->rt_ifp;
   1995 				rtfree(ro.ro_rt);
   1996 			}
   1997 		}
   1998 
   1999 		/*
   2000 		 * See if we found an interface, and confirm that it
   2001 		 * supports multicast
   2002 		 */
   2003 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   2004 			error = EADDRNOTAVAIL;
   2005 			break;
   2006 		}
   2007 		/*
   2008 		 * Put interface index into the multicast address,
   2009 		 * if the address has link-local scope.
   2010 		 */
   2011 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
   2012 			mreq->ipv6mr_multiaddr.s6_addr16[1] =
   2013 			    htons(ifp->if_index);
   2014 		}
   2015 		/*
   2016 		 * See if the membership already exists.
   2017 		 */
   2018 		for (imm = im6o->im6o_memberships.lh_first;
   2019 		     imm != NULL; imm = imm->i6mm_chain.le_next)
   2020 			if (imm->i6mm_maddr->in6m_ifp == ifp &&
   2021 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
   2022 			    &mreq->ipv6mr_multiaddr))
   2023 				break;
   2024 		if (imm != NULL) {
   2025 			error = EADDRINUSE;
   2026 			break;
   2027 		}
   2028 		/*
   2029 		 * Everything looks good; add a new record to the multicast
   2030 		 * address list for the given interface.
   2031 		 */
   2032 		imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error);
   2033 		if (!imm)
   2034 			break;
   2035 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
   2036 		break;
   2037 
   2038 	case IPV6_LEAVE_GROUP:
   2039 		/*
   2040 		 * Drop a multicast group membership.
   2041 		 * Group must be a valid IP6 multicast address.
   2042 		 */
   2043 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
   2044 			error = EINVAL;
   2045 			break;
   2046 		}
   2047 		mreq = mtod(m, struct ipv6_mreq *);
   2048 		if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
   2049 			if (suser(p->p_ucred, &p->p_acflag))
   2050 			{
   2051 				error = EACCES;
   2052 				break;
   2053 			}
   2054 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
   2055 			error = EINVAL;
   2056 			break;
   2057 		}
   2058 		/*
   2059 		 * If an interface address was specified, get a pointer
   2060 		 * to its ifnet structure.
   2061 		 */
   2062 		if (mreq->ipv6mr_interface != 0) {
   2063 			if (mreq->ipv6mr_interface < 0 ||
   2064 			    if_indexlim <= mreq->ipv6mr_interface ||
   2065 			    !ifindex2ifnet[mreq->ipv6mr_interface]) {
   2066 				error = ENXIO;	/* XXX EINVAL? */
   2067 				break;
   2068 			}
   2069 			ifp = ifindex2ifnet[mreq->ipv6mr_interface];
   2070 		} else
   2071 			ifp = NULL;
   2072 		/*
   2073 		 * Put interface index into the multicast address,
   2074 		 * if the address has link-local scope.
   2075 		 */
   2076 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
   2077 			mreq->ipv6mr_multiaddr.s6_addr16[1] =
   2078 			    htons(mreq->ipv6mr_interface);
   2079 		}
   2080 		/*
   2081 		 * Find the membership in the membership list.
   2082 		 */
   2083 		for (imm = im6o->im6o_memberships.lh_first;
   2084 		     imm != NULL; imm = imm->i6mm_chain.le_next) {
   2085 			if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
   2086 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
   2087 			    &mreq->ipv6mr_multiaddr))
   2088 				break;
   2089 		}
   2090 		if (imm == NULL) {
   2091 			/* Unable to resolve interface */
   2092 			error = EADDRNOTAVAIL;
   2093 			break;
   2094 		}
   2095 		/*
   2096 		 * Give up the multicast address record to which the
   2097 		 * membership points.
   2098 		 */
   2099 		LIST_REMOVE(imm, i6mm_chain);
   2100 		in6_leavegroup(imm);
   2101 		break;
   2102 
   2103 	default:
   2104 		error = EOPNOTSUPP;
   2105 		break;
   2106 	}
   2107 
   2108 	/*
   2109 	 * If all options have default values, no need to keep the mbuf.
   2110 	 */
   2111 	if (im6o->im6o_multicast_ifp == NULL &&
   2112 	    im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
   2113 	    im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
   2114 	    im6o->im6o_memberships.lh_first == NULL) {
   2115 		free(*im6op, M_IPMOPTS);
   2116 		*im6op = NULL;
   2117 	}
   2118 
   2119 	return (error);
   2120 }
   2121 
   2122 /*
   2123  * Return the IP6 multicast options in response to user getsockopt().
   2124  */
   2125 static int
   2126 ip6_getmoptions(optname, im6o, mp)
   2127 	int optname;
   2128 	struct ip6_moptions *im6o;
   2129 	struct mbuf **mp;
   2130 {
   2131 	u_int *hlim, *loop, *ifindex;
   2132 
   2133 	*mp = m_get(M_WAIT, MT_SOOPTS);
   2134 
   2135 	switch (optname) {
   2136 
   2137 	case IPV6_MULTICAST_IF:
   2138 		ifindex = mtod(*mp, u_int *);
   2139 		(*mp)->m_len = sizeof(u_int);
   2140 		if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
   2141 			*ifindex = 0;
   2142 		else
   2143 			*ifindex = im6o->im6o_multicast_ifp->if_index;
   2144 		return (0);
   2145 
   2146 	case IPV6_MULTICAST_HOPS:
   2147 		hlim = mtod(*mp, u_int *);
   2148 		(*mp)->m_len = sizeof(u_int);
   2149 		if (im6o == NULL)
   2150 			*hlim = ip6_defmcasthlim;
   2151 		else
   2152 			*hlim = im6o->im6o_multicast_hlim;
   2153 		return (0);
   2154 
   2155 	case IPV6_MULTICAST_LOOP:
   2156 		loop = mtod(*mp, u_int *);
   2157 		(*mp)->m_len = sizeof(u_int);
   2158 		if (im6o == NULL)
   2159 			*loop = ip6_defmcasthlim;
   2160 		else
   2161 			*loop = im6o->im6o_multicast_loop;
   2162 		return (0);
   2163 
   2164 	default:
   2165 		return (EOPNOTSUPP);
   2166 	}
   2167 }
   2168 
   2169 /*
   2170  * Discard the IP6 multicast options.
   2171  */
   2172 void
   2173 ip6_freemoptions(im6o)
   2174 	struct ip6_moptions *im6o;
   2175 {
   2176 	struct in6_multi_mship *imm;
   2177 
   2178 	if (im6o == NULL)
   2179 		return;
   2180 
   2181 	while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
   2182 		LIST_REMOVE(imm, i6mm_chain);
   2183 		in6_leavegroup(imm);
   2184 	}
   2185 	free(im6o, M_IPMOPTS);
   2186 }
   2187 
   2188 /*
   2189  * Set IPv6 outgoing packet options based on advanced API.
   2190  */
   2191 int
   2192 ip6_setpktoptions(control, opt, priv)
   2193 	struct mbuf *control;
   2194 	struct ip6_pktopts *opt;
   2195 	int priv;
   2196 {
   2197 	struct cmsghdr *cm = 0;
   2198 
   2199 	if (control == 0 || opt == 0)
   2200 		return (EINVAL);
   2201 
   2202 	bzero(opt, sizeof(*opt));
   2203 	opt->ip6po_hlim = -1; /* -1 means to use default hop limit */
   2204 
   2205 	/*
   2206 	 * XXX: Currently, we assume all the optional information is stored
   2207 	 * in a single mbuf.
   2208 	 */
   2209 	if (control->m_next)
   2210 		return (EINVAL);
   2211 
   2212 	opt->ip6po_m = control;
   2213 
   2214 	for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
   2215 	    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
   2216 		cm = mtod(control, struct cmsghdr *);
   2217 		if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
   2218 			return (EINVAL);
   2219 		if (cm->cmsg_level != IPPROTO_IPV6)
   2220 			continue;
   2221 
   2222 		switch (cm->cmsg_type) {
   2223 		case IPV6_PKTINFO:
   2224 			if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
   2225 				return (EINVAL);
   2226 			opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
   2227 			if (opt->ip6po_pktinfo->ipi6_ifindex &&
   2228 			    IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
   2229 				opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
   2230 					htons(opt->ip6po_pktinfo->ipi6_ifindex);
   2231 
   2232 			if (opt->ip6po_pktinfo->ipi6_ifindex >= if_indexlim ||
   2233 			    opt->ip6po_pktinfo->ipi6_ifindex < 0)
   2234 				return (ENXIO);
   2235 			if (opt->ip6po_pktinfo->ipi6_ifindex > 0 &&
   2236 			    !ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex])
   2237 				return (ENXIO);
   2238 
   2239 			/*
   2240 			 * Check if the requested source address is indeed a
   2241 			 * unicast address assigned to the node, and can be
   2242 			 * used as the packet's source address.
   2243 			 */
   2244 			if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
   2245 				struct ifaddr *ia;
   2246 				struct in6_ifaddr *ia6;
   2247 				struct sockaddr_in6 sin6;
   2248 
   2249 				bzero(&sin6, sizeof(sin6));
   2250 				sin6.sin6_len = sizeof(sin6);
   2251 				sin6.sin6_family = AF_INET6;
   2252 				sin6.sin6_addr =
   2253 					opt->ip6po_pktinfo->ipi6_addr;
   2254 				ia = ifa_ifwithaddr(sin6tosa(&sin6));
   2255 				if (ia == NULL ||
   2256 				    (opt->ip6po_pktinfo->ipi6_ifindex &&
   2257 				     (ia->ifa_ifp->if_index !=
   2258 				      opt->ip6po_pktinfo->ipi6_ifindex))) {
   2259 					return (EADDRNOTAVAIL);
   2260 				}
   2261 				ia6 = (struct in6_ifaddr *)ia;
   2262 				if ((ia6->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) != 0) {
   2263 					return (EADDRNOTAVAIL);
   2264 				}
   2265 
   2266 				/*
   2267 				 * Check if the requested source address is
   2268 				 * indeed a unicast address assigned to the
   2269 				 * node.
   2270 				 */
   2271 				if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr))
   2272 					return (EADDRNOTAVAIL);
   2273 			}
   2274 			break;
   2275 
   2276 		case IPV6_HOPLIMIT:
   2277 			if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
   2278 				return (EINVAL);
   2279 			else {
   2280 				int t;
   2281 
   2282 				bcopy(CMSG_DATA(cm), &t, sizeof(t));
   2283 				if (t < -1 || t > 255)
   2284 					return (EINVAL);
   2285 				opt->ip6po_hlim = t;
   2286 			}
   2287 			break;
   2288 
   2289 		case IPV6_NEXTHOP:
   2290 			if (!priv)
   2291 				return (EPERM);
   2292 
   2293 			/* check if cmsg_len is large enough for sa_len */
   2294 			if (cm->cmsg_len < sizeof(u_char) ||
   2295 			    cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
   2296 				return (EINVAL);
   2297 
   2298 			opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm);
   2299 
   2300 			break;
   2301 
   2302 		case IPV6_HOPOPTS:
   2303 			if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
   2304 				return (EINVAL);
   2305 			else {
   2306 				struct  ip6_hbh *t;
   2307 
   2308 				t = (struct ip6_hbh *)CMSG_DATA(cm);
   2309 				if (cm->cmsg_len !=
   2310 				    CMSG_LEN((t->ip6h_len + 1) << 3))
   2311 					return (EINVAL);
   2312 				opt->ip6po_hbh = t;
   2313 			}
   2314 			break;
   2315 
   2316 		case IPV6_DSTOPTS:
   2317 			if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
   2318 				return (EINVAL);
   2319 
   2320 			/*
   2321 			 * If there is no routing header yet, the destination
   2322 			 * options header should be put on the 1st part.
   2323 			 * Otherwise, the header should be on the 2nd part.
   2324 			 * (See RFC 2460, section 4.1)
   2325 			 */
   2326 			if (opt->ip6po_rthdr == NULL) {
   2327 				struct ip6_dest *t;
   2328 
   2329 				t = (struct ip6_dest *)CMSG_DATA(cm);
   2330 				if (cm->cmsg_len !=
   2331 				    CMSG_LEN((t->ip6d_len + 1) << 3));
   2332 					return (EINVAL);
   2333 				opt->ip6po_dest1 = t;
   2334 			}
   2335 			else {
   2336 				struct ip6_dest *t;
   2337 
   2338 				t = (struct ip6_dest *)CMSG_DATA(cm);
   2339 				if (cm->cmsg_len !=
   2340 				    CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1) << 3))
   2341 					return (EINVAL);
   2342 				opt->ip6po_dest2 = t;
   2343 			}
   2344 			break;
   2345 
   2346 		case IPV6_RTHDR:
   2347 			if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
   2348 				return (EINVAL);
   2349 			else {
   2350 				struct ip6_rthdr *t;
   2351 
   2352 				t = (struct ip6_rthdr *)CMSG_DATA(cm);
   2353 				if (cm->cmsg_len !=
   2354 				    CMSG_LEN((t->ip6r_len + 1) << 3))
   2355 					return (EINVAL);
   2356 				switch (t->ip6r_type) {
   2357 				case IPV6_RTHDR_TYPE_0:
   2358 					if (t->ip6r_segleft == 0)
   2359 						return (EINVAL);
   2360 					break;
   2361 				default:
   2362 					return (EINVAL);
   2363 				}
   2364 				opt->ip6po_rthdr = t;
   2365 			}
   2366 			break;
   2367 
   2368 		default:
   2369 			return (ENOPROTOOPT);
   2370 		}
   2371 	}
   2372 
   2373 	return (0);
   2374 }
   2375 
   2376 /*
   2377  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
   2378  * packet to the input queue of a specified interface.  Note that this
   2379  * calls the output routine of the loopback "driver", but with an interface
   2380  * pointer that might NOT be lo0ifp -- easier than replicating that code here.
   2381  */
   2382 void
   2383 ip6_mloopback(ifp, m, dst)
   2384 	struct ifnet *ifp;
   2385 	struct mbuf *m;
   2386 	struct sockaddr_in6 *dst;
   2387 {
   2388 	struct mbuf *copym;
   2389 	struct ip6_hdr *ip6;
   2390 
   2391 	copym = m_copy(m, 0, M_COPYALL);
   2392 	if (copym == NULL)
   2393 		return;
   2394 
   2395 	/*
   2396 	 * Make sure to deep-copy IPv6 header portion in case the data
   2397 	 * is in an mbuf cluster, so that we can safely override the IPv6
   2398 	 * header portion later.
   2399 	 */
   2400 	if ((copym->m_flags & M_EXT) != 0 ||
   2401 	    copym->m_len < sizeof(struct ip6_hdr)) {
   2402 		copym = m_pullup(copym, sizeof(struct ip6_hdr));
   2403 		if (copym == NULL)
   2404 			return;
   2405 	}
   2406 
   2407 #ifdef DIAGNOSTIC
   2408 	if (copym->m_len < sizeof(*ip6)) {
   2409 		m_freem(copym);
   2410 		return;
   2411 	}
   2412 #endif
   2413 
   2414 	ip6 = mtod(copym, struct ip6_hdr *);
   2415 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
   2416 		ip6->ip6_src.s6_addr16[1] = 0;
   2417 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
   2418 		ip6->ip6_dst.s6_addr16[1] = 0;
   2419 
   2420 	(void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
   2421 }
   2422 
   2423 /*
   2424  * Chop IPv6 header off from the payload.
   2425  */
   2426 static int
   2427 ip6_splithdr(m, exthdrs)
   2428 	struct mbuf *m;
   2429 	struct ip6_exthdrs *exthdrs;
   2430 {
   2431 	struct mbuf *mh;
   2432 	struct ip6_hdr *ip6;
   2433 
   2434 	ip6 = mtod(m, struct ip6_hdr *);
   2435 	if (m->m_len > sizeof(*ip6)) {
   2436 		MGETHDR(mh, M_DONTWAIT, MT_HEADER);
   2437 		if (mh == 0) {
   2438 			m_freem(m);
   2439 			return ENOBUFS;
   2440 		}
   2441 		M_MOVE_PKTHDR(mh, m);
   2442 		MH_ALIGN(mh, sizeof(*ip6));
   2443 		m->m_len -= sizeof(*ip6);
   2444 		m->m_data += sizeof(*ip6);
   2445 		mh->m_next = m;
   2446 		m = mh;
   2447 		m->m_len = sizeof(*ip6);
   2448 		bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
   2449 	}
   2450 	exthdrs->ip6e_ip6 = m;
   2451 	return 0;
   2452 }
   2453 
   2454 /*
   2455  * Compute IPv6 extension header length.
   2456  */
   2457 int
   2458 ip6_optlen(in6p)
   2459 	struct in6pcb *in6p;
   2460 {
   2461 	int len;
   2462 
   2463 	if (!in6p->in6p_outputopts)
   2464 		return 0;
   2465 
   2466 	len = 0;
   2467 #define elen(x) \
   2468     (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
   2469 
   2470 	len += elen(in6p->in6p_outputopts->ip6po_hbh);
   2471 	len += elen(in6p->in6p_outputopts->ip6po_dest1);
   2472 	len += elen(in6p->in6p_outputopts->ip6po_rthdr);
   2473 	len += elen(in6p->in6p_outputopts->ip6po_dest2);
   2474 	return len;
   2475 #undef elen
   2476 }
   2477