Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tests/sys/netinet6/ndp.sh
103860 views
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
31
atf_test_case "ndp_add_gu_success" "cleanup"
32
ndp_add_gu_success_head() {
33
atf_set descr 'Test static ndp record addition'
34
atf_set require.user root
35
}
36
37
ndp_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
66
ndp_add_gu_success_cleanup() {
67
vnet_cleanup
68
}
69
70
atf_test_case "ndp_del_gu_success" "cleanup"
71
ndp_del_gu_success_head() {
72
atf_set descr 'Test ndp record deletion'
73
atf_set require.user root
74
}
75
76
ndp_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
102
ndp_del_gu_success_cleanup() {
103
vnet_cleanup
104
}
105
106
ndp_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
121
ndp_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
133
atf_test_case "ndp_slaac_default_route" "cleanup"
134
ndp_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 python3 scapy
138
}
139
140
ndp_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 python3 $(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 python3 $(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
187
ndp_slaac_default_route_cleanup() {
188
vnet_cleanup
189
}
190
191
atf_test_case "ndp_prefix_len_mismatch" "cleanup"
192
ndp_prefix_len_mismatch_head() {
193
atf_set descr 'Test RAs on an interface without a /64 lladdr'
194
atf_set require.user root
195
atf_set require.progs python3 scapy
196
}
197
198
ndp_prefix_len_mismatch_body() {
199
vnet_init
200
201
epair=$(vnet_mkepair)
202
203
vnet_mkjail alcatraz ${epair}a
204
205
jexec alcatraz ifconfig ${epair}a inet6 -auto_linklocal
206
jexec alcatraz ifconfig ${epair}a inet6 -ifdisabled
207
jexec alcatraz ifconfig ${epair}a inet6 accept_rtadv
208
jexec alcatraz ifconfig ${epair}a inet6 fe80::5a9c:fcff:fe10:5d07/127
209
jexec alcatraz ifconfig ${epair}a up
210
211
ifconfig ${epair}b inet6 -ifdisabled
212
ifconfig ${epair}b up
213
214
atf_check -e ignore python3 $(atf_get_srcdir)/ra.py \
215
--sendif ${epair}b \
216
--dst $(ndp_if_lladdr ${epair}a alcatraz) \
217
--src $(ndp_if_lladdr ${epair}b) \
218
--prefix "2001:db8:ffff:1000::" --prefixlen 64
219
220
atf_check \
221
-o match:"inet6 2001:db8:ffff:1000:.* prefixlen 64.*autoconf.*" \
222
jexec alcatraz ifconfig ${epair}a
223
}
224
225
ndp_prefix_len_mismatch_cleanup() {
226
vnet_cleanup
227
}
228
229
atf_test_case "ndp_prefix_lifetime" "cleanup"
230
ndp_prefix_lifetime_head() {
231
atf_set descr 'Test ndp slaac address lifetime handling'
232
atf_set require.user root
233
atf_set require.progs python3 scapy
234
}
235
236
ndp_prefix_lifetime_body() {
237
local epair0 jname prefix
238
239
vnet_init
240
241
jname="v6t-ndp_prefix_lifetime"
242
243
epair0=$(vnet_mkepair)
244
245
vnet_mkjail ${jname} ${epair0}a
246
247
ndp_if_up ${epair0}a ${jname}
248
ndp_if_up ${epair0}b
249
atf_check jexec ${jname} ifconfig ${epair0}a inet6 accept_rtadv no_dad
250
251
prefix="2001:db8:ffff:1000:"
252
253
# Send an RA advertising a prefix.
254
atf_check -e ignore python3 $(atf_get_srcdir)/ra.py \
255
--sendif ${epair0}b \
256
--dst $(ndp_if_lladdr ${epair0}a ${jname}) \
257
--src $(ndp_if_lladdr ${epair0}b) \
258
--prefix "2001:db8:ffff:1000::" --prefixlen 64 \
259
--validlifetime 10 --preferredlifetime 5
260
261
# Wait for a default router to appear.
262
while [ -z "$(jexec ${jname} ndp -r)" ]; do
263
sleep 0.1
264
done
265
atf_check \
266
-o match:"^default[[:space:]]+fe80:" \
267
jexec ${jname} netstat -rn -6
268
269
atf_check \
270
-o match:"inet6 ${prefix}.* prefixlen 64 autoconf pltime 5 vltime 10" \
271
jexec ${jname} ifconfig ${epair0}a
272
273
# Wait for the address to become deprecated.
274
sleep 6
275
atf_check \
276
-o match:"inet6 ${prefix}.* prefixlen 64 deprecated autoconf pltime 0 vltime [1-9]+" \
277
jexec ${jname} ifconfig -L ${epair0}a
278
279
# Wait for the address to expire.
280
sleep 6
281
atf_check \
282
-o not-match:"inet6 ${prefix}.*" \
283
jexec ${jname} ifconfig ${epair0}a
284
}
285
286
ndp_prefix_lifetime_cleanup() {
287
vnet_cleanup
288
}
289
290
atf_test_case "ndp_prefix_lifetime_extend"
291
ndp_prefix_lifetime_extend_head() {
292
atf_set descr 'Test prefix lifetime updates via ifconfig'
293
atf_set require.user root
294
atf_set require.progs jq
295
}
296
297
get_prefix_attr() {
298
local prefix=$1
299
local attr=$2
300
301
ndp -p --libxo json | \
302
jq -r '.ndp.["prefix-list"][] |
303
select(.prefix == "'${prefix}'") | .["'${attr}'"]'
304
}
305
306
# Given a prefix, return its expiry time in seconds.
307
prefix_expiry() {
308
get_prefix_attr $1 "expires_sec"
309
}
310
311
# Given a prefix, return its valid and preferred lifetimes.
312
prefix_lifetimes() {
313
local p v
314
315
v=$(get_prefix_attr $1 "valid-lifetime")
316
p=$(get_prefix_attr $1 "preferred-lifetime")
317
echo $v $p
318
}
319
320
ndp_prefix_lifetime_extend_body() {
321
local epair ex1 ex2 ex3 prefix pltime vltime
322
323
atf_check -o save:epair ifconfig epair create
324
epair=$(cat epair)
325
atf_check ifconfig ${epair} up
326
327
prefix="2001:db8:ffff:1000::"
328
329
atf_check ifconfig ${epair} inet6 ${prefix}1/64 pltime 5 vltime 10
330
t=$(prefix_lifetimes ${prefix}/64)
331
if [ "${t}" != "10 5" ]; then
332
atf_fail "Unexpected lifetimes: ${t}"
333
fi
334
ex1=$(prefix_expiry ${prefix}/64)
335
if [ "${ex1}" -gt 10 ]; then
336
atf_fail "Unexpected expiry time: ${ex1}"
337
fi
338
339
# Double the address lifetime and verify that the prefix is
340
# updated.
341
atf_check ifconfig ${epair} inet6 ${prefix}1/64 pltime 10 vltime 20
342
t=$(prefix_lifetimes ${prefix}/64)
343
if [ "${t}" != "20 10" ]; then
344
atf_fail "Unexpected lifetimes: ${t}"
345
fi
346
ex2=$(prefix_expiry ${prefix}/64)
347
if [ "${ex2}" -le "${ex1}" ]; then
348
atf_fail "Expiry time was not extended: ${ex1} <= ${ex2}"
349
fi
350
351
# Add a second address from the same prefix with a shorter
352
# lifetime, and make sure that the prefix lifetime is not
353
# shortened.
354
atf_check ifconfig ${epair} inet6 ${prefix}2/64 pltime 5 vltime 10
355
t=$(prefix_lifetimes ${prefix}/64)
356
if [ "${t}" != "20 10" ]; then
357
atf_fail "Unexpected lifetimes: ${t}"
358
fi
359
ex3=$(prefix_expiry ${prefix}/64)
360
if [ "${ex3}" -lt "${ex2}" ]; then
361
atf_fail "Expiry time was shortened: ${ex2} <= ${ex3}"
362
fi
363
}
364
365
atf_init_test_cases()
366
{
367
atf_add_test_case "ndp_add_gu_success"
368
atf_add_test_case "ndp_del_gu_success"
369
atf_add_test_case "ndp_slaac_default_route"
370
atf_add_test_case "ndp_prefix_len_mismatch"
371
atf_add_test_case "ndp_prefix_lifetime"
372
atf_add_test_case "ndp_prefix_lifetime_extend"
373
}
374
375