-
Notifications
You must be signed in to change notification settings - Fork 536
/
packet_builder.c
1045 lines (906 loc) · 35.6 KB
/
packet_builder.c
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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) Microsoft Corporation.
Licensed under the MIT License.
Abstract:
Packet builder abstracts the logic to build up a chain of UDP datagrams each
of which may consist of multiple QUIC packets. As necessary, it allocates
additional datagrams, adds QUIC packet headers, finalizes the QUIC packet
encryption and sends the packets off.
--*/
#include "precomp.h"
#ifdef QUIC_CLOG
#include "packet_builder.c.clog.h"
#endif
#ifdef QUIC_FUZZER
__declspec(noinline)
void
QuicFuzzInjectHook(
_Inout_ QUIC_PACKET_BUILDER *Builder
);
#endif
_IRQL_requires_max_(PASSIVE_LEVEL)
void
QuicPacketBuilderSendBatch(
_Inout_ QUIC_PACKET_BUILDER* Builder
);
#if DEBUG
_IRQL_requires_max_(PASSIVE_LEVEL)
void
QuicPacketBuilderValidate(
_In_ const QUIC_PACKET_BUILDER* Builder,
_In_ BOOLEAN ShouldHaveData
)
{
if (ShouldHaveData) {
CXPLAT_DBG_ASSERT(Builder->Key != NULL);
CXPLAT_DBG_ASSERT(Builder->SendData != NULL);
CXPLAT_DBG_ASSERT(Builder->Datagram != NULL);
CXPLAT_DBG_ASSERT(Builder->DatagramLength != 0);
CXPLAT_DBG_ASSERT(Builder->HeaderLength != 0);
CXPLAT_DBG_ASSERT(Builder->Metadata->FrameCount != 0);
}
CXPLAT_DBG_ASSERT(Builder->Path != NULL);
CXPLAT_DBG_ASSERT(Builder->Path->DestCid != NULL);
CXPLAT_DBG_ASSERT(Builder->BatchCount <= QUIC_MAX_CRYPTO_BATCH_COUNT);
if (Builder->Key != NULL) {
CXPLAT_DBG_ASSERT(Builder->Key->PacketKey != NULL);
CXPLAT_DBG_ASSERT(Builder->Key->HeaderKey != NULL);
}
CXPLAT_DBG_ASSERT(Builder->EncryptionOverhead <= 16);
if (Builder->SendData == NULL) {
CXPLAT_DBG_ASSERT(Builder->Datagram == NULL);
}
if (Builder->Datagram) {
CXPLAT_DBG_ASSERT(Builder->Datagram->Length != 0);
CXPLAT_DBG_ASSERT(Builder->Datagram->Length <= UINT16_MAX);
CXPLAT_DBG_ASSERT(Builder->Datagram->Length >= Builder->MinimumDatagramLength);
CXPLAT_DBG_ASSERT(Builder->Datagram->Length >= (uint32_t)(Builder->DatagramLength + Builder->EncryptionOverhead));
CXPLAT_DBG_ASSERT(Builder->DatagramLength >= Builder->PacketStart);
CXPLAT_DBG_ASSERT(Builder->DatagramLength >= Builder->HeaderLength);
CXPLAT_DBG_ASSERT(Builder->DatagramLength >= Builder->PacketStart + Builder->HeaderLength);
if (Builder->PacketType != SEND_PACKET_SHORT_HEADER_TYPE) {
CXPLAT_DBG_ASSERT(Builder->PayloadLengthOffset != 0);
if (ShouldHaveData) {
CXPLAT_DBG_ASSERT(Builder->DatagramLength >= Builder->PacketStart + Builder->PayloadLengthOffset);
}
}
} else {
CXPLAT_DBG_ASSERT(Builder->DatagramLength == 0);
CXPLAT_DBG_ASSERT(Builder->Metadata->FrameCount == 0);
}
}
#else
#define QuicPacketBuilderValidate(Builder, ShouldHaveData) // no-op
#endif
_IRQL_requires_max_(DISPATCH_LEVEL)
_Success_(return != FALSE)
BOOLEAN
QuicPacketBuilderInitialize(
_Inout_ QUIC_PACKET_BUILDER* Builder,
_In_ QUIC_CONNECTION* Connection,
_In_ QUIC_PATH* Path
)
{
CXPLAT_DBG_ASSERT(Path->DestCid != NULL);
Builder->Connection = Connection;
Builder->Path = Path;
Builder->PacketBatchSent = FALSE;
Builder->PacketBatchRetransmittable = FALSE;
Builder->Metadata = &Builder->MetadataStorage.Metadata;
Builder->EncryptionOverhead = CXPLAT_ENCRYPTION_OVERHEAD;
Builder->TotalDatagramsLength = 0;
if (Connection->SourceCids.Next == NULL) {
QuicTraceLogConnWarning(
NoSrcCidAvailable,
Connection,
"No src CID to send with");
return FALSE;
}
Builder->SourceCid =
CXPLAT_CONTAINING_RECORD(
Connection->SourceCids.Next,
QUIC_CID_HASH_ENTRY,
Link);
uint64_t TimeNow = CxPlatTimeUs64();
uint64_t TimeSinceLastSend;
if (Connection->Send.LastFlushTimeValid) {
TimeSinceLastSend =
CxPlatTimeDiff64(Connection->Send.LastFlushTime, TimeNow);
} else {
TimeSinceLastSend = 0;
}
Builder->SendAllowance =
QuicCongestionControlGetSendAllowance(
&Connection->CongestionControl,
TimeSinceLastSend,
Connection->Send.LastFlushTimeValid);
if (Builder->SendAllowance > Path->Allowance) {
Builder->SendAllowance = Path->Allowance;
}
Connection->Send.LastFlushTime = TimeNow;
Connection->Send.LastFlushTimeValid = TRUE;
return TRUE;
}
_IRQL_requires_max_(PASSIVE_LEVEL)
void
QuicPacketBuilderCleanup(
_Inout_ QUIC_PACKET_BUILDER* Builder
)
{
CXPLAT_DBG_ASSERT(Builder->SendData == NULL);
if (Builder->PacketBatchSent && Builder->PacketBatchRetransmittable) {
QuicLossDetectionUpdateTimer(&Builder->Connection->LossDetection, FALSE);
}
QuicSentPacketMetadataReleaseFrames(Builder->Metadata);
CxPlatSecureZeroMemory(Builder->HpMask, sizeof(Builder->HpMask));
}
//
// This function makes sure the current send buffer and other related data is
// prepared for writing the requested data. If there was already a QUIC packet
// in the process of being built, it will try to reuse it if possible. If not,
// it will finalize the current one and start a new one.
//
_IRQL_requires_max_(PASSIVE_LEVEL)
_Success_(return != FALSE)
BOOLEAN
QuicPacketBuilderPrepare(
_Inout_ QUIC_PACKET_BUILDER* Builder,
_In_ QUIC_PACKET_KEY_TYPE NewPacketKeyType,
_In_ BOOLEAN IsTailLossProbe,
_In_ BOOLEAN IsPathMtuDiscovery
)
{
QUIC_CONNECTION* Connection = Builder->Connection;
if (Connection->Crypto.TlsState.WriteKeys[NewPacketKeyType] == NULL) {
//
// A NULL key here usually means the connection had a fatal error in
// such a way that resulted in the key not getting created. The
// connection is most likely trying to send a connection close frame,
// but without the key, nothing can be done. Just silently kill the
// connection.
//
QuicTraceEvent(
ConnError,
"[conn][%p] ERROR, %s.",
Connection,
"NULL key in builder prepare");
QuicConnSilentlyAbort(Connection);
return FALSE;
}
BOOLEAN Result = FALSE;
uint8_t NewPacketType =
Connection->Stats.QuicVersion == QUIC_VERSION_2 ?
QuicKeyTypeToPacketTypeV2(NewPacketKeyType) :
QuicKeyTypeToPacketTypeV1(NewPacketKeyType);
//
// For now, we can't send QUIC Bit as 0 on initial packets from client to server.
// see: https://www.ietf.org/archive/id/draft-ietf-quic-bit-grease-04.html#name-clearing-the-quic-bit
//
BOOLEAN FixedBit = (QuicConnIsClient(Connection) &&
(NewPacketType == (uint8_t)QUIC_INITIAL_V1 || NewPacketKeyType == (uint8_t)QUIC_INITIAL_V2)) ? TRUE : Connection->State.FixedBit;
uint16_t DatagramSize = Builder->Path->Mtu;
if ((uint32_t)DatagramSize > Builder->Path->Allowance) {
CXPLAT_DBG_ASSERT(!IsPathMtuDiscovery); // PMTUD always happens after source addr validation.
DatagramSize = (uint16_t)Builder->Path->Allowance;
}
CXPLAT_DBG_ASSERT(!IsPathMtuDiscovery || !IsTailLossProbe); // Never both.
QuicPacketBuilderValidate(Builder, FALSE);
//
// Next, make sure the current QUIC packet matches the new packet type. If
// the current one doesn't match, finalize it and then start a new one.
//
const uint16_t Partition = Connection->Worker->PartitionIndex;
const uint64_t PartitionShifted = ((uint64_t)Partition + 1) << 40;
BOOLEAN NewQuicPacket = FALSE;
if (Builder->PacketType != NewPacketType || IsPathMtuDiscovery ||
(Builder->Datagram != NULL && (Builder->Datagram->Length - Builder->DatagramLength) < QUIC_MIN_PACKET_SPARE_SPACE)) {
//
// The current data cannot go in the current QUIC packet. Finalize the
// current QUIC packet up so we can create another.
//
if (Builder->SendData != NULL) {
BOOLEAN FlushDatagrams = IsPathMtuDiscovery;
if (Builder->PacketType != NewPacketType &&
Builder->PacketType == SEND_PACKET_SHORT_HEADER_TYPE) {
FlushDatagrams = TRUE;
}
QuicPacketBuilderFinalize(Builder, FlushDatagrams);
}
if (Builder->SendData == NULL &&
Builder->TotalCountDatagrams >= QUIC_MAX_DATAGRAMS_PER_SEND) {
goto Error;
}
NewQuicPacket = TRUE;
} else if (Builder->Datagram == NULL) {
NewQuicPacket = TRUE;
}
if (Builder->Datagram == NULL) {
//
// Allocate and initialize a new send buffer (UDP packet/payload).
//
BOOLEAN SendDataAllocated = FALSE;
if (Builder->SendData == NULL) {
Builder->BatchId =
PartitionShifted | InterlockedIncrement64((int64_t*)&QuicLibraryGetPerProc()->SendBatchId);
CXPLAT_SEND_CONFIG SendConfig = {
&Builder->Path->Route,
IsPathMtuDiscovery ?
0 :
MaxUdpPayloadSizeForFamily(
QuicAddrGetFamily(&Builder->Path->Route.RemoteAddress),
DatagramSize),
Builder->EcnEctSet ? CXPLAT_ECN_ECT_0 : CXPLAT_ECN_NON_ECT,
Builder->Connection->Registration->ExecProfile == QUIC_EXECUTION_PROFILE_TYPE_MAX_THROUGHPUT ?
CXPLAT_SEND_FLAGS_MAX_THROUGHPUT : CXPLAT_SEND_FLAGS_NONE
};
Builder->SendData =
CxPlatSendDataAlloc(Builder->Path->Binding->Socket, &SendConfig);
if (Builder->SendData == NULL) {
QuicTraceEvent(
AllocFailure,
"Allocation of '%s' failed. (%llu bytes)",
"packet send context",
0);
goto Error;
}
SendDataAllocated = TRUE;
}
uint16_t NewDatagramLength =
MaxUdpPayloadSizeForFamily(
QuicAddrGetFamily(&Builder->Path->Route.RemoteAddress),
IsPathMtuDiscovery ? Builder->Path->MtuDiscovery.ProbeSize : DatagramSize);
if ((Connection->PeerTransportParams.Flags & QUIC_TP_FLAG_MAX_UDP_PAYLOAD_SIZE) &&
NewDatagramLength > Connection->PeerTransportParams.MaxUdpPayloadSize) {
NewDatagramLength = (uint16_t)Connection->PeerTransportParams.MaxUdpPayloadSize;
}
Builder->Datagram =
CxPlatSendDataAllocBuffer(
Builder->SendData,
NewDatagramLength);
if (Builder->Datagram == NULL) {
QuicTraceEvent(
AllocFailure,
"Allocation of '%s' failed. (%llu bytes)",
"packet datagram",
NewDatagramLength);
if (SendDataAllocated) {
CxPlatSendDataFree(Builder->SendData);
Builder->SendData = NULL;
}
goto Error;
}
Builder->DatagramLength = 0;
Builder->MinimumDatagramLength = 0;
if (IsTailLossProbe && QuicConnIsClient(Connection)) {
if (NewPacketType == SEND_PACKET_SHORT_HEADER_TYPE) {
//
// Short header (1-RTT) packets need to be padded enough to
// elicit stateless resets from the server.
//
Builder->MinimumDatagramLength =
QUIC_RECOMMENDED_STATELESS_RESET_PACKET_LENGTH +
8 /* a little fudge factor */;
} else {
//
// Initial/Handshake packets need to be padded to unblock a
// server (possibly) blocked on source address validation.
//
Builder->MinimumDatagramLength = NewDatagramLength;
}
} else if ((Connection->Stats.QuicVersion == QUIC_VERSION_2 && NewPacketType == QUIC_INITIAL_V2) ||
(Connection->Stats.QuicVersion != QUIC_VERSION_2 && NewPacketType == QUIC_INITIAL_V1)) {
//
// Make sure to pad Initial packets.
//
Builder->MinimumDatagramLength =
MaxUdpPayloadSizeForFamily(
QuicAddrGetFamily(&Builder->Path->Route.RemoteAddress),
Builder->Path->Mtu);
if ((uint32_t)Builder->MinimumDatagramLength > Builder->Datagram->Length) {
//
// On server, if we're limited by amplification protection, just
// pad up to that limit instead.
//
Builder->MinimumDatagramLength = (uint16_t)Builder->Datagram->Length;
}
} else if (IsPathMtuDiscovery) {
Builder->MinimumDatagramLength = NewDatagramLength;
}
}
if (NewQuicPacket) {
//
// Initialize the new QUIC packet state.
//
Builder->PacketType = NewPacketType;
Builder->EncryptLevel =
Connection->Stats.QuicVersion == QUIC_VERSION_2 ?
QuicPacketTypeToEncryptLevelV2(NewPacketType) :
QuicPacketTypeToEncryptLevelV1(NewPacketType);
Builder->Key = Connection->Crypto.TlsState.WriteKeys[NewPacketKeyType];
CXPLAT_DBG_ASSERT(Builder->Key != NULL);
CXPLAT_DBG_ASSERT(Builder->Key->PacketKey != NULL);
CXPLAT_DBG_ASSERT(Builder->Key->HeaderKey != NULL);
if (NewPacketKeyType == QUIC_PACKET_KEY_1_RTT &&
Connection->State.Disable1RttEncrytion) {
Builder->EncryptionOverhead = 0;
}
Builder->Metadata->PacketId =
PartitionShifted | InterlockedIncrement64((int64_t*)&QuicLibraryGetPerProc()->SendPacketId);
QuicTraceEvent(
PacketCreated,
"[pack][%llu] Created in batch %llu",
Builder->Metadata->PacketId,
Builder->BatchId);
Builder->Metadata->FrameCount = 0;
Builder->Metadata->PacketNumber = Connection->Send.NextPacketNumber++;
Builder->Metadata->Flags.KeyType = NewPacketKeyType;
Builder->Metadata->Flags.IsAckEliciting = FALSE;
Builder->Metadata->Flags.IsMtuProbe = IsPathMtuDiscovery;
Builder->Metadata->Flags.SuspectedLost = FALSE;
#if DEBUG
Builder->Metadata->Flags.Freed = FALSE;
#endif
Builder->PacketStart = Builder->DatagramLength;
Builder->HeaderLength = 0;
uint8_t* Header =
Builder->Datagram->Buffer + Builder->DatagramLength;
uint16_t BufferSpaceAvailable =
(uint16_t)Builder->Datagram->Length - Builder->DatagramLength;
if (NewPacketType == SEND_PACKET_SHORT_HEADER_TYPE) {
QUIC_PACKET_SPACE* PacketSpace = Connection->Packets[Builder->EncryptLevel];
Builder->PacketNumberLength = 4; // TODO - Determine correct length based on BDP.
switch (Connection->Stats.QuicVersion) {
case QUIC_VERSION_1:
case QUIC_VERSION_DRAFT_29:
case QUIC_VERSION_MS_1:
case QUIC_VERSION_2:
Builder->HeaderLength =
QuicPacketEncodeShortHeaderV1(
&Builder->Path->DestCid->CID,
Builder->Metadata->PacketNumber,
Builder->PacketNumberLength,
Builder->Path->SpinBit,
PacketSpace->CurrentKeyPhase,
FixedBit,
BufferSpaceAvailable,
Header);
Builder->Metadata->Flags.KeyPhase = PacketSpace->CurrentKeyPhase;
break;
default:
CXPLAT_FRE_ASSERT(FALSE);
Builder->HeaderLength = 0; // For build warning.
break;
}
} else { // Long Header
switch (Connection->Stats.QuicVersion) {
case QUIC_VERSION_1:
case QUIC_VERSION_DRAFT_29:
case QUIC_VERSION_MS_1:
case QUIC_VERSION_2:
default:
Builder->HeaderLength =
QuicPacketEncodeLongHeaderV1(
Connection->Stats.QuicVersion,
NewPacketType,
FixedBit,
&Builder->Path->DestCid->CID,
&Builder->SourceCid->CID,
Connection->Send.InitialTokenLength,
Connection->Send.InitialToken,
(uint32_t)Builder->Metadata->PacketNumber,
BufferSpaceAvailable,
Header,
&Builder->PayloadLengthOffset,
&Builder->PacketNumberLength);
break;
}
}
Builder->DatagramLength += Builder->HeaderLength;
}
CXPLAT_DBG_ASSERT(Builder->PacketType == NewPacketType);
CXPLAT_DBG_ASSERT(Builder->Key == Connection->Crypto.TlsState.WriteKeys[NewPacketKeyType]);
CXPLAT_DBG_ASSERT(Builder->BatchCount == 0 || Builder->PacketType == SEND_PACKET_SHORT_HEADER_TYPE);
Result = TRUE;
Error:
QuicPacketBuilderValidate(Builder, FALSE);
return Result;
}
_IRQL_requires_max_(PASSIVE_LEVEL)
_Success_(return != FALSE)
BOOLEAN
QuicPacketBuilderGetPacketTypeAndKeyForControlFrames(
_In_ const QUIC_PACKET_BUILDER* Builder,
_In_ uint32_t SendFlags,
_Out_ QUIC_PACKET_KEY_TYPE* PacketKeyType
)
{
QUIC_CONNECTION* Connection = Builder->Connection;
CXPLAT_DBG_ASSERT(SendFlags != 0);
QuicSendValidate(&Builder->Connection->Send);
for (QUIC_PACKET_KEY_TYPE KeyType = 0;
KeyType <= Connection->Crypto.TlsState.WriteKey;
++KeyType) {
if (KeyType == QUIC_PACKET_KEY_0_RTT) {
continue; // Crypto is never written with 0-RTT key.
}
QUIC_PACKET_KEY* PacketsKey =
Connection->Crypto.TlsState.WriteKeys[KeyType];
if (PacketsKey == NULL) {
continue; // Key has been discarded.
}
QUIC_ENCRYPT_LEVEL EncryptLevel = QuicKeyTypeToEncryptLevel(KeyType);
if (EncryptLevel == QUIC_ENCRYPT_LEVEL_1_RTT) {
//
// Always allowed to send with 1-RTT.
//
*PacketKeyType = QUIC_PACKET_KEY_1_RTT;
return TRUE;
}
QUIC_PACKET_SPACE* Packets = Connection->Packets[EncryptLevel];
CXPLAT_DBG_ASSERT(Packets != NULL);
if (SendFlags & QUIC_CONN_SEND_FLAG_ACK &&
Packets->AckTracker.AckElicitingPacketsToAcknowledge) {
//
// ACK frames have the highest send priority; but they only
// determine a packet type if they can be sent as ACK-only.
//
*PacketKeyType = KeyType;
return TRUE;
}
if (SendFlags & QUIC_CONN_SEND_FLAG_CRYPTO &&
QuicCryptoHasPendingCryptoFrame(&Connection->Crypto) &&
EncryptLevel == QuicCryptoGetNextEncryptLevel(&Connection->Crypto)) {
//
// Crypto handshake data is ready to be sent.
//
*PacketKeyType = KeyType;
return TRUE;
}
}
if (SendFlags & (QUIC_CONN_SEND_FLAG_CONNECTION_CLOSE | QUIC_CONN_SEND_FLAG_PING)) {
//
// CLOSE or PING is ready to be sent. This is always sent with the
// current write key.
//
// TODO - This logic isn't correct. The peer might not be able to read
// this key, so the CLOSE frame should be sent at the current and
// previous encryption level if the handshake hasn't been confirmed.
//
if (Connection->Crypto.TlsState.WriteKey == QUIC_PACKET_KEY_0_RTT) {
*PacketKeyType = QUIC_PACKET_KEY_INITIAL;
} else {
*PacketKeyType = Connection->Crypto.TlsState.WriteKey;
}
return TRUE;
}
if (Connection->Crypto.TlsState.WriteKeys[QUIC_PACKET_KEY_1_RTT] != NULL) {
*PacketKeyType = QUIC_PACKET_KEY_1_RTT;
return TRUE;
}
QuicTraceLogConnWarning(
GetPacketTypeFailure,
Builder->Connection,
"Failed to get packet type for control frames, 0x%x",
SendFlags);
CXPLAT_DBG_ASSERT(CxPlatIsRandomMemoryFailureEnabled()); // This shouldn't have been called then!
return FALSE;
}
_IRQL_requires_max_(PASSIVE_LEVEL)
_Success_(return != FALSE)
BOOLEAN
QuicPacketBuilderPrepareForControlFrames(
_Inout_ QUIC_PACKET_BUILDER* Builder,
_In_ BOOLEAN IsTailLossProbe,
_In_ uint32_t SendFlags
)
{
CXPLAT_DBG_ASSERT(!(SendFlags & QUIC_CONN_SEND_FLAG_DPLPMTUD));
QUIC_PACKET_KEY_TYPE PacketKeyType;
return
QuicPacketBuilderGetPacketTypeAndKeyForControlFrames(
Builder,
SendFlags,
&PacketKeyType) &&
QuicPacketBuilderPrepare(
Builder,
PacketKeyType,
IsTailLossProbe,
FALSE);
}
_IRQL_requires_max_(PASSIVE_LEVEL)
_Success_(return != FALSE)
BOOLEAN
QuicPacketBuilderPrepareForPathMtuDiscovery(
_Inout_ QUIC_PACKET_BUILDER* Builder
)
{
return
QuicPacketBuilderPrepare(
Builder,
QUIC_PACKET_KEY_1_RTT,
FALSE,
TRUE);
}
_IRQL_requires_max_(PASSIVE_LEVEL)
_Success_(return != FALSE)
BOOLEAN
QuicPacketBuilderPrepareForStreamFrames(
_Inout_ QUIC_PACKET_BUILDER* Builder,
_In_ BOOLEAN IsTailLossProbe
)
{
QUIC_PACKET_KEY_TYPE PacketKeyType;
if (Builder->Connection->Crypto.TlsState.WriteKeys[QUIC_PACKET_KEY_0_RTT] != NULL &&
Builder->Connection->Crypto.TlsState.WriteKeys[QUIC_PACKET_KEY_1_RTT] == NULL) {
//
// Application stream data can only be sent with the 0-RTT key if the
// 1-RTT key is unavailable.
//
PacketKeyType = QUIC_PACKET_KEY_0_RTT;
} else {
CXPLAT_DBG_ASSERT(Builder->Connection->Crypto.TlsState.WriteKeys[QUIC_PACKET_KEY_1_RTT]);
PacketKeyType = QUIC_PACKET_KEY_1_RTT;
}
return QuicPacketBuilderPrepare(Builder, PacketKeyType, IsTailLossProbe, FALSE);
}
_IRQL_requires_max_(PASSIVE_LEVEL)
void
QuicPacketBuilderFinalizeHeaderProtection(
_Inout_ QUIC_PACKET_BUILDER* Builder
)
{
CXPLAT_DBG_ASSERT(Builder->Key != NULL);
QUIC_STATUS Status;
if (QUIC_FAILED(
Status =
CxPlatHpComputeMask(
Builder->Key->HeaderKey,
Builder->BatchCount,
Builder->CipherBatch,
Builder->HpMask))) {
CXPLAT_TEL_ASSERT(FALSE);
QuicConnFatalError(Builder->Connection, Status, "HP failure");
return;
}
for (uint8_t i = 0; i < Builder->BatchCount; ++i) {
uint16_t Offset = i * CXPLAT_HP_SAMPLE_LENGTH;
uint8_t* Header = Builder->HeaderBatch[i];
Header[0] ^= (Builder->HpMask[Offset] & 0x1f); // Bottom 5 bits for SH
Header += 1 + Builder->Path->DestCid->CID.Length;
for (uint8_t j = 0; j < Builder->PacketNumberLength; ++j) {
Header[j] ^= Builder->HpMask[Offset + 1 + j];
}
}
Builder->BatchCount = 0;
}
//
// This function completes the current QUIC packet. It updates the header if
// necessary and encrypts the payload. If there isn't enough space for another
// QUIC packet, it also completes the send buffer (i.e. UDP payload) and sets
// the current send buffer pointer to NULL. If that send buffer was the last
// in the current send batch, then the send context is also completed and sent
// off.
//
_IRQL_requires_max_(PASSIVE_LEVEL)
BOOLEAN
QuicPacketBuilderFinalize(
_Inout_ QUIC_PACKET_BUILDER* Builder,
_In_ BOOLEAN FlushBatchedDatagrams
)
{
QUIC_CONNECTION* Connection = Builder->Connection;
BOOLEAN FinalQuicPacket = FALSE;
BOOLEAN CanKeepSending = TRUE;
QuicPacketBuilderValidate(Builder, FALSE);
if (Builder->Datagram == NULL || Builder->Metadata->FrameCount == 0) {
//
// Nothing got framed into this packet. Undo the header of this
// packet.
//
if (Builder->Datagram != NULL) {
--Connection->Send.NextPacketNumber;
Builder->DatagramLength -= Builder->HeaderLength;
Builder->HeaderLength = 0;
CanKeepSending = FALSE;
if (Builder->DatagramLength == 0) {
CxPlatSendDataFreeBuffer(Builder->SendData, Builder->Datagram);
Builder->Datagram = NULL;
}
}
if (Builder->Path->Allowance != UINT32_MAX) {
QuicConnAddOutFlowBlockedReason(
Connection, QUIC_FLOW_BLOCKED_AMPLIFICATION_PROT);
}
FinalQuicPacket = FlushBatchedDatagrams && (Builder->TotalCountDatagrams != 0);
goto Exit;
}
QuicPacketBuilderValidate(Builder, TRUE);
//
// Calculate some of the packet buffer parameters (mostly used for encryption).
//
uint8_t* Header =
Builder->Datagram->Buffer + Builder->PacketStart;
uint16_t PayloadLength =
Builder->DatagramLength - (Builder->PacketStart + Builder->HeaderLength);
uint16_t ExpectedFinalDatagramLength =
Builder->DatagramLength + Builder->EncryptionOverhead;
if (FlushBatchedDatagrams ||
Builder->PacketType == SEND_PACKET_SHORT_HEADER_TYPE ||
(uint16_t)Builder->Datagram->Length - ExpectedFinalDatagramLength < QUIC_MIN_PACKET_SPARE_SPACE) {
FinalQuicPacket = TRUE;
if (!FlushBatchedDatagrams && CxPlatDataPathIsPaddingPreferred(MsQuicLib.Datapath, Builder->SendData)) {
//
// When buffering multiple datagrams in a single contiguous buffer
// (at the datapath layer), all but the last datagram needs to be
// fully padded.
//
Builder->MinimumDatagramLength = (uint16_t)Builder->Datagram->Length;
}
}
uint16_t PaddingLength;
if (FinalQuicPacket && ExpectedFinalDatagramLength < Builder->MinimumDatagramLength) {
PaddingLength = Builder->MinimumDatagramLength - ExpectedFinalDatagramLength;
} else if (Builder->PacketNumberLength + PayloadLength < sizeof(uint32_t)) {
//
// For packet protection to work, there must always be at least 4 bytes
// of payload and/or packet number.
//
PaddingLength = sizeof(uint32_t) - Builder->PacketNumberLength - PayloadLength;
} else {
PaddingLength = 0;
}
if (PaddingLength != 0) {
CxPlatZeroMemory(
Builder->Datagram->Buffer + Builder->DatagramLength,
PaddingLength);
PayloadLength += PaddingLength;
Builder->DatagramLength += PaddingLength;
}
if (Builder->PacketType != SEND_PACKET_SHORT_HEADER_TYPE) {
switch (Connection->Stats.QuicVersion) {
case QUIC_VERSION_1:
case QUIC_VERSION_DRAFT_29:
case QUIC_VERSION_MS_1:
case QUIC_VERSION_2:
default:
QuicVarIntEncode2Bytes(
(uint16_t)Builder->PacketNumberLength +
PayloadLength +
Builder->EncryptionOverhead,
Header + Builder->PayloadLengthOffset);
break;
}
}
#ifdef QUIC_FUZZER
QuicFuzzInjectHook(Builder);
#endif
if (QuicTraceLogVerboseEnabled()) {
QuicPacketLogHeader(
Connection,
FALSE,
Builder->Path->DestCid->CID.Length,
Builder->Metadata->PacketNumber,
Builder->HeaderLength + PayloadLength,
Header,
Connection->Stats.QuicVersion);
QuicFrameLogAll(
Connection,
FALSE,
Builder->Metadata->PacketNumber,
Builder->HeaderLength + PayloadLength,
Header,
Builder->HeaderLength);
}
if (Builder->EncryptionOverhead != 0 &&
!(Builder->Key->Type == QUIC_PACKET_KEY_1_RTT && Connection->Paths[0].EncryptionOffloading)) {
//
// Encrypt the data.
//
QuicTraceEvent(
PacketEncrypt,
"[pack][%llu] Encrypting",
Builder->Metadata->PacketId);
PayloadLength += Builder->EncryptionOverhead;
Builder->DatagramLength += Builder->EncryptionOverhead;
uint8_t* Payload = Header + Builder->HeaderLength;
uint8_t Iv[CXPLAT_MAX_IV_LENGTH];
QuicCryptoCombineIvAndPacketNumber(Builder->Key->Iv, (uint8_t*) &Builder->Metadata->PacketNumber, Iv);
QUIC_STATUS Status;
if (QUIC_FAILED(
Status =
CxPlatEncrypt(
Builder->Key->PacketKey,
Iv,
Builder->HeaderLength,
Header,
PayloadLength,
Payload))) {
QuicConnFatalError(Connection, Status, "Encryption failure");
goto Exit;
}
QuicTraceEvent(
PacketFinalize,
"[pack][%llu] Finalizing",
Builder->Metadata->PacketId);
if (Connection->State.HeaderProtectionEnabled) {
uint8_t* PnStart = Payload - Builder->PacketNumberLength;
if (Builder->PacketType == SEND_PACKET_SHORT_HEADER_TYPE) {
CXPLAT_DBG_ASSERT(Builder->BatchCount < QUIC_MAX_CRYPTO_BATCH_COUNT);
//
// Batch the header protection for short header packets.
//
CxPlatCopyMemory(
Builder->CipherBatch + Builder->BatchCount * CXPLAT_HP_SAMPLE_LENGTH,
PnStart + 4,
CXPLAT_HP_SAMPLE_LENGTH);
Builder->HeaderBatch[Builder->BatchCount] = Header;
if (++Builder->BatchCount == QUIC_MAX_CRYPTO_BATCH_COUNT) {
QuicPacketBuilderFinalizeHeaderProtection(Builder);
}
} else {
CXPLAT_DBG_ASSERT(Builder->BatchCount == 0);
//
// Individually do header protection for long header packets as
// they generally use different keys.
//
if (QUIC_FAILED(
Status =
CxPlatHpComputeMask(
Builder->Key->HeaderKey,
1,
PnStart + 4,
Builder->HpMask))) {
CXPLAT_TEL_ASSERT(FALSE);
QuicConnFatalError(Connection, Status, "HP failure");
goto Exit;
}
Header[0] ^= (Builder->HpMask[0] & 0x0f); // Bottom 4 bits for LH
for (uint8_t i = 0; i < Builder->PacketNumberLength; ++i) {
PnStart[i] ^= Builder->HpMask[1 + i];
}
}
}
//
// Increment the key phase sent bytes count.
//
QUIC_PACKET_SPACE* PacketSpace = Connection->Packets[Builder->EncryptLevel];
PacketSpace->CurrentKeyPhaseBytesSent += (PayloadLength - Builder->EncryptionOverhead);
//
// Check if the next packet sent will exceed the limit of bytes per
// key phase, and update the keys. Only for 1-RTT keys.
//
if (Builder->PacketType == SEND_PACKET_SHORT_HEADER_TYPE &&
PacketSpace->CurrentKeyPhaseBytesSent + CXPLAT_MAX_MTU >=
Connection->Settings.MaxBytesPerKey &&
!PacketSpace->AwaitingKeyPhaseConfirmation &&
Connection->State.HandshakeConfirmed) {
Status = QuicCryptoGenerateNewKeys(Connection);
if (QUIC_FAILED(Status)) {
QuicTraceEvent(
ConnErrorStatus,
"[conn][%p] ERROR, %u, %s.",
Connection,
Status,
"Send-triggered key update");
QuicConnFatalError(Connection, Status, "Send-triggered key update");
goto Exit;
}
QuicCryptoUpdateKeyPhase(Connection, TRUE);
//
// Update the packet key in use by the send builder.
//
Builder->Key = Connection->Crypto.TlsState.WriteKeys[QUIC_PACKET_KEY_1_RTT];
CXPLAT_DBG_ASSERT(Builder->Key != NULL);
CXPLAT_DBG_ASSERT(Builder->Key->PacketKey != NULL);
CXPLAT_DBG_ASSERT(Builder->Key->HeaderKey != NULL);
}
} else {
QuicTraceEvent(
PacketFinalize,
"[pack][%llu] Finalizing",
Builder->Metadata->PacketId);
}
//
// Track the sent packet.
//
CXPLAT_DBG_ASSERT(Builder->Metadata->FrameCount != 0);
Builder->Metadata->SentTime = CxPlatTimeUs64();
Builder->Metadata->PacketLength =
Builder->HeaderLength + PayloadLength;
Builder->Metadata->Flags.EcnEctSet = Builder->EcnEctSet;
QuicTraceEvent(
ConnPacketSent,
"[conn][%p][TX][%llu] %hhu (%hu bytes)",
Connection,
Builder->Metadata->PacketNumber,
QuicPacketTraceType(Builder->Metadata),
Builder->Metadata->PacketLength);
QuicLossDetectionOnPacketSent(
&Connection->LossDetection,
Builder->Path,
Builder->Metadata);
Builder->Metadata->FrameCount = 0;
if (Builder->Metadata->Flags.IsAckEliciting) {
Builder->PacketBatchRetransmittable = TRUE;
//
// Remove the bytes from the allowance.
//
if ((uint32_t)Builder->Metadata->PacketLength > Builder->SendAllowance) {
Builder->SendAllowance = 0;
} else {
Builder->SendAllowance -= Builder->Metadata->PacketLength;
}
}
Exit:
//
// Send the packet out if necessary.
//
if (FinalQuicPacket) {
if (Builder->Datagram != NULL) {
if (Builder->Metadata->Flags.EcnEctSet) {
++Connection->Send.NumPacketsSentWithEct;
}
Builder->Datagram->Length = Builder->DatagramLength;
Builder->Datagram = NULL;
++Builder->TotalCountDatagrams;
Builder->TotalDatagramsLength += Builder->DatagramLength;
Builder->DatagramLength = 0;
}
if (FlushBatchedDatagrams || CxPlatSendDataIsFull(Builder->SendData)) {
if (Builder->BatchCount != 0) {
QuicPacketBuilderFinalizeHeaderProtection(Builder);
}
CXPLAT_DBG_ASSERT(Builder->TotalCountDatagrams > 0);
QuicPacketBuilderSendBatch(Builder);
CXPLAT_DBG_ASSERT(Builder->Metadata->FrameCount == 0);
QuicTraceEvent(
PacketBatchSent,
"[pack][%llu] Batch sent",
Builder->BatchId);
}
if ((Connection->Stats.QuicVersion != QUIC_VERSION_2 && Builder->PacketType == QUIC_RETRY_V1) ||
(Connection->Stats.QuicVersion == QUIC_VERSION_2 && Builder->PacketType == QUIC_RETRY_V2)) {
CXPLAT_DBG_ASSERT(Builder->Metadata->PacketNumber == 0);
QuicConnCloseLocally(
Connection,
QUIC_CLOSE_SILENT,
QUIC_ERROR_NO_ERROR,
NULL);