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_fw.h>
152 #include <netinet/ip_dummynet.h>
153 #endif /* DUMMYNET */
155 /* we need it for NLOOP. */
159 #include <net/pfvar.h>
164 struct ip6protosw
*ip6_protox
[IPPROTO_MAX
];
166 static lck_grp_attr_t
*in6_ifaddr_rwlock_grp_attr
;
167 static lck_grp_t
*in6_ifaddr_rwlock_grp
;
168 static lck_attr_t
*in6_ifaddr_rwlock_attr
;
169 decl_lck_rw_data(, in6_ifaddr_rwlock
);
171 /* Protected by in6_ifaddr_rwlock */
172 struct in6_ifaddrhead in6_ifaddrhead
;
173 struct in6_ifaddrhashhead
* in6_ifaddrhashtbl
;
174 uint32_t in6_ifaddrhmask
;
176 #define IN6ADDR_NHASH 61
177 u_int32_t in6addr_nhash
= 0; /* hash table size */
178 u_int32_t in6addr_hashp
= 0; /* next largest prime */
181 #define IN6_IFSTAT_REQUIRE_ALIGNED_64(f) \
182 _CASSERT(!(offsetof(struct in6_ifstat, f) % sizeof (uint64_t)))
184 #define ICMP6_IFSTAT_REQUIRE_ALIGNED_64(f) \
185 _CASSERT(!(offsetof(struct icmp6_ifstat, f) % sizeof (uint64_t)))
187 struct ip6stat ip6stat
;
189 decl_lck_mtx_data(, proxy6_lock
);
190 decl_lck_mtx_data(static, dad6_mutex_data
);
191 decl_lck_mtx_data(static, nd6_mutex_data
);
192 decl_lck_mtx_data(static, prefix6_mutex_data
);
193 lck_mtx_t
*dad6_mutex
= &dad6_mutex_data
;
194 lck_mtx_t
*nd6_mutex
= &nd6_mutex_data
;
195 lck_mtx_t
*prefix6_mutex
= &prefix6_mutex_data
;
196 #ifdef ENABLE_ADDRSEL
197 decl_lck_mtx_data(static, addrsel_mutex_data
);
198 lck_mtx_t
*addrsel_mutex
= &addrsel_mutex_data
;
200 static lck_attr_t
*ip6_mutex_attr
;
201 static lck_grp_t
*ip6_mutex_grp
;
202 static lck_grp_attr_t
*ip6_mutex_grp_attr
;
204 extern int loopattach_done
;
205 extern void addrsel_policy_init(void);
207 static int sysctl_reset_ip6_input_stats SYSCTL_HANDLER_ARGS
;
208 static int sysctl_ip6_input_measure_bins SYSCTL_HANDLER_ARGS
;
209 static int sysctl_ip6_input_getperf SYSCTL_HANDLER_ARGS
;
210 static void ip6_init_delayed(void);
211 static int ip6_hopopts_input(u_int32_t
*, u_int32_t
*, struct mbuf
**, int *);
213 static void in6_ifaddrhashtbl_init(void);
216 extern void stfattach(void);
219 SYSCTL_DECL(_net_inet6_ip6
);
221 static uint32_t ip6_adj_clear_hwcksum
= 0;
222 SYSCTL_UINT(_net_inet6_ip6
, OID_AUTO
, adj_clear_hwcksum
,
223 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_adj_clear_hwcksum
, 0,
224 "Invalidate hwcksum info when adjusting length");
226 static uint32_t ip6_adj_partial_sum
= 1;
227 SYSCTL_UINT(_net_inet6_ip6
, OID_AUTO
, adj_partial_sum
,
228 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_adj_partial_sum
, 0,
229 "Perform partial sum adjustment of trailing bytes at IP layer");
231 static int ip6_input_measure
= 0;
232 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, input_perf
,
233 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
234 &ip6_input_measure
, 0, sysctl_reset_ip6_input_stats
, "I", "Do time measurement");
236 static uint64_t ip6_input_measure_bins
= 0;
237 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, input_perf_bins
,
238 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_input_measure_bins
, 0,
239 sysctl_ip6_input_measure_bins
, "I",
240 "bins for chaining performance data histogram");
242 static net_perf_t net_perf
;
243 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, input_perf_data
,
244 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
245 0, 0, sysctl_ip6_input_getperf
, "S,net_perf",
246 "IP6 input performance data (struct net_perf, net/net_perf.h)");
249 * ip6_checkinterface controls the receive side of the models for multihoming
250 * that are discussed in RFC 1122.
252 * sysctl_ip6_checkinterface values are:
253 * IP6_CHECKINTERFACE_WEAK_ES:
254 * This corresponds to the Weak End-System model where incoming packets from
255 * any interface are accepted provided the destination address of the incoming packet
256 * is assigned to some interface.
258 * IP6_CHECKINTERFACE_HYBRID_ES:
259 * The Hybrid End-System model use the Strong End-System for tunnel interfaces
260 * (ipsec and utun) and the weak End-System model for other interfaces families.
261 * This prevents a rogue middle box to probe for signs of TCP connections
262 * that use the tunnel interface.
264 * IP6_CHECKINTERFACE_STRONG_ES:
265 * The Strong model model requires the packet arrived on an interface that
266 * is assigned the destination address of the packet.
268 * Since the routing table and transmit implementation do not implement the Strong ES model,
269 * setting this to a value different from IP6_CHECKINTERFACE_WEAK_ES may lead to unexpected results.
271 * When forwarding is enabled, the system reverts to the Weak ES model as a router
272 * is expected by design to receive packets from several interfaces to the same address.
274 #define IP6_CHECKINTERFACE_WEAK_ES 0
275 #define IP6_CHECKINTERFACE_HYBRID_ES 1
276 #define IP6_CHECKINTERFACE_STRONG_ES 2
278 static int ip6_checkinterface
= IP6_CHECKINTERFACE_HYBRID_ES
;
280 static int sysctl_ip6_checkinterface SYSCTL_HANDLER_ARGS
;
281 SYSCTL_PROC(_net_inet6_ip6
, OID_AUTO
, check_interface
,
282 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
283 0, 0, sysctl_ip6_checkinterface
, "I", "Verify packet arrives on correct interface");
285 #if (DEBUG || DEVELOPMENT)
286 #define IP6_CHECK_IFDEBUG 1
288 #define IP6_CHECK_IFDEBUG 0
289 #endif /* (DEBUG || DEVELOPMENT) */
290 static int ip6_checkinterface_debug
= IP6_CHECK_IFDEBUG
;
291 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, checkinterface_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
292 &ip6_checkinterface_debug
, IP6_CHECK_IFDEBUG
, "");
294 typedef enum ip6_check_if_result
{
295 IP6_CHECK_IF_NONE
= 0,
296 IP6_CHECK_IF_OURS
= 1,
297 IP6_CHECK_IF_DROP
= 2,
298 IP6_CHECK_IF_FORWARD
= 3
299 } ip6_check_if_result_t
;
301 static ip6_check_if_result_t
ip6_input_check_interface(struct mbuf
*, struct ip6_hdr
*, struct ifnet
*, struct route_in6
*rin6
, struct ifnet
**);
304 * On platforms which require strict alignment (currently for anything but
305 * i386 or x86_64), check if the IP header pointer is 32-bit aligned; if not,
306 * copy the contents of the mbuf chain into a new chain, and free the original
307 * one. Create some head room in the first mbuf of the new chain, in case
308 * it's needed later on.
310 * RFC 2460 says that IPv6 headers are 64-bit aligned, but network interfaces
311 * mostly align to 32-bit boundaries. Care should be taken never to use 64-bit
312 * load/store operations on the fields in IPv6 headers.
314 #if defined(__i386__) || defined(__x86_64__)
315 #define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0)
316 #else /* !__i386__ && !__x86_64__ */
317 #define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { \
318 if (!IP6_HDR_ALIGNED_P(mtod(_m, caddr_t))) { \
320 struct ifnet *__ifp = (_ifp); \
321 atomic_add_64(&(__ifp)->if_alignerrs, 1); \
322 if (((_m)->m_flags & M_PKTHDR) && \
323 (_m)->m_pkthdr.pkt_hdr != NULL) \
324 (_m)->m_pkthdr.pkt_hdr = NULL; \
325 _n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT); \
327 ip6stat.ip6s_toosmall++; \
332 VERIFY(_n != (_m)); \
337 #endif /* !__i386__ && !__x86_64__ */
340 ip6_proto_input(protocol_family_t protocol
, mbuf_t packet
)
342 #pragma unused(protocol)
344 struct timeval start_tv
;
345 if (ip6_input_measure
) {
346 net_perf_start_time(&net_perf
, &start_tv
);
351 if (ip6_input_measure
) {
352 net_perf_measure_time(&net_perf
, &start_tv
, 1);
353 net_perf_histogram(&net_perf
, 1);
359 * IP6 initialization: fill in IP6 protocol switch table.
360 * All protocols not implemented in kernel go to raw IP6 protocol handler.
363 ip6_init(struct ip6protosw
*pp
, struct domain
*dp
)
365 static int ip6_initialized
= 0;
369 domain_unguard_t unguard
;
371 domain_proto_mtx_lock_assert_held();
372 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
| PR_ATTACHED
)) == PR_ATTACHED
);
374 _CASSERT((sizeof(struct ip6_hdr
) +
375 sizeof(struct icmp6_hdr
)) <= _MHLEN
);
377 if (ip6_initialized
) {
382 eventhandler_lists_ctxt_init(&in6_evhdlr_ctxt
);
383 (void)EVENTHANDLER_REGISTER(&in6_evhdlr_ctxt
, in6_event
,
384 in6_eventhdlr_callback
, eventhandler_entry_dummy_arg
,
385 EVENTHANDLER_PRI_ANY
);
387 eventhandler_lists_ctxt_init(&in6_clat46_evhdlr_ctxt
);
388 (void)EVENTHANDLER_REGISTER(&in6_clat46_evhdlr_ctxt
, in6_clat46_event
,
389 in6_clat46_eventhdlr_callback
, eventhandler_entry_dummy_arg
,
390 EVENTHANDLER_PRI_ANY
);
392 for (i
= 0; i
< IN6_EVENT_MAX
; i
++) {
393 VERIFY(in6_event2kev_array
[i
].in6_event_code
== i
);
396 pr
= pffindproto_locked(PF_INET6
, IPPROTO_RAW
, SOCK_RAW
);
398 panic("%s: Unable to find [PF_INET6,IPPROTO_RAW,SOCK_RAW]\n",
403 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
404 for (i
= 0; i
< IPPROTO_MAX
; i
++) {
405 ip6_protox
[i
] = (struct ip6protosw
*)pr
;
408 * Cycle through IP protocols and put them into the appropriate place
409 * in ip6_protox[], skipping protocols IPPROTO_{IP,RAW}.
411 VERIFY(dp
== inet6domain
&& dp
->dom_family
== PF_INET6
);
412 TAILQ_FOREACH(pr
, &dp
->dom_protosw
, pr_entry
) {
413 VERIFY(pr
->pr_domain
== dp
);
414 if (pr
->pr_protocol
!= 0 && pr
->pr_protocol
!= IPPROTO_RAW
) {
415 /* Be careful to only index valid IP protocols. */
416 if (pr
->pr_protocol
< IPPROTO_MAX
) {
417 ip6_protox
[pr
->pr_protocol
] =
418 (struct ip6protosw
*)pr
;
423 ip6_mutex_grp_attr
= lck_grp_attr_alloc_init();
425 ip6_mutex_grp
= lck_grp_alloc_init("ip6", ip6_mutex_grp_attr
);
426 ip6_mutex_attr
= lck_attr_alloc_init();
428 lck_mtx_init(dad6_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
429 lck_mtx_init(nd6_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
430 lck_mtx_init(prefix6_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
431 scope6_init(ip6_mutex_grp
, ip6_mutex_attr
);
433 #ifdef ENABLE_ADDRSEL
434 lck_mtx_init(addrsel_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
437 lck_mtx_init(&proxy6_lock
, ip6_mutex_grp
, ip6_mutex_attr
);
439 in6_ifaddr_rwlock_grp_attr
= lck_grp_attr_alloc_init();
440 in6_ifaddr_rwlock_grp
= lck_grp_alloc_init("in6_ifaddr_rwlock",
441 in6_ifaddr_rwlock_grp_attr
);
442 in6_ifaddr_rwlock_attr
= lck_attr_alloc_init();
443 lck_rw_init(&in6_ifaddr_rwlock
, in6_ifaddr_rwlock_grp
,
444 in6_ifaddr_rwlock_attr
);
446 TAILQ_INIT(&in6_ifaddrhead
);
447 in6_ifaddrhashtbl_init();
449 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_receive
);
450 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_hdrerr
);
451 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_toobig
);
452 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_noroute
);
453 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_addrerr
);
454 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_protounknown
);
455 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_truncated
);
456 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_discard
);
457 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_deliver
);
458 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_forward
);
459 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_request
);
460 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_discard
);
461 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragok
);
462 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragfail
);
463 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragcreat
);
464 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_reqd
);
465 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_ok
);
466 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_fail
);
467 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mcast
);
468 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mcast
);
470 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_msg
);
471 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_error
);
472 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_dstunreach
);
473 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_adminprohib
);
474 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_timeexceed
);
475 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_paramprob
);
476 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_pkttoobig
);
477 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echo
);
478 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echoreply
);
479 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routersolicit
);
480 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routeradvert
);
481 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighborsolicit
);
482 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighboradvert
);
483 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_redirect
);
484 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldquery
);
485 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldreport
);
486 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mlddone
);
488 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_msg
);
489 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_error
);
490 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_dstunreach
);
491 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_adminprohib
);
492 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_timeexceed
);
493 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_paramprob
);
494 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_pkttoobig
);
495 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echo
);
496 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echoreply
);
497 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routersolicit
);
498 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routeradvert
);
499 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighborsolicit
);
500 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighboradvert
);
501 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_redirect
);
502 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldquery
);
503 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldreport
);
504 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mlddone
);
508 (RandomULong() ^ tv
.tv_usec
) % MAX_TEMP_DESYNC_FACTOR
;
510 PE_parse_boot_argn("ip6_checkinterface", &i
, sizeof(i
));
512 case IP6_CHECKINTERFACE_WEAK_ES
:
513 case IP6_CHECKINTERFACE_HYBRID_ES
:
514 case IP6_CHECKINTERFACE_STRONG_ES
:
515 ip6_checkinterface
= i
;
525 icmp6_init(NULL
, dp
);
526 addrsel_policy_init();
529 * P2P interfaces often route the local address to the loopback
530 * interface. At this point, lo0 hasn't been initialized yet, which
531 * means that we need to delay the IPv6 configuration of lo0.
533 net_init_add(ip6_init_delayed
);
535 unguard
= domain_unguard_deploy();
536 i
= proto_register_input(PF_INET6
, ip6_proto_input
, NULL
, 0);
538 panic("%s: failed to register PF_INET6 protocol: %d\n",
542 domain_unguard_release(unguard
);
546 ip6_init_delayed(void)
548 (void) in6_ifattach_prelim(lo_ifp
);
550 /* timer for regeneranation of temporary addresses randomize ID */
551 timeout(in6_tmpaddrtimer
, NULL
,
552 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
553 ip6_temp_regen_advance
) * hz
);
561 ip6_input_adjust(struct mbuf
*m
, struct ip6_hdr
*ip6
, uint32_t plen
,
564 boolean_t adjust
= TRUE
;
565 uint32_t tot_len
= sizeof(*ip6
) + plen
;
567 ASSERT(m_pktlen(m
) > tot_len
);
570 * Invalidate hardware checksum info if ip6_adj_clear_hwcksum
571 * is set; useful to handle buggy drivers. Note that this
572 * should not be enabled by default, as we may get here due
573 * to link-layer padding.
575 if (ip6_adj_clear_hwcksum
&&
576 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) &&
577 !(inifp
->if_flags
& IFF_LOOPBACK
) &&
578 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
579 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
580 m
->m_pkthdr
.csum_data
= 0;
581 ip6stat
.ip6s_adj_hwcsum_clr
++;
585 * If partial checksum information is available, subtract
586 * out the partial sum of postpended extraneous bytes, and
587 * update the checksum metadata accordingly. By doing it
588 * here, the upper layer transport only needs to adjust any
589 * prepended extraneous bytes (else it will do both.)
591 if (ip6_adj_partial_sum
&&
592 (m
->m_pkthdr
.csum_flags
& (CSUM_DATA_VALID
| CSUM_PARTIAL
)) ==
593 (CSUM_DATA_VALID
| CSUM_PARTIAL
)) {
594 m
->m_pkthdr
.csum_rx_val
= m_adj_sum16(m
,
595 m
->m_pkthdr
.csum_rx_start
, m
->m_pkthdr
.csum_rx_start
,
596 (tot_len
- m
->m_pkthdr
.csum_rx_start
),
597 m
->m_pkthdr
.csum_rx_val
);
598 } else if ((m
->m_pkthdr
.csum_flags
&
599 (CSUM_DATA_VALID
| CSUM_PARTIAL
)) ==
600 (CSUM_DATA_VALID
| CSUM_PARTIAL
)) {
602 * If packet has partial checksum info and we decided not
603 * to subtract the partial sum of postpended extraneous
604 * bytes here (not the default case), leave that work to
605 * be handled by the other layers. For now, only TCP, UDP
606 * layers are capable of dealing with this. For all other
607 * protocols (including fragments), trim and ditch the
608 * partial sum as those layers might not implement partial
609 * checksumming (or adjustment) at all.
611 if (ip6
->ip6_nxt
== IPPROTO_TCP
||
612 ip6
->ip6_nxt
== IPPROTO_UDP
) {
615 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
616 m
->m_pkthdr
.csum_data
= 0;
617 ip6stat
.ip6s_adj_hwcsum_clr
++;
623 if (m
->m_len
== m
->m_pkthdr
.len
) {
625 m
->m_pkthdr
.len
= tot_len
;
627 m_adj(m
, tot_len
- m
->m_pkthdr
.len
);
631 static ip6_check_if_result_t
632 ip6_input_check_interface(struct mbuf
*m
, struct ip6_hdr
*ip6
, struct ifnet
*inifp
, struct route_in6
*rin6
, struct ifnet
**deliverifp
)
634 struct in6_ifaddr
*ia6
= NULL
;
635 struct in6_addr tmp_dst
= ip6
->ip6_dst
; /* copy to avoid unaligned access */
636 struct in6_ifaddr
*best_ia6
= NULL
;
637 ip6_check_if_result_t result
= IP6_CHECK_IF_NONE
;
642 * Check for exact addresses in the hash bucket.
644 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
645 TAILQ_FOREACH(ia6
, IN6ADDR_HASH(&tmp_dst
), ia6_hash
) {
647 * TODO: should we accept loopbacl
649 if (IN6_ARE_ADDR_EQUAL(&ia6
->ia_addr
.sin6_addr
, &tmp_dst
)) {
650 if ((ia6
->ia6_flags
& (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
))) {
654 if (ia6
->ia_ifp
== inifp
) {
656 * TODO: should we also accept locally originated packets
657 * or from loopback ???
662 * Continue the loop in case there's a exact match with another
667 if (best_ia6
!= NULL
) {
668 if (best_ia6
->ia_ifp
!= inifp
&& ip6_forwarding
== 0 &&
669 ((ip6_checkinterface
== IP6_CHECKINTERFACE_HYBRID_ES
&&
670 (best_ia6
->ia_ifp
->if_family
== IFNET_FAMILY_IPSEC
||
671 best_ia6
->ia_ifp
->if_family
== IFNET_FAMILY_UTUN
)) ||
672 ip6_checkinterface
== IP6_CHECKINTERFACE_STRONG_ES
)) {
674 * Drop when interface address check is strict and forwarding
677 result
= IP6_CHECK_IF_DROP
;
679 result
= IP6_CHECK_IF_OURS
;
680 *deliverifp
= best_ia6
->ia_ifp
;
681 ip6_setdstifaddr_info(m
, 0, best_ia6
);
684 lck_rw_done(&in6_ifaddr_rwlock
);
686 if (result
== IP6_CHECK_IF_NONE
) {
688 * Slow path: route lookup.
690 struct sockaddr_in6
*dst6
;
692 dst6
= SIN6(&rin6
->ro_dst
);
693 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
694 dst6
->sin6_family
= AF_INET6
;
695 dst6
->sin6_addr
= ip6
->ip6_dst
;
697 rtalloc_scoped_ign((struct route
*)rin6
,
698 RTF_PRCLONING
, IFSCOPE_NONE
);
699 if (rin6
->ro_rt
!= NULL
) {
700 RT_LOCK_SPIN(rin6
->ro_rt
);
703 #define rt6_key(r) (SIN6((r)->rt_nodes->rn_key))
706 * Accept the packet if the forwarding interface to the destination
707 * according to the routing table is the loopback interface,
708 * unless the associated route has a gateway.
709 * Note that this approach causes to accept a packet if there is a
710 * route to the loopback interface for the destination of the packet.
711 * But we think it's even useful in some situations, e.g. when using
712 * a special daemon which wants to intercept the packet.
714 * XXX: some OSes automatically make a cloned route for the destination
715 * of an outgoing packet. If the outgoing interface of the packet
716 * is a loopback one, the kernel would consider the packet to be
717 * accepted, even if we have no such address assinged on the interface.
718 * We check the cloned flag of the route entry to reject such cases,
719 * assuming that route entries for our own addresses are not made by
720 * cloning (it should be true because in6_addloop explicitly installs
721 * the host route). However, we might have to do an explicit check
722 * while it would be less efficient. Or, should we rather install a
723 * reject route for such a case?
725 if (rin6
->ro_rt
!= NULL
&&
726 (rin6
->ro_rt
->rt_flags
& (RTF_HOST
| RTF_GATEWAY
)) == RTF_HOST
&&
728 !(rin6
->ro_rt
->rt_flags
& RTF_WASCLONED
) &&
730 rin6
->ro_rt
->rt_ifp
->if_type
== IFT_LOOP
) {
731 ia6
= (struct in6_ifaddr
*)rin6
->ro_rt
->rt_ifa
;
733 * Packets to a tentative, duplicated, or somehow invalid
734 * address must not be accepted.
736 * For performance, test without acquiring the address lock;
737 * a lot of things in the address are set once and never
738 * changed (e.g. ia_ifp.)
740 if (!(ia6
->ia6_flags
& IN6_IFF_NOTREADY
)) {
741 /* this address is ready */
742 result
= IP6_CHECK_IF_OURS
;
743 *deliverifp
= ia6
->ia_ifp
; /* correct? */
745 * record dst address information into mbuf.
747 (void) ip6_setdstifaddr_info(m
, 0, ia6
);
751 if (rin6
->ro_rt
!= NULL
) {
752 RT_UNLOCK(rin6
->ro_rt
);
756 if (result
== IP6_CHECK_IF_NONE
) {
757 if (ip6_forwarding
== 0) {
758 result
= IP6_CHECK_IF_DROP
;
760 result
= IP6_CHECK_IF_FORWARD
;
761 ip6_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
765 if (result
== IP6_CHECK_IF_OURS
&& *deliverifp
!= inifp
) {
766 ASSERT(*deliverifp
!= NULL
);
767 ip6stat
.ip6s_rcv_if_weak_match
++;
769 /* Logging is too noisy when forwarding is enabled */
770 if (ip6_checkinterface_debug
!= IP6_CHECKINTERFACE_WEAK_ES
&& ip6_forwarding
!= 0) {
771 char src_str
[MAX_IPv6_STR_LEN
];
772 char dst_str
[MAX_IPv6_STR_LEN
];
774 inet_ntop(AF_INET6
, &ip6
->ip6_src
, src_str
, sizeof(src_str
));
775 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dst_str
, sizeof(dst_str
));
776 os_log_info(OS_LOG_DEFAULT
,
777 "%s: weak ES interface match to %s for packet from %s to %s proto %u received via %s",
778 __func__
, (*deliverifp
)->if_xname
, src_str
, dst_str
, ip6
->ip6_nxt
, inifp
->if_xname
);
780 } else if (result
== IP6_CHECK_IF_DROP
) {
781 ip6stat
.ip6s_rcv_if_no_match
++;
782 if (ip6_checkinterface_debug
> 0) {
783 char src_str
[MAX_IPv6_STR_LEN
];
784 char dst_str
[MAX_IPv6_STR_LEN
];
786 inet_ntop(AF_INET6
, &ip6
->ip6_src
, src_str
, sizeof(src_str
));
787 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dst_str
, sizeof(dst_str
));
788 os_log_info(OS_LOG_DEFAULT
,
789 "%s: no interface match for packet from %s to %s proto %u received via %s",
790 __func__
, src_str
, dst_str
, ip6
->ip6_nxt
, inifp
->if_xname
);
798 ip6_input(struct mbuf
*m
)
801 int off
= sizeof(struct ip6_hdr
), nest
;
803 u_int32_t rtalert
= ~0;
804 int nxt
= 0, ours
= 0;
805 struct ifnet
*inifp
, *deliverifp
= NULL
;
806 ipfilter_t inject_ipfref
= NULL
;
810 struct ip_fw_args args
= {};
811 #endif /* DUMMYNET */
812 struct route_in6 rin6
= {};
815 * Check if the packet we received is valid after interface filter
818 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
819 inifp
= m
->m_pkthdr
.rcvif
;
820 VERIFY(inifp
!= NULL
);
822 /* Perform IP header alignment fixup, if needed */
823 IP6_HDR_ALIGNMENT_FIXUP(m
, inifp
, return );
825 m
->m_pkthdr
.pkt_flags
&= ~PKTF_FORWARDED
;
828 * should the inner packet be considered authentic?
829 * see comment in ah4_input().
831 m
->m_flags
&= ~M_AUTHIPHDR
;
832 m
->m_flags
&= ~M_AUTHIPDGM
;
836 * make sure we don't have onion peering information into m_aux.
841 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
842 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
843 struct dn_pkt_tag
*dn_tag
;
845 dn_tag
= (struct dn_pkt_tag
*)(tag
+ 1);
847 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
849 m_tag_delete(m
, tag
);
852 if (args
.fwa_pf_rule
) {
853 ip6
= mtod(m
, struct ip6_hdr
*); /* In case PF got disabled */
857 #endif /* DUMMYNET */
860 * No need to proccess packet twice if we've already seen it.
862 inject_ipfref
= ipf_get_inject_filter(m
);
863 if (inject_ipfref
!= NULL
) {
864 ip6
= mtod(m
, struct ip6_hdr
*);
875 if (m
->m_flags
& M_EXT
) {
876 if (m
->m_next
!= NULL
) {
877 ip6stat
.ip6s_mext2m
++;
879 ip6stat
.ip6s_mext1
++;
882 #define M2MMAX (sizeof (ip6stat.ip6s_m2m) / sizeof (ip6stat.ip6s_m2m[0]))
883 if (m
->m_next
!= NULL
) {
884 if (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) {
886 ip6stat
.ip6s_m2m
[ifnet_index(lo_ifp
)]++;
887 } else if (inifp
->if_index
< M2MMAX
) {
888 ip6stat
.ip6s_m2m
[inifp
->if_index
]++;
890 ip6stat
.ip6s_m2m
[0]++;
899 * Drop the packet if IPv6 operation is disabled on the interface.
901 if (inifp
->if_eflags
& IFEF_IPV6_DISABLED
) {
905 in6_ifstat_inc_na(inifp
, ifs6_in_receive
);
906 ip6stat
.ip6s_total
++;
909 * L2 bridge code and some other code can return mbuf chain
910 * that does not conform to KAME requirement. too bad.
911 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
913 if (m
->m_next
!= NULL
&& m
->m_pkthdr
.len
< MCLBYTES
) {
916 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
920 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
921 MCLGET(n
, M_DONTWAIT
);
922 if ((n
->m_flags
& M_EXT
) == 0) {
931 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
932 n
->m_len
= m
->m_pkthdr
.len
;
936 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), { goto done
; });
938 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
939 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == 0) {
940 ip6stat
.ip6s_toosmall
++;
941 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
946 ip6
= mtod(m
, struct ip6_hdr
*);
948 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
949 ip6stat
.ip6s_badvers
++;
950 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
954 ip6stat
.ip6s_nxthist
[ip6
->ip6_nxt
]++;
957 * Check against address spoofing/corruption.
959 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) &&
960 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
)) {
961 ip6stat
.ip6s_badscope
++;
962 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
965 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
) ||
966 IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_dst
)) {
968 * XXX: "badscope" is not very suitable for a multicast source.
970 ip6stat
.ip6s_badscope
++;
971 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
974 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
) &&
975 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
977 * In this case, the packet should come from the loopback
978 * interface. However, we cannot just check the if_flags,
979 * because ip6_mloopback() passes the "actual" interface
980 * as the outgoing/incoming interface.
982 ip6stat
.ip6s_badscope
++;
983 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
988 * The following check is not documented in specs. A malicious
989 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
990 * and bypass security checks (act as if it was from 127.0.0.1 by using
991 * IPv6 src ::ffff:127.0.0.1). Be cautious.
993 * This check chokes if we are in an SIIT cloud. As none of BSDs
994 * support IPv4-less kernel compilation, we cannot support SIIT
995 * environment at all. So, it makes more sense for us to reject any
996 * malicious packets for non-SIIT environment, than try to do a
997 * partial support for SIIT environment.
999 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
1000 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
1001 ip6stat
.ip6s_badscope
++;
1002 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
1007 * Reject packets with IPv4 compatible addresses (auto tunnel).
1009 * The code forbids auto tunnel relay case in RFC1933 (the check is
1010 * stronger than RFC1933). We may want to re-enable it if mech-xx
1011 * is revised to forbid relaying case.
1013 if (IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_src
) ||
1014 IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_dst
)) {
1015 ip6stat
.ip6s_badscope
++;
1016 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
1022 * Naively assume we can attribute inbound data to the route we would
1023 * use to send to this destination. Asymetric routing breaks this
1024 * assumption, but it still allows us to account for traffic from
1025 * a remote node in the routing table.
1026 * this has a very significant performance impact so we bypass
1027 * if nstat_collect is disabled. We may also bypass if the
1028 * protocol is tcp in the future because tcp will have a route that
1029 * we can use to attribute the data to. That does mean we would not
1030 * account for forwarded tcp traffic.
1032 if (nstat_collect
) {
1033 struct rtentry
*rte
=
1034 ifnet_cached_rtlookup_inet6(inifp
, &ip6
->ip6_src
);
1036 nstat_route_rx(rte
, 1, m
->m_pkthdr
.len
, 0);
1043 #endif /* DUMMYNET */
1045 /* Invoke inbound packet filter */
1046 if (PF_IS_ENABLED
) {
1049 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET6
, TRUE
, &args
);
1050 #else /* !DUMMYNET */
1051 error
= pf_af_hook(inifp
, NULL
, &m
, AF_INET6
, TRUE
, NULL
);
1052 #endif /* !DUMMYNET */
1053 if (error
!= 0 || m
== NULL
) {
1055 panic("%s: unexpected packet %p\n",
1059 /* Already freed by callee */
1062 ip6
= mtod(m
, struct ip6_hdr
*);
1066 /* drop packets if interface ID portion is already filled */
1067 if (!(inifp
->if_flags
& IFF_LOOPBACK
) &&
1068 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1069 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
) &&
1070 ip6
->ip6_src
.s6_addr16
[1]) {
1071 ip6stat
.ip6s_badscope
++;
1074 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
) &&
1075 ip6
->ip6_dst
.s6_addr16
[1]) {
1076 ip6stat
.ip6s_badscope
++;
1081 if (m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
) {
1082 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1083 ip6
->ip6_src
.s6_addr16
[1] =
1084 htons(m
->m_pkthdr
.src_ifindex
);
1086 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
1087 ip6
->ip6_dst
.s6_addr16
[1] =
1088 htons(m
->m_pkthdr
.dst_ifindex
);
1091 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1092 ip6
->ip6_src
.s6_addr16
[1] = htons(inifp
->if_index
);
1094 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
1095 ip6
->ip6_dst
.s6_addr16
[1] = htons(inifp
->if_index
);
1102 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1103 struct in6_multi
*in6m
= NULL
;
1105 in6_ifstat_inc_na(inifp
, ifs6_in_mcast
);
1107 * See if we belong to the destination multicast group on the
1108 * arrival interface.
1110 in6_multihead_lock_shared();
1111 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, inifp
, in6m
);
1112 in6_multihead_lock_done();
1116 } else if (!nd6_prproxy
) {
1117 ip6stat
.ip6s_notmember
++;
1118 ip6stat
.ip6s_cantforward
++;
1119 in6_ifstat_inc(inifp
, ifs6_in_discard
);
1124 * record dst address information into mbuf, if we don't have one yet.
1125 * note that we are unable to record it, if the address is not listed
1126 * as our interface address (e.g. multicast addresses, etc.)
1128 if (deliverifp
!= NULL
) {
1129 struct in6_ifaddr
*ia6
= NULL
;
1131 ia6
= in6_ifawithifp(deliverifp
, &ip6
->ip6_dst
);
1133 (void) ip6_setdstifaddr_info(m
, 0, ia6
);
1134 IFA_REMREF(&ia6
->ia_ifa
);
1136 (void) ip6_setdstifaddr_info(m
, inifp
->if_index
, NULL
);
1144 ip6_check_if_result_t check_if_result
= IP6_CHECK_IF_NONE
;
1145 check_if_result
= ip6_input_check_interface(m
, ip6
, inifp
, &rin6
, &deliverifp
);
1146 ASSERT(check_if_result
!= IP6_CHECK_IF_NONE
);
1147 if (check_if_result
== IP6_CHECK_IF_OURS
) {
1150 } else if (check_if_result
== IP6_CHECK_IF_DROP
) {
1156 * Now there is no reason to process the packet if it's not our own
1157 * and we're not a router.
1159 if (!ip6_forwarding
) {
1160 ip6stat
.ip6s_cantforward
++;
1161 in6_ifstat_inc(inifp
, ifs6_in_discard
);
1163 * Raise a kernel event if the packet received on cellular
1164 * interface is not intended for local host.
1165 * For now limit it to ICMPv6 packets.
1167 if (inifp
->if_type
== IFT_CELLULAR
&&
1168 ip6
->ip6_nxt
== IPPROTO_ICMPV6
) {
1169 in6_ifstat_inc(inifp
, ifs6_cantfoward_icmp6
);
1176 * Process Hop-by-Hop options header if it's contained.
1177 * m may be modified in ip6_hopopts_input().
1178 * If a JumboPayload option is included, plen will also be modified.
1180 plen
= (u_int32_t
)ntohs(ip6
->ip6_plen
);
1181 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
1182 struct ip6_hbh
*hbh
;
1185 * Mark the packet to imply that HBH option has been checked.
1186 * This can only be true is the packet came in unfragmented
1187 * or if the option is in the first fragment
1189 m
->m_pkthdr
.pkt_flags
|= PKTF_HBH_CHKED
;
1190 if (ip6_hopopts_input(&plen
, &rtalert
, &m
, &off
)) {
1191 #if 0 /* touches NULL pointer */
1192 in6_ifstat_inc(inifp
, ifs6_in_discard
);
1194 goto done
; /* m have already been freed */
1197 /* adjust pointer */
1198 ip6
= mtod(m
, struct ip6_hdr
*);
1201 * if the payload length field is 0 and the next header field
1202 * indicates Hop-by-Hop Options header, then a Jumbo Payload
1203 * option MUST be included.
1205 if (ip6
->ip6_plen
== 0 && plen
== 0) {
1207 * Note that if a valid jumbo payload option is
1208 * contained, ip6_hopopts_input() must set a valid
1209 * (non-zero) payload length to the variable plen.
1211 ip6stat
.ip6s_badoptions
++;
1212 in6_ifstat_inc(inifp
, ifs6_in_discard
);
1213 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
1214 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_HEADER
,
1215 (caddr_t
)&ip6
->ip6_plen
- (caddr_t
)ip6
);
1218 /* ip6_hopopts_input() ensures that mbuf is contiguous */
1219 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
1220 nxt
= hbh
->ip6h_nxt
;
1223 * If we are acting as a router and the packet contains a
1224 * router alert option, see if we know the option value.
1225 * Currently, we only support the option value for MLD, in which
1226 * case we should pass the packet to the multicast routing
1229 if (rtalert
!= ~0 && ip6_forwarding
) {
1231 case IP6OPT_RTALERT_MLD
:
1236 * RFC2711 requires unrecognized values must be
1247 * Check that the amount of data in the buffers
1248 * is as at least much as the IPv6 header would have us expect.
1249 * Trim mbufs if longer than we expect.
1250 * Drop packet if shorter than we expect.
1252 if (m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
) < plen
) {
1253 ip6stat
.ip6s_tooshort
++;
1254 in6_ifstat_inc(inifp
, ifs6_in_truncated
);
1257 if (m
->m_pkthdr
.len
> sizeof(struct ip6_hdr
) + plen
) {
1258 ip6_input_adjust(m
, ip6
, plen
, inifp
);
1262 * Forward if desirable.
1264 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1265 if (!ours
&& nd6_prproxy
) {
1267 * If this isn't for us, this might be a Neighbor
1268 * Solicitation (dst is solicited-node multicast)
1269 * against an address in one of the proxied prefixes;
1270 * if so, claim the packet and let icmp6_input()
1273 ours
= nd6_prproxy_isours(m
, ip6
, NULL
, IFSCOPE_NONE
);
1275 (m
->m_pkthdr
.pkt_flags
& PKTF_PROXY_DST
));
1282 * The unicast forwarding function might return the packet
1283 * if we are proxying prefix(es), and if the packet is an
1284 * ICMPv6 packet that has failed the zone checks, but is
1285 * targetted towards a proxied address (this is optimized by
1286 * way of RTF_PROXY test.) If so, claim the packet as ours
1287 * and let icmp6_input() handle the rest. The packet's hop
1288 * limit value is kept intact (it's not decremented). This
1289 * is for supporting Neighbor Unreachability Detection between
1290 * proxied nodes on different links (src is link-local, dst
1291 * is target address.)
1293 if ((m
= ip6_forward(m
, &rin6
, 0)) == NULL
) {
1296 VERIFY(rin6
.ro_rt
!= NULL
);
1297 VERIFY(m
->m_pkthdr
.pkt_flags
& PKTF_PROXY_DST
);
1298 deliverifp
= rin6
.ro_rt
->rt_ifp
;
1302 ip6
= mtod(m
, struct ip6_hdr
*);
1305 * Malicious party may be able to use IPv4 mapped addr to confuse
1306 * tcp/udp stack and bypass security checks (act as if it was from
1307 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
1309 * For SIIT end node behavior, you may want to disable the check.
1310 * However, you will become vulnerable to attacks using IPv4 mapped
1313 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
1314 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
1315 ip6stat
.ip6s_badscope
++;
1316 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
1321 * Tell launch routine the next header
1323 ip6stat
.ip6s_delivered
++;
1324 in6_ifstat_inc_na(deliverifp
, ifs6_in_deliver
);
1330 * Perform IP header alignment fixup again, if needed. Note that
1331 * we do it once for the outermost protocol, and we assume each
1332 * protocol handler wouldn't mess with the alignment afterwards.
1334 IP6_HDR_ALIGNMENT_FIXUP(m
, inifp
, return );
1336 while (nxt
!= IPPROTO_DONE
) {
1337 struct ipfilter
*filter
;
1338 int (*pr_input
)(struct mbuf
**, int *, int);
1341 * This would imply either IPPROTO_HOPOPTS was not the first
1342 * option or it did not come in the first fragment.
1344 if (nxt
== IPPROTO_HOPOPTS
&&
1345 (m
->m_pkthdr
.pkt_flags
& PKTF_HBH_CHKED
) == 0) {
1347 * This implies that HBH option was not contained
1348 * in the first fragment
1350 ip6stat
.ip6s_badoptions
++;
1354 if (ip6_hdrnestlimit
&& (++nest
> ip6_hdrnestlimit
)) {
1355 ip6stat
.ip6s_toomanyhdr
++;
1360 * protection against faulty packet - there should be
1361 * more sanity checks in header chain processing.
1363 if (m
->m_pkthdr
.len
< off
) {
1364 ip6stat
.ip6s_tooshort
++;
1365 in6_ifstat_inc(inifp
, ifs6_in_truncated
);
1371 * enforce IPsec policy checking if we are seeing last header.
1372 * note that we do not visit this with protocols with pcb layer
1373 * code - like udp/tcp/raw ip.
1375 if ((ipsec_bypass
== 0) &&
1376 (ip6_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0) {
1377 if (ipsec6_in_reject(m
, NULL
)) {
1378 IPSEC_STAT_INCREMENT(ipsec6stat
.in_polvio
);
1387 if (!TAILQ_EMPTY(&ipv6_filters
) && !IFNET_IS_INTCOPROC(inifp
)) {
1389 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
1391 if ((struct ipfilter
*)inject_ipfref
==
1395 } else if (filter
->ipf_filter
.ipf_input
) {
1398 result
= filter
->ipf_filter
.ipf_input(
1399 filter
->ipf_filter
.cookie
,
1400 (mbuf_t
*)&m
, off
, nxt
);
1401 if (result
== EJUSTRETURN
) {
1414 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1415 struct ip6_hdr
*, ip6
, struct ifnet
*, inifp
,
1416 struct ip
*, NULL
, struct ip6_hdr
*, ip6
);
1418 if ((pr_input
= ip6_protox
[nxt
]->pr_input
) == NULL
) {
1422 } else if (!(ip6_protox
[nxt
]->pr_flags
& PR_PROTOLOCK
)) {
1423 lck_mtx_lock(inet6_domain_mutex
);
1424 nxt
= pr_input(&m
, &off
, nxt
);
1425 lck_mtx_unlock(inet6_domain_mutex
);
1427 nxt
= pr_input(&m
, &off
, nxt
);
1431 ROUTE_RELEASE(&rin6
);
1439 ip6_setsrcifaddr_info(struct mbuf
*m
, uint32_t src_idx
, struct in6_ifaddr
*ia6
)
1441 VERIFY(m
->m_flags
& M_PKTHDR
);
1444 * If the source ifaddr is specified, pick up the information
1445 * from there; otherwise just grab the passed-in ifindex as the
1446 * caller may not have the ifaddr available.
1449 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1450 m
->m_pkthdr
.src_ifindex
= ia6
->ia_ifp
->if_index
;
1452 /* See IN6_IFF comments in in6_var.h */
1453 m
->m_pkthdr
.src_iff
= (ia6
->ia6_flags
& 0xffff);
1455 m
->m_pkthdr
.src_iff
= 0;
1456 m
->m_pkthdr
.src_ifindex
= src_idx
;
1458 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1464 ip6_setdstifaddr_info(struct mbuf
*m
, uint32_t dst_idx
, struct in6_ifaddr
*ia6
)
1466 VERIFY(m
->m_flags
& M_PKTHDR
);
1469 * If the destination ifaddr is specified, pick up the information
1470 * from there; otherwise just grab the passed-in ifindex as the
1471 * caller may not have the ifaddr available.
1474 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1475 m
->m_pkthdr
.dst_ifindex
= ia6
->ia_ifp
->if_index
;
1477 /* See IN6_IFF comments in in6_var.h */
1478 m
->m_pkthdr
.dst_iff
= (ia6
->ia6_flags
& 0xffff);
1480 m
->m_pkthdr
.dst_iff
= 0;
1481 m
->m_pkthdr
.dst_ifindex
= dst_idx
;
1483 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1489 ip6_getsrcifaddr_info(struct mbuf
*m
, uint32_t *src_idx
, uint32_t *ia6f
)
1491 VERIFY(m
->m_flags
& M_PKTHDR
);
1493 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
)) {
1497 if (src_idx
!= NULL
) {
1498 *src_idx
= m
->m_pkthdr
.src_ifindex
;
1502 *ia6f
= m
->m_pkthdr
.src_iff
;
1509 ip6_getdstifaddr_info(struct mbuf
*m
, uint32_t *dst_idx
, uint32_t *ia6f
)
1511 VERIFY(m
->m_flags
& M_PKTHDR
);
1513 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
)) {
1517 if (dst_idx
!= NULL
) {
1518 *dst_idx
= m
->m_pkthdr
.dst_ifindex
;
1522 *ia6f
= m
->m_pkthdr
.dst_iff
;
1529 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1530 * included, the real payload length will be stored in plenp.
1533 ip6_hopopts_input(uint32_t *plenp
, uint32_t *rtalertp
, struct mbuf
**mp
,
1536 struct mbuf
*m
= *mp
;
1537 int off
= *offp
, hbhlen
;
1538 struct ip6_hbh
*hbh
;
1541 /* validation of the length of the header */
1542 IP6_EXTHDR_CHECK(m
, off
, sizeof(*hbh
), return (-1));
1543 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1544 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1546 IP6_EXTHDR_CHECK(m
, off
, hbhlen
, return (-1));
1547 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1549 hbhlen
-= sizeof(struct ip6_hbh
);
1550 opt
= (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
);
1552 if (ip6_process_hopopts(m
, (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
),
1553 hbhlen
, rtalertp
, plenp
) < 0) {
1563 * Search header for all Hop-by-hop options and process each option.
1564 * This function is separate from ip6_hopopts_input() in order to
1565 * handle a case where the sending node itself process its hop-by-hop
1566 * options header. In such a case, the function is called from ip6_output().
1568 * The function assumes that hbh header is located right after the IPv6 header
1569 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1570 * opthead + hbhlen is located in continuous memory region.
1573 ip6_process_hopopts(struct mbuf
*m
, u_int8_t
*opthead
, int hbhlen
,
1574 u_int32_t
*rtalertp
, u_int32_t
*plenp
)
1576 struct ip6_hdr
*ip6
;
1578 u_int8_t
*opt
= opthead
;
1579 u_int16_t rtalert_val
;
1580 u_int32_t jumboplen
;
1581 const int erroff
= sizeof(struct ip6_hdr
) + sizeof(struct ip6_hbh
);
1583 for (; hbhlen
> 0; hbhlen
-= optlen
, opt
+= optlen
) {
1589 if (hbhlen
< IP6OPT_MINLEN
) {
1590 ip6stat
.ip6s_toosmall
++;
1593 optlen
= *(opt
+ 1) + 2;
1595 case IP6OPT_ROUTER_ALERT
:
1596 /* XXX may need check for alignment */
1597 if (hbhlen
< IP6OPT_RTALERT_LEN
) {
1598 ip6stat
.ip6s_toosmall
++;
1601 if (*(opt
+ 1) != IP6OPT_RTALERT_LEN
- 2) {
1603 icmp6_error(m
, ICMP6_PARAM_PROB
,
1604 ICMP6_PARAMPROB_HEADER
,
1605 erroff
+ opt
+ 1 - opthead
);
1608 optlen
= IP6OPT_RTALERT_LEN
;
1609 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&rtalert_val
, 2);
1610 *rtalertp
= ntohs(rtalert_val
);
1613 /* XXX may need check for alignment */
1614 if (hbhlen
< IP6OPT_JUMBO_LEN
) {
1615 ip6stat
.ip6s_toosmall
++;
1618 if (*(opt
+ 1) != IP6OPT_JUMBO_LEN
- 2) {
1620 icmp6_error(m
, ICMP6_PARAM_PROB
,
1621 ICMP6_PARAMPROB_HEADER
,
1622 erroff
+ opt
+ 1 - opthead
);
1625 optlen
= IP6OPT_JUMBO_LEN
;
1628 * IPv6 packets that have non 0 payload length
1629 * must not contain a jumbo payload option.
1631 ip6
= mtod(m
, struct ip6_hdr
*);
1632 if (ip6
->ip6_plen
) {
1633 ip6stat
.ip6s_badoptions
++;
1634 icmp6_error(m
, ICMP6_PARAM_PROB
,
1635 ICMP6_PARAMPROB_HEADER
,
1636 erroff
+ opt
- opthead
);
1641 * We may see jumbolen in unaligned location, so
1642 * we'd need to perform bcopy().
1644 bcopy(opt
+ 2, &jumboplen
, sizeof(jumboplen
));
1645 jumboplen
= (u_int32_t
)htonl(jumboplen
);
1649 * if there are multiple jumbo payload options,
1650 * *plenp will be non-zero and the packet will be
1652 * the behavior may need some debate in ipngwg -
1653 * multiple options does not make sense, however,
1654 * there's no explicit mention in specification.
1657 ip6stat
.ip6s_badoptions
++;
1658 icmp6_error(m
, ICMP6_PARAM_PROB
,
1659 ICMP6_PARAMPROB_HEADER
,
1660 erroff
+ opt
+ 2 - opthead
);
1666 * jumbo payload length must be larger than 65535.
1668 if (jumboplen
<= IPV6_MAXPACKET
) {
1669 ip6stat
.ip6s_badoptions
++;
1670 icmp6_error(m
, ICMP6_PARAM_PROB
,
1671 ICMP6_PARAMPROB_HEADER
,
1672 erroff
+ opt
+ 2 - opthead
);
1678 default: /* unknown option */
1679 if (hbhlen
< IP6OPT_MINLEN
) {
1680 ip6stat
.ip6s_toosmall
++;
1683 optlen
= ip6_unknown_opt(opt
, m
,
1684 erroff
+ opt
- opthead
);
1701 * Unknown option processing.
1702 * The third argument `off' is the offset from the IPv6 header to the option,
1703 * which is necessary if the IPv6 header the and option header and IPv6 header
1704 * is not continuous in order to return an ICMPv6 error.
1707 ip6_unknown_opt(uint8_t *optp
, struct mbuf
*m
, int off
)
1709 struct ip6_hdr
*ip6
;
1711 switch (IP6OPT_TYPE(*optp
)) {
1712 case IP6OPT_TYPE_SKIP
: /* ignore the option */
1713 return (int)*(optp
+ 1);
1715 case IP6OPT_TYPE_DISCARD
: /* silently discard */
1719 case IP6OPT_TYPE_FORCEICMP
: /* send ICMP even if multicasted */
1720 ip6stat
.ip6s_badoptions
++;
1721 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_OPTION
, off
);
1724 case IP6OPT_TYPE_ICMP
: /* send ICMP if not multicasted */
1725 ip6stat
.ip6s_badoptions
++;
1726 ip6
= mtod(m
, struct ip6_hdr
*);
1727 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
1728 (m
->m_flags
& (M_BCAST
| M_MCAST
))) {
1731 icmp6_error(m
, ICMP6_PARAM_PROB
,
1732 ICMP6_PARAMPROB_OPTION
, off
);
1737 m_freem(m
); /* XXX: NOTREACHED */
1742 * Create the "control" list for this pcb.
1743 * These functions will not modify mbuf chain at all.
1745 * With KAME mbuf chain restriction:
1746 * The routine will be called from upper layer handlers like tcp6_input().
1747 * Thus the routine assumes that the caller (tcp6_input) have already
1748 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1749 * very first mbuf on the mbuf chain.
1751 * ip6_savecontrol_v4 will handle those options that are possible to be
1752 * set on a v4-mapped socket.
1753 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1754 * options and handle the v6-only ones itself.
1757 ip6_savecontrol_v4(struct inpcb
*inp
, struct mbuf
*m
, struct mbuf
**mp
,
1760 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1762 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0) {
1766 mp
= sbcreatecontrol_mbuf((caddr_t
)&tv
, sizeof(tv
),
1767 SCM_TIMESTAMP
, SOL_SOCKET
, mp
);
1772 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
1775 time
= mach_absolute_time();
1776 mp
= sbcreatecontrol_mbuf((caddr_t
)&time
, sizeof(time
),
1777 SCM_TIMESTAMP_MONOTONIC
, SOL_SOCKET
, mp
);
1782 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP_CONTINUOUS
) != 0) {
1785 time
= mach_continuous_time();
1786 mp
= sbcreatecontrol_mbuf((caddr_t
)&time
, sizeof(time
),
1787 SCM_TIMESTAMP_CONTINUOUS
, SOL_SOCKET
, mp
);
1792 if ((inp
->inp_socket
->so_flags
& SOF_RECV_TRAFFIC_CLASS
) != 0) {
1793 int tc
= m_get_traffic_class(m
);
1795 mp
= sbcreatecontrol_mbuf((caddr_t
)&tc
, sizeof(tc
),
1796 SO_TRAFFIC_CLASS
, SOL_SOCKET
, mp
);
1802 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1803 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
1804 if (v4only
!= NULL
) {
1808 // Send ECN flags for v4-mapped addresses
1809 if ((inp
->inp_flags
& IN6P_TCLASS
) != 0) {
1810 struct ip
*ip_header
= mtod(m
, struct ip
*);
1812 int tclass
= (int)(ip_header
->ip_tos
);
1813 mp
= sbcreatecontrol_mbuf((caddr_t
)&tclass
, sizeof(tclass
),
1814 IPV6_TCLASS
, IPPROTO_IPV6
, mp
);
1820 // Send IN6P_PKTINFO for v4-mapped address
1821 if ((inp
->inp_flags
& IN6P_PKTINFO
) != 0) {
1822 struct in6_pktinfo pi6
= {
1823 .ipi6_addr
= IN6ADDR_V4MAPPED_INIT
,
1824 .ipi6_ifindex
= (m
&& m
->m_pkthdr
.rcvif
) ? m
->m_pkthdr
.rcvif
->if_index
: 0,
1827 struct ip
*ip_header
= mtod(m
, struct ip
*);
1828 bcopy(&ip_header
->ip_dst
, &pi6
.ipi6_addr
.s6_addr32
[3], sizeof(struct in_addr
));
1830 mp
= sbcreatecontrol_mbuf((caddr_t
)&pi6
,
1831 sizeof(struct in6_pktinfo
),
1832 IS2292(inp
, IPV6_2292PKTINFO
, IPV6_PKTINFO
),
1841 /* RFC 2292 sec. 5 */
1842 if ((inp
->inp_flags
& IN6P_PKTINFO
) != 0) {
1843 struct in6_pktinfo pi6
;
1845 bcopy(&ip6
->ip6_dst
, &pi6
.ipi6_addr
, sizeof(struct in6_addr
));
1846 in6_clearscope(&pi6
.ipi6_addr
); /* XXX */
1848 (m
&& m
->m_pkthdr
.rcvif
) ? m
->m_pkthdr
.rcvif
->if_index
: 0;
1850 mp
= sbcreatecontrol_mbuf((caddr_t
)&pi6
,
1851 sizeof(struct in6_pktinfo
),
1852 IS2292(inp
, IPV6_2292PKTINFO
, IPV6_PKTINFO
),
1859 if ((inp
->inp_flags
& IN6P_HOPLIMIT
) != 0) {
1860 int hlim
= ip6
->ip6_hlim
& 0xff;
1862 mp
= sbcreatecontrol_mbuf((caddr_t
)&hlim
, sizeof(int),
1863 IS2292(inp
, IPV6_2292HOPLIMIT
, IPV6_HOPLIMIT
),
1870 if (v4only
!= NULL
) {
1877 ip6_savecontrol(struct inpcb
*in6p
, struct mbuf
*m
, struct mbuf
**mp
)
1880 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1884 np
= ip6_savecontrol_v4(in6p
, m
, mp
, &v4only
);
1894 if ((in6p
->inp_flags
& IN6P_TCLASS
) != 0) {
1898 flowinfo
= (u_int32_t
)ntohl(ip6
->ip6_flow
& IPV6_FLOWINFO_MASK
);
1901 tclass
= flowinfo
& 0xff;
1902 mp
= sbcreatecontrol_mbuf((caddr_t
)&tclass
, sizeof(tclass
),
1903 IPV6_TCLASS
, IPPROTO_IPV6
, mp
);
1910 * IPV6_HOPOPTS socket option. Recall that we required super-user
1911 * privilege for the option (see ip6_ctloutput), but it might be too
1912 * strict, since there might be some hop-by-hop options which can be
1913 * returned to normal user.
1914 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1916 if ((in6p
->inp_flags
& IN6P_HOPOPTS
) != 0) {
1918 * Check if a hop-by-hop options header is contatined in the
1919 * received packet, and if so, store the options as ancillary
1920 * data. Note that a hop-by-hop options header must be
1921 * just after the IPv6 header, which is assured through the
1922 * IPv6 input processing.
1924 ip6
= mtod(m
, struct ip6_hdr
*);
1925 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
1926 struct ip6_hbh
*hbh
;
1928 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
1929 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1932 * XXX: We copy the whole header even if a
1933 * jumbo payload option is included, the option which
1934 * is to be removed before returning according to
1936 * Note: this constraint is removed in RFC3542
1938 mp
= sbcreatecontrol_mbuf((caddr_t
)hbh
, hbhlen
,
1939 IS2292(in6p
, IPV6_2292HOPOPTS
, IPV6_HOPOPTS
),
1948 if ((in6p
->inp_flags
& (IN6P_RTHDR
| IN6P_DSTOPTS
)) != 0) {
1949 int nxt
= ip6
->ip6_nxt
, off
= sizeof(struct ip6_hdr
);
1952 * Search for destination options headers or routing
1953 * header(s) through the header chain, and stores each
1954 * header as ancillary data.
1955 * Note that the order of the headers remains in
1956 * the chain of ancillary data.
1958 while (1) { /* is explicit loop prevention necessary? */
1959 struct ip6_ext
*ip6e
= NULL
;
1963 * if it is not an extension header, don't try to
1964 * pull it from the chain.
1967 case IPPROTO_DSTOPTS
:
1968 case IPPROTO_ROUTING
:
1969 case IPPROTO_HOPOPTS
:
1970 case IPPROTO_AH
: /* is it possible? */
1976 if (off
+ sizeof(*ip6e
) > m
->m_len
) {
1979 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + off
);
1980 if (nxt
== IPPROTO_AH
) {
1981 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1983 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1985 if (off
+ elen
> m
->m_len
) {
1990 case IPPROTO_DSTOPTS
:
1991 if (!(in6p
->inp_flags
& IN6P_DSTOPTS
)) {
1995 mp
= sbcreatecontrol_mbuf((caddr_t
)ip6e
, elen
,
1996 IS2292(in6p
, IPV6_2292DSTOPTS
,
1997 IPV6_DSTOPTS
), IPPROTO_IPV6
, mp
);
2002 case IPPROTO_ROUTING
:
2003 if (!(in6p
->inp_flags
& IN6P_RTHDR
)) {
2007 mp
= sbcreatecontrol_mbuf((caddr_t
)ip6e
, elen
,
2008 IS2292(in6p
, IPV6_2292RTHDR
, IPV6_RTHDR
),
2014 case IPPROTO_HOPOPTS
:
2015 case IPPROTO_AH
: /* is it possible? */
2020 * other cases have been filtered in the above.
2021 * none will visit this case. here we supply
2022 * the code just in case (nxt overwritten or
2028 /* proceed with the next header. */
2030 nxt
= ip6e
->ip6e_nxt
;
2038 ip6stat
.ip6s_pktdropcntrl
++;
2039 /* XXX increment a stat to show the failure */
2045 ip6_notify_pmtu(struct inpcb
*in6p
, struct sockaddr_in6
*dst
, u_int32_t
*mtu
)
2049 struct ip6_mtuinfo mtuctl
;
2051 so
= in6p
->inp_socket
;
2053 if ((in6p
->inp_flags
& IN6P_MTU
) == 0) {
2062 if (so
== NULL
) { /* I believe this is impossible */
2063 panic("ip6_notify_pmtu: socket is NULL");
2068 if (IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
2069 (so
->so_proto
== NULL
|| so
->so_proto
->pr_protocol
== IPPROTO_TCP
)) {
2073 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
2074 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &dst
->sin6_addr
)) {
2078 bzero(&mtuctl
, sizeof(mtuctl
)); /* zero-clear for safety */
2079 mtuctl
.ip6m_mtu
= *mtu
;
2080 mtuctl
.ip6m_addr
= *dst
;
2081 if (sa6_recoverscope(&mtuctl
.ip6m_addr
, TRUE
)) {
2085 if ((m_mtu
= sbcreatecontrol((caddr_t
)&mtuctl
, sizeof(mtuctl
),
2086 IPV6_PATHMTU
, IPPROTO_IPV6
)) == NULL
) {
2090 if (sbappendaddr(&so
->so_rcv
, SA(dst
), NULL
, m_mtu
, NULL
) == 0) {
2097 * Get pointer to the previous header followed by the header
2098 * currently processed.
2099 * XXX: This function supposes that
2100 * M includes all headers,
2101 * the next header field and the header length field of each header
2103 * the sum of each header length equals to OFF.
2104 * Because of these assumptions, this function must be called very
2105 * carefully. Moreover, it will not be used in the near future when
2106 * we develop `neater' mechanism to process extension headers.
2109 ip6_get_prevhdr(struct mbuf
*m
, int off
)
2111 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2113 if (off
== sizeof(struct ip6_hdr
)) {
2114 return (char *)&ip6
->ip6_nxt
;
2117 struct ip6_ext
*ip6e
= NULL
;
2120 len
= sizeof(struct ip6_hdr
);
2122 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + len
);
2125 case IPPROTO_FRAGMENT
:
2126 len
+= sizeof(struct ip6_frag
);
2129 len
+= (ip6e
->ip6e_len
+ 2) << 2;
2132 len
+= (ip6e
->ip6e_len
+ 1) << 3;
2135 nxt
= ip6e
->ip6e_nxt
;
2138 return (char *)&ip6e
->ip6e_nxt
;
2146 * get next header offset. m will be retained.
2149 ip6_nexthdr(struct mbuf
*m
, int off
, int proto
, int *nxtp
)
2152 struct ip6_ext ip6e
;
2157 if ((m
->m_flags
& M_PKTHDR
) == 0 || m
->m_pkthdr
.len
< off
) {
2163 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6
)) {
2166 m_copydata(m
, off
, sizeof(ip6
), (caddr_t
)&ip6
);
2168 *nxtp
= ip6
.ip6_nxt
;
2173 case IPPROTO_FRAGMENT
:
2175 * terminate parsing if it is not the first fragment,
2176 * it does not make sense to parse through it.
2178 if (m
->m_pkthdr
.len
< off
+ sizeof(fh
)) {
2181 m_copydata(m
, off
, sizeof(fh
), (caddr_t
)&fh
);
2182 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
2183 if (fh
.ip6f_offlg
& IP6F_OFF_MASK
) {
2187 *nxtp
= fh
.ip6f_nxt
;
2189 off
+= sizeof(struct ip6_frag
);
2193 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
)) {
2196 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
2198 *nxtp
= ip6e
.ip6e_nxt
;
2200 off
+= (ip6e
.ip6e_len
+ 2) << 2;
2203 case IPPROTO_HOPOPTS
:
2204 case IPPROTO_ROUTING
:
2205 case IPPROTO_DSTOPTS
:
2206 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
)) {
2209 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
2211 *nxtp
= ip6e
.ip6e_nxt
;
2213 off
+= (ip6e
.ip6e_len
+ 1) << 3;
2218 case IPPROTO_IPCOMP
:
2228 * get offset for the last header in the chain. m will be kept untainted.
2231 ip6_lasthdr(struct mbuf
*m
, int off
, int proto
, int *nxtp
)
2241 newoff
= ip6_nexthdr(m
, off
, proto
, nxtp
);
2244 } else if (newoff
< off
) {
2245 return -1; /* invalid */
2246 } else if (newoff
== off
) {
2256 ip6_pkt_has_ulp(struct mbuf
*m
)
2258 int off
= 0, nxt
= IPPROTO_NONE
;
2260 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
2261 if (off
< 0 || m
->m_pkthdr
.len
< off
) {
2267 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
) {
2272 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
) {
2276 case IPPROTO_ICMPV6
:
2277 if (off
+ sizeof(uint32_t) > m
->m_pkthdr
.len
) {
2285 case IPPROTO_IPCOMP
:
2294 ip6_addaux(struct mbuf
*m
)
2298 /* Check if one is already allocated */
2299 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2300 KERNEL_TAG_TYPE_INET6
, NULL
);
2302 /* Allocate a tag */
2303 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_INET6
,
2304 sizeof(struct ip6aux
), M_DONTWAIT
, m
);
2306 /* Attach it to the mbuf */
2308 m_tag_prepend(m
, tag
);
2312 return tag
? (struct ip6aux
*)(tag
+ 1) : NULL
;
2316 ip6_findaux(struct mbuf
*m
)
2320 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2321 KERNEL_TAG_TYPE_INET6
, NULL
);
2323 return tag
? (struct ip6aux
*)(tag
+ 1) : NULL
;
2327 ip6_delaux(struct mbuf
*m
)
2331 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2332 KERNEL_TAG_TYPE_INET6
, NULL
);
2334 m_tag_delete(m
, tag
);
2344 frag6_drain(); /* fragments */
2345 in6_rtqdrain(); /* protocol cloned routes */
2346 nd6_drain(NULL
); /* cloned routes: ND6 */
2350 * System control for IP6
2353 u_char inet6ctlerrmap
[PRC_NCMDS
] = {
2355 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2356 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2357 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
2363 sysctl_reset_ip6_input_stats SYSCTL_HANDLER_ARGS
2365 #pragma unused(arg1, arg2)
2368 i
= ip6_input_measure
;
2369 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
2370 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
2374 if (i
< 0 || i
> 1) {
2378 if (ip6_input_measure
!= i
&& i
== 1) {
2379 net_perf_initialize(&net_perf
, ip6_input_measure_bins
);
2381 ip6_input_measure
= i
;
2387 sysctl_ip6_input_measure_bins SYSCTL_HANDLER_ARGS
2389 #pragma unused(arg1, arg2)
2393 i
= ip6_input_measure_bins
;
2394 error
= sysctl_handle_quad(oidp
, &i
, 0, req
);
2395 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
2399 if (!net_perf_validate_bins(i
)) {
2403 ip6_input_measure_bins
= i
;
2409 sysctl_ip6_input_getperf SYSCTL_HANDLER_ARGS
2411 #pragma unused(oidp, arg1, arg2)
2412 if (req
->oldptr
== USER_ADDR_NULL
) {
2413 req
->oldlen
= (size_t)sizeof(struct net_perf
);
2416 return SYSCTL_OUT(req
, &net_perf
, MIN(sizeof(net_perf
), req
->oldlen
));
2421 * Initialize IPv6 source address hash table.
2424 in6_ifaddrhashtbl_init(void)
2428 if (in6_ifaddrhashtbl
!= NULL
) {
2432 PE_parse_boot_argn("ina6ddr_nhash", &in6addr_nhash
,
2433 sizeof(in6addr_nhash
));
2434 if (in6addr_nhash
== 0) {
2435 in6addr_nhash
= IN6ADDR_NHASH
;
2438 MALLOC(in6_ifaddrhashtbl
, struct in6_ifaddrhashhead
*,
2439 in6addr_nhash
* sizeof(*in6_ifaddrhashtbl
),
2440 M_IFADDR
, M_WAITOK
| M_ZERO
);
2441 if (in6_ifaddrhashtbl
== NULL
) {
2442 panic("in6_ifaddrhashtbl allocation failed");
2446 * Generate the next largest prime greater than in6addr_nhash.
2448 k
= (in6addr_nhash
% 2 == 0) ? in6addr_nhash
+ 1 : in6addr_nhash
+ 2;
2451 for (i
= 3; i
* i
<= k
; i
+= 2) {
2465 sysctl_ip6_checkinterface SYSCTL_HANDLER_ARGS
2467 #pragma unused(arg1, arg2)
2470 i
= ip6_checkinterface
;
2471 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
2472 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
2477 case IP6_CHECKINTERFACE_WEAK_ES
:
2478 case IP6_CHECKINTERFACE_HYBRID_ES
:
2479 case IP6_CHECKINTERFACE_STRONG_ES
:
2480 if (ip6_checkinterface
!= i
) {
2481 ip6_checkinterface
= i
;
2482 os_log(OS_LOG_DEFAULT
, "%s: ip6_checkinterface is now %d\n",
2483 __func__
, ip6_checkinterface
);