git: 3ccfcdacd6ae - main - pf: Do not report a packet the outbound pass sent as dropped

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 21:45:28 UTC
The branch main has been updated by rcm:

URL: https://cgit.FreeBSD.org/src/commit/?id=3ccfcdacd6ae7611dc0f2a1375713dc0111c3d5f

commit 3ccfcdacd6ae7611dc0f2a1375713dc0111c3d5f
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-18 21:44:44 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-18 21:44:44 +0000

    pf: Do not report a packet the outbound pass sent as dropped
    
    For route-to on the inbound pass, pf_route() and pf_route6() run the
    outbound pass on the new interface themselves and hand it the actions
    of the rule.  When that pass creates a state, the state has the route-to
    as well, so the outbound pass sends the packet and returns PF_PASS
    without an mbuf.  Since 349360b17eab the caller turns this into PF_DROP,
    and the pfil hook returns PFIL_DROPPED for a packet that was forwarded.
    ip6_tryforward() then counts an input discard on the receiving
    interface for every such packet.
    
    Leave the action at PF_PASS, so that the hook returns PFIL_CONSUMED, as
    it does when pf_route() or pf_route6() send the packet themselves.
    Extend forward:v6_route_to_discards to rules that keep state.
    
    Reviewed by:            kp
    Fixes:                  349360b17eab ("pf: return errors from pf_route() and ...")
    MFC after:              1 week
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61674
---
 sys/netpfil/pf/pf.c             | 12 ++++++++----
 tests/sys/netpfil/pf/forward.sh | 30 ++++++++++++++++--------------
 2 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c
index 6601763d4b51..77a3eb643eeb 100644
--- a/sys/netpfil/pf/pf.c
+++ b/sys/netpfil/pf/pf.c
@@ -9947,8 +9947,10 @@ pf_route(struct pf_krule *r, struct ifnet *oifp,
 				SDT_PROBE1(pf, ip, route_to, drop, __LINE__);
 				goto bad;
 			} else if (m0 == NULL) {
-				action = PF_DROP;
-				SDT_PROBE1(pf, ip, route_to, drop, __LINE__);
+				/*
+				 * The outbound pass sent the packet on itself
+				 * or queued it, so it is consumed, not dropped.
+				 */
 				goto done;
 			}
 			if (m0->m_len < sizeof(struct ip)) {
@@ -10291,8 +10293,10 @@ pf_route6(struct pf_krule *r, struct ifnet *oifp,
 				SDT_PROBE1(pf, ip6, route_to, drop, __LINE__);
 				goto bad;
 			} else if (m0 == NULL) {
-				action = PF_DROP;
-				SDT_PROBE1(pf, ip6, route_to, drop, __LINE__);
+				/*
+				 * The outbound pass sent the packet on itself
+				 * or queued it, so it is consumed, not dropped.
+				 */
 				goto done;
 			}
 			if (m0->m_len < sizeof(struct ip6_hdr)) {
diff --git a/tests/sys/netpfil/pf/forward.sh b/tests/sys/netpfil/pf/forward.sh
index 4524a32dc392..fcb47e1718f1 100644
--- a/tests/sys/netpfil/pf/forward.sh
+++ b/tests/sys/netpfil/pf/forward.sh
@@ -316,24 +316,26 @@ v6_route_to_discards_body()
 	# the outbound hook, after the route lookup chose ${epair_recv}b, for
 	# route-to on the outbound pass.  Neither is a discard.
 	#
-	# The rules do not keep state: when the outbound pass that pf runs from
-	# the inbound hook creates a state, pf reports the packet it sent as
-	# dropped.
+	# With state, the outbound pass that pf runs from the inbound hook
+	# creates a state of its own and sends the packet; without, the inbound
+	# pass does.
 	#
 	# The host's MLD reports are for a group alcatraz has not joined, and
 	# ip6_input() would count them as discarded.  Block them.
 	for dir in in out; do
-		pft_set_rules alcatraz \
-			"block in quick inet6 to ff02::16" \
-			"pass no state" \
-			"pass ${dir} quick route-to (${epair_route}b 2001:db8:44::3) inet6 to 2001:db8:666::1 no state"
-		in_discards=$(jexec alcatraz netstat -s -p ip6 -I ${epair_send}b |
-		    awk '/input datagrams? discarded/ { print $1 }')
-		atf_check -s exit:2 -o ignore ping -6 -c 1 -W 1 2001:db8:666::1
-		atf_check -o match:"[[:space:]]${in_discards} input datagrams? discarded" \
-		    jexec alcatraz netstat -s -p ip6 -I ${epair_send}b
-		atf_check -o match:"[[:space:]]0 total discarded output datagrams" \
-		    jexec alcatraz netstat -s -p ip6 -I ${epair_recv}b
+		for state in "keep state" "no state"; do
+			pft_set_rules alcatraz \
+				"block in quick inet6 to ff02::16" \
+				"pass ${state}" \
+				"pass ${dir} quick route-to (${epair_route}b 2001:db8:44::3) inet6 to 2001:db8:666::1 ${state}"
+			in_discards=$(jexec alcatraz netstat -s -p ip6 -I ${epair_send}b |
+			    awk '/input datagrams? discarded/ { print $1 }')
+			atf_check -s exit:2 -o ignore ping -6 -c 1 -W 1 2001:db8:666::1
+			atf_check -o match:"[[:space:]]${in_discards} input datagrams? discarded" \
+			    jexec alcatraz netstat -s -p ip6 -I ${epair_send}b
+			atf_check -o match:"[[:space:]]0 total discarded output datagrams" \
+			    jexec alcatraz netstat -s -p ip6 -I ${epair_recv}b
+		done
 	done
 }