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");
167 static struct netisr_handler ip6_direct_nh = {
168 .nh_name = "ip6_direct",
169 .nh_handler = ip6_direct_input,
170 .nh_proto = NETISR_IPV6_DIRECT,
171 .nh_m2cpuid = rss_soft_m2cpuid_v6,
172 .nh_policy = NETISR_POLICY_CPU,
173 .nh_dispatch = NETISR_DISPATCH_HYBRID,
177 sysctl_netinet6_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
181 netisr_getqlimit(&ip6_direct_nh, &qlimit);
182 error = sysctl_handle_int(oidp, &qlimit, 0, req);
183 if (error || !req->newptr)
187 return (netisr_setqlimit(&ip6_direct_nh, qlimit));
189 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
190 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
191 0, 0, sysctl_netinet6_intr_direct_queue_maxlen, "I",
192 "Maximum size of the IPv6 direct input queue");
196 VNET_DEFINE(pfil_head_t, inet6_pfil_head);
198 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
199 VNET_PCPUSTAT_SYSINIT(ip6stat);
201 VNET_PCPUSTAT_SYSUNINIT(ip6stat);
204 struct rmlock in6_ifaddr_lock;
205 RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
207 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
210 * IP6 initialization: fill in IP6 protocol switch table.
211 * All protocols not implemented in kernel go to raw IP6 protocol handler.
216 struct pfil_head_args args;
220 TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
221 &V_ip6_auto_linklocal);
222 TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
223 TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
225 CK_STAILQ_INIT(&V_in6_ifaddrhead);
226 V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
229 /* Initialize packet filter hooks. */
230 args.pa_version = PFIL_VERSION;
231 args.pa_flags = PFIL_IN | PFIL_OUT;
232 args.pa_type = PFIL_TYPE_IP6;
233 args.pa_headname = PFIL_INET6_NAME;
234 V_inet6_pfil_head = pfil_head_register(&args);
236 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6,
237 &V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
238 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
239 printf("%s: WARNING: unable to register input helper hook\n",
241 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6,
242 &V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
243 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
244 printf("%s: WARNING: unable to register output helper hook\n",
248 addrsel_policy_init();
252 V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
254 /* Skip global initialization stuff for non-default instances. */
256 if (!IS_DEFAULT_VNET(curvnet)) {
257 netisr_register_vnet(&ip6_nh);
259 netisr_register_vnet(&ip6_direct_nh);
265 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
269 /* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
270 for (i = 0; i < IPPROTO_MAX; i++)
271 ip6_protox[i] = pr - inet6sw;
273 * Cycle through IP protocols and put them into the appropriate place
276 for (pr = inet6domain.dom_protosw;
277 pr < inet6domain.dom_protoswNPROTOSW; pr++)
278 if (pr->pr_domain->dom_family == PF_INET6 &&
279 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
280 /* Be careful to only index valid IP protocols. */
281 if (pr->pr_protocol < IPPROTO_MAX)
282 ip6_protox[pr->pr_protocol] = pr - inet6sw;
285 netisr_register(&ip6_nh);
287 netisr_register(&ip6_direct_nh);
292 * The protocol to be inserted into ip6_protox[] must be already registered
293 * in inet6sw[], either statically or through pf_proto_register().
296 ip6proto_register(short ip6proto)
301 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
302 return (EPROTONOSUPPORT);
305 * The protocol slot must not be occupied by another protocol
306 * already. An index pointing to IPPROTO_RAW is unused.
308 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
310 return (EPFNOSUPPORT);
311 if (ip6_protox[ip6proto] != pr - inet6sw) /* IPPROTO_RAW */
315 * Find the protocol position in inet6sw[] and set the index.
317 for (pr = inet6domain.dom_protosw;
318 pr < inet6domain.dom_protoswNPROTOSW; pr++) {
319 if (pr->pr_domain->dom_family == PF_INET6 &&
320 pr->pr_protocol && pr->pr_protocol == ip6proto) {
321 ip6_protox[pr->pr_protocol] = pr - inet6sw;
325 return (EPROTONOSUPPORT);
329 ip6proto_unregister(short ip6proto)
334 if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
335 return (EPROTONOSUPPORT);
337 /* Check if the protocol was indeed registered. */
338 pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
340 return (EPFNOSUPPORT);
341 if (ip6_protox[ip6proto] == pr - inet6sw) /* IPPROTO_RAW */
344 /* Reset the protocol slot to IPPROTO_RAW. */
345 ip6_protox[ip6proto] = pr - inet6sw;
351 ip6_destroy(void *unused __unused)
353 struct ifaddr *ifa, *nifa;
358 netisr_unregister_vnet(&ip6_direct_nh);
360 netisr_unregister_vnet(&ip6_nh);
362 pfil_head_unregister(V_inet6_pfil_head);
363 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]);
365 printf("%s: WARNING: unable to deregister input helper hook "
366 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: "
367 "error %d returned\n", __func__, error);
369 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]);
371 printf("%s: WARNING: unable to deregister output helper hook "
372 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: "
373 "error %d returned\n", __func__, error);
376 /* Cleanup addresses. */
378 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
379 /* Cannot lock here - lock recursion. */
380 /* IF_ADDR_LOCK(ifp); */
381 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
382 if (ifa->ifa_addr->sa_family != AF_INET6)
386 /* IF_ADDR_UNLOCK(ifp); */
387 in6_ifdetach_destroy(ifp);
388 mld_domifdetach(ifp);
389 /* Make sure any routes are gone as well. */
390 rt_flushifroutes_af(ifp, AF_INET6);
396 in6_ifattach_destroy();
398 hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
401 VNET_SYSUNINIT(inet6, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_destroy, NULL);
405 ip6_input_hbh(struct mbuf **mp, uint32_t *plen, uint32_t *rtalert, int *off,
412 if (ip6_hopopts_input(plen, rtalert, mp, off)) {
413 #if 0 /*touches NULL pointer*/
414 in6_ifstat_inc((*mp)->m_pkthdr.rcvif, ifs6_in_discard);
416 goto out; /* m have already been freed */
421 ip6 = mtod(m, struct ip6_hdr *);
424 * if the payload length field is 0 and the next header field
425 * indicates Hop-by-Hop Options header, then a Jumbo Payload
426 * option MUST be included.
428 if (ip6->ip6_plen == 0 && *plen == 0) {
430 * Note that if a valid jumbo payload option is
431 * contained, ip6_hopopts_input() must set a valid
432 * (non-zero) payload length to the variable plen.
434 IP6STAT_INC(ip6s_badoptions);
435 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
436 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
437 icmp6_error(m, ICMP6_PARAM_PROB,
438 ICMP6_PARAMPROB_HEADER,
439 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
442 /* ip6_hopopts_input() ensures that mbuf is contiguous */
443 hbh = (struct ip6_hbh *)(ip6 + 1);
444 *nxt = hbh->ip6h_nxt;
447 * If we are acting as a router and the packet contains a
448 * router alert option, see if we know the option value.
449 * Currently, we only support the option value for MLD, in which
450 * case we should pass the packet to the multicast routing
453 if (*rtalert != ~0) {
455 case IP6OPT_RTALERT_MLD:
456 if (V_ip6_forwarding)
461 * RFC2711 requires unrecognized values must be
476 * IPv6 direct input routine.
478 * This is called when reinjecting completed fragments where
479 * all of the previous checking and book-keeping has been done.
482 ip6_direct_input(struct mbuf *m)
487 struct ip6_direct_ctx *ip6dc;
489 mtag = m_tag_locate(m, MTAG_ABI_IPV6, IPV6_TAG_DIRECT, NULL);
490 KASSERT(mtag != NULL, ("Reinjected packet w/o direct ctx tag!"));
492 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
493 nxt = ip6dc->ip6dc_nxt;
494 off = ip6dc->ip6dc_off;
498 m_tag_delete(m, mtag);
500 while (nxt != IPPROTO_DONE) {
501 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
502 IP6STAT_INC(ip6s_toomanyhdr);
507 * protection against faulty packet - there should be
508 * more sanity checks in header chain processing.
510 if (m->m_pkthdr.len < off) {
511 IP6STAT_INC(ip6s_tooshort);
512 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
516 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
517 if (IPSEC_ENABLED(ipv6)) {
518 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
523 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
532 ip6_input(struct mbuf *m)
534 struct in6_addr odst;
536 struct in6_ifaddr *ia;
539 u_int32_t rtalert = ~0;
540 int off = sizeof(struct ip6_hdr), nest;
545 * Drop the packet if IPv6 operation is disabled on the interface.
547 rcvif = m->m_pkthdr.rcvif;
548 if ((ND_IFINFO(rcvif)->flags & ND6_IFF_IFDISABLED))
551 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
553 * should the inner packet be considered authentic?
554 * see comment in ah4_input().
555 * NB: m cannot be NULL when passed to the input routine
558 m->m_flags &= ~M_AUTHIPHDR;
559 m->m_flags &= ~M_AUTHIPDGM;
563 if (m->m_flags & M_FASTFWD_OURS) {
565 * Firewall changed destination to local.
567 ip6 = mtod(m, struct ip6_hdr *);
574 if (m->m_flags & M_EXT) {
576 IP6STAT_INC(ip6s_mext2m);
578 IP6STAT_INC(ip6s_mext1);
581 if (m->m_flags & M_LOOP) {
582 IP6STAT_INC(ip6s_m2m[V_loif->if_index]);
583 } else if (rcvif->if_index < IP6S_M2MMAX)
584 IP6STAT_INC(ip6s_m2m[rcvif->if_index]);
586 IP6STAT_INC(ip6s_m2m[0]);
588 IP6STAT_INC(ip6s_m1);
591 in6_ifstat_inc(rcvif, ifs6_in_receive);
592 IP6STAT_INC(ip6s_total);
595 * L2 bridge code and some other code can return mbuf chain
596 * that does not conform to KAME requirement. too bad.
597 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
599 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
602 if (m->m_pkthdr.len > MHLEN)
603 n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
605 n = m_gethdr(M_NOWAIT, MT_DATA);
610 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
611 n->m_len = n->m_pkthdr.len;
615 if (m->m_len < sizeof(struct ip6_hdr)) {
616 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
617 IP6STAT_INC(ip6s_toosmall);
618 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
623 ip6 = mtod(m, struct ip6_hdr *);
624 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
625 IP6STAT_INC(ip6s_badvers);
626 in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
630 IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
631 IP_PROBE(receive, NULL, NULL, ip6, rcvif, NULL, ip6);
634 * Check against address spoofing/corruption.
636 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
637 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
639 * XXX: "badscope" is not very suitable for a multicast source.
641 IP6STAT_INC(ip6s_badscope);
642 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
645 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
646 !(m->m_flags & M_LOOP)) {
648 * In this case, the packet should come from the loopback
649 * interface. However, we cannot just check the if_flags,
650 * because ip6_mloopback() passes the "actual" interface
651 * as the outgoing/incoming interface.
653 IP6STAT_INC(ip6s_badscope);
654 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
657 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
658 IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
661 * Nodes must not originate a packet to a multicast address
662 * whose scop field contains the reserved value 0; if such
663 * a packet is received, it must be silently dropped.
665 IP6STAT_INC(ip6s_badscope);
666 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
670 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
671 /* packet is dropped by traffic conditioner */
676 * The following check is not documented in specs. A malicious
677 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
678 * and bypass security checks (act as if it was from 127.0.0.1 by using
679 * IPv6 src ::ffff:127.0.0.1). Be cautious.
681 * We have supported IPv6-only kernels for a few years and this issue
682 * has not come up. The world seems to move mostly towards not using
683 * v4mapped on the wire, so it makes sense for us to keep rejecting
686 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
687 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
688 IP6STAT_INC(ip6s_badscope);
689 in6_ifstat_inc(rcvif, ifs6_in_addrerr);
694 * Reject packets with IPv4 compatible addresses (auto tunnel).
696 * The code forbids auto tunnel relay case in RFC1933 (the check is
697 * stronger than RFC1933). We may want to re-enable it if mech-xx
698 * is revised to forbid relaying case.
700 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
701 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
702 IP6STAT_INC(ip6s_badscope);
703 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
708 * Try to forward the packet, but if we fail continue.
709 * ip6_tryforward() does not generate redirects, so fall
710 * through to normal processing if redirects are required.
711 * ip6_tryforward() does inbound and outbound packet firewall
712 * processing. If firewall has decided that destination becomes
713 * our local address, it sets M_FASTFWD_OURS flag. In this
714 * case skip another inbound firewall processing and update
717 if (V_ip6_forwarding != 0 && V_ip6_sendredirects == 0
718 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
719 && (!IPSEC_ENABLED(ipv6) ||
720 IPSEC_CAPS(ipv6, m, IPSEC_CAP_OPERABLE) == 0)
723 if ((m = ip6_tryforward(m)) == NULL)
725 if (m->m_flags & M_FASTFWD_OURS) {
726 ip6 = mtod(m, struct ip6_hdr *);
730 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
732 * Bypass packet filtering for packets previously handled by IPsec.
734 if (IPSEC_ENABLED(ipv6) &&
735 IPSEC_CAPS(ipv6, m, IPSEC_CAP_BYPASS_FILTER) != 0)
739 * Run through list of hooks for input packets.
741 * NB: Beware of the destination address changing
742 * (e.g. by NAT rewriting). When this happens,
743 * tell ip6_forward to do the right thing.
746 /* Jump over all PFIL processing if hooks are not active. */
747 if (!PFIL_HOOKED_IN(V_inet6_pfil_head))
751 if (pfil_run_hooks(V_inet6_pfil_head, &m, m->m_pkthdr.rcvif, PFIL_IN,
754 ip6 = mtod(m, struct ip6_hdr *);
755 srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
756 if ((m->m_flags & (M_IP6_NEXTHOP | M_FASTFWD_OURS)) == M_IP6_NEXTHOP &&
757 m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
759 * Directly ship the packet on. This allows forwarding
760 * packets originally destined to us to some other directly
769 * Disambiguate address scope zones (if there is ambiguity).
770 * We first make sure that the original source or destination address
771 * is not in our internal form for scoped addresses. Such addresses
772 * are not necessarily invalid spec-wise, but we cannot accept them due
773 * to the usage conflict.
774 * in6_setscope() then also checks and rejects the cases where src or
775 * dst are the loopback address and the receiving interface
778 if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
779 IP6STAT_INC(ip6s_badscope); /* XXX */
782 if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
783 in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
784 IP6STAT_INC(ip6s_badscope);
787 if (m->m_flags & M_FASTFWD_OURS) {
788 m->m_flags &= ~M_FASTFWD_OURS;
793 * Multicast check. Assume packet is for us to avoid
794 * prematurely taking locks.
796 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
798 in6_ifstat_inc(rcvif, ifs6_in_mcast);
803 * XXX: For now we keep link-local IPv6 addresses with embedded
804 * scope zone id, therefore we use zero zoneid here.
806 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
808 if (ia->ia6_flags & IN6_IFF_NOTREADY) {
809 char ip6bufs[INET6_ADDRSTRLEN];
810 char ip6bufd[INET6_ADDRSTRLEN];
811 /* address is not ready, so discard the packet. */
813 "ip6_input: packet to an unready address %s->%s\n",
814 ip6_sprintf(ip6bufs, &ip6->ip6_src),
815 ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
816 ifa_free(&ia->ia_ifa);
819 /* Count the packet in the ip address stats */
820 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
821 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
822 ifa_free(&ia->ia_ifa);
828 * Now there is no reason to process the packet if it's not our own
829 * and we're not a router.
831 if (!V_ip6_forwarding) {
832 IP6STAT_INC(ip6s_cantforward);
838 * Process Hop-by-Hop options header if it's contained.
839 * m may be modified in ip6_hopopts_input().
840 * If a JumboPayload option is included, plen will also be modified.
842 plen = (u_int32_t)ntohs(ip6->ip6_plen);
843 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
844 if (ip6_input_hbh(&m, &plen, &rtalert, &off, &nxt, &ours) != 0)
850 * Use mbuf flags to propagate Router Alert option to
851 * ICMPv6 layer, as hop-by-hop options have been stripped.
854 m->m_flags |= M_RTALERT_MLD;
857 * Check that the amount of data in the buffers
858 * is as at least much as the IPv6 header would have us expect.
859 * Trim mbufs if longer than we expect.
860 * Drop packet if shorter than we expect.
862 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
863 IP6STAT_INC(ip6s_tooshort);
864 in6_ifstat_inc(rcvif, ifs6_in_truncated);
867 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
868 if (m->m_len == m->m_pkthdr.len) {
869 m->m_len = sizeof(struct ip6_hdr) + plen;
870 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
872 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
876 * Forward if desirable.
879 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
881 * If we are acting as a multicast router, all
882 * incoming multicast packets are passed to the
883 * kernel-level multicast forwarding function.
884 * The packet is returned (relatively) intact; if
885 * ip6_mforward() returns a non-zero value, the packet
886 * must be discarded, else it may be accepted below.
888 * XXX TODO: Check hlim and multicast scope here to avoid
889 * unnecessarily calling into ip6_mforward().
891 if (ip6_mforward && ip6_mforward(ip6, rcvif, m)) {
892 IP6STAT_INC(ip6s_cantforward);
896 ip6_forward(m, srcrt);
901 * Tell launch routine the next header
903 IP6STAT_INC(ip6s_delivered);
904 in6_ifstat_inc(rcvif, ifs6_in_deliver);
907 while (nxt != IPPROTO_DONE) {
908 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
909 IP6STAT_INC(ip6s_toomanyhdr);
914 * protection against faulty packet - there should be
915 * more sanity checks in header chain processing.
917 if (m->m_pkthdr.len < off) {
918 IP6STAT_INC(ip6s_tooshort);
919 in6_ifstat_inc(rcvif, ifs6_in_truncated);
923 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
924 if (IPSEC_ENABLED(ipv6)) {
925 if (IPSEC_INPUT(ipv6, m, off, nxt) != 0)
930 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
934 in6_ifstat_inc(rcvif, ifs6_in_discard);
940 * Hop-by-Hop options header processing. If a valid jumbo payload option is
941 * included, the real payload length will be stored in plenp.
943 * rtalertp - XXX: should be stored more smart way
946 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
947 struct mbuf **mp, int *offp)
949 struct mbuf *m = *mp;
950 int off = *offp, hbhlen;
953 /* validation of the length of the header */
954 if (m->m_len < off + sizeof(*hbh)) {
955 m = m_pullup(m, off + sizeof(*hbh));
957 IP6STAT_INC(ip6s_exthdrtoolong);
962 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
963 hbhlen = (hbh->ip6h_len + 1) << 3;
965 if (m->m_len < off + hbhlen) {
966 m = m_pullup(m, off + hbhlen);
968 IP6STAT_INC(ip6s_exthdrtoolong);
973 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
975 hbhlen -= sizeof(struct ip6_hbh);
976 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
977 hbhlen, rtalertp, plenp) < 0) {
988 * Search header for all Hop-by-hop options and process each option.
989 * This function is separate from ip6_hopopts_input() in order to
990 * handle a case where the sending node itself process its hop-by-hop
991 * options header. In such a case, the function is called from ip6_output().
993 * The function assumes that hbh header is located right after the IPv6 header
994 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
995 * opthead + hbhlen is located in contiguous memory region.
998 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
999 u_int32_t *rtalertp, u_int32_t *plenp)
1001 struct ip6_hdr *ip6;
1003 u_int8_t *opt = opthead;
1004 u_int16_t rtalert_val;
1005 u_int32_t jumboplen;
1006 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1008 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1014 if (hbhlen < IP6OPT_MINLEN) {
1015 IP6STAT_INC(ip6s_toosmall);
1018 optlen = *(opt + 1) + 2;
1020 case IP6OPT_ROUTER_ALERT:
1021 /* XXX may need check for alignment */
1022 if (hbhlen < IP6OPT_RTALERT_LEN) {
1023 IP6STAT_INC(ip6s_toosmall);
1026 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1028 icmp6_error(m, ICMP6_PARAM_PROB,
1029 ICMP6_PARAMPROB_HEADER,
1030 erroff + opt + 1 - opthead);
1033 optlen = IP6OPT_RTALERT_LEN;
1034 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1035 *rtalertp = ntohs(rtalert_val);
1038 /* XXX may need check for alignment */
1039 if (hbhlen < IP6OPT_JUMBO_LEN) {
1040 IP6STAT_INC(ip6s_toosmall);
1043 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1045 icmp6_error(m, ICMP6_PARAM_PROB,
1046 ICMP6_PARAMPROB_HEADER,
1047 erroff + opt + 1 - opthead);
1050 optlen = IP6OPT_JUMBO_LEN;
1053 * IPv6 packets that have non 0 payload length
1054 * must not contain a jumbo payload option.
1056 ip6 = mtod(m, struct ip6_hdr *);
1057 if (ip6->ip6_plen) {
1058 IP6STAT_INC(ip6s_badoptions);
1059 icmp6_error(m, ICMP6_PARAM_PROB,
1060 ICMP6_PARAMPROB_HEADER,
1061 erroff + opt - opthead);
1066 * We may see jumbolen in unaligned location, so
1067 * we'd need to perform bcopy().
1069 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1070 jumboplen = (u_int32_t)htonl(jumboplen);
1074 * if there are multiple jumbo payload options,
1075 * *plenp will be non-zero and the packet will be
1077 * the behavior may need some debate in ipngwg -
1078 * multiple options does not make sense, however,
1079 * there's no explicit mention in specification.
1082 IP6STAT_INC(ip6s_badoptions);
1083 icmp6_error(m, ICMP6_PARAM_PROB,
1084 ICMP6_PARAMPROB_HEADER,
1085 erroff + opt + 2 - opthead);
1091 * jumbo payload length must be larger than 65535.
1093 if (jumboplen <= IPV6_MAXPACKET) {
1094 IP6STAT_INC(ip6s_badoptions);
1095 icmp6_error(m, ICMP6_PARAM_PROB,
1096 ICMP6_PARAMPROB_HEADER,
1097 erroff + opt + 2 - opthead);
1103 default: /* unknown option */
1104 if (hbhlen < IP6OPT_MINLEN) {
1105 IP6STAT_INC(ip6s_toosmall);
1108 optlen = ip6_unknown_opt(opt, m,
1109 erroff + opt - opthead);
1125 * Unknown option processing.
1126 * The third argument `off' is the offset from the IPv6 header to the option,
1127 * which is necessary if the IPv6 header the and option header and IPv6 header
1128 * is not contiguous in order to return an ICMPv6 error.
1131 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1133 struct ip6_hdr *ip6;
1135 switch (IP6OPT_TYPE(*optp)) {
1136 case IP6OPT_TYPE_SKIP: /* ignore the option */
1137 return ((int)*(optp + 1));
1138 case IP6OPT_TYPE_DISCARD: /* silently discard */
1141 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1142 IP6STAT_INC(ip6s_badoptions);
1143 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1145 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1146 IP6STAT_INC(ip6s_badoptions);
1147 ip6 = mtod(m, struct ip6_hdr *);
1148 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1149 (m->m_flags & (M_BCAST|M_MCAST)))
1152 icmp6_error(m, ICMP6_PARAM_PROB,
1153 ICMP6_PARAMPROB_OPTION, off);
1157 m_freem(m); /* XXX: NOTREACHED */
1162 * Create the "control" list for this pcb.
1163 * These functions will not modify mbuf chain at all.
1165 * The routine will be called from upper layer handlers like tcp6_input().
1166 * Thus the routine assumes that the caller (tcp6_input) have already
1167 * called m_pullup() and all the extension headers are located in the
1168 * very first mbuf on the mbuf chain.
1170 * ip6_savecontrol_v4 will handle those options that are possible to be
1171 * set on a v4-mapped socket.
1172 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1173 * options and handle the v6-only ones itself.
1176 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1179 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1182 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1188 struct bintime boottimebin, bt1;
1189 struct timespec ts1;
1193 switch (inp->inp_socket->so_ts_clock) {
1194 case SO_TS_REALTIME_MICRO:
1195 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1197 mbuf_tstmp2timespec(m, &ts1);
1198 timespec2bintime(&ts1, &bt1);
1199 getboottimebin(&boottimebin);
1200 bintime_add(&bt1, &boottimebin);
1201 bintime2timeval(&bt1, &t.tv);
1205 *mp = sbcreatecontrol((caddr_t) &t.tv, sizeof(t.tv),
1206 SCM_TIMESTAMP, SOL_SOCKET);
1208 mp = &(*mp)->m_next;
1214 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1216 mbuf_tstmp2timespec(m, &ts1);
1217 timespec2bintime(&ts1, &t.bt);
1218 getboottimebin(&boottimebin);
1219 bintime_add(&t.bt, &boottimebin);
1223 *mp = sbcreatecontrol((caddr_t)&t.bt, sizeof(t.bt),
1224 SCM_BINTIME, SOL_SOCKET);
1226 mp = &(*mp)->m_next;
1231 case SO_TS_REALTIME:
1232 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1234 mbuf_tstmp2timespec(m, &t.ts);
1235 getboottimebin(&boottimebin);
1236 bintime2timespec(&boottimebin, &ts1);
1237 timespecadd(&t.ts, &ts1, &t.ts);
1241 *mp = sbcreatecontrol((caddr_t)&t.ts, sizeof(t.ts),
1242 SCM_REALTIME, SOL_SOCKET);
1244 mp = &(*mp)->m_next;
1249 case SO_TS_MONOTONIC:
1250 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1252 mbuf_tstmp2timespec(m, &t.ts);
1255 *mp = sbcreatecontrol((caddr_t)&t.ts, sizeof(t.ts),
1256 SCM_MONOTONIC, SOL_SOCKET);
1258 mp = &(*mp)->m_next;
1264 panic("unknown (corrupted) so_ts_clock");
1266 if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) ==
1267 (M_PKTHDR | M_TSTMP)) {
1268 struct sock_timestamp_info sti;
1270 bzero(&sti, sizeof(sti));
1271 sti.st_info_flags = ST_INFO_HW;
1272 if ((m->m_flags & M_TSTMP_HPREC) != 0)
1273 sti.st_info_flags |= ST_INFO_HW_HPREC;
1274 *mp = sbcreatecontrol((caddr_t)&sti, sizeof(sti),
1275 SCM_TIME_INFO, SOL_SOCKET);
1277 mp = &(*mp)->m_next;
1282 #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1283 /* RFC 2292 sec. 5 */
1284 if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1285 struct in6_pktinfo pi6;
1287 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1291 ip = mtod(m, struct ip *);
1292 pi6.ipi6_addr.s6_addr32[0] = 0;
1293 pi6.ipi6_addr.s6_addr32[1] = 0;
1294 pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1295 pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1297 /* We won't hit this code */
1298 bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1301 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1302 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1305 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1307 *mp = sbcreatecontrol((caddr_t) &pi6,
1308 sizeof(struct in6_pktinfo),
1309 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1311 mp = &(*mp)->m_next;
1314 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1317 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1321 ip = mtod(m, struct ip *);
1324 /* We won't hit this code */
1328 hlim = ip6->ip6_hlim & 0xff;
1330 *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1331 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1334 mp = &(*mp)->m_next;
1337 if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1340 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1344 ip = mtod(m, struct ip *);
1345 tclass = ip->ip_tos;
1347 /* We won't hit this code */
1353 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1355 tclass = flowinfo & 0xff;
1357 *mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int),
1358 IPV6_TCLASS, IPPROTO_IPV6);
1360 mp = &(*mp)->m_next;
1363 if (v4only != NULL) {
1364 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1375 ip6_savecontrol(struct inpcb *inp, struct mbuf *m, struct mbuf **mp)
1377 struct ip6_hdr *ip6;
1380 mp = ip6_savecontrol_v4(inp, m, mp, &v4only);
1384 ip6 = mtod(m, struct ip6_hdr *);
1386 * IPV6_HOPOPTS socket option. Recall that we required super-user
1387 * privilege for the option (see ip6_ctloutput), but it might be too
1388 * strict, since there might be some hop-by-hop options which can be
1389 * returned to normal user.
1390 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1392 if ((inp->inp_flags & IN6P_HOPOPTS) != 0) {
1394 * Check if a hop-by-hop options header is contatined in the
1395 * received packet, and if so, store the options as ancillary
1396 * data. Note that a hop-by-hop options header must be
1397 * just after the IPv6 header, which is assured through the
1398 * IPv6 input processing.
1400 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1401 struct ip6_hbh *hbh;
1404 hbh = (struct ip6_hbh *)(ip6 + 1);
1405 hbhlen = (hbh->ip6h_len + 1) << 3;
1408 * XXX: We copy the whole header even if a
1409 * jumbo payload option is included, the option which
1410 * is to be removed before returning according to
1412 * Note: this constraint is removed in RFC3542
1414 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1415 IS2292(inp, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1418 mp = &(*mp)->m_next;
1422 if ((inp->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1423 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1426 * Search for destination options headers or routing
1427 * header(s) through the header chain, and stores each
1428 * header as ancillary data.
1429 * Note that the order of the headers remains in
1430 * the chain of ancillary data.
1432 while (1) { /* is explicit loop prevention necessary? */
1433 struct ip6_ext *ip6e = NULL;
1437 * if it is not an extension header, don't try to
1438 * pull it from the chain.
1441 case IPPROTO_DSTOPTS:
1442 case IPPROTO_ROUTING:
1443 case IPPROTO_HOPOPTS:
1444 case IPPROTO_AH: /* is it possible? */
1450 if (off + sizeof(*ip6e) > m->m_len)
1452 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1453 if (nxt == IPPROTO_AH)
1454 elen = (ip6e->ip6e_len + 2) << 2;
1456 elen = (ip6e->ip6e_len + 1) << 3;
1457 if (off + elen > m->m_len)
1461 case IPPROTO_DSTOPTS:
1462 if (!(inp->inp_flags & IN6P_DSTOPTS))
1465 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1467 IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1470 mp = &(*mp)->m_next;
1472 case IPPROTO_ROUTING:
1473 if (!(inp->inp_flags & IN6P_RTHDR))
1476 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1477 IS2292(inp, IPV6_2292RTHDR, IPV6_RTHDR),
1480 mp = &(*mp)->m_next;
1482 case IPPROTO_HOPOPTS:
1483 case IPPROTO_AH: /* is it possible? */
1488 * other cases have been filtered in the above.
1489 * none will visit this case. here we supply
1490 * the code just in case (nxt overwritten or
1496 /* proceed with the next header. */
1498 nxt = ip6e->ip6e_nxt;
1505 if (inp->inp_flags2 & INP_RECVFLOWID) {
1506 uint32_t flowid, flow_type;
1508 flowid = m->m_pkthdr.flowid;
1509 flow_type = M_HASHTYPE_GET(m);
1512 * XXX should handle the failure of one or the
1513 * other - don't populate both?
1515 *mp = sbcreatecontrol((caddr_t) &flowid,
1516 sizeof(uint32_t), IPV6_FLOWID, IPPROTO_IPV6);
1518 mp = &(*mp)->m_next;
1519 *mp = sbcreatecontrol((caddr_t) &flow_type,
1520 sizeof(uint32_t), IPV6_FLOWTYPE, IPPROTO_IPV6);
1522 mp = &(*mp)->m_next;
1526 if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1527 uint32_t flowid, flow_type;
1528 uint32_t rss_bucketid;
1530 flowid = m->m_pkthdr.flowid;
1531 flow_type = M_HASHTYPE_GET(m);
1533 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1534 *mp = sbcreatecontrol((caddr_t) &rss_bucketid,
1535 sizeof(uint32_t), IPV6_RSSBUCKETID, IPPROTO_IPV6);
1537 mp = &(*mp)->m_next;
1546 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu)
1550 struct ip6_mtuinfo mtuctl;
1552 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1554 * Notify the error by sending IPV6_PATHMTU ancillary data if
1555 * application wanted to know the MTU value.
1556 * NOTE: we notify disconnected sockets, because some udp
1557 * applications keep sending sockets disconnected.
1558 * NOTE: our implementation doesn't notify connected sockets that has
1559 * foreign address that is different than given destination addresses
1560 * (this is permitted by RFC 3542).
1562 if ((inp->inp_flags & IN6P_MTU) == 0 || (
1563 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1564 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr)))
1567 mtuctl.ip6m_mtu = mtu;
1568 mtuctl.ip6m_addr = *dst;
1569 if (sa6_recoverscope(&mtuctl.ip6m_addr))
1572 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1573 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1576 so = inp->inp_socket;
1577 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1580 /* XXX: should count statistics */
1586 * Get pointer to the previous header followed by the header
1587 * currently processed.
1590 ip6_get_prevhdr(const struct mbuf *m, int off)
1592 struct ip6_ext ip6e;
1593 struct ip6_hdr *ip6;
1596 if (off == sizeof(struct ip6_hdr))
1597 return (offsetof(struct ip6_hdr, ip6_nxt));
1598 if (off < sizeof(struct ip6_hdr))
1599 panic("%s: off < sizeof(struct ip6_hdr)", __func__);
1601 ip6 = mtod(m, struct ip6_hdr *);
1603 len = sizeof(struct ip6_hdr);
1606 m_copydata(m, len, sizeof(ip6e), (caddr_t)&ip6e);
1608 case IPPROTO_FRAGMENT:
1609 nlen = sizeof(struct ip6_frag);
1612 nlen = (ip6e.ip6e_len + 2) << 2;
1615 nlen = (ip6e.ip6e_len + 1) << 3;
1618 nxt = ip6e.ip6e_nxt;
1620 return (len - nlen);
1624 * get next header offset. m will be retained.
1627 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1630 struct ip6_ext ip6e;
1635 panic("ip6_nexthdr: m == NULL");
1636 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1641 if (m->m_pkthdr.len < off + sizeof(ip6))
1643 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1645 *nxtp = ip6.ip6_nxt;
1649 case IPPROTO_FRAGMENT:
1651 * terminate parsing if it is not the first fragment,
1652 * it does not make sense to parse through it.
1654 if (m->m_pkthdr.len < off + sizeof(fh))
1656 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1657 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1658 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1661 *nxtp = fh.ip6f_nxt;
1662 off += sizeof(struct ip6_frag);
1666 if (m->m_pkthdr.len < off + sizeof(ip6e))
1668 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1670 *nxtp = ip6e.ip6e_nxt;
1671 off += (ip6e.ip6e_len + 2) << 2;
1674 case IPPROTO_HOPOPTS:
1675 case IPPROTO_ROUTING:
1676 case IPPROTO_DSTOPTS:
1677 if (m->m_pkthdr.len < off + sizeof(ip6e))
1679 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1681 *nxtp = ip6e.ip6e_nxt;
1682 off += (ip6e.ip6e_len + 1) << 3;
1687 case IPPROTO_IPCOMP:
1699 * get offset for the last header in the chain. m will be kept untainted.
1702 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1712 newoff = ip6_nexthdr(m, off, proto, nxtp);
1715 else if (newoff < off)
1716 return -1; /* invalid */
1717 else if (newoff == off)
1726 * System control for IP6
1729 u_char inet6ctlerrmap[PRC_NCMDS] = {
1731 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1732 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1733 EMSGSIZE, EHOSTUNREACH, 0, 0,
1734 0, 0, EHOSTUNREACH, 0,
1735 ENOPROTOOPT, ECONNREFUSED