Skip to content

net/tcp: Fix data sent back after a FIN, ACKs after a gap, and the timing of connect(). - #22144

Merged
xiaoxiang781216 merged 6 commits into
apache:masterfrom
casaroli:tcp-input-fixes-up
Nov 23, 2026
Merged

xiaoxiang781216 merged 6 commits into
apache:masterfrom
casaroli:tcp-input-fixes-up

Conversation

@casaroli

@casaroli casaroli commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR fixes four defects of TCP in net/tcp. Each fix is one commit; fixes 3 and 4 are each followed by a documentation commit.

  1. net/tcp: Do not send received data back with the ACK. tcp_input() does not clear dev->d_sndlen on the paths for data in FIN_WAIT_1 and FIN_WAIT_2 and for a FIN in ESTABLISHED. Over the loopback device it still holds the length of the input segment, so tcp_appsend() sends that many bytes of the input buffer back to the peer as new data, after our FIN. A client sends 100 bytes, calls shutdown(SHUT_WR) and reads a 16 KiB answer: the transfer never ends (176496 segments in 60 s), and the server stays in CLOSE_WAIT. Now tcp_input() clears d_sndlen where it sets the length of the received data, as udp_input() does.
  2. net/tcp: Process a segment without data after a gap. net/tcp: Do not answer an empty segment in the receive window #22137 stops the ACK to a segment without data, SYN or FIN in the receive window after a gap (RFC 9293, 3.10.7.4), but it gives only a new ACK to the connection and returns before the window update and the state machine. So the window that such a segment opens is not used until the next zero window probe, an ACK of our FIN does not move FIN_WAIT_1 to FIN_WAIT_2, and in SYN_RCVD an ACK of our SYN-ACK does not complete the handshake. Now such a segment goes on to the window update and the state machine, as a segment at rcvseq does, and still gets no ACK. A segment at or after rcv_adv (the right edge) still gets the ACK and is dropped.
  3. net/tcp: Do not send the first SYN as a retransmission. The first SYN of connect() goes out through the timeout branch of tcp_timer(), which counts a retransmission and, since 872bff9, doubles the RTO first. A lost first SYN is sent again after 3 s instead of 1.5 s. Now TCP_SYNFIRST marks the first SYN, and TCP_MAXSYNRTX counts only the resends, as TCP_MAXRTX does for data. NET_TCP_MAXSYNRTX gets a help text that says so.
  4. net/tcp: End a blocking connect() at the send timeout. A blocking connect() waits for the handshake about 70 s, whatever SO_SNDTIMEO says. Now it waits at most SO_SNDTIMEO, then gives EINPROGRESS and the connection goes on as for a non-blocking connect(), as on Linux (socket(7)).

Impact

net/tcp only (tcp_input.c, tcp_timer.c, tcp_conn.c, tcp_connect.c, tcp_monitor.c, tcp.h, Kconfig). No API or ABI change, no new Kconfig option. Common network code: the same in flat, protected and kernel builds and on every architecture.

  • Fix 2: a segment with data, SYN or FIN after a gap, and a segment at or after the right edge of the window, get the ACK and are dropped, as before.
  • Fix 3: a connect() to a silent peer sends 6 SYNs instead of 5 (1.5, 3, 6, ... s apart) and gives ETIMEDOUT after 70.5 s instead of 69 s. The rexmit statistic no longer counts the first SYN.
  • Fix 4: only sockets with SO_SNDTIMEO. A program that handles EINPROGRESS as for a non-blocking socket works; one that closes the socket on it ends the attempt, as on Linux. These set SO_SNDTIMEO before a blocking connect() and take any error as fatal, so a handshake slower than the timeout now fails with EINPROGRESS: in nuttx-apps netutils/webclient/webclient.c (its configured timeout) and system/nxplayer/nxplayer.c (10 s); in NuttX the DNS client over TCP (libs/libc/netdb/lib_dnsbind.c, 5 s, doubled at each retry: the query is tried again). testing/nettest test_tcp_connect_ipv4 and _ipv6 set 10 ms before a connect() over the loopback device; the handshake completes before it (below).
  • Fix 4, not changed here: a second connect() while the handshake runs gives EISCONN (Linux: EALREADY), as it did before for a non-blocking socket.

Testing

qemu-armv8a:knsh (kernel build, 4 CPUs) and qemu-armv8a:nsh (flat build, 1 CPU) under HVF (Apple M-series), TCP write buffers, NET_TCPBACKLOG, NET_SOCKOPTS, on master c51b1af. Fixes 2 to 4 use a TUN device (NET_TUN_PKTSIZE 1500) and the peer that the test plays on it.

