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>
121 #include <netipsec/ipsec_support.h>
123 #include <netinet6/ip6protosw.h>
125 extern struct domain inet6domain;
127 u_char ip6_protox[IPPROTO_MAX];
128 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
129 VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
130 VNET_DEFINE(u_long, in6_ifaddrhmask);
132 static struct netisr_handler ip6_nh = {
134 .nh_handler = ip6_input,
135 .nh_proto = NETISR_IPV6,
137 .nh_m2cpuid = rss_soft_m2cpuid_v6,
138 .nh_policy = NETISR_POLICY_CPU,
139 .nh_dispatch = NETISR_DISPATCH_HYBRID,
141 .nh_policy = NETISR_POLICY_FLOW,
146 sysctl_netinet6_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
150 netisr_getqlimit(&ip6_nh, &qlimit);
151 error = sysctl_handle_int(oidp, &qlimit, 0, req);
152 if (error || !req->newptr)
156 return (netisr_setqlimit(&ip6_nh, qlimit));
158 SYSCTL_DECL(_net_inet6_ip6);
159 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRQMAXLEN, intr_queue_maxlen,
160 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet6_intr_queue_maxlen, "I",
161 "Maximum size of the IPv6 input queue");
164 static struct netisr_handler ip6_direct_nh = {
165 .nh_name = "ip6_direct",
166 .nh_handler = ip6_direct_input,
167 .nh_proto = NETISR_IPV6_DIRECT,
168 .nh_m2cpuid = rss_soft_m2cpuid_v6,
169 .nh_policy = NETISR_POLICY_CPU,
170 .nh_dispatch = NETISR_DISPATCH_HYBRID,
174 sysctl_netinet6_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
178 netisr_getqlimit(&ip6_direct_nh, &qlimit);
179 error = sysctl_handle_int(oidp, &qlimit, 0, req);
180 if (error || !req->newptr)
184 return (netisr_setqlimit(&ip6_direct_nh, qlimit));
186 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
187 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet6_intr_direct_queue_maxlen,
188 "I", "Maximum size of the IPv6 direct input queue");
192 VNET_DEFINE(struct pfil_head, inet6_pfil_hook);
194 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
195 VNET_PCPUSTAT_SYSINIT(ip6stat);
197 VNET_PCPUSTAT_SYSUNINIT(ip6stat);
200 struct rmlock in6_ifaddr_lock;
201 RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
203 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
205 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
209 * IP6 initialization: fill in IP6 protocol switch table.
210 * All protocols not implemented in kernel go to raw IP6 protocol handler.
218 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
219 &V_ip6_auto_linklocal);
220 TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
221 TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
223 TAILQ_INIT(&V_in6_ifaddrhead);
224 V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
227 /* Initialize packet filter hooks. */
228 V_inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
229 V_inet6_pfil_hook.ph_af = AF_INET6;
230 if ((i = pfil_head_register(&V_inet6_pfil_hook)) != 0)
231 printf("%s: WARNING: unable to register pfil hook, "
232 "error %d\n", __func__, i);
234 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6,
235 &V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
236 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
237 printf("%s: WARNING: unable to register input helper hook\n",
239 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6,
240 &V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
241 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
242 printf("%s: WARNING: unable to register output helper hook\n",
246 addrsel_policy_init();
250 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
252 /* Skip global initialization stuff for non-default instances. */
254 if (!IS_DEFAULT_VNET(curvnet)) {
255 netisr_register_vnet(&ip6_nh);
257 netisr_register_vnet(&ip6_direct_nh);
263 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
267 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
268 for (i = 0; i < IPPROTO_MAX; i++)
269 ip6_protox[i] = pr - inet6sw;
271 * Cycle through IP protocols and put them into the appropriate place
274 for (pr = inet6domain.dom_protosw;
275 pr < inet6domain.dom_protoswNPROTOSW; pr++)
276 if (pr->pr_domain->dom_family == PF_INET6 &&
277 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
278 /* Be careful to only index valid IP protocols. */
279 if (pr->pr_protocol < IPPROTO_MAX)
280 ip6_protox[pr->pr_protocol] = pr - inet6sw;
283 netisr_register(&ip6_nh);
285 netisr_register(&ip6_direct_nh);
290 * The protocol to be inserted into ip6_protox[] must be already registered
291 * in inet6sw[], either statically or through pf_proto_register().
294 ip6proto_register(short ip6proto)
299 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
300 return (EPROTONOSUPPORT);
303 * The protocol slot must not be occupied by another protocol
304 * already. An index pointing to IPPROTO_RAW is unused.
306 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
308 return (EPFNOSUPPORT);
309 if (ip6_protox[ip6proto] != pr - inet6sw) /* IPPROTO_RAW */
313 * Find the protocol position in inet6sw[] and set the index.
315 for (pr = inet6domain.dom_protosw;
316 pr < inet6domain.dom_protoswNPROTOSW; pr++) {
317 if (pr->pr_domain->dom_family == PF_INET6 &&
318 pr->pr_protocol && pr->pr_protocol == ip6proto) {
319 ip6_protox[pr->pr_protocol] = pr - inet6sw;
323 return (EPROTONOSUPPORT);
327 ip6proto_unregister(short ip6proto)
332 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
333 return (EPROTONOSUPPORT);
335 /* Check if the protocol was indeed registered. */
336 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
338 return (EPFNOSUPPORT);
339 if (ip6_protox[ip6proto] == pr - inet6sw) /* IPPROTO_RAW */
342 /* Reset the protocol slot to IPPROTO_RAW. */
343 ip6_protox[ip6proto] = pr - inet6sw;
349 ip6_destroy(void *unused __unused)
351 struct ifaddr *ifa, *nifa;
356 netisr_unregister_vnet(&ip6_direct_nh);
358 netisr_unregister_vnet(&ip6_nh);
360 if ((error = pfil_head_unregister(&V_inet6_pfil_hook)) != 0)
361 printf("%s: WARNING: unable to unregister pfil hook, "
362 "error %d\n", __func__, error);
363 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]);
365 printf("%s: WARNING: unable to deregister input helper hook "
366 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: "
367 "error %d returned\n", __func__, error);
369 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]);
371 printf("%s: WARNING: unable to deregister output helper hook "
372 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: "
373 "error %d returned\n", __func__, error);
376 /* Cleanup addresses. */
378 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
379 /* Cannot lock here - lock recursion. */
380 /* IF_ADDR_LOCK(ifp); */
381 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
383 if (ifa->ifa_addr->sa_family != AF_INET6)
387 /* IF_ADDR_UNLOCK(ifp); */
388 in6_ifdetach_destroy(ifp);
389 mld_domifdetach(ifp);
390 /* Make sure any routes are gone as well. */
391 rt_flushifroutes_af(ifp, AF_INET6);
396 in6_ifattach_destroy();
398 hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
401 VNET_SYSUNINIT(inet6, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_destroy, NULL);
405 ip6_input_hbh(struct mbuf *m, uint32_t *plen, uint32_t *rtalert, int *off,
411 if (ip6_hopopts_input(plen, rtalert, &m, off)) {
412 #if 0 /*touches NULL pointer*/
413 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
415 goto out; /* m have already been freed */
419 ip6 = mtod(m, struct ip6_hdr *);
422 * if the payload length field is 0 and the next header field
423 * indicates Hop-by-Hop Options header, then a Jumbo Payload
424 * option MUST be included.
426 if (ip6->ip6_plen == 0 && *plen == 0) {
428 * Note that if a valid jumbo payload option is
429 * contained, ip6_hopopts_input() must set a valid
430 * (non-zero) payload length to the variable plen.
432 IP6STAT_INC(ip6s_badoptions);
433 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
434 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
435 icmp6_error(m, ICMP6_PARAM_PROB,
436 ICMP6_PARAMPROB_HEADER,
437 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
440 #ifndef PULLDOWN_TEST
441 /* ip6_hopopts_input() ensures that mbuf is contiguous */
442 hbh = (struct ip6_hbh *)(ip6 + 1);
444 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
445 sizeof(struct ip6_hbh));
447 IP6STAT_INC(ip6s_tooshort);
451 *nxt = hbh->ip6h_nxt;
454 * If we are acting as a router and the packet contains a
455 * router alert option, see if we know the option value.
456 * Currently, we only support the option value for MLD, in which
457 * case we should pass the packet to the multicast routing
460 if (*rtalert != ~0) {
462 case IP6OPT_RTALERT_MLD:
463 if (V_ip6_forwarding)
468 * RFC2711 requires unrecognized values must be
483 * IPv6 direct input routine.
485 * This is called when reinjecting completed fragments where
486 * all of the previous checking and book-keeping has been done.
489 ip6_direct_input(struct mbuf *m)
494 struct ip6_direct_ctx *ip6dc;
496 mtag = m_tag_locate(m, MTAG_ABI_IPV6, IPV6_TAG_DIRECT, NULL);
497 KASSERT(mtag != NULL, ("Reinjected packet w/o direct ctx tag!"));
499 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
500 nxt = ip6dc->ip6dc_nxt;
501 off = ip6dc->ip6dc_off;
505 m_tag_delete(m, mtag);
507 while (nxt != IPPROTO_DONE) {
508 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
509 IP6STAT_INC(ip6s_toomanyhdr);
514 * protection against faulty packet - there should be
515 * more sanity checks in header chain processing.
517 if (m->m_pkthdr.len < off) {
518 IP6STAT_INC(ip6s_tooshort);
519 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
523 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
524 if (IPSEC_ENABLED(ipv6)) {
525 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
530 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
539 ip6_input(struct mbuf *m)
541 struct in6_addr odst;
543 struct in6_ifaddr *ia;
546 u_int32_t rtalert = ~0;
547 int off = sizeof(struct ip6_hdr), nest;
552 * Drop the packet if IPv6 operation is disabled on the interface.
554 rcvif = m->m_pkthdr.rcvif;
555 if ((ND_IFINFO(rcvif)->flags & ND6_IFF_IFDISABLED))
558 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
560 * should the inner packet be considered authentic?
561 * see comment in ah4_input().
562 * NB: m cannot be NULL when passed to the input routine
565 m->m_flags &= ~M_AUTHIPHDR;
566 m->m_flags &= ~M_AUTHIPDGM;
570 if (m->m_flags & M_FASTFWD_OURS) {
572 * Firewall changed destination to local.
574 ip6 = mtod(m, struct ip6_hdr *);
581 if (m->m_flags & M_EXT) {
583 IP6STAT_INC(ip6s_mext2m);
585 IP6STAT_INC(ip6s_mext1);
588 if (m->m_flags & M_LOOP) {
589 IP6STAT_INC(ip6s_m2m[V_loif->if_index]);
590 } else if (rcvif->if_index < IP6S_M2MMAX)
591 IP6STAT_INC(ip6s_m2m[rcvif->if_index]);
593 IP6STAT_INC(ip6s_m2m[0]);
595 IP6STAT_INC(ip6s_m1);
598 in6_ifstat_inc(rcvif, ifs6_in_receive);
599 IP6STAT_INC(ip6s_total);
601 #ifndef PULLDOWN_TEST
603 * L2 bridge code and some other code can return mbuf chain
604 * that does not conform to KAME requirement. too bad.
605 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
607 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
610 if (m->m_pkthdr.len > MHLEN)
611 n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
613 n = m_gethdr(M_NOWAIT, MT_DATA);
618 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
619 n->m_len = n->m_pkthdr.len;
623 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
626 if (m->m_len < sizeof(struct ip6_hdr)) {
627 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
628 IP6STAT_INC(ip6s_toosmall);
629 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
634 ip6 = mtod(m, struct ip6_hdr *);
635 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
636 IP6STAT_INC(ip6s_badvers);
637 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
641 IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
642 IP_PROBE(receive, NULL, NULL, ip6, rcvif, NULL, ip6);
645 * Check against address spoofing/corruption.
647 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
648 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
650 * XXX: "badscope" is not very suitable for a multicast source.
652 IP6STAT_INC(ip6s_badscope);
653 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
656 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
657 !(m->m_flags & M_LOOP)) {
659 * In this case, the packet should come from the loopback
660 * interface. However, we cannot just check the if_flags,
661 * because ip6_mloopback() passes the "actual" interface
662 * as the outgoing/incoming interface.
664 IP6STAT_INC(ip6s_badscope);
665 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
668 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
669 IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
672 * Nodes must not originate a packet to a multicast address
673 * whose scop field contains the reserved value 0; if such
674 * a packet is received, it must be silently dropped.
676 IP6STAT_INC(ip6s_badscope);
677 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
681 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
682 /* packet is dropped by traffic conditioner */
687 * The following check is not documented in specs. A malicious
688 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
689 * and bypass security checks (act as if it was from 127.0.0.1 by using
690 * IPv6 src ::ffff:127.0.0.1). Be cautious.
692 * This check chokes if we are in an SIIT cloud. As none of BSDs
693 * support IPv4-less kernel compilation, we cannot support SIIT
694 * environment at all. So, it makes more sense for us to reject any
695 * malicious packets for non-SIIT environment, than try to do a
696 * partial support for SIIT environment.
698 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
699 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
700 IP6STAT_INC(ip6s_badscope);
701 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
706 * Reject packets with IPv4 compatible addresses (auto tunnel).
708 * The code forbids auto tunnel relay case in RFC1933 (the check is
709 * stronger than RFC1933). We may want to re-enable it if mech-xx
710 * is revised to forbid relaying case.
712 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
713 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
714 IP6STAT_INC(ip6s_badscope);
715 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
720 * Try to forward the packet, but if we fail continue.
721 * ip6_tryforward() does not generate redirects, so fall
722 * through to normal processing if redirects are required.
723 * ip6_tryforward() does inbound and outbound packet firewall
724 * processing. If firewall has decided that destination becomes
725 * our local address, it sets M_FASTFWD_OURS flag. In this
726 * case skip another inbound firewall processing and update
729 if (V_ip6_forwarding != 0 && V_ip6_sendredirects == 0
730 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
731 && (!IPSEC_ENABLED(ipv6) ||
732 IPSEC_CAPS(ipv6, m, IPSEC_CAP_OPERABLE) == 0)
735 if ((m = ip6_tryforward(m)) == NULL)
737 if (m->m_flags & M_FASTFWD_OURS) {
738 ip6 = mtod(m, struct ip6_hdr *);
742 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
744 * Bypass packet filtering for packets previously handled by IPsec.
746 if (IPSEC_ENABLED(ipv6) &&
747 IPSEC_CAPS(ipv6, m, IPSEC_CAP_BYPASS_FILTER) != 0)
751 * Run through list of hooks for input packets.
753 * NB: Beware of the destination address changing
754 * (e.g. by NAT rewriting). When this happens,
755 * tell ip6_forward to do the right thing.
758 /* Jump over all PFIL processing if hooks are not active. */
759 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
763 if (pfil_run_hooks(&V_inet6_pfil_hook, &m,
764 m->m_pkthdr.rcvif, PFIL_IN, 0, NULL))
766 if (m == NULL) /* consumed by filter */
768 ip6 = mtod(m, struct ip6_hdr *);
769 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
770 if ((m->m_flags & (M_IP6_NEXTHOP | M_FASTFWD_OURS)) == M_IP6_NEXTHOP &&
771 m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
773 * Directly ship the packet on. This allows forwarding
774 * packets originally destined to us to some other directly
783 * Disambiguate address scope zones (if there is ambiguity).
784 * We first make sure that the original source or destination address
785 * is not in our internal form for scoped addresses. Such addresses
786 * are not necessarily invalid spec-wise, but we cannot accept them due
787 * to the usage conflict.
788 * in6_setscope() then also checks and rejects the cases where src or
789 * dst are the loopback address and the receiving interface
792 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
793 IP6STAT_INC(ip6s_badscope); /* XXX */
796 if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
797 in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
798 IP6STAT_INC(ip6s_badscope);
801 if (m->m_flags & M_FASTFWD_OURS) {
802 m->m_flags &= ~M_FASTFWD_OURS;
807 * Multicast check. Assume packet is for us to avoid
808 * prematurely taking locks.
810 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
812 in6_ifstat_inc(rcvif, ifs6_in_mcast);
817 * XXX: For now we keep link-local IPv6 addresses with embedded
818 * scope zone id, therefore we use zero zoneid here.
820 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
822 if (ia->ia6_flags & IN6_IFF_NOTREADY) {
823 char ip6bufs[INET6_ADDRSTRLEN];
824 char ip6bufd[INET6_ADDRSTRLEN];
825 /* address is not ready, so discard the packet. */
827 "ip6_input: packet to an unready address %s->%s\n",
828 ip6_sprintf(ip6bufs, &ip6->ip6_src),
829 ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
830 ifa_free(&ia->ia_ifa);
833 /* Count the packet in the ip address stats */
834 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
835 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
836 ifa_free(&ia->ia_ifa);
842 * Now there is no reason to process the packet if it's not our own
843 * and we're not a router.
845 if (!V_ip6_forwarding) {
846 IP6STAT_INC(ip6s_cantforward);
852 * Process Hop-by-Hop options header if it's contained.
853 * m may be modified in ip6_hopopts_input().
854 * If a JumboPayload option is included, plen will also be modified.
856 plen = (u_int32_t)ntohs(ip6->ip6_plen);
857 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
858 if (ip6_input_hbh(m, &plen, &rtalert, &off, &nxt, &ours) != 0)
864 * Use mbuf flags to propagate Router Alert option to
865 * ICMPv6 layer, as hop-by-hop options have been stripped.
868 m->m_flags |= M_RTALERT_MLD;
871 * Check that the amount of data in the buffers
872 * is as at least much as the IPv6 header would have us expect.
873 * Trim mbufs if longer than we expect.
874 * Drop packet if shorter than we expect.
876 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
877 IP6STAT_INC(ip6s_tooshort);
878 in6_ifstat_inc(rcvif, ifs6_in_truncated);
881 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
882 if (m->m_len == m->m_pkthdr.len) {
883 m->m_len = sizeof(struct ip6_hdr) + plen;
884 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
886 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
890 * Forward if desirable.
893 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
895 * If we are acting as a multicast router, all
896 * incoming multicast packets are passed to the
897 * kernel-level multicast forwarding function.
898 * The packet is returned (relatively) intact; if
899 * ip6_mforward() returns a non-zero value, the packet
900 * must be discarded, else it may be accepted below.
902 * XXX TODO: Check hlim and multicast scope here to avoid
903 * unnecessarily calling into ip6_mforward().
905 if (ip6_mforward && ip6_mforward(ip6, rcvif, m)) {
906 IP6STAT_INC(ip6s_cantforward);
910 ip6_forward(m, srcrt);
914 ip6 = mtod(m, struct ip6_hdr *);
917 * Malicious party may be able to use IPv4 mapped addr to confuse
918 * tcp/udp stack and bypass security checks (act as if it was from
919 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
921 * For SIIT end node behavior, you may want to disable the check.
922 * However, you will become vulnerable to attacks using IPv4 mapped
925 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
926 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
927 IP6STAT_INC(ip6s_badscope);
928 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
933 * Tell launch routine the next header
935 IP6STAT_INC(ip6s_delivered);
936 in6_ifstat_inc(rcvif, ifs6_in_deliver);
939 while (nxt != IPPROTO_DONE) {
940 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
941 IP6STAT_INC(ip6s_toomanyhdr);
946 * protection against faulty packet - there should be
947 * more sanity checks in header chain processing.
949 if (m->m_pkthdr.len < off) {
950 IP6STAT_INC(ip6s_tooshort);
951 in6_ifstat_inc(rcvif, ifs6_in_truncated);
955 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
956 if (IPSEC_ENABLED(ipv6)) {
957 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
962 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
966 in6_ifstat_inc(rcvif, ifs6_in_discard);
972 * Hop-by-Hop options header processing. If a valid jumbo payload option is
973 * included, the real payload length will be stored in plenp.
975 * rtalertp - XXX: should be stored more smart way
978 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
979 struct mbuf **mp, int *offp)
981 struct mbuf *m = *mp;
982 int off = *offp, hbhlen;
985 /* validation of the length of the header */
986 #ifndef PULLDOWN_TEST
987 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
988 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
989 hbhlen = (hbh->ip6h_len + 1) << 3;
991 IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
992 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
994 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
995 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
997 IP6STAT_INC(ip6s_tooshort);
1000 hbhlen = (hbh->ip6h_len + 1) << 3;
1001 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
1004 IP6STAT_INC(ip6s_tooshort);
1009 hbhlen -= sizeof(struct ip6_hbh);
1010 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
1011 hbhlen, rtalertp, plenp) < 0)
1020 * Search header for all Hop-by-hop options and process each option.
1021 * This function is separate from ip6_hopopts_input() in order to
1022 * handle a case where the sending node itself process its hop-by-hop
1023 * options header. In such a case, the function is called from ip6_output().
1025 * The function assumes that hbh header is located right after the IPv6 header
1026 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1027 * opthead + hbhlen is located in contiguous memory region.
1030 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
1031 u_int32_t *rtalertp, u_int32_t *plenp)
1033 struct ip6_hdr *ip6;
1035 u_int8_t *opt = opthead;
1036 u_int16_t rtalert_val;
1037 u_int32_t jumboplen;
1038 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1040 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1046 if (hbhlen < IP6OPT_MINLEN) {
1047 IP6STAT_INC(ip6s_toosmall);
1050 optlen = *(opt + 1) + 2;
1052 case IP6OPT_ROUTER_ALERT:
1053 /* XXX may need check for alignment */
1054 if (hbhlen < IP6OPT_RTALERT_LEN) {
1055 IP6STAT_INC(ip6s_toosmall);
1058 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1060 icmp6_error(m, ICMP6_PARAM_PROB,
1061 ICMP6_PARAMPROB_HEADER,
1062 erroff + opt + 1 - opthead);
1065 optlen = IP6OPT_RTALERT_LEN;
1066 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1067 *rtalertp = ntohs(rtalert_val);
1070 /* XXX may need check for alignment */
1071 if (hbhlen < IP6OPT_JUMBO_LEN) {
1072 IP6STAT_INC(ip6s_toosmall);
1075 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1077 icmp6_error(m, ICMP6_PARAM_PROB,
1078 ICMP6_PARAMPROB_HEADER,
1079 erroff + opt + 1 - opthead);
1082 optlen = IP6OPT_JUMBO_LEN;
1085 * IPv6 packets that have non 0 payload length
1086 * must not contain a jumbo payload option.
1088 ip6 = mtod(m, struct ip6_hdr *);
1089 if (ip6->ip6_plen) {
1090 IP6STAT_INC(ip6s_badoptions);
1091 icmp6_error(m, ICMP6_PARAM_PROB,
1092 ICMP6_PARAMPROB_HEADER,
1093 erroff + opt - opthead);
1098 * We may see jumbolen in unaligned location, so
1099 * we'd need to perform bcopy().
1101 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1102 jumboplen = (u_int32_t)htonl(jumboplen);
1106 * if there are multiple jumbo payload options,
1107 * *plenp will be non-zero and the packet will be
1109 * the behavior may need some debate in ipngwg -
1110 * multiple options does not make sense, however,
1111 * there's no explicit mention in specification.
1114 IP6STAT_INC(ip6s_badoptions);
1115 icmp6_error(m, ICMP6_PARAM_PROB,
1116 ICMP6_PARAMPROB_HEADER,
1117 erroff + opt + 2 - opthead);
1123 * jumbo payload length must be larger than 65535.
1125 if (jumboplen <= IPV6_MAXPACKET) {
1126 IP6STAT_INC(ip6s_badoptions);
1127 icmp6_error(m, ICMP6_PARAM_PROB,
1128 ICMP6_PARAMPROB_HEADER,
1129 erroff + opt + 2 - opthead);
1135 default: /* unknown option */
1136 if (hbhlen < IP6OPT_MINLEN) {
1137 IP6STAT_INC(ip6s_toosmall);
1140 optlen = ip6_unknown_opt(opt, m,
1141 erroff + opt - opthead);
1157 * Unknown option processing.
1158 * The third argument `off' is the offset from the IPv6 header to the option,
1159 * which is necessary if the IPv6 header the and option header and IPv6 header
1160 * is not contiguous in order to return an ICMPv6 error.
1163 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1165 struct ip6_hdr *ip6;
1167 switch (IP6OPT_TYPE(*optp)) {
1168 case IP6OPT_TYPE_SKIP: /* ignore the option */
1169 return ((int)*(optp + 1));
1170 case IP6OPT_TYPE_DISCARD: /* silently discard */
1173 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1174 IP6STAT_INC(ip6s_badoptions);
1175 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1177 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1178 IP6STAT_INC(ip6s_badoptions);
1179 ip6 = mtod(m, struct ip6_hdr *);
1180 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1181 (m->m_flags & (M_BCAST|M_MCAST)))
1184 icmp6_error(m, ICMP6_PARAM_PROB,
1185 ICMP6_PARAMPROB_OPTION, off);
1189 m_freem(m); /* XXX: NOTREACHED */
1194 * Create the "control" list for this pcb.
1195 * These functions will not modify mbuf chain at all.
1197 * With KAME mbuf chain restriction:
1198 * The routine will be called from upper layer handlers like tcp6_input().
1199 * Thus the routine assumes that the caller (tcp6_input) have already
1200 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1201 * very first mbuf on the mbuf chain.
1203 * ip6_savecontrol_v4 will handle those options that are possible to be
1204 * set on a v4-mapped socket.
1205 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1206 * options and handle the v6-only ones itself.
1209 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1212 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1215 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1222 switch (inp->inp_socket->so_ts_clock) {
1223 case SO_TS_REALTIME_MICRO:
1225 *mp = sbcreatecontrol((caddr_t) &t.tv, sizeof(t.tv),
1226 SCM_TIMESTAMP, SOL_SOCKET);
1228 mp = &(*mp)->m_next;
1233 *mp = sbcreatecontrol((caddr_t)&t.bt, sizeof(t.bt),
1234 SCM_BINTIME, SOL_SOCKET);
1236 mp = &(*mp)->m_next;
1239 case SO_TS_REALTIME:
1241 *mp = sbcreatecontrol((caddr_t)&t.ts, sizeof(t.ts),
1242 SCM_REALTIME, SOL_SOCKET);
1244 mp = &(*mp)->m_next;
1247 case SO_TS_MONOTONIC:
1249 *mp = sbcreatecontrol((caddr_t)&t.ts, sizeof(t.ts),
1250 SCM_MONOTONIC, SOL_SOCKET);
1252 mp = &(*mp)->m_next;
1256 panic("unknown (corrupted) so_ts_clock");
1261 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1262 /* RFC 2292 sec. 5 */
1263 if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1264 struct in6_pktinfo pi6;
1266 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1270 ip = mtod(m, struct ip *);
1271 pi6.ipi6_addr.s6_addr32[0] = 0;
1272 pi6.ipi6_addr.s6_addr32[1] = 0;
1273 pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1274 pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1276 /* We won't hit this code */
1277 bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1280 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1281 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1284 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1286 *mp = sbcreatecontrol((caddr_t) &pi6,
1287 sizeof(struct in6_pktinfo),
1288 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1290 mp = &(*mp)->m_next;
1293 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1296 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1300 ip = mtod(m, struct ip *);
1303 /* We won't hit this code */
1307 hlim = ip6->ip6_hlim & 0xff;
1309 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1310 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1313 mp = &(*mp)->m_next;
1316 if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1319 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1323 ip = mtod(m, struct ip *);
1324 tclass = ip->ip_tos;
1326 /* We won't hit this code */
1332 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1334 tclass = flowinfo & 0xff;
1336 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int),
1337 IPV6_TCLASS, IPPROTO_IPV6);
1339 mp = &(*mp)->m_next;
1342 if (v4only != NULL) {
1343 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1354 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1356 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1359 mp = ip6_savecontrol_v4(in6p, m, mp, &v4only);
1364 * IPV6_HOPOPTS socket option. Recall that we required super-user
1365 * privilege for the option (see ip6_ctloutput), but it might be too
1366 * strict, since there might be some hop-by-hop options which can be
1367 * returned to normal user.
1368 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1370 if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
1372 * Check if a hop-by-hop options header is contatined in the
1373 * received packet, and if so, store the options as ancillary
1374 * data. Note that a hop-by-hop options header must be
1375 * just after the IPv6 header, which is assured through the
1376 * IPv6 input processing.
1378 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1379 struct ip6_hbh *hbh;
1381 #ifdef PULLDOWN_TEST
1385 #ifndef PULLDOWN_TEST
1386 hbh = (struct ip6_hbh *)(ip6 + 1);
1387 hbhlen = (hbh->ip6h_len + 1) << 3;
1389 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1392 IP6STAT_INC(ip6s_tooshort);
1395 hbh = mtod(ext, struct ip6_hbh *);
1396 hbhlen = (hbh->ip6h_len + 1) << 3;
1397 if (hbhlen != ext->m_len) {
1399 IP6STAT_INC(ip6s_tooshort);
1405 * XXX: We copy the whole header even if a
1406 * jumbo payload option is included, the option which
1407 * is to be removed before returning according to
1409 * Note: this constraint is removed in RFC3542
1411 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1412 IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1415 mp = &(*mp)->m_next;
1416 #ifdef PULLDOWN_TEST
1422 if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1423 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1426 * Search for destination options headers or routing
1427 * header(s) through the header chain, and stores each
1428 * header as ancillary data.
1429 * Note that the order of the headers remains in
1430 * the chain of ancillary data.
1432 while (1) { /* is explicit loop prevention necessary? */
1433 struct ip6_ext *ip6e = NULL;
1435 #ifdef PULLDOWN_TEST
1436 struct mbuf *ext = NULL;
1440 * if it is not an extension header, don't try to
1441 * pull it from the chain.
1444 case IPPROTO_DSTOPTS:
1445 case IPPROTO_ROUTING:
1446 case IPPROTO_HOPOPTS:
1447 case IPPROTO_AH: /* is it possible? */
1453 #ifndef PULLDOWN_TEST
1454 if (off + sizeof(*ip6e) > m->m_len)
1456 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1457 if (nxt == IPPROTO_AH)
1458 elen = (ip6e->ip6e_len + 2) << 2;
1460 elen = (ip6e->ip6e_len + 1) << 3;
1461 if (off + elen > m->m_len)
1464 ext = ip6_pullexthdr(m, off, nxt);
1466 IP6STAT_INC(ip6s_tooshort);
1469 ip6e = mtod(ext, struct ip6_ext *);
1470 if (nxt == IPPROTO_AH)
1471 elen = (ip6e->ip6e_len + 2) << 2;
1473 elen = (ip6e->ip6e_len + 1) << 3;
1474 if (elen != ext->m_len) {
1476 IP6STAT_INC(ip6s_tooshort);
1482 case IPPROTO_DSTOPTS:
1483 if (!(in6p->inp_flags & IN6P_DSTOPTS))
1486 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1488 IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1491 mp = &(*mp)->m_next;
1493 case IPPROTO_ROUTING:
1494 if (!(in6p->inp_flags & IN6P_RTHDR))
1497 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1498 IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
1501 mp = &(*mp)->m_next;
1503 case IPPROTO_HOPOPTS:
1504 case IPPROTO_AH: /* is it possible? */
1509 * other cases have been filtered in the above.
1510 * none will visit this case. here we supply
1511 * the code just in case (nxt overwritten or
1514 #ifdef PULLDOWN_TEST
1521 /* proceed with the next header. */
1523 nxt = ip6e->ip6e_nxt;
1525 #ifdef PULLDOWN_TEST
1534 if (in6p->inp_flags2 & INP_RECVFLOWID) {
1535 uint32_t flowid, flow_type;
1537 flowid = m->m_pkthdr.flowid;
1538 flow_type = M_HASHTYPE_GET(m);
1541 * XXX should handle the failure of one or the
1542 * other - don't populate both?
1544 *mp = sbcreatecontrol((caddr_t) &flowid,
1545 sizeof(uint32_t), IPV6_FLOWID, IPPROTO_IPV6);
1547 mp = &(*mp)->m_next;
1548 *mp = sbcreatecontrol((caddr_t) &flow_type,
1549 sizeof(uint32_t), IPV6_FLOWTYPE, IPPROTO_IPV6);
1551 mp = &(*mp)->m_next;
1555 if (in6p->inp_flags2 & INP_RECVRSSBUCKETID) {
1556 uint32_t flowid, flow_type;
1557 uint32_t rss_bucketid;
1559 flowid = m->m_pkthdr.flowid;
1560 flow_type = M_HASHTYPE_GET(m);
1562 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1563 *mp = sbcreatecontrol((caddr_t) &rss_bucketid,
1564 sizeof(uint32_t), IPV6_RSSBUCKETID, IPPROTO_IPV6);
1566 mp = &(*mp)->m_next;
1575 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu)
1579 struct ip6_mtuinfo mtuctl;
1581 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1583 * Notify the error by sending IPV6_PATHMTU ancillary data if
1584 * application wanted to know the MTU value.
1585 * NOTE: we notify disconnected sockets, because some udp
1586 * applications keep sending sockets disconnected.
1587 * NOTE: our implementation doesn't notify connected sockets that has
1588 * foreign address that is different than given destination addresses
1589 * (this is permitted by RFC 3542).
1591 if ((inp->inp_flags & IN6P_MTU) == 0 || (
1592 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1593 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr)))
1596 mtuctl.ip6m_mtu = mtu;
1597 mtuctl.ip6m_addr = *dst;
1598 if (sa6_recoverscope(&mtuctl.ip6m_addr))
1601 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1602 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1605 so = inp->inp_socket;
1606 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1609 /* XXX: should count statistics */
1614 #ifdef PULLDOWN_TEST
1616 * pull single extension header from mbuf chain. returns single mbuf that
1617 * contains the result, or NULL on error.
1619 static struct mbuf *
1620 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1622 struct ip6_ext ip6e;
1628 case IPPROTO_DSTOPTS:
1629 case IPPROTO_ROUTING:
1630 case IPPROTO_HOPOPTS:
1631 case IPPROTO_AH: /* is it possible? */
1634 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1638 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1639 if (nxt == IPPROTO_AH)
1640 elen = (ip6e.ip6e_len + 2) << 2;
1642 elen = (ip6e.ip6e_len + 1) << 3;
1645 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1647 n = m_get(M_NOWAIT, MT_DATA);
1651 m_copydata(m, off, elen, mtod(n, caddr_t));
1658 * Get pointer to the previous header followed by the header
1659 * currently processed.
1662 ip6_get_prevhdr(const struct mbuf *m, int off)
1664 struct ip6_ext ip6e;
1665 struct ip6_hdr *ip6;
1668 if (off == sizeof(struct ip6_hdr))
1669 return (offsetof(struct ip6_hdr, ip6_nxt));
1670 if (off < sizeof(struct ip6_hdr))
1671 panic("%s: off < sizeof(struct ip6_hdr)", __func__);
1673 ip6 = mtod(m, struct ip6_hdr *);
1675 len = sizeof(struct ip6_hdr);
1678 m_copydata(m, len, sizeof(ip6e), (caddr_t)&ip6e);
1680 case IPPROTO_FRAGMENT:
1681 nlen = sizeof(struct ip6_frag);
1684 nlen = (ip6e.ip6e_len + 2) << 2;
1687 nlen = (ip6e.ip6e_len + 1) << 3;
1690 nxt = ip6e.ip6e_nxt;
1692 return (len - nlen);
1696 * get next header offset. m will be retained.
1699 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1702 struct ip6_ext ip6e;
1707 panic("ip6_nexthdr: m == NULL");
1708 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1713 if (m->m_pkthdr.len < off + sizeof(ip6))
1715 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1717 *nxtp = ip6.ip6_nxt;
1721 case IPPROTO_FRAGMENT:
1723 * terminate parsing if it is not the first fragment,
1724 * it does not make sense to parse through it.
1726 if (m->m_pkthdr.len < off + sizeof(fh))
1728 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1729 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1730 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1733 *nxtp = fh.ip6f_nxt;
1734 off += sizeof(struct ip6_frag);
1738 if (m->m_pkthdr.len < off + sizeof(ip6e))
1740 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1742 *nxtp = ip6e.ip6e_nxt;
1743 off += (ip6e.ip6e_len + 2) << 2;
1746 case IPPROTO_HOPOPTS:
1747 case IPPROTO_ROUTING:
1748 case IPPROTO_DSTOPTS:
1749 if (m->m_pkthdr.len < off + sizeof(ip6e))
1751 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1753 *nxtp = ip6e.ip6e_nxt;
1754 off += (ip6e.ip6e_len + 1) << 3;
1759 case IPPROTO_IPCOMP:
1771 * get offset for the last header in the chain. m will be kept untainted.
1774 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1784 newoff = ip6_nexthdr(m, off, proto, nxtp);
1787 else if (newoff < off)
1788 return -1; /* invalid */
1789 else if (newoff == off)
1798 * System control for IP6
1801 u_char inet6ctlerrmap[PRC_NCMDS] = {
1803 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1804 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1805 EMSGSIZE, EHOSTUNREACH, 0, 0,
1806 0, 0, EHOSTUNREACH, 0,
1807 ENOPROTOOPT, ECONNREFUSED