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/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <netinet/ip6.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet/icmp6.h>
55 #include <netinet/in_systm.h> /* for ECN definitions */
56 #include <netinet/ip.h> /* for ECN definitions */
59 * Define it to get a correct behavior on per-interface statistics.
60 * You will need to perform an extra routing table lookup, per fragment,
61 * to do it. This may, or may not be, a performance hit.
63 #define IN6_IFSTAT_STRICT
65 static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
66 static void frag6_deq(struct ip6asfrag *);
67 static void frag6_insque(struct ip6q *, struct ip6q *);
68 static void frag6_remque(struct ip6q *);
69 static void frag6_freef(struct ip6q *);
71 static struct mtx ip6qlock;
73 * These fields all protected by ip6qlock.
75 static u_int frag6_nfragpackets;
76 static u_int frag6_nfrags;
77 static struct ip6q ip6q; /* ip6 reassemble queue */
79 #define IP6Q_LOCK_INIT() mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
80 #define IP6Q_LOCK() mtx_lock(&ip6qlock)
81 #define IP6Q_TRYLOCK() mtx_trylock(&ip6qlock)
82 #define IP6Q_LOCK_ASSERT() mtx_assert(&ip6qlock, MA_OWNED)
83 #define IP6Q_UNLOCK() mtx_unlock(&ip6qlock)
85 static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
88 * Initialise reassembly queue and fragment identifier.
91 frag6_change(void *tag)
94 ip6_maxfragpackets = nmbclusters / 4;
95 ip6_maxfrags = nmbclusters / 4;
102 ip6_maxfragpackets = nmbclusters / 4;
103 ip6_maxfrags = nmbclusters / 4;
104 EVENTHANDLER_REGISTER(nmbclusters_change,
105 frag6_change, NULL, EVENTHANDLER_PRI_ANY);
109 ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
113 * In RFC2460, fragment and reassembly rule do not agree with each other,
114 * in terms of next header field handling in fragment header.
115 * While the sender will use the same value for all of the fragmented packets,
116 * receiver is suggested not to check the consistency.
118 * fragment rule (p20):
119 * (2) A Fragment header containing:
120 * The Next Header value that identifies the first header of
121 * the Fragmentable Part of the original packet.
122 * -> next header field is same for all fragments
124 * reassembly rule (p21):
125 * The Next Header field of the last header of the Unfragmentable
126 * Part is obtained from the Next Header field of the first
127 * fragment's Fragment header.
128 * -> should grab it from the first fragment only
130 * The following note also contradicts with fragment rule - noone is going to
131 * send different fragment with different next header field.
133 * additional note (p22):
134 * The Next Header values in the Fragment headers of different
135 * fragments of the same original packet may differ. Only the value
136 * from the Offset zero fragment packet is used for reassembly.
137 * -> should grab it from the first fragment only
139 * There is no explicit reason given in the RFC. Historical reason maybe?
145 frag6_input(struct mbuf **mp, int *offp, int proto)
147 struct mbuf *m = *mp, *t;
149 struct ip6_frag *ip6f;
151 struct ip6asfrag *af6, *ip6af, *af6dwn;
152 #ifdef IN6_IFSTAT_STRICT
153 struct in6_ifaddr *ia;
155 int offset = *offp, nxt, i, next;
157 int fragoff, frgpartlen; /* must be larger than u_int16_t */
158 struct ifnet *dstifp;
161 char ip6buf[INET6_ADDRSTRLEN];
164 ip6 = mtod(m, struct ip6_hdr *);
165 #ifndef PULLDOWN_TEST
166 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
167 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
169 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
171 return (IPPROTO_DONE);
175 #ifdef IN6_IFSTAT_STRICT
176 /* find the destination interface of the packet. */
177 if ((ia = ip6_getdstifaddr(m)) != NULL)
180 /* we are violating the spec, this is not the destination interface */
181 if ((m->m_flags & M_PKTHDR) != 0)
182 dstifp = m->m_pkthdr.rcvif;
185 /* jumbo payload can't contain a fragment header */
186 if (ip6->ip6_plen == 0) {
187 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
188 in6_ifstat_inc(dstifp, ifs6_reass_fail);
193 * check whether fragment packet's fragment length is
194 * multiple of 8 octets.
195 * sizeof(struct ip6_frag) == 8
196 * sizeof(struct ip6_hdr) = 40
198 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
199 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
200 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
201 offsetof(struct ip6_hdr, ip6_plen));
202 in6_ifstat_inc(dstifp, ifs6_reass_fail);
206 ip6stat.ip6s_fragments++;
207 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
209 /* offset now points to data portion */
210 offset += sizeof(struct ip6_frag);
215 * Enforce upper bound on number of fragments.
216 * If maxfrag is 0, never accept fragments.
217 * If maxfrag is -1, accept all fragments without limitation.
219 if (ip6_maxfrags < 0)
221 else if (frag6_nfrags >= (u_int)ip6_maxfrags)
224 for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
225 if (ip6f->ip6f_ident == q6->ip6q_ident &&
226 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
227 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
232 * the first fragment to arrive, create a reassembly queue.
237 * Enforce upper bound on number of fragmented packets
238 * for which we attempt reassembly;
239 * If maxfragpackets is 0, never accept fragments.
240 * If maxfragpackets is -1, accept all fragments without
243 if (ip6_maxfragpackets < 0)
245 else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
247 frag6_nfragpackets++;
248 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
252 bzero(q6, sizeof(*q6));
254 frag6_insque(q6, &ip6q);
256 /* ip6q_nxt will be filled afterwards, from 1st fragment */
257 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
259 q6->ip6q_nxtp = (u_char *)nxtp;
261 q6->ip6q_ident = ip6f->ip6f_ident;
262 q6->ip6q_ttl = IPV6_FRAGTTL;
263 q6->ip6q_src = ip6->ip6_src;
264 q6->ip6q_dst = ip6->ip6_dst;
266 (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
267 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
273 * If it's the 1st fragment, record the length of the
274 * unfragmentable part and the next header of the fragment header.
276 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
278 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
279 sizeof(struct ip6_frag);
280 q6->ip6q_nxt = ip6f->ip6f_nxt;
284 * Check that the reassembled packet would not exceed 65535 bytes
286 * If it would exceed, discard the fragment and return an ICMP error.
288 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
289 if (q6->ip6q_unfrglen >= 0) {
290 /* The 1st fragment has already arrived. */
291 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
292 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
293 offset - sizeof(struct ip6_frag) +
294 offsetof(struct ip6_frag, ip6f_offlg));
296 return (IPPROTO_DONE);
298 } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
299 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
300 offset - sizeof(struct ip6_frag) +
301 offsetof(struct ip6_frag, ip6f_offlg));
303 return (IPPROTO_DONE);
306 * If it's the first fragment, do the above check for each
307 * fragment already stored in the reassembly queue.
310 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
312 af6dwn = af6->ip6af_down;
314 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
316 struct mbuf *merr = IP6_REASS_MBUF(af6);
317 struct ip6_hdr *ip6err;
318 int erroff = af6->ip6af_offset;
320 /* dequeue the fragment. */
324 /* adjust pointer. */
325 ip6err = mtod(merr, struct ip6_hdr *);
328 * Restore source and destination addresses
329 * in the erroneous IPv6 header.
331 ip6err->ip6_src = q6->ip6q_src;
332 ip6err->ip6_dst = q6->ip6q_dst;
334 icmp6_error(merr, ICMP6_PARAM_PROB,
335 ICMP6_PARAMPROB_HEADER,
336 erroff - sizeof(struct ip6_frag) +
337 offsetof(struct ip6_frag, ip6f_offlg));
342 ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
346 bzero(ip6af, sizeof(*ip6af));
347 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
348 ip6af->ip6af_off = fragoff;
349 ip6af->ip6af_frglen = frgpartlen;
350 ip6af->ip6af_offset = offset;
351 IP6_REASS_MBUF(ip6af) = m;
354 af6 = (struct ip6asfrag *)q6;
359 * Handle ECN by comparing this segment with the first one;
360 * if CE is set, do not lose CE.
361 * drop if CE and not-ECT are mixed for the same packet.
363 ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
365 if (ecn == IPTOS_ECN_CE) {
366 if (ecn0 == IPTOS_ECN_NOTECT) {
367 free(ip6af, M_FTABLE);
370 if (ecn0 != IPTOS_ECN_CE)
371 q6->ip6q_ecn = IPTOS_ECN_CE;
373 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
374 free(ip6af, M_FTABLE);
379 * Find a segment which begins after this one does.
381 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
382 af6 = af6->ip6af_down)
383 if (af6->ip6af_off > ip6af->ip6af_off)
388 * If there is a preceding segment, it may provide some of
389 * our data already. If so, drop the data from the incoming
390 * segment. If it provides all of our data, drop us.
392 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
393 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
396 if (i >= ip6af->ip6af_frglen)
398 m_adj(IP6_REASS_MBUF(ip6af), i);
399 ip6af->ip6af_off += i;
400 ip6af->ip6af_frglen -= i;
405 * While we overlap succeeding segments trim them or,
406 * if they are completely covered, dequeue them.
408 while (af6 != (struct ip6asfrag *)q6 &&
409 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
410 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
411 if (i < af6->ip6af_frglen) {
412 af6->ip6af_frglen -= i;
414 m_adj(IP6_REASS_MBUF(af6), i);
417 af6 = af6->ip6af_down;
418 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
419 frag6_deq(af6->ip6af_up);
423 * If the incoming framgent overlaps some existing fragments in
424 * the reassembly queue, drop it, since it is dangerous to override
425 * existing fragments from a security point of view.
426 * We don't know which fragment is the bad guy - here we trust
427 * fragment that came in earlier, with no real reason.
429 * Note: due to changes after disabling this part, mbuf passed to
430 * m_adj() below now does not meet the requirement.
432 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
433 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
436 #if 0 /* suppress the noisy log */
437 log(LOG_ERR, "%d bytes of a fragment from %s "
438 "overlaps the previous fragment\n",
439 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
441 free(ip6af, M_FTABLE);
445 if (af6 != (struct ip6asfrag *)q6) {
446 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
448 #if 0 /* suppress the noisy log */
449 log(LOG_ERR, "%d bytes of a fragment from %s "
450 "overlaps the succeeding fragment",
451 i, ip6_sprintf(ip6buf, &q6->ip6q_src));
453 free(ip6af, M_FTABLE);
462 * Stick new segment in its place;
463 * check for complete reassembly.
464 * Move to front of packet queue, as we are
465 * the most recently active fragmented packet.
467 frag6_enq(ip6af, af6->ip6af_up);
471 if (q6 != ip6q.ip6q_next) {
473 frag6_insque(q6, &ip6q);
477 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
478 af6 = af6->ip6af_down) {
479 if (af6->ip6af_off != next) {
483 next += af6->ip6af_frglen;
485 if (af6->ip6af_up->ip6af_mff) {
491 * Reassembly is complete; concatenate fragments.
493 ip6af = q6->ip6q_down;
494 t = m = IP6_REASS_MBUF(ip6af);
495 af6 = ip6af->ip6af_down;
497 while (af6 != (struct ip6asfrag *)q6) {
498 af6dwn = af6->ip6af_down;
502 t->m_next = IP6_REASS_MBUF(af6);
503 m_adj(t->m_next, af6->ip6af_offset);
508 /* adjust offset to point where the original next header starts */
509 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
510 free(ip6af, M_FTABLE);
511 ip6 = mtod(m, struct ip6_hdr *);
512 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
513 if (q6->ip6q_ecn == IPTOS_ECN_CE)
514 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
517 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
520 /* Delete frag6 header */
521 if (m->m_len >= offset + sizeof(struct ip6_frag)) {
522 /* This is the only possible case with !PULLDOWN_TEST */
523 ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
525 m->m_data += sizeof(struct ip6_frag);
526 m->m_len -= sizeof(struct ip6_frag);
528 /* this comes with no copy if the boundary is on cluster */
529 if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
531 frag6_nfrags -= q6->ip6q_nfrag;
533 frag6_nfragpackets--;
536 m_adj(t, sizeof(struct ip6_frag));
541 * Store NXT to the original.
544 char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
549 frag6_nfrags -= q6->ip6q_nfrag;
551 frag6_nfragpackets--;
553 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
555 for (t = m; t; t = t->m_next)
557 m->m_pkthdr.len = plen;
560 ip6stat.ip6s_reassembled++;
561 in6_ifstat_inc(dstifp, ifs6_reass_ok);
564 * Tell launch routine the next header
575 in6_ifstat_inc(dstifp, ifs6_reass_fail);
576 ip6stat.ip6s_fragdropped++;
582 * Free a fragment reassembly header and all
583 * associated datagrams.
586 frag6_freef(struct ip6q *q6)
588 struct ip6asfrag *af6, *down6;
592 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
594 struct mbuf *m = IP6_REASS_MBUF(af6);
596 down6 = af6->ip6af_down;
600 * Return ICMP time exceeded error for the 1st fragment.
601 * Just free other fragments.
603 if (af6->ip6af_off == 0) {
607 ip6 = mtod(m, struct ip6_hdr *);
609 /* restore source and destination addresses */
610 ip6->ip6_src = q6->ip6q_src;
611 ip6->ip6_dst = q6->ip6q_dst;
613 icmp6_error(m, ICMP6_TIME_EXCEEDED,
614 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
620 frag6_nfrags -= q6->ip6q_nfrag;
622 frag6_nfragpackets--;
626 * Put an ip fragment on a reassembly chain.
627 * Like insque, but pointers in middle of structure.
630 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
636 af6->ip6af_down = up6->ip6af_down;
637 up6->ip6af_down->ip6af_up = af6;
638 up6->ip6af_down = af6;
642 * To frag6_enq as remque is to insque.
645 frag6_deq(struct ip6asfrag *af6)
650 af6->ip6af_up->ip6af_down = af6->ip6af_down;
651 af6->ip6af_down->ip6af_up = af6->ip6af_up;
655 frag6_insque(struct ip6q *new, struct ip6q *old)
660 new->ip6q_prev = old;
661 new->ip6q_next = old->ip6q_next;
662 old->ip6q_next->ip6q_prev= new;
663 old->ip6q_next = new;
667 frag6_remque(struct ip6q *p6)
672 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
673 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
677 * IPv6 reassembling timer processing;
678 * if a timer expires on a reassembly
687 GIANT_REQUIRED; /* XXX bz: ip6_forward_rt */
693 while (q6 != &ip6q) {
696 if (q6->ip6q_prev->ip6q_ttl == 0) {
697 ip6stat.ip6s_fragtimeout++;
698 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
699 frag6_freef(q6->ip6q_prev);
703 * If we are over the maximum number of fragments
704 * (due to the limit being lowered), drain off
705 * enough to get down to the new limit.
707 while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
709 ip6stat.ip6s_fragoverflow++;
710 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
711 frag6_freef(ip6q.ip6q_prev);
717 * Routing changes might produce a better route than we last used;
718 * make sure we notice eventually, even if forwarding only for one
719 * destination and the cache is never replaced.
721 if (ip6_forward_rt.ro_rt) {
722 RTFREE(ip6_forward_rt.ro_rt);
723 ip6_forward_rt.ro_rt = 0;
725 if (ipsrcchk_rt.ro_rt) {
726 RTFREE(ipsrcchk_rt.ro_rt);
727 ipsrcchk_rt.ro_rt = 0;
733 * Drain off all datagram fragments.
739 if (IP6Q_TRYLOCK() == 0)
741 while (ip6q.ip6q_next != &ip6q) {
742 ip6stat.ip6s_fragdropped++;
743 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
744 frag6_freef(ip6q.ip6q_next);