xref: /freebsd/tests/sys/netpfil/pf/match.sh (revision 7e70d94acd68b3ac6b45f49d4ab7a0f7867c3ea7)
1ab1868a7SKristof Provost#
24d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
3ab1868a7SKristof Provost#
4ab1868a7SKristof Provost# Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
5ab1868a7SKristof Provost#
6ab1868a7SKristof Provost# Redistribution and use in source and binary forms, with or without
7ab1868a7SKristof Provost# modification, are permitted provided that the following conditions
8ab1868a7SKristof Provost# are met:
9ab1868a7SKristof Provost# 1. Redistributions of source code must retain the above copyright
10ab1868a7SKristof Provost#    notice, this list of conditions and the following disclaimer.
11ab1868a7SKristof Provost# 2. Redistributions in binary form must reproduce the above copyright
12ab1868a7SKristof Provost#    notice, this list of conditions and the following disclaimer in the
13ab1868a7SKristof Provost#    documentation and/or other materials provided with the distribution.
14ab1868a7SKristof Provost#
15ab1868a7SKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16ab1868a7SKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17ab1868a7SKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ab1868a7SKristof Provost# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ab1868a7SKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ab1868a7SKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ab1868a7SKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ab1868a7SKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ab1868a7SKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ab1868a7SKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ab1868a7SKristof Provost# SUCH DAMAGE.
26ab1868a7SKristof Provost
27ab1868a7SKristof Provost. $(atf_get_srcdir)/utils.subr
28ab1868a7SKristof Provost
29*7e70d94aSKristof Provostcommon_dir=$(atf_get_srcdir)/../common
30*7e70d94aSKristof Provost
31ab1868a7SKristof Provostatf_test_case "dummynet" "cleanup"
32ab1868a7SKristof Provostdummynet_head()
33ab1868a7SKristof Provost{
34ab1868a7SKristof Provost	atf_set descr 'Test dummynet with match keyword'
35ab1868a7SKristof Provost	atf_set require.user root
36ab1868a7SKristof Provost}
37ab1868a7SKristof Provost
38ab1868a7SKristof Provostdummynet_body()
39ab1868a7SKristof Provost{
401977d9a3SKristof Provost	dummynet_init
41ab1868a7SKristof Provost
42ab1868a7SKristof Provost	epair=$(vnet_mkepair)
43ab1868a7SKristof Provost	vnet_mkjail alcatraz ${epair}b
44ab1868a7SKristof Provost
45ab1868a7SKristof Provost	ifconfig ${epair}a 192.0.2.1/24 up
46ab1868a7SKristof Provost	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
47ab1868a7SKristof Provost
48ab1868a7SKristof Provost	# Sanity check
49ab1868a7SKristof Provost	atf_check -s exit:0 -o ignore ping -i .1 -c 3 -s 1200 192.0.2.2
50ab1868a7SKristof Provost
51ab1868a7SKristof Provost	jexec alcatraz dnctl pipe 1 config bw 30Byte/s
52ab1868a7SKristof Provost	jexec alcatraz pfctl -e
53ab1868a7SKristof Provost	pft_set_rules alcatraz \
54ab1868a7SKristof Provost		"match in dnpipe 1" \
55ab1868a7SKristof Provost		"pass"
56ab1868a7SKristof Provost
57ab1868a7SKristof Provost	# single ping succeeds just fine
58ab1868a7SKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
59ab1868a7SKristof Provost
60ab1868a7SKristof Provost	# Saturate the link
61ab1868a7SKristof Provost	ping -i .1 -c 5 -s 1200 192.0.2.2
62ab1868a7SKristof Provost
63ab1868a7SKristof Provost	# We should now be hitting the limits and get this packet dropped.
64ab1868a7SKristof Provost	atf_check -s exit:2 -o ignore ping -c 1 -s 1200 192.0.2.2
65ab1868a7SKristof Provost}
66ab1868a7SKristof Provost
67ab1868a7SKristof Provostdummynet_cleanup()
68ab1868a7SKristof Provost{
69ab1868a7SKristof Provost	pft_cleanup
70ab1868a7SKristof Provost}
71ab1868a7SKristof Provost
7290ef7a0bSKristof Provostatf_test_case "quick" "cleanup"
7390ef7a0bSKristof Provostquick_head()
7490ef7a0bSKristof Provost{
7590ef7a0bSKristof Provost	atf_set descr 'Test quick on match rules'
7690ef7a0bSKristof Provost	atf_set require.user root
7790ef7a0bSKristof Provost}
7890ef7a0bSKristof Provost
7990ef7a0bSKristof Provostquick_body()
8090ef7a0bSKristof Provost{
8190ef7a0bSKristof Provost	pft_init
8290ef7a0bSKristof Provost
8390ef7a0bSKristof Provost	epair=$(vnet_mkepair)
8490ef7a0bSKristof Provost	vnet_mkjail alcatraz ${epair}b
8590ef7a0bSKristof Provost
8690ef7a0bSKristof Provost	ifconfig ${epair}a 192.0.2.1/24 up
8790ef7a0bSKristof Provost	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
8890ef7a0bSKristof Provost
8990ef7a0bSKristof Provost	# Sanity check
9090ef7a0bSKristof Provost	atf_check -s exit:0 -o ignore \
9190ef7a0bSKristof Provost	    ping -c 1 192.0.2.2
9290ef7a0bSKristof Provost
9390ef7a0bSKristof Provost	jexec alcatraz pfctl -e
9490ef7a0bSKristof Provost	pft_set_rules alcatraz \
9590ef7a0bSKristof Provost		"pass" \
9690ef7a0bSKristof Provost		"match in quick proto icmp" \
9790ef7a0bSKristof Provost		"block"
9890ef7a0bSKristof Provost
9990ef7a0bSKristof Provost	# 'match quick' should retain the previous pass/block state
10090ef7a0bSKristof Provost	atf_check -s exit:0 -o ignore \
10190ef7a0bSKristof Provost	    ping -c 1 192.0.2.2
10290ef7a0bSKristof Provost
10390ef7a0bSKristof Provost	pft_set_rules alcatraz \
10490ef7a0bSKristof Provost		"block" \
10590ef7a0bSKristof Provost		"match in quick proto icmp" \
10690ef7a0bSKristof Provost		"pass"
10790ef7a0bSKristof Provost
10890ef7a0bSKristof Provost	atf_check -s exit:2 -o ignore \
10990ef7a0bSKristof Provost	    ping -c 1 192.0.2.2
11090ef7a0bSKristof Provost}
11190ef7a0bSKristof Provost
11290ef7a0bSKristof Provostquick_cleanup()
11390ef7a0bSKristof Provost{
11490ef7a0bSKristof Provost	pft_cleanup
11590ef7a0bSKristof Provost}
11690ef7a0bSKristof Provost
117*7e70d94aSKristof Provostatf_test_case "allow_opts" "cleanup"
118*7e70d94aSKristof Provostallow_opts_head()
119*7e70d94aSKristof Provost{
120*7e70d94aSKristof Provost	atf_set descr 'Test allowing IP options via match'
121*7e70d94aSKristof Provost	atf_set require.user root
122*7e70d94aSKristof Provost	atf_set require.progs python3 scapy
123*7e70d94aSKristof Provost}
124*7e70d94aSKristof Provost
125*7e70d94aSKristof Provostallow_opts_body()
126*7e70d94aSKristof Provost{
127*7e70d94aSKristof Provost	pft_init
128*7e70d94aSKristof Provost
129*7e70d94aSKristof Provost	epair=$(vnet_mkepair)
130*7e70d94aSKristof Provost
131*7e70d94aSKristof Provost	ifconfig ${epair}b 192.0.2.2/24 up
132*7e70d94aSKristof Provost
133*7e70d94aSKristof Provost	vnet_mkjail alcatraz ${epair}a
134*7e70d94aSKristof Provost	jexec alcatraz ifconfig ${epair}a 192.0.2.1/24 up
135*7e70d94aSKristof Provost
136*7e70d94aSKristof Provost	jexec alcatraz pfctl -e
137*7e70d94aSKristof Provost	jexec alcatraz pfctl -x loud
138*7e70d94aSKristof Provost	pft_set_rules alcatraz \
139*7e70d94aSKristof Provost	    "match proto icmp allow-opts" \
140*7e70d94aSKristof Provost	    "pass"
141*7e70d94aSKristof Provost
142*7e70d94aSKristof Provost	# Sanity check
143*7e70d94aSKristof Provost	atf_check -s exit:0 -o ignore \
144*7e70d94aSKristof Provost	    ping -c 1 192.0.2.1
145*7e70d94aSKristof Provost
146*7e70d94aSKristof Provost	atf_check -s exit:0 -o ignore \
147*7e70d94aSKristof Provost	    ${common_dir}/pft_ping.py  \
148*7e70d94aSKristof Provost	    --sendif ${epair}b \
149*7e70d94aSKristof Provost	    --to 192.0.2.1 \
150*7e70d94aSKristof Provost	    --send-nop \
151*7e70d94aSKristof Provost	    --replyif ${epair}b
152*7e70d94aSKristof Provost
153*7e70d94aSKristof Provost	# This doesn't work without 'allow-opts'
154*7e70d94aSKristof Provost	pft_set_rules alcatraz \
155*7e70d94aSKristof Provost	    "match proto icmp" \
156*7e70d94aSKristof Provost	    "pass"
157*7e70d94aSKristof Provost	atf_check -s exit:1 -o ignore \
158*7e70d94aSKristof Provost	    ${common_dir}/pft_ping.py  \
159*7e70d94aSKristof Provost	    --sendif ${epair}b \
160*7e70d94aSKristof Provost	    --to 192.0.2.1 \
161*7e70d94aSKristof Provost	    --send-nop \
162*7e70d94aSKristof Provost	    --replyif ${epair}b
163*7e70d94aSKristof Provost
164*7e70d94aSKristof Provost	# Setting it on a pass rule still works.
165*7e70d94aSKristof Provost	pft_set_rules alcatraz \
166*7e70d94aSKristof Provost	    "pass allow-opts"
167*7e70d94aSKristof Provost	atf_check -s exit:0 -o ignore \
168*7e70d94aSKristof Provost	    ${common_dir}/pft_ping.py  \
169*7e70d94aSKristof Provost	    --sendif ${epair}b \
170*7e70d94aSKristof Provost	    --to 192.0.2.1 \
171*7e70d94aSKristof Provost	    --send-nop \
172*7e70d94aSKristof Provost	    --replyif ${epair}b
173*7e70d94aSKristof Provost}
174*7e70d94aSKristof Provost
175*7e70d94aSKristof Provostallow_opts_cleanup()
176*7e70d94aSKristof Provost{
177*7e70d94aSKristof Provost	pft_cleanup
178*7e70d94aSKristof Provost}
179*7e70d94aSKristof Provost
180ab1868a7SKristof Provostatf_init_test_cases()
181ab1868a7SKristof Provost{
182ab1868a7SKristof Provost	atf_add_test_case "dummynet"
18390ef7a0bSKristof Provost	atf_add_test_case "quick"
184*7e70d94aSKristof Provost	atf_add_test_case "allow_opts"
185ab1868a7SKristof Provost}
186