1 /* $FreeBSD: src/sys/netinet6/ip6_input.c,v 1.11.2.15 2003/01/24 05:11:35 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/ip6_input.c,v 1.9 2003/09/16 01:58:00 hsu Exp $ */
3 /* $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
69 #include "opt_ip6fw.h"
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/errno.h>
84 #include <sys/kernel.h>
85 #include <sys/syslog.h>
89 #include <net/if_types.h>
90 #include <net/if_dl.h>
91 #include <net/route.h>
92 #include <net/netisr.h>
93 #include <net/intrq.h>
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
99 #include <netinet/ip_icmp.h>
101 #include <netinet/ip6.h>
102 #include <netinet6/in6_var.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet/in_pcb.h>
105 #include <netinet/icmp6.h>
106 #include <netinet6/in6_ifattach.h>
107 #include <netinet6/nd6.h>
108 #include <netinet6/in6_prefix.h>
111 #include <netinet6/ipsec.h>
113 #include <netinet6/ipsec6.h>
121 #endif /* FAST_IPSEC */
123 #include <net/ip6fw/ip6_fw.h>
125 #include <netinet6/ip6protosw.h>
127 /* we need it for NLOOP. */
128 #include "use_loop.h"
130 #include <net/net_osdep.h>
132 extern int (*fr_checkp) (struct ip *, int, struct ifnet *, int, struct mbuf **);
134 extern struct domain inet6domain;
135 extern struct ip6protosw inet6sw[];
137 u_char ip6_protox[IPPROTO_MAX];
138 static struct ifqueue ip6intrq;
139 static int ip6qmaxlen = IFQ_MAXLEN;
140 struct in6_ifaddr *in6_ifaddr;
142 extern struct callout in6_tmpaddrtimer_ch;
144 int ip6_forward_srcrt; /* XXX */
145 int ip6_sourcecheck; /* XXX */
146 int ip6_sourcecheck_interval; /* XXX */
147 const int int6intrq_present = 1;
149 int ip6_ours_check_algorithm;
152 ip6_fw_chk_t *ip6_fw_chk_ptr;
153 ip6_fw_ctl_t *ip6_fw_ctl_ptr;
154 int ip6_fw_enable = 1;
156 struct ip6stat ip6stat;
158 static void ip6_init2 (void *);
159 static struct ip6aux *ip6_setdstifaddr (struct mbuf *, struct in6_ifaddr *);
160 static int ip6_hopopts_input (u_int32_t *, u_int32_t *, struct mbuf **, int *);
162 static struct mbuf *ip6_pullexthdr (struct mbuf *, size_t, int);
166 * IP6 initialization: fill in IP6 protocol switch table.
167 * All protocols not implemented in kernel go to raw IP6 protocol handler.
172 struct ip6protosw *pr;
177 if (sizeof(struct protosw) != sizeof(struct ip6protosw))
178 panic("sizeof(protosw) != sizeof(ip6protosw)");
180 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
183 for (i = 0; i < IPPROTO_MAX; i++)
184 ip6_protox[i] = pr - inet6sw;
185 for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
186 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
187 if (pr->pr_domain->dom_family == PF_INET6 &&
188 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
189 ip6_protox[pr->pr_protocol] = pr - inet6sw;
190 ip6intrq.ifq_maxlen = ip6qmaxlen;
191 netisr_register(NETISR_IPV6, ip6_input, &ip6intrq);
195 * in many cases, random() here does NOT return random number
196 * as initialization during bootstrap time occur in fixed order.
199 ip6_flow_seq = random() ^ tv.tv_usec;
201 ip6_desync_factor = (random() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR;
210 * to route local address of p2p link to loopback,
211 * assign loopback address first.
213 in6_ifattach(&loif[0], NULL);
216 callout_init(&nd6_timer_ch);
217 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
219 /* router renumbering prefix list maintenance */
220 callout_init(&in6_rr_timer_ch);
221 callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
223 /* timer for regeneranation of temporary addresses randomize ID */
224 callout_reset(&in6_tmpaddrtimer_ch,
225 (ip6_temp_preferred_lifetime - ip6_desync_factor -
226 ip6_temp_regen_advance) * hz,
227 in6_tmpaddrtimer, NULL);
231 /* This must be after route_init(), which is now SI_ORDER_THIRD */
232 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
234 extern struct route_in6 ip6_forward_rt;
241 int off = sizeof(struct ip6_hdr), nest;
243 u_int32_t rtalert = ~0;
245 struct ifnet *deliverifp = NULL;
249 * should the inner packet be considered authentic?
250 * see comment in ah4_input().
253 m->m_flags &= ~M_AUTHIPHDR;
254 m->m_flags &= ~M_AUTHIPDGM;
259 * make sure we don't have onion peering information into m_aux.
266 if (m->m_flags & M_EXT) {
268 ip6stat.ip6s_mext2m++;
270 ip6stat.ip6s_mext1++;
272 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
274 if (m->m_flags & M_LOOP) {
275 ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
276 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
277 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
279 ip6stat.ip6s_m2m[0]++;
285 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
286 ip6stat.ip6s_total++;
288 #ifndef PULLDOWN_TEST
290 * L2 bridge code and some other code can return mbuf chain
291 * that does not conform to KAME requirement. too bad.
292 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
294 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
297 MGETHDR(n, M_DONTWAIT, MT_HEADER);
300 if (n && n->m_pkthdr.len > MHLEN) {
301 MCLGET(n, M_DONTWAIT);
302 if ((n->m_flags & M_EXT) == 0) {
312 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
313 n->m_len = n->m_pkthdr.len;
317 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
320 if (m->m_len < sizeof(struct ip6_hdr)) {
322 inifp = m->m_pkthdr.rcvif;
323 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
324 ip6stat.ip6s_toosmall++;
325 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
330 ip6 = mtod(m, struct ip6_hdr *);
332 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
333 ip6stat.ip6s_badvers++;
334 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
339 * Check if we want to allow this packet to be processed.
340 * Consider it to be bad if not.
345 if ((*fr_checkp)((struct ip *)ip6, sizeof(*ip6),
346 m->m_pkthdr.rcvif, 0, &m1) != 0)
351 ip6 = mtod(m, struct ip6_hdr *);
354 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
357 * Check with the firewall...
359 if (ip6_fw_enable && ip6_fw_chk_ptr) {
361 /* If ipfw says divert, we have to just drop packet */
362 /* use port as a dummy argument */
363 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
372 * Check against address spoofing/corruption.
374 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
375 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
377 * XXX: "badscope" is not very suitable for a multicast source.
379 ip6stat.ip6s_badscope++;
380 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
383 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
384 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
385 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
386 ip6stat.ip6s_badscope++;
387 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
392 * The following check is not documented in specs. A malicious
393 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
394 * and bypass security checks (act as if it was from 127.0.0.1 by using
395 * IPv6 src ::ffff:127.0.0.1). Be cautious.
397 * This check chokes if we are in an SIIT cloud. As none of BSDs
398 * support IPv4-less kernel compilation, we cannot support SIIT
399 * environment at all. So, it makes more sense for us to reject any
400 * malicious packets for non-SIIT environment, than try to do a
401 * partical support for SIIT environment.
403 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
404 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
405 ip6stat.ip6s_badscope++;
406 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
411 * Reject packets with IPv4 compatible addresses (auto tunnel).
413 * The code forbids auto tunnel relay case in RFC1933 (the check is
414 * stronger than RFC1933). We may want to re-enable it if mech-xx
415 * is revised to forbid relaying case.
417 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
418 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
419 ip6stat.ip6s_badscope++;
420 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
425 /* drop packets if interface ID portion is already filled */
426 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
427 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
428 ip6->ip6_src.s6_addr16[1]) {
429 ip6stat.ip6s_badscope++;
432 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
433 ip6->ip6_dst.s6_addr16[1]) {
434 ip6stat.ip6s_badscope++;
439 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
440 ip6->ip6_src.s6_addr16[1]
441 = htons(m->m_pkthdr.rcvif->if_index);
442 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
443 ip6->ip6_dst.s6_addr16[1]
444 = htons(m->m_pkthdr.rcvif->if_index);
446 #if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */
448 * We use rt->rt_ifp to determine if the address is ours or not.
449 * If rt_ifp is lo0, the address is ours.
450 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
451 * so any address under fe80::%lo0/64 will be mistakenly considered
452 * local. The special case is supplied to handle the case properly
453 * by actually looking at interface addresses
454 * (using in6ifa_ifpwithaddr).
456 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
457 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
458 if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
459 icmp6_error(m, ICMP6_DST_UNREACH,
460 ICMP6_DST_UNREACH_ADDR, 0);
461 /* m is already freed */
466 deliverifp = m->m_pkthdr.rcvif;
474 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
475 struct in6_multi *in6m = 0;
477 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
479 * See if we belong to the destination multicast group on the
482 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
485 else if (!ip6_mrouter) {
486 ip6stat.ip6s_notmember++;
487 ip6stat.ip6s_cantforward++;
488 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
491 deliverifp = m->m_pkthdr.rcvif;
498 switch (ip6_ours_check_algorithm) {
501 * XXX: I intentionally broke our indentation rule here,
502 * since this switch-case is just for measurement and
503 * therefore should soon be removed.
505 if (ip6_forward_rt.ro_rt != NULL &&
506 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
507 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
508 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
509 ip6stat.ip6s_forward_cachehit++;
511 struct sockaddr_in6 *dst6;
513 if (ip6_forward_rt.ro_rt) {
514 /* route is down or destination is different */
515 ip6stat.ip6s_forward_cachemiss++;
516 RTFREE(ip6_forward_rt.ro_rt);
517 ip6_forward_rt.ro_rt = 0;
520 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
521 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
522 dst6->sin6_len = sizeof(struct sockaddr_in6);
523 dst6->sin6_family = AF_INET6;
524 dst6->sin6_addr = ip6->ip6_dst;
526 ip6_forward_rt.ro_dst.sin6_scope_id =
527 in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst);
530 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
533 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
536 * Accept the packet if the forwarding interface to the destination
537 * according to the routing table is the loopback interface,
538 * unless the associated route has a gateway.
539 * Note that this approach causes to accept a packet if there is a
540 * route to the loopback interface for the destination of the packet.
541 * But we think it's even useful in some situations, e.g. when using
542 * a special daemon which wants to intercept the packet.
544 * XXX: some OSes automatically make a cloned route for the destination
545 * of an outgoing packet. If the outgoing interface of the packet
546 * is a loopback one, the kernel would consider the packet to be
547 * accepted, even if we have no such address assinged on the interface.
548 * We check the cloned flag of the route entry to reject such cases,
549 * assuming that route entries for our own addresses are not made by
550 * cloning (it should be true because in6_addloop explicitly installs
551 * the host route). However, we might have to do an explicit check
552 * while it would be less efficient. Or, should we rather install a
553 * reject route for such a case?
555 if (ip6_forward_rt.ro_rt &&
556 (ip6_forward_rt.ro_rt->rt_flags &
557 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
559 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
562 !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
566 * The check below is redundant since the comparison of
567 * the destination and the key of the rtentry has
568 * already done through looking up the routing table.
570 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
571 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
573 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
574 struct in6_ifaddr *ia6 =
575 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
578 * record address information into m_aux.
580 (void)ip6_setdstifaddr(m, ia6);
583 * packets to a tentative, duplicated, or somehow invalid
584 * address must not be accepted.
586 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
587 /* this address is ready */
589 deliverifp = ia6->ia_ifp; /* correct? */
590 /* Count the packet in the ip address stats */
591 ia6->ia_ifa.if_ipackets++;
592 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
595 /* address is not ready, so discard the packet. */
597 "ip6_input: packet to an unready address %s->%s\n",
598 ip6_sprintf(&ip6->ip6_src),
599 ip6_sprintf(&ip6->ip6_dst)));
604 } /* XXX indentation (see above) */
607 * FAITH(Firewall Aided Internet Translator)
610 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
611 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
612 /* XXX do we need more sanity checks? */
614 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
620 * Now there is no reason to process the packet if it's not our own
621 * and we're not a router.
623 if (!ip6_forwarding) {
624 ip6stat.ip6s_cantforward++;
625 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
631 * record address information into m_aux, if we don't have one yet.
632 * note that we are unable to record it, if the address is not listed
633 * as our interface address (e.g. multicast addresses, addresses
634 * within FAITH prefixes and such).
636 if (deliverifp && !ip6_getdstifaddr(m)) {
637 struct in6_ifaddr *ia6;
639 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
641 if (!ip6_setdstifaddr(m, ia6)) {
643 * XXX maybe we should drop the packet here,
644 * as we could not provide enough information
645 * to the upper layers.
652 * Process Hop-by-Hop options header if it's contained.
653 * m may be modified in ip6_hopopts_input().
654 * If a JumboPayload option is included, plen will also be modified.
656 plen = (u_int32_t)ntohs(ip6->ip6_plen);
657 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
660 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
661 #if 0 /*touches NULL pointer*/
662 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
664 return; /* m have already been freed */
668 ip6 = mtod(m, struct ip6_hdr *);
671 * if the payload length field is 0 and the next header field
672 * indicates Hop-by-Hop Options header, then a Jumbo Payload
673 * option MUST be included.
675 if (ip6->ip6_plen == 0 && plen == 0) {
677 * Note that if a valid jumbo payload option is
678 * contained, ip6_hoptops_input() must set a valid
679 * (non-zero) payload length to the variable plen.
681 ip6stat.ip6s_badoptions++;
682 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
683 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
684 icmp6_error(m, ICMP6_PARAM_PROB,
685 ICMP6_PARAMPROB_HEADER,
686 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
689 #ifndef PULLDOWN_TEST
690 /* ip6_hopopts_input() ensures that mbuf is contiguous */
691 hbh = (struct ip6_hbh *)(ip6 + 1);
693 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
694 sizeof(struct ip6_hbh));
696 ip6stat.ip6s_tooshort++;
703 * accept the packet if a router alert option is included
704 * and we act as an IPv6 router.
706 if (rtalert != ~0 && ip6_forwarding)
712 * Check that the amount of data in the buffers
713 * is as at least much as the IPv6 header would have us expect.
714 * Trim mbufs if longer than we expect.
715 * Drop packet if shorter than we expect.
717 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
718 ip6stat.ip6s_tooshort++;
719 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
722 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
723 if (m->m_len == m->m_pkthdr.len) {
724 m->m_len = sizeof(struct ip6_hdr) + plen;
725 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
727 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
731 * Forward if desirable.
733 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
735 * If we are acting as a multicast router, all
736 * incoming multicast packets are passed to the
737 * kernel-level multicast forwarding function.
738 * The packet is returned (relatively) intact; if
739 * ip6_mforward() returns a non-zero value, the packet
740 * must be discarded, else it may be accepted below.
742 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
743 ip6stat.ip6s_cantforward++;
756 ip6 = mtod(m, struct ip6_hdr *);
759 * Malicious party may be able to use IPv4 mapped addr to confuse
760 * tcp/udp stack and bypass security checks (act as if it was from
761 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
763 * For SIIT end node behavior, you may want to disable the check.
764 * However, you will become vulnerable to attacks using IPv4 mapped
767 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
768 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
769 ip6stat.ip6s_badscope++;
770 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
775 * Tell launch routine the next header
777 ip6stat.ip6s_delivered++;
778 in6_ifstat_inc(deliverifp, ifs6_in_deliver);
781 while (nxt != IPPROTO_DONE) {
782 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
783 ip6stat.ip6s_toomanyhdr++;
788 * protection against faulty packet - there should be
789 * more sanity checks in header chain processing.
791 if (m->m_pkthdr.len < off) {
792 ip6stat.ip6s_tooshort++;
793 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
799 * do we need to do it for every header? yeah, other
800 * functions can play with it (like re-allocate and copy).
802 mhist = ip6_addaux(m);
803 if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) {
804 hist = mtod(mhist, caddr_t) + mhist->m_len;
805 bcopy(&nxt, hist, sizeof(nxt));
806 mhist->m_len += sizeof(nxt);
808 ip6stat.ip6s_toomanyhdr++;
815 * enforce IPsec policy checking if we are seeing last header.
816 * note that we do not visit this with protocols with pcb layer
817 * code - like udp/tcp/raw ip.
819 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
820 ipsec6_in_reject(m, NULL)) {
821 ipsec6stat.in_polvio++;
826 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
834 * set/grab in6_ifaddr correspond to IPv6 destination address.
835 * XXX backward compatibility wrapper
837 static struct ip6aux *
838 ip6_setdstifaddr(m, ia6)
840 struct in6_ifaddr *ia6;
846 n->ip6a_dstia6 = ia6;
847 return n; /* NULL if failed to set */
858 return n->ip6a_dstia6;
864 * Hop-by-Hop options header processing. If a valid jumbo payload option is
865 * included, the real payload length will be stored in plenp.
868 ip6_hopopts_input(plenp, rtalertp, mp, offp)
870 u_int32_t *rtalertp; /* XXX: should be stored more smart way */
874 struct mbuf *m = *mp;
875 int off = *offp, hbhlen;
879 /* validation of the length of the header */
880 #ifndef PULLDOWN_TEST
881 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
882 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
883 hbhlen = (hbh->ip6h_len + 1) << 3;
885 IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
886 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
888 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
889 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
891 ip6stat.ip6s_tooshort++;
894 hbhlen = (hbh->ip6h_len + 1) << 3;
895 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
898 ip6stat.ip6s_tooshort++;
903 hbhlen -= sizeof(struct ip6_hbh);
904 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
906 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
907 hbhlen, rtalertp, plenp) < 0)
916 * Search header for all Hop-by-hop options and process each option.
917 * This function is separate from ip6_hopopts_input() in order to
918 * handle a case where the sending node itself process its hop-by-hop
919 * options header. In such a case, the function is called from ip6_output().
921 * The function assumes that hbh header is located right after the IPv6 header
922 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
923 * opthead + hbhlen is located in continuous memory region.
926 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
935 u_int8_t *opt = opthead;
936 u_int16_t rtalert_val;
938 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
940 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
946 if (hbhlen < IP6OPT_MINLEN) {
947 ip6stat.ip6s_toosmall++;
950 optlen = *(opt + 1) + 2;
953 /* XXX may need check for alignment */
954 if (hbhlen < IP6OPT_RTALERT_LEN) {
955 ip6stat.ip6s_toosmall++;
958 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
960 icmp6_error(m, ICMP6_PARAM_PROB,
961 ICMP6_PARAMPROB_HEADER,
962 erroff + opt + 1 - opthead);
965 optlen = IP6OPT_RTALERT_LEN;
966 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
967 *rtalertp = ntohs(rtalert_val);
970 /* XXX may need check for alignment */
971 if (hbhlen < IP6OPT_JUMBO_LEN) {
972 ip6stat.ip6s_toosmall++;
975 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
977 icmp6_error(m, ICMP6_PARAM_PROB,
978 ICMP6_PARAMPROB_HEADER,
979 erroff + opt + 1 - opthead);
982 optlen = IP6OPT_JUMBO_LEN;
985 * IPv6 packets that have non 0 payload length
986 * must not contain a jumbo payload option.
988 ip6 = mtod(m, struct ip6_hdr *);
990 ip6stat.ip6s_badoptions++;
991 icmp6_error(m, ICMP6_PARAM_PROB,
992 ICMP6_PARAMPROB_HEADER,
993 erroff + opt - opthead);
998 * We may see jumbolen in unaligned location, so
999 * we'd need to perform bcopy().
1001 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1002 jumboplen = (u_int32_t)htonl(jumboplen);
1006 * if there are multiple jumbo payload options,
1007 * *plenp will be non-zero and the packet will be
1009 * the behavior may need some debate in ipngwg -
1010 * multiple options does not make sense, however,
1011 * there's no explicit mention in specification.
1014 ip6stat.ip6s_badoptions++;
1015 icmp6_error(m, ICMP6_PARAM_PROB,
1016 ICMP6_PARAMPROB_HEADER,
1017 erroff + opt + 2 - opthead);
1023 * jumbo payload length must be larger than 65535.
1025 if (jumboplen <= IPV6_MAXPACKET) {
1026 ip6stat.ip6s_badoptions++;
1027 icmp6_error(m, ICMP6_PARAM_PROB,
1028 ICMP6_PARAMPROB_HEADER,
1029 erroff + opt + 2 - opthead);
1035 default: /* unknown option */
1036 if (hbhlen < IP6OPT_MINLEN) {
1037 ip6stat.ip6s_toosmall++;
1040 optlen = ip6_unknown_opt(opt, m,
1041 erroff + opt - opthead);
1057 * Unknown option processing.
1058 * The third argument `off' is the offset from the IPv6 header to the option,
1059 * which is necessary if the IPv6 header the and option header and IPv6 header
1060 * is not continuous in order to return an ICMPv6 error.
1063 ip6_unknown_opt(optp, m, off)
1068 struct ip6_hdr *ip6;
1070 switch (IP6OPT_TYPE(*optp)) {
1071 case IP6OPT_TYPE_SKIP: /* ignore the option */
1072 return((int)*(optp + 1));
1073 case IP6OPT_TYPE_DISCARD: /* silently discard */
1076 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1077 ip6stat.ip6s_badoptions++;
1078 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1080 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1081 ip6stat.ip6s_badoptions++;
1082 ip6 = mtod(m, struct ip6_hdr *);
1083 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1084 (m->m_flags & (M_BCAST|M_MCAST)))
1087 icmp6_error(m, ICMP6_PARAM_PROB,
1088 ICMP6_PARAMPROB_OPTION, off);
1092 m_freem(m); /* XXX: NOTREACHED */
1097 * Create the "control" list for this pcb.
1098 * The function will not modify mbuf chain at all.
1100 * with KAME mbuf chain restriction:
1101 * The routine will be called from upper layer handlers like tcp6_input().
1102 * Thus the routine assumes that the caller (tcp6_input) have already
1103 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1104 * very first mbuf on the mbuf chain.
1107 ip6_savecontrol(in6p, mp, ip6, m)
1110 struct ip6_hdr *ip6;
1113 struct thread *td = curthread; /* XXX */
1115 int rthdr_exist = 0;
1122 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
1126 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1127 SCM_TIMESTAMP, SOL_SOCKET);
1129 mp = &(*mp)->m_next;
1134 /* RFC 2292 sec. 5 */
1135 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1136 struct in6_pktinfo pi6;
1137 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1138 if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
1139 pi6.ipi6_addr.s6_addr16[1] = 0;
1140 pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
1141 ? m->m_pkthdr.rcvif->if_index
1143 *mp = sbcreatecontrol((caddr_t) &pi6,
1144 sizeof(struct in6_pktinfo), IPV6_PKTINFO,
1147 mp = &(*mp)->m_next;
1150 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
1151 int hlim = ip6->ip6_hlim & 0xff;
1152 *mp = sbcreatecontrol((caddr_t) &hlim,
1153 sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
1155 mp = &(*mp)->m_next;
1159 * IPV6_HOPOPTS socket option. We require super-user privilege
1160 * for the option, but it might be too strict, since there might
1161 * be some hop-by-hop options which can be returned to normal user.
1162 * See RFC 2292 section 6.
1164 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
1166 * Check if a hop-by-hop options header is contatined in the
1167 * received packet, and if so, store the options as ancillary
1168 * data. Note that a hop-by-hop options header must be
1169 * just after the IPv6 header, which fact is assured through
1170 * the IPv6 input processing.
1172 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1173 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1174 struct ip6_hbh *hbh;
1176 #ifdef PULLDOWN_TEST
1180 #ifndef PULLDOWN_TEST
1181 hbh = (struct ip6_hbh *)(ip6 + 1);
1182 hbhlen = (hbh->ip6h_len + 1) << 3;
1184 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1187 ip6stat.ip6s_tooshort++;
1190 hbh = mtod(ext, struct ip6_hbh *);
1191 hbhlen = (hbh->ip6h_len + 1) << 3;
1192 if (hbhlen != ext->m_len) {
1194 ip6stat.ip6s_tooshort++;
1200 * XXX: We copy whole the header even if a jumbo
1201 * payload option is included, which option is to
1202 * be removed before returning in the RFC 2292.
1203 * Note: this constraint is removed in 2292bis.
1205 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1206 IPV6_HOPOPTS, IPPROTO_IPV6);
1208 mp = &(*mp)->m_next;
1209 #ifdef PULLDOWN_TEST
1215 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1216 if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
1217 int proto, off, nxt;
1220 * go through the header chain to see if a routing header is
1221 * contained in the packet. We need this information to store
1222 * destination options headers (if any) properly.
1223 * XXX: performance issue. We should record this info when
1224 * processing extension headers in incoming routine.
1227 proto = IPPROTO_IPV6;
1233 newoff = ip6_nexthdr(m, off, proto, &nxt);
1236 if (newoff < off) /* invalid, check for safety */
1238 if ((proto = nxt) == IPPROTO_ROUTING) {
1246 if ((in6p->in6p_flags &
1247 (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
1248 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1249 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1252 * Search for destination options headers or routing
1253 * header(s) through the header chain, and stores each
1254 * header as ancillary data.
1255 * Note that the order of the headers remains in
1256 * the chain of ancillary data.
1258 while (1) { /* is explicit loop prevention necessary? */
1259 struct ip6_ext *ip6e = NULL;
1261 #ifdef PULLDOWN_TEST
1262 struct mbuf *ext = NULL;
1266 * if it is not an extension header, don't try to
1267 * pull it from the chain.
1270 case IPPROTO_DSTOPTS:
1271 case IPPROTO_ROUTING:
1272 case IPPROTO_HOPOPTS:
1273 case IPPROTO_AH: /* is it possible? */
1279 #ifndef PULLDOWN_TEST
1280 if (off + sizeof(*ip6e) > m->m_len)
1282 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1283 if (nxt == IPPROTO_AH)
1284 elen = (ip6e->ip6e_len + 2) << 2;
1286 elen = (ip6e->ip6e_len + 1) << 3;
1287 if (off + elen > m->m_len)
1290 ext = ip6_pullexthdr(m, off, nxt);
1292 ip6stat.ip6s_tooshort++;
1295 ip6e = mtod(ext, struct ip6_ext *);
1296 if (nxt == IPPROTO_AH)
1297 elen = (ip6e->ip6e_len + 2) << 2;
1299 elen = (ip6e->ip6e_len + 1) << 3;
1300 if (elen != ext->m_len) {
1302 ip6stat.ip6s_tooshort++;
1308 case IPPROTO_DSTOPTS:
1309 if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0)
1313 * We also require super-user privilege for
1315 * See the comments on IN6_HOPOPTS.
1320 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1324 mp = &(*mp)->m_next;
1326 case IPPROTO_ROUTING:
1327 if (!in6p->in6p_flags & IN6P_RTHDR)
1330 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1334 mp = &(*mp)->m_next;
1336 case IPPROTO_HOPOPTS:
1337 case IPPROTO_AH: /* is it possible? */
1342 * other cases have been filtered in the above.
1343 * none will visit this case. here we supply
1344 * the code just in case (nxt overwritten or
1347 #ifdef PULLDOWN_TEST
1354 /* proceed with the next header. */
1356 nxt = ip6e->ip6e_nxt;
1358 #ifdef PULLDOWN_TEST
1369 #ifdef PULLDOWN_TEST
1371 * pull single extension header from mbuf chain. returns single mbuf that
1372 * contains the result, or NULL on error.
1374 static struct mbuf *
1375 ip6_pullexthdr(m, off, nxt)
1380 struct ip6_ext ip6e;
1386 case IPPROTO_DSTOPTS:
1387 case IPPROTO_ROUTING:
1388 case IPPROTO_HOPOPTS:
1389 case IPPROTO_AH: /* is it possible? */
1392 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1396 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1397 if (nxt == IPPROTO_AH)
1398 elen = (ip6e.ip6e_len + 2) << 2;
1400 elen = (ip6e.ip6e_len + 1) << 3;
1402 MGET(n, M_DONTWAIT, MT_DATA);
1403 if (n && elen >= MLEN) {
1404 MCLGET(n, M_DONTWAIT);
1405 if ((n->m_flags & M_EXT) == 0) {
1414 if (elen >= M_TRAILINGSPACE(n)) {
1419 m_copydata(m, off, elen, mtod(n, caddr_t));
1426 * Get pointer to the previous header followed by the header
1427 * currently processed.
1428 * XXX: This function supposes that
1429 * M includes all headers,
1430 * the next header field and the header length field of each header
1432 * the sum of each header length equals to OFF.
1433 * Because of these assumptions, this function must be called very
1434 * carefully. Moreover, it will not be used in the near future when
1435 * we develop `neater' mechanism to process extension headers.
1438 ip6_get_prevhdr(m, off)
1442 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1444 if (off == sizeof(struct ip6_hdr))
1445 return(&ip6->ip6_nxt);
1448 struct ip6_ext *ip6e = NULL;
1451 len = sizeof(struct ip6_hdr);
1453 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1456 case IPPROTO_FRAGMENT:
1457 len += sizeof(struct ip6_frag);
1460 len += (ip6e->ip6e_len + 2) << 2;
1463 len += (ip6e->ip6e_len + 1) << 3;
1466 nxt = ip6e->ip6e_nxt;
1469 return(&ip6e->ip6e_nxt);
1476 * get next header offset. m will be retained.
1479 ip6_nexthdr(m, off, proto, nxtp)
1486 struct ip6_ext ip6e;
1491 panic("ip6_nexthdr: m == NULL");
1492 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1497 if (m->m_pkthdr.len < off + sizeof(ip6))
1499 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1501 *nxtp = ip6.ip6_nxt;
1505 case IPPROTO_FRAGMENT:
1507 * terminate parsing if it is not the first fragment,
1508 * it does not make sense to parse through it.
1510 if (m->m_pkthdr.len < off + sizeof(fh))
1512 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1513 if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
1516 *nxtp = fh.ip6f_nxt;
1517 off += sizeof(struct ip6_frag);
1521 if (m->m_pkthdr.len < off + sizeof(ip6e))
1523 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1525 *nxtp = ip6e.ip6e_nxt;
1526 off += (ip6e.ip6e_len + 2) << 2;
1529 case IPPROTO_HOPOPTS:
1530 case IPPROTO_ROUTING:
1531 case IPPROTO_DSTOPTS:
1532 if (m->m_pkthdr.len < off + sizeof(ip6e))
1534 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1536 *nxtp = ip6e.ip6e_nxt;
1537 off += (ip6e.ip6e_len + 1) << 3;
1542 case IPPROTO_IPCOMP:
1554 * get offset for the last header in the chain. m will be kept untainted.
1557 ip6_lasthdr(m, off, proto, nxtp)
1571 newoff = ip6_nexthdr(m, off, proto, nxtp);
1574 else if (newoff < off)
1575 return -1; /* invalid */
1576 else if (newoff == off)
1588 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1590 tag = m_tag_get(PACKET_TAG_IPV6_INPUT,
1591 sizeof (struct ip6aux),
1594 m_tag_prepend(m, tag);
1597 bzero(tag+1, sizeof (struct ip6aux));
1598 return tag ? (struct ip6aux*)(tag+1) : NULL;
1605 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1606 return tag ? (struct ip6aux*)(tag+1) : NULL;
1613 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1615 m_tag_delete(m, tag);
1619 * System control for IP6
1622 u_char inet6ctlerrmap[PRC_NCMDS] = {
1624 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1625 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1626 EMSGSIZE, EHOSTUNREACH, 0, 0,