summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/netinet6/frag6.c
blob: 6f16c7129b5d7cbbac071b4e49802743ed3e5e26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
#include <machine/rtems-bsd-kernel-space.h>

/*-
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <rtems/bsd/local/opt_rss.h>

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/domain.h>
#include <sys/eventhandler.h>
#include <sys/hash.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>

#include <net/if.h>
#include <net/if_var.h>
#include <net/netisr.h>
#include <net/route.h>
#include <net/vnet.h>

#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet/icmp6.h>
#include <netinet/in_systm.h>	/* For ECN definitions. */
#include <netinet/ip.h>		/* For ECN definitions. */

#ifdef MAC
#include <security/mac/mac_framework.h>
#endif

/* Reassembly headers are stored in hash buckets. */
#define	IP6REASS_NHASH_LOG2	10
#define	IP6REASS_NHASH		(1 << IP6REASS_NHASH_LOG2)
#define	IP6REASS_HMASK		(IP6REASS_NHASH - 1)

static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *,
    uint32_t bucket __unused);
static void frag6_deq(struct ip6asfrag *, uint32_t bucket __unused);
static void frag6_insque_head(struct ip6q *, struct ip6q *,
    uint32_t bucket);
static void frag6_remque(struct ip6q *, uint32_t bucket);
static void frag6_freef(struct ip6q *, uint32_t bucket);

struct ip6qbucket {
	struct ip6q	ip6q;
	struct mtx	lock;
	int		count;
};

struct	ip6asfrag {
	struct ip6asfrag *ip6af_down;
	struct ip6asfrag *ip6af_up;
	struct mbuf	*ip6af_m;
	int		ip6af_offset;	/* offset in ip6af_m to next header */
	int		ip6af_frglen;	/* fragmentable part length */
	int		ip6af_off;	/* fragment offset */
	u_int16_t	ip6af_mff;	/* more fragment bit in frag off */
};

#define IP6_REASS_MBUF(ip6af) (*(struct mbuf **)&((ip6af)->ip6af_m))

static MALLOC_DEFINE(M_FRAG6, "frag6", "IPv6 fragment reassembly header");

/* System wide (global) maximum and count of packets in reassembly queues. */ 
static int ip6_maxfrags;
static volatile u_int frag6_nfrags = 0;

/* Maximum and current packets in per-VNET reassembly queue. */
VNET_DEFINE_STATIC(int,			ip6_maxfragpackets);
VNET_DEFINE_STATIC(volatile u_int,	frag6_nfragpackets);
#define	V_ip6_maxfragpackets		VNET(ip6_maxfragpackets)
#define	V_frag6_nfragpackets		VNET(frag6_nfragpackets)

/* Maximum per-VNET reassembly queues per bucket and fragments per packet. */
VNET_DEFINE_STATIC(int,			ip6_maxfragbucketsize);
VNET_DEFINE_STATIC(int,			ip6_maxfragsperpacket);
#define	V_ip6_maxfragbucketsize		VNET(ip6_maxfragbucketsize)
#define	V_ip6_maxfragsperpacket		VNET(ip6_maxfragsperpacket)

/* Per-VNET reassembly queue buckets. */
VNET_DEFINE_STATIC(struct ip6qbucket,	ip6qb[IP6REASS_NHASH]);
VNET_DEFINE_STATIC(uint32_t,		ip6qb_hashseed);
#define	V_ip6qb				VNET(ip6qb)
#define	V_ip6qb_hashseed		VNET(ip6qb_hashseed)

#define	IP6QB_LOCK(_b)		mtx_lock(&V_ip6qb[(_b)].lock)
#define	IP6QB_TRYLOCK(_b)	mtx_trylock(&V_ip6qb[(_b)].lock)
#define	IP6QB_LOCK_ASSERT(_b)	mtx_assert(&V_ip6qb[(_b)].lock, MA_OWNED)
#define	IP6QB_UNLOCK(_b)	mtx_unlock(&V_ip6qb[(_b)].lock)
#define	IP6QB_HEAD(_b)		(&V_ip6qb[(_b)].ip6q)

/*
 * By default, limit the number of IP6 fragments across all reassembly
 * queues to  1/32 of the total number of mbuf clusters.
 *
 * Limit the total number of reassembly queues per VNET to the
 * IP6 fragment limit, but ensure the limit will not allow any bucket
 * to grow above 100 items. (The bucket limit is
 * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct
 * multiplier to reach a 100-item limit.)
 * The 100-item limit was chosen as brief testing seems to show that
 * this produces "reasonable" performance on some subset of systems
 * under DoS attack.
 */
