quic_demux.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. #include "internal/quic_demux.h"
  10. #include "internal/quic_wire_pkt.h"
  11. #include "internal/common.h"
  12. #include <openssl/lhash.h>
  13. #include <openssl/err.h>
  14. #define URXE_DEMUX_STATE_FREE 0 /* on urx_free list */
  15. #define URXE_DEMUX_STATE_PENDING 1 /* on urx_pending list */
  16. #define URXE_DEMUX_STATE_ISSUED 2 /* on neither list */
  17. #define DEMUX_MAX_MSGS_PER_CALL 32
  18. #define DEMUX_DEFAULT_MTU 1500
  19. /* Structure used to track a given connection ID. */
  20. typedef struct quic_demux_conn_st QUIC_DEMUX_CONN;
  21. struct quic_demux_conn_st {
  22. QUIC_DEMUX_CONN *next; /* used when unregistering only */
  23. QUIC_CONN_ID dst_conn_id;
  24. ossl_quic_demux_cb_fn *cb;
  25. void *cb_arg;
  26. };
  27. DEFINE_LHASH_OF_EX(QUIC_DEMUX_CONN);
  28. static unsigned long demux_conn_hash(const QUIC_DEMUX_CONN *conn)
  29. {
  30. size_t i;
  31. unsigned long v = 0;
  32. assert(conn->dst_conn_id.id_len <= QUIC_MAX_CONN_ID_LEN);
  33. for (i = 0; i < conn->dst_conn_id.id_len; ++i)
  34. v ^= ((unsigned long)conn->dst_conn_id.id[i])
  35. << ((i * 8) % (sizeof(unsigned long) * 8));
  36. return v;
  37. }
  38. static int demux_conn_cmp(const QUIC_DEMUX_CONN *a, const QUIC_DEMUX_CONN *b)
  39. {
  40. return !ossl_quic_conn_id_eq(&a->dst_conn_id, &b->dst_conn_id);
  41. }
  42. struct quic_demux_st {
  43. /* The underlying transport BIO with datagram semantics. */
  44. BIO *net_bio;
  45. /*
  46. * QUIC short packets do not contain the length of the connection ID field,
  47. * therefore it must be known contextually. The demuxer requires connection
  48. * IDs of the same length to be used for all incoming packets.
  49. */
  50. size_t short_conn_id_len;
  51. /*
  52. * Our current understanding of the upper bound on an incoming datagram size
  53. * in bytes.
  54. */
  55. size_t mtu;
  56. /* Time retrieval callback. */
  57. OSSL_TIME (*now)(void *arg);
  58. void *now_arg;
  59. /* Hashtable mapping connection IDs to QUIC_DEMUX_CONN structures. */
  60. LHASH_OF(QUIC_DEMUX_CONN) *conns_by_id;
  61. /* The default packet handler, if any. */
  62. ossl_quic_demux_cb_fn *default_cb;
  63. void *default_cb_arg;
  64. /* The stateless reset token checker handler, if any. */
  65. ossl_quic_stateless_reset_cb_fn *reset_token_cb;
  66. void *reset_token_cb_arg;
  67. /*
  68. * List of URXEs which are not currently in use (i.e., not filled with
  69. * unconsumed data). These are moved to the pending list as they are filled.
  70. */
  71. QUIC_URXE_LIST urx_free;
  72. /*
  73. * List of URXEs which are filled with received encrypted data. These are
  74. * removed from this list as we invoke the callbacks for each of them. They
  75. * are then not on any list managed by us; we forget about them until our
  76. * user calls ossl_quic_demux_release_urxe to return the URXE to us, at
  77. * which point we add it to the free list.
  78. */
  79. QUIC_URXE_LIST urx_pending;
  80. /* Whether to use local address support. */
  81. char use_local_addr;
  82. };
  83. QUIC_DEMUX *ossl_quic_demux_new(BIO *net_bio,
  84. size_t short_conn_id_len,
  85. OSSL_TIME (*now)(void *arg),
  86. void *now_arg)
  87. {
  88. QUIC_DEMUX *demux;
  89. demux = OPENSSL_zalloc(sizeof(QUIC_DEMUX));
  90. if (demux == NULL)
  91. return NULL;
  92. demux->net_bio = net_bio;
  93. demux->short_conn_id_len = short_conn_id_len;
  94. /* We update this if possible when we get a BIO. */
  95. demux->mtu = DEMUX_DEFAULT_MTU;
  96. demux->now = now;
  97. demux->now_arg = now_arg;
  98. demux->conns_by_id
  99. = lh_QUIC_DEMUX_CONN_new(demux_conn_hash, demux_conn_cmp);
  100. if (demux->conns_by_id == NULL) {
  101. OPENSSL_free(demux);
  102. return NULL;
  103. }
  104. if (net_bio != NULL
  105. && BIO_dgram_get_local_addr_cap(net_bio)
  106. && BIO_dgram_set_local_addr_enable(net_bio, 1))
  107. demux->use_local_addr = 1;
  108. return demux;
  109. }
  110. static void demux_free_conn_it(QUIC_DEMUX_CONN *conn, void *arg)
  111. {
  112. OPENSSL_free(conn);
  113. }
  114. static void demux_free_urxl(QUIC_URXE_LIST *l)
  115. {
  116. QUIC_URXE *e, *enext;
  117. for (e = ossl_list_urxe_head(l); e != NULL; e = enext) {
  118. enext = ossl_list_urxe_next(e);
  119. ossl_list_urxe_remove(l, e);
  120. OPENSSL_free(e);
  121. }
  122. }
  123. void ossl_quic_demux_free(QUIC_DEMUX *demux)
  124. {
  125. if (demux == NULL)
  126. return;
  127. /* Free all connection structures. */
  128. lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id, demux_free_conn_it, NULL);
  129. lh_QUIC_DEMUX_CONN_free(demux->conns_by_id);
  130. /* Free all URXEs we are holding. */
  131. demux_free_urxl(&demux->urx_free);
  132. demux_free_urxl(&demux->urx_pending);
  133. OPENSSL_free(demux);
  134. }
  135. void ossl_quic_demux_set_bio(QUIC_DEMUX *demux, BIO *net_bio)
  136. {
  137. unsigned int mtu;
  138. demux->net_bio = net_bio;
  139. if (net_bio != NULL) {
  140. /*
  141. * Try to determine our MTU if possible. The BIO is not required to
  142. * support this, in which case we remain at the last known MTU, or our
  143. * initial default.
  144. */
  145. mtu = BIO_dgram_get_mtu(net_bio);
  146. if (mtu >= QUIC_MIN_INITIAL_DGRAM_LEN)
  147. ossl_quic_demux_set_mtu(demux, mtu); /* best effort */
  148. }
  149. }
  150. int ossl_quic_demux_set_mtu(QUIC_DEMUX *demux, unsigned int mtu)
  151. {
  152. if (mtu < QUIC_MIN_INITIAL_DGRAM_LEN)
  153. return 0;
  154. demux->mtu = mtu;
  155. return 1;
  156. }
  157. static QUIC_DEMUX_CONN *demux_get_by_conn_id(QUIC_DEMUX *demux,
  158. const QUIC_CONN_ID *dst_conn_id)
  159. {
  160. QUIC_DEMUX_CONN key;
  161. if (dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN)
  162. return NULL;
  163. key.dst_conn_id = *dst_conn_id;
  164. return lh_QUIC_DEMUX_CONN_retrieve(demux->conns_by_id, &key);
  165. }
  166. int ossl_quic_demux_register(QUIC_DEMUX *demux,
  167. const QUIC_CONN_ID *dst_conn_id,
  168. ossl_quic_demux_cb_fn *cb, void *cb_arg)
  169. {
  170. QUIC_DEMUX_CONN *conn;
  171. if (dst_conn_id == NULL
  172. || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN
  173. || cb == NULL)
  174. return 0;
  175. /* Ensure not already registered. */
  176. if (demux_get_by_conn_id(demux, dst_conn_id) != NULL)
  177. /* Handler already registered with this connection ID. */
  178. return 0;
  179. conn = OPENSSL_zalloc(sizeof(QUIC_DEMUX_CONN));
  180. if (conn == NULL)
  181. return 0;
  182. conn->dst_conn_id = *dst_conn_id;
  183. conn->cb = cb;
  184. conn->cb_arg = cb_arg;
  185. lh_QUIC_DEMUX_CONN_insert(demux->conns_by_id, conn);
  186. return 1;
  187. }
  188. static void demux_unregister(QUIC_DEMUX *demux,
  189. QUIC_DEMUX_CONN *conn)
  190. {
  191. lh_QUIC_DEMUX_CONN_delete(demux->conns_by_id, conn);
  192. OPENSSL_free(conn);
  193. }
  194. int ossl_quic_demux_unregister(QUIC_DEMUX *demux,
  195. const QUIC_CONN_ID *dst_conn_id)
  196. {
  197. QUIC_DEMUX_CONN *conn;
  198. if (dst_conn_id == NULL
  199. || dst_conn_id->id_len > QUIC_MAX_CONN_ID_LEN)
  200. return 0;
  201. conn = demux_get_by_conn_id(demux, dst_conn_id);
  202. if (conn == NULL)
  203. return 0;
  204. demux_unregister(demux, conn);
  205. return 1;
  206. }
  207. struct unreg_arg {
  208. ossl_quic_demux_cb_fn *cb;
  209. void *cb_arg;
  210. QUIC_DEMUX_CONN *head;
  211. };
  212. static void demux_unregister_by_cb(QUIC_DEMUX_CONN *conn, void *arg_)
  213. {
  214. struct unreg_arg *arg = arg_;
  215. if (conn->cb == arg->cb && conn->cb_arg == arg->cb_arg) {
  216. conn->next = arg->head;
  217. arg->head = conn;
  218. }
  219. }
  220. void ossl_quic_demux_unregister_by_cb(QUIC_DEMUX *demux,
  221. ossl_quic_demux_cb_fn *cb,
  222. void *cb_arg)
  223. {
  224. QUIC_DEMUX_CONN *conn, *cnext;
  225. struct unreg_arg arg = {0};
  226. arg.cb = cb;
  227. arg.cb_arg = cb_arg;
  228. lh_QUIC_DEMUX_CONN_doall_arg(demux->conns_by_id,
  229. demux_unregister_by_cb, &arg);
  230. for (conn = arg.head; conn != NULL; conn = cnext) {
  231. cnext = conn->next;
  232. demux_unregister(demux, conn);
  233. }
  234. }
  235. void ossl_quic_demux_set_default_handler(QUIC_DEMUX *demux,
  236. ossl_quic_demux_cb_fn *cb,
  237. void *cb_arg)
  238. {
  239. demux->default_cb = cb;
  240. demux->default_cb_arg = cb_arg;
  241. }
  242. void ossl_quic_demux_set_stateless_reset_handler(
  243. QUIC_DEMUX *demux,
  244. ossl_quic_stateless_reset_cb_fn *cb, void *cb_arg)
  245. {
  246. demux->reset_token_cb = cb;
  247. demux->reset_token_cb_arg = cb_arg;
  248. }
  249. static QUIC_URXE *demux_alloc_urxe(size_t alloc_len)
  250. {
  251. QUIC_URXE *e;
  252. if (alloc_len >= SIZE_MAX - sizeof(QUIC_URXE))
  253. return NULL;
  254. e = OPENSSL_malloc(sizeof(QUIC_URXE) + alloc_len);
  255. if (e == NULL)
  256. return NULL;
  257. ossl_list_urxe_init_elem(e);
  258. e->alloc_len = alloc_len;
  259. e->data_len = 0;
  260. return e;
  261. }
  262. static QUIC_URXE *demux_resize_urxe(QUIC_DEMUX *demux, QUIC_URXE *e,
  263. size_t new_alloc_len)
  264. {
  265. QUIC_URXE *e2, *prev;
  266. if (!ossl_assert(e->demux_state == URXE_DEMUX_STATE_FREE))
  267. /* Never attempt to resize a URXE which is not on the free list. */
  268. return NULL;
  269. prev = ossl_list_urxe_prev(e);
  270. ossl_list_urxe_remove(&demux->urx_free, e);
  271. e2 = OPENSSL_realloc(e, sizeof(QUIC_URXE) + new_alloc_len);
  272. if (e2 == NULL) {
  273. /* Failed to resize, abort. */
  274. if (prev == NULL)
  275. ossl_list_urxe_insert_head(&demux->urx_free, e);
  276. else
  277. ossl_list_urxe_insert_after(&demux->urx_free, prev, e);
  278. return NULL;
  279. }
  280. if (prev == NULL)
  281. ossl_list_urxe_insert_head(&demux->urx_free, e2);
  282. else
  283. ossl_list_urxe_insert_after(&demux->urx_free, prev, e2);
  284. e2->alloc_len = new_alloc_len;
  285. return e2;
  286. }
  287. static QUIC_URXE *demux_reserve_urxe(QUIC_DEMUX *demux, QUIC_URXE *e,
  288. size_t alloc_len)
  289. {
  290. return e->alloc_len < alloc_len ? demux_resize_urxe(demux, e, alloc_len) : e;
  291. }
  292. static int demux_ensure_free_urxe(QUIC_DEMUX *demux, size_t min_num_free)
  293. {
  294. QUIC_URXE *e;
  295. while (ossl_list_urxe_num(&demux->urx_free) < min_num_free) {
  296. e = demux_alloc_urxe(demux->mtu);
  297. if (e == NULL)
  298. return 0;
  299. ossl_list_urxe_insert_tail(&demux->urx_free, e);
  300. e->demux_state = URXE_DEMUX_STATE_FREE;
  301. }
  302. return 1;
  303. }
  304. /*
  305. * Receive datagrams from network, placing them into URXEs.
  306. *
  307. * Returns 1 on success or 0 on failure.
  308. *
  309. * Precondition: at least one URXE is free
  310. * Precondition: there are no pending URXEs
  311. */
  312. static int demux_recv(QUIC_DEMUX *demux)
  313. {
  314. BIO_MSG msg[DEMUX_MAX_MSGS_PER_CALL];
  315. size_t rd, i;
  316. QUIC_URXE *urxe = ossl_list_urxe_head(&demux->urx_free), *unext;
  317. OSSL_TIME now;
  318. /* This should never be called when we have any pending URXE. */
  319. assert(ossl_list_urxe_head(&demux->urx_pending) == NULL);
  320. assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);
  321. if (demux->net_bio == NULL)
  322. /*
  323. * If no BIO is plugged in, treat this as no datagram being available.
  324. */
  325. return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;
  326. /*
  327. * Opportunistically receive as many messages as possible in a single
  328. * syscall, determined by how many free URXEs are available.
  329. */
  330. for (i = 0; i < (ossl_ssize_t)OSSL_NELEM(msg);
  331. ++i, urxe = ossl_list_urxe_next(urxe)) {
  332. if (urxe == NULL) {
  333. /* We need at least one URXE to receive into. */
  334. if (!ossl_assert(i > 0))
  335. return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
  336. break;
  337. }
  338. /* Ensure the URXE is big enough. */
  339. urxe = demux_reserve_urxe(demux, urxe, demux->mtu);
  340. if (urxe == NULL)
  341. /* Allocation error, fail. */
  342. return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
  343. /* Ensure we zero any fields added to BIO_MSG at a later date. */
  344. memset(&msg[i], 0, sizeof(BIO_MSG));
  345. msg[i].data = ossl_quic_urxe_data(urxe);
  346. msg[i].data_len = urxe->alloc_len;
  347. msg[i].peer = &urxe->peer;
  348. BIO_ADDR_clear(&urxe->peer);
  349. if (demux->use_local_addr)
  350. msg[i].local = &urxe->local;
  351. else
  352. BIO_ADDR_clear(&urxe->local);
  353. }
  354. ERR_set_mark();
  355. if (!BIO_recvmmsg(demux->net_bio, msg, sizeof(BIO_MSG), i, 0, &rd)) {
  356. if (BIO_err_is_non_fatal(ERR_peek_last_error())) {
  357. /* Transient error, clear the error and stop. */
  358. ERR_pop_to_mark();
  359. return QUIC_DEMUX_PUMP_RES_TRANSIENT_FAIL;
  360. } else {
  361. /* Non-transient error, do not clear the error. */
  362. ERR_clear_last_mark();
  363. return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
  364. }
  365. }
  366. ERR_clear_last_mark();
  367. now = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
  368. urxe = ossl_list_urxe_head(&demux->urx_free);
  369. for (i = 0; i < rd; ++i, urxe = unext) {
  370. unext = ossl_list_urxe_next(urxe);
  371. /* Set URXE with actual length of received datagram. */
  372. urxe->data_len = msg[i].data_len;
  373. /* Time we received datagram. */
  374. urxe->time = now;
  375. /* Move from free list to pending list. */
  376. ossl_list_urxe_remove(&demux->urx_free, urxe);
  377. ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
  378. urxe->demux_state = URXE_DEMUX_STATE_PENDING;
  379. }
  380. return QUIC_DEMUX_PUMP_RES_OK;
  381. }
  382. /* Extract destination connection ID from the first packet in a datagram. */
  383. static int demux_identify_conn_id(QUIC_DEMUX *demux,
  384. QUIC_URXE *e,
  385. QUIC_CONN_ID *dst_conn_id)
  386. {
  387. return ossl_quic_wire_get_pkt_hdr_dst_conn_id(ossl_quic_urxe_data(e),
  388. e->data_len,
  389. demux->short_conn_id_len,
  390. dst_conn_id);
  391. }
  392. /* Identify the connection structure corresponding to a given URXE. */
  393. static QUIC_DEMUX_CONN *demux_identify_conn(QUIC_DEMUX *demux, QUIC_URXE *e)
  394. {
  395. QUIC_CONN_ID dst_conn_id;
  396. if (!demux_identify_conn_id(demux, e, &dst_conn_id))
  397. /*
  398. * Datagram is so badly malformed we can't get the DCID from the first
  399. * packet in it, so just give up.
  400. */
  401. return NULL;
  402. return demux_get_by_conn_id(demux, &dst_conn_id);
  403. }
  404. /*
  405. * Process a single pending URXE.
  406. * Returning 1 on success, 0 on failure and -1 on stateless reset.
  407. */
  408. static int demux_process_pending_urxe(QUIC_DEMUX *demux, QUIC_URXE *e)
  409. {
  410. QUIC_DEMUX_CONN *conn;
  411. int r;
  412. /* The next URXE we process should be at the head of the pending list. */
  413. if (!ossl_assert(e == ossl_list_urxe_head(&demux->urx_pending)))
  414. return 0;
  415. assert(e->demux_state == URXE_DEMUX_STATE_PENDING);
  416. /*
  417. * Check if the packet ends with a stateless reset token and if it does
  418. * skip it after dropping the connection.
  419. *
  420. * RFC 9000 s. 10.3.1 Detecting a Stateless Reset
  421. * If the last 16 bytes of the datagram are identical in value to
  422. * a stateless reset token, the endpoint MUST enter the draining
  423. * period and not send any further packets on this connection.
  424. *
  425. * Returning a failure here causes the connection to enter the terminating
  426. * state which achieves the desired outcome.
  427. *
  428. * TODO(QUIC FUTURE): only try to match unparsable packets
  429. */
  430. if (demux->reset_token_cb != NULL) {
  431. r = demux->reset_token_cb(ossl_quic_urxe_data(e), e->data_len,
  432. demux->reset_token_cb_arg);
  433. if (r > 0) /* Received a stateless reset */
  434. return -1;
  435. if (r < 0) /* Error during stateless reset detection */
  436. return 0;
  437. }
  438. conn = demux_identify_conn(demux, e);
  439. if (conn == NULL) {
  440. /*
  441. * We could not identify a connection. If we have a default packet
  442. * handler, pass it to the handler. Otherwise, we will never be able to
  443. * process this datagram, so get rid of it.
  444. */
  445. ossl_list_urxe_remove(&demux->urx_pending, e);
  446. if (demux->default_cb != NULL) {
  447. /* Pass to default handler. */
  448. e->demux_state = URXE_DEMUX_STATE_ISSUED;
  449. demux->default_cb(e, demux->default_cb_arg);
  450. } else {
  451. /* Discard. */
  452. ossl_list_urxe_insert_tail(&demux->urx_free, e);
  453. e->demux_state = URXE_DEMUX_STATE_FREE;
  454. }
  455. return 1; /* keep processing pending URXEs */
  456. }
  457. /*
  458. * Remove from list and invoke callback. The URXE now belongs to the
  459. * callback. (QUIC_DEMUX_CONN never has non-NULL cb.)
  460. */
  461. ossl_list_urxe_remove(&demux->urx_pending, e);
  462. e->demux_state = URXE_DEMUX_STATE_ISSUED;
  463. conn->cb(e, conn->cb_arg);
  464. return 1;
  465. }
  466. /* Process pending URXEs to generate callbacks. */
  467. static int demux_process_pending_urxl(QUIC_DEMUX *demux)
  468. {
  469. QUIC_URXE *e;
  470. int ret;
  471. while ((e = ossl_list_urxe_head(&demux->urx_pending)) != NULL)
  472. if ((ret = demux_process_pending_urxe(demux, e)) <= 0)
  473. return ret;
  474. return 1;
  475. }
  476. /*
  477. * Drain the pending URXE list, processing any pending URXEs by making their
  478. * callbacks. If no URXEs are pending, a network read is attempted first.
  479. */
  480. int ossl_quic_demux_pump(QUIC_DEMUX *demux)
  481. {
  482. int ret;
  483. if (ossl_list_urxe_head(&demux->urx_pending) == NULL) {
  484. ret = demux_ensure_free_urxe(demux, DEMUX_MAX_MSGS_PER_CALL);
  485. if (ret != 1)
  486. return QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL;
  487. ret = demux_recv(demux);
  488. if (ret != QUIC_DEMUX_PUMP_RES_OK)
  489. return ret;
  490. /*
  491. * If demux_recv returned successfully, we should always have something.
  492. */
  493. assert(ossl_list_urxe_head(&demux->urx_pending) != NULL);
  494. }
  495. if ((ret = demux_process_pending_urxl(demux)) <= 0)
  496. return ret == 0 ? QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL
  497. : QUIC_DEMUX_PUMP_RES_STATELESS_RESET;
  498. return QUIC_DEMUX_PUMP_RES_OK;
  499. }
  500. /* Artificially inject a packet into the demuxer for testing purposes. */
  501. int ossl_quic_demux_inject(QUIC_DEMUX *demux,
  502. const unsigned char *buf,
  503. size_t buf_len,
  504. const BIO_ADDR *peer,
  505. const BIO_ADDR *local)
  506. {
  507. int ret;
  508. QUIC_URXE *urxe;
  509. ret = demux_ensure_free_urxe(demux, 1);
  510. if (ret != 1)
  511. return 0;
  512. urxe = ossl_list_urxe_head(&demux->urx_free);
  513. assert(urxe->demux_state == URXE_DEMUX_STATE_FREE);
  514. urxe = demux_reserve_urxe(demux, urxe, buf_len);
  515. if (urxe == NULL)
  516. return 0;
  517. memcpy(ossl_quic_urxe_data(urxe), buf, buf_len);
  518. urxe->data_len = buf_len;
  519. if (peer != NULL)
  520. urxe->peer = *peer;
  521. else
  522. BIO_ADDR_clear(&urxe->peer);
  523. if (local != NULL)
  524. urxe->local = *local;
  525. else
  526. BIO_ADDR_clear(&urxe->local);
  527. urxe->time
  528. = demux->now != NULL ? demux->now(demux->now_arg) : ossl_time_zero();
  529. /* Move from free list to pending list. */
  530. ossl_list_urxe_remove(&demux->urx_free, urxe);
  531. ossl_list_urxe_insert_tail(&demux->urx_pending, urxe);
  532. urxe->demux_state = URXE_DEMUX_STATE_PENDING;
  533. return demux_process_pending_urxl(demux) > 0;
  534. }
  535. /* Called by our user to return a URXE to the free list. */
  536. void ossl_quic_demux_release_urxe(QUIC_DEMUX *demux,
  537. QUIC_URXE *e)
  538. {
  539. assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);
  540. assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);
  541. ossl_list_urxe_insert_tail(&demux->urx_free, e);
  542. e->demux_state = URXE_DEMUX_STATE_FREE;
  543. }
  544. void ossl_quic_demux_reinject_urxe(QUIC_DEMUX *demux,
  545. QUIC_URXE *e)
  546. {
  547. assert(ossl_list_urxe_prev(e) == NULL && ossl_list_urxe_next(e) == NULL);
  548. assert(e->demux_state == URXE_DEMUX_STATE_ISSUED);
  549. ossl_list_urxe_insert_head(&demux->urx_pending, e);
  550. e->demux_state = URXE_DEMUX_STATE_PENDING;
  551. }
  552. int ossl_quic_demux_has_pending(const QUIC_DEMUX *demux)
  553. {
  554. return ossl_list_urxe_head(&demux->urx_pending) != NULL;
  555. }