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 hashkey[(sizeof(struct in6_addr) * 2 +
220 sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)];
221 uint32_t hash, *hashkeyp;
222 struct ifnet *dstifp;
226 struct ip6_direct_ctx *ip6dc;
230 char ip6buf[INET6_ADDRSTRLEN];
233 ip6 = mtod(m, struct ip6_hdr *);
234 #ifndef PULLDOWN_TEST
235 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
236 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
238 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
240 return (IPPROTO_DONE);
244 /* find the destination interface of the packet. */
245 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
248 ifa_free(&ia->ia_ifa);
250 /* jumbo payload can't contain a fragment header */
251 if (ip6->ip6_plen == 0) {
252 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
253 in6_ifstat_inc(dstifp, ifs6_reass_fail);
258 * check whether fragment packet's fragment length is
259 * multiple of 8 octets.
260 * sizeof(struct ip6_frag) == 8
261 * sizeof(struct ip6_hdr) = 40
263 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
264 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
265 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
266 offsetof(struct ip6_hdr, ip6_plen));
267 in6_ifstat_inc(dstifp, ifs6_reass_fail);
271 IP6STAT_INC(ip6s_fragments);
272 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
274 /* offset now points to data portion */
275 offset += sizeof(struct ip6_frag);
278 * RFC 6946: Handle "atomic" fragments (offset and m bit set to 0)
279 * upfront, unrelated to any reassembly. Just skip the fragment header.
281 if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
282 /* XXX-BZ we want dedicated counters for this. */
283 IP6STAT_INC(ip6s_reassembled);
284 in6_ifstat_inc(dstifp, ifs6_reass_ok);
286 m->m_flags |= M_FRAGMENTED;
287 return (ip6f->ip6f_nxt);
290 /* Get fragment length and discard 0-byte fragments. */
291 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
292 if (frgpartlen == 0) {
293 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
294 offsetof(struct ip6_hdr, ip6_plen));
295 in6_ifstat_inc(dstifp, ifs6_reass_fail);
296 IP6STAT_INC(ip6s_fragdropped);
301 memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
302 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
303 memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
304 hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
305 *hashkeyp = ip6f->ip6f_ident;
306 hash = jenkins_hash32(hashkey, nitems(hashkey), V_ip6q_hashseed);
307 hash &= IP6REASS_HMASK;
308 head = IP6Q_HEAD(hash);
312 * Enforce upper bound on number of fragments.
313 * If maxfrag is 0, never accept fragments.
314 * If maxfrag is -1, accept all fragments without limitation.
316 if (ip6_maxfrags < 0)
318 else if (frag6_nfrags >= (u_int)ip6_maxfrags)
321 for (q6 = head->ip6q_next; q6 != head; q6 = q6->ip6q_next)
322 if (ip6f->ip6f_ident == q6->ip6q_ident &&
323 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
324 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
326 && mac_ip6q_match(m, q6)
333 * the first fragment to arrive, create a reassembly queue.
338 * Enforce upper bound on number of fragmented packets
339 * for which we attempt reassembly;
340 * If maxfragpackets is 0, never accept fragments.
341 * If maxfragpackets is -1, accept all fragments without
344 if (V_ip6_maxfragpackets < 0)
346 else if (V_ip6q[hash].count >= V_ip6_maxfragbucketsize ||
347 V_frag6_nfragpackets >= (u_int)V_ip6_maxfragpackets)
349 atomic_add_int(&V_frag6_nfragpackets, 1);
350 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
354 bzero(q6, sizeof(*q6));
356 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
360 mac_ip6q_create(m, q6);
362 frag6_insque_head(q6, head, hash);
364 /* ip6q_nxt will be filled afterwards, from 1st fragment */
365 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
367 q6->ip6q_nxtp = (u_char *)nxtp;
369 q6->ip6q_ident = ip6f->ip6f_ident;
370 q6->ip6q_ttl = IPV6_FRAGTTL;
371 q6->ip6q_src = ip6->ip6_src;
372 q6->ip6q_dst = ip6->ip6_dst;
374 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
375 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
381 * If it's the 1st fragment, record the length of the
382 * unfragmentable part and the next header of the fragment header.
384 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
386 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
387 sizeof(struct ip6_frag);
388 q6->ip6q_nxt = ip6f->ip6f_nxt;
392 * Check that the reassembled packet would not exceed 65535 bytes
394 * If it would exceed, discard the fragment and return an ICMP error.
396 if (q6->ip6q_unfrglen >= 0) {
397 /* The 1st fragment has already arrived. */
398 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
399 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
400 offset - sizeof(struct ip6_frag) +
401 offsetof(struct ip6_frag, ip6f_offlg));
403 return (IPPROTO_DONE);
405 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
406 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
407 offset - sizeof(struct ip6_frag) +
408 offsetof(struct ip6_frag, ip6f_offlg));
410 return (IPPROTO_DONE);
413 * If it's the first fragment, do the above check for each
414 * fragment already stored in the reassembly queue.
417 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
419 af6dwn = af6->ip6af_down;
421 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
423 struct mbuf *merr = IP6_REASS_MBUF(af6);
424 struct ip6_hdr *ip6err;
425 int erroff = af6->ip6af_offset;
427 /* dequeue the fragment. */
428 frag6_deq(af6, hash);
431 /* adjust pointer. */
432 ip6err = mtod(merr, struct ip6_hdr *);
435 * Restore source and destination addresses
436 * in the erroneous IPv6 header.
438 ip6err->ip6_src = q6->ip6q_src;
439 ip6err->ip6_dst = q6->ip6q_dst;
441 icmp6_error(merr, ICMP6_PARAM_PROB,
442 ICMP6_PARAMPROB_HEADER,
443 erroff - sizeof(struct ip6_frag) +
444 offsetof(struct ip6_frag, ip6f_offlg));
449 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
453 bzero(ip6af, sizeof(*ip6af));
454 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
455 ip6af->ip6af_off = fragoff;
456 ip6af->ip6af_frglen = frgpartlen;
457 ip6af->ip6af_offset = offset;
458 IP6_REASS_MBUF(ip6af) = m;
461 af6 = (struct ip6asfrag *)q6;
466 * Handle ECN by comparing this segment with the first one;
467 * if CE is set, do not lose CE.
468 * drop if CE and not-ECT are mixed for the same packet.
470 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
472 if (ecn == IPTOS_ECN_CE) {
473 if (ecn0 == IPTOS_ECN_NOTECT) {
474 free(ip6af, M_FTABLE);
477 if (ecn0 != IPTOS_ECN_CE)
478 q6->ip6q_ecn = IPTOS_ECN_CE;
480 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
481 free(ip6af, M_FTABLE);
486 * Find a segment which begins after this one does.
488 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
489 af6 = af6->ip6af_down)
490 if (af6->ip6af_off > ip6af->ip6af_off)
495 * If there is a preceding segment, it may provide some of
496 * our data already. If so, drop the data from the incoming
497 * segment. If it provides all of our data, drop us.
499 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
500 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
503 if (i >= ip6af->ip6af_frglen)
505 m_adj(IP6_REASS_MBUF(ip6af), i);
506 ip6af->ip6af_off += i;
507 ip6af->ip6af_frglen -= i;
512 * While we overlap succeeding segments trim them or,
513 * if they are completely covered, dequeue them.
515 while (af6 != (struct ip6asfrag *)q6 &&
516 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
517 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
518 if (i < af6->ip6af_frglen) {
519 af6->ip6af_frglen -= i;
521 m_adj(IP6_REASS_MBUF(af6), i);
524 af6 = af6->ip6af_down;
525 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
526 frag6_deq(af6->ip6af_up, hash);
530 * If the incoming framgent overlaps some existing fragments in
531 * the reassembly queue, drop it, since it is dangerous to override
532 * existing fragments from a security point of view.
533 * We don't know which fragment is the bad guy - here we trust
534 * fragment that came in earlier, with no real reason.
536 * Note: due to changes after disabling this part, mbuf passed to
537 * m_adj() below now does not meet the requirement.
539 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
540 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
543 #if 0 /* suppress the noisy log */
544 log(LOG_ERR, "%d bytes of a fragment from %s "
545 "overlaps the previous fragment\n",
546 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
548 free(ip6af, M_FTABLE);
552 if (af6 != (struct ip6asfrag *)q6) {
553 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
555 #if 0 /* suppress the noisy log */
556 log(LOG_ERR, "%d bytes of a fragment from %s "
557 "overlaps the succeeding fragment",
558 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
560 free(ip6af, M_FTABLE);
569 mac_ip6q_update(m, q6);
573 * Stick new segment in its place;
574 * check for complete reassembly.
575 * If not complete, check fragment limit.
576 * Move to front of packet queue, as we are
577 * the most recently active fragmented packet.
579 frag6_enq(ip6af, af6->ip6af_up, hash);
580 atomic_add_int(&frag6_nfrags, 1);
583 if (q6 != head->ip6q_next) {
584 frag6_remque(q6, hash);
585 frag6_insque_head(q6, head, hash);
589 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
590 af6 = af6->ip6af_down) {
591 if (af6->ip6af_off != next) {
592 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
593 IP6STAT_INC(ip6s_fragdropped);
594 frag6_freef(q6, hash);
599 next += af6->ip6af_frglen;
601 if (af6->ip6af_up->ip6af_mff) {
602 if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
603 IP6STAT_INC(ip6s_fragdropped);
604 frag6_freef(q6, hash);
611 * Reassembly is complete; concatenate fragments.
613 ip6af = q6->ip6q_down;
614 t = m = IP6_REASS_MBUF(ip6af);
615 af6 = ip6af->ip6af_down;
616 frag6_deq(ip6af, hash);
617 while (af6 != (struct ip6asfrag *)q6) {
618 m->m_pkthdr.csum_flags &=
619 IP6_REASS_MBUF(af6)->m_pkthdr.csum_flags;
620 m->m_pkthdr.csum_data +=
621 IP6_REASS_MBUF(af6)->m_pkthdr.csum_data;
623 af6dwn = af6->ip6af_down;
624 frag6_deq(af6, hash);
627 m_adj(IP6_REASS_MBUF(af6), af6->ip6af_offset);
628 m_demote_pkthdr(IP6_REASS_MBUF(af6));
629 m_cat(t, IP6_REASS_MBUF(af6));
634 while (m->m_pkthdr.csum_data & 0xffff0000)
635 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
636 (m->m_pkthdr.csum_data >> 16);
638 /* adjust offset to point where the original next header starts */
639 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
640 free(ip6af, M_FTABLE);
641 ip6 = mtod(m, struct ip6_hdr *);
642 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
643 if (q6->ip6q_ecn == IPTOS_ECN_CE)
644 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
647 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
650 if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
651 frag6_remque(q6, hash);
652 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
654 mac_ip6q_destroy(q6);
657 atomic_subtract_int(&V_frag6_nfragpackets, 1);
663 * Store NXT to the original.
665 m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
668 frag6_remque(q6, hash);
669 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
671 mac_ip6q_reassemble(q6, m);
672 mac_ip6q_destroy(q6);
675 atomic_subtract_int(&V_frag6_nfragpackets, 1);
677 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
679 for (t = m; t; t = t->m_next)
681 m->m_pkthdr.len = plen;
685 mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
690 ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
691 ip6dc->ip6dc_nxt = nxt;
692 ip6dc->ip6dc_off = offset;
694 m_tag_prepend(m, mtag);
698 IP6STAT_INC(ip6s_reassembled);
699 in6_ifstat_inc(dstifp, ifs6_reass_ok);
703 * Queue/dispatch for reprocessing.
705 netisr_dispatch(NETISR_IPV6_DIRECT, m);
710 * Tell launch routine the next header
720 in6_ifstat_inc(dstifp, ifs6_reass_fail);
721 IP6STAT_INC(ip6s_fragdropped);
727 * Free a fragment reassembly header and all
728 * associated datagrams.
731 frag6_freef(struct ip6q *q6, uint32_t bucket)
733 struct ip6asfrag *af6, *down6;
735 IP6Q_LOCK_ASSERT(bucket);
737 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
739 struct mbuf *m = IP6_REASS_MBUF(af6);
741 down6 = af6->ip6af_down;
742 frag6_deq(af6, bucket);
745 * Return ICMP time exceeded error for the 1st fragment.
746 * Just free other fragments.
748 if (af6->ip6af_off == 0) {
752 ip6 = mtod(m, struct ip6_hdr *);
754 /* restore source and destination addresses */
755 ip6->ip6_src = q6->ip6q_src;
756 ip6->ip6_dst = q6->ip6q_dst;
758 icmp6_error(m, ICMP6_TIME_EXCEEDED,
759 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
764 frag6_remque(q6, bucket);
765 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
767 mac_ip6q_destroy(q6);
770 atomic_subtract_int(&V_frag6_nfragpackets, 1);
774 * Put an ip fragment on a reassembly chain.
775 * Like insque, but pointers in middle of structure.
778 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
779 uint32_t bucket __unused)
782 IP6Q_LOCK_ASSERT(bucket);
785 af6->ip6af_down = up6->ip6af_down;
786 up6->ip6af_down->ip6af_up = af6;
787 up6->ip6af_down = af6;
791 * To frag6_enq as remque is to insque.
794 frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
797 IP6Q_LOCK_ASSERT(bucket);
799 af6->ip6af_up->ip6af_down = af6->ip6af_down;
800 af6->ip6af_down->ip6af_up = af6->ip6af_up;
804 frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket)
807 IP6Q_LOCK_ASSERT(bucket);
808 KASSERT(IP6Q_HEAD(bucket) == old,
809 ("%s: attempt to insert at head of wrong bucket"
810 " (bucket=%u, old=%p)", __func__, bucket, old));
812 new->ip6q_prev = old;
813 new->ip6q_next = old->ip6q_next;
814 old->ip6q_next->ip6q_prev= new;
815 old->ip6q_next = new;
816 V_ip6q[bucket].count++;
820 frag6_remque(struct ip6q *p6, uint32_t bucket)
823 IP6Q_LOCK_ASSERT(bucket);
825 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
826 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
827 V_ip6q[bucket].count--;
831 * IPv6 reassembling timer processing;
832 * if a timer expires on a reassembly
838 VNET_ITERATOR_DECL(vnet_iter);
839 struct ip6q *head, *q6;
842 VNET_LIST_RLOCK_NOSLEEP();
843 VNET_FOREACH(vnet_iter) {
844 CURVNET_SET(vnet_iter);
845 for (i = 0; i < IP6REASS_NHASH; i++) {
848 q6 = head->ip6q_next;
851 * XXXJTL: This should never happen. This
852 * should turn into an assertion.
860 if (q6->ip6q_prev->ip6q_ttl == 0) {
861 IP6STAT_INC(ip6s_fragtimeout);
862 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
863 frag6_freef(q6->ip6q_prev, i);
867 * If we are over the maximum number of fragments
868 * (due to the limit being lowered), drain off
869 * enough to get down to the new limit.
870 * Note that we drain all reassembly queues if
871 * maxfragpackets is 0 (fragmentation is disabled),
872 * and don't enforce a limit when maxfragpackets
875 while ((V_ip6_maxfragpackets == 0 ||
876 (V_ip6_maxfragpackets > 0 &&
877 V_ip6q[i].count > V_ip6_maxfragbucketsize)) &&
878 head->ip6q_prev != head) {
879 IP6STAT_INC(ip6s_fragoverflow);
880 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
881 frag6_freef(head->ip6q_prev, i);
886 * If we are still over the maximum number of fragmented
887 * packets, drain off enough to get down to the new limit.
890 while (V_ip6_maxfragpackets >= 0 &&
891 V_frag6_nfragpackets > (u_int)V_ip6_maxfragpackets) {
894 if (head->ip6q_prev != head) {
895 IP6STAT_INC(ip6s_fragoverflow);
896 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
897 frag6_freef(head->ip6q_prev, i);
900 i = (i + 1) % IP6REASS_NHASH;
904 VNET_LIST_RUNLOCK_NOSLEEP();
908 * Drain off all datagram fragments.
913 VNET_ITERATOR_DECL(vnet_iter);
917 VNET_LIST_RLOCK_NOSLEEP();
918 VNET_FOREACH(vnet_iter) {
919 CURVNET_SET(vnet_iter);
920 for (i = 0; i < IP6REASS_NHASH; i++) {
921 if (IP6Q_TRYLOCK(i) == 0)
924 while (head->ip6q_next != head) {
925 IP6STAT_INC(ip6s_fragdropped);
926 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
927 frag6_freef(head->ip6q_next, i);
933 VNET_LIST_RUNLOCK_NOSLEEP();
937 ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
939 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
942 /* Delete frag6 header. */
943 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
944 /* This is the only possible case with !PULLDOWN_TEST. */
945 bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
947 m->m_data += sizeof(struct ip6_frag);
948 m->m_len -= sizeof(struct ip6_frag);
950 /* This comes with no copy if the boundary is on cluster. */
951 if ((t = m_split(m, offset, wait)) == NULL)
953 m_adj(t, sizeof(struct ip6_frag));
957 m->m_flags |= M_FRAGMENTED;