xref: /freebsd/tests/sys/netpfil/pf/frag4.py (revision 8a309785c9b186c809a5d4b017fc8cf849af1ddd)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2025 Rubicon Communications, LLC (Netgate)
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26
27import pytest
28from utils import DelayedSend
29from atf_python.sys.net.tools import ToolsHelper
30from atf_python.sys.net.vnet import VnetTestTemplate
31
32class TestFrag4_NoReassemble(VnetTestTemplate):
33    REQUIRED_MODULES = [ "pf" ]
34    TOPOLOGY = {
35        "vnet1": {"ifaces": ["if1"]},
36        "vnet2": {"ifaces": ["if1", "if2"]},
37        "vnet3": {"ifaces": ["if2"]},
38        "if1": {"prefixes4": [("192.0.2.1/24", "192.0.2.2/24")]},
39        "if2": {"prefixes4": [("198.51.100.1/24", "198.51.100.2/24")]},
40    }
41
42    def vnet2_handler(self, vnet):
43        outifname = vnet.iface_alias_map["if2"].name
44
45        ToolsHelper.print_output("/sbin/pfctl -e")
46        ToolsHelper.pf_rules([
47            "set reassemble no",
48            "nat on %s from 192.0.2.0/24 to any -> (%s)" % (outifname, outifname),
49            "pass out"
50        ])
51
52        ToolsHelper.print_output("/sbin/sysctl net.inet.ip.forwarding=1")
53
54    def vnet3_handler(self, vnet):
55        # We deliberately don't set the default gateway here, so if we get a
56        # reply from this we know we did NAT in vnet2
57        pass
58
59    @pytest.mark.require_user("root")
60    @pytest.mark.require_progs(["scapy"])
61    def test_udp_frag(self):
62        ToolsHelper.print_output("/sbin/route add default 192.0.2.2")
63        ToolsHelper.print_output("/sbin/ping -c 3 198.51.100.2")
64
65        # Import in the correct vnet, so at to not confuse Scapy
66        import scapy.all as sp
67
68        pkt = sp.IP(dst="198.51.100.2", frag=123) \
69            / sp.UDP(dport=12345, sport=54321)
70        reply = sp.sr1(pkt, timeout=3)
71        # We don't expect a reply
72        assert not reply
73