git: d05d660c744e - releng/15.2 - tcp: count only the SACK blocks that fit in tcp_maxseg()

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Fri, 27 Nov 2026 00:00:29 UTC
The branch releng/15.2 has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=d05d660c744e30a222be712935053583abb6f26e

commit d05d660c744e30a222be712935053583abb6f26e
Author:     Nick Price <nprice@FreeBSD.org>
AuthorDate: 2026-11-13 20:27:46 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-11-26 23:59:59 +0000

    tcp: count only the SACK blocks that fit in tcp_maxseg()
    
    tcp_addoptions() sends only the SACK blocks that fit in TCP_MAXOLEN.
    tcp_maxseg() counted every pending block and clamped the total, so
    without timestamps and with five or more blocks pending it came out 4
    bytes below the segment tcp_output() sends.  Limited transmit after an
    RTO then sent one full segment and failed the "sent too much" KASSERT.
    
    Approved by:    re (cperciva)
    PR:             282605
    Approved by:    adrian (mentor)
    Signed-off-by:  Nick Price <nprice@FreeBSD.org>
    Reviewed by:    tuexen, glebius, adrian
    Differential Revision:  https://reviews.freebsd.org/D60460
    (cherry picked from commit d3b02115d4b373c719d9a716da38d94511de3f07)
    (cherry picked from commit aacae7b35a22a56e187a0a41db231d63ca9cefb6)
---
 sys/netinet/tcp_subr.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/sys/netinet/tcp_subr.c b/sys/netinet/tcp_subr.c
index 533f19699074..b787f9b527ba 100644
--- a/sys/netinet/tcp_subr.c
+++ b/sys/netinet/tcp_subr.c
@@ -3789,9 +3789,12 @@ tcp_maxseg(const struct tcpcb *tp)
 			optlen += PADTCPOLEN(TCPOLEN_SIGNATURE);
 #endif
 		if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0) {
-			optlen += TCPOLEN_SACKHDR;
-			optlen += tp->rcv_numsacks * TCPOLEN_SACK;
-			optlen = PADTCPOLEN(optlen);
+			u_int sackblks;
+
+			sackblks = min(tp->rcv_numsacks, (TCP_MAXOLEN - optlen -
+			    PADTCPOLEN(TCPOLEN_SACKHDR)) / TCPOLEN_SACK);
+			optlen += PADTCPOLEN(TCPOLEN_SACKHDR +
+			    sackblks * TCPOLEN_SACK);
 		}
 	} else {
 		if (tp->t_flags & TF_REQ_TSTMP)