From nobody Thu Aug 27 13:33:30 2026 X-Original-To: dev-commits-src-all@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 4hW2Xc4c7Gz6pxrY for ; Thu, 27 Aug 2026 13:33:36 +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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR2" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4hW2Xc0R92z3bYb for ; Thu, 27 Aug 2026 13:33:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1787837616; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rl63FMyz8mjz9Jpr7aDWJsHg+qong6+tuz/riMiCUuc=; b=e+0ofHdOByAWvRKw5MBs9GPbZGvOTSr3cleuTuqcLQsnQqh8kVsl9TVQzt1+8FAYmiDgXx ZcHtGds0wB7g3cI1hTCqABG5woNpxs2W0tKMpvzXxybsoE3DyjsR7x/kxJXr4oy70L21/r asWqrxOrO7SnVvDHyQn2AcWW60NM4NiPphUznoMB7aw9wpsFUFyxhKKqi69AD1kET9V7ip 2SXP1VCrzcOYpL/k2rZ58CnCmlpFSoPKEP+wZACdgxqrmPdVBHeQz8KpUFJZNq5/x5Ay+p KpcWr1p7H/LJisvZCpGo1qjhXB5f0XwiJTM6zhCLZWEzoziP4wQva4CQKhC5tA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1787837616; a=rsa-sha256; cv=none; b=XtV3i7Fb4pmRWIaua2mmD8cJJoa7/3y+oPqG7qI+JDWhrNvObqN8ADn0FkWMWg78txLb6s 5rzxJhebkRkRyPdobRR6m5eKNWN0+F1kT0ZA7mYktQ30PniaKqmlUgKAbQT/7A8tmxrqHB jN2DMv1TO7IWz/qnJ1cbVqT8AOsU6NjnUS1Pqio60jiOH2PSEhpM+Bc8uqhIunROXO3tjN NjBP5XLDCZ8kaTlJ047F1vIXlR7itZpSkPzsARnC0nArc/DXshPjF+Ro9OfRUOd715tYAE r/Ifn7Wo9XG4CPlOId+8DBsYLjNEwRMqxPuBWuRp/lHySYXbCZw/OFgrJCVKCg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1787837616; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rl63FMyz8mjz9Jpr7aDWJsHg+qong6+tuz/riMiCUuc=; b=KsuVOPFW1D90+jYubwaKtkamE0ot6KDVECq7BtKmB6llYg10CSMmm8aRLMLfqIH8tYO3JQ xf8hT0agcXL/tXogi04VrV5alO35d4/BBX1P4XZ78P6Mhj8UKkzIwfanpnzuyu2g6wV9AX huMBzNcs3NRcKFqQWXHaqhiheFgKpWjhoamPT2agw1Qitglz1FrfJVteLC1/ERTDVib/oc zAP9hU2bDXWWMQL2S/qMCOamjQ6R8JmRrZaS1QlsNXrGCDFUJUABNuPz1+kOZFID5IdId/ 5D5gM2VlCpIQ+6M5O0U2mTOpV/V9am+fRtN/RkDT0PHO8EumhyNQKdAu8f+ITQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hW2Xb60bqz9S1 for ; Thu, 27 Aug 2026 13:33:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 246b1 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 27 Aug 2026 13:33:30 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 00a793def743 - main - pf: Fix fallout from the STATE_LOOKUP macro removal List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: 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: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 00a793def743b5be8fcbdd39fc1dfa61534ed66c Auto-Submitted: auto-generated Date: Thu, 27 Aug 2026 13:33:30 +0000 Message-Id: <6a903caa.246b1.37ea2243@gitrepo.freebsd.org> The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=00a793def743b5be8fcbdd39fc1dfa61534ed66c commit 00a793def743b5be8fcbdd39fc1dfa61534ed66c Author: Mark Johnston AuthorDate: 2026-08-27 13:02:37 +0000 Commit: Mark Johnston CommitDate: 2026-08-27 13:33:10 +0000 pf: Fix fallout from the STATE_LOOKUP macro removal Commit 8572367b6814 ("pf: remove STATE_LOOKUP") introduced two seemingly unintentional changes with respect to divert(4)-injected packets (i.e., the PACKET_LOOPED case): we no longer return the matching state, and direct callers of pf_find_state() now treat matches of diverted packets the same as having no matching state at all. This seems inadvertent, and breaks certain rulesets which use divert-to. Fix them, and add a regression test case. Fixes: 8572367b6814 ("pf: remove STATE_LOOKUP") Reviewed by: kp MFC after: 2 weeks Sponsored by: OPNsense Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D59015 --- sys/netpfil/pf/pf.c | 17 +++------ tests/sys/netpfil/pf/route_to.sh | 82 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 11 deletions(-) diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c index 9f19f8203cd5..92b441d1306a 100644 --- a/sys/netpfil/pf/pf.c +++ b/sys/netpfil/pf/pf.c @@ -2231,14 +2231,9 @@ out: return (PF_DROP); } } - if (PACKET_LOOPED(pd)) { - PF_STATE_UNLOCK(s); - return (PF_PASS); - } *state = s; - - return (PF_MATCH); + return (PACKET_LOOPED(pd) ? PF_PASS : PF_MATCH); } /* @@ -8560,7 +8555,7 @@ pf_icmp_state_lookup(struct pf_state_key_cmp *key, struct pf_pdesc *pd, return (PF_DROP); action = pf_find_state(pd, key, state); - if (action != PF_MATCH) + if (action != PF_MATCH && action != PF_PASS) return (action); if ((*state)->state_flags & PFSTATE_SLOPPY) @@ -8885,7 +8880,7 @@ pf_test_state_icmp(struct pf_kstate **state, struct pf_pdesc *pd, key.port[pd2.didx] = th->th_dport; action = pf_find_state(&pd2, &key, state); - if (action != PF_MATCH) + if (action != PF_MATCH && action != PF_PASS) return (action); if (pd->dir == (*state)->direction) { @@ -9080,7 +9075,7 @@ pf_test_state_icmp(struct pf_kstate **state, struct pf_pdesc *pd, key.port[pd2.didx] = uh->uh_dport; action = pf_find_state(&pd2, &key, state); - if (action != PF_MATCH) + if (action != PF_MATCH && action != PF_PASS) return (action); /* translate source/destination address, if necessary */ @@ -9212,7 +9207,7 @@ pf_test_state_icmp(struct pf_kstate **state, struct pf_pdesc *pd, key.port[pd2.didx] = sh->dest_port; action = pf_find_state(&pd2, &key, state); - if (action != PF_MATCH) + if (action != PF_MATCH && action != PF_PASS) return (action); if (pd->dir == (*state)->direction) { @@ -9598,7 +9593,7 @@ pf_test_state_icmp(struct pf_kstate **state, struct pf_pdesc *pd, key.port[0] = key.port[1] = 0; action = pf_find_state(&pd2, &key, state); - if (action != PF_MATCH) + if (action != PF_MATCH && action != PF_PASS) return (action); /* translate source/destination address, if necessary */ diff --git a/tests/sys/netpfil/pf/route_to.sh b/tests/sys/netpfil/pf/route_to.sh index 1caf2367a9e3..3a3e44f0975d 100644 --- a/tests/sys/netpfil/pf/route_to.sh +++ b/tests/sys/netpfil/pf/route_to.sh @@ -1981,6 +1981,87 @@ mcast_v6_local_cleanup() pft_cleanup } +atf_test_case "divert_to" "cleanup" +divert_to_head() +{ + atf_set descr 'Test that route-to is applied to divert-injected packets' + atf_set require.user root + atf_set require.kmods ipdivert +} +divert_to_body() +{ + pft_init + + # Set up our topology: + # + # client <--> firewall <--> gateway <--> server + # + # The firewall has no default route. route-to forces TCP port 8080 + # traffic via epair_wan to the gateway; without it the firewall has no + # route to 10.4.4.2 and drops the packet. + + epair_cl=$(vnet_mkepair) + epair_wan=$(vnet_mkepair) + epair_srv=$(vnet_mkepair) + + vnet_mkjail client ${epair_cl}a + vnet_mkjail firewall ${epair_cl}b ${epair_wan}a + vnet_mkjail gateway ${epair_wan}b ${epair_srv}a + vnet_mkjail server ${epair_srv}b + + # Client + atf_check jexec client ifconfig ${epair_cl}a 10.1.1.2/24 up + atf_check -o ignore jexec client route add default 10.1.1.1 + + # Firewall: no default route; route-to sends traffic out via epair_wan + atf_check jexec firewall ifconfig ${epair_cl}b 10.1.1.1/24 up + atf_check jexec firewall ifconfig ${epair_wan}a 10.3.3.1/30 up + atf_check -o ignore jexec firewall sysctl net.inet.ip.forwarding=1 + + # Gateway: routes between firewall and server + atf_check jexec gateway ifconfig ${epair_wan}b 10.3.3.2/30 up + atf_check jexec gateway ifconfig ${epair_srv}a 10.4.4.1/24 up + atf_check -o ignore jexec gateway sysctl net.inet.ip.forwarding=1 + atf_check -o ignore jexec gateway route add -net 10.1.1.0/24 10.3.3.1 + + # Server + atf_check jexec server ifconfig ${epair_srv}b 10.4.4.2/24 up + atf_check -o ignore jexec server route add default 10.4.4.1 + + atf_check -o ignore jexec client ping -c 1 10.1.1.1 + + jexec server nc -l 8080 > $(pwd)/out & + server_pid=$! + + # Firewall pf: divert the flow and route-to via wan + jexec firewall pfctl -e + pft_set_rules firewall \ + "pass in quick on ${epair_cl}b \ + route-to (${epair_wan}a 10.3.3.2) \ + inet proto tcp from 10.1.1.0/24 to 10.4.4.2 port 8080 \ + keep state divert-to 8000" \ + "pass in all" \ + "pass out all" + + # Divert daemon: receives the SYN and re-injects it + jexec firewall ${common_dir}/divapp 8000 divert-back & + divapp_pid=$! + sleep 1 + + # The connection must succeed: route-to must be applied to the + # re-injected packet for it to reach the server. + echo "hello there" >$(pwd)/in + atf_check jexec client nc -N -w 3 10.4.4.2 8080 < $(pwd)/in + + wait $divapp_pid + wait $server_pid + atf_check -o match:"hello there" cat $(pwd)/out +} +divert_to_cleanup() +{ + pft_cleanup +} + atf_init_test_cases() { atf_add_test_case "v4" @@ -2008,6 +2089,7 @@ atf_init_test_cases() atf_add_test_case "mcast_v4_local" atf_add_test_case "mcast_v6_forwarded" atf_add_test_case "mcast_v6_local" + atf_add_test_case "divert_to" # Tests for pf_map_addr() without prefer-ipv6-nexthop atf_add_test_case "table_loop" atf_add_test_case "roundrobin"