Skip to contentSkip to conversation

tcp: Refine TFO client fallback behaviour.

PATCH In review Kuniyuki Iwashima 4 participants 14 messages Oct 16, 2025 – Oct 21, 2025

Subsystems (4)
lore
Share & tools
lore

Kuniyuki Iwashima

author reviewer
#0 lore raw

cover tcp: Refine TFO client fallback behaviour.

Patch 1 & 3 refine weird TFO fallback behaviours that I found while importing TFO client packetdrill tests from google/packetdrill on GitHub.

Patch 2 & 4 import packetdrill tests to cover Patch 1 & 3 scenarios respectively.

For the imported tests, these common changes are applied:

* Add SPDX header
* Adjust path to default.sh
* Adjust sysctl w/ set_sysctls.py
* Use SOCK_NONBLOCK for socket() instead of fcntl()
* Remove unnecessary delay (e.g. +.17 socket(...), etc)

Kuniyuki Iwashima (4):
tcp: Make TFO client fallback behaviour consistent.
selftest: packetdrill: Import TFO sendto tests.
tcp: Don't acknowledge SYN+ACK payload to TFO fallback client.
selftest: packetdrill: Import client_synack-data.pkt.

 net/ipv4/tcp_input.c                          |  17 +-
 net/ipv4/tcp_output.c                         |  39 ++---
 ...en_client_nonblocking-sendto-empty-buf.pkt |  45 ++++++
 ...topen_client_nonblocking-sendto-errnos.pkt | 125 +++++++++++++++
 .../tcp_fastopen_client_synack-data.pkt       | 150 ++++++++++++++++++
 5 files changed, 350 insertions(+), 26 deletions(-)
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_fastopen_client_nonblocking-sendto-empty-buf.pkt
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_fastopen_client_nonblocking-sendto-errnos.pkt
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_fastopen_client_synack-data.pkt
Signature · 1 line
2.51.0.788.g6d19910ace-goog
author reviewer
#1 lore raw

PATCH 1/4 tcp: Make TFO client fallback behaviour consistent.

In tcp_send_syn_data(), the TCP Fast Open client could give up embedding payload into SYN, but the behaviour is inconsistent.

1. Send a bare SYN with TFO request (option w/o cookie)
2. Send a bare SYN with TFO cookie

When the client does not have a valid cookie, a bare SYN is sent with the TFO option without a cookie.

When sendmsg(MSG_FASTOPEN) is called with zero payload and the client has a valid cookie, a bare SYN is sent with the TFO cookie, which is confusing.

This also happens when tcp_wmem_schedule() fails to charge non-zero payload.

OTOH, other fallback paths align with 1. In this case, a TFO request is not strictly needed as tcp_fastopen_cookie_check() has succeeded, but we can use this round to refresh the TFO cookie.

Let's avoid sending TFO cookie w/o payload to make fallback behaviour consistent.

---
 net/ipv4/tcp_output.c | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)
net/ipv4/tcp_output.c +21 -18
index bb3576ac0ad7d..2847c1ffa1615 100644
@@ -4151,6 +4151,9 @@ static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn)
 	if (!tcp_fastopen_cookie_check(sk, &tp->rx_opt.mss_clamp, &fo->cookie))
 		goto fallback;
 
+	if (!fo->size)
+		goto fallback;
+
 	/* MSS for SYN-data is based on cached MSS and bounded by PMTU and
 	 * user-MSS. Reserve maximum option space for middleboxes that add
 	 * private TCP options. The cost is reduced data space in SYN :(
@@ -4164,33 +4167,33 @@ static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn)
 
 	space = min_t(size_t, space, fo->size);
 
-	if (space &&
-	    !skb_page_frag_refill(min_t(size_t, space, PAGE_SIZE),
+	if (!skb_page_frag_refill(min_t(size_t, space, PAGE_SIZE),
 				  pfrag, sk->sk_allocation))
 		goto fallback;
+
 	syn_data = tcp_stream_alloc_skb(sk, sk->sk_allocation, false);
 	if (!syn_data)
 		goto fallback;
+
 	memcpy(syn_data->cb, syn->cb, sizeof(syn->cb));
-	if (space) {
-		space = min_t(size_t, space, pfrag->size - pfrag->offset);
-		space = tcp_wmem_schedule(sk, space);
-	}
-	if (space) {
+
+	space = min_t(size_t, space, pfrag->size - pfrag->offset);
+	space = tcp_wmem_schedule(sk, space);
+	if (space)
 		space = copy_page_from_iter(pfrag->page, pfrag->offset,
 					    space, &fo->data->msg_iter);
-		if (unlikely(!space)) {
-			tcp_skb_tsorted_anchor_cleanup(syn_data);
-			kfree_skb(syn_data);
-			goto fallback;
-		}
-		skb_fill_page_desc(syn_data, 0, pfrag->page,
-				   pfrag->offset, space);
-		page_ref_inc(pfrag->page);
-		pfrag->offset += space;
-		skb_len_add(syn_data, space);
-		skb_zcopy_set(syn_data, fo->uarg, NULL);
+	if (unlikely(!space)) {
+		tcp_skb_tsorted_anchor_cleanup(syn_data);
+		kfree_skb(syn_data);
+		goto fallback;
 	}
+
+	skb_fill_page_desc(syn_data, 0, pfrag->page, pfrag->offset, space);
+	page_ref_inc(pfrag->page);
+	pfrag->offset += space;
+	skb_len_add(syn_data, space);
+	skb_zcopy_set(syn_data, fo->uarg, NULL);
+
 	/* No more data pending in inet_wait_for_connect() */
 	if (space == fo->size)
 		fo->data = NULL;
