1/*
2 * Copyright (c) 2003-2021 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/*
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
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.
44 *
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
55 * SUCH DAMAGE.
56 */
57
58/*
59 * Copyright (c) 1982, 1986, 1988, 1993
60 * The Regents of the University of California. All rights reserved.
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
64 * are met:
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.
77 *
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
88 * SUCH DAMAGE.
89 *
90 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
91 */
92
93#include <sys/param.h>
94#include <sys/systm.h>
95#include <sys/malloc.h>
96#include <sys/mbuf.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>
109
110#include <mach/mach_time.h>
111#include <mach/sdt.h>
112#include <pexpert/pexpert.h>
113#include <dev/random/randomdev.h>
114
115#include <net/if.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>
125#include <net/if_ports_used.h>
126
127#include <netinet/in.h>
128#include <netinet/in_systm.h>
129#if INET
130#include <netinet/ip.h>
131#include <netinet/ip_icmp.h>
132#endif /* INET */
133#include <netinet/kpi_ipfilter_var.h>
134#include <netinet/ip6.h>
135#include <netinet/udp.h>
136#include <netinet6/in6_var.h>
137#include <netinet6/ip6_var.h>
138#include <netinet/in_pcb.h>
139#include <netinet/icmp6.h>
140#include <netinet6/in6_ifattach.h>
141#include <netinet6/nd6.h>
142#include <netinet6/scope6_var.h>
143#include <netinet6/ip6protosw.h>
144
145#if IPSEC
146#include <netinet6/ipsec.h>
147#include <netinet6/ipsec6.h>
148extern int ipsec_bypass;
149#endif /* IPSEC */
150
151#if DUMMYNET
152#include <netinet/ip_dummynet.h>
153#endif /* DUMMYNET */
154
155/* we need it for NLOOP. */
156#include "loop.h"
157
158#if PF
159#include <net/pfvar.h>
160#endif /* PF */
161
162#include <os/log.h>
163
164struct ip6protosw *ip6_protox[IPPROTO_MAX];
165
166static LCK_GRP_DECLARE(in6_ifaddr_rwlock_grp, "in6_ifaddr_rwlock");
167LCK_RW_DECLARE(in6_ifaddr_rwlock, &in6_ifaddr_rwlock_grp);
168
169/* Protected by in6_ifaddr_rwlock */
170struct in6_ifaddrhead in6_ifaddrhead;
171uint32_t in6_ifaddrlist_genid = 0;
172struct in6_ifaddrhashhead * in6_ifaddrhashtbl;
173uint32_t in6_ifaddrhmask;
174
175#define IN6ADDR_NHASH 61
176u_int32_t in6addr_nhash = 0; /* hash table size */
177u_int32_t in6addr_hashp = 0; /* next largest prime */
178
179
180#define IN6_IFSTAT_REQUIRE_ALIGNED_64(f) \
181 _CASSERT(!(offsetof(struct in6_ifstat, f) % sizeof (uint64_t)))
182
183#define ICMP6_IFSTAT_REQUIRE_ALIGNED_64(f) \
184 _CASSERT(!(offsetof(struct icmp6_ifstat, f) % sizeof (uint64_t)))
185
186struct ip6stat ip6stat;
187
188LCK_ATTR_DECLARE(ip6_mutex_attr, 0, 0);
189LCK_GRP_DECLARE(ip6_mutex_grp, "ip6");
190
191LCK_MTX_DECLARE_ATTR(proxy6_lock, &ip6_mutex_grp, &ip6_mutex_attr);
192LCK_MTX_DECLARE_ATTR(nd6_mutex_data, &ip6_mutex_grp, &ip6_mutex_attr);
193
194extern int loopattach_done;
195extern void addrsel_policy_init(void);
196
197static int sysctl_reset_ip6_input_stats SYSCTL_HANDLER_ARGS;
198static int sysctl_ip6_input_measure_bins SYSCTL_HANDLER_ARGS;
199static int sysctl_ip6_input_getperf SYSCTL_HANDLER_ARGS;
200static void ip6_init_delayed(void);
201static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
202
203static void in6_ifaddrhashtbl_init(void);
204
205static struct m_tag *m_tag_kalloc_inet6(u_int32_t id, u_int16_t type, uint16_t len, int wait);
206static void m_tag_kfree_inet6(struct m_tag *tag);
207
208#if NSTF
209extern void stfattach(void);
210#endif /* NSTF */
211
212SYSCTL_DECL(_net_inet6_ip6);
213
214static uint32_t ip6_adj_clear_hwcksum = 0;
215SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, adj_clear_hwcksum,
216 CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_adj_clear_hwcksum, 0,
217 "Invalidate hwcksum info when adjusting length");
218
219static uint32_t ip6_adj_partial_sum = 1;
220SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, adj_partial_sum,
221 CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_adj_partial_sum, 0,
222 "Perform partial sum adjustment of trailing bytes at IP layer");
223
224static int ip6_input_measure = 0;
225SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, input_perf,
226 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
227 &ip6_input_measure, 0, sysctl_reset_ip6_input_stats, "I", "Do time measurement");
228
229static uint64_t ip6_input_measure_bins = 0;
230SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, input_perf_bins,
231 CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_input_measure_bins, 0,
232 sysctl_ip6_input_measure_bins, "I",
233 "bins for chaining performance data histogram");
234
235static net_perf_t net_perf;
236SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, input_perf_data,
237 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
238 0, 0, sysctl_ip6_input_getperf, "S,net_perf",
239 "IP6 input performance data (struct net_perf, net/net_perf.h)");
240
241/*
242 * ip6_checkinterface controls the receive side of the models for multihoming
243 * that are discussed in RFC 1122.
244 *
245 * sysctl_ip6_checkinterface values are:
246 * IP6_CHECKINTERFACE_WEAK_ES:
247 * This corresponds to the Weak End-System model where incoming packets from
248 * any interface are accepted provided the destination address of the incoming packet
249 * is assigned to some interface.
250 *
251 * IP6_CHECKINTERFACE_HYBRID_ES:
252 * The Hybrid End-System model use the Strong End-System for tunnel interfaces
253 * (ipsec and utun) and the weak End-System model for other interfaces families.
254 * This prevents a rogue middle box to probe for signs of TCP connections
255 * that use the tunnel interface.
256 *
257 * IP6_CHECKINTERFACE_STRONG_ES:
258 * The Strong model model requires the packet arrived on an interface that
259 * is assigned the destination address of the packet.
260 *
261 * Since the routing table and transmit implementation do not implement the Strong ES model,
262 * setting this to a value different from IP6_CHECKINTERFACE_WEAK_ES may lead to unexpected results.
263 *
264 * When forwarding is enabled, the system reverts to the Weak ES model as a router
265 * is expected by design to receive packets from several interfaces to the same address.
266 */
267#define IP6_CHECKINTERFACE_WEAK_ES 0
268#define IP6_CHECKINTERFACE_HYBRID_ES 1
269#define IP6_CHECKINTERFACE_STRONG_ES 2
270
271static int ip6_checkinterface = IP6_CHECKINTERFACE_HYBRID_ES;
272
273static int sysctl_ip6_checkinterface SYSCTL_HANDLER_ARGS;
274SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, check_interface,
275 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
276 0, 0, sysctl_ip6_checkinterface, "I", "Verify packet arrives on correct interface");
277
278#if (DEBUG || DEVELOPMENT)
279#define IP6_CHECK_IFDEBUG 1
280#else
281#define IP6_CHECK_IFDEBUG 0
282#endif /* (DEBUG || DEVELOPMENT) */
283static int ip6_checkinterface_debug = IP6_CHECK_IFDEBUG;
284SYSCTL_INT(_net_inet6_ip6, OID_AUTO, checkinterface_debug, CTLFLAG_RW | CTLFLAG_LOCKED,
285 &ip6_checkinterface_debug, IP6_CHECK_IFDEBUG, "");
286
287typedef enum ip6_check_if_result {
288 IP6_CHECK_IF_NONE = 0,
289 IP6_CHECK_IF_OURS = 1,
290 IP6_CHECK_IF_DROP = 2,
291 IP6_CHECK_IF_FORWARD = 3
292} ip6_check_if_result_t;
293
294static ip6_check_if_result_t ip6_input_check_interface(struct mbuf *, struct ip6_hdr *, struct ifnet *, struct route_in6 *rin6, struct ifnet **);
295
296/*
297 * On platforms which require strict alignment (currently for anything but
298 * i386 or x86_64 or arm64), check if the IP header pointer is 32-bit aligned; if not,
299 * copy the contents of the mbuf chain into a new chain, and free the original
300 * one. Create some head room in the first mbuf of the new chain, in case
301 * it's needed later on.
302 *
303 * RFC 2460 says that IPv6 headers are 64-bit aligned, but network interfaces
304 * mostly align to 32-bit boundaries. Care should be taken never to use 64-bit
305 * load/store operations on the fields in IPv6 headers.
306 */
307#if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
308#define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0)
309#else /* !__i386__ && !__x86_64__ && !__arm64__ */
310#define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { \
311 if (!IP6_HDR_ALIGNED_P(mtod(_m, caddr_t))) { \
312 struct mbuf *_n; \
313 struct ifnet *__ifp = (_ifp); \
314 os_atomic_inc(&(__ifp)->if_alignerrs, relaxed); \
315 if (((_m)->m_flags & M_PKTHDR) && \
316 (_m)->m_pkthdr.pkt_hdr != NULL) \
317 (_m)->m_pkthdr.pkt_hdr = NULL; \
318 _n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT); \
319 if (_n == NULL) { \
320 ip6stat.ip6s_toosmall++; \
321 m_freem(_m); \
322 (_m) = NULL; \
323 _action; \
324 } else { \
325 VERIFY(_n != (_m)); \
326 (_m) = _n; \
327 } \
328 } \
329} while (0)
330#endif /* !__i386__ && !__x86_64___ && !__arm64__ */
331
332static void
333ip6_proto_input(protocol_family_t protocol, mbuf_t packet)
334{
335#pragma unused(protocol)
336#if INET
337 struct timeval start_tv;
338 if (ip6_input_measure) {
339 net_perf_start_time(npp: &net_perf, tv: &start_tv);
340 }
341#endif /* INET */
342 ip6_input(packet);
343#if INET
344 if (ip6_input_measure) {
345 net_perf_measure_time(npp: &net_perf, start: &start_tv, num_pkts: 1);
346 net_perf_histogram(npp: &net_perf, num_pkts: 1);
347 }
348#endif /* INET */
349}
350
351/*
352 * IP6 initialization: fill in IP6 protocol switch table.
353 * All protocols not implemented in kernel go to raw IP6 protocol handler.
354 */
355void
356ip6_init(struct ip6protosw *pp, struct domain *dp)
357{
358 static int ip6_initialized = 0;
359 struct protosw *pr;
360 struct timeval tv;
361 int i;
362 domain_unguard_t unguard;
363
364 domain_proto_mtx_lock_assert_held();
365 VERIFY((pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED);
366
367 _CASSERT((sizeof(struct ip6_hdr) +
368 sizeof(struct icmp6_hdr)) <= _MHLEN);
369
370 if (ip6_initialized) {
371 return;
372 }
373 ip6_initialized = 1;
374
375 eventhandler_lists_ctxt_init(evthdlr_lists_ctxt: &in6_evhdlr_ctxt);
376 (void)EVENTHANDLER_REGISTER(&in6_evhdlr_ctxt, in6_event,
377 in6_eventhdlr_callback, eventhandler_entry_dummy_arg,
378 EVENTHANDLER_PRI_ANY);
379
380 eventhandler_lists_ctxt_init(evthdlr_lists_ctxt: &in6_clat46_evhdlr_ctxt);
381 (void)EVENTHANDLER_REGISTER(&in6_clat46_evhdlr_ctxt, in6_clat46_event,
382 in6_clat46_eventhdlr_callback, eventhandler_entry_dummy_arg,
383 EVENTHANDLER_PRI_ANY);
384
385 for (i = 0; i < IN6_EVENT_MAX; i++) {
386 VERIFY(in6_event2kev_array[i].in6_event_code == i);
387 }
388
389 pr = pffindproto_locked(PF_INET6, IPPROTO_RAW, SOCK_RAW);
390 if (pr == NULL) {
391 panic("%s: Unable to find [PF_INET6,IPPROTO_RAW,SOCK_RAW]",
392 __func__);
393 /* NOTREACHED */
394 }
395
396 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
397 for (i = 0; i < IPPROTO_MAX; i++) {
398 ip6_protox[i] = (struct ip6protosw *)pr;
399 }
400 /*
401 * Cycle through IP protocols and put them into the appropriate place
402 * in ip6_protox[], skipping protocols IPPROTO_{IP,RAW}.
403 */
404 VERIFY(dp == inet6domain && dp->dom_family == PF_INET6);
405 TAILQ_FOREACH(pr, &dp->dom_protosw, pr_entry) {
406 VERIFY(pr->pr_domain == dp);
407 if (pr->pr_protocol != 0 && pr->pr_protocol != IPPROTO_RAW) {
408 /* Be careful to only index valid IP protocols. */
409 if (pr->pr_protocol < IPPROTO_MAX) {
410 ip6_protox[pr->pr_protocol] =
411 (struct ip6protosw *)pr;
412 }
413 }
414 }
415
416 TAILQ_INIT(&in6_ifaddrhead);
417 in6_ifaddrhashtbl_init();
418
419 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_receive);
420 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_hdrerr);
421 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_toobig);
422 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_noroute);
423 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_addrerr);
424 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_protounknown);
425 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_truncated);
426 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_discard);
427 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_deliver);
428 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_forward);
429 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_request);
430 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_discard);
431 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragok);
432 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragfail);
433 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragcreat);
434 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_reqd);
435 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_ok);
436 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_fail);
437 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mcast);
438 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mcast);
439
440 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_msg);
441 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_error);
442 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_dstunreach);
443 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_adminprohib);
444 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_timeexceed);
445 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_paramprob);
446 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_pkttoobig);
447 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echo);
448 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echoreply);
449 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routersolicit);
450 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routeradvert);
451 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighborsolicit);
452 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighboradvert);
453 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_redirect);
454 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldquery);
455 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldreport);
456 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mlddone);
457
458 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_msg);
459 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_error);
460 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_dstunreach);
461 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_adminprohib);
462 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_timeexceed);
463 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_paramprob);
464 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_pkttoobig);
465 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echo);
466 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echoreply);
467 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routersolicit);
468 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routeradvert);
469 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighborsolicit);
470 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighboradvert);
471 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_redirect);
472 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldquery);
473 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldreport);
474 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mlddone);
475
476 getmicrotime(&tv);
477 ip6_desync_factor =
478 (RandomULong() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR;
479
480 PE_parse_boot_argn(arg_string: "in6_embedded_scope", arg_ptr: &in6_embedded_scope, max_arg: sizeof(in6_embedded_scope));
481 PE_parse_boot_argn(arg_string: "ip6_checkinterface", arg_ptr: &i, max_arg: sizeof(i));
482 switch (i) {
483 case IP6_CHECKINTERFACE_WEAK_ES:
484 case IP6_CHECKINTERFACE_HYBRID_ES:
485 case IP6_CHECKINTERFACE_STRONG_ES:
486 ip6_checkinterface = i;
487 break;
488 default:
489 break;
490 }
491
492 in6_ifaddr_init();
493 ip6_moptions_init();
494 nd6_init();
495 frag6_init();
496 icmp6_init(NULL, dp);
497 addrsel_policy_init();
498
499 /*
500 * P2P interfaces often route the local address to the loopback
501 * interface. At this point, lo0 hasn't been initialized yet, which
502 * means that we need to delay the IPv6 configuration of lo0.
503 */
504 net_init_add(init_func: ip6_init_delayed);
505
506 unguard = domain_unguard_deploy();
507 i = proto_register_input(PF_INET6, input: ip6_proto_input, NULL, chains: 0);
508 if (i != 0) {
509 panic("%s: failed to register PF_INET6 protocol: %d",
510 __func__, i);
511 /* NOTREACHED */
512 }
513 domain_unguard_release(unguard);
514}
515
516static void
517ip6_init_delayed(void)
518{
519 (void) in6_ifattach_prelim(lo_ifp);
520
521 /* timer for regeneranation of temporary addresses randomize ID */
522 timeout(in6_tmpaddrtimer, NULL,
523 ticks: (ip6_temp_preferred_lifetime - ip6_desync_factor -
524 ip6_temp_regen_advance) * hz);
525
526#if NSTF
527 stfattach();
528#endif /* NSTF */
529}
530
531static void
532ip6_input_adjust(struct mbuf *m, struct ip6_hdr *ip6, uint32_t plen,
533 struct ifnet *inifp)
534{
535 boolean_t adjust = TRUE;
536 uint32_t tot_len = sizeof(*ip6) + plen;
537
538 ASSERT(m_pktlen(m) > tot_len);
539
540 /*
541 * Invalidate hardware checksum info if ip6_adj_clear_hwcksum
542 * is set; useful to handle buggy drivers. Note that this
543 * should not be enabled by default, as we may get here due
544 * to link-layer padding.
545 */
546 if (ip6_adj_clear_hwcksum &&
547 (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
548 !(inifp->if_flags & IFF_LOOPBACK) &&
549 !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) {
550 m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
551 m->m_pkthdr.csum_data = 0;
552 ip6stat.ip6s_adj_hwcsum_clr++;
553 }
554
555 /*
556 * If partial checksum information is available, subtract
557 * out the partial sum of postpended extraneous bytes, and
558 * update the checksum metadata accordingly. By doing it
559 * here, the upper layer transport only needs to adjust any
560 * prepended extraneous bytes (else it will do both.)
561 */
562 if (ip6_adj_partial_sum &&
563 (m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PARTIAL)) ==
564 (CSUM_DATA_VALID | CSUM_PARTIAL)) {
565 m->m_pkthdr.csum_rx_val = m_adj_sum16(m,
566 m->m_pkthdr.csum_rx_start, m->m_pkthdr.csum_rx_start,
567 (tot_len - m->m_pkthdr.csum_rx_start),
568 m->m_pkthdr.csum_rx_val);
569 } else if ((m->m_pkthdr.csum_flags &
570 (CSUM_DATA_VALID | CSUM_PARTIAL)) ==
571 (CSUM_DATA_VALID | CSUM_PARTIAL)) {
572 /*
573 * If packet has partial checksum info and we decided not
574 * to subtract the partial sum of postpended extraneous
575 * bytes here (not the default case), leave that work to
576 * be handled by the other layers. For now, only TCP, UDP
577 * layers are capable of dealing with this. For all other
578 * protocols (including fragments), trim and ditch the
579 * partial sum as those layers might not implement partial
580 * checksumming (or adjustment) at all.
581 */
582 if (ip6->ip6_nxt == IPPROTO_TCP ||
583 ip6->ip6_nxt == IPPROTO_UDP) {
584 adjust = FALSE;
585 } else {
586 m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
587 m->m_pkthdr.csum_data = 0;
588 ip6stat.ip6s_adj_hwcsum_clr++;
589 }
590 }
591
592 if (adjust) {
593 ip6stat.ip6s_adj++;
594 if (m->m_len == m->m_pkthdr.len) {
595 m->m_len = tot_len;
596 m->m_pkthdr.len = tot_len;
597 } else {
598 m_adj(m, tot_len - m->m_pkthdr.len);
599 }
600 }
601}
602static ip6_check_if_result_t
603ip6_input_check_interface(struct mbuf *m, struct ip6_hdr *ip6, struct ifnet *inifp, struct route_in6 *rin6, struct ifnet **deliverifp)
604{
605 struct in6_ifaddr *ia6 = NULL;
606 struct in6_addr tmp_dst = ip6->ip6_dst; /* copy to avoid unaligned access */
607 struct in6_ifaddr *best_ia6 = NULL;
608 uint32_t dst_ifscope = IFSCOPE_NONE;
609 ip6_check_if_result_t result = IP6_CHECK_IF_NONE;
610
611 *deliverifp = NULL;
612
613 if (m->m_pkthdr.pkt_flags & PKTF_IFAINFO) {
614 dst_ifscope = m->m_pkthdr.dst_ifindex;
615 } else {
616 dst_ifscope = inifp->if_index;
617 }
618 /*
619 * Check for exact addresses in the hash bucket.
620 */
621 lck_rw_lock_shared(lck: &in6_ifaddr_rwlock);
622 TAILQ_FOREACH(ia6, IN6ADDR_HASH(&tmp_dst), ia6_hash) {
623 /*
624 * TODO: should we accept loopback
625 */
626 if (in6_are_addr_equal_scoped(&ia6->ia_addr.sin6_addr, &tmp_dst, ia6->ia_ifp->if_index, dst_ifscope)) {
627 if ((ia6->ia6_flags & (IN6_IFF_NOTREADY | IN6_IFF_CLAT46))) {
628 continue;
629 }
630 best_ia6 = ia6;
631 if (ia6->ia_ifp == inifp) {
632 /*
633 * TODO: should we also accept locally originated packets
634 * or from loopback ???
635 */
636 break;
637 }
638 /*
639 * Continue the loop in case there's a exact match with another
640 * interface
641 */
642 }
643 }
644 if (best_ia6 != NULL) {
645 if (best_ia6->ia_ifp != inifp && ip6_forwarding == 0 &&
646 ((ip6_checkinterface == IP6_CHECKINTERFACE_HYBRID_ES &&
647 (best_ia6->ia_ifp->if_family == IFNET_FAMILY_IPSEC ||
648 best_ia6->ia_ifp->if_family == IFNET_FAMILY_UTUN)) ||
649 ip6_checkinterface == IP6_CHECKINTERFACE_STRONG_ES)) {
650 /*
651 * Drop when interface address check is strict and forwarding
652 * is disabled
653 */
654 result = IP6_CHECK_IF_DROP;
655 } else {
656 result = IP6_CHECK_IF_OURS;
657 *deliverifp = best_ia6->ia_ifp;
658 ip6_setdstifaddr_info(m, 0, best_ia6);
659 ip6_setsrcifaddr_info(m, best_ia6->ia_ifp->if_index, NULL);
660 }
661 }
662 lck_rw_done(lck: &in6_ifaddr_rwlock);
663
664 if (result == IP6_CHECK_IF_NONE) {
665 /*
666 * Slow path: route lookup.
667 */
668 struct sockaddr_in6 *dst6;
669
670 dst6 = SIN6(&rin6->ro_dst);
671 dst6->sin6_len = sizeof(struct sockaddr_in6);
672 dst6->sin6_family = AF_INET6;
673 dst6->sin6_addr = ip6->ip6_dst;
674 if (!in6_embedded_scope && IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
675 dst6->sin6_scope_id = dst_ifscope;
676 }
677 rtalloc_scoped_ign((struct route *)rin6,
678 RTF_PRCLONING, IFSCOPE_NONE);
679 if (rin6->ro_rt != NULL) {
680 RT_LOCK_SPIN(rin6->ro_rt);
681 }
682
683#define rt6_key(r) (SIN6((r)->rt_nodes->rn_key))
684
685 /*
686 * Accept the packet if the forwarding interface to the destination
687 * according to the routing table is the loopback interface,
688 * unless the associated route has a gateway.
689 * Note that this approach causes to accept a packet if there is a
690 * route to the loopback interface for the destination of the packet.
691 * But we think it's even useful in some situations, e.g. when using
692 * a special daemon which wants to intercept the packet.
693 *
694 * XXX: some OSes automatically make a cloned route for the destination
695 * of an outgoing packet. If the outgoing interface of the packet
696 * is a loopback one, the kernel would consider the packet to be
697 * accepted, even if we have no such address assinged on the interface.
698 * We check the cloned flag of the route entry to reject such cases,
699 * assuming that route entries for our own addresses are not made by
700 * cloning (it should be true because in6_addloop explicitly installs
701 * the host route). However, we might have to do an explicit check
702 * while it would be less efficient. Or, should we rather install a
703 * reject route for such a case?
704 */
705 if (rin6->ro_rt != NULL &&
706 (rin6->ro_rt->rt_flags & (RTF_HOST | RTF_GATEWAY)) == RTF_HOST &&
707#if RTF_WASCLONED
708 !(rin6->ro_rt->rt_flags & RTF_WASCLONED) &&
709#endif
710 rin6->ro_rt->rt_ifp->if_type == IFT_LOOP) {
711 ia6 = (struct in6_ifaddr *)rin6->ro_rt->rt_ifa;
712 /*
713 * Packets to a tentative, duplicated, or somehow invalid
714 * address must not be accepted.
715 *
716 * For performance, test without acquiring the address lock;
717 * a lot of things in the address are set once and never
718 * changed (e.g. ia_ifp.)
719 */
720 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
721 /* this address is ready */
722 result = IP6_CHECK_IF_OURS;
723 *deliverifp = ia6->ia_ifp; /* correct? */
724 /*
725 * record dst address information into mbuf.
726 */
727 (void) ip6_setdstifaddr_info(m, 0, ia6);
728 (void) ip6_setsrcifaddr_info(m, ia6->ia_ifp->if_index, NULL);
729 }
730 }
731
732 if (rin6->ro_rt != NULL) {
733 RT_UNLOCK(rin6->ro_rt);
734 }
735 }
736
737 if (result == IP6_CHECK_IF_NONE) {
738 if (ip6_forwarding == 0) {
739 result = IP6_CHECK_IF_DROP;
740 } else {
741 result = IP6_CHECK_IF_FORWARD;
742 ip6_setdstifaddr_info(m, inifp->if_index, NULL);
743 ip6_setsrcifaddr_info(m, inifp->if_index, NULL);
744 }
745 }
746
747 if (result == IP6_CHECK_IF_OURS && *deliverifp != inifp) {
748 ASSERT(*deliverifp != NULL);
749 ip6stat.ip6s_rcv_if_weak_match++;
750
751 /* Logging is too noisy when forwarding is enabled */
752 if (ip6_checkinterface_debug != IP6_CHECKINTERFACE_WEAK_ES && ip6_forwarding != 0) {
753 char src_str[MAX_IPv6_STR_LEN];
754 char dst_str[MAX_IPv6_STR_LEN];
755
756 inet_ntop(AF_INET6, &ip6->ip6_src, src_str, sizeof(src_str));
757 inet_ntop(AF_INET6, &ip6->ip6_dst, dst_str, sizeof(dst_str));
758 os_log_info(OS_LOG_DEFAULT,
759 "%s: weak ES interface match to %s for packet from %s to %s proto %u received via %s",
760 __func__, (*deliverifp)->if_xname, src_str, dst_str, ip6->ip6_nxt, inifp->if_xname);
761 }
762 } else if (result == IP6_CHECK_IF_DROP) {
763 ip6stat.ip6s_rcv_if_no_match++;
764 if (ip6_checkinterface_debug > 0) {
765 char src_str[MAX_IPv6_STR_LEN];
766 char dst_str[MAX_IPv6_STR_LEN];
767
768 inet_ntop(AF_INET6, &ip6->ip6_src, src_str, sizeof(src_str));
769 inet_ntop(AF_INET6, &ip6->ip6_dst, dst_str, sizeof(dst_str));
770 os_log(OS_LOG_DEFAULT,
771 "%s: no interface match for packet from %s to %s proto %u received via %s",
772 __func__, src_str, dst_str, ip6->ip6_nxt, inifp->if_xname);
773 }
774 }
775
776 return result;
777}
778
779void
780ip6_input(struct mbuf *m)
781{
782 struct ip6_hdr *ip6;
783 int off = sizeof(struct ip6_hdr), nest;
784 u_int32_t plen;
785 u_int32_t rtalert = ~0;
786 int nxt = 0, ours = 0;
787 struct ifnet *inifp, *deliverifp = NULL;
788 ipfilter_t inject_ipfref = NULL;
789 int seen = 1;
790#if DUMMYNET
791 struct m_tag *tag;
792 struct ip_fw_args args = {};
793#endif /* DUMMYNET */
794 struct route_in6 rin6 = {};
795
796 /*
797 * Check if the packet we received is valid after interface filter
798 * processing
799 */
800 MBUF_INPUT_CHECK(m, m->m_pkthdr.rcvif);
801 inifp = m->m_pkthdr.rcvif;
802 VERIFY(inifp != NULL);
803
804 /* Perform IP header alignment fixup, if needed */
805 IP6_HDR_ALIGNMENT_FIXUP(m, inifp, return );
806
807 m->m_pkthdr.pkt_flags &= ~PKTF_FORWARDED;
808#if IPSEC
809 /*
810 * should the inner packet be considered authentic?
811 * see comment in ah4_input().
812 */
813 m->m_flags &= ~M_AUTHIPHDR;
814 m->m_flags &= ~M_AUTHIPDGM;
815#endif /* IPSEC */
816
817 /*
818 * make sure we don't have onion peering information into m_aux.
819 */
820 ip6_delaux(m);
821
822#if DUMMYNET
823 if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
824 KERNEL_TAG_TYPE_DUMMYNET)) != NULL) {
825 struct dn_pkt_tag *dn_tag;
826
827 dn_tag = (struct dn_pkt_tag *)(tag->m_tag_data);
828
829 args.fwa_pf_rule = dn_tag->dn_pf_rule;
830
831 m_tag_delete(m, tag);
832 }
833
834 if (args.fwa_pf_rule) {
835 ip6 = mtod(m, struct ip6_hdr *); /* In case PF got disabled */
836
837 goto check_with_pf;
838 }
839#endif /* DUMMYNET */
840
841 /*
842 * No need to process packet twice if we've already seen it.
843 */
844 inject_ipfref = ipf_get_inject_filter(m);
845 if (inject_ipfref != NULL) {
846 ip6 = mtod(m, struct ip6_hdr *);
847 nxt = ip6->ip6_nxt;
848 seen = 0;
849 goto injectit;
850 } else {
851 seen = 1;
852 }
853
854 if (__improbable(m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT)) {
855 if_ports_used_match_mbuf(ifp: inifp, PF_INET6, m);
856 }
857
858 /*
859 * mbuf statistics
860 */
861 if (m->m_flags & M_EXT) {
862 if (m->m_next != NULL) {
863 ip6stat.ip6s_mext2m++;
864 } else {
865 ip6stat.ip6s_mext1++;
866 }
867 } else {
868#define M2MMAX (sizeof (ip6stat.ip6s_m2m) / sizeof (ip6stat.ip6s_m2m[0]))
869 if (m->m_next != NULL) {
870 if (m->m_pkthdr.pkt_flags & PKTF_LOOP) {
871 /* XXX */
872 ip6stat.ip6s_m2m[ifnet_index(interface: lo_ifp)]++;
873 } else if (inifp->if_index < M2MMAX) {
874 ip6stat.ip6s_m2m[inifp->if_index]++;
875 } else {
876 ip6stat.ip6s_m2m[0]++;
877 }
878 } else {
879 ip6stat.ip6s_m1++;
880 }
881#undef M2MMAX
882 }
883
884 /*
885 * Drop the packet if IPv6 operation is disabled on the interface.
886 */
887 if (inifp->if_eflags & IFEF_IPV6_DISABLED) {
888 goto bad;
889 }
890
891 in6_ifstat_inc_na(inifp, ifs6_in_receive);
892 ip6stat.ip6s_total++;
893
894 /*
895 * L2 bridge code and some other code can return mbuf chain
896 * that does not conform to KAME requirement. too bad.
897 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
898 */
899 if (m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
900 struct mbuf *n;
901
902 MGETHDR(n, M_DONTWAIT, MT_HEADER); /* MAC-OK */
903 if (n) {
904 M_COPY_PKTHDR(n, m);
905 }
906 if (n && m->m_pkthdr.len > MHLEN) {
907 MCLGET(n, M_DONTWAIT);
908 if ((n->m_flags & M_EXT) == 0) {
909 m_freem(n);
910 n = NULL;
911 }
912 }
913 if (n == NULL) {
914 goto bad;
915 }
916
917 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
918 n->m_len = m->m_pkthdr.len;
919 m_freem(m);
920 m = n;
921 }
922 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), { goto done; });
923
924 if (m->m_len < sizeof(struct ip6_hdr)) {
925 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
926 ip6stat.ip6s_toosmall++;
927 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
928 goto done;
929 }
930 }
931
932 ip6 = mtod(m, struct ip6_hdr *);
933
934 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
935 ip6stat.ip6s_badvers++;
936 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
937 goto bad;
938 }
939
940 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
941
942 /*
943 * Check against address spoofing/corruption.
944 */
945 if (!(m->m_pkthdr.pkt_flags & PKTF_LOOP) &&
946 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src)) {
947 ip6stat.ip6s_badscope++;
948 in6_ifstat_inc(inifp, ifs6_in_addrerr);
949 goto bad;
950 }
951 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
952 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
953 /*
954 * XXX: "badscope" is not very suitable for a multicast source.
955 */
956 ip6stat.ip6s_badscope++;
957 in6_ifstat_inc(inifp, ifs6_in_addrerr);
958 goto bad;
959 }
960 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
961 !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) {
962 /*
963 * In this case, the packet should come from the loopback
964 * interface. However, we cannot just check the if_flags,
965 * because ip6_mloopback() passes the "actual" interface
966 * as the outgoing/incoming interface.
967 */
968 ip6stat.ip6s_badscope++;
969 in6_ifstat_inc(inifp, ifs6_in_addrerr);
970 goto bad;
971 }
972
973 /*
974 * The following check is not documented in specs. A malicious
975 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
976 * and bypass security checks (act as if it was from 127.0.0.1 by using
977 * IPv6 src ::ffff:127.0.0.1). Be cautious.
978 *
979 * This check chokes if we are in an SIIT cloud. As none of BSDs
980 * support IPv4-less kernel compilation, we cannot support SIIT
981 * environment at all. So, it makes more sense for us to reject any
982 * malicious packets for non-SIIT environment, than try to do a
983 * partial support for SIIT environment.
984 */
985 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
986 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
987 ip6stat.ip6s_badscope++;
988 in6_ifstat_inc(inifp, ifs6_in_addrerr);
989 goto bad;
990 }
991
992 if (((ntohl(ip6->ip6_flow & IPV6_FLOW_ECN_MASK) >> 20) & IPTOS_ECN_ECT1) == IPTOS_ECN_ECT1) {
993 m->m_pkthdr.pkt_ext_flags |= PKTF_EXT_L4S;
994 }
995
996#if 0
997 /*
998 * Reject packets with IPv4 compatible addresses (auto tunnel).
999 *
1000 * The code forbids auto tunnel relay case in RFC1933 (the check is
1001 * stronger than RFC1933). We may want to re-enable it if mech-xx
1002 * is revised to forbid relaying case.
1003 */
1004 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
1005 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
1006 ip6stat.ip6s_badscope++;
1007 in6_ifstat_inc(inifp, ifs6_in_addrerr);
1008 goto bad;
1009 }
1010#endif
1011
1012 /*
1013 * Naively assume we can attribute inbound data to the route we would
1014 * use to send to this destination. Asymetric routing breaks this
1015 * assumption, but it still allows us to account for traffic from
1016 * a remote node in the routing table.
1017 * this has a very significant performance impact so we bypass
1018 * if nstat_collect is disabled. We may also bypass if the
1019 * protocol is tcp in the future because tcp will have a route that
1020 * we can use to attribute the data to. That does mean we would not
1021 * account for forwarded tcp traffic.
1022 */
1023 if (nstat_collect) {
1024 struct rtentry *rte =
1025 ifnet_cached_rtlookup_inet6(inifp, &ip6->ip6_src);
1026 if (rte != NULL) {
1027 nstat_route_rx(rte, packets: 1, bytes: m->m_pkthdr.len, flags: 0);
1028 rtfree(rte);
1029 }
1030 }
1031
1032#if DUMMYNET
1033check_with_pf:
1034#endif /* DUMMYNET */
1035#if PF
1036 /* Invoke inbound packet filter */
1037 if (PF_IS_ENABLED) {
1038 int error;
1039#if DUMMYNET
1040 error = pf_af_hook(inifp, NULL, &m, AF_INET6, TRUE, &args);
1041#else /* !DUMMYNET */
1042 error = pf_af_hook(inifp, NULL, &m, AF_INET6, TRUE, NULL);
1043#endif /* !DUMMYNET */
1044 if (error != 0 || m == NULL) {
1045 if (m != NULL) {
1046 panic("%s: unexpected packet %p",
1047 __func__, m);
1048 /* NOTREACHED */
1049 }
1050 /* Already freed by callee */
1051 goto done;
1052 }
1053 ip6 = mtod(m, struct ip6_hdr *);
1054 }
1055#endif /* PF */
1056
1057 /* drop packets if interface ID portion is already filled */
1058 if (!(inifp->if_flags & IFF_LOOPBACK) &&
1059 !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) {
1060 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
1061 ip6->ip6_src.s6_addr16[1]) {
1062 ip6stat.ip6s_badscope++;
1063 goto bad;
1064 }
1065 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst) &&
1066 ip6->ip6_dst.s6_addr16[1]) {
1067 ip6stat.ip6s_badscope++;
1068 goto bad;
1069 }
1070 }
1071
1072 if ((m->m_pkthdr.pkt_flags & PKTF_IFAINFO) != 0 && in6_embedded_scope) {
1073 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
1074 ip6->ip6_src.s6_addr16[1] =
1075 htons(m->m_pkthdr.src_ifindex);
1076 }
1077 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
1078 ip6->ip6_dst.s6_addr16[1] =
1079 htons(m->m_pkthdr.dst_ifindex);
1080 }
1081 } else if (in6_embedded_scope) {
1082 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
1083 ip6->ip6_src.s6_addr16[1] = htons(inifp->if_index);
1084 }
1085 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
1086 ip6->ip6_dst.s6_addr16[1] = htons(inifp->if_index);
1087 }
1088 }
1089
1090 /*
1091 * Multicast check
1092 */
1093 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1094 struct in6_multi *in6m = NULL;
1095
1096 in6_ifstat_inc_na(inifp, ifs6_in_mcast);
1097 /*
1098 * See if we belong to the destination multicast group on the
1099 * arrival interface.
1100 */
1101 in6_multihead_lock_shared();
1102 IN6_LOOKUP_MULTI(&ip6->ip6_dst, inifp, in6m);
1103 in6_multihead_lock_done();
1104 if (in6m != NULL) {
1105 IN6M_REMREF(in6m);
1106 ours = 1;
1107 } else if (!nd6_prproxy) {
1108 ip6stat.ip6s_notmember++;
1109 ip6stat.ip6s_cantforward++;
1110 in6_ifstat_inc(inifp, ifs6_in_discard);
1111 goto bad;
1112 }
1113 deliverifp = inifp;
1114 /*
1115 * record dst address information into mbuf, if we don't have one yet.
1116 * note that we are unable to record it, if the address is not listed
1117 * as our interface address (e.g. multicast addresses, etc.)
1118 */
1119 if (deliverifp != NULL) {
1120 struct in6_ifaddr *ia6 = NULL;
1121
1122 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
1123 if (ia6 != NULL) {
1124 (void) ip6_setdstifaddr_info(m, 0, ia6);
1125 (void) ip6_setsrcifaddr_info(m, ia6->ia_ifp->if_index, NULL);
1126 ifa_remref(ifa: &ia6->ia_ifa);
1127 } else {
1128 (void) ip6_setdstifaddr_info(m, inifp->if_index, NULL);
1129 (void) ip6_setsrcifaddr_info(m, inifp->if_index, NULL);
1130 }
1131 }
1132 goto hbhcheck;
1133 } else {
1134 /*
1135 * Unicast check
1136 */
1137 ip6_check_if_result_t check_if_result = IP6_CHECK_IF_NONE;
1138 check_if_result = ip6_input_check_interface(m, ip6, inifp, rin6: &rin6, deliverifp: &deliverifp);
1139 ASSERT(check_if_result != IP6_CHECK_IF_NONE);
1140 if (check_if_result == IP6_CHECK_IF_OURS) {
1141 ours = 1;
1142 goto hbhcheck;
1143 } else if (check_if_result == IP6_CHECK_IF_DROP) {
1144 goto bad;
1145 }
1146 }
1147
1148 /*
1149 * Now there is no reason to process the packet if it's not our own
1150 * and we're not a router.
1151 */
1152 if (!ip6_forwarding) {
1153 ip6stat.ip6s_cantforward++;
1154 in6_ifstat_inc(inifp, ifs6_in_discard);
1155 /*
1156 * Raise a kernel event if the packet received on cellular
1157 * interface is not intended for local host.
1158 * For now limit it to ICMPv6 packets.
1159 */
1160 if (inifp->if_type == IFT_CELLULAR &&
1161 ip6->ip6_nxt == IPPROTO_ICMPV6) {
1162 in6_ifstat_inc(inifp, ifs6_cantfoward_icmp6);
1163 }
1164 goto bad;
1165 }
1166
1167hbhcheck:
1168 /*
1169 * Process Hop-by-Hop options header if it's contained.
1170 * m may be modified in ip6_hopopts_input().
1171 * If a JumboPayload option is included, plen will also be modified.
1172 */
1173 plen = (u_int32_t)ntohs(ip6->ip6_plen);
1174 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1175 struct ip6_hbh *hbh;
1176
1177 /*
1178 * Mark the packet to imply that HBH option has been checked.
1179 * This can only be true is the packet came in unfragmented
1180 * or if the option is in the first fragment
1181 */
1182 m->m_pkthdr.pkt_flags |= PKTF_HBH_CHKED;
1183 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
1184#if 0 /* touches NULL pointer */
1185 in6_ifstat_inc(inifp, ifs6_in_discard);
1186#endif
1187 goto done; /* m have already been freed */
1188 }
1189
1190 /* adjust pointer */
1191 ip6 = mtod(m, struct ip6_hdr *);
1192
1193 /*
1194 * if the payload length field is 0 and the next header field
1195 * indicates Hop-by-Hop Options header, then a Jumbo Payload
1196 * option MUST be included.
1197 */
1198 if (ip6->ip6_plen == 0 && plen == 0) {
1199 /*
1200 * Note that if a valid jumbo payload option is
1201 * contained, ip6_hopopts_input() must set a valid
1202 * (non-zero) payload length to the variable plen.
1203 */
1204 ip6stat.ip6s_badoptions++;
1205 in6_ifstat_inc(inifp, ifs6_in_discard);
1206 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
1207 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
1208 (int)((caddr_t)&ip6->ip6_plen - (caddr_t)ip6));
1209 goto done;
1210 }
1211 /* ip6_hopopts_input() ensures that mbuf is contiguous */
1212 hbh = (struct ip6_hbh *)(ip6 + 1);
1213 nxt = hbh->ip6h_nxt;
1214
1215 /*
1216 * If we are acting as a router and the packet contains a
1217 * router alert option, see if we know the option value.
1218 * Currently, we only support the option value for MLD, in which
1219 * case we should pass the packet to the multicast routing
1220 * daemon.
1221 */
1222 if (rtalert != ~0 && ip6_forwarding) {
1223 switch (rtalert) {
1224 case IP6OPT_RTALERT_MLD:
1225 ours = 1;
1226 break;
1227 default:
1228 /*
1229 * RFC2711 requires unrecognized values must be
1230 * silently ignored.
1231 */
1232 break;
1233 }
1234 }
1235 } else {
1236 nxt = ip6->ip6_nxt;
1237 }
1238
1239 /*
1240 * Check that the amount of data in the buffers
1241 * is as at least much as the IPv6 header would have us expect.
1242 * Trim mbufs if longer than we expect.
1243 * Drop packet if shorter than we expect.
1244 */
1245 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
1246 ip6stat.ip6s_tooshort++;
1247 in6_ifstat_inc(inifp, ifs6_in_truncated);
1248 goto bad;
1249 }
1250 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
1251 ip6_input_adjust(m, ip6, plen, inifp);
1252 }
1253
1254 /*
1255 * Forward if desirable.
1256 */
1257 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1258 if (!ours && nd6_prproxy) {
1259 /*
1260 * If this isn't for us, this might be a Neighbor
1261 * Solicitation (dst is solicited-node multicast)
1262 * against an address in one of the proxied prefixes;
1263 * if so, claim the packet and let icmp6_input()
1264 * handle the rest.
1265 */
1266 ours = nd6_prproxy_isours(m, ip6, NULL, IFSCOPE_NONE);
1267 VERIFY(!ours ||
1268 (m->m_pkthdr.pkt_flags & PKTF_PROXY_DST));
1269 }
1270 if (!ours) {
1271 goto bad;
1272 }
1273 } else if (!ours) {
1274 /*
1275 * The unicast forwarding function might return the packet
1276 * if we are proxying prefix(es), and if the packet is an
1277 * ICMPv6 packet that has failed the zone checks, but is
1278 * targetted towards a proxied address (this is optimized by
1279 * way of RTF_PROXY test.) If so, claim the packet as ours
1280 * and let icmp6_input() handle the rest. The packet's hop
1281 * limit value is kept intact (it's not decremented). This
1282 * is for supporting Neighbor Unreachability Detection between
1283 * proxied nodes on different links (src is link-local, dst
1284 * is target address.)
1285 */
1286 if ((m = ip6_forward(m, &rin6, 0)) == NULL) {
1287 goto done;
1288 }
1289 VERIFY(rin6.ro_rt != NULL);
1290 VERIFY(m->m_pkthdr.pkt_flags & PKTF_PROXY_DST);
1291 deliverifp = rin6.ro_rt->rt_ifp;
1292 ours = 1;
1293 }
1294
1295 ip6 = mtod(m, struct ip6_hdr *);
1296
1297 /*
1298 * Malicious party may be able to use IPv4 mapped addr to confuse
1299 * tcp/udp stack and bypass security checks (act as if it was from
1300 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
1301 *
1302 * For SIIT end node behavior, you may want to disable the check.
1303 * However, you will become vulnerable to attacks using IPv4 mapped
1304 * source.
1305 */
1306 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
1307 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
1308 ip6stat.ip6s_badscope++;
1309 in6_ifstat_inc(inifp, ifs6_in_addrerr);
1310 goto bad;
1311 }
1312
1313 /*
1314 * Tell launch routine the next header
1315 */
1316 ip6stat.ip6s_delivered++;
1317 in6_ifstat_inc_na(deliverifp, ifs6_in_deliver);
1318
1319injectit:
1320 nest = 0;
1321
1322 /*
1323 * Perform IP header alignment fixup again, if needed. Note that
1324 * we do it once for the outermost protocol, and we assume each
1325 * protocol handler wouldn't mess with the alignment afterwards.
1326 */
1327 IP6_HDR_ALIGNMENT_FIXUP(m, inifp, return );
1328
1329 while (nxt != IPPROTO_DONE) {
1330 struct ipfilter *filter;
1331 int (*pr_input)(struct mbuf **, int *, int);
1332
1333 /*
1334 * This would imply either IPPROTO_HOPOPTS was not the first
1335 * option or it did not come in the first fragment.
1336 */
1337 if (nxt == IPPROTO_HOPOPTS &&
1338 (m->m_pkthdr.pkt_flags & PKTF_HBH_CHKED) == 0) {
1339 /*
1340 * This implies that HBH option was not contained
1341 * in the first fragment
1342 */
1343 ip6stat.ip6s_badoptions++;
1344 goto bad;
1345 }
1346
1347 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
1348 ip6stat.ip6s_toomanyhdr++;
1349 goto bad;
1350 }
1351
1352 /*
1353 * protection against faulty packet - there should be
1354 * more sanity checks in header chain processing.
1355 */
1356 if (m->m_pkthdr.len < off) {
1357 ip6stat.ip6s_tooshort++;
1358 in6_ifstat_inc(inifp, ifs6_in_truncated);
1359 goto bad;
1360 }
1361
1362#if IPSEC
1363 /*
1364 * enforce IPsec policy checking if we are seeing last header.
1365 * note that we do not visit this with protocols with pcb layer
1366 * code - like udp/tcp/raw ip.
1367 */
1368 if ((ipsec_bypass == 0) &&
1369 (ip6_protox[nxt]->pr_flags & PR_LASTHDR) != 0) {
1370 if (ipsec6_in_reject(m, NULL)) {
1371 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
1372 goto bad;
1373 }
1374 }
1375#endif /* IPSEC */
1376
1377 /*
1378 * Call IP filter
1379 */
1380 if (!TAILQ_EMPTY(&ipv6_filters) && !IFNET_IS_INTCOPROC(inifp)) {
1381 ipf_ref();
1382 TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) {
1383 if (seen == 0) {
1384 if ((struct ipfilter *)inject_ipfref ==
1385 filter) {
1386 seen = 1;
1387 }
1388 } else if (filter->ipf_filter.ipf_input) {
1389 errno_t result;
1390
1391 result = filter->ipf_filter.ipf_input(
1392 filter->ipf_filter.cookie,
1393 (mbuf_t *)&m, off, (uint8_t)nxt);
1394 if (result == EJUSTRETURN) {
1395 ipf_unref();
1396 goto done;
1397 }
1398 if (result != 0) {
1399 ipf_unref();
1400 goto bad;
1401 }
1402 }
1403 }
1404 ipf_unref();
1405 }
1406
1407 DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL,
1408 struct ip6_hdr *, ip6, struct ifnet *, inifp,
1409 struct ip *, NULL, struct ip6_hdr *, ip6);
1410
1411 if ((pr_input = ip6_protox[nxt]->pr_input) == NULL) {
1412 m_freem(m);
1413 m = NULL;
1414 nxt = IPPROTO_DONE;
1415 } else if (!(ip6_protox[nxt]->pr_flags & PR_PROTOLOCK)) {
1416 lck_mtx_lock(lck: inet6_domain_mutex);
1417 nxt = pr_input(&m, &off, nxt);
1418 lck_mtx_unlock(lck: inet6_domain_mutex);
1419 } else {
1420 nxt = pr_input(&m, &off, nxt);
1421 }
1422 }
1423done:
1424 ROUTE_RELEASE(&rin6);
1425 return;
1426bad:
1427 m_freem(m);
1428 goto done;
1429}
1430
1431void
1432ip6_setsrcifaddr_info(struct mbuf *m, uint32_t src_idx, struct in6_ifaddr *ia6)
1433{
1434 VERIFY(m->m_flags & M_PKTHDR);
1435 m->m_pkthdr.pkt_ext_flags &= ~PKTF_EXT_OUTPUT_SCOPE;
1436 /*
1437 * If the source ifaddr is specified, pick up the information
1438 * from there; otherwise just grab the passed-in ifindex as the
1439 * caller may not have the ifaddr available.
1440 */
1441 if (ia6 != NULL) {
1442 m->m_pkthdr.pkt_flags |= PKTF_IFAINFO;
1443 m->m_pkthdr.src_ifindex = ia6->ia_ifp->if_index;
1444
1445 /* See IN6_IFF comments in in6_var.h */
1446 m->m_pkthdr.src_iff = (ia6->ia6_flags & 0xffff);
1447 } else {
1448 m->m_pkthdr.src_iff = 0;
1449 m->m_pkthdr.src_ifindex = (uint16_t)src_idx;
1450 if (src_idx != 0) {
1451 m->m_pkthdr.pkt_flags |= PKTF_IFAINFO;
1452 }
1453 }
1454}
1455
1456void
1457ip6_setdstifaddr_info(struct mbuf *m, uint32_t dst_idx, struct in6_ifaddr *ia6)
1458{
1459 VERIFY(m->m_flags & M_PKTHDR);
1460 m->m_pkthdr.pkt_ext_flags &= ~PKTF_EXT_OUTPUT_SCOPE;
1461
1462 /*
1463 * If the destination ifaddr is specified, pick up the information
1464 * from there; otherwise just grab the passed-in ifindex as the
1465 * caller may not have the ifaddr available.
1466 */
1467 if (ia6 != NULL) {
1468 m->m_pkthdr.pkt_flags |= PKTF_IFAINFO;
1469 m->m_pkthdr.dst_ifindex = ia6->ia_ifp->if_index;
1470
1471 /* See IN6_IFF comments in in6_var.h */
1472 m->m_pkthdr.dst_iff = (ia6->ia6_flags & 0xffff);
1473 } else {
1474 m->m_pkthdr.dst_iff = 0;
1475 m->m_pkthdr.dst_ifindex = (uint16_t)dst_idx;
1476 if (dst_idx != 0) {
1477 m->m_pkthdr.pkt_flags |= PKTF_IFAINFO;
1478 }
1479 }
1480}
1481
1482int
1483ip6_getsrcifaddr_info(struct mbuf *m, uint32_t *src_idx, uint32_t *ia6f)
1484{
1485 VERIFY(m->m_flags & M_PKTHDR);
1486
1487 if (!(m->m_pkthdr.pkt_flags & PKTF_IFAINFO)) {
1488 return -1;
1489 }
1490
1491 if (src_idx != NULL) {
1492 *src_idx = m->m_pkthdr.src_ifindex;
1493 }
1494
1495 if (ia6f != NULL) {
1496 *ia6f = m->m_pkthdr.src_iff;
1497 }
1498
1499 return 0;
1500}
1501
1502int
1503ip6_getdstifaddr_info(struct mbuf *m, uint32_t *dst_idx, uint32_t *ia6f)
1504{
1505 VERIFY(m->m_flags & M_PKTHDR);
1506
1507 if (!(m->m_pkthdr.pkt_flags & PKTF_IFAINFO)) {
1508 return -1;
1509 }
1510
1511 if (dst_idx != NULL) {
1512 *dst_idx = m->m_pkthdr.dst_ifindex;
1513 }
1514
1515 if (ia6f != NULL) {
1516 *ia6f = m->m_pkthdr.dst_iff;
1517 }
1518
1519 return 0;
1520}
1521
1522uint32_t
1523ip6_input_getsrcifscope(struct mbuf *m)
1524{
1525 VERIFY(m->m_flags & M_PKTHDR);
1526
1527 if (m->m_pkthdr.rcvif != NULL) {
1528 return m->m_pkthdr.rcvif->if_index;
1529 }
1530
1531 uint32_t src_ifscope = IFSCOPE_NONE;
1532 ip6_getsrcifaddr_info(m, src_idx: &src_ifscope, NULL);
1533 return src_ifscope;
1534}
1535
1536uint32_t
1537ip6_input_getdstifscope(struct mbuf *m)
1538{
1539 VERIFY(m->m_flags & M_PKTHDR);
1540
1541 if (m->m_pkthdr.rcvif != NULL) {
1542 return m->m_pkthdr.rcvif->if_index;
1543 }
1544
1545 uint32_t dst_ifscope = IFSCOPE_NONE;
1546 ip6_getdstifaddr_info(m, dst_idx: &dst_ifscope, NULL);
1547 return dst_ifscope;
1548}
1549
1550/*
1551 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1552 * included, the real payload length will be stored in plenp.
1553 */
1554static int
1555ip6_hopopts_input(uint32_t *plenp, uint32_t *rtalertp, struct mbuf **mp,
1556 int *offp)
1557{
1558 struct mbuf *m = *mp;
1559 int off = *offp, hbhlen;
1560 struct ip6_hbh *hbh;
1561 u_int8_t *opt;
1562
1563 /* validation of the length of the header */
1564 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), return (-1));
1565 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1566 hbhlen = (hbh->ip6h_len + 1) << 3;
1567
1568 IP6_EXTHDR_CHECK(m, off, hbhlen, return (-1));
1569 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1570 off += hbhlen;
1571 hbhlen -= sizeof(struct ip6_hbh);
1572 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
1573
1574 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
1575 hbhlen, rtalertp, plenp) < 0) {
1576 return -1;
1577 }
1578
1579 *offp = off;
1580 *mp = m;
1581 return 0;
1582}
1583
1584/*
1585 * Search header for all Hop-by-hop options and process each option.
1586 * This function is separate from ip6_hopopts_input() in order to
1587 * handle a case where the sending node itself process its hop-by-hop
1588 * options header. In such a case, the function is called from ip6_output().
1589 *
1590 * The function assumes that hbh header is located right after the IPv6 header
1591 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1592 * opthead + hbhlen is located in continuous memory region.
1593 */
1594int
1595ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
1596 u_int32_t *rtalertp, u_int32_t *plenp)
1597{
1598 struct ip6_hdr *ip6;
1599 int optlen = 0;
1600 u_int8_t *opt = opthead;
1601 u_int16_t rtalert_val;
1602 u_int32_t jumboplen;
1603 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1604
1605 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1606 switch (*opt) {
1607 case IP6OPT_PAD1:
1608 optlen = 1;
1609 break;
1610 case IP6OPT_PADN:
1611 if (hbhlen < IP6OPT_MINLEN) {
1612 ip6stat.ip6s_toosmall++;
1613 goto bad;
1614 }
1615 optlen = *(opt + 1) + 2;
1616 break;
1617 case IP6OPT_ROUTER_ALERT:
1618 /* XXX may need check for alignment */
1619 if (hbhlen < IP6OPT_RTALERT_LEN) {
1620 ip6stat.ip6s_toosmall++;
1621 goto bad;
1622 }
1623 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1624 /* XXX stat */
1625 icmp6_error(m, ICMP6_PARAM_PROB,
1626 ICMP6_PARAMPROB_HEADER,
1627 (int)(erroff + opt + 1 - opthead));
1628 return -1;
1629 }
1630 optlen = IP6OPT_RTALERT_LEN;
1631 bcopy(src: (caddr_t)(opt + 2), dst: (caddr_t)&rtalert_val, n: 2);
1632 *rtalertp = ntohs(rtalert_val);
1633 break;
1634 case IP6OPT_JUMBO:
1635 /* XXX may need check for alignment */
1636 if (hbhlen < IP6OPT_JUMBO_LEN) {
1637 ip6stat.ip6s_toosmall++;
1638 goto bad;
1639 }
1640 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1641 /* XXX stat */
1642 icmp6_error(m, ICMP6_PARAM_PROB,
1643 ICMP6_PARAMPROB_HEADER,
1644 (int)(erroff + opt + 1 - opthead));
1645 return -1;
1646 }
1647 optlen = IP6OPT_JUMBO_LEN;
1648
1649 /*
1650 * IPv6 packets that have non 0 payload length
1651 * must not contain a jumbo payload option.
1652 */
1653 ip6 = mtod(m, struct ip6_hdr *);
1654 if (ip6->ip6_plen) {
1655 ip6stat.ip6s_badoptions++;
1656 icmp6_error(m, ICMP6_PARAM_PROB,
1657 ICMP6_PARAMPROB_HEADER,
1658 (int)(erroff + opt - opthead));
1659 return -1;
1660 }
1661
1662 /*
1663 * We may see jumbolen in unaligned location, so
1664 * we'd need to perform bcopy().
1665 */
1666 bcopy(src: opt + 2, dst: &jumboplen, n: sizeof(jumboplen));
1667 jumboplen = (u_int32_t)htonl(jumboplen);
1668
1669#if 1
1670 /*
1671 * if there are multiple jumbo payload options,
1672 * *plenp will be non-zero and the packet will be
1673 * rejected.
1674 * the behavior may need some debate in ipngwg -
1675 * multiple options does not make sense, however,
1676 * there's no explicit mention in specification.
1677 */
1678 if (*plenp != 0) {
1679 ip6stat.ip6s_badoptions++;
1680 icmp6_error(m, ICMP6_PARAM_PROB,
1681 ICMP6_PARAMPROB_HEADER,
1682 (int)(erroff + opt + 2 - opthead));
1683 return -1;
1684 }
1685#endif
1686
1687 /*
1688 * jumbo payload length must be larger than 65535.
1689 */
1690 if (jumboplen <= IPV6_MAXPACKET) {
1691 ip6stat.ip6s_badoptions++;
1692 icmp6_error(m, ICMP6_PARAM_PROB,
1693 ICMP6_PARAMPROB_HEADER,
1694 (int)(erroff + opt + 2 - opthead));
1695 return -1;
1696 }
1697 *plenp = jumboplen;
1698
1699 break;
1700 default: /* unknown option */
1701 if (hbhlen < IP6OPT_MINLEN) {
1702 ip6stat.ip6s_toosmall++;
1703 goto bad;
1704 }
1705 optlen = ip6_unknown_opt(opt, m,
1706 erroff + opt - opthead);
1707 if (optlen == -1) {
1708 return -1;
1709 }
1710 optlen += 2;
1711 break;
1712 }
1713 }
1714
1715 return 0;
1716
1717bad:
1718 m_freem(m);
1719 return -1;
1720}
1721
1722/*
1723 * Unknown option processing.
1724 * The third argument `off' is the offset from the IPv6 header to the option,
1725 * which is necessary if the IPv6 header the and option header and IPv6 header
1726 * is not continuous in order to return an ICMPv6 error.
1727 */
1728int
1729ip6_unknown_opt(uint8_t *optp, struct mbuf *m, size_t off)
1730{
1731 struct ip6_hdr *ip6;
1732
1733 switch (IP6OPT_TYPE(*optp)) {
1734 case IP6OPT_TYPE_SKIP: /* ignore the option */
1735 return (int)*(optp + 1);
1736
1737 case IP6OPT_TYPE_DISCARD: /* silently discard */
1738 m_freem(m);
1739 return -1;
1740
1741 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1742 ip6stat.ip6s_badoptions++;
1743 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, (int)off);
1744 return -1;
1745
1746 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1747 ip6stat.ip6s_badoptions++;
1748 ip6 = mtod(m, struct ip6_hdr *);
1749 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1750 (m->m_flags & (M_BCAST | M_MCAST))) {
1751 m_freem(m);
1752 } else {
1753 icmp6_error(m, ICMP6_PARAM_PROB,
1754 ICMP6_PARAMPROB_OPTION, (int)off);
1755 }
1756 return -1;
1757 }
1758
1759 m_freem(m); /* XXX: NOTREACHED */
1760 return -1;
1761}
1762
1763/*
1764 * Create the "control" list for this pcb.
1765 * These functions will not modify mbuf chain at all.
1766 *
1767 * With KAME mbuf chain restriction:
1768 * The routine will be called from upper layer handlers like tcp6_input().
1769 * Thus the routine assumes that the caller (tcp6_input) have already
1770 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1771 * very first mbuf on the mbuf chain.
1772 *
1773 * ip6_savecontrol_v4 will handle those options that are possible to be
1774 * set on a v4-mapped socket.
1775 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1776 * options and handle the v6-only ones itself.
1777 */
1778struct mbuf **
1779ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1780 int *v4only)
1781{
1782 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1783
1784 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1785 struct timeval tv;
1786
1787 getmicrotime(&tv);
1788 mp = sbcreatecontrol_mbuf(p: (caddr_t)&tv, size: sizeof(tv),
1789 SCM_TIMESTAMP, SOL_SOCKET, m: mp);
1790 if (*mp == NULL) {
1791 return NULL;
1792 }
1793 }
1794 if ((inp->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
1795 uint64_t time;
1796
1797 time = mach_absolute_time();
1798 mp = sbcreatecontrol_mbuf(p: (caddr_t)&time, size: sizeof(time),
1799 SCM_TIMESTAMP_MONOTONIC, SOL_SOCKET, m: mp);
1800 if (*mp == NULL) {
1801 return NULL;
1802 }
1803 }
1804 if ((inp->inp_socket->so_options & SO_TIMESTAMP_CONTINUOUS) != 0) {
1805 uint64_t time;
1806
1807 time = mach_continuous_time();
1808 mp = sbcreatecontrol_mbuf(p: (caddr_t)&time, size: sizeof(time),
1809 SCM_TIMESTAMP_CONTINUOUS, SOL_SOCKET, m: mp);
1810 if (*mp == NULL) {
1811 return NULL;
1812 }
1813 }
1814 if ((inp->inp_socket->so_flags & SOF_RECV_TRAFFIC_CLASS) != 0) {
1815 int tc = m_get_traffic_class(m);
1816
1817 mp = sbcreatecontrol_mbuf(p: (caddr_t)&tc, size: sizeof(tc),
1818 SO_TRAFFIC_CLASS, SOL_SOCKET, m: mp);
1819 if (*mp == NULL) {
1820 return NULL;
1821 }
1822 }
1823
1824 if ((inp->inp_socket->so_flags & SOF_RECV_WAKE_PKT) &&
1825 (m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT)) {
1826 int flag = 1;
1827
1828 mp = sbcreatecontrol_mbuf(p: (caddr_t)&flag, size: sizeof(flag),
1829 SO_RECV_WAKE_PKT, SOL_SOCKET, m: mp);
1830 if (*mp == NULL) {
1831 return NULL;
1832 }
1833 }
1834
1835#define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1836 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1837 if (v4only != NULL) {
1838 *v4only = 1;
1839 }
1840
1841 // Send ECN flags for v4-mapped addresses
1842 if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1843 struct ip *ip_header = mtod(m, struct ip *);
1844
1845 int tclass = (int)(ip_header->ip_tos);
1846 mp = sbcreatecontrol_mbuf(p: (caddr_t)&tclass, size: sizeof(tclass),
1847 IPV6_TCLASS, IPPROTO_IPV6, m: mp);
1848 if (*mp == NULL) {
1849 return NULL;
1850 }
1851 }
1852
1853 // Send IN6P_PKTINFO for v4-mapped address
1854 if ((inp->inp_flags & IN6P_PKTINFO) != 0 || SOFLOW_ENABLED(inp->inp_socket)) {
1855 struct in6_pktinfo pi6 = {
1856 .ipi6_addr = IN6ADDR_V4MAPPED_INIT,
1857 .ipi6_ifindex = (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0,
1858 };
1859
1860 struct ip *ip_header = mtod(m, struct ip *);
1861 bcopy(src: &ip_header->ip_dst, dst: &pi6.ipi6_addr.s6_addr32[3], n: sizeof(struct in_addr));
1862
1863 mp = sbcreatecontrol_mbuf(p: (caddr_t)&pi6,
1864 size: sizeof(struct in6_pktinfo),
1865 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO),
1866 IPPROTO_IPV6, m: mp);
1867 if (*mp == NULL) {
1868 return NULL;
1869 }
1870 }
1871 return mp;
1872 }
1873
1874 /* RFC 2292 sec. 5 */
1875 if ((inp->inp_flags & IN6P_PKTINFO) != 0 || SOFLOW_ENABLED(inp->inp_socket)) {
1876 struct in6_pktinfo pi6;
1877
1878 bcopy(src: &ip6->ip6_dst, dst: &pi6.ipi6_addr, n: sizeof(struct in6_addr));
1879 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1880 pi6.ipi6_ifindex =
1881 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1882
1883 mp = sbcreatecontrol_mbuf(p: (caddr_t)&pi6,
1884 size: sizeof(struct in6_pktinfo),
1885 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO),
1886 IPPROTO_IPV6, m: mp);
1887 if (*mp == NULL) {
1888 return NULL;
1889 }
1890 }
1891
1892 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1893 int hlim = ip6->ip6_hlim & 0xff;
1894
1895 mp = sbcreatecontrol_mbuf(p: (caddr_t)&hlim, size: sizeof(int),
1896 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1897 IPPROTO_IPV6, m: mp);
1898 if (*mp == NULL) {
1899 return NULL;
1900 }
1901 }
1902
1903 if (v4only != NULL) {
1904 *v4only = 0;
1905 }
1906 return mp;
1907}
1908
1909int
1910ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1911{
1912 struct mbuf **np;
1913 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1914 int v4only = 0;
1915
1916 *mp = NULL;
1917 np = ip6_savecontrol_v4(inp: in6p, m, mp, v4only: &v4only);
1918 if (np == NULL) {
1919 goto no_mbufs;
1920 }
1921
1922 mp = np;
1923 if (v4only) {
1924 return 0;
1925 }
1926
1927 if ((in6p->inp_flags & IN6P_TCLASS) != 0) {
1928 u_int32_t flowinfo;
1929 int tclass;
1930
1931 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1932 flowinfo >>= 20;
1933
1934 tclass = flowinfo & 0xff;
1935 mp = sbcreatecontrol_mbuf(p: (caddr_t)&tclass, size: sizeof(tclass),
1936 IPV6_TCLASS, IPPROTO_IPV6, m: mp);
1937 if (*mp == NULL) {
1938 goto no_mbufs;
1939 }
1940 }
1941
1942 /*
1943 * IPV6_HOPOPTS socket option. Recall that we required super-user
1944 * privilege for the option (see ip6_ctloutput), but it might be too
1945 * strict, since there might be some hop-by-hop options which can be
1946 * returned to normal user.
1947 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1948 */
1949 if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
1950 /*
1951 * Check if a hop-by-hop options header is contatined in the
1952 * received packet, and if so, store the options as ancillary
1953 * data. Note that a hop-by-hop options header must be
1954 * just after the IPv6 header, which is assured through the
1955 * IPv6 input processing.
1956 */
1957 ip6 = mtod(m, struct ip6_hdr *);
1958 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1959 struct ip6_hbh *hbh;
1960 int hbhlen = 0;
1961 hbh = (struct ip6_hbh *)(ip6 + 1);
1962 hbhlen = (hbh->ip6h_len + 1) << 3;
1963
1964 /*
1965 * XXX: We copy the whole header even if a
1966 * jumbo payload option is included, the option which
1967 * is to be removed before returning according to
1968 * RFC2292.
1969 * Note: this constraint is removed in RFC3542
1970 */
1971 mp = sbcreatecontrol_mbuf(p: (caddr_t)hbh, size: hbhlen,
1972 IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1973 IPPROTO_IPV6, m: mp);
1974
1975 if (*mp == NULL) {
1976 goto no_mbufs;
1977 }
1978 }
1979 }
1980
1981 if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1982 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1983
1984 /*
1985 * Search for destination options headers or routing
1986 * header(s) through the header chain, and stores each
1987 * header as ancillary data.
1988 * Note that the order of the headers remains in
1989 * the chain of ancillary data.
1990 */
1991 while (1) { /* is explicit loop prevention necessary? */
1992 struct ip6_ext *ip6e = NULL;
1993 int elen;
1994
1995 /*
1996 * if it is not an extension header, don't try to
1997 * pull it from the chain.
1998 */
1999 switch (nxt) {
2000 case IPPROTO_DSTOPTS:
2001 case IPPROTO_ROUTING:
2002 case IPPROTO_HOPOPTS:
2003 case IPPROTO_AH: /* is it possible? */
2004 break;
2005 default:
2006 goto loopend;
2007 }
2008
2009 if (off + sizeof(*ip6e) > m->m_len) {
2010 goto loopend;
2011 }
2012 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
2013 if (nxt == IPPROTO_AH) {
2014 elen = (ip6e->ip6e_len + 2) << 2;
2015 } else {
2016 elen = (ip6e->ip6e_len + 1) << 3;
2017 }
2018 if (off + elen > m->m_len) {
2019 goto loopend;
2020 }
2021
2022 switch (nxt) {
2023 case IPPROTO_DSTOPTS:
2024 if (!(in6p->inp_flags & IN6P_DSTOPTS)) {
2025 break;
2026 }
2027
2028 mp = sbcreatecontrol_mbuf(p: (caddr_t)ip6e, size: elen,
2029 IS2292(in6p, IPV6_2292DSTOPTS,
2030 IPV6_DSTOPTS), IPPROTO_IPV6, m: mp);
2031 if (*mp == NULL) {
2032 goto no_mbufs;
2033 }
2034 break;
2035 case IPPROTO_ROUTING:
2036 if (!(in6p->inp_flags & IN6P_RTHDR)) {
2037 break;
2038 }
2039
2040 mp = sbcreatecontrol_mbuf(p: (caddr_t)ip6e, size: elen,
2041 IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
2042 IPPROTO_IPV6, m: mp);
2043 if (*mp == NULL) {
2044 goto no_mbufs;
2045 }
2046 break;
2047 case IPPROTO_HOPOPTS:
2048 case IPPROTO_AH: /* is it possible? */
2049 break;
2050
2051 default:
2052 /*
2053 * other cases have been filtered in the above.
2054 * none will visit this case. here we supply
2055 * the code just in case (nxt overwritten or
2056 * other cases).
2057 */
2058 goto loopend;
2059 }
2060
2061 /* proceed with the next header. */
2062 off += elen;
2063 nxt = ip6e->ip6e_nxt;
2064 ip6e = NULL;
2065 }
2066loopend:
2067 ;
2068 }
2069 return 0;
2070no_mbufs:
2071 ip6stat.ip6s_pktdropcntrl++;
2072 /* XXX increment a stat to show the failure */
2073 return ENOBUFS;
2074}
2075#undef IS2292
2076
2077void
2078ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu)
2079{
2080 struct socket *so;
2081 struct mbuf *m_mtu;
2082 struct ip6_mtuinfo mtuctl;
2083
2084 so = in6p->inp_socket;
2085
2086 if ((in6p->inp_flags & IN6P_MTU) == 0) {
2087 return;
2088 }
2089
2090 if (mtu == NULL) {
2091 return;
2092 }
2093
2094 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && SOCK_CHECK_PROTO(so, IPPROTO_TCP)) {
2095 return;
2096 }
2097
2098 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
2099 !in6_are_addr_equal_scoped(&in6p->in6p_faddr, &dst->sin6_addr, in6p->inp_fifscope, dst->sin6_scope_id)) {
2100 return;
2101 }
2102
2103 bzero(s: &mtuctl, n: sizeof(mtuctl)); /* zero-clear for safety */
2104 mtuctl.ip6m_mtu = *mtu;
2105 mtuctl.ip6m_addr = *dst;
2106 if (!in6_embedded_scope) {
2107 mtuctl.ip6m_addr.sin6_scope_id = dst->sin6_scope_id;
2108 }
2109 if (sa6_recoverscope(&mtuctl.ip6m_addr, TRUE)) {
2110 return;
2111 }
2112
2113 if ((m_mtu = sbcreatecontrol(p: (caddr_t)&mtuctl, size: sizeof(mtuctl),
2114 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) {
2115 return;
2116 }
2117
2118 if (sbappendaddr(sb: &so->so_rcv, SA(dst), NULL, control: m_mtu, NULL) == 0) {
2119 return;
2120 }
2121 sorwakeup(so);
2122}
2123
2124/*
2125 * Get pointer to the previous header followed by the header
2126 * currently processed.
2127 * XXX: This function supposes that
2128 * M includes all headers,
2129 * the next header field and the header length field of each header
2130 * are valid, and
2131 * the sum of each header length equals to OFF.
2132 * Because of these assumptions, this function must be called very
2133 * carefully. Moreover, it will not be used in the near future when
2134 * we develop `neater' mechanism to process extension headers.
2135 */
2136char *
2137ip6_get_prevhdr(struct mbuf *m, int off)
2138{
2139 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2140
2141 if (off == sizeof(struct ip6_hdr)) {
2142 return (char *)&ip6->ip6_nxt;
2143 } else {
2144 int len, nxt;
2145 struct ip6_ext *ip6e = NULL;
2146
2147 nxt = ip6->ip6_nxt;
2148 len = sizeof(struct ip6_hdr);
2149 while (len < off) {
2150 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
2151
2152 switch (nxt) {
2153 case IPPROTO_FRAGMENT:
2154 len += sizeof(struct ip6_frag);
2155 break;
2156 case IPPROTO_AH:
2157 len += (ip6e->ip6e_len + 2) << 2;
2158 break;
2159 default:
2160 len += (ip6e->ip6e_len + 1) << 3;
2161 break;
2162 }
2163 nxt = ip6e->ip6e_nxt;
2164 }
2165 if (ip6e) {
2166 return (char *)&ip6e->ip6e_nxt;
2167 } else {
2168 return NULL;
2169 }
2170 }
2171}
2172
2173/*
2174 * get next header offset. m will be retained.
2175 */
2176int
2177ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
2178{
2179 struct ip6_hdr ip6;
2180 struct ip6_ext ip6e;
2181 struct ip6_frag fh;
2182
2183 /* just in case */
2184 VERIFY(m != NULL);
2185 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) {
2186 return -1;
2187 }
2188
2189 switch (proto) {
2190 case IPPROTO_IPV6:
2191 if (m->m_pkthdr.len < off + sizeof(ip6)) {
2192 return -1;
2193 }
2194 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
2195 if (nxtp) {
2196 *nxtp = ip6.ip6_nxt;
2197 }
2198 off += sizeof(ip6);
2199 return off;
2200
2201 case IPPROTO_FRAGMENT:
2202 /*
2203 * terminate parsing if it is not the first fragment,
2204 * it does not make sense to parse through it.
2205 */
2206 if (m->m_pkthdr.len < off + sizeof(fh)) {
2207 return -1;
2208 }
2209 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
2210 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
2211 if (fh.ip6f_offlg & IP6F_OFF_MASK) {
2212 return -1;
2213 }
2214 if (nxtp) {
2215 *nxtp = fh.ip6f_nxt;
2216 }
2217 off += sizeof(struct ip6_frag);
2218 return off;
2219
2220 case IPPROTO_AH:
2221 if (m->m_pkthdr.len < off + sizeof(ip6e)) {
2222 return -1;
2223 }
2224 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
2225 if (nxtp) {
2226 *nxtp = ip6e.ip6e_nxt;
2227 }
2228 off += (ip6e.ip6e_len + 2) << 2;
2229 return off;
2230
2231 case IPPROTO_HOPOPTS:
2232 case IPPROTO_ROUTING:
2233 case IPPROTO_DSTOPTS:
2234 if (m->m_pkthdr.len < off + sizeof(ip6e)) {
2235 return -1;
2236 }
2237 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
2238 if (nxtp) {
2239 *nxtp = ip6e.ip6e_nxt;
2240 }
2241 off += (ip6e.ip6e_len + 1) << 3;
2242 return off;
2243
2244 case IPPROTO_NONE:
2245 case IPPROTO_ESP:
2246 case IPPROTO_IPCOMP:
2247 /* give up */
2248 return -1;
2249
2250 default:
2251 return -1;
2252 }
2253}
2254
2255/*
2256 * get offset for the last header in the chain. m will be kept untainted.
2257 */
2258int
2259ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
2260{
2261 int newoff;
2262 int nxt;
2263
2264 if (!nxtp) {
2265 nxt = -1;
2266 nxtp = &nxt;
2267 }
2268 while (1) {
2269 newoff = ip6_nexthdr(m, off, proto, nxtp);
2270 if (newoff < 0) {
2271 return off;
2272 } else if (newoff < off) {
2273 return -1; /* invalid */
2274 } else if (newoff == off) {
2275 return newoff;
2276 }
2277
2278 off = newoff;
2279 proto = *nxtp;
2280 }
2281}
2282
2283boolean_t
2284ip6_pkt_has_ulp(struct mbuf *m)
2285{
2286 int off = 0, nxt = IPPROTO_NONE;
2287
2288 off = ip6_lasthdr(m, off: 0, IPPROTO_IPV6, nxtp: &nxt);
2289 if (off < 0 || m->m_pkthdr.len < off) {
2290 return FALSE;
2291 }
2292
2293 switch (nxt) {
2294 case IPPROTO_TCP:
2295 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) {
2296 return FALSE;
2297 }
2298 break;
2299 case IPPROTO_UDP:
2300 if (off + sizeof(struct udphdr) > m->m_pkthdr.len) {
2301 return FALSE;
2302 }
2303 break;
2304 case IPPROTO_ICMPV6:
2305 if (off + sizeof(uint32_t) > m->m_pkthdr.len) {
2306 return FALSE;
2307 }
2308 break;
2309 case IPPROTO_NONE:
2310 return TRUE;
2311 case IPPROTO_ESP:
2312 return TRUE;
2313 case IPPROTO_IPCOMP:
2314 return TRUE;
2315 default:
2316 return FALSE;
2317 }
2318 return TRUE;
2319}
2320
2321struct ip6aux *
2322ip6_addaux(struct mbuf *m)
2323{
2324 struct m_tag *tag;
2325
2326 /* Check if one is already allocated */
2327 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
2328 KERNEL_TAG_TYPE_INET6);
2329 if (tag == NULL) {
2330 /* Allocate a tag */
2331 tag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_INET6,
2332 sizeof(struct ip6aux), M_DONTWAIT, m);
2333
2334 /* Attach it to the mbuf */
2335 if (tag) {
2336 m_tag_prepend(m, tag);
2337 }
2338 }
2339
2340 return tag ? (struct ip6aux *)(tag->m_tag_data) : NULL;
2341}
2342
2343struct ip6aux *
2344ip6_findaux(struct mbuf *m)
2345{
2346 struct m_tag *tag;
2347
2348 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
2349 KERNEL_TAG_TYPE_INET6);
2350
2351 return tag != NULL ? (struct ip6aux *)(tag->m_tag_data) : NULL;
2352}
2353
2354void
2355ip6_delaux(struct mbuf *m)
2356{
2357 struct m_tag *tag;
2358
2359 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
2360 KERNEL_TAG_TYPE_INET6);
2361 if (tag != NULL) {
2362 m_tag_delete(m, tag);
2363 }
2364}
2365
2366struct inet6_tag_container {
2367 struct m_tag inet6_m_tag;
2368 struct ip6aux inet6_ip6a;
2369};
2370
2371struct m_tag *
2372m_tag_kalloc_inet6(u_int32_t id, u_int16_t type, uint16_t len, int wait)
2373{
2374 struct inet6_tag_container *tag_container;
2375 struct m_tag *tag = NULL;
2376
2377 assert3u(id, ==, KERNEL_MODULE_TAG_ID);
2378 assert3u(type, ==, KERNEL_TAG_TYPE_INET6);
2379 assert3u(len, ==, sizeof(struct ip6aux));
2380
2381 if (len != sizeof(struct ip6aux)) {
2382 return NULL;
2383 }
2384
2385 tag_container = kalloc_type(struct inet6_tag_container, wait | M_ZERO);
2386 if (tag_container != NULL) {
2387 tag = &tag_container->inet6_m_tag;
2388
2389 assert3p(tag, ==, tag_container);
2390
2391 M_TAG_INIT(tag, id, type, len, &tag_container->inet6_ip6a, NULL);
2392 }
2393
2394 return tag;
2395}
2396
2397void
2398m_tag_kfree_inet6(struct m_tag *tag)
2399{
2400 struct inet6_tag_container *tag_container = (struct inet6_tag_container *)tag;
2401
2402 assert3u(tag->m_tag_len, ==, sizeof(struct ip6aux));
2403
2404 kfree_type(struct inet6_tag_container, tag_container);
2405}
2406
2407void
2408ip6_register_m_tag(void)
2409{
2410 int error;
2411
2412 error = m_register_internal_tag_type(type: KERNEL_TAG_TYPE_INET6, len: sizeof(struct ip6aux),
2413 alloc_func: m_tag_kalloc_inet6, free_func: m_tag_kfree_inet6);
2414
2415 assert3u(error, ==, 0);
2416}
2417
2418/*
2419 * Drain callback
2420 */
2421void
2422ip6_drain(void)
2423{
2424 frag6_drain(); /* fragments */
2425 in6_rtqdrain(); /* protocol cloned routes */
2426 nd6_drain(NULL); /* cloned routes: ND6 */
2427}
2428
2429/*
2430 * System control for IP6
2431 */
2432
2433u_char inet6ctlerrmap[PRC_NCMDS] = {
2434 0, 0, 0, 0,
2435 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
2436 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
2437 EMSGSIZE, EHOSTUNREACH, 0, 0,
2438 0, 0, 0, 0,
2439 ENOPROTOOPT, ECONNREFUSED
2440};
2441
2442static int
2443sysctl_reset_ip6_input_stats SYSCTL_HANDLER_ARGS
2444{
2445#pragma unused(arg1, arg2)
2446 int error, i;
2447
2448 i = ip6_input_measure;
2449 error = sysctl_handle_int(oidp, arg1: &i, arg2: 0, req);
2450 if (error || req->newptr == USER_ADDR_NULL) {
2451 goto done;
2452 }
2453 /* impose bounds */
2454 if (i < 0 || i > 1) {
2455 error = EINVAL;
2456 goto done;
2457 }
2458 if (ip6_input_measure != i && i == 1) {
2459 net_perf_initialize(npp: &net_perf, bins: ip6_input_measure_bins);
2460 }
2461 ip6_input_measure = i;
2462done:
2463 return error;
2464}
2465
2466static int
2467sysctl_ip6_input_measure_bins SYSCTL_HANDLER_ARGS
2468{
2469#pragma unused(arg1, arg2)
2470 int error;
2471 uint64_t i;
2472
2473 i = ip6_input_measure_bins;
2474 error = sysctl_handle_quad(oidp, arg1: &i, arg2: 0, req);
2475 if (error || req->newptr == USER_ADDR_NULL) {
2476 goto done;
2477 }
2478 /* validate data */
2479 if (!net_perf_validate_bins(bins: i)) {
2480 error = EINVAL;
2481 goto done;
2482 }
2483 ip6_input_measure_bins = i;
2484done:
2485 return error;
2486}
2487
2488static int
2489sysctl_ip6_input_getperf SYSCTL_HANDLER_ARGS
2490{
2491#pragma unused(oidp, arg1, arg2)
2492 if (req->oldptr == USER_ADDR_NULL) {
2493 req->oldlen = (size_t)sizeof(struct net_perf);
2494 }
2495
2496 return SYSCTL_OUT(req, &net_perf, MIN(sizeof(net_perf), req->oldlen));
2497}
2498
2499
2500/*
2501 * Initialize IPv6 source address hash table.
2502 */
2503static void
2504in6_ifaddrhashtbl_init(void)
2505{
2506 int i, k, p;
2507
2508 if (in6_ifaddrhashtbl != NULL) {
2509 return;
2510 }
2511
2512 PE_parse_boot_argn(arg_string: "ina6ddr_nhash", arg_ptr: &in6addr_nhash,
2513 max_arg: sizeof(in6addr_nhash));
2514 if (in6addr_nhash == 0) {
2515 in6addr_nhash = IN6ADDR_NHASH;
2516 }
2517
2518 in6_ifaddrhashtbl = zalloc_permanent(
2519 in6addr_nhash * sizeof(*in6_ifaddrhashtbl),
2520 ZALIGN_PTR);
2521
2522 /*
2523 * Generate the next largest prime greater than in6addr_nhash.
2524 */
2525 k = (in6addr_nhash % 2 == 0) ? in6addr_nhash + 1 : in6addr_nhash + 2;
2526 for (;;) {
2527 p = 1;
2528 for (i = 3; i * i <= k; i += 2) {
2529 if (k % i == 0) {
2530 p = 0;
2531 }
2532 }
2533 if (p == 1) {
2534 break;
2535 }
2536 k += 2;
2537 }
2538 in6addr_hashp = k;
2539}
2540
2541static int
2542sysctl_ip6_checkinterface SYSCTL_HANDLER_ARGS
2543{
2544#pragma unused(arg1, arg2)
2545 int error, i;
2546
2547 i = ip6_checkinterface;
2548 error = sysctl_handle_int(oidp, arg1: &i, arg2: 0, req);
2549 if (error || req->newptr == USER_ADDR_NULL) {
2550 return error;
2551 }
2552
2553 switch (i) {
2554 case IP6_CHECKINTERFACE_WEAK_ES:
2555 case IP6_CHECKINTERFACE_HYBRID_ES:
2556 case IP6_CHECKINTERFACE_STRONG_ES:
2557 if (ip6_checkinterface != i) {
2558 ip6_checkinterface = i;
2559 os_log(OS_LOG_DEFAULT, "%s: ip6_checkinterface is now %d\n",
2560 __func__, ip6_checkinterface);
2561 }
2562 break;
2563 default:
2564 error = EINVAL;
2565 break;
2566 }
2567 return error;
2568}
2569