#define	IP6_MAXFRAGS		(nmbclusters / 32)
#define	IP6_MAXFRAGPACKETS	(imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))


/*
 * Sysctls and helper function.
 */
SYSCTL_DECL(_net_inet6_ip6);

static void
frag6_set_bucketsize(void)
{
	int i;

	if ((i = V_ip6_maxfragpackets) > 0)
		V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
}

SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags,
	CTLFLAG_RW, &ip6_maxfrags, 0,
	"Maximum allowed number of outstanding IPv6 packet fragments. "
	"A value of 0 means no fragmented packets will be accepted, while a "
	"a value of -1 means no limit");

static int
sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS)
{
	int error, val;

	val = V_ip6_maxfragpackets;
	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error != 0 || !req->newptr)
		return (error);
	V_ip6_maxfragpackets = val;
	frag6_set_bucketsize();
	return (0);
}
SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets,
	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
	sysctl_ip6_maxfragpackets, "I",
	"Default maximum number of outstanding fragmented IPv6 packets. "
	"A value of 0 means no fragmented packets will be accepted, while a "
	"a value of -1 means no limit");
SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGSPERPACKET, maxfragsperpacket,
	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragsperpacket), 0,
	"Maximum allowed number of fragments per packet");
SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGBUCKETSIZE, maxfragbucketsize,
	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragbucketsize), 0,
	"Maximum number of reassembly queues per hash bucket");


/*
 * Remove the IPv6 fragmentation header from the mbuf.
 */
int
ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
{
	struct ip6_hdr *ip6;
	struct mbuf *t;

	/* Delete frag6 header. */
	if (m->m_len >= offset + sizeof(struct ip6_frag)) {

		/* This is the only possible case with !PULLDOWN_TEST. */
		ip6  = mtod(m, struct ip6_hdr *);
		bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
		    offset);
		m->m_data += sizeof(struct ip6_frag);
		m->m_len -= sizeof(struct ip6_frag);
	} else {

		/* This comes with no copy if the boundary is on cluster. */
		if ((t = m_split(m, offset, wait)) == NULL)
			return (ENOMEM);
		m_adj(t, sizeof(struct ip6_frag));
		m_cat(m, t);
	}

	m->m_flags |= M_FRAGMENTED;
	return (0);
}

/*
 * Free a fragment reassembly header and all associated datagrams.
 */
static void
frag6_freef(struct ip6q *q6, uint32_t bucket)
{
	struct ip6_hdr *ip6;
	struct ip6asfrag *af6, *down6;
	struct mbuf *m;

	IP6QB_LOCK_ASSERT(bucket);

	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
	     af6 = down6) {

		m = IP6_REASS_MBUF(af6);
		down6 = af6->ip6af_down;
		frag6_deq(af6, bucket);

		/*
		 * Return ICMP time exceeded error for the 1st fragment.
		 * Just free other fragments.
		 */
		if (af6->ip6af_off == 0) {

			/* Adjust pointer. */
			ip6 = mtod(m, struct ip6_hdr *);

			/* Restore source and destination addresses. */
			ip6->ip6_src = q6->ip6q_src;
			ip6->ip6_dst = q6->ip6q_dst;

			icmp6_error(m, ICMP6_TIME_EXCEEDED,
			    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
		} else
			m_freem(m);

		free(af6, M_FRAG6);
	}
	frag6_remque(q6, bucket);
	atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
#ifdef MAC
	mac_ip6q_destroy(q6);
#endif
	free(q6, M_FRAG6);
	atomic_subtract_int(&V_frag6_nfragpackets, 1);
}

