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 * 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
68 #include "opt_ip6fw.h"
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
72 #include "opt_pfil_hooks.h"
73 #include "opt_random_ip_id.h"
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
80 #include <sys/domain.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/errno.h>
86 #include <sys/kernel.h>
87 #include <sys/syslog.h>
90 #include <net/if_types.h>
91 #include <net/if_dl.h>
92 #include <net/route.h>
93 #include <net/netisr.h>
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
101 #include <netinet/ip.h>
102 #include <netinet/ip_icmp.h>
104 #include <netinet/ip6.h>
105 #include <netinet6/in6_var.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet/in_pcb.h>
108 #include <netinet/icmp6.h>
109 #include <netinet6/in6_ifattach.h>
110 #include <netinet6/nd6.h>
111 #include <netinet6/in6_prefix.h>
114 #include <netinet6/ipsec.h>
116 #include <netinet6/ipsec6.h>
121 #include <netipsec/ipsec.h>
122 #include <netipsec/ipsec6.h>
124 #endif /* FAST_IPSEC */
126 #include <netinet6/ip6_fw.h>
128 #include <netinet6/ip6protosw.h>
130 #include <net/net_osdep.h>
132 extern struct domain inet6domain;
134 u_char ip6_protox[IPPROTO_MAX];
135 static struct ifqueue ip6intrq;
136 static int ip6qmaxlen = IFQ_MAXLEN;
137 struct in6_ifaddr *in6_ifaddr;
139 extern struct callout in6_tmpaddrtimer_ch;
141 int ip6_forward_srcrt; /* XXX */
142 int ip6_sourcecheck; /* XXX */
143 int ip6_sourcecheck_interval; /* XXX */
145 int ip6_ours_check_algorithm;
148 struct pfil_head inet6_pfil_hook;
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 __P((void *));
159 static struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
160 static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
162 static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
167 * IP6 initialization: fill in IP6 protocol switch table.
168 * All protocols not implemented in kernel go to raw IP6 protocol handler.
173 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;
191 inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
192 inet6_pfil_hook.ph_af = AF_INET6;
193 if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
194 printf("%s: WARNING: unable to register pfil hook, "
195 "error %d\n", __func__, i);
196 #endif /* PFIL_HOOKS */
197 ip6intrq.ifq_maxlen = ip6qmaxlen;
198 mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
199 netisr_register(NETISR_IPV6, ip6_input, &ip6intrq);
203 ip6_flow_seq = arc4random();
205 ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
214 * to route local address of p2p link to loopback,
215 * assign loopback address first.
217 in6_ifattach(&loif[0], NULL);
220 callout_init(&nd6_timer_ch, 0);
221 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
223 /* router renumbering prefix list maintenance */
224 callout_init(&in6_rr_timer_ch, 0);
225 callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
227 /* timer for regeneranation of temporary addresses randomize ID */
228 callout_reset(&in6_tmpaddrtimer_ch,
229 (ip6_temp_preferred_lifetime - ip6_desync_factor -
230 ip6_temp_regen_advance) * hz,
231 in6_tmpaddrtimer, NULL);
235 /* This must be after route_init(), which is now SI_ORDER_THIRD */
236 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
238 extern struct route_in6 ip6_forward_rt;
245 int off = sizeof(struct ip6_hdr), nest;
247 u_int32_t rtalert = ~0;
249 struct ifnet *deliverifp = NULL;
253 * should the inner packet be considered authentic?
254 * see comment in ah4_input().
257 m->m_flags &= ~M_AUTHIPHDR;
258 m->m_flags &= ~M_AUTHIPDGM;
263 * make sure we don't have onion peering information into m_aux.
270 if (m->m_flags & M_EXT) {
272 ip6stat.ip6s_mext2m++;
274 ip6stat.ip6s_mext1++;
276 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
278 if (m->m_flags & M_LOOP) {
279 ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
280 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
281 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
283 ip6stat.ip6s_m2m[0]++;
289 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
290 ip6stat.ip6s_total++;
292 #ifndef PULLDOWN_TEST
294 * L2 bridge code and some other code can return mbuf chain
295 * that does not conform to KAME requirement. too bad.
296 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
298 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
301 MGETHDR(n, M_DONTWAIT, MT_HEADER);
304 if (n && n->m_pkthdr.len > MHLEN) {
305 MCLGET(n, M_DONTWAIT);
306 if ((n->m_flags & M_EXT) == 0) {
316 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
317 n->m_len = n->m_pkthdr.len;
321 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
324 if (m->m_len < sizeof(struct ip6_hdr)) {
326 inifp = m->m_pkthdr.rcvif;
327 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
328 ip6stat.ip6s_toosmall++;
329 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
334 ip6 = mtod(m, struct ip6_hdr *);
336 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
337 ip6stat.ip6s_badvers++;
338 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
344 * Run through list of hooks for input packets.
346 if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN))
348 if (m == NULL) /* consumed by filter */
350 ip6 = mtod(m, struct ip6_hdr *);
351 #endif /* PFIL_HOOKS */
353 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
356 * Check with the firewall...
358 if (ip6_fw_enable && ip6_fw_chk_ptr) {
360 /* If ipfw says divert, we have to just drop packet */
361 /* use port as a dummy argument */
362 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
371 * Check against address spoofing/corruption.
373 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
374 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
376 * XXX: "badscope" is not very suitable for a multicast source.
378 ip6stat.ip6s_badscope++;
379 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
382 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
383 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
384 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
385 ip6stat.ip6s_badscope++;
386 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
391 * The following check is not documented in specs. A malicious
392 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
393 * and bypass security checks (act as if it was from 127.0.0.1 by using
394 * IPv6 src ::ffff:127.0.0.1). Be cautious.
396 * This check chokes if we are in an SIIT cloud. As none of BSDs
397 * support IPv4-less kernel compilation, we cannot support SIIT
398 * environment at all. So, it makes more sense for us to reject any
399 * malicious packets for non-SIIT environment, than try to do a
400 * partical support for SIIT environment.
402 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
403 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
404 ip6stat.ip6s_badscope++;
405 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
410 * Reject packets with IPv4 compatible addresses (auto tunnel).
412 * The code forbids auto tunnel relay case in RFC1933 (the check is
413 * stronger than RFC1933). We may want to re-enable it if mech-xx
414 * is revised to forbid relaying case.
416 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
417 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
418 ip6stat.ip6s_badscope++;
419 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
424 /* drop packets if interface ID portion is already filled */
425 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
426 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
427 ip6->ip6_src.s6_addr16[1]) {
428 ip6stat.ip6s_badscope++;
431 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
432 ip6->ip6_dst.s6_addr16[1]) {
433 ip6stat.ip6s_badscope++;
438 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
439 ip6->ip6_src.s6_addr16[1]
440 = htons(m->m_pkthdr.rcvif->if_index);
441 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
442 ip6->ip6_dst.s6_addr16[1]
443 = htons(m->m_pkthdr.rcvif->if_index);
445 #if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */
447 * We use rt->rt_ifp to determine if the address is ours or not.
448 * If rt_ifp is lo0, the address is ours.
449 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
450 * so any address under fe80::%lo0/64 will be mistakenly considered
451 * local. The special case is supplied to handle the case properly
452 * by actually looking at interface addresses
453 * (using in6ifa_ifpwithaddr).
455 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
456 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
457 if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
458 icmp6_error(m, ICMP6_DST_UNREACH,
459 ICMP6_DST_UNREACH_ADDR, 0);
460 /* m is already freed */
465 deliverifp = m->m_pkthdr.rcvif;
473 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
474 struct in6_multi *in6m = 0;
476 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
478 * See if we belong to the destination multicast group on the
481 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
484 else if (!ip6_mrouter) {
485 ip6stat.ip6s_notmember++;
486 ip6stat.ip6s_cantforward++;
487 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
490 deliverifp = m->m_pkthdr.rcvif;
497 switch (ip6_ours_check_algorithm) {
500 * XXX: I intentionally broke our indentation rule here,
501 * since this switch-case is just for measurement and
502 * therefore should soon be removed.
504 if (ip6_forward_rt.ro_rt != NULL &&
505 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
506 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
507 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
508 ip6stat.ip6s_forward_cachehit++;
510 struct sockaddr_in6 *dst6;
512 if (ip6_forward_rt.ro_rt) {
513 /* route is down or destination is different */
514 ip6stat.ip6s_forward_cachemiss++;
515 RTFREE(ip6_forward_rt.ro_rt);
516 ip6_forward_rt.ro_rt = 0;
519 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
520 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
521 dst6->sin6_len = sizeof(struct sockaddr_in6);
522 dst6->sin6_family = AF_INET6;
523 dst6->sin6_addr = ip6->ip6_dst;
525 ip6_forward_rt.ro_dst.sin6_scope_id =
526 in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst);
529 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
532 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
535 * Accept the packet if the forwarding interface to the destination
536 * according to the routing table is the loopback interface,
537 * unless the associated route has a gateway.
538 * Note that this approach causes to accept a packet if there is a
539 * route to the loopback interface for the destination of the packet.
540 * But we think it's even useful in some situations, e.g. when using
541 * a special daemon which wants to intercept the packet.
543 * XXX: some OSes automatically make a cloned route for the destination
544 * of an outgoing packet. If the outgoing interface of the packet
545 * is a loopback one, the kernel would consider the packet to be
546 * accepted, even if we have no such address assinged on the interface.
547 * We check the cloned flag of the route entry to reject such cases,
548 * assuming that route entries for our own addresses are not made by
549 * cloning (it should be true because in6_addloop explicitly installs
550 * the host route). However, we might have to do an explicit check
551 * while it would be less efficient. Or, should we rather install a
552 * reject route for such a case?
554 if (ip6_forward_rt.ro_rt &&
555 (ip6_forward_rt.ro_rt->rt_flags &
556 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
558 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
561 !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
565 * The check below is redundant since the comparison of
566 * the destination and the key of the rtentry has
567 * already done through looking up the routing table.
569 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
570 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
572 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
573 struct in6_ifaddr *ia6 =
574 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
577 * record address information into m_aux.
579 (void)ip6_setdstifaddr(m, ia6);
582 * packets to a tentative, duplicated, or somehow invalid
583 * address must not be accepted.
585 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
586 /* this address is ready */
588 deliverifp = ia6->ia_ifp; /* correct? */
589 /* Count the packet in the ip address stats */
590 ia6->ia_ifa.if_ipackets++;
591 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
594 /* address is not ready, so discard the packet. */
596 "ip6_input: packet to an unready address %s->%s\n",
597 ip6_sprintf(&ip6->ip6_src),
598 ip6_sprintf(&ip6->ip6_dst)));
603 } /* XXX indentation (see above) */
606 * FAITH(Firewall Aided Internet Translator)
609 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
610 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
611 /* XXX do we need more sanity checks? */
613 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
619 * Now there is no reason to process the packet if it's not our own
620 * and we're not a router.
622 if (!ip6_forwarding) {
623 ip6stat.ip6s_cantforward++;
624 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
630 * record address information into m_aux, if we don't have one yet.
631 * note that we are unable to record it, if the address is not listed
632 * as our interface address (e.g. multicast addresses, addresses
633 * within FAITH prefixes and such).
635 if (deliverifp && !ip6_getdstifaddr(m)) {
636 struct in6_ifaddr *ia6;
638 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
640 if (!ip6_setdstifaddr(m, ia6)) {
642 * XXX maybe we should drop the packet here,
643 * as we could not provide enough information
644 * to the upper layers.
651 * Process Hop-by-Hop options header if it's contained.
652 * m may be modified in ip6_hopopts_input().
653 * If a JumboPayload option is included, plen will also be modified.
655 plen = (u_int32_t)ntohs(ip6->ip6_plen);
656 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
659 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
660 #if 0 /*touches NULL pointer*/
661 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
663 return; /* m have already been freed */
667 ip6 = mtod(m, struct ip6_hdr *);
670 * if the payload length field is 0 and the next header field
671 * indicates Hop-by-Hop Options header, then a Jumbo Payload
672 * option MUST be included.
674 if (ip6->ip6_plen == 0 && plen == 0) {
676 * Note that if a valid jumbo payload option is
677 * contained, ip6_hoptops_input() must set a valid
678 * (non-zero) payload length to the variable plen.
680 ip6stat.ip6s_badoptions++;
681 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
682 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
683 icmp6_error(m, ICMP6_PARAM_PROB,
684 ICMP6_PARAMPROB_HEADER,
685 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
688 #ifndef PULLDOWN_TEST
689 /* ip6_hopopts_input() ensures that mbuf is contiguous */
690 hbh = (struct ip6_hbh *)(ip6 + 1);
692 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
693 sizeof(struct ip6_hbh));
695 ip6stat.ip6s_tooshort++;
702 * accept the packet if a router alert option is included
703 * and we act as an IPv6 router.
705 if (rtalert != ~0 && ip6_forwarding)
711 * Check that the amount of data in the buffers
712 * is as at least much as the IPv6 header would have us expect.
713 * Trim mbufs if longer than we expect.
714 * Drop packet if shorter than we expect.
716 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
717 ip6stat.ip6s_tooshort++;
718 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
721 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
722 if (m->m_len == m->m_pkthdr.len) {
723 m->m_len = sizeof(struct ip6_hdr) + plen;
724 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
726 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
730 * Forward if desirable.
732 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
734 * If we are acting as a multicast router, all
735 * incoming multicast packets are passed to the
736 * kernel-level multicast forwarding function.
737 * The packet is returned (relatively) intact; if
738 * ip6_mforward() returns a non-zero value, the packet
739 * must be discarded, else it may be accepted below.
741 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
742 ip6stat.ip6s_cantforward++;
755 ip6 = mtod(m, struct ip6_hdr *);
758 * Malicious party may be able to use IPv4 mapped addr to confuse
759 * tcp/udp stack and bypass security checks (act as if it was from
760 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
762 * For SIIT end node behavior, you may want to disable the check.
763 * However, you will become vulnerable to attacks using IPv4 mapped
766 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
767 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
768 ip6stat.ip6s_badscope++;
769 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
774 * Tell launch routine the next header
776 ip6stat.ip6s_delivered++;
777 in6_ifstat_inc(deliverifp, ifs6_in_deliver);
780 while (nxt != IPPROTO_DONE) {
781 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
782 ip6stat.ip6s_toomanyhdr++;
787 * protection against faulty packet - there should be
788 * more sanity checks in header chain processing.
790 if (m->m_pkthdr.len < off) {
791 ip6stat.ip6s_tooshort++;
792 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
798 * do we need to do it for every header? yeah, other
799 * functions can play with it (like re-allocate and copy).
801 mhist = ip6_addaux(m);
802 if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) {
803 hist = mtod(mhist, caddr_t) + mhist->m_len;
804 bcopy(&nxt, hist, sizeof(nxt));
805 mhist->m_len += sizeof(nxt);
807 ip6stat.ip6s_toomanyhdr++;
814 * enforce IPsec policy checking if we are seeing last header.
815 * note that we do not visit this with protocols with pcb layer
816 * code - like udp/tcp/raw ip.
818 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
819 ipsec6_in_reject(m, NULL)) {
820 ipsec6stat.in_polvio++;
825 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
833 * set/grab in6_ifaddr correspond to IPv6 destination address.
834 * XXX backward compatibility wrapper
836 static struct ip6aux *
837 ip6_setdstifaddr(m, ia6)
839 struct in6_ifaddr *ia6;
845 n->ip6a_dstia6 = ia6;
846 return n; /* NULL if failed to set */
857 return n->ip6a_dstia6;
863 * Hop-by-Hop options header processing. If a valid jumbo payload option is
864 * included, the real payload length will be stored in plenp.
867 ip6_hopopts_input(plenp, rtalertp, mp, offp)
869 u_int32_t *rtalertp; /* XXX: should be stored more smart way */
873 struct mbuf *m = *mp;
874 int off = *offp, hbhlen;
878 /* validation of the length of the header */
879 #ifndef PULLDOWN_TEST
880 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
881 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
882 hbhlen = (hbh->ip6h_len + 1) << 3;
884 IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
885 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
887 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
888 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
890 ip6stat.ip6s_tooshort++;
893 hbhlen = (hbh->ip6h_len + 1) << 3;
894 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
897 ip6stat.ip6s_tooshort++;
902 hbhlen -= sizeof(struct ip6_hbh);
903 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
905 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
906 hbhlen, rtalertp, plenp) < 0)
915 * Search header for all Hop-by-hop options and process each option.
916 * This function is separate from ip6_hopopts_input() in order to
917 * handle a case where the sending node itself process its hop-by-hop
918 * options header. In such a case, the function is called from ip6_output().
920 * The function assumes that hbh header is located right after the IPv6 header
921 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
922 * opthead + hbhlen is located in continuous memory region.
925 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
934 u_int8_t *opt = opthead;
935 u_int16_t rtalert_val;
937 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
939 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
945 if (hbhlen < IP6OPT_MINLEN) {
946 ip6stat.ip6s_toosmall++;
949 optlen = *(opt + 1) + 2;
952 /* XXX may need check for alignment */
953 if (hbhlen < IP6OPT_RTALERT_LEN) {
954 ip6stat.ip6s_toosmall++;
957 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
959 icmp6_error(m, ICMP6_PARAM_PROB,
960 ICMP6_PARAMPROB_HEADER,
961 erroff + opt + 1 - opthead);
964 optlen = IP6OPT_RTALERT_LEN;
965 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
966 *rtalertp = ntohs(rtalert_val);
969 /* XXX may need check for alignment */
970 if (hbhlen < IP6OPT_JUMBO_LEN) {
971 ip6stat.ip6s_toosmall++;
974 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
976 icmp6_error(m, ICMP6_PARAM_PROB,
977 ICMP6_PARAMPROB_HEADER,
978 erroff + opt + 1 - opthead);
981 optlen = IP6OPT_JUMBO_LEN;
984 * IPv6 packets that have non 0 payload length
985 * must not contain a jumbo payload option.
987 ip6 = mtod(m, struct ip6_hdr *);
989 ip6stat.ip6s_badoptions++;
990 icmp6_error(m, ICMP6_PARAM_PROB,
991 ICMP6_PARAMPROB_HEADER,
992 erroff + opt - opthead);
997 * We may see jumbolen in unaligned location, so
998 * we'd need to perform bcopy().
1000 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1001 jumboplen = (u_int32_t)htonl(jumboplen);
1005 * if there are multiple jumbo payload options,
1006 * *plenp will be non-zero and the packet will be
1008 * the behavior may need some debate in ipngwg -
1009 * multiple options does not make sense, however,
1010 * there's no explicit mention in specification.
1013 ip6stat.ip6s_badoptions++;
1014 icmp6_error(m, ICMP6_PARAM_PROB,
1015 ICMP6_PARAMPROB_HEADER,
1016 erroff + opt + 2 - opthead);
1022 * jumbo payload length must be larger than 65535.
1024 if (jumboplen <= IPV6_MAXPACKET) {
1025 ip6stat.ip6s_badoptions++;
1026 icmp6_error(m, ICMP6_PARAM_PROB,
1027 ICMP6_PARAMPROB_HEADER,
1028 erroff + opt + 2 - opthead);
1034 default: /* unknown option */
1035 if (hbhlen < IP6OPT_MINLEN) {
1036 ip6stat.ip6s_toosmall++;
1039 optlen = ip6_unknown_opt(opt, m,
1040 erroff + opt - opthead);
1056 * Unknown option processing.
1057 * The third argument `off' is the offset from the IPv6 header to the option,
1058 * which is necessary if the IPv6 header the and option header and IPv6 header
1059 * is not continuous in order to return an ICMPv6 error.
1062 ip6_unknown_opt(optp, m, off)
1067 struct ip6_hdr *ip6;
1069 switch (IP6OPT_TYPE(*optp)) {
1070 case IP6OPT_TYPE_SKIP: /* ignore the option */
1071 return((int)*(optp + 1));
1072 case IP6OPT_TYPE_DISCARD: /* silently discard */
1075 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1076 ip6stat.ip6s_badoptions++;
1077 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1079 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1080 ip6stat.ip6s_badoptions++;
1081 ip6 = mtod(m, struct ip6_hdr *);
1082 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1083 (m->m_flags & (M_BCAST|M_MCAST)))
1086 icmp6_error(m, ICMP6_PARAM_PROB,
1087 ICMP6_PARAMPROB_OPTION, off);
1091 m_freem(m); /* XXX: NOTREACHED */
1096 * Create the "control" list for this pcb.
1097 * The function will not modify mbuf chain at all.
1099 * with KAME mbuf chain restriction:
1100 * The routine will be called from upper layer handlers like tcp6_input().
1101 * Thus the routine assumes that the caller (tcp6_input) have already
1102 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1103 * very first mbuf on the mbuf chain.
1106 ip6_savecontrol(in6p, mp, ip6, m)
1109 struct ip6_hdr *ip6;
1112 #if __FreeBSD_version >= 500000
1113 struct thread *td = curthread; /* XXX */
1115 struct proc *td = curproc; /* XXX */
1118 int rthdr_exist = 0;
1121 if (td && !suser(td))
1125 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
1129 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1130 SCM_TIMESTAMP, SOL_SOCKET);
1132 mp = &(*mp)->m_next;
1137 /* RFC 2292 sec. 5 */
1138 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1139 struct in6_pktinfo pi6;
1140 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1141 if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
1142 pi6.ipi6_addr.s6_addr16[1] = 0;
1143 pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
1144 ? m->m_pkthdr.rcvif->if_index
1146 *mp = sbcreatecontrol((caddr_t) &pi6,
1147 sizeof(struct in6_pktinfo), IPV6_PKTINFO,
1150 mp = &(*mp)->m_next;
1153 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
1154 int hlim = ip6->ip6_hlim & 0xff;
1155 *mp = sbcreatecontrol((caddr_t) &hlim,
1156 sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
1158 mp = &(*mp)->m_next;
1162 * IPV6_HOPOPTS socket option. We require super-user privilege
1163 * for the option, but it might be too strict, since there might
1164 * be some hop-by-hop options which can be returned to normal user.
1165 * See RFC 2292 section 6.
1167 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
1169 * Check if a hop-by-hop options header is contatined in the
1170 * received packet, and if so, store the options as ancillary
1171 * data. Note that a hop-by-hop options header must be
1172 * just after the IPv6 header, which fact is assured through
1173 * the IPv6 input processing.
1175 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1176 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1177 struct ip6_hbh *hbh;
1179 #ifdef PULLDOWN_TEST
1183 #ifndef PULLDOWN_TEST
1184 hbh = (struct ip6_hbh *)(ip6 + 1);
1185 hbhlen = (hbh->ip6h_len + 1) << 3;
1187 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1190 ip6stat.ip6s_tooshort++;
1193 hbh = mtod(ext, struct ip6_hbh *);
1194 hbhlen = (hbh->ip6h_len + 1) << 3;
1195 if (hbhlen != ext->m_len) {
1197 ip6stat.ip6s_tooshort++;
1203 * XXX: We copy whole the header even if a jumbo
1204 * payload option is included, which option is to
1205 * be removed before returning in the RFC 2292.
1206 * Note: this constraint is removed in 2292bis.
1208 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1209 IPV6_HOPOPTS, IPPROTO_IPV6);
1211 mp = &(*mp)->m_next;
1212 #ifdef PULLDOWN_TEST
1218 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1219 if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
1220 int proto, off, nxt;
1223 * go through the header chain to see if a routing header is
1224 * contained in the packet. We need this information to store
1225 * destination options headers (if any) properly.
1226 * XXX: performance issue. We should record this info when
1227 * processing extension headers in incoming routine.
1230 proto = IPPROTO_IPV6;
1236 newoff = ip6_nexthdr(m, off, proto, &nxt);
1239 if (newoff < off) /* invalid, check for safety */
1241 if ((proto = nxt) == IPPROTO_ROUTING) {
1249 if ((in6p->in6p_flags &
1250 (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
1251 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1252 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1255 * Search for destination options headers or routing
1256 * header(s) through the header chain, and stores each
1257 * header as ancillary data.
1258 * Note that the order of the headers remains in
1259 * the chain of ancillary data.
1261 while (1) { /* is explicit loop prevention necessary? */
1262 struct ip6_ext *ip6e = NULL;
1264 #ifdef PULLDOWN_TEST
1265 struct mbuf *ext = NULL;
1269 * if it is not an extension header, don't try to
1270 * pull it from the chain.
1273 case IPPROTO_DSTOPTS:
1274 case IPPROTO_ROUTING:
1275 case IPPROTO_HOPOPTS:
1276 case IPPROTO_AH: /* is it possible? */
1282 #ifndef PULLDOWN_TEST
1283 if (off + sizeof(*ip6e) > m->m_len)
1285 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1286 if (nxt == IPPROTO_AH)
1287 elen = (ip6e->ip6e_len + 2) << 2;
1289 elen = (ip6e->ip6e_len + 1) << 3;
1290 if (off + elen > m->m_len)
1293 ext = ip6_pullexthdr(m, off, nxt);
1295 ip6stat.ip6s_tooshort++;
1298 ip6e = mtod(ext, struct ip6_ext *);
1299 if (nxt == IPPROTO_AH)
1300 elen = (ip6e->ip6e_len + 2) << 2;
1302 elen = (ip6e->ip6e_len + 1) << 3;
1303 if (elen != ext->m_len) {
1305 ip6stat.ip6s_tooshort++;
1311 case IPPROTO_DSTOPTS:
1312 if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0)
1316 * We also require super-user privilege for
1318 * See the comments on IN6_HOPOPTS.
1323 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1327 mp = &(*mp)->m_next;
1329 case IPPROTO_ROUTING:
1330 if (!in6p->in6p_flags & IN6P_RTHDR)
1333 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1337 mp = &(*mp)->m_next;
1339 case IPPROTO_HOPOPTS:
1340 case IPPROTO_AH: /* is it possible? */
1345 * other cases have been filtered in the above.
1346 * none will visit this case. here we supply
1347 * the code just in case (nxt overwritten or
1350 #ifdef PULLDOWN_TEST
1357 /* proceed with the next header. */
1359 nxt = ip6e->ip6e_nxt;
1361 #ifdef PULLDOWN_TEST
1372 #ifdef PULLDOWN_TEST
1374 * pull single extension header from mbuf chain. returns single mbuf that
1375 * contains the result, or NULL on error.
1377 static struct mbuf *
1378 ip6_pullexthdr(m, off, nxt)
1383 struct ip6_ext ip6e;
1389 case IPPROTO_DSTOPTS:
1390 case IPPROTO_ROUTING:
1391 case IPPROTO_HOPOPTS:
1392 case IPPROTO_AH: /* is it possible? */
1395 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1399 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1400 if (nxt == IPPROTO_AH)
1401 elen = (ip6e.ip6e_len + 2) << 2;
1403 elen = (ip6e.ip6e_len + 1) << 3;
1405 MGET(n, M_DONTWAIT, MT_DATA);
1406 if (n && elen >= MLEN) {
1407 MCLGET(n, M_DONTWAIT);
1408 if ((n->m_flags & M_EXT) == 0) {
1417 if (elen >= M_TRAILINGSPACE(n)) {
1422 m_copydata(m, off, elen, mtod(n, caddr_t));
1429 * Get pointer to the previous header followed by the header
1430 * currently processed.
1431 * XXX: This function supposes that
1432 * M includes all headers,
1433 * the next header field and the header length field of each header
1435 * the sum of each header length equals to OFF.
1436 * Because of these assumptions, this function must be called very
1437 * carefully. Moreover, it will not be used in the near future when
1438 * we develop `neater' mechanism to process extension headers.
1441 ip6_get_prevhdr(m, off)
1445 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1447 if (off == sizeof(struct ip6_hdr))
1448 return(&ip6->ip6_nxt);
1451 struct ip6_ext *ip6e = NULL;
1454 len = sizeof(struct ip6_hdr);
1456 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1459 case IPPROTO_FRAGMENT:
1460 len += sizeof(struct ip6_frag);
1463 len += (ip6e->ip6e_len + 2) << 2;
1466 len += (ip6e->ip6e_len + 1) << 3;
1469 nxt = ip6e->ip6e_nxt;
1472 return(&ip6e->ip6e_nxt);
1479 * get next header offset. m will be retained.
1482 ip6_nexthdr(m, off, proto, nxtp)
1489 struct ip6_ext ip6e;
1494 panic("ip6_nexthdr: m == NULL");
1495 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1500 if (m->m_pkthdr.len < off + sizeof(ip6))
1502 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1504 *nxtp = ip6.ip6_nxt;
1508 case IPPROTO_FRAGMENT:
1510 * terminate parsing if it is not the first fragment,
1511 * it does not make sense to parse through it.
1513 if (m->m_pkthdr.len < off + sizeof(fh))
1515 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1516 if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
1519 *nxtp = fh.ip6f_nxt;
1520 off += sizeof(struct ip6_frag);
1524 if (m->m_pkthdr.len < off + sizeof(ip6e))
1526 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1528 *nxtp = ip6e.ip6e_nxt;
1529 off += (ip6e.ip6e_len + 2) << 2;
1532 case IPPROTO_HOPOPTS:
1533 case IPPROTO_ROUTING:
1534 case IPPROTO_DSTOPTS:
1535 if (m->m_pkthdr.len < off + sizeof(ip6e))
1537 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1539 *nxtp = ip6e.ip6e_nxt;
1540 off += (ip6e.ip6e_len + 1) << 3;
1545 case IPPROTO_IPCOMP:
1557 * get offset for the last header in the chain. m will be kept untainted.
1560 ip6_lasthdr(m, off, proto, nxtp)
1574 newoff = ip6_nexthdr(m, off, proto, nxtp);
1577 else if (newoff < off)
1578 return -1; /* invalid */
1579 else if (newoff == off)
1591 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1593 tag = m_tag_get(PACKET_TAG_IPV6_INPUT,
1594 sizeof (struct ip6aux),
1597 m_tag_prepend(m, tag);
1600 bzero(tag+1, sizeof (struct ip6aux));
1601 return tag ? (struct ip6aux*)(tag+1) : NULL;
1608 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1609 return tag ? (struct ip6aux*)(tag+1) : NULL;
1616 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1618 m_tag_delete(m, tag);
1622 * System control for IP6
1625 u_char inet6ctlerrmap[PRC_NCMDS] = {
1627 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1628 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1629 EMSGSIZE, EHOSTUNREACH, 0, 0,