t_misc.sh revision 1.5 1 1.5 riastrad # $NetBSD: t_misc.sh,v 1.5 2020/08/31 20:32:58 riastradh Exp $
2 1.1 riastrad #
3 1.1 riastrad # Copyright (c) 2018 Ryota Ozaki <ozaki.ryota (at] gmail.com>
4 1.1 riastrad # All rights reserved.
5 1.1 riastrad #
6 1.1 riastrad # Redistribution and use in source and binary forms, with or without
7 1.1 riastrad # modification, are permitted provided that the following conditions
8 1.1 riastrad # are met:
9 1.1 riastrad # 1. Redistributions of source code must retain the above copyright
10 1.1 riastrad # notice, this list of conditions and the following disclaimer.
11 1.1 riastrad # 2. Redistributions in binary form must reproduce the above copyright
12 1.1 riastrad # notice, this list of conditions and the following disclaimer in the
13 1.1 riastrad # documentation and/or other materials provided with the distribution.
14 1.1 riastrad #
15 1.1 riastrad # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 riastrad # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 riastrad # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 riastrad # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 riastrad # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 riastrad # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 riastrad # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 riastrad # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 riastrad # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 riastrad # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 riastrad # POSSIBILITY OF SUCH DAMAGE.
26 1.1 riastrad #
27 1.1 riastrad
28 1.1 riastrad BUS=bus
29 1.1 riastrad SOCK_LOCAL=unix://wg_local
30 1.1 riastrad SOCK_PEER=unix://wg_peer
31 1.1 riastrad
32 1.1 riastrad
33 1.1 riastrad atf_test_case wg_rekey cleanup
34 1.1 riastrad wg_rekey_head()
35 1.1 riastrad {
36 1.1 riastrad
37 1.1 riastrad atf_set "descr" "tests of rekeying of wg(4)"
38 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
39 1.1 riastrad }
40 1.1 riastrad
41 1.1 riastrad wg_rekey_body()
42 1.1 riastrad {
43 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
44 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -c 1 -w 1"
45 1.1 riastrad local ip_local=192.168.1.1
46 1.1 riastrad local ip_peer=192.168.1.2
47 1.1 riastrad local ip_wg_local=10.0.0.1
48 1.1 riastrad local ip_wg_peer=10.0.0.2
49 1.1 riastrad local port=51820
50 1.1 riastrad local rekey_after_time=3
51 1.1 riastrad local latest_handshake=
52 1.1 riastrad
53 1.1 riastrad setup_servers
54 1.1 riastrad
55 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
56 1.1 riastrad atf_check -s exit:0 -o ignore \
57 1.1 riastrad rump.sysctl -w net.wg.rekey_after_time=$rekey_after_time
58 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
59 1.1 riastrad atf_check -s exit:0 -o ignore \
60 1.1 riastrad rump.sysctl -w net.wg.rekey_after_time=$rekey_after_time
61 1.1 riastrad
62 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
63 1.1 riastrad generate_keys
64 1.1 riastrad
65 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
66 1.1 riastrad setup_common shmif0 inet $ip_local 24
67 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
68 1.1 riastrad
69 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
70 1.1 riastrad setup_common shmif0 inet $ip_peer 24
71 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
72 1.1 riastrad
73 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
74 1.1 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
75 1.1 riastrad
76 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
77 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
78 1.1 riastrad
79 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
80 1.1 riastrad
81 1.1 riastrad $ping $ip_wg_peer
82 1.1 riastrad
83 1.1 riastrad latest_handshake=$($HIJACKING wgconfig wg0 show peer peer0 \
84 1.4 tih | awk -F ': ' '/latest-handshake/ {print $2;}')
85 1.1 riastrad $DEBUG && echo $latest_handshake
86 1.1 riastrad
87 1.1 riastrad sleep 1
88 1.1 riastrad
89 1.1 riastrad $ping $ip_wg_peer
90 1.1 riastrad
91 1.1 riastrad # No reinitiation is performed
92 1.1 riastrad atf_check -s exit:0 -o match:"$latest_handshake" \
93 1.1 riastrad $HIJACKING wgconfig wg0 show peer peer0
94 1.1 riastrad
95 1.1 riastrad # Wait for a reinitiation to be performed
96 1.1 riastrad sleep $rekey_after_time
97 1.1 riastrad
98 1.1 riastrad $ping $ip_wg_peer
99 1.1 riastrad
100 1.1 riastrad # A reinitiation should be performed
101 1.1 riastrad atf_check -s exit:0 -o not-match:"$latest_handshake" \
102 1.1 riastrad $HIJACKING wgconfig wg0 show peer peer0
103 1.1 riastrad
104 1.1 riastrad latest_handshake=$($HIJACKING wgconfig wg0 show peer peer0 \
105 1.4 tih | awk -F ': ' '/latest-handshake/ {print $2;}')
106 1.1 riastrad $DEBUG && echo $latest_handshake
107 1.1 riastrad
108 1.1 riastrad # Wait for a reinitiation to be performed again
109 1.5 riastrad sleep $((rekey_after_time+1))
110 1.1 riastrad
111 1.1 riastrad $ping $ip_wg_peer
112 1.1 riastrad
113 1.1 riastrad # A reinitiation should be performed
114 1.1 riastrad atf_check -s exit:0 -o not-match:"$latest_handshake" \
115 1.1 riastrad $HIJACKING wgconfig wg0 show peer peer0
116 1.1 riastrad
117 1.1 riastrad destroy_wg_interfaces
118 1.1 riastrad }
119 1.1 riastrad
120 1.1 riastrad wg_rekey_cleanup()
121 1.1 riastrad {
122 1.1 riastrad
123 1.1 riastrad $DEBUG && dump
124 1.1 riastrad cleanup
125 1.1 riastrad }
126 1.1 riastrad
127 1.1 riastrad atf_test_case wg_handshake_timeout cleanup
128 1.1 riastrad wg_handshake_timeout_head()
129 1.1 riastrad {
130 1.1 riastrad
131 1.1 riastrad atf_set "descr" "tests of handshake timeout of wg(4)"
132 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
133 1.1 riastrad }
134 1.1 riastrad
135 1.1 riastrad wg_handshake_timeout_body()
136 1.1 riastrad {
137 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
138 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -c 1 -w 1"
139 1.1 riastrad local ip_local=192.168.1.1
140 1.1 riastrad local ip_peer=192.168.1.2
141 1.1 riastrad local ip_wg_local=10.0.0.1
142 1.1 riastrad local ip_wg_peer=10.0.0.2
143 1.1 riastrad local port=51820
144 1.1 riastrad local rekey_after_time=3
145 1.1 riastrad local outfile=./out
146 1.1 riastrad local rekey_timeout=3
147 1.1 riastrad local rekey_attempt_time=8
148 1.1 riastrad local n=
149 1.1 riastrad
150 1.1 riastrad setup_servers
151 1.1 riastrad
152 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
153 1.1 riastrad atf_check -s exit:0 -o ignore \
154 1.1 riastrad rump.sysctl -w net.wg.rekey_timeout=$rekey_timeout
155 1.1 riastrad atf_check -s exit:0 -o ignore \
156 1.1 riastrad rump.sysctl -w net.wg.rekey_attempt_time=$rekey_attempt_time
157 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
158 1.1 riastrad atf_check -s exit:0 -o ignore \
159 1.1 riastrad rump.sysctl -w net.wg.rekey_timeout=$rekey_timeout
160 1.1 riastrad atf_check -s exit:0 -o ignore \
161 1.1 riastrad rump.sysctl -w net.wg.rekey_attempt_time=$rekey_attempt_time
162 1.1 riastrad
163 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
164 1.1 riastrad generate_keys
165 1.1 riastrad
166 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
167 1.1 riastrad setup_common shmif0 inet $ip_local 24
168 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
169 1.1 riastrad
170 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
171 1.1 riastrad setup_common shmif0 inet $ip_peer 24
172 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
173 1.1 riastrad
174 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
175 1.1 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
176 1.1 riastrad
177 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
178 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
179 1.1 riastrad
180 1.1 riastrad # Resolve arp
181 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
182 1.1 riastrad $ping $ip_peer
183 1.1 riastrad
184 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
185 1.1 riastrad $ifconfig shmif0 down
186 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
187 1.1 riastrad
188 1.1 riastrad extract_new_packets $BUS > $outfile
189 1.1 riastrad
190 1.1 riastrad # Should fail
191 1.1 riastrad atf_check -s not-exit:0 -o match:'100.0% packet loss' \
192 1.1 riastrad rump.ping -n -c 1 -w 1 $ip_wg_peer
193 1.1 riastrad
194 1.1 riastrad sleep $((rekey_attempt_time + rekey_timeout))
195 1.1 riastrad
196 1.1 riastrad extract_new_packets $BUS > $outfile
197 1.1 riastrad $DEBUG && cat $outfile
198 1.1 riastrad
199 1.1 riastrad n=$(grep "$ip_local.$port > $ip_peer.$port" $outfile |wc -l)
200 1.1 riastrad
201 1.1 riastrad # Give up handshaking after three attempts
202 1.1 riastrad atf_check_equal $n 3
203 1.1 riastrad
204 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
205 1.1 riastrad $ifconfig shmif0 up
206 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
207 1.1 riastrad
208 1.1 riastrad destroy_wg_interfaces
209 1.1 riastrad }
210 1.1 riastrad
211 1.1 riastrad wg_handshake_timeout_cleanup()
212 1.1 riastrad {
213 1.1 riastrad
214 1.1 riastrad $DEBUG && dump
215 1.1 riastrad cleanup
216 1.1 riastrad }
217 1.1 riastrad
218 1.1 riastrad atf_test_case wg_cookie cleanup
219 1.1 riastrad wg_cookie_head()
220 1.1 riastrad {
221 1.1 riastrad
222 1.1 riastrad atf_set "descr" "tests of cookie messages of the wg(4) protocol"
223 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
224 1.1 riastrad }
225 1.1 riastrad
226 1.1 riastrad wg_cookie_body()
227 1.1 riastrad {
228 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
229 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
230 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
231 1.1 riastrad local ip_local=192.168.1.1
232 1.1 riastrad local ip_peer=192.168.1.2
233 1.1 riastrad local ip_wg_local=10.0.0.1
234 1.1 riastrad local ip_wg_peer=10.0.0.2
235 1.1 riastrad local port=51820
236 1.1 riastrad local outfile=./out
237 1.1 riastrad local rekey_timeout=5
238 1.1 riastrad
239 1.1 riastrad setup_servers
240 1.1 riastrad
241 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
242 1.1 riastrad generate_keys
243 1.1 riastrad
244 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
245 1.1 riastrad setup_common shmif0 inet $ip_local 24
246 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
247 1.1 riastrad
248 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
249 1.1 riastrad setup_common shmif0 inet $ip_peer 24
250 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
251 1.1 riastrad
252 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
253 1.1 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
254 1.1 riastrad
255 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
256 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
257 1.1 riastrad
258 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
259 1.1 riastrad # Emulate load on the peer
260 1.1 riastrad atf_check -s exit:0 -o ignore \
261 1.1 riastrad rump.sysctl -w net.wg.force_underload=1
262 1.1 riastrad
263 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
264 1.1 riastrad
265 1.1 riastrad extract_new_packets $BUS > $outfile
266 1.1 riastrad $DEBUG && cat $outfile
267 1.1 riastrad
268 1.1 riastrad # The peer doesn't return a response message but a cookie message
269 1.1 riastrad # and a session doesn't start
270 1.1 riastrad $ping_fail $ip_wg_peer
271 1.1 riastrad
272 1.1 riastrad extract_new_packets $BUS > $outfile
273 1.1 riastrad $DEBUG && cat $outfile
274 1.1 riastrad # XXX length 64 indicates the message is a cookie message
275 1.1 riastrad atf_check -s exit:0 \
276 1.1 riastrad -o match:"$ip_peer.$port > $ip_local.$port: UDP, length 64" \
277 1.1 riastrad cat $outfile
278 1.1 riastrad
279 1.2 riastrad $DEBUG && $HIJACKING wgconfig wg0 show all
280 1.4 tih atf_check -s exit:0 -o match:"latest-handshake: \(never\)" \
281 1.1 riastrad $HIJACKING wgconfig wg0
282 1.1 riastrad
283 1.1 riastrad # Wait for restarting a session
284 1.1 riastrad sleep $rekey_timeout
285 1.1 riastrad
286 1.1 riastrad # The second attempt should be success because the init message has
287 1.1 riastrad # a valid cookie.
288 1.1 riastrad $ping $ip_wg_peer
289 1.1 riastrad
290 1.2 riastrad $DEBUG && $HIJACKING wgconfig wg0 show all
291 1.4 tih atf_check -s exit:0 -o not-match:"latest-handshake: \(never\)" \
292 1.1 riastrad $HIJACKING wgconfig wg0
293 1.1 riastrad
294 1.1 riastrad destroy_wg_interfaces
295 1.1 riastrad }
296 1.1 riastrad
297 1.1 riastrad wg_cookie_cleanup()
298 1.1 riastrad {
299 1.1 riastrad
300 1.1 riastrad $DEBUG && dump
301 1.1 riastrad cleanup
302 1.1 riastrad }
303 1.1 riastrad
304 1.1 riastrad atf_test_case wg_mobility cleanup
305 1.1 riastrad wg_mobility_head()
306 1.1 riastrad {
307 1.1 riastrad
308 1.1 riastrad atf_set "descr" "tests of the mobility of wg(4)"
309 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
310 1.1 riastrad }
311 1.1 riastrad
312 1.1 riastrad wg_mobility_body()
313 1.1 riastrad {
314 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
315 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
316 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
317 1.1 riastrad local ip_local=192.168.1.1
318 1.1 riastrad local ip_peer=192.168.1.2
319 1.1 riastrad local ip_peer_new=192.168.1.3
320 1.1 riastrad local ip_wg_local=10.0.0.1
321 1.1 riastrad local ip_wg_peer=10.0.0.2
322 1.1 riastrad local port=51820
323 1.1 riastrad local outfile=./out
324 1.1 riastrad
325 1.1 riastrad setup_servers
326 1.1 riastrad
327 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
328 1.1 riastrad generate_keys
329 1.1 riastrad
330 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
331 1.1 riastrad setup_common shmif0 inet $ip_local 24
332 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
333 1.1 riastrad
334 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
335 1.1 riastrad setup_common shmif0 inet $ip_peer 24
336 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
337 1.1 riastrad
338 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
339 1.1 riastrad # Initially, the local doesn't know the endpoint of the peer
340 1.1 riastrad add_peer wg0 peer0 $key_pub_peer "" $ip_wg_peer/32
341 1.1 riastrad
342 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
343 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
344 1.1 riastrad
345 1.1 riastrad extract_new_packets $BUS > $outfile
346 1.1 riastrad $DEBUG && cat $outfile
347 1.1 riastrad
348 1.1 riastrad # Ping from the local to the peer doesn't work because the local
349 1.1 riastrad # doesn't know the endpoint of the peer
350 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
351 1.1 riastrad $ping_fail $ip_wg_peer
352 1.1 riastrad
353 1.1 riastrad extract_new_packets $BUS > $outfile
354 1.1 riastrad $DEBUG && cat $outfile
355 1.1 riastrad
356 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
357 1.1 riastrad $ping $ip_wg_local
358 1.1 riastrad
359 1.1 riastrad extract_new_packets $BUS > $outfile
360 1.1 riastrad $DEBUG && cat $outfile
361 1.1 riastrad
362 1.1 riastrad atf_check -s exit:0 -o match:"$ip_local.$port > $ip_peer.$port" cat $outfile
363 1.1 riastrad
364 1.1 riastrad # Change the IP address of the peer
365 1.1 riastrad setup_common shmif0 inet $ip_peer_new 24
366 1.1 riastrad atf_check -s exit:0 rump.ifconfig -w 10
367 1.1 riastrad
368 1.1 riastrad # Ping from the local to the peer doesn't work because the local
369 1.1 riastrad # doesn't know the change of the IP address of the peer
370 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
371 1.1 riastrad $ping_fail $ip_wg_peer
372 1.1 riastrad
373 1.1 riastrad extract_new_packets $BUS > $outfile
374 1.1 riastrad $DEBUG && cat $outfile
375 1.1 riastrad
376 1.1 riastrad atf_check -s exit:0 -o match:"$ip_local.$port > $ip_peer.$port" cat $outfile
377 1.1 riastrad
378 1.1 riastrad # Ping from the peer to the local works because the local notices
379 1.1 riastrad # the change and updates the IP address of the peer
380 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
381 1.1 riastrad $ping $ip_wg_local
382 1.1 riastrad
383 1.1 riastrad extract_new_packets $BUS > $outfile
384 1.1 riastrad $DEBUG && cat $outfile
385 1.1 riastrad
386 1.1 riastrad atf_check -s exit:0 -o match:"$ip_local.$port > $ip_peer_new.$port" cat $outfile
387 1.1 riastrad atf_check -s exit:0 -o match:"$ip_peer_new.$port > $ip_local.$port" cat $outfile
388 1.1 riastrad atf_check -s exit:0 -o not-match:"$ip_local.$port > $ip_peer.$port" cat $outfile
389 1.1 riastrad
390 1.1 riastrad destroy_wg_interfaces
391 1.1 riastrad }
392 1.1 riastrad
393 1.1 riastrad wg_mobility_cleanup()
394 1.1 riastrad {
395 1.1 riastrad
396 1.1 riastrad $DEBUG && dump
397 1.1 riastrad cleanup
398 1.1 riastrad }
399 1.1 riastrad
400 1.1 riastrad atf_test_case wg_keepalive cleanup
401 1.1 riastrad wg_keepalive_head()
402 1.1 riastrad {
403 1.1 riastrad
404 1.1 riastrad atf_set "descr" "tests keepalive messages"
405 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
406 1.1 riastrad }
407 1.1 riastrad
408 1.1 riastrad wg_keepalive_body()
409 1.1 riastrad {
410 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
411 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
412 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
413 1.1 riastrad local ip_local=192.168.1.1
414 1.1 riastrad local ip_peer=192.168.1.2
415 1.1 riastrad local ip_peer_new=192.168.1.3
416 1.1 riastrad local ip_wg_local=10.0.0.1
417 1.1 riastrad local ip_wg_peer=10.0.0.2
418 1.1 riastrad local port=51820
419 1.1 riastrad local outfile=./out
420 1.1 riastrad local keepalive_timeout=3
421 1.1 riastrad
422 1.1 riastrad setup_servers
423 1.1 riastrad
424 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
425 1.1 riastrad generate_keys
426 1.1 riastrad
427 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
428 1.1 riastrad setup_common shmif0 inet $ip_local 24
429 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
430 1.1 riastrad
431 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
432 1.1 riastrad setup_common shmif0 inet $ip_peer 24
433 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
434 1.1 riastrad
435 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
436 1.1 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
437 1.1 riastrad
438 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
439 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
440 1.1 riastrad
441 1.1 riastrad # Shorten keepalive_timeout of the peer
442 1.1 riastrad atf_check -s exit:0 -o ignore \
443 1.1 riastrad rump.sysctl -w net.wg.keepalive_timeout=$keepalive_timeout
444 1.1 riastrad
445 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
446 1.1 riastrad
447 1.1 riastrad extract_new_packets $BUS > $outfile
448 1.1 riastrad $DEBUG && cat $outfile
449 1.1 riastrad
450 1.1 riastrad $ping $ip_wg_peer
451 1.1 riastrad
452 1.1 riastrad extract_new_packets $BUS > $outfile
453 1.1 riastrad $DEBUG && cat $outfile
454 1.1 riastrad
455 1.1 riastrad sleep $((keepalive_timeout + 1))
456 1.1 riastrad
457 1.1 riastrad $ping $ip_wg_peer
458 1.1 riastrad
459 1.1 riastrad extract_new_packets $BUS > $outfile
460 1.1 riastrad $DEBUG && cat $outfile
461 1.1 riastrad
462 1.1 riastrad # XXX length 32 indicates the message is a keepalive (empty) message
463 1.1 riastrad atf_check -s exit:0 -o match:"$ip_peer.$port > $ip_local.$port: UDP, length 32" \
464 1.1 riastrad cat $outfile
465 1.1 riastrad
466 1.1 riastrad destroy_wg_interfaces
467 1.1 riastrad }
468 1.1 riastrad
469 1.1 riastrad wg_keepalive_cleanup()
470 1.1 riastrad {
471 1.1 riastrad
472 1.1 riastrad $DEBUG && dump
473 1.1 riastrad cleanup
474 1.1 riastrad }
475 1.1 riastrad
476 1.1 riastrad atf_test_case wg_psk cleanup
477 1.1 riastrad wg_psk_head()
478 1.1 riastrad {
479 1.1 riastrad
480 1.1 riastrad atf_set "descr" "tests preshared-key"
481 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
482 1.1 riastrad }
483 1.1 riastrad
484 1.1 riastrad test_psk_common()
485 1.1 riastrad {
486 1.1 riastrad }
487 1.1 riastrad
488 1.1 riastrad wg_psk_body()
489 1.1 riastrad {
490 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
491 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
492 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
493 1.1 riastrad local ip_local=192.168.1.1
494 1.1 riastrad local ip_peer=192.168.1.2
495 1.1 riastrad local ip_peer_new=192.168.1.3
496 1.1 riastrad local ip_wg_local=10.0.0.1
497 1.1 riastrad local ip_wg_peer=10.0.0.2
498 1.1 riastrad local port=51820
499 1.1 riastrad local outfile=./out
500 1.1 riastrad local pskfile=./psk
501 1.1 riastrad local rekey_after_time=3
502 1.1 riastrad
503 1.1 riastrad setup_servers
504 1.1 riastrad
505 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
506 1.1 riastrad atf_check -s exit:0 -o ignore \
507 1.1 riastrad rump.sysctl -w net.wg.rekey_after_time=$rekey_after_time
508 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
509 1.1 riastrad atf_check -s exit:0 -o ignore \
510 1.1 riastrad rump.sysctl -w net.wg.rekey_after_time=$rekey_after_time
511 1.1 riastrad
512 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
513 1.1 riastrad generate_keys
514 1.1 riastrad key_psk=$(wg-keygen --psk)
515 1.1 riastrad $DEBUG && echo $key_psk
516 1.1 riastrad
517 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
518 1.1 riastrad setup_common shmif0 inet $ip_local 24
519 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
520 1.1 riastrad
521 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
522 1.1 riastrad setup_common shmif0 inet $ip_peer 24
523 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
524 1.1 riastrad
525 1.1 riastrad echo "$key_psk" > $pskfile
526 1.1 riastrad
527 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
528 1.1 riastrad
529 1.1 riastrad # The local always has the preshared key
530 1.1 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32 \
531 1.1 riastrad $pskfile "$key_psk"
532 1.1 riastrad
533 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
534 1.1 riastrad
535 1.1 riastrad # First, try the peer without the preshared key
536 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
537 1.1 riastrad
538 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
539 1.1 riastrad
540 1.1 riastrad extract_new_packets $BUS > $outfile
541 1.1 riastrad $DEBUG && cat $outfile
542 1.1 riastrad
543 1.1 riastrad $ping_fail $ip_wg_peer
544 1.1 riastrad
545 1.1 riastrad extract_new_packets $BUS > $outfile
546 1.1 riastrad $DEBUG && cat $outfile
547 1.1 riastrad
548 1.1 riastrad # Next, try with the preshared key
549 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
550 1.1 riastrad delete_peer wg0 peer0
551 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32 \
552 1.1 riastrad $pskfile "$key_psk"
553 1.1 riastrad
554 1.1 riastrad # Need a rekey
555 1.1 riastrad atf_check -s exit:0 sleep $((rekey_after_time + 1))
556 1.1 riastrad
557 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
558 1.1 riastrad
559 1.1 riastrad extract_new_packets $BUS > $outfile
560 1.1 riastrad $DEBUG && cat $outfile
561 1.1 riastrad
562 1.1 riastrad $ping $ip_wg_peer
563 1.1 riastrad
564 1.1 riastrad extract_new_packets $BUS > $outfile
565 1.1 riastrad $DEBUG && cat $outfile
566 1.1 riastrad
567 1.1 riastrad # Then, try again without the preshared key just in case
568 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
569 1.1 riastrad delete_peer wg0 peer0
570 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
571 1.1 riastrad
572 1.1 riastrad # Need a rekey
573 1.1 riastrad atf_check -s exit:0 sleep $((rekey_after_time + 1))
574 1.1 riastrad
575 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
576 1.1 riastrad $ping_fail $ip_wg_peer
577 1.1 riastrad
578 1.1 riastrad rm -f $pskfile
579 1.1 riastrad
580 1.1 riastrad destroy_wg_interfaces
581 1.1 riastrad }
582 1.1 riastrad
583 1.1 riastrad wg_psk_cleanup()
584 1.1 riastrad {
585 1.1 riastrad
586 1.1 riastrad $DEBUG && dump
587 1.1 riastrad cleanup
588 1.1 riastrad }
589 1.1 riastrad
590 1.3 riastrad atf_test_case wg_malformed cleanup
591 1.3 riastrad wg_malformed_head()
592 1.3 riastrad {
593 1.3 riastrad
594 1.3 riastrad atf_set "descr" "tests malformed packet headers"
595 1.3 riastrad atf_set "require.progs" "nc" "rump_server" "wgconfig" "wg-keygen"
596 1.3 riastrad atf_set "timeout" "10"
597 1.3 riastrad }
598 1.3 riastrad
599 1.3 riastrad wg_malformed_body()
600 1.3 riastrad {
601 1.3 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
602 1.3 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -c 1 -w 1"
603 1.3 riastrad local ip_local=192.168.1.1
604 1.3 riastrad local ip_peer=192.168.1.2
605 1.3 riastrad local ip_wg_local=10.0.0.1
606 1.3 riastrad local ip_wg_peer=10.0.0.2
607 1.3 riastrad local port=51820
608 1.3 riastrad setup_servers
609 1.3 riastrad
610 1.3 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
611 1.3 riastrad generate_keys
612 1.3 riastrad
613 1.3 riastrad export RUMP_SERVER=$SOCK_LOCAL
614 1.3 riastrad setup_common shmif0 inet $ip_local 24
615 1.3 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
616 1.3 riastrad
617 1.3 riastrad export RUMP_SERVER=$SOCK_PEER
618 1.3 riastrad setup_common shmif0 inet $ip_peer 24
619 1.3 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
620 1.3 riastrad
621 1.3 riastrad export RUMP_SERVER=$SOCK_LOCAL
622 1.3 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
623 1.3 riastrad
624 1.3 riastrad export RUMP_SERVER=$SOCK_PEER
625 1.3 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
626 1.3 riastrad
627 1.3 riastrad export RUMP_SERVER=$SOCK_LOCAL
628 1.3 riastrad
629 1.3 riastrad $ping $ip_wg_peer
630 1.3 riastrad
631 1.3 riastrad printf 'send malformed packets\n'
632 1.3 riastrad
633 1.3 riastrad $HIJACKING ping -c 1 -n $ip_peer
634 1.3 riastrad
635 1.3 riastrad printf 'x' | $HIJACKING nc -Nu -w 0 $ip_peer $port
636 1.3 riastrad printf 'xy' | $HIJACKING nc -Nu -w 0 $ip_peer $port
637 1.3 riastrad printf 'xyz' | $HIJACKING nc -Nu -w 0 $ip_peer $port
638 1.3 riastrad printf 'xyzw' | $HIJACKING nc -Nu -w 0 $ip_peer $port
639 1.3 riastrad printf '\x00\x00\x00\x00' | $HIJACKING nc -Nu -w 0 $ip_peer $port
640 1.3 riastrad printf '\x00\x00\x00\x00z' | $HIJACKING nc -Nu -w 0 $ip_peer $port
641 1.3 riastrad printf '\x01\x00\x00\x00' | $HIJACKING nc -Nu -w 0 $ip_peer $port
642 1.3 riastrad printf '\x01\x00\x00\x00z' | $HIJACKING nc -Nu -w 0 $ip_peer $port
643 1.3 riastrad printf '\x02\x00\x00\x00' | $HIJACKING nc -Nu -w 0 $ip_peer $port
644 1.3 riastrad printf '\x02\x00\x00\x00z' | $HIJACKING nc -Nu -w 0 $ip_peer $port
645 1.3 riastrad printf '\x03\x00\x00\x00' | $HIJACKING nc -Nu -w 0 $ip_peer $port
646 1.3 riastrad printf '\x03\x00\x00\x00z' | $HIJACKING nc -Nu -w 0 $ip_peer $port
647 1.3 riastrad printf '\x04\x00\x00\x00' | $HIJACKING nc -Nu -w 0 $ip_peer $port
648 1.3 riastrad printf '\x04\x00\x00\x00z' | $HIJACKING nc -Nu -w 0 $ip_peer $port
649 1.3 riastrad
650 1.3 riastrad printf 'done sending malformed packets\n'
651 1.3 riastrad
652 1.3 riastrad $ping $ip_wg_peer
653 1.3 riastrad }
654 1.3 riastrad
655 1.3 riastrad wg_malformed_cleanup()
656 1.3 riastrad {
657 1.3 riastrad
658 1.3 riastrad $DEBUG && dump
659 1.3 riastrad cleanup
660 1.3 riastrad }
661 1.3 riastrad
662 1.1 riastrad atf_init_test_cases()
663 1.1 riastrad {
664 1.1 riastrad
665 1.1 riastrad atf_add_test_case wg_rekey
666 1.1 riastrad atf_add_test_case wg_handshake_timeout
667 1.1 riastrad atf_add_test_case wg_cookie
668 1.1 riastrad atf_add_test_case wg_mobility
669 1.1 riastrad atf_add_test_case wg_keepalive
670 1.1 riastrad atf_add_test_case wg_psk
671 1.3 riastrad atf_add_test_case wg_malformed
672 1.1 riastrad }
673