xref: /freebsd/tests/sys/netpfil/ipfw/fwd.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
2c3ca3fa7SGleb Smirnoff#
3c3ca3fa7SGleb Smirnoff# Copyright (c) 2022 Gleb Smirnoff <glebius@FreeBSD.org>
4c3ca3fa7SGleb Smirnoff# Copyright (c) 2022 Pavel Polyakov <bsd@kobyla.org>
5c3ca3fa7SGleb Smirnoff#
6c3ca3fa7SGleb Smirnoff# Redistribution and use in source and binary forms, with or without
7c3ca3fa7SGleb Smirnoff# modification, are permitted provided that the following conditions
8c3ca3fa7SGleb Smirnoff# are met:
9c3ca3fa7SGleb Smirnoff# 1. Redistributions of source code must retain the above copyright
10c3ca3fa7SGleb Smirnoff#    notice, this list of conditions and the following disclaimer.
11c3ca3fa7SGleb Smirnoff# 2. Redistributions in binary form must reproduce the above copyright
12c3ca3fa7SGleb Smirnoff#    notice, this list of conditions and the following disclaimer in the
13c3ca3fa7SGleb Smirnoff#    documentation and/or other materials provided with the distribution.
14c3ca3fa7SGleb Smirnoff#
15c3ca3fa7SGleb Smirnoff# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c3ca3fa7SGleb Smirnoff# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c3ca3fa7SGleb Smirnoff# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c3ca3fa7SGleb Smirnoff# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c3ca3fa7SGleb Smirnoff# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c3ca3fa7SGleb Smirnoff# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c3ca3fa7SGleb Smirnoff# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c3ca3fa7SGleb Smirnoff# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c3ca3fa7SGleb Smirnoff# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c3ca3fa7SGleb Smirnoff# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c3ca3fa7SGleb Smirnoff# SUCH DAMAGE.
26c3ca3fa7SGleb Smirnoff
27c3ca3fa7SGleb Smirnoff. $(atf_get_srcdir)/../common/utils.subr
28c3ca3fa7SGleb Smirnoff
29c3ca3fa7SGleb Smirnoffatf_test_case "local" "cleanup"
30c3ca3fa7SGleb Smirnofflocal_head()
31c3ca3fa7SGleb Smirnoff{
32c3ca3fa7SGleb Smirnoff	atf_set descr 'Test that fwd 127.0.0.1,port works'
33c3ca3fa7SGleb Smirnoff	atf_set require.user root
34c3ca3fa7SGleb Smirnoff}
35c3ca3fa7SGleb Smirnoff
36c3ca3fa7SGleb Smirnofflocal_body()
37c3ca3fa7SGleb Smirnoff{
38c3ca3fa7SGleb Smirnoff	firewall_init "ipfw"
39c3ca3fa7SGleb Smirnoff
40c3ca3fa7SGleb Smirnoff	epair=$(vnet_mkepair)
41c3ca3fa7SGleb Smirnoff	vnet_mkjail alcatraz ${epair}b
42c3ca3fa7SGleb Smirnoff
43c3ca3fa7SGleb Smirnoff	ifconfig ${epair}a 192.0.2.0/31 up
44c3ca3fa7SGleb Smirnoff	route add 192.0.2.3/32 192.0.2.1
45c3ca3fa7SGleb Smirnoff
46c3ca3fa7SGleb Smirnoff	jexec alcatraz ifconfig lo0 127.0.0.1/8 up
47c3ca3fa7SGleb Smirnoff	jexec alcatraz ifconfig ${epair}b 192.0.2.1/31 up
48c3ca3fa7SGleb Smirnoff	jexec alcatraz route add default 192.0.2.0
49c3ca3fa7SGleb Smirnoff	jexec alcatraz /usr/sbin/inetd -p /dev/null $(atf_get_srcdir)/fwd_inetd.conf
50c3ca3fa7SGleb Smirnoff
51c3ca3fa7SGleb Smirnoff	firewall_config alcatraz ipfw ipfw \
52c3ca3fa7SGleb Smirnoff	    "ipfw add 10 fwd 127.0.0.1,82 tcp from any to any dst-port 80 in via ${epair}b" \
53c3ca3fa7SGleb Smirnoff	    "ipfw add 20 allow all from any to any"
54c3ca3fa7SGleb Smirnoff
55c3ca3fa7SGleb Smirnoff	# Sanity check
56c3ca3fa7SGleb Smirnoff	atf_check -s exit:0 -o ignore ping -i .1 -c 3 -s 1200 192.0.2.1
57c3ca3fa7SGleb Smirnoff
58c3ca3fa7SGleb Smirnoff	reply=$(nc -nN 192.0.2.3 80 < /dev/null)
59c3ca3fa7SGleb Smirnoff	atf_check [ "${reply}" = "GOOD 82" ]
60c3ca3fa7SGleb Smirnoff}
61c3ca3fa7SGleb Smirnoff
62c3ca3fa7SGleb Smirnofflocal_cleanup()
63c3ca3fa7SGleb Smirnoff{
64c3ca3fa7SGleb Smirnoff	firewall_cleanup $1
65c3ca3fa7SGleb Smirnoff}
66c3ca3fa7SGleb Smirnoff
67c3ca3fa7SGleb Smirnoffatf_init_test_cases()
68c3ca3fa7SGleb Smirnoff{
69c3ca3fa7SGleb Smirnoff	atf_add_test_case "local"
70c3ca3fa7SGleb Smirnoff}
71