From nobody Thu Nov 19 09:16:34 2026 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jVXsG3rWBz72Bht for ; Thu, 19 Nov 2026 09:16:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jVXsG131Dz4jqT for ; Thu, 19 Nov 2026 09:16:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795079794; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AROx3bZSlEJbyha1gTbtVratPqW/474E35wAssf4j5M=; b=yuMK6YfLXE3PgpZ1XJxH0rSmnhKHYn8b2kHmlsu3PZQtyPjUGypBEOUPJrQHzNP2+d5w/R kEkYBcjfg8e0f9oY8Sfu9ZaD4iDc5lyZzxP/tQ1yTg8mRC2oAmgpix+cXSpVjHVQ6WuVT7 /DVdqJXECiHEsc9s/dDVgvDS4QMJ8t3yTrt/fp/QfYB6V4Y9IcjDii6iXFqm3YYl37K/wO v81E4ZJLaI+ok46PiQANNh6MB1bXBfCh40IV8TFvE5jnkh4jtuHB0/WU4tI7UFHjULeuOu JKZLdcha4tQfLXGesquM0yW6yqeWU8f7kOdvzFUPy8YcrkQUp3vrdOrzBoYVLw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1795079794; b=mxvREGu+DTUmOWYfZSO+rzI4VCwBA2UtHkcciL7LOV0qJ1zT8ke4asu8dFFFOUC7pdYF9E CgNxY16jB32YQjXG/HzaFf8UD2rYwH9fvNiMmKZ6Yr0rqFr0en1WOrt5VEzRyU/vhwvI2i 58Xb3cFxUppGqOwLzrujakmH5K4nAzqTl6GqTNn8mlAxV2b9B6notzShn/zVrwfry9Yr7j AFD29qfMf4uSK6dtb58U8Mhz2YUD16ych7UCMyjMJTH08nSd0/WsuRvR3t+GQkQg/BYYxO 4/3wRDUIDphLVxPSA/kDW2mIwZ+1yHnZwKfRHissilmYpkNN9QHJU6jY4wl9wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795079794; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AROx3bZSlEJbyha1gTbtVratPqW/474E35wAssf4j5M=; b=Rk0qNUnh0bSsSwD5aSNxs+GY5zxCfapI1kOM9Y3ahO8h1Bcb+/V51KJzrKoIh+0YMJjSzI /1l1OnZChCi5T4w8Ksn7Atf0MJxaUB0TObjPEr4OvMX58DsOIGzxdm2A4em3Dd8oAHXUdB 4C8a6HHL5g/Wq4Q4H+RR6V7NlIUBv3qMAazwopHxpxp6iyR76+pPh+xl3vtMExE/cdgte9 hd42JA/4qlZ/QGxXsimk4BkGpNWzXiLi00Ile62JUkqNZMbn6n+B4LMZl1iTwHB07mj55b SX8YjI0hmurgo0LYxQ09ukrNTzdpktd4zIS0GxjlGkmb2G66uufM3EKDfpEqKQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jVXsG0jjXzvcr for ; Thu, 19 Nov 2026 09:16:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 40c9 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 19 Nov 2026 09:16:34 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Cc: Maksym Hrytsaienko From: Kristof Provost Subject: git: 4a97b40891f0 - main - pfsync: don't apply a stale dst peer from a state update List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main X-BeenThere: dev-commits-src-main@freebsd.org List-Id: Commit messages for the main branch of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 4a97b40891f0d5a85590935b84e8e0ee8e082401 Auto-Submitted: auto-generated Date: Thu, 19 Nov 2026 09:16:34 +0000 Message-Id: <6afebe72.40c9.8fc9c77@gitrepo.freebsd.org> The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=4a97b40891f0d5a85590935b84e8e0ee8e082401 commit 4a97b40891f0d5a85590935b84e8e0ee8e082401 Author: Maksym Hrytsaienko AuthorDate: 2026-11-02 19:56:42 +0000 Commit: Kristof Provost CommitDate: 2026-11-19 09:16:06 +0000 pfsync: don't apply a stale dst peer from a state update pfsync_upd_tcp() checks each peer of an incoming update separately. A peer whose window is behind the local copy is skipped and counted as stale, otherwise it is copied into the state. pfsync_in_upd() and pfsync_in_upd_c() then copied the dst peer again whenever fewer than two peers were stale. When only the dst peer was stale, that second copy moved the local dst window backwards. This is the common case for a bulk transfer, where the receiver's window barely moves and the sender's advances quickly. Once dst.seqlo regresses by more than MAXACKWINDOW, pf_tcp_track_full() drops every packet of the connection as PFRES_BADSTATE. Dropped packets never update the state, so the connection stays wedged until the application times out. The only trace is the state-mismatch counter. Remove the extra copy. Both branches that compute sync already copy dst when it is acceptable. Allocate dst scrub memory before the peers are compared, so an accepted update still finds it. Add a test that sends a compressed update with a current src peer and a stale dst peer, and checks that the dst window does not move back. PR: 298646 Signed-off-by: Maksym Hrytsaienko Reviewed-by: kp MFC after: 2 weeks Pull-Request: https://github.com/freebsd/freebsd-src/pull/2528 --- sys/netpfil/pf/if_pfsync.c | 6 +-- tests/sys/netpfil/pf/Makefile | 2 + tests/sys/netpfil/pf/pfsync.sh | 73 ++++++++++++++++++++++++++++++++++++ tests/sys/netpfil/pf/pfsync_upd_c.py | 71 +++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 4 deletions(-) diff --git a/sys/netpfil/pf/if_pfsync.c b/sys/netpfil/pf/if_pfsync.c index 23916241eb4c..373eadad5573 100644 --- a/sys/netpfil/pf/if_pfsync.c +++ b/sys/netpfil/pf/if_pfsync.c @@ -1395,6 +1395,7 @@ pfsync_in_upd(struct mbuf *m, int offset, int count, int flags, int action) pfsync_undefer_state(st, 1); } + pfsync_alloc_scrub_memory(&sp.pfs_1301.dst, &st->dst); if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP) sync = pfsync_upd_tcp(st, &sp.pfs_1301.src, &sp.pfs_1301.dst); else { @@ -1414,8 +1415,6 @@ pfsync_in_upd(struct mbuf *m, int offset, int count, int flags, int action) pf_state_peer_ntoh(&sp.pfs_1301.dst, &st->dst); } if (sync < 2) { - pfsync_alloc_scrub_memory(&sp.pfs_1301.dst, &st->dst); - pf_state_peer_ntoh(&sp.pfs_1301.dst, &st->dst); st->expire = pf_get_uptime(); st->timeout = timeout; } @@ -1479,6 +1478,7 @@ pfsync_in_upd_c(struct mbuf *m, int offset, int count, int flags, int action) pfsync_undefer_state(st, 1); } + pfsync_alloc_scrub_memory(&up.dst, &st->dst); if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP) sync = pfsync_upd_tcp(st, &up.src, &up.dst); else { @@ -1498,8 +1498,6 @@ pfsync_in_upd_c(struct mbuf *m, int offset, int count, int flags, int action) pf_state_peer_ntoh(&up.dst, &st->dst); } if (sync < 2) { - pfsync_alloc_scrub_memory(&up.dst, &st->dst); - pf_state_peer_ntoh(&up.dst, &st->dst); st->expire = pf_get_uptime(); st->timeout = up.timeout; } diff --git a/tests/sys/netpfil/pf/Makefile b/tests/sys/netpfil/pf/Makefile index be3ac9f91486..ebb73b52fc41 100644 --- a/tests/sys/netpfil/pf/Makefile +++ b/tests/sys/netpfil/pf/Makefile @@ -92,6 +92,7 @@ ${PACKAGE}FILES+= \ frag-adjhole.py \ pfctl_adopt.py \ pfsync_defer.py \ + pfsync_upd_c.py \ pft_ether.py \ pft_read_ipfix.py \ rdr-srcport.py \ @@ -111,6 +112,7 @@ ${PACKAGE}FILESMODE_frag-overhole.py= 0555 ${PACKAGE}FILESMODE_frag-adjhole.py= 0555 ${PACKAGE}FILESMODE_pfctl_adopt.py= 0555 ${PACKAGE}FILESMODE_pfsync_defer.py= 0555 +${PACKAGE}FILESMODE_pfsync_upd_c.py= 0555 ${PACKAGE}FILESMODE_pft_ether.py= 0555 ${PACKAGE}FILESMODE_pft_read_ipfix.py= 0555 diff --git a/tests/sys/netpfil/pf/pfsync.sh b/tests/sys/netpfil/pf/pfsync.sh index 1b07a6a3c620..e091cd28eee7 100644 --- a/tests/sys/netpfil/pf/pfsync.sh +++ b/tests/sys/netpfil/pf/pfsync.sh @@ -1971,6 +1971,78 @@ expire_underflow_cleanup() pfsynct_cleanup } +atf_test_case "stale_upd_c" "cleanup" +stale_upd_c_head() +{ + atf_set descr 'Stale dst peer in UPD_C must not regress the window' + atf_set require.user root + atf_set require.progs python3 scapy nc +} + +stale_upd_c_body() +{ + pfsynct_init + + epair_sync=$(vnet_mkepair) + epair_data=$(vnet_mkepair) + + vnet_mkjail one ${epair_sync}a ${epair_data}a + + jexec one ifconfig ${epair_sync}a 192.0.2.1/24 up + jexec one ifconfig ${epair_data}a 198.51.100.1/24 up + jexec one ifconfig pfsync0 \ + syncdev ${epair_sync}a \ + syncpeer 192.0.2.2 \ + maxupd 1 \ + up + ifconfig ${epair_sync}b 192.0.2.2/24 up + ifconfig ${epair_data}b 198.51.100.2/24 up + + jexec one pfctl -e + pft_set_rules one \ + "set skip on ${epair_sync}a" \ + "pass keep state" + + # The jail sends data, so the dst (responder) side of the state moves. + jexec one sh -c "(dd if=/dev/zero bs=1k count=256 2>/dev/null; \ + sleep 30) | nc -l 1234 >/dev/null" & + sleep 1 + (sleep 30 | nc 198.51.100.1 1234 >/dev/null) & + sleep 3 + + state=$(jexec one pfctl -ss -vv | grep -A3 "198.51.100.1:1234 <-") + echo "${state}" + # The state was created inbound, so pfctl prints the dst peer first. + # Line 2 is "[seqlo + win] wscale N [seqlo + win] wscale N". + set -- $(echo "${state}" | sed -n 2p | tr -d '[]+') + [ $# -eq 8 ] || atf_fail "Could not parse state" + dst_lo=$1; dst_hi=$(($1 + $2)); dst_ws=$4 + src_lo=$5; src_hi=$(($5 + $6)); src_ws=$8 + id=$(echo "${state}" | awk '/id:/ { print $2 }') + creatorid=$(echo "${state}" | awk '/id:/ { print $4 }') + + # Current src peer, dst peer 100000 bytes behind: only dst is stale. + stale=$(( (dst_lo - 100000) & 0xffffffff )) + atf_check -s exit:0 env PYTHONPATH=${common_dir} \ + $(atf_get_srcdir)/pfsync_upd_c.py \ + --to 192.0.2.1 --id ${id} --creatorid ${creatorid} \ + --src ${src_lo} ${src_hi} ${src_ws} \ + --dst ${stale} ${dst_hi} ${dst_ws} + sleep 1 + + jexec one pfctl -ss -vv | grep -A3 "198.51.100.1:1234 <-" + jexec one netstat -s -p pfsync | grep -q "1 stale state" || \ + atf_fail "Update was not seen as stale" + jexec one pfctl -ss -vv | grep -A1 "198.51.100.1:1234 <-" | \ + grep -q "\[${dst_lo} + " || \ + atf_fail "Stale update regressed the dst sequence window" +} + +stale_upd_c_cleanup() +{ + pfsynct_cleanup +} + atf_init_test_cases() { atf_add_test_case "basic" @@ -1995,6 +2067,7 @@ atf_init_test_cases() atf_add_test_case "tag" atf_add_test_case "altq_queues" atf_add_test_case "rt_af" + atf_add_test_case "stale_upd_c" atf_add_test_case "flush_by_intf" atf_add_test_case "stack_key_collision" atf_add_test_case "expire_underflow" diff --git a/tests/sys/netpfil/pf/pfsync_upd_c.py b/tests/sys/netpfil/pf/pfsync_upd_c.py new file mode 100644 index 000000000000..5ce6aea7813c --- /dev/null +++ b/tests/sys/netpfil/pf/pfsync_upd_c.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2026 Maksym Hrytsaienko +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + +# Send a single pfsync PFSYNC_ACT_UPD_C message for an existing TCP state. + +import argparse +import struct +import sys +import scapy.all as sp + +PFSYNC_VERSION = 5 +PFSYNC_ACT_UPD_C = 4 +TCPS_ESTABLISHED = 4 +PFTM_TCP_ESTABLISHED = 2 + +def peer(seqlo, seqhi, wscale): + # struct pf_state_peer_export: scrub, seqlo, seqhi, seqdiff, max_win, + # mss, state, wscale, dummy[6] + return bytes(8) + struct.pack('!IIIHHBB6x', seqlo, seqhi, 0, 65535, 0, + TCPS_ESTABLISHED, wscale) + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--to', required=True) + parser.add_argument('--id', required=True, + help='state id as printed by pfctl -ss -vv') + parser.add_argument('--creatorid', required=True) + parser.add_argument('--src', nargs=3, type=int, required=True, + metavar=('SEQLO', 'SEQHI', 'WSCALE')) + parser.add_argument('--dst', nargs=3, type=int, required=True, + metavar=('SEQLO', 'SEQHI', 'WSCALE')) + args = parser.parse_args() + + # pfctl prints the raw id as a host-order integer. + sid = bytes.fromhex(args.id) + if sys.byteorder == 'little': + sid = sid[::-1] + upd = sid + peer(*args.src) + peer(*args.dst) + \ + bytes.fromhex(args.creatorid) + \ + struct.pack('!IB3x', 0, PFTM_TCP_ESTABLISHED) + sub = struct.pack('!BBH', PFSYNC_ACT_UPD_C, 0, 1) + upd + hdr = struct.pack('!BBH', PFSYNC_VERSION, 0, 20 + len(sub)) + bytes(16) + sp.send(sp.IP(dst=args.to, ttl=255, proto=240) / sp.Raw(hdr + sub), + verbose=False) + +if __name__ == '__main__': + main()