Commit Graph

7136 Commits

Author SHA1 Message Date
Vladimir Homutov
99d4f2399d QUIC: packet processing refactoring.
All packet header parsing is now performed by ngx_quic_parse_packet()
function, located in the ngx_quic_transport.c file.

The packet processing is centralized in the ngx_quic_process_packet()
function which decides if the packet should be accepted, ignored or
connection should be closed, depending on the connection state.

As a result of refactoring, behavior has changed in some places:

 - minimal size of Initial packet is now always tested
 - connection IDs are always tested in existing connections
 - old keys are discarded on encryption level switch
2020-09-30 15:14:09 +03:00
Vladimir Homutov
fe626bda84 QUIC: simplified packet header parsing.
Now flags are processed in ngx_quic_input(), and raw->pos points to the first
byte after the flags. Redundant checks from ngx_quic_parse_short_header() and
ngx_quic_parse_long_header() are removed.
2020-09-25 21:47:28 +03:00
Roman Arutyunyan
80958b29a2 QUIC: keep the entire packet size in pkt->len.
Previously pkt->len kept the length of the packet remainder starting from
pkt->raw->pos.
2020-09-25 21:46:55 +03:00
Vladimir Homutov
6c0be4b4cf QUIC: switched to using fixed-length server connection IDs. 2020-09-18 15:53:37 +03:00
Roman Arutyunyan
469f69bf2c QUIC: resend frames by moving them to output queue.
Previously, when a packet was declared lost, another packet was sent with the
same frames.  Now lost frames are moved to the output frame queue and push
event is posted.  This has the advantage of forming packets with more frames
than before.

Also, the start argument is removed from the ngx_quic_resend_frames()
function as excess information.
2020-09-30 20:23:16 +01:00
Roman Arutyunyan
1f90fccd97 QUIC: switch stream context to a server selected by SNI.
Previously the default server configuration context was used until the
:authority or host header was parsed.  This led to using the configuration
parameters like client_header_buffer_size or request_pool_size from the default
server rather than from the server selected by SNI.

Also, the switch to the right server log is implemented.  This issue manifested
itself as QUIC stream being logged to the default server log until :authority
or host is parsed.
2020-09-29 22:09:09 +01:00
Sergey Kandaurov
0824d61fc9 QUIC: unbreak client certificate verification after 0d2b2664b41c.
Initially, client certificate verification didn't work due to the missing
hc->ssl on a QUIC stream, which is started to be set in 7738:7f0981be07c4.
Then it was lost in 7999:0d2b2664b41c introducing "quic" listen parameter.

This change re-adds hc->ssl back for all QUIC connections, similar to SSL.
2020-09-23 13:13:04 +01:00
Vladimir Homutov
766fc16f55 QUIC: prevented posted push event while in the draining state.
If the push event was posted before ngx_quic_close_connection(), it could send
data in the draining state.
2020-09-21 13:58:17 +03:00
Roman Arutyunyan
be719bbec8 HTTP/3: rearranged length check when parsing header.
The new code looks simpler and is similar to other checks.
2020-09-16 20:21:03 +01:00
Roman Arutyunyan
ebbcc329cb HTTP/3: removed HTTP/3 parser call from discard body filter.
Request body discard is disabled for QUIC streams anyway.
2020-09-16 19:48:33 +01:00
Roman Arutyunyan
9fff3b7516 HTTP/3: reject HTTP/2 frames.
As per HTTP/3 draft 30, section 7.2.8:

   Frame types that were used in HTTP/2 where there is no corresponding
   HTTP/3 frame have also been reserved (Section 11.2.1).  These frame
   types MUST NOT be sent, and their receipt MUST be treated as a
   connection error of type H3_FRAME_UNEXPECTED.
2020-09-16 12:27:23 +01:00
Roman Arutyunyan
d294369915 HTTP/3: skip unknown frames on request stream.
As per HTTP/3 draft 29, section 4.1:

   Frames of unknown types (Section 9), including reserved frames
   (Section 7.2.8) MAY be sent on a request or push stream before,
   after, or interleaved with other frames described in this section.