/*
 * Like in RFC2460, in RFC8200, fragment and reassembly rules do not agree with
 * each other, in terms of next header field handling in fragment header.
 * While the sender will use the same value for all of the fragmented packets,
 * receiver is suggested not to check for consistency.
 *
 * Fragment rules (p18,p19):
 *	(2)  A Fragment header containing:
 *	The Next Header value that identifies the first header
 *	after the Per-Fragment headers of the original packet.
 *		-> next header field is same for all fragments
 *
 * Reassembly rule (p20):
 *	The Next Header field of the last header of the Per-Fragment
 *	headers is obtained from the Next Header field of the first
 *	fragment's Fragment header.
 *		-> should grab it from the first fragment only
 *
 * The following note also contradicts with fragment rule - no one is going to
 * send different fragment with different next header field.
 *
 * Additional note (p22) [not an error]:
 *	The Next Header values in the Fragment headers of different
 *	fragments of the same original packet may differ.  Only the value
 *	from the Offset zero fragment packet is used for reassembly.
 *		-> should grab it from the first fragment only
 *
 * There is no explicit reason given in the RFC.  Historical reason maybe?
 */
/*
 * Fragment input.
 */
int
frag6_input(struct mbuf **mp, int *offp, int proto)
{
	struct ifnet *dstifp;
	struct in6_ifaddr *ia6;
	struct ip6_hdr *ip6;
	struct ip6_frag *ip6f;
	struct ip6q *head, *q6;
	struct ip6asfrag *af6, *af6dwn, *ip6af;
	struct mbuf *m, *t;
	uint32_t hashkey[(sizeof(struct in6_addr) * 2 +
		    sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)];
	uint32_t bucket, *hashkeyp;
	int fragoff, frgpartlen;	/* Must be larger than uint16_t. */
	int nxt, offset, plen;
	uint8_t ecn, ecn0;
	bool only_frag;
#ifdef RSS
	struct ip6_direct_ctx *ip6dc;
	struct m_tag *mtag;
#endif

	m = *mp;
	offset = *offp;

	ip6 = mtod(m, struct ip6_hdr *);
#ifndef PULLDOWN_TEST
	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
#else
	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
	if (ip6f == NULL)
		return (IPPROTO_DONE);
#endif

	dstifp = NULL;
	/* Find the destination interface of the packet. */
	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
	if (ia6 != NULL) {
		dstifp = ia6->ia_ifp;
		ifa_free(&ia6->ia_ifa);
	}

	/* Jumbo payload cannot contain a fragment header. */
	if (ip6->ip6_plen == 0) {
		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
		in6_ifstat_inc(dstifp, ifs6_reass_fail);
		return (IPPROTO_DONE);
	}

	/*
	 * Check whether fragment packet's fragment length is a
	 * multiple of 8 octets (unless it is the last one).
	 * sizeof(struct ip6_frag) == 8
	 * sizeof(struct ip6_hdr) = 40
	 */
	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
		    offsetof(struct ip6_hdr, ip6_plen));
		in6_ifstat_inc(dstifp, ifs6_reass_fail);
		return (IPPROTO_DONE);
	}

	IP6STAT_INC(ip6s_fragments);
	in6_ifstat_inc(dstifp, ifs6_reass_reqd);

	/* Offset now points to data portion. */
	offset += sizeof(struct ip6_frag);

	/*
	 * Handle "atomic" fragments (offset and m bit set to 0) upfront,
	 * unrelated to any reassembly.  Still need to remove the frag hdr.
	 * See RFC 6946 and section 4.5 of RFC 8200.
	 */
	if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
		IP6STAT_INC(ip6s_atomicfrags);
		/* XXX-BZ handle correctly. */
		in6_ifstat_inc(dstifp, ifs6_reass_ok);
		*offp = offset;
		m->m_flags |= M_FRAGMENTED;
		return (ip6f->ip6f_nxt);
	}

	/* Get fragment length and discard 0-byte fragments. */
	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
	if (frgpartlen == 0) {
		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
		    offsetof(struct ip6_hdr, ip6_plen));
		in6_ifstat_inc(dstifp, ifs6_reass_fail);
		IP6STAT_INC(ip6s_fragdropped);
		return (IPPROTO_DONE);
	}

	/* Generate a hash value for fragment bucket selection. */
	hashkeyp = hashkey;
	memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
	hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
	memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
	hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
	*hashkeyp = ip6f->ip6f_ident;
	bucket = jenkins_hash32(hashkey, nitems(hashkey), V_ip6qb_hashseed);
	bucket &= IP6REASS_HMASK;
	head = IP6QB_HEAD(bucket);
	IP6QB_LOCK(bucket);

	/*
	 * Enforce upper bound on number of fragments for the entire system.
	 * If maxfrag is 0, never accept fragments.
	 * If maxfrag is -1, accept all fragments without limitation.
	 */
	if (ip6_maxfrags < 0)
		;
	else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
		goto dropfrag;

	for (q6 = head->ip6q_next; q6 != head; q6 = q6->ip6q_next)
		if (ip6f->ip6f_ident == q6->ip6q_ident &&
		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
#ifdef MAC
		    && mac_ip6q_match(m, q6)
#endif
		    )
			break;

	only_frag = false;
	if (q6 == head) {

		/* A first fragment to arrive creates a reassembly queue. */
		only_frag = true;

		/*
		 * Enforce upper bound on number of fragmented packets
		 * for which we attempt reassembly;
		 * If maxfragpackets is 0, never accept fragments.
		 * If maxfragpackets is -1, accept all fragments without
		 * limitation.
		 */
		if (V_ip6_maxfragpackets < 0)
			;
		else if (V_ip6qb[bucket].count >= V_ip6_maxfragbucketsize ||
		    atomic_load_int(&V_frag6_nfragpackets) >=
		    (u_int)V_ip6_maxfragpackets)
			goto dropfrag;
		atomic_add_int(&V_frag6_nfragpackets, 1);

		/* Allocate IPv6 fragement packet queue entry. */
		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FRAG6,
		    M_NOWAIT | M_ZERO);
		if (q6 == NULL)
			goto dropfrag;
