]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/frag6.c
Revis manual pages. [SA-18:08.tcp]
[FreeBSD/FreeBSD.git] / sys / netinet6 / frag6.c
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  *      $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_rss.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/hash.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.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>
47 #include <sys/time.h>
48 #include <sys/kernel.h>
49 #include <sys/syslog.h>
50
51 #include <machine/atomic.h>
52
53 #include <net/if.h>
54 #include <net/if_var.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/vnet.h>
58
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 */
66
67 #include <security/mac/mac_framework.h>
68
69 /*
70  * Reassembly headers are stored in hash buckets.
71  */
72 #define IP6REASS_NHASH_LOG2     10
73 #define IP6REASS_NHASH          (1 << IP6REASS_NHASH_LOG2)
74 #define IP6REASS_HMASK          (IP6REASS_NHASH - 1)
75
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 *,
80     uint32_t bucket);
81 static void frag6_remque(struct ip6q *, uint32_t bucket);
82 static void frag6_freef(struct ip6q *, uint32_t bucket);
83
84 struct ip6qbucket {
85         struct ip6q     ip6q;
86         struct mtx      lock;
87         int             count;
88 };
89
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);
94
95 #define V_frag6_nfragpackets            VNET(frag6_nfragpackets)
96 #define V_ip6q                          VNET(ip6q)
97 #define V_ip6q_hashseed                 VNET(ip6q_hashseed)
98
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)
104
105 static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
106
107 /*
108  * By default, limit the number of IP6 fragments across all reassembly
109  * queues to  1/32 of the total number of mbuf clusters.
110  *
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
118  * under DoS attack.
119  */
120 #define IP6_MAXFRAGS            (nmbclusters / 32)
121 #define IP6_MAXFRAGPACKETS      (imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
122
123 /*
124  * Initialise reassembly queue and fragment identifier.
125  */
126 void
127 frag6_set_bucketsize()
128 {
129         int i;
130
131         if ((i = V_ip6_maxfragpackets) > 0)
132                 V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
133 }
134
135 static void
136 frag6_change(void *tag)
137 {
138         VNET_ITERATOR_DECL(vnet_iter);
139
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();
146                 CURVNET_RESTORE();
147         }
148         VNET_LIST_RUNLOCK_NOSLEEP();
149 }
150
151 void
152 frag6_init(void)
153 {
154         struct ip6q *q6;
155         int i;
156
157         V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
158         frag6_set_bucketsize();
159         for (i = 0; i < IP6REASS_NHASH; i++) {
160                 q6 = IP6Q_HEAD(i);
161                 q6->ip6q_next = q6->ip6q_prev = q6;
162                 mtx_init(&V_ip6q[i].lock, "ip6qlock", NULL, MTX_DEF);
163                 V_ip6q[i].count = 0;
164         }
165         V_ip6q_hashseed = arc4random();
166         V_ip6_maxfragsperpacket = 64;
167         if (!IS_DEFAULT_VNET(curvnet))
168                 return;
169
170         ip6_maxfrags = IP6_MAXFRAGS;
171         EVENTHANDLER_REGISTER(nmbclusters_change,
172             frag6_change, NULL, EVENTHANDLER_PRI_ANY);
173 }
174
175 /*
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.
180  *
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
186  *
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
192  *
193  * The following note also contradicts with fragment rule - no one is going to
194  * send different fragment with different next header field.
195  *
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
201  *
202  * There is no explicit reason given in the RFC.  Historical reason maybe?
203  */
204 /*
205  * Fragment input
206  */
207 int
208 frag6_input(struct mbuf **mp, int *offp, int proto)
209 {
210         struct mbuf *m = *mp, *t;
211         struct ip6_hdr *ip6;
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;
217         int first_frag = 0;
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;
221         u_int8_t ecn, ecn0;
222 #ifdef RSS
223         struct m_tag *mtag;
224         struct ip6_direct_ctx *ip6dc;
225 #endif
226
227 #if 0
228         char ip6buf[INET6_ADDRSTRLEN];
229 #endif
230
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);
235 #else
236         IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
237         if (ip6f == NULL)
238                 return (IPPROTO_DONE);
239 #endif
240
241         dstifp = NULL;
242         /* find the destination interface of the packet. */
243         ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
244         if (ia != NULL) {
245                 dstifp = ia->ia_ifp;
246                 ifa_free(&ia->ia_ifa);
247         }
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);
252                 return IPPROTO_DONE;
253         }
254
255         /*
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
260          */
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);
266                 return IPPROTO_DONE;
267         }
268
269         IP6STAT_INC(ip6s_fragments);
270         in6_ifstat_inc(dstifp, ifs6_reass_reqd);
271
272         /* offset now points to data portion */
273         offset += sizeof(struct ip6_frag);
274
275         /*
276          * RFC 6946: Handle "atomic" fragments (offset and m bit set to 0)
277          * upfront, unrelated to any reassembly.  Just skip the fragment header.
278          */
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);
283                 *offp = offset;
284                 m->m_flags |= M_FRAGMENTED;
285                 return (ip6f->ip6f_nxt);
286         }
287
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);
295                 return IPPROTO_DONE;
296         }
297
298         hashkeyp = hashkey;
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);
307         IP6Q_LOCK(hash);
308
309         /*
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.
313          */
314         if (ip6_maxfrags < 0)
315                 ;
316         else if (frag6_nfrags >= (u_int)ip6_maxfrags)
317                 goto dropfrag;
318
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)
323 #ifdef MAC
324                     && mac_ip6q_match(m, q6)
325 #endif
326                     )
327                         break;
328
329         if (q6 == head) {
330                 /*
331                  * the first fragment to arrive, create a reassembly queue.
332                  */
333                 first_frag = 1;
334
335                 /*
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
340                  * limitation.
341                  */
342                 if (V_ip6_maxfragpackets < 0)
343                         ;
344                 else if (V_ip6q[hash].count >= V_ip6_maxfragbucketsize ||
345                     V_frag6_nfragpackets >= (u_int)V_ip6_maxfragpackets)
346                         goto dropfrag;
347                 atomic_add_int(&V_frag6_nfragpackets, 1);
348                 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
349                     M_NOWAIT);
350                 if (q6 == NULL)
351                         goto dropfrag;
352                 bzero(q6, sizeof(*q6));
353 #ifdef MAC
354                 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
355                         free(q6, M_FTABLE);
356                         goto dropfrag;
357                 }
358                 mac_ip6q_create(m, q6);
359 #endif
360                 frag6_insque_head(q6, head, hash);
361
362                 /* ip6q_nxt will be filled afterwards, from 1st fragment */
363                 q6->ip6q_down   = q6->ip6q_up = (struct ip6asfrag *)q6;
364 #ifdef notyet
365                 q6->ip6q_nxtp   = (u_char *)nxtp;
366 #endif
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;
371                 q6->ip6q_ecn    =
372                     (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
373                 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
374
375                 q6->ip6q_nfrag = 0;
376         }
377
378         /*
379          * If it's the 1st fragment, record the length of the
380          * unfragmentable part and the next header of the fragment header.
381          */
382         fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
383         if (fragoff == 0) {
384                 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
385                     sizeof(struct ip6_frag);
386                 q6->ip6q_nxt = ip6f->ip6f_nxt;
387         }
388
389         /*
390          * Check that the reassembled packet would not exceed 65535 bytes
391          * in size.
392          * If it would exceed, discard the fragment and return an ICMP error.
393          */
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));
400                         IP6Q_UNLOCK(hash);
401                         return (IPPROTO_DONE);
402                 }
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));
407                 IP6Q_UNLOCK(hash);
408                 return (IPPROTO_DONE);
409         }
410         /*
411          * If it's the first fragment, do the above check for each
412          * fragment already stored in the reassembly queue.
413          */
414         if (fragoff == 0) {
415                 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
416                      af6 = af6dwn) {
417                         af6dwn = af6->ip6af_down;
418
419                         if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
420                             IPV6_MAXPACKET) {
421                                 struct mbuf *merr = IP6_REASS_MBUF(af6);
422                                 struct ip6_hdr *ip6err;
423                                 int erroff = af6->ip6af_offset;
424
425                                 /* dequeue the fragment. */
426                                 frag6_deq(af6, hash);
427                                 free(af6, M_FTABLE);
428
429                                 /* adjust pointer. */
430                                 ip6err = mtod(merr, struct ip6_hdr *);
431
432                                 /*
433                                  * Restore source and destination addresses
434                                  * in the erroneous IPv6 header.
435                                  */
436                                 ip6err->ip6_src = q6->ip6q_src;
437                                 ip6err->ip6_dst = q6->ip6q_dst;
438
439                                 icmp6_error(merr, ICMP6_PARAM_PROB,
440                                     ICMP6_PARAMPROB_HEADER,
441                                     erroff - sizeof(struct ip6_frag) +
442                                     offsetof(struct ip6_frag, ip6f_offlg));
443                         }
444                 }
445         }
446
447         ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
448             M_NOWAIT);
449         if (ip6af == NULL)
450                 goto dropfrag;
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;
457
458         if (first_frag) {
459                 af6 = (struct ip6asfrag *)q6;
460                 goto insert;
461         }
462
463         /*
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.
467          */
468         ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
469         ecn0 = q6->ip6q_ecn;
470         if (ecn == IPTOS_ECN_CE) {
471                 if (ecn0 == IPTOS_ECN_NOTECT) {
472                         free(ip6af, M_FTABLE);
473                         goto dropfrag;
474                 }
475                 if (ecn0 != IPTOS_ECN_CE)
476                         q6->ip6q_ecn = IPTOS_ECN_CE;
477         }
478         if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
479                 free(ip6af, M_FTABLE);
480                 goto dropfrag;
481         }
482
483         /*
484          * Find a segment which begins after this one does.
485          */
486         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
487              af6 = af6->ip6af_down)
488                 if (af6->ip6af_off > ip6af->ip6af_off)
489                         break;
490
491 #if 0
492         /*
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.
496          */
497         if (af6->ip6af_up != (struct ip6asfrag *)q6) {
498                 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
499                         - ip6af->ip6af_off;
500                 if (i > 0) {
501                         if (i >= ip6af->ip6af_frglen)
502                                 goto dropfrag;
503                         m_adj(IP6_REASS_MBUF(ip6af), i);
504                         ip6af->ip6af_off += i;
505                         ip6af->ip6af_frglen -= i;
506                 }
507         }
508
509         /*
510          * While we overlap succeeding segments trim them or,
511          * if they are completely covered, dequeue them.
512          */
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;
518                         af6->ip6af_off += i;
519                         m_adj(IP6_REASS_MBUF(af6), i);
520                         break;
521                 }
522                 af6 = af6->ip6af_down;
523                 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
524                 frag6_deq(af6->ip6af_up, hash);
525         }
526 #else
527         /*
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.
533          *
534          * Note: due to changes after disabling this part, mbuf passed to
535          * m_adj() below now does not meet the requirement.
536          */
537         if (af6->ip6af_up != (struct ip6asfrag *)q6) {
538                 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
539                         - ip6af->ip6af_off;
540                 if (i > 0) {
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));
545 #endif
546                         free(ip6af, M_FTABLE);
547                         goto dropfrag;
548                 }
549         }
550         if (af6 != (struct ip6asfrag *)q6) {
551                 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
552                 if (i > 0) {
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));
557 #endif
558                         free(ip6af, M_FTABLE);
559                         goto dropfrag;
560                 }
561         }
562 #endif
563
564 insert:
565 #ifdef MAC
566         if (!first_frag)
567                 mac_ip6q_update(m, q6);
568 #endif
569
570         /*
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.
576          */
577         frag6_enq(ip6af, af6->ip6af_up, hash);
578         atomic_add_int(&frag6_nfrags, 1);
579         q6->ip6q_nfrag++;
580 #if 0 /* xxx */
581         if (q6 != head->ip6q_next) {
582                 frag6_remque(q6, hash);
583                 frag6_insque_head(q6, head, hash);
584         }
585 #endif
586         next = 0;
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);
593                         }
594                         IP6Q_UNLOCK(hash);
595                         return IPPROTO_DONE;
596                 }
597                 next += af6->ip6af_frglen;
598         }
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);
603                 }
604                 IP6Q_UNLOCK(hash);
605                 return IPPROTO_DONE;
606         }
607
608         /*
609          * Reassembly is complete; concatenate fragments.
610          */
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;
620
621                 af6dwn = af6->ip6af_down;
622                 frag6_deq(af6, hash);
623                 while (t->m_next)
624                         t = t->m_next;
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));
628                 free(af6, M_FTABLE);
629                 af6 = af6dwn;
630         }
631
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);
635
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);
643         nxt = q6->ip6q_nxt;
644 #ifdef notyet
645         *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
646 #endif
647
648         if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
649                 frag6_remque(q6, hash);
650                 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
651 #ifdef MAC
652                 mac_ip6q_destroy(q6);
653 #endif
654                 free(q6, M_FTABLE);
655                 atomic_subtract_int(&V_frag6_nfragpackets, 1);
656
657                 goto dropfrag;
658         }
659
660         /*
661          * Store NXT to the original.
662          */
663         m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
664             (caddr_t)&nxt);
665
666         frag6_remque(q6, hash);
667         atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
668 #ifdef MAC
669         mac_ip6q_reassemble(q6, m);
670         mac_ip6q_destroy(q6);
671 #endif
672         free(q6, M_FTABLE);
673         atomic_subtract_int(&V_frag6_nfragpackets, 1);
674
675         if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
676                 int plen = 0;
677                 for (t = m; t; t = t->m_next)
678                         plen += t->m_len;
679                 m->m_pkthdr.len = plen;
680         }
681
682 #ifdef RSS
683         mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
684             M_NOWAIT);
685         if (mtag == NULL)
686                 goto dropfrag;
687
688         ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
689         ip6dc->ip6dc_nxt = nxt;
690         ip6dc->ip6dc_off = offset;
691
692         m_tag_prepend(m, mtag);
693 #endif
694
695         IP6Q_UNLOCK(hash);
696         IP6STAT_INC(ip6s_reassembled);
697         in6_ifstat_inc(dstifp, ifs6_reass_ok);
698
699 #ifdef RSS
700         /*
701          * Queue/dispatch for reprocessing.
702          */
703         netisr_dispatch(NETISR_IPV6_DIRECT, m);
704         return IPPROTO_DONE;
705 #endif
706
707         /*
708          * Tell launch routine the next header
709          */
710
711         *mp = m;
712         *offp = offset;
713
714         return nxt;
715
716  dropfrag:
717         IP6Q_UNLOCK(hash);
718         in6_ifstat_inc(dstifp, ifs6_reass_fail);
719         IP6STAT_INC(ip6s_fragdropped);
720         m_freem(m);
721         return IPPROTO_DONE;
722 }
723
724 /*
725  * Free a fragment reassembly header and all
726  * associated datagrams.
727  */
728 static void
729 frag6_freef(struct ip6q *q6, uint32_t bucket)
730 {
731         struct ip6asfrag *af6, *down6;
732
733         IP6Q_LOCK_ASSERT(bucket);
734
735         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
736              af6 = down6) {
737                 struct mbuf *m = IP6_REASS_MBUF(af6);
738
739                 down6 = af6->ip6af_down;
740                 frag6_deq(af6, bucket);
741
742                 /*
743                  * Return ICMP time exceeded error for the 1st fragment.
744                  * Just free other fragments.
745                  */
746                 if (af6->ip6af_off == 0) {
747                         struct ip6_hdr *ip6;
748
749                         /* adjust pointer */
750                         ip6 = mtod(m, struct ip6_hdr *);
751
752                         /* restore source and destination addresses */
753                         ip6->ip6_src = q6->ip6q_src;
754                         ip6->ip6_dst = q6->ip6q_dst;
755
756                         icmp6_error(m, ICMP6_TIME_EXCEEDED,
757                                     ICMP6_TIME_EXCEED_REASSEMBLY, 0);
758                 } else
759                         m_freem(m);
760                 free(af6, M_FTABLE);
761         }
762         frag6_remque(q6, bucket);
763         atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
764 #ifdef MAC
765         mac_ip6q_destroy(q6);
766 #endif
767         free(q6, M_FTABLE);
768         atomic_subtract_int(&V_frag6_nfragpackets, 1);
769 }
770
771 /*
772  * Put an ip fragment on a reassembly chain.
773  * Like insque, but pointers in middle of structure.
774  */
775 static void
776 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
777     uint32_t bucket __unused)
778 {
779
780         IP6Q_LOCK_ASSERT(bucket);
781
782         af6->ip6af_up = up6;
783         af6->ip6af_down = up6->ip6af_down;
784         up6->ip6af_down->ip6af_up = af6;
785         up6->ip6af_down = af6;
786 }
787
788 /*
789  * To frag6_enq as remque is to insque.
790  */
791 static void
792 frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
793 {
794
795         IP6Q_LOCK_ASSERT(bucket);
796
797         af6->ip6af_up->ip6af_down = af6->ip6af_down;
798         af6->ip6af_down->ip6af_up = af6->ip6af_up;
799 }
800
801 static void
802 frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket)
803 {
804
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));
809
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++;
815 }
816
817 static void
818 frag6_remque(struct ip6q *p6, uint32_t bucket)
819 {
820
821         IP6Q_LOCK_ASSERT(bucket);
822
823         p6->ip6q_prev->ip6q_next = p6->ip6q_next;
824         p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
825         V_ip6q[bucket].count--;
826 }
827
828 /*
829  * IPv6 reassembling timer processing;
830  * if a timer expires on a reassembly
831  * queue, discard it.
832  */
833 void
834 frag6_slowtimo(void)
835 {
836         VNET_ITERATOR_DECL(vnet_iter);
837         struct ip6q *head, *q6;
838         int i;
839
840         VNET_LIST_RLOCK_NOSLEEP();
841         VNET_FOREACH(vnet_iter) {
842                 CURVNET_SET(vnet_iter);
843                 for (i = 0; i < IP6REASS_NHASH; i++) {
844                         IP6Q_LOCK(i);
845                         head = IP6Q_HEAD(i);
846                         q6 = head->ip6q_next;
847                         if (q6 == NULL) {
848                                 /*
849                                  * XXXJTL: This should never happen. This
850                                  * should turn into an assertion.
851                                  */
852                                 IP6Q_UNLOCK(i);
853                                 continue;
854                         }
855                         while (q6 != head) {
856                                 --q6->ip6q_ttl;
857                                 q6 = q6->ip6q_next;
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);
862                                 }
863                         }
864                         /*
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
871                          * is negative.
872                          */
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);
880                         }
881                         IP6Q_UNLOCK(i);
882                 }
883                 /*
884                  * If we are still over the maximum number of fragmented
885                  * packets, drain off enough to get down to the new limit.
886                  */
887                 i = 0;
888                 while (V_ip6_maxfragpackets >= 0 &&
889                     V_frag6_nfragpackets > (u_int)V_ip6_maxfragpackets) {
890                         IP6Q_LOCK(i);
891                         head = IP6Q_HEAD(i);
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);
896                         }
897                         IP6Q_UNLOCK(i);
898                         i = (i + 1) % IP6REASS_NHASH;
899                 }
900                 CURVNET_RESTORE();
901         }
902         VNET_LIST_RUNLOCK_NOSLEEP();
903 }
904
905 /*
906  * Drain off all datagram fragments.
907  */
908 void
909 frag6_drain(void)
910 {
911         VNET_ITERATOR_DECL(vnet_iter);
912         struct ip6q *head;
913         int i;
914
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)
920                                 continue;
921                         head = IP6Q_HEAD(i);
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);
926                         }
927                         IP6Q_UNLOCK(i);
928                 }
929                 CURVNET_RESTORE();
930         }
931         VNET_LIST_RUNLOCK_NOSLEEP();
932 }
933
934 int
935 ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
936 {
937         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
938         struct mbuf *t;
939
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),
944                     offset);
945                 m->m_data += sizeof(struct ip6_frag);
946                 m->m_len -= sizeof(struct ip6_frag);
947         } else {
948                 /* This comes with no copy if the boundary is on cluster. */
949                 if ((t = m_split(m, offset, wait)) == NULL)
950                         return (ENOMEM);
951                 m_adj(t, sizeof(struct ip6_frag));
952                 m_cat(m, t);
953         }
954
955         m->m_flags |= M_FRAGMENTED;
956         return (0);
957 }