Test master this PR
test_tcp_halfclose_answer (apache/nuttx-apps#4105): request, shutdown(SHUT_WR), 16 KiB answer over loopback knsh: the case does not end (the loopback work thread keeps the device locked); flat: FAIL, no end of file after 10 s PASS
ackahead: the TUN peer sends 20 ACKs ahead of rcvseq, then opens the window no answers; the data comes 4.0 s after the window update no answers; the data at once
wndedge: the TUN peer sends segments without data at and after the right edge of the window, then reads each one gets an ACK; the rest of the stream comes at once the same
synrto: the TUN peer drops the first SYN first resend after 3.0 s after 1.5 s
syngiveup: a silent peer 5 SYNs, ETIMEDOUT after 69.0 s 6 SYNs, ETIMEDOUT after 70.5 s
connect() with SO_SNDTIMEO 1 s, the TUN peer answers after 2.5 s returns 0 at 3.0 s EINPROGRESS after 1.0 s, then POLLOUT, SO_ERROR 0, data reaches the peer

The TUN cases run 3 times in each boot. No other case of cmocka_net_tcp, cmocka_net_udp and cmocka_net_tun changes. In flat nsh test_tcp_reset_send fails on master and with this PR alike: the SIGPIPE goes to another thread of the process. test_tun_tcp_lossy needs #22132.

The TUN cases come to apache/nuttx-apps when the TUN test PR apache/nuttx-apps#4101 merges.

Builds in the NuttX CI image (EXTRAFLAGS="-Wno-cpp -Werror") of these fixes on master 5814b40, before the rebase on #22137: qemu-armv8a:netnsh (Make and CMake), rv-virt:pnsh64 (protected), rv-virt:knetnsh64 (kernel), sim:tcpblaster, lm3s6432-s2e:nsh, qemu-intel64:nsh_pci: 0 warnings. Text size: +20 to +128 bytes per fix.

tools/checkpatch.sh -c -u -m -g passes. Documentation: the commit after fix 3 adds the first SYN to the retransmission timer part of Documentation/os/networking/tcp_state_machine.rst; the commit after fix 4 adds the SO_SNDTIMEO case to EINPROGRESS of connect() in Documentation/reference/user/11_network.rst.

@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: S The size of the change in this PR is small labels Nov 21, 2026
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

@cederom

cederom commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Commit 1 (64980a8, clear d_sndlen): correct. It clears d_sndlen right after d_len is set, before any state path runs. The only paths that read it are the ones that don't reset it themselves, so no other behaviour changes.

Commit 2 (dc71005, no ACK for an empty segment after a gap): needs one more check.

  • The new code lets an empty segment with seq > rcvseq go on to tcp_snd_wnd_update() and the state machine, but nothing checks the right edge of the receive window.
  • RFC 9293 §3.10.7.4 only accepts an empty segment if RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND. A segment beyond the window should still get the ACK and be dropped, which is what the old code did.
  • What goes wrong: such a segment sets snd_wl1 = seq far ahead. After that, valid segments have seq < wl1, so the wl1 < seq and wl1 == seq conditions in tcp_snd_wnd_update() both fail. Window updates that come with a new ACK are then ignored, and snd_wnd can only shrink. The sender drifts toward zero-window probing.
  • That segment can be stale or spoofed. Its sequence number only has to be ahead of rcvseq (anywhere up to 2³¹), not inside the window.
  • Suggested fix: only take the new path when TCP_SEQ_LT(seq, conn->rcv_adv), the advertised right edge. Otherwise keep tcp_input_dropack().
  • A smaller side effect: in SYN_RCVD, an empty segment after a gap with a valid ACK now finishes the handshake. That is allowed by the RFC, but worth stating in the commit message.

Commit 3 (e6bd937, first SYN is not a retransmission): correct.

  • tcp_synack() goes through tcp_send() with the timer flag, which starts the retransmission timer at TCP_RTO.
  • I checked the timing with the defaults: resends at 1.5, 4.5, 10.5, 22.5 and 46.5 s, then the connect fails at 70.5 s. That matches the "70 s, was 69 s" in the commit message.
  • Nits:
    • TCP_SYNFIRST is 0x100 while 0x80 is free. If that's on purpose, please say why.
    • NET_TCP_MAXSYNRTX in net/tcp/Kconfig has no help text, and its meaning changes here (one more SYN is sent). A one-line help saying "number of SYN resends after the first SYN" would make the change visible.
    • The new fast path skips tcp_setsequence(conn->sndseq, conn->rexmit_seq), which the SYN_SENT resend case does. The two values are equal at that point, so it's harmless, just inconsistent.

Commit 5 (4ad8f55, blocking connect() ends at SO_SNDTIMEO): correct, and matches Linux.

  • Using tc_result when the handler finished just before the wait ended fixes a real race.

  • The _SS_ISCONNECTING change in tcp_start_monitor() is safe: the connect handler clears that flag on TCP_CONNECTED, so a successful blocking connect is unaffected.

  • Behaviour change for apps: I found two in nuttx-apps that set SO_SNDTIMEO before a blocking connect() and treat any error as fatal:

    • netutils/webclient/webclient.c, with its configured timeout;
    • system/nxplayer/nxplayer.c, with a hard-coded 10 s.

    A handshake slower than that now fails with EINPROGRESS instead of waiting. That's what Linux does, but it should go in the PR's Impact section.

  • Follow-up, not a blocker: a second connect() while the handshake is still running reaches tcp_connect(), which returns -EISCONN because the state isn't TCP_ALLOCATED. Linux returns EALREADY. Non-blocking sockets already had this problem, but blocking callers can now hit it too.

  • Nit: psock_teardown_callbacks(&state, -EINPROGRESS) calls tcp_stop_monitor() because the status is negative. Nothing is registered yet, so nothing happens, but ret != -EINPROGRESS ? ret : OK would read more clearly.

Doc commits (4f037b2, f5f3cef): fine. They use tab indentation, the same as the existing lines in those files.

Comment thread net/tcp/tcp.h Resolved
tcp_input() did not clear dev->d_sndlen, the length of the data that a
connection callback queues for the response.  It still holds the length
of the data that the device sent last: over the loopback device, the
length of the segment that tcp_input() now handles.  The paths for
ESTABLISHED, SYN_RCVD, SYN_SENT and CLOSE_WAIT clear it before they call
the callbacks; the paths for data in FIN_WAIT_1 and FIN_WAIT_2 and for a
FIN in ESTABLISHED do not.  tcp_appsend() then sent that many bytes of
the input buffer, the data that just came from the peer, back to the
peer as new data, after our FIN.

A client that shuts down writing and then reads the answer of the server
sent each segment of the answer back.  Its sequence numbers then went
beyond its FIN: the server dropped its ACKs, sent the last segment of
the answer again at each retransmission timeout, and stayed in
CLOSE_WAIT with that segment in its write buffers; the client never got
the FIN of the server.

Clear d_sndlen where tcp_input() sets the length of the received data,
as udp_input() does.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
0ac64ef ("net/tcp: Do not answer an empty segment in the receive
window") stops the ACK that tcp_input() gave to a segment without data,
SYN or FIN in the receive window after the next expected sequence
number: an ACK or a window update of a peer whose segment was lost.
But it gives only a new ACK to the connection and returns before the
window update and the state machine.  RFC 9293, section 3.10.7.4,
processes such a segment in full.

So the window that such a segment opens is not used: the sender waits
for its next zero window probe.  An ACK of our FIN in such a segment
does not move FIN_WAIT_1 to FIN_WAIT_2, and in SYN_RCVD an ACK of our
SYN-ACK does not complete the handshake.

Such a segment now goes on to the window update and the state machine,
as a segment at the next expected sequence number does.  It still gets
no ACK.  A segment at or after rcv_adv, the right edge of the window
that we advertised, is not in the window: it still gets the ACK and is
dropped, so it cannot move snd_wl1 ahead of the peer.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
tcp_connect() sets conn->timeout, so the first SYN goes out through the
timeout branch of tcp_timer().  That branch counts a retransmission
(nrtx and the rexmit statistic) and, since 872bff9 ("Double the
retransmission timeout at the first expiry too"), doubles the RTO
before it sends the SYN.  So the first resend of a lost SYN comes after
twice the initial RTO (3 s with the default CONFIG_NET_TCP_RTO of
1.5 s), and the statistic counts every SYN as a retransmission.

tcp_connect() now marks the connection with TCP_SYNFIRST.  The timer
sends the first SYN with the initial RTO, without a count, and clears
the mark.  A later timeout is a retransmission as before.  The first
resend now comes after the initial RTO (RFC 6298, section 2.1).

TCP_MAXSYNRTX now counts only the resends, as TCP_MAXRTX counts the
resends of data: a connect() sends one SYN more before it gives up
(with the default 5: the SYN and 5 resends).  This is what the name and
RFC 1122, section 4.2.3.5 (R2 for a SYN), mean: the limit is on
retransmissions, and the first SYN is not one.  The time until a
connect() to a silent peer fails stays about the same (70 s, was 69 s).
NET_TCP_MAXSYNRTX gets a help text that says so.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
The first SYN of connect() goes out with the initial retransmission
timeout and does not count as a retransmission.  NET_TCP_MAXSYNRTX
limits the resends of the SYN.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
psock_tcp_connect() waits for the handshake without a time limit.  A
blocking connect() to a peer that drops the SYNs waits until the SYN
retransmissions give up, about 70 s, whatever SO_SNDTIMEO says.

Linux ends the wait at the send timeout with EINPROGRESS, as if the
socket were non-blocking (socket(7), SO_SNDTIMEO), and the connection
goes on.  Do the same: wait at most SO_SNDTIMEO.  When it expires,
connect() gives EINPROGRESS and the connection monitor takes the
connection over, as for a non-blocking connect(): poll() reports
POLLOUT when the handshake completes, and SO_ERROR gives the error of a
connection that fails.

tcp_start_monitor() takes a connection in TCP_SYN_SENT as one in
progress when the socket is connecting, not only when it is
non-blocking.

A result that the event handler sets just before the wait ends (with
the timeout or a signal) is now used, as the handler already tore its
callback down.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
A blocking connect() gives EINPROGRESS when the send timeout of the
socket expires before the handshake completes.  The connection goes on.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli

Copy link
Copy Markdown
Contributor Author

Done. Commit 2 is now on top of #22137, which merged today: it keeps the check of the right edge there (TCP_SEQ_LT(seq, conn->rcv_adv)) and adds the window update and the state machine.

@cederom

cederom commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Re-reviewed the new head (range-diff against the previous one).

Resolved

  • net/tcp: Process a segment without data after a gap: an empty segment after a gap now goes to the window update and the state machine only if seq < rcv_adv. Anything at or past the advertised right edge still gets the ACK and is dropped, so snd_wl1 can no longer be pushed ahead by an out-of-window segment. With a zero window (rcv_adv == rcvseq) only seq == rcvseq is accepted, which is what RFC 9293 3.10.7.4 says. rcv_adv is set on the passive side too (tcp_conn.c, then updated by the SYN-ACK in tcp_send()), so the SYN_RCVD case is covered. I checked ESTABLISHED, FIN_WAIT_1 and FIN_WAIT_2: with d_len == 0 and no FIN, none of them uses rcvseq in a way that breaks when seq != rcvseq.
  • TCP_SYNFIRST is now 0x80U.
  • The first-SYN path now calls tcp_setsequence(conn->sndseq, conn->rexmit_seq), the same as the resend path.
  • NET_TCP_MAXSYNRTX has help text, and it is correct for both sides: SYN_RCVD also checks nrtx >= TCP_MAXSYNRTX before nrtx++, so the first SYN-ACK is not counted either.
  • psock_teardown_callbacks() gets OK for -EINPROGRESS.

Remaining

  1. Rebase. The base c51b1af1 is about 27 commits behind master, and master has since changed tcp.h, tcp_connect.c, tcp_monitor.c and tcp_timer.c (e2c6fad, 4daa109, e770d57, e54d067, ...). I rebased locally: it applies with no conflicts. I read the merged tcp_start_monitor() and found nothing that clashes with the _SS_ISCONNECTING change. Please still rebase so CI runs against current master.
  2. Impact section. Please name the in-tree users that set SO_SNDTIMEO before a blocking connect() and treat any error as fatal: netutils/webclient (its configured timeout) and system/nxplayer (a hard-coded 10 s). Both will now fail on a handshake slower than that timeout.
  3. Follow-up, not blocking: calling connect() again while the handshake is still running gives EISCONN (from the state check in tcp_connect()), where Linux gives EALREADY. Blocking callers can now hit this after EINPROGRESS.

nxstyle is clean on the changed files. With the rebase and the Impact note, LGTM.

@casaroli

Copy link
Copy Markdown
Contributor Author
  1. CI builds pull/22144/merge, the merge of this branch with master, and the branch merges with master with no conflict. A push now also clears the approval.
  2. Done: the Impact section names both, and the DNS client over TCP.
  3. The body names it as a follow-up.

@cederom cederom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @casaroli :-)

@xiaoxiang781216
xiaoxiang781216 merged commit d36ca1e into apache:master Nov 23, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants