xref: /freebsd/tests/sys/netpfil/common/nat.sh (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
10d9da68fSTom Jones#-
2*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
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#
280d9da68fSTom Jones
290d9da68fSTom Jones. $(atf_get_srcdir)/utils.subr
300d9da68fSTom Jones. $(atf_get_srcdir)/runner.subr
310d9da68fSTom Jones
320d9da68fSTom Jonesbasic_head()
330d9da68fSTom Jones{
340d9da68fSTom Jones	atf_set descr 'Basic IPv4 NAT test'
350d9da68fSTom Jones	atf_set require.user root
360d9da68fSTom Jones}
370d9da68fSTom Jones
380d9da68fSTom Jonesbasic_body()
390d9da68fSTom Jones{
400d9da68fSTom Jones	firewall=$1
410d9da68fSTom Jones	firewall_init $firewall
420d9da68fSTom Jones	nat_init $firewall
430d9da68fSTom Jones
440d9da68fSTom Jones	epair_host_nat=$(vnet_mkepair)
450d9da68fSTom Jones	epair_client1_nat=$(vnet_mkepair)
460d9da68fSTom Jones	epair_client2_nat=$(vnet_mkepair)
470d9da68fSTom Jones
480d9da68fSTom Jones	vnet_mkjail nat ${epair_host_nat}b ${epair_client1_nat}a ${epair_client2_nat}a
490d9da68fSTom Jones	vnet_mkjail client1 ${epair_client1_nat}b
500d9da68fSTom Jones	vnet_mkjail client2 ${epair_client2_nat}b
510d9da68fSTom Jones
520d9da68fSTom Jones	ifconfig ${epair_host_nat}a 198.51.100.2/24 up
530d9da68fSTom Jones	jexec nat ifconfig ${epair_host_nat}b 198.51.100.1/24 up
540d9da68fSTom Jones
550d9da68fSTom Jones	jexec nat ifconfig ${epair_client1_nat}a 192.0.2.1/24 up
560d9da68fSTom Jones	jexec client1 ifconfig ${epair_client1_nat}b 192.0.2.2/24 up
570d9da68fSTom Jones
580d9da68fSTom Jones	jexec nat ifconfig ${epair_client2_nat}a 192.0.3.1/24 up
590d9da68fSTom Jones	jexec client2 ifconfig ${epair_client2_nat}b 192.0.3.2/24 up
600d9da68fSTom Jones
610d9da68fSTom Jones	jexec nat sysctl net.inet.ip.forwarding=1
620d9da68fSTom Jones
630d9da68fSTom Jones	jexec client1 route add -net 198.51.100.0/24 192.0.2.1
640d9da68fSTom Jones	jexec client2 route add -net 198.51.100.0/24 192.0.3.1
650d9da68fSTom Jones
660d9da68fSTom Jones	# ping fails without NAT configuration
670d9da68fSTom Jones	atf_check -s exit:2 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2
680d9da68fSTom Jones	atf_check -s exit:2 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2
690d9da68fSTom Jones
700d9da68fSTom Jones	firewall_config nat ${firewall} \
710d9da68fSTom Jones		"pf" \
720d9da68fSTom Jones			"nat pass on ${epair_host_nat}b inet from any to any -> (${epair_host_nat}b)" \
730d9da68fSTom Jones		"ipfw" \
740d9da68fSTom Jones			"ipfw -q nat 123 config if ${epair_host_nat}b" \
750d9da68fSTom Jones			"ipfw -q add 1000 nat 123 all from any to any" \
760d9da68fSTom Jones		"ipfnat" \
770d9da68fSTom Jones			"map ${epair_host_nat}b 192.0.3.0/24 -> 0/32" \
780d9da68fSTom Jones			"map ${epair_host_nat}b 192.0.2.0/24 -> 0/32" \
790d9da68fSTom Jones
800d9da68fSTom Jones
810d9da68fSTom Jones	# ping is successful now
820d9da68fSTom Jones	atf_check -s exit:0 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2
830d9da68fSTom Jones	atf_check -s exit:0 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2
840d9da68fSTom Jones
850d9da68fSTom Jones}
860d9da68fSTom Jones
870d9da68fSTom Jonesbasic_cleanup()
880d9da68fSTom Jones{
890d9da68fSTom Jones	firewall=$1
900d9da68fSTom Jones	firewall_cleanup $firewall
910d9da68fSTom Jones}
920d9da68fSTom Jones
930d9da68fSTom Jonesuserspace_nat_head()
940d9da68fSTom Jones{
950d9da68fSTom Jones	atf_set descr 'Nat test for ipfw using userspace natd'
960d9da68fSTom Jones	atf_set require.user root
970d9da68fSTom Jones}
980d9da68fSTom Jonesuserspace_nat_body()
990d9da68fSTom Jones{
1000d9da68fSTom Jones	firewall=$1
1010d9da68fSTom Jones	firewall_init $firewall
1020d9da68fSTom Jones
1030d9da68fSTom Jones	if ! kldstat -q -m ipdivert; then
1040d9da68fSTom Jones		atf_skip "This test requires ipdivert module loaded"
1050d9da68fSTom Jones	fi
1060d9da68fSTom Jones
1070d9da68fSTom Jones	epair_host_nat=$(vnet_mkepair)
1080d9da68fSTom Jones	epair_client1_nat=$(vnet_mkepair)
1090d9da68fSTom Jones	epair_client2_nat=$(vnet_mkepair)
1100d9da68fSTom Jones
1110d9da68fSTom Jones	vnet_mkjail nat ${epair_host_nat}b ${epair_client1_nat}a ${epair_client2_nat}a
1120d9da68fSTom Jones	vnet_mkjail client1 ${epair_client1_nat}b
1130d9da68fSTom Jones	vnet_mkjail client2 ${epair_client2_nat}b
1140d9da68fSTom Jones
1150d9da68fSTom Jones	ifconfig ${epair_host_nat}a 198.51.100.2/24 up
1160d9da68fSTom Jones	jexec nat ifconfig ${epair_host_nat}b 198.51.100.1/24 up
1170d9da68fSTom Jones
1180d9da68fSTom Jones	jexec nat ifconfig ${epair_client1_nat}a 192.0.2.1/24 up
1190d9da68fSTom Jones	jexec client1 ifconfig ${epair_client1_nat}b 192.0.2.2/24 up
1200d9da68fSTom Jones
1210d9da68fSTom Jones	jexec nat ifconfig ${epair_client2_nat}a 192.0.3.1/24 up
1220d9da68fSTom Jones	jexec client2 ifconfig ${epair_client2_nat}b 192.0.3.2/24 up
1230d9da68fSTom Jones
1240d9da68fSTom Jones	jexec nat sysctl net.inet.ip.forwarding=1
1250d9da68fSTom Jones
1260d9da68fSTom Jones	jexec client1 route add -net 198.51.100.0/24 192.0.2.1
1270d9da68fSTom Jones	jexec client2 route add -net 198.51.100.0/24 192.0.3.1
1280d9da68fSTom Jones	# Test the userspace NAT of ipfw
1290d9da68fSTom Jones	# ping fails without NAT configuration
1300d9da68fSTom Jones	atf_check -s exit:2 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2
1310d9da68fSTom Jones	atf_check -s exit:2 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2
1320d9da68fSTom Jones
1330d9da68fSTom Jones	firewall_config nat ${firewall} \
1340d9da68fSTom Jones		"ipfw" \
1350d9da68fSTom Jones			"natd -interface ${epair_host_nat}b" \
1360d9da68fSTom Jones			"ipfw -q add divert natd all from any to any via ${epair_host_nat}b" \
1370d9da68fSTom Jones
1380d9da68fSTom Jones	# ping is successful now
1390d9da68fSTom Jones	atf_check -s exit:0 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2
1400d9da68fSTom Jones	atf_check -s exit:0 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2
1410d9da68fSTom Jones}
1420d9da68fSTom Jones
1430d9da68fSTom Jonesuserspace_nat_cleanup()
1440d9da68fSTom Jones{
1450d9da68fSTom Jones	firewall=$1
1460d9da68fSTom Jones	firewall_cleanup $firewall
1470d9da68fSTom Jones}
1480d9da68fSTom Jones
149a08cdb6cSNeel Chauhancommon_cgn() {
150a08cdb6cSNeel Chauhan	firewall=$1
151a08cdb6cSNeel Chauhan	portalias=$2
152a08cdb6cSNeel Chauhan	firewall_init $firewall
153a08cdb6cSNeel Chauhan	nat_init $firewall
154a08cdb6cSNeel Chauhan
155a08cdb6cSNeel Chauhan	epair_host_nat=$(vnet_mkepair)
156a08cdb6cSNeel Chauhan	epair_client1_nat=$(vnet_mkepair)
157a08cdb6cSNeel Chauhan	epair_client2_nat=$(vnet_mkepair)
158a08cdb6cSNeel Chauhan
159a08cdb6cSNeel Chauhan	vnet_mkjail nat ${epair_host_nat}b ${epair_client1_nat}a ${epair_client2_nat}a
160a08cdb6cSNeel Chauhan	vnet_mkjail client1 ${epair_client1_nat}b
161a08cdb6cSNeel Chauhan	vnet_mkjail client2 ${epair_client2_nat}b
162a08cdb6cSNeel Chauhan
163a08cdb6cSNeel Chauhan	ifconfig ${epair_host_nat}a 198.51.100.2/24 up
164a08cdb6cSNeel Chauhan	jexec nat ifconfig ${epair_host_nat}b 198.51.100.1/24 up
165a08cdb6cSNeel Chauhan
166a08cdb6cSNeel Chauhan	jexec nat ifconfig ${epair_client1_nat}a 100.64.0.1/24 up
167a08cdb6cSNeel Chauhan	jexec client1 ifconfig ${epair_client1_nat}b 100.64.0.2/24 up
168a08cdb6cSNeel Chauhan
169a08cdb6cSNeel Chauhan	jexec nat ifconfig ${epair_client2_nat}a 100.64.1.1/24 up
170a08cdb6cSNeel Chauhan	jexec client2 ifconfig ${epair_client2_nat}b 100.64.1.2/24 up
171a08cdb6cSNeel Chauhan
172a08cdb6cSNeel Chauhan	jexec nat sysctl net.inet.ip.forwarding=1
173a08cdb6cSNeel Chauhan
174a08cdb6cSNeel Chauhan	jexec client1 route add -net 198.51.100.0/24 100.64.0.1
175a08cdb6cSNeel Chauhan	jexec client2 route add -net 198.51.100.0/24 100.64.1.1
176a08cdb6cSNeel Chauhan
177a08cdb6cSNeel Chauhan	# ping fails without NAT configuration
178a08cdb6cSNeel Chauhan	atf_check -s exit:2 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2
179a08cdb6cSNeel Chauhan	atf_check -s exit:2 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2
180a08cdb6cSNeel Chauhan
181a08cdb6cSNeel Chauhan	if [[ $portalias ]]; then
182a08cdb6cSNeel Chauhan		firewall_config nat $firewall \
183a08cdb6cSNeel Chauhan			"ipfw" \
184a08cdb6cSNeel Chauhan				"ipfw -q nat 123 config if ${epair_host_nat}b unreg_cgn port_alias 2000-2999" \
185a08cdb6cSNeel Chauhan				"ipfw -q nat 456 config if ${epair_host_nat}b unreg_cgn port_alias 3000-3999" \
186a08cdb6cSNeel Chauhan				"ipfw -q add 1000 nat 123 all from any to 198.51.100.2 2000-2999 in via ${epair_host_nat}b" \
187a08cdb6cSNeel Chauhan				"ipfw -q add 2000 nat 456 all from any to 198.51.100.2 3000-3999 in via ${epair_host_nat}b" \
188a08cdb6cSNeel Chauhan				"ipfw -q add 3000 nat 123 all from 100.64.0.2 to any out via ${epair_host_nat}b" \
189a08cdb6cSNeel Chauhan				"ipfw -q add 4000 nat 456 all from 100.64.1.2 to any out via ${epair_host_nat}b"
190a08cdb6cSNeel Chauhan	else
191a08cdb6cSNeel Chauhan		firewall_config nat $firewall \
192a08cdb6cSNeel Chauhan			"ipfw" \
193a08cdb6cSNeel Chauhan				"ipfw -q nat 123 config if ${epair_host_nat}b unreg_cgn" \
194a08cdb6cSNeel Chauhan				"ipfw -q add 1000 nat 123 all from any to any"
195a08cdb6cSNeel Chauhan	fi
196a08cdb6cSNeel Chauhan
197a08cdb6cSNeel Chauhan	# ping is successful now
198a08cdb6cSNeel Chauhan	atf_check -s exit:0 -o ignore jexec client1 ping -t 1 -c 1 198.51.100.2
199a08cdb6cSNeel Chauhan	atf_check -s exit:0 -o ignore jexec client2 ping -t 1 -c 1 198.51.100.2
200a08cdb6cSNeel Chauhan
201a08cdb6cSNeel Chauhan	# if portalias, test a tcp server/client with nc
202a08cdb6cSNeel Chauhan	if [[ $portalias ]]; then
203a08cdb6cSNeel Chauhan		for inst in 1 2; do
204a08cdb6cSNeel Chauhan			daemon nc -p 198.51.100.2 7
205a08cdb6cSNeel Chauhan			atf_check -s exit:0 -o ignore jexec client$inst sh -c "echo | nc -N 198.51.100.2 7"
206a08cdb6cSNeel Chauhan		done
207a08cdb6cSNeel Chauhan	fi
208a08cdb6cSNeel Chauhan}
209a08cdb6cSNeel Chauhan
210a08cdb6cSNeel Chauhancgn_head()
211a08cdb6cSNeel Chauhan{
212a08cdb6cSNeel Chauhan	atf_set descr 'IPv4 CGN (RFC 6598) test'
213a08cdb6cSNeel Chauhan	atf_set require.user root
214a08cdb6cSNeel Chauhan}
215a08cdb6cSNeel Chauhan
216a08cdb6cSNeel Chauhancgn_body()
217a08cdb6cSNeel Chauhan{
218a08cdb6cSNeel Chauhan	common_cgn $1 false
219a08cdb6cSNeel Chauhan}
220a08cdb6cSNeel Chauhan
221a08cdb6cSNeel Chauhancgn_cleanup()
222a08cdb6cSNeel Chauhan{
223a08cdb6cSNeel Chauhan	firewall_cleanup ipfw
224a08cdb6cSNeel Chauhan}
225a08cdb6cSNeel Chauhan
226a08cdb6cSNeel Chauhanportalias_head()
227a08cdb6cSNeel Chauhan{
228a08cdb6cSNeel Chauhan	atf_set descr 'IPv4 CGN (RFC 6598) port aliasing test'
229a08cdb6cSNeel Chauhan	atf_set require.user root
230a08cdb6cSNeel Chauhan}
231a08cdb6cSNeel Chauhan
232a08cdb6cSNeel Chauhanportalias_body()
233a08cdb6cSNeel Chauhan{
234a08cdb6cSNeel Chauhan	common_cgn $1 true
235a08cdb6cSNeel Chauhan}
236a08cdb6cSNeel Chauhan
237a08cdb6cSNeel Chauhanportalias_cleanup()
238a08cdb6cSNeel Chauhan{
239a08cdb6cSNeel Chauhan	firewall_cleanup ipfw
240a08cdb6cSNeel Chauhan}
241a08cdb6cSNeel Chauhan
2420d9da68fSTom Jonessetup_tests \
2430d9da68fSTom Jones		basic \
2440d9da68fSTom Jones			pf \
2450d9da68fSTom Jones			ipfw \
2460d9da68fSTom Jones			ipfnat \
2470d9da68fSTom Jones		userspace_nat \
248a08cdb6cSNeel Chauhan			ipfw \
249a08cdb6cSNeel Chauhan		cgn \
250a08cdb6cSNeel Chauhan			ipfw \
251a08cdb6cSNeel Chauhan		portalias \
2520d9da68fSTom Jones			ipfw
253