xref: /freebsd/tests/sys/netpfil/pf/rdr.sh (revision 1f1963bd32d669f89ea3b044636de0cb7a33134b)
1b9870ba9STom Jones#
2b9870ba9STom Jones#
33db693f7STom Jones# SPDX-License-Identifier: BSD-2-Clause
4b9870ba9STom Jones#
5b9870ba9STom Jones# Copyright © 2023 Tom Jones <thj@freebsd.org>
6b9870ba9STom Jones#
7b9870ba9STom Jones# Redistribution and use in source and binary forms, with or without
8b9870ba9STom Jones# modification, are permitted provided that the following conditions
9b9870ba9STom Jones# are met:
10b9870ba9STom Jones# 1. Redistributions of source code must retain the above copyright
11b9870ba9STom Jones#    notice, this list of conditions and the following disclaimer.
12b9870ba9STom Jones# 2. Redistributions in binary form must reproduce the above copyright
13b9870ba9STom Jones#    notice, this list of conditions and the following disclaimer in the
14b9870ba9STom Jones#    documentation and/or other materials provided with the distribution.
15b9870ba9STom Jones#
16b9870ba9STom Jones# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b9870ba9STom Jones# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b9870ba9STom Jones# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b9870ba9STom Jones# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20b9870ba9STom Jones# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b9870ba9STom Jones# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b9870ba9STom Jones# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b9870ba9STom Jones# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b9870ba9STom Jones# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b9870ba9STom Jones# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b9870ba9STom Jones# SUCH DAMAGE.
27b9870ba9STom Jones
28b9870ba9STom Jones. $(atf_get_srcdir)/utils.subr
29b9870ba9STom Jones
30b9870ba9STom Jonesatf_test_case "tcp_v6" "cleanup"
31b9870ba9STom Jonestcp_v6_head()
32b9870ba9STom Jones{
33b9870ba9STom Jones	atf_set descr 'TCP rdr with IPv6'
34b9870ba9STom Jones	atf_set require.user root
35*1f1963bdSJose Luis Duran	atf_set require.progs python3
36b9870ba9STom Jones}
37b9870ba9STom Jones
38b9870ba9STom Jones#
39b9870ba9STom Jones# Test that rdr works for TCP with IPv6.
40b9870ba9STom Jones#
41b9870ba9STom Jones#	a <-----> b <-----> c
42b9870ba9STom Jones#
43b9870ba9STom Jones# Test configures three jails (a, b and c) and connects them together with b as
44b9870ba9STom Jones# a router between a and c.
45b9870ba9STom Jones#
46b9870ba9STom Jones# TCP traffic to b on port 80 is redirected to c on port 8000
47b9870ba9STom Jones#
48b9870ba9STom Jones# Test for incorrect checksums after the rewrite by looking at a packet capture (see bug 210860)
49b9870ba9STom Jones#
50b9870ba9STom Jonestcp_v6_body()
51b9870ba9STom Jones{
52b9870ba9STom Jones	pft_init
53b9870ba9STom Jones
54b9870ba9STom Jones	j="rdr:tcp_v6"
55b9870ba9STom Jones	epair_one=$(vnet_mkepair)
56b9870ba9STom Jones	epair_two=$(vnet_mkepair)
57b9870ba9STom Jones
58b9870ba9STom Jones	echo $epair_one
59b9870ba9STom Jones	echo $epair_two
60b9870ba9STom Jones
61b9870ba9STom Jones	vnet_mkjail ${j}a ${epair_one}b
62b9870ba9STom Jones	vnet_mkjail ${j}b ${epair_one}a ${epair_two}a
63b9870ba9STom Jones	vnet_mkjail ${j}c ${epair_two}b
64b9870ba9STom Jones
65b9870ba9STom Jones	# configure addresses for b
66b9870ba9STom Jones	jexec ${j}b ifconfig lo0 up
67b9870ba9STom Jones	jexec ${j}b ifconfig ${epair_one}a inet6 2001:db8:a::1/64 up no_dad
68b9870ba9STom Jones	jexec ${j}b ifconfig ${epair_two}a inet6 2001:db8:b::1/64 up no_dad
69b9870ba9STom Jones
70b9870ba9STom Jones	# configure addresses for a
71b9870ba9STom Jones	jexec ${j}a ifconfig lo0 up
72b9870ba9STom Jones	jexec ${j}a ifconfig ${epair_one}b inet6 2001:db8:a::2/64 up no_dad
73b9870ba9STom Jones
74b9870ba9STom Jones	# configure addresses for c
75b9870ba9STom Jones	jexec ${j}c ifconfig lo0 up
76b9870ba9STom Jones	jexec ${j}c ifconfig ${epair_two}b inet6 2001:db8:b::2/64 up no_dad
77b9870ba9STom Jones
78b9870ba9STom Jones	# enable forwarding in the b jail
79b9870ba9STom Jones	jexec ${j}b sysctl net.inet6.ip6.forwarding=1
80b9870ba9STom Jones
81b9870ba9STom Jones	# add routes so a and c can find each other
82b9870ba9STom Jones	jexec ${j}a route add -inet6 2001:db8:b::0/64 2001:db8:a::1
83b9870ba9STom Jones	jexec ${j}c route add -inet6 2001:db8:a::0/64 2001:db8:b::1
84b9870ba9STom Jones
85b9870ba9STom Jones	jexec ${j}b pfctl -e
86b9870ba9STom Jones
87b9870ba9STom Jones	pft_set_rules ${j}b \
88b9870ba9STom Jones		"rdr on ${epair_one}a proto tcp from any to any port 80 -> 2001:db8:b::2 port 8000"
89b9870ba9STom Jones
90b9870ba9STom Jones	# Check that a can reach c over the router
91b9870ba9STom Jones	atf_check -s exit:0 -o ignore \
92b9870ba9STom Jones	    jexec ${j}a ping -6 -c 1 2001:db8:b::2
93b9870ba9STom Jones
94b9870ba9STom Jones	# capture packets on c so we can look for incorrect checksums
95b0fcf4d5SKristof Provost	jexec ${j}c tcpdump --immediate-mode -w ${PWD}/${j}.pcap tcp and port 8000 &
96b9870ba9STom Jones	tcpdumppid=$!
97b9870ba9STom Jones
98b9870ba9STom Jones	# start a web server and give it a second to start
99b9870ba9STom Jones	jexec ${j}c python3 -m http.server &
100b9870ba9STom Jones	sleep 1
101b9870ba9STom Jones
102b9870ba9STom Jones	# http directly from a to c -> a ---> b
103b9870ba9STom Jones	atf_check -s exit:0 -o ignore \
104b9870ba9STom Jones		jexec ${j}a fetch -T 1 -o /dev/null -q "http://[2001:db8:b::2]:8000"
105b9870ba9STom Jones
106b9870ba9STom Jones	# http from a to b with a redirect  -> a ---> b
107b9870ba9STom Jones	atf_check -s exit:0 -o ignore \
108b9870ba9STom Jones		jexec ${j}a fetch -T 1 -o /dev/null -q "http://[2001:db8:a::1]:80"
109b9870ba9STom Jones
110b9870ba9STom Jones	# ask tcpdump to stop so we can check the packet capture
111b9870ba9STom Jones	jexec ${j}c kill -s SIGINT $tcpdumppid
112b9870ba9STom Jones
113b9870ba9STom Jones	# Check for 'incorrect' in packet capture, this should tell us if
114b9870ba9STom Jones	# checksums are bad with rdr rules
115b0fcf4d5SKristof Provost	count=$(jexec ${j}c tcpdump -vvvv -r ${PWD}/${j}.pcap | grep incorrect | wc -l)
116b9870ba9STom Jones	atf_check_equal "       0" "$count"
117b9870ba9STom Jones}
118b9870ba9STom Jones
119b9870ba9STom Jonestcp_v6_cleanup()
120b9870ba9STom Jones{
121b9870ba9STom Jones	pft_cleanup
122b9870ba9STom Jones}
123b9870ba9STom Jones
1249897a669SMark Johnston
1259897a669SMark Johnstonatf_test_case "srcport" "cleanup"
1269897a669SMark Johnstonsrcport_head()
1279897a669SMark Johnston{
1289897a669SMark Johnston	atf_set descr 'TCP rdr srcport modulation'
1299897a669SMark Johnston	atf_set require.user root
1309897a669SMark Johnston	atf_set require.progs python3
1319897a669SMark Johnston	atf_set timeout 9999
1329897a669SMark Johnston}
1339897a669SMark Johnston
1349897a669SMark Johnston#
1359897a669SMark Johnston# Test that rdr works for multiple TCP with same srcip and srcport.
1369897a669SMark Johnston#
1379897a669SMark Johnston# Four jails, a, b, c, d, are used:
1389897a669SMark Johnston# - jail d runs a server on port 8888,
1399897a669SMark Johnston# - jail a makes connections to the server, routed through jails b and c,
1409897a669SMark Johnston# - jail b uses NAT to rewrite source addresses and ports to the same 2-tuple,
1419897a669SMark Johnston#   avoiding the need to use SO_REUSEADDR in jail a,
1429897a669SMark Johnston# - jail c uses a redirect rule to map the destination address to the same
1439897a669SMark Johnston#   address and port, resulting in a NAT state conflict.
1449897a669SMark Johnston#
1459897a669SMark Johnston# In this case, the rdr rule should also rewrite the source port (again) to
1469897a669SMark Johnston# resolve the state conflict.
1479897a669SMark Johnston#
1489897a669SMark Johnstonsrcport_body()
1499897a669SMark Johnston{
1509897a669SMark Johnston	pft_init
1519897a669SMark Johnston
1529897a669SMark Johnston	j="rdr:srcport"
1539897a669SMark Johnston	epair1=$(vnet_mkepair)
1549897a669SMark Johnston	epair2=$(vnet_mkepair)
1559897a669SMark Johnston	epair3=$(vnet_mkepair)
1569897a669SMark Johnston
1579897a669SMark Johnston	echo $epair_one
1589897a669SMark Johnston	echo $epair_two
1599897a669SMark Johnston
1609897a669SMark Johnston	vnet_mkjail ${j}a ${epair1}a
1619897a669SMark Johnston	vnet_mkjail ${j}b ${epair1}b ${epair2}a
1629897a669SMark Johnston	vnet_mkjail ${j}c ${epair2}b ${epair3}a
1639897a669SMark Johnston	vnet_mkjail ${j}d ${epair3}b
1649897a669SMark Johnston
1659897a669SMark Johnston	# configure addresses for a
1669897a669SMark Johnston	jexec ${j}a ifconfig lo0 up
1679897a669SMark Johnston	jexec ${j}a ifconfig ${epair1}a inet 198.51.100.50/24 up
1689897a669SMark Johnston	jexec ${j}a ifconfig ${epair1}a inet alias 198.51.100.51/24
1699897a669SMark Johnston	jexec ${j}a ifconfig ${epair1}a inet alias 198.51.100.52/24
1709897a669SMark Johnston
1719897a669SMark Johnston	# configure addresses for b
1729897a669SMark Johnston	jexec ${j}b ifconfig lo0 up
1739897a669SMark Johnston	jexec ${j}b ifconfig ${epair1}b inet 198.51.100.1/24 up
1749897a669SMark Johnston	jexec ${j}b ifconfig ${epair2}a inet 198.51.101.2/24 up
1759897a669SMark Johnston
1769897a669SMark Johnston	# configure addresses for c
1779897a669SMark Johnston	jexec ${j}c ifconfig lo0 up
1789897a669SMark Johnston	jexec ${j}c ifconfig ${epair2}b inet 198.51.101.3/24 up
1799897a669SMark Johnston	jexec ${j}c ifconfig ${epair2}b inet alias 198.51.101.4/24
1809897a669SMark Johnston	jexec ${j}c ifconfig ${epair2}b inet alias 198.51.101.5/24
1819897a669SMark Johnston	jexec ${j}c ifconfig ${epair3}a inet 203.0.113.1/24 up
1829897a669SMark Johnston
1839897a669SMark Johnston	# configure addresses for d
1849897a669SMark Johnston	jexec ${j}d ifconfig lo0 up
1859897a669SMark Johnston	jexec ${j}d ifconfig ${epair3}b inet 203.0.113.50/24 up
1869897a669SMark Johnston
1879897a669SMark Johnston	jexec ${j}b sysctl net.inet.ip.forwarding=1
1889897a669SMark Johnston	jexec ${j}c sysctl net.inet.ip.forwarding=1
1899897a669SMark Johnston	jexec ${j}b pfctl -e
1909897a669SMark Johnston	jexec ${j}c pfctl -e
1919897a669SMark Johnston
1929897a669SMark Johnston	pft_set_rules ${j}b \
1939897a669SMark Johnston		"set debug misc" \
1949897a669SMark Johnston		"nat on ${epair2}a inet from 198.51.100.0/24 to any -> ${epair2}a static-port"
1959897a669SMark Johnston
1969897a669SMark Johnston	pft_set_rules ${j}c \
1979897a669SMark Johnston		"set debug misc" \
1989897a669SMark Johnston		"rdr on ${epair2}b proto tcp from any to ${epair2}b port 7777 -> 203.0.113.50 port 8888"
1999897a669SMark Johnston
2009897a669SMark Johnston	jexec ${j}a route add default 198.51.100.1
2019897a669SMark Johnston	jexec ${j}c route add 198.51.100.0/24 198.51.101.2
2029897a669SMark Johnston	jexec ${j}d route add 198.51.101.0/24 203.0.113.1
2039897a669SMark Johnston
2049897a669SMark Johnston	jexec ${j}d python3 $(atf_get_srcdir)/rdr-srcport.py &
2059897a669SMark Johnston        sleep 1
2069897a669SMark Johnston
2079897a669SMark Johnston	echo a | jexec ${j}a nc -w 3 -s 198.51.100.50 -p 1234 198.51.101.3 7777 > port1
2089897a669SMark Johnston
2099897a669SMark Johnston	jexec ${j}a nc -s 198.51.100.51 -p 1234 198.51.101.4 7777 > port2 &
2109897a669SMark Johnston	jexec ${j}a nc -s 198.51.100.52 -p 1234 198.51.101.5 7777 > port3 &
2119897a669SMark Johnston	sleep 1
2129897a669SMark Johnston
2139897a669SMark Johnston	atf_check -o inline:"1234" cat port1
2149897a669SMark Johnston	atf_check -o match:"[0-9]+" -o not-inline:"1234" cat port2
2159897a669SMark Johnston	atf_check -o match:"[0-9]+" -o not-inline:"1234" cat port3
2169897a669SMark Johnston}
2179897a669SMark Johnston
2189897a669SMark Johnstonsrcport_cleanup()
2199897a669SMark Johnston{
2209897a669SMark Johnston	pft_cleanup
2219897a669SMark Johnston}
2229897a669SMark Johnston
223b9870ba9STom Jonesatf_init_test_cases()
224b9870ba9STom Jones{
225b9870ba9STom Jones	atf_add_test_case "tcp_v6"
2269897a669SMark Johnston	atf_add_test_case "srcport"
227b9870ba9STom Jones}
228