git: 20e652ba25b2 - main - netinet6: Do not count a packet a hook consumed as discarded in ip6_tryforward()

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

URL: https://cgit.FreeBSD.org/src/commit/?id=20e652ba25b2ecdd2382aa10b0b5b2c183bef13f

commit 20e652ba25b2ecdd2382aa10b0b5b2c183bef13f
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-18 21:42:50 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-18 21:42:50 +0000

    netinet6: Do not count a packet a hook consumed as discarded in ip6_tryforward()
    
    ip6_tryforward() treats every pfil result but PFIL_PASS as a drop: it
    counts a discard on the receiving interface after the inbound hooks and
    on the outgoing interface after the outbound hooks.  A hook that
    consumes the packet returns PFIL_CONSUMED instead: pf sends a packet a
    route-to rule matches out itself, and sends a packet it reassembled on
    in fragments.  Those packets were forwarded, yet netstat -s -I shows
    them as discarded, in the outbound case on the interface the route
    lookup chose, which route-to did not even use.  ip6_forward() and
    ip_tryforward() count nothing for a consumed packet.
    
    Return without counting when a hook consumed the packet.  The new
    forward:v6_route_to_discards test checks both passes.  Its rules keep
    no state: pf reports a packet it sent from the inbound hook as dropped
    when its own outbound pass created a state, and that packet is still
    counted.
    
    Reviewed by:            kp, glebius, pouria
    Fixes:                  5a1842a2 ("Add ip6_tryforward() - a run to completion ...")
    MFC after:              1 week
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61042
---
 sys/netinet6/ip6_fastfwd.c      | 23 +++++++++++---
 tests/sys/netpfil/pf/forward.sh | 66 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 4 deletions(-)

diff --git a/sys/netinet6/ip6_fastfwd.c b/sys/netinet6/ip6_fastfwd.c
index 2328a020531e..3c970be1d1b0 100644
--- a/sys/netinet6/ip6_fastfwd.c
+++ b/sys/netinet6/ip6_fastfwd.c
@@ -156,9 +156,15 @@ ip6_tryforward(struct mbuf *m)
 	 */
 	if (!PFIL_HOOKED_IN(V_inet6_pfil_head))
 		goto passin;
-	if (pfil_mbuf_in(V_inet6_pfil_head, &m, rcvif, NULL) !=
-	    PFIL_PASS)
+	switch (pfil_mbuf_in(V_inet6_pfil_head, &m, rcvif, NULL)) {
+	case PFIL_PASS:
+		break;
+	case PFIL_CONSUMED:
+		/* A hook sent the packet on itself (pf route-to). */
+		return (NULL);
+	default:
 		goto dropin;
+	}
 	/*
 	 * If packet filter sets the M_FASTFWD_OURS flag, this means
 	 * that new destination or next hop is our local address.
@@ -230,9 +236,18 @@ passin:
 	 * Outgoing packet firewall processing.
 	 */
 	odst = ip6->ip6_dst;
-	if (pfil_mbuf_fwd(V_inet6_pfil_head, &m, nh->nh_ifp,
-	    NULL) != PFIL_PASS)
+	switch (pfil_mbuf_fwd(V_inet6_pfil_head, &m, nh->nh_ifp, NULL)) {
+	case PFIL_PASS:
+		break;
+	case PFIL_CONSUMED:
+		/*
+		 * A hook sent the packet on itself (pf route-to) or in
+		 * fragments (pf refragmentation).
+		 */
+		return (NULL);
+	default:
 		goto dropout;
+	}
 
 	/*
 	 * We used slow path processing for packets with scoped addresses.
diff --git a/tests/sys/netpfil/pf/forward.sh b/tests/sys/netpfil/pf/forward.sh
index 5e4a53cea5c3..4524a32dc392 100644
--- a/tests/sys/netpfil/pf/forward.sh
+++ b/tests/sys/netpfil/pf/forward.sh
@@ -277,10 +277,76 @@ v6_hlim_decrement_cleanup()
 	pft_cleanup
 }
 
+atf_test_case "v6_route_to_discards" "cleanup"
+v6_route_to_discards_head()
+{
+	atf_set descr 'Test that packets pf sends on with route-to are not counted as discarded'
+	atf_set require.user root
+}
+
+v6_route_to_discards_body()
+{
+	pft_init
+
+	epair_send=$(vnet_mkepair)
+	epair_recv=$(vnet_mkepair)
+	epair_route=$(vnet_mkepair)
+
+	ifconfig ${epair_send}a inet6 2001:db8:42::1/64 up no_dad -ifdisabled
+	ifconfig ${epair_recv}a up
+	ifconfig ${epair_route}a up
+
+	vnet_mkjail alcatraz ${epair_send}b ${epair_recv}b ${epair_route}b
+
+	jexec alcatraz ifconfig ${epair_send}b inet6 2001:db8:42::2/64 up no_dad
+	jexec alcatraz ifconfig ${epair_recv}b inet6 2001:db8:43::2/64 up no_dad
+	jexec alcatraz ifconfig ${epair_route}b inet6 2001:db8:44::2/64 up no_dad
+	jexec alcatraz sysctl net.inet6.ip6.forwarding=1
+	# ip6_tryforward() only forwards while redirects are off.
+	jexec alcatraz sysctl net.inet6.ip6.redirect=0
+	jexec alcatraz ndp -s 2001:db8:43::3 00:01:02:03:04:05
+	jexec alcatraz ndp -s 2001:db8:44::3 00:01:02:03:04:06
+	jexec alcatraz route add -6 2001:db8:666::/64 2001:db8:43::3
+	route add -6 2001:db8:666::/64 2001:db8:42::2
+
+	jexec alcatraz pfctl -e
+
+	# pf sends the packet on itself and tells ip6_tryforward() that it
+	# consumed it: in the inbound hook for route-to on the inbound pass, in
+	# 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.
+	#
+	# 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
+	done
+}
+
+v6_route_to_discards_cleanup()
+{
+	pft_cleanup
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case "v4"
 	atf_add_test_case "v6"
 	atf_add_test_case "v6_hlim"
 	atf_add_test_case "v6_hlim_decrement"
+	atf_add_test_case "v6_route_to_discards"
 }