xref: /minix3/tests/net/arp/t_dad.sh (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1#	$NetBSD: t_dad.sh,v 1.6 2015/08/25 02:24:34 ozaki-r Exp $
2#
3# Copyright (c) 2015 The NetBSD Foundation, Inc.
4# All rights reserved.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28inetserver="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif"
29HIJACKING="env LD_PRELOAD=/usr/lib/librumphijack.so RUMPHIJACK=sysctl=yes"
30
31SOCKLOCAL=unix://commsock1
32SOCKPEER=unix://commsock2
33
34DEBUG=false
35
36atf_test_case dad_basic cleanup
37atf_test_case dad_duplicated cleanup
38
39dad_basic_head()
40{
41	atf_set "descr" "Tests for IPv4 DAD basic behavior"
42	atf_set "require.progs" "rump_server"
43}
44
45dad_duplicated_head()
46{
47	atf_set "descr" "Tests for IPv4 DAD duplicated state"
48	atf_set "require.progs" "rump_server"
49}
50
51setup_server()
52{
53	local sock=$1
54	local ip=$2
55
56	export RUMP_SERVER=$sock
57
58	atf_check -s exit:0 rump.ifconfig shmif0 create
59	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
60	atf_check -s exit:0 rump.ifconfig shmif0 inet $ip/24
61	atf_check -s exit:0 rump.ifconfig shmif0 up
62	atf_check -s exit:0 rump.ifconfig -w 10
63
64	$DEBUG && rump.ifconfig shmif0
65}
66
67make_pkt_str()
68{
69	local target=$1
70	local sender=$2
71	pkt="> ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42:"
72	pkt="$pkt Request who-has $target tell $sender, length 28"
73	echo $pkt
74}
75
76extract_new_packets()
77{
78	local old=./old
79
80	if [ ! -f $old ]; then
81		old=/dev/null
82	fi
83
84	shmif_dumpbus -p - bus1 2>/dev/null| \
85	    tcpdump -n -e -r - 2>/dev/null > ./new
86	diff -u $old ./new |grep '^+' |cut -d '+' -f 2 > ./diff
87	mv -f ./new ./old
88	cat ./diff
89}
90
91dad_basic_body()
92{
93	local pkt=
94
95	atf_check -s exit:0 ${inetserver} $SOCKLOCAL
96	export RUMP_SERVER=$SOCKLOCAL
97
98	atf_check -s exit:0 rump.ifconfig shmif0 create
99	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
100	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.1/24
101	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.2/24 alias
102	$DEBUG && rump.ifconfig shmif0
103
104	atf_check -s exit:0 rump.ifconfig shmif0 up
105	rump.ifconfig shmif0 > ./out
106	$DEBUG && cat ./out
107
108	# The primary address doesn't start with tentative state
109	atf_check -s not-exit:0 -x "cat ./out |grep 10.0.0.1 |grep -q tentative"
110	# The alias address starts with tentative state
111	# XXX we have no stable way to check this, so skip for now
112	#atf_check -s exit:0 -x "cat ./out |grep 10.0.0.2 |grep -q tentative"
113
114	atf_check -s exit:0 sleep 2
115	extract_new_packets > ./out
116	$DEBUG && cat ./out
117
118	# Check DAD probe packets
119	pkt=$(make_pkt_str 10.0.0.2 0.0.0.0)
120	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
121	# No DAD for the primary address
122	pkt=$(make_pkt_str 10.0.0.1 0.0.0.0)
123	atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
124
125	# Waiting for DAD complete
126	atf_check -s exit:0 rump.ifconfig -w 10
127	# Give a chance to send a DAD announce packet
128	atf_check -s exit:0 sleep 1
129	extract_new_packets > ./out
130	$DEBUG && cat ./out
131
132	# Check the DAD announce packet
133	pkt=$(make_pkt_str 10.0.0.2 10.0.0.2)
134	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
135	# The alias address left tentative
136	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep 10.0.0.2 |grep -q tentative"
137
138	#
139	# Add a new address on the fly
140	#
141	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.3/24 alias
142
143	# The new address starts with tentative state
144	# XXX we have no stable way to check this, so skip for now
145	#atf_check -s exit:0 -x "rump.ifconfig shmif0 |grep 10.0.0.3 |grep -q tentative"
146
147	# Check DAD probe packets
148	atf_check -s exit:0 sleep 2
149	extract_new_packets > ./out
150	$DEBUG && cat ./out
151	pkt=$(make_pkt_str 10.0.0.3 0.0.0.0)
152	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
153
154	# Waiting for DAD complete
155	atf_check -s exit:0 rump.ifconfig -w 10
156	# Give a chance to send a DAD announce packet
157	atf_check -s exit:0 sleep 1
158	extract_new_packets > ./out
159	$DEBUG && cat ./out
160
161	# Check the DAD announce packet
162	pkt=$(make_pkt_str 10.0.0.3 10.0.0.3)
163	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
164	# The new address left tentative
165	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep 10.0.0.3 |grep -q tentative"
166}
167
168dad_duplicated_body()
169{
170	local localip1=10.0.1.1
171	local localip2=10.0.1.11
172	local peerip=10.0.1.2
173
174	atf_check -s exit:0 ${inetserver} $SOCKLOCAL
175	atf_check -s exit:0 ${inetserver} $SOCKPEER
176
177	setup_server $SOCKLOCAL $localip1
178	setup_server $SOCKPEER $peerip
179
180	export RUMP_SERVER=$SOCKLOCAL
181
182	# The primary address isn't marked as duplicated
183	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep $localip1 |grep -q duplicated"
184
185	#
186	# Add a new address duplicated with the peer server
187	#
188	atf_check -s exit:0 rump.ifconfig shmif0 inet $peerip alias
189	atf_check -s exit:0 sleep 1
190
191	# The new address is marked as duplicated
192	atf_check -s exit:0 -x "rump.ifconfig shmif0 |grep $peerip |grep -q duplicated"
193
194	# A unique address isn't marked as duplicated
195	atf_check -s exit:0 rump.ifconfig shmif0 inet $localip2 alias
196	atf_check -s exit:0 sleep 1
197	atf_check -s not-exit:0 -x "rump.ifconfig shmif0 |grep $localip2 |grep -q duplicated"
198}
199
200cleanup()
201{
202	env RUMP_SERVER=$SOCKLOCAL rump.halt
203	env RUMP_SERVER=$SOCKPEER rump.halt
204}
205
206dump_local()
207{
208	export RUMP_SERVER=$SOCKLOCAL
209	rump.netstat -nr
210	rump.arp -n -a
211	rump.ifconfig
212	$HIJACKING dmesg
213}
214
215dump_peer()
216{
217	export RUMP_SERVER=$SOCKPEER
218	rump.netstat -nr
219	rump.arp -n -a
220	rump.ifconfig
221	$HIJACKING dmesg
222}
223
224dump()
225{
226	dump_local
227	dump_peer
228	shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
229}
230
231dad_basic_cleanup()
232{
233	$DEBUG && dump_local
234	$DEBUG && shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
235	env RUMP_SERVER=$SOCKLOCAL rump.halt
236}
237
238dad_duplicated_cleanup()
239{
240	$DEBUG && dump
241	cleanup
242}
243
244atf_init_test_cases()
245{
246	atf_add_test_case dad_basic
247	atf_add_test_case dad_duplicated
248}
249