15ab15157SDoug Rabson# 25ab15157SDoug Rabson# SPDX-License-Identifier: BSD-2-Clause 35ab15157SDoug Rabson# 45ab15157SDoug Rabson# Copyright (c) 2018 Kristof Provost <kp@FreeBSD.org> 55ab15157SDoug Rabson# 65ab15157SDoug Rabson# Redistribution and use in source and binary forms, with or without 75ab15157SDoug Rabson# modification, are permitted provided that the following conditions 85ab15157SDoug Rabson# are met: 95ab15157SDoug Rabson# 1. Redistributions of source code must retain the above copyright 105ab15157SDoug Rabson# notice, this list of conditions and the following disclaimer. 115ab15157SDoug Rabson# 2. Redistributions in binary form must reproduce the above copyright 125ab15157SDoug Rabson# notice, this list of conditions and the following disclaimer in the 135ab15157SDoug Rabson# documentation and/or other materials provided with the distribution. 145ab15157SDoug Rabson# 155ab15157SDoug Rabson# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 165ab15157SDoug Rabson# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 175ab15157SDoug Rabson# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 185ab15157SDoug Rabson# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 195ab15157SDoug Rabson# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 205ab15157SDoug Rabson# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 215ab15157SDoug Rabson# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 225ab15157SDoug Rabson# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 235ab15157SDoug Rabson# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 245ab15157SDoug Rabson# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 255ab15157SDoug Rabson# SUCH DAMAGE. 265ab15157SDoug Rabson 275ab15157SDoug Rabson. $(atf_get_srcdir)/utils.subr 285ab15157SDoug Rabson. $(atf_get_srcdir)/runner.subr 295ab15157SDoug Rabson 305ab15157SDoug Rabsonbasic_head() 315ab15157SDoug Rabson{ 325ab15157SDoug Rabson atf_set descr 'Basic IPv4 NAT test' 335ab15157SDoug Rabson atf_set require.user root 345ab15157SDoug Rabson} 355ab15157SDoug Rabson 365ab15157SDoug Rabsonbasic_body() 375ab15157SDoug Rabson{ 385ab15157SDoug Rabson firewall=$1 395ab15157SDoug Rabson firewall_init $firewall 405ab15157SDoug Rabson nat_init $firewall 415ab15157SDoug Rabson 425ab15157SDoug Rabson epair=$(vnet_mkepair) 435ab15157SDoug Rabson 445ab15157SDoug Rabson vnet_mkjail alcatraz ${epair}b 455ab15157SDoug Rabson 465ab15157SDoug Rabson ifconfig ${epair}a 192.0.2.2/24 up 475ab15157SDoug Rabson route add -net 198.51.100.0/24 192.0.2.1 485ab15157SDoug Rabson 495ab15157SDoug Rabson jexec alcatraz ifconfig ${epair}b 192.0.2.1/24 up 505ab15157SDoug Rabson jexec alcatraz sysctl net.inet.ip.forwarding=1 515ab15157SDoug Rabson 525ab15157SDoug Rabson # Enable redirect filter rule 535ab15157SDoug Rabson firewall_config alcatraz ${firewall} \ 545ab15157SDoug Rabson "pf" \ 555ab15157SDoug Rabson "rdr pass on ${epair}b proto tcp from any to 198.51.100.0/24 port 1234 -> 192.0.2.1 port 4321" \ 565ab15157SDoug Rabson "ipfnat" \ 575ab15157SDoug Rabson "rdr ${epair}b from any to 198.51.100.0/24 port = 1234 -> 192.0.2.1 port 4321 tcp" 585ab15157SDoug Rabson 595ab15157SDoug Rabson 605ab15157SDoug Rabson echo "foo" | jexec alcatraz nc -N -l 4321 & 615ab15157SDoug Rabson sleep 1 625ab15157SDoug Rabson 635ab15157SDoug Rabson result=$(nc -N -w 3 198.51.100.2 1234) 645ab15157SDoug Rabson if [ "$result" != "foo" ]; then 655ab15157SDoug Rabson atf_fail "Redirect failed" 665ab15157SDoug Rabson fi 675ab15157SDoug Rabson} 685ab15157SDoug Rabson 695ab15157SDoug Rabsonbasic_cleanup() 705ab15157SDoug Rabson{ 715ab15157SDoug Rabson firewall=$1 725ab15157SDoug Rabson firewall_cleanup $firewall 735ab15157SDoug Rabson} 745ab15157SDoug Rabson 755ab15157SDoug Rabsonlocal_redirect_head() 765ab15157SDoug Rabson{ 775ab15157SDoug Rabson atf_set descr 'Redirect local traffic test' 785ab15157SDoug Rabson atf_set require.user root 795ab15157SDoug Rabson} 805ab15157SDoug Rabson 815ab15157SDoug Rabsonlocal_redirect_body() 825ab15157SDoug Rabson{ 835ab15157SDoug Rabson firewall=$1 845ab15157SDoug Rabson firewall_init $firewall 855ab15157SDoug Rabson nat_init $firewall 86*480ad405SKristof Provost vnet_init_bridge 875ab15157SDoug Rabson 885ab15157SDoug Rabson bridge=$(vnet_mkbridge) 895ab15157SDoug Rabson ifconfig ${bridge} 192.0.2.1/24 up 905ab15157SDoug Rabson 915ab15157SDoug Rabson epair1=$(vnet_mkepair) 925ab15157SDoug Rabson epair2=$(vnet_mkepair) 935ab15157SDoug Rabson 945ab15157SDoug Rabson vnet_mkjail first ${epair1}b 955ab15157SDoug Rabson ifconfig ${epair1}a up 965ab15157SDoug Rabson ifconfig ${bridge} addm ${epair1}a 975ab15157SDoug Rabson jexec first ifconfig ${epair1}b 192.0.2.2/24 up 985ab15157SDoug Rabson jexec first ifconfig lo0 127.0.0.1/8 up 995ab15157SDoug Rabson 1005ab15157SDoug Rabson vnet_mkjail second ${epair2}b 1015ab15157SDoug Rabson ifconfig ${epair2}a up 1025ab15157SDoug Rabson ifconfig ${bridge} addm ${epair2}a 1035ab15157SDoug Rabson jexec second ifconfig ${epair2}b 192.0.2.3/24 up 1045ab15157SDoug Rabson jexec second ifconfig lo0 127.0.0.1/8 up 1055ab15157SDoug Rabson jexec second sysctl net.inet.ip.forwarding=1 1065ab15157SDoug Rabson 1075ab15157SDoug Rabson # Enable redirect filter rule 1085ab15157SDoug Rabson firewall_config second ${firewall} \ 1095ab15157SDoug Rabson "pf" \ 1105ab15157SDoug Rabson "rdr pass proto tcp from any to 192.0.2.3/24 port 1234 -> 192.0.2.2 port 4321" \ 1115ab15157SDoug Rabson "ipfnat" \ 1125ab15157SDoug Rabson "rdr '*' from any to 192.0.2.3/24 port = 1234 -> 192.0.2.2 port 4321 tcp" 1135ab15157SDoug Rabson 1145ab15157SDoug Rabson echo "foo" | jexec first nc -N -l 4321 & 1155ab15157SDoug Rabson sleep 1 1165ab15157SDoug Rabson 1175ab15157SDoug Rabson # Verify that second can use its rule to redirect local connections to first 1185ab15157SDoug Rabson result=$(jexec second nc -N -w 3 192.0.2.3 1234) 1195ab15157SDoug Rabson if [ "$result" != "foo" ]; then 1205ab15157SDoug Rabson atf_fail "Redirect failed" 1215ab15157SDoug Rabson fi 1225ab15157SDoug Rabson} 1235ab15157SDoug Rabson 1245ab15157SDoug Rabsonlocal_redirect_cleanup() 1255ab15157SDoug Rabson{ 1265ab15157SDoug Rabson firewall=$1 1275ab15157SDoug Rabson firewall_cleanup $firewall 1285ab15157SDoug Rabson} 1295ab15157SDoug Rabson 1305ab15157SDoug Rabsonsetup_tests \ 1315ab15157SDoug Rabson basic \ 1325ab15157SDoug Rabson pf \ 1335ab15157SDoug Rabson ipfnat \ 1345ab15157SDoug Rabson local_redirect \ 1355ab15157SDoug Rabson pf \ 1365ab15157SDoug Rabson ipfnat 1375ab15157SDoug Rabson 138