2
0

quic_demux.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright 2022-2024 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/quic_predef.h"
  14. # include "internal/bio_addr.h"
  15. # include "internal/time.h"
  16. # include "internal/list.h"
  17. # ifndef OPENSSL_NO_QUIC
  18. /*
  19. * QUIC Demuxer
  20. * ============
  21. *
  22. * The QUIC connection demuxer is the entity responsible for receiving datagrams
  23. * from the network via a datagram BIO. It parses the headers of the first
  24. * packet in the datagram to determine that packet's DCID and hands off
  25. * processing of the entire datagram to a single callback function which can
  26. * decide how to handle and route the datagram, for example by looking up
  27. * a QRX instance and injecting the URXE into that QRX.
  28. *
  29. * A QRX will typically be instantiated per QUIC connection and contains the
  30. * cryptographic resources needed to decrypt QUIC packets for that connection.
  31. * However, it is up to the callback function to handle routing, for example by
  32. * consulting a LCIDM instance. Thus the demuxer has no specific knowledge of
  33. * any QRX and is not coupled to it. All CID knowledge is also externalised into
  34. * a LCIDM or other CID state tracking object, without the DEMUX being coupled
  35. * to any particular DCID resolution mechanism.
  36. *
  37. * URX Queue
  38. * ---------
  39. *
  40. * Since the demuxer must handle the initial reception of datagrams from the OS,
  41. * RX queue management for new, unprocessed datagrams is also handled by the
  42. * demuxer.
  43. *
  44. * The demuxer maintains a queue of Unprocessed RX Entries (URXEs), which store
  45. * unprocessed (i.e., encrypted, unvalidated) data received from the network.
  46. * The URXE queue is designed to allow multiple datagrams to be received in a
  47. * single call to BIO_recvmmsg, where supported.
  48. *
  49. * One URXE is used per received datagram. Each datagram may contain multiple
  50. * packets, however, this is not the demuxer's concern. QUIC prohibits different
  51. * packets in the same datagram from containing different DCIDs; the demuxer
  52. * only considers the DCID of the first packet in a datagram when deciding how
  53. * to route a received datagram, and it is the responsibility of the QRX to
  54. * enforce this rule. Packets other than the first packet in a datagram are not
  55. * examined by the demuxer, and the demuxer does not perform validation of
  56. * packet headers other than to the minimum extent necessary to extract the
  57. * DCID; further parsing and validation of packet headers is the responsibility
  58. * of the QRX.
  59. *
  60. * Rather than defining an opaque interface, the URXE structure internals
  61. * are exposed. Since the demuxer is only exposed to other parts of the QUIC
  62. * implementation internals, this poses no problem, and has a number of
  63. * advantages:
  64. *
  65. * - Fields in the URXE can be allocated to support requirements in other
  66. * components, like the QRX, which would otherwise have to allocate extra
  67. * memory corresponding to each URXE.
  68. *
  69. * - Other components, like the QRX, can keep the URXE in queues of its own
  70. * when it is not being managed by the demuxer.
  71. *
  72. * URX Queue Structure
  73. * -------------------
  74. *
  75. * The URXE queue is maintained as a simple doubly-linked list. URXE entries are
  76. * moved between different lists in their lifecycle (for example, from a free
  77. * list to a pending list and vice versa). The buffer into which datagrams are
  78. * received immediately follows this URXE header structure and is part of the
  79. * same allocation.
  80. */
  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. * This monotonically increases with each datagram received. It is used for
  102. * diagnostic purposes only.
  103. */
  104. uint64_t datagram_id;
  105. /*
  106. * Address of peer we received the datagram from, and the local interface
  107. * address we received it on. If local address support is not enabled, local
  108. * is zeroed.
  109. */
  110. BIO_ADDR peer, local;
  111. /*
  112. * Time at which datagram was received (or ossl_time_zero()) if a now
  113. * function was not provided).
  114. */
  115. OSSL_TIME time;
  116. /*
  117. * Used by the QRX to mark whether a datagram has been deferred. Used by the
  118. * QRX only; not used by the demuxer.
  119. */
  120. char deferred;
  121. /*
  122. * Used by the DEMUX to track if a URXE has been handed out. Used primarily
  123. * for debugging purposes.
  124. */
  125. char demux_state;
  126. };
  127. /* Accessors for URXE buffer. */
  128. static ossl_unused ossl_inline unsigned char *
  129. ossl_quic_urxe_data(const QUIC_URXE *e)
  130. {
  131. return (unsigned char *)&e[1];
  132. }
  133. static ossl_unused ossl_inline unsigned char *
  134. ossl_quic_urxe_data_end(const QUIC_URXE *e)
  135. {
  136. return ossl_quic_urxe_data(e) + e->data_len;
  137. }
  138. /* List structure tracking a queue of URXEs. */
  139. DEFINE_LIST_OF(urxe, QUIC_URXE);
  140. typedef OSSL_LIST(urxe) QUIC_URXE_LIST;
  141. /*
  142. * List management helpers. These are used by the demuxer but can also be used
  143. * by users of the demuxer to manage URXEs.
  144. */
  145. void ossl_quic_urxe_remove(QUIC_URXE_LIST *l, QUIC_URXE *e);
  146. void ossl_quic_urxe_insert_head(QUIC_URXE_LIST *l, QUIC_URXE *e);
  147. void ossl_quic_urxe_insert_tail(QUIC_URXE_LIST *l, QUIC_URXE *e);
  148. /*
  149. * Called when a datagram is received for a given connection ID.
  150. *
  151. * e is a URXE containing the datagram payload. It is permissible for the callee
  152. * to mutate this buffer; once the demuxer calls this callback, it will never
  153. * read the buffer again.
  154. *
  155. * If a DCID was identified for the datagram, dcid is non-NULL; otherwise
  156. * it is NULL.
  157. *
  158. * The callee must arrange for ossl_quic_demux_release_urxe or
  159. * ossl_quic_demux_reinject_urxe to be called on the URXE at some point in the
  160. * future (this need not be before the callback returns).
  161. *
  162. * At the time the callback is made, the URXE will not be in any queue,
  163. * therefore the callee can use the prev and next fields as it wishes.
  164. */
  165. typedef void (ossl_quic_demux_cb_fn)(QUIC_URXE *e, void *arg,
  166. const QUIC_CONN_ID *dcid);
  167. /*
  168. * Creates a new demuxer. The given BIO is used to receive datagrams from the
  169. * network using BIO_recvmmsg. short_conn_id_len is the length of destination
  170. * connection IDs used in RX'd packets; it must have the same value for all
  171. * connections used on a socket. default_urxe_alloc_len is the buffer size to
  172. * receive datagrams into; it should be a value large enough to contain any
  173. * received datagram according to local MTUs, etc.
  174. *
  175. * now is an optional function used to determine the time a datagram was
  176. * received. now_arg is an opaque argument passed to the function. If now is
  177. * NULL, ossl_time_zero() is used as the datagram reception time.
  178. */
  179. QUIC_DEMUX *ossl_quic_demux_new(BIO *net_bio,
  180. size_t short_conn_id_len,
  181. OSSL_TIME (*now)(void *arg),
  182. void *now_arg);
  183. /*
  184. * Destroy a demuxer. All URXEs must have been released back to the demuxer
  185. * before calling this. No-op if demux is NULL.
  186. */
  187. void ossl_quic_demux_free(QUIC_DEMUX *demux);
  188. /*
  189. * Changes the BIO which the demuxer reads from. This also sets the MTU if the
  190. * BIO supports querying the MTU.
  191. */
  192. void ossl_quic_demux_set_bio(QUIC_DEMUX *demux, BIO *net_bio);
  193. /*
  194. * Changes the MTU in bytes we use to receive datagrams.
  195. */
  196. int ossl_quic_demux_set_mtu(QUIC_DEMUX *demux, unsigned int mtu);
  197. /*
  198. * Set the default packet handler. This is used for incoming packets which don't
  199. * match a registered DCID. This is only needed for servers. If a default packet
  200. * handler is not set, a packet which doesn't match a registered DCID is
  201. * silently dropped. A default packet handler may be unset by passing NULL.
  202. *
  203. * The handler is responsible for ensuring that ossl_quic_demux_reinject_urxe or
  204. * ossl_quic_demux_release_urxe is called on the passed packet at some point in
  205. * the future, which may or may not be before the handler returns.
  206. */
  207. void ossl_quic_demux_set_default_handler(QUIC_DEMUX *demux,
  208. ossl_quic_demux_cb_fn *cb,
  209. void *cb_arg);
  210. /*
  211. * Releases a URXE back to the demuxer. No reference must be made to the URXE or
  212. * its buffer after calling this function. The URXE must not be in any queue;
  213. * that is, its prev and next pointers must be NULL.
  214. */
  215. void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux,
  216. QUIC_URXE *e);
  217. /*
  218. * Reinjects a URXE which was issued to a registered DCID callback or the
  219. * default packet handler callback back into the pending queue. This is useful
  220. * when a packet has been handled by the default packet handler callback such
  221. * that a DCID has now been registered and can be dispatched normally by DCID.
  222. * Once this has been called, the caller must not touch the URXE anymore and
  223. * must not also call ossl_quic_demux_release_urxe().
  224. *
  225. * The URXE is reinjected at the head of the queue, so it will be reprocessed
  226. * immediately.
  227. */
  228. void ossl_quic_demux_reinject_urxe(QUIC_DEMUX *demux,
  229. QUIC_URXE *e);
  230. /*
  231. * Process any unprocessed RX'd datagrams, by calling registered callbacks by
  232. * connection ID, reading more datagrams from the BIO if necessary.
  233. *
  234. * Returns one of the following values:
  235. *
  236. * QUIC_DEMUX_PUMP_RES_OK
  237. * At least one incoming datagram was processed.
  238. *
  239. * QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL
  240. * No more incoming datagrams are currently available.
  241. * Call again later.
  242. *
  243. * QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL
  244. * Either the network read BIO has failed in a non-transient fashion, or
  245. * the QUIC implementation has encountered an internal state, assertion
  246. * or allocation error. The caller should tear down the connection
  247. * similarly to in the case of a protocol violation.
  248. *
  249. */
  250. #define QUIC_DEMUX_PUMP_RES_OK 1
  251. #define QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL (-1)
  252. #define QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL (-2)
  253. int ossl_quic_demux_pump(QUIC_DEMUX *demux);
  254. /*
  255. * Artificially inject a packet into the demuxer for testing purposes. The
  256. * buffer must not exceed the URXE size being used by the demuxer.
  257. *
  258. * If peer or local are NULL, their respective fields are zeroed in the injected
  259. * URXE.
  260. *
  261. * Returns 1 on success or 0 on failure.
  262. */
  263. int ossl_quic_demux_inject(QUIC_DEMUX *demux,
  264. const unsigned char *buf,
  265. size_t buf_len,
  266. const BIO_ADDR *peer,
  267. const BIO_ADDR *local);
  268. /*
  269. * Returns 1 if there are any pending URXEs.
  270. */
  271. int ossl_quic_demux_has_pending(const QUIC_DEMUX *demux);
  272. # endif
  273. #endif