From nobody Sun Mar 22 10:12:44 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B165836212B; Mon, 9 Mar 2026 08:03:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773043426; cv=none; b=t6MDNdtrbdpJScmSXWhSESA93gIm1hsb1iX/FXuIJtD/fUog0ChpkdoZUNM1NGYHm3GgFH47ReOjU3X3JtnxsXYby2gwhbnGeTZcbD0e7357MEUDvOfAJchgpMi20IDmhLI+5sKjLKFaKdC5JejMfH59qBjIOcLhBjH6466W/yQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773043426; c=relaxed/simple; bh=6lpGQhvEHoauKsBpF2oCmP3IaU50PTmtKr43V5GFemI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DeF/1WnLKmbPRuVhW0r5NPA7MRy3gzkakNrKpycBuDnGcs7OL8stFodKHaVTbF8uvvO5Jd8BQIwdIH9kDy8ao3Wdb5qMhKEv8M4dmd6EzSaUxrDTYJcSGo+ftx+SexgkZnOJ3UR21qM9S1h3ldOcHq4YzxaoOsjfyZcJUznf8S8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=A2F3SSC+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="A2F3SSC+" Received: by smtp.kernel.org (Postfix) with ESMTPS id 5F219C19423; Mon, 9 Mar 2026 08:03:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773043426; bh=6lpGQhvEHoauKsBpF2oCmP3IaU50PTmtKr43V5GFemI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=A2F3SSC+psc+/q4F013fuYWvEsxH+vn35ViMvHIxlHOJ3jxtkMDZw97V1SgLeoJ0D PvlmT0Muzo9KGOzL/w05wIqqcwrya1VqcMMerMv4EjDjr23WD18wfkWr2CRs3RFIgo NBy7QszSuBpTfzrzpBgwXnqobeU81oGHtSfw0JA0LDTR5nPVaGHzDZQE/sVGH/H8U5 MDELMpYhDkoKRIhXLBe+S3EjZwEl9CXzPuDhSZheEXrhw/zYOk0JQ1DM7JbV7j5I1N AOoMsYSqs8hL50G7dQ5lzbn6+Z3jtXQGwotDT5+n24VH5DzNkf3tIIZTCm7eeSMnx/ DfEqNEbh1ByBw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D0E1EF3706; Mon, 9 Mar 2026 08:03:46 +0000 (UTC) From: Simon Baatz via B4 Relay Date: Mon, 09 Mar 2026 09:02:26 +0100 Subject: [PATCH net-next v3 1/6] tcp: implement RFC 7323 window retraction receiver requirements Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260309-tcp_rfc7323_retract_wnd_rfc-v3-1-4c7f96b1ec69@gmail.com> References: <20260309-tcp_rfc7323_retract_wnd_rfc-v3-0-4c7f96b1ec69@gmail.com> In-Reply-To: <20260309-tcp_rfc7323_retract_wnd_rfc-v3-0-4c7f96b1ec69@gmail.com> To: Eric Dumazet , Neal Cardwell , Kuniyuki Iwashima , "David S. Miller" , Jakub Kicinski , Paolo Abeni , Simon Horman , Jonathan Corbet , Shuah Khan , David Ahern , Jon Maloy , Jason Xing , mfreemon@cloudflare.com, Shuah Khan , Stefano Brivio , Matthieu Baerts , Mat Martineau , Geliang Tang Cc: netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, mptcp@lists.linux.dev, Simon Baatz X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1773043425; l=11695; i=gmbnomis@gmail.com; s=20260220; h=from:subject:message-id; bh=9E8zCE5nmQbTZ9o2NygPjxIomKughsAZvjNUYtJajfw=; b=V8tdU1ByKFW0d7c32JNgV3KR3ohZisAHDtPmNZ0FbMNAZI0ZP5ipHEi71GROA7dyV2dvT5cMW 1iu9BHemoNjDvZ0LWEx34wL4+1Hwzsl0TVCKUWsJfKmf8wZLEcmXrEs X-Developer-Key: i=gmbnomis@gmail.com; a=ed25519; pk=T/JIz/6F5bf1uQJr69lmyi7czVG+F9TVZ/8x5z9Wtqw= X-Endpoint-Received: by B4 Relay for gmbnomis@gmail.com/20260220 with auth_id=641 X-Original-From: Simon Baatz Reply-To: gmbnomis@gmail.com From: Simon Baatz By default, the Linux TCP implementation does not shrink the advertised window (RFC 7323 calls this "window retraction") with the following exceptions: - When an incoming segment cannot be added due to the receive buffer running out of memory. Since commit 8c670bdfa58e ("tcp: correct handling of extreme memory squeeze") a zero window will be advertised in this case. It turns out that reaching the required memory pressure is easy when window scaling is in use. In the simplest case, sending a sufficient number of segments smaller than the scale factor to a receiver that does not read data is enough. - Commit b650d953cd39 ("tcp: enforce receive buffer memory limits by allowing the tcp window to shrink") addressed the "eating memory" problem by introducing a sysctl knob that allows shrinking the window before running out of memory. However, RFC 7323 does not only state that shrinking the window is necessary in some cases, it also formulates requirements for TCP implementations when doing so (Section 2.4). This commit addresses the receiver-side requirements: After retracting the window, the peer may have a snd_nxt that lies within a previously advertised window but is now beyond the retracted window. This means that all incoming segments (including pure ACKs) will be rejected until the application happens to read enough data to let the peer's snd_nxt be in window again (which may be never). To comply with RFC 7323, the receiver MUST honor any segment that would have been in window for any ACK sent by the receiver and, when window scaling is in effect, SHOULD track the maximum window sequence number it has advertised. This patch tracks that maximum window sequence number rcv_mwnd_seq throughout the connection and uses it in tcp_sequence() when deciding whether a segment is acceptable. rcv_mwnd_seq is updated together with rcv_wup and rcv_wnd in tcp_select_window(). If we count tcp_sequence() as fast path, it is read in the fast path. Therefore, rcv_mwnd_seq is put into rcv_wnd's cacheline group. The logic for handling received data in tcp_data_queue() is already sufficient and does not need to be updated. Signed-off-by: Simon Baatz Reviewed-by: Eric Dumazet --- .../networking/net_cachelines/tcp_sock.rst | 1 + include/linux/tcp.h | 3 +++ include/net/tcp.h | 22 ++++++++++++++++++= ++++ net/ipv4/tcp.c | 2 ++ net/ipv4/tcp_fastopen.c | 1 + net/ipv4/tcp_input.c | 10 +++++----- net/ipv4/tcp_minisocks.c | 1 + net/ipv4/tcp_output.c | 3 +++ .../net/packetdrill/tcp_rcv_big_endseq.pkt | 2 +- 9 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Documentation/networking/net_cachelines/tcp_sock.rst b/Documen= tation/networking/net_cachelines/tcp_sock.rst index 563daea10d6c5c074f004cb1b8574f5392157abb..fecf61166a54ee2f64bcef5312c= 81dcc4aa9a124 100644 --- a/Documentation/networking/net_cachelines/tcp_sock.rst +++ b/Documentation/networking/net_cachelines/tcp_sock.rst @@ -121,6 +121,7 @@ u64 delivered_mstamp r= ead_write u32 rate_delivered = read_mostly tcp_rate_gen u32 rate_interval_us = read_mostly rate_delivered,rate_app_limited u32 rcv_wnd read_write = read_mostly tcp_select_window,tcp_receive_window,tcp_fast_path_check +u32 rcv_mwnd_seq read_write = tcp_select_window u32 write_seq read_write = tcp_rate_check_app_limited,tcp_write_queue_empty,tcp_sk= b_entail,forced_push,tcp_mark_push u32 notsent_lowat read_mostly = tcp_stream_memory_free u32 pushed_seq read_write = tcp_mark_push,forced_push diff --git a/include/linux/tcp.h b/include/linux/tcp.h index f72eef31fa23cc584f2f0cefacdc35cae43aa52d..73aa2e0ccd1d7a6314a00c27950= b019b62a3851c 100644 --- a/include/linux/tcp.h +++ b/include/linux/tcp.h @@ -316,6 +316,9 @@ struct tcp_sock { */ u32 app_limited; /* limited until "delivered" reaches this val */ u32 rcv_wnd; /* Current receiver window */ + u32 rcv_mwnd_seq; /* Maximum window sequence number (RFC 7323, + * section 2.4, receiver requirements) + */ u32 rcv_tstamp; /* timestamp of last received ACK (for keepalives) */ /* * Options received (usually on last packet, some only on SYN packets= ). diff --git a/include/net/tcp.h b/include/net/tcp.h index a6464142380696e4948a836145ac7aca4ca3ec15..5fa8455ee9bc52d1434feaf82dd= a80be067a36e6 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -921,6 +921,28 @@ static inline u32 tcp_receive_window(const struct tcp_= sock *tp) return (u32) win; } =20 +/* Compute the maximum receive window we ever advertised. + * Rcv_nxt can be after the window if our peer push more data + * than the offered window. + */ +static inline u32 tcp_max_receive_window(const struct tcp_sock *tp) +{ + s32 win =3D tp->rcv_mwnd_seq - tp->rcv_nxt; + + if (win < 0) + win =3D 0; + return (u32) win; +} + +/* Check if we need to update the maximum receive window sequence number */ +static inline void tcp_update_max_rcv_wnd_seq(struct tcp_sock *tp) +{ + u32 wre =3D tp->rcv_wup + tp->rcv_wnd; + + if (after(wre, tp->rcv_mwnd_seq)) + tp->rcv_mwnd_seq =3D wre; +} + /* Choose a new window, without checks for shrinking, and without * scaling applied to the result. The caller does these things * if necessary. This is a "raw" window selection. diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index ed6f6712f06076dc33af61947782bde436dde15e..516087c622ade78883ca41e4f88= 3740e305035a0 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -3561,6 +3561,7 @@ static int tcp_repair_set_window(struct tcp_sock *tp,= sockptr_t optbuf, int len) =20 tp->rcv_wnd =3D opt.rcv_wnd; tp->rcv_wup =3D opt.rcv_wup; + tp->rcv_mwnd_seq =3D opt.rcv_wup + opt.rcv_wnd; =20 return 0; } @@ -5275,6 +5276,7 @@ static void __init tcp_struct_check(void) CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_txrx, recei= ved_ecn_bytes); CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_txrx, app_l= imited); CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_txrx, rcv_w= nd); + CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_txrx, rcv_m= wnd_seq); CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_txrx, rcv_t= stamp); CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_txrx, rx_op= t); =20 diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c index 9fdc19accafd23c6ab74bd82f7a7d82de1d60b90..4e389d609f919c17435509c5007= bc3b2a13eac6c 100644 --- a/net/ipv4/tcp_fastopen.c +++ b/net/ipv4/tcp_fastopen.c @@ -377,6 +377,7 @@ static struct sock *tcp_fastopen_create_child(struct so= ck *sk, =20 tcp_rsk(req)->rcv_nxt =3D tp->rcv_nxt; tp->rcv_wup =3D tp->rcv_nxt; + tp->rcv_mwnd_seq =3D tp->rcv_wup + tp->rcv_wnd; /* tcp_conn_request() is sending the SYNACK, * and queues the child into listener accept queue. */ diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 71ac69b7b75e4919f69631a4894421fa4e417c95..2e1b237608150c2e9c9baf73cf0= 47ed0823ca555 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -4808,20 +4808,18 @@ static enum skb_drop_reason tcp_sequence(const stru= ct sock *sk, const struct tcphdr *th) { const struct tcp_sock *tp =3D tcp_sk(sk); - u32 seq_limit; =20 if (before(end_seq, tp->rcv_wup)) return SKB_DROP_REASON_TCP_OLD_SEQUENCE; =20 - seq_limit =3D tp->rcv_nxt + tcp_receive_window(tp); - if (unlikely(after(end_seq, seq_limit))) { + if (unlikely(after(end_seq, tp->rcv_nxt + tcp_max_receive_window(tp)))) { /* Some stacks are known to handle FIN incorrectly; allow the * FIN to extend beyond the window and check it in detail later. */ - if (!after(end_seq - th->fin, seq_limit)) + if (!after(end_seq - th->fin, tp->rcv_nxt + tcp_receive_window(tp))) return SKB_NOT_DROPPED_YET; =20 - if (after(seq, seq_limit)) + if (after(seq, tp->rcv_nxt + tcp_max_receive_window(tp))) return SKB_DROP_REASON_TCP_INVALID_SEQUENCE; =20 /* Only accept this packet if receive queue is empty. */ @@ -6903,6 +6901,7 @@ static int tcp_rcv_synsent_state_process(struct sock = *sk, struct sk_buff *skb, */ WRITE_ONCE(tp->rcv_nxt, TCP_SKB_CB(skb)->seq + 1); tp->rcv_wup =3D TCP_SKB_CB(skb)->seq + 1; + tp->rcv_mwnd_seq =3D tp->rcv_wup + tp->rcv_wnd; =20 /* RFC1323: The window in SYN & SYN/ACK segments is * never scaled. @@ -7015,6 +7014,7 @@ static int tcp_rcv_synsent_state_process(struct sock = *sk, struct sk_buff *skb, WRITE_ONCE(tp->rcv_nxt, TCP_SKB_CB(skb)->seq + 1); WRITE_ONCE(tp->copied_seq, tp->rcv_nxt); tp->rcv_wup =3D TCP_SKB_CB(skb)->seq + 1; + tp->rcv_mwnd_seq =3D tp->rcv_wup + tp->rcv_wnd; =20 /* RFC1323: The window in SYN & SYN/ACK segments is * never scaled. diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c index dafb63b923d0d08cb1a0e9a37d8ec025386a960a..d350d794a959720853ffd8937cf= dc34c03e2ce30 100644 --- a/net/ipv4/tcp_minisocks.c +++ b/net/ipv4/tcp_minisocks.c @@ -604,6 +604,7 @@ struct sock *tcp_create_openreq_child(const struct sock= *sk, newtp->window_clamp =3D req->rsk_window_clamp; newtp->rcv_ssthresh =3D req->rsk_rcv_wnd; newtp->rcv_wnd =3D req->rsk_rcv_wnd; + newtp->rcv_mwnd_seq =3D newtp->rcv_wup + req->rsk_rcv_wnd; newtp->rx_opt.wscale_ok =3D ireq->wscale_ok; if (newtp->rx_opt.wscale_ok) { newtp->rx_opt.snd_wscale =3D ireq->snd_wscale; diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index f0ebcc7e287173be6198fd100130e7ba1a1dbf03..c86910d147f2394bf414d7691d8= f90ed41c1b0e3 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -293,6 +293,7 @@ static u16 tcp_select_window(struct sock *sk) tp->pred_flags =3D 0; tp->rcv_wnd =3D 0; tp->rcv_wup =3D tp->rcv_nxt; + tcp_update_max_rcv_wnd_seq(tp); return 0; } =20 @@ -316,6 +317,7 @@ static u16 tcp_select_window(struct sock *sk) =20 tp->rcv_wnd =3D new_win; tp->rcv_wup =3D tp->rcv_nxt; + tcp_update_max_rcv_wnd_seq(tp); =20 /* Make sure we do not exceed the maximum possible * scaled window. @@ -4195,6 +4197,7 @@ static void tcp_connect_init(struct sock *sk) else tp->rcv_tstamp =3D tcp_jiffies32; tp->rcv_wup =3D tp->rcv_nxt; + tp->rcv_mwnd_seq =3D tp->rcv_nxt + tp->rcv_wnd; WRITE_ONCE(tp->copied_seq, tp->rcv_nxt); =20 inet_csk(sk)->icsk_rto =3D tcp_timeout_init(sk); diff --git a/tools/testing/selftests/net/packetdrill/tcp_rcv_big_endseq.pkt= b/tools/testing/selftests/net/packetdrill/tcp_rcv_big_endseq.pkt index 6c0f32c40f19be2a750fc9d69bbf64250cd7b525..12882be10f2e0cf19e6bc7bd247= 9b27c11ce8ac0 100644 --- a/tools/testing/selftests/net/packetdrill/tcp_rcv_big_endseq.pkt +++ b/tools/testing/selftests/net/packetdrill/tcp_rcv_big_endseq.pkt @@ -36,7 +36,7 @@ =20 +0 read(4, ..., 100000) =3D 4000 =20 -// If queue is empty, accept a packet even if its end_seq is above wup + r= cv_wnd +// If queue is empty, accept a packet even if its end_seq is above rcv_mwn= d_seq +0 < P. 4001:54001(50000) ack 1 win 257 * > . 1:1(0) ack 54001 win 0 =20 --=20 2.53.0