1065b5c7fSKristof Provost# $FreeBSD$ 2065b5c7fSKristof Provost# 3*4d846d26SWarner 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" \ 63065b5c7fSKristof Provost "pass in proto icmp" 64065b5c7fSKristof Provost 65065b5c7fSKristof Provost # Sanity check & establish state 66065b5c7fSKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 67065b5c7fSKristof Provost --sendif ${epair}a \ 68065b5c7fSKristof Provost --to 192.0.2.2 \ 69065b5c7fSKristof Provost --replyif ${epair}a 70065b5c7fSKristof Provost 71065b5c7fSKristof Provost # Change rules to now deny the ICMP traffic 72065b5c7fSKristof Provost pft_set_rules noflush alcatraz "block all" 7345258e1bSKristof Provost if ! find_state; 7445258e1bSKristof Provost then 7545258e1bSKristof Provost atf_fail "Setting new rules removed the state." 7645258e1bSKristof Provost fi 77065b5c7fSKristof Provost 78065b5c7fSKristof Provost # Killing with the wrong IP doesn't affect our state 79065b5c7fSKristof Provost jexec alcatraz pfctl -k 192.0.2.3 8045258e1bSKristof Provost if ! find_state; 8145258e1bSKristof Provost then 8245258e1bSKristof Provost atf_fail "Killing with the wrong IP removed our state." 8345258e1bSKristof Provost fi 84065b5c7fSKristof Provost 85065b5c7fSKristof Provost # Killing with one correct address and one incorrect doesn't kill the state 86065b5c7fSKristof Provost jexec alcatraz pfctl -k 192.0.2.1 -k 192.0.2.3 8745258e1bSKristof Provost if ! find_state; 8845258e1bSKristof Provost then 8945258e1bSKristof Provost atf_fail "Killing with one wrong IP removed our state." 9045258e1bSKristof Provost fi 91065b5c7fSKristof Provost 92065b5c7fSKristof Provost # Killing with correct address does remove the state 93065b5c7fSKristof Provost jexec alcatraz pfctl -k 192.0.2.1 9445258e1bSKristof Provost if find_state; 9545258e1bSKristof Provost then 9645258e1bSKristof Provost atf_fail "Killing with the correct IP did not remove our state." 9745258e1bSKristof Provost fi 98065b5c7fSKristof Provost} 99065b5c7fSKristof Provost 100065b5c7fSKristof Provostv4_cleanup() 101065b5c7fSKristof Provost{ 102065b5c7fSKristof Provost pft_cleanup 103065b5c7fSKristof Provost} 104065b5c7fSKristof Provost 1059af23174SKristof Provostatf_test_case "v6" "cleanup" 1069af23174SKristof Provostv6_head() 1079af23174SKristof Provost{ 1089af23174SKristof Provost atf_set descr 'Test killing states by IPv6 address' 1099af23174SKristof Provost atf_set require.user root 1109af23174SKristof Provost atf_set require.progs scapy 1119af23174SKristof Provost} 1129af23174SKristof Provost 1139af23174SKristof Provostv6_body() 1149af23174SKristof Provost{ 1159af23174SKristof Provost pft_init 1169af23174SKristof Provost 117300f4be4SWarner Losh if [ "$(atf_config_get ci false)" = "true" ]; then 118300f4be4SWarner Losh atf_skip "https://bugs.freebsd.org/260458" 119300f4be4SWarner Losh fi 120300f4be4SWarner Losh 1219af23174SKristof Provost epair=$(vnet_mkepair) 1229af23174SKristof Provost ifconfig ${epair}a inet6 2001:db8::1/64 up no_dad 1239af23174SKristof Provost 1249af23174SKristof Provost vnet_mkjail alcatraz ${epair}b 1259af23174SKristof Provost jexec alcatraz ifconfig ${epair}b inet6 2001:db8::2/64 up no_dad 1269af23174SKristof Provost jexec alcatraz pfctl -e 1279af23174SKristof Provost 1289af23174SKristof Provost pft_set_rules alcatraz "block all" \ 1299af23174SKristof Provost "pass in proto icmp6" 1309af23174SKristof Provost 1319af23174SKristof Provost # Sanity check & establish state 1329af23174SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 1339af23174SKristof Provost --sendif ${epair}a \ 1349af23174SKristof Provost --to 2001:db8::2 \ 1359af23174SKristof Provost --replyif ${epair}a 1369af23174SKristof Provost 1379af23174SKristof Provost # Change rules to now deny the ICMP traffic 1389af23174SKristof Provost pft_set_rules noflush alcatraz "block all" 13945258e1bSKristof Provost if ! find_state_v6; 14045258e1bSKristof Provost then 14145258e1bSKristof Provost atf_fail "Setting new rules removed the state." 14245258e1bSKristof Provost fi 1439af23174SKristof Provost 1449af23174SKristof Provost # Killing with the wrong IP doesn't affect our state 1459af23174SKristof Provost jexec alcatraz pfctl -k 2001:db8::3 14645258e1bSKristof Provost if ! find_state_v6; 14745258e1bSKristof Provost then 14845258e1bSKristof Provost atf_fail "Killing with the wrong IP removed our state." 14945258e1bSKristof Provost fi 1509af23174SKristof Provost 1519af23174SKristof Provost # Killing with one correct address and one incorrect doesn't kill the state 1529af23174SKristof Provost jexec alcatraz pfctl -k 2001:db8::1 -k 2001:db8::3 15345258e1bSKristof Provost if ! find_state_v6; 15445258e1bSKristof Provost then 15545258e1bSKristof Provost atf_fail "Killing with one wrong IP removed our state." 15645258e1bSKristof Provost fi 1579af23174SKristof Provost 1589af23174SKristof Provost # Killing with correct address does remove the state 1599af23174SKristof Provost jexec alcatraz pfctl -k 2001:db8::1 16045258e1bSKristof Provost if find_state_v6; 16145258e1bSKristof Provost then 16245258e1bSKristof Provost atf_fail "Killing with the correct IP did not remove our state." 16345258e1bSKristof Provost fi 1649af23174SKristof Provost} 1659af23174SKristof Provost 1669af23174SKristof Provostv6_cleanup() 1679af23174SKristof Provost{ 1689af23174SKristof Provost pft_cleanup 1699af23174SKristof Provost} 1709af23174SKristof Provost 171065b5c7fSKristof Provostatf_test_case "label" "cleanup" 172065b5c7fSKristof Provostlabel_head() 173065b5c7fSKristof Provost{ 174065b5c7fSKristof Provost atf_set descr 'Test killing states by label' 175065b5c7fSKristof Provost atf_set require.user root 176065b5c7fSKristof Provost atf_set require.progs scapy 177065b5c7fSKristof Provost} 178065b5c7fSKristof Provost 179065b5c7fSKristof Provostlabel_body() 180065b5c7fSKristof Provost{ 181065b5c7fSKristof Provost pft_init 182065b5c7fSKristof Provost 183065b5c7fSKristof Provost epair=$(vnet_mkepair) 184065b5c7fSKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 185065b5c7fSKristof Provost 186065b5c7fSKristof Provost vnet_mkjail alcatraz ${epair}b 187065b5c7fSKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 188065b5c7fSKristof Provost jexec alcatraz pfctl -e 189065b5c7fSKristof Provost 190065b5c7fSKristof Provost pft_set_rules alcatraz "block all" \ 191065b5c7fSKristof Provost "pass in proto tcp label bar" \ 192065b5c7fSKristof Provost "pass in proto icmp label foo" 193065b5c7fSKristof Provost 194065b5c7fSKristof Provost # Sanity check & establish state 195065b5c7fSKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 196065b5c7fSKristof Provost --sendif ${epair}a \ 197065b5c7fSKristof Provost --to 192.0.2.2 \ 198065b5c7fSKristof Provost --replyif ${epair}a 199065b5c7fSKristof Provost 200065b5c7fSKristof Provost # Change rules to now deny the ICMP traffic 201065b5c7fSKristof Provost pft_set_rules noflush alcatraz "block all" 20245258e1bSKristof Provost if ! find_state; 20345258e1bSKristof Provost then 20445258e1bSKristof Provost atf_fail "Setting new rules removed the state." 20545258e1bSKristof Provost fi 206065b5c7fSKristof Provost 207065b5c7fSKristof Provost # Killing a label on a different rules keeps the state 208065b5c7fSKristof Provost jexec alcatraz pfctl -k label -k bar 20945258e1bSKristof Provost if ! find_state; 21045258e1bSKristof Provost then 21145258e1bSKristof Provost atf_fail "Killing a different label removed the state." 21245258e1bSKristof Provost fi 213065b5c7fSKristof Provost 214065b5c7fSKristof Provost # Killing a non-existing label keeps the state 215065b5c7fSKristof Provost jexec alcatraz pfctl -k label -k baz 21645258e1bSKristof Provost if ! find_state; 21745258e1bSKristof Provost then 21845258e1bSKristof Provost atf_fail "Killing a non-existing label removed the state." 21945258e1bSKristof Provost fi 220065b5c7fSKristof Provost 221065b5c7fSKristof Provost # Killing the correct label kills the state 222065b5c7fSKristof Provost jexec alcatraz pfctl -k label -k foo 22345258e1bSKristof Provost if find_state; 22445258e1bSKristof Provost then 22545258e1bSKristof Provost atf_fail "Killing the state did not remove it." 22645258e1bSKristof Provost fi 227065b5c7fSKristof Provost} 228065b5c7fSKristof Provost 229065b5c7fSKristof Provostlabel_cleanup() 230065b5c7fSKristof Provost{ 231065b5c7fSKristof Provost pft_cleanup 232065b5c7fSKristof Provost} 233065b5c7fSKristof Provost 2345632f585SKristof Provostatf_test_case "multilabel" "cleanup" 2355632f585SKristof Provostmultilabel_head() 2365632f585SKristof Provost{ 2375632f585SKristof Provost atf_set descr 'Test killing states with multiple labels by label' 2385632f585SKristof Provost atf_set require.user root 2395632f585SKristof Provost atf_set require.progs scapy 2405632f585SKristof Provost} 2415632f585SKristof Provost 2425632f585SKristof Provostmultilabel_body() 2435632f585SKristof Provost{ 2445632f585SKristof Provost pft_init 2455632f585SKristof Provost 2465632f585SKristof Provost epair=$(vnet_mkepair) 2475632f585SKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 2485632f585SKristof Provost 2495632f585SKristof Provost vnet_mkjail alcatraz ${epair}b 2505632f585SKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 2515632f585SKristof Provost jexec alcatraz pfctl -e 2525632f585SKristof Provost 2535632f585SKristof Provost pft_set_rules alcatraz "block all" \ 2545632f585SKristof Provost "pass in proto icmp label foo label bar" 2555632f585SKristof Provost 2565632f585SKristof Provost # Sanity check & establish state 2575632f585SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 2585632f585SKristof Provost --sendif ${epair}a \ 2595632f585SKristof Provost --to 192.0.2.2 \ 2605632f585SKristof Provost --replyif ${epair}a 2615632f585SKristof Provost 2625632f585SKristof Provost # Change rules to now deny the ICMP traffic 2635632f585SKristof Provost pft_set_rules noflush alcatraz "block all" 26445258e1bSKristof Provost if ! find_state; 26545258e1bSKristof Provost then 26645258e1bSKristof Provost atf_fail "Setting new rules removed the state." 26745258e1bSKristof Provost fi 2685632f585SKristof Provost 2695632f585SKristof Provost # Killing a label on a different rules keeps the state 2705632f585SKristof Provost jexec alcatraz pfctl -k label -k baz 27145258e1bSKristof Provost if ! find_state; 27245258e1bSKristof Provost then 27345258e1bSKristof Provost atf_fail "Killing a different label removed the state." 27445258e1bSKristof Provost fi 2755632f585SKristof Provost 2765632f585SKristof Provost # Killing the state with the last label works 2775632f585SKristof Provost jexec alcatraz pfctl -k label -k bar 27845258e1bSKristof Provost if find_state; 27945258e1bSKristof Provost then 28045258e1bSKristof Provost atf_fail "Killing with the last label did not remove the state." 28145258e1bSKristof Provost fi 2825632f585SKristof Provost 2835632f585SKristof Provost pft_set_rules alcatraz "block all" \ 2845632f585SKristof Provost "pass in proto icmp label foo label bar" 2855632f585SKristof Provost 2865632f585SKristof Provost # Reestablish state 2875632f585SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 2885632f585SKristof Provost --sendif ${epair}a \ 2895632f585SKristof Provost --to 192.0.2.2 \ 2905632f585SKristof Provost --replyif ${epair}a 2915632f585SKristof Provost 2925632f585SKristof Provost # Change rules to now deny the ICMP traffic 2935632f585SKristof Provost pft_set_rules noflush alcatraz "block all" 29445258e1bSKristof Provost if ! find_state; 29545258e1bSKristof Provost then 29645258e1bSKristof Provost atf_fail "Setting new rules removed the state." 29745258e1bSKristof Provost fi 2985632f585SKristof Provost 2995632f585SKristof Provost # Killing with the first label works too 3005632f585SKristof Provost jexec alcatraz pfctl -k label -k foo 30145258e1bSKristof Provost if find_state; 30245258e1bSKristof Provost then 30345258e1bSKristof Provost atf_fail "Killing with the first label did not remove the state." 30445258e1bSKristof Provost fi 3055632f585SKristof Provost} 3065632f585SKristof Provost 3075632f585SKristof Provostmultilabel_cleanup() 3085632f585SKristof Provost{ 3095632f585SKristof Provost pft_cleanup 3105632f585SKristof Provost} 3115632f585SKristof Provost 312c2e11d81SKristof Provostatf_test_case "gateway" "cleanup" 313c2e11d81SKristof Provostgateway_head() 314c2e11d81SKristof Provost{ 315c2e11d81SKristof Provost atf_set descr 'Test killing states by route-to/reply-to address' 316c2e11d81SKristof Provost atf_set require.user root 317c2e11d81SKristof Provost atf_set require.progs scapy 318c2e11d81SKristof Provost} 319c2e11d81SKristof Provost 320c2e11d81SKristof Provostgateway_body() 321c2e11d81SKristof Provost{ 322c2e11d81SKristof Provost pft_init 323c2e11d81SKristof Provost 324c2e11d81SKristof Provost epair=$(vnet_mkepair) 325c2e11d81SKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 326c2e11d81SKristof Provost 327c2e11d81SKristof Provost vnet_mkjail alcatraz ${epair}b 328c2e11d81SKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 329c2e11d81SKristof Provost jexec alcatraz pfctl -e 330c2e11d81SKristof Provost 331c2e11d81SKristof Provost pft_set_rules alcatraz "block all" \ 332c2e11d81SKristof Provost "pass in reply-to (${epair}b 192.0.2.1) proto icmp" 333c2e11d81SKristof Provost 334c2e11d81SKristof Provost # Sanity check & establish state 335c2e11d81SKristof Provost # Note: use pft_ping so we always use the same ID, so pf considers all 336c2e11d81SKristof Provost # echo requests part of the same flow. 337c2e11d81SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 338c2e11d81SKristof Provost --sendif ${epair}a \ 339c2e11d81SKristof Provost --to 192.0.2.2 \ 340c2e11d81SKristof Provost --replyif ${epair}a 341c2e11d81SKristof Provost 342c2e11d81SKristof Provost # Change rules to now deny the ICMP traffic 343c2e11d81SKristof Provost pft_set_rules noflush alcatraz "block all" 34445258e1bSKristof Provost if ! find_state; 34545258e1bSKristof Provost then 34645258e1bSKristof Provost atf_fail "Setting new rules removed the state." 34745258e1bSKristof Provost fi 348c2e11d81SKristof Provost 349c2e11d81SKristof Provost # Killing with a different gateway does not affect our state 350c2e11d81SKristof Provost jexec alcatraz pfctl -k gateway -k 192.0.2.2 35145258e1bSKristof Provost if ! find_state; 35245258e1bSKristof Provost then 35345258e1bSKristof Provost atf_fail "Killing with a different gateway removed the state." 35445258e1bSKristof Provost fi 355c2e11d81SKristof Provost 356c2e11d81SKristof Provost # Killing states with the relevant gateway does terminate our state 357c2e11d81SKristof Provost jexec alcatraz pfctl -k gateway -k 192.0.2.1 35845258e1bSKristof Provost if find_state; 35945258e1bSKristof Provost then 36045258e1bSKristof Provost atf_fail "Killing with the gateway did not remove the state." 36145258e1bSKristof Provost fi 362c2e11d81SKristof Provost} 363c2e11d81SKristof Provost 364c2e11d81SKristof Provostgateway_cleanup() 365c2e11d81SKristof Provost{ 366c2e11d81SKristof Provost pft_cleanup 367c2e11d81SKristof Provost} 368c2e11d81SKristof Provost 369ac200a9cSKristof Provostatf_test_case "match" "cleanup" 370ac200a9cSKristof Provostmatch_head() 371ac200a9cSKristof Provost{ 372ac200a9cSKristof Provost atf_set descr 'Test killing matching states' 373ac200a9cSKristof Provost atf_set require.user root 374ac200a9cSKristof Provost} 375ac200a9cSKristof Provost 37670dd30d4SKristof Provostwait_for_state() 37770dd30d4SKristof Provost{ 37870dd30d4SKristof Provost jail=$1 37970dd30d4SKristof Provost addr=$2 38070dd30d4SKristof Provost 38170dd30d4SKristof Provost while ! jexec $jail pfctl -s s | grep $addr >/dev/null; 38270dd30d4SKristof Provost do 38370dd30d4SKristof Provost sleep .1 38470dd30d4SKristof Provost done 38570dd30d4SKristof Provost} 38670dd30d4SKristof Provost 387ac200a9cSKristof Provostmatch_body() 388ac200a9cSKristof Provost{ 389ac200a9cSKristof Provost pft_init 390ac200a9cSKristof Provost 391ac200a9cSKristof Provost epair_one=$(vnet_mkepair) 392ac200a9cSKristof Provost ifconfig ${epair_one}a 192.0.2.1/24 up 393ac200a9cSKristof Provost 394ac200a9cSKristof Provost epair_two=$(vnet_mkepair) 395ac200a9cSKristof Provost 396ac200a9cSKristof Provost vnet_mkjail alcatraz ${epair_one}b ${epair_two}a 397ac200a9cSKristof Provost jexec alcatraz ifconfig ${epair_one}b 192.0.2.2/24 up 398ac200a9cSKristof Provost jexec alcatraz ifconfig ${epair_two}a 198.51.100.1/24 up 399ac200a9cSKristof Provost jexec alcatraz sysctl net.inet.ip.forwarding=1 400ac200a9cSKristof Provost jexec alcatraz pfctl -e 401ac200a9cSKristof Provost 402ac200a9cSKristof Provost vnet_mkjail singsing ${epair_two}b 403ac200a9cSKristof Provost jexec singsing ifconfig ${epair_two}b 198.51.100.2/24 up 404ac200a9cSKristof Provost jexec singsing route add default 198.51.100.1 405ac200a9cSKristof Provost jexec singsing /usr/sbin/inetd -p inetd-echo.pid \ 406ac200a9cSKristof Provost $(atf_get_srcdir)/echo_inetd.conf 407ac200a9cSKristof Provost 408ac200a9cSKristof Provost route add 198.51.100.0/24 192.0.2.2 409ac200a9cSKristof Provost 410ac200a9cSKristof Provost pft_set_rules alcatraz \ 411ac200a9cSKristof Provost "nat on ${epair_two}a from 192.0.2.0/24 -> (${epair_two}a)" \ 412ac200a9cSKristof Provost "pass all" 413ac200a9cSKristof Provost 414ac200a9cSKristof Provost nc 198.51.100.2 7 & 41570dd30d4SKristof Provost wait_for_state alcatraz 192.0.2.1 416ac200a9cSKristof Provost 417ac200a9cSKristof Provost # Expect two states 4184e860bd5SKristof Provost states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l) 419ac200a9cSKristof Provost if [ $states -ne 2 ] ; 420ac200a9cSKristof Provost then 421ac200a9cSKristof Provost atf_fail "Expected two states, found $states" 422ac200a9cSKristof Provost fi 423ac200a9cSKristof Provost 424ac200a9cSKristof Provost # If we don't kill the matching NAT state one should be left 425ac200a9cSKristof Provost jexec alcatraz pfctl -k 192.0.2.1 4264e860bd5SKristof Provost states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l) 427ac200a9cSKristof Provost if [ $states -ne 1 ] ; 428ac200a9cSKristof Provost then 429ac200a9cSKristof Provost atf_fail "Expected one states, found $states" 430ac200a9cSKristof Provost fi 431ac200a9cSKristof Provost 432ac200a9cSKristof Provost # Flush 433ac200a9cSKristof Provost jexec alcatraz pfctl -F states 434ac200a9cSKristof Provost 435ac200a9cSKristof Provost nc 198.51.100.2 7 & 43670dd30d4SKristof Provost wait_for_state alcatraz 192.0.2.1 437ac200a9cSKristof Provost 438ac200a9cSKristof Provost # Kill matching states, expect all of them to be gone 439ac200a9cSKristof Provost jexec alcatraz pfctl -M -k 192.0.2.1 4404e860bd5SKristof Provost states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l) 441ac200a9cSKristof Provost if [ $states -ne 0 ] ; 442ac200a9cSKristof Provost then 443ac200a9cSKristof Provost atf_fail "Expected zero states, found $states" 444ac200a9cSKristof Provost fi 445ac200a9cSKristof Provost} 446ac200a9cSKristof Provost 447ac200a9cSKristof Provostmatch_cleanup() 448ac200a9cSKristof Provost{ 449ac200a9cSKristof Provost pft_cleanup 450ac200a9cSKristof Provost} 451ac200a9cSKristof Provost 4527bd7933fSKristof Provostatf_test_case "interface" "cleanup" 4537bd7933fSKristof Provostinterface_head() 4547bd7933fSKristof Provost{ 4557bd7933fSKristof Provost atf_set descr 'Test killing states based on interface' 4567bd7933fSKristof Provost atf_set require.user root 4577bd7933fSKristof Provost atf_set require.progs scapy 4587bd7933fSKristof Provost} 4597bd7933fSKristof Provost 4607bd7933fSKristof Provostinterface_body() 4617bd7933fSKristof Provost{ 4627bd7933fSKristof Provost pft_init 4637bd7933fSKristof Provost 4647bd7933fSKristof Provost epair=$(vnet_mkepair) 4657bd7933fSKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 4667bd7933fSKristof Provost 4677bd7933fSKristof Provost vnet_mkjail alcatraz ${epair}b 4687bd7933fSKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 4697bd7933fSKristof Provost jexec alcatraz pfctl -e 4707bd7933fSKristof Provost 4717bd7933fSKristof Provost pft_set_rules alcatraz "block all" \ 4727bd7933fSKristof Provost "pass in proto icmp" 4737bd7933fSKristof Provost 4747bd7933fSKristof Provost # Sanity check & establish state 4757bd7933fSKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 4767bd7933fSKristof Provost --sendif ${epair}a \ 4777bd7933fSKristof Provost --to 192.0.2.2 \ 4787bd7933fSKristof Provost --replyif ${epair}a 4797bd7933fSKristof Provost 4807bd7933fSKristof Provost # Change rules to now deny the ICMP traffic 4817bd7933fSKristof Provost pft_set_rules noflush alcatraz "block all" 48245258e1bSKristof Provost if ! find_state; 48345258e1bSKristof Provost then 48445258e1bSKristof Provost atf_fail "Setting new rules removed the state." 48545258e1bSKristof Provost fi 4867bd7933fSKristof Provost 4877bd7933fSKristof Provost # Flushing states on a different interface doesn't affect our state 4887bd7933fSKristof Provost jexec alcatraz pfctl -i ${epair}a -Fs 48945258e1bSKristof Provost if ! find_state; 49045258e1bSKristof Provost then 49145258e1bSKristof Provost atf_fail "Flushing on a different interface removed the state." 49245258e1bSKristof Provost fi 4937bd7933fSKristof Provost 4947bd7933fSKristof Provost # Flushing on the correct interface does (even with floating states) 4957bd7933fSKristof Provost jexec alcatraz pfctl -i ${epair}b -Fs 49645258e1bSKristof Provost if find_state; 49745258e1bSKristof Provost then 49845258e1bSKristof Provost atf_fail "Flushing on a the interface did not remove the state." 49945258e1bSKristof Provost fi 5007bd7933fSKristof Provost} 5017bd7933fSKristof Provost 5027bd7933fSKristof Provostinterface_cleanup() 5037bd7933fSKristof Provost{ 5047bd7933fSKristof Provost pft_cleanup 5057bd7933fSKristof Provost} 5067bd7933fSKristof Provost 507bbf832f3SKristof Provostatf_test_case "id" "cleanup" 508bbf832f3SKristof Provostid_head() 509bbf832f3SKristof Provost{ 510bbf832f3SKristof Provost atf_set descr 'Test killing states by id' 511bbf832f3SKristof Provost atf_set require.user root 512bbf832f3SKristof Provost atf_set require.progs scapy 513bbf832f3SKristof Provost} 514bbf832f3SKristof Provost 515bbf832f3SKristof Provostid_body() 516bbf832f3SKristof Provost{ 517bbf832f3SKristof Provost pft_init 518bbf832f3SKristof Provost 519bbf832f3SKristof Provost epair=$(vnet_mkepair) 520bbf832f3SKristof Provost ifconfig ${epair}a 192.0.2.1/24 up 521bbf832f3SKristof Provost 522bbf832f3SKristof Provost vnet_mkjail alcatraz ${epair}b 523bbf832f3SKristof Provost jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up 524bbf832f3SKristof Provost jexec alcatraz pfctl -e 525bbf832f3SKristof Provost 526bbf832f3SKristof Provost pft_set_rules alcatraz "block all" \ 527bbf832f3SKristof Provost "pass in proto tcp" \ 528bbf832f3SKristof Provost "pass in proto icmp" 529bbf832f3SKristof Provost 530bbf832f3SKristof Provost # Sanity check & establish state 531bbf832f3SKristof Provost atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \ 532bbf832f3SKristof Provost --sendif ${epair}a \ 533bbf832f3SKristof Provost --to 192.0.2.2 \ 534bbf832f3SKristof Provost --replyif ${epair}a 535bbf832f3SKristof Provost 536bbf832f3SKristof Provost # Change rules to now deny the ICMP traffic 537bbf832f3SKristof Provost pft_set_rules noflush alcatraz "block all" 53845258e1bSKristof Provost if ! find_state; 53945258e1bSKristof Provost then 54045258e1bSKristof Provost atf_fail "Setting new rules removed the state." 54145258e1bSKristof Provost fi 542bbf832f3SKristof Provost 543bbf832f3SKristof Provost # Get the state ID 544bbf832f3SKristof Provost id=$(jexec alcatraz pfctl -ss -vvv | grep -A 3 icmp | 545bbf832f3SKristof Provost grep -A 3 192.0.2.2 | awk '/id:/ { printf("%s/%s", $2, $4); }') 546bbf832f3SKristof Provost 547bbf832f3SKristof Provost # Kill the wrong ID 548bbf832f3SKristof Provost jexec alcatraz pfctl -k id -k 1 54945258e1bSKristof Provost if ! find_state; 55045258e1bSKristof Provost then 55145258e1bSKristof Provost atf_fail "Killing a different ID removed the state." 55245258e1bSKristof Provost fi 553bbf832f3SKristof Provost 554bbf832f3SKristof Provost # Kill the correct ID 555bbf832f3SKristof Provost jexec alcatraz pfctl -k id -k ${id} 55645258e1bSKristof Provost if find_state; 55745258e1bSKristof Provost then 55845258e1bSKristof Provost atf_fail "Killing the state did not remove it." 55945258e1bSKristof Provost fi 560bbf832f3SKristof Provost} 561bbf832f3SKristof Provost 562bbf832f3SKristof Provostid_cleanup() 563bbf832f3SKristof Provost{ 564bbf832f3SKristof Provost pft_cleanup 565bbf832f3SKristof Provost} 566bbf832f3SKristof Provost 567065b5c7fSKristof Provostatf_init_test_cases() 568065b5c7fSKristof Provost{ 569065b5c7fSKristof Provost atf_add_test_case "v4" 5709af23174SKristof Provost atf_add_test_case "v6" 571065b5c7fSKristof Provost atf_add_test_case "label" 5725632f585SKristof Provost atf_add_test_case "multilabel" 573c2e11d81SKristof Provost atf_add_test_case "gateway" 574ac200a9cSKristof Provost atf_add_test_case "match" 5757bd7933fSKristof Provost atf_add_test_case "interface" 576bbf832f3SKristof Provost atf_add_test_case "id" 577065b5c7fSKristof Provost} 578