2 # SPDX-License-Identifier: BSD-2-Clause
4 # Copyright (c) 2020 Mark Johnston <markj@FreeBSD.org>
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 . $(atf_get_srcdir)/utils.subr
29 TABLE_STATS_ZERO_REGEXP='Packets: 0[[:space:]]*Bytes: 0[[:space:]]'
30 TABLE_STATS_NONZERO_REGEXP='Packets: [1-9][0-9]*[[:space:]]*Bytes: [1-9][0-9]*[[:space:]]'
32 atf_test_case "v4_counters" "cleanup"
35 atf_set descr 'Verify per-address counters for v4'
36 atf_set require.user root
43 epair_send=$(vnet_mkepair)
44 ifconfig ${epair_send}a 192.0.2.1/24 up
46 vnet_mkjail alcatraz ${epair_send}b
47 jexec alcatraz ifconfig ${epair_send}b 192.0.2.2/24 up
48 jexec alcatraz pfctl -e
50 pft_set_rules alcatraz \
51 "table <foo> counters { 192.0.2.1 }" \
53 "pass in from <foo> to any" \
54 "pass out from any to <foo>" \
57 atf_check -s exit:0 -o ignore ping -c 3 192.0.2.2
59 atf_check -s exit:0 -e ignore \
60 -o match:'In/Block:.*'"$TABLE_STATS_ZERO_REGEXP" \
61 -o match:'In/Pass:.*'"$TABLE_STATS_NONZERO_REGEXP" \
62 -o match:'Out/Block:.*'"$TABLE_STATS_ZERO_REGEXP" \
63 -o match:'Out/Pass:.*'"$TABLE_STATS_NONZERO_REGEXP" \
64 jexec alcatraz pfctl -t foo -T show -vv
72 atf_test_case "v6_counters" "cleanup"
75 atf_set descr 'Verify per-address counters for v6'
76 atf_set require.user root
83 epair_send=$(vnet_mkepair)
84 ifconfig ${epair_send}a inet6 2001:db8:42::1/64 up no_dad -ifdisabled
86 vnet_mkjail alcatraz ${epair_send}b
87 jexec alcatraz ifconfig ${epair_send}b inet6 2001:db8:42::2/64 up no_dad
88 jexec alcatraz pfctl -e
90 pft_set_rules alcatraz \
91 "table <foo6> counters { 2001:db8:42::1 }" \
93 "pass in from <foo6> to any" \
94 "pass out from any to <foo6>" \
97 atf_check -s exit:0 -o ignore ping -6 -c 3 2001:db8:42::2
99 atf_check -s exit:0 -e ignore \
100 -o match:'In/Block:.*'"$TABLE_STATS_ZERO_REGEXP" \
101 -o match:'In/Pass:.*'"$TABLE_STATS_NONZERO_REGEXP" \
102 -o match:'Out/Block:.*'"$TABLE_STATS_ZERO_REGEXP" \
103 -o match:'Out/Pass:.*'"$TABLE_STATS_NONZERO_REGEXP" \
104 jexec alcatraz pfctl -t foo6 -T show -vv
107 v6_counters_cleanup()
112 atf_test_case "pr251414" "cleanup"
115 atf_set descr 'Test PR 251414'
116 atf_set require.user root
123 epair_send=$(vnet_mkepair)
124 ifconfig ${epair_send}a 192.0.2.1/24 up
126 vnet_mkjail alcatraz ${epair_send}b
127 jexec alcatraz ifconfig ${epair_send}b 192.0.2.2/24 up
128 jexec alcatraz pfctl -e
130 pft_set_rules alcatraz \
132 "table <tab> { self }" \
133 "pass in log to <tab>"
135 pft_set_rules noflush alcatraz \
137 "table <tab> counters { self }" \
138 "pass in log to <tab>"
140 atf_check -s exit:0 -o ignore ping -c 3 192.0.2.2
142 jexec alcatraz pfctl -t tab -T show -vv
150 atf_test_case "automatic" "cleanup"
153 atf_set descr "Test automatic - optimizer generated - tables"
154 atf_set require.user root
161 epair=$(vnet_mkepair)
162 ifconfig ${epair}a 192.0.2.1/24 up
164 vnet_mkjail alcatraz ${epair}b
165 jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
166 jexec alcatraz pfctl -e
168 pft_set_rules alcatraz \
170 "pass in proto icmp from 192.0.2.1" \
171 "pass in proto icmp from 192.0.2.3" \
172 "pass in proto icmp from 192.0.2.4" \
173 "pass in proto icmp from 192.0.2.5" \
174 "pass in proto icmp from 192.0.2.6" \
175 "pass in proto icmp from 192.0.2.7" \
176 "pass in proto icmp from 192.0.2.8" \
177 "pass in proto icmp from 192.0.2.9"
179 atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
187 atf_test_case "network" "cleanup"
190 atf_set descr 'Test <ifgroup>:network'
191 atf_set require.user root
198 epair=$(vnet_mkepair)
199 ifconfig ${epair}a 192.0.2.1/24 up
201 vnet_mkjail alcatraz ${epair}b
202 jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
203 jexec alcatraz pfctl -e
205 pft_set_rules alcatraz \
206 "table <allow> const { epair:network }"\
208 "pass in from <allow>"
210 atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
218 atf_test_case "pr259689" "cleanup"
221 atf_set descr 'Test PR 259689'
222 atf_set require.user root
230 jexec alcatraz pfctl -e
232 pft_set_rules alcatraz \
234 "block in inet from { 1.1.1.1, 1.1.1.2, 2.2.2.2, 2.2.2.3, 4.4.4.4, 4.4.4.5 }"
236 atf_check -o match:'block drop in inet from <__automatic_.*:6> to any' \
238 jexec alcatraz pfctl -sr -vv
246 atf_test_case "precreate" "cleanup"
249 atf_set descr 'Test creating a table without counters, then loading rules that add counters'
250 atf_set require.user root
259 jexec alcatraz pfctl -t foo -T add 192.0.2.1
260 jexec alcatraz pfctl -t foo -T show
262 pft_set_rules noflush alcatraz \
263 "table <foo> counters persist" \
266 # Expect all counters to be zero
267 atf_check -s exit:0 -e ignore \
268 -o match:'In/Block:.*'"$TABLE_STATS_ZERO_REGEXP" \
269 -o match:'In/Pass:.*'"$TABLE_STATS_ZERO_REGEXP" \
270 -o match:'Out/Block:.*'"$TABLE_STATS_ZERO_REGEXP" \
271 -o match:'Out/Pass:.*'"$TABLE_STATS_ZERO_REGEXP" \
272 jexec alcatraz pfctl -t foo -T show -vv
281 atf_test_case "anchor" "cleanup"
284 atf_set descr 'Test tables in anchors'
285 atf_set require.user root
292 epair=$(vnet_mkepair)
293 ifconfig ${epair}a 192.0.2.1/24 up
295 vnet_mkjail alcatraz ${epair}b
296 jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
297 jexec alcatraz pfctl -e
299 (echo "table <testtable> persist"
300 echo "block in quick from <testtable> to any"
301 ) | jexec alcatraz pfctl -a anchorage -f -
303 pft_set_rules noflush alcatraz \
307 atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
309 # Tables belong to anchors, so this is a different table and won't affect anything
310 jexec alcatraz pfctl -t testtable -T add 192.0.2.1
311 atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
313 # But when we add the address to the table in the anchor it does block traffic
314 jexec alcatraz pfctl -a anchorage -t testtable -T add 192.0.2.1
315 atf_check -s exit:2 -o ignore ping -c 1 192.0.2.2
323 atf_init_test_cases()
325 atf_add_test_case "v4_counters"
326 atf_add_test_case "v6_counters"
327 atf_add_test_case "pr251414"
328 atf_add_test_case "automatic"
329 atf_add_test_case "network"
330 atf_add_test_case "pr259689"
331 atf_add_test_case "precreate"
332 atf_add_test_case "anchor"