2 * Copyright (c) 2003-2011 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>
124 #include <netinet/in.h>
125 #include <netinet/in_systm.h>
127 #include <netinet/ip.h>
128 #include <netinet/ip_icmp.h>
130 #include <netinet/ip6.h>
131 #include <netinet6/in6_var.h>
132 #include <netinet6/ip6_var.h>
133 #include <netinet/in_pcb.h>
134 #include <netinet/icmp6.h>
135 #include <netinet6/in6_ifattach.h>
136 #include <netinet6/nd6.h>
137 #include <netinet6/scope6_var.h>
138 #include <mach/sdt.h>
141 #include <netinet6/ipsec.h>
143 #include <netinet6/ipsec6.h>
145 extern int ipsec_bypass
;
148 #include <netinet6/ip6_fw.h>
150 #include <netinet/kpi_ipfilter_var.h>
152 #include <netinet6/ip6protosw.h>
154 /* we need it for NLOOP. */
158 #include <net/net_osdep.h>
161 #include <net/pfvar.h>
164 extern struct domain inet6domain
;
165 extern struct ip6protosw inet6sw
[];
167 struct ip6protosw
* ip6_protox
[IPPROTO_MAX
];
168 static int ip6qmaxlen
= IFQ_MAXLEN
;
170 static lck_grp_attr_t
*in6_ifaddr_rwlock_grp_attr
;
171 static lck_grp_t
*in6_ifaddr_rwlock_grp
;
172 static lck_attr_t
*in6_ifaddr_rwlock_attr
;
173 decl_lck_rw_data(, in6_ifaddr_rwlock
);
175 /* Protected by in6_ifaddr_rwlock */
176 struct in6_ifaddr
*in6_ifaddrs
= NULL
;
178 int ip6_forward_srcrt
; /* XXX */
179 int ip6_sourcecheck
; /* XXX */
180 int ip6_sourcecheck_interval
; /* XXX */
181 const int int6intrq_present
= 1;
183 int ip6_ours_check_algorithm
;
184 int in6_init2done
= 0;
185 int in6_init_done
= 0;
187 #define _CASSERT(x) \
188 switch (0) { case 0: case (x): ; }
189 #define IN6_IFSTAT_REQUIRE_ALIGNED_64(f) \
190 _CASSERT(!(offsetof(struct in6_ifstat, f) % sizeof (uint64_t)))
191 #define ICMP6_IFSTAT_REQUIRE_ALIGNED_64(f) \
192 _CASSERT(!(offsetof(struct icmp6_ifstat, f) % sizeof (uint64_t)))
196 ip6_fw_chk_t
*ip6_fw_chk_ptr
;
197 ip6_fw_ctl_t
*ip6_fw_ctl_ptr
;
198 int ip6_fw_enable
= 1;
201 struct ip6stat ip6stat
;
204 struct ifqueue ip6intrq
;
205 decl_lck_mtx_data(, ip6_init_mutex
);
206 lck_mtx_t
*dad6_mutex
;
207 lck_mtx_t
*nd6_mutex
;
208 lck_mtx_t
*prefix6_mutex
;
209 lck_mtx_t
*scope6_mutex
;
210 #ifdef ENABLE_ADDRSEL
211 lck_mtx_t
*addrsel_mutex
;
213 decl_lck_rw_data(, in6_ifs_rwlock
);
214 decl_lck_rw_data(, icmp6_ifs_rwlock
);
215 lck_attr_t
*ip6_mutex_attr
;
216 lck_grp_t
*ip6_mutex_grp
;
217 lck_grp_attr_t
*ip6_mutex_grp_attr
;
218 extern lck_mtx_t
*inet6_domain_mutex
;
220 extern int loopattach_done
;
221 extern void addrsel_policy_init(void);
223 static void ip6_init2(void *);
224 static struct ip6aux
*ip6_setdstifaddr(struct mbuf
*, struct in6_ifaddr
*);
226 static int ip6_hopopts_input(u_int32_t
*, u_int32_t
*, struct mbuf
**, int *);
228 static struct mbuf
*ip6_pullexthdr(struct mbuf
*, size_t, int);
232 void gifattach(void);
233 void faithattach(void);
234 void stfattach(void);
237 extern lck_mtx_t
*domain_proto_mtx
;
239 SYSCTL_DECL(_net_inet6_ip6
);
241 int ip6_doscopedroute
= 1;
242 SYSCTL_INT(_net_inet6_ip6
, OID_AUTO
, scopedroute
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
243 &ip6_doscopedroute
, 0, "Enable IPv6 scoped routing");
247 __unused protocol_family_t protocol
,
254 * IP6 initialization: fill in IP6 protocol switch table.
255 * All protocols not implemented in kernel go to raw IP6 protocol handler.
260 struct ip6protosw
*pr
;
264 PE_parse_boot_argn("net.inet6.ip6.scopedroute", &ip6_doscopedroute
,
265 sizeof (ip6_doscopedroute
));
268 if (sizeof(struct protosw
) != sizeof(struct ip6protosw
))
269 panic("sizeof(protosw) != sizeof(ip6protosw)");
271 pr
= (struct ip6protosw
*)pffindproto_locked(PF_INET6
, IPPROTO_RAW
, SOCK_RAW
);
274 for (i
= 0; i
< IPPROTO_MAX
; i
++)
276 for (pr
= (struct ip6protosw
*)inet6domain
.dom_protosw
; pr
; pr
= pr
->pr_next
) {
277 if(!(pr
->pr_domain
)) continue; /* If uninitialized, skip */
278 if (pr
->pr_domain
->dom_family
== PF_INET6
&&
279 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
) {
280 ip6_protox
[pr
->pr_protocol
] = pr
;
284 ip6_mutex_grp_attr
= lck_grp_attr_alloc_init();
286 ip6_mutex_grp
= lck_grp_alloc_init("ip6", ip6_mutex_grp_attr
);
287 ip6_mutex_attr
= lck_attr_alloc_init();
289 if ((dad6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
290 panic("ip6_init: can't alloc dad6_mutex\n");
292 if ((nd6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
293 panic("ip6_init: can't alloc nd6_mutex\n");
296 if ((prefix6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
297 panic("ip6_init: can't alloc prefix6_mutex\n");
300 if ((scope6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
301 panic("ip6_init: can't alloc scope6_mutex\n");
304 #ifdef ENABLE_ADDRSEL
305 if ((addrsel_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
306 panic("ip6_init: can't alloc addrsel_mutex\n");
310 lck_rw_init(&in6_ifs_rwlock
, ip6_mutex_grp
, ip6_mutex_attr
);
311 lck_rw_init(&icmp6_ifs_rwlock
, ip6_mutex_grp
, ip6_mutex_attr
);
312 lck_mtx_init(&ip6_init_mutex
, ip6_mutex_grp
, ip6_mutex_attr
);
314 inet6domain
.dom_flags
= DOM_REENTRANT
;
316 ip6intrq
.ifq_maxlen
= ip6qmaxlen
;
318 in6_ifaddr_rwlock_grp_attr
= lck_grp_attr_alloc_init();
319 in6_ifaddr_rwlock_grp
= lck_grp_alloc_init("in6_ifaddr_rwlock",
320 in6_ifaddr_rwlock_grp_attr
);
321 in6_ifaddr_rwlock_attr
= lck_attr_alloc_init();
322 lck_rw_init(&in6_ifaddr_rwlock
, in6_ifaddr_rwlock_grp
,
323 in6_ifaddr_rwlock_attr
);
325 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_receive
);
326 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_hdrerr
);
327 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_toobig
);
328 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_noroute
);
329 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_addrerr
);
330 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_protounknown
);
331 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_truncated
);
332 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_discard
);
333 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_deliver
);
334 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_forward
);
335 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_request
);
336 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_discard
);
337 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragok
);
338 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragfail
);
339 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragcreat
);
340 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_reqd
);
341 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_ok
);
342 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_fail
);
343 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mcast
);
344 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mcast
);
346 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_msg
);
347 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_error
);
348 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_dstunreach
);
349 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_adminprohib
);
350 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_timeexceed
);
351 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_paramprob
);
352 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_pkttoobig
);
353 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echo
);
354 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echoreply
);
355 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routersolicit
);
356 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routeradvert
);
357 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighborsolicit
);
358 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighboradvert
);
359 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_redirect
);
360 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldquery
);
361 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldreport
);
362 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mlddone
);
364 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_msg
);
365 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_error
);
366 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_dstunreach
);
367 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_adminprohib
);
368 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_timeexceed
);
369 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_paramprob
);
370 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_pkttoobig
);
371 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echo
);
372 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echoreply
);
373 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routersolicit
);
374 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routeradvert
);
375 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighborsolicit
);
376 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighboradvert
);
377 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_redirect
);
378 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldquery
);
379 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldreport
);
380 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mlddone
);
387 addrsel_policy_init();
389 * in many cases, random() here does NOT return random number
390 * as initialization during bootstrap time occur in fixed order.
393 ip6_flow_seq
= random() ^ tv
.tv_usec
;
395 ip6_desync_factor
= (random() ^ tv
.tv_usec
) % MAX_TEMP_DESYNC_FACTOR
;
396 timeout(ip6_init2
, (caddr_t
)0, 1 * hz
);
398 lck_mtx_unlock(domain_proto_mtx
);
399 proto_register_input(PF_INET6
, ip6_proto_input
, NULL
, 0);
400 lck_mtx_lock(domain_proto_mtx
);
405 __unused
void *dummy
)
408 * to route local address of p2p link to loopback,
409 * assign loopback address first.
411 if (loopattach_done
== 0) {
412 timeout(ip6_init2
, (caddr_t
)0, 1 * hz
);
415 (void) in6_ifattach(lo_ifp
, NULL
, NULL
);
419 timeout(nd6_timer
, (caddr_t
)0, hz
);
421 /* timer for regeneranation of temporary addresses randomize ID */
422 timeout(in6_tmpaddrtimer
, (caddr_t
)0,
423 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
424 ip6_temp_regen_advance
) * hz
);
438 lck_mtx_lock(&ip6_init_mutex
);
440 wakeup(&in6_init_done
);
441 lck_mtx_unlock(&ip6_init_mutex
);
447 lck_mtx_lock(&ip6_init_mutex
);
448 while (in6_init_done
== 0) {
449 (void) msleep(&in6_init_done
, &ip6_init_mutex
, 0, "ip6_fin()", NULL
);
451 lck_mtx_unlock(&ip6_init_mutex
);
455 ip6_input(struct mbuf
*m
)
458 int off
= sizeof(struct ip6_hdr
), nest
;
460 u_int32_t rtalert
= ~0;
461 int nxt
= 0, ours
= 0;
462 struct ifnet
*deliverifp
= NULL
;
463 ipfilter_t inject_ipfref
= 0;
465 struct in6_ifaddr
*ia6
= NULL
;
466 struct route_in6 ip6_forward_rt
;
467 struct sockaddr_in6
*dst6
;
469 bzero(&ip6_forward_rt
, sizeof(ip6_forward_rt
));
471 /* Check if the packet we received is valid after interface filter
474 MBUF_INPUT_CHECK(m
, m
->m_pkthdr
.rcvif
);
477 * No need to proccess packet twice if we've
480 inject_ipfref
= ipf_get_inject_filter(m
);
481 if (inject_ipfref
!= 0) {
482 ip6
= mtod(m
, struct ip6_hdr
*);
491 * should the inner packet be considered authentic?
492 * see comment in ah4_input().
495 m
->m_flags
&= ~M_AUTHIPHDR
;
496 m
->m_flags
&= ~M_AUTHIPDGM
;
501 * make sure we don't have onion peering information into m_aux.
508 if (m
->m_flags
& M_EXT
) {
510 ip6stat
.ip6s_mext2m
++;
512 ip6stat
.ip6s_mext1
++;
514 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
516 if (m
->m_flags
& M_LOOP
) {
517 ip6stat
.ip6s_m2m
[ifnet_index(lo_ifp
)]++; /* XXX */
518 } else if (m
->m_pkthdr
.rcvif
->if_index
< M2MMAX
)
519 ip6stat
.ip6s_m2m
[m
->m_pkthdr
.rcvif
->if_index
]++;
521 ip6stat
.ip6s_m2m
[0]++;
527 /* drop the packet if IPv6 operation is disabled on the IF */
528 lck_rw_lock_shared(nd_if_rwlock
);
529 if (m
->m_pkthdr
.rcvif
->if_index
< nd_ifinfo_indexlim
&&
530 (nd_ifinfo
[m
->m_pkthdr
.rcvif
->if_index
].flags
& ND6_IFF_IFDISABLED
)) {
531 lck_rw_done(nd_if_rwlock
);
534 lck_rw_done(nd_if_rwlock
);
536 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_receive
);
537 ip6stat
.ip6s_total
++;
539 #ifndef PULLDOWN_TEST
541 * L2 bridge code and some other code can return mbuf chain
542 * that does not conform to KAME requirement. too bad.
543 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
545 if (m
&& m
->m_next
!= NULL
&& m
->m_pkthdr
.len
< MCLBYTES
) {
548 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
551 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
552 MCLGET(n
, M_DONTWAIT
);
553 if ((n
->m_flags
& M_EXT
) == 0) {
561 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
562 n
->m_len
= m
->m_pkthdr
.len
;
566 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
),
570 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
572 inifp
= m
->m_pkthdr
.rcvif
;
573 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == 0) {
574 ip6stat
.ip6s_toosmall
++;
575 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
580 ip6
= mtod(m
, struct ip6_hdr
*);
582 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
583 ip6stat
.ip6s_badvers
++;
584 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
588 ip6stat
.ip6s_nxthist
[ip6
->ip6_nxt
]++;
592 * Check with the firewall...
594 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
596 /* If ipfw says divert, we have to just drop packet */
597 /* use port as a dummy argument */
598 if ((*ip6_fw_chk_ptr
)(&ip6
, NULL
, &port
, &m
)) {
608 * Check against address spoofing/corruption.
610 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
) ||
611 IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_dst
)) {
613 * XXX: "badscope" is not very suitable for a multicast source.
615 ip6stat
.ip6s_badscope
++;
616 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
619 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
) &&
620 !(m
->m_flags
& M_LOOP
)) {
622 * In this case, the packet should come from the loopback
623 * interface. However, we cannot just check the if_flags,
624 * because ip6_mloopback() passes the "actual" interface
625 * as the outgoing/incoming interface.
627 ip6stat
.ip6s_badscope
++;
628 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
633 * The following check is not documented in specs. A malicious
634 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
635 * and bypass security checks (act as if it was from 127.0.0.1 by using
636 * IPv6 src ::ffff:127.0.0.1). Be cautious.
638 * This check chokes if we are in an SIIT cloud. As none of BSDs
639 * support IPv4-less kernel compilation, we cannot support SIIT
640 * environment at all. So, it makes more sense for us to reject any
641 * malicious packets for non-SIIT environment, than try to do a
642 * partial support for SIIT environment.
644 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
645 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
646 ip6stat
.ip6s_badscope
++;
647 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
652 * Reject packets with IPv4 compatible addresses (auto tunnel).
654 * The code forbids auto tunnel relay case in RFC1933 (the check is
655 * stronger than RFC1933). We may want to re-enable it if mech-xx
656 * is revised to forbid relaying case.
658 if (IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_src
) ||
659 IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_dst
)) {
660 ip6stat
.ip6s_badscope
++;
661 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
667 * Naively assume we can attribute inbound data to the route we would
668 * use to send to this destination. Asymetric routing breaks this
669 * assumption, but it still allows us to account for traffic from
670 * a remote node in the routing table.
671 * this has a very significant performance impact so we bypass
672 * if nstat_collect is disabled. We may also bypass if the
673 * protocol is tcp in the future because tcp will have a route that
674 * we can use to attribute the data to. That does mean we would not
675 * account for forwarded tcp traffic.
678 struct rtentry
*rte
=
679 ifnet_cached_rtlookup_inet6(m
->m_pkthdr
.rcvif
,
682 nstat_route_rx(rte
, 1, m
->m_pkthdr
.len
, 0);
688 /* Invoke inbound packet filter */
691 error
= pf_af_hook(m
->m_pkthdr
.rcvif
, NULL
, &m
, AF_INET6
, TRUE
);
694 panic("%s: unexpected packet %p\n", __func__
, m
);
697 /* Already freed by callee */
700 ip6
= mtod(m
, struct ip6_hdr
*);
704 /* drop packets if interface ID portion is already filled */
705 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
706 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
) &&
707 ip6
->ip6_src
.s6_addr16
[1]) {
708 ip6stat
.ip6s_badscope
++;
711 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
712 ip6
->ip6_dst
.s6_addr16
[1]) {
713 ip6stat
.ip6s_badscope
++;
718 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
719 ip6
->ip6_src
.s6_addr16
[1]
720 = htons(m
->m_pkthdr
.rcvif
->if_index
);
721 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
722 ip6
->ip6_dst
.s6_addr16
[1]
723 = htons(m
->m_pkthdr
.rcvif
->if_index
);
728 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
729 struct in6_multi
*in6m
= NULL
;
730 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
732 in6_ifstat_inc(ifp
, ifs6_in_mcast
);
734 * See if we belong to the destination multicast group on the
737 in6_multihead_lock_shared();
738 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, in6m
);
739 in6_multihead_lock_done();
749 ip6stat
.ip6s_notmember
++;
750 ip6stat
.ip6s_cantforward
++;
751 in6_ifstat_inc(ifp
, ifs6_in_discard
);
761 dst6
= (struct sockaddr_in6
*)&ip6_forward_rt
.ro_dst
;
762 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
763 dst6
->sin6_family
= AF_INET6
;
764 dst6
->sin6_addr
= ip6
->ip6_dst
;
766 rtalloc_scoped_ign((struct route
*)&ip6_forward_rt
,
767 RTF_PRCLONING
, IFSCOPE_NONE
);
768 if (ip6_forward_rt
.ro_rt
!= NULL
)
769 RT_LOCK(ip6_forward_rt
.ro_rt
);
771 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
774 * Accept the packet if the forwarding interface to the destination
775 * according to the routing table is the loopback interface,
776 * unless the associated route has a gateway.
777 * Note that this approach causes to accept a packet if there is a
778 * route to the loopback interface for the destination of the packet.
779 * But we think it's even useful in some situations, e.g. when using
780 * a special daemon which wants to intercept the packet.
782 * XXX: some OSes automatically make a cloned route for the destination
783 * of an outgoing packet. If the outgoing interface of the packet
784 * is a loopback one, the kernel would consider the packet to be
785 * accepted, even if we have no such address assinged on the interface.
786 * We check the cloned flag of the route entry to reject such cases,
787 * assuming that route entries for our own addresses are not made by
788 * cloning (it should be true because in6_addloop explicitly installs
789 * the host route). However, we might have to do an explicit check
790 * while it would be less efficient. Or, should we rather install a
791 * reject route for such a case?
793 if (ip6_forward_rt
.ro_rt
!= NULL
&&
794 (ip6_forward_rt
.ro_rt
->rt_flags
&
795 (RTF_HOST
|RTF_GATEWAY
)) == RTF_HOST
&&
797 !(ip6_forward_rt
.ro_rt
->rt_flags
& RTF_WASCLONED
) &&
801 * The check below is redundant since the comparison of
802 * the destination and the key of the rtentry has
803 * already done through looking up the routing table.
805 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
806 &rt6_key(ip6_forward_rt
.ro_rt
)->sin6_addr
)
808 ip6_forward_rt
.ro_rt
->rt_ifp
->if_type
== IFT_LOOP
) {
809 ia6
= (struct in6_ifaddr
*)ip6_forward_rt
.ro_rt
->rt_ifa
;
812 * record address information into m_aux.
814 (void)ip6_setdstifaddr(m
, ia6
);
817 * packets to a tentative, duplicated, or somehow invalid
818 * address must not be accepted.
820 RT_CONVERT_LOCK(ip6_forward_rt
.ro_rt
); /* just in case */
821 IFA_LOCK_SPIN(&ia6
->ia_ifa
);
822 if (!(ia6
->ia6_flags
& IN6_IFF_NOTREADY
)) {
823 IFA_UNLOCK(&ia6
->ia_ifa
);
824 /* this address is ready */
826 deliverifp
= ia6
->ia_ifp
; /* correct? */
827 /* Count the packet in the ip address stats */
829 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
833 IFA_UNLOCK(&ia6
->ia_ifa
);
834 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
835 /* address is not ready, so discard the packet. */
837 "ip6_input: packet to an unready address %s->%s\n",
838 ip6_sprintf(&ip6
->ip6_src
),
839 ip6_sprintf(&ip6
->ip6_dst
)));
845 * FAITH (Firewall Aided Internet Translator)
847 #if defined(NFAITH) && 0 < NFAITH
849 if (ip6_forward_rt
.ro_rt
&& ip6_forward_rt
.ro_rt
->rt_ifp
850 && ip6_forward_rt
.ro_rt
->rt_ifp
->if_type
== IFT_FAITH
) {
851 /* XXX do we need more sanity checks? */
853 deliverifp
= ip6_forward_rt
.ro_rt
->rt_ifp
; /* faith */
854 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
859 if (ip6_forward_rt
.ro_rt
!= NULL
)
860 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
863 * Now there is no reason to process the packet if it's not our own
864 * and we're not a router.
866 if (!ip6_forwarding
) {
867 ip6stat
.ip6s_cantforward
++;
868 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
874 * record address information into m_aux, if we don't have one yet.
875 * note that we are unable to record it, if the address is not listed
876 * as our interface address (e.g. multicast addresses, addresses
877 * within FAITH prefixes and such).
879 if (deliverifp
&& (ia6
= ip6_getdstifaddr(m
)) == NULL
) {
880 ia6
= in6_ifawithifp(deliverifp
, &ip6
->ip6_dst
);
882 if (!ip6_setdstifaddr(m
, ia6
)) {
884 * XXX maybe we should drop the packet here,
885 * as we could not provide enough information
886 * to the upper layers.
889 IFA_REMREF(&ia6
->ia_ifa
);
895 IFA_REMREF(&ia6
->ia_ifa
);
900 * Process Hop-by-Hop options header if it's contained.
901 * m may be modified in ip6_hopopts_input().
902 * If a JumboPayload option is included, plen will also be modified.
904 plen
= (u_int32_t
)ntohs(ip6
->ip6_plen
);
905 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
908 if (ip6_hopopts_input(&plen
, &rtalert
, &m
, &off
)) {
909 #if 0 /*touches NULL pointer*/
910 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
912 goto done
; /* m have already been freed */
916 ip6
= mtod(m
, struct ip6_hdr
*);
919 * if the payload length field is 0 and the next header field
920 * indicates Hop-by-Hop Options header, then a Jumbo Payload
921 * option MUST be included.
923 if (ip6
->ip6_plen
== 0 && plen
== 0) {
925 * Note that if a valid jumbo payload option is
926 * contained, ip6_hopopts_input() must set a valid
927 * (non-zero) payload length to the variable plen.
929 ip6stat
.ip6s_badoptions
++;
930 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
931 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
932 icmp6_error(m
, ICMP6_PARAM_PROB
,
933 ICMP6_PARAMPROB_HEADER
,
934 (caddr_t
)&ip6
->ip6_plen
- (caddr_t
)ip6
);
937 #ifndef PULLDOWN_TEST
938 /* ip6_hopopts_input() ensures that mbuf is contiguous */
939 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
941 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
, sizeof(struct ip6_hdr
),
942 sizeof(struct ip6_hbh
));
944 ip6stat
.ip6s_tooshort
++;
951 * If we are acting as a router and the packet contains a
952 * router alert option, see if we know the option value.
953 * Currently, we only support the option value for MLD, in which
954 * case we should pass the packet to the multicast routing
957 if (rtalert
!= ~0 && ip6_forwarding
) {
959 case IP6OPT_RTALERT_MLD
:
964 * RFC2711 requires unrecognized values must be
974 * Check that the amount of data in the buffers
975 * is as at least much as the IPv6 header would have us expect.
976 * Trim mbufs if longer than we expect.
977 * Drop packet if shorter than we expect.
979 if (m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
) < plen
) {
980 ip6stat
.ip6s_tooshort
++;
981 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_truncated
);
984 if (m
->m_pkthdr
.len
> sizeof(struct ip6_hdr
) + plen
) {
985 if (m
->m_len
== m
->m_pkthdr
.len
) {
986 m
->m_len
= sizeof(struct ip6_hdr
) + plen
;
987 m
->m_pkthdr
.len
= sizeof(struct ip6_hdr
) + plen
;
989 m_adj(m
, sizeof(struct ip6_hdr
) + plen
- m
->m_pkthdr
.len
);
993 * Forward if desirable.
995 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
997 * If we are acting as a multicast router, all
998 * incoming multicast packets are passed to the
999 * kernel-level multicast forwarding function.
1000 * The packet is returned (relatively) intact; if
1001 * ip6_mforward() returns a non-zero value, the packet
1002 * must be discarded, else it may be accepted below.
1005 if (ip6_mrouter
&& ip6_mforward(ip6
, m
->m_pkthdr
.rcvif
, m
)) {
1006 ip6stat
.ip6s_cantforward
++;
1013 ip6_forward(m
, &ip6_forward_rt
, 0);
1017 ip6
= mtod(m
, struct ip6_hdr
*);
1020 * Malicious party may be able to use IPv4 mapped addr to confuse
1021 * tcp/udp stack and bypass security checks (act as if it was from
1022 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
1024 * For SIIT end node behavior, you may want to disable the check.
1025 * However, you will become vulnerable to attacks using IPv4 mapped
1028 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
1029 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
1030 ip6stat
.ip6s_badscope
++;
1031 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
1036 * Tell launch routine the next header
1038 ip6stat
.ip6s_delivered
++;
1039 in6_ifstat_inc(deliverifp
, ifs6_in_deliver
);
1044 while (nxt
!= IPPROTO_DONE
) {
1045 struct ipfilter
*filter
;
1046 int (*pr_input
)(struct mbuf
**, int *, int);
1048 if (ip6_hdrnestlimit
&& (++nest
> ip6_hdrnestlimit
)) {
1049 ip6stat
.ip6s_toomanyhdr
++;
1054 * protection against faulty packet - there should be
1055 * more sanity checks in header chain processing.
1057 if (m
->m_pkthdr
.len
< off
) {
1058 ip6stat
.ip6s_tooshort
++;
1059 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_truncated
);
1066 * enforce IPsec policy checking if we are seeing last header.
1067 * note that we do not visit this with protocols with pcb layer
1068 * code - like udp/tcp/raw ip.
1070 if ((ipsec_bypass
== 0) && (ip6_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0) {
1071 if (ipsec6_in_reject(m
, NULL
)) {
1072 IPSEC_STAT_INCREMENT(ipsec6stat
.in_polvio
);
1081 if (!TAILQ_EMPTY(&ipv6_filters
)) {
1083 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
1085 if ((struct ipfilter
*)inject_ipfref
== filter
)
1087 } else if (filter
->ipf_filter
.ipf_input
) {
1090 result
= filter
->ipf_filter
.ipf_input(
1091 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, off
, nxt
);
1092 if (result
== EJUSTRETURN
) {
1105 DTRACE_IP6(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1106 struct ip6_hdr
*, ip6
, struct ifnet
*, m
->m_pkthdr
.rcvif
,
1107 struct ip
*, NULL
, struct ip6_hdr
*, ip6
);
1109 if ((pr_input
= ip6_protox
[nxt
]->pr_input
) == NULL
) {
1113 } else if (!(ip6_protox
[nxt
]->pr_flags
& PR_PROTOLOCK
)) {
1114 lck_mtx_lock(inet6_domain_mutex
);
1115 nxt
= pr_input(&m
, &off
, nxt
);
1116 lck_mtx_unlock(inet6_domain_mutex
);
1118 nxt
= pr_input(&m
, &off
, nxt
);
1122 if (ip6_forward_rt
.ro_rt
!= NULL
)
1123 rtfree(ip6_forward_rt
.ro_rt
);
1131 * set/grab in6_ifaddr correspond to IPv6 destination address.
1132 * XXX backward compatibility wrapper
1134 static struct ip6aux
*
1135 ip6_setdstifaddr(struct mbuf
*m
, struct in6_ifaddr
*ia6
)
1142 IFA_ADDREF(&ia6
->ia_ifa
);
1143 if (n
->ip6a_dstia6
!= NULL
)
1144 IFA_REMREF(&n
->ip6a_dstia6
->ia_ifa
);
1145 n
->ip6a_dstia6
= ia6
;
1147 return (struct ip6aux
*)n
; /* NULL if failed to set */
1158 if (n
->ip6a_dstia6
!= NULL
)
1159 IFA_ADDREF(&n
->ip6a_dstia6
->ia_ifa
);
1160 return (n
->ip6a_dstia6
);
1166 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1167 * included, the real payload length will be stored in plenp.
1170 ip6_hopopts_input(uint32_t *plenp
, uint32_t *rtalertp
, struct mbuf
**mp
,
1173 struct mbuf
*m
= *mp
;
1174 int off
= *offp
, hbhlen
;
1175 struct ip6_hbh
*hbh
;
1178 /* validation of the length of the header */
1179 #ifndef PULLDOWN_TEST
1180 IP6_EXTHDR_CHECK(m
, off
, sizeof(*hbh
), return -1);
1181 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1182 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1184 IP6_EXTHDR_CHECK(m
, off
, hbhlen
, return -1);
1185 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1187 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
,
1188 sizeof(struct ip6_hdr
), sizeof(struct ip6_hbh
));
1190 ip6stat
.ip6s_tooshort
++;
1193 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1194 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
, sizeof(struct ip6_hdr
),
1197 ip6stat
.ip6s_tooshort
++;
1202 hbhlen
-= sizeof(struct ip6_hbh
);
1203 opt
= (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
);
1205 if (ip6_process_hopopts(m
, (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
),
1206 hbhlen
, rtalertp
, plenp
) < 0)
1215 * Search header for all Hop-by-hop options and process each option.
1216 * This function is separate from ip6_hopopts_input() in order to
1217 * handle a case where the sending node itself process its hop-by-hop
1218 * options header. In such a case, the function is called from ip6_output().
1220 * The function assumes that hbh header is located right after the IPv6 header
1221 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1222 * opthead + hbhlen is located in continuous memory region.
1225 ip6_process_hopopts(m
, opthead
, hbhlen
, rtalertp
, plenp
)
1229 u_int32_t
*rtalertp
;
1232 struct ip6_hdr
*ip6
;
1234 u_int8_t
*opt
= opthead
;
1235 u_int16_t rtalert_val
;
1236 u_int32_t jumboplen
;
1237 const int erroff
= sizeof(struct ip6_hdr
) + sizeof(struct ip6_hbh
);
1239 for (; hbhlen
> 0; hbhlen
-= optlen
, opt
+= optlen
) {
1245 if (hbhlen
< IP6OPT_MINLEN
) {
1246 ip6stat
.ip6s_toosmall
++;
1249 optlen
= *(opt
+ 1) + 2;
1251 case IP6OPT_ROUTER_ALERT
:
1252 /* XXX may need check for alignment */
1253 if (hbhlen
< IP6OPT_RTALERT_LEN
) {
1254 ip6stat
.ip6s_toosmall
++;
1257 if (*(opt
+ 1) != IP6OPT_RTALERT_LEN
- 2) {
1259 icmp6_error(m
, ICMP6_PARAM_PROB
,
1260 ICMP6_PARAMPROB_HEADER
,
1261 erroff
+ opt
+ 1 - opthead
);
1264 optlen
= IP6OPT_RTALERT_LEN
;
1265 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&rtalert_val
, 2);
1266 *rtalertp
= ntohs(rtalert_val
);
1269 /* XXX may need check for alignment */
1270 if (hbhlen
< IP6OPT_JUMBO_LEN
) {
1271 ip6stat
.ip6s_toosmall
++;
1274 if (*(opt
+ 1) != IP6OPT_JUMBO_LEN
- 2) {
1276 icmp6_error(m
, ICMP6_PARAM_PROB
,
1277 ICMP6_PARAMPROB_HEADER
,
1278 erroff
+ opt
+ 1 - opthead
);
1281 optlen
= IP6OPT_JUMBO_LEN
;
1284 * IPv6 packets that have non 0 payload length
1285 * must not contain a jumbo payload option.
1287 ip6
= mtod(m
, struct ip6_hdr
*);
1288 if (ip6
->ip6_plen
) {
1289 ip6stat
.ip6s_badoptions
++;
1290 icmp6_error(m
, ICMP6_PARAM_PROB
,
1291 ICMP6_PARAMPROB_HEADER
,
1292 erroff
+ opt
- opthead
);
1297 * We may see jumbolen in unaligned location, so
1298 * we'd need to perform bcopy().
1300 bcopy(opt
+ 2, &jumboplen
, sizeof(jumboplen
));
1301 jumboplen
= (u_int32_t
)htonl(jumboplen
);
1305 * if there are multiple jumbo payload options,
1306 * *plenp will be non-zero and the packet will be
1308 * the behavior may need some debate in ipngwg -
1309 * multiple options does not make sense, however,
1310 * there's no explicit mention in specification.
1313 ip6stat
.ip6s_badoptions
++;
1314 icmp6_error(m
, ICMP6_PARAM_PROB
,
1315 ICMP6_PARAMPROB_HEADER
,
1316 erroff
+ opt
+ 2 - opthead
);
1322 * jumbo payload length must be larger than 65535.
1324 if (jumboplen
<= IPV6_MAXPACKET
) {
1325 ip6stat
.ip6s_badoptions
++;
1326 icmp6_error(m
, ICMP6_PARAM_PROB
,
1327 ICMP6_PARAMPROB_HEADER
,
1328 erroff
+ opt
+ 2 - opthead
);
1334 default: /* unknown option */
1335 if (hbhlen
< IP6OPT_MINLEN
) {
1336 ip6stat
.ip6s_toosmall
++;
1339 optlen
= ip6_unknown_opt(opt
, m
,
1340 erroff
+ opt
- opthead
);
1357 * Unknown option processing.
1358 * The third argument `off' is the offset from the IPv6 header to the option,
1359 * which is necessary if the IPv6 header the and option header and IPv6 header
1360 * is not continuous in order to return an ICMPv6 error.
1363 ip6_unknown_opt(uint8_t *optp
, struct mbuf
*m
, int off
)
1365 struct ip6_hdr
*ip6
;
1367 switch (IP6OPT_TYPE(*optp
)) {
1368 case IP6OPT_TYPE_SKIP
: /* ignore the option */
1369 return((int)*(optp
+ 1));
1370 case IP6OPT_TYPE_DISCARD
: /* silently discard */
1373 case IP6OPT_TYPE_FORCEICMP
: /* send ICMP even if multicasted */
1374 ip6stat
.ip6s_badoptions
++;
1375 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_OPTION
, off
);
1377 case IP6OPT_TYPE_ICMP
: /* send ICMP if not multicasted */
1378 ip6stat
.ip6s_badoptions
++;
1379 ip6
= mtod(m
, struct ip6_hdr
*);
1380 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
1381 (m
->m_flags
& (M_BCAST
|M_MCAST
)))
1384 icmp6_error(m
, ICMP6_PARAM_PROB
,
1385 ICMP6_PARAMPROB_OPTION
, off
);
1389 m_freem(m
); /* XXX: NOTREACHED */
1394 * Create the "control" list for this pcb.
1395 * These functions will not modify mbuf chain at all.
1397 * With KAME mbuf chain restriction:
1398 * The routine will be called from upper layer handlers like tcp6_input().
1399 * Thus the routine assumes that the caller (tcp6_input) have already
1400 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1401 * very first mbuf on the mbuf chain.
1403 * ip6_savecontrol_v4 will handle those options that are possible to be
1404 * set on a v4-mapped socket.
1405 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1406 * options and handle the v6-only ones itself.
1409 ip6_savecontrol_v4(struct inpcb
*inp
, struct mbuf
*m
, struct mbuf
**mp
,
1412 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1414 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0) {
1418 mp
= sbcreatecontrol_mbuf((caddr_t
) &tv
, sizeof(tv
),
1419 SCM_TIMESTAMP
, SOL_SOCKET
, mp
);
1423 if ((inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
1426 time
= mach_absolute_time();
1427 mp
= sbcreatecontrol_mbuf((caddr_t
) &time
, sizeof(time
),
1428 SCM_TIMESTAMP_MONOTONIC
, SOL_SOCKET
, mp
);
1433 if ((inp
->inp_socket
->so_flags
& SOF_RECV_TRAFFIC_CLASS
) != 0) {
1434 int tc
= m
->m_pkthdr
.prio
;
1436 mp
= sbcreatecontrol_mbuf((caddr_t
) &tc
, sizeof(tc
),
1437 SO_TRAFFIC_CLASS
, SOL_SOCKET
, mp
);
1442 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
1448 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1449 /* RFC 2292 sec. 5 */
1450 if ((inp
->inp_flags
& IN6P_PKTINFO
) != 0) {
1451 struct in6_pktinfo pi6
;
1453 bcopy(&ip6
->ip6_dst
, &pi6
.ipi6_addr
, sizeof(struct in6_addr
));
1454 in6_clearscope(&pi6
.ipi6_addr
); /* XXX */
1456 (m
&& m
->m_pkthdr
.rcvif
) ? m
->m_pkthdr
.rcvif
->if_index
: 0;
1458 mp
= sbcreatecontrol_mbuf((caddr_t
) &pi6
,
1459 sizeof(struct in6_pktinfo
),
1460 IS2292(inp
, IPV6_2292PKTINFO
, IPV6_PKTINFO
), IPPROTO_IPV6
, mp
);
1465 if ((inp
->inp_flags
& IN6P_HOPLIMIT
) != 0) {
1466 int hlim
= ip6
->ip6_hlim
& 0xff;
1468 mp
= sbcreatecontrol_mbuf((caddr_t
) &hlim
, sizeof(int),
1469 IS2292(inp
, IPV6_2292HOPLIMIT
, IPV6_HOPLIMIT
),
1481 ip6_savecontrol(struct inpcb
*in6p
, struct mbuf
*m
, struct mbuf
**mp
)
1484 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1488 np
= ip6_savecontrol_v4(in6p
, m
, mp
, &v4only
);
1496 if ((in6p
->inp_flags
& IN6P_TCLASS
) != 0) {
1500 flowinfo
= (u_int32_t
)ntohl(ip6
->ip6_flow
& IPV6_FLOWINFO_MASK
);
1503 tclass
= flowinfo
& 0xff;
1504 mp
= sbcreatecontrol_mbuf((caddr_t
) &tclass
, sizeof(tclass
),
1505 IPV6_TCLASS
, IPPROTO_IPV6
, mp
);
1511 * IPV6_HOPOPTS socket option. Recall that we required super-user
1512 * privilege for the option (see ip6_ctloutput), but it might be too
1513 * strict, since there might be some hop-by-hop options which can be
1514 * returned to normal user.
1515 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1517 if ((in6p
->inp_flags
& IN6P_HOPOPTS
) != 0) {
1519 * Check if a hop-by-hop options header is contatined in the
1520 * received packet, and if so, store the options as ancillary
1521 * data. Note that a hop-by-hop options header must be
1522 * just after the IPv6 header, which is assured through the
1523 * IPv6 input processing.
1525 ip6
= mtod(m
, struct ip6_hdr
*);
1526 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
1527 struct ip6_hbh
*hbh
;
1533 #ifndef PULLDOWN_TEST
1534 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
1535 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1537 ext
= ip6_pullexthdr(m
, sizeof(struct ip6_hdr
),
1540 ip6stat
.ip6s_tooshort
++;
1543 hbh
= mtod(ext
, struct ip6_hbh
*);
1544 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1545 if (hbhlen
!= ext
->m_len
) {
1547 ip6stat
.ip6s_tooshort
++;
1553 * XXX: We copy the whole header even if a
1554 * jumbo payload option is included, the option which
1555 * is to be removed before returning according to
1557 * Note: this constraint is removed in RFC3542
1559 mp
= sbcreatecontrol_mbuf((caddr_t
)hbh
, hbhlen
,
1560 IS2292(in6p
, IPV6_2292HOPOPTS
, IPV6_HOPOPTS
),
1572 if ((in6p
->inp_flags
& (IN6P_RTHDR
| IN6P_DSTOPTS
)) != 0) {
1573 int nxt
= ip6
->ip6_nxt
, off
= sizeof(struct ip6_hdr
);
1576 * Search for destination options headers or routing
1577 * header(s) through the header chain, and stores each
1578 * header as ancillary data.
1579 * Note that the order of the headers remains in
1580 * the chain of ancillary data.
1582 while (1) { /* is explicit loop prevention necessary? */
1583 struct ip6_ext
*ip6e
= NULL
;
1586 struct mbuf
*ext
= NULL
;
1590 * if it is not an extension header, don't try to
1591 * pull it from the chain.
1594 case IPPROTO_DSTOPTS
:
1595 case IPPROTO_ROUTING
:
1596 case IPPROTO_HOPOPTS
:
1597 case IPPROTO_AH
: /* is it possible? */
1603 #ifndef PULLDOWN_TEST
1604 if (off
+ sizeof(*ip6e
) > m
->m_len
)
1606 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + off
);
1607 if (nxt
== IPPROTO_AH
)
1608 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1610 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1611 if (off
+ elen
> m
->m_len
)
1614 ext
= ip6_pullexthdr(m
, off
, nxt
);
1616 ip6stat
.ip6s_tooshort
++;
1619 ip6e
= mtod(ext
, struct ip6_ext
*);
1620 if (nxt
== IPPROTO_AH
)
1621 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1623 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1624 if (elen
!= ext
->m_len
) {
1626 ip6stat
.ip6s_tooshort
++;
1632 case IPPROTO_DSTOPTS
:
1633 if (!(in6p
->inp_flags
& IN6P_DSTOPTS
))
1636 mp
= sbcreatecontrol_mbuf((caddr_t
)ip6e
, elen
,
1638 IPV6_2292DSTOPTS
, IPV6_DSTOPTS
),
1647 case IPPROTO_ROUTING
:
1648 if (!in6p
->inp_flags
& IN6P_RTHDR
)
1651 mp
= sbcreatecontrol_mbuf((caddr_t
)ip6e
, elen
,
1652 IS2292(in6p
, IPV6_2292RTHDR
, IPV6_RTHDR
),
1661 case IPPROTO_HOPOPTS
:
1662 case IPPROTO_AH
: /* is it possible? */
1667 * other cases have been filtered in the above.
1668 * none will visit this case. here we supply
1669 * the code just in case (nxt overwritten or
1679 /* proceed with the next header. */
1681 nxt
= ip6e
->ip6e_nxt
;
1693 ip6stat
.ip6s_pktdropcntrl
++;
1694 /* XXX increment a stat to show the failure */
1700 ip6_notify_pmtu(struct inpcb
*in6p
, struct sockaddr_in6
*dst
, u_int32_t
*mtu
)
1704 struct ip6_mtuinfo mtuctl
;
1706 so
= in6p
->inp_socket
;
1712 if (so
== NULL
) /* I believe this is impossible */
1713 panic("ip6_notify_pmtu: socket is NULL");
1716 bzero(&mtuctl
, sizeof(mtuctl
)); /* zero-clear for safety */
1717 mtuctl
.ip6m_mtu
= *mtu
;
1718 mtuctl
.ip6m_addr
= *dst
;
1719 if (sa6_recoverscope(&mtuctl
.ip6m_addr
))
1722 if ((m_mtu
= sbcreatecontrol((caddr_t
)&mtuctl
, sizeof(mtuctl
),
1723 IPV6_PATHMTU
, IPPROTO_IPV6
)) == NULL
)
1726 if (sbappendaddr(&so
->so_rcv
, (struct sockaddr
*)dst
, NULL
, m_mtu
, NULL
)
1729 /* XXX: should count statistics */
1738 * pull single extension header from mbuf chain. returns single mbuf that
1739 * contains the result, or NULL on error.
1741 static struct mbuf
*
1742 ip6_pullexthdr(m
, off
, nxt
)
1747 struct ip6_ext ip6e
;
1753 case IPPROTO_DSTOPTS
:
1754 case IPPROTO_ROUTING
:
1755 case IPPROTO_HOPOPTS
:
1756 case IPPROTO_AH
: /* is it possible? */
1759 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt
);
1763 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1764 if (nxt
== IPPROTO_AH
)
1765 elen
= (ip6e
.ip6e_len
+ 2) << 2;
1767 elen
= (ip6e
.ip6e_len
+ 1) << 3;
1769 MGET(n
, M_DONTWAIT
, MT_DATA
);
1770 if (n
&& elen
>= MLEN
) {
1771 MCLGET(n
, M_DONTWAIT
);
1772 if ((n
->m_flags
& M_EXT
) == 0) {
1781 if (elen
>= M_TRAILINGSPACE(n
)) {
1786 m_copydata(m
, off
, elen
, mtod(n
, caddr_t
));
1793 * Get pointer to the previous header followed by the header
1794 * currently processed.
1795 * XXX: This function supposes that
1796 * M includes all headers,
1797 * the next header field and the header length field of each header
1799 * the sum of each header length equals to OFF.
1800 * Because of these assumptions, this function must be called very
1801 * carefully. Moreover, it will not be used in the near future when
1802 * we develop `neater' mechanism to process extension headers.
1805 ip6_get_prevhdr(m
, off
)
1809 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1811 if (off
== sizeof(struct ip6_hdr
))
1812 return((char *) &ip6
->ip6_nxt
);
1815 struct ip6_ext
*ip6e
= NULL
;
1818 len
= sizeof(struct ip6_hdr
);
1820 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + len
);
1823 case IPPROTO_FRAGMENT
:
1824 len
+= sizeof(struct ip6_frag
);
1827 len
+= (ip6e
->ip6e_len
+ 2) << 2;
1830 len
+= (ip6e
->ip6e_len
+ 1) << 3;
1833 nxt
= ip6e
->ip6e_nxt
;
1836 return((char *) &ip6e
->ip6e_nxt
);
1843 * get next header offset. m will be retained.
1846 ip6_nexthdr(m
, off
, proto
, nxtp
)
1853 struct ip6_ext ip6e
;
1858 panic("ip6_nexthdr: m == NULL");
1859 if ((m
->m_flags
& M_PKTHDR
) == 0 || m
->m_pkthdr
.len
< off
)
1864 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6
))
1866 m_copydata(m
, off
, sizeof(ip6
), (caddr_t
)&ip6
);
1868 *nxtp
= ip6
.ip6_nxt
;
1872 case IPPROTO_FRAGMENT
:
1874 * terminate parsing if it is not the first fragment,
1875 * it does not make sense to parse through it.
1877 if (m
->m_pkthdr
.len
< off
+ sizeof(fh
))
1879 m_copydata(m
, off
, sizeof(fh
), (caddr_t
)&fh
);
1880 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1881 if (fh
.ip6f_offlg
& IP6F_OFF_MASK
)
1884 *nxtp
= fh
.ip6f_nxt
;
1885 off
+= sizeof(struct ip6_frag
);
1889 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
))
1891 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1893 *nxtp
= ip6e
.ip6e_nxt
;
1894 off
+= (ip6e
.ip6e_len
+ 2) << 2;
1897 case IPPROTO_HOPOPTS
:
1898 case IPPROTO_ROUTING
:
1899 case IPPROTO_DSTOPTS
:
1900 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
))
1902 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1904 *nxtp
= ip6e
.ip6e_nxt
;
1905 off
+= (ip6e
.ip6e_len
+ 1) << 3;
1910 case IPPROTO_IPCOMP
:
1922 * get offset for the last header in the chain. m will be kept untainted.
1925 ip6_lasthdr(m
, off
, proto
, nxtp
)
1939 newoff
= ip6_nexthdr(m
, off
, proto
, nxtp
);
1942 else if (newoff
< off
)
1943 return -1; /* invalid */
1944 else if (newoff
== off
)
1958 /* Check if one is already allocated */
1959 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_INET6
, NULL
);
1961 /* Allocate a tag */
1962 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_INET6
,
1963 sizeof (struct ip6aux
), M_DONTWAIT
, m
);
1965 /* Attach it to the mbuf */
1967 m_tag_prepend(m
, tag
);
1971 return tag
? (struct ip6aux
*)(tag
+ 1) : NULL
;
1980 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_INET6
, NULL
);
1982 return tag
? (struct ip6aux
*)(tag
+ 1) : NULL
;
1991 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_INET6
, NULL
);
1993 m_tag_delete(m
, tag
);
1998 * Called by m_tag_free().
2001 ip6_destroyaux(struct ip6aux
*n
)
2003 if (n
->ip6a_dstia6
!= NULL
) {
2004 IFA_REMREF(&n
->ip6a_dstia6
->ia_ifa
);
2005 n
->ip6a_dstia6
= NULL
;
2010 * Called by m_tag_copy()
2013 ip6_copyaux(struct ip6aux
*src
, struct ip6aux
*dst
)
2015 bcopy(src
, dst
, sizeof (*dst
));
2016 if (dst
->ip6a_dstia6
!= NULL
)
2017 IFA_ADDREF(&dst
->ip6a_dstia6
->ia_ifa
);
2021 * System control for IP6
2024 u_char inet6ctlerrmap
[PRC_NCMDS
] = {
2026 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
2027 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
2028 EMSGSIZE
, EHOSTUNREACH
, 0, 0,