xref: /xnu-8020.121.3/bsd/netinet6/ip6_output.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941) !
1 /*
2  * Copyright (c) 2000-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*
30  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. Neither the name of the project nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 /*
59  * Copyright (c) 1982, 1986, 1988, 1990, 1993
60  *	The Regents of the University of California.  All rights reserved.
61  *
62  * Redistribution and use in source and binary forms, with or without
63  * modification, are permitted provided that the following conditions
64  * are met:
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in the
69  *    documentation and/or other materials provided with the distribution.
70  * 3. All advertising materials mentioning features or use of this software
71  *    must display the following acknowledgement:
72  *	This product includes software developed by the University of
73  *	California, Berkeley and its contributors.
74  * 4. Neither the name of the University nor the names of its contributors
75  *    may be used to endorse or promote products derived from this software
76  *    without specific prior written permission.
77  *
78  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88  * SUCH DAMAGE.
89  *
90  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
91  */
92 /*
93  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
94  * support for mandatory and extensible security protections.  This notice
95  * is included in support of clause 2.2 (b) of the Apple Public License,
96  * Version 2.0.
97  */
98 
99 #include <sys/param.h>
100 #include <sys/malloc.h>
101 #include <sys/mbuf.h>
102 #include <sys/errno.h>
103 #include <sys/protosw.h>
104 #include <sys/socket.h>
105 #include <sys/socketvar.h>
106 #include <sys/systm.h>
107 #include <sys/kernel.h>
108 #include <sys/proc.h>
109 #include <sys/kauth.h>
110 #include <sys/mcache.h>
111 #include <sys/sysctl.h>
112 #include <kern/zalloc.h>
113 #include <libkern/OSByteOrder.h>
114 
115 #include <pexpert/pexpert.h>
116 #include <mach/sdt.h>
117 
118 #include <net/if.h>
119 #include <net/route.h>
120 #include <net/dlil.h>
121 #include <net/net_api_stats.h>
122 #include <net/net_osdep.h>
123 #include <net/net_perf.h>
124 
125 #include <netinet/ip.h>
126 #include <netinet/in.h>
127 #include <netinet/in_var.h>
128 #include <netinet/ip_var.h>
129 #include <netinet6/in6_var.h>
130 #include <netinet/ip6.h>
131 #include <netinet/kpi_ipfilter_var.h>
132 #include <netinet/in_tclass.h>
133 
134 #include <netinet6/ip6protosw.h>
135 #include <netinet/icmp6.h>
136 #include <netinet6/ip6_var.h>
137 #include <netinet/in_pcb.h>
138 #include <netinet6/nd6.h>
139 #include <netinet6/scope6_var.h>
140 #if IPSEC
141 #include <netinet6/ipsec.h>
142 #include <netinet6/ipsec6.h>
143 #include <netkey/key.h>
144 extern int ipsec_bypass;
145 #endif /* IPSEC */
146 
147 #if NECP
148 #include <net/necp.h>
149 #endif /* NECP */
150 
151 #if DUMMYNET
152 #include <netinet/ip_dummynet.h>
153 #endif /* DUMMYNET */
154 
155 #if PF
156 #include <net/pfvar.h>
157 #endif /* PF */
158 
159 static int sysctl_reset_ip6_output_stats SYSCTL_HANDLER_ARGS;
160 static int sysctl_ip6_output_measure_bins SYSCTL_HANDLER_ARGS;
161 static int sysctl_ip6_output_getperf SYSCTL_HANDLER_ARGS;
162 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
163 static void ip6_out_cksum_stats(int, u_int32_t);
164 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
165 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
166     struct ip6_frag **);
167 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
168     struct ifnet *, struct in6_addr *, uint32_t, u_int32_t *);
169 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *, struct socket *,
170     struct sockopt *sopt);
171 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **, int);
172 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
173 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, zalloc_flags_t);
174 static void im6o_trace(struct ip6_moptions *, int);
175 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *, int,
176     int, int);
177 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
178 static void ip6_output_checksum(struct ifnet *, uint32_t, struct mbuf *,
179     int, uint32_t, uint32_t);
180 extern int udp_ctloutput(struct socket *, struct sockopt *);
181 static int ip6_fragment_packet(struct mbuf **m,
182     struct ip6_pktopts *opt, struct ip6_out_args * ip6oa,
183     struct ip6_exthdrs *exthdrsp, struct ifnet *ifp,
184     uint32_t mtu, uint32_t unfragpartlen, struct route_in6 *ro_pmtu,
185     int nxt0, uint32_t optlen);
186 
187 SYSCTL_DECL(_net_inet6_ip6);
188 
189 static int ip6_output_measure = 0;
190 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, output_perf,
191     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
192     &ip6_output_measure, 0, sysctl_reset_ip6_output_stats, "I", "Do time measurement");
193 
194 static uint64_t ip6_output_measure_bins = 0;
195 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, output_perf_bins,
196     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_output_measure_bins, 0,
197     sysctl_ip6_output_measure_bins, "I",
198     "bins for chaining performance data histogram");
199 
200 static net_perf_t net_perf;
201 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, output_perf_data,
202     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
203     0, 0, sysctl_ip6_output_getperf, "S,net_perf",
204     "IP6 output performance data (struct net_perf, net/net_perf.h)");
205 
206 #define IM6O_TRACE_HIST_SIZE    32      /* size of trace history */
207 
208 /* For gdb */
209 __private_extern__ unsigned int im6o_trace_hist_size = IM6O_TRACE_HIST_SIZE;
210 
211 struct ip6_moptions_dbg {
212 	struct ip6_moptions     im6o;                   /* ip6_moptions */
213 	u_int16_t               im6o_refhold_cnt;       /* # of IM6O_ADDREF */
214 	u_int16_t               im6o_refrele_cnt;       /* # of IM6O_REMREF */
215 	/*
216 	 * Alloc and free callers.
217 	 */
218 	ctrace_t                im6o_alloc;
219 	ctrace_t                im6o_free;
220 	/*
221 	 * Circular lists of IM6O_ADDREF and IM6O_REMREF callers.
222 	 */
223 	ctrace_t                im6o_refhold[IM6O_TRACE_HIST_SIZE];
224 	ctrace_t                im6o_refrele[IM6O_TRACE_HIST_SIZE];
225 };
226 
227 #if DEBUG
228 static unsigned int im6o_debug = 1;     /* debugging (enabled) */
229 #else
230 static unsigned int im6o_debug;         /* debugging (disabled) */
231 #endif /* !DEBUG */
232 
233 static struct zone *im6o_zone;          /* zone for ip6_moptions */
234 #define IM6O_ZONE_NAME          "ip6_moptions"  /* zone name */
235 
236 /*
237  * ip6_output() calls ip6_output_list() to do the work
238  */
239 int
ip6_output(struct mbuf * m0,struct ip6_pktopts * opt,struct route_in6 * ro,int flags,struct ip6_moptions * im6o,struct ifnet ** ifpp,struct ip6_out_args * ip6oa)240 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
241     struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
242     struct ifnet **ifpp, struct ip6_out_args *ip6oa)
243 {
244 	return ip6_output_list(m0, 0, opt, ro, flags, im6o, ifpp, ip6oa);
245 }
246 
247 /*
248  * IP6 output. Each packet in mbuf chain m contains a skeletal IP6
249  * header (with pri, len, nxt, hlim, src, dst).
250  * This function may modify ver and hlim only.
251  * The mbuf chain containing the packet will be freed.
252  * The mbuf opt, if present, will not be freed.
253  *
254  * If ro is non-NULL and has valid ro->ro_rt, route lookup would be
255  * skipped and ro->ro_rt would be used.  Otherwise the result of route
256  * lookup is stored in ro->ro_rt.
257  *
258  * type of "mtu": rt_rmx.rmx_mtu is u_int32_t, ifnet.ifr_mtu is int, and
259  * nd_ifinfo.linkmtu is u_int32_t.  so we use u_int32_t to hold largest one,
260  * which is rt_rmx.rmx_mtu.
261  */
262 int
ip6_output_list(struct mbuf * m0,int packetchain,struct ip6_pktopts * opt,struct route_in6 * ro,int flags,struct ip6_moptions * im6o,struct ifnet ** ifpp,struct ip6_out_args * ip6oa)263 ip6_output_list(struct mbuf *m0, int packetchain, struct ip6_pktopts *opt,
264     struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
265     struct ifnet **ifpp, struct ip6_out_args *ip6oa)
266 {
267 	struct ip6_hdr *ip6;
268 	u_char *nexthdrp;
269 	struct ifnet *ifp = NULL, *origifp = NULL;      /* refcnt'd */
270 	struct ifnet **ifpp_save = ifpp;
271 	struct mbuf *m, *mprev;
272 	struct mbuf *sendchain = NULL, *sendchain_last = NULL;
273 	struct mbuf *inputchain = NULL;
274 	int nxt0 = 0;
275 	struct route_in6 *ro_pmtu = NULL;
276 	struct rtentry *rt = NULL;
277 	struct sockaddr_in6 *dst = NULL, src_sa, dst_sa;
278 	int error = 0;
279 	struct in6_ifaddr *ia = NULL, *src_ia = NULL;
280 	u_int32_t mtu = 0;
281 	u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
282 	struct ip6_rthdr *rh;
283 	struct in6_addr finaldst;
284 	ipfilter_t inject_filter_ref;
285 	struct ipf_pktopts *ippo = NULL;
286 	struct flowadv *adv = NULL;
287 	uint32_t pktcnt = 0;
288 	uint32_t packets_processed = 0;
289 	struct timeval start_tv;
290 #if PF
291 	boolean_t skip_pf = (ip6oa != NULL) &&
292 	    (ip6oa->ip6oa_flags & IP6OAF_SKIP_PF);
293 #endif
294 
295 #if DUMMYNET
296 	struct m_tag *tag;
297 	struct ip6_out_args saved_ip6oa;
298 	struct sockaddr_in6 dst_buf;
299 #endif /* DUMMYNET */
300 #if IPSEC
301 	struct socket *so = NULL;
302 	struct secpolicy *sp = NULL;
303 	struct route_in6 *ipsec_saved_route = NULL;
304 	boolean_t needipsectun = FALSE;
305 #endif /* IPSEC */
306 #if NECP
307 	necp_kernel_policy_result necp_result = 0;
308 	necp_kernel_policy_result_parameter necp_result_parameter;
309 	necp_kernel_policy_id necp_matched_policy_id = 0;
310 #endif /* NECP */
311 	struct {
312 		struct ipf_pktopts ipf_pktopts;
313 		struct ip6_exthdrs exthdrs;
314 		struct route_in6 ip6route;
315 #if IPSEC
316 		struct ipsec_output_state ipsec_state;
317 #endif /* IPSEC */
318 #if NECP
319 		struct route_in6 necp_route;
320 #endif /* NECP */
321 #if DUMMYNET
322 		struct route_in6 saved_route;
323 		struct route_in6 saved_ro_pmtu;
324 		struct ip_fw_args args;
325 #endif /* DUMMYNET */
326 	} ip6obz;
327 #define ipf_pktopts     ip6obz.ipf_pktopts
328 #define exthdrs         ip6obz.exthdrs
329 #define ip6route        ip6obz.ip6route
330 #define ipsec_state     ip6obz.ipsec_state
331 #define necp_route      ip6obz.necp_route
332 #define saved_route     ip6obz.saved_route
333 #define saved_ro_pmtu   ip6obz.saved_ro_pmtu
334 #define args            ip6obz.args
335 	union {
336 		struct {
337 			boolean_t select_srcif : 1;
338 			boolean_t hdrsplit : 1;
339 			boolean_t route_selected : 1;
340 			boolean_t dontfrag : 1;
341 #if IPSEC
342 			boolean_t needipsec : 1;
343 			boolean_t noipsec : 1;
344 #endif /* IPSEC */
345 		};
346 		uint32_t raw;
347 	} ip6obf = { .raw = 0 };
348 
349 	if (ip6_output_measure) {
350 		net_perf_start_time(&net_perf, &start_tv);
351 	}
352 
353 	VERIFY(m0->m_flags & M_PKTHDR);
354 
355 	/* zero out {saved_route, saved_ro_pmtu, ip6route, exthdrs, args} */
356 	bzero(&ip6obz, sizeof(ip6obz));
357 
358 #if DUMMYNET
359 	if (SLIST_EMPTY(&m0->m_pkthdr.tags)) {
360 		goto tags_done;
361 	}
362 
363 	/* Grab info from mtags prepended to the chain */
364 	if ((tag = m_tag_locate(m0, KERNEL_MODULE_TAG_ID,
365 	    KERNEL_TAG_TYPE_DUMMYNET, NULL)) != NULL) {
366 		struct dn_pkt_tag       *dn_tag;
367 
368 		/*
369 		 * ip6_output_list() cannot handle chains of packets reinjected
370 		 * by dummynet. The same restriction applies to
371 		 * ip_output_list().
372 		 */
373 		VERIFY(0 == packetchain);
374 
375 		dn_tag = (struct dn_pkt_tag *)(tag + 1);
376 		args.fwa_pf_rule = dn_tag->dn_pf_rule;
377 
378 		bcopy(&dn_tag->dn_dst6, &dst_buf, sizeof(dst_buf));
379 		dst = &dst_buf;
380 		ifp = dn_tag->dn_ifp;
381 		if (ifp != NULL) {
382 			ifnet_reference(ifp);
383 		}
384 		flags = dn_tag->dn_flags;
385 		if (dn_tag->dn_flags & IPV6_OUTARGS) {
386 			saved_ip6oa = dn_tag->dn_ip6oa;
387 			ip6oa = &saved_ip6oa;
388 		}
389 
390 		saved_route = dn_tag->dn_ro6;
391 		ro = &saved_route;
392 		saved_ro_pmtu = dn_tag->dn_ro6_pmtu;
393 		ro_pmtu = &saved_ro_pmtu;
394 		origifp = dn_tag->dn_origifp;
395 		if (origifp != NULL) {
396 			ifnet_reference(origifp);
397 		}
398 		mtu = dn_tag->dn_mtu;
399 		unfragpartlen = dn_tag->dn_unfragpartlen;
400 
401 		bcopy(&dn_tag->dn_exthdrs, &exthdrs, sizeof(exthdrs));
402 
403 		m_tag_delete(m0, tag);
404 	}
405 
406 tags_done:
407 #endif /* DUMMYNET */
408 
409 	m = m0;
410 
411 #if IPSEC
412 	if (ipsec_bypass == 0) {
413 		so = ipsec_getsocket(m);
414 		if (so != NULL) {
415 			(void) ipsec_setsocket(m, NULL);
416 		}
417 		/* If packet is bound to an interface, check bound policies */
418 		if ((flags & IPV6_OUTARGS) &&
419 		    (ip6oa->ip6oa_flags & IP6OAF_BOUND_IF) &&
420 		    ip6oa->ip6oa_boundif != IFSCOPE_NONE) {
421 			/* ip6obf.noipsec is a bitfield, use temp integer */
422 			int noipsec = 0;
423 
424 			if (ipsec6_getpolicybyinterface(m, IPSEC_DIR_OUTBOUND,
425 			    flags, ip6oa, &noipsec, &sp) != 0) {
426 				goto bad;
427 			}
428 
429 			ip6obf.noipsec = (noipsec != 0);
430 		}
431 	}
432 #endif /* IPSEC */
433 
434 	ippo = &ipf_pktopts;
435 
436 	if (flags & IPV6_OUTARGS) {
437 		/*
438 		 * In the forwarding case, only the ifscope value is used,
439 		 * as source interface selection doesn't take place.
440 		 */
441 		if ((ip6obf.select_srcif = (!(flags & (IPV6_FORWARDING |
442 		    IPV6_UNSPECSRC | IPV6_FLAG_NOSRCIFSEL)) &&
443 		    (ip6oa->ip6oa_flags & IP6OAF_SELECT_SRCIF)))) {
444 			ipf_pktopts.ippo_flags |= IPPOF_SELECT_SRCIF;
445 		}
446 
447 		if ((ip6oa->ip6oa_flags & IP6OAF_BOUND_IF) &&
448 		    ip6oa->ip6oa_boundif != IFSCOPE_NONE) {
449 			ipf_pktopts.ippo_flags |= (IPPOF_BOUND_IF |
450 			    (ip6oa->ip6oa_boundif << IPPOF_SHIFT_IFSCOPE));
451 		}
452 
453 		if (ip6oa->ip6oa_flags & IP6OAF_BOUND_SRCADDR) {
454 			ipf_pktopts.ippo_flags |= IPPOF_BOUND_SRCADDR;
455 		}
456 	} else {
457 		ip6obf.select_srcif = FALSE;
458 		if (flags & IPV6_OUTARGS) {
459 			ip6oa->ip6oa_boundif = IFSCOPE_NONE;
460 			ip6oa->ip6oa_flags &= ~(IP6OAF_SELECT_SRCIF |
461 			    IP6OAF_BOUND_IF | IP6OAF_BOUND_SRCADDR);
462 		}
463 	}
464 
465 	if (flags & IPV6_OUTARGS) {
466 		if (ip6oa->ip6oa_flags & IP6OAF_NO_CELLULAR) {
467 			ipf_pktopts.ippo_flags |= IPPOF_NO_IFT_CELLULAR;
468 		}
469 		if (ip6oa->ip6oa_flags & IP6OAF_NO_EXPENSIVE) {
470 			ipf_pktopts.ippo_flags |= IPPOF_NO_IFF_EXPENSIVE;
471 		}
472 		if (ip6oa->ip6oa_flags & IP6OAF_NO_CONSTRAINED) {
473 			ipf_pktopts.ippo_flags |= IPPOF_NO_IFF_CONSTRAINED;
474 		}
475 		adv = &ip6oa->ip6oa_flowadv;
476 		adv->code = FADV_SUCCESS;
477 		ip6oa->ip6oa_flags &= ~IP6OAF_RET_MASK;
478 	}
479 
480 	/*
481 	 * Clear out ifpp to be filled in after determining route. ifpp_save is
482 	 * used to keep old value to release reference properly and dtrace
483 	 * ipsec tunnel traffic properly.
484 	 */
485 	if (ifpp != NULL && *ifpp != NULL) {
486 		*ifpp = NULL;
487 	}
488 
489 #if DUMMYNET
490 	if (args.fwa_pf_rule) {
491 		ip6 = mtod(m, struct ip6_hdr *);
492 		VERIFY(ro != NULL);     /* ro == saved_route */
493 		goto check_with_pf;
494 	}
495 #endif /* DUMMYNET */
496 
497 #if NECP
498 	/*
499 	 * Since all packets are assumed to come from same socket, necp lookup
500 	 * only needs to happen once per function entry.
501 	 */
502 	necp_matched_policy_id = necp_ip6_output_find_policy_match(m, flags,
503 	    (flags & IPV6_OUTARGS) ? ip6oa : NULL, ro ? ro->ro_rt : NULL, &necp_result,
504 	    &necp_result_parameter);
505 #endif /* NECP */
506 
507 	/*
508 	 * If a chain was passed in, prepare for ther first iteration. For all
509 	 * other iterations, this work will be done at evaluateloop: label.
510 	 */
511 	if (packetchain) {
512 		/*
513 		 * Remove m from the chain during processing to avoid
514 		 * accidental frees on entire list.
515 		 */
516 		inputchain = m->m_nextpkt;
517 		m->m_nextpkt = NULL;
518 	}
519 
520 loopit:
521 	packets_processed++;
522 	m->m_pkthdr.pkt_flags &= ~(PKTF_LOOP | PKTF_IFAINFO);
523 	ip6 = mtod(m, struct ip6_hdr *);
524 	nxt0 = ip6->ip6_nxt;
525 	finaldst = ip6->ip6_dst;
526 	ip6obf.hdrsplit = FALSE;
527 	ro_pmtu = NULL;
528 
529 	if (!SLIST_EMPTY(&m->m_pkthdr.tags)) {
530 		inject_filter_ref = ipf_get_inject_filter(m);
531 	} else {
532 		inject_filter_ref = NULL;
533 	}
534 
535 #define MAKE_EXTHDR(hp, mp) do {                                        \
536 	if (hp != NULL) {                                               \
537 	        struct ip6_ext *eh = (struct ip6_ext *)(hp);            \
538 	        error = ip6_copyexthdr((mp), (caddr_t)(hp),             \
539 	            ((eh)->ip6e_len + 1) << 3);                         \
540 	        if (error)                                              \
541 	                goto freehdrs;                                  \
542 	}                                                               \
543 } while (0)
544 
545 	if (opt != NULL) {
546 		/* Hop-by-Hop options header */
547 		MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
548 		/* Destination options header(1st part) */
549 		if (opt->ip6po_rthdr) {
550 			/*
551 			 * Destination options header(1st part)
552 			 * This only makes sense with a routing header.
553 			 * See Section 9.2 of RFC 3542.
554 			 * Disabling this part just for MIP6 convenience is
555 			 * a bad idea.  We need to think carefully about a
556 			 * way to make the advanced API coexist with MIP6
557 			 * options, which might automatically be inserted in
558 			 * the kernel.
559 			 */
560 			MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
561 		}
562 		/* Routing header */
563 		MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
564 		/* Destination options header(2nd part) */
565 		MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
566 	}
567 
568 #undef MAKE_EXTHDR
569 
570 #if NECP
571 	if (necp_matched_policy_id) {
572 		necp_mark_packet_from_ip(m, necp_matched_policy_id);
573 
574 		switch (necp_result) {
575 		case NECP_KERNEL_POLICY_RESULT_PASS:
576 			if (necp_result_parameter.pass_flags & NECP_KERNEL_POLICY_PASS_NO_SKIP_IPSEC) {
577 				break;
578 			}
579 			goto skip_ipsec;
580 		case NECP_KERNEL_POLICY_RESULT_DROP:
581 			error = EHOSTUNREACH;
582 			ip6stat.ip6s_necp_policy_drop++;
583 			goto freehdrs;
584 		case NECP_KERNEL_POLICY_RESULT_SOCKET_DIVERT:
585 			/*
586 			 * Flow divert packets should be blocked at the IP
587 			 * layer.
588 			 */
589 			error = EHOSTUNREACH;
590 			ip6stat.ip6s_necp_policy_drop++;
591 			goto freehdrs;
592 		case NECP_KERNEL_POLICY_RESULT_IP_TUNNEL: {
593 			/*
594 			 * Verify that the packet is being routed to the tunnel
595 			 */
596 			struct ifnet *policy_ifp =
597 			    necp_get_ifnet_from_result_parameter(
598 				&necp_result_parameter);
599 
600 			/*
601 			 * Update the QOS marking policy if
602 			 * 1. upper layer asks it to do so
603 			 * 2. net_qos_policy_restricted is not set
604 			 * 3. qos_marking_gencount doesn't match necp_kernel_socket_policies_gencount (checked in necp_lookup_current_qos_marking)
605 			 */
606 			if (ip6oa != NULL && (ip6oa->ip6oa_flags & IP6OAF_REDO_QOSMARKING_POLICY) &&
607 			    net_qos_policy_restricted != 0) {
608 				bool qos_marking = (ip6oa->ip6oa_flags & IP6OAF_QOSMARKING_ALLOWED) != 0;
609 				qos_marking = necp_lookup_current_qos_marking(&ip6oa->qos_marking_gencount, NULL, policy_ifp, necp_result_parameter.route_rule_id, qos_marking);
610 				if (qos_marking) {
611 					ip6oa->ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED;
612 				} else {
613 					ip6oa->ip6oa_flags &= ~IP6OAF_QOSMARKING_ALLOWED;
614 				}
615 			}
616 
617 			if (policy_ifp == ifp) {
618 				goto skip_ipsec;
619 			} else {
620 				if (necp_packet_can_rebind_to_ifnet(m,
621 				    policy_ifp, (struct route *)&necp_route,
622 				    AF_INET6)) {
623 					/*
624 					 * Set scoped index to the tunnel
625 					 * interface, since it is compatible
626 					 * with the packet. This will only work
627 					 * for callers who pass IPV6_OUTARGS,
628 					 * but that covers all of the clients
629 					 * we care about today.
630 					 */
631 					if (flags & IPV6_OUTARGS) {
632 						ip6oa->ip6oa_boundif =
633 						    policy_ifp->if_index;
634 						ip6oa->ip6oa_flags |=
635 						    IP6OAF_BOUND_IF;
636 					}
637 					if (opt != NULL
638 					    && opt->ip6po_pktinfo != NULL) {
639 						opt->ip6po_pktinfo->
640 						ipi6_ifindex =
641 						    policy_ifp->if_index;
642 					}
643 					ro = &necp_route;
644 					goto skip_ipsec;
645 				} else {
646 					error = ENETUNREACH;
647 					ip6stat.ip6s_necp_policy_drop++;
648 					goto freehdrs;
649 				}
650 			}
651 		}
652 		default:
653 			break;
654 		}
655 	}
656 #endif /* NECP */
657 
658 #if IPSEC
659 	if (ipsec_bypass != 0 || ip6obf.noipsec) {
660 		goto skip_ipsec;
661 	}
662 
663 	if (sp == NULL) {
664 		/* get a security policy for this packet */
665 		if (so != NULL) {
666 			sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND,
667 			    so, &error);
668 		} else {
669 			sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
670 			    0, &error);
671 		}
672 		if (sp == NULL) {
673 			IPSEC_STAT_INCREMENT(ipsec6stat.out_inval);
674 			goto freehdrs;
675 		}
676 	}
677 
678 	error = 0;
679 
680 	/* check policy */
681 	switch (sp->policy) {
682 	case IPSEC_POLICY_DISCARD:
683 	case IPSEC_POLICY_GENERATE:
684 		/*
685 		 * This packet is just discarded.
686 		 */
687 		IPSEC_STAT_INCREMENT(ipsec6stat.out_polvio);
688 		goto freehdrs;
689 
690 	case IPSEC_POLICY_BYPASS:
691 	case IPSEC_POLICY_NONE:
692 		/* no need to do IPsec. */
693 		ip6obf.needipsec = FALSE;
694 		break;
695 
696 	case IPSEC_POLICY_IPSEC:
697 		if (sp->req == NULL) {
698 			/* acquire a policy */
699 			error = key_spdacquire(sp);
700 			goto freehdrs;
701 		}
702 		if (sp->ipsec_if) {
703 			goto skip_ipsec;
704 		} else {
705 			ip6obf.needipsec = TRUE;
706 		}
707 		break;
708 
709 	case IPSEC_POLICY_ENTRUST:
710 	default:
711 		printf("%s: Invalid policy found: %d\n", __func__, sp->policy);
712 		break;
713 	}
714 skip_ipsec:
715 #endif /* IPSEC */
716 
717 	/*
718 	 * Calculate the total length of the extension header chain.
719 	 * Keep the length of the unfragmentable part for fragmentation.
720 	 */
721 	optlen = 0;
722 	if (exthdrs.ip6e_hbh != NULL) {
723 		optlen += exthdrs.ip6e_hbh->m_len;
724 	}
725 	if (exthdrs.ip6e_dest1 != NULL) {
726 		optlen += exthdrs.ip6e_dest1->m_len;
727 	}
728 	if (exthdrs.ip6e_rthdr != NULL) {
729 		optlen += exthdrs.ip6e_rthdr->m_len;
730 	}
731 	unfragpartlen = optlen + sizeof(struct ip6_hdr);
732 
733 	/* NOTE: we don't add AH/ESP length here. do that later. */
734 	if (exthdrs.ip6e_dest2 != NULL) {
735 		optlen += exthdrs.ip6e_dest2->m_len;
736 	}
737 
738 	/*
739 	 * If we need IPsec, or there is at least one extension header,
740 	 * separate IP6 header from the payload.
741 	 */
742 	if ((
743 #if IPSEC
744 		    ip6obf.needipsec ||
745 #endif /* IPSEC */
746 		    optlen) && !ip6obf.hdrsplit) {
747 		if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
748 			m = NULL;
749 			goto freehdrs;
750 		}
751 		m = exthdrs.ip6e_ip6;
752 		ip6obf.hdrsplit = TRUE;
753 	}
754 
755 	/* adjust pointer */
756 	ip6 = mtod(m, struct ip6_hdr *);
757 
758 	/* adjust mbuf packet header length */
759 	m->m_pkthdr.len += optlen;
760 	plen = m->m_pkthdr.len - sizeof(*ip6);
761 
762 	/* If this is a jumbo payload, insert a jumbo payload option. */
763 	if (plen > IPV6_MAXPACKET) {
764 		if (!ip6obf.hdrsplit) {
765 			if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
766 				m = NULL;
767 				goto freehdrs;
768 			}
769 			m = exthdrs.ip6e_ip6;
770 			ip6obf.hdrsplit = TRUE;
771 		}
772 		/* adjust pointer */
773 		ip6 = mtod(m, struct ip6_hdr *);
774 		if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) {
775 			goto freehdrs;
776 		}
777 		ip6->ip6_plen = 0;
778 	} else {
779 		ip6->ip6_plen = htons((uint16_t)plen);
780 	}
781 	/*
782 	 * Concatenate headers and fill in next header fields.
783 	 * Here we have, on "m"
784 	 *	IPv6 payload
785 	 * and we insert headers accordingly.  Finally, we should be getting:
786 	 *	IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
787 	 *
788 	 * during the header composing process, "m" points to IPv6 header.
789 	 * "mprev" points to an extension header prior to esp.
790 	 */
791 	nexthdrp = &ip6->ip6_nxt;
792 	mprev = m;
793 
794 	/*
795 	 * we treat dest2 specially.  this makes IPsec processing
796 	 * much easier.  the goal here is to make mprev point the
797 	 * mbuf prior to dest2.
798 	 *
799 	 * result: IPv6 dest2 payload
800 	 * m and mprev will point to IPv6 header.
801 	 */
802 	if (exthdrs.ip6e_dest2 != NULL) {
803 		if (!ip6obf.hdrsplit) {
804 			panic("assumption failed: hdr not split");
805 			/* NOTREACHED */
806 		}
807 		exthdrs.ip6e_dest2->m_next = m->m_next;
808 		m->m_next = exthdrs.ip6e_dest2;
809 		*mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
810 		ip6->ip6_nxt = IPPROTO_DSTOPTS;
811 	}
812 
813 #define MAKE_CHAIN(m, mp, p, i) do {                                    \
814 	if (m != NULL) {                                                \
815 	        if (!ip6obf.hdrsplit) {                                 \
816 	                panic("assumption failed: hdr not split");      \
817 	/* NOTREACHED */                                \
818 	        }                                                       \
819 	        *mtod((m), u_char *) = *(p);                            \
820 	        *(p) = (i);                                             \
821 	        p = mtod((m), u_char *);                                \
822 	        (m)->m_next = (mp)->m_next;                             \
823 	        (mp)->m_next = (m);                                     \
824 	        (mp) = (m);                                             \
825 	}                                                               \
826 } while (0)
827 	/*
828 	 * result: IPv6 hbh dest1 rthdr dest2 payload
829 	 * m will point to IPv6 header.  mprev will point to the
830 	 * extension header prior to dest2 (rthdr in the above case).
831 	 */
832 	MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
833 	MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, IPPROTO_DSTOPTS);
834 	MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, IPPROTO_ROUTING);
835 
836 	/* It is no longer safe to free the pointers in exthdrs. */
837 	exthdrs.merged = TRUE;
838 
839 #undef MAKE_CHAIN
840 
841 #if IPSEC
842 	if (ip6obf.needipsec && (m->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA)) {
843 		in6_delayed_cksum_offset(m, 0, optlen, nxt0);
844 	}
845 #endif /* IPSEC */
846 
847 	if (!TAILQ_EMPTY(&ipv6_filters) &&
848 	    !((flags & IPV6_OUTARGS) &&
849 	    (ip6oa->ip6oa_flags & IP6OAF_INTCOPROC_ALLOWED)
850 #if NECP
851 	    && !necp_packet_should_skip_filters(m)
852 #endif // NECP
853 	    )) {
854 		struct ipfilter *filter;
855 		int seen = (inject_filter_ref == NULL);
856 		int fixscope = 0;
857 
858 		if (im6o != NULL && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
859 			ippo->ippo_flags |= IPPOF_MCAST_OPTS;
860 			IM6O_LOCK(im6o);
861 			ippo->ippo_mcast_ifnet = im6o->im6o_multicast_ifp;
862 			ippo->ippo_mcast_ttl = im6o->im6o_multicast_hlim;
863 			ippo->ippo_mcast_loop = im6o->im6o_multicast_loop;
864 			IM6O_UNLOCK(im6o);
865 		}
866 
867 		/* Hack: embed the scope_id in the destination */
868 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
869 		    (ip6->ip6_dst.s6_addr16[1] == 0) && (ro != NULL)) {
870 			fixscope = 1;
871 			ip6->ip6_dst.s6_addr16[1] =
872 			    htons((uint16_t)ro->ro_dst.sin6_scope_id);
873 		}
874 
875 		ipf_ref();
876 		TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) {
877 			/*
878 			 * Don't process packet twice if we've already seen it.
879 			 */
880 			if (seen == 0) {
881 				if ((struct ipfilter *)inject_filter_ref ==
882 				    filter) {
883 					seen = 1;
884 				}
885 			} else if (filter->ipf_filter.ipf_output != NULL) {
886 				errno_t result;
887 
888 				result = filter->ipf_filter.ipf_output(
889 					filter->ipf_filter.cookie,
890 					(mbuf_t *)&m, ippo);
891 				if (result == EJUSTRETURN) {
892 					ipf_unref();
893 					m = NULL;
894 					goto evaluateloop;
895 				}
896 				if (result != 0) {
897 					ipf_unref();
898 					goto bad;
899 				}
900 			}
901 		}
902 		ipf_unref();
903 
904 		ip6 = mtod(m, struct ip6_hdr *);
905 		/* Hack: cleanup embedded scope_id if we put it there */
906 		if (fixscope) {
907 			ip6->ip6_dst.s6_addr16[1] = 0;
908 		}
909 	}
910 
911 #if IPSEC
912 	if (ip6obf.needipsec) {
913 		uint8_t segleft_org;
914 
915 		/*
916 		 * pointers after IPsec headers are not valid any more.
917 		 * other pointers need a great care too.
918 		 * (IPsec routines should not mangle mbufs prior to AH/ESP)
919 		 */
920 		exthdrs.ip6e_dest2 = NULL;
921 
922 		if (exthdrs.ip6e_rthdr != NULL) {
923 			rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
924 			segleft_org = rh->ip6r_segleft;
925 			rh->ip6r_segleft = 0;
926 		} else {
927 			rh = NULL;
928 			segleft_org = 0;
929 		}
930 
931 		ipsec_state.m = m;
932 		error = ipsec6_output_trans(&ipsec_state, nexthdrp, mprev,
933 		    sp, flags, &needipsectun);
934 		m = ipsec_state.m;
935 		if (error) {
936 			/* mbuf is already reclaimed in ipsec6_output_trans. */
937 			m = NULL;
938 			switch (error) {
939 			case EHOSTUNREACH:
940 			case ENETUNREACH:
941 			case EMSGSIZE:
942 			case ENOBUFS:
943 			case ENOMEM:
944 				break;
945 			default:
946 				printf("ip6_output (ipsec): error code %d\n",
947 				    error);
948 				OS_FALLTHROUGH;
949 			case ENOENT:
950 				/* don't show these error codes to the user */
951 				error = 0;
952 				break;
953 			}
954 			goto bad;
955 		}
956 		if (exthdrs.ip6e_rthdr != NULL) {
957 			/* ah6_output doesn't modify mbuf chain */
958 			rh->ip6r_segleft = segleft_org;
959 		}
960 	}
961 #endif /* IPSEC */
962 
963 	/* If there is a routing header, discard the packet. */
964 	if (exthdrs.ip6e_rthdr != NULL) {
965 		error = EINVAL;
966 		goto bad;
967 	}
968 
969 	/* Source address validation */
970 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
971 	    !(flags & IPV6_UNSPECSRC)) {
972 		error = EOPNOTSUPP;
973 		ip6stat.ip6s_badscope++;
974 		goto bad;
975 	}
976 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
977 		error = EOPNOTSUPP;
978 		ip6stat.ip6s_badscope++;
979 		goto bad;
980 	}
981 
982 	ip6stat.ip6s_localout++;
983 
984 	/*
985 	 * Route packet.
986 	 */
987 	if (ro == NULL) {
988 		ro = &ip6route;
989 		bzero((caddr_t)ro, sizeof(*ro));
990 	}
991 	ro_pmtu = ro;
992 	if (opt != NULL && opt->ip6po_rthdr) {
993 		ro = &opt->ip6po_route;
994 	}
995 	dst = SIN6(&ro->ro_dst);
996 
997 	if (ro->ro_rt != NULL) {
998 		RT_LOCK_ASSERT_NOTHELD(ro->ro_rt);
999 	}
1000 	/*
1001 	 * if specified, try to fill in the traffic class field.
1002 	 * do not override if a non-zero value is already set.
1003 	 * we check the diffserv field and the ecn field separately.
1004 	 */
1005 	if (opt != NULL && opt->ip6po_tclass >= 0) {
1006 		int mask = 0;
1007 
1008 		if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0) {
1009 			mask |= 0xfc;
1010 		}
1011 		if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0) {
1012 			mask |= 0x03;
1013 		}
1014 		if (mask != 0) {
1015 			ip6->ip6_flow |=
1016 			    htonl((opt->ip6po_tclass & mask) << 20);
1017 		}
1018 	}
1019 
1020 	/* fill in or override the hop limit field, if necessary. */
1021 	if (opt && opt->ip6po_hlim != -1) {
1022 		ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
1023 	} else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1024 		if (im6o != NULL) {
1025 			IM6O_LOCK(im6o);
1026 			ip6->ip6_hlim = im6o->im6o_multicast_hlim;
1027 			IM6O_UNLOCK(im6o);
1028 		} else {
1029 			ip6->ip6_hlim = (uint8_t)ip6_defmcasthlim;
1030 		}
1031 	}
1032 
1033 	/*
1034 	 * If there is a cached route, check that it is to the same
1035 	 * destination and is still up. If not, free it and try again.
1036 	 * Test rt_flags without holding rt_lock for performance reasons;
1037 	 * if the route is down it will hopefully be caught by the layer
1038 	 * below (since it uses this route as a hint) or during the
1039 	 * next transmit.
1040 	 */
1041 	if (ROUTE_UNUSABLE(ro) || dst->sin6_family != AF_INET6 ||
1042 	    !in6_are_addr_equal_scoped(&dst->sin6_addr, &ip6->ip6_dst, dst->sin6_scope_id, ip6_output_getdstifscope(m))) {
1043 		ROUTE_RELEASE(ro);
1044 	}
1045 
1046 	if (ro->ro_rt == NULL) {
1047 		bzero(dst, sizeof(*dst));
1048 		dst->sin6_family = AF_INET6;
1049 		dst->sin6_len = sizeof(struct sockaddr_in6);
1050 		dst->sin6_addr = ip6->ip6_dst;
1051 	}
1052 #if IPSEC
1053 	if (ip6obf.needipsec && needipsectun) {
1054 #if CONFIG_DTRACE
1055 		struct ifnet *trace_ifp = (ifpp_save != NULL) ? (*ifpp_save) : NULL;
1056 #endif /* CONFIG_DTRACE */
1057 		/*
1058 		 * All the extension headers will become inaccessible
1059 		 * (since they can be encrypted).
1060 		 * Don't panic, we need no more updates to extension headers
1061 		 * on inner IPv6 packet (since they are now encapsulated).
1062 		 *
1063 		 * IPv6 [ESP|AH] IPv6 [extension headers] payload
1064 		 */
1065 		bzero(&exthdrs, sizeof(exthdrs));
1066 		exthdrs.ip6e_ip6 = m;
1067 
1068 		ipsec_state.m = m;
1069 		route_copyout((struct route *)&ipsec_state.ro, (struct route *)ro,
1070 		    sizeof(struct route_in6));
1071 		ipsec_state.dst = SA(dst);
1072 
1073 		/* So that we can see packets inside the tunnel */
1074 		DTRACE_IP6(send, struct mbuf *, m, struct inpcb *, NULL,
1075 		    struct ip6_hdr *, ip6, struct ifnet *, trace_ifp,
1076 		    struct ip *, NULL, struct ip6_hdr *, ip6);
1077 
1078 		error = ipsec6_output_tunnel(&ipsec_state, sp, flags);
1079 		/* tunneled in IPv4? packet is gone */
1080 		if (ipsec_state.tunneled == 4) {
1081 			m = NULL;
1082 			goto evaluateloop;
1083 		}
1084 		m = ipsec_state.m;
1085 		ipsec_saved_route = ro;
1086 		ro = (struct route_in6 *)&ipsec_state.ro;
1087 		dst = SIN6(ipsec_state.dst);
1088 		if (error) {
1089 			/* mbuf is already reclaimed in ipsec6_output_tunnel. */
1090 			m = NULL;
1091 			switch (error) {
1092 			case EHOSTUNREACH:
1093 			case ENETUNREACH:
1094 			case EMSGSIZE:
1095 			case ENOBUFS:
1096 			case ENOMEM:
1097 				break;
1098 			default:
1099 				printf("ip6_output (ipsec): error code %d\n",
1100 				    error);
1101 				OS_FALLTHROUGH;
1102 			case ENOENT:
1103 				/* don't show these error codes to the user */
1104 				error = 0;
1105 				break;
1106 			}
1107 			goto bad;
1108 		}
1109 		/*
1110 		 * The packet has been encapsulated so the ifscope
1111 		 * is no longer valid since it does not apply to the
1112 		 * outer address: ignore the ifscope.
1113 		 */
1114 		if (flags & IPV6_OUTARGS) {
1115 			ip6oa->ip6oa_boundif = IFSCOPE_NONE;
1116 			ip6oa->ip6oa_flags &= ~IP6OAF_BOUND_IF;
1117 		}
1118 		if (opt != NULL && opt->ip6po_pktinfo != NULL) {
1119 			if (opt->ip6po_pktinfo->ipi6_ifindex != IFSCOPE_NONE) {
1120 				opt->ip6po_pktinfo->ipi6_ifindex = IFSCOPE_NONE;
1121 			}
1122 		}
1123 		exthdrs.ip6e_ip6 = m;
1124 	}
1125 #endif /* IPSEC */
1126 
1127 	/*
1128 	 * ifp should only be filled in for dummy net packets which will jump
1129 	 * to check_with_pf label.
1130 	 */
1131 	if (ifp != NULL) {
1132 		VERIFY(ip6obf.route_selected);
1133 	}
1134 
1135 	/* adjust pointer */
1136 	ip6 = mtod(m, struct ip6_hdr *);
1137 
1138 	if (ip6obf.select_srcif) {
1139 		bzero(&src_sa, sizeof(src_sa));
1140 		src_sa.sin6_family = AF_INET6;
1141 		src_sa.sin6_len = sizeof(src_sa);
1142 		src_sa.sin6_addr = ip6->ip6_src;
1143 		src_sa.sin6_scope_id = (!in6_embedded_scope && IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) ? ip6_output_getsrcifscope(m) : IFSCOPE_NONE;
1144 	}
1145 	bzero(&dst_sa, sizeof(dst_sa));
1146 	dst_sa.sin6_family = AF_INET6;
1147 	dst_sa.sin6_len = sizeof(dst_sa);
1148 	dst_sa.sin6_addr = ip6->ip6_dst;
1149 	dst_sa.sin6_scope_id = (!in6_embedded_scope && IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) ? ip6_output_getdstifscope(m) : IFSCOPE_NONE;
1150 
1151 	/*
1152 	 * Only call in6_selectroute() on first iteration to avoid taking
1153 	 * multiple references on ifp and rt.
1154 	 *
1155 	 * in6_selectroute() might return an ifp with its reference held
1156 	 * even in the error case, so make sure to release its reference.
1157 	 * ip6oa may be NULL if IPV6_OUTARGS isn't set.
1158 	 */
1159 	if (!ip6obf.route_selected) {
1160 		error = in6_selectroute( ip6obf.select_srcif ? &src_sa : NULL,
1161 		    &dst_sa, opt, im6o, &src_ia, ro, &ifp, &rt, 0, ip6oa);
1162 
1163 		if (error != 0) {
1164 			switch (error) {
1165 			case EHOSTUNREACH:
1166 				ip6stat.ip6s_noroute++;
1167 				break;
1168 			case EADDRNOTAVAIL:
1169 			default:
1170 				break; /* XXX statistics? */
1171 			}
1172 			if (ifp != NULL) {
1173 				in6_ifstat_inc(ifp, ifs6_out_discard);
1174 			}
1175 			/* ifp (if non-NULL) will be released at the end */
1176 			goto bad;
1177 		}
1178 		ip6obf.route_selected = TRUE;
1179 	}
1180 	if (rt == NULL) {
1181 		/*
1182 		 * If in6_selectroute() does not return a route entry,
1183 		 * dst may not have been updated.
1184 		 */
1185 		*dst = dst_sa;  /* XXX */
1186 	}
1187 
1188 #if NECP
1189 	/* Catch-all to check if the interface is allowed */
1190 	if (!necp_packet_is_allowed_over_interface(m, ifp)) {
1191 		error = EHOSTUNREACH;
1192 		ip6stat.ip6s_necp_policy_drop++;
1193 		goto bad;
1194 	}
1195 #endif /* NECP */
1196 
1197 	/*
1198 	 * then rt (for unicast) and ifp must be non-NULL valid values.
1199 	 */
1200 	if (!(flags & IPV6_FORWARDING)) {
1201 		in6_ifstat_inc_na(ifp, ifs6_out_request);
1202 	}
1203 	if (rt != NULL) {
1204 		RT_LOCK(rt);
1205 		if (ia == NULL) {
1206 			ia = (struct in6_ifaddr *)(rt->rt_ifa);
1207 			if (ia != NULL) {
1208 				IFA_ADDREF(&ia->ia_ifa);
1209 			}
1210 		}
1211 		rt->rt_use++;
1212 		RT_UNLOCK(rt);
1213 	}
1214 
1215 	/*
1216 	 * The outgoing interface must be in the zone of source and
1217 	 * destination addresses (except local/loopback).  We should
1218 	 * use ia_ifp to support the case of sending packets to an
1219 	 * address of our own.
1220 	 */
1221 	if (ia != NULL && ia->ia_ifp) {
1222 		ifnet_reference(ia->ia_ifp);    /* for origifp */
1223 		if (origifp != NULL) {
1224 			ifnet_release(origifp);
1225 		}
1226 		origifp = ia->ia_ifp;
1227 	} else {
1228 		if (ifp != NULL) {
1229 			ifnet_reference(ifp);   /* for origifp */
1230 		}
1231 		if (origifp != NULL) {
1232 			ifnet_release(origifp);
1233 		}
1234 		origifp = ifp;
1235 	}
1236 
1237 	/* skip scope enforcements for local/loopback route */
1238 	if (rt == NULL || !(rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
1239 		struct in6_addr src0, dst0;
1240 		u_int32_t zone;
1241 
1242 		src0 = ip6->ip6_src;
1243 		if (in6_setscope(&src0, origifp, &zone)) {
1244 			goto badscope;
1245 		}
1246 		bzero(&src_sa, sizeof(src_sa));
1247 		src_sa.sin6_family = AF_INET6;
1248 		src_sa.sin6_len = sizeof(src_sa);
1249 		src_sa.sin6_addr = ip6->ip6_src;
1250 		src_sa.sin6_scope_id = (!in6_embedded_scope && IN6_IS_SCOPE_EMBED(&src_sa.sin6_addr)) ? ip6_output_getsrcifscope(m) : IFSCOPE_NONE;
1251 		if ((sa6_recoverscope(&src_sa, TRUE) ||
1252 		    zone != src_sa.sin6_scope_id)) {
1253 			goto badscope;
1254 		}
1255 
1256 		dst0 = ip6->ip6_dst;
1257 		if ((in6_setscope(&dst0, origifp, &zone))) {
1258 			goto badscope;
1259 		}
1260 		/* re-initialize to be sure */
1261 		bzero(&dst_sa, sizeof(dst_sa));
1262 		dst_sa.sin6_family = AF_INET6;
1263 		dst_sa.sin6_len = sizeof(dst_sa);
1264 		dst_sa.sin6_addr = ip6->ip6_dst;
1265 		dst_sa.sin6_scope_id = (!in6_embedded_scope && IN6_IS_SCOPE_EMBED(&dst_sa.sin6_addr)) ?  ip6_output_getdstifscope(m) : IFSCOPE_NONE;
1266 		if ((sa6_recoverscope(&dst_sa, TRUE) ||
1267 		    zone != dst_sa.sin6_scope_id)) {
1268 			goto badscope;
1269 		}
1270 
1271 		/* scope check is done. */
1272 		goto routefound;
1273 
1274 badscope:
1275 		ip6stat.ip6s_badscope++;
1276 		in6_ifstat_inc(origifp, ifs6_out_discard);
1277 		if (error == 0) {
1278 			error = EHOSTUNREACH; /* XXX */
1279 		}
1280 		goto bad;
1281 	}
1282 
1283 routefound:
1284 	if (rt != NULL && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1285 		if (opt != NULL && opt->ip6po_nextroute.ro_rt) {
1286 			/*
1287 			 * The nexthop is explicitly specified by the
1288 			 * application.  We assume the next hop is an IPv6
1289 			 * address.
1290 			 */
1291 			dst = SIN6(opt->ip6po_nexthop);
1292 		} else if ((rt->rt_flags & RTF_GATEWAY)) {
1293 			dst = SIN6(rt->rt_gateway);
1294 		}
1295 		/*
1296 		 * For packets destined to local/loopback, record the
1297 		 * source the source interface (which owns the source
1298 		 * address), as well as the output interface.  This is
1299 		 * needed to reconstruct the embedded zone for the
1300 		 * link-local address case in ip6_input().
1301 		 */
1302 		if (ia != NULL && (ifp->if_flags & IFF_LOOPBACK)) {
1303 			uint32_t srcidx;
1304 
1305 			if (src_ia != NULL) {
1306 				srcidx = src_ia->ia_ifp->if_index;
1307 			} else if (ro->ro_srcia != NULL) {
1308 				srcidx = ro->ro_srcia->ifa_ifp->if_index;
1309 			} else {
1310 				srcidx = 0;
1311 			}
1312 
1313 			ip6_setsrcifaddr_info(m, srcidx, NULL);
1314 			ip6_setdstifaddr_info(m, 0, ia);
1315 		}
1316 	}
1317 
1318 	if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1319 		m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
1320 	} else {
1321 		struct  in6_multi *in6m;
1322 
1323 		m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
1324 		in6_ifstat_inc_na(ifp, ifs6_out_mcast);
1325 
1326 		/*
1327 		 * Confirm that the outgoing interface supports multicast.
1328 		 */
1329 		if (!(ifp->if_flags & IFF_MULTICAST)) {
1330 			ip6stat.ip6s_noroute++;
1331 			in6_ifstat_inc(ifp, ifs6_out_discard);
1332 			error = ENETUNREACH;
1333 			goto bad;
1334 		}
1335 		in6_multihead_lock_shared();
1336 		IN6_LOOKUP_MULTI(&ip6->ip6_dst, ifp, in6m);
1337 		in6_multihead_lock_done();
1338 		if (im6o != NULL) {
1339 			IM6O_LOCK(im6o);
1340 		}
1341 		if (in6m != NULL &&
1342 		    (im6o == NULL || im6o->im6o_multicast_loop)) {
1343 			if (im6o != NULL) {
1344 				IM6O_UNLOCK(im6o);
1345 			}
1346 			/*
1347 			 * If we belong to the destination multicast group
1348 			 * on the outgoing interface, and the caller did not
1349 			 * forbid loopback, loop back a copy.
1350 			 */
1351 			ip6_mloopback(NULL, ifp, m, dst, optlen, nxt0);
1352 		} else if (im6o != NULL) {
1353 			IM6O_UNLOCK(im6o);
1354 		}
1355 		if (in6m != NULL) {
1356 			IN6M_REMREF(in6m);
1357 		}
1358 		/*
1359 		 * Multicasts with a hoplimit of zero may be looped back,
1360 		 * above, but must not be transmitted on a network.
1361 		 * Also, multicasts addressed to the loopback interface
1362 		 * are not sent -- the above call to ip6_mloopback() will
1363 		 * loop back a copy if this host actually belongs to the
1364 		 * destination group on the loopback interface.
1365 		 */
1366 		if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
1367 		    IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
1368 			/* remove m from the packetchain and continue looping */
1369 			if (m != NULL) {
1370 				m_freem(m);
1371 			}
1372 			m = NULL;
1373 			goto evaluateloop;
1374 		}
1375 	}
1376 
1377 	/*
1378 	 * Fill the outgoing inteface to tell the upper layer
1379 	 * to increment per-interface statistics.
1380 	 */
1381 	if (ifpp != NULL && *ifpp == NULL) {
1382 		ifnet_reference(ifp);   /* for caller */
1383 		*ifpp = ifp;
1384 	}
1385 
1386 	/* Determine path MTU. */
1387 	if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, ifp->if_index, &mtu)) != 0) {
1388 		goto bad;
1389 	}
1390 
1391 	/*
1392 	 * The caller of this function may specify to use the minimum MTU
1393 	 * in some cases.
1394 	 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
1395 	 * setting.  The logic is a bit complicated; by default, unicast
1396 	 * packets will follow path MTU while multicast packets will be sent at
1397 	 * the minimum MTU.  If IP6PO_MINMTU_ALL is specified, all packets
1398 	 * including unicast ones will be sent at the minimum MTU.  Multicast
1399 	 * packets will always be sent at the minimum MTU unless
1400 	 * IP6PO_MINMTU_DISABLE is explicitly specified.
1401 	 * See RFC 3542 for more details.
1402 	 */
1403 	if (mtu > IPV6_MMTU) {
1404 		if ((flags & IPV6_MINMTU)) {
1405 			mtu = IPV6_MMTU;
1406 		} else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL) {
1407 			mtu = IPV6_MMTU;
1408 		} else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
1409 		    (opt == NULL ||
1410 		    opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
1411 			mtu = IPV6_MMTU;
1412 		}
1413 	}
1414 
1415 	/*
1416 	 * clear embedded scope identifiers if necessary.
1417 	 * in6_clearscope will touch the addresses only when necessary.
1418 	 */
1419 	in6_clearscope(&ip6->ip6_src);
1420 	in6_clearscope(&ip6->ip6_dst);
1421 	/*
1422 	 * If the outgoing packet contains a hop-by-hop options header,
1423 	 * it must be examined and processed even by the source node.
1424 	 * (RFC 2460, section 4.)
1425 	 */
1426 	if (exthdrs.ip6e_hbh != NULL) {
1427 		struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
1428 		u_int32_t dummy; /* XXX unused */
1429 		uint32_t oplen = 0; /* for ip6_process_hopopts() */
1430 #if DIAGNOSTIC
1431 		if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len) {
1432 			panic("ip6e_hbh is not continuous");
1433 		}
1434 #endif
1435 		/*
1436 		 * XXX: If we have to send an ICMPv6 error to the sender,
1437 		 * we need the M_LOOP flag since icmp6_error() expects
1438 		 * the IPv6 and the hop-by-hop options header are
1439 		 * continuous unless the flag is set.
1440 		 */
1441 		m->m_flags |= M_LOOP;
1442 		m->m_pkthdr.rcvif = ifp;
1443 		if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
1444 		    ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
1445 		    &dummy, &oplen) < 0) {
1446 			/*
1447 			 * m was already freed at this point. Set to NULL so it
1448 			 * is not re-freed at end of ip6_output_list.
1449 			 */
1450 			m = NULL;
1451 			error = EINVAL; /* better error? */
1452 			goto bad;
1453 		}
1454 		m->m_flags &= ~M_LOOP; /* XXX */
1455 		m->m_pkthdr.rcvif = NULL;
1456 	}
1457 
1458 #if DUMMYNET
1459 check_with_pf:
1460 #endif /* DUMMYNET */
1461 #if PF
1462 	if (PF_IS_ENABLED && !skip_pf) {
1463 #if DUMMYNET
1464 
1465 		/*
1466 		 * TODO: Need to save opt->ip6po_flags for reinjection
1467 		 * rdar://10434993
1468 		 */
1469 		args.fwa_oif = ifp;
1470 		args.fwa_oflags = flags;
1471 		if (flags & IPV6_OUTARGS) {
1472 			args.fwa_ip6oa = ip6oa;
1473 		}
1474 		args.fwa_ro6 = ro;
1475 		args.fwa_dst6 = dst;
1476 		args.fwa_ro6_pmtu = ro_pmtu;
1477 		args.fwa_origifp = origifp;
1478 		args.fwa_mtu = mtu;
1479 		args.fwa_unfragpartlen = unfragpartlen;
1480 		args.fwa_exthdrs = &exthdrs;
1481 		/* Invoke outbound packet filter */
1482 		error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE, &args);
1483 #else /* !DUMMYNET */
1484 		error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE, NULL);
1485 #endif /* !DUMMYNET */
1486 
1487 		if (error != 0 || m == NULL) {
1488 			if (m != NULL) {
1489 				panic("%s: unexpected packet %p",
1490 				    __func__, m);
1491 				/* NOTREACHED */
1492 			}
1493 			/* m was already freed by callee and is now NULL.  */
1494 			goto evaluateloop;
1495 		}
1496 		ip6 = mtod(m, struct ip6_hdr *);
1497 	}
1498 #endif /* PF */
1499 
1500 #ifdef IPSEC
1501 	/* clean ipsec history before fragmentation */
1502 	ipsec_delaux(m);
1503 #endif /* IPSEC */
1504 
1505 	if (ip6oa != NULL) {
1506 		u_int8_t dscp;
1507 
1508 		dscp = (ntohl(ip6->ip6_flow) & IP6FLOW_DSCP_MASK) >> IP6FLOW_DSCP_SHIFT;
1509 
1510 		error = set_packet_qos(m, ifp,
1511 		    ip6oa->ip6oa_flags & IP6OAF_QOSMARKING_ALLOWED ? TRUE : FALSE,
1512 		    ip6oa->ip6oa_sotc, ip6oa->ip6oa_netsvctype, &dscp);
1513 		if (error == 0) {
1514 			ip6->ip6_flow &= ~htonl(IP6FLOW_DSCP_MASK);
1515 			ip6->ip6_flow |= htonl((u_int32_t)dscp << IP6FLOW_DSCP_SHIFT);
1516 		} else {
1517 			printf("%s if_dscp_for_mbuf() error %d\n", __func__, error);
1518 			error = 0;
1519 		}
1520 	}
1521 	/*
1522 	 * Determine whether fragmentation is necessary. If so, m is passed
1523 	 * back as a chain of packets and original mbuf is freed. Otherwise, m
1524 	 * is unchanged.
1525 	 */
1526 	error = ip6_fragment_packet(&m, opt, ip6oa,
1527 	    &exthdrs, ifp, mtu, unfragpartlen, ro_pmtu, nxt0,
1528 	    optlen);
1529 
1530 	if (error) {
1531 		goto bad;
1532 	}
1533 
1534 /*
1535  * The evaluateloop label is where we decide whether to continue looping over
1536  * packets or call into nd code to send.
1537  */
1538 evaluateloop:
1539 
1540 	/*
1541 	 * m may be NULL when we jump to the evaluateloop label from PF or
1542 	 * other code that can drop packets.
1543 	 */
1544 	if (m != NULL) {
1545 		/*
1546 		 * If we already have a chain to send, tack m onto the end.
1547 		 * Otherwise make m the start and end of the to-be-sent chain.
1548 		 */
1549 		if (sendchain != NULL) {
1550 			sendchain_last->m_nextpkt = m;
1551 		} else {
1552 			sendchain = m;
1553 		}
1554 
1555 		/* Fragmentation may mean m is a chain. Find the last packet. */
1556 		while (m->m_nextpkt) {
1557 			m = m->m_nextpkt;
1558 		}
1559 		sendchain_last = m;
1560 		pktcnt++;
1561 	}
1562 
1563 	/* Fill in next m from inputchain as appropriate. */
1564 	m = inputchain;
1565 	if (m != NULL) {
1566 		/* Isolate m from rest of input chain. */
1567 		inputchain = m->m_nextpkt;
1568 		m->m_nextpkt = NULL;
1569 
1570 		/*
1571 		 * Clear exthdrs and ipsec_state so stale contents are not
1572 		 * reused. Note this also clears the exthdrs.merged flag.
1573 		 */
1574 		bzero(&exthdrs, sizeof(exthdrs));
1575 		bzero(&ipsec_state, sizeof(ipsec_state));
1576 
1577 		/* Continue looping. */
1578 		goto loopit;
1579 	}
1580 
1581 	/*
1582 	 * If we get here, there's no more mbufs in inputchain, so send the
1583 	 * sendchain if there is one.
1584 	 */
1585 	if (pktcnt > 0) {
1586 		error = nd6_output_list(ifp, origifp, sendchain, dst,
1587 		    ro->ro_rt, adv);
1588 		/*
1589 		 * Fall through to done label even in error case because
1590 		 * nd6_output_list frees packetchain in both success and
1591 		 * failure cases.
1592 		 */
1593 	}
1594 
1595 done:
1596 	if (ifpp_save != NULL && *ifpp_save != NULL) {
1597 		ifnet_release(*ifpp_save);
1598 		*ifpp_save = NULL;
1599 	}
1600 	ROUTE_RELEASE(&ip6route);
1601 #if IPSEC
1602 	ROUTE_RELEASE(&ipsec_state.ro);
1603 	if (sp != NULL) {
1604 		key_freesp(sp, KEY_SADB_UNLOCKED);
1605 	}
1606 #endif /* IPSEC */
1607 #if NECP
1608 	ROUTE_RELEASE(&necp_route);
1609 #endif /* NECP */
1610 #if DUMMYNET
1611 	ROUTE_RELEASE(&saved_route);
1612 	ROUTE_RELEASE(&saved_ro_pmtu);
1613 #endif /* DUMMYNET */
1614 
1615 	if (ia != NULL) {
1616 		IFA_REMREF(&ia->ia_ifa);
1617 	}
1618 	if (src_ia != NULL) {
1619 		IFA_REMREF(&src_ia->ia_ifa);
1620 	}
1621 	if (ifp != NULL) {
1622 		ifnet_release(ifp);
1623 	}
1624 	if (origifp != NULL) {
1625 		ifnet_release(origifp);
1626 	}
1627 	if (ip6_output_measure) {
1628 		net_perf_measure_time(&net_perf, &start_tv, packets_processed);
1629 		net_perf_histogram(&net_perf, packets_processed);
1630 	}
1631 	return error;
1632 
1633 freehdrs:
1634 	if (exthdrs.ip6e_hbh != NULL) {
1635 		if (exthdrs.merged) {
1636 			panic("Double free of ip6e_hbh");
1637 		}
1638 		m_freem(exthdrs.ip6e_hbh);
1639 	}
1640 	if (exthdrs.ip6e_dest1 != NULL) {
1641 		if (exthdrs.merged) {
1642 			panic("Double free of ip6e_dest1");
1643 		}
1644 		m_freem(exthdrs.ip6e_dest1);
1645 	}
1646 	if (exthdrs.ip6e_rthdr != NULL) {
1647 		if (exthdrs.merged) {
1648 			panic("Double free of ip6e_rthdr");
1649 		}
1650 		m_freem(exthdrs.ip6e_rthdr);
1651 	}
1652 	if (exthdrs.ip6e_dest2 != NULL) {
1653 		if (exthdrs.merged) {
1654 			panic("Double free of ip6e_dest2");
1655 		}
1656 		m_freem(exthdrs.ip6e_dest2);
1657 	}
1658 	/* FALLTHRU */
1659 bad:
1660 	if (inputchain != NULL) {
1661 		m_freem_list(inputchain);
1662 	}
1663 	if (sendchain != NULL) {
1664 		m_freem_list(sendchain);
1665 	}
1666 	if (m != NULL) {
1667 		m_freem(m);
1668 	}
1669 
1670 	goto done;
1671 
1672 #undef ipf_pktopts
1673 #undef exthdrs
1674 #undef ip6route
1675 #undef ipsec_state
1676 #undef saved_route
1677 #undef saved_ro_pmtu
1678 #undef args
1679 }
1680 
1681 /* ip6_fragment_packet
1682  *
1683  * The fragmentation logic is rather complex:
1684  * 1: normal case (dontfrag == 0)
1685  * 1-a:	send as is if tlen <= path mtu
1686  * 1-b:	fragment if tlen > path mtu
1687  *
1688  * 2: if user asks us not to fragment (dontfrag == 1)
1689  * 2-a:	send as is if tlen <= interface mtu
1690  * 2-b:	error if tlen > interface mtu
1691  */
1692 
1693 static int
ip6_fragment_packet(struct mbuf ** mptr,struct ip6_pktopts * opt,struct ip6_out_args * ip6oa,struct ip6_exthdrs * exthdrsp,struct ifnet * ifp,uint32_t mtu,uint32_t unfragpartlen,struct route_in6 * ro_pmtu,int nxt0,uint32_t optlen)1694 ip6_fragment_packet(struct mbuf **mptr, struct ip6_pktopts *opt,
1695     struct ip6_out_args *ip6oa, struct ip6_exthdrs *exthdrsp,
1696     struct ifnet *ifp, uint32_t mtu, uint32_t unfragpartlen,
1697     struct route_in6 *ro_pmtu, int nxt0, uint32_t optlen)
1698 {
1699 	VERIFY(NULL != mptr);
1700 	struct mbuf *m = *mptr;
1701 	int error = 0;
1702 	uint32_t tlen = m->m_pkthdr.len;
1703 	boolean_t dontfrag = (opt != NULL && (opt->ip6po_flags & IP6PO_DONTFRAG)) ||
1704 	    (ip6oa != NULL && (ip6oa->ip6oa_flags & IP6OAF_DONT_FRAG));
1705 
1706 	if (m->m_pkthdr.pkt_flags & PKTF_FORWARDED) {
1707 		dontfrag = TRUE;
1708 		/*
1709 		 * Discard partial sum information if this packet originated
1710 		 * from another interface; the packet would already have the
1711 		 * final checksum and we shouldn't recompute it.
1712 		 */
1713 		if ((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PARTIAL)) ==
1714 		    (CSUM_DATA_VALID | CSUM_PARTIAL)) {
1715 			m->m_pkthdr.csum_flags &= ~CSUM_TX_FLAGS;
1716 			m->m_pkthdr.csum_data = 0;
1717 		}
1718 	}
1719 
1720 	/* Access without acquiring nd_ifinfo lock for performance */
1721 	if (dontfrag && tlen > IN6_LINKMTU(ifp)) {      /* case 2-b */
1722 		/*
1723 		 * Even if the DONTFRAG option is specified, we cannot send the
1724 		 * packet when the data length is larger than the MTU of the
1725 		 * outgoing interface.
1726 		 * Notify the error by sending IPV6_PATHMTU ancillary data as
1727 		 * well as returning an error code (the latter is not described
1728 		 * in the API spec.)
1729 		 */
1730 		u_int32_t mtu32;
1731 		struct ip6ctlparam ip6cp;
1732 
1733 		mtu32 = (u_int32_t)mtu;
1734 		bzero(&ip6cp, sizeof(ip6cp));
1735 		ip6cp.ip6c_cmdarg = (void *)&mtu32;
1736 		pfctlinput2(PRC_MSGSIZE, SA(&ro_pmtu->ro_dst), (void *)&ip6cp);
1737 		return EMSGSIZE;
1738 	}
1739 
1740 	/*
1741 	 * transmit packet without fragmentation
1742 	 */
1743 	if (dontfrag ||
1744 	    (tlen <= mtu || TSO_IPV6_OK(ifp, m) ||
1745 	    (ifp->if_hwassist & CSUM_FRAGMENT_IPV6))) {
1746 		/*
1747 		 * mppn not updated in this case because no new chain is formed
1748 		 * and inserted
1749 		 */
1750 		ip6_output_checksum(ifp, mtu, m, nxt0, tlen, optlen);
1751 	} else {
1752 		/*
1753 		 * time to fragment - cases 1-b is handled inside
1754 		 * ip6_do_fragmentation().
1755 		 * mppn is passed down to be updated to point at fragment chain.
1756 		 */
1757 		u_int8_t *lexthdrsp;
1758 
1759 		if (exthdrsp->ip6e_rthdr != NULL) {
1760 			lexthdrsp = mtod(exthdrsp->ip6e_rthdr, uint8_t *);
1761 		} else if (exthdrsp->ip6e_dest1 != NULL) {
1762 			lexthdrsp = mtod(exthdrsp->ip6e_dest1, uint8_t *);
1763 		} else if (exthdrsp->ip6e_hbh != NULL) {
1764 			lexthdrsp = mtod(exthdrsp->ip6e_hbh, uint8_t *);
1765 		} else {
1766 			lexthdrsp = NULL;
1767 		}
1768 		error = ip6_do_fragmentation(mptr, optlen, ifp,
1769 		    unfragpartlen, mtod(m, struct ip6_hdr *), lexthdrsp, mtu,
1770 		    nxt0, htonl(ip6_randomid()));
1771 	}
1772 
1773 	return error;
1774 }
1775 
1776 /*
1777  * ip6_do_fragmentation() is called by ip6_fragment_packet() after determining
1778  * the packet needs to be fragmented. on success, morig is freed and a chain
1779  * of fragments is linked into the packet chain where morig existed. Otherwise,
1780  * an errno is returned.
1781  * optlen:        total length of all extension headers (excludes the IPv6 header).
1782  * unfragpartlen: length of the per-fragment headers which consist of the IPv6
1783  *                header plus any extension headers that must be processed by nodes
1784  *                en route to the destination.
1785  * lexthdrsp:     pointer to the last extension header in the unfragmentable part
1786  *                or NULL.
1787  * nxt0:          upper-layer protocol number.
1788  * id:            Identification value to be used in the fragment header.
1789  */
1790 int
ip6_do_fragmentation(struct mbuf ** mptr,uint32_t optlen,struct ifnet * ifp,uint32_t unfragpartlen,struct ip6_hdr * ip6,uint8_t * lexthdrsp,uint32_t mtu,int nxt0,uint32_t id)1791 ip6_do_fragmentation(struct mbuf **mptr, uint32_t optlen, struct ifnet *ifp,
1792     uint32_t unfragpartlen, struct ip6_hdr *ip6, uint8_t *lexthdrsp,
1793     uint32_t mtu, int nxt0, uint32_t id)
1794 {
1795 	VERIFY(NULL != mptr);
1796 	int error = 0;
1797 
1798 	struct mbuf *morig = *mptr;
1799 	struct mbuf *first_mbufp = NULL;
1800 	struct mbuf *last_mbufp = NULL;
1801 
1802 	uint32_t tlen = morig->m_pkthdr.len;
1803 
1804 	/* try to fragment the packet. case 1-b */
1805 	if ((morig->m_pkthdr.csum_flags & CSUM_TSO_IPV6)) {
1806 		/* TSO and fragment aren't compatible */
1807 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
1808 		return EMSGSIZE;
1809 	} else if (mtu < IPV6_MMTU) {
1810 		/* path MTU cannot be less than IPV6_MMTU */
1811 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
1812 		return EMSGSIZE;
1813 	} else if (ip6->ip6_plen == 0) {
1814 		/* jumbo payload cannot be fragmented */
1815 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
1816 		return EMSGSIZE;
1817 	} else {
1818 		uint32_t hlen, off, len;
1819 		struct mbuf **mnext = NULL;
1820 		struct ip6_frag *ip6f;
1821 		u_char nextproto;
1822 
1823 		/*
1824 		 * Too large for the destination or interface;
1825 		 * fragment if possible.
1826 		 * Must be able to put at least 8 bytes per fragment.
1827 		 */
1828 		hlen = unfragpartlen;
1829 		if (mtu > IPV6_MAXPACKET) {
1830 			mtu = IPV6_MAXPACKET;
1831 		}
1832 
1833 		len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1834 		if (len < 8) {
1835 			in6_ifstat_inc(ifp, ifs6_out_fragfail);
1836 			return EMSGSIZE;
1837 		}
1838 
1839 		/*
1840 		 * Change the next header field of the last header in the
1841 		 * unfragmentable part.
1842 		 */
1843 		if (lexthdrsp != NULL) {
1844 			nextproto = *lexthdrsp;
1845 			*lexthdrsp = IPPROTO_FRAGMENT;
1846 		} else {
1847 			nextproto = ip6->ip6_nxt;
1848 			ip6->ip6_nxt = IPPROTO_FRAGMENT;
1849 		}
1850 
1851 		if (morig->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA) {
1852 			in6_delayed_cksum_offset(morig, 0, optlen, nxt0);
1853 		}
1854 
1855 		/*
1856 		 * Loop through length of segment after first fragment,
1857 		 * make new header and copy data of each part and link onto
1858 		 * chain.
1859 		 */
1860 		for (off = hlen; off < tlen; off += len) {
1861 			struct ip6_hdr *new_mhip6;
1862 			struct mbuf *new_m;
1863 			struct mbuf *m_frgpart;
1864 
1865 			MGETHDR(new_m, M_DONTWAIT, MT_HEADER);  /* MAC-OK */
1866 			if (new_m == NULL) {
1867 				error = ENOBUFS;
1868 				ip6stat.ip6s_odropped++;
1869 				break;
1870 			}
1871 			new_m->m_pkthdr.rcvif = NULL;
1872 			new_m->m_flags = morig->m_flags & M_COPYFLAGS;
1873 
1874 			if (first_mbufp != NULL) {
1875 				/* Every pass through loop but first */
1876 				*mnext = new_m;
1877 				last_mbufp = new_m;
1878 			} else {
1879 				/* This is the first element of the fragment chain */
1880 				first_mbufp = new_m;
1881 				last_mbufp = new_m;
1882 			}
1883 			mnext = &new_m->m_nextpkt;
1884 
1885 			new_m->m_data += max_linkhdr;
1886 			new_mhip6 = mtod(new_m, struct ip6_hdr *);
1887 			*new_mhip6 = *ip6;
1888 			new_m->m_len = sizeof(*new_mhip6);
1889 
1890 			error = ip6_insertfraghdr(morig, new_m, hlen, &ip6f);
1891 			if (error) {
1892 				ip6stat.ip6s_odropped++;
1893 				break;
1894 			}
1895 
1896 			ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1897 			if (off + len >= tlen) {
1898 				len = tlen - off;
1899 			} else {
1900 				ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1901 			}
1902 			new_mhip6->ip6_plen = htons((u_short)(len + hlen +
1903 			    sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1904 
1905 			if ((m_frgpart = m_copy(morig, off, len)) == NULL) {
1906 				error = ENOBUFS;
1907 				ip6stat.ip6s_odropped++;
1908 				break;
1909 			}
1910 			m_cat(new_m, m_frgpart);
1911 			new_m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1912 			new_m->m_pkthdr.rcvif = NULL;
1913 
1914 			M_COPY_CLASSIFIER(new_m, morig);
1915 			M_COPY_PFTAG(new_m, morig);
1916 			M_COPY_NECPTAG(new_m, morig);
1917 
1918 			ip6f->ip6f_reserved = 0;
1919 			ip6f->ip6f_ident = id;
1920 			ip6f->ip6f_nxt = nextproto;
1921 			ip6stat.ip6s_ofragments++;
1922 			in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1923 		}
1924 
1925 		if (error) {
1926 			/* free all the fragments created */
1927 			if (first_mbufp != NULL) {
1928 				m_freem_list(first_mbufp);
1929 				first_mbufp = NULL;
1930 			}
1931 			last_mbufp = NULL;
1932 		} else {
1933 			/* successful fragmenting */
1934 			m_freem(morig);
1935 			*mptr = first_mbufp;
1936 			last_mbufp->m_nextpkt = NULL;
1937 			ip6stat.ip6s_fragmented++;
1938 			in6_ifstat_inc(ifp, ifs6_out_fragok);
1939 		}
1940 	}
1941 	return error;
1942 }
1943 
1944 static int
ip6_copyexthdr(struct mbuf ** mp,caddr_t hdr,int hlen)1945 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1946 {
1947 	struct mbuf *m;
1948 
1949 	if (hlen > MCLBYTES) {
1950 		return ENOBUFS; /* XXX */
1951 	}
1952 	MGET(m, M_DONTWAIT, MT_DATA);
1953 	if (m == NULL) {
1954 		return ENOBUFS;
1955 	}
1956 
1957 	if (hlen > MLEN) {
1958 		MCLGET(m, M_DONTWAIT);
1959 		if (!(m->m_flags & M_EXT)) {
1960 			m_free(m);
1961 			return ENOBUFS;
1962 		}
1963 	}
1964 	m->m_len = hlen;
1965 	if (hdr != NULL) {
1966 		bcopy(hdr, mtod(m, caddr_t), hlen);
1967 	}
1968 
1969 	*mp = m;
1970 	return 0;
1971 }
1972 
1973 static void
ip6_out_cksum_stats(int proto,u_int32_t len)1974 ip6_out_cksum_stats(int proto, u_int32_t len)
1975 {
1976 	switch (proto) {
1977 	case IPPROTO_TCP:
1978 		tcp_out6_cksum_stats(len);
1979 		break;
1980 	case IPPROTO_UDP:
1981 		udp_out6_cksum_stats(len);
1982 		break;
1983 	default:
1984 		/* keep only TCP or UDP stats for now */
1985 		break;
1986 	}
1987 }
1988 
1989 /*
1990  * Process a delayed payload checksum calculation (outbound path.)
1991  *
1992  * hoff is the number of bytes beyond the mbuf data pointer which
1993  * points to the IPv6 header.  optlen is the number of bytes, if any,
1994  * between the end of IPv6 header and the beginning of the ULP payload
1995  * header, which represents the extension headers.  If optlen is less
1996  * than zero, this routine will bail when it detects extension headers.
1997  *
1998  * Returns a bitmask representing all the work done in software.
1999  */
2000 uint32_t
in6_finalize_cksum(struct mbuf * m,uint32_t hoff,int32_t optlen,int32_t nxt0,uint32_t csum_flags)2001 in6_finalize_cksum(struct mbuf *m, uint32_t hoff, int32_t optlen,
2002     int32_t nxt0, uint32_t csum_flags)
2003 {
2004 	unsigned char buf[sizeof(struct ip6_hdr)] __attribute__((aligned(8)));
2005 	struct ip6_hdr *ip6;
2006 	uint32_t offset, mlen, hlen, olen, sw_csum;
2007 	uint16_t csum, ulpoff, plen;
2008 	uint8_t nxt;
2009 
2010 	_CASSERT(sizeof(csum) == sizeof(uint16_t));
2011 	VERIFY(m->m_flags & M_PKTHDR);
2012 
2013 	sw_csum = (csum_flags & m->m_pkthdr.csum_flags);
2014 
2015 	if ((sw_csum &= CSUM_DELAY_IPV6_DATA) == 0) {
2016 		goto done;
2017 	}
2018 
2019 	mlen = m->m_pkthdr.len;                         /* total mbuf len */
2020 	hlen = sizeof(*ip6);                            /* IPv6 header len */
2021 
2022 	/* sanity check (need at least IPv6 header) */
2023 	if (mlen < (hoff + hlen)) {
2024 		panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr "
2025 		    "(%u+%u)\n", __func__, m, mlen, hoff, hlen);
2026 		/* NOTREACHED */
2027 	}
2028 
2029 	/*
2030 	 * In case the IPv6 header is not contiguous, or not 32-bit
2031 	 * aligned, copy it to a local buffer.
2032 	 */
2033 	if ((hoff + hlen) > m->m_len ||
2034 	    !IP6_HDR_ALIGNED_P(mtod(m, caddr_t) + hoff)) {
2035 		m_copydata(m, hoff, hlen, (caddr_t)buf);
2036 		ip6 = (struct ip6_hdr *)(void *)buf;
2037 	} else {
2038 		ip6 = (struct ip6_hdr *)(void *)(m->m_data + hoff);
2039 	}
2040 
2041 	nxt = ip6->ip6_nxt;
2042 	plen = ntohs(ip6->ip6_plen);
2043 	if (plen != (mlen - (hoff + hlen))) {
2044 		plen = OSSwapInt16(plen);
2045 		if (plen != (mlen - (hoff + hlen))) {
2046 			/* Don't complain for jumbograms */
2047 			if (plen != 0 || nxt != IPPROTO_HOPOPTS) {
2048 				printf("%s: mbuf 0x%llx proto %d IPv6 "
2049 				    "plen %d (%x) [swapped %d (%x)] doesn't "
2050 				    "match actual packet length; %d is used "
2051 				    "instead\n", __func__,
2052 				    (uint64_t)VM_KERNEL_ADDRPERM(m), nxt,
2053 				    ip6->ip6_plen, ip6->ip6_plen, plen, plen,
2054 				    (mlen - (hoff + hlen)));
2055 			}
2056 			plen = (uint16_t)(mlen - (hoff + hlen));
2057 		}
2058 	}
2059 
2060 	if (optlen < 0) {
2061 		/* next header isn't TCP/UDP and we don't know optlen, bail */
2062 		if (nxt != IPPROTO_TCP && nxt != IPPROTO_UDP) {
2063 			sw_csum = 0;
2064 			goto done;
2065 		}
2066 		olen = 0;
2067 	} else {
2068 		/* caller supplied the original transport number; use it */
2069 		if (nxt0 >= 0) {
2070 			nxt = (uint8_t)nxt0;
2071 		}
2072 		olen = optlen;
2073 	}
2074 
2075 	offset = hoff + hlen + olen;                    /* ULP header */
2076 
2077 	/* sanity check */
2078 	if (mlen < offset) {
2079 		panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr+ext_hdr "
2080 		    "(%u+%u+%u)\n", __func__, m, mlen, hoff, hlen, olen);
2081 		/* NOTREACHED */
2082 	}
2083 
2084 	/*
2085 	 * offset is added to the lower 16-bit value of csum_data,
2086 	 * which is expected to contain the ULP offset; therefore
2087 	 * CSUM_PARTIAL offset adjustment must be undone.
2088 	 */
2089 	if ((m->m_pkthdr.csum_flags & (CSUM_PARTIAL | CSUM_DATA_VALID)) ==
2090 	    (CSUM_PARTIAL | CSUM_DATA_VALID)) {
2091 		/*
2092 		 * Get back the original ULP offset (this will
2093 		 * undo the CSUM_PARTIAL logic in ip6_output.)
2094 		 */
2095 		m->m_pkthdr.csum_data = (m->m_pkthdr.csum_tx_stuff -
2096 		    m->m_pkthdr.csum_tx_start);
2097 	}
2098 
2099 	ulpoff = (m->m_pkthdr.csum_data & 0xffff);      /* ULP csum offset */
2100 
2101 	if (mlen < (ulpoff + sizeof(csum))) {
2102 		panic("%s: mbuf %p pkt len (%u) proto %d invalid ULP "
2103 		    "cksum offset (%u) cksum flags 0x%x\n", __func__,
2104 		    m, mlen, nxt, ulpoff, m->m_pkthdr.csum_flags);
2105 		/* NOTREACHED */
2106 	}
2107 
2108 	csum = inet6_cksum(m, 0, offset, plen - olen);
2109 
2110 	/* Update stats */
2111 	ip6_out_cksum_stats(nxt, plen - olen);
2112 
2113 	/* RFC1122 4.1.3.4 */
2114 	if (csum == 0 &&
2115 	    (m->m_pkthdr.csum_flags & (CSUM_UDPIPV6 | CSUM_ZERO_INVERT))) {
2116 		csum = 0xffff;
2117 	}
2118 
2119 	/* Insert the checksum in the ULP csum field */
2120 	offset += ulpoff;
2121 	if ((offset + sizeof(csum)) > m->m_len) {
2122 		m_copyback(m, offset, sizeof(csum), &csum);
2123 	} else if (IP6_HDR_ALIGNED_P(mtod(m, char *) + hoff)) {
2124 		*(uint16_t *)(void *)(mtod(m, char *) + offset) = csum;
2125 	} else {
2126 		bcopy(&csum, (mtod(m, char *) + offset), sizeof(csum));
2127 	}
2128 	m->m_pkthdr.csum_flags &= ~(CSUM_DELAY_IPV6_DATA | CSUM_DATA_VALID |
2129 	    CSUM_PARTIAL | CSUM_ZERO_INVERT);
2130 
2131 done:
2132 	return sw_csum;
2133 }
2134 
2135 /*
2136  * Insert jumbo payload option.
2137  */
2138 static int
ip6_insert_jumboopt(struct ip6_exthdrs * exthdrs,u_int32_t plen)2139 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
2140 {
2141 	struct mbuf *mopt;
2142 	u_char *optbuf;
2143 	u_int32_t v;
2144 
2145 #define JUMBOOPTLEN     8       /* length of jumbo payload option and padding */
2146 
2147 	/*
2148 	 * If there is no hop-by-hop options header, allocate new one.
2149 	 * If there is one but it doesn't have enough space to store the
2150 	 * jumbo payload option, allocate a cluster to store the whole options.
2151 	 * Otherwise, use it to store the options.
2152 	 */
2153 	if (exthdrs->ip6e_hbh == NULL) {
2154 		MGET(mopt, M_DONTWAIT, MT_DATA);
2155 		if (mopt == NULL) {
2156 			return ENOBUFS;
2157 		}
2158 		mopt->m_len = JUMBOOPTLEN;
2159 		optbuf = mtod(mopt, u_char *);
2160 		optbuf[1] = 0;  /* = ((JUMBOOPTLEN) >> 3) - 1 */
2161 		exthdrs->ip6e_hbh = mopt;
2162 	} else {
2163 		struct ip6_hbh *hbh;
2164 
2165 		mopt = exthdrs->ip6e_hbh;
2166 		if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
2167 			/*
2168 			 * XXX assumption:
2169 			 * - exthdrs->ip6e_hbh is not referenced from places
2170 			 *   other than exthdrs.
2171 			 * - exthdrs->ip6e_hbh is not an mbuf chain.
2172 			 */
2173 			u_int32_t oldoptlen = mopt->m_len;
2174 			struct mbuf *n;
2175 
2176 			/*
2177 			 * XXX: give up if the whole (new) hbh header does
2178 			 * not fit even in an mbuf cluster.
2179 			 */
2180 			if (oldoptlen + JUMBOOPTLEN > MCLBYTES) {
2181 				return ENOBUFS;
2182 			}
2183 
2184 			/*
2185 			 * As a consequence, we must always prepare a cluster
2186 			 * at this point.
2187 			 */
2188 			MGET(n, M_DONTWAIT, MT_DATA);
2189 			if (n != NULL) {
2190 				MCLGET(n, M_DONTWAIT);
2191 				if (!(n->m_flags & M_EXT)) {
2192 					m_freem(n);
2193 					n = NULL;
2194 				}
2195 			}
2196 			if (n == NULL) {
2197 				return ENOBUFS;
2198 			}
2199 			n->m_len = oldoptlen + JUMBOOPTLEN;
2200 			bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
2201 			    oldoptlen);
2202 			optbuf = mtod(n, u_char *) + oldoptlen;
2203 			m_freem(mopt);
2204 			mopt = exthdrs->ip6e_hbh = n;
2205 		} else {
2206 			optbuf = mtod(mopt, u_char *) + mopt->m_len;
2207 			mopt->m_len += JUMBOOPTLEN;
2208 		}
2209 		optbuf[0] = IP6OPT_PADN;
2210 		optbuf[1] = 1;
2211 
2212 		/*
2213 		 * Adjust the header length according to the pad and
2214 		 * the jumbo payload option.
2215 		 */
2216 		hbh = mtod(mopt, struct ip6_hbh *);
2217 		hbh->ip6h_len += (JUMBOOPTLEN >> 3);
2218 	}
2219 
2220 	/* fill in the option. */
2221 	optbuf[2] = IP6OPT_JUMBO;
2222 	optbuf[3] = 4;
2223 	v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
2224 	bcopy(&v, &optbuf[4], sizeof(u_int32_t));
2225 
2226 	/* finally, adjust the packet header length */
2227 	exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
2228 
2229 	return 0;
2230 #undef JUMBOOPTLEN
2231 }
2232 
2233 /*
2234  * Insert fragment header and copy unfragmentable header portions.
2235  */
2236 static int
ip6_insertfraghdr(struct mbuf * m0,struct mbuf * m,int hlen,struct ip6_frag ** frghdrp)2237 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
2238     struct ip6_frag **frghdrp)
2239 {
2240 	struct mbuf *n, *mlast;
2241 
2242 	if (hlen > sizeof(struct ip6_hdr)) {
2243 		n = m_copym(m0, sizeof(struct ip6_hdr),
2244 		    hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
2245 		if (n == NULL) {
2246 			return ENOBUFS;
2247 		}
2248 		m->m_next = n;
2249 	} else {
2250 		n = m;
2251 	}
2252 
2253 	/* Search for the last mbuf of unfragmentable part. */
2254 	for (mlast = n; mlast->m_next; mlast = mlast->m_next) {
2255 		;
2256 	}
2257 
2258 	if (!(mlast->m_flags & M_EXT) &&
2259 	    M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
2260 		/* use the trailing space of the last mbuf for the frag hdr */
2261 		*frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
2262 		    mlast->m_len);
2263 		mlast->m_len += sizeof(struct ip6_frag);
2264 		m->m_pkthdr.len += sizeof(struct ip6_frag);
2265 	} else {
2266 		/* allocate a new mbuf for the fragment header */
2267 		struct mbuf *mfrg;
2268 
2269 		MGET(mfrg, M_DONTWAIT, MT_DATA);
2270 		if (mfrg == NULL) {
2271 			return ENOBUFS;
2272 		}
2273 		mfrg->m_len = sizeof(struct ip6_frag);
2274 		*frghdrp = mtod(mfrg, struct ip6_frag *);
2275 		mlast->m_next = mfrg;
2276 	}
2277 
2278 	return 0;
2279 }
2280 
2281 static int
ip6_getpmtu(struct route_in6 * ro_pmtu,struct route_in6 * ro,struct ifnet * ifp,struct in6_addr * dst,uint32_t dst_ifscope,u_int32_t * mtup)2282 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
2283     struct ifnet *ifp, struct in6_addr *dst, uint32_t dst_ifscope, u_int32_t *mtup)
2284 {
2285 	u_int32_t mtu = 0;
2286 	int error = 0;
2287 
2288 	if (ro_pmtu != ro) {
2289 		/* The first hop and the final destination may differ. */
2290 		struct sockaddr_in6 *sa6_dst = SIN6(&ro_pmtu->ro_dst);
2291 		if (ROUTE_UNUSABLE(ro_pmtu) ||
2292 		    !in6_are_addr_equal_scoped(&sa6_dst->sin6_addr, dst, sa6_dst->sin6_scope_id, dst_ifscope)) {
2293 			ROUTE_RELEASE(ro_pmtu);
2294 		}
2295 
2296 		if (ro_pmtu->ro_rt == NULL) {
2297 			bzero(sa6_dst, sizeof(*sa6_dst));
2298 			sa6_dst->sin6_family = AF_INET6;
2299 			sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
2300 			sa6_dst->sin6_addr = *dst;
2301 
2302 			rtalloc_scoped((struct route *)ro_pmtu,
2303 			    ifp != NULL ? ifp->if_index : IFSCOPE_NONE);
2304 		}
2305 	}
2306 
2307 	if (ro_pmtu->ro_rt != NULL) {
2308 		u_int32_t ifmtu;
2309 
2310 		if (ifp == NULL) {
2311 			ifp = ro_pmtu->ro_rt->rt_ifp;
2312 		}
2313 		/* Access without acquiring nd_ifinfo lock for performance */
2314 		ifmtu = IN6_LINKMTU(ifp);
2315 
2316 		/*
2317 		 * Access rmx_mtu without holding the route entry lock,
2318 		 * for performance; this isn't something that changes
2319 		 * often, so optimize.
2320 		 */
2321 		mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
2322 		if (mtu > ifmtu || mtu == 0) {
2323 			/*
2324 			 * The MTU on the route is larger than the MTU on
2325 			 * the interface!  This shouldn't happen, unless the
2326 			 * MTU of the interface has been changed after the
2327 			 * interface was brought up.  Change the MTU in the
2328 			 * route to match the interface MTU (as long as the
2329 			 * field isn't locked).
2330 			 *
2331 			 * if MTU on the route is 0, we need to fix the MTU.
2332 			 * this case happens with path MTU discovery timeouts.
2333 			 */
2334 			mtu = ifmtu;
2335 			if (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU)) {
2336 				ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
2337 			}
2338 		}
2339 	} else {
2340 		if (ifp) {
2341 			/* Don't hold nd_ifinfo lock for performance */
2342 			mtu = IN6_LINKMTU(ifp);
2343 		} else {
2344 			error = EHOSTUNREACH; /* XXX */
2345 		}
2346 	}
2347 
2348 	*mtup = mtu;
2349 	return error;
2350 }
2351 
2352 /*
2353  * IP6 socket option processing.
2354  */
2355 int
ip6_ctloutput(struct socket * so,struct sockopt * sopt)2356 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
2357 {
2358 	int optdatalen, uproto;
2359 	void *optdata;
2360 	int privileged;
2361 	struct inpcb *in6p = sotoinpcb(so);
2362 	int error = 0, optval = 0;
2363 	int level, op = -1, optname = 0;
2364 	size_t optlen = 0;
2365 	struct proc *p;
2366 	lck_mtx_t *mutex_held = NULL;
2367 
2368 	VERIFY(sopt != NULL);
2369 
2370 	level = sopt->sopt_level;
2371 	op = sopt->sopt_dir;
2372 	optname = sopt->sopt_name;
2373 	optlen = sopt->sopt_valsize;
2374 	p = sopt->sopt_p;
2375 	uproto = (int)SOCK_PROTO(so);
2376 
2377 	privileged = (proc_suser(p) == 0);
2378 
2379 	if (level == IPPROTO_IPV6) {
2380 		boolean_t capture_exthdrstat_in = FALSE;
2381 		switch (op) {
2382 		case SOPT_SET:
2383 			mutex_held = socket_getlock(so, PR_F_WILLUNLOCK);
2384 			/*
2385 			 * Wait if we are in the middle of ip6_output
2386 			 * as we unlocked the socket there and don't
2387 			 * want to overwrite the IP options
2388 			 */
2389 			if (in6p->inp_sndinprog_cnt > 0) {
2390 				in6p->inp_sndingprog_waiters++;
2391 
2392 				while (in6p->inp_sndinprog_cnt > 0) {
2393 					msleep(&in6p->inp_sndinprog_cnt, mutex_held,
2394 					    PSOCK | PCATCH, "inp_sndinprog_cnt",
2395 					    NULL);
2396 				}
2397 				in6p->inp_sndingprog_waiters--;
2398 			}
2399 			switch (optname) {
2400 			case IPV6_2292PKTOPTIONS: {
2401 				struct mbuf *m;
2402 
2403 				error = soopt_getm(sopt, &m);
2404 				if (error != 0) {
2405 					break;
2406 				}
2407 				error = soopt_mcopyin(sopt, m);
2408 				if (error != 0) {
2409 					break;
2410 				}
2411 				error = ip6_pcbopts(&in6p->in6p_outputopts,
2412 				    m, so, sopt);
2413 				m_freem(m);
2414 				break;
2415 			}
2416 
2417 			/*
2418 			 * Use of some Hop-by-Hop options or some
2419 			 * Destination options, might require special
2420 			 * privilege.  That is, normal applications
2421 			 * (without special privilege) might be forbidden
2422 			 * from setting certain options in outgoing packets,
2423 			 * and might never see certain options in received
2424 			 * packets. [RFC 2292 Section 6]
2425 			 * KAME specific note:
2426 			 *  KAME prevents non-privileged users from sending or
2427 			 *  receiving ANY hbh/dst options in order to avoid
2428 			 *  overhead of parsing options in the kernel.
2429 			 */
2430 			case IPV6_RECVHOPOPTS:
2431 			case IPV6_RECVDSTOPTS:
2432 			case IPV6_RECVRTHDRDSTOPTS:
2433 				if (!privileged) {
2434 					break;
2435 				}
2436 				OS_FALLTHROUGH;
2437 			case IPV6_UNICAST_HOPS:
2438 			case IPV6_HOPLIMIT:
2439 			case IPV6_RECVPKTINFO:
2440 			case IPV6_RECVHOPLIMIT:
2441 			case IPV6_RECVRTHDR:
2442 			case IPV6_RECVPATHMTU:
2443 			case IPV6_RECVTCLASS:
2444 			case IPV6_V6ONLY:
2445 			case IPV6_AUTOFLOWLABEL:
2446 				if (optlen != sizeof(int)) {
2447 					error = EINVAL;
2448 					break;
2449 				}
2450 				error = sooptcopyin(sopt, &optval,
2451 				    sizeof(optval), sizeof(optval));
2452 				if (error) {
2453 					break;
2454 				}
2455 
2456 				switch (optname) {
2457 				case IPV6_UNICAST_HOPS:
2458 					if (optval < -1 || optval >= 256) {
2459 						error = EINVAL;
2460 					} else {
2461 						/* -1 = kernel default */
2462 						in6p->in6p_hops = (short)optval;
2463 						if (in6p->inp_vflag &
2464 						    INP_IPV4) {
2465 							in6p->inp_ip_ttl =
2466 							    (uint8_t)optval;
2467 						}
2468 					}
2469 					break;
2470 #define OPTSET(bit) do {                                                \
2471 	if (optval)                                                     \
2472 	        in6p->inp_flags |= (bit);                               \
2473 	else                                                            \
2474 	        in6p->inp_flags &= ~(bit);                              \
2475 } while (0)
2476 
2477 #define OPTSET2292(bit) do {                                            \
2478 	in6p->inp_flags |= IN6P_RFC2292;                                \
2479 	if (optval)                                                     \
2480 	        in6p->inp_flags |= (bit);                               \
2481 	else                                                            \
2482 	        in6p->inp_flags &= ~(bit);                              \
2483 } while (0)
2484 
2485 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
2486 
2487 				case IPV6_RECVPKTINFO:
2488 					/* cannot mix with RFC2292 */
2489 					if (OPTBIT(IN6P_RFC2292)) {
2490 						error = EINVAL;
2491 						break;
2492 					}
2493 					OPTSET(IN6P_PKTINFO);
2494 					break;
2495 
2496 				case IPV6_HOPLIMIT: {
2497 					struct ip6_pktopts **optp;
2498 
2499 					/* cannot mix with RFC2292 */
2500 					if (OPTBIT(IN6P_RFC2292)) {
2501 						error = EINVAL;
2502 						break;
2503 					}
2504 					optp = &in6p->in6p_outputopts;
2505 					error = ip6_pcbopt(IPV6_HOPLIMIT,
2506 					    (u_char *)&optval, sizeof(optval),
2507 					    optp, uproto);
2508 					break;
2509 				}
2510 
2511 				case IPV6_RECVHOPLIMIT:
2512 					/* cannot mix with RFC2292 */
2513 					if (OPTBIT(IN6P_RFC2292)) {
2514 						error = EINVAL;
2515 						break;
2516 					}
2517 					OPTSET(IN6P_HOPLIMIT);
2518 					break;
2519 
2520 				case IPV6_RECVHOPOPTS:
2521 					/* cannot mix with RFC2292 */
2522 					if (OPTBIT(IN6P_RFC2292)) {
2523 						error = EINVAL;
2524 						break;
2525 					}
2526 					OPTSET(IN6P_HOPOPTS);
2527 					capture_exthdrstat_in = TRUE;
2528 					break;
2529 
2530 				case IPV6_RECVDSTOPTS:
2531 					/* cannot mix with RFC2292 */
2532 					if (OPTBIT(IN6P_RFC2292)) {
2533 						error = EINVAL;
2534 						break;
2535 					}
2536 					OPTSET(IN6P_DSTOPTS);
2537 					capture_exthdrstat_in = TRUE;
2538 					break;
2539 
2540 				case IPV6_RECVRTHDRDSTOPTS:
2541 					/* cannot mix with RFC2292 */
2542 					if (OPTBIT(IN6P_RFC2292)) {
2543 						error = EINVAL;
2544 						break;
2545 					}
2546 					OPTSET(IN6P_RTHDRDSTOPTS);
2547 					capture_exthdrstat_in = TRUE;
2548 					break;
2549 
2550 				case IPV6_RECVRTHDR:
2551 					/* cannot mix with RFC2292 */
2552 					if (OPTBIT(IN6P_RFC2292)) {
2553 						error = EINVAL;
2554 						break;
2555 					}
2556 					OPTSET(IN6P_RTHDR);
2557 					capture_exthdrstat_in = TRUE;
2558 					break;
2559 
2560 				case IPV6_RECVPATHMTU:
2561 					/*
2562 					 * We ignore this option for TCP
2563 					 * sockets.
2564 					 * (RFC3542 leaves this case
2565 					 * unspecified.)
2566 					 */
2567 					if (uproto != IPPROTO_TCP) {
2568 						OPTSET(IN6P_MTU);
2569 					}
2570 					break;
2571 
2572 				case IPV6_V6ONLY:
2573 					/*
2574 					 * make setsockopt(IPV6_V6ONLY)
2575 					 * available only prior to bind(2).
2576 					 * see ipng mailing list, Jun 22 2001.
2577 					 */
2578 					if (in6p->inp_lport ||
2579 					    !IN6_IS_ADDR_UNSPECIFIED(
2580 						    &in6p->in6p_laddr)) {
2581 						error = EINVAL;
2582 						break;
2583 					}
2584 					OPTSET(IN6P_IPV6_V6ONLY);
2585 					if (optval) {
2586 						in6p->inp_vflag &= ~INP_IPV4;
2587 					} else {
2588 						in6p->inp_vflag |= INP_IPV4;
2589 					}
2590 					break;
2591 
2592 				case IPV6_RECVTCLASS:
2593 					/* we can mix with RFC2292 */
2594 					OPTSET(IN6P_TCLASS);
2595 					break;
2596 
2597 				case IPV6_AUTOFLOWLABEL:
2598 					OPTSET(IN6P_AUTOFLOWLABEL);
2599 					break;
2600 				}
2601 				break;
2602 
2603 			case IPV6_TCLASS:
2604 			case IPV6_DONTFRAG:
2605 			case IPV6_USE_MIN_MTU:
2606 			case IPV6_PREFER_TEMPADDR: {
2607 				struct ip6_pktopts **optp;
2608 
2609 				if (optlen != sizeof(optval)) {
2610 					error = EINVAL;
2611 					break;
2612 				}
2613 				error = sooptcopyin(sopt, &optval,
2614 				    sizeof(optval), sizeof(optval));
2615 				if (error) {
2616 					break;
2617 				}
2618 
2619 				optp = &in6p->in6p_outputopts;
2620 				error = ip6_pcbopt(optname, (u_char *)&optval,
2621 				    sizeof(optval), optp, uproto);
2622 
2623 				if (optname == IPV6_TCLASS) {
2624 					// Add in the ECN flags
2625 					u_int8_t tos = (in6p->inp_ip_tos & ~IPTOS_ECN_MASK);
2626 					u_int8_t ecn = optval & IPTOS_ECN_MASK;
2627 					in6p->inp_ip_tos = tos | ecn;
2628 				}
2629 				break;
2630 			}
2631 
2632 			case IPV6_2292PKTINFO:
2633 			case IPV6_2292HOPLIMIT:
2634 			case IPV6_2292HOPOPTS:
2635 			case IPV6_2292DSTOPTS:
2636 			case IPV6_2292RTHDR:
2637 				/* RFC 2292 */
2638 				if (optlen != sizeof(int)) {
2639 					error = EINVAL;
2640 					break;
2641 				}
2642 				error = sooptcopyin(sopt, &optval,
2643 				    sizeof(optval), sizeof(optval));
2644 				if (error) {
2645 					break;
2646 				}
2647 				switch (optname) {
2648 				case IPV6_2292PKTINFO:
2649 					OPTSET2292(IN6P_PKTINFO);
2650 					break;
2651 				case IPV6_2292HOPLIMIT:
2652 					OPTSET2292(IN6P_HOPLIMIT);
2653 					break;
2654 				case IPV6_2292HOPOPTS:
2655 					/*
2656 					 * Check super-user privilege.
2657 					 * See comments for IPV6_RECVHOPOPTS.
2658 					 */
2659 					if (!privileged) {
2660 						return EPERM;
2661 					}
2662 					OPTSET2292(IN6P_HOPOPTS);
2663 					capture_exthdrstat_in = TRUE;
2664 					break;
2665 				case IPV6_2292DSTOPTS:
2666 					if (!privileged) {
2667 						return EPERM;
2668 					}
2669 					OPTSET2292(IN6P_DSTOPTS |
2670 					    IN6P_RTHDRDSTOPTS); /* XXX */
2671 					capture_exthdrstat_in = TRUE;
2672 					break;
2673 				case IPV6_2292RTHDR:
2674 					OPTSET2292(IN6P_RTHDR);
2675 					capture_exthdrstat_in = TRUE;
2676 					break;
2677 				}
2678 				break;
2679 
2680 			case IPV6_3542PKTINFO:
2681 			case IPV6_3542HOPOPTS:
2682 			case IPV6_3542RTHDR:
2683 			case IPV6_3542DSTOPTS:
2684 			case IPV6_RTHDRDSTOPTS:
2685 			case IPV6_3542NEXTHOP: {
2686 				struct ip6_pktopts **optp;
2687 				/* new advanced API (RFC3542) */
2688 				struct mbuf *m;
2689 
2690 				/* cannot mix with RFC2292 */
2691 				if (OPTBIT(IN6P_RFC2292)) {
2692 					error = EINVAL;
2693 					break;
2694 				}
2695 				error = soopt_getm(sopt, &m);
2696 				if (error != 0) {
2697 					break;
2698 				}
2699 				error = soopt_mcopyin(sopt, m);
2700 				if (error != 0) {
2701 					break;
2702 				}
2703 
2704 				optp = &in6p->in6p_outputopts;
2705 				error = ip6_pcbopt(optname, mtod(m, u_char *),
2706 				    m->m_len, optp, uproto);
2707 				m_freem(m);
2708 				break;
2709 			}
2710 #undef OPTSET
2711 			case IPV6_MULTICAST_IF:
2712 			case IPV6_MULTICAST_HOPS:
2713 			case IPV6_MULTICAST_LOOP:
2714 			case IPV6_JOIN_GROUP:
2715 			case IPV6_LEAVE_GROUP:
2716 			case IPV6_MSFILTER:
2717 			case MCAST_BLOCK_SOURCE:
2718 			case MCAST_UNBLOCK_SOURCE:
2719 			case MCAST_JOIN_GROUP:
2720 			case MCAST_LEAVE_GROUP:
2721 			case MCAST_JOIN_SOURCE_GROUP:
2722 			case MCAST_LEAVE_SOURCE_GROUP:
2723 				error = ip6_setmoptions(in6p, sopt);
2724 				break;
2725 
2726 			case IPV6_PORTRANGE:
2727 				error = sooptcopyin(sopt, &optval,
2728 				    sizeof(optval), sizeof(optval));
2729 				if (error) {
2730 					break;
2731 				}
2732 
2733 				switch (optval) {
2734 				case IPV6_PORTRANGE_DEFAULT:
2735 					in6p->inp_flags &= ~(INP_LOWPORT);
2736 					in6p->inp_flags &= ~(INP_HIGHPORT);
2737 					break;
2738 
2739 				case IPV6_PORTRANGE_HIGH:
2740 					in6p->inp_flags &= ~(INP_LOWPORT);
2741 					in6p->inp_flags |= INP_HIGHPORT;
2742 					break;
2743 
2744 				case IPV6_PORTRANGE_LOW:
2745 					in6p->inp_flags &= ~(INP_HIGHPORT);
2746 					in6p->inp_flags |= INP_LOWPORT;
2747 					break;
2748 
2749 				default:
2750 					error = EINVAL;
2751 					break;
2752 				}
2753 				break;
2754 #if IPSEC
2755 			case IPV6_IPSEC_POLICY: {
2756 				caddr_t req = NULL;
2757 				size_t len = 0;
2758 				struct mbuf *m;
2759 
2760 				if ((error = soopt_getm(sopt, &m)) != 0) {
2761 					break;
2762 				}
2763 				if ((error = soopt_mcopyin(sopt, m)) != 0) {
2764 					break;
2765 				}
2766 
2767 				req = mtod(m, caddr_t);
2768 				len = m->m_len;
2769 				error = ipsec6_set_policy(in6p, optname, req,
2770 				    len, privileged);
2771 				m_freem(m);
2772 				break;
2773 			}
2774 #endif /* IPSEC */
2775 			/*
2776 			 * IPv6 variant of IP_BOUND_IF; for details see
2777 			 * comments on IP_BOUND_IF in ip_ctloutput().
2778 			 */
2779 			case IPV6_BOUND_IF:
2780 				/* This option is settable only on IPv6 */
2781 				if (!(in6p->inp_vflag & INP_IPV6)) {
2782 					error = EINVAL;
2783 					break;
2784 				}
2785 
2786 				error = sooptcopyin(sopt, &optval,
2787 				    sizeof(optval), sizeof(optval));
2788 
2789 				if (error) {
2790 					break;
2791 				}
2792 
2793 				error = inp_bindif(in6p, optval, NULL);
2794 				break;
2795 
2796 			case IPV6_NO_IFT_CELLULAR:
2797 				/* This option is settable only for IPv6 */
2798 				if (!(in6p->inp_vflag & INP_IPV6)) {
2799 					error = EINVAL;
2800 					break;
2801 				}
2802 
2803 				error = sooptcopyin(sopt, &optval,
2804 				    sizeof(optval), sizeof(optval));
2805 
2806 				if (error) {
2807 					break;
2808 				}
2809 
2810 				/* once set, it cannot be unset */
2811 				if (!optval && INP_NO_CELLULAR(in6p)) {
2812 					error = EINVAL;
2813 					break;
2814 				}
2815 
2816 				error = so_set_restrictions(so,
2817 				    SO_RESTRICT_DENY_CELLULAR);
2818 				break;
2819 
2820 			case IPV6_OUT_IF:
2821 				/* This option is not settable */
2822 				error = EINVAL;
2823 				break;
2824 
2825 			default:
2826 				error = ENOPROTOOPT;
2827 				break;
2828 			}
2829 			if (capture_exthdrstat_in) {
2830 				if (uproto == IPPROTO_TCP) {
2831 					INC_ATOMIC_INT64_LIM(net_api_stats.nas_sock_inet6_stream_exthdr_in);
2832 				} else if (uproto == IPPROTO_UDP) {
2833 					INC_ATOMIC_INT64_LIM(net_api_stats.nas_sock_inet6_dgram_exthdr_in);
2834 				}
2835 			}
2836 			break;
2837 
2838 		case SOPT_GET:
2839 			switch (optname) {
2840 			case IPV6_2292PKTOPTIONS:
2841 				/*
2842 				 * RFC3542 (effectively) deprecated the
2843 				 * semantics of the 2292-style pktoptions.
2844 				 * Since it was not reliable in nature (i.e.,
2845 				 * applications had to expect the lack of some
2846 				 * information after all), it would make sense
2847 				 * to simplify this part by always returning
2848 				 * empty data.
2849 				 */
2850 				sopt->sopt_valsize = 0;
2851 				break;
2852 
2853 			case IPV6_RECVHOPOPTS:
2854 			case IPV6_RECVDSTOPTS:
2855 			case IPV6_RECVRTHDRDSTOPTS:
2856 			case IPV6_UNICAST_HOPS:
2857 			case IPV6_RECVPKTINFO:
2858 			case IPV6_RECVHOPLIMIT:
2859 			case IPV6_RECVRTHDR:
2860 			case IPV6_RECVPATHMTU:
2861 			case IPV6_V6ONLY:
2862 			case IPV6_PORTRANGE:
2863 			case IPV6_RECVTCLASS:
2864 			case IPV6_AUTOFLOWLABEL:
2865 				switch (optname) {
2866 				case IPV6_RECVHOPOPTS:
2867 					optval = OPTBIT(IN6P_HOPOPTS);
2868 					break;
2869 
2870 				case IPV6_RECVDSTOPTS:
2871 					optval = OPTBIT(IN6P_DSTOPTS);
2872 					break;
2873 
2874 				case IPV6_RECVRTHDRDSTOPTS:
2875 					optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2876 					break;
2877 
2878 				case IPV6_UNICAST_HOPS:
2879 					optval = in6p->in6p_hops;
2880 					break;
2881 
2882 				case IPV6_RECVPKTINFO:
2883 					optval = OPTBIT(IN6P_PKTINFO);
2884 					break;
2885 
2886 				case IPV6_RECVHOPLIMIT:
2887 					optval = OPTBIT(IN6P_HOPLIMIT);
2888 					break;
2889 
2890 				case IPV6_RECVRTHDR:
2891 					optval = OPTBIT(IN6P_RTHDR);
2892 					break;
2893 
2894 				case IPV6_RECVPATHMTU:
2895 					optval = OPTBIT(IN6P_MTU);
2896 					break;
2897 
2898 				case IPV6_V6ONLY:
2899 					optval = OPTBIT(IN6P_IPV6_V6ONLY);
2900 					break;
2901 
2902 				case IPV6_PORTRANGE: {
2903 					int flags;
2904 					flags = in6p->inp_flags;
2905 					if (flags & INP_HIGHPORT) {
2906 						optval = IPV6_PORTRANGE_HIGH;
2907 					} else if (flags & INP_LOWPORT) {
2908 						optval = IPV6_PORTRANGE_LOW;
2909 					} else {
2910 						optval = 0;
2911 					}
2912 					break;
2913 				}
2914 				case IPV6_RECVTCLASS:
2915 					optval = OPTBIT(IN6P_TCLASS);
2916 					break;
2917 
2918 				case IPV6_AUTOFLOWLABEL:
2919 					optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2920 					break;
2921 				}
2922 				if (error) {
2923 					break;
2924 				}
2925 				error = sooptcopyout(sopt, &optval,
2926 				    sizeof(optval));
2927 				break;
2928 
2929 			case IPV6_PATHMTU: {
2930 				u_int32_t pmtu = 0;
2931 				struct ip6_mtuinfo mtuinfo;
2932 				struct route_in6 sro;
2933 
2934 				bzero(&sro, sizeof(sro));
2935 
2936 				if (!(so->so_state & SS_ISCONNECTED)) {
2937 					return ENOTCONN;
2938 				}
2939 				/*
2940 				 * XXX: we dot not consider the case of source
2941 				 * routing, or optional information to specify
2942 				 * the outgoing interface.
2943 				 */
2944 				error = ip6_getpmtu(&sro, NULL, NULL,
2945 				    &in6p->in6p_faddr, in6p->inp_fifscope, &pmtu);
2946 				ROUTE_RELEASE(&sro);
2947 				if (error) {
2948 					break;
2949 				}
2950 				if (pmtu > IPV6_MAXPACKET) {
2951 					pmtu = IPV6_MAXPACKET;
2952 				}
2953 
2954 				bzero(&mtuinfo, sizeof(mtuinfo));
2955 				mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2956 				optdata = (void *)&mtuinfo;
2957 				optdatalen = sizeof(mtuinfo);
2958 				error = sooptcopyout(sopt, optdata,
2959 				    optdatalen);
2960 				break;
2961 			}
2962 
2963 			case IPV6_2292PKTINFO:
2964 			case IPV6_2292HOPLIMIT:
2965 			case IPV6_2292HOPOPTS:
2966 			case IPV6_2292RTHDR:
2967 			case IPV6_2292DSTOPTS:
2968 				switch (optname) {
2969 				case IPV6_2292PKTINFO:
2970 					optval = OPTBIT(IN6P_PKTINFO);
2971 					break;
2972 				case IPV6_2292HOPLIMIT:
2973 					optval = OPTBIT(IN6P_HOPLIMIT);
2974 					break;
2975 				case IPV6_2292HOPOPTS:
2976 					optval = OPTBIT(IN6P_HOPOPTS);
2977 					break;
2978 				case IPV6_2292RTHDR:
2979 					optval = OPTBIT(IN6P_RTHDR);
2980 					break;
2981 				case IPV6_2292DSTOPTS:
2982 					optval = OPTBIT(IN6P_DSTOPTS |
2983 					    IN6P_RTHDRDSTOPTS);
2984 					break;
2985 				}
2986 				error = sooptcopyout(sopt, &optval,
2987 				    sizeof(optval));
2988 				break;
2989 
2990 			case IPV6_PKTINFO:
2991 			case IPV6_HOPOPTS:
2992 			case IPV6_RTHDR:
2993 			case IPV6_DSTOPTS:
2994 			case IPV6_RTHDRDSTOPTS:
2995 			case IPV6_NEXTHOP:
2996 			case IPV6_TCLASS:
2997 			case IPV6_DONTFRAG:
2998 			case IPV6_USE_MIN_MTU:
2999 			case IPV6_PREFER_TEMPADDR:
3000 				error = ip6_getpcbopt(in6p->in6p_outputopts,
3001 				    optname, sopt);
3002 				break;
3003 
3004 			case IPV6_MULTICAST_IF:
3005 			case IPV6_MULTICAST_HOPS:
3006 			case IPV6_MULTICAST_LOOP:
3007 			case IPV6_MSFILTER:
3008 				error = ip6_getmoptions(in6p, sopt);
3009 				break;
3010 #if IPSEC
3011 			case IPV6_IPSEC_POLICY: {
3012 				error = 0; /* This option is no longer supported */
3013 				break;
3014 			}
3015 #endif /* IPSEC */
3016 			case IPV6_BOUND_IF:
3017 				if (in6p->inp_flags & INP_BOUND_IF) {
3018 					optval = in6p->inp_boundifp->if_index;
3019 				}
3020 				error = sooptcopyout(sopt, &optval,
3021 				    sizeof(optval));
3022 				break;
3023 
3024 			case IPV6_NO_IFT_CELLULAR:
3025 				optval = INP_NO_CELLULAR(in6p) ? 1 : 0;
3026 				error = sooptcopyout(sopt, &optval,
3027 				    sizeof(optval));
3028 				break;
3029 
3030 			case IPV6_OUT_IF:
3031 				optval = (in6p->in6p_last_outifp != NULL) ?
3032 				    in6p->in6p_last_outifp->if_index : 0;
3033 				error = sooptcopyout(sopt, &optval,
3034 				    sizeof(optval));
3035 				break;
3036 
3037 			default:
3038 				error = ENOPROTOOPT;
3039 				break;
3040 			}
3041 			break;
3042 		}
3043 	} else {
3044 		error = EINVAL;
3045 	}
3046 	return error;
3047 }
3048 
3049 int
ip6_raw_ctloutput(struct socket * so,struct sockopt * sopt)3050 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
3051 {
3052 	int error = 0, optval;
3053 	size_t optlen;
3054 	const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
3055 	struct inpcb *in6p = sotoinpcb(so);
3056 	int level, op, optname;
3057 
3058 	level = sopt->sopt_level;
3059 	op = sopt->sopt_dir;
3060 	optname = sopt->sopt_name;
3061 	optlen = sopt->sopt_valsize;
3062 
3063 	if (level != IPPROTO_IPV6) {
3064 		return EINVAL;
3065 	}
3066 
3067 	switch (optname) {
3068 	case IPV6_CHECKSUM:
3069 		/*
3070 		 * For ICMPv6 sockets, no modification allowed for checksum
3071 		 * offset, permit "no change" values to help existing apps.
3072 		 *
3073 		 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
3074 		 * for an ICMPv6 socket will fail."
3075 		 * The current behavior does not meet RFC3542.
3076 		 */
3077 		switch (op) {
3078 		case SOPT_SET:
3079 			if (optlen != sizeof(int)) {
3080 				error = EINVAL;
3081 				break;
3082 			}
3083 			error = sooptcopyin(sopt, &optval, sizeof(optval),
3084 			    sizeof(optval));
3085 			if (error) {
3086 				break;
3087 			}
3088 			if ((optval % 2) != 0) {
3089 				/* the API assumes even offset values */
3090 				error = EINVAL;
3091 			} else if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
3092 				if (optval != icmp6off) {
3093 					error = EINVAL;
3094 				}
3095 			} else {
3096 				in6p->in6p_cksum = optval;
3097 			}
3098 			break;
3099 
3100 		case SOPT_GET:
3101 			if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
3102 				optval = icmp6off;
3103 			} else {
3104 				optval = in6p->in6p_cksum;
3105 			}
3106 
3107 			error = sooptcopyout(sopt, &optval, sizeof(optval));
3108 			break;
3109 
3110 		default:
3111 			error = EINVAL;
3112 			break;
3113 		}
3114 		break;
3115 
3116 	default:
3117 		error = ENOPROTOOPT;
3118 		break;
3119 	}
3120 
3121 	return error;
3122 }
3123 
3124 /*
3125  * Set up IP6 options in pcb for insertion in output packets or
3126  * specifying behavior of outgoing packets.
3127  */
3128 static int
ip6_pcbopts(struct ip6_pktopts ** pktopt,struct mbuf * m,struct socket * so,struct sockopt * sopt)3129 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, struct socket *so,
3130     struct sockopt *sopt)
3131 {
3132 #pragma unused(sopt)
3133 	struct ip6_pktopts *opt = *pktopt;
3134 	int error = 0;
3135 
3136 	/* turn off any old options. */
3137 	if (opt != NULL) {
3138 #if DIAGNOSTIC
3139 		if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
3140 		    opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
3141 		    opt->ip6po_rhinfo.ip6po_rhi_rthdr) {
3142 			printf("%s: all specified options are cleared.\n",
3143 			    __func__);
3144 		}
3145 #endif
3146 		ip6_clearpktopts(opt, -1);
3147 	} else {
3148 		opt = kalloc_type(struct ip6_pktopts, Z_WAITOK | Z_NOFAIL);
3149 	}
3150 	*pktopt = NULL;
3151 
3152 	if (m == NULL || m->m_len == 0) {
3153 		/*
3154 		 * Only turning off any previous options, regardless of
3155 		 * whether the opt is just created or given.
3156 		 */
3157 		if (opt != NULL) {
3158 			kfree_type(struct ip6_pktopts, opt);
3159 		}
3160 		return 0;
3161 	}
3162 
3163 	/*  set options specified by user. */
3164 	if ((error = ip6_setpktopts(m, opt, NULL, SOCK_PROTO(so))) != 0) {
3165 		ip6_clearpktopts(opt, -1); /* XXX: discard all options */
3166 		kfree_type(struct ip6_pktopts, opt);
3167 		return error;
3168 	}
3169 	*pktopt = opt;
3170 	return 0;
3171 }
3172 
3173 /*
3174  * initialize ip6_pktopts.  beware that there are non-zero default values in
3175  * the struct.
3176  */
3177 void
ip6_initpktopts(struct ip6_pktopts * opt)3178 ip6_initpktopts(struct ip6_pktopts *opt)
3179 {
3180 	bzero(opt, sizeof(*opt));
3181 	opt->ip6po_hlim = -1;   /* -1 means default hop limit */
3182 	opt->ip6po_tclass = -1; /* -1 means default traffic class */
3183 	opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
3184 	opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
3185 }
3186 
3187 static int
ip6_pcbopt(int optname,u_char * buf,int len,struct ip6_pktopts ** pktopt,int uproto)3188 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
3189     int uproto)
3190 {
3191 	struct ip6_pktopts *opt;
3192 
3193 	opt = *pktopt;
3194 	if (opt == NULL) {
3195 		opt = kalloc_type(struct ip6_pktopts, Z_WAITOK | Z_NOFAIL);
3196 		ip6_initpktopts(opt);
3197 		*pktopt = opt;
3198 	}
3199 
3200 	return ip6_setpktopt(optname, buf, len, opt, 1, 0, uproto);
3201 }
3202 
3203 static int
ip6_getpcbopt(struct ip6_pktopts * pktopt,int optname,struct sockopt * sopt)3204 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
3205 {
3206 	void *optdata = NULL;
3207 	int optdatalen = 0;
3208 	struct ip6_ext *ip6e;
3209 	struct in6_pktinfo null_pktinfo;
3210 	int deftclass = 0, on;
3211 	int defminmtu = IP6PO_MINMTU_MCASTONLY;
3212 	int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
3213 
3214 
3215 	switch (optname) {
3216 	case IPV6_PKTINFO:
3217 		if (pktopt && pktopt->ip6po_pktinfo) {
3218 			optdata = (void *)pktopt->ip6po_pktinfo;
3219 		} else {
3220 			/* XXX: we don't have to do this every time... */
3221 			bzero(&null_pktinfo, sizeof(null_pktinfo));
3222 			optdata = (void *)&null_pktinfo;
3223 		}
3224 		optdatalen = sizeof(struct in6_pktinfo);
3225 		break;
3226 
3227 	case IPV6_TCLASS:
3228 		if (pktopt && pktopt->ip6po_tclass >= 0) {
3229 			optdata = (void *)&pktopt->ip6po_tclass;
3230 		} else {
3231 			optdata = (void *)&deftclass;
3232 		}
3233 		optdatalen = sizeof(int);
3234 		break;
3235 
3236 	case IPV6_HOPOPTS:
3237 		if (pktopt && pktopt->ip6po_hbh) {
3238 			optdata = (void *)pktopt->ip6po_hbh;
3239 			ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
3240 			optdatalen = (ip6e->ip6e_len + 1) << 3;
3241 		}
3242 		break;
3243 
3244 	case IPV6_RTHDR:
3245 		if (pktopt && pktopt->ip6po_rthdr) {
3246 			optdata = (void *)pktopt->ip6po_rthdr;
3247 			ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
3248 			optdatalen = (ip6e->ip6e_len + 1) << 3;
3249 		}
3250 		break;
3251 
3252 	case IPV6_RTHDRDSTOPTS:
3253 		if (pktopt && pktopt->ip6po_dest1) {
3254 			optdata = (void *)pktopt->ip6po_dest1;
3255 			ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
3256 			optdatalen = (ip6e->ip6e_len + 1) << 3;
3257 		}
3258 		break;
3259 
3260 	case IPV6_DSTOPTS:
3261 		if (pktopt && pktopt->ip6po_dest2) {
3262 			optdata = (void *)pktopt->ip6po_dest2;
3263 			ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
3264 			optdatalen = (ip6e->ip6e_len + 1) << 3;
3265 		}
3266 		break;
3267 
3268 	case IPV6_NEXTHOP:
3269 		if (pktopt && pktopt->ip6po_nexthop) {
3270 			optdata = (void *)pktopt->ip6po_nexthop;
3271 			optdatalen = pktopt->ip6po_nexthop->sa_len;
3272 		}
3273 		break;
3274 
3275 	case IPV6_USE_MIN_MTU:
3276 		if (pktopt) {
3277 			optdata = (void *)&pktopt->ip6po_minmtu;
3278 		} else {
3279 			optdata = (void *)&defminmtu;
3280 		}
3281 		optdatalen = sizeof(int);
3282 		break;
3283 
3284 	case IPV6_DONTFRAG:
3285 		if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) {
3286 			on = 1;
3287 		} else {
3288 			on = 0;
3289 		}
3290 		optdata = (void *)&on;
3291 		optdatalen = sizeof(on);
3292 		break;
3293 
3294 	case IPV6_PREFER_TEMPADDR:
3295 		if (pktopt) {
3296 			optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
3297 		} else {
3298 			optdata = (void *)&defpreftemp;
3299 		}
3300 		optdatalen = sizeof(int);
3301 		break;
3302 
3303 	default:                /* should not happen */
3304 #ifdef DIAGNOSTIC
3305 		panic("ip6_getpcbopt: unexpected option");
3306 #endif
3307 		return ENOPROTOOPT;
3308 	}
3309 
3310 	return sooptcopyout(sopt, optdata, optdatalen);
3311 }
3312 
3313 void
ip6_clearpktopts(struct ip6_pktopts * pktopt,int optname)3314 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
3315 {
3316 	if (pktopt == NULL) {
3317 		return;
3318 	}
3319 
3320 	if (optname == -1 || optname == IPV6_PKTINFO) {
3321 		if (pktopt->ip6po_pktinfo) {
3322 			kfree_type(struct in6_pktinfo, pktopt->ip6po_pktinfo);
3323 		}
3324 		pktopt->ip6po_pktinfo = NULL;
3325 	}
3326 	if (optname == -1 || optname == IPV6_HOPLIMIT) {
3327 		pktopt->ip6po_hlim = -1;
3328 	}
3329 	if (optname == -1 || optname == IPV6_TCLASS) {
3330 		pktopt->ip6po_tclass = -1;
3331 	}
3332 	if (optname == -1 || optname == IPV6_NEXTHOP) {
3333 		ROUTE_RELEASE(&pktopt->ip6po_nextroute);
3334 		if (pktopt->ip6po_nexthop) {
3335 			kfree_data_addr(pktopt->ip6po_nexthop);
3336 		}
3337 		pktopt->ip6po_nexthop = NULL;
3338 	}
3339 	if (optname == -1 || optname == IPV6_HOPOPTS) {
3340 		if (pktopt->ip6po_hbh) {
3341 			kfree_data_addr(pktopt->ip6po_hbh);
3342 		}
3343 		pktopt->ip6po_hbh = NULL;
3344 	}
3345 	if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
3346 		if (pktopt->ip6po_dest1) {
3347 			kfree_data_addr(pktopt->ip6po_dest1);
3348 		}
3349 		pktopt->ip6po_dest1 = NULL;
3350 	}
3351 	if (optname == -1 || optname == IPV6_RTHDR) {
3352 		if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) {
3353 			kfree_data_addr(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr);
3354 		}
3355 		pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
3356 		ROUTE_RELEASE(&pktopt->ip6po_route);
3357 	}
3358 	if (optname == -1 || optname == IPV6_DSTOPTS) {
3359 		if (pktopt->ip6po_dest2) {
3360 			kfree_data_addr(pktopt->ip6po_dest2);
3361 		}
3362 		pktopt->ip6po_dest2 = NULL;
3363 	}
3364 }
3365 
3366 #define PKTOPT_EXTHDRCPY(type) do {                                 \
3367 	if (src->type) {                                                \
3368 	        int hlen =                                              \
3369 	            (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3; \
3370 	        dst->type = kalloc_data(hlen, canwait);                 \
3371 	        if (dst->type == NULL && canwait == Z_NOWAIT)           \
3372 	            goto bad;                                           \
3373 	        bcopy(src->type, dst->type, hlen);                      \
3374 	}                                                               \
3375 } while (0)
3376 
3377 static int
copypktopts(struct ip6_pktopts * dst,struct ip6_pktopts * src,zalloc_flags_t canwait)3378 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, zalloc_flags_t canwait)
3379 {
3380 	if (dst == NULL || src == NULL) {
3381 		printf("copypktopts: invalid argument\n");
3382 		return EINVAL;
3383 	}
3384 
3385 	dst->ip6po_hlim = src->ip6po_hlim;
3386 	dst->ip6po_tclass = src->ip6po_tclass;
3387 	dst->ip6po_flags = src->ip6po_flags;
3388 	if (src->ip6po_pktinfo) {
3389 		dst->ip6po_pktinfo = kalloc_type(struct in6_pktinfo, canwait);
3390 		if (dst->ip6po_pktinfo == NULL && canwait == Z_NOWAIT) {
3391 			goto bad;
3392 		}
3393 		*dst->ip6po_pktinfo = *src->ip6po_pktinfo;
3394 	}
3395 	if (src->ip6po_nexthop) {
3396 		dst->ip6po_nexthop = kalloc_data(src->ip6po_nexthop->sa_len, canwait);
3397 		if (dst->ip6po_nexthop == NULL && canwait == Z_NOWAIT) {
3398 			goto bad;
3399 		}
3400 		bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
3401 		    src->ip6po_nexthop->sa_len);
3402 	}
3403 	PKTOPT_EXTHDRCPY(ip6po_hbh);
3404 	PKTOPT_EXTHDRCPY(ip6po_dest1);
3405 	PKTOPT_EXTHDRCPY(ip6po_dest2);
3406 	PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
3407 	return 0;
3408 
3409 bad:
3410 	ip6_clearpktopts(dst, -1);
3411 	return ENOBUFS;
3412 }
3413 #undef PKTOPT_EXTHDRCPY
3414 
3415 struct ip6_pktopts *
ip6_copypktopts(struct ip6_pktopts * src,zalloc_flags_t canwait)3416 ip6_copypktopts(struct ip6_pktopts *src, zalloc_flags_t canwait)
3417 {
3418 	int error;
3419 	struct ip6_pktopts *dst;
3420 
3421 	dst = kalloc_type(struct ip6_pktopts, canwait);
3422 	if (dst == NULL) {
3423 		return NULL;
3424 	}
3425 	ip6_initpktopts(dst);
3426 
3427 	if ((error = copypktopts(dst, src, canwait)) != 0) {
3428 		kfree_type(struct ip6_pktopts, dst);
3429 		return NULL;
3430 	}
3431 
3432 	return dst;
3433 }
3434 
3435 void
ip6_freepcbopts(struct ip6_pktopts * pktopt)3436 ip6_freepcbopts(struct ip6_pktopts *pktopt)
3437 {
3438 	if (pktopt == NULL) {
3439 		return;
3440 	}
3441 
3442 	ip6_clearpktopts(pktopt, -1);
3443 
3444 	kfree_type(struct ip6_pktopts, pktopt);
3445 }
3446 
3447 void
ip6_moptions_init(void)3448 ip6_moptions_init(void)
3449 {
3450 	PE_parse_boot_argn("ifa_debug", &im6o_debug, sizeof(im6o_debug));
3451 
3452 	vm_size_t im6o_size = (im6o_debug == 0) ? sizeof(struct ip6_moptions) :
3453 	    sizeof(struct ip6_moptions_dbg);
3454 
3455 	im6o_zone = zone_create(IM6O_ZONE_NAME, im6o_size, ZC_ZFREE_CLEARMEM);
3456 }
3457 
3458 void
im6o_addref(struct ip6_moptions * im6o,int locked)3459 im6o_addref(struct ip6_moptions *im6o, int locked)
3460 {
3461 	if (!locked) {
3462 		IM6O_LOCK(im6o);
3463 	} else {
3464 		IM6O_LOCK_ASSERT_HELD(im6o);
3465 	}
3466 
3467 	if (++im6o->im6o_refcnt == 0) {
3468 		panic("%s: im6o %p wraparound refcnt", __func__, im6o);
3469 		/* NOTREACHED */
3470 	} else if (im6o->im6o_trace != NULL) {
3471 		(*im6o->im6o_trace)(im6o, TRUE);
3472 	}
3473 
3474 	if (!locked) {
3475 		IM6O_UNLOCK(im6o);
3476 	}
3477 }
3478 
3479 void
im6o_remref(struct ip6_moptions * im6o)3480 im6o_remref(struct ip6_moptions *im6o)
3481 {
3482 	int i;
3483 
3484 	IM6O_LOCK(im6o);
3485 	if (im6o->im6o_refcnt == 0) {
3486 		panic("%s: im6o %p negative refcnt", __func__, im6o);
3487 		/* NOTREACHED */
3488 	} else if (im6o->im6o_trace != NULL) {
3489 		(*im6o->im6o_trace)(im6o, FALSE);
3490 	}
3491 
3492 	--im6o->im6o_refcnt;
3493 	if (im6o->im6o_refcnt > 0) {
3494 		IM6O_UNLOCK(im6o);
3495 		return;
3496 	}
3497 
3498 	for (i = 0; i < im6o->im6o_num_memberships; ++i) {
3499 		struct in6_mfilter *imf;
3500 
3501 		imf = im6o->im6o_mfilters ? &im6o->im6o_mfilters[i] : NULL;
3502 		if (imf != NULL) {
3503 			im6f_leave(imf);
3504 		}
3505 
3506 		(void) in6_mc_leave(im6o->im6o_membership[i], imf);
3507 
3508 		if (imf != NULL) {
3509 			im6f_purge(imf);
3510 		}
3511 
3512 		IN6M_REMREF(im6o->im6o_membership[i]);
3513 		im6o->im6o_membership[i] = NULL;
3514 	}
3515 	im6o->im6o_num_memberships = 0;
3516 	IM6O_UNLOCK(im6o);
3517 
3518 	kfree_type(struct in6_multi *, im6o->im6o_max_memberships, im6o->im6o_membership);
3519 	kfree_type(struct in6_mfilter, im6o->im6o_max_memberships, im6o->im6o_mfilters);
3520 	lck_mtx_destroy(&im6o->im6o_lock, &ifa_mtx_grp);
3521 
3522 	if (!(im6o->im6o_debug & IFD_ALLOC)) {
3523 		panic("%s: im6o %p cannot be freed", __func__, im6o);
3524 		/* NOTREACHED */
3525 	}
3526 	zfree(im6o_zone, im6o);
3527 }
3528 
3529 static void
im6o_trace(struct ip6_moptions * im6o,int refhold)3530 im6o_trace(struct ip6_moptions *im6o, int refhold)
3531 {
3532 	struct ip6_moptions_dbg *im6o_dbg = (struct ip6_moptions_dbg *)im6o;
3533 	ctrace_t *tr;
3534 	u_int32_t idx;
3535 	u_int16_t *cnt;
3536 
3537 	if (!(im6o->im6o_debug & IFD_DEBUG)) {
3538 		panic("%s: im6o %p has no debug structure", __func__, im6o);
3539 		/* NOTREACHED */
3540 	}
3541 	if (refhold) {
3542 		cnt = &im6o_dbg->im6o_refhold_cnt;
3543 		tr = im6o_dbg->im6o_refhold;
3544 	} else {
3545 		cnt = &im6o_dbg->im6o_refrele_cnt;
3546 		tr = im6o_dbg->im6o_refrele;
3547 	}
3548 
3549 	idx = atomic_add_16_ov(cnt, 1) % IM6O_TRACE_HIST_SIZE;
3550 	ctrace_record(&tr[idx]);
3551 }
3552 
3553 struct ip6_moptions *
ip6_allocmoptions(zalloc_flags_t how)3554 ip6_allocmoptions(zalloc_flags_t how)
3555 {
3556 	struct ip6_moptions *im6o;
3557 
3558 	im6o = zalloc_flags(im6o_zone, how | Z_ZERO);
3559 	if (im6o != NULL) {
3560 		lck_mtx_init(&im6o->im6o_lock, &ifa_mtx_grp, &ifa_mtx_attr);
3561 		im6o->im6o_debug |= IFD_ALLOC;
3562 		if (im6o_debug != 0) {
3563 			im6o->im6o_debug |= IFD_DEBUG;
3564 			im6o->im6o_trace = im6o_trace;
3565 		}
3566 		IM6O_ADDREF(im6o);
3567 	}
3568 
3569 	return im6o;
3570 }
3571 
3572 /*
3573  * Set IPv6 outgoing packet options based on advanced API.
3574  */
3575 int
ip6_setpktopts(struct mbuf * control,struct ip6_pktopts * opt,struct ip6_pktopts * stickyopt,int uproto)3576 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
3577     struct ip6_pktopts *stickyopt, int uproto)
3578 {
3579 	struct cmsghdr *cm = NULL;
3580 
3581 	if (control == NULL || opt == NULL) {
3582 		return EINVAL;
3583 	}
3584 
3585 	ip6_initpktopts(opt);
3586 	if (stickyopt) {
3587 		int error;
3588 
3589 		/*
3590 		 * If stickyopt is provided, make a local copy of the options
3591 		 * for this particular packet, then override them by ancillary
3592 		 * objects.
3593 		 * XXX: copypktopts() does not copy the cached route to a next
3594 		 * hop (if any).  This is not very good in terms of efficiency,
3595 		 * but we can allow this since this option should be rarely
3596 		 * used.
3597 		 */
3598 		if ((error = copypktopts(opt, stickyopt, Z_NOWAIT)) != 0) {
3599 			return error;
3600 		}
3601 	}
3602 
3603 	/*
3604 	 * XXX: Currently, we assume all the optional information is stored
3605 	 * in a single mbuf.
3606 	 */
3607 	if (control->m_next) {
3608 		return EINVAL;
3609 	}
3610 
3611 	if (control->m_len < CMSG_LEN(0)) {
3612 		return EINVAL;
3613 	}
3614 
3615 	for (cm = M_FIRST_CMSGHDR(control);
3616 	    is_cmsg_valid(control, cm);
3617 	    cm = M_NXT_CMSGHDR(control, cm)) {
3618 		int error;
3619 
3620 		if (cm->cmsg_level != IPPROTO_IPV6) {
3621 			continue;
3622 		}
3623 
3624 		error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
3625 		    cm->cmsg_len - CMSG_LEN(0), opt, 0, 1, uproto);
3626 		if (error) {
3627 			return error;
3628 		}
3629 	}
3630 
3631 	return 0;
3632 }
3633 /*
3634  * Set a particular packet option, as a sticky option or an ancillary data
3635  * item.  "len" can be 0 only when it's a sticky option.
3636  * We have 4 cases of combination of "sticky" and "cmsg":
3637  * "sticky=0, cmsg=0": impossible
3638  * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3639  * "sticky=1, cmsg=0": RFC3542 socket option
3640  * "sticky=1, cmsg=1": RFC2292 socket option
3641  */
3642 static int
ip6_setpktopt(int optname,u_char * buf,int len,struct ip6_pktopts * opt,int sticky,int cmsg,int uproto)3643 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
3644     int sticky, int cmsg, int uproto)
3645 {
3646 	int minmtupolicy, preftemp;
3647 	int error;
3648 	boolean_t capture_exthdrstat_out = FALSE;
3649 
3650 	if (!sticky && !cmsg) {
3651 #ifdef DIAGNOSTIC
3652 		printf("ip6_setpktopt: impossible case\n");
3653 #endif
3654 		return EINVAL;
3655 	}
3656 
3657 	/*
3658 	 * Caller must have ensured that the buffer is at least
3659 	 * aligned on 32-bit boundary.
3660 	 */
3661 	VERIFY(IS_P2ALIGNED(buf, sizeof(u_int32_t)));
3662 
3663 	/*
3664 	 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3665 	 * not be specified in the context of RFC3542.  Conversely,
3666 	 * RFC3542 types should not be specified in the context of RFC2292.
3667 	 */
3668 	if (!cmsg) {
3669 		switch (optname) {
3670 		case IPV6_2292PKTINFO:
3671 		case IPV6_2292HOPLIMIT:
3672 		case IPV6_2292NEXTHOP:
3673 		case IPV6_2292HOPOPTS:
3674 		case IPV6_2292DSTOPTS:
3675 		case IPV6_2292RTHDR:
3676 		case IPV6_2292PKTOPTIONS:
3677 			return ENOPROTOOPT;
3678 		}
3679 	}
3680 	if (sticky && cmsg) {
3681 		switch (optname) {
3682 		case IPV6_PKTINFO:
3683 		case IPV6_HOPLIMIT:
3684 		case IPV6_NEXTHOP:
3685 		case IPV6_HOPOPTS:
3686 		case IPV6_DSTOPTS:
3687 		case IPV6_RTHDRDSTOPTS:
3688 		case IPV6_RTHDR:
3689 		case IPV6_USE_MIN_MTU:
3690 		case IPV6_DONTFRAG:
3691 		case IPV6_TCLASS:
3692 		case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
3693 			return ENOPROTOOPT;
3694 		}
3695 	}
3696 
3697 	switch (optname) {
3698 	case IPV6_2292PKTINFO:
3699 	case IPV6_PKTINFO: {
3700 		struct ifnet *ifp = NULL;
3701 		struct in6_pktinfo *pktinfo;
3702 
3703 		if (len != sizeof(struct in6_pktinfo)) {
3704 			return EINVAL;
3705 		}
3706 
3707 		pktinfo = (struct in6_pktinfo *)(void *)buf;
3708 
3709 		/*
3710 		 * An application can clear any sticky IPV6_PKTINFO option by
3711 		 * doing a "regular" setsockopt with ipi6_addr being
3712 		 * in6addr_any and ipi6_ifindex being zero.
3713 		 * [RFC 3542, Section 6]
3714 		 */
3715 		if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3716 		    pktinfo->ipi6_ifindex == 0 &&
3717 		    IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3718 			ip6_clearpktopts(opt, optname);
3719 			break;
3720 		}
3721 
3722 		if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3723 		    sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3724 			return EINVAL;
3725 		}
3726 
3727 		/* validate the interface index if specified. */
3728 		ifnet_head_lock_shared();
3729 
3730 		if (pktinfo->ipi6_ifindex > if_index) {
3731 			ifnet_head_done();
3732 			return ENXIO;
3733 		}
3734 
3735 		if (pktinfo->ipi6_ifindex) {
3736 			ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
3737 			if (ifp == NULL) {
3738 				ifnet_head_done();
3739 				return ENXIO;
3740 			}
3741 		}
3742 
3743 		ifnet_head_done();
3744 
3745 		/*
3746 		 * We store the address anyway, and let in6_selectsrc()
3747 		 * validate the specified address.  This is because ipi6_addr
3748 		 * may not have enough information about its scope zone, and
3749 		 * we may need additional information (such as outgoing
3750 		 * interface or the scope zone of a destination address) to
3751 		 * disambiguate the scope.
3752 		 * XXX: the delay of the validation may confuse the
3753 		 * application when it is used as a sticky option.
3754 		 */
3755 		if (opt->ip6po_pktinfo == NULL) {
3756 			opt->ip6po_pktinfo = kalloc_type(struct in6_pktinfo, Z_NOWAIT);
3757 			if (opt->ip6po_pktinfo == NULL) {
3758 				return ENOBUFS;
3759 			}
3760 		}
3761 		bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3762 		break;
3763 	}
3764 
3765 	case IPV6_2292HOPLIMIT:
3766 	case IPV6_HOPLIMIT: {
3767 		int *hlimp;
3768 
3769 		/*
3770 		 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3771 		 * to simplify the ordering among hoplimit options.
3772 		 */
3773 		if (optname == IPV6_HOPLIMIT && sticky) {
3774 			return ENOPROTOOPT;
3775 		}
3776 
3777 		if (len != sizeof(int)) {
3778 			return EINVAL;
3779 		}
3780 		hlimp = (int *)(void *)buf;
3781 		if (*hlimp < -1 || *hlimp > IPV6_MAXHLIM) {
3782 			return EINVAL;
3783 		}
3784 
3785 		opt->ip6po_hlim = *hlimp;
3786 		break;
3787 	}
3788 
3789 	case IPV6_TCLASS: {
3790 		int tclass;
3791 
3792 		if (len != sizeof(int)) {
3793 			return EINVAL;
3794 		}
3795 		tclass = *(int *)(void *)buf;
3796 		if (tclass < -1 || tclass > 255) {
3797 			return EINVAL;
3798 		}
3799 
3800 		opt->ip6po_tclass = tclass;
3801 		break;
3802 	}
3803 
3804 	case IPV6_2292NEXTHOP:
3805 	case IPV6_NEXTHOP:
3806 		error = suser(kauth_cred_get(), 0);
3807 		if (error) {
3808 			return EACCES;
3809 		}
3810 
3811 		if (len == 0) { /* just remove the option */
3812 			ip6_clearpktopts(opt, IPV6_NEXTHOP);
3813 			break;
3814 		}
3815 
3816 		/* check if cmsg_len is large enough for sa_len */
3817 		if (len < sizeof(struct sockaddr) || len < *buf) {
3818 			return EINVAL;
3819 		}
3820 
3821 		switch (SA(buf)->sa_family) {
3822 		case AF_INET6: {
3823 			struct sockaddr_in6 *sa6 = SIN6(buf);
3824 
3825 			if (sa6->sin6_len != sizeof(struct sockaddr_in6)) {
3826 				return EINVAL;
3827 			}
3828 
3829 			if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3830 			    IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3831 				return EINVAL;
3832 			}
3833 			if ((error = sa6_embedscope(sa6, ip6_use_defzone, IN6_NULL_IF_EMBEDDED_SCOPE(&sa6->sin6_scope_id)))
3834 			    != 0) {
3835 				return error;
3836 			}
3837 			break;
3838 		}
3839 		case AF_LINK:   /* should eventually be supported */
3840 		default:
3841 			return EAFNOSUPPORT;
3842 		}
3843 
3844 		/* turn off the previous option, then set the new option. */
3845 		ip6_clearpktopts(opt, IPV6_NEXTHOP);
3846 		opt->ip6po_nexthop = kalloc_data(*buf, Z_NOWAIT);
3847 		if (opt->ip6po_nexthop == NULL) {
3848 			return ENOBUFS;
3849 		}
3850 		bcopy(buf, opt->ip6po_nexthop, *buf);
3851 		break;
3852 
3853 	case IPV6_2292HOPOPTS:
3854 	case IPV6_HOPOPTS: {
3855 		struct ip6_hbh *hbh;
3856 		int hbhlen;
3857 
3858 		/*
3859 		 * XXX: We don't allow a non-privileged user to set ANY HbH
3860 		 * options, since per-option restriction has too much
3861 		 * overhead.
3862 		 */
3863 		error = suser(kauth_cred_get(), 0);
3864 		if (error) {
3865 			return EACCES;
3866 		}
3867 
3868 		if (len == 0) {
3869 			ip6_clearpktopts(opt, IPV6_HOPOPTS);
3870 			break;  /* just remove the option */
3871 		}
3872 
3873 		/* message length validation */
3874 		if (len < sizeof(struct ip6_hbh)) {
3875 			return EINVAL;
3876 		}
3877 		hbh = (struct ip6_hbh *)(void *)buf;
3878 		hbhlen = (hbh->ip6h_len + 1) << 3;
3879 		if (len != hbhlen) {
3880 			return EINVAL;
3881 		}
3882 
3883 		/* turn off the previous option, then set the new option. */
3884 		ip6_clearpktopts(opt, IPV6_HOPOPTS);
3885 		opt->ip6po_hbh = kalloc_data(hbhlen, Z_NOWAIT);
3886 		if (opt->ip6po_hbh == NULL) {
3887 			return ENOBUFS;
3888 		}
3889 		bcopy(hbh, opt->ip6po_hbh, hbhlen);
3890 		capture_exthdrstat_out = TRUE;
3891 		break;
3892 	}
3893 
3894 	case IPV6_2292DSTOPTS:
3895 	case IPV6_DSTOPTS:
3896 	case IPV6_RTHDRDSTOPTS: {
3897 		struct ip6_dest *dest, **newdest = NULL;
3898 		int destlen;
3899 
3900 		error = suser(kauth_cred_get(), 0);
3901 		if (error) {
3902 			return EACCES;
3903 		}
3904 
3905 		if (len == 0) {
3906 			ip6_clearpktopts(opt, optname);
3907 			break;  /* just remove the option */
3908 		}
3909 
3910 		/* message length validation */
3911 		if (len < sizeof(struct ip6_dest)) {
3912 			return EINVAL;
3913 		}
3914 		dest = (struct ip6_dest *)(void *)buf;
3915 		destlen = (dest->ip6d_len + 1) << 3;
3916 		if (len != destlen) {
3917 			return EINVAL;
3918 		}
3919 
3920 		/*
3921 		 * Determine the position that the destination options header
3922 		 * should be inserted; before or after the routing header.
3923 		 */
3924 		switch (optname) {
3925 		case IPV6_2292DSTOPTS:
3926 			/*
3927 			 * The old advacned API is ambiguous on this point.
3928 			 * Our approach is to determine the position based
3929 			 * according to the existence of a routing header.
3930 			 * Note, however, that this depends on the order of the
3931 			 * extension headers in the ancillary data; the 1st
3932 			 * part of the destination options header must appear
3933 			 * before the routing header in the ancillary data,
3934 			 * too.
3935 			 * RFC3542 solved the ambiguity by introducing
3936 			 * separate ancillary data or option types.
3937 			 */
3938 			if (opt->ip6po_rthdr == NULL) {
3939 				newdest = &opt->ip6po_dest1;
3940 			} else {
3941 				newdest = &opt->ip6po_dest2;
3942 			}
3943 			break;
3944 		case IPV6_RTHDRDSTOPTS:
3945 			newdest = &opt->ip6po_dest1;
3946 			break;
3947 		case IPV6_DSTOPTS:
3948 			newdest = &opt->ip6po_dest2;
3949 			break;
3950 		}
3951 
3952 		/* turn off the previous option, then set the new option. */
3953 		ip6_clearpktopts(opt, optname);
3954 		*newdest = kalloc_data(destlen, Z_NOWAIT);
3955 		if (*newdest == NULL) {
3956 			return ENOBUFS;
3957 		}
3958 		bcopy(dest, *newdest, destlen);
3959 		capture_exthdrstat_out = TRUE;
3960 		break;
3961 	}
3962 
3963 	case IPV6_2292RTHDR:
3964 	case IPV6_RTHDR: {
3965 		struct ip6_rthdr *rth;
3966 		int rthlen;
3967 
3968 		if (len == 0) {
3969 			ip6_clearpktopts(opt, IPV6_RTHDR);
3970 			break;  /* just remove the option */
3971 		}
3972 
3973 		/* message length validation */
3974 		if (len < sizeof(struct ip6_rthdr)) {
3975 			return EINVAL;
3976 		}
3977 		rth = (struct ip6_rthdr *)(void *)buf;
3978 		rthlen = (rth->ip6r_len + 1) << 3;
3979 		if (len != rthlen) {
3980 			return EINVAL;
3981 		}
3982 
3983 		switch (rth->ip6r_type) {
3984 		case IPV6_RTHDR_TYPE_0:
3985 			if (rth->ip6r_len == 0) { /* must contain one addr */
3986 				return EINVAL;
3987 			}
3988 			if (rth->ip6r_len % 2) { /* length must be even */
3989 				return EINVAL;
3990 			}
3991 			if (rth->ip6r_len / 2 != rth->ip6r_segleft) {
3992 				return EINVAL;
3993 			}
3994 			break;
3995 		default:
3996 			return EINVAL;        /* not supported */
3997 		}
3998 
3999 		/* turn off the previous option */
4000 		ip6_clearpktopts(opt, IPV6_RTHDR);
4001 		opt->ip6po_rthdr = kalloc_data(rthlen, Z_NOWAIT);
4002 		if (opt->ip6po_rthdr == NULL) {
4003 			return ENOBUFS;
4004 		}
4005 		bcopy(rth, opt->ip6po_rthdr, rthlen);
4006 		capture_exthdrstat_out = TRUE;
4007 		break;
4008 	}
4009 
4010 	case IPV6_USE_MIN_MTU:
4011 		if (len != sizeof(int)) {
4012 			return EINVAL;
4013 		}
4014 		minmtupolicy = *(int *)(void *)buf;
4015 		if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
4016 		    minmtupolicy != IP6PO_MINMTU_DISABLE &&
4017 		    minmtupolicy != IP6PO_MINMTU_ALL) {
4018 			return EINVAL;
4019 		}
4020 		opt->ip6po_minmtu = minmtupolicy;
4021 		break;
4022 
4023 	case IPV6_DONTFRAG:
4024 		if (len != sizeof(int)) {
4025 			return EINVAL;
4026 		}
4027 
4028 		if (uproto == IPPROTO_TCP || *(int *)(void *)buf == 0) {
4029 			/*
4030 			 * we ignore this option for TCP sockets.
4031 			 * (RFC3542 leaves this case unspecified.)
4032 			 */
4033 			opt->ip6po_flags &= ~IP6PO_DONTFRAG;
4034 		} else {
4035 			opt->ip6po_flags |= IP6PO_DONTFRAG;
4036 		}
4037 		break;
4038 
4039 	case IPV6_PREFER_TEMPADDR:
4040 		if (len != sizeof(int)) {
4041 			return EINVAL;
4042 		}
4043 		preftemp = *(int *)(void *)buf;
4044 		if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
4045 		    preftemp != IP6PO_TEMPADDR_NOTPREFER &&
4046 		    preftemp != IP6PO_TEMPADDR_PREFER) {
4047 			return EINVAL;
4048 		}
4049 		opt->ip6po_prefer_tempaddr = preftemp;
4050 		break;
4051 
4052 	default:
4053 		return ENOPROTOOPT;
4054 	} /* end of switch */
4055 
4056 	if (capture_exthdrstat_out) {
4057 		if (uproto == IPPROTO_TCP) {
4058 			INC_ATOMIC_INT64_LIM(net_api_stats.nas_sock_inet6_stream_exthdr_out);
4059 		} else if (uproto == IPPROTO_UDP) {
4060 			INC_ATOMIC_INT64_LIM(net_api_stats.nas_sock_inet6_dgram_exthdr_out);
4061 		}
4062 	}
4063 
4064 	return 0;
4065 }
4066 
4067 /*
4068  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
4069  * packet to the input queue of a specified interface.  Note that this
4070  * calls the output routine of the loopback "driver", but with an interface
4071  * pointer that might NOT be &loif -- easier than replicating that code here.
4072  */
4073 void
ip6_mloopback(struct ifnet * srcifp,struct ifnet * origifp,struct mbuf * m,struct sockaddr_in6 * dst,uint32_t optlen,int32_t nxt0)4074 ip6_mloopback(struct ifnet *srcifp, struct ifnet *origifp, struct mbuf *m,
4075     struct sockaddr_in6 *dst, uint32_t optlen, int32_t nxt0)
4076 {
4077 	struct mbuf *copym;
4078 	struct ip6_hdr *ip6;
4079 	struct in6_addr src;
4080 
4081 	if (lo_ifp == NULL) {
4082 		return;
4083 	}
4084 
4085 	/*
4086 	 * Copy the packet header as it's needed for the checksum.
4087 	 * Make sure to deep-copy IPv6 header portion in case the data
4088 	 * is in an mbuf cluster, so that we can safely override the IPv6
4089 	 * header portion later.
4090 	 */
4091 	copym = m_copym_mode(m, 0, M_COPYALL, M_DONTWAIT, M_COPYM_COPY_HDR);
4092 	if (copym != NULL && ((copym->m_flags & M_EXT) ||
4093 	    copym->m_len < sizeof(struct ip6_hdr))) {
4094 		copym = m_pullup(copym, sizeof(struct ip6_hdr));
4095 	}
4096 
4097 	if (copym == NULL) {
4098 		return;
4099 	}
4100 
4101 	ip6 = mtod(copym, struct ip6_hdr *);
4102 	src = ip6->ip6_src;
4103 	/*
4104 	 * clear embedded scope identifiers if necessary.
4105 	 * in6_clearscope will touch the addresses only when necessary.
4106 	 */
4107 	in6_clearscope(&ip6->ip6_src);
4108 	in6_clearscope(&ip6->ip6_dst);
4109 
4110 	if (copym->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA) {
4111 		in6_delayed_cksum_offset(copym, 0, optlen, nxt0);
4112 	}
4113 
4114 	/*
4115 	 * Stuff the 'real' ifp into the pkthdr, to be used in matching
4116 	 * in ip6_input(); we need the loopback ifp/dl_tag passed as args
4117 	 * to make the loopback driver compliant with the data link
4118 	 * requirements.
4119 	 */
4120 	copym->m_pkthdr.rcvif = origifp;
4121 
4122 	/*
4123 	 * Also record the source interface (which owns the source address).
4124 	 * This is basically a stripped down version of ifa_foraddr6().
4125 	 */
4126 	if (srcifp == NULL) {
4127 		struct in6_ifaddr *ia;
4128 
4129 		lck_rw_lock_shared(&in6_ifaddr_rwlock);
4130 		TAILQ_FOREACH(ia, IN6ADDR_HASH(&src), ia6_hash) {
4131 			IFA_LOCK_SPIN(&ia->ia_ifa);
4132 			/* compare against src addr with embedded scope */
4133 			if (in6_are_addr_equal_scoped(&ia->ia_addr.sin6_addr, &src, ia->ia_addr.sin6_scope_id, ip6_output_getsrcifscope(m))) {
4134 				srcifp = ia->ia_ifp;
4135 				IFA_UNLOCK(&ia->ia_ifa);
4136 				break;
4137 			}
4138 			IFA_UNLOCK(&ia->ia_ifa);
4139 		}
4140 		lck_rw_done(&in6_ifaddr_rwlock);
4141 	}
4142 	if (srcifp != NULL) {
4143 		ip6_setsrcifaddr_info(copym, srcifp->if_index, NULL);
4144 	}
4145 	ip6_setdstifaddr_info(copym, origifp->if_index, NULL);
4146 
4147 	dlil_output(lo_ifp, PF_INET6, copym, NULL, SA(dst), 0, NULL);
4148 }
4149 
4150 /*
4151  * Chop IPv6 header off from the payload.
4152  */
4153 static int
ip6_splithdr(struct mbuf * m,struct ip6_exthdrs * exthdrs)4154 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
4155 {
4156 	struct mbuf *mh;
4157 	struct ip6_hdr *ip6;
4158 
4159 	ip6 = mtod(m, struct ip6_hdr *);
4160 	if (m->m_len > sizeof(*ip6)) {
4161 		MGETHDR(mh, M_DONTWAIT, MT_HEADER);     /* MAC-OK */
4162 		if (mh == NULL) {
4163 			m_freem(m);
4164 			return ENOBUFS;
4165 		}
4166 		M_COPY_PKTHDR(mh, m);
4167 		MH_ALIGN(mh, sizeof(*ip6));
4168 		m->m_flags &= ~M_PKTHDR;
4169 		m->m_len -= sizeof(*ip6);
4170 		m->m_data += sizeof(*ip6);
4171 		mh->m_next = m;
4172 		m = mh;
4173 		m->m_len = sizeof(*ip6);
4174 		bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
4175 	}
4176 	exthdrs->ip6e_ip6 = m;
4177 	return 0;
4178 }
4179 
4180 static void
ip6_output_checksum(struct ifnet * ifp,uint32_t mtu,struct mbuf * m,int nxt0,uint32_t tlen,uint32_t optlen)4181 ip6_output_checksum(struct ifnet *ifp, uint32_t mtu, struct mbuf *m,
4182     int nxt0, uint32_t tlen, uint32_t optlen)
4183 {
4184 	uint32_t sw_csum, hwcap = ifp->if_hwassist;
4185 	int tso = TSO_IPV6_OK(ifp, m);
4186 
4187 	if (!hwcksum_tx) {
4188 		/* do all in software; checksum offload is disabled */
4189 		sw_csum = CSUM_DELAY_IPV6_DATA & m->m_pkthdr.csum_flags;
4190 	} else {
4191 		/* do in software what the hardware cannot */
4192 		sw_csum = m->m_pkthdr.csum_flags &
4193 		    ~IF_HWASSIST_CSUM_FLAGS(hwcap);
4194 	}
4195 
4196 	if (optlen != 0) {
4197 		sw_csum |= (CSUM_DELAY_IPV6_DATA &
4198 		    m->m_pkthdr.csum_flags);
4199 	} else if (!(sw_csum & CSUM_DELAY_IPV6_DATA) &&
4200 	    (hwcap & CSUM_PARTIAL)) {
4201 		/*
4202 		 * Partial checksum offload, ere), if no extension headers,
4203 		 * and TCP only (no UDP support, as the hardware may not be
4204 		 * able to convert +0 to -0 (0xffff) per RFC1122 4.1.3.4.
4205 		 * unless the interface supports "invert zero" capability.)
4206 		 */
4207 		if (hwcksum_tx && !tso &&
4208 		    ((m->m_pkthdr.csum_flags & CSUM_TCPIPV6) ||
4209 		    ((hwcap & CSUM_ZERO_INVERT) &&
4210 		    (m->m_pkthdr.csum_flags & CSUM_ZERO_INVERT))) &&
4211 		    tlen <= mtu) {
4212 			uint16_t start = sizeof(struct ip6_hdr);
4213 			uint16_t ulpoff =
4214 			    m->m_pkthdr.csum_data & 0xffff;
4215 			m->m_pkthdr.csum_flags |=
4216 			    (CSUM_DATA_VALID | CSUM_PARTIAL);
4217 			m->m_pkthdr.csum_tx_stuff = (ulpoff + start);
4218 			m->m_pkthdr.csum_tx_start = start;
4219 			sw_csum = 0;
4220 		} else {
4221 			sw_csum |= (CSUM_DELAY_IPV6_DATA &
4222 			    m->m_pkthdr.csum_flags);
4223 		}
4224 	}
4225 
4226 	if (sw_csum & CSUM_DELAY_IPV6_DATA) {
4227 		in6_delayed_cksum_offset(m, 0, optlen, nxt0);
4228 		sw_csum &= ~CSUM_DELAY_IPV6_DATA;
4229 	}
4230 
4231 	if (hwcksum_tx) {
4232 		/*
4233 		 * Drop off bits that aren't supported by hardware;
4234 		 * also make sure to preserve non-checksum related bits.
4235 		 */
4236 		m->m_pkthdr.csum_flags =
4237 		    ((m->m_pkthdr.csum_flags &
4238 		    (IF_HWASSIST_CSUM_FLAGS(hwcap) | CSUM_DATA_VALID)) |
4239 		    (m->m_pkthdr.csum_flags & ~IF_HWASSIST_CSUM_MASK));
4240 	} else {
4241 		/* drop all bits; checksum offload is disabled */
4242 		m->m_pkthdr.csum_flags = 0;
4243 	}
4244 }
4245 
4246 /*
4247  * Compute IPv6 extension header length.
4248  */
4249 int
ip6_optlen(struct in6pcb * in6p)4250 ip6_optlen(struct in6pcb *in6p)
4251 {
4252 	int len;
4253 
4254 	if (!in6p->in6p_outputopts) {
4255 		return 0;
4256 	}
4257 
4258 	len = 0;
4259 #define elen(x)                                                         \
4260 	(((struct ip6_ext *)(x)) ?                                      \
4261 	(((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
4262 
4263 	len += elen(in6p->in6p_outputopts->ip6po_hbh);
4264 	if (in6p->in6p_outputopts->ip6po_rthdr) {
4265 		/* dest1 is valid with rthdr only */
4266 		len += elen(in6p->in6p_outputopts->ip6po_dest1);
4267 	}
4268 	len += elen(in6p->in6p_outputopts->ip6po_rthdr);
4269 	len += elen(in6p->in6p_outputopts->ip6po_dest2);
4270 	return len;
4271 #undef elen
4272 }
4273 
4274 static int
4275 sysctl_reset_ip6_output_stats SYSCTL_HANDLER_ARGS
4276 {
4277 #pragma unused(arg1, arg2)
4278 	int error, i;
4279 
4280 	i = ip6_output_measure;
4281 	error = sysctl_handle_int(oidp, &i, 0, req);
4282 	if (error || req->newptr == USER_ADDR_NULL) {
4283 		goto done;
4284 	}
4285 	/* impose bounds */
4286 	if (i < 0 || i > 1) {
4287 		error = EINVAL;
4288 		goto done;
4289 	}
4290 	if (ip6_output_measure != i && i == 1) {
4291 		net_perf_initialize(&net_perf, ip6_output_measure_bins);
4292 	}
4293 	ip6_output_measure = i;
4294 done:
4295 	return error;
4296 }
4297 
4298 static int
4299 sysctl_ip6_output_measure_bins SYSCTL_HANDLER_ARGS
4300 {
4301 #pragma unused(arg1, arg2)
4302 	int error;
4303 	uint64_t i;
4304 
4305 	i = ip6_output_measure_bins;
4306 	error = sysctl_handle_quad(oidp, &i, 0, req);
4307 	if (error || req->newptr == USER_ADDR_NULL) {
4308 		goto done;
4309 	}
4310 	/* validate data */
4311 	if (!net_perf_validate_bins(i)) {
4312 		error = EINVAL;
4313 		goto done;
4314 	}
4315 	ip6_output_measure_bins = i;
4316 done:
4317 	return error;
4318 }
4319 
4320 static int
4321 sysctl_ip6_output_getperf SYSCTL_HANDLER_ARGS
4322 {
4323 #pragma unused(oidp, arg1, arg2)
4324 	if (req->oldptr == USER_ADDR_NULL) {
4325 		req->oldlen = (size_t)sizeof(struct ipstat);
4326 	}
4327 
4328 	return SYSCTL_OUT(req, &net_perf, MIN(sizeof(net_perf), req->oldlen));
4329 }
4330 
4331 void
ip6_output_setsrcifscope(struct mbuf * m,uint32_t src_idx,struct in6_ifaddr * ia6)4332 ip6_output_setsrcifscope(struct mbuf *m, uint32_t src_idx, struct in6_ifaddr *ia6)
4333 {
4334 	VERIFY(m->m_flags & M_PKTHDR);
4335 
4336 	m->m_pkthdr.pkt_ext_flags |= PKTF_EXT_OUTPUT_SCOPE;
4337 	if (ia6 != NULL) {
4338 		m->m_pkthdr.src_ifindex = ia6->ia_ifp->if_index;
4339 	} else {
4340 		m->m_pkthdr.src_ifindex = (uint16_t)src_idx;
4341 	}
4342 }
4343 
4344 void
ip6_output_setdstifscope(struct mbuf * m,uint32_t dst_idx,struct in6_ifaddr * ia6)4345 ip6_output_setdstifscope(struct mbuf *m, uint32_t dst_idx, struct in6_ifaddr *ia6)
4346 {
4347 	VERIFY(m->m_flags & M_PKTHDR);
4348 
4349 	m->m_pkthdr.pkt_ext_flags |= PKTF_EXT_OUTPUT_SCOPE;
4350 	if (ia6 != NULL) {
4351 		m->m_pkthdr.dst_ifindex = ia6->ia_ifp->if_index;
4352 	} else {
4353 		m->m_pkthdr.dst_ifindex = (uint16_t)dst_idx;
4354 	}
4355 }
4356 
4357 uint32_t
ip6_output_getsrcifscope(struct mbuf * m)4358 ip6_output_getsrcifscope(struct mbuf *m)
4359 {
4360 	VERIFY(m->m_flags & M_PKTHDR);
4361 	if (in6_embedded_scope_debug) {
4362 		VERIFY(m->m_pkthdr.pkt_ext_flags & PKTF_EXT_OUTPUT_SCOPE);
4363 		VERIFY((m->m_pkthdr.pkt_flags & PKTF_IFAINFO) == 0);
4364 	}
4365 
4366 	return m->m_pkthdr.src_ifindex;
4367 }
4368 
4369 uint32_t
ip6_output_getdstifscope(struct mbuf * m)4370 ip6_output_getdstifscope(struct mbuf *m)
4371 {
4372 	VERIFY(m->m_flags & M_PKTHDR);
4373 	if (in6_embedded_scope_debug) {
4374 		VERIFY(m->m_pkthdr.pkt_ext_flags & PKTF_EXT_OUTPUT_SCOPE);
4375 		VERIFY((m->m_pkthdr.pkt_flags & PKTF_IFAINFO) == 0);
4376 	}
4377 
4378 	return m->m_pkthdr.dst_ifindex;
4379 }
4380