Skip to content

Commit 5f92385

Browse files
edumazetkuba-moo
authored andcommitted
tcp: fix __tcp_close() to only send RST when required
If the receive queue contains payload that was already received, __tcp_close() can send an unexpected RST. Refine the code to take tp->copied_seq into account, as we already do in tcp recvmsg(). Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet <edumazet@google.com> Reviewed-by: Neal Cardwell <ncardwell@google.com> Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com> Reviewed-by: Jason Xing <kerneljasonxing@gmail.com> Link: https://patch.msgid.link/20250903084720.1168904-2-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 6977775 commit 5f92385

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

net/ipv4/tcp.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3099,8 +3099,8 @@ bool tcp_check_oom(const struct sock *sk, int shift)
30993099

31003100
void __tcp_close(struct sock *sk, long timeout)
31013101
{
3102+
bool data_was_unread = false;
31023103
struct sk_buff *skb;
3103-
int data_was_unread = 0;
31043104
int state;
31053105

31063106
WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
@@ -3119,11 +3119,12 @@ void __tcp_close(struct sock *sk, long timeout)
31193119
* reader process may not have drained the data yet!
31203120
*/
31213121
while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
3122-
u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq;
3122+
u32 end_seq = TCP_SKB_CB(skb)->end_seq;
31233123

31243124
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
3125-
len--;
3126-
data_was_unread += len;
3125+
end_seq--;
3126+
if (after(end_seq, tcp_sk(sk)->copied_seq))
3127+
data_was_unread = true;
31273128
__kfree_skb(skb);
31283129
}
31293130

0 commit comments

Comments
 (0)