Signature · 1 line
2.51.0.788.g6d19910ace-goog
#2 lore raw

Re: [PATCH v1 net-next 1/4] tcp: Make TFO client fallback behaviour consistent.

Quoted 17 lines · Kuniyuki Iwashima

In tcp_send_syn_data(), the TCP Fast Open client could give up embedding payload into SYN, but the behaviour is inconsistent.

1. Send a bare SYN with TFO request (option w/o cookie)
2. Send a bare SYN with TFO cookie

When the client does not have a valid cookie, a bare SYN is sent with the TFO option without a cookie.

When sendmsg(MSG_FASTOPEN) is called with zero payload and the client has a valid cookie, a bare SYN is sent with the TFO cookie, which is confusing.

This also happens when tcp_wmem_schedule() fails to charge non-zero payload.

Quoted 8 lines

OTOH, other fallback paths align with 1. In this case, a TFO request is not strictly needed as tcp_fastopen_cookie_check() has succeeded, but we can use this round to refresh the TFO cookie.

Let's avoid sending TFO cookie w/o payload to make fallback behaviour consistent.

I am unsure. Some applications could break ?

They might prime the cookie cache initiating a TCP flow with no payload, so that later at critical times then can save one RTT at their connection establishment.

Quoted 74 lines
---
 net/ipv4/tcp_output.c | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)
net/ipv4/tcp_output.c +21 -19
index bb3576ac0ad7d..2847c1ffa1615 100644
@@ -4151,6 +4151,9 @@ static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn)
        if (!tcp_fastopen_cookie_check(sk, &tp->rx_opt.mss_clamp, &fo->cookie))
                goto fallback;
 
+       if (!fo->size)
+               goto fallback;
+
        /* MSS for SYN-data is based on cached MSS and bounded by PMTU and
         * user-MSS. Reserve maximum option space for middleboxes that add
         * private TCP options. The cost is reduced data space in SYN :(
@@ -4164,33 +4167,33 @@ static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn)
 
        space = min_t(size_t, space, fo->size);
 
-       if (space &&
-           !skb_page_frag_refill(min_t(size_t, space, PAGE_SIZE),
+       if (!skb_page_frag_refill(min_t(size_t, space, PAGE_SIZE),
                                  pfrag, sk->sk_allocation))
                goto fallback;
+
        syn_data = tcp_stream_alloc_skb(sk, sk->sk_allocation, false);
        if (!syn_data)
                goto fallback;
+
        memcpy(syn_data->cb, syn->cb, sizeof(syn->cb));
-       if (space) {
-               space = min_t(size_t, space, pfrag->size - pfrag->offset);
-               space = tcp_wmem_schedule(sk, space);
-       }
-       if (space) {
+
+       space = min_t(size_t, space, pfrag->size - pfrag->offset);
+       space = tcp_wmem_schedule(sk, space);
+       if (space)
                space = copy_page_from_iter(pfrag->page, pfrag->offset,
                                            space, &fo->data->msg_iter);
-               if (unlikely(!space)) {
-                       tcp_skb_tsorted_anchor_cleanup(syn_data);
-                       kfree_skb(syn_data);
-                       goto fallback;
-               }
-               skb_fill_page_desc(syn_data, 0, pfrag->page,
-                                  pfrag->offset, space);
-               page_ref_inc(pfrag->page);
-               pfrag->offset += space;
-               skb_len_add(syn_data, space);
-               skb_zcopy_set(syn_data, fo->uarg, NULL);
+       if (unlikely(!space)) {
+               tcp_skb_tsorted_anchor_cleanup(syn_data);
+               kfree_skb(syn_data);
+               goto fallback;
        }
+
+       skb_fill_page_desc(syn_data, 0, pfrag->page, pfrag->offset, space);
+       page_ref_inc(pfrag->page);
+       pfrag->offset += space;
+       skb_len_add(syn_data, space);
+       skb_zcopy_set(syn_data, fo->uarg, NULL);
+
        /* No more data pending in inet_wait_for_connect() */
        if (space == fo->size)
                fo->data = NULL;
--

2.51.0.788.g6d19910ace-goog

author reviewer
#3 lore raw

Re: [PATCH v1 net-next 1/4] tcp: Make TFO client fallback behaviour consistent.

Quoted 28 lines · Eric Dumazet
Quoted 17 lines · Kuniyuki Iwashima

In tcp_send_syn_data(), the TCP Fast Open client could give up embedding payload into SYN, but the behaviour is inconsistent.

1. Send a bare SYN with TFO request (option w/o cookie)
2. Send a bare SYN with TFO cookie

When the client does not have a valid cookie, a bare SYN is sent with the TFO option without a cookie.

When sendmsg(MSG_FASTOPEN) is called with zero payload and the client has a valid cookie, a bare SYN is sent with the TFO cookie, which is confusing.

This also happens when tcp_wmem_schedule() fails to charge non-zero payload.

Quoted 8 lines

OTOH, other fallback paths align with 1. In this case, a TFO request is not strictly needed as tcp_fastopen_cookie_check() has succeeded, but we can use this round to refresh the TFO cookie.

Let's avoid sending TFO cookie w/o payload to make fallback behaviour consistent.

Quoted 5 lines

I am unsure. Some applications could break ?

