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