git: cb2657c9622a - stable/14 - netinet6 tests: Add a regression test for default router handling

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 08 Aug 2024 19:11:48 UTC
The branch stable/14 has been updated by markj:

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

commit cb2657c9622abb416ba90239e6f652147e7b62dc
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-07-25 21:01:11 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-08-08 19:11:34 +0000

    netinet6 tests: Add a regression test for default router handling
    
    This serves as a regression test for commit a48df53e4249.
    
    Reviewed by:    bz, allanjude
    MFC after:      2 weeks
    Sponsored by:   Klara, Inc.
    Sponsored by:   Bell Tower Integration
    Differential Revision:  https://reviews.freebsd.org/D46136
    
    (cherry picked from commit feda329622bc77ab64ae5f0bf852743f4a037616)
---
 tests/sys/netinet6/Makefile | 34 +++++++++--------
 tests/sys/netinet6/ndp.sh   | 92 ++++++++++++++++++++++++++++++++++++++++++---
 tests/sys/netinet6/ra.py    | 38 +++++++++++++++++++
 3 files changed, 143 insertions(+), 21 deletions(-)

diff --git a/tests/sys/netinet6/Makefile b/tests/sys/netinet6/Makefile
index 82e84859ecbc..46f6f26115fe 100644
--- a/tests/sys/netinet6/Makefile
+++ b/tests/sys/netinet6/Makefile
@@ -5,28 +5,30 @@ TESTSDIR=	${TESTSBASE}/sys/netinet6
 FILESDIR=	${TESTSDIR}
 
 ATF_TESTS_PYTEST=	test_ip6_output.py
-ATF_TESTS_SH=			\
-				exthdr \
-				mld \
-				scapyi386 \
-				redirect \
-				divert \
-				forward6 \
-				output6 \
-				lpm6 \
-				fibs6 \
-				ndp \
-				proxy_ndp
+ATF_TESTS_SH=		exthdr \
+			mld \
+			scapyi386 \
+			redirect \
+			divert \
+			forward6 \
+			output6 \
+			lpm6 \
+			fibs6 \
+			ndp \
+			proxy_ndp
+
 TEST_METADATA.output6+=	required_programs="python"
 
-${PACKAGE}FILES+=		exthdr.py
-${PACKAGE}FILES+=		mld.py
-${PACKAGE}FILES+=		scapyi386.py
-${PACKAGE}FILES+=		redirect.py
+${PACKAGE}FILES+=	exthdr.py \
+			mld.py \
+			scapyi386.py \
+			ra.py \
+			redirect.py
 
 ${PACKAGE}FILESMODE_exthdr.py=	0555
 ${PACKAGE}FILESMODE_mld.py=	0555
 ${PACKAGE}FILESMODE_scapyi386.py=0555
+${PACKAGE}FILESMODE_ra.py=0555
 ${PACKAGE}FILESMODE_redirect.py=0555
 
 TESTS_SUBDIRS+=	frag6
diff --git a/tests/sys/netinet6/ndp.sh b/tests/sys/netinet6/ndp.sh
index eddd49112421..038a640f331e 100755
--- a/tests/sys/netinet6/ndp.sh
+++ b/tests/sys/netinet6/ndp.sh
@@ -25,7 +25,6 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-#
 
 . $(atf_get_srcdir)/../common/vnet.subr
 
