quic_channel_local.h 18 KB

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