They might prime the cookie cache initiating a TCP flow with no payload, so that later at critical times then can save one RTT at their connection establishment.

For that RTT purpose, we send the TFO request in all fallback cases unless the client sets the no cookie option.

I think this is better than sending TFO cookie w/o payload because when a cookie in SYN is valid, we do not generate SYN+ACK w/ a cookie unless the received cookie is the secondary one.

Also, errno is not changed in all paths.

Quoted 75 lines
Quoted 75 lines
---
 net/ipv4/tcp_output.c | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)
net/ipv4/tcp_output.c +21 -19
index bb3576ac0ad7d..2847c1ffa1615 100644
@@ -4151,6 +4151,9 @@ static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn)
        if (!tcp_fastopen_cookie_check(sk, &tp->rx_opt.mss_clamp, &fo->cookie))
                goto fallback;
 
+       if (!fo->size)
+               goto fallback;
+
        /* MSS for SYN-data is based on cached MSS and bounded by PMTU and
         * user-MSS. Reserve maximum option space for middleboxes that add
         * private TCP options. The cost is reduced data space in SYN :(
@@ -4164,33 +4167,33 @@ static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn)
 
        space = min_t(size_t, space, fo->size);
 
-       if (space &&
-           !skb_page_frag_refill(min_t(size_t, space, PAGE_SIZE),
+       if (!skb_page_frag_refill(min_t(size_t, space, PAGE_SIZE),
                                  pfrag, sk->sk_allocation))
                goto fallback;
+
        syn_data = tcp_stream_alloc_skb(sk, sk->sk_allocation, false);
        if (!syn_data)
                goto fallback;
+
        memcpy(syn_data->cb, syn->cb, sizeof(syn->cb));
-       if (space) {
-               space = min_t(size_t, space, pfrag->size - pfrag->offset);
-               space = tcp_wmem_schedule(sk, space);
-       }
-       if (space) {
+
+       space = min_t(size_t, space, pfrag->size - pfrag->offset);
+       space = tcp_wmem_schedule(sk, space);
+       if (space)
                space = copy_page_from_iter(pfrag->page, pfrag->offset,
                                            space, &fo->data->msg_iter);
-               if (unlikely(!space)) {
-                       tcp_skb_tsorted_anchor_cleanup(syn_data);
-                       kfree_skb(syn_data);
-                       goto fallback;
-               }
-               skb_fill_page_desc(syn_data, 0, pfrag->page,
-                                  pfrag->offset, space);
-               page_ref_inc(pfrag->page);
-               pfrag->offset += space;
-               skb_len_add(syn_data, space);
-               skb_zcopy_set(syn_data, fo->uarg, NULL);
+       if (unlikely(!space)) {
+               tcp_skb_tsorted_anchor_cleanup(syn_data);
+               kfree_skb(syn_data);
+               goto fallback;
        }
+
+       skb_fill_page_desc(syn_data, 0, pfrag->page, pfrag->offset, space);
+       page_ref_inc(pfrag->page);
+       pfrag->offset += space;
+       skb_len_add(syn_data, space);
+       skb_zcopy_set(syn_data, fo->uarg, NULL);
+
        /* No more data pending in inet_wait_for_connect() */
        if (space == fo->size)
                fo->data = NULL;
--

2.51.0.788.g6d19910ace-goog

#4 lore raw

Re: [PATCH v1 net-next 1/4] tcp: Make TFO client fallback behaviour consistent.

Quoted 36 lines · Kuniyuki Iwashima
Quoted 28 lines · Eric Dumazet
Quoted 17 lines · Kuniyuki Iwashima

In tcp_send_syn_data(), the TCP Fast Open client could give up embedding payload into SYN, but the behaviour is inconsistent.

1. Send a bare SYN with TFO request (option w/o cookie)
2. Send a bare SYN with TFO cookie

When the client does not have a valid cookie, a bare SYN is sent with the TFO option without a cookie.

When sendmsg(MSG_FASTOPEN) is called with zero payload and the client has a valid cookie, a bare SYN is sent with the TFO cookie, which is confusing.

This also happens when tcp_wmem_schedule() fails to charge non-zero payload.

Quoted 8 lines

OTOH, other fallback paths align with 1. In this case, a TFO request is not strictly needed as tcp_fastopen_cookie_check() has succeeded, but we can use this round to refresh the TFO cookie.

Let's avoid sending TFO cookie w/o payload to make fallback behaviour consistent.

Quoted 5 lines

I am unsure. Some applications could break ?

They might prime the cookie cache initiating a TCP flow with no payload, so that later at critical times then can save one RTT at their connection establishment.

Quoted 8 lines

For that RTT purpose, we send the TFO request in all fallback cases unless the client sets the no cookie option.

I think this is better than sending TFO cookie w/o payload because when a cookie in SYN is valid, we do not generate SYN+ACK w/ a cookie unless the received cookie is the secondary one.

Also, errno is not changed in all paths.

I am not that familiar with TFO code, maybe Yuchung can have some time to review/comment your patches.

#5 lore raw

Re: [PATCH v1 net-next 1/4] tcp: Make TFO client fallback behaviour consistent.

In reply to Eric Dumazet
Quoted 15 lines · Eric Dumazet
Quoted 7 lines · Kuniyuki Iwashima

In tcp_send_syn_data(), the TCP Fast Open client could give up embedding payload into SYN, but the behaviour is inconsistent.

1. Send a bare SYN with TFO request (option w/o cookie)
2. Send a bare SYN with TFO cookie

