git: 9566d9272600 - main - pf: fix packet-to-big for route-to as well

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Tue, 27 Feb 2024 15:26:00 UTC
The branch main has been updated by kp:

URL: https://cgit.FreeBSD.org/src/commit/?id=9566d9272600a472be3a608ea79197c57cad1dc3

commit 9566d9272600a472be3a608ea79197c57cad1dc3
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2024-02-27 13:24:25 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2024-02-27 13:38:38 +0000

    pf: fix packet-to-big for route-to as well
    
    When we handle a packet via route-to (i.e. pf_route6()) we still need to
    verify the MTU. However, we only run that check in the forwarding case.
    
    Set the PFIL_FWD tag when running the pf_test6(PF_OUT) check from
    pf_route6(). We are in fact forwarding, so should call the test function
    as such. This will cause us to run the MTU check, and generate an ICMP6
    packet-too-big error when required.
    
    See also:       54c62e3e5d8cd90c5571a1d4c8c5f062d580480e
    See also:       f1c0030bb05cfa01bdd500e50befbb425fecc4c4
    See also:       https://redmine.pfsense.org/issues/14290
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
---
 sys/netpfil/pf/pf.c           |  4 ++--
 tests/sys/netpfil/pf/nat66.py | 47 +++++++++++++++++++++++++++++++++++--------
 2 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c
