From f8f340650247e97b0633a33cdb08e20f2880b36b Mon Sep 17 00:00:00 2001 From: Jon Maloy Date: Wed, 2 Dec 2026 15:21:03 -0500 Subject: tcp: Retransmit the same SYN or SYN-ACK on handshake timeout Commit 0e8df13c96be ("tcp: Resend SYN for inbound connections") lets tcp_timer_handler() retry the handshake for connections that are taking too long to establish, by sending a new, bare SYN at each retry. However, this approach has two issues: - For connections initiated by the guest (TAP_SYN_RCVD), we send a SYN-ACK, not a SYN, so the retry carries the wrong flags, and lacks the ACK - As tcp_prepare_flags() advances seq_to_tap for every SYN we send, every retry uses a different sequence number, and, once the handshake completes, we start sending data from the ISN plus the number of retries, instead of ISN + 1 If the guest acknowledges our original SYN-ACK (or SYN) after one or more retries, for example because the ACK was held in a queue, we send data with sequence numbers shifted by the number of retries, and, once the retransmission timer rewinds to the acknowledged sequence, the same data again, so that the guest assembles a corrupted stream. Retransmit what we originally sent instead: a SYN-ACK if the guest initiated the connection, a SYN otherwise, and reset seq_to_tap to the initial sequence number first, which is still stored in seq_ack_from_tap at this point. If the guest initiated the connection, i.e. the TAP_SYN_RCVD flag is set, we set the ACK flag in each retry, otherwise not. In both cases, we rewind the sequence number to ISN before retransmitting. Reported-by: Fumin Link: https://bugs.passt.top/show_bug.cgi?id=229 Fixes: 0e8df13c96be ("tcp: Resend SYN for inbound connections") Signed-off-by: Jon Maloy Signed-off-by: Stefano Brivio --- tcp.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tcp.c b/tcp.c index ca0c185..f55cf26 100644 --- a/tcp.c +++ b/tcp.c @@ -2724,8 +2724,19 @@ void tcp_timer_handler(const struct ctx *c, union epoll_ref ref, flow_dbg(conn, "handshake timeout"); tcp_rst(c, conn, now); } else { + int flags = SYN; + flow_trace(conn, "SYN timeout, retry"); - if (tcp_send_flag(c, conn, SYN, now)) { + + /* Retransmit what we sent: a SYN-ACK if the + * guest initiated the connection, a SYN + * otherwise. Rewind to ISN at each retry. + */ + if (conn->events & TAP_SYN_RCVD) + flags |= ACK; + conn->seq_to_tap = conn->seq_ack_from_tap; + + if (tcp_send_flag(c, conn, flags, now)) { tcp_rst(c, conn, now); return; } -- cgit v1.2.3