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