git: 15178d8ed290 - main - pf: Fix state handling when ICMP packets are diverted

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 22 Dec 2025 14:47:36 UTC
The branch main has been updated by markj:

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

commit 15178d8ed2904d5c4fa31a1531e60dcb1e0f9209
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-12-20 18:18:50 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-12-22 14:47:27 +0000

    pf: Fix state handling when ICMP packets are diverted
    
    Commit 66f2f1c83247 ("pf: handle divert packets") missed a case that I
    happened to hit while testing something.
    
    Add a regression test for the ICMP case, based on the existing test.
    Fix a buglet in the existing test (missing whitespace after "[").
    
    Reviewed by:    kp
    Sponsored by:   OPNsense
    Sponsored by:   Klara, Inc.
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D54321
---
 sys/netpfil/pf/pf.c               | 10 ++++++----
 tests/sys/netpfil/pf/divert-to.sh | 40 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c
index 2ebaf61a165f..755b87bcfeb7 100644
--- a/sys/netpfil/pf/pf.c
+++ b/sys/netpfil/pf/pf.c
@@ -11178,10 +11178,12 @@ pf_test(sa_family_t af, int dir, int pflags, struct ifnet *ifp, struct mbuf **m0
 		}
 		action = pf_test_state_icmp(&s, &pd, &reason);
 		if (action == PF_PASS || action == PF_AFRT) {
-			if (V_pfsync_update_state_ptr != NULL)
-				V_pfsync_update_state_ptr(s);
-			r = s->rule;
-			a = s->anchor;
+			if (s != NULL) {
+				if (V_pfsync_update_state_ptr != NULL)
+					V_pfsync_update_state_ptr(s);
+				r = s->rule;
+				a = s->anchor;
+			}
 		} else if (s == NULL)
 			action = pf_test_rule(&r, &s, &pd,
 			    &a, &ruleset, &reason, inp, &match_rules);
diff --git a/tests/sys/netpfil/pf/divert-to.sh b/tests/sys/netpfil/pf/divert-to.sh
index 153136199311..3028c9e75afd 100644
--- a/tests/sys/netpfil/pf/divert-to.sh
+++ b/tests/sys/netpfil/pf/divert-to.sh
@@ -402,8 +402,7 @@ pr260867_body()
 	    "pass in on ${epair}b proto tcp from any to port 7 divert-to 0.0.0.0 port 1001"
 
 	reply=$(echo "foo" | nc -N 192.0.2.2 7)
-	if ["${reply}" != "foo" ];
-	then
+	if [ "${reply}" != "foo" ]; then
 		atf_fail "Did not receive echo reply"
 	fi
 }
@@ -413,6 +412,42 @@ pr260867_cleanup()
 	pft_cleanup
 }
 
+atf_test_case "pr260867_icmp" "cleanup"
+pr260867_icmp_head()
+{
+	atf_set descr 'Variant of the PR260867 test'
+	atf_set require.user root
+}
+
+pr260867_icmp_body()
+{
+	pft_init
+	divert_init
+
+	epair=$(vnet_mkepair)
+
+	atf_check ifconfig ${epair}a 192.0.2.1/24 up
+
+	vnet_mkjail alcatraz ${epair}b
+	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
+
+	# Sanity check
+	atf_check -s exit:0 -o ignore ping -c3 192.0.2.2
+
+	jexec alcatraz $(atf_get_srcdir)/../common/divapp 1001 divert-back &
+
+	jexec alcatraz pfctl -e
+	pft_set_rules alcatraz \
+	    "pass in on ${epair}b proto icmp from any to any divert-to 0.0.0.0 port 1001"
+
+	atf_check -o ignore ping -c 3 192.0.2.2
+}
+
+pr260867_icmp_cleanup()
+{
+	pft_cleanup
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case "in_div"
@@ -426,4 +461,5 @@ atf_init_test_cases()
 	atf_add_test_case "in_dn_in_div_in_out_div_out_dn_out"
 
 	atf_add_test_case "pr260867"
+	atf_add_test_case "pr260867_icmp"
 }