xref: /freebsd/tests/sys/netpfil/common/rdr.sh (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1# $FreeBSD$
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018 Kristof Provost <kp@FreeBSD.org>
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27
28. $(atf_get_srcdir)/utils.subr
29. $(atf_get_srcdir)/runner.subr
30
31basic_head()
32{
33	atf_set descr 'Basic IPv4 NAT test'
34	atf_set require.user root
35}
36
37basic_body()
38{
39	firewall=$1
40	firewall_init $firewall
41	nat_init $firewall
42
43	epair=$(vnet_mkepair)
44
45	vnet_mkjail alcatraz ${epair}b
46
47	ifconfig ${epair}a 192.0.2.2/24 up
48	route add -net 198.51.100.0/24 192.0.2.1
49
50	jexec alcatraz ifconfig ${epair}b 192.0.2.1/24 up
51	jexec alcatraz sysctl net.inet.ip.forwarding=1
52
53	# Enable redirect filter rule
54	firewall_config alcatraz ${firewall} \
55		"pf" \
56			"rdr pass on ${epair}b proto tcp from any to 198.51.100.0/24 port 1234 -> 192.0.2.1 port 4321" \
57		"ipfnat" \
58			"rdr ${epair}b from any to 198.51.100.0/24 port = 1234 -> 192.0.2.1 port 4321 tcp"
59
60
61	echo "foo" | jexec alcatraz nc -N -l 4321 &
62	sleep 1
63
64	result=$(nc -N -w 3 198.51.100.2 1234)
65	if [ "$result" != "foo" ]; then
66		atf_fail "Redirect failed"
67	fi
68}
69
70basic_cleanup()
71{
72	firewall=$1
73	firewall_cleanup $firewall
74}
75
76local_redirect_head()
77{
78	atf_set descr 'Redirect local traffic test'
79	atf_set require.user root
80}
81
82local_redirect_body()
83{
84	firewall=$1
85	firewall_init $firewall
86	nat_init $firewall
87
88	bridge=$(vnet_mkbridge)
89	ifconfig ${bridge} 192.0.2.1/24 up
90
91	epair1=$(vnet_mkepair)
92	epair2=$(vnet_mkepair)
93
94	vnet_mkjail first ${epair1}b
95	ifconfig ${epair1}a up
96	ifconfig ${bridge} addm ${epair1}a
97	jexec first ifconfig ${epair1}b 192.0.2.2/24 up
98	jexec first ifconfig lo0 127.0.0.1/8 up
99
100	vnet_mkjail second ${epair2}b
101	ifconfig ${epair2}a up
102	ifconfig ${bridge} addm ${epair2}a
103	jexec second ifconfig ${epair2}b 192.0.2.3/24 up
104	jexec second ifconfig lo0 127.0.0.1/8 up
105	jexec second sysctl net.inet.ip.forwarding=1
106
107	# Enable redirect filter rule
108	firewall_config second ${firewall} \
109		"pf" \
110			"rdr pass proto tcp from any to 192.0.2.3/24 port 1234 -> 192.0.2.2 port 4321" \
111		"ipfnat" \
112			"rdr '*' from any to 192.0.2.3/24 port = 1234 -> 192.0.2.2 port 4321 tcp"
113
114	echo "foo" | jexec first nc -N -l 4321 &
115	sleep 1
116
117	# Verify that second can use its rule to redirect local connections to first
118	result=$(jexec second nc -N -w 3 192.0.2.3 1234)
119	if [ "$result" != "foo" ]; then
120		atf_fail "Redirect failed"
121	fi
122}
123
124local_redirect_cleanup()
125{
126	firewall=$1
127	firewall_cleanup $firewall
128}
129
130setup_tests \
131		basic \
132			pf \
133			ipfnat \
134		local_redirect \
135			pf \
136			ipfnat
137
138