From nobody Mon Jan 22 12:52:52 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TJVW85PdLz57rRc; Mon, 22 Jan 2024 12:52:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TJVW84vn7z3xVZ; Mon, 22 Jan 2024 12:52:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705927972; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4pyd/joJSxm7NNwGiT0U2AsKYJSessjj0Xs8PqudYmI=; b=RPCnc0mNq4WTUm7JIb1YDE/cpx3DnNlpHG2nFn/nb0BPe9rYLlVAiFtAFEaIFgMMLg8LN+ j1vEglS/mTMMA15rvqhVrSbdPzd8DTp6xJVqzHJDyY0pRlXdxSZbVSUK48/ruzsPoIKG1c 1SOwXqlYmQoh1EbYP8Kh26MyWmS54mtK35iA6KZcKRlmsz6mLu0++QfnG374LoTS9khHD1 t73UH3tyqn9x8tzzumzFEGhDE90k2gHbViOlA4noRWI+YNl1REnsVBJ/QgD+x5zsBXD/ej xzRj3tjDB2tJ8GHCdhCQt9XCaBFE+ipvlKpJ2INI5PCSA4k3NIvlmhognzwtAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705927972; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4pyd/joJSxm7NNwGiT0U2AsKYJSessjj0Xs8PqudYmI=; b=LgMze+oAs2KDiIC3SLR5XFRa8w1UgvmwLEBKss81dcmtI1XFghN/EPI45LjnGim1lZz3WL S8GpbFCrpUbjPdDOsaiDCGKQg6tno85Wy9o0B9aty1uGhUSgO65r4BGcTrIfIF+m+kVlHd w9FL+BM+QStXA70r4KO7gqissZ2lWGcan9puXzjs7w2+La9pExnBeqYK937YdUaBfjBDtn TWkLdwOjfm0a3PiJVvIQTBqPnzGVyFoz+tyG4+nEAoUW0ld/NGqW7svUIKHSSk9Gs+bv4C s2AoYldUlHriMbv73PGTUa1S9BZL0vGD+sA59OhdmkGM0rIq1hIJNPCrcxYaWQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1705927972; a=rsa-sha256; cv=none; b=x2fxE8tM4RzzdU1yGE1+GhJ6OiEaM99JHlGTLhSJzq58Zxu1osg94Z8DdgjLSxc4sHEReo d+t1IwGwBiuZ4uVrTtGutq+CnOzLVk5FC0YSjY+wkDI5oL9KRZ4L0Qx1tdNDpDN0adzUOk VMoR0cpVo7dWNA2u1qsREsJdqOSg25D3BbEywb7LWf2tCtc9mlfxRJi3B3ge4JGh76YE1V dcex4IskOHRptHJqtOC2LSJ0CSrHnD7ds16L4A/yP6SUnbevoT+0/Tj51gRQA1gNL5m87C 0XUIPvVOfwWYKXrWc5mmnShLsG80CLYMAFSS4cGyMWVZZglG7nZNQ5HQUII7ZA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TJVW83zzHz12xW; Mon, 22 Jan 2024 12:52:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 40MCqqtr047583; Mon, 22 Jan 2024 12:52:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40MCqq7m047580; Mon, 22 Jan 2024 12:52:52 GMT (envelope-from git) Date: Mon, 22 Jan 2024 12:52:52 GMT Message-Id: <202401221252.40MCqq7m047580@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kristof Provost Subject: git: 57c50d6b3673 - main - pf tests: test ICMP6 packet too big with binat List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 57c50d6b3673225064d9c38cd06a41ec1730d3d1 Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=57c50d6b3673225064d9c38cd06a41ec1730d3d1 commit 57c50d6b3673225064d9c38cd06a41ec1730d3d1 Author: Kristof Provost AuthorDate: 2024-01-17 17:03:56 +0000 Commit: Kristof Provost CommitDate: 2024-01-22 12:49:56 +0000 pf tests: test ICMP6 packet too big with binat Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D43500 --- tests/sys/netpfil/pf/Makefile | 1 + tests/sys/netpfil/pf/nat66.py | 166 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) diff --git a/tests/sys/netpfil/pf/Makefile b/tests/sys/netpfil/pf/Makefile index f1a2fff5a45d..30099fdc4c17 100644 --- a/tests/sys/netpfil/pf/Makefile +++ b/tests/sys/netpfil/pf/Makefile @@ -46,6 +46,7 @@ ATF_TESTS_SH+= altq \ tos ATF_TESTS_PYTEST+= frag6.py +ATF_TESTS_PYTEST+= nat66.py ATF_TESTS_PYTEST+= sctp.py # Tests reuse jail names and so cannot run in parallel. diff --git a/tests/sys/netpfil/pf/nat66.py b/tests/sys/netpfil/pf/nat66.py new file mode 100644 index 000000000000..006ab5afee7f --- /dev/null +++ b/tests/sys/netpfil/pf/nat66.py @@ -0,0 +1,166 @@ +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2024 Rubicon Communications, LLC (Netgate) +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + +import ctypes +import ipaddress +import pytest +import re +import socket +import threading +import time +from atf_python.sys.net.tools import ToolsHelper +from atf_python.sys.net.vnet import VnetTestTemplate + +class DelayedSend(threading.Thread): + def __init__(self, packet): + threading.Thread.__init__(self) + self._packet = packet + + self.start() + + def run(self): + import scapy.all as sp + time.sleep(1) + sp.send(self._packet) + +class TestNAT66(VnetTestTemplate): + REQUIRED_MODUES = [ "pf" ] + TOPOLOGY = { + "vnet1": {"ifaces": ["if1"]}, + "vnet2": {"ifaces": ["if1", "if2"]}, + "vnet3": {"ifaces": ["if2"]}, + "if1": {"prefixes6": [("2001:db8::2/64", "2001:db8::1/64")]}, + "if2": {"prefixes6": [("2001:db8:1::1/64", "2001:db8:1::2/64")]}, + } + + def vnet2_handler(self, vnet): + ifname = vnet.iface_alias_map["if1"].name + ToolsHelper.print_output("/sbin/ifconfig %s mtu 9000" % ifname) + + ToolsHelper.print_output("/sbin/pfctl -e") + ToolsHelper.pf_rules([ + "set reassemble yes", + "binat inet6 from 2001:db8::/64 to 2001:db8:1::/64 -> 2001:db8:42::/64", + "binat inet6 from 2001:db8:1::/64 to 2001:db8:42::/64 -> 2001:db8::/64", + "pass inet6 proto icmp6"]) + + ToolsHelper.print_output("/sbin/sysctl net.inet6.ip6.forwarding=1") + + def vnet3_handler(self, vnet): + ToolsHelper.print_output("/sbin/route add -6 2001:db8:42::/64 2001:db8:1::1") + + def check_icmp_too_big(self, sp, payload_size, frag_size=None): + packet = sp.IPv6(src="2001:db8::2", dst="2001:db8:1::2") \ + / sp.ICMPv6EchoRequest(data=sp.raw(bytes.fromhex('f0') * payload_size)) + + if frag_size is not None: + packet = sp.fragment6(packet, frag_size) + + # Delay the send so the sniffer is running when we transmit. + s = DelayedSend(packet) + + packets = sp.sniff(iface=self.vnet.iface_alias_map["if1"].name, + timeout=3) + found=False + for p in packets: + # We can't get a reply to this + assert not p.getlayer(sp.ICMPv6EchoReply) + + if not p.getlayer(sp.ICMPv6PacketTooBig): + continue + + ip6 = p.getlayer(sp.IPv6) + icmp6 = p.getlayer(sp.ICMPv6PacketTooBig) + + # Error is from the router vnet + assert ip6.src == "2001:db8::1" + assert ip6.dst == "2001:db8::2" + + # And the relevant MTU is 1500 + assert icmp6.mtu == 1500 + + # The icmp6 error contains our original IPv6 packet + err = icmp6.getlayer(sp.IPerror6) + assert err.src == "2001:db8::2" + assert err.dst == "2001:db8:1::2" + assert err.nh == 58 + + found = True + + assert found + + def check_icmp_echo(self, sp, payload_size): + packet = sp.IPv6(src="2001:db8::2", dst="2001:db8:1::2") \ + / sp.ICMPv6EchoRequest(data=sp.raw(bytes.fromhex('f0') * payload_size)) + + # Delay the send so the sniffer is running when we transmit. + s = DelayedSend(packet) + + packets = sp.sniff(iface=self.vnet.iface_alias_map["if1"].name, + timeout=3) + found=False + for p in packets: + if not p.getlayer(sp.ICMPv6EchoReply): + continue + + ip6 = p.getlayer(sp.IPv6) + icmp6 = p.getlayer(sp.ICMPv6EchoReply) + + # Error is from the router vnet + assert ip6.src == "2001:db8:1::2" + assert ip6.dst == "2001:db8::2" + + found = True + + assert found + + @pytest.mark.require_user("root") + def test_npt_icmp(self): + cl_vnet = self.vnet_map["vnet1"] + ifname = cl_vnet.iface_alias_map["if1"].name + ToolsHelper.print_output("/sbin/ifconfig %s mtu 9000" % ifname) + + ToolsHelper.print_output("/sbin/route add -6 2001:db8:1::/64 2001:db8::1") + + # For unclear reasons vnet3 doesn't respond to the first ping. + # Just send two for now. + ToolsHelper.print_output("/sbin/ping -6 -c 1 2001:db8:1::2") + ToolsHelper.print_output("/sbin/ping -6 -c 1 2001:db8:1::2") + + # Import in the correct vnet, so at to not confuse Scapy + import scapy.all as sp + + # A ping that easily passes without fragmentation + self.check_icmp_echo(sp, 128) + + # Send a ping that just barely doesn't need to be fragmented + self.check_icmp_echo(sp, 1452) + + # Send a ping that just barely needs to be fragmented + self.check_icmp_too_big(sp, 1453) + + # A ping that arrives fragmented + self.check_icmp_too_big(sp, 12000, 5000)