Quoted 6 lines

When the client does not have a valid cookie, a bare SYN is sent with the TFO option without a cookie.

When sendmsg(MSG_FASTOPEN) is called with zero payload and the client has a valid cookie, a bare SYN is sent with the TFO cookie, which is confusing.

Quoted 5 lines

I am unsure. Some applications could break ?

They might prime the cookie cache initiating a TCP flow with no payload, so that later at critical times then can save one RTT at their connection establishment.

In addition, a client doing this (SYN with cookie but no data) is granting permission for the server to respond with data on the SYN,ACK (before 3rd-ACK).

Signature · 2 lines
Cheers,
  Jeremy
author reviewer
#6 lore raw

Re: [PATCH v1 net-next 1/4] tcp: Make TFO client fallback behaviour consistent.

Quoted 19 lines · Jeremy Harris
Quoted 17 lines · Eric Dumazet
Quoted 7 lines · Kuniyuki Iwashima

In tcp_send_syn_data(), the TCP Fast Open client could give up embedding payload into SYN, but the behaviour is inconsistent.

1. Send a bare SYN with TFO request (option w/o cookie)
2. Send a bare SYN with TFO cookie

Quoted 6 lines

When the client does not have a valid cookie, a bare SYN is sent with the TFO option without a cookie.

When sendmsg(MSG_FASTOPEN) is called with zero payload and the client has a valid cookie, a bare SYN is sent with the TFO cookie, which is confusing.

I am unsure. Some applications could break ?

Quoted 7 lines
Quoted 3 lines

They might prime the cookie cache initiating a TCP flow with no payload, so that later at critical times then can save one RTT at their connection establishment.

In addition, a client doing this (SYN with cookie but no data) is granting permission for the server to respond with data on the SYN,ACK (before 3rd-ACK).

As I quoted in patch 2, the server should not respond as such for SYN without payload.

https://datatracker.ietf.org/doc/html/rfc7413#section-3
---8<---
Performing TCP Fast Open:

1. The client sends a SYN with data and the cookie in the Fast Open option.

2. The server validates the cookie:
...
3. If the server accepts the data in the SYN packet, it may send the response data before the handshake finishes.
---8<---

#7 lore raw

Re: [PATCH v1 net-next 1/4] tcp: Make TFO client fallback behaviour consistent.

Quoted 11 lines · Kuniyuki Iwashima
Quoted 3 lines

In addition, a client doing this (SYN with cookie but no data) is granting permission for the server to respond with data on the SYN,ACK (before 3rd-ACK).

As I quoted in patch 2, the server should not respond as such for SYN without payload.

https://datatracker.ietf.org/doc/html/rfc7413#section-3
---8<---
Performing TCP Fast Open:

Quoted 8 lines

1. The client sends a SYN with data and the cookie in the Fast Open option.

2. The server validates the cookie:
...
3. If the server accepts the data in the SYN packet, it may send the response data before the handshake finishes.
---8<---

In language lawyer terms, that (item 3 above) is a permission. It does not restrict from doing other things. In particular, there are no RFC 2119 key words (MUST NOT, SHOULD etc).

I argue that once the server has validated a TFO cookie from the client, it is safe to send data to the client; the connection is effectively open.

For traditional, non-TFO, connections the wait for the 3rd-ACK is required to be certain that the IP of the alleged client, given in the SYN packet, was not spoofed by a 3rd-party. For TFO that certainty is given by the cookie; the server can conclude that it has previously conversed with the source IP of the SYN.

Alternately, one could read "the data" in that item 3 as including "zero length data"; the important part being accepting it.

Signature · 2 lines
Cheers,
  Jeremy
author reviewer
#8 lore raw

Re: [PATCH v1 net-next 1/4] tcp: Make TFO client fallback behaviour consistent.

From: Jeremy Harris <[email protected]>
Date: Sat, 18 Oct 2025 22:17:12 +0100

Quoted 36 lines · Kuniyuki Iwashima
Quoted 11 lines
Quoted 3 lines

In addition, a client doing this (SYN with cookie but no data) is granting permission for the server to respond with data on the SYN,ACK (before 3rd-ACK).

As I quoted in patch 2, the server should not respond as such for SYN without payload.

https://datatracker.ietf.org/doc/html/rfc7413#section-3
---8<---
Performing TCP Fast Open:

Quoted 8 lines

1. The client sends a SYN with data and the cookie in the Fast Open option.

2. The server validates the cookie:
...
3. If the server accepts the data in the SYN packet, it may send the response data before the handshake finishes.
---8<---

In language lawyer terms, that (item 3 above) is a permission. It does not restrict from doing other things. In particular, there are no RFC 2119 key words (MUST NOT, SHOULD etc).

I argue that once the server has validated a TFO cookie from the client, it is safe to send data to the client; the connection is effectively open.

For traditional, non-TFO, connections the wait for the 3rd-ACK is required to be certain that the IP of the alleged client, given in the SYN packet, was not spoofed by a 3rd-party. For TFO that certainty is given by the cookie; the server can conclude that it has previously conversed with the source IP of the SYN.

Quoted 2 lines

Alternately, one could read "the data" in that item 3 as including "zero length data"; the important part being accepting it.

Actually, even FreeBSD does not read like that, and it delays SYN+ACK only when SYN has "non-zero length data" (tlen > 0) in tcp_do_segment().

---8<---

	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
	    (tp->t_flags & TF_FASTOPEN));
	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
