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