2 /* $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei 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 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
65 #include "opt_inet6.h"
66 #include "opt_ipsec.h"
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #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>
83 #include <net/if_types.h>
84 #include <net/if_dl.h>
85 #include <net/route.h>
86 #include <net/netisr.h>
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/ip_icmp.h>
95 #include <netinet/ip6.h>
96 #include <netinet6/in6_var.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/scope6_var.h>
101 #include <netinet6/in6_ifattach.h>
102 #include <netinet6/nd6.h>
105 #include <netinet6/ipsec.h>
107 #include <netinet6/ipsec6.h>
112 #include <netipsec/ipsec.h>
113 #include <netipsec/ipsec6.h>
115 #endif /* FAST_IPSEC */
117 #include <netinet6/ip6protosw.h>
119 extern struct domain inet6domain;
121 u_char ip6_protox[IPPROTO_MAX];
122 static struct ifqueue ip6intrq;
123 static int ip6qmaxlen = IFQ_MAXLEN;
124 struct in6_ifaddr *in6_ifaddr;
126 extern struct callout in6_tmpaddrtimer_ch;
128 int ip6_forward_srcrt; /* XXX */
129 int ip6_sourcecheck; /* XXX */
130 int ip6_sourcecheck_interval; /* XXX */
132 int ip6_ours_check_algorithm;
134 struct pfil_head inet6_pfil_hook;
136 struct ip6stat ip6stat;
138 static void ip6_init2 __P((void *));
139 static struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
140 static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
142 static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
146 * IP6 initialization: fill in IP6 protocol switch table.
147 * All protocols not implemented in kernel go to raw IP6 protocol handler.
152 struct ip6protosw *pr;
156 if (sizeof(struct protosw) != sizeof(struct ip6protosw))
157 panic("sizeof(protosw) != sizeof(ip6protosw)");
159 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
163 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
164 for (i = 0; i < IPPROTO_MAX; i++)
165 ip6_protox[i] = pr - inet6sw;
167 * Cycle through IP protocols and put them into the appropriate place
170 for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
171 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
172 if (pr->pr_domain->dom_family == PF_INET6 &&
173 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
174 /* Be careful to only index valid IP protocols. */
175 if (pr->pr_protocol < IPPROTO_MAX)
176 ip6_protox[pr->pr_protocol] = pr - inet6sw;
179 /* Initialize packet filter hooks. */
180 inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
181 inet6_pfil_hook.ph_af = AF_INET6;
182 if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
183 printf("%s: WARNING: unable to register pfil hook, "
184 "error %d\n", __func__, i);
186 ip6intrq.ifq_maxlen = ip6qmaxlen;
187 mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
188 netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0);
190 addrsel_policy_init();
193 ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
202 callout_init(&nd6_timer_ch, 0);
203 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
205 /* timer for regeneranation of temporary addresses randomize ID */
206 callout_init(&in6_tmpaddrtimer_ch, 0);
207 callout_reset(&in6_tmpaddrtimer_ch,
208 (ip6_temp_preferred_lifetime - ip6_desync_factor -
209 ip6_temp_regen_advance) * hz,
210 in6_tmpaddrtimer, NULL);
214 /* This must be after route_init(), which is now SI_ORDER_THIRD */
215 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
217 extern struct route_in6 ip6_forward_rt;
224 int off = sizeof(struct ip6_hdr), nest;
226 u_int32_t rtalert = ~0;
228 struct ifnet *deliverifp = NULL;
229 struct in6_addr odst;
232 GIANT_REQUIRED; /* XXX for now */
235 * should the inner packet be considered authentic?
236 * see comment in ah4_input().
239 m->m_flags &= ~M_AUTHIPHDR;
240 m->m_flags &= ~M_AUTHIPDGM;
245 * make sure we don't have onion peering information into m_tag.
252 if (m->m_flags & M_EXT) {
254 ip6stat.ip6s_mext2m++;
256 ip6stat.ip6s_mext1++;
258 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
260 if (m->m_flags & M_LOOP) {
261 ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
262 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
263 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
265 ip6stat.ip6s_m2m[0]++;
271 /* drop the packet if IPv6 operation is disabled on the IF */
272 if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) {
277 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
278 ip6stat.ip6s_total++;
280 #ifndef PULLDOWN_TEST
282 * L2 bridge code and some other code can return mbuf chain
283 * that does not conform to KAME requirement. too bad.
284 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
286 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
289 MGETHDR(n, M_DONTWAIT, MT_HEADER);
292 if (n && n->m_pkthdr.len > MHLEN) {
293 MCLGET(n, M_DONTWAIT);
294 if ((n->m_flags & M_EXT) == 0) {
301 return; /* ENOBUFS */
304 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
305 n->m_len = n->m_pkthdr.len;
309 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
312 if (m->m_len < sizeof(struct ip6_hdr)) {
314 inifp = m->m_pkthdr.rcvif;
315 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
316 ip6stat.ip6s_toosmall++;
317 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
322 ip6 = mtod(m, struct ip6_hdr *);
324 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
325 ip6stat.ip6s_badvers++;
326 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
330 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
333 * Check against address spoofing/corruption.
335 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
336 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
338 * XXX: "badscope" is not very suitable for a multicast source.
340 ip6stat.ip6s_badscope++;
341 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
344 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
345 !(m->m_flags & M_LOOP)) {
347 * In this case, the packet should come from the loopback
348 * interface. However, we cannot just check the if_flags,
349 * because ip6_mloopback() passes the "actual" interface
350 * as the outgoing/incoming interface.
352 ip6stat.ip6s_badscope++;
353 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
358 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
359 /* packet is dropped by traffic conditioner */
364 * The following check is not documented in specs. A malicious
365 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
366 * and bypass security checks (act as if it was from 127.0.0.1 by using
367 * IPv6 src ::ffff:127.0.0.1). Be cautious.
369 * This check chokes if we are in an SIIT cloud. As none of BSDs
370 * support IPv4-less kernel compilation, we cannot support SIIT
371 * environment at all. So, it makes more sense for us to reject any
372 * malicious packets for non-SIIT environment, than try to do a
373 * partial support for SIIT environment.
375 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
376 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
377 ip6stat.ip6s_badscope++;
378 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
383 * Reject packets with IPv4 compatible addresses (auto tunnel).
385 * The code forbids auto tunnel relay case in RFC1933 (the check is
386 * stronger than RFC1933). We may want to re-enable it if mech-xx
387 * is revised to forbid relaying case.
389 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
390 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
391 ip6stat.ip6s_badscope++;
392 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
398 * Run through list of hooks for input packets.
400 * NB: Beware of the destination address changing
401 * (e.g. by NAT rewriting). When this happens,
402 * tell ip6_forward to do the right thing.
406 /* Jump over all PFIL processing if hooks are not active. */
407 if (!PFIL_HOOKED(&inet6_pfil_hook))
410 if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL))
412 if (m == NULL) /* consumed by filter */
414 ip6 = mtod(m, struct ip6_hdr *);
415 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
419 * Disambiguate address scope zones (if there is ambiguity).
420 * We first make sure that the original source or destination address
421 * is not in our internal form for scoped addresses. Such addresses
422 * are not necessarily invalid spec-wise, but we cannot accept them due
423 * to the usage conflict.
424 * in6_setscope() then also checks and rejects the cases where src or
425 * dst are the loopback address and the receiving interface
428 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
429 ip6stat.ip6s_badscope++; /* XXX */
432 if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
433 in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
434 ip6stat.ip6s_badscope++;
441 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
442 struct in6_multi *in6m = 0;
444 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
446 * See if we belong to the destination multicast group on the
449 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
452 else if (!ip6_mrouter) {
453 ip6stat.ip6s_notmember++;
454 ip6stat.ip6s_cantforward++;
455 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
458 deliverifp = m->m_pkthdr.rcvif;
465 if (ip6_forward_rt.ro_rt != NULL &&
466 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
467 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
468 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
469 ip6stat.ip6s_forward_cachehit++;
471 struct sockaddr_in6 *dst6;
473 if (ip6_forward_rt.ro_rt) {
474 /* route is down or destination is different */
475 ip6stat.ip6s_forward_cachemiss++;
476 RTFREE(ip6_forward_rt.ro_rt);
477 ip6_forward_rt.ro_rt = 0;
480 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
481 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
482 dst6->sin6_len = sizeof(struct sockaddr_in6);
483 dst6->sin6_family = AF_INET6;
484 dst6->sin6_addr = ip6->ip6_dst;
486 rtalloc((struct route *)&ip6_forward_rt);
489 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
492 * Accept the packet if the forwarding interface to the destination
493 * according to the routing table is the loopback interface,
494 * unless the associated route has a gateway.
495 * Note that this approach causes to accept a packet if there is a
496 * route to the loopback interface for the destination of the packet.
497 * But we think it's even useful in some situations, e.g. when using
498 * a special daemon which wants to intercept the packet.
500 * XXX: some OSes automatically make a cloned route for the destination
501 * of an outgoing packet. If the outgoing interface of the packet
502 * is a loopback one, the kernel would consider the packet to be
503 * accepted, even if we have no such address assinged on the interface.
504 * We check the cloned flag of the route entry to reject such cases,
505 * assuming that route entries for our own addresses are not made by
506 * cloning (it should be true because in6_addloop explicitly installs
507 * the host route). However, we might have to do an explicit check
508 * while it would be less efficient. Or, should we rather install a
509 * reject route for such a case?
511 if (ip6_forward_rt.ro_rt &&
512 (ip6_forward_rt.ro_rt->rt_flags &
513 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
515 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
518 !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
522 * The check below is redundant since the comparison of
523 * the destination and the key of the rtentry has
524 * already done through looking up the routing table.
526 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
527 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
529 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
530 struct in6_ifaddr *ia6 =
531 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
534 * record address information into m_tag.
536 (void)ip6_setdstifaddr(m, ia6);
539 * packets to a tentative, duplicated, or somehow invalid
540 * address must not be accepted.
542 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
543 /* this address is ready */
545 deliverifp = ia6->ia_ifp; /* correct? */
546 /* Count the packet in the ip address stats */
547 ia6->ia_ifa.if_ipackets++;
548 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
551 char ip6bufs[INET6_ADDRSTRLEN];
552 char ip6bufd[INET6_ADDRSTRLEN];
553 /* address is not ready, so discard the packet. */
555 "ip6_input: packet to an unready address %s->%s\n",
556 ip6_sprintf(ip6bufs, &ip6->ip6_src),
557 ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
564 * FAITH (Firewall Aided Internet Translator)
567 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
568 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
569 /* XXX do we need more sanity checks? */
571 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
577 * Now there is no reason to process the packet if it's not our own
578 * and we're not a router.
580 if (!ip6_forwarding) {
581 ip6stat.ip6s_cantforward++;
582 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
588 * record address information into m_tag, if we don't have one yet.
589 * note that we are unable to record it, if the address is not listed
590 * as our interface address (e.g. multicast addresses, addresses
591 * within FAITH prefixes and such).
593 if (deliverifp && !ip6_getdstifaddr(m)) {
594 struct in6_ifaddr *ia6;
596 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
598 if (!ip6_setdstifaddr(m, ia6)) {
600 * XXX maybe we should drop the packet here,
601 * as we could not provide enough information
602 * to the upper layers.
609 * Process Hop-by-Hop options header if it's contained.
610 * m may be modified in ip6_hopopts_input().
611 * If a JumboPayload option is included, plen will also be modified.
613 plen = (u_int32_t)ntohs(ip6->ip6_plen);
614 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
617 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
618 #if 0 /*touches NULL pointer*/
619 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
621 return; /* m have already been freed */
625 ip6 = mtod(m, struct ip6_hdr *);
628 * if the payload length field is 0 and the next header field
629 * indicates Hop-by-Hop Options header, then a Jumbo Payload
630 * option MUST be included.
632 if (ip6->ip6_plen == 0 && plen == 0) {
634 * Note that if a valid jumbo payload option is
635 * contained, ip6_hopopts_input() must set a valid
636 * (non-zero) payload length to the variable plen.
638 ip6stat.ip6s_badoptions++;
639 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
640 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
641 icmp6_error(m, ICMP6_PARAM_PROB,
642 ICMP6_PARAMPROB_HEADER,
643 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
646 #ifndef PULLDOWN_TEST
647 /* ip6_hopopts_input() ensures that mbuf is contiguous */
648 hbh = (struct ip6_hbh *)(ip6 + 1);
650 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
651 sizeof(struct ip6_hbh));
653 ip6stat.ip6s_tooshort++;
660 * If we are acting as a router and the packet contains a
661 * router alert option, see if we know the option value.
662 * Currently, we only support the option value for MLD, in which
663 * case we should pass the packet to the multicast routing
666 if (rtalert != ~0 && ip6_forwarding) {
668 case IP6OPT_RTALERT_MLD:
673 * RFC2711 requires unrecognized values must be
683 * Check that the amount of data in the buffers
684 * is as at least much as the IPv6 header would have us expect.
685 * Trim mbufs if longer than we expect.
686 * Drop packet if shorter than we expect.
688 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
689 ip6stat.ip6s_tooshort++;
690 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
693 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
694 if (m->m_len == m->m_pkthdr.len) {
695 m->m_len = sizeof(struct ip6_hdr) + plen;
696 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
698 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
702 * Forward if desirable.
704 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
706 * If we are acting as a multicast router, all
707 * incoming multicast packets are passed to the
708 * kernel-level multicast forwarding function.
709 * The packet is returned (relatively) intact; if
710 * ip6_mforward() returns a non-zero value, the packet
711 * must be discarded, else it may be accepted below.
713 if (ip6_mrouter && ip6_mforward &&
714 ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
715 ip6stat.ip6s_cantforward++;
724 ip6_forward(m, srcrt);
728 ip6 = mtod(m, struct ip6_hdr *);
731 * Malicious party may be able to use IPv4 mapped addr to confuse
732 * tcp/udp stack and bypass security checks (act as if it was from
733 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
735 * For SIIT end node behavior, you may want to disable the check.
736 * However, you will become vulnerable to attacks using IPv4 mapped
739 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
740 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
741 ip6stat.ip6s_badscope++;
742 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
747 * Tell launch routine the next header
749 ip6stat.ip6s_delivered++;
750 in6_ifstat_inc(deliverifp, ifs6_in_deliver);
753 while (nxt != IPPROTO_DONE) {
754 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
755 ip6stat.ip6s_toomanyhdr++;
760 * protection against faulty packet - there should be
761 * more sanity checks in header chain processing.
763 if (m->m_pkthdr.len < off) {
764 ip6stat.ip6s_tooshort++;
765 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
771 * enforce IPsec policy checking if we are seeing last header.
772 * note that we do not visit this with protocols with pcb layer
773 * code - like udp/tcp/raw ip.
775 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
776 ipsec6_in_reject(m, NULL)) {
777 ipsec6stat.in_polvio++;
781 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
789 * set/grab in6_ifaddr correspond to IPv6 destination address.
790 * XXX backward compatibility wrapper
792 static struct ip6aux *
793 ip6_setdstifaddr(m, ia6)
795 struct in6_ifaddr *ia6;
799 ip6a = ip6_addaux(m);
801 ip6a->ip6a_dstia6 = ia6;
802 return ip6a; /* NULL if failed to set */
811 ip6a = ip6_findaux(m);
813 return ip6a->ip6a_dstia6;
819 * Hop-by-Hop options header processing. If a valid jumbo payload option is
820 * included, the real payload length will be stored in plenp.
823 ip6_hopopts_input(plenp, rtalertp, mp, offp)
825 u_int32_t *rtalertp; /* XXX: should be stored more smart way */
829 struct mbuf *m = *mp;
830 int off = *offp, hbhlen;
834 /* validation of the length of the header */
835 #ifndef PULLDOWN_TEST
836 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
837 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
838 hbhlen = (hbh->ip6h_len + 1) << 3;
840 IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
841 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
843 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
844 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
846 ip6stat.ip6s_tooshort++;
849 hbhlen = (hbh->ip6h_len + 1) << 3;
850 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
853 ip6stat.ip6s_tooshort++;
858 hbhlen -= sizeof(struct ip6_hbh);
859 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
861 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
862 hbhlen, rtalertp, plenp) < 0)
871 * Search header for all Hop-by-hop options and process each option.
872 * This function is separate from ip6_hopopts_input() in order to
873 * handle a case where the sending node itself process its hop-by-hop
874 * options header. In such a case, the function is called from ip6_output().
876 * The function assumes that hbh header is located right after the IPv6 header
877 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
878 * opthead + hbhlen is located in continuous memory region.
881 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
890 u_int8_t *opt = opthead;
891 u_int16_t rtalert_val;
893 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
895 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
901 if (hbhlen < IP6OPT_MINLEN) {
902 ip6stat.ip6s_toosmall++;
905 optlen = *(opt + 1) + 2;
907 case IP6OPT_ROUTER_ALERT:
908 /* XXX may need check for alignment */
909 if (hbhlen < IP6OPT_RTALERT_LEN) {
910 ip6stat.ip6s_toosmall++;
913 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
915 icmp6_error(m, ICMP6_PARAM_PROB,
916 ICMP6_PARAMPROB_HEADER,
917 erroff + opt + 1 - opthead);
920 optlen = IP6OPT_RTALERT_LEN;
921 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
922 *rtalertp = ntohs(rtalert_val);
925 /* XXX may need check for alignment */
926 if (hbhlen < IP6OPT_JUMBO_LEN) {
927 ip6stat.ip6s_toosmall++;
930 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
932 icmp6_error(m, ICMP6_PARAM_PROB,
933 ICMP6_PARAMPROB_HEADER,
934 erroff + opt + 1 - opthead);
937 optlen = IP6OPT_JUMBO_LEN;
940 * IPv6 packets that have non 0 payload length
941 * must not contain a jumbo payload option.
943 ip6 = mtod(m, struct ip6_hdr *);
945 ip6stat.ip6s_badoptions++;
946 icmp6_error(m, ICMP6_PARAM_PROB,
947 ICMP6_PARAMPROB_HEADER,
948 erroff + opt - opthead);
953 * We may see jumbolen in unaligned location, so
954 * we'd need to perform bcopy().
956 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
957 jumboplen = (u_int32_t)htonl(jumboplen);
961 * if there are multiple jumbo payload options,
962 * *plenp will be non-zero and the packet will be
964 * the behavior may need some debate in ipngwg -
965 * multiple options does not make sense, however,
966 * there's no explicit mention in specification.
969 ip6stat.ip6s_badoptions++;
970 icmp6_error(m, ICMP6_PARAM_PROB,
971 ICMP6_PARAMPROB_HEADER,
972 erroff + opt + 2 - opthead);
978 * jumbo payload length must be larger than 65535.
980 if (jumboplen <= IPV6_MAXPACKET) {
981 ip6stat.ip6s_badoptions++;
982 icmp6_error(m, ICMP6_PARAM_PROB,
983 ICMP6_PARAMPROB_HEADER,
984 erroff + opt + 2 - opthead);
990 default: /* unknown option */
991 if (hbhlen < IP6OPT_MINLEN) {
992 ip6stat.ip6s_toosmall++;
995 optlen = ip6_unknown_opt(opt, m,
996 erroff + opt - opthead);
1012 * Unknown option processing.
1013 * The third argument `off' is the offset from the IPv6 header to the option,
1014 * which is necessary if the IPv6 header the and option header and IPv6 header
1015 * is not continuous in order to return an ICMPv6 error.
1018 ip6_unknown_opt(optp, m, off)
1023 struct ip6_hdr *ip6;
1025 switch (IP6OPT_TYPE(*optp)) {
1026 case IP6OPT_TYPE_SKIP: /* ignore the option */
1027 return ((int)*(optp + 1));
1028 case IP6OPT_TYPE_DISCARD: /* silently discard */
1031 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1032 ip6stat.ip6s_badoptions++;
1033 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1035 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1036 ip6stat.ip6s_badoptions++;
1037 ip6 = mtod(m, struct ip6_hdr *);
1038 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1039 (m->m_flags & (M_BCAST|M_MCAST)))
1042 icmp6_error(m, ICMP6_PARAM_PROB,
1043 ICMP6_PARAMPROB_OPTION, off);
1047 m_freem(m); /* XXX: NOTREACHED */
1052 * Create the "control" list for this pcb.
1053 * The function will not modify mbuf chain at all.
1055 * with KAME mbuf chain restriction:
1056 * The routine will be called from upper layer handlers like tcp6_input().
1057 * Thus the routine assumes that the caller (tcp6_input) have already
1058 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1059 * very first mbuf on the mbuf chain.
1062 ip6_savecontrol(in6p, m, mp)
1064 struct mbuf *m, **mp;
1066 #define IS2292(x, y) ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1067 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1070 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
1074 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1075 SCM_TIMESTAMP, SOL_SOCKET);
1077 mp = &(*mp)->m_next;
1081 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1084 /* RFC 2292 sec. 5 */
1085 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1086 struct in6_pktinfo pi6;
1088 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1089 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1091 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1093 *mp = sbcreatecontrol((caddr_t) &pi6,
1094 sizeof(struct in6_pktinfo),
1095 IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1097 mp = &(*mp)->m_next;
1100 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
1101 int hlim = ip6->ip6_hlim & 0xff;
1103 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1104 IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1106 mp = &(*mp)->m_next;
1109 if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1113 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1116 tclass = flowinfo & 0xff;
1117 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1118 IPV6_TCLASS, IPPROTO_IPV6);
1120 mp = &(*mp)->m_next;
1124 * IPV6_HOPOPTS socket option. Recall that we required super-user
1125 * privilege for the option (see ip6_ctloutput), but it might be too
1126 * strict, since there might be some hop-by-hop options which can be
1127 * returned to normal user.
1128 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1130 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1132 * Check if a hop-by-hop options header is contatined in the
1133 * received packet, and if so, store the options as ancillary
1134 * data. Note that a hop-by-hop options header must be
1135 * just after the IPv6 header, which is assured through the
1136 * IPv6 input processing.
1138 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1139 struct ip6_hbh *hbh;
1141 #ifdef PULLDOWN_TEST
1145 #ifndef PULLDOWN_TEST
1146 hbh = (struct ip6_hbh *)(ip6 + 1);
1147 hbhlen = (hbh->ip6h_len + 1) << 3;
1149 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1152 ip6stat.ip6s_tooshort++;
1155 hbh = mtod(ext, struct ip6_hbh *);
1156 hbhlen = (hbh->ip6h_len + 1) << 3;
1157 if (hbhlen != ext->m_len) {
1159 ip6stat.ip6s_tooshort++;
1165 * XXX: We copy the whole header even if a
1166 * jumbo payload option is included, the option which
1167 * is to be removed before returning according to
1169 * Note: this constraint is removed in RFC3542
1171 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1172 IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1175 mp = &(*mp)->m_next;
1176 #ifdef PULLDOWN_TEST
1182 if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1183 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1186 * Search for destination options headers or routing
1187 * header(s) through the header chain, and stores each
1188 * header as ancillary data.
1189 * Note that the order of the headers remains in
1190 * the chain of ancillary data.
1192 while (1) { /* is explicit loop prevention necessary? */
1193 struct ip6_ext *ip6e = NULL;
1195 #ifdef PULLDOWN_TEST
1196 struct mbuf *ext = NULL;
1200 * if it is not an extension header, don't try to
1201 * pull it from the chain.
1204 case IPPROTO_DSTOPTS:
1205 case IPPROTO_ROUTING:
1206 case IPPROTO_HOPOPTS:
1207 case IPPROTO_AH: /* is it possible? */
1213 #ifndef PULLDOWN_TEST
1214 if (off + sizeof(*ip6e) > m->m_len)
1216 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1217 if (nxt == IPPROTO_AH)
1218 elen = (ip6e->ip6e_len + 2) << 2;
1220 elen = (ip6e->ip6e_len + 1) << 3;
1221 if (off + elen > m->m_len)
1224 ext = ip6_pullexthdr(m, off, nxt);
1226 ip6stat.ip6s_tooshort++;
1229 ip6e = mtod(ext, struct ip6_ext *);
1230 if (nxt == IPPROTO_AH)
1231 elen = (ip6e->ip6e_len + 2) << 2;
1233 elen = (ip6e->ip6e_len + 1) << 3;
1234 if (elen != ext->m_len) {
1236 ip6stat.ip6s_tooshort++;
1242 case IPPROTO_DSTOPTS:
1243 if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1246 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1247 IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1250 mp = &(*mp)->m_next;
1252 case IPPROTO_ROUTING:
1253 if (!in6p->in6p_flags & IN6P_RTHDR)
1256 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1257 IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1260 mp = &(*mp)->m_next;
1262 case IPPROTO_HOPOPTS:
1263 case IPPROTO_AH: /* is it possible? */
1268 * other cases have been filtered in the above.
1269 * none will visit this case. here we supply
1270 * the code just in case (nxt overwritten or
1273 #ifdef PULLDOWN_TEST
1280 /* proceed with the next header. */
1282 nxt = ip6e->ip6e_nxt;
1284 #ifdef PULLDOWN_TEST
1297 ip6_notify_pmtu(in6p, dst, mtu)
1299 struct sockaddr_in6 *dst;
1304 struct ip6_mtuinfo mtuctl;
1306 so = in6p->inp_socket;
1312 if (so == NULL) /* I believe this is impossible */
1313 panic("ip6_notify_pmtu: socket is NULL");
1316 bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */
1317 mtuctl.ip6m_mtu = *mtu;
1318 mtuctl.ip6m_addr = *dst;
1319 if (sa6_recoverscope(&mtuctl.ip6m_addr))
1322 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1323 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1326 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1329 /* XXX: should count statistics */
1336 #ifdef PULLDOWN_TEST
1338 * pull single extension header from mbuf chain. returns single mbuf that
1339 * contains the result, or NULL on error.
1341 static struct mbuf *
1342 ip6_pullexthdr(m, off, nxt)
1347 struct ip6_ext ip6e;
1353 case IPPROTO_DSTOPTS:
1354 case IPPROTO_ROUTING:
1355 case IPPROTO_HOPOPTS:
1356 case IPPROTO_AH: /* is it possible? */
1359 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1363 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1364 if (nxt == IPPROTO_AH)
1365 elen = (ip6e.ip6e_len + 2) << 2;
1367 elen = (ip6e.ip6e_len + 1) << 3;
1369 MGET(n, M_DONTWAIT, MT_DATA);
1370 if (n && elen >= MLEN) {
1371 MCLGET(n, M_DONTWAIT);
1372 if ((n->m_flags & M_EXT) == 0) {
1381 if (elen >= M_TRAILINGSPACE(n)) {
1386 m_copydata(m, off, elen, mtod(n, caddr_t));
1393 * Get pointer to the previous header followed by the header
1394 * currently processed.
1395 * XXX: This function supposes that
1396 * M includes all headers,
1397 * the next header field and the header length field of each header
1399 * the sum of each header length equals to OFF.
1400 * Because of these assumptions, this function must be called very
1401 * carefully. Moreover, it will not be used in the near future when
1402 * we develop `neater' mechanism to process extension headers.
1405 ip6_get_prevhdr(m, off)
1409 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1411 if (off == sizeof(struct ip6_hdr))
1412 return (&ip6->ip6_nxt);
1415 struct ip6_ext *ip6e = NULL;
1418 len = sizeof(struct ip6_hdr);
1420 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1423 case IPPROTO_FRAGMENT:
1424 len += sizeof(struct ip6_frag);
1427 len += (ip6e->ip6e_len + 2) << 2;
1430 len += (ip6e->ip6e_len + 1) << 3;
1433 nxt = ip6e->ip6e_nxt;
1436 return (&ip6e->ip6e_nxt);
1443 * get next header offset. m will be retained.
1446 ip6_nexthdr(m, off, proto, nxtp)
1453 struct ip6_ext ip6e;
1458 panic("ip6_nexthdr: m == NULL");
1459 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1464 if (m->m_pkthdr.len < off + sizeof(ip6))
1466 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1468 *nxtp = ip6.ip6_nxt;
1472 case IPPROTO_FRAGMENT:
1474 * terminate parsing if it is not the first fragment,
1475 * it does not make sense to parse through it.
1477 if (m->m_pkthdr.len < off + sizeof(fh))
1479 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1480 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1481 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1484 *nxtp = fh.ip6f_nxt;
1485 off += sizeof(struct ip6_frag);
1489 if (m->m_pkthdr.len < off + sizeof(ip6e))
1491 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1493 *nxtp = ip6e.ip6e_nxt;
1494 off += (ip6e.ip6e_len + 2) << 2;
1497 case IPPROTO_HOPOPTS:
1498 case IPPROTO_ROUTING:
1499 case IPPROTO_DSTOPTS:
1500 if (m->m_pkthdr.len < off + sizeof(ip6e))
1502 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1504 *nxtp = ip6e.ip6e_nxt;
1505 off += (ip6e.ip6e_len + 1) << 3;
1510 case IPPROTO_IPCOMP:
1522 * get offset for the last header in the chain. m will be kept untainted.
1525 ip6_lasthdr(m, off, proto, nxtp)
1539 newoff = ip6_nexthdr(m, off, proto, nxtp);
1542 else if (newoff < off)
1543 return -1; /* invalid */
1544 else if (newoff == off)
1558 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1560 mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1563 m_tag_prepend(m, mtag);
1564 bzero(mtag + 1, sizeof(struct ip6aux));
1567 return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1576 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1577 return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1586 mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1588 m_tag_delete(m, mtag);
1592 * System control for IP6
1595 u_char inet6ctlerrmap[PRC_NCMDS] = {
1597 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1598 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1599 EMSGSIZE, EHOSTUNREACH, 0, 0,