...
			if (DELAY_ACK(tp, tlen) || tfo_syn)
				tp->t_flags |= TF_DELACK;
			else
				tp->t_flags |= TF_ACKNOW;
			tp->rcv_nxt += tlen;

---8<---

#9 lore raw

Re: [PATCH v1 net-next 1/4] tcp: Make TFO client fallback behaviour consistent.

In reply to Jeremy Harris
Quoted 22 lines · Jeremy Harris
Quoted 11 lines · Kuniyuki Iwashima
Quoted 3 lines

In addition, a client doing this (SYN with cookie but no data) is granting permission for the server to respond with data on the SYN,ACK (before 3rd-ACK).

As I quoted in patch 2, the server should not respond as such for SYN without payload.

https://datatracker.ietf.org/doc/html/rfc7413#section-3
---8<---
Performing TCP Fast Open:

Quoted 8 lines

1. The client sends a SYN with data and the cookie in the Fast Open option.

2. The server validates the cookie:
...
3. If the server accepts the data in the SYN packet, it may send the response data before the handshake finishes.
---8<---

Quoted 7 lines

In language lawyer terms, that (item 3 above) is a permission. It does not restrict from doing other things. In particular, there are no RFC 2119 key words (MUST NOT, SHOULD etc).

I argue that once the server has validated a TFO cookie from the client, it is safe to send data to the client; the connection is effectively open.

Thanks for the patch. But indeed this was the intentional design (i.e. empty MSG_FASTOPEN call triggers server immediate accept and send before final ACK in 3WHS). It's allowing more application scenarios for TFO. Now some applications may have taken advantage of this design so this patch set may break them.

But the RFC could be more specific about this edge case so revising the RFC as an errata?

Quoted 13 lines

For traditional, non-TFO, connections the wait for the 3rd-ACK is required to be certain that the IP of the alleged client, given in the SYN packet, was not spoofed by a 3rd-party. For TFO that certainty is given by the cookie; the server can conclude that it has previously conversed with the source IP of the SYN.

Alternately, one could read "the data" in that item 3 as including "zero length data"; the important part being accepting it.
--
Cheers,
Jeremy

author reviewer
#10 lore raw

Re: [PATCH v1 net-next 1/4] tcp: Make TFO client fallback behaviour consistent.

Quoted 29 lines · Yuchung Cheng
Quoted 27 lines · Jeremy Harris
Quoted 11 lines · Kuniyuki Iwashima
Quoted 3 lines

In addition, a client doing this (SYN with cookie but no data) is granting permission for the server to respond with data on the SYN,ACK (before 3rd-ACK).

As I quoted in patch 2, the server should not respond as such for SYN without payload.

https://datatracker.ietf.org/doc/html/rfc7413#section-3
---8<---
Performing TCP Fast Open:

Quoted 8 lines

1. The client sends a SYN with data and the cookie in the Fast Open option.

2. The server validates the cookie:
...
3. If the server accepts the data in the SYN packet, it may send the response data before the handshake finishes.
---8<---

In language lawyer terms, that (item 3 above) is a permission. It does not restrict from doing other things. In particular, there are no RFC 2119 key words (MUST NOT, SHOULD etc).

Quoted 8 lines
Quoted 2 lines

I argue that once the server has validated a TFO cookie from the client, it is safe to send data to the client; the connection is effectively open.

Thanks for the patch. But indeed this was the intentional design (i.e. empty MSG_FASTOPEN call triggers server immediate accept and send before final ACK in 3WHS). It's allowing more application scenarios for TFO. Now some applications may have taken advantage of this design so this patch set may break them.

I see. I'll leave the 0-byte send as is and make the error path of tcp_wmem_schedule() align with others.

What do you think about the edge case in patch 2 description ? Actually, this patch was a prep to detect the case easily.

The script could be valid if the server set the no cookie option, but it cannot be detected on the client side. (and no cookie feature is not an official feature in RFC)

Quoted 3 lines

But the RFC could be more specific about this edge case so revising the RFC as an errata?

Sure, I can post an errata like

3. If the server accepts the SYN packet with a valid TFO cookie, it may send the response data before the handshake finishes.

Quoted 14 lines
Quoted 14 lines

For traditional, non-TFO, connections the wait for the 3rd-ACK is required to be certain that the IP of the alleged client, given in the SYN packet, was not spoofed by a 3rd-party. For TFO that certainty is given by the cookie; the server can conclude that it has previously conversed with the source IP of the SYN.

Alternately, one could read "the data" in that item 3 as including "zero length data"; the important part being accepting it.
--
Cheers,
Jeremy

author reviewer
#11 lore raw

PATCH 2/4 selftest: packetdrill: Import TFO sendto tests.

In reply to Kuniyuki Iwashima

These two TFO client tests are imported from google/packetdrill on GitHub:

* client_nonblocking-sendto-errnos.pkt
* nonblocking-sendto-empty-buf.pkt

Both files had tests for 0 payload sendto(MSG_FASTOPEN) assuming that SYN will be sent w/ TFO cookie if cached,

+0 sendto(4, ..., 0, MSG_FASTOPEN, ..., ...) = -1 EINPROGRESS (Operation is now in progress) +0 > S 0:0(0) <mss 1460,nop,nop,sackOK,nop,wscale 8,FO 112233445566>

and now it fails with the previous patch, so this part is fixed up in both files.

In addition, the former had lengthy 6s wait in the last test case to ensure that there will not be multiple SYN retransmissions, and I changed it to 2s because now linear timeout is enabled by default (it can be reduced further to 1s, but 2s just in case to avoid flakiness).

