1065b5c7fSKristof Provost# 24d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 3065b5c7fSKristof Provost# 4065b5c7fSKristof Provost# Copyright (c) 2021 Rubicon Communications, LLC (Netgate) 5065b5c7fSKristof Provost# 6065b5c7fSKristof Provost# Redistribution and use in source and binary forms, with or without 7065b5c7fSKristof Provost# modification, are permitted provided that the following conditions 8065b5c7fSKristof Provost# are met: 9065b5c7fSKristof Provost# 1. Redistributions of source code must retain the above copyright 10065b5c7fSKristof Provost# notice, this list of conditions and the following disclaimer. 11065b5c7fSKristof Provost# 2. Redistributions in binary form must reproduce the above copyright 12065b5c7fSKristof Provost# notice, this list of conditions and the following disclaimer in the 13065b5c7fSKristof Provost# documentation and/or other materials provided with the distribution. 14065b5c7fSKristof Provost# 15065b5c7fSKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16065b5c7fSKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17065b5c7fSKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18065b5c7fSKristof Provost# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19065b5c7fSKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20065b5c7fSKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21065b5c7fSKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22065b5c7fSKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23065b5c7fSKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24065b5c7fSKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25065b5c7fSKristof Provost# SUCH DAMAGE. 26065b5c7fSKristof Provost 27065b5c7fSKristof Provost. $(atf_get_srcdir)/utils.subr 28065b5c7fSKristof Provost 29065b5c7fSKristof Provostcommon_dir=$(atf_get_srcdir)/../common 30065b5c7fSKristof Provost 3145258e1bSKristof Provostfind_state() 3245258e1bSKristof Provost{ 33501b7fbdSKristof Provost jail=${1:-alcatraz} 34501b7fbdSKristof Provost ip=${2:-192.0.2.2} 35501b7fbdSKristof Provost 36501b7fbdSKristof Provost jexec ${jail} pfctl -ss | grep icmp | grep ${ip} 3745258e1bSKristof Provost} 3845258e1bSKristof Provost 3945258e1bSKristof Provostfind_state_v6() 4045258e1bSKristof Provost{ 4145258e1bSKristof Provost jexec alcatraz pfctl -ss | grep icmp | grep 2001:db8::2 4245258e1bSKristof Provost} 4345258e1bSKristof Provost 4445258e1bSKristof Provost 45065b5c7fSKristof Provostatf_test_case "v4" "cleanup" 46065b5c7fSKristof Provostv4_head() 47065b5c7fSKristof Provost{ 48065b5c7fSKristof Provost atf_set descr 'Test killing states by IPv4 address' 49065b5c7fSKristof Provost atf_set require.user root 50065b5c7fSKristof Provost atf_set require.progs scapy 51065b5c7fSKristof Provost} 52065b5c7fSKristof Provost 53065b5c7fSKristof Provostv4_body() 54065b5c7fSKristof Provost{ 55065b5c7fSKristof Provost pft_init 56065b5c7fSKristof Provost 57065b5c7fSKristof Provost epair=$(vnet_mkepair) 58065b5c7fSKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 59065b5c7fSKristof Provost 60065b5c7fSKristof Provost vnet_mkjail alcatraz ${epair}b 61065b5c7fSKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 62065b5c7fSKristof Provost jexec alcatraz pfctl -e 63065b5c7fSKristof Provost 64065b5c7fSKristof Provost pft_set_rules alcatraz "block all" \ 653a1f834bSDoug Rabson "pass in proto icmp" \ 663a1f834bSDoug Rabson "set skip on lo" 67065b5c7fSKristof Provost 68065b5c7fSKristof Provost # Sanity check & establish state 69065b5c7fSKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 70065b5c7fSKristof Provost --sendif ${epair}a \ 71065b5c7fSKristof Provost --to 192.0.2.2 \ 72065b5c7fSKristof Provost --replyif ${epair}a 73065b5c7fSKristof Provost 74065b5c7fSKristof Provost # Change rules to now deny the ICMP traffic 75065b5c7fSKristof Provost pft_set_rules noflush alcatraz "block all" 7645258e1bSKristof Provost if ! find_state; 7745258e1bSKristof Provost then 7845258e1bSKristof Provost atf_fail "Setting new rules removed the state." 7945258e1bSKristof Provost fi 80065b5c7fSKristof Provost 81065b5c7fSKristof Provost # Killing with the wrong IP doesn't affect our state 82065b5c7fSKristof Provost jexec alcatraz pfctl -k 192.0.2.3 8345258e1bSKristof Provost if ! find_state; 8445258e1bSKristof Provost then 8545258e1bSKristof Provost atf_fail "Killing with the wrong IP removed our state." 8645258e1bSKristof Provost fi 87065b5c7fSKristof Provost 88065b5c7fSKristof Provost # Killing with one correct address and one incorrect doesn't kill the state 89065b5c7fSKristof Provost jexec alcatraz pfctl -k 192.0.2.1 -k 192.0.2.3 9045258e1bSKristof Provost if ! find_state; 9145258e1bSKristof Provost then 9245258e1bSKristof Provost atf_fail "Killing with one wrong IP removed our state." 9345258e1bSKristof Provost fi 94065b5c7fSKristof Provost 95065b5c7fSKristof Provost # Killing with correct address does remove the state 96065b5c7fSKristof Provost jexec alcatraz pfctl -k 192.0.2.1 9745258e1bSKristof Provost if find_state; 9845258e1bSKristof Provost then 9945258e1bSKristof Provost atf_fail "Killing with the correct IP did not remove our state." 10045258e1bSKristof Provost fi 101065b5c7fSKristof Provost} 102065b5c7fSKristof Provost 103065b5c7fSKristof Provostv4_cleanup() 104065b5c7fSKristof Provost{ 105065b5c7fSKristof Provost pft_cleanup 106065b5c7fSKristof Provost} 107065b5c7fSKristof Provost 1089af23174SKristof Provostatf_test_case "v6" "cleanup" 1099af23174SKristof Provostv6_head() 1109af23174SKristof Provost{ 1119af23174SKristof Provost atf_set descr 'Test killing states by IPv6 address' 1129af23174SKristof Provost atf_set require.user root 1139af23174SKristof Provost atf_set require.progs scapy 1149af23174SKristof Provost} 1159af23174SKristof Provost 1169af23174SKristof Provostv6_body() 1179af23174SKristof Provost{ 1189af23174SKristof Provost pft_init 1199af23174SKristof Provost 120300f4be4SWarner Losh if [ "$(atf_config_get ci false)" = "true" ]; then 121300f4be4SWarner Losh atf_skip "https://bugs.freebsd.org/260458" 122300f4be4SWarner Losh fi 123300f4be4SWarner Losh 1249af23174SKristof Provost epair=$(vnet_mkepair) 1259af23174SKristof Provost ifconfig ${epair}a inet6 2001:db8::1/64 up no_dad 1269af23174SKristof Provost 1279af23174SKristof Provost vnet_mkjail alcatraz ${epair}b 1289af23174SKristof Provost jexec alcatraz ifconfig ${epair}b inet6 2001:db8::2/64 up no_dad 1299af23174SKristof Provost jexec alcatraz pfctl -e 1309af23174SKristof Provost 1319af23174SKristof Provost pft_set_rules alcatraz "block all" \ 1323a1f834bSDoug Rabson "pass in proto icmp6" \ 1333a1f834bSDoug Rabson "set skip on lo" 1349af23174SKristof Provost 1359af23174SKristof Provost # Sanity check & establish state 1369af23174SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 1379af23174SKristof Provost --sendif ${epair}a \ 1389af23174SKristof Provost --to 2001:db8::2 \ 1399af23174SKristof Provost --replyif ${epair}a 1409af23174SKristof Provost 1419af23174SKristof Provost # Change rules to now deny the ICMP traffic 1429af23174SKristof Provost pft_set_rules noflush alcatraz "block all" 14345258e1bSKristof Provost if ! find_state_v6; 14445258e1bSKristof Provost then 14545258e1bSKristof Provost atf_fail "Setting new rules removed the state." 14645258e1bSKristof Provost fi 1479af23174SKristof Provost 1489af23174SKristof Provost # Killing with the wrong IP doesn't affect our state 1499af23174SKristof Provost jexec alcatraz pfctl -k 2001:db8::3 15045258e1bSKristof Provost if ! find_state_v6; 15145258e1bSKristof Provost then 15245258e1bSKristof Provost atf_fail "Killing with the wrong IP removed our state." 15345258e1bSKristof Provost fi 1549af23174SKristof Provost 1559af23174SKristof Provost # Killing with one correct address and one incorrect doesn't kill the state 1569af23174SKristof Provost jexec alcatraz pfctl -k 2001:db8::1 -k 2001:db8::3 15745258e1bSKristof Provost if ! find_state_v6; 15845258e1bSKristof Provost then 15945258e1bSKristof Provost atf_fail "Killing with one wrong IP removed our state." 16045258e1bSKristof Provost fi 1619af23174SKristof Provost 1629af23174SKristof Provost # Killing with correct address does remove the state 1639af23174SKristof Provost jexec alcatraz pfctl -k 2001:db8::1 16445258e1bSKristof Provost if find_state_v6; 16545258e1bSKristof Provost then 16645258e1bSKristof Provost atf_fail "Killing with the correct IP did not remove our state." 16745258e1bSKristof Provost fi 1689af23174SKristof Provost} 1699af23174SKristof Provost 1709af23174SKristof Provostv6_cleanup() 1719af23174SKristof Provost{ 1729af23174SKristof Provost pft_cleanup 1739af23174SKristof Provost} 1749af23174SKristof Provost 175065b5c7fSKristof Provostatf_test_case "label" "cleanup" 176065b5c7fSKristof Provostlabel_head() 177065b5c7fSKristof Provost{ 178065b5c7fSKristof Provost atf_set descr 'Test killing states by label' 179065b5c7fSKristof Provost atf_set require.user root 180065b5c7fSKristof Provost atf_set require.progs scapy 181065b5c7fSKristof Provost} 182065b5c7fSKristof Provost 183065b5c7fSKristof Provostlabel_body() 184065b5c7fSKristof Provost{ 185065b5c7fSKristof Provost pft_init 186065b5c7fSKristof Provost 187065b5c7fSKristof Provost epair=$(vnet_mkepair) 188065b5c7fSKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 189065b5c7fSKristof Provost 190065b5c7fSKristof Provost vnet_mkjail alcatraz ${epair}b 191065b5c7fSKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 192065b5c7fSKristof Provost jexec alcatraz pfctl -e 193065b5c7fSKristof Provost 194065b5c7fSKristof Provost pft_set_rules alcatraz "block all" \ 195065b5c7fSKristof Provost "pass in proto tcp label bar" \ 1963a1f834bSDoug Rabson "pass in proto icmp label foo" \ 1973a1f834bSDoug Rabson "set skip on lo" 198065b5c7fSKristof Provost 199065b5c7fSKristof Provost # Sanity check & establish state 200065b5c7fSKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 201065b5c7fSKristof Provost --sendif ${epair}a \ 202065b5c7fSKristof Provost --to 192.0.2.2 \ 203065b5c7fSKristof Provost --replyif ${epair}a 204065b5c7fSKristof Provost 205065b5c7fSKristof Provost # Change rules to now deny the ICMP traffic 206065b5c7fSKristof Provost pft_set_rules noflush alcatraz "block all" 20745258e1bSKristof Provost if ! find_state; 20845258e1bSKristof Provost then 20945258e1bSKristof Provost atf_fail "Setting new rules removed the state." 21045258e1bSKristof Provost fi 211065b5c7fSKristof Provost 212065b5c7fSKristof Provost # Killing a label on a different rules keeps the state 213065b5c7fSKristof Provost jexec alcatraz pfctl -k label -k bar 21445258e1bSKristof Provost if ! find_state; 21545258e1bSKristof Provost then 21645258e1bSKristof Provost atf_fail "Killing a different label removed the state." 21745258e1bSKristof Provost fi 218065b5c7fSKristof Provost 219065b5c7fSKristof Provost # Killing a non-existing label keeps the state 220065b5c7fSKristof Provost jexec alcatraz pfctl -k label -k baz 22145258e1bSKristof Provost if ! find_state; 22245258e1bSKristof Provost then 22345258e1bSKristof Provost atf_fail "Killing a non-existing label removed the state." 22445258e1bSKristof Provost fi 225065b5c7fSKristof Provost 226065b5c7fSKristof Provost # Killing the correct label kills the state 227065b5c7fSKristof Provost jexec alcatraz pfctl -k label -k foo 22845258e1bSKristof Provost if find_state; 22945258e1bSKristof Provost then 23045258e1bSKristof Provost atf_fail "Killing the state did not remove it." 23145258e1bSKristof Provost fi 232065b5c7fSKristof Provost} 233065b5c7fSKristof Provost 234065b5c7fSKristof Provostlabel_cleanup() 235065b5c7fSKristof Provost{ 236065b5c7fSKristof Provost pft_cleanup 237065b5c7fSKristof Provost} 238065b5c7fSKristof Provost 2395632f585SKristof Provostatf_test_case "multilabel" "cleanup" 2405632f585SKristof Provostmultilabel_head() 2415632f585SKristof Provost{ 2425632f585SKristof Provost atf_set descr 'Test killing states with multiple labels by label' 2435632f585SKristof Provost atf_set require.user root 2445632f585SKristof Provost atf_set require.progs scapy 2455632f585SKristof Provost} 2465632f585SKristof Provost 2475632f585SKristof Provostmultilabel_body() 2485632f585SKristof Provost{ 2495632f585SKristof Provost pft_init 2505632f585SKristof Provost 2515632f585SKristof Provost epair=$(vnet_mkepair) 2525632f585SKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 2535632f585SKristof Provost 2545632f585SKristof Provost vnet_mkjail alcatraz ${epair}b 2555632f585SKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 2565632f585SKristof Provost jexec alcatraz pfctl -e 2575632f585SKristof Provost 2585632f585SKristof Provost pft_set_rules alcatraz "block all" \ 2593a1f834bSDoug Rabson "pass in proto icmp label foo label bar" \ 2603a1f834bSDoug Rabson "set skip on lo" 2615632f585SKristof Provost 2625632f585SKristof Provost # Sanity check & establish state 2635632f585SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 2645632f585SKristof Provost --sendif ${epair}a \ 2655632f585SKristof Provost --to 192.0.2.2 \ 2665632f585SKristof Provost --replyif ${epair}a 2675632f585SKristof Provost 2685632f585SKristof Provost # Change rules to now deny the ICMP traffic 2695632f585SKristof Provost pft_set_rules noflush alcatraz "block all" 27045258e1bSKristof Provost if ! find_state; 27145258e1bSKristof Provost then 27245258e1bSKristof Provost atf_fail "Setting new rules removed the state." 27345258e1bSKristof Provost fi 2745632f585SKristof Provost 2755632f585SKristof Provost # Killing a label on a different rules keeps the state 2765632f585SKristof Provost jexec alcatraz pfctl -k label -k baz 27745258e1bSKristof Provost if ! find_state; 27845258e1bSKristof Provost then 27945258e1bSKristof Provost atf_fail "Killing a different label removed the state." 28045258e1bSKristof Provost fi 2815632f585SKristof Provost 2825632f585SKristof Provost # Killing the state with the last label works 2835632f585SKristof Provost jexec alcatraz pfctl -k label -k bar 28445258e1bSKristof Provost if find_state; 28545258e1bSKristof Provost then 28645258e1bSKristof Provost atf_fail "Killing with the last label did not remove the state." 28745258e1bSKristof Provost fi 2885632f585SKristof Provost 2895632f585SKristof Provost pft_set_rules alcatraz "block all" \ 2903a1f834bSDoug Rabson "pass in proto icmp label foo label bar" \ 2913a1f834bSDoug Rabson "set skip on lo" 2925632f585SKristof Provost 2935632f585SKristof Provost # Reestablish state 2945632f585SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 2955632f585SKristof Provost --sendif ${epair}a \ 2965632f585SKristof Provost --to 192.0.2.2 \ 2975632f585SKristof Provost --replyif ${epair}a 2985632f585SKristof Provost 2995632f585SKristof Provost # Change rules to now deny the ICMP traffic 3005632f585SKristof Provost pft_set_rules noflush alcatraz "block all" 30145258e1bSKristof Provost if ! find_state; 30245258e1bSKristof Provost then 30345258e1bSKristof Provost atf_fail "Setting new rules removed the state." 30445258e1bSKristof Provost fi 3055632f585SKristof Provost 3065632f585SKristof Provost # Killing with the first label works too 3075632f585SKristof Provost jexec alcatraz pfctl -k label -k foo 30845258e1bSKristof Provost if find_state; 30945258e1bSKristof Provost then 31045258e1bSKristof Provost atf_fail "Killing with the first label did not remove the state." 31145258e1bSKristof Provost fi 3125632f585SKristof Provost} 3135632f585SKristof Provost 3145632f585SKristof Provostmultilabel_cleanup() 3155632f585SKristof Provost{ 3165632f585SKristof Provost pft_cleanup 3175632f585SKristof Provost} 3185632f585SKristof Provost 319c2e11d81SKristof Provostatf_test_case "gateway" "cleanup" 320c2e11d81SKristof Provostgateway_head() 321c2e11d81SKristof Provost{ 322c2e11d81SKristof Provost atf_set descr 'Test killing states by route-to/reply-to address' 323c2e11d81SKristof Provost atf_set require.user root 324c2e11d81SKristof Provost atf_set require.progs scapy 325c2e11d81SKristof Provost} 326c2e11d81SKristof Provost 327c2e11d81SKristof Provostgateway_body() 328c2e11d81SKristof Provost{ 329c2e11d81SKristof Provost pft_init 330c2e11d81SKristof Provost 331c2e11d81SKristof Provost epair=$(vnet_mkepair) 332c2e11d81SKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 333c2e11d81SKristof Provost 334c2e11d81SKristof Provost vnet_mkjail alcatraz ${epair}b 335c2e11d81SKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 336c2e11d81SKristof Provost jexec alcatraz pfctl -e 337c2e11d81SKristof Provost 338c2e11d81SKristof Provost pft_set_rules alcatraz "block all" \ 3393a1f834bSDoug Rabson "pass in reply-to (${epair}b 192.0.2.1) proto icmp" \ 3403a1f834bSDoug Rabson "set skip on lo" 341c2e11d81SKristof Provost 342c2e11d81SKristof Provost # Sanity check & establish state 343c2e11d81SKristof Provost # Note: use pft_ping so we always use the same ID, so pf considers all 344c2e11d81SKristof Provost # echo requests part of the same flow. 345c2e11d81SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 346c2e11d81SKristof Provost --sendif ${epair}a \ 347c2e11d81SKristof Provost --to 192.0.2.2 \ 348c2e11d81SKristof Provost --replyif ${epair}a 349c2e11d81SKristof Provost 350c2e11d81SKristof Provost # Change rules to now deny the ICMP traffic 351c2e11d81SKristof Provost pft_set_rules noflush alcatraz "block all" 35245258e1bSKristof Provost if ! find_state; 35345258e1bSKristof Provost then 35445258e1bSKristof Provost atf_fail "Setting new rules removed the state." 35545258e1bSKristof Provost fi 356c2e11d81SKristof Provost 357c2e11d81SKristof Provost # Killing with a different gateway does not affect our state 358c2e11d81SKristof Provost jexec alcatraz pfctl -k gateway -k 192.0.2.2 35945258e1bSKristof Provost if ! find_state; 36045258e1bSKristof Provost then 36145258e1bSKristof Provost atf_fail "Killing with a different gateway removed the state." 36245258e1bSKristof Provost fi 363c2e11d81SKristof Provost 364c2e11d81SKristof Provost # Killing states with the relevant gateway does terminate our state 365c2e11d81SKristof Provost jexec alcatraz pfctl -k gateway -k 192.0.2.1 36645258e1bSKristof Provost if find_state; 36745258e1bSKristof Provost then 36845258e1bSKristof Provost atf_fail "Killing with the gateway did not remove the state." 36945258e1bSKristof Provost fi 370c2e11d81SKristof Provost} 371c2e11d81SKristof Provost 372c2e11d81SKristof Provostgateway_cleanup() 373c2e11d81SKristof Provost{ 374c2e11d81SKristof Provost pft_cleanup 375c2e11d81SKristof Provost} 376c2e11d81SKristof Provost 377ac200a9cSKristof Provostatf_test_case "match" "cleanup" 378ac200a9cSKristof Provostmatch_head() 379ac200a9cSKristof Provost{ 380ac200a9cSKristof Provost atf_set descr 'Test killing matching states' 381ac200a9cSKristof Provost atf_set require.user root 382ac200a9cSKristof Provost} 383ac200a9cSKristof Provost 38470dd30d4SKristof Provostwait_for_state() 38570dd30d4SKristof Provost{ 38670dd30d4SKristof Provost jail=$1 38770dd30d4SKristof Provost addr=$2 38870dd30d4SKristof Provost 38970dd30d4SKristof Provost while ! jexec $jail pfctl -s s | grep $addr >/dev/null; 39070dd30d4SKristof Provost do 39170dd30d4SKristof Provost sleep .1 39270dd30d4SKristof Provost done 39370dd30d4SKristof Provost} 39470dd30d4SKristof Provost 395ac200a9cSKristof Provostmatch_body() 396ac200a9cSKristof Provost{ 397ac200a9cSKristof Provost pft_init 398ac200a9cSKristof Provost 399ac200a9cSKristof Provost epair_one=$(vnet_mkepair) 400ac200a9cSKristof Provost ifconfig ${epair_one}a 192.0.2.1/24 up 401ac200a9cSKristof Provost 402ac200a9cSKristof Provost epair_two=$(vnet_mkepair) 403ac200a9cSKristof Provost 404ac200a9cSKristof Provost vnet_mkjail alcatraz ${epair_one}b ${epair_two}a 405ac200a9cSKristof Provost jexec alcatraz ifconfig ${epair_one}b 192.0.2.2/24 up 406ac200a9cSKristof Provost jexec alcatraz ifconfig ${epair_two}a 198.51.100.1/24 up 407ac200a9cSKristof Provost jexec alcatraz sysctl net.inet.ip.forwarding=1 408ac200a9cSKristof Provost jexec alcatraz pfctl -e 409ac200a9cSKristof Provost 410ac200a9cSKristof Provost vnet_mkjail singsing ${epair_two}b 411ac200a9cSKristof Provost jexec singsing ifconfig ${epair_two}b 198.51.100.2/24 up 412ac200a9cSKristof Provost jexec singsing route add default 198.51.100.1 413*b0fcf4d5SKristof Provost jexec singsing /usr/sbin/inetd -p ${PWD}/inetd-echo.pid \ 414ac200a9cSKristof Provost $(atf_get_srcdir)/echo_inetd.conf 415ac200a9cSKristof Provost 416ac200a9cSKristof Provost route add 198.51.100.0/24 192.0.2.2 417ac200a9cSKristof Provost 418ac200a9cSKristof Provost pft_set_rules alcatraz \ 419ac200a9cSKristof Provost "nat on ${epair_two}a from 192.0.2.0/24 -> (${epair_two}a)" \ 420ac200a9cSKristof Provost "pass all" 421ac200a9cSKristof Provost 422ac200a9cSKristof Provost nc 198.51.100.2 7 & 42370dd30d4SKristof Provost wait_for_state alcatraz 192.0.2.1 424ac200a9cSKristof Provost 425ac200a9cSKristof Provost # Expect two states 4264e860bd5SKristof Provost states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l) 427ac200a9cSKristof Provost if [ $states -ne 2 ] ; 428ac200a9cSKristof Provost then 429ac200a9cSKristof Provost atf_fail "Expected two states, found $states" 430ac200a9cSKristof Provost fi 431ac200a9cSKristof Provost 432ac200a9cSKristof Provost # If we don't kill the matching NAT state one should be left 433ac200a9cSKristof Provost jexec alcatraz pfctl -k 192.0.2.1 4344e860bd5SKristof Provost states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l) 435ac200a9cSKristof Provost if [ $states -ne 1 ] ; 436ac200a9cSKristof Provost then 437ac200a9cSKristof Provost atf_fail "Expected one states, found $states" 438ac200a9cSKristof Provost fi 439ac200a9cSKristof Provost 440ac200a9cSKristof Provost # Flush 441ac200a9cSKristof Provost jexec alcatraz pfctl -F states 442ac200a9cSKristof Provost 443ac200a9cSKristof Provost nc 198.51.100.2 7 & 44470dd30d4SKristof Provost wait_for_state alcatraz 192.0.2.1 445ac200a9cSKristof Provost 446ac200a9cSKristof Provost # Kill matching states, expect all of them to be gone 447ac200a9cSKristof Provost jexec alcatraz pfctl -M -k 192.0.2.1 4484e860bd5SKristof Provost states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l) 449ac200a9cSKristof Provost if [ $states -ne 0 ] ; 450ac200a9cSKristof Provost then 451ac200a9cSKristof Provost atf_fail "Expected zero states, found $states" 452ac200a9cSKristof Provost fi 453ac200a9cSKristof Provost} 454ac200a9cSKristof Provost 455ac200a9cSKristof Provostmatch_cleanup() 456ac200a9cSKristof Provost{ 457ac200a9cSKristof Provost pft_cleanup 458ac200a9cSKristof Provost} 459ac200a9cSKristof Provost 4607bd7933fSKristof Provostatf_test_case "interface" "cleanup" 4617bd7933fSKristof Provostinterface_head() 4627bd7933fSKristof Provost{ 4637bd7933fSKristof Provost atf_set descr 'Test killing states based on interface' 4647bd7933fSKristof Provost atf_set require.user root 4657bd7933fSKristof Provost atf_set require.progs scapy 4667bd7933fSKristof Provost} 4677bd7933fSKristof Provost 4687bd7933fSKristof Provostinterface_body() 4697bd7933fSKristof Provost{ 4707bd7933fSKristof Provost pft_init 4717bd7933fSKristof Provost 4727bd7933fSKristof Provost epair=$(vnet_mkepair) 4737bd7933fSKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 4747bd7933fSKristof Provost 4757bd7933fSKristof Provost vnet_mkjail alcatraz ${epair}b 4767bd7933fSKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 4777bd7933fSKristof Provost jexec alcatraz pfctl -e 4787bd7933fSKristof Provost 4797bd7933fSKristof Provost pft_set_rules alcatraz "block all" \ 4803a1f834bSDoug Rabson "pass in proto icmp" \ 4813a1f834bSDoug Rabson "set skip on lo" 4827bd7933fSKristof Provost 4837bd7933fSKristof Provost # Sanity check & establish state 4847bd7933fSKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 4857bd7933fSKristof Provost --sendif ${epair}a \ 4867bd7933fSKristof Provost --to 192.0.2.2 \ 4877bd7933fSKristof Provost --replyif ${epair}a 4887bd7933fSKristof Provost 4897bd7933fSKristof Provost # Change rules to now deny the ICMP traffic 4907bd7933fSKristof Provost pft_set_rules noflush alcatraz "block all" 49145258e1bSKristof Provost if ! find_state; 49245258e1bSKristof Provost then 49345258e1bSKristof Provost atf_fail "Setting new rules removed the state." 49445258e1bSKristof Provost fi 4957bd7933fSKristof Provost 4967bd7933fSKristof Provost # Flushing states on a different interface doesn't affect our state 4977bd7933fSKristof Provost jexec alcatraz pfctl -i ${epair}a -Fs 49845258e1bSKristof Provost if ! find_state; 49945258e1bSKristof Provost then 50045258e1bSKristof Provost atf_fail "Flushing on a different interface removed the state." 50145258e1bSKristof Provost fi 5027bd7933fSKristof Provost 5037bd7933fSKristof Provost # Flushing on the correct interface does (even with floating states) 5047bd7933fSKristof Provost jexec alcatraz pfctl -i ${epair}b -Fs 50545258e1bSKristof Provost if find_state; 50645258e1bSKristof Provost then 50745258e1bSKristof Provost atf_fail "Flushing on a the interface did not remove the state." 50845258e1bSKristof Provost fi 5097bd7933fSKristof Provost} 5107bd7933fSKristof Provost 5117bd7933fSKristof Provostinterface_cleanup() 5127bd7933fSKristof Provost{ 5137bd7933fSKristof Provost pft_cleanup 5147bd7933fSKristof Provost} 5157bd7933fSKristof Provost 516bbf832f3SKristof Provostatf_test_case "id" "cleanup" 517bbf832f3SKristof Provostid_head() 518bbf832f3SKristof Provost{ 519bbf832f3SKristof Provost atf_set descr 'Test killing states by id' 520bbf832f3SKristof Provost atf_set require.user root 521bbf832f3SKristof Provost atf_set require.progs scapy 522bbf832f3SKristof Provost} 523bbf832f3SKristof Provost 524bbf832f3SKristof Provostid_body() 525bbf832f3SKristof Provost{ 526bbf832f3SKristof Provost pft_init 527bbf832f3SKristof Provost 528bbf832f3SKristof Provost epair=$(vnet_mkepair) 529bbf832f3SKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 530bbf832f3SKristof Provost 531bbf832f3SKristof Provost vnet_mkjail alcatraz ${epair}b 532bbf832f3SKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 533bbf832f3SKristof Provost jexec alcatraz pfctl -e 534bbf832f3SKristof Provost 535bbf832f3SKristof Provost pft_set_rules alcatraz "block all" \ 536bbf832f3SKristof Provost "pass in proto tcp" \ 5373a1f834bSDoug Rabson "pass in proto icmp" \ 5383a1f834bSDoug Rabson "set skip on lo" 539bbf832f3SKristof Provost 540bbf832f3SKristof Provost # Sanity check & establish state 541bbf832f3SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 542bbf832f3SKristof Provost --sendif ${epair}a \ 543bbf832f3SKristof Provost --to 192.0.2.2 \ 544bbf832f3SKristof Provost --replyif ${epair}a 545bbf832f3SKristof Provost 546bbf832f3SKristof Provost # Change rules to now deny the ICMP traffic 547bbf832f3SKristof Provost pft_set_rules noflush alcatraz "block all" 54845258e1bSKristof Provost if ! find_state; 54945258e1bSKristof Provost then 55045258e1bSKristof Provost atf_fail "Setting new rules removed the state." 55145258e1bSKristof Provost fi 552bbf832f3SKristof Provost 553bbf832f3SKristof Provost # Get the state ID 554bbf832f3SKristof Provost id=$(jexec alcatraz pfctl -ss -vvv | grep -A 3 icmp | 555bbf832f3SKristof Provost grep -A 3 192.0.2.2 | awk '/id:/ { printf("%s/%s", $2, $4); }') 556bbf832f3SKristof Provost 557bbf832f3SKristof Provost # Kill the wrong ID 558bbf832f3SKristof Provost jexec alcatraz pfctl -k id -k 1 55945258e1bSKristof Provost if ! find_state; 56045258e1bSKristof Provost then 56145258e1bSKristof Provost atf_fail "Killing a different ID removed the state." 56245258e1bSKristof Provost fi 563bbf832f3SKristof Provost 564bbf832f3SKristof Provost # Kill the correct ID 565bbf832f3SKristof Provost jexec alcatraz pfctl -k id -k ${id} 56645258e1bSKristof Provost if find_state; 56745258e1bSKristof Provost then 56845258e1bSKristof Provost atf_fail "Killing the state did not remove it." 56945258e1bSKristof Provost fi 570bbf832f3SKristof Provost} 571bbf832f3SKristof Provost 572bbf832f3SKristof Provostid_cleanup() 573bbf832f3SKristof Provost{ 574bbf832f3SKristof Provost pft_cleanup 575bbf832f3SKristof Provost} 576bbf832f3SKristof Provost 577501b7fbdSKristof Provostatf_test_case "nat" "cleanup" 578501b7fbdSKristof Provostnat_head() 579501b7fbdSKristof Provost{ 580501b7fbdSKristof Provost atf_set descr 'Test killing states by their NAT-ed IP address' 581501b7fbdSKristof Provost atf_set require.user root 582501b7fbdSKristof Provost atf_set require.progs scapy 583501b7fbdSKristof Provost} 584501b7fbdSKristof Provost 585501b7fbdSKristof Provostnat_body() 586501b7fbdSKristof Provost{ 587501b7fbdSKristof Provost pft_init 588501b7fbdSKristof Provost j="killstate:nat" 589501b7fbdSKristof Provost 590501b7fbdSKristof Provost epair_c=$(vnet_mkepair) 591501b7fbdSKristof Provost epair_srv=$(vnet_mkepair) 592501b7fbdSKristof Provost 593501b7fbdSKristof Provost vnet_mkjail ${j}c ${epair_c}a 594501b7fbdSKristof Provost ifconfig -j ${j}c ${epair_c}a inet 192.0.2.2/24 up 595501b7fbdSKristof Provost jexec ${j}c route add default 192.0.2.1 596501b7fbdSKristof Provost 597501b7fbdSKristof Provost vnet_mkjail ${j}srv ${epair_srv}a 598501b7fbdSKristof Provost ifconfig -j ${j}srv ${epair_srv}a inet 198.51.100.2/24 up 599501b7fbdSKristof Provost 600501b7fbdSKristof Provost vnet_mkjail ${j}r ${epair_c}b ${epair_srv}b 601501b7fbdSKristof Provost ifconfig -j ${j}r ${epair_c}b inet 192.0.2.1/24 up 602501b7fbdSKristof Provost ifconfig -j ${j}r ${epair_srv}b inet 198.51.100.1/24 up 603501b7fbdSKristof Provost jexec ${j}r sysctl net.inet.ip.forwarding=1 604501b7fbdSKristof Provost 605501b7fbdSKristof Provost jexec ${j}r pfctl -e 606501b7fbdSKristof Provost pft_set_rules ${j}r \ 607501b7fbdSKristof Provost "nat on ${epair_srv}b inet from 192.0.2.0/24 to any -> (${epair_srv}b)" 608501b7fbdSKristof Provost 609501b7fbdSKristof Provost # Sanity check 610501b7fbdSKristof Provost atf_check -s exit:0 -o ignore \ 611501b7fbdSKristof Provost jexec ${j}c ping -c 1 192.0.2.1 612501b7fbdSKristof Provost atf_check -s exit:0 -o ignore \ 613501b7fbdSKristof Provost jexec ${j}srv ping -c 1 198.51.100.1 614501b7fbdSKristof Provost atf_check -s exit:0 -o ignore \ 615501b7fbdSKristof Provost jexec ${j}c ping -c 1 198.51.100.2 616501b7fbdSKristof Provost 617501b7fbdSKristof Provost # Establish state 618501b7fbdSKristof Provost # Note: use pft_ping so we always use the same ID, so pf considers all 619501b7fbdSKristof Provost # echo requests part of the same flow. 620501b7fbdSKristof Provost atf_check -s exit:0 -o ignore jexec ${j}c ${common_dir}/pft_ping.py \ 621501b7fbdSKristof Provost --sendif ${epair_c}a \ 622501b7fbdSKristof Provost --to 198.51.100.1 \ 623501b7fbdSKristof Provost --replyif ${epair_c}a 624501b7fbdSKristof Provost 625501b7fbdSKristof Provost # There's NAT here, so the source IP will be 198.51.100.1 626501b7fbdSKristof Provost if ! find_state ${j}r 198.51.100.1; 627501b7fbdSKristof Provost then 628501b7fbdSKristof Provost atf_fail "Expected state not found" 629501b7fbdSKristof Provost fi 630501b7fbdSKristof Provost 631501b7fbdSKristof Provost # By NAT-ed address? 632501b7fbdSKristof Provost jexec ${j}r pfctl -k nat -k 192.0.2.2 633501b7fbdSKristof Provost 634501b7fbdSKristof Provost if find_state ${j}r 198.51.100.1; 635501b7fbdSKristof Provost then 636501b7fbdSKristof Provost jexec ${j}r pfctl -ss -v 637501b7fbdSKristof Provost atf_fail "Failed to remove state" 638501b7fbdSKristof Provost fi 639501b7fbdSKristof Provost} 640501b7fbdSKristof Provost 641501b7fbdSKristof Provostnat_cleanup() 642501b7fbdSKristof Provost{ 643501b7fbdSKristof Provost pft_cleanup 644501b7fbdSKristof Provost} 645501b7fbdSKristof Provost 646065b5c7fSKristof Provostatf_init_test_cases() 647065b5c7fSKristof Provost{ 648065b5c7fSKristof Provost atf_add_test_case "v4" 6499af23174SKristof Provost atf_add_test_case "v6" 650065b5c7fSKristof Provost atf_add_test_case "label" 6515632f585SKristof Provost atf_add_test_case "multilabel" 652c2e11d81SKristof Provost atf_add_test_case "gateway" 653ac200a9cSKristof Provost atf_add_test_case "match" 6547bd7933fSKristof Provost atf_add_test_case "interface" 655bbf832f3SKristof Provost atf_add_test_case "id" 656501b7fbdSKristof Provost atf_add_test_case "nat" 657065b5c7fSKristof Provost} 658