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