Move channel mutex out of QUIC_CHANNEL for init/teardown flexibility
[openssl.git] / ssl / quic / quic_channel_local.h
1 #ifndef OSSL_QUIC_CHANNEL_LOCAL_H
2 # define OSSL_QUIC_CHANNEL_LOCAL_H
3
4 # include "internal/quic_channel.h"
5
6 # ifndef OPENSSL_NO_QUIC
7
8 /*
9  * QUIC Channel Structure
10  * ======================
11  *
12  * QUIC channel internals. It is intended that only the QUIC_CHANNEL
13  * implementation and the RX depacketiser be allowed to access this structure
14  * directly. As the RX depacketiser has no state of its own and computes over a
15  * QUIC_CHANNEL structure, it can be viewed as an extention of the QUIC_CHANNEL
16  * implementation. While the RX depacketiser could be provided with adequate
17  * accessors to do what it needs, this would weaken the abstraction provided by
18  * the QUIC_CHANNEL to other components; moreover the coupling of the RX
19  * depacketiser to QUIC_CHANNEL internals is too deep and bespoke to make this
20  * desirable.
21  *
22  * Other components should not include this header.
23  */
24 struct quic_channel_st {
25     OSSL_LIB_CTX                    *libctx;
26     const char                      *propq;
27
28     /*
29      * Master synchronisation mutex used for thread assisted mode
30      * synchronisation. We don't own this; the instantiator of the channel
31      * passes it to us and is responsible for freeing it after channel
32      * destruction.
33      */
34     CRYPTO_RWLOCK                   *mutex;
35
36     /*
37      * The associated TLS 1.3 connection data. Used to provide the handshake
38      * layer; its 'network' side is plugged into the crypto stream for each EL
39      * (other than the 0-RTT EL).
40      */
41     QUIC_TLS                        *qtls;
42     SSL                             *tls;
43
44     /*
45      * The transport parameter block we will send or have sent.
46      * Freed after sending or when connection is freed.
47      */
48     unsigned char                   *local_transport_params;
49
50     /* Asynchronous I/O reactor. */
51     QUIC_REACTOR                    rtor;
52
53     /* Our current L4 peer address, if any. */
54     BIO_ADDR                        cur_peer_addr;
55
56     /* Network-side read and write BIOs. */
57     BIO                             *net_rbio, *net_wbio;
58
59     /*
60      * Subcomponents of the connection. All of these components are instantiated
61      * and owned by us.
62      */
63     OSSL_QUIC_TX_PACKETISER         *txp;
64     QUIC_TXPIM                      *txpim;
65     QUIC_CFQ                        *cfq;
66     /* Connection level FC. */
67     QUIC_TXFC                       conn_txfc;
68     QUIC_RXFC                       conn_rxfc;
69     QUIC_STREAM_MAP                 qsm;
70     OSSL_STATM                      statm;
71     OSSL_CC_DATA                    *cc_data;
72     const OSSL_CC_METHOD            *cc_method;
73     OSSL_ACKM                       *ackm;
74
75     /*
76      * RX demuxer. We register incoming DCIDs with this. Since we currently only
77      * support client operation and use one L4 port per connection, we own the
78      * demuxer and register a single zero-length DCID with it.
79      */
80     QUIC_DEMUX                      *demux;
81
82     /* Record layers in the TX and RX directions, plus the RX demuxer. */
83     OSSL_QTX                        *qtx;
84     OSSL_QRX                        *qrx;
85
86     /*
87      * Send and receive parts of the crypto streams.
88      * crypto_send[QUIC_PN_SPACE_APP] is the 1-RTT crypto stream. There is no
89      * 0-RTT crypto stream.
90      */
91     QUIC_SSTREAM                    *crypto_send[QUIC_PN_SPACE_NUM];
92     QUIC_RSTREAM                    *crypto_recv[QUIC_PN_SPACE_NUM];
93
94     /*
95      * Our (currently only) application data stream. This is a bidirectional
96      * client-initiated stream and thus (in QUICv1) always has a stream ID of 0.
97      */
98     QUIC_STREAM                     *stream0;
99
100     /* Internal state. */
101     /*
102      * Client: The DCID used in the first Initial packet we transmit as a client.
103      * Server: The DCID used in the first Initial packet the client transmitted.
104      * Randomly generated and required by RFC to be at least 8 bytes.
105      */
106     QUIC_CONN_ID                    init_dcid;
107
108     /*
109      * Client: The SCID found in the first Initial packet from the server.
110      * Not valid for servers.
111      * Valid if have_received_enc_pkt is set.
112      */
113     QUIC_CONN_ID                    init_scid;
114
115     /*
116      * Client only: The SCID found in an incoming Retry packet we handled.
117      * Not valid for servers.
118      */
119     QUIC_CONN_ID                    retry_scid;
120
121     /* Server only: The DCID we currently use to talk to the peer. */
122     QUIC_CONN_ID                    cur_remote_dcid;
123     /* Server only: The DCID we currently expect the peer to use to talk to us. */
124     QUIC_CONN_ID                    cur_local_dcid;
125
126     /* Transport parameter values received from server. */
127     uint64_t                        init_max_stream_data_bidi_local;
128     uint64_t                        init_max_stream_data_bidi_remote;
129     uint64_t                        init_max_stream_data_uni_remote;
130     uint64_t                        rx_max_ack_delay; /* ms */
131     unsigned char                   rx_ack_delay_exp;
132
133     /*
134      * Temporary staging area to store information about the incoming packet we
135      * are currently processing.
136      */
137     OSSL_QRX_PKT                    *qrx_pkt;
138
139     /*
140      * Current limit on number of streams we may create. Set by transport
141      * parameters initially and then by MAX_STREAMS frames.
142      */
143     uint64_t                        max_local_streams_bidi;
144     uint64_t                        max_local_streams_uni;
145
146     /* The negotiated maximum idle timeout in milliseconds. */
147     uint64_t                        max_idle_timeout;
148
149     /*
150      * Maximum payload size in bytes for datagrams sent to our peer, as
151      * negotiated by transport parameters.
152      */
153     uint64_t                        rx_max_udp_payload_size;
154     /* Maximum active CID limit, as negotiated by transport parameters. */
155     uint64_t                        rx_active_conn_id_limit;
156
157     /* Valid if we are in the TERMINATING or TERMINATED states. */
158     QUIC_TERMINATE_CAUSE            terminate_cause;
159
160     /*
161      * Deadline at which we move to TERMINATING state. Valid if in the
162      * TERMINATING state.
163      */
164     OSSL_TIME                       terminate_deadline;
165
166     /*
167      * Deadline at which connection dies due to idle timeout if no further
168      * events occur.
169      */
170     OSSL_TIME                       idle_deadline;
171
172     /*
173      * State tracking. QUIC connection-level state is best represented based on
174      * whether various things have happened yet or not, rather than as an
175      * explicit FSM. We do have a coarse state variable which tracks the basic
176      * state of the connection's lifecycle, but more fine-grained conditions of
177      * the Active state are tracked via flags below. For more details, see
178      * doc/designs/quic-design/connection-state-machine.md. We are in the Open
179      * state if the state is QUIC_CSM_STATE_ACTIVE and handshake_confirmed is
180      * set.
181      */
182     unsigned int                    state                   : 3;
183
184     /*
185      * Have we received at least one encrypted packet from the peer?
186      * (If so, Retry and Version Negotiation messages should no longer
187      *  be received and should be ignored if they do occur.)
188      */
189     unsigned int                    have_received_enc_pkt   : 1;
190
191     /*
192      * Have we sent literally any packet yet? If not, there is no point polling
193      * RX.
194      */
195     unsigned int                    have_sent_any_pkt       : 1;
196
197     /*
198      * Are we currently doing proactive version negotiation?
199      */
200     unsigned int                    doing_proactive_ver_neg : 1;
201
202     /* We have received transport parameters from the peer. */
203     unsigned int                    got_remote_transport_params    : 1;
204
205     /*
206      * This monotonically transitions to 1 once the TLS state machine is
207      * 'complete', meaning that it has both sent a Finished and successfully
208      * verified the peer's Finished (see RFC 9001 s. 4.1.1). Note that it
209      * does not transition to 1 at both peers simultaneously.
210      *
211      * Handshake completion is not the same as handshake confirmation (see
212      * below).
213      */
214     unsigned int                    handshake_complete      : 1;
215
216     /*
217      * This monotonically transitions to 1 once the handshake is confirmed.
218      * This happens on the client when we receive a HANDSHAKE_DONE frame.
219      * At our option, we may also take acknowledgement of any 1-RTT packet
220      * we sent as a handshake confirmation.
221      */
222     unsigned int                    handshake_confirmed     : 1;
223
224     /*
225      * We are sending Initial packets based on a Retry. This means we definitely
226      * should not receive another Retry, and if we do it is an error.
227      */
228     unsigned int                    doing_retry             : 1;
229
230     /*
231      * We don't store the current EL here; the TXP asks the QTX which ELs
232      * are provisioned to determine which ELs to use.
233      */
234
235     /* Have statm, qsm been initialised? Used to track cleanup. */
236     unsigned int                    have_statm              : 1;
237     unsigned int                    have_qsm                : 1;
238
239     /*
240      * Preferred ELs for transmission and reception. This is not strictly needed
241      * as it can be inferred from what keys we have provisioned, but makes
242      * determining the current EL simpler and faster. A separate EL for
243      * transmission and reception is not strictly necessary but makes things
244      * easier for interoperation with the handshake layer, which likes to invoke
245      * the yield secret callback at different times for TX and RX.
246      */
247     unsigned int                    tx_enc_level            : 3;
248     unsigned int                    rx_enc_level            : 3;
249
250     /* If bit n is set, EL n has been discarded. */
251     unsigned int                    el_discarded            : 4;
252
253     /*
254      * While in TERMINATING - CLOSING, set when we should generate a connection
255      * close frame.
256      */
257     unsigned int                    conn_close_queued       : 1;
258
259     /* Are we in server mode? Never changes after instantiation. */
260     unsigned int                    is_server               : 1;
261
262     /*
263      * Set temporarily when the handshake layer has given us a new RX secret.
264      * Used to determine if we need to check our RX queues again.
265      */
266     unsigned int                    have_new_rx_secret      : 1;
267 };
268
269 # endif
270
271 #endif