2 * Copyright (c) 2000-2019 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 <security/mac.h>
153 #endif /* CONFIG_MACF_NET */
156 #include <netinet/ip_fw.h>
157 #include <netinet/ip_dummynet.h>
158 #endif /* DUMMYNET */
161 #include <net/pfvar.h>
164 static int sysctl_reset_ip6_output_stats SYSCTL_HANDLER_ARGS
;
165 static int sysctl_ip6_output_measure_bins SYSCTL_HANDLER_ARGS
;
166 static int sysctl_ip6_output_getperf SYSCTL_HANDLER_ARGS
;
167 static int ip6_copyexthdr(struct mbuf
**, caddr_t
, int);
168 static void ip6_out_cksum_stats(int, u_int32_t
);
169 static int ip6_insert_jumboopt(struct ip6_exthdrs
*, u_int32_t
);
170 static int ip6_insertfraghdr(struct mbuf
*, struct mbuf
*, int,
172 static int ip6_getpmtu(struct route_in6
*, struct route_in6
*,
173 struct ifnet
*, struct in6_addr
*, u_int32_t
*, boolean_t
*);
174 static int ip6_pcbopts(struct ip6_pktopts
**, struct mbuf
*, struct socket
*,
175 struct sockopt
*sopt
);
176 static int ip6_pcbopt(int, u_char
*, int, struct ip6_pktopts
**, int);
177 static int ip6_getpcbopt(struct ip6_pktopts
*, int, struct sockopt
*);
178 static int copypktopts(struct ip6_pktopts
*, struct ip6_pktopts
*, int);
179 static void im6o_trace(struct ip6_moptions
*, int);
180 static int ip6_setpktopt(int, u_char
*, int, struct ip6_pktopts
*, int,
182 static int ip6_splithdr(struct mbuf
*, struct ip6_exthdrs
*);
183 static void ip6_output_checksum(struct ifnet
*, uint32_t, struct mbuf
*,
184 int, uint32_t, uint32_t);
185 extern int udp_ctloutput(struct socket
*, struct sockopt
*);
186 static int ip6_fragment_packet(struct mbuf
**m
,
187 struct ip6_pktopts
*opt
, struct ip6_exthdrs
*exthdrsp
, struct ifnet
*ifp
,
188 uint32_t mtu
, boolean_t alwaysfrag
, uint32_t unfragpartlen
,
189 struct route_in6
*ro_pmtu
, int nxt0
, uint32_t optlen
);
191 SYSCTL_DECL(_net_inet6_ip6
);
193 static int ip6_output_measure
= 0;
194 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, output_perf
,
195 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
196 &ip6_output_measure
, 0, sysctl_reset_ip6_output_stats
, "I", "Do time measurement");
198 static uint64_t ip6_output_measure_bins
= 0;
199 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, output_perf_bins
,
200 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_output_measure_bins
, 0,
201 sysctl_ip6_output_measure_bins
, "I",
202 "bins for chaining performance data histogram");
204 static net_perf_t net_perf
;
205 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, output_perf_data
,
206 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
207 0, 0, sysctl_ip6_output_getperf
, "S,net_perf",
208 "IP6 output performance data (struct net_perf, net/net_perf.h)");
210 #define IM6O_TRACE_HIST_SIZE 32 /* size of trace history */
213 __private_extern__
unsigned int im6o_trace_hist_size
= IM6O_TRACE_HIST_SIZE
;
215 struct ip6_moptions_dbg
{
216 struct ip6_moptions im6o
; /* ip6_moptions */
217 u_int16_t im6o_refhold_cnt
; /* # of IM6O_ADDREF */
218 u_int16_t im6o_refrele_cnt
; /* # of IM6O_REMREF */
220 * Alloc and free callers.
225 * Circular lists of IM6O_ADDREF and IM6O_REMREF callers.
227 ctrace_t im6o_refhold
[IM6O_TRACE_HIST_SIZE
];
228 ctrace_t im6o_refrele
[IM6O_TRACE_HIST_SIZE
];
232 static unsigned int im6o_debug
= 1; /* debugging (enabled) */
234 static unsigned int im6o_debug
; /* debugging (disabled) */
237 static unsigned int im6o_size
; /* size of zone element */
238 static struct zone
*im6o_zone
; /* zone for ip6_moptions */
240 #define IM6O_ZONE_MAX 64 /* maximum elements in zone */
241 #define IM6O_ZONE_NAME "ip6_moptions" /* zone name */
244 * ip6_output() calls ip6_output_list() to do the work
247 ip6_output(struct mbuf
*m0
, struct ip6_pktopts
*opt
,
248 struct route_in6
*ro
, int flags
, struct ip6_moptions
*im6o
,
249 struct ifnet
**ifpp
, struct ip6_out_args
*ip6oa
)
251 return ip6_output_list(m0
, 0, opt
, ro
, flags
, im6o
, ifpp
, ip6oa
);
255 * IP6 output. Each packet in mbuf chain m contains a skeletal IP6
256 * header (with pri, len, nxt, hlim, src, dst).
257 * This function may modify ver and hlim only.
258 * The mbuf chain containing the packet will be freed.
259 * The mbuf opt, if present, will not be freed.
261 * If ro is non-NULL and has valid ro->ro_rt, route lookup would be
262 * skipped and ro->ro_rt would be used. Otherwise the result of route
263 * lookup is stored in ro->ro_rt.
265 * type of "mtu": rt_rmx.rmx_mtu is u_int32_t, ifnet.ifr_mtu is int, and
266 * nd_ifinfo.linkmtu is u_int32_t. so we use u_int32_t to hold largest one,
267 * which is rt_rmx.rmx_mtu.
270 ip6_output_list(struct mbuf
*m0
, int packetchain
, struct ip6_pktopts
*opt
,
271 struct route_in6
*ro
, int flags
, struct ip6_moptions
*im6o
,
272 struct ifnet
**ifpp
, struct ip6_out_args
*ip6oa
)
276 struct ifnet
*ifp
= NULL
, *origifp
= NULL
; /* refcnt'd */
277 struct ifnet
**ifpp_save
= ifpp
;
278 struct mbuf
*m
, *mprev
;
279 struct mbuf
*sendchain
= NULL
, *sendchain_last
= NULL
;
280 struct mbuf
*inputchain
= NULL
;
282 struct route_in6
*ro_pmtu
= NULL
;
283 struct rtentry
*rt
= NULL
;
284 struct sockaddr_in6
*dst
= NULL
, src_sa
, dst_sa
;
286 struct in6_ifaddr
*ia
= NULL
, *src_ia
= NULL
;
288 boolean_t alwaysfrag
= FALSE
;
289 u_int32_t optlen
= 0, plen
= 0, unfragpartlen
= 0;
290 struct ip6_rthdr
*rh
;
291 struct in6_addr finaldst
;
292 ipfilter_t inject_filter_ref
;
293 struct ipf_pktopts
*ippo
= NULL
;
294 struct flowadv
*adv
= NULL
;
296 uint32_t packets_processed
= 0;
297 struct timeval start_tv
;
300 struct ip6_out_args saved_ip6oa
;
301 struct sockaddr_in6 dst_buf
;
302 #endif /* DUMMYNET */
304 struct socket
*so
= NULL
;
305 struct secpolicy
*sp
= NULL
;
306 struct route_in6
*ipsec_saved_route
= NULL
;
307 boolean_t needipsectun
= FALSE
;
310 necp_kernel_policy_result necp_result
= 0;
311 necp_kernel_policy_result_parameter necp_result_parameter
;
312 necp_kernel_policy_id necp_matched_policy_id
= 0;
315 struct ipf_pktopts ipf_pktopts
;
316 struct ip6_exthdrs exthdrs
;
317 struct route_in6 ip6route
;
319 struct ipsec_output_state ipsec_state
;
322 struct route_in6 necp_route
;
325 struct route_in6 saved_route
;
326 struct route_in6 saved_ro_pmtu
;
327 struct ip_fw_args args
;
328 #endif /* DUMMYNET */
330 #define ipf_pktopts ip6obz.ipf_pktopts
331 #define exthdrs ip6obz.exthdrs
332 #define ip6route ip6obz.ip6route
333 #define ipsec_state ip6obz.ipsec_state
334 #define necp_route ip6obz.necp_route
335 #define saved_route ip6obz.saved_route
336 #define saved_ro_pmtu ip6obz.saved_ro_pmtu
337 #define args ip6obz.args
340 boolean_t select_srcif
: 1;
341 boolean_t hdrsplit
: 1;
342 boolean_t route_selected
: 1;
343 boolean_t dontfrag
: 1;
345 boolean_t needipsec
: 1;
346 boolean_t noipsec
: 1;
350 } ip6obf
= { .raw
= 0 };
352 if (ip6_output_measure
) {
353 net_perf_start_time(&net_perf
, &start_tv
);
356 VERIFY(m0
->m_flags
& M_PKTHDR
);
358 /* zero out {saved_route, saved_ro_pmtu, ip6route, exthdrs, args} */
359 bzero(&ip6obz
, sizeof(ip6obz
));
362 if (SLIST_EMPTY(&m0
->m_pkthdr
.tags
)) {
366 /* Grab info from mtags prepended to the chain */
367 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
,
368 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
369 struct dn_pkt_tag
*dn_tag
;
372 * ip6_output_list() cannot handle chains of packets reinjected
373 * by dummynet. The same restriction applies to
376 VERIFY(0 == packetchain
);
378 dn_tag
= (struct dn_pkt_tag
*)(tag
+ 1);
379 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
381 bcopy(&dn_tag
->dn_dst6
, &dst_buf
, sizeof(dst_buf
));
383 ifp
= dn_tag
->dn_ifp
;
385 ifnet_reference(ifp
);
387 flags
= dn_tag
->dn_flags
;
388 if (dn_tag
->dn_flags
& IPV6_OUTARGS
) {
389 saved_ip6oa
= dn_tag
->dn_ip6oa
;
390 ip6oa
= &saved_ip6oa
;
393 saved_route
= dn_tag
->dn_ro6
;
395 saved_ro_pmtu
= dn_tag
->dn_ro6_pmtu
;
396 ro_pmtu
= &saved_ro_pmtu
;
397 origifp
= dn_tag
->dn_origifp
;
398 if (origifp
!= NULL
) {
399 ifnet_reference(origifp
);
401 mtu
= dn_tag
->dn_mtu
;
402 alwaysfrag
= (dn_tag
->dn_alwaysfrag
!= 0);
403 unfragpartlen
= dn_tag
->dn_unfragpartlen
;
405 bcopy(&dn_tag
->dn_exthdrs
, &exthdrs
, sizeof(exthdrs
));
407 m_tag_delete(m0
, tag
);
411 #endif /* DUMMYNET */
416 if (ipsec_bypass
== 0) {
417 so
= ipsec_getsocket(m
);
419 (void) ipsec_setsocket(m
, NULL
);
421 /* If packet is bound to an interface, check bound policies */
422 if ((flags
& IPV6_OUTARGS
) &&
423 (ip6oa
->ip6oa_flags
& IP6OAF_BOUND_IF
) &&
424 ip6oa
->ip6oa_boundif
!= IFSCOPE_NONE
) {
425 /* ip6obf.noipsec is a bitfield, use temp integer */
428 if (ipsec6_getpolicybyinterface(m
, IPSEC_DIR_OUTBOUND
,
429 flags
, ip6oa
, &noipsec
, &sp
) != 0) {
433 ip6obf
.noipsec
= (noipsec
!= 0);
440 if (flags
& IPV6_OUTARGS
) {
442 * In the forwarding case, only the ifscope value is used,
443 * as source interface selection doesn't take place.
445 if ((ip6obf
.select_srcif
= (!(flags
& (IPV6_FORWARDING
|
446 IPV6_UNSPECSRC
| IPV6_FLAG_NOSRCIFSEL
)) &&
447 (ip6oa
->ip6oa_flags
& IP6OAF_SELECT_SRCIF
)))) {
448 ipf_pktopts
.ippo_flags
|= IPPOF_SELECT_SRCIF
;
451 if ((ip6oa
->ip6oa_flags
& IP6OAF_BOUND_IF
) &&
452 ip6oa
->ip6oa_boundif
!= IFSCOPE_NONE
) {
453 ipf_pktopts
.ippo_flags
|= (IPPOF_BOUND_IF
|
454 (ip6oa
->ip6oa_boundif
<< IPPOF_SHIFT_IFSCOPE
));
457 if (ip6oa
->ip6oa_flags
& IP6OAF_BOUND_SRCADDR
) {
458 ipf_pktopts
.ippo_flags
|= IPPOF_BOUND_SRCADDR
;
461 ip6obf
.select_srcif
= FALSE
;
462 if (flags
& IPV6_OUTARGS
) {
463 ip6oa
->ip6oa_boundif
= IFSCOPE_NONE
;
464 ip6oa
->ip6oa_flags
&= ~(IP6OAF_SELECT_SRCIF
|
465 IP6OAF_BOUND_IF
| IP6OAF_BOUND_SRCADDR
);
469 if (flags
& IPV6_OUTARGS
) {
470 if (ip6oa
->ip6oa_flags
& IP6OAF_NO_CELLULAR
) {
471 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFT_CELLULAR
;
473 if (ip6oa
->ip6oa_flags
& IP6OAF_NO_EXPENSIVE
) {
474 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFF_EXPENSIVE
;
476 adv
= &ip6oa
->ip6oa_flowadv
;
477 adv
->code
= FADV_SUCCESS
;
478 ip6oa
->ip6oa_retflags
= 0;
482 * Clear out ifpp to be filled in after determining route. ifpp_save is
483 * used to keep old value to release reference properly and dtrace
484 * ipsec tunnel traffic properly.
486 if (ifpp
!= NULL
&& *ifpp
!= NULL
) {
491 if (args
.fwa_pf_rule
) {
492 ip6
= mtod(m
, struct ip6_hdr
*);
493 VERIFY(ro
!= NULL
); /* ro == saved_route */
496 #endif /* DUMMYNET */
500 * Since all packets are assumed to come from same socket, necp lookup
501 * only needs to happen once per function entry.
503 necp_matched_policy_id
= necp_ip6_output_find_policy_match(m
, flags
,
504 (flags
& IPV6_OUTARGS
) ? ip6oa
: NULL
, &necp_result
,
505 &necp_result_parameter
);
509 * If a chain was passed in, prepare for ther first iteration. For all
510 * other iterations, this work will be done at evaluateloop: label.
514 * Remove m from the chain during processing to avoid
515 * accidental frees on entire list.
517 inputchain
= m
->m_nextpkt
;
523 m
->m_pkthdr
.pkt_flags
&= ~(PKTF_LOOP
| PKTF_IFAINFO
);
524 ip6
= mtod(m
, struct ip6_hdr
*);
526 finaldst
= ip6
->ip6_dst
;
527 ip6obf
.hdrsplit
= FALSE
;
530 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
531 inject_filter_ref
= ipf_get_inject_filter(m
);
533 inject_filter_ref
= NULL
;
536 #define MAKE_EXTHDR(hp, mp) do { \
538 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
539 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
540 ((eh)->ip6e_len + 1) << 3); \
547 /* Hop-by-Hop options header */
548 MAKE_EXTHDR(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
);
549 /* Destination options header(1st part) */
550 if (opt
->ip6po_rthdr
) {
552 * Destination options header(1st part)
553 * This only makes sense with a routing header.
554 * See Section 9.2 of RFC 3542.
555 * Disabling this part just for MIP6 convenience is
556 * a bad idea. We need to think carefully about a
557 * way to make the advanced API coexist with MIP6
558 * options, which might automatically be inserted in
561 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
564 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
565 /* Destination options header(2nd part) */
566 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
572 if (necp_matched_policy_id
) {
573 necp_mark_packet_from_ip(m
, necp_matched_policy_id
);
575 switch (necp_result
) {
576 case NECP_KERNEL_POLICY_RESULT_PASS
:
578 case NECP_KERNEL_POLICY_RESULT_DROP
:
579 case NECP_KERNEL_POLICY_RESULT_SOCKET_DIVERT
:
581 * Flow divert packets should be blocked at the IP
584 error
= EHOSTUNREACH
;
585 ip6stat
.ip6s_necp_policy_drop
++;
587 case NECP_KERNEL_POLICY_RESULT_IP_TUNNEL
: {
589 * Verify that the packet is being routed to the tunnel
591 struct ifnet
*policy_ifp
=
592 necp_get_ifnet_from_result_parameter(
593 &necp_result_parameter
);
595 if (policy_ifp
== ifp
) {
598 if (necp_packet_can_rebind_to_ifnet(m
,
599 policy_ifp
, (struct route
*)&necp_route
,
602 * Set scoped index to the tunnel
603 * interface, since it is compatible
604 * with the packet. This will only work
605 * for callers who pass IPV6_OUTARGS,
606 * but that covers all of the clients
607 * we care about today.
609 if (flags
& IPV6_OUTARGS
) {
610 ip6oa
->ip6oa_boundif
=
611 policy_ifp
->if_index
;
612 ip6oa
->ip6oa_flags
|=
616 && opt
->ip6po_pktinfo
!= NULL
) {
619 policy_ifp
->if_index
;
625 ip6stat
.ip6s_necp_policy_drop
++;
637 if (ipsec_bypass
!= 0 || ip6obf
.noipsec
) {
642 /* get a security policy for this packet */
644 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
,
647 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
,
651 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
659 switch (sp
->policy
) {
660 case IPSEC_POLICY_DISCARD
:
661 case IPSEC_POLICY_GENERATE
:
663 * This packet is just discarded.
665 IPSEC_STAT_INCREMENT(ipsec6stat
.out_polvio
);
668 case IPSEC_POLICY_BYPASS
:
669 case IPSEC_POLICY_NONE
:
670 /* no need to do IPsec. */
671 ip6obf
.needipsec
= FALSE
;
674 case IPSEC_POLICY_IPSEC
:
675 if (sp
->req
== NULL
) {
676 /* acquire a policy */
677 error
= key_spdacquire(sp
);
683 ip6obf
.needipsec
= TRUE
;
687 case IPSEC_POLICY_ENTRUST
:
689 printf("%s: Invalid policy found: %d\n", __func__
, sp
->policy
);
696 * Calculate the total length of the extension header chain.
697 * Keep the length of the unfragmentable part for fragmentation.
700 if (exthdrs
.ip6e_hbh
!= NULL
) {
701 optlen
+= exthdrs
.ip6e_hbh
->m_len
;
703 if (exthdrs
.ip6e_dest1
!= NULL
) {
704 optlen
+= exthdrs
.ip6e_dest1
->m_len
;
706 if (exthdrs
.ip6e_rthdr
!= NULL
) {
707 optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
709 unfragpartlen
= optlen
+ sizeof(struct ip6_hdr
);
711 /* NOTE: we don't add AH/ESP length here. do that later. */
712 if (exthdrs
.ip6e_dest2
!= NULL
) {
713 optlen
+= exthdrs
.ip6e_dest2
->m_len
;
717 * If we need IPsec, or there is at least one extension header,
718 * separate IP6 header from the payload.
724 optlen
) && !ip6obf
.hdrsplit
) {
725 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
729 m
= exthdrs
.ip6e_ip6
;
730 ip6obf
.hdrsplit
= TRUE
;
734 ip6
= mtod(m
, struct ip6_hdr
*);
736 /* adjust mbuf packet header length */
737 m
->m_pkthdr
.len
+= optlen
;
738 plen
= m
->m_pkthdr
.len
- sizeof(*ip6
);
740 /* If this is a jumbo payload, insert a jumbo payload option. */
741 if (plen
> IPV6_MAXPACKET
) {
742 if (!ip6obf
.hdrsplit
) {
743 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
747 m
= exthdrs
.ip6e_ip6
;
748 ip6obf
.hdrsplit
= TRUE
;
751 ip6
= mtod(m
, struct ip6_hdr
*);
752 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0) {
757 ip6
->ip6_plen
= htons(plen
);
760 * Concatenate headers and fill in next header fields.
761 * Here we have, on "m"
763 * and we insert headers accordingly. Finally, we should be getting:
764 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
766 * during the header composing process, "m" points to IPv6 header.
767 * "mprev" points to an extension header prior to esp.
769 nexthdrp
= &ip6
->ip6_nxt
;
773 * we treat dest2 specially. this makes IPsec processing
774 * much easier. the goal here is to make mprev point the
775 * mbuf prior to dest2.
777 * result: IPv6 dest2 payload
778 * m and mprev will point to IPv6 header.
780 if (exthdrs
.ip6e_dest2
!= NULL
) {
781 if (!ip6obf
.hdrsplit
) {
782 panic("assumption failed: hdr not split");
785 exthdrs
.ip6e_dest2
->m_next
= m
->m_next
;
786 m
->m_next
= exthdrs
.ip6e_dest2
;
787 *mtod(exthdrs
.ip6e_dest2
, u_char
*) = ip6
->ip6_nxt
;
788 ip6
->ip6_nxt
= IPPROTO_DSTOPTS
;
791 #define MAKE_CHAIN(m, mp, p, i) do { \
793 if (!ip6obf.hdrsplit) { \
794 panic("assumption failed: hdr not split"); \
797 *mtod((m), u_char *) = *(p); \
799 p = mtod((m), u_char *); \
800 (m)->m_next = (mp)->m_next; \
801 (mp)->m_next = (m); \
806 * result: IPv6 hbh dest1 rthdr dest2 payload
807 * m will point to IPv6 header. mprev will point to the
808 * extension header prior to dest2 (rthdr in the above case).
810 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
, nexthdrp
, IPPROTO_HOPOPTS
);
811 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
, nexthdrp
, IPPROTO_DSTOPTS
);
812 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
, nexthdrp
, IPPROTO_ROUTING
);
814 /* It is no longer safe to free the pointers in exthdrs. */
815 exthdrs
.merged
= TRUE
;
820 if (ip6obf
.needipsec
&& (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
)) {
821 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
825 if (!TAILQ_EMPTY(&ipv6_filters
) &&
826 !((flags
& IPV6_OUTARGS
) &&
827 (ip6oa
->ip6oa_flags
& IP6OAF_INTCOPROC_ALLOWED
))) {
828 struct ipfilter
*filter
;
829 int seen
= (inject_filter_ref
== NULL
);
832 if (im6o
!= NULL
&& IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
833 ippo
->ippo_flags
|= IPPOF_MCAST_OPTS
;
835 ippo
->ippo_mcast_ifnet
= im6o
->im6o_multicast_ifp
;
836 ippo
->ippo_mcast_ttl
= im6o
->im6o_multicast_hlim
;
837 ippo
->ippo_mcast_loop
= im6o
->im6o_multicast_loop
;
841 /* Hack: embed the scope_id in the destination */
842 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
843 (ip6
->ip6_dst
.s6_addr16
[1] == 0) && (ro
!= NULL
)) {
845 ip6
->ip6_dst
.s6_addr16
[1] =
846 htons(ro
->ro_dst
.sin6_scope_id
);
850 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
852 * Don't process packet twice if we've already seen it.
855 if ((struct ipfilter
*)inject_filter_ref
==
859 } else if (filter
->ipf_filter
.ipf_output
!= NULL
) {
862 result
= filter
->ipf_filter
.ipf_output(
863 filter
->ipf_filter
.cookie
,
865 if (result
== EJUSTRETURN
) {
878 ip6
= mtod(m
, struct ip6_hdr
*);
879 /* Hack: cleanup embedded scope_id if we put it there */
881 ip6
->ip6_dst
.s6_addr16
[1] = 0;
886 if (ip6obf
.needipsec
) {
890 * pointers after IPsec headers are not valid any more.
891 * other pointers need a great care too.
892 * (IPsec routines should not mangle mbufs prior to AH/ESP)
894 exthdrs
.ip6e_dest2
= NULL
;
896 if (exthdrs
.ip6e_rthdr
!= NULL
) {
897 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
898 segleft_org
= rh
->ip6r_segleft
;
899 rh
->ip6r_segleft
= 0;
906 error
= ipsec6_output_trans(&ipsec_state
, nexthdrp
, mprev
,
907 sp
, flags
, &needipsectun
);
910 /* mbuf is already reclaimed in ipsec6_output_trans. */
920 printf("ip6_output (ipsec): error code %d\n",
924 /* don't show these error codes to the user */
930 if (exthdrs
.ip6e_rthdr
!= NULL
) {
931 /* ah6_output doesn't modify mbuf chain */
932 rh
->ip6r_segleft
= segleft_org
;
937 /* If there is a routing header, discard the packet. */
938 if (exthdrs
.ip6e_rthdr
!= NULL
) {
943 /* Source address validation */
944 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
945 !(flags
& IPV6_UNSPECSRC
)) {
947 ip6stat
.ip6s_badscope
++;
950 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
952 ip6stat
.ip6s_badscope
++;
956 ip6stat
.ip6s_localout
++;
963 bzero((caddr_t
)ro
, sizeof(*ro
));
966 if (opt
!= NULL
&& opt
->ip6po_rthdr
) {
967 ro
= &opt
->ip6po_route
;
969 dst
= SIN6(&ro
->ro_dst
);
971 if (ro
->ro_rt
!= NULL
) {
972 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
975 * if specified, try to fill in the traffic class field.
976 * do not override if a non-zero value is already set.
977 * we check the diffserv field and the ecn field separately.
979 if (opt
!= NULL
&& opt
->ip6po_tclass
>= 0) {
982 if ((ip6
->ip6_flow
& htonl(0xfc << 20)) == 0) {
985 if ((ip6
->ip6_flow
& htonl(0x03 << 20)) == 0) {
990 htonl((opt
->ip6po_tclass
& mask
) << 20);
994 /* fill in or override the hop limit field, if necessary. */
995 if (opt
&& opt
->ip6po_hlim
!= -1) {
996 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
997 } else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1000 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
1003 ip6
->ip6_hlim
= ip6_defmcasthlim
;
1008 * If there is a cached route, check that it is to the same
1009 * destination and is still up. If not, free it and try again.
1010 * Test rt_flags without holding rt_lock for performance reasons;
1011 * if the route is down it will hopefully be caught by the layer
1012 * below (since it uses this route as a hint) or during the
1015 if (ROUTE_UNUSABLE(ro
) || dst
->sin6_family
!= AF_INET6
||
1016 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
)) {
1020 if (ro
->ro_rt
== NULL
) {
1021 bzero(dst
, sizeof(*dst
));
1022 dst
->sin6_family
= AF_INET6
;
1023 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
1024 dst
->sin6_addr
= ip6
->ip6_dst
;
1027 if (ip6obf
.needipsec
&& needipsectun
) {
1029 struct ifnet
*trace_ifp
= (ifpp_save
!= NULL
) ? (*ifpp_save
) : NULL
;
1030 #endif /* CONFIG_DTRACE */
1032 * All the extension headers will become inaccessible
1033 * (since they can be encrypted).
1034 * Don't panic, we need no more updates to extension headers
1035 * on inner IPv6 packet (since they are now encapsulated).
1037 * IPv6 [ESP|AH] IPv6 [extension headers] payload
1039 bzero(&exthdrs
, sizeof(exthdrs
));
1040 exthdrs
.ip6e_ip6
= m
;
1043 route_copyout((struct route
*)&ipsec_state
.ro
, (struct route
*)ro
,
1044 sizeof(struct route_in6
));
1045 ipsec_state
.dst
= SA(dst
);
1047 /* So that we can see packets inside the tunnel */
1048 DTRACE_IP6(send
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1049 struct ip6_hdr
*, ip6
, struct ifnet
*, trace_ifp
,
1050 struct ip
*, NULL
, struct ip6_hdr
*, ip6
);
1052 error
= ipsec6_output_tunnel(&ipsec_state
, sp
, flags
);
1053 /* tunneled in IPv4? packet is gone */
1054 if (ipsec_state
.tunneled
== 4) {
1059 ipsec_saved_route
= ro
;
1060 ro
= (struct route_in6
*)&ipsec_state
.ro
;
1061 dst
= SIN6(ipsec_state
.dst
);
1063 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
1073 printf("ip6_output (ipsec): error code %d\n",
1077 /* don't show these error codes to the user */
1084 * The packet has been encapsulated so the ifscope
1085 * is no longer valid since it does not apply to the
1086 * outer address: ignore the ifscope.
1088 if (flags
& IPV6_OUTARGS
) {
1089 ip6oa
->ip6oa_boundif
= IFSCOPE_NONE
;
1090 ip6oa
->ip6oa_flags
&= ~IP6OAF_BOUND_IF
;
1092 if (opt
!= NULL
&& opt
->ip6po_pktinfo
!= NULL
) {
1093 if (opt
->ip6po_pktinfo
->ipi6_ifindex
!= IFSCOPE_NONE
) {
1094 opt
->ip6po_pktinfo
->ipi6_ifindex
= IFSCOPE_NONE
;
1097 exthdrs
.ip6e_ip6
= m
;
1102 * ifp should only be filled in for dummy net packets which will jump
1103 * to check_with_pf label.
1106 VERIFY(ip6obf
.route_selected
);
1109 /* adjust pointer */
1110 ip6
= mtod(m
, struct ip6_hdr
*);
1112 if (ip6obf
.select_srcif
) {
1113 bzero(&src_sa
, sizeof(src_sa
));
1114 src_sa
.sin6_family
= AF_INET6
;
1115 src_sa
.sin6_len
= sizeof(src_sa
);
1116 src_sa
.sin6_addr
= ip6
->ip6_src
;
1118 bzero(&dst_sa
, sizeof(dst_sa
));
1119 dst_sa
.sin6_family
= AF_INET6
;
1120 dst_sa
.sin6_len
= sizeof(dst_sa
);
1121 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
1124 * Only call in6_selectroute() on first iteration to avoid taking
1125 * multiple references on ifp and rt.
1127 * in6_selectroute() might return an ifp with its reference held
1128 * even in the error case, so make sure to release its reference.
1129 * ip6oa may be NULL if IPV6_OUTARGS isn't set.
1131 if (!ip6obf
.route_selected
) {
1132 error
= in6_selectroute( ip6obf
.select_srcif
? &src_sa
: NULL
,
1133 &dst_sa
, opt
, im6o
, &src_ia
, ro
, &ifp
, &rt
, 0, ip6oa
);
1138 ip6stat
.ip6s_noroute
++;
1142 break; /* XXX statistics? */
1145 in6_ifstat_inc(ifp
, ifs6_out_discard
);
1147 /* ifp (if non-NULL) will be released at the end */
1150 ip6obf
.route_selected
= TRUE
;
1154 * If in6_selectroute() does not return a route entry,
1155 * dst may not have been updated.
1157 *dst
= dst_sa
; /* XXX */
1161 /* Catch-all to check if the interface is allowed */
1162 if (!necp_packet_is_allowed_over_interface(m
, ifp
)) {
1163 error
= EHOSTUNREACH
;
1164 ip6stat
.ip6s_necp_policy_drop
++;
1170 * then rt (for unicast) and ifp must be non-NULL valid values.
1172 if (!(flags
& IPV6_FORWARDING
)) {
1173 in6_ifstat_inc_na(ifp
, ifs6_out_request
);
1178 ia
= (struct in6_ifaddr
*)(rt
->rt_ifa
);
1180 IFA_ADDREF(&ia
->ia_ifa
);
1188 * The outgoing interface must be in the zone of source and
1189 * destination addresses (except local/loopback). We should
1190 * use ia_ifp to support the case of sending packets to an
1191 * address of our own.
1193 if (ia
!= NULL
&& ia
->ia_ifp
) {
1194 ifnet_reference(ia
->ia_ifp
); /* for origifp */
1195 if (origifp
!= NULL
) {
1196 ifnet_release(origifp
);
1198 origifp
= ia
->ia_ifp
;
1201 ifnet_reference(ifp
); /* for origifp */
1203 if (origifp
!= NULL
) {
1204 ifnet_release(origifp
);
1209 /* skip scope enforcements for local/loopback route */
1210 if (rt
== NULL
|| !(rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
1211 struct in6_addr src0
, dst0
;
1214 src0
= ip6
->ip6_src
;
1215 if (in6_setscope(&src0
, origifp
, &zone
)) {
1218 bzero(&src_sa
, sizeof(src_sa
));
1219 src_sa
.sin6_family
= AF_INET6
;
1220 src_sa
.sin6_len
= sizeof(src_sa
);
1221 src_sa
.sin6_addr
= ip6
->ip6_src
;
1222 if ((sa6_recoverscope(&src_sa
, TRUE
) ||
1223 zone
!= src_sa
.sin6_scope_id
)) {
1227 dst0
= ip6
->ip6_dst
;
1228 if ((in6_setscope(&dst0
, origifp
, &zone
))) {
1231 /* re-initialize to be sure */
1232 bzero(&dst_sa
, sizeof(dst_sa
));
1233 dst_sa
.sin6_family
= AF_INET6
;
1234 dst_sa
.sin6_len
= sizeof(dst_sa
);
1235 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
1236 if ((sa6_recoverscope(&dst_sa
, TRUE
) ||
1237 zone
!= dst_sa
.sin6_scope_id
)) {
1241 /* scope check is done. */
1245 ip6stat
.ip6s_badscope
++;
1246 in6_ifstat_inc(origifp
, ifs6_out_discard
);
1248 error
= EHOSTUNREACH
; /* XXX */
1254 if (rt
!= NULL
&& !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1255 if (opt
!= NULL
&& opt
->ip6po_nextroute
.ro_rt
) {
1257 * The nexthop is explicitly specified by the
1258 * application. We assume the next hop is an IPv6
1261 dst
= SIN6(opt
->ip6po_nexthop
);
1262 } else if ((rt
->rt_flags
& RTF_GATEWAY
)) {
1263 dst
= SIN6(rt
->rt_gateway
);
1266 * For packets destined to local/loopback, record the
1267 * source the source interface (which owns the source
1268 * address), as well as the output interface. This is
1269 * needed to reconstruct the embedded zone for the
1270 * link-local address case in ip6_input().
1272 if (ia
!= NULL
&& (ifp
->if_flags
& IFF_LOOPBACK
)) {
1275 if (src_ia
!= NULL
) {
1276 srcidx
= src_ia
->ia_ifp
->if_index
;
1277 } else if (ro
->ro_srcia
!= NULL
) {
1278 srcidx
= ro
->ro_srcia
->ifa_ifp
->if_index
;
1283 ip6_setsrcifaddr_info(m
, srcidx
, NULL
);
1284 ip6_setdstifaddr_info(m
, 0, ia
);
1288 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1289 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
1291 struct in6_multi
*in6m
;
1293 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
1294 in6_ifstat_inc_na(ifp
, ifs6_out_mcast
);
1297 * Confirm that the outgoing interface supports multicast.
1299 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
1300 ip6stat
.ip6s_noroute
++;
1301 in6_ifstat_inc(ifp
, ifs6_out_discard
);
1302 error
= ENETUNREACH
;
1305 in6_multihead_lock_shared();
1306 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, in6m
);
1307 in6_multihead_lock_done();
1312 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
1317 * If we belong to the destination multicast group
1318 * on the outgoing interface, and the caller did not
1319 * forbid loopback, loop back a copy.
1321 ip6_mloopback(NULL
, ifp
, m
, dst
, optlen
, nxt0
);
1322 } else if (im6o
!= NULL
) {
1329 * Multicasts with a hoplimit of zero may be looped back,
1330 * above, but must not be transmitted on a network.
1331 * Also, multicasts addressed to the loopback interface
1332 * are not sent -- the above call to ip6_mloopback() will
1333 * loop back a copy if this host actually belongs to the
1334 * destination group on the loopback interface.
1336 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
) ||
1337 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
)) {
1338 /* remove m from the packetchain and continue looping */
1348 * Fill the outgoing inteface to tell the upper layer
1349 * to increment per-interface statistics.
1351 if (ifpp
!= NULL
&& *ifpp
== NULL
) {
1352 ifnet_reference(ifp
); /* for caller */
1356 /* Determine path MTU. */
1357 if ((error
= ip6_getpmtu(ro_pmtu
, ro
, ifp
, &finaldst
, &mtu
,
1358 &alwaysfrag
)) != 0) {
1363 * The caller of this function may specify to use the minimum MTU
1365 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
1366 * setting. The logic is a bit complicated; by default, unicast
1367 * packets will follow path MTU while multicast packets will be sent at
1368 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
1369 * including unicast ones will be sent at the minimum MTU. Multicast
1370 * packets will always be sent at the minimum MTU unless
1371 * IP6PO_MINMTU_DISABLE is explicitly specified.
1372 * See RFC 3542 for more details.
1374 if (mtu
> IPV6_MMTU
) {
1375 if ((flags
& IPV6_MINMTU
)) {
1377 } else if (opt
&& opt
->ip6po_minmtu
== IP6PO_MINMTU_ALL
) {
1379 } else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) &&
1381 opt
->ip6po_minmtu
!= IP6PO_MINMTU_DISABLE
)) {
1387 * clear embedded scope identifiers if necessary.
1388 * in6_clearscope will touch the addresses only when necessary.
1390 in6_clearscope(&ip6
->ip6_src
);
1391 in6_clearscope(&ip6
->ip6_dst
);
1393 * If the outgoing packet contains a hop-by-hop options header,
1394 * it must be examined and processed even by the source node.
1395 * (RFC 2460, section 4.)
1397 if (exthdrs
.ip6e_hbh
!= NULL
) {
1398 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
1399 u_int32_t dummy
; /* XXX unused */
1400 uint32_t oplen
= 0; /* for ip6_process_hopopts() */
1402 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
) {
1403 panic("ip6e_hbh is not continuous");
1407 * XXX: If we have to send an ICMPv6 error to the sender,
1408 * we need the M_LOOP flag since icmp6_error() expects
1409 * the IPv6 and the hop-by-hop options header are
1410 * continuous unless the flag is set.
1412 m
->m_flags
|= M_LOOP
;
1413 m
->m_pkthdr
.rcvif
= ifp
;
1414 if (ip6_process_hopopts(m
, (u_int8_t
*)(hbh
+ 1),
1415 ((hbh
->ip6h_len
+ 1) << 3) - sizeof(struct ip6_hbh
),
1416 &dummy
, &oplen
) < 0) {
1418 * m was already freed at this point. Set to NULL so it
1419 * is not re-freed at end of ip6_output_list.
1422 error
= EINVAL
; /* better error? */
1425 m
->m_flags
&= ~M_LOOP
; /* XXX */
1426 m
->m_pkthdr
.rcvif
= NULL
;
1431 #endif /* DUMMYNET */
1433 if (PF_IS_ENABLED
) {
1437 * TODO: Need to save opt->ip6po_flags for reinjection
1442 args
.fwa_oflags
= flags
;
1443 if (flags
& IPV6_OUTARGS
) {
1444 args
.fwa_ip6oa
= ip6oa
;
1447 args
.fwa_dst6
= dst
;
1448 args
.fwa_ro6_pmtu
= ro_pmtu
;
1449 args
.fwa_origifp
= origifp
;
1451 args
.fwa_alwaysfrag
= alwaysfrag
;
1452 args
.fwa_unfragpartlen
= unfragpartlen
;
1453 args
.fwa_exthdrs
= &exthdrs
;
1454 /* Invoke outbound packet filter */
1455 error
= pf_af_hook(ifp
, NULL
, &m
, AF_INET6
, FALSE
, &args
);
1456 #else /* !DUMMYNET */
1457 error
= pf_af_hook(ifp
, NULL
, &m
, AF_INET6
, FALSE
, NULL
);
1458 #endif /* !DUMMYNET */
1460 if (error
!= 0 || m
== NULL
) {
1462 panic("%s: unexpected packet %p\n",
1466 /* m was already freed by callee and is now NULL. */
1469 ip6
= mtod(m
, struct ip6_hdr
*);
1474 /* clean ipsec history before fragmentation */
1478 if (ip6oa
!= NULL
) {
1481 dscp
= (ntohl(ip6
->ip6_flow
) & IP6FLOW_DSCP_MASK
) >> IP6FLOW_DSCP_SHIFT
;
1483 error
= set_packet_qos(m
, ifp
,
1484 ip6oa
->ip6oa_flags
& IP6OAF_QOSMARKING_ALLOWED
? TRUE
: FALSE
,
1485 ip6oa
->ip6oa_sotc
, ip6oa
->ip6oa_netsvctype
, &dscp
);
1487 ip6
->ip6_flow
&= ~htonl(IP6FLOW_DSCP_MASK
);
1488 ip6
->ip6_flow
|= htonl((u_int32_t
)dscp
<< IP6FLOW_DSCP_SHIFT
);
1490 printf("%s if_dscp_for_mbuf() error %d\n", __func__
, error
);
1495 * Determine whether fragmentation is necessary. If so, m is passed
1496 * back as a chain of packets and original mbuf is freed. Otherwise, m
1499 error
= ip6_fragment_packet(&m
, opt
,
1500 &exthdrs
, ifp
, mtu
, alwaysfrag
, unfragpartlen
, ro_pmtu
, nxt0
,
1508 * The evaluateloop label is where we decide whether to continue looping over
1509 * packets or call into nd code to send.
1514 * m may be NULL when we jump to the evaluateloop label from PF or
1515 * other code that can drop packets.
1519 * If we already have a chain to send, tack m onto the end.
1520 * Otherwise make m the start and end of the to-be-sent chain.
1522 if (sendchain
!= NULL
) {
1523 sendchain_last
->m_nextpkt
= m
;
1528 /* Fragmentation may mean m is a chain. Find the last packet. */
1529 while (m
->m_nextpkt
) {
1536 /* Fill in next m from inputchain as appropriate. */
1539 /* Isolate m from rest of input chain. */
1540 inputchain
= m
->m_nextpkt
;
1541 m
->m_nextpkt
= NULL
;
1544 * Clear exthdrs and ipsec_state so stale contents are not
1545 * reused. Note this also clears the exthdrs.merged flag.
1547 bzero(&exthdrs
, sizeof(exthdrs
));
1548 bzero(&ipsec_state
, sizeof(ipsec_state
));
1550 /* Continue looping. */
1555 * If we get here, there's no more mbufs in inputchain, so send the
1556 * sendchain if there is one.
1559 error
= nd6_output_list(ifp
, origifp
, sendchain
, dst
,
1562 * Fall through to done label even in error case because
1563 * nd6_output_list frees packetchain in both success and
1569 if (ifpp_save
!= NULL
&& *ifpp_save
!= NULL
) {
1570 ifnet_release(*ifpp_save
);
1573 ROUTE_RELEASE(&ip6route
);
1575 ROUTE_RELEASE(&ipsec_state
.ro
);
1577 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1581 ROUTE_RELEASE(&necp_route
);
1584 ROUTE_RELEASE(&saved_route
);
1585 ROUTE_RELEASE(&saved_ro_pmtu
);
1586 #endif /* DUMMYNET */
1589 IFA_REMREF(&ia
->ia_ifa
);
1591 if (src_ia
!= NULL
) {
1592 IFA_REMREF(&src_ia
->ia_ifa
);
1597 if (origifp
!= NULL
) {
1598 ifnet_release(origifp
);
1600 if (ip6_output_measure
) {
1601 net_perf_measure_time(&net_perf
, &start_tv
, packets_processed
);
1602 net_perf_histogram(&net_perf
, packets_processed
);
1607 if (exthdrs
.ip6e_hbh
!= NULL
) {
1608 if (exthdrs
.merged
) {
1609 panic("Double free of ip6e_hbh");
1611 m_freem(exthdrs
.ip6e_hbh
);
1613 if (exthdrs
.ip6e_dest1
!= NULL
) {
1614 if (exthdrs
.merged
) {
1615 panic("Double free of ip6e_dest1");
1617 m_freem(exthdrs
.ip6e_dest1
);
1619 if (exthdrs
.ip6e_rthdr
!= NULL
) {
1620 if (exthdrs
.merged
) {
1621 panic("Double free of ip6e_rthdr");
1623 m_freem(exthdrs
.ip6e_rthdr
);
1625 if (exthdrs
.ip6e_dest2
!= NULL
) {
1626 if (exthdrs
.merged
) {
1627 panic("Double free of ip6e_dest2");
1629 m_freem(exthdrs
.ip6e_dest2
);
1633 if (inputchain
!= NULL
) {
1634 m_freem_list(inputchain
);
1636 if (sendchain
!= NULL
) {
1637 m_freem_list(sendchain
);
1650 #undef saved_ro_pmtu
1654 /* ip6_fragment_packet
1656 * The fragmentation logic is rather complex:
1657 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1658 * 1-a: send as is if tlen <= path mtu
1659 * 1-b: fragment if tlen > path mtu
1661 * 2: if user asks us not to fragment (dontfrag == 1)
1662 * 2-a: send as is if tlen <= interface mtu
1663 * 2-b: error if tlen > interface mtu
1665 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1668 * 4: if dontfrag == 1 && alwaysfrag == 1
1669 * error, as we cannot handle this conflicting request
1673 ip6_fragment_packet(struct mbuf
**mptr
, struct ip6_pktopts
*opt
,
1674 struct ip6_exthdrs
*exthdrsp
, struct ifnet
*ifp
, uint32_t mtu
,
1675 boolean_t alwaysfrag
, uint32_t unfragpartlen
, struct route_in6
*ro_pmtu
,
1676 int nxt0
, uint32_t optlen
)
1678 VERIFY(NULL
!= mptr
);
1679 struct mbuf
*m
= *mptr
;
1681 size_t tlen
= m
->m_pkthdr
.len
;
1682 boolean_t dontfrag
= (opt
!= NULL
&& (opt
->ip6po_flags
& IP6PO_DONTFRAG
));
1684 if (m
->m_pkthdr
.pkt_flags
& PKTF_FORWARDED
) {
1687 * Discard partial sum information if this packet originated
1688 * from another interface; the packet would already have the
1689 * final checksum and we shouldn't recompute it.
1691 if ((m
->m_pkthdr
.csum_flags
& (CSUM_DATA_VALID
| CSUM_PARTIAL
)) ==
1692 (CSUM_DATA_VALID
| CSUM_PARTIAL
)) {
1693 m
->m_pkthdr
.csum_flags
&= ~CSUM_TX_FLAGS
;
1694 m
->m_pkthdr
.csum_data
= 0;
1698 if (dontfrag
&& alwaysfrag
) { /* case 4 */
1699 /* conflicting request - can't transmit */
1703 /* Access without acquiring nd_ifinfo lock for performance */
1704 if (dontfrag
&& tlen
> IN6_LINKMTU(ifp
)) { /* case 2-b */
1706 * Even if the DONTFRAG option is specified, we cannot send the
1707 * packet when the data length is larger than the MTU of the
1708 * outgoing interface.
1709 * Notify the error by sending IPV6_PATHMTU ancillary data as
1710 * well as returning an error code (the latter is not described
1714 struct ip6ctlparam ip6cp
;
1716 mtu32
= (u_int32_t
)mtu
;
1717 bzero(&ip6cp
, sizeof(ip6cp
));
1718 ip6cp
.ip6c_cmdarg
= (void *)&mtu32
;
1719 pfctlinput2(PRC_MSGSIZE
, SA(&ro_pmtu
->ro_dst
), (void *)&ip6cp
);
1724 * transmit packet without fragmentation
1726 if (dontfrag
|| (!alwaysfrag
&& /* case 1-a and 2-a */
1727 (tlen
<= mtu
|| TSO_IPV6_OK(ifp
, m
) ||
1728 (ifp
->if_hwassist
& CSUM_FRAGMENT_IPV6
)))) {
1730 * mppn not updated in this case because no new chain is formed
1733 ip6_output_checksum(ifp
, mtu
, m
, nxt0
, tlen
, optlen
);
1736 * time to fragment - cases 1-b and 3 are handled inside
1737 * ip6_do_fragmentation().
1738 * mppn is passed down to be updated to point at fragment chain.
1740 error
= ip6_do_fragmentation(mptr
, optlen
, ifp
,
1741 unfragpartlen
, mtod(m
, struct ip6_hdr
*), exthdrsp
, mtu
, nxt0
);
1748 * ip6_do_fragmentation() is called by ip6_fragment_packet() after determining
1749 * the packet needs to be fragmented. on success, morig is freed and a chain
1750 * of fragments is linked into the packet chain where morig existed. Otherwise,
1751 * an errno is returned.
1754 ip6_do_fragmentation(struct mbuf
**mptr
, uint32_t optlen
, struct ifnet
*ifp
,
1755 uint32_t unfragpartlen
, struct ip6_hdr
*ip6
, struct ip6_exthdrs
*exthdrsp
,
1756 uint32_t mtu
, int nxt0
)
1758 VERIFY(NULL
!= mptr
);
1761 struct mbuf
*morig
= *mptr
;
1762 struct mbuf
*first_mbufp
= NULL
;
1763 struct mbuf
*last_mbufp
= NULL
;
1765 size_t tlen
= morig
->m_pkthdr
.len
;
1768 * try to fragment the packet. case 1-b and 3
1770 if ((morig
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV6
)) {
1771 /* TSO and fragment aren't compatible */
1772 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1774 } else if (mtu
< IPV6_MMTU
) {
1775 /* path MTU cannot be less than IPV6_MMTU */
1776 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1778 } else if (ip6
->ip6_plen
== 0) {
1779 /* jumbo payload cannot be fragmented */
1780 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1783 size_t hlen
, len
, off
;
1784 struct mbuf
**mnext
= NULL
;
1785 struct ip6_frag
*ip6f
;
1786 u_int32_t id
= htonl(ip6_randomid());
1790 * Too large for the destination or interface;
1791 * fragment if possible.
1792 * Must be able to put at least 8 bytes per fragment.
1794 hlen
= unfragpartlen
;
1795 if (mtu
> IPV6_MAXPACKET
) {
1796 mtu
= IPV6_MAXPACKET
;
1799 len
= (mtu
- hlen
- sizeof(struct ip6_frag
)) & ~7;
1801 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1806 * Change the next header field of the last header in the
1807 * unfragmentable part.
1809 if (exthdrsp
->ip6e_rthdr
!= NULL
) {
1810 nextproto
= *mtod(exthdrsp
->ip6e_rthdr
, u_char
*);
1811 *mtod(exthdrsp
->ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
1812 } else if (exthdrsp
->ip6e_dest1
!= NULL
) {
1813 nextproto
= *mtod(exthdrsp
->ip6e_dest1
, u_char
*);
1814 *mtod(exthdrsp
->ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
1815 } else if (exthdrsp
->ip6e_hbh
!= NULL
) {
1816 nextproto
= *mtod(exthdrsp
->ip6e_hbh
, u_char
*);
1817 *mtod(exthdrsp
->ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
1819 nextproto
= ip6
->ip6_nxt
;
1820 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
1823 if (morig
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
) {
1824 in6_delayed_cksum_offset(morig
, 0, optlen
, nxt0
);
1828 * Loop through length of segment after first fragment,
1829 * make new header and copy data of each part and link onto
1832 for (off
= hlen
; off
< tlen
; off
+= len
) {
1833 struct ip6_hdr
*new_mhip6
;
1835 struct mbuf
*m_frgpart
;
1837 MGETHDR(new_m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1838 if (new_m
== NULL
) {
1840 ip6stat
.ip6s_odropped
++;
1843 new_m
->m_pkthdr
.rcvif
= NULL
;
1844 new_m
->m_flags
= morig
->m_flags
& M_COPYFLAGS
;
1846 if (first_mbufp
!= NULL
) {
1847 /* Every pass through loop but first */
1851 /* This is the first element of the fragment chain */
1852 first_mbufp
= new_m
;
1855 mnext
= &new_m
->m_nextpkt
;
1857 new_m
->m_data
+= max_linkhdr
;
1858 new_mhip6
= mtod(new_m
, struct ip6_hdr
*);
1860 new_m
->m_len
= sizeof(*new_mhip6
);
1862 error
= ip6_insertfraghdr(morig
, new_m
, hlen
, &ip6f
);
1864 ip6stat
.ip6s_odropped
++;
1868 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1869 if (off
+ len
>= tlen
) {
1872 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1874 new_mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1875 sizeof(*ip6f
) - sizeof(struct ip6_hdr
)));
1877 if ((m_frgpart
= m_copy(morig
, off
, len
)) == NULL
) {
1879 ip6stat
.ip6s_odropped
++;
1882 m_cat(new_m
, m_frgpart
);
1883 new_m
->m_pkthdr
.len
= len
+ hlen
+ sizeof(*ip6f
);
1884 new_m
->m_pkthdr
.rcvif
= NULL
;
1886 M_COPY_CLASSIFIER(new_m
, morig
);
1887 M_COPY_PFTAG(new_m
, morig
);
1891 mac_create_fragment(morig
, new_m
);
1892 #endif /* CONFIG_MACF_NET */
1895 ip6f
->ip6f_reserved
= 0;
1896 ip6f
->ip6f_ident
= id
;
1897 ip6f
->ip6f_nxt
= nextproto
;
1898 ip6stat
.ip6s_ofragments
++;
1899 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1903 /* free all the fragments created */
1904 if (first_mbufp
!= NULL
) {
1905 m_freem_list(first_mbufp
);
1910 /* successful fragmenting */
1912 *mptr
= first_mbufp
;
1913 last_mbufp
->m_nextpkt
= NULL
;
1914 ip6stat
.ip6s_fragmented
++;
1915 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1922 ip6_copyexthdr(struct mbuf
**mp
, caddr_t hdr
, int hlen
)
1926 if (hlen
> MCLBYTES
) {
1927 return ENOBUFS
; /* XXX */
1929 MGET(m
, M_DONTWAIT
, MT_DATA
);
1935 MCLGET(m
, M_DONTWAIT
);
1936 if (!(m
->m_flags
& M_EXT
)) {
1943 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1951 ip6_out_cksum_stats(int proto
, u_int32_t len
)
1955 tcp_out6_cksum_stats(len
);
1958 udp_out6_cksum_stats(len
);
1961 /* keep only TCP or UDP stats for now */
1967 * Process a delayed payload checksum calculation (outbound path.)
1969 * hoff is the number of bytes beyond the mbuf data pointer which
1970 * points to the IPv6 header. optlen is the number of bytes, if any,
1971 * between the end of IPv6 header and the beginning of the ULP payload
1972 * header, which represents the extension headers. If optlen is less
1973 * than zero, this routine will bail when it detects extension headers.
1975 * Returns a bitmask representing all the work done in software.
1978 in6_finalize_cksum(struct mbuf
*m
, uint32_t hoff
, int32_t optlen
,
1979 int32_t nxt0
, uint32_t csum_flags
)
1981 unsigned char buf
[sizeof(struct ip6_hdr
)] __attribute__((aligned(8)));
1982 struct ip6_hdr
*ip6
;
1983 uint32_t offset
, mlen
, hlen
, olen
, sw_csum
;
1984 uint16_t csum
, ulpoff
, plen
;
1987 _CASSERT(sizeof(csum
) == sizeof(uint16_t));
1988 VERIFY(m
->m_flags
& M_PKTHDR
);
1990 sw_csum
= (csum_flags
& m
->m_pkthdr
.csum_flags
);
1992 if ((sw_csum
&= CSUM_DELAY_IPV6_DATA
) == 0) {
1996 mlen
= m
->m_pkthdr
.len
; /* total mbuf len */
1997 hlen
= sizeof(*ip6
); /* IPv6 header len */
1999 /* sanity check (need at least IPv6 header) */
2000 if (mlen
< (hoff
+ hlen
)) {
2001 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr "
2002 "(%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
);
2007 * In case the IPv6 header is not contiguous, or not 32-bit
2008 * aligned, copy it to a local buffer.
2010 if ((hoff
+ hlen
) > m
->m_len
||
2011 !IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
) + hoff
)) {
2012 m_copydata(m
, hoff
, hlen
, (caddr_t
)buf
);
2013 ip6
= (struct ip6_hdr
*)(void *)buf
;
2015 ip6
= (struct ip6_hdr
*)(void *)(m
->m_data
+ hoff
);
2019 plen
= ntohs(ip6
->ip6_plen
);
2020 if (plen
!= (mlen
- (hoff
+ hlen
))) {
2021 plen
= OSSwapInt16(plen
);
2022 if (plen
!= (mlen
- (hoff
+ hlen
))) {
2023 /* Don't complain for jumbograms */
2024 if (plen
!= 0 || nxt
!= IPPROTO_HOPOPTS
) {
2025 printf("%s: mbuf 0x%llx proto %d IPv6 "
2026 "plen %d (%x) [swapped %d (%x)] doesn't "
2027 "match actual packet length; %d is used "
2028 "instead\n", __func__
,
2029 (uint64_t)VM_KERNEL_ADDRPERM(m
), nxt
,
2030 ip6
->ip6_plen
, ip6
->ip6_plen
, plen
, plen
,
2031 (mlen
- (hoff
+ hlen
)));
2033 plen
= mlen
- (hoff
+ hlen
);
2038 /* next header isn't TCP/UDP and we don't know optlen, bail */
2039 if (nxt
!= IPPROTO_TCP
&& nxt
!= IPPROTO_UDP
) {
2045 /* caller supplied the original transport number; use it */
2052 offset
= hoff
+ hlen
+ olen
; /* ULP header */
2055 if (mlen
< offset
) {
2056 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr+ext_hdr "
2057 "(%u+%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
, olen
);
2062 * offset is added to the lower 16-bit value of csum_data,
2063 * which is expected to contain the ULP offset; therefore
2064 * CSUM_PARTIAL offset adjustment must be undone.
2066 if ((m
->m_pkthdr
.csum_flags
& (CSUM_PARTIAL
| CSUM_DATA_VALID
)) ==
2067 (CSUM_PARTIAL
| CSUM_DATA_VALID
)) {
2069 * Get back the original ULP offset (this will
2070 * undo the CSUM_PARTIAL logic in ip6_output.)
2072 m
->m_pkthdr
.csum_data
= (m
->m_pkthdr
.csum_tx_stuff
-
2073 m
->m_pkthdr
.csum_tx_start
);
2076 ulpoff
= (m
->m_pkthdr
.csum_data
& 0xffff); /* ULP csum offset */
2078 if (mlen
< (ulpoff
+ sizeof(csum
))) {
2079 panic("%s: mbuf %p pkt len (%u) proto %d invalid ULP "
2080 "cksum offset (%u) cksum flags 0x%x\n", __func__
,
2081 m
, mlen
, nxt
, ulpoff
, m
->m_pkthdr
.csum_flags
);
2085 csum
= inet6_cksum(m
, 0, offset
, plen
- olen
);
2088 ip6_out_cksum_stats(nxt
, plen
- olen
);
2090 /* RFC1122 4.1.3.4 */
2092 (m
->m_pkthdr
.csum_flags
& (CSUM_UDPIPV6
| CSUM_ZERO_INVERT
))) {
2096 /* Insert the checksum in the ULP csum field */
2098 if ((offset
+ sizeof(csum
)) > m
->m_len
) {
2099 m_copyback(m
, offset
, sizeof(csum
), &csum
);
2100 } else if (IP6_HDR_ALIGNED_P(mtod(m
, char *) + hoff
)) {
2101 *(uint16_t *)(void *)(mtod(m
, char *) + offset
) = csum
;
2103 bcopy(&csum
, (mtod(m
, char *) + offset
), sizeof(csum
));
2105 m
->m_pkthdr
.csum_flags
&= ~(CSUM_DELAY_IPV6_DATA
| CSUM_DATA_VALID
|
2106 CSUM_PARTIAL
| CSUM_ZERO_INVERT
);
2113 * Insert jumbo payload option.
2116 ip6_insert_jumboopt(struct ip6_exthdrs
*exthdrs
, u_int32_t plen
)
2122 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
2125 * If there is no hop-by-hop options header, allocate new one.
2126 * If there is one but it doesn't have enough space to store the
2127 * jumbo payload option, allocate a cluster to store the whole options.
2128 * Otherwise, use it to store the options.
2130 if (exthdrs
->ip6e_hbh
== NULL
) {
2131 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
2135 mopt
->m_len
= JUMBOOPTLEN
;
2136 optbuf
= mtod(mopt
, u_char
*);
2137 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
2138 exthdrs
->ip6e_hbh
= mopt
;
2140 struct ip6_hbh
*hbh
;
2142 mopt
= exthdrs
->ip6e_hbh
;
2143 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
2146 * - exthdrs->ip6e_hbh is not referenced from places
2147 * other than exthdrs.
2148 * - exthdrs->ip6e_hbh is not an mbuf chain.
2150 u_int32_t oldoptlen
= mopt
->m_len
;
2154 * XXX: give up if the whole (new) hbh header does
2155 * not fit even in an mbuf cluster.
2157 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
) {
2162 * As a consequence, we must always prepare a cluster
2165 MGET(n
, M_DONTWAIT
, MT_DATA
);
2167 MCLGET(n
, M_DONTWAIT
);
2168 if (!(n
->m_flags
& M_EXT
)) {
2176 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
2177 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
2179 optbuf
= mtod(n
, u_char
*) + oldoptlen
;
2181 mopt
= exthdrs
->ip6e_hbh
= n
;
2183 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
2184 mopt
->m_len
+= JUMBOOPTLEN
;
2186 optbuf
[0] = IP6OPT_PADN
;
2190 * Adjust the header length according to the pad and
2191 * the jumbo payload option.
2193 hbh
= mtod(mopt
, struct ip6_hbh
*);
2194 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
2197 /* fill in the option. */
2198 optbuf
[2] = IP6OPT_JUMBO
;
2200 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
2201 bcopy(&v
, &optbuf
[4], sizeof(u_int32_t
));
2203 /* finally, adjust the packet header length */
2204 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
2211 * Insert fragment header and copy unfragmentable header portions.
2214 ip6_insertfraghdr(struct mbuf
*m0
, struct mbuf
*m
, int hlen
,
2215 struct ip6_frag
**frghdrp
)
2217 struct mbuf
*n
, *mlast
;
2219 if (hlen
> sizeof(struct ip6_hdr
)) {
2220 n
= m_copym(m0
, sizeof(struct ip6_hdr
),
2221 hlen
- sizeof(struct ip6_hdr
), M_DONTWAIT
);
2230 /* Search for the last mbuf of unfragmentable part. */
2231 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
) {
2235 if (!(mlast
->m_flags
& M_EXT
) &&
2236 M_TRAILINGSPACE(mlast
) >= sizeof(struct ip6_frag
)) {
2237 /* use the trailing space of the last mbuf for the frag hdr */
2238 *frghdrp
= (struct ip6_frag
*)(mtod(mlast
, caddr_t
) +
2240 mlast
->m_len
+= sizeof(struct ip6_frag
);
2241 m
->m_pkthdr
.len
+= sizeof(struct ip6_frag
);
2243 /* allocate a new mbuf for the fragment header */
2246 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
2250 mfrg
->m_len
= sizeof(struct ip6_frag
);
2251 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
2252 mlast
->m_next
= mfrg
;
2259 ip6_getpmtu(struct route_in6
*ro_pmtu
, struct route_in6
*ro
,
2260 struct ifnet
*ifp
, struct in6_addr
*dst
, u_int32_t
*mtup
,
2261 boolean_t
*alwaysfragp
)
2264 boolean_t alwaysfrag
= FALSE
;
2266 boolean_t is_local
= FALSE
;
2268 if (IN6_IS_SCOPE_LINKLOCAL(dst
)) {
2272 if (ro_pmtu
!= ro
) {
2273 /* The first hop and the final destination may differ. */
2274 struct sockaddr_in6
*sa6_dst
= SIN6(&ro_pmtu
->ro_dst
);
2275 if (ROUTE_UNUSABLE(ro_pmtu
) ||
2276 !IN6_ARE_ADDR_EQUAL(&sa6_dst
->sin6_addr
, dst
)) {
2277 ROUTE_RELEASE(ro_pmtu
);
2280 if (ro_pmtu
->ro_rt
== NULL
) {
2281 bzero(sa6_dst
, sizeof(*sa6_dst
));
2282 sa6_dst
->sin6_family
= AF_INET6
;
2283 sa6_dst
->sin6_len
= sizeof(struct sockaddr_in6
);
2284 sa6_dst
->sin6_addr
= *dst
;
2286 rtalloc_scoped((struct route
*)ro_pmtu
,
2287 ifp
!= NULL
? ifp
->if_index
: IFSCOPE_NONE
);
2291 if (ro_pmtu
->ro_rt
!= NULL
) {
2295 ifp
= ro_pmtu
->ro_rt
->rt_ifp
;
2297 /* Access without acquiring nd_ifinfo lock for performance */
2298 ifmtu
= IN6_LINKMTU(ifp
);
2301 * Access rmx_mtu without holding the route entry lock,
2302 * for performance; this isn't something that changes
2303 * often, so optimize.
2305 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
2306 if (mtu
> ifmtu
|| mtu
== 0) {
2308 * The MTU on the route is larger than the MTU on
2309 * the interface! This shouldn't happen, unless the
2310 * MTU of the interface has been changed after the
2311 * interface was brought up. Change the MTU in the
2312 * route to match the interface MTU (as long as the
2313 * field isn't locked).
2315 * if MTU on the route is 0, we need to fix the MTU.
2316 * this case happens with path MTU discovery timeouts.
2319 if (!(ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
2320 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
2322 } else if (mtu
< IPV6_MMTU
) {
2324 * RFC2460 section 5, last paragraph:
2325 * if we record ICMPv6 too big message with
2326 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
2327 * or smaller, with framgent header attached.
2328 * (fragment header is needed regardless from the
2329 * packet size, for translators to identify packets)
2336 /* Don't hold nd_ifinfo lock for performance */
2337 mtu
= IN6_LINKMTU(ifp
);
2339 error
= EHOSTUNREACH
; /* XXX */
2344 if ((alwaysfragp
!= NULL
) && !is_local
) {
2345 *alwaysfragp
= alwaysfrag
;
2351 * IP6 socket option processing.
2354 ip6_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2356 int optdatalen
, uproto
;
2359 struct inpcb
*in6p
= sotoinpcb(so
);
2360 int error
= 0, optval
= 0;
2361 int level
, op
= -1, optname
= 0;
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
;
2381 case IPV6_2292PKTOPTIONS
: {
2384 error
= soopt_getm(sopt
, &m
);
2388 error
= soopt_mcopyin(sopt
, m
);
2392 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
2399 * Use of some Hop-by-Hop options or some
2400 * Destination options, might require special
2401 * privilege. That is, normal applications
2402 * (without special privilege) might be forbidden
2403 * from setting certain options in outgoing packets,
2404 * and might never see certain options in received
2405 * packets. [RFC 2292 Section 6]
2406 * KAME specific note:
2407 * KAME prevents non-privileged users from sending or
2408 * receiving ANY hbh/dst options in order to avoid
2409 * overhead of parsing options in the kernel.
2411 case IPV6_RECVHOPOPTS
:
2412 case IPV6_RECVDSTOPTS
:
2413 case IPV6_RECVRTHDRDSTOPTS
:
2418 case IPV6_UNICAST_HOPS
:
2420 case IPV6_RECVPKTINFO
:
2421 case IPV6_RECVHOPLIMIT
:
2422 case IPV6_RECVRTHDR
:
2423 case IPV6_RECVPATHMTU
:
2424 case IPV6_RECVTCLASS
:
2426 case IPV6_AUTOFLOWLABEL
:
2427 if (optlen
!= sizeof(int)) {
2431 error
= sooptcopyin(sopt
, &optval
,
2432 sizeof(optval
), sizeof(optval
));
2438 case IPV6_UNICAST_HOPS
:
2439 if (optval
< -1 || optval
>= 256) {
2442 /* -1 = kernel default */
2443 in6p
->in6p_hops
= optval
;
2444 if (in6p
->inp_vflag
&
2451 #define OPTSET(bit) do { \
2453 in6p->inp_flags |= (bit); \
2455 in6p->inp_flags &= ~(bit); \
2458 #define OPTSET2292(bit) do { \
2459 in6p->inp_flags |= IN6P_RFC2292; \
2461 in6p->inp_flags |= (bit); \
2463 in6p->inp_flags &= ~(bit); \
2466 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
2468 case IPV6_RECVPKTINFO
:
2469 /* cannot mix with RFC2292 */
2470 if (OPTBIT(IN6P_RFC2292
)) {
2474 OPTSET(IN6P_PKTINFO
);
2477 case IPV6_HOPLIMIT
: {
2478 struct ip6_pktopts
**optp
;
2480 /* cannot mix with RFC2292 */
2481 if (OPTBIT(IN6P_RFC2292
)) {
2485 optp
= &in6p
->in6p_outputopts
;
2486 error
= ip6_pcbopt(IPV6_HOPLIMIT
,
2487 (u_char
*)&optval
, sizeof(optval
),
2492 case IPV6_RECVHOPLIMIT
:
2493 /* cannot mix with RFC2292 */
2494 if (OPTBIT(IN6P_RFC2292
)) {
2498 OPTSET(IN6P_HOPLIMIT
);
2501 case IPV6_RECVHOPOPTS
:
2502 /* cannot mix with RFC2292 */
2503 if (OPTBIT(IN6P_RFC2292
)) {
2507 OPTSET(IN6P_HOPOPTS
);
2508 capture_exthdrstat_in
= TRUE
;
2511 case IPV6_RECVDSTOPTS
:
2512 /* cannot mix with RFC2292 */
2513 if (OPTBIT(IN6P_RFC2292
)) {
2517 OPTSET(IN6P_DSTOPTS
);
2518 capture_exthdrstat_in
= TRUE
;
2521 case IPV6_RECVRTHDRDSTOPTS
:
2522 /* cannot mix with RFC2292 */
2523 if (OPTBIT(IN6P_RFC2292
)) {
2527 OPTSET(IN6P_RTHDRDSTOPTS
);
2528 capture_exthdrstat_in
= TRUE
;
2531 case IPV6_RECVRTHDR
:
2532 /* cannot mix with RFC2292 */
2533 if (OPTBIT(IN6P_RFC2292
)) {
2538 capture_exthdrstat_in
= TRUE
;
2541 case IPV6_RECVPATHMTU
:
2543 * We ignore this option for TCP
2545 * (RFC3542 leaves this case
2548 if (uproto
!= IPPROTO_TCP
) {
2555 * make setsockopt(IPV6_V6ONLY)
2556 * available only prior to bind(2).
2557 * see ipng mailing list, Jun 22 2001.
2559 if (in6p
->inp_lport
||
2560 !IN6_IS_ADDR_UNSPECIFIED(
2561 &in6p
->in6p_laddr
)) {
2565 OPTSET(IN6P_IPV6_V6ONLY
);
2567 in6p
->inp_vflag
&= ~INP_IPV4
;
2569 in6p
->inp_vflag
|= INP_IPV4
;
2573 case IPV6_RECVTCLASS
:
2574 /* we can mix with RFC2292 */
2575 OPTSET(IN6P_TCLASS
);
2578 case IPV6_AUTOFLOWLABEL
:
2579 OPTSET(IN6P_AUTOFLOWLABEL
);
2586 case IPV6_USE_MIN_MTU
:
2587 case IPV6_PREFER_TEMPADDR
: {
2588 struct ip6_pktopts
**optp
;
2590 if (optlen
!= sizeof(optval
)) {
2594 error
= sooptcopyin(sopt
, &optval
,
2595 sizeof(optval
), sizeof(optval
));
2600 optp
= &in6p
->in6p_outputopts
;
2601 error
= ip6_pcbopt(optname
, (u_char
*)&optval
,
2602 sizeof(optval
), optp
, uproto
);
2604 if (optname
== IPV6_TCLASS
) {
2605 // Add in the ECN flags
2606 u_int8_t tos
= (in6p
->inp_ip_tos
& ~IPTOS_ECN_MASK
);
2607 u_int8_t ecn
= optval
& IPTOS_ECN_MASK
;
2608 in6p
->inp_ip_tos
= tos
| ecn
;
2613 case IPV6_2292PKTINFO
:
2614 case IPV6_2292HOPLIMIT
:
2615 case IPV6_2292HOPOPTS
:
2616 case IPV6_2292DSTOPTS
:
2617 case IPV6_2292RTHDR
:
2619 if (optlen
!= sizeof(int)) {
2623 error
= sooptcopyin(sopt
, &optval
,
2624 sizeof(optval
), sizeof(optval
));
2629 case IPV6_2292PKTINFO
:
2630 OPTSET2292(IN6P_PKTINFO
);
2632 case IPV6_2292HOPLIMIT
:
2633 OPTSET2292(IN6P_HOPLIMIT
);
2635 case IPV6_2292HOPOPTS
:
2637 * Check super-user privilege.
2638 * See comments for IPV6_RECVHOPOPTS.
2643 OPTSET2292(IN6P_HOPOPTS
);
2644 capture_exthdrstat_in
= TRUE
;
2646 case IPV6_2292DSTOPTS
:
2650 OPTSET2292(IN6P_DSTOPTS
|
2651 IN6P_RTHDRDSTOPTS
); /* XXX */
2652 capture_exthdrstat_in
= TRUE
;
2654 case IPV6_2292RTHDR
:
2655 OPTSET2292(IN6P_RTHDR
);
2656 capture_exthdrstat_in
= TRUE
;
2661 case IPV6_3542PKTINFO
:
2662 case IPV6_3542HOPOPTS
:
2663 case IPV6_3542RTHDR
:
2664 case IPV6_3542DSTOPTS
:
2665 case IPV6_RTHDRDSTOPTS
:
2666 case IPV6_3542NEXTHOP
: {
2667 struct ip6_pktopts
**optp
;
2668 /* new advanced API (RFC3542) */
2671 /* cannot mix with RFC2292 */
2672 if (OPTBIT(IN6P_RFC2292
)) {
2676 error
= soopt_getm(sopt
, &m
);
2680 error
= soopt_mcopyin(sopt
, m
);
2685 optp
= &in6p
->in6p_outputopts
;
2686 error
= ip6_pcbopt(optname
, mtod(m
, u_char
*),
2687 m
->m_len
, optp
, uproto
);
2692 case IPV6_MULTICAST_IF
:
2693 case IPV6_MULTICAST_HOPS
:
2694 case IPV6_MULTICAST_LOOP
:
2695 case IPV6_JOIN_GROUP
:
2696 case IPV6_LEAVE_GROUP
:
2698 case MCAST_BLOCK_SOURCE
:
2699 case MCAST_UNBLOCK_SOURCE
:
2700 case MCAST_JOIN_GROUP
:
2701 case MCAST_LEAVE_GROUP
:
2702 case MCAST_JOIN_SOURCE_GROUP
:
2703 case MCAST_LEAVE_SOURCE_GROUP
:
2704 error
= ip6_setmoptions(in6p
, sopt
);
2707 case IPV6_PORTRANGE
:
2708 error
= sooptcopyin(sopt
, &optval
,
2709 sizeof(optval
), sizeof(optval
));
2715 case IPV6_PORTRANGE_DEFAULT
:
2716 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2717 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2720 case IPV6_PORTRANGE_HIGH
:
2721 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2722 in6p
->inp_flags
|= INP_HIGHPORT
;
2725 case IPV6_PORTRANGE_LOW
:
2726 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2727 in6p
->inp_flags
|= INP_LOWPORT
;
2736 case IPV6_IPSEC_POLICY
: {
2741 if ((error
= soopt_getm(sopt
, &m
)) != 0) {
2744 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) {
2748 req
= mtod(m
, caddr_t
);
2750 error
= ipsec6_set_policy(in6p
, optname
, req
,
2757 * IPv6 variant of IP_BOUND_IF; for details see
2758 * comments on IP_BOUND_IF in ip_ctloutput().
2761 /* This option is settable only on IPv6 */
2762 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2767 error
= sooptcopyin(sopt
, &optval
,
2768 sizeof(optval
), sizeof(optval
));
2774 error
= inp_bindif(in6p
, optval
, NULL
);
2777 case IPV6_NO_IFT_CELLULAR
:
2778 /* This option is settable only for IPv6 */
2779 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2784 error
= sooptcopyin(sopt
, &optval
,
2785 sizeof(optval
), sizeof(optval
));
2791 /* once set, it cannot be unset */
2792 if (!optval
&& INP_NO_CELLULAR(in6p
)) {
2797 error
= so_set_restrictions(so
,
2798 SO_RESTRICT_DENY_CELLULAR
);
2802 /* This option is not settable */
2807 error
= ENOPROTOOPT
;
2810 if (capture_exthdrstat_in
) {
2811 if (uproto
== IPPROTO_TCP
) {
2812 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_stream_exthdr_in
);
2813 } else if (uproto
== IPPROTO_UDP
) {
2814 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_dgram_exthdr_in
);
2821 case IPV6_2292PKTOPTIONS
:
2823 * RFC3542 (effectively) deprecated the
2824 * semantics of the 2292-style pktoptions.
2825 * Since it was not reliable in nature (i.e.,
2826 * applications had to expect the lack of some
2827 * information after all), it would make sense
2828 * to simplify this part by always returning
2831 sopt
->sopt_valsize
= 0;
2834 case IPV6_RECVHOPOPTS
:
2835 case IPV6_RECVDSTOPTS
:
2836 case IPV6_RECVRTHDRDSTOPTS
:
2837 case IPV6_UNICAST_HOPS
:
2838 case IPV6_RECVPKTINFO
:
2839 case IPV6_RECVHOPLIMIT
:
2840 case IPV6_RECVRTHDR
:
2841 case IPV6_RECVPATHMTU
:
2843 case IPV6_PORTRANGE
:
2844 case IPV6_RECVTCLASS
:
2845 case IPV6_AUTOFLOWLABEL
:
2847 case IPV6_RECVHOPOPTS
:
2848 optval
= OPTBIT(IN6P_HOPOPTS
);
2851 case IPV6_RECVDSTOPTS
:
2852 optval
= OPTBIT(IN6P_DSTOPTS
);
2855 case IPV6_RECVRTHDRDSTOPTS
:
2856 optval
= OPTBIT(IN6P_RTHDRDSTOPTS
);
2859 case IPV6_UNICAST_HOPS
:
2860 optval
= in6p
->in6p_hops
;
2863 case IPV6_RECVPKTINFO
:
2864 optval
= OPTBIT(IN6P_PKTINFO
);
2867 case IPV6_RECVHOPLIMIT
:
2868 optval
= OPTBIT(IN6P_HOPLIMIT
);
2871 case IPV6_RECVRTHDR
:
2872 optval
= OPTBIT(IN6P_RTHDR
);
2875 case IPV6_RECVPATHMTU
:
2876 optval
= OPTBIT(IN6P_MTU
);
2880 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
2883 case IPV6_PORTRANGE
: {
2885 flags
= in6p
->inp_flags
;
2886 if (flags
& INP_HIGHPORT
) {
2887 optval
= IPV6_PORTRANGE_HIGH
;
2888 } else if (flags
& INP_LOWPORT
) {
2889 optval
= IPV6_PORTRANGE_LOW
;
2895 case IPV6_RECVTCLASS
:
2896 optval
= OPTBIT(IN6P_TCLASS
);
2899 case IPV6_AUTOFLOWLABEL
:
2900 optval
= OPTBIT(IN6P_AUTOFLOWLABEL
);
2906 error
= sooptcopyout(sopt
, &optval
,
2910 case IPV6_PATHMTU
: {
2912 struct ip6_mtuinfo mtuinfo
;
2913 struct route_in6 sro
;
2915 bzero(&sro
, sizeof(sro
));
2917 if (!(so
->so_state
& SS_ISCONNECTED
)) {
2921 * XXX: we dot not consider the case of source
2922 * routing, or optional information to specify
2923 * the outgoing interface.
2925 error
= ip6_getpmtu(&sro
, NULL
, NULL
,
2926 &in6p
->in6p_faddr
, &pmtu
, NULL
);
2927 ROUTE_RELEASE(&sro
);
2931 if (pmtu
> IPV6_MAXPACKET
) {
2932 pmtu
= IPV6_MAXPACKET
;
2935 bzero(&mtuinfo
, sizeof(mtuinfo
));
2936 mtuinfo
.ip6m_mtu
= (u_int32_t
)pmtu
;
2937 optdata
= (void *)&mtuinfo
;
2938 optdatalen
= sizeof(mtuinfo
);
2939 error
= sooptcopyout(sopt
, optdata
,
2944 case IPV6_2292PKTINFO
:
2945 case IPV6_2292HOPLIMIT
:
2946 case IPV6_2292HOPOPTS
:
2947 case IPV6_2292RTHDR
:
2948 case IPV6_2292DSTOPTS
:
2950 case IPV6_2292PKTINFO
:
2951 optval
= OPTBIT(IN6P_PKTINFO
);
2953 case IPV6_2292HOPLIMIT
:
2954 optval
= OPTBIT(IN6P_HOPLIMIT
);
2956 case IPV6_2292HOPOPTS
:
2957 optval
= OPTBIT(IN6P_HOPOPTS
);
2959 case IPV6_2292RTHDR
:
2960 optval
= OPTBIT(IN6P_RTHDR
);
2962 case IPV6_2292DSTOPTS
:
2963 optval
= OPTBIT(IN6P_DSTOPTS
|
2967 error
= sooptcopyout(sopt
, &optval
,
2975 case IPV6_RTHDRDSTOPTS
:
2979 case IPV6_USE_MIN_MTU
:
2980 case IPV6_PREFER_TEMPADDR
:
2981 error
= ip6_getpcbopt(in6p
->in6p_outputopts
,
2985 case IPV6_MULTICAST_IF
:
2986 case IPV6_MULTICAST_HOPS
:
2987 case IPV6_MULTICAST_LOOP
:
2989 error
= ip6_getmoptions(in6p
, sopt
);
2992 case IPV6_IPSEC_POLICY
: {
2993 error
= 0; /* This option is no longer supported */
2998 if (in6p
->inp_flags
& INP_BOUND_IF
) {
2999 optval
= in6p
->inp_boundifp
->if_index
;
3001 error
= sooptcopyout(sopt
, &optval
,
3005 case IPV6_NO_IFT_CELLULAR
:
3006 optval
= INP_NO_CELLULAR(in6p
) ? 1 : 0;
3007 error
= sooptcopyout(sopt
, &optval
,
3012 optval
= (in6p
->in6p_last_outifp
!= NULL
) ?
3013 in6p
->in6p_last_outifp
->if_index
: 0;
3014 error
= sooptcopyout(sopt
, &optval
,
3019 error
= ENOPROTOOPT
;
3024 } else if (level
== IPPROTO_UDP
) {
3025 error
= udp_ctloutput(so
, sopt
);
3033 ip6_raw_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
3035 int error
= 0, optval
, optlen
;
3036 const int icmp6off
= offsetof(struct icmp6_hdr
, icmp6_cksum
);
3037 struct inpcb
*in6p
= sotoinpcb(so
);
3038 int level
, op
, optname
;
3040 level
= sopt
->sopt_level
;
3041 op
= sopt
->sopt_dir
;
3042 optname
= sopt
->sopt_name
;
3043 optlen
= sopt
->sopt_valsize
;
3045 if (level
!= IPPROTO_IPV6
) {
3052 * For ICMPv6 sockets, no modification allowed for checksum
3053 * offset, permit "no change" values to help existing apps.
3055 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
3056 * for an ICMPv6 socket will fail."
3057 * The current behavior does not meet RFC3542.
3061 if (optlen
!= sizeof(int)) {
3065 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
3070 if ((optval
% 2) != 0) {
3071 /* the API assumes even offset values */
3073 } else if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
3074 if (optval
!= icmp6off
) {
3078 in6p
->in6p_cksum
= optval
;
3083 if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
3086 optval
= in6p
->in6p_cksum
;
3089 error
= sooptcopyout(sopt
, &optval
, sizeof(optval
));
3099 error
= ENOPROTOOPT
;
3107 * Set up IP6 options in pcb for insertion in output packets or
3108 * specifying behavior of outgoing packets.
3111 ip6_pcbopts(struct ip6_pktopts
**pktopt
, struct mbuf
*m
, struct socket
*so
,
3112 struct sockopt
*sopt
)
3114 #pragma unused(sopt)
3115 struct ip6_pktopts
*opt
= *pktopt
;
3118 /* turn off any old options. */
3121 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
3122 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
3123 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
) {
3124 printf("%s: all specified options are cleared.\n",
3128 ip6_clearpktopts(opt
, -1);
3130 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
3137 if (m
== NULL
|| m
->m_len
== 0) {
3139 * Only turning off any previous options, regardless of
3140 * whether the opt is just created or given.
3143 FREE(opt
, M_IP6OPT
);
3148 /* set options specified by user. */
3149 if ((error
= ip6_setpktopts(m
, opt
, NULL
, SOCK_PROTO(so
))) != 0) {
3150 ip6_clearpktopts(opt
, -1); /* XXX: discard all options */
3151 FREE(opt
, M_IP6OPT
);
3159 * initialize ip6_pktopts. beware that there are non-zero default values in
3163 ip6_initpktopts(struct ip6_pktopts
*opt
)
3165 bzero(opt
, sizeof(*opt
));
3166 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
3167 opt
->ip6po_tclass
= -1; /* -1 means default traffic class */
3168 opt
->ip6po_minmtu
= IP6PO_MINMTU_MCASTONLY
;
3169 opt
->ip6po_prefer_tempaddr
= IP6PO_TEMPADDR_SYSTEM
;
3173 ip6_pcbopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
**pktopt
,
3176 struct ip6_pktopts
*opt
;
3180 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
3184 ip6_initpktopts(opt
);
3188 return ip6_setpktopt(optname
, buf
, len
, opt
, 1, 0, uproto
);
3192 ip6_getpcbopt(struct ip6_pktopts
*pktopt
, int optname
, struct sockopt
*sopt
)
3194 void *optdata
= NULL
;
3196 struct ip6_ext
*ip6e
;
3197 struct in6_pktinfo null_pktinfo
;
3198 int deftclass
= 0, on
;
3199 int defminmtu
= IP6PO_MINMTU_MCASTONLY
;
3200 int defpreftemp
= IP6PO_TEMPADDR_SYSTEM
;
3205 if (pktopt
&& pktopt
->ip6po_pktinfo
) {
3206 optdata
= (void *)pktopt
->ip6po_pktinfo
;
3208 /* XXX: we don't have to do this every time... */
3209 bzero(&null_pktinfo
, sizeof(null_pktinfo
));
3210 optdata
= (void *)&null_pktinfo
;
3212 optdatalen
= sizeof(struct in6_pktinfo
);
3216 if (pktopt
&& pktopt
->ip6po_tclass
>= 0) {
3217 optdata
= (void *)&pktopt
->ip6po_tclass
;
3219 optdata
= (void *)&deftclass
;
3221 optdatalen
= sizeof(int);
3225 if (pktopt
&& pktopt
->ip6po_hbh
) {
3226 optdata
= (void *)pktopt
->ip6po_hbh
;
3227 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_hbh
;
3228 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3233 if (pktopt
&& pktopt
->ip6po_rthdr
) {
3234 optdata
= (void *)pktopt
->ip6po_rthdr
;
3235 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_rthdr
;
3236 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3240 case IPV6_RTHDRDSTOPTS
:
3241 if (pktopt
&& pktopt
->ip6po_dest1
) {
3242 optdata
= (void *)pktopt
->ip6po_dest1
;
3243 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest1
;
3244 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3249 if (pktopt
&& pktopt
->ip6po_dest2
) {
3250 optdata
= (void *)pktopt
->ip6po_dest2
;
3251 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest2
;
3252 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3257 if (pktopt
&& pktopt
->ip6po_nexthop
) {
3258 optdata
= (void *)pktopt
->ip6po_nexthop
;
3259 optdatalen
= pktopt
->ip6po_nexthop
->sa_len
;
3263 case IPV6_USE_MIN_MTU
:
3265 optdata
= (void *)&pktopt
->ip6po_minmtu
;
3267 optdata
= (void *)&defminmtu
;
3269 optdatalen
= sizeof(int);
3273 if (pktopt
&& ((pktopt
->ip6po_flags
) & IP6PO_DONTFRAG
)) {
3278 optdata
= (void *)&on
;
3279 optdatalen
= sizeof(on
);
3282 case IPV6_PREFER_TEMPADDR
:
3284 optdata
= (void *)&pktopt
->ip6po_prefer_tempaddr
;
3286 optdata
= (void *)&defpreftemp
;
3288 optdatalen
= sizeof(int);
3291 default: /* should not happen */
3293 panic("ip6_getpcbopt: unexpected option\n");
3298 return sooptcopyout(sopt
, optdata
, optdatalen
);
3302 ip6_clearpktopts(struct ip6_pktopts
*pktopt
, int optname
)
3304 if (pktopt
== NULL
) {
3308 if (optname
== -1 || optname
== IPV6_PKTINFO
) {
3309 if (pktopt
->ip6po_pktinfo
) {
3310 FREE(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
3312 pktopt
->ip6po_pktinfo
= NULL
;
3314 if (optname
== -1 || optname
== IPV6_HOPLIMIT
) {
3315 pktopt
->ip6po_hlim
= -1;
3317 if (optname
== -1 || optname
== IPV6_TCLASS
) {
3318 pktopt
->ip6po_tclass
= -1;
3320 if (optname
== -1 || optname
== IPV6_NEXTHOP
) {
3321 ROUTE_RELEASE(&pktopt
->ip6po_nextroute
);
3322 if (pktopt
->ip6po_nexthop
) {
3323 FREE(pktopt
->ip6po_nexthop
, M_IP6OPT
);
3325 pktopt
->ip6po_nexthop
= NULL
;
3327 if (optname
== -1 || optname
== IPV6_HOPOPTS
) {
3328 if (pktopt
->ip6po_hbh
) {
3329 FREE(pktopt
->ip6po_hbh
, M_IP6OPT
);
3331 pktopt
->ip6po_hbh
= NULL
;
3333 if (optname
== -1 || optname
== IPV6_RTHDRDSTOPTS
) {
3334 if (pktopt
->ip6po_dest1
) {
3335 FREE(pktopt
->ip6po_dest1
, M_IP6OPT
);
3337 pktopt
->ip6po_dest1
= NULL
;
3339 if (optname
== -1 || optname
== IPV6_RTHDR
) {
3340 if (pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
) {
3341 FREE(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
3343 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
3344 ROUTE_RELEASE(&pktopt
->ip6po_route
);
3346 if (optname
== -1 || optname
== IPV6_DSTOPTS
) {
3347 if (pktopt
->ip6po_dest2
) {
3348 FREE(pktopt
->ip6po_dest2
, M_IP6OPT
);
3350 pktopt
->ip6po_dest2
= NULL
;
3354 #define PKTOPT_EXTHDRCPY(type) do { \
3357 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3; \
3358 dst->type = _MALLOC(hlen, M_IP6OPT, canwait); \
3359 if (dst->type == NULL && canwait == M_NOWAIT) \
3361 bcopy(src->type, dst->type, hlen); \
3366 copypktopts(struct ip6_pktopts
*dst
, struct ip6_pktopts
*src
, int canwait
)
3368 if (dst
== NULL
|| src
== NULL
) {
3369 printf("copypktopts: invalid argument\n");
3373 dst
->ip6po_hlim
= src
->ip6po_hlim
;
3374 dst
->ip6po_tclass
= src
->ip6po_tclass
;
3375 dst
->ip6po_flags
= src
->ip6po_flags
;
3376 if (src
->ip6po_pktinfo
) {
3377 dst
->ip6po_pktinfo
= _MALLOC(sizeof(*dst
->ip6po_pktinfo
),
3379 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
) {
3382 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
3384 if (src
->ip6po_nexthop
) {
3385 dst
->ip6po_nexthop
= _MALLOC(src
->ip6po_nexthop
->sa_len
,
3387 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
) {
3390 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
3391 src
->ip6po_nexthop
->sa_len
);
3393 PKTOPT_EXTHDRCPY(ip6po_hbh
);
3394 PKTOPT_EXTHDRCPY(ip6po_dest1
);
3395 PKTOPT_EXTHDRCPY(ip6po_dest2
);
3396 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
3400 ip6_clearpktopts(dst
, -1);
3403 #undef PKTOPT_EXTHDRCPY
3405 struct ip6_pktopts
*
3406 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
3409 struct ip6_pktopts
*dst
;
3411 dst
= _MALLOC(sizeof(*dst
), M_IP6OPT
, canwait
);
3415 ip6_initpktopts(dst
);
3417 if ((error
= copypktopts(dst
, src
, canwait
)) != 0) {
3418 FREE(dst
, M_IP6OPT
);
3426 ip6_freepcbopts(struct ip6_pktopts
*pktopt
)
3428 if (pktopt
== NULL
) {
3432 ip6_clearpktopts(pktopt
, -1);
3434 FREE(pktopt
, M_IP6OPT
);
3438 ip6_moptions_init(void)
3440 PE_parse_boot_argn("ifa_debug", &im6o_debug
, sizeof(im6o_debug
));
3442 im6o_size
= (im6o_debug
== 0) ? sizeof(struct ip6_moptions
) :
3443 sizeof(struct ip6_moptions_dbg
);
3445 im6o_zone
= zinit(im6o_size
, IM6O_ZONE_MAX
* im6o_size
, 0,
3447 if (im6o_zone
== NULL
) {
3448 panic("%s: failed allocating %s", __func__
, IM6O_ZONE_NAME
);
3451 zone_change(im6o_zone
, Z_EXPAND
, TRUE
);
3455 im6o_addref(struct ip6_moptions
*im6o
, int locked
)
3460 IM6O_LOCK_ASSERT_HELD(im6o
);
3463 if (++im6o
->im6o_refcnt
== 0) {
3464 panic("%s: im6o %p wraparound refcnt\n", __func__
, im6o
);
3466 } else if (im6o
->im6o_trace
!= NULL
) {
3467 (*im6o
->im6o_trace
)(im6o
, TRUE
);
3476 im6o_remref(struct ip6_moptions
*im6o
)
3481 if (im6o
->im6o_refcnt
== 0) {
3482 panic("%s: im6o %p negative refcnt", __func__
, im6o
);
3484 } else if (im6o
->im6o_trace
!= NULL
) {
3485 (*im6o
->im6o_trace
)(im6o
, FALSE
);
3488 --im6o
->im6o_refcnt
;
3489 if (im6o
->im6o_refcnt
> 0) {
3494 for (i
= 0; i
< im6o
->im6o_num_memberships
; ++i
) {
3495 struct in6_mfilter
*imf
;
3497 imf
= im6o
->im6o_mfilters
? &im6o
->im6o_mfilters
[i
] : NULL
;
3502 (void) in6_mc_leave(im6o
->im6o_membership
[i
], imf
);
3508 IN6M_REMREF(im6o
->im6o_membership
[i
]);
3509 im6o
->im6o_membership
[i
] = NULL
;
3511 im6o
->im6o_num_memberships
= 0;
3512 if (im6o
->im6o_mfilters
!= NULL
) {
3513 FREE(im6o
->im6o_mfilters
, M_IN6MFILTER
);
3514 im6o
->im6o_mfilters
= NULL
;
3516 if (im6o
->im6o_membership
!= NULL
) {
3517 FREE(im6o
->im6o_membership
, M_IP6MOPTS
);
3518 im6o
->im6o_membership
= NULL
;
3522 lck_mtx_destroy(&im6o
->im6o_lock
, ifa_mtx_grp
);
3524 if (!(im6o
->im6o_debug
& IFD_ALLOC
)) {
3525 panic("%s: im6o %p cannot be freed", __func__
, im6o
);
3528 zfree(im6o_zone
, im6o
);
3532 im6o_trace(struct ip6_moptions
*im6o
, int refhold
)
3534 struct ip6_moptions_dbg
*im6o_dbg
= (struct ip6_moptions_dbg
*)im6o
;
3539 if (!(im6o
->im6o_debug
& IFD_DEBUG
)) {
3540 panic("%s: im6o %p has no debug structure", __func__
, im6o
);
3544 cnt
= &im6o_dbg
->im6o_refhold_cnt
;
3545 tr
= im6o_dbg
->im6o_refhold
;
3547 cnt
= &im6o_dbg
->im6o_refrele_cnt
;
3548 tr
= im6o_dbg
->im6o_refrele
;
3551 idx
= atomic_add_16_ov(cnt
, 1) % IM6O_TRACE_HIST_SIZE
;
3552 ctrace_record(&tr
[idx
]);
3555 struct ip6_moptions
*
3556 ip6_allocmoptions(int how
)
3558 struct ip6_moptions
*im6o
;
3560 im6o
= (how
== M_WAITOK
) ?
3561 zalloc(im6o_zone
) : zalloc_noblock(im6o_zone
);
3563 bzero(im6o
, im6o_size
);
3564 lck_mtx_init(&im6o
->im6o_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3565 im6o
->im6o_debug
|= IFD_ALLOC
;
3566 if (im6o_debug
!= 0) {
3567 im6o
->im6o_debug
|= IFD_DEBUG
;
3568 im6o
->im6o_trace
= im6o_trace
;
3577 * Set IPv6 outgoing packet options based on advanced API.
3580 ip6_setpktopts(struct mbuf
*control
, struct ip6_pktopts
*opt
,
3581 struct ip6_pktopts
*stickyopt
, int uproto
)
3583 struct cmsghdr
*cm
= NULL
;
3585 if (control
== NULL
|| opt
== NULL
) {
3589 ip6_initpktopts(opt
);
3594 * If stickyopt is provided, make a local copy of the options
3595 * for this particular packet, then override them by ancillary
3597 * XXX: copypktopts() does not copy the cached route to a next
3598 * hop (if any). This is not very good in terms of efficiency,
3599 * but we can allow this since this option should be rarely
3602 if ((error
= copypktopts(opt
, stickyopt
, M_NOWAIT
)) != 0) {
3608 * XXX: Currently, we assume all the optional information is stored
3611 if (control
->m_next
) {
3615 if (control
->m_len
< CMSG_LEN(0)) {
3619 for (cm
= M_FIRST_CMSGHDR(control
);
3620 is_cmsg_valid(control
, cm
);
3621 cm
= M_NXT_CMSGHDR(control
, cm
)) {
3624 if (cm
->cmsg_level
!= IPPROTO_IPV6
) {
3628 error
= ip6_setpktopt(cm
->cmsg_type
, CMSG_DATA(cm
),
3629 cm
->cmsg_len
- CMSG_LEN(0), opt
, 0, 1, uproto
);
3638 * Set a particular packet option, as a sticky option or an ancillary data
3639 * item. "len" can be 0 only when it's a sticky option.
3640 * We have 4 cases of combination of "sticky" and "cmsg":
3641 * "sticky=0, cmsg=0": impossible
3642 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3643 * "sticky=1, cmsg=0": RFC3542 socket option
3644 * "sticky=1, cmsg=1": RFC2292 socket option
3647 ip6_setpktopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
*opt
,
3648 int sticky
, int cmsg
, int uproto
)
3650 int minmtupolicy
, preftemp
;
3652 boolean_t capture_exthdrstat_out
= FALSE
;
3654 if (!sticky
&& !cmsg
) {
3656 printf("ip6_setpktopt: impossible case\n");
3662 * Caller must have ensured that the buffer is at least
3663 * aligned on 32-bit boundary.
3665 VERIFY(IS_P2ALIGNED(buf
, sizeof(u_int32_t
)));
3668 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3669 * not be specified in the context of RFC3542. Conversely,
3670 * RFC3542 types should not be specified in the context of RFC2292.
3674 case IPV6_2292PKTINFO
:
3675 case IPV6_2292HOPLIMIT
:
3676 case IPV6_2292NEXTHOP
:
3677 case IPV6_2292HOPOPTS
:
3678 case IPV6_2292DSTOPTS
:
3679 case IPV6_2292RTHDR
:
3680 case IPV6_2292PKTOPTIONS
:
3684 if (sticky
&& cmsg
) {
3691 case IPV6_RTHDRDSTOPTS
:
3693 case IPV6_USE_MIN_MTU
:
3696 case IPV6_PREFER_TEMPADDR
: /* XXX: not an RFC3542 option */
3702 case IPV6_2292PKTINFO
:
3703 case IPV6_PKTINFO
: {
3704 struct ifnet
*ifp
= NULL
;
3705 struct in6_pktinfo
*pktinfo
;
3707 if (len
!= sizeof(struct in6_pktinfo
)) {
3711 pktinfo
= (struct in6_pktinfo
*)(void *)buf
;
3714 * An application can clear any sticky IPV6_PKTINFO option by
3715 * doing a "regular" setsockopt with ipi6_addr being
3716 * in6addr_any and ipi6_ifindex being zero.
3717 * [RFC 3542, Section 6]
3719 if (optname
== IPV6_PKTINFO
&& opt
->ip6po_pktinfo
&&
3720 pktinfo
->ipi6_ifindex
== 0 &&
3721 IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3722 ip6_clearpktopts(opt
, optname
);
3726 if (uproto
== IPPROTO_TCP
&& optname
== IPV6_PKTINFO
&&
3727 sticky
&& !IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3731 /* validate the interface index if specified. */
3732 ifnet_head_lock_shared();
3734 if (pktinfo
->ipi6_ifindex
> if_index
) {
3739 if (pktinfo
->ipi6_ifindex
) {
3740 ifp
= ifindex2ifnet
[pktinfo
->ipi6_ifindex
];
3750 * We store the address anyway, and let in6_selectsrc()
3751 * validate the specified address. This is because ipi6_addr
3752 * may not have enough information about its scope zone, and
3753 * we may need additional information (such as outgoing
3754 * interface or the scope zone of a destination address) to
3755 * disambiguate the scope.
3756 * XXX: the delay of the validation may confuse the
3757 * application when it is used as a sticky option.
3759 if (opt
->ip6po_pktinfo
== NULL
) {
3760 opt
->ip6po_pktinfo
= _MALLOC(sizeof(*pktinfo
),
3761 M_IP6OPT
, M_NOWAIT
);
3762 if (opt
->ip6po_pktinfo
== NULL
) {
3766 bcopy(pktinfo
, opt
->ip6po_pktinfo
, sizeof(*pktinfo
));
3770 case IPV6_2292HOPLIMIT
:
3771 case IPV6_HOPLIMIT
: {
3775 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3776 * to simplify the ordering among hoplimit options.
3778 if (optname
== IPV6_HOPLIMIT
&& sticky
) {
3782 if (len
!= sizeof(int)) {
3785 hlimp
= (int *)(void *)buf
;
3786 if (*hlimp
< -1 || *hlimp
> 255) {
3790 opt
->ip6po_hlim
= *hlimp
;
3797 if (len
!= sizeof(int)) {
3800 tclass
= *(int *)(void *)buf
;
3801 if (tclass
< -1 || tclass
> 255) {
3805 opt
->ip6po_tclass
= tclass
;
3809 case IPV6_2292NEXTHOP
:
3811 error
= suser(kauth_cred_get(), 0);
3816 if (len
== 0) { /* just remove the option */
3817 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3821 /* check if cmsg_len is large enough for sa_len */
3822 if (len
< sizeof(struct sockaddr
) || len
< *buf
) {
3826 switch (SA(buf
)->sa_family
) {
3828 struct sockaddr_in6
*sa6
= SIN6(buf
);
3830 if (sa6
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
3834 if (IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
) ||
3835 IN6_IS_ADDR_MULTICAST(&sa6
->sin6_addr
)) {
3838 if ((error
= sa6_embedscope(sa6
, ip6_use_defzone
))
3844 case AF_LINK
: /* should eventually be supported */
3846 return EAFNOSUPPORT
;
3849 /* turn off the previous option, then set the new option. */
3850 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3851 opt
->ip6po_nexthop
= _MALLOC(*buf
, M_IP6OPT
, M_NOWAIT
);
3852 if (opt
->ip6po_nexthop
== NULL
) {
3855 bcopy(buf
, opt
->ip6po_nexthop
, *buf
);
3858 case IPV6_2292HOPOPTS
:
3859 case IPV6_HOPOPTS
: {
3860 struct ip6_hbh
*hbh
;
3864 * XXX: We don't allow a non-privileged user to set ANY HbH
3865 * options, since per-option restriction has too much
3868 error
= suser(kauth_cred_get(), 0);
3874 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3875 break; /* just remove the option */
3878 /* message length validation */
3879 if (len
< sizeof(struct ip6_hbh
)) {
3882 hbh
= (struct ip6_hbh
*)(void *)buf
;
3883 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
3884 if (len
!= hbhlen
) {
3888 /* turn off the previous option, then set the new option. */
3889 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3890 opt
->ip6po_hbh
= _MALLOC(hbhlen
, M_IP6OPT
, M_NOWAIT
);
3891 if (opt
->ip6po_hbh
== NULL
) {
3894 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
3895 capture_exthdrstat_out
= TRUE
;
3899 case IPV6_2292DSTOPTS
:
3901 case IPV6_RTHDRDSTOPTS
: {
3902 struct ip6_dest
*dest
, **newdest
= NULL
;
3905 error
= suser(kauth_cred_get(), 0);
3911 ip6_clearpktopts(opt
, optname
);
3912 break; /* just remove the option */
3915 /* message length validation */
3916 if (len
< sizeof(struct ip6_dest
)) {
3919 dest
= (struct ip6_dest
*)(void *)buf
;
3920 destlen
= (dest
->ip6d_len
+ 1) << 3;
3921 if (len
!= destlen
) {
3926 * Determine the position that the destination options header
3927 * should be inserted; before or after the routing header.
3930 case IPV6_2292DSTOPTS
:
3932 * The old advacned API is ambiguous on this point.
3933 * Our approach is to determine the position based
3934 * according to the existence of a routing header.
3935 * Note, however, that this depends on the order of the
3936 * extension headers in the ancillary data; the 1st
3937 * part of the destination options header must appear
3938 * before the routing header in the ancillary data,
3940 * RFC3542 solved the ambiguity by introducing
3941 * separate ancillary data or option types.
3943 if (opt
->ip6po_rthdr
== NULL
) {
3944 newdest
= &opt
->ip6po_dest1
;
3946 newdest
= &opt
->ip6po_dest2
;
3949 case IPV6_RTHDRDSTOPTS
:
3950 newdest
= &opt
->ip6po_dest1
;
3953 newdest
= &opt
->ip6po_dest2
;
3957 /* turn off the previous option, then set the new option. */
3958 ip6_clearpktopts(opt
, optname
);
3959 *newdest
= _MALLOC(destlen
, M_IP6OPT
, M_NOWAIT
);
3960 if (*newdest
== NULL
) {
3963 bcopy(dest
, *newdest
, destlen
);
3964 capture_exthdrstat_out
= TRUE
;
3968 case IPV6_2292RTHDR
:
3970 struct ip6_rthdr
*rth
;
3974 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3975 break; /* just remove the option */
3978 /* message length validation */
3979 if (len
< sizeof(struct ip6_rthdr
)) {
3982 rth
= (struct ip6_rthdr
*)(void *)buf
;
3983 rthlen
= (rth
->ip6r_len
+ 1) << 3;
3984 if (len
!= rthlen
) {
3988 switch (rth
->ip6r_type
) {
3989 case IPV6_RTHDR_TYPE_0
:
3990 if (rth
->ip6r_len
== 0) { /* must contain one addr */
3993 if (rth
->ip6r_len
% 2) { /* length must be even */
3996 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
) {
4001 return EINVAL
; /* not supported */
4004 /* turn off the previous option */
4005 ip6_clearpktopts(opt
, IPV6_RTHDR
);
4006 opt
->ip6po_rthdr
= _MALLOC(rthlen
, M_IP6OPT
, M_NOWAIT
);
4007 if (opt
->ip6po_rthdr
== NULL
) {
4010 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
4011 capture_exthdrstat_out
= TRUE
;
4015 case IPV6_USE_MIN_MTU
:
4016 if (len
!= sizeof(int)) {
4019 minmtupolicy
= *(int *)(void *)buf
;
4020 if (minmtupolicy
!= IP6PO_MINMTU_MCASTONLY
&&
4021 minmtupolicy
!= IP6PO_MINMTU_DISABLE
&&
4022 minmtupolicy
!= IP6PO_MINMTU_ALL
) {
4025 opt
->ip6po_minmtu
= minmtupolicy
;
4029 if (len
!= sizeof(int)) {
4033 if (uproto
== IPPROTO_TCP
|| *(int *)(void *)buf
== 0) {
4035 * we ignore this option for TCP sockets.
4036 * (RFC3542 leaves this case unspecified.)
4038 opt
->ip6po_flags
&= ~IP6PO_DONTFRAG
;
4040 opt
->ip6po_flags
|= IP6PO_DONTFRAG
;
4044 case IPV6_PREFER_TEMPADDR
:
4045 if (len
!= sizeof(int)) {
4048 preftemp
= *(int *)(void *)buf
;
4049 if (preftemp
!= IP6PO_TEMPADDR_SYSTEM
&&
4050 preftemp
!= IP6PO_TEMPADDR_NOTPREFER
&&
4051 preftemp
!= IP6PO_TEMPADDR_PREFER
) {
4054 opt
->ip6po_prefer_tempaddr
= preftemp
;
4059 } /* end of switch */
4061 if (capture_exthdrstat_out
) {
4062 if (uproto
== IPPROTO_TCP
) {
4063 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_stream_exthdr_out
);
4064 } else if (uproto
== IPPROTO_UDP
) {
4065 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_sock_inet6_dgram_exthdr_out
);
4073 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
4074 * packet to the input queue of a specified interface. Note that this
4075 * calls the output routine of the loopback "driver", but with an interface
4076 * pointer that might NOT be &loif -- easier than replicating that code here.
4079 ip6_mloopback(struct ifnet
*srcifp
, struct ifnet
*origifp
, struct mbuf
*m
,
4080 struct sockaddr_in6
*dst
, uint32_t optlen
, int32_t nxt0
)
4083 struct ip6_hdr
*ip6
;
4084 struct in6_addr src
;
4086 if (lo_ifp
== NULL
) {
4091 * Copy the packet header as it's needed for the checksum.
4092 * Make sure to deep-copy IPv6 header portion in case the data
4093 * is in an mbuf cluster, so that we can safely override the IPv6
4094 * header portion later.
4096 copym
= m_copym_mode(m
, 0, M_COPYALL
, M_DONTWAIT
, M_COPYM_COPY_HDR
);
4097 if (copym
!= NULL
&& ((copym
->m_flags
& M_EXT
) ||
4098 copym
->m_len
< sizeof(struct ip6_hdr
))) {
4099 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
4102 if (copym
== NULL
) {
4106 ip6
= mtod(copym
, struct ip6_hdr
*);
4109 * clear embedded scope identifiers if necessary.
4110 * in6_clearscope will touch the addresses only when necessary.
4112 in6_clearscope(&ip6
->ip6_src
);
4113 in6_clearscope(&ip6
->ip6_dst
);
4115 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
) {
4116 in6_delayed_cksum_offset(copym
, 0, optlen
, nxt0
);
4120 * Stuff the 'real' ifp into the pkthdr, to be used in matching
4121 * in ip6_input(); we need the loopback ifp/dl_tag passed as args
4122 * to make the loopback driver compliant with the data link
4125 copym
->m_pkthdr
.rcvif
= origifp
;
4128 * Also record the source interface (which owns the source address).
4129 * This is basically a stripped down version of ifa_foraddr6().
4131 if (srcifp
== NULL
) {
4132 struct in6_ifaddr
*ia
;
4134 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
4135 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= ia
->ia_next
) {
4136 IFA_LOCK_SPIN(&ia
->ia_ifa
);
4137 /* compare against src addr with embedded scope */
4138 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, &src
)) {
4139 srcifp
= ia
->ia_ifp
;
4140 IFA_UNLOCK(&ia
->ia_ifa
);
4143 IFA_UNLOCK(&ia
->ia_ifa
);
4145 lck_rw_done(&in6_ifaddr_rwlock
);
4147 if (srcifp
!= NULL
) {
4148 ip6_setsrcifaddr_info(copym
, srcifp
->if_index
, NULL
);
4150 ip6_setdstifaddr_info(copym
, origifp
->if_index
, NULL
);
4152 dlil_output(lo_ifp
, PF_INET6
, copym
, NULL
, SA(dst
), 0, NULL
);
4156 * Chop IPv6 header off from the payload.
4159 ip6_splithdr(struct mbuf
*m
, struct ip6_exthdrs
*exthdrs
)
4162 struct ip6_hdr
*ip6
;
4164 ip6
= mtod(m
, struct ip6_hdr
*);
4165 if (m
->m_len
> sizeof(*ip6
)) {
4166 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
4171 M_COPY_PKTHDR(mh
, m
);
4172 MH_ALIGN(mh
, sizeof(*ip6
));
4173 m
->m_flags
&= ~M_PKTHDR
;
4174 m
->m_len
-= sizeof(*ip6
);
4175 m
->m_data
+= sizeof(*ip6
);
4178 m
->m_len
= sizeof(*ip6
);
4179 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof(*ip6
));
4181 exthdrs
->ip6e_ip6
= m
;
4186 ip6_output_checksum(struct ifnet
*ifp
, uint32_t mtu
, struct mbuf
*m
,
4187 int nxt0
, uint32_t tlen
, uint32_t optlen
)
4189 uint32_t sw_csum
, hwcap
= ifp
->if_hwassist
;
4190 int tso
= TSO_IPV6_OK(ifp
, m
);
4193 /* do all in software; checksum offload is disabled */
4194 sw_csum
= CSUM_DELAY_IPV6_DATA
& m
->m_pkthdr
.csum_flags
;
4196 /* do in software what the hardware cannot */
4197 sw_csum
= m
->m_pkthdr
.csum_flags
&
4198 ~IF_HWASSIST_CSUM_FLAGS(hwcap
);
4202 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
4203 m
->m_pkthdr
.csum_flags
);
4204 } else if (!(sw_csum
& CSUM_DELAY_IPV6_DATA
) &&
4205 (hwcap
& CSUM_PARTIAL
)) {
4207 * Partial checksum offload, ere), if no extension headers,
4208 * and TCP only (no UDP support, as the hardware may not be
4209 * able to convert +0 to -0 (0xffff) per RFC1122 4.1.3.4.
4210 * unless the interface supports "invert zero" capability.)
4212 if (hwcksum_tx
&& !tso
&&
4213 ((m
->m_pkthdr
.csum_flags
& CSUM_TCPIPV6
) ||
4214 ((hwcap
& CSUM_ZERO_INVERT
) &&
4215 (m
->m_pkthdr
.csum_flags
& CSUM_ZERO_INVERT
))) &&
4217 uint16_t start
= sizeof(struct ip6_hdr
);
4219 m
->m_pkthdr
.csum_data
& 0xffff;
4220 m
->m_pkthdr
.csum_flags
|=
4221 (CSUM_DATA_VALID
| CSUM_PARTIAL
);
4222 m
->m_pkthdr
.csum_tx_stuff
= (ulpoff
+ start
);
4223 m
->m_pkthdr
.csum_tx_start
= start
;
4226 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
4227 m
->m_pkthdr
.csum_flags
);
4231 if (sw_csum
& CSUM_DELAY_IPV6_DATA
) {
4232 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
4233 sw_csum
&= ~CSUM_DELAY_IPV6_DATA
;
4238 * Drop off bits that aren't supported by hardware;
4239 * also make sure to preserve non-checksum related bits.
4241 m
->m_pkthdr
.csum_flags
=
4242 ((m
->m_pkthdr
.csum_flags
&
4243 (IF_HWASSIST_CSUM_FLAGS(hwcap
) | CSUM_DATA_VALID
)) |
4244 (m
->m_pkthdr
.csum_flags
& ~IF_HWASSIST_CSUM_MASK
));
4246 /* drop all bits; checksum offload is disabled */
4247 m
->m_pkthdr
.csum_flags
= 0;
4252 * Compute IPv6 extension header length.
4255 ip6_optlen(struct in6pcb
*in6p
)
4259 if (!in6p
->in6p_outputopts
) {
4265 (((struct ip6_ext *)(x)) ? \
4266 (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
4268 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
4269 if (in6p
->in6p_outputopts
->ip6po_rthdr
) {
4270 /* dest1 is valid with rthdr only */
4271 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
4273 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
4274 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);
4280 sysctl_reset_ip6_output_stats SYSCTL_HANDLER_ARGS
4282 #pragma unused(arg1, arg2)
4285 i
= ip6_output_measure
;
4286 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
4287 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
4291 if (i
< 0 || i
> 1) {
4295 if (ip6_output_measure
!= i
&& i
== 1) {
4296 net_perf_initialize(&net_perf
, ip6_output_measure_bins
);
4298 ip6_output_measure
= i
;
4304 sysctl_ip6_output_measure_bins SYSCTL_HANDLER_ARGS
4306 #pragma unused(arg1, arg2)
4310 i
= ip6_output_measure_bins
;
4311 error
= sysctl_handle_quad(oidp
, &i
, 0, req
);
4312 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
4316 if (!net_perf_validate_bins(i
)) {
4320 ip6_output_measure_bins
= i
;
4326 sysctl_ip6_output_getperf SYSCTL_HANDLER_ARGS
4328 #pragma unused(oidp, arg1, arg2)
4329 if (req
->oldptr
== USER_ADDR_NULL
) {
4330 req
->oldlen
= (size_t)sizeof(struct ipstat
);
4333 return SYSCTL_OUT(req
, &net_perf
, MIN(sizeof(net_perf
), req
->oldlen
));