xref: /freebsd/tests/sys/netpfil/pf/utils.subr (revision d27ba3088424e53eabc0b0186ed122ec43119501)
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	trap pft_onerror EXIT
63
64	vnet_init
65
66	if [ ! -c /dev/pf ]; then
67		atf_skip "This test requires pf"
68	fi
69}
70
71pfsynct_init()
72{
73	pft_init
74
75	if ! kldstat -q -m pfsync; then
76		atf_skip "This test requires pfsync"
77	fi
78}
79
80pflog_init()
81{
82	if ! kldstat -q -m pflog; then
83		atf_skip "This test requires pflog"
84	fi
85}
86
87pft_set_rules()
88{
89	jname=$1
90	shift
91
92	if [ $jname == "noflush" ];
93	then
94		jname=$1
95		shift
96	else
97		# Flush all states, rules, fragments, ...
98		jexec ${jname} pfctl -F all
99	fi
100
101	while [ $# -gt 0 ]; do
102		printf "$1\n"
103		shift
104	done | jexec ${jname} pfctl -f -
105	if [ $? -ne 0 ];
106	then
107		atf_fail "Failed to set PF rules in ${jname}"
108	fi
109}
110
111pft_cleanup()
112{
113	vnet_cleanup
114}
115
116pfsynct_cleanup()
117{
118	pft_cleanup
119}
120
121is_altq_supported()
122{
123	sysctl -q kern.features.altq >/dev/null || \
124	    atf_skip "Test requires ALTQ"
125
126	while [ -n "$1" ]
127	do
128		sysctl -q kern.features.altq.${1} >/dev/null || \
129		    atf_skip "Test required ALTQ_${1}"
130		shift
131	done
132}
133
134altq_init()
135{
136	pft_init
137	is_altq_supported
138}
139
140altq_cleanup()
141{
142	pft_cleanup
143}
144