xref: /freebsd/tests/sys/netinet6/ndp.sh (revision feda329622bc77ab64ae5f0bf852743f4a037616)
1#!/usr/bin/env atf-sh
2#-
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2021 Alexander V. Chernikov
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29. $(atf_get_srcdir)/../common/vnet.subr
30
31atf_test_case "ndp_add_gu_success" "cleanup"
32ndp_add_gu_success_head() {
33	atf_set descr 'Test static ndp record addition'
34	atf_set require.user root
35}
36
37ndp_add_gu_success_body() {
38	local epair0 jname
39
40	vnet_init
41
42	jname="v6t-ndp_add_success"
43
44	epair0=$(vnet_mkepair)
45
46	vnet_mkjail ${jname} ${epair0}a
47	jexec ${jname} ndp -i ${epair0}a -- -disabled
48	jexec ${jname} ifconfig ${epair0}a up
49
50	jexec ${jname} ifconfig ${epair0}a inet6 2001:db8::1/64
51
52	# wait for DAD to complete
53	while [ `jexec ${jname} ifconfig | grep inet6 | grep -c tentative` != "0" ]; do
54		sleep 0.1
55	done
56
57	atf_check jexec ${jname} ndp -s 2001:db8::2 90:10:00:01:02:03
58
59	t=`jexec ${jname} ndp -an | grep 2001:db8::2 | awk '{print $1, $2, $3, $4}'`
60	if [ "${t}" != "2001:db8::2 90:10:00:01:02:03 ${epair0}a permanent" ]; then
61		atf_fail "Wrong output: ${t}"
62	fi
63	echo "T='${t}'"
64}
65
66ndp_add_gu_success_cleanup() {
67	vnet_cleanup
68}
69
70atf_test_case "ndp_del_gu_success" "cleanup"
71ndp_del_gu_success_head() {
72	atf_set descr 'Test ndp record deletion'
73	atf_set require.user root
74}
75
76ndp_del_gu_success_body() {
77	local epair0 jname
78
79	vnet_init
80
81	jname="v6t-ndp_del_gu_success"
82
83	epair0=$(vnet_mkepair)
84
85	vnet_mkjail ${jname} ${epair0}a
86
87	jexec ${jname} ndp -i ${epair0}a -- -disabled
88	jexec ${jname} ifconfig ${epair0}a up
89
90	jexec ${jname} ifconfig ${epair0}a inet6 2001:db8::1/64
91
92	# wait for DAD to complete
93	while [ `jexec ${jname} ifconfig | grep inet6 | grep -c tentative` != "0" ]; do
94		sleep 0.1
95	done
96
97	jexec ${jname} ping -c1 -t1 2001:db8::2
98
99	atf_check -o match:"2001:db8::2 \(2001:db8::2\) deleted" jexec ${jname} ndp -nd 2001:db8::2
100}
101
102ndp_del_gu_success_cleanup() {
103	vnet_cleanup
104}
105
106ndp_if_up()
107{
108	local ifname=$1
109	local jname=$2
110
111	if [ -n "$jname" ]; then
112		jname="jexec ${jname}"
113	fi
114	atf_check ${jname} ifconfig ${ifname} up
115	atf_check ${jname} ifconfig ${ifname} inet6 -ifdisabled
116	while ${jname} ifconfig ${ifname} inet6 | grep tentative; do
117		sleep 0.1
118	done
119}
120
121ndp_if_lladdr()
122{
123	local ifname=$1
124	local jname=$2
125
126	if [ -n "$jname" ]; then
127		jname="jexec ${jname}"
128	fi
129	${jname} ifconfig ${ifname} inet6 | \
130	    awk '/inet6 fe80:/{split($2, addr, "%"); print addr[1]}'
131}
132
133atf_test_case "ndp_slaac_default_route" "cleanup"
134ndp_slaac_default_route_head() {
135	atf_set descr 'Test default route installation via SLAAC'
136	atf_set require.user root
137	atf_set require.progs "python"
138}
139
140ndp_slaac_default_route_body() {
141	local epair0 jname lladdr
142
143	vnet_init
144
145	jname="v6t-ndp_slaac_default_route"
146
147	epair0=$(vnet_mkepair)
148
149	vnet_mkjail ${jname} ${epair0}a
150
151	ndp_if_up ${epair0}a ${jname}
152	ndp_if_up ${epair0}b
153	atf_check jexec ${jname} ifconfig ${epair0}a inet6 accept_rtadv
154
155        # Send an RA advertising a prefix.
156	atf_check -e ignore python $(atf_get_srcdir)/ra.py \
157	    --sendif ${epair0}b \
158	    --dst $(ndp_if_lladdr ${epair0}a ${jname}) \
159	    --src $(ndp_if_lladdr ${epair0}b) \
160	    --prefix "2001:db8:ffff:1000::" --prefixlen 64
161
162	# Wait for a default router to appear.
163	while [ -z "$(jexec ${jname} ndp -r)" ]; do
164		sleep 0.1
165	done
166	atf_check -o match:"^default[[:space:]]+fe80:" \
167	    jexec ${jname} netstat -rn -6
168
169	# Get rid of the default route.
170	jexec ${jname} route -6 flush
171	atf_check -o not-match:"^default[[:space:]]+fe80:" \
172	    jexec ${jname} netstat -rn -6
173
174	# Send another RA, make sure that the default route is installed again.
175	atf_check -e ignore python $(atf_get_srcdir)/ra.py \
176	    --sendif ${epair0}b \
177	    --dst $(ndp_if_lladdr ${epair0}a ${jname}) \
178	    --src $(ndp_if_lladdr ${epair0}b) \
179	    --prefix "2001:db8:ffff:1000::" --prefixlen 64
180	while [ -z "$(jexec ${jname} ndp -r)" ]; do
181		sleep 0.1
182	done
183	atf_check -o match:"^default[[:space:]]+fe80:" \
184	    jexec ${jname} netstat -rn -6
185}
186
187ndp_slaac_default_route_cleanup() {
188	vnet_cleanup
189}
190
191atf_init_test_cases()
192{
193	atf_add_test_case "ndp_add_gu_success"
194	atf_add_test_case "ndp_del_gu_success"
195	atf_add_test_case "ndp_slaac_default_route"
196}
197