QUIC APL, TSERVER: Start using a QUIC_ENGINE object
[openssl.git] / include / internal / quic_txp.h
1 /*
2  * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef OSSL_QUIC_TXP_H
11 # define OSSL_QUIC_TXP_H
12
13 # include <openssl/ssl.h>
14 # include "internal/quic_types.h"
15 # include "internal/quic_predef.h"
16 # include "internal/quic_record_tx.h"
17 # include "internal/quic_cfq.h"
18 # include "internal/quic_txpim.h"
19 # include "internal/quic_stream.h"
20 # include "internal/quic_stream_map.h"
21 # include "internal/quic_fc.h"
22 # include "internal/bio_addr.h"
23 # include "internal/time.h"
24
25 # ifndef OPENSSL_NO_QUIC
26
27 /*
28  * QUIC TX Packetiser
29  * ==================
30  */
31 typedef struct ossl_quic_tx_packetiser_args_st {
32     /* Configuration Settings */
33     QUIC_CONN_ID    cur_scid;   /* Current Source Connection ID we use. */
34     QUIC_CONN_ID    cur_dcid;   /* Current Destination Connection ID we use. */
35     BIO_ADDR        peer;       /* Current destination L4 address we use. */
36     uint32_t        ack_delay_exponent; /* ACK delay exponent used when encoding. */
37
38     /* Injected Dependencies */
39     OSSL_QTX        *qtx;       /* QUIC Record Layer TX we are using */
40     QUIC_TXPIM      *txpim;     /* QUIC TX'd Packet Information Manager */
41     QUIC_CFQ        *cfq;       /* QUIC Control Frame Queue */
42     OSSL_ACKM       *ackm;      /* QUIC Acknowledgement Manager */
43     QUIC_STREAM_MAP *qsm;       /* QUIC Streams Map */
44     QUIC_TXFC       *conn_txfc; /* QUIC Connection-Level TX Flow Controller */
45     QUIC_RXFC       *conn_rxfc; /* QUIC Connection-Level RX Flow Controller */
46     QUIC_RXFC       *max_streams_bidi_rxfc; /* QUIC RXFC for MAX_STREAMS generation */
47     QUIC_RXFC       *max_streams_uni_rxfc;
48     const OSSL_CC_METHOD *cc_method; /* QUIC Congestion Controller */
49     OSSL_CC_DATA    *cc_data;   /* QUIC Congestion Controller Instance */
50     OSSL_TIME       (*now)(void *arg);  /* Callback to get current time. */
51     void            *now_arg;
52
53     /*
54      * Injected dependencies - crypto streams.
55      *
56      * Note: There is no crypto stream for the 0-RTT EL.
57      *       crypto[QUIC_PN_SPACE_APP] is the 1-RTT crypto stream.
58      */
59     QUIC_SSTREAM    *crypto[QUIC_PN_SPACE_NUM];
60
61  } OSSL_QUIC_TX_PACKETISER_ARGS;
62
63 OSSL_QUIC_TX_PACKETISER *ossl_quic_tx_packetiser_new(const OSSL_QUIC_TX_PACKETISER_ARGS *args);
64
65 typedef void (ossl_quic_initial_token_free_fn)(const unsigned char *buf,
66                                                size_t buf_len, void *arg);
67
68 void ossl_quic_tx_packetiser_free(OSSL_QUIC_TX_PACKETISER *txp);
69
70 /*
71  * When in the closing state we need to maintain a count of received bytes
72  * so that we can limit the number of close connection frames we send.
73  * Refer RFC 9000 s. 10.2.1 Closing Connection State.
74  */
75 void ossl_quic_tx_packetiser_record_received_closing_bytes(
76         OSSL_QUIC_TX_PACKETISER *txp, size_t n);
77
78 /*
79  * Generates a datagram by polling the various ELs to determine if they want to
80  * generate any frames, and generating a datagram which coalesces packets for
81  * any ELs which do.
82  *
83  * Returns 0 on failure (e.g. allocation error or other errors), 1 otherwise.
84  *
85  * *status is filled with status information about the generated packet.
86  * It is always filled even in case of failure. In particular, packets can be
87  * sent even if failure is later returned.
88  * See QUIC_TXP_STATUS for details.
89  */
90 typedef struct quic_txp_status_st {
91     int sent_ack_eliciting; /* Was an ACK-eliciting packet sent? */
92     int sent_handshake; /* Was a Handshake packet sent? */
93     size_t sent_pkt; /* Number of packets sent (0 if nothing was sent) */
94 } QUIC_TXP_STATUS;
95
96 int ossl_quic_tx_packetiser_generate(OSSL_QUIC_TX_PACKETISER *txp,
97                                      QUIC_TXP_STATUS *status);
98
99 /*
100  * Returns a deadline after which a call to ossl_quic_tx_packetiser_generate()
101  * might succeed even if it did not previously. This may return
102  * ossl_time_infinite() if there is no such deadline currently applicable. It
103  * returns ossl_time_zero() if there is (potentially) more data to be generated
104  * immediately. The value returned is liable to change after any call to
105  * ossl_quic_tx_packetiser_generate() (or after ACKM or CC state changes). Note
106  * that ossl_quic_tx_packetiser_generate() can also start to succeed for other
107  * non-chronological reasons, such as changes to send stream buffers, etc.
108  */
109 OSSL_TIME ossl_quic_tx_packetiser_get_deadline(OSSL_QUIC_TX_PACKETISER *txp);
110
111 /*
112  * Set the token used in Initial packets. The callback is called when the buffer
113  * is no longer needed; for example, when the TXP is freed or when this function
114  * is called again with a new buffer. Fails returning 0 if the token is too big
115  * to ever be reasonably encapsulated in an outgoing packet based on our current
116  * understanding of our PMTU.
117  */
118 int ossl_quic_tx_packetiser_set_initial_token(OSSL_QUIC_TX_PACKETISER *txp,
119                                               const unsigned char *token,
120                                               size_t token_len,
121                                               ossl_quic_initial_token_free_fn *free_cb,
122                                               void *free_cb_arg);
123
124 /* Change the DCID the TXP uses to send outgoing packets. */
125 int ossl_quic_tx_packetiser_set_cur_dcid(OSSL_QUIC_TX_PACKETISER *txp,
126                                          const QUIC_CONN_ID *dcid);
127
128 /* Change the SCID the TXP uses to send outgoing (long) packets. */
129 int ossl_quic_tx_packetiser_set_cur_scid(OSSL_QUIC_TX_PACKETISER *txp,
130                                          const QUIC_CONN_ID *scid);
131
132 /*
133  * Change the destination L4 address the TXP uses to send datagrams. Specify
134  * NULL (or AF_UNSPEC) to disable use of addressed mode.
135  */
136 int ossl_quic_tx_packetiser_set_peer(OSSL_QUIC_TX_PACKETISER *txp,
137                                      const BIO_ADDR *peer);
138
139 /*
140  * Inform the TX packetiser that an EL has been discarded. Idempotent.
141  *
142  * This does not inform the QTX as well; the caller must also inform the QTX.
143  *
144  * The TXP will no longer reference the crypto[enc_level] QUIC_SSTREAM which was
145  * provided in the TXP arguments. However, it is the callers responsibility to
146  * free that QUIC_SSTREAM if desired.
147  */
148 int ossl_quic_tx_packetiser_discard_enc_level(OSSL_QUIC_TX_PACKETISER *txp,
149                                               uint32_t enc_level);
150
151 /*
152  * Informs the TX packetiser that the handshake is complete. The TX packetiser
153  * will not send 1-RTT application data until the handshake is complete,
154  * as the authenticity of the peer is not confirmed until the handshake
155  * complete event occurs.
156  */
157 void ossl_quic_tx_packetiser_notify_handshake_complete(OSSL_QUIC_TX_PACKETISER *txp);
158
159 /* Asks the TXP to generate a HANDSHAKE_DONE frame in the next 1-RTT packet. */
160 void ossl_quic_tx_packetiser_schedule_handshake_done(OSSL_QUIC_TX_PACKETISER *txp);
161
162 /* Asks the TXP to ensure the next packet in the given PN space is ACK-eliciting. */
163 void ossl_quic_tx_packetiser_schedule_ack_eliciting(OSSL_QUIC_TX_PACKETISER *txp,
164                                                     uint32_t pn_space);
165
166 /*
167  * Asks the TXP to ensure an ACK is put in the next packet in the given PN
168  * space.
169  */
170 void ossl_quic_tx_packetiser_schedule_ack(OSSL_QUIC_TX_PACKETISER *txp,
171                                           uint32_t pn_space);
172
173 /*
174  * Schedules a connection close. *f and f->reason are copied. This operation is
175  * irreversible and causes all further packets generated by the TXP to contain a
176  * CONNECTION_CLOSE frame. This function fails if it has already been called
177  * successfully; the information in *f cannot be changed after the first
178  * successful call to this function.
179  */
180 int ossl_quic_tx_packetiser_schedule_conn_close(OSSL_QUIC_TX_PACKETISER *txp,
181                                                 const OSSL_QUIC_FRAME_CONN_CLOSE *f);
182
183 /* Setters for the msg_callback and msg_callback_arg */
184 void ossl_quic_tx_packetiser_set_msg_callback(OSSL_QUIC_TX_PACKETISER *txp,
185                                               ossl_msg_cb msg_callback,
186                                               SSL *msg_callback_ssl);
187 void ossl_quic_tx_packetiser_set_msg_callback_arg(OSSL_QUIC_TX_PACKETISER *txp,
188                                                   void *msg_callback_arg);
189
190 /*
191  * Determines the next PN which will be used for a given PN space.
192  */
193 QUIC_PN ossl_quic_tx_packetiser_get_next_pn(OSSL_QUIC_TX_PACKETISER *txp,
194                                             uint32_t pn_space);
195
196 /*
197  * Sets a callback which is called whenever TXP sends an ACK frame. The callee
198  * must not modify the ACK frame data. Can be used to snoop on PNs being ACKed.
199  */
200 void ossl_quic_tx_packetiser_set_ack_tx_cb(OSSL_QUIC_TX_PACKETISER *txp,
201                                            void (*cb)(const OSSL_QUIC_FRAME_ACK *ack,
202                                                       uint32_t pn_space,
203                                                       void *arg),
204                                            void *cb_arg);
205
206 # endif
207
208 #endif