#ifdef MAC
		if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
			free(q6, M_FRAG6);
			goto dropfrag;
		}
		mac_ip6q_create(m, q6);
#endif
		frag6_insque_head(q6, head, bucket);

		/* ip6q_nxt will be filled afterwards, from 1st fragment. */
		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
#ifdef notyet
		q6->ip6q_nxtp	= (u_char *)nxtp;
#endif
		q6->ip6q_ident	= ip6f->ip6f_ident;
		q6->ip6q_ttl	= IPV6_FRAGTTL;
		q6->ip6q_src	= ip6->ip6_src;
		q6->ip6q_dst	= ip6->ip6_dst;
		q6->ip6q_ecn	=
		    (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */

		q6->ip6q_nfrag = 0;
	}

	/*
	 * If it is the 1st fragment, record the length of the
	 * unfragmentable part and the next header of the fragment header.
	 */
	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
	if (fragoff == 0) {
		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
		    sizeof(struct ip6_frag);
		q6->ip6q_nxt = ip6f->ip6f_nxt;
	}

	/*
	 * Check that the reassembled packet would not exceed 65535 bytes
	 * in size.
	 * If it would exceed, discard the fragment and return an ICMP error.
	 */
	if (q6->ip6q_unfrglen >= 0) {
		/* The 1st fragment has already arrived. */
		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
			    offset - sizeof(struct ip6_frag) +
			    offsetof(struct ip6_frag, ip6f_offlg));
			IP6QB_UNLOCK(bucket);
			return (IPPROTO_DONE);
		}
	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
		    offset - sizeof(struct ip6_frag) +
		    offsetof(struct ip6_frag, ip6f_offlg));
		IP6QB_UNLOCK(bucket);
		return (IPPROTO_DONE);
	}
	/*
	 * If it is the first fragment, do the above check for each
	 * fragment already stored in the reassembly queue.
	 */
	if (fragoff == 0) {
		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
		     af6 = af6dwn) {
			af6dwn = af6->ip6af_down;

			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
			    IPV6_MAXPACKET) {
				struct ip6_hdr *ip6err;
				struct mbuf *merr;
				int erroff;

				merr = IP6_REASS_MBUF(af6);
				erroff = af6->ip6af_offset;

				/* Dequeue the fragment. */
				frag6_deq(af6, bucket);
				free(af6, M_FRAG6);

				/* Adjust pointer. */
				ip6err = mtod(merr, struct ip6_hdr *);

				/*
				 * Restore source and destination addresses
				 * in the erroneous IPv6 header.
				 */
				ip6err->ip6_src = q6->ip6q_src;
				ip6err->ip6_dst = q6->ip6q_dst;

				icmp6_error(merr, ICMP6_PARAM_PROB,
				    ICMP6_PARAMPROB_HEADER,
				    erroff - sizeof(struct ip6_frag) +
				    offsetof(struct ip6_frag, ip6f_offlg));
			}
		}
	}

	/* Allocate an IPv6 fragement queue entry for this fragmented part. */
	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FRAG6,
	    M_NOWAIT | M_ZERO);
	if (ip6af == NULL)
		goto dropfrag;
	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
	ip6af->ip6af_off = fragoff;
	ip6af->ip6af_frglen = frgpartlen;
	ip6af->ip6af_offset = offset;
	IP6_REASS_MBUF(ip6af) = m;

	if (only_frag) {
		af6 = (struct ip6asfrag *)q6;
		goto insert;
	}

	/* Do duplicate, condition, and boundry checks. */
	/*
	 * Handle ECN by comparing this segment with the first one;
	 * if CE is set, do not lose CE.
	 * Drop if CE and not-ECT are mixed for the same packet.
	 */
	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
	ecn0 = q6->ip6q_ecn;
	if (ecn == IPTOS_ECN_CE) {
		if (ecn0 == IPTOS_ECN_NOTECT) {
			free(ip6af, M_FRAG6);
			goto dropfrag;
		}
		if (ecn0 != IPTOS_ECN_CE)
			q6->ip6q_ecn = IPTOS_ECN_CE;
	}
	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
		free(ip6af, M_FRAG6);
		goto dropfrag;
	}

	/* Find a fragmented part which begins after this one does. */
	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
	     af6 = af6->ip6af_down)
		if (af6->ip6af_off > ip6af->ip6af_off)
			break;

	/*
	 * If the incoming framgent overlaps some existing fragments in
	 * the reassembly queue, drop both the new fragment and the
	 * entire reassembly queue.  However, if the new fragment
	 * is an exact duplicate of an existing fragment, only silently
	 * drop the existing fragment and leave the fragmentation queue
	 * unchanged, as allowed by the RFC.  (RFC 8200, 4.5)
	 */
	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
		if (af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen -
		    ip6af->ip6af_off > 0) {
			free(ip6af, M_FRAG6);
			goto dropfrag;
		}
	}
	if (af6 != (struct ip6asfrag *)q6) {
		if (ip6af->ip6af_off + ip6af->ip6af_frglen -
		    af6->ip6af_off > 0) {
			free(ip6af, M_FRAG6);
			goto dropfrag;
		}
	}

