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