]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/frag6.c
Make the IPv6 fragment limits be global, rather than per-VNET, limits.
[FreeBSD/FreeBSD.git] / sys / netinet6 / frag6.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_rss.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/hash.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/domain.h>
45 #include <sys/eventhandler.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
49 #include <sys/time.h>
50 #include <sys/kernel.h>
51 #include <sys/syslog.h>
52
53 #include <machine/atomic.h>
54
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59 #include <net/vnet.h>
60
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet/icmp6.h>
66 #include <netinet/in_systm.h>   /* for ECN definitions */
67 #include <netinet/ip.h>         /* for ECN definitions */
68
69 #include <security/mac/mac_framework.h>
70
71 /*
72  * Reassembly headers are stored in hash buckets.
73  */
74 #define IP6REASS_NHASH_LOG2     6
75 #define IP6REASS_NHASH          (1 << IP6REASS_NHASH_LOG2)
76 #define IP6REASS_HMASK          (IP6REASS_NHASH - 1)
77
78 static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *,
79     uint32_t bucket __unused);
80 static void frag6_deq(struct ip6asfrag *, uint32_t bucket __unused);
81 static void frag6_insque_head(struct ip6q *, struct ip6q *,
82     uint32_t bucket __unused);
83 static void frag6_remque(struct ip6q *, uint32_t bucket __unused);
84 static void frag6_freef(struct ip6q *, uint32_t bucket);
85
86 struct ip6qbucket {
87         struct ip6q     ip6q;
88         struct mtx      lock;
89 };
90
91 VNET_DEFINE_STATIC(volatile u_int, frag6_nfragpackets);
92 volatile u_int frag6_nfrags = 0;
93 VNET_DEFINE_STATIC(struct ip6qbucket, ip6q[IP6REASS_NHASH]);
94 VNET_DEFINE_STATIC(uint32_t, ip6q_hashseed);
95
96 #define V_frag6_nfragpackets            VNET(frag6_nfragpackets)
97 #define V_ip6q                          VNET(ip6q)
98 #define V_ip6q_hashseed                 VNET(ip6q_hashseed)
99
100 #define IP6Q_LOCK(i)            mtx_lock(&V_ip6q[(i)].lock)
101 #define IP6Q_TRYLOCK(i)         mtx_trylock(&V_ip6q[(i)].lock)
102 #define IP6Q_LOCK_ASSERT(i)     mtx_assert(&V_ip6q[(i)].lock, MA_OWNED)
103 #define IP6Q_UNLOCK(i)          mtx_unlock(&V_ip6q[(i)].lock)
104 #define IP6Q_HEAD(i)            (&V_ip6q[(i)].ip6q)
105
106 static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
107
108 /*
109  * Initialise reassembly queue and fragment identifier.
110  */
111 static void
112 frag6_change(void *tag)
113 {
114         VNET_ITERATOR_DECL(vnet_iter);
115
116         ip6_maxfrags = nmbclusters / 4;
117         VNET_LIST_RLOCK_NOSLEEP();
118         VNET_FOREACH(vnet_iter) {
119                 CURVNET_SET(vnet_iter);
120                 V_ip6_maxfragpackets = nmbclusters / 4;
121                 CURVNET_RESTORE();
122         }
123         VNET_LIST_RUNLOCK_NOSLEEP();
124 }
125
126 void
127 frag6_init(void)
128 {
129         struct ip6q *q6;
130         int i;
131
132         V_ip6_maxfragpackets = nmbclusters / 4;
133         for (i = 0; i < IP6REASS_NHASH; i++) {
134                 q6 = IP6Q_HEAD(i);
135                 q6->ip6q_next = q6->ip6q_prev = q6;
136                 mtx_init(&V_ip6q[i].lock, "ip6qlock", NULL, MTX_DEF);
137         }
138         V_ip6q_hashseed = arc4random();
139         if (!IS_DEFAULT_VNET(curvnet))
140                 return;
141
142         ip6_maxfrags = nmbclusters / 4;
143         EVENTHANDLER_REGISTER(nmbclusters_change,
144             frag6_change, NULL, EVENTHANDLER_PRI_ANY);
145 }
146
147 /*
148  * In RFC2460, fragment and reassembly rule do not agree with each other,
149  * in terms of next header field handling in fragment header.
150  * While the sender will use the same value for all of the fragmented packets,
151  * receiver is suggested not to check the consistency.
152  *
153  * fragment rule (p20):
154  *      (2) A Fragment header containing:
155  *      The Next Header value that identifies the first header of
156  *      the Fragmentable Part of the original packet.
157  *              -> next header field is same for all fragments
158  *
159  * reassembly rule (p21):
160  *      The Next Header field of the last header of the Unfragmentable
161  *      Part is obtained from the Next Header field of the first
162  *      fragment's Fragment header.
163  *              -> should grab it from the first fragment only
164  *
165  * The following note also contradicts with fragment rule - no one is going to
166  * send different fragment with different next header field.
167  *
168  * additional note (p22):
169  *      The Next Header values in the Fragment headers of different
170  *      fragments of the same original packet may differ.  Only the value
171  *      from the Offset zero fragment packet is used for reassembly.
172  *              -> should grab it from the first fragment only
173  *
174  * There is no explicit reason given in the RFC.  Historical reason maybe?
175  */
176 /*
177  * Fragment input
178  */
179 int
180 frag6_input(struct mbuf **mp, int *offp, int proto)
181 {
182         struct mbuf *m = *mp, *t;
183         struct ip6_hdr *ip6;
184         struct ip6_frag *ip6f;
185         struct ip6q *head, *q6;
186         struct ip6asfrag *af6, *ip6af, *af6dwn;
187         struct in6_ifaddr *ia;
188         int offset = *offp, nxt, i, next;
189         int first_frag = 0;
190         int fragoff, frgpartlen;        /* must be larger than u_int16_t */
191         uint32_t hash, hashkey[sizeof(struct in6_addr) * 2 + 1], *hashkeyp;
192         struct ifnet *dstifp;
193         u_int8_t ecn, ecn0;
194 #ifdef RSS
195         struct m_tag *mtag;
196         struct ip6_direct_ctx *ip6dc;
197 #endif
198
199 #if 0
200         char ip6buf[INET6_ADDRSTRLEN];
201 #endif
202
203         ip6 = mtod(m, struct ip6_hdr *);
204 #ifndef PULLDOWN_TEST
205         IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
206         ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
207 #else
208         IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
209         if (ip6f == NULL)
210                 return (IPPROTO_DONE);
211 #endif
212
213         dstifp = NULL;
214         /* find the destination interface of the packet. */
215         ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
216         if (ia != NULL) {
217                 dstifp = ia->ia_ifp;
218                 ifa_free(&ia->ia_ifa);
219         }
220         /* jumbo payload can't contain a fragment header */
221         if (ip6->ip6_plen == 0) {
222                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
223                 in6_ifstat_inc(dstifp, ifs6_reass_fail);
224                 return IPPROTO_DONE;
225         }
226
227         /*
228          * check whether fragment packet's fragment length is
229          * multiple of 8 octets.
230          * sizeof(struct ip6_frag) == 8
231          * sizeof(struct ip6_hdr) = 40
232          */
233         if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
234             (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
235                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
236                     offsetof(struct ip6_hdr, ip6_plen));
237                 in6_ifstat_inc(dstifp, ifs6_reass_fail);
238                 return IPPROTO_DONE;
239         }
240
241         IP6STAT_INC(ip6s_fragments);
242         in6_ifstat_inc(dstifp, ifs6_reass_reqd);
243
244         /* offset now points to data portion */
245         offset += sizeof(struct ip6_frag);
246
247         /*
248          * RFC 6946: Handle "atomic" fragments (offset and m bit set to 0)
249          * upfront, unrelated to any reassembly.  Just skip the fragment header.
250          */
251         if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
252                 /* XXX-BZ we want dedicated counters for this. */
253                 IP6STAT_INC(ip6s_reassembled);
254                 in6_ifstat_inc(dstifp, ifs6_reass_ok);
255                 *offp = offset;
256                 m->m_flags |= M_FRAGMENTED;
257                 return (ip6f->ip6f_nxt);
258         }
259
260         hashkeyp = hashkey;
261         memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
262         hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
263         memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
264         hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
265         *hashkeyp = ip6f->ip6f_ident;
266         hash = jenkins_hash32(hashkey, nitems(hashkey), V_ip6q_hashseed);
267         hash &= IP6REASS_HMASK;
268         head = IP6Q_HEAD(hash);
269         IP6Q_LOCK(hash);
270
271         /*
272          * Enforce upper bound on number of fragments.
273          * If maxfrag is 0, never accept fragments.
274          * If maxfrag is -1, accept all fragments without limitation.
275          */
276         if (ip6_maxfrags < 0)
277                 ;
278         else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
279                 goto dropfrag;
280
281         for (q6 = head->ip6q_next; q6 != head; q6 = q6->ip6q_next)
282                 if (ip6f->ip6f_ident == q6->ip6q_ident &&
283                     IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
284                     IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
285 #ifdef MAC
286                     && mac_ip6q_match(m, q6)
287 #endif
288                     )
289                         break;
290
291         if (q6 == head) {
292                 /*
293                  * the first fragment to arrive, create a reassembly queue.
294                  */
295                 first_frag = 1;
296
297                 /*
298                  * Enforce upper bound on number of fragmented packets
299                  * for which we attempt reassembly;
300                  * If maxfragpackets is 0, never accept fragments.
301                  * If maxfragpackets is -1, accept all fragments without
302                  * limitation.
303                  */
304                 if (V_ip6_maxfragpackets < 0)
305                         ;
306                 else if (atomic_load_int(&V_frag6_nfragpackets) >=
307                     (u_int)V_ip6_maxfragpackets)
308                         goto dropfrag;
309                 atomic_add_int(&V_frag6_nfragpackets, 1);
310                 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
311                     M_NOWAIT);
312                 if (q6 == NULL)
313                         goto dropfrag;
314                 bzero(q6, sizeof(*q6));
315 #ifdef MAC
316                 if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
317                         free(q6, M_FTABLE);
318                         goto dropfrag;
319                 }
320                 mac_ip6q_create(m, q6);
321 #endif
322                 frag6_insque_head(q6, head, hash);
323
324                 /* ip6q_nxt will be filled afterwards, from 1st fragment */
325                 q6->ip6q_down   = q6->ip6q_up = (struct ip6asfrag *)q6;
326 #ifdef notyet
327                 q6->ip6q_nxtp   = (u_char *)nxtp;
328 #endif
329                 q6->ip6q_ident  = ip6f->ip6f_ident;
330                 q6->ip6q_ttl    = IPV6_FRAGTTL;
331                 q6->ip6q_src    = ip6->ip6_src;
332                 q6->ip6q_dst    = ip6->ip6_dst;
333                 q6->ip6q_ecn    =
334                     (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
335                 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
336
337                 q6->ip6q_nfrag = 0;
338         }
339
340         /*
341          * If it's the 1st fragment, record the length of the
342          * unfragmentable part and the next header of the fragment header.
343          */
344         fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
345         if (fragoff == 0) {
346                 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
347                     sizeof(struct ip6_frag);
348                 q6->ip6q_nxt = ip6f->ip6f_nxt;
349         }
350
351         /*
352          * Check that the reassembled packet would not exceed 65535 bytes
353          * in size.
354          * If it would exceed, discard the fragment and return an ICMP error.
355          */
356         frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
357         if (q6->ip6q_unfrglen >= 0) {
358                 /* The 1st fragment has already arrived. */
359                 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
360                         icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
361                             offset - sizeof(struct ip6_frag) +
362                             offsetof(struct ip6_frag, ip6f_offlg));
363                         IP6Q_UNLOCK(hash);
364                         return (IPPROTO_DONE);
365                 }
366         } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
367                 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
368                     offset - sizeof(struct ip6_frag) +
369                     offsetof(struct ip6_frag, ip6f_offlg));
370                 IP6Q_UNLOCK(hash);
371                 return (IPPROTO_DONE);
372         }
373         /*
374          * If it's the first fragment, do the above check for each
375          * fragment already stored in the reassembly queue.
376          */
377         if (fragoff == 0) {
378                 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
379                      af6 = af6dwn) {
380                         af6dwn = af6->ip6af_down;
381
382                         if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
383                             IPV6_MAXPACKET) {
384                                 struct mbuf *merr = IP6_REASS_MBUF(af6);
385                                 struct ip6_hdr *ip6err;
386                                 int erroff = af6->ip6af_offset;
387
388                                 /* dequeue the fragment. */
389                                 frag6_deq(af6, hash);
390                                 free(af6, M_FTABLE);
391
392                                 /* adjust pointer. */
393                                 ip6err = mtod(merr, struct ip6_hdr *);
394
395                                 /*
396                                  * Restore source and destination addresses
397                                  * in the erroneous IPv6 header.
398                                  */
399                                 ip6err->ip6_src = q6->ip6q_src;
400                                 ip6err->ip6_dst = q6->ip6q_dst;
401
402                                 icmp6_error(merr, ICMP6_PARAM_PROB,
403                                     ICMP6_PARAMPROB_HEADER,
404                                     erroff - sizeof(struct ip6_frag) +
405                                     offsetof(struct ip6_frag, ip6f_offlg));
406                         }
407                 }
408         }
409
410         ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
411             M_NOWAIT);
412         if (ip6af == NULL)
413                 goto dropfrag;
414         bzero(ip6af, sizeof(*ip6af));
415         ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
416         ip6af->ip6af_off = fragoff;
417         ip6af->ip6af_frglen = frgpartlen;
418         ip6af->ip6af_offset = offset;
419         IP6_REASS_MBUF(ip6af) = m;
420
421         if (first_frag) {
422                 af6 = (struct ip6asfrag *)q6;
423                 goto insert;
424         }
425
426         /*
427          * Handle ECN by comparing this segment with the first one;
428          * if CE is set, do not lose CE.
429          * drop if CE and not-ECT are mixed for the same packet.
430          */
431         ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
432         ecn0 = q6->ip6q_ecn;
433         if (ecn == IPTOS_ECN_CE) {
434                 if (ecn0 == IPTOS_ECN_NOTECT) {
435                         free(ip6af, M_FTABLE);
436                         goto dropfrag;
437                 }
438                 if (ecn0 != IPTOS_ECN_CE)
439                         q6->ip6q_ecn = IPTOS_ECN_CE;
440         }
441         if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
442                 free(ip6af, M_FTABLE);
443                 goto dropfrag;
444         }
445
446         /*
447          * Find a segment which begins after this one does.
448          */
449         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
450              af6 = af6->ip6af_down)
451                 if (af6->ip6af_off > ip6af->ip6af_off)
452                         break;
453
454 #if 0
455         /*
456          * If there is a preceding segment, it may provide some of
457          * our data already.  If so, drop the data from the incoming
458          * segment.  If it provides all of our data, drop us.
459          */
460         if (af6->ip6af_up != (struct ip6asfrag *)q6) {
461                 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
462                         - ip6af->ip6af_off;
463                 if (i > 0) {
464                         if (i >= ip6af->ip6af_frglen)
465                                 goto dropfrag;
466                         m_adj(IP6_REASS_MBUF(ip6af), i);
467                         ip6af->ip6af_off += i;
468                         ip6af->ip6af_frglen -= i;
469                 }
470         }
471
472         /*
473          * While we overlap succeeding segments trim them or,
474          * if they are completely covered, dequeue them.
475          */
476         while (af6 != (struct ip6asfrag *)q6 &&
477                ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
478                 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
479                 if (i < af6->ip6af_frglen) {
480                         af6->ip6af_frglen -= i;
481                         af6->ip6af_off += i;
482                         m_adj(IP6_REASS_MBUF(af6), i);
483                         break;
484                 }
485                 af6 = af6->ip6af_down;
486                 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
487                 frag6_deq(af6->ip6af_up, hash);
488         }
489 #else
490         /*
491          * If the incoming framgent overlaps some existing fragments in
492          * the reassembly queue, drop it, since it is dangerous to override
493          * existing fragments from a security point of view.
494          * We don't know which fragment is the bad guy - here we trust
495          * fragment that came in earlier, with no real reason.
496          *
497          * Note: due to changes after disabling this part, mbuf passed to
498          * m_adj() below now does not meet the requirement.
499          */
500         if (af6->ip6af_up != (struct ip6asfrag *)q6) {
501                 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
502                         - ip6af->ip6af_off;
503                 if (i > 0) {
504 #if 0                           /* suppress the noisy log */
505                         log(LOG_ERR, "%d bytes of a fragment from %s "
506                             "overlaps the previous fragment\n",
507                             i, ip6_sprintf(ip6buf, &q6->ip6q_src));
508 #endif
509                         free(ip6af, M_FTABLE);
510                         goto dropfrag;
511                 }
512         }
513         if (af6 != (struct ip6asfrag *)q6) {
514                 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
515                 if (i > 0) {
516 #if 0                           /* suppress the noisy log */
517                         log(LOG_ERR, "%d bytes of a fragment from %s "
518                             "overlaps the succeeding fragment",
519                             i, ip6_sprintf(ip6buf, &q6->ip6q_src));
520 #endif
521                         free(ip6af, M_FTABLE);
522                         goto dropfrag;
523                 }
524         }
525 #endif
526
527 insert:
528 #ifdef MAC
529         if (!first_frag)
530                 mac_ip6q_update(m, q6);
531 #endif
532
533         /*
534          * Stick new segment in its place;
535          * check for complete reassembly.
536          * Move to front of packet queue, as we are
537          * the most recently active fragmented packet.
538          */
539         frag6_enq(ip6af, af6->ip6af_up, hash);
540         atomic_add_int(&frag6_nfrags, 1);
541         q6->ip6q_nfrag++;
542 #if 0 /* xxx */
543         if (q6 != head->ip6q_next) {
544                 frag6_remque(q6, hash);
545                 frag6_insque_head(q6, head, hash);
546         }
547 #endif
548         next = 0;
549         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
550              af6 = af6->ip6af_down) {
551                 if (af6->ip6af_off != next) {
552                         IP6Q_UNLOCK(hash);
553                         return IPPROTO_DONE;
554                 }
555                 next += af6->ip6af_frglen;
556         }
557         if (af6->ip6af_up->ip6af_mff) {
558                 IP6Q_UNLOCK(hash);
559                 return IPPROTO_DONE;
560         }
561
562         /*
563          * Reassembly is complete; concatenate fragments.
564          */
565         ip6af = q6->ip6q_down;
566         t = m = IP6_REASS_MBUF(ip6af);
567         af6 = ip6af->ip6af_down;
568         frag6_deq(ip6af, hash);
569         while (af6 != (struct ip6asfrag *)q6) {
570                 m->m_pkthdr.csum_flags &=
571                     IP6_REASS_MBUF(af6)->m_pkthdr.csum_flags;
572                 m->m_pkthdr.csum_data +=
573                     IP6_REASS_MBUF(af6)->m_pkthdr.csum_data;
574
575                 af6dwn = af6->ip6af_down;
576                 frag6_deq(af6, hash);
577                 while (t->m_next)
578                         t = t->m_next;
579                 m_adj(IP6_REASS_MBUF(af6), af6->ip6af_offset);
580                 m_demote_pkthdr(IP6_REASS_MBUF(af6));
581                 m_cat(t, IP6_REASS_MBUF(af6));
582                 free(af6, M_FTABLE);
583                 af6 = af6dwn;
584         }
585
586         while (m->m_pkthdr.csum_data & 0xffff0000)
587                 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
588                     (m->m_pkthdr.csum_data >> 16);
589
590         /* adjust offset to point where the original next header starts */
591         offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
592         free(ip6af, M_FTABLE);
593         ip6 = mtod(m, struct ip6_hdr *);
594         ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
595         if (q6->ip6q_ecn == IPTOS_ECN_CE)
596                 ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
597         nxt = q6->ip6q_nxt;
598 #ifdef notyet
599         *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
600 #endif
601
602         if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
603                 frag6_remque(q6, hash);
604                 atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
605 #ifdef MAC
606                 mac_ip6q_destroy(q6);
607 #endif
608                 free(q6, M_FTABLE);
609                 atomic_subtract_int(&V_frag6_nfragpackets, 1);
610
611                 goto dropfrag;
612         }
613
614         /*
615          * Store NXT to the original.
616          */
617         m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
618             (caddr_t)&nxt);
619
620         frag6_remque(q6, hash);
621         atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
622 #ifdef MAC
623         mac_ip6q_reassemble(q6, m);
624         mac_ip6q_destroy(q6);
625 #endif
626         free(q6, M_FTABLE);
627         atomic_subtract_int(&V_frag6_nfragpackets, 1);
628
629         if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
630                 int plen = 0;
631                 for (t = m; t; t = t->m_next)
632                         plen += t->m_len;
633                 m->m_pkthdr.len = plen;
634         }
635
636 #ifdef RSS
637         mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
638             M_NOWAIT);
639         if (mtag == NULL)
640                 goto dropfrag;
641
642         ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
643         ip6dc->ip6dc_nxt = nxt;
644         ip6dc->ip6dc_off = offset;
645
646         m_tag_prepend(m, mtag);
647 #endif
648
649         IP6Q_UNLOCK(hash);
650         IP6STAT_INC(ip6s_reassembled);
651         in6_ifstat_inc(dstifp, ifs6_reass_ok);
652
653 #ifdef RSS
654         /*
655          * Queue/dispatch for reprocessing.
656          */
657         netisr_dispatch(NETISR_IPV6_DIRECT, m);
658         return IPPROTO_DONE;
659 #endif
660
661         /*
662          * Tell launch routine the next header
663          */
664
665         *mp = m;
666         *offp = offset;
667
668         return nxt;
669
670  dropfrag:
671         IP6Q_UNLOCK(hash);
672         in6_ifstat_inc(dstifp, ifs6_reass_fail);
673         IP6STAT_INC(ip6s_fragdropped);
674         m_freem(m);
675         return IPPROTO_DONE;
676 }
677
678 /*
679  * Free a fragment reassembly header and all
680  * associated datagrams.
681  */
682 static void
683 frag6_freef(struct ip6q *q6, uint32_t bucket)
684 {
685         struct ip6asfrag *af6, *down6;
686
687         IP6Q_LOCK_ASSERT(bucket);
688
689         for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
690              af6 = down6) {
691                 struct mbuf *m = IP6_REASS_MBUF(af6);
692
693                 down6 = af6->ip6af_down;
694                 frag6_deq(af6, bucket);
695
696                 /*
697                  * Return ICMP time exceeded error for the 1st fragment.
698                  * Just free other fragments.
699                  */
700                 if (af6->ip6af_off == 0) {
701                         struct ip6_hdr *ip6;
702
703                         /* adjust pointer */
704                         ip6 = mtod(m, struct ip6_hdr *);
705
706                         /* restore source and destination addresses */
707                         ip6->ip6_src = q6->ip6q_src;
708                         ip6->ip6_dst = q6->ip6q_dst;
709
710                         icmp6_error(m, ICMP6_TIME_EXCEEDED,
711                                     ICMP6_TIME_EXCEED_REASSEMBLY, 0);
712                 } else
713                         m_freem(m);
714                 free(af6, M_FTABLE);
715         }
716         frag6_remque(q6, bucket);
717         atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
718 #ifdef MAC
719         mac_ip6q_destroy(q6);
720 #endif
721         free(q6, M_FTABLE);
722         atomic_subtract_int(&V_frag6_nfragpackets, 1);
723 }
724
725 /*
726  * Put an ip fragment on a reassembly chain.
727  * Like insque, but pointers in middle of structure.
728  */
729 static void
730 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
731     uint32_t bucket __unused)
732 {
733
734         IP6Q_LOCK_ASSERT(bucket);
735
736         af6->ip6af_up = up6;
737         af6->ip6af_down = up6->ip6af_down;
738         up6->ip6af_down->ip6af_up = af6;
739         up6->ip6af_down = af6;
740 }
741
742 /*
743  * To frag6_enq as remque is to insque.
744  */
745 static void
746 frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
747 {
748
749         IP6Q_LOCK_ASSERT(bucket);
750
751         af6->ip6af_up->ip6af_down = af6->ip6af_down;
752         af6->ip6af_down->ip6af_up = af6->ip6af_up;
753 }
754
755 static void
756 frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket __unused)
757 {
758
759         IP6Q_LOCK_ASSERT(bucket);
760         KASSERT(IP6Q_HEAD(bucket) == old,
761             ("%s: attempt to insert at head of wrong bucket"
762             " (bucket=%u, old=%p)", __func__, bucket, old));
763
764         new->ip6q_prev = old;
765         new->ip6q_next = old->ip6q_next;
766         old->ip6q_next->ip6q_prev= new;
767         old->ip6q_next = new;
768 }
769
770 static void
771 frag6_remque(struct ip6q *p6, uint32_t bucket __unused)
772 {
773
774         IP6Q_LOCK_ASSERT(bucket);
775
776         p6->ip6q_prev->ip6q_next = p6->ip6q_next;
777         p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
778 }
779
780 /*
781  * IPv6 reassembling timer processing;
782  * if a timer expires on a reassembly
783  * queue, discard it.
784  */
785 void
786 frag6_slowtimo(void)
787 {
788         VNET_ITERATOR_DECL(vnet_iter);
789         struct ip6q *head, *q6;
790         int i;
791
792         VNET_LIST_RLOCK_NOSLEEP();
793         VNET_FOREACH(vnet_iter) {
794                 CURVNET_SET(vnet_iter);
795                 for (i = 0; i < IP6REASS_NHASH; i++) {
796                         IP6Q_LOCK(i);
797                         head = IP6Q_HEAD(i);
798                         q6 = head->ip6q_next;
799                         if (q6)
800                                 while (q6 != head) {
801                                         --q6->ip6q_ttl;
802                                         q6 = q6->ip6q_next;
803                                         if (q6->ip6q_prev->ip6q_ttl == 0) {
804                                                 IP6STAT_INC(ip6s_fragtimeout);
805                                                 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
806                                                 frag6_freef(q6->ip6q_prev, i);
807                                         }
808                                 }
809                         /*
810                          * If we are over the maximum number of fragments
811                          * (due to the limit being lowered), drain off
812                          * enough to get down to the new limit.
813                          */
814                         while (atomic_load_int(&V_frag6_nfragpackets) >
815                             (u_int)V_ip6_maxfragpackets &&
816                             head->ip6q_prev != head) {
817                                 IP6STAT_INC(ip6s_fragoverflow);
818                                 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
819                                 frag6_freef(head->ip6q_prev, i);
820                         }
821                         IP6Q_UNLOCK(i);
822                 }
823                 CURVNET_RESTORE();
824         }
825         VNET_LIST_RUNLOCK_NOSLEEP();
826 }
827
828 /*
829  * Drain off all datagram fragments.
830  */
831 void
832 frag6_drain(void)
833 {
834         VNET_ITERATOR_DECL(vnet_iter);
835         struct ip6q *head;
836         int i;
837
838         VNET_LIST_RLOCK_NOSLEEP();
839         VNET_FOREACH(vnet_iter) {
840                 CURVNET_SET(vnet_iter);
841                 for (i = 0; i < IP6REASS_NHASH; i++) {
842                         if (IP6Q_TRYLOCK(i) == 0)
843                                 continue;
844                         head = IP6Q_HEAD(i);
845                         while (head->ip6q_next != head) {
846                                 IP6STAT_INC(ip6s_fragdropped);
847                                 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
848                                 frag6_freef(head->ip6q_next, i);
849                         }
850                         IP6Q_UNLOCK(i);
851                 }
852                 CURVNET_RESTORE();
853         }
854         VNET_LIST_RUNLOCK_NOSLEEP();
855 }
856
857 int
858 ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
859 {
860         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
861         struct mbuf *t;
862
863         /* Delete frag6 header. */
864         if (m->m_len >= offset + sizeof(struct ip6_frag)) {
865                 /* This is the only possible case with !PULLDOWN_TEST. */
866                 bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
867                     offset);
868                 m->m_data += sizeof(struct ip6_frag);
869                 m->m_len -= sizeof(struct ip6_frag);
870         } else {
871                 /* This comes with no copy if the boundary is on cluster. */
872                 if ((t = m_split(m, offset, wait)) == NULL)
873                         return (ENOMEM);
874                 m_adj(t, sizeof(struct ip6_frag));
875                 m_cat(m, t);
876         }
877
878         m->m_flags |= M_FRAGMENTED;
879         return (0);
880 }