diff mbox series

[RFC,net-next,v2] net: tcp: accept old ack during closing

Message ID 20240112094603.23706-1-menglong8.dong@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [RFC,net-next,v2] net: tcp: accept old ack during closing | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1095 this patch: 1095
netdev/cc_maintainers success CCed 0 of 0 maintainers
netdev/build_clang success Errors and warnings before: 1108 this patch: 1108
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1110 this patch: 1110
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 28 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Menglong Dong Jan. 12, 2024, 9:46 a.m. UTC
For now, the packet with an old ack is not accepted if we are in
FIN_WAIT1 state, which can cause retransmission. Taking the following
case as an example:

    Client                               Server
      |                                    |
  FIN_WAIT1(Send FIN, seq=10)          FIN_WAIT1(Send FIN, seq=20, ack=10)
      |                                    |
      |                                Send ACK(seq=21, ack=11)
   Recv ACK(seq=21, ack=11)
      |
   Recv FIN(seq=20, ack=10)

In the case above, simultaneous close is happening, and the FIN and ACK
packet that send from the server is out of order. Then, the FIN will be
dropped by the client, as it has an old ack. Then, the server has to
retransmit the FIN, which can cause delay if the server has set the
SO_LINGER on the socket.

Old ack is accepted in the ESTABLISHED and TIME_WAIT state, and I think
it should be better to keep the same logic.

In this commit, we accept old ack in FIN_WAIT1/FIN_WAIT2/CLOSING/LAST_ACK
states. Maybe we should limit it to FIN_WAIT1 for now?

Signed-off-by: Menglong Dong <menglong8.dong@gmail.com>
---
v2:
- fix the compiling error
---
 net/ipv4/tcp_input.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

Comments

Simon Horman Jan. 13, 2024, 3:46 p.m. UTC | #1
On Fri, Jan 12, 2024 at 05:46:03PM +0800, Menglong Dong wrote:
> For now, the packet with an old ack is not accepted if we are in
> FIN_WAIT1 state, which can cause retransmission. Taking the following
> case as an example:
> 
>     Client                               Server
>       |                                    |
>   FIN_WAIT1(Send FIN, seq=10)          FIN_WAIT1(Send FIN, seq=20, ack=10)
>       |                                    |
>       |                                Send ACK(seq=21, ack=11)
>    Recv ACK(seq=21, ack=11)
>       |
>    Recv FIN(seq=20, ack=10)
> 
> In the case above, simultaneous close is happening, and the FIN and ACK
> packet that send from the server is out of order. Then, the FIN will be
> dropped by the client, as it has an old ack. Then, the server has to
> retransmit the FIN, which can cause delay if the server has set the
> SO_LINGER on the socket.
> 
> Old ack is accepted in the ESTABLISHED and TIME_WAIT state, and I think
> it should be better to keep the same logic.
> 
> In this commit, we accept old ack in FIN_WAIT1/FIN_WAIT2/CLOSING/LAST_ACK
> states. Maybe we should limit it to FIN_WAIT1 for now?
> 
> Signed-off-by: Menglong Dong <menglong8.dong@gmail.com>
> ---
> v2:
> - fix the compiling error
> ---
>  net/ipv4/tcp_input.c | 18 +++++++++++-------
>  1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index df7b13f0e5e0..70642bb08f3a 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
> @@ -6699,17 +6699,21 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
>  		return 0;
>  
>  	/* step 5: check the ACK field */
> -	acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
> -				      FLAG_UPDATE_TS_RECENT |
> -				      FLAG_NO_CHALLENGE_ACK) > 0;
> +	reason = tcp_ack(sk, skb, FLAG_SLOWPATH |
> +				  FLAG_UPDATE_TS_RECENT |
> +				  FLAG_NO_CHALLENGE_ACK);

Hi Menglong Dong,

Probably I am missing something terribly obvious,
but I am confused about the types used here.

The type of reason is enum skb_drop_reason.
For which, which on my system, the compiler uses an unsigned entity.
i.e. it is an unsigned integer.

But tcp_ack returns a (signed) int. And below reason is checked
for values less than zero, and negated. This doesn't seem right.

