The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=32f793e2297619e1cb835c0938396ab6f6d0a6b3
commit 32f793e2297619e1cb835c0938396ab6f6d0a6b3 Author: Kristof Provost <k...@freebsd.org> AuthorDate: 2025-06-27 08:18:18 +0000 Commit: Kristof Provost <k...@freebsd.org> CommitDate: 2025-06-27 11:12:50 +0000 pf tests: test handling of ICMP echo requests with the same ID We previously broke this, so add a test to prevent regressions. Sponsored by: Rubicon Communications, LLC ("Netgate") --- tests/sys/netpfil/pf/icmp.py | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/sys/netpfil/pf/icmp.py b/tests/sys/netpfil/pf/icmp.py index 232c56a23dbf..83096886691e 100644 --- a/tests/sys/netpfil/pf/icmp.py +++ b/tests/sys/netpfil/pf/icmp.py @@ -175,3 +175,80 @@ class TestICMP(VnetTestTemplate): self.check_icmp_echo(sp, 128) self.check_icmp_echo(sp, 1464) self.check_icmp_echo(sp, 1468) + +class TestICMP_NAT(VnetTestTemplate): + REQUIRED_MODULES = [ "pf" ] + TOPOLOGY = { + "vnet1": {"ifaces": ["if1"]}, + "vnet2": {"ifaces": ["if1", "if2"]}, + "vnet3": {"ifaces": ["if2"]}, + "if1": {"prefixes4": [("192.0.2.2/24", "192.0.2.1/24")]}, + "if2": {"prefixes4": [("198.51.100.1/24", "198.51.100.2/24")]}, + } + + def vnet2_handler(self, vnet): + ifname = vnet.iface_alias_map["if1"].name + if2name = vnet.iface_alias_map["if2"].name + + ToolsHelper.print_output("/sbin/pfctl -e") + ToolsHelper.pf_rules([ + "set reassemble yes", + "set state-policy if-bound", + "nat on %s inet from 192.0.2.0/24 to any -> (%s)" % (if2name, if2name), + "block", + "pass inet proto icmp icmp-type echoreq", + ]) + + ToolsHelper.print_output("/sbin/sysctl net.inet.ip.forwarding=1") + ToolsHelper.print_output("/sbin/pfctl -x loud") + + def vnet3_handler(self, vnet): + ifname = vnet.iface_alias_map["if2"].name + ToolsHelper.print_output("/sbin/ifconfig %s inet alias 198.51.100.3/24" % ifname) + + @pytest.mark.require_user("root") + @pytest.mark.require_progs(["scapy"]) + def test_id_conflict(self): + """ + Test ICMP echo requests with the same ID from different clients. + Windows does this, and it can confuse pf. + """ + ifname = self.vnet.iface_alias_map["if1"].name + ToolsHelper.print_output("/sbin/route add default 192.0.2.1") + ToolsHelper.print_output("/sbin/ifconfig %s inet alias 192.0.2.3/24" % ifname) + + ToolsHelper.print_output("/sbin/ping -c 1 192.0.2.1") + ToolsHelper.print_output("/sbin/ping -c 1 198.51.100.1") + ToolsHelper.print_output("/sbin/ping -c 1 198.51.100.2") + ToolsHelper.print_output("/sbin/ping -c 1 198.51.100.3") + + # Import in the correct vnet, so at to not confuse Scapy + import scapy.all as sp + + # Address one + packet = sp.IP(src="192.0.2.2", dst="198.51.100.2", flags="DF") \ + / sp.ICMP(type='echo-request', id=42) \ + / sp.raw(bytes.fromhex('f0') * 16) + + p = sp.sr1(packet, timeout=3) + p.show() + ip = p.getlayer(sp.IP) + icmp = p.getlayer(sp.ICMP) + assert ip + assert icmp + assert ip.dst == "192.0.2.2" + assert icmp.id == 42 + + # Address one + packet = sp.IP(src="192.0.2.3", dst="198.51.100.2", flags="DF") \ + / sp.ICMP(type='echo-request', id=42) \ + / sp.raw(bytes.fromhex('f0') * 16) + + p = sp.sr1(packet, timeout=3) + p.show() + ip = p.getlayer(sp.IP) + icmp = p.getlayer(sp.ICMP) + assert ip + assert icmp + assert ip.dst == "192.0.2.3" + assert icmp.id == 42