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