1# 2# 3# SPDX-License-Identifier: BSD-2-Clause 4# 5# Copyright © 2023 Tom Jones <thj@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 30# 31# Test that rdr works for TCP with IPv6. 32# 33# a <-----> b <-----> c 34# 35# Test configures three jails (a, b and c) and connects them together with b as 36# a router between a and c. 37# 38# TCP traffic to b on port 80 is redirected to c on port 8000 39# 40# Test for incorrect checksums after the rewrite by looking at a packet capture (see bug 210860) 41# 42tcp_v6_setup() 43{ 44 pft_init 45 46 j="rdr:tcp_v6" 47 epair_one=$(vnet_mkepair) 48 epair_two=$(vnet_mkepair) 49 50 echo $epair_one 51 echo $epair_two 52 53 vnet_mkjail ${j}a ${epair_one}b 54 vnet_mkjail ${j}b ${epair_one}a ${epair_two}a 55 vnet_mkjail ${j}c ${epair_two}b 56 57 # configure addresses for b 58 jexec ${j}b ifconfig lo0 up 59 jexec ${j}b ifconfig ${epair_one}a inet6 2001:db8:a::1/64 up no_dad 60 jexec ${j}b ifconfig ${epair_two}a inet6 2001:db8:b::1/64 up no_dad 61 62 # configure addresses for a 63 jexec ${j}a ifconfig lo0 up 64 jexec ${j}a ifconfig ${epair_one}b inet6 2001:db8:a::2/64 up no_dad 65 66 # configure addresses for c 67 jexec ${j}c ifconfig lo0 up 68 jexec ${j}c ifconfig ${epair_two}b inet6 2001:db8:b::2/64 up no_dad 69 70 # enable forwarding in the b jail 71 jexec ${j}b sysctl net.inet6.ip6.forwarding=1 72 73 # add routes so a and c can find each other 74 jexec ${j}a route add -inet6 2001:db8:b::0/64 2001:db8:a::1 75 jexec ${j}c route add -inet6 2001:db8:a::0/64 2001:db8:b::1 76 77 jexec ${j}b pfctl -e 78} 79 80tcp_v6_common() 81{ 82 pft_set_rules ${j}b "${1}" 83 84 # Check that a can reach c over the router 85 atf_check -s exit:0 -o ignore \ 86 jexec ${j}a ping -6 -c 1 2001:db8:b::2 87 88 # capture packets on c so we can look for incorrect checksums 89 jexec ${j}c tcpdump --immediate-mode -w ${PWD}/${j}.pcap tcp and port 8000 & 90 tcpdumppid=$! 91 92 # start a web server and give it a second to start 93 jexec ${j}c python3 -m http.server & 94 sleep 1 95 96 # http directly from a to c -> a ---> b 97 atf_check -s exit:0 -o ignore \ 98 jexec ${j}a fetch -T 1 -o /dev/null -q "http://[2001:db8:b::2]:8000" 99 100 # http from a to b with a redirect -> a ---> b 101 atf_check -s exit:0 -o ignore \ 102 jexec ${j}a fetch -T 1 -o /dev/null -q "http://[2001:db8:a::1]:80" 103 104 # ask tcpdump to stop so we can check the packet capture 105 jexec ${j}c kill -s SIGINT $tcpdumppid 106 107 # Check for 'incorrect' in packet capture, this should tell us if 108 # checksums are bad with rdr rules 109 count=$(jexec ${j}c tcpdump -vvvv -r ${PWD}/${j}.pcap | grep incorrect | wc -l) 110 atf_check_equal " 0" "$count" 111} 112 113atf_test_case "tcp_v6_compat" "cleanup" 114tcp_v6_compat_head() 115{ 116 atf_set descr 'TCP rdr with IPv6 with NAT rules' 117 atf_set require.user root 118 atf_set require.progs python3 119} 120 121tcp_v6_compat_body() 122{ 123 tcp_v6_setup # Sets ${epair_…} variables 124 tcp_v6_common \ 125 "rdr on ${epair_one}a proto tcp from any to any port 80 -> 2001:db8:b::2 port 8000" 126} 127 128tcp_v6_compat_cleanup() 129{ 130 pft_cleanup 131} 132 133atf_test_case "tcp_v6_pass" "cleanup" 134tcp_v6_pass_head() 135{ 136 atf_set descr 'TCP rdr with IPv6 with pass/match rules' 137 atf_set require.user root 138 atf_set require.progs python3 139} 140 141tcp_v6_pass_body() 142{ 143 tcp_v6_setup # Sets ${epair_…} variables 144 tcp_v6_common \ 145 "pass in on ${epair_one}a proto tcp from any to any port 80 rdr-to 2001:db8:b::2 port 8000" 146} 147 148tcp_v6_pass_cleanup() 149{ 150 pft_cleanup 151} 152 153# 154# Test that rdr works for multiple TCP with same srcip and srcport. 155# 156# Four jails, a, b, c, d, are used: 157# - jail d runs a server on port 8888, 158# - jail a makes connections to the server, routed through jails b and c, 159# - jail b uses NAT to rewrite source addresses and ports to the same 2-tuple, 160# avoiding the need to use SO_REUSEADDR in jail a, 161# - jail c uses a redirect rule to map the destination address to the same 162# address and port, resulting in a NAT state conflict. 163# 164# In this case, the rdr rule should also rewrite the source port (again) to 165# resolve the state conflict. 166# 167srcport_setup() 168{ 169 pft_init 170 171 j="rdr:srcport" 172 epair1=$(vnet_mkepair) 173 epair2=$(vnet_mkepair) 174 epair3=$(vnet_mkepair) 175 176 echo $epair_one 177 echo $epair_two 178 179 vnet_mkjail ${j}a ${epair1}a 180 vnet_mkjail ${j}b ${epair1}b ${epair2}a 181 vnet_mkjail ${j}c ${epair2}b ${epair3}a 182 vnet_mkjail ${j}d ${epair3}b 183 184 # configure addresses for a 185 jexec ${j}a ifconfig lo0 up 186 jexec ${j}a ifconfig ${epair1}a inet 198.51.100.50/24 up 187 jexec ${j}a ifconfig ${epair1}a inet alias 198.51.100.51/24 188 jexec ${j}a ifconfig ${epair1}a inet alias 198.51.100.52/24 189 190 # configure addresses for b 191 jexec ${j}b ifconfig lo0 up 192 jexec ${j}b ifconfig ${epair1}b inet 198.51.100.1/24 up 193 jexec ${j}b ifconfig ${epair2}a inet 198.51.101.2/24 up 194 195 # configure addresses for c 196 jexec ${j}c ifconfig lo0 up 197 jexec ${j}c ifconfig ${epair2}b inet 198.51.101.3/24 up 198 jexec ${j}c ifconfig ${epair2}b inet alias 198.51.101.4/24 199 jexec ${j}c ifconfig ${epair2}b inet alias 198.51.101.5/24 200 jexec ${j}c ifconfig ${epair3}a inet 203.0.113.1/24 up 201 202 # configure addresses for d 203 jexec ${j}d ifconfig lo0 up 204 jexec ${j}d ifconfig ${epair3}b inet 203.0.113.50/24 up 205 206 jexec ${j}b sysctl net.inet.ip.forwarding=1 207 jexec ${j}c sysctl net.inet.ip.forwarding=1 208 jexec ${j}b pfctl -e 209 jexec ${j}c pfctl -e 210} 211 212srcport_common() 213{ 214 pft_set_rules ${j}b \ 215 "set debug misc" \ 216 "${1}" 217 218 pft_set_rules ${j}c \ 219 "set debug misc" \ 220 "${2}" 221 222 jexec ${j}a route add default 198.51.100.1 223 jexec ${j}c route add 198.51.100.0/24 198.51.101.2 224 jexec ${j}d route add 198.51.101.0/24 203.0.113.1 225 226 jexec ${j}d python3 $(atf_get_srcdir)/rdr-srcport.py & 227 sleep 1 228 229 echo a | jexec ${j}a nc -w 3 -s 198.51.100.50 -p 1234 198.51.101.3 7777 > port1 230 231 jexec ${j}a nc -s 198.51.100.51 -p 1234 198.51.101.4 7777 > port2 & 232 jexec ${j}a nc -s 198.51.100.52 -p 1234 198.51.101.5 7777 > port3 & 233 sleep 1 234 235 atf_check -o inline:"1234" cat port1 236 atf_check -o match:"[0-9]+" -o not-inline:"1234" cat port2 237 atf_check -o match:"[0-9]+" -o not-inline:"1234" cat port3 238} 239 240atf_test_case "srcport_compat" "cleanup" 241srcport_compat_head() 242{ 243 atf_set descr 'TCP rdr srcport modulation with NAT rules' 244 atf_set require.user root 245 atf_set require.progs python3 246 atf_set timeout 9999 247} 248 249srcport_compat_body() 250{ 251 srcport_setup # Sets ${epair_…} variables 252 srcport_common \ 253 "nat on ${epair2}a inet from 198.51.100.0/24 to any -> ${epair2}a static-port" \ 254 "rdr on ${epair2}b proto tcp from any to ${epair2}b port 7777 -> 203.0.113.50 port 8888" 255} 256 257srcport_compat_cleanup() 258{ 259 pft_cleanup 260} 261 262atf_test_case "srcport_pass" "cleanup" 263srcport_pass_head() 264{ 265 atf_set descr 'TCP rdr srcport modulation with pass/match rules' 266 atf_set require.user root 267 atf_set require.progs python3 268 atf_set timeout 9999 269} 270 271srcport_pass_body() 272{ 273 srcport_setup # Sets ${epair_…} variables 274 srcport_common \ 275 "pass out on ${epair2}a inet from 198.51.100.0/24 to any nat-to ${epair2}a static-port" \ 276 "pass in on ${epair2}b proto tcp from any to ${epair2}b port 7777 rdr-to 203.0.113.50 port 8888" 277} 278 279srcport_pass_cleanup() 280{ 281 pft_cleanup 282} 283 284atf_test_case "natpass" "cleanup" 285natpass_head() 286{ 287 atf_set descr 'Test rdr pass' 288 atf_set require.user root 289} 290 291natpass_body() 292{ 293 pft_init 294 295 epair=$(vnet_mkepair) 296 epair_link=$(vnet_mkepair) 297 298 ifconfig ${epair}a 192.0.2.2/24 up 299 300 vnet_mkjail alcatraz ${epair}b ${epair_link}a 301 jexec alcatraz ifconfig lo0 inet 127.0.0.1/8 up 302 jexec alcatraz ifconfig ${epair}b inet 192.0.2.1/24 up 303 jexec alcatraz ifconfig ${epair_link}a 198.51.100.1/24 up 304 jexec alcatraz sysctl net.inet.ip.forwarding=1 305 306 vnet_mkjail srv ${epair_link}b 307 jexec srv ifconfig ${epair_link}b inet 198.51.100.2/24 up 308 jexec srv route add default 198.51.100.1 309 310 # Sanity check 311 atf_check -s exit:0 -o ignore \ 312 ping -c 1 192.0.2.1 313 atf_check -s exit:0 -o ignore \ 314 jexec alcatraz ping -c 1 198.51.100.2 315 316 jexec alcatraz pfctl -e 317 pft_set_rules alcatraz \ 318 "rdr pass on ${epair}b proto udp from any to 192.0.2.1 port 80 -> 198.51.100.2" \ 319 "nat on ${epair}b inet from 198.51.100.0/24 to any -> 192.0.2.1" \ 320 "block in proto udp from any to any port 80" \ 321 "pass in proto icmp" 322 323 echo "foo" | jexec srv nc -u -l 80 & 324 sleep 1 # Give the above a moment to start 325 326 out=$(echo 1 | nc -u -w 1 192.0.2.1 80) 327 echo "out ${out}" 328 if [ "${out}" != "foo" ]; 329 then 330 jexec alcatraz pfctl -sn -vv 331 jexec alcatraz pfctl -ss -vv 332 atf_fail "rdr failed" 333 fi 334} 335 336natpass_cleanup() 337{ 338 pft_cleanup 339} 340 341atf_test_case "pr290177" "cleanup" 342pr290177_head() 343{ 344 atf_set descr 'Test PR290177' 345 atf_set require.user root 346} 347 348pr290177_body() 349{ 350 pft_init 351 352 epair=$(vnet_mkepair) 353 354 ifconfig ${epair}a 192.0.2.2/24 up 355 ifconfig ${epair}a inet alias 192.0.2.3/24 up 356 357 vnet_mkjail alcatraz ${epair}b 358 jexec alcatraz ifconfig ${epair}b 192.0.2.1/24 up 359 jexec alcatraz ifconfig lo0 127.0.0.1/8 up 360 361 # Sanity check 362 atf_check -s exit:0 -o ignore \ 363 ping -c 1 192.0.2.1 364 365 jexec alcatraz pfctl -e 366 pft_set_rules alcatraz \ 367 "table <white> { 192.0.2.2 }" \ 368 "no rdr inet proto tcp from <white> to any port 25" \ 369 "rdr pass inet proto tcp from any to any port 25 -> 127.0.0.1 port 2500" 370 371 echo foo | jexec alcatraz nc -N -l 2500 & 372 sleep 1 373 374 reply=$(nc -w 3 -s 192.0.2.2 192.0.2.1 25) 375 if [ "${reply}" == "foo" ] 376 then 377 atf_fail "no rdr rule failed" 378 fi 379 reply=$(nc -w 3 -s 192.0.2.3 192.0.2.1 25) 380 if [ "${reply}" != "foo" ] 381 then 382 atf_fail "rdr rule failed" 383 fi 384} 385 386pr290177_cleanup() 387{ 388 pft_cleanup 389} 390 391atf_init_test_cases() 392{ 393 atf_add_test_case "natpass" 394 atf_add_test_case "tcp_v6_compat" 395 atf_add_test_case "tcp_v6_pass" 396 atf_add_test_case "srcport_compat" 397 atf_add_test_case "srcport_pass" 398 atf_add_test_case "pr290177" 399} 400