quic_channel_local.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #ifndef OSSL_QUIC_CHANNEL_LOCAL_H
  2. # define OSSL_QUIC_CHANNEL_LOCAL_H
  3. # include "internal/quic_channel.h"
  4. # ifndef OPENSSL_NO_QUIC
  5. # include <openssl/lhash.h>
  6. # include "internal/list.h"
  7. typedef struct quic_srt_elem_st QUIC_SRT_ELEM;
  8. struct quic_srt_elem_st {
  9. OSSL_LIST_MEMBER(stateless_reset_tokens, QUIC_SRT_ELEM);
  10. QUIC_STATELESS_RESET_TOKEN token;
  11. uint64_t seq_num;
  12. };
  13. DEFINE_LIST_OF(stateless_reset_tokens, QUIC_SRT_ELEM);
  14. /*
  15. * QUIC Channel Structure
  16. * ======================
  17. *
  18. * QUIC channel internals. It is intended that only the QUIC_CHANNEL
  19. * implementation and the RX depacketiser be allowed to access this structure
  20. * directly. As the RX depacketiser has no state of its own and computes over a
  21. * QUIC_CHANNEL structure, it can be viewed as an extension of the QUIC_CHANNEL
  22. * implementation. While the RX depacketiser could be provided with adequate
  23. * accessors to do what it needs, this would weaken the abstraction provided by
  24. * the QUIC_CHANNEL to other components; moreover the coupling of the RX
  25. * depacketiser to QUIC_CHANNEL internals is too deep and bespoke to make this
  26. * desirable.
  27. *
  28. * Other components should not include this header.
  29. */
  30. struct quic_channel_st {
  31. OSSL_LIB_CTX *libctx;
  32. const char *propq;
  33. /*
  34. * Master synchronisation mutex used for thread assisted mode
  35. * synchronisation. We don't own this; the instantiator of the channel
  36. * passes it to us and is responsible for freeing it after channel
  37. * destruction.
  38. */
  39. CRYPTO_MUTEX *mutex;
  40. /*
  41. * Callback used to get the current time.
  42. */
  43. OSSL_TIME (*now_cb)(void *arg);
  44. void *now_cb_arg;
  45. /*
  46. * The associated TLS 1.3 connection data. Used to provide the handshake
  47. * layer; its 'network' side is plugged into the crypto stream for each EL
  48. * (other than the 0-RTT EL).
  49. */
  50. QUIC_TLS *qtls;
  51. SSL *tls;
  52. /*
  53. * The transport parameter block we will send or have sent.
  54. * Freed after sending or when connection is freed.
  55. */
  56. unsigned char *local_transport_params;
  57. /* Asynchronous I/O reactor. */
  58. QUIC_REACTOR rtor;
  59. /* Our current L4 peer address, if any. */
  60. BIO_ADDR cur_peer_addr;
  61. /* Network-side read and write BIOs. */
  62. BIO *net_rbio, *net_wbio;
  63. /*
  64. * Subcomponents of the connection. All of these components are instantiated
  65. * and owned by us.
  66. */
  67. OSSL_QUIC_TX_PACKETISER *txp;
  68. QUIC_TXPIM *txpim;
  69. QUIC_CFQ *cfq;
  70. /*
  71. * Connection level FC. The stream_count RXFCs is used to manage
  72. * MAX_STREAMS signalling.
  73. */
  74. QUIC_TXFC conn_txfc;
  75. QUIC_RXFC conn_rxfc, crypto_rxfc[QUIC_PN_SPACE_NUM];
  76. QUIC_RXFC max_streams_bidi_rxfc, max_streams_uni_rxfc;
  77. QUIC_STREAM_MAP qsm;
  78. OSSL_STATM statm;
  79. OSSL_CC_DATA *cc_data;
  80. const OSSL_CC_METHOD *cc_method;
  81. OSSL_ACKM *ackm;
  82. /*
  83. * RX demuxer. We register incoming DCIDs with this. Since we currently only
  84. * support client operation and use one L4 port per connection, we own the
  85. * demuxer and register a single zero-length DCID with it.
  86. */
  87. QUIC_DEMUX *demux;
  88. /* Record layers in the TX and RX directions, plus the RX demuxer. */
  89. OSSL_QTX *qtx;
  90. OSSL_QRX *qrx;
  91. /* Message callback related arguments */
  92. ossl_msg_cb msg_callback;
  93. void *msg_callback_arg;
  94. SSL *msg_callback_ssl;
  95. /*
  96. * Send and receive parts of the crypto streams.
  97. * crypto_send[QUIC_PN_SPACE_APP] is the 1-RTT crypto stream. There is no
  98. * 0-RTT crypto stream.
  99. */
  100. QUIC_SSTREAM *crypto_send[QUIC_PN_SPACE_NUM];
  101. QUIC_RSTREAM *crypto_recv[QUIC_PN_SPACE_NUM];
  102. /* Internal state. */
  103. /*
  104. * Client: The DCID used in the first Initial packet we transmit as a client.
  105. * Server: The DCID used in the first Initial packet the client transmitted.
  106. * Randomly generated and required by RFC to be at least 8 bytes.
  107. */
  108. QUIC_CONN_ID init_dcid;
  109. /*
  110. * Client: The SCID found in the first Initial packet from the server.
  111. * Not valid for servers.
  112. * Valid if have_received_enc_pkt is set.
  113. */
  114. QUIC_CONN_ID init_scid;
  115. /*
  116. * Client only: The SCID found in an incoming Retry packet we handled.
  117. * Not valid for servers.
  118. */
  119. QUIC_CONN_ID retry_scid;
  120. /*
  121. * The DCID we currently use to talk to the peer and its sequence num.
  122. *
  123. * TODO(QUIC FUTURE) consider removing the second two, both are contained in
  124. * srt_list_seq (defined below).
  125. *
  126. * cur_remote_seq_num is same as the sequence number in the last element.
  127. * cur_retire_prior_to corresponds to the sequence number in first element.
  128. *
  129. * Leaving them here avoids null checking etc
  130. */
  131. QUIC_CONN_ID cur_remote_dcid;
  132. uint64_t cur_remote_seq_num;
  133. uint64_t cur_retire_prior_to;
  134. /* Server only: The DCID we currently expect the peer to use to talk to us. */
  135. QUIC_CONN_ID cur_local_cid;
  136. /* Hash of stateless reset tokens keyed on the token */
  137. LHASH_OF(QUIC_SRT_ELEM) *srt_hash_tok;
  138. /* List of the stateless reset tokens ordered by sequence number */
  139. OSSL_LIST(stateless_reset_tokens) srt_list_seq;
  140. /* Transport parameter values we send to our peer. */
  141. uint64_t tx_init_max_stream_data_bidi_local;
  142. uint64_t tx_init_max_stream_data_bidi_remote;
  143. uint64_t tx_init_max_stream_data_uni;
  144. uint64_t tx_max_ack_delay; /* ms */
  145. /* Transport parameter values received from server. */
  146. uint64_t rx_init_max_stream_data_bidi_local;
  147. uint64_t rx_init_max_stream_data_bidi_remote;
  148. uint64_t rx_init_max_stream_data_uni;
  149. uint64_t rx_max_ack_delay; /* ms */
  150. unsigned char rx_ack_delay_exp;
  151. /*
  152. * Temporary staging area to store information about the incoming packet we
  153. * are currently processing.
  154. */
  155. OSSL_QRX_PKT *qrx_pkt;
  156. /*
  157. * Current limit on number of streams we may create. Set by transport
  158. * parameters initially and then by MAX_STREAMS frames.
  159. */
  160. uint64_t max_local_streams_bidi;
  161. uint64_t max_local_streams_uni;
  162. /* The negotiated maximum idle timeout in milliseconds. */
  163. uint64_t max_idle_timeout;
  164. /*
  165. * Maximum payload size in bytes for datagrams sent to our peer, as
  166. * negotiated by transport parameters.
  167. */
  168. uint64_t rx_max_udp_payload_size;
  169. /* Maximum active CID limit, as negotiated by transport parameters. */
  170. uint64_t rx_active_conn_id_limit;
  171. /*
  172. * Used to allocate stream IDs. This is a stream ordinal, i.e., a stream ID
  173. * without the low two bits designating type and initiator. Shift and or in
  174. * the type bits to convert to a stream ID.
  175. */
  176. uint64_t next_local_stream_ordinal_bidi;
  177. uint64_t next_local_stream_ordinal_uni;
  178. /*
  179. * Used to track which stream ordinals within a given stream type have been
  180. * used by the remote peer. This is an optimisation used to determine
  181. * which streams should be implicitly created due to usage of a higher
  182. * stream ordinal.
  183. */
  184. uint64_t next_remote_stream_ordinal_bidi;
  185. uint64_t next_remote_stream_ordinal_uni;
  186. /*
  187. * Application error code to be used for STOP_SENDING/RESET_STREAM frames
  188. * used to autoreject incoming streams.
  189. */
  190. uint64_t incoming_stream_auto_reject_aec;
  191. /*
  192. * Override packet count threshold at which we do a spontaneous TXKU.
  193. * Usually UINT64_MAX in which case a suitable value is chosen based on AEAD
  194. * limit advice from the QRL utility functions. This is intended for testing
  195. * use only. Usually set to UINT64_MAX.
  196. */
  197. uint64_t txku_threshold_override;
  198. /* Diagnostic counters for testing purposes only. May roll over. */
  199. uint16_t diag_num_rx_ack; /* Number of ACK frames received */
  200. /* Valid if we are in the TERMINATING or TERMINATED states. */
  201. QUIC_TERMINATE_CAUSE terminate_cause;
  202. /*
  203. * Deadline at which we move to TERMINATING state. Valid if in the
  204. * TERMINATING state.
  205. */
  206. OSSL_TIME terminate_deadline;
  207. /*
  208. * Deadline at which connection dies due to idle timeout if no further
  209. * events occur.
  210. */
  211. OSSL_TIME idle_deadline;
  212. /*
  213. * Deadline at which we should send an ACK-eliciting packet to ensure
  214. * idle timeout does not occur.
  215. */
  216. OSSL_TIME ping_deadline;
  217. /*
  218. * The deadline at which the period in which it is RECOMMENDED that we not
  219. * initiate any spontaneous TXKU ends. This is zero if no such deadline
  220. * applies.
  221. */
  222. OSSL_TIME txku_cooldown_deadline;
  223. /*
  224. * The deadline at which we take the QRX out of UPDATING and back to NORMAL.
  225. * Valid if rxku_in_progress in 1.
  226. */
  227. OSSL_TIME rxku_update_end_deadline;
  228. /*
  229. * The first (application space) PN sent with a new key phase. Valid if the
  230. * QTX key epoch is greater than 0. Once a packet we sent with a PN p (p >=
  231. * txku_pn) is ACKed, the TXKU is considered completed and txku_in_progress
  232. * becomes 0. For sanity's sake, such a PN p should also be <= the highest
  233. * PN we have ever sent, of course.
  234. */
  235. QUIC_PN txku_pn;
  236. /*
  237. * The (application space) PN which triggered RXKU detection. Valid if
  238. * rxku_pending_confirm.
  239. */
  240. QUIC_PN rxku_trigger_pn;
  241. /*
  242. * State tracking. QUIC connection-level state is best represented based on
  243. * whether various things have happened yet or not, rather than as an
  244. * explicit FSM. We do have a coarse state variable which tracks the basic
  245. * state of the connection's lifecycle, but more fine-grained conditions of
  246. * the Active state are tracked via flags below. For more details, see
  247. * doc/designs/quic-design/connection-state-machine.md. We are in the Open
  248. * state if the state is QUIC_CHANNEL_STATE_ACTIVE and handshake_confirmed is
  249. * set.
  250. */
  251. unsigned int state : 3;
  252. /*
  253. * Have we received at least one encrypted packet from the peer?
  254. * (If so, Retry and Version Negotiation messages should no longer
  255. * be received and should be ignored if they do occur.)
  256. */
  257. unsigned int have_received_enc_pkt : 1;
  258. /*
  259. * Have we successfully processed any packet, including a Version
  260. * Negotiation packet? If so, further Version Negotiation packets should be
  261. * ignored.
  262. */
  263. unsigned int have_processed_any_pkt : 1;
  264. /*
  265. * Have we sent literally any packet yet? If not, there is no point polling
  266. * RX.
  267. */
  268. unsigned int have_sent_any_pkt : 1;
  269. /*
  270. * Are we currently doing proactive version negotiation?
  271. */
  272. unsigned int doing_proactive_ver_neg : 1;
  273. /* We have received transport parameters from the peer. */
  274. unsigned int got_remote_transport_params : 1;
  275. /*
  276. * This monotonically transitions to 1 once the TLS state machine is
  277. * 'complete', meaning that it has both sent a Finished and successfully
  278. * verified the peer's Finished (see RFC 9001 s. 4.1.1). Note that it
  279. * does not transition to 1 at both peers simultaneously.
  280. *
  281. * Handshake completion is not the same as handshake confirmation (see
  282. * below).
  283. */
  284. unsigned int handshake_complete : 1;
  285. /*
  286. * This monotonically transitions to 1 once the handshake is confirmed.
  287. * This happens on the client when we receive a HANDSHAKE_DONE frame.
  288. * At our option, we may also take acknowledgement of any 1-RTT packet
  289. * we sent as a handshake confirmation.
  290. */
  291. unsigned int handshake_confirmed : 1;
  292. /*
  293. * We are sending Initial packets based on a Retry. This means we definitely
  294. * should not receive another Retry, and if we do it is an error.
  295. */
  296. unsigned int doing_retry : 1;
  297. /*
  298. * We don't store the current EL here; the TXP asks the QTX which ELs
  299. * are provisioned to determine which ELs to use.
  300. */
  301. /* Have statm, qsm been initialised? Used to track cleanup. */
  302. unsigned int have_statm : 1;
  303. unsigned int have_qsm : 1;
  304. /*
  305. * Preferred ELs for transmission and reception. This is not strictly needed
  306. * as it can be inferred from what keys we have provisioned, but makes
  307. * determining the current EL simpler and faster. A separate EL for
  308. * transmission and reception is not strictly necessary but makes things
  309. * easier for interoperation with the handshake layer, which likes to invoke
  310. * the yield secret callback at different times for TX and RX.
  311. */
  312. unsigned int tx_enc_level : 3;
  313. unsigned int rx_enc_level : 3;
  314. /* If bit n is set, EL n has been discarded. */
  315. unsigned int el_discarded : 4;
  316. /*
  317. * While in TERMINATING - CLOSING, set when we should generate a connection
  318. * close frame.
  319. */
  320. unsigned int conn_close_queued : 1;
  321. /* Are we in server mode? Never changes after instantiation. */
  322. unsigned int is_server : 1;
  323. /*
  324. * Set temporarily when the handshake layer has given us a new RX secret.
  325. * Used to determine if we need to check our RX queues again.
  326. */
  327. unsigned int have_new_rx_secret : 1;
  328. /* Have we ever called QUIC_TLS yet during RX processing? */
  329. unsigned int did_tls_tick : 1;
  330. /* Has any CRYPTO frame been processed during this tick? */
  331. unsigned int did_crypto_frame : 1;
  332. /*
  333. * Have we sent an ack-eliciting packet since the last successful packet
  334. * reception? Used to determine when to bump idle timer (see RFC 9000 s.
  335. * 10.1).
  336. */
  337. unsigned int have_sent_ack_eliciting_since_rx : 1;
  338. /* Should incoming streams automatically be rejected? */
  339. unsigned int incoming_stream_auto_reject : 1;
  340. /*
  341. * 1 if a key update sequence was locally initiated, meaning we sent the
  342. * TXKU first and the resultant RXKU shouldn't result in our triggering
  343. * another TXKU. 0 if a key update sequence was initiated by the peer,
  344. * meaning we detect a RXKU first and have to generate a TXKU in response.
  345. */
  346. unsigned int ku_locally_initiated : 1;
  347. /*
  348. * 1 if we have triggered TXKU (whether spontaneous or solicited) but are
  349. * waiting for any PN using that new KP to be ACKed. While this is set, we
  350. * are not allowed to trigger spontaneous TXKU (but solicited TXKU is
  351. * potentially still possible).
  352. */
  353. unsigned int txku_in_progress : 1;
  354. /*
  355. * We have received an RXKU event and currently are going through
  356. * UPDATING/COOLDOWN on the QRX. COOLDOWN is currently not used. Since RXKU
  357. * cannot be detected in this state, this doesn't cause a protocol error or
  358. * anything similar if a peer tries TXKU in this state. That traffic would
  359. * simply be dropped. It's only used to track that our UPDATING timer is
  360. * active so we know when to take the QRX out of UPDATING and back to
  361. * NORMAL.
  362. */
  363. unsigned int rxku_in_progress : 1;
  364. /*
  365. * We have received an RXKU but have yet to send an ACK for it, which means
  366. * no further RXKUs are allowed yet. Note that we cannot detect further
  367. * RXKUs anyway while the QRX remains in the UPDATING/COOLDOWN states, so
  368. * this restriction comes into play if we take more than PTO time to send
  369. * an ACK for it (not likely).
  370. */
  371. unsigned int rxku_pending_confirm : 1;
  372. /* Temporary variable indicating rxku_pending_confirm is to become 0. */
  373. unsigned int rxku_pending_confirm_done : 1;
  374. /*
  375. * If set, RXKU is expected (because we initiated a spontaneous TXKU).
  376. */
  377. unsigned int rxku_expected : 1;
  378. /* Permanent net error encountered */
  379. unsigned int net_error : 1;
  380. /*
  381. * Protocol error encountered. Note that you should refer to the state field
  382. * rather than this. This is only used so we can ignore protocol errors
  383. * after the first protocol error, but still record the first protocol error
  384. * if it happens during the TERMINATING state.
  385. */
  386. unsigned int protocol_error : 1;
  387. /* Inhibit tick for testing purposes? */
  388. unsigned int inhibit_tick : 1;
  389. /* Are we using addressed mode? */
  390. unsigned int addressed_mode : 1;
  391. /* Saved error stack in case permanent error was encountered */
  392. ERR_STATE *err_state;
  393. /* Scratch area for use by RXDP to store decoded ACK ranges. */
  394. OSSL_QUIC_ACK_RANGE *ack_range_scratch;
  395. size_t num_ack_range_scratch;
  396. };
  397. # endif
  398. #endif