git: 820804014074 - stable/15 - pf: first-match-wins for nat rules, even in anchors
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 20 Oct 2026 13:58:06 UTC
The branch stable/15 has been updated by kp:
URL: https://cgit.FreeBSD.org/src/commit/?id=820804014074cdc4f56dca7a3966019b8aa172ae
commit 820804014074cdc4f56dca7a3966019b8aa172ae
Author: Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2026-10-13 12:40:58 +0000
Commit: Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-10-20 13:53:41 +0000
pf: first-match-wins for nat rules, even in anchors
Ensure we stop evaluating nat rules if we find a match in a nat-anchor.
PR: 299320
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")
(cherry picked from commit b5219c068fbc133dde91d5cf48423e34ce27bd4f)
---
sys/netpfil/pf/pf_lb.c | 4 ++
tests/sys/netpfil/pf/nat.sh | 91 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 95 insertions(+)
diff --git a/sys/netpfil/pf/pf_lb.c b/sys/netpfil/pf/pf_lb.c
index cb99038a07fa..eecb14149dfd 100644
--- a/sys/netpfil/pf/pf_lb.c
+++ b/sys/netpfil/pf/pf_lb.c
@@ -224,6 +224,10 @@ pf_match_translation_rule(int rs_num, struct pf_test_ctx *ctx, struct pf_krulese
r) != PF_TEST_OK) {
break;
}
+ /* If we found a match in the anchor we're done. */
+ if (ctx->tr != NULL) {
+ break;
+ }
}
r = TAILQ_NEXT(r, entries);
}
diff --git a/tests/sys/netpfil/pf/nat.sh b/tests/sys/netpfil/pf/nat.sh
index 1355da83d2f3..47292b1c7642 100644
--- a/tests/sys/netpfil/pf/nat.sh
+++ b/tests/sys/netpfil/pf/nat.sh
@@ -916,6 +916,95 @@ empty_pool_cleanup()
pft_cleanup
}
+atf_test_case "first_match" "cleanup"
+first_match_head()
+{
+ atf_set descr 'Test that NAT rules are first match'
+ atf_set require.user root
+}
+
+first_match_body()
+{
+ pft_init
+
+ epair_nat=$(vnet_mkepair)
+ epair_echo=$(vnet_mkepair)
+
+ vnet_mkjail nat ${epair_nat}b ${epair_echo}a
+ vnet_mkjail echo ${epair_echo}b
+
+ ifconfig ${epair_nat}a 192.0.2.2/24 up
+ route add -net 198.51.100.0/24 192.0.2.1
+
+ jexec nat ifconfig ${epair_nat}b 192.0.2.1/24 up
+ jexec nat ifconfig ${epair_echo}a 198.51.100.1/24 up
+ jexec nat sysctl net.inet.ip.forwarding=1
+
+ jexec echo ifconfig ${epair_echo}b 198.51.100.2/24 up
+
+ # Enable pf!
+ jexec nat pfctl -e
+ pft_set_rules nat \
+ "table <foo> { 192.0.2.0/24 }" \
+ "nat on ${epair_echo}a inet from <foo> to any -> 198.51.100.1" \
+ "nat on ${epair_echo}a inet from 192.0.2.0/24 to any -> 198.51.100.3"
+
+ atf_check -s exit:0 -o ignore ping -c 3 198.51.100.2
+ atf_check -s exit:0 -e ignore \
+ -o match:"all icmp 198.51.100.1:.*(192.0.2.2:.*) -> 198.51.100.2:8.*" \
+ jexec nat pfctl -ss
+}
+
+first_match_cleanup()
+{
+ pft_cleanup
+}
+
+atf_test_case "first_match_anchor" "cleanup"
+first_match_anchor_head()
+{
+ atf_set descr 'Test that NAT matches within an anchor are final (PR 299320)'
+ atf_set require.user root
+}
+
+first_match_anchor_body()
+{
+ pft_init
+
+ epair_nat=$(vnet_mkepair)
+ epair_echo=$(vnet_mkepair)
+
+ vnet_mkjail nat ${epair_nat}b ${epair_echo}a
+ vnet_mkjail echo ${epair_echo}b
+
+ ifconfig ${epair_nat}a 192.0.2.2/24 up
+ route add -net 198.51.100.0/24 192.0.2.1
+
+ jexec nat ifconfig ${epair_nat}b 192.0.2.1/24 up
+ jexec nat ifconfig ${epair_echo}a 198.51.100.1/24 up
+ jexec nat sysctl net.inet.ip.forwarding=1
+
+ jexec echo ifconfig ${epair_echo}b 198.51.100.2/24 up
+
+ # Enable pf!
+ jexec nat pfctl -e
+ pft_set_rules nat \
+ "nat-anchor \"foo\"" \
+ "nat on ${epair_echo}a inet from 192.0.2.0/24 to any -> 198.51.100.3"
+
+ echo "nat on ${epair_echo}a inet from 192.0.2.0/24 to any -> 198.51.100.1" | jexec nat pfctl -g -f - -a "foo"
+
+ atf_check -s exit:0 -o ignore ping -c 3 198.51.100.2
+ atf_check -s exit:0 -e ignore \
+ -o match:"all icmp 198.51.100.1:.*(192.0.2.2:.*) -> 198.51.100.2:8.*" \
+ jexec nat pfctl -ss
+}
+
+first_match_anchor_cleanup()
+{
+ pft_cleanup
+}
+
atf_init_test_cases()
{
atf_add_test_case "exhaust"
@@ -937,4 +1026,6 @@ atf_init_test_cases()
atf_add_test_case "binat_compat"
atf_add_test_case "binat_match"
atf_add_test_case "empty_pool"
+ atf_add_test_case "first_match"
+ atf_add_test_case "first_match_anchor"
}