xref: /freebsd/tests/sys/netpfil/pf/utils.subr (revision 1ade1c411307f58d4518938883116c31766faaf7)
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
16pfsynct_init()
17{
18	pft_init
19
20	if ! kldstat -q -m pfsync; then
21		atf_skip "This test requires pfsync"
22	fi
23}
24
25pft_mkepair()
26{
27	ifname=$(ifconfig epair create)
28	echo $ifname >> created_interfaces.lst
29	echo ${ifname%a}
30}
31
32pft_mkjail()
33{
34	jailname=$1
35	shift
36
37	vnet_interfaces=
38	for ifname in $@
39	do
40		vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
41	done
42	jail -c name=${jailname} persist vnet ${vnet_interfaces}
43
44	echo $jailname >> created_jails.lst
45}
46
47pft_set_rules()
48{
49	jname=$1
50	shift
51
52	# Flush all states, rules, fragments, ...
53	jexec ${jname} pfctl -F all
54
55	while [ $# -gt 0 ]; do
56		printf "$1\n"
57		shift
58	done | jexec ${jname} pfctl -f -
59}
60
61pft_cleanup()
62{
63	if [ -f created_jails.lst ]; then
64		for jailname in `cat created_jails.lst`
65		do
66			jail -r ${jailname}
67		done
68		rm created_jails.lst
69	fi
70
71	if [ -f created_interfaces.lst ]; then
72		for ifname in `cat created_interfaces.lst`
73		do
74			ifconfig ${ifname} destroy
75		done
76		rm created_interfaces.lst
77	fi
78}
79
80pfsynct_cleanup()
81{
82	pft_cleanup
83}
84