xref: /freebsd/tests/sys/netpfil/pf/anchor.sh (revision 5eee34fa0351f1fdb240f7d085145c05e9ed95d5)
165d553b0SKristof Provost#
24d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
365d553b0SKristof Provost#
465d553b0SKristof Provost# Copyright (c) 2018 Kristof Provost <kp@FreeBSD.org>
565d553b0SKristof Provost#
665d553b0SKristof Provost# Redistribution and use in source and binary forms, with or without
765d553b0SKristof Provost# modification, are permitted provided that the following conditions
865d553b0SKristof Provost# are met:
965d553b0SKristof Provost# 1. Redistributions of source code must retain the above copyright
1065d553b0SKristof Provost#    notice, this list of conditions and the following disclaimer.
1165d553b0SKristof Provost# 2. Redistributions in binary form must reproduce the above copyright
1265d553b0SKristof Provost#    notice, this list of conditions and the following disclaimer in the
1365d553b0SKristof Provost#    documentation and/or other materials provided with the distribution.
1465d553b0SKristof Provost#
1565d553b0SKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1665d553b0SKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1765d553b0SKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1865d553b0SKristof Provost# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1965d553b0SKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2065d553b0SKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2165d553b0SKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2265d553b0SKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2365d553b0SKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2465d553b0SKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2565d553b0SKristof Provost# SUCH DAMAGE.
2655177f18SKristof Provost
2755177f18SKristof Provost. $(atf_get_srcdir)/utils.subr
2855177f18SKristof Provost
2955177f18SKristof Provostatf_test_case "pr183198" "cleanup"
3055177f18SKristof Provostpr183198_head()
3155177f18SKristof Provost{
3255177f18SKristof Provost	atf_set descr 'Test tables referenced by rules in anchors'
3355177f18SKristof Provost	atf_set require.user root
3455177f18SKristof Provost}
3555177f18SKristof Provost
3655177f18SKristof Provostpr183198_body()
3755177f18SKristof Provost{
3855177f18SKristof Provost	pft_init
3955177f18SKristof Provost
4006aac31aSKristof Provost	epair=$(vnet_mkepair)
4106aac31aSKristof Provost	vnet_mkjail alcatraz ${epair}b
4255177f18SKristof Provost	jexec alcatraz pfctl -e
4355177f18SKristof Provost
4455177f18SKristof Provost	# Forward with pf enabled
4555177f18SKristof Provost	pft_set_rules alcatraz  \
4655177f18SKristof Provost		"table <test> { 10.0.0.1, 10.0.0.2, 10.0.0.3 }" \
4755177f18SKristof Provost		"block in" \
4855177f18SKristof Provost		"anchor \"epair\" on ${epair}b { \n\
4955177f18SKristof Provost			pass in from <test> \n\
5055177f18SKristof Provost		}"
5155177f18SKristof Provost
5255177f18SKristof Provost	atf_check -s exit:0 -o ignore jexec alcatraz pfctl -sr -a '*'
5355177f18SKristof Provost	atf_check -s exit:0 -o ignore jexec alcatraz pfctl -t test -T show
5455177f18SKristof Provost}
5555177f18SKristof Provost
5655177f18SKristof Provostpr183198_cleanup()
5755177f18SKristof Provost{
5855177f18SKristof Provost	pft_cleanup
5955177f18SKristof Provost}
6055177f18SKristof Provost
618b28ec38SKristof Provostatf_test_case "pr279225" "cleanup"
628b28ec38SKristof Provostpr279225_head()
638b28ec38SKristof Provost{
648b28ec38SKristof Provost	atf_set descr "Test that we can retrieve longer anchor names, PR 279225"
658b28ec38SKristof Provost	atf_set require.user root
668b28ec38SKristof Provost}
678b28ec38SKristof Provost
688b28ec38SKristof Provostpr279225_body()
698b28ec38SKristof Provost{
708b28ec38SKristof Provost	pft_init
718b28ec38SKristof Provost
728b28ec38SKristof Provost	vnet_mkjail alcatraz
738b28ec38SKristof Provost
748b28ec38SKristof Provost	pft_set_rules alcatraz \
758b28ec38SKristof Provost		"nat-anchor \"appjail-nat/jail/*\" all" \
768b28ec38SKristof Provost		"rdr-anchor \"appjail-rdr/*\" all" \
778b28ec38SKristof Provost		"anchor \"appjail/jail/*\" all"
788b28ec38SKristof Provost
798b28ec38SKristof Provost	atf_check -s exit:0 -o match:"nat-anchor \"appjail-nat/jail/\*\" all \{" \
808b28ec38SKristof Provost		jexec alcatraz pfctl -sn -a "*"
818b28ec38SKristof Provost	atf_check -s exit:0 -o match:"rdr-anchor \"appjail-rdr/\*\" all \{" \
828b28ec38SKristof Provost		jexec alcatraz pfctl -sn -a "*"
838b28ec38SKristof Provost	atf_check -s exit:0 -o match:"anchor \"appjail/jail/\*\" all \{" \
848b28ec38SKristof Provost		jexec alcatraz pfctl -sr -a "*"
858b28ec38SKristof Provost}
868b28ec38SKristof Provost
878b28ec38SKristof Provostpr279225_cleanup()
888b28ec38SKristof Provost{
898b28ec38SKristof Provost	pft_cleanup
908b28ec38SKristof Provost}
918b28ec38SKristof Provost
92d58d2e40SKristof Provostatf_test_case "nested_anchor" "cleanup"
93d58d2e40SKristof Provostnested_anchor_head()
94d58d2e40SKristof Provost{
95d58d2e40SKristof Provost	atf_set descr 'Test setting and retrieving nested anchors'
96d58d2e40SKristof Provost	atf_set require.user root
97d58d2e40SKristof Provost}
98d58d2e40SKristof Provost
99d58d2e40SKristof Provostnested_anchor_body()
100d58d2e40SKristof Provost{
101d58d2e40SKristof Provost	pft_init
102d58d2e40SKristof Provost
103d58d2e40SKristof Provost	epair=$(vnet_mkepair)
104d58d2e40SKristof Provost	vnet_mkjail alcatraz ${epair}a
105d58d2e40SKristof Provost
106d58d2e40SKristof Provost	pft_set_rules alcatraz \
107d58d2e40SKristof Provost		"anchor \"foo\" { \n\
108d58d2e40SKristof Provost			anchor \"bar\" { \n\
109d58d2e40SKristof Provost				pass on ${epair}a \n\
110d58d2e40SKristof Provost			} \n\
111d58d2e40SKristof Provost		}"
112d58d2e40SKristof Provost
113d58d2e40SKristof Provost	atf_check -s exit:0 -o inline:"anchor \"foo\" all {
114d58d2e40SKristof Provost  anchor \"bar\" all {
115d58d2e40SKristof Provost    pass on ${epair}a all flags S/SA keep state
116d58d2e40SKristof Provost  }
117d58d2e40SKristof Provost}
118d58d2e40SKristof Provost" jexec alcatraz pfctl -sr -a "*"
119d58d2e40SKristof Provost}
120d58d2e40SKristof Provost
121d58d2e40SKristof Provostnested_anchor_cleanup()
122d58d2e40SKristof Provost{
123d58d2e40SKristof Provost	pft_cleanup
124d58d2e40SKristof Provost}
125d58d2e40SKristof Provost
126d5a0bf45SKristof Provostatf_test_case "wildcard" "cleanup"
127d5a0bf45SKristof Provostwildcard_head()
128d5a0bf45SKristof Provost{
129d5a0bf45SKristof Provost	atf_set descr 'Test wildcard anchors for functionality'
130d5a0bf45SKristof Provost	atf_set require.user root
131d5a0bf45SKristof Provost}
132d5a0bf45SKristof Provost
133d5a0bf45SKristof Provostwildcard_body()
134d5a0bf45SKristof Provost{
135d5a0bf45SKristof Provost	pft_init
136d5a0bf45SKristof Provost
137d5a0bf45SKristof Provost	epair=$(vnet_mkepair)
138d5a0bf45SKristof Provost	vnet_mkjail alcatraz ${epair}a
139d5a0bf45SKristof Provost
140d5a0bf45SKristof Provost	ifconfig ${epair}b 192.0.2.2/24 up
141d5a0bf45SKristof Provost	jexec alcatraz ifconfig ${epair}a 192.0.2.1/24 up
142d5a0bf45SKristof Provost
143d5a0bf45SKristof Provost	# Sanity check
144d5a0bf45SKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.1
145d5a0bf45SKristof Provost
146d5a0bf45SKristof Provost	jexec alcatraz pfctl -e
147d5a0bf45SKristof Provost	pft_set_rules alcatraz \
148d5a0bf45SKristof Provost		"block" \
149d5a0bf45SKristof Provost		"anchor \"foo/*\""
150d5a0bf45SKristof Provost
151d5a0bf45SKristof Provost	atf_check -s exit:2 -o ignore ping -c 1 192.0.2.1
152d5a0bf45SKristof Provost
153d5a0bf45SKristof Provost	echo "pass" | jexec alcatraz pfctl -g -f - -a "foo/bar"
154d5a0bf45SKristof Provost
155d5a0bf45SKristof Provost	jexec alcatraz pfctl -sr -a "*"
156d5a0bf45SKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.1
157d5a0bf45SKristof Provost}
158d5a0bf45SKristof Provost
159d5a0bf45SKristof Provostwildcard_cleanup()
160d5a0bf45SKristof Provost{
161d5a0bf45SKristof Provost	pft_cleanup
162d5a0bf45SKristof Provost}
163d5a0bf45SKristof Provost
1641d723c1eSKristof Provostatf_test_case "nested_label" "cleanup"
1651d723c1eSKristof Provostnested_label_head()
1661d723c1eSKristof Provost{
1671d723c1eSKristof Provost	atf_set descr "Test recursive listing of labels"
1681d723c1eSKristof Provost	atf_set require.user root
1691d723c1eSKristof Provost}
1701d723c1eSKristof Provost
1711d723c1eSKristof Provostnested_label_body()
1721d723c1eSKristof Provost{
1731d723c1eSKristof Provost	pft_init
1741d723c1eSKristof Provost
1751d723c1eSKristof Provost	vnet_mkjail alcatraz
1761d723c1eSKristof Provost
1771d723c1eSKristof Provost	pft_set_rules alcatraz \
1781d723c1eSKristof Provost		"anchor \"foo\" { \n\
1791d723c1eSKristof Provost			pass in quick proto icmp label \"passicmp\"\n\
1801d723c1eSKristof Provost			anchor \"bar\" { \n\
1811d723c1eSKristof Provost				pass in proto tcp label \"passtcp\"\n\
1821d723c1eSKristof Provost			} \n\
1831d723c1eSKristof Provost		}" \
1841d723c1eSKristof Provost		"pass quick from any to any label \"anytoany\""
1851d723c1eSKristof Provost
1861d723c1eSKristof Provost	atf_check -s exit:0 \
1871d723c1eSKristof Provost	    -o inline:"passicmp 0 0 0 0 0 0 0 0
1881d723c1eSKristof Provostpasstcp 0 0 0 0 0 0 0 0
1891d723c1eSKristof Provostanytoany 0 0 0 0 0 0 0 0
1901d723c1eSKristof Provost" jexec alcatraz pfctl -sl -a*
1911d723c1eSKristof Provost}
1921d723c1eSKristof Provost
1931d723c1eSKristof Provostnested_label_cleanup()
1941d723c1eSKristof Provost{
1951d723c1eSKristof Provost	pft_cleanup
1961d723c1eSKristof Provost}
1971d723c1eSKristof Provost
198fff62125SKristof Provostatf_test_case "quick" "cleanup"
199fff62125SKristof Provostquick_head()
200fff62125SKristof Provost{
201fff62125SKristof Provost	atf_set descr "Test handling of quick on anchors"
202fff62125SKristof Provost	atf_set require.user root
203fff62125SKristof Provost}
204fff62125SKristof Provost
205fff62125SKristof Provostquick_body()
206fff62125SKristof Provost{
207fff62125SKristof Provost	pft_init
208fff62125SKristof Provost
209fff62125SKristof Provost	epair=$(vnet_mkepair)
210fff62125SKristof Provost	vnet_mkjail alcatraz ${epair}a
211fff62125SKristof Provost
212fff62125SKristof Provost	ifconfig ${epair}b 192.0.2.2/24 up
213fff62125SKristof Provost	jexec alcatraz ifconfig ${epair}a 192.0.2.1/24 up
214fff62125SKristof Provost
215fff62125SKristof Provost	# Sanity check
216fff62125SKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.1
217fff62125SKristof Provost
218fff62125SKristof Provost	jexec alcatraz pfctl -e
219fff62125SKristof Provost	pft_set_rules alcatraz \
220fff62125SKristof Provost	    "anchor quick {\n\
221fff62125SKristof Provost	        pass\n\
222fff62125SKristof Provost	    }" \
223fff62125SKristof Provost	    "block"
224fff62125SKristof Provost
225fff62125SKristof Provost	# We can still ping because the anchor is 'quick'
226fff62125SKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.1
227fff62125SKristof Provost	jexec alcatraz pfctl -sr -v
228fff62125SKristof Provost	jexec alcatraz pfctl -ss -v
229fff62125SKristof Provost}
230fff62125SKristof Provost
231fff62125SKristof Provostquick_cleanup()
232fff62125SKristof Provost{
233fff62125SKristof Provost	pft_cleanup
234fff62125SKristof Provost}
235fff62125SKristof Provost
236*5eee34faSKristof Provostatf_test_case "counter" "cleanup"
237*5eee34faSKristof Provostcounter_head()
238*5eee34faSKristof Provost{
239*5eee34faSKristof Provost	atf_set descr 'Test counters on anchors'
240*5eee34faSKristof Provost	atf_set require.user root
241*5eee34faSKristof Provost}
242*5eee34faSKristof Provost
243*5eee34faSKristof Provostcounter_body()
244*5eee34faSKristof Provost{
245*5eee34faSKristof Provost	pft_init
246*5eee34faSKristof Provost
247*5eee34faSKristof Provost	epair=$(vnet_mkepair)
248*5eee34faSKristof Provost	vnet_mkjail alcatraz ${epair}a
249*5eee34faSKristof Provost
250*5eee34faSKristof Provost	ifconfig ${epair}b 192.0.2.2/24 up
251*5eee34faSKristof Provost	jexec alcatraz ifconfig ${epair}a 192.0.2.1/24 up
252*5eee34faSKristof Provost
253*5eee34faSKristof Provost	# Sanity check
254*5eee34faSKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.1
255*5eee34faSKristof Provost
256*5eee34faSKristof Provost	jexec alcatraz pfctl -e
257*5eee34faSKristof Provost	pft_set_rules alcatraz \
258*5eee34faSKristof Provost	    "anchor \"foo\" {\n\
259*5eee34faSKristof Provost	        pass\n\
260*5eee34faSKristof Provost	    }"
261*5eee34faSKristof Provost
262*5eee34faSKristof Provost	# Generate traffic
263*5eee34faSKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.1
264*5eee34faSKristof Provost	atf_check -s exit:0 -e ignore \
265*5eee34faSKristof Provost	    -o match:'[ Evaluations: 1         Packets: 2         Bytes: 168         States: 1     ]' \
266*5eee34faSKristof Provost	    jexec alcatraz pfctl -sr -vv
267*5eee34faSKristof Provost}
268*5eee34faSKristof Provost
269*5eee34faSKristof Provostcounter_cleanup()
270*5eee34faSKristof Provost{
271*5eee34faSKristof Provost	pft_cleanup
272*5eee34faSKristof Provost}
273*5eee34faSKristof Provost
27455177f18SKristof Provostatf_init_test_cases()
27555177f18SKristof Provost{
27655177f18SKristof Provost	atf_add_test_case "pr183198"
2778b28ec38SKristof Provost	atf_add_test_case "pr279225"
278d58d2e40SKristof Provost	atf_add_test_case "nested_anchor"
279d5a0bf45SKristof Provost	atf_add_test_case "wildcard"
2801d723c1eSKristof Provost	atf_add_test_case "nested_label"
281fff62125SKristof Provost	atf_add_test_case "quick"
282*5eee34faSKristof Provost	atf_add_test_case "counter"
28355177f18SKristof Provost}
284