t_misc.sh revision 1.2 1 1.2 riastrad # $NetBSD: t_misc.sh,v 1.2 2020/08/27 02:51:49 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.1 riastrad | 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.1 riastrad | 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.1 riastrad sleep $rekey_after_time
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 latest_handshake=
146 1.1 riastrad local outfile=./out
147 1.1 riastrad local rekey_timeout=3
148 1.1 riastrad local rekey_attempt_time=8
149 1.1 riastrad local n=
150 1.1 riastrad
151 1.1 riastrad setup_servers
152 1.1 riastrad
153 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
154 1.1 riastrad atf_check -s exit:0 -o ignore \
155 1.1 riastrad rump.sysctl -w net.wg.rekey_timeout=$rekey_timeout
156 1.1 riastrad atf_check -s exit:0 -o ignore \
157 1.1 riastrad rump.sysctl -w net.wg.rekey_attempt_time=$rekey_attempt_time
158 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
159 1.1 riastrad atf_check -s exit:0 -o ignore \
160 1.1 riastrad rump.sysctl -w net.wg.rekey_timeout=$rekey_timeout
161 1.1 riastrad atf_check -s exit:0 -o ignore \
162 1.1 riastrad rump.sysctl -w net.wg.rekey_attempt_time=$rekey_attempt_time
163 1.1 riastrad
164 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
165 1.1 riastrad generate_keys
166 1.1 riastrad
167 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
168 1.1 riastrad setup_common shmif0 inet $ip_local 24
169 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
170 1.1 riastrad
171 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
172 1.1 riastrad setup_common shmif0 inet $ip_peer 24
173 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
174 1.1 riastrad
175 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
176 1.1 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
177 1.1 riastrad
178 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
179 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
180 1.1 riastrad
181 1.1 riastrad # Resolve arp
182 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
183 1.1 riastrad $ping $ip_peer
184 1.1 riastrad
185 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
186 1.1 riastrad $ifconfig shmif0 down
187 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
188 1.1 riastrad
189 1.1 riastrad extract_new_packets $BUS > $outfile
190 1.1 riastrad
191 1.1 riastrad # Should fail
192 1.1 riastrad atf_check -s not-exit:0 -o match:'100.0% packet loss' \
193 1.1 riastrad rump.ping -n -c 1 -w 1 $ip_wg_peer
194 1.1 riastrad
195 1.1 riastrad sleep $((rekey_attempt_time + rekey_timeout))
196 1.1 riastrad
197 1.1 riastrad extract_new_packets $BUS > $outfile
198 1.1 riastrad $DEBUG && cat $outfile
199 1.1 riastrad
200 1.1 riastrad n=$(grep "$ip_local.$port > $ip_peer.$port" $outfile |wc -l)
201 1.1 riastrad
202 1.1 riastrad # Give up handshaking after three attempts
203 1.1 riastrad atf_check_equal $n 3
204 1.1 riastrad
205 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
206 1.1 riastrad $ifconfig shmif0 up
207 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
208 1.1 riastrad
209 1.1 riastrad destroy_wg_interfaces
210 1.1 riastrad }
211 1.1 riastrad
212 1.1 riastrad wg_handshake_timeout_cleanup()
213 1.1 riastrad {
214 1.1 riastrad
215 1.1 riastrad $DEBUG && dump
216 1.1 riastrad cleanup
217 1.1 riastrad }
218 1.1 riastrad
219 1.1 riastrad atf_test_case wg_cookie cleanup
220 1.1 riastrad wg_cookie_head()
221 1.1 riastrad {
222 1.1 riastrad
223 1.1 riastrad atf_set "descr" "tests of cookie messages of the wg(4) protocol"
224 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
225 1.1 riastrad }
226 1.1 riastrad
227 1.1 riastrad wg_cookie_body()
228 1.1 riastrad {
229 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
230 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
231 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
232 1.1 riastrad local ip_local=192.168.1.1
233 1.1 riastrad local ip_peer=192.168.1.2
234 1.1 riastrad local ip_wg_local=10.0.0.1
235 1.1 riastrad local ip_wg_peer=10.0.0.2
236 1.1 riastrad local port=51820
237 1.1 riastrad local outfile=./out
238 1.1 riastrad local rekey_timeout=5
239 1.1 riastrad
240 1.1 riastrad setup_servers
241 1.1 riastrad
242 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
243 1.1 riastrad generate_keys
244 1.1 riastrad
245 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
246 1.1 riastrad setup_common shmif0 inet $ip_local 24
247 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
248 1.1 riastrad
249 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
250 1.1 riastrad setup_common shmif0 inet $ip_peer 24
251 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
252 1.1 riastrad
253 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
254 1.1 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
255 1.1 riastrad
256 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
257 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
258 1.1 riastrad
259 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
260 1.1 riastrad # Emulate load on the peer
261 1.1 riastrad atf_check -s exit:0 -o ignore \
262 1.1 riastrad rump.sysctl -w net.wg.force_underload=1
263 1.1 riastrad
264 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
265 1.1 riastrad
266 1.1 riastrad extract_new_packets $BUS > $outfile
267 1.1 riastrad $DEBUG && cat $outfile
268 1.1 riastrad
269 1.1 riastrad # The peer doesn't return a response message but a cookie message
270 1.1 riastrad # and a session doesn't start
271 1.1 riastrad $ping_fail $ip_wg_peer
272 1.1 riastrad
273 1.1 riastrad extract_new_packets $BUS > $outfile
274 1.1 riastrad $DEBUG && cat $outfile
275 1.1 riastrad # XXX length 64 indicates the message is a cookie message
276 1.1 riastrad atf_check -s exit:0 \
277 1.1 riastrad -o match:"$ip_peer.$port > $ip_local.$port: UDP, length 64" \
278 1.1 riastrad cat $outfile
279 1.1 riastrad
280 1.2 riastrad $DEBUG && $HIJACKING wgconfig wg0 show all
281 1.1 riastrad atf_check -s exit:0 -o match:"latest-handshake: 0" \
282 1.1 riastrad $HIJACKING wgconfig wg0
283 1.1 riastrad
284 1.1 riastrad # Wait for restarting a session
285 1.1 riastrad sleep $rekey_timeout
286 1.1 riastrad
287 1.1 riastrad # The second attempt should be success because the init message has
288 1.1 riastrad # a valid cookie.
289 1.1 riastrad $ping $ip_wg_peer
290 1.1 riastrad
291 1.2 riastrad $DEBUG && $HIJACKING wgconfig wg0 show all
292 1.1 riastrad atf_check -s exit:0 -o not-match:"latest-handshake: 0" \
293 1.1 riastrad $HIJACKING wgconfig wg0
294 1.1 riastrad
295 1.1 riastrad destroy_wg_interfaces
296 1.1 riastrad }
297 1.1 riastrad
298 1.1 riastrad wg_cookie_cleanup()
299 1.1 riastrad {
300 1.1 riastrad
301 1.1 riastrad $DEBUG && dump
302 1.1 riastrad cleanup
303 1.1 riastrad }
304 1.1 riastrad
305 1.1 riastrad atf_test_case wg_mobility cleanup
306 1.1 riastrad wg_mobility_head()
307 1.1 riastrad {
308 1.1 riastrad
309 1.1 riastrad atf_set "descr" "tests of the mobility of wg(4)"
310 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
311 1.1 riastrad }
312 1.1 riastrad
313 1.1 riastrad wg_mobility_body()
314 1.1 riastrad {
315 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
316 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
317 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
318 1.1 riastrad local ip_local=192.168.1.1
319 1.1 riastrad local ip_peer=192.168.1.2
320 1.1 riastrad local ip_peer_new=192.168.1.3
321 1.1 riastrad local ip_wg_local=10.0.0.1
322 1.1 riastrad local ip_wg_peer=10.0.0.2
323 1.1 riastrad local port=51820
324 1.1 riastrad local outfile=./out
325 1.1 riastrad
326 1.1 riastrad setup_servers
327 1.1 riastrad
328 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
329 1.1 riastrad generate_keys
330 1.1 riastrad
331 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
332 1.1 riastrad setup_common shmif0 inet $ip_local 24
333 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
334 1.1 riastrad
335 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
336 1.1 riastrad setup_common shmif0 inet $ip_peer 24
337 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
338 1.1 riastrad
339 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
340 1.1 riastrad # Initially, the local doesn't know the endpoint of the peer
341 1.1 riastrad add_peer wg0 peer0 $key_pub_peer "" $ip_wg_peer/32
342 1.1 riastrad
343 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
344 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
345 1.1 riastrad
346 1.1 riastrad extract_new_packets $BUS > $outfile
347 1.1 riastrad $DEBUG && cat $outfile
348 1.1 riastrad
349 1.1 riastrad # Ping from the local to the peer doesn't work because the local
350 1.1 riastrad # doesn't know the endpoint of the peer
351 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
352 1.1 riastrad $ping_fail $ip_wg_peer
353 1.1 riastrad
354 1.1 riastrad extract_new_packets $BUS > $outfile
355 1.1 riastrad $DEBUG && cat $outfile
356 1.1 riastrad
357 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
358 1.1 riastrad $ping $ip_wg_local
359 1.1 riastrad
360 1.1 riastrad extract_new_packets $BUS > $outfile
361 1.1 riastrad $DEBUG && cat $outfile
362 1.1 riastrad
363 1.1 riastrad atf_check -s exit:0 -o match:"$ip_local.$port > $ip_peer.$port" cat $outfile
364 1.1 riastrad
365 1.1 riastrad # Change the IP address of the peer
366 1.1 riastrad setup_common shmif0 inet $ip_peer_new 24
367 1.1 riastrad atf_check -s exit:0 rump.ifconfig -w 10
368 1.1 riastrad
369 1.1 riastrad # Ping from the local to the peer doesn't work because the local
370 1.1 riastrad # doesn't know the change of the IP address of the peer
371 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
372 1.1 riastrad $ping_fail $ip_wg_peer
373 1.1 riastrad
374 1.1 riastrad extract_new_packets $BUS > $outfile
375 1.1 riastrad $DEBUG && cat $outfile
376 1.1 riastrad
377 1.1 riastrad atf_check -s exit:0 -o match:"$ip_local.$port > $ip_peer.$port" cat $outfile
378 1.1 riastrad
379 1.1 riastrad # Ping from the peer to the local works because the local notices
380 1.1 riastrad # the change and updates the IP address of the peer
381 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
382 1.1 riastrad $ping $ip_wg_local
383 1.1 riastrad
384 1.1 riastrad extract_new_packets $BUS > $outfile
385 1.1 riastrad $DEBUG && cat $outfile
386 1.1 riastrad
387 1.1 riastrad atf_check -s exit:0 -o match:"$ip_local.$port > $ip_peer_new.$port" cat $outfile
388 1.1 riastrad atf_check -s exit:0 -o match:"$ip_peer_new.$port > $ip_local.$port" cat $outfile
389 1.1 riastrad atf_check -s exit:0 -o not-match:"$ip_local.$port > $ip_peer.$port" cat $outfile
390 1.1 riastrad
391 1.1 riastrad destroy_wg_interfaces
392 1.1 riastrad }
393 1.1 riastrad
394 1.1 riastrad wg_mobility_cleanup()
395 1.1 riastrad {
396 1.1 riastrad
397 1.1 riastrad $DEBUG && dump
398 1.1 riastrad cleanup
399 1.1 riastrad }
400 1.1 riastrad
401 1.1 riastrad atf_test_case wg_keepalive cleanup
402 1.1 riastrad wg_keepalive_head()
403 1.1 riastrad {
404 1.1 riastrad
405 1.1 riastrad atf_set "descr" "tests keepalive messages"
406 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
407 1.1 riastrad }
408 1.1 riastrad
409 1.1 riastrad wg_keepalive_body()
410 1.1 riastrad {
411 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
412 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
413 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
414 1.1 riastrad local ip_local=192.168.1.1
415 1.1 riastrad local ip_peer=192.168.1.2
416 1.1 riastrad local ip_peer_new=192.168.1.3
417 1.1 riastrad local ip_wg_local=10.0.0.1
418 1.1 riastrad local ip_wg_peer=10.0.0.2
419 1.1 riastrad local port=51820
420 1.1 riastrad local outfile=./out
421 1.1 riastrad local keepalive_timeout=3
422 1.1 riastrad
423 1.1 riastrad setup_servers
424 1.1 riastrad
425 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
426 1.1 riastrad generate_keys
427 1.1 riastrad
428 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
429 1.1 riastrad setup_common shmif0 inet $ip_local 24
430 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
431 1.1 riastrad
432 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
433 1.1 riastrad setup_common shmif0 inet $ip_peer 24
434 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
435 1.1 riastrad
436 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
437 1.1 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
438 1.1 riastrad
439 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
440 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
441 1.1 riastrad
442 1.1 riastrad # Shorten keepalive_timeout of the peer
443 1.1 riastrad atf_check -s exit:0 -o ignore \
444 1.1 riastrad rump.sysctl -w net.wg.keepalive_timeout=$keepalive_timeout
445 1.1 riastrad
446 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
447 1.1 riastrad
448 1.1 riastrad extract_new_packets $BUS > $outfile
449 1.1 riastrad $DEBUG && cat $outfile
450 1.1 riastrad
451 1.1 riastrad $ping $ip_wg_peer
452 1.1 riastrad
453 1.1 riastrad extract_new_packets $BUS > $outfile
454 1.1 riastrad $DEBUG && cat $outfile
455 1.1 riastrad
456 1.1 riastrad sleep $((keepalive_timeout + 1))
457 1.1 riastrad
458 1.1 riastrad $ping $ip_wg_peer
459 1.1 riastrad
460 1.1 riastrad extract_new_packets $BUS > $outfile
461 1.1 riastrad $DEBUG && cat $outfile
462 1.1 riastrad
463 1.1 riastrad # XXX length 32 indicates the message is a keepalive (empty) message
464 1.1 riastrad atf_check -s exit:0 -o match:"$ip_peer.$port > $ip_local.$port: UDP, length 32" \
465 1.1 riastrad cat $outfile
466 1.1 riastrad
467 1.1 riastrad destroy_wg_interfaces
468 1.1 riastrad }
469 1.1 riastrad
470 1.1 riastrad wg_keepalive_cleanup()
471 1.1 riastrad {
472 1.1 riastrad
473 1.1 riastrad $DEBUG && dump
474 1.1 riastrad cleanup
475 1.1 riastrad }
476 1.1 riastrad
477 1.1 riastrad atf_test_case wg_psk cleanup
478 1.1 riastrad wg_psk_head()
479 1.1 riastrad {
480 1.1 riastrad
481 1.1 riastrad atf_set "descr" "tests preshared-key"
482 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
483 1.1 riastrad }
484 1.1 riastrad
485 1.1 riastrad test_psk_common()
486 1.1 riastrad {
487 1.1 riastrad }
488 1.1 riastrad
489 1.1 riastrad wg_psk_body()
490 1.1 riastrad {
491 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
492 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
493 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
494 1.1 riastrad local ip_local=192.168.1.1
495 1.1 riastrad local ip_peer=192.168.1.2
496 1.1 riastrad local ip_peer_new=192.168.1.3
497 1.1 riastrad local ip_wg_local=10.0.0.1
498 1.1 riastrad local ip_wg_peer=10.0.0.2
499 1.1 riastrad local port=51820
500 1.1 riastrad local outfile=./out
501 1.1 riastrad local pskfile=./psk
502 1.1 riastrad local rekey_after_time=3
503 1.1 riastrad
504 1.1 riastrad setup_servers
505 1.1 riastrad
506 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
507 1.1 riastrad atf_check -s exit:0 -o ignore \
508 1.1 riastrad rump.sysctl -w net.wg.rekey_after_time=$rekey_after_time
509 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
510 1.1 riastrad atf_check -s exit:0 -o ignore \
511 1.1 riastrad rump.sysctl -w net.wg.rekey_after_time=$rekey_after_time
512 1.1 riastrad
513 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
514 1.1 riastrad generate_keys
515 1.1 riastrad key_psk=$(wg-keygen --psk)
516 1.1 riastrad $DEBUG && echo $key_psk
517 1.1 riastrad
518 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
519 1.1 riastrad setup_common shmif0 inet $ip_local 24
520 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
521 1.1 riastrad
522 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
523 1.1 riastrad setup_common shmif0 inet $ip_peer 24
524 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
525 1.1 riastrad
526 1.1 riastrad echo "$key_psk" > $pskfile
527 1.1 riastrad
528 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
529 1.1 riastrad
530 1.1 riastrad # The local always has the preshared key
531 1.1 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32 \
532 1.1 riastrad $pskfile "$key_psk"
533 1.1 riastrad
534 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
535 1.1 riastrad
536 1.1 riastrad # First, try the peer without the preshared key
537 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
538 1.1 riastrad
539 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
540 1.1 riastrad
541 1.1 riastrad extract_new_packets $BUS > $outfile
542 1.1 riastrad $DEBUG && cat $outfile
543 1.1 riastrad
544 1.1 riastrad $ping_fail $ip_wg_peer
545 1.1 riastrad
546 1.1 riastrad extract_new_packets $BUS > $outfile
547 1.1 riastrad $DEBUG && cat $outfile
548 1.1 riastrad
549 1.1 riastrad # Next, try with the preshared key
550 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
551 1.1 riastrad delete_peer wg0 peer0
552 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32 \
553 1.1 riastrad $pskfile "$key_psk"
554 1.1 riastrad
555 1.1 riastrad # Need a rekey
556 1.1 riastrad atf_check -s exit:0 sleep $((rekey_after_time + 1))
557 1.1 riastrad
558 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
559 1.1 riastrad
560 1.1 riastrad extract_new_packets $BUS > $outfile
561 1.1 riastrad $DEBUG && cat $outfile
562 1.1 riastrad
563 1.1 riastrad $ping $ip_wg_peer
564 1.1 riastrad
565 1.1 riastrad extract_new_packets $BUS > $outfile
566 1.1 riastrad $DEBUG && cat $outfile
567 1.1 riastrad
568 1.1 riastrad # Then, try again without the preshared key just in case
569 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
570 1.1 riastrad delete_peer wg0 peer0
571 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
572 1.1 riastrad
573 1.1 riastrad # Need a rekey
574 1.1 riastrad atf_check -s exit:0 sleep $((rekey_after_time + 1))
575 1.1 riastrad
576 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
577 1.1 riastrad $ping_fail $ip_wg_peer
578 1.1 riastrad
579 1.1 riastrad rm -f $pskfile
580 1.1 riastrad
581 1.1 riastrad destroy_wg_interfaces
582 1.1 riastrad }
583 1.1 riastrad
584 1.1 riastrad wg_psk_cleanup()
585 1.1 riastrad {
586 1.1 riastrad
587 1.1 riastrad $DEBUG && dump
588 1.1 riastrad cleanup
589 1.1 riastrad }
590 1.1 riastrad
591 1.1 riastrad atf_init_test_cases()
592 1.1 riastrad {
593 1.1 riastrad
594 1.1 riastrad atf_add_test_case wg_rekey
595 1.1 riastrad atf_add_test_case wg_handshake_timeout
596 1.1 riastrad atf_add_test_case wg_cookie
597 1.1 riastrad atf_add_test_case wg_mobility
598 1.1 riastrad atf_add_test_case wg_keepalive
599 1.1 riastrad atf_add_test_case wg_psk
600 1.1 riastrad }
601