Also, trailers frame is now used as an indication of the request body end.
2020-08-24 09:56:36 +03:00
Roman Arutyunyan
46173bd4b4 HTTP/3: fixed handling request body eof.
While for HTTP/1 unexpected eof always means an error, for HTTP/3 an eof right
after a DATA frame end means the end of the request body.  For this reason,
since adding HTTP/3 support, eof no longer produced an error right after recv()
but was passed to filters which would make a decision.  This decision was made
in ngx_http_parse_chunked() and ngx_http_v3_parse_request_body() based on the
b->last_buf flag.

Now that since 0f7f1a509113 (1.19.2) rb->chunked->length is a lower threshold
for the expected number of bytes, it can be set to zero to indicate that more
bytes may or may not follow.  Now it's possible to move the check for eof from
parser functions to ngx_http_request_body_chunked_filter() and clean up the
parsing code.

Also, in the default branch, in case of eof, the following three things
happened, which were replaced with returning NGX_ERROR while implementing
HTTP/3:

- "client prematurely closed connection" message was logged
- c->error flag was set
- NGX_HTTP_BAD_REQUEST was returned

The change brings back this behavior for HTTP/1 as well as HTTP/3.
2020-09-16 18:59:25 +01:00
Vladimir Homutov
0bc772d1fb QUIC: switched to draft 29 by default. 2020-09-11 10:56:05 +03:00
Roman Arutyunyan
2e24e3811b QUIC: allowed old DCID for initial packets until first ACK.
If a packet sent in response to an initial client packet was lost, then
successive client initial packets were dropped by nginx with the unexpected
dcid message logged.  This was because the new DCID generated by the server was
not available to the client.
2020-09-09 16:35:29 +03:00
Roman Arutyunyan
f3bed9cd67 QUIC: eliminated idle timeout restart for dropped packets. 2020-09-08 15:54:02 +03:00
Sergey Kandaurov
786a74e34e QUIC: removed check for packet size beyond MAX_UDP_PAYLOAD_SIZE.
The check tested the total size of a packet header and unprotected packet
payload, which doesn't include the packet number length and expansion of
the packet protection AEAD.  If the packet was corrupted, it could cause
false triggering of the condition due to unsigned type underflow leading
to a connection error.

Existing checks for the QUIC header and protected packet payload lengths
should be enough.
2020-09-08 13:35:50 +03:00
Sergey Kandaurov
d8360f912a QUIC: check that the packet length is of at least sample size.
From quic-tls draft, section 5.4.2:
   An endpoint MUST discard packets that are not long enough to contain
   a complete sample.

The check includes the Packet Number field assumed to be 4 bytes long.
2020-09-08 13:28:56 +03:00
Sergey Kandaurov
952c6f1989 QUIC: update packet length for short packets too.
During long packet header parsing, pkt->len is updated with the Length
field value that is used to find next coalesced packets in a datagram.
For short packets it still contained the whole QUIC packet size.

