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