Home | History | Annotate | Line # | Download | only in netinet6
ip6_output.c revision 1.124
      1 /*	$NetBSD: ip6_output.c,v 1.124 2007/12/20 19:53:34 dyoung 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.124 2007/12/20 19:53:34 dyoung Exp $");
     66 
     67 #include "opt_inet.h"
     68 #include "opt_inet6.h"
     69 #include "opt_ipsec.h"
     70 #include "opt_pfil_hooks.h"
     71 
     72 #include <sys/param.h>
     73 #include <sys/malloc.h>
     74 #include <sys/mbuf.h>
     75 #include <sys/errno.h>
     76 #include <sys/protosw.h>
     77 #include <sys/socket.h>
     78 #include <sys/socketvar.h>
     79 #include <sys/systm.h>
     80 #include <sys/proc.h>
     81 #include <sys/kauth.h>
     82 
     83 #include <net/if.h>
     84 #include <net/route.h>
     85 #ifdef PFIL_HOOKS
     86 #include <net/pfil.h>
     87 #endif
     88 
     89 #include <netinet/in.h>
     90 #include <netinet/in_var.h>
     91 #include <netinet/ip6.h>
     92 #include <netinet/icmp6.h>
     93 #include <netinet/in_offload.h>
     94 #include <netinet6/in6_offload.h>
     95 #include <netinet6/ip6_var.h>
     96 #include <netinet6/in6_pcb.h>
     97 #include <netinet6/nd6.h>
     98 #include <netinet6/ip6protosw.h>
     99 #include <netinet6/scope6_var.h>
    100 
    101 #ifdef IPSEC
    102 #include <netinet6/ipsec.h>
    103 #include <netkey/key.h>
    104 #endif /* IPSEC */
    105 
    106 #ifdef FAST_IPSEC
    107 #include <netipsec/ipsec.h>
    108 #include <netipsec/ipsec6.h>
    109 #include <netipsec/key.h>
    110 #include <netipsec/xform.h>
    111 #endif
    112 
    113 
    114 #include <net/net_osdep.h>
    115 
    116 #ifdef PFIL_HOOKS
    117 extern struct pfil_head inet6_pfil_hook;	/* XXX */
    118 #endif
    119 
    120 struct ip6_exthdrs {
    121 	struct mbuf *ip6e_ip6;
    122 	struct mbuf *ip6e_hbh;
    123 	struct mbuf *ip6e_dest1;
    124 	struct mbuf *ip6e_rthdr;
    125 	struct mbuf *ip6e_dest2;
    126 };
    127 
    128 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
    129 	int, int);
    130 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct mbuf **);
    131 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *, int,
    132 	int, int, int);
    133 static int ip6_setmoptions(int, struct ip6_moptions **, struct mbuf *);
    134 static int ip6_getmoptions(int, struct ip6_moptions *, struct mbuf **);
    135 static int ip6_copyexthdr(struct mbuf **, void *, int);
    136 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
    137 	struct ip6_frag **);
    138 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
    139 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
    140 static int ip6_getpmtu(struct route *, struct route *, struct ifnet *,
    141     const struct in6_addr *, u_long *, int *);
    142 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
    143 
    144 #ifdef RFC2292
    145 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
    146 	struct socket *);
    147 #endif
    148 
    149 #define	IN6_NEED_CHECKSUM(ifp, csum_flags) \
    150 	(__predict_true(((ifp)->if_flags & IFF_LOOPBACK) == 0 || \
    151 	(((csum_flags) & M_CSUM_UDPv6) != 0 && udp_do_loopback_cksum) || \
    152 	(((csum_flags) & M_CSUM_TCPv6) != 0 && tcp_do_loopback_cksum)))
    153 
    154 /*
    155  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
    156  * header (with pri, len, nxt, hlim, src, dst).
    157  * This function may modify ver and hlim only.
    158  * The mbuf chain containing the packet will be freed.
    159  * The mbuf opt, if present, will not be freed.
    160  *
    161  * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
    162  * nd_ifinfo.linkmtu is u_int32_t.  so we use u_long to hold largest one,
    163  * which is rt_rmx.rmx_mtu.
    164  */
    165 int
    166 ip6_output(
    167     struct mbuf *m0,
    168     struct ip6_pktopts *opt,
    169     struct route *ro,
    170     int flags,
    171     struct ip6_moptions *im6o,
    172     struct socket *so,
    173     struct ifnet **ifpp		/* XXX: just for statistics */
    174 )
    175 {
    176 	struct ip6_hdr *ip6, *mhip6;
    177 	struct ifnet *ifp, *origifp;
    178 	struct mbuf *m = m0;
    179 	int hlen, tlen, len, off;
    180 	bool tso;
    181 	struct route ip6route;
    182 	struct rtentry *rt = NULL;
    183 	const struct sockaddr_in6 *dst = NULL;
    184 	struct sockaddr_in6 src_sa, dst_sa;
    185 	int error = 0;
    186 	struct in6_ifaddr *ia = NULL;
    187 	u_long mtu;
    188 	int alwaysfrag, dontfrag;
    189 	u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
    190 	struct ip6_exthdrs exthdrs;
    191 	struct in6_addr finaldst, src0, dst0;
    192 	u_int32_t zone;
    193 	struct route *ro_pmtu = NULL;
    194 	int hdrsplit = 0;
    195 	int needipsec = 0;
    196 #ifdef IPSEC
    197 	int needipsectun = 0;
    198 	struct secpolicy *sp = NULL;
    199 
    200 	ip6 = mtod(m, struct ip6_hdr *);
    201 #endif /* IPSEC */
    202 #ifdef FAST_IPSEC
    203 	struct secpolicy *sp = NULL;
    204 	int s;
    205 #endif
    206 
    207 	memset(&ip6route, 0, sizeof(ip6route));
    208 
    209 #ifdef  DIAGNOSTIC
    210 	if ((m->m_flags & M_PKTHDR) == 0)
    211 		panic("ip6_output: no HDR");
    212 
    213 	if ((m->m_pkthdr.csum_flags &
    214 	    (M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_TSOv4)) != 0) {
    215 		panic("ip6_output: IPv4 checksum offload flags: %d",
    216 		    m->m_pkthdr.csum_flags);
    217 	}
    218 
    219 	if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) ==
    220 	    (M_CSUM_TCPv6|M_CSUM_UDPv6)) {
    221 		panic("ip6_output: conflicting checksum offload flags: %d",
    222 		    m->m_pkthdr.csum_flags);
    223 	}
    224 #endif
    225 
    226 	M_CSUM_DATA_IPv6_HL_SET(m->m_pkthdr.csum_data, sizeof(struct ip6_hdr));
    227 
    228 #define MAKE_EXTHDR(hp, mp)						\
    229     do {								\
    230 	if (hp) {							\
    231 		struct ip6_ext *eh = (struct ip6_ext *)(hp);		\
    232 		error = ip6_copyexthdr((mp), (void *)(hp), 		\
    233 		    ((eh)->ip6e_len + 1) << 3);				\
    234 		if (error)						\
    235 			goto freehdrs;					\
    236 	}								\
    237     } while (/*CONSTCOND*/ 0)
    238 
    239 	bzero(&exthdrs, sizeof(exthdrs));
    240 	if (opt) {
    241 		/* Hop-by-Hop options header */
    242 		MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
    243 		/* Destination options header(1st part) */
    244 		MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
    245 		/* Routing header */
    246 		MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
    247 		/* Destination options header(2nd part) */
    248 		MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
    249 	}
    250 
    251 #ifdef IPSEC
    252 	if ((flags & IPV6_FORWARDING) != 0) {
    253 		needipsec = 0;
    254 		goto skippolicycheck;
    255 	}
    256 
    257 	/* get a security policy for this packet */
    258 	if (so == NULL)
    259 		sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
    260 	else {
    261 		if (IPSEC_PCB_SKIP_IPSEC(sotoinpcb_hdr(so)->inph_sp,
    262 					 IPSEC_DIR_OUTBOUND)) {
    263 			needipsec = 0;
    264 			goto skippolicycheck;
    265 		}
    266 		sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
    267 	}
    268 
    269 	if (sp == NULL) {
    270 		ipsec6stat.out_inval++;
    271 		goto freehdrs;
    272 	}
    273 
    274 	error = 0;
    275 
    276 	/* check policy */
    277 	switch (sp->policy) {
    278 	case IPSEC_POLICY_DISCARD:
    279 		/*
    280 		 * This packet is just discarded.
    281 		 */
    282 		ipsec6stat.out_polvio++;
    283 		goto freehdrs;
    284 
    285 	case IPSEC_POLICY_BYPASS:
    286 	case IPSEC_POLICY_NONE:
    287 		/* no need to do IPsec. */
    288 		needipsec = 0;
    289 		break;
    290 
    291 	case IPSEC_POLICY_IPSEC:
    292 		if (sp->req == NULL) {
    293 			/* XXX should be panic ? */
    294 			printf("ip6_output: No IPsec request specified.\n");
    295 			error = EINVAL;
    296 			goto freehdrs;
    297 		}
    298 		needipsec = 1;
    299 		break;
    300 
    301 	case IPSEC_POLICY_ENTRUST:
    302 	default:
    303 		printf("ip6_output: Invalid policy found. %d\n", sp->policy);
    304 	}
    305 
    306   skippolicycheck:;
    307 #endif /* IPSEC */
    308 
    309 	/*
    310 	 * Calculate the total length of the extension header chain.
    311 	 * Keep the length of the unfragmentable part for fragmentation.
    312 	 */
    313 	optlen = 0;
    314 	if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
    315 	if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
    316 	if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
    317 	unfragpartlen = optlen + sizeof(struct ip6_hdr);
    318 	/* NOTE: we don't add AH/ESP length here. do that later. */
    319 	if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
    320 
    321 #ifdef FAST_IPSEC
    322 	/* Check the security policy (SP) for the packet */
    323 
    324 	/* XXX For moment, we doesn't support packet with extented action */
    325 	if (optlen !=0)
    326 		goto freehdrs;
    327 
    328 	sp = ipsec6_check_policy(m,so,flags,&needipsec,&error);
    329 	if (error != 0) {
    330 		/*
    331 		 * Hack: -EINVAL is used to signal that a packet
    332 		 * should be silently discarded.  This is typically
    333 		 * because we asked key management for an SA and
    334 		 * it was delayed (e.g. kicked up to IKE).
    335 		 */
    336 	if (error == -EINVAL)
    337 		error = 0;
    338 	goto freehdrs;
    339     }
    340 #endif /* FAST_IPSEC */
    341 
    342 
    343 	if (needipsec &&
    344 	    (m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) {
    345 		in6_delayed_cksum(m);
    346 		m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
    347 	}
    348 
    349 
    350 	/*
    351 	 * If we need IPsec, or there is at least one extension header,
    352 	 * separate IP6 header from the payload.
    353 	 */
    354 	if ((needipsec || optlen) && !hdrsplit) {
    355 		if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
    356 			m = NULL;
    357 			goto freehdrs;
    358 		}
    359 		m = exthdrs.ip6e_ip6;
    360 		hdrsplit++;
    361 	}
    362 
    363 	/* adjust pointer */
    364 	ip6 = mtod(m, struct ip6_hdr *);
    365 
    366 	/* adjust mbuf packet header length */
    367 	m->m_pkthdr.len += optlen;
    368 	plen = m->m_pkthdr.len - sizeof(*ip6);
    369 
    370 	/* If this is a jumbo payload, insert a jumbo payload option. */
    371 	if (plen > IPV6_MAXPACKET) {
    372 		if (!hdrsplit) {
    373 			if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
    374 				m = NULL;
    375 				goto freehdrs;
    376 			}
    377 			m = exthdrs.ip6e_ip6;
    378 			hdrsplit++;
    379 		}
    380 		/* adjust pointer */
    381 		ip6 = mtod(m, struct ip6_hdr *);
    382 		if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
    383 			goto freehdrs;
    384 		optlen += 8; /* XXX JUMBOOPTLEN */
    385 		ip6->ip6_plen = 0;
    386 	} else
    387 		ip6->ip6_plen = htons(plen);
    388 
    389 	/*
    390 	 * Concatenate headers and fill in next header fields.
    391 	 * Here we have, on "m"
    392 	 *	IPv6 payload
    393 	 * and we insert headers accordingly.  Finally, we should be getting:
    394 	 *	IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
    395 	 *
    396 	 * during the header composing process, "m" points to IPv6 header.
    397 	 * "mprev" points to an extension header prior to esp.
    398 	 */
    399 	{
    400 		u_char *nexthdrp = &ip6->ip6_nxt;
    401 		struct mbuf *mprev = m;
    402 
    403 		/*
    404 		 * we treat dest2 specially.  this makes IPsec processing
    405 		 * much easier.  the goal here is to make mprev point the
    406 		 * mbuf prior to dest2.
    407 		 *
    408 		 * result: IPv6 dest2 payload
    409 		 * m and mprev will point to IPv6 header.
    410 		 */
    411 		if (exthdrs.ip6e_dest2) {
    412 			if (!hdrsplit)
    413 				panic("assumption failed: hdr not split");
    414 			exthdrs.ip6e_dest2->m_next = m->m_next;
    415 			m->m_next = exthdrs.ip6e_dest2;
    416 			*mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
    417 			ip6->ip6_nxt = IPPROTO_DSTOPTS;
    418 		}
    419 
    420 #define MAKE_CHAIN(m, mp, p, i)\
    421     do {\
    422 	if (m) {\
    423 		if (!hdrsplit) \
    424 			panic("assumption failed: hdr not split"); \
    425 		*mtod((m), u_char *) = *(p);\
    426 		*(p) = (i);\
    427 		p = mtod((m), u_char *);\
    428 		(m)->m_next = (mp)->m_next;\
    429 		(mp)->m_next = (m);\
    430 		(mp) = (m);\
    431 	}\
    432     } while (/*CONSTCOND*/ 0)
    433 		/*
    434 		 * result: IPv6 hbh dest1 rthdr dest2 payload
    435 		 * m will point to IPv6 header.  mprev will point to the
    436 		 * extension header prior to dest2 (rthdr in the above case).
    437 		 */
    438 		MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
    439 		MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
    440 		    IPPROTO_DSTOPTS);
    441 		MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
    442 		    IPPROTO_ROUTING);
    443 
    444 		M_CSUM_DATA_IPv6_HL_SET(m->m_pkthdr.csum_data,
    445 		    sizeof(struct ip6_hdr) + optlen);
    446 
    447 #ifdef IPSEC
    448 		if (!needipsec)
    449 			goto skip_ipsec2;
    450 
    451 		/*
    452 		 * pointers after IPsec headers are not valid any more.
    453 		 * other pointers need a great care too.
    454 		 * (IPsec routines should not mangle mbufs prior to AH/ESP)
    455 		 */
    456 		exthdrs.ip6e_dest2 = NULL;
    457 
    458 	    {
    459 		struct ip6_rthdr *rh = NULL;
    460 		int segleft_org = 0;
    461 		struct ipsec_output_state state;
    462 
    463 		if (exthdrs.ip6e_rthdr) {
    464 			rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
    465 			segleft_org = rh->ip6r_segleft;
    466 			rh->ip6r_segleft = 0;
    467 		}
    468 
    469 		bzero(&state, sizeof(state));
    470 		state.m = m;
    471 		error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
    472 		    &needipsectun);
    473 		m = state.m;
    474 		if (error) {
    475 			rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
    476 			/* mbuf is already reclaimed in ipsec6_output_trans. */
    477 			m = NULL;
    478 			switch (error) {
    479 			case EHOSTUNREACH:
    480 			case ENETUNREACH:
    481 			case EMSGSIZE:
    482 			case ENOBUFS:
    483 			case ENOMEM:
    484 				break;
    485 			default:
    486 				printf("ip6_output (ipsec): error code %d\n", error);
    487 				/* FALLTHROUGH */
    488 			case ENOENT:
    489 				/* don't show these error codes to the user */
    490 				error = 0;
    491 				break;
    492 			}
    493 			goto bad;
    494 		}
    495 		if (exthdrs.ip6e_rthdr) {
    496 			/* ah6_output doesn't modify mbuf chain */
    497 			rh->ip6r_segleft = segleft_org;
    498 		}
    499 	    }
    500 skip_ipsec2:;
    501 #endif
    502 	}
    503 
    504 	/*
    505 	 * If there is a routing header, replace destination address field
    506 	 * with the first hop of the routing header.
    507 	 */
    508 	if (exthdrs.ip6e_rthdr) {
    509 		struct ip6_rthdr *rh;
    510 		struct ip6_rthdr0 *rh0;
    511 		struct in6_addr *addr;
    512 		struct sockaddr_in6 sa;
    513 
    514 		rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
    515 		    struct ip6_rthdr *));
    516 		finaldst = ip6->ip6_dst;
    517 		switch (rh->ip6r_type) {
    518 		case IPV6_RTHDR_TYPE_0:
    519 			 rh0 = (struct ip6_rthdr0 *)rh;
    520 			 addr = (struct in6_addr *)(rh0 + 1);
    521 
    522 			 /*
    523 			  * construct a sockaddr_in6 form of
    524 			  * the first hop.
    525 			  *
    526 			  * XXX: we may not have enough
    527 			  * information about its scope zone;
    528 			  * there is no standard API to pass
    529 			  * the information from the
    530 			  * application.
    531 			  */
    532 			 sockaddr_in6_init(&sa, addr, 0, 0, 0);
    533 			 if ((error = sa6_embedscope(&sa,
    534 			     ip6_use_defzone)) != 0) {
    535 				 goto bad;
    536 			 }
    537 			 ip6->ip6_dst = sa.sin6_addr;
    538 			 (void)memmove(&addr[0], &addr[1],
    539 			     sizeof(struct in6_addr) *
    540 			     (rh0->ip6r0_segleft - 1));
    541 			 addr[rh0->ip6r0_segleft - 1] = finaldst;
    542 			 /* XXX */
    543 			 in6_clearscope(addr + rh0->ip6r0_segleft - 1);
    544 			 break;
    545 		default:	/* is it possible? */
    546 			 error = EINVAL;
    547 			 goto bad;
    548 		}
    549 	}
    550 
    551 	/* Source address validation */
    552 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
    553 	    (flags & IPV6_UNSPECSRC) == 0) {
    554 		error = EOPNOTSUPP;
    555 		ip6stat.ip6s_badscope++;
    556 		goto bad;
    557 	}
    558 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
    559 		error = EOPNOTSUPP;
    560 		ip6stat.ip6s_badscope++;
    561 		goto bad;
    562 	}
    563 
    564 	ip6stat.ip6s_localout++;
    565 
    566 	/*
    567 	 * Route packet.
    568 	 */
    569 	/* initialize cached route */
    570 	if (ro == NULL) {
    571 		ro = &ip6route;
    572 	}
    573 	ro_pmtu = ro;
    574 	if (opt && opt->ip6po_rthdr)
    575 		ro = &opt->ip6po_route;
    576 
    577  	/*
    578 	 * if specified, try to fill in the traffic class field.
    579 	 * do not override if a non-zero value is already set.
    580 	 * we check the diffserv field and the ecn field separately.
    581 	 */
    582 	if (opt && opt->ip6po_tclass >= 0) {
    583 		int mask = 0;
    584 
    585 		if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
    586 			mask |= 0xfc;
    587 		if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
    588 			mask |= 0x03;
    589 		if (mask != 0)
    590 			ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
    591 	}
    592 
    593 	/* fill in or override the hop limit field, if necessary. */
    594 	if (opt && opt->ip6po_hlim != -1)
    595 		ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
    596 	else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    597 		if (im6o != NULL)
    598 			ip6->ip6_hlim = im6o->im6o_multicast_hlim;
    599 		else
    600 			ip6->ip6_hlim = ip6_defmcasthlim;
    601 	}
    602 
    603 #ifdef IPSEC
    604 	if (needipsec && needipsectun) {
    605 		struct ipsec_output_state state;
    606 
    607 		/*
    608 		 * All the extension headers will become inaccessible
    609 		 * (since they can be encrypted).
    610 		 * Don't panic, we need no more updates to extension headers
    611 		 * on inner IPv6 packet (since they are now encapsulated).
    612 		 *
    613 		 * IPv6 [ESP|AH] IPv6 [extension headers] payload
    614 		 */
    615 		bzero(&exthdrs, sizeof(exthdrs));
    616 		exthdrs.ip6e_ip6 = m;
    617 
    618 		bzero(&state, sizeof(state));
    619 		state.m = m;
    620 		state.ro = ro;
    621 		state.dst = rtcache_getdst(ro);
    622 
    623 		error = ipsec6_output_tunnel(&state, sp, flags);
    624 
    625 		m = state.m;
    626 		ro_pmtu = ro = state.ro;
    627 		dst = satocsin6(state.dst);
    628 		if (error) {
    629 			/* mbuf is already reclaimed in ipsec6_output_tunnel. */
    630 			m0 = m = NULL;
    631 			m = NULL;
    632 			switch (error) {
    633 			case EHOSTUNREACH:
    634 			case ENETUNREACH:
    635 			case EMSGSIZE:
    636 			case ENOBUFS:
    637 			case ENOMEM:
    638 				break;
    639 			default:
    640 				printf("ip6_output (ipsec): error code %d\n", error);
    641 				/* FALLTHROUGH */
    642 			case ENOENT:
    643 				/* don't show these error codes to the user */
    644 				error = 0;
    645 				break;
    646 			}
    647 			goto bad;
    648 		}
    649 
    650 		exthdrs.ip6e_ip6 = m;
    651 	}
    652 #endif /* IPSEC */
    653 #ifdef FAST_IPSEC
    654 	if (needipsec) {
    655 		s = splsoftnet();
    656 		error = ipsec6_process_packet(m,sp->req);
    657 
    658 		/*
    659 		 * Preserve KAME behaviour: ENOENT can be returned
    660 		 * when an SA acquire is in progress.  Don't propagate
    661 		 * this to user-level; it confuses applications.
    662 		 * XXX this will go away when the SADB is redone.
    663 		 */
    664 		if (error == ENOENT)
    665 			error = 0;
    666 		splx(s);
    667 		goto done;
    668 	}
    669 #endif /* FAST_IPSEC */
    670 
    671 
    672 
    673 	/* adjust pointer */
    674 	ip6 = mtod(m, struct ip6_hdr *);
    675 
    676 	sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0);
    677 	if ((error = in6_selectroute(&dst_sa, opt, im6o, ro,
    678 	    &ifp, &rt, 0)) != 0) {
    679 		if (ifp != NULL)
    680 			in6_ifstat_inc(ifp, ifs6_out_discard);
    681 		goto bad;
    682 	}
    683 	if (rt == NULL) {
    684 		/*
    685 		 * If in6_selectroute() does not return a route entry,
    686 		 * dst may not have been updated.
    687 		 */
    688 		rtcache_setdst(ro, sin6tosa(&dst_sa));
    689 	}
    690 
    691 	/*
    692 	 * then rt (for unicast) and ifp must be non-NULL valid values.
    693 	 */
    694 	if ((flags & IPV6_FORWARDING) == 0) {
    695 		/* XXX: the FORWARDING flag can be set for mrouting. */
    696 		in6_ifstat_inc(ifp, ifs6_out_request);
    697 	}
    698 	if (rt != NULL) {
    699 		ia = (struct in6_ifaddr *)(rt->rt_ifa);
    700 		rt->rt_use++;
    701 	}
    702 
    703 	/*
    704 	 * The outgoing interface must be in the zone of source and
    705 	 * destination addresses.  We should use ia_ifp to support the
    706 	 * case of sending packets to an address of our own.
    707 	 */
    708 	if (ia != NULL && ia->ia_ifp)
    709 		origifp = ia->ia_ifp;
    710 	else
    711 		origifp = ifp;
    712 
    713 	src0 = ip6->ip6_src;
    714 	if (in6_setscope(&src0, origifp, &zone))
    715 		goto badscope;
    716 	sockaddr_in6_init(&src_sa, &ip6->ip6_src, 0, 0, 0);
    717 	if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
    718 		goto badscope;
    719 
    720 	dst0 = ip6->ip6_dst;
    721 	if (in6_setscope(&dst0, origifp, &zone))
    722 		goto badscope;
    723 	/* re-initialize to be sure */
    724 	sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0);
    725 	if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id)
    726 		goto badscope;
    727 
    728 	/* scope check is done. */
    729 
    730 	if (rt == NULL || IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    731 		if (dst == NULL)
    732 			dst = satocsin6(rtcache_getdst(ro));
    733 		KASSERT(dst != NULL);
    734 	} else if (opt && rtcache_getrt(&opt->ip6po_nextroute) != NULL) {
    735 		/*
    736 		 * The nexthop is explicitly specified by the
    737 		 * application.  We assume the next hop is an IPv6
    738 		 * address.
    739 		 */
    740 		dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
    741 	} else if ((rt->rt_flags & RTF_GATEWAY))
    742 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
    743 	else if (dst == NULL)
    744 		dst = satocsin6(rtcache_getdst(ro));
    745 
    746 	/*
    747 	 * XXXXXX: original code follows:
    748 	 */
    749 	if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
    750 		m->m_flags &= ~(M_BCAST | M_MCAST);	/* just in case */
    751 	else {
    752 		struct	in6_multi *in6m;
    753 
    754 		m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
    755 
    756 		in6_ifstat_inc(ifp, ifs6_out_mcast);
    757 
    758 		/*
    759 		 * Confirm that the outgoing interface supports multicast.
    760 		 */
    761 		if (!(ifp->if_flags & IFF_MULTICAST)) {
    762 			ip6stat.ip6s_noroute++;
    763 			in6_ifstat_inc(ifp, ifs6_out_discard);
    764 			error = ENETUNREACH;
    765 			goto bad;
    766 		}
    767 
    768 		IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
    769 		if (in6m != NULL &&
    770 		   (im6o == NULL || im6o->im6o_multicast_loop)) {
    771 			/*
    772 			 * If we belong to the destination multicast group
    773 			 * on the outgoing interface, and the caller did not
    774 			 * forbid loopback, loop back a copy.
    775 			 */
    776 			KASSERT(dst != NULL);
    777 			ip6_mloopback(ifp, m, dst);
    778 		} else {
    779 			/*
    780 			 * If we are acting as a multicast router, perform
    781 			 * multicast forwarding as if the packet had just
    782 			 * arrived on the interface to which we are about
    783 			 * to send.  The multicast forwarding function
    784 			 * recursively calls this function, using the
    785 			 * IPV6_FORWARDING flag to prevent infinite recursion.
    786 			 *
    787 			 * Multicasts that are looped back by ip6_mloopback(),
    788 			 * above, will be forwarded by the ip6_input() routine,
    789 			 * if necessary.
    790 			 */
    791 			if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
    792 				if (ip6_mforward(ip6, ifp, m) != 0) {
    793 					m_freem(m);
    794 					goto done;
    795 				}
    796 			}
    797 		}
    798 		/*
    799 		 * Multicasts with a hoplimit of zero may be looped back,
    800 		 * above, but must not be transmitted on a network.
    801 		 * Also, multicasts addressed to the loopback interface
    802 		 * are not sent -- the above call to ip6_mloopback() will
    803 		 * loop back a copy if this host actually belongs to the
    804 		 * destination group on the loopback interface.
    805 		 */
    806 		if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
    807 		    IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
    808 			m_freem(m);
    809 			goto done;
    810 		}
    811 	}
    812 
    813 	/*
    814 	 * Fill the outgoing inteface to tell the upper layer
    815 	 * to increment per-interface statistics.
    816 	 */
    817 	if (ifpp)
    818 		*ifpp = ifp;
    819 
    820 	/* Determine path MTU. */
    821 	if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
    822 	    &alwaysfrag)) != 0)
    823 		goto bad;
    824 #ifdef IPSEC
    825 	if (needipsectun)
    826 		mtu = IPV6_MMTU;
    827 #endif
    828 
    829 	/*
    830 	 * The caller of this function may specify to use the minimum MTU
    831 	 * in some cases.
    832 	 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
    833 	 * setting.  The logic is a bit complicated; by default, unicast
    834 	 * packets will follow path MTU while multicast packets will be sent at
    835 	 * the minimum MTU.  If IP6PO_MINMTU_ALL is specified, all packets
    836 	 * including unicast ones will be sent at the minimum MTU.  Multicast
    837 	 * packets will always be sent at the minimum MTU unless
    838 	 * IP6PO_MINMTU_DISABLE is explicitly specified.
    839 	 * See RFC 3542 for more details.
    840 	 */
    841 	if (mtu > IPV6_MMTU) {
    842 		if ((flags & IPV6_MINMTU))
    843 			mtu = IPV6_MMTU;
    844 		else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
    845 			mtu = IPV6_MMTU;
    846 		else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
    847 			 (opt == NULL ||
    848 			  opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
    849 			mtu = IPV6_MMTU;
    850 		}
    851 	}
    852 
    853 	/*
    854 	 * clear embedded scope identifiers if necessary.
    855 	 * in6_clearscope will touch the addresses only when necessary.
    856 	 */
    857 	in6_clearscope(&ip6->ip6_src);
    858 	in6_clearscope(&ip6->ip6_dst);
    859 
    860 	/*
    861 	 * If the outgoing packet contains a hop-by-hop options header,
    862 	 * it must be examined and processed even by the source node.
    863 	 * (RFC 2460, section 4.)
    864 	 */
    865 	if (exthdrs.ip6e_hbh) {
    866 		struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
    867 		u_int32_t dummy1; /* XXX unused */
    868 		u_int32_t dummy2; /* XXX unused */
    869 
    870 		/*
    871 		 *  XXX: if we have to send an ICMPv6 error to the sender,
    872 		 *       we need the M_LOOP flag since icmp6_error() expects
    873 		 *       the IPv6 and the hop-by-hop options header are
    874 		 *       continuous unless the flag is set.
    875 		 */
    876 		m->m_flags |= M_LOOP;
    877 		m->m_pkthdr.rcvif = ifp;
    878 		if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
    879 		    ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
    880 		    &dummy1, &dummy2) < 0) {
    881 			/* m was already freed at this point */
    882 			error = EINVAL;/* better error? */
    883 			goto done;
    884 		}
    885 		m->m_flags &= ~M_LOOP; /* XXX */
    886 		m->m_pkthdr.rcvif = NULL;
    887 	}
    888 
    889 #ifdef PFIL_HOOKS
    890 	/*
    891 	 * Run through list of hooks for output packets.
    892 	 */
    893 	if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
    894 		goto done;
    895 	if (m == NULL)
    896 		goto done;
    897 	ip6 = mtod(m, struct ip6_hdr *);
    898 #endif /* PFIL_HOOKS */
    899 	/*
    900 	 * Send the packet to the outgoing interface.
    901 	 * If necessary, do IPv6 fragmentation before sending.
    902 	 *
    903 	 * the logic here is rather complex:
    904 	 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
    905 	 * 1-a:	send as is if tlen <= path mtu
    906 	 * 1-b:	fragment if tlen > path mtu
    907 	 *
    908 	 * 2: if user asks us not to fragment (dontfrag == 1)
    909 	 * 2-a:	send as is if tlen <= interface mtu
    910 	 * 2-b:	error if tlen > interface mtu
    911 	 *
    912 	 * 3: if we always need to attach fragment header (alwaysfrag == 1)
    913 	 *	always fragment
    914 	 *
    915 	 * 4: if dontfrag == 1 && alwaysfrag == 1
    916 	 *	error, as we cannot handle this conflicting request
    917 	 */
    918 	tlen = m->m_pkthdr.len;
    919 	tso = (m->m_pkthdr.csum_flags & M_CSUM_TSOv6) != 0;
    920 	if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
    921 		dontfrag = 1;
    922 	else
    923 		dontfrag = 0;
    924 
    925 	if (dontfrag && alwaysfrag) {	/* case 4 */
    926 		/* conflicting request - can't transmit */
    927 		error = EMSGSIZE;
    928 		goto bad;
    929 	}
    930 	if (dontfrag && (!tso && tlen > IN6_LINKMTU(ifp))) {	/* case 2-b */
    931 		/*
    932 		 * Even if the DONTFRAG option is specified, we cannot send the
    933 		 * packet when the data length is larger than the MTU of the
    934 		 * outgoing interface.
    935 		 * Notify the error by sending IPV6_PATHMTU ancillary data as
    936 		 * well as returning an error code (the latter is not described
    937 		 * in the API spec.)
    938 		 */
    939 		u_int32_t mtu32;
    940 		struct ip6ctlparam ip6cp;
    941 
    942 		mtu32 = (u_int32_t)mtu;
    943 		bzero(&ip6cp, sizeof(ip6cp));
    944 		ip6cp.ip6c_cmdarg = (void *)&mtu32;
    945 		pfctlinput2(PRC_MSGSIZE,
    946 		    rtcache_getdst(ro_pmtu), &ip6cp);
    947 
    948 		error = EMSGSIZE;
    949 		goto bad;
    950 	}
    951 
    952 	/*
    953 	 * transmit packet without fragmentation
    954 	 */
    955 	if (dontfrag || (!alwaysfrag && (tlen <= mtu || tso))) {
    956 		/* case 1-a and 2-a */
    957 		struct in6_ifaddr *ia6;
    958 		int sw_csum;
    959 
    960 		ip6 = mtod(m, struct ip6_hdr *);
    961 		ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
    962 		if (ia6) {
    963 			/* Record statistics for this interface address. */
    964 			ia6->ia_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len;
    965 		}
    966 #ifdef IPSEC
    967 		/* clean ipsec history once it goes out of the node */
    968 		ipsec_delaux(m);
    969 #endif
    970 
    971 		sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
    972 		if ((sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) {
    973 			if (IN6_NEED_CHECKSUM(ifp,
    974 			    sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6))) {
    975 				in6_delayed_cksum(m);
    976 			}
    977 			m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
    978 		}
    979 
    980 		KASSERT(dst != NULL);
    981 		if (__predict_true(!tso ||
    982 		    (ifp->if_capenable & IFCAP_TSOv6) != 0)) {
    983 			error = nd6_output(ifp, origifp, m, dst, rt);
    984 		} else {
    985 			error = ip6_tso_output(ifp, origifp, m, dst, rt);
    986 		}
    987 		goto done;
    988 	}
    989 
    990 	if (tso) {
    991 		error = EINVAL; /* XXX */
    992 		goto bad;
    993 	}
    994 
    995 	/*
    996 	 * try to fragment the packet.  case 1-b and 3
    997 	 */
    998 	if (mtu < IPV6_MMTU) {
    999 		/* path MTU cannot be less than IPV6_MMTU */
   1000 		error = EMSGSIZE;
   1001 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
   1002 		goto bad;
   1003 	} else if (ip6->ip6_plen == 0) {
   1004 		/* jumbo payload cannot be fragmented */
   1005 		error = EMSGSIZE;
   1006 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
   1007 		goto bad;
   1008 	} else {
   1009 		struct mbuf **mnext, *m_frgpart;
   1010 		struct ip6_frag *ip6f;
   1011 		u_int32_t id = htonl(ip6_randomid());
   1012 		u_char nextproto;
   1013 #if 0				/* see below */
   1014 		struct ip6ctlparam ip6cp;
   1015 		u_int32_t mtu32;
   1016 #endif
   1017 
   1018 		/*
   1019 		 * Too large for the destination or interface;
   1020 		 * fragment if possible.
   1021 		 * Must be able to put at least 8 bytes per fragment.
   1022 		 */
   1023 		hlen = unfragpartlen;
   1024 		if (mtu > IPV6_MAXPACKET)
   1025 			mtu = IPV6_MAXPACKET;
   1026 
   1027 #if 0
   1028 		/*
   1029 		 * It is believed this code is a leftover from the
   1030 		 * development of the IPV6_RECVPATHMTU sockopt and
   1031 		 * associated work to implement RFC3542.
   1032 		 * It's not entirely clear what the intent of the API
   1033 		 * is at this point, so disable this code for now.
   1034 		 * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG
   1035 		 * will send notifications if the application requests.
   1036 		 */
   1037 
   1038 		/* Notify a proper path MTU to applications. */
   1039 		mtu32 = (u_int32_t)mtu;
   1040 		bzero(&ip6cp, sizeof(ip6cp));
   1041 		ip6cp.ip6c_cmdarg = (void *)&mtu32;
   1042 		pfctlinput2(PRC_MSGSIZE,
   1043 		    rtcache_getdst(ro_pmtu), &ip6cp);
   1044 #endif
   1045 
   1046 		len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
   1047 		if (len < 8) {
   1048 			error = EMSGSIZE;
   1049 			in6_ifstat_inc(ifp, ifs6_out_fragfail);
   1050 			goto bad;
   1051 		}
   1052 
   1053 		mnext = &m->m_nextpkt;
   1054 
   1055 		/*
   1056 		 * Change the next header field of the last header in the
   1057 		 * unfragmentable part.
   1058 		 */
   1059 		if (exthdrs.ip6e_rthdr) {
   1060 			nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
   1061 			*mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
   1062 		} else if (exthdrs.ip6e_dest1) {
   1063 			nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
   1064 			*mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
   1065 		} else if (exthdrs.ip6e_hbh) {
   1066 			nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
   1067 			*mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
   1068 		} else {
   1069 			nextproto = ip6->ip6_nxt;
   1070 			ip6->ip6_nxt = IPPROTO_FRAGMENT;
   1071 		}
   1072 
   1073 		if ((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6))
   1074 		    != 0) {
   1075 			if (IN6_NEED_CHECKSUM(ifp,
   1076 			    m->m_pkthdr.csum_flags &
   1077 			    (M_CSUM_UDPv6|M_CSUM_TCPv6))) {
   1078 				in6_delayed_cksum(m);
   1079 			}
   1080 			m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
   1081 		}
   1082 
   1083 		/*
   1084 		 * Loop through length of segment after first fragment,
   1085 		 * make new header and copy data of each part and link onto
   1086 		 * chain.
   1087 		 */
   1088 		m0 = m;
   1089 		for (off = hlen; off < tlen; off += len) {
   1090 			struct mbuf *mlast;
   1091 
   1092 			MGETHDR(m, M_DONTWAIT, MT_HEADER);
   1093 			if (!m) {
   1094 				error = ENOBUFS;
   1095 				ip6stat.ip6s_odropped++;
   1096 				goto sendorfree;
   1097 			}
   1098 			m->m_pkthdr.rcvif = NULL;
   1099 			m->m_flags = m0->m_flags & M_COPYFLAGS;
   1100 			*mnext = m;
   1101 			mnext = &m->m_nextpkt;
   1102 			m->m_data += max_linkhdr;
   1103 			mhip6 = mtod(m, struct ip6_hdr *);
   1104 			*mhip6 = *ip6;
   1105 			m->m_len = sizeof(*mhip6);
   1106 			error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
   1107 			if (error) {
   1108 				ip6stat.ip6s_odropped++;
   1109 				goto sendorfree;
   1110 			}
   1111 			ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7));
   1112 			if (off + len >= tlen)
   1113 				len = tlen - off;
   1114 			else
   1115 				ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
   1116 			mhip6->ip6_plen = htons((u_int16_t)(len + hlen +
   1117 			    sizeof(*ip6f) - sizeof(struct ip6_hdr)));
   1118 			if ((m_frgpart = m_copy(m0, off, len)) == 0) {
   1119 				error = ENOBUFS;
   1120 				ip6stat.ip6s_odropped++;
   1121 				goto sendorfree;
   1122 			}
   1123 			for (mlast = m; mlast->m_next; mlast = mlast->m_next)
   1124 				;
   1125 			mlast->m_next = m_frgpart;
   1126 			m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
   1127 			m->m_pkthdr.rcvif = (struct ifnet *)0;
   1128 			ip6f->ip6f_reserved = 0;
   1129 			ip6f->ip6f_ident = id;
   1130 			ip6f->ip6f_nxt = nextproto;
   1131 			ip6stat.ip6s_ofragments++;
   1132 			in6_ifstat_inc(ifp, ifs6_out_fragcreat);
   1133 		}
   1134 
   1135 		in6_ifstat_inc(ifp, ifs6_out_fragok);
   1136 	}
   1137 
   1138 	/*
   1139 	 * Remove leading garbages.
   1140 	 */
   1141 sendorfree:
   1142 	m = m0->m_nextpkt;
   1143 	m0->m_nextpkt = 0;
   1144 	m_freem(m0);
   1145 	for (m0 = m; m; m = m0) {
   1146 		m0 = m->m_nextpkt;
   1147 		m->m_nextpkt = 0;
   1148 		if (error == 0) {
   1149 			struct in6_ifaddr *ia6;
   1150 			ip6 = mtod(m, struct ip6_hdr *);
   1151 			ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
   1152 			if (ia6) {
   1153 				/*
   1154 				 * Record statistics for this interface
   1155 				 * address.
   1156 				 */
   1157 				ia6->ia_ifa.ifa_data.ifad_outbytes +=
   1158 				    m->m_pkthdr.len;
   1159 			}
   1160 #ifdef IPSEC
   1161 			/* clean ipsec history once it goes out of the node */
   1162 			ipsec_delaux(m);
   1163 #endif
   1164 			KASSERT(dst != NULL);
   1165 			error = nd6_output(ifp, origifp, m, dst, rt);
   1166 		} else
   1167 			m_freem(m);
   1168 	}
   1169 
   1170 	if (error == 0)
   1171 		ip6stat.ip6s_fragmented++;
   1172 
   1173 done:
   1174 	rtcache_free(&ip6route);
   1175 
   1176 #ifdef IPSEC
   1177 	if (sp != NULL)
   1178 		key_freesp(sp);
   1179 #endif /* IPSEC */
   1180 #ifdef FAST_IPSEC
   1181 	if (sp != NULL)
   1182 		KEY_FREESP(&sp);
   1183 #endif /* FAST_IPSEC */
   1184 
   1185 
   1186 	return (error);
   1187 
   1188 freehdrs:
   1189 	m_freem(exthdrs.ip6e_hbh);	/* m_freem will check if mbuf is 0 */
   1190 	m_freem(exthdrs.ip6e_dest1);
   1191 	m_freem(exthdrs.ip6e_rthdr);
   1192 	m_freem(exthdrs.ip6e_dest2);
   1193 	/* FALLTHROUGH */
   1194 bad:
   1195 	m_freem(m);
   1196 	goto done;
   1197 badscope:
   1198 	ip6stat.ip6s_badscope++;
   1199 	in6_ifstat_inc(origifp, ifs6_out_discard);
   1200 	if (error == 0)
   1201 		error = EHOSTUNREACH; /* XXX */
   1202 	goto bad;
   1203 }
   1204 
   1205 static int
   1206 ip6_copyexthdr(struct mbuf **mp, void *hdr, int hlen)
   1207 {
   1208 	struct mbuf *m;
   1209 
   1210 	if (hlen > MCLBYTES)
   1211 		return (ENOBUFS); /* XXX */
   1212 
   1213 	MGET(m, M_DONTWAIT, MT_DATA);
   1214 	if (!m)
   1215 		return (ENOBUFS);
   1216 
   1217 	if (hlen > MLEN) {
   1218 		MCLGET(m, M_DONTWAIT);
   1219 		if ((m->m_flags & M_EXT) == 0) {
   1220 			m_free(m);
   1221 			return (ENOBUFS);
   1222 		}
   1223 	}
   1224 	m->m_len = hlen;
   1225 	if (hdr)
   1226 		bcopy(hdr, mtod(m, void *), hlen);
   1227 
   1228 	*mp = m;
   1229 	return (0);
   1230 }
   1231 
   1232 /*
   1233  * Process a delayed payload checksum calculation.
   1234  */
   1235 void
   1236 in6_delayed_cksum(struct mbuf *m)
   1237 {
   1238 	uint16_t csum, offset;
   1239 
   1240 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
   1241 	KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
   1242 	KASSERT((m->m_pkthdr.csum_flags
   1243 	    & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0);
   1244 
   1245 	offset = M_CSUM_DATA_IPv6_HL(m->m_pkthdr.csum_data);
   1246 	csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset);
   1247 	if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) {
   1248 		csum = 0xffff;
   1249 	}
   1250 
   1251 	offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
   1252 	if ((offset + sizeof(csum)) > m->m_len) {
   1253 		m_copyback(m, offset, sizeof(csum), &csum);
   1254 	} else {
   1255 		*(uint16_t *)(mtod(m, char *) + offset) = csum;
   1256 	}
   1257 }
   1258 
   1259 /*
   1260  * Insert jumbo payload option.
   1261  */
   1262 static int
   1263 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
   1264 {
   1265 	struct mbuf *mopt;
   1266 	u_int8_t *optbuf;
   1267 	u_int32_t v;
   1268 
   1269 #define JUMBOOPTLEN	8	/* length of jumbo payload option and padding */
   1270 
   1271 	/*
   1272 	 * If there is no hop-by-hop options header, allocate new one.
   1273 	 * If there is one but it doesn't have enough space to store the
   1274 	 * jumbo payload option, allocate a cluster to store the whole options.
   1275 	 * Otherwise, use it to store the options.
   1276 	 */
   1277 	if (exthdrs->ip6e_hbh == 0) {
   1278 		MGET(mopt, M_DONTWAIT, MT_DATA);
   1279 		if (mopt == 0)
   1280 			return (ENOBUFS);
   1281 		mopt->m_len = JUMBOOPTLEN;
   1282 		optbuf = mtod(mopt, u_int8_t *);
   1283 		optbuf[1] = 0;	/* = ((JUMBOOPTLEN) >> 3) - 1 */
   1284 		exthdrs->ip6e_hbh = mopt;
   1285 	} else {
   1286 		struct ip6_hbh *hbh;
   1287 
   1288 		mopt = exthdrs->ip6e_hbh;
   1289 		if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
   1290 			/*
   1291 			 * XXX assumption:
   1292 			 * - exthdrs->ip6e_hbh is not referenced from places
   1293 			 *   other than exthdrs.
   1294 			 * - exthdrs->ip6e_hbh is not an mbuf chain.
   1295 			 */
   1296 			int oldoptlen = mopt->m_len;
   1297 			struct mbuf *n;
   1298 
   1299 			/*
   1300 			 * XXX: give up if the whole (new) hbh header does
   1301 			 * not fit even in an mbuf cluster.
   1302 			 */
   1303 			if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
   1304 				return (ENOBUFS);
   1305 
   1306 			/*
   1307 			 * As a consequence, we must always prepare a cluster
   1308 			 * at this point.
   1309 			 */
   1310 			MGET(n, M_DONTWAIT, MT_DATA);
   1311 			if (n) {
   1312 				MCLGET(n, M_DONTWAIT);
   1313 				if ((n->m_flags & M_EXT) == 0) {
   1314 					m_freem(n);
   1315 					n = NULL;
   1316 				}
   1317 			}
   1318 			if (!n)
   1319 				return (ENOBUFS);
   1320 			n->m_len = oldoptlen + JUMBOOPTLEN;
   1321 			bcopy(mtod(mopt, void *), mtod(n, void *),
   1322 			    oldoptlen);
   1323 			optbuf = mtod(n, u_int8_t *) + oldoptlen;
   1324 			m_freem(mopt);
   1325 			mopt = exthdrs->ip6e_hbh = n;
   1326 		} else {
   1327 			optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
   1328 			mopt->m_len += JUMBOOPTLEN;
   1329 		}
   1330 		optbuf[0] = IP6OPT_PADN;
   1331 		optbuf[1] = 0;
   1332 
   1333 		/*
   1334 		 * Adjust the header length according to the pad and
   1335 		 * the jumbo payload option.
   1336 		 */
   1337 		hbh = mtod(mopt, struct ip6_hbh *);
   1338 		hbh->ip6h_len += (JUMBOOPTLEN >> 3);
   1339 	}
   1340 
   1341 	/* fill in the option. */
   1342 	optbuf[2] = IP6OPT_JUMBO;
   1343 	optbuf[3] = 4;
   1344 	v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
   1345 	bcopy(&v, &optbuf[4], sizeof(u_int32_t));
   1346 
   1347 	/* finally, adjust the packet header length */
   1348 	exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
   1349 
   1350 	return (0);
   1351 #undef JUMBOOPTLEN
   1352 }
   1353 
   1354 /*
   1355  * Insert fragment header and copy unfragmentable header portions.
   1356  */
   1357 static int
   1358 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
   1359 	struct ip6_frag **frghdrp)
   1360 {
   1361 	struct mbuf *n, *mlast;
   1362 
   1363 	if (hlen > sizeof(struct ip6_hdr)) {
   1364 		n = m_copym(m0, sizeof(struct ip6_hdr),
   1365 		    hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
   1366 		if (n == 0)
   1367 			return (ENOBUFS);
   1368 		m->m_next = n;
   1369 	} else
   1370 		n = m;
   1371 
   1372 	/* Search for the last mbuf of unfragmentable part. */
   1373 	for (mlast = n; mlast->m_next; mlast = mlast->m_next)
   1374 		;
   1375 
   1376 	if ((mlast->m_flags & M_EXT) == 0 &&
   1377 	    M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
   1378 		/* use the trailing space of the last mbuf for the fragment hdr */
   1379 		*frghdrp = (struct ip6_frag *)(mtod(mlast, char *) +
   1380 		    mlast->m_len);
   1381 		mlast->m_len += sizeof(struct ip6_frag);
   1382 		m->m_pkthdr.len += sizeof(struct ip6_frag);
   1383 	} else {
   1384 		/* allocate a new mbuf for the fragment header */
   1385 		struct mbuf *mfrg;
   1386 
   1387 		MGET(mfrg, M_DONTWAIT, MT_DATA);
   1388 		if (mfrg == 0)
   1389 			return (ENOBUFS);
   1390 		mfrg->m_len = sizeof(struct ip6_frag);
   1391 		*frghdrp = mtod(mfrg, struct ip6_frag *);
   1392 		mlast->m_next = mfrg;
   1393 	}
   1394 
   1395 	return (0);
   1396 }
   1397 
   1398 static int
   1399 ip6_getpmtu(struct route *ro_pmtu, struct route *ro, struct ifnet *ifp,
   1400     const struct in6_addr *dst, u_long *mtup, int *alwaysfragp)
   1401 {
   1402 	struct rtentry *rt;
   1403 	u_int32_t mtu = 0;
   1404 	int alwaysfrag = 0;
   1405 	int error = 0;
   1406 
   1407 	if (ro_pmtu != ro) {
   1408 		union {
   1409 			struct sockaddr		dst;
   1410 			struct sockaddr_in6	dst6;
   1411 		} u;
   1412 
   1413 		/* The first hop and the final destination may differ. */
   1414 		sockaddr_in6_init(&u.dst6, dst, 0, 0, 0);
   1415 		rtcache_lookup(ro_pmtu, &u.dst);
   1416 	}
   1417 	if ((rt = rtcache_getrt(ro_pmtu)) != NULL) {
   1418 		u_int32_t ifmtu;
   1419 
   1420 		if (ifp == NULL)
   1421 			ifp = rt->rt_ifp;
   1422 		ifmtu = IN6_LINKMTU(ifp);
   1423 		mtu = rt->rt_rmx.rmx_mtu;
   1424 		if (mtu == 0)
   1425 			mtu = ifmtu;
   1426 		else if (mtu < IPV6_MMTU) {
   1427 			/*
   1428 			 * RFC2460 section 5, last paragraph:
   1429 			 * if we record ICMPv6 too big message with
   1430 			 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
   1431 			 * or smaller, with fragment header attached.
   1432 			 * (fragment header is needed regardless from the
   1433 			 * packet size, for translators to identify packets)
   1434 			 */
   1435 			alwaysfrag = 1;
   1436 			mtu = IPV6_MMTU;
   1437 		} else if (mtu > ifmtu) {
   1438 			/*
   1439 			 * The MTU on the route is larger than the MTU on
   1440 			 * the interface!  This shouldn't happen, unless the
   1441 			 * MTU of the interface has been changed after the
   1442 			 * interface was brought up.  Change the MTU in the
   1443 			 * route to match the interface MTU (as long as the
   1444 			 * field isn't locked).
   1445 			 */
   1446 			mtu = ifmtu;
   1447 			if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
   1448 				rt->rt_rmx.rmx_mtu = mtu;
   1449 		}
   1450 	} else if (ifp) {
   1451 		mtu = IN6_LINKMTU(ifp);
   1452 	} else
   1453 		error = EHOSTUNREACH; /* XXX */
   1454 
   1455 	*mtup = mtu;
   1456 	if (alwaysfragp)
   1457 		*alwaysfragp = alwaysfrag;
   1458 	return (error);
   1459 }
   1460 
   1461 /*
   1462  * IP6 socket option processing.
   1463  */
   1464 int
   1465 ip6_ctloutput(int op, struct socket *so, int level, int optname,
   1466     struct mbuf **mp)
   1467 {
   1468 	int privileged, optdatalen, uproto;
   1469 	void *optdata;
   1470 	struct in6pcb *in6p = sotoin6pcb(so);
   1471 	struct mbuf *m = *mp;
   1472 	int error, optval;
   1473 	int optlen;
   1474 	struct lwp *l = curlwp;	/* XXX */
   1475 
   1476 	optlen = m ? m->m_len : 0;
   1477 	error = optval = 0;
   1478 	privileged = (l == 0 || kauth_authorize_generic(l->l_cred,
   1479 	    KAUTH_GENERIC_ISSUSER, NULL)) ? 0 : 1;
   1480 	uproto = (int)so->so_proto->pr_protocol;
   1481 
   1482 	if (level != IPPROTO_IPV6) {
   1483 		if (op == PRCO_SETOPT && *mp)
   1484 			(void)m_free(*mp);
   1485 		return ENOPROTOOPT;
   1486 	}
   1487 	switch (op) {
   1488 	case PRCO_SETOPT:
   1489 		switch (optname) {
   1490 #ifdef RFC2292
   1491 		case IPV6_2292PKTOPTIONS:
   1492 			/* m is freed in ip6_pcbopts */
   1493 			error = ip6_pcbopts(&in6p->in6p_outputopts,
   1494 			    m, so);
   1495 			break;
   1496 #endif
   1497 
   1498 		/*
   1499 		 * Use of some Hop-by-Hop options or some
   1500 		 * Destination options, might require special
   1501 		 * privilege.  That is, normal applications
   1502 		 * (without special privilege) might be forbidden
   1503 		 * from setting certain options in outgoing packets,
   1504 		 * and might never see certain options in received
   1505 		 * packets. [RFC 2292 Section 6]
   1506 		 * KAME specific note:
   1507 		 *  KAME prevents non-privileged users from sending or
   1508 		 *  receiving ANY hbh/dst options in order to avoid
   1509 		 *  overhead of parsing options in the kernel.
   1510 		 */
   1511 		case IPV6_RECVHOPOPTS:
   1512 		case IPV6_RECVDSTOPTS:
   1513 		case IPV6_RECVRTHDRDSTOPTS:
   1514 			if (!privileged) {
   1515 				error = EPERM;
   1516 				break;
   1517 			}
   1518 			/* FALLTHROUGH */
   1519 		case IPV6_UNICAST_HOPS:
   1520 		case IPV6_HOPLIMIT:
   1521 		case IPV6_FAITH:
   1522 
   1523 		case IPV6_RECVPKTINFO:
   1524 		case IPV6_RECVHOPLIMIT:
   1525 		case IPV6_RECVRTHDR:
   1526 		case IPV6_RECVPATHMTU:
   1527 		case IPV6_RECVTCLASS:
   1528 		case IPV6_V6ONLY:
   1529 			if (optlen != sizeof(int)) {
   1530 				error = EINVAL;
   1531 				break;
   1532 			}
   1533 			optval = *mtod(m, int *);
   1534 			switch (optname) {
   1535 
   1536 			case IPV6_UNICAST_HOPS:
   1537 				if (optval < -1 || optval >= 256)
   1538 					error = EINVAL;
   1539 				else {
   1540 					/* -1 = kernel default */
   1541 					in6p->in6p_hops = optval;
   1542 				}
   1543 				break;
   1544 #define OPTSET(bit) \
   1545 do { \
   1546 if (optval) \
   1547 	in6p->in6p_flags |= (bit); \
   1548 else \
   1549 	in6p->in6p_flags &= ~(bit); \
   1550 } while (/*CONSTCOND*/ 0)
   1551 
   1552 #ifdef RFC2292
   1553 #define OPTSET2292(bit) 			\
   1554 do { 						\
   1555 in6p->in6p_flags |= IN6P_RFC2292; 	\
   1556 if (optval) 				\
   1557 	in6p->in6p_flags |= (bit); 	\
   1558 else 					\
   1559 	in6p->in6p_flags &= ~(bit); 	\
   1560 } while (/*CONSTCOND*/ 0)
   1561 #endif
   1562 
   1563 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
   1564 
   1565 			case IPV6_RECVPKTINFO:
   1566 #ifdef RFC2292
   1567 				/* cannot mix with RFC2292 */
   1568 				if (OPTBIT(IN6P_RFC2292)) {
   1569 					error = EINVAL;
   1570 					break;
   1571 				}
   1572 #endif
   1573 				OPTSET(IN6P_PKTINFO);
   1574 				break;
   1575 
   1576 			case IPV6_HOPLIMIT:
   1577 			{
   1578 				struct ip6_pktopts **optp;
   1579 
   1580 #ifdef RFC2292
   1581 				/* cannot mix with RFC2292 */
   1582 				if (OPTBIT(IN6P_RFC2292)) {
   1583 					error = EINVAL;
   1584 					break;
   1585 				}
   1586 #endif
   1587 				optp = &in6p->in6p_outputopts;
   1588 				error = ip6_pcbopt(IPV6_HOPLIMIT,
   1589 						   (u_char *)&optval,
   1590 						   sizeof(optval),
   1591 						   optp,
   1592 						   privileged, uproto);
   1593 				break;
   1594 			}
   1595 
   1596 			case IPV6_RECVHOPLIMIT:
   1597 #ifdef RFC2292
   1598 				/* cannot mix with RFC2292 */
   1599 				if (OPTBIT(IN6P_RFC2292)) {
   1600 					error = EINVAL;
   1601 					break;
   1602 				}
   1603 #endif
   1604 				OPTSET(IN6P_HOPLIMIT);
   1605 				break;
   1606 
   1607 			case IPV6_RECVHOPOPTS:
   1608 #ifdef RFC2292
   1609 				/* cannot mix with RFC2292 */
   1610 				if (OPTBIT(IN6P_RFC2292)) {
   1611 					error = EINVAL;
   1612 					break;
   1613 				}
   1614 #endif
   1615 				OPTSET(IN6P_HOPOPTS);
   1616 				break;
   1617 
   1618 			case IPV6_RECVDSTOPTS:
   1619 #ifdef RFC2292
   1620 				/* cannot mix with RFC2292 */
   1621 				if (OPTBIT(IN6P_RFC2292)) {
   1622 					error = EINVAL;
   1623 					break;
   1624 				}
   1625 #endif
   1626 				OPTSET(IN6P_DSTOPTS);
   1627 				break;
   1628 
   1629 			case IPV6_RECVRTHDRDSTOPTS:
   1630 #ifdef RFC2292
   1631 				/* cannot mix with RFC2292 */
   1632 				if (OPTBIT(IN6P_RFC2292)) {
   1633 					error = EINVAL;
   1634 					break;
   1635 				}
   1636 #endif
   1637 				OPTSET(IN6P_RTHDRDSTOPTS);
   1638 				break;
   1639 
   1640 			case IPV6_RECVRTHDR:
   1641 #ifdef RFC2292
   1642 				/* cannot mix with RFC2292 */
   1643 				if (OPTBIT(IN6P_RFC2292)) {
   1644 					error = EINVAL;
   1645 					break;
   1646 				}
   1647 #endif
   1648 				OPTSET(IN6P_RTHDR);
   1649 				break;
   1650 
   1651 			case IPV6_FAITH:
   1652 				OPTSET(IN6P_FAITH);
   1653 				break;
   1654 
   1655 			case IPV6_RECVPATHMTU:
   1656 				/*
   1657 				 * We ignore this option for TCP
   1658 				 * sockets.
   1659 				 * (RFC3542 leaves this case
   1660 				 * unspecified.)
   1661 				 */
   1662 				if (uproto != IPPROTO_TCP)
   1663 					OPTSET(IN6P_MTU);
   1664 				break;
   1665 
   1666 			case IPV6_V6ONLY:
   1667 				/*
   1668 				 * make setsockopt(IPV6_V6ONLY)
   1669 				 * available only prior to bind(2).
   1670 				 * see ipng mailing list, Jun 22 2001.
   1671 				 */
   1672 				if (in6p->in6p_lport ||
   1673 				    !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
   1674 					error = EINVAL;
   1675 					break;
   1676 				}
   1677 #ifdef INET6_BINDV6ONLY
   1678 				if (!optval)
   1679 					error = EINVAL;
   1680 #else
   1681 				OPTSET(IN6P_IPV6_V6ONLY);
   1682 #endif
   1683 				break;
   1684 			case IPV6_RECVTCLASS:
   1685 #ifdef RFC2292
   1686 				/* cannot mix with RFC2292 XXX */
   1687 				if (OPTBIT(IN6P_RFC2292)) {
   1688 					error = EINVAL;
   1689 					break;
   1690 				}
   1691 #endif
   1692 				OPTSET(IN6P_TCLASS);
   1693 				break;
   1694 
   1695 			}
   1696 			break;
   1697 
   1698 		case IPV6_OTCLASS:
   1699 		{
   1700 			struct ip6_pktopts **optp;
   1701 			u_int8_t tclass;
   1702 
   1703 			if (optlen != sizeof(tclass)) {
   1704 				error = EINVAL;
   1705 				break;
   1706 			}
   1707 			tclass = *mtod(m, u_int8_t *);
   1708 			optp = &in6p->in6p_outputopts;
   1709 			error = ip6_pcbopt(optname,
   1710 					   (u_char *)&tclass,
   1711 					   sizeof(tclass),
   1712 					   optp,
   1713 					   privileged, uproto);
   1714 			break;
   1715 		}
   1716 
   1717 		case IPV6_TCLASS:
   1718 		case IPV6_DONTFRAG:
   1719 		case IPV6_USE_MIN_MTU:
   1720 			if (optlen != sizeof(optval)) {
   1721 				error = EINVAL;
   1722 				break;
   1723 			}
   1724 			optval = *mtod(m, int *);
   1725 			{
   1726 				struct ip6_pktopts **optp;
   1727 				optp = &in6p->in6p_outputopts;
   1728 				error = ip6_pcbopt(optname,
   1729 						   (u_char *)&optval,
   1730 						   sizeof(optval),
   1731 						   optp,
   1732 						   privileged, uproto);
   1733 				break;
   1734 			}
   1735 
   1736 #ifdef RFC2292
   1737 		case IPV6_2292PKTINFO:
   1738 		case IPV6_2292HOPLIMIT:
   1739 		case IPV6_2292HOPOPTS:
   1740 		case IPV6_2292DSTOPTS:
   1741 		case IPV6_2292RTHDR:
   1742 			/* RFC 2292 */
   1743 			if (optlen != sizeof(int)) {
   1744 				error = EINVAL;
   1745 				break;
   1746 			}
   1747 			optval = *mtod(m, int *);
   1748 			switch (optname) {
   1749 			case IPV6_2292PKTINFO:
   1750 				OPTSET2292(IN6P_PKTINFO);
   1751 				break;
   1752 			case IPV6_2292HOPLIMIT:
   1753 				OPTSET2292(IN6P_HOPLIMIT);
   1754 				break;
   1755 			case IPV6_2292HOPOPTS:
   1756 				/*
   1757 				 * Check super-user privilege.
   1758 				 * See comments for IPV6_RECVHOPOPTS.
   1759 				 */
   1760 				if (!privileged)
   1761 					return (EPERM);
   1762 				OPTSET2292(IN6P_HOPOPTS);
   1763 				break;
   1764 			case IPV6_2292DSTOPTS:
   1765 				if (!privileged)
   1766 					return (EPERM);
   1767 				OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
   1768 				break;
   1769 			case IPV6_2292RTHDR:
   1770 				OPTSET2292(IN6P_RTHDR);
   1771 				break;
   1772 			}
   1773 			break;
   1774 #endif
   1775 		case IPV6_PKTINFO:
   1776 		case IPV6_HOPOPTS:
   1777 		case IPV6_RTHDR:
   1778 		case IPV6_DSTOPTS:
   1779 		case IPV6_RTHDRDSTOPTS:
   1780 		case IPV6_NEXTHOP:
   1781 		{
   1782 			/* new advanced API (RFC3542) */
   1783 			u_char *optbuf;
   1784 			int optbuflen;
   1785 			struct ip6_pktopts **optp;
   1786 			if (!m) {
   1787 				error = EINVAL;
   1788 				break;
   1789 			}
   1790 
   1791 #ifdef RFC2292
   1792 			/* cannot mix with RFC2292 */
   1793 			if (OPTBIT(IN6P_RFC2292)) {
   1794 				error = EINVAL;
   1795 				break;
   1796 			}
   1797 #endif
   1798 
   1799 			if (m && m->m_next) {
   1800 				error = EINVAL;	/* XXX */
   1801 				break;
   1802 			}
   1803 
   1804 			optbuf = mtod(m, u_char *);
   1805 			optbuflen = m->m_len;
   1806 			optp = &in6p->in6p_outputopts;
   1807 			error = ip6_pcbopt(optname, optbuf, optbuflen,
   1808 			    optp, privileged, uproto);
   1809 			break;
   1810 		}
   1811 #undef OPTSET
   1812 
   1813 		case IPV6_MULTICAST_IF:
   1814 		case IPV6_MULTICAST_HOPS:
   1815 		case IPV6_MULTICAST_LOOP:
   1816 		case IPV6_JOIN_GROUP:
   1817 		case IPV6_LEAVE_GROUP:
   1818 			error = ip6_setmoptions(optname,
   1819 			    &in6p->in6p_moptions, m);
   1820 			break;
   1821 
   1822 		case IPV6_PORTRANGE:
   1823 			if (!m) {
   1824 				error = EINVAL;
   1825 				break;
   1826 			}
   1827 			optval = *mtod(m, int *);
   1828 
   1829 			switch (optval) {
   1830 			case IPV6_PORTRANGE_DEFAULT:
   1831 				in6p->in6p_flags &= ~(IN6P_LOWPORT);
   1832 				in6p->in6p_flags &= ~(IN6P_HIGHPORT);
   1833 				break;
   1834 
   1835 			case IPV6_PORTRANGE_HIGH:
   1836 				in6p->in6p_flags &= ~(IN6P_LOWPORT);
   1837 				in6p->in6p_flags |= IN6P_HIGHPORT;
   1838 				break;
   1839 
   1840 			case IPV6_PORTRANGE_LOW:
   1841 				in6p->in6p_flags &= ~(IN6P_HIGHPORT);
   1842 				in6p->in6p_flags |= IN6P_LOWPORT;
   1843 				break;
   1844 
   1845 			default:
   1846 				error = EINVAL;
   1847 				break;
   1848 			}
   1849 			break;
   1850 
   1851 
   1852 #if defined(IPSEC) || defined(FAST_IPSEC)
   1853 		case IPV6_IPSEC_POLICY:
   1854 		{
   1855 			void *req = NULL;
   1856 			size_t len = 0;
   1857 			if (m) {
   1858 				req = mtod(m, void *);
   1859 				len = m->m_len;
   1860 			}
   1861 			error = ipsec6_set_policy(in6p, optname, req,
   1862 						  len, privileged);
   1863 		}
   1864 			break;
   1865 #endif /* IPSEC */
   1866 
   1867 		default:
   1868 			error = ENOPROTOOPT;
   1869 			break;
   1870 		}
   1871 		if (m)
   1872 			(void)m_free(m);
   1873 		break;
   1874 
   1875 	case PRCO_GETOPT:
   1876 		switch (optname) {
   1877 #ifdef RFC2292
   1878 		case IPV6_2292PKTOPTIONS:
   1879 			/*
   1880 			 * RFC3542 (effectively) deprecated the
   1881 			 * semantics of the 2292-style pktoptions.
   1882 			 * Since it was not reliable in nature (i.e.,
   1883 			 * applications had to expect the lack of some
   1884 			 * information after all), it would make sense
   1885 			 * to simplify this part by always returning
   1886 			 * empty data.
   1887 			 */
   1888 			*mp = m_get(M_WAIT, MT_SOOPTS);
   1889 			(*mp)->m_len = 0;
   1890 			break;
   1891 #endif
   1892 
   1893 		case IPV6_RECVHOPOPTS:
   1894 		case IPV6_RECVDSTOPTS:
   1895 		case IPV6_RECVRTHDRDSTOPTS:
   1896 		case IPV6_UNICAST_HOPS:
   1897 		case IPV6_RECVPKTINFO:
   1898 		case IPV6_RECVHOPLIMIT:
   1899 		case IPV6_RECVRTHDR:
   1900 		case IPV6_RECVPATHMTU:
   1901 
   1902 		case IPV6_FAITH:
   1903 		case IPV6_V6ONLY:
   1904 		case IPV6_PORTRANGE:
   1905 		case IPV6_RECVTCLASS:
   1906 			switch (optname) {
   1907 
   1908 			case IPV6_RECVHOPOPTS:
   1909 				optval = OPTBIT(IN6P_HOPOPTS);
   1910 				break;
   1911 
   1912 			case IPV6_RECVDSTOPTS:
   1913 				optval = OPTBIT(IN6P_DSTOPTS);
   1914 				break;
   1915 
   1916 			case IPV6_RECVRTHDRDSTOPTS:
   1917 				optval = OPTBIT(IN6P_RTHDRDSTOPTS);
   1918 				break;
   1919 
   1920 			case IPV6_UNICAST_HOPS:
   1921 				optval = in6p->in6p_hops;
   1922 				break;
   1923 
   1924 			case IPV6_RECVPKTINFO:
   1925 				optval = OPTBIT(IN6P_PKTINFO);
   1926 				break;
   1927 
   1928 			case IPV6_RECVHOPLIMIT:
   1929 				optval = OPTBIT(IN6P_HOPLIMIT);
   1930 				break;
   1931 
   1932 			case IPV6_RECVRTHDR:
   1933 				optval = OPTBIT(IN6P_RTHDR);
   1934 				break;
   1935 
   1936 			case IPV6_RECVPATHMTU:
   1937 				optval = OPTBIT(IN6P_MTU);
   1938 				break;
   1939 
   1940 			case IPV6_FAITH:
   1941 				optval = OPTBIT(IN6P_FAITH);
   1942 				break;
   1943 
   1944 			case IPV6_V6ONLY:
   1945 				optval = OPTBIT(IN6P_IPV6_V6ONLY);
   1946 				break;
   1947 
   1948 			case IPV6_PORTRANGE:
   1949 			    {
   1950 				int flags;
   1951 				flags = in6p->in6p_flags;
   1952 				if (flags & IN6P_HIGHPORT)
   1953 					optval = IPV6_PORTRANGE_HIGH;
   1954 				else if (flags & IN6P_LOWPORT)
   1955 					optval = IPV6_PORTRANGE_LOW;
   1956 				else
   1957 					optval = 0;
   1958 				break;
   1959 			    }
   1960 			case IPV6_RECVTCLASS:
   1961 				optval = OPTBIT(IN6P_TCLASS);
   1962 				break;
   1963 
   1964 			}
   1965 			if (error)
   1966 				break;
   1967 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1968 			m->m_len = sizeof(int);
   1969 			*mtod(m, int *) = optval;
   1970 			break;
   1971 
   1972 		case IPV6_PATHMTU:
   1973 		    {
   1974 			u_long pmtu = 0;
   1975 			struct ip6_mtuinfo mtuinfo;
   1976 			struct route *ro = &in6p->in6p_route;
   1977 
   1978 			if (!(so->so_state & SS_ISCONNECTED))
   1979 				return (ENOTCONN);
   1980 			/*
   1981 			 * XXX: we dot not consider the case of source
   1982 			 * routing, or optional information to specify
   1983 			 * the outgoing interface.
   1984 			 */
   1985 			error = ip6_getpmtu(ro, NULL, NULL,
   1986 			    &in6p->in6p_faddr, &pmtu, NULL);
   1987 			if (error)
   1988 				break;
   1989 			if (pmtu > IPV6_MAXPACKET)
   1990 				pmtu = IPV6_MAXPACKET;
   1991 
   1992 			memset(&mtuinfo, 0, sizeof(mtuinfo));
   1993 			mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
   1994 			optdata = (void *)&mtuinfo;
   1995 			optdatalen = sizeof(mtuinfo);
   1996 			if (optdatalen > MCLBYTES)
   1997 				return (EMSGSIZE); /* XXX */
   1998 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1999 			if (optdatalen > MLEN)
   2000 				MCLGET(m, M_WAIT);
   2001 			m->m_len = optdatalen;
   2002 			memcpy(mtod(m, void *), optdata, optdatalen);
   2003 			break;
   2004 		    }
   2005 
   2006 #ifdef RFC2292
   2007 		case IPV6_2292PKTINFO:
   2008 		case IPV6_2292HOPLIMIT:
   2009 		case IPV6_2292HOPOPTS:
   2010 		case IPV6_2292RTHDR:
   2011 		case IPV6_2292DSTOPTS:
   2012 			switch (optname) {
   2013 			case IPV6_2292PKTINFO:
   2014 				optval = OPTBIT(IN6P_PKTINFO);
   2015 				break;
   2016 			case IPV6_2292HOPLIMIT:
   2017 				optval = OPTBIT(IN6P_HOPLIMIT);
   2018 				break;
   2019 			case IPV6_2292HOPOPTS:
   2020 				optval = OPTBIT(IN6P_HOPOPTS);
   2021 				break;
   2022 			case IPV6_2292RTHDR:
   2023 				optval = OPTBIT(IN6P_RTHDR);
   2024 				break;
   2025 			case IPV6_2292DSTOPTS:
   2026 				optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
   2027 				break;
   2028 			}
   2029 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   2030 			m->m_len = sizeof(int);
   2031 			*mtod(m, int *) = optval;
   2032 			break;
   2033 #endif
   2034 		case IPV6_PKTINFO:
   2035 		case IPV6_HOPOPTS:
   2036 		case IPV6_RTHDR:
   2037 		case IPV6_DSTOPTS:
   2038 		case IPV6_RTHDRDSTOPTS:
   2039 		case IPV6_NEXTHOP:
   2040 		case IPV6_OTCLASS:
   2041 		case IPV6_TCLASS:
   2042 		case IPV6_DONTFRAG:
   2043 		case IPV6_USE_MIN_MTU:
   2044 			error = ip6_getpcbopt(in6p->in6p_outputopts,
   2045 			    optname, mp);
   2046 			break;
   2047 
   2048 		case IPV6_MULTICAST_IF:
   2049 		case IPV6_MULTICAST_HOPS:
   2050 		case IPV6_MULTICAST_LOOP:
   2051 		case IPV6_JOIN_GROUP:
   2052 		case IPV6_LEAVE_GROUP:
   2053 			error = ip6_getmoptions(optname,
   2054 			    in6p->in6p_moptions, mp);
   2055 			break;
   2056 
   2057 #if defined(IPSEC) || defined(FAST_IPSEC)
   2058 		case IPV6_IPSEC_POLICY:
   2059 		    {
   2060 			void *req = NULL;
   2061 			size_t len = 0;
   2062 			if (m) {
   2063 				req = mtod(m, void *);
   2064 				len = m->m_len;
   2065 			}
   2066 			error = ipsec6_get_policy(in6p, req, len, mp);
   2067 			break;
   2068 		    }
   2069 #endif /* IPSEC */
   2070 
   2071 
   2072 
   2073 
   2074 		default:
   2075 			error = ENOPROTOOPT;
   2076 			break;
   2077 		}
   2078 		break;
   2079 	}
   2080 	return (error);
   2081 }
   2082 
   2083 int
   2084 ip6_raw_ctloutput(int op, struct socket *so, int level, int optname,
   2085 	struct mbuf **mp)
   2086 {
   2087 	int error = 0, optval, optlen;
   2088 	const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
   2089 	struct in6pcb *in6p = sotoin6pcb(so);
   2090 	struct mbuf *m = *mp;
   2091 
   2092 	optlen = m ? m->m_len : 0;
   2093 
   2094 	if (level != IPPROTO_IPV6) {
   2095 		if (op == PRCO_SETOPT && *mp)
   2096 			(void)m_free(*mp);
   2097 		return ENOPROTOOPT;
   2098 	}
   2099 
   2100 	switch (optname) {
   2101 	case IPV6_CHECKSUM:
   2102 		/*
   2103 		 * For ICMPv6 sockets, no modification allowed for checksum
   2104 		 * offset, permit "no change" values to help existing apps.
   2105 		 *
   2106 		 * XXX RFC3542 says: "An attempt to set IPV6_CHECKSUM
   2107 		 * for an ICMPv6 socket will fail."  The current
   2108 		 * behavior does not meet RFC3542.
   2109 		 */
   2110 		switch (op) {
   2111 		case PRCO_SETOPT:
   2112 			if (optlen != sizeof(int)) {
   2113 				error = EINVAL;
   2114 				break;
   2115 			}
   2116 			optval = *mtod(m, int *);
   2117 			if ((optval % 2) != 0) {
   2118 				/* the API assumes even offset values */
   2119 				error = EINVAL;
   2120 			} else if (so->so_proto->pr_protocol ==
   2121 			    IPPROTO_ICMPV6) {
   2122 				if (optval != icmp6off)
   2123 					error = EINVAL;
   2124 			} else
   2125 				in6p->in6p_cksum = optval;
   2126 			break;
   2127 
   2128 		case PRCO_GETOPT:
   2129 			if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
   2130 				optval = icmp6off;
   2131 			else
   2132 				optval = in6p->in6p_cksum;
   2133 
   2134 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   2135 			m->m_len = sizeof(int);
   2136 			*mtod(m, int *) = optval;
   2137 			break;
   2138 
   2139 		default:
   2140 			error = EINVAL;
   2141 			break;
   2142 		}
   2143 		break;
   2144 
   2145 	default:
   2146 		error = ENOPROTOOPT;
   2147 		break;
   2148 	}
   2149 
   2150 	if (op == PRCO_SETOPT && m)
   2151 		(void)m_free(m);
   2152 
   2153 	return (error);
   2154 }
   2155 
   2156 #ifdef RFC2292
   2157 /*
   2158  * Set up IP6 options in pcb for insertion in output packets or
   2159  * specifying behavior of outgoing packets.
   2160  */
   2161 static int
   2162 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, struct socket *so)
   2163 {
   2164 	struct ip6_pktopts *opt = *pktopt;
   2165 	int error = 0;
   2166 	struct lwp *l = curlwp;	/* XXX */
   2167 	int priv = 0;
   2168 
   2169 	/* turn off any old options. */
   2170 	if (opt) {
   2171 #ifdef DIAGNOSTIC
   2172 	    if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
   2173 		opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
   2174 		opt->ip6po_rhinfo.ip6po_rhi_rthdr)
   2175 		    printf("ip6_pcbopts: all specified options are cleared.\n");
   2176 #endif
   2177 		ip6_clearpktopts(opt, -1);
   2178 	} else
   2179 		opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
   2180 	*pktopt = NULL;
   2181 
   2182 	if (!m || m->m_len == 0) {
   2183 		/*
   2184 		 * Only turning off any previous options, regardless of
   2185 		 * whether the opt is just created or given.
   2186 		 */
   2187 		free(opt, M_IP6OPT);
   2188 		return (0);
   2189 	}
   2190 
   2191 	/*  set options specified by user. */
   2192 	if (l && !kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
   2193 	    NULL))
   2194 		priv = 1;
   2195 	if ((error = ip6_setpktopts(m, opt, NULL, priv,
   2196 	    so->so_proto->pr_protocol)) != 0) {
   2197 		ip6_clearpktopts(opt, -1); /* XXX: discard all options */
   2198 		free(opt, M_IP6OPT);
   2199 		return (error);
   2200 	}
   2201 	*pktopt = opt;
   2202 	return (0);
   2203 }
   2204 #endif
   2205 
   2206 /*
   2207  * initialize ip6_pktopts.  beware that there are non-zero default values in
   2208  * the struct.
   2209  */
   2210 void
   2211 ip6_initpktopts(struct ip6_pktopts *opt)
   2212 {
   2213 
   2214 	memset(opt, 0, sizeof(*opt));
   2215 	opt->ip6po_hlim = -1;	/* -1 means default hop limit */
   2216 	opt->ip6po_tclass = -1;	/* -1 means default traffic class */
   2217 	opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
   2218 }
   2219 
   2220 #define sin6tosa(sin6)	((struct sockaddr *)(sin6)) /* XXX */
   2221 static int
   2222 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
   2223     int priv, int uproto)
   2224 {
   2225 	struct ip6_pktopts *opt;
   2226 
   2227 	if (*pktopt == NULL) {
   2228 		*pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
   2229 		    M_WAITOK);
   2230 		ip6_initpktopts(*pktopt);
   2231 	}
   2232 	opt = *pktopt;
   2233 
   2234 	return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
   2235 }
   2236 
   2237 static int
   2238 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct mbuf **mp)
   2239 {
   2240 	void *optdata = NULL;
   2241 	int optdatalen = 0;
   2242 	struct ip6_ext *ip6e;
   2243 	int error = 0;
   2244 	struct in6_pktinfo null_pktinfo;
   2245 	int deftclass = 0, on;
   2246 	int defminmtu = IP6PO_MINMTU_MCASTONLY;
   2247 	struct mbuf *m;
   2248 
   2249 	switch (optname) {
   2250 	case IPV6_PKTINFO:
   2251 		if (pktopt && pktopt->ip6po_pktinfo)
   2252 			optdata = (void *)pktopt->ip6po_pktinfo;
   2253 		else {
   2254 			/* XXX: we don't have to do this every time... */
   2255 			memset(&null_pktinfo, 0, sizeof(null_pktinfo));
   2256 			optdata = (void *)&null_pktinfo;
   2257 		}
   2258 		optdatalen = sizeof(struct in6_pktinfo);
   2259 		break;
   2260 	case IPV6_OTCLASS:
   2261 		/* XXX */
   2262 		return (EINVAL);
   2263 	case IPV6_TCLASS:
   2264 		if (pktopt && pktopt->ip6po_tclass >= 0)
   2265 			optdata = (void *)&pktopt->ip6po_tclass;
   2266 		else
   2267 			optdata = (void *)&deftclass;
   2268 		optdatalen = sizeof(int);
   2269 		break;
   2270 	case IPV6_HOPOPTS:
   2271 		if (pktopt && pktopt->ip6po_hbh) {
   2272 			optdata = (void *)pktopt->ip6po_hbh;
   2273 			ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
   2274 			optdatalen = (ip6e->ip6e_len + 1) << 3;
   2275 		}
   2276 		break;
   2277 	case IPV6_RTHDR:
   2278 		if (pktopt && pktopt->ip6po_rthdr) {
   2279 			optdata = (void *)pktopt->ip6po_rthdr;
   2280 			ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
   2281 			optdatalen = (ip6e->ip6e_len + 1) << 3;
   2282 		}
   2283 		break;
   2284 	case IPV6_RTHDRDSTOPTS:
   2285 		if (pktopt && pktopt->ip6po_dest1) {
   2286 			optdata = (void *)pktopt->ip6po_dest1;
   2287 			ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
   2288 			optdatalen = (ip6e->ip6e_len + 1) << 3;
   2289 		}
   2290 		break;
   2291 	case IPV6_DSTOPTS:
   2292 		if (pktopt && pktopt->ip6po_dest2) {
   2293 			optdata = (void *)pktopt->ip6po_dest2;
   2294 			ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
   2295 			optdatalen = (ip6e->ip6e_len + 1) << 3;
   2296 		}
   2297 		break;
   2298 	case IPV6_NEXTHOP:
   2299 		if (pktopt && pktopt->ip6po_nexthop) {
   2300 			optdata = (void *)pktopt->ip6po_nexthop;
   2301 			optdatalen = pktopt->ip6po_nexthop->sa_len;
   2302 		}
   2303 		break;
   2304 	case IPV6_USE_MIN_MTU:
   2305 		if (pktopt)
   2306 			optdata = (void *)&pktopt->ip6po_minmtu;
   2307 		else
   2308 			optdata = (void *)&defminmtu;
   2309 		optdatalen = sizeof(int);
   2310 		break;
   2311 	case IPV6_DONTFRAG:
   2312 		if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
   2313 			on = 1;
   2314 		else
   2315 			on = 0;
   2316 		optdata = (void *)&on;
   2317 		optdatalen = sizeof(on);
   2318 		break;
   2319 	default:		/* should not happen */
   2320 #ifdef DIAGNOSTIC
   2321 		panic("ip6_getpcbopt: unexpected option\n");
   2322 #endif
   2323 		return (ENOPROTOOPT);
   2324 	}
   2325 
   2326 	if (optdatalen > MCLBYTES)
   2327 		return (EMSGSIZE); /* XXX */
   2328 	*mp = m = m_get(M_WAIT, MT_SOOPTS);
   2329 	if (optdatalen > MLEN)
   2330 		MCLGET(m, M_WAIT);
   2331 	m->m_len = optdatalen;
   2332 	if (optdatalen)
   2333 		memcpy(mtod(m, void *), optdata, optdatalen);
   2334 
   2335 	return (error);
   2336 }
   2337 
   2338 void
   2339 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
   2340 {
   2341 	if (optname == -1 || optname == IPV6_PKTINFO) {
   2342 		if (pktopt->ip6po_pktinfo)
   2343 			free(pktopt->ip6po_pktinfo, M_IP6OPT);
   2344 		pktopt->ip6po_pktinfo = NULL;
   2345 	}
   2346 	if (optname == -1 || optname == IPV6_HOPLIMIT)
   2347 		pktopt->ip6po_hlim = -1;
   2348 	if (optname == -1 || optname == IPV6_TCLASS)
   2349 		pktopt->ip6po_tclass = -1;
   2350 	if (optname == -1 || optname == IPV6_NEXTHOP) {
   2351 		rtcache_free(&pktopt->ip6po_nextroute);
   2352 		if (pktopt->ip6po_nexthop)
   2353 			free(pktopt->ip6po_nexthop, M_IP6OPT);
   2354 		pktopt->ip6po_nexthop = NULL;
   2355 	}
   2356 	if (optname == -1 || optname == IPV6_HOPOPTS) {
   2357 		if (pktopt->ip6po_hbh)
   2358 			free(pktopt->ip6po_hbh, M_IP6OPT);
   2359 		pktopt->ip6po_hbh = NULL;
   2360 	}
   2361 	if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
   2362 		if (pktopt->ip6po_dest1)
   2363 			free(pktopt->ip6po_dest1, M_IP6OPT);
   2364 		pktopt->ip6po_dest1 = NULL;
   2365 	}
   2366 	if (optname == -1 || optname == IPV6_RTHDR) {
   2367 		if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
   2368 			free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
   2369 		pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
   2370 		rtcache_free(&pktopt->ip6po_route);
   2371 	}
   2372 	if (optname == -1 || optname == IPV6_DSTOPTS) {
   2373 		if (pktopt->ip6po_dest2)
   2374 			free(pktopt->ip6po_dest2, M_IP6OPT);
   2375 		pktopt->ip6po_dest2 = NULL;
   2376 	}
   2377 }
   2378 
   2379 #define PKTOPT_EXTHDRCPY(type) 					\
   2380 do {								\
   2381 	if (src->type) {					\
   2382 		int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
   2383 		dst->type = malloc(hlen, M_IP6OPT, canwait);	\
   2384 		if (dst->type == NULL && canwait == M_NOWAIT)	\
   2385 			goto bad;				\
   2386 		memcpy(dst->type, src->type, hlen);		\
   2387 	}							\
   2388 } while (/*CONSTCOND*/ 0)
   2389 
   2390 static int
   2391 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
   2392 {
   2393 	dst->ip6po_hlim = src->ip6po_hlim;
   2394 	dst->ip6po_tclass = src->ip6po_tclass;
   2395 	dst->ip6po_flags = src->ip6po_flags;
   2396 	if (src->ip6po_pktinfo) {
   2397 		dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
   2398 		    M_IP6OPT, canwait);
   2399 		if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
   2400 			goto bad;
   2401 		*dst->ip6po_pktinfo = *src->ip6po_pktinfo;
   2402 	}
   2403 	if (src->ip6po_nexthop) {
   2404 		dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
   2405 		    M_IP6OPT, canwait);
   2406 		if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
   2407 			goto bad;
   2408 		memcpy(dst->ip6po_nexthop, src->ip6po_nexthop,
   2409 		    src->ip6po_nexthop->sa_len);
   2410 	}
   2411 	PKTOPT_EXTHDRCPY(ip6po_hbh);
   2412 	PKTOPT_EXTHDRCPY(ip6po_dest1);
   2413 	PKTOPT_EXTHDRCPY(ip6po_dest2);
   2414 	PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
   2415 	return (0);
   2416 
   2417   bad:
   2418 	if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
   2419 	if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
   2420 	if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
   2421 	if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
   2422 	if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
   2423 	if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
   2424 
   2425 	return (ENOBUFS);
   2426 }
   2427 #undef PKTOPT_EXTHDRCPY
   2428 
   2429 struct ip6_pktopts *
   2430 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
   2431 {
   2432 	int error;
   2433 	struct ip6_pktopts *dst;
   2434 
   2435 	dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
   2436 	if (dst == NULL && canwait == M_NOWAIT)
   2437 		return (NULL);
   2438 	ip6_initpktopts(dst);
   2439 
   2440 	if ((error = copypktopts(dst, src, canwait)) != 0) {
   2441 		free(dst, M_IP6OPT);
   2442 		return (NULL);
   2443 	}
   2444 
   2445 	return (dst);
   2446 }
   2447 
   2448 void
   2449 ip6_freepcbopts(struct ip6_pktopts *pktopt)
   2450 {
   2451 	if (pktopt == NULL)
   2452 		return;
   2453 
   2454 	ip6_clearpktopts(pktopt, -1);
   2455 
   2456 	free(pktopt, M_IP6OPT);
   2457 }
   2458 
   2459 /*
   2460  * Set the IP6 multicast options in response to user setsockopt().
   2461  */
   2462 static int
   2463 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
   2464 {
   2465 	int error = 0;
   2466 	u_int loop, ifindex;
   2467 	struct ipv6_mreq *mreq;
   2468 	struct ifnet *ifp;
   2469 	struct ip6_moptions *im6o = *im6op;
   2470 	struct route ro;
   2471 	struct in6_multi_mship *imm;
   2472 	struct lwp *l = curlwp;	/* XXX */
   2473 
   2474 	if (im6o == NULL) {
   2475 		/*
   2476 		 * No multicast option buffer attached to the pcb;
   2477 		 * allocate one and initialize to default values.
   2478 		 */
   2479 		im6o = (struct ip6_moptions *)
   2480 			malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
   2481 
   2482 		if (im6o == NULL)
   2483 			return (ENOBUFS);
   2484 		*im6op = im6o;
   2485 		im6o->im6o_multicast_ifp = NULL;
   2486 		im6o->im6o_multicast_hlim = ip6_defmcasthlim;
   2487 		im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
   2488 		LIST_INIT(&im6o->im6o_memberships);
   2489 	}
   2490 
   2491 	switch (optname) {
   2492 
   2493 	case IPV6_MULTICAST_IF:
   2494 		/*
   2495 		 * Select the interface for outgoing multicast packets.
   2496 		 */
   2497 		if (m == NULL || m->m_len != sizeof(u_int)) {
   2498 			error = EINVAL;
   2499 			break;
   2500 		}
   2501 		bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
   2502 		if (ifindex != 0) {
   2503 			if (if_indexlim <= ifindex || !ifindex2ifnet[ifindex]) {
   2504 				error = ENXIO;	/* XXX EINVAL? */
   2505 				break;
   2506 			}
   2507 			ifp = ifindex2ifnet[ifindex];
   2508 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
   2509 				error = EADDRNOTAVAIL;
   2510 				break;
   2511 			}
   2512 		} else
   2513 			ifp = NULL;
   2514 		im6o->im6o_multicast_ifp = ifp;
   2515 		break;
   2516 
   2517 	case IPV6_MULTICAST_HOPS:
   2518 	    {
   2519 		/*
   2520 		 * Set the IP6 hoplimit for outgoing multicast packets.
   2521 		 */
   2522 		int optval;
   2523 		if (m == NULL || m->m_len != sizeof(int)) {
   2524 			error = EINVAL;
   2525 			break;
   2526 		}
   2527 		bcopy(mtod(m, u_int *), &optval, sizeof(optval));
   2528 		if (optval < -1 || optval >= 256)
   2529 			error = EINVAL;
   2530 		else if (optval == -1)
   2531 			im6o->im6o_multicast_hlim = ip6_defmcasthlim;
   2532 		else
   2533 			im6o->im6o_multicast_hlim = optval;
   2534 		break;
   2535 	    }
   2536 
   2537 	case IPV6_MULTICAST_LOOP:
   2538 		/*
   2539 		 * Set the loopback flag for outgoing multicast packets.
   2540 		 * Must be zero or one.
   2541 		 */
   2542 		if (m == NULL || m->m_len != sizeof(u_int)) {
   2543 			error = EINVAL;
   2544 			break;
   2545 		}
   2546 		bcopy(mtod(m, u_int *), &loop, sizeof(loop));
   2547 		if (loop > 1) {
   2548 			error = EINVAL;
   2549 			break;
   2550 		}
   2551 		im6o->im6o_multicast_loop = loop;
   2552 		break;
   2553 
   2554 	case IPV6_JOIN_GROUP:
   2555 		/*
   2556 		 * Add a multicast group membership.
   2557 		 * Group must be a valid IP6 multicast address.
   2558 		 */
   2559 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
   2560 			error = EINVAL;
   2561 			break;
   2562 		}
   2563 		mreq = mtod(m, struct ipv6_mreq *);
   2564 		if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
   2565 			/*
   2566 			 * We use the unspecified address to specify to accept
   2567 			 * all multicast addresses. Only super user is allowed
   2568 			 * to do this.
   2569 			 */
   2570 			if (kauth_authorize_generic(l->l_cred,
   2571 			    KAUTH_GENERIC_ISSUSER, NULL))
   2572 			{
   2573 				error = EACCES;
   2574 				break;
   2575 			}
   2576 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
   2577 			error = EINVAL;
   2578 			break;
   2579 		}
   2580 
   2581 		/*
   2582 		 * If no interface was explicitly specified, choose an
   2583 		 * appropriate one according to the given multicast address.
   2584 		 */
   2585 		if (mreq->ipv6mr_interface == 0) {
   2586 			struct rtentry *rt;
   2587 			union {
   2588 				struct sockaddr		dst;
   2589 				struct sockaddr_in6	dst6;
   2590 			} u;
   2591 
   2592 			/*
   2593 			 * Look up the routing table for the
   2594 			 * address, and choose the outgoing interface.
   2595 			 *   XXX: is it a good approach?
   2596 			 */
   2597 			memset(&ro, 0, sizeof(ro));
   2598 			sockaddr_in6_init(&u.dst6, &mreq->ipv6mr_multiaddr, 0,
   2599 			    0, 0);
   2600 			rtcache_setdst(&ro, &u.dst);
   2601 			rtcache_init(&ro);
   2602 			ifp = (rt = rtcache_getrt(&ro)) != NULL ? rt->rt_ifp
   2603 			                                        : NULL;
   2604 			rtcache_free(&ro);
   2605 		} else {
   2606 			/*
   2607 			 * If the interface is specified, validate it.
   2608 			 */
   2609 			if (if_indexlim <= mreq->ipv6mr_interface ||
   2610 			    !ifindex2ifnet[mreq->ipv6mr_interface]) {
   2611 				error = ENXIO;	/* XXX EINVAL? */
   2612 				break;
   2613 			}
   2614 			ifp = ifindex2ifnet[mreq->ipv6mr_interface];
   2615 		}
   2616 
   2617 		/*
   2618 		 * See if we found an interface, and confirm that it
   2619 		 * supports multicast
   2620 		 */
   2621 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   2622 			error = EADDRNOTAVAIL;
   2623 			break;
   2624 		}
   2625 
   2626 		if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
   2627 			error = EADDRNOTAVAIL; /* XXX: should not happen */
   2628 			break;
   2629 		}
   2630 
   2631 		/*
   2632 		 * See if the membership already exists.
   2633 		 */
   2634 		for (imm = im6o->im6o_memberships.lh_first;
   2635 		     imm != NULL; imm = imm->i6mm_chain.le_next)
   2636 			if (imm->i6mm_maddr->in6m_ifp == ifp &&
   2637 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
   2638 			    &mreq->ipv6mr_multiaddr))
   2639 				break;
   2640 		if (imm != NULL) {
   2641 			error = EADDRINUSE;
   2642 			break;
   2643 		}
   2644 		/*
   2645 		 * Everything looks good; add a new record to the multicast
   2646 		 * address list for the given interface.
   2647 		 */
   2648 		imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
   2649 		if (imm == NULL)
   2650 			break;
   2651 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
   2652 		break;
   2653 
   2654 	case IPV6_LEAVE_GROUP:
   2655 		/*
   2656 		 * Drop a multicast group membership.
   2657 		 * Group must be a valid IP6 multicast address.
   2658 		 */
   2659 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
   2660 			error = EINVAL;
   2661 			break;
   2662 		}
   2663 		mreq = mtod(m, struct ipv6_mreq *);
   2664 
   2665 		/*
   2666 		 * If an interface address was specified, get a pointer
   2667 		 * to its ifnet structure.
   2668 		 */
   2669 		if (mreq->ipv6mr_interface != 0) {
   2670 			if (if_indexlim <= mreq->ipv6mr_interface ||
   2671 			    !ifindex2ifnet[mreq->ipv6mr_interface]) {
   2672 				error = ENXIO;	/* XXX EINVAL? */
   2673 				break;
   2674 			}
   2675 			ifp = ifindex2ifnet[mreq->ipv6mr_interface];
   2676 		} else
   2677 			ifp = NULL;
   2678 
   2679 		/* Fill in the scope zone ID */
   2680 		if (ifp) {
   2681 			if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
   2682 				/* XXX: should not happen */
   2683 				error = EADDRNOTAVAIL;
   2684 				break;
   2685 			}
   2686 		} else if (mreq->ipv6mr_interface != 0) {
   2687 			/*
   2688 			 * XXX: This case would happens when the (positive)
   2689 			 * index is in the valid range, but the corresponding
   2690 			 * interface has been detached dynamically.  The above
   2691 			 * check probably avoids such case to happen here, but
   2692 			 * we check it explicitly for safety.
   2693 			 */
   2694 			error = EADDRNOTAVAIL;
   2695 			break;
   2696 		} else {	/* ipv6mr_interface == 0 */
   2697 			struct sockaddr_in6 sa6_mc;
   2698 
   2699 			/*
   2700 			 * The API spec says as follows:
   2701 			 *  If the interface index is specified as 0, the
   2702 			 *  system may choose a multicast group membership to
   2703 			 *  drop by matching the multicast address only.
   2704 			 * On the other hand, we cannot disambiguate the scope
   2705 			 * zone unless an interface is provided.  Thus, we
   2706 			 * check if there's ambiguity with the default scope
   2707 			 * zone as the last resort.
   2708 			 */
   2709 			sockaddr_in6_init(&sa6_mc, &mreq->ipv6mr_multiaddr,
   2710 			    0, 0, 0);
   2711 			error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
   2712 			if (error != 0)
   2713 				break;
   2714 			mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
   2715 		}
   2716 
   2717 		/*
   2718 		 * Find the membership in the membership list.
   2719 		 */
   2720 		for (imm = im6o->im6o_memberships.lh_first;
   2721 		     imm != NULL; imm = imm->i6mm_chain.le_next) {
   2722 			if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
   2723 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
   2724 			    &mreq->ipv6mr_multiaddr))
   2725 				break;
   2726 		}
   2727 		if (imm == NULL) {
   2728 			/* Unable to resolve interface */
   2729 			error = EADDRNOTAVAIL;
   2730 			break;
   2731 		}
   2732 		/*
   2733 		 * Give up the multicast address record to which the
   2734 		 * membership points.
   2735 		 */
   2736 		LIST_REMOVE(imm, i6mm_chain);
   2737 		in6_leavegroup(imm);
   2738 		break;
   2739 
   2740 	default:
   2741 		error = EOPNOTSUPP;
   2742 		break;
   2743 	}
   2744 
   2745 	/*
   2746 	 * If all options have default values, no need to keep the mbuf.
   2747 	 */
   2748 	if (im6o->im6o_multicast_ifp == NULL &&
   2749 	    im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
   2750 	    im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
   2751 	    im6o->im6o_memberships.lh_first == NULL) {
   2752 		free(*im6op, M_IPMOPTS);
   2753 		*im6op = NULL;
   2754 	}
   2755 
   2756 	return (error);
   2757 }
   2758 
   2759 /*
   2760  * Return the IP6 multicast options in response to user getsockopt().
   2761  */
   2762 static int
   2763 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
   2764 {
   2765 	u_int *hlim, *loop, *ifindex;
   2766 
   2767 	*mp = m_get(M_WAIT, MT_SOOPTS);
   2768 
   2769 	switch (optname) {
   2770 
   2771 	case IPV6_MULTICAST_IF:
   2772 		ifindex = mtod(*mp, u_int *);
   2773 		(*mp)->m_len = sizeof(u_int);
   2774 		if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
   2775 			*ifindex = 0;
   2776 		else
   2777 			*ifindex = im6o->im6o_multicast_ifp->if_index;
   2778 		return (0);
   2779 
   2780 	case IPV6_MULTICAST_HOPS:
   2781 		hlim = mtod(*mp, u_int *);
   2782 		(*mp)->m_len = sizeof(u_int);
   2783 		if (im6o == NULL)
   2784 			*hlim = ip6_defmcasthlim;
   2785 		else
   2786 			*hlim = im6o->im6o_multicast_hlim;
   2787 		return (0);
   2788 
   2789 	case IPV6_MULTICAST_LOOP:
   2790 		loop = mtod(*mp, u_int *);
   2791 		(*mp)->m_len = sizeof(u_int);
   2792 		if (im6o == NULL)
   2793 			*loop = ip6_defmcasthlim;
   2794 		else
   2795 			*loop = im6o->im6o_multicast_loop;
   2796 		return (0);
   2797 
   2798 	default:
   2799 		return (EOPNOTSUPP);
   2800 	}
   2801 }
   2802 
   2803 /*
   2804  * Discard the IP6 multicast options.
   2805  */
   2806 void
   2807 ip6_freemoptions(struct ip6_moptions *im6o)
   2808 {
   2809 	struct in6_multi_mship *imm;
   2810 
   2811 	if (im6o == NULL)
   2812 		return;
   2813 
   2814 	while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
   2815 		LIST_REMOVE(imm, i6mm_chain);
   2816 		in6_leavegroup(imm);
   2817 	}
   2818 	free(im6o, M_IPMOPTS);
   2819 }
   2820 
   2821 /*
   2822  * Set IPv6 outgoing packet options based on advanced API.
   2823  */
   2824 int
   2825 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
   2826 	struct ip6_pktopts *stickyopt, int priv, int uproto)
   2827 {
   2828 	struct cmsghdr *cm = 0;
   2829 
   2830 	if (control == NULL || opt == NULL)
   2831 		return (EINVAL);
   2832 
   2833 	ip6_initpktopts(opt);
   2834 	if (stickyopt) {
   2835 		int error;
   2836 
   2837 		/*
   2838 		 * If stickyopt is provided, make a local copy of the options
   2839 		 * for this particular packet, then override them by ancillary
   2840 		 * objects.
   2841 		 * XXX: copypktopts() does not copy the cached route to a next
   2842 		 * hop (if any).  This is not very good in terms of efficiency,
   2843 		 * but we can allow this since this option should be rarely
   2844 		 * used.
   2845 		 */
   2846 		if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
   2847 			return (error);
   2848 	}
   2849 
   2850 	/*
   2851 	 * XXX: Currently, we assume all the optional information is stored
   2852 	 * in a single mbuf.
   2853 	 */
   2854 	if (control->m_next)
   2855 		return (EINVAL);
   2856 
   2857 	for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
   2858 	    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
   2859 		int error;
   2860 
   2861 		if (control->m_len < CMSG_LEN(0))
   2862 			return (EINVAL);
   2863 
   2864 		cm = mtod(control, struct cmsghdr *);
   2865 		if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
   2866 			return (EINVAL);
   2867 		if (cm->cmsg_level != IPPROTO_IPV6)
   2868 			continue;
   2869 
   2870 		error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
   2871 		    cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
   2872 		if (error)
   2873 			return (error);
   2874 	}
   2875 
   2876 	return (0);
   2877 }
   2878 
   2879 /*
   2880  * Set a particular packet option, as a sticky option or an ancillary data
   2881  * item.  "len" can be 0 only when it's a sticky option.
   2882  * We have 4 cases of combination of "sticky" and "cmsg":
   2883  * "sticky=0, cmsg=0": impossible
   2884  * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
   2885  * "sticky=1, cmsg=0": RFC3542 socket option
   2886  * "sticky=1, cmsg=1": RFC2292 socket option
   2887  */
   2888 static int
   2889 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
   2890     int priv, int sticky, int cmsg, int uproto)
   2891 {
   2892 	int minmtupolicy;
   2893 
   2894 	if (!sticky && !cmsg) {
   2895 #ifdef DIAGNOSTIC
   2896 		printf("ip6_setpktopt: impossible case\n");
   2897 #endif
   2898 		return (EINVAL);
   2899 	}
   2900 
   2901 	/*
   2902 	 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
   2903 	 * not be specified in the context of RFC3542.  Conversely,
   2904 	 * RFC3542 types should not be specified in the context of RFC2292.
   2905 	 */
   2906 	if (!cmsg) {
   2907 		switch (optname) {
   2908 		case IPV6_2292PKTINFO:
   2909 		case IPV6_2292HOPLIMIT:
   2910 		case IPV6_2292NEXTHOP:
   2911 		case IPV6_2292HOPOPTS:
   2912 		case IPV6_2292DSTOPTS:
   2913 		case IPV6_2292RTHDR:
   2914 		case IPV6_2292PKTOPTIONS:
   2915 			return (ENOPROTOOPT);
   2916 		}
   2917 	}
   2918 	if (sticky && cmsg) {
   2919 		switch (optname) {
   2920 		case IPV6_PKTINFO:
   2921 		case IPV6_HOPLIMIT:
   2922 		case IPV6_NEXTHOP:
   2923 		case IPV6_HOPOPTS:
   2924 		case IPV6_DSTOPTS:
   2925 		case IPV6_RTHDRDSTOPTS:
   2926 		case IPV6_RTHDR:
   2927 		case IPV6_USE_MIN_MTU:
   2928 		case IPV6_DONTFRAG:
   2929 		case IPV6_OTCLASS:
   2930 		case IPV6_TCLASS:
   2931 			return (ENOPROTOOPT);
   2932 		}
   2933 	}
   2934 
   2935 	switch (optname) {
   2936 #ifdef RFC2292
   2937 	case IPV6_2292PKTINFO:
   2938 #endif
   2939 	case IPV6_PKTINFO:
   2940 	{
   2941 		struct ifnet *ifp = NULL;
   2942 		struct in6_pktinfo *pktinfo;
   2943 
   2944 		if (len != sizeof(struct in6_pktinfo))
   2945 			return (EINVAL);
   2946 
   2947 		pktinfo = (struct in6_pktinfo *)buf;
   2948 
   2949 		/*
   2950 		 * An application can clear any sticky IPV6_PKTINFO option by
   2951 		 * doing a "regular" setsockopt with ipi6_addr being
   2952 		 * in6addr_any and ipi6_ifindex being zero.
   2953 		 * [RFC 3542, Section 6]
   2954 		 */
   2955 		if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
   2956 		    pktinfo->ipi6_ifindex == 0 &&
   2957 		    IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
   2958 			ip6_clearpktopts(opt, optname);
   2959 			break;
   2960 		}
   2961 
   2962 		if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
   2963 		    sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
   2964 			return (EINVAL);
   2965 		}
   2966 
   2967 		/* validate the interface index if specified. */
   2968 		if (pktinfo->ipi6_ifindex >= if_indexlim) {
   2969 			 return (ENXIO);
   2970 		}
   2971 		if (pktinfo->ipi6_ifindex) {
   2972 			ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
   2973 			if (ifp == NULL)
   2974 				return (ENXIO);
   2975 		}
   2976 
   2977 		/*
   2978 		 * We store the address anyway, and let in6_selectsrc()
   2979 		 * validate the specified address.  This is because ipi6_addr
   2980 		 * may not have enough information about its scope zone, and
   2981 		 * we may need additional information (such as outgoing
   2982 		 * interface or the scope zone of a destination address) to
   2983 		 * disambiguate the scope.
   2984 		 * XXX: the delay of the validation may confuse the
   2985 		 * application when it is used as a sticky option.
   2986 		 */
   2987 		if (opt->ip6po_pktinfo == NULL) {
   2988 			opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
   2989 			    M_IP6OPT, M_NOWAIT);
   2990 			if (opt->ip6po_pktinfo == NULL)
   2991 				return (ENOBUFS);
   2992 		}
   2993 		memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo));
   2994 		break;
   2995 	}
   2996 
   2997 #ifdef RFC2292
   2998 	case IPV6_2292HOPLIMIT:
   2999 #endif
   3000 	case IPV6_HOPLIMIT:
   3001 	{
   3002 		int *hlimp;
   3003 
   3004 		/*
   3005 		 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
   3006 		 * to simplify the ordering among hoplimit options.
   3007 		 */
   3008 		if (optname == IPV6_HOPLIMIT && sticky)
   3009 			return (ENOPROTOOPT);
   3010 
   3011 		if (len != sizeof(int))
   3012 			return (EINVAL);
   3013 		hlimp = (int *)buf;
   3014 		if (*hlimp < -1 || *hlimp > 255)
   3015 			return (EINVAL);
   3016 
   3017 		opt->ip6po_hlim = *hlimp;
   3018 		break;
   3019 	}
   3020 
   3021 	case IPV6_OTCLASS:
   3022 		if (len != sizeof(u_int8_t))
   3023 			return (EINVAL);
   3024 
   3025 		opt->ip6po_tclass = *(u_int8_t *)buf;
   3026 		break;
   3027 
   3028 	case IPV6_TCLASS:
   3029 	{
   3030 		int tclass;
   3031 
   3032 		if (len != sizeof(int))
   3033 			return (EINVAL);
   3034 		tclass = *(int *)buf;
   3035 		if (tclass < -1 || tclass > 255)
   3036 			return (EINVAL);
   3037 
   3038 		opt->ip6po_tclass = tclass;
   3039 		break;
   3040 	}
   3041 
   3042 #ifdef RFC2292
   3043 	case IPV6_2292NEXTHOP:
   3044 #endif
   3045 	case IPV6_NEXTHOP:
   3046 		if (!priv)
   3047 			return (EPERM);
   3048 
   3049 		if (len == 0) {	/* just remove the option */
   3050 			ip6_clearpktopts(opt, IPV6_NEXTHOP);
   3051 			break;
   3052 		}
   3053 
   3054 		/* check if cmsg_len is large enough for sa_len */
   3055 		if (len < sizeof(struct sockaddr) || len < *buf)
   3056 			return (EINVAL);
   3057 
   3058 		switch (((struct sockaddr *)buf)->sa_family) {
   3059 		case AF_INET6:
   3060 		{
   3061 			struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
   3062 			int error;
   3063 
   3064 			if (sa6->sin6_len != sizeof(struct sockaddr_in6))
   3065 				return (EINVAL);
   3066 
   3067 			if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
   3068 			    IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
   3069 				return (EINVAL);
   3070 			}
   3071 			if ((error = sa6_embedscope(sa6, ip6_use_defzone))
   3072 			    != 0) {
   3073 				return (error);
   3074 			}
   3075 			break;
   3076 		}
   3077 		case AF_LINK:	/* eventually be supported? */
   3078 		default:
   3079 			return (EAFNOSUPPORT);
   3080 		}
   3081 
   3082 		/* turn off the previous option, then set the new option. */
   3083 		ip6_clearpktopts(opt, IPV6_NEXTHOP);
   3084 		opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
   3085 		if (opt->ip6po_nexthop == NULL)
   3086 			return (ENOBUFS);
   3087 		memcpy(opt->ip6po_nexthop, buf, *buf);
   3088 		break;
   3089 
   3090 #ifdef RFC2292
   3091 	case IPV6_2292HOPOPTS:
   3092 #endif
   3093 	case IPV6_HOPOPTS:
   3094 	{
   3095 		struct ip6_hbh *hbh;
   3096 		int hbhlen;
   3097 
   3098 		/*
   3099 		 * XXX: We don't allow a non-privileged user to set ANY HbH
   3100 		 * options, since per-option restriction has too much
   3101 		 * overhead.
   3102 		 */
   3103 		if (!priv)
   3104 			return (EPERM);
   3105 
   3106 		if (len == 0) {
   3107 			ip6_clearpktopts(opt, IPV6_HOPOPTS);
   3108 			break;	/* just remove the option */
   3109 		}
   3110 
   3111 		/* message length validation */
   3112 		if (len < sizeof(struct ip6_hbh))
   3113 			return (EINVAL);
   3114 		hbh = (struct ip6_hbh *)buf;
   3115 		hbhlen = (hbh->ip6h_len + 1) << 3;
   3116 		if (len != hbhlen)
   3117 			return (EINVAL);
   3118 
   3119 		/* turn off the previous option, then set the new option. */
   3120 		ip6_clearpktopts(opt, IPV6_HOPOPTS);
   3121 		opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
   3122 		if (opt->ip6po_hbh == NULL)
   3123 			return (ENOBUFS);
   3124 		memcpy(opt->ip6po_hbh, hbh, hbhlen);
   3125 
   3126 		break;
   3127 	}
   3128 
   3129 #ifdef RFC2292
   3130 	case IPV6_2292DSTOPTS:
   3131 #endif
   3132 	case IPV6_DSTOPTS:
   3133 	case IPV6_RTHDRDSTOPTS:
   3134 	{
   3135 		struct ip6_dest *dest, **newdest = NULL;
   3136 		int destlen;
   3137 
   3138 		if (!priv)	/* XXX: see the comment for IPV6_HOPOPTS */
   3139 			return (EPERM);
   3140 
   3141 		if (len == 0) {
   3142 			ip6_clearpktopts(opt, optname);
   3143 			break;	/* just remove the option */
   3144 		}
   3145 
   3146 		/* message length validation */
   3147 		if (len < sizeof(struct ip6_dest))
   3148 			return (EINVAL);
   3149 		dest = (struct ip6_dest *)buf;
   3150 		destlen = (dest->ip6d_len + 1) << 3;
   3151 		if (len != destlen)
   3152 			return (EINVAL);
   3153 		/*
   3154 		 * Determine the position that the destination options header
   3155 		 * should be inserted; before or after the routing header.
   3156 		 */
   3157 		switch (optname) {
   3158 		case IPV6_2292DSTOPTS:
   3159 			/*
   3160 			 * The old advanced API is ambiguous on this point.
   3161 			 * Our approach is to determine the position based
   3162 			 * according to the existence of a routing header.
   3163 			 * Note, however, that this depends on the order of the
   3164 			 * extension headers in the ancillary data; the 1st
   3165 			 * part of the destination options header must appear
   3166 			 * before the routing header in the ancillary data,
   3167 			 * too.
   3168 			 * RFC3542 solved the ambiguity by introducing
   3169 			 * separate ancillary data or option types.
   3170 			 */
   3171 			if (opt->ip6po_rthdr == NULL)
   3172 				newdest = &opt->ip6po_dest1;
   3173 			else
   3174 				newdest = &opt->ip6po_dest2;
   3175 			break;
   3176 		case IPV6_RTHDRDSTOPTS:
   3177 			newdest = &opt->ip6po_dest1;
   3178 			break;
   3179 		case IPV6_DSTOPTS:
   3180 			newdest = &opt->ip6po_dest2;
   3181 			break;
   3182 		}
   3183 
   3184 		/* turn off the previous option, then set the new option. */
   3185 		ip6_clearpktopts(opt, optname);
   3186 		*newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
   3187 		if (*newdest == NULL)
   3188 			return (ENOBUFS);
   3189 		memcpy(*newdest, dest, destlen);
   3190 
   3191 		break;
   3192 	}
   3193 
   3194 #ifdef RFC2292
   3195 	case IPV6_2292RTHDR:
   3196 #endif
   3197 	case IPV6_RTHDR:
   3198 	{
   3199 		struct ip6_rthdr *rth;
   3200 		int rthlen;
   3201 
   3202 		if (len == 0) {
   3203 			ip6_clearpktopts(opt, IPV6_RTHDR);
   3204 			break;	/* just remove the option */
   3205 		}
   3206 
   3207 		/* message length validation */
   3208 		if (len < sizeof(struct ip6_rthdr))
   3209 			return (EINVAL);
   3210 		rth = (struct ip6_rthdr *)buf;
   3211 		rthlen = (rth->ip6r_len + 1) << 3;
   3212 		if (len != rthlen)
   3213 			return (EINVAL);
   3214 		switch (rth->ip6r_type) {
   3215 		case IPV6_RTHDR_TYPE_0:
   3216 			if (rth->ip6r_len == 0)	/* must contain one addr */
   3217 				return (EINVAL);
   3218 			if (rth->ip6r_len % 2) /* length must be even */
   3219 				return (EINVAL);
   3220 			if (rth->ip6r_len / 2 != rth->ip6r_segleft)
   3221 				return (EINVAL);
   3222 			break;
   3223 		default:
   3224 			return (EINVAL);	/* not supported */
   3225 		}
   3226 		/* turn off the previous option */
   3227 		ip6_clearpktopts(opt, IPV6_RTHDR);
   3228 		opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
   3229 		if (opt->ip6po_rthdr == NULL)
   3230 			return (ENOBUFS);
   3231 		memcpy(opt->ip6po_rthdr, rth, rthlen);
   3232 		break;
   3233 	}
   3234 
   3235 	case IPV6_USE_MIN_MTU:
   3236 		if (len != sizeof(int))
   3237 			return (EINVAL);
   3238 		minmtupolicy = *(int *)buf;
   3239 		if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
   3240 		    minmtupolicy != IP6PO_MINMTU_DISABLE &&
   3241 		    minmtupolicy != IP6PO_MINMTU_ALL) {
   3242 			return (EINVAL);
   3243 		}
   3244 		opt->ip6po_minmtu = minmtupolicy;
   3245 		break;
   3246 
   3247 	case IPV6_DONTFRAG:
   3248 		if (len != sizeof(int))
   3249 			return (EINVAL);
   3250 
   3251 		if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
   3252 			/*
   3253 			 * we ignore this option for TCP sockets.
   3254 			 * (RFC3542 leaves this case unspecified.)
   3255 			 */
   3256 			opt->ip6po_flags &= ~IP6PO_DONTFRAG;
   3257 		} else
   3258 			opt->ip6po_flags |= IP6PO_DONTFRAG;
   3259 		break;
   3260 
   3261 	default:
   3262 		return (ENOPROTOOPT);
   3263 	} /* end of switch */
   3264 
   3265 	return (0);
   3266 }
   3267 
   3268 /*
   3269  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
   3270  * packet to the input queue of a specified interface.  Note that this
   3271  * calls the output routine of the loopback "driver", but with an interface
   3272  * pointer that might NOT be lo0ifp -- easier than replicating that code here.
   3273  */
   3274 void
   3275 ip6_mloopback(struct ifnet *ifp, struct mbuf *m,
   3276 	const struct sockaddr_in6 *dst)
   3277 {
   3278 	struct mbuf *copym;
   3279 	struct ip6_hdr *ip6;
   3280 
   3281 	copym = m_copy(m, 0, M_COPYALL);
   3282 	if (copym == NULL)
   3283 		return;
   3284 
   3285 	/*
   3286 	 * Make sure to deep-copy IPv6 header portion in case the data
   3287 	 * is in an mbuf cluster, so that we can safely override the IPv6
   3288 	 * header portion later.
   3289 	 */
   3290 	if ((copym->m_flags & M_EXT) != 0 ||
   3291 	    copym->m_len < sizeof(struct ip6_hdr)) {
   3292 		copym = m_pullup(copym, sizeof(struct ip6_hdr));
   3293 		if (copym == NULL)
   3294 			return;
   3295 	}
   3296 
   3297 #ifdef DIAGNOSTIC
   3298 	if (copym->m_len < sizeof(*ip6)) {
   3299 		m_freem(copym);
   3300 		return;
   3301 	}
   3302 #endif
   3303 
   3304 	ip6 = mtod(copym, struct ip6_hdr *);
   3305 	/*
   3306 	 * clear embedded scope identifiers if necessary.
   3307 	 * in6_clearscope will touch the addresses only when necessary.
   3308 	 */
   3309 	in6_clearscope(&ip6->ip6_src);
   3310 	in6_clearscope(&ip6->ip6_dst);
   3311 
   3312 	(void)looutput(ifp, copym, (const struct sockaddr *)dst, NULL);
   3313 }
   3314 
   3315 /*
   3316  * Chop IPv6 header off from the payload.
   3317  */
   3318 static int
   3319 ip6_splithdr(struct mbuf *m,  struct ip6_exthdrs *exthdrs)
   3320 {
   3321 	struct mbuf *mh;
   3322 	struct ip6_hdr *ip6;
   3323 
   3324 	ip6 = mtod(m, struct ip6_hdr *);
   3325 	if (m->m_len > sizeof(*ip6)) {
   3326 		MGETHDR(mh, M_DONTWAIT, MT_HEADER);
   3327 		if (mh == 0) {
   3328 			m_freem(m);
   3329 			return ENOBUFS;
   3330 		}
   3331 		M_MOVE_PKTHDR(mh, m);
   3332 		MH_ALIGN(mh, sizeof(*ip6));
   3333 		m->m_len -= sizeof(*ip6);
   3334 		m->m_data += sizeof(*ip6);
   3335 		mh->m_next = m;
   3336 		m = mh;
   3337 		m->m_len = sizeof(*ip6);
   3338 		bcopy((void *)ip6, mtod(m, void *), sizeof(*ip6));
   3339 	}
   3340 	exthdrs->ip6e_ip6 = m;
   3341 	return 0;
   3342 }
   3343 
   3344 /*
   3345  * Compute IPv6 extension header length.
   3346  */
   3347 int
   3348 ip6_optlen(struct in6pcb *in6p)
   3349 {
   3350 	int len;
   3351 
   3352 	if (!in6p->in6p_outputopts)
   3353 		return 0;
   3354 
   3355 	len = 0;
   3356 #define elen(x) \
   3357     (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
   3358 
   3359 	len += elen(in6p->in6p_outputopts->ip6po_hbh);
   3360 	len += elen(in6p->in6p_outputopts->ip6po_dest1);
   3361 	len += elen(in6p->in6p_outputopts->ip6po_rthdr);
   3362 	len += elen(in6p->in6p_outputopts->ip6po_dest2);
   3363 	return len;
   3364 #undef elen
   3365 }
   3366