git: 4a97b40891f0 - main - pfsync: don't apply a stale dst peer from a state update
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 19 Nov 2026 09:16:34 UTC
The branch main has been updated by kp:
URL: https://cgit.FreeBSD.org/src/commit/?id=4a97b40891f0d5a85590935b84e8e0ee8e082401
commit 4a97b40891f0d5a85590935b84e8e0ee8e082401
Author: Maksym Hrytsaienko <hrytsaienko.my@gmail.com>
AuthorDate: 2026-11-02 19:56:42 +0000
Commit: Kristof Provost <kp@FreeBSD.org>
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 <hrytsaienko.my@gmail.com>
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 <hrytsaienko.my@gmail.com>
+#
+# 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()