xref: /freebsd/tests/sys/netpfil/pf/icmp.py (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2024 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
28import subprocess
29import re
30from atf_python.sys.net.tools import ToolsHelper
31from atf_python.sys.net.vnet import VnetTestTemplate
32
33def check(cmd):
34    ps = subprocess.Popen(cmd, shell=True)
35    ret = ps.wait()
36    if ret != 0:
37        raise Exception("Command %s returned %d" % (cmd, ret))
38
39class TestICMP(VnetTestTemplate):
40    REQUIRED_MODULES = [ "pf" ]
41    TOPOLOGY = {
42        "vnet1": {"ifaces": ["if1"]},
43        "vnet2": {"ifaces": ["if1", "if2"]},
44        "vnet3": {"ifaces": ["if2"]},
45        "if1": {"prefixes4": [("192.0.2.2/24", "192.0.2.1/24")]},
46        "if2": {"prefixes4": [("198.51.100.1/24", "198.51.100.2/24")]},
47    }
48
49    def vnet2_handler(self, vnet):
50        ifname = vnet.iface_alias_map["if1"].name
51
52        ToolsHelper.print_output("/sbin/pfctl -e")
53        ToolsHelper.pf_rules([
54            "set reassemble yes",
55            "block",
56            "pass inet proto icmp icmp-type echoreq",
57            ])
58
59        ToolsHelper.print_output("/sbin/sysctl net.inet.ip.forwarding=1")
60        ToolsHelper.print_output("/sbin/pfctl -x loud")
61
62    def vnet3_handler(self, vnet):
63        # Import in the correct vnet, so at to not confuse Scapy
64        import scapy.all as sp
65
66        ifname = vnet.iface_alias_map["if2"].name
67        ToolsHelper.print_output("/sbin/route add default 198.51.100.1")
68        ToolsHelper.print_output("/sbin/ifconfig %s inet alias 198.51.100.3/24" % ifname)
69
70        def checkfn(packet):
71            icmp = packet.getlayer(sp.ICMP)
72            if not icmp:
73                return False
74
75            if icmp.type != 3:
76                return False
77
78            packet.show()
79            return True
80
81        sp.sniff(iface=ifname, stop_filter=checkfn)
82        vnet.pipe.send("Got ICMP destination unreachable packet")
83
84    @pytest.mark.require_user("root")
85    def test_inner_match(self):
86        vnet = self.vnet_map["vnet1"]
87        dst_vnet = self.vnet_map["vnet3"]
88        sendif = vnet.iface_alias_map["if1"].name
89
90        our_mac = ToolsHelper.get_output("/sbin/ifconfig %s ether | awk '/ether/ { print $2; }'" % sendif)
91        dst_mac = re.sub("0a$", "0b", our_mac)
92
93        # Import in the correct vnet, so at to not confuse Scapy
94        import scapy.all as sp
95
96        ToolsHelper.print_output("/sbin/route add default 192.0.2.1")
97
98        # Sanity check
99        check("/sbin/ping -c 1 192.0.2.1")
100        check("/sbin/ping -c 1 198.51.100.1")
101        check("/sbin/ping -c 2 198.51.100.3")
102
103        # Establish a state
104        pkt = sp.Ether(src=our_mac, dst=dst_mac) \
105            / sp.IP(src="192.0.2.2", dst="198.51.100.2") \
106            / sp.ICMP(type='echo-request') \
107            / "PAYLOAD"
108        sp.sendp(pkt, sendif, verbose=False)
109
110        # Now try to pass an ICMP error message piggy-backing on that state, but
111        # use a different source address
112        pkt = sp.Ether(src=our_mac, dst=dst_mac) \
113            / sp.IP(src="192.0.2.2", dst="198.51.100.3") \
114            / sp.ICMP(type='dest-unreach') \
115            / sp.IP(src="198.51.100.2", dst="192.0.2.2") \
116            / sp.ICMP(type='echo-reply')
117        sp.sendp(pkt, sendif, verbose=False)
118
119        try:
120            rcvd = self.wait_object(dst_vnet.pipe, timeout=1)
121            if rcvd:
122                raise Exception(rcvd)
123        except TimeoutError as e:
124            # We expect the timeout here. It means we didn't get the destination
125            # unreachable packet in vnet3
126            pass
127