quic_txp.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_TXP_H
  10. # define OSSL_QUIC_TXP_H
  11. # include <openssl/ssl.h>
  12. # include "internal/quic_types.h"
  13. # include "internal/quic_record_tx.h"
  14. # include "internal/quic_cfq.h"
  15. # include "internal/quic_txpim.h"
  16. # include "internal/quic_stream.h"
  17. # include "internal/quic_stream_map.h"
  18. # include "internal/quic_fc.h"
  19. # include "internal/bio_addr.h"
  20. # include "internal/time.h"
  21. # ifndef OPENSSL_NO_QUIC
  22. /*
  23. * QUIC TX Packetiser
  24. * ==================
  25. */
  26. typedef struct ossl_quic_tx_packetiser_args_st {
  27. /* Configuration Settings */
  28. QUIC_CONN_ID cur_scid; /* Current Source Connection ID we use. */
  29. QUIC_CONN_ID cur_dcid; /* Current Destination Connection ID we use. */
  30. BIO_ADDR peer; /* Current destination L4 address we use. */
  31. uint32_t ack_delay_exponent; /* ACK delay exponent used when encoding. */
  32. /* Injected Dependencies */
  33. OSSL_QTX *qtx; /* QUIC Record Layer TX we are using */
  34. QUIC_TXPIM *txpim; /* QUIC TX'd Packet Information Manager */
  35. QUIC_CFQ *cfq; /* QUIC Control Frame Queue */
  36. OSSL_ACKM *ackm; /* QUIC Acknowledgement Manager */
  37. QUIC_STREAM_MAP *qsm; /* QUIC Streams Map */
  38. QUIC_TXFC *conn_txfc; /* QUIC Connection-Level TX Flow Controller */
  39. QUIC_RXFC *conn_rxfc; /* QUIC Connection-Level RX Flow Controller */
  40. QUIC_RXFC *max_streams_bidi_rxfc; /* QUIC RXFC for MAX_STREAMS generation */
  41. QUIC_RXFC *max_streams_uni_rxfc;
  42. const OSSL_CC_METHOD *cc_method; /* QUIC Congestion Controller */
  43. OSSL_CC_DATA *cc_data; /* QUIC Congestion Controller Instance */
  44. OSSL_TIME (*now)(void *arg); /* Callback to get current time. */
  45. void *now_arg;
  46. /*
  47. * Injected dependencies - crypto streams.
  48. *
  49. * Note: There is no crypto stream for the 0-RTT EL.
  50. * crypto[QUIC_PN_SPACE_APP] is the 1-RTT crypto stream.
  51. */
  52. QUIC_SSTREAM *crypto[QUIC_PN_SPACE_NUM];
  53. } OSSL_QUIC_TX_PACKETISER_ARGS;
  54. typedef struct ossl_quic_tx_packetiser_st OSSL_QUIC_TX_PACKETISER;
  55. OSSL_QUIC_TX_PACKETISER *ossl_quic_tx_packetiser_new(const OSSL_QUIC_TX_PACKETISER_ARGS *args);
  56. typedef void (ossl_quic_initial_token_free_fn)(const unsigned char *buf,
  57. size_t buf_len, void *arg);
  58. void ossl_quic_tx_packetiser_free(OSSL_QUIC_TX_PACKETISER *txp);
  59. /*
  60. * When in the closing state we need to maintain a count of received bytes
  61. * so that we can limit the number of close connection frames we send.
  62. * Refer RFC 9000 s. 10.2.1 Closing Connection State.
  63. */
  64. void ossl_quic_tx_packetiser_record_received_closing_bytes(
  65. OSSL_QUIC_TX_PACKETISER *txp, size_t n);
  66. /*
  67. * Generates a datagram by polling the various ELs to determine if they want to
  68. * generate any frames, and generating a datagram which coalesces packets for
  69. * any ELs which do.
  70. *
  71. * Returns 0 on failure (e.g. allocation error or other errors), 1 otherwise.
  72. *
  73. * *status is filled with status information about the generated packet.
  74. * It is always filled even in case of failure. In particular, packets can be
  75. * sent even if failure is later returned.
  76. * See QUIC_TXP_STATUS for details.
  77. */
  78. typedef struct quic_txp_status_st {
  79. int sent_ack_eliciting; /* Was an ACK-eliciting packet sent? */
  80. int sent_handshake; /* Was a Handshake packet sent? */
  81. size_t sent_pkt; /* Number of packets sent (0 if nothing was sent) */
  82. } QUIC_TXP_STATUS;
  83. int ossl_quic_tx_packetiser_generate(OSSL_QUIC_TX_PACKETISER *txp,
  84. QUIC_TXP_STATUS *status);
  85. /*
  86. * Returns a deadline after which a call to ossl_quic_tx_packetiser_generate()
  87. * might succeed even if it did not previously. This may return
  88. * ossl_time_infinite() if there is no such deadline currently applicable. It
  89. * returns ossl_time_zero() if there is (potentially) more data to be generated
  90. * immediately. The value returned is liable to change after any call to
  91. * ossl_quic_tx_packetiser_generate() (or after ACKM or CC state changes). Note
  92. * that ossl_quic_tx_packetiser_generate() can also start to succeed for other
  93. * non-chronological reasons, such as changes to send stream buffers, etc.
  94. */
  95. OSSL_TIME ossl_quic_tx_packetiser_get_deadline(OSSL_QUIC_TX_PACKETISER *txp);
  96. /*
  97. * Set the token used in Initial packets. The callback is called when the buffer
  98. * is no longer needed; for example, when the TXP is freed or when this function
  99. * is called again with a new buffer. Fails returning 0 if the token is too big
  100. * to ever be reasonably encapsulated in an outgoing packet based on our current
  101. * understanding of our PMTU.
  102. */
  103. int ossl_quic_tx_packetiser_set_initial_token(OSSL_QUIC_TX_PACKETISER *txp,
  104. const unsigned char *token,
  105. size_t token_len,
  106. ossl_quic_initial_token_free_fn *free_cb,
  107. void *free_cb_arg);
  108. /* Change the DCID the TXP uses to send outgoing packets. */
  109. int ossl_quic_tx_packetiser_set_cur_dcid(OSSL_QUIC_TX_PACKETISER *txp,
  110. const QUIC_CONN_ID *dcid);
  111. /* Change the SCID the TXP uses to send outgoing (long) packets. */
  112. int ossl_quic_tx_packetiser_set_cur_scid(OSSL_QUIC_TX_PACKETISER *txp,
  113. const QUIC_CONN_ID *scid);
  114. /*
  115. * Change the destination L4 address the TXP uses to send datagrams. Specify
  116. * NULL (or AF_UNSPEC) to disable use of addressed mode.
  117. */
  118. int ossl_quic_tx_packetiser_set_peer(OSSL_QUIC_TX_PACKETISER *txp,
  119. const BIO_ADDR *peer);
  120. /*
  121. * Inform the TX packetiser that an EL has been discarded. Idempotent.
  122. *
  123. * This does not inform the QTX as well; the caller must also inform the QTX.
  124. *
  125. * The TXP will no longer reference the crypto[enc_level] QUIC_SSTREAM which was
  126. * provided in the TXP arguments. However, it is the callers responsibility to
  127. * free that QUIC_SSTREAM if desired.
  128. */
  129. int ossl_quic_tx_packetiser_discard_enc_level(OSSL_QUIC_TX_PACKETISER *txp,
  130. uint32_t enc_level);
  131. /*
  132. * Informs the TX packetiser that the handshake is complete. The TX packetiser
  133. * will not send 1-RTT application data until the handshake is complete,
  134. * as the authenticity of the peer is not confirmed until the handshake
  135. * complete event occurs.
  136. */
  137. void ossl_quic_tx_packetiser_notify_handshake_complete(OSSL_QUIC_TX_PACKETISER *txp);
  138. /* Asks the TXP to generate a HANDSHAKE_DONE frame in the next 1-RTT packet. */
  139. void ossl_quic_tx_packetiser_schedule_handshake_done(OSSL_QUIC_TX_PACKETISER *txp);
  140. /* Asks the TXP to ensure the next packet in the given PN space is ACK-eliciting. */
  141. void ossl_quic_tx_packetiser_schedule_ack_eliciting(OSSL_QUIC_TX_PACKETISER *txp,
  142. uint32_t pn_space);
  143. /*
  144. * Asks the TXP to ensure an ACK is put in the next packet in the given PN
  145. * space.
  146. */
  147. void ossl_quic_tx_packetiser_schedule_ack(OSSL_QUIC_TX_PACKETISER *txp,
  148. uint32_t pn_space);
  149. /*
  150. * Schedules a connection close. *f and f->reason are copied. This operation is
  151. * irreversible and causes all further packets generated by the TXP to contain a
  152. * CONNECTION_CLOSE frame. This function fails if it has already been called
  153. * successfully; the information in *f cannot be changed after the first
  154. * successful call to this function.
  155. */
  156. int ossl_quic_tx_packetiser_schedule_conn_close(OSSL_QUIC_TX_PACKETISER *txp,
  157. const OSSL_QUIC_FRAME_CONN_CLOSE *f);
  158. /* Setters for the msg_callback and msg_callback_arg */
  159. void ossl_quic_tx_packetiser_set_msg_callback(OSSL_QUIC_TX_PACKETISER *txp,
  160. ossl_msg_cb msg_callback,
  161. SSL *msg_callback_ssl);
  162. void ossl_quic_tx_packetiser_set_msg_callback_arg(OSSL_QUIC_TX_PACKETISER *txp,
  163. void *msg_callback_arg);
  164. /*
  165. * Determines the next PN which will be used for a given PN space.
  166. */
  167. QUIC_PN ossl_quic_tx_packetiser_get_next_pn(OSSL_QUIC_TX_PACKETISER *txp,
  168. uint32_t pn_space);
  169. /*
  170. * Sets a callback which is called whenever TXP sends an ACK frame. The callee
  171. * must not modify the ACK frame data. Can be used to snoop on PNs being ACKed.
  172. */
  173. void ossl_quic_tx_packetiser_set_ack_tx_cb(OSSL_QUIC_TX_PACKETISER *txp,
  174. void (*cb)(const OSSL_QUIC_FRAME_ACK *ack,
  175. uint32_t pn_space,
  176. void *arg),
  177. void *cb_arg);
  178. # endif
  179. #endif