pf: fix min-ttl and set-tos for nat64

If we have both af-to and min-ttl or set-tos on a single rule we didn't
apply the new ttl or tos.

That's because the scrub code still applied the change, but we
subsequently create a new header for the new address family. That's done
based on the ttl/tos saved in the struct pf_pdesc, which are the values
from the incoming packet, before the scrub modification(s).

Also update the struct pf_pdesc values when we update packets.

Reported by:	Marek Zarychta
Sponsored by:	Rubicon Communications, LLC ("Netgate")
This commit is contained in:
Kristof Provost
2025-12-15 15:26:57 +01:00
parent f1809eab82
commit cdbc9b76ff
2 changed files with 73 additions and 2 deletions
+4 -2
View File
@@ -2244,14 +2244,14 @@ pf_scrub(struct pf_pdesc *pd)
h->ip_ttl < pd->act.min_ttl) { h->ip_ttl < pd->act.min_ttl) {
u_int16_t ip_ttl = h->ip_ttl; u_int16_t ip_ttl = h->ip_ttl;
h->ip_ttl = pd->act.min_ttl; pd->ttl = h->ip_ttl = pd->act.min_ttl;
h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0); h->ip_sum = pf_cksum_fixup(h->ip_sum, ip_ttl, h->ip_ttl, 0);
} }
#ifdef INET6 #ifdef INET6
/* Enforce a minimum ttl, may cause endless packet loops */ /* Enforce a minimum ttl, may cause endless packet loops */
if (pd->af == AF_INET6 && pd->act.min_ttl && if (pd->af == AF_INET6 && pd->act.min_ttl &&
h6->ip6_hlim < pd->act.min_ttl) h6->ip6_hlim < pd->act.min_ttl)
h6->ip6_hlim = pd->act.min_ttl; pd->ttl = h6->ip6_hlim = pd->act.min_ttl;
#endif /* INET6 */ #endif /* INET6 */
/* Enforce tos */ /* Enforce tos */
if (pd->act.flags & PFSTATE_SETTOS) { if (pd->act.flags & PFSTATE_SETTOS) {
@@ -2261,6 +2261,7 @@ pf_scrub(struct pf_pdesc *pd)
ov = *(u_int16_t *)h; ov = *(u_int16_t *)h;
h->ip_tos = pd->act.set_tos | (h->ip_tos & IPTOS_ECN_MASK); h->ip_tos = pd->act.set_tos | (h->ip_tos & IPTOS_ECN_MASK);
pd->tos = h->ip_tos & ~IPTOS_ECN_MASK;
nv = *(u_int16_t *)h; nv = *(u_int16_t *)h;
h->ip_sum = pf_cksum_fixup(h->ip_sum, ov, nv, 0); h->ip_sum = pf_cksum_fixup(h->ip_sum, ov, nv, 0);
@@ -2270,6 +2271,7 @@ pf_scrub(struct pf_pdesc *pd)
case AF_INET6: case AF_INET6:
h6->ip6_flow &= IPV6_FLOWLABEL_MASK | IPV6_VERSION_MASK; h6->ip6_flow &= IPV6_FLOWLABEL_MASK | IPV6_VERSION_MASK;
h6->ip6_flow |= htonl((pd->act.set_tos | IPV6_ECN(h6)) << 20); h6->ip6_flow |= htonl((pd->act.set_tos | IPV6_ECN(h6)) << 20);
pd->tos = IPV6_DSCP(h6);
break; break;
#endif /* INET6 */ #endif /* INET6 */
} }
+69
View File
@@ -1179,6 +1179,74 @@ v6_gateway_cleanup()
pft_cleanup pft_cleanup
} }
atf_test_case "scrub_min_ttl" "cleanup"
scrub_min_ttl_head()
{
atf_set descr 'Ensure scrub min-ttl applies to nat64 traffic'
atf_set require.user root
}
scrub_min_ttl_body()
{
pft_init
epair=$(vnet_mkepair)
epair_link=$(vnet_mkepair)
epair_link_two=$(vnet_mkepair)
ifconfig ${epair}a inet6 2001:db8::2/64 up no_dad
route -6 add default 2001:db8::1
vnet_mkjail rtr ${epair}b ${epair_link}a
jexec rtr ifconfig ${epair}b inet6 2001:db8::1/64 up no_dad
jexec rtr ifconfig ${epair_link}a 192.0.2.1/24 up
jexec rtr route add default 192.0.2.2
vnet_mkjail rtr2 ${epair_link}b ${epair_link_two}a
jexec rtr2 ifconfig ${epair_link}b 192.0.2.2/24 up
jexec rtr2 ifconfig ${epair_link_two}a 198.51.100.2/24 up
jexec rtr2 sysctl net.inet.ip.forwarding=1
vnet_mkjail dst ${epair_link_two}b
jexec dst ifconfig ${epair_link_two}b 198.51.100.1/24 up
jexec dst route add default 198.51.100.2
# Sanity checks
atf_check -s exit:0 -o ignore \
ping6 -c 1 2001:db8::1
atf_check -s exit:0 -o ignore \
jexec rtr ping -c 1 192.0.2.2
atf_check -s exit:0 -o ignore \
jexec rtr ping -c 1 198.51.100.2
atf_check -s exit:0 -o ignore \
jexec rtr ping -c 1 198.51.100.1
jexec rtr pfctl -e
pft_set_rules rtr \
"pass" \
"pass in on ${epair}b inet6 from any to 64:ff9b::/96 af-to inet from (${epair_link}a)"
# Ping works with a normal TTL
atf_check -s exit:0 -o ignore \
ping6 -c 1 64:ff9b::198.51.100.1
# If we set a TTL of two the packet gets dropped
atf_check -s exit:2 -o ignore \
ping6 -c 1 -m 2 64:ff9b::198.51.100.1
# But if we have pf enforce a minimum ttl of 10 the ping does pass
pft_set_rules rtr \
"pass" \
"pass in on ${epair}b inet6 from any to 64:ff9b::/96 af-to inet from (${epair_link}a) scrub (min-ttl 10)"
atf_check -s exit:0 -o ignore \
ping6 -c 1 -m 2 64:ff9b::198.51.100.1
}
scub_min_ttl_cleanup()
{
pft_cleanup
}
atf_init_test_cases() atf_init_test_cases()
{ {
atf_add_test_case "icmp_echo_in" atf_add_test_case "icmp_echo_in"
@@ -1206,4 +1274,5 @@ atf_init_test_cases()
atf_add_test_case "route_to" atf_add_test_case "route_to"
atf_add_test_case "reply_to" atf_add_test_case "reply_to"
atf_add_test_case "v6_gateway" atf_add_test_case "v6_gateway"
atf_add_test_case "scrub_min_ttl"
} }