xref: /freebsd/tests/sys/netinet6/frag6/frag6.subr (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
1#-
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2019 Netflix, Inc.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27
28. $(atf_get_srcdir)/../../common/vnet.subr
29
30frag6_head()
31{
32	atf_set descr 'Test IPv6 fragmentation code'
33	atf_set require.user root
34	atf_set require.progs scapy
35}
36
37frag6_body()
38{
39	ids=${1:="65533"}
40	shift
41	id=`printf "%x" ${ids}`
42	if [ $$ -gt 65535 ]; then
43		xl=`printf "%x" $(($$ - 65535))`
44		yl="1"
45	else
46		xl=`printf "%x" $$`
47		yl=""
48	fi
49
50	vnet_init
51
52	ip6a="2001:db8:6666:6666:${yl}:${id}:1:${xl}"
53	ip6b="2001:db8:6666:6666:${yl}:${id}:2:${xl}"
54
55	epair=$(vnet_mkepair)
56	ifconfig ${epair}a up
57	ifconfig ${epair}a inet6 ${ip6a}/64
58
59	jname="v6t-${id}-${yl}-${xl}"
60	vnet_mkjail ${jname} ${epair}b
61	jexec ${jname} sysctl net.inet6.ip6.dad_count=0
62	jexec ${jname} ifconfig ${epair}b up
63	jexec ${jname} ifconfig ${epair}b inet6 ${ip6b}/64
64
65	# Set max fragment reassembly time to 2 seconds
66	jexec ${jname} sysctl net.inet6.ip6.fraglifetime_ms=2000
67
68	# Let IPv6 ND do its thing.
69	while [ `ifconfig ${epair}a inet6 | grep -c tentative` != "0" ]; do
70		sleep 0.1
71	done
72
73	# We need to try to make sure all expiry happened, otherwise there might
74	# be global fragments queued.  (This still does not rule out that there
75	# are no other fragments queued anywhere else in the system).
76	i=0
77	while test $i -lt 60; do
78		nf=`sysctl -n net.inet6.ip6.frag6_nfrags`
79		case ${nf} in
80		0)	break ;;
81		esac
82		sleep 1
83		i=$((i + 1))
84	done
85	case ${nf} in
86	0)	;;
87	*)	atf_fail "Global frag6_nfrags count is not zero but ${nf}" ;;
88	esac
89
90	pretestf=$2
91	case "${pretestf}" in
92	"")	;;
93	[A-Za-z0-9_]*)
94		eval ${pretestf} "${jname}" "${epair}b"
95		;;
96	esac
97
98	# Clear statistics.
99	jexec ${jname} netstat -z -s > /dev/null
100
101	# Run fragment tests.
102	pyname=$(atf_get ident)
103	pyname=${pyname%*_[0-9]}
104	atf_check -s exit:0 $(atf_get_srcdir)/${pyname}.py \
105		--sendif ${epair}a \
106		--recvif ${epair}a \
107		--src ${ip6a} \
108		--to  ${ip6b}
109
110	checkf=$1
111	case "${checkf}" in
112	"")	;;
113	[A-Za-z0-9_]*)
114		eval ${checkf} "${jname}" "${epair}b"
115		;;
116	esac
117}
118
119frag6_cleanup()
120{
121
122	vnet_cleanup
123}
124
125# end
126