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 static_assert(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 case IPV6_RECV_LINK_ADDR_TYPE:
2502 if (optlen != sizeof(int)) {
2503 error = EINVAL;
2504 break;
2505 }
2506 error = sooptcopyin(sopt, &optval,
2507 sizeof(optval), sizeof(optval));
2508 if (error) {
2509 break;
2510 }
2511
2512 switch (optname) {
2513 case IPV6_UNICAST_HOPS:
2514 if (optval < -1 || optval >= 256) {
2515 error = EINVAL;
2516 } else {
2517 /* -1 = kernel default */
2518 in6p->in6p_hops = (short)optval;
2519 if (in6p->inp_vflag &
2520 INP_IPV4) {
2521 in6p->inp_ip_ttl =
2522 (uint8_t)optval;
2523 }
2524 }
2525 break;
2526 #define OPTSET(bit) do { \
2527 if (optval) \
2528 in6p->inp_flags |= (bit); \
2529 else \
2530 in6p->inp_flags &= ~(bit); \
2531 } while (0)
2532
2533 #define OPTSET2(bit) do { \
2534 if (optval) { \
2535 in6p->inp_flags2 |= (bit); \
2536 } else { \
2537 in6p->inp_flags2 &= ~(bit); \
2538 } \
2539 } while (0)
2540
2541 #define OPTSET2292(bit) do { \
2542 in6p->inp_flags |= IN6P_RFC2292; \
2543 if (optval) \
2544 in6p->inp_flags |= (bit); \
2545 else \
2546 in6p->inp_flags &= ~(bit); \
2547 } while (0)
2548
2549 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
2550 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
2551
2552 case IPV6_RECVPKTINFO:
2553 /* cannot mix with RFC2292 */
2554 if (OPTBIT(IN6P_RFC2292)) {
2555 error = EINVAL;
2556 break;
2557 }
2558 OPTSET(IN6P_PKTINFO);
2559 break;
2560
2561 case IPV6_HOPLIMIT: {
2562 struct ip6_pktopts **optp;
2563
2564 /* cannot mix with RFC2292 */
2565 if (OPTBIT(IN6P_RFC2292)) {
2566 error = EINVAL;
2567 break;
2568 }
2569 optp = &in6p->in6p_outputopts;
2570 error = ip6_pcbopt(IPV6_HOPLIMIT,
2571 (u_char *)&optval, sizeof(optval),
2572 optp, uproto);
2573 break;
2574 }
2575
2576 case IPV6_RECVHOPLIMIT:
2577 /* cannot mix with RFC2292 */
2578 if (OPTBIT(IN6P_RFC2292)) {
2579 error = EINVAL;
2580 break;
2581 }
2582 OPTSET(IN6P_HOPLIMIT);
2583 break;
2584
2585 case IPV6_RECVHOPOPTS:
2586 /* cannot mix with RFC2292 */
2587 if (OPTBIT(IN6P_RFC2292)) {
2588 error = EINVAL;
2589 break;
2590 }
2591 OPTSET(IN6P_HOPOPTS);
2592 capture_exthdrstat_in = TRUE;
2593 break;
2594
2595 case IPV6_RECVDSTOPTS:
2596 /* cannot mix with RFC2292 */
2597 if (OPTBIT(IN6P_RFC2292)) {
2598 error = EINVAL;
2599 break;
2600 }
2601 OPTSET(IN6P_DSTOPTS);
2602 capture_exthdrstat_in = TRUE;
2603 break;
2604
2605 case IPV6_RECVRTHDRDSTOPTS:
2606 /* cannot mix with RFC2292 */
2607 if (OPTBIT(IN6P_RFC2292)) {
2608 error = EINVAL;
2609 break;
2610 }
2611 OPTSET(IN6P_RTHDRDSTOPTS);
2612 capture_exthdrstat_in = TRUE;
2613 break;
2614
2615 case IPV6_RECVRTHDR:
2616 /* cannot mix with RFC2292 */
2617 if (OPTBIT(IN6P_RFC2292)) {
2618 error = EINVAL;
2619 break;
2620 }
2621 OPTSET(IN6P_RTHDR);
2622 capture_exthdrstat_in = TRUE;
2623 break;
2624
2625 case IPV6_RECVPATHMTU:
2626 /*
2627 * We ignore this option for TCP
2628 * sockets.
2629 * (RFC3542 leaves this case
2630 * unspecified.)
2631 */
2632 if (uproto != IPPROTO_TCP) {
2633 OPTSET(IN6P_MTU);
2634 }
2635 break;
2636
2637 case IPV6_V6ONLY:
2638 /*
2639 * make setsockopt(IPV6_V6ONLY)
2640 * available only prior to bind(2).
2641 * see ipng mailing list, Jun 22 2001.
2642 */
2643 if (in6p->inp_lport ||
2644 !IN6_IS_ADDR_UNSPECIFIED(
2645 &in6p->in6p_laddr)) {
2646 error = EINVAL;
2647 break;
2648 }
2649 OPTSET(IN6P_IPV6_V6ONLY);
2650 if (optval) {
2651 in6p->inp_vflag &= ~INP_IPV4;
2652 } else {
2653 in6p->inp_vflag |= INP_IPV4;
2654 }
2655 break;
2656
2657 case IPV6_RECVTCLASS:
2658 /* we can mix with RFC2292 */
2659 OPTSET(IN6P_TCLASS);
2660 break;
2661
2662 case IPV6_AUTOFLOWLABEL:
2663 OPTSET(IN6P_AUTOFLOWLABEL);
2664 break;
2665
2666 case IPV6_RECV_LINK_ADDR_TYPE:
2667 OPTSET2(INP2_RECV_LINK_ADDR_TYPE);
2668 break;
2669 }
2670 break;
2671
2672 case IPV6_TCLASS:
2673 case IPV6_DONTFRAG:
2674 case IPV6_USE_MIN_MTU:
2675 case IPV6_PREFER_TEMPADDR: {
2676 struct ip6_pktopts **optp;
2677
2678 if (optlen != sizeof(optval)) {
2679 error = EINVAL;
2680 break;
2681 }
2682 error = sooptcopyin(sopt, &optval,
2683 sizeof(optval), sizeof(optval));
2684 if (error) {
2685 break;
2686 }
2687
2688 optp = &in6p->in6p_outputopts;
2689 error = ip6_pcbopt(optname, (u_char *)&optval,
2690 sizeof(optval), optp, uproto);
2691 switch (optname) {
2692 case IPV6_TCLASS: {
2693 // Add in the ECN flags
2694 u_int8_t tos = (in6p->inp_ip_tos & ~IPTOS_ECN_MASK);
2695 u_int8_t ecn = optval & IPTOS_ECN_MASK;
2696 in6p->inp_ip_tos = tos | ecn;
2697 }
2698 break;
2699 case IPV6_DONTFRAG:
2700 if (in6p->inp_vflag & INP_IPV4) {
2701 OPTSET2(INP2_DONTFRAG);
2702 }
2703 break;
2704 }
2705 break;
2706 }
2707
2708 case IPV6_2292PKTINFO:
2709 case IPV6_2292HOPLIMIT:
2710 case IPV6_2292HOPOPTS:
2711 case IPV6_2292DSTOPTS:
2712 case IPV6_2292RTHDR:
2713 /* RFC 2292 */
2714 if (optlen != sizeof(int)) {
2715 error = EINVAL;
2716 break;
2717 }
2718 error = sooptcopyin(sopt, &optval,
2719 sizeof(optval), sizeof(optval));
2720 if (error) {
2721 break;
2722 }
2723 switch (optname) {
2724 case IPV6_2292PKTINFO:
2725 OPTSET2292(IN6P_PKTINFO);
2726 break;
2727 case IPV6_2292HOPLIMIT:
2728 OPTSET2292(IN6P_HOPLIMIT);
2729 break;
2730 case IPV6_2292HOPOPTS:
2731 /*
2732 * Check super-user privilege.
2733 * See comments for IPV6_RECVHOPOPTS.
2734 */
2735 if (!privileged) {
2736 return EPERM;
2737 }
2738 OPTSET2292(IN6P_HOPOPTS);
2739 capture_exthdrstat_in = TRUE;
2740 break;
2741 case IPV6_2292DSTOPTS:
2742 if (!privileged) {
2743 return EPERM;
2744 }
2745 OPTSET2292(IN6P_DSTOPTS |
2746 IN6P_RTHDRDSTOPTS); /* XXX */
2747 capture_exthdrstat_in = TRUE;
2748 break;
2749 case IPV6_2292RTHDR:
2750 OPTSET2292(IN6P_RTHDR);
2751 capture_exthdrstat_in = TRUE;
2752 break;
2753 }
2754 break;
2755
2756 case IPV6_3542PKTINFO:
2757 case IPV6_3542HOPOPTS:
2758 case IPV6_3542RTHDR:
2759 case IPV6_3542DSTOPTS:
2760 case IPV6_RTHDRDSTOPTS:
2761 case IPV6_3542NEXTHOP: {
2762 struct ip6_pktopts **optp;
2763 /* new advanced API (RFC3542) */
2764 mbuf_ref_t m;
2765
2766 /* cannot mix with RFC2292 */
2767 if (OPTBIT(IN6P_RFC2292)) {
2768 error = EINVAL;
2769 break;
2770 }
2771 error = soopt_getm(sopt, &m);
2772 if (error != 0) {
2773 break;
2774 }
2775 error = soopt_mcopyin(sopt, m);
2776 if (error != 0) {
2777 break;
2778 }
2779
2780 optp = &in6p->in6p_outputopts;
2781 error = ip6_pcbopt(optname, mtod(m, u_char *),
2782 m->m_len, optp, uproto);
2783 m_freem(m);
2784 break;
2785 }
2786 #undef OPTSET
2787 #undef OPTSET2
2788 case IPV6_MULTICAST_IF:
2789 case IPV6_MULTICAST_HOPS:
2790 case IPV6_MULTICAST_LOOP:
2791 case IPV6_JOIN_GROUP:
2792 case IPV6_LEAVE_GROUP:
2793 case IPV6_MSFILTER:
2794 case MCAST_BLOCK_SOURCE:
2795 case MCAST_UNBLOCK_SOURCE:
2796 case MCAST_JOIN_GROUP:
2797 case MCAST_LEAVE_GROUP:
2798 case MCAST_JOIN_SOURCE_GROUP:
2799 case MCAST_LEAVE_SOURCE_GROUP:
2800 error = ip6_setmoptions(in6p, sopt);
2801 break;
2802
2803 case IPV6_PORTRANGE:
2804 error = sooptcopyin(sopt, &optval,
2805 sizeof(optval), sizeof(optval));
2806 if (error) {
2807 break;
2808 }
2809
2810 switch (optval) {
2811 case IPV6_PORTRANGE_DEFAULT:
2812 in6p->inp_flags &= ~(INP_LOWPORT);
2813 in6p->inp_flags &= ~(INP_HIGHPORT);
2814 break;
2815
2816 case IPV6_PORTRANGE_HIGH:
2817 in6p->inp_flags &= ~(INP_LOWPORT);
2818 in6p->inp_flags |= INP_HIGHPORT;
2819 break;
2820
2821 case IPV6_PORTRANGE_LOW:
2822 in6p->inp_flags &= ~(INP_HIGHPORT);
2823 in6p->inp_flags |= INP_LOWPORT;
2824 break;
2825
2826 default:
2827 error = EINVAL;
2828 break;
2829 }
2830 break;
2831 #if IPSEC
2832 case IPV6_IPSEC_POLICY: {
2833 caddr_t req = NULL;
2834 size_t len = 0;
2835 mbuf_ref_t m;
2836
2837 if ((error = soopt_getm(sopt, &m)) != 0) {
2838 break;
2839 }
2840 if ((error = soopt_mcopyin(sopt, m)) != 0) {
2841 break;
2842 }
2843
2844 req = mtod(m, caddr_t);
2845 len = m->m_len;
2846 error = ipsec6_set_policy(in6p, optname, req,
2847 len, privileged);
2848 m_freem(m);
2849 break;
2850 }
2851 #endif /* IPSEC */
2852 /*
2853 * IPv6 variant of IP_BOUND_IF; for details see
2854 * comments on IP_BOUND_IF in ip_ctloutput().
2855 */
2856 case IPV6_BOUND_IF:
2857 /* This option is settable only on IPv6 */
2858 if (!(in6p->inp_vflag & INP_IPV6)) {
2859 error = EINVAL;
2860 break;
2861 }
2862
2863 error = sooptcopyin(sopt, &optval,
2864 sizeof(optval), sizeof(optval));
2865
2866 if (error) {
2867 break;
2868 }
2869
2870 error = inp_bindif(in6p, optval, NULL);
2871 break;
2872
2873 case IPV6_NO_IFT_CELLULAR:
2874 /* This option is settable only for IPv6 */
2875 if (!(in6p->inp_vflag & INP_IPV6)) {
2876 error = EINVAL;
2877 break;
2878 }
2879
2880 error = sooptcopyin(sopt, &optval,
2881 sizeof(optval), sizeof(optval));
2882
2883 if (error) {
2884 break;
2885 }
2886
2887 /* once set, it cannot be unset */
2888 if (!optval && INP_NO_CELLULAR(in6p)) {
2889 error = EINVAL;
2890 break;
2891 }
2892
2893 error = so_set_restrictions(so,
2894 SO_RESTRICT_DENY_CELLULAR);
2895 break;
2896
2897 case IPV6_OUT_IF:
2898 /* This option is not settable */
2899 error = EINVAL;
2900 break;
2901
2902 default:
2903 error = ENOPROTOOPT;
2904 break;
2905 }
2906 if (capture_exthdrstat_in) {
2907 if (uproto == IPPROTO_TCP) {
2908 INC_ATOMIC_INT64_LIM(net_api_stats.nas_sock_inet6_stream_exthdr_in);
2909 } else if (uproto == IPPROTO_UDP) {
2910 INC_ATOMIC_INT64_LIM(net_api_stats.nas_sock_inet6_dgram_exthdr_in);
2911 }
2912 }
2913 break;
2914
2915 case SOPT_GET:
2916 switch (optname) {
2917 case IPV6_2292PKTOPTIONS:
2918 /*
2919 * RFC3542 (effectively) deprecated the
2920 * semantics of the 2292-style pktoptions.
2921 * Since it was not reliable in nature (i.e.,
2922 * applications had to expect the lack of some
2923 * information after all), it would make sense
2924 * to simplify this part by always returning
2925 * empty data.
2926 */
2927 sopt->sopt_valsize = 0;
2928 break;
2929
2930 case IPV6_RECVHOPOPTS:
2931 case IPV6_RECVDSTOPTS:
2932 case IPV6_RECVRTHDRDSTOPTS:
2933 case IPV6_UNICAST_HOPS:
2934 case IPV6_RECVPKTINFO:
2935 case IPV6_RECVHOPLIMIT:
2936 case IPV6_RECVRTHDR:
2937 case IPV6_RECVPATHMTU:
2938 case IPV6_V6ONLY:
2939 case IPV6_PORTRANGE:
2940 case IPV6_RECVTCLASS:
2941 case IPV6_AUTOFLOWLABEL:
2942 case IPV6_RECV_LINK_ADDR_TYPE:
2943 switch (optname) {
2944 case IPV6_RECVHOPOPTS:
2945 optval = OPTBIT(IN6P_HOPOPTS);
2946 break;
2947
2948 case IPV6_RECVDSTOPTS:
2949 optval = OPTBIT(IN6P_DSTOPTS);
2950 break;
2951
2952 case IPV6_RECVRTHDRDSTOPTS:
2953 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2954 break;
2955
2956 case IPV6_UNICAST_HOPS:
2957 optval = in6p->in6p_hops;
2958 break;
2959
2960 case IPV6_RECVPKTINFO:
2961 optval = OPTBIT(IN6P_PKTINFO);
2962 break;
2963
2964 case IPV6_RECVHOPLIMIT:
2965 optval = OPTBIT(IN6P_HOPLIMIT);
2966 break;
2967
2968 case IPV6_RECVRTHDR:
2969 optval = OPTBIT(IN6P_RTHDR);
2970 break;
2971
2972 case IPV6_RECVPATHMTU:
2973 optval = OPTBIT(IN6P_MTU);
2974 break;
2975
2976 case IPV6_V6ONLY:
2977 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2978 break;
2979
2980 case IPV6_PORTRANGE: {
2981 int flags;
2982 flags = in6p->inp_flags;
2983 if (flags & INP_HIGHPORT) {
2984 optval = IPV6_PORTRANGE_HIGH;
2985 } else if (flags & INP_LOWPORT) {
2986 optval = IPV6_PORTRANGE_LOW;
2987 } else {
2988 optval = 0;
2989 }
2990 break;
2991 }
2992 case IPV6_RECVTCLASS:
2993 optval = OPTBIT(IN6P_TCLASS);
2994 break;
2995
2996 case IPV6_AUTOFLOWLABEL:
2997 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2998 break;
2999
3000 case IPV6_RECV_LINK_ADDR_TYPE:
3001 optval = OPTBIT2(INP2_RECV_LINK_ADDR_TYPE);
3002 break;
3003 }
3004
3005 if (error) {
3006 break;
3007 }
3008 error = sooptcopyout(sopt, &optval,
3009 sizeof(optval));
3010 break;
3011
3012 case IPV6_PATHMTU: {
3013 u_int32_t pmtu = 0;
3014 struct ip6_mtuinfo mtuinfo;
3015 struct route_in6 sro;
3016
3017 bzero(&sro, sizeof(sro));
3018
3019 if (!(so->so_state & SS_ISCONNECTED)) {
3020 return ENOTCONN;
3021 }
3022 /*
3023 * XXX: we dot not consider the case of source
3024 * routing, or optional information to specify
3025 * the outgoing interface.
3026 */
3027 error = ip6_getpmtu(&sro, NULL, NULL,
3028 &in6p->in6p_faddr, in6p->inp_fifscope, &pmtu);
3029 ROUTE_RELEASE(&sro);
3030 if (error) {
3031 break;
3032 }
3033 if (pmtu > IPV6_MAXPACKET) {
3034 pmtu = IPV6_MAXPACKET;
3035 }
3036
3037 bzero(&mtuinfo, sizeof(mtuinfo));
3038 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
3039 optdata = (void *)&mtuinfo;
3040 optdatalen = sizeof(mtuinfo);
3041 error = sooptcopyout(sopt, optdata,
3042 optdatalen);
3043 break;
3044 }
3045
3046 case IPV6_2292PKTINFO:
3047 case IPV6_2292HOPLIMIT:
3048 case IPV6_2292HOPOPTS:
3049 case IPV6_2292RTHDR:
3050 case IPV6_2292DSTOPTS:
3051 switch (optname) {
3052 case IPV6_2292PKTINFO:
3053 optval = OPTBIT(IN6P_PKTINFO);
3054 break;
3055 case IPV6_2292HOPLIMIT:
3056 optval = OPTBIT(IN6P_HOPLIMIT);
3057 break;
3058 case IPV6_2292HOPOPTS:
3059 optval = OPTBIT(IN6P_HOPOPTS);
3060 break;
3061 case IPV6_2292RTHDR:
3062 optval = OPTBIT(IN6P_RTHDR);
3063 break;
3064 case IPV6_2292DSTOPTS:
3065 optval = OPTBIT(IN6P_DSTOPTS |
3066 IN6P_RTHDRDSTOPTS);
3067 break;
3068 }
3069 error = sooptcopyout(sopt, &optval,
3070 sizeof(optval));
3071 break;
3072
3073 case IPV6_PKTINFO:
3074 case IPV6_HOPOPTS:
3075 case IPV6_RTHDR:
3076 case IPV6_DSTOPTS:
3077 case IPV6_RTHDRDSTOPTS:
3078 case IPV6_NEXTHOP:
3079 case IPV6_TCLASS:
3080 case IPV6_DONTFRAG:
3081 case IPV6_USE_MIN_MTU:
3082 case IPV6_PREFER_TEMPADDR:
3083 error = ip6_getpcbopt(in6p->in6p_outputopts,
3084 optname, sopt);
3085 break;
3086
3087 case IPV6_MULTICAST_IF:
3088 case IPV6_MULTICAST_HOPS:
3089 case IPV6_MULTICAST_LOOP:
3090 case IPV6_MSFILTER:
3091 error = ip6_getmoptions(in6p, sopt);
3092 break;
3093 #if IPSEC
3094 case IPV6_IPSEC_POLICY: {
3095 error = 0; /* This option is no longer supported */
3096 break;
3097 }
3098 #endif /* IPSEC */
3099 case IPV6_BOUND_IF:
3100 if (in6p->inp_flags & INP_BOUND_IF) {
3101 optval = in6p->inp_boundifp->if_index;
3102 }
3103 error = sooptcopyout(sopt, &optval,
3104 sizeof(optval));
3105 break;
3106
3107 case IPV6_NO_IFT_CELLULAR:
3108 optval = INP_NO_CELLULAR(in6p) ? 1 : 0;
3109 error = sooptcopyout(sopt, &optval,
3110 sizeof(optval));
3111 break;
3112
3113 case IPV6_OUT_IF:
3114 optval = (in6p->in6p_last_outifp != NULL) ?
3115 in6p->in6p_last_outifp->if_index : 0;
3116 error = sooptcopyout(sopt, &optval,
3117 sizeof(optval));
3118 break;
3119
3120 default:
3121 error = ENOPROTOOPT;
3122 break;
3123 }
3124 break;
3125 }
3126 } else if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_BINDTODEVICE) {
3127 char namebuf[IFNAMSIZ + 1] = {};
3128
3129 switch (op) {
3130 case SOPT_SET:
3131 error = sooptcopyin_bindtodevice(sopt, namebuf, sizeof(namebuf));
3132 if (error != 0) {
3133 break;
3134 }
3135 namebuf[IFNAMSIZ] = 0;
3136
3137 error = inp_bindtodevice(in6p, __unsafe_null_terminated_from_indexable(namebuf));
3138
3139 break;
3140 case SOPT_GET:
3141 if (in6p->inp_flags & INP_BOUND_IF) {
3142 strlcpy(namebuf, in6p->inp_boundifp->if_xname, IFNAMSIZ);
3143 }
3144 error = sooptcopyout(sopt, &namebuf, IFNAMSIZ);
3145 break;
3146 }
3147 } else {
3148 error = EINVAL;
3149 }
3150 return error;
3151 }
3152
3153 int
ip6_raw_ctloutput(struct socket * so,struct sockopt * sopt)3154 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
3155 {
3156 int error = 0, optval;
3157 size_t optlen;
3158 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
3159 struct inpcb *__single in6p = sotoinpcb(so);
3160 int level, op, optname;
3161
3162 level = sopt->sopt_level;
3163 op = sopt->sopt_dir;
3164 optname = sopt->sopt_name;
3165 optlen = sopt->sopt_valsize;
3166
3167 if (level != IPPROTO_IPV6) {
3168 return EINVAL;
3169 }
3170
3171 switch (optname) {
3172 case IPV6_CHECKSUM:
3173 /*
3174 * For ICMPv6 sockets, no modification allowed for checksum
3175 * offset, permit "no change" values to help existing apps.
3176 *
3177 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
3178 * for an ICMPv6 socket will fail."
3179 * The current behavior does not meet RFC3542.
3180 */
3181 switch (op) {
3182 case SOPT_SET:
3183 if (optlen != sizeof(int)) {
3184 error = EINVAL;
3185 break;
3186 }
3187 error = sooptcopyin(sopt, &optval, sizeof(optval),
3188 sizeof(optval));
3189 if (error) {
3190 break;
3191 }
3192 if ((optval % 2) != 0) {
3193 /* the API assumes even offset values */
3194 error = EINVAL;
3195 } else if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
3196 if (optval != icmp6off) {
3197 error = EINVAL;
3198 }
3199 } else {
3200 in6p->in6p_cksum = optval;
3201 }
3202 break;
3203
3204 case SOPT_GET:
3205 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
3206 optval = icmp6off;
3207 } else {
3208 optval = in6p->in6p_cksum;
3209 }
3210
3211 error = sooptcopyout(sopt, &optval, sizeof(optval));
3212 break;
3213
3214 default:
3215 error = EINVAL;
3216 break;
3217 }
3218 break;
3219 default:
3220 error = ENOPROTOOPT;
3221 break;
3222 }
3223
3224 return error;
3225 }
3226
3227 /*
3228 * Set up IP6 options in pcb for insertion in output packets or
3229 * specifying behavior of outgoing packets.
3230 */
3231 static int
ip6_pcbopts(struct ip6_pktopts ** pktopt,struct mbuf * m,struct socket * so,struct sockopt * sopt)3232 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, struct socket *so,
3233 struct sockopt *sopt)
3234 {
3235 #pragma unused(sopt)
3236 struct ip6_pktopts *__single opt = *pktopt;
3237 int error = 0;
3238
3239 /* turn off any old options. */
3240 if (opt != NULL) {
3241 #if DIAGNOSTIC
3242 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
3243 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
3244 opt->ip6po_rhinfo.ip6po_rhi_rthdr) {
3245 printf("%s: all specified options are cleared.\n",
3246 __func__);
3247 }
3248 #endif
3249 ip6_clearpktopts(opt, -1);
3250 } else {
3251 opt = kalloc_type(struct ip6_pktopts, Z_WAITOK | Z_NOFAIL);
3252 }
3253 *pktopt = NULL;
3254
3255 if (m == NULL || m->m_len == 0) {
3256 /*
3257 * Only turning off any previous options, regardless of
3258 * whether the opt is just created or given.
3259 */
3260 if (opt != NULL) {
3261 kfree_type(struct ip6_pktopts, opt);
3262 }
3263 return 0;
3264 }
3265
3266 /* set options specified by user. */
3267 if ((error = ip6_setpktopts(m, opt, NULL, SOCK_PROTO(so))) != 0) {
3268 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
3269 kfree_type(struct ip6_pktopts, opt);
3270 return error;
3271 }
3272 *pktopt = opt;
3273 return 0;
3274 }
3275
3276 /*
3277 * initialize ip6_pktopts. beware that there are non-zero default values in
3278 * the struct.
3279 */
3280 void
ip6_initpktopts(struct ip6_pktopts * opt)3281 ip6_initpktopts(struct ip6_pktopts *opt)
3282 {
3283 bzero(opt, sizeof(*opt));
3284 opt->ip6po_hlim = -1; /* -1 means default hop limit */
3285 opt->ip6po_tclass = -1; /* -1 means default traffic class */
3286 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
3287 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
3288 }
3289
3290 static int
ip6_pcbopt(int optname,u_char * buf __sized_by (len),int len,struct ip6_pktopts ** pktopt,int uproto)3291 ip6_pcbopt(int optname, u_char *buf __sized_by(len), int len, struct ip6_pktopts **pktopt,
3292 int uproto)
3293 {
3294 struct ip6_pktopts *__single opt;
3295
3296 opt = *pktopt;
3297 if (opt == NULL) {
3298 opt = kalloc_type(struct ip6_pktopts, Z_WAITOK | Z_NOFAIL);
3299 ip6_initpktopts(opt);
3300 *pktopt = opt;
3301 }
3302
3303 return ip6_setpktopt(optname, buf, len, opt, 1, 0, uproto);
3304 }
3305
3306 static int
ip6_getpcbopt(struct ip6_pktopts * pktopt,int optname,struct sockopt * sopt)3307 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
3308 {
3309 struct ip6_ext *ip6e;
3310
3311 /*
3312 * Buffer for serialization of empty IPV6_PKTINO option.
3313 * Initialized only once.
3314 */
3315 static uint8_t null_pktinfo_bytes[sizeof(struct in6_pktinfo)] = {0, };
3316
3317 switch (optname) {
3318 case IPV6_PKTINFO:
3319 if (pktopt && pktopt->ip6po_pktinfo) {
3320 return sooptcopyout(sopt, pktopt->ip6po_pktinfo, sizeof(struct in6_pktinfo));
3321 } else {
3322 return sooptcopyout(sopt, &null_pktinfo_bytes, sizeof(struct in6_pktinfo));
3323 }
3324 case IPV6_TCLASS: {
3325 int optval;
3326 if (pktopt && pktopt->ip6po_tclass >= 0) {
3327 optval = pktopt->ip6po_tclass;
3328 } else {
3329 optval = 0;
3330 }
3331 return sooptcopyout(sopt, &optval, sizeof(optval));
3332 }
3333 case IPV6_HOPOPTS:
3334 if (pktopt && pktopt->ip6po_hbh) {
3335 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
3336 return sooptcopyout(sopt, (struct ip6_rthdr *__indexable)pktopt->ip6po_hbh, IP6_EXT_LEN(ip6e));
3337 }
3338 break;
3339 case IPV6_RTHDR:
3340 if (pktopt && pktopt->ip6po_rthdr) {
3341 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
3342 return sooptcopyout(sopt, (struct ip6_rthdr *__indexable)pktopt->ip6po_rthdr, IP6_EXT_LEN(ip6e));
3343 }
3344 break;
3345 case IPV6_RTHDRDSTOPTS:
3346 if (pktopt && pktopt->ip6po_dest1) {
3347 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
3348 return sooptcopyout(sopt, (struct ip6_dest *__indexable)pktopt->ip6po_dest1, IP6_EXT_LEN(ip6e));
3349 }
3350 break;
3351 case IPV6_DSTOPTS:
3352 if (pktopt && pktopt->ip6po_dest2) {
3353 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
3354 return sooptcopyout(sopt, (struct ip6_dest *__indexable)pktopt->ip6po_dest2, IP6_EXT_LEN(ip6e));
3355 }
3356 break;
3357 case IPV6_NEXTHOP:
3358 if (pktopt && pktopt->ip6po_nexthop) {
3359 return sooptcopyout(sopt, (struct sockaddr *__indexable)pktopt->ip6po_nexthop, pktopt->ip6po_nexthop->sa_len);
3360 }
3361 break;
3362 case IPV6_USE_MIN_MTU: {
3363 int optval;
3364 if (pktopt) {
3365 optval = pktopt->ip6po_minmtu;
3366 } else {
3367 optval = IP6PO_MINMTU_MCASTONLY;
3368 }
3369 return sooptcopyout(sopt, &optval, sizeof(optval));
3370 }
3371 case IPV6_DONTFRAG: {
3372 int on;
3373 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) {
3374 on = 1;
3375 } else {
3376 on = 0;
3377 }
3378 return sooptcopyout(sopt, &on, sizeof(on));
3379 }
3380 case IPV6_PREFER_TEMPADDR: {
3381 int optval;
3382 if (pktopt) {
3383 optval = pktopt->ip6po_prefer_tempaddr;
3384 } else {
3385 optval = IP6PO_TEMPADDR_SYSTEM;
3386 }
3387 return sooptcopyout(sopt, &optval, sizeof(optval));
3388 }
3389 default: /* should not happen */
3390 #ifdef DIAGNOSTIC
3391 panic("ip6_getpcbopt: unexpected option");
3392 #endif
3393 return ENOPROTOOPT;
3394 }
3395
3396 // for bincompat, update sopt->sopt_valsize to be 0
3397 return sooptcopyout(sopt, NULL, 0);
3398 }
3399
3400 void
ip6_clearpktopts(struct ip6_pktopts * pktopt,int optname)3401 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
3402 {
3403 if (pktopt == NULL) {
3404 return;
3405 }
3406
3407 if (optname == -1 || optname == IPV6_PKTINFO) {
3408 if (pktopt->ip6po_pktinfo) {
3409 kfree_type(struct in6_pktinfo, pktopt->ip6po_pktinfo);
3410 }
3411 pktopt->ip6po_pktinfo = NULL;
3412 }
3413 if (optname == -1 || optname == IPV6_HOPLIMIT) {
3414 pktopt->ip6po_hlim = -1;
3415 }
3416 if (optname == -1 || optname == IPV6_TCLASS) {
3417 pktopt->ip6po_tclass = -1;
3418 }
3419 if (optname == -1 || optname == IPV6_NEXTHOP) {
3420 ROUTE_RELEASE(&pktopt->ip6po_nextroute);
3421 if (pktopt->ip6po_nexthop) {
3422 kfree_data_addr(pktopt->ip6po_nexthop);
3423 }
3424 pktopt->ip6po_nexthop = NULL;
3425 }
3426 if (optname == -1 || optname == IPV6_HOPOPTS) {
3427 if (pktopt->ip6po_hbh) {
3428 kfree_data_addr(pktopt->ip6po_hbh);
3429 }
3430 pktopt->ip6po_hbh = NULL;
3431 }
3432 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
3433 if (pktopt->ip6po_dest1) {
3434 kfree_data_addr(pktopt->ip6po_dest1);
3435 }
3436 pktopt->ip6po_dest1 = NULL;
3437 }
3438 if (optname == -1 || optname == IPV6_RTHDR) {
3439 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) {
3440 kfree_data_addr(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr);
3441 }
3442 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
3443 ROUTE_RELEASE(&pktopt->ip6po_route);
3444 }
3445 if (optname == -1 || optname == IPV6_DSTOPTS) {
3446 if (pktopt->ip6po_dest2) {
3447 kfree_data_addr(pktopt->ip6po_dest2);
3448 }
3449 pktopt->ip6po_dest2 = NULL;
3450 }
3451 }
3452
3453 #define PKTOPT_EXTHDRCPY(t, f) do { \
3454 if (src->f != NULL) { \
3455 int hlen = \
3456 (((struct ip6_ext *)src->f)->ip6e_len + 1) << 3; \
3457 dst->f = kalloc_data(hlen, canwait); \
3458 if (dst->f == NULL && canwait == Z_NOWAIT) \
3459 goto bad; \
3460 bcopy(GET_IP6EXTHDR(src, t, f), \
3461 GET_IP6EXTHDR(dst, t, f), \
3462 hlen); \
3463 } \
3464 } while (0)
3465
3466 #define GET_IP6_DEST_OPT(b, d, dlen) { \
3467 do { \
3468 struct ip6_dest *t = (struct ip6_dest *)(void *)buf; \
3469 (dlen) = (t->ip6d_len + 1) << 3; \
3470 (d) = __unsafe_forge_bidi_indexable(struct ip6_dest *, (b), len); \
3471 } while(0); \
3472 }
3473
3474 static int
copypktopts(struct ip6_pktopts * dst,struct ip6_pktopts * src,zalloc_flags_t canwait)3475 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, zalloc_flags_t canwait)
3476 {
3477 if (dst == NULL || src == NULL) {
3478 printf("copypktopts: invalid argument\n");
3479 return EINVAL;
3480 }
3481
3482 dst->ip6po_hlim = src->ip6po_hlim;
3483 dst->ip6po_tclass = src->ip6po_tclass;
3484 dst->ip6po_flags = src->ip6po_flags;
3485 if (src->ip6po_pktinfo) {
3486 dst->ip6po_pktinfo = kalloc_type(struct in6_pktinfo, canwait);
3487 if (dst->ip6po_pktinfo == NULL && canwait == Z_NOWAIT) {
3488 goto bad;
3489 }
3490 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
3491 }
3492 if (src->ip6po_nexthop) {
3493 dst->ip6po_nexthop = kalloc_data(src->ip6po_nexthop->sa_len, canwait);
3494 if (dst->ip6po_nexthop == NULL && canwait == Z_NOWAIT) {
3495 goto bad;
3496 }
3497 SOCKADDR_COPY(src->ip6po_nexthop, dst->ip6po_nexthop,
3498 src->ip6po_nexthop->sa_len);
3499 }
3500 PKTOPT_EXTHDRCPY(struct ip6_hbh*, ip6po_hbh);
3501 PKTOPT_EXTHDRCPY(struct ip6_dest*, ip6po_dest1);
3502 PKTOPT_EXTHDRCPY(struct ip6_dest*, ip6po_dest2);
3503 PKTOPT_EXTHDRCPY(struct ip6_rthdr*, ip6po_rthdr); /* not copy the cached route */
3504 return 0;
3505
3506 bad:
3507 ip6_clearpktopts(dst, -1);
3508 return ENOBUFS;
3509 }
3510 #undef PKTOPT_EXTHDRCPY
3511
3512 struct ip6_pktopts *
ip6_copypktopts(struct ip6_pktopts * src,zalloc_flags_t canwait)3513 ip6_copypktopts(struct ip6_pktopts *src, zalloc_flags_t canwait)
3514 {
3515 int error;
3516 struct ip6_pktopts *__single dst;
3517
3518 dst = kalloc_type(struct ip6_pktopts, canwait);
3519 if (dst == NULL) {
3520 return NULL;
3521 }
3522 ip6_initpktopts(dst);
3523
3524 if ((error = copypktopts(dst, src, canwait)) != 0) {
3525 kfree_type(struct ip6_pktopts, dst);
3526 return NULL;
3527 }
3528
3529 return dst;
3530 }
3531
3532 void
ip6_freepcbopts(struct ip6_pktopts * pktopt)3533 ip6_freepcbopts(struct ip6_pktopts *pktopt)
3534 {
3535 if (pktopt == NULL) {
3536 return;
3537 }
3538
3539 ip6_clearpktopts(pktopt, -1);
3540
3541 kfree_type(struct ip6_pktopts, pktopt);
3542 }
3543
3544 void
ip6_moptions_init(void)3545 ip6_moptions_init(void)
3546 {
3547 PE_parse_boot_argn("ifa_debug", &im6o_debug, sizeof(im6o_debug));
3548
3549 vm_size_t im6o_size = (im6o_debug == 0) ? sizeof(struct ip6_moptions) :
3550 sizeof(struct ip6_moptions_dbg);
3551
3552 im6o_zone = zone_create(IM6O_ZONE_NAME, im6o_size, ZC_ZFREE_CLEARMEM);
3553 }
3554
3555 void
im6o_addref(struct ip6_moptions * im6o,int locked)3556 im6o_addref(struct ip6_moptions *im6o, int locked)
3557 {
3558 if (!locked) {
3559 IM6O_LOCK(im6o);
3560 } else {
3561 IM6O_LOCK_ASSERT_HELD(im6o);
3562 }
3563
3564 if (++im6o->im6o_refcnt == 0) {
3565 panic("%s: im6o %p wraparound refcnt", __func__, im6o);
3566 /* NOTREACHED */
3567 } else if (im6o->im6o_trace != NULL) {
3568 (*im6o->im6o_trace)(im6o, TRUE);
3569 }
3570
3571 if (!locked) {
3572 IM6O_UNLOCK(im6o);
3573 }
3574 }
3575
3576 void
im6o_remref(struct ip6_moptions * im6o)3577 im6o_remref(struct ip6_moptions *im6o)
3578 {
3579 int i;
3580
3581 IM6O_LOCK(im6o);
3582 if (im6o->im6o_refcnt == 0) {
3583 panic("%s: im6o %p negative refcnt", __func__, im6o);
3584 /* NOTREACHED */
3585 } else if (im6o->im6o_trace != NULL) {
3586 (*im6o->im6o_trace)(im6o, FALSE);
3587 }
3588
3589 --im6o->im6o_refcnt;
3590 if (im6o->im6o_refcnt > 0) {
3591 IM6O_UNLOCK(im6o);
3592 return;
3593 }
3594
3595 for (i = 0; i < im6o->im6o_num_memberships; ++i) {
3596 struct in6_mfilter *__single imf;
3597
3598 imf = im6o->im6o_mfilters ? &im6o->im6o_mfilters[i] : NULL;
3599 if (imf != NULL) {
3600 im6f_leave(imf);
3601 }
3602
3603 (void) in6_mc_leave(im6o->im6o_membership[i], imf);
3604
3605 if (imf != NULL) {
3606 im6f_purge(imf);
3607 }
3608
3609 IN6M_REMREF(im6o->im6o_membership[i]);
3610 im6o->im6o_membership[i] = NULL;
3611 }
3612 im6o->im6o_num_memberships = 0;
3613 IM6O_UNLOCK(im6o);
3614
3615 kfree_type_counted_by(struct in6_multi *, im6o->im6o_max_memberships, im6o->im6o_membership);
3616 kfree_type_counted_by(struct in6_mfilter, im6o->im6o_max_filters, im6o->im6o_mfilters);
3617
3618 lck_mtx_destroy(&im6o->im6o_lock, &ifa_mtx_grp);
3619
3620 if (!(im6o->im6o_debug & IFD_ALLOC)) {
3621 panic("%s: im6o %p cannot be freed", __func__, im6o);
3622 /* NOTREACHED */
3623 }
3624 zfree(im6o_zone, im6o);
3625 }
3626
3627 static void
im6o_trace(struct ip6_moptions * im6o,int refhold)3628 im6o_trace(struct ip6_moptions *im6o, int refhold)
3629 {
3630 struct ip6_moptions_dbg *im6o_dbg = (struct ip6_moptions_dbg *)im6o;
3631 ctrace_t *tr;
3632 u_int32_t idx;
3633 u_int16_t *cnt;
3634
3635 if (!(im6o->im6o_debug & IFD_DEBUG)) {
3636 panic("%s: im6o %p has no debug structure", __func__, im6o);
3637 /* NOTREACHED */
3638 }
3639 if (refhold) {
3640 cnt = &im6o_dbg->im6o_refhold_cnt;
3641 tr = im6o_dbg->im6o_refhold;
3642 } else {
3643 cnt = &im6o_dbg->im6o_refrele_cnt;
3644 tr = im6o_dbg->im6o_refrele;
3645 }
3646
3647 idx = os_atomic_inc_orig(cnt, relaxed) % IM6O_TRACE_HIST_SIZE;
3648 ctrace_record(&tr[idx]);
3649 }
3650
3651 struct ip6_moptions *
ip6_allocmoptions(zalloc_flags_t how)3652 ip6_allocmoptions(zalloc_flags_t how)
3653 {
3654 struct ip6_moptions *im6o;
3655
3656 im6o = zalloc_flags(im6o_zone, how | Z_ZERO);
3657 if (im6o != NULL) {
3658 lck_mtx_init(&im6o->im6o_lock, &ifa_mtx_grp, &ifa_mtx_attr);
3659 im6o->im6o_debug |= IFD_ALLOC;
3660 if (im6o_debug != 0) {
3661 im6o->im6o_debug |= IFD_DEBUG;
3662 im6o->im6o_trace = im6o_trace;
3663 }
3664 IM6O_ADDREF(im6o);
3665 }
3666
3667 return im6o;
3668 }
3669
3670 /*
3671 * Set IPv6 outgoing packet options based on advanced API.
3672 */
3673 int
ip6_setpktopts(struct mbuf * control,struct ip6_pktopts * opt,struct ip6_pktopts * stickyopt,int uproto)3674 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
3675 struct ip6_pktopts *stickyopt, int uproto)
3676 {
3677 struct cmsghdr *cm = NULL;
3678
3679 if (control == NULL || opt == NULL) {
3680 return EINVAL;
3681 }
3682
3683 ip6_initpktopts(opt);
3684 if (stickyopt != NULL) {
3685 int error;
3686
3687 /*
3688 * If stickyopt is provided, make a local copy of the options
3689 * for this particular packet, then override them by ancillary
3690 * objects.
3691 * XXX: copypktopts() does not copy the cached route to a next
3692 * hop (if any). This is not very good in terms of efficiency,
3693 * but we can allow this since this option should be rarely
3694 * used.
3695 */
3696
3697 if ((error = copypktopts(opt, stickyopt, Z_NOWAIT)) != 0) {
3698 return error;
3699 }
3700 }
3701
3702 /*
3703 * XXX: Currently, we assume all the optional information is stored
3704 * in a single mbuf.
3705 */
3706 if (control->m_next) {
3707 return EINVAL;
3708 }
3709
3710 if (control->m_len < CMSG_LEN(0)) {
3711 return EINVAL;
3712 }
3713
3714 for (cm = M_FIRST_CMSGHDR(control);
3715 is_cmsg_valid(control, cm);
3716 cm = M_NXT_CMSGHDR(control, cm)) {
3717 int error;
3718
3719 if (cm->cmsg_level != IPPROTO_IPV6) {
3720 continue;
3721 }
3722
3723 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
3724 cm->cmsg_len - CMSG_LEN(0), opt, 0, 1, uproto);
3725 if (error) {
3726 return error;
3727 }
3728 }
3729
3730 return 0;
3731 }
3732 /*
3733 * Set a particular packet option, as a sticky option or an ancillary data
3734 * item. "len" can be 0 only when it's a sticky option.
3735 * We have 4 cases of combination of "sticky" and "cmsg":
3736 * "sticky=0, cmsg=0": impossible
3737 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3738 * "sticky=1, cmsg=0": RFC3542 socket option
3739 * "sticky=1, cmsg=1": RFC2292 socket option
3740 */
3741 static int
ip6_setpktopt(int optname,uint8_t * buf __sized_by (len),int len,struct ip6_pktopts * opt,int sticky,int cmsg,int uproto)3742 ip6_setpktopt(int optname, uint8_t *buf __sized_by(len), int len, struct ip6_pktopts *opt,
3743 int sticky, int cmsg, int uproto)
3744 {
3745 int minmtupolicy, preftemp;
3746 int error;
3747 boolean_t capture_exthdrstat_out = FALSE;
3748
3749 if (!sticky && !cmsg) {
3750 #ifdef DIAGNOSTIC
3751 printf("ip6_setpktopt: impossible case\n");
3752 #endif
3753 return EINVAL;
3754 }
3755
3756 /*
3757 * Caller must have ensured that the buffer is at least
3758 * aligned on 32-bit boundary.
3759 */
3760 VERIFY(IS_P2ALIGNED(buf, sizeof(u_int32_t)));
3761
3762 /*
3763 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3764 * not be specified in the context of RFC3542. Conversely,
3765 * RFC3542 types should not be specified in the context of RFC2292.
3766 */
3767 if (!cmsg) {
3768 switch (optname) {
3769 case IPV6_2292PKTINFO:
3770 case IPV6_2292HOPLIMIT:
3771 case IPV6_2292NEXTHOP:
3772 case IPV6_2292HOPOPTS:
3773 case IPV6_2292DSTOPTS:
3774 case IPV6_2292RTHDR:
3775 case IPV6_2292PKTOPTIONS:
3776 return ENOPROTOOPT;
3777 }
3778 }
3779 if (sticky && cmsg) {
3780 switch (optname) {
3781 case IPV6_PKTINFO:
3782 case IPV6_HOPLIMIT:
3783 case IPV6_NEXTHOP:
3784 case IPV6_HOPOPTS:
3785 case IPV6_DSTOPTS:
3786 case IPV6_RTHDRDSTOPTS:
3787 case IPV6_RTHDR:
3788 case IPV6_USE_MIN_MTU:
3789 case IPV6_DONTFRAG:
3790 case IPV6_TCLASS:
3791 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
3792 return ENOPROTOOPT;
3793 }
3794 }
3795
3796 switch (optname) {
3797 case IPV6_2292PKTINFO:
3798 case IPV6_PKTINFO: {
3799 struct ifnet *__single ifp = NULL;
3800 struct in6_pktinfo *pktinfo;
3801
3802 if (len != sizeof(struct in6_pktinfo)) {
3803 return EINVAL;
3804 }
3805
3806 pktinfo = (struct in6_pktinfo *)(void *)buf;
3807
3808 /*
3809 * An application can clear any sticky IPV6_PKTINFO option by
3810 * doing a "regular" setsockopt with ipi6_addr being
3811 * in6addr_any and ipi6_ifindex being zero.
3812 * [RFC 3542, Section 6]
3813 */
3814 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3815 pktinfo->ipi6_ifindex == 0 &&
3816 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3817 ip6_clearpktopts(opt, optname);
3818 break;
3819 }
3820
3821 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3822 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3823 return EINVAL;
3824 }
3825
3826 /* validate the interface index if specified. */
3827 ifnet_head_lock_shared();
3828
3829 if (pktinfo->ipi6_ifindex > if_index) {
3830 ifnet_head_done();
3831 return ENXIO;
3832 }
3833
3834 if (pktinfo->ipi6_ifindex) {
3835 ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
3836 if (ifp == NULL) {
3837 ifnet_head_done();
3838 return ENXIO;
3839 }
3840 }
3841
3842 ifnet_head_done();
3843
3844 /*
3845 * We store the address anyway, and let in6_selectsrc()
3846 * validate the specified address. This is because ipi6_addr
3847 * may not have enough information about its scope zone, and
3848 * we may need additional information (such as outgoing
3849 * interface or the scope zone of a destination address) to
3850 * disambiguate the scope.
3851 * XXX: the delay of the validation may confuse the
3852 * application when it is used as a sticky option.
3853 */
3854 if (opt->ip6po_pktinfo == NULL) {
3855 opt->ip6po_pktinfo = kalloc_type(struct in6_pktinfo, Z_NOWAIT);
3856 if (opt->ip6po_pktinfo == NULL) {
3857 return ENOBUFS;
3858 }
3859 }
3860 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3861 break;
3862 }
3863
3864 case IPV6_2292HOPLIMIT:
3865 case IPV6_HOPLIMIT: {
3866 int *hlimp;
3867
3868 /*
3869 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3870 * to simplify the ordering among hoplimit options.
3871 */
3872 if (optname == IPV6_HOPLIMIT && sticky) {
3873 return ENOPROTOOPT;
3874 }
3875
3876 if (len != sizeof(int)) {
3877 return EINVAL;
3878 }
3879 hlimp = (int *)(void *)buf;
3880 if (*hlimp < -1 || *hlimp > IPV6_MAXHLIM) {
3881 return EINVAL;
3882 }
3883
3884 opt->ip6po_hlim = (int16_t)(*hlimp);
3885 break;
3886 }
3887
3888 case IPV6_TCLASS: {
3889 int tclass;
3890
3891 if (len != sizeof(int)) {
3892 return EINVAL;
3893 }
3894 tclass = *(int *)(void *)buf;
3895 if (tclass < -1 || tclass > 255) {
3896 return EINVAL;
3897 }
3898
3899 opt->ip6po_tclass = (int16_t)tclass;
3900 break;
3901 }
3902
3903 case IPV6_2292NEXTHOP:
3904 case IPV6_NEXTHOP:
3905 error = suser(kauth_cred_get(), 0);
3906 if (error) {
3907 return EACCES;
3908 }
3909
3910 if (len == 0) { /* just remove the option */
3911 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3912 break;
3913 }
3914
3915 /* check if cmsg_len is large enough for sa_len */
3916 if (len < sizeof(struct sockaddr) || len < *buf) {
3917 return EINVAL;
3918 }
3919
3920 switch (SA(buf)->sa_family) {
3921 case AF_INET6: {
3922 struct sockaddr_in6 *sa6 = SIN6(buf);
3923
3924 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) {
3925 return EINVAL;
3926 }
3927
3928 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3929 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3930 return EINVAL;
3931 }
3932 if ((error = sa6_embedscope(sa6, ip6_use_defzone, IN6_NULL_IF_EMBEDDED_SCOPE(&sa6->sin6_scope_id)))
3933 != 0) {
3934 return error;
3935 }
3936 break;
3937 }
3938 case AF_LINK: /* should eventually be supported */
3939 default:
3940 return EAFNOSUPPORT;
3941 }
3942
3943 /* turn off the previous option, then set the new option. */
3944 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3945 opt->ip6po_nexthop = kalloc_data(*buf, Z_NOWAIT);
3946 if (opt->ip6po_nexthop == NULL) {
3947 return ENOBUFS;
3948 }
3949 SOCKADDR_COPY(buf, opt->ip6po_nexthop, *buf);
3950 break;
3951
3952 case IPV6_2292HOPOPTS:
3953 case IPV6_HOPOPTS: {
3954 struct ip6_hbh *hbh, *thbh = NULL;
3955 int hbhlen;
3956
3957 /*
3958 * XXX: We don't allow a non-privileged user to set ANY HbH
3959 * options, since per-option restriction has too much
3960 * overhead.
3961 */
3962 error = suser(kauth_cred_get(), 0);
3963 if (error) {
3964 return EACCES;
3965 }
3966
3967 if (len == 0) {
3968 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3969 break; /* just remove the option */
3970 }
3971
3972 /* message length validation */
3973 if (len < sizeof(struct ip6_hbh)) {
3974 return EINVAL;
3975 }
3976 hbh = (struct ip6_hbh *)(void *)buf;
3977 hbhlen = (hbh->ip6h_len + 1) << 3;
3978 if (len != hbhlen) {
3979 return EINVAL;
3980 }
3981
3982 /* turn off the previous option, then set the new option. */
3983 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3984 thbh = kalloc_data(hbhlen, Z_NOWAIT);
3985 if (thbh == NULL) {
3986 return ENOBUFS;
3987 }
3988
3989 bcopy(hbh, thbh, hbhlen);
3990 opt->ip6po_hbh = thbh;
3991 capture_exthdrstat_out = TRUE;
3992 break;
3993 }
3994
3995 case IPV6_2292DSTOPTS:
3996 case IPV6_DSTOPTS:
3997 case IPV6_RTHDRDSTOPTS: {
3998 struct ip6_dest *dest, *__single * newdest = NULL;
3999 int destlen;
4000
4001 error = suser(kauth_cred_get(), 0);
4002 if (error) {
4003 return EACCES;
4004 }
4005
4006 if (len == 0) {
4007 ip6_clearpktopts(opt, optname);
4008 break; /* just remove the option */
4009 }
4010
4011 /* message length validation */
4012 if (len < sizeof(struct ip6_dest)) {
4013 return EINVAL;
4014 }
4015
4016 GET_IP6_DEST_OPT(buf, dest, destlen);
4017 if (len != destlen) {
4018 return EINVAL;
4019 }
4020
4021 /*
4022 * Determine the position that the destination options header
4023 * should be inserted; before or after the routing header.
4024 */
4025 switch (optname) {
4026 case IPV6_2292DSTOPTS:
4027 /*
4028 * The old advanced API is ambiguous on this point.
4029 * Our approach is to determine the position based
4030 * according to the existence of a routing header.
4031 * Note, however, that this depends on the order of the
4032 * extension headers in the ancillary data; the 1st
4033 * part of the destination options header must appear
4034 * before the routing header in the ancillary data,
4035 * too.
4036 * RFC3542 solved the ambiguity by introducing
4037 * separate ancillary data or option types.
4038 */
4039 if (opt->ip6po_rthdr == NULL) {
4040 newdest = &opt->ip6po_dest1;
4041 } else {
4042 newdest = &opt->ip6po_dest2;
4043 }
4044 break;
4045 case IPV6_RTHDRDSTOPTS:
4046 newdest = &opt->ip6po_dest1;
4047 break;
4048 case IPV6_DSTOPTS:
4049 newdest = &opt->ip6po_dest2;
4050 break;
4051 }
4052
4053 /* turn off the previous option, then set the new option. */
4054 ip6_clearpktopts(opt, optname);
4055
4056 void *destbytes = kalloc_data(destlen, Z_NOWAIT);
4057 if (destbytes == NULL) {
4058 return ENOBUFS;
4059 }
4060 bcopy(dest, destbytes, destlen);
4061 *newdest = destbytes;
4062
4063 capture_exthdrstat_out = TRUE;
4064 break;
4065 }
4066
4067 case IPV6_2292RTHDR:
4068 case IPV6_RTHDR: {
4069 struct ip6_rthdr *rth, *trth = NULL;
4070 int rthlen;
4071
4072 if (len == 0) {
4073 ip6_clearpktopts(opt, IPV6_RTHDR);
4074 break; /* just remove the option */
4075 }
4076
4077 /* message length validation */
4078 if (len < sizeof(struct ip6_rthdr)) {
4079 return EINVAL;
4080 }
4081 rth = (struct ip6_rthdr *)(void *)buf;
4082 rthlen = (rth->ip6r_len + 1) << 3;
4083 if (len != rthlen) {
4084 return EINVAL;
4085 }
4086
4087 switch (rth->ip6r_type) {
4088 case IPV6_RTHDR_TYPE_0:
4089 if (rth->ip6r_len == 0) { /* must contain one addr */
4090 return EINVAL;
4091 }
4092 if (rth->ip6r_len % 2) { /* length must be even */
4093 return EINVAL;
4094 }
4095 if (rth->ip6r_len / 2 != rth->ip6r_segleft) {
4096 return EINVAL;
4097 }
4098 break;
4099 default:
4100 return EINVAL; /* not supported */
4101 }
4102
4103 /* turn off the previous option */
4104 ip6_clearpktopts(opt, IPV6_RTHDR);
4105
4106 trth = kalloc_data(rthlen, Z_NOWAIT);
4107 if (trth == NULL) {
4108 return ENOBUFS;
4109 }
4110 bcopy(rth, trth, rthlen);
4111 opt->ip6po_rthdr = trth;
4112 capture_exthdrstat_out = TRUE;
4113 break;
4114 }
4115
4116 case IPV6_USE_MIN_MTU:
4117 if (len != sizeof(int)) {
4118 return EINVAL;
4119 }
4120 minmtupolicy = *(int *)(void *)buf;
4121 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
4122 minmtupolicy != IP6PO_MINMTU_DISABLE &&
4123 minmtupolicy != IP6PO_MINMTU_ALL) {
4124 return EINVAL;
4125 }
4126 opt->ip6po_minmtu = (int8_t)minmtupolicy;
4127 break;
4128
4129 case IPV6_DONTFRAG:
4130 if (len != sizeof(int)) {
4131 return EINVAL;
4132 }
4133
4134 if (uproto == IPPROTO_TCP || *(int *)(void *)buf == 0) {
4135 /*
4136 * we ignore this option for TCP sockets.
4137 * (RFC3542 leaves this case unspecified.)
4138 */
4139 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
4140 } else {
4141 opt->ip6po_flags |= IP6PO_DONTFRAG;
4142 }
4143 break;
4144
4145 case IPV6_PREFER_TEMPADDR:
4146 if (len != sizeof(int)) {
4147 return EINVAL;
4148 }
4149 preftemp = *(int *)(void *)buf;
4150 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
4151 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
4152 preftemp != IP6PO_TEMPADDR_PREFER) {
4153 return EINVAL;
4154 }
4155 opt->ip6po_prefer_tempaddr = (int8_t)preftemp;
4156 break;
4157
4158 default:
4159 return ENOPROTOOPT;
4160 } /* end of switch */
4161
4162 if (capture_exthdrstat_out) {
4163 if (uproto == IPPROTO_TCP) {
4164 INC_ATOMIC_INT64_LIM(net_api_stats.nas_sock_inet6_stream_exthdr_out);
4165 } else if (uproto == IPPROTO_UDP) {
4166 INC_ATOMIC_INT64_LIM(net_api_stats.nas_sock_inet6_dgram_exthdr_out);
4167 }
4168 }
4169
4170 return 0;
4171 }
4172
4173 /*
4174 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
4175 * packet to the input queue of a specified interface. Note that this
4176 * calls the output routine of the loopback "driver", but with an interface
4177 * pointer that might NOT be &loif -- easier than replicating that code here.
4178 */
4179 void
ip6_mloopback(struct ifnet * srcifp,struct ifnet * origifp,struct mbuf * m,struct sockaddr_in6 * dst,uint32_t optlen,int32_t nxt0)4180 ip6_mloopback(struct ifnet *srcifp, struct ifnet *origifp, struct mbuf *m,
4181 struct sockaddr_in6 *dst, uint32_t optlen, int32_t nxt0)
4182 {
4183 mbuf_ref_t copym;
4184 struct ip6_hdr *__single ip6;
4185 struct in6_addr src;
4186
4187 if (lo_ifp == NULL) {
4188 return;
4189 }
4190
4191 /*
4192 * Copy the packet header as it's needed for the checksum.
4193 * Make sure to deep-copy IPv6 header portion in case the data
4194 * is in an mbuf cluster, so that we can safely override the IPv6
4195 * header portion later.
4196 */
4197 copym = m_copym_mode(m, 0, M_COPYALL, M_DONTWAIT, NULL, NULL, M_COPYM_COPY_HDR);
4198 if (copym != NULL && ((copym->m_flags & M_EXT) ||
4199 copym->m_len < sizeof(struct ip6_hdr))) {
4200 copym = m_pullup(copym, sizeof(struct ip6_hdr));
4201 }
4202
4203 if (copym == NULL) {
4204 return;
4205 }
4206
4207 ip6 = mtod(copym, struct ip6_hdr *);
4208 src = ip6->ip6_src;
4209 /*
4210 * clear embedded scope identifiers if necessary.
4211 * in6_clearscope will touch the addresses only when necessary.
4212 */
4213 in6_clearscope(&ip6->ip6_src);
4214 in6_clearscope(&ip6->ip6_dst);
4215
4216 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA) {
4217 in6_delayed_cksum_offset(copym, 0, optlen, nxt0);
4218 }
4219
4220 /*
4221 * Stuff the 'real' ifp into the pkthdr, to be used in matching
4222 * in ip6_input(); we need the loopback ifp/dl_tag passed as args
4223 * to make the loopback driver compliant with the data link
4224 * requirements.
4225 */
4226 copym->m_pkthdr.rcvif = origifp;
4227
4228 /*
4229 * Also record the source interface (which owns the source address).
4230 * This is basically a stripped down version of ifa_foraddr6().
4231 */
4232 if (srcifp == NULL) {
4233 struct in6_ifaddr *__single ia;
4234
4235 lck_rw_lock_shared(&in6_ifaddr_rwlock);
4236 TAILQ_FOREACH(ia, IN6ADDR_HASH(&src), ia6_hash) {
4237 IFA_LOCK_SPIN(&ia->ia_ifa);
4238 /* compare against src addr with embedded scope */
4239 if (in6_are_addr_equal_scoped(&ia->ia_addr.sin6_addr, &src, ia->ia_addr.sin6_scope_id, ip6_output_getsrcifscope(m))) {
4240 srcifp = ia->ia_ifp;
4241 IFA_UNLOCK(&ia->ia_ifa);
4242 break;
4243 }
4244 IFA_UNLOCK(&ia->ia_ifa);
4245 }
4246 lck_rw_done(&in6_ifaddr_rwlock);
4247 }
4248 if (srcifp != NULL) {
4249 ip6_setsrcifaddr_info(copym, srcifp->if_index, NULL);
4250 }
4251 ip6_setdstifaddr_info(copym, origifp->if_index, NULL);
4252
4253 dlil_output(lo_ifp, PF_INET6, copym, NULL, SA(dst), 0, NULL);
4254 }
4255
4256 /*
4257 * Chop IPv6 header off from the payload.
4258 */
4259 static int
ip6_splithdr(struct mbuf * m,struct ip6_exthdrs * exthdrs)4260 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
4261 {
4262 mbuf_ref_t mh;
4263 struct ip6_hdr *ip6;
4264
4265 ip6 = mtod(m, struct ip6_hdr *);
4266 if (m->m_len > sizeof(*ip6)) {
4267 MGETHDR(mh, M_DONTWAIT, MT_HEADER); /* MAC-OK */
4268 if (mh == NULL) {
4269 m_freem(m);
4270 return ENOBUFS;
4271 }
4272 M_COPY_PKTHDR(mh, m);
4273 MH_ALIGN(mh, sizeof(*ip6));
4274 m->m_flags &= ~M_PKTHDR;
4275 m->m_len -= sizeof(*ip6);
4276 m->m_data += sizeof(*ip6);
4277 mh->m_next = m;
4278 m = mh;
4279 m->m_len = sizeof(*ip6);
4280 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
4281 }
4282 exthdrs->ip6e_ip6 = m;
4283 return 0;
4284 }
4285
4286 static void
ip6_output_checksum(struct ifnet * ifp,uint32_t mtu,struct mbuf * m,int nxt0,uint32_t tlen,uint32_t optlen)4287 ip6_output_checksum(struct ifnet *ifp, uint32_t mtu, struct mbuf *m,
4288 int nxt0, uint32_t tlen, uint32_t optlen)
4289 {
4290 uint32_t sw_csum, hwcap = ifp->if_hwassist;
4291
4292 if (!hwcksum_tx) {
4293 /* do all in software; checksum offload is disabled */
4294 sw_csum = CSUM_DELAY_IPV6_DATA & m->m_pkthdr.csum_flags;
4295 } else {
4296 /* do in software what the hardware cannot */
4297 sw_csum = m->m_pkthdr.csum_flags & ~IF_HWASSIST_CSUM_FLAGS(hwcap);
4298 }
4299
4300 if (optlen != 0) {
4301 sw_csum |= (CSUM_DELAY_IPV6_DATA &
4302 m->m_pkthdr.csum_flags);
4303 } else if ((sw_csum & CSUM_DELAY_IPV6_DATA) && (hwcap & CSUM_PARTIAL)) {
4304 /*
4305 * Partial checksum offload, ere), if no extension headers,
4306 * and TCP only (no UDP support, as the hardware may not be
4307 * able to convert +0 to -0 (0xffff) per RFC1122 4.1.3.4.
4308 * unless the interface supports "invert zero" capability.)
4309 */
4310 if (hwcksum_tx &&
4311 ((m->m_pkthdr.csum_flags & CSUM_TCPIPV6) ||
4312 ((hwcap & CSUM_ZERO_INVERT) &&
4313 (m->m_pkthdr.csum_flags & CSUM_ZERO_INVERT))) &&
4314 tlen <= mtu) {
4315 uint16_t start = sizeof(struct ip6_hdr);
4316 uint16_t ulpoff =
4317 m->m_pkthdr.csum_data & 0xffff;
4318 m->m_pkthdr.csum_flags |=
4319 (CSUM_DATA_VALID | CSUM_PARTIAL);
4320 m->m_pkthdr.csum_tx_stuff = (ulpoff + start);
4321 m->m_pkthdr.csum_tx_start = start;
4322 sw_csum = 0;
4323 } else {
4324 sw_csum |= (CSUM_DELAY_IPV6_DATA &
4325 m->m_pkthdr.csum_flags);
4326 }
4327 }
4328
4329 if (sw_csum & CSUM_DELAY_IPV6_DATA) {
4330 in6_delayed_cksum_offset(m, 0, optlen, nxt0);
4331 sw_csum &= ~CSUM_DELAY_IPV6_DATA;
4332 }
4333
4334 if (hwcksum_tx) {
4335 uint32_t delay_data = m->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA;
4336 uint32_t hw_csum = IF_HWASSIST_CSUM_FLAGS(hwcap);
4337
4338 /*
4339 * Drop off bits that aren't supported by hardware;
4340 * also make sure to preserve non-checksum related bits.
4341 */
4342 m->m_pkthdr.csum_flags =
4343 ((m->m_pkthdr.csum_flags & (hw_csum | CSUM_DATA_VALID)) |
4344 (m->m_pkthdr.csum_flags & ~IF_HWASSIST_CSUM_MASK));
4345
4346 /*
4347 * If hardware supports partial checksum but not delay_data,
4348 * add back delay_data.
4349 */
4350 if ((hw_csum & CSUM_PARTIAL) != 0 &&
4351 (hw_csum & delay_data) == 0) {
4352 m->m_pkthdr.csum_flags |= delay_data;
4353 }
4354 } else {
4355 /* drop all bits; checksum offload is disabled */
4356 m->m_pkthdr.csum_flags = 0;
4357 }
4358 }
4359
4360 /*
4361 * Compute IPv6 extension header length.
4362 */
4363 int
ip6_optlen(struct in6pcb * in6p)4364 ip6_optlen(struct in6pcb *in6p)
4365 {
4366 int len;
4367
4368 if (!in6p->in6p_outputopts) {
4369 return 0;
4370 }
4371
4372 len = 0;
4373 #define elen(x) \
4374 (((struct ip6_ext *)(x)) ? \
4375 (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
4376
4377 len += elen(in6p->in6p_outputopts->ip6po_hbh);
4378 if (in6p->in6p_outputopts->ip6po_rthdr) {
4379 /* dest1 is valid with rthdr only */
4380 len += elen(in6p->in6p_outputopts->ip6po_dest1);
4381 }
4382 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
4383 len += elen(in6p->in6p_outputopts->ip6po_dest2);
4384 return len;
4385 #undef elen
4386 }
4387
4388 static int
4389 sysctl_reset_ip6_output_stats SYSCTL_HANDLER_ARGS
4390 {
4391 #pragma unused(arg1, arg2)
4392 int error, i;
4393
4394 i = ip6_output_measure;
4395 error = sysctl_handle_int(oidp, &i, 0, req);
4396 if (error || req->newptr == USER_ADDR_NULL) {
4397 goto done;
4398 }
4399 /* impose bounds */
4400 if (i < 0 || i > 1) {
4401 error = EINVAL;
4402 goto done;
4403 }
4404 if (ip6_output_measure != i && i == 1) {
4405 net_perf_initialize(&net_perf, ip6_output_measure_bins);
4406 }
4407 ip6_output_measure = i;
4408 done:
4409 return error;
4410 }
4411
4412 static int
4413 sysctl_ip6_output_measure_bins SYSCTL_HANDLER_ARGS
4414 {
4415 #pragma unused(arg1, arg2)
4416 int error;
4417 uint64_t i;
4418
4419 i = ip6_output_measure_bins;
4420 error = sysctl_handle_quad(oidp, &i, 0, req);
4421 if (error || req->newptr == USER_ADDR_NULL) {
4422 goto done;
4423 }
4424 /* validate data */
4425 if (!net_perf_validate_bins(i)) {
4426 error = EINVAL;
4427 goto done;
4428 }
4429 ip6_output_measure_bins = i;
4430 done:
4431 return error;
4432 }
4433
4434 static int
4435 sysctl_ip6_output_getperf SYSCTL_HANDLER_ARGS
4436 {
4437 #pragma unused(oidp, arg1, arg2)
4438 if (req->oldptr == USER_ADDR_NULL) {
4439 req->oldlen = (size_t)sizeof(struct ipstat);
4440 }
4441
4442 return SYSCTL_OUT(req, &net_perf, MIN(sizeof(net_perf), req->oldlen));
4443 }
4444
4445 void
ip6_output_setsrcifscope(struct mbuf * m,uint32_t src_idx,struct in6_ifaddr * ia6)4446 ip6_output_setsrcifscope(struct mbuf *m, uint32_t src_idx, struct in6_ifaddr *ia6)
4447 {
4448 VERIFY(m->m_flags & M_PKTHDR);
4449
4450 m->m_pkthdr.pkt_ext_flags |= PKTF_EXT_OUTPUT_SCOPE;
4451 if (ia6 != NULL) {
4452 m->m_pkthdr.src_ifindex = ia6->ia_ifp->if_index;
4453 } else {
4454 m->m_pkthdr.src_ifindex = (uint16_t)src_idx;
4455 }
4456 }
4457
4458 void
ip6_output_setdstifscope(struct mbuf * m,uint32_t dst_idx,struct in6_ifaddr * ia6)4459 ip6_output_setdstifscope(struct mbuf *m, uint32_t dst_idx, struct in6_ifaddr *ia6)
4460 {
4461 VERIFY(m->m_flags & M_PKTHDR);
4462
4463 m->m_pkthdr.pkt_ext_flags |= PKTF_EXT_OUTPUT_SCOPE;
4464 if (ia6 != NULL) {
4465 m->m_pkthdr.dst_ifindex = ia6->ia_ifp->if_index;
4466 } else {
4467 m->m_pkthdr.dst_ifindex = (uint16_t)dst_idx;
4468 }
4469 }
4470
4471 uint32_t
ip6_output_getsrcifscope(struct mbuf * m)4472 ip6_output_getsrcifscope(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.src_ifindex;
4481 }
4482
4483 uint32_t
ip6_output_getdstifscope(struct mbuf * m)4484 ip6_output_getdstifscope(struct mbuf *m)
4485 {
4486 VERIFY(m->m_flags & M_PKTHDR);
4487 if (in6_embedded_scope_debug) {
4488 VERIFY(m->m_pkthdr.pkt_ext_flags & PKTF_EXT_OUTPUT_SCOPE);
4489 VERIFY((m->m_pkthdr.pkt_flags & PKTF_IFAINFO) == 0);
4490 }
4491
4492 return m->m_pkthdr.dst_ifindex;
4493 }
4494