insert:
#ifdef MAC
	if (!only_frag)
		mac_ip6q_update(m, q6);
#endif

	/*
	 * Stick new segment in its place; check for complete reassembly.
	 * If not complete, check fragment limit.  Move to front of packet
	 * queue, as we are the most recently active fragmented packet.
	 */
	frag6_enq(ip6af, af6->ip6af_up, bucket);
	atomic_add_int(&frag6_nfrags, 1);
	q6->ip6q_nfrag++;
	plen = 0;
	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
	     af6 = af6->ip6af_down) {
		if (af6->ip6af_off != plen) {
			if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
				IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
				frag6_freef(q6, bucket);
			}
			IP6QB_UNLOCK(bucket);
			return (IPPROTO_DONE);
		}
		plen += af6->ip6af_frglen;
	}
	if (af6->ip6af_up->ip6af_mff) {
		if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
			IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
			frag6_freef(q6, bucket);
		}
		IP6QB_UNLOCK(bucket);
		return (IPPROTO_DONE);
	}

	/* Reassembly is complete; concatenate fragments. */
	ip6af = q6->ip6q_down;
	t = m = IP6_REASS_MBUF(ip6af);
	af6 = ip6af->ip6af_down;
	frag6_deq(ip6af, bucket);
	while (af6 != (struct ip6asfrag *)q6) {
		m->m_pkthdr.csum_flags &=
		    IP6_REASS_MBUF(af6)->m_pkthdr.csum_flags;
		m->m_pkthdr.csum_data +=
		    IP6_REASS_MBUF(af6)->m_pkthdr.csum_data;

		af6dwn = af6->ip6af_down;
		frag6_deq(af6, bucket);
		while (t->m_next)
			t = t->m_next;
		m_adj(IP6_REASS_MBUF(af6), af6->ip6af_offset);
		m_demote_pkthdr(IP6_REASS_MBUF(af6));
		m_cat(t, IP6_REASS_MBUF(af6));
		free(af6, M_FRAG6);
		af6 = af6dwn;
	}

	while (m->m_pkthdr.csum_data & 0xffff0000)
		m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
		    (m->m_pkthdr.csum_data >> 16);

	/* Adjust offset to point where the original next header starts. */
	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
	free(ip6af, M_FRAG6);
	ip6 = mtod(m, struct ip6_hdr *);
	ip6->ip6_plen = htons((u_short)plen + offset - sizeof(struct ip6_hdr));
	if (q6->ip6q_ecn == IPTOS_ECN_CE)
		ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
	nxt = q6->ip6q_nxt;

	if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
		frag6_remque(q6, bucket);
		atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
