2 * Copyright (c) 2003-2012 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@
28 /* $FreeBSD: src/sys/netinet6/ip6_input.c,v 1.11.2.10 2001/07/24 19:10:18 brooks Exp $ */
29 /* $KAME: ip6_input.c,v 1.194 2001/05/27 13:28:35 itojun Exp $ */
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * Copyright (c) 1982, 1986, 1988, 1993
62 * The Regents of the University of California. All rights reserved.
64 * Redistribution and use in source and binary forms, with or without
65 * modification, are permitted provided that the following conditions
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
72 * 3. All advertising materials mentioning features or use of this software
73 * must display the following acknowledgement:
74 * This product includes software developed by the University of
75 * California, Berkeley and its contributors.
76 * 4. Neither the name of the University nor the names of its contributors
77 * may be used to endorse or promote products derived from this software
78 * without specific prior written permission.
80 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
81 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
83 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
86 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
88 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
89 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
96 #include <sys/param.h>
97 #include <sys/systm.h>
98 #include <sys/malloc.h>
100 #include <sys/domain.h>
101 #include <sys/protosw.h>
102 #include <sys/socket.h>
103 #include <sys/socketvar.h>
104 #include <sys/errno.h>
105 #include <sys/time.h>
106 #include <sys/kernel.h>
107 #include <sys/syslog.h>
108 #include <sys/sysctl.h>
109 #include <sys/proc.h>
110 #include <sys/kauth.h>
111 #include <sys/mcache.h>
112 #include <mach/mach_time.h>
114 #include <pexpert/pexpert.h>
117 #include <net/if_var.h>
118 #include <net/if_types.h>
119 #include <net/if_dl.h>
120 #include <net/route.h>
121 #include <net/kpi_protocol.h>
122 #include <net/ntstat.h>
123 #include <net/init.h>
125 #include <netinet/in.h>
126 #include <netinet/in_systm.h>
128 #include <netinet/ip.h>
129 #include <netinet/ip_icmp.h>
131 #include <netinet/ip6.h>
132 #include <netinet6/in6_var.h>
133 #include <netinet6/ip6_var.h>
134 #include <netinet/in_pcb.h>
135 #include <netinet/icmp6.h>
136 #include <netinet6/in6_ifattach.h>
137 #include <netinet6/nd6.h>
138 #include <netinet6/scope6_var.h>
139 #include <mach/sdt.h>
142 #include <netinet6/ipsec.h>
144 #include <netinet6/ipsec6.h>
146 extern int ipsec_bypass
;
149 #include <netinet6/ip6_fw.h>
152 #include <netinet/ip_fw.h>
153 #include <netinet/ip_dummynet.h>
154 #endif /* DUMMYNET */
156 #include <netinet/kpi_ipfilter_var.h>
158 #include <netinet6/ip6protosw.h>
160 /* we need it for NLOOP. */
163 #include <net/net_osdep.h>
166 #include <net/pfvar.h>
169 extern struct domain inet6domain
;
170 extern struct ip6protosw inet6sw
[];
172 struct ip6protosw
* ip6_protox
[IPPROTO_MAX
];
173 static int ip6qmaxlen
= IFQ_MAXLEN
;
175 static lck_grp_attr_t
*in6_ifaddr_rwlock_grp_attr
;
176 static lck_grp_t
*in6_ifaddr_rwlock_grp
;
177 static lck_attr_t
*in6_ifaddr_rwlock_attr
;
178 decl_lck_rw_data(, in6_ifaddr_rwlock
);
180 /* Protected by in6_ifaddr_rwlock */
181 struct in6_ifaddr
*in6_ifaddrs
= NULL
;
183 int ip6_forward_srcrt
; /* XXX */
184 int ip6_sourcecheck
; /* XXX */
185 int ip6_sourcecheck_interval
; /* XXX */
186 const int int6intrq_present
= 1;
188 int ip6_ours_check_algorithm
;
190 #define IN6_IFSTAT_REQUIRE_ALIGNED_64(f) \
191 _CASSERT(!(offsetof(struct in6_ifstat, f) % sizeof (uint64_t)))
193 #define ICMP6_IFSTAT_REQUIRE_ALIGNED_64(f) \
194 _CASSERT(!(offsetof(struct icmp6_ifstat, f) % sizeof (uint64_t)))
198 ip6_fw_chk_t
*ip6_fw_chk_ptr
;
199 ip6_fw_ctl_t
*ip6_fw_ctl_ptr
;
200 int ip6_fw_enable
= 1;
203 struct ip6stat ip6stat
;
206 struct ifqueue ip6intrq
;
207 decl_lck_mtx_data(, ip6_init_mutex
);
208 decl_lck_mtx_data(, proxy6_lock
);
209 decl_lck_mtx_data(, dad6_mutex_data
);
210 decl_lck_mtx_data(, nd6_mutex_data
);
211 decl_lck_mtx_data(, prefix6_mutex_data
);
212 decl_lck_mtx_data(, scope6_mutex_data
);
213 lck_mtx_t
*dad6_mutex
= &dad6_mutex_data
;
214 lck_mtx_t
*nd6_mutex
= &nd6_mutex_data
;
215 lck_mtx_t
*prefix6_mutex
= &prefix6_mutex_data
;
216 lck_mtx_t
*scope6_mutex
= &scope6_mutex_data
;
217 #ifdef ENABLE_ADDRSEL
218 decl_lck_mtx_data(, addrsel_mutex_data
);
219 lck_mtx_t
*addrsel_mutex
= &addrsel_mutex_data
;
221 decl_lck_rw_data(, in6_ifs_rwlock
);
222 decl_lck_rw_data(, icmp6_ifs_rwlock
);
223 lck_attr_t
*ip6_mutex_attr
;
224 lck_grp_t
*ip6_mutex_grp
;
225 lck_grp_attr_t
*ip6_mutex_grp_attr
;
226 extern lck_mtx_t
*inet6_domain_mutex
;
228 extern int loopattach_done
;
229 extern void addrsel_policy_init(void);
231 static void ip6_init_delayed(void);
232 static struct ip6aux
*ip6_setdstifaddr(struct mbuf
*, struct in6_ifaddr
*);
234 static int ip6_hopopts_input(u_int32_t
*, u_int32_t
*, struct mbuf
**, int *);
236 static struct mbuf
*ip6_pullexthdr(struct mbuf
*, size_t, int);
240 void gifattach(void);
241 void stfattach(void);
244 SYSCTL_DECL(_net_inet6_ip6
);
246 int ip6_doscopedroute
= 1;
247 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, scopedroute
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
248 &ip6_doscopedroute
, 0, "Enable IPv6 scoped routing");
250 int ip6_restrictrecvif
= 1;
251 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, restrictrecvif
,
252 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_restrictrecvif
, 0,
253 "Enable inbound interface restrictions");
256 * On platforms which require strict alignment (currently for anything but
257 * i386 or x86_64), check if the IP header pointer is 32-bit aligned; if not,
258 * copy the contents of the mbuf chain into a new chain, and free the original
259 * one. Create some head room in the first mbuf of the new chain, in case
260 * it's needed later on.
262 * RFC 2460 says that IPv6 headers are 64-bit aligned, but network interfaces
263 * mostly align to 32-bit boundaries. Care should be taken never to use 64-bit
264 * load/store operations on the fields in IPv6 headers.
266 #if defined(__i386__) || defined(__x86_64__)
267 #define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0)
268 #else /* !__i386__ && !__x86_64__ */
269 #define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { \
270 if (!IP6_HDR_ALIGNED_P(mtod(_m, caddr_t))) { \
272 struct ifnet *__ifp = (_ifp); \
273 atomic_add_64(&(__ifp)->if_alignerrs, 1); \
274 if (((_m)->m_flags & M_PKTHDR) && \
275 (_m)->m_pkthdr.header != NULL) \
276 (_m)->m_pkthdr.header = NULL; \
277 _n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT); \
279 ip6stat.ip6s_toosmall++; \
284 VERIFY(_n != (_m)); \
289 #endif /* !__i386__ && !__x86_64__ */
293 __unused protocol_family_t protocol
,
300 * IP6 initialization: fill in IP6 protocol switch table.
301 * All protocols not implemented in kernel go to raw IP6 protocol handler.
306 struct ip6protosw
*pr
;
310 _CASSERT((sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
)) <=
313 PE_parse_boot_argn("net.inet6.ip6.scopedroute", &ip6_doscopedroute
,
314 sizeof (ip6_doscopedroute
));
317 if (sizeof(struct protosw
) != sizeof(struct ip6protosw
))
318 panic("sizeof(protosw) != sizeof(ip6protosw)");
320 pr
= (struct ip6protosw
*)pffindproto_locked(PF_INET6
, IPPROTO_RAW
, SOCK_RAW
);
323 for (i
= 0; i
< IPPROTO_MAX
; i
++)
325 for (pr
= (struct ip6protosw
*)inet6domain
.dom_protosw
; pr
; pr
= pr
->pr_next
) {
326 if(!(pr
->pr_domain
)) continue; /* If uninitialized, skip */
327 if (pr
->pr_domain
->dom_family
== PF_INET6
&&
328 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
) {
329 ip6_protox
[pr
->pr_protocol
] = pr
;
333 ip6_mutex_grp_attr
= lck_grp_attr_alloc_init();
335 ip6_mutex_grp
= lck_grp_alloc_init("ip6", ip6_mutex_grp_attr
);
336 ip6_mutex_attr
= lck_attr_alloc_init();
338 lck_mtx_init(dad6_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
339 lck_mtx_init(nd6_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
340 lck_mtx_init(prefix6_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
341 lck_mtx_init(scope6_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
343 #ifdef ENABLE_ADDRSEL
344 lck_mtx_init(addrsel_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
347 lck_mtx_init(&proxy6_lock
, ip6_mutex_grp
, ip6_mutex_attr
);
348 lck_mtx_init(&ip6_init_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
350 lck_rw_init(&in6_ifs_rwlock
, ip6_mutex_grp
, ip6_mutex_attr
);
351 lck_rw_init(&icmp6_ifs_rwlock
, ip6_mutex_grp
, ip6_mutex_attr
);
353 inet6domain
.dom_flags
= DOM_REENTRANT
;
355 ip6intrq
.ifq_maxlen
= ip6qmaxlen
;
357 in6_ifaddr_rwlock_grp_attr
= lck_grp_attr_alloc_init();
358 in6_ifaddr_rwlock_grp
= lck_grp_alloc_init("in6_ifaddr_rwlock",
359 in6_ifaddr_rwlock_grp_attr
);
360 in6_ifaddr_rwlock_attr
= lck_attr_alloc_init();
361 lck_rw_init(&in6_ifaddr_rwlock
, in6_ifaddr_rwlock_grp
,
362 in6_ifaddr_rwlock_attr
);
364 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_receive
);
365 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_hdrerr
);
366 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_toobig
);
367 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_noroute
);
368 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_addrerr
);
369 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_protounknown
);
370 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_truncated
);
371 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_discard
);
372 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_deliver
);
373 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_forward
);
374 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_request
);
375 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_discard
);
376 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragok
);
377 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragfail
);
378 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragcreat
);
379 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_reqd
);
380 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_ok
);
381 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_fail
);
382 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mcast
);
383 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mcast
);
385 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_msg
);
386 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_error
);
387 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_dstunreach
);
388 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_adminprohib
);
389 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_timeexceed
);
390 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_paramprob
);
391 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_pkttoobig
);
392 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echo
);
393 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echoreply
);
394 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routersolicit
);
395 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routeradvert
);
396 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighborsolicit
);
397 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighboradvert
);
398 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_redirect
);
399 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldquery
);
400 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldreport
);
401 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mlddone
);
403 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_msg
);
404 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_error
);
405 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_dstunreach
);
406 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_adminprohib
);
407 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_timeexceed
);
408 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_paramprob
);
409 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_pkttoobig
);
410 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echo
);
411 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echoreply
);
412 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routersolicit
);
413 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routeradvert
);
414 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighborsolicit
);
415 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighboradvert
);
416 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_redirect
);
417 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldquery
);
418 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldreport
);
419 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mlddone
);
426 addrsel_policy_init();
428 * in many cases, random() here does NOT return random number
429 * as initialization during bootstrap time occur in fixed order.
432 ip6_flow_seq
= random() ^ tv
.tv_usec
;
434 ip6_desync_factor
= (random() ^ tv
.tv_usec
) % MAX_TEMP_DESYNC_FACTOR
;
437 * P2P interfaces often route the local address to the loopback
438 * interface. At this point, lo0 hasn't been initialized yet, which
439 * means that we need to delay the IPv6 configuration of lo0.
441 net_init_add(ip6_init_delayed
);
443 domain_proto_mtx_unlock(TRUE
);
444 proto_register_input(PF_INET6
, ip6_proto_input
, NULL
, 0);
445 domain_proto_mtx_lock();
449 ip6_init_delayed(void)
451 (void) in6_ifattach(lo_ifp
, NULL
, NULL
);
455 timeout(nd6_timer
, (caddr_t
)0, hz
);
457 /* timer for regeneranation of temporary addresses randomize ID */
458 timeout(in6_tmpaddrtimer
, (caddr_t
)0,
459 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
460 ip6_temp_regen_advance
) * hz
);
468 #endif /* __APPLE__ */
472 ip6_input(struct mbuf
*m
)
475 int off
= sizeof(struct ip6_hdr
), nest
;
477 u_int32_t rtalert
= ~0;
478 int nxt
= 0, ours
= 0;
479 struct ifnet
*deliverifp
= NULL
;
480 ipfilter_t inject_ipfref
= 0;
482 struct in6_ifaddr
*ia6
= NULL
;
483 struct route_in6 ip6_forward_rt
;
484 struct sockaddr_in6
*dst6
;
487 struct ip_fw_args args
;
489 bzero(&args
, sizeof(struct ip_fw_args
));
490 #endif /* DUMMYNET */
492 bzero(&ip6_forward_rt
, sizeof(ip6_forward_rt
));
494 /* Check if the packet we received is valid after interface filter
497 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
499 /* Perform IP header alignment fixup, if needed */
500 IP6_HDR_ALIGNMENT_FIXUP(m
, m
->m_pkthdr
.rcvif
, return;);
503 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
504 KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
505 struct dn_pkt_tag
*dn_tag
;
507 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
509 args
.fwa_pf_rule
= dn_tag
->dn_pf_rule
;
511 m_tag_delete(m
, tag
);
514 if (args
.fwa_pf_rule
) {
515 ip6
= mtod(m
, struct ip6_hdr
*); /* In case PF got disabled */
519 #endif /* DUMMYNET */
522 * No need to proccess packet twice if we've
525 inject_ipfref
= ipf_get_inject_filter(m
);
526 if (inject_ipfref
!= 0) {
527 ip6
= mtod(m
, struct ip6_hdr
*);
536 * should the inner packet be considered authentic?
537 * see comment in ah4_input().
540 m
->m_flags
&= ~M_AUTHIPHDR
;
541 m
->m_flags
&= ~M_AUTHIPDGM
;
546 * make sure we don't have onion peering information into m_aux.
553 if (m
->m_flags
& M_EXT
) {
555 ip6stat
.ip6s_mext2m
++;
557 ip6stat
.ip6s_mext1
++;
559 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
561 if (m
->m_flags
& M_LOOP
) {
562 ip6stat
.ip6s_m2m
[ifnet_index(lo_ifp
)]++; /* XXX */
563 } else if (m
->m_pkthdr
.rcvif
->if_index
< M2MMAX
)
564 ip6stat
.ip6s_m2m
[m
->m_pkthdr
.rcvif
->if_index
]++;
566 ip6stat
.ip6s_m2m
[0]++;
573 * Drop the packet if IPv6 operation is disabled on the IF;
574 * accessing the flag is done without acquiring nd_ifinfo lock
575 * for performance reasons.
577 lck_rw_lock_shared(nd_if_rwlock
);
578 if (m
->m_pkthdr
.rcvif
->if_index
< nd_ifinfo_indexlim
&&
579 (nd_ifinfo
[m
->m_pkthdr
.rcvif
->if_index
].flags
& ND6_IFF_IFDISABLED
)) {
580 lck_rw_done(nd_if_rwlock
);
583 lck_rw_done(nd_if_rwlock
);
585 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_receive
);
586 ip6stat
.ip6s_total
++;
588 #ifndef PULLDOWN_TEST
590 * L2 bridge code and some other code can return mbuf chain
591 * that does not conform to KAME requirement. too bad.
592 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
594 if (m
&& m
->m_next
!= NULL
&& m
->m_pkthdr
.len
< MCLBYTES
) {
597 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
600 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
601 MCLGET(n
, M_DONTWAIT
);
602 if ((n
->m_flags
& M_EXT
) == 0) {
610 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
611 n
->m_len
= m
->m_pkthdr
.len
;
615 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
),
619 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
621 inifp
= m
->m_pkthdr
.rcvif
;
622 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == 0) {
623 ip6stat
.ip6s_toosmall
++;
624 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
629 ip6
= mtod(m
, struct ip6_hdr
*);
631 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
632 ip6stat
.ip6s_badvers
++;
633 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
637 ip6stat
.ip6s_nxthist
[ip6
->ip6_nxt
]++;
641 * Check with the firewall...
643 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
645 /* If ipfw says divert, we have to just drop packet */
646 /* use port as a dummy argument */
647 if ((*ip6_fw_chk_ptr
)(&ip6
, NULL
, &port
, &m
)) {
657 * Check against address spoofing/corruption.
659 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
) ||
660 IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_dst
)) {
662 * XXX: "badscope" is not very suitable for a multicast source.
664 ip6stat
.ip6s_badscope
++;
665 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
668 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
) &&
669 !(m
->m_flags
& M_LOOP
)) {
671 * In this case, the packet should come from the loopback
672 * interface. However, we cannot just check the if_flags,
673 * because ip6_mloopback() passes the "actual" interface
674 * as the outgoing/incoming interface.
676 ip6stat
.ip6s_badscope
++;
677 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
682 * The following check is not documented in specs. A malicious
683 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
684 * and bypass security checks (act as if it was from 127.0.0.1 by using
685 * IPv6 src ::ffff:127.0.0.1). Be cautious.
687 * This check chokes if we are in an SIIT cloud. As none of BSDs
688 * support IPv4-less kernel compilation, we cannot support SIIT
689 * environment at all. So, it makes more sense for us to reject any
690 * malicious packets for non-SIIT environment, than try to do a
691 * partial support for SIIT environment.
693 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
694 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
695 ip6stat
.ip6s_badscope
++;
696 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
701 * Reject packets with IPv4 compatible addresses (auto tunnel).
703 * The code forbids auto tunnel relay case in RFC1933 (the check is
704 * stronger than RFC1933). We may want to re-enable it if mech-xx
705 * is revised to forbid relaying case.
707 if (IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_src
) ||
708 IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_dst
)) {
709 ip6stat
.ip6s_badscope
++;
710 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
716 * Naively assume we can attribute inbound data to the route we would
717 * use to send to this destination. Asymetric routing breaks this
718 * assumption, but it still allows us to account for traffic from
719 * a remote node in the routing table.
720 * this has a very significant performance impact so we bypass
721 * if nstat_collect is disabled. We may also bypass if the
722 * protocol is tcp in the future because tcp will have a route that
723 * we can use to attribute the data to. That does mean we would not
724 * account for forwarded tcp traffic.
727 struct rtentry
*rte
=
728 ifnet_cached_rtlookup_inet6(m
->m_pkthdr
.rcvif
,
731 nstat_route_rx(rte
, 1, m
->m_pkthdr
.len
, 0);
740 /* Invoke inbound packet filter */
744 error
= pf_af_hook(m
->m_pkthdr
.rcvif
, NULL
, &m
, AF_INET6
, TRUE
, &args
);
746 error
= pf_af_hook(m
->m_pkthdr
.rcvif
, NULL
, &m
, AF_INET6
, TRUE
, NULL
);
748 if (error
!= 0 || m
== NULL
) {
750 panic("%s: unexpected packet %p\n", __func__
, m
);
753 /* Already freed by callee */
756 ip6
= mtod(m
, struct ip6_hdr
*);
760 /* drop packets if interface ID portion is already filled */
761 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
762 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
) &&
763 ip6
->ip6_src
.s6_addr16
[1]) {
764 ip6stat
.ip6s_badscope
++;
767 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
768 ip6
->ip6_dst
.s6_addr16
[1]) {
769 ip6stat
.ip6s_badscope
++;
774 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
775 ip6
->ip6_src
.s6_addr16
[1]
776 = htons(m
->m_pkthdr
.rcvif
->if_index
);
777 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
778 ip6
->ip6_dst
.s6_addr16
[1]
779 = htons(m
->m_pkthdr
.rcvif
->if_index
);
784 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
785 struct in6_multi
*in6m
= NULL
;
786 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
788 in6_ifstat_inc(ifp
, ifs6_in_mcast
);
790 * See if we belong to the destination multicast group on the
793 in6_multihead_lock_shared();
794 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, in6m
);
795 in6_multihead_lock_done();
799 } else if (!nd6_prproxy
804 ip6stat
.ip6s_notmember
++;
805 ip6stat
.ip6s_cantforward
++;
806 in6_ifstat_inc(ifp
, ifs6_in_discard
);
816 dst6
= (struct sockaddr_in6
*)&ip6_forward_rt
.ro_dst
;
817 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
818 dst6
->sin6_family
= AF_INET6
;
819 dst6
->sin6_addr
= ip6
->ip6_dst
;
821 rtalloc_scoped_ign((struct route
*)&ip6_forward_rt
,
822 RTF_PRCLONING
, IFSCOPE_NONE
);
823 if (ip6_forward_rt
.ro_rt
!= NULL
)
824 RT_LOCK(ip6_forward_rt
.ro_rt
);
826 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
829 * Accept the packet if the forwarding interface to the destination
830 * according to the routing table is the loopback interface,
831 * unless the associated route has a gateway.
832 * Note that this approach causes to accept a packet if there is a
833 * route to the loopback interface for the destination of the packet.
834 * But we think it's even useful in some situations, e.g. when using
835 * a special daemon which wants to intercept the packet.
837 * XXX: some OSes automatically make a cloned route for the destination
838 * of an outgoing packet. If the outgoing interface of the packet
839 * is a loopback one, the kernel would consider the packet to be
840 * accepted, even if we have no such address assinged on the interface.
841 * We check the cloned flag of the route entry to reject such cases,
842 * assuming that route entries for our own addresses are not made by
843 * cloning (it should be true because in6_addloop explicitly installs
844 * the host route). However, we might have to do an explicit check
845 * while it would be less efficient. Or, should we rather install a
846 * reject route for such a case?
848 if (ip6_forward_rt
.ro_rt
!= NULL
&&
849 (ip6_forward_rt
.ro_rt
->rt_flags
&
850 (RTF_HOST
|RTF_GATEWAY
)) == RTF_HOST
&&
852 !(ip6_forward_rt
.ro_rt
->rt_flags
& RTF_WASCLONED
) &&
856 * The check below is redundant since the comparison of
857 * the destination and the key of the rtentry has
858 * already done through looking up the routing table.
860 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
861 &rt6_key(ip6_forward_rt
.ro_rt
)->sin6_addr
)
863 ip6_forward_rt
.ro_rt
->rt_ifp
->if_type
== IFT_LOOP
) {
864 ia6
= (struct in6_ifaddr
*)ip6_forward_rt
.ro_rt
->rt_ifa
;
867 * record address information into m_aux.
869 (void)ip6_setdstifaddr(m
, ia6
);
872 * packets to a tentative, duplicated, or somehow invalid
873 * address must not be accepted.
875 RT_CONVERT_LOCK(ip6_forward_rt
.ro_rt
); /* just in case */
876 IFA_LOCK_SPIN(&ia6
->ia_ifa
);
877 if (!(ia6
->ia6_flags
& IN6_IFF_NOTREADY
)) {
878 IFA_UNLOCK(&ia6
->ia_ifa
);
879 /* this address is ready */
881 deliverifp
= ia6
->ia_ifp
; /* correct? */
882 /* Count the packet in the ip address stats */
884 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
888 IFA_UNLOCK(&ia6
->ia_ifa
);
889 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
890 /* address is not ready, so discard the packet. */
892 "ip6_input: packet to an unready address %s->%s\n",
893 ip6_sprintf(&ip6
->ip6_src
),
894 ip6_sprintf(&ip6
->ip6_dst
)));
899 if (ip6_forward_rt
.ro_rt
!= NULL
)
900 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
903 * Now there is no reason to process the packet if it's not our own
904 * and we're not a router.
906 if (!ip6_forwarding
) {
907 ip6stat
.ip6s_cantforward
++;
908 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
914 * record address information into m_aux, if we don't have one yet.
915 * note that we are unable to record it, if the address is not listed
916 * as our interface address (e.g. multicast addresses, etc.)
918 if (deliverifp
&& (ia6
= ip6_getdstifaddr(m
)) == NULL
) {
919 ia6
= in6_ifawithifp(deliverifp
, &ip6
->ip6_dst
);
921 if (!ip6_setdstifaddr(m
, ia6
)) {
923 * XXX maybe we should drop the packet here,
924 * as we could not provide enough information
925 * to the upper layers.
928 IFA_REMREF(&ia6
->ia_ifa
);
934 IFA_REMREF(&ia6
->ia_ifa
);
939 * Process Hop-by-Hop options header if it's contained.
940 * m may be modified in ip6_hopopts_input().
941 * If a JumboPayload option is included, plen will also be modified.
943 plen
= (u_int32_t
)ntohs(ip6
->ip6_plen
);
944 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
947 if (ip6_hopopts_input(&plen
, &rtalert
, &m
, &off
)) {
948 #if 0 /*touches NULL pointer*/
949 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
951 goto done
; /* m have already been freed */
955 ip6
= mtod(m
, struct ip6_hdr
*);
958 * if the payload length field is 0 and the next header field
959 * indicates Hop-by-Hop Options header, then a Jumbo Payload
960 * option MUST be included.
962 if (ip6
->ip6_plen
== 0 && plen
== 0) {
964 * Note that if a valid jumbo payload option is
965 * contained, ip6_hopopts_input() must set a valid
966 * (non-zero) payload length to the variable plen.
968 ip6stat
.ip6s_badoptions
++;
969 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
970 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
971 icmp6_error(m
, ICMP6_PARAM_PROB
,
972 ICMP6_PARAMPROB_HEADER
,
973 (caddr_t
)&ip6
->ip6_plen
- (caddr_t
)ip6
);
976 #ifndef PULLDOWN_TEST
977 /* ip6_hopopts_input() ensures that mbuf is contiguous */
978 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
980 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
, sizeof(struct ip6_hdr
),
981 sizeof(struct ip6_hbh
));
983 ip6stat
.ip6s_tooshort
++;
990 * If we are acting as a router and the packet contains a
991 * router alert option, see if we know the option value.
992 * Currently, we only support the option value for MLD, in which
993 * case we should pass the packet to the multicast routing
996 if (rtalert
!= ~0 && ip6_forwarding
) {
998 case IP6OPT_RTALERT_MLD
:
1003 * RFC2711 requires unrecognized values must be
1013 * Check that the amount of data in the buffers
1014 * is as at least much as the IPv6 header would have us expect.
1015 * Trim mbufs if longer than we expect.
1016 * Drop packet if shorter than we expect.
1018 if (m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
) < plen
) {
1019 ip6stat
.ip6s_tooshort
++;
1020 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_truncated
);
1023 if (m
->m_pkthdr
.len
> sizeof(struct ip6_hdr
) + plen
) {
1024 if (m
->m_len
== m
->m_pkthdr
.len
) {
1025 m
->m_len
= sizeof(struct ip6_hdr
) + plen
;
1026 m
->m_pkthdr
.len
= sizeof(struct ip6_hdr
) + plen
;
1028 m_adj(m
, sizeof(struct ip6_hdr
) + plen
- m
->m_pkthdr
.len
);
1032 * Forward if desirable.
1034 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1036 * If we are acting as a multicast router, all
1037 * incoming multicast packets are passed to the
1038 * kernel-level multicast forwarding function.
1039 * The packet is returned (relatively) intact; if
1040 * ip6_mforward() returns a non-zero value, the packet
1041 * must be discarded, else it may be accepted below.
1044 if (ip6_mrouter
&& ip6_mforward(ip6
, m
->m_pkthdr
.rcvif
, m
)) {
1045 ip6stat
.ip6s_cantforward
++;
1049 if (!ours
&& nd6_prproxy
) {
1051 * If this isn't for us, this might be a Neighbor
1052 * Solicitation (dst is solicited-node multicast)
1053 * against an address in one of the proxied prefixes;
1054 * if so, claim the packet and let icmp6_input()
1057 ours
= nd6_prproxy_isours(m
, ip6
, NULL
, IFSCOPE_NONE
);
1059 (m
->m_pkthdr
.aux_flags
& MAUXF_PROXY_DST
));
1065 * The unicast forwarding function might return the packet
1066 * if we are proxying prefix(es), and if the packet is an
1067 * ICMPv6 packet that has failed the zone checks, but is
1068 * targetted towards a proxied address (this is optimized by
1069 * way of RTF_PROXY test.) If so, claim the packet as ours
1070 * and let icmp6_input() handle the rest. The packet's hop
1071 * limit value is kept intact (it's not decremented). This
1072 * is for supporting Neighbor Unreachability Detection between
1073 * proxied nodes on different links (src is link-local, dst
1074 * is target address.)
1076 if ((m
= ip6_forward(m
, &ip6_forward_rt
, 0)) == NULL
)
1078 VERIFY(ip6_forward_rt
.ro_rt
!= NULL
);
1079 VERIFY(m
->m_pkthdr
.aux_flags
& MAUXF_PROXY_DST
);
1080 deliverifp
= ip6_forward_rt
.ro_rt
->rt_ifp
;
1084 ip6
= mtod(m
, struct ip6_hdr
*);
1087 * Malicious party may be able to use IPv4 mapped addr to confuse
1088 * tcp/udp stack and bypass security checks (act as if it was from
1089 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
1091 * For SIIT end node behavior, you may want to disable the check.
1092 * However, you will become vulnerable to attacks using IPv4 mapped
1095 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
1096 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
1097 ip6stat
.ip6s_badscope
++;
1098 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
1103 * Tell launch routine the next header
1105 ip6stat
.ip6s_delivered
++;
1106 in6_ifstat_inc(deliverifp
, ifs6_in_deliver
);
1112 * Perform IP header alignment fixup again, if needed. Note that
1113 * we do it once for the outermost protocol, and we assume each
1114 * protocol handler wouldn't mess with the alignment afterwards.
1116 IP6_HDR_ALIGNMENT_FIXUP(m
, m
->m_pkthdr
.rcvif
, return;);
1118 while (nxt
!= IPPROTO_DONE
) {
1119 struct ipfilter
*filter
;
1120 int (*pr_input
)(struct mbuf
**, int *, int);
1122 if (ip6_hdrnestlimit
&& (++nest
> ip6_hdrnestlimit
)) {
1123 ip6stat
.ip6s_toomanyhdr
++;
1128 * protection against faulty packet - there should be
1129 * more sanity checks in header chain processing.
1131 if (m
->m_pkthdr
.len
< off
) {
1132 ip6stat
.ip6s_tooshort
++;
1133 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_truncated
);
1140 * enforce IPsec policy checking if we are seeing last header.
1141 * note that we do not visit this with protocols with pcb layer
1142 * code - like udp/tcp/raw ip.
1144 if ((ipsec_bypass
== 0) &&
1145 (ip6_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0) {
1146 if (ipsec6_in_reject(m
, NULL
)) {
1147 IPSEC_STAT_INCREMENT(ipsec6stat
.in_polvio
);
1156 if (!TAILQ_EMPTY(&ipv6_filters
)) {
1158 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
1160 if ((struct ipfilter
*)inject_ipfref
==
1163 } else if (filter
->ipf_filter
.ipf_input
) {
1166 result
= filter
->ipf_filter
.ipf_input(
1167 filter
->ipf_filter
.cookie
,
1168 (mbuf_t
*)&m
, off
, nxt
);
1169 if (result
== EJUSTRETURN
) {
1182 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1183 struct ip6_hdr
*, ip6
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
1184 struct ip
*, NULL
, struct ip6_hdr
*, ip6
);
1186 if ((pr_input
= ip6_protox
[nxt
]->pr_input
) == NULL
) {
1190 } else if (!(ip6_protox
[nxt
]->pr_flags
& PR_PROTOLOCK
)) {
1191 lck_mtx_lock(inet6_domain_mutex
);
1192 nxt
= pr_input(&m
, &off
, nxt
);
1193 lck_mtx_unlock(inet6_domain_mutex
);
1195 nxt
= pr_input(&m
, &off
, nxt
);
1199 if (ip6_forward_rt
.ro_rt
!= NULL
)
1200 rtfree(ip6_forward_rt
.ro_rt
);
1208 * set/grab in6_ifaddr correspond to IPv6 destination address.
1209 * XXX backward compatibility wrapper
1211 static struct ip6aux
*
1212 ip6_setdstifaddr(struct mbuf
*m
, struct in6_ifaddr
*ia6
)
1219 IFA_ADDREF(&ia6
->ia_ifa
);
1220 if (n
->ip6a_dstia6
!= NULL
)
1221 IFA_REMREF(&n
->ip6a_dstia6
->ia_ifa
);
1222 n
->ip6a_dstia6
= ia6
;
1224 return (struct ip6aux
*)n
; /* NULL if failed to set */
1235 if (n
->ip6a_dstia6
!= NULL
)
1236 IFA_ADDREF(&n
->ip6a_dstia6
->ia_ifa
);
1237 return (n
->ip6a_dstia6
);
1243 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1244 * included, the real payload length will be stored in plenp.
1247 ip6_hopopts_input(uint32_t *plenp
, uint32_t *rtalertp
, struct mbuf
**mp
,
1250 struct mbuf
*m
= *mp
;
1251 int off
= *offp
, hbhlen
;
1252 struct ip6_hbh
*hbh
;
1255 /* validation of the length of the header */
1256 #ifndef PULLDOWN_TEST
1257 IP6_EXTHDR_CHECK(m
, off
, sizeof(*hbh
), return -1);
1258 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1259 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1261 IP6_EXTHDR_CHECK(m
, off
, hbhlen
, return -1);
1262 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1264 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
,
1265 sizeof(struct ip6_hdr
), sizeof(struct ip6_hbh
));
1267 ip6stat
.ip6s_tooshort
++;
1270 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1271 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
, sizeof(struct ip6_hdr
),
1274 ip6stat
.ip6s_tooshort
++;
1279 hbhlen
-= sizeof(struct ip6_hbh
);
1280 opt
= (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
);
1282 if (ip6_process_hopopts(m
, (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
),
1283 hbhlen
, rtalertp
, plenp
) < 0)
1292 * Search header for all Hop-by-hop options and process each option.
1293 * This function is separate from ip6_hopopts_input() in order to
1294 * handle a case where the sending node itself process its hop-by-hop
1295 * options header. In such a case, the function is called from ip6_output().
1297 * The function assumes that hbh header is located right after the IPv6 header
1298 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1299 * opthead + hbhlen is located in continuous memory region.
1302 ip6_process_hopopts(m
, opthead
, hbhlen
, rtalertp
, plenp
)
1306 u_int32_t
*rtalertp
;
1309 struct ip6_hdr
*ip6
;
1311 u_int8_t
*opt
= opthead
;
1312 u_int16_t rtalert_val
;
1313 u_int32_t jumboplen
;
1314 const int erroff
= sizeof(struct ip6_hdr
) + sizeof(struct ip6_hbh
);
1316 for (; hbhlen
> 0; hbhlen
-= optlen
, opt
+= optlen
) {
1322 if (hbhlen
< IP6OPT_MINLEN
) {
1323 ip6stat
.ip6s_toosmall
++;
1326 optlen
= *(opt
+ 1) + 2;
1328 case IP6OPT_ROUTER_ALERT
:
1329 /* XXX may need check for alignment */
1330 if (hbhlen
< IP6OPT_RTALERT_LEN
) {
1331 ip6stat
.ip6s_toosmall
++;
1334 if (*(opt
+ 1) != IP6OPT_RTALERT_LEN
- 2) {
1336 icmp6_error(m
, ICMP6_PARAM_PROB
,
1337 ICMP6_PARAMPROB_HEADER
,
1338 erroff
+ opt
+ 1 - opthead
);
1341 optlen
= IP6OPT_RTALERT_LEN
;
1342 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&rtalert_val
, 2);
1343 *rtalertp
= ntohs(rtalert_val
);
1346 /* XXX may need check for alignment */
1347 if (hbhlen
< IP6OPT_JUMBO_LEN
) {
1348 ip6stat
.ip6s_toosmall
++;
1351 if (*(opt
+ 1) != IP6OPT_JUMBO_LEN
- 2) {
1353 icmp6_error(m
, ICMP6_PARAM_PROB
,
1354 ICMP6_PARAMPROB_HEADER
,
1355 erroff
+ opt
+ 1 - opthead
);
1358 optlen
= IP6OPT_JUMBO_LEN
;
1361 * IPv6 packets that have non 0 payload length
1362 * must not contain a jumbo payload option.
1364 ip6
= mtod(m
, struct ip6_hdr
*);
1365 if (ip6
->ip6_plen
) {
1366 ip6stat
.ip6s_badoptions
++;
1367 icmp6_error(m
, ICMP6_PARAM_PROB
,
1368 ICMP6_PARAMPROB_HEADER
,
1369 erroff
+ opt
- opthead
);
1374 * We may see jumbolen in unaligned location, so
1375 * we'd need to perform bcopy().
1377 bcopy(opt
+ 2, &jumboplen
, sizeof(jumboplen
));
1378 jumboplen
= (u_int32_t
)htonl(jumboplen
);
1382 * if there are multiple jumbo payload options,
1383 * *plenp will be non-zero and the packet will be
1385 * the behavior may need some debate in ipngwg -
1386 * multiple options does not make sense, however,
1387 * there's no explicit mention in specification.
1390 ip6stat
.ip6s_badoptions
++;
1391 icmp6_error(m
, ICMP6_PARAM_PROB
,
1392 ICMP6_PARAMPROB_HEADER
,
1393 erroff
+ opt
+ 2 - opthead
);
1399 * jumbo payload length must be larger than 65535.
1401 if (jumboplen
<= IPV6_MAXPACKET
) {
1402 ip6stat
.ip6s_badoptions
++;
1403 icmp6_error(m
, ICMP6_PARAM_PROB
,
1404 ICMP6_PARAMPROB_HEADER
,
1405 erroff
+ opt
+ 2 - opthead
);
1411 default: /* unknown option */
1412 if (hbhlen
< IP6OPT_MINLEN
) {
1413 ip6stat
.ip6s_toosmall
++;
1416 optlen
= ip6_unknown_opt(opt
, m
,
1417 erroff
+ opt
- opthead
);
1434 * Unknown option processing.
1435 * The third argument `off' is the offset from the IPv6 header to the option,
1436 * which is necessary if the IPv6 header the and option header and IPv6 header
1437 * is not continuous in order to return an ICMPv6 error.
1440 ip6_unknown_opt(uint8_t *optp
, struct mbuf
*m
, int off
)
1442 struct ip6_hdr
*ip6
;
1444 switch (IP6OPT_TYPE(*optp
)) {
1445 case IP6OPT_TYPE_SKIP
: /* ignore the option */
1446 return((int)*(optp
+ 1));
1447 case IP6OPT_TYPE_DISCARD
: /* silently discard */
1450 case IP6OPT_TYPE_FORCEICMP
: /* send ICMP even if multicasted */
1451 ip6stat
.ip6s_badoptions
++;
1452 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_OPTION
, off
);
1454 case IP6OPT_TYPE_ICMP
: /* send ICMP if not multicasted */
1455 ip6stat
.ip6s_badoptions
++;
1456 ip6
= mtod(m
, struct ip6_hdr
*);
1457 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
1458 (m
->m_flags
& (M_BCAST
|M_MCAST
)))
1461 icmp6_error(m
, ICMP6_PARAM_PROB
,
1462 ICMP6_PARAMPROB_OPTION
, off
);
1466 m_freem(m
); /* XXX: NOTREACHED */
1471 * Create the "control" list for this pcb.
1472 * These functions will not modify mbuf chain at all.
1474 * With KAME mbuf chain restriction:
1475 * The routine will be called from upper layer handlers like tcp6_input().
1476 * Thus the routine assumes that the caller (tcp6_input) have already
1477 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1478 * very first mbuf on the mbuf chain.
1480 * ip6_savecontrol_v4 will handle those options that are possible to be
1481 * set on a v4-mapped socket.
1482 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1483 * options and handle the v6-only ones itself.
1486 ip6_savecontrol_v4(struct inpcb
*inp
, struct mbuf
*m
, struct mbuf
**mp
,
1489 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1491 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0) {
1495 mp
= sbcreatecontrol_mbuf((caddr_t
) &tv
, sizeof(tv
),
1496 SCM_TIMESTAMP
, SOL_SOCKET
, mp
);
1500 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
1503 time
= mach_absolute_time();
1504 mp
= sbcreatecontrol_mbuf((caddr_t
) &time
, sizeof(time
),
1505 SCM_TIMESTAMP_MONOTONIC
, SOL_SOCKET
, mp
);
1510 if ((inp
->inp_socket
->so_flags
& SOF_RECV_TRAFFIC_CLASS
) != 0) {
1511 int tc
= m_get_traffic_class(m
);
1513 mp
= sbcreatecontrol_mbuf((caddr_t
) &tc
, sizeof(tc
),
1514 SO_TRAFFIC_CLASS
, SOL_SOCKET
, mp
);
1519 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
1525 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1526 /* RFC 2292 sec. 5 */
1527 if ((inp
->inp_flags
& IN6P_PKTINFO
) != 0) {
1528 struct in6_pktinfo pi6
;
1530 bcopy(&ip6
->ip6_dst
, &pi6
.ipi6_addr
, sizeof(struct in6_addr
));
1531 in6_clearscope(&pi6
.ipi6_addr
); /* XXX */
1533 (m
&& m
->m_pkthdr
.rcvif
) ? m
->m_pkthdr
.rcvif
->if_index
: 0;
1535 mp
= sbcreatecontrol_mbuf((caddr_t
) &pi6
,
1536 sizeof(struct in6_pktinfo
),
1537 IS2292(inp
, IPV6_2292PKTINFO
, IPV6_PKTINFO
), IPPROTO_IPV6
, mp
);
1542 if ((inp
->inp_flags
& IN6P_HOPLIMIT
) != 0) {
1543 int hlim
= ip6
->ip6_hlim
& 0xff;
1545 mp
= sbcreatecontrol_mbuf((caddr_t
) &hlim
, sizeof(int),
1546 IS2292(inp
, IPV6_2292HOPLIMIT
, IPV6_HOPLIMIT
),
1558 ip6_savecontrol(struct inpcb
*in6p
, struct mbuf
*m
, struct mbuf
**mp
)
1561 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1565 np
= ip6_savecontrol_v4(in6p
, m
, mp
, &v4only
);
1573 if ((in6p
->inp_flags
& IN6P_TCLASS
) != 0) {
1577 flowinfo
= (u_int32_t
)ntohl(ip6
->ip6_flow
& IPV6_FLOWINFO_MASK
);
1580 tclass
= flowinfo
& 0xff;
1581 mp
= sbcreatecontrol_mbuf((caddr_t
) &tclass
, sizeof(tclass
),
1582 IPV6_TCLASS
, IPPROTO_IPV6
, mp
);
1588 * IPV6_HOPOPTS socket option. Recall that we required super-user
1589 * privilege for the option (see ip6_ctloutput), but it might be too
1590 * strict, since there might be some hop-by-hop options which can be
1591 * returned to normal user.
1592 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1594 if ((in6p
->inp_flags
& IN6P_HOPOPTS
) != 0) {
1596 * Check if a hop-by-hop options header is contatined in the
1597 * received packet, and if so, store the options as ancillary
1598 * data. Note that a hop-by-hop options header must be
1599 * just after the IPv6 header, which is assured through the
1600 * IPv6 input processing.
1602 ip6
= mtod(m
, struct ip6_hdr
*);
1603 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
1604 struct ip6_hbh
*hbh
;
1610 #ifndef PULLDOWN_TEST
1611 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
1612 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1614 ext
= ip6_pullexthdr(m
, sizeof(struct ip6_hdr
),
1617 ip6stat
.ip6s_tooshort
++;
1620 hbh
= mtod(ext
, struct ip6_hbh
*);
1621 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1622 if (hbhlen
!= ext
->m_len
) {
1624 ip6stat
.ip6s_tooshort
++;
1630 * XXX: We copy the whole header even if a
1631 * jumbo payload option is included, the option which
1632 * is to be removed before returning according to
1634 * Note: this constraint is removed in RFC3542
1636 mp
= sbcreatecontrol_mbuf((caddr_t
)hbh
, hbhlen
,
1637 IS2292(in6p
, IPV6_2292HOPOPTS
, IPV6_HOPOPTS
),
1649 if ((in6p
->inp_flags
& (IN6P_RTHDR
| IN6P_DSTOPTS
)) != 0) {
1650 int nxt
= ip6
->ip6_nxt
, off
= sizeof(struct ip6_hdr
);
1653 * Search for destination options headers or routing
1654 * header(s) through the header chain, and stores each
1655 * header as ancillary data.
1656 * Note that the order of the headers remains in
1657 * the chain of ancillary data.
1659 while (1) { /* is explicit loop prevention necessary? */
1660 struct ip6_ext
*ip6e
= NULL
;
1663 struct mbuf
*ext
= NULL
;
1667 * if it is not an extension header, don't try to
1668 * pull it from the chain.
1671 case IPPROTO_DSTOPTS
:
1672 case IPPROTO_ROUTING
:
1673 case IPPROTO_HOPOPTS
:
1674 case IPPROTO_AH
: /* is it possible? */
1680 #ifndef PULLDOWN_TEST
1681 if (off
+ sizeof(*ip6e
) > m
->m_len
)
1683 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + off
);
1684 if (nxt
== IPPROTO_AH
)
1685 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1687 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1688 if (off
+ elen
> m
->m_len
)
1691 ext
= ip6_pullexthdr(m
, off
, nxt
);
1693 ip6stat
.ip6s_tooshort
++;
1696 ip6e
= mtod(ext
, struct ip6_ext
*);
1697 if (nxt
== IPPROTO_AH
)
1698 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1700 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1701 if (elen
!= ext
->m_len
) {
1703 ip6stat
.ip6s_tooshort
++;
1709 case IPPROTO_DSTOPTS
:
1710 if (!(in6p
->inp_flags
& IN6P_DSTOPTS
))
1713 mp
= sbcreatecontrol_mbuf((caddr_t
)ip6e
, elen
,
1715 IPV6_2292DSTOPTS
, IPV6_DSTOPTS
),
1724 case IPPROTO_ROUTING
:
1725 if (!in6p
->inp_flags
& IN6P_RTHDR
)
1728 mp
= sbcreatecontrol_mbuf((caddr_t
)ip6e
, elen
,
1729 IS2292(in6p
, IPV6_2292RTHDR
, IPV6_RTHDR
),
1738 case IPPROTO_HOPOPTS
:
1739 case IPPROTO_AH
: /* is it possible? */
1744 * other cases have been filtered in the above.
1745 * none will visit this case. here we supply
1746 * the code just in case (nxt overwritten or
1756 /* proceed with the next header. */
1758 nxt
= ip6e
->ip6e_nxt
;
1770 ip6stat
.ip6s_pktdropcntrl
++;
1771 /* XXX increment a stat to show the failure */
1777 ip6_notify_pmtu(struct inpcb
*in6p
, struct sockaddr_in6
*dst
, u_int32_t
*mtu
)
1781 struct ip6_mtuinfo mtuctl
;
1783 so
= in6p
->inp_socket
;
1789 if (so
== NULL
) /* I believe this is impossible */
1790 panic("ip6_notify_pmtu: socket is NULL");
1793 bzero(&mtuctl
, sizeof(mtuctl
)); /* zero-clear for safety */
1794 mtuctl
.ip6m_mtu
= *mtu
;
1795 mtuctl
.ip6m_addr
= *dst
;
1796 if (sa6_recoverscope(&mtuctl
.ip6m_addr
, TRUE
))
1799 if ((m_mtu
= sbcreatecontrol((caddr_t
)&mtuctl
, sizeof(mtuctl
),
1800 IPV6_PATHMTU
, IPPROTO_IPV6
)) == NULL
)
1803 if (sbappendaddr(&so
->so_rcv
, (struct sockaddr
*)dst
, NULL
, m_mtu
, NULL
)
1806 /* XXX: should count statistics */
1815 * pull single extension header from mbuf chain. returns single mbuf that
1816 * contains the result, or NULL on error.
1818 static struct mbuf
*
1819 ip6_pullexthdr(m
, off
, nxt
)
1824 struct ip6_ext ip6e
;
1830 case IPPROTO_DSTOPTS
:
1831 case IPPROTO_ROUTING
:
1832 case IPPROTO_HOPOPTS
:
1833 case IPPROTO_AH
: /* is it possible? */
1836 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt
);
1840 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1841 if (nxt
== IPPROTO_AH
)
1842 elen
= (ip6e
.ip6e_len
+ 2) << 2;
1844 elen
= (ip6e
.ip6e_len
+ 1) << 3;
1846 MGET(n
, M_DONTWAIT
, MT_DATA
);
1847 if (n
&& elen
>= MLEN
) {
1848 MCLGET(n
, M_DONTWAIT
);
1849 if ((n
->m_flags
& M_EXT
) == 0) {
1858 if (elen
>= M_TRAILINGSPACE(n
)) {
1863 m_copydata(m
, off
, elen
, mtod(n
, caddr_t
));
1870 * Get pointer to the previous header followed by the header
1871 * currently processed.
1872 * XXX: This function supposes that
1873 * M includes all headers,
1874 * the next header field and the header length field of each header
1876 * the sum of each header length equals to OFF.
1877 * Because of these assumptions, this function must be called very
1878 * carefully. Moreover, it will not be used in the near future when
1879 * we develop `neater' mechanism to process extension headers.
1882 ip6_get_prevhdr(m
, off
)
1886 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1888 if (off
== sizeof(struct ip6_hdr
))
1889 return((char *) &ip6
->ip6_nxt
);
1892 struct ip6_ext
*ip6e
= NULL
;
1895 len
= sizeof(struct ip6_hdr
);
1897 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + len
);
1900 case IPPROTO_FRAGMENT
:
1901 len
+= sizeof(struct ip6_frag
);
1904 len
+= (ip6e
->ip6e_len
+ 2) << 2;
1907 len
+= (ip6e
->ip6e_len
+ 1) << 3;
1910 nxt
= ip6e
->ip6e_nxt
;
1913 return((char *) &ip6e
->ip6e_nxt
);
1920 * get next header offset. m will be retained.
1923 ip6_nexthdr(m
, off
, proto
, nxtp
)
1930 struct ip6_ext ip6e
;
1935 panic("ip6_nexthdr: m == NULL");
1936 if ((m
->m_flags
& M_PKTHDR
) == 0 || m
->m_pkthdr
.len
< off
)
1941 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6
))
1943 m_copydata(m
, off
, sizeof(ip6
), (caddr_t
)&ip6
);
1945 *nxtp
= ip6
.ip6_nxt
;
1949 case IPPROTO_FRAGMENT
:
1951 * terminate parsing if it is not the first fragment,
1952 * it does not make sense to parse through it.
1954 if (m
->m_pkthdr
.len
< off
+ sizeof(fh
))
1956 m_copydata(m
, off
, sizeof(fh
), (caddr_t
)&fh
);
1957 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1958 if (fh
.ip6f_offlg
& IP6F_OFF_MASK
)
1961 *nxtp
= fh
.ip6f_nxt
;
1962 off
+= sizeof(struct ip6_frag
);
1966 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
))
1968 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1970 *nxtp
= ip6e
.ip6e_nxt
;
1971 off
+= (ip6e
.ip6e_len
+ 2) << 2;
1974 case IPPROTO_HOPOPTS
:
1975 case IPPROTO_ROUTING
:
1976 case IPPROTO_DSTOPTS
:
1977 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
))
1979 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1981 *nxtp
= ip6e
.ip6e_nxt
;
1982 off
+= (ip6e
.ip6e_len
+ 1) << 3;
1987 case IPPROTO_IPCOMP
:
1999 * get offset for the last header in the chain. m will be kept untainted.
2002 ip6_lasthdr(m
, off
, proto
, nxtp
)
2016 newoff
= ip6_nexthdr(m
, off
, proto
, nxtp
);
2019 else if (newoff
< off
)
2020 return -1; /* invalid */
2021 else if (newoff
== off
)
2030 ip6_addaux(struct mbuf
*m
)
2034 /* Check if one is already allocated */
2035 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2036 KERNEL_TAG_TYPE_INET6
, NULL
);
2038 /* Allocate a tag */
2039 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_INET6
,
2040 sizeof (struct ip6aux
), M_DONTWAIT
, m
);
2042 /* Attach it to the mbuf */
2044 m_tag_prepend(m
, tag
);
2048 return (tag
? (struct ip6aux
*)(tag
+ 1) : NULL
);
2052 ip6_findaux(struct mbuf
*m
)
2056 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2057 KERNEL_TAG_TYPE_INET6
, NULL
);
2059 return (tag
? (struct ip6aux
*)(tag
+ 1) : NULL
);
2063 ip6_delaux(struct mbuf
*m
)
2067 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
2068 KERNEL_TAG_TYPE_INET6
, NULL
);
2070 m_tag_delete(m
, tag
);
2075 * Called by m_tag_free().
2078 ip6_destroyaux(struct ip6aux
*n
)
2080 if (n
->ip6a_dstia6
!= NULL
) {
2081 IFA_REMREF(&n
->ip6a_dstia6
->ia_ifa
);
2082 n
->ip6a_dstia6
= NULL
;
2087 * Called by m_tag_copy()
2090 ip6_copyaux(struct ip6aux
*src
, struct ip6aux
*dst
)
2092 bcopy(src
, dst
, sizeof (*dst
));
2093 if (dst
->ip6a_dstia6
!= NULL
)
2094 IFA_ADDREF(&dst
->ip6a_dstia6
->ia_ifa
);
2098 * System control for IP6
2101 u_char inet6ctlerrmap
[PRC_NCMDS
] = {
2103 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2104 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2105 EMSGSIZE
, EHOSTUNREACH
, 0, 0,