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"
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/hhook.h>
78 #include <sys/malloc.h>
81 #include <sys/domain.h>
82 #include <sys/protosw.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
86 #include <sys/errno.h>
88 #include <sys/kernel.h>
90 #include <sys/rmlock.h>
91 #include <sys/syslog.h>
92 #include <sys/sysctl.h>
93 #include <sys/eventhandler.h>
96 #include <net/if_var.h>
97 #include <net/if_types.h>
98 #include <net/if_dl.h>
99 #include <net/route.h>
100 #include <net/netisr.h>
101 #include <net/rss_config.h>
102 #include <net/pfil.h>
103 #include <net/vnet.h>
105 #include <netinet/in.h>
106 #include <netinet/in_kdtrace.h>
107 #include <netinet/ip_var.h>
108 #include <netinet/in_systm.h>
109 #include <net/if_llatbl.h>
111 #include <netinet/ip.h>
112 #include <netinet/ip_icmp.h>
114 #include <netinet/ip6.h>
115 #include <netinet6/in6_var.h>
116 #include <netinet6/ip6_var.h>
117 #include <netinet/ip_encap.h>
118 #include <netinet/in_pcb.h>
119 #include <netinet/icmp6.h>
120 #include <netinet6/scope6_var.h>
121 #include <netinet6/in6_ifattach.h>
122 #include <netinet6/mld6_var.h>
123 #include <netinet6/nd6.h>
124 #include <netinet6/in6_rss.h>
126 #include <netinet/sctp_pcb.h>
127 #include <netinet6/sctp6_var.h>
130 #include <netipsec/ipsec_support.h>
132 ip6proto_input_t *ip6_protox[IPPROTO_MAX] = {
133 [0 ... IPPROTO_MAX - 1] = rip6_input };
134 ip6proto_ctlinput_t *ip6_ctlprotox[IPPROTO_MAX] = {
135 [0 ... IPPROTO_MAX - 1] = rip6_ctlinput };
137 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
138 VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
139 VNET_DEFINE(u_long, in6_ifaddrhmask);
141 static struct netisr_handler ip6_nh = {
143 .nh_handler = ip6_input,
144 .nh_proto = NETISR_IPV6,
146 .nh_m2cpuid = rss_soft_m2cpuid_v6,
147 .nh_policy = NETISR_POLICY_CPU,
148 .nh_dispatch = NETISR_DISPATCH_HYBRID,
150 .nh_policy = NETISR_POLICY_FLOW,
155 sysctl_netinet6_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
159 netisr_getqlimit(&ip6_nh, &qlimit);
160 error = sysctl_handle_int(oidp, &qlimit, 0, req);
161 if (error || !req->newptr)
165 return (netisr_setqlimit(&ip6_nh, qlimit));
167 SYSCTL_DECL(_net_inet6_ip6);
168 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRQMAXLEN, intr_queue_maxlen,
169 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
170 0, 0, sysctl_netinet6_intr_queue_maxlen, "I",
171 "Maximum size of the IPv6 input queue");
173 VNET_DEFINE_STATIC(bool, ip6_sav) = true;
174 #define V_ip6_sav VNET(ip6_sav)
175 SYSCTL_BOOL(_net_inet6_ip6, OID_AUTO, source_address_validation,
176 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_sav), true,
177 "Drop incoming packets with source address that is a local address");
180 static struct netisr_handler ip6_direct_nh = {
181 .nh_name = "ip6_direct",
182 .nh_handler = ip6_direct_input,
183 .nh_proto = NETISR_IPV6_DIRECT,
184 .nh_m2cpuid = rss_soft_m2cpuid_v6,
185 .nh_policy = NETISR_POLICY_CPU,
186 .nh_dispatch = NETISR_DISPATCH_HYBRID,
190 sysctl_netinet6_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
194 netisr_getqlimit(&ip6_direct_nh, &qlimit);
195 error = sysctl_handle_int(oidp, &qlimit, 0, req);
196 if (error || !req->newptr)
200 return (netisr_setqlimit(&ip6_direct_nh, qlimit));
202 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
203 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
204 0, 0, sysctl_netinet6_intr_direct_queue_maxlen, "I",
205 "Maximum size of the IPv6 direct input queue");
209 VNET_DEFINE(pfil_head_t, inet6_pfil_head);
211 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
212 VNET_PCPUSTAT_SYSINIT(ip6stat);
214 VNET_PCPUSTAT_SYSUNINIT(ip6stat);
217 struct rmlock in6_ifaddr_lock;
218 RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
220 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
223 * IP6 initialization: fill in IP6 protocol switch table.
224 * All protocols not implemented in kernel go to raw IP6 protocol handler.
227 ip6_vnet_init(void *arg __unused)
229 struct pfil_head_args args;
231 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
232 &V_ip6_auto_linklocal);
233 TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
234 TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
236 CK_STAILQ_INIT(&V_in6_ifaddrhead);
237 V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
240 /* Initialize packet filter hooks. */
241 args.pa_version = PFIL_VERSION;
242 args.pa_flags = PFIL_IN | PFIL_OUT;
243 args.pa_type = PFIL_TYPE_IP6;
244 args.pa_headname = PFIL_INET6_NAME;
245 V_inet6_pfil_head = pfil_head_register(&args);
247 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6,
248 &V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
249 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
250 printf("%s: WARNING: unable to register input helper hook\n",
252 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6,
253 &V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
254 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
255 printf("%s: WARNING: unable to register output helper hook\n",
259 addrsel_policy_init();
263 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
265 /* Skip global initialization stuff for non-default instances. */
267 netisr_register_vnet(&ip6_nh);
269 netisr_register_vnet(&ip6_direct_nh);
273 VNET_SYSINIT(ip6_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
274 ip6_vnet_init, NULL);
277 ip6_init(void *arg __unused)
281 * Register statically those protocols that are unlikely to ever go
284 IP6PROTO_REGISTER(IPPROTO_ICMPV6, icmp6_input, rip6_ctlinput);
285 IP6PROTO_REGISTER(IPPROTO_DSTOPTS, dest6_input, NULL);
286 IP6PROTO_REGISTER(IPPROTO_ROUTING, route6_input, NULL);
287 IP6PROTO_REGISTER(IPPROTO_FRAGMENT, frag6_input, NULL);
288 IP6PROTO_REGISTER(IPPROTO_IPV4, encap6_input, NULL);
289 IP6PROTO_REGISTER(IPPROTO_IPV6, encap6_input, NULL);
290 IP6PROTO_REGISTER(IPPROTO_ETHERIP, encap6_input, NULL);
291 IP6PROTO_REGISTER(IPPROTO_GRE, encap6_input, NULL);
292 IP6PROTO_REGISTER(IPPROTO_PIM, encap6_input, NULL);
293 #ifdef SCTP /* XXX: has a loadable & static version */
294 IP6PROTO_REGISTER(IPPROTO_SCTP, sctp6_input, sctp6_ctlinput);
297 EVENTHANDLER_REGISTER(vm_lowmem, frag6_drain, NULL, LOWMEM_PRI_DEFAULT);
298 EVENTHANDLER_REGISTER(mbuf_lowmem, frag6_drain, NULL,
301 netisr_register(&ip6_nh);
303 netisr_register(&ip6_direct_nh);
306 SYSINIT(ip6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_init, NULL);
309 ip6proto_register(uint8_t proto, ip6proto_input_t input,
310 ip6proto_ctlinput_t ctl)
315 if (ip6_protox[proto] == rip6_input) {
316 ip6_protox[proto] = input;
317 ip6_ctlprotox[proto] = ctl;
324 ip6proto_unregister(uint8_t proto)
329 if (ip6_protox[proto] != rip6_input) {
330 ip6_protox[proto] = rip6_input;
331 ip6_ctlprotox[proto] = rip6_ctlinput;
339 ip6_destroy(void *unused __unused)
341 struct ifaddr *ifa, *nifa;
346 netisr_unregister_vnet(&ip6_direct_nh);
348 netisr_unregister_vnet(&ip6_nh);
350 pfil_head_unregister(V_inet6_pfil_head);
351 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]);
353 printf("%s: WARNING: unable to deregister input helper hook "
354 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: "
355 "error %d returned\n", __func__, error);
357 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]);
359 printf("%s: WARNING: unable to deregister output helper hook "
360 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: "
361 "error %d returned\n", __func__, error);
364 /* Cleanup addresses. */
366 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
367 /* Cannot lock here - lock recursion. */
368 /* IF_ADDR_LOCK(ifp); */
369 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
370 if (ifa->ifa_addr->sa_family != AF_INET6)
374 /* IF_ADDR_UNLOCK(ifp); */
375 in6_ifdetach_destroy(ifp);
376 mld_domifdetach(ifp);
380 /* Make sure any routes are gone as well. */
381 rib_flush_routes_family(AF_INET6);
385 in6_ifattach_destroy();
387 hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
390 VNET_SYSUNINIT(inet6, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_destroy, NULL);
394 ip6_input_hbh(struct mbuf **mp, uint32_t *plen, uint32_t *rtalert, int *off,
401 if (ip6_hopopts_input(plen, rtalert, mp, off)) {
402 #if 0 /*touches NULL pointer*/
403 in6_ifstat_inc((*mp)->m_pkthdr.rcvif, ifs6_in_discard);
405 goto out; /* m have already been freed */
410 ip6 = mtod(m, struct ip6_hdr *);
413 * if the payload length field is 0 and the next header field
414 * indicates Hop-by-Hop Options header, then a Jumbo Payload
415 * option MUST be included.
417 if (ip6->ip6_plen == 0 && *plen == 0) {
419 * Note that if a valid jumbo payload option is
420 * contained, ip6_hopopts_input() must set a valid
421 * (non-zero) payload length to the variable plen.
423 IP6STAT_INC(ip6s_badoptions);
424 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
425 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
426 icmp6_error(m, ICMP6_PARAM_PROB,
427 ICMP6_PARAMPROB_HEADER,
428 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
431 /* ip6_hopopts_input() ensures that mbuf is contiguous */
432 hbh = (struct ip6_hbh *)(ip6 + 1);
433 *nxt = hbh->ip6h_nxt;
436 * If we are acting as a router and the packet contains a
437 * router alert option, see if we know the option value.
438 * Currently, we only support the option value for MLD, in which
439 * case we should pass the packet to the multicast routing
442 if (*rtalert != ~0) {
444 case IP6OPT_RTALERT_MLD:
445 if (V_ip6_forwarding)
450 * RFC2711 requires unrecognized values must be
465 * IPv6 direct input routine.
467 * This is called when reinjecting completed fragments where
468 * all of the previous checking and book-keeping has been done.
471 ip6_direct_input(struct mbuf *m)
476 struct ip6_direct_ctx *ip6dc;
478 mtag = m_tag_locate(m, MTAG_ABI_IPV6, IPV6_TAG_DIRECT, NULL);
479 KASSERT(mtag != NULL, ("Reinjected packet w/o direct ctx tag!"));
481 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
482 nxt = ip6dc->ip6dc_nxt;
483 off = ip6dc->ip6dc_off;
487 m_tag_delete(m, mtag);
489 while (nxt != IPPROTO_DONE) {
490 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
491 IP6STAT_INC(ip6s_toomanyhdr);
496 * protection against faulty packet - there should be
497 * more sanity checks in header chain processing.
499 if (m->m_pkthdr.len < off) {
500 IP6STAT_INC(ip6s_tooshort);
501 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
505 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
506 if (IPSEC_ENABLED(ipv6)) {
507 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
512 nxt = ip6_protox[nxt](&m, &off, nxt);
521 ip6_input(struct mbuf *m)
523 struct in6_addr odst;
525 struct in6_ifaddr *ia;
528 u_int32_t rtalert = ~0;
529 int off = sizeof(struct ip6_hdr), nest;
534 * Drop the packet if IPv6 operation is disabled on the interface.
536 rcvif = m->m_pkthdr.rcvif;
537 if ((ND_IFINFO(rcvif)->flags & ND6_IFF_IFDISABLED))
540 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
542 * should the inner packet be considered authentic?
543 * see comment in ah4_input().
544 * NB: m cannot be NULL when passed to the input routine
547 m->m_flags &= ~M_AUTHIPHDR;
548 m->m_flags &= ~M_AUTHIPDGM;
552 if (m->m_flags & M_FASTFWD_OURS) {
554 * Firewall changed destination to local.
556 ip6 = mtod(m, struct ip6_hdr *);
563 if (m->m_flags & M_EXT) {
565 IP6STAT_INC(ip6s_mext2m);
567 IP6STAT_INC(ip6s_mext1);
570 struct ifnet *ifp = (m->m_flags & M_LOOP) ? V_loif : rcvif;
571 int ifindex = ifp->if_index;
572 if (ifindex >= IP6S_M2MMAX)
574 IP6STAT_INC(ip6s_m2m[ifindex]);
576 IP6STAT_INC(ip6s_m1);
579 in6_ifstat_inc(rcvif, ifs6_in_receive);
580 IP6STAT_INC(ip6s_total);
583 * L2 bridge code and some other code can return mbuf chain
584 * that does not conform to KAME requirement. too bad.
585 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
587 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
590 if (m->m_pkthdr.len > MHLEN)
591 n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
593 n = m_gethdr(M_NOWAIT, MT_DATA);
598 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
599 n->m_len = n->m_pkthdr.len;
603 if (m->m_len < sizeof(struct ip6_hdr)) {
604 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
605 IP6STAT_INC(ip6s_toosmall);
606 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
611 ip6 = mtod(m, struct ip6_hdr *);
612 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
613 IP6STAT_INC(ip6s_badvers);
614 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
618 IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
619 IP_PROBE(receive, NULL, NULL, ip6, rcvif, NULL, ip6);
622 * Check against address spoofing/corruption.
624 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
625 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
627 * XXX: "badscope" is not very suitable for a multicast source.
629 IP6STAT_INC(ip6s_badscope);
630 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
633 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
634 !(m->m_flags & M_LOOP)) {
636 * In this case, the packet should come from the loopback
637 * interface. However, we cannot just check the if_flags,
638 * because ip6_mloopback() passes the "actual" interface
639 * as the outgoing/incoming interface.
641 IP6STAT_INC(ip6s_badscope);
642 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
645 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
646 IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
649 * Nodes must not originate a packet to a multicast address
650 * whose scop field contains the reserved value 0; if such
651 * a packet is received, it must be silently dropped.
653 IP6STAT_INC(ip6s_badscope);
654 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
658 * The following check is not documented in specs. A malicious
659 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
660 * and bypass security checks (act as if it was from 127.0.0.1 by using
661 * IPv6 src ::ffff:127.0.0.1). Be cautious.
663 * We have supported IPv6-only kernels for a few years and this issue
664 * has not come up. The world seems to move mostly towards not using
665 * v4mapped on the wire, so it makes sense for us to keep rejecting
668 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
669 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
670 IP6STAT_INC(ip6s_badscope);
671 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
676 * Reject packets with IPv4 compatible addresses (auto tunnel).
678 * The code forbids auto tunnel relay case in RFC1933 (the check is
679 * stronger than RFC1933). We may want to re-enable it if mech-xx
680 * is revised to forbid relaying case.
682 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
683 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
684 IP6STAT_INC(ip6s_badscope);
685 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
690 * Try to forward the packet, but if we fail continue.
691 * ip6_tryforward() does not generate redirects, so fall
692 * through to normal processing if redirects are required.
693 * ip6_tryforward() does inbound and outbound packet firewall
694 * processing. If firewall has decided that destination becomes
695 * our local address, it sets M_FASTFWD_OURS flag. In this
696 * case skip another inbound firewall processing and update
699 if (V_ip6_forwarding != 0 && V_ip6_sendredirects == 0
700 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
701 && (!IPSEC_ENABLED(ipv6) ||
702 IPSEC_CAPS(ipv6, m, IPSEC_CAP_OPERABLE) == 0)
705 if ((m = ip6_tryforward(m)) == NULL)
707 if (m->m_flags & M_FASTFWD_OURS) {
708 ip6 = mtod(m, struct ip6_hdr *);
712 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
714 * Bypass packet filtering for packets previously handled by IPsec.
716 if (IPSEC_ENABLED(ipv6) &&
717 IPSEC_CAPS(ipv6, m, IPSEC_CAP_BYPASS_FILTER) != 0)
721 * Run through list of hooks for input packets.
723 * NB: Beware of the destination address changing
724 * (e.g. by NAT rewriting). When this happens,
725 * tell ip6_forward to do the right thing.
728 /* Jump over all PFIL processing if hooks are not active. */
729 if (!PFIL_HOOKED_IN(V_inet6_pfil_head))
733 if (pfil_mbuf_in(V_inet6_pfil_head, &m, m->m_pkthdr.rcvif,
736 ip6 = mtod(m, struct ip6_hdr *);
737 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
738 if ((m->m_flags & (M_IP6_NEXTHOP | M_FASTFWD_OURS)) == M_IP6_NEXTHOP &&
739 m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
741 * Directly ship the packet on. This allows forwarding
742 * packets originally destined to us to some other directly
751 * Disambiguate address scope zones (if there is ambiguity).
752 * We first make sure that the original source or destination address
753 * is not in our internal form for scoped addresses. Such addresses
754 * are not necessarily invalid spec-wise, but we cannot accept them due
755 * to the usage conflict.
756 * in6_setscope() then also checks and rejects the cases where src or
757 * dst are the loopback address and the receiving interface
760 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
761 IP6STAT_INC(ip6s_badscope); /* XXX */
764 if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
765 in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
766 IP6STAT_INC(ip6s_badscope);
769 if (m->m_flags & M_FASTFWD_OURS) {
770 m->m_flags &= ~M_FASTFWD_OURS;
775 * Multicast check. Assume packet is for us to avoid
776 * prematurely taking locks.
778 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
780 in6_ifstat_inc(rcvif, ifs6_in_mcast);
785 * XXX: For now we keep link-local IPv6 addresses with embedded
786 * scope zone id, therefore we use zero zoneid here.
788 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
790 if (ia->ia6_flags & IN6_IFF_NOTREADY) {
791 char ip6bufs[INET6_ADDRSTRLEN];
792 char ip6bufd[INET6_ADDRSTRLEN];
793 /* address is not ready, so discard the packet. */
795 "ip6_input: packet to an unready address %s->%s\n",
796 ip6_sprintf(ip6bufs, &ip6->ip6_src),
797 ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
800 if (V_ip6_sav && !(m->m_flags & M_LOOP) &&
801 __predict_false(in6_localip_fib(&ip6->ip6_src,
803 IP6STAT_INC(ip6s_badscope); /* XXX */
806 /* Count the packet in the ip address stats */
807 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
808 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
814 * Now there is no reason to process the packet if it's not our own
815 * and we're not a router.
817 if (!V_ip6_forwarding) {
818 IP6STAT_INC(ip6s_cantforward);
824 * Process Hop-by-Hop options header if it's contained.
825 * m may be modified in ip6_hopopts_input().
826 * If a JumboPayload option is included, plen will also be modified.
828 plen = (u_int32_t)ntohs(ip6->ip6_plen);
829 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
830 if (ip6_input_hbh(&m, &plen, &rtalert, &off, &nxt, &ours) != 0)
836 * Use mbuf flags to propagate Router Alert option to
837 * ICMPv6 layer, as hop-by-hop options have been stripped.
840 m->m_flags |= M_RTALERT_MLD;
843 * Check that the amount of data in the buffers
844 * is as at least much as the IPv6 header would have us expect.
845 * Trim mbufs if longer than we expect.
846 * Drop packet if shorter than we expect.
848 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
849 IP6STAT_INC(ip6s_tooshort);
850 in6_ifstat_inc(rcvif, ifs6_in_truncated);
853 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
854 if (m->m_len == m->m_pkthdr.len) {
855 m->m_len = sizeof(struct ip6_hdr) + plen;
856 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
858 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
862 * Forward if desirable.
865 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
867 * If we are acting as a multicast router, all
868 * incoming multicast packets are passed to the
869 * kernel-level multicast forwarding function.
870 * The packet is returned (relatively) intact; if
871 * ip6_mforward() returns a non-zero value, the packet
872 * must be discarded, else it may be accepted below.
874 * XXX TODO: Check hlim and multicast scope here to avoid
875 * unnecessarily calling into ip6_mforward().
877 if (ip6_mforward && ip6_mforward(ip6, rcvif, m)) {
878 IP6STAT_INC(ip6s_cantforward);
882 ip6_forward(m, srcrt);
887 * Tell launch routine the next header
889 IP6STAT_INC(ip6s_delivered);
890 in6_ifstat_inc(rcvif, ifs6_in_deliver);
893 while (nxt != IPPROTO_DONE) {
894 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
895 IP6STAT_INC(ip6s_toomanyhdr);
900 * protection against faulty packet - there should be
901 * more sanity checks in header chain processing.
903 if (m->m_pkthdr.len < off) {
904 IP6STAT_INC(ip6s_tooshort);
905 in6_ifstat_inc(rcvif, ifs6_in_truncated);
909 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
910 if (IPSEC_ENABLED(ipv6)) {
911 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
916 nxt = ip6_protox[nxt](&m, &off, nxt);
920 in6_ifstat_inc(rcvif, ifs6_in_discard);
926 * Hop-by-Hop options header processing. If a valid jumbo payload option is
927 * included, the real payload length will be stored in plenp.
929 * rtalertp - XXX: should be stored more smart way
932 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
933 struct mbuf **mp, int *offp)
935 struct mbuf *m = *mp;
936 int off = *offp, hbhlen;
939 /* validation of the length of the header */
940 if (m->m_len < off + sizeof(*hbh)) {
941 m = m_pullup(m, off + sizeof(*hbh));
943 IP6STAT_INC(ip6s_exthdrtoolong);
948 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
949 hbhlen = (hbh->ip6h_len + 1) << 3;
951 if (m->m_len < off + hbhlen) {
952 m = m_pullup(m, off + hbhlen);
954 IP6STAT_INC(ip6s_exthdrtoolong);
959 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
961 hbhlen -= sizeof(struct ip6_hbh);
962 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
963 hbhlen, rtalertp, plenp) < 0) {
974 * Search header for all Hop-by-hop options and process each option.
975 * This function is separate from ip6_hopopts_input() in order to
976 * handle a case where the sending node itself process its hop-by-hop
977 * options header. In such a case, the function is called from ip6_output().
979 * The function assumes that hbh header is located right after the IPv6 header
980 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
981 * opthead + hbhlen is located in contiguous memory region.
984 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
985 u_int32_t *rtalertp, u_int32_t *plenp)
989 u_int8_t *opt = opthead;
990 u_int16_t rtalert_val;
992 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
994 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1000 if (hbhlen < IP6OPT_MINLEN) {
1001 IP6STAT_INC(ip6s_toosmall);
1004 optlen = *(opt + 1) + 2;
1006 case IP6OPT_ROUTER_ALERT:
1007 /* XXX may need check for alignment */
1008 if (hbhlen < IP6OPT_RTALERT_LEN) {
1009 IP6STAT_INC(ip6s_toosmall);
1012 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1014 icmp6_error(m, ICMP6_PARAM_PROB,
1015 ICMP6_PARAMPROB_HEADER,
1016 erroff + opt + 1 - opthead);
1019 optlen = IP6OPT_RTALERT_LEN;
1020 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1021 *rtalertp = ntohs(rtalert_val);
1024 /* XXX may need check for alignment */
1025 if (hbhlen < IP6OPT_JUMBO_LEN) {
1026 IP6STAT_INC(ip6s_toosmall);
1029 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1031 icmp6_error(m, ICMP6_PARAM_PROB,
1032 ICMP6_PARAMPROB_HEADER,
1033 erroff + opt + 1 - opthead);
1036 optlen = IP6OPT_JUMBO_LEN;
1039 * IPv6 packets that have non 0 payload length
1040 * must not contain a jumbo payload option.
1042 ip6 = mtod(m, struct ip6_hdr *);
1043 if (ip6->ip6_plen) {
1044 IP6STAT_INC(ip6s_badoptions);
1045 icmp6_error(m, ICMP6_PARAM_PROB,
1046 ICMP6_PARAMPROB_HEADER,
1047 erroff + opt - opthead);
1052 * We may see jumbolen in unaligned location, so
1053 * we'd need to perform bcopy().
1055 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1056 jumboplen = (u_int32_t)htonl(jumboplen);
1060 * if there are multiple jumbo payload options,
1061 * *plenp will be non-zero and the packet will be
1063 * the behavior may need some debate in ipngwg -
1064 * multiple options does not make sense, however,
1065 * there's no explicit mention in specification.
1068 IP6STAT_INC(ip6s_badoptions);
1069 icmp6_error(m, ICMP6_PARAM_PROB,
1070 ICMP6_PARAMPROB_HEADER,
1071 erroff + opt + 2 - opthead);
1077 * jumbo payload length must be larger than 65535.
1079 if (jumboplen <= IPV6_MAXPACKET) {
1080 IP6STAT_INC(ip6s_badoptions);
1081 icmp6_error(m, ICMP6_PARAM_PROB,
1082 ICMP6_PARAMPROB_HEADER,
1083 erroff + opt + 2 - opthead);
1089 default: /* unknown option */
1090 if (hbhlen < IP6OPT_MINLEN) {
1091 IP6STAT_INC(ip6s_toosmall);
1094 optlen = ip6_unknown_opt(opt, m,
1095 erroff + opt - opthead);
1111 * Unknown option processing.
1112 * The third argument `off' is the offset from the IPv6 header to the option,
1113 * which is necessary if the IPv6 header the and option header and IPv6 header
1114 * is not contiguous in order to return an ICMPv6 error.
1117 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1119 struct ip6_hdr *ip6;
1121 switch (IP6OPT_TYPE(*optp)) {
1122 case IP6OPT_TYPE_SKIP: /* ignore the option */
1123 return ((int)*(optp + 1));
1124 case IP6OPT_TYPE_DISCARD: /* silently discard */
1127 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1128 IP6STAT_INC(ip6s_badoptions);
1129 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1131 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1132 IP6STAT_INC(ip6s_badoptions);
1133 ip6 = mtod(m, struct ip6_hdr *);
1134 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1135 (m->m_flags & (M_BCAST|M_MCAST)))
1138 icmp6_error(m, ICMP6_PARAM_PROB,
1139 ICMP6_PARAMPROB_OPTION, off);
1143 m_freem(m); /* XXX: NOTREACHED */
1148 * Create the "control" list for this pcb.
1149 * These functions will not modify mbuf chain at all.
1151 * The routine will be called from upper layer handlers like tcp6_input().
1152 * Thus the routine assumes that the caller (tcp6_input) have already
1153 * called m_pullup() and all the extension headers are located in the
1154 * very first mbuf on the mbuf chain.
1156 * ip6_savecontrol_v4 will handle those options that are possible to be
1157 * set on a v4-mapped socket.
1158 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1159 * options and handle the v6-only ones itself.
1162 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1165 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1168 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1174 struct bintime boottimebin, bt1;
1175 struct timespec ts1;
1179 switch (inp->inp_socket->so_ts_clock) {
1180 case SO_TS_REALTIME_MICRO:
1181 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1183 mbuf_tstmp2timespec(m, &ts1);
1184 timespec2bintime(&ts1, &bt1);
1185 getboottimebin(&boottimebin);
1186 bintime_add(&bt1, &boottimebin);
1187 bintime2timeval(&bt1, &t.tv);
1191 *mp = sbcreatecontrol(&t.tv, sizeof(t.tv),
1192 SCM_TIMESTAMP, SOL_SOCKET, M_NOWAIT);
1194 mp = &(*mp)->m_next;
1200 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1202 mbuf_tstmp2timespec(m, &ts1);
1203 timespec2bintime(&ts1, &t.bt);
1204 getboottimebin(&boottimebin);
1205 bintime_add(&t.bt, &boottimebin);
1209 *mp = sbcreatecontrol(&t.bt, sizeof(t.bt), SCM_BINTIME,
1210 SOL_SOCKET, M_NOWAIT);
1212 mp = &(*mp)->m_next;
1217 case SO_TS_REALTIME:
1218 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1220 mbuf_tstmp2timespec(m, &t.ts);
1221 getboottimebin(&boottimebin);
1222 bintime2timespec(&boottimebin, &ts1);
1223 timespecadd(&t.ts, &ts1, &t.ts);
1227 *mp = sbcreatecontrol(&t.ts, sizeof(t.ts),
1228 SCM_REALTIME, SOL_SOCKET, M_NOWAIT);
1230 mp = &(*mp)->m_next;
1235 case SO_TS_MONOTONIC:
1236 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1238 mbuf_tstmp2timespec(m, &t.ts);
1241 *mp = sbcreatecontrol(&t.ts, sizeof(t.ts),
1242 SCM_MONOTONIC, SOL_SOCKET, M_NOWAIT);
1244 mp = &(*mp)->m_next;
1250 panic("unknown (corrupted) so_ts_clock");
1252 if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) ==
1253 (M_PKTHDR | M_TSTMP)) {
1254 struct sock_timestamp_info sti;
1256 bzero(&sti, sizeof(sti));
1257 sti.st_info_flags = ST_INFO_HW;
1258 if ((m->m_flags & M_TSTMP_HPREC) != 0)
1259 sti.st_info_flags |= ST_INFO_HW_HPREC;
1260 *mp = sbcreatecontrol(&sti, sizeof(sti), SCM_TIME_INFO,
1261 SOL_SOCKET, M_NOWAIT);
1263 mp = &(*mp)->m_next;
1268 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1269 /* RFC 2292 sec. 5 */
1270 if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1271 struct in6_pktinfo pi6;
1273 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1277 ip = mtod(m, struct ip *);
1278 pi6.ipi6_addr.s6_addr32[0] = 0;
1279 pi6.ipi6_addr.s6_addr32[1] = 0;
1280 pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1281 pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1283 /* We won't hit this code */
1284 bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1287 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1288 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1291 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1293 *mp = sbcreatecontrol(&pi6, sizeof(struct in6_pktinfo),
1294 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6,
1297 mp = &(*mp)->m_next;
1300 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1303 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1307 ip = mtod(m, struct ip *);
1310 /* We won't hit this code */
1314 hlim = ip6->ip6_hlim & 0xff;
1316 *mp = sbcreatecontrol(&hlim, sizeof(int),
1317 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1318 IPPROTO_IPV6, M_NOWAIT);
1320 mp = &(*mp)->m_next;
1323 if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1326 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1330 ip = mtod(m, struct ip *);
1331 tclass = ip->ip_tos;
1333 /* We won't hit this code */
1339 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1341 tclass = flowinfo & 0xff;
1343 *mp = sbcreatecontrol(&tclass, sizeof(int), IPV6_TCLASS,
1344 IPPROTO_IPV6, M_NOWAIT);
1346 mp = &(*mp)->m_next;
1349 if (v4only != NULL) {
1350 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1361 ip6_savecontrol(struct inpcb *inp, struct mbuf *m, struct mbuf **mp)
1363 struct ip6_hdr *ip6;
1366 mp = ip6_savecontrol_v4(inp, m, mp, &v4only);
1370 ip6 = mtod(m, struct ip6_hdr *);
1372 * IPV6_HOPOPTS socket option. Recall that we required super-user
1373 * privilege for the option (see ip6_ctloutput), but it might be too
1374 * strict, since there might be some hop-by-hop options which can be
1375 * returned to normal user.
1376 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1378 if ((inp->inp_flags & IN6P_HOPOPTS) != 0) {
1380 * Check if a hop-by-hop options header is contatined in the
1381 * received packet, and if so, store the options as ancillary
1382 * data. Note that a hop-by-hop options header must be
1383 * just after the IPv6 header, which is assured through the
1384 * IPv6 input processing.
1386 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1387 struct ip6_hbh *hbh;
1390 hbh = (struct ip6_hbh *)(ip6 + 1);
1391 hbhlen = (hbh->ip6h_len + 1) << 3;
1394 * XXX: We copy the whole header even if a
1395 * jumbo payload option is included, the option which
1396 * is to be removed before returning according to
1398 * Note: this constraint is removed in RFC3542
1400 *mp = sbcreatecontrol(hbh, hbhlen,
1401 IS2292(inp, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1402 IPPROTO_IPV6, M_NOWAIT);
1404 mp = &(*mp)->m_next;
1408 if ((inp->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1409 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1412 * Search for destination options headers or routing
1413 * header(s) through the header chain, and stores each
1414 * header as ancillary data.
1415 * Note that the order of the headers remains in
1416 * the chain of ancillary data.
1418 while (1) { /* is explicit loop prevention necessary? */
1419 struct ip6_ext *ip6e = NULL;
1423 * if it is not an extension header, don't try to
1424 * pull it from the chain.
1427 case IPPROTO_DSTOPTS:
1428 case IPPROTO_ROUTING:
1429 case IPPROTO_HOPOPTS:
1430 case IPPROTO_AH: /* is it possible? */
1436 if (off + sizeof(*ip6e) > m->m_len)
1438 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1439 if (nxt == IPPROTO_AH)
1440 elen = (ip6e->ip6e_len + 2) << 2;
1442 elen = (ip6e->ip6e_len + 1) << 3;
1443 if (off + elen > m->m_len)
1447 case IPPROTO_DSTOPTS:
1448 if (!(inp->inp_flags & IN6P_DSTOPTS))
1451 *mp = sbcreatecontrol(ip6e, elen,
1452 IS2292(inp, IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1453 IPPROTO_IPV6, M_NOWAIT);
1455 mp = &(*mp)->m_next;
1457 case IPPROTO_ROUTING:
1458 if (!(inp->inp_flags & IN6P_RTHDR))
1461 *mp = sbcreatecontrol(ip6e, elen,
1462 IS2292(inp, IPV6_2292RTHDR, IPV6_RTHDR),
1463 IPPROTO_IPV6, M_NOWAIT);
1465 mp = &(*mp)->m_next;
1467 case IPPROTO_HOPOPTS:
1468 case IPPROTO_AH: /* is it possible? */
1473 * other cases have been filtered in the above.
1474 * none will visit this case. here we supply
1475 * the code just in case (nxt overwritten or
1481 /* proceed with the next header. */
1483 nxt = ip6e->ip6e_nxt;
1490 if (inp->inp_flags2 & INP_RECVFLOWID) {
1491 uint32_t flowid, flow_type;
1493 flowid = m->m_pkthdr.flowid;
1494 flow_type = M_HASHTYPE_GET(m);
1497 * XXX should handle the failure of one or the
1498 * other - don't populate both?
1500 *mp = sbcreatecontrol(&flowid, sizeof(uint32_t), IPV6_FLOWID,
1501 IPPROTO_IPV6, M_NOWAIT);
1503 mp = &(*mp)->m_next;
1504 *mp = sbcreatecontrol(&flow_type, sizeof(uint32_t),
1505 IPV6_FLOWTYPE, IPPROTO_IPV6, M_NOWAIT);
1507 mp = &(*mp)->m_next;
1511 if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1512 uint32_t flowid, flow_type;
1513 uint32_t rss_bucketid;
1515 flowid = m->m_pkthdr.flowid;
1516 flow_type = M_HASHTYPE_GET(m);
1518 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1519 *mp = sbcreatecontrol(&rss_bucketid, sizeof(uint32_t),
1520 IPV6_RSSBUCKETID, IPPROTO_IPV6, M_NOWAIT);
1522 mp = &(*mp)->m_next;
1531 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu)
1535 struct ip6_mtuinfo mtuctl;
1537 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1539 * Notify the error by sending IPV6_PATHMTU ancillary data if
1540 * application wanted to know the MTU value.
1541 * NOTE: we notify disconnected sockets, because some udp
1542 * applications keep sending sockets disconnected.
1543 * NOTE: our implementation doesn't notify connected sockets that has
1544 * foreign address that is different than given destination addresses
1545 * (this is permitted by RFC 3542).
1547 if ((inp->inp_flags & IN6P_MTU) == 0 || (
1548 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1549 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr)))
1552 mtuctl.ip6m_mtu = mtu;
1553 mtuctl.ip6m_addr = *dst;
1554 if (sa6_recoverscope(&mtuctl.ip6m_addr))
1557 if ((m_mtu = sbcreatecontrol(&mtuctl, sizeof(mtuctl), IPV6_PATHMTU,
1558 IPPROTO_IPV6, M_NOWAIT)) == NULL)
1561 so = inp->inp_socket;
1562 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1566 /* XXX: should count statistics */
1572 * Get pointer to the previous header followed by the header
1573 * currently processed.
1576 ip6_get_prevhdr(const struct mbuf *m, int off)
1578 struct ip6_ext ip6e;
1579 struct ip6_hdr *ip6;
1582 if (off == sizeof(struct ip6_hdr))
1583 return (offsetof(struct ip6_hdr, ip6_nxt));
1584 if (off < sizeof(struct ip6_hdr))
1585 panic("%s: off < sizeof(struct ip6_hdr)", __func__);
1587 ip6 = mtod(m, struct ip6_hdr *);
1589 len = sizeof(struct ip6_hdr);
1592 m_copydata(m, len, sizeof(ip6e), (caddr_t)&ip6e);
1594 case IPPROTO_FRAGMENT:
1595 nlen = sizeof(struct ip6_frag);
1598 nlen = (ip6e.ip6e_len + 2) << 2;
1601 nlen = (ip6e.ip6e_len + 1) << 3;
1604 nxt = ip6e.ip6e_nxt;
1606 return (len - nlen);
1610 * get next header offset. m will be retained.
1613 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1616 struct ip6_ext ip6e;
1621 panic("ip6_nexthdr: m == NULL");
1622 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1627 if (m->m_pkthdr.len < off + sizeof(ip6))
1629 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1631 *nxtp = ip6.ip6_nxt;
1635 case IPPROTO_FRAGMENT:
1637 * terminate parsing if it is not the first fragment,
1638 * it does not make sense to parse through it.
1640 if (m->m_pkthdr.len < off + sizeof(fh))
1642 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1643 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1644 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1647 *nxtp = fh.ip6f_nxt;
1648 off += sizeof(struct ip6_frag);
1652 if (m->m_pkthdr.len < off + sizeof(ip6e))
1654 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1656 *nxtp = ip6e.ip6e_nxt;
1657 off += (ip6e.ip6e_len + 2) << 2;
1660 case IPPROTO_HOPOPTS:
1661 case IPPROTO_ROUTING:
1662 case IPPROTO_DSTOPTS:
1663 if (m->m_pkthdr.len < off + sizeof(ip6e))
1665 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1667 *nxtp = ip6e.ip6e_nxt;
1668 off += (ip6e.ip6e_len + 1) << 3;
1673 case IPPROTO_IPCOMP:
1685 * get offset for the last header in the chain. m will be kept untainted.
1688 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1698 newoff = ip6_nexthdr(m, off, proto, nxtp);
1701 else if (newoff < off)
1702 return -1; /* invalid */
1703 else if (newoff == off)