xref: /freebsd/tests/sys/netpfil/pf/utils.subr (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
1# $FreeBSD$
2# Utility functions
3##
4
5pft_init()
6{
7	if [ ! -c /dev/pf ]; then
8		atf_skip "This test requires pf"
9	fi
10
11	if [ "`sysctl -i -n kern.features.vimage`" != 1 ]; then
12		atf_skip "This test requires VIMAGE"
13	fi
14}
15
16pft_mkepair()
17{
18	ifname=$(ifconfig epair create)
19	echo $ifname >> created_interfaces.lst
20	echo ${ifname%a}
21}
22
23pft_mkjail()
24{
25	jailname=$1
26	shift
27
28	vnet_interfaces=
29	for ifname in $@
30	do
31		vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
32	done
33	jail -c name=${jailname} persist vnet ${vnet_interfaces}
34
35	echo $jailname >> created_jails.lst
36}
37
38pft_set_rules()
39{
40	jname=$1
41	shift
42
43	# Flush all states, rules, fragments, ...
44	jexec ${jname} pfctl -F all
45
46	while [ $# -gt 0 ]; do
47		printf "$1\n"
48		shift
49	done | jexec ${jname} pfctl -f -
50}
51
52pft_cleanup()
53{
54	if [ -f created_jails.lst ]; then
55		for jailname in `cat created_jails.lst`
56		do
57			jail -r ${jailname}
58		done
59		rm created_jails.lst
60	fi
61
62	if [ -f created_interfaces.lst ]; then
63		for ifname in `cat created_interfaces.lst`
64		do
65			ifconfig ${ifname} destroy
66		done
67		rm created_interfaces.lst
68	fi
69}
70