xref: /freebsd/tests/sys/netpfil/common/rdr.sh (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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
87	bridge=$(vnet_mkbridge)
88	ifconfig ${bridge} 192.0.2.1/24 up
89
90	epair1=$(vnet_mkepair)
91	epair2=$(vnet_mkepair)
92
93	vnet_mkjail first ${epair1}b
94	ifconfig ${epair1}a up
95	ifconfig ${bridge} addm ${epair1}a
96	jexec first ifconfig ${epair1}b 192.0.2.2/24 up
97	jexec first ifconfig lo0 127.0.0.1/8 up
98
99	vnet_mkjail second ${epair2}b
100	ifconfig ${epair2}a up
101	ifconfig ${bridge} addm ${epair2}a
102	jexec second ifconfig ${epair2}b 192.0.2.3/24 up
103	jexec second ifconfig lo0 127.0.0.1/8 up
104	jexec second sysctl net.inet.ip.forwarding=1
105
106	# Enable redirect filter rule
107	firewall_config second ${firewall} \
108		"pf" \
109			"rdr pass proto tcp from any to 192.0.2.3/24 port 1234 -> 192.0.2.2 port 4321" \
110		"ipfnat" \
111			"rdr '*' from any to 192.0.2.3/24 port = 1234 -> 192.0.2.2 port 4321 tcp"
112
113	echo "foo" | jexec first nc -N -l 4321 &
114	sleep 1
115
116	# Verify that second can use its rule to redirect local connections to first
117	result=$(jexec second nc -N -w 3 192.0.2.3 1234)
118	if [ "$result" != "foo" ]; then
119		atf_fail "Redirect failed"
120	fi
121}
122
123local_redirect_cleanup()
124{
125	firewall=$1
126	firewall_cleanup $firewall
127}
128
129setup_tests \
130		basic \
131			pf \
132			ipfnat \
133		local_redirect \
134			pf \
135			ipfnat
136
137