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 ctypes 28import ipaddress 29import pytest 30import re 31import socket 32import threading 33import time 34from atf_python.sys.net.tools import ToolsHelper 35from atf_python.sys.net.vnet import VnetTestTemplate 36 37class DelayedSend(threading.Thread): 38 def __init__(self, packet): 39 threading.Thread.__init__(self) 40 self._packet = packet 41 42 self.start() 43 44 def run(self): 45 import scapy.all as sp 46 time.sleep(1) 47 sp.send(self._packet) 48 49class TestNAT66(VnetTestTemplate): 50 REQUIRED_MODUES = [ "pf" ] 51 TOPOLOGY = { 52 "vnet1": {"ifaces": ["if1"]}, 53 "vnet2": {"ifaces": ["if1", "if2"]}, 54 "vnet3": {"ifaces": ["if2"]}, 55 "if1": {"prefixes6": [("2001:db8::2/64", "2001:db8::1/64")]}, 56 "if2": {"prefixes6": [("2001:db8:1::1/64", "2001:db8:1::2/64")]}, 57 } 58 59 def vnet2_handler(self, vnet): 60 ifname = vnet.iface_alias_map["if1"].name 61 ToolsHelper.print_output("/sbin/ifconfig %s mtu 9000" % ifname) 62 63 ToolsHelper.print_output("/sbin/pfctl -e") 64 ToolsHelper.pf_rules([ 65 "set reassemble yes", 66 "binat inet6 from 2001:db8::/64 to 2001:db8:1::/64 -> 2001:db8:42::/64", 67 "binat inet6 from 2001:db8:1::/64 to 2001:db8:42::/64 -> 2001:db8::/64", 68 "pass inet6 proto icmp6"]) 69 70 ToolsHelper.print_output("/sbin/sysctl net.inet6.ip6.forwarding=1") 71 72 def vnet3_handler(self, vnet): 73 ToolsHelper.print_output("/sbin/route add -6 2001:db8:42::/64 2001:db8:1::1") 74 75 def check_icmp_too_big(self, sp, payload_size, frag_size=None): 76 packet = sp.IPv6(src="2001:db8::2", dst="2001:db8:1::2") \ 77 / sp.ICMPv6EchoRequest(data=sp.raw(bytes.fromhex('f0') * payload_size)) 78 79 if frag_size is not None: 80 packet = sp.fragment6(packet, frag_size) 81 82 # Delay the send so the sniffer is running when we transmit. 83 s = DelayedSend(packet) 84 85 packets = sp.sniff(iface=self.vnet.iface_alias_map["if1"].name, 86 timeout=3) 87 found=False 88 for p in packets: 89 # We can't get a reply to this 90 assert not p.getlayer(sp.ICMPv6EchoReply) 91 92 if not p.getlayer(sp.ICMPv6PacketTooBig): 93 continue 94 95 ip6 = p.getlayer(sp.IPv6) 96 icmp6 = p.getlayer(sp.ICMPv6PacketTooBig) 97 98 # Error is from the router vnet 99 assert ip6.src == "2001:db8::1" 100 assert ip6.dst == "2001:db8::2" 101 102 # And the relevant MTU is 1500 103 assert icmp6.mtu == 1500 104 105 # The icmp6 error contains our original IPv6 packet 106 err = icmp6.getlayer(sp.IPerror6) 107 assert err.src == "2001:db8::2" 108 assert err.dst == "2001:db8:1::2" 109 assert err.nh == 58 110 111 found = True 112 113 assert found 114 115 def check_icmp_echo(self, sp, payload_size): 116 packet = sp.IPv6(src="2001:db8::2", dst="2001:db8:1::2") \ 117 / sp.ICMPv6EchoRequest(data=sp.raw(bytes.fromhex('f0') * payload_size)) 118 119 # Delay the send so the sniffer is running when we transmit. 120 s = DelayedSend(packet) 121 122 packets = sp.sniff(iface=self.vnet.iface_alias_map["if1"].name, 123 timeout=3) 124 found=False 125 for p in packets: 126 if not p.getlayer(sp.ICMPv6EchoReply): 127 continue 128 129 ip6 = p.getlayer(sp.IPv6) 130 icmp6 = p.getlayer(sp.ICMPv6EchoReply) 131 132 # Error is from the router vnet 133 assert ip6.src == "2001:db8:1::2" 134 assert ip6.dst == "2001:db8::2" 135 136 found = True 137 138 assert found 139 140 @pytest.mark.require_user("root") 141 def test_npt_icmp(self): 142 cl_vnet = self.vnet_map["vnet1"] 143 ifname = cl_vnet.iface_alias_map["if1"].name 144 ToolsHelper.print_output("/sbin/ifconfig %s mtu 9000" % ifname) 145 146 ToolsHelper.print_output("/sbin/route add -6 2001:db8:1::/64 2001:db8::1") 147 148 # For unclear reasons vnet3 doesn't respond to the first ping. 149 # Just send two for now. 150 ToolsHelper.print_output("/sbin/ping -6 -c 1 2001:db8:1::2") 151 ToolsHelper.print_output("/sbin/ping -6 -c 1 2001:db8:1::2") 152 153 # Import in the correct vnet, so at to not confuse Scapy 154 import scapy.all as sp 155 156 # A ping that easily passes without fragmentation 157 self.check_icmp_echo(sp, 128) 158 159 # Send a ping that just barely doesn't need to be fragmented 160 self.check_icmp_echo(sp, 1452) 161 162 # Send a ping that just barely needs to be fragmented 163 self.check_icmp_too_big(sp, 1453) 164 165 # A ping that arrives fragmented 166 self.check_icmp_too_big(sp, 12000, 5000) 167