2 * Copyright (c) 2000-2016 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_osdep.h>
122 #include <net/net_perf.h>
124 #include <netinet/in.h>
125 #include <netinet/in_var.h>
126 #include <netinet/ip_var.h>
127 #include <netinet6/in6_var.h>
128 #include <netinet/ip6.h>
129 #include <netinet/kpi_ipfilter_var.h>
130 #include <netinet/in_tclass.h>
132 #include <netinet6/ip6protosw.h>
133 #include <netinet/icmp6.h>
134 #include <netinet6/ip6_var.h>
135 #include <netinet/in_pcb.h>
136 #include <netinet6/nd6.h>
137 #include <netinet6/scope6_var.h>
139 #include <netinet6/ipsec.h>
140 #include <netinet6/ipsec6.h>
141 #include <netkey/key.h>
142 extern int ipsec_bypass
;
146 #include <net/necp.h>
150 #include <security/mac.h>
151 #endif /* CONFIG_MACF_NET */
154 #include <netinet/ip_fw.h>
155 #include <netinet/ip_dummynet.h>
156 #endif /* DUMMYNET */
159 #include <net/pfvar.h>
162 static int sysctl_reset_ip6_output_stats SYSCTL_HANDLER_ARGS
;
163 static int sysctl_ip6_output_measure_bins SYSCTL_HANDLER_ARGS
;
164 static int sysctl_ip6_output_getperf SYSCTL_HANDLER_ARGS
;
165 static int ip6_copyexthdr(struct mbuf
**, caddr_t
, int);
166 static void ip6_out_cksum_stats(int, u_int32_t
);
167 static int ip6_insert_jumboopt(struct ip6_exthdrs
*, u_int32_t
);
168 static int ip6_insertfraghdr(struct mbuf
*, struct mbuf
*, int,
170 static int ip6_getpmtu(struct route_in6
*, struct route_in6
*,
171 struct ifnet
*, struct in6_addr
*, u_int32_t
*, boolean_t
*);
172 static int ip6_pcbopts(struct ip6_pktopts
**, struct mbuf
*, struct socket
*,
173 struct sockopt
*sopt
);
174 static int ip6_pcbopt(int, u_char
*, int, struct ip6_pktopts
**, int);
175 static int ip6_getpcbopt(struct ip6_pktopts
*, int, struct sockopt
*);
176 static int copypktopts(struct ip6_pktopts
*, struct ip6_pktopts
*, int);
177 static void im6o_trace(struct ip6_moptions
*, int);
178 static int ip6_setpktopt(int, u_char
*, int, struct ip6_pktopts
*, int,
180 static int ip6_splithdr(struct mbuf
*, struct ip6_exthdrs
*);
181 static void ip6_output_checksum(struct ifnet
*, uint32_t, struct mbuf
*,
182 int, uint32_t, uint32_t);
183 extern int udp_ctloutput(struct socket
*, struct sockopt
*);
184 static int ip6_do_fragmentation(struct mbuf
**morig
,
185 uint32_t optlen
, struct ifnet
*ifp
, uint32_t unfragpartlen
,
186 struct ip6_hdr
*ip6
, struct ip6_exthdrs
*exthdrsp
, uint32_t mtu
,
188 static int ip6_fragment_packet(struct mbuf
**m
,
189 struct ip6_pktopts
*opt
, struct ip6_exthdrs
*exthdrsp
, struct ifnet
*ifp
,
190 uint32_t mtu
, boolean_t alwaysfrag
, uint32_t unfragpartlen
,
191 struct route_in6
*ro_pmtu
, int nxt0
, uint32_t optlen
);
193 SYSCTL_DECL(_net_inet6_ip6
);
195 static int ip6_output_measure
= 0;
196 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, output_perf
,
197 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
198 &ip6_output_measure
, 0, sysctl_reset_ip6_output_stats
, "I", "Do time measurement");
200 static uint64_t ip6_output_measure_bins
= 0;
201 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, output_perf_bins
,
202 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_output_measure_bins
, 0,
203 sysctl_ip6_output_measure_bins
, "I",
204 "bins for chaining performance data histogram");
206 static net_perf_t net_perf
;
207 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, output_perf_data
,
208 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
209 0, 0, sysctl_ip6_output_getperf
, "S,net_perf",
210 "IP6 output performance data (struct net_perf, net/net_perf.h)");
212 #define IM6O_TRACE_HIST_SIZE 32 /* size of trace history */
215 __private_extern__
unsigned int im6o_trace_hist_size
= IM6O_TRACE_HIST_SIZE
;
217 struct ip6_moptions_dbg
{
218 struct ip6_moptions im6o
; /* ip6_moptions */
219 u_int16_t im6o_refhold_cnt
; /* # of IM6O_ADDREF */
220 u_int16_t im6o_refrele_cnt
; /* # of IM6O_REMREF */
222 * Alloc and free callers.
227 * Circular lists of IM6O_ADDREF and IM6O_REMREF callers.
229 ctrace_t im6o_refhold
[IM6O_TRACE_HIST_SIZE
];
230 ctrace_t im6o_refrele
[IM6O_TRACE_HIST_SIZE
];
234 static unsigned int im6o_debug
= 1; /* debugging (enabled) */
236 static unsigned int im6o_debug
; /* debugging (disabled) */
239 static unsigned int im6o_size
; /* size of zone element */
240 static struct zone
*im6o_zone
; /* zone for ip6_moptions */
242 #define IM6O_ZONE_MAX 64 /* maximum elements in zone */
243 #define IM6O_ZONE_NAME "ip6_moptions" /* zone name */
246 * ip6_output() calls ip6_output_list() to do the work
249 ip6_output(struct mbuf
*m0
, struct ip6_pktopts
*opt
,
250 struct route_in6
*ro
, int flags
, struct ip6_moptions
*im6o
,
251 struct ifnet
**ifpp
, struct ip6_out_args
*ip6oa
)
253 return ip6_output_list(m0
, 0, opt
, ro
, flags
, im6o
, ifpp
, ip6oa
);
257 * IP6 output. Each packet in mbuf chain m contains a skeletal IP6
258 * header (with pri, len, nxt, hlim, src, dst).
259 * This function may modify ver and hlim only.
260 * The mbuf chain containing the packet will be freed.
261 * The mbuf opt, if present, will not be freed.
263 * If ro is non-NULL and has valid ro->ro_rt, route lookup would be
264 * skipped and ro->ro_rt would be used. Otherwise the result of route
265 * lookup is stored in ro->ro_rt.
267 * type of "mtu": rt_rmx.rmx_mtu is u_int32_t, ifnet.ifr_mtu is int, and
268 * nd_ifinfo.linkmtu is u_int32_t. so we use u_int32_t to hold largest one,
269 * which is rt_rmx.rmx_mtu.
272 ip6_output_list(struct mbuf
*m0
, int packetchain
, struct ip6_pktopts
*opt
,
273 struct route_in6
*ro
, int flags
, struct ip6_moptions
*im6o
,
274 struct ifnet
**ifpp
, struct ip6_out_args
*ip6oa
)
278 struct ifnet
*ifp
= NULL
, *origifp
= NULL
; /* refcnt'd */
279 struct ifnet
**ifpp_save
= ifpp
;
280 struct mbuf
*m
, *mprev
;
281 struct mbuf
*sendchain
= NULL
, *sendchain_last
= NULL
;
282 struct mbuf
*inputchain
= NULL
;
284 struct route_in6
*ro_pmtu
= NULL
;
285 struct rtentry
*rt
= NULL
;
286 struct sockaddr_in6
*dst
, src_sa
, dst_sa
;
288 struct in6_ifaddr
*ia
= NULL
, *src_ia
= NULL
;
290 boolean_t alwaysfrag
= FALSE
;
291 u_int32_t optlen
= 0, plen
= 0, unfragpartlen
= 0;
292 struct ip6_rthdr
*rh
;
293 struct in6_addr finaldst
;
294 ipfilter_t inject_filter_ref
;
295 struct ipf_pktopts
*ippo
= NULL
;
296 struct flowadv
*adv
= NULL
;
298 uint32_t packets_processed
= 0;
299 struct timeval start_tv
;
302 struct ip6_out_args saved_ip6oa
;
303 struct sockaddr_in6 dst_buf
;
304 #endif /* DUMMYNET */
306 struct socket
*so
= NULL
;
307 struct secpolicy
*sp
= NULL
;
308 struct route_in6
*ipsec_saved_route
= NULL
;
309 boolean_t needipsectun
= FALSE
;
312 necp_kernel_policy_result necp_result
= 0;
313 necp_kernel_policy_result_parameter necp_result_parameter
;
314 necp_kernel_policy_id necp_matched_policy_id
= 0;
317 struct ipf_pktopts ipf_pktopts
;
318 struct ip6_exthdrs exthdrs
;
319 struct route_in6 ip6route
;
321 struct ipsec_output_state ipsec_state
;
324 struct route_in6 necp_route
;
327 struct route_in6 saved_route
;
328 struct route_in6 saved_ro_pmtu
;
329 struct ip_fw_args args
;
330 #endif /* DUMMYNET */
332 #define ipf_pktopts ip6obz.ipf_pktopts
333 #define exthdrs ip6obz.exthdrs
334 #define ip6route ip6obz.ip6route
335 #define ipsec_state ip6obz.ipsec_state
336 #define necp_route ip6obz.necp_route
337 #define saved_route ip6obz.saved_route
338 #define saved_ro_pmtu ip6obz.saved_ro_pmtu
339 #define args ip6obz.args
342 boolean_t select_srcif
: 1;
343 boolean_t hdrsplit
: 1;
344 boolean_t route_selected
: 1;
345 boolean_t dontfrag
: 1;
347 boolean_t needipsec
: 1;
348 boolean_t noipsec
: 1;
352 } ip6obf
= { .raw
= 0 };
354 if (ip6_output_measure
)
355 net_perf_start_time(&net_perf
, &start_tv
);
357 VERIFY(m0
->m_flags
& M_PKTHDR
);
359 /* zero out {saved_route, saved_ro_pmtu, ip6route, exthdrs, args} */
360 bzero(&ip6obz
, sizeof (ip6obz
));
363 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
);
386 flags
= dn_tag
->dn_flags
;
387 if (dn_tag
->dn_flags
& IPV6_OUTARGS
) {
388 saved_ip6oa
= dn_tag
->dn_ip6oa
;
389 ip6oa
= &saved_ip6oa
;
392 saved_route
= dn_tag
->dn_ro6
;
394 saved_ro_pmtu
= dn_tag
->dn_ro6_pmtu
;
395 ro_pmtu
= &saved_ro_pmtu
;
396 origifp
= dn_tag
->dn_origifp
;
398 ifnet_reference(origifp
);
399 mtu
= dn_tag
->dn_mtu
;
400 alwaysfrag
= (dn_tag
->dn_alwaysfrag
!= 0);
401 unfragpartlen
= dn_tag
->dn_unfragpartlen
;
403 bcopy(&dn_tag
->dn_exthdrs
, &exthdrs
, sizeof (exthdrs
));
405 m_tag_delete(m0
, tag
);
409 #endif /* DUMMYNET */
414 if (ipsec_bypass
== 0) {
415 so
= ipsec_getsocket(m
);
417 (void) ipsec_setsocket(m
, NULL
);
419 /* If packet is bound to an interface, check bound policies */
420 if ((flags
& IPV6_OUTARGS
) &&
421 (ip6oa
->ip6oa_flags
& IP6OAF_BOUND_IF
) &&
422 ip6oa
->ip6oa_boundif
!= IFSCOPE_NONE
) {
423 /* ip6obf.noipsec is a bitfield, use temp integer */
426 if (ipsec6_getpolicybyinterface(m
, IPSEC_DIR_OUTBOUND
,
427 flags
, ip6oa
, &noipsec
, &sp
) != 0)
430 ip6obf
.noipsec
= (noipsec
!= 0);
437 if (flags
& IPV6_OUTARGS
) {
439 * In the forwarding case, only the ifscope value is used,
440 * as source interface selection doesn't take place.
442 if ((ip6obf
.select_srcif
= (!(flags
& (IPV6_FORWARDING
|
443 IPV6_UNSPECSRC
| IPV6_FLAG_NOSRCIFSEL
)) &&
444 (ip6oa
->ip6oa_flags
& IP6OAF_SELECT_SRCIF
))))
445 ipf_pktopts
.ippo_flags
|= IPPOF_SELECT_SRCIF
;
447 if ((ip6oa
->ip6oa_flags
& IP6OAF_BOUND_IF
) &&
448 ip6oa
->ip6oa_boundif
!= IFSCOPE_NONE
) {
449 ipf_pktopts
.ippo_flags
|= (IPPOF_BOUND_IF
|
450 (ip6oa
->ip6oa_boundif
<< IPPOF_SHIFT_IFSCOPE
));
453 if (ip6oa
->ip6oa_flags
& IP6OAF_BOUND_SRCADDR
)
454 ipf_pktopts
.ippo_flags
|= IPPOF_BOUND_SRCADDR
;
456 ip6obf
.select_srcif
= FALSE
;
457 if (flags
& IPV6_OUTARGS
) {
458 ip6oa
->ip6oa_boundif
= IFSCOPE_NONE
;
459 ip6oa
->ip6oa_flags
&= ~(IP6OAF_SELECT_SRCIF
|
460 IP6OAF_BOUND_IF
| IP6OAF_BOUND_SRCADDR
);
464 if (flags
& IPV6_OUTARGS
) {
465 if (ip6oa
->ip6oa_flags
& IP6OAF_NO_CELLULAR
)
466 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFT_CELLULAR
;
467 if (ip6oa
->ip6oa_flags
& IP6OAF_NO_EXPENSIVE
)
468 ipf_pktopts
.ippo_flags
|= IPPOF_NO_IFF_EXPENSIVE
;
469 adv
= &ip6oa
->ip6oa_flowadv
;
470 adv
->code
= FADV_SUCCESS
;
471 ip6oa
->ip6oa_retflags
= 0;
475 * Clear out ifpp to be filled in after determining route. ifpp_save is
476 * used to keep old value to release reference properly and dtrace
477 * ipsec tunnel traffic properly.
479 if (ifpp
!= NULL
&& *ifpp
!= NULL
)
483 if (args
.fwa_pf_rule
) {
484 ip6
= mtod(m
, struct ip6_hdr
*);
485 VERIFY(ro
!= NULL
); /* ro == saved_route */
488 #endif /* DUMMYNET */
492 * Since all packets are assumed to come from same socket, necp lookup
493 * only needs to happen once per function entry.
495 necp_matched_policy_id
= necp_ip6_output_find_policy_match(m
, flags
,
496 (flags
& IPV6_OUTARGS
) ? ip6oa
: NULL
, &necp_result
,
497 &necp_result_parameter
);
501 * If a chain was passed in, prepare for ther first iteration. For all
502 * other iterations, this work will be done at evaluateloop: label.
506 * Remove m from the chain during processing to avoid
507 * accidental frees on entire list.
509 inputchain
= m
->m_nextpkt
;
515 m
->m_pkthdr
.pkt_flags
&= ~(PKTF_LOOP
|PKTF_IFAINFO
);
516 ip6
= mtod(m
, struct ip6_hdr
*);
518 finaldst
= ip6
->ip6_dst
;
519 ip6obf
.hdrsplit
= FALSE
;
522 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
))
523 inject_filter_ref
= ipf_get_inject_filter(m
);
525 inject_filter_ref
= NULL
;
527 #define MAKE_EXTHDR(hp, mp) do { \
529 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
530 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
531 ((eh)->ip6e_len + 1) << 3); \
538 /* Hop-by-Hop options header */
539 MAKE_EXTHDR(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
);
540 /* Destination options header(1st part) */
541 if (opt
->ip6po_rthdr
) {
543 * Destination options header(1st part)
544 * This only makes sense with a routing header.
545 * See Section 9.2 of RFC 3542.
546 * Disabling this part just for MIP6 convenience is
547 * a bad idea. We need to think carefully about a
548 * way to make the advanced API coexist with MIP6
549 * options, which might automatically be inserted in
552 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
555 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
556 /* Destination options header(2nd part) */
557 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
563 if (necp_matched_policy_id
) {
564 necp_mark_packet_from_ip(m
, necp_matched_policy_id
);
566 switch (necp_result
) {
567 case NECP_KERNEL_POLICY_RESULT_PASS
:
569 case NECP_KERNEL_POLICY_RESULT_DROP
:
570 case NECP_KERNEL_POLICY_RESULT_SOCKET_DIVERT
:
572 * Flow divert packets should be blocked at the IP
575 error
= EHOSTUNREACH
;
577 case NECP_KERNEL_POLICY_RESULT_IP_TUNNEL
: {
579 * Verify that the packet is being routed to the tunnel
581 struct ifnet
*policy_ifp
=
582 necp_get_ifnet_from_result_parameter(
583 &necp_result_parameter
);
585 if (policy_ifp
== ifp
) {
588 if (necp_packet_can_rebind_to_ifnet(m
,
589 policy_ifp
, (struct route
*)&necp_route
,
592 * Set scoped index to the tunnel
593 * interface, since it is compatible
594 * with the packet. This will only work
595 * for callers who pass IPV6_OUTARGS,
596 * but that covers all of the clients
597 * we care about today.
599 if (flags
& IPV6_OUTARGS
) {
600 ip6oa
->ip6oa_boundif
=
601 policy_ifp
->if_index
;
602 ip6oa
->ip6oa_flags
|=
606 && opt
->ip6po_pktinfo
!= NULL
) {
609 policy_ifp
->if_index
;
626 if (ipsec_bypass
!= 0 || ip6obf
.noipsec
)
630 /* get a security policy for this packet */
632 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
,
635 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
,
639 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
647 switch (sp
->policy
) {
648 case IPSEC_POLICY_DISCARD
:
649 case IPSEC_POLICY_GENERATE
:
651 * This packet is just discarded.
653 IPSEC_STAT_INCREMENT(ipsec6stat
.out_polvio
);
656 case IPSEC_POLICY_BYPASS
:
657 case IPSEC_POLICY_NONE
:
658 /* no need to do IPsec. */
659 ip6obf
.needipsec
= FALSE
;
662 case IPSEC_POLICY_IPSEC
:
663 if (sp
->req
== NULL
) {
664 /* acquire a policy */
665 error
= key_spdacquire(sp
);
671 ip6obf
.needipsec
= TRUE
;
675 case IPSEC_POLICY_ENTRUST
:
677 printf("%s: Invalid policy found: %d\n", __func__
, sp
->policy
);
684 * Calculate the total length of the extension header chain.
685 * Keep the length of the unfragmentable part for fragmentation.
688 if (exthdrs
.ip6e_hbh
!= NULL
)
689 optlen
+= exthdrs
.ip6e_hbh
->m_len
;
690 if (exthdrs
.ip6e_dest1
!= NULL
)
691 optlen
+= exthdrs
.ip6e_dest1
->m_len
;
692 if (exthdrs
.ip6e_rthdr
!= NULL
)
693 optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
694 unfragpartlen
= optlen
+ sizeof (struct ip6_hdr
);
696 /* NOTE: we don't add AH/ESP length here. do that later. */
697 if (exthdrs
.ip6e_dest2
!= NULL
)
698 optlen
+= exthdrs
.ip6e_dest2
->m_len
;
701 * If we need IPsec, or there is at least one extension header,
702 * separate IP6 header from the payload.
708 optlen
) && !ip6obf
.hdrsplit
) {
709 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
713 m
= exthdrs
.ip6e_ip6
;
714 ip6obf
.hdrsplit
= TRUE
;
718 ip6
= mtod(m
, struct ip6_hdr
*);
720 /* adjust mbuf packet header length */
721 m
->m_pkthdr
.len
+= optlen
;
722 plen
= m
->m_pkthdr
.len
- sizeof (*ip6
);
724 /* If this is a jumbo payload, insert a jumbo payload option. */
725 if (plen
> IPV6_MAXPACKET
) {
726 if (!ip6obf
.hdrsplit
) {
727 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
731 m
= exthdrs
.ip6e_ip6
;
732 ip6obf
.hdrsplit
= TRUE
;
735 ip6
= mtod(m
, struct ip6_hdr
*);
736 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0)
740 ip6
->ip6_plen
= htons(plen
);
743 * Concatenate headers and fill in next header fields.
744 * Here we have, on "m"
746 * and we insert headers accordingly. Finally, we should be getting:
747 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
749 * during the header composing process, "m" points to IPv6 header.
750 * "mprev" points to an extension header prior to esp.
752 nexthdrp
= &ip6
->ip6_nxt
;
756 * we treat dest2 specially. this makes IPsec processing
757 * much easier. the goal here is to make mprev point the
758 * mbuf prior to dest2.
760 * result: IPv6 dest2 payload
761 * m and mprev will point to IPv6 header.
763 if (exthdrs
.ip6e_dest2
!= NULL
) {
764 if (!ip6obf
.hdrsplit
) {
765 panic("assumption failed: hdr not split");
768 exthdrs
.ip6e_dest2
->m_next
= m
->m_next
;
769 m
->m_next
= exthdrs
.ip6e_dest2
;
770 *mtod(exthdrs
.ip6e_dest2
, u_char
*) = ip6
->ip6_nxt
;
771 ip6
->ip6_nxt
= IPPROTO_DSTOPTS
;
774 #define MAKE_CHAIN(m, mp, p, i) do { \
776 if (!ip6obf.hdrsplit) { \
777 panic("assumption failed: hdr not split"); \
780 *mtod((m), u_char *) = *(p); \
782 p = mtod((m), u_char *); \
783 (m)->m_next = (mp)->m_next; \
784 (mp)->m_next = (m); \
789 * result: IPv6 hbh dest1 rthdr dest2 payload
790 * m will point to IPv6 header. mprev will point to the
791 * extension header prior to dest2 (rthdr in the above case).
793 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
, nexthdrp
, IPPROTO_HOPOPTS
);
794 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
, nexthdrp
, IPPROTO_DSTOPTS
);
795 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
, nexthdrp
, IPPROTO_ROUTING
);
797 /* It is no longer safe to free the pointers in exthdrs. */
798 exthdrs
.merged
= TRUE
;
803 if (ip6obf
.needipsec
&& (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
))
804 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
807 if (!TAILQ_EMPTY(&ipv6_filters
)) {
808 struct ipfilter
*filter
;
809 int seen
= (inject_filter_ref
== NULL
);
812 if (im6o
!= NULL
&& IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
813 ippo
->ippo_flags
|= IPPOF_MCAST_OPTS
;
815 ippo
->ippo_mcast_ifnet
= im6o
->im6o_multicast_ifp
;
816 ippo
->ippo_mcast_ttl
= im6o
->im6o_multicast_hlim
;
817 ippo
->ippo_mcast_loop
= im6o
->im6o_multicast_loop
;
821 /* Hack: embed the scope_id in the destination */
822 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
823 (ip6
->ip6_dst
.s6_addr16
[1] == 0) && (ro
!= NULL
)) {
825 ip6
->ip6_dst
.s6_addr16
[1] =
826 htons(ro
->ro_dst
.sin6_scope_id
);
830 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
832 * Don't process packet twice if we've already seen it.
835 if ((struct ipfilter
*)inject_filter_ref
==
838 } else if (filter
->ipf_filter
.ipf_output
!= NULL
) {
841 result
= filter
->ipf_filter
.ipf_output(
842 filter
->ipf_filter
.cookie
,
844 if (result
== EJUSTRETURN
) {
857 ip6
= mtod(m
, struct ip6_hdr
*);
858 /* Hack: cleanup embedded scope_id if we put it there */
860 ip6
->ip6_dst
.s6_addr16
[1] = 0;
864 if (ip6obf
.needipsec
) {
868 * pointers after IPsec headers are not valid any more.
869 * other pointers need a great care too.
870 * (IPsec routines should not mangle mbufs prior to AH/ESP)
872 exthdrs
.ip6e_dest2
= NULL
;
874 if (exthdrs
.ip6e_rthdr
!= NULL
) {
875 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
876 segleft_org
= rh
->ip6r_segleft
;
877 rh
->ip6r_segleft
= 0;
884 error
= ipsec6_output_trans(&ipsec_state
, nexthdrp
, mprev
,
885 sp
, flags
, &needipsectun
);
888 /* mbuf is already reclaimed in ipsec6_output_trans. */
898 printf("ip6_output (ipsec): error code %d\n",
902 /* don't show these error codes to the user */
908 if (exthdrs
.ip6e_rthdr
!= NULL
) {
909 /* ah6_output doesn't modify mbuf chain */
910 rh
->ip6r_segleft
= segleft_org
;
916 * If there is a routing header, replace the destination address field
917 * with the first hop of the routing header.
919 if (exthdrs
.ip6e_rthdr
!= NULL
) {
920 struct ip6_rthdr0
*rh0
;
921 struct in6_addr
*addr
;
922 struct sockaddr_in6 sa
;
924 rh
= (struct ip6_rthdr
*)
925 (mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*));
926 switch (rh
->ip6r_type
) {
927 case IPV6_RTHDR_TYPE_0
:
928 rh0
= (struct ip6_rthdr0
*)rh
;
929 addr
= (struct in6_addr
*)(void *)(rh0
+ 1);
932 * construct a sockaddr_in6 form of
935 * XXX: we may not have enough
936 * information about its scope zone;
937 * there is no standard API to pass
938 * the information from the
941 bzero(&sa
, sizeof (sa
));
942 sa
.sin6_family
= AF_INET6
;
943 sa
.sin6_len
= sizeof (sa
);
944 sa
.sin6_addr
= addr
[0];
945 if ((error
= sa6_embedscope(&sa
,
946 ip6_use_defzone
)) != 0) {
949 ip6
->ip6_dst
= sa
.sin6_addr
;
950 bcopy(&addr
[1], &addr
[0], sizeof (struct in6_addr
) *
951 (rh0
->ip6r0_segleft
- 1));
952 addr
[rh0
->ip6r0_segleft
- 1] = finaldst
;
954 in6_clearscope(addr
+ rh0
->ip6r0_segleft
- 1);
956 default: /* is it possible? */
962 /* Source address validation */
963 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
964 !(flags
& IPV6_UNSPECSRC
)) {
966 ip6stat
.ip6s_badscope
++;
969 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
971 ip6stat
.ip6s_badscope
++;
975 ip6stat
.ip6s_localout
++;
982 bzero((caddr_t
)ro
, sizeof (*ro
));
985 if (opt
!= NULL
&& opt
->ip6po_rthdr
)
986 ro
= &opt
->ip6po_route
;
987 dst
= SIN6(&ro
->ro_dst
);
989 if (ro
->ro_rt
!= NULL
)
990 RT_LOCK_ASSERT_NOTHELD(ro
->ro_rt
);
992 * if specified, try to fill in the traffic class field.
993 * do not override if a non-zero value is already set.
994 * we check the diffserv field and the ecn field separately.
996 if (opt
!= NULL
&& opt
->ip6po_tclass
>= 0) {
999 if ((ip6
->ip6_flow
& htonl(0xfc << 20)) == 0)
1001 if ((ip6
->ip6_flow
& htonl(0x03 << 20)) == 0)
1005 htonl((opt
->ip6po_tclass
& mask
) << 20);
1009 /* fill in or override the hop limit field, if necessary. */
1010 if (opt
&& opt
->ip6po_hlim
!= -1) {
1011 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
1012 } else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1015 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
1018 ip6
->ip6_hlim
= ip6_defmcasthlim
;
1023 * If there is a cached route, check that it is to the same
1024 * destination and is still up. If not, free it and try again.
1025 * Test rt_flags without holding rt_lock for performance reasons;
1026 * if the route is down it will hopefully be caught by the layer
1027 * below (since it uses this route as a hint) or during the
1030 if (ROUTE_UNUSABLE(ro
) || dst
->sin6_family
!= AF_INET6
||
1031 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
))
1034 if (ro
->ro_rt
== NULL
) {
1035 bzero(dst
, sizeof (*dst
));
1036 dst
->sin6_family
= AF_INET6
;
1037 dst
->sin6_len
= sizeof (struct sockaddr_in6
);
1038 dst
->sin6_addr
= ip6
->ip6_dst
;
1041 if (ip6obf
.needipsec
&& needipsectun
) {
1043 struct ifnet
*trace_ifp
= (ifpp_save
!= NULL
) ? (*ifpp_save
) : NULL
;
1044 #endif /* CONFIG_DTRACE */
1046 * All the extension headers will become inaccessible
1047 * (since they can be encrypted).
1048 * Don't panic, we need no more updates to extension headers
1049 * on inner IPv6 packet (since they are now encapsulated).
1051 * IPv6 [ESP|AH] IPv6 [extension headers] payload
1053 bzero(&exthdrs
, sizeof (exthdrs
));
1054 exthdrs
.ip6e_ip6
= m
;
1057 route_copyout(&ipsec_state
.ro
, (struct route
*)ro
,
1058 sizeof (ipsec_state
.ro
));
1059 ipsec_state
.dst
= SA(dst
);
1061 /* So that we can see packets inside the tunnel */
1062 DTRACE_IP6(send
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1063 struct ip6_hdr
*, ip6
, struct ifnet
*, trace_ifp
,
1064 struct ip
*, NULL
, struct ip6_hdr
*, ip6
);
1066 error
= ipsec6_output_tunnel(&ipsec_state
, sp
, flags
);
1067 /* tunneled in IPv4? packet is gone */
1068 if (ipsec_state
.tunneled
== 4) {
1073 ipsec_saved_route
= ro
;
1074 ro
= (struct route_in6
*)&ipsec_state
.ro
;
1075 dst
= SIN6(ipsec_state
.dst
);
1077 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
1087 printf("ip6_output (ipsec): error code %d\n",
1091 /* don't show these error codes to the user */
1098 * The packet has been encapsulated so the ifscope
1099 * is no longer valid since it does not apply to the
1100 * outer address: ignore the ifscope.
1102 if (flags
& IPV6_OUTARGS
) {
1103 ip6oa
->ip6oa_boundif
= IFSCOPE_NONE
;
1104 ip6oa
->ip6oa_flags
&= ~IP6OAF_BOUND_IF
;
1106 if (opt
!= NULL
&& opt
->ip6po_pktinfo
!= NULL
) {
1107 if (opt
->ip6po_pktinfo
->ipi6_ifindex
!= IFSCOPE_NONE
)
1108 opt
->ip6po_pktinfo
->ipi6_ifindex
= IFSCOPE_NONE
;
1110 exthdrs
.ip6e_ip6
= m
;
1115 * ifp should only be filled in for dummy net packets which will jump
1116 * to check_with_pf label.
1119 VERIFY(ip6obf
.route_selected
);
1122 /* adjust pointer */
1123 ip6
= mtod(m
, struct ip6_hdr
*);
1125 if (ip6obf
.select_srcif
) {
1126 bzero(&src_sa
, sizeof (src_sa
));
1127 src_sa
.sin6_family
= AF_INET6
;
1128 src_sa
.sin6_len
= sizeof (src_sa
);
1129 src_sa
.sin6_addr
= ip6
->ip6_src
;
1131 bzero(&dst_sa
, sizeof (dst_sa
));
1132 dst_sa
.sin6_family
= AF_INET6
;
1133 dst_sa
.sin6_len
= sizeof (dst_sa
);
1134 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
1137 * Only call in6_selectroute() on first iteration to avoid taking
1138 * multiple references on ifp and rt.
1140 * in6_selectroute() might return an ifp with its reference held
1141 * even in the error case, so make sure to release its reference.
1142 * ip6oa may be NULL if IPV6_OUTARGS isn't set.
1144 if (!ip6obf
.route_selected
) {
1145 error
= in6_selectroute( ip6obf
.select_srcif
? &src_sa
: NULL
,
1146 &dst_sa
, opt
, im6o
, &src_ia
, ro
, &ifp
, &rt
, 0, ip6oa
);
1151 ip6stat
.ip6s_noroute
++;
1155 break; /* XXX statistics? */
1158 in6_ifstat_inc(ifp
, ifs6_out_discard
);
1159 /* ifp (if non-NULL) will be released at the end */
1162 ip6obf
.route_selected
= TRUE
;
1166 * If in6_selectroute() does not return a route entry,
1167 * dst may not have been updated.
1169 *dst
= dst_sa
; /* XXX */
1173 /* Catch-all to check if the interface is allowed */
1174 if (!necp_packet_is_allowed_over_interface(m
, ifp
)) {
1175 error
= EHOSTUNREACH
;
1181 * then rt (for unicast) and ifp must be non-NULL valid values.
1183 if (!(flags
& IPV6_FORWARDING
)) {
1184 in6_ifstat_inc_na(ifp
, ifs6_out_request
);
1189 ia
= (struct in6_ifaddr
*)(rt
->rt_ifa
);
1191 IFA_ADDREF(&ia
->ia_ifa
);
1198 * The outgoing interface must be in the zone of source and
1199 * destination addresses (except local/loopback). We should
1200 * use ia_ifp to support the case of sending packets to an
1201 * address of our own.
1203 if (ia
!= NULL
&& ia
->ia_ifp
) {
1204 ifnet_reference(ia
->ia_ifp
); /* for origifp */
1205 if (origifp
!= NULL
)
1206 ifnet_release(origifp
);
1207 origifp
= ia
->ia_ifp
;
1210 ifnet_reference(ifp
); /* for origifp */
1211 if (origifp
!= NULL
)
1212 ifnet_release(origifp
);
1216 /* skip scope enforcements for local/loopback route */
1217 if (rt
== NULL
|| !(rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
1218 struct in6_addr src0
, dst0
;
1221 src0
= ip6
->ip6_src
;
1222 if (in6_setscope(&src0
, origifp
, &zone
))
1224 bzero(&src_sa
, sizeof (src_sa
));
1225 src_sa
.sin6_family
= AF_INET6
;
1226 src_sa
.sin6_len
= sizeof (src_sa
);
1227 src_sa
.sin6_addr
= ip6
->ip6_src
;
1228 if ((sa6_recoverscope(&src_sa
, TRUE
) ||
1229 zone
!= src_sa
.sin6_scope_id
))
1232 dst0
= ip6
->ip6_dst
;
1233 if ((in6_setscope(&dst0
, origifp
, &zone
)))
1235 /* re-initialize to be sure */
1236 bzero(&dst_sa
, sizeof (dst_sa
));
1237 dst_sa
.sin6_family
= AF_INET6
;
1238 dst_sa
.sin6_len
= sizeof (dst_sa
);
1239 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
1240 if ((sa6_recoverscope(&dst_sa
, TRUE
) ||
1241 zone
!= dst_sa
.sin6_scope_id
))
1244 /* scope check is done. */
1248 ip6stat
.ip6s_badscope
++;
1249 in6_ifstat_inc(origifp
, ifs6_out_discard
);
1251 error
= EHOSTUNREACH
; /* XXX */
1256 if (rt
!= NULL
&& !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1257 if (opt
!= NULL
&& opt
->ip6po_nextroute
.ro_rt
) {
1259 * The nexthop is explicitly specified by the
1260 * application. We assume the next hop is an IPv6
1263 dst
= SIN6(opt
->ip6po_nexthop
);
1264 } else if ((rt
->rt_flags
& RTF_GATEWAY
)) {
1265 dst
= SIN6(rt
->rt_gateway
);
1268 * For packets destined to local/loopback, record the
1269 * source the source interface (which owns the source
1270 * address), as well as the output interface. This is
1271 * needed to reconstruct the embedded zone for the
1272 * link-local address case in ip6_input().
1274 if (ia
!= NULL
&& (ifp
->if_flags
& IFF_LOOPBACK
)) {
1278 srcidx
= src_ia
->ia_ifp
->if_index
;
1279 else if (ro
->ro_srcia
!= NULL
)
1280 srcidx
= ro
->ro_srcia
->ifa_ifp
->if_index
;
1284 ip6_setsrcifaddr_info(m
, srcidx
, NULL
);
1285 ip6_setdstifaddr_info(m
, 0, ia
);
1289 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1290 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
1292 struct in6_multi
*in6m
;
1294 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
1295 in6_ifstat_inc_na(ifp
, ifs6_out_mcast
);
1298 * Confirm that the outgoing interface supports multicast.
1300 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
1301 ip6stat
.ip6s_noroute
++;
1302 in6_ifstat_inc(ifp
, ifs6_out_discard
);
1303 error
= ENETUNREACH
;
1306 in6_multihead_lock_shared();
1307 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, in6m
);
1308 in6_multihead_lock_done();
1312 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
1316 * If we belong to the destination multicast group
1317 * on the outgoing interface, and the caller did not
1318 * forbid loopback, loop back a copy.
1320 ip6_mloopback(NULL
, ifp
, m
, dst
, optlen
, nxt0
);
1321 } else if (im6o
!= NULL
)
1326 * Multicasts with a hoplimit of zero may be looped back,
1327 * above, but must not be transmitted on a network.
1328 * Also, multicasts addressed to the loopback interface
1329 * are not sent -- the above call to ip6_mloopback() will
1330 * loop back a copy if this host actually belongs to the
1331 * destination group on the loopback interface.
1333 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
) ||
1334 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
)) {
1335 /* remove m from the packetchain and continue looping */
1344 * Fill the outgoing inteface to tell the upper layer
1345 * to increment per-interface statistics.
1347 if (ifpp
!= NULL
&& *ifpp
== NULL
) {
1348 ifnet_reference(ifp
); /* for caller */
1352 /* Determine path MTU. */
1353 if ((error
= ip6_getpmtu(ro_pmtu
, ro
, ifp
, &finaldst
, &mtu
,
1358 * The caller of this function may specify to use the minimum MTU
1360 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
1361 * setting. The logic is a bit complicated; by default, unicast
1362 * packets will follow path MTU while multicast packets will be sent at
1363 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
1364 * including unicast ones will be sent at the minimum MTU. Multicast
1365 * packets will always be sent at the minimum MTU unless
1366 * IP6PO_MINMTU_DISABLE is explicitly specified.
1367 * See RFC 3542 for more details.
1369 if (mtu
> IPV6_MMTU
) {
1370 if ((flags
& IPV6_MINMTU
)) {
1372 } else if (opt
&& opt
->ip6po_minmtu
== IP6PO_MINMTU_ALL
) {
1374 } else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) &&
1376 opt
->ip6po_minmtu
!= IP6PO_MINMTU_DISABLE
)) {
1382 * clear embedded scope identifiers if necessary.
1383 * in6_clearscope will touch the addresses only when necessary.
1385 in6_clearscope(&ip6
->ip6_src
);
1386 in6_clearscope(&ip6
->ip6_dst
);
1388 * If the outgoing packet contains a hop-by-hop options header,
1389 * it must be examined and processed even by the source node.
1390 * (RFC 2460, section 4.)
1392 if (exthdrs
.ip6e_hbh
!= NULL
) {
1393 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
1394 u_int32_t dummy
; /* XXX unused */
1395 uint32_t oplen
= 0; /* for ip6_process_hopopts() */
1397 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
)
1398 panic("ip6e_hbh is not continuous");
1401 * XXX: If we have to send an ICMPv6 error to the sender,
1402 * we need the M_LOOP flag since icmp6_error() expects
1403 * the IPv6 and the hop-by-hop options header are
1404 * continuous unless the flag is set.
1406 m
->m_flags
|= M_LOOP
;
1407 m
->m_pkthdr
.rcvif
= ifp
;
1408 if (ip6_process_hopopts(m
, (u_int8_t
*)(hbh
+ 1),
1409 ((hbh
->ip6h_len
+ 1) << 3) - sizeof (struct ip6_hbh
),
1410 &dummy
, &oplen
) < 0) {
1412 * m was already freed at this point. Set to NULL so it
1413 * is not re-freed at end of ip6_output_list.
1416 error
= EINVAL
; /* better error? */
1419 m
->m_flags
&= ~M_LOOP
; /* XXX */
1420 m
->m_pkthdr
.rcvif
= NULL
;
1425 #endif /* DUMMYNET */
1427 if (PF_IS_ENABLED
) {
1431 * TODO: Need to save opt->ip6po_flags for reinjection
1436 args
.fwa_oflags
= flags
;
1437 if (flags
& IPV6_OUTARGS
)
1438 args
.fwa_ip6oa
= ip6oa
;
1440 args
.fwa_dst6
= dst
;
1441 args
.fwa_ro6_pmtu
= ro_pmtu
;
1442 args
.fwa_origifp
= origifp
;
1444 args
.fwa_alwaysfrag
= alwaysfrag
;
1445 args
.fwa_unfragpartlen
= unfragpartlen
;
1446 args
.fwa_exthdrs
= &exthdrs
;
1447 /* Invoke outbound packet filter */
1448 error
= pf_af_hook(ifp
, NULL
, &m
, AF_INET6
, FALSE
, &args
);
1449 #else /* !DUMMYNET */
1450 error
= pf_af_hook(ifp
, NULL
, &m
, AF_INET6
, FALSE
, NULL
);
1451 #endif /* !DUMMYNET */
1453 if (error
!= 0 || m
== NULL
) {
1455 panic("%s: unexpected packet %p\n",
1459 /* m was already freed by callee and is now NULL. */
1462 ip6
= mtod(m
, struct ip6_hdr
*);
1467 /* clean ipsec history before fragmentation */
1471 if (ip6oa
!= NULL
) {
1474 dscp
= (ntohl(ip6
->ip6_flow
) & IP6FLOW_DSCP_MASK
) >> IP6FLOW_DSCP_SHIFT
;
1476 error
= set_packet_qos(m
, ifp
,
1477 ip6oa
->ip6oa_flags
& IP6OAF_QOSMARKING_ALLOWED
? TRUE
: FALSE
,
1478 ip6oa
->ip6oa_sotc
, ip6oa
->ip6oa_netsvctype
, &dscp
);
1480 ip6
->ip6_flow
&= ~htonl(IP6FLOW_DSCP_MASK
);
1481 ip6
->ip6_flow
|= htonl((u_int32_t
)dscp
<< IP6FLOW_DSCP_SHIFT
);
1483 printf("%s if_dscp_for_mbuf() error %d\n", __func__
, error
);
1488 * Determine whether fragmentation is necessary. If so, m is passed
1489 * back as a chain of packets and original mbuf is freed. Otherwise, m
1492 error
= ip6_fragment_packet(&m
, opt
,
1493 &exthdrs
, ifp
, mtu
, alwaysfrag
, unfragpartlen
, ro_pmtu
, nxt0
,
1500 * The evaluateloop label is where we decide whether to continue looping over
1501 * packets or call into nd code to send.
1506 * m may be NULL when we jump to the evaluateloop label from PF or
1507 * other code that can drop packets.
1511 * If we already have a chain to send, tack m onto the end.
1512 * Otherwise make m the start and end of the to-be-sent chain.
1514 if (sendchain
!= NULL
) {
1515 sendchain_last
->m_nextpkt
= m
;
1520 /* Fragmentation may mean m is a chain. Find the last packet. */
1521 while (m
->m_nextpkt
)
1527 /* Fill in next m from inputchain as appropriate. */
1530 /* Isolate m from rest of input chain. */
1531 inputchain
= m
->m_nextpkt
;
1532 m
->m_nextpkt
= NULL
;
1535 * Clear exthdrs and ipsec_state so stale contents are not
1536 * reused. Note this also clears the exthdrs.merged flag.
1538 bzero(&exthdrs
, sizeof(exthdrs
));
1539 bzero(&ipsec_state
, sizeof(ipsec_state
));
1541 /* Continue looping. */
1546 * If we get here, there's no more mbufs in inputchain, so send the
1547 * sendchain if there is one.
1550 error
= nd6_output_list(ifp
, origifp
, sendchain
, dst
,
1553 * Fall through to done label even in error case because
1554 * nd6_output_list frees packetchain in both success and
1560 if (ifpp_save
!= NULL
&& *ifpp_save
!= NULL
) {
1561 ifnet_release(*ifpp_save
);
1564 ROUTE_RELEASE(&ip6route
);
1566 ROUTE_RELEASE(&ipsec_state
.ro
);
1568 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1571 ROUTE_RELEASE(&necp_route
);
1574 ROUTE_RELEASE(&saved_route
);
1575 ROUTE_RELEASE(&saved_ro_pmtu
);
1576 #endif /* DUMMYNET */
1579 IFA_REMREF(&ia
->ia_ifa
);
1581 IFA_REMREF(&src_ia
->ia_ifa
);
1584 if (origifp
!= NULL
)
1585 ifnet_release(origifp
);
1586 if (ip6_output_measure
) {
1587 net_perf_measure_time(&net_perf
, &start_tv
, packets_processed
);
1588 net_perf_histogram(&net_perf
, packets_processed
);
1593 if (exthdrs
.ip6e_hbh
!= NULL
) {
1595 panic("Double free of ip6e_hbh");
1596 m_freem(exthdrs
.ip6e_hbh
);
1598 if (exthdrs
.ip6e_dest1
!= NULL
) {
1600 panic("Double free of ip6e_dest1");
1601 m_freem(exthdrs
.ip6e_dest1
);
1603 if (exthdrs
.ip6e_rthdr
!= NULL
) {
1605 panic("Double free of ip6e_rthdr");
1606 m_freem(exthdrs
.ip6e_rthdr
);
1608 if (exthdrs
.ip6e_dest2
!= NULL
) {
1610 panic("Double free of ip6e_dest2");
1611 m_freem(exthdrs
.ip6e_dest2
);
1615 if (inputchain
!= NULL
)
1616 m_freem_list(inputchain
);
1617 if (sendchain
!= NULL
)
1618 m_freem_list(sendchain
);
1629 #undef saved_ro_pmtu
1633 /* ip6_fragment_packet
1635 * The fragmentation logic is rather complex:
1636 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1637 * 1-a: send as is if tlen <= path mtu
1638 * 1-b: fragment if tlen > path mtu
1640 * 2: if user asks us not to fragment (dontfrag == 1)
1641 * 2-a: send as is if tlen <= interface mtu
1642 * 2-b: error if tlen > interface mtu
1644 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1647 * 4: if dontfrag == 1 && alwaysfrag == 1
1648 * error, as we cannot handle this conflicting request
1652 ip6_fragment_packet(struct mbuf
**mptr
, struct ip6_pktopts
*opt
,
1653 struct ip6_exthdrs
*exthdrsp
, struct ifnet
*ifp
, uint32_t mtu
,
1654 boolean_t alwaysfrag
, uint32_t unfragpartlen
, struct route_in6
*ro_pmtu
,
1655 int nxt0
, uint32_t optlen
)
1657 VERIFY(NULL
!= mptr
);
1658 struct mbuf
*m
= *mptr
;
1660 size_t tlen
= m
->m_pkthdr
.len
;
1661 boolean_t dontfrag
= (opt
!= NULL
&& (opt
->ip6po_flags
& IP6PO_DONTFRAG
));
1663 if (m
->m_pkthdr
.pkt_flags
& PKTF_FORWARDED
)
1666 if (dontfrag
&& alwaysfrag
) { /* case 4 */
1667 /* conflicting request - can't transmit */
1671 /* Access without acquiring nd_ifinfo lock for performance */
1672 if (dontfrag
&& tlen
> IN6_LINKMTU(ifp
)) { /* case 2-b */
1674 * Even if the DONTFRAG option is specified, we cannot send the
1675 * packet when the data length is larger than the MTU of the
1676 * outgoing interface.
1677 * Notify the error by sending IPV6_PATHMTU ancillary data as
1678 * well as returning an error code (the latter is not described
1682 struct ip6ctlparam ip6cp
;
1684 mtu32
= (u_int32_t
)mtu
;
1685 bzero(&ip6cp
, sizeof (ip6cp
));
1686 ip6cp
.ip6c_cmdarg
= (void *)&mtu32
;
1687 pfctlinput2(PRC_MSGSIZE
, SA(&ro_pmtu
->ro_dst
), (void *)&ip6cp
);
1692 * transmit packet without fragmentation
1694 if (dontfrag
|| (!alwaysfrag
&& /* case 1-a and 2-a */
1695 (tlen
<= mtu
|| TSO_IPV6_OK(ifp
, m
) ||
1696 (ifp
->if_hwassist
& CSUM_FRAGMENT_IPV6
)))) {
1698 * mppn not updated in this case because no new chain is formed
1701 ip6_output_checksum(ifp
, mtu
, m
, nxt0
, tlen
, optlen
);
1704 * time to fragment - cases 1-b and 3 are handled inside
1705 * ip6_do_fragmentation().
1706 * mppn is passed down to be updated to point at fragment chain.
1708 error
= ip6_do_fragmentation(mptr
, optlen
, ifp
,
1709 unfragpartlen
, mtod(m
, struct ip6_hdr
*), exthdrsp
, mtu
, nxt0
);
1716 * ip6_do_fragmentation() is called by ip6_fragment_packet() after determining
1717 * the packet needs to be fragmented. on success, morig is freed and a chain
1718 * of fragments is linked into the packet chain where morig existed. Otherwise,
1719 * an errno is returned.
1722 ip6_do_fragmentation(struct mbuf
**mptr
, uint32_t optlen
, struct ifnet
*ifp
,
1723 uint32_t unfragpartlen
, struct ip6_hdr
*ip6
, struct ip6_exthdrs
*exthdrsp
,
1724 uint32_t mtu
, int nxt0
)
1726 VERIFY(NULL
!= mptr
);
1729 struct mbuf
*morig
= *mptr
;
1730 struct mbuf
*first_mbufp
= NULL
;
1731 struct mbuf
*last_mbufp
= NULL
;
1733 size_t tlen
= morig
->m_pkthdr
.len
;
1736 * try to fragment the packet. case 1-b and 3
1738 if ((morig
->m_pkthdr
.csum_flags
& CSUM_TSO_IPV6
)) {
1739 /* TSO and fragment aren't compatible */
1740 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1742 } else if (mtu
< IPV6_MMTU
) {
1743 /* path MTU cannot be less than IPV6_MMTU */
1744 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1746 } else if (ip6
->ip6_plen
== 0) {
1747 /* jumbo payload cannot be fragmented */
1748 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1751 size_t hlen
, len
, off
;
1752 struct mbuf
**mnext
= NULL
;
1753 struct ip6_frag
*ip6f
;
1754 u_int32_t id
= htonl(ip6_randomid());
1758 * Too large for the destination or interface;
1759 * fragment if possible.
1760 * Must be able to put at least 8 bytes per fragment.
1762 hlen
= unfragpartlen
;
1763 if (mtu
> IPV6_MAXPACKET
)
1764 mtu
= IPV6_MAXPACKET
;
1766 len
= (mtu
- hlen
- sizeof (struct ip6_frag
)) & ~7;
1768 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1773 * Change the next header field of the last header in the
1774 * unfragmentable part.
1776 if (exthdrsp
->ip6e_rthdr
!= NULL
) {
1777 nextproto
= *mtod(exthdrsp
->ip6e_rthdr
, u_char
*);
1778 *mtod(exthdrsp
->ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
1779 } else if (exthdrsp
->ip6e_dest1
!= NULL
) {
1780 nextproto
= *mtod(exthdrsp
->ip6e_dest1
, u_char
*);
1781 *mtod(exthdrsp
->ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
1782 } else if (exthdrsp
->ip6e_hbh
!= NULL
) {
1783 nextproto
= *mtod(exthdrsp
->ip6e_hbh
, u_char
*);
1784 *mtod(exthdrsp
->ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
1786 nextproto
= ip6
->ip6_nxt
;
1787 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
1790 if (morig
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
)
1791 in6_delayed_cksum_offset(morig
, 0, optlen
, nxt0
);
1794 * Loop through length of segment after first fragment,
1795 * make new header and copy data of each part and link onto
1798 for (off
= hlen
; off
< tlen
; off
+= len
) {
1799 struct ip6_hdr
*new_mhip6
;
1801 struct mbuf
*m_frgpart
;
1803 MGETHDR(new_m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1804 if (new_m
== NULL
) {
1806 ip6stat
.ip6s_odropped
++;
1809 new_m
->m_pkthdr
.rcvif
= NULL
;
1810 new_m
->m_flags
= morig
->m_flags
& M_COPYFLAGS
;
1812 if (first_mbufp
!= NULL
) {
1813 /* Every pass through loop but first */
1817 /* This is the first element of the fragment chain */
1818 first_mbufp
= new_m
;
1821 mnext
= &new_m
->m_nextpkt
;
1823 new_m
->m_data
+= max_linkhdr
;
1824 new_mhip6
= mtod(new_m
, struct ip6_hdr
*);
1826 new_m
->m_len
= sizeof (*new_mhip6
);
1828 error
= ip6_insertfraghdr(morig
, new_m
, hlen
, &ip6f
);
1830 ip6stat
.ip6s_odropped
++;
1834 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1835 if (off
+ len
>= tlen
)
1838 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1839 new_mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1840 sizeof (*ip6f
) - sizeof (struct ip6_hdr
)));
1842 if ((m_frgpart
= m_copy(morig
, off
, len
)) == NULL
) {
1844 ip6stat
.ip6s_odropped
++;
1847 m_cat(new_m
, m_frgpart
);
1848 new_m
->m_pkthdr
.len
= len
+ hlen
+ sizeof (*ip6f
);
1849 new_m
->m_pkthdr
.rcvif
= NULL
;
1851 M_COPY_CLASSIFIER(new_m
, morig
);
1852 M_COPY_PFTAG(new_m
, morig
);
1856 mac_create_fragment(morig
, new_m
);
1857 #endif /* CONFIG_MACF_NET */
1860 ip6f
->ip6f_reserved
= 0;
1861 ip6f
->ip6f_ident
= id
;
1862 ip6f
->ip6f_nxt
= nextproto
;
1863 ip6stat
.ip6s_ofragments
++;
1864 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1868 /* free all the fragments created */
1869 if (first_mbufp
!= NULL
) {
1870 m_freem_list(first_mbufp
);
1875 /* successful fragmenting */
1877 *mptr
= first_mbufp
;
1878 last_mbufp
->m_nextpkt
= NULL
;
1879 ip6stat
.ip6s_fragmented
++;
1880 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1887 ip6_copyexthdr(struct mbuf
**mp
, caddr_t hdr
, int hlen
)
1891 if (hlen
> MCLBYTES
)
1892 return (ENOBUFS
); /* XXX */
1894 MGET(m
, M_DONTWAIT
, MT_DATA
);
1899 MCLGET(m
, M_DONTWAIT
);
1900 if (!(m
->m_flags
& M_EXT
)) {
1907 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1914 ip6_out_cksum_stats(int proto
, u_int32_t len
)
1918 tcp_out6_cksum_stats(len
);
1921 udp_out6_cksum_stats(len
);
1924 /* keep only TCP or UDP stats for now */
1930 * Process a delayed payload checksum calculation (outbound path.)
1932 * hoff is the number of bytes beyond the mbuf data pointer which
1933 * points to the IPv6 header. optlen is the number of bytes, if any,
1934 * between the end of IPv6 header and the beginning of the ULP payload
1935 * header, which represents the extension headers. If optlen is less
1936 * than zero, this routine will bail when it detects extension headers.
1938 * Returns a bitmask representing all the work done in software.
1941 in6_finalize_cksum(struct mbuf
*m
, uint32_t hoff
, int32_t optlen
,
1942 int32_t nxt0
, uint32_t csum_flags
)
1944 unsigned char buf
[sizeof (struct ip6_hdr
)] __attribute__((aligned(8)));
1945 struct ip6_hdr
*ip6
;
1946 uint32_t offset
, mlen
, hlen
, olen
, sw_csum
;
1947 uint16_t csum
, ulpoff
, plen
;
1950 _CASSERT(sizeof (csum
) == sizeof (uint16_t));
1951 VERIFY(m
->m_flags
& M_PKTHDR
);
1953 sw_csum
= (csum_flags
& m
->m_pkthdr
.csum_flags
);
1955 if ((sw_csum
&= CSUM_DELAY_IPV6_DATA
) == 0)
1958 mlen
= m
->m_pkthdr
.len
; /* total mbuf len */
1959 hlen
= sizeof (*ip6
); /* IPv6 header len */
1961 /* sanity check (need at least IPv6 header) */
1962 if (mlen
< (hoff
+ hlen
)) {
1963 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr "
1964 "(%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
);
1969 * In case the IPv6 header is not contiguous, or not 32-bit
1970 * aligned, copy it to a local buffer.
1972 if ((hoff
+ hlen
) > m
->m_len
||
1973 !IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
) + hoff
)) {
1974 m_copydata(m
, hoff
, hlen
, (caddr_t
)buf
);
1975 ip6
= (struct ip6_hdr
*)(void *)buf
;
1977 ip6
= (struct ip6_hdr
*)(void *)(m
->m_data
+ hoff
);
1981 plen
= ntohs(ip6
->ip6_plen
);
1982 if (plen
!= (mlen
- (hoff
+ hlen
))) {
1983 plen
= OSSwapInt16(plen
);
1984 if (plen
!= (mlen
- (hoff
+ hlen
))) {
1985 /* Don't complain for jumbograms */
1986 if (plen
!= 0 || nxt
!= IPPROTO_HOPOPTS
) {
1987 printf("%s: mbuf 0x%llx proto %d IPv6 "
1988 "plen %d (%x) [swapped %d (%x)] doesn't "
1989 "match actual packet length; %d is used "
1990 "instead\n", __func__
,
1991 (uint64_t)VM_KERNEL_ADDRPERM(m
), nxt
,
1992 ip6
->ip6_plen
, ip6
->ip6_plen
, plen
, plen
,
1993 (mlen
- (hoff
+ hlen
)));
1995 plen
= mlen
- (hoff
+ hlen
);
2000 /* next header isn't TCP/UDP and we don't know optlen, bail */
2001 if (nxt
!= IPPROTO_TCP
&& nxt
!= IPPROTO_UDP
) {
2007 /* caller supplied the original transport number; use it */
2013 offset
= hoff
+ hlen
+ olen
; /* ULP header */
2016 if (mlen
< offset
) {
2017 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr+ext_hdr "
2018 "(%u+%u+%u)\n", __func__
, m
, mlen
, hoff
, hlen
, olen
);
2023 * offset is added to the lower 16-bit value of csum_data,
2024 * which is expected to contain the ULP offset; therefore
2025 * CSUM_PARTIAL offset adjustment must be undone.
2027 if ((m
->m_pkthdr
.csum_flags
& (CSUM_PARTIAL
|CSUM_DATA_VALID
)) ==
2028 (CSUM_PARTIAL
|CSUM_DATA_VALID
)) {
2030 * Get back the original ULP offset (this will
2031 * undo the CSUM_PARTIAL logic in ip6_output.)
2033 m
->m_pkthdr
.csum_data
= (m
->m_pkthdr
.csum_tx_stuff
-
2034 m
->m_pkthdr
.csum_tx_start
);
2037 ulpoff
= (m
->m_pkthdr
.csum_data
& 0xffff); /* ULP csum offset */
2039 if (mlen
< (ulpoff
+ sizeof (csum
))) {
2040 panic("%s: mbuf %p pkt len (%u) proto %d invalid ULP "
2041 "cksum offset (%u) cksum flags 0x%x\n", __func__
,
2042 m
, mlen
, nxt
, ulpoff
, m
->m_pkthdr
.csum_flags
);
2046 csum
= inet6_cksum(m
, 0, offset
, plen
- olen
);
2049 ip6_out_cksum_stats(nxt
, plen
- olen
);
2051 /* RFC1122 4.1.3.4 */
2052 if (csum
== 0 && (m
->m_pkthdr
.csum_flags
& CSUM_UDPIPV6
))
2055 /* Insert the checksum in the ULP csum field */
2057 if ((offset
+ sizeof (csum
)) > m
->m_len
) {
2058 m_copyback(m
, offset
, sizeof (csum
), &csum
);
2059 } else if (IP6_HDR_ALIGNED_P(mtod(m
, char *) + hoff
)) {
2060 *(uint16_t *)(void *)(mtod(m
, char *) + offset
) = csum
;
2062 bcopy(&csum
, (mtod(m
, char *) + offset
), sizeof (csum
));
2064 m
->m_pkthdr
.csum_flags
&=
2065 ~(CSUM_DELAY_IPV6_DATA
| CSUM_DATA_VALID
| CSUM_PARTIAL
);
2072 * Insert jumbo payload option.
2075 ip6_insert_jumboopt(struct ip6_exthdrs
*exthdrs
, u_int32_t plen
)
2081 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
2084 * If there is no hop-by-hop options header, allocate new one.
2085 * If there is one but it doesn't have enough space to store the
2086 * jumbo payload option, allocate a cluster to store the whole options.
2087 * Otherwise, use it to store the options.
2089 if (exthdrs
->ip6e_hbh
== NULL
) {
2090 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
2093 mopt
->m_len
= JUMBOOPTLEN
;
2094 optbuf
= mtod(mopt
, u_char
*);
2095 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
2096 exthdrs
->ip6e_hbh
= mopt
;
2098 struct ip6_hbh
*hbh
;
2100 mopt
= exthdrs
->ip6e_hbh
;
2101 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
2104 * - exthdrs->ip6e_hbh is not referenced from places
2105 * other than exthdrs.
2106 * - exthdrs->ip6e_hbh is not an mbuf chain.
2108 u_int32_t oldoptlen
= mopt
->m_len
;
2112 * XXX: give up if the whole (new) hbh header does
2113 * not fit even in an mbuf cluster.
2115 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
)
2119 * As a consequence, we must always prepare a cluster
2122 MGET(n
, M_DONTWAIT
, MT_DATA
);
2124 MCLGET(n
, M_DONTWAIT
);
2125 if (!(n
->m_flags
& M_EXT
)) {
2132 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
2133 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
2135 optbuf
= mtod(n
, u_char
*) + oldoptlen
;
2137 mopt
= exthdrs
->ip6e_hbh
= n
;
2139 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
2140 mopt
->m_len
+= JUMBOOPTLEN
;
2142 optbuf
[0] = IP6OPT_PADN
;
2146 * Adjust the header length according to the pad and
2147 * the jumbo payload option.
2149 hbh
= mtod(mopt
, struct ip6_hbh
*);
2150 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
2153 /* fill in the option. */
2154 optbuf
[2] = IP6OPT_JUMBO
;
2156 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
2157 bcopy(&v
, &optbuf
[4], sizeof (u_int32_t
));
2159 /* finally, adjust the packet header length */
2160 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
2167 * Insert fragment header and copy unfragmentable header portions.
2170 ip6_insertfraghdr(struct mbuf
*m0
, struct mbuf
*m
, int hlen
,
2171 struct ip6_frag
**frghdrp
)
2173 struct mbuf
*n
, *mlast
;
2175 if (hlen
> sizeof (struct ip6_hdr
)) {
2176 n
= m_copym(m0
, sizeof (struct ip6_hdr
),
2177 hlen
- sizeof (struct ip6_hdr
), M_DONTWAIT
);
2184 /* Search for the last mbuf of unfragmentable part. */
2185 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
)
2188 if (!(mlast
->m_flags
& M_EXT
) &&
2189 M_TRAILINGSPACE(mlast
) >= sizeof (struct ip6_frag
)) {
2190 /* use the trailing space of the last mbuf for the frag hdr */
2191 *frghdrp
= (struct ip6_frag
*)(mtod(mlast
, caddr_t
) +
2193 mlast
->m_len
+= sizeof (struct ip6_frag
);
2194 m
->m_pkthdr
.len
+= sizeof (struct ip6_frag
);
2196 /* allocate a new mbuf for the fragment header */
2199 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
2202 mfrg
->m_len
= sizeof (struct ip6_frag
);
2203 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
2204 mlast
->m_next
= mfrg
;
2211 ip6_getpmtu(struct route_in6
*ro_pmtu
, struct route_in6
*ro
,
2212 struct ifnet
*ifp
, struct in6_addr
*dst
, u_int32_t
*mtup
,
2213 boolean_t
*alwaysfragp
)
2216 boolean_t alwaysfrag
= FALSE
;
2219 if (ro_pmtu
!= ro
) {
2220 /* The first hop and the final destination may differ. */
2221 struct sockaddr_in6
*sa6_dst
= SIN6(&ro_pmtu
->ro_dst
);
2222 if (ROUTE_UNUSABLE(ro_pmtu
) ||
2223 !IN6_ARE_ADDR_EQUAL(&sa6_dst
->sin6_addr
, dst
))
2224 ROUTE_RELEASE(ro_pmtu
);
2226 if (ro_pmtu
->ro_rt
== NULL
) {
2227 bzero(sa6_dst
, sizeof (*sa6_dst
));
2228 sa6_dst
->sin6_family
= AF_INET6
;
2229 sa6_dst
->sin6_len
= sizeof (struct sockaddr_in6
);
2230 sa6_dst
->sin6_addr
= *dst
;
2232 rtalloc_scoped((struct route
*)ro_pmtu
,
2233 ifp
!= NULL
? ifp
->if_index
: IFSCOPE_NONE
);
2237 if (ro_pmtu
->ro_rt
!= NULL
) {
2241 ifp
= ro_pmtu
->ro_rt
->rt_ifp
;
2242 /* Access without acquiring nd_ifinfo lock for performance */
2243 ifmtu
= IN6_LINKMTU(ifp
);
2246 * Access rmx_mtu without holding the route entry lock,
2247 * for performance; this isn't something that changes
2248 * often, so optimize.
2250 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
2251 if (mtu
> ifmtu
|| mtu
== 0) {
2253 * The MTU on the route is larger than the MTU on
2254 * the interface! This shouldn't happen, unless the
2255 * MTU of the interface has been changed after the
2256 * interface was brought up. Change the MTU in the
2257 * route to match the interface MTU (as long as the
2258 * field isn't locked).
2260 * if MTU on the route is 0, we need to fix the MTU.
2261 * this case happens with path MTU discovery timeouts.
2264 if (!(ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
))
2265 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
2266 } else if (mtu
< IPV6_MMTU
) {
2268 * RFC2460 section 5, last paragraph:
2269 * if we record ICMPv6 too big message with
2270 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
2271 * or smaller, with framgent header attached.
2272 * (fragment header is needed regardless from the
2273 * packet size, for translators to identify packets)
2280 /* Don't hold nd_ifinfo lock for performance */
2281 mtu
= IN6_LINKMTU(ifp
);
2283 error
= EHOSTUNREACH
; /* XXX */
2288 if (alwaysfragp
!= NULL
)
2289 *alwaysfragp
= alwaysfrag
;
2294 * IP6 socket option processing.
2297 ip6_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2299 int optdatalen
, uproto
;
2302 struct inpcb
*in6p
= sotoinpcb(so
);
2303 int error
= 0, optval
= 0;
2304 int level
, op
= -1, optname
= 0;
2308 VERIFY(sopt
!= NULL
);
2310 level
= sopt
->sopt_level
;
2311 op
= sopt
->sopt_dir
;
2312 optname
= sopt
->sopt_name
;
2313 optlen
= sopt
->sopt_valsize
;
2315 uproto
= (int)SOCK_PROTO(so
);
2317 privileged
= (proc_suser(p
) == 0);
2319 if (level
== IPPROTO_IPV6
) {
2323 case IPV6_2292PKTOPTIONS
: {
2326 error
= soopt_getm(sopt
, &m
);
2329 error
= soopt_mcopyin(sopt
, m
);
2332 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
2339 * Use of some Hop-by-Hop options or some
2340 * Destination options, might require special
2341 * privilege. That is, normal applications
2342 * (without special privilege) might be forbidden
2343 * from setting certain options in outgoing packets,
2344 * and might never see certain options in received
2345 * packets. [RFC 2292 Section 6]
2346 * KAME specific note:
2347 * KAME prevents non-privileged users from sending or
2348 * receiving ANY hbh/dst options in order to avoid
2349 * overhead of parsing options in the kernel.
2351 case IPV6_RECVHOPOPTS
:
2352 case IPV6_RECVDSTOPTS
:
2353 case IPV6_RECVRTHDRDSTOPTS
:
2357 case IPV6_UNICAST_HOPS
:
2359 case IPV6_RECVPKTINFO
:
2360 case IPV6_RECVHOPLIMIT
:
2361 case IPV6_RECVRTHDR
:
2362 case IPV6_RECVPATHMTU
:
2363 case IPV6_RECVTCLASS
:
2365 case IPV6_AUTOFLOWLABEL
:
2366 if (optlen
!= sizeof (int)) {
2370 error
= sooptcopyin(sopt
, &optval
,
2371 sizeof (optval
), sizeof (optval
));
2376 case IPV6_UNICAST_HOPS
:
2377 if (optval
< -1 || optval
>= 256) {
2380 /* -1 = kernel default */
2381 in6p
->in6p_hops
= optval
;
2382 if (in6p
->inp_vflag
&
2389 #define OPTSET(bit) do { \
2391 in6p->inp_flags |= (bit); \
2393 in6p->inp_flags &= ~(bit); \
2396 #define OPTSET2292(bit) do { \
2397 in6p->inp_flags |= IN6P_RFC2292; \
2399 in6p->inp_flags |= (bit); \
2401 in6p->inp_flags &= ~(bit); \
2404 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
2406 case IPV6_RECVPKTINFO
:
2407 /* cannot mix with RFC2292 */
2408 if (OPTBIT(IN6P_RFC2292
)) {
2412 OPTSET(IN6P_PKTINFO
);
2415 case IPV6_HOPLIMIT
: {
2416 struct ip6_pktopts
**optp
;
2418 /* cannot mix with RFC2292 */
2419 if (OPTBIT(IN6P_RFC2292
)) {
2423 optp
= &in6p
->in6p_outputopts
;
2424 error
= ip6_pcbopt(IPV6_HOPLIMIT
,
2425 (u_char
*)&optval
, sizeof (optval
),
2430 case IPV6_RECVHOPLIMIT
:
2431 /* cannot mix with RFC2292 */
2432 if (OPTBIT(IN6P_RFC2292
)) {
2436 OPTSET(IN6P_HOPLIMIT
);
2439 case IPV6_RECVHOPOPTS
:
2440 /* cannot mix with RFC2292 */
2441 if (OPTBIT(IN6P_RFC2292
)) {
2445 OPTSET(IN6P_HOPOPTS
);
2448 case IPV6_RECVDSTOPTS
:
2449 /* cannot mix with RFC2292 */
2450 if (OPTBIT(IN6P_RFC2292
)) {
2454 OPTSET(IN6P_DSTOPTS
);
2457 case IPV6_RECVRTHDRDSTOPTS
:
2458 /* cannot mix with RFC2292 */
2459 if (OPTBIT(IN6P_RFC2292
)) {
2463 OPTSET(IN6P_RTHDRDSTOPTS
);
2466 case IPV6_RECVRTHDR
:
2467 /* cannot mix with RFC2292 */
2468 if (OPTBIT(IN6P_RFC2292
)) {
2475 case IPV6_RECVPATHMTU
:
2477 * We ignore this option for TCP
2479 * (RFC3542 leaves this case
2482 if (uproto
!= IPPROTO_TCP
)
2488 * make setsockopt(IPV6_V6ONLY)
2489 * available only prior to bind(2).
2490 * see ipng mailing list, Jun 22 2001.
2492 if (in6p
->inp_lport
||
2493 !IN6_IS_ADDR_UNSPECIFIED(
2494 &in6p
->in6p_laddr
)) {
2498 OPTSET(IN6P_IPV6_V6ONLY
);
2500 in6p
->inp_vflag
&= ~INP_IPV4
;
2502 in6p
->inp_vflag
|= INP_IPV4
;
2505 case IPV6_RECVTCLASS
:
2506 /* we can mix with RFC2292 */
2507 OPTSET(IN6P_TCLASS
);
2510 case IPV6_AUTOFLOWLABEL
:
2511 OPTSET(IN6P_AUTOFLOWLABEL
);
2519 case IPV6_USE_MIN_MTU
:
2520 case IPV6_PREFER_TEMPADDR
: {
2521 struct ip6_pktopts
**optp
;
2523 if (optlen
!= sizeof (optval
)) {
2527 error
= sooptcopyin(sopt
, &optval
,
2528 sizeof (optval
), sizeof (optval
));
2532 optp
= &in6p
->in6p_outputopts
;
2533 error
= ip6_pcbopt(optname
, (u_char
*)&optval
,
2534 sizeof (optval
), optp
, uproto
);
2538 case IPV6_2292PKTINFO
:
2539 case IPV6_2292HOPLIMIT
:
2540 case IPV6_2292HOPOPTS
:
2541 case IPV6_2292DSTOPTS
:
2542 case IPV6_2292RTHDR
:
2544 if (optlen
!= sizeof (int)) {
2548 error
= sooptcopyin(sopt
, &optval
,
2549 sizeof (optval
), sizeof (optval
));
2553 case IPV6_2292PKTINFO
:
2554 OPTSET2292(IN6P_PKTINFO
);
2556 case IPV6_2292HOPLIMIT
:
2557 OPTSET2292(IN6P_HOPLIMIT
);
2559 case IPV6_2292HOPOPTS
:
2561 * Check super-user privilege.
2562 * See comments for IPV6_RECVHOPOPTS.
2566 OPTSET2292(IN6P_HOPOPTS
);
2568 case IPV6_2292DSTOPTS
:
2571 OPTSET2292(IN6P_DSTOPTS
|
2572 IN6P_RTHDRDSTOPTS
); /* XXX */
2574 case IPV6_2292RTHDR
:
2575 OPTSET2292(IN6P_RTHDR
);
2580 case IPV6_3542PKTINFO
:
2581 case IPV6_3542HOPOPTS
:
2582 case IPV6_3542RTHDR
:
2583 case IPV6_3542DSTOPTS
:
2584 case IPV6_RTHDRDSTOPTS
:
2585 case IPV6_3542NEXTHOP
: {
2586 struct ip6_pktopts
**optp
;
2587 /* new advanced API (RFC3542) */
2590 /* cannot mix with RFC2292 */
2591 if (OPTBIT(IN6P_RFC2292
)) {
2595 error
= soopt_getm(sopt
, &m
);
2598 error
= soopt_mcopyin(sopt
, m
);
2602 optp
= &in6p
->in6p_outputopts
;
2603 error
= ip6_pcbopt(optname
, mtod(m
, u_char
*),
2604 m
->m_len
, optp
, uproto
);
2609 case IPV6_MULTICAST_IF
:
2610 case IPV6_MULTICAST_HOPS
:
2611 case IPV6_MULTICAST_LOOP
:
2612 case IPV6_JOIN_GROUP
:
2613 case IPV6_LEAVE_GROUP
:
2615 case MCAST_BLOCK_SOURCE
:
2616 case MCAST_UNBLOCK_SOURCE
:
2617 case MCAST_JOIN_GROUP
:
2618 case MCAST_LEAVE_GROUP
:
2619 case MCAST_JOIN_SOURCE_GROUP
:
2620 case MCAST_LEAVE_SOURCE_GROUP
:
2621 error
= ip6_setmoptions(in6p
, sopt
);
2624 case IPV6_PORTRANGE
:
2625 error
= sooptcopyin(sopt
, &optval
,
2626 sizeof (optval
), sizeof (optval
));
2631 case IPV6_PORTRANGE_DEFAULT
:
2632 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2633 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2636 case IPV6_PORTRANGE_HIGH
:
2637 in6p
->inp_flags
&= ~(INP_LOWPORT
);
2638 in6p
->inp_flags
|= INP_HIGHPORT
;
2641 case IPV6_PORTRANGE_LOW
:
2642 in6p
->inp_flags
&= ~(INP_HIGHPORT
);
2643 in6p
->inp_flags
|= INP_LOWPORT
;
2652 case IPV6_IPSEC_POLICY
: {
2657 if ((error
= soopt_getm(sopt
, &m
)) != 0)
2659 if ((error
= soopt_mcopyin(sopt
, m
)) != 0)
2662 req
= mtod(m
, caddr_t
);
2664 error
= ipsec6_set_policy(in6p
, optname
, req
,
2671 * IPv6 variant of IP_BOUND_IF; for details see
2672 * comments on IP_BOUND_IF in ip_ctloutput().
2675 /* This option is settable only on IPv6 */
2676 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2681 error
= sooptcopyin(sopt
, &optval
,
2682 sizeof (optval
), sizeof (optval
));
2687 error
= inp_bindif(in6p
, optval
, NULL
);
2690 case IPV6_NO_IFT_CELLULAR
:
2691 /* This option is settable only for IPv6 */
2692 if (!(in6p
->inp_vflag
& INP_IPV6
)) {
2697 error
= sooptcopyin(sopt
, &optval
,
2698 sizeof (optval
), sizeof (optval
));
2703 /* once set, it cannot be unset */
2704 if (!optval
&& INP_NO_CELLULAR(in6p
)) {
2709 error
= so_set_restrictions(so
,
2710 SO_RESTRICT_DENY_CELLULAR
);
2714 /* This option is not settable */
2719 error
= ENOPROTOOPT
;
2727 case IPV6_2292PKTOPTIONS
:
2729 * RFC3542 (effectively) deprecated the
2730 * semantics of the 2292-style pktoptions.
2731 * Since it was not reliable in nature (i.e.,
2732 * applications had to expect the lack of some
2733 * information after all), it would make sense
2734 * to simplify this part by always returning
2737 sopt
->sopt_valsize
= 0;
2740 case IPV6_RECVHOPOPTS
:
2741 case IPV6_RECVDSTOPTS
:
2742 case IPV6_RECVRTHDRDSTOPTS
:
2743 case IPV6_UNICAST_HOPS
:
2744 case IPV6_RECVPKTINFO
:
2745 case IPV6_RECVHOPLIMIT
:
2746 case IPV6_RECVRTHDR
:
2747 case IPV6_RECVPATHMTU
:
2749 case IPV6_PORTRANGE
:
2750 case IPV6_RECVTCLASS
:
2751 case IPV6_AUTOFLOWLABEL
:
2754 case IPV6_RECVHOPOPTS
:
2755 optval
= OPTBIT(IN6P_HOPOPTS
);
2758 case IPV6_RECVDSTOPTS
:
2759 optval
= OPTBIT(IN6P_DSTOPTS
);
2762 case IPV6_RECVRTHDRDSTOPTS
:
2763 optval
= OPTBIT(IN6P_RTHDRDSTOPTS
);
2766 case IPV6_UNICAST_HOPS
:
2767 optval
= in6p
->in6p_hops
;
2770 case IPV6_RECVPKTINFO
:
2771 optval
= OPTBIT(IN6P_PKTINFO
);
2774 case IPV6_RECVHOPLIMIT
:
2775 optval
= OPTBIT(IN6P_HOPLIMIT
);
2778 case IPV6_RECVRTHDR
:
2779 optval
= OPTBIT(IN6P_RTHDR
);
2782 case IPV6_RECVPATHMTU
:
2783 optval
= OPTBIT(IN6P_MTU
);
2787 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
2790 case IPV6_PORTRANGE
: {
2792 flags
= in6p
->inp_flags
;
2793 if (flags
& INP_HIGHPORT
)
2794 optval
= IPV6_PORTRANGE_HIGH
;
2795 else if (flags
& INP_LOWPORT
)
2796 optval
= IPV6_PORTRANGE_LOW
;
2801 case IPV6_RECVTCLASS
:
2802 optval
= OPTBIT(IN6P_TCLASS
);
2805 case IPV6_AUTOFLOWLABEL
:
2806 optval
= OPTBIT(IN6P_AUTOFLOWLABEL
);
2811 error
= sooptcopyout(sopt
, &optval
,
2815 case IPV6_PATHMTU
: {
2817 struct ip6_mtuinfo mtuinfo
;
2818 struct route_in6 sro
;
2820 bzero(&sro
, sizeof (sro
));
2822 if (!(so
->so_state
& SS_ISCONNECTED
))
2825 * XXX: we dot not consider the case of source
2826 * routing, or optional information to specify
2827 * the outgoing interface.
2829 error
= ip6_getpmtu(&sro
, NULL
, NULL
,
2830 &in6p
->in6p_faddr
, &pmtu
, NULL
);
2831 ROUTE_RELEASE(&sro
);
2834 if (pmtu
> IPV6_MAXPACKET
)
2835 pmtu
= IPV6_MAXPACKET
;
2837 bzero(&mtuinfo
, sizeof (mtuinfo
));
2838 mtuinfo
.ip6m_mtu
= (u_int32_t
)pmtu
;
2839 optdata
= (void *)&mtuinfo
;
2840 optdatalen
= sizeof (mtuinfo
);
2841 error
= sooptcopyout(sopt
, optdata
,
2846 case IPV6_2292PKTINFO
:
2847 case IPV6_2292HOPLIMIT
:
2848 case IPV6_2292HOPOPTS
:
2849 case IPV6_2292RTHDR
:
2850 case IPV6_2292DSTOPTS
:
2852 case IPV6_2292PKTINFO
:
2853 optval
= OPTBIT(IN6P_PKTINFO
);
2855 case IPV6_2292HOPLIMIT
:
2856 optval
= OPTBIT(IN6P_HOPLIMIT
);
2858 case IPV6_2292HOPOPTS
:
2859 optval
= OPTBIT(IN6P_HOPOPTS
);
2861 case IPV6_2292RTHDR
:
2862 optval
= OPTBIT(IN6P_RTHDR
);
2864 case IPV6_2292DSTOPTS
:
2865 optval
= OPTBIT(IN6P_DSTOPTS
|
2869 error
= sooptcopyout(sopt
, &optval
,
2877 case IPV6_RTHDRDSTOPTS
:
2881 case IPV6_USE_MIN_MTU
:
2882 case IPV6_PREFER_TEMPADDR
:
2883 error
= ip6_getpcbopt(in6p
->in6p_outputopts
,
2887 case IPV6_MULTICAST_IF
:
2888 case IPV6_MULTICAST_HOPS
:
2889 case IPV6_MULTICAST_LOOP
:
2891 error
= ip6_getmoptions(in6p
, sopt
);
2894 case IPV6_IPSEC_POLICY
: {
2895 error
= 0; /* This option is no longer supported */
2900 if (in6p
->inp_flags
& INP_BOUND_IF
)
2901 optval
= in6p
->inp_boundifp
->if_index
;
2902 error
= sooptcopyout(sopt
, &optval
,
2906 case IPV6_NO_IFT_CELLULAR
:
2907 optval
= INP_NO_CELLULAR(in6p
) ? 1 : 0;
2908 error
= sooptcopyout(sopt
, &optval
,
2913 optval
= (in6p
->in6p_last_outifp
!= NULL
) ?
2914 in6p
->in6p_last_outifp
->if_index
: 0;
2915 error
= sooptcopyout(sopt
, &optval
,
2920 error
= ENOPROTOOPT
;
2925 } else if (level
== IPPROTO_UDP
) {
2926 error
= udp_ctloutput(so
, sopt
);
2934 ip6_raw_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2936 int error
= 0, optval
, optlen
;
2937 const int icmp6off
= offsetof(struct icmp6_hdr
, icmp6_cksum
);
2938 struct inpcb
*in6p
= sotoinpcb(so
);
2939 int level
, op
, optname
;
2941 level
= sopt
->sopt_level
;
2942 op
= sopt
->sopt_dir
;
2943 optname
= sopt
->sopt_name
;
2944 optlen
= sopt
->sopt_valsize
;
2946 if (level
!= IPPROTO_IPV6
)
2952 * For ICMPv6 sockets, no modification allowed for checksum
2953 * offset, permit "no change" values to help existing apps.
2955 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2956 * for an ICMPv6 socket will fail."
2957 * The current behavior does not meet RFC3542.
2961 if (optlen
!= sizeof (int)) {
2965 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2969 if ((optval
% 2) != 0) {
2970 /* the API assumes even offset values */
2972 } else if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
) {
2973 if (optval
!= icmp6off
)
2976 in6p
->in6p_cksum
= optval
;
2981 if (SOCK_PROTO(so
) == IPPROTO_ICMPV6
)
2984 optval
= in6p
->in6p_cksum
;
2986 error
= sooptcopyout(sopt
, &optval
, sizeof (optval
));
2996 error
= ENOPROTOOPT
;
3004 * Set up IP6 options in pcb for insertion in output packets or
3005 * specifying behavior of outgoing packets.
3008 ip6_pcbopts(struct ip6_pktopts
**pktopt
, struct mbuf
*m
, struct socket
*so
,
3009 struct sockopt
*sopt
)
3011 #pragma unused(sopt)
3012 struct ip6_pktopts
*opt
= *pktopt
;
3015 /* turn off any old options. */
3018 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
3019 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
3020 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
3021 printf("%s: all specified options are cleared.\n",
3024 ip6_clearpktopts(opt
, -1);
3026 opt
= _MALLOC(sizeof (*opt
), M_IP6OPT
, M_WAITOK
);
3032 if (m
== NULL
|| m
->m_len
== 0) {
3034 * Only turning off any previous options, regardless of
3035 * whether the opt is just created or given.
3038 FREE(opt
, M_IP6OPT
);
3042 /* set options specified by user. */
3043 if ((error
= ip6_setpktopts(m
, opt
, NULL
, SOCK_PROTO(so
))) != 0) {
3044 ip6_clearpktopts(opt
, -1); /* XXX: discard all options */
3045 FREE(opt
, M_IP6OPT
);
3053 * initialize ip6_pktopts. beware that there are non-zero default values in
3057 ip6_initpktopts(struct ip6_pktopts
*opt
)
3060 bzero(opt
, sizeof (*opt
));
3061 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
3062 opt
->ip6po_tclass
= -1; /* -1 means default traffic class */
3063 opt
->ip6po_minmtu
= IP6PO_MINMTU_MCASTONLY
;
3064 opt
->ip6po_prefer_tempaddr
= IP6PO_TEMPADDR_SYSTEM
;
3068 ip6_pcbopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
**pktopt
,
3071 struct ip6_pktopts
*opt
;
3075 opt
= _MALLOC(sizeof (*opt
), M_IP6OPT
, M_WAITOK
);
3078 ip6_initpktopts(opt
);
3082 return (ip6_setpktopt(optname
, buf
, len
, opt
, 1, 0, uproto
));
3086 ip6_getpcbopt(struct ip6_pktopts
*pktopt
, int optname
, struct sockopt
*sopt
)
3088 void *optdata
= NULL
;
3090 struct ip6_ext
*ip6e
;
3091 struct in6_pktinfo null_pktinfo
;
3092 int deftclass
= 0, on
;
3093 int defminmtu
= IP6PO_MINMTU_MCASTONLY
;
3094 int defpreftemp
= IP6PO_TEMPADDR_SYSTEM
;
3099 if (pktopt
&& pktopt
->ip6po_pktinfo
)
3100 optdata
= (void *)pktopt
->ip6po_pktinfo
;
3102 /* XXX: we don't have to do this every time... */
3103 bzero(&null_pktinfo
, sizeof (null_pktinfo
));
3104 optdata
= (void *)&null_pktinfo
;
3106 optdatalen
= sizeof (struct in6_pktinfo
);
3110 if (pktopt
&& pktopt
->ip6po_tclass
>= 0)
3111 optdata
= (void *)&pktopt
->ip6po_tclass
;
3113 optdata
= (void *)&deftclass
;
3114 optdatalen
= sizeof (int);
3118 if (pktopt
&& pktopt
->ip6po_hbh
) {
3119 optdata
= (void *)pktopt
->ip6po_hbh
;
3120 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_hbh
;
3121 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3126 if (pktopt
&& pktopt
->ip6po_rthdr
) {
3127 optdata
= (void *)pktopt
->ip6po_rthdr
;
3128 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_rthdr
;
3129 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3133 case IPV6_RTHDRDSTOPTS
:
3134 if (pktopt
&& pktopt
->ip6po_dest1
) {
3135 optdata
= (void *)pktopt
->ip6po_dest1
;
3136 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest1
;
3137 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3142 if (pktopt
&& pktopt
->ip6po_dest2
) {
3143 optdata
= (void *)pktopt
->ip6po_dest2
;
3144 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest2
;
3145 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
3150 if (pktopt
&& pktopt
->ip6po_nexthop
) {
3151 optdata
= (void *)pktopt
->ip6po_nexthop
;
3152 optdatalen
= pktopt
->ip6po_nexthop
->sa_len
;
3156 case IPV6_USE_MIN_MTU
:
3158 optdata
= (void *)&pktopt
->ip6po_minmtu
;
3160 optdata
= (void *)&defminmtu
;
3161 optdatalen
= sizeof (int);
3165 if (pktopt
&& ((pktopt
->ip6po_flags
) & IP6PO_DONTFRAG
))
3169 optdata
= (void *)&on
;
3170 optdatalen
= sizeof (on
);
3173 case IPV6_PREFER_TEMPADDR
:
3175 optdata
= (void *)&pktopt
->ip6po_prefer_tempaddr
;
3177 optdata
= (void *)&defpreftemp
;
3178 optdatalen
= sizeof (int);
3181 default: /* should not happen */
3183 panic("ip6_getpcbopt: unexpected option\n");
3185 return (ENOPROTOOPT
);
3188 return (sooptcopyout(sopt
, optdata
, optdatalen
));
3192 ip6_clearpktopts(struct ip6_pktopts
*pktopt
, int optname
)
3197 if (optname
== -1 || optname
== IPV6_PKTINFO
) {
3198 if (pktopt
->ip6po_pktinfo
)
3199 FREE(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
3200 pktopt
->ip6po_pktinfo
= NULL
;
3202 if (optname
== -1 || optname
== IPV6_HOPLIMIT
)
3203 pktopt
->ip6po_hlim
= -1;
3204 if (optname
== -1 || optname
== IPV6_TCLASS
)
3205 pktopt
->ip6po_tclass
= -1;
3206 if (optname
== -1 || optname
== IPV6_NEXTHOP
) {
3207 ROUTE_RELEASE(&pktopt
->ip6po_nextroute
);
3208 if (pktopt
->ip6po_nexthop
)
3209 FREE(pktopt
->ip6po_nexthop
, M_IP6OPT
);
3210 pktopt
->ip6po_nexthop
= NULL
;
3212 if (optname
== -1 || optname
== IPV6_HOPOPTS
) {
3213 if (pktopt
->ip6po_hbh
)
3214 FREE(pktopt
->ip6po_hbh
, M_IP6OPT
);
3215 pktopt
->ip6po_hbh
= NULL
;
3217 if (optname
== -1 || optname
== IPV6_RTHDRDSTOPTS
) {
3218 if (pktopt
->ip6po_dest1
)
3219 FREE(pktopt
->ip6po_dest1
, M_IP6OPT
);
3220 pktopt
->ip6po_dest1
= NULL
;
3222 if (optname
== -1 || optname
== IPV6_RTHDR
) {
3223 if (pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
3224 FREE(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
3225 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
3226 ROUTE_RELEASE(&pktopt
->ip6po_route
);
3228 if (optname
== -1 || optname
== IPV6_DSTOPTS
) {
3229 if (pktopt
->ip6po_dest2
)
3230 FREE(pktopt
->ip6po_dest2
, M_IP6OPT
);
3231 pktopt
->ip6po_dest2
= NULL
;
3235 #define PKTOPT_EXTHDRCPY(type) do { \
3238 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3; \
3239 dst->type = _MALLOC(hlen, M_IP6OPT, canwait); \
3240 if (dst->type == NULL && canwait == M_NOWAIT) \
3242 bcopy(src->type, dst->type, hlen); \
3247 copypktopts(struct ip6_pktopts
*dst
, struct ip6_pktopts
*src
, int canwait
)
3249 if (dst
== NULL
|| src
== NULL
) {
3250 printf("copypktopts: invalid argument\n");
3254 dst
->ip6po_hlim
= src
->ip6po_hlim
;
3255 dst
->ip6po_tclass
= src
->ip6po_tclass
;
3256 dst
->ip6po_flags
= src
->ip6po_flags
;
3257 if (src
->ip6po_pktinfo
) {
3258 dst
->ip6po_pktinfo
= _MALLOC(sizeof (*dst
->ip6po_pktinfo
),
3260 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
)
3262 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
3264 if (src
->ip6po_nexthop
) {
3265 dst
->ip6po_nexthop
= _MALLOC(src
->ip6po_nexthop
->sa_len
,
3267 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
)
3269 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
3270 src
->ip6po_nexthop
->sa_len
);
3272 PKTOPT_EXTHDRCPY(ip6po_hbh
);
3273 PKTOPT_EXTHDRCPY(ip6po_dest1
);
3274 PKTOPT_EXTHDRCPY(ip6po_dest2
);
3275 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
3279 ip6_clearpktopts(dst
, -1);
3282 #undef PKTOPT_EXTHDRCPY
3284 struct ip6_pktopts
*
3285 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
3288 struct ip6_pktopts
*dst
;
3290 dst
= _MALLOC(sizeof (*dst
), M_IP6OPT
, canwait
);
3293 ip6_initpktopts(dst
);
3295 if ((error
= copypktopts(dst
, src
, canwait
)) != 0) {
3296 FREE(dst
, M_IP6OPT
);
3304 ip6_freepcbopts(struct ip6_pktopts
*pktopt
)
3309 ip6_clearpktopts(pktopt
, -1);
3311 FREE(pktopt
, M_IP6OPT
);
3315 ip6_moptions_init(void)
3317 PE_parse_boot_argn("ifa_debug", &im6o_debug
, sizeof (im6o_debug
));
3319 im6o_size
= (im6o_debug
== 0) ? sizeof (struct ip6_moptions
) :
3320 sizeof (struct ip6_moptions_dbg
);
3322 im6o_zone
= zinit(im6o_size
, IM6O_ZONE_MAX
* im6o_size
, 0,
3324 if (im6o_zone
== NULL
) {
3325 panic("%s: failed allocating %s", __func__
, IM6O_ZONE_NAME
);
3328 zone_change(im6o_zone
, Z_EXPAND
, TRUE
);
3332 im6o_addref(struct ip6_moptions
*im6o
, int locked
)
3337 IM6O_LOCK_ASSERT_HELD(im6o
);
3339 if (++im6o
->im6o_refcnt
== 0) {
3340 panic("%s: im6o %p wraparound refcnt\n", __func__
, im6o
);
3342 } else if (im6o
->im6o_trace
!= NULL
) {
3343 (*im6o
->im6o_trace
)(im6o
, TRUE
);
3351 im6o_remref(struct ip6_moptions
*im6o
)
3356 if (im6o
->im6o_refcnt
== 0) {
3357 panic("%s: im6o %p negative refcnt", __func__
, im6o
);
3359 } else if (im6o
->im6o_trace
!= NULL
) {
3360 (*im6o
->im6o_trace
)(im6o
, FALSE
);
3363 --im6o
->im6o_refcnt
;
3364 if (im6o
->im6o_refcnt
> 0) {
3369 for (i
= 0; i
< im6o
->im6o_num_memberships
; ++i
) {
3370 struct in6_mfilter
*imf
;
3372 imf
= im6o
->im6o_mfilters
? &im6o
->im6o_mfilters
[i
] : NULL
;
3376 (void) in6_mc_leave(im6o
->im6o_membership
[i
], imf
);
3381 IN6M_REMREF(im6o
->im6o_membership
[i
]);
3382 im6o
->im6o_membership
[i
] = NULL
;
3384 im6o
->im6o_num_memberships
= 0;
3385 if (im6o
->im6o_mfilters
!= NULL
) {
3386 FREE(im6o
->im6o_mfilters
, M_IN6MFILTER
);
3387 im6o
->im6o_mfilters
= NULL
;
3389 if (im6o
->im6o_membership
!= NULL
) {
3390 FREE(im6o
->im6o_membership
, M_IP6MOPTS
);
3391 im6o
->im6o_membership
= NULL
;
3395 lck_mtx_destroy(&im6o
->im6o_lock
, ifa_mtx_grp
);
3397 if (!(im6o
->im6o_debug
& IFD_ALLOC
)) {
3398 panic("%s: im6o %p cannot be freed", __func__
, im6o
);
3401 zfree(im6o_zone
, im6o
);
3405 im6o_trace(struct ip6_moptions
*im6o
, int refhold
)
3407 struct ip6_moptions_dbg
*im6o_dbg
= (struct ip6_moptions_dbg
*)im6o
;
3412 if (!(im6o
->im6o_debug
& IFD_DEBUG
)) {
3413 panic("%s: im6o %p has no debug structure", __func__
, im6o
);
3417 cnt
= &im6o_dbg
->im6o_refhold_cnt
;
3418 tr
= im6o_dbg
->im6o_refhold
;
3420 cnt
= &im6o_dbg
->im6o_refrele_cnt
;
3421 tr
= im6o_dbg
->im6o_refrele
;
3424 idx
= atomic_add_16_ov(cnt
, 1) % IM6O_TRACE_HIST_SIZE
;
3425 ctrace_record(&tr
[idx
]);
3428 struct ip6_moptions
*
3429 ip6_allocmoptions(int how
)
3431 struct ip6_moptions
*im6o
;
3433 im6o
= (how
== M_WAITOK
) ?
3434 zalloc(im6o_zone
) : zalloc_noblock(im6o_zone
);
3436 bzero(im6o
, im6o_size
);
3437 lck_mtx_init(&im6o
->im6o_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3438 im6o
->im6o_debug
|= IFD_ALLOC
;
3439 if (im6o_debug
!= 0) {
3440 im6o
->im6o_debug
|= IFD_DEBUG
;
3441 im6o
->im6o_trace
= im6o_trace
;
3450 * Set IPv6 outgoing packet options based on advanced API.
3453 ip6_setpktopts(struct mbuf
*control
, struct ip6_pktopts
*opt
,
3454 struct ip6_pktopts
*stickyopt
, int uproto
)
3456 struct cmsghdr
*cm
= NULL
;
3458 if (control
== NULL
|| opt
== NULL
)
3461 ip6_initpktopts(opt
);
3466 * If stickyopt is provided, make a local copy of the options
3467 * for this particular packet, then override them by ancillary
3469 * XXX: copypktopts() does not copy the cached route to a next
3470 * hop (if any). This is not very good in terms of efficiency,
3471 * but we can allow this since this option should be rarely
3474 if ((error
= copypktopts(opt
, stickyopt
, M_NOWAIT
)) != 0)
3479 * XXX: Currently, we assume all the optional information is stored
3482 if (control
->m_next
)
3485 if (control
->m_len
< CMSG_LEN(0))
3488 for (cm
= M_FIRST_CMSGHDR(control
); cm
!= NULL
;
3489 cm
= M_NXT_CMSGHDR(control
, cm
)) {
3492 if (cm
->cmsg_len
< sizeof (struct cmsghdr
) ||
3493 cm
->cmsg_len
> control
->m_len
)
3495 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
3498 error
= ip6_setpktopt(cm
->cmsg_type
, CMSG_DATA(cm
),
3499 cm
->cmsg_len
- CMSG_LEN(0), opt
, 0, 1, uproto
);
3507 * Set a particular packet option, as a sticky option or an ancillary data
3508 * item. "len" can be 0 only when it's a sticky option.
3509 * We have 4 cases of combination of "sticky" and "cmsg":
3510 * "sticky=0, cmsg=0": impossible
3511 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3512 * "sticky=1, cmsg=0": RFC3542 socket option
3513 * "sticky=1, cmsg=1": RFC2292 socket option
3516 ip6_setpktopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
*opt
,
3517 int sticky
, int cmsg
, int uproto
)
3519 int minmtupolicy
, preftemp
;
3522 if (!sticky
&& !cmsg
) {
3524 printf("ip6_setpktopt: impossible case\n");
3530 * Caller must have ensured that the buffer is at least
3531 * aligned on 32-bit boundary.
3533 VERIFY(IS_P2ALIGNED(buf
, sizeof (u_int32_t
)));
3536 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3537 * not be specified in the context of RFC3542. Conversely,
3538 * RFC3542 types should not be specified in the context of RFC2292.
3542 case IPV6_2292PKTINFO
:
3543 case IPV6_2292HOPLIMIT
:
3544 case IPV6_2292NEXTHOP
:
3545 case IPV6_2292HOPOPTS
:
3546 case IPV6_2292DSTOPTS
:
3547 case IPV6_2292RTHDR
:
3548 case IPV6_2292PKTOPTIONS
:
3549 return (ENOPROTOOPT
);
3552 if (sticky
&& cmsg
) {
3559 case IPV6_RTHDRDSTOPTS
:
3561 case IPV6_USE_MIN_MTU
:
3564 case IPV6_PREFER_TEMPADDR
: /* XXX: not an RFC3542 option */
3565 return (ENOPROTOOPT
);
3570 case IPV6_2292PKTINFO
:
3571 case IPV6_PKTINFO
: {
3572 struct ifnet
*ifp
= NULL
;
3573 struct in6_pktinfo
*pktinfo
;
3575 if (len
!= sizeof (struct in6_pktinfo
))
3578 pktinfo
= (struct in6_pktinfo
*)(void *)buf
;
3581 * An application can clear any sticky IPV6_PKTINFO option by
3582 * doing a "regular" setsockopt with ipi6_addr being
3583 * in6addr_any and ipi6_ifindex being zero.
3584 * [RFC 3542, Section 6]
3586 if (optname
== IPV6_PKTINFO
&& opt
->ip6po_pktinfo
&&
3587 pktinfo
->ipi6_ifindex
== 0 &&
3588 IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3589 ip6_clearpktopts(opt
, optname
);
3593 if (uproto
== IPPROTO_TCP
&& optname
== IPV6_PKTINFO
&&
3594 sticky
&& !IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
3598 /* validate the interface index if specified. */
3599 ifnet_head_lock_shared();
3601 if (pktinfo
->ipi6_ifindex
> if_index
) {
3606 if (pktinfo
->ipi6_ifindex
) {
3607 ifp
= ifindex2ifnet
[pktinfo
->ipi6_ifindex
];
3617 * We store the address anyway, and let in6_selectsrc()
3618 * validate the specified address. This is because ipi6_addr
3619 * may not have enough information about its scope zone, and
3620 * we may need additional information (such as outgoing
3621 * interface or the scope zone of a destination address) to
3622 * disambiguate the scope.
3623 * XXX: the delay of the validation may confuse the
3624 * application when it is used as a sticky option.
3626 if (opt
->ip6po_pktinfo
== NULL
) {
3627 opt
->ip6po_pktinfo
= _MALLOC(sizeof (*pktinfo
),
3628 M_IP6OPT
, M_NOWAIT
);
3629 if (opt
->ip6po_pktinfo
== NULL
)
3632 bcopy(pktinfo
, opt
->ip6po_pktinfo
, sizeof (*pktinfo
));
3636 case IPV6_2292HOPLIMIT
:
3637 case IPV6_HOPLIMIT
: {
3641 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3642 * to simplify the ordering among hoplimit options.
3644 if (optname
== IPV6_HOPLIMIT
&& sticky
)
3645 return (ENOPROTOOPT
);
3647 if (len
!= sizeof (int))
3649 hlimp
= (int *)(void *)buf
;
3650 if (*hlimp
< -1 || *hlimp
> 255)
3653 opt
->ip6po_hlim
= *hlimp
;
3660 if (len
!= sizeof (int))
3662 tclass
= *(int *)(void *)buf
;
3663 if (tclass
< -1 || tclass
> 255)
3666 opt
->ip6po_tclass
= tclass
;
3670 case IPV6_2292NEXTHOP
:
3672 error
= suser(kauth_cred_get(), 0);
3676 if (len
== 0) { /* just remove the option */
3677 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3681 /* check if cmsg_len is large enough for sa_len */
3682 if (len
< sizeof (struct sockaddr
) || len
< *buf
)
3685 switch (SA(buf
)->sa_family
) {
3687 struct sockaddr_in6
*sa6
= SIN6(buf
);
3689 if (sa6
->sin6_len
!= sizeof (struct sockaddr_in6
))
3692 if (IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
) ||
3693 IN6_IS_ADDR_MULTICAST(&sa6
->sin6_addr
)) {
3696 if ((error
= sa6_embedscope(sa6
, ip6_use_defzone
))
3702 case AF_LINK
: /* should eventually be supported */
3704 return (EAFNOSUPPORT
);
3707 /* turn off the previous option, then set the new option. */
3708 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3709 opt
->ip6po_nexthop
= _MALLOC(*buf
, M_IP6OPT
, M_NOWAIT
);
3710 if (opt
->ip6po_nexthop
== NULL
)
3712 bcopy(buf
, opt
->ip6po_nexthop
, *buf
);
3715 case IPV6_2292HOPOPTS
:
3716 case IPV6_HOPOPTS
: {
3717 struct ip6_hbh
*hbh
;
3721 * XXX: We don't allow a non-privileged user to set ANY HbH
3722 * options, since per-option restriction has too much
3725 error
= suser(kauth_cred_get(), 0);
3730 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3731 break; /* just remove the option */
3734 /* message length validation */
3735 if (len
< sizeof (struct ip6_hbh
))
3737 hbh
= (struct ip6_hbh
*)(void *)buf
;
3738 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
3742 /* turn off the previous option, then set the new option. */
3743 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3744 opt
->ip6po_hbh
= _MALLOC(hbhlen
, M_IP6OPT
, M_NOWAIT
);
3745 if (opt
->ip6po_hbh
== NULL
)
3747 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
3752 case IPV6_2292DSTOPTS
:
3754 case IPV6_RTHDRDSTOPTS
: {
3755 struct ip6_dest
*dest
, **newdest
= NULL
;
3758 error
= suser(kauth_cred_get(), 0);
3763 ip6_clearpktopts(opt
, optname
);
3764 break; /* just remove the option */
3767 /* message length validation */
3768 if (len
< sizeof (struct ip6_dest
))
3770 dest
= (struct ip6_dest
*)(void *)buf
;
3771 destlen
= (dest
->ip6d_len
+ 1) << 3;
3776 * Determine the position that the destination options header
3777 * should be inserted; before or after the routing header.
3780 case IPV6_2292DSTOPTS
:
3782 * The old advacned API is ambiguous on this point.
3783 * Our approach is to determine the position based
3784 * according to the existence of a routing header.
3785 * Note, however, that this depends on the order of the
3786 * extension headers in the ancillary data; the 1st
3787 * part of the destination options header must appear
3788 * before the routing header in the ancillary data,
3790 * RFC3542 solved the ambiguity by introducing
3791 * separate ancillary data or option types.
3793 if (opt
->ip6po_rthdr
== NULL
)
3794 newdest
= &opt
->ip6po_dest1
;
3796 newdest
= &opt
->ip6po_dest2
;
3798 case IPV6_RTHDRDSTOPTS
:
3799 newdest
= &opt
->ip6po_dest1
;
3802 newdest
= &opt
->ip6po_dest2
;
3806 /* turn off the previous option, then set the new option. */
3807 ip6_clearpktopts(opt
, optname
);
3808 *newdest
= _MALLOC(destlen
, M_IP6OPT
, M_NOWAIT
);
3809 if (*newdest
== NULL
)
3811 bcopy(dest
, *newdest
, destlen
);
3815 case IPV6_2292RTHDR
:
3817 struct ip6_rthdr
*rth
;
3821 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3822 break; /* just remove the option */
3825 /* message length validation */
3826 if (len
< sizeof (struct ip6_rthdr
))
3828 rth
= (struct ip6_rthdr
*)(void *)buf
;
3829 rthlen
= (rth
->ip6r_len
+ 1) << 3;
3833 switch (rth
->ip6r_type
) {
3834 case IPV6_RTHDR_TYPE_0
:
3835 if (rth
->ip6r_len
== 0) /* must contain one addr */
3837 if (rth
->ip6r_len
% 2) /* length must be even */
3839 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
3843 return (EINVAL
); /* not supported */
3846 /* turn off the previous option */
3847 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3848 opt
->ip6po_rthdr
= _MALLOC(rthlen
, M_IP6OPT
, M_NOWAIT
);
3849 if (opt
->ip6po_rthdr
== NULL
)
3851 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
3855 case IPV6_USE_MIN_MTU
:
3856 if (len
!= sizeof (int))
3858 minmtupolicy
= *(int *)(void *)buf
;
3859 if (minmtupolicy
!= IP6PO_MINMTU_MCASTONLY
&&
3860 minmtupolicy
!= IP6PO_MINMTU_DISABLE
&&
3861 minmtupolicy
!= IP6PO_MINMTU_ALL
) {
3864 opt
->ip6po_minmtu
= minmtupolicy
;
3868 if (len
!= sizeof (int))
3871 if (uproto
== IPPROTO_TCP
|| *(int *)(void *)buf
== 0) {
3873 * we ignore this option for TCP sockets.
3874 * (RFC3542 leaves this case unspecified.)
3876 opt
->ip6po_flags
&= ~IP6PO_DONTFRAG
;
3878 opt
->ip6po_flags
|= IP6PO_DONTFRAG
;
3882 case IPV6_PREFER_TEMPADDR
:
3883 if (len
!= sizeof (int))
3885 preftemp
= *(int *)(void *)buf
;
3886 if (preftemp
!= IP6PO_TEMPADDR_SYSTEM
&&
3887 preftemp
!= IP6PO_TEMPADDR_NOTPREFER
&&
3888 preftemp
!= IP6PO_TEMPADDR_PREFER
) {
3891 opt
->ip6po_prefer_tempaddr
= preftemp
;
3895 return (ENOPROTOOPT
);
3896 } /* end of switch */
3902 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3903 * packet to the input queue of a specified interface. Note that this
3904 * calls the output routine of the loopback "driver", but with an interface
3905 * pointer that might NOT be &loif -- easier than replicating that code here.
3908 ip6_mloopback(struct ifnet
*srcifp
, struct ifnet
*origifp
, struct mbuf
*m
,
3909 struct sockaddr_in6
*dst
, uint32_t optlen
, int32_t nxt0
)
3912 struct ip6_hdr
*ip6
;
3913 struct in6_addr src
;
3919 * Copy the packet header as it's needed for the checksum.
3920 * Make sure to deep-copy IPv6 header portion in case the data
3921 * is in an mbuf cluster, so that we can safely override the IPv6
3922 * header portion later.
3924 copym
= m_copym_mode(m
, 0, M_COPYALL
, M_DONTWAIT
, M_COPYM_COPY_HDR
);
3925 if (copym
!= NULL
&& ((copym
->m_flags
& M_EXT
) ||
3926 copym
->m_len
< sizeof (struct ip6_hdr
)))
3927 copym
= m_pullup(copym
, sizeof (struct ip6_hdr
));
3932 ip6
= mtod(copym
, struct ip6_hdr
*);
3935 * clear embedded scope identifiers if necessary.
3936 * in6_clearscope will touch the addresses only when necessary.
3938 in6_clearscope(&ip6
->ip6_src
);
3939 in6_clearscope(&ip6
->ip6_dst
);
3941 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
)
3942 in6_delayed_cksum_offset(copym
, 0, optlen
, nxt0
);
3945 * Stuff the 'real' ifp into the pkthdr, to be used in matching
3946 * in ip6_input(); we need the loopback ifp/dl_tag passed as args
3947 * to make the loopback driver compliant with the data link
3950 copym
->m_pkthdr
.rcvif
= origifp
;
3953 * Also record the source interface (which owns the source address).
3954 * This is basically a stripped down version of ifa_foraddr6().
3956 if (srcifp
== NULL
) {
3957 struct in6_ifaddr
*ia
;
3959 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
3960 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= ia
->ia_next
) {
3961 IFA_LOCK_SPIN(&ia
->ia_ifa
);
3962 /* compare against src addr with embedded scope */
3963 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, &src
)) {
3964 srcifp
= ia
->ia_ifp
;
3965 IFA_UNLOCK(&ia
->ia_ifa
);
3968 IFA_UNLOCK(&ia
->ia_ifa
);
3970 lck_rw_done(&in6_ifaddr_rwlock
);
3973 ip6_setsrcifaddr_info(copym
, srcifp
->if_index
, NULL
);
3974 ip6_setdstifaddr_info(copym
, origifp
->if_index
, NULL
);
3976 dlil_output(lo_ifp
, PF_INET6
, copym
, NULL
, SA(dst
), 0, NULL
);
3980 * Chop IPv6 header off from the payload.
3983 ip6_splithdr(struct mbuf
*m
, struct ip6_exthdrs
*exthdrs
)
3986 struct ip6_hdr
*ip6
;
3988 ip6
= mtod(m
, struct ip6_hdr
*);
3989 if (m
->m_len
> sizeof (*ip6
)) {
3990 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
3995 M_COPY_PKTHDR(mh
, m
);
3996 MH_ALIGN(mh
, sizeof (*ip6
));
3997 m
->m_flags
&= ~M_PKTHDR
;
3998 m
->m_len
-= sizeof (*ip6
);
3999 m
->m_data
+= sizeof (*ip6
);
4002 m
->m_len
= sizeof (*ip6
);
4003 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof (*ip6
));
4005 exthdrs
->ip6e_ip6
= m
;
4010 ip6_output_checksum(struct ifnet
*ifp
, uint32_t mtu
, struct mbuf
*m
,
4011 int nxt0
, uint32_t tlen
, uint32_t optlen
)
4013 uint32_t sw_csum
, hwcap
= ifp
->if_hwassist
;
4014 int tso
= TSO_IPV6_OK(ifp
, m
);
4017 /* do all in software; checksum offload is disabled */
4018 sw_csum
= CSUM_DELAY_IPV6_DATA
& m
->m_pkthdr
.csum_flags
;
4020 /* do in software what the hardware cannot */
4021 sw_csum
= m
->m_pkthdr
.csum_flags
&
4022 ~IF_HWASSIST_CSUM_FLAGS(hwcap
);
4026 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
4027 m
->m_pkthdr
.csum_flags
);
4028 } else if (!(sw_csum
& CSUM_DELAY_IPV6_DATA
) &&
4029 (hwcap
& CSUM_PARTIAL
)) {
4031 * Partial checksum offload, ere), if no extension
4032 * headers, and TCP only (no UDP support, as the
4033 * hardware may not be able to convert +0 to
4034 * -0 (0xffff) per RFC1122 4.1.3.4.)
4036 if (hwcksum_tx
&& !tso
&&
4037 (m
->m_pkthdr
.csum_flags
& CSUM_TCPIPV6
) &&
4039 uint16_t start
= sizeof (struct ip6_hdr
);
4041 m
->m_pkthdr
.csum_data
& 0xffff;
4042 m
->m_pkthdr
.csum_flags
|=
4043 (CSUM_DATA_VALID
| CSUM_PARTIAL
);
4044 m
->m_pkthdr
.csum_tx_stuff
= (ulpoff
+ start
);
4045 m
->m_pkthdr
.csum_tx_start
= start
;
4048 sw_csum
|= (CSUM_DELAY_IPV6_DATA
&
4049 m
->m_pkthdr
.csum_flags
);
4053 if (sw_csum
& CSUM_DELAY_IPV6_DATA
) {
4054 in6_delayed_cksum_offset(m
, 0, optlen
, nxt0
);
4055 sw_csum
&= ~CSUM_DELAY_IPV6_DATA
;
4060 * Drop off bits that aren't supported by hardware;
4061 * also make sure to preserve non-checksum related bits.
4063 m
->m_pkthdr
.csum_flags
=
4064 ((m
->m_pkthdr
.csum_flags
&
4065 (IF_HWASSIST_CSUM_FLAGS(hwcap
) | CSUM_DATA_VALID
)) |
4066 (m
->m_pkthdr
.csum_flags
& ~IF_HWASSIST_CSUM_MASK
));
4068 /* drop all bits; checksum offload is disabled */
4069 m
->m_pkthdr
.csum_flags
= 0;
4074 * Compute IPv6 extension header length.
4077 ip6_optlen(struct in6pcb
*in6p
)
4081 if (!in6p
->in6p_outputopts
)
4086 (((struct ip6_ext *)(x)) ? \
4087 (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
4089 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
4090 if (in6p
->in6p_outputopts
->ip6po_rthdr
) {
4091 /* dest1 is valid with rthdr only */
4092 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
4094 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
4095 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);
4101 sysctl_reset_ip6_output_stats SYSCTL_HANDLER_ARGS
4103 #pragma unused(arg1, arg2)
4106 i
= ip6_output_measure
;
4107 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
4108 if (error
|| req
->newptr
== USER_ADDR_NULL
)
4111 if (i
< 0 || i
> 1) {
4115 if (ip6_output_measure
!= i
&& i
== 1) {
4116 net_perf_initialize(&net_perf
, ip6_output_measure_bins
);
4118 ip6_output_measure
= i
;
4124 sysctl_ip6_output_measure_bins SYSCTL_HANDLER_ARGS
4126 #pragma unused(arg1, arg2)
4130 i
= ip6_output_measure_bins
;
4131 error
= sysctl_handle_quad(oidp
, &i
, 0, req
);
4132 if (error
|| req
->newptr
== USER_ADDR_NULL
)
4135 if (!net_perf_validate_bins(i
)) {
4139 ip6_output_measure_bins
= i
;
4145 sysctl_ip6_output_getperf SYSCTL_HANDLER_ARGS
4147 #pragma unused(oidp, arg1, arg2)
4148 if (req
->oldptr
== USER_ADDR_NULL
)
4149 req
->oldlen
= (size_t)sizeof (struct ipstat
);
4151 return (SYSCTL_OUT(req
, &net_perf
, MIN(sizeof (net_perf
), req
->oldlen
)));