2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1988, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
69 #include "opt_ipsec.h"
70 #include "opt_route.h"
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/hhook.h>
76 #include <sys/malloc.h>
79 #include <sys/domain.h>
80 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/errno.h>
86 #include <sys/kernel.h>
88 #include <sys/rmlock.h>
89 #include <sys/syslog.h>
92 #include <net/if_var.h>
93 #include <net/if_types.h>
94 #include <net/if_dl.h>
95 #include <net/route.h>
96 #include <net/netisr.h>
97 #include <net/rss_config.h>
101 #include <netinet/in.h>
102 #include <netinet/in_kdtrace.h>
103 #include <netinet/ip_var.h>
104 #include <netinet/in_systm.h>
105 #include <net/if_llatbl.h>
107 #include <netinet/ip.h>
108 #include <netinet/ip_icmp.h>
110 #include <netinet/ip6.h>
111 #include <netinet6/in6_var.h>
112 #include <netinet6/ip6_var.h>
113 #include <netinet/in_pcb.h>
114 #include <netinet/icmp6.h>
115 #include <netinet6/scope6_var.h>
116 #include <netinet6/in6_ifattach.h>
117 #include <netinet6/nd6.h>
118 #include <netinet6/in6_rss.h>
121 #include <netipsec/ipsec.h>
122 #include <netinet6/ip6_ipsec.h>
123 #include <netipsec/ipsec6.h>
126 #include <netinet6/ip6protosw.h>
128 extern struct domain inet6domain;
130 u_char ip6_protox[IPPROTO_MAX];
131 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
132 VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
133 VNET_DEFINE(u_long, in6_ifaddrhmask);
135 static struct netisr_handler ip6_nh = {
137 .nh_handler = ip6_input,
138 .nh_proto = NETISR_IPV6,
140 .nh_m2cpuid = rss_soft_m2cpuid_v6,
141 .nh_policy = NETISR_POLICY_CPU,
142 .nh_dispatch = NETISR_DISPATCH_HYBRID,
144 .nh_policy = NETISR_POLICY_FLOW,
149 static struct netisr_handler ip6_direct_nh = {
150 .nh_name = "ip6_direct",
151 .nh_handler = ip6_direct_input,
152 .nh_proto = NETISR_IPV6_DIRECT,
153 .nh_m2cpuid = rss_soft_m2cpuid_v6,
154 .nh_policy = NETISR_POLICY_CPU,
155 .nh_dispatch = NETISR_DISPATCH_HYBRID,
159 VNET_DECLARE(struct callout, in6_tmpaddrtimer_ch);
160 #define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch)
162 VNET_DEFINE(struct pfil_head, inet6_pfil_hook);
164 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
165 VNET_PCPUSTAT_SYSINIT(ip6stat);
167 VNET_PCPUSTAT_SYSUNINIT(ip6stat);
170 struct rmlock in6_ifaddr_lock;
171 RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
173 static void ip6_init2(void *);
174 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
176 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
180 * IP6 initialization: fill in IP6 protocol switch table.
181 * All protocols not implemented in kernel go to raw IP6 protocol handler.
189 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
190 &V_ip6_auto_linklocal);
191 TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
192 TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
194 TAILQ_INIT(&V_in6_ifaddrhead);
195 V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
198 /* Initialize packet filter hooks. */
199 V_inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
200 V_inet6_pfil_hook.ph_af = AF_INET6;
201 if ((i = pfil_head_register(&V_inet6_pfil_hook)) != 0)
202 printf("%s: WARNING: unable to register pfil hook, "
203 "error %d\n", __func__, i);
205 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6,
206 &V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
207 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
208 printf("%s: WARNING: unable to register input helper hook\n",
210 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6,
211 &V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
212 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
213 printf("%s: WARNING: unable to register output helper hook\n",
217 addrsel_policy_init();
221 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
223 /* Skip global initialization stuff for non-default instances. */
224 if (!IS_DEFAULT_VNET(curvnet))
227 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
231 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
232 for (i = 0; i < IPPROTO_MAX; i++)
233 ip6_protox[i] = pr - inet6sw;
235 * Cycle through IP protocols and put them into the appropriate place
238 for (pr = inet6domain.dom_protosw;
239 pr < inet6domain.dom_protoswNPROTOSW; pr++)
240 if (pr->pr_domain->dom_family == PF_INET6 &&
241 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
242 /* Be careful to only index valid IP protocols. */
243 if (pr->pr_protocol < IPPROTO_MAX)
244 ip6_protox[pr->pr_protocol] = pr - inet6sw;
247 netisr_register(&ip6_nh);
249 netisr_register(&ip6_direct_nh);
254 * The protocol to be inserted into ip6_protox[] must be already registered
255 * in inet6sw[], either statically or through pf_proto_register().
258 ip6proto_register(short ip6proto)
263 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
264 return (EPROTONOSUPPORT);
267 * The protocol slot must not be occupied by another protocol
268 * already. An index pointing to IPPROTO_RAW is unused.
270 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
272 return (EPFNOSUPPORT);
273 if (ip6_protox[ip6proto] != pr - inet6sw) /* IPPROTO_RAW */
277 * Find the protocol position in inet6sw[] and set the index.
279 for (pr = inet6domain.dom_protosw;
280 pr < inet6domain.dom_protoswNPROTOSW; pr++) {
281 if (pr->pr_domain->dom_family == PF_INET6 &&
282 pr->pr_protocol && pr->pr_protocol == ip6proto) {
283 ip6_protox[pr->pr_protocol] = pr - inet6sw;
287 return (EPROTONOSUPPORT);
291 ip6proto_unregister(short ip6proto)
296 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
297 return (EPROTONOSUPPORT);
299 /* Check if the protocol was indeed registered. */
300 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
302 return (EPFNOSUPPORT);
303 if (ip6_protox[ip6proto] == pr - inet6sw) /* IPPROTO_RAW */
306 /* Reset the protocol slot to IPPROTO_RAW. */
307 ip6_protox[ip6proto] = pr - inet6sw;
317 if ((error = pfil_head_unregister(&V_inet6_pfil_hook)) != 0)
318 printf("%s: WARNING: unable to unregister pfil hook, "
319 "error %d\n", __func__, error);
320 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]);
322 printf("%s: WARNING: unable to deregister input helper hook "
323 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: "
324 "error %d returned\n", __func__, error);
326 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]);
328 printf("%s: WARNING: unable to deregister output helper hook "
329 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: "
330 "error %d returned\n", __func__, error);
332 hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
334 callout_drain(&V_in6_tmpaddrtimer_ch);
339 ip6_init2_vnet(const void *unused __unused)
343 callout_init(&V_nd6_timer_ch, 0);
344 callout_reset(&V_nd6_timer_ch, hz, nd6_timer, curvnet);
346 /* timer for regeneranation of temporary addresses randomize ID */
347 callout_init(&V_in6_tmpaddrtimer_ch, 0);
348 callout_reset(&V_in6_tmpaddrtimer_ch,
349 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
350 V_ip6_temp_regen_advance) * hz,
351 in6_tmpaddrtimer, curvnet);
357 ip6_init2(void *dummy)
360 ip6_init2_vnet(NULL);
364 /* This must be after route_init(), which is now SI_ORDER_THIRD */
365 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
368 ip6_input_hbh(struct mbuf *m, uint32_t *plen, uint32_t *rtalert, int *off,
374 if (ip6_hopopts_input(plen, rtalert, &m, off)) {
375 #if 0 /*touches NULL pointer*/
376 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
378 goto out; /* m have already been freed */
382 ip6 = mtod(m, struct ip6_hdr *);
385 * if the payload length field is 0 and the next header field
386 * indicates Hop-by-Hop Options header, then a Jumbo Payload
387 * option MUST be included.
389 if (ip6->ip6_plen == 0 && *plen == 0) {
391 * Note that if a valid jumbo payload option is
392 * contained, ip6_hopopts_input() must set a valid
393 * (non-zero) payload length to the variable plen.
395 IP6STAT_INC(ip6s_badoptions);
396 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
397 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
398 icmp6_error(m, ICMP6_PARAM_PROB,
399 ICMP6_PARAMPROB_HEADER,
400 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
403 #ifndef PULLDOWN_TEST
404 /* ip6_hopopts_input() ensures that mbuf is contiguous */
405 hbh = (struct ip6_hbh *)(ip6 + 1);
407 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
408 sizeof(struct ip6_hbh));
410 IP6STAT_INC(ip6s_tooshort);
414 *nxt = hbh->ip6h_nxt;
417 * If we are acting as a router and the packet contains a
418 * router alert option, see if we know the option value.
419 * Currently, we only support the option value for MLD, in which
420 * case we should pass the packet to the multicast routing
423 if (*rtalert != ~0) {
425 case IP6OPT_RTALERT_MLD:
426 if (V_ip6_forwarding)
431 * RFC2711 requires unrecognized values must be
446 * IPv6 direct input routine.
448 * This is called when reinjecting completed fragments where
449 * all of the previous checking and book-keeping has been done.
452 ip6_direct_input(struct mbuf *m)
457 struct ip6_direct_ctx *ip6dc;
459 mtag = m_tag_locate(m, MTAG_ABI_IPV6, IPV6_TAG_DIRECT, NULL);
460 KASSERT(mtag != NULL, ("Reinjected packet w/o direct ctx tag!"));
462 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
463 nxt = ip6dc->ip6dc_nxt;
464 off = ip6dc->ip6dc_off;
468 m_tag_delete(m, mtag);
470 while (nxt != IPPROTO_DONE) {
471 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
472 IP6STAT_INC(ip6s_toomanyhdr);
477 * protection against faulty packet - there should be
478 * more sanity checks in header chain processing.
480 if (m->m_pkthdr.len < off) {
481 IP6STAT_INC(ip6s_tooshort);
482 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
488 * enforce IPsec policy checking if we are seeing last header.
489 * note that we do not visit this with protocols with pcb layer
490 * code - like udp/tcp/raw ip.
492 if (ip6_ipsec_input(m, nxt))
496 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
505 ip6_input(struct mbuf *m)
507 struct in6_addr odst;
509 struct in6_ifaddr *ia;
511 u_int32_t rtalert = ~0;
512 int off = sizeof(struct ip6_hdr), nest;
518 * should the inner packet be considered authentic?
519 * see comment in ah4_input().
520 * NB: m cannot be NULL when passed to the input routine
523 m->m_flags &= ~M_AUTHIPHDR;
524 m->m_flags &= ~M_AUTHIPDGM;
528 if (m->m_flags & M_FASTFWD_OURS) {
530 * Firewall changed destination to local.
532 m->m_flags &= ~M_FASTFWD_OURS;
534 ip6 = mtod(m, struct ip6_hdr *);
541 if (m->m_flags & M_EXT) {
543 IP6STAT_INC(ip6s_mext2m);
545 IP6STAT_INC(ip6s_mext1);
548 if (m->m_flags & M_LOOP) {
549 IP6STAT_INC(ip6s_m2m[V_loif->if_index]);
550 } else if (m->m_pkthdr.rcvif->if_index < IP6S_M2MMAX)
552 ip6s_m2m[m->m_pkthdr.rcvif->if_index]);
554 IP6STAT_INC(ip6s_m2m[0]);
556 IP6STAT_INC(ip6s_m1);
559 /* drop the packet if IPv6 operation is disabled on the IF */
560 if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED))
563 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
564 IP6STAT_INC(ip6s_total);
566 #ifndef PULLDOWN_TEST
568 * L2 bridge code and some other code can return mbuf chain
569 * that does not conform to KAME requirement. too bad.
570 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
572 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
575 if (m->m_pkthdr.len > MHLEN)
576 n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
578 n = m_gethdr(M_NOWAIT, MT_DATA);
581 return; /* ENOBUFS */
585 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
586 n->m_len = n->m_pkthdr.len;
590 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
593 if (m->m_len < sizeof(struct ip6_hdr)) {
595 inifp = m->m_pkthdr.rcvif;
596 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
597 IP6STAT_INC(ip6s_toosmall);
598 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
603 ip6 = mtod(m, struct ip6_hdr *);
605 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
606 IP6STAT_INC(ip6s_badvers);
607 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
611 IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
613 IP_PROBE(receive, NULL, NULL, ip6, m->m_pkthdr.rcvif, NULL, ip6);
616 * Check against address spoofing/corruption.
618 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
619 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
621 * XXX: "badscope" is not very suitable for a multicast source.
623 IP6STAT_INC(ip6s_badscope);
624 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
627 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
628 !(m->m_flags & M_LOOP)) {
630 * In this case, the packet should come from the loopback
631 * interface. However, we cannot just check the if_flags,
632 * because ip6_mloopback() passes the "actual" interface
633 * as the outgoing/incoming interface.
635 IP6STAT_INC(ip6s_badscope);
636 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
639 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
640 IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
643 * Nodes must not originate a packet to a multicast address
644 * whose scop field contains the reserved value 0; if such
645 * a packet is received, it must be silently dropped.
647 IP6STAT_INC(ip6s_badscope);
648 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
652 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
653 /* packet is dropped by traffic conditioner */
658 * The following check is not documented in specs. A malicious
659 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
660 * and bypass security checks (act as if it was from 127.0.0.1 by using
661 * IPv6 src ::ffff:127.0.0.1). Be cautious.
663 * This check chokes if we are in an SIIT cloud. As none of BSDs
664 * support IPv4-less kernel compilation, we cannot support SIIT
665 * environment at all. So, it makes more sense for us to reject any
666 * malicious packets for non-SIIT environment, than try to do a
667 * partial support for SIIT environment.
669 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
670 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
671 IP6STAT_INC(ip6s_badscope);
672 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
677 * Reject packets with IPv4 compatible addresses (auto tunnel).
679 * The code forbids auto tunnel relay case in RFC1933 (the check is
680 * stronger than RFC1933). We may want to re-enable it if mech-xx
681 * is revised to forbid relaying case.
683 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
684 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
685 IP6STAT_INC(ip6s_badscope);
686 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
692 * Bypass packet filtering for packets previously handled by IPsec.
694 if (ip6_ipsec_filtertunnel(m))
699 * Run through list of hooks for input packets.
701 * NB: Beware of the destination address changing
702 * (e.g. by NAT rewriting). When this happens,
703 * tell ip6_forward to do the right thing.
707 /* Jump over all PFIL processing if hooks are not active. */
708 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
711 if (pfil_run_hooks(&V_inet6_pfil_hook, &m,
712 m->m_pkthdr.rcvif, PFIL_IN, NULL))
714 if (m == NULL) /* consumed by filter */
716 ip6 = mtod(m, struct ip6_hdr *);
717 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
719 if (m->m_flags & M_FASTFWD_OURS) {
720 m->m_flags &= ~M_FASTFWD_OURS;
724 if ((m->m_flags & M_IP6_NEXTHOP) &&
725 m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
727 * Directly ship the packet on. This allows forwarding
728 * packets originally destined to us to some other directly
737 * Disambiguate address scope zones (if there is ambiguity).
738 * We first make sure that the original source or destination address
739 * is not in our internal form for scoped addresses. Such addresses
740 * are not necessarily invalid spec-wise, but we cannot accept them due
741 * to the usage conflict.
742 * in6_setscope() then also checks and rejects the cases where src or
743 * dst are the loopback address and the receiving interface
746 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
747 IP6STAT_INC(ip6s_badscope); /* XXX */
750 if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
751 in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
752 IP6STAT_INC(ip6s_badscope);
756 * Multicast check. Assume packet is for us to avoid
757 * prematurely taking locks.
759 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
761 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
766 * XXX: For now we keep link-local IPv6 addresses with embedded
767 * scope zone id, therefore we use zero zoneid here.
769 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
771 if (ia->ia6_flags & IN6_IFF_NOTREADY) {
772 char ip6bufs[INET6_ADDRSTRLEN];
773 char ip6bufd[INET6_ADDRSTRLEN];
774 /* address is not ready, so discard the packet. */
776 "ip6_input: packet to an unready address %s->%s\n",
777 ip6_sprintf(ip6bufs, &ip6->ip6_src),
778 ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
779 ifa_free(&ia->ia_ifa);
782 /* Count the packet in the ip address stats */
783 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
784 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
785 ifa_free(&ia->ia_ifa);
791 * Now there is no reason to process the packet if it's not our own
792 * and we're not a router.
794 if (!V_ip6_forwarding) {
795 IP6STAT_INC(ip6s_cantforward);
796 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
802 * Process Hop-by-Hop options header if it's contained.
803 * m may be modified in ip6_hopopts_input().
804 * If a JumboPayload option is included, plen will also be modified.
806 plen = (u_int32_t)ntohs(ip6->ip6_plen);
807 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
808 if (ip6_input_hbh(m, &plen, &rtalert, &off, &nxt, &ours) != 0)
814 * Use mbuf flags to propagate Router Alert option to
815 * ICMPv6 layer, as hop-by-hop options have been stripped.
818 m->m_flags |= M_RTALERT_MLD;
821 * Check that the amount of data in the buffers
822 * is as at least much as the IPv6 header would have us expect.
823 * Trim mbufs if longer than we expect.
824 * Drop packet if shorter than we expect.
826 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
827 IP6STAT_INC(ip6s_tooshort);
828 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
831 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
832 if (m->m_len == m->m_pkthdr.len) {
833 m->m_len = sizeof(struct ip6_hdr) + plen;
834 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
836 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
840 * Forward if desirable.
843 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
845 * If we are acting as a multicast router, all
846 * incoming multicast packets are passed to the
847 * kernel-level multicast forwarding function.
848 * The packet is returned (relatively) intact; if
849 * ip6_mforward() returns a non-zero value, the packet
850 * must be discarded, else it may be accepted below.
852 * XXX TODO: Check hlim and multicast scope here to avoid
853 * unnecessarily calling into ip6_mforward().
856 ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
857 IP6STAT_INC(ip6s_cantforward);
858 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
862 ip6_forward(m, srcrt);
866 ip6 = mtod(m, struct ip6_hdr *);
869 * Malicious party may be able to use IPv4 mapped addr to confuse
870 * tcp/udp stack and bypass security checks (act as if it was from
871 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
873 * For SIIT end node behavior, you may want to disable the check.
874 * However, you will become vulnerable to attacks using IPv4 mapped
877 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
878 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
879 IP6STAT_INC(ip6s_badscope);
880 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
885 * Tell launch routine the next header
887 IP6STAT_INC(ip6s_delivered);
888 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_deliver);
891 while (nxt != IPPROTO_DONE) {
892 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
893 IP6STAT_INC(ip6s_toomanyhdr);
898 * protection against faulty packet - there should be
899 * more sanity checks in header chain processing.
901 if (m->m_pkthdr.len < off) {
902 IP6STAT_INC(ip6s_tooshort);
903 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
909 * enforce IPsec policy checking if we are seeing last header.
910 * note that we do not visit this with protocols with pcb layer
911 * code - like udp/tcp/raw ip.
913 if (ip6_ipsec_input(m, nxt))
917 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
925 * Hop-by-Hop options header processing. If a valid jumbo payload option is
926 * included, the real payload length will be stored in plenp.
928 * rtalertp - XXX: should be stored more smart way
931 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
932 struct mbuf **mp, int *offp)
934 struct mbuf *m = *mp;
935 int off = *offp, hbhlen;
938 /* validation of the length of the header */
939 #ifndef PULLDOWN_TEST
940 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
941 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
942 hbhlen = (hbh->ip6h_len + 1) << 3;
944 IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
945 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
947 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
948 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
950 IP6STAT_INC(ip6s_tooshort);
953 hbhlen = (hbh->ip6h_len + 1) << 3;
954 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
957 IP6STAT_INC(ip6s_tooshort);
962 hbhlen -= sizeof(struct ip6_hbh);
963 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
964 hbhlen, rtalertp, plenp) < 0)
973 * Search header for all Hop-by-hop options and process each option.
974 * This function is separate from ip6_hopopts_input() in order to
975 * handle a case where the sending node itself process its hop-by-hop
976 * options header. In such a case, the function is called from ip6_output().
978 * The function assumes that hbh header is located right after the IPv6 header
979 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
980 * opthead + hbhlen is located in contiguous memory region.
983 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
984 u_int32_t *rtalertp, u_int32_t *plenp)
988 u_int8_t *opt = opthead;
989 u_int16_t rtalert_val;
991 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
993 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
999 if (hbhlen < IP6OPT_MINLEN) {
1000 IP6STAT_INC(ip6s_toosmall);
1003 optlen = *(opt + 1) + 2;
1005 case IP6OPT_ROUTER_ALERT:
1006 /* XXX may need check for alignment */
1007 if (hbhlen < IP6OPT_RTALERT_LEN) {
1008 IP6STAT_INC(ip6s_toosmall);
1011 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1013 icmp6_error(m, ICMP6_PARAM_PROB,
1014 ICMP6_PARAMPROB_HEADER,
1015 erroff + opt + 1 - opthead);
1018 optlen = IP6OPT_RTALERT_LEN;
1019 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1020 *rtalertp = ntohs(rtalert_val);
1023 /* XXX may need check for alignment */
1024 if (hbhlen < IP6OPT_JUMBO_LEN) {
1025 IP6STAT_INC(ip6s_toosmall);
1028 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1030 icmp6_error(m, ICMP6_PARAM_PROB,
1031 ICMP6_PARAMPROB_HEADER,
1032 erroff + opt + 1 - opthead);
1035 optlen = IP6OPT_JUMBO_LEN;
1038 * IPv6 packets that have non 0 payload length
1039 * must not contain a jumbo payload option.
1041 ip6 = mtod(m, struct ip6_hdr *);
1042 if (ip6->ip6_plen) {
1043 IP6STAT_INC(ip6s_badoptions);
1044 icmp6_error(m, ICMP6_PARAM_PROB,
1045 ICMP6_PARAMPROB_HEADER,
1046 erroff + opt - opthead);
1051 * We may see jumbolen in unaligned location, so
1052 * we'd need to perform bcopy().
1054 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1055 jumboplen = (u_int32_t)htonl(jumboplen);
1059 * if there are multiple jumbo payload options,
1060 * *plenp will be non-zero and the packet will be
1062 * the behavior may need some debate in ipngwg -
1063 * multiple options does not make sense, however,
1064 * there's no explicit mention in specification.
1067 IP6STAT_INC(ip6s_badoptions);
1068 icmp6_error(m, ICMP6_PARAM_PROB,
1069 ICMP6_PARAMPROB_HEADER,
1070 erroff + opt + 2 - opthead);
1076 * jumbo payload length must be larger than 65535.
1078 if (jumboplen <= IPV6_MAXPACKET) {
1079 IP6STAT_INC(ip6s_badoptions);
1080 icmp6_error(m, ICMP6_PARAM_PROB,
1081 ICMP6_PARAMPROB_HEADER,
1082 erroff + opt + 2 - opthead);
1088 default: /* unknown option */
1089 if (hbhlen < IP6OPT_MINLEN) {
1090 IP6STAT_INC(ip6s_toosmall);
1093 optlen = ip6_unknown_opt(opt, m,
1094 erroff + opt - opthead);
1110 * Unknown option processing.
1111 * The third argument `off' is the offset from the IPv6 header to the option,
1112 * which is necessary if the IPv6 header the and option header and IPv6 header
1113 * is not contiguous in order to return an ICMPv6 error.
1116 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1118 struct ip6_hdr *ip6;
1120 switch (IP6OPT_TYPE(*optp)) {
1121 case IP6OPT_TYPE_SKIP: /* ignore the option */
1122 return ((int)*(optp + 1));
1123 case IP6OPT_TYPE_DISCARD: /* silently discard */
1126 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1127 IP6STAT_INC(ip6s_badoptions);
1128 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1130 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1131 IP6STAT_INC(ip6s_badoptions);
1132 ip6 = mtod(m, struct ip6_hdr *);
1133 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1134 (m->m_flags & (M_BCAST|M_MCAST)))
1137 icmp6_error(m, ICMP6_PARAM_PROB,
1138 ICMP6_PARAMPROB_OPTION, off);
1142 m_freem(m); /* XXX: NOTREACHED */
1147 * Create the "control" list for this pcb.
1148 * These functions will not modify mbuf chain at all.
1150 * With KAME mbuf chain restriction:
1151 * The routine will be called from upper layer handlers like tcp6_input().
1152 * Thus the routine assumes that the caller (tcp6_input) have already
1153 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1154 * very first mbuf on the mbuf chain.
1156 * ip6_savecontrol_v4 will handle those options that are possible to be
1157 * set on a v4-mapped socket.
1158 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1159 * options and handle the v6-only ones itself.
1162 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1165 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1168 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1172 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1173 SCM_TIMESTAMP, SOL_SOCKET);
1175 mp = &(*mp)->m_next;
1179 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1180 /* RFC 2292 sec. 5 */
1181 if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1182 struct in6_pktinfo pi6;
1184 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1188 ip = mtod(m, struct ip *);
1189 pi6.ipi6_addr.s6_addr32[0] = 0;
1190 pi6.ipi6_addr.s6_addr32[1] = 0;
1191 pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1192 pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1194 /* We won't hit this code */
1195 bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1198 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1199 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1202 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1204 *mp = sbcreatecontrol((caddr_t) &pi6,
1205 sizeof(struct in6_pktinfo),
1206 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1208 mp = &(*mp)->m_next;
1211 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1214 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1218 ip = mtod(m, struct ip *);
1221 /* We won't hit this code */
1225 hlim = ip6->ip6_hlim & 0xff;
1227 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1228 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1231 mp = &(*mp)->m_next;
1234 if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1237 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1241 ip = mtod(m, struct ip *);
1242 tclass = ip->ip_tos;
1244 /* We won't hit this code */
1250 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1252 tclass = flowinfo & 0xff;
1254 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int),
1255 IPV6_TCLASS, IPPROTO_IPV6);
1257 mp = &(*mp)->m_next;
1260 if (v4only != NULL) {
1261 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1272 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1274 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1277 mp = ip6_savecontrol_v4(in6p, m, mp, &v4only);
1282 * IPV6_HOPOPTS socket option. Recall that we required super-user
1283 * privilege for the option (see ip6_ctloutput), but it might be too
1284 * strict, since there might be some hop-by-hop options which can be
1285 * returned to normal user.
1286 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1288 if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
1290 * Check if a hop-by-hop options header is contatined in the
1291 * received packet, and if so, store the options as ancillary
1292 * data. Note that a hop-by-hop options header must be
1293 * just after the IPv6 header, which is assured through the
1294 * IPv6 input processing.
1296 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1297 struct ip6_hbh *hbh;
1299 #ifdef PULLDOWN_TEST
1303 #ifndef PULLDOWN_TEST
1304 hbh = (struct ip6_hbh *)(ip6 + 1);
1305 hbhlen = (hbh->ip6h_len + 1) << 3;
1307 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1310 IP6STAT_INC(ip6s_tooshort);
1313 hbh = mtod(ext, struct ip6_hbh *);
1314 hbhlen = (hbh->ip6h_len + 1) << 3;
1315 if (hbhlen != ext->m_len) {
1317 IP6STAT_INC(ip6s_tooshort);
1323 * XXX: We copy the whole header even if a
1324 * jumbo payload option is included, the option which
1325 * is to be removed before returning according to
1327 * Note: this constraint is removed in RFC3542
1329 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1330 IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1333 mp = &(*mp)->m_next;
1334 #ifdef PULLDOWN_TEST
1340 if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1341 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1344 * Search for destination options headers or routing
1345 * header(s) through the header chain, and stores each
1346 * header as ancillary data.
1347 * Note that the order of the headers remains in
1348 * the chain of ancillary data.
1350 while (1) { /* is explicit loop prevention necessary? */
1351 struct ip6_ext *ip6e = NULL;
1353 #ifdef PULLDOWN_TEST
1354 struct mbuf *ext = NULL;
1358 * if it is not an extension header, don't try to
1359 * pull it from the chain.
1362 case IPPROTO_DSTOPTS:
1363 case IPPROTO_ROUTING:
1364 case IPPROTO_HOPOPTS:
1365 case IPPROTO_AH: /* is it possible? */
1371 #ifndef PULLDOWN_TEST
1372 if (off + sizeof(*ip6e) > m->m_len)
1374 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1375 if (nxt == IPPROTO_AH)
1376 elen = (ip6e->ip6e_len + 2) << 2;
1378 elen = (ip6e->ip6e_len + 1) << 3;
1379 if (off + elen > m->m_len)
1382 ext = ip6_pullexthdr(m, off, nxt);
1384 IP6STAT_INC(ip6s_tooshort);
1387 ip6e = mtod(ext, struct ip6_ext *);
1388 if (nxt == IPPROTO_AH)
1389 elen = (ip6e->ip6e_len + 2) << 2;
1391 elen = (ip6e->ip6e_len + 1) << 3;
1392 if (elen != ext->m_len) {
1394 IP6STAT_INC(ip6s_tooshort);
1400 case IPPROTO_DSTOPTS:
1401 if (!(in6p->inp_flags & IN6P_DSTOPTS))
1404 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1406 IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1409 mp = &(*mp)->m_next;
1411 case IPPROTO_ROUTING:
1412 if (!(in6p->inp_flags & IN6P_RTHDR))
1415 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1416 IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
1419 mp = &(*mp)->m_next;
1421 case IPPROTO_HOPOPTS:
1422 case IPPROTO_AH: /* is it possible? */
1427 * other cases have been filtered in the above.
1428 * none will visit this case. here we supply
1429 * the code just in case (nxt overwritten or
1432 #ifdef PULLDOWN_TEST
1439 /* proceed with the next header. */
1441 nxt = ip6e->ip6e_nxt;
1443 #ifdef PULLDOWN_TEST
1452 if (in6p->inp_flags2 & INP_RECVFLOWID) {
1453 uint32_t flowid, flow_type;
1455 flowid = m->m_pkthdr.flowid;
1456 flow_type = M_HASHTYPE_GET(m);
1459 * XXX should handle the failure of one or the
1460 * other - don't populate both?
1462 *mp = sbcreatecontrol((caddr_t) &flowid,
1463 sizeof(uint32_t), IPV6_FLOWID, IPPROTO_IPV6);
1465 mp = &(*mp)->m_next;
1466 *mp = sbcreatecontrol((caddr_t) &flow_type,
1467 sizeof(uint32_t), IPV6_FLOWTYPE, IPPROTO_IPV6);
1469 mp = &(*mp)->m_next;
1473 if (in6p->inp_flags2 & INP_RECVRSSBUCKETID) {
1474 uint32_t flowid, flow_type;
1475 uint32_t rss_bucketid;
1477 flowid = m->m_pkthdr.flowid;
1478 flow_type = M_HASHTYPE_GET(m);
1480 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1481 *mp = sbcreatecontrol((caddr_t) &rss_bucketid,
1482 sizeof(uint32_t), IPV6_RSSBUCKETID, IPPROTO_IPV6);
1484 mp = &(*mp)->m_next;
1493 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu)
1497 struct ip6_mtuinfo mtuctl;
1499 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1501 * Notify the error by sending IPV6_PATHMTU ancillary data if
1502 * application wanted to know the MTU value.
1503 * NOTE: we notify disconnected sockets, because some udp
1504 * applications keep sending sockets disconnected.
1505 * NOTE: our implementation doesn't notify connected sockets that has
1506 * foreign address that is different than given destination addresses
1507 * (this is permitted by RFC 3542).
1509 if ((inp->inp_flags & IN6P_MTU) == 0 || (
1510 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1511 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr)))
1514 mtuctl.ip6m_mtu = mtu;
1515 mtuctl.ip6m_addr = *dst;
1516 if (sa6_recoverscope(&mtuctl.ip6m_addr))
1519 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1520 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1523 so = inp->inp_socket;
1524 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1527 /* XXX: should count statistics */
1532 #ifdef PULLDOWN_TEST
1534 * pull single extension header from mbuf chain. returns single mbuf that
1535 * contains the result, or NULL on error.
1537 static struct mbuf *
1538 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1540 struct ip6_ext ip6e;
1546 case IPPROTO_DSTOPTS:
1547 case IPPROTO_ROUTING:
1548 case IPPROTO_HOPOPTS:
1549 case IPPROTO_AH: /* is it possible? */
1552 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1556 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1557 if (nxt == IPPROTO_AH)
1558 elen = (ip6e.ip6e_len + 2) << 2;
1560 elen = (ip6e.ip6e_len + 1) << 3;
1563 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1565 n = m_get(M_NOWAIT, MT_DATA);
1569 m_copydata(m, off, elen, mtod(n, caddr_t));
1576 * Get pointer to the previous header followed by the header
1577 * currently processed.
1578 * XXX: This function supposes that
1579 * M includes all headers,
1580 * the next header field and the header length field of each header
1582 * the sum of each header length equals to OFF.
1583 * Because of these assumptions, this function must be called very
1584 * carefully. Moreover, it will not be used in the near future when
1585 * we develop `neater' mechanism to process extension headers.
1588 ip6_get_prevhdr(const struct mbuf *m, int off)
1590 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1592 if (off == sizeof(struct ip6_hdr))
1593 return (&ip6->ip6_nxt);
1596 struct ip6_ext *ip6e = NULL;
1599 len = sizeof(struct ip6_hdr);
1601 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1604 case IPPROTO_FRAGMENT:
1605 len += sizeof(struct ip6_frag);
1608 len += (ip6e->ip6e_len + 2) << 2;
1611 len += (ip6e->ip6e_len + 1) << 3;
1614 nxt = ip6e->ip6e_nxt;
1617 return (&ip6e->ip6e_nxt);
1624 * get next header offset. m will be retained.
1627 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1630 struct ip6_ext ip6e;
1635 panic("ip6_nexthdr: m == NULL");
1636 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1641 if (m->m_pkthdr.len < off + sizeof(ip6))
1643 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1645 *nxtp = ip6.ip6_nxt;
1649 case IPPROTO_FRAGMENT:
1651 * terminate parsing if it is not the first fragment,
1652 * it does not make sense to parse through it.
1654 if (m->m_pkthdr.len < off + sizeof(fh))
1656 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1657 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1658 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1661 *nxtp = fh.ip6f_nxt;
1662 off += sizeof(struct ip6_frag);
1666 if (m->m_pkthdr.len < off + sizeof(ip6e))
1668 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1670 *nxtp = ip6e.ip6e_nxt;
1671 off += (ip6e.ip6e_len + 2) << 2;
1674 case IPPROTO_HOPOPTS:
1675 case IPPROTO_ROUTING:
1676 case IPPROTO_DSTOPTS:
1677 if (m->m_pkthdr.len < off + sizeof(ip6e))
1679 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1681 *nxtp = ip6e.ip6e_nxt;
1682 off += (ip6e.ip6e_len + 1) << 3;
1687 case IPPROTO_IPCOMP:
1699 * get offset for the last header in the chain. m will be kept untainted.
1702 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1712 newoff = ip6_nexthdr(m, off, proto, nxtp);
1715 else if (newoff < off)
1716 return -1; /* invalid */
1717 else if (newoff == off)
1726 * System control for IP6
1729 u_char inet6ctlerrmap[PRC_NCMDS] = {
1731 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1732 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1733 EMSGSIZE, EHOSTUNREACH, 0, 0,