-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquic.c
4006 lines (3914 loc) · 167 KB
/
quic.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
/*! \file quic.c
* \author Lorenzo Miniero <[email protected]>
* \copyright MIT License
* \brief QUIC stack implementation
* \details Implementation of the QUIC stack itself. This is where message
* parsing and building is implemented, including connection establishment
* and (mostly) state management.
*
* \ingroup Core
*/
#include <arpa/inet.h>
#include <netdb.h>
#include "internal/quic.h"
#include "internal/error.h"
#include "internal/utils.h"
#include "imquic/debug.h"
/* Connections tracking */
static GHashTable *connections = NULL;
void imquic_quic_connection_add(imquic_connection *conn, imquic_connection_id *cid) {
if(conn == NULL || g_atomic_int_get(&conn->destroyed) || cid == NULL)
return;
imquic_refcount_increase(&conn->ref);
conn->connection_ids = g_list_prepend(conn->connection_ids, imquic_connection_id_dup(cid));
g_hash_table_insert(connections, imquic_connection_id_dup(cid), conn);
}
void imquic_quic_connection_remove(imquic_connection_id *cid) {
if(cid == NULL)
return;
g_hash_table_remove(connections, cid);
}
/* Initialize the stack */
void imquic_quic_init(void) {
connections = g_hash_table_new_full(imquic_connection_id_hash, imquic_connection_id_equal,
(GDestroyNotify)g_free, (GDestroyNotify)imquic_connection_unref);
}
void imquic_quic_deinit(void) {
/* TODO */
}
/* QUIC stack */
const char *imquic_long_packet_type_str(imquic_long_packet_type type) {
switch(type) {
case IMQUIC_INITIAL:
return "Initial";
case IMQUIC_0RTT:
return "0-RTT";
case IMQUIC_HANDSHAKE:
return "Handshake";
case IMQUIC_RETRY:
return "Retry";
default: break;
}
return NULL;
}
const char *imquic_frame_type_str(imquic_frame_type type) {
switch(type) {
case IMQUIC_PADDING:
return "PADDING";
case IMQUIC_PING:
return "PING";
case IMQUIC_ACK:
case IMQUIC_ACK_WITH_ECN:
return "ACK";
case IMQUIC_RESET_STREAM:
return "RESET_STREAM";
case IMQUIC_STOP_SENDING:
return "STOP_SENDING";
case IMQUIC_CRYPTO:
return "CRYPTO";
case IMQUIC_NEW_TOKEN:
return "NEW_TOKEN";
case IMQUIC_STREAM:
case IMQUIC_STREAM_F:
case IMQUIC_STREAM_L:
case IMQUIC_STREAM_LF:
case IMQUIC_STREAM_O:
case IMQUIC_STREAM_OF:
case IMQUIC_STREAM_OL:
case IMQUIC_STREAM_OLF:
return "STREAM";
case IMQUIC_MAX_DATA:
return "MAX_DATA";
case IMQUIC_MAX_STREAM_DATA:
return "MAX_STREAM_DATA";
case IMQUIC_MAX_STREAMS:
return "MAX_STREAMS";
case IMQUIC_DATA_BLOCKED:
return "DATA_BLOCKED";
case IMQUIC_STREAM_DATA_BLOCKED:
return "STREAM_DATA_BLOCKED";
case IMQUIC_STREAMS_BLOCKED:
case IMQUIC_STREAMS_BLOCKED_UNI:
return "STREAMS_BLOCKED";
case IMQUIC_NEW_CONNECTION_ID:
return "NEW_CONNECTION_ID";
case IMQUIC_RETIRE_CONNECTION_ID:
return "RETIRE_CONNECTION_ID";
case IMQUIC_PATH_CHALLENGE:
return "PATH_CHALLENGE";
case IMQUIC_PATH_RESPONSE:
return "PATH_RESPONSE";
case IMQUIC_CONNECTION_CLOSE:
case IMQUIC_CONNECTION_CLOSE_APP:
return "CONNECTION_CLOSE";
case IMQUIC_HANDSHAKE_DONE:
return "HANDSHAKE_DONE";
case IMQUIC_DATAGRAM:
case IMQUIC_DATAGRAM_L:
return "DATAGRAM";
default: break;
}
return NULL;
}
const char *imquic_transport_parameter_str(imquic_transport_parameter param) {
switch(param) {
case IMQUIC_ORIGINAL_DESTINATION_CONNECTION_ID:
return "original_destination_connection_id";
case IMQUIC_MAX_IDLE_TIMEOUT:
return "max_idle_timeout";
case IMQUIC_STATELESS_RESET_TOKEN:
return "stateless_reset_token";
case IMQUIC_MAX_UDP_PAYLOAD_SIZE:
return "max_udp_payload_size";
case IMQUIC_INITIAL_MAX_DATA:
return "initial_max_data";
case IMQUIC_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL:
return "initial_max_stream_data_bidi_local";
case IMQUIC_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE:
return "initial_max_stream_data_bidi_remote";
case IMQUIC_INITIAL_MAX_STREAM_DATA_UNI:
return "initial_max_stream_data_uni";
case IMQUIC_INITIAL_MAX_STREAMS_BIDI:
return "initial_max_streams_bidi";
case IMQUIC_INITIAL_MAX_STREAMS_UNI:
return "initial_max_streams_uni";
case IMQUIC_ACK_DELAY_EXPONENT:
return "ack_delay_exponent";
case IMQUIC_MAX_ACK_DELAY:
return "max_ack_delay";
case IMQUIC_DISABLE_ACTIVE_MIGRATION:
return "disable_active_migration";
case IMQUIC_PREFERRED_ADDRESS:
return "preferred_address";
case IMQUIC_ACTIVE_CONNECTION_ID_LIMIT:
return "active_connection_id_limit";
case IMQUIC_INITIAL_SOURCE_CONNECTION_ID:
return "initial_source_connection_id";
case IMQUIC_RETRY_SOURCE_CONNECTION_ID:
return "retry_source_connection_id";
case IMQUIC_MAX_DATAGRAM_FRAME_SIZE:
return "max_datagram_frame_size";
default: break;
}
return NULL;
}
/* Frame serialization */
imquic_frame *imquic_frame_create(imquic_frame_type type, uint8_t *buffer, size_t size) {
if(size == 0)
return NULL;
imquic_frame *frame = g_malloc(sizeof(imquic_frame));
frame->type = type;
frame->buffer = g_malloc(size);
if(buffer != NULL)
memcpy(frame->buffer, buffer, size);
else
memset(frame->buffer, 0, size);
frame->size = size;
#ifdef HAVE_QLOG
frame->qlog_frame = NULL;
#endif
return frame;
}
void imquic_frame_destroy(imquic_frame *frame) {
if(frame) {
g_free(frame->buffer);
#ifdef HAVE_QLOG
if(frame->qlog_frame != NULL)
json_decref(frame->qlog_frame);
#endif
g_free(frame);
}
}
/* Packet management */
imquic_packet *imquic_packet_create(void) {
imquic_packet *pkt = g_malloc0(sizeof(imquic_packet));
return pkt;
}
int imquic_packet_long_init(imquic_packet *pkt, imquic_long_packet_type type, imquic_connection_id *src, imquic_connection_id *dest) {
pkt->type = type;
if(type == IMQUIC_INITIAL)
pkt->level = ssl_encryption_initial;
else if(type == IMQUIC_HANDSHAKE)
pkt->level = ssl_encryption_handshake;
pkt->is_valid = TRUE;
pkt->is_protected = FALSE;
pkt->is_encrypted = FALSE;
pkt->longheader = TRUE;
pkt->version = 1;
pkt->packet_number = 0; /* Will be set later */
pkt->length_offset = 0;
pkt->pkn_offset = 0;
pkt->payload_offset = 0;
pkt->retransmit_if_lost = FALSE;
pkt->ack_eliciting = FALSE;
if(dest)
memcpy(&pkt->destination, dest, sizeof(imquic_connection_id));
else
memset(&pkt->destination, 0, sizeof(imquic_connection_id));
if(src)
memcpy(&pkt->source, src, sizeof(imquic_connection_id));
else
memset(&pkt->source, 0, sizeof(imquic_connection_id));
return 0;
}
int imquic_packet_short_init(imquic_packet *pkt, imquic_connection_id *dest) {
pkt->level = ssl_encryption_application; /* FIXME */
pkt->is_valid = TRUE;
pkt->is_protected = FALSE;
pkt->is_encrypted = FALSE;
pkt->longheader = FALSE;
pkt->spin_bit = FALSE;
pkt->key_phase = FALSE;
pkt->packet_number = 0; /* Will be set later */
pkt->length_offset = 0;
pkt->pkn_offset = 0;
pkt->payload_offset = 0;
pkt->retransmit_if_lost = FALSE;
pkt->ack_eliciting = FALSE;
if(dest)
memcpy(&pkt->destination, dest, sizeof(imquic_connection_id));
else
memset(&pkt->destination, 0, sizeof(imquic_connection_id));
return 0;
}
void imquic_packet_destroy(imquic_packet *pkt) {
if(pkt) {
g_list_free_full(pkt->frames, (GDestroyNotify)imquic_frame_destroy);
#ifdef HAVE_QLOG
if(pkt->qlog_frames != NULL)
json_decref(pkt->qlog_frames);
if(pkt->qlog_frame != NULL)
json_decref(pkt->qlog_frame);
#endif
g_free(pkt);
}
}
void imquic_sent_packet_destroy(imquic_sent_packet *sent_pkt) {
if(sent_pkt) {
imquic_packet_destroy(sent_pkt->packet);
g_free(sent_pkt);
}
}
/* Helper to parse an incoming packet */
int imquic_parse_packet(imquic_network_endpoint *socket, imquic_network_address *sender,
imquic_connection **pconn, imquic_packet *pkt, uint8_t *quic, size_t bytes, size_t tot) {
IMQUIC_LOG(IMQUIC_LOG_HUGE, "Parsing packet (exploring %zu bytes)\n", bytes);
*pconn = NULL;
imquic_connection *conn = NULL;
memset(pkt, 0, sizeof(*pkt));
pkt->is_protected = TRUE;
pkt->is_encrypted = TRUE;
size_t blen = bytes;
size_t offset = 0;
uint8_t pn_length = 0;
uint64_t pn = 0;
uint64_t p_len = 0;
/* Header */
uint8_t byte = quic[0];
IMQUIC_LOG(IMQUIC_LOG_HUGE, "%02x\n", byte);
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- " BYTE_TO_BINARY_PATTERN "\n", BYTE_TO_BINARY(byte));
uint8_t lh = (byte & 0x80) >> 7;
if(lh) {
IMQUIC_LOG(IMQUIC_LOG_HUGE, "QUIC Long Header\n");
pkt->longheader = TRUE;
/* Start from the header packet protection */
uint8_t fb = (byte & 0x40) >> 6;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Fixed Bit: %d\n", (fb ? 1 : 0));
if(!fb) {
IMQUIC_LOG(IMQUIC_LOG_VERB, "Fixed Bit is not 1, invalid packet\n");
pkt->is_valid = FALSE;
return -1;
}
uint8_t type = (byte & 0x30) >> 4;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Packet Type: %02x (%s)\n", type, imquic_long_packet_type_str(type));
pkt->type = type;
/* Set the encryption level */
if(type == IMQUIC_INITIAL)
pkt->level = ssl_encryption_initial;
else if(type == IMQUIC_0RTT)
pkt->level = ssl_encryption_early_data;
else if(type == IMQUIC_HANDSHAKE)
pkt->level = ssl_encryption_handshake;
else
pkt->level = ssl_encryption_application;
uint8_t tsb = byte & 0x0F;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Type Bits: %02x (protected)\n", tsb);
offset++;
uint32_t version = 0;
memcpy(&version, &quic[offset], sizeof(version));
version = g_ntohl(version);
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Version: %08x\n", version);
if(version != 1) {
IMQUIC_LOG(IMQUIC_LOG_WARN, "Version is not 1, unsupported QUIC version\n");
/* TODO We should send a version negotiation packet here (17.2.1) */
pkt->is_valid = FALSE;
return -1;
}
pkt->version = version;
offset += 4;
size_t dcid_len = quic[offset];
if(dcid_len > 20) {
IMQUIC_LOG(IMQUIC_LOG_WARN, "Destination Connection ID too long (%zu)\n", dcid_len);
pkt->is_valid = FALSE;
return -1;
}
offset++;
size_t dcid_offset = offset;
offset += dcid_len;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Dest (len): %zu\n", dcid_len);
if(dcid_len > 0) {
imquic_print_hex(IMQUIC_LOG_HUGE, &quic[dcid_offset], dcid_len);
memcpy(pkt->destination.id, &quic[dcid_offset], dcid_len);
pkt->destination.len = dcid_len;
}
size_t scid_len = quic[offset];
if(scid_len > 20) {
IMQUIC_LOG(IMQUIC_LOG_WARN, "Source Connection ID too long (%zu)\n", scid_len);
pkt->is_valid = FALSE;
return -1;
}
offset++;
size_t scid_offset = offset;
offset += scid_len;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Src (len): %zu\n", scid_len);
if(scid_len > 0) {
imquic_print_hex(IMQUIC_LOG_HUGE, &quic[scid_offset], scid_len);
memcpy(pkt->source.id, &quic[scid_offset], scid_len);
pkt->source.len = scid_len;
}
if(type == IMQUIC_RETRY) {
/* We got a Retry */
conn = g_hash_table_lookup(connections, &pkt->destination);
#ifdef HAVE_QLOG
if(conn != NULL && conn->qlog != NULL && conn->qlog->quic && bytes == tot) {
conn->dgram_id_in++;
imquic_qlog_udp_datagrams_received(conn->qlog, conn->dgram_id_in, bytes);
}
#endif
if(conn == NULL || conn->is_server || conn->level > ssl_encryption_initial) {
IMQUIC_LOG(IMQUIC_LOG_WARN, "Ignoring invalid Retry packet\n");
#ifdef HAVE_QLOG
if(conn != NULL && conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header("retry",
&pkt->source, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_INVALID);
}
#endif
return bytes;
}
IMQUIC_LOG(IMQUIC_LOG_INFO, "Received a Retry\n");
if((bytes - offset) < 16) {
IMQUIC_LOG(IMQUIC_LOG_WARN, "Invalid Retry packet, not enough room for integrity tag\n");
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header("retry",
&pkt->source, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_INVALID);
}
#endif
return bytes;
}
size_t token_len = bytes - offset - 16;
if(token_len > sizeof(conn->retry_token.buffer)) {
IMQUIC_LOG(IMQUIC_LOG_WARN, "Ignoring Retry packet, token too large\n");
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header("retry",
&pkt->source, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_INVALID);
}
#endif
return bytes;
}
conn->retry_token.length = token_len;
memcpy(conn->retry_token.buffer, &quic[offset], token_len);
IMQUIC_LOG(IMQUIC_LOG_VERB, " -- Retry token (%zu bytes)\n", token_len);
imquic_print_hex(IMQUIC_LOG_HUGE, conn->retry_token.buffer, conn->retry_token.length);
offset += token_len;
/* Validate Retry integrity tag */
IMQUIC_LOG(IMQUIC_LOG_VERB, " -- Retry integrity tag (%zu bytes)\n", bytes-offset);
imquic_print_hex(IMQUIC_LOG_HUGE, &quic[offset], 16);
if(imquic_verify_retry(quic, bytes, conn->remote_cid.id, conn->remote_cid.len) < 0) {
/* The verification of the integrity tag failed */
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header("retry",
&pkt->source, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_DECRYPTION_FAILURE);
}
#endif
return bytes;
}
/* Update destination ID and secrets */
conn->remote_cid.len = pkt->source.len;
if(pkt->source.len > 0)
memcpy(conn->remote_cid.id, pkt->source.id, pkt->source.len);
if(imquic_derive_initial_secret(&conn->keys[ssl_encryption_initial],
conn->remote_cid.id, conn->remote_cid.len, FALSE) < 0) {
IMQUIC_LOG(IMQUIC_LOG_ERR, "[%s] Error deriving initial secret\n", imquic_get_connection_name(conn));
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header("retry",
&pkt->source, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_DECRYPTION_FAILURE);
}
#endif
return bytes;
}
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
imquic_qlog_key_updated(conn->qlog, imquic_encryption_key_type_str(ssl_encryption_initial, FALSE),
conn->keys[ssl_encryption_initial].local.key[0], conn->keys[ssl_encryption_initial].local.key_len, 0);
imquic_qlog_key_updated(conn->qlog, imquic_encryption_key_type_str(ssl_encryption_initial, TRUE),
conn->keys[ssl_encryption_initial].remote.key[0], conn->keys[ssl_encryption_initial].remote.key_len, 0);
}
#endif
/* Updated the Initial packet and resend it */
uint64_t last_pkn = conn->pkn[ssl_encryption_initial] - 1;
imquic_sent_packet *sent_pkt = imquic_listmap_find(conn->sent_pkts[ssl_encryption_initial], &last_pkn);
if(sent_pkt && sent_pkt->packet) {
sent_pkt->packet->destination.len = pkt->source.len;
if(pkt->source.len > 0)
memcpy(sent_pkt->packet->destination.id, pkt->source.id, pkt->source.len);
GList *temp = sent_pkt->packet->frames;
imquic_frame *frame = NULL;
while(temp) {
frame = (imquic_frame *)temp->data;
if(frame->type == IMQUIC_PADDING) {
if(frame->size > token_len) {
sent_pkt->packet_size -= token_len;
frame->size -= token_len;
}
break;
}
temp = temp->next;
}
}
imquic_retransmit_packet(conn, sent_pkt);
/* Done */
conn->last_activity = g_get_monotonic_time();
return bytes;
} else if(type == IMQUIC_0RTT) {
conn = g_hash_table_lookup(connections, &pkt->destination);
#ifdef HAVE_QLOG
if(conn != NULL && conn->qlog != NULL && conn->qlog->quic && bytes == tot) {
conn->dgram_id_in++;
imquic_qlog_udp_datagrams_received(conn->qlog, conn->dgram_id_in, bytes);
}
#endif
if(conn == NULL || !conn->is_server || conn->keys[ssl_encryption_early_data].remote.md == NULL) {
IMQUIC_LOG(IMQUIC_LOG_WARN, "Ignoring 0-RTT\n");
#ifdef HAVE_QLOG
if(conn != NULL && conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header("0RTT",
&pkt->source, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_GENERAL);
}
#endif
return bytes;
}
}
uint8_t length = 0;
if(type == IMQUIC_INITIAL) {
/* TODO We don't support Tokens yet, we just skip them */
uint64_t token_len = imquic_read_varint(&quic[offset], bytes-offset, &length);
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Token (len): %"SCNu64"\n", token_len);
if(length > 0) {
/* FIXME Skip token */
imquic_print_hex(IMQUIC_LOG_HUGE, &quic[offset], length);
offset += length;
}
}
p_len = imquic_read_varint(&quic[offset], bytes-offset, &length);
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Length: %"SCNu64"\n", p_len);
pkt->length_offset = offset;
offset += length;
/* Now we are where the packet number is, which we need for the sample */
size_t pn_offset = offset;
if(bytes-offset-4 < 16) {
IMQUIC_LOG(IMQUIC_LOG_VERB, "Invalid packet: not enough bytes for header protection sample\n");
pkt->is_valid = FALSE;
return -1;
}
pkt->pkn_offset = pn_offset;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- PKN (protected)\n");
imquic_print_hex(IMQUIC_LOG_HUGE, &quic[pn_offset], 4);
/* Check which connection this is for */
if(pkt->destination.len == 0 && pkt->source.len == 0) {
IMQUIC_LOG(IMQUIC_LOG_VERB, "Invalid packet: no source and no destination\n");
pkt->is_valid = FALSE;
return -1;
}
conn = g_hash_table_lookup(connections, &pkt->destination);
if(conn == NULL) {
/* Connection not found: is this a new one? */
if(!socket->is_server) {
IMQUIC_LOG(IMQUIC_LOG_WARN, "Ignoring unknown connection on client socket\n");
return -1;
}
char address[60];
IMQUIC_LOG(IMQUIC_LOG_VERB, "Creating new connection (%s)\n",
imquic_network_address_str(sender, address, sizeof(address), TRUE));
conn = imquic_connection_create(socket);
memcpy(&conn->peer, sender, sizeof(conn->peer));
if(pkt->destination.len > 0) {
conn->initial_cid.len = pkt->destination.len;
memcpy(conn->initial_cid.id, pkt->destination.id, pkt->destination.len);
imquic_quic_connection_add(conn, &conn->initial_cid);
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic)
imquic_qlog_set_odcid(conn->qlog, &conn->initial_cid);
#endif
}
uint64_t local_cid = imquic_random_uint64();
conn->local_cid.len = sizeof(local_cid);
memcpy(conn->local_cid.id, &local_cid, conn->local_cid.len);
uint64_t st1 = imquic_random_uint64(), st2 = imquic_random_uint64();
memcpy(conn->local_cid.token, &st1, sizeof(st1));
memcpy(&conn->local_cid.token[sizeof(st1)], &st2, sizeof(st2));
imquic_quic_connection_add(conn, &conn->local_cid);
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
char src_ip[NI_MAXHOST] = { 0 }, dst_ip[NI_MAXHOST] = { 0 };
imquic_qlog_connection_started(conn->qlog,
imquic_network_address_str(sender, src_ip, sizeof(src_ip), FALSE), imquic_network_address_port(sender),
imquic_network_address_str(&conn->socket->local_address, dst_ip, sizeof(dst_ip), FALSE), conn->socket->port);
imquic_qlog_version_information(conn->qlog, 1, 1);
}
#endif
}
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic && bytes == tot) {
conn->dgram_id_in++;
imquic_qlog_udp_datagrams_received(conn->qlog, conn->dgram_id_in, bytes);
}
#endif
*pconn = conn;
imquic_refcount_increase(&conn->ref);
if(pkt->source.len > 0 && conn->remote_cid.len == 0) {
conn->remote_cid.len = pkt->source.len;
memcpy(conn->remote_cid.id, pkt->source.id, pkt->source.len);
}
/* Unprotect the header */
if(conn->just_started) {
if(pkt->is_protected && imquic_derive_initial_secret(&conn->keys[ssl_encryption_initial],
pkt->destination.id, pkt->destination.len, conn->is_server) < 0) {
IMQUIC_LOG(IMQUIC_LOG_ERR, "Error deriving initial secret\n");
pkt->is_valid = FALSE;
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header(imquic_encryption_level_str(pkt->level),
&pkt->source, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_DECRYPTION_FAILURE);
}
#endif
return -1;
}
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
imquic_qlog_key_updated(conn->qlog, imquic_encryption_key_type_str(ssl_encryption_initial, TRUE),
conn->keys[ssl_encryption_initial].local.key[0], conn->keys[ssl_encryption_initial].local.key_len, 0);
imquic_qlog_key_updated(conn->qlog, imquic_encryption_key_type_str(ssl_encryption_initial, FALSE),
conn->keys[ssl_encryption_initial].remote.key[0], conn->keys[ssl_encryption_initial].remote.key_len, 0);
}
#endif
}
conn->just_started = FALSE;
/* Check which key we should use */
uint8_t *hp = conn->keys[pkt->level].remote.hp;
size_t hp_len = conn->keys[pkt->level].remote.hp_len;
if(pkt->is_protected && imquic_unprotect_header(quic, bytes, pn_offset, hp, hp_len) < 0) {
IMQUIC_LOG(IMQUIC_LOG_VERB, "Error unprotecting packet\n");
pkt->is_valid = FALSE;
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header(imquic_encryption_level_str(pkt->level),
&pkt->source, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_DECRYPTION_FAILURE);
}
#endif
return -1;
}
IMQUIC_LOG(IMQUIC_LOG_HUGE, "Header unprotected\n");
pkt->is_protected = FALSE;
uint8_t reserved = (quic[0] & 0x0C);
pn_length = (quic[0] & 0x03) + 1;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Reserved: %"SCNu8"\n", reserved);
if(reserved != 0) {
IMQUIC_LOG(IMQUIC_LOG_VERB, "Invalid packet: reserved is not 0\n");
pkt->is_valid = FALSE;
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header(imquic_encryption_level_str(pkt->level),
&pkt->source, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_INVALID);
}
#endif
return -1;
}
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- PKN (len): %"SCNu8"\n", pn_length);
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- PKN (unprotected)\n");
imquic_print_hex(IMQUIC_LOG_HUGE, &quic[pn_offset], 4);
/* Read the packet number and move to the payload */
for(uint8_t i=0; i<pn_length; i++) {
uint8_t t = quic[pn_offset+i];
pn |= t << (pn_length-i-1)*8;
}
pkt->packet_number = imquic_full_packet_number(conn->largest[pkt->level], pn, pn_length*8);
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Packet Num[%s]: %"SCNu64" (largest=%"SCNu64", pkn=%"SCNu64", pkn_len=%"SCNu8")\n",
imquic_encryption_level_str(pkt->level), pkt->packet_number, conn->largest[pkt->level], pn, pn_length);
} else {
IMQUIC_LOG(IMQUIC_LOG_HUGE, "QUIC Short Header\n");
pkt->longheader = FALSE;
/* Start from the header packet protection */
uint8_t fb = (byte & 0x40) >> 6;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Fixed Bit: %d\n", (fb ? 1 : 0));
if(!fb) {
IMQUIC_LOG(IMQUIC_LOG_VERB, "Fixed Bit is not 1, invalid packet\n");
pkt->is_valid = FALSE;
return -1;
}
pkt->spin_bit = (byte & 0x20) >> 5;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Spin Bit: %d\n", (pkt->spin_bit ? 1 : 0));
/* Set the encryption level */
pkt->level = ssl_encryption_application;
uint8_t tsb = byte & 0x1F;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Type Bits: %02x (protected)\n", tsb);
offset++;
size_t dcid_len = 8; /* FIXME */
size_t dcid_offset = offset;
offset += dcid_len;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Dest (len, already known): %zu\n", dcid_len);
if(dcid_len > 0) {
imquic_print_hex(IMQUIC_LOG_HUGE, &quic[dcid_offset], dcid_len);
memcpy(pkt->destination.id, &quic[dcid_offset], dcid_len);
pkt->destination.len = dcid_len;
}
/* Now we are where the packet number is, which we need for the sample */
size_t pn_offset = offset;
if(bytes-offset-4 < 16) {
IMQUIC_LOG(IMQUIC_LOG_VERB, "Invalid packet: not enough bytes for header protection sample\n");
pkt->is_valid = FALSE;
return -1;
}
pkt->pkn_offset = pn_offset;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- PKN (protected)\n");
imquic_print_hex(IMQUIC_LOG_HUGE, &quic[pn_offset], 4);
/* Check which connection this is for */
if(pkt->destination.len == 0) {
IMQUIC_LOG(IMQUIC_LOG_VERB, "Invalid packet: no destination\n");
pkt->is_valid = FALSE;
return -1;
}
conn = g_hash_table_lookup(connections, &pkt->destination);
if(conn == NULL) {
/* Connection not found: is this a new one? */
IMQUIC_LOG(IMQUIC_LOG_WARN, "Dropping packet for unknown connection\n");
pkt->is_valid = FALSE;
return -1;
}
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic && bytes == tot) {
conn->dgram_id_in++;
imquic_qlog_udp_datagrams_received(conn->qlog, conn->dgram_id_in, bytes);
}
#endif
*pconn = conn;
imquic_refcount_increase(&conn->ref);
/* Unprotect the header: check which key we should use */
uint8_t *hp = conn->keys[pkt->level].remote.hp;
size_t hp_len = conn->keys[pkt->level].remote.hp_len;
if(pkt->is_protected && imquic_unprotect_header(quic, bytes, pn_offset, hp, hp_len) < 0) {
IMQUIC_LOG(IMQUIC_LOG_ERR, "Error unprotecting packet\n");
pkt->is_valid = FALSE;
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header("1RTT",
NULL, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_DECRYPTION_FAILURE);
}
#endif
return -1;
}
IMQUIC_LOG(IMQUIC_LOG_HUGE, "Header unprotected\n");
pkt->is_protected = FALSE;
uint8_t reserved = (quic[0] & 0x18) >> 3;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Reserved: %"SCNu8"\n", reserved);
if(reserved != 0) {
IMQUIC_LOG(IMQUIC_LOG_VERB, "Invalid packet: reserved is not 0\n");
pkt->is_valid = FALSE;
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header("1RTT",
NULL, &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_INVALID);
}
#endif
return -1;
}
pkt->key_phase = (quic[0] & 0x04) >> 2;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Key Phase: %d\n", (pkt->key_phase ? 1 : 0));
pn_length = (quic[0] & 0x03) + 1;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- PKN (len): %"SCNu8"\n", pn_length);
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- PKN (unprotected)\n");
imquic_print_hex(IMQUIC_LOG_HUGE, &quic[pn_offset], 4);
/* Read the packet number and move to the payload */
for(uint8_t i=0; i<pn_length; i++) {
uint8_t t = quic[pn_offset+i];
pn |= t << (pn_length-i-1)*8;
}
pkt->packet_number = imquic_full_packet_number(conn->largest[pkt->level], pn, pn_length*8);
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Packet Num[%s]: %"SCNu64" (largest=%"SCNu64", pkn=%"SCNu64", pkn_len=%"SCNu8")\n",
imquic_encryption_level_str(pkt->level), pkt->packet_number, conn->largest[pkt->level], pn, pn_length);
/* Check if there's a phase change */
if(pkt->key_phase != conn->current_phase) {
/* Key phase change, update the keys */
IMQUIC_LOG(IMQUIC_LOG_INFO, "Detected key update to phase %d\n", pkt->key_phase);
imquic_update_keys(&conn->keys[ssl_encryption_application], pkt->key_phase);
conn->current_phase = pkt->key_phase;
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
imquic_qlog_key_updated(conn->qlog, imquic_encryption_key_type_str(ssl_encryption_application, conn->is_server),
conn->keys[ssl_encryption_application].local.key[conn->current_phase],
conn->keys[ssl_encryption_application].local.key_len, conn->current_phase);
imquic_qlog_key_updated(conn->qlog, imquic_encryption_key_type_str(ssl_encryption_application, !conn->is_server),
conn->keys[ssl_encryption_application].remote.key[conn->current_phase],
conn->keys[ssl_encryption_application].remote.key_len, conn->current_phase);
}
#endif
}
/* There's no payload length in Short headers, so the rest of the data is payload */
p_len = blen - pn_offset;
}
if(p_len > 0 && p_len > pn_length) {
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Payload (encrypted, %"SCNu64")\n", p_len - pn_length);
imquic_print_hex(IMQUIC_LOG_HUGE, &quic[offset+pn_length], p_len - pn_length);
/* Decrypt the payload */
pkt->payload_offset = offset+pn_length;
/* Check which keys we should use */
uint8_t *key = conn->keys[pkt->level].remote.key[conn->current_phase];
size_t key_len = conn->keys[pkt->level].remote.key_len;
uint8_t *iv = conn->keys[pkt->level].remote.iv[conn->current_phase];
size_t iv_len = conn->keys[pkt->level].remote.iv_len;
int dlen = imquic_decrypt_payload(&quic[pkt->payload_offset], p_len - pn_length,
pkt->payload.buffer, sizeof(pkt->payload),
&quic[0], pkt->payload_offset, pkt->packet_number, key, key_len, iv, iv_len);
if(dlen < 0) {
IMQUIC_LOG(IMQUIC_LOG_ERR, "Error decrypting packet\n");
pkt->is_valid = FALSE;
#ifdef HAVE_QLOG
if(conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header(imquic_encryption_level_str(pkt->level),
(lh ? &pkt->source : NULL), &pkt->destination);
imquic_qlog_packet_dropped(conn->qlog, ph, conn->dgram_id_in, bytes, IMQUIC_QLOG_TRIGGER_DECRYPTION_FAILURE);
}
#endif
return -1;
}
pkt->payload.length = dlen;
pkt->is_encrypted = FALSE;
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- Payload (decrypted, %zu)\n", pkt->payload.length);
imquic_print_hex(IMQUIC_LOG_HUGE, pkt->payload.buffer, pkt->payload.length);
/* If we got here, the packet is good */
pkt->is_valid = TRUE;
conn->recvd[pkt->level] = g_list_prepend(conn->recvd[pkt->level], imquic_dup_uint64(pkt->packet_number));
if(conn->largest[pkt->level] <= pkt->packet_number) {
conn->largest[pkt->level] = pkt->packet_number;
conn->largest_time[pkt->level] = g_get_monotonic_time();
}
/* Now parse the frames in the payload */
imquic_parse_frames(conn, pkt);
/* Done: the rest is padding (or another packet?) */
pkt->data.length = pkt->payload_offset + pkt->payload.length;
memcpy(pkt->data.buffer, quic, pkt->data.length);
offset += p_len;
} else {
/* FIXME Is a QUIC packet with no payload broken? */
pkt->is_encrypted = FALSE;
pkt->data.length = offset + pn_length;
memcpy(pkt->data.buffer, quic, pkt->data.length);
/* If we got here, the packet is good */
pkt->is_valid = TRUE;
conn->recvd[pkt->level] = g_list_prepend(conn->recvd[pkt->level], imquic_dup_uint64(pkt->packet_number));
if(conn->largest[pkt->level] <= pkt->packet_number) {
conn->largest[pkt->level] = pkt->packet_number;
conn->largest_time[pkt->level] = g_get_monotonic_time();
}
}
/* Return the size of this packet */
if(conn != NULL)
conn->last_activity = g_get_monotonic_time();
#ifdef HAVE_QLOG
if(conn != NULL && conn->qlog != NULL && conn->qlog->quic) {
json_t *ph = imquic_qlog_prepare_packet_header(
imquic_encryption_level_str(pkt->level),
(lh ? &pkt->source : NULL),
&pkt->destination);
json_object_set_new(ph, "packet_number", json_integer(pkt->packet_number));
imquic_qlog_packet_received(conn->qlog, ph, pkt->qlog_frames, conn->dgram_id_in, offset);
pkt->qlog_frames = NULL;
}
if(pkt->qlog_frames != NULL)
json_decref(pkt->qlog_frames);
pkt->qlog_frames = NULL;
#endif
return offset;
}
/* Helpers to parse frames */
int imquic_parse_frames(imquic_connection *conn, imquic_packet *pkt) {
IMQUIC_LOG(IMQUIC_LOG_HUGE, "[%s] Parsing frames\n", imquic_get_connection_name(conn));
uint8_t *bytes = pkt->payload.buffer;
size_t blen = pkt->payload.length;
imquic_print_hex(IMQUIC_LOG_HUGE, bytes, blen);
size_t offset = 0, padding = 0, parsed = 0;
#ifdef HAVE_QLOG
/* If QLOG is enabled, create an array to add parsed frames to */
if(conn->qlog != NULL && conn->qlog->quic) {
if(pkt->qlog_frames != NULL)
json_decref(pkt->qlog_frames);
pkt->qlog_frames = json_array();
}
#endif
/* Iterate on all frames */
pkt->ack_eliciting = FALSE;
while(blen > 0 && !g_atomic_int_get(&conn->closed)) {
imquic_frame_type type = bytes[offset];
if(type == IMQUIC_PADDING) {
padding++;
} else {
if(padding > 0) {
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- %s (%02x), %zu items\n", imquic_frame_type_str(IMQUIC_PADDING), IMQUIC_PADDING, padding);
padding = 0;
}
IMQUIC_LOG(IMQUIC_LOG_HUGE, " -- %s (%02x)\n", imquic_frame_type_str(type), bytes[offset]);
}
if(type == IMQUIC_PADDING) {
/* Nothing else to do */
offset++;
blen--;
} else if(type == IMQUIC_PING) {
/* Nothing to do */
pkt->ack_eliciting = TRUE;
offset++;
blen--;
#ifdef HAVE_QLOG
if(conn != NULL && conn->qlog != NULL && conn->qlog->quic) {
json_t *frame = imquic_qlog_prepare_packet_frame("ping");
json_array_append_new(pkt->qlog_frames, frame);
}
#endif
} else if(type == IMQUIC_ACK || type == IMQUIC_ACK_WITH_ECN) {
/* Parse this ACK frame */
parsed = imquic_payload_parse_ack(conn, pkt, &bytes[offset], blen, pkt->level);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_RESET_STREAM) {
/* Parse this RESET_STREAM frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_reset_stream(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_STOP_SENDING) {
/* Parse this STOP_SENDING frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_stop_sending(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_CRYPTO) {
/* Parse this CRYPTO frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_crypto(conn, pkt, &bytes[offset], blen, pkt->level);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_NEW_TOKEN) {
/* Parse this NEW_TOKEN frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_new_token(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type >= IMQUIC_STREAM && type <= IMQUIC_STREAM_OLF) {
/* Parse this STREAM frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_stream(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_MAX_DATA) {
/* Parse this MAX_DATA frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_max_data(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_MAX_STREAM_DATA) {
/* Parse this MAX_STREAM_DATA frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_max_stream_data(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_MAX_STREAMS || type == IMQUIC_MAX_STREAMS_UNI) {
/* Parse this MAX_STREAMS frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_max_streams(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_DATA_BLOCKED) {
/* Parse this DATA_BLOCKED frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_data_blocked(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_STREAM_DATA_BLOCKED) {
/* Parse this STREAM_DATA_BLOCKED frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_stream_data_blocked(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_STREAMS_BLOCKED || type == IMQUIC_STREAMS_BLOCKED_UNI) {
/* Parse this STREAMS_BLOCKED frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_streams_blocked(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_NEW_CONNECTION_ID) {
/* Parse this NEW_CONNECTION_ID frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_new_connection_id(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_RETIRE_CONNECTION_ID) {
/* Parse this RETIRE_CONNECTION_ID frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_retire_connection_id(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_PATH_CHALLENGE) {
/* Parse this PATH_CHALLENGE frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_path_challenge(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_PATH_RESPONSE) {
/* Parse this PATH_RESPONSE frame */
pkt->ack_eliciting = TRUE;
parsed = imquic_payload_parse_path_response(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_CONNECTION_CLOSE || type == IMQUIC_CONNECTION_CLOSE_APP) {
/* Parse this CONNECTION_CLOSE frame */
parsed = imquic_payload_parse_connection_close(conn, pkt, &bytes[offset], blen);
offset += parsed;
blen -= parsed;
} else if(type == IMQUIC_HANDSHAKE_DONE) {
/* Nothing to do */
pkt->ack_eliciting = TRUE;
offset++;
blen--;
#ifdef HAVE_QLOG
if(conn != NULL && conn->qlog != NULL && conn->qlog->quic) {
json_t *frame = imquic_qlog_prepare_packet_frame("handshake_done");
json_array_append_new(pkt->qlog_frames, frame);
}