git: 93b64cdc59d6 - main - pf tests: slightly more complect captive portal setup

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Wed, 02 Mar 2022 16:01:04 UTC
The branch main has been updated by kp:

URL: https://cgit.FreeBSD.org/src/commit/?id=93b64cdc59d66fa7cd5d7e2ba2de0a67bd717840

commit 93b64cdc59d66fa7cd5d7e2ba2de0a67bd717840
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2021-10-13 13:21:43 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2022-03-02 16:00:07 +0000

    pf tests: slightly more complect captive portal setup
    
    Combine anchor, dummynet and rdr to produce a more complex captive
    portal setup.
    
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D32484
---
 tests/sys/netpfil/pf/Makefile           |  1 +
 tests/sys/netpfil/pf/daytime_inetd.conf | 31 +++++++++++++
 tests/sys/netpfil/pf/ether.sh           | 81 +++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+)

diff --git a/tests/sys/netpfil/pf/Makefile b/tests/sys/netpfil/pf/Makefile
index f793746cdbf0..45eaec2f8ddc 100644
--- a/tests/sys/netpfil/pf/Makefile
+++ b/tests/sys/netpfil/pf/Makefile
@@ -38,6 +38,7 @@ ATF_TESTS_SH+=	altq \
 
 ${PACKAGE}FILES+=	CVE-2019-5597.py \
 			CVE-2019-5598.py \
+			daytime_inetd.conf \
 			echo_inetd.conf \
 			fragcommon.py \
 			frag-overindex.py \
diff --git a/tests/sys/netpfil/pf/daytime_inetd.conf b/tests/sys/netpfil/pf/daytime_inetd.conf
new file mode 100644
index 000000000000..99cef08c3da0
--- /dev/null
+++ b/tests/sys/netpfil/pf/daytime_inetd.conf
@@ -0,0 +1,31 @@
+# $FreeBSD$
+#
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+daytime stream  tcp     nowait  root    internal
+daytime stream  tcp6    nowait  root    internal
+daytime dgram   udp     wait    root    internal
+daytime dgram   udp6    wait    root    internal
diff --git a/tests/sys/netpfil/pf/ether.sh b/tests/sys/netpfil/pf/ether.sh
index 998badad91f1..77245666a86e 100644
--- a/tests/sys/netpfil/pf/ether.sh
+++ b/tests/sys/netpfil/pf/ether.sh
@@ -295,6 +295,86 @@ captive_cleanup()
 	pft_cleanup
 }
 
+atf_test_case "captive_long" "cleanup"
+captive_long_head()
+{
+	atf_set descr 'More complex captive portal setup'
+	atf_set require.user root
+}
+
+captive_long_body()
+{
+	# Host is client, jail 'gw' is the captive portal gateway, jail 'srv'
+	# is a random (web)server. We use the echo protocol rather than http
+	# for the test, because that's easier.
+	pft_init
+
+	if ! kldstat -q -m dummynet; then
+		atf_skip "This test requires dummynet"
+	fi
+
+	epair_gw=$(vnet_mkepair)
+	epair_srv=$(vnet_mkepair)
+	epair_gw_a_mac=$(ifconfig ${epair_gw}a ether | awk '/ether/ { print $2; }')
+
+	vnet_mkjail gw ${epair_gw}b ${epair_srv}a
+	vnet_mkjail srv ${epair_srv}b
+
+	ifconfig ${epair_gw}a 192.0.2.2/24 up
+	route add -net 198.51.100.0/24 192.0.2.1
+	jexec gw ifconfig ${epair_gw}b 192.0.2.1/24 up
+	jexec gw ifconfig lo0 127.0.0.1/8 up
+	jexec gw sysctl net.inet.ip.forwarding=1
+
+	jexec gw ifconfig ${epair_srv}a 198.51.100.1/24 up
+	jexec srv ifconfig ${epair_srv}b 198.51.100.2/24 up
+	jexec srv route add -net 192.0.2.0/24 198.51.100.1
+
+	jexec gw dnctl pipe 1 config bw 300KByte/s
+
+	# Sanity check
+	atf_check -s exit:0 -o ignore ping -c 1 -t 1 198.51.100.2
+
+	pft_set_rules gw \
+		"ether anchor \"captiveportal\" on { ${epair_gw}b } {" \
+			"ether pass quick proto { 0x0806, 0x8035, 0x888e, 0x88c7, 0x8863, 0x8864 }" \
+			"ether pass tag \"captive\"" \
+		"}" \
+		"rdr on ${epair_gw}b proto tcp to port daytime tagged captive -> 127.0.0.1 port echo"
+	jexec gw pfctl -e
+
+	# ICMP should still work, because we don't redirect it.
+	atf_check -s exit:0 -o ignore ping -c 1 -t 1 198.51.100.2
+
+	jexec gw /usr/sbin/inetd -p gw.pid $(atf_get_srcdir)/echo_inetd.conf
+	jexec srv /usr/sbin/inetd -p srv.pid $(atf_get_srcdir)/daytime_inetd.conf
+
+	echo foo | nc -N 198.51.100.2 13
+
+	# Confirm that we're getting redirected
+	atf_check -s exit:0 -o match:"^foo$" -x "echo foo | nc -N 198.51.100.2 13"
+
+	# Now update the rules to allow our client to pass without redirect
+	pft_set_rules gw \
+		"ether anchor \"captiveportal\" on { ${epair_gw}b } {" \
+			"ether pass quick proto { 0x0806, 0x8035, 0x888e, 0x88c7, 0x8863, 0x8864 }" \
+			"ether pass quick from { ${epair_gw_a_mac} } dnpipe 1" \
+			"ether pass tag \"captive\"" \
+		"}" \
+		"rdr on ${epair_gw}b proto tcp to port daytime tagged captive -> 127.0.0.1 port echo"
+
+	# We're not being redirected and get datime information now
+	atf_check -s exit:0 -o match:"^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)" -x "echo foo | nc -N 198.51.100.2 13"
+
+	jexec gw killall inetd
+	jexec srv killall inetd
+}
+
+captive_long_cleanup()
+{
+	pft_cleanup
+}
+
 atf_test_case "dummynet" "cleanup"
 dummynet_head()
 {
@@ -404,6 +484,7 @@ atf_init_test_cases()
 	atf_add_test_case "proto"
 	atf_add_test_case "direction"
 	atf_add_test_case "captive"
+	atf_add_test_case "captive_long"
 	atf_add_test_case "dummynet"
 	atf_add_test_case "anchor"
 }