]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/internal/quic_channel.h
QUIC CHANNEL: Handle incoming remotely-created streams
[thirdparty/openssl.git] / include / internal / quic_channel.h
1 /*
2 * Copyright 2022 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_CHANNEL_H
11 # define OSSL_QUIC_CHANNEL_H
12
13 # include <openssl/ssl.h>
14 # include "internal/quic_types.h"
15 # include "internal/quic_stream_map.h"
16 # include "internal/quic_reactor.h"
17 # include "internal/quic_statm.h"
18 # include "internal/time.h"
19 # include "internal/thread.h"
20
21 # ifndef OPENSSL_NO_QUIC
22
23 /*
24 * QUIC Channel
25 * ============
26 *
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.
32 *
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.
39 *
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.
44 *
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
50 * currently modelled.
51 *
52 * Synchronisation
53 * ---------------
54 *
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.
60 *
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.
63 */
64
65 /*
66 * The function does not acquire the channel mutex and assumes it is already
67 * held by the calling thread.
68 *
69 * Any function tagged with this has the following precondition:
70 *
71 * Precondition: must hold channel mutex (unchecked)
72 */
73 # define QUIC_NEEDS_LOCK
74
75 /*
76 * The function acquires the channel mutex and releases it before returning in
77 * all circumstances.
78 *
79 * Any function tagged with this has the following precondition and
80 * postcondition:
81 *
82 * Precondition: must not hold channel mutex (unchecked)
83 * Postcondition: channel mutex is not held (by calling thread)
84 */
85 # define QUIC_TAKES_LOCK
86
87 # define QUIC_TODO_LOCK
88
89 # define QUIC_CHANNEL_STATE_IDLE 0
90 # define QUIC_CHANNEL_STATE_ACTIVE 1
91 # define QUIC_CHANNEL_STATE_TERMINATING_CLOSING 2
92 # define QUIC_CHANNEL_STATE_TERMINATING_DRAINING 3
93 # define QUIC_CHANNEL_STATE_TERMINATED 4
94
95 typedef struct quic_channel_args_st {
96 OSSL_LIB_CTX *libctx;
97 const char *propq;
98 int is_server;
99 SSL *tls;
100
101 /*
102 * This must be a mutex the lifetime of which will exceed that of the
103 * channel. The instantiator of the channel is responsible for providing a
104 * mutex as this makes it easier to handle instantiation and teardown of
105 * channels in situations potentially requiring locking.
106 *
107 * Note that this is a MUTEX not a RWLOCK as it needs to be an OS mutex for
108 * compatibility with an OS's condition variable wait API, whereas RWLOCK
109 * may, depending on the build configuration, be implemented using an OS's
110 * mutex primitive or using its RW mutex primitive.
111 */
112 CRYPTO_MUTEX *mutex;
113
114 /*
115 * Optional function pointer to use to retrieve the current time. If NULL,
116 * ossl_time_now() is used.
117 */
118 OSSL_TIME (*now_cb)(void *arg);
119 void *now_cb_arg;
120 } QUIC_CHANNEL_ARGS;
121
122 typedef struct quic_channel_st QUIC_CHANNEL;
123
124 /* Represents the cause for a connection's termination. */
125 typedef struct quic_terminate_cause_st {
126 /*
127 * If we are in a TERMINATING or TERMINATED state, this is the error code
128 * associated with the error. This field is valid iff we are in the
129 * TERMINATING or TERMINATED states.
130 */
131 uint64_t error_code;
132
133 /*
134 * If terminate_app is set and this is nonzero, this is the frame type which
135 * caused the connection to be terminated.
136 */
137 uint64_t frame_type;
138
139 /* Is this error code in the transport (0) or application (1) space? */
140 unsigned int app : 1;
141
142 /*
143 * If set, the cause of the termination is a received CONNECTION_CLOSE
144 * frame. Otherwise, we decided to terminate ourselves and sent a
145 * CONNECTION_CLOSE frame (regardless of whether the peer later also sends
146 * one).
147 */
148 unsigned int remote : 1;
149 } QUIC_TERMINATE_CAUSE;
150
151
152 /*
153 * Create a new QUIC channel using the given arguments. The argument structure
154 * does not need to remain allocated. Returns NULL on failure.
155 */
156 QUIC_CHANNEL *ossl_quic_channel_new(const QUIC_CHANNEL_ARGS *args);
157
158 /* No-op if ch is NULL. */
159 void ossl_quic_channel_free(QUIC_CHANNEL *ch);
160
161 /* Set mutator callbacks for test framework support */
162 int ossl_quic_channel_set_mutator(QUIC_CHANNEL *ch,
163 ossl_mutate_packet_cb mutatecb,
164 ossl_finish_mutate_cb finishmutatecb,
165 void *mutatearg);
166
167 /*
168 * Connection Lifecycle Events
169 * ===========================
170 *
171 * Various events that can be raised on the channel by other parts of the QUIC
172 * implementation. Some of these are suitable for general use by any part of the
173 * code (e.g. ossl_quic_channel_raise_protocol_error), others are for very
174 * specific use by particular components only (e.g.
175 * ossl_quic_channel_on_handshake_confirmed).
176 */
177
178 /*
179 * To be used by a QUIC connection. Starts the channel. For a client-mode
180 * channel, this starts sending the first handshake layer message, etc. Can only
181 * be called in the idle state; successive calls are ignored.
182 */
183 int ossl_quic_channel_start(QUIC_CHANNEL *ch);
184
185 /* Start a locally initiated connection shutdown. */
186 void ossl_quic_channel_local_close(QUIC_CHANNEL *ch, uint64_t app_error_code);
187
188 /*
189 * Called when the handshake is confirmed.
190 */
191 int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch);
192
193 /*
194 * Raises a protocol error. This is intended to be the universal call suitable
195 * for handling of all peer-triggered protocol violations or errors detected by
196 * us. We specify a QUIC transport-scope error code and optional frame type
197 * which was responsible. If a frame type is not applicable, specify zero. The
198 * reason string is not currently handled, but should be a string of static
199 * storage duration. If the connection has already terminated due to a previous
200 * protocol error, this is a no-op; first error wins.
201 */
202 void ossl_quic_channel_raise_protocol_error(QUIC_CHANNEL *ch,
203 uint64_t error_code,
204 uint64_t frame_type,
205 const char *reason);
206
207 /* For RXDP use. */
208 void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL *ch,
209 OSSL_QUIC_FRAME_CONN_CLOSE *f);
210
211 /*
212 * Queries and Accessors
213 * =====================
214 */
215
216 /* Gets the reactor which can be used to tick/poll on the channel. */
217 QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch);
218
219 /* Gets the QSM used with the channel. */
220 QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch);
221
222 /* Gets the statistics manager used with the channel. */
223 OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch);
224
225 /*
226 * Gets/sets the current peer address. Generally this should be used before
227 * starting a channel in client mode.
228 */
229 int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL *ch, BIO_ADDR *peer_addr);
230 int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL *ch, const BIO_ADDR *peer_addr);
231
232 /* Gets/sets the underlying network read and write BIOs. */
233 BIO *ossl_quic_channel_get_net_rbio(QUIC_CHANNEL *ch);
234 BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch);
235 int ossl_quic_channel_set_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio);
236 int ossl_quic_channel_set_net_wbio(QUIC_CHANNEL *ch, BIO *net_wbio);
237
238 /*
239 * Returns an existing stream by stream ID. Returns NULL if the stream does not
240 * exist.
241 */
242 QUIC_STREAM *ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL *ch,
243 uint64_t stream_id);
244
245 /* Returns 1 if channel is terminating or terminated. */
246 int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch);
247 QUIC_TERMINATE_CAUSE ossl_quic_channel_get_terminate_cause(const QUIC_CHANNEL *ch);
248 int ossl_quic_channel_is_terminating(const QUIC_CHANNEL *ch);
249 int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch);
250 int ossl_quic_channel_is_active(const QUIC_CHANNEL *ch);
251 int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL *ch);
252 int ossl_quic_channel_is_handshake_confirmed(const QUIC_CHANNEL *ch);
253
254 QUIC_DEMUX *ossl_quic_channel_get0_demux(QUIC_CHANNEL *ch);
255
256 SSL *ossl_quic_channel_get0_ssl(QUIC_CHANNEL *ch);
257
258 /*
259 * Retrieves a pointer to the channel mutex which was provided at the time the
260 * channel was instantiated. In order to allow locks to be acquired and released
261 * with the correct granularity, it is the caller's responsibility to ensure
262 * this lock is held for write while calling any QUIC_CHANNEL method, except for
263 * methods explicitly designed otherwise.
264 *
265 * This method is thread safe and does not require prior locking. It can also be
266 * called while the lock is already held. Note that this is simply a convenience
267 * function to access the mutex which was passed to the channel at instantiation
268 * time; it does not belong to the channel but rather is presumed to belong to
269 * the owner of the channel.
270 */
271 CRYPTO_MUTEX *ossl_quic_channel_get_mutex(QUIC_CHANNEL *ch);
272
273 /*
274 * Creates a new locally-initiated stream in the stream mapper, choosing an
275 * appropriate stream ID. If is_uni is 1, creates a unidirectional stream, else
276 * creates a bidirectional stream. Returns NULL on failure.
277 */
278 QUIC_STREAM *ossl_quic_channel_new_stream_local(QUIC_CHANNEL *ch, int is_uni);
279
280 /*
281 * Creates a new remotely-initiated stream in the stream mapper. The stream ID
282 * is used to confirm the initiator and determine the stream type. The stream is
283 * automatically added to the QSM's accept queue. A pointer to the stream is
284 * also returned. Returns NULL on failure.
285 */
286 QUIC_STREAM *ossl_quic_channel_new_stream_remote(QUIC_CHANNEL *ch,
287 uint64_t stream_id);
288
289 # endif
290
291 #endif