This change uniforms packet length handling to always contain the total
length of the packet number and protected packet payload in pkt->len.
2020-09-08 13:27:39 +03:00
Roman Arutyunyan
e43ef3dda9 QUIC: added logging output stream frame offset. 2020-09-07 20:55:36 +03:00
Vladimir Homutov
6f78befe99 QUIC: refactored ngx_quic_retry_input().
The function now returns NGX_DECLINED for packets that need to be ignored
and integrates nicely into ngx_quic_input().
2020-09-04 15:48:53 +03:00
Roman Arutyunyan
6983bc0a37 QUIC: do not send STOP_SENDING after STREAM fin.
Previously STOP_SENDING was sent to client upon stream closure if rev->eof and
rev->error were not set.  This was an indirect indication that no RESET_STREAM
or STREAM fin has arrived.  But it is indeed possible that rev->eof is not set,
but STREAM fin has already been received, just not read out by the application.
In this case sending STOP_SENDING does not make sense and can be misleading for
some clients.
2020-09-06 14:51:23 +03:00
Vladimir Homutov
d6065b2791 QUIC: added support for multiple connection IDs.
The peer may issue additional connection IDs up to the limit defined by
transport parameter "active_connection_id_limit", using NEW_CONNECTION_ID
frames, and retire such IDs using RETIRE_CONNECTION_ID frame.
2020-09-03 13:11:27 +03:00
Vladimir Homutov
c36c54f500 QUIC: style.
Moved processing of RETIRE_CONNECTION_ID right after the NEW_CONNECTION_ID.
2020-08-27 10:15:37 +03:00
Vladimir Homutov
fb54f2acd9 QUIC: pass return code from ngx_quic_decrypt() to the caller.
It is required to distinguish internal errors from corrupted packets and
perform actions accordingly: drop the packet or close the connection.

While there, made processing of ngx_quic_decrypt() erorrs similar and
removed couple of protocol violation errors.
2020-09-02 22:34:15 +03:00
Vladimir Homutov
ff4cfa80e5 QUIC: discard unrecognized long packes.
While there, updated comment about discarded packets.
2020-09-02 09:54:15 +03:00
Roman Arutyunyan
e443b1244f HTTP/3: do not set the never-indexed literal bit by default.
The "Literal Header Field Never Indexed" header field representation is not
used in HTTP/2, and it makes little sense to make a distinction in HTTP/3.
2020-08-31 18:42:26 +03:00
Vladimir Homutov
d73a289c43 QUIC: discard incorrect packets instead of closing the connection.
quic-transport

5.2:
    Packets that are matched to an existing connection are discarded if
    the packets are inconsistent with the state of that connection.

5.2.2:
   Servers MUST drop incoming packets under all other circumstances.
2020-09-01 17:20:42 +03:00
Roman Arutyunyan
2087359675 QUIC: do not update largest packet number from a bad packet.
The removal of QUIC packet protection depends on the largest packet number
received.  When a garbage packet was received, the decoder still updated the
largest packet number from that packet.  This could affect removing protection
from subsequent QUIC packets.
2020-09-01 15:21:49 +03:00
Roman Arutyunyan
6f0e1bc14f QUIC: handle PATH_CHALLENGE frame.
A PATH_RESPONSE frame with the same data is sent in response.
2020-08-28 12:01:35 +03:00
Roman Arutyunyan
d69471b81b QUIC: enforce flow control on incoming STREAM and CRYPTO frames. 2020-08-25 17:22:57 +03:00
Roman Arutyunyan
693e55a4b2 HTTP/3: drop the unwanted remainder of the request.
As per HTTP/3 draft 29, section 4.1:

   When the server does not need to receive the remainder of the request,
   it MAY abort reading the request stream, send a complete response, and
   cleanly close the sending part of the stream.
2020-08-25 12:45:21 +03:00
Roman Arutyunyan
4e4d0938b9 QUIC: send STOP_SENDING on stream closure.
The frame is sent for a read-enabled stream which has not received a FIN or
RESET_STREAM.
2020-08-25 14:07:26 +03:00
Vladimir Homutov
2cd05e1909 QUIC: updated README.
- version negotiation is implemented
 - quic recovery implementation is greatly improved
2020-08-21 14:55:32 +03:00
Sergey Kandaurov
e153f4993c QUIC: disabled bidirectional SSL shutdown after 09fb2135a589.
On QUIC connections, SSL_shutdown() is used to call the send_alert callback
to send a CONNECTION_CLOSE frame.  The reverse side is handled by other means.
At least BoringSSL doesn't differentiate whether this is a QUIC SSL method,
so waiting for the peer's close_notify alert should be explicitly disabled.
2020-08-21 14:41:42 +03:00
Sergey Kandaurov
6c089cda29 QUIC: stripped down debug traces that have served its purpose.
The most observable remainers are incoming packet and stream payload
that could still be useful to debug various QUIC and HTTP/3 frames.
2020-08-21 14:41:41 +03:00
Vladimir Homutov
fb21151ff8 QUIC: dead code removed.
This case was already handled in c70446e3d771.
2020-08-21 10:00:25 +03:00
Vladimir Homutov
51b4d208d6 QUIC: removed outdated TODOs.
The logical quic connection state is tested by handler functions that
process corresponding types of packets (initial/handshake/application).
The packet is declined if state is incorrect.

