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
);
1914 ip6f
->ip6f_reserved
= 0;
1915 ip6f
->ip6f_ident
= id
;
1916 ip6f
->ip6f_nxt
= nextproto
;
1917 ip6stat
.ip6s_ofragments
++;
1918 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1922 /* free all the fragments created */
1923 if (first_mbufp
!= NULL
) {
1924 m_freem_list(first_mbufp
);
1929 /* successful fragmenting */
1931 *mptr
= first_mbufp
;
1932 last_mbufp
->m_nextpkt
= NULL
;
1933 ip6stat
.ip6s_fragmented
++;
1934 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1941 ip6_copyexthdr(struct mbuf
**mp
, caddr_t hdr
, int hlen
)
1945 if (hlen
> MCLBYTES
) {
1946 return ENOBUFS
; /* XXX */
1948 MGET(m
, M_DONTWAIT
, MT_DATA
);
1954 MCLGET(m
, M_DONTWAIT
);
1955 if (!(m
->m_flags
& M_EXT
)) {
1962 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1970 ip6_out_cksum_stats(int proto
, u_int32_t len
)
1974 tcp_out6_cksum_stats(len
);
1977 udp_out6_cksum_stats(len
);
1980 /* keep only TCP or UDP stats for now */
1986 * Process a delayed payload checksum calculation (outbound path.)
1988 * hoff is the number of bytes beyond the mbuf data pointer which
1989 * points to the IPv6 header. optlen is the number of bytes, if any,
1990 * between the end of IPv6 header and the beginning of the ULP payload
1991 * header, which represents the extension headers. If optlen is less
1992 * than zero, this routine will bail when it detects extension headers.
1994 * Returns a bitmask representing all the work done in software.
1997 in6_finalize_cksum(struct mbuf
*m
, uint32_t hoff
, int32_t optlen
,
1998 int32_t nxt0
, uint32_t csum_flags
)
2000 unsigned char buf
[sizeof(struct ip6_hdr
)] __attribute__((aligned(8)));
2001 struct ip6_hdr
*ip6
;
2002 uint32_t offset
, mlen
, hlen
, olen
, sw_csum
;
2003 uint16_t csum
, ulpoff
, plen
;
2006 _CASSERT(sizeof(csum
) == sizeof(uint16_t));
2007 VERIFY(m
->m_flags
& M_PKTHDR
);
2009 sw_csum
= (csum_flags
& m
->m_pkthdr
.csum_flags
);
2011 if ((sw_csum
&= CSUM_DELAY_IPV6_DATA
) == 0) {
2015 mlen
= m
->m_pkthdr
.len
; /* total mbuf len */
2016 hlen
= sizeof(*ip6
); /* IPv6 header len */
2018 /* sanity check (need at least IPv6 header) */
2019 if (mlen
< (hoff
+ hlen
)) {
2020 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr "
2021 "(%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
);
2026 * In case the IPv6 header is not contiguous, or not 32-bit
2027 * aligned, copy it to a local buffer.
2029 if ((hoff
+ hlen
) > m
->m_len
||
2030 !IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
) + hoff
)) {
2031 m_copydata(m
, hoff
, hlen
, (caddr_t
)buf
);
2032 ip6
= (struct ip6_hdr
*)(void *)buf
;
2034 ip6
= (struct ip6_hdr
*)(void *)(m
->m_data
+ hoff
);
2038 plen
= ntohs(ip6
->ip6_plen
);
2039 if (plen
!= (mlen
- (hoff
+ hlen
))) {
2040 plen
= OSSwapInt16(plen
);
2041 if (plen
!= (mlen
- (hoff
+ hlen
))) {
2042 /* Don't complain for jumbograms */
2043 if (plen
!= 0 || nxt
!= IPPROTO_HOPOPTS
) {
2044 printf("%s: mbuf 0x%llx proto %d IPv6 "
2045 "plen %d (%x) [swapped %d (%x)] doesn't "
2046 "match actual packet length; %d is used "
2047 "instead\n", __func__
,
2048 (uint64_t)VM_KERNEL_ADDRPERM(m
), nxt
,
2049 ip6
->ip6_plen
, ip6
->ip6_plen
, plen
, plen
,
2050 (mlen
- (hoff
+ hlen
)));
2052 plen
= (uint16_t)(mlen
- (hoff
+ hlen
));
2057 /* next header isn't TCP/UDP and we don't know optlen, bail */
2058 if (nxt
!= IPPROTO_TCP
&& nxt
!= IPPROTO_UDP
) {
2064 /* caller supplied the original transport number; use it */
2066 nxt
= (uint8_t)nxt0
;
2071 offset
= hoff
+ hlen
+ olen
; /* ULP header */
2074 if (mlen
< offset
) {
2075 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr+ext_hdr "
2076 "(%u+%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
, olen
);
2081 * offset is added to the lower 16-bit value of csum_data,
2082 * which is expected to contain the ULP offset; therefore
2083 * CSUM_PARTIAL offset adjustment must be undone.
2085 if ((m
->m_pkthdr
.csum_flags
& (CSUM_PARTIAL
| CSUM_DATA_VALID
)) ==
2086 (CSUM_PARTIAL
| CSUM_DATA_VALID
)) {
2088 * Get back the original ULP offset (this will
2089 * undo the CSUM_PARTIAL logic in ip6_output.)
2091 m
->m_pkthdr
.csum_data
= (m
->m_pkthdr
.csum_tx_stuff
-
2092 m
->m_pkthdr
.csum_tx_start
);
2095 ulpoff
= (m
->m_pkthdr
.csum_data
& 0xffff); /* ULP csum offset */
2097 if (mlen
< (ulpoff
+ sizeof(csum
))) {
2098 panic("%s: mbuf %p pkt len (%u) proto %d invalid ULP "
2099 "cksum offset (%u) cksum flags 0x%x\n", __func__
,
2100 m
, mlen
, nxt
, ulpoff
, m
->m_pkthdr
.csum_flags
);
2104 csum
= inet6_cksum(m
, 0, offset
, plen
- olen
);
2107 ip6_out_cksum_stats(nxt
, plen
- olen
);
2109 /* RFC1122 4.1.3.4 */
2111 (m
->m_pkthdr
.csum_flags
& (CSUM_UDPIPV6
| CSUM_ZERO_INVERT
))) {
2115 /* Insert the checksum in the ULP csum field */
2117 if ((offset
+ sizeof(csum
)) > m
->m_len
) {
2118 m_copyback(m
, offset
, sizeof(csum
), &csum
);
2119 } else if (IP6_HDR_ALIGNED_P(mtod(m
, char *) + hoff
)) {
2120 *(uint16_t *)(void *)(mtod(m
, char *) + offset
) = csum
;
2122 bcopy(&csum
, (mtod(m
, char *) + offset
), sizeof(csum
));
2124 m
->m_pkthdr
.csum_flags
&= ~(CSUM_DELAY_IPV6_DATA
| CSUM_DATA_VALID
|
2125 CSUM_PARTIAL
| CSUM_ZERO_INVERT
);
2132 * Insert jumbo payload option.
2135 ip6_insert_jumboopt(struct ip6_exthdrs
*exthdrs
, u_int32_t plen
)
2141 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
2144 * If there is no hop-by-hop options header, allocate new one.
2145 * If there is one but it doesn't have enough space to store the
2146 * jumbo payload option, allocate a cluster to store the whole options.
2147 * Otherwise, use it to store the options.
2149 if (exthdrs
->ip6e_hbh
== NULL
) {
2150 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
2154 mopt
->m_len
= JUMBOOPTLEN
;
2155 optbuf
= mtod(mopt
, u_char
*);
2156 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
2157 exthdrs
->ip6e_hbh
= mopt
;
2159 struct ip6_hbh
*hbh
;
2161 mopt
= exthdrs
->ip6e_hbh
;
2162 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
2165 * - exthdrs->ip6e_hbh is not referenced from places
2166 * other than exthdrs.
2167 * - exthdrs->ip6e_hbh is not an mbuf chain.
2169 u_int32_t oldoptlen
= mopt
->m_len
;
2173 * XXX: give up if the whole (new) hbh header does
2174 * not fit even in an mbuf cluster.
2176 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
) {
2181 * As a consequence, we must always prepare a cluster
2184 MGET(n
, M_DONTWAIT
, MT_DATA
);
2186 MCLGET(n
, M_DONTWAIT
);
2187 if (!(n
->m_flags
& M_EXT
)) {
2195 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
2196 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
2198 optbuf
= mtod(n
, u_char
*) + oldoptlen
;
2200 mopt
= exthdrs
->ip6e_hbh
= n
;
2202 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
2203 mopt
->m_len
+= JUMBOOPTLEN
;
2205 optbuf
[0] = IP6OPT_PADN
;
2209 * Adjust the header length according to the pad and
2210 * the jumbo payload option.
2212 hbh
= mtod(mopt
, struct ip6_hbh
*);
2213 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
2216 /* fill in the option. */
2217 optbuf
[2] = IP6OPT_JUMBO
;
2219 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
2220 bcopy(&v
, &optbuf
[4], sizeof(u_int32_t
));
2222 /* finally, adjust the packet header length */
2223 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
2230 * Insert fragment header and copy unfragmentable header portions.
2233 ip6_insertfraghdr(struct mbuf
*m0
, struct mbuf
*m
, int hlen
,
2234 struct ip6_frag
**frghdrp
)
2236 struct mbuf
*n
, *mlast
;
2238 if (hlen
> sizeof(struct ip6_hdr
)) {
2239 n
= m_copym(m0
, sizeof(struct ip6_hdr
),
2240 hlen
- sizeof(struct ip6_hdr
), M_DONTWAIT
);
2249 /* Search for the last mbuf of unfragmentable part. */
2250 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
) {
2254 if (!(mlast
->m_flags
& M_EXT
) &&
2255 M_TRAILINGSPACE(mlast
) >= sizeof(struct ip6_frag
)) {
2256 /* use the trailing space of the last mbuf for the frag hdr */
2257 *frghdrp
= (struct ip6_frag
*)(mtod(mlast
, caddr_t
) +
2259 mlast
->m_len
+= sizeof(struct ip6_frag
);
2260 m
->m_pkthdr
.len
+= sizeof(struct ip6_frag
);
2262 /* allocate a new mbuf for the fragment header */
2265 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
2269 mfrg
->m_len
= sizeof(struct ip6_frag
);
2270 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
2271 mlast
->m_next
= mfrg
;
2278 ip6_getpmtu(struct route_in6
*ro_pmtu
, struct route_in6
*ro
,
2279 struct ifnet
*ifp
, struct in6_addr
*dst
, u_int32_t
*mtup
)
2285 if (ro_pmtu
!= ro
) {
2286 /* The first hop and the final destination may differ. */
2287 struct sockaddr_in6
*sa6_dst
= SIN6(&ro_pmtu
->ro_dst
);
2288 if (ROUTE_UNUSABLE(ro_pmtu
) ||
2289 !IN6_ARE_ADDR_EQUAL(&sa6_dst
->sin6_addr
, dst
)) {
2290 ROUTE_RELEASE(ro_pmtu
);
2293 if (ro_pmtu
->ro_rt
== NULL
) {
2294 bzero(sa6_dst
, sizeof(*sa6_dst
));
2295 sa6_dst
->sin6_family
= AF_INET6
;
2296 sa6_dst
->sin6_len
= sizeof(struct sockaddr_in6
);
2297 sa6_dst
->sin6_addr
= *dst
;
2299 rtalloc_scoped((struct route
*)ro_pmtu
,
2300 ifp
!= NULL
? ifp
->if_index
: IFSCOPE_NONE
);
2304 if (ro_pmtu
->ro_rt
!= NULL
) {
2308 ifp
= ro_pmtu
->ro_rt
->rt_ifp
;
2310 /* Access without acquiring nd_ifinfo lock for performance */
2311 ifmtu
= IN6_LINKMTU(ifp
);
2314 * Access rmx_mtu without holding the route entry lock,
2315 * for performance; this isn't something that changes
2316 * often, so optimize.
2318 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
2319 if (mtu
> ifmtu
|| mtu
== 0) {
2321 * The MTU on the route is larger than the MTU on
2322 * the interface! This shouldn't happen, unless the
2323 * MTU of the interface has been changed after the
2324 * interface was brought up. Change the MTU in the
2325 * route to match the interface MTU (as long as the
2326 * field isn't locked).
2328 * if MTU on the route is 0, we need to fix the MTU.
2329 * this case happens with path MTU discovery timeouts.
2332 if (!(ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
2333 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
2338 /* Don't hold nd_ifinfo lock for performance */
2339 mtu
= IN6_LINKMTU(ifp
);
2341 error
= EHOSTUNREACH
; /* XXX */
2350 * IP6 socket option processing.
2353 ip6_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2355 int optdatalen
, uproto
;
2358 struct inpcb
*in6p
= sotoinpcb(so
);
2359 int error
= 0, optval
= 0;
2360 int level
, op
= -1, optname
= 0;
2363 lck_mtx_t
*mutex_held
= NULL
;
2365 VERIFY(sopt
!= NULL
);
2367 level
= sopt
->sopt_level
;
2368 op
= sopt
->sopt_dir
;
2369 optname
= sopt
->sopt_name
;
2370 optlen
= sopt
->sopt_valsize
;
2372 uproto
= (int)SOCK_PROTO(so
);
2374 privileged
= (proc_suser(p
) == 0);
2376 if (level
== IPPROTO_IPV6
) {
2377 boolean_t capture_exthdrstat_in
= FALSE
;
2380 mutex_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
2382 * Wait if we are in the middle of ip6_output
2383 * as we unlocked the socket there and don't
2384 * want to overwrite the IP options
2386 if (in6p
->inp_sndinprog_cnt
> 0) {
2387 in6p
->inp_sndingprog_waiters
++;
2389 while (in6p
->inp_sndinprog_cnt
> 0) {
2390 msleep(&in6p
->inp_sndinprog_cnt
, mutex_held
,
2391 PSOCK
| PCATCH
, "inp_sndinprog_cnt",
2394 in6p
->inp_sndingprog_waiters
--;
2397 case IPV6_2292PKTOPTIONS
: {
2400 error
= soopt_getm(sopt
, &m
);
2404 error
= soopt_mcopyin(sopt
, m
);
2408 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
2415 * Use of some Hop-by-Hop options or some
2416 * Destination options, might require special
2417 * privilege. That is, normal applications
2418 * (without special privilege) might be forbidden
2419 * from setting certain options in outgoing packets,
2420 * and might never see certain options in received
2421 * packets. [RFC 2292 Section 6]
2422 * KAME specific note:
2423 * KAME prevents non-privileged users from sending or
2424 * receiving ANY hbh/dst options in order to avoid
2425 * overhead of parsing options in the kernel.
2427 case IPV6_RECVHOPOPTS
:
2428 case IPV6_RECVDSTOPTS
:
2429 case IPV6_RECVRTHDRDSTOPTS
:
2434 case IPV6_UNICAST_HOPS
:
2436 case IPV6_RECVPKTINFO
:
2437 case IPV6_RECVHOPLIMIT
:
2438 case IPV6_RECVRTHDR
:
2439 case IPV6_RECVPATHMTU
:
2440 case IPV6_RECVTCLASS
:
2442 case IPV6_AUTOFLOWLABEL
:
2443 if (optlen
!= sizeof(int)) {
2447 error
= sooptcopyin(sopt
, &optval
,
2448 sizeof(optval
), sizeof(optval
));
2454 case IPV6_UNICAST_HOPS
:
2455 if (optval
< -1 || optval
>= 256) {
2458 /* -1 = kernel default */
2459 in6p
->in6p_hops
= (short)optval
;
2460 if (in6p
->inp_vflag
&
2467 #define OPTSET(bit) do { \
2469 in6p->inp_flags |= (bit); \
2471 in6p->inp_flags &= ~(bit); \
2474 #define OPTSET2292(bit) do { \
2475 in6p->inp_flags |= IN6P_RFC2292; \
2477 in6p->inp_flags |= (bit); \
2479 in6p->inp_flags &= ~(bit); \
2482 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
2484 case IPV6_RECVPKTINFO
:
2485 /* cannot mix with RFC2292 */
2486 if (OPTBIT(IN6P_RFC2292
)) {
2490 OPTSET(IN6P_PKTINFO
);
2493 case IPV6_HOPLIMIT
: {
2494 struct ip6_pktopts
**optp
;
2496 /* cannot mix with RFC2292 */
2497 if (OPTBIT(IN6P_RFC2292
)) {
2501 optp
= &in6p
->in6p_outputopts
;
2502 error
= ip6_pcbopt(IPV6_HOPLIMIT
,
2503 (u_char
*)&optval
, sizeof(optval
),
2508 case IPV6_RECVHOPLIMIT
:
2509 /* cannot mix with RFC2292 */
2510 if (OPTBIT(IN6P_RFC2292
)) {
2514 OPTSET(IN6P_HOPLIMIT
);
2517 case IPV6_RECVHOPOPTS
:
2518 /* cannot mix with RFC2292 */
2519 if (OPTBIT(IN6P_RFC2292
)) {
2523 OPTSET(IN6P_HOPOPTS
);
2524 capture_exthdrstat_in
= TRUE
;
2527 case IPV6_RECVDSTOPTS
:
2528 /* cannot mix with RFC2292 */
2529 if (OPTBIT(IN6P_RFC2292
)) {
2533 OPTSET(IN6P_DSTOPTS
);
2534 capture_exthdrstat_in
= TRUE
;
2537 case IPV6_RECVRTHDRDSTOPTS
:
2538 /* cannot mix with RFC2292 */
2539 if (OPTBIT(IN6P_RFC2292
)) {
2543 OPTSET(IN6P_RTHDRDSTOPTS
);
2544 capture_exthdrstat_in
= TRUE
;
2547 case IPV6_RECVRTHDR
:
2548 /* cannot mix with RFC2292 */
2549 if (OPTBIT(IN6P_RFC2292
)) {
2554 capture_exthdrstat_in
= TRUE
;
2557 case IPV6_RECVPATHMTU
:
2559 * We ignore this option for TCP
2561 * (RFC3542 leaves this case
2564 if (uproto
!= IPPROTO_TCP
) {
2571 * make setsockopt(IPV6_V6ONLY)
2572 * available only prior to bind(2).
2573 * see ipng mailing list, Jun 22 2001.
2575 if (in6p
->inp_lport
||
2576 !IN6_IS_ADDR_UNSPECIFIED(
2577 &in6p
->in6p_laddr
)) {
2581 OPTSET(IN6P_IPV6_V6ONLY
);
2583 in6p
->inp_vflag
&= ~INP_IPV4
;
2585 in6p
->inp_vflag
|= INP_IPV4
;
2589 case IPV6_RECVTCLASS
:
2590 /* we can mix with RFC2292 */
2591 OPTSET(IN6P_TCLASS
);
2594 case IPV6_AUTOFLOWLABEL
:
2595 OPTSET(IN6P_AUTOFLOWLABEL
);
2602 case IPV6_USE_MIN_MTU
:
2603 case IPV6_PREFER_TEMPADDR
: {
2604 struct ip6_pktopts
**optp
;
2606 if (optlen
!= sizeof(optval
)) {
2610 error
= sooptcopyin(sopt
, &optval
,
2611 sizeof(optval
), sizeof(optval
));
2616 optp
= &in6p
->in6p_outputopts
;
2617 error
= ip6_pcbopt(optname
, (u_char
*)&optval
,
2618 sizeof(optval
), optp
, uproto
);
2620 if (optname
== IPV6_TCLASS
) {
2621 // Add in the ECN flags
2622 u_int8_t tos
= (in6p
->inp_ip_tos
& ~IPTOS_ECN_MASK
);
2623 u_int8_t ecn
= optval
& IPTOS_ECN_MASK
;
2624 in6p
->inp_ip_tos
= tos
| ecn
;
2629 case IPV6_2292PKTINFO
:
2630 case IPV6_2292HOPLIMIT
:
2631 case IPV6_2292HOPOPTS
:
2632 case IPV6_2292DSTOPTS
:
2633 case IPV6_2292RTHDR
:
2635 if (optlen
!= sizeof(int)) {
2639 error
= sooptcopyin(sopt
, &optval
,
2640 sizeof(optval
), sizeof(optval
));
2645 case IPV6_2292PKTINFO
:
2646 OPTSET2292(IN6P_PKTINFO
);
2648 case IPV6_2292HOPLIMIT
:
2649 OPTSET2292(IN6P_HOPLIMIT
);
2651 case IPV6_2292HOPOPTS
:
2653 * Check super-user privilege.
2654 * See comments for IPV6_RECVHOPOPTS.
2659 OPTSET2292(IN6P_HOPOPTS
);
2660 capture_exthdrstat_in
= TRUE
;
2662 case IPV6_2292DSTOPTS
:
2666 OPTSET2292(IN6P_DSTOPTS
|
2667 IN6P_RTHDRDSTOPTS
); /* XXX */
2668 capture_exthdrstat_in
= TRUE
;
2670 case IPV6_2292RTHDR
:
2671 OPTSET2292(IN6P_RTHDR
);
2672 capture_exthdrstat_in
= TRUE
;
2677 case IPV6_3542PKTINFO
:
2678 case IPV6_3542HOPOPTS
:
2679 case IPV6_3542RTHDR
:
2680 case IPV6_3542DSTOPTS
:
2681 case IPV6_RTHDRDSTOPTS
:
2682 case IPV6_3542NEXTHOP
: {
2683 struct ip6_pktopts
**optp
;
2684 /* new advanced API (RFC3542) */
2687 /* cannot mix with RFC2292 */
2688 if (OPTBIT(IN6P_RFC2292
)) {
2692 error
= soopt_getm(sopt
, &m
);
2696 error
= soopt_mcopyin(sopt
, m
);
2701 optp
= &in6p
->in6p_outputopts
;
2702 error
= ip6_pcbopt(optname
, mtod(m
, u_char
*),
2703 m
->m_len
, optp
, uproto
);
2708 case IPV6_MULTICAST_IF
:
2709 case IPV6_MULTICAST_HOPS
:
2710 case IPV6_MULTICAST_LOOP
:
2711 case IPV6_JOIN_GROUP
:
2712 case IPV6_LEAVE_GROUP
:
2714 case MCAST_BLOCK_SOURCE
:
2715 case MCAST_UNBLOCK_SOURCE
:
2716 case MCAST_JOIN_GROUP
:
2717 case MCAST_LEAVE_GROUP
:
2718 case MCAST_JOIN_SOURCE_GROUP
:
2719 case MCAST_LEAVE_SOURCE_GROUP
:
2720 error
= ip6_setmoptions(in6p
, sopt
);
2723 case IPV6_PORTRANGE
:
2724 error
= sooptcopyin(sopt
, &optval
,
2725 sizeof(optval
), sizeof(optval
));
2731 case IPV6_PORTRANGE_DEFAULT
:
2732 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2733 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2736 case IPV6_PORTRANGE_HIGH
:
2737 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2738 in6p
->inp_flags
|= INP_HIGHPORT
;
2741 case IPV6_PORTRANGE_LOW
:
2742 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2743 in6p
->inp_flags
|= INP_LOWPORT
;
2752 case IPV6_IPSEC_POLICY
: {
2757 if ((error
= soopt_getm(sopt
, &m
)) != 0) {
2760 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) {
2764 req
= mtod(m
, caddr_t
);
2766 error
= ipsec6_set_policy(in6p
, optname
, req
,
2773 * IPv6 variant of IP_BOUND_IF; for details see
2774 * comments on IP_BOUND_IF in ip_ctloutput().
2777 /* This option is settable only on IPv6 */
2778 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2783 error
= sooptcopyin(sopt
, &optval
,
2784 sizeof(optval
), sizeof(optval
));
2790 error
= inp_bindif(in6p
, optval
, NULL
);
2793 case IPV6_NO_IFT_CELLULAR
:
2794 /* This option is settable only for IPv6 */
2795 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2800 error
= sooptcopyin(sopt
, &optval
,
2801 sizeof(optval
), sizeof(optval
));
2807 /* once set, it cannot be unset */
2808 if (!optval
&& INP_NO_CELLULAR(in6p
)) {
2813 error
= so_set_restrictions(so
,
2814 SO_RESTRICT_DENY_CELLULAR
);
2818 /* This option is not settable */
2823 error
= ENOPROTOOPT
;
2826 if (capture_exthdrstat_in
) {
2827 if (uproto
== IPPROTO_TCP
) {
2828 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_stream_exthdr_in
);
2829 } else if (uproto
== IPPROTO_UDP
) {
2830 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_dgram_exthdr_in
);
2837 case IPV6_2292PKTOPTIONS
:
2839 * RFC3542 (effectively) deprecated the
2840 * semantics of the 2292-style pktoptions.
2841 * Since it was not reliable in nature (i.e.,
2842 * applications had to expect the lack of some
2843 * information after all), it would make sense
2844 * to simplify this part by always returning
2847 sopt
->sopt_valsize
= 0;
2850 case IPV6_RECVHOPOPTS
:
2851 case IPV6_RECVDSTOPTS
:
2852 case IPV6_RECVRTHDRDSTOPTS
:
2853 case IPV6_UNICAST_HOPS
:
2854 case IPV6_RECVPKTINFO
:
2855 case IPV6_RECVHOPLIMIT
:
2856 case IPV6_RECVRTHDR
:
2857 case IPV6_RECVPATHMTU
:
2859 case IPV6_PORTRANGE
:
2860 case IPV6_RECVTCLASS
:
2861 case IPV6_AUTOFLOWLABEL
:
2863 case IPV6_RECVHOPOPTS
:
2864 optval
= OPTBIT(IN6P_HOPOPTS
);
2867 case IPV6_RECVDSTOPTS
:
2868 optval
= OPTBIT(IN6P_DSTOPTS
);
2871 case IPV6_RECVRTHDRDSTOPTS
:
2872 optval
= OPTBIT(IN6P_RTHDRDSTOPTS
);
2875 case IPV6_UNICAST_HOPS
:
2876 optval
= in6p
->in6p_hops
;
2879 case IPV6_RECVPKTINFO
:
2880 optval
= OPTBIT(IN6P_PKTINFO
);
2883 case IPV6_RECVHOPLIMIT
:
2884 optval
= OPTBIT(IN6P_HOPLIMIT
);
2887 case IPV6_RECVRTHDR
:
2888 optval
= OPTBIT(IN6P_RTHDR
);
2891 case IPV6_RECVPATHMTU
:
2892 optval
= OPTBIT(IN6P_MTU
);
2896 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
2899 case IPV6_PORTRANGE
: {
2901 flags
= in6p
->inp_flags
;
2902 if (flags
& INP_HIGHPORT
) {
2903 optval
= IPV6_PORTRANGE_HIGH
;
2904 } else if (flags
& INP_LOWPORT
) {
2905 optval
= IPV6_PORTRANGE_LOW
;
2911 case IPV6_RECVTCLASS
:
2912 optval
= OPTBIT(IN6P_TCLASS
);
2915 case IPV6_AUTOFLOWLABEL
:
2916 optval
= OPTBIT(IN6P_AUTOFLOWLABEL
);
2922 error
= sooptcopyout(sopt
, &optval
,
2926 case IPV6_PATHMTU
: {
2928 struct ip6_mtuinfo mtuinfo
;
2929 struct route_in6 sro
;
2931 bzero(&sro
, sizeof(sro
));
2933 if (!(so
->so_state
& SS_ISCONNECTED
)) {
2937 * XXX: we dot not consider the case of source
2938 * routing, or optional information to specify
2939 * the outgoing interface.
2941 error
= ip6_getpmtu(&sro
, NULL
, NULL
,
2942 &in6p
->in6p_faddr
, &pmtu
);
2943 ROUTE_RELEASE(&sro
);
2947 if (pmtu
> IPV6_MAXPACKET
) {
2948 pmtu
= IPV6_MAXPACKET
;
2951 bzero(&mtuinfo
, sizeof(mtuinfo
));
2952 mtuinfo
.ip6m_mtu
= (u_int32_t
)pmtu
;
2953 optdata
= (void *)&mtuinfo
;
2954 optdatalen
= sizeof(mtuinfo
);
2955 error
= sooptcopyout(sopt
, optdata
,
2960 case IPV6_2292PKTINFO
:
2961 case IPV6_2292HOPLIMIT
:
2962 case IPV6_2292HOPOPTS
:
2963 case IPV6_2292RTHDR
:
2964 case IPV6_2292DSTOPTS
:
2966 case IPV6_2292PKTINFO
:
2967 optval
= OPTBIT(IN6P_PKTINFO
);
2969 case IPV6_2292HOPLIMIT
:
2970 optval
= OPTBIT(IN6P_HOPLIMIT
);
2972 case IPV6_2292HOPOPTS
:
2973 optval
= OPTBIT(IN6P_HOPOPTS
);
2975 case IPV6_2292RTHDR
:
2976 optval
= OPTBIT(IN6P_RTHDR
);
2978 case IPV6_2292DSTOPTS
:
2979 optval
= OPTBIT(IN6P_DSTOPTS
|
2983 error
= sooptcopyout(sopt
, &optval
,
2991 case IPV6_RTHDRDSTOPTS
:
2995 case IPV6_USE_MIN_MTU
:
2996 case IPV6_PREFER_TEMPADDR
:
2997 error
= ip6_getpcbopt(in6p
->in6p_outputopts
,
3001 case IPV6_MULTICAST_IF
:
3002 case IPV6_MULTICAST_HOPS
:
3003 case IPV6_MULTICAST_LOOP
:
3005 error
= ip6_getmoptions(in6p
, sopt
);
3008 case IPV6_IPSEC_POLICY
: {
3009 error
= 0; /* This option is no longer supported */
3014 if (in6p
->inp_flags
& INP_BOUND_IF
) {
3015 optval
= in6p
->inp_boundifp
->if_index
;
3017 error
= sooptcopyout(sopt
, &optval
,
3021 case IPV6_NO_IFT_CELLULAR
:
3022 optval
= INP_NO_CELLULAR(in6p
) ? 1 : 0;
3023 error
= sooptcopyout(sopt
, &optval
,
3028 optval
= (in6p
->in6p_last_outifp
!= NULL
) ?
3029 in6p
->in6p_last_outifp
->if_index
: 0;
3030 error
= sooptcopyout(sopt
, &optval
,
3035 error
= ENOPROTOOPT
;
3040 } else if (level
== IPPROTO_UDP
) {
3041 error
= udp_ctloutput(so
, sopt
);
3049 ip6_raw_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
3051 int error
= 0, optval
;
3053 const int icmp6off
= offsetof(struct icmp6_hdr
, icmp6_cksum
);
3054 struct inpcb
*in6p
= sotoinpcb(so
);
3055 int level
, op
, optname
;
3057 level
= sopt
->sopt_level
;
3058 op
= sopt
->sopt_dir
;
3059 optname
= sopt
->sopt_name
;
3060 optlen
= sopt
->sopt_valsize
;
3062 if (level
!= IPPROTO_IPV6
) {
3069 * For ICMPv6 sockets, no modification allowed for checksum
3070 * offset, permit "no change" values to help existing apps.
3072 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
3073 * for an ICMPv6 socket will fail."
3074 * The current behavior does not meet RFC3542.
3078 if (optlen
!= sizeof(int)) {
3082 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
3087 if ((optval
% 2) != 0) {
3088 /* the API assumes even offset values */
3090 } else if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
3091 if (optval
!= icmp6off
) {
3095 in6p
->in6p_cksum
= optval
;
3100 if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
3103 optval
= in6p
->in6p_cksum
;
3106 error
= sooptcopyout(sopt
, &optval
, sizeof(optval
));
3116 error
= ENOPROTOOPT
;
3124 * Set up IP6 options in pcb for insertion in output packets or
3125 * specifying behavior of outgoing packets.
3128 ip6_pcbopts(struct ip6_pktopts
**pktopt
, struct mbuf
*m
, struct socket
*so
,
3129 struct sockopt
*sopt
)
3131 #pragma unused(sopt)
3132 struct ip6_pktopts
*opt
= *pktopt
;
3135 /* turn off any old options. */
3138 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
3139 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
3140 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
) {
3141 printf("%s: all specified options are cleared.\n",
3145 ip6_clearpktopts(opt
, -1);
3147 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
3154 if (m
== NULL
|| m
->m_len
== 0) {
3156 * Only turning off any previous options, regardless of
3157 * whether the opt is just created or given.
3160 FREE(opt
, M_IP6OPT
);
3165 /* set options specified by user. */
3166 if ((error
= ip6_setpktopts(m
, opt
, NULL
, SOCK_PROTO(so
))) != 0) {
3167 ip6_clearpktopts(opt
, -1); /* XXX: discard all options */
3168 FREE(opt
, M_IP6OPT
);
3176 * initialize ip6_pktopts. beware that there are non-zero default values in
3180 ip6_initpktopts(struct ip6_pktopts
*opt
)
3182 bzero(opt
, sizeof(*opt
));
3183 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
3184 opt
->ip6po_tclass
= -1; /* -1 means default traffic class */
3185 opt
->ip6po_minmtu
= IP6PO_MINMTU_MCASTONLY
;
3186 opt
->ip6po_prefer_tempaddr
= IP6PO_TEMPADDR_SYSTEM
;
3190 ip6_pcbopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
**pktopt
,
3193 struct ip6_pktopts
*opt
;
3197 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
3201 ip6_initpktopts(opt
);
3205 return ip6_setpktopt(optname
, buf
, len
, opt
, 1, 0, uproto
);
3209 ip6_getpcbopt(struct ip6_pktopts
*pktopt
, int optname
, struct sockopt
*sopt
)
3211 void *optdata
= NULL
;
3213 struct ip6_ext
*ip6e
;
3214 struct in6_pktinfo null_pktinfo
;
3215 int deftclass
= 0, on
;
3216 int defminmtu
= IP6PO_MINMTU_MCASTONLY
;
3217 int defpreftemp
= IP6PO_TEMPADDR_SYSTEM
;
3222 if (pktopt
&& pktopt
->ip6po_pktinfo
) {
3223 optdata
= (void *)pktopt
->ip6po_pktinfo
;
3225 /* XXX: we don't have to do this every time... */
3226 bzero(&null_pktinfo
, sizeof(null_pktinfo
));
3227 optdata
= (void *)&null_pktinfo
;
3229 optdatalen
= sizeof(struct in6_pktinfo
);
3233 if (pktopt
&& pktopt
->ip6po_tclass
>= 0) {
3234 optdata
= (void *)&pktopt
->ip6po_tclass
;
3236 optdata
= (void *)&deftclass
;
3238 optdatalen
= sizeof(int);
3242 if (pktopt
&& pktopt
->ip6po_hbh
) {
3243 optdata
= (void *)pktopt
->ip6po_hbh
;
3244 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_hbh
;
3245 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3250 if (pktopt
&& pktopt
->ip6po_rthdr
) {
3251 optdata
= (void *)pktopt
->ip6po_rthdr
;
3252 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_rthdr
;
3253 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3257 case IPV6_RTHDRDSTOPTS
:
3258 if (pktopt
&& pktopt
->ip6po_dest1
) {
3259 optdata
= (void *)pktopt
->ip6po_dest1
;
3260 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest1
;
3261 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3266 if (pktopt
&& pktopt
->ip6po_dest2
) {
3267 optdata
= (void *)pktopt
->ip6po_dest2
;
3268 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest2
;
3269 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3274 if (pktopt
&& pktopt
->ip6po_nexthop
) {
3275 optdata
= (void *)pktopt
->ip6po_nexthop
;
3276 optdatalen
= pktopt
->ip6po_nexthop
->sa_len
;
3280 case IPV6_USE_MIN_MTU
:
3282 optdata
= (void *)&pktopt
->ip6po_minmtu
;
3284 optdata
= (void *)&defminmtu
;
3286 optdatalen
= sizeof(int);
3290 if (pktopt
&& ((pktopt
->ip6po_flags
) & IP6PO_DONTFRAG
)) {
3295 optdata
= (void *)&on
;
3296 optdatalen
= sizeof(on
);
3299 case IPV6_PREFER_TEMPADDR
:
3301 optdata
= (void *)&pktopt
->ip6po_prefer_tempaddr
;
3303 optdata
= (void *)&defpreftemp
;
3305 optdatalen
= sizeof(int);
3308 default: /* should not happen */
3310 panic("ip6_getpcbopt: unexpected option\n");
3315 return sooptcopyout(sopt
, optdata
, optdatalen
);
3319 ip6_clearpktopts(struct ip6_pktopts
*pktopt
, int optname
)
3321 if (pktopt
== NULL
) {
3325 if (optname
== -1 || optname
== IPV6_PKTINFO
) {
3326 if (pktopt
->ip6po_pktinfo
) {
3327 FREE(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
3329 pktopt
->ip6po_pktinfo
= NULL
;
3331 if (optname
== -1 || optname
== IPV6_HOPLIMIT
) {
3332 pktopt
->ip6po_hlim
= -1;
3334 if (optname
== -1 || optname
== IPV6_TCLASS
) {
3335 pktopt
->ip6po_tclass
= -1;
3337 if (optname
== -1 || optname
== IPV6_NEXTHOP
) {
3338 ROUTE_RELEASE(&pktopt
->ip6po_nextroute
);
3339 if (pktopt
->ip6po_nexthop
) {
3340 FREE(pktopt
->ip6po_nexthop
, M_IP6OPT
);
3342 pktopt
->ip6po_nexthop
= NULL
;
3344 if (optname
== -1 || optname
== IPV6_HOPOPTS
) {
3345 if (pktopt
->ip6po_hbh
) {
3346 FREE(pktopt
->ip6po_hbh
, M_IP6OPT
);
3348 pktopt
->ip6po_hbh
= NULL
;
3350 if (optname
== -1 || optname
== IPV6_RTHDRDSTOPTS
) {
3351 if (pktopt
->ip6po_dest1
) {
3352 FREE(pktopt
->ip6po_dest1
, M_IP6OPT
);
3354 pktopt
->ip6po_dest1
= NULL
;
3356 if (optname
== -1 || optname
== IPV6_RTHDR
) {
3357 if (pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
) {
3358 FREE(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
3360 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
3361 ROUTE_RELEASE(&pktopt
->ip6po_route
);
3363 if (optname
== -1 || optname
== IPV6_DSTOPTS
) {
3364 if (pktopt
->ip6po_dest2
) {
3365 FREE(pktopt
->ip6po_dest2
, M_IP6OPT
);
3367 pktopt
->ip6po_dest2
= NULL
;
3371 #define PKTOPT_EXTHDRCPY(type) do { \
3374 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3; \
3375 dst->type = _MALLOC(hlen, M_IP6OPT, canwait); \
3376 if (dst->type == NULL && canwait == M_NOWAIT) \
3378 bcopy(src->type, dst->type, hlen); \
3383 copypktopts(struct ip6_pktopts
*dst
, struct ip6_pktopts
*src
, int canwait
)
3385 if (dst
== NULL
|| src
== NULL
) {
3386 printf("copypktopts: invalid argument\n");
3390 dst
->ip6po_hlim
= src
->ip6po_hlim
;
3391 dst
->ip6po_tclass
= src
->ip6po_tclass
;
3392 dst
->ip6po_flags
= src
->ip6po_flags
;
3393 if (src
->ip6po_pktinfo
) {
3394 dst
->ip6po_pktinfo
= _MALLOC(sizeof(*dst
->ip6po_pktinfo
),
3396 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
) {
3399 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
3401 if (src
->ip6po_nexthop
) {
3402 dst
->ip6po_nexthop
= _MALLOC(src
->ip6po_nexthop
->sa_len
,
3404 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
) {
3407 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
3408 src
->ip6po_nexthop
->sa_len
);
3410 PKTOPT_EXTHDRCPY(ip6po_hbh
);
3411 PKTOPT_EXTHDRCPY(ip6po_dest1
);
3412 PKTOPT_EXTHDRCPY(ip6po_dest2
);
3413 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
3417 ip6_clearpktopts(dst
, -1);
3420 #undef PKTOPT_EXTHDRCPY
3422 struct ip6_pktopts
*
3423 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
3426 struct ip6_pktopts
*dst
;
3428 dst
= _MALLOC(sizeof(*dst
), M_IP6OPT
, canwait
);
3432 ip6_initpktopts(dst
);
3434 if ((error
= copypktopts(dst
, src
, canwait
)) != 0) {
3435 FREE(dst
, M_IP6OPT
);
3443 ip6_freepcbopts(struct ip6_pktopts
*pktopt
)
3445 if (pktopt
== NULL
) {
3449 ip6_clearpktopts(pktopt
, -1);
3451 FREE(pktopt
, M_IP6OPT
);
3455 ip6_moptions_init(void)
3457 PE_parse_boot_argn("ifa_debug", &im6o_debug
, sizeof(im6o_debug
));
3459 vm_size_t im6o_size
= (im6o_debug
== 0) ? sizeof(struct ip6_moptions
) :
3460 sizeof(struct ip6_moptions_dbg
);
3462 im6o_zone
= zone_create(IM6O_ZONE_NAME
, im6o_size
, ZC_ZFREE_CLEARMEM
);
3466 im6o_addref(struct ip6_moptions
*im6o
, int locked
)
3471 IM6O_LOCK_ASSERT_HELD(im6o
);
3474 if (++im6o
->im6o_refcnt
== 0) {
3475 panic("%s: im6o %p wraparound refcnt\n", __func__
, im6o
);
3477 } else if (im6o
->im6o_trace
!= NULL
) {
3478 (*im6o
->im6o_trace
)(im6o
, TRUE
);
3487 im6o_remref(struct ip6_moptions
*im6o
)
3492 if (im6o
->im6o_refcnt
== 0) {
3493 panic("%s: im6o %p negative refcnt", __func__
, im6o
);
3495 } else if (im6o
->im6o_trace
!= NULL
) {
3496 (*im6o
->im6o_trace
)(im6o
, FALSE
);
3499 --im6o
->im6o_refcnt
;
3500 if (im6o
->im6o_refcnt
> 0) {
3505 for (i
= 0; i
< im6o
->im6o_num_memberships
; ++i
) {
3506 struct in6_mfilter
*imf
;
3508 imf
= im6o
->im6o_mfilters
? &im6o
->im6o_mfilters
[i
] : NULL
;
3513 (void) in6_mc_leave(im6o
->im6o_membership
[i
], imf
);
3519 IN6M_REMREF(im6o
->im6o_membership
[i
]);
3520 im6o
->im6o_membership
[i
] = NULL
;
3522 im6o
->im6o_num_memberships
= 0;
3523 if (im6o
->im6o_mfilters
!= NULL
) {
3524 FREE(im6o
->im6o_mfilters
, M_IN6MFILTER
);
3525 im6o
->im6o_mfilters
= NULL
;
3527 if (im6o
->im6o_membership
!= NULL
) {
3528 FREE(im6o
->im6o_membership
, M_IP6MOPTS
);
3529 im6o
->im6o_membership
= NULL
;
3533 lck_mtx_destroy(&im6o
->im6o_lock
, ifa_mtx_grp
);
3535 if (!(im6o
->im6o_debug
& IFD_ALLOC
)) {
3536 panic("%s: im6o %p cannot be freed", __func__
, im6o
);
3539 zfree(im6o_zone
, im6o
);
3543 im6o_trace(struct ip6_moptions
*im6o
, int refhold
)
3545 struct ip6_moptions_dbg
*im6o_dbg
= (struct ip6_moptions_dbg
*)im6o
;
3550 if (!(im6o
->im6o_debug
& IFD_DEBUG
)) {
3551 panic("%s: im6o %p has no debug structure", __func__
, im6o
);
3555 cnt
= &im6o_dbg
->im6o_refhold_cnt
;
3556 tr
= im6o_dbg
->im6o_refhold
;
3558 cnt
= &im6o_dbg
->im6o_refrele_cnt
;
3559 tr
= im6o_dbg
->im6o_refrele
;
3562 idx
= atomic_add_16_ov(cnt
, 1) % IM6O_TRACE_HIST_SIZE
;
3563 ctrace_record(&tr
[idx
]);
3566 struct ip6_moptions
*
3567 ip6_allocmoptions(zalloc_flags_t how
)
3569 struct ip6_moptions
*im6o
;
3571 im6o
= zalloc_flags(im6o_zone
, how
| Z_ZERO
);
3573 lck_mtx_init(&im6o
->im6o_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3574 im6o
->im6o_debug
|= IFD_ALLOC
;
3575 if (im6o_debug
!= 0) {
3576 im6o
->im6o_debug
|= IFD_DEBUG
;
3577 im6o
->im6o_trace
= im6o_trace
;
3586 * Set IPv6 outgoing packet options based on advanced API.
3589 ip6_setpktopts(struct mbuf
*control
, struct ip6_pktopts
*opt
,
3590 struct ip6_pktopts
*stickyopt
, int uproto
)
3592 struct cmsghdr
*cm
= NULL
;
3594 if (control
== NULL
|| opt
== NULL
) {
3598 ip6_initpktopts(opt
);
3603 * If stickyopt is provided, make a local copy of the options
3604 * for this particular packet, then override them by ancillary
3606 * XXX: copypktopts() does not copy the cached route to a next
3607 * hop (if any). This is not very good in terms of efficiency,
3608 * but we can allow this since this option should be rarely
3611 if ((error
= copypktopts(opt
, stickyopt
, M_NOWAIT
)) != 0) {
3617 * XXX: Currently, we assume all the optional information is stored
3620 if (control
->m_next
) {
3624 if (control
->m_len
< CMSG_LEN(0)) {
3628 for (cm
= M_FIRST_CMSGHDR(control
);
3629 is_cmsg_valid(control
, cm
);
3630 cm
= M_NXT_CMSGHDR(control
, cm
)) {
3633 if (cm
->cmsg_level
!= IPPROTO_IPV6
) {
3637 error
= ip6_setpktopt(cm
->cmsg_type
, CMSG_DATA(cm
),
3638 cm
->cmsg_len
- CMSG_LEN(0), opt
, 0, 1, uproto
);
3647 * Set a particular packet option, as a sticky option or an ancillary data
3648 * item. "len" can be 0 only when it's a sticky option.
3649 * We have 4 cases of combination of "sticky" and "cmsg":
3650 * "sticky=0, cmsg=0": impossible
3651 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3652 * "sticky=1, cmsg=0": RFC3542 socket option
3653 * "sticky=1, cmsg=1": RFC2292 socket option
3656 ip6_setpktopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
*opt
,
3657 int sticky
, int cmsg
, int uproto
)
3659 int minmtupolicy
, preftemp
;
3661 boolean_t capture_exthdrstat_out
= FALSE
;
3663 if (!sticky
&& !cmsg
) {
3665 printf("ip6_setpktopt: impossible case\n");
3671 * Caller must have ensured that the buffer is at least
3672 * aligned on 32-bit boundary.
3674 VERIFY(IS_P2ALIGNED(buf
, sizeof(u_int32_t
)));
3677 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3678 * not be specified in the context of RFC3542. Conversely,
3679 * RFC3542 types should not be specified in the context of RFC2292.
3683 case IPV6_2292PKTINFO
:
3684 case IPV6_2292HOPLIMIT
:
3685 case IPV6_2292NEXTHOP
:
3686 case IPV6_2292HOPOPTS
:
3687 case IPV6_2292DSTOPTS
:
3688 case IPV6_2292RTHDR
:
3689 case IPV6_2292PKTOPTIONS
:
3693 if (sticky
&& cmsg
) {
3700 case IPV6_RTHDRDSTOPTS
:
3702 case IPV6_USE_MIN_MTU
:
3705 case IPV6_PREFER_TEMPADDR
: /* XXX: not an RFC3542 option */
3711 case IPV6_2292PKTINFO
:
3712 case IPV6_PKTINFO
: {
3713 struct ifnet
*ifp
= NULL
;
3714 struct in6_pktinfo
*pktinfo
;
3716 if (len
!= sizeof(struct in6_pktinfo
)) {
3720 pktinfo
= (struct in6_pktinfo
*)(void *)buf
;
3723 * An application can clear any sticky IPV6_PKTINFO option by
3724 * doing a "regular" setsockopt with ipi6_addr being
3725 * in6addr_any and ipi6_ifindex being zero.
3726 * [RFC 3542, Section 6]
3728 if (optname
== IPV6_PKTINFO
&& opt
->ip6po_pktinfo
&&
3729 pktinfo
->ipi6_ifindex
== 0 &&
3730 IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3731 ip6_clearpktopts(opt
, optname
);
3735 if (uproto
== IPPROTO_TCP
&& optname
== IPV6_PKTINFO
&&
3736 sticky
&& !IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3740 /* validate the interface index if specified. */
3741 ifnet_head_lock_shared();
3743 if (pktinfo
->ipi6_ifindex
> if_index
) {
3748 if (pktinfo
->ipi6_ifindex
) {
3749 ifp
= ifindex2ifnet
[pktinfo
->ipi6_ifindex
];
3759 * We store the address anyway, and let in6_selectsrc()
3760 * validate the specified address. This is because ipi6_addr
3761 * may not have enough information about its scope zone, and
3762 * we may need additional information (such as outgoing
3763 * interface or the scope zone of a destination address) to
3764 * disambiguate the scope.
3765 * XXX: the delay of the validation may confuse the
3766 * application when it is used as a sticky option.
3768 if (opt
->ip6po_pktinfo
== NULL
) {
3769 opt
->ip6po_pktinfo
= _MALLOC(sizeof(*pktinfo
),
3770 M_IP6OPT
, M_NOWAIT
);
3771 if (opt
->ip6po_pktinfo
== NULL
) {
3775 bcopy(pktinfo
, opt
->ip6po_pktinfo
, sizeof(*pktinfo
));
3779 case IPV6_2292HOPLIMIT
:
3780 case IPV6_HOPLIMIT
: {
3784 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3785 * to simplify the ordering among hoplimit options.
3787 if (optname
== IPV6_HOPLIMIT
&& sticky
) {
3791 if (len
!= sizeof(int)) {
3794 hlimp
= (int *)(void *)buf
;
3795 if (*hlimp
< -1 || *hlimp
> IPV6_MAXHLIM
) {
3799 opt
->ip6po_hlim
= *hlimp
;
3806 if (len
!= sizeof(int)) {
3809 tclass
= *(int *)(void *)buf
;
3810 if (tclass
< -1 || tclass
> 255) {
3814 opt
->ip6po_tclass
= tclass
;
3818 case IPV6_2292NEXTHOP
:
3820 error
= suser(kauth_cred_get(), 0);
3825 if (len
== 0) { /* just remove the option */
3826 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3830 /* check if cmsg_len is large enough for sa_len */
3831 if (len
< sizeof(struct sockaddr
) || len
< *buf
) {
3835 switch (SA(buf
)->sa_family
) {
3837 struct sockaddr_in6
*sa6
= SIN6(buf
);
3839 if (sa6
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
3843 if (IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
) ||
3844 IN6_IS_ADDR_MULTICAST(&sa6
->sin6_addr
)) {
3847 if ((error
= sa6_embedscope(sa6
, ip6_use_defzone
))
3853 case AF_LINK
: /* should eventually be supported */
3855 return EAFNOSUPPORT
;
3858 /* turn off the previous option, then set the new option. */
3859 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3860 opt
->ip6po_nexthop
= _MALLOC(*buf
, M_IP6OPT
, M_NOWAIT
);
3861 if (opt
->ip6po_nexthop
== NULL
) {
3864 bcopy(buf
, opt
->ip6po_nexthop
, *buf
);
3867 case IPV6_2292HOPOPTS
:
3868 case IPV6_HOPOPTS
: {
3869 struct ip6_hbh
*hbh
;
3873 * XXX: We don't allow a non-privileged user to set ANY HbH
3874 * options, since per-option restriction has too much
3877 error
= suser(kauth_cred_get(), 0);
3883 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3884 break; /* just remove the option */
3887 /* message length validation */
3888 if (len
< sizeof(struct ip6_hbh
)) {
3891 hbh
= (struct ip6_hbh
*)(void *)buf
;
3892 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
3893 if (len
!= hbhlen
) {
3897 /* turn off the previous option, then set the new option. */
3898 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3899 opt
->ip6po_hbh
= _MALLOC(hbhlen
, M_IP6OPT
, M_NOWAIT
);
3900 if (opt
->ip6po_hbh
== NULL
) {
3903 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
3904 capture_exthdrstat_out
= TRUE
;
3908 case IPV6_2292DSTOPTS
:
3910 case IPV6_RTHDRDSTOPTS
: {
3911 struct ip6_dest
*dest
, **newdest
= NULL
;
3914 error
= suser(kauth_cred_get(), 0);
3920 ip6_clearpktopts(opt
, optname
);
3921 break; /* just remove the option */
3924 /* message length validation */
3925 if (len
< sizeof(struct ip6_dest
)) {
3928 dest
= (struct ip6_dest
*)(void *)buf
;
3929 destlen
= (dest
->ip6d_len
+ 1) << 3;
3930 if (len
!= destlen
) {
3935 * Determine the position that the destination options header
3936 * should be inserted; before or after the routing header.
3939 case IPV6_2292DSTOPTS
:
3941 * The old advacned API is ambiguous on this point.
3942 * Our approach is to determine the position based
3943 * according to the existence of a routing header.
3944 * Note, however, that this depends on the order of the
3945 * extension headers in the ancillary data; the 1st
3946 * part of the destination options header must appear
3947 * before the routing header in the ancillary data,
3949 * RFC3542 solved the ambiguity by introducing
3950 * separate ancillary data or option types.
3952 if (opt
->ip6po_rthdr
== NULL
) {
3953 newdest
= &opt
->ip6po_dest1
;
3955 newdest
= &opt
->ip6po_dest2
;
3958 case IPV6_RTHDRDSTOPTS
:
3959 newdest
= &opt
->ip6po_dest1
;
3962 newdest
= &opt
->ip6po_dest2
;
3966 /* turn off the previous option, then set the new option. */
3967 ip6_clearpktopts(opt
, optname
);
3968 *newdest
= _MALLOC(destlen
, M_IP6OPT
, M_NOWAIT
);
3969 if (*newdest
== NULL
) {
3972 bcopy(dest
, *newdest
, destlen
);
3973 capture_exthdrstat_out
= TRUE
;
3977 case IPV6_2292RTHDR
:
3979 struct ip6_rthdr
*rth
;
3983 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3984 break; /* just remove the option */
3987 /* message length validation */
3988 if (len
< sizeof(struct ip6_rthdr
)) {
3991 rth
= (struct ip6_rthdr
*)(void *)buf
;
3992 rthlen
= (rth
->ip6r_len
+ 1) << 3;
3993 if (len
!= rthlen
) {
3997 switch (rth
->ip6r_type
) {
3998 case IPV6_RTHDR_TYPE_0
:
3999 if (rth
->ip6r_len
== 0) { /* must contain one addr */
4002 if (rth
->ip6r_len
% 2) { /* length must be even */
4005 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
) {
4010 return EINVAL
; /* not supported */
4013 /* turn off the previous option */
4014 ip6_clearpktopts(opt
, IPV6_RTHDR
);
4015 opt
->ip6po_rthdr
= _MALLOC(rthlen
, M_IP6OPT
, M_NOWAIT
);
4016 if (opt
->ip6po_rthdr
== NULL
) {
4019 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
4020 capture_exthdrstat_out
= TRUE
;
4024 case IPV6_USE_MIN_MTU
:
4025 if (len
!= sizeof(int)) {
4028 minmtupolicy
= *(int *)(void *)buf
;
4029 if (minmtupolicy
!= IP6PO_MINMTU_MCASTONLY
&&
4030 minmtupolicy
!= IP6PO_MINMTU_DISABLE
&&
4031 minmtupolicy
!= IP6PO_MINMTU_ALL
) {
4034 opt
->ip6po_minmtu
= minmtupolicy
;
4038 if (len
!= sizeof(int)) {
4042 if (uproto
== IPPROTO_TCP
|| *(int *)(void *)buf
== 0) {
4044 * we ignore this option for TCP sockets.
4045 * (RFC3542 leaves this case unspecified.)
4047 opt
->ip6po_flags
&= ~IP6PO_DONTFRAG
;
4049 opt
->ip6po_flags
|= IP6PO_DONTFRAG
;
4053 case IPV6_PREFER_TEMPADDR
:
4054 if (len
!= sizeof(int)) {
4057 preftemp
= *(int *)(void *)buf
;
4058 if (preftemp
!= IP6PO_TEMPADDR_SYSTEM
&&
4059 preftemp
!= IP6PO_TEMPADDR_NOTPREFER
&&
4060 preftemp
!= IP6PO_TEMPADDR_PREFER
) {
4063 opt
->ip6po_prefer_tempaddr
= preftemp
;
4068 } /* end of switch */
4070 if (capture_exthdrstat_out
) {
4071 if (uproto
== IPPROTO_TCP
) {
4072 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_stream_exthdr_out
);
4073 } else if (uproto
== IPPROTO_UDP
) {
4074 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_dgram_exthdr_out
);
4082 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
4083 * packet to the input queue of a specified interface. Note that this
4084 * calls the output routine of the loopback "driver", but with an interface
4085 * pointer that might NOT be &loif -- easier than replicating that code here.
4088 ip6_mloopback(struct ifnet
*srcifp
, struct ifnet
*origifp
, struct mbuf
*m
,
4089 struct sockaddr_in6
*dst
, uint32_t optlen
, int32_t nxt0
)
4092 struct ip6_hdr
*ip6
;
4093 struct in6_addr src
;
4095 if (lo_ifp
== NULL
) {
4100 * Copy the packet header as it's needed for the checksum.
4101 * Make sure to deep-copy IPv6 header portion in case the data
4102 * is in an mbuf cluster, so that we can safely override the IPv6
4103 * header portion later.
4105 copym
= m_copym_mode(m
, 0, M_COPYALL
, M_DONTWAIT
, M_COPYM_COPY_HDR
);
4106 if (copym
!= NULL
&& ((copym
->m_flags
& M_EXT
) ||
4107 copym
->m_len
< sizeof(struct ip6_hdr
))) {
4108 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
4111 if (copym
== NULL
) {
4115 ip6
= mtod(copym
, struct ip6_hdr
*);
4118 * clear embedded scope identifiers if necessary.
4119 * in6_clearscope will touch the addresses only when necessary.
4121 in6_clearscope(&ip6
->ip6_src
);
4122 in6_clearscope(&ip6
->ip6_dst
);
4124 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
) {
4125 in6_delayed_cksum_offset(copym
, 0, optlen
, nxt0
);
4129 * Stuff the 'real' ifp into the pkthdr, to be used in matching
4130 * in ip6_input(); we need the loopback ifp/dl_tag passed as args
4131 * to make the loopback driver compliant with the data link
4134 copym
->m_pkthdr
.rcvif
= origifp
;
4137 * Also record the source interface (which owns the source address).
4138 * This is basically a stripped down version of ifa_foraddr6().
4140 if (srcifp
== NULL
) {
4141 struct in6_ifaddr
*ia
;
4143 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
4144 TAILQ_FOREACH(ia
, IN6ADDR_HASH(&src
), ia6_hash
) {
4145 IFA_LOCK_SPIN(&ia
->ia_ifa
);
4146 /* compare against src addr with embedded scope */
4147 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, &src
)) {
4148 srcifp
= ia
->ia_ifp
;
4149 IFA_UNLOCK(&ia
->ia_ifa
);
4152 IFA_UNLOCK(&ia
->ia_ifa
);
4154 lck_rw_done(&in6_ifaddr_rwlock
);
4156 if (srcifp
!= NULL
) {
4157 ip6_setsrcifaddr_info(copym
, srcifp
->if_index
, NULL
);
4159 ip6_setdstifaddr_info(copym
, origifp
->if_index
, NULL
);
4161 dlil_output(lo_ifp
, PF_INET6
, copym
, NULL
, SA(dst
), 0, NULL
);
4165 * Chop IPv6 header off from the payload.
4168 ip6_splithdr(struct mbuf
*m
, struct ip6_exthdrs
*exthdrs
)
4171 struct ip6_hdr
*ip6
;
4173 ip6
= mtod(m
, struct ip6_hdr
*);
4174 if (m
->m_len
> sizeof(*ip6
)) {
4175 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
4180 M_COPY_PKTHDR(mh
, m
);
4181 MH_ALIGN(mh
, sizeof(*ip6
));
4182 m
->m_flags
&= ~M_PKTHDR
;
4183 m
->m_len
-= sizeof(*ip6
);
4184 m
->m_data
+= sizeof(*ip6
);
4187 m
->m_len
= sizeof(*ip6
);
4188 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof(*ip6
));
4190 exthdrs
->ip6e_ip6
= m
;
4195 ip6_output_checksum(struct ifnet
*ifp
, uint32_t mtu
, struct mbuf
*m
,
4196 int nxt0
, uint32_t tlen
, uint32_t optlen
)
4198 uint32_t sw_csum
, hwcap
= ifp
->if_hwassist
;
4199 int tso
= TSO_IPV6_OK(ifp
, m
);
4202 /* do all in software; checksum offload is disabled */
4203 sw_csum
= CSUM_DELAY_IPV6_DATA
& m
->m_pkthdr
.csum_flags
;
4205 /* do in software what the hardware cannot */
4206 sw_csum
= m
->m_pkthdr
.csum_flags
&
4207 ~IF_HWASSIST_CSUM_FLAGS(hwcap
);
4211 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
4212 m
->m_pkthdr
.csum_flags
);
4213 } else if (!(sw_csum
& CSUM_DELAY_IPV6_DATA
) &&
4214 (hwcap
& CSUM_PARTIAL
)) {
4216 * Partial checksum offload, ere), if no extension headers,
4217 * and TCP only (no UDP support, as the hardware may not be
4218 * able to convert +0 to -0 (0xffff) per RFC1122 4.1.3.4.
4219 * unless the interface supports "invert zero" capability.)
4221 if (hwcksum_tx
&& !tso
&&
4222 ((m
->m_pkthdr
.csum_flags
& CSUM_TCPIPV6
) ||
4223 ((hwcap
& CSUM_ZERO_INVERT
) &&
4224 (m
->m_pkthdr
.csum_flags
& CSUM_ZERO_INVERT
))) &&
4226 uint16_t start
= sizeof(struct ip6_hdr
);
4228 m
->m_pkthdr
.csum_data
& 0xffff;
4229 m
->m_pkthdr
.csum_flags
|=
4230 (CSUM_DATA_VALID
| CSUM_PARTIAL
);
4231 m
->m_pkthdr
.csum_tx_stuff
= (ulpoff
+ start
);
4232 m
->m_pkthdr
.csum_tx_start
= start
;
4235 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
4236 m
->m_pkthdr
.csum_flags
);
4240 if (sw_csum
& CSUM_DELAY_IPV6_DATA
) {
4241 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
4242 sw_csum
&= ~CSUM_DELAY_IPV6_DATA
;
4247 * Drop off bits that aren't supported by hardware;
4248 * also make sure to preserve non-checksum related bits.
4250 m
->m_pkthdr
.csum_flags
=
4251 ((m
->m_pkthdr
.csum_flags
&
4252 (IF_HWASSIST_CSUM_FLAGS(hwcap
) | CSUM_DATA_VALID
)) |
4253 (m
->m_pkthdr
.csum_flags
& ~IF_HWASSIST_CSUM_MASK
));
4255 /* drop all bits; checksum offload is disabled */
4256 m
->m_pkthdr
.csum_flags
= 0;
4261 * Compute IPv6 extension header length.
4264 ip6_optlen(struct in6pcb
*in6p
)
4268 if (!in6p
->in6p_outputopts
) {
4274 (((struct ip6_ext *)(x)) ? \
4275 (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
4277 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
4278 if (in6p
->in6p_outputopts
->ip6po_rthdr
) {
4279 /* dest1 is valid with rthdr only */
4280 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
4282 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
4283 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);
4289 sysctl_reset_ip6_output_stats SYSCTL_HANDLER_ARGS
4291 #pragma unused(arg1, arg2)
4294 i
= ip6_output_measure
;
4295 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
4296 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
4300 if (i
< 0 || i
> 1) {
4304 if (ip6_output_measure
!= i
&& i
== 1) {
4305 net_perf_initialize(&net_perf
, ip6_output_measure_bins
);
4307 ip6_output_measure
= i
;
4313 sysctl_ip6_output_measure_bins SYSCTL_HANDLER_ARGS
4315 #pragma unused(arg1, arg2)
4319 i
= ip6_output_measure_bins
;
4320 error
= sysctl_handle_quad(oidp
, &i
, 0, req
);
4321 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
4325 if (!net_perf_validate_bins(i
)) {
4329 ip6_output_measure_bins
= i
;
4335 sysctl_ip6_output_getperf SYSCTL_HANDLER_ARGS
4337 #pragma unused(oidp, arg1, arg2)
4338 if (req
->oldptr
== USER_ADDR_NULL
) {
4339 req
->oldlen
= (size_t)sizeof(struct ipstat
);
4342 return SYSCTL_OUT(req
, &net_perf
, MIN(sizeof(net_perf
), req
->oldlen
));