#ifdef MAC
		mac_ip6q_destroy(q6);
#endif
		free(q6, M_FRAG6);
		atomic_subtract_int(&V_frag6_nfragpackets, 1);

		goto dropfrag;
	}

	/* Set nxt(-hdr field value) to the original value. */
	m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
	    (caddr_t)&nxt);

	frag6_remque(q6, bucket);
	atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
#ifdef MAC
	mac_ip6q_reassemble(q6, m);
	mac_ip6q_destroy(q6);
#endif
	free(q6, M_FRAG6);
	atomic_subtract_int(&V_frag6_nfragpackets, 1);

	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */

		plen = 0;
		for (t = m; t; t = t->m_next)
			plen += t->m_len;
		m->m_pkthdr.len = plen;
	}

#ifdef RSS
	mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
	    M_NOWAIT);
	if (mtag == NULL)
		goto dropfrag;

	ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
	ip6dc->ip6dc_nxt = nxt;
	ip6dc->ip6dc_off = offset;

	m_tag_prepend(m, mtag);
#endif

	IP6QB_UNLOCK(bucket);
	IP6STAT_INC(ip6s_reassembled);
	in6_ifstat_inc(dstifp, ifs6_reass_ok);

#ifdef RSS
	/* Queue/dispatch for reprocessing. */
	netisr_dispatch(NETISR_IPV6_DIRECT, m);
	return (IPPROTO_DONE);
#endif

	/* Tell launch routine the next header. */
	*mp = m;
	*offp = offset;

	return (nxt);

dropfrag:
	IP6QB_UNLOCK(bucket);
	in6_ifstat_inc(dstifp, ifs6_reass_fail);
	IP6STAT_INC(ip6s_fragdropped);
	m_freem(m);
	return (IPPROTO_DONE);
}

/*
 * IPv6 reassembling timer processing;
 * if a timer expires on a reassembly queue, discard it.
 */
void
frag6_slowtimo(void)
{
	VNET_ITERATOR_DECL(vnet_iter);
	struct ip6q *head, *q6;
	uint32_t bucket;

	VNET_LIST_RLOCK_NOSLEEP();
	VNET_FOREACH(vnet_iter) {
		CURVNET_SET(vnet_iter);
		for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
			IP6QB_LOCK(bucket);
			head = IP6QB_HEAD(bucket);
			q6 = head->ip6q_next;
			if (q6 == NULL) {
				/*
				 * XXXJTL: This should never happen. This
				 * should turn into an assertion.
				 */
				IP6QB_UNLOCK(bucket);
				continue;
			}
			while (q6 != head) {
				--q6->ip6q_ttl;
				q6 = q6->ip6q_next;
				if (q6->ip6q_prev->ip6q_ttl == 0) {
					IP6STAT_ADD(ip6s_fragtimeout,
						q6->ip6q_prev->ip6q_nfrag);
					/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
					frag6_freef(q6->ip6q_prev, bucket);
				}
			}
			/*
			 * If we are over the maximum number of fragments
			 * (due to the limit being lowered), drain off
			 * enough to get down to the new limit.
			 * Note that we drain all reassembly queues if
			 * maxfragpackets is 0 (fragmentation is disabled),
			 * and do not enforce a limit when maxfragpackets
			 * is negative.
			 */
			while ((V_ip6_maxfragpackets == 0 ||
			    (V_ip6_maxfragpackets > 0 &&
			    V_ip6qb[bucket].count > V_ip6_maxfragbucketsize)) &&
			    head->ip6q_prev != head) {
				IP6STAT_ADD(ip6s_fragoverflow,
					q6->ip6q_prev->ip6q_nfrag);
				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
				frag6_freef(head->ip6q_prev, bucket);
			}
			IP6QB_UNLOCK(bucket);
		}
		/*
		 * If we are still over the maximum number of fragmented
		 * packets, drain off enough to get down to the new limit.
		 */
		bucket = 0;
		while (V_ip6_maxfragpackets >= 0 &&
		    atomic_load_int(&V_frag6_nfragpackets) >
		    (u_int)V_ip6_maxfragpackets) {
			IP6QB_LOCK(bucket);
			head = IP6QB_HEAD(bucket);
			if (head->ip6q_prev != head) {
				IP6STAT_ADD(ip6s_fragoverflow,
					q6->ip6q_prev->ip6q_nfrag);
				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
				frag6_freef(head->ip6q_prev, bucket);
			}
			IP6QB_UNLOCK(bucket);
			bucket = (bucket + 1) % IP6REASS_NHASH;
		}
		CURVNET_RESTORE();
	}
	VNET_LIST_RUNLOCK_NOSLEEP();
}

