xref: /freebsd/tests/sys/netpfil/ipfw/log.sh (revision 35caa56a329deb0d13aa48ea1a6f7c1d5ebdb073)
1*35caa56aSGleb Smirnoff# SPDX-License-Identifier: BSD-2-Clause
2*35caa56aSGleb Smirnoff#
3*35caa56aSGleb Smirnoff# Copyright (c) 2026 Gleb Smirnoff <glebius@FreeBSD.org>
4*35caa56aSGleb Smirnoff#
5*35caa56aSGleb Smirnoff# Redistribution and use in source and binary forms, with or without
6*35caa56aSGleb Smirnoff# modification, are permitted provided that the following conditions
7*35caa56aSGleb Smirnoff# are met:
8*35caa56aSGleb Smirnoff# 1. Redistributions of source code must retain the above copyright
9*35caa56aSGleb Smirnoff#    notice, this list of conditions and the following disclaimer.
10*35caa56aSGleb Smirnoff# 2. Redistributions in binary form must reproduce the above copyright
11*35caa56aSGleb Smirnoff#    notice, this list of conditions and the following disclaimer in the
12*35caa56aSGleb Smirnoff#    documentation and/or other materials provided with the distribution.
13*35caa56aSGleb Smirnoff#
14*35caa56aSGleb Smirnoff# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*35caa56aSGleb Smirnoff# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*35caa56aSGleb Smirnoff# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*35caa56aSGleb Smirnoff# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*35caa56aSGleb Smirnoff# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*35caa56aSGleb Smirnoff# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*35caa56aSGleb Smirnoff# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*35caa56aSGleb Smirnoff# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*35caa56aSGleb Smirnoff# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*35caa56aSGleb Smirnoff# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*35caa56aSGleb Smirnoff# SUCH DAMAGE.
25*35caa56aSGleb Smirnoff
26*35caa56aSGleb Smirnoff. $(atf_get_srcdir)/../common/utils.subr
27*35caa56aSGleb Smirnoff
28*35caa56aSGleb Smirnoffatf_test_case "bpf" "cleanup"
29*35caa56aSGleb Smirnoffbpf_head()
30*35caa56aSGleb Smirnoff{
31*35caa56aSGleb Smirnoff	atf_set descr 'Creates several rules with log and probes bpf taps'
32*35caa56aSGleb Smirnoff	atf_set require.user root
33*35caa56aSGleb Smirnoff}
34*35caa56aSGleb Smirnoff
35*35caa56aSGleb Smirnoffbpf_body()
36*35caa56aSGleb Smirnoff{
37*35caa56aSGleb Smirnoff	firewall_init "ipfw"
38*35caa56aSGleb Smirnoff
39*35caa56aSGleb Smirnoff	epair=$(vnet_mkepair)
40*35caa56aSGleb Smirnoff	vnet_mkjail alcatraz ${epair}b
41*35caa56aSGleb Smirnoff	ifconfig ${epair}a 192.0.2.0/31 up
42*35caa56aSGleb Smirnoff	jexec alcatraz ifconfig ${epair}b 192.0.2.1/31 up
43*35caa56aSGleb Smirnoff
44*35caa56aSGleb Smirnoff	# Create a bunch of statically and auto numbered logging rules
45*35caa56aSGleb Smirnoff	rules="100 200 201"
46*35caa56aSGleb Smirnoff	for r in ${rules}; do
47*35caa56aSGleb Smirnoff		jexec alcatraz \
48*35caa56aSGleb Smirnoff		    ipfw add ${r} count log udp from any to any 10${r}
49*35caa56aSGleb Smirnoff	done
50*35caa56aSGleb Smirnoff	auto=$(jexec alcatraz ipfw add count log udp from any to any 10666 \
51*35caa56aSGleb Smirnoff	    | awk '{print $1}' | sed -Ee 's/^0+//')
52*35caa56aSGleb Smirnoff
53*35caa56aSGleb Smirnoff	pids=""
54*35caa56aSGleb Smirnoff	for r in ${rules} ${auto}; do
55*35caa56aSGleb Smirnoff		jexec alcatraz tcpdump --immediate-mode -i ipfw${r} \
56*35caa56aSGleb Smirnoff		    -w ${PWD}/${r}.pcap -c 1 &
57*35caa56aSGleb Smirnoff		pids="${pids} $!"
58*35caa56aSGleb Smirnoff	done
59*35caa56aSGleb Smirnoff
60*35caa56aSGleb Smirnoff	# wait for tcpdumps to attach, include netstat(1) header in ${count}
61*35caa56aSGleb Smirnoff	count=$(( $(echo ${rules} ${auto} | wc -w) + 1))
62*35caa56aSGleb Smirnoff	while [ $(jexec alcatraz netstat -B | wc -l) -ne ${count} ]; do
63*35caa56aSGleb Smirnoff		sleep 0.01;
64*35caa56aSGleb Smirnoff	done
65*35caa56aSGleb Smirnoff
66*35caa56aSGleb Smirnoff	for p in ${rules} 666; do
67*35caa56aSGleb Smirnoff		echo foo | nc -u 192.0.2.1 10${p} -w 0
68*35caa56aSGleb Smirnoff	done
69*35caa56aSGleb Smirnoff
70*35caa56aSGleb Smirnoff	for p in ${pids}; do
71*35caa56aSGleb Smirnoff		atf_check -s exit:0 sh -c "wait $pid; exit $?"
72*35caa56aSGleb Smirnoff	done
73*35caa56aSGleb Smirnoff
74*35caa56aSGleb Smirnoff	# statically numbered taps
75*35caa56aSGleb Smirnoff	for p in ${rules}; do
76*35caa56aSGleb Smirnoff		atf_check -o match:"192.0.2.0.[0-9]+ > 192.0.2.1.10${p}: UDP" \
77*35caa56aSGleb Smirnoff		    -e match:"reading from file [a-zA-Z0-9/.]+${p}.pcap" \
78*35caa56aSGleb Smirnoff		    tcpdump -nr ${PWD}/${p}.pcap
79*35caa56aSGleb Smirnoff	done
80*35caa56aSGleb Smirnoff
81*35caa56aSGleb Smirnoff	# autonumbered tap with 10666 port
82*35caa56aSGleb Smirnoff	atf_check -o match:"192.0.2.0.[0-9]+ > 192.0.2.1.10666: UDP" \
83*35caa56aSGleb Smirnoff	    -e match:"reading from file [a-zA-Z0-9/.]+${auto}.pcap" \
84*35caa56aSGleb Smirnoff	    tcpdump -nr ${PWD}/${auto}.pcap
85*35caa56aSGleb Smirnoff}
86*35caa56aSGleb Smirnoff
87*35caa56aSGleb Smirnoffbpf_cleanup()
88*35caa56aSGleb Smirnoff{
89*35caa56aSGleb Smirnoff	firewall_cleanup $1
90*35caa56aSGleb Smirnoff}
91*35caa56aSGleb Smirnoff
92*35caa56aSGleb Smirnoffatf_init_test_cases()
93*35caa56aSGleb Smirnoff{
94*35caa56aSGleb Smirnoff	atf_add_test_case "bpf"
95*35caa56aSGleb Smirnoff}
96