---
 ...en_client_nonblocking-sendto-empty-buf.pkt |  45 +++++++
 ...topen_client_nonblocking-sendto-errnos.pkt | 125 ++++++++++++++++++
 2 files changed, 170 insertions(+)
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_fastopen_client_nonblocking-sendto-empty-buf.pkt
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_fastopen_client_nonblocking-sendto-errnos.pkt
tools/testing/selftests/net/packetdrill/tcp_fastopen_client_nonblocking-sendto-empty-buf.pkt new file +45 -0
new file mode 100644
index 0000000000000..6ebeff7c5857e
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// Non-blocking Fast Open with an empty buffer
+//
+`./defaults.sh
+ ./set_sysctls.py /proc/sys/net/ipv4/tcp_timestamps=0`
+
+    0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
+   +0 sendto(3, ..., 0, MSG_FASTOPEN, ..., ...) = -1 EINPROGRESS (Operation is now in progress)
+   +0 > S 0:0(0) <mss 1460,nop,nop,sackOK,nop,wscale 8,FO,nop,nop>
+   +0 < S. 123:123(0) ack 1 win 14600 <mss 1460,nop,nop,sackOK,nop,wscale 6,FO abcd1234,nop,nop>
+   +0 > . 1:1(0) ack 1
+   +0 close(3) = 0
+   +0 > F. 1:1(0) ack 1
+   +0 < F. 1:1(0) ack 2 win 92
+   +0 > .  2:2(0) ack 2
+
+
+//
+// Test: non-blocking sendto() of 0B
+//
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 4
+   +0 sendto(4, ..., 0, MSG_FASTOPEN, ..., ...) = -1 EINPROGRESS (Operation is now in progress)
+
+// TFO cookie (FO abcd1234) is not sent w/o payload
+   +0 > S 0:0(0) <mss 1460,nop,nop,sackOK,nop,wscale 8,FO,nop,nop>
+
+// Server acks FO and replies a different MSS (940B)
+   +0 < S. 1234:1234(0) ack 1 win 14600 <mss 940,nop,nop,sackOK,nop,wscale 6,FO 12345678,nop,nop>
+   +0 > . 1:1(0) ack 1
+   +0 %{ assert (tcpi_options & TCPI_OPT_SYN_DATA) == 0, tcpi_options }%
+
+
+//
+// Test: previous server's MSS (940B) and cookie are still cached
+//
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 5
+   +0 sendto(5, ..., 2000, MSG_FASTOPEN, ..., ...) = 900
+   +0 > S 0:900(900) <mss 1460,nop,nop,sackOK,nop,wscale 8,FO 12345678,nop,nop>
+// Sever acknowledges the data but also sends new cookie
+   +0 < S. 5678:5678(0) ack 901 win 14600 <mss 1460,nop,nop,sackOK,nop,wscale 6,FO 000000000000>
+   +0 > . 901:901(0) ack 1
+   +0 %{ assert (tcpi_options & TCPI_OPT_SYN_DATA) != 0, tcpi_options }%
+
+`/tmp/sysctl_restore_${PPID}.sh`
tools/testing/selftests/net/packetdrill/tcp_fastopen_client_nonblocking-sendto-errnos.pkt new file +125 -0

125 lines not on this page · raw message

Signature · 1 line
2.51.0.788.g6d19910ace-goog
author reviewer
#12 lore raw

PATCH 3/4 tcp: Don't acknowledge SYN+ACK payload to TFO fallback client.

In reply to Kuniyuki Iwashima

Currently, SYN+ACK payload is acknowledged and queued even for a TFO fallback client, which did not send SYN with payload.

For example, this packetdrill script does not fail, even though the server does not send a TFO cookie.

0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
+0 sendto(3, ..., 0, MSG_FASTOPEN, ..., ...) = -1 EINPROGRESS (Operation is now in progress) +0 > S 0:0(0) <mss 1460,nop,nop,sackOK,nop,wscale 8,FO,nop,nop>
+0 < S. 0:1000(1000) ack 1 win 5840 <mss 1040,nop,nop,sackOK,nop,wscale 6>
+0 > . 1:1(0) ack 1001 // should be ack 1
+0 read(3, ..., 1000) = 1000 // should fail with -EAGAIN

This is because tcp_rcv_fastopen_synack(), which handles SYN+ACK for both TFO client and TFO fallback client, calls tcp_fastopen_add_skb() unconditionally.

RFC 7413 (TCP Fast Open), in Section 3. Protocol Overview [0], states that the SYN+ACK payload is only allowed when the server acknowledges SYN data:

3. If the server accepts the data in the SYN packet, it may send the response data before the handshake finishes.

Let's not call tcp_fastopen_add_skb() when the client did not send SYN with payload.

Note that Linux does not send SYN+ACK with payload but FreeBSD could as mentioned in the commit below. [1]

---
 net/ipv4/tcp_input.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
net/ipv4/tcp_input.c +9 -8
index 8fc97f4d8a6b2..e1d3066782b57 100644
@@ -6552,6 +6552,9 @@ static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
 
 	tcp_fastopen_cache_set(sk, mss, cookie, syn_drop, try_exp);
 
+	if (!tp->syn_data)
+		return false;
+
 	if (data) { /* Retransmit unacked data in SYN */
 		if (tp->total_retrans)
 			tp->fastopen_client_fail = TFO_SYN_RETRANSMITTED;
@@ -6564,16 +6567,14 @@ static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
 				LINUX_MIB_TCPFASTOPENACTIVEFAIL);
 		return true;
 	}
