2 * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/quic_port.h"
11 #include "internal/quic_channel.h"
12 #include "internal/quic_lcidm.h"
13 #include "internal/quic_srtm.h"
14 #include "quic_port_local.h"
15 #include "quic_channel_local.h"
16 #include "../ssl_local.h"
22 #define INIT_DCID_LEN 8
24 static int port_init(QUIC_PORT
*port
);
25 static void port_cleanup(QUIC_PORT
*port
);
26 static OSSL_TIME
get_time(void *arg
);
27 static void port_tick(QUIC_TICK_RESULT
*res
, void *arg
, uint32_t flags
);
28 static void port_default_packet_handler(QUIC_URXE
*e
, void *arg
,
29 const QUIC_CONN_ID
*dcid
);
30 static void port_rx_pre(QUIC_PORT
*port
);
32 DEFINE_LIST_OF_IMPL(ch
, QUIC_CHANNEL
);
34 QUIC_PORT
*ossl_quic_port_new(const QUIC_PORT_ARGS
*args
)
38 if ((port
= OPENSSL_zalloc(sizeof(QUIC_PORT
))) == NULL
)
41 port
->libctx
= args
->libctx
;
42 port
->propq
= args
->propq
;
43 port
->mutex
= args
->mutex
;
44 port
->now_cb
= args
->now_cb
;
45 port
->now_cb_arg
= args
->now_cb_arg
;
46 port
->channel_ctx
= args
->channel_ctx
;
47 port
->is_multi_conn
= args
->is_multi_conn
;
49 if (!port_init(port
)) {
57 void ossl_quic_port_free(QUIC_PORT
*port
)
66 static int port_init(QUIC_PORT
*port
)
68 size_t rx_short_dcid_len
= (port
->is_multi_conn
? INIT_DCID_LEN
: 0);
70 if (port
->channel_ctx
== NULL
)
73 if ((port
->demux
= ossl_quic_demux_new(/*BIO=*/NULL
,
74 /*Short CID Len=*/rx_short_dcid_len
,
75 get_time
, port
)) == NULL
)
79 * If we are a server, setup our handler for packets not corresponding to
80 * any known DCID on our end. This is for handling clients establishing new
84 ossl_quic_demux_set_default_handler(port
->demux
,
85 port_default_packet_handler
,
88 if ((port
->srtm
= ossl_quic_srtm_new(port
->libctx
, port
->propq
)) == NULL
)
91 if ((port
->lcidm
= ossl_quic_lcidm_new(port
->libctx
, rx_short_dcid_len
)) == NULL
)
94 ossl_quic_reactor_init(&port
->rtor
, port_tick
, port
, ossl_time_zero());
95 port
->rx_short_dcid_len
= (unsigned char)rx_short_dcid_len
;
96 port
->tx_init_dcid_len
= INIT_DCID_LEN
;
104 static void port_cleanup(QUIC_PORT
*port
)
106 assert(ossl_list_ch_num(&port
->channel_list
) == 0);
108 ossl_quic_demux_free(port
->demux
);
111 ossl_quic_srtm_free(port
->srtm
);
114 ossl_quic_lcidm_free(port
->lcidm
);
118 QUIC_REACTOR
*ossl_quic_port_get0_reactor(QUIC_PORT
*port
)
123 QUIC_DEMUX
*ossl_quic_port_get0_demux(QUIC_PORT
*port
)
128 CRYPTO_MUTEX
*ossl_quic_port_get0_mutex(QUIC_PORT
*port
)
133 OSSL_TIME
ossl_quic_port_get_time(QUIC_PORT
*port
)
135 if (port
->now_cb
== NULL
)
136 return ossl_time_now();
138 return port
->now_cb(port
->now_cb_arg
);
141 static OSSL_TIME
get_time(void *port
)
143 return ossl_quic_port_get_time(port
);
146 int ossl_quic_port_get_rx_short_dcid_len(const QUIC_PORT
*port
)
148 return port
->rx_short_dcid_len
;
151 int ossl_quic_port_get_tx_init_dcid_len(const QUIC_PORT
*port
)
153 return port
->tx_init_dcid_len
;
157 * QUIC Port: Network BIO Configuration
158 * ====================================
161 /* Determines whether we can support a given poll descriptor. */
162 static int validate_poll_descriptor(const BIO_POLL_DESCRIPTOR
*d
)
164 if (d
->type
== BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD
&& d
->value
.fd
< 0) {
165 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_INVALID_ARGUMENT
);
172 BIO
*ossl_quic_port_get_net_rbio(QUIC_PORT
*port
)
174 return port
->net_rbio
;
177 BIO
*ossl_quic_port_get_net_wbio(QUIC_PORT
*port
)
179 return port
->net_wbio
;
182 static int port_update_poll_desc(QUIC_PORT
*port
, BIO
*net_bio
, int for_write
)
184 BIO_POLL_DESCRIPTOR d
= {0};
187 || (!for_write
&& !BIO_get_rpoll_descriptor(net_bio
, &d
))
188 || (for_write
&& !BIO_get_wpoll_descriptor(net_bio
, &d
)))
189 /* Non-pollable BIO */
190 d
.type
= BIO_POLL_DESCRIPTOR_TYPE_NONE
;
192 if (!validate_poll_descriptor(&d
))
196 ossl_quic_reactor_set_poll_w(&port
->rtor
, &d
);
198 ossl_quic_reactor_set_poll_r(&port
->rtor
, &d
);
203 int ossl_quic_port_update_poll_descriptors(QUIC_PORT
*port
)
207 if (!port_update_poll_desc(port
, port
->net_rbio
, /*for_write=*/0))
210 if (!port_update_poll_desc(port
, port
->net_wbio
, /*for_write=*/1))
217 * QUIC_PORT does not ref any BIO it is provided with, nor is any ref
218 * transferred to it. The caller (e.g., QUIC_CONNECTION) is responsible for
219 * ensuring the BIO lasts until the channel is freed or the BIO is switched out
220 * for another BIO by a subsequent successful call to this function.
222 int ossl_quic_port_set_net_rbio(QUIC_PORT
*port
, BIO
*net_rbio
)
224 if (port
->net_rbio
== net_rbio
)
227 if (!port_update_poll_desc(port
, net_rbio
, /*for_write=*/0))
230 ossl_quic_demux_set_bio(port
->demux
, net_rbio
);
231 port
->net_rbio
= net_rbio
;
235 int ossl_quic_port_set_net_wbio(QUIC_PORT
*port
, BIO
*net_wbio
)
239 if (port
->net_wbio
== net_wbio
)
242 if (!port_update_poll_desc(port
, net_wbio
, /*for_write=*/1))
245 LIST_FOREACH(ch
, ch
, &port
->channel_list
)
246 ossl_qtx_set_bio(ch
->qtx
, net_wbio
);
248 port
->net_wbio
= net_wbio
;
253 * QUIC Port: Channel Lifecycle
254 * ============================
257 static SSL
*port_new_handshake_layer(QUIC_PORT
*port
)
260 SSL_CONNECTION
*tls_conn
= NULL
;
262 tls
= ossl_ssl_connection_new_int(port
->channel_ctx
, TLS_method());
263 if (tls
== NULL
|| (tls_conn
= SSL_CONNECTION_FROM_SSL(tls
)) == NULL
)
266 /* Override the user_ssl of the inner connection. */
267 tls_conn
->s3
.flags
|= TLS1_FLAGS_QUIC
;
269 /* Restrict options derived from the SSL_CTX. */
270 tls_conn
->options
&= OSSL_QUIC_PERMITTED_OPTIONS_CONN
;
271 tls_conn
->pha_enabled
= 0;
275 static QUIC_CHANNEL
*port_make_channel(QUIC_PORT
*port
, SSL
*tls
, int is_server
)
277 QUIC_CHANNEL_ARGS args
= {0};
281 args
.is_server
= is_server
;
282 args
.tls
= (tls
!= NULL
? tls
: port_new_handshake_layer(port
));
283 if (args
.tls
== NULL
)
286 ch
= ossl_quic_channel_new(&args
);
297 QUIC_CHANNEL
*ossl_quic_port_create_outgoing(QUIC_PORT
*port
, SSL
*tls
)
299 return port_make_channel(port
, tls
, /*is_server=*/0);
302 QUIC_CHANNEL
*ossl_quic_port_create_incoming(QUIC_PORT
*port
, SSL
*tls
)
306 assert(port
->tserver_ch
== NULL
);
308 ch
= port_make_channel(port
, tls
, /*is_server=*/1);
309 port
->tserver_ch
= ch
;
314 * QUIC Port: Ticker-Mutator
315 * =========================
319 * The central ticker function called by the reactor. This does everything, or
320 * at least everything network I/O related. Best effort - not allowed to fail
323 static void port_tick(QUIC_TICK_RESULT
*res
, void *arg
, uint32_t flags
)
325 QUIC_PORT
*port
= arg
;
328 res
->net_read_desired
= 0;
329 res
->net_write_desired
= 0;
330 res
->tick_deadline
= ossl_time_infinite();
332 if (!port
->inhibit_tick
) {
333 /* Handle any incoming data from network. */
336 /* Iterate through all channels and service them. */
337 LIST_FOREACH(ch
, ch
, &port
->channel_list
) {
338 QUIC_TICK_RESULT subr
= {0};
340 ossl_quic_channel_subtick(ch
, &subr
, flags
);
341 ossl_quic_tick_result_merge_into(res
, &subr
);
346 /* Process incoming datagrams, if any. */
347 static void port_rx_pre(QUIC_PORT
*port
)
351 // TODO !have_sent_any_pkt
354 * Get DEMUX to BIO_recvmmsg from the network and queue incoming datagrams
355 * to the appropriate QRX instances.
357 ret
= ossl_quic_demux_pump(port
->demux
);
358 // TODO: handle ret, stateless reset
360 if (ret
== QUIC_DEMUX_PUMP_RES_PERMANENT_FAIL
)
362 * We don't care about transient failure, but permanent failure means we
363 * should tear down the port. All connections skip straight to the
364 * Terminated state as there is no point trying to send CONNECTION_CLOSE
365 * frames if the network BIO is not operating correctly.
367 ossl_quic_port_raise_net_error(port
);
371 * Handles an incoming connection request and potentially decides to make a
372 * connection from it. If a new connection is made, the new channel is written
375 static void port_on_new_conn(QUIC_PORT
*port
, const BIO_ADDR
*peer
,
376 const QUIC_CONN_ID
*scid
,
377 const QUIC_CONN_ID
*dcid
,
378 QUIC_CHANNEL
**new_ch
)
380 if (port
->tserver_ch
!= NULL
) {
381 /* Specially assign to existing channel */
382 if (!ossl_quic_channel_on_new_conn(port
->tserver_ch
, peer
, scid
, dcid
))
385 *new_ch
= port
->tserver_ch
;
386 port
->tserver_ch
= NULL
;
391 static int port_try_handle_stateless_reset(QUIC_PORT
*port
, const QUIC_URXE
*e
)
394 const unsigned char *data
= ossl_quic_urxe_data(e
);
398 * Perform some fast and cheap checks for a packet not being a stateless
399 * reset token. RFC 9000 s. 10.3 specifies this layout for stateless
403 * Fixed Bits (2) = 1,
404 * Unpredictable Bits (38..),
405 * Stateless Reset Token (128),
409 * However, endpoints MUST treat any packet ending in a valid
410 * stateless reset token as a Stateless Reset, as other QUIC
411 * versions might allow the use of a long header.
413 * We can rapidly check for the minimum length and that the first pair
414 * of bits in the first byte are 01 or 11.
416 * The function returns 1 if it is a stateless reset packet, 0 if it isn't
417 * and -1 if an error was encountered.
419 if (e
->data_len
< QUIC_STATELESS_RESET_TOKEN_LEN
+ 5
420 || (0100 & *data
) != 0100)
424 if (!ossl_quic_srtm_lookup(port
->srtm
,
425 (QUIC_STATELESS_RESET_TOKEN
*)(data
+ e
->data_len
426 - sizeof(QUIC_STATELESS_RESET_TOKEN
)),
430 assert(opaque
!= NULL
);
431 ossl_quic_channel_on_stateless_reset((QUIC_CHANNEL
*)opaque
);
438 * This is called by the demux when we get a packet not destined for any known
441 static void port_default_packet_handler(QUIC_URXE
*e
, void *arg
,
442 const QUIC_CONN_ID
*dcid
)
444 QUIC_PORT
*port
= arg
;
447 QUIC_CHANNEL
*ch
= NULL
, *new_ch
= NULL
;
450 && ossl_quic_lcidm_lookup(port
->lcidm
, dcid
, NULL
,
453 ossl_quic_channel_inject(ch
, e
);
457 if (port_try_handle_stateless_reset(port
, e
))
461 if (port
->tserver_ch
== NULL
)
464 // TODO allow_incoming
465 //if (!ossl_assert(ch->is_server))
468 //TODO if (ch->state != QUIC_CHANNEL_STATE_IDLE)
472 * We have got a packet for an unknown DCID. This might be an attempt to
473 * open a new connection.
475 if (e
->data_len
< QUIC_MIN_INITIAL_DGRAM_LEN
)
478 if (!PACKET_buf_init(&pkt
, ossl_quic_urxe_data(e
), e
->data_len
))
482 * We set short_conn_id_len to SIZE_MAX here which will cause the decode
483 * operation to fail if we get a 1-RTT packet. This is fine since we only
484 * care about Initial packets.
486 if (!ossl_quic_wire_decode_pkt_hdr(&pkt
, SIZE_MAX
, 1, 0, &hdr
, NULL
))
489 switch (hdr
.version
) {
493 case QUIC_VERSION_NONE
:
495 /* Unknown version or proactive version negotiation request, bail. */
496 /* TODO(QUIC SERVER): Handle version negotiation on server side */
501 * We only care about Initial packets which might be trying to establish a
504 if (hdr
.type
!= QUIC_PKT_TYPE_INITIAL
)
508 * Try to process this as a valid attempt to initiate a connection.
510 * We do not register the DCID in the Initial packet we received as
511 * that DCID is not actually used again, thus after provisioning
512 * the new connection and associated Initial keys, we inject the
513 * received packet directly to the new channel's QRX so that it can
514 * process it as a one-time thing, instead of going through the usual
515 * DEMUX DCID-based routing.
517 port_on_new_conn(port
, &e
->peer
, &hdr
.src_conn_id
, &hdr
.dst_conn_id
,
520 ossl_qrx_inject_urxe(new_ch
->qrx
, e
);
525 ossl_quic_demux_release_urxe(port
->demux
, e
);
528 void ossl_quic_port_set_inhibit_tick(QUIC_PORT
*port
, int inhibit
)
530 port
->inhibit_tick
= (inhibit
!= 0);
533 void ossl_quic_port_raise_net_error(QUIC_PORT
*port
)
539 LIST_FOREACH(ch
, ch
, &port
->channel_list
)
540 ossl_quic_channel_raise_net_error(ch
);