2 * Copyright (c) 2003-2008 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/proc.h>
109 #include <sys/kauth.h>
112 #include <net/if_var.h>
113 #include <net/if_types.h>
114 #include <net/if_dl.h>
115 #include <net/route.h>
116 #include <net/kpi_protocol.h>
118 #include <netinet/in.h>
119 #include <netinet/in_systm.h>
121 #include <netinet/ip.h>
122 #include <netinet/ip_icmp.h>
124 #include <netinet/ip6.h>
125 #include <netinet6/in6_var.h>
126 #include <netinet6/ip6_var.h>
127 #include <netinet/in_pcb.h>
128 #include <netinet/icmp6.h>
129 #include <netinet6/in6_ifattach.h>
130 #include <netinet6/nd6.h>
131 #include <netinet6/in6_prefix.h>
134 #include <netinet6/ipsec.h>
136 #include <netinet6/ipsec6.h>
138 extern int ipsec_bypass
;
141 #include <netinet6/ip6_fw.h>
143 #include <netinet/kpi_ipfilter_var.h>
145 #include <netinet6/ip6protosw.h>
147 /* we need it for NLOOP. */
151 #include <net/net_osdep.h>
154 #include <net/pfvar.h>
157 extern struct domain inet6domain
;
158 extern struct ip6protosw inet6sw
[];
160 struct ip6protosw
* ip6_protox
[IPPROTO_MAX
];
161 static int ip6qmaxlen
= IFQ_MAXLEN
;
162 struct in6_ifaddr
*in6_ifaddrs
;
164 int ip6_forward_srcrt
; /* XXX */
165 int ip6_sourcecheck
; /* XXX */
166 int ip6_sourcecheck_interval
; /* XXX */
167 const int int6intrq_present
= 1;
169 int ip6_ours_check_algorithm
;
170 int in6_init2done
= 0;
174 ip6_fw_chk_t
*ip6_fw_chk_ptr
;
175 ip6_fw_ctl_t
*ip6_fw_ctl_ptr
;
176 int ip6_fw_enable
= 1;
178 struct ip6stat ip6stat
;
181 struct ifqueue ip6intrq
;
182 lck_mtx_t
*ip6_mutex
;
183 lck_mtx_t
*dad6_mutex
;
184 lck_mtx_t
*nd6_mutex
;
185 lck_mtx_t
*prefix6_mutex
;
186 lck_mtx_t
*scope6_mutex
;
187 lck_attr_t
*ip6_mutex_attr
;
188 lck_grp_t
*ip6_mutex_grp
;
189 lck_grp_attr_t
*ip6_mutex_grp_attr
;
190 extern lck_mtx_t
*inet6_domain_mutex
;
192 extern int loopattach_done
;
194 static void ip6_init2(void *);
195 static struct ip6aux
*ip6_setdstifaddr(struct mbuf
*, struct in6_ifaddr
*);
197 static int ip6_hopopts_input(u_int32_t
*, u_int32_t
*, struct mbuf
**, int *);
199 static struct mbuf
*ip6_pullexthdr(struct mbuf
*, size_t, int);
203 void gifattach(void);
204 void faithattach(void);
205 void stfattach(void);
208 extern lck_mtx_t
*domain_proto_mtx
;
213 __unused protocol_family_t protocol
,
220 * IP6 initialization: fill in IP6 protocol switch table.
221 * All protocols not implemented in kernel go to raw IP6 protocol handler.
226 struct ip6protosw
*pr
;
231 if (sizeof(struct protosw
) != sizeof(struct ip6protosw
))
232 panic("sizeof(protosw) != sizeof(ip6protosw)");
234 pr
= (struct ip6protosw
*)pffindproto_locked(PF_INET6
, IPPROTO_RAW
, SOCK_RAW
);
237 for (i
= 0; i
< IPPROTO_MAX
; i
++)
239 for (pr
= (struct ip6protosw
*)inet6domain
.dom_protosw
; pr
; pr
= pr
->pr_next
) {
240 if(!(pr
->pr_domain
)) continue; /* If uninitialized, skip */
241 if (pr
->pr_domain
->dom_family
== PF_INET6
&&
242 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
) {
243 ip6_protox
[pr
->pr_protocol
] = pr
;
247 ip6_mutex_grp_attr
= lck_grp_attr_alloc_init();
249 ip6_mutex_grp
= lck_grp_alloc_init("ip6", ip6_mutex_grp_attr
);
250 ip6_mutex_attr
= lck_attr_alloc_init();
252 if ((ip6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
253 panic("ip6_init: can't alloc ip6_mutex\n");
255 if ((dad6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
256 panic("ip6_init: can't alloc dad6_mutex\n");
258 if ((nd6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
259 panic("ip6_init: can't alloc nd6_mutex\n");
262 if ((prefix6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
263 panic("ip6_init: can't alloc prefix6_mutex\n");
266 if ((scope6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
267 panic("ip6_init: can't alloc scope6_mutex\n");
271 inet6domain
.dom_flags
= DOM_REENTRANT
;
273 ip6intrq
.ifq_maxlen
= ip6qmaxlen
;
279 * in many cases, random() here does NOT return random number
280 * as initialization during bootstrap time occur in fixed order.
283 ip6_flow_seq
= random() ^ tv
.tv_usec
;
285 ip6_desync_factor
= (random() ^ tv
.tv_usec
) % MAX_TEMP_DESYNC_FACTOR
;
286 timeout(ip6_init2
, (caddr_t
)0, 1 * hz
);
288 lck_mtx_unlock(domain_proto_mtx
);
289 proto_register_input(PF_INET6
, ip6_proto_input
, NULL
, 0);
290 lck_mtx_lock(domain_proto_mtx
);
295 __unused
void *dummy
)
298 * to route local address of p2p link to loopback,
299 * assign loopback address first.
301 if (loopattach_done
== 0) {
302 timeout(ip6_init2
, (caddr_t
)0, 1 * hz
);
305 (void) in6_ifattach(lo_ifp
, NULL
, NULL
);
309 timeout(nd6_timer
, (caddr_t
)0, hz
);
311 /* router renumbering prefix list maintenance */
312 timeout(in6_rr_timer
, (caddr_t
)0, hz
);
314 /* timer for regeneranation of temporary addresses randomize ID */
315 timeout(in6_tmpaddrtimer
, (caddr_t
)0,
316 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
317 ip6_temp_regen_advance
) * hz
);
331 callout_init(&nd6_timer_ch
);
332 callout_reset(&nd6_timer_ch
, hz
, nd6_timer
, NULL
);
334 /* router renumbering prefix list maintenance */
335 callout_init(&in6_rr_timer_ch
);
336 callout_reset(&in6_rr_timer_ch
, hz
, in6_rr_timer
, NULL
);
338 /* timer for regeneranation of temporary addresses randomize ID */
339 callout_reset(&in6_tmpaddrtimer_ch
,
340 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
341 ip6_temp_regen_advance
) * hz
,
342 in6_tmpaddrtimer
, NULL
);
350 /* This must be after route_init(), which is now SI_ORDER_THIRD */
351 SYSINIT(netinet6init2
, SI_SUB_PROTO_DOMAIN
, SI_ORDER_MIDDLE
, ip6_init2
, NULL
);
355 * ip6_forward_rt contains the route entry that was recently used during
356 * the forwarding of an IPv6 packet and thus acts as a route cache. Access
357 * to this variable is protected by the global lock ip6_mutex.
359 static struct route_in6 ip6_forward_rt
;
366 int off
= sizeof(struct ip6_hdr
), nest
;
368 u_int32_t rtalert
= ~0;
369 int nxt
= 0, ours
= 0;
370 struct ifnet
*deliverifp
= NULL
;
371 ipfilter_t inject_ipfref
= 0;
375 * No need to proccess packet twice if we've
378 inject_ipfref
= ipf_get_inject_filter(m
);
379 if (inject_ipfref
!= 0) {
380 ip6
= mtod(m
, struct ip6_hdr
*);
389 * should the inner packet be considered authentic?
390 * see comment in ah4_input().
393 m
->m_flags
&= ~M_AUTHIPHDR
;
394 m
->m_flags
&= ~M_AUTHIPDGM
;
399 * make sure we don't have onion peering information into m_aux.
403 lck_mtx_lock(ip6_mutex
);
407 if (m
->m_flags
& M_EXT
) {
409 ip6stat
.ip6s_mext2m
++;
411 ip6stat
.ip6s_mext1
++;
413 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
415 if (m
->m_flags
& M_LOOP
) {
416 ip6stat
.ip6s_m2m
[ifnet_index(lo_ifp
)]++; /* XXX */
417 } else if (m
->m_pkthdr
.rcvif
->if_index
< M2MMAX
)
418 ip6stat
.ip6s_m2m
[m
->m_pkthdr
.rcvif
->if_index
]++;
420 ip6stat
.ip6s_m2m
[0]++;
426 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_receive
);
427 ip6stat
.ip6s_total
++;
429 #ifndef PULLDOWN_TEST
431 * L2 bridge code and some other code can return mbuf chain
432 * that does not conform to KAME requirement. too bad.
433 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
435 if (m
&& m
->m_next
!= NULL
&& m
->m_pkthdr
.len
< MCLBYTES
) {
438 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
441 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
442 MCLGET(n
, M_DONTWAIT
);
443 if ((n
->m_flags
& M_EXT
) == 0) {
450 lck_mtx_unlock(ip6_mutex
);
454 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
455 n
->m_len
= m
->m_pkthdr
.len
;
459 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
),
460 {lck_mtx_unlock(ip6_mutex
); return;});
463 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
465 inifp
= m
->m_pkthdr
.rcvif
;
466 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == 0) {
467 ip6stat
.ip6s_toosmall
++;
468 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
469 lck_mtx_unlock(ip6_mutex
);
474 ip6
= mtod(m
, struct ip6_hdr
*);
476 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
477 ip6stat
.ip6s_badvers
++;
478 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
482 ip6stat
.ip6s_nxthist
[ip6
->ip6_nxt
]++;
485 * Check with the firewall...
487 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
489 /* If ipfw says divert, we have to just drop packet */
490 /* use port as a dummy argument */
491 if ((*ip6_fw_chk_ptr
)(&ip6
, NULL
, &port
, &m
)) {
496 lck_mtx_unlock(ip6_mutex
);
502 * Check against address spoofing/corruption.
504 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
) ||
505 IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_dst
)) {
507 * XXX: "badscope" is not very suitable for a multicast source.
509 ip6stat
.ip6s_badscope
++;
510 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
513 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
514 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
)) &&
515 (m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
516 ip6stat
.ip6s_badscope
++;
517 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
522 * The following check is not documented in specs. A malicious
523 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
524 * and bypass security checks (act as if it was from 127.0.0.1 by using
525 * IPv6 src ::ffff:127.0.0.1). Be cautious.
527 * This check chokes if we are in an SIIT cloud. As none of BSDs
528 * support IPv4-less kernel compilation, we cannot support SIIT
529 * environment at all. So, it makes more sense for us to reject any
530 * malicious packets for non-SIIT environment, than try to do a
531 * partical support for SIIT environment.
533 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
534 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
535 ip6stat
.ip6s_badscope
++;
536 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
541 * Reject packets with IPv4 compatible addresses (auto tunnel).
543 * The code forbids auto tunnel relay case in RFC1933 (the check is
544 * stronger than RFC1933). We may want to re-enable it if mech-xx
545 * is revised to forbid relaying case.
547 if (IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_src
) ||
548 IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_dst
)) {
549 ip6stat
.ip6s_badscope
++;
550 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
556 /* Invoke inbound packet filter */
557 lck_mtx_unlock(ip6_mutex
);
558 if (pf_af_hook(m
->m_pkthdr
.rcvif
, NULL
, &m
, AF_INET6
, TRUE
) != 0) {
560 panic("%s: unexpected packet %p\n", __func__
, m
);
563 /* Already freed by callee */
566 ip6
= mtod(m
, struct ip6_hdr
*);
567 lck_mtx_lock(ip6_mutex
);
570 /* drop packets if interface ID portion is already filled */
571 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
572 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
) &&
573 ip6
->ip6_src
.s6_addr16
[1]) {
574 ip6stat
.ip6s_badscope
++;
577 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
578 ip6
->ip6_dst
.s6_addr16
[1]) {
579 ip6stat
.ip6s_badscope
++;
584 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
585 ip6
->ip6_src
.s6_addr16
[1]
586 = htons(m
->m_pkthdr
.rcvif
->if_index
);
587 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
588 ip6
->ip6_dst
.s6_addr16
[1]
589 = htons(m
->m_pkthdr
.rcvif
->if_index
);
591 #if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */
593 * We use rt->rt_ifp to determine if the address is ours or not.
594 * If rt_ifp is lo0, the address is ours.
595 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
596 * so any address under fe80::%lo0/64 will be mistakenly considered
597 * local. The special case is supplied to handle the case properly
598 * by actually looking at interface addresses
599 * (using in6ifa_ifpwithaddr).
601 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) != 0 &&
602 IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_dst
)) {
603 struct in6_ifaddr
*ia6
;
604 if (!(ia6
= in6ifa_ifpwithaddr(m
->m_pkthdr
.rcvif
, &ip6
->ip6_dst
))) {
605 lck_mtx_unlock(ip6_mutex
);
606 icmp6_error(m
, ICMP6_DST_UNREACH
,
607 ICMP6_DST_UNREACH_ADDR
, 0);
608 /* m is already freed */
611 ifafree(&ia6
->ia_ifa
);
614 deliverifp
= m
->m_pkthdr
.rcvif
;
622 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
623 struct in6_multi
*in6m
= 0;
624 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
626 in6_ifstat_inc(ifp
, ifs6_in_mcast
);
628 * See if we belong to the destination multicast group on the
631 ifnet_lock_shared(ifp
);
632 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, ifp
, in6m
);
633 ifnet_lock_done(ifp
);
636 else if (!ip6_mrouter
) {
637 ip6stat
.ip6s_notmember
++;
638 ip6stat
.ip6s_cantforward
++;
639 in6_ifstat_inc(ifp
, ifs6_in_discard
);
646 if (ip6_forward_rt
.ro_rt
!= NULL
)
647 RT_LOCK(ip6_forward_rt
.ro_rt
);
651 if (ip6_forward_rt
.ro_rt
!= NULL
&&
652 (ip6_forward_rt
.ro_rt
->rt_flags
& RTF_UP
) &&
653 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
654 &((struct sockaddr_in6
*)(&ip6_forward_rt
.ro_dst
))->sin6_addr
) &&
655 ip6_forward_rt
.ro_rt
->generation_id
== route_generation
) {
656 ip6stat
.ip6s_forward_cachehit
++;
658 struct sockaddr_in6
*dst6
;
660 if (ip6_forward_rt
.ro_rt
!= NULL
) {
661 /* route is down/stale or destination is different */
662 ip6stat
.ip6s_forward_cachemiss
++;
663 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
664 rtfree(ip6_forward_rt
.ro_rt
);
665 ip6_forward_rt
.ro_rt
= NULL
;
668 bzero(&ip6_forward_rt
.ro_dst
, sizeof(struct sockaddr_in6
));
669 dst6
= (struct sockaddr_in6
*)&ip6_forward_rt
.ro_dst
;
670 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
671 dst6
->sin6_family
= AF_INET6
;
672 dst6
->sin6_addr
= ip6
->ip6_dst
;
674 ip6_forward_rt
.ro_dst
.sin6_scope_id
=
675 in6_addr2scopeid(m
->m_pkthdr
.rcvif
, &ip6
->ip6_dst
);
678 rtalloc_ign((struct route
*)&ip6_forward_rt
, RTF_PRCLONING
);
679 if (ip6_forward_rt
.ro_rt
!= NULL
)
680 RT_LOCK(ip6_forward_rt
.ro_rt
);
683 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
686 * Accept the packet if the forwarding interface to the destination
687 * according to the routing table is the loopback interface,
688 * unless the associated route has a gateway.
689 * Note that this approach causes to accept a packet if there is a
690 * route to the loopback interface for the destination of the packet.
691 * But we think it's even useful in some situations, e.g. when using
692 * a special daemon which wants to intercept the packet.
694 * XXX: some OSes automatically make a cloned route for the destination
695 * of an outgoing packet. If the outgoing interface of the packet
696 * is a loopback one, the kernel would consider the packet to be
697 * accepted, even if we have no such address assinged on the interface.
698 * We check the cloned flag of the route entry to reject such cases,
699 * assuming that route entries for our own addresses are not made by
700 * cloning (it should be true because in6_addloop explicitly installs
701 * the host route). However, we might have to do an explicit check
702 * while it would be less efficient. Or, should we rather install a
703 * reject route for such a case?
705 if (ip6_forward_rt
.ro_rt
!= NULL
&&
706 (ip6_forward_rt
.ro_rt
->rt_flags
&
707 (RTF_HOST
|RTF_GATEWAY
)) == RTF_HOST
&&
709 !(ip6_forward_rt
.ro_rt
->rt_flags
& RTF_WASCLONED
) &&
713 * The check below is redundant since the comparison of
714 * the destination and the key of the rtentry has
715 * already done through looking up the routing table.
717 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
718 &rt6_key(ip6_forward_rt
.ro_rt
)->sin6_addr
)
720 ip6_forward_rt
.ro_rt
->rt_ifp
->if_type
== IFT_LOOP
) {
721 struct in6_ifaddr
*ia6
=
722 (struct in6_ifaddr
*)ip6_forward_rt
.ro_rt
->rt_ifa
;
725 * record address information into m_aux.
727 (void)ip6_setdstifaddr(m
, ia6
);
730 * packets to a tentative, duplicated, or somehow invalid
731 * address must not be accepted.
733 if (!(ia6
->ia6_flags
& IN6_IFF_NOTREADY
)) {
734 /* this address is ready */
736 deliverifp
= ia6
->ia_ifp
; /* correct? */
737 /* Count the packet in the ip address stats */
740 ia6
->ia_ifa
.if_ipackets
++;
741 ia6
->ia_ifa
.if_ibytes
+= m
->m_pkthdr
.len
;
743 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
746 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
747 /* address is not ready, so discard the packet. */
749 "ip6_input: packet to an unready address %s->%s\n",
750 ip6_sprintf(&ip6
->ip6_src
),
751 ip6_sprintf(&ip6
->ip6_dst
)));
757 * FAITH(Firewall Aided Internet Translator)
759 #if defined(NFAITH) && 0 < NFAITH
761 if (ip6_forward_rt
.ro_rt
&& ip6_forward_rt
.ro_rt
->rt_ifp
762 && ip6_forward_rt
.ro_rt
->rt_ifp
->if_type
== IFT_FAITH
) {
763 /* XXX do we need more sanity checks? */
765 deliverifp
= ip6_forward_rt
.ro_rt
->rt_ifp
; /* faith */
766 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
771 if (ip6_forward_rt
.ro_rt
!= NULL
)
772 RT_UNLOCK(ip6_forward_rt
.ro_rt
);
775 * Now there is no reason to process the packet if it's not our own
776 * and we're not a router.
778 if (!ip6_forwarding
) {
779 ip6stat
.ip6s_cantforward
++;
780 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
786 * record address information into m_aux, if we don't have one yet.
787 * note that we are unable to record it, if the address is not listed
788 * as our interface address (e.g. multicast addresses, addresses
789 * within FAITH prefixes and such).
791 if (deliverifp
&& !ip6_getdstifaddr(m
)) {
792 struct in6_ifaddr
*ia6
;
794 ia6
= in6_ifawithifp(deliverifp
, &ip6
->ip6_dst
);
796 if (!ip6_setdstifaddr(m
, ia6
)) {
798 * XXX maybe we should drop the packet here,
799 * as we could not provide enough information
800 * to the upper layers.
803 ifafree(&ia6
->ia_ifa
);
808 * Process Hop-by-Hop options header if it's contained.
809 * m may be modified in ip6_hopopts_input().
810 * If a JumboPayload option is included, plen will also be modified.
812 plen
= (u_int32_t
)ntohs(ip6
->ip6_plen
);
813 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
816 if (ip6_hopopts_input(&plen
, &rtalert
, &m
, &off
)) {
817 #if 0 /*touches NULL pointer*/
818 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
820 lck_mtx_unlock(ip6_mutex
);
821 return; /* m have already been freed */
825 ip6
= mtod(m
, struct ip6_hdr
*);
828 * if the payload length field is 0 and the next header field
829 * indicates Hop-by-Hop Options header, then a Jumbo Payload
830 * option MUST be included.
832 if (ip6
->ip6_plen
== 0 && plen
== 0) {
834 * Note that if a valid jumbo payload option is
835 * contained, ip6_hoptops_input() must set a valid
836 * (non-zero) payload length to the variable plen.
838 ip6stat
.ip6s_badoptions
++;
839 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
840 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
841 lck_mtx_unlock(ip6_mutex
);
842 icmp6_error(m
, ICMP6_PARAM_PROB
,
843 ICMP6_PARAMPROB_HEADER
,
844 (caddr_t
)&ip6
->ip6_plen
- (caddr_t
)ip6
);
847 #ifndef PULLDOWN_TEST
848 /* ip6_hopopts_input() ensures that mbuf is contiguous */
849 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
851 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
, sizeof(struct ip6_hdr
),
852 sizeof(struct ip6_hbh
));
854 ip6stat
.ip6s_tooshort
++;
855 lck_mtx_unlock(ip6_mutex
);
862 * accept the packet if a router alert option is included
863 * and we act as an IPv6 router.
865 if (rtalert
!= ~0 && ip6_forwarding
)
871 * Check that the amount of data in the buffers
872 * is as at least much as the IPv6 header would have us expect.
873 * Trim mbufs if longer than we expect.
874 * Drop packet if shorter than we expect.
876 if (m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
) < plen
) {
877 ip6stat
.ip6s_tooshort
++;
878 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_truncated
);
881 if (m
->m_pkthdr
.len
> sizeof(struct ip6_hdr
) + plen
) {
882 if (m
->m_len
== m
->m_pkthdr
.len
) {
883 m
->m_len
= sizeof(struct ip6_hdr
) + plen
;
884 m
->m_pkthdr
.len
= sizeof(struct ip6_hdr
) + plen
;
886 m_adj(m
, sizeof(struct ip6_hdr
) + plen
- m
->m_pkthdr
.len
);
890 * Forward if desirable.
892 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
894 * If we are acting as a multicast router, all
895 * incoming multicast packets are passed to the
896 * kernel-level multicast forwarding function.
897 * The packet is returned (relatively) intact; if
898 * ip6_mforward() returns a non-zero value, the packet
899 * must be discarded, else it may be accepted below.
901 if (ip6_mrouter
&& ip6_mforward(ip6
, m
->m_pkthdr
.rcvif
, m
)) {
902 ip6stat
.ip6s_cantforward
++;
904 lck_mtx_unlock(ip6_mutex
);
909 lck_mtx_unlock(ip6_mutex
);
913 ip6_forward(m
, &ip6_forward_rt
, 0, 1);
914 lck_mtx_unlock(ip6_mutex
);
918 ip6
= mtod(m
, struct ip6_hdr
*);
921 * Malicious party may be able to use IPv4 mapped addr to confuse
922 * tcp/udp stack and bypass security checks (act as if it was from
923 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
925 * For SIIT end node behavior, you may want to disable the check.
926 * However, you will become vulnerable to attacks using IPv4 mapped
929 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
930 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
931 ip6stat
.ip6s_badscope
++;
932 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
937 * Tell launch routine the next header
939 ip6stat
.ip6s_delivered
++;
940 in6_ifstat_inc(deliverifp
, ifs6_in_deliver
);
942 lck_mtx_unlock(ip6_mutex
);
946 while (nxt
!= IPPROTO_DONE
) {
947 struct ipfilter
*filter
;
949 if (ip6_hdrnestlimit
&& (++nest
> ip6_hdrnestlimit
)) {
950 ip6stat
.ip6s_toomanyhdr
++;
955 * protection against faulty packet - there should be
956 * more sanity checks in header chain processing.
958 if (m
->m_pkthdr
.len
< off
) {
959 ip6stat
.ip6s_tooshort
++;
960 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_truncated
);
966 * do we need to do it for every header? yeah, other
967 * functions can play with it (like re-allocate and copy).
969 mhist
= ip6_addaux(m
);
970 if (mhist
&& M_TRAILINGSPACE(mhist
) >= sizeof(nxt
)) {
971 hist
= mtod(mhist
, caddr_t
) + mhist
->m_len
;
972 bcopy(&nxt
, hist
, sizeof(nxt
));
973 mhist
->m_len
+= sizeof(nxt
);
975 ip6stat
.ip6s_toomanyhdr
++;
982 * enforce IPsec policy checking if we are seeing last header.
983 * note that we do not visit this with protocols with pcb layer
984 * code - like udp/tcp/raw ip.
986 if ((ipsec_bypass
== 0) && (ip6_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0) {
987 if (ipsec6_in_reject(m
, NULL
)) {
988 IPSEC_STAT_INCREMENT(ipsec6stat
.in_polvio
);
997 if (!TAILQ_EMPTY(&ipv6_filters
)) {
999 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
1001 if ((struct ipfilter
*)inject_ipfref
== filter
)
1003 } else if (filter
->ipf_filter
.ipf_input
) {
1006 result
= filter
->ipf_filter
.ipf_input(
1007 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, off
, nxt
);
1008 if (result
== EJUSTRETURN
) {
1021 if (!(ip6_protox
[nxt
]->pr_flags
& PR_PROTOLOCK
)) {
1022 lck_mtx_lock(inet6_domain_mutex
);
1023 nxt
= (*ip6_protox
[nxt
]->pr_input
)(&m
, &off
);
1024 lck_mtx_unlock(inet6_domain_mutex
);
1027 nxt
= (*ip6_protox
[nxt
]->pr_input
)(&m
, &off
);
1031 lck_mtx_unlock(ip6_mutex
);
1038 * set/grab in6_ifaddr correspond to IPv6 destination address.
1039 * XXX backward compatibility wrapper
1041 static struct ip6aux
*
1042 ip6_setdstifaddr(struct mbuf
*m
, struct in6_ifaddr
*ia6
)
1048 n
->ip6a_dstia6
= ia6
;
1049 return (struct ip6aux
*)n
; /* NULL if failed to set */
1060 return n
->ip6a_dstia6
;
1066 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1067 * included, the real payload length will be stored in plenp.
1070 ip6_hopopts_input(plenp
, rtalertp
, mp
, offp
)
1072 u_int32_t
*rtalertp
; /* XXX: should be stored more smart way */
1076 struct mbuf
*m
= *mp
;
1077 int off
= *offp
, hbhlen
;
1078 struct ip6_hbh
*hbh
;
1081 /* validation of the length of the header */
1082 #ifndef PULLDOWN_TEST
1083 IP6_EXTHDR_CHECK(m
, off
, sizeof(*hbh
), return -1);
1084 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1085 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1087 IP6_EXTHDR_CHECK(m
, off
, hbhlen
, return -1);
1088 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1090 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
,
1091 sizeof(struct ip6_hdr
), sizeof(struct ip6_hbh
));
1093 ip6stat
.ip6s_tooshort
++;
1096 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1097 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
, sizeof(struct ip6_hdr
),
1100 ip6stat
.ip6s_tooshort
++;
1105 hbhlen
-= sizeof(struct ip6_hbh
);
1106 opt
= (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
);
1108 if (ip6_process_hopopts(m
, (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
),
1109 hbhlen
, rtalertp
, plenp
) < 0)
1118 * Search header for all Hop-by-hop options and process each option.
1119 * This function is separate from ip6_hopopts_input() in order to
1120 * handle a case where the sending node itself process its hop-by-hop
1121 * options header. In such a case, the function is called from ip6_output().
1123 * The function assumes that hbh header is located right after the IPv6 header
1124 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1125 * opthead + hbhlen is located in continuous memory region.
1128 ip6_process_hopopts(m
, opthead
, hbhlen
, rtalertp
, plenp
)
1132 u_int32_t
*rtalertp
;
1135 struct ip6_hdr
*ip6
;
1137 u_int8_t
*opt
= opthead
;
1138 u_int16_t rtalert_val
;
1139 u_int32_t jumboplen
;
1140 const int erroff
= sizeof(struct ip6_hdr
) + sizeof(struct ip6_hbh
);
1142 for (; hbhlen
> 0; hbhlen
-= optlen
, opt
+= optlen
) {
1148 if (hbhlen
< IP6OPT_MINLEN
) {
1149 ip6stat
.ip6s_toosmall
++;
1152 optlen
= *(opt
+ 1) + 2;
1154 case IP6OPT_RTALERT
:
1155 /* XXX may need check for alignment */
1156 if (hbhlen
< IP6OPT_RTALERT_LEN
) {
1157 ip6stat
.ip6s_toosmall
++;
1160 if (*(opt
+ 1) != IP6OPT_RTALERT_LEN
- 2) {
1162 lck_mtx_unlock(ip6_mutex
);
1163 icmp6_error(m
, ICMP6_PARAM_PROB
,
1164 ICMP6_PARAMPROB_HEADER
,
1165 erroff
+ opt
+ 1 - opthead
);
1166 lck_mtx_lock(ip6_mutex
);
1169 optlen
= IP6OPT_RTALERT_LEN
;
1170 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&rtalert_val
, 2);
1171 *rtalertp
= ntohs(rtalert_val
);
1174 /* XXX may need check for alignment */
1175 if (hbhlen
< IP6OPT_JUMBO_LEN
) {
1176 ip6stat
.ip6s_toosmall
++;
1179 if (*(opt
+ 1) != IP6OPT_JUMBO_LEN
- 2) {
1181 lck_mtx_unlock(ip6_mutex
);
1182 icmp6_error(m
, ICMP6_PARAM_PROB
,
1183 ICMP6_PARAMPROB_HEADER
,
1184 erroff
+ opt
+ 1 - opthead
);
1185 lck_mtx_lock(ip6_mutex
);
1188 optlen
= IP6OPT_JUMBO_LEN
;
1191 * IPv6 packets that have non 0 payload length
1192 * must not contain a jumbo payload option.
1194 ip6
= mtod(m
, struct ip6_hdr
*);
1195 if (ip6
->ip6_plen
) {
1196 ip6stat
.ip6s_badoptions
++;
1197 lck_mtx_unlock(ip6_mutex
);
1198 icmp6_error(m
, ICMP6_PARAM_PROB
,
1199 ICMP6_PARAMPROB_HEADER
,
1200 erroff
+ opt
- opthead
);
1201 lck_mtx_lock(ip6_mutex
);
1206 * We may see jumbolen in unaligned location, so
1207 * we'd need to perform bcopy().
1209 bcopy(opt
+ 2, &jumboplen
, sizeof(jumboplen
));
1210 jumboplen
= (u_int32_t
)htonl(jumboplen
);
1214 * if there are multiple jumbo payload options,
1215 * *plenp will be non-zero and the packet will be
1217 * the behavior may need some debate in ipngwg -
1218 * multiple options does not make sense, however,
1219 * there's no explicit mention in specification.
1222 ip6stat
.ip6s_badoptions
++;
1223 lck_mtx_unlock(ip6_mutex
);
1224 icmp6_error(m
, ICMP6_PARAM_PROB
,
1225 ICMP6_PARAMPROB_HEADER
,
1226 erroff
+ opt
+ 2 - opthead
);
1227 lck_mtx_lock(ip6_mutex
);
1233 * jumbo payload length must be larger than 65535.
1235 if (jumboplen
<= IPV6_MAXPACKET
) {
1236 ip6stat
.ip6s_badoptions
++;
1237 lck_mtx_unlock(ip6_mutex
);
1238 icmp6_error(m
, ICMP6_PARAM_PROB
,
1239 ICMP6_PARAMPROB_HEADER
,
1240 erroff
+ opt
+ 2 - opthead
);
1241 lck_mtx_lock(ip6_mutex
);
1247 default: /* unknown option */
1248 if (hbhlen
< IP6OPT_MINLEN
) {
1249 ip6stat
.ip6s_toosmall
++;
1252 optlen
= ip6_unknown_opt(opt
, m
,
1253 erroff
+ opt
- opthead
, 1);
1255 /* ip6_unknown opt unlocked ip6_mutex */
1271 * Unknown option processing.
1272 * The third argument `off' is the offset from the IPv6 header to the option,
1273 * which is necessary if the IPv6 header the and option header and IPv6 header
1274 * is not continuous in order to return an ICMPv6 error.
1277 ip6_unknown_opt(optp
, m
, off
, locked
)
1283 struct ip6_hdr
*ip6
;
1285 switch (IP6OPT_TYPE(*optp
)) {
1286 case IP6OPT_TYPE_SKIP
: /* ignore the option */
1287 return((int)*(optp
+ 1));
1288 case IP6OPT_TYPE_DISCARD
: /* silently discard */
1291 case IP6OPT_TYPE_FORCEICMP
: /* send ICMP even if multicasted */
1292 ip6stat
.ip6s_badoptions
++;
1294 lck_mtx_unlock(ip6_mutex
);
1295 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_OPTION
, off
);
1297 lck_mtx_lock(ip6_mutex
);
1299 case IP6OPT_TYPE_ICMP
: /* send ICMP if not multicasted */
1300 ip6stat
.ip6s_badoptions
++;
1301 ip6
= mtod(m
, struct ip6_hdr
*);
1302 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
1303 (m
->m_flags
& (M_BCAST
|M_MCAST
)))
1307 lck_mtx_unlock(ip6_mutex
);
1308 icmp6_error(m
, ICMP6_PARAM_PROB
,
1309 ICMP6_PARAMPROB_OPTION
, off
);
1311 lck_mtx_lock(ip6_mutex
);
1316 m_freem(m
); /* XXX: NOTREACHED */
1321 * Create the "control" list for this pcb.
1322 * The function will not modify mbuf chain at all.
1324 * with KAME mbuf chain restriction:
1325 * The routine will be called from upper layer handlers like tcp6_input().
1326 * Thus the routine assumes that the caller (tcp6_input) have already
1327 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1328 * very first mbuf on the mbuf chain.
1331 ip6_savecontrol(in6p
, mp
, ip6
, m
)
1334 struct ip6_hdr
*ip6
;
1337 int rthdr_exist
= 0;
1340 if ((in6p
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0) {
1344 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
1345 SCM_TIMESTAMP
, SOL_SOCKET
);
1347 mp
= &(*mp
)->m_next
;
1352 /* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
1353 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
)
1356 /* RFC 2292 sec. 5 */
1357 if ((in6p
->in6p_flags
& IN6P_PKTINFO
) != 0) {
1358 struct in6_pktinfo pi6
;
1359 bcopy(&ip6
->ip6_dst
, &pi6
.ipi6_addr
, sizeof(struct in6_addr
));
1360 if (IN6_IS_SCOPE_LINKLOCAL(&pi6
.ipi6_addr
))
1361 pi6
.ipi6_addr
.s6_addr16
[1] = 0;
1362 pi6
.ipi6_ifindex
= (m
&& m
->m_pkthdr
.rcvif
)
1363 ? m
->m_pkthdr
.rcvif
->if_index
1365 *mp
= sbcreatecontrol((caddr_t
) &pi6
,
1366 sizeof(struct in6_pktinfo
), IPV6_PKTINFO
,
1369 mp
= &(*mp
)->m_next
;
1372 if ((in6p
->in6p_flags
& IN6P_HOPLIMIT
) != 0) {
1373 int hlim
= ip6
->ip6_hlim
& 0xff;
1374 *mp
= sbcreatecontrol((caddr_t
) &hlim
,
1375 sizeof(int), IPV6_HOPLIMIT
, IPPROTO_IPV6
);
1377 mp
= &(*mp
)->m_next
;
1380 if ((in6p
->in6p_flags
& IN6P_TCLASS
) != 0) {
1384 flowinfo
= (u_int32_t
)ntohl(ip6
->ip6_flow
& IPV6_FLOWINFO_MASK
);
1387 tclass
= flowinfo
& 0xff;
1388 *mp
= sbcreatecontrol((caddr_t
) &tclass
, sizeof(tclass
),
1389 IPV6_TCLASS
, IPPROTO_IPV6
);
1391 mp
= &(*mp
)->m_next
;
1395 * IPV6_HOPOPTS socket option. Recall that we required super-user
1396 * privilege for the option (see ip6_ctloutput), but it might be too
1397 * strict, since there might be some hop-by-hop options which can be
1398 * returned to normal user.
1399 * See RFC 2292 section 6.
1401 if ((in6p
->in6p_flags
& IN6P_HOPOPTS
) != 0) {
1403 * Check if a hop-by-hop options header is contatined in the
1404 * received packet, and if so, store the options as ancillary
1405 * data. Note that a hop-by-hop options header must be
1406 * just after the IPv6 header, which fact is assured through
1407 * the IPv6 input processing.
1409 ip6
= mtod(m
, struct ip6_hdr
*);
1410 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
1411 struct ip6_hbh
*hbh
;
1417 #ifndef PULLDOWN_TEST
1418 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
1419 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1421 ext
= ip6_pullexthdr(m
, sizeof(struct ip6_hdr
),
1424 ip6stat
.ip6s_tooshort
++;
1427 hbh
= mtod(ext
, struct ip6_hbh
*);
1428 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1429 if (hbhlen
!= ext
->m_len
) {
1431 ip6stat
.ip6s_tooshort
++;
1437 * XXX: We copy whole the header even if a jumbo
1438 * payload option is included, which option is to
1439 * be removed before returning in the RFC 2292.
1440 * Note: this constraint is removed in 2292bis.
1442 *mp
= sbcreatecontrol((caddr_t
)hbh
, hbhlen
,
1443 IPV6_HOPOPTS
, IPPROTO_IPV6
);
1445 mp
= &(*mp
)->m_next
;
1452 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1453 if ((in6p
->in6p_flags
& (IN6P_DSTOPTS
| IN6P_RTHDRDSTOPTS
)) != 0) {
1454 int proto
, off
, nxt
;
1457 * go through the header chain to see if a routing header is
1458 * contained in the packet. We need this information to store
1459 * destination options headers (if any) properly.
1460 * XXX: performance issue. We should record this info when
1461 * processing extension headers in incoming routine.
1464 proto
= IPPROTO_IPV6
;
1470 newoff
= ip6_nexthdr(m
, off
, proto
, &nxt
);
1473 if (newoff
< off
) /* invalid, check for safety */
1475 if ((proto
= nxt
) == IPPROTO_ROUTING
) {
1483 if ((in6p
->in6p_flags
&
1484 (IN6P_RTHDR
| IN6P_DSTOPTS
| IN6P_RTHDRDSTOPTS
)) != 0) {
1485 ip6
= mtod(m
, struct ip6_hdr
*);
1486 int nxt
= ip6
->ip6_nxt
, off
= sizeof(struct ip6_hdr
);
1489 * Search for destination options headers or routing
1490 * header(s) through the header chain, and stores each
1491 * header as ancillary data.
1492 * Note that the order of the headers remains in
1493 * the chain of ancillary data.
1495 while (1) { /* is explicit loop prevention necessary? */
1496 struct ip6_ext
*ip6e
= NULL
;
1499 struct mbuf
*ext
= NULL
;
1503 * if it is not an extension header, don't try to
1504 * pull it from the chain.
1507 case IPPROTO_DSTOPTS
:
1508 case IPPROTO_ROUTING
:
1509 case IPPROTO_HOPOPTS
:
1510 case IPPROTO_AH
: /* is it possible? */
1516 #ifndef PULLDOWN_TEST
1517 if (off
+ sizeof(*ip6e
) > m
->m_len
)
1519 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + off
);
1520 if (nxt
== IPPROTO_AH
)
1521 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1523 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1524 if (off
+ elen
> m
->m_len
)
1527 ext
= ip6_pullexthdr(m
, off
, nxt
);
1529 ip6stat
.ip6s_tooshort
++;
1532 ip6e
= mtod(ext
, struct ip6_ext
*);
1533 if (nxt
== IPPROTO_AH
)
1534 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1536 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1537 if (elen
!= ext
->m_len
) {
1539 ip6stat
.ip6s_tooshort
++;
1545 case IPPROTO_DSTOPTS
:
1546 if ((in6p
->in6p_flags
& IN6P_DSTOPTS
) == 0)
1549 *mp
= sbcreatecontrol((caddr_t
)ip6e
, elen
,
1553 mp
= &(*mp
)->m_next
;
1555 case IPPROTO_ROUTING
:
1556 if (!in6p
->in6p_flags
& IN6P_RTHDR
)
1559 *mp
= sbcreatecontrol((caddr_t
)ip6e
, elen
,
1563 mp
= &(*mp
)->m_next
;
1565 case IPPROTO_HOPOPTS
:
1566 case IPPROTO_AH
: /* is it possible? */
1571 * other cases have been filtered in the above.
1572 * none will visit this case. here we supply
1573 * the code just in case (nxt overwritten or
1583 /* proceed with the next header. */
1585 nxt
= ip6e
->ip6e_nxt
;
1600 * pull single extension header from mbuf chain. returns single mbuf that
1601 * contains the result, or NULL on error.
1603 static struct mbuf
*
1604 ip6_pullexthdr(m
, off
, nxt
)
1609 struct ip6_ext ip6e
;
1615 case IPPROTO_DSTOPTS
:
1616 case IPPROTO_ROUTING
:
1617 case IPPROTO_HOPOPTS
:
1618 case IPPROTO_AH
: /* is it possible? */
1621 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt
);
1625 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1626 if (nxt
== IPPROTO_AH
)
1627 elen
= (ip6e
.ip6e_len
+ 2) << 2;
1629 elen
= (ip6e
.ip6e_len
+ 1) << 3;
1631 MGET(n
, M_DONTWAIT
, MT_DATA
);
1632 if (n
&& elen
>= MLEN
) {
1633 MCLGET(n
, M_DONTWAIT
);
1634 if ((n
->m_flags
& M_EXT
) == 0) {
1643 if (elen
>= M_TRAILINGSPACE(n
)) {
1648 m_copydata(m
, off
, elen
, mtod(n
, caddr_t
));
1655 * Get pointer to the previous header followed by the header
1656 * currently processed.
1657 * XXX: This function supposes that
1658 * M includes all headers,
1659 * the next header field and the header length field of each header
1661 * the sum of each header length equals to OFF.
1662 * Because of these assumptions, this function must be called very
1663 * carefully. Moreover, it will not be used in the near future when
1664 * we develop `neater' mechanism to process extension headers.
1667 ip6_get_prevhdr(m
, off
)
1671 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1673 if (off
== sizeof(struct ip6_hdr
))
1674 return((char *) &ip6
->ip6_nxt
);
1677 struct ip6_ext
*ip6e
= NULL
;
1680 len
= sizeof(struct ip6_hdr
);
1682 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + len
);
1685 case IPPROTO_FRAGMENT
:
1686 len
+= sizeof(struct ip6_frag
);
1689 len
+= (ip6e
->ip6e_len
+ 2) << 2;
1692 len
+= (ip6e
->ip6e_len
+ 1) << 3;
1695 nxt
= ip6e
->ip6e_nxt
;
1698 return((char *) &ip6e
->ip6e_nxt
);
1705 * get next header offset. m will be retained.
1708 ip6_nexthdr(m
, off
, proto
, nxtp
)
1715 struct ip6_ext ip6e
;
1720 panic("ip6_nexthdr: m == NULL");
1721 if ((m
->m_flags
& M_PKTHDR
) == 0 || m
->m_pkthdr
.len
< off
)
1726 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6
))
1728 m_copydata(m
, off
, sizeof(ip6
), (caddr_t
)&ip6
);
1730 *nxtp
= ip6
.ip6_nxt
;
1734 case IPPROTO_FRAGMENT
:
1736 * terminate parsing if it is not the first fragment,
1737 * it does not make sense to parse through it.
1739 if (m
->m_pkthdr
.len
< off
+ sizeof(fh
))
1741 m_copydata(m
, off
, sizeof(fh
), (caddr_t
)&fh
);
1742 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1743 if (fh
.ip6f_offlg
& IP6F_OFF_MASK
)
1746 *nxtp
= fh
.ip6f_nxt
;
1747 off
+= sizeof(struct ip6_frag
);
1751 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
))
1753 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1755 *nxtp
= ip6e
.ip6e_nxt
;
1756 off
+= (ip6e
.ip6e_len
+ 2) << 2;
1759 case IPPROTO_HOPOPTS
:
1760 case IPPROTO_ROUTING
:
1761 case IPPROTO_DSTOPTS
:
1762 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
))
1764 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1766 *nxtp
= ip6e
.ip6e_nxt
;
1767 off
+= (ip6e
.ip6e_len
+ 1) << 3;
1772 case IPPROTO_IPCOMP
:
1784 * get offset for the last header in the chain. m will be kept untainted.
1787 ip6_lasthdr(m
, off
, proto
, nxtp
)
1801 newoff
= ip6_nexthdr(m
, off
, proto
, nxtp
);
1804 else if (newoff
< off
)
1805 return -1; /* invalid */
1806 else if (newoff
== off
)
1820 /* Check if one is already allocated */
1821 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_INET6
, NULL
);
1823 /* Allocate a tag */
1824 tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_INET6
,
1825 sizeof (struct ip6aux
), M_DONTWAIT
);
1827 /* Attach it to the mbuf */
1829 m_tag_prepend(m
, tag
);
1833 return tag
? (struct ip6aux
*)(tag
+ 1) : NULL
;
1842 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_ENCAP
, NULL
);
1844 return tag
? (struct ip6aux
*)(tag
+ 1) : NULL
;
1853 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_ENCAP
, NULL
);
1855 m_tag_delete(m
, tag
);
1860 * System control for IP6
1863 u_char inet6ctlerrmap
[PRC_NCMDS
] = {
1865 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
1866 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
1867 EMSGSIZE
, EHOSTUNREACH
, 0, 0,