@@ -36,6 +35,7 @@ ndp_add_gu_success_head() {
 }
 
 ndp_add_gu_success_body() {
+	local epair0 jname
 
 	vnet_init
 
@@ -74,6 +74,7 @@ ndp_del_gu_success_head() {
 }
 
 ndp_del_gu_success_body() {
+	local epair0 jname
 
 	vnet_init
 
@@ -102,13 +103,94 @@ ndp_del_gu_success_cleanup() {
 	vnet_cleanup
 }
 
+ndp_if_up()
+{
+	local ifname=$1
+	local jname=$2
 
-atf_init_test_cases()
+	if [ -n "$jname" ]; then
+		jname="jexec ${jname}"
+	fi
+	atf_check ${jname} ifconfig ${ifname} up
+	atf_check ${jname} ifconfig ${ifname} inet6 -ifdisabled
+	while ${jname} ifconfig ${ifname} inet6 | grep tentative; do
+		sleep 0.1
+	done
+}
+
+ndp_if_lladdr()
 {
+	local ifname=$1
+	local jname=$2
 
-	atf_add_test_case "ndp_add_gu_success"
-	atf_add_test_case "ndp_del_gu_success"
+	if [ -n "$jname" ]; then
+		jname="jexec ${jname}"
+	fi
+	${jname} ifconfig ${ifname} inet6 | \
+	    awk '/inet6 fe80:/{split($2, addr, "%"); print addr[1]}'
 }
 
-# end
+atf_test_case "ndp_slaac_default_route" "cleanup"
+ndp_slaac_default_route_head() {
+	atf_set descr 'Test default route installation via SLAAC'
+	atf_set require.user root
+	atf_set require.progs "python"
+}
+
+ndp_slaac_default_route_body() {
+	local epair0 jname lladdr
+
+	vnet_init
+
+	jname="v6t-ndp_slaac_default_route"
+
+	epair0=$(vnet_mkepair)
 
+	vnet_mkjail ${jname} ${epair0}a
+
+	ndp_if_up ${epair0}a ${jname}
+	ndp_if_up ${epair0}b
+	atf_check jexec ${jname} ifconfig ${epair0}a inet6 accept_rtadv
+
+        # Send an RA advertising a prefix.
+	atf_check -e ignore python $(atf_get_srcdir)/ra.py \
+	    --sendif ${epair0}b \
+	    --dst $(ndp_if_lladdr ${epair0}a ${jname}) \
+	    --src $(ndp_if_lladdr ${epair0}b) \
+	    --prefix "2001:db8:ffff:1000::" --prefixlen 64
+
+	# Wait for a default router to appear.
+	while [ -z "$(jexec ${jname} ndp -r)" ]; do
+		sleep 0.1
+	done
+	atf_check -o match:"^default[[:space:]]+fe80:" \
+	    jexec ${jname} netstat -rn -6
+
+	# Get rid of the default route.
+	jexec ${jname} route -6 flush
+	atf_check -o not-match:"^default[[:space:]]+fe80:" \
+	    jexec ${jname} netstat -rn -6
+
+	# Send another RA, make sure that the default route is installed again.
+	atf_check -e ignore python $(atf_get_srcdir)/ra.py \
+	    --sendif ${epair0}b \
+	    --dst $(ndp_if_lladdr ${epair0}a ${jname}) \
+	    --src $(ndp_if_lladdr ${epair0}b) \
+	    --prefix "2001:db8:ffff:1000::" --prefixlen 64
+	while [ -z "$(jexec ${jname} ndp -r)" ]; do
+		sleep 0.1
+	done
+	atf_check -o match:"^default[[:space:]]+fe80:" \
+	    jexec ${jname} netstat -rn -6
+}
+
+ndp_slaac_default_route_cleanup() {
+	vnet_cleanup
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case "ndp_add_gu_success"
+	atf_add_test_case "ndp_del_gu_success"
+	atf_add_test_case "ndp_slaac_default_route"
+}
diff --git a/tests/sys/netinet6/ra.py b/tests/sys/netinet6/ra.py
new file mode 100644
index 000000000000..44814418da48
--- /dev/null
+++ b/tests/sys/netinet6/ra.py
@@ -0,0 +1,38 @@
+#-
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Klara, Inc.
+#
+
+import argparse
+import scapy.all as sp
+import sys
+
+#
+# Emit a router advertisement with the specified prefix.
+#
+def main():
+    parser = argparse.ArgumentParser("ra.py",
+        description="Emits Router Advertisement packets")
+    parser.add_argument('--sendif', nargs=1, required=True,
+        help='The interface through which the packet will be sent')
+    parser.add_argument('--src', nargs=1, required=True,
+        help='The source IP address')
+    parser.add_argument('--dst', nargs=1, required=True,
+        help='The destination IP address')
+    parser.add_argument('--prefix', nargs=1, required=True,
+        help='The prefix to be advertised')
+    parser.add_argument('--prefixlen', nargs=1, required=True, type=int,
+        help='The prefix length to be advertised')
+
+    args = parser.parse_args()
+    pkt = sp.Ether() / \
+          sp.IPv6(src=args.src, dst=args.dst) / \
+          sp.ICMPv6ND_RA(chlim=64) / \
+          sp.ICMPv6NDOptPrefixInfo(prefix=args.prefix, prefixlen=args.prefixlen)
+
+    sp.sendp(pkt, iface=args.sendif[0], verbose=False)
+    sys.exit(0)
+
+if __name__ == '__main__':
+    main()