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