t_arp.sh revision 1.3 1 # $NetBSD: t_arp.sh,v 1.3 2015/07/30 08:41:18 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
28 inetserver="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif"
29 HIJACKING="env LD_PRELOAD=/usr/lib/librumphijack.so RUMPHIJACK=sysctl=yes"
30
31 SOCKSRC=unix://commsock1
32 SOCKDST=unix://commsock2
33 IP4SRC=10.0.1.1
34 IP4DST=10.0.1.2
35
36 DEBUG=false
37 TIMEOUT=1
38
39 atf_test_case cache_expiration_5s cleanup
40 atf_test_case cache_expiration_10s cleanup
41 atf_test_case command cleanup
42 atf_test_case garp cleanup
43
44 cache_expiration_5s_head()
45 {
46 atf_set "descr" "Tests for ARP cache expiration (5s)"
47 atf_set "require.progs" "rump_server"
48 }
49
50 cache_expiration_10s_head()
51 {
52 atf_set "descr" "Tests for ARP cache expiration (10s)"
53 atf_set "require.progs" "rump_server"
54 }
55
56 command_head()
57 {
58 atf_set "descr" "Tests for commands of arp(8)"
59 atf_set "require.progs" "rump_server"
60 }
61
62 garp_head()
63 {
64 atf_set "descr" "Tests for GARP"
65 atf_set "require.progs" "rump_server"
66 }
67
68 setup_dst_server()
69 {
70 export RUMP_SERVER=$SOCKDST
71 atf_check -s exit:0 rump.ifconfig shmif0 create
72 atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
73 atf_check -s exit:0 rump.ifconfig shmif0 inet $IP4DST/24
74 atf_check -s exit:0 rump.ifconfig shmif0 up
75 atf_check -s exit:0 rump.ifconfig -w 10
76
77 $DEBUG && rump.ifconfig shmif0
78 $DEBUG && rump.arp -n -a
79 }
80
81 setup_src_server()
82 {
83 local prune=$1
84 local keep=$2
85
86 export RUMP_SERVER=$SOCKSRC
87
88 # Adjust ARP parameters
89 atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.arp.prune=$prune
90 atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.arp.keep=$keep
91 # Don't refresh to test expiration easily
92 atf_check -s exit:0 -o ignore rump.sysctl -w net.inet.arp.refresh=0
93
94 # Setup an interface
95 atf_check -s exit:0 rump.ifconfig shmif0 create
96 atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
97 atf_check -s exit:0 rump.ifconfig shmif0 inet $IP4SRC/24
98 atf_check -s exit:0 rump.ifconfig shmif0 up
99 atf_check -s exit:0 rump.ifconfig -w 10
100
101 # Sanity check
102 $DEBUG && rump.ifconfig shmif0
103 $DEBUG && rump.arp -n -a
104 atf_check -s exit:0 -o ignore rump.arp -n $IP4SRC
105 atf_check -s not-exit:0 -e ignore rump.arp -n $IP4DST
106 }
107
108 test_cache_expiration()
109 {
110 local arp_prune=1
111 local arp_keep=$1
112 local bonus=2
113
114 atf_check -s exit:0 ${inetserver} $SOCKSRC
115 atf_check -s exit:0 ${inetserver} $SOCKDST
116
117 setup_dst_server
118 setup_src_server $arp_prune $arp_keep
119
120 #
121 # Check if a cache is expired expectedly
122 #
123 export RUMP_SERVER=$SOCKSRC
124 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP4DST
125
126 $DEBUG && rump.arp -n -a
127 atf_check -s exit:0 -o ignore rump.arp -n $IP4SRC
128 # Should be cached
129 atf_check -s exit:0 -o ignore rump.arp -n $IP4DST
130
131 atf_check -s exit:0 sleep $(($arp_keep + $arp_prune + $bonus))
132
133 $DEBUG && rump.arp -n -a
134 atf_check -s exit:0 -o ignore rump.arp -n $IP4SRC
135 # Should be expired
136 #atf_check -s not-exit:0 -e ignore rump.arp -n $IP4DST
137 atf_check -s exit:0 -o match:'incomplete' rump.arp -n $IP4DST
138 }
139
140 cache_expiration_5s_body()
141 {
142 test_cache_expiration 5
143 }
144
145 cache_expiration_10s_body()
146 {
147 test_cache_expiration 10
148 }
149
150 command_body()
151 {
152 local arp_prune=1
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_prune $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:'permanent' rump.arp -n 10.0.1.10
169 atf_check -s exit:0 -o ignore rump.arp -d 10.0.1.10
170 $DEBUG && rump.arp -n -a
171 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.10
172
173 # Add multiple entries via a file
174 cat - > ./list <<-EOF
175 10.0.1.11 b2:a0:20:00:00:11
176 10.0.1.12 b2:a0:20:00:00:12
177 10.0.1.13 b2:a0:20:00:00:13
178 10.0.1.14 b2:a0:20:00:00:14
179 10.0.1.15 b2:a0:20:00:00:15
180 EOF
181 $DEBUG && rump.arp -n -a
182 atf_check -s exit:0 -o ignore rump.arp -f ./list
183 $DEBUG && rump.arp -n -a
184 atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.11
185 atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.12
186 atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.13
187 atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.14
188 atf_check -s exit:0 -o match:'permanent' rump.arp -n 10.0.1.15
189
190 # Test arp -a
191 atf_check -s exit:0 -o match:'10.0.1.11' rump.arp -n -a
192 atf_check -s exit:0 -o match:'10.0.1.12' rump.arp -n -a
193 atf_check -s exit:0 -o match:'10.0.1.13' rump.arp -n -a
194 atf_check -s exit:0 -o match:'10.0.1.14' rump.arp -n -a
195 atf_check -s exit:0 -o match:'10.0.1.15' rump.arp -n -a
196
197 # Flush all entries
198 $DEBUG && rump.arp -n -a
199 atf_check -s exit:0 -o ignore rump.arp -d -a
200 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.11
201 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.12
202 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.13
203 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.14
204 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.15
205 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.1
206 }
207
208 make_pkt_str()
209 {
210 local target=$1
211 local sender=$2
212 pkt="> ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42:"
213 pkt="$pkt Request who-has $target tell $sender, length 28"
214 echo $pkt
215 }
216
217 garp_body()
218 {
219 local pkt=
220
221 atf_check -s exit:0 ${inetserver} $SOCKSRC
222 export RUMP_SERVER=$SOCKSRC
223
224 # Setup an interface
225 atf_check -s exit:0 rump.ifconfig shmif0 create
226 atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
227 atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.1/24
228 atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.2/24 alias
229 atf_check -s exit:0 rump.ifconfig shmif0 up
230 $DEBUG && rump.ifconfig shmif0
231
232 atf_check -s exit:0 sleep 1
233 shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r - > ./out
234
235 # A GARP packet is sent for the primary address
236 pkt=$(make_pkt_str 10.0.0.1 10.0.0.1)
237 atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
238 # No GARP packet is sent for the alias address
239 pkt=$(make_pkt_str 10.0.0.2 10.0.0.2)
240 atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
241
242 atf_check -s exit:0 rump.ifconfig -w 10
243 atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.3/24
244 atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.4/24 alias
245
246 # No GARP packets are sent during IFF_UP
247 shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r - > ./out
248 pkt=$(make_pkt_str 10.0.0.3 10.0.0.3)
249 atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
250 pkt=$(make_pkt_str 10.0.0.4 10.0.0.4)
251 atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
252 }
253
254 cleanup()
255 {
256 env RUMP_SERVER=$SOCKSRC rump.halt
257 env RUMP_SERVER=$SOCKDST rump.halt
258 }
259
260 dump_src()
261 {
262 export RUMP_SERVER=$SOCKSRC
263 rump.netstat -nr
264 rump.arp -n -a
265 rump.ifconfig
266 $HIJACKING dmesg
267 }
268
269 dump_dst()
270 {
271 export RUMP_SERVER=$SOCKDST
272 rump.netstat -nr
273 rump.arp -n -a
274 rump.ifconfig
275 $HIJACKING dmesg
276 }
277
278 dump()
279 {
280 dump_src
281 dump_dst
282 shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
283 }
284
285 cache_expiration_5s_cleanup()
286 {
287 $DEBUG && dump
288 cleanup
289 }
290
291 cache_expiration_10s_cleanup()
292 {
293 $DEBUG && dump
294 cleanup
295 }
296
297 command_cleanup()
298 {
299 $DEBUG && dump
300 cleanup
301 }
302
303 garp_cleanup()
304 {
305 $DEBUG && dump_src
306 $DEBUG && shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
307 env RUMP_SERVER=$SOCKSRC rump.halt
308 }
309
310 atf_init_test_cases()
311 {
312 atf_add_test_case cache_expiration_5s
313 atf_add_test_case cache_expiration_10s
314 atf_add_test_case command
315 atf_add_test_case garp
316 }
317