xref: /freebsd/tests/sys/netpfil/pf/utils.subr (revision 9768746b)
1# $FreeBSD$
2# Utility functions
3##
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2017 Kristof Provost <kp@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28
29. $(atf_get_srcdir)/../../common/vnet.subr
30
31pft_onerror()
32{
33	status=$?
34
35	echo "Debug log."
36	echo "=========="
37	echo "Test exit status: $?"
38	echo
39
40	if [ -f created_jails.lst ]; then
41		for jailname in `cat created_jails.lst`
42		do
43			echo "Jail ${jailname}"
44			echo "----------------"
45			jexec ${jailname} ifconfig
46			jexec ${jailname} netstat -rn
47			jexec ${jailname} pfctl -sa -v
48		done
49	fi
50
51	echo "Created interfaces:"
52	echo "-------------------"
53	cat created_interfaces.lst
54
55	echo "Host interfaces:"
56	echo "----------------"
57	ifconfig
58}
59
60pft_init()
61{
62	if [ "$1" == "debug" ]
63	then
64		trap pft_onerror EXIT
65	fi
66
67	vnet_init
68
69	if [ ! -c /dev/pf ]; then
70		atf_skip "This test requires pf"
71	fi
72}
73
74pfsynct_init()
75{
76	pft_init
77
78	if ! kldstat -q -m pfsync; then
79		atf_skip "This test requires pfsync"
80	fi
81}
82
83pflog_init()
84{
85	if ! kldstat -q -m pflog; then
86		atf_skip "This test requires pflog"
87	fi
88}
89
90dummynet_init()
91{
92	pft_init
93
94	if ! kldstat -q -m dummynet; then
95		atf_skip "This test requires dummynet"
96	fi
97}
98
99pft_set_rules()
100{
101	jname=$1
102	shift
103
104	if [ $jname == "noflush" ];
105	then
106		jname=$1
107		shift
108	else
109		# Flush all states, rules, fragments, ...
110		jexec ${jname} pfctl -F all
111	fi
112
113	while [ $# -gt 0 ]; do
114		printf "$1\n"
115		shift
116	done | jexec ${jname} pfctl -f -
117	if [ $? -ne 0 ];
118	then
119		atf_fail "Failed to set PF rules in ${jname}"
120	fi
121}
122
123pft_cleanup()
124{
125	vnet_cleanup
126}
127
128pfsynct_cleanup()
129{
130	pft_cleanup
131}
132
133is_altq_supported()
134{
135	sysctl -q kern.features.altq >/dev/null || \
136	    atf_skip "Test requires ALTQ"
137
138	while [ -n "$1" ]
139	do
140		sysctl -q kern.features.altq.${1} >/dev/null || \
141		    atf_skip "Test required ALTQ_${1}"
142		shift
143	done
144}
145
146altq_init()
147{
148	pft_init
149	is_altq_supported
150}
151
152altq_cleanup()
153{
154	pft_cleanup
155}
156