#1# SPDX-License-Identifier: BSD-2-Clause2#3# Copyright (c) 2025 Rubicon Communications, LLC (Netgate)4#5# Redistribution and use in source and binary forms, with or without6# modification, are permitted provided that the following conditions7# are met:8# 1. Redistributions of source code must retain the above copyright9# notice, this list of conditions and the following disclaimer.10# 2. Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13#14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24# SUCH DAMAGE.2526import pytest27from atf_python.sys.net.tools import ToolsHelper28from atf_python.sys.net.vnet import VnetTestTemplate2930class TestNAT44(VnetTestTemplate):31REQUIRED_MODULES = [ "pf" ]32TOPOLOGY = {33"vnet1": {"ifaces": ["if1"]},34"vnet2": {"ifaces": ["if1", "if2"]},35"vnet3": {"ifaces": ["if2"]},36"if1": {"prefixes4": [("192.0.2.2/24", "192.0.2.1/24")]},37"if2": {"prefixes4": [("198.51.100.1/24", "198.51.100.2")]},38}3940def vnet2_handler(self, vnet):41outifname = vnet.iface_alias_map["if2"].name42ToolsHelper.print_output("/sbin/sysctl net.inet.ip.forwarding=1")4344ToolsHelper.print_output("/sbin/pfctl -e")45ToolsHelper.print_output("/sbin/pfctl -x loud")46ToolsHelper.pf_rules([47"set reassemble yes",48"nat on {} inet from 192.0.2.0/24 -> ({})".format(outifname, outifname),49"pass"])5051def vnet3_handler(self, vnet):52pass5354@pytest.mark.require_user("root")55@pytest.mark.require_progs(["scapy"])56def test_nat_igmp(self):57"Verify that NAT translation of !(TCP|UDP|SCTP|ICMP) doesn't panic"58ToolsHelper.print_output("/sbin/route add default 192.0.2.1")59ToolsHelper.print_output("ping -c 3 198.51.100.2")6061# Import in the correct vnet, so at to not confuse Scapy62import scapy.all as sp63import scapy.contrib as sc64import scapy.contrib.igmp6566pkt = sp.IP(dst="198.51.100.2", ttl=64) \67/ sc.igmp.IGMP(type=0x11, mrcode=1)68sp.send(pkt)6970# This time we'll hit an existing state71pkt = sp.IP(dst="198.51.100.2", ttl=64) \72/ sc.igmp.IGMP(type=0x11, mrcode=1)73reply = sp.sr1(pkt, timeout=3)74if reply:75reply.show()767778