netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <kerneljasonxing@gmail.com>
Cc: <davem@davemloft.net>, <dsahern@kernel.org>,
	<edumazet@google.com>, <horms@kernel.org>,
	<kernelxing@tencent.com>, <kuba@kernel.org>,
	<netdev@vger.kernel.org>, <pabeni@redhat.com>,
	<kuniyu@amazon.com>
Subject: Re: [PATCH net-next] tcp: avoid RST in 3-way shakehands due to failure in tcp_timewait_state_process
Date: Mon, 4 Nov 2024 23:49:16 -0800	[thread overview]
Message-ID: <20241105074916.75107-1-kuniyu@amazon.com> (raw)
In-Reply-To: <20241105025511.42652-1-kerneljasonxing@gmail.com>

From: Jason Xing <kerneljasonxing@gmail.com>
Date: Tue,  5 Nov 2024 10:55:11 +0800
> From: Jason Xing <kernelxing@tencent.com>
> 
> We found there are rare chances that some RST packets appear during
> the shakehands because the timewait socket cannot accept the SYN and

s/shakehands/handshake/

same in the subject.

> doesn't return TCP_TW_SYN in tcp_timewait_state_process().
> 
> Here is how things happen in production:
> Time        Client(A)        Server(B)
> 0s          SYN-->
> ...
> 132s                         <-- FIN
> ...
> 169s        FIN-->
> 169s                         <-- ACK
> 169s        SYN-->
> 169s                         <-- ACK
> 169s        RST-->
> As above picture shows, the two flows have a start time difference
> of 169 seconds. B starts to send FIN so it will finally enter into
> TIMEWAIT state. Nearly at the same time A launches a new connection
> that soon is reset by itself due to receiving a ACK.
> 
> There are two key checks in tcp_timewait_state_process() when timewait
> socket in B receives the SYN packet:
> 1) after(TCP_SKB_CB(skb)->seq, rcv_nxt)
> 2) (s32)(READ_ONCE(tcptw->tw_ts_recent) - tmp_opt.rcv_tsval) < 0)
> 
> Regarding the first rule, it fails as expected because in the first
> connection the seq of SYN sent from A is 1892994276, then 169s have
> passed, the second SYN has 239034613 (caused by overflow of s32).
> 
> Then how about the second rule?
> It fails again!
> Let's take a look at how the tsval comes out:
> __tcp_transmit_skb()
>     -> tcp_syn_options()
>         -> opts->tsval = tcp_skb_timestamp_ts(tp->tcp_usec_ts, skb) + tp->tsoffset;
> The timestamp depends on two things, one is skb->skb_mstamp_ns, the
> other is tp->tsoffset. The latter value is fixed, so we don't need
> to care about it. If both operations (sending FIN and then starting
> sending SYN) from A happen in 1ms, then the tsval would be the same.
> It can be clearly seen in the tcpdump log. Notice that the tsval is
> with millisecond precision.
> 
> Based on the above analysis, I decided to make a small change to
> the check in tcp_timewait_state_process() so that the second flow
> would not fail.
> 
> Signed-off-by: Jason Xing <kernelxing@tencent.com>
> ---
>  net/ipv4/tcp_minisocks.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c
> index bb1fe1ba867a..2b29d1bf5ca0 100644
> --- a/net/ipv4/tcp_minisocks.c
> +++ b/net/ipv4/tcp_minisocks.c
> @@ -234,7 +234,7 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
>  	if (th->syn && !th->rst && !th->ack && !paws_reject &&
>  	    (after(TCP_SKB_CB(skb)->seq, rcv_nxt) ||
>  	     (tmp_opt.saw_tstamp &&
> -	      (s32)(READ_ONCE(tcptw->tw_ts_recent) - tmp_opt.rcv_tsval) < 0))) {

I think this follows RFC 6191 and such a change requires a formal
discussion at IETF.

https://datatracker.ietf.org/doc/html/rfc6191#section-2

---8<---
      *  If TCP Timestamps would be enabled for the new incarnation of
         the connection, and the timestamp contained in the incoming SYN
         segment is greater than the last timestamp seen on the previous
         incarnation of the connection (for that direction of the data
         transfer), honor the connection request (creating a connection
         in the SYN-RECEIVED state).

      *  If TCP Timestamps would be enabled for the new incarnation of
         the connection, the timestamp contained in the incoming SYN
         segment is equal to the last timestamp seen on the previous
         incarnation of the connection (for that direction of the data
         transfer), and the Sequence Number of the incoming SYN segment
         is greater than the last sequence number seen on the previous
         incarnation of the connection (for that direction of the data
         transfer), honor the connection request (creating a connection
         in the SYN-RECEIVED state).
---8<---

> +	      (s32)(READ_ONCE(tcptw->tw_ts_recent) - tmp_opt.rcv_tsval) <= 0))) {
>  		u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
>  		if (isn == 0)
>  			isn++;
> -- 
> 2.37.3

  reply	other threads:[~2024-11-05  7:49 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-05  2:55 [PATCH net-next] tcp: avoid RST in 3-way shakehands due to failure in tcp_timewait_state_process Jason Xing
2024-11-05  7:49 ` Kuniyuki Iwashima [this message]
2024-11-05  9:08   ` Jason Xing
2024-11-05  9:35     ` Eric Dumazet
2024-11-05  9:41       ` Jason Xing
2024-11-05  9:50         ` Eric Dumazet
2024-11-05  9:56           ` Jason Xing
2024-11-05 11:48           ` Jason Xing
2024-11-07  3:16 ` Jason Xing
2024-11-07  4:15   ` Kuniyuki Iwashima
2024-11-07  4:21     ` Kuniyuki Iwashima
2024-11-07  5:23     ` Jason Xing
2024-11-07  5:43       ` Kuniyuki Iwashima
2024-11-07  6:51         ` Jason Xing
2024-11-07  7:11           ` Kuniyuki Iwashima
2024-11-07  7:44             ` Jason Xing
2024-11-07  7:51 ` Philo Lu
2024-11-07  8:01   ` Jason Xing
2024-11-07  8:22     ` Philo Lu
2024-11-07  8:26       ` Jason Xing
2024-11-07  8:37         ` Eric Dumazet
2024-11-07  9:00           ` Jason Xing
2024-11-07  9:16             ` Eric Dumazet
2024-11-07  9:18               ` Jason Xing
2024-11-07  9:30             ` Jason Xing
2024-11-07  9:45               ` Eric Dumazet
2024-11-07  9:48                 ` Eric Dumazet
2024-11-07  9:57                 ` Jason Xing

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241105074916.75107-1-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kerneljasonxing@gmail.com \
    --cc=kernelxing@tencent.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).