No timeout is required for the input queue.
2020-08-20 16:45:48 +03:00
Vladimir Homutov
4ecea6cbed QUIC: added version negotiation support.
If a client attemtps to start a new connection with unsupported version,
a version negotiation packet is sent that contains a list of supported
versions (currently this is a single version, selected at compile time).
2020-08-20 17:11:04 +03:00
Roman Arutyunyan
5a4aaa6aed HTTP/3: special handling of client errors in the upstream module.
The function ngx_http_upstream_check_broken_connection() terminates the HTTP/1
request if client sends eof.  For QUIC (including HTTP/3) the c->write->error
flag is now checked instead.  This flag is set when the entire QUIC connection
is closed or STOP_SENDING was received from client.
2020-08-20 12:33:00 +03:00
Roman Arutyunyan
7ce1a68aad HTTP/3: request more client body bytes.
Previously the request body DATA frame header was read by one byte because
filters were called only when the requested number of bytes were read.  Now,
after 08ff2e10ae92 (1.19.2), filters are called after each read.  More bytes
can be read at once, which simplifies and optimizes the code.

This also reduces diff with the default branch.
2020-08-18 17:23:16 +03:00
Sergey Kandaurov
338c401534 QUIC: fixed format specifiers. 2020-08-19 16:00:12 +03:00
Sergey Kandaurov
a4e06606c5 QUIC: changed c->quic->pto_count type to ngx_uint_t.
This field is served as a simple counter for PTO backoff.
2020-08-19 15:58:03 +03:00
Sergey Kandaurov
f760147d9d QUIC: do not artificially delay sending queued frames.
This interacts badly with retransmissions of lost packets
and can provoke spurious client retransmits.
2020-08-19 13:24:54 +03:00
Sergey Kandaurov
3b83a140ff QUIC: do not arm loss detection timer on packet threshold. 2020-08-19 13:24:53 +03:00
Sergey Kandaurov
391abc00c9 QUIC: do not arm loss detection timer for succeeding packets. 2020-08-19 13:24:47 +03:00
Sergey Kandaurov
3bf7b02e6e QUIC: handling packets with send time equal to lost send time.
Previously, such packets weren't handled as the resulting zero remaining time
prevented setting the loss detection timer, which, instead, could be disarmed.
For implementation details, see quic-recovery draft 29, appendix A.10.
2020-08-19 13:24:30 +03:00
Sergey Kandaurov
fa82dccd2a QUIC: sending probe packets on PTO timer expiration.
The PTO handler is split into separate PTO and loss detection handlers
that operate interchangeably depending on which timer should be set.

The present ngx_quic_lost_handler is now only used for packet loss detection.
It replaces ngx_quic_pto_handler if there are packets preceeding largest_ack.
Once there is no more such packets, ngx_quic_pto_handler is installed again.

Probes carry unacknowledged data previously sent in the oldest packet number,
one per each packet number space.  That is, it could be up to two probes.

PTO backoff is now increased before scheduling next probes.
2020-08-19 13:24:23 +03:00
Sergey Kandaurov
160242dd2e QUIC: changed ctx->largest_ack initial value to type maximum.
In particular, this prevents declaring packet number 0 as lost if
there aren't yet any acknowledgements in this packet number space.
For example, only Initial packets were acknowledged in handshake.
2020-08-18 23:33:40 +03:00
Sergey Kandaurov
9a0fb643bf HTTP/3: fixed context storage in request body parser. 2020-08-18 17:11:32 +03:00