2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * Copyright (c) 2019 Netflix, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
35 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/domain.h>
41 #include <sys/eventhandler.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/syslog.h>
53 #include <net/if_var.h>
54 #include <net/if_private.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/in_systm.h> /* For ECN definitions. */
65 #include <netinet/ip.h> /* For ECN definitions. */
68 #include <security/mac/mac_framework.h>
72 * A "big picture" of how IPv6 fragment queues are all linked together.
74 * struct ip6qbucket ip6qb[...]; hashed buckets
77 * +--- TAILQ(struct ip6q, packets) *q6; tailq entries holding
78 * |||||||| fragmented packets
79 * | (1 per original packet)
81 * +--- TAILQ(struct ip6asfrag, ip6q_frags) *af6; tailq entries of IPv6
82 * | *ip6af;fragment packets
83 * | for one original packet
87 /* Reassembly headers are stored in hash buckets. */
88 #define IP6REASS_NHASH_LOG2 10
89 #define IP6REASS_NHASH (1 << IP6REASS_NHASH_LOG2)
90 #define IP6REASS_HMASK (IP6REASS_NHASH - 1)
92 TAILQ_HEAD(ip6qhead, ip6q);
94 struct ip6qhead packets;
100 TAILQ_ENTRY(ip6asfrag) ip6af_tq;
101 struct mbuf *ip6af_m;
102 int ip6af_offset; /* Offset in ip6af_m to next header. */
103 int ip6af_frglen; /* Fragmentable part length. */
104 int ip6af_off; /* Fragment offset. */
105 bool ip6af_mff; /* More fragment bit in frag off. */
108 static MALLOC_DEFINE(M_FRAG6, "frag6", "IPv6 fragment reassembly header");
111 /* A flag to indicate if IPv6 fragmentation is initialized. */
112 VNET_DEFINE_STATIC(bool, frag6_on);
113 #define V_frag6_on VNET(frag6_on)
116 /* System wide (global) maximum and count of packets in reassembly queues. */
117 static int ip6_maxfrags;
118 static u_int __exclusive_cache_line frag6_nfrags;
120 /* Maximum and current packets in per-VNET reassembly queue. */
121 VNET_DEFINE_STATIC(int, ip6_maxfragpackets);
122 VNET_DEFINE_STATIC(volatile u_int, frag6_nfragpackets);
123 #define V_ip6_maxfragpackets VNET(ip6_maxfragpackets)
124 #define V_frag6_nfragpackets VNET(frag6_nfragpackets)
126 /* Maximum per-VNET reassembly timeout (milliseconds) */
127 VNET_DEFINE_STATIC(u_int, ip6_fraglifetime) = IPV6_DEFFRAGTTL;
128 #define V_ip6_fraglifetime VNET(ip6_fraglifetime)
130 /* Maximum per-VNET reassembly queues per bucket and fragments per packet. */
131 VNET_DEFINE_STATIC(int, ip6_maxfragbucketsize);
132 VNET_DEFINE_STATIC(int, ip6_maxfragsperpacket);
133 #define V_ip6_maxfragbucketsize VNET(ip6_maxfragbucketsize)
134 #define V_ip6_maxfragsperpacket VNET(ip6_maxfragsperpacket)
136 /* Per-VNET reassembly queue buckets. */
137 VNET_DEFINE_STATIC(struct ip6qbucket, ip6qb[IP6REASS_NHASH]);
138 VNET_DEFINE_STATIC(uint32_t, ip6qb_hashseed);
139 #define V_ip6qb VNET(ip6qb)
140 #define V_ip6qb_hashseed VNET(ip6qb_hashseed)
142 #define IP6QB_LOCK(_b) mtx_lock(&V_ip6qb[(_b)].lock)
143 #define IP6QB_TRYLOCK(_b) mtx_trylock(&V_ip6qb[(_b)].lock)
144 #define IP6QB_LOCK_ASSERT(_b) mtx_assert(&V_ip6qb[(_b)].lock, MA_OWNED)
145 #define IP6QB_UNLOCK(_b) mtx_unlock(&V_ip6qb[(_b)].lock)
146 #define IP6QB_HEAD(_b) (&V_ip6qb[(_b)].packets)
149 * By default, limit the number of IP6 fragments across all reassembly
150 * queues to 1/32 of the total number of mbuf clusters.
152 * Limit the total number of reassembly queues per VNET to the
153 * IP6 fragment limit, but ensure the limit will not allow any bucket
154 * to grow above 100 items. (The bucket limit is
155 * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct
156 * multiplier to reach a 100-item limit.)
157 * The 100-item limit was chosen as brief testing seems to show that
158 * this produces "reasonable" performance on some subset of systems
161 #define IP6_MAXFRAGS (nmbclusters / 32)
162 #define IP6_MAXFRAGPACKETS (imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
164 /* Interval between periodic reassembly queue inspections */
165 #define IP6_CALLOUT_INTERVAL_MS 500
168 * Sysctls and helper function.
170 SYSCTL_DECL(_net_inet6_ip6);
172 SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfrags,
173 CTLFLAG_RD, &frag6_nfrags, 0,
174 "Global number of IPv6 fragments across all reassembly queues.");
177 frag6_set_bucketsize(void)
181 if ((i = V_ip6_maxfragpackets) > 0)
182 V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
185 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags,
186 CTLFLAG_RW, &ip6_maxfrags, 0,
187 "Maximum allowed number of outstanding IPv6 packet fragments. "
188 "A value of 0 means no fragmented packets will be accepted, while "
189 "a value of -1 means no limit");
192 sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS)
196 val = V_ip6_maxfragpackets;
197 error = sysctl_handle_int(oidp, &val, 0, req);
198 if (error != 0 || !req->newptr)
200 V_ip6_maxfragpackets = val;
201 frag6_set_bucketsize();
204 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets,
205 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
206 NULL, 0, sysctl_ip6_maxfragpackets, "I",
207 "Default maximum number of outstanding fragmented IPv6 packets. "
208 "A value of 0 means no fragmented packets will be accepted, while a "
209 "a value of -1 means no limit");
210 SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfragpackets,
211 CTLFLAG_VNET | CTLFLAG_RD,
212 __DEVOLATILE(u_int *, &VNET_NAME(frag6_nfragpackets)), 0,
213 "Per-VNET number of IPv6 fragments across all reassembly queues.");
214 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGSPERPACKET, maxfragsperpacket,
215 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragsperpacket), 0,
216 "Maximum allowed number of fragments per packet");
217 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGBUCKETSIZE, maxfragbucketsize,
218 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragbucketsize), 0,
219 "Maximum number of reassembly queues per hash bucket");
222 frag6_milli_to_callout_ticks(int ms)
224 return (ms / IP6_CALLOUT_INTERVAL_MS);
228 frag6_callout_ticks_to_milli(int ms)
230 return (ms * IP6_CALLOUT_INTERVAL_MS);
233 _Static_assert(sizeof(((struct ip6q *)NULL)->ip6q_ttl) >= 2,
234 "ip6q_ttl field is not large enough");
237 sysctl_ip6_fraglifetime(SYSCTL_HANDLER_ARGS)
241 val = V_ip6_fraglifetime;
242 error = sysctl_handle_int(oidp, &val, 0, req);
243 if (error != 0 || !req->newptr)
246 val = IPV6_DEFFRAGTTL;
248 if (frag6_milli_to_callout_ticks(val) >= 65536)
249 val = frag6_callout_ticks_to_milli(65535);
251 if (!IS_DEFAULT_VNET(curvnet)) {
253 int host_val = V_ip6_fraglifetime;
260 V_ip6_fraglifetime = val;
263 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, fraglifetime_ms,
264 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
265 NULL, 0, sysctl_ip6_fraglifetime, "I",
266 "Fragment lifetime, in milliseconds");
269 * Remove the IPv6 fragmentation header from the mbuf.
272 ip6_deletefraghdr(struct mbuf *m, int offset, int wait __unused)
276 KASSERT(m->m_len >= offset + sizeof(struct ip6_frag),
277 ("%s: ext headers not contigous in mbuf %p m_len %d >= "
278 "offset %d + %zu\n", __func__, m, m->m_len, offset,
279 sizeof(struct ip6_frag)));
281 /* Delete frag6 header. */
282 ip6 = mtod(m, struct ip6_hdr *);
283 bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag), offset);
284 m->m_data += sizeof(struct ip6_frag);
285 m->m_len -= sizeof(struct ip6_frag);
286 m->m_flags |= M_FRAGMENTED;
292 * Free a fragment reassembly header and all associated datagrams.
295 frag6_freef(struct ip6q *q6, uint32_t bucket)
298 struct ip6asfrag *af6;
301 IP6QB_LOCK_ASSERT(bucket);
303 while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) {
305 TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
308 * Return ICMP time exceeded error for the 1st fragment.
309 * Just free other fragments.
311 if (af6->ip6af_off == 0 && m->m_pkthdr.rcvif != NULL) {
312 /* Adjust pointer. */
313 ip6 = mtod(m, struct ip6_hdr *);
315 /* Restore source and destination addresses. */
316 ip6->ip6_src = q6->ip6q_src;
317 ip6->ip6_dst = q6->ip6q_dst;
319 icmp6_error(m, ICMP6_TIME_EXCEEDED,
320 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
327 TAILQ_REMOVE(IP6QB_HEAD(bucket), q6, ip6q_tq);
328 V_ip6qb[bucket].count--;
329 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
331 mac_ip6q_destroy(q6);
334 atomic_subtract_int(&V_frag6_nfragpackets, 1);
338 * Drain off all datagram fragments belonging to
339 * the given network interface.
342 frag6_cleanup(void *arg __unused, struct ifnet *ifp)
344 struct ip6qhead *head;
346 struct ip6asfrag *af6;
349 KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
351 CURVNET_SET_QUIET(ifp->if_vnet);
354 * Skip processing if IPv6 reassembly is not initialised or
355 * torn down by frag6_destroy().
363 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
365 head = IP6QB_HEAD(bucket);
366 /* Scan fragment list. */
367 TAILQ_FOREACH(q6, head, ip6q_tq) {
368 TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) {
369 /* Clear no longer valid rcvif pointer. */
370 if (af6->ip6af_m->m_pkthdr.rcvif == ifp)
371 af6->ip6af_m->m_pkthdr.rcvif = NULL;
374 IP6QB_UNLOCK(bucket);
378 EVENTHANDLER_DEFINE(ifnet_departure_event, frag6_cleanup, NULL, 0);
381 * Like in RFC2460, in RFC8200, fragment and reassembly rules do not agree with
382 * each other, in terms of next header field handling in fragment header.
383 * While the sender will use the same value for all of the fragmented packets,
384 * receiver is suggested not to check for consistency.
386 * Fragment rules (p18,p19):
387 * (2) A Fragment header containing:
388 * The Next Header value that identifies the first header
389 * after the Per-Fragment headers of the original packet.
390 * -> next header field is same for all fragments
392 * Reassembly rule (p20):
393 * The Next Header field of the last header of the Per-Fragment
394 * headers is obtained from the Next Header field of the first
395 * fragment's Fragment header.
396 * -> should grab it from the first fragment only
398 * The following note also contradicts with fragment rule - no one is going to
399 * send different fragment with different next header field.
401 * Additional note (p22) [not an error]:
402 * The Next Header values in the Fragment headers of different
403 * fragments of the same original packet may differ. Only the value
404 * from the Offset zero fragment packet is used for reassembly.
405 * -> should grab it from the first fragment only
407 * There is no explicit reason given in the RFC. Historical reason maybe?
413 frag6_input(struct mbuf **mp, int *offp, int proto)
417 struct ip6_frag *ip6f;
418 struct ip6qhead *head;
420 struct ip6asfrag *af6, *ip6af, *af6tmp;
421 struct in6_ifaddr *ia6;
422 struct ifnet *dstifp, *srcifp;
423 uint32_t hashkey[(sizeof(struct in6_addr) * 2 +
424 sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)];
425 uint32_t bucket, *hashkeyp;
426 int fragoff, frgpartlen; /* Must be larger than uint16_t. */
427 int nxt, offset, plen;
431 struct ip6_direct_ctx *ip6dc;
440 if (m->m_len < offset + sizeof(struct ip6_frag)) {
441 m = m_pullup(m, offset + sizeof(struct ip6_frag));
443 IP6STAT_INC(ip6s_exthdrtoolong);
445 return (IPPROTO_DONE);
448 ip6 = mtod(m, struct ip6_hdr *);
451 /* Find the destination interface of the packet. */
452 ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
454 dstifp = ia6->ia_ifp;
456 /* Jumbo payload cannot contain a fragment header. */
457 if (ip6->ip6_plen == 0) {
458 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
459 in6_ifstat_inc(dstifp, ifs6_reass_fail);
461 return (IPPROTO_DONE);
465 * Check whether fragment packet's fragment length is a
466 * multiple of 8 octets (unless it is the last one).
467 * sizeof(struct ip6_frag) == 8
468 * sizeof(struct ip6_hdr) = 40
470 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
471 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
472 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
473 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
474 offsetof(struct ip6_hdr, ip6_plen));
475 in6_ifstat_inc(dstifp, ifs6_reass_fail);
477 return (IPPROTO_DONE);
480 IP6STAT_INC(ip6s_fragments);
481 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
484 * Handle "atomic" fragments (offset and m bit set to 0) upfront,
485 * unrelated to any reassembly. We need to remove the frag hdr
487 * See RFC 6946 and section 4.5 of RFC 8200.
489 if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
490 IP6STAT_INC(ip6s_atomicfrags);
491 nxt = ip6f->ip6f_nxt;
493 * Set nxt(-hdr field value) to the original value.
494 * We cannot just set ip6->ip6_nxt as there might be
495 * an unfragmentable part with extension headers and
496 * we must update the last one.
498 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
500 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) -
501 sizeof(struct ip6_frag));
502 if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0)
504 m->m_pkthdr.len -= sizeof(struct ip6_frag);
505 in6_ifstat_inc(dstifp, ifs6_reass_ok);
510 /* Offset now points to data portion. */
511 offset += sizeof(struct ip6_frag);
513 /* Get fragment length and discard 0-byte fragments. */
514 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
515 if (frgpartlen == 0) {
516 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
517 offsetof(struct ip6_hdr, ip6_plen));
518 in6_ifstat_inc(dstifp, ifs6_reass_fail);
519 IP6STAT_INC(ip6s_fragdropped);
521 return (IPPROTO_DONE);
525 * Enforce upper bound on number of fragments for the entire system.
526 * If maxfrag is 0, never accept fragments.
527 * If maxfrag is -1, accept all fragments without limitation.
529 if (ip6_maxfrags < 0)
531 else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
535 * Validate that a full header chain to the ULP is present in the
536 * packet containing the first fragment as per RFC RFC7112 and
537 * RFC 8200 pages 18,19:
538 * The first fragment packet is composed of:
539 * (3) Extension headers, if any, and the Upper-Layer header. These
540 * headers must be in the first fragment. ...
542 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
543 /* XXX TODO. thj has D16851 open for this. */
544 /* Send ICMPv6 4,3 in case of violation. */
546 /* Store receive network interface pointer for later. */
547 srcifp = m->m_pkthdr.rcvif;
549 /* Generate a hash value for fragment bucket selection. */
551 memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
552 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
553 memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
554 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
555 *hashkeyp = ip6f->ip6f_ident;
556 bucket = jenkins_hash32(hashkey, nitems(hashkey), V_ip6qb_hashseed);
557 bucket &= IP6REASS_HMASK;
559 head = IP6QB_HEAD(bucket);
561 TAILQ_FOREACH(q6, head, ip6q_tq)
562 if (ip6f->ip6f_ident == q6->ip6q_ident &&
563 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
564 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
566 && mac_ip6q_match(m, q6)
573 /* A first fragment to arrive creates a reassembly queue. */
577 * Enforce upper bound on number of fragmented packets
578 * for which we attempt reassembly;
579 * If maxfragpackets is 0, never accept fragments.
580 * If maxfragpackets is -1, accept all fragments without
583 if (V_ip6_maxfragpackets < 0)
585 else if (V_ip6qb[bucket].count >= V_ip6_maxfragbucketsize ||
586 atomic_load_int(&V_frag6_nfragpackets) >=
587 (u_int)V_ip6_maxfragpackets)
590 /* Allocate IPv6 fragement packet queue entry. */
591 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FRAG6,
596 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
600 mac_ip6q_create(m, q6);
602 atomic_add_int(&V_frag6_nfragpackets, 1);
604 /* ip6q_nxt will be filled afterwards, from 1st fragment. */
605 TAILQ_INIT(&q6->ip6q_frags);
606 q6->ip6q_ident = ip6f->ip6f_ident;
607 q6->ip6q_ttl = frag6_milli_to_callout_ticks(V_ip6_fraglifetime);
608 q6->ip6q_src = ip6->ip6_src;
609 q6->ip6q_dst = ip6->ip6_dst;
610 q6->ip6q_ecn = IPV6_ECN(ip6);
611 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
613 /* Add the fragemented packet to the bucket. */
614 TAILQ_INSERT_HEAD(head, q6, ip6q_tq);
615 V_ip6qb[bucket].count++;
619 * If it is the 1st fragment, record the length of the
620 * unfragmentable part and the next header of the fragment header.
621 * Assume the first 1st fragement to arrive will be correct.
622 * We do not have any duplicate checks here yet so another packet
623 * with fragoff == 0 could come and overwrite the ip6q_unfrglen
624 * and worse, the next header, at any time.
626 if (fragoff == 0 && q6->ip6q_unfrglen == -1) {
627 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
628 sizeof(struct ip6_frag);
629 q6->ip6q_nxt = ip6f->ip6f_nxt;
634 * Check that the reassembled packet would not exceed 65535 bytes
636 * If it would exceed, discard the fragment and return an ICMP error.
638 if (q6->ip6q_unfrglen >= 0) {
639 /* The 1st fragment has already arrived. */
640 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
642 TAILQ_REMOVE(head, q6, ip6q_tq);
643 V_ip6qb[bucket].count--;
644 atomic_subtract_int(&V_frag6_nfragpackets, 1);
646 mac_ip6q_destroy(q6);
650 IP6QB_UNLOCK(bucket);
651 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
652 offset - sizeof(struct ip6_frag) +
653 offsetof(struct ip6_frag, ip6f_offlg));
655 return (IPPROTO_DONE);
657 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
659 TAILQ_REMOVE(head, q6, ip6q_tq);
660 V_ip6qb[bucket].count--;
661 atomic_subtract_int(&V_frag6_nfragpackets, 1);
663 mac_ip6q_destroy(q6);
667 IP6QB_UNLOCK(bucket);
668 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
669 offset - sizeof(struct ip6_frag) +
670 offsetof(struct ip6_frag, ip6f_offlg));
672 return (IPPROTO_DONE);
676 * If it is the first fragment, do the above check for each
677 * fragment already stored in the reassembly queue.
679 if (fragoff == 0 && !only_frag) {
680 TAILQ_FOREACH_SAFE(af6, &q6->ip6q_frags, ip6af_tq, af6tmp) {
681 if (q6->ip6q_unfrglen + af6->ip6af_off +
682 af6->ip6af_frglen > IPV6_MAXPACKET) {
683 struct ip6_hdr *ip6err;
688 erroff = af6->ip6af_offset;
690 /* Dequeue the fragment. */
691 TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
693 atomic_subtract_int(&frag6_nfrags, 1);
696 /* Set a valid receive interface pointer. */
697 merr->m_pkthdr.rcvif = srcifp;
699 /* Adjust pointer. */
700 ip6err = mtod(merr, struct ip6_hdr *);
703 * Restore source and destination addresses
704 * in the erroneous IPv6 header.
706 ip6err->ip6_src = q6->ip6q_src;
707 ip6err->ip6_dst = q6->ip6q_dst;
709 icmp6_error(merr, ICMP6_PARAM_PROB,
710 ICMP6_PARAMPROB_HEADER,
711 erroff - sizeof(struct ip6_frag) +
712 offsetof(struct ip6_frag, ip6f_offlg));
717 /* Allocate an IPv6 fragement queue entry for this fragmented part. */
718 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FRAG6,
722 ip6af->ip6af_mff = (ip6f->ip6f_offlg & IP6F_MORE_FRAG) ? true : false;
723 ip6af->ip6af_off = fragoff;
724 ip6af->ip6af_frglen = frgpartlen;
725 ip6af->ip6af_offset = offset;
730 * Do a manual insert rather than a hard-to-understand cast
731 * to a different type relying on data structure order to work.
733 TAILQ_INSERT_HEAD(&q6->ip6q_frags, ip6af, ip6af_tq);
737 /* Do duplicate, condition, and boundry checks. */
739 * Handle ECN by comparing this segment with the first one;
740 * if CE is set, do not lose CE.
741 * Drop if CE and not-ECT are mixed for the same packet.
745 if (ecn == IPTOS_ECN_CE) {
746 if (ecn0 == IPTOS_ECN_NOTECT) {
747 free(ip6af, M_FRAG6);
750 if (ecn0 != IPTOS_ECN_CE)
751 q6->ip6q_ecn = IPTOS_ECN_CE;
753 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
754 free(ip6af, M_FRAG6);
758 /* Find a fragmented part which begins after this one does. */
759 TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq)
760 if (af6->ip6af_off > ip6af->ip6af_off)
764 * If the incoming framgent overlaps some existing fragments in
765 * the reassembly queue, drop both the new fragment and the
766 * entire reassembly queue. However, if the new fragment
767 * is an exact duplicate of an existing fragment, only silently
768 * drop the existing fragment and leave the fragmentation queue
769 * unchanged, as allowed by the RFC. (RFC 8200, 4.5)
772 af6tmp = TAILQ_PREV(af6, ip6fraghead, ip6af_tq);
774 af6tmp = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead);
775 if (af6tmp != NULL) {
776 if (af6tmp->ip6af_off + af6tmp->ip6af_frglen -
777 ip6af->ip6af_off > 0) {
778 if (af6tmp->ip6af_off != ip6af->ip6af_off ||
779 af6tmp->ip6af_frglen != ip6af->ip6af_frglen)
780 frag6_freef(q6, bucket);
781 free(ip6af, M_FRAG6);
786 if (ip6af->ip6af_off + ip6af->ip6af_frglen -
787 af6->ip6af_off > 0) {
788 if (af6->ip6af_off != ip6af->ip6af_off ||
789 af6->ip6af_frglen != ip6af->ip6af_frglen)
790 frag6_freef(q6, bucket);
791 free(ip6af, M_FRAG6);
797 mac_ip6q_update(m, q6);
801 * Stick new segment in its place; check for complete reassembly.
802 * If not complete, check fragment limit. Move to front of packet
803 * queue, as we are the most recently active fragmented packet.
806 TAILQ_INSERT_BEFORE(af6, ip6af, ip6af_tq);
808 TAILQ_INSERT_TAIL(&q6->ip6q_frags, ip6af, ip6af_tq);
810 atomic_add_int(&frag6_nfrags, 1);
814 TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) {
815 if (af6->ip6af_off != plen) {
816 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
817 IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
818 frag6_freef(q6, bucket);
820 IP6QB_UNLOCK(bucket);
822 return (IPPROTO_DONE);
824 plen += af6->ip6af_frglen;
826 af6 = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead);
827 if (af6->ip6af_mff) {
828 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
829 IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
830 frag6_freef(q6, bucket);
832 IP6QB_UNLOCK(bucket);
834 return (IPPROTO_DONE);
837 /* Reassembly is complete; concatenate fragments. */
838 ip6af = TAILQ_FIRST(&q6->ip6q_frags);
839 t = m = ip6af->ip6af_m;
840 TAILQ_REMOVE(&q6->ip6q_frags, ip6af, ip6af_tq);
841 while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) {
842 m->m_pkthdr.csum_flags &=
843 af6->ip6af_m->m_pkthdr.csum_flags;
844 m->m_pkthdr.csum_data +=
845 af6->ip6af_m->m_pkthdr.csum_data;
847 TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
849 m_adj(af6->ip6af_m, af6->ip6af_offset);
850 m_demote_pkthdr(af6->ip6af_m);
851 m_cat(t, af6->ip6af_m);
855 while (m->m_pkthdr.csum_data & 0xffff0000)
856 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
857 (m->m_pkthdr.csum_data >> 16);
859 /* Adjust offset to point where the original next header starts. */
860 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
861 free(ip6af, M_FRAG6);
862 if ((u_int)plen + (u_int)offset - sizeof(struct ip6_hdr) >
864 frag6_freef(q6, bucket);
867 ip6 = mtod(m, struct ip6_hdr *);
868 ip6->ip6_plen = htons((u_short)plen + offset - sizeof(struct ip6_hdr));
869 if (q6->ip6q_ecn == IPTOS_ECN_CE)
870 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
873 TAILQ_REMOVE(head, q6, ip6q_tq);
874 V_ip6qb[bucket].count--;
875 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
877 ip6_deletefraghdr(m, offset, M_NOWAIT);
879 /* Set nxt(-hdr field value) to the original value. */
880 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
884 mac_ip6q_reassemble(q6, m);
885 mac_ip6q_destroy(q6);
888 atomic_subtract_int(&V_frag6_nfragpackets, 1);
890 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
893 for (t = m; t; t = t->m_next)
895 m->m_pkthdr.len = plen;
896 /* Set a valid receive interface pointer. */
897 m->m_pkthdr.rcvif = srcifp;
901 mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
906 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
907 ip6dc->ip6dc_nxt = nxt;
908 ip6dc->ip6dc_off = offset;
910 m_tag_prepend(m, mtag);
913 IP6QB_UNLOCK(bucket);
914 IP6STAT_INC(ip6s_reassembled);
915 in6_ifstat_inc(dstifp, ifs6_reass_ok);
918 /* Queue/dispatch for reprocessing. */
919 netisr_dispatch(NETISR_IPV6_DIRECT, m);
921 return (IPPROTO_DONE);
924 /* Tell launch routine the next header. */
931 IP6QB_UNLOCK(bucket);
933 in6_ifstat_inc(dstifp, ifs6_reass_fail);
934 IP6STAT_INC(ip6s_fragdropped);
937 return (IPPROTO_DONE);
941 * IPv6 reassembling timer processing;
942 * if a timer expires on a reassembly queue, discard it.
944 static struct callout frag6_callout;
946 frag6_slowtimo(void *arg __unused)
948 VNET_ITERATOR_DECL(vnet_iter);
949 struct ip6qhead *head;
950 struct ip6q *q6, *q6tmp;
953 if (atomic_load_int(&frag6_nfrags) == 0)
956 VNET_LIST_RLOCK_NOSLEEP();
957 VNET_FOREACH(vnet_iter) {
958 CURVNET_SET(vnet_iter);
959 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
960 if (V_ip6qb[bucket].count == 0)
963 head = IP6QB_HEAD(bucket);
964 TAILQ_FOREACH_SAFE(q6, head, ip6q_tq, q6tmp)
965 if (--q6->ip6q_ttl == 0) {
966 IP6STAT_ADD(ip6s_fragtimeout,
968 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
969 frag6_freef(q6, bucket);
972 * If we are over the maximum number of fragments
973 * (due to the limit being lowered), drain off
974 * enough to get down to the new limit.
975 * Note that we drain all reassembly queues if
976 * maxfragpackets is 0 (fragmentation is disabled),
977 * and do not enforce a limit when maxfragpackets
980 while ((V_ip6_maxfragpackets == 0 ||
981 (V_ip6_maxfragpackets > 0 &&
982 V_ip6qb[bucket].count > V_ip6_maxfragbucketsize)) &&
983 (q6 = TAILQ_LAST(head, ip6qhead)) != NULL) {
984 IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag);
985 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
986 frag6_freef(q6, bucket);
988 IP6QB_UNLOCK(bucket);
991 * If we are still over the maximum number of fragmented
992 * packets, drain off enough to get down to the new limit.
995 while (V_ip6_maxfragpackets >= 0 &&
996 atomic_load_int(&V_frag6_nfragpackets) >
997 (u_int)V_ip6_maxfragpackets) {
999 q6 = TAILQ_LAST(IP6QB_HEAD(bucket), ip6qhead);
1001 IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag);
1002 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
1003 frag6_freef(q6, bucket);
1005 IP6QB_UNLOCK(bucket);
1006 bucket = (bucket + 1) % IP6REASS_NHASH;
1010 VNET_LIST_RUNLOCK_NOSLEEP();
1012 callout_reset_sbt(&frag6_callout, SBT_1MS * IP6_CALLOUT_INTERVAL_MS,
1013 SBT_1MS * 10, frag6_slowtimo, NULL, 0);
1017 frag6_slowtimo_init(void *arg __unused)
1020 callout_init(&frag6_callout, 1);
1021 callout_reset_sbt(&frag6_callout, SBT_1MS * IP6_CALLOUT_INTERVAL_MS,
1022 SBT_1MS * 10, frag6_slowtimo, NULL, 0);
1024 SYSINIT(frag6, SI_SUB_VNET_DONE, SI_ORDER_ANY, frag6_slowtimo_init, NULL);
1027 * Eventhandler to adjust limits in case nmbclusters change.
1030 frag6_change(void *tag)
1032 VNET_ITERATOR_DECL(vnet_iter);
1034 ip6_maxfrags = IP6_MAXFRAGS;
1035 VNET_LIST_RLOCK_NOSLEEP();
1036 VNET_FOREACH(vnet_iter) {
1037 CURVNET_SET(vnet_iter);
1038 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
1039 frag6_set_bucketsize();
1042 VNET_LIST_RUNLOCK_NOSLEEP();
1046 * Initialise reassembly queue and fragment identifier.
1053 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
1054 frag6_set_bucketsize();
1055 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
1056 TAILQ_INIT(IP6QB_HEAD(bucket));
1057 mtx_init(&V_ip6qb[bucket].lock, "ip6qb", NULL, MTX_DEF);
1058 V_ip6qb[bucket].count = 0;
1060 V_ip6qb_hashseed = arc4random();
1061 V_ip6_maxfragsperpacket = 64;
1065 if (!IS_DEFAULT_VNET(curvnet))
1068 ip6_maxfrags = IP6_MAXFRAGS;
1069 EVENTHANDLER_REGISTER(nmbclusters_change,
1070 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
1074 * Drain off all datagram fragments.
1077 frag6_drain_one(void)
1082 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
1084 while ((q6 = TAILQ_FIRST(IP6QB_HEAD(bucket))) != NULL) {
1085 IP6STAT_INC(ip6s_fragdropped);
1086 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
1087 frag6_freef(q6, bucket);
1089 IP6QB_UNLOCK(bucket);
1096 VNET_ITERATOR_DECL(vnet_iter);
1098 VNET_LIST_RLOCK_NOSLEEP();
1099 VNET_FOREACH(vnet_iter) {
1100 CURVNET_SET(vnet_iter);
1104 VNET_LIST_RUNLOCK_NOSLEEP();
1109 * Clear up IPv6 reassembly structures.
1118 for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
1119 KASSERT(V_ip6qb[bucket].count == 0,
1120 ("%s: V_ip6qb[%d] (%p) count not 0 (%d)", __func__,
1121 bucket, &V_ip6qb[bucket], V_ip6qb[bucket].count));
1122 mtx_destroy(&V_ip6qb[bucket].lock);