2 * Copyright (c) 2003-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Copyright (c) 1982, 1986, 1988, 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_input.c 8.2 (Berkeley) 1/4/94
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/malloc.h>
97 #include <sys/domain.h>
98 #include <sys/protosw.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/errno.h>
102 #include <sys/time.h>
103 #include <sys/kernel.h>
104 #include <sys/syslog.h>
105 #include <sys/sysctl.h>
106 #include <sys/proc.h>
107 #include <sys/kauth.h>
108 #include <sys/mcache.h>
110 #include <mach/mach_time.h>
111 #include <mach/sdt.h>
112 #include <pexpert/pexpert.h>
113 #include <dev/random/randomdev.h>
116 #include <net/if_var.h>
117 #include <net/if_types.h>
118 #include <net/if_dl.h>
119 #include <net/route.h>
120 #include <net/kpi_protocol.h>
121 #include <net/ntstat.h>
122 #include <net/init.h>
123 #include <net/net_osdep.h>
124 #include <net/net_perf.h>
126 #include <netinet/in.h>
127 #include <netinet/in_systm.h>
129 #include <netinet/ip.h>
130 #include <netinet/ip_icmp.h>
132 #include <netinet/kpi_ipfilter_var.h>
133 #include <netinet/ip6.h>
134 #include <netinet/udp.h>
135 #include <netinet6/in6_var.h>
136 #include <netinet6/ip6_var.h>
137 #include <netinet/in_pcb.h>
138 #include <netinet/icmp6.h>
139 #include <netinet6/in6_ifattach.h>
140 #include <netinet6/nd6.h>
141 #include <netinet6/scope6_var.h>
142 #include <netinet6/ip6protosw.h>
145 #include <netinet6/ipsec.h>
146 #include <netinet6/ipsec6.h>
147 extern int ipsec_bypass
;
151 #include <netinet/ip_dummynet.h>
152 #endif /* DUMMYNET */
154 /* we need it for NLOOP. */
158 #include <net/pfvar.h>
163 struct ip6protosw
*ip6_protox
[IPPROTO_MAX
];
165 static lck_grp_attr_t
*in6_ifaddr_rwlock_grp_attr
;
166 static lck_grp_t
*in6_ifaddr_rwlock_grp
;
167 static lck_attr_t
*in6_ifaddr_rwlock_attr
;
168 decl_lck_rw_data(, in6_ifaddr_rwlock
);
170 /* Protected by in6_ifaddr_rwlock */
171 struct in6_ifaddrhead in6_ifaddrhead
;
172 struct in6_ifaddrhashhead
* in6_ifaddrhashtbl
;
173 uint32_t in6_ifaddrhmask
;
175 #define IN6ADDR_NHASH 61
176 u_int32_t in6addr_nhash
= 0; /* hash table size */
177 u_int32_t in6addr_hashp
= 0; /* next largest prime */
180 #define IN6_IFSTAT_REQUIRE_ALIGNED_64(f) \
181 _CASSERT(!(offsetof(struct in6_ifstat, f) % sizeof (uint64_t)))
183 #define ICMP6_IFSTAT_REQUIRE_ALIGNED_64(f) \
184 _CASSERT(!(offsetof(struct icmp6_ifstat, f) % sizeof (uint64_t)))
186 struct ip6stat ip6stat
;
188 decl_lck_mtx_data(, proxy6_lock
);
189 decl_lck_mtx_data(static, dad6_mutex_data
);
190 decl_lck_mtx_data(static, nd6_mutex_data
);
191 decl_lck_mtx_data(static, prefix6_mutex_data
);
192 lck_mtx_t
*dad6_mutex
= &dad6_mutex_data
;
193 lck_mtx_t
*nd6_mutex
= &nd6_mutex_data
;
194 lck_mtx_t
*prefix6_mutex
= &prefix6_mutex_data
;
195 #ifdef ENABLE_ADDRSEL
196 decl_lck_mtx_data(static, addrsel_mutex_data
);
197 lck_mtx_t
*addrsel_mutex
= &addrsel_mutex_data
;
199 static lck_attr_t
*ip6_mutex_attr
;
200 static lck_grp_t
*ip6_mutex_grp
;
201 static lck_grp_attr_t
*ip6_mutex_grp_attr
;
203 extern int loopattach_done
;
204 extern void addrsel_policy_init(void);
206 static int sysctl_reset_ip6_input_stats SYSCTL_HANDLER_ARGS
;
207 static int sysctl_ip6_input_measure_bins SYSCTL_HANDLER_ARGS
;
208 static int sysctl_ip6_input_getperf SYSCTL_HANDLER_ARGS
;
209 static void ip6_init_delayed(void);
210 static int ip6_hopopts_input(u_int32_t
*, u_int32_t
*, struct mbuf
**, int *);
212 static void in6_ifaddrhashtbl_init(void);
215 extern void stfattach(void);
218 SYSCTL_DECL(_net_inet6_ip6
);
220 static uint32_t ip6_adj_clear_hwcksum
= 0;
221 SYSCTL_UINT(_net_inet6_ip6
, OID_AUTO
, adj_clear_hwcksum
,
222 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_adj_clear_hwcksum
, 0,
223 "Invalidate hwcksum info when adjusting length");
225 static uint32_t ip6_adj_partial_sum
= 1;
226 SYSCTL_UINT(_net_inet6_ip6
, OID_AUTO
, adj_partial_sum
,
227 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_adj_partial_sum
, 0,
228 "Perform partial sum adjustment of trailing bytes at IP layer");
230 static int ip6_input_measure
= 0;
231 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, input_perf
,
232 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
233 &ip6_input_measure
, 0, sysctl_reset_ip6_input_stats
, "I", "Do time measurement");
235 static uint64_t ip6_input_measure_bins
= 0;
236 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, input_perf_bins
,
237 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_input_measure_bins
, 0,
238 sysctl_ip6_input_measure_bins
, "I",
239 "bins for chaining performance data histogram");
241 static net_perf_t net_perf
;
242 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, input_perf_data
,
243 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
244 0, 0, sysctl_ip6_input_getperf
, "S,net_perf",
245 "IP6 input performance data (struct net_perf, net/net_perf.h)");
248 * ip6_checkinterface controls the receive side of the models for multihoming
249 * that are discussed in RFC 1122.
251 * sysctl_ip6_checkinterface values are:
252 * IP6_CHECKINTERFACE_WEAK_ES:
253 * This corresponds to the Weak End-System model where incoming packets from
254 * any interface are accepted provided the destination address of the incoming packet
255 * is assigned to some interface.
257 * IP6_CHECKINTERFACE_HYBRID_ES:
258 * The Hybrid End-System model use the Strong End-System for tunnel interfaces
259 * (ipsec and utun) and the weak End-System model for other interfaces families.
260 * This prevents a rogue middle box to probe for signs of TCP connections
261 * that use the tunnel interface.
263 * IP6_CHECKINTERFACE_STRONG_ES:
264 * The Strong model model requires the packet arrived on an interface that
265 * is assigned the destination address of the packet.
267 * Since the routing table and transmit implementation do not implement the Strong ES model,
268 * setting this to a value different from IP6_CHECKINTERFACE_WEAK_ES may lead to unexpected results.
270 * When forwarding is enabled, the system reverts to the Weak ES model as a router
271 * is expected by design to receive packets from several interfaces to the same address.
273 #define IP6_CHECKINTERFACE_WEAK_ES 0
274 #define IP6_CHECKINTERFACE_HYBRID_ES 1
275 #define IP6_CHECKINTERFACE_STRONG_ES 2
277 static int ip6_checkinterface
= IP6_CHECKINTERFACE_HYBRID_ES
;
279 static int sysctl_ip6_checkinterface SYSCTL_HANDLER_ARGS
;
280 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, check_interface
,
281 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
282 0, 0, sysctl_ip6_checkinterface
, "I", "Verify packet arrives on correct interface");
284 #if (DEBUG || DEVELOPMENT)
285 #define IP6_CHECK_IFDEBUG 1
287 #define IP6_CHECK_IFDEBUG 0
288 #endif /* (DEBUG || DEVELOPMENT) */
289 static int ip6_checkinterface_debug
= IP6_CHECK_IFDEBUG
;
290 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, checkinterface_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
291 &ip6_checkinterface_debug
, IP6_CHECK_IFDEBUG
, "");
293 typedef enum ip6_check_if_result
{
294 IP6_CHECK_IF_NONE
= 0,
295 IP6_CHECK_IF_OURS
= 1,
296 IP6_CHECK_IF_DROP
= 2,
297 IP6_CHECK_IF_FORWARD
= 3
298 } ip6_check_if_result_t
;
300 static ip6_check_if_result_t
ip6_input_check_interface(struct mbuf
*, struct ip6_hdr
*, struct ifnet
*, struct route_in6
*rin6
, struct ifnet
**);
303 * On platforms which require strict alignment (currently for anything but
304 * i386 or x86_64), check if the IP header pointer is 32-bit aligned; if not,
305 * copy the contents of the mbuf chain into a new chain, and free the original
306 * one. Create some head room in the first mbuf of the new chain, in case
307 * it's needed later on.
309 * RFC 2460 says that IPv6 headers are 64-bit aligned, but network interfaces
310 * mostly align to 32-bit boundaries. Care should be taken never to use 64-bit
311 * load/store operations on the fields in IPv6 headers.
313 #if defined(__i386__) || defined(__x86_64__)
314 #define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0)
315 #else /* !__i386__ && !__x86_64__ */
316 #define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { \
317 if (!IP6_HDR_ALIGNED_P(mtod(_m, caddr_t))) { \
319 struct ifnet *__ifp = (_ifp); \
320 atomic_add_64(&(__ifp)->if_alignerrs, 1); \
321 if (((_m)->m_flags & M_PKTHDR) && \
322 (_m)->m_pkthdr.pkt_hdr != NULL) \
323 (_m)->m_pkthdr.pkt_hdr = NULL; \
324 _n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT); \
326 ip6stat.ip6s_toosmall++; \
331 VERIFY(_n != (_m)); \
336 #endif /* !__i386__ && !__x86_64__ */
339 ip6_proto_input(protocol_family_t protocol
, mbuf_t packet
)
341 #pragma unused(protocol)
343 struct timeval start_tv
;
344 if (ip6_input_measure
) {
345 net_perf_start_time(&net_perf
, &start_tv
);
350 if (ip6_input_measure
) {
351 net_perf_measure_time(&net_perf
, &start_tv
, 1);
352 net_perf_histogram(&net_perf
, 1);
358 * IP6 initialization: fill in IP6 protocol switch table.
359 * All protocols not implemented in kernel go to raw IP6 protocol handler.
362 ip6_init(struct ip6protosw
*pp
, struct domain
*dp
)
364 static int ip6_initialized
= 0;
368 domain_unguard_t unguard
;
370 domain_proto_mtx_lock_assert_held();
371 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
| PR_ATTACHED
)) == PR_ATTACHED
);
373 _CASSERT((sizeof(struct ip6_hdr
) +
374 sizeof(struct icmp6_hdr
)) <= _MHLEN
);
376 if (ip6_initialized
) {
381 eventhandler_lists_ctxt_init(&in6_evhdlr_ctxt
);
382 (void)EVENTHANDLER_REGISTER(&in6_evhdlr_ctxt
, in6_event
,
383 in6_eventhdlr_callback
, eventhandler_entry_dummy_arg
,
384 EVENTHANDLER_PRI_ANY
);
386 eventhandler_lists_ctxt_init(&in6_clat46_evhdlr_ctxt
);
387 (void)EVENTHANDLER_REGISTER(&in6_clat46_evhdlr_ctxt
, in6_clat46_event
,
388 in6_clat46_eventhdlr_callback
, eventhandler_entry_dummy_arg
,
389 EVENTHANDLER_PRI_ANY
);
391 for (i
= 0; i
< IN6_EVENT_MAX
; i
++) {
392 VERIFY(in6_event2kev_array
[i
].in6_event_code
== i
);
395 pr
= pffindproto_locked(PF_INET6
, IPPROTO_RAW
, SOCK_RAW
);
397 panic("%s: Unable to find [PF_INET6,IPPROTO_RAW,SOCK_RAW]\n",
402 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
403 for (i
= 0; i
< IPPROTO_MAX
; i
++) {
404 ip6_protox
[i
] = (struct ip6protosw
*)pr
;
407 * Cycle through IP protocols and put them into the appropriate place
408 * in ip6_protox[], skipping protocols IPPROTO_{IP,RAW}.
410 VERIFY(dp
== inet6domain
&& dp
->dom_family
== PF_INET6
);
411 TAILQ_FOREACH(pr
, &dp
->dom_protosw
, pr_entry
) {
412 VERIFY(pr
->pr_domain
== dp
);
413 if (pr
->pr_protocol
!= 0 && pr
->pr_protocol
!= IPPROTO_RAW
) {
414 /* Be careful to only index valid IP protocols. */
415 if (pr
->pr_protocol
< IPPROTO_MAX
) {
416 ip6_protox
[pr
->pr_protocol
] =
417 (struct ip6protosw
*)pr
;
422 ip6_mutex_grp_attr
= lck_grp_attr_alloc_init();
424 ip6_mutex_grp
= lck_grp_alloc_init("ip6", ip6_mutex_grp_attr
);
425 ip6_mutex_attr
= lck_attr_alloc_init();
427 lck_mtx_init(dad6_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
428 lck_mtx_init(nd6_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
429 lck_mtx_init(prefix6_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
430 scope6_init(ip6_mutex_grp
, ip6_mutex_attr
);
432 #ifdef ENABLE_ADDRSEL
433 lck_mtx_init(addrsel_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
436 lck_mtx_init(&proxy6_lock
, ip6_mutex_grp
, ip6_mutex_attr
);
438 in6_ifaddr_rwlock_grp_attr
= lck_grp_attr_alloc_init();
439 in6_ifaddr_rwlock_grp
= lck_grp_alloc_init("in6_ifaddr_rwlock",
440 in6_ifaddr_rwlock_grp_attr
);
441 in6_ifaddr_rwlock_attr
= lck_attr_alloc_init();
442 lck_rw_init(&in6_ifaddr_rwlock
, in6_ifaddr_rwlock_grp
,
443 in6_ifaddr_rwlock_attr
);
445 TAILQ_INIT(&in6_ifaddrhead
);
446 in6_ifaddrhashtbl_init();
448 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_receive
);
449 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_hdrerr
);
450 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_toobig
);
451 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_noroute
);
452 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_addrerr
);
453 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_protounknown
);
454 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_truncated
);
455 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_discard
);
456 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_deliver
);
457 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_forward
);
458 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_request
);
459 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_discard
);
460 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragok
);
461 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragfail
);
462 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragcreat
);
463 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_reqd
);
464 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_ok
);
465 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_fail
);
466 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mcast
);
467 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mcast
);
469 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_msg
);
470 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_error
);
471 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_dstunreach
);
472 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_adminprohib
);
473 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_timeexceed
);
474 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_paramprob
);
475 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_pkttoobig
);
476 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echo
);
477 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echoreply
);
478 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routersolicit
);
479 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routeradvert
);
480 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighborsolicit
);
481 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighboradvert
);
482 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_redirect
);
483 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldquery
);
484 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldreport
);
485 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mlddone
);
487 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_msg
);
488 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_error
);
489 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_dstunreach
);
490 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_adminprohib
);
491 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_timeexceed
);
492 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_paramprob
);
493 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_pkttoobig
);
494 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echo
);
495 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echoreply
);
496 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routersolicit
);
497 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routeradvert
);
498 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighborsolicit
);
499 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighboradvert
);
500 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_redirect
);
501 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldquery
);
502 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldreport
);
503 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mlddone
);
507 (RandomULong() ^ tv
.tv_usec
) % MAX_TEMP_DESYNC_FACTOR
;
509 PE_parse_boot_argn("ip6_checkinterface", &i
, sizeof(i
));
511 case IP6_CHECKINTERFACE_WEAK_ES
:
512 case IP6_CHECKINTERFACE_HYBRID_ES
:
513 case IP6_CHECKINTERFACE_STRONG_ES
:
514 ip6_checkinterface
= i
;
524 icmp6_init(NULL
, dp
);
525 addrsel_policy_init();
528 * P2P interfaces often route the local address to the loopback
529 * interface. At this point, lo0 hasn't been initialized yet, which
530 * means that we need to delay the IPv6 configuration of lo0.
532 net_init_add(ip6_init_delayed
);
534 unguard
= domain_unguard_deploy();
535 i
= proto_register_input(PF_INET6
, ip6_proto_input
, NULL
, 0);
537 panic("%s: failed to register PF_INET6 protocol: %d\n",
541 domain_unguard_release(unguard
);
545 ip6_init_delayed(void)
547 (void) in6_ifattach_prelim(lo_ifp
);
549 /* timer for regeneranation of temporary addresses randomize ID */
550 timeout(in6_tmpaddrtimer
, NULL
,
551 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
552 ip6_temp_regen_advance
) * hz
);
560 ip6_input_adjust(struct mbuf
*m
, struct ip6_hdr
*ip6
, uint32_t plen
,
563 boolean_t adjust
= TRUE
;
564 uint32_t tot_len
= sizeof(*ip6
) + plen
;
566 ASSERT(m_pktlen(m
) > tot_len
);
569 * Invalidate hardware checksum info if ip6_adj_clear_hwcksum
570 * is set; useful to handle buggy drivers. Note that this
571 * should not be enabled by default, as we may get here due
572 * to link-layer padding.
574 if (ip6_adj_clear_hwcksum
&&
575 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) &&
576 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
577 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
578 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
579 m
->m_pkthdr
.csum_data
= 0;
580 ip6stat
.ip6s_adj_hwcsum_clr
++;
584 * If partial checksum information is available, subtract
585 * out the partial sum of postpended extraneous bytes, and
586 * update the checksum metadata accordingly. By doing it
587 * here, the upper layer transport only needs to adjust any
588 * prepended extraneous bytes (else it will do both.)
590 if (ip6_adj_partial_sum
&&
591 (m
->m_pkthdr
.csum_flags
& (CSUM_DATA_VALID
| CSUM_PARTIAL
)) ==
592 (CSUM_DATA_VALID
| CSUM_PARTIAL
)) {
593 m
->m_pkthdr
.csum_rx_val
= m_adj_sum16(m
,
594 m
->m_pkthdr
.csum_rx_start
, m
->m_pkthdr
.csum_rx_start
,
595 (tot_len
- m
->m_pkthdr
.csum_rx_start
),
596 m
->m_pkthdr
.csum_rx_val
);
597 } else if ((m
->m_pkthdr
.csum_flags
&
598 (CSUM_DATA_VALID
| CSUM_PARTIAL
)) ==
599 (CSUM_DATA_VALID
| CSUM_PARTIAL
)) {
601 * If packet has partial checksum info and we decided not
602 * to subtract the partial sum of postpended extraneous
603 * bytes here (not the default case), leave that work to
604 * be handled by the other layers. For now, only TCP, UDP
605 * layers are capable of dealing with this. For all other
606 * protocols (including fragments), trim and ditch the
607 * partial sum as those layers might not implement partial
608 * checksumming (or adjustment) at all.
610 if (ip6
->ip6_nxt
== IPPROTO_TCP
||
611 ip6
->ip6_nxt
== IPPROTO_UDP
) {
614 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
615 m
->m_pkthdr
.csum_data
= 0;
616 ip6stat
.ip6s_adj_hwcsum_clr
++;
622 if (m
->m_len
== m
->m_pkthdr
.len
) {
624 m
->m_pkthdr
.len
= tot_len
;
626 m_adj(m
, tot_len
- m
->m_pkthdr
.len
);
630 static ip6_check_if_result_t
631 ip6_input_check_interface(struct mbuf
*m
, struct ip6_hdr
*ip6
, struct ifnet
*inifp
, struct route_in6
*rin6
, struct ifnet
**deliverifp
)
633 struct in6_ifaddr
*ia6
= NULL
;
634 struct in6_addr tmp_dst
= ip6
->ip6_dst
; /* copy to avoid unaligned access */
635 struct in6_ifaddr
*best_ia6
= NULL
;
636 ip6_check_if_result_t result
= IP6_CHECK_IF_NONE
;
641 * Check for exact addresses in the hash bucket.
643 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
644 TAILQ_FOREACH(ia6
, IN6ADDR_HASH(&tmp_dst
), ia6_hash
) {
646 * TODO: should we accept loopbacl
648 if (IN6_ARE_ADDR_EQUAL(&ia6
->ia_addr
.sin6_addr
, &tmp_dst
)) {
649 if ((ia6
->ia6_flags
& (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
))) {
653 if (ia6
->ia_ifp
== inifp
) {
655 * TODO: should we also accept locally originated packets
656 * or from loopback ???
661 * Continue the loop in case there's a exact match with another
666 if (best_ia6
!= NULL
) {
667 if (best_ia6
->ia_ifp
!= inifp
&& ip6_forwarding
== 0 &&
668 ((ip6_checkinterface
== IP6_CHECKINTERFACE_HYBRID_ES
&&
669 (best_ia6
->ia_ifp
->if_family
== IFNET_FAMILY_IPSEC
||
670 best_ia6
->ia_ifp
->if_family
== IFNET_FAMILY_UTUN
)) ||
671 ip6_checkinterface
== IP6_CHECKINTERFACE_STRONG_ES
)) {
673 * Drop when interface address check is strict and forwarding
676 result
= IP6_CHECK_IF_DROP
;
678 result
= IP6_CHECK_IF_OURS
;
679 *deliverifp
= best_ia6
->ia_ifp
;
680 ip6_setdstifaddr_info(m
, 0, best_ia6
);
683 lck_rw_done(&in6_ifaddr_rwlock
);
685 if (result
== IP6_CHECK_IF_NONE
) {
687 * Slow path: route lookup.
689 struct sockaddr_in6
*dst6
;
691 dst6
= SIN6(&rin6
->ro_dst
);
692 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
693 dst6
->sin6_family
= AF_INET6
;
694 dst6
->sin6_addr
= ip6
->ip6_dst
;
696 rtalloc_scoped_ign((struct route
*)rin6
,
697 RTF_PRCLONING
, IFSCOPE_NONE
);
698 if (rin6
->ro_rt
!= NULL
) {
699 RT_LOCK_SPIN(rin6
->ro_rt
);
702 #define rt6_key(r) (SIN6((r)->rt_nodes->rn_key))
705 * Accept the packet if the forwarding interface to the destination
706 * according to the routing table is the loopback interface,
707 * unless the associated route has a gateway.
708 * Note that this approach causes to accept a packet if there is a
709 * route to the loopback interface for the destination of the packet.
710 * But we think it's even useful in some situations, e.g. when using
711 * a special daemon which wants to intercept the packet.
713 * XXX: some OSes automatically make a cloned route for the destination
714 * of an outgoing packet. If the outgoing interface of the packet
715 * is a loopback one, the kernel would consider the packet to be
716 * accepted, even if we have no such address assinged on the interface.
717 * We check the cloned flag of the route entry to reject such cases,
718 * assuming that route entries for our own addresses are not made by
719 * cloning (it should be true because in6_addloop explicitly installs
720 * the host route). However, we might have to do an explicit check
721 * while it would be less efficient. Or, should we rather install a
722 * reject route for such a case?
724 if (rin6
->ro_rt
!= NULL
&&
725 (rin6
->ro_rt
->rt_flags
& (RTF_HOST
| RTF_GATEWAY
)) == RTF_HOST
&&
727 !(rin6
->ro_rt
->rt_flags
& RTF_WASCLONED
) &&
729 rin6
->ro_rt
->rt_ifp
->if_type
== IFT_LOOP
) {
730 ia6
= (struct in6_ifaddr
*)rin6
->ro_rt
->rt_ifa
;
732 * Packets to a tentative, duplicated, or somehow invalid
733 * address must not be accepted.
735 * For performance, test without acquiring the address lock;
736 * a lot of things in the address are set once and never
737 * changed (e.g. ia_ifp.)
739 if (!(ia6
->ia6_flags
& IN6_IFF_NOTREADY
)) {
740 /* this address is ready */
741 result
= IP6_CHECK_IF_OURS
;
742 *deliverifp
= ia6
->ia_ifp
; /* correct? */
744 * record dst address information into mbuf.
746 (void) ip6_setdstifaddr_info(m
, 0, ia6
);
750 if (rin6
->ro_rt
!= NULL
) {
751 RT_UNLOCK(rin6
->ro_rt
);
755 if (result
== IP6_CHECK_IF_NONE
) {
756 if (ip6_forwarding
== 0) {
757 result
= IP6_CHECK_IF_DROP
;
759 result
= IP6_CHECK_IF_FORWARD
;
760 ip6_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
764 if (result
== IP6_CHECK_IF_OURS
&& *deliverifp
!= inifp
) {
765 ASSERT(*deliverifp
!= NULL
);
766 ip6stat
.ip6s_rcv_if_weak_match
++;
768 /* Logging is too noisy when forwarding is enabled */
769 if (ip6_checkinterface_debug
!= IP6_CHECKINTERFACE_WEAK_ES
&& ip6_forwarding
!= 0) {
770 char src_str
[MAX_IPv6_STR_LEN
];
771 char dst_str
[MAX_IPv6_STR_LEN
];
773 inet_ntop(AF_INET6
, &ip6
->ip6_src
, src_str
, sizeof(src_str
));
774 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dst_str
, sizeof(dst_str
));
775 os_log_info(OS_LOG_DEFAULT
,
776 "%s: weak ES interface match to %s for packet from %s to %s proto %u received via %s",
777 __func__
, (*deliverifp
)->if_xname
, src_str
, dst_str
, ip6
->ip6_nxt
, inifp
->if_xname
);
779 } else if (result
== IP6_CHECK_IF_DROP
) {
780 ip6stat
.ip6s_rcv_if_no_match
++;
781 if (ip6_checkinterface_debug
> 0) {
782 char src_str
[MAX_IPv6_STR_LEN
];
783 char dst_str
[MAX_IPv6_STR_LEN
];
785 inet_ntop(AF_INET6
, &ip6
->ip6_src
, src_str
, sizeof(src_str
));
786 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dst_str
, sizeof(dst_str
));
787 os_log(OS_LOG_DEFAULT
,
788 "%s: no interface match for packet from %s to %s proto %u received via %s",
789 __func__
, src_str
, dst_str
, ip6
->ip6_nxt
, inifp
->if_xname
);
797 ip6_input(struct mbuf
*m
)
800 int off
= sizeof(struct ip6_hdr
), nest
;
802 u_int32_t rtalert
= ~0;
803 int nxt
= 0, ours
= 0;
804 struct ifnet
*inifp
, *deliverifp
= NULL
;
805 ipfilter_t inject_ipfref
= NULL
;
809 struct ip_fw_args args
= {};
810 #endif /* DUMMYNET */
811 struct route_in6 rin6
= {};
814 * Check if the packet we received is valid after interface filter
817 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
818 inifp
= m
->m_pkthdr
.rcvif
;
819 VERIFY(inifp
!= NULL
);
821 /* Perform IP header alignment fixup, if needed */
822 IP6_HDR_ALIGNMENT_FIXUP(m
, inifp
, return );
824 m
->m_pkthdr
.pkt_flags
&= ~PKTF_FORWARDED
;
827 * should the inner packet be considered authentic?
828 * see comment in ah4_input().
830 m
->m_flags
&= ~M_AUTHIPHDR
;
831 m
->m_flags
&= ~M_AUTHIPDGM
;
835 * make sure we don't have onion peering information into m_aux.
840 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
841 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
842 struct dn_pkt_tag
*dn_tag
;
844 dn_tag
= (struct dn_pkt_tag
*)(tag
+ 1);
846 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
848 m_tag_delete(m
, tag
);
851 if (args
.fwa_pf_rule
) {
852 ip6
= mtod(m
, struct ip6_hdr
*); /* In case PF got disabled */
856 #endif /* DUMMYNET */
859 * No need to proccess packet twice if we've already seen it.
861 inject_ipfref
= ipf_get_inject_filter(m
);
862 if (inject_ipfref
!= NULL
) {
863 ip6
= mtod(m
, struct ip6_hdr
*);
874 if (m
->m_flags
& M_EXT
) {
875 if (m
->m_next
!= NULL
) {
876 ip6stat
.ip6s_mext2m
++;
878 ip6stat
.ip6s_mext1
++;
881 #define M2MMAX (sizeof (ip6stat.ip6s_m2m) / sizeof (ip6stat.ip6s_m2m[0]))
882 if (m
->m_next
!= NULL
) {
883 if (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) {
885 ip6stat
.ip6s_m2m
[ifnet_index(lo_ifp
)]++;
886 } else if (inifp
->if_index
< M2MMAX
) {
887 ip6stat
.ip6s_m2m
[inifp
->if_index
]++;
889 ip6stat
.ip6s_m2m
[0]++;
898 * Drop the packet if IPv6 operation is disabled on the interface.
900 if (inifp
->if_eflags
& IFEF_IPV6_DISABLED
) {
904 in6_ifstat_inc_na(inifp
, ifs6_in_receive
);
905 ip6stat
.ip6s_total
++;
908 * L2 bridge code and some other code can return mbuf chain
909 * that does not conform to KAME requirement. too bad.
910 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
912 if (m
->m_next
!= NULL
&& m
->m_pkthdr
.len
< MCLBYTES
) {
915 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
919 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
920 MCLGET(n
, M_DONTWAIT
);
921 if ((n
->m_flags
& M_EXT
) == 0) {
930 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
931 n
->m_len
= m
->m_pkthdr
.len
;
935 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), { goto done
; });
937 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
938 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == 0) {
939 ip6stat
.ip6s_toosmall
++;
940 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
945 ip6
= mtod(m
, struct ip6_hdr
*);
947 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
948 ip6stat
.ip6s_badvers
++;
949 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
953 ip6stat
.ip6s_nxthist
[ip6
->ip6_nxt
]++;
956 * Check against address spoofing/corruption.
958 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) &&
959 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
)) {
960 ip6stat
.ip6s_badscope
++;
961 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
964 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
) ||
965 IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_dst
)) {
967 * XXX: "badscope" is not very suitable for a multicast source.
969 ip6stat
.ip6s_badscope
++;
970 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
973 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
) &&
974 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
976 * In this case, the packet should come from the loopback
977 * interface. However, we cannot just check the if_flags,
978 * because ip6_mloopback() passes the "actual" interface
979 * as the outgoing/incoming interface.
981 ip6stat
.ip6s_badscope
++;
982 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
987 * The following check is not documented in specs. A malicious
988 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
989 * and bypass security checks (act as if it was from 127.0.0.1 by using
990 * IPv6 src ::ffff:127.0.0.1). Be cautious.
992 * This check chokes if we are in an SIIT cloud. As none of BSDs
993 * support IPv4-less kernel compilation, we cannot support SIIT
994 * environment at all. So, it makes more sense for us to reject any
995 * malicious packets for non-SIIT environment, than try to do a
996 * partial support for SIIT environment.
998 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
999 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
1000 ip6stat
.ip6s_badscope
++;
1001 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
1006 * Reject packets with IPv4 compatible addresses (auto tunnel).
1008 * The code forbids auto tunnel relay case in RFC1933 (the check is
1009 * stronger than RFC1933). We may want to re-enable it if mech-xx
1010 * is revised to forbid relaying case.
1012 if (IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_src
) ||
1013 IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_dst
)) {
1014 ip6stat
.ip6s_badscope
++;
1015 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
1021 * Naively assume we can attribute inbound data to the route we would
1022 * use to send to this destination. Asymetric routing breaks this
1023 * assumption, but it still allows us to account for traffic from
1024 * a remote node in the routing table.
1025 * this has a very significant performance impact so we bypass
1026 * if nstat_collect is disabled. We may also bypass if the
1027 * protocol is tcp in the future because tcp will have a route that
1028 * we can use to attribute the data to. That does mean we would not
1029 * account for forwarded tcp traffic.
1031 if (nstat_collect
) {
1032 struct rtentry
*rte
=
1033 ifnet_cached_rtlookup_inet6(inifp
, &ip6
->ip6_src
);
1035 nstat_route_rx(rte
, 1, m
->m_pkthdr
.len
, 0);
1042 #endif /* DUMMYNET */
1044 /* Invoke inbound packet filter */
1045 if (PF_IS_ENABLED
) {
1048 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET6
, TRUE
, &args
);
1049 #else /* !DUMMYNET */
1050 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET6
, TRUE
, NULL
);
1051 #endif /* !DUMMYNET */
1052 if (error
!= 0 || m
== NULL
) {
1054 panic("%s: unexpected packet %p\n",
1058 /* Already freed by callee */
1061 ip6
= mtod(m
, struct ip6_hdr
*);
1065 /* drop packets if interface ID portion is already filled */
1066 if (!(inifp
->if_flags
& IFF_LOOPBACK
) &&
1067 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1068 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
) &&
1069 ip6
->ip6_src
.s6_addr16
[1]) {
1070 ip6stat
.ip6s_badscope
++;
1073 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
) &&
1074 ip6
->ip6_dst
.s6_addr16
[1]) {
1075 ip6stat
.ip6s_badscope
++;
1080 if (m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
) {
1081 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1082 ip6
->ip6_src
.s6_addr16
[1] =
1083 htons(m
->m_pkthdr
.src_ifindex
);
1085 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
1086 ip6
->ip6_dst
.s6_addr16
[1] =
1087 htons(m
->m_pkthdr
.dst_ifindex
);
1090 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1091 ip6
->ip6_src
.s6_addr16
[1] = htons(inifp
->if_index
);
1093 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
1094 ip6
->ip6_dst
.s6_addr16
[1] = htons(inifp
->if_index
);
1101 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1102 struct in6_multi
*in6m
= NULL
;
1104 in6_ifstat_inc_na(inifp
, ifs6_in_mcast
);
1106 * See if we belong to the destination multicast group on the
1107 * arrival interface.
1109 in6_multihead_lock_shared();
1110 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, inifp
, in6m
);
1111 in6_multihead_lock_done();
1115 } else if (!nd6_prproxy
) {
1116 ip6stat
.ip6s_notmember
++;
1117 ip6stat
.ip6s_cantforward
++;
1118 in6_ifstat_inc(inifp
, ifs6_in_discard
);
1123 * record dst address information into mbuf, if we don't have one yet.
1124 * note that we are unable to record it, if the address is not listed
1125 * as our interface address (e.g. multicast addresses, etc.)
1127 if (deliverifp
!= NULL
) {
1128 struct in6_ifaddr
*ia6
= NULL
;
1130 ia6
= in6_ifawithifp(deliverifp
, &ip6
->ip6_dst
);
1132 (void) ip6_setdstifaddr_info(m
, 0, ia6
);
1133 IFA_REMREF(&ia6
->ia_ifa
);
1135 (void) ip6_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
1143 ip6_check_if_result_t check_if_result
= IP6_CHECK_IF_NONE
;
1144 check_if_result
= ip6_input_check_interface(m
, ip6
, inifp
, &rin6
, &deliverifp
);
1145 ASSERT(check_if_result
!= IP6_CHECK_IF_NONE
);
1146 if (check_if_result
== IP6_CHECK_IF_OURS
) {
1149 } else if (check_if_result
== IP6_CHECK_IF_DROP
) {
1155 * Now there is no reason to process the packet if it's not our own
1156 * and we're not a router.
1158 if (!ip6_forwarding
) {
1159 ip6stat
.ip6s_cantforward
++;
1160 in6_ifstat_inc(inifp
, ifs6_in_discard
);
1162 * Raise a kernel event if the packet received on cellular
1163 * interface is not intended for local host.
1164 * For now limit it to ICMPv6 packets.
1166 if (inifp
->if_type
== IFT_CELLULAR
&&
1167 ip6
->ip6_nxt
== IPPROTO_ICMPV6
) {
1168 in6_ifstat_inc(inifp
, ifs6_cantfoward_icmp6
);
1175 * Process Hop-by-Hop options header if it's contained.
1176 * m may be modified in ip6_hopopts_input().
1177 * If a JumboPayload option is included, plen will also be modified.
1179 plen
= (u_int32_t
)ntohs(ip6
->ip6_plen
);
1180 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
1181 struct ip6_hbh
*hbh
;
1184 * Mark the packet to imply that HBH option has been checked.
1185 * This can only be true is the packet came in unfragmented
1186 * or if the option is in the first fragment
1188 m
->m_pkthdr
.pkt_flags
|= PKTF_HBH_CHKED
;
1189 if (ip6_hopopts_input(&plen
, &rtalert
, &m
, &off
)) {
1190 #if 0 /* touches NULL pointer */
1191 in6_ifstat_inc(inifp
, ifs6_in_discard
);
1193 goto done
; /* m have already been freed */
1196 /* adjust pointer */
1197 ip6
= mtod(m
, struct ip6_hdr
*);
1200 * if the payload length field is 0 and the next header field
1201 * indicates Hop-by-Hop Options header, then a Jumbo Payload
1202 * option MUST be included.
1204 if (ip6
->ip6_plen
== 0 && plen
== 0) {
1206 * Note that if a valid jumbo payload option is
1207 * contained, ip6_hopopts_input() must set a valid
1208 * (non-zero) payload length to the variable plen.
1210 ip6stat
.ip6s_badoptions
++;
1211 in6_ifstat_inc(inifp
, ifs6_in_discard
);
1212 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
1213 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_HEADER
,
1214 (int)((caddr_t
)&ip6
->ip6_plen
- (caddr_t
)ip6
));
1217 /* ip6_hopopts_input() ensures that mbuf is contiguous */
1218 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
1219 nxt
= hbh
->ip6h_nxt
;
1222 * If we are acting as a router and the packet contains a
1223 * router alert option, see if we know the option value.
1224 * Currently, we only support the option value for MLD, in which
1225 * case we should pass the packet to the multicast routing
1228 if (rtalert
!= ~0 && ip6_forwarding
) {
1230 case IP6OPT_RTALERT_MLD
:
1235 * RFC2711 requires unrecognized values must be
1246 * Check that the amount of data in the buffers
1247 * is as at least much as the IPv6 header would have us expect.
1248 * Trim mbufs if longer than we expect.
1249 * Drop packet if shorter than we expect.
1251 if (m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
) < plen
) {
1252 ip6stat
.ip6s_tooshort
++;
1253 in6_ifstat_inc(inifp
, ifs6_in_truncated
);
1256 if (m
->m_pkthdr
.len
> sizeof(struct ip6_hdr
) + plen
) {
1257 ip6_input_adjust(m
, ip6
, plen
, inifp
);
1261 * Forward if desirable.
1263 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1264 if (!ours
&& nd6_prproxy
) {
1266 * If this isn't for us, this might be a Neighbor
1267 * Solicitation (dst is solicited-node multicast)
1268 * against an address in one of the proxied prefixes;
1269 * if so, claim the packet and let icmp6_input()
1272 ours
= nd6_prproxy_isours(m
, ip6
, NULL
, IFSCOPE_NONE
);
1274 (m
->m_pkthdr
.pkt_flags
& PKTF_PROXY_DST
));
1281 * The unicast forwarding function might return the packet
1282 * if we are proxying prefix(es), and if the packet is an
1283 * ICMPv6 packet that has failed the zone checks, but is
1284 * targetted towards a proxied address (this is optimized by
1285 * way of RTF_PROXY test.) If so, claim the packet as ours
1286 * and let icmp6_input() handle the rest. The packet's hop
1287 * limit value is kept intact (it's not decremented). This
1288 * is for supporting Neighbor Unreachability Detection between
1289 * proxied nodes on different links (src is link-local, dst
1290 * is target address.)
1292 if ((m
= ip6_forward(m
, &rin6
, 0)) == NULL
) {
1295 VERIFY(rin6
.ro_rt
!= NULL
);
1296 VERIFY(m
->m_pkthdr
.pkt_flags
& PKTF_PROXY_DST
);
1297 deliverifp
= rin6
.ro_rt
->rt_ifp
;
1301 ip6
= mtod(m
, struct ip6_hdr
*);
1304 * Malicious party may be able to use IPv4 mapped addr to confuse
1305 * tcp/udp stack and bypass security checks (act as if it was from
1306 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
1308 * For SIIT end node behavior, you may want to disable the check.
1309 * However, you will become vulnerable to attacks using IPv4 mapped
1312 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
1313 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
1314 ip6stat
.ip6s_badscope
++;
1315 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
1320 * Tell launch routine the next header
1322 ip6stat
.ip6s_delivered
++;
1323 in6_ifstat_inc_na(deliverifp
, ifs6_in_deliver
);
1329 * Perform IP header alignment fixup again, if needed. Note that
1330 * we do it once for the outermost protocol, and we assume each
1331 * protocol handler wouldn't mess with the alignment afterwards.
1333 IP6_HDR_ALIGNMENT_FIXUP(m
, inifp
, return );
1335 while (nxt
!= IPPROTO_DONE
) {
1336 struct ipfilter
*filter
;
1337 int (*pr_input
)(struct mbuf
**, int *, int);
1340 * This would imply either IPPROTO_HOPOPTS was not the first
1341 * option or it did not come in the first fragment.
1343 if (nxt
== IPPROTO_HOPOPTS
&&
1344 (m
->m_pkthdr
.pkt_flags
& PKTF_HBH_CHKED
) == 0) {
1346 * This implies that HBH option was not contained
1347 * in the first fragment
1349 ip6stat
.ip6s_badoptions
++;
1353 if (ip6_hdrnestlimit
&& (++nest
> ip6_hdrnestlimit
)) {
1354 ip6stat
.ip6s_toomanyhdr
++;
1359 * protection against faulty packet - there should be
1360 * more sanity checks in header chain processing.
1362 if (m
->m_pkthdr
.len
< off
) {
1363 ip6stat
.ip6s_tooshort
++;
1364 in6_ifstat_inc(inifp
, ifs6_in_truncated
);
1370 * enforce IPsec policy checking if we are seeing last header.
1371 * note that we do not visit this with protocols with pcb layer
1372 * code - like udp/tcp/raw ip.
1374 if ((ipsec_bypass
== 0) &&
1375 (ip6_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0) {
1376 if (ipsec6_in_reject(m
, NULL
)) {
1377 IPSEC_STAT_INCREMENT(ipsec6stat
.in_polvio
);
1386 if (!TAILQ_EMPTY(&ipv6_filters
) && !IFNET_IS_INTCOPROC(inifp
)) {
1388 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
1390 if ((struct ipfilter
*)inject_ipfref
==
1394 } else if (filter
->ipf_filter
.ipf_input
) {
1397 result
= filter
->ipf_filter
.ipf_input(
1398 filter
->ipf_filter
.cookie
,
1399 (mbuf_t
*)&m
, off
, (uint8_t)nxt
);
1400 if (result
== EJUSTRETURN
) {
1413 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1414 struct ip6_hdr
*, ip6
, struct ifnet
*, inifp
,
1415 struct ip
*, NULL
, struct ip6_hdr
*, ip6
);
1417 if ((pr_input
= ip6_protox
[nxt
]->pr_input
) == NULL
) {
1421 } else if (!(ip6_protox
[nxt
]->pr_flags
& PR_PROTOLOCK
)) {
1422 lck_mtx_lock(inet6_domain_mutex
);
1423 nxt
= pr_input(&m
, &off
, nxt
);
1424 lck_mtx_unlock(inet6_domain_mutex
);
1426 nxt
= pr_input(&m
, &off
, nxt
);
1430 ROUTE_RELEASE(&rin6
);
1438 ip6_setsrcifaddr_info(struct mbuf
*m
, uint32_t src_idx
, struct in6_ifaddr
*ia6
)
1440 VERIFY(m
->m_flags
& M_PKTHDR
);
1443 * If the source ifaddr is specified, pick up the information
1444 * from there; otherwise just grab the passed-in ifindex as the
1445 * caller may not have the ifaddr available.
1448 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1449 m
->m_pkthdr
.src_ifindex
= ia6
->ia_ifp
->if_index
;
1451 /* See IN6_IFF comments in in6_var.h */
1452 m
->m_pkthdr
.src_iff
= (ia6
->ia6_flags
& 0xffff);
1454 m
->m_pkthdr
.src_iff
= 0;
1455 m
->m_pkthdr
.src_ifindex
= (uint16_t)src_idx
;
1457 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1463 ip6_setdstifaddr_info(struct mbuf
*m
, uint32_t dst_idx
, struct in6_ifaddr
*ia6
)
1465 VERIFY(m
->m_flags
& M_PKTHDR
);
1468 * If the destination ifaddr is specified, pick up the information
1469 * from there; otherwise just grab the passed-in ifindex as the
1470 * caller may not have the ifaddr available.
1473 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1474 m
->m_pkthdr
.dst_ifindex
= ia6
->ia_ifp
->if_index
;
1476 /* See IN6_IFF comments in in6_var.h */
1477 m
->m_pkthdr
.dst_iff
= (ia6
->ia6_flags
& 0xffff);
1479 m
->m_pkthdr
.dst_iff
= 0;
1480 m
->m_pkthdr
.dst_ifindex
= (uint16_t)dst_idx
;
1482 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1488 ip6_getsrcifaddr_info(struct mbuf
*m
, uint32_t *src_idx
, uint32_t *ia6f
)
1490 VERIFY(m
->m_flags
& M_PKTHDR
);
1492 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
)) {
1496 if (src_idx
!= NULL
) {
1497 *src_idx
= m
->m_pkthdr
.src_ifindex
;
1501 *ia6f
= m
->m_pkthdr
.src_iff
;
1508 ip6_getdstifaddr_info(struct mbuf
*m
, uint32_t *dst_idx
, uint32_t *ia6f
)
1510 VERIFY(m
->m_flags
& M_PKTHDR
);
1512 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
)) {
1516 if (dst_idx
!= NULL
) {
1517 *dst_idx
= m
->m_pkthdr
.dst_ifindex
;
1521 *ia6f
= m
->m_pkthdr
.dst_iff
;
1528 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1529 * included, the real payload length will be stored in plenp.
1532 ip6_hopopts_input(uint32_t *plenp
, uint32_t *rtalertp
, struct mbuf
**mp
,
1535 struct mbuf
*m
= *mp
;
1536 int off
= *offp
, hbhlen
;
1537 struct ip6_hbh
*hbh
;
1540 /* validation of the length of the header */
1541 IP6_EXTHDR_CHECK(m
, off
, sizeof(*hbh
), return (-1));
1542 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1543 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1545 IP6_EXTHDR_CHECK(m
, off
, hbhlen
, return (-1));
1546 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1548 hbhlen
-= sizeof(struct ip6_hbh
);
1549 opt
= (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
);
1551 if (ip6_process_hopopts(m
, (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
),
1552 hbhlen
, rtalertp
, plenp
) < 0) {
1562 * Search header for all Hop-by-hop options and process each option.
1563 * This function is separate from ip6_hopopts_input() in order to
1564 * handle a case where the sending node itself process its hop-by-hop
1565 * options header. In such a case, the function is called from ip6_output().
1567 * The function assumes that hbh header is located right after the IPv6 header
1568 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1569 * opthead + hbhlen is located in continuous memory region.
1572 ip6_process_hopopts(struct mbuf
*m
, u_int8_t
*opthead
, int hbhlen
,
1573 u_int32_t
*rtalertp
, u_int32_t
*plenp
)
1575 struct ip6_hdr
*ip6
;
1577 u_int8_t
*opt
= opthead
;
1578 u_int16_t rtalert_val
;
1579 u_int32_t jumboplen
;
1580 const int erroff
= sizeof(struct ip6_hdr
) + sizeof(struct ip6_hbh
);
1582 for (; hbhlen
> 0; hbhlen
-= optlen
, opt
+= optlen
) {
1588 if (hbhlen
< IP6OPT_MINLEN
) {
1589 ip6stat
.ip6s_toosmall
++;
1592 optlen
= *(opt
+ 1) + 2;
1594 case IP6OPT_ROUTER_ALERT
:
1595 /* XXX may need check for alignment */
1596 if (hbhlen
< IP6OPT_RTALERT_LEN
) {
1597 ip6stat
.ip6s_toosmall
++;
1600 if (*(opt
+ 1) != IP6OPT_RTALERT_LEN
- 2) {
1602 icmp6_error(m
, ICMP6_PARAM_PROB
,
1603 ICMP6_PARAMPROB_HEADER
,
1604 (int)(erroff
+ opt
+ 1 - opthead
));
1607 optlen
= IP6OPT_RTALERT_LEN
;
1608 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&rtalert_val
, 2);
1609 *rtalertp
= ntohs(rtalert_val
);
1612 /* XXX may need check for alignment */
1613 if (hbhlen
< IP6OPT_JUMBO_LEN
) {
1614 ip6stat
.ip6s_toosmall
++;
1617 if (*(opt
+ 1) != IP6OPT_JUMBO_LEN
- 2) {
1619 icmp6_error(m
, ICMP6_PARAM_PROB
,
1620 ICMP6_PARAMPROB_HEADER
,
1621 (int)(erroff
+ opt
+ 1 - opthead
));
1624 optlen
= IP6OPT_JUMBO_LEN
;
1627 * IPv6 packets that have non 0 payload length
1628 * must not contain a jumbo payload option.
1630 ip6
= mtod(m
, struct ip6_hdr
*);
1631 if (ip6
->ip6_plen
) {
1632 ip6stat
.ip6s_badoptions
++;
1633 icmp6_error(m
, ICMP6_PARAM_PROB
,
1634 ICMP6_PARAMPROB_HEADER
,
1635 (int)(erroff
+ opt
- opthead
));
1640 * We may see jumbolen in unaligned location, so
1641 * we'd need to perform bcopy().
1643 bcopy(opt
+ 2, &jumboplen
, sizeof(jumboplen
));
1644 jumboplen
= (u_int32_t
)htonl(jumboplen
);
1648 * if there are multiple jumbo payload options,
1649 * *plenp will be non-zero and the packet will be
1651 * the behavior may need some debate in ipngwg -
1652 * multiple options does not make sense, however,
1653 * there's no explicit mention in specification.
1656 ip6stat
.ip6s_badoptions
++;
1657 icmp6_error(m
, ICMP6_PARAM_PROB
,
1658 ICMP6_PARAMPROB_HEADER
,
1659 (int)(erroff
+ opt
+ 2 - opthead
));
1665 * jumbo payload length must be larger than 65535.
1667 if (jumboplen
<= IPV6_MAXPACKET
) {
1668 ip6stat
.ip6s_badoptions
++;
1669 icmp6_error(m
, ICMP6_PARAM_PROB
,
1670 ICMP6_PARAMPROB_HEADER
,
1671 (int)(erroff
+ opt
+ 2 - opthead
));
1677 default: /* unknown option */
1678 if (hbhlen
< IP6OPT_MINLEN
) {
1679 ip6stat
.ip6s_toosmall
++;
1682 optlen
= ip6_unknown_opt(opt
, m
,
1683 erroff
+ opt
- opthead
);
1700 * Unknown option processing.
1701 * The third argument `off' is the offset from the IPv6 header to the option,
1702 * which is necessary if the IPv6 header the and option header and IPv6 header
1703 * is not continuous in order to return an ICMPv6 error.
1706 ip6_unknown_opt(uint8_t *optp
, struct mbuf
*m
, size_t off
)
1708 struct ip6_hdr
*ip6
;
1710 switch (IP6OPT_TYPE(*optp
)) {
1711 case IP6OPT_TYPE_SKIP
: /* ignore the option */
1712 return (int)*(optp
+ 1);
1714 case IP6OPT_TYPE_DISCARD
: /* silently discard */
1718 case IP6OPT_TYPE_FORCEICMP
: /* send ICMP even if multicasted */
1719 ip6stat
.ip6s_badoptions
++;
1720 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_OPTION
, (int)off
);
1723 case IP6OPT_TYPE_ICMP
: /* send ICMP if not multicasted */
1724 ip6stat
.ip6s_badoptions
++;
1725 ip6
= mtod(m
, struct ip6_hdr
*);
1726 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
1727 (m
->m_flags
& (M_BCAST
| M_MCAST
))) {
1730 icmp6_error(m
, ICMP6_PARAM_PROB
,
1731 ICMP6_PARAMPROB_OPTION
, (int)off
);
1736 m_freem(m
); /* XXX: NOTREACHED */
1741 * Create the "control" list for this pcb.
1742 * These functions will not modify mbuf chain at all.
1744 * With KAME mbuf chain restriction:
1745 * The routine will be called from upper layer handlers like tcp6_input().
1746 * Thus the routine assumes that the caller (tcp6_input) have already
1747 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1748 * very first mbuf on the mbuf chain.
1750 * ip6_savecontrol_v4 will handle those options that are possible to be
1751 * set on a v4-mapped socket.
1752 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1753 * options and handle the v6-only ones itself.
1756 ip6_savecontrol_v4(struct inpcb
*inp
, struct mbuf
*m
, struct mbuf
**mp
,
1759 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1761 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0) {
1765 mp
= sbcreatecontrol_mbuf((caddr_t
)&tv
, sizeof(tv
),
1766 SCM_TIMESTAMP
, SOL_SOCKET
, mp
);
1771 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
1774 time
= mach_absolute_time();
1775 mp
= sbcreatecontrol_mbuf((caddr_t
)&time
, sizeof(time
),
1776 SCM_TIMESTAMP_MONOTONIC
, SOL_SOCKET
, mp
);
1781 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP_CONTINUOUS
) != 0) {
1784 time
= mach_continuous_time();
1785 mp
= sbcreatecontrol_mbuf((caddr_t
)&time
, sizeof(time
),
1786 SCM_TIMESTAMP_CONTINUOUS
, SOL_SOCKET
, mp
);
1791 if ((inp
->inp_socket
->so_flags
& SOF_RECV_TRAFFIC_CLASS
) != 0) {
1792 int tc
= m_get_traffic_class(m
);
1794 mp
= sbcreatecontrol_mbuf((caddr_t
)&tc
, sizeof(tc
),
1795 SO_TRAFFIC_CLASS
, SOL_SOCKET
, mp
);
1801 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1802 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
1803 if (v4only
!= NULL
) {
1807 // Send ECN flags for v4-mapped addresses
1808 if ((inp
->inp_flags
& IN6P_TCLASS
) != 0) {
1809 struct ip
*ip_header
= mtod(m
, struct ip
*);
1811 int tclass
= (int)(ip_header
->ip_tos
);
1812 mp
= sbcreatecontrol_mbuf((caddr_t
)&tclass
, sizeof(tclass
),
1813 IPV6_TCLASS
, IPPROTO_IPV6
, mp
);
1819 // Send IN6P_PKTINFO for v4-mapped address
1820 if ((inp
->inp_flags
& IN6P_PKTINFO
) != 0
1822 /* Content Filter needs to see local address */
1823 || (inp
->inp_socket
->so_cfil_db
!= NULL
)
1826 struct in6_pktinfo pi6
= {
1827 .ipi6_addr
= IN6ADDR_V4MAPPED_INIT
,
1828 .ipi6_ifindex
= (m
&& m
->m_pkthdr
.rcvif
) ? m
->m_pkthdr
.rcvif
->if_index
: 0,
1831 struct ip
*ip_header
= mtod(m
, struct ip
*);
1832 bcopy(&ip_header
->ip_dst
, &pi6
.ipi6_addr
.s6_addr32
[3], sizeof(struct in_addr
));
1834 mp
= sbcreatecontrol_mbuf((caddr_t
)&pi6
,
1835 sizeof(struct in6_pktinfo
),
1836 IS2292(inp
, IPV6_2292PKTINFO
, IPV6_PKTINFO
),
1845 /* RFC 2292 sec. 5 */
1846 if ((inp
->inp_flags
& IN6P_PKTINFO
) != 0
1848 /* Content Filter needs to see local address */
1849 || (inp
->inp_socket
->so_cfil_db
!= NULL
)
1852 struct in6_pktinfo pi6
;
1854 bcopy(&ip6
->ip6_dst
, &pi6
.ipi6_addr
, sizeof(struct in6_addr
));
1855 in6_clearscope(&pi6
.ipi6_addr
); /* XXX */
1857 (m
&& m
->m_pkthdr
.rcvif
) ? m
->m_pkthdr
.rcvif
->if_index
: 0;
1859 mp
= sbcreatecontrol_mbuf((caddr_t
)&pi6
,
1860 sizeof(struct in6_pktinfo
),
1861 IS2292(inp
, IPV6_2292PKTINFO
, IPV6_PKTINFO
),
1868 if ((inp
->inp_flags
& IN6P_HOPLIMIT
) != 0) {
1869 int hlim
= ip6
->ip6_hlim
& 0xff;
1871 mp
= sbcreatecontrol_mbuf((caddr_t
)&hlim
, sizeof(int),
1872 IS2292(inp
, IPV6_2292HOPLIMIT
, IPV6_HOPLIMIT
),
1879 if (v4only
!= NULL
) {
1886 ip6_savecontrol(struct inpcb
*in6p
, struct mbuf
*m
, struct mbuf
**mp
)
1889 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1893 np
= ip6_savecontrol_v4(in6p
, m
, mp
, &v4only
);
1903 if ((in6p
->inp_flags
& IN6P_TCLASS
) != 0) {
1907 flowinfo
= (u_int32_t
)ntohl(ip6
->ip6_flow
& IPV6_FLOWINFO_MASK
);
1910 tclass
= flowinfo
& 0xff;
1911 mp
= sbcreatecontrol_mbuf((caddr_t
)&tclass
, sizeof(tclass
),
1912 IPV6_TCLASS
, IPPROTO_IPV6
, mp
);
1919 * IPV6_HOPOPTS socket option. Recall that we required super-user
1920 * privilege for the option (see ip6_ctloutput), but it might be too
1921 * strict, since there might be some hop-by-hop options which can be
1922 * returned to normal user.
1923 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1925 if ((in6p
->inp_flags
& IN6P_HOPOPTS
) != 0) {
1927 * Check if a hop-by-hop options header is contatined in the
1928 * received packet, and if so, store the options as ancillary
1929 * data. Note that a hop-by-hop options header must be
1930 * just after the IPv6 header, which is assured through the
1931 * IPv6 input processing.
1933 ip6
= mtod(m
, struct ip6_hdr
*);
1934 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
1935 struct ip6_hbh
*hbh
;
1937 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
1938 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1941 * XXX: We copy the whole header even if a
1942 * jumbo payload option is included, the option which
1943 * is to be removed before returning according to
1945 * Note: this constraint is removed in RFC3542
1947 mp
= sbcreatecontrol_mbuf((caddr_t
)hbh
, hbhlen
,
1948 IS2292(in6p
, IPV6_2292HOPOPTS
, IPV6_HOPOPTS
),
1957 if ((in6p
->inp_flags
& (IN6P_RTHDR
| IN6P_DSTOPTS
)) != 0) {
1958 int nxt
= ip6
->ip6_nxt
, off
= sizeof(struct ip6_hdr
);
1961 * Search for destination options headers or routing
1962 * header(s) through the header chain, and stores each
1963 * header as ancillary data.
1964 * Note that the order of the headers remains in
1965 * the chain of ancillary data.
1967 while (1) { /* is explicit loop prevention necessary? */
1968 struct ip6_ext
*ip6e
= NULL
;
1972 * if it is not an extension header, don't try to
1973 * pull it from the chain.
1976 case IPPROTO_DSTOPTS
:
1977 case IPPROTO_ROUTING
:
1978 case IPPROTO_HOPOPTS
:
1979 case IPPROTO_AH
: /* is it possible? */
1985 if (off
+ sizeof(*ip6e
) > m
->m_len
) {
1988 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + off
);
1989 if (nxt
== IPPROTO_AH
) {
1990 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1992 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1994 if (off
+ elen
> m
->m_len
) {
1999 case IPPROTO_DSTOPTS
:
2000 if (!(in6p
->inp_flags
& IN6P_DSTOPTS
)) {
2004 mp
= sbcreatecontrol_mbuf((caddr_t
)ip6e
, elen
,
2005 IS2292(in6p
, IPV6_2292DSTOPTS
,
2006 IPV6_DSTOPTS
), IPPROTO_IPV6
, mp
);
2011 case IPPROTO_ROUTING
:
2012 if (!(in6p
->inp_flags
& IN6P_RTHDR
)) {
2016 mp
= sbcreatecontrol_mbuf((caddr_t
)ip6e
, elen
,
2017 IS2292(in6p
, IPV6_2292RTHDR
, IPV6_RTHDR
),
2023 case IPPROTO_HOPOPTS
:
2024 case IPPROTO_AH
: /* is it possible? */
2029 * other cases have been filtered in the above.
2030 * none will visit this case. here we supply
2031 * the code just in case (nxt overwritten or
2037 /* proceed with the next header. */
2039 nxt
= ip6e
->ip6e_nxt
;
2047 ip6stat
.ip6s_pktdropcntrl
++;
2048 /* XXX increment a stat to show the failure */
2054 ip6_notify_pmtu(struct inpcb
*in6p
, struct sockaddr_in6
*dst
, u_int32_t
*mtu
)
2058 struct ip6_mtuinfo mtuctl
;
2060 so
= in6p
->inp_socket
;
2062 if ((in6p
->inp_flags
& IN6P_MTU
) == 0) {
2071 if (so
== NULL
) { /* I believe this is impossible */
2072 panic("ip6_notify_pmtu: socket is NULL");
2077 if (IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
2078 (so
->so_proto
== NULL
|| so
->so_proto
->pr_protocol
== IPPROTO_TCP
)) {
2082 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
2083 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &dst
->sin6_addr
)) {
2087 bzero(&mtuctl
, sizeof(mtuctl
)); /* zero-clear for safety */
2088 mtuctl
.ip6m_mtu
= *mtu
;
2089 mtuctl
.ip6m_addr
= *dst
;
2090 if (sa6_recoverscope(&mtuctl
.ip6m_addr
, TRUE
)) {
2094 if ((m_mtu
= sbcreatecontrol((caddr_t
)&mtuctl
, sizeof(mtuctl
),
2095 IPV6_PATHMTU
, IPPROTO_IPV6
)) == NULL
) {
2099 if (sbappendaddr(&so
->so_rcv
, SA(dst
), NULL
, m_mtu
, NULL
) == 0) {
2106 * Get pointer to the previous header followed by the header
2107 * currently processed.
2108 * XXX: This function supposes that
2109 * M includes all headers,
2110 * the next header field and the header length field of each header
2112 * the sum of each header length equals to OFF.
2113 * Because of these assumptions, this function must be called very
2114 * carefully. Moreover, it will not be used in the near future when
2115 * we develop `neater' mechanism to process extension headers.
2118 ip6_get_prevhdr(struct mbuf
*m
, int off
)
2120 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2122 if (off
== sizeof(struct ip6_hdr
)) {
2123 return (char *)&ip6
->ip6_nxt
;
2126 struct ip6_ext
*ip6e
= NULL
;
2129 len
= sizeof(struct ip6_hdr
);
2131 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + len
);
2134 case IPPROTO_FRAGMENT
:
2135 len
+= sizeof(struct ip6_frag
);
2138 len
+= (ip6e
->ip6e_len
+ 2) << 2;
2141 len
+= (ip6e
->ip6e_len
+ 1) << 3;
2144 nxt
= ip6e
->ip6e_nxt
;
2147 return (char *)&ip6e
->ip6e_nxt
;
2155 * get next header offset. m will be retained.
2158 ip6_nexthdr(struct mbuf
*m
, int off
, int proto
, int *nxtp
)
2161 struct ip6_ext ip6e
;
2166 if ((m
->m_flags
& M_PKTHDR
) == 0 || m
->m_pkthdr
.len
< off
) {
2172 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6
)) {
2175 m_copydata(m
, off
, sizeof(ip6
), (caddr_t
)&ip6
);
2177 *nxtp
= ip6
.ip6_nxt
;
2182 case IPPROTO_FRAGMENT
:
2184 * terminate parsing if it is not the first fragment,
2185 * it does not make sense to parse through it.
2187 if (m
->m_pkthdr
.len
< off
+ sizeof(fh
)) {
2190 m_copydata(m
, off
, sizeof(fh
), (caddr_t
)&fh
);
2191 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
2192 if (fh
.ip6f_offlg
& IP6F_OFF_MASK
) {
2196 *nxtp
= fh
.ip6f_nxt
;
2198 off
+= sizeof(struct ip6_frag
);
2202 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
)) {
2205 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
2207 *nxtp
= ip6e
.ip6e_nxt
;
2209 off
+= (ip6e
.ip6e_len
+ 2) << 2;
2212 case IPPROTO_HOPOPTS
:
2213 case IPPROTO_ROUTING
:
2214 case IPPROTO_DSTOPTS
:
2215 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
)) {
2218 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
2220 *nxtp
= ip6e
.ip6e_nxt
;
2222 off
+= (ip6e
.ip6e_len
+ 1) << 3;
2227 case IPPROTO_IPCOMP
:
2237 * get offset for the last header in the chain. m will be kept untainted.
2240 ip6_lasthdr(struct mbuf
*m
, int off
, int proto
, int *nxtp
)
2250 newoff
= ip6_nexthdr(m
, off
, proto
, nxtp
);
2253 } else if (newoff
< off
) {
2254 return -1; /* invalid */
2255 } else if (newoff
== off
) {
2265 ip6_pkt_has_ulp(struct mbuf
*m
)
2267 int off
= 0, nxt
= IPPROTO_NONE
;
2269 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
2270 if (off
< 0 || m
->m_pkthdr
.len
< off
) {
2276 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
) {
2281 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
) {
2285 case IPPROTO_ICMPV6
:
2286 if (off
+ sizeof(uint32_t) > m
->m_pkthdr
.len
) {
2294 case IPPROTO_IPCOMP
:
2303 ip6_addaux(struct mbuf
*m
)
2307 /* Check if one is already allocated */
2308 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2309 KERNEL_TAG_TYPE_INET6
, NULL
);
2311 /* Allocate a tag */
2312 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_INET6
,
2313 sizeof(struct ip6aux
), M_DONTWAIT
, m
);
2315 /* Attach it to the mbuf */
2317 m_tag_prepend(m
, tag
);
2321 return tag
? (struct ip6aux
*)(tag
+ 1) : NULL
;
2325 ip6_findaux(struct mbuf
*m
)
2329 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2330 KERNEL_TAG_TYPE_INET6
, NULL
);
2332 return tag
? (struct ip6aux
*)(tag
+ 1) : NULL
;
2336 ip6_delaux(struct mbuf
*m
)
2340 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2341 KERNEL_TAG_TYPE_INET6
, NULL
);
2343 m_tag_delete(m
, tag
);
2353 frag6_drain(); /* fragments */
2354 in6_rtqdrain(); /* protocol cloned routes */
2355 nd6_drain(NULL
); /* cloned routes: ND6 */
2359 * System control for IP6
2362 u_char inet6ctlerrmap
[PRC_NCMDS
] = {
2364 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2365 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2366 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
2372 sysctl_reset_ip6_input_stats SYSCTL_HANDLER_ARGS
2374 #pragma unused(arg1, arg2)
2377 i
= ip6_input_measure
;
2378 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
2379 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
2383 if (i
< 0 || i
> 1) {
2387 if (ip6_input_measure
!= i
&& i
== 1) {
2388 net_perf_initialize(&net_perf
, ip6_input_measure_bins
);
2390 ip6_input_measure
= i
;
2396 sysctl_ip6_input_measure_bins SYSCTL_HANDLER_ARGS
2398 #pragma unused(arg1, arg2)
2402 i
= ip6_input_measure_bins
;
2403 error
= sysctl_handle_quad(oidp
, &i
, 0, req
);
2404 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
2408 if (!net_perf_validate_bins(i
)) {
2412 ip6_input_measure_bins
= i
;
2418 sysctl_ip6_input_getperf SYSCTL_HANDLER_ARGS
2420 #pragma unused(oidp, arg1, arg2)
2421 if (req
->oldptr
== USER_ADDR_NULL
) {
2422 req
->oldlen
= (size_t)sizeof(struct net_perf
);
2425 return SYSCTL_OUT(req
, &net_perf
, MIN(sizeof(net_perf
), req
->oldlen
));
2430 * Initialize IPv6 source address hash table.
2433 in6_ifaddrhashtbl_init(void)
2437 if (in6_ifaddrhashtbl
!= NULL
) {
2441 PE_parse_boot_argn("ina6ddr_nhash", &in6addr_nhash
,
2442 sizeof(in6addr_nhash
));
2443 if (in6addr_nhash
== 0) {
2444 in6addr_nhash
= IN6ADDR_NHASH
;
2447 MALLOC(in6_ifaddrhashtbl
, struct in6_ifaddrhashhead
*,
2448 in6addr_nhash
* sizeof(*in6_ifaddrhashtbl
),
2449 M_IFADDR
, M_WAITOK
| M_ZERO
);
2450 if (in6_ifaddrhashtbl
== NULL
) {
2451 panic("in6_ifaddrhashtbl allocation failed");
2455 * Generate the next largest prime greater than in6addr_nhash.
2457 k
= (in6addr_nhash
% 2 == 0) ? in6addr_nhash
+ 1 : in6addr_nhash
+ 2;
2460 for (i
= 3; i
* i
<= k
; i
+= 2) {
2474 sysctl_ip6_checkinterface SYSCTL_HANDLER_ARGS
2476 #pragma unused(arg1, arg2)
2479 i
= ip6_checkinterface
;
2480 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
2481 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
2486 case IP6_CHECKINTERFACE_WEAK_ES
:
2487 case IP6_CHECKINTERFACE_HYBRID_ES
:
2488 case IP6_CHECKINTERFACE_STRONG_ES
:
2489 if (ip6_checkinterface
!= i
) {
2490 ip6_checkinterface
= i
;
2491 os_log(OS_LOG_DEFAULT
, "%s: ip6_checkinterface is now %d\n",
2492 __func__
, ip6_checkinterface
);