xref: /freebsd/tests/sys/netpfil/pf/once.sh (revision 2c53e251601c1fcd558f42a16f6030ba75b7e8df)
1c913959eSKristof Provost#
2c913959eSKristof Provost# SPDX-License-Identifier: BSD-2-Clause
3c913959eSKristof Provost#
4c913959eSKristof Provost# Copyright (c) 2025 Rubicon Communications, LLC (Netgate)
5c913959eSKristof Provost#
6c913959eSKristof Provost# Redistribution and use in source and binary forms, with or without
7c913959eSKristof Provost# modification, are permitted provided that the following conditions
8c913959eSKristof Provost# are met:
9c913959eSKristof Provost# 1. Redistributions of source code must retain the above copyright
10c913959eSKristof Provost#    notice, this list of conditions and the following disclaimer.
11c913959eSKristof Provost# 2. Redistributions in binary form must reproduce the above copyright
12c913959eSKristof Provost#    notice, this list of conditions and the following disclaimer in the
13c913959eSKristof Provost#    documentation and/or other materials provided with the distribution.
14c913959eSKristof Provost#
15c913959eSKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c913959eSKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c913959eSKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c913959eSKristof Provost# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c913959eSKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c913959eSKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c913959eSKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c913959eSKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c913959eSKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c913959eSKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c913959eSKristof Provost# SUCH DAMAGE.
26c913959eSKristof Provost
27c913959eSKristof Provost. $(atf_get_srcdir)/utils.subr
28c913959eSKristof Provost
29c913959eSKristof Provostatf_test_case "basic" "cleanup"
30c913959eSKristof Provostbasic_head()
31c913959eSKristof Provost{
32c913959eSKristof Provost	atf_set descr 'Basic one shot rule test'
33c913959eSKristof Provost	atf_set require.user root
34c913959eSKristof Provost}
35c913959eSKristof Provost
36c913959eSKristof Provostbasic_body()
37c913959eSKristof Provost{
38c913959eSKristof Provost	pft_init
39c913959eSKristof Provost
40c913959eSKristof Provost	epair=$(vnet_mkepair)
41c913959eSKristof Provost
42c913959eSKristof Provost	vnet_mkjail alcatraz ${epair}a
43c913959eSKristof Provost	jexec alcatraz ifconfig ${epair}a 192.0.2.1/24 up
44c913959eSKristof Provost
45c913959eSKristof Provost	ifconfig ${epair}b 192.0.2.2/24 up
46c913959eSKristof Provost
47c913959eSKristof Provost	# Sanity checks
48c913959eSKristof Provost	atf_check -s exit:0 -o ignore \
49c913959eSKristof Provost	    ping -c 1 192.0.2.1
50c913959eSKristof Provost
51c913959eSKristof Provost	jexec alcatraz pfctl -e
52c913959eSKristof Provost	pft_set_rules alcatraz \
53c913959eSKristof Provost	    "block" \
54c913959eSKristof Provost	    "pass in from 192.0.2.2 once"
55c913959eSKristof Provost
56c913959eSKristof Provost	# First once succeeds
57c913959eSKristof Provost	atf_check -s exit:0 -o ignore \
58c913959eSKristof Provost	    ping -c 3 192.0.2.1
59c913959eSKristof Provost
60c913959eSKristof Provost	# Check for '# expired'
61c913959eSKristof Provost	atf_check -s exit:0 -e ignore \
62c913959eSKristof Provost	    -o match:'pass in inet from 192.0.2.2 to any flags S/SA keep state once # expired' \
63c913959eSKristof Provost	    jexec alcatraz pfctl -sr -vv
64c913959eSKristof Provost
65c913959eSKristof Provost	# The second one does not
66c913959eSKristof Provost	atf_check -s exit:2 -o ignore \
67c913959eSKristof Provost	    ping -c 3 192.0.2.1
68c913959eSKristof Provost
69c913959eSKristof Provost	# Flush states, still shouldn't work
70c913959eSKristof Provost	jexec alcatraz pfctl -Fs
71c913959eSKristof Provost	atf_check -s exit:2 -o ignore \
72c913959eSKristof Provost	    ping -c 3 192.0.2.1
73c913959eSKristof Provost}
74c913959eSKristof Provost
75c913959eSKristof Provostbasic_cleanup()
76c913959eSKristof Provost{
77c913959eSKristof Provost	pft_cleanup
78c913959eSKristof Provost}
79c913959eSKristof Provost
80*2c53e251SKristof Provostatf_test_case "anchor" "cleanup"
81*2c53e251SKristof Provostanchor_head()
82*2c53e251SKristof Provost{
83*2c53e251SKristof Provost	atf_set descr 'Test one shot rule in anchors'
84*2c53e251SKristof Provost	atf_set require.user root
85*2c53e251SKristof Provost}
86*2c53e251SKristof Provost
87*2c53e251SKristof Provostanchor_body()
88*2c53e251SKristof Provost{
89*2c53e251SKristof Provost	pft_init
90*2c53e251SKristof Provost	epair=$(vnet_mkepair)
91*2c53e251SKristof Provost
92*2c53e251SKristof Provost	vnet_mkjail alcatraz ${epair}a
93*2c53e251SKristof Provost	jexec alcatraz ifconfig ${epair}a 192.0.2.1/24 up
94*2c53e251SKristof Provost
95*2c53e251SKristof Provost	ifconfig ${epair}b 192.0.2.2/24 up
96*2c53e251SKristof Provost
97*2c53e251SKristof Provost	# Sanity checks
98*2c53e251SKristof Provost	atf_check -s exit:0 -o ignore \
99*2c53e251SKristof Provost	    ping -c 1 192.0.2.1
100*2c53e251SKristof Provost
101*2c53e251SKristof Provost	jexec alcatraz pfctl -e
102*2c53e251SKristof Provost	pft_set_rules alcatraz \
103*2c53e251SKristof Provost	    "block" \
104*2c53e251SKristof Provost	    "anchor \"once\" {\n
105*2c53e251SKristof Provost	        pass in from 192.0.2.2 once\n
106*2c53e251SKristof Provost	    }"
107*2c53e251SKristof Provost
108*2c53e251SKristof Provost	# First once succeeds
109*2c53e251SKristof Provost	atf_check -s exit:0 -o ignore \
110*2c53e251SKristof Provost	    ping -c 3 192.0.2.1
111*2c53e251SKristof Provost
112*2c53e251SKristof Provost	# Check for '# expired'
113*2c53e251SKristof Provost	jexec alcatraz pfctl -sr -vv -a "*"
114*2c53e251SKristof Provost	atf_check -s exit:0 -e ignore \
115*2c53e251SKristof Provost	    -o match:'pass in inet from 192.0.2.2 to any flags S/SA keep state once # expired' \
116*2c53e251SKristof Provost	    jexec alcatraz pfctl -sr -vv -a "*"
117*2c53e251SKristof Provost
118*2c53e251SKristof Provost	# The second one does not
119*2c53e251SKristof Provost	atf_check -s exit:2 -o ignore \
120*2c53e251SKristof Provost	    ping -c 3 192.0.2.1
121*2c53e251SKristof Provost}
122*2c53e251SKristof Provost
123*2c53e251SKristof Provostanchor_cleanup()
124*2c53e251SKristof Provost{
125*2c53e251SKristof Provost	pft_cleanup
126*2c53e251SKristof Provost}
127*2c53e251SKristof Provost
128c913959eSKristof Provostatf_init_test_cases()
129c913959eSKristof Provost{
130c913959eSKristof Provost	atf_add_test_case "basic"
131*2c53e251SKristof Provost	atf_add_test_case "anchor"
132c913959eSKristof Provost}
133