-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
quic_txp.c
2420 lines (2056 loc) · 82.4 KB
/
quic_txp.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 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "internal/quic_txp.h"
#include "internal/quic_fifd.h"
#include "internal/quic_stream_map.h"
#include "internal/common.h"
#include <openssl/err.h>
#define MIN_CRYPTO_HDR_SIZE 3
#define MIN_FRAME_SIZE_HANDSHAKE_DONE 1
#define MIN_FRAME_SIZE_MAX_DATA 2
#define MIN_FRAME_SIZE_ACK 5
#define MIN_FRAME_SIZE_CRYPTO (MIN_CRYPTO_HDR_SIZE + 1)
#define MIN_FRAME_SIZE_STREAM 3 /* minimum useful size (for non-FIN) */
#define MIN_FRAME_SIZE_MAX_STREAMS_BIDI 2
#define MIN_FRAME_SIZE_MAX_STREAMS_UNI 2
struct ossl_quic_tx_packetiser_st {
OSSL_QUIC_TX_PACKETISER_ARGS args;
/*
* Opaque initial token blob provided by caller. TXP frees using the
* callback when it is no longer needed.
*/
const unsigned char *initial_token;
size_t initial_token_len;
ossl_quic_initial_token_free_fn *initial_token_free_cb;
void *initial_token_free_cb_arg;
/* Subcomponents of the TXP that we own. */
QUIC_FIFD fifd; /* QUIC Frame-in-Flight Dispatcher */
/* Internal state. */
uint64_t next_pn[QUIC_PN_SPACE_NUM]; /* Next PN to use in given PN space. */
OSSL_TIME last_tx_time; /* Last time a packet was generated, or 0. */
/* Internal state - frame (re)generation flags. */
unsigned int want_handshake_done : 1;
unsigned int want_max_data : 1;
unsigned int want_max_streams_bidi : 1;
unsigned int want_max_streams_uni : 1;
/* Internal state - frame (re)generation flags - per PN space. */
unsigned int want_ack : QUIC_PN_SPACE_NUM;
unsigned int force_ack_eliciting : QUIC_PN_SPACE_NUM;
/*
* Internal state - connection close terminal state.
* Once this is set, it is not unset unlike other want_ flags - we keep
* sending it in every packet.
*/
unsigned int want_conn_close : 1;
/* Has the handshake been completed? */
unsigned int handshake_complete : 1;
OSSL_QUIC_FRAME_CONN_CLOSE conn_close_frame;
/* Internal state - packet assembly. */
unsigned char *scratch; /* scratch buffer for packet assembly */
size_t scratch_len; /* number of bytes allocated for scratch */
OSSL_QTX_IOVEC *iovec; /* scratch iovec array for use with QTX */
size_t alloc_iovec; /* size of iovec array */
/* Message callback related arguments */
ossl_msg_cb msg_callback;
void *msg_callback_arg;
SSL *msg_callback_ssl;
/* Callbacks. */
void (*ack_tx_cb)(const OSSL_QUIC_FRAME_ACK *ack,
uint32_t pn_space,
void *arg);
void *ack_tx_cb_arg;
};
/*
* The TX helper records state used while generating frames into packets. It
* enables serialization into the packet to be done "transactionally" where
* serialization of a frame can be rolled back if it fails midway (e.g. if it
* does not fit).
*/
struct tx_helper {
OSSL_QUIC_TX_PACKETISER *txp;
/*
* The Maximum Packet Payload Length in bytes. This is the amount of
* space we have to generate frames into.
*/
size_t max_ppl;
/*
* Number of bytes we have generated so far.
*/
size_t bytes_appended;
/*
* Number of scratch bytes in txp->scratch we have used so far. Some iovecs
* will reference this scratch buffer. When we need to use more of it (e.g.
* when we need to put frame headers somewhere), we append to the scratch
* buffer, resizing if necessary, and increase this accordingly.
*/
size_t scratch_bytes;
/*
* Bytes reserved in the MaxPPL budget. We keep this number of bytes spare
* until reserve_allowed is set to 1. Currently this is always at most 1, as
* a PING frame takes up one byte and this mechanism is only used to ensure
* we can encode a PING frame if we have been asked to ensure a packet is
* ACK-eliciting and we are unusure if we are going to add any other
* ACK-eliciting frames before we reach our MaxPPL budget.
*/
size_t reserve;
/*
* Number of iovecs we have currently appended. This is the number of
* entries valid in txp->iovec.
*/
size_t num_iovec;
/*
* Whether we are allowed to make use of the reserve bytes in our MaxPPL
* budget. This is used to ensure we have room to append a PING frame later
* if we need to. Once we know we will not need to append a PING frame, this
* is set to 1.
*/
unsigned int reserve_allowed : 1;
/*
* Set to 1 if we have appended a STREAM frame with an implicit length. If
* this happens we should never append another frame after that frame as it
* cannot be validly encoded. This is just a safety check.
*/
unsigned int done_implicit : 1;
struct {
/*
* The fields in this structure are valid if active is set, which means
* that a serialization transaction is currently in progress.
*/
unsigned char *data;
WPACKET wpkt;
unsigned int active : 1;
} txn;
};
static void tx_helper_rollback(struct tx_helper *h);
static int txp_ensure_iovec(OSSL_QUIC_TX_PACKETISER *txp, size_t num);
/* Initialises the TX helper. */
static int tx_helper_init(struct tx_helper *h, OSSL_QUIC_TX_PACKETISER *txp,
size_t max_ppl, size_t reserve)
{
if (reserve > max_ppl)
return 0;
h->txp = txp;
h->max_ppl = max_ppl;
h->reserve = reserve;
h->num_iovec = 0;
h->bytes_appended = 0;
h->scratch_bytes = 0;
h->reserve_allowed = 0;
h->done_implicit = 0;
h->txn.data = NULL;
h->txn.active = 0;
if (max_ppl > h->txp->scratch_len) {
unsigned char *scratch;
scratch = OPENSSL_realloc(h->txp->scratch, max_ppl);
if (scratch == NULL)
return 0;
h->txp->scratch = scratch;
h->txp->scratch_len = max_ppl;
}
return 1;
}
static void tx_helper_cleanup(struct tx_helper *h)
{
if (h->txn.active)
tx_helper_rollback(h);
h->txp = NULL;
}
static void tx_helper_unrestrict(struct tx_helper *h)
{
h->reserve_allowed = 1;
}
/*
* Append an extent of memory to the iovec list. The memory must remain
* allocated until we finish generating the packet and call the QTX.
*
* In general, the buffers passed to this function will be from one of two
* ranges:
*
* - Application data contained in stream buffers managed elsewhere
* in the QUIC stack; or
*
* - Control frame data appended into txp->scratch using tx_helper_begin and
* tx_helper_commit.
*
*/
static int tx_helper_append_iovec(struct tx_helper *h,
const unsigned char *buf,
size_t buf_len)
{
if (buf_len == 0)
return 1;
if (!ossl_assert(!h->done_implicit))
return 0;
if (!txp_ensure_iovec(h->txp, h->num_iovec + 1))
return 0;
h->txp->iovec[h->num_iovec].buf = buf;
h->txp->iovec[h->num_iovec].buf_len = buf_len;
++h->num_iovec;
h->bytes_appended += buf_len;
return 1;
}
/*
* How many more bytes of space do we have left in our plaintext packet payload?
*/
static size_t tx_helper_get_space_left(struct tx_helper *h)
{
return h->max_ppl
- (h->reserve_allowed ? 0 : h->reserve) - h->bytes_appended;
}
/*
* Begin a control frame serialization transaction. This allows the
* serialization of the control frame to be backed out if it turns out it won't
* fit. Write the control frame to the returned WPACKET. Ensure you always
* call tx_helper_rollback or tx_helper_commit (or tx_helper_cleanup). Returns
* NULL on failure.
*/
static WPACKET *tx_helper_begin(struct tx_helper *h)
{
size_t space_left, len;
unsigned char *data;
if (!ossl_assert(!h->txn.active))
return NULL;
if (!ossl_assert(!h->done_implicit))
return NULL;
data = (unsigned char *)h->txp->scratch + h->scratch_bytes;
len = h->txp->scratch_len - h->scratch_bytes;
space_left = tx_helper_get_space_left(h);
if (!ossl_assert(space_left <= len))
return NULL;
if (!WPACKET_init_static_len(&h->txn.wpkt, data, len, 0))
return NULL;
if (!WPACKET_set_max_size(&h->txn.wpkt, space_left)) {
WPACKET_cleanup(&h->txn.wpkt);
return NULL;
}
h->txn.data = data;
h->txn.active = 1;
return &h->txn.wpkt;
}
static void tx_helper_end(struct tx_helper *h, int success)
{
if (success)
WPACKET_finish(&h->txn.wpkt);
else
WPACKET_cleanup(&h->txn.wpkt);
h->txn.active = 0;
h->txn.data = NULL;
}
/* Abort a control frame serialization transaction. */
static void tx_helper_rollback(struct tx_helper *h)
{
if (!h->txn.active)
return;
tx_helper_end(h, 0);
}
/* Commit a control frame. */
static int tx_helper_commit(struct tx_helper *h)
{
size_t l = 0;
if (!h->txn.active)
return 0;
if (!WPACKET_get_total_written(&h->txn.wpkt, &l)) {
tx_helper_end(h, 0);
return 0;
}
if (!tx_helper_append_iovec(h, h->txn.data, l)) {
tx_helper_end(h, 0);
return 0;
}
if (h->txp->msg_callback != NULL && l > 0) {
uint64_t ftype;
int ctype = SSL3_RT_QUIC_FRAME_FULL;
PACKET pkt;
if (!PACKET_buf_init(&pkt, h->txn.data, l)
|| !ossl_quic_wire_peek_frame_header(&pkt, &ftype)) {
tx_helper_end(h, 0);
return 0;
}
if (ftype == OSSL_QUIC_FRAME_TYPE_PADDING)
ctype = SSL3_RT_QUIC_FRAME_PADDING;
else if (OSSL_QUIC_FRAME_TYPE_IS_STREAM(ftype)
|| ftype == OSSL_QUIC_FRAME_TYPE_CRYPTO)
ctype = SSL3_RT_QUIC_FRAME_HEADER;
h->txp->msg_callback(1, OSSL_QUIC1_VERSION, ctype, h->txn.data, l,
h->txp->msg_callback_ssl,
h->txp->msg_callback_arg);
}
h->scratch_bytes += l;
tx_helper_end(h, 1);
return 1;
}
static QUIC_SSTREAM *get_sstream_by_id(uint64_t stream_id, uint32_t pn_space,
void *arg);
static void on_regen_notify(uint64_t frame_type, uint64_t stream_id,
QUIC_TXPIM_PKT *pkt, void *arg);
static void on_confirm_notify(uint64_t frame_type, uint64_t stream_id,
QUIC_TXPIM_PKT *pkt, void *arg);
static void on_sstream_updated(uint64_t stream_id, void *arg);
static int sstream_is_pending(QUIC_SSTREAM *sstream);
static int txp_el_pending(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
uint32_t archetype,
int cc_can_send,
uint32_t *conn_close_enc_level);
static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
uint32_t archetype,
int cc_can_send,
int is_last_in_dgram,
int dgram_contains_initial,
int chosen_for_conn_close,
QUIC_TXP_STATUS *status);
static size_t txp_determine_pn_len(OSSL_QUIC_TX_PACKETISER *txp);
static int txp_determine_ppl_from_pl(OSSL_QUIC_TX_PACKETISER *txp,
size_t pl,
uint32_t enc_level,
size_t hdr_len,
size_t *r);
static size_t txp_get_mdpl(OSSL_QUIC_TX_PACKETISER *txp);
static int txp_generate_for_el_actual(OSSL_QUIC_TX_PACKETISER *txp,
uint32_t enc_level,
uint32_t archetype,
size_t min_ppl,
size_t max_ppl,
size_t pkt_overhead,
QUIC_PKT_HDR *phdr,
int chosen_for_conn_close,
QUIC_TXP_STATUS *status);
OSSL_QUIC_TX_PACKETISER *ossl_quic_tx_packetiser_new(const OSSL_QUIC_TX_PACKETISER_ARGS *args)
{
OSSL_QUIC_TX_PACKETISER *txp;
if (args == NULL
|| args->qtx == NULL
|| args->txpim == NULL
|| args->cfq == NULL
|| args->ackm == NULL
|| args->qsm == NULL
|| args->conn_txfc == NULL
|| args->conn_rxfc == NULL
|| args->max_streams_bidi_rxfc == NULL
|| args->max_streams_uni_rxfc == NULL) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
return NULL;
}
txp = OPENSSL_zalloc(sizeof(*txp));
if (txp == NULL)
return NULL;
txp->args = *args;
txp->last_tx_time = ossl_time_zero();
if (!ossl_quic_fifd_init(&txp->fifd,
txp->args.cfq, txp->args.ackm, txp->args.txpim,
get_sstream_by_id, txp,
on_regen_notify, txp,
on_confirm_notify, txp,
on_sstream_updated, txp)) {
OPENSSL_free(txp);
return NULL;
}
return txp;
}
void ossl_quic_tx_packetiser_free(OSSL_QUIC_TX_PACKETISER *txp)
{
if (txp == NULL)
return;
ossl_quic_tx_packetiser_set_initial_token(txp, NULL, 0, NULL, NULL);
ossl_quic_fifd_cleanup(&txp->fifd);
OPENSSL_free(txp->iovec);
OPENSSL_free(txp->conn_close_frame.reason);
OPENSSL_free(txp->scratch);
OPENSSL_free(txp);
}
void ossl_quic_tx_packetiser_set_initial_token(OSSL_QUIC_TX_PACKETISER *txp,
const unsigned char *token,
size_t token_len,
ossl_quic_initial_token_free_fn *free_cb,
void *free_cb_arg)
{
if (txp->initial_token != NULL && txp->initial_token_free_cb != NULL)
txp->initial_token_free_cb(txp->initial_token, txp->initial_token_len,
txp->initial_token_free_cb_arg);
txp->initial_token = token;
txp->initial_token_len = token_len;
txp->initial_token_free_cb = free_cb;
txp->initial_token_free_cb_arg = free_cb_arg;
}
int ossl_quic_tx_packetiser_set_cur_dcid(OSSL_QUIC_TX_PACKETISER *txp,
const QUIC_CONN_ID *dcid)
{
if (dcid == NULL) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
txp->args.cur_dcid = *dcid;
return 1;
}
int ossl_quic_tx_packetiser_set_cur_scid(OSSL_QUIC_TX_PACKETISER *txp,
const QUIC_CONN_ID *scid)
{
if (scid == NULL) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
txp->args.cur_scid = *scid;
return 1;
}
/* Change the destination L4 address the TXP uses to send datagrams. */
int ossl_quic_tx_packetiser_set_peer(OSSL_QUIC_TX_PACKETISER *txp,
const BIO_ADDR *peer)
{
if (peer == NULL) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
txp->args.peer = *peer;
return 1;
}
void ossl_quic_tx_packetiser_set_ack_tx_cb(OSSL_QUIC_TX_PACKETISER *txp,
void (*cb)(const OSSL_QUIC_FRAME_ACK *ack,
uint32_t pn_space,
void *arg),
void *cb_arg)
{
txp->ack_tx_cb = cb;
txp->ack_tx_cb_arg = cb_arg;
}
int ossl_quic_tx_packetiser_discard_enc_level(OSSL_QUIC_TX_PACKETISER *txp,
uint32_t enc_level)
{
if (enc_level >= QUIC_ENC_LEVEL_NUM) {
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
if (enc_level != QUIC_ENC_LEVEL_0RTT)
txp->args.crypto[ossl_quic_enc_level_to_pn_space(enc_level)] = NULL;
return 1;
}
void ossl_quic_tx_packetiser_notify_handshake_complete(OSSL_QUIC_TX_PACKETISER *txp)
{
txp->handshake_complete = 1;
}
void ossl_quic_tx_packetiser_schedule_handshake_done(OSSL_QUIC_TX_PACKETISER *txp)
{
txp->want_handshake_done = 1;
}
void ossl_quic_tx_packetiser_schedule_ack_eliciting(OSSL_QUIC_TX_PACKETISER *txp,
uint32_t pn_space)
{
txp->force_ack_eliciting |= (1UL << pn_space);
}
void ossl_quic_tx_packetiser_schedule_ack(OSSL_QUIC_TX_PACKETISER *txp,
uint32_t pn_space)
{
txp->want_ack |= (1UL << pn_space);
}
#define TXP_ERR_INTERNAL 0 /* Internal (e.g. alloc) error */
#define TXP_ERR_SUCCESS 1 /* Success */
#define TXP_ERR_SPACE 2 /* Not enough room for another packet */
#define TXP_ERR_INPUT 3 /* Invalid/malformed input */
int ossl_quic_tx_packetiser_has_pending(OSSL_QUIC_TX_PACKETISER *txp,
uint32_t archetype,
uint32_t flags)
{
uint32_t enc_level, conn_close_enc_level = QUIC_ENC_LEVEL_NUM;
int bypass_cc = ((flags & TX_PACKETISER_BYPASS_CC) != 0);
int cc_can_send;
cc_can_send
= (bypass_cc
|| txp->args.cc_method->get_tx_allowance(txp->args.cc_data) > 0);
for (enc_level = QUIC_ENC_LEVEL_INITIAL;
enc_level < QUIC_ENC_LEVEL_NUM;
++enc_level)
if (txp_el_pending(txp, enc_level, archetype, cc_can_send,
&conn_close_enc_level))
return 1;
return 0;
}
/*
* Generates a datagram by polling the various ELs to determine if they want to
* generate any frames, and generating a datagram which coalesces packets for
* any ELs which do.
*/
int ossl_quic_tx_packetiser_generate(OSSL_QUIC_TX_PACKETISER *txp,
uint32_t archetype,
QUIC_TXP_STATUS *status)
{
uint32_t enc_level, conn_close_enc_level = QUIC_ENC_LEVEL_NUM;
int have_pkt_for_el[QUIC_ENC_LEVEL_NUM], is_last_in_dgram, cc_can_send;
size_t num_el_in_dgram = 0, pkts_done = 0;
int rc;
status->sent_ack_eliciting = 0;
/*
* If CC says we cannot send we still may be able to send any queued probes.
*/
cc_can_send = (txp->args.cc_method->get_tx_allowance(txp->args.cc_data) > 0);
for (enc_level = QUIC_ENC_LEVEL_INITIAL;
enc_level < QUIC_ENC_LEVEL_NUM;
++enc_level) {
have_pkt_for_el[enc_level] = txp_el_pending(txp, enc_level, archetype,
cc_can_send,
&conn_close_enc_level);
if (have_pkt_for_el[enc_level])
++num_el_in_dgram;
}
if (num_el_in_dgram == 0)
return TX_PACKETISER_RES_NO_PKT;
/*
* Should not be needed, but a sanity check in case anyone else has been
* using the QTX.
*/
ossl_qtx_finish_dgram(txp->args.qtx);
for (enc_level = QUIC_ENC_LEVEL_INITIAL;
enc_level < QUIC_ENC_LEVEL_NUM;
++enc_level) {
if (!have_pkt_for_el[enc_level])
continue;
is_last_in_dgram = (pkts_done + 1 == num_el_in_dgram);
rc = txp_generate_for_el(txp, enc_level, archetype, cc_can_send,
is_last_in_dgram,
have_pkt_for_el[QUIC_ENC_LEVEL_INITIAL],
enc_level == conn_close_enc_level,
status);
if (rc != TXP_ERR_SUCCESS) {
/*
* If we already successfully did at least one, make sure we report
* this via the return code.
*/
if (pkts_done > 0)
break;
else
return TX_PACKETISER_RES_FAILURE;
}
++pkts_done;
}
ossl_qtx_finish_dgram(txp->args.qtx);
return TX_PACKETISER_RES_SENT_PKT;
}
struct archetype_data {
unsigned int allow_ack : 1;
unsigned int allow_ping : 1;
unsigned int allow_crypto : 1;
unsigned int allow_handshake_done : 1;
unsigned int allow_path_challenge : 1;
unsigned int allow_path_response : 1;
unsigned int allow_new_conn_id : 1;
unsigned int allow_retire_conn_id : 1;
unsigned int allow_stream_rel : 1;
unsigned int allow_conn_fc : 1;
unsigned int allow_conn_close : 1;
unsigned int allow_cfq_other : 1;
unsigned int allow_new_token : 1;
unsigned int allow_force_ack_eliciting : 1;
};
static const struct archetype_data archetypes[QUIC_ENC_LEVEL_NUM][TX_PACKETISER_ARCHETYPE_NUM] = {
/* EL 0(INITIAL) */
{
/* EL 0(INITIAL) - Archetype 0(NORMAL) */
{
/*allow_ack =*/ 1,
/*allow_ping =*/ 1,
/*allow_crypto =*/ 1,
/*allow_handshake_done =*/ 0,
/*allow_path_challenge =*/ 0,
/*allow_path_response =*/ 0,
/*allow_new_conn_id =*/ 0,
/*allow_retire_conn_id =*/ 0,
/*allow_stream_rel =*/ 0,
/*allow_conn_fc =*/ 0,
/*allow_conn_close =*/ 1,
/*allow_cfq_other =*/ 1,
/*allow_new_token =*/ 0,
/*allow_force_ack_eliciting =*/ 1,
},
/* EL 0(INITIAL) - Archetype 1(ACK_ONLY) */
{
/*allow_ack =*/ 1,
/*allow_ping =*/ 0,
/*allow_crypto =*/ 0,
/*allow_handshake_done =*/ 0,
/*allow_path_challenge =*/ 0,
/*allow_path_response =*/ 0,
/*allow_new_conn_id =*/ 0,
/*allow_retire_conn_id =*/ 0,
/*allow_stream_rel =*/ 0,
/*allow_conn_fc =*/ 0,
/*allow_conn_close =*/ 0,
/*allow_cfq_other =*/ 0,
/*allow_new_token =*/ 0,
/*allow_force_ack_eliciting =*/ 1,
},
},
/* EL 1(HANDSHAKE) */
{
/* EL 1(HANDSHAKE) - Archetype 0(NORMAL) */
{
/*allow_ack =*/ 1,
/*allow_ping =*/ 1,
/*allow_crypto =*/ 1,
/*allow_handshake_done =*/ 0,
/*allow_path_challenge =*/ 0,
/*allow_path_response =*/ 0,
/*allow_new_conn_id =*/ 0,
/*allow_retire_conn_id =*/ 0,
/*allow_stream_rel =*/ 0,
/*allow_conn_fc =*/ 0,
/*allow_conn_close =*/ 1,
/*allow_cfq_other =*/ 1,
/*allow_new_token =*/ 0,
/*allow_force_ack_eliciting =*/ 1,
},
/* EL 1(HANDSHAKE) - Archetype 1(ACK_ONLY) */
{
/*allow_ack =*/ 1,
/*allow_ping =*/ 0,
/*allow_crypto =*/ 0,
/*allow_handshake_done =*/ 0,
/*allow_path_challenge =*/ 0,
/*allow_path_response =*/ 0,
/*allow_new_conn_id =*/ 0,
/*allow_retire_conn_id =*/ 0,
/*allow_stream_rel =*/ 0,
/*allow_conn_fc =*/ 0,
/*allow_conn_close =*/ 0,
/*allow_cfq_other =*/ 0,
/*allow_new_token =*/ 0,
/*allow_force_ack_eliciting =*/ 1,
},
},
/* EL 2(0RTT) */
{
/* EL 2(0RTT) - Archetype 0(NORMAL) */
{
/*allow_ack =*/ 0,
/*allow_ping =*/ 1,
/*allow_crypto =*/ 0,
/*allow_handshake_done =*/ 0,
/*allow_path_challenge =*/ 0,
/*allow_path_response =*/ 0,
/*allow_new_conn_id =*/ 1,
/*allow_retire_conn_id =*/ 1,
/*allow_stream_rel =*/ 1,
/*allow_conn_fc =*/ 1,
/*allow_conn_close =*/ 1,
/*allow_cfq_other =*/ 0,
/*allow_new_token =*/ 0,
/*allow_force_ack_eliciting =*/ 0,
},
/* EL 2(0RTT) - Archetype 1(ACK_ONLY) */
{
/*allow_ack =*/ 0,
/*allow_ping =*/ 0,
/*allow_crypto =*/ 0,
/*allow_handshake_done =*/ 0,
/*allow_path_challenge =*/ 0,
/*allow_path_response =*/ 0,
/*allow_new_conn_id =*/ 0,
/*allow_retire_conn_id =*/ 0,
/*allow_stream_rel =*/ 0,
/*allow_conn_fc =*/ 0,
/*allow_conn_close =*/ 0,
/*allow_cfq_other =*/ 0,
/*allow_new_token =*/ 0,
/*allow_force_ack_eliciting =*/ 0,
},
},
/* EL 3(1RTT) */
{
/* EL 3(1RTT) - Archetype 0(NORMAL) */
{
/*allow_ack =*/ 1,
/*allow_ping =*/ 1,
/*allow_crypto =*/ 1,
/*allow_handshake_done =*/ 1,
/*allow_path_challenge =*/ 0,
/*allow_path_response =*/ 0,
/*allow_new_conn_id =*/ 1,
/*allow_retire_conn_id =*/ 1,
/*allow_stream_rel =*/ 1,
/*allow_conn_fc =*/ 1,
/*allow_conn_close =*/ 1,
/*allow_cfq_other =*/ 1,
/*allow_new_token =*/ 1,
/*allow_force_ack_eliciting =*/ 1,
},
/* EL 3(1RTT) - Archetype 1(ACK_ONLY) */
{
/*allow_ack =*/ 1,
/*allow_ping =*/ 0,
/*allow_crypto =*/ 0,
/*allow_handshake_done =*/ 0,
/*allow_path_challenge =*/ 0,
/*allow_path_response =*/ 0,
/*allow_new_conn_id =*/ 0,
/*allow_retire_conn_id =*/ 0,
/*allow_stream_rel =*/ 0,
/*allow_conn_fc =*/ 0,
/*allow_conn_close =*/ 0,
/*allow_cfq_other =*/ 0,
/*allow_new_token =*/ 0,
/*allow_force_ack_eliciting =*/ 1,
}
}
};
static int txp_get_archetype_data(uint32_t enc_level,
uint32_t archetype,
struct archetype_data *a)
{
if (enc_level >= QUIC_ENC_LEVEL_NUM
|| archetype >= TX_PACKETISER_ARCHETYPE_NUM)
return 0;
/* No need to avoid copying this as it should not exceed one int in size. */
*a = archetypes[enc_level][archetype];
return 1;
}
/*
* Returns 1 if the given EL wants to produce one or more frames.
* Always returns 0 if the given EL is discarded.
*/
static int txp_el_pending(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
uint32_t archetype,
int cc_can_send,
uint32_t *conn_close_enc_level)
{
struct archetype_data a;
uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
QUIC_CFQ_ITEM *cfq_item;
if (!ossl_qtx_is_enc_level_provisioned(txp->args.qtx, enc_level))
return 0;
if (*conn_close_enc_level > enc_level)
*conn_close_enc_level = enc_level;
if (!txp_get_archetype_data(enc_level, archetype, &a))
return 0;
/* Do we need to send a PTO probe? */
if (a.allow_force_ack_eliciting) {
OSSL_ACKM_PROBE_INFO *probe_info
= ossl_ackm_get0_probe_request(txp->args.ackm);
if ((enc_level == QUIC_ENC_LEVEL_INITIAL
&& probe_info->anti_deadlock_initial > 0)
|| (enc_level == QUIC_ENC_LEVEL_HANDSHAKE
&& probe_info->anti_deadlock_handshake > 0)
|| probe_info->pto[pn_space] > 0)
return 1;
}
if (!cc_can_send)
/* If CC says we cannot currently send, we can only send probes. */
return 0;
/* Does the crypto stream for this EL want to produce anything? */
if (a.allow_crypto && sstream_is_pending(txp->args.crypto[pn_space]))
return 1;
/* Does the ACKM for this PN space want to produce anything? */
if (a.allow_ack && (ossl_ackm_is_ack_desired(txp->args.ackm, pn_space)
|| (txp->want_ack & (1UL << pn_space)) != 0))
return 1;
/* Do we need to force emission of an ACK-eliciting packet? */
if (a.allow_force_ack_eliciting
&& (txp->force_ack_eliciting & (1UL << pn_space)) != 0)
return 1;
/* Does the connection-level RXFC want to produce a frame? */
if (a.allow_conn_fc && (txp->want_max_data
|| ossl_quic_rxfc_has_cwm_changed(txp->args.conn_rxfc, 0)))
return 1;
/* Do we want to produce a MAX_STREAMS frame? */
if (a.allow_conn_fc
&& (txp->want_max_streams_bidi
|| ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_bidi_rxfc,
0)
|| txp->want_max_streams_uni
|| ossl_quic_rxfc_has_cwm_changed(txp->args.max_streams_uni_rxfc,
0)))
return 1;
/* Do we want to produce a HANDSHAKE_DONE frame? */
if (a.allow_handshake_done && txp->want_handshake_done)
return 1;
/* Do we want to produce a CONNECTION_CLOSE frame? */
if (a.allow_conn_close && txp->want_conn_close &&
*conn_close_enc_level == enc_level)
/*
* This is a bit of a special case since CONNECTION_CLOSE can appear in
* most packet types, and when we decide we want to send it this status
* isn't tied to a specific EL. So if we want to send it, we send it
* only on the lowest non-dropped EL.
*/
return 1;
/* Does the CFQ have any frames queued for this PN space? */
if (enc_level != QUIC_ENC_LEVEL_0RTT)
for (cfq_item = ossl_quic_cfq_get_priority_head(txp->args.cfq, pn_space);
cfq_item != NULL;
cfq_item = ossl_quic_cfq_item_get_priority_next(cfq_item, pn_space)) {
uint64_t frame_type = ossl_quic_cfq_item_get_frame_type(cfq_item);
switch (frame_type) {
case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
if (a.allow_new_conn_id)
return 1;
break;
case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
if (a.allow_retire_conn_id)
return 1;
break;
case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
if (a.allow_new_token)
return 1;
break;
default:
if (a.allow_cfq_other)
return 1;
break;
}
}
if (a.allow_stream_rel && txp->handshake_complete) {
QUIC_STREAM_ITER it;
/* If there are any active streams, 0/1-RTT wants to produce a packet.
* Whether a stream is on the active list is required to be precise
* (i.e., a stream is never on the active list if we cannot produce a
* frame for it), and all stream-related frames are governed by
* a.allow_stream_rel (i.e., if we can send one type of stream-related
* frame, we can send any of them), so we don't need to inspect
* individual streams on the active list, just confirm that the active
* list is non-empty.
*/
ossl_quic_stream_iter_init(&it, txp->args.qsm, 0);
if (it.stream != NULL)
return 1;
}
return 0;
}
static int sstream_is_pending(QUIC_SSTREAM *sstream)
{
OSSL_QUIC_FRAME_STREAM hdr;
OSSL_QTX_IOVEC iov[2];
size_t num_iov = OSSL_NELEM(iov);
return ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov, &num_iov);
}
/*
* Generates a packet for a given EL, coalescing it into the current datagram.
*
* is_last_in_dgram and dgram_contains_initial are used to determine padding
* requirements.
*
* Returns TXP_ERR_* value.
*/
static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp, uint32_t enc_level,
uint32_t archetype,
int cc_can_send,
int is_last_in_dgram,
int dgram_contains_initial,
int chosen_for_conn_close,
QUIC_TXP_STATUS *status)
{
int must_pad = dgram_contains_initial && is_last_in_dgram;
size_t min_dpl, min_pl, min_ppl, cmpl, cmppl, running_total;
size_t mdpl, hdr_len, pkt_overhead, cc_limit;
uint64_t cc_limit_;
QUIC_PKT_HDR phdr;
/* Determine the limit CC imposes on what we can send. */
if (!cc_can_send) {
/*
* If we are called when we cannot send, this must be because we want
* to generate a probe. In this circumstance, don't clamp based on CC.
*/
cc_limit = SIZE_MAX;
} else {
/* Allow CC to clamp how much we can send. */
cc_limit_ = txp->args.cc_method->get_tx_allowance(txp->args.cc_data);
cc_limit = (cc_limit_ > SIZE_MAX ? SIZE_MAX : (size_t)cc_limit_);
}
/* Assemble packet header. */
phdr.type = ossl_quic_enc_level_to_pkt_type(enc_level);
phdr.spin_bit = 0;
phdr.pn_len = txp_determine_pn_len(txp);
phdr.partial = 0;
phdr.fixed = 1;
phdr.version = QUIC_VERSION_1;
phdr.dst_conn_id = txp->args.cur_dcid;
phdr.src_conn_id = txp->args.cur_scid;
/*
* We need to know the length of the payload to get an accurate header
* length for non-1RTT packets, because the Length field found in
* Initial/Handshake/0-RTT packets uses a variable-length encoding. However,
* we don't have a good idea of the length of our payload, because the
* length of the payload depends on the room in the datagram after fitting
* the header, which depends on the size of the header.
*
* In general, it does not matter if a packet is slightly shorter (because
* e.g. we predicted use of a 2-byte length field, but ended up only needing