quic_channel.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright 2022-2023 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. #ifndef OSSL_QUIC_CHANNEL_H
  10. # define OSSL_QUIC_CHANNEL_H
  11. # include <openssl/ssl.h>
  12. # include "internal/quic_types.h"
  13. # include "internal/quic_stream_map.h"
  14. # include "internal/quic_reactor.h"
  15. # include "internal/quic_statm.h"
  16. # include "internal/time.h"
  17. # include "internal/thread.h"
  18. # ifndef OPENSSL_NO_QUIC
  19. /*
  20. * QUIC Channel
  21. * ============
  22. *
  23. * A QUIC channel (QUIC_CHANNEL) is an object which binds together all of the
  24. * various pieces of QUIC into a single top-level object, and handles connection
  25. * state which is not specific to the client or server roles. In particular, it
  26. * is strictly separated from the libssl front end I/O API personality layer,
  27. * and is not an SSL object.
  28. *
  29. * The name QUIC_CHANNEL is chosen because QUIC_CONNECTION is already in use,
  30. * but functionally these relate to the same thing (a QUIC connection). The use
  31. * of two separate objects ensures clean separation between the API personality
  32. * layer and common code for handling connections, and between the functionality
  33. * which is specific to clients and which is specific to servers, and the
  34. * functionality which is common to both.
  35. *
  36. * The API personality layer provides SSL objects (e.g. a QUIC_CONNECTION) which
  37. * consume a QUIC channel and implement a specific public API. Things which are
  38. * handled by the API personality layer include emulation of blocking semantics,
  39. * handling of SSL object mode flags like non-partial write mode, etc.
  40. *
  41. * Where the QUIC_CHANNEL is used in a server role, there is one QUIC_CHANNEL
  42. * per connection. In the future a QUIC Channel Manager will probably be defined
  43. * to handle ownership of resources which are shared between connections (e.g.
  44. * demuxers). Since we only use server-side functionality for dummy test servers
  45. * for now, which only need to handle one connection at a time, this is not
  46. * currently modelled.
  47. *
  48. * Synchronisation
  49. * ---------------
  50. *
  51. * To support thread assisted mode, QUIC_CHANNEL can be used by multiple
  52. * threads. **It is the caller's responsibility to ensure that the QUIC_CHANNEL
  53. * is only accessed (whether via its methods or via direct access to its state)
  54. * while the channel mutex is held**, except for methods explicitly marked as
  55. * not requiring prior locking. This is an unchecked precondition.
  56. *
  57. * The instantiator of the channel is responsible for providing a suitable
  58. * mutex which then serves as the channel mutex; see QUIC_CHANNEL_ARGS.
  59. */
  60. /*
  61. * The function does not acquire the channel mutex and assumes it is already
  62. * held by the calling thread.
  63. *
  64. * Any function tagged with this has the following precondition:
  65. *
  66. * Precondition: must hold channel mutex (unchecked)
  67. */
  68. # define QUIC_NEEDS_LOCK
  69. /*
  70. * The function acquires the channel mutex and releases it before returning in
  71. * all circumstances.
  72. *
  73. * Any function tagged with this has the following precondition and
  74. * postcondition:
  75. *
  76. * Precondition: must not hold channel mutex (unchecked)
  77. * Postcondition: channel mutex is not held (by calling thread)
  78. */
  79. # define QUIC_TAKES_LOCK
  80. /*
  81. * The function acquires the channel mutex and leaves it acquired
  82. * when returning success.
  83. *
  84. * Any function tagged with this has the following precondition and
  85. * postcondition:
  86. *
  87. * Precondition: must not hold channel mutex (unchecked)
  88. * Postcondition: channel mutex is held by calling thread
  89. * or function returned failure
  90. */
  91. # define QUIC_ACQUIRES_LOCK
  92. # define QUIC_TODO_LOCK
  93. # define QUIC_CHANNEL_STATE_IDLE 0
  94. # define QUIC_CHANNEL_STATE_ACTIVE 1
  95. # define QUIC_CHANNEL_STATE_TERMINATING_CLOSING 2
  96. # define QUIC_CHANNEL_STATE_TERMINATING_DRAINING 3
  97. # define QUIC_CHANNEL_STATE_TERMINATED 4
  98. typedef struct quic_channel_args_st {
  99. OSSL_LIB_CTX *libctx;
  100. const char *propq;
  101. int is_server;
  102. SSL *tls;
  103. /*
  104. * This must be a mutex the lifetime of which will exceed that of the
  105. * channel. The instantiator of the channel is responsible for providing a
  106. * mutex as this makes it easier to handle instantiation and teardown of
  107. * channels in situations potentially requiring locking.
  108. *
  109. * Note that this is a MUTEX not a RWLOCK as it needs to be an OS mutex for
  110. * compatibility with an OS's condition variable wait API, whereas RWLOCK
  111. * may, depending on the build configuration, be implemented using an OS's
  112. * mutex primitive or using its RW mutex primitive.
  113. */
  114. CRYPTO_MUTEX *mutex;
  115. /*
  116. * Optional function pointer to use to retrieve the current time. If NULL,
  117. * ossl_time_now() is used.
  118. */
  119. OSSL_TIME (*now_cb)(void *arg);
  120. void *now_cb_arg;
  121. } QUIC_CHANNEL_ARGS;
  122. typedef struct quic_channel_st QUIC_CHANNEL;
  123. /* Represents the cause for a connection's termination. */
  124. typedef struct quic_terminate_cause_st {
  125. /*
  126. * If we are in a TERMINATING or TERMINATED state, this is the error code
  127. * associated with the error. This field is valid iff we are in the
  128. * TERMINATING or TERMINATED states.
  129. */
  130. uint64_t error_code;
  131. /*
  132. * If terminate_app is set and this is nonzero, this is the frame type which
  133. * caused the connection to be terminated.
  134. */
  135. uint64_t frame_type;
  136. /*
  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.
  142. */
  143. const char *reason;
  144. /*
  145. * Length of reason in bytes. The reason is supposed to contain a UTF-8
  146. * string but may be arbitrary data if the reason came from the network.
  147. */
  148. size_t reason_len;
  149. /* Is this error code in the transport (0) or application (1) space? */
  150. unsigned int app : 1;
  151. /*
  152. * If set, the cause of the termination is a received CONNECTION_CLOSE
  153. * frame. Otherwise, we decided to terminate ourselves and sent a
  154. * CONNECTION_CLOSE frame (regardless of whether the peer later also sends
  155. * one).
  156. */
  157. unsigned int remote : 1;
  158. } QUIC_TERMINATE_CAUSE;
  159. /*
  160. * Create a new QUIC channel using the given arguments. The argument structure
  161. * does not need to remain allocated. Returns NULL on failure.
  162. */
  163. QUIC_CHANNEL *ossl_quic_channel_new(const QUIC_CHANNEL_ARGS *args);
  164. /* No-op if ch is NULL. */
  165. void ossl_quic_channel_free(QUIC_CHANNEL *ch);
  166. /* Set mutator callbacks for test framework support */
  167. int ossl_quic_channel_set_mutator(QUIC_CHANNEL *ch,
  168. ossl_mutate_packet_cb mutatecb,
  169. ossl_finish_mutate_cb finishmutatecb,
  170. void *mutatearg);
  171. /*
  172. * Connection Lifecycle Events
  173. * ===========================
  174. *
  175. * Various events that can be raised on the channel by other parts of the QUIC
  176. * implementation. Some of these are suitable for general use by any part of the
  177. * code (e.g. ossl_quic_channel_raise_protocol_error), others are for very
  178. * specific use by particular components only (e.g.
  179. * ossl_quic_channel_on_handshake_confirmed).
  180. */
  181. /*
  182. * To be used by a QUIC connection. Starts the channel. For a client-mode
  183. * channel, this starts sending the first handshake layer message, etc. Can only
  184. * be called in the idle state; successive calls are ignored.
  185. */
  186. int ossl_quic_channel_start(QUIC_CHANNEL *ch);
  187. /* Start a locally initiated connection shutdown. */
  188. void ossl_quic_channel_local_close(QUIC_CHANNEL *ch, uint64_t app_error_code,
  189. const char *app_reason);
  190. /*
  191. * Called when the handshake is confirmed.
  192. */
  193. int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch);
  194. /*
  195. * Raises a protocol error. This is intended to be the universal call suitable
  196. * for handling of all peer-triggered protocol violations or errors detected by
  197. * us. We specify a QUIC transport-scope error code and optional frame type
  198. * which was responsible. If a frame type is not applicable, specify zero. The
  199. * reason string is not currently handled, but should be a string of static
  200. * storage duration. If the connection has already terminated due to a previous
  201. * protocol error, this is a no-op; first error wins.
  202. *
  203. * Usually the ossl_quic_channel_raise_protocol_error() function should be used.
  204. * The ossl_quic_channel_raise_protocol_error_loc() function can be used
  205. * directly for passing through existing call site information from an existing
  206. * error.
  207. */
  208. void ossl_quic_channel_raise_protocol_error_loc(QUIC_CHANNEL *ch,
  209. uint64_t error_code,
  210. uint64_t frame_type,
  211. const char *reason,
  212. ERR_STATE *err_state,
  213. const char *src_file,
  214. int src_line,
  215. const char *src_func);
  216. #define ossl_quic_channel_raise_protocol_error(ch, error_code, frame_type, reason) \
  217. ossl_quic_channel_raise_protocol_error_loc((ch), (error_code), \
  218. (frame_type), \
  219. (reason), \
  220. NULL, \
  221. OPENSSL_FILE, \
  222. OPENSSL_LINE, \
  223. OPENSSL_FUNC)
  224. #define ossl_quic_channel_raise_protocol_error_state(ch, error_code, frame_type, reason, state) \
  225. ossl_quic_channel_raise_protocol_error_loc((ch), (error_code), \
  226. (frame_type), \
  227. (reason), \
  228. (state), \
  229. OPENSSL_FILE, \
  230. OPENSSL_LINE, \
  231. OPENSSL_FUNC)
  232. /*
  233. * Returns 1 if permanent net error was detected on the QUIC_CHANNEL,
  234. * 0 otherwise.
  235. */
  236. int ossl_quic_channel_net_error(QUIC_CHANNEL *ch);
  237. /* Restore saved error state (best effort) */
  238. void ossl_quic_channel_restore_err_state(QUIC_CHANNEL *ch);
  239. /* For RXDP use. */
  240. void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL *ch,
  241. OSSL_QUIC_FRAME_CONN_CLOSE *f);
  242. void ossl_quic_channel_on_new_conn_id(QUIC_CHANNEL *ch,
  243. OSSL_QUIC_FRAME_NEW_CONN_ID *f);
  244. /*
  245. * Queries and Accessors
  246. * =====================
  247. */
  248. /* Gets the reactor which can be used to tick/poll on the channel. */
  249. QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch);
  250. /* Gets the QSM used with the channel. */
  251. QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch);
  252. /* Gets the statistics manager used with the channel. */
  253. OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch);
  254. /*
  255. * Gets/sets the current peer address. Generally this should be used before
  256. * starting a channel in client mode.
  257. */
  258. int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL *ch, BIO_ADDR *peer_addr);
  259. int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL *ch, const BIO_ADDR *peer_addr);
  260. /* Gets/sets the underlying network read and write BIOs. */
  261. BIO *ossl_quic_channel_get_net_rbio(QUIC_CHANNEL *ch);
  262. BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch);
  263. int ossl_quic_channel_set_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio);
  264. int ossl_quic_channel_set_net_wbio(QUIC_CHANNEL *ch, BIO *net_wbio);
  265. /*
  266. * Re-poll the network BIOs already set to determine if their support
  267. * for polling has changed.
  268. */
  269. int ossl_quic_channel_update_poll_descriptors(QUIC_CHANNEL *ch);
  270. /*
  271. * Returns an existing stream by stream ID. Returns NULL if the stream does not
  272. * exist.
  273. */
  274. QUIC_STREAM *ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL *ch,
  275. uint64_t stream_id);
  276. /* Returns 1 if channel is terminating or terminated. */
  277. int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch);
  278. const QUIC_TERMINATE_CAUSE *
  279. ossl_quic_channel_get_terminate_cause(const QUIC_CHANNEL *ch);
  280. int ossl_quic_channel_is_closing(const QUIC_CHANNEL *ch);
  281. int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch);
  282. int ossl_quic_channel_is_active(const QUIC_CHANNEL *ch);
  283. int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL *ch);
  284. int ossl_quic_channel_is_handshake_confirmed(const QUIC_CHANNEL *ch);
  285. QUIC_DEMUX *ossl_quic_channel_get0_demux(QUIC_CHANNEL *ch);
  286. SSL *ossl_quic_channel_get0_ssl(QUIC_CHANNEL *ch);
  287. /*
  288. * Retrieves a pointer to the channel mutex which was provided at the time the
  289. * channel was instantiated. In order to allow locks to be acquired and released
  290. * with the correct granularity, it is the caller's responsibility to ensure
  291. * this lock is held for write while calling any QUIC_CHANNEL method, except for
  292. * methods explicitly designed otherwise.
  293. *
  294. * This method is thread safe and does not require prior locking. It can also be
  295. * called while the lock is already held. Note that this is simply a convenience
  296. * function to access the mutex which was passed to the channel at instantiation
  297. * time; it does not belong to the channel but rather is presumed to belong to
  298. * the owner of the channel.
  299. */
  300. CRYPTO_MUTEX *ossl_quic_channel_get_mutex(QUIC_CHANNEL *ch);
  301. /*
  302. * Creates a new locally-initiated stream in the stream mapper, choosing an
  303. * appropriate stream ID. If is_uni is 1, creates a unidirectional stream, else
  304. * creates a bidirectional stream. Returns NULL on failure.
  305. */
  306. QUIC_STREAM *ossl_quic_channel_new_stream_local(QUIC_CHANNEL *ch, int is_uni);
  307. /*
  308. * Creates a new remotely-initiated stream in the stream mapper. The stream ID
  309. * is used to confirm the initiator and determine the stream type. The stream is
  310. * automatically added to the QSM's accept queue. A pointer to the stream is
  311. * also returned. Returns NULL on failure.
  312. */
  313. QUIC_STREAM *ossl_quic_channel_new_stream_remote(QUIC_CHANNEL *ch,
  314. uint64_t stream_id);
  315. /*
  316. * Configures incoming stream auto-reject. If enabled, incoming streams have
  317. * both their sending and receiving parts automatically rejected using
  318. * STOP_SENDING and STREAM_RESET frames. aec is the application error
  319. * code to be used for those frames.
  320. */
  321. void ossl_quic_channel_set_incoming_stream_auto_reject(QUIC_CHANNEL *ch,
  322. int enable,
  323. uint64_t aec);
  324. /*
  325. * Causes the channel to reject the sending and receiving parts of a stream,
  326. * as though autorejected. Can be used if a stream has already been
  327. * accepted.
  328. */
  329. void ossl_quic_channel_reject_stream(QUIC_CHANNEL *ch, QUIC_STREAM *qs);
  330. /* Replace local connection ID in TXP and DEMUX for testing purposes. */
  331. int ossl_quic_channel_replace_local_cid(QUIC_CHANNEL *ch,
  332. const QUIC_CONN_ID *conn_id);
  333. /* Setters for the msg_callback and msg_callback_arg */
  334. void ossl_quic_channel_set_msg_callback(QUIC_CHANNEL *ch,
  335. ossl_msg_cb msg_callback,
  336. SSL *msg_callback_ssl);
  337. void ossl_quic_channel_set_msg_callback_arg(QUIC_CHANNEL *ch,
  338. void *msg_callback_arg);
  339. /* Testing use only - sets a TXKU threshold packet count override value. */
  340. void ossl_quic_channel_set_txku_threshold_override(QUIC_CHANNEL *ch,
  341. uint64_t tx_pkt_threshold);
  342. /* Testing use only - gets current 1-RTT key epochs for QTX and QRX. */
  343. uint64_t ossl_quic_channel_get_tx_key_epoch(QUIC_CHANNEL *ch);
  344. uint64_t ossl_quic_channel_get_rx_key_epoch(QUIC_CHANNEL *ch);
  345. /* Artificially trigger a spontaneous TXKU if possible. */
  346. int ossl_quic_channel_trigger_txku(QUIC_CHANNEL *ch);
  347. int ossl_quic_channel_has_pending(const QUIC_CHANNEL *ch);
  348. /* Force transmission of an ACK-eliciting packet. */
  349. int ossl_quic_channel_ping(QUIC_CHANNEL *ch);
  350. /* For testing use. While enabled, ticking is not performed. */
  351. void ossl_quic_channel_set_inhibit_tick(QUIC_CHANNEL *ch, int inhibit);
  352. /*
  353. * These queries exist for diagnostic purposes only. They may roll over.
  354. * Do not rely on them for non-testing purposes.
  355. */
  356. uint16_t ossl_quic_channel_get_diag_num_rx_ack(QUIC_CHANNEL *ch);
  357. /*
  358. * Diagnostic use only. Gets the current local CID.
  359. */
  360. void ossl_quic_channel_get_diag_local_cid(QUIC_CHANNEL *ch, QUIC_CONN_ID *cid);
  361. /*
  362. * Returns 1 if stream count flow control allows us to create a new
  363. * locally-initiated stream.
  364. */
  365. int ossl_quic_channel_is_new_local_stream_admissible(QUIC_CHANNEL *ch, int is_uni);
  366. # endif
  367. #endif