public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH] tcp: Handle data and FIN in TAP_SYN_RCVD like in established connections
@ 2026-11-27 20:01 Jon Maloy
  2026-12-01  8:23 ` Stefano Brivio
  0 siblings, 1 reply; 2+ messages in thread
From: Jon Maloy @ 2026-11-27 20:01 UTC (permalink / raw)
  To: sbrivio, jmaloy, passt-dev; +Cc: Zhenrong (Daniel) Gu, Xing Feng, Peng Zhang

In tcp_tap_handler(), the TAP_SYN_RCVD branch handles the segment
completing the handshake from the guest, but it also handles a FIN in
the same segment on its own, without checking that its sequence is the
expected one, or that the segment carries no data: on a FIN, we
increase seq_from_tap, shut down the socket for writing, send an ACK,
and return.

If the FIN is out of order, for example because it overtook the data
preceding it, we acknowledge data we never received, the server gets
an EOF without any data, and once the data arrives, we reset the
connection. If the FIN comes together with data, the data is lost. A
FIN without ACK is also accepted, as we only check for the ACK flag
afterwards. In both cases, we don't set TAP_FIN_RCVD either.

Further, if the segment is the last one in a batch, we return right
after processing the handshake, without looking at any data it might
carry: the data is neither forwarded nor acknowledged, and the guest
needs to retransmit it.

Check for the ACK flag before considering the connection established,
and only return early for a pure ACK: otherwise, let
tcp_data_from_tap() handle data and FIN, which already checks that the
FIN is in sequence, and sets TAP_FIN_RCVD, so that we shut down the
socket once all the data was forwarded.

With the reproducer attached to the bug report, which opens 100
connections through pasta with reordering on the tap interface, 34
connections end up with an empty EOF without this change, and none
with it.

Link: https://bugs.passt.top/show_bug.cgi?id=226
Reported-by: Zhenrong (Daniel) Gu <zrgu@stu.xjtu.edu.cn>
Reported-by: Xing Feng <xingfeng@stu.xjtu.edu.cn>
Reported-by: Peng Zhang <p-zhang@xjtu.edu.cn>
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
 tcp.c | 29 +++++++----------------------
 1 file changed, 7 insertions(+), 22 deletions(-)

diff --git a/tcp.c b/tcp.c
index 409f4ce1..8b249dd9 100644
--- a/tcp.c
+++ b/tcp.c
@@ -2391,37 +2391,22 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
 		if (th->syn && !th->ack && !th->fin)
 			return packet_next(p, idx) - idx;
 
-		if (!(conn->events & TAP_SYN_ACK_SENT))
+		if (!(conn->events & TAP_SYN_ACK_SENT) || !th->ack)
 			goto reset;
 
 		conn_event(c, conn, ESTABLISHED, now);
 		if (tcp_set_peek_offset(conn, 0, now))
 			goto reset;
 
-		if (th->fin) {
-			conn->seq_from_tap++;
-
-			if (shutdown(conn->sock, SHUT_WR) < 0) {
-				flow_warn_perror_ratelimit(conn, now,
-							   "shutdown() failed");
-				goto reset;
-			}
-
-			if (tcp_send_flag(c, conn, ACK, now))
-				goto reset;
-
-			conn_event(c, conn, SOCK_FIN_SENT, now);
-
-			return packet_next(p, idx) - idx;
-		}
-
-		if (!th->ack)
-			goto reset;
-
 		if (tcp_tap_window_update(c, conn, ntohs(th->window), now))
 			tcp_data_from_sock(c, conn, now);
 
-		if (packet_next(p, idx) == p->count)
+		/* Pure ACK completing the handshake, and nothing else to
+		 * process: otherwise, data and FIN, even if they're out of
+		 * order, are handled by tcp_data_from_tap() below
+		 */
+		if (packet_next(p, idx) == p->count && !th->fin &&
+		    !tcp_packet_data_len(th, l4len))
 			return packet_next(p, idx) - idx;
 	}
 
-- 
2.56.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] tcp: Handle data and FIN in TAP_SYN_RCVD like in established connections
  2026-11-27 20:01 [PATCH] tcp: Handle data and FIN in TAP_SYN_RCVD like in established connections Jon Maloy
@ 2026-12-01  8:23 ` Stefano Brivio
  0 siblings, 0 replies; 2+ messages in thread
From: Stefano Brivio @ 2026-12-01  8:23 UTC (permalink / raw)
  To: Jon Maloy; +Cc: passt-dev, Zhenrong (Daniel) Gu, Xing Feng, Peng Zhang

