2 * Copyright (c) 2003-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
59 * Copyright (c) 1982, 1986, 1988, 1993
60 * The Regents of the University of California. All rights reserved.
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
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.
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
90 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/malloc.h>
97 #include <sys/domain.h>
98 #include <sys/protosw.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/errno.h>
102 #include <sys/time.h>
103 #include <sys/kernel.h>
104 #include <sys/syslog.h>
105 #include <sys/sysctl.h>
106 #include <sys/proc.h>
107 #include <sys/kauth.h>
108 #include <sys/mcache.h>
110 #include <mach/mach_time.h>
111 #include <mach/sdt.h>
112 #include <pexpert/pexpert.h>
113 #include <dev/random/randomdev.h>
116 #include <net/if_var.h>
117 #include <net/if_types.h>
118 #include <net/if_dl.h>
119 #include <net/route.h>
120 #include <net/kpi_protocol.h>
121 #include <net/ntstat.h>
122 #include <net/init.h>
123 #include <net/net_osdep.h>
124 #include <net/net_perf.h>
126 #include <netinet/in.h>
127 #include <netinet/in_systm.h>
129 #include <netinet/ip.h>
130 #include <netinet/ip_icmp.h>
132 #include <netinet/kpi_ipfilter_var.h>
133 #include <netinet/ip6.h>
134 #include <netinet6/in6_var.h>
135 #include <netinet6/ip6_var.h>
136 #include <netinet/in_pcb.h>
137 #include <netinet/icmp6.h>
138 #include <netinet6/in6_ifattach.h>
139 #include <netinet6/nd6.h>
140 #include <netinet6/scope6_var.h>
141 #include <netinet6/ip6protosw.h>
144 #include <netinet6/ipsec.h>
145 #include <netinet6/ipsec6.h>
146 extern int ipsec_bypass
;
150 #include <netinet/ip_fw.h>
151 #include <netinet/ip_dummynet.h>
152 #endif /* DUMMYNET */
154 /* we need it for NLOOP. */
158 #include <net/pfvar.h>
161 struct ip6protosw
*ip6_protox
[IPPROTO_MAX
];
163 static lck_grp_attr_t
*in6_ifaddr_rwlock_grp_attr
;
164 static lck_grp_t
*in6_ifaddr_rwlock_grp
;
165 static lck_attr_t
*in6_ifaddr_rwlock_attr
;
166 decl_lck_rw_data(, in6_ifaddr_rwlock
);
168 /* Protected by in6_ifaddr_rwlock */
169 struct in6_ifaddr
*in6_ifaddrs
= NULL
;
171 #define IN6_IFSTAT_REQUIRE_ALIGNED_64(f) \
172 _CASSERT(!(offsetof(struct in6_ifstat, f) % sizeof (uint64_t)))
174 #define ICMP6_IFSTAT_REQUIRE_ALIGNED_64(f) \
175 _CASSERT(!(offsetof(struct icmp6_ifstat, f) % sizeof (uint64_t)))
177 struct ip6stat ip6stat
;
179 decl_lck_mtx_data(, proxy6_lock
);
180 decl_lck_mtx_data(static, dad6_mutex_data
);
181 decl_lck_mtx_data(static, nd6_mutex_data
);
182 decl_lck_mtx_data(static, prefix6_mutex_data
);
183 lck_mtx_t
*dad6_mutex
= &dad6_mutex_data
;
184 lck_mtx_t
*nd6_mutex
= &nd6_mutex_data
;
185 lck_mtx_t
*prefix6_mutex
= &prefix6_mutex_data
;
186 #ifdef ENABLE_ADDRSEL
187 decl_lck_mtx_data(static, addrsel_mutex_data
);
188 lck_mtx_t
*addrsel_mutex
= &addrsel_mutex_data
;
190 static lck_attr_t
*ip6_mutex_attr
;
191 static lck_grp_t
*ip6_mutex_grp
;
192 static lck_grp_attr_t
*ip6_mutex_grp_attr
;
194 extern int loopattach_done
;
195 extern void addrsel_policy_init(void);
197 static int sysctl_reset_ip6_input_stats SYSCTL_HANDLER_ARGS
;
198 static int sysctl_ip6_input_measure_bins SYSCTL_HANDLER_ARGS
;
199 static int sysctl_ip6_input_getperf SYSCTL_HANDLER_ARGS
;
200 static void ip6_init_delayed(void);
201 static int ip6_hopopts_input(u_int32_t
*, u_int32_t
*, struct mbuf
**, int *);
204 extern void stfattach(void);
207 SYSCTL_DECL(_net_inet6_ip6
);
209 static uint32_t ip6_adj_clear_hwcksum
= 0;
210 SYSCTL_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");
214 static uint32_t ip6_adj_partial_sum
= 1;
215 SYSCTL_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");
219 static int ip6_input_measure
= 0;
220 SYSCTL_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");
224 static uint64_t ip6_input_measure_bins
= 0;
225 SYSCTL_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");
230 static net_perf_t net_perf
;
231 SYSCTL_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)");
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.
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.
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))) { \
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); \
260 ip6stat.ip6s_toosmall++; \
265 VERIFY(_n != (_m)); \
270 #endif /* !__i386__ && !__x86_64__ */
273 ip6_proto_input(protocol_family_t protocol
, mbuf_t packet
)
275 #pragma unused(protocol)
277 struct timeval start_tv
;
278 if (ip6_input_measure
)
279 net_perf_start_time(&net_perf
, &start_tv
);
283 if (ip6_input_measure
) {
284 net_perf_measure_time(&net_perf
, &start_tv
, 1);
285 net_perf_histogram(&net_perf
, 1);
291 * IP6 initialization: fill in IP6 protocol switch table.
292 * All protocols not implemented in kernel go to raw IP6 protocol handler.
295 ip6_init(struct ip6protosw
*pp
, struct domain
*dp
)
297 static int ip6_initialized
= 0;
301 domain_unguard_t unguard
;
303 domain_proto_mtx_lock_assert_held();
304 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
306 _CASSERT((sizeof (struct ip6_hdr
) +
307 sizeof (struct icmp6_hdr
)) <= _MHLEN
);
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
);
318 for (i
= 0; i
< IN6_EVENT_MAX
; i
++)
319 VERIFY(in6_event2kev_array
[i
].in6_event_code
== i
);
321 pr
= pffindproto_locked(PF_INET6
, IPPROTO_RAW
, SOCK_RAW
);
323 panic("%s: Unable to find [PF_INET6,IPPROTO_RAW,SOCK_RAW]\n",
328 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
329 for (i
= 0; i
< IPPROTO_MAX
; i
++)
330 ip6_protox
[i
] = (struct ip6protosw
*)pr
;
332 * Cycle through IP protocols and put them into the appropriate place
333 * in ip6_protox[], skipping protocols IPPROTO_{IP,RAW}.
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
;
346 ip6_mutex_grp_attr
= lck_grp_attr_alloc_init();
348 ip6_mutex_grp
= lck_grp_alloc_init("ip6", ip6_mutex_grp_attr
);
349 ip6_mutex_attr
= lck_attr_alloc_init();
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
);
354 scope6_init(ip6_mutex_grp
, ip6_mutex_attr
);
356 #ifdef ENABLE_ADDRSEL
357 lck_mtx_init(addrsel_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
360 lck_mtx_init(&proxy6_lock
, ip6_mutex_grp
, ip6_mutex_attr
);
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
);
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
);
388 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mcast
);
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
);
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
);
428 (RandomULong() ^ tv
.tv_usec
) % MAX_TEMP_DESYNC_FACTOR
;
434 icmp6_init(NULL
, dp
);
435 addrsel_policy_init();
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.
442 net_init_add(ip6_init_delayed
);
444 unguard
= domain_unguard_deploy();
445 i
= proto_register_input(PF_INET6
, ip6_proto_input
, NULL
, 0);
447 panic("%s: failed to register PF_INET6 protocol: %d\n",
451 domain_unguard_release(unguard
);
455 ip6_init_delayed(void)
457 (void) in6_ifattach_prelim(lo_ifp
);
459 /* timer for regeneranation of temporary addresses randomize ID */
460 timeout(in6_tmpaddrtimer
, NULL
,
461 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
462 ip6_temp_regen_advance
) * hz
);
470 ip6_input_adjust(struct mbuf
*m
, struct ip6_hdr
*ip6
, uint32_t plen
,
473 boolean_t adjust
= TRUE
;
474 uint32_t tot_len
= sizeof (*ip6
) + plen
;
476 ASSERT(m_pktlen(m
) > tot_len
);
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.
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
++;
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.)
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
)) {
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.
520 if (ip6
->ip6_nxt
== IPPROTO_TCP
||
521 ip6
->ip6_nxt
== IPPROTO_UDP
) {
524 m
->m_pkthdr
.csum_flags
&= ~CSUM_DATA_VALID
;
525 m
->m_pkthdr
.csum_data
= 0;
526 ip6stat
.ip6s_adj_hwcsum_clr
++;
532 if (m
->m_len
== m
->m_pkthdr
.len
) {
534 m
->m_pkthdr
.len
= tot_len
;
536 m_adj(m
, tot_len
- m
->m_pkthdr
.len
);
542 ip6_input(struct mbuf
*m
)
545 int off
= sizeof (struct ip6_hdr
), nest
;
547 u_int32_t rtalert
= ~0;
548 int nxt
= 0, ours
= 0;
549 struct ifnet
*inifp
, *deliverifp
= NULL
;
550 ipfilter_t inject_ipfref
= NULL
;
552 struct in6_ifaddr
*ia6
= NULL
;
553 struct sockaddr_in6
*dst6
;
556 #endif /* DUMMYNET */
558 struct route_in6 rin6
;
560 struct ip_fw_args args
;
561 #endif /* DUMMYNET */
563 #define rin6 ip6ibz.rin6
564 #define args ip6ibz.args
566 /* zero out {rin6, args} */
567 bzero(&ip6ibz
, sizeof (ip6ibz
));
570 * Check if the packet we received is valid after interface filter
573 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
574 inifp
= m
->m_pkthdr
.rcvif
;
575 VERIFY(inifp
!= NULL
);
577 /* Perform IP header alignment fixup, if needed */
578 IP6_HDR_ALIGNMENT_FIXUP(m
, inifp
, return);
580 m
->m_pkthdr
.pkt_flags
&= ~PKTF_FORWARDED
;
583 * should the inner packet be considered authentic?
584 * see comment in ah4_input().
586 m
->m_flags
&= ~M_AUTHIPHDR
;
587 m
->m_flags
&= ~M_AUTHIPDGM
;
591 * make sure we don't have onion peering information into m_aux.
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
;
600 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
602 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
604 m_tag_delete(m
, tag
);
607 if (args
.fwa_pf_rule
) {
608 ip6
= mtod(m
, struct ip6_hdr
*); /* In case PF got disabled */
612 #endif /* DUMMYNET */
615 * No need to proccess packet twice if we've already seen it.
617 inject_ipfref
= ipf_get_inject_filter(m
);
618 if (inject_ipfref
!= NULL
) {
619 ip6
= mtod(m
, struct ip6_hdr
*);
630 if (m
->m_flags
& M_EXT
) {
631 if (m
->m_next
!= NULL
)
632 ip6stat
.ip6s_mext2m
++;
634 ip6stat
.ip6s_mext1
++;
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
) {
640 ip6stat
.ip6s_m2m
[ifnet_index(lo_ifp
)]++;
641 } else if (inifp
->if_index
< M2MMAX
) {
642 ip6stat
.ip6s_m2m
[inifp
->if_index
]++;
644 ip6stat
.ip6s_m2m
[0]++;
653 * Drop the packet if IPv6 operation is disabled on the interface.
655 if (inifp
->if_eflags
& IFEF_IPV6_DISABLED
)
658 in6_ifstat_inc_na(inifp
, ifs6_in_receive
);
659 ip6stat
.ip6s_total
++;
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?
666 if (m
->m_next
!= NULL
&& m
->m_pkthdr
.len
< MCLBYTES
) {
669 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
672 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
673 MCLGET(n
, M_DONTWAIT
);
674 if ((n
->m_flags
& M_EXT
) == 0) {
682 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
683 n
->m_len
= m
->m_pkthdr
.len
;
687 IP6_EXTHDR_CHECK(m
, 0, sizeof (struct ip6_hdr
), { goto done
; });
689 if (m
->m_len
< sizeof (struct ip6_hdr
)) {
690 if ((m
= m_pullup(m
, sizeof (struct ip6_hdr
))) == 0) {
691 ip6stat
.ip6s_toosmall
++;
692 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
697 ip6
= mtod(m
, struct ip6_hdr
*);
699 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
700 ip6stat
.ip6s_badvers
++;
701 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
705 ip6stat
.ip6s_nxthist
[ip6
->ip6_nxt
]++;
708 * Check against address spoofing/corruption.
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
);
716 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
) ||
717 IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_dst
)) {
719 * XXX: "badscope" is not very suitable for a multicast source.
721 ip6stat
.ip6s_badscope
++;
722 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
725 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
) &&
726 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
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.
733 ip6stat
.ip6s_badscope
++;
734 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
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
742 * IPv6 src ::ffff:127.0.0.1). Be cautious.
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
748 * partial support for SIIT environment.
750 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
751 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
752 ip6stat
.ip6s_badscope
++;
753 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
758 * Reject packets with IPv4 compatible addresses (auto tunnel).
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.
764 if (IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_src
) ||
765 IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_dst
)) {
766 ip6stat
.ip6s_badscope
++;
767 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
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.
784 struct rtentry
*rte
=
785 ifnet_cached_rtlookup_inet6(inifp
, &ip6
->ip6_src
);
787 nstat_route_rx(rte
, 1, m
->m_pkthdr
.len
, 0);
792 /* for consistency */
793 m
->m_pkthdr
.pkt_proto
= ip6
->ip6_nxt
;
797 #endif /* DUMMYNET */
799 /* Invoke inbound packet filter */
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 */
807 if (error
!= 0 || m
== NULL
) {
809 panic("%s: unexpected packet %p\n",
813 /* Already freed by callee */
816 ip6
= mtod(m
, struct ip6_hdr
*);
820 /* drop packets if interface ID portion is already filled */
821 if (!(inifp
->if_flags
& IFF_LOOPBACK
) &&
822 !(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
823 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
) &&
824 ip6
->ip6_src
.s6_addr16
[1]) {
825 ip6stat
.ip6s_badscope
++;
828 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
) &&
829 ip6
->ip6_dst
.s6_addr16
[1]) {
830 ip6stat
.ip6s_badscope
++;
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
);
839 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
))
840 ip6
->ip6_dst
.s6_addr16
[1] =
841 htons(m
->m_pkthdr
.dst_ifindex
);
843 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
844 ip6
->ip6_src
.s6_addr16
[1] = htons(inifp
->if_index
);
845 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
))
846 ip6
->ip6_dst
.s6_addr16
[1] = htons(inifp
->if_index
);
852 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
853 struct in6_multi
*in6m
= NULL
;
855 in6_ifstat_inc_na(inifp
, ifs6_in_mcast
);
857 * See if we belong to the destination multicast group on the
860 in6_multihead_lock_shared();
861 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, inifp
, in6m
);
862 in6_multihead_lock_done();
866 } else if (!nd6_prproxy
) {
867 ip6stat
.ip6s_notmember
++;
868 ip6stat
.ip6s_cantforward
++;
869 in6_ifstat_inc(inifp
, ifs6_in_discard
);
880 * Fast path: see if the target is ourselves.
882 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
883 for (ia6
= in6_ifaddrs
; ia6
!= NULL
; ia6
= ia6
->ia_next
) {
885 * No reference is held on the address, as we just need
886 * to test for a few things while holding the RW lock.
888 if (IN6_ARE_ADDR_EQUAL(&ia6
->ia_addr
.sin6_addr
, &ip6
->ip6_dst
))
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.)
898 if (!(ia6
->ia6_flags
& IN6_IFF_NOTREADY
)) {
899 /* this address is ready */
901 deliverifp
= ia6
->ia_ifp
;
903 * record dst address information into mbuf.
905 (void) ip6_setdstifaddr_info(m
, 0, ia6
);
906 lck_rw_done(&in6_ifaddr_rwlock
);
909 lck_rw_done(&in6_ifaddr_rwlock
);
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
)));
917 lck_rw_done(&in6_ifaddr_rwlock
);
920 * Slow path: route lookup.
922 dst6
= SIN6(&rin6
.ro_dst
);
923 dst6
->sin6_len
= sizeof (struct sockaddr_in6
);
924 dst6
->sin6_family
= AF_INET6
;
925 dst6
->sin6_addr
= ip6
->ip6_dst
;
927 rtalloc_scoped_ign((struct route
*)&rin6
,
928 RTF_PRCLONING
, IFSCOPE_NONE
);
929 if (rin6
.ro_rt
!= NULL
)
930 RT_LOCK_SPIN(rin6
.ro_rt
);
932 #define rt6_key(r) (SIN6((r)->rt_nodes->rn_key))
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.
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?
954 if (rin6
.ro_rt
!= NULL
&&
955 (rin6
.ro_rt
->rt_flags
& (RTF_HOST
|RTF_GATEWAY
)) == RTF_HOST
&&
957 !(rin6
.ro_rt
->rt_flags
& RTF_WASCLONED
) &&
959 rin6
.ro_rt
->rt_ifp
->if_type
== IFT_LOOP
) {
960 ia6
= (struct in6_ifaddr
*)rin6
.ro_rt
->rt_ifa
;
962 * Packets to a tentative, duplicated, or somehow invalid
963 * address must not be accepted.
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.)
969 if (!(ia6
->ia6_flags
& IN6_IFF_NOTREADY
)) {
970 /* this address is ready */
972 deliverifp
= ia6
->ia_ifp
; /* correct? */
974 * record dst address information into mbuf.
976 (void) ip6_setdstifaddr_info(m
, 0, ia6
);
977 RT_UNLOCK(rin6
.ro_rt
);
980 RT_UNLOCK(rin6
.ro_rt
);
982 /* address is not ready, so discard the packet. */
983 nd6log((LOG_INFO
, "%s: packet to an unready address %s->%s\n",
984 __func__
, ip6_sprintf(&ip6
->ip6_src
),
985 ip6_sprintf(&ip6
->ip6_dst
)));
989 if (rin6
.ro_rt
!= NULL
)
990 RT_UNLOCK(rin6
.ro_rt
);
993 * Now there is no reason to process the packet if it's not our own
994 * and we're not a router.
996 if (!ip6_forwarding
) {
997 ip6stat
.ip6s_cantforward
++;
998 in6_ifstat_inc(inifp
, ifs6_in_discard
);
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.
1004 if (inifp
->if_type
== IFT_CELLULAR
&&
1005 ip6
->ip6_nxt
== IPPROTO_ICMPV6
)
1006 in6_ifstat_inc(inifp
, ifs6_cantfoward_icmp6
);
1012 * record dst address information into mbuf, if we don't have one yet.
1013 * note that we are unable to record it, if the address is not listed
1014 * as our interface address (e.g. multicast addresses, etc.)
1016 if (deliverifp
!= NULL
&& ia6
== NULL
) {
1017 ia6
= in6_ifawithifp(deliverifp
, &ip6
->ip6_dst
);
1019 (void) ip6_setdstifaddr_info(m
, 0, ia6
);
1020 IFA_REMREF(&ia6
->ia_ifa
);
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.
1029 plen
= (u_int32_t
)ntohs(ip6
->ip6_plen
);
1030 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
1031 struct ip6_hbh
*hbh
;
1034 * Mark the packet to imply that HBH option has been checked.
1035 * This can only be true is the packet came in unfragmented
1036 * or if the option is in the first fragment
1038 m
->m_pkthdr
.pkt_flags
|= PKTF_HBH_CHKED
;
1039 if (ip6_hopopts_input(&plen
, &rtalert
, &m
, &off
)) {
1040 #if 0 /* touches NULL pointer */
1041 in6_ifstat_inc(inifp
, ifs6_in_discard
);
1043 goto done
; /* m have already been freed */
1046 /* adjust pointer */
1047 ip6
= mtod(m
, struct ip6_hdr
*);
1050 * if the payload length field is 0 and the next header field
1051 * indicates Hop-by-Hop Options header, then a Jumbo Payload
1052 * option MUST be included.
1054 if (ip6
->ip6_plen
== 0 && plen
== 0) {
1056 * Note that if a valid jumbo payload option is
1057 * contained, ip6_hopopts_input() must set a valid
1058 * (non-zero) payload length to the variable plen.
1060 ip6stat
.ip6s_badoptions
++;
1061 in6_ifstat_inc(inifp
, ifs6_in_discard
);
1062 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
1063 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_HEADER
,
1064 (caddr_t
)&ip6
->ip6_plen
- (caddr_t
)ip6
);
1067 /* ip6_hopopts_input() ensures that mbuf is contiguous */
1068 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
1069 nxt
= hbh
->ip6h_nxt
;
1072 * If we are acting as a router and the packet contains a
1073 * router alert option, see if we know the option value.
1074 * Currently, we only support the option value for MLD, in which
1075 * case we should pass the packet to the multicast routing
1078 if (rtalert
!= ~0 && ip6_forwarding
) {
1080 case IP6OPT_RTALERT_MLD
:
1085 * RFC2711 requires unrecognized values must be
1095 * Check that the amount of data in the buffers
1096 * is as at least much as the IPv6 header would have us expect.
1097 * Trim mbufs if longer than we expect.
1098 * Drop packet if shorter than we expect.
1100 if (m
->m_pkthdr
.len
- sizeof (struct ip6_hdr
) < plen
) {
1101 ip6stat
.ip6s_tooshort
++;
1102 in6_ifstat_inc(inifp
, ifs6_in_truncated
);
1105 if (m
->m_pkthdr
.len
> sizeof (struct ip6_hdr
) + plen
) {
1106 ip6_input_adjust(m
, ip6
, plen
, inifp
);
1110 * Forward if desirable.
1112 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1113 if (!ours
&& nd6_prproxy
) {
1115 * If this isn't for us, this might be a Neighbor
1116 * Solicitation (dst is solicited-node multicast)
1117 * against an address in one of the proxied prefixes;
1118 * if so, claim the packet and let icmp6_input()
1121 ours
= nd6_prproxy_isours(m
, ip6
, NULL
, IFSCOPE_NONE
);
1123 (m
->m_pkthdr
.pkt_flags
& PKTF_PROXY_DST
));
1129 * The unicast forwarding function might return the packet
1130 * if we are proxying prefix(es), and if the packet is an
1131 * ICMPv6 packet that has failed the zone checks, but is
1132 * targetted towards a proxied address (this is optimized by
1133 * way of RTF_PROXY test.) If so, claim the packet as ours
1134 * and let icmp6_input() handle the rest. The packet's hop
1135 * limit value is kept intact (it's not decremented). This
1136 * is for supporting Neighbor Unreachability Detection between
1137 * proxied nodes on different links (src is link-local, dst
1138 * is target address.)
1140 if ((m
= ip6_forward(m
, &rin6
, 0)) == NULL
)
1142 VERIFY(rin6
.ro_rt
!= NULL
);
1143 VERIFY(m
->m_pkthdr
.pkt_flags
& PKTF_PROXY_DST
);
1144 deliverifp
= rin6
.ro_rt
->rt_ifp
;
1148 ip6
= mtod(m
, struct ip6_hdr
*);
1151 * Malicious party may be able to use IPv4 mapped addr to confuse
1152 * tcp/udp stack and bypass security checks (act as if it was from
1153 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
1155 * For SIIT end node behavior, you may want to disable the check.
1156 * However, you will become vulnerable to attacks using IPv4 mapped
1159 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
1160 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
1161 ip6stat
.ip6s_badscope
++;
1162 in6_ifstat_inc(inifp
, ifs6_in_addrerr
);
1167 * Tell launch routine the next header
1169 ip6stat
.ip6s_delivered
++;
1170 in6_ifstat_inc_na(deliverifp
, ifs6_in_deliver
);
1176 * Perform IP header alignment fixup again, if needed. Note that
1177 * we do it once for the outermost protocol, and we assume each
1178 * protocol handler wouldn't mess with the alignment afterwards.
1180 IP6_HDR_ALIGNMENT_FIXUP(m
, inifp
, return);
1182 while (nxt
!= IPPROTO_DONE
) {
1183 struct ipfilter
*filter
;
1184 int (*pr_input
)(struct mbuf
**, int *, int);
1187 * This would imply either IPPROTO_HOPOPTS was not the first
1188 * option or it did not come in the first fragment.
1190 if (nxt
== IPPROTO_HOPOPTS
&&
1191 (m
->m_pkthdr
.pkt_flags
& PKTF_HBH_CHKED
) == 0) {
1193 * This implies that HBH option was not contained
1194 * in the first fragment
1196 ip6stat
.ip6s_badoptions
++;
1200 if (ip6_hdrnestlimit
&& (++nest
> ip6_hdrnestlimit
)) {
1201 ip6stat
.ip6s_toomanyhdr
++;
1206 * protection against faulty packet - there should be
1207 * more sanity checks in header chain processing.
1209 if (m
->m_pkthdr
.len
< off
) {
1210 ip6stat
.ip6s_tooshort
++;
1211 in6_ifstat_inc(inifp
, ifs6_in_truncated
);
1217 * enforce IPsec policy checking if we are seeing last header.
1218 * note that we do not visit this with protocols with pcb layer
1219 * code - like udp/tcp/raw ip.
1221 if ((ipsec_bypass
== 0) &&
1222 (ip6_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0) {
1223 if (ipsec6_in_reject(m
, NULL
)) {
1224 IPSEC_STAT_INCREMENT(ipsec6stat
.in_polvio
);
1233 if (!TAILQ_EMPTY(&ipv6_filters
) && !IFNET_IS_INTCOPROC(inifp
)) {
1235 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
1237 if ((struct ipfilter
*)inject_ipfref
==
1240 } else if (filter
->ipf_filter
.ipf_input
) {
1243 result
= filter
->ipf_filter
.ipf_input(
1244 filter
->ipf_filter
.cookie
,
1245 (mbuf_t
*)&m
, off
, nxt
);
1246 if (result
== EJUSTRETURN
) {
1259 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1260 struct ip6_hdr
*, ip6
, struct ifnet
*, inifp
,
1261 struct ip
*, NULL
, struct ip6_hdr
*, ip6
);
1263 if ((pr_input
= ip6_protox
[nxt
]->pr_input
) == NULL
) {
1267 } else if (!(ip6_protox
[nxt
]->pr_flags
& PR_PROTOLOCK
)) {
1268 lck_mtx_lock(inet6_domain_mutex
);
1269 nxt
= pr_input(&m
, &off
, nxt
);
1270 lck_mtx_unlock(inet6_domain_mutex
);
1272 nxt
= pr_input(&m
, &off
, nxt
);
1276 ROUTE_RELEASE(&rin6
);
1284 ip6_setsrcifaddr_info(struct mbuf
*m
, uint32_t src_idx
, struct in6_ifaddr
*ia6
)
1286 VERIFY(m
->m_flags
& M_PKTHDR
);
1289 * If the source ifaddr is specified, pick up the information
1290 * from there; otherwise just grab the passed-in ifindex as the
1291 * caller may not have the ifaddr available.
1294 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1295 m
->m_pkthdr
.src_ifindex
= ia6
->ia_ifp
->if_index
;
1297 /* See IN6_IFF comments in in6_var.h */
1298 m
->m_pkthdr
.src_iff
= (ia6
->ia6_flags
& 0xffff);
1300 m
->m_pkthdr
.src_iff
= 0;
1301 m
->m_pkthdr
.src_ifindex
= src_idx
;
1303 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1308 ip6_setdstifaddr_info(struct mbuf
*m
, uint32_t dst_idx
, struct in6_ifaddr
*ia6
)
1310 VERIFY(m
->m_flags
& M_PKTHDR
);
1313 * If the destination ifaddr is specified, pick up the information
1314 * from there; otherwise just grab the passed-in ifindex as the
1315 * caller may not have the ifaddr available.
1318 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1319 m
->m_pkthdr
.dst_ifindex
= ia6
->ia_ifp
->if_index
;
1321 /* See IN6_IFF comments in in6_var.h */
1322 m
->m_pkthdr
.dst_iff
= (ia6
->ia6_flags
& 0xffff);
1324 m
->m_pkthdr
.dst_iff
= 0;
1325 m
->m_pkthdr
.dst_ifindex
= dst_idx
;
1327 m
->m_pkthdr
.pkt_flags
|= PKTF_IFAINFO
;
1332 ip6_getsrcifaddr_info(struct mbuf
*m
, uint32_t *src_idx
, uint32_t *ia6f
)
1334 VERIFY(m
->m_flags
& M_PKTHDR
);
1336 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
1339 if (src_idx
!= NULL
)
1340 *src_idx
= m
->m_pkthdr
.src_ifindex
;
1343 *ia6f
= m
->m_pkthdr
.src_iff
;
1349 ip6_getdstifaddr_info(struct mbuf
*m
, uint32_t *dst_idx
, uint32_t *ia6f
)
1351 VERIFY(m
->m_flags
& M_PKTHDR
);
1353 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
1356 if (dst_idx
!= NULL
)
1357 *dst_idx
= m
->m_pkthdr
.dst_ifindex
;
1360 *ia6f
= m
->m_pkthdr
.dst_iff
;
1366 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1367 * included, the real payload length will be stored in plenp.
1370 ip6_hopopts_input(uint32_t *plenp
, uint32_t *rtalertp
, struct mbuf
**mp
,
1373 struct mbuf
*m
= *mp
;
1374 int off
= *offp
, hbhlen
;
1375 struct ip6_hbh
*hbh
;
1378 /* validation of the length of the header */
1379 IP6_EXTHDR_CHECK(m
, off
, sizeof (*hbh
), return (-1));
1380 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1381 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1383 IP6_EXTHDR_CHECK(m
, off
, hbhlen
, return (-1));
1384 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1386 hbhlen
-= sizeof (struct ip6_hbh
);
1387 opt
= (u_int8_t
*)hbh
+ sizeof (struct ip6_hbh
);
1389 if (ip6_process_hopopts(m
, (u_int8_t
*)hbh
+ sizeof (struct ip6_hbh
),
1390 hbhlen
, rtalertp
, plenp
) < 0)
1399 * Search header for all Hop-by-hop options and process each option.
1400 * This function is separate from ip6_hopopts_input() in order to
1401 * handle a case where the sending node itself process its hop-by-hop
1402 * options header. In such a case, the function is called from ip6_output().
1404 * The function assumes that hbh header is located right after the IPv6 header
1405 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1406 * opthead + hbhlen is located in continuous memory region.
1409 ip6_process_hopopts(struct mbuf
*m
, u_int8_t
*opthead
, int hbhlen
,
1410 u_int32_t
*rtalertp
, u_int32_t
*plenp
)
1412 struct ip6_hdr
*ip6
;
1414 u_int8_t
*opt
= opthead
;
1415 u_int16_t rtalert_val
;
1416 u_int32_t jumboplen
;
1417 const int erroff
= sizeof (struct ip6_hdr
) + sizeof (struct ip6_hbh
);
1419 for (; hbhlen
> 0; hbhlen
-= optlen
, opt
+= optlen
) {
1425 if (hbhlen
< IP6OPT_MINLEN
) {
1426 ip6stat
.ip6s_toosmall
++;
1429 optlen
= *(opt
+ 1) + 2;
1431 case IP6OPT_ROUTER_ALERT
:
1432 /* XXX may need check for alignment */
1433 if (hbhlen
< IP6OPT_RTALERT_LEN
) {
1434 ip6stat
.ip6s_toosmall
++;
1437 if (*(opt
+ 1) != IP6OPT_RTALERT_LEN
- 2) {
1439 icmp6_error(m
, ICMP6_PARAM_PROB
,
1440 ICMP6_PARAMPROB_HEADER
,
1441 erroff
+ opt
+ 1 - opthead
);
1444 optlen
= IP6OPT_RTALERT_LEN
;
1445 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&rtalert_val
, 2);
1446 *rtalertp
= ntohs(rtalert_val
);
1449 /* XXX may need check for alignment */
1450 if (hbhlen
< IP6OPT_JUMBO_LEN
) {
1451 ip6stat
.ip6s_toosmall
++;
1454 if (*(opt
+ 1) != IP6OPT_JUMBO_LEN
- 2) {
1456 icmp6_error(m
, ICMP6_PARAM_PROB
,
1457 ICMP6_PARAMPROB_HEADER
,
1458 erroff
+ opt
+ 1 - opthead
);
1461 optlen
= IP6OPT_JUMBO_LEN
;
1464 * IPv6 packets that have non 0 payload length
1465 * must not contain a jumbo payload option.
1467 ip6
= mtod(m
, struct ip6_hdr
*);
1468 if (ip6
->ip6_plen
) {
1469 ip6stat
.ip6s_badoptions
++;
1470 icmp6_error(m
, ICMP6_PARAM_PROB
,
1471 ICMP6_PARAMPROB_HEADER
,
1472 erroff
+ opt
- opthead
);
1477 * We may see jumbolen in unaligned location, so
1478 * we'd need to perform bcopy().
1480 bcopy(opt
+ 2, &jumboplen
, sizeof (jumboplen
));
1481 jumboplen
= (u_int32_t
)htonl(jumboplen
);
1485 * if there are multiple jumbo payload options,
1486 * *plenp will be non-zero and the packet will be
1488 * the behavior may need some debate in ipngwg -
1489 * multiple options does not make sense, however,
1490 * there's no explicit mention in specification.
1493 ip6stat
.ip6s_badoptions
++;
1494 icmp6_error(m
, ICMP6_PARAM_PROB
,
1495 ICMP6_PARAMPROB_HEADER
,
1496 erroff
+ opt
+ 2 - opthead
);
1502 * jumbo payload length must be larger than 65535.
1504 if (jumboplen
<= IPV6_MAXPACKET
) {
1505 ip6stat
.ip6s_badoptions
++;
1506 icmp6_error(m
, ICMP6_PARAM_PROB
,
1507 ICMP6_PARAMPROB_HEADER
,
1508 erroff
+ opt
+ 2 - opthead
);
1514 default: /* unknown option */
1515 if (hbhlen
< IP6OPT_MINLEN
) {
1516 ip6stat
.ip6s_toosmall
++;
1519 optlen
= ip6_unknown_opt(opt
, m
,
1520 erroff
+ opt
- opthead
);
1537 * Unknown option processing.
1538 * The third argument `off' is the offset from the IPv6 header to the option,
1539 * which is necessary if the IPv6 header the and option header and IPv6 header
1540 * is not continuous in order to return an ICMPv6 error.
1543 ip6_unknown_opt(uint8_t *optp
, struct mbuf
*m
, int off
)
1545 struct ip6_hdr
*ip6
;
1547 switch (IP6OPT_TYPE(*optp
)) {
1548 case IP6OPT_TYPE_SKIP
: /* ignore the option */
1549 return ((int)*(optp
+ 1));
1551 case IP6OPT_TYPE_DISCARD
: /* silently discard */
1555 case IP6OPT_TYPE_FORCEICMP
: /* send ICMP even if multicasted */
1556 ip6stat
.ip6s_badoptions
++;
1557 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_OPTION
, off
);
1560 case IP6OPT_TYPE_ICMP
: /* send ICMP if not multicasted */
1561 ip6stat
.ip6s_badoptions
++;
1562 ip6
= mtod(m
, struct ip6_hdr
*);
1563 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
1564 (m
->m_flags
& (M_BCAST
|M_MCAST
))) {
1567 icmp6_error(m
, ICMP6_PARAM_PROB
,
1568 ICMP6_PARAMPROB_OPTION
, off
);
1573 m_freem(m
); /* XXX: NOTREACHED */
1578 * Create the "control" list for this pcb.
1579 * These functions will not modify mbuf chain at all.
1581 * With KAME mbuf chain restriction:
1582 * The routine will be called from upper layer handlers like tcp6_input().
1583 * Thus the routine assumes that the caller (tcp6_input) have already
1584 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1585 * very first mbuf on the mbuf chain.
1587 * ip6_savecontrol_v4 will handle those options that are possible to be
1588 * set on a v4-mapped socket.
1589 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1590 * options and handle the v6-only ones itself.
1593 ip6_savecontrol_v4(struct inpcb
*inp
, struct mbuf
*m
, struct mbuf
**mp
,
1596 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1598 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0) {
1602 mp
= sbcreatecontrol_mbuf((caddr_t
)&tv
, sizeof (tv
),
1603 SCM_TIMESTAMP
, SOL_SOCKET
, mp
);
1607 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
1610 time
= mach_absolute_time();
1611 mp
= sbcreatecontrol_mbuf((caddr_t
)&time
, sizeof (time
),
1612 SCM_TIMESTAMP_MONOTONIC
, SOL_SOCKET
, mp
);
1616 if ((inp
->inp_socket
->so_flags
& SOF_RECV_TRAFFIC_CLASS
) != 0) {
1617 int tc
= m_get_traffic_class(m
);
1619 mp
= sbcreatecontrol_mbuf((caddr_t
)&tc
, sizeof (tc
),
1620 SO_TRAFFIC_CLASS
, SOL_SOCKET
, mp
);
1625 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
1631 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1632 /* RFC 2292 sec. 5 */
1633 if ((inp
->inp_flags
& IN6P_PKTINFO
) != 0) {
1634 struct in6_pktinfo pi6
;
1636 bcopy(&ip6
->ip6_dst
, &pi6
.ipi6_addr
, sizeof (struct in6_addr
));
1637 in6_clearscope(&pi6
.ipi6_addr
); /* XXX */
1639 (m
&& m
->m_pkthdr
.rcvif
) ? m
->m_pkthdr
.rcvif
->if_index
: 0;
1641 mp
= sbcreatecontrol_mbuf((caddr_t
)&pi6
,
1642 sizeof (struct in6_pktinfo
),
1643 IS2292(inp
, IPV6_2292PKTINFO
, IPV6_PKTINFO
),
1649 if ((inp
->inp_flags
& IN6P_HOPLIMIT
) != 0) {
1650 int hlim
= ip6
->ip6_hlim
& 0xff;
1652 mp
= sbcreatecontrol_mbuf((caddr_t
)&hlim
, sizeof (int),
1653 IS2292(inp
, IPV6_2292HOPLIMIT
, IPV6_HOPLIMIT
),
1665 ip6_savecontrol(struct inpcb
*in6p
, struct mbuf
*m
, struct mbuf
**mp
)
1668 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1672 np
= ip6_savecontrol_v4(in6p
, m
, mp
, &v4only
);
1680 if ((in6p
->inp_flags
& IN6P_TCLASS
) != 0) {
1684 flowinfo
= (u_int32_t
)ntohl(ip6
->ip6_flow
& IPV6_FLOWINFO_MASK
);
1687 tclass
= flowinfo
& 0xff;
1688 mp
= sbcreatecontrol_mbuf((caddr_t
)&tclass
, sizeof (tclass
),
1689 IPV6_TCLASS
, IPPROTO_IPV6
, mp
);
1695 * IPV6_HOPOPTS socket option. Recall that we required super-user
1696 * privilege for the option (see ip6_ctloutput), but it might be too
1697 * strict, since there might be some hop-by-hop options which can be
1698 * returned to normal user.
1699 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1701 if ((in6p
->inp_flags
& IN6P_HOPOPTS
) != 0) {
1703 * Check if a hop-by-hop options header is contatined in the
1704 * received packet, and if so, store the options as ancillary
1705 * data. Note that a hop-by-hop options header must be
1706 * just after the IPv6 header, which is assured through the
1707 * IPv6 input processing.
1709 ip6
= mtod(m
, struct ip6_hdr
*);
1710 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
1711 struct ip6_hbh
*hbh
;
1713 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
1714 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1717 * XXX: We copy the whole header even if a
1718 * jumbo payload option is included, the option which
1719 * is to be removed before returning according to
1721 * Note: this constraint is removed in RFC3542
1723 mp
= sbcreatecontrol_mbuf((caddr_t
)hbh
, hbhlen
,
1724 IS2292(in6p
, IPV6_2292HOPOPTS
, IPV6_HOPOPTS
),
1733 if ((in6p
->inp_flags
& (IN6P_RTHDR
| IN6P_DSTOPTS
)) != 0) {
1734 int nxt
= ip6
->ip6_nxt
, off
= sizeof (struct ip6_hdr
);
1737 * Search for destination options headers or routing
1738 * header(s) through the header chain, and stores each
1739 * header as ancillary data.
1740 * Note that the order of the headers remains in
1741 * the chain of ancillary data.
1743 while (1) { /* is explicit loop prevention necessary? */
1744 struct ip6_ext
*ip6e
= NULL
;
1748 * if it is not an extension header, don't try to
1749 * pull it from the chain.
1752 case IPPROTO_DSTOPTS
:
1753 case IPPROTO_ROUTING
:
1754 case IPPROTO_HOPOPTS
:
1755 case IPPROTO_AH
: /* is it possible? */
1761 if (off
+ sizeof (*ip6e
) > m
->m_len
)
1763 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + off
);
1764 if (nxt
== IPPROTO_AH
)
1765 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1767 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1768 if (off
+ elen
> m
->m_len
)
1772 case IPPROTO_DSTOPTS
:
1773 if (!(in6p
->inp_flags
& IN6P_DSTOPTS
))
1776 mp
= sbcreatecontrol_mbuf((caddr_t
)ip6e
, elen
,
1777 IS2292(in6p
, IPV6_2292DSTOPTS
,
1778 IPV6_DSTOPTS
), IPPROTO_IPV6
, mp
);
1783 case IPPROTO_ROUTING
:
1784 if (!(in6p
->inp_flags
& IN6P_RTHDR
))
1787 mp
= sbcreatecontrol_mbuf((caddr_t
)ip6e
, elen
,
1788 IS2292(in6p
, IPV6_2292RTHDR
, IPV6_RTHDR
),
1794 case IPPROTO_HOPOPTS
:
1795 case IPPROTO_AH
: /* is it possible? */
1800 * other cases have been filtered in the above.
1801 * none will visit this case. here we supply
1802 * the code just in case (nxt overwritten or
1809 /* proceed with the next header. */
1811 nxt
= ip6e
->ip6e_nxt
;
1819 ip6stat
.ip6s_pktdropcntrl
++;
1820 /* XXX increment a stat to show the failure */
1826 ip6_notify_pmtu(struct inpcb
*in6p
, struct sockaddr_in6
*dst
, u_int32_t
*mtu
)
1830 struct ip6_mtuinfo mtuctl
;
1832 so
= in6p
->inp_socket
;
1834 if ((in6p
->inp_flags
& IN6P_MTU
) == 0)
1841 if (so
== NULL
) { /* I believe this is impossible */
1842 panic("ip6_notify_pmtu: socket is NULL");
1847 if (IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
1848 (so
->so_proto
== NULL
|| so
->so_proto
->pr_protocol
== IPPROTO_TCP
))
1851 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
1852 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &dst
->sin6_addr
))
1855 bzero(&mtuctl
, sizeof (mtuctl
)); /* zero-clear for safety */
1856 mtuctl
.ip6m_mtu
= *mtu
;
1857 mtuctl
.ip6m_addr
= *dst
;
1858 if (sa6_recoverscope(&mtuctl
.ip6m_addr
, TRUE
))
1861 if ((m_mtu
= sbcreatecontrol((caddr_t
)&mtuctl
, sizeof (mtuctl
),
1862 IPV6_PATHMTU
, IPPROTO_IPV6
)) == NULL
)
1865 if (sbappendaddr(&so
->so_rcv
, SA(dst
), NULL
, m_mtu
, NULL
) == 0) {
1867 /* XXX: should count statistics */
1874 * Get pointer to the previous header followed by the header
1875 * currently processed.
1876 * XXX: This function supposes that
1877 * M includes all headers,
1878 * the next header field and the header length field of each header
1880 * the sum of each header length equals to OFF.
1881 * Because of these assumptions, this function must be called very
1882 * carefully. Moreover, it will not be used in the near future when
1883 * we develop `neater' mechanism to process extension headers.
1886 ip6_get_prevhdr(struct mbuf
*m
, int off
)
1888 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1890 if (off
== sizeof (struct ip6_hdr
)) {
1891 return ((char *)&ip6
->ip6_nxt
);
1894 struct ip6_ext
*ip6e
= NULL
;
1897 len
= sizeof (struct ip6_hdr
);
1899 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + len
);
1902 case IPPROTO_FRAGMENT
:
1903 len
+= sizeof (struct ip6_frag
);
1906 len
+= (ip6e
->ip6e_len
+ 2) << 2;
1909 len
+= (ip6e
->ip6e_len
+ 1) << 3;
1912 nxt
= ip6e
->ip6e_nxt
;
1915 return ((char *)&ip6e
->ip6e_nxt
);
1922 * get next header offset. m will be retained.
1925 ip6_nexthdr(struct mbuf
*m
, int off
, int proto
, int *nxtp
)
1928 struct ip6_ext ip6e
;
1933 if ((m
->m_flags
& M_PKTHDR
) == 0 || m
->m_pkthdr
.len
< off
)
1938 if (m
->m_pkthdr
.len
< off
+ sizeof (ip6
))
1940 m_copydata(m
, off
, sizeof (ip6
), (caddr_t
)&ip6
);
1942 *nxtp
= ip6
.ip6_nxt
;
1943 off
+= sizeof (ip6
);
1946 case IPPROTO_FRAGMENT
:
1948 * terminate parsing if it is not the first fragment,
1949 * it does not make sense to parse through it.
1951 if (m
->m_pkthdr
.len
< off
+ sizeof (fh
))
1953 m_copydata(m
, off
, sizeof (fh
), (caddr_t
)&fh
);
1954 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1955 if (fh
.ip6f_offlg
& IP6F_OFF_MASK
)
1958 *nxtp
= fh
.ip6f_nxt
;
1959 off
+= sizeof (struct ip6_frag
);
1963 if (m
->m_pkthdr
.len
< off
+ sizeof (ip6e
))
1965 m_copydata(m
, off
, sizeof (ip6e
), (caddr_t
)&ip6e
);
1967 *nxtp
= ip6e
.ip6e_nxt
;
1968 off
+= (ip6e
.ip6e_len
+ 2) << 2;
1971 case IPPROTO_HOPOPTS
:
1972 case IPPROTO_ROUTING
:
1973 case IPPROTO_DSTOPTS
:
1974 if (m
->m_pkthdr
.len
< off
+ sizeof (ip6e
))
1976 m_copydata(m
, off
, sizeof (ip6e
), (caddr_t
)&ip6e
);
1978 *nxtp
= ip6e
.ip6e_nxt
;
1979 off
+= (ip6e
.ip6e_len
+ 1) << 3;
1984 case IPPROTO_IPCOMP
:
1994 * get offset for the last header in the chain. m will be kept untainted.
1997 ip6_lasthdr(struct mbuf
*m
, int off
, int proto
, int *nxtp
)
2007 newoff
= ip6_nexthdr(m
, off
, proto
, nxtp
);
2010 else if (newoff
< off
)
2011 return (-1); /* invalid */
2012 else if (newoff
== off
)
2021 ip6_addaux(struct mbuf
*m
)
2025 /* Check if one is already allocated */
2026 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2027 KERNEL_TAG_TYPE_INET6
, NULL
);
2029 /* Allocate a tag */
2030 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_INET6
,
2031 sizeof (struct ip6aux
), M_DONTWAIT
, m
);
2033 /* Attach it to the mbuf */
2035 m_tag_prepend(m
, tag
);
2039 return (tag
? (struct ip6aux
*)(tag
+ 1) : NULL
);
2043 ip6_findaux(struct mbuf
*m
)
2047 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2048 KERNEL_TAG_TYPE_INET6
, NULL
);
2050 return (tag
? (struct ip6aux
*)(tag
+ 1) : NULL
);
2054 ip6_delaux(struct mbuf
*m
)
2058 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2059 KERNEL_TAG_TYPE_INET6
, NULL
);
2061 m_tag_delete(m
, tag
);
2071 frag6_drain(); /* fragments */
2072 in6_rtqdrain(); /* protocol cloned routes */
2073 nd6_drain(NULL
); /* cloned routes: ND6 */
2077 * System control for IP6
2080 u_char inet6ctlerrmap
[PRC_NCMDS
] = {
2082 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2083 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2084 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
2090 sysctl_reset_ip6_input_stats SYSCTL_HANDLER_ARGS
2092 #pragma unused(arg1, arg2)
2095 i
= ip6_input_measure
;
2096 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
2097 if (error
|| req
->newptr
== USER_ADDR_NULL
)
2100 if (i
< 0 || i
> 1) {
2104 if (ip6_input_measure
!= i
&& i
== 1) {
2105 net_perf_initialize(&net_perf
, ip6_input_measure_bins
);
2107 ip6_input_measure
= i
;
2113 sysctl_ip6_input_measure_bins SYSCTL_HANDLER_ARGS
2115 #pragma unused(arg1, arg2)
2119 i
= ip6_input_measure_bins
;
2120 error
= sysctl_handle_quad(oidp
, &i
, 0, req
);
2121 if (error
|| req
->newptr
== USER_ADDR_NULL
)
2124 if (!net_perf_validate_bins(i
)) {
2128 ip6_input_measure_bins
= i
;
2134 sysctl_ip6_input_getperf SYSCTL_HANDLER_ARGS
2136 #pragma unused(oidp, arg1, arg2)
2137 if (req
->oldptr
== USER_ADDR_NULL
)
2138 req
->oldlen
= (size_t)sizeof (struct ipstat
);
2140 return (SYSCTL_OUT(req
, &net_perf
, MIN(sizeof (net_perf
), req
->oldlen
)));