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