xref: /freebsd/tests/sys/netpfil/ipfw/table.sh (revision 7eac31c83ee3fca6a2c19ee0aa21a310e0050f51)
1*7eac31c8SGleb Smirnoff# SPDX-License-Identifier: BSD-2-Clause
2*7eac31c8SGleb Smirnoff#
3*7eac31c8SGleb Smirnoff# Copyright (c) 2026 Gleb Smirnoff <glebius@FreeBSD.org>
4*7eac31c8SGleb Smirnoff#
5*7eac31c8SGleb Smirnoff# Redistribution and use in source and binary forms, with or without
6*7eac31c8SGleb Smirnoff# modification, are permitted provided that the following conditions
7*7eac31c8SGleb Smirnoff# are met:
8*7eac31c8SGleb Smirnoff# 1. Redistributions of source code must retain the above copyright
9*7eac31c8SGleb Smirnoff#    notice, this list of conditions and the following disclaimer.
10*7eac31c8SGleb Smirnoff# 2. Redistributions in binary form must reproduce the above copyright
11*7eac31c8SGleb Smirnoff#    notice, this list of conditions and the following disclaimer in the
12*7eac31c8SGleb Smirnoff#    documentation and/or other materials provided with the distribution.
13*7eac31c8SGleb Smirnoff#
14*7eac31c8SGleb Smirnoff# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*7eac31c8SGleb Smirnoff# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*7eac31c8SGleb Smirnoff# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*7eac31c8SGleb Smirnoff# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*7eac31c8SGleb Smirnoff# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*7eac31c8SGleb Smirnoff# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*7eac31c8SGleb Smirnoff# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*7eac31c8SGleb Smirnoff# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*7eac31c8SGleb Smirnoff# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*7eac31c8SGleb Smirnoff# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*7eac31c8SGleb Smirnoff# SUCH DAMAGE.
25*7eac31c8SGleb Smirnoff
26*7eac31c8SGleb Smirnoff. $(atf_get_srcdir)/../common/utils.subr
27*7eac31c8SGleb Smirnoff
28*7eac31c8SGleb Smirnoffatf_test_case "fuzz" "cleanup"
29*7eac31c8SGleb Smirnofffuzz_head()
30*7eac31c8SGleb Smirnoff{
31*7eac31c8SGleb Smirnoff	atf_set descr 'Create couple tables and fuzzes them'
32*7eac31c8SGleb Smirnoff	atf_set require.user root
33*7eac31c8SGleb Smirnoff}
34*7eac31c8SGleb Smirnoff
35*7eac31c8SGleb Smirnofffuzz_body()
36*7eac31c8SGleb Smirnoff{
37*7eac31c8SGleb Smirnoff	firewall_init "ipfw"
38*7eac31c8SGleb Smirnoff
39*7eac31c8SGleb Smirnoff	epair=$(vnet_mkepair)
40*7eac31c8SGleb Smirnoff	vnet_mkjail sender ${epair}a
41*7eac31c8SGleb Smirnoff	jexec sender ifconfig ${epair}a 192.0.2.0/31 up
42*7eac31c8SGleb Smirnoff	jexec sender route add 10.0.0.0/8 192.0.2.1
43*7eac31c8SGleb Smirnoff
44*7eac31c8SGleb Smirnoff	vnet_mkjail receiver ${epair}b
45*7eac31c8SGleb Smirnoff	jexec receiver ifconfig lo0 127.0.0.1/8 up
46*7eac31c8SGleb Smirnoff	jexec receiver ifconfig ${epair}b 192.0.2.1/31 up
47*7eac31c8SGleb Smirnoff	jexec receiver route add 10.0.0.0/8 -blackhole -iface lo0
48*7eac31c8SGleb Smirnoff
49*7eac31c8SGleb Smirnoff	jexec receiver ipfw add 100 count ip from any to table\(tb0\)
50*7eac31c8SGleb Smirnoff	jexec receiver ipfw add 200 count ip from any to table\(tb1\)
51*7eac31c8SGleb Smirnoff
52*7eac31c8SGleb Smirnoff	( jexec sender sh -c \
53*7eac31c8SGleb Smirnoff	    'while true; do \
54*7eac31c8SGleb Smirnoff		oct=$(od -An -N1 -tu1 < /dev/urandom); \
55*7eac31c8SGleb Smirnoff		oct=$(echo $oct); \
56*7eac31c8SGleb Smirnoff		ping -c 5 -i .01 -W .01 10.0.0.${oct} >/dev/null; \
57*7eac31c8SGleb Smirnoff	    done' ) &
58*7eac31c8SGleb Smirnoff	pinger=$!
59*7eac31c8SGleb Smirnoff
60*7eac31c8SGleb Smirnoff	( jexec receiver sh -c \
61*7eac31c8SGleb Smirnoff	    'while true; do \
62*7eac31c8SGleb Smirnoff		set -- $(od -An -N2 -tu1 < /dev/urandom); \
63*7eac31c8SGleb Smirnoff		ipfw -q table tb$(($1 % 2)) add 10.0.0.$2; \
64*7eac31c8SGleb Smirnoff	     done' ) &
65*7eac31c8SGleb Smirnoff	adder=$!
66*7eac31c8SGleb Smirnoff
67*7eac31c8SGleb Smirnoff	( jexec receiver sh -c \
68*7eac31c8SGleb Smirnoff	    'while true; do \
69*7eac31c8SGleb Smirnoff		set -- $(od -An -N2 -tu1 < /dev/urandom); \
70*7eac31c8SGleb Smirnoff		ipfw -q table tb$(($1 % 2)) del 10.0.0.$2; \
71*7eac31c8SGleb Smirnoff	     done' ) &
72*7eac31c8SGleb Smirnoff	deleter=$!
73*7eac31c8SGleb Smirnoff
74*7eac31c8SGleb Smirnoff	( jexec receiver sh -c \
75*7eac31c8SGleb Smirnoff	    'while true; do \
76*7eac31c8SGleb Smirnoff		ipfw table tb0 swap tb1; \
77*7eac31c8SGleb Smirnoff		sleep .25; \
78*7eac31c8SGleb Smirnoff	    done' ) &
79*7eac31c8SGleb Smirnoff	swapper=$!
80*7eac31c8SGleb Smirnoff
81*7eac31c8SGleb Smirnoff	sleep 30
82*7eac31c8SGleb Smirnoff	kill $pinger
83*7eac31c8SGleb Smirnoff	kill $adder
84*7eac31c8SGleb Smirnoff	kill $deleter
85*7eac31c8SGleb Smirnoff	kill $swapper
86*7eac31c8SGleb Smirnoff}
87*7eac31c8SGleb Smirnoff
88*7eac31c8SGleb Smirnofffuzz_cleanup()
89*7eac31c8SGleb Smirnoff{
90*7eac31c8SGleb Smirnoff	firewall_cleanup $1
91*7eac31c8SGleb Smirnoff}
92*7eac31c8SGleb Smirnoff
93*7eac31c8SGleb Smirnoffatf_init_test_cases()
94*7eac31c8SGleb Smirnoff{
95*7eac31c8SGleb Smirnoff	atf_add_test_case "fuzz"
96*7eac31c8SGleb Smirnoff}
97