2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
59 * Copyright (c) 1982, 1986, 1988, 1990, 1993
60 * The Regents of the University of California. All rights reserved.
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
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.
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
90 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
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,
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>
115 #include <pexpert/pexpert.h>
116 #include <mach/sdt.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>
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>
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>
141 #include <netinet6/ipsec.h>
142 #include <netinet6/ipsec6.h>
143 #include <netkey/key.h>
144 extern int ipsec_bypass
;
148 #include <net/necp.h>
152 #include <netinet/ip_dummynet.h>
153 #endif /* DUMMYNET */
156 #include <net/pfvar.h>
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,
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,
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
);
187 SYSCTL_DECL(_net_inet6_ip6
);
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");
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");
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)");
206 #define IM6O_TRACE_HIST_SIZE 32 /* size of trace history */
209 __private_extern__
unsigned int im6o_trace_hist_size
= IM6O_TRACE_HIST_SIZE
;
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 */
216 * Alloc and free callers.
221 * Circular lists of IM6O_ADDREF and IM6O_REMREF callers.
223 ctrace_t im6o_refhold
[IM6O_TRACE_HIST_SIZE
];
224 ctrace_t im6o_refrele
[IM6O_TRACE_HIST_SIZE
];
228 static unsigned int im6o_debug
= 1; /* debugging (enabled) */
230 static unsigned int im6o_debug
; /* debugging (disabled) */
233 static struct zone
*im6o_zone
; /* zone for ip6_moptions */
234 #define IM6O_ZONE_NAME "ip6_moptions" /* zone name */
237 * ip6_output() calls ip6_output_list() to do the work
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
)
244 return ip6_output_list(m0
, 0, opt
, ro
, flags
, im6o
, ifpp
, ip6oa
);
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.
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.
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.
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
)
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
;
275 struct route_in6
*ro_pmtu
= NULL
;
276 struct rtentry
*rt
= NULL
;
277 struct sockaddr_in6
*dst
= NULL
, src_sa
, dst_sa
;
279 struct in6_ifaddr
*ia
= NULL
, *src_ia
= NULL
;
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
;
288 uint32_t packets_processed
= 0;
289 struct timeval start_tv
;
291 boolean_t skip_pf
= (ip6oa
!= NULL
) &&
292 (ip6oa
->ip6oa_flags
& IP6OAF_SKIP_PF
);
297 struct ip6_out_args saved_ip6oa
;
298 struct sockaddr_in6 dst_buf
;
299 #endif /* DUMMYNET */
301 struct socket
*so
= NULL
;
302 struct secpolicy
*sp
= NULL
;
303 struct route_in6
*ipsec_saved_route
= NULL
;
304 boolean_t needipsectun
= FALSE
;
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;
312 struct ipf_pktopts ipf_pktopts
;
313 struct ip6_exthdrs exthdrs
;
314 struct route_in6 ip6route
;
316 struct ipsec_output_state ipsec_state
;
319 struct route_in6 necp_route
;
322 struct route_in6 saved_route
;
323 struct route_in6 saved_ro_pmtu
;
324 struct ip_fw_args args
;
325 #endif /* DUMMYNET */
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
337 boolean_t select_srcif
: 1;
338 boolean_t hdrsplit
: 1;
339 boolean_t route_selected
: 1;
340 boolean_t dontfrag
: 1;
342 boolean_t needipsec
: 1;
343 boolean_t noipsec
: 1;
347 } ip6obf
= { .raw
= 0 };
349 if (ip6_output_measure
) {
350 net_perf_start_time(&net_perf
, &start_tv
);
353 VERIFY(m0
->m_flags
& M_PKTHDR
);
355 /* zero out {saved_route, saved_ro_pmtu, ip6route, exthdrs, args} */
356 bzero(&ip6obz
, sizeof(ip6obz
));
359 if (SLIST_EMPTY(&m0
->m_pkthdr
.tags
)) {
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
;
369 * ip6_output_list() cannot handle chains of packets reinjected
370 * by dummynet. The same restriction applies to
373 VERIFY(0 == packetchain
);
375 dn_tag
= (struct dn_pkt_tag
*)(tag
+ 1);
376 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
378 bcopy(&dn_tag
->dn_dst6
, &dst_buf
, sizeof(dst_buf
));
380 ifp
= dn_tag
->dn_ifp
;
382 ifnet_reference(ifp
);
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
;
390 saved_route
= dn_tag
->dn_ro6
;
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
);
398 mtu
= dn_tag
->dn_mtu
;
399 unfragpartlen
= dn_tag
->dn_unfragpartlen
;
401 bcopy(&dn_tag
->dn_exthdrs
, &exthdrs
, sizeof(exthdrs
));
403 m_tag_delete(m0
, tag
);
407 #endif /* DUMMYNET */
412 if (ipsec_bypass
== 0) {
413 so
= ipsec_getsocket(m
);
415 (void) ipsec_setsocket(m
, NULL
);
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 */
424 if (ipsec6_getpolicybyinterface(m
, IPSEC_DIR_OUTBOUND
,
425 flags
, ip6oa
, &noipsec
, &sp
) != 0) {
429 ip6obf
.noipsec
= (noipsec
!= 0);
436 if (flags
& IPV6_OUTARGS
) {
438 * In the forwarding case, only the ifscope value is used,
439 * as source interface selection doesn't take place.
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
;
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
));
453 if (ip6oa
->ip6oa_flags
& IP6OAF_BOUND_SRCADDR
) {
454 ipf_pktopts
.ippo_flags
|= IPPOF_BOUND_SRCADDR
;
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
);
465 if (flags
& IPV6_OUTARGS
) {
466 if (ip6oa
->ip6oa_flags
& IP6OAF_NO_CELLULAR
) {
467 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFT_CELLULAR
;
469 if (ip6oa
->ip6oa_flags
& IP6OAF_NO_EXPENSIVE
) {
470 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFF_EXPENSIVE
;
472 if (ip6oa
->ip6oa_flags
& IP6OAF_NO_CONSTRAINED
) {
473 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFF_CONSTRAINED
;
475 adv
= &ip6oa
->ip6oa_flowadv
;
476 adv
->code
= FADV_SUCCESS
;
477 ip6oa
->ip6oa_retflags
= 0;
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.
485 if (ifpp
!= NULL
&& *ifpp
!= NULL
) {
490 if (args
.fwa_pf_rule
) {
491 ip6
= mtod(m
, struct ip6_hdr
*);
492 VERIFY(ro
!= NULL
); /* ro == saved_route */
495 #endif /* DUMMYNET */
499 * Since all packets are assumed to come from same socket, necp lookup
500 * only needs to happen once per function entry.
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
);
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.
513 * Remove m from the chain during processing to avoid
514 * accidental frees on entire list.
516 inputchain
= m
->m_nextpkt
;
522 m
->m_pkthdr
.pkt_flags
&= ~(PKTF_LOOP
| PKTF_IFAINFO
);
523 ip6
= mtod(m
, struct ip6_hdr
*);
525 finaldst
= ip6
->ip6_dst
;
526 ip6obf
.hdrsplit
= FALSE
;
529 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
530 inject_filter_ref
= ipf_get_inject_filter(m
);
532 inject_filter_ref
= NULL
;
535 #define MAKE_EXTHDR(hp, mp) do { \
537 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
538 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
539 ((eh)->ip6e_len + 1) << 3); \
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
) {
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
560 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
563 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
564 /* Destination options header(2nd part) */
565 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
571 if (necp_matched_policy_id
) {
572 necp_mark_packet_from_ip(m
, necp_matched_policy_id
);
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
) {
580 case NECP_KERNEL_POLICY_RESULT_DROP
:
581 error
= EHOSTUNREACH
;
582 ip6stat
.ip6s_necp_policy_drop
++;
584 case NECP_KERNEL_POLICY_RESULT_SOCKET_DIVERT
:
586 * Flow divert packets should be blocked at the IP
589 error
= EHOSTUNREACH
;
590 ip6stat
.ip6s_necp_policy_drop
++;
592 case NECP_KERNEL_POLICY_RESULT_IP_TUNNEL
: {
594 * Verify that the packet is being routed to the tunnel
596 struct ifnet
*policy_ifp
=
597 necp_get_ifnet_from_result_parameter(
598 &necp_result_parameter
);
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)
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
);
611 ip6oa
->ip6oa_flags
|= IP6OAF_QOSMARKING_ALLOWED
;
613 ip6oa
->ip6oa_flags
&= ~IP6OAF_QOSMARKING_ALLOWED
;
617 if (policy_ifp
== ifp
) {
620 if (necp_packet_can_rebind_to_ifnet(m
,
621 policy_ifp
, (struct route
*)&necp_route
,
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.
631 if (flags
& IPV6_OUTARGS
) {
632 ip6oa
->ip6oa_boundif
=
633 policy_ifp
->if_index
;
634 ip6oa
->ip6oa_flags
|=
638 && opt
->ip6po_pktinfo
!= NULL
) {
641 policy_ifp
->if_index
;
647 ip6stat
.ip6s_necp_policy_drop
++;
659 if (ipsec_bypass
!= 0 || ip6obf
.noipsec
) {
664 /* get a security policy for this packet */
666 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
,
669 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
,
673 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
681 switch (sp
->policy
) {
682 case IPSEC_POLICY_DISCARD
:
683 case IPSEC_POLICY_GENERATE
:
685 * This packet is just discarded.
687 IPSEC_STAT_INCREMENT(ipsec6stat
.out_polvio
);
690 case IPSEC_POLICY_BYPASS
:
691 case IPSEC_POLICY_NONE
:
692 /* no need to do IPsec. */
693 ip6obf
.needipsec
= FALSE
;
696 case IPSEC_POLICY_IPSEC
:
697 if (sp
->req
== NULL
) {
698 /* acquire a policy */
699 error
= key_spdacquire(sp
);
705 ip6obf
.needipsec
= TRUE
;
709 case IPSEC_POLICY_ENTRUST
:
711 printf("%s: Invalid policy found: %d\n", __func__
, sp
->policy
);
718 * Calculate the total length of the extension header chain.
719 * Keep the length of the unfragmentable part for fragmentation.
722 if (exthdrs
.ip6e_hbh
!= NULL
) {
723 optlen
+= exthdrs
.ip6e_hbh
->m_len
;
725 if (exthdrs
.ip6e_dest1
!= NULL
) {
726 optlen
+= exthdrs
.ip6e_dest1
->m_len
;
728 if (exthdrs
.ip6e_rthdr
!= NULL
) {
729 optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
731 unfragpartlen
= optlen
+ sizeof(struct ip6_hdr
);
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
;
739 * If we need IPsec, or there is at least one extension header,
740 * separate IP6 header from the payload.
746 optlen
) && !ip6obf
.hdrsplit
) {
747 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
751 m
= exthdrs
.ip6e_ip6
;
752 ip6obf
.hdrsplit
= TRUE
;
756 ip6
= mtod(m
, struct ip6_hdr
*);
758 /* adjust mbuf packet header length */
759 m
->m_pkthdr
.len
+= optlen
;
760 plen
= m
->m_pkthdr
.len
- sizeof(*ip6
);
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) {
769 m
= exthdrs
.ip6e_ip6
;
770 ip6obf
.hdrsplit
= TRUE
;
773 ip6
= mtod(m
, struct ip6_hdr
*);
774 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0) {
779 ip6
->ip6_plen
= htons((uint16_t)plen
);
782 * Concatenate headers and fill in next header fields.
783 * Here we have, on "m"
785 * and we insert headers accordingly. Finally, we should be getting:
786 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
788 * during the header composing process, "m" points to IPv6 header.
789 * "mprev" points to an extension header prior to esp.
791 nexthdrp
= &ip6
->ip6_nxt
;
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.
799 * result: IPv6 dest2 payload
800 * m and mprev will point to IPv6 header.
802 if (exthdrs
.ip6e_dest2
!= NULL
) {
803 if (!ip6obf
.hdrsplit
) {
804 panic("assumption failed: hdr not split");
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
;
813 #define MAKE_CHAIN(m, mp, p, i) do { \
815 if (!ip6obf.hdrsplit) { \
816 panic("assumption failed: hdr not split"); \
819 *mtod((m), u_char *) = *(p); \
821 p = mtod((m), u_char *); \
822 (m)->m_next = (mp)->m_next; \
823 (mp)->m_next = (m); \
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).
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
);
836 /* It is no longer safe to free the pointers in exthdrs. */
837 exthdrs
.merged
= TRUE
;
842 if (ip6obf
.needipsec
&& (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
)) {
843 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
847 if (!TAILQ_EMPTY(&ipv6_filters
) &&
848 !((flags
& IPV6_OUTARGS
) &&
849 (ip6oa
->ip6oa_flags
& IP6OAF_INTCOPROC_ALLOWED
)
851 && !necp_packet_should_skip_filters(m
)
854 struct ipfilter
*filter
;
855 int seen
= (inject_filter_ref
== NULL
);
858 if (im6o
!= NULL
&& IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
859 ippo
->ippo_flags
|= IPPOF_MCAST_OPTS
;
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
;
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
)) {
871 ip6
->ip6_dst
.s6_addr16
[1] =
872 htons((uint16_t)ro
->ro_dst
.sin6_scope_id
);
876 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
878 * Don't process packet twice if we've already seen it.
881 if ((struct ipfilter
*)inject_filter_ref
==
885 } else if (filter
->ipf_filter
.ipf_output
!= NULL
) {
888 result
= filter
->ipf_filter
.ipf_output(
889 filter
->ipf_filter
.cookie
,
891 if (result
== EJUSTRETURN
) {
904 ip6
= mtod(m
, struct ip6_hdr
*);
905 /* Hack: cleanup embedded scope_id if we put it there */
907 ip6
->ip6_dst
.s6_addr16
[1] = 0;
912 if (ip6obf
.needipsec
) {
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)
920 exthdrs
.ip6e_dest2
= NULL
;
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;
932 error
= ipsec6_output_trans(&ipsec_state
, nexthdrp
, mprev
,
933 sp
, flags
, &needipsectun
);
936 /* mbuf is already reclaimed in ipsec6_output_trans. */
946 printf("ip6_output (ipsec): error code %d\n",
950 /* don't show these error codes to the user */
956 if (exthdrs
.ip6e_rthdr
!= NULL
) {
957 /* ah6_output doesn't modify mbuf chain */
958 rh
->ip6r_segleft
= segleft_org
;
963 /* If there is a routing header, discard the packet. */
964 if (exthdrs
.ip6e_rthdr
!= NULL
) {
969 /* Source address validation */
970 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
971 !(flags
& IPV6_UNSPECSRC
)) {
973 ip6stat
.ip6s_badscope
++;
976 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
978 ip6stat
.ip6s_badscope
++;
982 ip6stat
.ip6s_localout
++;
989 bzero((caddr_t
)ro
, sizeof(*ro
));
992 if (opt
!= NULL
&& opt
->ip6po_rthdr
) {
993 ro
= &opt
->ip6po_route
;
995 dst
= SIN6(&ro
->ro_dst
);
997 if (ro
->ro_rt
!= NULL
) {
998 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
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.
1005 if (opt
!= NULL
&& opt
->ip6po_tclass
>= 0) {
1008 if ((ip6
->ip6_flow
& htonl(0xfc << 20)) == 0) {
1011 if ((ip6
->ip6_flow
& htonl(0x03 << 20)) == 0) {
1016 htonl((opt
->ip6po_tclass
& mask
) << 20);
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
)) {
1026 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
1029 ip6
->ip6_hlim
= (uint8_t)ip6_defmcasthlim
;
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
1041 if (ROUTE_UNUSABLE(ro
) || dst
->sin6_family
!= AF_INET6
||
1042 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
)) {
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
;
1053 if (ip6obf
.needipsec
&& needipsectun
) {
1055 struct ifnet
*trace_ifp
= (ifpp_save
!= NULL
) ? (*ifpp_save
) : NULL
;
1056 #endif /* CONFIG_DTRACE */
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).
1063 * IPv6 [ESP|AH] IPv6 [extension headers] payload
1065 bzero(&exthdrs
, sizeof(exthdrs
));
1066 exthdrs
.ip6e_ip6
= m
;
1069 route_copyout((struct route
*)&ipsec_state
.ro
, (struct route
*)ro
,
1070 sizeof(struct route_in6
));
1071 ipsec_state
.dst
= SA(dst
);
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
);
1078 error
= ipsec6_output_tunnel(&ipsec_state
, sp
, flags
);
1079 /* tunneled in IPv4? packet is gone */
1080 if (ipsec_state
.tunneled
== 4) {
1085 ipsec_saved_route
= ro
;
1086 ro
= (struct route_in6
*)&ipsec_state
.ro
;
1087 dst
= SIN6(ipsec_state
.dst
);
1089 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
1099 printf("ip6_output (ipsec): error code %d\n",
1103 /* don't show these error codes to the user */
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.
1114 if (flags
& IPV6_OUTARGS
) {
1115 ip6oa
->ip6oa_boundif
= IFSCOPE_NONE
;
1116 ip6oa
->ip6oa_flags
&= ~IP6OAF_BOUND_IF
;
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
;
1123 exthdrs
.ip6e_ip6
= m
;
1128 * ifp should only be filled in for dummy net packets which will jump
1129 * to check_with_pf label.
1132 VERIFY(ip6obf
.route_selected
);
1135 /* adjust pointer */
1136 ip6
= mtod(m
, struct ip6_hdr
*);
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
;
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
;
1150 * Only call in6_selectroute() on first iteration to avoid taking
1151 * multiple references on ifp and rt.
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.
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
);
1164 ip6stat
.ip6s_noroute
++;
1168 break; /* XXX statistics? */
1171 in6_ifstat_inc(ifp
, ifs6_out_discard
);
1173 /* ifp (if non-NULL) will be released at the end */
1176 ip6obf
.route_selected
= TRUE
;
1180 * If in6_selectroute() does not return a route entry,
1181 * dst may not have been updated.
1183 *dst
= dst_sa
; /* XXX */
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
++;
1196 * then rt (for unicast) and ifp must be non-NULL valid values.
1198 if (!(flags
& IPV6_FORWARDING
)) {
1199 in6_ifstat_inc_na(ifp
, ifs6_out_request
);
1204 ia
= (struct in6_ifaddr
*)(rt
->rt_ifa
);
1206 IFA_ADDREF(&ia
->ia_ifa
);
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.
1219 if (ia
!= NULL
&& ia
->ia_ifp
) {
1220 ifnet_reference(ia
->ia_ifp
); /* for origifp */
1221 if (origifp
!= NULL
) {
1222 ifnet_release(origifp
);
1224 origifp
= ia
->ia_ifp
;
1227 ifnet_reference(ifp
); /* for origifp */
1229 if (origifp
!= NULL
) {
1230 ifnet_release(origifp
);
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
;
1240 src0
= ip6
->ip6_src
;
1241 if (in6_setscope(&src0
, origifp
, &zone
)) {
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
)) {
1253 dst0
= ip6
->ip6_dst
;
1254 if ((in6_setscope(&dst0
, origifp
, &zone
))) {
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
)) {
1267 /* scope check is done. */
1271 ip6stat
.ip6s_badscope
++;
1272 in6_ifstat_inc(origifp
, ifs6_out_discard
);
1274 error
= EHOSTUNREACH
; /* XXX */
1280 if (rt
!= NULL
&& !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1281 if (opt
!= NULL
&& opt
->ip6po_nextroute
.ro_rt
) {
1283 * The nexthop is explicitly specified by the
1284 * application. We assume the next hop is an IPv6
1287 dst
= SIN6(opt
->ip6po_nexthop
);
1288 } else if ((rt
->rt_flags
& RTF_GATEWAY
)) {
1289 dst
= SIN6(rt
->rt_gateway
);
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().
1298 if (ia
!= NULL
&& (ifp
->if_flags
& IFF_LOOPBACK
)) {
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
;
1309 ip6_setsrcifaddr_info(m
, srcidx
, NULL
);
1310 ip6_setdstifaddr_info(m
, 0, ia
);
1314 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1315 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
1317 struct in6_multi
*in6m
;
1319 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
1320 in6_ifstat_inc_na(ifp
, ifs6_out_mcast
);
1323 * Confirm that the outgoing interface supports multicast.
1325 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
1326 ip6stat
.ip6s_noroute
++;
1327 in6_ifstat_inc(ifp
, ifs6_out_discard
);
1328 error
= ENETUNREACH
;
1331 in6_multihead_lock_shared();
1332 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, in6m
);
1333 in6_multihead_lock_done();
1338 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
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.
1347 ip6_mloopback(NULL
, ifp
, m
, dst
, optlen
, nxt0
);
1348 } else if (im6o
!= NULL
) {
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.
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 */
1374 * Fill the outgoing inteface to tell the upper layer
1375 * to increment per-interface statistics.
1377 if (ifpp
!= NULL
&& *ifpp
== NULL
) {
1378 ifnet_reference(ifp
); /* for caller */
1382 /* Determine path MTU. */
1383 if ((error
= ip6_getpmtu(ro_pmtu
, ro
, ifp
, &finaldst
, &mtu
)) != 0) {
1388 * The caller of this function may specify to use the minimum MTU
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.
1399 if (mtu
> IPV6_MMTU
) {
1400 if ((flags
& IPV6_MINMTU
)) {
1402 } else if (opt
&& opt
->ip6po_minmtu
== IP6PO_MINMTU_ALL
) {
1404 } else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) &&
1406 opt
->ip6po_minmtu
!= IP6PO_MINMTU_DISABLE
)) {
1412 * clear embedded scope identifiers if necessary.
1413 * in6_clearscope will touch the addresses only when necessary.
1415 in6_clearscope(&ip6
->ip6_src
);
1416 in6_clearscope(&ip6
->ip6_dst
);
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.)
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() */
1427 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
) {
1428 panic("ip6e_hbh is not continuous");
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.
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) {
1443 * m was already freed at this point. Set to NULL so it
1444 * is not re-freed at end of ip6_output_list.
1447 error
= EINVAL
; /* better error? */
1450 m
->m_flags
&= ~M_LOOP
; /* XXX */
1451 m
->m_pkthdr
.rcvif
= NULL
;
1456 #endif /* DUMMYNET */
1458 if (PF_IS_ENABLED
&& !skip_pf
) {
1462 * TODO: Need to save opt->ip6po_flags for reinjection
1467 args
.fwa_oflags
= flags
;
1468 if (flags
& IPV6_OUTARGS
) {
1469 args
.fwa_ip6oa
= ip6oa
;
1472 args
.fwa_dst6
= dst
;
1473 args
.fwa_ro6_pmtu
= ro_pmtu
;
1474 args
.fwa_origifp
= origifp
;
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 */
1484 if (error
!= 0 || m
== NULL
) {
1486 panic("%s: unexpected packet %p\n",
1490 /* m was already freed by callee and is now NULL. */
1493 ip6
= mtod(m
, struct ip6_hdr
*);
1498 /* clean ipsec history before fragmentation */
1502 if (ip6oa
!= NULL
) {
1505 dscp
= (ntohl(ip6
->ip6_flow
) & IP6FLOW_DSCP_MASK
) >> IP6FLOW_DSCP_SHIFT
;
1507 error
= set_packet_qos(m
, ifp
,
1508 ip6oa
->ip6oa_flags
& IP6OAF_QOSMARKING_ALLOWED
? TRUE
: FALSE
,
1509 ip6oa
->ip6oa_sotc
, ip6oa
->ip6oa_netsvctype
, &dscp
);
1511 ip6
->ip6_flow
&= ~htonl(IP6FLOW_DSCP_MASK
);
1512 ip6
->ip6_flow
|= htonl((u_int32_t
)dscp
<< IP6FLOW_DSCP_SHIFT
);
1514 printf("%s if_dscp_for_mbuf() error %d\n", __func__
, error
);
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
1523 error
= ip6_fragment_packet(&m
, opt
, ip6oa
,
1524 &exthdrs
, ifp
, mtu
, unfragpartlen
, ro_pmtu
, nxt0
,
1532 * The evaluateloop label is where we decide whether to continue looping over
1533 * packets or call into nd code to send.
1538 * m may be NULL when we jump to the evaluateloop label from PF or
1539 * other code that can drop packets.
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.
1546 if (sendchain
!= NULL
) {
1547 sendchain_last
->m_nextpkt
= m
;
1552 /* Fragmentation may mean m is a chain. Find the last packet. */
1553 while (m
->m_nextpkt
) {
1560 /* Fill in next m from inputchain as appropriate. */
1563 /* Isolate m from rest of input chain. */
1564 inputchain
= m
->m_nextpkt
;
1565 m
->m_nextpkt
= NULL
;
1568 * Clear exthdrs and ipsec_state so stale contents are not
1569 * reused. Note this also clears the exthdrs.merged flag.
1571 bzero(&exthdrs
, sizeof(exthdrs
));
1572 bzero(&ipsec_state
, sizeof(ipsec_state
));
1574 /* Continue looping. */
1579 * If we get here, there's no more mbufs in inputchain, so send the
1580 * sendchain if there is one.
1583 error
= nd6_output_list(ifp
, origifp
, sendchain
, dst
,
1586 * Fall through to done label even in error case because
1587 * nd6_output_list frees packetchain in both success and
1593 if (ifpp_save
!= NULL
&& *ifpp_save
!= NULL
) {
1594 ifnet_release(*ifpp_save
);
1597 ROUTE_RELEASE(&ip6route
);
1599 ROUTE_RELEASE(&ipsec_state
.ro
);
1601 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1605 ROUTE_RELEASE(&necp_route
);
1608 ROUTE_RELEASE(&saved_route
);
1609 ROUTE_RELEASE(&saved_ro_pmtu
);
1610 #endif /* DUMMYNET */
1613 IFA_REMREF(&ia
->ia_ifa
);
1615 if (src_ia
!= NULL
) {
1616 IFA_REMREF(&src_ia
->ia_ifa
);
1621 if (origifp
!= NULL
) {
1622 ifnet_release(origifp
);
1624 if (ip6_output_measure
) {
1625 net_perf_measure_time(&net_perf
, &start_tv
, packets_processed
);
1626 net_perf_histogram(&net_perf
, packets_processed
);
1631 if (exthdrs
.ip6e_hbh
!= NULL
) {
1632 if (exthdrs
.merged
) {
1633 panic("Double free of ip6e_hbh");
1635 m_freem(exthdrs
.ip6e_hbh
);
1637 if (exthdrs
.ip6e_dest1
!= NULL
) {
1638 if (exthdrs
.merged
) {
1639 panic("Double free of ip6e_dest1");
1641 m_freem(exthdrs
.ip6e_dest1
);
1643 if (exthdrs
.ip6e_rthdr
!= NULL
) {
1644 if (exthdrs
.merged
) {
1645 panic("Double free of ip6e_rthdr");
1647 m_freem(exthdrs
.ip6e_rthdr
);
1649 if (exthdrs
.ip6e_dest2
!= NULL
) {
1650 if (exthdrs
.merged
) {
1651 panic("Double free of ip6e_dest2");
1653 m_freem(exthdrs
.ip6e_dest2
);
1657 if (inputchain
!= NULL
) {
1658 m_freem_list(inputchain
);
1660 if (sendchain
!= NULL
) {
1661 m_freem_list(sendchain
);
1674 #undef saved_ro_pmtu
1678 /* ip6_fragment_packet
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
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
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
)
1696 VERIFY(NULL
!= mptr
);
1697 struct mbuf
*m
= *mptr
;
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
));
1703 if (m
->m_pkthdr
.pkt_flags
& PKTF_FORWARDED
) {
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.
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;
1717 /* Access without acquiring nd_ifinfo lock for performance */
1718 if (dontfrag
&& tlen
> IN6_LINKMTU(ifp
)) { /* case 2-b */
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
1728 struct ip6ctlparam ip6cp
;
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
);
1738 * transmit packet without fragmentation
1741 (tlen
<= mtu
|| TSO_IPV6_OK(ifp
, m
) ||
1742 (ifp
->if_hwassist
& CSUM_FRAGMENT_IPV6
))) {
1744 * mppn not updated in this case because no new chain is formed
1747 ip6_output_checksum(ifp
, mtu
, m
, nxt0
, tlen
, optlen
);
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.
1754 u_int8_t
*lexthdrsp
;
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 *);
1765 error
= ip6_do_fragmentation(mptr
, optlen
, ifp
,
1766 unfragpartlen
, mtod(m
, struct ip6_hdr
*), lexthdrsp
, mtu
,
1767 nxt0
, htonl(ip6_randomid()));
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
1784 * nxt0: upper-layer protocol number.
1785 * id: Identification value to be used in the fragment header.
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
)
1792 VERIFY(NULL
!= mptr
);
1795 struct mbuf
*morig
= *mptr
;
1796 struct mbuf
*first_mbufp
= NULL
;
1797 struct mbuf
*last_mbufp
= NULL
;
1799 uint32_t tlen
= morig
->m_pkthdr
.len
;
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
);
1806 } else if (mtu
< IPV6_MMTU
) {
1807 /* path MTU cannot be less than IPV6_MMTU */
1808 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1810 } else if (ip6
->ip6_plen
== 0) {
1811 /* jumbo payload cannot be fragmented */
1812 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1815 uint32_t hlen
, off
, len
;
1816 struct mbuf
**mnext
= NULL
;
1817 struct ip6_frag
*ip6f
;
1821 * Too large for the destination or interface;
1822 * fragment if possible.
1823 * Must be able to put at least 8 bytes per fragment.
1825 hlen
= unfragpartlen
;
1826 if (mtu
> IPV6_MAXPACKET
) {
1827 mtu
= IPV6_MAXPACKET
;
1830 len
= (mtu
- hlen
- sizeof(struct ip6_frag
)) & ~7;
1832 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1837 * Change the next header field of the last header in the
1838 * unfragmentable part.
1840 if (lexthdrsp
!= NULL
) {
1841 nextproto
= *lexthdrsp
;
1842 *lexthdrsp
= IPPROTO_FRAGMENT
;
1844 nextproto
= ip6
->ip6_nxt
;
1845 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
1848 if (morig
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
) {
1849 in6_delayed_cksum_offset(morig
, 0, optlen
, nxt0
);
1853 * Loop through length of segment after first fragment,
1854 * make new header and copy data of each part and link onto
1857 for (off
= hlen
; off
< tlen
; off
+= len
) {
1858 struct ip6_hdr
*new_mhip6
;
1860 struct mbuf
*m_frgpart
;
1862 MGETHDR(new_m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1863 if (new_m
== NULL
) {
1865 ip6stat
.ip6s_odropped
++;
1868 new_m
->m_pkthdr
.rcvif
= NULL
;
1869 new_m
->m_flags
= morig
->m_flags
& M_COPYFLAGS
;
1871 if (first_mbufp
!= NULL
) {
1872 /* Every pass through loop but first */
1876 /* This is the first element of the fragment chain */
1877 first_mbufp
= new_m
;
1880 mnext
= &new_m
->m_nextpkt
;
1882 new_m
->m_data
+= max_linkhdr
;
1883 new_mhip6
= mtod(new_m
, struct ip6_hdr
*);
1885 new_m
->m_len
= sizeof(*new_mhip6
);
1887 error
= ip6_insertfraghdr(morig
, new_m
, hlen
, &ip6f
);
1889 ip6stat
.ip6s_odropped
++;
1893 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1894 if (off
+ len
>= tlen
) {
1897 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1899 new_mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1900 sizeof(*ip6f
) - sizeof(struct ip6_hdr
)));
1902 if ((m_frgpart
= m_copy(morig
, off
, len
)) == NULL
) {
1904 ip6stat
.ip6s_odropped
++;
1907 m_cat(new_m
, m_frgpart
);
1908 new_m
->m_pkthdr
.len
= len
+ hlen
+ sizeof(*ip6f
);
1909 new_m
->m_pkthdr
.rcvif
= NULL
;
1911 M_COPY_CLASSIFIER(new_m
, morig
);
1912 M_COPY_PFTAG(new_m
, morig
);
1913 M_COPY_NECPTAG(new_m
, morig
);
1915 ip6f
->ip6f_reserved
= 0;
1916 ip6f
->ip6f_ident
= id
;
1917 ip6f
->ip6f_nxt
= nextproto
;
1918 ip6stat
.ip6s_ofragments
++;
1919 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1923 /* free all the fragments created */
1924 if (first_mbufp
!= NULL
) {
1925 m_freem_list(first_mbufp
);
1930 /* successful fragmenting */
1932 *mptr
= first_mbufp
;
1933 last_mbufp
->m_nextpkt
= NULL
;
1934 ip6stat
.ip6s_fragmented
++;
1935 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1942 ip6_copyexthdr(struct mbuf
**mp
, caddr_t hdr
, int hlen
)
1946 if (hlen
> MCLBYTES
) {
1947 return ENOBUFS
; /* XXX */
1949 MGET(m
, M_DONTWAIT
, MT_DATA
);
1955 MCLGET(m
, M_DONTWAIT
);
1956 if (!(m
->m_flags
& M_EXT
)) {
1963 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1971 ip6_out_cksum_stats(int proto
, u_int32_t len
)
1975 tcp_out6_cksum_stats(len
);
1978 udp_out6_cksum_stats(len
);
1981 /* keep only TCP or UDP stats for now */
1987 * Process a delayed payload checksum calculation (outbound path.)
1989 * hoff is the number of bytes beyond the mbuf data pointer which
1990 * points to the IPv6 header. optlen is the number of bytes, if any,
1991 * between the end of IPv6 header and the beginning of the ULP payload
1992 * header, which represents the extension headers. If optlen is less
1993 * than zero, this routine will bail when it detects extension headers.
1995 * Returns a bitmask representing all the work done in software.
1998 in6_finalize_cksum(struct mbuf
*m
, uint32_t hoff
, int32_t optlen
,
1999 int32_t nxt0
, uint32_t csum_flags
)
2001 unsigned char buf
[sizeof(struct ip6_hdr
)] __attribute__((aligned(8)));
2002 struct ip6_hdr
*ip6
;
2003 uint32_t offset
, mlen
, hlen
, olen
, sw_csum
;
2004 uint16_t csum
, ulpoff
, plen
;
2007 _CASSERT(sizeof(csum
) == sizeof(uint16_t));
2008 VERIFY(m
->m_flags
& M_PKTHDR
);
2010 sw_csum
= (csum_flags
& m
->m_pkthdr
.csum_flags
);
2012 if ((sw_csum
&= CSUM_DELAY_IPV6_DATA
) == 0) {
2016 mlen
= m
->m_pkthdr
.len
; /* total mbuf len */
2017 hlen
= sizeof(*ip6
); /* IPv6 header len */
2019 /* sanity check (need at least IPv6 header) */
2020 if (mlen
< (hoff
+ hlen
)) {
2021 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr "
2022 "(%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
);
2027 * In case the IPv6 header is not contiguous, or not 32-bit
2028 * aligned, copy it to a local buffer.
2030 if ((hoff
+ hlen
) > m
->m_len
||
2031 !IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
) + hoff
)) {
2032 m_copydata(m
, hoff
, hlen
, (caddr_t
)buf
);
2033 ip6
= (struct ip6_hdr
*)(void *)buf
;
2035 ip6
= (struct ip6_hdr
*)(void *)(m
->m_data
+ hoff
);
2039 plen
= ntohs(ip6
->ip6_plen
);
2040 if (plen
!= (mlen
- (hoff
+ hlen
))) {
2041 plen
= OSSwapInt16(plen
);
2042 if (plen
!= (mlen
- (hoff
+ hlen
))) {
2043 /* Don't complain for jumbograms */
2044 if (plen
!= 0 || nxt
!= IPPROTO_HOPOPTS
) {
2045 printf("%s: mbuf 0x%llx proto %d IPv6 "
2046 "plen %d (%x) [swapped %d (%x)] doesn't "
2047 "match actual packet length; %d is used "
2048 "instead\n", __func__
,
2049 (uint64_t)VM_KERNEL_ADDRPERM(m
), nxt
,
2050 ip6
->ip6_plen
, ip6
->ip6_plen
, plen
, plen
,
2051 (mlen
- (hoff
+ hlen
)));
2053 plen
= (uint16_t)(mlen
- (hoff
+ hlen
));
2058 /* next header isn't TCP/UDP and we don't know optlen, bail */
2059 if (nxt
!= IPPROTO_TCP
&& nxt
!= IPPROTO_UDP
) {
2065 /* caller supplied the original transport number; use it */
2067 nxt
= (uint8_t)nxt0
;
2072 offset
= hoff
+ hlen
+ olen
; /* ULP header */
2075 if (mlen
< offset
) {
2076 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr+ext_hdr "
2077 "(%u+%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
, olen
);
2082 * offset is added to the lower 16-bit value of csum_data,
2083 * which is expected to contain the ULP offset; therefore
2084 * CSUM_PARTIAL offset adjustment must be undone.
2086 if ((m
->m_pkthdr
.csum_flags
& (CSUM_PARTIAL
| CSUM_DATA_VALID
)) ==
2087 (CSUM_PARTIAL
| CSUM_DATA_VALID
)) {
2089 * Get back the original ULP offset (this will
2090 * undo the CSUM_PARTIAL logic in ip6_output.)
2092 m
->m_pkthdr
.csum_data
= (m
->m_pkthdr
.csum_tx_stuff
-
2093 m
->m_pkthdr
.csum_tx_start
);
2096 ulpoff
= (m
->m_pkthdr
.csum_data
& 0xffff); /* ULP csum offset */
2098 if (mlen
< (ulpoff
+ sizeof(csum
))) {
2099 panic("%s: mbuf %p pkt len (%u) proto %d invalid ULP "
2100 "cksum offset (%u) cksum flags 0x%x\n", __func__
,
2101 m
, mlen
, nxt
, ulpoff
, m
->m_pkthdr
.csum_flags
);
2105 csum
= inet6_cksum(m
, 0, offset
, plen
- olen
);
2108 ip6_out_cksum_stats(nxt
, plen
- olen
);
2110 /* RFC1122 4.1.3.4 */
2112 (m
->m_pkthdr
.csum_flags
& (CSUM_UDPIPV6
| CSUM_ZERO_INVERT
))) {
2116 /* Insert the checksum in the ULP csum field */
2118 if ((offset
+ sizeof(csum
)) > m
->m_len
) {
2119 m_copyback(m
, offset
, sizeof(csum
), &csum
);
2120 } else if (IP6_HDR_ALIGNED_P(mtod(m
, char *) + hoff
)) {
2121 *(uint16_t *)(void *)(mtod(m
, char *) + offset
) = csum
;
2123 bcopy(&csum
, (mtod(m
, char *) + offset
), sizeof(csum
));
2125 m
->m_pkthdr
.csum_flags
&= ~(CSUM_DELAY_IPV6_DATA
| CSUM_DATA_VALID
|
2126 CSUM_PARTIAL
| CSUM_ZERO_INVERT
);
2133 * Insert jumbo payload option.
2136 ip6_insert_jumboopt(struct ip6_exthdrs
*exthdrs
, u_int32_t plen
)
2142 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
2145 * If there is no hop-by-hop options header, allocate new one.
2146 * If there is one but it doesn't have enough space to store the
2147 * jumbo payload option, allocate a cluster to store the whole options.
2148 * Otherwise, use it to store the options.
2150 if (exthdrs
->ip6e_hbh
== NULL
) {
2151 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
2155 mopt
->m_len
= JUMBOOPTLEN
;
2156 optbuf
= mtod(mopt
, u_char
*);
2157 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
2158 exthdrs
->ip6e_hbh
= mopt
;
2160 struct ip6_hbh
*hbh
;
2162 mopt
= exthdrs
->ip6e_hbh
;
2163 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
2166 * - exthdrs->ip6e_hbh is not referenced from places
2167 * other than exthdrs.
2168 * - exthdrs->ip6e_hbh is not an mbuf chain.
2170 u_int32_t oldoptlen
= mopt
->m_len
;
2174 * XXX: give up if the whole (new) hbh header does
2175 * not fit even in an mbuf cluster.
2177 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
) {
2182 * As a consequence, we must always prepare a cluster
2185 MGET(n
, M_DONTWAIT
, MT_DATA
);
2187 MCLGET(n
, M_DONTWAIT
);
2188 if (!(n
->m_flags
& M_EXT
)) {
2196 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
2197 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
2199 optbuf
= mtod(n
, u_char
*) + oldoptlen
;
2201 mopt
= exthdrs
->ip6e_hbh
= n
;
2203 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
2204 mopt
->m_len
+= JUMBOOPTLEN
;
2206 optbuf
[0] = IP6OPT_PADN
;
2210 * Adjust the header length according to the pad and
2211 * the jumbo payload option.
2213 hbh
= mtod(mopt
, struct ip6_hbh
*);
2214 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
2217 /* fill in the option. */
2218 optbuf
[2] = IP6OPT_JUMBO
;
2220 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
2221 bcopy(&v
, &optbuf
[4], sizeof(u_int32_t
));
2223 /* finally, adjust the packet header length */
2224 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
2231 * Insert fragment header and copy unfragmentable header portions.
2234 ip6_insertfraghdr(struct mbuf
*m0
, struct mbuf
*m
, int hlen
,
2235 struct ip6_frag
**frghdrp
)
2237 struct mbuf
*n
, *mlast
;
2239 if (hlen
> sizeof(struct ip6_hdr
)) {
2240 n
= m_copym(m0
, sizeof(struct ip6_hdr
),
2241 hlen
- sizeof(struct ip6_hdr
), M_DONTWAIT
);
2250 /* Search for the last mbuf of unfragmentable part. */
2251 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
) {
2255 if (!(mlast
->m_flags
& M_EXT
) &&
2256 M_TRAILINGSPACE(mlast
) >= sizeof(struct ip6_frag
)) {
2257 /* use the trailing space of the last mbuf for the frag hdr */
2258 *frghdrp
= (struct ip6_frag
*)(mtod(mlast
, caddr_t
) +
2260 mlast
->m_len
+= sizeof(struct ip6_frag
);
2261 m
->m_pkthdr
.len
+= sizeof(struct ip6_frag
);
2263 /* allocate a new mbuf for the fragment header */
2266 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
2270 mfrg
->m_len
= sizeof(struct ip6_frag
);
2271 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
2272 mlast
->m_next
= mfrg
;
2279 ip6_getpmtu(struct route_in6
*ro_pmtu
, struct route_in6
*ro
,
2280 struct ifnet
*ifp
, struct in6_addr
*dst
, u_int32_t
*mtup
)
2286 if (ro_pmtu
!= ro
) {
2287 /* The first hop and the final destination may differ. */
2288 struct sockaddr_in6
*sa6_dst
= SIN6(&ro_pmtu
->ro_dst
);
2289 if (ROUTE_UNUSABLE(ro_pmtu
) ||
2290 !IN6_ARE_ADDR_EQUAL(&sa6_dst
->sin6_addr
, dst
)) {
2291 ROUTE_RELEASE(ro_pmtu
);
2294 if (ro_pmtu
->ro_rt
== NULL
) {
2295 bzero(sa6_dst
, sizeof(*sa6_dst
));
2296 sa6_dst
->sin6_family
= AF_INET6
;
2297 sa6_dst
->sin6_len
= sizeof(struct sockaddr_in6
);
2298 sa6_dst
->sin6_addr
= *dst
;
2300 rtalloc_scoped((struct route
*)ro_pmtu
,
2301 ifp
!= NULL
? ifp
->if_index
: IFSCOPE_NONE
);
2305 if (ro_pmtu
->ro_rt
!= NULL
) {
2309 ifp
= ro_pmtu
->ro_rt
->rt_ifp
;
2311 /* Access without acquiring nd_ifinfo lock for performance */
2312 ifmtu
= IN6_LINKMTU(ifp
);
2315 * Access rmx_mtu without holding the route entry lock,
2316 * for performance; this isn't something that changes
2317 * often, so optimize.
2319 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
2320 if (mtu
> ifmtu
|| mtu
== 0) {
2322 * The MTU on the route is larger than the MTU on
2323 * the interface! This shouldn't happen, unless the
2324 * MTU of the interface has been changed after the
2325 * interface was brought up. Change the MTU in the
2326 * route to match the interface MTU (as long as the
2327 * field isn't locked).
2329 * if MTU on the route is 0, we need to fix the MTU.
2330 * this case happens with path MTU discovery timeouts.
2333 if (!(ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
2334 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
2339 /* Don't hold nd_ifinfo lock for performance */
2340 mtu
= IN6_LINKMTU(ifp
);
2342 error
= EHOSTUNREACH
; /* XXX */
2351 * IP6 socket option processing.
2354 ip6_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2356 int optdatalen
, uproto
;
2359 struct inpcb
*in6p
= sotoinpcb(so
);
2360 int error
= 0, optval
= 0;
2361 int level
, op
= -1, optname
= 0;
2364 lck_mtx_t
*mutex_held
= NULL
;
2366 VERIFY(sopt
!= NULL
);
2368 level
= sopt
->sopt_level
;
2369 op
= sopt
->sopt_dir
;
2370 optname
= sopt
->sopt_name
;
2371 optlen
= sopt
->sopt_valsize
;
2373 uproto
= (int)SOCK_PROTO(so
);
2375 privileged
= (proc_suser(p
) == 0);
2377 if (level
== IPPROTO_IPV6
) {
2378 boolean_t capture_exthdrstat_in
= FALSE
;
2381 mutex_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
2383 * Wait if we are in the middle of ip6_output
2384 * as we unlocked the socket there and don't
2385 * want to overwrite the IP options
2387 if (in6p
->inp_sndinprog_cnt
> 0) {
2388 in6p
->inp_sndingprog_waiters
++;
2390 while (in6p
->inp_sndinprog_cnt
> 0) {
2391 msleep(&in6p
->inp_sndinprog_cnt
, mutex_held
,
2392 PSOCK
| PCATCH
, "inp_sndinprog_cnt",
2395 in6p
->inp_sndingprog_waiters
--;
2398 case IPV6_2292PKTOPTIONS
: {
2401 error
= soopt_getm(sopt
, &m
);
2405 error
= soopt_mcopyin(sopt
, m
);
2409 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
2416 * Use of some Hop-by-Hop options or some
2417 * Destination options, might require special
2418 * privilege. That is, normal applications
2419 * (without special privilege) might be forbidden
2420 * from setting certain options in outgoing packets,
2421 * and might never see certain options in received
2422 * packets. [RFC 2292 Section 6]
2423 * KAME specific note:
2424 * KAME prevents non-privileged users from sending or
2425 * receiving ANY hbh/dst options in order to avoid
2426 * overhead of parsing options in the kernel.
2428 case IPV6_RECVHOPOPTS
:
2429 case IPV6_RECVDSTOPTS
:
2430 case IPV6_RECVRTHDRDSTOPTS
:
2435 case IPV6_UNICAST_HOPS
:
2437 case IPV6_RECVPKTINFO
:
2438 case IPV6_RECVHOPLIMIT
:
2439 case IPV6_RECVRTHDR
:
2440 case IPV6_RECVPATHMTU
:
2441 case IPV6_RECVTCLASS
:
2443 case IPV6_AUTOFLOWLABEL
:
2444 if (optlen
!= sizeof(int)) {
2448 error
= sooptcopyin(sopt
, &optval
,
2449 sizeof(optval
), sizeof(optval
));
2455 case IPV6_UNICAST_HOPS
:
2456 if (optval
< -1 || optval
>= 256) {
2459 /* -1 = kernel default */
2460 in6p
->in6p_hops
= (short)optval
;
2461 if (in6p
->inp_vflag
&
2468 #define OPTSET(bit) do { \
2470 in6p->inp_flags |= (bit); \
2472 in6p->inp_flags &= ~(bit); \
2475 #define OPTSET2292(bit) do { \
2476 in6p->inp_flags |= IN6P_RFC2292; \
2478 in6p->inp_flags |= (bit); \
2480 in6p->inp_flags &= ~(bit); \
2483 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
2485 case IPV6_RECVPKTINFO
:
2486 /* cannot mix with RFC2292 */
2487 if (OPTBIT(IN6P_RFC2292
)) {
2491 OPTSET(IN6P_PKTINFO
);
2494 case IPV6_HOPLIMIT
: {
2495 struct ip6_pktopts
**optp
;
2497 /* cannot mix with RFC2292 */
2498 if (OPTBIT(IN6P_RFC2292
)) {
2502 optp
= &in6p
->in6p_outputopts
;
2503 error
= ip6_pcbopt(IPV6_HOPLIMIT
,
2504 (u_char
*)&optval
, sizeof(optval
),
2509 case IPV6_RECVHOPLIMIT
:
2510 /* cannot mix with RFC2292 */
2511 if (OPTBIT(IN6P_RFC2292
)) {
2515 OPTSET(IN6P_HOPLIMIT
);
2518 case IPV6_RECVHOPOPTS
:
2519 /* cannot mix with RFC2292 */
2520 if (OPTBIT(IN6P_RFC2292
)) {
2524 OPTSET(IN6P_HOPOPTS
);
2525 capture_exthdrstat_in
= TRUE
;
2528 case IPV6_RECVDSTOPTS
:
2529 /* cannot mix with RFC2292 */
2530 if (OPTBIT(IN6P_RFC2292
)) {
2534 OPTSET(IN6P_DSTOPTS
);
2535 capture_exthdrstat_in
= TRUE
;
2538 case IPV6_RECVRTHDRDSTOPTS
:
2539 /* cannot mix with RFC2292 */
2540 if (OPTBIT(IN6P_RFC2292
)) {
2544 OPTSET(IN6P_RTHDRDSTOPTS
);
2545 capture_exthdrstat_in
= TRUE
;
2548 case IPV6_RECVRTHDR
:
2549 /* cannot mix with RFC2292 */
2550 if (OPTBIT(IN6P_RFC2292
)) {
2555 capture_exthdrstat_in
= TRUE
;
2558 case IPV6_RECVPATHMTU
:
2560 * We ignore this option for TCP
2562 * (RFC3542 leaves this case
2565 if (uproto
!= IPPROTO_TCP
) {
2572 * make setsockopt(IPV6_V6ONLY)
2573 * available only prior to bind(2).
2574 * see ipng mailing list, Jun 22 2001.
2576 if (in6p
->inp_lport
||
2577 !IN6_IS_ADDR_UNSPECIFIED(
2578 &in6p
->in6p_laddr
)) {
2582 OPTSET(IN6P_IPV6_V6ONLY
);
2584 in6p
->inp_vflag
&= ~INP_IPV4
;
2586 in6p
->inp_vflag
|= INP_IPV4
;
2590 case IPV6_RECVTCLASS
:
2591 /* we can mix with RFC2292 */
2592 OPTSET(IN6P_TCLASS
);
2595 case IPV6_AUTOFLOWLABEL
:
2596 OPTSET(IN6P_AUTOFLOWLABEL
);
2603 case IPV6_USE_MIN_MTU
:
2604 case IPV6_PREFER_TEMPADDR
: {
2605 struct ip6_pktopts
**optp
;
2607 if (optlen
!= sizeof(optval
)) {
2611 error
= sooptcopyin(sopt
, &optval
,
2612 sizeof(optval
), sizeof(optval
));
2617 optp
= &in6p
->in6p_outputopts
;
2618 error
= ip6_pcbopt(optname
, (u_char
*)&optval
,
2619 sizeof(optval
), optp
, uproto
);
2621 if (optname
== IPV6_TCLASS
) {
2622 // Add in the ECN flags
2623 u_int8_t tos
= (in6p
->inp_ip_tos
& ~IPTOS_ECN_MASK
);
2624 u_int8_t ecn
= optval
& IPTOS_ECN_MASK
;
2625 in6p
->inp_ip_tos
= tos
| ecn
;
2630 case IPV6_2292PKTINFO
:
2631 case IPV6_2292HOPLIMIT
:
2632 case IPV6_2292HOPOPTS
:
2633 case IPV6_2292DSTOPTS
:
2634 case IPV6_2292RTHDR
:
2636 if (optlen
!= sizeof(int)) {
2640 error
= sooptcopyin(sopt
, &optval
,
2641 sizeof(optval
), sizeof(optval
));
2646 case IPV6_2292PKTINFO
:
2647 OPTSET2292(IN6P_PKTINFO
);
2649 case IPV6_2292HOPLIMIT
:
2650 OPTSET2292(IN6P_HOPLIMIT
);
2652 case IPV6_2292HOPOPTS
:
2654 * Check super-user privilege.
2655 * See comments for IPV6_RECVHOPOPTS.
2660 OPTSET2292(IN6P_HOPOPTS
);
2661 capture_exthdrstat_in
= TRUE
;
2663 case IPV6_2292DSTOPTS
:
2667 OPTSET2292(IN6P_DSTOPTS
|
2668 IN6P_RTHDRDSTOPTS
); /* XXX */
2669 capture_exthdrstat_in
= TRUE
;
2671 case IPV6_2292RTHDR
:
2672 OPTSET2292(IN6P_RTHDR
);
2673 capture_exthdrstat_in
= TRUE
;
2678 case IPV6_3542PKTINFO
:
2679 case IPV6_3542HOPOPTS
:
2680 case IPV6_3542RTHDR
:
2681 case IPV6_3542DSTOPTS
:
2682 case IPV6_RTHDRDSTOPTS
:
2683 case IPV6_3542NEXTHOP
: {
2684 struct ip6_pktopts
**optp
;
2685 /* new advanced API (RFC3542) */
2688 /* cannot mix with RFC2292 */
2689 if (OPTBIT(IN6P_RFC2292
)) {
2693 error
= soopt_getm(sopt
, &m
);
2697 error
= soopt_mcopyin(sopt
, m
);
2702 optp
= &in6p
->in6p_outputopts
;
2703 error
= ip6_pcbopt(optname
, mtod(m
, u_char
*),
2704 m
->m_len
, optp
, uproto
);
2709 case IPV6_MULTICAST_IF
:
2710 case IPV6_MULTICAST_HOPS
:
2711 case IPV6_MULTICAST_LOOP
:
2712 case IPV6_JOIN_GROUP
:
2713 case IPV6_LEAVE_GROUP
:
2715 case MCAST_BLOCK_SOURCE
:
2716 case MCAST_UNBLOCK_SOURCE
:
2717 case MCAST_JOIN_GROUP
:
2718 case MCAST_LEAVE_GROUP
:
2719 case MCAST_JOIN_SOURCE_GROUP
:
2720 case MCAST_LEAVE_SOURCE_GROUP
:
2721 error
= ip6_setmoptions(in6p
, sopt
);
2724 case IPV6_PORTRANGE
:
2725 error
= sooptcopyin(sopt
, &optval
,
2726 sizeof(optval
), sizeof(optval
));
2732 case IPV6_PORTRANGE_DEFAULT
:
2733 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2734 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2737 case IPV6_PORTRANGE_HIGH
:
2738 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2739 in6p
->inp_flags
|= INP_HIGHPORT
;
2742 case IPV6_PORTRANGE_LOW
:
2743 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2744 in6p
->inp_flags
|= INP_LOWPORT
;
2753 case IPV6_IPSEC_POLICY
: {
2758 if ((error
= soopt_getm(sopt
, &m
)) != 0) {
2761 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) {
2765 req
= mtod(m
, caddr_t
);
2767 error
= ipsec6_set_policy(in6p
, optname
, req
,
2774 * IPv6 variant of IP_BOUND_IF; for details see
2775 * comments on IP_BOUND_IF in ip_ctloutput().
2778 /* This option is settable only on IPv6 */
2779 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2784 error
= sooptcopyin(sopt
, &optval
,
2785 sizeof(optval
), sizeof(optval
));
2791 error
= inp_bindif(in6p
, optval
, NULL
);
2794 case IPV6_NO_IFT_CELLULAR
:
2795 /* This option is settable only for IPv6 */
2796 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2801 error
= sooptcopyin(sopt
, &optval
,
2802 sizeof(optval
), sizeof(optval
));
2808 /* once set, it cannot be unset */
2809 if (!optval
&& INP_NO_CELLULAR(in6p
)) {
2814 error
= so_set_restrictions(so
,
2815 SO_RESTRICT_DENY_CELLULAR
);
2819 /* This option is not settable */
2824 error
= ENOPROTOOPT
;
2827 if (capture_exthdrstat_in
) {
2828 if (uproto
== IPPROTO_TCP
) {
2829 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_stream_exthdr_in
);
2830 } else if (uproto
== IPPROTO_UDP
) {
2831 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_dgram_exthdr_in
);
2838 case IPV6_2292PKTOPTIONS
:
2840 * RFC3542 (effectively) deprecated the
2841 * semantics of the 2292-style pktoptions.
2842 * Since it was not reliable in nature (i.e.,
2843 * applications had to expect the lack of some
2844 * information after all), it would make sense
2845 * to simplify this part by always returning
2848 sopt
->sopt_valsize
= 0;
2851 case IPV6_RECVHOPOPTS
:
2852 case IPV6_RECVDSTOPTS
:
2853 case IPV6_RECVRTHDRDSTOPTS
:
2854 case IPV6_UNICAST_HOPS
:
2855 case IPV6_RECVPKTINFO
:
2856 case IPV6_RECVHOPLIMIT
:
2857 case IPV6_RECVRTHDR
:
2858 case IPV6_RECVPATHMTU
:
2860 case IPV6_PORTRANGE
:
2861 case IPV6_RECVTCLASS
:
2862 case IPV6_AUTOFLOWLABEL
:
2864 case IPV6_RECVHOPOPTS
:
2865 optval
= OPTBIT(IN6P_HOPOPTS
);
2868 case IPV6_RECVDSTOPTS
:
2869 optval
= OPTBIT(IN6P_DSTOPTS
);
2872 case IPV6_RECVRTHDRDSTOPTS
:
2873 optval
= OPTBIT(IN6P_RTHDRDSTOPTS
);
2876 case IPV6_UNICAST_HOPS
:
2877 optval
= in6p
->in6p_hops
;
2880 case IPV6_RECVPKTINFO
:
2881 optval
= OPTBIT(IN6P_PKTINFO
);
2884 case IPV6_RECVHOPLIMIT
:
2885 optval
= OPTBIT(IN6P_HOPLIMIT
);
2888 case IPV6_RECVRTHDR
:
2889 optval
= OPTBIT(IN6P_RTHDR
);
2892 case IPV6_RECVPATHMTU
:
2893 optval
= OPTBIT(IN6P_MTU
);
2897 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
2900 case IPV6_PORTRANGE
: {
2902 flags
= in6p
->inp_flags
;
2903 if (flags
& INP_HIGHPORT
) {
2904 optval
= IPV6_PORTRANGE_HIGH
;
2905 } else if (flags
& INP_LOWPORT
) {
2906 optval
= IPV6_PORTRANGE_LOW
;
2912 case IPV6_RECVTCLASS
:
2913 optval
= OPTBIT(IN6P_TCLASS
);
2916 case IPV6_AUTOFLOWLABEL
:
2917 optval
= OPTBIT(IN6P_AUTOFLOWLABEL
);
2923 error
= sooptcopyout(sopt
, &optval
,
2927 case IPV6_PATHMTU
: {
2929 struct ip6_mtuinfo mtuinfo
;
2930 struct route_in6 sro
;
2932 bzero(&sro
, sizeof(sro
));
2934 if (!(so
->so_state
& SS_ISCONNECTED
)) {
2938 * XXX: we dot not consider the case of source
2939 * routing, or optional information to specify
2940 * the outgoing interface.
2942 error
= ip6_getpmtu(&sro
, NULL
, NULL
,
2943 &in6p
->in6p_faddr
, &pmtu
);
2944 ROUTE_RELEASE(&sro
);
2948 if (pmtu
> IPV6_MAXPACKET
) {
2949 pmtu
= IPV6_MAXPACKET
;
2952 bzero(&mtuinfo
, sizeof(mtuinfo
));
2953 mtuinfo
.ip6m_mtu
= (u_int32_t
)pmtu
;
2954 optdata
= (void *)&mtuinfo
;
2955 optdatalen
= sizeof(mtuinfo
);
2956 error
= sooptcopyout(sopt
, optdata
,
2961 case IPV6_2292PKTINFO
:
2962 case IPV6_2292HOPLIMIT
:
2963 case IPV6_2292HOPOPTS
:
2964 case IPV6_2292RTHDR
:
2965 case IPV6_2292DSTOPTS
:
2967 case IPV6_2292PKTINFO
:
2968 optval
= OPTBIT(IN6P_PKTINFO
);
2970 case IPV6_2292HOPLIMIT
:
2971 optval
= OPTBIT(IN6P_HOPLIMIT
);
2973 case IPV6_2292HOPOPTS
:
2974 optval
= OPTBIT(IN6P_HOPOPTS
);
2976 case IPV6_2292RTHDR
:
2977 optval
= OPTBIT(IN6P_RTHDR
);
2979 case IPV6_2292DSTOPTS
:
2980 optval
= OPTBIT(IN6P_DSTOPTS
|
2984 error
= sooptcopyout(sopt
, &optval
,
2992 case IPV6_RTHDRDSTOPTS
:
2996 case IPV6_USE_MIN_MTU
:
2997 case IPV6_PREFER_TEMPADDR
:
2998 error
= ip6_getpcbopt(in6p
->in6p_outputopts
,
3002 case IPV6_MULTICAST_IF
:
3003 case IPV6_MULTICAST_HOPS
:
3004 case IPV6_MULTICAST_LOOP
:
3006 error
= ip6_getmoptions(in6p
, sopt
);
3009 case IPV6_IPSEC_POLICY
: {
3010 error
= 0; /* This option is no longer supported */
3015 if (in6p
->inp_flags
& INP_BOUND_IF
) {
3016 optval
= in6p
->inp_boundifp
->if_index
;
3018 error
= sooptcopyout(sopt
, &optval
,
3022 case IPV6_NO_IFT_CELLULAR
:
3023 optval
= INP_NO_CELLULAR(in6p
) ? 1 : 0;
3024 error
= sooptcopyout(sopt
, &optval
,
3029 optval
= (in6p
->in6p_last_outifp
!= NULL
) ?
3030 in6p
->in6p_last_outifp
->if_index
: 0;
3031 error
= sooptcopyout(sopt
, &optval
,
3036 error
= ENOPROTOOPT
;
3041 } else if (level
== IPPROTO_UDP
) {
3042 error
= udp_ctloutput(so
, sopt
);
3050 ip6_raw_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
3052 int error
= 0, optval
;
3054 const int icmp6off
= offsetof(struct icmp6_hdr
, icmp6_cksum
);
3055 struct inpcb
*in6p
= sotoinpcb(so
);
3056 int level
, op
, optname
;
3058 level
= sopt
->sopt_level
;
3059 op
= sopt
->sopt_dir
;
3060 optname
= sopt
->sopt_name
;
3061 optlen
= sopt
->sopt_valsize
;
3063 if (level
!= IPPROTO_IPV6
) {
3070 * For ICMPv6 sockets, no modification allowed for checksum
3071 * offset, permit "no change" values to help existing apps.
3073 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
3074 * for an ICMPv6 socket will fail."
3075 * The current behavior does not meet RFC3542.
3079 if (optlen
!= sizeof(int)) {
3083 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
3088 if ((optval
% 2) != 0) {
3089 /* the API assumes even offset values */
3091 } else if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
3092 if (optval
!= icmp6off
) {
3096 in6p
->in6p_cksum
= optval
;
3101 if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
3104 optval
= in6p
->in6p_cksum
;
3107 error
= sooptcopyout(sopt
, &optval
, sizeof(optval
));
3117 error
= ENOPROTOOPT
;
3125 * Set up IP6 options in pcb for insertion in output packets or
3126 * specifying behavior of outgoing packets.
3129 ip6_pcbopts(struct ip6_pktopts
**pktopt
, struct mbuf
*m
, struct socket
*so
,
3130 struct sockopt
*sopt
)
3132 #pragma unused(sopt)
3133 struct ip6_pktopts
*opt
= *pktopt
;
3136 /* turn off any old options. */
3139 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
3140 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
3141 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
) {
3142 printf("%s: all specified options are cleared.\n",
3146 ip6_clearpktopts(opt
, -1);
3148 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
3155 if (m
== NULL
|| m
->m_len
== 0) {
3157 * Only turning off any previous options, regardless of
3158 * whether the opt is just created or given.
3161 FREE(opt
, M_IP6OPT
);
3166 /* set options specified by user. */
3167 if ((error
= ip6_setpktopts(m
, opt
, NULL
, SOCK_PROTO(so
))) != 0) {
3168 ip6_clearpktopts(opt
, -1); /* XXX: discard all options */
3169 FREE(opt
, M_IP6OPT
);
3177 * initialize ip6_pktopts. beware that there are non-zero default values in
3181 ip6_initpktopts(struct ip6_pktopts
*opt
)
3183 bzero(opt
, sizeof(*opt
));
3184 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
3185 opt
->ip6po_tclass
= -1; /* -1 means default traffic class */
3186 opt
->ip6po_minmtu
= IP6PO_MINMTU_MCASTONLY
;
3187 opt
->ip6po_prefer_tempaddr
= IP6PO_TEMPADDR_SYSTEM
;
3191 ip6_pcbopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
**pktopt
,
3194 struct ip6_pktopts
*opt
;
3198 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
3202 ip6_initpktopts(opt
);
3206 return ip6_setpktopt(optname
, buf
, len
, opt
, 1, 0, uproto
);
3210 ip6_getpcbopt(struct ip6_pktopts
*pktopt
, int optname
, struct sockopt
*sopt
)
3212 void *optdata
= NULL
;
3214 struct ip6_ext
*ip6e
;
3215 struct in6_pktinfo null_pktinfo
;
3216 int deftclass
= 0, on
;
3217 int defminmtu
= IP6PO_MINMTU_MCASTONLY
;
3218 int defpreftemp
= IP6PO_TEMPADDR_SYSTEM
;
3223 if (pktopt
&& pktopt
->ip6po_pktinfo
) {
3224 optdata
= (void *)pktopt
->ip6po_pktinfo
;
3226 /* XXX: we don't have to do this every time... */
3227 bzero(&null_pktinfo
, sizeof(null_pktinfo
));
3228 optdata
= (void *)&null_pktinfo
;
3230 optdatalen
= sizeof(struct in6_pktinfo
);
3234 if (pktopt
&& pktopt
->ip6po_tclass
>= 0) {
3235 optdata
= (void *)&pktopt
->ip6po_tclass
;
3237 optdata
= (void *)&deftclass
;
3239 optdatalen
= sizeof(int);
3243 if (pktopt
&& pktopt
->ip6po_hbh
) {
3244 optdata
= (void *)pktopt
->ip6po_hbh
;
3245 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_hbh
;
3246 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3251 if (pktopt
&& pktopt
->ip6po_rthdr
) {
3252 optdata
= (void *)pktopt
->ip6po_rthdr
;
3253 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_rthdr
;
3254 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3258 case IPV6_RTHDRDSTOPTS
:
3259 if (pktopt
&& pktopt
->ip6po_dest1
) {
3260 optdata
= (void *)pktopt
->ip6po_dest1
;
3261 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest1
;
3262 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3267 if (pktopt
&& pktopt
->ip6po_dest2
) {
3268 optdata
= (void *)pktopt
->ip6po_dest2
;
3269 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest2
;
3270 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3275 if (pktopt
&& pktopt
->ip6po_nexthop
) {
3276 optdata
= (void *)pktopt
->ip6po_nexthop
;
3277 optdatalen
= pktopt
->ip6po_nexthop
->sa_len
;
3281 case IPV6_USE_MIN_MTU
:
3283 optdata
= (void *)&pktopt
->ip6po_minmtu
;
3285 optdata
= (void *)&defminmtu
;
3287 optdatalen
= sizeof(int);
3291 if (pktopt
&& ((pktopt
->ip6po_flags
) & IP6PO_DONTFRAG
)) {
3296 optdata
= (void *)&on
;
3297 optdatalen
= sizeof(on
);
3300 case IPV6_PREFER_TEMPADDR
:
3302 optdata
= (void *)&pktopt
->ip6po_prefer_tempaddr
;
3304 optdata
= (void *)&defpreftemp
;
3306 optdatalen
= sizeof(int);
3309 default: /* should not happen */
3311 panic("ip6_getpcbopt: unexpected option\n");
3316 return sooptcopyout(sopt
, optdata
, optdatalen
);
3320 ip6_clearpktopts(struct ip6_pktopts
*pktopt
, int optname
)
3322 if (pktopt
== NULL
) {
3326 if (optname
== -1 || optname
== IPV6_PKTINFO
) {
3327 if (pktopt
->ip6po_pktinfo
) {
3328 FREE(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
3330 pktopt
->ip6po_pktinfo
= NULL
;
3332 if (optname
== -1 || optname
== IPV6_HOPLIMIT
) {
3333 pktopt
->ip6po_hlim
= -1;
3335 if (optname
== -1 || optname
== IPV6_TCLASS
) {
3336 pktopt
->ip6po_tclass
= -1;
3338 if (optname
== -1 || optname
== IPV6_NEXTHOP
) {
3339 ROUTE_RELEASE(&pktopt
->ip6po_nextroute
);
3340 if (pktopt
->ip6po_nexthop
) {
3341 FREE(pktopt
->ip6po_nexthop
, M_IP6OPT
);
3343 pktopt
->ip6po_nexthop
= NULL
;
3345 if (optname
== -1 || optname
== IPV6_HOPOPTS
) {
3346 if (pktopt
->ip6po_hbh
) {
3347 FREE(pktopt
->ip6po_hbh
, M_IP6OPT
);
3349 pktopt
->ip6po_hbh
= NULL
;
3351 if (optname
== -1 || optname
== IPV6_RTHDRDSTOPTS
) {
3352 if (pktopt
->ip6po_dest1
) {
3353 FREE(pktopt
->ip6po_dest1
, M_IP6OPT
);
3355 pktopt
->ip6po_dest1
= NULL
;
3357 if (optname
== -1 || optname
== IPV6_RTHDR
) {
3358 if (pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
) {
3359 FREE(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
3361 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
3362 ROUTE_RELEASE(&pktopt
->ip6po_route
);
3364 if (optname
== -1 || optname
== IPV6_DSTOPTS
) {
3365 if (pktopt
->ip6po_dest2
) {
3366 FREE(pktopt
->ip6po_dest2
, M_IP6OPT
);
3368 pktopt
->ip6po_dest2
= NULL
;
3372 #define PKTOPT_EXTHDRCPY(type) do { \
3375 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3; \
3376 dst->type = _MALLOC(hlen, M_IP6OPT, canwait); \
3377 if (dst->type == NULL && canwait == M_NOWAIT) \
3379 bcopy(src->type, dst->type, hlen); \
3384 copypktopts(struct ip6_pktopts
*dst
, struct ip6_pktopts
*src
, int canwait
)
3386 if (dst
== NULL
|| src
== NULL
) {
3387 printf("copypktopts: invalid argument\n");
3391 dst
->ip6po_hlim
= src
->ip6po_hlim
;
3392 dst
->ip6po_tclass
= src
->ip6po_tclass
;
3393 dst
->ip6po_flags
= src
->ip6po_flags
;
3394 if (src
->ip6po_pktinfo
) {
3395 dst
->ip6po_pktinfo
= _MALLOC(sizeof(*dst
->ip6po_pktinfo
),
3397 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
) {
3400 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
3402 if (src
->ip6po_nexthop
) {
3403 dst
->ip6po_nexthop
= _MALLOC(src
->ip6po_nexthop
->sa_len
,
3405 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
) {
3408 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
3409 src
->ip6po_nexthop
->sa_len
);
3411 PKTOPT_EXTHDRCPY(ip6po_hbh
);
3412 PKTOPT_EXTHDRCPY(ip6po_dest1
);
3413 PKTOPT_EXTHDRCPY(ip6po_dest2
);
3414 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
3418 ip6_clearpktopts(dst
, -1);
3421 #undef PKTOPT_EXTHDRCPY
3423 struct ip6_pktopts
*
3424 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
3427 struct ip6_pktopts
*dst
;
3429 dst
= _MALLOC(sizeof(*dst
), M_IP6OPT
, canwait
);
3433 ip6_initpktopts(dst
);
3435 if ((error
= copypktopts(dst
, src
, canwait
)) != 0) {
3436 FREE(dst
, M_IP6OPT
);
3444 ip6_freepcbopts(struct ip6_pktopts
*pktopt
)
3446 if (pktopt
== NULL
) {
3450 ip6_clearpktopts(pktopt
, -1);
3452 FREE(pktopt
, M_IP6OPT
);
3456 ip6_moptions_init(void)
3458 PE_parse_boot_argn("ifa_debug", &im6o_debug
, sizeof(im6o_debug
));
3460 vm_size_t im6o_size
= (im6o_debug
== 0) ? sizeof(struct ip6_moptions
) :
3461 sizeof(struct ip6_moptions_dbg
);
3463 im6o_zone
= zone_create(IM6O_ZONE_NAME
, im6o_size
, ZC_ZFREE_CLEARMEM
);
3467 im6o_addref(struct ip6_moptions
*im6o
, int locked
)
3472 IM6O_LOCK_ASSERT_HELD(im6o
);
3475 if (++im6o
->im6o_refcnt
== 0) {
3476 panic("%s: im6o %p wraparound refcnt\n", __func__
, im6o
);
3478 } else if (im6o
->im6o_trace
!= NULL
) {
3479 (*im6o
->im6o_trace
)(im6o
, TRUE
);
3488 im6o_remref(struct ip6_moptions
*im6o
)
3493 if (im6o
->im6o_refcnt
== 0) {
3494 panic("%s: im6o %p negative refcnt", __func__
, im6o
);
3496 } else if (im6o
->im6o_trace
!= NULL
) {
3497 (*im6o
->im6o_trace
)(im6o
, FALSE
);
3500 --im6o
->im6o_refcnt
;
3501 if (im6o
->im6o_refcnt
> 0) {
3506 for (i
= 0; i
< im6o
->im6o_num_memberships
; ++i
) {
3507 struct in6_mfilter
*imf
;
3509 imf
= im6o
->im6o_mfilters
? &im6o
->im6o_mfilters
[i
] : NULL
;
3514 (void) in6_mc_leave(im6o
->im6o_membership
[i
], imf
);
3520 IN6M_REMREF(im6o
->im6o_membership
[i
]);
3521 im6o
->im6o_membership
[i
] = NULL
;
3523 im6o
->im6o_num_memberships
= 0;
3524 if (im6o
->im6o_mfilters
!= NULL
) {
3525 FREE(im6o
->im6o_mfilters
, M_IN6MFILTER
);
3526 im6o
->im6o_mfilters
= NULL
;
3528 if (im6o
->im6o_membership
!= NULL
) {
3529 FREE(im6o
->im6o_membership
, M_IP6MOPTS
);
3530 im6o
->im6o_membership
= NULL
;
3534 lck_mtx_destroy(&im6o
->im6o_lock
, ifa_mtx_grp
);
3536 if (!(im6o
->im6o_debug
& IFD_ALLOC
)) {
3537 panic("%s: im6o %p cannot be freed", __func__
, im6o
);
3540 zfree(im6o_zone
, im6o
);
3544 im6o_trace(struct ip6_moptions
*im6o
, int refhold
)
3546 struct ip6_moptions_dbg
*im6o_dbg
= (struct ip6_moptions_dbg
*)im6o
;
3551 if (!(im6o
->im6o_debug
& IFD_DEBUG
)) {
3552 panic("%s: im6o %p has no debug structure", __func__
, im6o
);
3556 cnt
= &im6o_dbg
->im6o_refhold_cnt
;
3557 tr
= im6o_dbg
->im6o_refhold
;
3559 cnt
= &im6o_dbg
->im6o_refrele_cnt
;
3560 tr
= im6o_dbg
->im6o_refrele
;
3563 idx
= atomic_add_16_ov(cnt
, 1) % IM6O_TRACE_HIST_SIZE
;
3564 ctrace_record(&tr
[idx
]);
3567 struct ip6_moptions
*
3568 ip6_allocmoptions(zalloc_flags_t how
)
3570 struct ip6_moptions
*im6o
;
3572 im6o
= zalloc_flags(im6o_zone
, how
| Z_ZERO
);
3574 lck_mtx_init(&im6o
->im6o_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3575 im6o
->im6o_debug
|= IFD_ALLOC
;
3576 if (im6o_debug
!= 0) {
3577 im6o
->im6o_debug
|= IFD_DEBUG
;
3578 im6o
->im6o_trace
= im6o_trace
;
3587 * Set IPv6 outgoing packet options based on advanced API.
3590 ip6_setpktopts(struct mbuf
*control
, struct ip6_pktopts
*opt
,
3591 struct ip6_pktopts
*stickyopt
, int uproto
)
3593 struct cmsghdr
*cm
= NULL
;
3595 if (control
== NULL
|| opt
== NULL
) {
3599 ip6_initpktopts(opt
);
3604 * If stickyopt is provided, make a local copy of the options
3605 * for this particular packet, then override them by ancillary
3607 * XXX: copypktopts() does not copy the cached route to a next
3608 * hop (if any). This is not very good in terms of efficiency,
3609 * but we can allow this since this option should be rarely
3612 if ((error
= copypktopts(opt
, stickyopt
, M_NOWAIT
)) != 0) {
3618 * XXX: Currently, we assume all the optional information is stored
3621 if (control
->m_next
) {
3625 if (control
->m_len
< CMSG_LEN(0)) {
3629 for (cm
= M_FIRST_CMSGHDR(control
);
3630 is_cmsg_valid(control
, cm
);
3631 cm
= M_NXT_CMSGHDR(control
, cm
)) {
3634 if (cm
->cmsg_level
!= IPPROTO_IPV6
) {
3638 error
= ip6_setpktopt(cm
->cmsg_type
, CMSG_DATA(cm
),
3639 cm
->cmsg_len
- CMSG_LEN(0), opt
, 0, 1, uproto
);
3648 * Set a particular packet option, as a sticky option or an ancillary data
3649 * item. "len" can be 0 only when it's a sticky option.
3650 * We have 4 cases of combination of "sticky" and "cmsg":
3651 * "sticky=0, cmsg=0": impossible
3652 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3653 * "sticky=1, cmsg=0": RFC3542 socket option
3654 * "sticky=1, cmsg=1": RFC2292 socket option
3657 ip6_setpktopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
*opt
,
3658 int sticky
, int cmsg
, int uproto
)
3660 int minmtupolicy
, preftemp
;
3662 boolean_t capture_exthdrstat_out
= FALSE
;
3664 if (!sticky
&& !cmsg
) {
3666 printf("ip6_setpktopt: impossible case\n");
3672 * Caller must have ensured that the buffer is at least
3673 * aligned on 32-bit boundary.
3675 VERIFY(IS_P2ALIGNED(buf
, sizeof(u_int32_t
)));
3678 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3679 * not be specified in the context of RFC3542. Conversely,
3680 * RFC3542 types should not be specified in the context of RFC2292.
3684 case IPV6_2292PKTINFO
:
3685 case IPV6_2292HOPLIMIT
:
3686 case IPV6_2292NEXTHOP
:
3687 case IPV6_2292HOPOPTS
:
3688 case IPV6_2292DSTOPTS
:
3689 case IPV6_2292RTHDR
:
3690 case IPV6_2292PKTOPTIONS
:
3694 if (sticky
&& cmsg
) {
3701 case IPV6_RTHDRDSTOPTS
:
3703 case IPV6_USE_MIN_MTU
:
3706 case IPV6_PREFER_TEMPADDR
: /* XXX: not an RFC3542 option */
3712 case IPV6_2292PKTINFO
:
3713 case IPV6_PKTINFO
: {
3714 struct ifnet
*ifp
= NULL
;
3715 struct in6_pktinfo
*pktinfo
;
3717 if (len
!= sizeof(struct in6_pktinfo
)) {
3721 pktinfo
= (struct in6_pktinfo
*)(void *)buf
;
3724 * An application can clear any sticky IPV6_PKTINFO option by
3725 * doing a "regular" setsockopt with ipi6_addr being
3726 * in6addr_any and ipi6_ifindex being zero.
3727 * [RFC 3542, Section 6]
3729 if (optname
== IPV6_PKTINFO
&& opt
->ip6po_pktinfo
&&
3730 pktinfo
->ipi6_ifindex
== 0 &&
3731 IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3732 ip6_clearpktopts(opt
, optname
);
3736 if (uproto
== IPPROTO_TCP
&& optname
== IPV6_PKTINFO
&&
3737 sticky
&& !IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3741 /* validate the interface index if specified. */
3742 ifnet_head_lock_shared();
3744 if (pktinfo
->ipi6_ifindex
> if_index
) {
3749 if (pktinfo
->ipi6_ifindex
) {
3750 ifp
= ifindex2ifnet
[pktinfo
->ipi6_ifindex
];
3760 * We store the address anyway, and let in6_selectsrc()
3761 * validate the specified address. This is because ipi6_addr
3762 * may not have enough information about its scope zone, and
3763 * we may need additional information (such as outgoing
3764 * interface or the scope zone of a destination address) to
3765 * disambiguate the scope.
3766 * XXX: the delay of the validation may confuse the
3767 * application when it is used as a sticky option.
3769 if (opt
->ip6po_pktinfo
== NULL
) {
3770 opt
->ip6po_pktinfo
= _MALLOC(sizeof(*pktinfo
),
3771 M_IP6OPT
, M_NOWAIT
);
3772 if (opt
->ip6po_pktinfo
== NULL
) {
3776 bcopy(pktinfo
, opt
->ip6po_pktinfo
, sizeof(*pktinfo
));
3780 case IPV6_2292HOPLIMIT
:
3781 case IPV6_HOPLIMIT
: {
3785 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3786 * to simplify the ordering among hoplimit options.
3788 if (optname
== IPV6_HOPLIMIT
&& sticky
) {
3792 if (len
!= sizeof(int)) {
3795 hlimp
= (int *)(void *)buf
;
3796 if (*hlimp
< -1 || *hlimp
> IPV6_MAXHLIM
) {
3800 opt
->ip6po_hlim
= *hlimp
;
3807 if (len
!= sizeof(int)) {
3810 tclass
= *(int *)(void *)buf
;
3811 if (tclass
< -1 || tclass
> 255) {
3815 opt
->ip6po_tclass
= tclass
;
3819 case IPV6_2292NEXTHOP
:
3821 error
= suser(kauth_cred_get(), 0);
3826 if (len
== 0) { /* just remove the option */
3827 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3831 /* check if cmsg_len is large enough for sa_len */
3832 if (len
< sizeof(struct sockaddr
) || len
< *buf
) {
3836 switch (SA(buf
)->sa_family
) {
3838 struct sockaddr_in6
*sa6
= SIN6(buf
);
3840 if (sa6
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
3844 if (IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
) ||
3845 IN6_IS_ADDR_MULTICAST(&sa6
->sin6_addr
)) {
3848 if ((error
= sa6_embedscope(sa6
, ip6_use_defzone
))
3854 case AF_LINK
: /* should eventually be supported */
3856 return EAFNOSUPPORT
;
3859 /* turn off the previous option, then set the new option. */
3860 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3861 opt
->ip6po_nexthop
= _MALLOC(*buf
, M_IP6OPT
, M_NOWAIT
);
3862 if (opt
->ip6po_nexthop
== NULL
) {
3865 bcopy(buf
, opt
->ip6po_nexthop
, *buf
);
3868 case IPV6_2292HOPOPTS
:
3869 case IPV6_HOPOPTS
: {
3870 struct ip6_hbh
*hbh
;
3874 * XXX: We don't allow a non-privileged user to set ANY HbH
3875 * options, since per-option restriction has too much
3878 error
= suser(kauth_cred_get(), 0);
3884 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3885 break; /* just remove the option */
3888 /* message length validation */
3889 if (len
< sizeof(struct ip6_hbh
)) {
3892 hbh
= (struct ip6_hbh
*)(void *)buf
;
3893 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
3894 if (len
!= hbhlen
) {
3898 /* turn off the previous option, then set the new option. */
3899 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3900 opt
->ip6po_hbh
= _MALLOC(hbhlen
, M_IP6OPT
, M_NOWAIT
);
3901 if (opt
->ip6po_hbh
== NULL
) {
3904 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
3905 capture_exthdrstat_out
= TRUE
;
3909 case IPV6_2292DSTOPTS
:
3911 case IPV6_RTHDRDSTOPTS
: {
3912 struct ip6_dest
*dest
, **newdest
= NULL
;
3915 error
= suser(kauth_cred_get(), 0);
3921 ip6_clearpktopts(opt
, optname
);
3922 break; /* just remove the option */
3925 /* message length validation */
3926 if (len
< sizeof(struct ip6_dest
)) {
3929 dest
= (struct ip6_dest
*)(void *)buf
;
3930 destlen
= (dest
->ip6d_len
+ 1) << 3;
3931 if (len
!= destlen
) {
3936 * Determine the position that the destination options header
3937 * should be inserted; before or after the routing header.
3940 case IPV6_2292DSTOPTS
:
3942 * The old advacned API is ambiguous on this point.
3943 * Our approach is to determine the position based
3944 * according to the existence of a routing header.
3945 * Note, however, that this depends on the order of the
3946 * extension headers in the ancillary data; the 1st
3947 * part of the destination options header must appear
3948 * before the routing header in the ancillary data,
3950 * RFC3542 solved the ambiguity by introducing
3951 * separate ancillary data or option types.
3953 if (opt
->ip6po_rthdr
== NULL
) {
3954 newdest
= &opt
->ip6po_dest1
;
3956 newdest
= &opt
->ip6po_dest2
;
3959 case IPV6_RTHDRDSTOPTS
:
3960 newdest
= &opt
->ip6po_dest1
;
3963 newdest
= &opt
->ip6po_dest2
;
3967 /* turn off the previous option, then set the new option. */
3968 ip6_clearpktopts(opt
, optname
);
3969 *newdest
= _MALLOC(destlen
, M_IP6OPT
, M_NOWAIT
);
3970 if (*newdest
== NULL
) {
3973 bcopy(dest
, *newdest
, destlen
);
3974 capture_exthdrstat_out
= TRUE
;
3978 case IPV6_2292RTHDR
:
3980 struct ip6_rthdr
*rth
;
3984 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3985 break; /* just remove the option */
3988 /* message length validation */
3989 if (len
< sizeof(struct ip6_rthdr
)) {
3992 rth
= (struct ip6_rthdr
*)(void *)buf
;
3993 rthlen
= (rth
->ip6r_len
+ 1) << 3;
3994 if (len
!= rthlen
) {
3998 switch (rth
->ip6r_type
) {
3999 case IPV6_RTHDR_TYPE_0
:
4000 if (rth
->ip6r_len
== 0) { /* must contain one addr */
4003 if (rth
->ip6r_len
% 2) { /* length must be even */
4006 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
) {
4011 return EINVAL
; /* not supported */
4014 /* turn off the previous option */
4015 ip6_clearpktopts(opt
, IPV6_RTHDR
);
4016 opt
->ip6po_rthdr
= _MALLOC(rthlen
, M_IP6OPT
, M_NOWAIT
);
4017 if (opt
->ip6po_rthdr
== NULL
) {
4020 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
4021 capture_exthdrstat_out
= TRUE
;
4025 case IPV6_USE_MIN_MTU
:
4026 if (len
!= sizeof(int)) {
4029 minmtupolicy
= *(int *)(void *)buf
;
4030 if (minmtupolicy
!= IP6PO_MINMTU_MCASTONLY
&&
4031 minmtupolicy
!= IP6PO_MINMTU_DISABLE
&&
4032 minmtupolicy
!= IP6PO_MINMTU_ALL
) {
4035 opt
->ip6po_minmtu
= minmtupolicy
;
4039 if (len
!= sizeof(int)) {
4043 if (uproto
== IPPROTO_TCP
|| *(int *)(void *)buf
== 0) {
4045 * we ignore this option for TCP sockets.
4046 * (RFC3542 leaves this case unspecified.)
4048 opt
->ip6po_flags
&= ~IP6PO_DONTFRAG
;
4050 opt
->ip6po_flags
|= IP6PO_DONTFRAG
;
4054 case IPV6_PREFER_TEMPADDR
:
4055 if (len
!= sizeof(int)) {
4058 preftemp
= *(int *)(void *)buf
;
4059 if (preftemp
!= IP6PO_TEMPADDR_SYSTEM
&&
4060 preftemp
!= IP6PO_TEMPADDR_NOTPREFER
&&
4061 preftemp
!= IP6PO_TEMPADDR_PREFER
) {
4064 opt
->ip6po_prefer_tempaddr
= preftemp
;
4069 } /* end of switch */
4071 if (capture_exthdrstat_out
) {
4072 if (uproto
== IPPROTO_TCP
) {
4073 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_stream_exthdr_out
);
4074 } else if (uproto
== IPPROTO_UDP
) {
4075 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_dgram_exthdr_out
);
4083 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
4084 * packet to the input queue of a specified interface. Note that this
4085 * calls the output routine of the loopback "driver", but with an interface
4086 * pointer that might NOT be &loif -- easier than replicating that code here.
4089 ip6_mloopback(struct ifnet
*srcifp
, struct ifnet
*origifp
, struct mbuf
*m
,
4090 struct sockaddr_in6
*dst
, uint32_t optlen
, int32_t nxt0
)
4093 struct ip6_hdr
*ip6
;
4094 struct in6_addr src
;
4096 if (lo_ifp
== NULL
) {
4101 * Copy the packet header as it's needed for the checksum.
4102 * Make sure to deep-copy IPv6 header portion in case the data
4103 * is in an mbuf cluster, so that we can safely override the IPv6
4104 * header portion later.
4106 copym
= m_copym_mode(m
, 0, M_COPYALL
, M_DONTWAIT
, M_COPYM_COPY_HDR
);
4107 if (copym
!= NULL
&& ((copym
->m_flags
& M_EXT
) ||
4108 copym
->m_len
< sizeof(struct ip6_hdr
))) {
4109 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
4112 if (copym
== NULL
) {
4116 ip6
= mtod(copym
, struct ip6_hdr
*);
4119 * clear embedded scope identifiers if necessary.
4120 * in6_clearscope will touch the addresses only when necessary.
4122 in6_clearscope(&ip6
->ip6_src
);
4123 in6_clearscope(&ip6
->ip6_dst
);
4125 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
) {
4126 in6_delayed_cksum_offset(copym
, 0, optlen
, nxt0
);
4130 * Stuff the 'real' ifp into the pkthdr, to be used in matching
4131 * in ip6_input(); we need the loopback ifp/dl_tag passed as args
4132 * to make the loopback driver compliant with the data link
4135 copym
->m_pkthdr
.rcvif
= origifp
;
4138 * Also record the source interface (which owns the source address).
4139 * This is basically a stripped down version of ifa_foraddr6().
4141 if (srcifp
== NULL
) {
4142 struct in6_ifaddr
*ia
;
4144 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
4145 TAILQ_FOREACH(ia
, IN6ADDR_HASH(&src
), ia6_hash
) {
4146 IFA_LOCK_SPIN(&ia
->ia_ifa
);
4147 /* compare against src addr with embedded scope */
4148 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, &src
)) {
4149 srcifp
= ia
->ia_ifp
;
4150 IFA_UNLOCK(&ia
->ia_ifa
);
4153 IFA_UNLOCK(&ia
->ia_ifa
);
4155 lck_rw_done(&in6_ifaddr_rwlock
);
4157 if (srcifp
!= NULL
) {
4158 ip6_setsrcifaddr_info(copym
, srcifp
->if_index
, NULL
);
4160 ip6_setdstifaddr_info(copym
, origifp
->if_index
, NULL
);
4162 dlil_output(lo_ifp
, PF_INET6
, copym
, NULL
, SA(dst
), 0, NULL
);
4166 * Chop IPv6 header off from the payload.
4169 ip6_splithdr(struct mbuf
*m
, struct ip6_exthdrs
*exthdrs
)
4172 struct ip6_hdr
*ip6
;
4174 ip6
= mtod(m
, struct ip6_hdr
*);
4175 if (m
->m_len
> sizeof(*ip6
)) {
4176 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
4181 M_COPY_PKTHDR(mh
, m
);
4182 MH_ALIGN(mh
, sizeof(*ip6
));
4183 m
->m_flags
&= ~M_PKTHDR
;
4184 m
->m_len
-= sizeof(*ip6
);
4185 m
->m_data
+= sizeof(*ip6
);
4188 m
->m_len
= sizeof(*ip6
);
4189 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof(*ip6
));
4191 exthdrs
->ip6e_ip6
= m
;
4196 ip6_output_checksum(struct ifnet
*ifp
, uint32_t mtu
, struct mbuf
*m
,
4197 int nxt0
, uint32_t tlen
, uint32_t optlen
)
4199 uint32_t sw_csum
, hwcap
= ifp
->if_hwassist
;
4200 int tso
= TSO_IPV6_OK(ifp
, m
);
4203 /* do all in software; checksum offload is disabled */
4204 sw_csum
= CSUM_DELAY_IPV6_DATA
& m
->m_pkthdr
.csum_flags
;
4206 /* do in software what the hardware cannot */
4207 sw_csum
= m
->m_pkthdr
.csum_flags
&
4208 ~IF_HWASSIST_CSUM_FLAGS(hwcap
);
4212 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
4213 m
->m_pkthdr
.csum_flags
);
4214 } else if (!(sw_csum
& CSUM_DELAY_IPV6_DATA
) &&
4215 (hwcap
& CSUM_PARTIAL
)) {
4217 * Partial checksum offload, ere), if no extension headers,
4218 * and TCP only (no UDP support, as the hardware may not be
4219 * able to convert +0 to -0 (0xffff) per RFC1122 4.1.3.4.
4220 * unless the interface supports "invert zero" capability.)
4222 if (hwcksum_tx
&& !tso
&&
4223 ((m
->m_pkthdr
.csum_flags
& CSUM_TCPIPV6
) ||
4224 ((hwcap
& CSUM_ZERO_INVERT
) &&
4225 (m
->m_pkthdr
.csum_flags
& CSUM_ZERO_INVERT
))) &&
4227 uint16_t start
= sizeof(struct ip6_hdr
);
4229 m
->m_pkthdr
.csum_data
& 0xffff;
4230 m
->m_pkthdr
.csum_flags
|=
4231 (CSUM_DATA_VALID
| CSUM_PARTIAL
);
4232 m
->m_pkthdr
.csum_tx_stuff
= (ulpoff
+ start
);
4233 m
->m_pkthdr
.csum_tx_start
= start
;
4236 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
4237 m
->m_pkthdr
.csum_flags
);
4241 if (sw_csum
& CSUM_DELAY_IPV6_DATA
) {
4242 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
4243 sw_csum
&= ~CSUM_DELAY_IPV6_DATA
;
4248 * Drop off bits that aren't supported by hardware;
4249 * also make sure to preserve non-checksum related bits.
4251 m
->m_pkthdr
.csum_flags
=
4252 ((m
->m_pkthdr
.csum_flags
&
4253 (IF_HWASSIST_CSUM_FLAGS(hwcap
) | CSUM_DATA_VALID
)) |
4254 (m
->m_pkthdr
.csum_flags
& ~IF_HWASSIST_CSUM_MASK
));
4256 /* drop all bits; checksum offload is disabled */
4257 m
->m_pkthdr
.csum_flags
= 0;
4262 * Compute IPv6 extension header length.
4265 ip6_optlen(struct in6pcb
*in6p
)
4269 if (!in6p
->in6p_outputopts
) {
4275 (((struct ip6_ext *)(x)) ? \
4276 (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
4278 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
4279 if (in6p
->in6p_outputopts
->ip6po_rthdr
) {
4280 /* dest1 is valid with rthdr only */
4281 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
4283 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
4284 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);
4290 sysctl_reset_ip6_output_stats SYSCTL_HANDLER_ARGS
4292 #pragma unused(arg1, arg2)
4295 i
= ip6_output_measure
;
4296 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
4297 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
4301 if (i
< 0 || i
> 1) {
4305 if (ip6_output_measure
!= i
&& i
== 1) {
4306 net_perf_initialize(&net_perf
, ip6_output_measure_bins
);
4308 ip6_output_measure
= i
;
4314 sysctl_ip6_output_measure_bins SYSCTL_HANDLER_ARGS
4316 #pragma unused(arg1, arg2)
4320 i
= ip6_output_measure_bins
;
4321 error
= sysctl_handle_quad(oidp
, &i
, 0, req
);
4322 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
4326 if (!net_perf_validate_bins(i
)) {
4330 ip6_output_measure_bins
= i
;
4336 sysctl_ip6_output_getperf SYSCTL_HANDLER_ARGS
4338 #pragma unused(oidp, arg1, arg2)
4339 if (req
->oldptr
== USER_ADDR_NULL
) {
4340 req
->oldlen
= (size_t)sizeof(struct ipstat
);
4343 return SYSCTL_OUT(req
, &net_perf
, MIN(sizeof(net_perf
), req
->oldlen
));