2 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef OSSL_QUIC_CHANNEL_H
11 # define OSSL_QUIC_CHANNEL_H
13 # include <openssl/ssl.h>
14 # include "internal/quic_types.h"
15 # include "internal/quic_record_tx.h"
16 # include "internal/quic_wire.h"
17 # include "internal/quic_predef.h"
18 # include "internal/time.h"
19 # include "internal/thread.h"
21 # ifndef OPENSSL_NO_QUIC
27 * A QUIC channel (QUIC_CHANNEL) is an object which binds together all of the
28 * various pieces of QUIC into a single top-level object, and handles connection
29 * state which is not specific to the client or server roles. In particular, it
30 * is strictly separated from the libssl front end I/O API personality layer,
31 * and is not an SSL object.
33 * The name QUIC_CHANNEL is chosen because QUIC_CONNECTION is already in use,
34 * but functionally these relate to the same thing (a QUIC connection). The use
35 * of two separate objects ensures clean separation between the API personality
36 * layer and common code for handling connections, and between the functionality
37 * which is specific to clients and which is specific to servers, and the
38 * functionality which is common to both.
40 * The API personality layer provides SSL objects (e.g. a QUIC_CONNECTION) which
41 * consume a QUIC channel and implement a specific public API. Things which are
42 * handled by the API personality layer include emulation of blocking semantics,
43 * handling of SSL object mode flags like non-partial write mode, etc.
45 * Where the QUIC_CHANNEL is used in a server role, there is one QUIC_CHANNEL
46 * per connection. In the future a QUIC Channel Manager will probably be defined
47 * to handle ownership of resources which are shared between connections (e.g.
48 * demuxers). Since we only use server-side functionality for dummy test servers
49 * for now, which only need to handle one connection at a time, this is not
55 * To support thread assisted mode, QUIC_CHANNEL can be used by multiple
56 * threads. **It is the caller's responsibility to ensure that the QUIC_CHANNEL
57 * is only accessed (whether via its methods or via direct access to its state)
58 * while the channel mutex is held**, except for methods explicitly marked as
59 * not requiring prior locking. This is an unchecked precondition.
61 * The instantiator of the channel is responsible for providing a suitable
62 * mutex which then serves as the channel mutex; see QUIC_CHANNEL_ARGS.
66 * The function does not acquire the channel mutex and assumes it is already
67 * held by the calling thread.
69 * Any function tagged with this has the following precondition:
71 * Precondition: must hold channel mutex (unchecked)
73 # define QUIC_NEEDS_LOCK
76 * The function acquires the channel mutex and releases it before returning in
79 * Any function tagged with this has the following precondition and
82 * Precondition: must not hold channel mutex (unchecked)
83 * Postcondition: channel mutex is not held (by calling thread)
85 # define QUIC_TAKES_LOCK
88 * The function acquires the channel mutex and leaves it acquired
89 * when returning success.
91 * Any function tagged with this has the following precondition and
94 * Precondition: must not hold channel mutex (unchecked)
95 * Postcondition: channel mutex is held by calling thread
96 * or function returned failure
98 # define QUIC_ACQUIRES_LOCK
100 # define QUIC_TODO_LOCK
102 # define QUIC_CHANNEL_STATE_IDLE 0
103 # define QUIC_CHANNEL_STATE_ACTIVE 1
104 # define QUIC_CHANNEL_STATE_TERMINATING_CLOSING 2
105 # define QUIC_CHANNEL_STATE_TERMINATING_DRAINING 3
106 # define QUIC_CHANNEL_STATE_TERMINATED 4
108 typedef struct quic_channel_args_st
{
110 * The QUIC_PORT which the channel is to belong to. The lifetime of the
111 * QUIC_PORT must exceed that of the created channel.
119 typedef struct quic_channel_st QUIC_CHANNEL
;
121 /* Represents the cause for a connection's termination. */
122 typedef struct quic_terminate_cause_st
{
124 * If we are in a TERMINATING or TERMINATED state, this is the error code
125 * associated with the error. This field is valid iff we are in the
126 * TERMINATING or TERMINATED states.
131 * If terminate_app is set and this is nonzero, this is the frame type which
132 * caused the connection to be terminated.
137 * Optional reason string. When calling ossl_quic_channel_local_close, if a
138 * reason string pointer is passed, it is copied and stored inside
139 * QUIC_CHANNEL for the remainder of the lifetime of the channel object.
140 * Thus the string pointed to by this value, if non-NULL, is valid for the
141 * lifetime of the QUIC_CHANNEL object.
146 * Length of reason in bytes. The reason is supposed to contain a UTF-8
147 * string but may be arbitrary data if the reason came from the network.
151 /* Is this error code in the transport (0) or application (1) space? */
152 unsigned int app
: 1;
155 * If set, the cause of the termination is a received CONNECTION_CLOSE
156 * frame. Otherwise, we decided to terminate ourselves and sent a
157 * CONNECTION_CLOSE frame (regardless of whether the peer later also sends
160 unsigned int remote
: 1;
161 } QUIC_TERMINATE_CAUSE
;
165 * Create a new QUIC channel using the given arguments. The argument structure
166 * does not need to remain allocated. Returns NULL on failure.
168 * Only QUIC_PORT should use this function.
170 QUIC_CHANNEL
*ossl_quic_channel_new(const QUIC_CHANNEL_ARGS
*args
);
172 /* No-op if ch is NULL. */
173 void ossl_quic_channel_free(QUIC_CHANNEL
*ch
);
175 /* Set mutator callbacks for test framework support */
176 int ossl_quic_channel_set_mutator(QUIC_CHANNEL
*ch
,
177 ossl_mutate_packet_cb mutatecb
,
178 ossl_finish_mutate_cb finishmutatecb
,
182 * Connection Lifecycle Events
183 * ===========================
185 * Various events that can be raised on the channel by other parts of the QUIC
186 * implementation. Some of these are suitable for general use by any part of the
187 * code (e.g. ossl_quic_channel_raise_protocol_error), others are for very
188 * specific use by particular components only (e.g.
189 * ossl_quic_channel_on_handshake_confirmed).
193 * To be used by a QUIC connection. Starts the channel. For a client-mode
194 * channel, this starts sending the first handshake layer message, etc. Can only
195 * be called in the idle state; successive calls are ignored.
197 int ossl_quic_channel_start(QUIC_CHANNEL
*ch
);
199 /* Start a locally initiated connection shutdown. */
200 void ossl_quic_channel_local_close(QUIC_CHANNEL
*ch
, uint64_t app_error_code
,
201 const char *app_reason
);
204 * Called when the handshake is confirmed.
206 int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL
*ch
);
209 * Raises a protocol error. This is intended to be the universal call suitable
210 * for handling of all peer-triggered protocol violations or errors detected by
211 * us. We specify a QUIC transport-scope error code and optional frame type
212 * which was responsible. If a frame type is not applicable, specify zero. The
213 * reason string is not currently handled, but should be a string of static
214 * storage duration. If the connection has already terminated due to a previous
215 * protocol error, this is a no-op; first error wins.
217 * Usually the ossl_quic_channel_raise_protocol_error() function should be used.
218 * The ossl_quic_channel_raise_protocol_error_loc() function can be used
219 * directly for passing through existing call site information from an existing
222 void ossl_quic_channel_raise_protocol_error_loc(QUIC_CHANNEL
*ch
,
226 ERR_STATE
*err_state
,
227 const char *src_file
,
229 const char *src_func
);
231 #define ossl_quic_channel_raise_protocol_error(ch, error_code, frame_type, reason) \
232 ossl_quic_channel_raise_protocol_error_loc((ch), (error_code), \
240 #define ossl_quic_channel_raise_protocol_error_state(ch, error_code, frame_type, reason, state) \
241 ossl_quic_channel_raise_protocol_error_loc((ch), (error_code), \
251 * Returns 1 if permanent net error was detected on the QUIC_CHANNEL,
254 int ossl_quic_channel_net_error(QUIC_CHANNEL
*ch
);
256 /* Restore saved error state (best effort) */
257 void ossl_quic_channel_restore_err_state(QUIC_CHANNEL
*ch
);
260 void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL
*ch
,
261 OSSL_QUIC_FRAME_CONN_CLOSE
*f
);
262 void ossl_quic_channel_on_new_conn_id(QUIC_CHANNEL
*ch
,
263 OSSL_QUIC_FRAME_NEW_CONN_ID
*f
);
265 /* Temporarily exposed during QUIC_PORT transition. */
266 int ossl_quic_channel_on_new_conn(QUIC_CHANNEL
*ch
, const BIO_ADDR
*peer
,
267 const QUIC_CONN_ID
*peer_scid
,
268 const QUIC_CONN_ID
*peer_dcid
);
270 /* For use by QUIC_PORT. You should not need to call this directly. */
271 void ossl_quic_channel_subtick(QUIC_CHANNEL
*ch
, QUIC_TICK_RESULT
*r
,
274 /* For use by QUIC_PORT only. */
275 void ossl_quic_channel_raise_net_error(QUIC_CHANNEL
*ch
);
278 * Queries and Accessors
279 * =====================
282 /* Gets the reactor which can be used to tick/poll on the channel. */
283 QUIC_REACTOR
*ossl_quic_channel_get_reactor(QUIC_CHANNEL
*ch
);
285 /* Gets the QSM used with the channel. */
286 QUIC_STREAM_MAP
*ossl_quic_channel_get_qsm(QUIC_CHANNEL
*ch
);
288 /* Gets the statistics manager used with the channel. */
289 OSSL_STATM
*ossl_quic_channel_get_statm(QUIC_CHANNEL
*ch
);
292 * Gets/sets the current peer address. Generally this should be used before
293 * starting a channel in client mode.
295 int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL
*ch
, BIO_ADDR
*peer_addr
);
296 int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL
*ch
, const BIO_ADDR
*peer_addr
);
298 /* Gets/sets the underlying network read and write BIOs. */
299 BIO
*ossl_quic_channel_get_net_rbio(QUIC_CHANNEL
*ch
);
300 BIO
*ossl_quic_channel_get_net_wbio(QUIC_CHANNEL
*ch
);
301 int ossl_quic_channel_set_net_rbio(QUIC_CHANNEL
*ch
, BIO
*net_rbio
);
302 int ossl_quic_channel_set_net_wbio(QUIC_CHANNEL
*ch
, BIO
*net_wbio
);
305 * Returns an existing stream by stream ID. Returns NULL if the stream does not
308 QUIC_STREAM
*ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL
*ch
,
311 /* Returns 1 if channel is terminating or terminated. */
312 int ossl_quic_channel_is_term_any(const QUIC_CHANNEL
*ch
);
313 const QUIC_TERMINATE_CAUSE
*
314 ossl_quic_channel_get_terminate_cause(const QUIC_CHANNEL
*ch
);
315 int ossl_quic_channel_is_closing(const QUIC_CHANNEL
*ch
);
316 int ossl_quic_channel_is_terminated(const QUIC_CHANNEL
*ch
);
317 int ossl_quic_channel_is_active(const QUIC_CHANNEL
*ch
);
318 int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL
*ch
);
319 int ossl_quic_channel_is_handshake_confirmed(const QUIC_CHANNEL
*ch
);
321 QUIC_PORT
*ossl_quic_channel_get0_port(QUIC_CHANNEL
*ch
);
322 QUIC_DEMUX
*ossl_quic_channel_get0_demux(QUIC_CHANNEL
*ch
);
324 SSL
*ossl_quic_channel_get0_ssl(QUIC_CHANNEL
*ch
);
327 * Retrieves a pointer to the channel mutex which was provided at the time the
328 * channel was instantiated. In order to allow locks to be acquired and released
329 * with the correct granularity, it is the caller's responsibility to ensure
330 * this lock is held for write while calling any QUIC_CHANNEL method, except for
331 * methods explicitly designed otherwise.
333 * This method is thread safe and does not require prior locking. It can also be
334 * called while the lock is already held. Note that this is simply a convenience
335 * function to access the mutex which was passed to the channel at instantiation
336 * time; it does not belong to the channel but rather is presumed to belong to
337 * the owner of the channel.
339 CRYPTO_MUTEX
*ossl_quic_channel_get_mutex(QUIC_CHANNEL
*ch
);
342 * Creates a new locally-initiated stream in the stream mapper, choosing an
343 * appropriate stream ID. If is_uni is 1, creates a unidirectional stream, else
344 * creates a bidirectional stream. Returns NULL on failure.
346 QUIC_STREAM
*ossl_quic_channel_new_stream_local(QUIC_CHANNEL
*ch
, int is_uni
);
349 * Creates a new remotely-initiated stream in the stream mapper. The stream ID
350 * is used to confirm the initiator and determine the stream type. The stream is
351 * automatically added to the QSM's accept queue. A pointer to the stream is
352 * also returned. Returns NULL on failure.
354 QUIC_STREAM
*ossl_quic_channel_new_stream_remote(QUIC_CHANNEL
*ch
,
358 * Configures incoming stream auto-reject. If enabled, incoming streams have
359 * both their sending and receiving parts automatically rejected using
360 * STOP_SENDING and STREAM_RESET frames. aec is the application error
361 * code to be used for those frames.
363 void ossl_quic_channel_set_incoming_stream_auto_reject(QUIC_CHANNEL
*ch
,
368 * Causes the channel to reject the sending and receiving parts of a stream,
369 * as though autorejected. Can be used if a stream has already been
372 void ossl_quic_channel_reject_stream(QUIC_CHANNEL
*ch
, QUIC_STREAM
*qs
);
374 /* Replace local connection ID in TXP and DEMUX for testing purposes. */
375 int ossl_quic_channel_replace_local_cid(QUIC_CHANNEL
*ch
,
376 const QUIC_CONN_ID
*conn_id
);
378 /* Setters for the msg_callback and msg_callback_arg */
379 void ossl_quic_channel_set_msg_callback(QUIC_CHANNEL
*ch
,
380 ossl_msg_cb msg_callback
,
381 SSL
*msg_callback_ssl
);
382 void ossl_quic_channel_set_msg_callback_arg(QUIC_CHANNEL
*ch
,
383 void *msg_callback_arg
);
385 /* Testing use only - sets a TXKU threshold packet count override value. */
386 void ossl_quic_channel_set_txku_threshold_override(QUIC_CHANNEL
*ch
,
387 uint64_t tx_pkt_threshold
);
389 /* Testing use only - gets current 1-RTT key epochs for QTX and QRX. */
390 uint64_t ossl_quic_channel_get_tx_key_epoch(QUIC_CHANNEL
*ch
);
391 uint64_t ossl_quic_channel_get_rx_key_epoch(QUIC_CHANNEL
*ch
);
393 /* Artificially trigger a spontaneous TXKU if possible. */
394 int ossl_quic_channel_trigger_txku(QUIC_CHANNEL
*ch
);
395 int ossl_quic_channel_has_pending(const QUIC_CHANNEL
*ch
);
397 /* Force transmission of an ACK-eliciting packet. */
398 int ossl_quic_channel_ping(QUIC_CHANNEL
*ch
);
401 * These queries exist for diagnostic purposes only. They may roll over.
402 * Do not rely on them for non-testing purposes.
404 uint16_t ossl_quic_channel_get_diag_num_rx_ack(QUIC_CHANNEL
*ch
);
407 * Diagnostic use only. Gets the current local CID.
409 void ossl_quic_channel_get_diag_local_cid(QUIC_CHANNEL
*ch
, QUIC_CONN_ID
*cid
);
412 * Returns 1 if stream count flow control allows us to create a new
413 * locally-initiated stream.
415 int ossl_quic_channel_is_new_local_stream_admissible(QUIC_CHANNEL
*ch
, int is_uni
);