From ee5009295c1606cbb5a71e25c1545aef46efc1cc Mon Sep 17 00:00:00 2001 From: wenquan1 Date: Tue, 1 Jul 2025 15:36:28 +0800 Subject: [PATCH] tcp_reset: Modify RST packet responses to comply with RFC 793. According to RFC793, the flags of TCP reply packet should be as follows: 1. `RST` if flags of request packet has `ACK` 2. `RST|ACK` if flags of request packet has no `ACK`, at the same time, the sequence should be set to zero. Signed-off-by: wenquan1 --- net/tcp/tcp_send.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/net/tcp/tcp_send.c b/net/tcp/tcp_send.c index 9e6dd4e523e..02e289ee563 100644 --- a/net/tcp/tcp_send.c +++ b/net/tcp/tcp_send.c @@ -382,7 +382,6 @@ void tcp_send(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn, void tcp_reset(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) { FAR struct tcp_hdr_s *tcp; - uint32_t ackno; uint16_t tmp16; uint16_t acklen = 0; uint8_t seqbyte; @@ -437,7 +436,6 @@ void tcp_reset(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) acklen -= (tcp->tcpoffset >> 4) << 2; - tcp->flags = TCP_RST | TCP_ACK; tcp->tcpoffset = 5 << 4; /* Flip the seqno and ackno fields in the TCP header. */ @@ -463,9 +461,19 @@ void tcp_reset(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) * to propagate the carry to the other bytes as well. */ - ackno = tcp_addsequence(tcp->ackno, acklen); - - tcp_setsequence(tcp->ackno, ackno); + if ((tcp->flags & TCP_ACK) != 0) + { + tcp->flags = TCP_RST; + tcp_setsequence(tcp->ackno, 0); + } + else + { + uint32_t ackno; + tcp->flags = TCP_RST | TCP_ACK; + tcp_setsequence(tcp->seqno, 0); + ackno = tcp_addsequence(tcp->ackno, acklen); + tcp_setsequence(tcp->ackno, ackno); + } /* Swap port numbers. */