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: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/domain.h>
41 #include <sys/errno.h>
42 #include <sys/eventhandler.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
53 #include <machine/atomic.h>
56 #include <net/if_var.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet/icmp6.h>
66 #include <netinet/in_systm.h> /* for ECN definitions */
67 #include <netinet/ip.h> /* for ECN definitions */
70 #include <security/mac/mac_framework.h>
74 * Reassembly headers are stored in hash buckets.
76 #define IP6REASS_NHASH_LOG2 10
77 #define IP6REASS_NHASH (1 << IP6REASS_NHASH_LOG2)
78 #define IP6REASS_HMASK (IP6REASS_NHASH - 1)
80 static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *,
81 uint32_t bucket __unused);
82 static void frag6_deq(struct ip6asfrag *, uint32_t bucket __unused);
83 static void frag6_insque_head(struct ip6q *, struct ip6q *,
85 static void frag6_remque(struct ip6q *, uint32_t bucket);
86 static void frag6_freef(struct ip6q *, uint32_t bucket);
94 /* System wide (global) maximum and count of packets in reassembly queues. */
95 static int ip6_maxfrags;
96 static volatile u_int frag6_nfrags = 0;
98 /* Maximum and current packets in per-VNET reassembly queue. */
99 VNET_DEFINE_STATIC(int, ip6_maxfragpackets);
100 VNET_DEFINE_STATIC(volatile u_int, frag6_nfragpackets);
101 #define V_ip6_maxfragpackets VNET(ip6_maxfragpackets)
102 #define V_frag6_nfragpackets VNET(frag6_nfragpackets)
104 /* Maximum per-VNET reassembly queues per bucket and fragments per packet. */
105 VNET_DEFINE_STATIC(int, ip6_maxfragbucketsize);
106 VNET_DEFINE_STATIC(int, ip6_maxfragsperpacket);
107 #define V_ip6_maxfragbucketsize VNET(ip6_maxfragbucketsize)
108 #define V_ip6_maxfragsperpacket VNET(ip6_maxfragsperpacket)
110 /* Per-VNET reassembly queue buckets. */
111 VNET_DEFINE_STATIC(struct ip6qbucket, ip6q[IP6REASS_NHASH]);
112 VNET_DEFINE_STATIC(uint32_t, ip6q_hashseed);
113 #define V_ip6q VNET(ip6q)
114 #define V_ip6q_hashseed VNET(ip6q_hashseed)
116 #define IP6Q_LOCK(i) mtx_lock(&V_ip6q[(i)].lock)
117 #define IP6Q_TRYLOCK(i) mtx_trylock(&V_ip6q[(i)].lock)
118 #define IP6Q_LOCK_ASSERT(i) mtx_assert(&V_ip6q[(i)].lock, MA_OWNED)
119 #define IP6Q_UNLOCK(i) mtx_unlock(&V_ip6q[(i)].lock)
120 #define IP6Q_HEAD(i) (&V_ip6q[(i)].ip6q)
122 static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
125 * By default, limit the number of IP6 fragments across all reassembly
126 * queues to 1/32 of the total number of mbuf clusters.
128 * Limit the total number of reassembly queues per VNET to the
129 * IP6 fragment limit, but ensure the limit will not allow any bucket
130 * to grow above 100 items. (The bucket limit is
131 * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct
132 * multiplier to reach a 100-item limit.)
133 * The 100-item limit was chosen as brief testing seems to show that
134 * this produces "reasonable" performance on some subset of systems
137 #define IP6_MAXFRAGS (nmbclusters / 32)
138 #define IP6_MAXFRAGPACKETS (imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
142 * Sysctls and helper function.
144 SYSCTL_DECL(_net_inet6_ip6);
147 frag6_set_bucketsize()
151 if ((i = V_ip6_maxfragpackets) > 0)
152 V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
155 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags,
156 CTLFLAG_RW, &ip6_maxfrags, 0,
157 "Maximum allowed number of outstanding IPv6 packet fragments. "
158 "A value of 0 means no fragmented packets will be accepted, while a "
159 "a value of -1 means no limit");
162 sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS)
166 val = V_ip6_maxfragpackets;
167 error = sysctl_handle_int(oidp, &val, 0, req);
168 if (error != 0 || !req->newptr)
170 V_ip6_maxfragpackets = val;
171 frag6_set_bucketsize();
174 SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets,
175 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
176 sysctl_ip6_maxfragpackets, "I",
177 "Default maximum number of outstanding fragmented IPv6 packets. "
178 "A value of 0 means no fragmented packets will be accepted, while a "
179 "a value of -1 means no limit");
180 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGSPERPACKET, maxfragsperpacket,
181 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragsperpacket), 0,
182 "Maximum allowed number of fragments per packet");
183 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGBUCKETSIZE, maxfragbucketsize,
184 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragbucketsize), 0,
185 "Maximum number of reassembly queues per hash bucket");
189 * Initialise reassembly queue and fragment identifier.
192 frag6_change(void *tag)
194 VNET_ITERATOR_DECL(vnet_iter);
196 ip6_maxfrags = IP6_MAXFRAGS;
197 VNET_LIST_RLOCK_NOSLEEP();
198 VNET_FOREACH(vnet_iter) {
199 CURVNET_SET(vnet_iter);
200 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
201 frag6_set_bucketsize();
204 VNET_LIST_RUNLOCK_NOSLEEP();
213 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
214 frag6_set_bucketsize();
215 for (i = 0; i < IP6REASS_NHASH; i++) {
217 q6->ip6q_next = q6->ip6q_prev = q6;
218 mtx_init(&V_ip6q[i].lock, "ip6qlock", NULL, MTX_DEF);
221 V_ip6q_hashseed = arc4random();
222 V_ip6_maxfragsperpacket = 64;
223 if (!IS_DEFAULT_VNET(curvnet))
226 ip6_maxfrags = IP6_MAXFRAGS;
227 EVENTHANDLER_REGISTER(nmbclusters_change,
228 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
232 * In RFC2460, fragment and reassembly rule do not agree with each other,
233 * in terms of next header field handling in fragment header.
234 * While the sender will use the same value for all of the fragmented packets,
235 * receiver is suggested not to check the consistency.
237 * fragment rule (p20):
238 * (2) A Fragment header containing:
239 * The Next Header value that identifies the first header of
240 * the Fragmentable Part of the original packet.
241 * -> next header field is same for all fragments
243 * reassembly rule (p21):
244 * The Next Header field of the last header of the Unfragmentable
245 * Part is obtained from the Next Header field of the first
246 * fragment's Fragment header.
247 * -> should grab it from the first fragment only
249 * The following note also contradicts with fragment rule - no one is going to
250 * send different fragment with different next header field.
252 * additional note (p22):
253 * The Next Header values in the Fragment headers of different
254 * fragments of the same original packet may differ. Only the value
255 * from the Offset zero fragment packet is used for reassembly.
256 * -> should grab it from the first fragment only
258 * There is no explicit reason given in the RFC. Historical reason maybe?
264 frag6_input(struct mbuf **mp, int *offp, int proto)
266 struct mbuf *m = *mp, *t;
268 struct ip6_frag *ip6f;
269 struct ip6q *head, *q6;
270 struct ip6asfrag *af6, *ip6af, *af6dwn;
271 struct in6_ifaddr *ia;
272 int offset = *offp, nxt, i, next;
274 int fragoff, frgpartlen; /* must be larger than u_int16_t */
275 uint32_t hashkey[(sizeof(struct in6_addr) * 2 +
276 sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)];
277 uint32_t hash, *hashkeyp;
278 struct ifnet *dstifp;
282 struct ip6_direct_ctx *ip6dc;
286 char ip6buf[INET6_ADDRSTRLEN];
289 ip6 = mtod(m, struct ip6_hdr *);
290 #ifndef PULLDOWN_TEST
291 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
292 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
294 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
296 return (IPPROTO_DONE);
300 /* find the destination interface of the packet. */
301 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
304 ifa_free(&ia->ia_ifa);
306 /* jumbo payload can't contain a fragment header */
307 if (ip6->ip6_plen == 0) {
308 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
309 in6_ifstat_inc(dstifp, ifs6_reass_fail);
314 * check whether fragment packet's fragment length is
315 * multiple of 8 octets.
316 * sizeof(struct ip6_frag) == 8
317 * sizeof(struct ip6_hdr) = 40
319 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
320 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
321 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
322 offsetof(struct ip6_hdr, ip6_plen));
323 in6_ifstat_inc(dstifp, ifs6_reass_fail);
327 IP6STAT_INC(ip6s_fragments);
328 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
330 /* offset now points to data portion */
331 offset += sizeof(struct ip6_frag);
334 * Handle "atomic" fragments (offset and m bit set to 0) upfront,
335 * unrelated to any reassembly (see RFC 6946 and section 4.5 of RFC
336 * 8200). Just skip the fragment header.
338 if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
339 IP6STAT_INC(ip6s_atomicfrags);
340 in6_ifstat_inc(dstifp, ifs6_reass_ok);
342 m->m_flags |= M_FRAGMENTED;
343 return (ip6f->ip6f_nxt);
346 /* Get fragment length and discard 0-byte fragments. */
347 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
348 if (frgpartlen == 0) {
349 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
350 offsetof(struct ip6_hdr, ip6_plen));
351 in6_ifstat_inc(dstifp, ifs6_reass_fail);
352 IP6STAT_INC(ip6s_fragdropped);
357 memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
358 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
359 memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
360 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
361 *hashkeyp = ip6f->ip6f_ident;
362 hash = jenkins_hash32(hashkey, nitems(hashkey), V_ip6q_hashseed);
363 hash &= IP6REASS_HMASK;
364 head = IP6Q_HEAD(hash);
368 * Enforce upper bound on number of fragments.
369 * If maxfrag is 0, never accept fragments.
370 * If maxfrag is -1, accept all fragments without limitation.
372 if (ip6_maxfrags < 0)
374 else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
377 for (q6 = head->ip6q_next; q6 != head; q6 = q6->ip6q_next)
378 if (ip6f->ip6f_ident == q6->ip6q_ident &&
379 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
380 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
382 && mac_ip6q_match(m, q6)
389 * the first fragment to arrive, create a reassembly queue.
394 * Enforce upper bound on number of fragmented packets
395 * for which we attempt reassembly;
396 * If maxfragpackets is 0, never accept fragments.
397 * If maxfragpackets is -1, accept all fragments without
400 if (V_ip6_maxfragpackets < 0)
402 else if (V_ip6q[hash].count >= V_ip6_maxfragbucketsize ||
403 atomic_load_int(&V_frag6_nfragpackets) >=
404 (u_int)V_ip6_maxfragpackets)
406 atomic_add_int(&V_frag6_nfragpackets, 1);
407 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
411 bzero(q6, sizeof(*q6));
413 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
417 mac_ip6q_create(m, q6);
419 frag6_insque_head(q6, head, hash);
421 /* ip6q_nxt will be filled afterwards, from 1st fragment */
422 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
424 q6->ip6q_nxtp = (u_char *)nxtp;
426 q6->ip6q_ident = ip6f->ip6f_ident;
427 q6->ip6q_ttl = IPV6_FRAGTTL;
428 q6->ip6q_src = ip6->ip6_src;
429 q6->ip6q_dst = ip6->ip6_dst;
431 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
432 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
438 * If it's the 1st fragment, record the length of the
439 * unfragmentable part and the next header of the fragment header.
441 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
443 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
444 sizeof(struct ip6_frag);
445 q6->ip6q_nxt = ip6f->ip6f_nxt;
449 * Check that the reassembled packet would not exceed 65535 bytes
451 * If it would exceed, discard the fragment and return an ICMP error.
453 if (q6->ip6q_unfrglen >= 0) {
454 /* The 1st fragment has already arrived. */
455 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
456 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
457 offset - sizeof(struct ip6_frag) +
458 offsetof(struct ip6_frag, ip6f_offlg));
460 return (IPPROTO_DONE);
462 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
463 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
464 offset - sizeof(struct ip6_frag) +
465 offsetof(struct ip6_frag, ip6f_offlg));
467 return (IPPROTO_DONE);
470 * If it's the first fragment, do the above check for each
471 * fragment already stored in the reassembly queue.
474 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
476 af6dwn = af6->ip6af_down;
478 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
480 struct mbuf *merr = IP6_REASS_MBUF(af6);
481 struct ip6_hdr *ip6err;
482 int erroff = af6->ip6af_offset;
484 /* dequeue the fragment. */
485 frag6_deq(af6, hash);
488 /* adjust pointer. */
489 ip6err = mtod(merr, struct ip6_hdr *);
492 * Restore source and destination addresses
493 * in the erroneous IPv6 header.
495 ip6err->ip6_src = q6->ip6q_src;
496 ip6err->ip6_dst = q6->ip6q_dst;
498 icmp6_error(merr, ICMP6_PARAM_PROB,
499 ICMP6_PARAMPROB_HEADER,
500 erroff - sizeof(struct ip6_frag) +
501 offsetof(struct ip6_frag, ip6f_offlg));
506 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
510 bzero(ip6af, sizeof(*ip6af));
511 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
512 ip6af->ip6af_off = fragoff;
513 ip6af->ip6af_frglen = frgpartlen;
514 ip6af->ip6af_offset = offset;
515 IP6_REASS_MBUF(ip6af) = m;
518 af6 = (struct ip6asfrag *)q6;
523 * Handle ECN by comparing this segment with the first one;
524 * if CE is set, do not lose CE.
525 * drop if CE and not-ECT are mixed for the same packet.
527 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
529 if (ecn == IPTOS_ECN_CE) {
530 if (ecn0 == IPTOS_ECN_NOTECT) {
531 free(ip6af, M_FTABLE);
534 if (ecn0 != IPTOS_ECN_CE)
535 q6->ip6q_ecn = IPTOS_ECN_CE;
537 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
538 free(ip6af, M_FTABLE);
543 * Find a segment which begins after this one does.
545 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
546 af6 = af6->ip6af_down)
547 if (af6->ip6af_off > ip6af->ip6af_off)
552 * If there is a preceding segment, it may provide some of
553 * our data already. If so, drop the data from the incoming
554 * segment. If it provides all of our data, drop us.
556 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
557 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
560 if (i >= ip6af->ip6af_frglen)
562 m_adj(IP6_REASS_MBUF(ip6af), i);
563 ip6af->ip6af_off += i;
564 ip6af->ip6af_frglen -= i;
569 * While we overlap succeeding segments trim them or,
570 * if they are completely covered, dequeue them.
572 while (af6 != (struct ip6asfrag *)q6 &&
573 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
574 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
575 if (i < af6->ip6af_frglen) {
576 af6->ip6af_frglen -= i;
578 m_adj(IP6_REASS_MBUF(af6), i);
581 af6 = af6->ip6af_down;
582 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
583 frag6_deq(af6->ip6af_up, hash);
587 * If the incoming framgent overlaps some existing fragments in
588 * the reassembly queue, drop it, since it is dangerous to override
589 * existing fragments from a security point of view.
590 * We don't know which fragment is the bad guy - here we trust
591 * fragment that came in earlier, with no real reason.
593 * Note: due to changes after disabling this part, mbuf passed to
594 * m_adj() below now does not meet the requirement.
596 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
597 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
600 #if 0 /* suppress the noisy log */
601 log(LOG_ERR, "%d bytes of a fragment from %s "
602 "overlaps the previous fragment\n",
603 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
605 free(ip6af, M_FTABLE);
609 if (af6 != (struct ip6asfrag *)q6) {
610 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
612 #if 0 /* suppress the noisy log */
613 log(LOG_ERR, "%d bytes of a fragment from %s "
614 "overlaps the succeeding fragment",
615 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
617 free(ip6af, M_FTABLE);
626 mac_ip6q_update(m, q6);
630 * Stick new segment in its place;
631 * check for complete reassembly.
632 * If not complete, check fragment limit.
633 * Move to front of packet queue, as we are
634 * the most recently active fragmented packet.
636 frag6_enq(ip6af, af6->ip6af_up, hash);
637 atomic_add_int(&frag6_nfrags, 1);
640 if (q6 != head->ip6q_next) {
641 frag6_remque(q6, hash);
642 frag6_insque_head(q6, head, hash);
646 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
647 af6 = af6->ip6af_down) {
648 if (af6->ip6af_off != next) {
649 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
650 IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
651 frag6_freef(q6, hash);
656 next += af6->ip6af_frglen;
658 if (af6->ip6af_up->ip6af_mff) {
659 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
660 IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
661 frag6_freef(q6, hash);
668 * Reassembly is complete; concatenate fragments.
670 ip6af = q6->ip6q_down;
671 t = m = IP6_REASS_MBUF(ip6af);
672 af6 = ip6af->ip6af_down;
673 frag6_deq(ip6af, hash);
674 while (af6 != (struct ip6asfrag *)q6) {
675 m->m_pkthdr.csum_flags &=
676 IP6_REASS_MBUF(af6)->m_pkthdr.csum_flags;
677 m->m_pkthdr.csum_data +=
678 IP6_REASS_MBUF(af6)->m_pkthdr.csum_data;
680 af6dwn = af6->ip6af_down;
681 frag6_deq(af6, hash);
684 m_adj(IP6_REASS_MBUF(af6), af6->ip6af_offset);
685 m_demote_pkthdr(IP6_REASS_MBUF(af6));
686 m_cat(t, IP6_REASS_MBUF(af6));
691 while (m->m_pkthdr.csum_data & 0xffff0000)
692 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
693 (m->m_pkthdr.csum_data >> 16);
695 /* adjust offset to point where the original next header starts */
696 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
697 free(ip6af, M_FTABLE);
698 ip6 = mtod(m, struct ip6_hdr *);
699 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
700 if (q6->ip6q_ecn == IPTOS_ECN_CE)
701 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
704 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
707 if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
708 frag6_remque(q6, hash);
709 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
711 mac_ip6q_destroy(q6);
714 atomic_subtract_int(&V_frag6_nfragpackets, 1);
720 * Store NXT to the original.
722 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
725 frag6_remque(q6, hash);
726 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
728 mac_ip6q_reassemble(q6, m);
729 mac_ip6q_destroy(q6);
732 atomic_subtract_int(&V_frag6_nfragpackets, 1);
734 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
736 for (t = m; t; t = t->m_next)
738 m->m_pkthdr.len = plen;
742 mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
747 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
748 ip6dc->ip6dc_nxt = nxt;
749 ip6dc->ip6dc_off = offset;
751 m_tag_prepend(m, mtag);
755 IP6STAT_INC(ip6s_reassembled);
756 in6_ifstat_inc(dstifp, ifs6_reass_ok);
760 * Queue/dispatch for reprocessing.
762 netisr_dispatch(NETISR_IPV6_DIRECT, m);
767 * Tell launch routine the next header
777 in6_ifstat_inc(dstifp, ifs6_reass_fail);
778 IP6STAT_INC(ip6s_fragdropped);
784 * Free a fragment reassembly header and all
785 * associated datagrams.
788 frag6_freef(struct ip6q *q6, uint32_t bucket)
790 struct ip6asfrag *af6, *down6;
792 IP6Q_LOCK_ASSERT(bucket);
794 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
796 struct mbuf *m = IP6_REASS_MBUF(af6);
798 down6 = af6->ip6af_down;
799 frag6_deq(af6, bucket);
802 * Return ICMP time exceeded error for the 1st fragment.
803 * Just free other fragments.
805 if (af6->ip6af_off == 0) {
809 ip6 = mtod(m, struct ip6_hdr *);
811 /* restore source and destination addresses */
812 ip6->ip6_src = q6->ip6q_src;
813 ip6->ip6_dst = q6->ip6q_dst;
815 icmp6_error(m, ICMP6_TIME_EXCEEDED,
816 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
821 frag6_remque(q6, bucket);
822 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
824 mac_ip6q_destroy(q6);
827 atomic_subtract_int(&V_frag6_nfragpackets, 1);
831 * Put an ip fragment on a reassembly chain.
832 * Like insque, but pointers in middle of structure.
835 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
836 uint32_t bucket __unused)
839 IP6Q_LOCK_ASSERT(bucket);
842 af6->ip6af_down = up6->ip6af_down;
843 up6->ip6af_down->ip6af_up = af6;
844 up6->ip6af_down = af6;
848 * To frag6_enq as remque is to insque.
851 frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
854 IP6Q_LOCK_ASSERT(bucket);
856 af6->ip6af_up->ip6af_down = af6->ip6af_down;
857 af6->ip6af_down->ip6af_up = af6->ip6af_up;
861 frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket)
864 IP6Q_LOCK_ASSERT(bucket);
865 KASSERT(IP6Q_HEAD(bucket) == old,
866 ("%s: attempt to insert at head of wrong bucket"
867 " (bucket=%u, old=%p)", __func__, bucket, old));
869 new->ip6q_prev = old;
870 new->ip6q_next = old->ip6q_next;
871 old->ip6q_next->ip6q_prev= new;
872 old->ip6q_next = new;
873 V_ip6q[bucket].count++;
877 frag6_remque(struct ip6q *p6, uint32_t bucket)
880 IP6Q_LOCK_ASSERT(bucket);
882 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
883 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
884 V_ip6q[bucket].count--;
888 * IPv6 reassembling timer processing;
889 * if a timer expires on a reassembly
895 VNET_ITERATOR_DECL(vnet_iter);
896 struct ip6q *head, *q6;
899 VNET_LIST_RLOCK_NOSLEEP();
900 VNET_FOREACH(vnet_iter) {
901 CURVNET_SET(vnet_iter);
902 for (i = 0; i < IP6REASS_NHASH; i++) {
905 q6 = head->ip6q_next;
908 * XXXJTL: This should never happen. This
909 * should turn into an assertion.
917 if (q6->ip6q_prev->ip6q_ttl == 0) {
918 IP6STAT_ADD(ip6s_fragtimeout,
919 q6->ip6q_prev->ip6q_nfrag);
920 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
921 frag6_freef(q6->ip6q_prev, i);
925 * If we are over the maximum number of fragments
926 * (due to the limit being lowered), drain off
927 * enough to get down to the new limit.
928 * Note that we drain all reassembly queues if
929 * maxfragpackets is 0 (fragmentation is disabled),
930 * and don't enforce a limit when maxfragpackets
933 while ((V_ip6_maxfragpackets == 0 ||
934 (V_ip6_maxfragpackets > 0 &&
935 V_ip6q[i].count > V_ip6_maxfragbucketsize)) &&
936 head->ip6q_prev != head) {
937 IP6STAT_ADD(ip6s_fragoverflow,
938 q6->ip6q_prev->ip6q_nfrag);
939 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
940 frag6_freef(head->ip6q_prev, i);
945 * If we are still over the maximum number of fragmented
946 * packets, drain off enough to get down to the new limit.
949 while (V_ip6_maxfragpackets >= 0 &&
950 atomic_load_int(&V_frag6_nfragpackets) >
951 (u_int)V_ip6_maxfragpackets) {
954 if (head->ip6q_prev != head) {
955 IP6STAT_ADD(ip6s_fragoverflow,
956 q6->ip6q_prev->ip6q_nfrag);
957 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
958 frag6_freef(head->ip6q_prev, i);
961 i = (i + 1) % IP6REASS_NHASH;
965 VNET_LIST_RUNLOCK_NOSLEEP();
969 * Drain off all datagram fragments.
974 VNET_ITERATOR_DECL(vnet_iter);
978 VNET_LIST_RLOCK_NOSLEEP();
979 VNET_FOREACH(vnet_iter) {
980 CURVNET_SET(vnet_iter);
981 for (i = 0; i < IP6REASS_NHASH; i++) {
982 if (IP6Q_TRYLOCK(i) == 0)
985 while (head->ip6q_next != head) {
986 IP6STAT_INC(ip6s_fragdropped);
987 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
988 frag6_freef(head->ip6q_next, i);
994 VNET_LIST_RUNLOCK_NOSLEEP();
998 ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
1000 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1003 /* Delete frag6 header. */
1004 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
1005 /* This is the only possible case with !PULLDOWN_TEST. */
1006 bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
1008 m->m_data += sizeof(struct ip6_frag);
1009 m->m_len -= sizeof(struct ip6_frag);
1011 /* This comes with no copy if the boundary is on cluster. */
1012 if ((t = m_split(m, offset, wait)) == NULL)
1014 m_adj(t, sizeof(struct ip6_frag));
1018 m->m_flags |= M_FRAGMENTED;