On Fri, 27 Nov 2026 15:01:24 -0500
Jon Maloy <jmaloy@redhat.com> wrote:

> In tcp_tap_handler(), the TAP_SYN_RCVD branch handles the segment
> completing the handshake from the guest, but it also handles a FIN in
> the same segment on its own, without checking that its sequence is the
> expected one, or that the segment carries no data: on a FIN, we
> increase seq_from_tap, shut down the socket for writing, send an ACK,
> and return.
> 
> If the FIN is out of order, for example because it overtook the data
> preceding it, we acknowledge data we never received, the server gets
> an EOF without any data, and once the data arrives, we reset the
> connection. If the FIN comes together with data, the data is lost. A
> FIN without ACK is also accepted, as we only check for the ACK flag
> afterwards. In both cases, we don't set TAP_FIN_RCVD either.
> 
> Further, if the segment is the last one in a batch, we return right
> after processing the handshake, without looking at any data it might
> carry: the data is neither forwarded nor acknowledged, and the guest
> needs to retransmit it.
> 
> Check for the ACK flag before considering the connection established,
> and only return early for a pure ACK: otherwise, let
> tcp_data_from_tap() handle data and FIN, which already checks that the
> FIN is in sequence, and sets TAP_FIN_RCVD, so that we shut down the
> socket once all the data was forwarded.
> 
> With the reproducer attached to the bug report, which opens 100
> connections through pasta with reordering on the tap interface, 34
> connections end up with an empty EOF without this change, and none
> with it.
> 
> Link: https://bugs.passt.top/show_bug.cgi?id=226
> Reported-by: Zhenrong (Daniel) Gu <zrgu@stu.xjtu.edu.cn>
> Reported-by: Xing Feng <xingfeng@stu.xjtu.edu.cn>
> Reported-by: Peng Zhang <p-zhang@xjtu.edu.cn>
> Signed-off-by: Jon Maloy <jmaloy@redhat.com>

Applied, thanks everybody for reporting and fixing this!

Checking again RFC 9293 after this report, I start thinking that
there's an unreachable situation described in 3.10.7 "SEGMENT ARRIVES".

I actually wrote the original implementation before RFC 9293 was
published, but the same reasoning applies to RFC 793.  Using RFC 9293
for clarity, section 3.10.7.4 "Other States" reports, as eighth step:

  https://www.rfc-editor.org/info/rfc9293/#section-3.10.7.4-2.8.1

  [...]

      -  If the FIN bit is set, signal the user "connection closing" and
         return any pending RECEIVEs with same message, advance RCV.NXT
         over the FIN, and send an acknowledgment for the FIN.  Note
         that FIN implies PUSH for any segment text not yet delivered to
         the user.

         o  SYN-RECEIVED STATE

         o  ESTABLISHED STATE

            +  Enter the CLOSE-WAIT state.

  [...]

and SYN-RECEIVED --FIN--> CLOSE-WAIT is the transition I intended to
implement.

However, the fifth step, if ACK is set, and we are in the SYN-RECEIVED
state:

  https://www.rfc-editor.org/info/rfc9293/#section-3.10.7.4-2.5.2.2.2.2.1

says:

  If SND.UNA < SEG.ACK =< SND.NXT, then enter ESTABLISHED state

(we should probably check the sequence explicitly here, but let's
ignore that for a moment).  So at that point we must always be in
ESTABLISHED state, unless ACK is not set, in which case:

  https://www.rfc-editor.org/info/rfc9293/#section-3.10.7.4-2.5.2.1

  if the ACK bit is off, drop the segment and return

So I don't see a way to reach that eighth step in SYN-RECEIVED state:

- if we get a segment with ACK set, we'll switch to ESTABLISHED in step
  five

- and if ACK is not set, we return at step five anyway

...which is rather weird.  Perhaps we should submit an erratum.  I see
a similar one (https://errata.rfc-editor.org/eid8805/) currently on
hold, but that one refers to FIN-WAIT-1 instead.  I'll need to check
again in a bit with fresh eyes, and submit it if it makes sense.

Anyway, with this change, we'll go (at least conceptually) through
ESTABLISHED in any case, which appears to be correct.

Back to the sequence check and that RFC 5961 mention in steps five and
four: _perhaps_ we should implement that at some point, even though
that kind of attack is not very relevant for us (if at all), because
the implementation is exposed to a single guest (at least for the
moment).

That's something we should definitely look into if we ever get to
support multiple guests/containers, though.

-- 
Stefano

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01  8:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 20:01 [PATCH] tcp: Handle data and FIN in TAP_SYN_RCVD like in established connections Jon Maloy
2026-12-01  8:23 ` Stefano Brivio

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).