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"
68 #include "opt_ipsec.h"
69 #include "opt_route.h"
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/hhook.h>
75 #include <sys/malloc.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/errno.h>
85 #include <sys/kernel.h>
87 #include <sys/rmlock.h>
88 #include <sys/syslog.h>
89 #include <sys/sysctl.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/mld6_var.h>
118 #include <netinet6/nd6.h>
119 #include <netinet6/in6_rss.h>
122 #include <netipsec/ipsec.h>
123 #include <netinet6/ip6_ipsec.h>
124 #include <netipsec/ipsec6.h>
127 #include <netinet6/ip6protosw.h>
129 extern struct domain inet6domain;
131 u_char ip6_protox[IPPROTO_MAX];
132 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
133 VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
134 VNET_DEFINE(u_long, in6_ifaddrhmask);
136 static struct netisr_handler ip6_nh = {
138 .nh_handler = ip6_input,
139 .nh_proto = NETISR_IPV6,
141 .nh_m2cpuid = rss_soft_m2cpuid_v6,
142 .nh_policy = NETISR_POLICY_CPU,
143 .nh_dispatch = NETISR_DISPATCH_HYBRID,
145 .nh_policy = NETISR_POLICY_FLOW,
150 sysctl_netinet6_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
154 netisr_getqlimit(&ip6_nh, &qlimit);
155 error = sysctl_handle_int(oidp, &qlimit, 0, req);
156 if (error || !req->newptr)
160 return (netisr_setqlimit(&ip6_nh, qlimit));
162 SYSCTL_DECL(_net_inet6_ip6);
163 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRQMAXLEN, intr_queue_maxlen,
164 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet6_intr_queue_maxlen, "I",
165 "Maximum size of the IPv6 input queue");
168 static struct netisr_handler ip6_direct_nh = {
169 .nh_name = "ip6_direct",
170 .nh_handler = ip6_direct_input,
171 .nh_proto = NETISR_IPV6_DIRECT,
172 .nh_m2cpuid = rss_soft_m2cpuid_v6,
173 .nh_policy = NETISR_POLICY_CPU,
174 .nh_dispatch = NETISR_DISPATCH_HYBRID,
178 sysctl_netinet6_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
182 netisr_getqlimit(&ip6_direct_nh, &qlimit);
183 error = sysctl_handle_int(oidp, &qlimit, 0, req);
184 if (error || !req->newptr)
188 return (netisr_setqlimit(&ip6_direct_nh, qlimit));
190 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
191 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet6_intr_direct_queue_maxlen,
192 "I", "Maximum size of the IPv6 direct input queue");
196 VNET_DEFINE(struct pfil_head, inet6_pfil_hook);
198 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
199 VNET_PCPUSTAT_SYSINIT(ip6stat);
201 VNET_PCPUSTAT_SYSUNINIT(ip6stat);
204 struct rmlock in6_ifaddr_lock;
205 RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
207 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
209 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
213 * IP6 initialization: fill in IP6 protocol switch table.
214 * All protocols not implemented in kernel go to raw IP6 protocol handler.
222 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
223 &V_ip6_auto_linklocal);
224 TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
225 TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
227 TAILQ_INIT(&V_in6_ifaddrhead);
228 V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
231 /* Initialize packet filter hooks. */
232 V_inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
233 V_inet6_pfil_hook.ph_af = AF_INET6;
234 if ((i = pfil_head_register(&V_inet6_pfil_hook)) != 0)
235 printf("%s: WARNING: unable to register pfil hook, "
236 "error %d\n", __func__, i);
238 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6,
239 &V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
240 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
241 printf("%s: WARNING: unable to register input helper hook\n",
243 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6,
244 &V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
245 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
246 printf("%s: WARNING: unable to register output helper hook\n",
250 addrsel_policy_init();
254 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
256 /* Skip global initialization stuff for non-default instances. */
258 if (!IS_DEFAULT_VNET(curvnet)) {
259 netisr_register_vnet(&ip6_nh);
261 netisr_register_vnet(&ip6_direct_nh);
267 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
271 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
272 for (i = 0; i < IPPROTO_MAX; i++)
273 ip6_protox[i] = pr - inet6sw;
275 * Cycle through IP protocols and put them into the appropriate place
278 for (pr = inet6domain.dom_protosw;
279 pr < inet6domain.dom_protoswNPROTOSW; pr++)
280 if (pr->pr_domain->dom_family == PF_INET6 &&
281 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
282 /* Be careful to only index valid IP protocols. */
283 if (pr->pr_protocol < IPPROTO_MAX)
284 ip6_protox[pr->pr_protocol] = pr - inet6sw;
287 netisr_register(&ip6_nh);
289 netisr_register(&ip6_direct_nh);
294 * The protocol to be inserted into ip6_protox[] must be already registered
295 * in inet6sw[], either statically or through pf_proto_register().
298 ip6proto_register(short ip6proto)
303 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
304 return (EPROTONOSUPPORT);
307 * The protocol slot must not be occupied by another protocol
308 * already. An index pointing to IPPROTO_RAW is unused.
310 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
312 return (EPFNOSUPPORT);
313 if (ip6_protox[ip6proto] != pr - inet6sw) /* IPPROTO_RAW */
317 * Find the protocol position in inet6sw[] and set the index.
319 for (pr = inet6domain.dom_protosw;
320 pr < inet6domain.dom_protoswNPROTOSW; pr++) {
321 if (pr->pr_domain->dom_family == PF_INET6 &&
322 pr->pr_protocol && pr->pr_protocol == ip6proto) {
323 ip6_protox[pr->pr_protocol] = pr - inet6sw;
327 return (EPROTONOSUPPORT);
331 ip6proto_unregister(short ip6proto)
336 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
337 return (EPROTONOSUPPORT);
339 /* Check if the protocol was indeed registered. */
340 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
342 return (EPFNOSUPPORT);
343 if (ip6_protox[ip6proto] == pr - inet6sw) /* IPPROTO_RAW */
346 /* Reset the protocol slot to IPPROTO_RAW. */
347 ip6_protox[ip6proto] = pr - inet6sw;
353 ip6_destroy(void *unused __unused)
355 struct ifaddr *ifa, *nifa;
360 netisr_unregister_vnet(&ip6_direct_nh);
362 netisr_unregister_vnet(&ip6_nh);
364 if ((error = pfil_head_unregister(&V_inet6_pfil_hook)) != 0)
365 printf("%s: WARNING: unable to unregister pfil hook, "
366 "error %d\n", __func__, error);
367 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]);
369 printf("%s: WARNING: unable to deregister input helper hook "
370 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: "
371 "error %d returned\n", __func__, error);
373 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]);
375 printf("%s: WARNING: unable to deregister output helper hook "
376 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: "
377 "error %d returned\n", __func__, error);
380 /* Cleanup addresses. */
382 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
383 /* Cannot lock here - lock recursion. */
384 /* IF_ADDR_LOCK(ifp); */
385 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
387 if (ifa->ifa_addr->sa_family != AF_INET6)
391 /* IF_ADDR_UNLOCK(ifp); */
392 in6_ifdetach_destroy(ifp);
393 mld_domifdetach(ifp);
394 /* Make sure any routes are gone as well. */
395 rt_flushifroutes_af(ifp, AF_INET6);
400 in6_ifattach_destroy();
402 hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
405 VNET_SYSUNINIT(inet6, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_destroy, NULL);
409 ip6_input_hbh(struct mbuf *m, uint32_t *plen, uint32_t *rtalert, int *off,
415 if (ip6_hopopts_input(plen, rtalert, &m, off)) {
416 #if 0 /*touches NULL pointer*/
417 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
419 goto out; /* m have already been freed */
423 ip6 = mtod(m, struct ip6_hdr *);
426 * if the payload length field is 0 and the next header field
427 * indicates Hop-by-Hop Options header, then a Jumbo Payload
428 * option MUST be included.
430 if (ip6->ip6_plen == 0 && *plen == 0) {
432 * Note that if a valid jumbo payload option is
433 * contained, ip6_hopopts_input() must set a valid
434 * (non-zero) payload length to the variable plen.
436 IP6STAT_INC(ip6s_badoptions);
437 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
438 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
439 icmp6_error(m, ICMP6_PARAM_PROB,
440 ICMP6_PARAMPROB_HEADER,
441 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
444 #ifndef PULLDOWN_TEST
445 /* ip6_hopopts_input() ensures that mbuf is contiguous */
446 hbh = (struct ip6_hbh *)(ip6 + 1);
448 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
449 sizeof(struct ip6_hbh));
451 IP6STAT_INC(ip6s_tooshort);
455 *nxt = hbh->ip6h_nxt;
458 * If we are acting as a router and the packet contains a
459 * router alert option, see if we know the option value.
460 * Currently, we only support the option value for MLD, in which
461 * case we should pass the packet to the multicast routing
464 if (*rtalert != ~0) {
466 case IP6OPT_RTALERT_MLD:
467 if (V_ip6_forwarding)
472 * RFC2711 requires unrecognized values must be
487 * IPv6 direct input routine.
489 * This is called when reinjecting completed fragments where
490 * all of the previous checking and book-keeping has been done.
493 ip6_direct_input(struct mbuf *m)
498 struct ip6_direct_ctx *ip6dc;
500 mtag = m_tag_locate(m, MTAG_ABI_IPV6, IPV6_TAG_DIRECT, NULL);
501 KASSERT(mtag != NULL, ("Reinjected packet w/o direct ctx tag!"));
503 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
504 nxt = ip6dc->ip6dc_nxt;
505 off = ip6dc->ip6dc_off;
509 m_tag_delete(m, mtag);
511 while (nxt != IPPROTO_DONE) {
512 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
513 IP6STAT_INC(ip6s_toomanyhdr);
518 * protection against faulty packet - there should be
519 * more sanity checks in header chain processing.
521 if (m->m_pkthdr.len < off) {
522 IP6STAT_INC(ip6s_tooshort);
523 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
529 * enforce IPsec policy checking if we are seeing last header.
530 * note that we do not visit this with protocols with pcb layer
531 * code - like udp/tcp/raw ip.
533 if (ip6_ipsec_input(m, nxt))
537 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
546 ip6_input(struct mbuf *m)
548 struct in6_addr odst;
550 struct in6_ifaddr *ia;
552 u_int32_t rtalert = ~0;
553 int off = sizeof(struct ip6_hdr), nest;
559 * should the inner packet be considered authentic?
560 * see comment in ah4_input().
561 * NB: m cannot be NULL when passed to the input routine
564 m->m_flags &= ~M_AUTHIPHDR;
565 m->m_flags &= ~M_AUTHIPDGM;
569 if (m->m_flags & M_FASTFWD_OURS) {
571 * Firewall changed destination to local.
573 m->m_flags &= ~M_FASTFWD_OURS;
575 ip6 = mtod(m, struct ip6_hdr *);
582 if (m->m_flags & M_EXT) {
584 IP6STAT_INC(ip6s_mext2m);
586 IP6STAT_INC(ip6s_mext1);
589 if (m->m_flags & M_LOOP) {
590 IP6STAT_INC(ip6s_m2m[V_loif->if_index]);
591 } else if (m->m_pkthdr.rcvif->if_index < IP6S_M2MMAX)
593 ip6s_m2m[m->m_pkthdr.rcvif->if_index]);
595 IP6STAT_INC(ip6s_m2m[0]);
597 IP6STAT_INC(ip6s_m1);
600 /* drop the packet if IPv6 operation is disabled on the IF */
601 if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED))
604 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
605 IP6STAT_INC(ip6s_total);
607 #ifndef PULLDOWN_TEST
609 * L2 bridge code and some other code can return mbuf chain
610 * that does not conform to KAME requirement. too bad.
611 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
613 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
616 if (m->m_pkthdr.len > MHLEN)
617 n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
619 n = m_gethdr(M_NOWAIT, MT_DATA);
622 return; /* ENOBUFS */
626 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
627 n->m_len = n->m_pkthdr.len;
631 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
634 if (m->m_len < sizeof(struct ip6_hdr)) {
636 inifp = m->m_pkthdr.rcvif;
637 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
638 IP6STAT_INC(ip6s_toosmall);
639 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
644 ip6 = mtod(m, struct ip6_hdr *);
646 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
647 IP6STAT_INC(ip6s_badvers);
648 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
652 IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
654 IP_PROBE(receive, NULL, NULL, ip6, m->m_pkthdr.rcvif, NULL, ip6);
657 * Check against address spoofing/corruption.
659 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
660 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
662 * XXX: "badscope" is not very suitable for a multicast source.
664 IP6STAT_INC(ip6s_badscope);
665 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
668 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
669 !(m->m_flags & M_LOOP)) {
671 * In this case, the packet should come from the loopback
672 * interface. However, we cannot just check the if_flags,
673 * because ip6_mloopback() passes the "actual" interface
674 * as the outgoing/incoming interface.
676 IP6STAT_INC(ip6s_badscope);
677 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
680 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
681 IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
684 * Nodes must not originate a packet to a multicast address
685 * whose scop field contains the reserved value 0; if such
686 * a packet is received, it must be silently dropped.
688 IP6STAT_INC(ip6s_badscope);
689 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
693 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
694 /* packet is dropped by traffic conditioner */
699 * The following check is not documented in specs. A malicious
700 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
701 * and bypass security checks (act as if it was from 127.0.0.1 by using
702 * IPv6 src ::ffff:127.0.0.1). Be cautious.
704 * This check chokes if we are in an SIIT cloud. As none of BSDs
705 * support IPv4-less kernel compilation, we cannot support SIIT
706 * environment at all. So, it makes more sense for us to reject any
707 * malicious packets for non-SIIT environment, than try to do a
708 * partial support for SIIT environment.
710 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
711 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
712 IP6STAT_INC(ip6s_badscope);
713 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
718 * Reject packets with IPv4 compatible addresses (auto tunnel).
720 * The code forbids auto tunnel relay case in RFC1933 (the check is
721 * stronger than RFC1933). We may want to re-enable it if mech-xx
722 * is revised to forbid relaying case.
724 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
725 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
726 IP6STAT_INC(ip6s_badscope);
727 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
733 * Bypass packet filtering for packets previously handled by IPsec.
735 if (ip6_ipsec_filtertunnel(m))
740 * Run through list of hooks for input packets.
742 * NB: Beware of the destination address changing
743 * (e.g. by NAT rewriting). When this happens,
744 * tell ip6_forward to do the right thing.
748 /* Jump over all PFIL processing if hooks are not active. */
749 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
752 if (pfil_run_hooks(&V_inet6_pfil_hook, &m,
753 m->m_pkthdr.rcvif, PFIL_IN, NULL))
755 if (m == NULL) /* consumed by filter */
757 ip6 = mtod(m, struct ip6_hdr *);
758 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
760 if (m->m_flags & M_FASTFWD_OURS) {
761 m->m_flags &= ~M_FASTFWD_OURS;
765 if ((m->m_flags & M_IP6_NEXTHOP) &&
766 m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
768 * Directly ship the packet on. This allows forwarding
769 * packets originally destined to us to some other directly
778 * Disambiguate address scope zones (if there is ambiguity).
779 * We first make sure that the original source or destination address
780 * is not in our internal form for scoped addresses. Such addresses
781 * are not necessarily invalid spec-wise, but we cannot accept them due
782 * to the usage conflict.
783 * in6_setscope() then also checks and rejects the cases where src or
784 * dst are the loopback address and the receiving interface
787 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
788 IP6STAT_INC(ip6s_badscope); /* XXX */
791 if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
792 in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
793 IP6STAT_INC(ip6s_badscope);
797 * Multicast check. Assume packet is for us to avoid
798 * prematurely taking locks.
800 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
802 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
807 * XXX: For now we keep link-local IPv6 addresses with embedded
808 * scope zone id, therefore we use zero zoneid here.
810 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
812 if (ia->ia6_flags & IN6_IFF_NOTREADY) {
813 char ip6bufs[INET6_ADDRSTRLEN];
814 char ip6bufd[INET6_ADDRSTRLEN];
815 /* address is not ready, so discard the packet. */
817 "ip6_input: packet to an unready address %s->%s\n",
818 ip6_sprintf(ip6bufs, &ip6->ip6_src),
819 ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
820 ifa_free(&ia->ia_ifa);
823 /* Count the packet in the ip address stats */
824 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
825 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
826 ifa_free(&ia->ia_ifa);
832 * Now there is no reason to process the packet if it's not our own
833 * and we're not a router.
835 if (!V_ip6_forwarding) {
836 IP6STAT_INC(ip6s_cantforward);
837 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
843 * Process Hop-by-Hop options header if it's contained.
844 * m may be modified in ip6_hopopts_input().
845 * If a JumboPayload option is included, plen will also be modified.
847 plen = (u_int32_t)ntohs(ip6->ip6_plen);
848 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
849 if (ip6_input_hbh(m, &plen, &rtalert, &off, &nxt, &ours) != 0)
855 * Use mbuf flags to propagate Router Alert option to
856 * ICMPv6 layer, as hop-by-hop options have been stripped.
859 m->m_flags |= M_RTALERT_MLD;
862 * Check that the amount of data in the buffers
863 * is as at least much as the IPv6 header would have us expect.
864 * Trim mbufs if longer than we expect.
865 * Drop packet if shorter than we expect.
867 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
868 IP6STAT_INC(ip6s_tooshort);
869 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
872 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
873 if (m->m_len == m->m_pkthdr.len) {
874 m->m_len = sizeof(struct ip6_hdr) + plen;
875 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
877 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
881 * Forward if desirable.
884 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
886 * If we are acting as a multicast router, all
887 * incoming multicast packets are passed to the
888 * kernel-level multicast forwarding function.
889 * The packet is returned (relatively) intact; if
890 * ip6_mforward() returns a non-zero value, the packet
891 * must be discarded, else it may be accepted below.
893 * XXX TODO: Check hlim and multicast scope here to avoid
894 * unnecessarily calling into ip6_mforward().
897 ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
898 IP6STAT_INC(ip6s_cantforward);
899 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
903 ip6_forward(m, srcrt);
907 ip6 = mtod(m, struct ip6_hdr *);
910 * Malicious party may be able to use IPv4 mapped addr to confuse
911 * tcp/udp stack and bypass security checks (act as if it was from
912 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
914 * For SIIT end node behavior, you may want to disable the check.
915 * However, you will become vulnerable to attacks using IPv4 mapped
918 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
919 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
920 IP6STAT_INC(ip6s_badscope);
921 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
926 * Tell launch routine the next header
928 IP6STAT_INC(ip6s_delivered);
929 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_deliver);
932 while (nxt != IPPROTO_DONE) {
933 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
934 IP6STAT_INC(ip6s_toomanyhdr);
939 * protection against faulty packet - there should be
940 * more sanity checks in header chain processing.
942 if (m->m_pkthdr.len < off) {
943 IP6STAT_INC(ip6s_tooshort);
944 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
950 * enforce IPsec policy checking if we are seeing last header.
951 * note that we do not visit this with protocols with pcb layer
952 * code - like udp/tcp/raw ip.
954 if (ip6_ipsec_input(m, nxt))
958 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
966 * Hop-by-Hop options header processing. If a valid jumbo payload option is
967 * included, the real payload length will be stored in plenp.
969 * rtalertp - XXX: should be stored more smart way
972 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
973 struct mbuf **mp, int *offp)
975 struct mbuf *m = *mp;
976 int off = *offp, hbhlen;
979 /* validation of the length of the header */
980 #ifndef PULLDOWN_TEST
981 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
982 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
983 hbhlen = (hbh->ip6h_len + 1) << 3;
985 IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
986 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
988 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
989 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
991 IP6STAT_INC(ip6s_tooshort);
994 hbhlen = (hbh->ip6h_len + 1) << 3;
995 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
998 IP6STAT_INC(ip6s_tooshort);
1003 hbhlen -= sizeof(struct ip6_hbh);
1004 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
1005 hbhlen, rtalertp, plenp) < 0)
1014 * Search header for all Hop-by-hop options and process each option.
1015 * This function is separate from ip6_hopopts_input() in order to
1016 * handle a case where the sending node itself process its hop-by-hop
1017 * options header. In such a case, the function is called from ip6_output().
1019 * The function assumes that hbh header is located right after the IPv6 header
1020 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1021 * opthead + hbhlen is located in contiguous memory region.
1024 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
1025 u_int32_t *rtalertp, u_int32_t *plenp)
1027 struct ip6_hdr *ip6;
1029 u_int8_t *opt = opthead;
1030 u_int16_t rtalert_val;
1031 u_int32_t jumboplen;
1032 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1034 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1040 if (hbhlen < IP6OPT_MINLEN) {
1041 IP6STAT_INC(ip6s_toosmall);
1044 optlen = *(opt + 1) + 2;
1046 case IP6OPT_ROUTER_ALERT:
1047 /* XXX may need check for alignment */
1048 if (hbhlen < IP6OPT_RTALERT_LEN) {
1049 IP6STAT_INC(ip6s_toosmall);
1052 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1054 icmp6_error(m, ICMP6_PARAM_PROB,
1055 ICMP6_PARAMPROB_HEADER,
1056 erroff + opt + 1 - opthead);
1059 optlen = IP6OPT_RTALERT_LEN;
1060 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1061 *rtalertp = ntohs(rtalert_val);
1064 /* XXX may need check for alignment */
1065 if (hbhlen < IP6OPT_JUMBO_LEN) {
1066 IP6STAT_INC(ip6s_toosmall);
1069 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1071 icmp6_error(m, ICMP6_PARAM_PROB,
1072 ICMP6_PARAMPROB_HEADER,
1073 erroff + opt + 1 - opthead);
1076 optlen = IP6OPT_JUMBO_LEN;
1079 * IPv6 packets that have non 0 payload length
1080 * must not contain a jumbo payload option.
1082 ip6 = mtod(m, struct ip6_hdr *);
1083 if (ip6->ip6_plen) {
1084 IP6STAT_INC(ip6s_badoptions);
1085 icmp6_error(m, ICMP6_PARAM_PROB,
1086 ICMP6_PARAMPROB_HEADER,
1087 erroff + opt - opthead);
1092 * We may see jumbolen in unaligned location, so
1093 * we'd need to perform bcopy().
1095 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1096 jumboplen = (u_int32_t)htonl(jumboplen);
1100 * if there are multiple jumbo payload options,
1101 * *plenp will be non-zero and the packet will be
1103 * the behavior may need some debate in ipngwg -
1104 * multiple options does not make sense, however,
1105 * there's no explicit mention in specification.
1108 IP6STAT_INC(ip6s_badoptions);
1109 icmp6_error(m, ICMP6_PARAM_PROB,
1110 ICMP6_PARAMPROB_HEADER,
1111 erroff + opt + 2 - opthead);
1117 * jumbo payload length must be larger than 65535.
1119 if (jumboplen <= IPV6_MAXPACKET) {
1120 IP6STAT_INC(ip6s_badoptions);
1121 icmp6_error(m, ICMP6_PARAM_PROB,
1122 ICMP6_PARAMPROB_HEADER,
1123 erroff + opt + 2 - opthead);
1129 default: /* unknown option */
1130 if (hbhlen < IP6OPT_MINLEN) {
1131 IP6STAT_INC(ip6s_toosmall);
1134 optlen = ip6_unknown_opt(opt, m,
1135 erroff + opt - opthead);
1151 * Unknown option processing.
1152 * The third argument `off' is the offset from the IPv6 header to the option,
1153 * which is necessary if the IPv6 header the and option header and IPv6 header
1154 * is not contiguous in order to return an ICMPv6 error.
1157 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1159 struct ip6_hdr *ip6;
1161 switch (IP6OPT_TYPE(*optp)) {
1162 case IP6OPT_TYPE_SKIP: /* ignore the option */
1163 return ((int)*(optp + 1));
1164 case IP6OPT_TYPE_DISCARD: /* silently discard */
1167 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1168 IP6STAT_INC(ip6s_badoptions);
1169 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1171 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1172 IP6STAT_INC(ip6s_badoptions);
1173 ip6 = mtod(m, struct ip6_hdr *);
1174 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1175 (m->m_flags & (M_BCAST|M_MCAST)))
1178 icmp6_error(m, ICMP6_PARAM_PROB,
1179 ICMP6_PARAMPROB_OPTION, off);
1183 m_freem(m); /* XXX: NOTREACHED */
1188 * Create the "control" list for this pcb.
1189 * These functions will not modify mbuf chain at all.
1191 * With KAME mbuf chain restriction:
1192 * The routine will be called from upper layer handlers like tcp6_input().
1193 * Thus the routine assumes that the caller (tcp6_input) have already
1194 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1195 * very first mbuf on the mbuf chain.
1197 * ip6_savecontrol_v4 will handle those options that are possible to be
1198 * set on a v4-mapped socket.
1199 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1200 * options and handle the v6-only ones itself.
1203 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1206 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1209 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1213 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1214 SCM_TIMESTAMP, SOL_SOCKET);
1216 mp = &(*mp)->m_next;
1220 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1221 /* RFC 2292 sec. 5 */
1222 if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1223 struct in6_pktinfo pi6;
1225 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1229 ip = mtod(m, struct ip *);
1230 pi6.ipi6_addr.s6_addr32[0] = 0;
1231 pi6.ipi6_addr.s6_addr32[1] = 0;
1232 pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1233 pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1235 /* We won't hit this code */
1236 bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1239 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1240 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1243 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1245 *mp = sbcreatecontrol((caddr_t) &pi6,
1246 sizeof(struct in6_pktinfo),
1247 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1249 mp = &(*mp)->m_next;
1252 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1255 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1259 ip = mtod(m, struct ip *);
1262 /* We won't hit this code */
1266 hlim = ip6->ip6_hlim & 0xff;
1268 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1269 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1272 mp = &(*mp)->m_next;
1275 if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1278 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1282 ip = mtod(m, struct ip *);
1283 tclass = ip->ip_tos;
1285 /* We won't hit this code */
1291 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1293 tclass = flowinfo & 0xff;
1295 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int),
1296 IPV6_TCLASS, IPPROTO_IPV6);
1298 mp = &(*mp)->m_next;
1301 if (v4only != NULL) {
1302 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1313 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1315 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1318 mp = ip6_savecontrol_v4(in6p, m, mp, &v4only);
1323 * IPV6_HOPOPTS socket option. Recall that we required super-user
1324 * privilege for the option (see ip6_ctloutput), but it might be too
1325 * strict, since there might be some hop-by-hop options which can be
1326 * returned to normal user.
1327 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1329 if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
1331 * Check if a hop-by-hop options header is contatined in the
1332 * received packet, and if so, store the options as ancillary
1333 * data. Note that a hop-by-hop options header must be
1334 * just after the IPv6 header, which is assured through the
1335 * IPv6 input processing.
1337 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1338 struct ip6_hbh *hbh;
1340 #ifdef PULLDOWN_TEST
1344 #ifndef PULLDOWN_TEST
1345 hbh = (struct ip6_hbh *)(ip6 + 1);
1346 hbhlen = (hbh->ip6h_len + 1) << 3;
1348 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1351 IP6STAT_INC(ip6s_tooshort);
1354 hbh = mtod(ext, struct ip6_hbh *);
1355 hbhlen = (hbh->ip6h_len + 1) << 3;
1356 if (hbhlen != ext->m_len) {
1358 IP6STAT_INC(ip6s_tooshort);
1364 * XXX: We copy the whole header even if a
1365 * jumbo payload option is included, the option which
1366 * is to be removed before returning according to
1368 * Note: this constraint is removed in RFC3542
1370 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1371 IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1374 mp = &(*mp)->m_next;
1375 #ifdef PULLDOWN_TEST
1381 if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1382 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1385 * Search for destination options headers or routing
1386 * header(s) through the header chain, and stores each
1387 * header as ancillary data.
1388 * Note that the order of the headers remains in
1389 * the chain of ancillary data.
1391 while (1) { /* is explicit loop prevention necessary? */
1392 struct ip6_ext *ip6e = NULL;
1394 #ifdef PULLDOWN_TEST
1395 struct mbuf *ext = NULL;
1399 * if it is not an extension header, don't try to
1400 * pull it from the chain.
1403 case IPPROTO_DSTOPTS:
1404 case IPPROTO_ROUTING:
1405 case IPPROTO_HOPOPTS:
1406 case IPPROTO_AH: /* is it possible? */
1412 #ifndef PULLDOWN_TEST
1413 if (off + sizeof(*ip6e) > m->m_len)
1415 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1416 if (nxt == IPPROTO_AH)
1417 elen = (ip6e->ip6e_len + 2) << 2;
1419 elen = (ip6e->ip6e_len + 1) << 3;
1420 if (off + elen > m->m_len)
1423 ext = ip6_pullexthdr(m, off, nxt);
1425 IP6STAT_INC(ip6s_tooshort);
1428 ip6e = mtod(ext, struct ip6_ext *);
1429 if (nxt == IPPROTO_AH)
1430 elen = (ip6e->ip6e_len + 2) << 2;
1432 elen = (ip6e->ip6e_len + 1) << 3;
1433 if (elen != ext->m_len) {
1435 IP6STAT_INC(ip6s_tooshort);
1441 case IPPROTO_DSTOPTS:
1442 if (!(in6p->inp_flags & IN6P_DSTOPTS))
1445 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1447 IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1450 mp = &(*mp)->m_next;
1452 case IPPROTO_ROUTING:
1453 if (!(in6p->inp_flags & IN6P_RTHDR))
1456 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1457 IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
1460 mp = &(*mp)->m_next;
1462 case IPPROTO_HOPOPTS:
1463 case IPPROTO_AH: /* is it possible? */
1468 * other cases have been filtered in the above.
1469 * none will visit this case. here we supply
1470 * the code just in case (nxt overwritten or
1473 #ifdef PULLDOWN_TEST
1480 /* proceed with the next header. */
1482 nxt = ip6e->ip6e_nxt;
1484 #ifdef PULLDOWN_TEST
1493 if (in6p->inp_flags2 & INP_RECVFLOWID) {
1494 uint32_t flowid, flow_type;
1496 flowid = m->m_pkthdr.flowid;
1497 flow_type = M_HASHTYPE_GET(m);
1500 * XXX should handle the failure of one or the
1501 * other - don't populate both?
1503 *mp = sbcreatecontrol((caddr_t) &flowid,
1504 sizeof(uint32_t), IPV6_FLOWID, IPPROTO_IPV6);
1506 mp = &(*mp)->m_next;
1507 *mp = sbcreatecontrol((caddr_t) &flow_type,
1508 sizeof(uint32_t), IPV6_FLOWTYPE, IPPROTO_IPV6);
1510 mp = &(*mp)->m_next;
1514 if (in6p->inp_flags2 & INP_RECVRSSBUCKETID) {
1515 uint32_t flowid, flow_type;
1516 uint32_t rss_bucketid;
1518 flowid = m->m_pkthdr.flowid;
1519 flow_type = M_HASHTYPE_GET(m);
1521 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1522 *mp = sbcreatecontrol((caddr_t) &rss_bucketid,
1523 sizeof(uint32_t), IPV6_RSSBUCKETID, IPPROTO_IPV6);
1525 mp = &(*mp)->m_next;
1534 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu)
1538 struct ip6_mtuinfo mtuctl;
1540 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1542 * Notify the error by sending IPV6_PATHMTU ancillary data if
1543 * application wanted to know the MTU value.
1544 * NOTE: we notify disconnected sockets, because some udp
1545 * applications keep sending sockets disconnected.
1546 * NOTE: our implementation doesn't notify connected sockets that has
1547 * foreign address that is different than given destination addresses
1548 * (this is permitted by RFC 3542).
1550 if ((inp->inp_flags & IN6P_MTU) == 0 || (
1551 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1552 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr)))
1555 mtuctl.ip6m_mtu = mtu;
1556 mtuctl.ip6m_addr = *dst;
1557 if (sa6_recoverscope(&mtuctl.ip6m_addr))
1560 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1561 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1564 so = inp->inp_socket;
1565 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1568 /* XXX: should count statistics */
1573 #ifdef PULLDOWN_TEST
1575 * pull single extension header from mbuf chain. returns single mbuf that
1576 * contains the result, or NULL on error.
1578 static struct mbuf *
1579 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1581 struct ip6_ext ip6e;
1587 case IPPROTO_DSTOPTS:
1588 case IPPROTO_ROUTING:
1589 case IPPROTO_HOPOPTS:
1590 case IPPROTO_AH: /* is it possible? */
1593 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1597 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1598 if (nxt == IPPROTO_AH)
1599 elen = (ip6e.ip6e_len + 2) << 2;
1601 elen = (ip6e.ip6e_len + 1) << 3;
1604 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1606 n = m_get(M_NOWAIT, MT_DATA);
1610 m_copydata(m, off, elen, mtod(n, caddr_t));
1617 * Get pointer to the previous header followed by the header
1618 * currently processed.
1619 * XXX: This function supposes that
1620 * M includes all headers,
1621 * the next header field and the header length field of each header
1623 * the sum of each header length equals to OFF.
1624 * Because of these assumptions, this function must be called very
1625 * carefully. Moreover, it will not be used in the near future when
1626 * we develop `neater' mechanism to process extension headers.
1629 ip6_get_prevhdr(const struct mbuf *m, int off)
1631 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1633 if (off == sizeof(struct ip6_hdr))
1634 return (&ip6->ip6_nxt);
1637 struct ip6_ext *ip6e = NULL;
1640 len = sizeof(struct ip6_hdr);
1642 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1645 case IPPROTO_FRAGMENT:
1646 len += sizeof(struct ip6_frag);
1649 len += (ip6e->ip6e_len + 2) << 2;
1652 len += (ip6e->ip6e_len + 1) << 3;
1655 nxt = ip6e->ip6e_nxt;
1658 return (&ip6e->ip6e_nxt);
1665 * get next header offset. m will be retained.
1668 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1671 struct ip6_ext ip6e;
1676 panic("ip6_nexthdr: m == NULL");
1677 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1682 if (m->m_pkthdr.len < off + sizeof(ip6))
1684 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1686 *nxtp = ip6.ip6_nxt;
1690 case IPPROTO_FRAGMENT:
1692 * terminate parsing if it is not the first fragment,
1693 * it does not make sense to parse through it.
1695 if (m->m_pkthdr.len < off + sizeof(fh))
1697 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1698 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1699 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1702 *nxtp = fh.ip6f_nxt;
1703 off += sizeof(struct ip6_frag);
1707 if (m->m_pkthdr.len < off + sizeof(ip6e))
1709 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1711 *nxtp = ip6e.ip6e_nxt;
1712 off += (ip6e.ip6e_len + 2) << 2;
1715 case IPPROTO_HOPOPTS:
1716 case IPPROTO_ROUTING:
1717 case IPPROTO_DSTOPTS:
1718 if (m->m_pkthdr.len < off + sizeof(ip6e))
1720 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1722 *nxtp = ip6e.ip6e_nxt;
1723 off += (ip6e.ip6e_len + 1) << 3;
1728 case IPPROTO_IPCOMP:
1740 * get offset for the last header in the chain. m will be kept untainted.
1743 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1753 newoff = ip6_nexthdr(m, off, proto, nxtp);
1756 else if (newoff < off)
1757 return -1; /* invalid */
1758 else if (newoff == off)
1767 * System control for IP6
1770 u_char inet6ctlerrmap[PRC_NCMDS] = {
1772 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1773 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1774 EMSGSIZE, EHOSTUNREACH, 0, 0,