/*
 * Eventhandler to adjust limits in case nmbclusters change.
 */
static void
frag6_change(void *tag)
{
	VNET_ITERATOR_DECL(vnet_iter);

	ip6_maxfrags = IP6_MAXFRAGS;
	VNET_LIST_RLOCK_NOSLEEP();
	VNET_FOREACH(vnet_iter) {
		CURVNET_SET(vnet_iter);
		V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
		frag6_set_bucketsize();
		CURVNET_RESTORE();
	}
	VNET_LIST_RUNLOCK_NOSLEEP();
}

/*
 * Initialise reassembly queue and fragment identifier.
 */
void
frag6_init(void)
{
	struct ip6q *q6;
	uint32_t bucket;

	V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
	frag6_set_bucketsize();
	for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
		q6 = IP6QB_HEAD(bucket);
		q6->ip6q_next = q6->ip6q_prev = q6;
		mtx_init(&V_ip6qb[bucket].lock, "ip6qlock", NULL, MTX_DEF);
		V_ip6qb[bucket].count = 0;
	}
	V_ip6qb_hashseed = arc4random();
	V_ip6_maxfragsperpacket = 64;
	if (!IS_DEFAULT_VNET(curvnet))
		return;

	ip6_maxfrags = IP6_MAXFRAGS;
	EVENTHANDLER_REGISTER(nmbclusters_change,
	    frag6_change, NULL, EVENTHANDLER_PRI_ANY);
}

/*
 * Drain off all datagram fragments.
 */
void
frag6_drain(void)
{
	VNET_ITERATOR_DECL(vnet_iter);
	struct ip6q *head;
	uint32_t bucket;

	VNET_LIST_RLOCK_NOSLEEP();
	VNET_FOREACH(vnet_iter) {
		CURVNET_SET(vnet_iter);
		for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
			if (IP6QB_TRYLOCK(bucket) == 0)
				continue;
			head = IP6QB_HEAD(bucket);
			while (head->ip6q_next != head) {
				IP6STAT_INC(ip6s_fragdropped);
				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
				frag6_freef(head->ip6q_next, bucket);
			}
			IP6QB_UNLOCK(bucket);
		}
		CURVNET_RESTORE();
	}
	VNET_LIST_RUNLOCK_NOSLEEP();
}

/*
 * Put an ip fragment on a reassembly chain.
 * Like insque, but pointers in middle of structure.
 */
static void
frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
    uint32_t bucket __unused)
{

	IP6QB_LOCK_ASSERT(bucket);

	af6->ip6af_up = up6;
	af6->ip6af_down = up6->ip6af_down;
	up6->ip6af_down->ip6af_up = af6;
	up6->ip6af_down = af6;
}

/*
 * To frag6_enq as remque is to insque.
 */
static void
frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
{

	IP6QB_LOCK_ASSERT(bucket);

	af6->ip6af_up->ip6af_down = af6->ip6af_down;
	af6->ip6af_down->ip6af_up = af6->ip6af_up;
}

static void
frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket)
{

	IP6QB_LOCK_ASSERT(bucket);
	KASSERT(IP6QB_HEAD(bucket) == old,
	    ("%s: attempt to insert at head of wrong bucket"
	    " (bucket=%u, old=%p)", __func__, bucket, old));

	new->ip6q_prev = old;
	new->ip6q_next = old->ip6q_next;
	old->ip6q_next->ip6q_prev= new;
	old->ip6q_next = new;
	V_ip6qb[bucket].count++;
}

static void
frag6_remque(struct ip6q *p6, uint32_t bucket)
{

	IP6QB_LOCK_ASSERT(bucket);

	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
	V_ip6qb[bucket].count--;
}