xref: /freebsd/tests/sys/netpfil/pf/rules_counter.sh (revision 025df3e6a0c3a4477dd55153543f395bc018dd37)
1112f007eSKristof Provost#
24d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
3112f007eSKristof Provost#
4112f007eSKristof Provost# Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
5112f007eSKristof Provost#
6112f007eSKristof Provost# Redistribution and use in source and binary forms, with or without
7112f007eSKristof Provost# modification, are permitted provided that the following conditions
8112f007eSKristof Provost# are met:
9112f007eSKristof Provost# 1. Redistributions of source code must retain the above copyright
10112f007eSKristof Provost#    notice, this list of conditions and the following disclaimer.
11112f007eSKristof Provost# 2. Redistributions in binary form must reproduce the above copyright
12112f007eSKristof Provost#    notice, this list of conditions and the following disclaimer in the
13112f007eSKristof Provost#    documentation and/or other materials provided with the distribution.
14112f007eSKristof Provost#
15112f007eSKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16112f007eSKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17112f007eSKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18112f007eSKristof Provost# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19112f007eSKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20112f007eSKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21112f007eSKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22112f007eSKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23112f007eSKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24112f007eSKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25112f007eSKristof Provost# SUCH DAMAGE.
26112f007eSKristof Provost
27112f007eSKristof Provost. $(atf_get_srcdir)/utils.subr
28112f007eSKristof Provost
29112f007eSKristof Provostatf_test_case "get_clear" "cleanup"
30112f007eSKristof Provostget_clear_head()
31112f007eSKristof Provost{
32112f007eSKristof Provost	atf_set descr 'Test clearing rules counters on get rules'
33112f007eSKristof Provost	atf_set require.user root
34112f007eSKristof Provost}
35112f007eSKristof Provost
36112f007eSKristof Provostget_clear_body()
37112f007eSKristof Provost{
38112f007eSKristof Provost	pft_init
39112f007eSKristof Provost
40112f007eSKristof Provost	epair_send=$(vnet_mkepair)
41112f007eSKristof Provost	ifconfig ${epair_send}a 192.0.2.1/24 up
42112f007eSKristof Provost
43112f007eSKristof Provost	vnet_mkjail alcatraz ${epair_send}b
44112f007eSKristof Provost	jexec alcatraz ifconfig ${epair_send}b 192.0.2.2/24 up
45112f007eSKristof Provost	jexec alcatraz pfctl -e
46112f007eSKristof Provost
47112f007eSKristof Provost	pft_set_rules alcatraz \
48112f007eSKristof Provost		"pass all"
49112f007eSKristof Provost
50112f007eSKristof Provost	# Ensure the rule matched packets, so we can verify non-zero counters
51112f007eSKristof Provost	atf_check -s exit:0 -o ignore ping -c 3 192.0.2.2
52112f007eSKristof Provost
53112f007eSKristof Provost	# Expect non-zero counters
54112f007eSKristof Provost	atf_check -s exit:0 -e ignore \
55112f007eSKristof Provost	    -o match:'Evaluations: [1-9][0-9]*[[:space:]]*Packets: [1-9][0-9]*[[:space:]]*Bytes: [1-9][0-9]*[[:space:]]*' \
56112f007eSKristof Provost	    jexec alcatraz pfctl -s r -v
57112f007eSKristof Provost
58112f007eSKristof Provost	# We should still see non-zero because we didn't clear on the last
59112f007eSKristof Provost	# pfctl, but are going to clear now
60112f007eSKristof Provost	atf_check -s exit:0 -e ignore \
61112f007eSKristof Provost	    -o match:'Evaluations: [1-9][0-9]*[[:space:]]*Packets: [1-9][0-9]*[[:space:]]*Bytes: [1-9][0-9]*[[:space:]]*' \
62112f007eSKristof Provost	    jexec alcatraz pfctl -s r -v -z
63112f007eSKristof Provost
64112f007eSKristof Provost	# Expect zero counters
65112f007eSKristof Provost	atf_check -s exit:0 -e ignore \
66112f007eSKristof Provost	    -o match:'Evaluations: 0[[:space:]]*Packets: 0*[[:space:]]*Bytes: 0*[[:space:]]*' \
67112f007eSKristof Provost	    jexec alcatraz pfctl -s r -v
68112f007eSKristof Provost}
69112f007eSKristof Provost
70112f007eSKristof Provostget_clear_cleanup()
71112f007eSKristof Provost{
72112f007eSKristof Provost	pft_cleanup
73112f007eSKristof Provost}
74112f007eSKristof Provost
75e14d56f3SKristof Provostatf_test_case "keepcounters" "cleanup"
76e14d56f3SKristof Provostkeepcounters_head()
77e14d56f3SKristof Provost{
78e14d56f3SKristof Provost	atf_set descr 'Test keepcounter functionality'
79e14d56f3SKristof Provost	atf_set require.user root
80e14d56f3SKristof Provost}
81e14d56f3SKristof Provost
82e14d56f3SKristof Provostkeepcounters_body()
83e14d56f3SKristof Provost{
84e14d56f3SKristof Provost	pft_init
85e14d56f3SKristof Provost
86e14d56f3SKristof Provost	epair_send=$(vnet_mkepair)
87e14d56f3SKristof Provost	ifconfig ${epair_send}a 192.0.2.1/24 up
88e14d56f3SKristof Provost
89e14d56f3SKristof Provost	vnet_mkjail alcatraz ${epair_send}b
90e14d56f3SKristof Provost	jexec alcatraz ifconfig ${epair_send}b 192.0.2.2/24 up
91e14d56f3SKristof Provost	jexec alcatraz pfctl -e
92e14d56f3SKristof Provost
93e14d56f3SKristof Provost	pft_set_rules alcatraz \
94e14d56f3SKristof Provost		"pass all"
95e14d56f3SKristof Provost
96e14d56f3SKristof Provost	# Expect zero counters
97e14d56f3SKristof Provost	atf_check -s exit:0 -e ignore \
98e14d56f3SKristof Provost	    -o match:'Evaluations: 0[[:space:]]*Packets: 0*[[:space:]]*Bytes: 0*[[:space:]]*' \
99e14d56f3SKristof Provost	    jexec alcatraz pfctl -s r -v
100e14d56f3SKristof Provost
101e14d56f3SKristof Provost	# Ensure the rule matched packets, so we can verify non-zero counters
102e14d56f3SKristof Provost	atf_check -s exit:0 -o ignore ping -c 3 192.0.2.2
103e14d56f3SKristof Provost
104e14d56f3SKristof Provost	# Expect non-zero counters
105e14d56f3SKristof Provost	atf_check -s exit:0 -e ignore \
106e14d56f3SKristof Provost	    -o match:'Evaluations: [1-9][0-9]*[[:space:]]*Packets: [1-9][0-9]*[[:space:]]*Bytes: [1-9][0-9]*[[:space:]]*' \
107e14d56f3SKristof Provost	    jexec alcatraz pfctl -s r -v
108e14d56f3SKristof Provost
109e14d56f3SKristof Provost	# As we set the (same) rules again we'd expect the counters to return
110e14d56f3SKristof Provost	# to zero
111e14d56f3SKristof Provost	pft_set_rules noflush alcatraz \
112e14d56f3SKristof Provost		"pass all"
113e14d56f3SKristof Provost
114e14d56f3SKristof Provost	atf_check -s exit:0 -e ignore \
115e14d56f3SKristof Provost	    -o match:'Evaluations: 0[[:space:]]*Packets: 0*[[:space:]]*Bytes: 0*[[:space:]]*' \
116e14d56f3SKristof Provost	    jexec alcatraz pfctl -s r -v
117e14d56f3SKristof Provost
118e14d56f3SKristof Provost	# Increment rule counters
119e14d56f3SKristof Provost	atf_check -s exit:0 -o ignore ping -c 3 192.0.2.2
120e14d56f3SKristof Provost
121e14d56f3SKristof Provost	# Now set new rules with 'keepcounters' set, so we'd expect nonzero
122e14d56f3SKristof Provost	# counters
123e14d56f3SKristof Provost	pft_set_rules noflush alcatraz \
124e14d56f3SKristof Provost		"set keepcounters" \
125e14d56f3SKristof Provost		"pass all"
126e14d56f3SKristof Provost
127e14d56f3SKristof Provost	atf_check -s exit:0 -e ignore \
128e14d56f3SKristof Provost	    -o match:'Evaluations: [1-9][0-9]*[[:space:]]*Packets: [1-9][0-9]*[[:space:]]*Bytes: [1-9][0-9]*[[:space:]]*' \
129e14d56f3SKristof Provost	    jexec alcatraz pfctl -s r -v
130e14d56f3SKristof Provost
131e14d56f3SKristof Provost	# However, if we set a different rule it should return to zero
132e14d56f3SKristof Provost	pft_set_rules noflush alcatraz \
133e14d56f3SKristof Provost		"set keepcounters" \
134e14d56f3SKristof Provost		"pass inet all"
135e14d56f3SKristof Provost
136e14d56f3SKristof Provost	atf_check -s exit:0 -e ignore \
137e14d56f3SKristof Provost	    -o match:'Evaluations: 0[[:space:]]*Packets: 0*[[:space:]]*Bytes: 0*[[:space:]]*' \
138e14d56f3SKristof Provost	    jexec alcatraz pfctl -s r -v
139e14d56f3SKristof Provost
140e14d56f3SKristof Provost	# If we generate traffic and don't set keepcounters we also see zero
141e14d56f3SKristof Provost	# counts when setting new rules
142e14d56f3SKristof Provost	atf_check -s exit:0 -o ignore ping -c 3 192.0.2.2
143e14d56f3SKristof Provost	pft_set_rules noflush alcatraz \
144e14d56f3SKristof Provost		"pass inet all"
145e14d56f3SKristof Provost
146e14d56f3SKristof Provost	atf_check -s exit:0 -e ignore \
147e14d56f3SKristof Provost	    -o match:'Evaluations: 0[[:space:]]*Packets: 0*[[:space:]]*Bytes: 0*[[:space:]]*' \
148e14d56f3SKristof Provost	    jexec alcatraz pfctl -s r -v
149e14d56f3SKristof Provost}
150e14d56f3SKristof Provost
151*025df3e6SKristof Provostatf_test_case "4G" "cleanup"
152*025df3e6SKristof Provost4G_head()
153*025df3e6SKristof Provost{
154*025df3e6SKristof Provost	atf_set descr 'Test keepcounter for values above 32 bits'
155*025df3e6SKristof Provost	atf_set require.user root
156*025df3e6SKristof Provost}
157*025df3e6SKristof Provost
158*025df3e6SKristof Provost4G_body()
159*025df3e6SKristof Provost{
160*025df3e6SKristof Provost	pft_init
161*025df3e6SKristof Provost
162*025df3e6SKristof Provost	epair=$(vnet_mkepair)
163*025df3e6SKristof Provost	ifconfig ${epair}a 192.0.2.1/24 up
164*025df3e6SKristof Provost
165*025df3e6SKristof Provost	vnet_mkjail alcatraz ${epair}b
166*025df3e6SKristof Provost	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
167*025df3e6SKristof Provost	jexec alcatraz nc -l 1234 >/dev/null &
168*025df3e6SKristof Provost
169*025df3e6SKristof Provost	jexec alcatraz pfctl -e
170*025df3e6SKristof Provost	pft_set_rules alcatraz \
171*025df3e6SKristof Provost		"pass all"
172*025df3e6SKristof Provost
173*025df3e6SKristof Provost	# Now pass more than 4GB of data
174*025df3e6SKristof Provost	dd if=/dev/zero bs=1k count=4M | nc -N 192.0.2.2 1234
175*025df3e6SKristof Provost
176*025df3e6SKristof Provost	bytes=$(jexec alcatraz pfctl -s r -v | awk '/Bytes:/ { print $7; }')
177*025df3e6SKristof Provost	if [ $bytes -lt 4000000000 ];
178*025df3e6SKristof Provost	then
179*025df3e6SKristof Provost		atf_fail "Expected to see > 4GB"
180*025df3e6SKristof Provost	fi
181*025df3e6SKristof Provost
182*025df3e6SKristof Provost	# Set new rules, keeping counters
183*025df3e6SKristof Provost	pft_set_rules noflush alcatraz \
184*025df3e6SKristof Provost		"set keepcounters" \
185*025df3e6SKristof Provost		"pass all"
186*025df3e6SKristof Provost
187*025df3e6SKristof Provost	bytes=$(jexec alcatraz pfctl -s r -v | awk '/Bytes:/ { print $7; }')
188*025df3e6SKristof Provost	if [ $bytes -lt 4000000000 ];
189*025df3e6SKristof Provost	then
190*025df3e6SKristof Provost		atf_fail "Expected to see > 4GB after rule reload"
191*025df3e6SKristof Provost	fi
192*025df3e6SKristof Provost}
193*025df3e6SKristof Provost
194*025df3e6SKristof Provost4G_cleanup()
195*025df3e6SKristof Provost{
196*025df3e6SKristof Provost	pft_cleanup
197*025df3e6SKristof Provost}
198*025df3e6SKristof Provost
199e14d56f3SKristof Provostkeepcounters_cleanup()
200e14d56f3SKristof Provost{
201e14d56f3SKristof Provost	pft_cleanup
202e14d56f3SKristof Provost}
203e14d56f3SKristof Provost
204112f007eSKristof Provostatf_init_test_cases()
205112f007eSKristof Provost{
206112f007eSKristof Provost	atf_add_test_case "get_clear"
207e14d56f3SKristof Provost	atf_add_test_case "keepcounters"
208*025df3e6SKristof Provost	atf_add_test_case "4G"
209112f007eSKristof Provost}
210