t_arp.sh revision 1.2 1 # $NetBSD: t_arp.sh,v 1.2 2015/07/30 02:51:05 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 # Flush all entries
191 $DEBUG && rump.arp -n -a
192 atf_check -s exit:0 -o ignore rump.arp -d -a
193 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.11
194 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.12
195 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.13
196 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.14
197 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.15
198 atf_check -s not-exit:0 -e ignore rump.arp -n 10.0.1.1
199 }
200
201 make_pkt_str()
202 {
203 local target=$1
204 local sender=$2
205 pkt="> ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42:"
206 pkt="$pkt Request who-has $target tell $sender, length 28"
207 echo $pkt
208 }
209
210 garp_body()
211 {
212 local pkt=
213
214 atf_check -s exit:0 ${inetserver} $SOCKSRC
215 export RUMP_SERVER=$SOCKSRC
216
217 # Setup an interface
218 atf_check -s exit:0 rump.ifconfig shmif0 create
219 atf_check -s exit:0 rump.ifconfig shmif0 linkstr bus1
220 atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.1/24
221 atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.2/24 alias
222 atf_check -s exit:0 rump.ifconfig shmif0 up
223 $DEBUG && rump.ifconfig shmif0
224
225 atf_check -s exit:0 sleep 1
226 shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r - > ./out
227
228 # A GARP packet is sent for the primary address
229 pkt=$(make_pkt_str 10.0.0.1 10.0.0.1)
230 atf_check -s exit:0 -x "cat ./out |grep -q '$pkt'"
231 # No GARP packet is sent for the alias address
232 pkt=$(make_pkt_str 10.0.0.2 10.0.0.2)
233 atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
234
235 atf_check -s exit:0 rump.ifconfig -w 10
236 atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.3/24
237 atf_check -s exit:0 rump.ifconfig shmif0 inet 10.0.0.4/24 alias
238
239 # No GARP packets are sent during IFF_UP
240 shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r - > ./out
241 pkt=$(make_pkt_str 10.0.0.3 10.0.0.3)
242 atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
243 pkt=$(make_pkt_str 10.0.0.4 10.0.0.4)
244 atf_check -s not-exit:0 -x "cat ./out |grep -q '$pkt'"
245 }
246
247 cleanup()
248 {
249 env RUMP_SERVER=$SOCKSRC rump.halt
250 env RUMP_SERVER=$SOCKDST rump.halt
251 }
252
253 dump_src()
254 {
255 export RUMP_SERVER=$SOCKSRC
256 rump.netstat -nr
257 rump.arp -n -a
258 rump.ifconfig
259 $HIJACKING dmesg
260 }
261
262 dump_dst()
263 {
264 export RUMP_SERVER=$SOCKDST
265 rump.netstat -nr
266 rump.arp -n -a
267 rump.ifconfig
268 $HIJACKING dmesg
269 }
270
271 dump()
272 {
273 dump_src
274 dump_dst
275 shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
276 }
277
278 cache_expiration_5s_cleanup()
279 {
280 $DEBUG && dump
281 cleanup
282 }
283
284 cache_expiration_10s_cleanup()
285 {
286 $DEBUG && dump
287 cleanup
288 }
289
290 command_cleanup()
291 {
292 $DEBUG && dump
293 cleanup
294 }
295
296 garp_cleanup()
297 {
298 $DEBUG && dump_src
299 $DEBUG && shmif_dumpbus -p - bus1 2>/dev/null| tcpdump -n -e -r -
300 env RUMP_SERVER=$SOCKSRC rump.halt
301 }
302
303 atf_init_test_cases()
304 {
305 atf_add_test_case cache_expiration_5s
306 atf_add_test_case cache_expiration_10s
307 atf_add_test_case command
308 atf_add_test_case garp
309 }
310