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