git: 2cfb60783900 - stable/13 - pf tests: test fast port re-use with syncookies

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Sat, 28 Jan 2023 01:39:21 UTC
The branch stable/13 has been updated by kp:

URL: https://cgit.FreeBSD.org/src/commit/?id=2cfb60783900b625798c5ceb52b9aa93c5c412ae

commit 2cfb60783900b625798c5ceb52b9aa93c5c412ae
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2022-12-31 18:23:15 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2023-01-28 01:34:38 +0000

    pf tests: test fast port re-use with syncookies
    
    When a src/dst ip/port tuple is re-used before the pf state fully
    expires we clean up the state and create a new one, unless syncookies
    are enabled.
    
    Test this, by running two back-to-back nc sessions, with a fixed source
    port. Move the interface and IP to a different (vnet) jail, to trick the
    network stack into letting us do this.
    
    MFC after:      2 weeks
    Event:          Aberdeen hackathon 2022
    Differential Revision:  https://reviews.freebsd.org/D36886
    
    (cherry picked from commit dc698b2cd59ebc08b05a261dbba8ee5707450d28)
---
 tests/sys/netpfil/pf/syncookie.sh | 57 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/tests/sys/netpfil/pf/syncookie.sh b/tests/sys/netpfil/pf/syncookie.sh
index d85beac1a4d1..dbb6407e9c38 100644
--- a/tests/sys/netpfil/pf/syncookie.sh
+++ b/tests/sys/netpfil/pf/syncookie.sh
@@ -217,10 +217,67 @@ adaptive_cleanup()
 	pft_cleanup
 }
 
+atf_test_case "port_reuse" "cleanup"
+port_reuse_head()
+{
+	atf_set descr 'Test rapid port re-use'
+	atf_set require.user root
+}
+
+port_reuse_body()
+{
+	pft_init
+
+	epair=$(vnet_mkepair)
+
+	vnet_mkjail alcatraz ${epair}b
+	vnet_mkjail singsing
+	jexec alcatraz ifconfig ${epair}b 192.0.2.1/24 up
+	jexec alcatraz /usr/sbin/inetd -p ${HOME}/inetd-alcatraz.pid \
+	    $(atf_get_srcdir)/echo_inetd.conf
+
+	ifconfig ${epair}a 192.0.2.2/24 up
+
+	jexec alcatraz pfctl -e
+	jexec alcatraz pfctl -x loud
+	pft_set_rules alcatraz \
+		"set syncookies always" \
+		"pass in" \
+		"pass out"
+
+	# Sanity check
+	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.1
+
+	reply=$(echo foo | nc -p 1234 -N -w 5 192.0.2.1 7)
+	if [ "${reply}" != "foo" ];
+	then
+		atf_fail "Failed to connect to syncookie protected echo daemon"
+	fi
+
+	# We can't re-use the source IP/port combo quickly enough, so we're
+	# going to play a really dirty trick, and move our interface to a new
+	# jail, and do it from there.
+	ifconfig ${epair}a vnet singsing
+	jexec singsing ifconfig ${epair}a 192.0.2.2/24 up
+	atf_check -s exit:0 -o ignore jexec singsing ping -c 1 192.0.2.1
+
+	reply=$(echo bar | jexec singsing nc -p 1234 -N -w 5 192.0.2.1 7)
+	if [ "${reply}" != "bar" ];
+	then
+		atf_fail "Failed to connect to syncookie protected echo daemon (2)"
+	fi
+}
+
+port_reuse_cleanup()
+{
+	pft_cleanup
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case "basic"
 	atf_add_test_case "forward"
 	atf_add_test_case "nostate"
 	atf_add_test_case "adaptive"
+	atf_add_test_case "port_reuse"
 }