From 424de28a2f5cb2f24e81264a56c2425efb44d42d Mon Sep 17 00:00:00 2001 From: Yaoxing Shan Date: Wed, 23 Apr 2025 06:07:45 +0800 Subject: [PATCH] [tcp] send RST and clear send buffer on abort (#11269) This commit corrects the timing of Transmission Control Block (TCB) re-initialization to ensure proper RST packet sending during TCP connection aborts and to prevent potential issues due to incomplete TCB cleanup. --- src/core/net/tcp6.cpp | 10 +++------- third_party/tcplp/bsdtcp/tcp_subr.c | 15 ++++++++++++++- third_party/tcplp/bsdtcp/tcp_usrreq.c | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/core/net/tcp6.cpp b/src/core/net/tcp6.cpp index c5cd51606..671ad6259 100644 --- a/src/core/net/tcp6.cpp +++ b/src/core/net/tcp6.cpp @@ -1089,13 +1089,9 @@ void tcplp_sys_connection_lost(struct tcpcb *aTcb, uint8_t aErrNum) void tcplp_sys_on_state_change(struct tcpcb *aTcb, int aNewState) { - if (aNewState == TCP6S_CLOSED) - { - /* Re-initialize the TCB. */ - cbuf_pop(&aTcb->recvbuf, cbuf_used_space(&aTcb->recvbuf)); - aTcb->accepted_from = nullptr; - initialize_tcb(aTcb); - } + OT_UNUSED_VARIABLE(aTcb); + OT_UNUSED_VARIABLE(aNewState); + /* Any adaptive changes to the sleep interval would go here. */ } diff --git a/third_party/tcplp/bsdtcp/tcp_subr.c b/third_party/tcplp/bsdtcp/tcp_subr.c index 78f2a5942..46d089675 100644 --- a/third_party/tcplp/bsdtcp/tcp_subr.c +++ b/third_party/tcplp/bsdtcp/tcp_subr.c @@ -49,6 +49,8 @@ #include "tcp_const.h" +static void reinitialize_tcb(struct tcpcb* tp); + /* * samkumar: This is rewritten to have the host network stack to generate the * ISN with appropriate randomness. @@ -145,6 +147,15 @@ void initialize_tcb(struct tcpcb* tp) { tcp_sack_init(tp); } +/* Re-initialize the TCB. */ +static void reinitialize_tcb(struct tcpcb* tp) +{ + uint32_t ntraversed; + lbuf_pop(&tp->sendbuf, lbuf_used_space(&tp->sendbuf), &ntraversed); + cbuf_pop(&tp->recvbuf, cbuf_used_space(&tp->recvbuf)); + tp->accepted_from = NULL; + initialize_tcb(tp); +} /* * samkumar: Most of this function was no longer needed. It did things like @@ -165,6 +176,8 @@ tcp_discardcb(struct tcpcb *tp) CC_ALGO(tp)->cb_destroy(tp->ccv); tcp_free_sackholes(tp); + + reinitialize_tcb(tp); } @@ -352,7 +365,7 @@ struct tcpcb * tcp_drop(struct tcpcb *tp, int errnum) { if (TCPS_HAVERCVDSYN(tp->t_state)) { - tcp_state_change(tp, TCPS_CLOSED); + tcp_state_change(tp, TCP6S_CLOSED); (void) tcplp_output(tp); } if (errnum == ETIMEDOUT && tp->t_softerror) diff --git a/third_party/tcplp/bsdtcp/tcp_usrreq.c b/third_party/tcplp/bsdtcp/tcp_usrreq.c index 079e6da1d..032496921 100644 --- a/third_party/tcplp/bsdtcp/tcp_usrreq.c +++ b/third_party/tcplp/bsdtcp/tcp_usrreq.c @@ -522,7 +522,7 @@ tcp_usr_abort(struct tcpcb* tp) if (tp->t_state != TCP6S_TIME_WAIT && tp->t_state != TCP6S_CLOSED) { tcp_drop(tp, ECONNABORTED); - } else if (tp->t_state == TCPS_TIME_WAIT) { // samkumar: I added this clause + } else if (tp->t_state == TCP6S_TIME_WAIT) { // samkumar: I added this clause tp = tcp_close_tcb(tp); tcplp_sys_connection_lost(tp, CONN_LOST_NORMAL); }