>  
> -	if (!acceptable) {
> +	if (reason <= 0) {
>  		if (sk->sk_state == TCP_SYN_RECV)
>  			return 1;	/* send one RST */
> -		tcp_send_challenge_ack(sk);
> -		SKB_DR_SET(reason, TCP_OLD_ACK);
> -		goto discard;
> +		/* accept old ack during closing */
> +		if (reason < 0) {
> +			tcp_send_challenge_ack(sk);
> +			reason = -reason;
> +			goto discard;
> +		}
>  	}
> +	SKB_DR_SET(reason, NOT_SPECIFIED);
>  	switch (sk->sk_state) {
>  	case TCP_SYN_RECV:
>  		tp->delivered++; /* SYN-ACK delivery isn't tracked in tcp_ack */
> -- 
> 2.39.2
>
Menglong Dong Jan. 15, 2024, 2:40 a.m. UTC | #2
On Sat, Jan 13, 2024 at 11:46 PM Simon Horman <horms@kernel.org> wrote:
>
> On Fri, Jan 12, 2024 at 05:46:03PM +0800, Menglong Dong wrote:
> > For now, the packet with an old ack is not accepted if we are in
> > FIN_WAIT1 state, which can cause retransmission. Taking the following
> > case as an example:
> >
> >     Client                               Server
> >       |                                    |
> >   FIN_WAIT1(Send FIN, seq=10)          FIN_WAIT1(Send FIN, seq=20, ack=10)
> >       |                                    |
> >       |                                Send ACK(seq=21, ack=11)
> >    Recv ACK(seq=21, ack=11)
> >       |
> >    Recv FIN(seq=20, ack=10)
> >
> > In the case above, simultaneous close is happening, and the FIN and ACK
> > packet that send from the server is out of order. Then, the FIN will be
> > dropped by the client, as it has an old ack. Then, the server has to
> > retransmit the FIN, which can cause delay if the server has set the
> > SO_LINGER on the socket.
> >
> > Old ack is accepted in the ESTABLISHED and TIME_WAIT state, and I think
> > it should be better to keep the same logic.
> >
> > In this commit, we accept old ack in FIN_WAIT1/FIN_WAIT2/CLOSING/LAST_ACK
> > states. Maybe we should limit it to FIN_WAIT1 for now?
> >
> > Signed-off-by: Menglong Dong <menglong8.dong@gmail.com>
> > ---
> > v2:
> > - fix the compiling error
> > ---
> >  net/ipv4/tcp_input.c | 18 +++++++++++-------
> >  1 file changed, 11 insertions(+), 7 deletions(-)
> >
> > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> > index df7b13f0e5e0..70642bb08f3a 100644
> > --- a/net/ipv4/tcp_input.c
> > +++ b/net/ipv4/tcp_input.c
> > @@ -6699,17 +6699,21 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
> >               return 0;
> >
> >       /* step 5: check the ACK field */
> > -     acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
> > -                                   FLAG_UPDATE_TS_RECENT |
> > -                                   FLAG_NO_CHALLENGE_ACK) > 0;
> > +     reason = tcp_ack(sk, skb, FLAG_SLOWPATH |
> > +                               FLAG_UPDATE_TS_RECENT |
> > +                               FLAG_NO_CHALLENGE_ACK);
>
> Hi Menglong Dong,
>
> Probably I am missing something terribly obvious,
> but I am confused about the types used here.
>
> The type of reason is enum skb_drop_reason.
> For which, which on my system, the compiler uses an unsigned entity.
> i.e. it is an unsigned integer.
>
> But tcp_ack returns a (signed) int. And below reason is checked
> for values less than zero, and negated. This doesn't seem right.
>

Hello! You are right, and it seems that I make the same
mistake with Eric in this commit:

843f77407eeb ("tcp: fix signed/unsigned comparison")

I should convert it to signed int before comparing it
like this:

  if ((int)reason <= 0) {
      ......
      if ((int)reason < 0) {
          ....
      }
  }

Thanks!
Menglong Dong

> >
> > -     if (!acceptable) {
> > +     if (reason <= 0) {
> >               if (sk->sk_state == TCP_SYN_RECV)
> >                       return 1;       /* send one RST */
> > -             tcp_send_challenge_ack(sk);
> > -             SKB_DR_SET(reason, TCP_OLD_ACK);
> > -             goto discard;
> > +             /* accept old ack during closing */
> > +             if (reason < 0) {
> > +                     tcp_send_challenge_ack(sk);
> > +                     reason = -reason;
> > +                     goto discard;
> > +             }
> >       }
> > +     SKB_DR_SET(reason, NOT_SPECIFIED);
> >       switch (sk->sk_state) {
> >       case TCP_SYN_RECV:
> >               tp->delivered++; /* SYN-ACK delivery isn't tracked in tcp_ack */
> > --
> > 2.39.2
> >
Simon Horman Jan. 15, 2024, 11:58 a.m. UTC | #3
On Mon, Jan 15, 2024 at 10:40:56AM +0800, Menglong Dong wrote:
> On Sat, Jan 13, 2024 at 11:46 PM Simon Horman <horms@kernel.org> wrote:
> >
> > On Fri, Jan 12, 2024 at 05:46:03PM +0800, Menglong Dong wrote:
> > > For now, the packet with an old ack is not accepted if we are in
> > > FIN_WAIT1 state, which can cause retransmission. Taking the following
> > > case as an example:
> > >
> > >     Client                               Server
> > >       |                                    |
> > >   FIN_WAIT1(Send FIN, seq=10)          FIN_WAIT1(Send FIN, seq=20, ack=10)
> > >       |                                    |
> > >       |                                Send ACK(seq=21, ack=11)
> > >    Recv ACK(seq=21, ack=11)
> > >       |
> > >    Recv FIN(seq=20, ack=10)
> > >
> > > In the case above, simultaneous close is happening, and the FIN and ACK
> > > packet that send from the server is out of order. Then, the FIN will be
> > > dropped by the client, as it has an old ack. Then, the server has to
> > > retransmit the FIN, which can cause delay if the server has set the
> > > SO_LINGER on the socket.
> > >
> > > Old ack is accepted in the ESTABLISHED and TIME_WAIT state, and I think
> > > it should be better to keep the same logic.
> > >
> > > In this commit, we accept old ack in FIN_WAIT1/FIN_WAIT2/CLOSING/LAST_ACK
> > > states. Maybe we should limit it to FIN_WAIT1 for now?
> > >
> > > Signed-off-by: Menglong Dong <menglong8.dong@gmail.com>
> > > ---
> > > v2:
> > > - fix the compiling error
> > > ---
> > >  net/ipv4/tcp_input.c | 18 +++++++++++-------
> > >  1 file changed, 11 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> > > index df7b13f0e5e0..70642bb08f3a 100644
> > > --- a/net/ipv4/tcp_input.c
> > > +++ b/net/ipv4/tcp_input.c
> > > @@ -6699,17 +6699,21 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
> > >               return 0;
> > >
> > >       /* step 5: check the ACK field */
> > > -     acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
> > > -                                   FLAG_UPDATE_TS_RECENT |
> > > -                                   FLAG_NO_CHALLENGE_ACK) > 0;
> > > +     reason = tcp_ack(sk, skb, FLAG_SLOWPATH |
> > > +                               FLAG_UPDATE_TS_RECENT |
> > > +                               FLAG_NO_CHALLENGE_ACK);
> >
> > Hi Menglong Dong,
> >
> > Probably I am missing something terribly obvious,
> > but I am confused about the types used here.
> >
> > The type of reason is enum skb_drop_reason.
> > For which, which on my system, the compiler uses an unsigned entity.
> > i.e. it is an unsigned integer.
> >
> > But tcp_ack returns a (signed) int. And below reason is checked
> > for values less than zero, and negated. This doesn't seem right.
> >
> 
> Hello! You are right, and it seems that I make the same
> mistake with Eric in this commit:
> 
> 843f77407eeb ("tcp: fix signed/unsigned comparison")
> 
> I should convert it to signed int before comparing it
> like this:
> 
>   if ((int)reason <= 0) {
>       ......
>       if ((int)reason < 0) {
>           ....
>       }
>   }

Thanks. FWIIW, I would probably assign the unsigned value to an unsigned
local variable.

...
diff mbox series

Patch

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index df7b13f0e5e0..70642bb08f3a 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -6699,17 +6699,21 @@  int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
 		return 0;
 
 	/* step 5: check the ACK field */
-	acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
-				      FLAG_UPDATE_TS_RECENT |
-				      FLAG_NO_CHALLENGE_ACK) > 0;
+	reason = tcp_ack(sk, skb, FLAG_SLOWPATH |
+				  FLAG_UPDATE_TS_RECENT |
+				  FLAG_NO_CHALLENGE_ACK);
 
-	if (!acceptable) {
+	if (reason <= 0) {
 		if (sk->sk_state == TCP_SYN_RECV)
 			return 1;	/* send one RST */
-		tcp_send_challenge_ack(sk);
-		SKB_DR_SET(reason, TCP_OLD_ACK);
-		goto discard;
+		/* accept old ack during closing */
+		if (reason < 0) {
+			tcp_send_challenge_ack(sk);
+			reason = -reason;
+			goto discard;
+		}
 	}
+	SKB_DR_SET(reason, NOT_SPECIFIED);
 	switch (sk->sk_state) {
 	case TCP_SYN_RECV:
 		tp->delivered++; /* SYN-ACK delivery isn't tracked in tcp_ack */