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 6
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 *,
82 uint32_t bucket __unused);
83 static void frag6_remque(struct ip6q *, uint32_t bucket __unused);
84 static void frag6_freef(struct ip6q *, uint32_t bucket);
91 VNET_DEFINE_STATIC(volatile u_int, frag6_nfragpackets);
92 volatile u_int frag6_nfrags = 0;
93 VNET_DEFINE_STATIC(struct ip6qbucket, ip6q[IP6REASS_NHASH]);
94 VNET_DEFINE_STATIC(uint32_t, ip6q_hashseed);
96 #define V_frag6_nfragpackets VNET(frag6_nfragpackets)
97 #define V_ip6q VNET(ip6q)
98 #define V_ip6q_hashseed VNET(ip6q_hashseed)
100 #define IP6Q_LOCK(i) mtx_lock(&V_ip6q[(i)].lock)
101 #define IP6Q_TRYLOCK(i) mtx_trylock(&V_ip6q[(i)].lock)
102 #define IP6Q_LOCK_ASSERT(i) mtx_assert(&V_ip6q[(i)].lock, MA_OWNED)
103 #define IP6Q_UNLOCK(i) mtx_unlock(&V_ip6q[(i)].lock)
104 #define IP6Q_HEAD(i) (&V_ip6q[(i)].ip6q)
106 static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
109 * Initialise reassembly queue and fragment identifier.
112 frag6_change(void *tag)
114 VNET_ITERATOR_DECL(vnet_iter);
116 ip6_maxfrags = nmbclusters / 4;
117 VNET_LIST_RLOCK_NOSLEEP();
118 VNET_FOREACH(vnet_iter) {
119 CURVNET_SET(vnet_iter);
120 V_ip6_maxfragpackets = nmbclusters / 4;
123 VNET_LIST_RUNLOCK_NOSLEEP();
132 V_ip6_maxfragpackets = nmbclusters / 4;
133 for (i = 0; i < IP6REASS_NHASH; i++) {
135 q6->ip6q_next = q6->ip6q_prev = q6;
136 mtx_init(&V_ip6q[i].lock, "ip6qlock", NULL, MTX_DEF);
138 V_ip6q_hashseed = arc4random();
139 if (!IS_DEFAULT_VNET(curvnet))
142 ip6_maxfrags = nmbclusters / 4;
143 EVENTHANDLER_REGISTER(nmbclusters_change,
144 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
148 * In RFC2460, fragment and reassembly rule do not agree with each other,
149 * in terms of next header field handling in fragment header.
150 * While the sender will use the same value for all of the fragmented packets,
151 * receiver is suggested not to check the consistency.
153 * fragment rule (p20):
154 * (2) A Fragment header containing:
155 * The Next Header value that identifies the first header of
156 * the Fragmentable Part of the original packet.
157 * -> next header field is same for all fragments
159 * reassembly rule (p21):
160 * The Next Header field of the last header of the Unfragmentable
161 * Part is obtained from the Next Header field of the first
162 * fragment's Fragment header.
163 * -> should grab it from the first fragment only
165 * The following note also contradicts with fragment rule - no one is going to
166 * send different fragment with different next header field.
168 * additional note (p22):
169 * The Next Header values in the Fragment headers of different
170 * fragments of the same original packet may differ. Only the value
171 * from the Offset zero fragment packet is used for reassembly.
172 * -> should grab it from the first fragment only
174 * There is no explicit reason given in the RFC. Historical reason maybe?
180 frag6_input(struct mbuf **mp, int *offp, int proto)
182 struct mbuf *m = *mp, *t;
184 struct ip6_frag *ip6f;
185 struct ip6q *head, *q6;
186 struct ip6asfrag *af6, *ip6af, *af6dwn;
187 struct in6_ifaddr *ia;
188 int offset = *offp, nxt, i, next;
190 int fragoff, frgpartlen; /* must be larger than u_int16_t */
191 uint32_t hash, hashkey[sizeof(struct in6_addr) * 2 + 1], *hashkeyp;
192 struct ifnet *dstifp;
196 struct ip6_direct_ctx *ip6dc;
200 char ip6buf[INET6_ADDRSTRLEN];
203 ip6 = mtod(m, struct ip6_hdr *);
204 #ifndef PULLDOWN_TEST
205 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
206 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
208 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
210 return (IPPROTO_DONE);
214 /* find the destination interface of the packet. */
215 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
218 ifa_free(&ia->ia_ifa);
220 /* jumbo payload can't contain a fragment header */
221 if (ip6->ip6_plen == 0) {
222 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
223 in6_ifstat_inc(dstifp, ifs6_reass_fail);
228 * check whether fragment packet's fragment length is
229 * multiple of 8 octets.
230 * sizeof(struct ip6_frag) == 8
231 * sizeof(struct ip6_hdr) = 40
233 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
234 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
235 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
236 offsetof(struct ip6_hdr, ip6_plen));
237 in6_ifstat_inc(dstifp, ifs6_reass_fail);
241 IP6STAT_INC(ip6s_fragments);
242 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
244 /* offset now points to data portion */
245 offset += sizeof(struct ip6_frag);
248 * RFC 6946: Handle "atomic" fragments (offset and m bit set to 0)
249 * upfront, unrelated to any reassembly. Just skip the fragment header.
251 if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
252 /* XXX-BZ we want dedicated counters for this. */
253 IP6STAT_INC(ip6s_reassembled);
254 in6_ifstat_inc(dstifp, ifs6_reass_ok);
256 m->m_flags |= M_FRAGMENTED;
257 return (ip6f->ip6f_nxt);
261 memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
262 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
263 memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
264 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
265 *hashkeyp = ip6f->ip6f_ident;
266 hash = jenkins_hash32(hashkey, nitems(hashkey), V_ip6q_hashseed);
267 hash &= IP6REASS_HMASK;
268 head = IP6Q_HEAD(hash);
272 * Enforce upper bound on number of fragments.
273 * If maxfrag is 0, never accept fragments.
274 * If maxfrag is -1, accept all fragments without limitation.
276 if (ip6_maxfrags < 0)
278 else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
281 for (q6 = head->ip6q_next; q6 != head; q6 = q6->ip6q_next)
282 if (ip6f->ip6f_ident == q6->ip6q_ident &&
283 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
284 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
286 && mac_ip6q_match(m, q6)
293 * the first fragment to arrive, create a reassembly queue.
298 * Enforce upper bound on number of fragmented packets
299 * for which we attempt reassembly;
300 * If maxfragpackets is 0, never accept fragments.
301 * If maxfragpackets is -1, accept all fragments without
304 if (V_ip6_maxfragpackets < 0)
306 else if (atomic_load_int(&V_frag6_nfragpackets) >=
307 (u_int)V_ip6_maxfragpackets)
309 atomic_add_int(&V_frag6_nfragpackets, 1);
310 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
314 bzero(q6, sizeof(*q6));
316 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
320 mac_ip6q_create(m, q6);
322 frag6_insque_head(q6, head, hash);
324 /* ip6q_nxt will be filled afterwards, from 1st fragment */
325 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
327 q6->ip6q_nxtp = (u_char *)nxtp;
329 q6->ip6q_ident = ip6f->ip6f_ident;
330 q6->ip6q_ttl = IPV6_FRAGTTL;
331 q6->ip6q_src = ip6->ip6_src;
332 q6->ip6q_dst = ip6->ip6_dst;
334 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
335 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
341 * If it's the 1st fragment, record the length of the
342 * unfragmentable part and the next header of the fragment header.
344 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
346 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
347 sizeof(struct ip6_frag);
348 q6->ip6q_nxt = ip6f->ip6f_nxt;
352 * Check that the reassembled packet would not exceed 65535 bytes
354 * If it would exceed, discard the fragment and return an ICMP error.
356 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
357 if (q6->ip6q_unfrglen >= 0) {
358 /* The 1st fragment has already arrived. */
359 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
360 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
361 offset - sizeof(struct ip6_frag) +
362 offsetof(struct ip6_frag, ip6f_offlg));
364 return (IPPROTO_DONE);
366 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
367 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
368 offset - sizeof(struct ip6_frag) +
369 offsetof(struct ip6_frag, ip6f_offlg));
371 return (IPPROTO_DONE);
374 * If it's the first fragment, do the above check for each
375 * fragment already stored in the reassembly queue.
378 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
380 af6dwn = af6->ip6af_down;
382 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
384 struct mbuf *merr = IP6_REASS_MBUF(af6);
385 struct ip6_hdr *ip6err;
386 int erroff = af6->ip6af_offset;
388 /* dequeue the fragment. */
389 frag6_deq(af6, hash);
392 /* adjust pointer. */
393 ip6err = mtod(merr, struct ip6_hdr *);
396 * Restore source and destination addresses
397 * in the erroneous IPv6 header.
399 ip6err->ip6_src = q6->ip6q_src;
400 ip6err->ip6_dst = q6->ip6q_dst;
402 icmp6_error(merr, ICMP6_PARAM_PROB,
403 ICMP6_PARAMPROB_HEADER,
404 erroff - sizeof(struct ip6_frag) +
405 offsetof(struct ip6_frag, ip6f_offlg));
410 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
414 bzero(ip6af, sizeof(*ip6af));
415 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
416 ip6af->ip6af_off = fragoff;
417 ip6af->ip6af_frglen = frgpartlen;
418 ip6af->ip6af_offset = offset;
419 IP6_REASS_MBUF(ip6af) = m;
422 af6 = (struct ip6asfrag *)q6;
427 * Handle ECN by comparing this segment with the first one;
428 * if CE is set, do not lose CE.
429 * drop if CE and not-ECT are mixed for the same packet.
431 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
433 if (ecn == IPTOS_ECN_CE) {
434 if (ecn0 == IPTOS_ECN_NOTECT) {
435 free(ip6af, M_FTABLE);
438 if (ecn0 != IPTOS_ECN_CE)
439 q6->ip6q_ecn = IPTOS_ECN_CE;
441 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
442 free(ip6af, M_FTABLE);
447 * Find a segment which begins after this one does.
449 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
450 af6 = af6->ip6af_down)
451 if (af6->ip6af_off > ip6af->ip6af_off)
456 * If there is a preceding segment, it may provide some of
457 * our data already. If so, drop the data from the incoming
458 * segment. If it provides all of our data, drop us.
460 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
461 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
464 if (i >= ip6af->ip6af_frglen)
466 m_adj(IP6_REASS_MBUF(ip6af), i);
467 ip6af->ip6af_off += i;
468 ip6af->ip6af_frglen -= i;
473 * While we overlap succeeding segments trim them or,
474 * if they are completely covered, dequeue them.
476 while (af6 != (struct ip6asfrag *)q6 &&
477 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
478 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
479 if (i < af6->ip6af_frglen) {
480 af6->ip6af_frglen -= i;
482 m_adj(IP6_REASS_MBUF(af6), i);
485 af6 = af6->ip6af_down;
486 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
487 frag6_deq(af6->ip6af_up, hash);
491 * If the incoming framgent overlaps some existing fragments in
492 * the reassembly queue, drop it, since it is dangerous to override
493 * existing fragments from a security point of view.
494 * We don't know which fragment is the bad guy - here we trust
495 * fragment that came in earlier, with no real reason.
497 * Note: due to changes after disabling this part, mbuf passed to
498 * m_adj() below now does not meet the requirement.
500 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
501 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
504 #if 0 /* suppress the noisy log */
505 log(LOG_ERR, "%d bytes of a fragment from %s "
506 "overlaps the previous fragment\n",
507 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
509 free(ip6af, M_FTABLE);
513 if (af6 != (struct ip6asfrag *)q6) {
514 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
516 #if 0 /* suppress the noisy log */
517 log(LOG_ERR, "%d bytes of a fragment from %s "
518 "overlaps the succeeding fragment",
519 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
521 free(ip6af, M_FTABLE);
530 mac_ip6q_update(m, q6);
534 * Stick new segment in its place;
535 * check for complete reassembly.
536 * Move to front of packet queue, as we are
537 * the most recently active fragmented packet.
539 frag6_enq(ip6af, af6->ip6af_up, hash);
540 atomic_add_int(&frag6_nfrags, 1);
543 if (q6 != head->ip6q_next) {
544 frag6_remque(q6, hash);
545 frag6_insque_head(q6, head, hash);
549 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
550 af6 = af6->ip6af_down) {
551 if (af6->ip6af_off != next) {
555 next += af6->ip6af_frglen;
557 if (af6->ip6af_up->ip6af_mff) {
563 * Reassembly is complete; concatenate fragments.
565 ip6af = q6->ip6q_down;
566 t = m = IP6_REASS_MBUF(ip6af);
567 af6 = ip6af->ip6af_down;
568 frag6_deq(ip6af, hash);
569 while (af6 != (struct ip6asfrag *)q6) {
570 m->m_pkthdr.csum_flags &=
571 IP6_REASS_MBUF(af6)->m_pkthdr.csum_flags;
572 m->m_pkthdr.csum_data +=
573 IP6_REASS_MBUF(af6)->m_pkthdr.csum_data;
575 af6dwn = af6->ip6af_down;
576 frag6_deq(af6, hash);
579 m_adj(IP6_REASS_MBUF(af6), af6->ip6af_offset);
580 m_demote_pkthdr(IP6_REASS_MBUF(af6));
581 m_cat(t, IP6_REASS_MBUF(af6));
586 while (m->m_pkthdr.csum_data & 0xffff0000)
587 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
588 (m->m_pkthdr.csum_data >> 16);
590 /* adjust offset to point where the original next header starts */
591 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
592 free(ip6af, M_FTABLE);
593 ip6 = mtod(m, struct ip6_hdr *);
594 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
595 if (q6->ip6q_ecn == IPTOS_ECN_CE)
596 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
599 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
602 if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
603 frag6_remque(q6, hash);
604 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
606 mac_ip6q_destroy(q6);
609 atomic_subtract_int(&V_frag6_nfragpackets, 1);
615 * Store NXT to the original.
617 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
620 frag6_remque(q6, hash);
621 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
623 mac_ip6q_reassemble(q6, m);
624 mac_ip6q_destroy(q6);
627 atomic_subtract_int(&V_frag6_nfragpackets, 1);
629 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
631 for (t = m; t; t = t->m_next)
633 m->m_pkthdr.len = plen;
637 mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
642 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
643 ip6dc->ip6dc_nxt = nxt;
644 ip6dc->ip6dc_off = offset;
646 m_tag_prepend(m, mtag);
650 IP6STAT_INC(ip6s_reassembled);
651 in6_ifstat_inc(dstifp, ifs6_reass_ok);
655 * Queue/dispatch for reprocessing.
657 netisr_dispatch(NETISR_IPV6_DIRECT, m);
662 * Tell launch routine the next header
672 in6_ifstat_inc(dstifp, ifs6_reass_fail);
673 IP6STAT_INC(ip6s_fragdropped);
679 * Free a fragment reassembly header and all
680 * associated datagrams.
683 frag6_freef(struct ip6q *q6, uint32_t bucket)
685 struct ip6asfrag *af6, *down6;
687 IP6Q_LOCK_ASSERT(bucket);
689 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
691 struct mbuf *m = IP6_REASS_MBUF(af6);
693 down6 = af6->ip6af_down;
694 frag6_deq(af6, bucket);
697 * Return ICMP time exceeded error for the 1st fragment.
698 * Just free other fragments.
700 if (af6->ip6af_off == 0) {
704 ip6 = mtod(m, struct ip6_hdr *);
706 /* restore source and destination addresses */
707 ip6->ip6_src = q6->ip6q_src;
708 ip6->ip6_dst = q6->ip6q_dst;
710 icmp6_error(m, ICMP6_TIME_EXCEEDED,
711 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
716 frag6_remque(q6, bucket);
717 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
719 mac_ip6q_destroy(q6);
722 atomic_subtract_int(&V_frag6_nfragpackets, 1);
726 * Put an ip fragment on a reassembly chain.
727 * Like insque, but pointers in middle of structure.
730 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
731 uint32_t bucket __unused)
734 IP6Q_LOCK_ASSERT(bucket);
737 af6->ip6af_down = up6->ip6af_down;
738 up6->ip6af_down->ip6af_up = af6;
739 up6->ip6af_down = af6;
743 * To frag6_enq as remque is to insque.
746 frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
749 IP6Q_LOCK_ASSERT(bucket);
751 af6->ip6af_up->ip6af_down = af6->ip6af_down;
752 af6->ip6af_down->ip6af_up = af6->ip6af_up;
756 frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket __unused)
759 IP6Q_LOCK_ASSERT(bucket);
760 KASSERT(IP6Q_HEAD(bucket) == old,
761 ("%s: attempt to insert at head of wrong bucket"
762 " (bucket=%u, old=%p)", __func__, bucket, old));
764 new->ip6q_prev = old;
765 new->ip6q_next = old->ip6q_next;
766 old->ip6q_next->ip6q_prev= new;
767 old->ip6q_next = new;
771 frag6_remque(struct ip6q *p6, uint32_t bucket __unused)
774 IP6Q_LOCK_ASSERT(bucket);
776 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
777 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
781 * IPv6 reassembling timer processing;
782 * if a timer expires on a reassembly
788 VNET_ITERATOR_DECL(vnet_iter);
789 struct ip6q *head, *q6;
792 VNET_LIST_RLOCK_NOSLEEP();
793 VNET_FOREACH(vnet_iter) {
794 CURVNET_SET(vnet_iter);
795 for (i = 0; i < IP6REASS_NHASH; i++) {
798 q6 = head->ip6q_next;
803 if (q6->ip6q_prev->ip6q_ttl == 0) {
804 IP6STAT_INC(ip6s_fragtimeout);
805 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
806 frag6_freef(q6->ip6q_prev, i);
810 * If we are over the maximum number of fragments
811 * (due to the limit being lowered), drain off
812 * enough to get down to the new limit.
814 while (atomic_load_int(&V_frag6_nfragpackets) >
815 (u_int)V_ip6_maxfragpackets &&
816 head->ip6q_prev != head) {
817 IP6STAT_INC(ip6s_fragoverflow);
818 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
819 frag6_freef(head->ip6q_prev, i);
825 VNET_LIST_RUNLOCK_NOSLEEP();
829 * Drain off all datagram fragments.
834 VNET_ITERATOR_DECL(vnet_iter);
838 VNET_LIST_RLOCK_NOSLEEP();
839 VNET_FOREACH(vnet_iter) {
840 CURVNET_SET(vnet_iter);
841 for (i = 0; i < IP6REASS_NHASH; i++) {
842 if (IP6Q_TRYLOCK(i) == 0)
845 while (head->ip6q_next != head) {
846 IP6STAT_INC(ip6s_fragdropped);
847 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
848 frag6_freef(head->ip6q_next, i);
854 VNET_LIST_RUNLOCK_NOSLEEP();
858 ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
860 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
863 /* Delete frag6 header. */
864 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
865 /* This is the only possible case with !PULLDOWN_TEST. */
866 bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
868 m->m_data += sizeof(struct ip6_frag);
869 m->m_len -= sizeof(struct ip6_frag);
871 /* This comes with no copy if the boundary is on cluster. */
872 if ((t = m_split(m, offset, wait)) == NULL)
874 m_adj(t, sizeof(struct ip6_frag));
878 m->m_flags |= M_FRAGMENTED;