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