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