-	tp->syn_data_acked = tp->syn_data;
-	if (tp->syn_data_acked) {
-		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVE);
-		/* SYN-data is counted as two separate packets in tcp_ack() */
-		if (tp->delivered > 1)
-			--tp->delivered;
-	}
 
-	tcp_fastopen_add_skb(sk, synack);
+	/* SYN-data is counted as two separate packets in tcp_ack() */
+	if (tp->delivered > 1)
+		--tp->delivered;
 
+	tp->syn_data_acked = 1;
+	tcp_fastopen_add_skb(sk, synack);
+	NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVE);
 	return false;
 }
 
Signature · 1 line
2.51.0.788.g6d19910ace-goog
author reviewer
#13 lore raw

PATCH 4/4 selftest: packetdrill: Import client_synack-data.pkt.

In reply to Kuniyuki Iwashima

client_synack-data.pkt tests various TFO client scenarios for SYN+ACK payload processing, which never happen with Linux server.

In addition to the common changes mentioned in the cover letter, the following changes are added to the original script:

1. Add payload to SYN+ACK for TFO fallback client to cover the previous patch

2. Add TCPI_OPT_SYN_DATA assertion in each test case

3. Add TcpExtPAWSActive check in the last test case

---
 .../tcp_fastopen_client_synack-data.pkt       | 150 ++++++++++++++++++
 1 file changed, 150 insertions(+)
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_fastopen_client_synack-data.pkt
tools/testing/selftests/net/packetdrill/tcp_fastopen_client_synack-data.pkt new file +150 -0
new file mode 100644
index 0000000000000..c49cfd3d491e5
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// Test server sending SYNACK with data
+//
+--tcp_ts_ecr_scaled // used in TEST 5
+
+`./defaults.sh
+ ./set_sysctls.py /proc/sys/net/ipv4/tcp_timestamps=0`
+
+
+//
+// Cache warmup: send a Fast Open cookie request
+// SYN-ACK payload must not be ACKed
+//
+    0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 3
+   +0 sendto(3, ..., 0, MSG_FASTOPEN, ..., ...) = -1 EINPROGRESS (Operation is now in progress)
+   +0 > S 0:0(0) <mss 1460,nop,nop,sackOK,nop,wscale 8,FO,nop,nop>
+   +0 < S. 123:133(10) ack 1 win 5840 <mss 1040,nop,nop,sackOK,nop,wscale 6,FO abcd1234,nop,nop>
+// SYN+ACK data cannot be ACKed for TFO fallback client
+   +0 > . 1:1(0) ack 1
+
+   +0 %{ assert (tcpi_options & TCPI_OPT_SYN_DATA) == 0, tcpi_options }%
+   +0 read(3, ..., 1000) = -1 EAGAIN (Resource temporarily unavailable)
+   +0 close(3) = 0
+   +0 > F. 1:1(0) ack 1
+ +.01 < F. 1:1(0) ack 2 win 92
+   +0 > .  2:2(0) ack 2
+
+
+//
+// TEST1: Servers sends SYN-ACK with data and another two data packets
+//
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 4
+   +0 sendto(4, ..., 1000, MSG_FASTOPEN, ..., ...) = 1000
+   +0 > S 0:1000(1000) <mss 1460,nop,nop,sackOK,nop,wscale 8,FO abcd1234,nop,nop>
+   +0 < S. 1000000:1001400(1400) ack 1001 win 5840 <mss 1040,nop,nop,sackOK,nop,wscale 6>
+   +0 < . 1401:2801(1400) ack 1001 win 257
+   +0 < P. 2801:3001(200) ack 1001 win 257
+   +0 > . 1001:1001(0) ack 1401
+   +0 > . 1001:1001(0) ack 2801
+   +0 > . 1001:1001(0) ack 3001
+
+   +0 %{ assert (tcpi_options & TCPI_OPT_SYN_DATA) != 0, tcpi_options }%
+   +0 read(4, ..., 100000) = 3000
+   +0 close(4) = 0
+   +0 > F. 1001:1001(0) ack 3001
+ +.01 < F. 3001:3001(0) ack 1002 win 257
+   +0 > . 1002:1002(0) ack 3002
+
+
+//
+// TEST2: SYN-ACK-DATA-FIN is accepted. state SYN_SENT -> CLOSE_WAIT.
+//        poll() functions correctly.
+//
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 4
+   +0 sendto(4, ..., 1000, MSG_FASTOPEN, ..., ...) = 1000
+   +0...0.010 poll([{fd=4,
+                     events=POLLIN|POLLOUT|POLLERR|POLLRDHUP,
+                     revents=POLLIN|POLLOUT|POLLRDHUP}], 1, 100) = 1
+   +0 > S 0:1000(1000) <mss 1460,nop,nop,sackOK,nop,wscale 8,FO abcd1234,nop,nop>
+ +.01 < SF. 1000000:1001400(1400) ack 1001 win 5840 <mss 1040,nop,nop,sackOK,nop,wscale 6>
+
+   +0 %{ assert tcpi_state == TCP_CLOSE_WAIT, tcpi_state }%
+   +0 %{ assert (tcpi_options & TCPI_OPT_SYN_DATA) != 0, tcpi_options }%
+   +0 read(4, ..., 100000) = 1400
+   +0 read(4, ..., 100000) = 0
+   +0 > . 1001:1001(0) ack 1402
+   +0 close(4) = 0
+   +0 > F. 1001:1001(0) ack 1402
+ +.01 < . 1402:1402(0) ack 1002 win 257
+
+
+//
+// TEST3: Servers sends SYN-ACK with data and another two data packets. SYN-ACK
+//        is lost and the two data packets are ignored. Client timed out and
+//        retransmitted SYN.
+//
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 4
+   +0 sendto(4, ..., 1000, MSG_FASTOPEN, ..., ...) = 1000
+   +0 > S 0:1000(1000) <mss 1460,nop,nop,sackOK,nop,wscale 8,FO abcd1234,nop,nop>
+ +.01 < . 1401:2801(1400) ack 1001 win 257
+   +0 < P. 2801:3001(200) ack 1001 win 257
+
+// SYN timeout
+ +.99~+1.1 > S 0:0(0) <mss 1460,nop,nop,sackOK,nop,wscale 8>
+ +.01 < S. 1000000:1001400(1400) ack 1001 win 5840 <mss 1040,nop,nop,sackOK,nop,wscale 6>
+   +0 > . 1001:1001(0) ack 1401
+ +.01 < . 1401:2801(1400) ack 1001 win 257
+   +0 > . 1001:1001(0) ack 2801
+   +0 < P. 2801:3001(200) ack 1001 win 257
+   +0 > . 1001:1001(0) ack 3001
+
+   +0 %{ assert (tcpi_options & TCPI_OPT_SYN_DATA) != 0, tcpi_options }%
+   +0 read(4, ..., 100000) = 3000
+   +0 close(4) = 0
+   +0 > F. 1001:1001(0) ack 3001
+  +.1 < F. 3001:3001(0) ack 1002 win 257
+   +0 > . 1002:1002(0) ack 3002
+
+
+//
+// TEST4: SYN-ACK-DATA with TS opt. Also test poll()
+//
+   +0 `sysctl -q net.ipv4.tcp_timestamps=1`
+
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 4
+   +0 sendto(4, ..., 1000, MSG_FASTOPEN, ..., ...) = 1000
+   +0...0.010 poll([{fd=4,
+                     events=POLLIN|POLLOUT|POLLERR,
+                     revents=POLLIN|POLLOUT}], 1, 100) = 1
+   +0 > S 0:1000(1000) <mss 1460,sackOK,TS val 1 ecr 0,nop,wscale 8,FO abcd1234,nop,nop>
+ +.01 < S. 1000000:1001400(1400) ack 1001 win 5840 <mss 1040,TS val 1000000 ecr 1,sackOK,nop,wscale 6>
+   +0 > . 1001:1001(0) ack 1401 <nop,nop,TS val 101 ecr 1000000>
+   +0 < . 1401:2801(1400) ack 1001 win 257 <nop,nop,TS val 1000000 ecr 1>
+   +0 > . 1001:1001(0) ack 2801 <nop,nop,TS val 101 ecr 1000000>
+   +0 < P. 2801:3001(200) ack 1001 win 257 <nop,nop,TS val 1000000 ecr 1>
+   +0 > . 1001:1001(0) ack 3001 <nop,nop,TS val 101 ecr 1000000>
+
+   +0 %{ assert (tcpi_options & TCPI_OPT_SYN_DATA) != 0, tcpi_options }%
+   +0 read(4, ..., 100000) = 3000
+   +0 close(4) = 0
+   +0 > F. 1001:1001(0) ack 3001 <nop,nop,TS val 301 ecr 1000000>
+ +.01 < F. 3001:3001(0) ack 1002 win 257 <nop,nop,TS val 1000300 ecr 301>
+   +0 > . 1002:1002(0) ack 3002 <nop,nop,TS val 401 ecr 1000300>
+
+
+//
+// TEST5: SYN-ACK-DATA with bad TS opt is repelled with an RST.
+//
+   +0 `nstat > /dev/null`
+   +0 socket(..., SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP) = 4
+   +0 sendto(4, ..., 1000, MSG_FASTOPEN, ..., ...) = 1000
+   +0 > S 0:1000(1000) <mss 1460,sackOK,TS val 1 ecr 0,nop,wscale 8,FO abcd1234,nop,nop>
+
+// bad ECR value is rejected as LINUX_MIB_PAWSACTIVEREJECTED
+ +.01 < S. 1000000:1001400(1400) ack 1001 win 5840 <mss 1040,TS val 1000000 ecr 9999,sackOK,nop,wscale 6>
+   +0 > R 1001:1001(0)
+
+// A later valid SYN establishes the connection
+ +.01 < S. 1000000:1000100(100) ack 1001 win 5840 <mss 1040,TS val 1000000 ecr 1,sackOK,nop,wscale 6>
+   +0 > . 1001:1001(0) ack 101 <nop,nop,TS val 21 ecr 1000000>
+
+// Make sure the RST above incremented LINUX_MIB_PAWSACTIVEREJECTED
+   +0 `nstat | grep -q TcpExtPAWSActive`
+
+   +0 %{ assert (tcpi_options & TCPI_OPT_SYN_DATA) != 0, tcpi_options }%
+   +0 read(4, ..., 100000) = 100
+   +0 %{ assert tcpi_state == TCP_ESTABLISHED, tcpi_state }%
+
+`/tmp/sysctl_restore_${PPID}.sh`
\ No newline at end of file
Signature · 1 line
2.51.0.788.g6d19910ace-goog