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/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/domain.h>
45 #include <sys/eventhandler.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
50 #include <sys/kernel.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 */
69 #include <security/mac/mac_framework.h>
72 * Reassembly headers are stored in hash buckets.
74 #define IP6REASS_NHASH_LOG2 10
75 #define IP6REASS_NHASH (1 << IP6REASS_NHASH_LOG2)
76 #define IP6REASS_HMASK (IP6REASS_NHASH - 1)
78 static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *,
79 uint32_t bucket __unused);
80 static void frag6_deq(struct ip6asfrag *, uint32_t bucket __unused);
81 static void frag6_insque_head(struct ip6q *, struct ip6q *,
83 static void frag6_remque(struct ip6q *, uint32_t bucket);
84 static void frag6_freef(struct ip6q *, uint32_t bucket);
92 VNET_DEFINE_STATIC(volatile u_int, frag6_nfragpackets);
93 volatile u_int frag6_nfrags = 0;
94 VNET_DEFINE_STATIC(struct ip6qbucket, ip6q[IP6REASS_NHASH]);
95 VNET_DEFINE_STATIC(uint32_t, ip6q_hashseed);
97 #define V_frag6_nfragpackets VNET(frag6_nfragpackets)
98 #define V_ip6q VNET(ip6q)
99 #define V_ip6q_hashseed VNET(ip6q_hashseed)
101 #define IP6Q_LOCK(i) mtx_lock(&V_ip6q[(i)].lock)
102 #define IP6Q_TRYLOCK(i) mtx_trylock(&V_ip6q[(i)].lock)
103 #define IP6Q_LOCK_ASSERT(i) mtx_assert(&V_ip6q[(i)].lock, MA_OWNED)
104 #define IP6Q_UNLOCK(i) mtx_unlock(&V_ip6q[(i)].lock)
105 #define IP6Q_HEAD(i) (&V_ip6q[(i)].ip6q)
107 static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
110 * By default, limit the number of IP6 fragments across all reassembly
111 * queues to 1/32 of the total number of mbuf clusters.
113 * Limit the total number of reassembly queues per VNET to the
114 * IP6 fragment limit, but ensure the limit will not allow any bucket
115 * to grow above 100 items. (The bucket limit is
116 * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct
117 * multiplier to reach a 100-item limit.)
118 * The 100-item limit was chosen as brief testing seems to show that
119 * this produces "reasonable" performance on some subset of systems
122 #define IP6_MAXFRAGS (nmbclusters / 32)
123 #define IP6_MAXFRAGPACKETS (imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
126 * Initialise reassembly queue and fragment identifier.
129 frag6_set_bucketsize()
133 if ((i = V_ip6_maxfragpackets) > 0)
134 V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
138 frag6_change(void *tag)
140 VNET_ITERATOR_DECL(vnet_iter);
142 ip6_maxfrags = IP6_MAXFRAGS;
143 VNET_LIST_RLOCK_NOSLEEP();
144 VNET_FOREACH(vnet_iter) {
145 CURVNET_SET(vnet_iter);
146 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
147 frag6_set_bucketsize();
150 VNET_LIST_RUNLOCK_NOSLEEP();
159 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
160 frag6_set_bucketsize();
161 for (i = 0; i < IP6REASS_NHASH; i++) {
163 q6->ip6q_next = q6->ip6q_prev = q6;
164 mtx_init(&V_ip6q[i].lock, "ip6qlock", NULL, MTX_DEF);
167 V_ip6q_hashseed = arc4random();
168 V_ip6_maxfragsperpacket = 64;
169 if (!IS_DEFAULT_VNET(curvnet))
172 ip6_maxfrags = IP6_MAXFRAGS;
173 EVENTHANDLER_REGISTER(nmbclusters_change,
174 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
178 * In RFC2460, fragment and reassembly rule do not agree with each other,
179 * in terms of next header field handling in fragment header.
180 * While the sender will use the same value for all of the fragmented packets,
181 * receiver is suggested not to check the consistency.
183 * fragment rule (p20):
184 * (2) A Fragment header containing:
185 * The Next Header value that identifies the first header of
186 * the Fragmentable Part of the original packet.
187 * -> next header field is same for all fragments
189 * reassembly rule (p21):
190 * The Next Header field of the last header of the Unfragmentable
191 * Part is obtained from the Next Header field of the first
192 * fragment's Fragment header.
193 * -> should grab it from the first fragment only
195 * The following note also contradicts with fragment rule - no one is going to
196 * send different fragment with different next header field.
198 * additional note (p22):
199 * The Next Header values in the Fragment headers of different
200 * fragments of the same original packet may differ. Only the value
201 * from the Offset zero fragment packet is used for reassembly.
202 * -> should grab it from the first fragment only
204 * There is no explicit reason given in the RFC. Historical reason maybe?
210 frag6_input(struct mbuf **mp, int *offp, int proto)
212 struct mbuf *m = *mp, *t;
214 struct ip6_frag *ip6f;
215 struct ip6q *head, *q6;
216 struct ip6asfrag *af6, *ip6af, *af6dwn;
217 struct in6_ifaddr *ia;
218 int offset = *offp, nxt, i, next;
220 int fragoff, frgpartlen; /* must be larger than u_int16_t */
221 uint32_t hashkey[(sizeof(struct in6_addr) * 2 +
222 sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)];
223 uint32_t hash, *hashkeyp;
224 struct ifnet *dstifp;
228 struct ip6_direct_ctx *ip6dc;
232 char ip6buf[INET6_ADDRSTRLEN];
235 ip6 = mtod(m, struct ip6_hdr *);
236 #ifndef PULLDOWN_TEST
237 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
238 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
240 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
242 return (IPPROTO_DONE);
246 /* find the destination interface of the packet. */
247 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
250 ifa_free(&ia->ia_ifa);
252 /* jumbo payload can't contain a fragment header */
253 if (ip6->ip6_plen == 0) {
254 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
255 in6_ifstat_inc(dstifp, ifs6_reass_fail);
260 * check whether fragment packet's fragment length is
261 * multiple of 8 octets.
262 * sizeof(struct ip6_frag) == 8
263 * sizeof(struct ip6_hdr) = 40
265 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
266 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
267 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
268 offsetof(struct ip6_hdr, ip6_plen));
269 in6_ifstat_inc(dstifp, ifs6_reass_fail);
273 IP6STAT_INC(ip6s_fragments);
274 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
276 /* offset now points to data portion */
277 offset += sizeof(struct ip6_frag);
280 * Handle "atomic" fragments (offset and m bit set to 0) upfront,
281 * unrelated to any reassembly (see RFC 6946 and section 4.5 of RFC
282 * 8200). Just skip the fragment header.
284 if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
285 IP6STAT_INC(ip6s_atomicfrags);
286 in6_ifstat_inc(dstifp, ifs6_reass_ok);
288 m->m_flags |= M_FRAGMENTED;
289 return (ip6f->ip6f_nxt);
292 /* Get fragment length and discard 0-byte fragments. */
293 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
294 if (frgpartlen == 0) {
295 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
296 offsetof(struct ip6_hdr, ip6_plen));
297 in6_ifstat_inc(dstifp, ifs6_reass_fail);
298 IP6STAT_INC(ip6s_fragdropped);
303 memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
304 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
305 memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
306 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
307 *hashkeyp = ip6f->ip6f_ident;
308 hash = jenkins_hash32(hashkey, nitems(hashkey), V_ip6q_hashseed);
309 hash &= IP6REASS_HMASK;
310 head = IP6Q_HEAD(hash);
314 * Enforce upper bound on number of fragments.
315 * If maxfrag is 0, never accept fragments.
316 * If maxfrag is -1, accept all fragments without limitation.
318 if (ip6_maxfrags < 0)
320 else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
323 for (q6 = head->ip6q_next; q6 != head; q6 = q6->ip6q_next)
324 if (ip6f->ip6f_ident == q6->ip6q_ident &&
325 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
326 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
328 && mac_ip6q_match(m, q6)
335 * the first fragment to arrive, create a reassembly queue.
340 * Enforce upper bound on number of fragmented packets
341 * for which we attempt reassembly;
342 * If maxfragpackets is 0, never accept fragments.
343 * If maxfragpackets is -1, accept all fragments without
346 if (V_ip6_maxfragpackets < 0)
348 else if (V_ip6q[hash].count >= V_ip6_maxfragbucketsize ||
349 atomic_load_int(&V_frag6_nfragpackets) >=
350 (u_int)V_ip6_maxfragpackets)
352 atomic_add_int(&V_frag6_nfragpackets, 1);
353 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
357 bzero(q6, sizeof(*q6));
359 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
363 mac_ip6q_create(m, q6);
365 frag6_insque_head(q6, head, hash);
367 /* ip6q_nxt will be filled afterwards, from 1st fragment */
368 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
370 q6->ip6q_nxtp = (u_char *)nxtp;
372 q6->ip6q_ident = ip6f->ip6f_ident;
373 q6->ip6q_ttl = IPV6_FRAGTTL;
374 q6->ip6q_src = ip6->ip6_src;
375 q6->ip6q_dst = ip6->ip6_dst;
377 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
378 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
384 * If it's the 1st fragment, record the length of the
385 * unfragmentable part and the next header of the fragment header.
387 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
389 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
390 sizeof(struct ip6_frag);
391 q6->ip6q_nxt = ip6f->ip6f_nxt;
395 * Check that the reassembled packet would not exceed 65535 bytes
397 * If it would exceed, discard the fragment and return an ICMP error.
399 if (q6->ip6q_unfrglen >= 0) {
400 /* The 1st fragment has already arrived. */
401 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
402 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
403 offset - sizeof(struct ip6_frag) +
404 offsetof(struct ip6_frag, ip6f_offlg));
406 return (IPPROTO_DONE);
408 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
409 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
410 offset - sizeof(struct ip6_frag) +
411 offsetof(struct ip6_frag, ip6f_offlg));
413 return (IPPROTO_DONE);
416 * If it's the first fragment, do the above check for each
417 * fragment already stored in the reassembly queue.
420 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
422 af6dwn = af6->ip6af_down;
424 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
426 struct mbuf *merr = IP6_REASS_MBUF(af6);
427 struct ip6_hdr *ip6err;
428 int erroff = af6->ip6af_offset;
430 /* dequeue the fragment. */
431 frag6_deq(af6, hash);
434 /* adjust pointer. */
435 ip6err = mtod(merr, struct ip6_hdr *);
438 * Restore source and destination addresses
439 * in the erroneous IPv6 header.
441 ip6err->ip6_src = q6->ip6q_src;
442 ip6err->ip6_dst = q6->ip6q_dst;
444 icmp6_error(merr, ICMP6_PARAM_PROB,
445 ICMP6_PARAMPROB_HEADER,
446 erroff - sizeof(struct ip6_frag) +
447 offsetof(struct ip6_frag, ip6f_offlg));
452 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
456 bzero(ip6af, sizeof(*ip6af));
457 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
458 ip6af->ip6af_off = fragoff;
459 ip6af->ip6af_frglen = frgpartlen;
460 ip6af->ip6af_offset = offset;
461 IP6_REASS_MBUF(ip6af) = m;
464 af6 = (struct ip6asfrag *)q6;
469 * Handle ECN by comparing this segment with the first one;
470 * if CE is set, do not lose CE.
471 * drop if CE and not-ECT are mixed for the same packet.
473 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
475 if (ecn == IPTOS_ECN_CE) {
476 if (ecn0 == IPTOS_ECN_NOTECT) {
477 free(ip6af, M_FTABLE);
480 if (ecn0 != IPTOS_ECN_CE)
481 q6->ip6q_ecn = IPTOS_ECN_CE;
483 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
484 free(ip6af, M_FTABLE);
489 * Find a segment which begins after this one does.
491 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
492 af6 = af6->ip6af_down)
493 if (af6->ip6af_off > ip6af->ip6af_off)
498 * If there is a preceding segment, it may provide some of
499 * our data already. If so, drop the data from the incoming
500 * segment. If it provides all of our data, drop us.
502 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
503 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
506 if (i >= ip6af->ip6af_frglen)
508 m_adj(IP6_REASS_MBUF(ip6af), i);
509 ip6af->ip6af_off += i;
510 ip6af->ip6af_frglen -= i;
515 * While we overlap succeeding segments trim them or,
516 * if they are completely covered, dequeue them.
518 while (af6 != (struct ip6asfrag *)q6 &&
519 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
520 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
521 if (i < af6->ip6af_frglen) {
522 af6->ip6af_frglen -= i;
524 m_adj(IP6_REASS_MBUF(af6), i);
527 af6 = af6->ip6af_down;
528 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
529 frag6_deq(af6->ip6af_up, hash);
533 * If the incoming framgent overlaps some existing fragments in
534 * the reassembly queue, drop it, since it is dangerous to override
535 * existing fragments from a security point of view.
536 * We don't know which fragment is the bad guy - here we trust
537 * fragment that came in earlier, with no real reason.
539 * Note: due to changes after disabling this part, mbuf passed to
540 * m_adj() below now does not meet the requirement.
542 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
543 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
546 #if 0 /* suppress the noisy log */
547 log(LOG_ERR, "%d bytes of a fragment from %s "
548 "overlaps the previous fragment\n",
549 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
551 free(ip6af, M_FTABLE);
555 if (af6 != (struct ip6asfrag *)q6) {
556 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
558 #if 0 /* suppress the noisy log */
559 log(LOG_ERR, "%d bytes of a fragment from %s "
560 "overlaps the succeeding fragment",
561 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
563 free(ip6af, M_FTABLE);
572 mac_ip6q_update(m, q6);
576 * Stick new segment in its place;
577 * check for complete reassembly.
578 * If not complete, check fragment limit.
579 * Move to front of packet queue, as we are
580 * the most recently active fragmented packet.
582 frag6_enq(ip6af, af6->ip6af_up, hash);
583 atomic_add_int(&frag6_nfrags, 1);
586 if (q6 != head->ip6q_next) {
587 frag6_remque(q6, hash);
588 frag6_insque_head(q6, head, hash);
592 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
593 af6 = af6->ip6af_down) {
594 if (af6->ip6af_off != next) {
595 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
596 IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
597 frag6_freef(q6, hash);
602 next += af6->ip6af_frglen;
604 if (af6->ip6af_up->ip6af_mff) {
605 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
606 IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
607 frag6_freef(q6, hash);
614 * Reassembly is complete; concatenate fragments.
616 ip6af = q6->ip6q_down;
617 t = m = IP6_REASS_MBUF(ip6af);
618 af6 = ip6af->ip6af_down;
619 frag6_deq(ip6af, hash);
620 while (af6 != (struct ip6asfrag *)q6) {
621 m->m_pkthdr.csum_flags &=
622 IP6_REASS_MBUF(af6)->m_pkthdr.csum_flags;
623 m->m_pkthdr.csum_data +=
624 IP6_REASS_MBUF(af6)->m_pkthdr.csum_data;
626 af6dwn = af6->ip6af_down;
627 frag6_deq(af6, hash);
630 m_adj(IP6_REASS_MBUF(af6), af6->ip6af_offset);
631 m_demote_pkthdr(IP6_REASS_MBUF(af6));
632 m_cat(t, IP6_REASS_MBUF(af6));
637 while (m->m_pkthdr.csum_data & 0xffff0000)
638 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
639 (m->m_pkthdr.csum_data >> 16);
641 /* adjust offset to point where the original next header starts */
642 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
643 free(ip6af, M_FTABLE);
644 ip6 = mtod(m, struct ip6_hdr *);
645 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
646 if (q6->ip6q_ecn == IPTOS_ECN_CE)
647 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
650 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
653 if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
654 frag6_remque(q6, hash);
655 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
657 mac_ip6q_destroy(q6);
660 atomic_subtract_int(&V_frag6_nfragpackets, 1);
666 * Store NXT to the original.
668 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
671 frag6_remque(q6, hash);
672 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
674 mac_ip6q_reassemble(q6, m);
675 mac_ip6q_destroy(q6);
678 atomic_subtract_int(&V_frag6_nfragpackets, 1);
680 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
682 for (t = m; t; t = t->m_next)
684 m->m_pkthdr.len = plen;
688 mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
693 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
694 ip6dc->ip6dc_nxt = nxt;
695 ip6dc->ip6dc_off = offset;
697 m_tag_prepend(m, mtag);
701 IP6STAT_INC(ip6s_reassembled);
702 in6_ifstat_inc(dstifp, ifs6_reass_ok);
706 * Queue/dispatch for reprocessing.
708 netisr_dispatch(NETISR_IPV6_DIRECT, m);
713 * Tell launch routine the next header
723 in6_ifstat_inc(dstifp, ifs6_reass_fail);
724 IP6STAT_INC(ip6s_fragdropped);
730 * Free a fragment reassembly header and all
731 * associated datagrams.
734 frag6_freef(struct ip6q *q6, uint32_t bucket)
736 struct ip6asfrag *af6, *down6;
738 IP6Q_LOCK_ASSERT(bucket);
740 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
742 struct mbuf *m = IP6_REASS_MBUF(af6);
744 down6 = af6->ip6af_down;
745 frag6_deq(af6, bucket);
748 * Return ICMP time exceeded error for the 1st fragment.
749 * Just free other fragments.
751 if (af6->ip6af_off == 0) {
755 ip6 = mtod(m, struct ip6_hdr *);
757 /* restore source and destination addresses */
758 ip6->ip6_src = q6->ip6q_src;
759 ip6->ip6_dst = q6->ip6q_dst;
761 icmp6_error(m, ICMP6_TIME_EXCEEDED,
762 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
767 frag6_remque(q6, bucket);
768 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
770 mac_ip6q_destroy(q6);
773 atomic_subtract_int(&V_frag6_nfragpackets, 1);
777 * Put an ip fragment on a reassembly chain.
778 * Like insque, but pointers in middle of structure.
781 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
782 uint32_t bucket __unused)
785 IP6Q_LOCK_ASSERT(bucket);
788 af6->ip6af_down = up6->ip6af_down;
789 up6->ip6af_down->ip6af_up = af6;
790 up6->ip6af_down = af6;
794 * To frag6_enq as remque is to insque.
797 frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
800 IP6Q_LOCK_ASSERT(bucket);
802 af6->ip6af_up->ip6af_down = af6->ip6af_down;
803 af6->ip6af_down->ip6af_up = af6->ip6af_up;
807 frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket)
810 IP6Q_LOCK_ASSERT(bucket);
811 KASSERT(IP6Q_HEAD(bucket) == old,
812 ("%s: attempt to insert at head of wrong bucket"
813 " (bucket=%u, old=%p)", __func__, bucket, old));
815 new->ip6q_prev = old;
816 new->ip6q_next = old->ip6q_next;
817 old->ip6q_next->ip6q_prev= new;
818 old->ip6q_next = new;
819 V_ip6q[bucket].count++;
823 frag6_remque(struct ip6q *p6, uint32_t bucket)
826 IP6Q_LOCK_ASSERT(bucket);
828 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
829 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
830 V_ip6q[bucket].count--;
834 * IPv6 reassembling timer processing;
835 * if a timer expires on a reassembly
841 VNET_ITERATOR_DECL(vnet_iter);
842 struct ip6q *head, *q6;
845 VNET_LIST_RLOCK_NOSLEEP();
846 VNET_FOREACH(vnet_iter) {
847 CURVNET_SET(vnet_iter);
848 for (i = 0; i < IP6REASS_NHASH; i++) {
851 q6 = head->ip6q_next;
854 * XXXJTL: This should never happen. This
855 * should turn into an assertion.
863 if (q6->ip6q_prev->ip6q_ttl == 0) {
864 IP6STAT_ADD(ip6s_fragtimeout,
865 q6->ip6q_prev->ip6q_nfrag);
866 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
867 frag6_freef(q6->ip6q_prev, i);
871 * If we are over the maximum number of fragments
872 * (due to the limit being lowered), drain off
873 * enough to get down to the new limit.
874 * Note that we drain all reassembly queues if
875 * maxfragpackets is 0 (fragmentation is disabled),
876 * and don't enforce a limit when maxfragpackets
879 while ((V_ip6_maxfragpackets == 0 ||
880 (V_ip6_maxfragpackets > 0 &&
881 V_ip6q[i].count > V_ip6_maxfragbucketsize)) &&
882 head->ip6q_prev != head) {
883 IP6STAT_ADD(ip6s_fragoverflow,
884 q6->ip6q_prev->ip6q_nfrag);
885 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
886 frag6_freef(head->ip6q_prev, i);
891 * If we are still over the maximum number of fragmented
892 * packets, drain off enough to get down to the new limit.
895 while (V_ip6_maxfragpackets >= 0 &&
896 atomic_load_int(&V_frag6_nfragpackets) >
897 (u_int)V_ip6_maxfragpackets) {
900 if (head->ip6q_prev != head) {
901 IP6STAT_ADD(ip6s_fragoverflow,
902 q6->ip6q_prev->ip6q_nfrag);
903 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
904 frag6_freef(head->ip6q_prev, i);
907 i = (i + 1) % IP6REASS_NHASH;
911 VNET_LIST_RUNLOCK_NOSLEEP();
915 * Drain off all datagram fragments.
920 VNET_ITERATOR_DECL(vnet_iter);
924 VNET_LIST_RLOCK_NOSLEEP();
925 VNET_FOREACH(vnet_iter) {
926 CURVNET_SET(vnet_iter);
927 for (i = 0; i < IP6REASS_NHASH; i++) {
928 if (IP6Q_TRYLOCK(i) == 0)
931 while (head->ip6q_next != head) {
932 IP6STAT_INC(ip6s_fragdropped);
933 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
934 frag6_freef(head->ip6q_next, i);
940 VNET_LIST_RUNLOCK_NOSLEEP();
944 ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
946 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
949 /* Delete frag6 header. */
950 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
951 /* This is the only possible case with !PULLDOWN_TEST. */
952 bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
954 m->m_data += sizeof(struct ip6_frag);
955 m->m_len -= sizeof(struct ip6_frag);
957 /* This comes with no copy if the boundary is on cluster. */
958 if ((t = m_split(m, offset, wait)) == NULL)
960 m_adj(t, sizeof(struct ip6_frag));
964 m->m_flags |= M_FRAGMENTED;