git: bf8783a32faf - main - netinet6: Decrement the hop limit before the outbound hooks in ip6_tryforward()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 18 Nov 2026 21:40:59 UTC
The branch main has been updated by rcm:
URL: https://cgit.FreeBSD.org/src/commit/?id=bf8783a32faf5eed6d4165c4763febf5916f25e7
commit bf8783a32faf5eed6d4165c4763febf5916f25e7
Author: R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-18 21:39:38 +0000
Commit: R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-18 21:39:38 +0000
netinet6: Decrement the hop limit before the outbound hooks in ip6_tryforward()
ip6_tryforward() decrements the hop limit after the outbound pfil hooks,
whereas ip_tryforward() and ip6_forward() do it before them. The hooks
therefore see a packet one hop older than the one that leaves, and a hook
that transmits the packet itself does not decrement it at all: pf's
route-to and reply-to on an outbound state send it on with the hop limit
it arrived with.
Decrement it right after the check, before the route lookup, as the other
two paths do. Errors sent after that point quote the decremented value,
as they do on those paths.
pf_refragment6() hands the fragments of a forwarded packet it
reassembled to ip6_forward(), which decrements the hop limit again, so
on the slow path such a packet has always lost two hops at this router,
and with this change it would on the fast path as well. Give the hop
back before ip6_forward(), unless ip6stealth is on, in which case
neither path decrements, or the hop limit is 255, which a scrub
(min-ttl 255) rule sets and which has no room for it; such a packet
leaves with 254, as it did before. fragmentation_pass:v6_refrag_hlim
checks both paths, and that such a packet still gets through.
Reviewed by: kp, 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/D61039
---
sys/modules/pf/Makefile | 2 +-
sys/netinet6/ip6_fastfwd.c | 20 +++++----
sys/netpfil/pf/pf_norm.c | 16 +++++++
tests/sys/netpfil/pf/forward.sh | 67 ++++++++++++++++++++++++++++++
tests/sys/netpfil/pf/fragmentation_pass.sh | 14 +++++++
5 files changed, 110 insertions(+), 9 deletions(-)
diff --git a/sys/modules/pf/Makefile b/sys/modules/pf/Makefile
index ad9790704cf1..0ca117bd3d83 100644
--- a/sys/modules/pf/Makefile
+++ b/sys/modules/pf/Makefile
@@ -5,7 +5,7 @@ SRCS= pf.c pf_if.c pf_lb.c pf_osfp.c pf_ioctl.c pf_norm.c pf_table.c \
pf_ruleset.c pf_nl.c pf_nv.c pf_syncookies.c in4_cksum.c inet_nat64.c \
bus_if.h device_if.h \
opt_pf.h opt_inet.h opt_inet6.h opt_bpf.h opt_sctp.h opt_global.h \
- opt_kern_tls.h
+ opt_ipstealth.h opt_kern_tls.h
.if !defined(KERNBUILDDIR)
# pflog can be loaded as a module, have the additional checks turned on
diff --git a/sys/netinet6/ip6_fastfwd.c b/sys/netinet6/ip6_fastfwd.c
index 6256631c78f3..2328a020531e 100644
--- a/sys/netinet6/ip6_fastfwd.c
+++ b/sys/netinet6/ip6_fastfwd.c
@@ -198,6 +198,16 @@ passin:
m = NULL;
goto dropin;
}
+ /*
+ * Decrement it here, before the route lookup and the outbound hooks,
+ * as ip_tryforward() and ip6_forward() do: the hooks then see the
+ * hop limit the packet leaves with, and a hook that transmits the
+ * packet itself (pf route-to) does not skip the decrement.
+ */
+#ifdef IPSTEALTH
+ if (!V_ip6stealth)
+#endif
+ ip6->ip6_hlim -= IPV6_HLIMDEC;
/*
* Find route to destination.
*/
@@ -245,8 +255,8 @@ passin:
*/
if (m->m_flags & M_FASTFWD_OURS) {
/*
- * XXX: we did one hop and should decrement hop limit. But
- * now we are the destination and just don't pay attention.
+ * The hop limit is already decremented, but now we are
+ * the destination and ip6_input() does not look at it.
*/
return (m);
}
@@ -276,12 +286,6 @@ passin:
}
}
passout:
-#ifdef IPSTEALTH
- if (!V_ip6stealth)
-#endif
- {
- ip6->ip6_hlim -= IPV6_HLIMDEC;
- }
/*
* If TCP/UDP header still needs a valid checksum and interface will not
diff --git a/sys/netpfil/pf/pf_norm.c b/sys/netpfil/pf/pf_norm.c
index c3c2e9c47fe4..4683078acd48 100644
--- a/sys/netpfil/pf/pf_norm.c
+++ b/sys/netpfil/pf/pf_norm.c
@@ -31,6 +31,7 @@
#include <sys/cdefs.h>
#include "opt_inet.h"
#include "opt_inet6.h"
+#include "opt_ipstealth.h"
#include "opt_pf.h"
#include <sys/param.h>
@@ -1142,6 +1143,21 @@ pf_refragment6(struct ifnet *ifp, struct mbuf **m0, struct m_tag *mtag,
}
} else if (forward) {
MPASS(m->m_pkthdr.rcvif != NULL);
+ /*
+ * The forwarding path decremented the hop limit before
+ * its outbound hooks, and ip6_forward() would do it
+ * again, so give the hop back. A hop limit raised to
+ * the maximum (scrub min-ttl 255) has no room for it,
+ * and the packet leaves with one less.
+ */
+#ifdef IPSTEALTH
+ if (!V_ip6stealth)
+#endif
+ {
+ hdr = mtod(m, struct ip6_hdr *);
+ if (hdr->ip6_hlim < IPV6_MAXHLIM)
+ hdr->ip6_hlim += IPV6_HLIMDEC;
+ }
ip6_forward(m, 0);
} else {
(void)ip6_output(m, NULL, NULL, 0, NULL, NULL,
diff --git a/tests/sys/netpfil/pf/forward.sh b/tests/sys/netpfil/pf/forward.sh
index a01da5f62e7a..5e4a53cea5c3 100644
--- a/tests/sys/netpfil/pf/forward.sh
+++ b/tests/sys/netpfil/pf/forward.sh
@@ -211,9 +211,76 @@ v6_hlim_cleanup()
pft_cleanup
}
+atf_test_case "v6_hlim_decrement" "cleanup"
+v6_hlim_decrement_head()
+{
+ atf_set descr 'Test that forwarded packets leave with the hop limit decremented once'
+ atf_set require.user root
+ atf_set require.progs python3 scapy
+}
+
+v6_hlim_decrement_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
+ 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:43::/64 2001:db8:42::2
+ route add -6 2001:db8:666::/64 2001:db8:42::2
+
+ jexec alcatraz pfctl -e
+
+ # Forwarded by the stack, with and without the fast path.
+ pft_set_rules alcatraz "pass"
+ for redirect in 0 1; do
+ jexec alcatraz sysctl net.inet6.ip6.redirect=${redirect}
+ atf_check -s exit:0 ${common_dir}/pft_ping.py \
+ --sendif ${epair_send}a \
+ --to 2001:db8:43::3 \
+ --recvif ${epair_recv}a \
+ --send-hlim 5 \
+ --expect-hlim 4
+ done
+
+ # Transmitted by pf's outbound pass (route-to), same expectation.
+ pft_set_rules alcatraz \
+ "pass in" \
+ "pass out route-to (${epair_route}b 2001:db8:44::3) inet6 to 2001:db8:666::1"
+ for redirect in 0 1; do
+ jexec alcatraz sysctl net.inet6.ip6.redirect=${redirect}
+ atf_check -s exit:0 ${common_dir}/pft_ping.py \
+ --sendif ${epair_send}a \
+ --to 2001:db8:666::1 \
+ --recvif ${epair_route}a \
+ --send-hlim 5 \
+ --expect-hlim 4
+ done
+}
+
+v6_hlim_decrement_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"
}
diff --git a/tests/sys/netpfil/pf/fragmentation_pass.sh b/tests/sys/netpfil/pf/fragmentation_pass.sh
index d5310d6eeb58..f523ec2dd1d9 100644
--- a/tests/sys/netpfil/pf/fragmentation_pass.sh
+++ b/tests/sys/netpfil/pf/fragmentation_pass.sh
@@ -202,6 +202,20 @@ v6_refrag_hlim_body()
# The fast path: pf must refragment through ip6_forward().
refrag_hlim 0
+ # The slow path: ip6_forward() has already decremented the hop limit,
+ # and pf must not have it decremented again.
+ refrag_hlim 1
+
+ # A rule that raises the hop limit to the maximum leaves no room to
+ # give the hop back, but the packet must still get through.
+ pft_set_rules alcatraz \
+ "set reassemble yes" \
+ "pass in" \
+ "pass out scrub (min-ttl 255)"
+ for redirect in 0 1; do
+ jexec alcatraz sysctl net.inet6.ip6.redirect=${redirect}
+ atf_check -o ignore ping -6 -c 1 -s 4500 2001:db8:43::3
+ done
}
v6_refrag_hlim_cleanup()