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