]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/internal/quic_channel.h
Move channel mutex out of QUIC_CHANNEL for init/teardown flexibility
[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
20 # ifndef OPENSSL_NO_QUIC
21
22 /*
23 * QUIC Channel
24 * ============
25 *
26 * A QUIC channel (QUIC_CHANNEL) is an object which binds together all of the
27 * various pieces of QUIC into a single top-level object, and handles connection
28 * state which is not specific to the client or server roles. In particular, it
29 * is strictly separated from the libssl front end I/O API personality layer,
30 * and is not an SSL object.
31 *
32 * The name QUIC_CHANNEL is chosen because QUIC_CONNECTION is already in use,
33 * but functionally these relate to the same thing (a QUIC connection). The use
34 * of two separate objects ensures clean separation between the API personality
35 * layer and common code for handling connections, and between the functionality
36 * which is specific to clients and which is specific to servers, and the
37 * functionality which is common to both.
38 *
39 * The API personality layer provides SSL objects (e.g. a QUIC_CONNECTION) which
40 * consume a QUIC channel and implement a specific public API. Things which are
41 * handled by the API personality layer include emulation of blocking semantics,
42 * handling of SSL object mode flags like non-partial write mode, etc.
43 *
44 * Where the QUIC_CHANNEL is used in a server role, there is one QUIC_CHANNEL
45 * per connection. In the future a QUIC Channel Manager will probably be defined
46 * to handle ownership of resources which are shared between connections (e.g.
47 * demuxers). Since we only use server-side functionality for dummy test servers
48 * for now, which only need to handle one connection at a time, this is not
49 * currently modelled.
50 *
51 * Synchronisation
52 * ---------------
53 *
54 * To support thread assisted mode, QUIC_CHANNEL can be used by multiple
55 * threads. **It is the caller's responsibility to ensure that the QUIC_CHANNEL
56 * is only accessed (whether via its methods or via direct access to its state)
57 * while the channel mutex is held**, except for methods explicitly marked as
58 * not requiring prior locking. This is an unchecked precondition.
59 *
60 * The instantiator of the channel is responsible for providing a suitable
61 * mutex which then serves as the channel mutex; see QUIC_CHANNEL_ARGS.
62 */
63
64 # define QUIC_NEEDS_LOCK
65 # define QUIC_TAKES_LOCK
66 # define QUIC_TODO_LOCK
67
68 # define QUIC_CHANNEL_STATE_IDLE 0
69 # define QUIC_CHANNEL_STATE_ACTIVE 1
70 # define QUIC_CHANNEL_STATE_TERMINATING_CLOSING 2
71 # define QUIC_CHANNEL_STATE_TERMINATING_DRAINING 3
72 # define QUIC_CHANNEL_STATE_TERMINATED 4
73
74 typedef struct quic_channel_args_st {
75 OSSL_LIB_CTX *libctx;
76 const char *propq;
77 int is_server;
78 SSL *tls;
79
80 /*
81 * This must be a mutex the lifetime of which will exceed that of the
82 * channel. The instantiator of the channel is responsible for providing a
83 * mutex as this makes it easier to handle instantiation and teardown of
84 * channels in situations potentially requiring locking.
85 */
86 CRYPTO_RWLOCK *mutex;
87 } QUIC_CHANNEL_ARGS;
88
89 typedef struct quic_channel_st QUIC_CHANNEL;
90
91 /* Represents the cause for a connection's termination. */
92 typedef struct quic_terminate_cause_st {
93 /*
94 * If we are in a TERMINATING or TERMINATED state, this is the error code
95 * associated with the error. This field is valid iff we are in the
96 * TERMINATING or TERMINATED states.
97 */
98 uint64_t error_code;
99
100 /*
101 * If terminate_app is set and this is nonzero, this is the frame type which
102 * caused the connection to be terminated.
103 */
104 uint64_t frame_type;
105
106 /* Is this error code in the transport (0) or application (1) space? */
107 unsigned int app : 1;
108
109 /*
110 * If set, the cause of the termination is a received CONNECTION_CLOSE
111 * frame. Otherwise, we decided to terminate ourselves and sent a
112 * CONNECTION_CLOSE frame (regardless of whether the peer later also sends
113 * one).
114 */
115 unsigned int remote : 1;
116 } QUIC_TERMINATE_CAUSE;
117
118
119 /*
120 * Create a new QUIC channel using the given arguments. The argument structure
121 * does not need to remain allocated. Returns NULL on failure.
122 */
123 QUIC_CHANNEL *ossl_quic_channel_new(const QUIC_CHANNEL_ARGS *args);
124
125 /* No-op if ch is NULL. */
126 void ossl_quic_channel_free(QUIC_CHANNEL *ch);
127
128 /* Set mutator callbacks for test framework support */
129 int ossl_quic_channel_set_mutator(QUIC_CHANNEL *ch,
130 ossl_mutate_packet_cb mutatecb,
131 ossl_finish_mutate_cb finishmutatecb,
132 void *mutatearg);
133
134 /*
135 * Connection Lifecycle Events
136 * ===========================
137 *
138 * Various events that can be raised on the channel by other parts of the QUIC
139 * implementation. Some of these are suitable for general use by any part of the
140 * code (e.g. ossl_quic_channel_raise_protocol_error), others are for very
141 * specific use by particular components only (e.g.
142 * ossl_quic_channel_on_handshake_confirmed).
143 */
144
145 /*
146 * To be used by a QUIC connection. Starts the channel. For a client-mode
147 * channel, this starts sending the first handshake layer message, etc. Can only
148 * be called in the idle state; successive calls are ignored.
149 */
150 int ossl_quic_channel_start(QUIC_CHANNEL *ch);
151
152 /* Start a locally initiated connection shutdown. */
153 void ossl_quic_channel_local_close(QUIC_CHANNEL *ch, uint64_t app_error_code);
154
155 /*
156 * Called when the handshake is confirmed.
157 */
158 int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch);
159
160 /*
161 * Raises a protocol error. This is intended to be the universal call suitable
162 * for handling of all peer-triggered protocol violations or errors detected by
163 * us. We specify a QUIC transport-scope error code and optional frame type
164 * which was responsible. If a frame type is not applicable, specify zero. The
165 * reason string is not currently handled, but should be a string of static
166 * storage duration. If the connection has already terminated due to a previous
167 * protocol error, this is a no-op; first error wins.
168 */
169 void ossl_quic_channel_raise_protocol_error(QUIC_CHANNEL *ch,
170 uint64_t error_code,
171 uint64_t frame_type,
172 const char *reason);
173
174 /* For RXDP use. */
175 void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL *ch,
176 OSSL_QUIC_FRAME_CONN_CLOSE *f);
177
178 /*
179 * Queries and Accessors
180 * =====================
181 */
182
183 /* Gets the reactor which can be used to tick/poll on the channel. */
184 QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch);
185
186 /* Gets the QSM used with the channel. */
187 QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch);
188
189 /* Gets the statistics manager used with the channel. */
190 OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch);
191
192 /*
193 * Gets/sets the current peer address. Generally this should be used before
194 * starting a channel in client mode.
195 */
196 int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL *ch, BIO_ADDR *peer_addr);
197 int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL *ch, const BIO_ADDR *peer_addr);
198
199 /* Gets/sets the underlying network read and write BIOs. */
200 BIO *ossl_quic_channel_get_net_rbio(QUIC_CHANNEL *ch);
201 BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch);
202 int ossl_quic_channel_set_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio);
203 int ossl_quic_channel_set_net_wbio(QUIC_CHANNEL *ch, BIO *net_wbio);
204
205 /*
206 * Returns an existing stream by stream ID. Returns NULL if the stream does not
207 * exist.
208 */
209 QUIC_STREAM *ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL *ch,
210 uint64_t stream_id);
211
212 /* Returns 1 if channel is terminating or terminated. */
213 int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch);
214 QUIC_TERMINATE_CAUSE ossl_quic_channel_get_terminate_cause(const QUIC_CHANNEL *ch);
215 int ossl_quic_channel_is_terminating(const QUIC_CHANNEL *ch);
216 int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch);
217 int ossl_quic_channel_is_active(const QUIC_CHANNEL *ch);
218 int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL *ch);
219 int ossl_quic_channel_is_handshake_confirmed(const QUIC_CHANNEL *ch);
220
221 QUIC_DEMUX *ossl_quic_channel_get0_demux(QUIC_CHANNEL *ch);
222
223 SSL *ossl_quic_channel_get0_ssl(QUIC_CHANNEL *ch);
224
225 /*
226 * Retrieves a pointer to the channel mutex which was provided at the time the
227 * channel was instantiated. In order to allow locks to be acquired and released
228 * with the correct granularity, it is the caller's responsibility to ensure
229 * this lock is held for write while calling any QUIC_CHANNEL method, except for
230 * methods explicitly designed otherwise.
231 *
232 * This method is thread safe and does not require prior locking. It can also be
233 * called while the lock is already held. Note that this is simply a convenience
234 * function to access the mutex which was passed to the channel at instantiation
235 * time; it does not belong to the channel but rather is presumed to belong to
236 * the owner of the channel.
237 */
238 CRYPTO_RWLOCK *ossl_quic_channel_get_mutex(QUIC_CHANNEL *ch);
239
240 # endif
241
242 #endif