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