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$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
39 #include <sys/domain.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
44 #include <sys/kernel.h>
45 #include <sys/syslog.h>
48 #include <net/if_var.h>
49 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip6.h>
55 #include <netinet6/ip6_var.h>
56 #include <netinet/icmp6.h>
57 #include <netinet/in_systm.h> /* for ECN definitions */
58 #include <netinet/ip.h> /* for ECN definitions */
60 #include <security/mac/mac_framework.h>
62 static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
63 static void frag6_deq(struct ip6asfrag *);
64 static void frag6_insque(struct ip6q *, struct ip6q *);
65 static void frag6_remque(struct ip6q *);
66 static void frag6_freef(struct ip6q *);
68 static struct mtx ip6qlock;
70 * These fields all protected by ip6qlock.
72 static VNET_DEFINE(u_int, frag6_nfragpackets);
73 static VNET_DEFINE(u_int, frag6_nfrags);
74 static VNET_DEFINE(struct ip6q, ip6q); /* ip6 reassemble queue */
76 #define V_frag6_nfragpackets VNET(frag6_nfragpackets)
77 #define V_frag6_nfrags VNET(frag6_nfrags)
78 #define V_ip6q VNET(ip6q)
80 #define IP6Q_LOCK_INIT() mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
81 #define IP6Q_LOCK() mtx_lock(&ip6qlock)
82 #define IP6Q_TRYLOCK() mtx_trylock(&ip6qlock)
83 #define IP6Q_LOCK_ASSERT() mtx_assert(&ip6qlock, MA_OWNED)
84 #define IP6Q_UNLOCK() mtx_unlock(&ip6qlock)
86 static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
89 * Initialise reassembly queue and fragment identifier.
92 frag6_change(void *tag)
95 V_ip6_maxfragpackets = nmbclusters / 4;
96 V_ip6_maxfrags = nmbclusters / 4;
103 V_ip6_maxfragpackets = nmbclusters / 4;
104 V_ip6_maxfrags = nmbclusters / 4;
105 V_ip6q.ip6q_next = V_ip6q.ip6q_prev = &V_ip6q;
107 if (!IS_DEFAULT_VNET(curvnet))
110 EVENTHANDLER_REGISTER(nmbclusters_change,
111 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
117 * In RFC2460, fragment and reassembly rule do not agree with each other,
118 * in terms of next header field handling in fragment header.
119 * While the sender will use the same value for all of the fragmented packets,
120 * receiver is suggested not to check the consistency.
122 * fragment rule (p20):
123 * (2) A Fragment header containing:
124 * The Next Header value that identifies the first header of
125 * the Fragmentable Part of the original packet.
126 * -> next header field is same for all fragments
128 * reassembly rule (p21):
129 * The Next Header field of the last header of the Unfragmentable
130 * Part is obtained from the Next Header field of the first
131 * fragment's Fragment header.
132 * -> should grab it from the first fragment only
134 * The following note also contradicts with fragment rule - noone is going to
135 * send different fragment with different next header field.
137 * additional note (p22):
138 * The Next Header values in the Fragment headers of different
139 * fragments of the same original packet may differ. Only the value
140 * from the Offset zero fragment packet is used for reassembly.
141 * -> should grab it from the first fragment only
143 * There is no explicit reason given in the RFC. Historical reason maybe?
149 frag6_input(struct mbuf **mp, int *offp, int proto)
151 struct mbuf *m = *mp, *t;
153 struct ip6_frag *ip6f;
155 struct ip6asfrag *af6, *ip6af, *af6dwn;
156 struct in6_ifaddr *ia;
157 int offset = *offp, nxt, i, next;
159 int fragoff, frgpartlen; /* must be larger than u_int16_t */
160 struct ifnet *dstifp;
163 char ip6buf[INET6_ADDRSTRLEN];
166 ip6 = mtod(m, struct ip6_hdr *);
167 #ifndef PULLDOWN_TEST
168 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
169 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
171 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
173 return (IPPROTO_DONE);
177 /* find the destination interface of the packet. */
178 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
181 ifa_free(&ia->ia_ifa);
183 /* jumbo payload can't contain a fragment header */
184 if (ip6->ip6_plen == 0) {
185 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
186 in6_ifstat_inc(dstifp, ifs6_reass_fail);
191 * check whether fragment packet's fragment length is
192 * multiple of 8 octets.
193 * sizeof(struct ip6_frag) == 8
194 * sizeof(struct ip6_hdr) = 40
196 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
197 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
198 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
199 offsetof(struct ip6_hdr, ip6_plen));
200 in6_ifstat_inc(dstifp, ifs6_reass_fail);
204 IP6STAT_INC(ip6s_fragments);
205 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
207 /* offset now points to data portion */
208 offset += sizeof(struct ip6_frag);
211 * RFC 6946: Handle "atomic" fragments (offset and m bit set to 0)
212 * upfront, unrelated to any reassembly. Just skip the fragment header.
214 if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
215 /* XXX-BZ we want dedicated counters for this. */
216 IP6STAT_INC(ip6s_reassembled);
217 in6_ifstat_inc(dstifp, ifs6_reass_ok);
219 return (ip6f->ip6f_nxt);
225 * Enforce upper bound on number of fragments.
226 * If maxfrag is 0, never accept fragments.
227 * If maxfrag is -1, accept all fragments without limitation.
229 if (V_ip6_maxfrags < 0)
231 else if (V_frag6_nfrags >= (u_int)V_ip6_maxfrags)
234 for (q6 = V_ip6q.ip6q_next; q6 != &V_ip6q; q6 = q6->ip6q_next)
235 if (ip6f->ip6f_ident == q6->ip6q_ident &&
236 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
237 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
239 && mac_ip6q_match(m, q6)
246 * the first fragment to arrive, create a reassembly queue.
251 * Enforce upper bound on number of fragmented packets
252 * for which we attempt reassembly;
253 * If maxfragpackets is 0, never accept fragments.
254 * If maxfragpackets is -1, accept all fragments without
257 if (V_ip6_maxfragpackets < 0)
259 else if (V_frag6_nfragpackets >= (u_int)V_ip6_maxfragpackets)
261 V_frag6_nfragpackets++;
262 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
266 bzero(q6, sizeof(*q6));
268 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
272 mac_ip6q_create(m, q6);
274 frag6_insque(q6, &V_ip6q);
276 /* ip6q_nxt will be filled afterwards, from 1st fragment */
277 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
279 q6->ip6q_nxtp = (u_char *)nxtp;
281 q6->ip6q_ident = ip6f->ip6f_ident;
282 q6->ip6q_ttl = IPV6_FRAGTTL;
283 q6->ip6q_src = ip6->ip6_src;
284 q6->ip6q_dst = ip6->ip6_dst;
286 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
287 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
293 * If it's the 1st fragment, record the length of the
294 * unfragmentable part and the next header of the fragment header.
296 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
298 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
299 sizeof(struct ip6_frag);
300 q6->ip6q_nxt = ip6f->ip6f_nxt;
304 * Check that the reassembled packet would not exceed 65535 bytes
306 * If it would exceed, discard the fragment and return an ICMP error.
308 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
309 if (q6->ip6q_unfrglen >= 0) {
310 /* The 1st fragment has already arrived. */
311 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
312 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
313 offset - sizeof(struct ip6_frag) +
314 offsetof(struct ip6_frag, ip6f_offlg));
316 return (IPPROTO_DONE);
318 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
319 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
320 offset - sizeof(struct ip6_frag) +
321 offsetof(struct ip6_frag, ip6f_offlg));
323 return (IPPROTO_DONE);
326 * If it's the first fragment, do the above check for each
327 * fragment already stored in the reassembly queue.
330 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
332 af6dwn = af6->ip6af_down;
334 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
336 struct mbuf *merr = IP6_REASS_MBUF(af6);
337 struct ip6_hdr *ip6err;
338 int erroff = af6->ip6af_offset;
340 /* dequeue the fragment. */
344 /* adjust pointer. */
345 ip6err = mtod(merr, struct ip6_hdr *);
348 * Restore source and destination addresses
349 * in the erroneous IPv6 header.
351 ip6err->ip6_src = q6->ip6q_src;
352 ip6err->ip6_dst = q6->ip6q_dst;
354 icmp6_error(merr, ICMP6_PARAM_PROB,
355 ICMP6_PARAMPROB_HEADER,
356 erroff - sizeof(struct ip6_frag) +
357 offsetof(struct ip6_frag, ip6f_offlg));
362 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
366 bzero(ip6af, sizeof(*ip6af));
367 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
368 ip6af->ip6af_off = fragoff;
369 ip6af->ip6af_frglen = frgpartlen;
370 ip6af->ip6af_offset = offset;
371 IP6_REASS_MBUF(ip6af) = m;
374 af6 = (struct ip6asfrag *)q6;
379 * Handle ECN by comparing this segment with the first one;
380 * if CE is set, do not lose CE.
381 * drop if CE and not-ECT are mixed for the same packet.
383 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
385 if (ecn == IPTOS_ECN_CE) {
386 if (ecn0 == IPTOS_ECN_NOTECT) {
387 free(ip6af, M_FTABLE);
390 if (ecn0 != IPTOS_ECN_CE)
391 q6->ip6q_ecn = IPTOS_ECN_CE;
393 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
394 free(ip6af, M_FTABLE);
399 * Find a segment which begins after this one does.
401 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
402 af6 = af6->ip6af_down)
403 if (af6->ip6af_off > ip6af->ip6af_off)
408 * If there is a preceding segment, it may provide some of
409 * our data already. If so, drop the data from the incoming
410 * segment. If it provides all of our data, drop us.
412 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
413 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
416 if (i >= ip6af->ip6af_frglen)
418 m_adj(IP6_REASS_MBUF(ip6af), i);
419 ip6af->ip6af_off += i;
420 ip6af->ip6af_frglen -= i;
425 * While we overlap succeeding segments trim them or,
426 * if they are completely covered, dequeue them.
428 while (af6 != (struct ip6asfrag *)q6 &&
429 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
430 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
431 if (i < af6->ip6af_frglen) {
432 af6->ip6af_frglen -= i;
434 m_adj(IP6_REASS_MBUF(af6), i);
437 af6 = af6->ip6af_down;
438 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
439 frag6_deq(af6->ip6af_up);
443 * If the incoming framgent overlaps some existing fragments in
444 * the reassembly queue, drop it, since it is dangerous to override
445 * existing fragments from a security point of view.
446 * We don't know which fragment is the bad guy - here we trust
447 * fragment that came in earlier, with no real reason.
449 * Note: due to changes after disabling this part, mbuf passed to
450 * m_adj() below now does not meet the requirement.
452 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
453 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
456 #if 0 /* suppress the noisy log */
457 log(LOG_ERR, "%d bytes of a fragment from %s "
458 "overlaps the previous fragment\n",
459 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
461 free(ip6af, M_FTABLE);
465 if (af6 != (struct ip6asfrag *)q6) {
466 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
468 #if 0 /* suppress the noisy log */
469 log(LOG_ERR, "%d bytes of a fragment from %s "
470 "overlaps the succeeding fragment",
471 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
473 free(ip6af, M_FTABLE);
482 mac_ip6q_update(m, q6);
486 * Stick new segment in its place;
487 * check for complete reassembly.
488 * Move to front of packet queue, as we are
489 * the most recently active fragmented packet.
491 frag6_enq(ip6af, af6->ip6af_up);
495 if (q6 != V_ip6q.ip6q_next) {
497 frag6_insque(q6, &V_ip6q);
501 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
502 af6 = af6->ip6af_down) {
503 if (af6->ip6af_off != next) {
507 next += af6->ip6af_frglen;
509 if (af6->ip6af_up->ip6af_mff) {
515 * Reassembly is complete; concatenate fragments.
517 ip6af = q6->ip6q_down;
518 t = m = IP6_REASS_MBUF(ip6af);
519 af6 = ip6af->ip6af_down;
521 while (af6 != (struct ip6asfrag *)q6) {
522 af6dwn = af6->ip6af_down;
526 t->m_next = IP6_REASS_MBUF(af6);
527 m_adj(t->m_next, af6->ip6af_offset);
532 /* adjust offset to point where the original next header starts */
533 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
534 free(ip6af, M_FTABLE);
535 ip6 = mtod(m, struct ip6_hdr *);
536 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
537 if (q6->ip6q_ecn == IPTOS_ECN_CE)
538 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
541 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
544 if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
546 V_frag6_nfrags -= q6->ip6q_nfrag;
548 mac_ip6q_destroy(q6);
551 V_frag6_nfragpackets--;
557 * Store NXT to the original.
560 char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
565 V_frag6_nfrags -= q6->ip6q_nfrag;
567 mac_ip6q_reassemble(q6, m);
568 mac_ip6q_destroy(q6);
571 V_frag6_nfragpackets--;
573 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
575 for (t = m; t; t = t->m_next)
577 m->m_pkthdr.len = plen;
580 IP6STAT_INC(ip6s_reassembled);
581 in6_ifstat_inc(dstifp, ifs6_reass_ok);
584 * Tell launch routine the next header
595 in6_ifstat_inc(dstifp, ifs6_reass_fail);
596 IP6STAT_INC(ip6s_fragdropped);
602 * Free a fragment reassembly header and all
603 * associated datagrams.
606 frag6_freef(struct ip6q *q6)
608 struct ip6asfrag *af6, *down6;
612 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
614 struct mbuf *m = IP6_REASS_MBUF(af6);
616 down6 = af6->ip6af_down;
620 * Return ICMP time exceeded error for the 1st fragment.
621 * Just free other fragments.
623 if (af6->ip6af_off == 0) {
627 ip6 = mtod(m, struct ip6_hdr *);
629 /* restore source and destination addresses */
630 ip6->ip6_src = q6->ip6q_src;
631 ip6->ip6_dst = q6->ip6q_dst;
633 icmp6_error(m, ICMP6_TIME_EXCEEDED,
634 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
640 V_frag6_nfrags -= q6->ip6q_nfrag;
642 mac_ip6q_destroy(q6);
645 V_frag6_nfragpackets--;
649 * Put an ip fragment on a reassembly chain.
650 * Like insque, but pointers in middle of structure.
653 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
659 af6->ip6af_down = up6->ip6af_down;
660 up6->ip6af_down->ip6af_up = af6;
661 up6->ip6af_down = af6;
665 * To frag6_enq as remque is to insque.
668 frag6_deq(struct ip6asfrag *af6)
673 af6->ip6af_up->ip6af_down = af6->ip6af_down;
674 af6->ip6af_down->ip6af_up = af6->ip6af_up;
678 frag6_insque(struct ip6q *new, struct ip6q *old)
683 new->ip6q_prev = old;
684 new->ip6q_next = old->ip6q_next;
685 old->ip6q_next->ip6q_prev= new;
686 old->ip6q_next = new;
690 frag6_remque(struct ip6q *p6)
695 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
696 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
700 * IPv6 reassembling timer processing;
701 * if a timer expires on a reassembly
707 VNET_ITERATOR_DECL(vnet_iter);
710 VNET_LIST_RLOCK_NOSLEEP();
712 VNET_FOREACH(vnet_iter) {
713 CURVNET_SET(vnet_iter);
714 q6 = V_ip6q.ip6q_next;
716 while (q6 != &V_ip6q) {
719 if (q6->ip6q_prev->ip6q_ttl == 0) {
720 IP6STAT_INC(ip6s_fragtimeout);
721 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
722 frag6_freef(q6->ip6q_prev);
726 * If we are over the maximum number of fragments
727 * (due to the limit being lowered), drain off
728 * enough to get down to the new limit.
730 while (V_frag6_nfragpackets > (u_int)V_ip6_maxfragpackets &&
732 IP6STAT_INC(ip6s_fragoverflow);
733 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
734 frag6_freef(V_ip6q.ip6q_prev);
739 VNET_LIST_RUNLOCK_NOSLEEP();
743 * Drain off all datagram fragments.
748 VNET_ITERATOR_DECL(vnet_iter);
750 VNET_LIST_RLOCK_NOSLEEP();
751 if (IP6Q_TRYLOCK() == 0) {
752 VNET_LIST_RUNLOCK_NOSLEEP();
755 VNET_FOREACH(vnet_iter) {
756 CURVNET_SET(vnet_iter);
757 while (V_ip6q.ip6q_next != &V_ip6q) {
758 IP6STAT_INC(ip6s_fragdropped);
759 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
760 frag6_freef(V_ip6q.ip6q_next);
765 VNET_LIST_RUNLOCK_NOSLEEP();
769 ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
771 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
774 /* Delete frag6 header. */
775 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
776 /* This is the only possible case with !PULLDOWN_TEST. */
777 bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
779 m->m_data += sizeof(struct ip6_frag);
780 m->m_len -= sizeof(struct ip6_frag);
782 /* This comes with no copy if the boundary is on cluster. */
783 if ((t = m_split(m, offset, wait)) == NULL)
785 m_adj(t, sizeof(struct ip6_frag));