xref: /netbsd-src/tests/net/arp/t_arp.sh (revision fa28c6faa16e0b00edee7acdcaf4899797043def)
1#	$NetBSD: t_arp.sh,v 1.10 2015/12/02 06:05:14 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
31SOCKSRC=unix://commsock1
32SOCKDST=unix://commsock2
33IP4SRC=10.0.1.1
34IP4DST=10.0.1.2
35
36DEBUG=false
37TIMEOUT=1
38
39atf_test_case cache_expiration_5s cleanup
40atf_test_case cache_expiration_10s cleanup
41atf_test_case command cleanup
42atf_test_case garp cleanup
43atf_test_case cache_overwriting cleanup
44
45cache_expiration_5s_head()
46{
47	atf_set "descr" "Tests for ARP cache expiration (5s)"
48	atf_set "require.progs" "rump_server"
49}
50
51cache_expiration_10s_head()
52{
53	atf_set "descr" "Tests for ARP cache expiration (10s)"
54	atf_set "require.progs" "rump_server"
55}
56
57command_head()
58{
59	atf_set "descr" "Tests for commands of arp(8)"
60	atf_set "require.progs" "rump_server"
61}
62
63garp_head()
64{
65	atf_set "descr" "Tests for GARP"
66	atf_set "require.progs" "rump_server"
67}
68
69cache_overwriting_head()
70{
71	atf_set "descr" "Tests for behavior of overwriting ARP caches"
72	atf_set "require.progs" "rump_server"
73}
74
75setup_dst_server()
76{
77	export RUMP_SERVER=$SOCKDST
78	atf_check -s exit:0 rump.ifconfig shmif0 create
79	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
80	atf_check -s exit:0 rump.ifconfig shmif0 inet $IP4DST/24
81	atf_check -s exit:0 rump.ifconfig shmif0 up
82	atf_check -s exit:0 rump.ifconfig -w 10
83
84	$DEBUG && rump.ifconfig shmif0
85	$DEBUG && rump.arp -n -a
86}
87
88setup_src_server()
89{
90	local keep=$1
91
92	export RUMP_SERVER=$SOCKSRC
93
94	# Adjust ARP parameters
95	atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.arp.keep=$keep
96
97	# Setup an interface
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 $IP4SRC/24
101	atf_check -s exit:0 rump.ifconfig shmif0 up
102	atf_check -s exit:0 rump.ifconfig -w 10
103
104	# Sanity check
105	$DEBUG && rump.ifconfig shmif0
106	$DEBUG && rump.arp -n -a
107	atf_check -s exit:0 -o ignore rump.arp -n $IP4SRC
108	atf_check -s not-exit:0 -e ignore rump.arp -n $IP4DST
109}
110
111test_cache_expiration()
112{
113	local arp_keep=$1
114	local bonus=2
115
116	atf_check -s exit:0 ${inetserver} $SOCKSRC
117	atf_check -s exit:0 ${inetserver} $SOCKDST
118
119	setup_dst_server
120	setup_src_server $arp_keep
121
122	#
123	# Check if a cache is expired expectedly
124	#
125	export RUMP_SERVER=$SOCKSRC
126	atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP4DST
127
128	$DEBUG && rump.arp -n -a
129	atf_check -s exit:0 -o ignore rump.arp -n $IP4SRC
130	# Should be cached
131	atf_check -s exit:0 -o ignore rump.arp -n $IP4DST
132
133	atf_check -s exit:0 sleep $(($arp_keep + $bonus))
134
135	$DEBUG && rump.arp -n -a
136	atf_check -s exit:0 -o ignore rump.arp -n $IP4SRC
137	# Should be expired
138	atf_check -s not-exit:0 -e ignore rump.arp -n $IP4DST
139}
140
141cache_expiration_5s_body()
142{
143	test_cache_expiration 5
144}
145
146cache_expiration_10s_body()
147{
148	test_cache_expiration 10
149}
150
151command_body()
152{
153	local arp_keep=5
154	local bonus=2
155
156	atf_check -s exit:0 ${inetserver} $SOCKSRC
157	atf_check -s exit:0 ${inetserver} $SOCKDST
158
159	setup_dst_server
160	setup_src_server $arp_keep
161
162	export RUMP_SERVER=$SOCKSRC
163
164	# Add and delete a static entry
165	$DEBUG && rump.arp -n -a
166	atf_check -s exit:0 -o ignore rump.arp -s 10.0.1.10 b2:a0:20:00:00:10
167	$DEBUG && rump.arp -n -a
168	atf_check -s exit:0 -o match:'b2:a0:20:00:00:10' rump.arp -n 10.0.1.10
169	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.10
170	atf_check -s exit:0 -o ignore rump.arp -d 10.0.1.10
171	$DEBUG && rump.arp -n -a
172	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.10
173
174	# Add multiple entries via a file
175	cat - > ./list <<-EOF
176	10.0.1.11 b2:a0:20:00:00:11
177	10.0.1.12 b2:a0:20:00:00:12
178	10.0.1.13 b2:a0:20:00:00:13
179	10.0.1.14 b2:a0:20:00:00:14
180	10.0.1.15 b2:a0:20:00:00:15
181	EOF
182	$DEBUG && rump.arp -n -a
183	atf_check -s exit:0 -o ignore rump.arp -f ./list
184	$DEBUG && rump.arp -n -a
185	atf_check -s exit:0 -o match:'b2:a0:20:00:00:11' rump.arp -n 10.0.1.11
186	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.11
187	atf_check -s exit:0 -o match:'b2:a0:20:00:00:12' rump.arp -n 10.0.1.12
188	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.12
189	atf_check -s exit:0 -o match:'b2:a0:20:00:00:13' rump.arp -n 10.0.1.13
190	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.13
191	atf_check -s exit:0 -o match:'b2:a0:20:00:00:14' rump.arp -n 10.0.1.14
192	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.14
193	atf_check -s exit:0 -o match:'b2:a0:20:00:00:15' rump.arp -n 10.0.1.15
194	atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.15
195
196	# Test arp -a
197	atf_check -s exit:0 -o match:'10.0.1.11' rump.arp -n -a
198	atf_check -s exit:0 -o match:'10.0.1.12' rump.arp -n -a
199	atf_check -s exit:0 -o match:'10.0.1.13' rump.arp -n -a
200	atf_check -s exit:0 -o match:'10.0.1.14' rump.arp -n -a
201	atf_check -s exit:0 -o match:'10.0.1.15' rump.arp -n -a
202
203	# Flush all entries
204	$DEBUG && rump.arp -n -a
205	atf_check -s exit:0 -o ignore rump.arp -d -a
206	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.11
207	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.12
208	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.13
209	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.14
210	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.15
211	atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.1
212
213	# Test temp option
214	$DEBUG && rump.arp -n -a
215	atf_check -s exit:0 -o ignore rump.arp -s 10.0.1.10 b2:a0:20:00:00:10 temp
216	$DEBUG && rump.arp -n -a
217	atf_check -s exit:0 -o match:'b2:a0:20:00:00:10' rump.arp -n 10.0.1.10
218	atf_check -s exit:0 -o not-match:'permanent' rump.arp -n 10.0.1.10
219
220	# Hm? the cache doesn't expire...
221	atf_check -s exit:0 sleep $(($arp_keep + $bonus))
222	$DEBUG && rump.arp -n -a
223	#atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.10
224
225	return 0
226}
227
228make_pkt_str()
229{
230	local target=$1
231	local sender=$2
232	pkt="> ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42:"
233	pkt="$pkt Request who-has $target tell $sender, length 28"
234	echo $pkt
235}
236
237garp_body()
238{
239	local pkt=
240
241	atf_check -s exit:0 ${inetserver} $SOCKSRC
242	export RUMP_SERVER=$SOCKSRC
243
244	# Setup an interface
245	atf_check -s exit:0 rump.ifconfig shmif0 create
246	atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
247	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.1/24
248	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.2/24 alias
249	atf_check -s exit:0 rump.ifconfig shmif0 up
250	$DEBUG && rump.ifconfig shmif0
251
252	atf_check -s exit:0 sleep 1
253	shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r - > ./out
254
255	# A GARP packet is sent for the primary address
256	pkt=$(make_pkt_str 10.0.0.1 10.0.0.1)
257	atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
258	# No GARP packet is sent for the alias address
259	pkt=$(make_pkt_str 10.0.0.2 10.0.0.2)
260	atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
261
262	atf_check -s exit:0 rump.ifconfig -w 10
263	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.3/24
264	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.4/24 alias
265
266	# No GARP packets are sent during IFF_UP
267	shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r - > ./out
268	pkt=$(make_pkt_str 10.0.0.3 10.0.0.3)
269	atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
270	pkt=$(make_pkt_str 10.0.0.4 10.0.0.4)
271	atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
272}
273
274cache_overwriting_body()
275{
276	local arp_keep=5
277	local bonus=2
278
279	atf_check -s exit:0 ${inetserver} $SOCKSRC
280	atf_check -s exit:0 ${inetserver} $SOCKDST
281
282	setup_dst_server
283	setup_src_server $arp_keep
284
285	export RUMP_SERVER=$SOCKSRC
286
287	# Cannot overwrite a permanent cache
288	atf_check -s not-exit:0 -e match:'File exists' \
289	    rump.arp -s $IP4SRC b2:a0:20:00:00:ff
290	$DEBUG && rump.arp -n -a
291
292	atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP4DST
293	$DEBUG && rump.arp -n -a
294	# Can overwrite a dynamic cache
295	atf_check -s exit:0 -o ignore rump.arp -s $IP4DST b2:a0:20:00:00:00
296	$DEBUG && rump.arp -n -a
297	atf_check -s exit:0 -o match:'b2:a0:20:00:00:00' rump.arp -n $IP4DST
298	atf_check -s exit:0 -o match:'permanent' rump.arp -n $IP4DST
299
300	atf_check -s exit:0 -o ignore rump.arp -s 10.0.1.10 b2:a0:20:00:00:10 temp
301	$DEBUG && rump.arp -n -a
302	atf_check -s exit:0 -o match:'b2:a0:20:00:00:10' rump.arp -n 10.0.1.10
303	atf_check -s exit:0 -o not-match:'permanent' rump.arp -n 10.0.1.10
304	# Cannot overwrite a temp cache
305	atf_check -s not-exit:0 -e match:'File exists' \
306	    rump.arp -s 10.0.1.10 b2:a0:20:00:00:ff
307	$DEBUG && rump.arp -n -a
308
309	return 0
310}
311
312cleanup()
313{
314	env RUMP_SERVER=$SOCKSRC rump.halt
315	env RUMP_SERVER=$SOCKDST rump.halt
316}
317
318dump_src()
319{
320	export RUMP_SERVER=$SOCKSRC
321	rump.netstat -nr
322	rump.arp -n -a
323	rump.ifconfig
324	$HIJACKING dmesg
325}
326
327dump_dst()
328{
329	export RUMP_SERVER=$SOCKDST
330	rump.netstat -nr
331	rump.arp -n -a
332	rump.ifconfig
333	$HIJACKING dmesg
334}
335
336dump()
337{
338	dump_src
339	dump_dst
340	shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
341}
342
343cache_expiration_5s_cleanup()
344{
345	$DEBUG && dump
346	cleanup
347}
348
349cache_expiration_10s_cleanup()
350{
351	$DEBUG && dump
352	cleanup
353}
354
355command_cleanup()
356{
357	$DEBUG && dump
358	cleanup
359}
360
361garp_cleanup()
362{
363	$DEBUG && dump_src
364	$DEBUG && shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
365	env RUMP_SERVER=$SOCKSRC rump.halt
366}
367
368cache_overwriting_cleanup()
369{
370	$DEBUG && dump
371	cleanup
372}
373
374atf_init_test_cases()
375{
376	atf_add_test_case cache_expiration_5s
377	atf_add_test_case cache_expiration_10s
378	atf_add_test_case command
379	atf_add_test_case garp
380	atf_add_test_case cache_overwriting
381}
382