1 /* $FreeBSD: src/sys/netinet6/ip6_input.c,v 1.11.2.10 2001/07/24 19:10:18 brooks Exp $ */
2 /* $KAME: ip6_input.c,v 1.194 2001/05/27 13:28:35 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
73 #include <sys/domain.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/errno.h>
79 #include <sys/kernel.h>
80 #include <sys/syslog.h>
82 #include <sys/kauth.h>
85 #include <net/if_var.h>
86 #include <net/if_types.h>
87 #include <net/if_dl.h>
88 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip_icmp.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/in6_var.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet/in_pcb.h>
100 #include <netinet/icmp6.h>
101 #include <netinet6/in6_ifattach.h>
102 #include <netinet6/nd6.h>
103 #include <netinet6/in6_prefix.h>
106 #include <netinet6/ipsec.h>
108 #include <netinet6/ipsec6.h>
110 extern int ipsec_bypass
;
111 extern lck_mtx_t
*sadb_mutex
;
114 #include <netinet6/ip6_fw.h>
116 #include <netinet/kpi_ipfilter_var.h>
118 #include <netinet6/ip6protosw.h>
120 /* we need it for NLOOP. */
124 #include <net/net_osdep.h>
126 extern struct domain inet6domain
;
127 extern struct ip6protosw inet6sw
[];
129 struct ip6protosw
* ip6_protox
[IPPROTO_MAX
];
130 static int ip6qmaxlen
= IFQ_MAXLEN
;
131 struct in6_ifaddr
*in6_ifaddrs
;
133 int ip6_forward_srcrt
; /* XXX */
134 int ip6_sourcecheck
; /* XXX */
135 int ip6_sourcecheck_interval
; /* XXX */
136 const int int6intrq_present
= 1;
138 int ip6_ours_check_algorithm
;
139 int in6_init2done
= 0;
143 ip6_fw_chk_t
*ip6_fw_chk_ptr
;
144 ip6_fw_ctl_t
*ip6_fw_ctl_ptr
;
145 int ip6_fw_enable
= 1;
147 struct ip6stat ip6stat
;
150 struct ifqueue ip6intrq
;
151 lck_mtx_t
*ip6_mutex
;
152 lck_mtx_t
*dad6_mutex
;
153 lck_mtx_t
*nd6_mutex
;
154 lck_mtx_t
*prefix6_mutex
;
155 lck_attr_t
*ip6_mutex_attr
;
156 lck_grp_t
*ip6_mutex_grp
;
157 lck_grp_attr_t
*ip6_mutex_grp_attr
;
158 extern lck_mtx_t
*inet6_domain_mutex
;
160 extern int loopattach_done
;
162 static void ip6_init2(void *);
163 static struct mbuf
*ip6_setdstifaddr(struct mbuf
*, struct in6_ifaddr
*);
165 static int ip6_hopopts_input(u_int32_t
*, u_int32_t
*, struct mbuf
**, int *);
167 static struct mbuf
*ip6_pullexthdr(struct mbuf
*, size_t, int);
171 void gifattach(void);
172 void faithattach(void);
173 void stfattach(void);
178 protocol_family_t protocol
,
185 * IP6 initialization: fill in IP6 protocol switch table.
186 * All protocols not implemented in kernel go to raw IP6 protocol handler.
191 struct ip6protosw
*pr
;
194 extern lck_mtx_t
*domain_proto_mtx
;
197 if (sizeof(struct protosw
) != sizeof(struct ip6protosw
))
198 panic("sizeof(protosw) != sizeof(ip6protosw)");
200 pr
= (struct ip6protosw
*)pffindproto_locked(PF_INET6
, IPPROTO_RAW
, SOCK_RAW
);
203 for (i
= 0; i
< IPPROTO_MAX
; i
++)
205 for (pr
= (struct ip6protosw
*)inet6domain
.dom_protosw
; pr
; pr
= pr
->pr_next
) {
206 if(!((unsigned int)pr
->pr_domain
)) continue; /* If uninitialized, skip */
207 if (pr
->pr_domain
->dom_family
== PF_INET6
&&
208 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
) {
209 ip6_protox
[pr
->pr_protocol
] = pr
;
213 ip6_mutex_grp_attr
= lck_grp_attr_alloc_init();
215 ip6_mutex_grp
= lck_grp_alloc_init("ip6", ip6_mutex_grp_attr
);
216 ip6_mutex_attr
= lck_attr_alloc_init();
218 if ((ip6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
219 printf("ip6_init: can't alloc ip6_mutex\n");
222 if ((dad6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
223 printf("ip6_init: can't alloc dad6_mutex\n");
226 if ((nd6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
227 printf("ip6_init: can't alloc nd6_mutex\n");
231 if ((prefix6_mutex
= lck_mtx_alloc_init(ip6_mutex_grp
, ip6_mutex_attr
)) == NULL
) {
232 printf("ip6_init: can't alloc prefix6_mutex\n");
236 inet6domain
.dom_flags
= DOM_REENTRANT
;
238 ip6intrq
.ifq_maxlen
= ip6qmaxlen
;
243 * in many cases, random() here does NOT return random number
244 * as initialization during bootstrap time occur in fixed order.
247 ip6_flow_seq
= random() ^ tv
.tv_usec
;
249 ip6_desync_factor
= (random() ^ tv
.tv_usec
) % MAX_TEMP_DESYNC_FACTOR
;
250 timeout(ip6_init2
, (caddr_t
)0, 1 * hz
);
252 lck_mtx_unlock(domain_proto_mtx
);
253 proto_register_input(PF_INET6
, ip6_proto_input
, NULL
);
254 lck_mtx_lock(domain_proto_mtx
);
262 * to route local address of p2p link to loopback,
263 * assign loopback address first.
265 if (loopattach_done
== 0) {
266 timeout(ip6_init2
, (caddr_t
)0, 1 * hz
);
269 in6_ifattach(&loif
[0], NULL
, NULL
);
273 timeout(nd6_timer
, (caddr_t
)0, hz
);
275 /* router renumbering prefix list maintenance */
276 timeout(in6_rr_timer
, (caddr_t
)0, hz
);
278 /* timer for regeneranation of temporary addresses randomize ID */
279 timeout(in6_tmpaddrtimer
, (caddr_t
)0,
280 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
281 ip6_temp_regen_advance
) * hz
);
295 callout_init(&nd6_timer_ch
);
296 callout_reset(&nd6_timer_ch
, hz
, nd6_timer
, NULL
);
298 /* router renumbering prefix list maintenance */
299 callout_init(&in6_rr_timer_ch
);
300 callout_reset(&in6_rr_timer_ch
, hz
, in6_rr_timer
, NULL
);
302 /* timer for regeneranation of temporary addresses randomize ID */
303 callout_reset(&in6_tmpaddrtimer_ch
,
304 (ip6_temp_preferred_lifetime
- ip6_desync_factor
-
305 ip6_temp_regen_advance
) * hz
,
306 in6_tmpaddrtimer
, NULL
);
314 /* This must be after route_init(), which is now SI_ORDER_THIRD */
315 SYSINIT(netinet6init2
, SI_SUB_PROTO_DOMAIN
, SI_ORDER_MIDDLE
, ip6_init2
, NULL
);
318 extern struct route_in6 ip6_forward_rt
;
325 int off
= sizeof(struct ip6_hdr
), nest
;
327 u_int32_t rtalert
= ~0;
328 int nxt
= 0, ours
= 0;
329 struct ifnet
*deliverifp
= NULL
;
330 ipfilter_t inject_ipfref
= 0;
334 * No need to proccess packet twice if we've
337 inject_ipfref
= ipf_get_inject_filter(m
);
338 if (inject_ipfref
!= 0) {
339 ip6
= mtod(m
, struct ip6_hdr
*);
348 * should the inner packet be considered authentic?
349 * see comment in ah4_input().
352 m
->m_flags
&= ~M_AUTHIPHDR
;
353 m
->m_flags
&= ~M_AUTHIPDGM
;
358 * make sure we don't have onion peering information into m_aux.
362 lck_mtx_lock(ip6_mutex
);
366 if (m
->m_flags
& M_EXT
) {
368 ip6stat
.ip6s_mext2m
++;
370 ip6stat
.ip6s_mext1
++;
372 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
374 if (m
->m_flags
& M_LOOP
) {
375 ip6stat
.ip6s_m2m
[loif
[0].if_index
]++; /* XXX */
376 } else if (m
->m_pkthdr
.rcvif
->if_index
< M2MMAX
)
377 ip6stat
.ip6s_m2m
[m
->m_pkthdr
.rcvif
->if_index
]++;
379 ip6stat
.ip6s_m2m
[0]++;
385 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_receive
);
386 ip6stat
.ip6s_total
++;
388 #ifndef PULLDOWN_TEST
390 * L2 bridge code and some other code can return mbuf chain
391 * that does not conform to KAME requirement. too bad.
392 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
394 if (m
&& m
->m_next
!= NULL
&& m
->m_pkthdr
.len
< MCLBYTES
) {
397 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
400 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
401 MCLGET(n
, M_DONTWAIT
);
402 if ((n
->m_flags
& M_EXT
) == 0) {
409 lck_mtx_unlock(ip6_mutex
);
413 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
414 n
->m_len
= m
->m_pkthdr
.len
;
418 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
),
419 {lck_mtx_unlock(ip6_mutex
); return;});
422 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
424 inifp
= m
->m_pkthdr
.rcvif
;
425 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == 0) {
426 ip6stat
.ip6s_toosmall
++;
427 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
428 lck_mtx_unlock(ip6_mutex
);
433 ip6
= mtod(m
, struct ip6_hdr
*);
435 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
436 ip6stat
.ip6s_badvers
++;
437 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
441 ip6stat
.ip6s_nxthist
[ip6
->ip6_nxt
]++;
444 * Check with the firewall...
446 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
448 /* If ipfw says divert, we have to just drop packet */
449 /* use port as a dummy argument */
450 if ((*ip6_fw_chk_ptr
)(&ip6
, NULL
, &port
, &m
)) {
455 lck_mtx_unlock(ip6_mutex
);
461 * Check against address spoofing/corruption.
463 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
) ||
464 IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_dst
)) {
466 * XXX: "badscope" is not very suitable for a multicast source.
468 ip6stat
.ip6s_badscope
++;
469 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
472 if ((IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) ||
473 IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_dst
)) &&
474 (m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
475 ip6stat
.ip6s_badscope
++;
476 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
481 * The following check is not documented in specs. A malicious
482 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
483 * and bypass security checks (act as if it was from 127.0.0.1 by using
484 * IPv6 src ::ffff:127.0.0.1). Be cautious.
486 * This check chokes if we are in an SIIT cloud. As none of BSDs
487 * support IPv4-less kernel compilation, we cannot support SIIT
488 * environment at all. So, it makes more sense for us to reject any
489 * malicious packets for non-SIIT environment, than try to do a
490 * partical support for SIIT environment.
492 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
493 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
494 ip6stat
.ip6s_badscope
++;
495 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
500 * Reject packets with IPv4 compatible addresses (auto tunnel).
502 * The code forbids auto tunnel relay case in RFC1933 (the check is
503 * stronger than RFC1933). We may want to re-enable it if mech-xx
504 * is revised to forbid relaying case.
506 if (IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_src
) ||
507 IN6_IS_ADDR_V4COMPAT(&ip6
->ip6_dst
)) {
508 ip6stat
.ip6s_badscope
++;
509 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
514 /* drop packets if interface ID portion is already filled */
515 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
516 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
) &&
517 ip6
->ip6_src
.s6_addr16
[1]) {
518 ip6stat
.ip6s_badscope
++;
521 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
522 ip6
->ip6_dst
.s6_addr16
[1]) {
523 ip6stat
.ip6s_badscope
++;
528 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
529 ip6
->ip6_src
.s6_addr16
[1]
530 = htons(m
->m_pkthdr
.rcvif
->if_index
);
531 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
532 ip6
->ip6_dst
.s6_addr16
[1]
533 = htons(m
->m_pkthdr
.rcvif
->if_index
);
535 #if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */
537 * We use rt->rt_ifp to determine if the address is ours or not.
538 * If rt_ifp is lo0, the address is ours.
539 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
540 * so any address under fe80::%lo0/64 will be mistakenly considered
541 * local. The special case is supplied to handle the case properly
542 * by actually looking at interface addresses
543 * (using in6ifa_ifpwithaddr).
545 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) != 0 &&
546 IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_dst
)) {
547 struct in6_ifaddr
*ia6
;
548 if (!(ia6
= in6ifa_ifpwithaddr(m
->m_pkthdr
.rcvif
, &ip6
->ip6_dst
))) {
549 lck_mtx_unlock(ip6_mutex
);
550 icmp6_error(m
, ICMP6_DST_UNREACH
,
551 ICMP6_DST_UNREACH_ADDR
, 0);
552 /* m is already freed */
555 ifafree(&ia6
->ia_ifa
);
558 deliverifp
= m
->m_pkthdr
.rcvif
;
566 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
567 struct in6_multi
*in6m
= 0;
569 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mcast
);
571 * See if we belong to the destination multicast group on the
574 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, m
->m_pkthdr
.rcvif
, in6m
);
577 else if (!ip6_mrouter
) {
578 ip6stat
.ip6s_notmember
++;
579 ip6stat
.ip6s_cantforward
++;
580 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
583 deliverifp
= m
->m_pkthdr
.rcvif
;
590 switch (ip6_ours_check_algorithm
) {
593 * XXX: I intentionally broke our indentation rule here,
594 * since this switch-case is just for measurement and
595 * therefore should soon be removed.
597 if (ip6_forward_rt
.ro_rt
!= NULL
&&
598 (ip6_forward_rt
.ro_rt
->rt_flags
& RTF_UP
) != 0 &&
599 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
600 &((struct sockaddr_in6
*)(&ip6_forward_rt
.ro_dst
))->sin6_addr
))
601 ip6stat
.ip6s_forward_cachehit
++;
603 struct sockaddr_in6
*dst6
;
605 if (ip6_forward_rt
.ro_rt
) {
606 /* route is down or destination is different */
607 ip6stat
.ip6s_forward_cachemiss
++;
608 rtfree(ip6_forward_rt
.ro_rt
);
609 ip6_forward_rt
.ro_rt
= 0;
612 bzero(&ip6_forward_rt
.ro_dst
, sizeof(struct sockaddr_in6
));
613 dst6
= (struct sockaddr_in6
*)&ip6_forward_rt
.ro_dst
;
614 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
615 dst6
->sin6_family
= AF_INET6
;
616 dst6
->sin6_addr
= ip6
->ip6_dst
;
618 ip6_forward_rt
.ro_dst
.sin6_scope_id
=
619 in6_addr2scopeid(m
->m_pkthdr
.rcvif
, &ip6
->ip6_dst
);
622 rtalloc_ign((struct route
*)&ip6_forward_rt
, RTF_PRCLONING
);
625 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
628 * Accept the packet if the forwarding interface to the destination
629 * according to the routing table is the loopback interface,
630 * unless the associated route has a gateway.
631 * Note that this approach causes to accept a packet if there is a
632 * route to the loopback interface for the destination of the packet.
633 * But we think it's even useful in some situations, e.g. when using
634 * a special daemon which wants to intercept the packet.
636 * XXX: some OSes automatically make a cloned route for the destination
637 * of an outgoing packet. If the outgoing interface of the packet
638 * is a loopback one, the kernel would consider the packet to be
639 * accepted, even if we have no such address assinged on the interface.
640 * We check the cloned flag of the route entry to reject such cases,
641 * assuming that route entries for our own addresses are not made by
642 * cloning (it should be true because in6_addloop explicitly installs
643 * the host route). However, we might have to do an explicit check
644 * while it would be less efficient. Or, should we rather install a
645 * reject route for such a case?
647 if (ip6_forward_rt
.ro_rt
&&
648 (ip6_forward_rt
.ro_rt
->rt_flags
&
649 (RTF_HOST
|RTF_GATEWAY
)) == RTF_HOST
&&
651 !(ip6_forward_rt
.ro_rt
->rt_flags
& RTF_WASCLONED
) &&
654 !(ip6_forward_rt
.ro_rt
->rt_flags
& RTF_CLONED
) &&
658 * The check below is redundant since the comparison of
659 * the destination and the key of the rtentry has
660 * already done through looking up the routing table.
662 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
663 &rt6_key(ip6_forward_rt
.ro_rt
)->sin6_addr
)
665 ip6_forward_rt
.ro_rt
->rt_ifp
->if_type
== IFT_LOOP
) {
666 struct in6_ifaddr
*ia6
=
667 (struct in6_ifaddr
*)ip6_forward_rt
.ro_rt
->rt_ifa
;
670 * record address information into m_aux.
672 (void)ip6_setdstifaddr(m
, ia6
);
675 * packets to a tentative, duplicated, or somehow invalid
676 * address must not be accepted.
678 if (!(ia6
->ia6_flags
& IN6_IFF_NOTREADY
)) {
679 /* this address is ready */
681 deliverifp
= ia6
->ia_ifp
; /* correct? */
682 /* Count the packet in the ip address stats */
685 ia6
->ia_ifa
.if_ipackets
++;
686 ia6
->ia_ifa
.if_ibytes
+= m
->m_pkthdr
.len
;
690 /* address is not ready, so discard the packet. */
692 "ip6_input: packet to an unready address %s->%s\n",
693 ip6_sprintf(&ip6
->ip6_src
),
694 ip6_sprintf(&ip6
->ip6_dst
)));
698 } /* XXX indentation (see above) */
701 * FAITH(Firewall Aided Internet Translator)
703 #if defined(NFAITH) && 0 < NFAITH
705 if (ip6_forward_rt
.ro_rt
&& ip6_forward_rt
.ro_rt
->rt_ifp
706 && ip6_forward_rt
.ro_rt
->rt_ifp
->if_type
== IFT_FAITH
) {
707 /* XXX do we need more sanity checks? */
709 deliverifp
= ip6_forward_rt
.ro_rt
->rt_ifp
; /* faith */
716 * Now there is no reason to process the packet if it's not our own
717 * and we're not a router.
719 if (!ip6_forwarding
) {
720 ip6stat
.ip6s_cantforward
++;
721 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
727 * record address information into m_aux, if we don't have one yet.
728 * note that we are unable to record it, if the address is not listed
729 * as our interface address (e.g. multicast addresses, addresses
730 * within FAITH prefixes and such).
732 if (deliverifp
&& !ip6_getdstifaddr(m
)) {
733 struct in6_ifaddr
*ia6
;
735 ia6
= in6_ifawithifp(deliverifp
, &ip6
->ip6_dst
);
737 if (!ip6_setdstifaddr(m
, ia6
)) {
739 * XXX maybe we should drop the packet here,
740 * as we could not provide enough information
741 * to the upper layers.
748 * Process Hop-by-Hop options header if it's contained.
749 * m may be modified in ip6_hopopts_input().
750 * If a JumboPayload option is included, plen will also be modified.
752 plen
= (u_int32_t
)ntohs(ip6
->ip6_plen
);
753 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
756 if (ip6_hopopts_input(&plen
, &rtalert
, &m
, &off
)) {
757 #if 0 /*touches NULL pointer*/
758 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
760 lck_mtx_unlock(ip6_mutex
);
761 return; /* m have already been freed */
765 ip6
= mtod(m
, struct ip6_hdr
*);
768 * if the payload length field is 0 and the next header field
769 * indicates Hop-by-Hop Options header, then a Jumbo Payload
770 * option MUST be included.
772 if (ip6
->ip6_plen
== 0 && plen
== 0) {
774 * Note that if a valid jumbo payload option is
775 * contained, ip6_hoptops_input() must set a valid
776 * (non-zero) payload length to the variable plen.
778 ip6stat
.ip6s_badoptions
++;
779 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
780 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
781 lck_mtx_unlock(ip6_mutex
);
782 icmp6_error(m
, ICMP6_PARAM_PROB
,
783 ICMP6_PARAMPROB_HEADER
,
784 (caddr_t
)&ip6
->ip6_plen
- (caddr_t
)ip6
);
787 #ifndef PULLDOWN_TEST
788 /* ip6_hopopts_input() ensures that mbuf is contiguous */
789 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
791 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
, sizeof(struct ip6_hdr
),
792 sizeof(struct ip6_hbh
));
794 ip6stat
.ip6s_tooshort
++;
795 lck_mtx_unlock(ip6_mutex
);
802 * accept the packet if a router alert option is included
803 * and we act as an IPv6 router.
805 if (rtalert
!= ~0 && ip6_forwarding
)
811 * Check that the amount of data in the buffers
812 * is as at least much as the IPv6 header would have us expect.
813 * Trim mbufs if longer than we expect.
814 * Drop packet if shorter than we expect.
816 if (m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
) < plen
) {
817 ip6stat
.ip6s_tooshort
++;
818 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_truncated
);
821 if (m
->m_pkthdr
.len
> sizeof(struct ip6_hdr
) + plen
) {
822 if (m
->m_len
== m
->m_pkthdr
.len
) {
823 m
->m_len
= sizeof(struct ip6_hdr
) + plen
;
824 m
->m_pkthdr
.len
= sizeof(struct ip6_hdr
) + plen
;
826 m_adj(m
, sizeof(struct ip6_hdr
) + plen
- m
->m_pkthdr
.len
);
830 * Forward if desirable.
832 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
834 * If we are acting as a multicast router, all
835 * incoming multicast packets are passed to the
836 * kernel-level multicast forwarding function.
837 * The packet is returned (relatively) intact; if
838 * ip6_mforward() returns a non-zero value, the packet
839 * must be discarded, else it may be accepted below.
841 if (ip6_mrouter
&& ip6_mforward(ip6
, m
->m_pkthdr
.rcvif
, m
)) {
842 ip6stat
.ip6s_cantforward
++;
844 lck_mtx_unlock(ip6_mutex
);
849 lck_mtx_unlock(ip6_mutex
);
853 ip6_forward(m
, 0, 1);
854 lck_mtx_unlock(ip6_mutex
);
858 ip6
= mtod(m
, struct ip6_hdr
*);
861 * Malicious party may be able to use IPv4 mapped addr to confuse
862 * tcp/udp stack and bypass security checks (act as if it was from
863 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
865 * For SIIT end node behavior, you may want to disable the check.
866 * However, you will become vulnerable to attacks using IPv4 mapped
869 if (IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_src
) ||
870 IN6_IS_ADDR_V4MAPPED(&ip6
->ip6_dst
)) {
871 ip6stat
.ip6s_badscope
++;
872 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_addrerr
);
877 * Tell launch routine the next header
879 ip6stat
.ip6s_delivered
++;
880 in6_ifstat_inc(deliverifp
, ifs6_in_deliver
);
882 lck_mtx_unlock(ip6_mutex
);
886 while (nxt
!= IPPROTO_DONE
) {
887 struct ipfilter
*filter
;
889 if (ip6_hdrnestlimit
&& (++nest
> ip6_hdrnestlimit
)) {
890 ip6stat
.ip6s_toomanyhdr
++;
895 * protection against faulty packet - there should be
896 * more sanity checks in header chain processing.
898 if (m
->m_pkthdr
.len
< off
) {
899 ip6stat
.ip6s_tooshort
++;
900 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_truncated
);
906 * do we need to do it for every header? yeah, other
907 * functions can play with it (like re-allocate and copy).
909 mhist
= ip6_addaux(m
);
910 if (mhist
&& M_TRAILINGSPACE(mhist
) >= sizeof(nxt
)) {
911 hist
= mtod(mhist
, caddr_t
) + mhist
->m_len
;
912 bcopy(&nxt
, hist
, sizeof(nxt
));
913 mhist
->m_len
+= sizeof(nxt
);
915 ip6stat
.ip6s_toomanyhdr
++;
922 * enforce IPsec policy checking if we are seeing last header.
923 * note that we do not visit this with protocols with pcb layer
924 * code - like udp/tcp/raw ip.
926 if ((ipsec_bypass
== 0) && (ip6_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0) {
927 lck_mtx_lock(sadb_mutex
);
928 if (ipsec6_in_reject(m
, NULL
)) {
929 ipsec6stat
.in_polvio
++;
930 lck_mtx_unlock(sadb_mutex
);
933 lck_mtx_unlock(sadb_mutex
);
938 * Call IP filter on last header only
940 if ((ip6_protox
[nxt
]->pr_flags
& PR_LASTHDR
) != 0 && !TAILQ_EMPTY(&ipv6_filters
)) {
942 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
944 if ((struct ipfilter
*)inject_ipfref
== filter
)
946 } else if (filter
->ipf_filter
.ipf_input
) {
949 result
= filter
->ipf_filter
.ipf_input(
950 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, off
, nxt
);
951 if (result
== EJUSTRETURN
) {
964 if (!(ip6_protox
[nxt
]->pr_flags
& PR_PROTOLOCK
)) {
965 lck_mtx_lock(inet6_domain_mutex
);
966 nxt
= (*ip6_protox
[nxt
]->pr_input
)(&m
, &off
);
967 lck_mtx_unlock(inet6_domain_mutex
);
970 nxt
= (*ip6_protox
[nxt
]->pr_input
)(&m
, &off
);
974 lck_mtx_unlock(ip6_mutex
);
981 * set/grab in6_ifaddr correspond to IPv6 destination address.
982 * XXX backward compatibility wrapper
985 ip6_setdstifaddr(m
, ia6
)
987 struct in6_ifaddr
*ia6
;
993 mtod(n
, struct ip6aux
*)->ip6a_dstia6
= ia6
;
994 return n
; /* NULL if failed to set */
1005 return mtod(n
, struct ip6aux
*)->ip6a_dstia6
;
1011 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1012 * included, the real payload length will be stored in plenp.
1015 ip6_hopopts_input(plenp
, rtalertp
, mp
, offp
)
1017 u_int32_t
*rtalertp
; /* XXX: should be stored more smart way */
1021 struct mbuf
*m
= *mp
;
1022 int off
= *offp
, hbhlen
;
1023 struct ip6_hbh
*hbh
;
1026 /* validation of the length of the header */
1027 #ifndef PULLDOWN_TEST
1028 IP6_EXTHDR_CHECK(m
, off
, sizeof(*hbh
), return -1);
1029 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1030 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1032 IP6_EXTHDR_CHECK(m
, off
, hbhlen
, return -1);
1033 hbh
= (struct ip6_hbh
*)(mtod(m
, caddr_t
) + off
);
1035 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
,
1036 sizeof(struct ip6_hdr
), sizeof(struct ip6_hbh
));
1038 ip6stat
.ip6s_tooshort
++;
1041 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1042 IP6_EXTHDR_GET(hbh
, struct ip6_hbh
*, m
, sizeof(struct ip6_hdr
),
1045 ip6stat
.ip6s_tooshort
++;
1050 hbhlen
-= sizeof(struct ip6_hbh
);
1051 opt
= (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
);
1053 if (ip6_process_hopopts(m
, (u_int8_t
*)hbh
+ sizeof(struct ip6_hbh
),
1054 hbhlen
, rtalertp
, plenp
) < 0)
1063 * Search header for all Hop-by-hop options and process each option.
1064 * This function is separate from ip6_hopopts_input() in order to
1065 * handle a case where the sending node itself process its hop-by-hop
1066 * options header. In such a case, the function is called from ip6_output().
1068 * The function assumes that hbh header is located right after the IPv6 header
1069 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1070 * opthead + hbhlen is located in continuous memory region.
1073 ip6_process_hopopts(m
, opthead
, hbhlen
, rtalertp
, plenp
)
1077 u_int32_t
*rtalertp
;
1080 struct ip6_hdr
*ip6
;
1082 u_int8_t
*opt
= opthead
;
1083 u_int16_t rtalert_val
;
1084 u_int32_t jumboplen
;
1085 const int erroff
= sizeof(struct ip6_hdr
) + sizeof(struct ip6_hbh
);
1087 for (; hbhlen
> 0; hbhlen
-= optlen
, opt
+= optlen
) {
1093 if (hbhlen
< IP6OPT_MINLEN
) {
1094 ip6stat
.ip6s_toosmall
++;
1097 optlen
= *(opt
+ 1) + 2;
1099 case IP6OPT_RTALERT
:
1100 /* XXX may need check for alignment */
1101 if (hbhlen
< IP6OPT_RTALERT_LEN
) {
1102 ip6stat
.ip6s_toosmall
++;
1105 if (*(opt
+ 1) != IP6OPT_RTALERT_LEN
- 2) {
1107 lck_mtx_unlock(ip6_mutex
);
1108 icmp6_error(m
, ICMP6_PARAM_PROB
,
1109 ICMP6_PARAMPROB_HEADER
,
1110 erroff
+ opt
+ 1 - opthead
);
1111 lck_mtx_lock(ip6_mutex
);
1114 optlen
= IP6OPT_RTALERT_LEN
;
1115 bcopy((caddr_t
)(opt
+ 2), (caddr_t
)&rtalert_val
, 2);
1116 *rtalertp
= ntohs(rtalert_val
);
1119 /* XXX may need check for alignment */
1120 if (hbhlen
< IP6OPT_JUMBO_LEN
) {
1121 ip6stat
.ip6s_toosmall
++;
1124 if (*(opt
+ 1) != IP6OPT_JUMBO_LEN
- 2) {
1126 lck_mtx_unlock(ip6_mutex
);
1127 icmp6_error(m
, ICMP6_PARAM_PROB
,
1128 ICMP6_PARAMPROB_HEADER
,
1129 erroff
+ opt
+ 1 - opthead
);
1130 lck_mtx_lock(ip6_mutex
);
1133 optlen
= IP6OPT_JUMBO_LEN
;
1136 * IPv6 packets that have non 0 payload length
1137 * must not contain a jumbo payload option.
1139 ip6
= mtod(m
, struct ip6_hdr
*);
1140 if (ip6
->ip6_plen
) {
1141 ip6stat
.ip6s_badoptions
++;
1142 lck_mtx_unlock(ip6_mutex
);
1143 icmp6_error(m
, ICMP6_PARAM_PROB
,
1144 ICMP6_PARAMPROB_HEADER
,
1145 erroff
+ opt
- opthead
);
1146 lck_mtx_lock(ip6_mutex
);
1151 * We may see jumbolen in unaligned location, so
1152 * we'd need to perform bcopy().
1154 bcopy(opt
+ 2, &jumboplen
, sizeof(jumboplen
));
1155 jumboplen
= (u_int32_t
)htonl(jumboplen
);
1159 * if there are multiple jumbo payload options,
1160 * *plenp will be non-zero and the packet will be
1162 * the behavior may need some debate in ipngwg -
1163 * multiple options does not make sense, however,
1164 * there's no explicit mention in specification.
1167 ip6stat
.ip6s_badoptions
++;
1168 lck_mtx_unlock(ip6_mutex
);
1169 icmp6_error(m
, ICMP6_PARAM_PROB
,
1170 ICMP6_PARAMPROB_HEADER
,
1171 erroff
+ opt
+ 2 - opthead
);
1172 lck_mtx_lock(ip6_mutex
);
1178 * jumbo payload length must be larger than 65535.
1180 if (jumboplen
<= IPV6_MAXPACKET
) {
1181 ip6stat
.ip6s_badoptions
++;
1182 lck_mtx_unlock(ip6_mutex
);
1183 icmp6_error(m
, ICMP6_PARAM_PROB
,
1184 ICMP6_PARAMPROB_HEADER
,
1185 erroff
+ opt
+ 2 - opthead
);
1186 lck_mtx_lock(ip6_mutex
);
1192 default: /* unknown option */
1193 if (hbhlen
< IP6OPT_MINLEN
) {
1194 ip6stat
.ip6s_toosmall
++;
1197 optlen
= ip6_unknown_opt(opt
, m
,
1198 erroff
+ opt
- opthead
, 1);
1200 /* ip6_unknown opt unlocked ip6_mutex */
1216 * Unknown option processing.
1217 * The third argument `off' is the offset from the IPv6 header to the option,
1218 * which is necessary if the IPv6 header the and option header and IPv6 header
1219 * is not continuous in order to return an ICMPv6 error.
1222 ip6_unknown_opt(optp
, m
, off
, locked
)
1228 struct ip6_hdr
*ip6
;
1230 switch (IP6OPT_TYPE(*optp
)) {
1231 case IP6OPT_TYPE_SKIP
: /* ignore the option */
1232 return((int)*(optp
+ 1));
1233 case IP6OPT_TYPE_DISCARD
: /* silently discard */
1236 case IP6OPT_TYPE_FORCEICMP
: /* send ICMP even if multicasted */
1237 ip6stat
.ip6s_badoptions
++;
1239 lck_mtx_unlock(ip6_mutex
);
1240 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_OPTION
, off
);
1242 lck_mtx_lock(ip6_mutex
);
1244 case IP6OPT_TYPE_ICMP
: /* send ICMP if not multicasted */
1245 ip6stat
.ip6s_badoptions
++;
1246 ip6
= mtod(m
, struct ip6_hdr
*);
1247 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
1248 (m
->m_flags
& (M_BCAST
|M_MCAST
)))
1252 lck_mtx_unlock(ip6_mutex
);
1253 icmp6_error(m
, ICMP6_PARAM_PROB
,
1254 ICMP6_PARAMPROB_OPTION
, off
);
1256 lck_mtx_lock(ip6_mutex
);
1261 m_freem(m
); /* XXX: NOTREACHED */
1266 * Create the "control" list for this pcb.
1267 * The function will not modify mbuf chain at all.
1269 * with KAME mbuf chain restriction:
1270 * The routine will be called from upper layer handlers like tcp6_input().
1271 * Thus the routine assumes that the caller (tcp6_input) have already
1272 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1273 * very first mbuf on the mbuf chain.
1276 ip6_savecontrol(in6p
, mp
, ip6
, m
)
1279 struct ip6_hdr
*ip6
;
1282 int rthdr_exist
= 0;
1285 if ((in6p
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0) {
1289 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
1290 SCM_TIMESTAMP
, SOL_SOCKET
);
1292 mp
= &(*mp
)->m_next
;
1297 /* RFC 2292 sec. 5 */
1298 if ((in6p
->in6p_flags
& IN6P_PKTINFO
) != 0) {
1299 struct in6_pktinfo pi6
;
1300 bcopy(&ip6
->ip6_dst
, &pi6
.ipi6_addr
, sizeof(struct in6_addr
));
1301 if (IN6_IS_SCOPE_LINKLOCAL(&pi6
.ipi6_addr
))
1302 pi6
.ipi6_addr
.s6_addr16
[1] = 0;
1303 pi6
.ipi6_ifindex
= (m
&& m
->m_pkthdr
.rcvif
)
1304 ? m
->m_pkthdr
.rcvif
->if_index
1306 *mp
= sbcreatecontrol((caddr_t
) &pi6
,
1307 sizeof(struct in6_pktinfo
), IPV6_PKTINFO
,
1310 mp
= &(*mp
)->m_next
;
1313 if ((in6p
->in6p_flags
& IN6P_HOPLIMIT
) != 0) {
1314 int hlim
= ip6
->ip6_hlim
& 0xff;
1315 *mp
= sbcreatecontrol((caddr_t
) &hlim
,
1316 sizeof(int), IPV6_HOPLIMIT
, IPPROTO_IPV6
);
1318 mp
= &(*mp
)->m_next
;
1322 * IPV6_HOPOPTS socket option. Recall that we required super-user
1323 * privilege for the option (see ip6_ctloutput), but it might be too
1324 * strict, since there might be some hop-by-hop options which can be
1325 * returned to normal user.
1326 * See RFC 2292 section 6.
1328 if ((in6p
->in6p_flags
& IN6P_HOPOPTS
) != 0) {
1330 * Check if a hop-by-hop options header is contatined in the
1331 * received packet, and if so, store the options as ancillary
1332 * data. Note that a hop-by-hop options header must be
1333 * just after the IPv6 header, which fact is assured through
1334 * the IPv6 input processing.
1336 ip6
= mtod(m
, struct ip6_hdr
*);
1337 if (ip6
->ip6_nxt
== IPPROTO_HOPOPTS
) {
1338 struct ip6_hbh
*hbh
;
1344 #ifndef PULLDOWN_TEST
1345 hbh
= (struct ip6_hbh
*)(ip6
+ 1);
1346 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1348 ext
= ip6_pullexthdr(m
, sizeof(struct ip6_hdr
),
1351 ip6stat
.ip6s_tooshort
++;
1354 hbh
= mtod(ext
, struct ip6_hbh
*);
1355 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
1356 if (hbhlen
!= ext
->m_len
) {
1358 ip6stat
.ip6s_tooshort
++;
1364 * XXX: We copy whole the header even if a jumbo
1365 * payload option is included, which option is to
1366 * be removed before returning in the RFC 2292.
1367 * Note: this constraint is removed in 2292bis.
1369 *mp
= sbcreatecontrol((caddr_t
)hbh
, hbhlen
,
1370 IPV6_HOPOPTS
, IPPROTO_IPV6
);
1372 mp
= &(*mp
)->m_next
;
1379 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1380 if ((in6p
->in6p_flags
& (IN6P_DSTOPTS
| IN6P_RTHDRDSTOPTS
)) != 0) {
1381 int proto
, off
, nxt
;
1384 * go through the header chain to see if a routing header is
1385 * contained in the packet. We need this information to store
1386 * destination options headers (if any) properly.
1387 * XXX: performance issue. We should record this info when
1388 * processing extension headers in incoming routine.
1391 proto
= IPPROTO_IPV6
;
1397 newoff
= ip6_nexthdr(m
, off
, proto
, &nxt
);
1400 if (newoff
< off
) /* invalid, check for safety */
1402 if ((proto
= nxt
) == IPPROTO_ROUTING
) {
1410 if ((in6p
->in6p_flags
&
1411 (IN6P_RTHDR
| IN6P_DSTOPTS
| IN6P_RTHDRDSTOPTS
)) != 0) {
1412 ip6
= mtod(m
, struct ip6_hdr
*);
1413 int nxt
= ip6
->ip6_nxt
, off
= sizeof(struct ip6_hdr
);
1416 * Search for destination options headers or routing
1417 * header(s) through the header chain, and stores each
1418 * header as ancillary data.
1419 * Note that the order of the headers remains in
1420 * the chain of ancillary data.
1422 while (1) { /* is explicit loop prevention necessary? */
1423 struct ip6_ext
*ip6e
= NULL
;
1426 struct mbuf
*ext
= NULL
;
1430 * if it is not an extension header, don't try to
1431 * pull it from the chain.
1434 case IPPROTO_DSTOPTS
:
1435 case IPPROTO_ROUTING
:
1436 case IPPROTO_HOPOPTS
:
1437 case IPPROTO_AH
: /* is it possible? */
1443 #ifndef PULLDOWN_TEST
1444 if (off
+ sizeof(*ip6e
) > m
->m_len
)
1446 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + off
);
1447 if (nxt
== IPPROTO_AH
)
1448 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1450 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1451 if (off
+ elen
> m
->m_len
)
1454 ext
= ip6_pullexthdr(m
, off
, nxt
);
1456 ip6stat
.ip6s_tooshort
++;
1459 ip6e
= mtod(ext
, struct ip6_ext
*);
1460 if (nxt
== IPPROTO_AH
)
1461 elen
= (ip6e
->ip6e_len
+ 2) << 2;
1463 elen
= (ip6e
->ip6e_len
+ 1) << 3;
1464 if (elen
!= ext
->m_len
) {
1466 ip6stat
.ip6s_tooshort
++;
1472 case IPPROTO_DSTOPTS
:
1473 if ((in6p
->in6p_flags
& IN6P_DSTOPTS
) == 0)
1476 *mp
= sbcreatecontrol((caddr_t
)ip6e
, elen
,
1480 mp
= &(*mp
)->m_next
;
1482 case IPPROTO_ROUTING
:
1483 if (!in6p
->in6p_flags
& IN6P_RTHDR
)
1486 *mp
= sbcreatecontrol((caddr_t
)ip6e
, elen
,
1490 mp
= &(*mp
)->m_next
;
1492 case IPPROTO_HOPOPTS
:
1493 case IPPROTO_AH
: /* is it possible? */
1498 * other cases have been filtered in the above.
1499 * none will visit this case. here we supply
1500 * the code just in case (nxt overwritten or
1510 /* proceed with the next header. */
1512 nxt
= ip6e
->ip6e_nxt
;
1527 * pull single extension header from mbuf chain. returns single mbuf that
1528 * contains the result, or NULL on error.
1530 static struct mbuf
*
1531 ip6_pullexthdr(m
, off
, nxt
)
1536 struct ip6_ext ip6e
;
1542 case IPPROTO_DSTOPTS
:
1543 case IPPROTO_ROUTING
:
1544 case IPPROTO_HOPOPTS
:
1545 case IPPROTO_AH
: /* is it possible? */
1548 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt
);
1552 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1553 if (nxt
== IPPROTO_AH
)
1554 elen
= (ip6e
.ip6e_len
+ 2) << 2;
1556 elen
= (ip6e
.ip6e_len
+ 1) << 3;
1558 MGET(n
, M_DONTWAIT
, MT_DATA
);
1559 if (n
&& elen
>= MLEN
) {
1560 MCLGET(n
, M_DONTWAIT
);
1561 if ((n
->m_flags
& M_EXT
) == 0) {
1570 if (elen
>= M_TRAILINGSPACE(n
)) {
1575 m_copydata(m
, off
, elen
, mtod(n
, caddr_t
));
1582 * Get pointer to the previous header followed by the header
1583 * currently processed.
1584 * XXX: This function supposes that
1585 * M includes all headers,
1586 * the next header field and the header length field of each header
1588 * the sum of each header length equals to OFF.
1589 * Because of these assumptions, this function must be called very
1590 * carefully. Moreover, it will not be used in the near future when
1591 * we develop `neater' mechanism to process extension headers.
1594 ip6_get_prevhdr(m
, off
)
1598 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1600 if (off
== sizeof(struct ip6_hdr
))
1601 return(&ip6
->ip6_nxt
);
1604 struct ip6_ext
*ip6e
= NULL
;
1607 len
= sizeof(struct ip6_hdr
);
1609 ip6e
= (struct ip6_ext
*)(mtod(m
, caddr_t
) + len
);
1612 case IPPROTO_FRAGMENT
:
1613 len
+= sizeof(struct ip6_frag
);
1616 len
+= (ip6e
->ip6e_len
+ 2) << 2;
1619 len
+= (ip6e
->ip6e_len
+ 1) << 3;
1622 nxt
= ip6e
->ip6e_nxt
;
1625 return(&ip6e
->ip6e_nxt
);
1632 * get next header offset. m will be retained.
1635 ip6_nexthdr(m
, off
, proto
, nxtp
)
1642 struct ip6_ext ip6e
;
1647 panic("ip6_nexthdr: m == NULL");
1648 if ((m
->m_flags
& M_PKTHDR
) == 0 || m
->m_pkthdr
.len
< off
)
1653 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6
))
1655 m_copydata(m
, off
, sizeof(ip6
), (caddr_t
)&ip6
);
1657 *nxtp
= ip6
.ip6_nxt
;
1661 case IPPROTO_FRAGMENT
:
1663 * terminate parsing if it is not the first fragment,
1664 * it does not make sense to parse through it.
1666 if (m
->m_pkthdr
.len
< off
+ sizeof(fh
))
1668 m_copydata(m
, off
, sizeof(fh
), (caddr_t
)&fh
);
1669 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1670 if (fh
.ip6f_offlg
& IP6F_OFF_MASK
)
1673 *nxtp
= fh
.ip6f_nxt
;
1674 off
+= sizeof(struct ip6_frag
);
1678 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
))
1680 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1682 *nxtp
= ip6e
.ip6e_nxt
;
1683 off
+= (ip6e
.ip6e_len
+ 2) << 2;
1686 case IPPROTO_HOPOPTS
:
1687 case IPPROTO_ROUTING
:
1688 case IPPROTO_DSTOPTS
:
1689 if (m
->m_pkthdr
.len
< off
+ sizeof(ip6e
))
1691 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1693 *nxtp
= ip6e
.ip6e_nxt
;
1694 off
+= (ip6e
.ip6e_len
+ 1) << 3;
1699 case IPPROTO_IPCOMP
:
1711 * get offset for the last header in the chain. m will be kept untainted.
1714 ip6_lasthdr(m
, off
, proto
, nxtp
)
1728 newoff
= ip6_nexthdr(m
, off
, proto
, nxtp
);
1731 else if (newoff
< off
)
1732 return -1; /* invalid */
1733 else if (newoff
== off
)
1748 if (sizeof(struct ip6aux
) > MHLEN
)
1749 panic("assumption failed on sizeof(ip6aux)");
1751 n
= m_aux_find(m
, AF_INET6
, -1);
1753 if (n
->m_len
< sizeof(struct ip6aux
)) {
1754 printf("conflicting use of ip6aux");
1758 n
= m_aux_add(m
, AF_INET6
, -1);
1760 n
->m_len
= sizeof(struct ip6aux
);
1761 bzero(mtod(n
, caddr_t
), n
->m_len
);
1773 n
= m_aux_find(m
, AF_INET6
, -1);
1774 if (n
&& n
->m_len
< sizeof(struct ip6aux
)) {
1775 printf("conflicting use of ip6aux");
1787 n
= m_aux_find(m
, AF_INET6
, -1);
1793 * System control for IP6
1796 u_char inet6ctlerrmap
[PRC_NCMDS
] = {
1798 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
1799 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
1800 EMSGSIZE
, EHOSTUNREACH
, 0, 0,