xref: /freebsd/tests/sys/netpfil/pf/killstate.sh (revision 45258e1bc71b1dc4ec28e5d37c3ca6c02de61857)
1065b5c7fSKristof Provost# $FreeBSD$
2065b5c7fSKristof Provost#
3065b5c7fSKristof Provost# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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
32*45258e1bSKristof Provostfind_state()
33*45258e1bSKristof Provost{
34*45258e1bSKristof Provost	jexec alcatraz pfctl -ss | grep icmp | grep 192.0.2.2
35*45258e1bSKristof Provost}
36*45258e1bSKristof Provost
37*45258e1bSKristof Provostfind_state_v6()
38*45258e1bSKristof Provost{
39*45258e1bSKristof Provost	jexec alcatraz pfctl -ss | grep icmp | grep 2001:db8::2
40*45258e1bSKristof Provost}
41*45258e1bSKristof Provost
42*45258e1bSKristof 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"
73*45258e1bSKristof Provost	if ! find_state;
74*45258e1bSKristof Provost	then
75*45258e1bSKristof Provost		atf_fail "Setting new rules removed the state."
76*45258e1bSKristof 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
80*45258e1bSKristof Provost	if ! find_state;
81*45258e1bSKristof Provost	then
82*45258e1bSKristof Provost		atf_fail "Killing with the wrong IP removed our state."
83*45258e1bSKristof 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
87*45258e1bSKristof Provost	if ! find_state;
88*45258e1bSKristof Provost	then
89*45258e1bSKristof Provost		atf_fail "Killing with one wrong IP removed our state."
90*45258e1bSKristof Provost	fi
91065b5c7fSKristof Provost
92065b5c7fSKristof Provost	# Killing with correct address does remove the state
93065b5c7fSKristof Provost	jexec alcatraz pfctl -k 192.0.2.1
94*45258e1bSKristof Provost	if find_state;
95*45258e1bSKristof Provost	then
96*45258e1bSKristof Provost		atf_fail "Killing with the correct IP did not remove our state."
97*45258e1bSKristof 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		--ip6 \
1349af23174SKristof Provost		--sendif ${epair}a \
1359af23174SKristof Provost		--to 2001:db8::2 \
1369af23174SKristof Provost		--replyif ${epair}a
1379af23174SKristof Provost
1389af23174SKristof Provost	# Change rules to now deny the ICMP traffic
1399af23174SKristof Provost	pft_set_rules noflush alcatraz "block all"
140*45258e1bSKristof Provost	if ! find_state_v6;
141*45258e1bSKristof Provost	then
142*45258e1bSKristof Provost		atf_fail "Setting new rules removed the state."
143*45258e1bSKristof Provost	fi
1449af23174SKristof Provost
1459af23174SKristof Provost	# Killing with the wrong IP doesn't affect our state
1469af23174SKristof Provost	jexec alcatraz pfctl -k 2001:db8::3
147*45258e1bSKristof Provost	if ! find_state_v6;
148*45258e1bSKristof Provost	then
149*45258e1bSKristof Provost		atf_fail "Killing with the wrong IP removed our state."
150*45258e1bSKristof Provost	fi
1519af23174SKristof Provost
1529af23174SKristof Provost	# Killing with one correct address and one incorrect doesn't kill the state
1539af23174SKristof Provost	jexec alcatraz pfctl -k 2001:db8::1 -k 2001:db8::3
154*45258e1bSKristof Provost	if ! find_state_v6;
155*45258e1bSKristof Provost	then
156*45258e1bSKristof Provost		atf_fail "Killing with one wrong IP removed our state."
157*45258e1bSKristof Provost	fi
1589af23174SKristof Provost
1599af23174SKristof Provost	# Killing with correct address does remove the state
1609af23174SKristof Provost	jexec alcatraz pfctl -k 2001:db8::1
161*45258e1bSKristof Provost	if find_state_v6;
162*45258e1bSKristof Provost	then
163*45258e1bSKristof Provost		atf_fail "Killing with the correct IP did not remove our state."
164*45258e1bSKristof Provost	fi
1659af23174SKristof Provost}
1669af23174SKristof Provost
1679af23174SKristof Provostv6_cleanup()
1689af23174SKristof Provost{
1699af23174SKristof Provost	pft_cleanup
1709af23174SKristof Provost}
1719af23174SKristof Provost
172065b5c7fSKristof Provostatf_test_case "label" "cleanup"
173065b5c7fSKristof Provostlabel_head()
174065b5c7fSKristof Provost{
175065b5c7fSKristof Provost	atf_set descr 'Test killing states by label'
176065b5c7fSKristof Provost	atf_set require.user root
177065b5c7fSKristof Provost	atf_set require.progs scapy
178065b5c7fSKristof Provost}
179065b5c7fSKristof Provost
180065b5c7fSKristof Provostlabel_body()
181065b5c7fSKristof Provost{
182065b5c7fSKristof Provost	pft_init
183065b5c7fSKristof Provost
184065b5c7fSKristof Provost	epair=$(vnet_mkepair)
185065b5c7fSKristof Provost	ifconfig ${epair}a 192.0.2.1/24 up
186065b5c7fSKristof Provost
187065b5c7fSKristof Provost	vnet_mkjail alcatraz ${epair}b
188065b5c7fSKristof Provost	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
189065b5c7fSKristof Provost	jexec alcatraz pfctl -e
190065b5c7fSKristof Provost
191065b5c7fSKristof Provost	pft_set_rules alcatraz "block all" \
192065b5c7fSKristof Provost		"pass in proto tcp label bar" \
193065b5c7fSKristof Provost		"pass in proto icmp label foo"
194065b5c7fSKristof Provost
195065b5c7fSKristof Provost	# Sanity check & establish state
196065b5c7fSKristof Provost	atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \
197065b5c7fSKristof Provost		--sendif ${epair}a \
198065b5c7fSKristof Provost		--to 192.0.2.2 \
199065b5c7fSKristof Provost		--replyif ${epair}a
200065b5c7fSKristof Provost
201065b5c7fSKristof Provost	# Change rules to now deny the ICMP traffic
202065b5c7fSKristof Provost	pft_set_rules noflush alcatraz "block all"
203*45258e1bSKristof Provost	if ! find_state;
204*45258e1bSKristof Provost	then
205*45258e1bSKristof Provost		atf_fail "Setting new rules removed the state."
206*45258e1bSKristof Provost	fi
207065b5c7fSKristof Provost
208065b5c7fSKristof Provost	# Killing a label on a different rules keeps the state
209065b5c7fSKristof Provost	jexec alcatraz pfctl -k label -k bar
210*45258e1bSKristof Provost	if ! find_state;
211*45258e1bSKristof Provost	then
212*45258e1bSKristof Provost		atf_fail "Killing a different label removed the state."
213*45258e1bSKristof Provost	fi
214065b5c7fSKristof Provost
215065b5c7fSKristof Provost	# Killing a non-existing label keeps the state
216065b5c7fSKristof Provost	jexec alcatraz pfctl -k label -k baz
217*45258e1bSKristof Provost	if ! find_state;
218*45258e1bSKristof Provost	then
219*45258e1bSKristof Provost		atf_fail "Killing a non-existing label removed the state."
220*45258e1bSKristof Provost	fi
221065b5c7fSKristof Provost
222065b5c7fSKristof Provost	# Killing the correct label kills the state
223065b5c7fSKristof Provost	jexec alcatraz pfctl -k label -k foo
224*45258e1bSKristof Provost	if find_state;
225*45258e1bSKristof Provost	then
226*45258e1bSKristof Provost		atf_fail "Killing the state did not remove it."
227*45258e1bSKristof Provost	fi
228065b5c7fSKristof Provost}
229065b5c7fSKristof Provost
230065b5c7fSKristof Provostlabel_cleanup()
231065b5c7fSKristof Provost{
232065b5c7fSKristof Provost	pft_cleanup
233065b5c7fSKristof Provost}
234065b5c7fSKristof Provost
2355632f585SKristof Provostatf_test_case "multilabel" "cleanup"
2365632f585SKristof Provostmultilabel_head()
2375632f585SKristof Provost{
2385632f585SKristof Provost	atf_set descr 'Test killing states with multiple labels by label'
2395632f585SKristof Provost	atf_set require.user root
2405632f585SKristof Provost	atf_set require.progs scapy
2415632f585SKristof Provost}
2425632f585SKristof Provost
2435632f585SKristof Provostmultilabel_body()
2445632f585SKristof Provost{
2455632f585SKristof Provost	pft_init
2465632f585SKristof Provost
2475632f585SKristof Provost	epair=$(vnet_mkepair)
2485632f585SKristof Provost	ifconfig ${epair}a 192.0.2.1/24 up
2495632f585SKristof Provost
2505632f585SKristof Provost	vnet_mkjail alcatraz ${epair}b
2515632f585SKristof Provost	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
2525632f585SKristof Provost	jexec alcatraz pfctl -e
2535632f585SKristof Provost
2545632f585SKristof Provost	pft_set_rules alcatraz "block all" \
2555632f585SKristof Provost		"pass in proto icmp label foo label bar"
2565632f585SKristof Provost
2575632f585SKristof Provost	# Sanity check & establish state
2585632f585SKristof Provost	atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \
2595632f585SKristof Provost		--sendif ${epair}a \
2605632f585SKristof Provost		--to 192.0.2.2 \
2615632f585SKristof Provost		--replyif ${epair}a
2625632f585SKristof Provost
2635632f585SKristof Provost	# Change rules to now deny the ICMP traffic
2645632f585SKristof Provost	pft_set_rules noflush alcatraz "block all"
265*45258e1bSKristof Provost	if ! find_state;
266*45258e1bSKristof Provost	then
267*45258e1bSKristof Provost		atf_fail "Setting new rules removed the state."
268*45258e1bSKristof Provost	fi
2695632f585SKristof Provost
2705632f585SKristof Provost	# Killing a label on a different rules keeps the state
2715632f585SKristof Provost	jexec alcatraz pfctl -k label -k baz
272*45258e1bSKristof Provost	if ! find_state;
273*45258e1bSKristof Provost	then
274*45258e1bSKristof Provost		atf_fail "Killing a different label removed the state."
275*45258e1bSKristof Provost	fi
2765632f585SKristof Provost
2775632f585SKristof Provost	# Killing the state with the last label works
2785632f585SKristof Provost	jexec alcatraz pfctl -k label -k bar
279*45258e1bSKristof Provost	if find_state;
280*45258e1bSKristof Provost	then
281*45258e1bSKristof Provost		atf_fail "Killing with the last label did not remove the state."
282*45258e1bSKristof Provost	fi
2835632f585SKristof Provost
2845632f585SKristof Provost	pft_set_rules alcatraz "block all" \
2855632f585SKristof Provost		"pass in proto icmp label foo label bar"
2865632f585SKristof Provost
2875632f585SKristof Provost	# Reestablish state
2885632f585SKristof Provost	atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \
2895632f585SKristof Provost		--sendif ${epair}a \
2905632f585SKristof Provost		--to 192.0.2.2 \
2915632f585SKristof Provost		--replyif ${epair}a
2925632f585SKristof Provost
2935632f585SKristof Provost	# Change rules to now deny the ICMP traffic
2945632f585SKristof Provost	pft_set_rules noflush alcatraz "block all"
295*45258e1bSKristof Provost	if ! find_state;
296*45258e1bSKristof Provost	then
297*45258e1bSKristof Provost		atf_fail "Setting new rules removed the state."
298*45258e1bSKristof Provost	fi
2995632f585SKristof Provost
3005632f585SKristof Provost	# Killing with the first label works too
3015632f585SKristof Provost	jexec alcatraz pfctl -k label -k foo
302*45258e1bSKristof Provost	if find_state;
303*45258e1bSKristof Provost	then
304*45258e1bSKristof Provost		atf_fail "Killing with the first label did not remove the state."
305*45258e1bSKristof Provost	fi
3065632f585SKristof Provost}
3075632f585SKristof Provost
3085632f585SKristof Provostmultilabel_cleanup()
3095632f585SKristof Provost{
3105632f585SKristof Provost	pft_cleanup
3115632f585SKristof Provost}
3125632f585SKristof Provost
313c2e11d81SKristof Provostatf_test_case "gateway" "cleanup"
314c2e11d81SKristof Provostgateway_head()
315c2e11d81SKristof Provost{
316c2e11d81SKristof Provost	atf_set descr 'Test killing states by route-to/reply-to address'
317c2e11d81SKristof Provost	atf_set require.user root
318c2e11d81SKristof Provost	atf_set require.progs scapy
319c2e11d81SKristof Provost}
320c2e11d81SKristof Provost
321c2e11d81SKristof Provostgateway_body()
322c2e11d81SKristof Provost{
323c2e11d81SKristof Provost	pft_init
324c2e11d81SKristof Provost
325c2e11d81SKristof Provost	epair=$(vnet_mkepair)
326c2e11d81SKristof Provost	ifconfig ${epair}a 192.0.2.1/24 up
327c2e11d81SKristof Provost
328c2e11d81SKristof Provost	vnet_mkjail alcatraz ${epair}b
329c2e11d81SKristof Provost	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
330c2e11d81SKristof Provost	jexec alcatraz pfctl -e
331c2e11d81SKristof Provost
332c2e11d81SKristof Provost	pft_set_rules alcatraz "block all" \
333c2e11d81SKristof Provost		"pass in reply-to (${epair}b 192.0.2.1) proto icmp"
334c2e11d81SKristof Provost
335c2e11d81SKristof Provost	# Sanity check & establish state
336c2e11d81SKristof Provost	# Note: use pft_ping so we always use the same ID, so pf considers all
337c2e11d81SKristof Provost	# echo requests part of the same flow.
338c2e11d81SKristof Provost	atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \
339c2e11d81SKristof Provost		--sendif ${epair}a \
340c2e11d81SKristof Provost		--to 192.0.2.2 \
341c2e11d81SKristof Provost		--replyif ${epair}a
342c2e11d81SKristof Provost
343c2e11d81SKristof Provost	# Change rules to now deny the ICMP traffic
344c2e11d81SKristof Provost	pft_set_rules noflush alcatraz "block all"
345*45258e1bSKristof Provost	if ! find_state;
346*45258e1bSKristof Provost	then
347*45258e1bSKristof Provost		atf_fail "Setting new rules removed the state."
348*45258e1bSKristof Provost	fi
349c2e11d81SKristof Provost
350c2e11d81SKristof Provost	# Killing with a different gateway does not affect our state
351c2e11d81SKristof Provost	jexec alcatraz pfctl -k gateway -k 192.0.2.2
352*45258e1bSKristof Provost	if ! find_state;
353*45258e1bSKristof Provost	then
354*45258e1bSKristof Provost		atf_fail "Killing with a different gateway removed the state."
355*45258e1bSKristof Provost	fi
356c2e11d81SKristof Provost
357c2e11d81SKristof Provost	# Killing states with the relevant gateway does terminate our state
358c2e11d81SKristof Provost	jexec alcatraz pfctl -k gateway -k 192.0.2.1
359*45258e1bSKristof Provost	if find_state;
360*45258e1bSKristof Provost	then
361*45258e1bSKristof Provost		atf_fail "Killing with the gateway did not remove the state."
362*45258e1bSKristof Provost	fi
363c2e11d81SKristof Provost}
364c2e11d81SKristof Provost
365c2e11d81SKristof Provostgateway_cleanup()
366c2e11d81SKristof Provost{
367c2e11d81SKristof Provost	pft_cleanup
368c2e11d81SKristof Provost}
369c2e11d81SKristof Provost
370ac200a9cSKristof Provostatf_test_case "match" "cleanup"
371ac200a9cSKristof Provostmatch_head()
372ac200a9cSKristof Provost{
373ac200a9cSKristof Provost	atf_set descr 'Test killing matching states'
374ac200a9cSKristof Provost	atf_set require.user root
375ac200a9cSKristof Provost}
376ac200a9cSKristof Provost
37770dd30d4SKristof Provostwait_for_state()
37870dd30d4SKristof Provost{
37970dd30d4SKristof Provost	jail=$1
38070dd30d4SKristof Provost	addr=$2
38170dd30d4SKristof Provost
38270dd30d4SKristof Provost	while ! jexec $jail pfctl -s s | grep $addr >/dev/null;
38370dd30d4SKristof Provost	do
38470dd30d4SKristof Provost		sleep .1
38570dd30d4SKristof Provost	done
38670dd30d4SKristof Provost}
38770dd30d4SKristof Provost
388ac200a9cSKristof Provostmatch_body()
389ac200a9cSKristof Provost{
390ac200a9cSKristof Provost	pft_init
391ac200a9cSKristof Provost
392ac200a9cSKristof Provost	epair_one=$(vnet_mkepair)
393ac200a9cSKristof Provost	ifconfig ${epair_one}a 192.0.2.1/24 up
394ac200a9cSKristof Provost
395ac200a9cSKristof Provost	epair_two=$(vnet_mkepair)
396ac200a9cSKristof Provost
397ac200a9cSKristof Provost	vnet_mkjail alcatraz ${epair_one}b ${epair_two}a
398ac200a9cSKristof Provost	jexec alcatraz ifconfig ${epair_one}b 192.0.2.2/24 up
399ac200a9cSKristof Provost	jexec alcatraz ifconfig ${epair_two}a 198.51.100.1/24 up
400ac200a9cSKristof Provost	jexec alcatraz sysctl net.inet.ip.forwarding=1
401ac200a9cSKristof Provost	jexec alcatraz pfctl -e
402ac200a9cSKristof Provost
403ac200a9cSKristof Provost	vnet_mkjail singsing ${epair_two}b
404ac200a9cSKristof Provost	jexec singsing ifconfig ${epair_two}b 198.51.100.2/24 up
405ac200a9cSKristof Provost	jexec singsing route add default 198.51.100.1
406ac200a9cSKristof Provost	jexec singsing /usr/sbin/inetd -p inetd-echo.pid \
407ac200a9cSKristof Provost	    $(atf_get_srcdir)/echo_inetd.conf
408ac200a9cSKristof Provost
409ac200a9cSKristof Provost	route add 198.51.100.0/24 192.0.2.2
410ac200a9cSKristof Provost
411ac200a9cSKristof Provost	pft_set_rules alcatraz \
412ac200a9cSKristof Provost		"nat on ${epair_two}a from 192.0.2.0/24 -> (${epair_two}a)" \
413ac200a9cSKristof Provost		"pass all"
414ac200a9cSKristof Provost
415ac200a9cSKristof Provost	nc 198.51.100.2 7 &
41670dd30d4SKristof Provost	wait_for_state alcatraz 192.0.2.1
417ac200a9cSKristof Provost
418ac200a9cSKristof Provost	# Expect two states
4194e860bd5SKristof Provost	states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l)
420ac200a9cSKristof Provost	if [ $states -ne 2 ] ;
421ac200a9cSKristof Provost	then
422ac200a9cSKristof Provost		atf_fail "Expected two states, found $states"
423ac200a9cSKristof Provost	fi
424ac200a9cSKristof Provost
425ac200a9cSKristof Provost	# If we don't kill the matching NAT state one should be left
426ac200a9cSKristof Provost	jexec alcatraz pfctl -k 192.0.2.1
4274e860bd5SKristof Provost	states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l)
428ac200a9cSKristof Provost	if [ $states -ne 1 ] ;
429ac200a9cSKristof Provost	then
430ac200a9cSKristof Provost		atf_fail "Expected one states, found $states"
431ac200a9cSKristof Provost	fi
432ac200a9cSKristof Provost
433ac200a9cSKristof Provost	# Flush
434ac200a9cSKristof Provost	jexec alcatraz pfctl -F states
435ac200a9cSKristof Provost
436ac200a9cSKristof Provost	nc 198.51.100.2 7 &
43770dd30d4SKristof Provost	wait_for_state alcatraz 192.0.2.1
438ac200a9cSKristof Provost
439ac200a9cSKristof Provost	# Kill matching states, expect all of them to be gone
440ac200a9cSKristof Provost	jexec alcatraz pfctl -M -k 192.0.2.1
4414e860bd5SKristof Provost	states=$(jexec alcatraz pfctl -s s | grep 192.0.2.1 | wc -l)
442ac200a9cSKristof Provost	if [ $states -ne 0 ] ;
443ac200a9cSKristof Provost	then
444ac200a9cSKristof Provost		atf_fail "Expected zero states, found $states"
445ac200a9cSKristof Provost	fi
446ac200a9cSKristof Provost}
447ac200a9cSKristof Provost
448ac200a9cSKristof Provostmatch_cleanup()
449ac200a9cSKristof Provost{
450ac200a9cSKristof Provost	pft_cleanup
451ac200a9cSKristof Provost}
452ac200a9cSKristof Provost
4537bd7933fSKristof Provostatf_test_case "interface" "cleanup"
4547bd7933fSKristof Provostinterface_head()
4557bd7933fSKristof Provost{
4567bd7933fSKristof Provost	atf_set descr 'Test killing states based on interface'
4577bd7933fSKristof Provost	atf_set require.user root
4587bd7933fSKristof Provost	atf_set require.progs scapy
4597bd7933fSKristof Provost}
4607bd7933fSKristof Provost
4617bd7933fSKristof Provostinterface_body()
4627bd7933fSKristof Provost{
4637bd7933fSKristof Provost	pft_init
4647bd7933fSKristof Provost
4657bd7933fSKristof Provost	epair=$(vnet_mkepair)
4667bd7933fSKristof Provost	ifconfig ${epair}a 192.0.2.1/24 up
4677bd7933fSKristof Provost
4687bd7933fSKristof Provost	vnet_mkjail alcatraz ${epair}b
4697bd7933fSKristof Provost	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
4707bd7933fSKristof Provost	jexec alcatraz pfctl -e
4717bd7933fSKristof Provost
4727bd7933fSKristof Provost	pft_set_rules alcatraz "block all" \
4737bd7933fSKristof Provost		"pass in proto icmp"
4747bd7933fSKristof Provost
4757bd7933fSKristof Provost	# Sanity check & establish state
4767bd7933fSKristof Provost	atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \
4777bd7933fSKristof Provost		--sendif ${epair}a \
4787bd7933fSKristof Provost		--to 192.0.2.2 \
4797bd7933fSKristof Provost		--replyif ${epair}a
4807bd7933fSKristof Provost
4817bd7933fSKristof Provost	# Change rules to now deny the ICMP traffic
4827bd7933fSKristof Provost	pft_set_rules noflush alcatraz "block all"
483*45258e1bSKristof Provost	if ! find_state;
484*45258e1bSKristof Provost	then
485*45258e1bSKristof Provost		atf_fail "Setting new rules removed the state."
486*45258e1bSKristof Provost	fi
4877bd7933fSKristof Provost
4887bd7933fSKristof Provost	# Flushing states on a different interface doesn't affect our state
4897bd7933fSKristof Provost	jexec alcatraz pfctl -i ${epair}a -Fs
490*45258e1bSKristof Provost	if ! find_state;
491*45258e1bSKristof Provost	then
492*45258e1bSKristof Provost		atf_fail "Flushing on a different interface removed the state."
493*45258e1bSKristof Provost	fi
4947bd7933fSKristof Provost
4957bd7933fSKristof Provost	# Flushing on the correct interface does (even with floating states)
4967bd7933fSKristof Provost	jexec alcatraz pfctl -i ${epair}b -Fs
497*45258e1bSKristof Provost	if find_state;
498*45258e1bSKristof Provost	then
499*45258e1bSKristof Provost		atf_fail "Flushing on a the interface did not remove the state."
500*45258e1bSKristof Provost	fi
5017bd7933fSKristof Provost}
5027bd7933fSKristof Provost
5037bd7933fSKristof Provostinterface_cleanup()
5047bd7933fSKristof Provost{
5057bd7933fSKristof Provost	pft_cleanup
5067bd7933fSKristof Provost}
5077bd7933fSKristof Provost
508bbf832f3SKristof Provostatf_test_case "id" "cleanup"
509bbf832f3SKristof Provostid_head()
510bbf832f3SKristof Provost{
511bbf832f3SKristof Provost	atf_set descr 'Test killing states by id'
512bbf832f3SKristof Provost	atf_set require.user root
513bbf832f3SKristof Provost	atf_set require.progs scapy
514bbf832f3SKristof Provost}
515bbf832f3SKristof Provost
516bbf832f3SKristof Provostid_body()
517bbf832f3SKristof Provost{
518bbf832f3SKristof Provost	pft_init
519bbf832f3SKristof Provost
520bbf832f3SKristof Provost	epair=$(vnet_mkepair)
521bbf832f3SKristof Provost	ifconfig ${epair}a 192.0.2.1/24 up
522bbf832f3SKristof Provost
523bbf832f3SKristof Provost	vnet_mkjail alcatraz ${epair}b
524bbf832f3SKristof Provost	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
525bbf832f3SKristof Provost	jexec alcatraz pfctl -e
526bbf832f3SKristof Provost
527bbf832f3SKristof Provost	pft_set_rules alcatraz "block all" \
528bbf832f3SKristof Provost		"pass in proto tcp" \
529bbf832f3SKristof Provost		"pass in proto icmp"
530bbf832f3SKristof Provost
531bbf832f3SKristof Provost	# Sanity check & establish state
532bbf832f3SKristof Provost	atf_check -s exit:0 -o ignore ${common_dir}/pft_ping.py \
533bbf832f3SKristof Provost		--sendif ${epair}a \
534bbf832f3SKristof Provost		--to 192.0.2.2 \
535bbf832f3SKristof Provost		--replyif ${epair}a
536bbf832f3SKristof Provost
537bbf832f3SKristof Provost	# Change rules to now deny the ICMP traffic
538bbf832f3SKristof Provost	pft_set_rules noflush alcatraz "block all"
539*45258e1bSKristof Provost	if ! find_state;
540*45258e1bSKristof Provost	then
541*45258e1bSKristof Provost		atf_fail "Setting new rules removed the state."
542*45258e1bSKristof Provost	fi
543bbf832f3SKristof Provost
544bbf832f3SKristof Provost	# Get the state ID
545bbf832f3SKristof Provost	id=$(jexec alcatraz pfctl -ss -vvv | grep -A 3 icmp |
546bbf832f3SKristof Provost	    grep -A 3 192.0.2.2 | awk '/id:/ { printf("%s/%s", $2, $4); }')
547bbf832f3SKristof Provost
548bbf832f3SKristof Provost	# Kill the wrong ID
549bbf832f3SKristof Provost	jexec alcatraz pfctl -k id -k 1
550*45258e1bSKristof Provost	if ! find_state;
551*45258e1bSKristof Provost	then
552*45258e1bSKristof Provost		atf_fail "Killing a different ID removed the state."
553*45258e1bSKristof Provost	fi
554bbf832f3SKristof Provost
555bbf832f3SKristof Provost	# Kill the correct ID
556bbf832f3SKristof Provost	jexec alcatraz pfctl -k id -k ${id}
557*45258e1bSKristof Provost	if find_state;
558*45258e1bSKristof Provost	then
559*45258e1bSKristof Provost		atf_fail "Killing the state did not remove it."
560*45258e1bSKristof Provost	fi
561bbf832f3SKristof Provost}
562bbf832f3SKristof Provost
563bbf832f3SKristof Provostid_cleanup()
564bbf832f3SKristof Provost{
565bbf832f3SKristof Provost	pft_cleanup
566bbf832f3SKristof Provost}
567bbf832f3SKristof Provost
568065b5c7fSKristof Provostatf_init_test_cases()
569065b5c7fSKristof Provost{
570065b5c7fSKristof Provost	atf_add_test_case "v4"
5719af23174SKristof Provost	atf_add_test_case "v6"
572065b5c7fSKristof Provost	atf_add_test_case "label"
5735632f585SKristof Provost	atf_add_test_case "multilabel"
574c2e11d81SKristof Provost	atf_add_test_case "gateway"
575ac200a9cSKristof Provost	atf_add_test_case "match"
5767bd7933fSKristof Provost	atf_add_test_case "interface"
577bbf832f3SKristof Provost	atf_add_test_case "id"
578065b5c7fSKristof Provost}
579