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 #include <netinet6/ip6protosw.h>
134 ipproto_input_t *ip6_protox[IPPROTO_MAX] = {
135 [0 ... IPPROTO_MAX - 1] = rip6_input };
136 ipproto_ctlinput_t *ip6_ctlprotox[IPPROTO_MAX] = {
137 [0 ... IPPROTO_MAX - 1] = rip6_ctlinput };
139 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
140 VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
141 VNET_DEFINE(u_long, in6_ifaddrhmask);
143 static struct netisr_handler ip6_nh = {
145 .nh_handler = ip6_input,
146 .nh_proto = NETISR_IPV6,
148 .nh_m2cpuid = rss_soft_m2cpuid_v6,
149 .nh_policy = NETISR_POLICY_CPU,
150 .nh_dispatch = NETISR_DISPATCH_HYBRID,
152 .nh_policy = NETISR_POLICY_FLOW,
157 sysctl_netinet6_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
161 netisr_getqlimit(&ip6_nh, &qlimit);
162 error = sysctl_handle_int(oidp, &qlimit, 0, req);
163 if (error || !req->newptr)
167 return (netisr_setqlimit(&ip6_nh, qlimit));
169 SYSCTL_DECL(_net_inet6_ip6);
170 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRQMAXLEN, intr_queue_maxlen,
171 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
172 0, 0, sysctl_netinet6_intr_queue_maxlen, "I",
173 "Maximum size of the IPv6 input queue");
175 VNET_DEFINE_STATIC(bool, ip6_sav) = true;
176 #define V_ip6_sav VNET(ip6_sav)
177 SYSCTL_BOOL(_net_inet6_ip6, OID_AUTO, source_address_validation,
178 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_sav), true,
179 "Drop incoming packets with source address that is a local address");
182 static struct netisr_handler ip6_direct_nh = {
183 .nh_name = "ip6_direct",
184 .nh_handler = ip6_direct_input,
185 .nh_proto = NETISR_IPV6_DIRECT,
186 .nh_m2cpuid = rss_soft_m2cpuid_v6,
187 .nh_policy = NETISR_POLICY_CPU,
188 .nh_dispatch = NETISR_DISPATCH_HYBRID,
192 sysctl_netinet6_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
196 netisr_getqlimit(&ip6_direct_nh, &qlimit);
197 error = sysctl_handle_int(oidp, &qlimit, 0, req);
198 if (error || !req->newptr)
202 return (netisr_setqlimit(&ip6_direct_nh, qlimit));
204 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
205 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
206 0, 0, sysctl_netinet6_intr_direct_queue_maxlen, "I",
207 "Maximum size of the IPv6 direct input queue");
211 VNET_DEFINE(pfil_head_t, inet6_pfil_head);
213 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
214 VNET_PCPUSTAT_SYSINIT(ip6stat);
216 VNET_PCPUSTAT_SYSUNINIT(ip6stat);
219 struct rmlock in6_ifaddr_lock;
220 RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
222 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
225 * IP6 initialization: fill in IP6 protocol switch table.
226 * All protocols not implemented in kernel go to raw IP6 protocol handler.
229 ip6_vnet_init(void *arg __unused)
231 struct pfil_head_args args;
233 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
234 &V_ip6_auto_linklocal);
235 TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
236 TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
238 CK_STAILQ_INIT(&V_in6_ifaddrhead);
239 V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
242 /* Initialize packet filter hooks. */
243 args.pa_version = PFIL_VERSION;
244 args.pa_flags = PFIL_IN | PFIL_OUT;
245 args.pa_type = PFIL_TYPE_IP6;
246 args.pa_headname = PFIL_INET6_NAME;
247 V_inet6_pfil_head = pfil_head_register(&args);
249 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6,
250 &V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
251 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
252 printf("%s: WARNING: unable to register input helper hook\n",
254 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6,
255 &V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
256 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
257 printf("%s: WARNING: unable to register output helper hook\n",
261 addrsel_policy_init();
265 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
267 /* Skip global initialization stuff for non-default instances. */
269 netisr_register_vnet(&ip6_nh);
271 netisr_register_vnet(&ip6_direct_nh);
275 VNET_SYSINIT(ip6_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
276 ip6_vnet_init, NULL);
279 ip6_init(void *arg __unused)
283 * Register statically those protocols that are unlikely to ever go
286 IP6PROTO_REGISTER(IPPROTO_ICMPV6, icmp6_input, rip6_ctlinput);
287 IP6PROTO_REGISTER(IPPROTO_DSTOPTS, dest6_input, NULL);
288 IP6PROTO_REGISTER(IPPROTO_ROUTING, route6_input, NULL);
289 IP6PROTO_REGISTER(IPPROTO_FRAGMENT, frag6_input, NULL);
290 IP6PROTO_REGISTER(IPPROTO_IPV4, encap6_input, NULL);
291 IP6PROTO_REGISTER(IPPROTO_IPV6, encap6_input, NULL);
292 IP6PROTO_REGISTER(IPPROTO_ETHERIP, encap6_input, NULL);
293 IP6PROTO_REGISTER(IPPROTO_GRE, encap6_input, NULL);
294 IP6PROTO_REGISTER(IPPROTO_PIM, encap6_input, NULL);
295 #ifdef SCTP /* XXX: has a loadable & static version */
296 IP6PROTO_REGISTER(IPPROTO_SCTP, sctp6_input, sctp6_ctlinput);
299 EVENTHANDLER_REGISTER(vm_lowmem, frag6_drain, NULL, LOWMEM_PRI_DEFAULT);
300 EVENTHANDLER_REGISTER(mbuf_lowmem, frag6_drain, NULL,
303 netisr_register(&ip6_nh);
305 netisr_register(&ip6_direct_nh);
308 SYSINIT(ip6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_init, NULL);
311 ip6proto_register(uint8_t proto, ipproto_input_t input, ipproto_ctlinput_t ctl)
316 if (ip6_protox[proto] == rip6_input) {
317 ip6_protox[proto] = input;
318 ip6_ctlprotox[proto] = ctl;
325 ip6proto_unregister(uint8_t proto)
330 if (ip6_protox[proto] != rip6_input) {
331 ip6_protox[proto] = rip6_input;
332 ip6_ctlprotox[proto] = rip6_ctlinput;
340 ip6_destroy(void *unused __unused)
342 struct ifaddr *ifa, *nifa;
347 netisr_unregister_vnet(&ip6_direct_nh);
349 netisr_unregister_vnet(&ip6_nh);
351 pfil_head_unregister(V_inet6_pfil_head);
352 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]);
354 printf("%s: WARNING: unable to deregister input helper hook "
355 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: "
356 "error %d returned\n", __func__, error);
358 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]);
360 printf("%s: WARNING: unable to deregister output helper hook "
361 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: "
362 "error %d returned\n", __func__, error);
365 /* Cleanup addresses. */
367 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
368 /* Cannot lock here - lock recursion. */
369 /* IF_ADDR_LOCK(ifp); */
370 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
371 if (ifa->ifa_addr->sa_family != AF_INET6)
375 /* IF_ADDR_UNLOCK(ifp); */
376 in6_ifdetach_destroy(ifp);
377 mld_domifdetach(ifp);
381 /* Make sure any routes are gone as well. */
382 rib_flush_routes_family(AF_INET6);
386 in6_ifattach_destroy();
388 hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
391 VNET_SYSUNINIT(inet6, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_destroy, NULL);
395 ip6_input_hbh(struct mbuf **mp, uint32_t *plen, uint32_t *rtalert, int *off,
402 if (ip6_hopopts_input(plen, rtalert, mp, off)) {
403 #if 0 /*touches NULL pointer*/
404 in6_ifstat_inc((*mp)->m_pkthdr.rcvif, ifs6_in_discard);
406 goto out; /* m have already been freed */
411 ip6 = mtod(m, struct ip6_hdr *);
414 * if the payload length field is 0 and the next header field
415 * indicates Hop-by-Hop Options header, then a Jumbo Payload
416 * option MUST be included.
418 if (ip6->ip6_plen == 0 && *plen == 0) {
420 * Note that if a valid jumbo payload option is
421 * contained, ip6_hopopts_input() must set a valid
422 * (non-zero) payload length to the variable plen.
424 IP6STAT_INC(ip6s_badoptions);
425 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
426 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
427 icmp6_error(m, ICMP6_PARAM_PROB,
428 ICMP6_PARAMPROB_HEADER,
429 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
432 /* ip6_hopopts_input() ensures that mbuf is contiguous */
433 hbh = (struct ip6_hbh *)(ip6 + 1);
434 *nxt = hbh->ip6h_nxt;
437 * If we are acting as a router and the packet contains a
438 * router alert option, see if we know the option value.
439 * Currently, we only support the option value for MLD, in which
440 * case we should pass the packet to the multicast routing
443 if (*rtalert != ~0) {
445 case IP6OPT_RTALERT_MLD:
446 if (V_ip6_forwarding)
451 * RFC2711 requires unrecognized values must be
466 * IPv6 direct input routine.
468 * This is called when reinjecting completed fragments where
469 * all of the previous checking and book-keeping has been done.
472 ip6_direct_input(struct mbuf *m)
477 struct ip6_direct_ctx *ip6dc;
479 mtag = m_tag_locate(m, MTAG_ABI_IPV6, IPV6_TAG_DIRECT, NULL);
480 KASSERT(mtag != NULL, ("Reinjected packet w/o direct ctx tag!"));
482 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
483 nxt = ip6dc->ip6dc_nxt;
484 off = ip6dc->ip6dc_off;
488 m_tag_delete(m, mtag);
490 while (nxt != IPPROTO_DONE) {
491 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
492 IP6STAT_INC(ip6s_toomanyhdr);
497 * protection against faulty packet - there should be
498 * more sanity checks in header chain processing.
500 if (m->m_pkthdr.len < off) {
501 IP6STAT_INC(ip6s_tooshort);
502 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
506 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
507 if (IPSEC_ENABLED(ipv6)) {
508 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
513 nxt = ip6_protox[nxt](&m, &off, nxt);
522 ip6_input(struct mbuf *m)
524 struct in6_addr odst;
526 struct in6_ifaddr *ia;
529 u_int32_t rtalert = ~0;
530 int off = sizeof(struct ip6_hdr), nest;
535 * Drop the packet if IPv6 operation is disabled on the interface.
537 rcvif = m->m_pkthdr.rcvif;
538 if ((ND_IFINFO(rcvif)->flags & ND6_IFF_IFDISABLED))
541 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
543 * should the inner packet be considered authentic?
544 * see comment in ah4_input().
545 * NB: m cannot be NULL when passed to the input routine
548 m->m_flags &= ~M_AUTHIPHDR;
549 m->m_flags &= ~M_AUTHIPDGM;
553 if (m->m_flags & M_FASTFWD_OURS) {
555 * Firewall changed destination to local.
557 ip6 = mtod(m, struct ip6_hdr *);
564 if (m->m_flags & M_EXT) {
566 IP6STAT_INC(ip6s_mext2m);
568 IP6STAT_INC(ip6s_mext1);
571 struct ifnet *ifp = (m->m_flags & M_LOOP) ? V_loif : rcvif;
572 int ifindex = ifp->if_index;
573 if (ifindex >= IP6S_M2MMAX)
575 IP6STAT_INC(ip6s_m2m[ifindex]);
577 IP6STAT_INC(ip6s_m1);
580 in6_ifstat_inc(rcvif, ifs6_in_receive);
581 IP6STAT_INC(ip6s_total);
584 * L2 bridge code and some other code can return mbuf chain
585 * that does not conform to KAME requirement. too bad.
586 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
588 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
591 if (m->m_pkthdr.len > MHLEN)
592 n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
594 n = m_gethdr(M_NOWAIT, MT_DATA);
599 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
600 n->m_len = n->m_pkthdr.len;
604 if (m->m_len < sizeof(struct ip6_hdr)) {
605 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
606 IP6STAT_INC(ip6s_toosmall);
607 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
612 ip6 = mtod(m, struct ip6_hdr *);
613 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
614 IP6STAT_INC(ip6s_badvers);
615 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
619 IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
620 IP_PROBE(receive, NULL, NULL, ip6, rcvif, NULL, ip6);
623 * Check against address spoofing/corruption.
625 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
626 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
628 * XXX: "badscope" is not very suitable for a multicast source.
630 IP6STAT_INC(ip6s_badscope);
631 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
634 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
635 !(m->m_flags & M_LOOP)) {
637 * In this case, the packet should come from the loopback
638 * interface. However, we cannot just check the if_flags,
639 * because ip6_mloopback() passes the "actual" interface
640 * as the outgoing/incoming interface.
642 IP6STAT_INC(ip6s_badscope);
643 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
646 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
647 IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
650 * Nodes must not originate a packet to a multicast address
651 * whose scop field contains the reserved value 0; if such
652 * a packet is received, it must be silently dropped.
654 IP6STAT_INC(ip6s_badscope);
655 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
659 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
660 /* packet is dropped by traffic conditioner */
665 * The following check is not documented in specs. A malicious
666 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
667 * and bypass security checks (act as if it was from 127.0.0.1 by using
668 * IPv6 src ::ffff:127.0.0.1). Be cautious.
670 * We have supported IPv6-only kernels for a few years and this issue
671 * has not come up. The world seems to move mostly towards not using
672 * v4mapped on the wire, so it makes sense for us to keep rejecting
675 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
676 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
677 IP6STAT_INC(ip6s_badscope);
678 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
683 * Reject packets with IPv4 compatible addresses (auto tunnel).
685 * The code forbids auto tunnel relay case in RFC1933 (the check is
686 * stronger than RFC1933). We may want to re-enable it if mech-xx
687 * is revised to forbid relaying case.
689 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
690 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
691 IP6STAT_INC(ip6s_badscope);
692 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
697 * Try to forward the packet, but if we fail continue.
698 * ip6_tryforward() does not generate redirects, so fall
699 * through to normal processing if redirects are required.
700 * ip6_tryforward() does inbound and outbound packet firewall
701 * processing. If firewall has decided that destination becomes
702 * our local address, it sets M_FASTFWD_OURS flag. In this
703 * case skip another inbound firewall processing and update
706 if (V_ip6_forwarding != 0 && V_ip6_sendredirects == 0
707 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
708 && (!IPSEC_ENABLED(ipv6) ||
709 IPSEC_CAPS(ipv6, m, IPSEC_CAP_OPERABLE) == 0)
712 if ((m = ip6_tryforward(m)) == NULL)
714 if (m->m_flags & M_FASTFWD_OURS) {
715 ip6 = mtod(m, struct ip6_hdr *);
719 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
721 * Bypass packet filtering for packets previously handled by IPsec.
723 if (IPSEC_ENABLED(ipv6) &&
724 IPSEC_CAPS(ipv6, m, IPSEC_CAP_BYPASS_FILTER) != 0)
728 * Run through list of hooks for input packets.
730 * NB: Beware of the destination address changing
731 * (e.g. by NAT rewriting). When this happens,
732 * tell ip6_forward to do the right thing.
735 /* Jump over all PFIL processing if hooks are not active. */
736 if (!PFIL_HOOKED_IN(V_inet6_pfil_head))
740 if (pfil_run_hooks(V_inet6_pfil_head, &m, m->m_pkthdr.rcvif, PFIL_IN,
743 ip6 = mtod(m, struct ip6_hdr *);
744 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
745 if ((m->m_flags & (M_IP6_NEXTHOP | M_FASTFWD_OURS)) == M_IP6_NEXTHOP &&
746 m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
748 * Directly ship the packet on. This allows forwarding
749 * packets originally destined to us to some other directly
758 * Disambiguate address scope zones (if there is ambiguity).
759 * We first make sure that the original source or destination address
760 * is not in our internal form for scoped addresses. Such addresses
761 * are not necessarily invalid spec-wise, but we cannot accept them due
762 * to the usage conflict.
763 * in6_setscope() then also checks and rejects the cases where src or
764 * dst are the loopback address and the receiving interface
767 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
768 IP6STAT_INC(ip6s_badscope); /* XXX */
771 if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
772 in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
773 IP6STAT_INC(ip6s_badscope);
776 if (m->m_flags & M_FASTFWD_OURS) {
777 m->m_flags &= ~M_FASTFWD_OURS;
782 * Multicast check. Assume packet is for us to avoid
783 * prematurely taking locks.
785 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
787 in6_ifstat_inc(rcvif, ifs6_in_mcast);
792 * XXX: For now we keep link-local IPv6 addresses with embedded
793 * scope zone id, therefore we use zero zoneid here.
795 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
797 if (ia->ia6_flags & IN6_IFF_NOTREADY) {
798 char ip6bufs[INET6_ADDRSTRLEN];
799 char ip6bufd[INET6_ADDRSTRLEN];
800 /* address is not ready, so discard the packet. */
802 "ip6_input: packet to an unready address %s->%s\n",
803 ip6_sprintf(ip6bufs, &ip6->ip6_src),
804 ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
807 if (V_ip6_sav && !(m->m_flags & M_LOOP) &&
808 __predict_false(in6_localip_fib(&ip6->ip6_src,
810 IP6STAT_INC(ip6s_badscope); /* XXX */
813 /* Count the packet in the ip address stats */
814 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
815 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
821 * Now there is no reason to process the packet if it's not our own
822 * and we're not a router.
824 if (!V_ip6_forwarding) {
825 IP6STAT_INC(ip6s_cantforward);
831 * Process Hop-by-Hop options header if it's contained.
832 * m may be modified in ip6_hopopts_input().
833 * If a JumboPayload option is included, plen will also be modified.
835 plen = (u_int32_t)ntohs(ip6->ip6_plen);
836 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
837 if (ip6_input_hbh(&m, &plen, &rtalert, &off, &nxt, &ours) != 0)
843 * Use mbuf flags to propagate Router Alert option to
844 * ICMPv6 layer, as hop-by-hop options have been stripped.
847 m->m_flags |= M_RTALERT_MLD;
850 * Check that the amount of data in the buffers
851 * is as at least much as the IPv6 header would have us expect.
852 * Trim mbufs if longer than we expect.
853 * Drop packet if shorter than we expect.
855 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
856 IP6STAT_INC(ip6s_tooshort);
857 in6_ifstat_inc(rcvif, ifs6_in_truncated);
860 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
861 if (m->m_len == m->m_pkthdr.len) {
862 m->m_len = sizeof(struct ip6_hdr) + plen;
863 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
865 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
869 * Forward if desirable.
872 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
874 * If we are acting as a multicast router, all
875 * incoming multicast packets are passed to the
876 * kernel-level multicast forwarding function.
877 * The packet is returned (relatively) intact; if
878 * ip6_mforward() returns a non-zero value, the packet
879 * must be discarded, else it may be accepted below.
881 * XXX TODO: Check hlim and multicast scope here to avoid
882 * unnecessarily calling into ip6_mforward().
884 if (ip6_mforward && ip6_mforward(ip6, rcvif, m)) {
885 IP6STAT_INC(ip6s_cantforward);
889 ip6_forward(m, srcrt);
894 * Tell launch routine the next header
896 IP6STAT_INC(ip6s_delivered);
897 in6_ifstat_inc(rcvif, ifs6_in_deliver);
900 while (nxt != IPPROTO_DONE) {
901 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
902 IP6STAT_INC(ip6s_toomanyhdr);
907 * protection against faulty packet - there should be
908 * more sanity checks in header chain processing.
910 if (m->m_pkthdr.len < off) {
911 IP6STAT_INC(ip6s_tooshort);
912 in6_ifstat_inc(rcvif, ifs6_in_truncated);
916 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
917 if (IPSEC_ENABLED(ipv6)) {
918 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
923 nxt = ip6_protox[nxt](&m, &off, nxt);
927 in6_ifstat_inc(rcvif, ifs6_in_discard);
933 * Hop-by-Hop options header processing. If a valid jumbo payload option is
934 * included, the real payload length will be stored in plenp.
936 * rtalertp - XXX: should be stored more smart way
939 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
940 struct mbuf **mp, int *offp)
942 struct mbuf *m = *mp;
943 int off = *offp, hbhlen;
946 /* validation of the length of the header */
947 if (m->m_len < off + sizeof(*hbh)) {
948 m = m_pullup(m, off + sizeof(*hbh));
950 IP6STAT_INC(ip6s_exthdrtoolong);
955 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
956 hbhlen = (hbh->ip6h_len + 1) << 3;
958 if (m->m_len < off + hbhlen) {
959 m = m_pullup(m, off + hbhlen);
961 IP6STAT_INC(ip6s_exthdrtoolong);
966 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
968 hbhlen -= sizeof(struct ip6_hbh);
969 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
970 hbhlen, rtalertp, plenp) < 0) {
981 * Search header for all Hop-by-hop options and process each option.
982 * This function is separate from ip6_hopopts_input() in order to
983 * handle a case where the sending node itself process its hop-by-hop
984 * options header. In such a case, the function is called from ip6_output().
986 * The function assumes that hbh header is located right after the IPv6 header
987 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
988 * opthead + hbhlen is located in contiguous memory region.
991 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
992 u_int32_t *rtalertp, u_int32_t *plenp)
996 u_int8_t *opt = opthead;
997 u_int16_t rtalert_val;
999 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1001 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1007 if (hbhlen < IP6OPT_MINLEN) {
1008 IP6STAT_INC(ip6s_toosmall);
1011 optlen = *(opt + 1) + 2;
1013 case IP6OPT_ROUTER_ALERT:
1014 /* XXX may need check for alignment */
1015 if (hbhlen < IP6OPT_RTALERT_LEN) {
1016 IP6STAT_INC(ip6s_toosmall);
1019 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1021 icmp6_error(m, ICMP6_PARAM_PROB,
1022 ICMP6_PARAMPROB_HEADER,
1023 erroff + opt + 1 - opthead);
1026 optlen = IP6OPT_RTALERT_LEN;
1027 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1028 *rtalertp = ntohs(rtalert_val);
1031 /* XXX may need check for alignment */
1032 if (hbhlen < IP6OPT_JUMBO_LEN) {
1033 IP6STAT_INC(ip6s_toosmall);
1036 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1038 icmp6_error(m, ICMP6_PARAM_PROB,
1039 ICMP6_PARAMPROB_HEADER,
1040 erroff + opt + 1 - opthead);
1043 optlen = IP6OPT_JUMBO_LEN;
1046 * IPv6 packets that have non 0 payload length
1047 * must not contain a jumbo payload option.
1049 ip6 = mtod(m, struct ip6_hdr *);
1050 if (ip6->ip6_plen) {
1051 IP6STAT_INC(ip6s_badoptions);
1052 icmp6_error(m, ICMP6_PARAM_PROB,
1053 ICMP6_PARAMPROB_HEADER,
1054 erroff + opt - opthead);
1059 * We may see jumbolen in unaligned location, so
1060 * we'd need to perform bcopy().
1062 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1063 jumboplen = (u_int32_t)htonl(jumboplen);
1067 * if there are multiple jumbo payload options,
1068 * *plenp will be non-zero and the packet will be
1070 * the behavior may need some debate in ipngwg -
1071 * multiple options does not make sense, however,
1072 * there's no explicit mention in specification.
1075 IP6STAT_INC(ip6s_badoptions);
1076 icmp6_error(m, ICMP6_PARAM_PROB,
1077 ICMP6_PARAMPROB_HEADER,
1078 erroff + opt + 2 - opthead);
1084 * jumbo payload length must be larger than 65535.
1086 if (jumboplen <= IPV6_MAXPACKET) {
1087 IP6STAT_INC(ip6s_badoptions);
1088 icmp6_error(m, ICMP6_PARAM_PROB,
1089 ICMP6_PARAMPROB_HEADER,
1090 erroff + opt + 2 - opthead);
1096 default: /* unknown option */
1097 if (hbhlen < IP6OPT_MINLEN) {
1098 IP6STAT_INC(ip6s_toosmall);
1101 optlen = ip6_unknown_opt(opt, m,
1102 erroff + opt - opthead);
1118 * Unknown option processing.
1119 * The third argument `off' is the offset from the IPv6 header to the option,
1120 * which is necessary if the IPv6 header the and option header and IPv6 header
1121 * is not contiguous in order to return an ICMPv6 error.
1124 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1126 struct ip6_hdr *ip6;
1128 switch (IP6OPT_TYPE(*optp)) {
1129 case IP6OPT_TYPE_SKIP: /* ignore the option */
1130 return ((int)*(optp + 1));
1131 case IP6OPT_TYPE_DISCARD: /* silently discard */
1134 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1135 IP6STAT_INC(ip6s_badoptions);
1136 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1138 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1139 IP6STAT_INC(ip6s_badoptions);
1140 ip6 = mtod(m, struct ip6_hdr *);
1141 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1142 (m->m_flags & (M_BCAST|M_MCAST)))
1145 icmp6_error(m, ICMP6_PARAM_PROB,
1146 ICMP6_PARAMPROB_OPTION, off);
1150 m_freem(m); /* XXX: NOTREACHED */
1155 * Create the "control" list for this pcb.
1156 * These functions will not modify mbuf chain at all.
1158 * The routine will be called from upper layer handlers like tcp6_input().
1159 * Thus the routine assumes that the caller (tcp6_input) have already
1160 * called m_pullup() and all the extension headers are located in the
1161 * very first mbuf on the mbuf chain.
1163 * ip6_savecontrol_v4 will handle those options that are possible to be
1164 * set on a v4-mapped socket.
1165 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1166 * options and handle the v6-only ones itself.
1169 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1172 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1175 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1181 struct bintime boottimebin, bt1;
1182 struct timespec ts1;
1186 switch (inp->inp_socket->so_ts_clock) {
1187 case SO_TS_REALTIME_MICRO:
1188 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1190 mbuf_tstmp2timespec(m, &ts1);
1191 timespec2bintime(&ts1, &bt1);
1192 getboottimebin(&boottimebin);
1193 bintime_add(&bt1, &boottimebin);
1194 bintime2timeval(&bt1, &t.tv);
1198 *mp = sbcreatecontrol(&t.tv, sizeof(t.tv),
1199 SCM_TIMESTAMP, SOL_SOCKET, M_NOWAIT);
1201 mp = &(*mp)->m_next;
1207 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1209 mbuf_tstmp2timespec(m, &ts1);
1210 timespec2bintime(&ts1, &t.bt);
1211 getboottimebin(&boottimebin);
1212 bintime_add(&t.bt, &boottimebin);
1216 *mp = sbcreatecontrol(&t.bt, sizeof(t.bt), SCM_BINTIME,
1217 SOL_SOCKET, M_NOWAIT);
1219 mp = &(*mp)->m_next;
1224 case SO_TS_REALTIME:
1225 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1227 mbuf_tstmp2timespec(m, &t.ts);
1228 getboottimebin(&boottimebin);
1229 bintime2timespec(&boottimebin, &ts1);
1230 timespecadd(&t.ts, &ts1, &t.ts);
1234 *mp = sbcreatecontrol(&t.ts, sizeof(t.ts),
1235 SCM_REALTIME, SOL_SOCKET, M_NOWAIT);
1237 mp = &(*mp)->m_next;
1242 case SO_TS_MONOTONIC:
1243 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1245 mbuf_tstmp2timespec(m, &t.ts);
1248 *mp = sbcreatecontrol(&t.ts, sizeof(t.ts),
1249 SCM_MONOTONIC, SOL_SOCKET, M_NOWAIT);
1251 mp = &(*mp)->m_next;
1257 panic("unknown (corrupted) so_ts_clock");
1259 if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) ==
1260 (M_PKTHDR | M_TSTMP)) {
1261 struct sock_timestamp_info sti;
1263 bzero(&sti, sizeof(sti));
1264 sti.st_info_flags = ST_INFO_HW;
1265 if ((m->m_flags & M_TSTMP_HPREC) != 0)
1266 sti.st_info_flags |= ST_INFO_HW_HPREC;
1267 *mp = sbcreatecontrol(&sti, sizeof(sti), SCM_TIME_INFO,
1268 SOL_SOCKET, M_NOWAIT);
1270 mp = &(*mp)->m_next;
1275 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1276 /* RFC 2292 sec. 5 */
1277 if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1278 struct in6_pktinfo pi6;
1280 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1284 ip = mtod(m, struct ip *);
1285 pi6.ipi6_addr.s6_addr32[0] = 0;
1286 pi6.ipi6_addr.s6_addr32[1] = 0;
1287 pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1288 pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1290 /* We won't hit this code */
1291 bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1294 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1295 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1298 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1300 *mp = sbcreatecontrol(&pi6, sizeof(struct in6_pktinfo),
1301 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6,
1304 mp = &(*mp)->m_next;
1307 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1310 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1314 ip = mtod(m, struct ip *);
1317 /* We won't hit this code */
1321 hlim = ip6->ip6_hlim & 0xff;
1323 *mp = sbcreatecontrol(&hlim, sizeof(int),
1324 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1325 IPPROTO_IPV6, M_NOWAIT);
1327 mp = &(*mp)->m_next;
1330 if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1333 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1337 ip = mtod(m, struct ip *);
1338 tclass = ip->ip_tos;
1340 /* We won't hit this code */
1346 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1348 tclass = flowinfo & 0xff;
1350 *mp = sbcreatecontrol(&tclass, sizeof(int), IPV6_TCLASS,
1351 IPPROTO_IPV6, M_NOWAIT);
1353 mp = &(*mp)->m_next;
1356 if (v4only != NULL) {
1357 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1368 ip6_savecontrol(struct inpcb *inp, struct mbuf *m, struct mbuf **mp)
1370 struct ip6_hdr *ip6;
1373 mp = ip6_savecontrol_v4(inp, m, mp, &v4only);
1377 ip6 = mtod(m, struct ip6_hdr *);
1379 * IPV6_HOPOPTS socket option. Recall that we required super-user
1380 * privilege for the option (see ip6_ctloutput), but it might be too
1381 * strict, since there might be some hop-by-hop options which can be
1382 * returned to normal user.
1383 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1385 if ((inp->inp_flags & IN6P_HOPOPTS) != 0) {
1387 * Check if a hop-by-hop options header is contatined in the
1388 * received packet, and if so, store the options as ancillary
1389 * data. Note that a hop-by-hop options header must be
1390 * just after the IPv6 header, which is assured through the
1391 * IPv6 input processing.
1393 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1394 struct ip6_hbh *hbh;
1397 hbh = (struct ip6_hbh *)(ip6 + 1);
1398 hbhlen = (hbh->ip6h_len + 1) << 3;
1401 * XXX: We copy the whole header even if a
1402 * jumbo payload option is included, the option which
1403 * is to be removed before returning according to
1405 * Note: this constraint is removed in RFC3542
1407 *mp = sbcreatecontrol(hbh, hbhlen,
1408 IS2292(inp, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1409 IPPROTO_IPV6, M_NOWAIT);
1411 mp = &(*mp)->m_next;
1415 if ((inp->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1416 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1419 * Search for destination options headers or routing
1420 * header(s) through the header chain, and stores each
1421 * header as ancillary data.
1422 * Note that the order of the headers remains in
1423 * the chain of ancillary data.
1425 while (1) { /* is explicit loop prevention necessary? */
1426 struct ip6_ext *ip6e = NULL;
1430 * if it is not an extension header, don't try to
1431 * pull it from the chain.
1434 case IPPROTO_DSTOPTS:
1435 case IPPROTO_ROUTING:
1436 case IPPROTO_HOPOPTS:
1437 case IPPROTO_AH: /* is it possible? */
1443 if (off + sizeof(*ip6e) > m->m_len)
1445 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1446 if (nxt == IPPROTO_AH)
1447 elen = (ip6e->ip6e_len + 2) << 2;
1449 elen = (ip6e->ip6e_len + 1) << 3;
1450 if (off + elen > m->m_len)
1454 case IPPROTO_DSTOPTS:
1455 if (!(inp->inp_flags & IN6P_DSTOPTS))
1458 *mp = sbcreatecontrol(ip6e, elen,
1459 IS2292(inp, IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1460 IPPROTO_IPV6, M_NOWAIT);
1462 mp = &(*mp)->m_next;
1464 case IPPROTO_ROUTING:
1465 if (!(inp->inp_flags & IN6P_RTHDR))
1468 *mp = sbcreatecontrol(ip6e, elen,
1469 IS2292(inp, IPV6_2292RTHDR, IPV6_RTHDR),
1470 IPPROTO_IPV6, M_NOWAIT);
1472 mp = &(*mp)->m_next;
1474 case IPPROTO_HOPOPTS:
1475 case IPPROTO_AH: /* is it possible? */
1480 * other cases have been filtered in the above.
1481 * none will visit this case. here we supply
1482 * the code just in case (nxt overwritten or
1488 /* proceed with the next header. */
1490 nxt = ip6e->ip6e_nxt;
1497 if (inp->inp_flags2 & INP_RECVFLOWID) {
1498 uint32_t flowid, flow_type;
1500 flowid = m->m_pkthdr.flowid;
1501 flow_type = M_HASHTYPE_GET(m);
1504 * XXX should handle the failure of one or the
1505 * other - don't populate both?
1507 *mp = sbcreatecontrol(&flowid, sizeof(uint32_t), IPV6_FLOWID,
1508 IPPROTO_IPV6, M_NOWAIT);
1510 mp = &(*mp)->m_next;
1511 *mp = sbcreatecontrol(&flow_type, sizeof(uint32_t),
1512 IPV6_FLOWTYPE, IPPROTO_IPV6, M_NOWAIT);
1514 mp = &(*mp)->m_next;
1518 if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1519 uint32_t flowid, flow_type;
1520 uint32_t rss_bucketid;
1522 flowid = m->m_pkthdr.flowid;
1523 flow_type = M_HASHTYPE_GET(m);
1525 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1526 *mp = sbcreatecontrol(&rss_bucketid, sizeof(uint32_t),
1527 IPV6_RSSBUCKETID, IPPROTO_IPV6, M_NOWAIT);
1529 mp = &(*mp)->m_next;
1538 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu)
1542 struct ip6_mtuinfo mtuctl;
1544 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1546 * Notify the error by sending IPV6_PATHMTU ancillary data if
1547 * application wanted to know the MTU value.
1548 * NOTE: we notify disconnected sockets, because some udp
1549 * applications keep sending sockets disconnected.
1550 * NOTE: our implementation doesn't notify connected sockets that has
1551 * foreign address that is different than given destination addresses
1552 * (this is permitted by RFC 3542).
1554 if ((inp->inp_flags & IN6P_MTU) == 0 || (
1555 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1556 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr)))
1559 mtuctl.ip6m_mtu = mtu;
1560 mtuctl.ip6m_addr = *dst;
1561 if (sa6_recoverscope(&mtuctl.ip6m_addr))
1564 if ((m_mtu = sbcreatecontrol(&mtuctl, sizeof(mtuctl), IPV6_PATHMTU,
1565 IPPROTO_IPV6, M_NOWAIT)) == NULL)
1568 so = inp->inp_socket;
1569 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1573 /* XXX: should count statistics */
1579 * Get pointer to the previous header followed by the header
1580 * currently processed.
1583 ip6_get_prevhdr(const struct mbuf *m, int off)
1585 struct ip6_ext ip6e;
1586 struct ip6_hdr *ip6;
1589 if (off == sizeof(struct ip6_hdr))
1590 return (offsetof(struct ip6_hdr, ip6_nxt));
1591 if (off < sizeof(struct ip6_hdr))
1592 panic("%s: off < sizeof(struct ip6_hdr)", __func__);
1594 ip6 = mtod(m, struct ip6_hdr *);
1596 len = sizeof(struct ip6_hdr);
1599 m_copydata(m, len, sizeof(ip6e), (caddr_t)&ip6e);
1601 case IPPROTO_FRAGMENT:
1602 nlen = sizeof(struct ip6_frag);
1605 nlen = (ip6e.ip6e_len + 2) << 2;
1608 nlen = (ip6e.ip6e_len + 1) << 3;
1611 nxt = ip6e.ip6e_nxt;
1613 return (len - nlen);
1617 * get next header offset. m will be retained.
1620 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1623 struct ip6_ext ip6e;
1628 panic("ip6_nexthdr: m == NULL");
1629 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1634 if (m->m_pkthdr.len < off + sizeof(ip6))
1636 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1638 *nxtp = ip6.ip6_nxt;
1642 case IPPROTO_FRAGMENT:
1644 * terminate parsing if it is not the first fragment,
1645 * it does not make sense to parse through it.
1647 if (m->m_pkthdr.len < off + sizeof(fh))
1649 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1650 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1651 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1654 *nxtp = fh.ip6f_nxt;
1655 off += sizeof(struct ip6_frag);
1659 if (m->m_pkthdr.len < off + sizeof(ip6e))
1661 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1663 *nxtp = ip6e.ip6e_nxt;
1664 off += (ip6e.ip6e_len + 2) << 2;
1667 case IPPROTO_HOPOPTS:
1668 case IPPROTO_ROUTING:
1669 case IPPROTO_DSTOPTS:
1670 if (m->m_pkthdr.len < off + sizeof(ip6e))
1672 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1674 *nxtp = ip6e.ip6e_nxt;
1675 off += (ip6e.ip6e_len + 1) << 3;
1680 case IPPROTO_IPCOMP:
1692 * get offset for the last header in the chain. m will be kept untainted.
1695 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1705 newoff = ip6_nexthdr(m, off, proto, nxtp);
1708 else if (newoff < off)
1709 return -1; /* invalid */
1710 else if (newoff == off)
1719 * System control for IP6
1722 u_char inet6ctlerrmap[PRC_NCMDS] = {
1724 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1725 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1726 EMSGSIZE, EHOSTUNREACH, 0, 0,
1727 0, 0, EHOSTUNREACH, 0,
1728 ENOPROTOOPT, ECONNREFUSED