2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
69 #include "opt_inet6.h"
70 #include "opt_ipsec.h"
71 #include "opt_route.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/hhook.h>
77 #include <sys/malloc.h>
80 #include <sys/domain.h>
81 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/errno.h>
87 #include <sys/kernel.h>
89 #include <sys/rmlock.h>
90 #include <sys/syslog.h>
91 #include <sys/sysctl.h>
94 #include <net/if_var.h>
95 #include <net/if_types.h>
96 #include <net/if_dl.h>
97 #include <net/route.h>
98 #include <net/netisr.h>
99 #include <net/rss_config.h>
100 #include <net/pfil.h>
101 #include <net/vnet.h>
103 #include <netinet/in.h>
104 #include <netinet/in_kdtrace.h>
105 #include <netinet/ip_var.h>
106 #include <netinet/in_systm.h>
107 #include <net/if_llatbl.h>
109 #include <netinet/ip.h>
110 #include <netinet/ip_icmp.h>
112 #include <netinet/ip6.h>
113 #include <netinet6/in6_var.h>
114 #include <netinet6/ip6_var.h>
115 #include <netinet/in_pcb.h>
116 #include <netinet/icmp6.h>
117 #include <netinet6/scope6_var.h>
118 #include <netinet6/in6_ifattach.h>
119 #include <netinet6/mld6_var.h>
120 #include <netinet6/nd6.h>
121 #include <netinet6/in6_rss.h>
123 #include <netipsec/ipsec_support.h>
125 #include <netinet6/ip6protosw.h>
127 extern struct domain inet6domain;
129 u_char ip6_protox[IPPROTO_MAX];
130 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
131 VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
132 VNET_DEFINE(u_long, in6_ifaddrhmask);
134 static struct netisr_handler ip6_nh = {
136 .nh_handler = ip6_input,
137 .nh_proto = NETISR_IPV6,
139 .nh_m2cpuid = rss_soft_m2cpuid_v6,
140 .nh_policy = NETISR_POLICY_CPU,
141 .nh_dispatch = NETISR_DISPATCH_HYBRID,
143 .nh_policy = NETISR_POLICY_FLOW,
148 sysctl_netinet6_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
152 netisr_getqlimit(&ip6_nh, &qlimit);
153 error = sysctl_handle_int(oidp, &qlimit, 0, req);
154 if (error || !req->newptr)
158 return (netisr_setqlimit(&ip6_nh, qlimit));
160 SYSCTL_DECL(_net_inet6_ip6);
161 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRQMAXLEN, intr_queue_maxlen,
162 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
163 0, 0, sysctl_netinet6_intr_queue_maxlen, "I",
164 "Maximum size of the IPv6 input queue");
166 VNET_DEFINE_STATIC(bool, ip6_sav) = true;
167 #define V_ip6_sav VNET(ip6_sav)
168 SYSCTL_BOOL(_net_inet6_ip6, OID_AUTO, source_address_validation,
169 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_sav), true,
170 "Drop incoming packets with source address that is a local address");
173 static struct netisr_handler ip6_direct_nh = {
174 .nh_name = "ip6_direct",
175 .nh_handler = ip6_direct_input,
176 .nh_proto = NETISR_IPV6_DIRECT,
177 .nh_m2cpuid = rss_soft_m2cpuid_v6,
178 .nh_policy = NETISR_POLICY_CPU,
179 .nh_dispatch = NETISR_DISPATCH_HYBRID,
183 sysctl_netinet6_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
187 netisr_getqlimit(&ip6_direct_nh, &qlimit);
188 error = sysctl_handle_int(oidp, &qlimit, 0, req);
189 if (error || !req->newptr)
193 return (netisr_setqlimit(&ip6_direct_nh, qlimit));
195 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
196 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
197 0, 0, sysctl_netinet6_intr_direct_queue_maxlen, "I",
198 "Maximum size of the IPv6 direct input queue");
202 VNET_DEFINE(pfil_head_t, inet6_pfil_head);
204 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
205 VNET_PCPUSTAT_SYSINIT(ip6stat);
207 VNET_PCPUSTAT_SYSUNINIT(ip6stat);
210 struct rmlock in6_ifaddr_lock;
211 RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
213 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
216 * IP6 initialization: fill in IP6 protocol switch table.
217 * All protocols not implemented in kernel go to raw IP6 protocol handler.
220 ip6_vnet_init(void *arg __unused)
222 struct pfil_head_args args;
224 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
225 &V_ip6_auto_linklocal);
226 TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
227 TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
229 CK_STAILQ_INIT(&V_in6_ifaddrhead);
230 V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
233 /* Initialize packet filter hooks. */
234 args.pa_version = PFIL_VERSION;
235 args.pa_flags = PFIL_IN | PFIL_OUT;
236 args.pa_type = PFIL_TYPE_IP6;
237 args.pa_headname = PFIL_INET6_NAME;
238 V_inet6_pfil_head = pfil_head_register(&args);
240 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6,
241 &V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
242 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
243 printf("%s: WARNING: unable to register input helper hook\n",
245 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6,
246 &V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
247 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
248 printf("%s: WARNING: unable to register output helper hook\n",
252 addrsel_policy_init();
256 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
258 /* Skip global initialization stuff for non-default instances. */
260 netisr_register_vnet(&ip6_nh);
262 netisr_register_vnet(&ip6_direct_nh);
266 VNET_SYSINIT(ip6_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
267 ip6_vnet_init, NULL);
270 ip6_init(void *arg __unused)
274 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
275 KASSERT(pr, ("%s: PF_INET6 not found", __func__));
277 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
278 for (int i = 0; i < IPPROTO_MAX; i++)
279 ip6_protox[i] = pr - inet6sw;
281 * Cycle through IP protocols and put them into the appropriate place
284 for (pr = inet6domain.dom_protosw;
285 pr < inet6domain.dom_protoswNPROTOSW; pr++)
286 if (pr->pr_domain->dom_family == PF_INET6 &&
287 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
288 /* Be careful to only index valid IP protocols. */
289 if (pr->pr_protocol < IPPROTO_MAX)
290 ip6_protox[pr->pr_protocol] = pr - inet6sw;
293 netisr_register(&ip6_nh);
295 netisr_register(&ip6_direct_nh);
298 SYSINIT(ip6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_init, NULL);
301 * The protocol to be inserted into ip6_protox[] must be already registered
302 * in inet6sw[], either statically or through pf_proto_register().
305 ip6proto_register(short ip6proto)
310 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
311 return (EPROTONOSUPPORT);
314 * The protocol slot must not be occupied by another protocol
315 * already. An index pointing to IPPROTO_RAW is unused.
317 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
319 return (EPFNOSUPPORT);
320 if (ip6_protox[ip6proto] != pr - inet6sw) /* IPPROTO_RAW */
324 * Find the protocol position in inet6sw[] and set the index.
326 for (pr = inet6domain.dom_protosw;
327 pr < inet6domain.dom_protoswNPROTOSW; pr++) {
328 if (pr->pr_domain->dom_family == PF_INET6 &&
329 pr->pr_protocol && pr->pr_protocol == ip6proto) {
330 ip6_protox[pr->pr_protocol] = pr - inet6sw;
334 return (EPROTONOSUPPORT);
338 ip6proto_unregister(short ip6proto)
343 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
344 return (EPROTONOSUPPORT);
346 /* Check if the protocol was indeed registered. */
347 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
349 return (EPFNOSUPPORT);
350 if (ip6_protox[ip6proto] == pr - inet6sw) /* IPPROTO_RAW */
353 /* Reset the protocol slot to IPPROTO_RAW. */
354 ip6_protox[ip6proto] = pr - inet6sw;
360 ip6_destroy(void *unused __unused)
362 struct ifaddr *ifa, *nifa;
367 netisr_unregister_vnet(&ip6_direct_nh);
369 netisr_unregister_vnet(&ip6_nh);
371 pfil_head_unregister(V_inet6_pfil_head);
372 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]);
374 printf("%s: WARNING: unable to deregister input helper hook "
375 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: "
376 "error %d returned\n", __func__, error);
378 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]);
380 printf("%s: WARNING: unable to deregister output helper hook "
381 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: "
382 "error %d returned\n", __func__, error);
385 /* Cleanup addresses. */
387 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
388 /* Cannot lock here - lock recursion. */
389 /* IF_ADDR_LOCK(ifp); */
390 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
391 if (ifa->ifa_addr->sa_family != AF_INET6)
395 /* IF_ADDR_UNLOCK(ifp); */
396 in6_ifdetach_destroy(ifp);
397 mld_domifdetach(ifp);
401 /* Make sure any routes are gone as well. */
402 rib_flush_routes_family(AF_INET6);
406 in6_ifattach_destroy();
408 hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
411 VNET_SYSUNINIT(inet6, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_destroy, NULL);
415 ip6_input_hbh(struct mbuf **mp, uint32_t *plen, uint32_t *rtalert, int *off,
422 if (ip6_hopopts_input(plen, rtalert, mp, off)) {
423 #if 0 /*touches NULL pointer*/
424 in6_ifstat_inc((*mp)->m_pkthdr.rcvif, ifs6_in_discard);
426 goto out; /* m have already been freed */
431 ip6 = mtod(m, struct ip6_hdr *);
434 * if the payload length field is 0 and the next header field
435 * indicates Hop-by-Hop Options header, then a Jumbo Payload
436 * option MUST be included.
438 if (ip6->ip6_plen == 0 && *plen == 0) {
440 * Note that if a valid jumbo payload option is
441 * contained, ip6_hopopts_input() must set a valid
442 * (non-zero) payload length to the variable plen.
444 IP6STAT_INC(ip6s_badoptions);
445 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
446 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
447 icmp6_error(m, ICMP6_PARAM_PROB,
448 ICMP6_PARAMPROB_HEADER,
449 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
452 /* ip6_hopopts_input() ensures that mbuf is contiguous */
453 hbh = (struct ip6_hbh *)(ip6 + 1);
454 *nxt = hbh->ip6h_nxt;
457 * If we are acting as a router and the packet contains a
458 * router alert option, see if we know the option value.
459 * Currently, we only support the option value for MLD, in which
460 * case we should pass the packet to the multicast routing
463 if (*rtalert != ~0) {
465 case IP6OPT_RTALERT_MLD:
466 if (V_ip6_forwarding)
471 * RFC2711 requires unrecognized values must be
486 * IPv6 direct input routine.
488 * This is called when reinjecting completed fragments where
489 * all of the previous checking and book-keeping has been done.
492 ip6_direct_input(struct mbuf *m)
497 struct ip6_direct_ctx *ip6dc;
499 mtag = m_tag_locate(m, MTAG_ABI_IPV6, IPV6_TAG_DIRECT, NULL);
500 KASSERT(mtag != NULL, ("Reinjected packet w/o direct ctx tag!"));
502 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
503 nxt = ip6dc->ip6dc_nxt;
504 off = ip6dc->ip6dc_off;
508 m_tag_delete(m, mtag);
510 while (nxt != IPPROTO_DONE) {
511 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
512 IP6STAT_INC(ip6s_toomanyhdr);
517 * protection against faulty packet - there should be
518 * more sanity checks in header chain processing.
520 if (m->m_pkthdr.len < off) {
521 IP6STAT_INC(ip6s_tooshort);
522 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
526 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
527 if (IPSEC_ENABLED(ipv6)) {
528 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
533 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
542 ip6_input(struct mbuf *m)
544 struct in6_addr odst;
546 struct in6_ifaddr *ia;
549 u_int32_t rtalert = ~0;
550 int off = sizeof(struct ip6_hdr), nest;
555 * Drop the packet if IPv6 operation is disabled on the interface.
557 rcvif = m->m_pkthdr.rcvif;
558 if ((ND_IFINFO(rcvif)->flags & ND6_IFF_IFDISABLED))
561 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
563 * should the inner packet be considered authentic?
564 * see comment in ah4_input().
565 * NB: m cannot be NULL when passed to the input routine
568 m->m_flags &= ~M_AUTHIPHDR;
569 m->m_flags &= ~M_AUTHIPDGM;
573 if (m->m_flags & M_FASTFWD_OURS) {
575 * Firewall changed destination to local.
577 ip6 = mtod(m, struct ip6_hdr *);
584 if (m->m_flags & M_EXT) {
586 IP6STAT_INC(ip6s_mext2m);
588 IP6STAT_INC(ip6s_mext1);
591 if (m->m_flags & M_LOOP) {
592 IP6STAT_INC(ip6s_m2m[V_loif->if_index]);
593 } else if (rcvif->if_index < IP6S_M2MMAX)
594 IP6STAT_INC(ip6s_m2m[rcvif->if_index]);
596 IP6STAT_INC(ip6s_m2m[0]);
598 IP6STAT_INC(ip6s_m1);
601 in6_ifstat_inc(rcvif, ifs6_in_receive);
602 IP6STAT_INC(ip6s_total);
605 * L2 bridge code and some other code can return mbuf chain
606 * that does not conform to KAME requirement. too bad.
607 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
609 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
612 if (m->m_pkthdr.len > MHLEN)
613 n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
615 n = m_gethdr(M_NOWAIT, MT_DATA);
620 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
621 n->m_len = n->m_pkthdr.len;
625 if (m->m_len < sizeof(struct ip6_hdr)) {
626 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
627 IP6STAT_INC(ip6s_toosmall);
628 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
633 ip6 = mtod(m, struct ip6_hdr *);
634 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
635 IP6STAT_INC(ip6s_badvers);
636 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
640 IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
641 IP_PROBE(receive, NULL, NULL, ip6, rcvif, NULL, ip6);
644 * Check against address spoofing/corruption.
646 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
647 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
649 * XXX: "badscope" is not very suitable for a multicast source.
651 IP6STAT_INC(ip6s_badscope);
652 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
655 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
656 !(m->m_flags & M_LOOP)) {
658 * In this case, the packet should come from the loopback
659 * interface. However, we cannot just check the if_flags,
660 * because ip6_mloopback() passes the "actual" interface
661 * as the outgoing/incoming interface.
663 IP6STAT_INC(ip6s_badscope);
664 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
667 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
668 IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
671 * Nodes must not originate a packet to a multicast address
672 * whose scop field contains the reserved value 0; if such
673 * a packet is received, it must be silently dropped.
675 IP6STAT_INC(ip6s_badscope);
676 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
680 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
681 /* packet is dropped by traffic conditioner */
686 * The following check is not documented in specs. A malicious
687 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
688 * and bypass security checks (act as if it was from 127.0.0.1 by using
689 * IPv6 src ::ffff:127.0.0.1). Be cautious.
691 * We have supported IPv6-only kernels for a few years and this issue
692 * has not come up. The world seems to move mostly towards not using
693 * v4mapped on the wire, so it makes sense for us to keep rejecting
696 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
697 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
698 IP6STAT_INC(ip6s_badscope);
699 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
704 * Reject packets with IPv4 compatible addresses (auto tunnel).
706 * The code forbids auto tunnel relay case in RFC1933 (the check is
707 * stronger than RFC1933). We may want to re-enable it if mech-xx
708 * is revised to forbid relaying case.
710 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
711 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
712 IP6STAT_INC(ip6s_badscope);
713 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
718 * Try to forward the packet, but if we fail continue.
719 * ip6_tryforward() does not generate redirects, so fall
720 * through to normal processing if redirects are required.
721 * ip6_tryforward() does inbound and outbound packet firewall
722 * processing. If firewall has decided that destination becomes
723 * our local address, it sets M_FASTFWD_OURS flag. In this
724 * case skip another inbound firewall processing and update
727 if (V_ip6_forwarding != 0 && V_ip6_sendredirects == 0
728 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
729 && (!IPSEC_ENABLED(ipv6) ||
730 IPSEC_CAPS(ipv6, m, IPSEC_CAP_OPERABLE) == 0)
733 if ((m = ip6_tryforward(m)) == NULL)
735 if (m->m_flags & M_FASTFWD_OURS) {
736 ip6 = mtod(m, struct ip6_hdr *);
740 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
742 * Bypass packet filtering for packets previously handled by IPsec.
744 if (IPSEC_ENABLED(ipv6) &&
745 IPSEC_CAPS(ipv6, m, IPSEC_CAP_BYPASS_FILTER) != 0)
749 * Run through list of hooks for input packets.
751 * NB: Beware of the destination address changing
752 * (e.g. by NAT rewriting). When this happens,
753 * tell ip6_forward to do the right thing.
756 /* Jump over all PFIL processing if hooks are not active. */
757 if (!PFIL_HOOKED_IN(V_inet6_pfil_head))
761 if (pfil_run_hooks(V_inet6_pfil_head, &m, m->m_pkthdr.rcvif, PFIL_IN,
764 ip6 = mtod(m, struct ip6_hdr *);
765 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
766 if ((m->m_flags & (M_IP6_NEXTHOP | M_FASTFWD_OURS)) == M_IP6_NEXTHOP &&
767 m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
769 * Directly ship the packet on. This allows forwarding
770 * packets originally destined to us to some other directly
779 * Disambiguate address scope zones (if there is ambiguity).
780 * We first make sure that the original source or destination address
781 * is not in our internal form for scoped addresses. Such addresses
782 * are not necessarily invalid spec-wise, but we cannot accept them due
783 * to the usage conflict.
784 * in6_setscope() then also checks and rejects the cases where src or
785 * dst are the loopback address and the receiving interface
788 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
789 IP6STAT_INC(ip6s_badscope); /* XXX */
792 if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
793 in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
794 IP6STAT_INC(ip6s_badscope);
797 if (m->m_flags & M_FASTFWD_OURS) {
798 m->m_flags &= ~M_FASTFWD_OURS;
803 * Multicast check. Assume packet is for us to avoid
804 * prematurely taking locks.
806 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
808 in6_ifstat_inc(rcvif, ifs6_in_mcast);
813 * XXX: For now we keep link-local IPv6 addresses with embedded
814 * scope zone id, therefore we use zero zoneid here.
816 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
818 if (ia->ia6_flags & IN6_IFF_NOTREADY) {
819 char ip6bufs[INET6_ADDRSTRLEN];
820 char ip6bufd[INET6_ADDRSTRLEN];
821 /* address is not ready, so discard the packet. */
823 "ip6_input: packet to an unready address %s->%s\n",
824 ip6_sprintf(ip6bufs, &ip6->ip6_src),
825 ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
828 if (V_ip6_sav && !(rcvif->if_flags & IFF_LOOPBACK) &&
829 __predict_false(in6_localip_fib(&ip6->ip6_src,
831 IP6STAT_INC(ip6s_badscope); /* XXX */
834 /* Count the packet in the ip address stats */
835 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
836 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
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);
915 * Tell launch routine the next header
917 IP6STAT_INC(ip6s_delivered);
918 in6_ifstat_inc(rcvif, ifs6_in_deliver);
921 while (nxt != IPPROTO_DONE) {
922 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
923 IP6STAT_INC(ip6s_toomanyhdr);
928 * protection against faulty packet - there should be
929 * more sanity checks in header chain processing.
931 if (m->m_pkthdr.len < off) {
932 IP6STAT_INC(ip6s_tooshort);
933 in6_ifstat_inc(rcvif, ifs6_in_truncated);
937 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
938 if (IPSEC_ENABLED(ipv6)) {
939 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
944 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
948 in6_ifstat_inc(rcvif, ifs6_in_discard);
954 * Hop-by-Hop options header processing. If a valid jumbo payload option is
955 * included, the real payload length will be stored in plenp.
957 * rtalertp - XXX: should be stored more smart way
960 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
961 struct mbuf **mp, int *offp)
963 struct mbuf *m = *mp;
964 int off = *offp, hbhlen;
967 /* validation of the length of the header */
968 if (m->m_len < off + sizeof(*hbh)) {
969 m = m_pullup(m, off + sizeof(*hbh));
971 IP6STAT_INC(ip6s_exthdrtoolong);
976 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
977 hbhlen = (hbh->ip6h_len + 1) << 3;
979 if (m->m_len < off + hbhlen) {
980 m = m_pullup(m, off + hbhlen);
982 IP6STAT_INC(ip6s_exthdrtoolong);
987 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
989 hbhlen -= sizeof(struct ip6_hbh);
990 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
991 hbhlen, rtalertp, plenp) < 0) {
1002 * Search header for all Hop-by-hop options and process each option.
1003 * This function is separate from ip6_hopopts_input() in order to
1004 * handle a case where the sending node itself process its hop-by-hop
1005 * options header. In such a case, the function is called from ip6_output().
1007 * The function assumes that hbh header is located right after the IPv6 header
1008 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1009 * opthead + hbhlen is located in contiguous memory region.
1012 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
1013 u_int32_t *rtalertp, u_int32_t *plenp)
1015 struct ip6_hdr *ip6;
1017 u_int8_t *opt = opthead;
1018 u_int16_t rtalert_val;
1019 u_int32_t jumboplen;
1020 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1022 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1028 if (hbhlen < IP6OPT_MINLEN) {
1029 IP6STAT_INC(ip6s_toosmall);
1032 optlen = *(opt + 1) + 2;
1034 case IP6OPT_ROUTER_ALERT:
1035 /* XXX may need check for alignment */
1036 if (hbhlen < IP6OPT_RTALERT_LEN) {
1037 IP6STAT_INC(ip6s_toosmall);
1040 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1042 icmp6_error(m, ICMP6_PARAM_PROB,
1043 ICMP6_PARAMPROB_HEADER,
1044 erroff + opt + 1 - opthead);
1047 optlen = IP6OPT_RTALERT_LEN;
1048 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1049 *rtalertp = ntohs(rtalert_val);
1052 /* XXX may need check for alignment */
1053 if (hbhlen < IP6OPT_JUMBO_LEN) {
1054 IP6STAT_INC(ip6s_toosmall);
1057 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1059 icmp6_error(m, ICMP6_PARAM_PROB,
1060 ICMP6_PARAMPROB_HEADER,
1061 erroff + opt + 1 - opthead);
1064 optlen = IP6OPT_JUMBO_LEN;
1067 * IPv6 packets that have non 0 payload length
1068 * must not contain a jumbo payload option.
1070 ip6 = mtod(m, struct ip6_hdr *);
1071 if (ip6->ip6_plen) {
1072 IP6STAT_INC(ip6s_badoptions);
1073 icmp6_error(m, ICMP6_PARAM_PROB,
1074 ICMP6_PARAMPROB_HEADER,
1075 erroff + opt - opthead);
1080 * We may see jumbolen in unaligned location, so
1081 * we'd need to perform bcopy().
1083 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1084 jumboplen = (u_int32_t)htonl(jumboplen);
1088 * if there are multiple jumbo payload options,
1089 * *plenp will be non-zero and the packet will be
1091 * the behavior may need some debate in ipngwg -
1092 * multiple options does not make sense, however,
1093 * there's no explicit mention in specification.
1096 IP6STAT_INC(ip6s_badoptions);
1097 icmp6_error(m, ICMP6_PARAM_PROB,
1098 ICMP6_PARAMPROB_HEADER,
1099 erroff + opt + 2 - opthead);
1105 * jumbo payload length must be larger than 65535.
1107 if (jumboplen <= IPV6_MAXPACKET) {
1108 IP6STAT_INC(ip6s_badoptions);
1109 icmp6_error(m, ICMP6_PARAM_PROB,
1110 ICMP6_PARAMPROB_HEADER,
1111 erroff + opt + 2 - opthead);
1117 default: /* unknown option */
1118 if (hbhlen < IP6OPT_MINLEN) {
1119 IP6STAT_INC(ip6s_toosmall);
1122 optlen = ip6_unknown_opt(opt, m,
1123 erroff + opt - opthead);
1139 * Unknown option processing.
1140 * The third argument `off' is the offset from the IPv6 header to the option,
1141 * which is necessary if the IPv6 header the and option header and IPv6 header
1142 * is not contiguous in order to return an ICMPv6 error.
1145 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1147 struct ip6_hdr *ip6;
1149 switch (IP6OPT_TYPE(*optp)) {
1150 case IP6OPT_TYPE_SKIP: /* ignore the option */
1151 return ((int)*(optp + 1));
1152 case IP6OPT_TYPE_DISCARD: /* silently discard */
1155 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1156 IP6STAT_INC(ip6s_badoptions);
1157 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1159 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1160 IP6STAT_INC(ip6s_badoptions);
1161 ip6 = mtod(m, struct ip6_hdr *);
1162 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1163 (m->m_flags & (M_BCAST|M_MCAST)))
1166 icmp6_error(m, ICMP6_PARAM_PROB,
1167 ICMP6_PARAMPROB_OPTION, off);
1171 m_freem(m); /* XXX: NOTREACHED */
1176 * Create the "control" list for this pcb.
1177 * These functions will not modify mbuf chain at all.
1179 * The routine will be called from upper layer handlers like tcp6_input().
1180 * Thus the routine assumes that the caller (tcp6_input) have already
1181 * called m_pullup() and all the extension headers are located in the
1182 * very first mbuf on the mbuf chain.
1184 * ip6_savecontrol_v4 will handle those options that are possible to be
1185 * set on a v4-mapped socket.
1186 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1187 * options and handle the v6-only ones itself.
1190 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1193 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1196 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1202 struct bintime boottimebin, bt1;
1203 struct timespec ts1;
1207 switch (inp->inp_socket->so_ts_clock) {
1208 case SO_TS_REALTIME_MICRO:
1209 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1211 mbuf_tstmp2timespec(m, &ts1);
1212 timespec2bintime(&ts1, &bt1);
1213 getboottimebin(&boottimebin);
1214 bintime_add(&bt1, &boottimebin);
1215 bintime2timeval(&bt1, &t.tv);
1219 *mp = sbcreatecontrol((caddr_t) &t.tv, sizeof(t.tv),
1220 SCM_TIMESTAMP, SOL_SOCKET);
1222 mp = &(*mp)->m_next;
1228 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1230 mbuf_tstmp2timespec(m, &ts1);
1231 timespec2bintime(&ts1, &t.bt);
1232 getboottimebin(&boottimebin);
1233 bintime_add(&t.bt, &boottimebin);
1237 *mp = sbcreatecontrol((caddr_t)&t.bt, sizeof(t.bt),
1238 SCM_BINTIME, SOL_SOCKET);
1240 mp = &(*mp)->m_next;
1245 case SO_TS_REALTIME:
1246 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1248 mbuf_tstmp2timespec(m, &t.ts);
1249 getboottimebin(&boottimebin);
1250 bintime2timespec(&boottimebin, &ts1);
1251 timespecadd(&t.ts, &ts1, &t.ts);
1255 *mp = sbcreatecontrol((caddr_t)&t.ts, sizeof(t.ts),
1256 SCM_REALTIME, SOL_SOCKET);
1258 mp = &(*mp)->m_next;
1263 case SO_TS_MONOTONIC:
1264 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1266 mbuf_tstmp2timespec(m, &t.ts);
1269 *mp = sbcreatecontrol((caddr_t)&t.ts, sizeof(t.ts),
1270 SCM_MONOTONIC, SOL_SOCKET);
1272 mp = &(*mp)->m_next;
1278 panic("unknown (corrupted) so_ts_clock");
1280 if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) ==
1281 (M_PKTHDR | M_TSTMP)) {
1282 struct sock_timestamp_info sti;
1284 bzero(&sti, sizeof(sti));
1285 sti.st_info_flags = ST_INFO_HW;
1286 if ((m->m_flags & M_TSTMP_HPREC) != 0)
1287 sti.st_info_flags |= ST_INFO_HW_HPREC;
1288 *mp = sbcreatecontrol((caddr_t)&sti, sizeof(sti),
1289 SCM_TIME_INFO, SOL_SOCKET);
1291 mp = &(*mp)->m_next;
1296 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1297 /* RFC 2292 sec. 5 */
1298 if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1299 struct in6_pktinfo pi6;
1301 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1305 ip = mtod(m, struct ip *);
1306 pi6.ipi6_addr.s6_addr32[0] = 0;
1307 pi6.ipi6_addr.s6_addr32[1] = 0;
1308 pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1309 pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1311 /* We won't hit this code */
1312 bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1315 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1316 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1319 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1321 *mp = sbcreatecontrol((caddr_t) &pi6,
1322 sizeof(struct in6_pktinfo),
1323 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1325 mp = &(*mp)->m_next;
1328 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1331 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1335 ip = mtod(m, struct ip *);
1338 /* We won't hit this code */
1342 hlim = ip6->ip6_hlim & 0xff;
1344 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1345 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1348 mp = &(*mp)->m_next;
1351 if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1354 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1358 ip = mtod(m, struct ip *);
1359 tclass = ip->ip_tos;
1361 /* We won't hit this code */
1367 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1369 tclass = flowinfo & 0xff;
1371 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int),
1372 IPV6_TCLASS, IPPROTO_IPV6);
1374 mp = &(*mp)->m_next;
1377 if (v4only != NULL) {
1378 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1389 ip6_savecontrol(struct inpcb *inp, struct mbuf *m, struct mbuf **mp)
1391 struct ip6_hdr *ip6;
1394 mp = ip6_savecontrol_v4(inp, m, mp, &v4only);
1398 ip6 = mtod(m, struct ip6_hdr *);
1400 * IPV6_HOPOPTS socket option. Recall that we required super-user
1401 * privilege for the option (see ip6_ctloutput), but it might be too
1402 * strict, since there might be some hop-by-hop options which can be
1403 * returned to normal user.
1404 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1406 if ((inp->inp_flags & IN6P_HOPOPTS) != 0) {
1408 * Check if a hop-by-hop options header is contatined in the
1409 * received packet, and if so, store the options as ancillary
1410 * data. Note that a hop-by-hop options header must be
1411 * just after the IPv6 header, which is assured through the
1412 * IPv6 input processing.
1414 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1415 struct ip6_hbh *hbh;
1418 hbh = (struct ip6_hbh *)(ip6 + 1);
1419 hbhlen = (hbh->ip6h_len + 1) << 3;
1422 * XXX: We copy the whole header even if a
1423 * jumbo payload option is included, the option which
1424 * is to be removed before returning according to
1426 * Note: this constraint is removed in RFC3542
1428 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1429 IS2292(inp, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1432 mp = &(*mp)->m_next;
1436 if ((inp->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1437 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1440 * Search for destination options headers or routing
1441 * header(s) through the header chain, and stores each
1442 * header as ancillary data.
1443 * Note that the order of the headers remains in
1444 * the chain of ancillary data.
1446 while (1) { /* is explicit loop prevention necessary? */
1447 struct ip6_ext *ip6e = NULL;
1451 * if it is not an extension header, don't try to
1452 * pull it from the chain.
1455 case IPPROTO_DSTOPTS:
1456 case IPPROTO_ROUTING:
1457 case IPPROTO_HOPOPTS:
1458 case IPPROTO_AH: /* is it possible? */
1464 if (off + sizeof(*ip6e) > m->m_len)
1466 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1467 if (nxt == IPPROTO_AH)
1468 elen = (ip6e->ip6e_len + 2) << 2;
1470 elen = (ip6e->ip6e_len + 1) << 3;
1471 if (off + elen > m->m_len)
1475 case IPPROTO_DSTOPTS:
1476 if (!(inp->inp_flags & IN6P_DSTOPTS))
1479 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1481 IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1484 mp = &(*mp)->m_next;
1486 case IPPROTO_ROUTING:
1487 if (!(inp->inp_flags & IN6P_RTHDR))
1490 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1491 IS2292(inp, IPV6_2292RTHDR, IPV6_RTHDR),
1494 mp = &(*mp)->m_next;
1496 case IPPROTO_HOPOPTS:
1497 case IPPROTO_AH: /* is it possible? */
1502 * other cases have been filtered in the above.
1503 * none will visit this case. here we supply
1504 * the code just in case (nxt overwritten or
1510 /* proceed with the next header. */
1512 nxt = ip6e->ip6e_nxt;
1519 if (inp->inp_flags2 & INP_RECVFLOWID) {
1520 uint32_t flowid, flow_type;
1522 flowid = m->m_pkthdr.flowid;
1523 flow_type = M_HASHTYPE_GET(m);
1526 * XXX should handle the failure of one or the
1527 * other - don't populate both?
1529 *mp = sbcreatecontrol((caddr_t) &flowid,
1530 sizeof(uint32_t), IPV6_FLOWID, IPPROTO_IPV6);
1532 mp = &(*mp)->m_next;
1533 *mp = sbcreatecontrol((caddr_t) &flow_type,
1534 sizeof(uint32_t), IPV6_FLOWTYPE, IPPROTO_IPV6);
1536 mp = &(*mp)->m_next;
1540 if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1541 uint32_t flowid, flow_type;
1542 uint32_t rss_bucketid;
1544 flowid = m->m_pkthdr.flowid;
1545 flow_type = M_HASHTYPE_GET(m);
1547 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1548 *mp = sbcreatecontrol((caddr_t) &rss_bucketid,
1549 sizeof(uint32_t), IPV6_RSSBUCKETID, IPPROTO_IPV6);
1551 mp = &(*mp)->m_next;
1560 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu)
1564 struct ip6_mtuinfo mtuctl;
1566 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1568 * Notify the error by sending IPV6_PATHMTU ancillary data if
1569 * application wanted to know the MTU value.
1570 * NOTE: we notify disconnected sockets, because some udp
1571 * applications keep sending sockets disconnected.
1572 * NOTE: our implementation doesn't notify connected sockets that has
1573 * foreign address that is different than given destination addresses
1574 * (this is permitted by RFC 3542).
1576 if ((inp->inp_flags & IN6P_MTU) == 0 || (
1577 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1578 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr)))
1581 mtuctl.ip6m_mtu = mtu;
1582 mtuctl.ip6m_addr = *dst;
1583 if (sa6_recoverscope(&mtuctl.ip6m_addr))
1586 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1587 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1590 so = inp->inp_socket;
1591 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1595 /* XXX: should count statistics */
1601 * Get pointer to the previous header followed by the header
1602 * currently processed.
1605 ip6_get_prevhdr(const struct mbuf *m, int off)
1607 struct ip6_ext ip6e;
1608 struct ip6_hdr *ip6;
1611 if (off == sizeof(struct ip6_hdr))
1612 return (offsetof(struct ip6_hdr, ip6_nxt));
1613 if (off < sizeof(struct ip6_hdr))
1614 panic("%s: off < sizeof(struct ip6_hdr)", __func__);
1616 ip6 = mtod(m, struct ip6_hdr *);
1618 len = sizeof(struct ip6_hdr);
1621 m_copydata(m, len, sizeof(ip6e), (caddr_t)&ip6e);
1623 case IPPROTO_FRAGMENT:
1624 nlen = sizeof(struct ip6_frag);
1627 nlen = (ip6e.ip6e_len + 2) << 2;
1630 nlen = (ip6e.ip6e_len + 1) << 3;
1633 nxt = ip6e.ip6e_nxt;
1635 return (len - nlen);
1639 * get next header offset. m will be retained.
1642 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1645 struct ip6_ext ip6e;
1650 panic("ip6_nexthdr: m == NULL");
1651 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1656 if (m->m_pkthdr.len < off + sizeof(ip6))
1658 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1660 *nxtp = ip6.ip6_nxt;
1664 case IPPROTO_FRAGMENT:
1666 * terminate parsing if it is not the first fragment,
1667 * it does not make sense to parse through it.
1669 if (m->m_pkthdr.len < off + sizeof(fh))
1671 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1672 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1673 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1676 *nxtp = fh.ip6f_nxt;
1677 off += sizeof(struct ip6_frag);
1681 if (m->m_pkthdr.len < off + sizeof(ip6e))
1683 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1685 *nxtp = ip6e.ip6e_nxt;
1686 off += (ip6e.ip6e_len + 2) << 2;
1689 case IPPROTO_HOPOPTS:
1690 case IPPROTO_ROUTING:
1691 case IPPROTO_DSTOPTS:
1692 if (m->m_pkthdr.len < off + sizeof(ip6e))
1694 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1696 *nxtp = ip6e.ip6e_nxt;
1697 off += (ip6e.ip6e_len + 1) << 3;
1702 case IPPROTO_IPCOMP:
1714 * get offset for the last header in the chain. m will be kept untainted.
1717 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1727 newoff = ip6_nexthdr(m, off, proto, nxtp);
1730 else if (newoff < off)
1731 return -1; /* invalid */
1732 else if (newoff == off)
1741 * System control for IP6
1744 u_char inet6ctlerrmap[PRC_NCMDS] = {
1746 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1747 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1748 EMSGSIZE, EHOSTUNREACH, 0, 0,
1749 0, 0, EHOSTUNREACH, 0,
1750 ENOPROTOOPT, ECONNREFUSED