1065b5c7fSKristof Provost# $FreeBSD$ 2065b5c7fSKristof Provost# 34d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 4065b5c7fSKristof Provost# 5065b5c7fSKristof Provost# Copyright (c) 2021 Rubicon Communications, LLC (Netgate) 6065b5c7fSKristof Provost# 7065b5c7fSKristof Provost# Redistribution and use in source and binary forms, with or without 8065b5c7fSKristof Provost# modification, are permitted provided that the following conditions 9065b5c7fSKristof Provost# are met: 10065b5c7fSKristof Provost# 1. Redistributions of source code must retain the above copyright 11065b5c7fSKristof Provost# notice, this list of conditions and the following disclaimer. 12065b5c7fSKristof Provost# 2. Redistributions in binary form must reproduce the above copyright 13065b5c7fSKristof Provost# notice, this list of conditions and the following disclaimer in the 14065b5c7fSKristof Provost# documentation and/or other materials provided with the distribution. 15065b5c7fSKristof Provost# 16065b5c7fSKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17065b5c7fSKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18065b5c7fSKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19065b5c7fSKristof Provost# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20065b5c7fSKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21065b5c7fSKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22065b5c7fSKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23065b5c7fSKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24065b5c7fSKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25065b5c7fSKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26065b5c7fSKristof Provost# SUCH DAMAGE. 27065b5c7fSKristof Provost 28065b5c7fSKristof Provost. $(atf_get_srcdir)/utils.subr 29065b5c7fSKristof Provost 30065b5c7fSKristof Provostcommon_dir=$(atf_get_srcdir)/../common 31065b5c7fSKristof Provost 3245258e1bSKristof Provostfind_state() 3345258e1bSKristof Provost{ 3445258e1bSKristof Provost jexec alcatraz pfctl -ss | grep icmp | grep 192.0.2.2 3545258e1bSKristof Provost} 3645258e1bSKristof Provost 3745258e1bSKristof Provostfind_state_v6() 3845258e1bSKristof Provost{ 3945258e1bSKristof Provost jexec alcatraz pfctl -ss | grep icmp | grep 2001:db8::2 4045258e1bSKristof Provost} 4145258e1bSKristof Provost 4245258e1bSKristof Provost 43065b5c7fSKristof Provostatf_test_case "v4" "cleanup" 44065b5c7fSKristof Provostv4_head() 45065b5c7fSKristof Provost{ 46065b5c7fSKristof Provost atf_set descr 'Test killing states by IPv4 address' 47065b5c7fSKristof Provost atf_set require.user root 48065b5c7fSKristof Provost atf_set require.progs scapy 49065b5c7fSKristof Provost} 50065b5c7fSKristof Provost 51065b5c7fSKristof Provostv4_body() 52065b5c7fSKristof Provost{ 53065b5c7fSKristof Provost pft_init 54065b5c7fSKristof Provost 55065b5c7fSKristof Provost epair=$(vnet_mkepair) 56065b5c7fSKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 57065b5c7fSKristof Provost 58065b5c7fSKristof Provost vnet_mkjail alcatraz ${epair}b 59065b5c7fSKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 60065b5c7fSKristof Provost jexec alcatraz pfctl -e 61065b5c7fSKristof Provost 62065b5c7fSKristof Provost pft_set_rules alcatraz "block all" \ 63*3a1f834bSDoug Rabson "pass in proto icmp" \ 64*3a1f834bSDoug Rabson "set skip on lo" 65065b5c7fSKristof Provost 66065b5c7fSKristof Provost # Sanity check & establish state 67065b5c7fSKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 68065b5c7fSKristof Provost --sendif ${epair}a \ 69065b5c7fSKristof Provost --to 192.0.2.2 \ 70065b5c7fSKristof Provost --replyif ${epair}a 71065b5c7fSKristof Provost 72065b5c7fSKristof Provost # Change rules to now deny the ICMP traffic 73065b5c7fSKristof Provost pft_set_rules noflush alcatraz "block all" 7445258e1bSKristof Provost if ! find_state; 7545258e1bSKristof Provost then 7645258e1bSKristof Provost atf_fail "Setting new rules removed the state." 7745258e1bSKristof Provost fi 78065b5c7fSKristof Provost 79065b5c7fSKristof Provost # Killing with the wrong IP doesn't affect our state 80065b5c7fSKristof Provost jexec alcatraz pfctl -k 192.0.2.3 8145258e1bSKristof Provost if ! find_state; 8245258e1bSKristof Provost then 8345258e1bSKristof Provost atf_fail "Killing with the wrong IP removed our state." 8445258e1bSKristof Provost fi 85065b5c7fSKristof Provost 86065b5c7fSKristof Provost # Killing with one correct address and one incorrect doesn't kill the state 87065b5c7fSKristof Provost jexec alcatraz pfctl -k 192.0.2.1 -k 192.0.2.3 8845258e1bSKristof Provost if ! find_state; 8945258e1bSKristof Provost then 9045258e1bSKristof Provost atf_fail "Killing with one wrong IP removed our state." 9145258e1bSKristof Provost fi 92065b5c7fSKristof Provost 93065b5c7fSKristof Provost # Killing with correct address does remove the state 94065b5c7fSKristof Provost jexec alcatraz pfctl -k 192.0.2.1 9545258e1bSKristof Provost if find_state; 9645258e1bSKristof Provost then 9745258e1bSKristof Provost atf_fail "Killing with the correct IP did not remove our state." 9845258e1bSKristof Provost fi 99065b5c7fSKristof Provost} 100065b5c7fSKristof Provost 101065b5c7fSKristof Provostv4_cleanup() 102065b5c7fSKristof Provost{ 103065b5c7fSKristof Provost pft_cleanup 104065b5c7fSKristof Provost} 105065b5c7fSKristof Provost 1069af23174SKristof Provostatf_test_case "v6" "cleanup" 1079af23174SKristof Provostv6_head() 1089af23174SKristof Provost{ 1099af23174SKristof Provost atf_set descr 'Test killing states by IPv6 address' 1109af23174SKristof Provost atf_set require.user root 1119af23174SKristof Provost atf_set require.progs scapy 1129af23174SKristof Provost} 1139af23174SKristof Provost 1149af23174SKristof Provostv6_body() 1159af23174SKristof Provost{ 1169af23174SKristof Provost pft_init 1179af23174SKristof Provost 118300f4be4SWarner Losh if [ "$(atf_config_get ci false)" = "true" ]; then 119300f4be4SWarner Losh atf_skip "https://bugs.freebsd.org/260458" 120300f4be4SWarner Losh fi 121300f4be4SWarner Losh 1229af23174SKristof Provost epair=$(vnet_mkepair) 1239af23174SKristof Provost ifconfig ${epair}a inet6 2001:db8::1/64 up no_dad 1249af23174SKristof Provost 1259af23174SKristof Provost vnet_mkjail alcatraz ${epair}b 1269af23174SKristof Provost jexec alcatraz ifconfig ${epair}b inet6 2001:db8::2/64 up no_dad 1279af23174SKristof Provost jexec alcatraz pfctl -e 1289af23174SKristof Provost 1299af23174SKristof Provost pft_set_rules alcatraz "block all" \ 130*3a1f834bSDoug Rabson "pass in proto icmp6" \ 131*3a1f834bSDoug Rabson "set skip on lo" 1329af23174SKristof Provost 1339af23174SKristof Provost # Sanity check & establish state 1349af23174SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 1359af23174SKristof Provost --sendif ${epair}a \ 1369af23174SKristof Provost --to 2001:db8::2 \ 1379af23174SKristof Provost --replyif ${epair}a 1389af23174SKristof Provost 1399af23174SKristof Provost # Change rules to now deny the ICMP traffic 1409af23174SKristof Provost pft_set_rules noflush alcatraz "block all" 14145258e1bSKristof Provost if ! find_state_v6; 14245258e1bSKristof Provost then 14345258e1bSKristof Provost atf_fail "Setting new rules removed the state." 14445258e1bSKristof Provost fi 1459af23174SKristof Provost 1469af23174SKristof Provost # Killing with the wrong IP doesn't affect our state 1479af23174SKristof Provost jexec alcatraz pfctl -k 2001:db8::3 14845258e1bSKristof Provost if ! find_state_v6; 14945258e1bSKristof Provost then 15045258e1bSKristof Provost atf_fail "Killing with the wrong IP removed our state." 15145258e1bSKristof Provost fi 1529af23174SKristof Provost 1539af23174SKristof Provost # Killing with one correct address and one incorrect doesn't kill the state 1549af23174SKristof Provost jexec alcatraz pfctl -k 2001:db8::1 -k 2001:db8::3 15545258e1bSKristof Provost if ! find_state_v6; 15645258e1bSKristof Provost then 15745258e1bSKristof Provost atf_fail "Killing with one wrong IP removed our state." 15845258e1bSKristof Provost fi 1599af23174SKristof Provost 1609af23174SKristof Provost # Killing with correct address does remove the state 1619af23174SKristof Provost jexec alcatraz pfctl -k 2001:db8::1 16245258e1bSKristof Provost if find_state_v6; 16345258e1bSKristof Provost then 16445258e1bSKristof Provost atf_fail "Killing with the correct IP did not remove our state." 16545258e1bSKristof Provost fi 1669af23174SKristof Provost} 1679af23174SKristof Provost 1689af23174SKristof Provostv6_cleanup() 1699af23174SKristof Provost{ 1709af23174SKristof Provost pft_cleanup 1719af23174SKristof Provost} 1729af23174SKristof Provost 173065b5c7fSKristof Provostatf_test_case "label" "cleanup" 174065b5c7fSKristof Provostlabel_head() 175065b5c7fSKristof Provost{ 176065b5c7fSKristof Provost atf_set descr 'Test killing states by label' 177065b5c7fSKristof Provost atf_set require.user root 178065b5c7fSKristof Provost atf_set require.progs scapy 179065b5c7fSKristof Provost} 180065b5c7fSKristof Provost 181065b5c7fSKristof Provostlabel_body() 182065b5c7fSKristof Provost{ 183065b5c7fSKristof Provost pft_init 184065b5c7fSKristof Provost 185065b5c7fSKristof Provost epair=$(vnet_mkepair) 186065b5c7fSKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 187065b5c7fSKristof Provost 188065b5c7fSKristof Provost vnet_mkjail alcatraz ${epair}b 189065b5c7fSKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 190065b5c7fSKristof Provost jexec alcatraz pfctl -e 191065b5c7fSKristof Provost 192065b5c7fSKristof Provost pft_set_rules alcatraz "block all" \ 193065b5c7fSKristof Provost "pass in proto tcp label bar" \ 194*3a1f834bSDoug Rabson "pass in proto icmp label foo" \ 195*3a1f834bSDoug Rabson "set skip on lo" 196065b5c7fSKristof Provost 197065b5c7fSKristof Provost # Sanity check & establish state 198065b5c7fSKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 199065b5c7fSKristof Provost --sendif ${epair}a \ 200065b5c7fSKristof Provost --to 192.0.2.2 \ 201065b5c7fSKristof Provost --replyif ${epair}a 202065b5c7fSKristof Provost 203065b5c7fSKristof Provost # Change rules to now deny the ICMP traffic 204065b5c7fSKristof Provost pft_set_rules noflush alcatraz "block all" 20545258e1bSKristof Provost if ! find_state; 20645258e1bSKristof Provost then 20745258e1bSKristof Provost atf_fail "Setting new rules removed the state." 20845258e1bSKristof Provost fi 209065b5c7fSKristof Provost 210065b5c7fSKristof Provost # Killing a label on a different rules keeps the state 211065b5c7fSKristof Provost jexec alcatraz pfctl -k label -k bar 21245258e1bSKristof Provost if ! find_state; 21345258e1bSKristof Provost then 21445258e1bSKristof Provost atf_fail "Killing a different label removed the state." 21545258e1bSKristof Provost fi 216065b5c7fSKristof Provost 217065b5c7fSKristof Provost # Killing a non-existing label keeps the state 218065b5c7fSKristof Provost jexec alcatraz pfctl -k label -k baz 21945258e1bSKristof Provost if ! find_state; 22045258e1bSKristof Provost then 22145258e1bSKristof Provost atf_fail "Killing a non-existing label removed the state." 22245258e1bSKristof Provost fi 223065b5c7fSKristof Provost 224065b5c7fSKristof Provost # Killing the correct label kills the state 225065b5c7fSKristof Provost jexec alcatraz pfctl -k label -k foo 22645258e1bSKristof Provost if find_state; 22745258e1bSKristof Provost then 22845258e1bSKristof Provost atf_fail "Killing the state did not remove it." 22945258e1bSKristof Provost fi 230065b5c7fSKristof Provost} 231065b5c7fSKristof Provost 232065b5c7fSKristof Provostlabel_cleanup() 233065b5c7fSKristof Provost{ 234065b5c7fSKristof Provost pft_cleanup 235065b5c7fSKristof Provost} 236065b5c7fSKristof Provost 2375632f585SKristof Provostatf_test_case "multilabel" "cleanup" 2385632f585SKristof Provostmultilabel_head() 2395632f585SKristof Provost{ 2405632f585SKristof Provost atf_set descr 'Test killing states with multiple labels by label' 2415632f585SKristof Provost atf_set require.user root 2425632f585SKristof Provost atf_set require.progs scapy 2435632f585SKristof Provost} 2445632f585SKristof Provost 2455632f585SKristof Provostmultilabel_body() 2465632f585SKristof Provost{ 2475632f585SKristof Provost pft_init 2485632f585SKristof Provost 2495632f585SKristof Provost epair=$(vnet_mkepair) 2505632f585SKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 2515632f585SKristof Provost 2525632f585SKristof Provost vnet_mkjail alcatraz ${epair}b 2535632f585SKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 2545632f585SKristof Provost jexec alcatraz pfctl -e 2555632f585SKristof Provost 2565632f585SKristof Provost pft_set_rules alcatraz "block all" \ 257*3a1f834bSDoug Rabson "pass in proto icmp label foo label bar" \ 258*3a1f834bSDoug Rabson "set skip on lo" 2595632f585SKristof Provost 2605632f585SKristof Provost # Sanity check & establish state 2615632f585SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 2625632f585SKristof Provost --sendif ${epair}a \ 2635632f585SKristof Provost --to 192.0.2.2 \ 2645632f585SKristof Provost --replyif ${epair}a 2655632f585SKristof Provost 2665632f585SKristof Provost # Change rules to now deny the ICMP traffic 2675632f585SKristof Provost pft_set_rules noflush alcatraz "block all" 26845258e1bSKristof Provost if ! find_state; 26945258e1bSKristof Provost then 27045258e1bSKristof Provost atf_fail "Setting new rules removed the state." 27145258e1bSKristof Provost fi 2725632f585SKristof Provost 2735632f585SKristof Provost # Killing a label on a different rules keeps the state 2745632f585SKristof Provost jexec alcatraz pfctl -k label -k baz 27545258e1bSKristof Provost if ! find_state; 27645258e1bSKristof Provost then 27745258e1bSKristof Provost atf_fail "Killing a different label removed the state." 27845258e1bSKristof Provost fi 2795632f585SKristof Provost 2805632f585SKristof Provost # Killing the state with the last label works 2815632f585SKristof Provost jexec alcatraz pfctl -k label -k bar 28245258e1bSKristof Provost if find_state; 28345258e1bSKristof Provost then 28445258e1bSKristof Provost atf_fail "Killing with the last label did not remove the state." 28545258e1bSKristof Provost fi 2865632f585SKristof Provost 2875632f585SKristof Provost pft_set_rules alcatraz "block all" \ 288*3a1f834bSDoug Rabson "pass in proto icmp label foo label bar" \ 289*3a1f834bSDoug Rabson "set skip on lo" 2905632f585SKristof Provost 2915632f585SKristof Provost # Reestablish state 2925632f585SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 2935632f585SKristof Provost --sendif ${epair}a \ 2945632f585SKristof Provost --to 192.0.2.2 \ 2955632f585SKristof Provost --replyif ${epair}a 2965632f585SKristof Provost 2975632f585SKristof Provost # Change rules to now deny the ICMP traffic 2985632f585SKristof Provost pft_set_rules noflush alcatraz "block all" 29945258e1bSKristof Provost if ! find_state; 30045258e1bSKristof Provost then 30145258e1bSKristof Provost atf_fail "Setting new rules removed the state." 30245258e1bSKristof Provost fi 3035632f585SKristof Provost 3045632f585SKristof Provost # Killing with the first label works too 3055632f585SKristof Provost jexec alcatraz pfctl -k label -k foo 30645258e1bSKristof Provost if find_state; 30745258e1bSKristof Provost then 30845258e1bSKristof Provost atf_fail "Killing with the first label did not remove the state." 30945258e1bSKristof Provost fi 3105632f585SKristof Provost} 3115632f585SKristof Provost 3125632f585SKristof Provostmultilabel_cleanup() 3135632f585SKristof Provost{ 3145632f585SKristof Provost pft_cleanup 3155632f585SKristof Provost} 3165632f585SKristof Provost 317c2e11d81SKristof Provostatf_test_case "gateway" "cleanup" 318c2e11d81SKristof Provostgateway_head() 319c2e11d81SKristof Provost{ 320c2e11d81SKristof Provost atf_set descr 'Test killing states by route-to/reply-to address' 321c2e11d81SKristof Provost atf_set require.user root 322c2e11d81SKristof Provost atf_set require.progs scapy 323c2e11d81SKristof Provost} 324c2e11d81SKristof Provost 325c2e11d81SKristof Provostgateway_body() 326c2e11d81SKristof Provost{ 327c2e11d81SKristof Provost pft_init 328c2e11d81SKristof Provost 329c2e11d81SKristof Provost epair=$(vnet_mkepair) 330c2e11d81SKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 331c2e11d81SKristof Provost 332c2e11d81SKristof Provost vnet_mkjail alcatraz ${epair}b 333c2e11d81SKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 334c2e11d81SKristof Provost jexec alcatraz pfctl -e 335c2e11d81SKristof Provost 336c2e11d81SKristof Provost pft_set_rules alcatraz "block all" \ 337*3a1f834bSDoug Rabson "pass in reply-to (${epair}b 192.0.2.1) proto icmp" \ 338*3a1f834bSDoug Rabson "set skip on lo" 339c2e11d81SKristof Provost 340c2e11d81SKristof Provost # Sanity check & establish state 341c2e11d81SKristof Provost # Note: use pft_ping so we always use the same ID, so pf considers all 342c2e11d81SKristof Provost # echo requests part of the same flow. 343c2e11d81SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 344c2e11d81SKristof Provost --sendif ${epair}a \ 345c2e11d81SKristof Provost --to 192.0.2.2 \ 346c2e11d81SKristof Provost --replyif ${epair}a 347c2e11d81SKristof Provost 348c2e11d81SKristof Provost # Change rules to now deny the ICMP traffic 349c2e11d81SKristof Provost pft_set_rules noflush alcatraz "block all" 35045258e1bSKristof Provost if ! find_state; 35145258e1bSKristof Provost then 35245258e1bSKristof Provost atf_fail "Setting new rules removed the state." 35345258e1bSKristof Provost fi 354c2e11d81SKristof Provost 355c2e11d81SKristof Provost # Killing with a different gateway does not affect our state 356c2e11d81SKristof Provost jexec alcatraz pfctl -k gateway -k 192.0.2.2 35745258e1bSKristof Provost if ! find_state; 35845258e1bSKristof Provost then 35945258e1bSKristof Provost atf_fail "Killing with a different gateway removed the state." 36045258e1bSKristof Provost fi 361c2e11d81SKristof Provost 362c2e11d81SKristof Provost # Killing states with the relevant gateway does terminate our state 363c2e11d81SKristof Provost jexec alcatraz pfctl -k gateway -k 192.0.2.1 36445258e1bSKristof Provost if find_state; 36545258e1bSKristof Provost then 36645258e1bSKristof Provost atf_fail "Killing with the gateway did not remove the state." 36745258e1bSKristof Provost fi 368c2e11d81SKristof Provost} 369c2e11d81SKristof Provost 370c2e11d81SKristof Provostgateway_cleanup() 371c2e11d81SKristof Provost{ 372c2e11d81SKristof Provost pft_cleanup 373c2e11d81SKristof Provost} 374c2e11d81SKristof Provost 375ac200a9cSKristof Provostatf_test_case "match" "cleanup" 376ac200a9cSKristof Provostmatch_head() 377ac200a9cSKristof Provost{ 378ac200a9cSKristof Provost atf_set descr 'Test killing matching states' 379ac200a9cSKristof Provost atf_set require.user root 380ac200a9cSKristof Provost} 381ac200a9cSKristof Provost 38270dd30d4SKristof Provostwait_for_state() 38370dd30d4SKristof Provost{ 38470dd30d4SKristof Provost jail=$1 38570dd30d4SKristof Provost addr=$2 38670dd30d4SKristof Provost 38770dd30d4SKristof Provost while ! jexec $jail pfctl -s s | grep $addr >/dev/null; 38870dd30d4SKristof Provost do 38970dd30d4SKristof Provost sleep .1 39070dd30d4SKristof Provost done 39170dd30d4SKristof Provost} 39270dd30d4SKristof Provost 393ac200a9cSKristof Provostmatch_body() 394ac200a9cSKristof Provost{ 395ac200a9cSKristof Provost pft_init 396ac200a9cSKristof Provost 397ac200a9cSKristof Provost epair_one=$(vnet_mkepair) 398ac200a9cSKristof Provost ifconfig ${epair_one}a 192.0.2.1/24 up 399ac200a9cSKristof Provost 400ac200a9cSKristof Provost epair_two=$(vnet_mkepair) 401ac200a9cSKristof Provost 402ac200a9cSKristof Provost vnet_mkjail alcatraz ${epair_one}b ${epair_two}a 403ac200a9cSKristof Provost jexec alcatraz ifconfig ${epair_one}b 192.0.2.2/24 up 404ac200a9cSKristof Provost jexec alcatraz ifconfig ${epair_two}a 198.51.100.1/24 up 405ac200a9cSKristof Provost jexec alcatraz sysctl net.inet.ip.forwarding=1 406ac200a9cSKristof Provost jexec alcatraz pfctl -e 407ac200a9cSKristof Provost 408ac200a9cSKristof Provost vnet_mkjail singsing ${epair_two}b 409ac200a9cSKristof Provost jexec singsing ifconfig ${epair_two}b 198.51.100.2/24 up 410ac200a9cSKristof Provost jexec singsing route add default 198.51.100.1 411ac200a9cSKristof Provost jexec singsing /usr/sbin/inetd -p inetd-echo.pid \ 412ac200a9cSKristof Provost $(atf_get_srcdir)/echo_inetd.conf 413ac200a9cSKristof Provost 414ac200a9cSKristof Provost route add 198.51.100.0/24 192.0.2.2 415ac200a9cSKristof Provost 416ac200a9cSKristof Provost pft_set_rules alcatraz \ 417ac200a9cSKristof Provost "nat on ${epair_two}a from 192.0.2.0/24 -> (${epair_two}a)" \ 418ac200a9cSKristof Provost "pass all" 419ac200a9cSKristof Provost 420ac200a9cSKristof Provost nc 198.51.100.2 7 & 42170dd30d4SKristof Provost wait_for_state alcatraz 192.0.2.1 422ac200a9cSKristof Provost 423ac200a9cSKristof Provost # Expect two states 4244e860bd5SKristof Provost states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l) 425ac200a9cSKristof Provost if [ $states -ne 2 ] ; 426ac200a9cSKristof Provost then 427ac200a9cSKristof Provost atf_fail "Expected two states, found $states" 428ac200a9cSKristof Provost fi 429ac200a9cSKristof Provost 430ac200a9cSKristof Provost # If we don't kill the matching NAT state one should be left 431ac200a9cSKristof Provost jexec alcatraz pfctl -k 192.0.2.1 4324e860bd5SKristof Provost states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l) 433ac200a9cSKristof Provost if [ $states -ne 1 ] ; 434ac200a9cSKristof Provost then 435ac200a9cSKristof Provost atf_fail "Expected one states, found $states" 436ac200a9cSKristof Provost fi 437ac200a9cSKristof Provost 438ac200a9cSKristof Provost # Flush 439ac200a9cSKristof Provost jexec alcatraz pfctl -F states 440ac200a9cSKristof Provost 441ac200a9cSKristof Provost nc 198.51.100.2 7 & 44270dd30d4SKristof Provost wait_for_state alcatraz 192.0.2.1 443ac200a9cSKristof Provost 444ac200a9cSKristof Provost # Kill matching states, expect all of them to be gone 445ac200a9cSKristof Provost jexec alcatraz pfctl -M -k 192.0.2.1 4464e860bd5SKristof Provost states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l) 447ac200a9cSKristof Provost if [ $states -ne 0 ] ; 448ac200a9cSKristof Provost then 449ac200a9cSKristof Provost atf_fail "Expected zero states, found $states" 450ac200a9cSKristof Provost fi 451ac200a9cSKristof Provost} 452ac200a9cSKristof Provost 453ac200a9cSKristof Provostmatch_cleanup() 454ac200a9cSKristof Provost{ 455ac200a9cSKristof Provost pft_cleanup 456ac200a9cSKristof Provost} 457ac200a9cSKristof Provost 4587bd7933fSKristof Provostatf_test_case "interface" "cleanup" 4597bd7933fSKristof Provostinterface_head() 4607bd7933fSKristof Provost{ 4617bd7933fSKristof Provost atf_set descr 'Test killing states based on interface' 4627bd7933fSKristof Provost atf_set require.user root 4637bd7933fSKristof Provost atf_set require.progs scapy 4647bd7933fSKristof Provost} 4657bd7933fSKristof Provost 4667bd7933fSKristof Provostinterface_body() 4677bd7933fSKristof Provost{ 4687bd7933fSKristof Provost pft_init 4697bd7933fSKristof Provost 4707bd7933fSKristof Provost epair=$(vnet_mkepair) 4717bd7933fSKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 4727bd7933fSKristof Provost 4737bd7933fSKristof Provost vnet_mkjail alcatraz ${epair}b 4747bd7933fSKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 4757bd7933fSKristof Provost jexec alcatraz pfctl -e 4767bd7933fSKristof Provost 4777bd7933fSKristof Provost pft_set_rules alcatraz "block all" \ 478*3a1f834bSDoug Rabson "pass in proto icmp" \ 479*3a1f834bSDoug Rabson "set skip on lo" 4807bd7933fSKristof Provost 4817bd7933fSKristof Provost # Sanity check & establish state 4827bd7933fSKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 4837bd7933fSKristof Provost --sendif ${epair}a \ 4847bd7933fSKristof Provost --to 192.0.2.2 \ 4857bd7933fSKristof Provost --replyif ${epair}a 4867bd7933fSKristof Provost 4877bd7933fSKristof Provost # Change rules to now deny the ICMP traffic 4887bd7933fSKristof Provost pft_set_rules noflush alcatraz "block all" 48945258e1bSKristof Provost if ! find_state; 49045258e1bSKristof Provost then 49145258e1bSKristof Provost atf_fail "Setting new rules removed the state." 49245258e1bSKristof Provost fi 4937bd7933fSKristof Provost 4947bd7933fSKristof Provost # Flushing states on a different interface doesn't affect our state 4957bd7933fSKristof Provost jexec alcatraz pfctl -i ${epair}a -Fs 49645258e1bSKristof Provost if ! find_state; 49745258e1bSKristof Provost then 49845258e1bSKristof Provost atf_fail "Flushing on a different interface removed the state." 49945258e1bSKristof Provost fi 5007bd7933fSKristof Provost 5017bd7933fSKristof Provost # Flushing on the correct interface does (even with floating states) 5027bd7933fSKristof Provost jexec alcatraz pfctl -i ${epair}b -Fs 50345258e1bSKristof Provost if find_state; 50445258e1bSKristof Provost then 50545258e1bSKristof Provost atf_fail "Flushing on a the interface did not remove the state." 50645258e1bSKristof Provost fi 5077bd7933fSKristof Provost} 5087bd7933fSKristof Provost 5097bd7933fSKristof Provostinterface_cleanup() 5107bd7933fSKristof Provost{ 5117bd7933fSKristof Provost pft_cleanup 5127bd7933fSKristof Provost} 5137bd7933fSKristof Provost 514bbf832f3SKristof Provostatf_test_case "id" "cleanup" 515bbf832f3SKristof Provostid_head() 516bbf832f3SKristof Provost{ 517bbf832f3SKristof Provost atf_set descr 'Test killing states by id' 518bbf832f3SKristof Provost atf_set require.user root 519bbf832f3SKristof Provost atf_set require.progs scapy 520bbf832f3SKristof Provost} 521bbf832f3SKristof Provost 522bbf832f3SKristof Provostid_body() 523bbf832f3SKristof Provost{ 524bbf832f3SKristof Provost pft_init 525bbf832f3SKristof Provost 526bbf832f3SKristof Provost epair=$(vnet_mkepair) 527bbf832f3SKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 528bbf832f3SKristof Provost 529bbf832f3SKristof Provost vnet_mkjail alcatraz ${epair}b 530bbf832f3SKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 531bbf832f3SKristof Provost jexec alcatraz pfctl -e 532bbf832f3SKristof Provost 533bbf832f3SKristof Provost pft_set_rules alcatraz "block all" \ 534bbf832f3SKristof Provost "pass in proto tcp" \ 535*3a1f834bSDoug Rabson "pass in proto icmp" \ 536*3a1f834bSDoug Rabson "set skip on lo" 537bbf832f3SKristof Provost 538bbf832f3SKristof Provost # Sanity check & establish state 539bbf832f3SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 540bbf832f3SKristof Provost --sendif ${epair}a \ 541bbf832f3SKristof Provost --to 192.0.2.2 \ 542bbf832f3SKristof Provost --replyif ${epair}a 543bbf832f3SKristof Provost 544bbf832f3SKristof Provost # Change rules to now deny the ICMP traffic 545bbf832f3SKristof Provost pft_set_rules noflush alcatraz "block all" 54645258e1bSKristof Provost if ! find_state; 54745258e1bSKristof Provost then 54845258e1bSKristof Provost atf_fail "Setting new rules removed the state." 54945258e1bSKristof Provost fi 550bbf832f3SKristof Provost 551bbf832f3SKristof Provost # Get the state ID 552bbf832f3SKristof Provost id=$(jexec alcatraz pfctl -ss -vvv | grep -A 3 icmp | 553bbf832f3SKristof Provost grep -A 3 192.0.2.2 | awk '/id:/ { printf("%s/%s", $2, $4); }') 554bbf832f3SKristof Provost 555bbf832f3SKristof Provost # Kill the wrong ID 556bbf832f3SKristof Provost jexec alcatraz pfctl -k id -k 1 55745258e1bSKristof Provost if ! find_state; 55845258e1bSKristof Provost then 55945258e1bSKristof Provost atf_fail "Killing a different ID removed the state." 56045258e1bSKristof Provost fi 561bbf832f3SKristof Provost 562bbf832f3SKristof Provost # Kill the correct ID 563bbf832f3SKristof Provost jexec alcatraz pfctl -k id -k ${id} 56445258e1bSKristof Provost if find_state; 56545258e1bSKristof Provost then 56645258e1bSKristof Provost atf_fail "Killing the state did not remove it." 56745258e1bSKristof Provost fi 568bbf832f3SKristof Provost} 569bbf832f3SKristof Provost 570bbf832f3SKristof Provostid_cleanup() 571bbf832f3SKristof Provost{ 572bbf832f3SKristof Provost pft_cleanup 573bbf832f3SKristof Provost} 574bbf832f3SKristof Provost 575065b5c7fSKristof Provostatf_init_test_cases() 576065b5c7fSKristof Provost{ 577065b5c7fSKristof Provost atf_add_test_case "v4" 5789af23174SKristof Provost atf_add_test_case "v6" 579065b5c7fSKristof Provost atf_add_test_case "label" 5805632f585SKristof Provost atf_add_test_case "multilabel" 581c2e11d81SKristof Provost atf_add_test_case "gateway" 582ac200a9cSKristof Provost atf_add_test_case "match" 5837bd7933fSKristof Provost atf_add_test_case "interface" 584bbf832f3SKristof Provost atf_add_test_case "id" 585065b5c7fSKristof Provost} 586