2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/domain.h>
43 #include <sys/eventhandler.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
48 #include <sys/kernel.h>
49 #include <sys/syslog.h>
51 #include <machine/atomic.h>
54 #include <net/if_var.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 */
67 #include <security/mac/mac_framework.h>
70 * Reassembly headers are stored in hash buckets.
72 #define IP6REASS_NHASH_LOG2 10
73 #define IP6REASS_NHASH (1 << IP6REASS_NHASH_LOG2)
74 #define IP6REASS_HMASK (IP6REASS_NHASH - 1)
76 static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *,
77 uint32_t bucket __unused);
78 static void frag6_deq(struct ip6asfrag *, uint32_t bucket __unused);
79 static void frag6_insque_head(struct ip6q *, struct ip6q *,
81 static void frag6_remque(struct ip6q *, uint32_t bucket);
82 static void frag6_freef(struct ip6q *, uint32_t bucket);
90 static VNET_DEFINE(volatile u_int, frag6_nfragpackets);
91 volatile u_int frag6_nfrags = 0;
92 static VNET_DEFINE(struct ip6qbucket, ip6q[IP6REASS_NHASH]);
93 static VNET_DEFINE(uint32_t, ip6q_hashseed);
95 #define V_frag6_nfragpackets VNET(frag6_nfragpackets)
96 #define V_ip6q VNET(ip6q)
97 #define V_ip6q_hashseed VNET(ip6q_hashseed)
99 #define IP6Q_LOCK(i) mtx_lock(&V_ip6q[(i)].lock)
100 #define IP6Q_TRYLOCK(i) mtx_trylock(&V_ip6q[(i)].lock)
101 #define IP6Q_LOCK_ASSERT(i) mtx_assert(&V_ip6q[(i)].lock, MA_OWNED)
102 #define IP6Q_UNLOCK(i) mtx_unlock(&V_ip6q[(i)].lock)
103 #define IP6Q_HEAD(i) (&V_ip6q[(i)].ip6q)
105 static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
108 * By default, limit the number of IP6 fragments across all reassembly
109 * queues to 1/32 of the total number of mbuf clusters.
111 * Limit the total number of reassembly queues per VNET to the
112 * IP6 fragment limit, but ensure the limit will not allow any bucket
113 * to grow above 100 items. (The bucket limit is
114 * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct
115 * multiplier to reach a 100-item limit.)
116 * The 100-item limit was chosen as brief testing seems to show that
117 * this produces "reasonable" performance on some subset of systems
120 #define IP6_MAXFRAGS (nmbclusters / 32)
121 #define IP6_MAXFRAGPACKETS (imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
124 * Initialise reassembly queue and fragment identifier.
127 frag6_set_bucketsize()
131 if ((i = V_ip6_maxfragpackets) > 0)
132 V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
136 frag6_change(void *tag)
138 VNET_ITERATOR_DECL(vnet_iter);
140 ip6_maxfrags = IP6_MAXFRAGS;
141 VNET_LIST_RLOCK_NOSLEEP();
142 VNET_FOREACH(vnet_iter) {
143 CURVNET_SET(vnet_iter);
144 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
145 frag6_set_bucketsize();
148 VNET_LIST_RUNLOCK_NOSLEEP();
157 V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
158 frag6_set_bucketsize();
159 for (i = 0; i < IP6REASS_NHASH; i++) {
161 q6->ip6q_next = q6->ip6q_prev = q6;
162 mtx_init(&V_ip6q[i].lock, "ip6qlock", NULL, MTX_DEF);
165 V_ip6q_hashseed = arc4random();
166 V_ip6_maxfragsperpacket = 64;
167 if (!IS_DEFAULT_VNET(curvnet))
170 ip6_maxfrags = IP6_MAXFRAGS;
171 EVENTHANDLER_REGISTER(nmbclusters_change,
172 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
176 * In RFC2460, fragment and reassembly rule do not agree with each other,
177 * in terms of next header field handling in fragment header.
178 * While the sender will use the same value for all of the fragmented packets,
179 * receiver is suggested not to check the consistency.
181 * fragment rule (p20):
182 * (2) A Fragment header containing:
183 * The Next Header value that identifies the first header of
184 * the Fragmentable Part of the original packet.
185 * -> next header field is same for all fragments
187 * reassembly rule (p21):
188 * The Next Header field of the last header of the Unfragmentable
189 * Part is obtained from the Next Header field of the first
190 * fragment's Fragment header.
191 * -> should grab it from the first fragment only
193 * The following note also contradicts with fragment rule - no one is going to
194 * send different fragment with different next header field.
196 * additional note (p22):
197 * The Next Header values in the Fragment headers of different
198 * fragments of the same original packet may differ. Only the value
199 * from the Offset zero fragment packet is used for reassembly.
200 * -> should grab it from the first fragment only
202 * There is no explicit reason given in the RFC. Historical reason maybe?
208 frag6_input(struct mbuf **mp, int *offp, int proto)
210 struct mbuf *m = *mp, *t;
212 struct ip6_frag *ip6f;
213 struct ip6q *head, *q6;
214 struct ip6asfrag *af6, *ip6af, *af6dwn;
215 struct in6_ifaddr *ia;
216 int offset = *offp, nxt, i, next;
218 int fragoff, frgpartlen; /* must be larger than u_int16_t */
219 uint32_t hash, hashkey[sizeof(struct in6_addr) * 2 + 1], *hashkeyp;
220 struct ifnet *dstifp;
224 struct ip6_direct_ctx *ip6dc;
228 char ip6buf[INET6_ADDRSTRLEN];
231 ip6 = mtod(m, struct ip6_hdr *);
232 #ifndef PULLDOWN_TEST
233 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
234 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
236 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
238 return (IPPROTO_DONE);
242 /* find the destination interface of the packet. */
243 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
246 ifa_free(&ia->ia_ifa);
248 /* jumbo payload can't contain a fragment header */
249 if (ip6->ip6_plen == 0) {
250 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
251 in6_ifstat_inc(dstifp, ifs6_reass_fail);
256 * check whether fragment packet's fragment length is
257 * multiple of 8 octets.
258 * sizeof(struct ip6_frag) == 8
259 * sizeof(struct ip6_hdr) = 40
261 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
262 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
263 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
264 offsetof(struct ip6_hdr, ip6_plen));
265 in6_ifstat_inc(dstifp, ifs6_reass_fail);
269 IP6STAT_INC(ip6s_fragments);
270 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
272 /* offset now points to data portion */
273 offset += sizeof(struct ip6_frag);
276 * RFC 6946: Handle "atomic" fragments (offset and m bit set to 0)
277 * upfront, unrelated to any reassembly. Just skip the fragment header.
279 if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
280 /* XXX-BZ we want dedicated counters for this. */
281 IP6STAT_INC(ip6s_reassembled);
282 in6_ifstat_inc(dstifp, ifs6_reass_ok);
284 m->m_flags |= M_FRAGMENTED;
285 return (ip6f->ip6f_nxt);
288 /* Get fragment length and discard 0-byte fragments. */
289 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
290 if (frgpartlen == 0) {
291 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
292 offsetof(struct ip6_hdr, ip6_plen));
293 in6_ifstat_inc(dstifp, ifs6_reass_fail);
294 IP6STAT_INC(ip6s_fragdropped);
299 memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
300 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
301 memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
302 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
303 *hashkeyp = ip6f->ip6f_ident;
304 hash = jenkins_hash32(hashkey, nitems(hashkey), V_ip6q_hashseed);
305 hash &= IP6REASS_HMASK;
306 head = IP6Q_HEAD(hash);
310 * Enforce upper bound on number of fragments.
311 * If maxfrag is 0, never accept fragments.
312 * If maxfrag is -1, accept all fragments without limitation.
314 if (ip6_maxfrags < 0)
316 else if (frag6_nfrags >= (u_int)ip6_maxfrags)
319 for (q6 = head->ip6q_next; q6 != head; q6 = q6->ip6q_next)
320 if (ip6f->ip6f_ident == q6->ip6q_ident &&
321 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
322 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
324 && mac_ip6q_match(m, q6)
331 * the first fragment to arrive, create a reassembly queue.
336 * Enforce upper bound on number of fragmented packets
337 * for which we attempt reassembly;
338 * If maxfragpackets is 0, never accept fragments.
339 * If maxfragpackets is -1, accept all fragments without
342 if (V_ip6_maxfragpackets < 0)
344 else if (V_ip6q[hash].count >= V_ip6_maxfragbucketsize ||
345 V_frag6_nfragpackets >= (u_int)V_ip6_maxfragpackets)
347 atomic_add_int(&V_frag6_nfragpackets, 1);
348 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
352 bzero(q6, sizeof(*q6));
354 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
358 mac_ip6q_create(m, q6);
360 frag6_insque_head(q6, head, hash);
362 /* ip6q_nxt will be filled afterwards, from 1st fragment */
363 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
365 q6->ip6q_nxtp = (u_char *)nxtp;
367 q6->ip6q_ident = ip6f->ip6f_ident;
368 q6->ip6q_ttl = IPV6_FRAGTTL;
369 q6->ip6q_src = ip6->ip6_src;
370 q6->ip6q_dst = ip6->ip6_dst;
372 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
373 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
379 * If it's the 1st fragment, record the length of the
380 * unfragmentable part and the next header of the fragment header.
382 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
384 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
385 sizeof(struct ip6_frag);
386 q6->ip6q_nxt = ip6f->ip6f_nxt;
390 * Check that the reassembled packet would not exceed 65535 bytes
392 * If it would exceed, discard the fragment and return an ICMP error.
394 if (q6->ip6q_unfrglen >= 0) {
395 /* The 1st fragment has already arrived. */
396 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
397 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
398 offset - sizeof(struct ip6_frag) +
399 offsetof(struct ip6_frag, ip6f_offlg));
401 return (IPPROTO_DONE);
403 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
404 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
405 offset - sizeof(struct ip6_frag) +
406 offsetof(struct ip6_frag, ip6f_offlg));
408 return (IPPROTO_DONE);
411 * If it's the first fragment, do the above check for each
412 * fragment already stored in the reassembly queue.
415 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
417 af6dwn = af6->ip6af_down;
419 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
421 struct mbuf *merr = IP6_REASS_MBUF(af6);
422 struct ip6_hdr *ip6err;
423 int erroff = af6->ip6af_offset;
425 /* dequeue the fragment. */
426 frag6_deq(af6, hash);
429 /* adjust pointer. */
430 ip6err = mtod(merr, struct ip6_hdr *);
433 * Restore source and destination addresses
434 * in the erroneous IPv6 header.
436 ip6err->ip6_src = q6->ip6q_src;
437 ip6err->ip6_dst = q6->ip6q_dst;
439 icmp6_error(merr, ICMP6_PARAM_PROB,
440 ICMP6_PARAMPROB_HEADER,
441 erroff - sizeof(struct ip6_frag) +
442 offsetof(struct ip6_frag, ip6f_offlg));
447 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
451 bzero(ip6af, sizeof(*ip6af));
452 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
453 ip6af->ip6af_off = fragoff;
454 ip6af->ip6af_frglen = frgpartlen;
455 ip6af->ip6af_offset = offset;
456 IP6_REASS_MBUF(ip6af) = m;
459 af6 = (struct ip6asfrag *)q6;
464 * Handle ECN by comparing this segment with the first one;
465 * if CE is set, do not lose CE.
466 * drop if CE and not-ECT are mixed for the same packet.
468 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
470 if (ecn == IPTOS_ECN_CE) {
471 if (ecn0 == IPTOS_ECN_NOTECT) {
472 free(ip6af, M_FTABLE);
475 if (ecn0 != IPTOS_ECN_CE)
476 q6->ip6q_ecn = IPTOS_ECN_CE;
478 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
479 free(ip6af, M_FTABLE);
484 * Find a segment which begins after this one does.
486 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
487 af6 = af6->ip6af_down)
488 if (af6->ip6af_off > ip6af->ip6af_off)
493 * If there is a preceding segment, it may provide some of
494 * our data already. If so, drop the data from the incoming
495 * segment. If it provides all of our data, drop us.
497 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
498 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
501 if (i >= ip6af->ip6af_frglen)
503 m_adj(IP6_REASS_MBUF(ip6af), i);
504 ip6af->ip6af_off += i;
505 ip6af->ip6af_frglen -= i;
510 * While we overlap succeeding segments trim them or,
511 * if they are completely covered, dequeue them.
513 while (af6 != (struct ip6asfrag *)q6 &&
514 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
515 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
516 if (i < af6->ip6af_frglen) {
517 af6->ip6af_frglen -= i;
519 m_adj(IP6_REASS_MBUF(af6), i);
522 af6 = af6->ip6af_down;
523 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
524 frag6_deq(af6->ip6af_up, hash);
528 * If the incoming framgent overlaps some existing fragments in
529 * the reassembly queue, drop it, since it is dangerous to override
530 * existing fragments from a security point of view.
531 * We don't know which fragment is the bad guy - here we trust
532 * fragment that came in earlier, with no real reason.
534 * Note: due to changes after disabling this part, mbuf passed to
535 * m_adj() below now does not meet the requirement.
537 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
538 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
541 #if 0 /* suppress the noisy log */
542 log(LOG_ERR, "%d bytes of a fragment from %s "
543 "overlaps the previous fragment\n",
544 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
546 free(ip6af, M_FTABLE);
550 if (af6 != (struct ip6asfrag *)q6) {
551 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
553 #if 0 /* suppress the noisy log */
554 log(LOG_ERR, "%d bytes of a fragment from %s "
555 "overlaps the succeeding fragment",
556 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
558 free(ip6af, M_FTABLE);
567 mac_ip6q_update(m, q6);
571 * Stick new segment in its place;
572 * check for complete reassembly.
573 * If not complete, check fragment limit.
574 * Move to front of packet queue, as we are
575 * the most recently active fragmented packet.
577 frag6_enq(ip6af, af6->ip6af_up, hash);
578 atomic_add_int(&frag6_nfrags, 1);
581 if (q6 != head->ip6q_next) {
582 frag6_remque(q6, hash);
583 frag6_insque_head(q6, head, hash);
587 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
588 af6 = af6->ip6af_down) {
589 if (af6->ip6af_off != next) {
590 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
591 IP6STAT_INC(ip6s_fragdropped);
592 frag6_freef(q6, hash);
597 next += af6->ip6af_frglen;
599 if (af6->ip6af_up->ip6af_mff) {
600 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
601 IP6STAT_INC(ip6s_fragdropped);
602 frag6_freef(q6, hash);
609 * Reassembly is complete; concatenate fragments.
611 ip6af = q6->ip6q_down;
612 t = m = IP6_REASS_MBUF(ip6af);
613 af6 = ip6af->ip6af_down;
614 frag6_deq(ip6af, hash);
615 while (af6 != (struct ip6asfrag *)q6) {
616 m->m_pkthdr.csum_flags &=
617 IP6_REASS_MBUF(af6)->m_pkthdr.csum_flags;
618 m->m_pkthdr.csum_data +=
619 IP6_REASS_MBUF(af6)->m_pkthdr.csum_data;
621 af6dwn = af6->ip6af_down;
622 frag6_deq(af6, hash);
625 m_adj(IP6_REASS_MBUF(af6), af6->ip6af_offset);
626 m_demote_pkthdr(IP6_REASS_MBUF(af6));
627 m_cat(t, IP6_REASS_MBUF(af6));
632 while (m->m_pkthdr.csum_data & 0xffff0000)
633 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
634 (m->m_pkthdr.csum_data >> 16);
636 /* adjust offset to point where the original next header starts */
637 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
638 free(ip6af, M_FTABLE);
639 ip6 = mtod(m, struct ip6_hdr *);
640 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
641 if (q6->ip6q_ecn == IPTOS_ECN_CE)
642 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
645 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
648 if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
649 frag6_remque(q6, hash);
650 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
652 mac_ip6q_destroy(q6);
655 atomic_subtract_int(&V_frag6_nfragpackets, 1);
661 * Store NXT to the original.
663 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
666 frag6_remque(q6, hash);
667 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
669 mac_ip6q_reassemble(q6, m);
670 mac_ip6q_destroy(q6);
673 atomic_subtract_int(&V_frag6_nfragpackets, 1);
675 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
677 for (t = m; t; t = t->m_next)
679 m->m_pkthdr.len = plen;
683 mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
688 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
689 ip6dc->ip6dc_nxt = nxt;
690 ip6dc->ip6dc_off = offset;
692 m_tag_prepend(m, mtag);
696 IP6STAT_INC(ip6s_reassembled);
697 in6_ifstat_inc(dstifp, ifs6_reass_ok);
701 * Queue/dispatch for reprocessing.
703 netisr_dispatch(NETISR_IPV6_DIRECT, m);
708 * Tell launch routine the next header
718 in6_ifstat_inc(dstifp, ifs6_reass_fail);
719 IP6STAT_INC(ip6s_fragdropped);
725 * Free a fragment reassembly header and all
726 * associated datagrams.
729 frag6_freef(struct ip6q *q6, uint32_t bucket)
731 struct ip6asfrag *af6, *down6;
733 IP6Q_LOCK_ASSERT(bucket);
735 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
737 struct mbuf *m = IP6_REASS_MBUF(af6);
739 down6 = af6->ip6af_down;
740 frag6_deq(af6, bucket);
743 * Return ICMP time exceeded error for the 1st fragment.
744 * Just free other fragments.
746 if (af6->ip6af_off == 0) {
750 ip6 = mtod(m, struct ip6_hdr *);
752 /* restore source and destination addresses */
753 ip6->ip6_src = q6->ip6q_src;
754 ip6->ip6_dst = q6->ip6q_dst;
756 icmp6_error(m, ICMP6_TIME_EXCEEDED,
757 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
762 frag6_remque(q6, bucket);
763 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
765 mac_ip6q_destroy(q6);
768 atomic_subtract_int(&V_frag6_nfragpackets, 1);
772 * Put an ip fragment on a reassembly chain.
773 * Like insque, but pointers in middle of structure.
776 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
777 uint32_t bucket __unused)
780 IP6Q_LOCK_ASSERT(bucket);
783 af6->ip6af_down = up6->ip6af_down;
784 up6->ip6af_down->ip6af_up = af6;
785 up6->ip6af_down = af6;
789 * To frag6_enq as remque is to insque.
792 frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
795 IP6Q_LOCK_ASSERT(bucket);
797 af6->ip6af_up->ip6af_down = af6->ip6af_down;
798 af6->ip6af_down->ip6af_up = af6->ip6af_up;
802 frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket)
805 IP6Q_LOCK_ASSERT(bucket);
806 KASSERT(IP6Q_HEAD(bucket) == old,
807 ("%s: attempt to insert at head of wrong bucket"
808 " (bucket=%u, old=%p)", __func__, bucket, old));
810 new->ip6q_prev = old;
811 new->ip6q_next = old->ip6q_next;
812 old->ip6q_next->ip6q_prev= new;
813 old->ip6q_next = new;
814 V_ip6q[bucket].count++;
818 frag6_remque(struct ip6q *p6, uint32_t bucket)
821 IP6Q_LOCK_ASSERT(bucket);
823 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
824 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
825 V_ip6q[bucket].count--;
829 * IPv6 reassembling timer processing;
830 * if a timer expires on a reassembly
836 VNET_ITERATOR_DECL(vnet_iter);
837 struct ip6q *head, *q6;
840 VNET_LIST_RLOCK_NOSLEEP();
841 VNET_FOREACH(vnet_iter) {
842 CURVNET_SET(vnet_iter);
843 for (i = 0; i < IP6REASS_NHASH; i++) {
846 q6 = head->ip6q_next;
849 * XXXJTL: This should never happen. This
850 * should turn into an assertion.
858 if (q6->ip6q_prev->ip6q_ttl == 0) {
859 IP6STAT_INC(ip6s_fragtimeout);
860 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
861 frag6_freef(q6->ip6q_prev, i);
865 * If we are over the maximum number of fragments
866 * (due to the limit being lowered), drain off
867 * enough to get down to the new limit.
868 * Note that we drain all reassembly queues if
869 * maxfragpackets is 0 (fragmentation is disabled),
870 * and don't enforce a limit when maxfragpackets
873 while ((V_ip6_maxfragpackets == 0 ||
874 (V_ip6_maxfragpackets > 0 &&
875 V_ip6q[i].count > V_ip6_maxfragbucketsize)) &&
876 head->ip6q_prev != head) {
877 IP6STAT_INC(ip6s_fragoverflow);
878 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
879 frag6_freef(head->ip6q_prev, i);
884 * If we are still over the maximum number of fragmented
885 * packets, drain off enough to get down to the new limit.
888 while (V_ip6_maxfragpackets >= 0 &&
889 V_frag6_nfragpackets > (u_int)V_ip6_maxfragpackets) {
892 if (head->ip6q_prev != head) {
893 IP6STAT_INC(ip6s_fragoverflow);
894 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
895 frag6_freef(head->ip6q_prev, i);
898 i = (i + 1) % IP6REASS_NHASH;
902 VNET_LIST_RUNLOCK_NOSLEEP();
906 * Drain off all datagram fragments.
911 VNET_ITERATOR_DECL(vnet_iter);
915 VNET_LIST_RLOCK_NOSLEEP();
916 VNET_FOREACH(vnet_iter) {
917 CURVNET_SET(vnet_iter);
918 for (i = 0; i < IP6REASS_NHASH; i++) {
919 if (IP6Q_TRYLOCK(i) == 0)
922 while (head->ip6q_next != head) {
923 IP6STAT_INC(ip6s_fragdropped);
924 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
925 frag6_freef(head->ip6q_next, i);
931 VNET_LIST_RUNLOCK_NOSLEEP();
935 ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
937 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
940 /* Delete frag6 header. */
941 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
942 /* This is the only possible case with !PULLDOWN_TEST. */
943 bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
945 m->m_data += sizeof(struct ip6_frag);
946 m->m_len -= sizeof(struct ip6_frag);
948 /* This comes with no copy if the boundary is on cluster. */
949 if ((t = m_split(m, offset, wait)) == NULL)
951 m_adj(t, sizeof(struct ip6_frag));
955 m->m_flags |= M_FRAGMENTED;