quic_demux.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #ifndef OSSL_QUIC_DEMUX_H
  10. # define OSSL_QUIC_DEMUX_H
  11. # include <openssl/ssl.h>
  12. # include "internal/quic_types.h"
  13. # include "internal/bio_addr.h"
  14. # include "internal/time.h"
  15. # include "internal/list.h"
  16. /*
  17. * QUIC Demuxer
  18. * ============
  19. *
  20. * The QUIC connection demuxer is the entity responsible for receiving datagrams
  21. * from the network via a datagram BIO. It parses packet headers to determine
  22. * each packet's destination connection ID (DCID) and hands off processing of
  23. * the packet to the correct QUIC Record Layer (QRL)'s RX side (known as the
  24. * QRX).
  25. *
  26. * A QRX is instantiated per QUIC connection and contains the cryptographic
  27. * resources needed to decrypt QUIC packets for that connection. Received
  28. * datagrams are passed from the demuxer to the QRX via a callback registered
  29. * for a specific DCID by the QRX; thus the demuxer has no specific knowledge of
  30. * the QRX and is not coupled to it.
  31. *
  32. * A connection may have multiple connection IDs associated with it; a QRX
  33. * handles this simply by registering multiple connection IDs with the demuxer
  34. * via multiple register calls.
  35. *
  36. * URX Queue
  37. * ---------
  38. *
  39. * Since the demuxer must handle the initial reception of datagrams from the OS,
  40. * RX queue management for new, unprocessed datagrams is also handled by the
  41. * demuxer.
  42. *
  43. * The demuxer maintains a queue of Unprocessed RX Entries (URXEs), which store
  44. * unprocessed (i.e., encrypted, unvalidated) data received from the network.
  45. * The URXE queue is designed to allow multiple datagrams to be received in a
  46. * single call to BIO_recvmmsg, where supported.
  47. *
  48. * One URXE is used per received datagram. Each datagram may contain multiple
  49. * packets, however, this is not the demuxer's concern. QUIC prohibits different
  50. * packets in the same datagram from containing different DCIDs; the demuxer
  51. * only considers the DCID of the first packet in a datagram when deciding how
  52. * to route a received datagram, and it is the responsibility of the QRX to
  53. * enforce this rule. Packets other than the first packet in a datagram are not
  54. * examined by the demuxer, and the demuxer does not perform validation of
  55. * packet headers other than to the minimum extent necessary to extract the
  56. * DCID; further parsing and validation of packet headers is the responsibility
  57. * of the QRX.
  58. *
  59. * Rather than defining an opaque interface, the URXE structure internals
  60. * are exposed. Since the demuxer is only exposed to other parts of the QUIC
  61. * implementation internals, this poses no problem, and has a number of
  62. * advantages:
  63. *
  64. * - Fields in the URXE can be allocated to support requirements in other
  65. * components, like the QRX, which would otherwise have to allocate extra
  66. * memory corresponding to each URXE.
  67. *
  68. * - Other components, like the QRX, can keep the URXE in queues of its own
  69. * when it is not being managed by the demuxer.
  70. *
  71. * URX Queue Structure
  72. * -------------------
  73. *
  74. * The URXE queue is maintained as a simple doubly-linked list. URXE entries are
  75. * moved between different lists in their lifecycle (for example, from a free
  76. * list to a pending list and vice versa). The buffer into which datagrams are
  77. * received immediately follows this URXE header structure and is part of the
  78. * same allocation.
  79. */
  80. typedef struct quic_urxe_st QUIC_URXE;
  81. /* Maximum number of packets we allow to exist in one datagram. */
  82. #define QUIC_MAX_PKT_PER_URXE (sizeof(uint64_t) * 8)
  83. struct quic_urxe_st {
  84. OSSL_LIST_MEMBER(urxe, QUIC_URXE);
  85. /*
  86. * The URXE data starts after this structure so we don't need a pointer.
  87. * data_len stores the current length (i.e., the length of the received
  88. * datagram) and alloc_len stores the allocation length. The URXE will be
  89. * reallocated if we need a larger allocation than is available, though this
  90. * should not be common as we will have a good idea of worst-case MTUs up
  91. * front.
  92. */
  93. size_t data_len, alloc_len;
  94. /*
  95. * Bitfields per packet. processed indicates the packet has been processed
  96. * and must not be processed again, hpr_removed indicates header protection
  97. * has already been removed. Used by QRX only; not used by the demuxer.
  98. */
  99. uint64_t processed, hpr_removed;
  100. /*
  101. * Address of peer we received the datagram from, and the local interface
  102. * address we received it on. If local address support is not enabled, local
  103. * is zeroed.
  104. */
  105. BIO_ADDR peer, local;
  106. /*
  107. * Time at which datagram was received (or ossl_time_zero()) if a now
  108. * function was not provided).
  109. */
  110. OSSL_TIME time;
  111. /*
  112. * Used by the QRX to mark whether a datagram has been deferred. Used by the
  113. * QRX only; not used by the demuxer.
  114. */
  115. char deferred;
  116. };
  117. /* Accessors for URXE buffer. */
  118. static ossl_unused ossl_inline unsigned char *
  119. ossl_quic_urxe_data(const QUIC_URXE *e)
  120. {
  121. return (unsigned char *)&e[1];
  122. }
  123. static ossl_unused ossl_inline unsigned char *
  124. ossl_quic_urxe_data_end(const QUIC_URXE *e)
  125. {
  126. return ossl_quic_urxe_data(e) + e->data_len;
  127. }
  128. /* List structure tracking a queue of URXEs. */
  129. DEFINE_LIST_OF(urxe, QUIC_URXE);
  130. typedef OSSL_LIST(urxe) QUIC_URXE_LIST;
  131. /*
  132. * List management helpers. These are used by the demuxer but can also be used
  133. * by users of the demuxer to manage URXEs.
  134. */
  135. void ossl_quic_urxe_remove(QUIC_URXE_LIST *l, QUIC_URXE *e);
  136. void ossl_quic_urxe_insert_head(QUIC_URXE_LIST *l, QUIC_URXE *e);
  137. void ossl_quic_urxe_insert_tail(QUIC_URXE_LIST *l, QUIC_URXE *e);
  138. /* Opaque type representing a demuxer. */
  139. typedef struct quic_demux_st QUIC_DEMUX;
  140. /*
  141. * Called when a datagram is received for a given connection ID.
  142. *
  143. * e is a URXE containing the datagram payload. It is permissible for the callee
  144. * to mutate this buffer; once the demuxer calls this callback, it will never
  145. * read the buffer again.
  146. *
  147. * The callee must arrange for ossl_quic_demux_release_urxe to be called on the URXE
  148. * at some point in the future (this need not be before the callback returns).
  149. *
  150. * At the time the callback is made, the URXE will not be in any queue,
  151. * therefore the callee can use the prev and next fields as it wishes.
  152. */
  153. typedef void (ossl_quic_demux_cb_fn)(QUIC_URXE *e, void *arg);
  154. /*
  155. * Creates a new demuxer. The given BIO is used to receive datagrams from the
  156. * network using BIO_recvmmsg. short_conn_id_len is the length of destination
  157. * connection IDs used in RX'd packets; it must have the same value for all
  158. * connections used on a socket. default_urxe_alloc_len is the buffer size to
  159. * receive datagrams into; it should be a value large enough to contain any
  160. * received datagram according to local MTUs, etc.
  161. *
  162. * now is an optional function used to determine the time a datagram was
  163. * received. now_arg is an opaque argument passed to the function. If now is
  164. * NULL, ossl_time_zero() is used as the datagram reception time.
  165. */
  166. QUIC_DEMUX *ossl_quic_demux_new(BIO *net_bio,
  167. size_t short_conn_id_len,
  168. size_t default_urxe_alloc_len,
  169. OSSL_TIME (*now)(void *arg),
  170. void *now_arg);
  171. /*
  172. * Destroy a demuxer. All URXEs must have been released back to the demuxer
  173. * before calling this. No-op if demux is NULL.
  174. */
  175. void ossl_quic_demux_free(QUIC_DEMUX *demux);
  176. /*
  177. * Register a datagram handler callback for a connection ID.
  178. *
  179. * ossl_quic_demux_pump will call the specified function if it receives a datagram
  180. * the first packet of which has the specified destination connection ID.
  181. *
  182. * It is assumed all packets in a datagram have the same destination connection
  183. * ID (as QUIC mandates this), but it is the user's responsibility to check for
  184. * this and reject subsequent packets in a datagram that violate this rule.
  185. *
  186. * dst_conn_id is a destination connection ID; it is copied and need not remain
  187. * valid after this function returns.
  188. *
  189. * cb_arg is passed to cb when it is called. For information on the callback,
  190. * see its typedef above.
  191. *
  192. * Only one handler can be set for a given connection ID. If a handler is
  193. * already set for the given connection ID, returns 0.
  194. *
  195. * Returns 1 on success or 0 on failure.
  196. */
  197. int ossl_quic_demux_register(QUIC_DEMUX *demux,
  198. const QUIC_CONN_ID *dst_conn_id,
  199. ossl_quic_demux_cb_fn *cb,
  200. void *cb_arg);
  201. /*
  202. * Unregisters any datagram handler callback set for the given connection ID.
  203. * Fails if no handler is registered for the given connection ID.
  204. *
  205. * Returns 1 on success or 0 on failure.
  206. */
  207. int ossl_quic_demux_unregister(QUIC_DEMUX *demux,
  208. const QUIC_CONN_ID *dst_conn_id);
  209. /*
  210. * Unregisters any datagram handler callback from all connection IDs it is used
  211. * for. cb and cb_arg must both match the values passed to
  212. * ossl_quic_demux_register.
  213. */
  214. void ossl_quic_demux_unregister_by_cb(QUIC_DEMUX *demux,
  215. ossl_quic_demux_cb_fn *cb,
  216. void *cb_arg);
  217. /*
  218. * Releases a URXE back to the demuxer. No reference must be made to the URXE or
  219. * its buffer after calling this function. The URXE must not be in any queue;
  220. * that is, its prev and next pointers must be NULL.
  221. */
  222. void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux,
  223. QUIC_URXE *e);
  224. /*
  225. * Process any unprocessed RX'd datagrams, by calling registered callbacks by
  226. * connection ID, reading more datagrams from the BIO if necessary.
  227. *
  228. * Returns 1 on success or 0 on failure.
  229. */
  230. int ossl_quic_demux_pump(QUIC_DEMUX *demux);
  231. /*
  232. * Artificially inject a packet into the demuxer for testing purposes. The
  233. * buffer must not exceed the URXE size being used by the demuxer.
  234. *
  235. * If peer or local are NULL, their respective fields are zeroed in the injected
  236. * URXE.
  237. *
  238. * Returns 1 on success or 0 on failure.
  239. */
  240. int ossl_quic_demux_inject(QUIC_DEMUX *demux,
  241. const unsigned char *buf,
  242. size_t buf_len,
  243. const BIO_ADDR *peer,
  244. const BIO_ADDR *local);
  245. #endif