xref: /freebsd/tests/sys/netpfil/pf/synproxy.sh (revision b0fcf4d5222bfdbbc0e2af2b14f0d73704706aa0)
165d553b0SKristof Provost#
24d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
365d553b0SKristof Provost#
465d553b0SKristof Provost# Copyright (c) 2018 Kristof Provost <kp@FreeBSD.org>
565d553b0SKristof Provost#
665d553b0SKristof Provost# Redistribution and use in source and binary forms, with or without
765d553b0SKristof Provost# modification, are permitted provided that the following conditions
865d553b0SKristof Provost# are met:
965d553b0SKristof Provost# 1. Redistributions of source code must retain the above copyright
1065d553b0SKristof Provost#    notice, this list of conditions and the following disclaimer.
1165d553b0SKristof Provost# 2. Redistributions in binary form must reproduce the above copyright
1265d553b0SKristof Provost#    notice, this list of conditions and the following disclaimer in the
1365d553b0SKristof Provost#    documentation and/or other materials provided with the distribution.
1465d553b0SKristof Provost#
1565d553b0SKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1665d553b0SKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1765d553b0SKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1865d553b0SKristof Provost# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1965d553b0SKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2065d553b0SKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2165d553b0SKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2265d553b0SKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2365d553b0SKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2465d553b0SKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2565d553b0SKristof Provost# SUCH DAMAGE.
266e0bca03SKristof Provost
276e0bca03SKristof Provost. $(atf_get_srcdir)/utils.subr
286e0bca03SKristof Provost
296e0bca03SKristof Provostatf_test_case "synproxy" "cleanup"
306e0bca03SKristof Provostsynproxy_head()
316e0bca03SKristof Provost{
326e0bca03SKristof Provost	atf_set descr 'Basic synproxy test'
336e0bca03SKristof Provost	atf_set require.user root
346e0bca03SKristof Provost}
356e0bca03SKristof Provost
366e0bca03SKristof Provostsynproxy_body()
376e0bca03SKristof Provost{
386e0bca03SKristof Provost	pft_init
396e0bca03SKristof Provost
4006aac31aSKristof Provost	epair=$(vnet_mkepair)
416e0bca03SKristof Provost	ifconfig ${epair}a 192.0.2.1/24 up
426e0bca03SKristof Provost	route add -net 198.51.100.0/24 192.0.2.2
436e0bca03SKristof Provost
4406aac31aSKristof Provost	link=$(vnet_mkepair)
456e0bca03SKristof Provost
4606aac31aSKristof Provost	vnet_mkjail alcatraz ${epair}b ${link}a
476e0bca03SKristof Provost	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
486e0bca03SKristof Provost	jexec alcatraz ifconfig ${link}a 198.51.100.1/24 up
496e0bca03SKristof Provost	jexec alcatraz sysctl net.inet.ip.forwarding=1
506e0bca03SKristof Provost
5106aac31aSKristof Provost	vnet_mkjail singsing ${link}b
526e0bca03SKristof Provost	jexec singsing ifconfig ${link}b 198.51.100.2/24 up
536e0bca03SKristof Provost	jexec singsing route add default 198.51.100.1
546e0bca03SKristof Provost
55*b0fcf4d5SKristof Provost	jexec singsing /usr/sbin/inetd -p ${PWD}/inetd-singsing.pid $(atf_get_srcdir)/echo_inetd.conf
566e0bca03SKristof Provost
576e0bca03SKristof Provost	jexec alcatraz pfctl -e
586e0bca03SKristof Provost	pft_set_rules alcatraz "set fail-policy return" \
596e0bca03SKristof Provost		"scrub in all fragment reassemble" \
606e0bca03SKristof Provost		"pass out quick on ${epair}b all no state allow-opts" \
616e0bca03SKristof Provost		"pass in quick on ${epair}b proto tcp from any to any port 7 synproxy state" \
626e0bca03SKristof Provost		"pass in quick on ${epair}b all no state"
636e0bca03SKristof Provost
646e0bca03SKristof Provost	# Sanity check, can we ping singing
656e0bca03SKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 198.51.100.2
666e0bca03SKristof Provost
676e0bca03SKristof Provost	# Check that we can talk to the singsing jail, after synproxying
686e0bca03SKristof Provost	reply=$(echo ping | nc -N 198.51.100.2 7)
696e0bca03SKristof Provost	if [ "${reply}" != "ping" ];
706e0bca03SKristof Provost	then
716e0bca03SKristof Provost		atf_fail "echo failed"
726e0bca03SKristof Provost	fi
736e0bca03SKristof Provost}
746e0bca03SKristof Provost
756e0bca03SKristof Provostsynproxy_cleanup()
766e0bca03SKristof Provost{
776e0bca03SKristof Provost	pft_cleanup
786e0bca03SKristof Provost}
796e0bca03SKristof Provost
806598cabaSKristof Provostatf_test_case "local" "cleanup"
816598cabaSKristof Provostlocal_head()
826598cabaSKristof Provost{
836598cabaSKristof Provost	atf_set descr 'Synproxy a locally terminated connection'
846598cabaSKristof Provost	atf_set require.user root
856598cabaSKristof Provost}
866598cabaSKristof Provost
876598cabaSKristof Provostlocal_body()
886598cabaSKristof Provost{
896598cabaSKristof Provost	pft_init
906598cabaSKristof Provost
916598cabaSKristof Provost	epair=$(vnet_mkepair)
926598cabaSKristof Provost	ifconfig ${epair}a 192.0.2.2/24 up
936598cabaSKristof Provost
946598cabaSKristof Provost	vnet_mkjail alcatraz ${epair}b
956598cabaSKristof Provost	jexec alcatraz ifconfig ${epair}b 192.0.2.1/24 up
96*b0fcf4d5SKristof Provost	jexec alcatraz /usr/sbin/inetd -p ${PWD}/inetd-alcatraz.pid \
976598cabaSKristof Provost		$(atf_get_srcdir)/echo_inetd.conf
986598cabaSKristof Provost
996598cabaSKristof Provost	jexec alcatraz pfctl -e
1006598cabaSKristof Provost	pft_set_rules alcatraz "set fail-policy return" \
1016598cabaSKristof Provost		"scrub in all fragment reassemble" \
1026598cabaSKristof Provost		"pass in quick on ${epair}b proto tcp from any to any port 7 synproxy state"
1036598cabaSKristof Provost
1046598cabaSKristof Provost	# Sanity check
1056598cabaSKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.1
1066598cabaSKristof Provost
1076598cabaSKristof Provost	# Check that we can talk to the jail, after synproxying
1086598cabaSKristof Provost	reply=$(echo ping | nc -N -w 5 192.0.2.1 7)
1096598cabaSKristof Provost	if [ "${reply}" != "ping" ];
1106598cabaSKristof Provost	then
1116598cabaSKristof Provost		atf_fail "echo failed"
1126598cabaSKristof Provost	fi
1136598cabaSKristof Provost}
1146598cabaSKristof Provost
1156598cabaSKristof Provostlocal_cleanup()
1166598cabaSKristof Provost{
1176598cabaSKristof Provost	pft_cleanup
1186598cabaSKristof Provost}
1196598cabaSKristof Provost
1206598cabaSKristof Provostatf_test_case "local_v6" "cleanup"
1216598cabaSKristof Provostlocal_v6_head()
1226598cabaSKristof Provost{
1236598cabaSKristof Provost	atf_set descr 'Synproxy (v6) a locally terminated connection'
1246598cabaSKristof Provost	atf_set require.user root
1256598cabaSKristof Provost}
1266598cabaSKristof Provost
1276598cabaSKristof Provostlocal_v6_body()
1286598cabaSKristof Provost{
1296598cabaSKristof Provost	pft_init
1306598cabaSKristof Provost
1316598cabaSKristof Provost	epair=$(vnet_mkepair)
1326598cabaSKristof Provost	ifconfig ${epair}a inet6 2001:db8:42::1/64 up
1336598cabaSKristof Provost
1346598cabaSKristof Provost	vnet_mkjail alcatraz ${epair}b
1356598cabaSKristof Provost	jexec alcatraz ifconfig ${epair}b inet6 2001:db8:42::2/64 up
136*b0fcf4d5SKristof Provost	jexec alcatraz /usr/sbin/inetd -p ${PWD}/inetd-alcatraz.pid \
1376598cabaSKristof Provost		$(atf_get_srcdir)/echo_inetd.conf
1386598cabaSKristof Provost
1396598cabaSKristof Provost	jexec alcatraz pfctl -e
1406598cabaSKristof Provost	pft_set_rules alcatraz "set fail-policy return" \
1416598cabaSKristof Provost		"scrub in all fragment reassemble" \
1426598cabaSKristof Provost		"pass in quick on ${epair}b proto tcp from any to any port 7 synproxy state"
1436598cabaSKristof Provost
1446598cabaSKristof Provost	# Sanity check
1456598cabaSKristof Provost	atf_check -s exit:0 -o ignore ping6 -c 1 2001:db8:42::2
1466598cabaSKristof Provost
1476598cabaSKristof Provost	reply=$(echo ping | nc -N -w 5 2001:db8:42::2 7)
1486598cabaSKristof Provost	if [ "${reply}" != "ping" ];
1496598cabaSKristof Provost	then
1506598cabaSKristof Provost		atf_fail "echo failed"
1516598cabaSKristof Provost	fi
1526598cabaSKristof Provost}
1536598cabaSKristof Provost
1546598cabaSKristof Provostlocal_v6_cleanup()
1556598cabaSKristof Provost{
1566598cabaSKristof Provost	pft_cleanup
1576598cabaSKristof Provost}
1586598cabaSKristof Provost
1596e0bca03SKristof Provostatf_init_test_cases()
1606e0bca03SKristof Provost{
1616e0bca03SKristof Provost	atf_add_test_case "synproxy"
1626598cabaSKristof Provost	atf_add_test_case "local"
1636598cabaSKristof Provost	atf_add_test_case "local_v6"
1646e0bca03SKristof Provost}
165