index 8bfda12b2b2e..46f01f986d05 100644
--- a/sys/netpfil/pf/pf.c
+++ b/sys/netpfil/pf/pf.c
@@ -7333,7 +7333,7 @@ pf_route(struct mbuf **m, struct pf_krule *r, struct ifnet *oifp,
 		goto bad;
 
 	if (pd->dir == PF_IN) {
-		if (pf_test(PF_OUT, 0, ifp, &m0, inp, &pd->act) != PF_PASS)
+		if (pf_test(PF_OUT, PFIL_FWD, ifp, &m0, inp, &pd->act) != PF_PASS)
 			goto bad;
 		else if (m0 == NULL)
 			goto done;
@@ -7555,7 +7555,7 @@ pf_route6(struct mbuf **m, struct pf_krule *r, struct ifnet *oifp,
 		goto bad;
 
 	if (pd->dir == PF_IN) {
-		if (pf_test6(PF_OUT, 0, ifp, &m0, inp, &pd->act) != PF_PASS)
+		if (pf_test6(PF_OUT, PFIL_FWD, ifp, &m0, inp, &pd->act) != PF_PASS)
 			goto bad;
 		else if (m0 == NULL)
 			goto done;
diff --git a/tests/sys/netpfil/pf/nat66.py b/tests/sys/netpfil/pf/nat66.py
index 006ab5afee7f..12d3cb25af0a 100644
--- a/tests/sys/netpfil/pf/nat66.py
+++ b/tests/sys/netpfil/pf/nat66.py
@@ -59,21 +59,23 @@ class TestNAT66(VnetTestTemplate):
     def vnet2_handler(self, vnet):
         ifname = vnet.iface_alias_map["if1"].name
         ToolsHelper.print_output("/sbin/ifconfig %s mtu 9000" % ifname)
+        outifname = vnet.iface_alias_map["if2"].name
 
         ToolsHelper.print_output("/sbin/pfctl -e")
         ToolsHelper.pf_rules([
             "set reassemble yes",
             "binat inet6 from 2001:db8::/64 to 2001:db8:1::/64 -> 2001:db8:42::/64",
             "binat inet6 from 2001:db8:1::/64 to 2001:db8:42::/64 -> 2001:db8::/64",
-            "pass inet6 proto icmp6"])
+            "pass inet6 proto icmp6",
+            "pass in route-to ( %s 2001:db8:1::2 ) inet6 from 2001:db8::3 to 2001:db8:1::/64" % outifname])
 
         ToolsHelper.print_output("/sbin/sysctl net.inet6.ip6.forwarding=1")
 
     def vnet3_handler(self, vnet):
         ToolsHelper.print_output("/sbin/route add -6 2001:db8:42::/64 2001:db8:1::1")
 
-    def check_icmp_too_big(self, sp, payload_size, frag_size=None):
-        packet = sp.IPv6(src="2001:db8::2", dst="2001:db8:1::2") \
+    def check_icmp_too_big(self, sp, payload_size, frag_size=None, src="2001:db8::2"):
+        packet = sp.IPv6(src=src, dst="2001:db8:1::2") \
             / sp.ICMPv6EchoRequest(data=sp.raw(bytes.fromhex('f0') * payload_size))
 
         if frag_size is not None:
@@ -97,14 +99,14 @@ class TestNAT66(VnetTestTemplate):
 
             # Error is from the router vnet
             assert ip6.src == "2001:db8::1"
-            assert ip6.dst == "2001:db8::2"
+            assert ip6.dst == src
 
             # And the relevant MTU is 1500
             assert icmp6.mtu == 1500
 
             # The icmp6 error contains our original IPv6 packet
             err = icmp6.getlayer(sp.IPerror6)
-            assert err.src == "2001:db8::2"
+            assert err.src == src
             assert err.dst == "2001:db8:1::2"
             assert err.nh == 58
 
@@ -112,8 +114,8 @@ class TestNAT66(VnetTestTemplate):
 
         assert found
 
-    def check_icmp_echo(self, sp, payload_size):
-        packet = sp.IPv6(src="2001:db8::2", dst="2001:db8:1::2") \
+    def check_icmp_echo(self, sp, payload_size, src="2001:db8::2"):
+        packet = sp.IPv6(src=src, dst="2001:db8:1::2") \
             / sp.ICMPv6EchoRequest(data=sp.raw(bytes.fromhex('f0') * payload_size))
 
         # Delay the send so the sniffer is running when we transmit.
@@ -131,7 +133,7 @@ class TestNAT66(VnetTestTemplate):
 
             # Error is from the router vnet
             assert ip6.src == "2001:db8:1::2"
-            assert ip6.dst == "2001:db8::2"
+            assert ip6.dst == src
 
             found = True
 
@@ -164,3 +166,32 @@ class TestNAT66(VnetTestTemplate):
 
         # A ping that arrives fragmented
         self.check_icmp_too_big(sp, 12000, 5000)
+
+    @pytest.mark.require_user("root")
+    def test_npt_route_to_icmp(self):
+        cl_vnet = self.vnet_map["vnet1"]
+        ifname = cl_vnet.iface_alias_map["if1"].name
+        ToolsHelper.print_output("/sbin/ifconfig %s mtu 9000" % ifname)
+        ToolsHelper.print_output("/sbin/ifconfig %s inet6 alias 2001:db8::3/64" % ifname)
+
+        ToolsHelper.print_output("/sbin/route add -6 2001:db8:1::/64 2001:db8::1")
+
+        # For unclear reasons vnet3 doesn't respond to the first ping.
+        # Just send two for now.
+        ToolsHelper.print_output("/sbin/ping -6 -c 1 -S 2001:db8::3 2001:db8:1::2")
+        ToolsHelper.print_output("/sbin/ping -6 -c 1 -S 2001:db8::3 2001:db8:1::2")
+
+        # Import in the correct vnet, so at to not confuse Scapy
+        import scapy.all as sp
+
+        # A ping that easily passes without fragmentation
+        self.check_icmp_echo(sp, 128, src="2001:db8::3")
+
+        # Send a ping that just barely doesn't need to be fragmented
+        self.check_icmp_echo(sp, 1452, src="2001:db8::3")
+
+        # Send a ping that just barely needs to be fragmented
+        self.check_icmp_too_big(sp, 1453, src="2001:db8::3")
+
+        # A ping that arrives fragmented
+        self.check_icmp_too_big(sp, 12000, 5000, src="2001:db8::3")