xref: /freebsd/tests/sys/netpfil/pf/utils.subr (revision fdc418f15e92732a3551832bcb625ba9b47242df)
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
90pft_set_rules()
91{
92	jname=$1
93	shift
94
95	if [ $jname == "noflush" ];
96	then
97		jname=$1
98		shift
99	else
100		# Flush all states, rules, fragments, ...
101		jexec ${jname} pfctl -F all
102	fi
103
104	while [ $# -gt 0 ]; do
105		printf "$1\n"
106		shift
107	done | jexec ${jname} pfctl -f -
108	if [ $? -ne 0 ];
109	then
110		atf_fail "Failed to set PF rules in ${jname}"
111	fi
112}
113
114pft_cleanup()
115{
116	vnet_cleanup
117}
118
119pfsynct_cleanup()
120{
121	pft_cleanup
122}
123
124is_altq_supported()
125{
126	sysctl -q kern.features.altq >/dev/null || \
127	    atf_skip "Test requires ALTQ"
128
129	while [ -n "$1" ]
130	do
131		sysctl -q kern.features.altq.${1} >/dev/null || \
132		    atf_skip "Test required ALTQ_${1}"
133		shift
134	done
135}
136
137altq_init()
138{
139	pft_init
140	is_altq_supported
141}
142
143altq_cleanup()
144{
145	pft_cleanup
146}
147