10d9da68fSTom Jones#- 20d9da68fSTom Jones# SPDX-License-Identifier: BSD-2-Clause-FreeBSD 30d9da68fSTom Jones# 40d9da68fSTom Jones# Copyright (c) 2019 Ahsan Barkati 50d9da68fSTom Jones# 60d9da68fSTom Jones# Redistribution and use in source and binary forms, with or without 70d9da68fSTom Jones# modification, are permitted provided that the following conditions 80d9da68fSTom Jones# are met: 90d9da68fSTom Jones# 1. Redistributions of source code must retain the above copyright 100d9da68fSTom Jones# notice, this list of conditions and the following disclaimer. 110d9da68fSTom Jones# 2. Redistributions in binary form must reproduce the above copyright 120d9da68fSTom Jones# notice, this list of conditions and the following disclaimer in the 130d9da68fSTom Jones# documentation and/or other materials provided with the distribution. 140d9da68fSTom Jones# 150d9da68fSTom Jones# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 160d9da68fSTom Jones# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 170d9da68fSTom Jones# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 180d9da68fSTom Jones# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 190d9da68fSTom Jones# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 200d9da68fSTom Jones# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 210d9da68fSTom Jones# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 220d9da68fSTom Jones# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 230d9da68fSTom Jones# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 240d9da68fSTom Jones# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 250d9da68fSTom Jones# SUCH DAMAGE. 260d9da68fSTom Jones# 270d9da68fSTom Jones# $FreeBSD$ 280d9da68fSTom Jones# 290d9da68fSTom Jones 300d9da68fSTom Jones. $(atf_get_srcdir)/utils.subr 310d9da68fSTom Jones. $(atf_get_srcdir)/runner.subr 320d9da68fSTom Jones 330d9da68fSTom Jonesbasic_head() 340d9da68fSTom Jones{ 350d9da68fSTom Jones atf_set descr 'Basic IPv4 NAT test' 360d9da68fSTom Jones atf_set require.user root 370d9da68fSTom Jones} 380d9da68fSTom Jones 390d9da68fSTom Jonesbasic_body() 400d9da68fSTom Jones{ 410d9da68fSTom Jones firewall=$1 420d9da68fSTom Jones firewall_init $firewall 430d9da68fSTom Jones nat_init $firewall 440d9da68fSTom Jones 450d9da68fSTom Jones epair_host_nat=$(vnet_mkepair) 460d9da68fSTom Jones epair_client1_nat=$(vnet_mkepair) 470d9da68fSTom Jones epair_client2_nat=$(vnet_mkepair) 480d9da68fSTom Jones 490d9da68fSTom Jones vnet_mkjail nat ${epair_host_nat}b ${epair_client1_nat}a ${epair_client2_nat}a 500d9da68fSTom Jones vnet_mkjail client1 ${epair_client1_nat}b 510d9da68fSTom Jones vnet_mkjail client2 ${epair_client2_nat}b 520d9da68fSTom Jones 530d9da68fSTom Jones ifconfig ${epair_host_nat}a 198.51.100.2/24 up 540d9da68fSTom Jones jexec nat ifconfig ${epair_host_nat}b 198.51.100.1/24 up 550d9da68fSTom Jones 560d9da68fSTom Jones jexec nat ifconfig ${epair_client1_nat}a 192.0.2.1/24 up 570d9da68fSTom Jones jexec client1 ifconfig ${epair_client1_nat}b 192.0.2.2/24 up 580d9da68fSTom Jones 590d9da68fSTom Jones jexec nat ifconfig ${epair_client2_nat}a 192.0.3.1/24 up 600d9da68fSTom Jones jexec client2 ifconfig ${epair_client2_nat}b 192.0.3.2/24 up 610d9da68fSTom Jones 620d9da68fSTom Jones jexec nat sysctl net.inet.ip.forwarding=1 630d9da68fSTom Jones 640d9da68fSTom Jones jexec client1 route add -net 198.51.100.0/24 192.0.2.1 650d9da68fSTom Jones jexec client2 route add -net 198.51.100.0/24 192.0.3.1 660d9da68fSTom Jones 670d9da68fSTom Jones # ping fails without NAT configuration 680d9da68fSTom Jones atf_check -s exit:2 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2 690d9da68fSTom Jones atf_check -s exit:2 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2 700d9da68fSTom Jones 710d9da68fSTom Jones firewall_config nat ${firewall} \ 720d9da68fSTom Jones "pf" \ 730d9da68fSTom Jones "nat pass on ${epair_host_nat}b inet from any to any -> (${epair_host_nat}b)" \ 740d9da68fSTom Jones "ipfw" \ 750d9da68fSTom Jones "ipfw -q nat 123 config if ${epair_host_nat}b" \ 760d9da68fSTom Jones "ipfw -q add 1000 nat 123 all from any to any" \ 770d9da68fSTom Jones "ipfnat" \ 780d9da68fSTom Jones "map ${epair_host_nat}b 192.0.3.0/24 -> 0/32" \ 790d9da68fSTom Jones "map ${epair_host_nat}b 192.0.2.0/24 -> 0/32" \ 800d9da68fSTom Jones 810d9da68fSTom Jones 820d9da68fSTom Jones # ping is successful now 830d9da68fSTom Jones atf_check -s exit:0 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2 840d9da68fSTom Jones atf_check -s exit:0 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2 850d9da68fSTom Jones 860d9da68fSTom Jones} 870d9da68fSTom Jones 880d9da68fSTom Jonesbasic_cleanup() 890d9da68fSTom Jones{ 900d9da68fSTom Jones firewall=$1 910d9da68fSTom Jones firewall_cleanup $firewall 920d9da68fSTom Jones} 930d9da68fSTom Jones 940d9da68fSTom Jonesuserspace_nat_head() 950d9da68fSTom Jones{ 960d9da68fSTom Jones atf_set descr 'Nat test for ipfw using userspace natd' 970d9da68fSTom Jones atf_set require.user root 980d9da68fSTom Jones} 990d9da68fSTom Jonesuserspace_nat_body() 1000d9da68fSTom Jones{ 1010d9da68fSTom Jones firewall=$1 1020d9da68fSTom Jones firewall_init $firewall 1030d9da68fSTom Jones 1040d9da68fSTom Jones if ! kldstat -q -m ipdivert; then 1050d9da68fSTom Jones atf_skip "This test requires ipdivert module loaded" 1060d9da68fSTom Jones fi 1070d9da68fSTom Jones 1080d9da68fSTom Jones epair_host_nat=$(vnet_mkepair) 1090d9da68fSTom Jones epair_client1_nat=$(vnet_mkepair) 1100d9da68fSTom Jones epair_client2_nat=$(vnet_mkepair) 1110d9da68fSTom Jones 1120d9da68fSTom Jones vnet_mkjail nat ${epair_host_nat}b ${epair_client1_nat}a ${epair_client2_nat}a 1130d9da68fSTom Jones vnet_mkjail client1 ${epair_client1_nat}b 1140d9da68fSTom Jones vnet_mkjail client2 ${epair_client2_nat}b 1150d9da68fSTom Jones 1160d9da68fSTom Jones ifconfig ${epair_host_nat}a 198.51.100.2/24 up 1170d9da68fSTom Jones jexec nat ifconfig ${epair_host_nat}b 198.51.100.1/24 up 1180d9da68fSTom Jones 1190d9da68fSTom Jones jexec nat ifconfig ${epair_client1_nat}a 192.0.2.1/24 up 1200d9da68fSTom Jones jexec client1 ifconfig ${epair_client1_nat}b 192.0.2.2/24 up 1210d9da68fSTom Jones 1220d9da68fSTom Jones jexec nat ifconfig ${epair_client2_nat}a 192.0.3.1/24 up 1230d9da68fSTom Jones jexec client2 ifconfig ${epair_client2_nat}b 192.0.3.2/24 up 1240d9da68fSTom Jones 1250d9da68fSTom Jones jexec nat sysctl net.inet.ip.forwarding=1 1260d9da68fSTom Jones 1270d9da68fSTom Jones jexec client1 route add -net 198.51.100.0/24 192.0.2.1 1280d9da68fSTom Jones jexec client2 route add -net 198.51.100.0/24 192.0.3.1 1290d9da68fSTom Jones # Test the userspace NAT of ipfw 1300d9da68fSTom Jones # ping fails without NAT configuration 1310d9da68fSTom Jones atf_check -s exit:2 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2 1320d9da68fSTom Jones atf_check -s exit:2 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2 1330d9da68fSTom Jones 1340d9da68fSTom Jones firewall_config nat ${firewall} \ 1350d9da68fSTom Jones "ipfw" \ 1360d9da68fSTom Jones "natd -interface ${epair_host_nat}b" \ 1370d9da68fSTom Jones "ipfw -q add divert natd all from any to any via ${epair_host_nat}b" \ 1380d9da68fSTom Jones 1390d9da68fSTom Jones # ping is successful now 1400d9da68fSTom Jones atf_check -s exit:0 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2 1410d9da68fSTom Jones atf_check -s exit:0 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2 1420d9da68fSTom Jones} 1430d9da68fSTom Jones 1440d9da68fSTom Jonesuserspace_nat_cleanup() 1450d9da68fSTom Jones{ 1460d9da68fSTom Jones firewall=$1 1470d9da68fSTom Jones firewall_cleanup $firewall 1480d9da68fSTom Jones} 1490d9da68fSTom Jones 150*a08cdb6cSNeel Chauhancommon_cgn() { 151*a08cdb6cSNeel Chauhan firewall=$1 152*a08cdb6cSNeel Chauhan portalias=$2 153*a08cdb6cSNeel Chauhan firewall_init $firewall 154*a08cdb6cSNeel Chauhan nat_init $firewall 155*a08cdb6cSNeel Chauhan 156*a08cdb6cSNeel Chauhan epair_host_nat=$(vnet_mkepair) 157*a08cdb6cSNeel Chauhan epair_client1_nat=$(vnet_mkepair) 158*a08cdb6cSNeel Chauhan epair_client2_nat=$(vnet_mkepair) 159*a08cdb6cSNeel Chauhan 160*a08cdb6cSNeel Chauhan vnet_mkjail nat ${epair_host_nat}b ${epair_client1_nat}a ${epair_client2_nat}a 161*a08cdb6cSNeel Chauhan vnet_mkjail client1 ${epair_client1_nat}b 162*a08cdb6cSNeel Chauhan vnet_mkjail client2 ${epair_client2_nat}b 163*a08cdb6cSNeel Chauhan 164*a08cdb6cSNeel Chauhan ifconfig ${epair_host_nat}a 198.51.100.2/24 up 165*a08cdb6cSNeel Chauhan jexec nat ifconfig ${epair_host_nat}b 198.51.100.1/24 up 166*a08cdb6cSNeel Chauhan 167*a08cdb6cSNeel Chauhan jexec nat ifconfig ${epair_client1_nat}a 100.64.0.1/24 up 168*a08cdb6cSNeel Chauhan jexec client1 ifconfig ${epair_client1_nat}b 100.64.0.2/24 up 169*a08cdb6cSNeel Chauhan 170*a08cdb6cSNeel Chauhan jexec nat ifconfig ${epair_client2_nat}a 100.64.1.1/24 up 171*a08cdb6cSNeel Chauhan jexec client2 ifconfig ${epair_client2_nat}b 100.64.1.2/24 up 172*a08cdb6cSNeel Chauhan 173*a08cdb6cSNeel Chauhan jexec nat sysctl net.inet.ip.forwarding=1 174*a08cdb6cSNeel Chauhan 175*a08cdb6cSNeel Chauhan jexec client1 route add -net 198.51.100.0/24 100.64.0.1 176*a08cdb6cSNeel Chauhan jexec client2 route add -net 198.51.100.0/24 100.64.1.1 177*a08cdb6cSNeel Chauhan 178*a08cdb6cSNeel Chauhan # ping fails without NAT configuration 179*a08cdb6cSNeel Chauhan atf_check -s exit:2 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2 180*a08cdb6cSNeel Chauhan atf_check -s exit:2 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2 181*a08cdb6cSNeel Chauhan 182*a08cdb6cSNeel Chauhan if [[ $portalias ]]; then 183*a08cdb6cSNeel Chauhan firewall_config nat $firewall \ 184*a08cdb6cSNeel Chauhan "ipfw" \ 185*a08cdb6cSNeel Chauhan "ipfw -q nat 123 config if ${epair_host_nat}b unreg_cgn port_alias 2000-2999" \ 186*a08cdb6cSNeel Chauhan "ipfw -q nat 456 config if ${epair_host_nat}b unreg_cgn port_alias 3000-3999" \ 187*a08cdb6cSNeel Chauhan "ipfw -q add 1000 nat 123 all from any to 198.51.100.2 2000-2999 in via ${epair_host_nat}b" \ 188*a08cdb6cSNeel Chauhan "ipfw -q add 2000 nat 456 all from any to 198.51.100.2 3000-3999 in via ${epair_host_nat}b" \ 189*a08cdb6cSNeel Chauhan "ipfw -q add 3000 nat 123 all from 100.64.0.2 to any out via ${epair_host_nat}b" \ 190*a08cdb6cSNeel Chauhan "ipfw -q add 4000 nat 456 all from 100.64.1.2 to any out via ${epair_host_nat}b" 191*a08cdb6cSNeel Chauhan else 192*a08cdb6cSNeel Chauhan firewall_config nat $firewall \ 193*a08cdb6cSNeel Chauhan "ipfw" \ 194*a08cdb6cSNeel Chauhan "ipfw -q nat 123 config if ${epair_host_nat}b unreg_cgn" \ 195*a08cdb6cSNeel Chauhan "ipfw -q add 1000 nat 123 all from any to any" 196*a08cdb6cSNeel Chauhan fi 197*a08cdb6cSNeel Chauhan 198*a08cdb6cSNeel Chauhan # ping is successful now 199*a08cdb6cSNeel Chauhan atf_check -s exit:0 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2 200*a08cdb6cSNeel Chauhan atf_check -s exit:0 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2 201*a08cdb6cSNeel Chauhan 202*a08cdb6cSNeel Chauhan # if portalias, test a tcp server/client with nc 203*a08cdb6cSNeel Chauhan if [[ $portalias ]]; then 204*a08cdb6cSNeel Chauhan for inst in 1 2; do 205*a08cdb6cSNeel Chauhan daemon nc -p 198.51.100.2 7 206*a08cdb6cSNeel Chauhan atf_check -s exit:0 -o ignore jexec client$inst sh -c "echo | nc -N 198.51.100.2 7" 207*a08cdb6cSNeel Chauhan done 208*a08cdb6cSNeel Chauhan fi 209*a08cdb6cSNeel Chauhan} 210*a08cdb6cSNeel Chauhan 211*a08cdb6cSNeel Chauhancgn_head() 212*a08cdb6cSNeel Chauhan{ 213*a08cdb6cSNeel Chauhan atf_set descr 'IPv4 CGN (RFC 6598) test' 214*a08cdb6cSNeel Chauhan atf_set require.user root 215*a08cdb6cSNeel Chauhan} 216*a08cdb6cSNeel Chauhan 217*a08cdb6cSNeel Chauhancgn_body() 218*a08cdb6cSNeel Chauhan{ 219*a08cdb6cSNeel Chauhan common_cgn $1 false 220*a08cdb6cSNeel Chauhan} 221*a08cdb6cSNeel Chauhan 222*a08cdb6cSNeel Chauhancgn_cleanup() 223*a08cdb6cSNeel Chauhan{ 224*a08cdb6cSNeel Chauhan firewall_cleanup ipfw 225*a08cdb6cSNeel Chauhan} 226*a08cdb6cSNeel Chauhan 227*a08cdb6cSNeel Chauhanportalias_head() 228*a08cdb6cSNeel Chauhan{ 229*a08cdb6cSNeel Chauhan atf_set descr 'IPv4 CGN (RFC 6598) port aliasing test' 230*a08cdb6cSNeel Chauhan atf_set require.user root 231*a08cdb6cSNeel Chauhan} 232*a08cdb6cSNeel Chauhan 233*a08cdb6cSNeel Chauhanportalias_body() 234*a08cdb6cSNeel Chauhan{ 235*a08cdb6cSNeel Chauhan common_cgn $1 true 236*a08cdb6cSNeel Chauhan} 237*a08cdb6cSNeel Chauhan 238*a08cdb6cSNeel Chauhanportalias_cleanup() 239*a08cdb6cSNeel Chauhan{ 240*a08cdb6cSNeel Chauhan firewall_cleanup ipfw 241*a08cdb6cSNeel Chauhan} 242*a08cdb6cSNeel Chauhan 2430d9da68fSTom Jonessetup_tests \ 2440d9da68fSTom Jones basic \ 2450d9da68fSTom Jones pf \ 2460d9da68fSTom Jones ipfw \ 2470d9da68fSTom Jones ipfnat \ 2480d9da68fSTom Jones userspace_nat \ 249*a08cdb6cSNeel Chauhan ipfw \ 250*a08cdb6cSNeel Chauhan cgn \ 251*a08cdb6cSNeel Chauhan ipfw \ 252*a08cdb6cSNeel Chauhan portalias \ 2530d9da68fSTom Jones ipfw 254