quic_port.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Copyright 2023-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. #include "internal/quic_port.h"
  10. #include "internal/quic_channel.h"
  11. #include "internal/quic_lcidm.h"
  12. #include "internal/quic_srtm.h"
  13. #include "quic_port_local.h"
  14. #include "quic_channel_local.h"
  15. #include "quic_engine_local.h"
  16. #include "../ssl_local.h"
  17. /*
  18. * QUIC Port Structure
  19. * ===================
  20. */
  21. #define INIT_DCID_LEN 8
  22. static int port_init(QUIC_PORT *port);
  23. static void port_cleanup(QUIC_PORT *port);
  24. static OSSL_TIME get_time(void *arg);
  25. static void port_default_packet_handler(QUIC_URXE *e, void *arg,
  26. const QUIC_CONN_ID *dcid);
  27. static void port_rx_pre(QUIC_PORT *port);
  28. DEFINE_LIST_OF_IMPL(ch, QUIC_CHANNEL);
  29. DEFINE_LIST_OF_IMPL(port, QUIC_PORT);
  30. QUIC_PORT *ossl_quic_port_new(const QUIC_PORT_ARGS *args)
  31. {
  32. QUIC_PORT *port;
  33. if ((port = OPENSSL_zalloc(sizeof(QUIC_PORT))) == NULL)
  34. return NULL;
  35. port->engine = args->engine;
  36. port->channel_ctx = args->channel_ctx;
  37. port->is_multi_conn = args->is_multi_conn;
  38. if (!port_init(port)) {
  39. OPENSSL_free(port);
  40. return NULL;
  41. }
  42. return port;
  43. }
  44. void ossl_quic_port_free(QUIC_PORT *port)
  45. {
  46. if (port == NULL)
  47. return;
  48. port_cleanup(port);
  49. OPENSSL_free(port);
  50. }
  51. static int port_init(QUIC_PORT *port)
  52. {
  53. size_t rx_short_dcid_len = (port->is_multi_conn ? INIT_DCID_LEN : 0);
  54. if (port->engine == NULL || port->channel_ctx == NULL)
  55. goto err;
  56. if ((port->err_state = OSSL_ERR_STATE_new()) == NULL)
  57. goto err;
  58. if ((port->demux = ossl_quic_demux_new(/*BIO=*/NULL,
  59. /*Short CID Len=*/rx_short_dcid_len,
  60. get_time, port)) == NULL)
  61. goto err;
  62. ossl_quic_demux_set_default_handler(port->demux,
  63. port_default_packet_handler,
  64. port);
  65. if ((port->srtm = ossl_quic_srtm_new(port->engine->libctx,
  66. port->engine->propq)) == NULL)
  67. goto err;
  68. if ((port->lcidm = ossl_quic_lcidm_new(port->engine->libctx,
  69. rx_short_dcid_len)) == NULL)
  70. goto err;
  71. port->rx_short_dcid_len = (unsigned char)rx_short_dcid_len;
  72. port->tx_init_dcid_len = INIT_DCID_LEN;
  73. port->state = QUIC_PORT_STATE_RUNNING;
  74. ossl_list_port_insert_tail(&port->engine->port_list, port);
  75. port->on_engine_list = 1;
  76. return 1;
  77. err:
  78. port_cleanup(port);
  79. return 0;
  80. }
  81. static void port_cleanup(QUIC_PORT *port)
  82. {
  83. assert(ossl_list_ch_num(&port->channel_list) == 0);
  84. ossl_quic_demux_free(port->demux);
  85. port->demux = NULL;
  86. ossl_quic_srtm_free(port->srtm);
  87. port->srtm = NULL;
  88. ossl_quic_lcidm_free(port->lcidm);
  89. port->lcidm = NULL;
  90. OSSL_ERR_STATE_free(port->err_state);
  91. port->err_state = NULL;
  92. if (port->on_engine_list) {
  93. ossl_list_port_remove(&port->engine->port_list, port);
  94. port->on_engine_list = 0;
  95. }
  96. }
  97. static void port_transition_failed(QUIC_PORT *port)
  98. {
  99. if (port->state == QUIC_PORT_STATE_FAILED)
  100. return;
  101. port->state = QUIC_PORT_STATE_FAILED;
  102. }
  103. int ossl_quic_port_is_running(const QUIC_PORT *port)
  104. {
  105. return port->state == QUIC_PORT_STATE_RUNNING;
  106. }
  107. QUIC_ENGINE *ossl_quic_port_get0_engine(QUIC_PORT *port)
  108. {
  109. return port->engine;
  110. }
  111. QUIC_REACTOR *ossl_quic_port_get0_reactor(QUIC_PORT *port)
  112. {
  113. return ossl_quic_engine_get0_reactor(port->engine);
  114. }
  115. QUIC_DEMUX *ossl_quic_port_get0_demux(QUIC_PORT *port)
  116. {
  117. return port->demux;
  118. }
  119. CRYPTO_MUTEX *ossl_quic_port_get0_mutex(QUIC_PORT *port)
  120. {
  121. return ossl_quic_engine_get0_mutex(port->engine);
  122. }
  123. OSSL_TIME ossl_quic_port_get_time(QUIC_PORT *port)
  124. {
  125. return ossl_quic_engine_get_time(port->engine);
  126. }
  127. static OSSL_TIME get_time(void *port)
  128. {
  129. return ossl_quic_port_get_time((QUIC_PORT *)port);
  130. }
  131. int ossl_quic_port_get_rx_short_dcid_len(const QUIC_PORT *port)
  132. {
  133. return port->rx_short_dcid_len;
  134. }
  135. int ossl_quic_port_get_tx_init_dcid_len(const QUIC_PORT *port)
  136. {
  137. return port->tx_init_dcid_len;
  138. }
  139. /*
  140. * QUIC Port: Network BIO Configuration
  141. * ====================================
  142. */
  143. /* Determines whether we can support a given poll descriptor. */
  144. static int validate_poll_descriptor(const BIO_POLL_DESCRIPTOR *d)
  145. {
  146. if (d->type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD && d->value.fd < 0) {
  147. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  148. return 0;
  149. }
  150. return 1;
  151. }
  152. BIO *ossl_quic_port_get_net_rbio(QUIC_PORT *port)
  153. {
  154. return port->net_rbio;
  155. }
  156. BIO *ossl_quic_port_get_net_wbio(QUIC_PORT *port)
  157. {
  158. return port->net_wbio;
  159. }
  160. static int port_update_poll_desc(QUIC_PORT *port, BIO *net_bio, int for_write)
  161. {
  162. BIO_POLL_DESCRIPTOR d = {0};
  163. if (net_bio == NULL
  164. || (!for_write && !BIO_get_rpoll_descriptor(net_bio, &d))
  165. || (for_write && !BIO_get_wpoll_descriptor(net_bio, &d)))
  166. /* Non-pollable BIO */
  167. d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
  168. if (!validate_poll_descriptor(&d))
  169. return 0;
  170. /*
  171. * TODO(QUIC MULTIPORT): We currently only support one port per
  172. * engine/domain. This is necessitated because QUIC_REACTOR only supports a
  173. * single pollable currently. In the future, once complete polling
  174. * infrastructure has been implemented, this limitation can be removed.
  175. *
  176. * For now, just update the descriptor on the engine's reactor as we are
  177. * guaranteed to be the only port under it.
  178. */
  179. if (for_write)
  180. ossl_quic_reactor_set_poll_w(&port->engine->rtor, &d);
  181. else
  182. ossl_quic_reactor_set_poll_r(&port->engine->rtor, &d);
  183. return 1;
  184. }
  185. int ossl_quic_port_update_poll_descriptors(QUIC_PORT *port)
  186. {
  187. int ok = 1;
  188. if (!port_update_poll_desc(port, port->net_rbio, /*for_write=*/0))
  189. ok = 0;
  190. if (!port_update_poll_desc(port, port->net_wbio, /*for_write=*/1))
  191. ok = 0;
  192. return ok;
  193. }
  194. /*
  195. * QUIC_PORT does not ref any BIO it is provided with, nor is any ref
  196. * transferred to it. The caller (e.g., QUIC_CONNECTION) is responsible for
  197. * ensuring the BIO lasts until the channel is freed or the BIO is switched out
  198. * for another BIO by a subsequent successful call to this function.
  199. */
  200. int ossl_quic_port_set_net_rbio(QUIC_PORT *port, BIO *net_rbio)
  201. {
  202. if (port->net_rbio == net_rbio)
  203. return 1;
  204. if (!port_update_poll_desc(port, net_rbio, /*for_write=*/0))
  205. return 0;
  206. ossl_quic_demux_set_bio(port->demux, net_rbio);
  207. port->net_rbio = net_rbio;
  208. return 1;
  209. }
  210. int ossl_quic_port_set_net_wbio(QUIC_PORT *port, BIO *net_wbio)
  211. {
  212. QUIC_CHANNEL *ch;
  213. if (port->net_wbio == net_wbio)
  214. return 1;
  215. if (!port_update_poll_desc(port, net_wbio, /*for_write=*/1))
  216. return 0;
  217. LIST_FOREACH(ch, ch, &port->channel_list)
  218. ossl_qtx_set_bio(ch->qtx, net_wbio);
  219. port->net_wbio = net_wbio;
  220. return 1;
  221. }
  222. /*
  223. * QUIC Port: Channel Lifecycle
  224. * ============================
  225. */
  226. static SSL *port_new_handshake_layer(QUIC_PORT *port)
  227. {
  228. SSL *tls = NULL;
  229. SSL_CONNECTION *tls_conn = NULL;
  230. tls = ossl_ssl_connection_new_int(port->channel_ctx, TLS_method());
  231. if (tls == NULL || (tls_conn = SSL_CONNECTION_FROM_SSL(tls)) == NULL)
  232. return NULL;
  233. /* Override the user_ssl of the inner connection. */
  234. tls_conn->s3.flags |= TLS1_FLAGS_QUIC;
  235. /* Restrict options derived from the SSL_CTX. */
  236. tls_conn->options &= OSSL_QUIC_PERMITTED_OPTIONS_CONN;
  237. tls_conn->pha_enabled = 0;
  238. return tls;
  239. }
  240. static QUIC_CHANNEL *port_make_channel(QUIC_PORT *port, SSL *tls, int is_server)
  241. {
  242. QUIC_CHANNEL_ARGS args = {0};
  243. QUIC_CHANNEL *ch;
  244. args.port = port;
  245. args.is_server = is_server;
  246. args.tls = (tls != NULL ? tls : port_new_handshake_layer(port));
  247. args.lcidm = port->lcidm;
  248. args.srtm = port->srtm;
  249. if (args.tls == NULL)
  250. return NULL;
  251. #ifndef OPENSSL_NO_QLOG
  252. args.use_qlog = 1; /* disabled if env not set */
  253. args.qlog_title = args.tls->ctx->qlog_title;
  254. #endif
  255. ch = ossl_quic_channel_new(&args);
  256. if (ch == NULL) {
  257. if (tls == NULL)
  258. SSL_free(args.tls);
  259. return NULL;
  260. }
  261. return ch;
  262. }
  263. QUIC_CHANNEL *ossl_quic_port_create_outgoing(QUIC_PORT *port, SSL *tls)
  264. {
  265. return port_make_channel(port, tls, /*is_server=*/0);
  266. }
  267. QUIC_CHANNEL *ossl_quic_port_create_incoming(QUIC_PORT *port, SSL *tls)
  268. {
  269. QUIC_CHANNEL *ch;
  270. assert(port->tserver_ch == NULL);
  271. ch = port_make_channel(port, tls, /*is_server=*/1);
  272. port->tserver_ch = ch;
  273. port->is_server = 1;
  274. return ch;
  275. }
  276. /*
  277. * QUIC Port: Ticker-Mutator
  278. * =========================
  279. */
  280. /*
  281. * Tick function for this port. This does everything related to network I/O for
  282. * this port's network BIOs, and services child channels.
  283. */
  284. void ossl_quic_port_subtick(QUIC_PORT *port, QUIC_TICK_RESULT *res,
  285. uint32_t flags)
  286. {
  287. QUIC_CHANNEL *ch;
  288. res->net_read_desired = 0;
  289. res->net_write_desired = 0;
  290. res->tick_deadline = ossl_time_infinite();
  291. if (!port->engine->inhibit_tick) {
  292. /* Handle any incoming data from network. */
  293. if (ossl_quic_port_is_running(port))
  294. port_rx_pre(port);
  295. /* Iterate through all channels and service them. */
  296. LIST_FOREACH(ch, ch, &port->channel_list) {
  297. QUIC_TICK_RESULT subr = {0};
  298. ossl_quic_channel_subtick(ch, &subr, flags);
  299. ossl_quic_tick_result_merge_into(res, &subr);
  300. }
  301. }
  302. }
  303. /* Process incoming datagrams, if any. */
  304. static void port_rx_pre(QUIC_PORT *port)
  305. {
  306. int ret;
  307. /*
  308. * Originally, this check (don't RX before we have sent anything if we are
  309. * not a server, because there can't be anything) was just intended as a
  310. * minor optimisation. However, it is actually required on Windows, and
  311. * removing this check will cause Windows to break.
  312. *
  313. * The reason is that under Win32, recvfrom() does not work on a UDP socket
  314. * which has not had bind() called (???). However, calling sendto() will
  315. * automatically bind an unbound UDP socket. Therefore, if we call a Winsock
  316. * recv-type function before calling a Winsock send-type function, that call
  317. * will fail with WSAEINVAL, which we will regard as a permanent network
  318. * error.
  319. *
  320. * Therefore, this check is essential as we do not require our API users to
  321. * bind a socket first when using the API in client mode.
  322. */
  323. if (!port->is_server && !port->have_sent_any_pkt)
  324. return;
  325. /*
  326. * Get DEMUX to BIO_recvmmsg from the network and queue incoming datagrams
  327. * to the appropriate QRX instances.
  328. */
  329. ret = ossl_quic_demux_pump(port->demux);
  330. if (ret == QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL)
  331. /*
  332. * We don't care about transient failure, but permanent failure means we
  333. * should tear down the port. All connections skip straight to the
  334. * Terminated state as there is no point trying to send CONNECTION_CLOSE
  335. * frames if the network BIO is not operating correctly.
  336. */
  337. ossl_quic_port_raise_net_error(port, NULL);
  338. }
  339. /*
  340. * Handles an incoming connection request and potentially decides to make a
  341. * connection from it. If a new connection is made, the new channel is written
  342. * to *new_ch.
  343. */
  344. static void port_on_new_conn(QUIC_PORT *port, const BIO_ADDR *peer,
  345. const QUIC_CONN_ID *scid,
  346. const QUIC_CONN_ID *dcid,
  347. QUIC_CHANNEL **new_ch)
  348. {
  349. if (port->tserver_ch != NULL) {
  350. /* Specially assign to existing channel */
  351. if (!ossl_quic_channel_on_new_conn(port->tserver_ch, peer, scid, dcid))
  352. return;
  353. *new_ch = port->tserver_ch;
  354. port->tserver_ch = NULL;
  355. return;
  356. }
  357. }
  358. static int port_try_handle_stateless_reset(QUIC_PORT *port, const QUIC_URXE *e)
  359. {
  360. size_t i;
  361. const unsigned char *data = ossl_quic_urxe_data(e);
  362. void *opaque = NULL;
  363. /*
  364. * Perform some fast and cheap checks for a packet not being a stateless
  365. * reset token. RFC 9000 s. 10.3 specifies this layout for stateless
  366. * reset packets:
  367. *
  368. * Stateless Reset {
  369. * Fixed Bits (2) = 1,
  370. * Unpredictable Bits (38..),
  371. * Stateless Reset Token (128),
  372. * }
  373. *
  374. * It also specifies:
  375. * However, endpoints MUST treat any packet ending in a valid
  376. * stateless reset token as a Stateless Reset, as other QUIC
  377. * versions might allow the use of a long header.
  378. *
  379. * We can rapidly check for the minimum length and that the first pair
  380. * of bits in the first byte are 01 or 11.
  381. *
  382. * The function returns 1 if it is a stateless reset packet, 0 if it isn't
  383. * and -1 if an error was encountered.
  384. */
  385. if (e->data_len < QUIC_STATELESS_RESET_TOKEN_LEN + 5
  386. || (0100 & *data) != 0100)
  387. return 0;
  388. for (i = 0;; ++i) {
  389. if (!ossl_quic_srtm_lookup(port->srtm,
  390. (QUIC_STATELESS_RESET_TOKEN *)(data + e->data_len
  391. - sizeof(QUIC_STATELESS_RESET_TOKEN)),
  392. i, &opaque, NULL))
  393. break;
  394. assert(opaque != NULL);
  395. ossl_quic_channel_on_stateless_reset((QUIC_CHANNEL *)opaque);
  396. }
  397. return i > 0;
  398. }
  399. /*
  400. * This is called by the demux when we get a packet not destined for any known
  401. * DCID.
  402. */
  403. static void port_default_packet_handler(QUIC_URXE *e, void *arg,
  404. const QUIC_CONN_ID *dcid)
  405. {
  406. QUIC_PORT *port = arg;
  407. PACKET pkt;
  408. QUIC_PKT_HDR hdr;
  409. QUIC_CHANNEL *ch = NULL, *new_ch = NULL;
  410. /* Don't handle anything if we are no longer running. */
  411. if (!ossl_quic_port_is_running(port))
  412. goto undesirable;
  413. if (port_try_handle_stateless_reset(port, e))
  414. goto undesirable;
  415. if (dcid != NULL
  416. && ossl_quic_lcidm_lookup(port->lcidm, dcid, NULL,
  417. (void **)&ch)) {
  418. assert(ch != NULL);
  419. ossl_quic_channel_inject(ch, e);
  420. return;
  421. }
  422. /*
  423. * If we have an incoming packet which doesn't match any existing connection
  424. * we assume this is an attempt to make a new connection. Currently we
  425. * require our caller to have precreated a latent 'incoming' channel via
  426. * TSERVER which then gets turned into the new connection.
  427. *
  428. * TODO(QUIC SERVER): In the future we will construct channels dynamically
  429. * in this case.
  430. */
  431. if (port->tserver_ch == NULL)
  432. goto undesirable;
  433. /*
  434. * We have got a packet for an unknown DCID. This might be an attempt to
  435. * open a new connection.
  436. */
  437. if (e->data_len < QUIC_MIN_INITIAL_DGRAM_LEN)
  438. goto undesirable;
  439. if (!PACKET_buf_init(&pkt, ossl_quic_urxe_data(e), e->data_len))
  440. goto undesirable;
  441. /*
  442. * We set short_conn_id_len to SIZE_MAX here which will cause the decode
  443. * operation to fail if we get a 1-RTT packet. This is fine since we only
  444. * care about Initial packets.
  445. */
  446. if (!ossl_quic_wire_decode_pkt_hdr(&pkt, SIZE_MAX, 1, 0, &hdr, NULL))
  447. goto undesirable;
  448. switch (hdr.version) {
  449. case QUIC_VERSION_1:
  450. break;
  451. case QUIC_VERSION_NONE:
  452. default:
  453. /* Unknown version or proactive version negotiation request, bail. */
  454. /* TODO(QUIC SERVER): Handle version negotiation on server side */
  455. goto undesirable;
  456. }
  457. /*
  458. * We only care about Initial packets which might be trying to establish a
  459. * connection.
  460. */
  461. if (hdr.type != QUIC_PKT_TYPE_INITIAL)
  462. goto undesirable;
  463. /*
  464. * Try to process this as a valid attempt to initiate a connection.
  465. *
  466. * The channel will do all the LCID registration needed, but as an
  467. * optimization inject this packet directly into the channel's QRX for
  468. * processing without going through the DEMUX again.
  469. */
  470. port_on_new_conn(port, &e->peer, &hdr.src_conn_id, &hdr.dst_conn_id,
  471. &new_ch);
  472. if (new_ch != NULL)
  473. ossl_qrx_inject_urxe(new_ch->qrx, e);
  474. return;
  475. undesirable:
  476. ossl_quic_demux_release_urxe(port->demux, e);
  477. }
  478. void ossl_quic_port_raise_net_error(QUIC_PORT *port,
  479. QUIC_CHANNEL *triggering_ch)
  480. {
  481. QUIC_CHANNEL *ch;
  482. if (!ossl_quic_port_is_running(port))
  483. return;
  484. /*
  485. * Immediately capture any triggering error on the error stack, with a
  486. * cover error.
  487. */
  488. ERR_raise_data(ERR_LIB_SSL, SSL_R_QUIC_NETWORK_ERROR,
  489. "port failed due to network BIO I/O error");
  490. OSSL_ERR_STATE_save(port->err_state);
  491. port_transition_failed(port);
  492. /* Give the triggering channel (if any) the first notification. */
  493. if (triggering_ch != NULL)
  494. ossl_quic_channel_raise_net_error(triggering_ch);
  495. LIST_FOREACH(ch, ch, &port->channel_list)
  496. if (ch != triggering_ch)
  497. ossl_quic_channel_raise_net_error(ch);
  498. }
  499. void ossl_quic_port_restore_err_state(const QUIC_PORT *port)
  500. {
  501. ERR_clear_error();
  502. OSSL_ERR_STATE_restore(port->err_state);
  503. }