summaryrefslogtreecommitdiffhomepage
path: root/src/event/quic/ngx_event_quic.c (follow)
AgeCommit message (Collapse)AuthorFilesLines
2025-06-21Use NULL instead of 0 for null pointer constant.HEADmasterAndrew Clayton1-1/+1
There were a few random places where 0 was being used as a null pointer constant. We have a NULL macro for this very purpose, use it. There is also some interest in actually deprecating the use of 0 as a null pointer constant in C. This was found with -Wzero-as-null-pointer-constant which was enabled for C in GCC 15 (not enabled with Wall or Wextra... yet). Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117059>
2025-05-23QUIC: ssl_encryption_level_t abstraction layer.Sergey Kandaurov1-17/+19
Encryption level values are decoupled from ssl_encryption_level_t, which is now limited to BoringSSL QUIC callbacks, with mappings provided. Although the values match, this provides a technically safe approach, in particular, to access protection level sized arrays. In preparation for using OpenSSL 3.5 TLS callbacks.
2025-05-23QUIC: defined SSL API macros in a single place.Sergey Kandaurov1-2/+2
All definitions now set in ngx_event_quic.h, this includes moving NGX_QUIC_OPENSSL_COMPAT from autotests to compile time. Further, to improve code readability, a new NGX_QUIC_QUICTLS_API macro is used for QuicTLS that provides old BoringSSL QUIC API.
2025-05-23QUIC: reset qc->error to zero again.Sergey Kandaurov1-3/+3
Following the previous change that removed posting a close event in OpenSSL compat layer, now ngx_quic_close_connection() is always called on error path with either NGX_ERROR or qc->error set. This allows to remove a special value -1 served as a missing error, which simplifies the code. Partially reverts d3fb12d77. Also, this improves handling of the draining connection state, which consists of posting a close event with NGX_OK and no qc->error set, where it was previously converted to NGX_QUIC_ERR_INTERNAL_ERROR. Notably, this is rather a cosmetic fix, because drained connections do not send any packets including CONNECTION_CLOSE, and qc->error is not otherwise used.
2025-05-23QUIC: adjusted handling of callback errors.Sergey Kandaurov1-0/+3
Changed handshake callbacks to always return success. This allows to avoid logging SSL_do_handshake() errors with empty or cryptic "internal error" OpenSSL error messages at the inappropriate "crit" log level. Further, connections with failed callbacks are closed now right away when using OpenSSL compat layer. This change supersedes and reverts c37fdcdd1, with the conditions to check callbacks invocation kept to slightly improve code readability of control flow; they are optimized out in the resulting assembly code.
2025-04-15QUIC: optimized connection frame threshold.Roman Arutyunyan1-0/+4
Previosly the threshold was hardcoded at 10000. This value is too low for high BDP networks. For example, if all frames are STREAM frames, and MTU is 1500, the upper limit for congestion window would be roughly 15M (10000 * 1500). With 100ms RTT it's just a 1.2Gbps network (15M * 10 * 8). In reality, the limit is even lower because of other frame types. Also, the number of frames that could be used simultaneously depends on the total amount of data buffered in all server streams, and client flow control. The change sets frame threshold based on max concurrent streams and stream buffer size, the product of which is the maximum number of in-flight stream data in all server streams at any moment. The value is divided by 2000 to account for a typical MTU 1500 and the fact that not all frames are STREAM frames.
2025-04-15QUIC: CUBIC congestion control.Roman Arutyunyan1-0/+1
2025-04-15QUIC: prevent spurious congestion control recovery mode.Roman Arutyunyan1-1/+1
Since recovery_start field was initialized with ngx_current_msec, all congestion events that happened within the same millisecond or cycle iteration, were treated as in recovery mode. Also, when handling persistent congestion, initializing recovery_start with ngx_current_msec resulted in treating all sent packets as in recovery mode, which violates RFC 9002, see example in Appendix B.8. While here, also fixed recovery_start wrap protection. Previously it used 2 * max_idle_timeout time frame for all sent frames, which is not a reliable protection since max_idle_timeout is unrelated to congestion control. Now recovery_start <= now condition is enforced. Note that recovery_start wrap is highly unlikely and can only occur on a 32-bit system if there are no congestion events for 24 days.
2025-04-15QUIC: use path MTU in congestion window computations.Roman Arutyunyan1-2/+2
As per RFC 9002, Section B.2, max_datagram_size used in congestion window computations should be based on path MTU.
2024-12-10QUIC: fixed client request timeout in 0-RTT scenarios.nandsky1-1/+1
Since 0-RTT and 1-RTT data exist in the same packet number space, ngx_quic_discard_ctx incorrectly discards 1-RTT packets when 0-RTT keys are discarded. The issue was introduced by 58b92177e7c3c50f77f807ab3846ad5c7bbf0ebe.
2024-08-09QUIC: discarding 0-RTT keys.Sergey Kandaurov1-0/+10
For simplicity, this is done on successful decryption of a 1-RTT packet.
2024-04-10QUIC: fixed close timer processing with early data.Vladimir Khomutov1-1/+4
The ngx_quic_run() function uses qc->close timer to limit the handshake duration. Normally it is removed by ngx_quic_do_init_streams() which is called once when we are done with initial SSL processing. The problem happens when the client sends early data and streams are initialized in the ngx_quic_run() -> ngx_quic_handle_datagram() call. The order of set/remove timer calls is now reversed; the close timer is set up and the timer fires when assigned, starting the unexpected connection close process. The fix is to skip setting the timer if streams were initialized during handling of the initial datagram. The idle timer for quic is set anyway, and stream-related timeouts are managed by application layer.
2023-12-12QUIC: reset RTT estimator for the new path.Sergey Kandaurov1-8/+1
RTT is a property of the path, it must be reset on confirming a peer's ownership of its new address.
2023-10-20QUIC: reusing crypto contexts for packet protection.Sergey Kandaurov1-0/+3
2023-08-31QUIC: split keys availability checks to read and write sides.Sergey Kandaurov1-3/+5
Keys may be released by TLS stack in different times, so it makes sense to check this independently as well. This allows to fine-tune what key direction is used when checking keys availability. When discarding, server keys are now marked in addition to client keys.
2023-09-14QUIC: simplified setting close timer when closing connection.Roman Arutyunyan1-1/+1
Previously, the timer was never reset due to an explicit check. The check was added in 36b59521a41c as part of connection close simplification. The reason was to retain the earliest timeout. However, the timeouts are all the same while QUIC handshake is in progress and resetting the timer for the same value has no performance implications. After handshake completion there's only application level. The change removes the check.
2023-09-21QUIC: do not call shutdown() when handshake is in progress.Roman Arutyunyan1-1/+1
Instead, when worker is shutting down and handshake is not yet completed, connection is terminated immediately. Previously the callback could be called while QUIC handshake was in progress and, what's more important, before the init() callback. Now it's postponed after init(). This change is a preparation to postponing HTTP/3 session creation to init().
2023-09-13QUIC: "handshake_timeout" configuration parameter.Roman Arutyunyan1-0/+6
Previously QUIC did not have such parameter and handshake duration was controlled by HTTP/3. However that required creating and storing HTTP/3 session on first client datagram. Apparently there's no convenient way to store the session object until QUIC handshake is complete. In the followup patches session creation will be postponed to init() callback.
2023-09-01QUIC: removed use of SSL_quic_read_level and SSL_quic_write_level.Sergey Kandaurov1-4/+1
As explained in BoringSSL change[1], levels were introduced in the original QUIC API to draw a line between when keys are released and when are active. In the new QUIC API they are released in separate calls when it's needed. BoringSSL has then a consideration to remove levels API, hence the change. If not available e.g. from a QUIC packet header, levels can be taken based on keys availability. The only real use of levels is to prevent using app keys before they are active in QuicTLS that provides the old BoringSSL QUIC API, it is replaced with an equivalent check of c->ssl->handshaked. This change also removes OpenSSL compat shims since they are no longer used. The only exception left is caching write level from the keylog callback in the internal field which is a handy equivalent of checking keys availability. [1] https://boringssl.googlesource.com/boringssl/+/1e859054
2023-09-01QUIC: refined sending CONNECTION_CLOSE in various packet types.Sergey Kandaurov1-11/+10
As per RFC 9000, section 10.2.3, to ensure that peer successfully removed packet protection, CONNECTION_CLOSE can be sent in multiple packets using different packet protection levels. Now it is sent in all protection levels available. This roughly corresponds to the following paragraph: * Prior to confirming the handshake, a peer might be unable to process 1-RTT packets, so an endpoint SHOULD send a CONNECTION_CLOSE frame in both Handshake and 1-RTT packets. A server SHOULD also send a CONNECTION_CLOSE frame in an Initial packet. In practice, this change allows to avoid sending an Initial packet when we know the client has handshake keys, by checking if we have discarded initial keys. Also, this fixes sending CONNECTION_CLOSE when using QuicTLS with old QUIC API, where TLS stack releases application read keys before handshake confirmation; it is fixed by sending CONNECTION_CLOSE additionally in a Handshake packet.
2023-08-30QUIC: use last client dcid to receive initial packets.Roman Arutyunyan1-1/+1
Previously, original dcid was used to receive initial client packets in case server initial response was lost. However, last dcid should be used instead. These two are the same unless retry is used. In case of retry, client resends initial packet with a new dcid, that is different from the original dcid. If server response is lost, the client resends this packet again with the same dcid. This is shown in RFC 9000, 7.3. Authenticating Connection IDs, Figure 8. The issue manifested itself with creating multiple server sessions in response to each post-retry client initial packet, if server response is lost.
2023-08-25QUIC: posted generating TLS Key Update next keys.Sergey Kandaurov1-1/+11
Since at least f9fbeb4ee0de and certainly after 924882f42dea, which TLS Key Update support predates, queued data output is deferred to a posted push handler. To address timing signals after these changes, generating next keys is now posted to run after the push handler.
2023-08-14QUIC: path MTU discovery.Roman Arutyunyan1-7/+2
MTU selection starts by doubling the initial MTU until the first failure. Then binary search is used to find the path MTU.
2023-07-06QUIC: removed path->limited flag.Roman Arutyunyan1-1/+0
Its value is the opposite of path->validated.
2023-05-22QUIC: fixed post-close use-after-free.Roman Arutyunyan1-2/+2
Previously, ngx_quic_close_connection() could be called in a way that QUIC connection was accessed after the call. In most cases the connection is not closed right away, but close timeout is scheduled. However, it's not always the case. Also, if the close process started earlier for a different reason, calling ngx_quic_close_connection() may actually close the connection. The connection object should not be accessed after that. Now, when possible, return statement is added to eliminate post-close connection object access. In other places ngx_quic_close_connection() is substituted with posting close event. Also, the new way of closing connection in ngx_quic_stream_cleanup_handler() fixes another problem in this function. Previously it passed stream connection instead of QUIC connection to ngx_quic_close_connection(). This could result in incomplete connection shutdown. One consequence of that could be that QUIC streams were freed without shutting down their application contexts. This could result in another use-after-free. Found by Coverity (CID 1530402).
2023-05-02QUIC: optimized immediate close.Roman Arutyunyan1-0/+1
Previously, before sending CONNECTION_CLOSE to client, all pending frames were sent. This is redundant and could prevent CONNECTION_CLOSE from being sent due to congestion control. Now pending frames are freed and CONNECTION_CLOSE is sent without congestion control, as advised by RFC 9002: Packets containing frames besides ACK or CONNECTION_CLOSE frames count toward congestion control limits and are considered to be in flight.
2022-11-30QUIC: removed cancelable flag from QUIC and HTTP/3 events.Roman Arutyunyan1-4/+0
All these events are created in context of a client connection and are deleted when the connection is closed. Setting ev->cancelable could trigger premature connection closure and a socket leak alert.
2022-10-19QUIC: idle mode for main connection.Roman Arutyunyan1-3/+14
Now main QUIC connection for HTTP/3 always has c->idle flag set. This allows the connection to receive worker shutdown notification. It is passed to application level via a new conf->shutdown() callback. The HTTP/3 shutdown callback sends GOAWAY to client and gracefully shuts down the QUIC connection.
2022-11-29QUIC: reusable mode for main connection.Roman Arutyunyan1-35/+14
The connection is automatically switched to this mode by transport layer when there are no non-cancelable streams. Currently, cancelable streams are HTTP/3 encoder/decoder/control streams.
2022-09-07QUIC: post close event for connection close.Roman Arutyunyan1-21/+24
Previously, close event was used only for close timeout, while read event was used for posting connection close.
2022-08-22QUIC: made ngx_quic_finalize_connecion() more graceful.Roman Arutyunyan1-18/+14
Previously, ngx_quic_finalize_connection() closed the connection with NGX_ERROR code, which resulted in immediate connection closure. Now the code is NGX_OK, which provides a more graceful shutdown with a timeout.
2022-09-07QUIC: treat qc->error == -1 as a missing error.Roman Arutyunyan1-3/+3
Previously, zero was used for this purpose. However, NGX_QUIC_ERR_NO_ERROR is zero too. As a result, NGX_QUIC_ERR_NO_ERROR was changed to NGX_QUIC_ERR_INTERNAL_ERROR when closing a QUIC connection.
2022-07-27QUIC: removed ngx_quic_keys_new().Vladimir Homutov1-1/+1
The ngx_quic_keys_t structure is now exposed.
2022-07-27QUIC: avoided pool usage in ngx_quic_protection.c.Vladimir Homutov1-1/+1
2022-04-20QUIC: separate UDP framework for QUIC.Roman Arutyunyan1-3/+5
Previously, QUIC used the existing UDP framework, which was created for UDP in Stream. However the way QUIC connections are created and looked up is different from the way UDP connections in Stream are created and looked up. Now these two implementations are decoupled.
2022-02-16QUIC: fixed indentation.Sergey Kandaurov1-4/+4
2022-02-14QUIC: ngx_quic_buffer_t object.Roman Arutyunyan1-1/+1
The object is used instead of ngx_chain_t pointer for buffer operations like ngx_quic_write_chain() and ngx_quic_read_chain(). These functions are renamed to ngx_quic_write_buffer() and ngx_quic_read_buffer().
2022-02-05QUIC: stream lingering.Roman Arutyunyan1-0/+1
Now ngx_quic_stream_t is decoupled from ngx_connection_t in a way that it can persist after connection is closed by application. During this period, server is expecting stream final size from client for correct flow control. Also, buffered output is sent to client as more flow control credit is granted.
2022-02-09QUIC: fixed in-flight bytes accounting.Vladimir Homutov1-1/+0
Initially, frames are genereated and stored in ctx->frames. Next, ngx_quic_output() collects frames to be sent in in ctx->sending. On failure, ngx_quic_revert_sned() returns frames into ctx->frames. On success, the ngx_quic_commit_send() moves ack-eliciting frames into ctx->sent and frees non-ack-eliciting frames. This function also updates in-flight bytes counter, so only actually sent frames are accounted. The counter is decremented in the following cases: - acknowledgment is received - packet was declared lost - we are discarding context completely In each of this cases frame is removed from ctx->sent queue and in-flight counter is accordingly decremented. The patch fixes the case of discarding context - only removing frames from ctx->sent must be followed by in-flight bytes counter decrement, otherwise cg->in_flight could experience type underflow. The issue appeared in b1676cd64dc9.
2022-02-01QUIC: improved debug logging.Vladimir Homutov1-21/+22
- wording in log->action is adjusted to match function names. - connection close steps are made obvious and start with "quic close" prefix: *1 quic close initiated rc:-4 *1 quic close silent drain:0 timedout:1 *1 quic close resumed rc:-1 *1 quic close resumed rc:-1 *1 quic close resumed rc:-4 *1 quic close completed this makes it easy to understand if particular "close" record is an initial cause or lasting process, or the final one. - cases of close without quic connection now logged as "packet rejected": *14 quic run *14 quic packet rx long flags:ec version:1 *14 quic packet rx hs len:61 *14 quic packet rx dcid len:20 00000000000002c32f60e4aa2b90a64a39dc4228 *14 quic packet rx scid len:8 81190308612cd019 *14 quic expected initial, got handshake *14 quic packet done rc:-1 level:hs decr:0 pn:0 perr:0 *14 quic packet rejected rc:-1, cleanup connection *14 reusable connection: 0 this makes it easy to spot early packet rejection and avoid confuse with quic connection closing (which in fact was not even created). - packet processing summary now uses same prefix "quic packet done rc:" - added debug to places where packet was rejected without any reason logged
2022-02-01QUIC: dead code removed.Vladimir Homutov1-2/+2
The ngx_quic_parse_packet() now returns NGX_OK, NGX_ERROR (parsing failed) and NGX_ABORT (unsupported version).
2022-02-01QUIC: merged ngx_quic_close_quic() and ngx_quic_close_connection().Vladimir Homutov1-37/+26
The separate ngx_quic_close_quic() doesn't make much sense.
2022-02-01QUIC: revised ngx_quic_handle_datagram() error codes.Vladimir Homutov1-15/+6
The NGX_DECLINED is replaced with NGX_DONE to match closer to return code of ngx_quic_handle_packet() and ngx_quic_close_connection() rc argument. The ngx_quic_close_connection() rc code is used only when quic connection exists, thus anything goes if qc == NULL. The ngx_quic_handle_datagram() does not return NG_OK in cases when quic connection is not yet created.
2022-01-26QUIC: style.Roman Arutyunyan1-2/+0
2022-01-26QUIC: removed draft versions support.Sergey Kandaurov1-2/+1
2022-01-26HTTP/3: removed draft versions support in ALPN.Sergey Kandaurov1-12/+0
2022-01-21QUIC: changed debug message.Roman Arutyunyan1-1/+1
2022-01-19QUIC: reworked migration handling.Vladimir Homutov1-32/+34
The quic connection now holds active, backup and probe paths instead of sockets. The number of migration paths is now limited and cannot be inflated by a bad client or an attacker. The client id is now associated with path rather than socket. This allows to simplify processing of output and connection ids handling. New migration abandons any previously started migrations. This allows to free consumed client ids and request new for use in future migrations and make progress in case when connection id limit is hit during migration. A path now can be revalidated without losing its state. The patch also fixes various issues with NAT rebinding case handling: - paths are now validated (previously, there was no validation and paths were left in limited state) - attempt to reuse id on different path is now again verified (this was broken in 40445fc7c403) - former path is now validated in case of apparent migration
2021-12-27QUIC: renamed input handling functions.Roman Arutyunyan1-14/+15
Now these functions have names ngx_quic_handle_XXX(): - ngx_quic_process_stateless_reset() -> ngx_quic_handle_stateless_reset() - ngx_quic_input() -> ngx_quic_handle_datagram() - ngx_quic_process_packet() -> ngx_quic_handle_packet() - ngx_quic_process_payload() -> ngx_quic_handle_payload()
2021-12-16QUIC: renamed buffer-related functions.Roman Arutyunyan1-1/+1
ngx_quic_alloc_buf() -> ngx_quic_alloc_chain(), ngx_quic_free_bufs() -> ngx_quic_free_chain(), ngx_quic_trim_bufs() -> ngx_quic_trim_chain()