This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 345bcf33c94b298fbd309d23cec88f3bf2b439ff Author: Marco Casaroli <[email protected]> AuthorDate: Wed Nov 18 19:42:11 2026 +0100 net/tcp: Report a reset of the peer as ECONNRESET. When the peer reset an established connection, recv() gave end-of-file after the data that came before the RST, as for a FIN, and a recv() that waited at the reset gave ENOTCONN. A program could not tell a stream that was cut from one that ended: a download cut by a reset looked complete. send() gave EPIPE at once, and poll() set SO_ERROR to ECONNREFUSED. POSIX and Linux: the first receive or send fails with ECONNRESET, and SO_ERROR gives it until it is reported. Now tcp_input() marks the connection when a RST comes in TCP_ESTABLISHED, TCP_FIN_WAIT_1 or TCP_FIN_WAIT_2 (TCP_RESET, TCP_RESETERR and SO_ERROR ECONNRESET). The first of these that returns the error reports it, once: - a recv() without data (the data that came before the RST is read first), also one that waits at the reset; the next one gives end-of-file; - a send() or sendfile(); the next one gives EPIPE (and SIGPIPE without MSG_NOSIGNAL); - getsockopt(SO_ERROR). A receive or send that returns a count keeps the error for the next call: recv() with MSG_WAITALL or with more than one buffer, a send() that queued a part of its data. poll() after the reset gives POLLHUP and POLLIN, and POLLERR while the error is not yet reported. A RST in TCP_SYN_SENT still refuses the connection: connect() and SO_ERROR give ECONNREFUSED. After the FIN of the peer the program already read the end of the stream: a receive still gives end-of-file and a send EPIPE. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/tcp/tcp.h | 49 +++++++++++++++++++++++++++++++--- net/tcp/tcp_input.c | 15 +++++++++++ net/tcp/tcp_monitor.c | 62 +++++++++++++++++++++++++++++++++++++++++++ net/tcp/tcp_netpoll.c | 32 ++++++++++++++++++---- net/tcp/tcp_recvfrom.c | 35 +++++++++++++++++++----- net/tcp/tcp_send_buffered.c | 9 ++++++- net/tcp/tcp_send_unbuffered.c | 6 ++--- net/tcp/tcp_sendfile.c | 4 +-- 8 files changed, 191 insertions(+), 21 deletions(-) diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h index 0ac488d22df..82704a2673b 100644 --- a/net/tcp/tcp.h +++ b/net/tcp/tcp.h @@ -100,13 +100,15 @@ #define TCP_SEQ_ADD(a, b) ((uint32_t)((a) + (b))) #define TCP_SEQ_SUB(a, b) ((uint32_t)((a) - (b))) -/* The error of a send on a TCP socket that is not connected: -EPIPE if the - * connection was closed (POSIX: the socket is no longer connected), else - * -ENOTCONN. +/* The error of a send on a TCP socket that is not connected: -ECONNRESET + * if the peer reset the connection and this is not yet reported, -EPIPE if + * the connection was closed (POSIX: the socket is no longer connected), + * else -ENOTCONN. */ #define TCP_NOTCONN_ERROR(conn) \ - (_SS_ISCLOSED((conn)->sconn.s_flags) ? -EPIPE : -ENOTCONN) + (tcp_reset_pending(conn) ? -ECONNRESET : \ + _SS_ISCLOSED((conn)->sconn.s_flags) ? -EPIPE : -ENOTCONN) /* The TCP options flags */ @@ -122,6 +124,9 @@ #endif +#define TCP_RESET 0x20U /* The peer reset the connection */ +#define TCP_RESETERR 0x40U /* ECONNRESET of it not yet reported */ + /* The Max Range count of TCP Selective ACKs */ #define TCP_SACK_RANGES_MAX 4 @@ -855,6 +860,42 @@ void tcp_stop_monitor(FAR struct tcp_conn_s *conn, uint32_t flags); void tcp_lost_connection(FAR struct tcp_conn_s *conn, FAR struct devif_callback_s *cb, uint32_t flags); +/**************************************************************************** + * Name: tcp_reset_pending + * + * Description: + * Tell if the ECONNRESET of a reset of the peer is not yet reported (by a + * receive, a send or getsockopt(SO_ERROR)). + * + * Input Parameters: + * conn - The TCP connection + * + * Returned Value: + * true if a receive or send must give ECONNRESET. + * + ****************************************************************************/ + +bool tcp_reset_pending(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_reset_report + * + * Description: + * Give the result of a receive or send that returns now. If it is + * -ECONNRESET, the reset is reported: the next receive gives end-of-file, + * the next send EPIPE and SO_ERROR 0. + * + * Input Parameters: + * conn - The TCP connection + * ret - The result that the receive or send returns + * + * Returned Value: + * ret + * + ****************************************************************************/ + +ssize_t tcp_reset_report(FAR struct tcp_conn_s *conn, ssize_t ret); + /**************************************************************************** * Name: tcp_close * diff --git a/net/tcp/tcp_input.c b/net/tcp/tcp_input.c index 702f0a6cbc1..3b7c0a7118e 100644 --- a/net/tcp/tcp_input.c +++ b/net/tcp/tcp_input.c @@ -53,6 +53,7 @@ #include <stdint.h> #include <string.h> #include <assert.h> +#include <errno.h> #include <nuttx/debug.h> #include <nuttx/net/netconfig.h> @@ -62,6 +63,7 @@ #include <nuttx/net/tcp.h> #include "devif/devif.h" +#include "socket/socket.h" #include "utils/utils.h" #include "tcp/tcp.h" @@ -1156,6 +1158,19 @@ found: tcp_conn_list_unlock(); #endif + /* A RST before the FIN of the peer: the next receive or send + * gives ECONNRESET. In TCP_SYN_SENT the RST refuses the + * connection (ECONNREFUSED). After the FIN of the peer a + * receive gives end-of-file and a send EPIPE. + */ + + if ((conn->tcpstateflags & TCP_STATE_MASK) >= TCP_ESTABLISHED && + (conn->tcpstateflags & TCP_STATE_MASK) <= TCP_FIN_WAIT_2) + { + conn->flags |= TCP_RESET | TCP_RESETERR; + _SO_CONN_SETERRNO(conn, ECONNRESET); + } + nwarn("WARNING: RESET TCP state: TCP_CLOSED\n"); conn->tcpstateflags = TCP_CLOSED; diff --git a/net/tcp/tcp_monitor.c b/net/tcp/tcp_monitor.c index d8b55aaa8ac..dbd769dda36 100644 --- a/net/tcp/tcp_monitor.c +++ b/net/tcp/tcp_monitor.c @@ -28,6 +28,7 @@ #include <stdint.h> #include <assert.h> +#include <errno.h> #include <nuttx/debug.h> #include <nuttx/net/tcp.h> @@ -412,4 +413,65 @@ void tcp_lost_connection(FAR struct tcp_conn_s *conn, tcp_shutdown_monitor(conn, flags); } +/**************************************************************************** + * Name: tcp_reset_pending + * + * Description: + * Tell if the ECONNRESET of a reset of the peer is not yet reported (by a + * receive, a send or getsockopt(SO_ERROR)). + * + * Input Parameters: + * conn - The TCP connection + * + * Returned Value: + * true if a receive or send must give ECONNRESET. + * + ****************************************************************************/ + +bool tcp_reset_pending(FAR struct tcp_conn_s *conn) +{ +#ifdef CONFIG_NET_SOCKOPTS + /* getsockopt(SO_ERROR) reads and clears the same error */ + + return (conn->flags & TCP_RESETERR) != 0 && + conn->sconn.s_error == ECONNRESET; +#else + return (conn->flags & TCP_RESETERR) != 0; +#endif +} + +/**************************************************************************** + * Name: tcp_reset_report + * + * Description: + * Give the result of a receive or send that returns now. If it is + * -ECONNRESET, the reset is reported: the next receive gives end-of-file, + * the next send EPIPE and SO_ERROR 0. + * + * Input Parameters: + * conn - The TCP connection + * ret - The result that the receive or send returns + * + * Returned Value: + * ret + * + ****************************************************************************/ + +ssize_t tcp_reset_report(FAR struct tcp_conn_s *conn, ssize_t ret) +{ + if (ret == -ECONNRESET) + { + conn_dev_lock(&conn->sconn, conn->dev); + if (tcp_reset_pending(conn)) + { + conn->flags &= ~TCP_RESETERR; + _SO_CONN_SETERRNO(conn, OK); + } + + conn_dev_unlock(&conn->sconn, conn->dev); + } + + return ret; +} + #endif /* NET_TCP_HAVE_STACK */ diff --git a/net/tcp/tcp_netpoll.c b/net/tcp/tcp_netpoll.c index ebed6bdfc36..66cf9614149 100644 --- a/net/tcp/tcp_netpoll.c +++ b/net/tcp/tcp_netpoll.c @@ -123,6 +123,13 @@ static uint32_t tcp_poll_eventhandler(FAR struct net_driver_s *dev, reason = ENETUNREACH; } + /* TCP_ABORT: a reset of the peer */ + + else if ((info->conn->flags & TCP_RESET) != 0) + { + reason = ECONNRESET; + } + /* TCP_ABORT: The remote host has aborted the connection */ else @@ -135,6 +142,13 @@ static uint32_t tcp_poll_eventhandler(FAR struct net_driver_s *dev, _SO_CONN_SETERRNO(info->conn, reason); #endif + /* After a reset of the peer, a receive does not block */ + + if ((info->conn->flags & TCP_RESET) != 0) + { + eventset |= POLLIN; + } + /* Mark that the connection has been lost */ tcp_lost_connection(info->conn, info->cb, flags); @@ -368,16 +382,24 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds) * exceptional event. */ - if (_SS_ISCLOSED(conn->sconn.s_flags)) + /* After a reset of the peer: POLLHUP and end-of-file to read, and + * POLLERR while its ECONNRESET is not yet reported (SO_ERROR has it). + */ + + if ((conn->flags & TCP_RESET) != 0) { - _SO_CONN_SETERRNO(conn, ECONNREFUSED); + eventset |= POLLHUP | POLLRDNORM; + if (tcp_reset_pending(conn)) + { + eventset |= POLLERR; + } } else { - _SO_CONN_SETERRNO(conn, ENOTCONN); + _SO_CONN_SETERRNO(conn, (_SS_ISCLOSED(conn->sconn.s_flags) ? + ECONNREFUSED : ENOTCONN)); + eventset |= POLLERR | POLLHUP; } - - eventset |= POLLERR | POLLHUP; } else if (_SS_ISCONNECTED(conn->sconn.s_flags) && psock_tcp_cansend(conn) >= 0) diff --git a/net/tcp/tcp_recvfrom.c b/net/tcp/tcp_recvfrom.c index b8c6f6a2b2e..7ec13d5c0c4 100644 --- a/net/tcp/tcp_recvfrom.c +++ b/net/tcp/tcp_recvfrom.c @@ -506,11 +506,22 @@ static uint32_t tcp_recvhandler(FAR struct net_driver_s *dev, } /* Report a timed out connection (too many retransmissions or - * unanswered keep-alive probes) as ETIMEDOUT. + * unanswered keep-alive probes) as ETIMEDOUT, and a reset of the + * peer as ECONNRESET (end-of-file once that is reported). */ - pstate->ir_result = (flags & TCP_TIMEDOUT) != 0 ? -ETIMEDOUT : - -ENOTCONN; + if ((flags & TCP_TIMEDOUT) != 0) + { + pstate->ir_result = -ETIMEDOUT; + } + else if ((conn->flags & TCP_RESET) != 0) + { + pstate->ir_result = tcp_reset_pending(conn) ? -ECONNRESET : 0; + } + else + { + pstate->ir_result = -ENOTCONN; + } /* Wake up the waiting thread */ @@ -729,10 +740,17 @@ static ssize_t tcp_recvfrom_one(FAR struct tcp_conn_s *conn, FAR void *buf, * negative) and the connection was gracefully closed by the remote * peer, then return success. If ir_recvlen is zero, the caller of * recvfrom() will get an end-of-file indication. + * + * A reset of the peer gives ECONNRESET, after the data that came + * before it, until psock_tcp_recvfrom() returns it. */ - if (ret <= 0 && !_SS_ISCLOSED(conn->sconn.s_flags) && - (conn->shutdown & SHUT_RD) == 0) + if (ret <= 0 && tcp_reset_pending(conn)) + { + ret = -ECONNRESET; + } + else if (ret <= 0 && !_SS_ISCLOSED(conn->sconn.s_flags) && + (conn->shutdown & SHUT_RD) == 0) { /* Nothing was previously received from the read-ahead buffers. * The SOCK_STREAM must be (re-)connected in order to receive @@ -911,8 +929,13 @@ ssize_t psock_tcp_recvfrom(FAR struct socket *psock, FAR struct msghdr *msg, } } + /* Data that came before a reset of the peer goes first: its ECONNRESET + * stays for the next receive. + */ + + ret = nrecv ? nrecv : tcp_reset_report(conn, ret); conn_dev_unlock(&conn->sconn, conn->dev); - return nrecv ? nrecv : ret; + return ret; } #endif /* CONFIG_NET_TCP */ diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c index ef251305061..a8f43163751 100644 --- a/net/tcp/tcp_send_buffered.c +++ b/net/tcp/tcp_send_buffered.c @@ -1346,7 +1346,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { nerr("ERROR: Not connected\n"); - ret = TCP_NOTCONN_ERROR(conn); + ret = tcp_reset_report(conn, TCP_NOTCONN_ERROR(conn)); goto errout; } @@ -1414,6 +1414,13 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, { nerr("ERROR: No longer connected\n"); ret = TCP_NOTCONN_ERROR(conn); + if (result == 0) + { + /* No byte of this send was queued: it returns the error */ + + tcp_reset_report(conn, ret); + } + goto errout_with_lock; } diff --git a/net/tcp/tcp_send_unbuffered.c b/net/tcp/tcp_send_unbuffered.c index ebd74565acf..f587330ca84 100644 --- a/net/tcp/tcp_send_unbuffered.c +++ b/net/tcp/tcp_send_unbuffered.c @@ -521,7 +521,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock, if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { nerr("ERROR: Not connected\n"); - ret = TCP_NOTCONN_ERROR(conn); + ret = tcp_reset_report(conn, TCP_NOTCONN_ERROR(conn)); goto errout; } @@ -578,8 +578,8 @@ ssize_t psock_tcp_send(FAR struct socket *psock, if (!_SS_ISCONNECTED(conn->sconn.s_flags)) { nerr("ERROR: No longer connected\n"); + ret = tcp_reset_report(conn, TCP_NOTCONN_ERROR(conn)); conn_dev_unlock(&conn->sconn, conn->dev); - ret = TCP_NOTCONN_ERROR(conn); goto errout; } @@ -667,7 +667,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock, if (state.snd_sent < 0) { - ret = state.snd_sent; + ret = tcp_reset_report(conn, state.snd_sent); goto errout; } diff --git a/net/tcp/tcp_sendfile.c b/net/tcp/tcp_sendfile.c index c44a2798e4b..b1bb76fb14d 100644 --- a/net/tcp/tcp_sendfile.c +++ b/net/tcp/tcp_sendfile.c @@ -432,7 +432,7 @@ ssize_t tcp_sendfile(FAR struct socket *psock, FAR struct file *infile, if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(conn->sconn.s_flags)) { nerr("ERROR: Not connected\n"); - return TCP_NOTCONN_ERROR(conn); + return tcp_reset_report(conn, TCP_NOTCONN_ERROR(conn)); } /* Make sure that we have the IP address mapping */ @@ -584,7 +584,7 @@ errout_locked: return startpos; } - return state.snd_sent; + return tcp_reset_report(conn, state.snd_sent); } #endif /* CONFIG_NET_SENDFILE && CONFIG_NET_TCP && NET_TCP_HAVE_STACK */
