t_misc.sh revision 1.13 1 1.13 riastrad # $NetBSD: t_misc.sh,v 1.13 2024/07/28 14:46:33 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.6 roy add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
69 1.7 martin $ifconfig -w 10
70 1.1 riastrad
71 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
72 1.1 riastrad setup_common shmif0 inet $ip_peer 24
73 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
74 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
75 1.6 roy $ifconfig -w 10
76 1.1 riastrad
77 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
78 1.1 riastrad
79 1.1 riastrad $ping $ip_wg_peer
80 1.1 riastrad
81 1.1 riastrad latest_handshake=$($HIJACKING wgconfig wg0 show peer peer0 \
82 1.4 tih | awk -F ': ' '/latest-handshake/ {print $2;}')
83 1.1 riastrad $DEBUG && echo $latest_handshake
84 1.1 riastrad
85 1.1 riastrad sleep 1
86 1.1 riastrad
87 1.1 riastrad $ping $ip_wg_peer
88 1.1 riastrad
89 1.9 riastrad atf_expect_fail "PR kern/56252"
90 1.9 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.9 riastrad
119 1.9 riastrad atf_fail "failed to trigger PR kern/56252"
120 1.1 riastrad }
121 1.1 riastrad
122 1.1 riastrad wg_rekey_cleanup()
123 1.1 riastrad {
124 1.1 riastrad
125 1.1 riastrad $DEBUG && dump
126 1.1 riastrad cleanup
127 1.1 riastrad }
128 1.1 riastrad
129 1.1 riastrad atf_test_case wg_handshake_timeout cleanup
130 1.1 riastrad wg_handshake_timeout_head()
131 1.1 riastrad {
132 1.1 riastrad
133 1.1 riastrad atf_set "descr" "tests of handshake timeout of wg(4)"
134 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
135 1.1 riastrad }
136 1.1 riastrad
137 1.1 riastrad wg_handshake_timeout_body()
138 1.1 riastrad {
139 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
140 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -c 1 -w 1"
141 1.1 riastrad local ip_local=192.168.1.1
142 1.1 riastrad local ip_peer=192.168.1.2
143 1.1 riastrad local ip_wg_local=10.0.0.1
144 1.1 riastrad local ip_wg_peer=10.0.0.2
145 1.1 riastrad local port=51820
146 1.1 riastrad local outfile=./out
147 1.13 riastrad local rekey_timeout=4
148 1.13 riastrad local rekey_attempt_time=10
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.6 roy add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
171 1.6 roy $ifconfig -w 10
172 1.1 riastrad
173 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
174 1.1 riastrad setup_common shmif0 inet $ip_peer 24
175 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
176 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
177 1.6 roy $ifconfig -w 10
178 1.1 riastrad
179 1.1 riastrad # Resolve arp
180 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
181 1.1 riastrad $ping $ip_peer
182 1.1 riastrad
183 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
184 1.1 riastrad $ifconfig shmif0 down
185 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
186 1.1 riastrad
187 1.1 riastrad extract_new_packets $BUS > $outfile
188 1.1 riastrad
189 1.1 riastrad # Should fail
190 1.1 riastrad atf_check -s not-exit:0 -o match:'100.0% packet loss' \
191 1.1 riastrad rump.ping -n -c 1 -w 1 $ip_wg_peer
192 1.1 riastrad
193 1.1 riastrad sleep $((rekey_attempt_time + rekey_timeout))
194 1.1 riastrad
195 1.1 riastrad extract_new_packets $BUS > $outfile
196 1.1 riastrad $DEBUG && cat $outfile
197 1.1 riastrad
198 1.1 riastrad n=$(grep "$ip_local.$port > $ip_peer.$port" $outfile |wc -l)
199 1.1 riastrad
200 1.11 gson atf_expect_fail "PR kern/56252"
201 1.11 gson
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.9 riastrad
211 1.9 riastrad atf_fail "failed to trigger PR kern/56252"
212 1.1 riastrad }
213 1.1 riastrad
214 1.1 riastrad wg_handshake_timeout_cleanup()
215 1.1 riastrad {
216 1.1 riastrad
217 1.1 riastrad $DEBUG && dump
218 1.1 riastrad cleanup
219 1.1 riastrad }
220 1.1 riastrad
221 1.1 riastrad atf_test_case wg_cookie cleanup
222 1.1 riastrad wg_cookie_head()
223 1.1 riastrad {
224 1.1 riastrad
225 1.1 riastrad atf_set "descr" "tests of cookie messages of the wg(4) protocol"
226 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
227 1.1 riastrad }
228 1.1 riastrad
229 1.1 riastrad wg_cookie_body()
230 1.1 riastrad {
231 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
232 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
233 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
234 1.1 riastrad local ip_local=192.168.1.1
235 1.1 riastrad local ip_peer=192.168.1.2
236 1.1 riastrad local ip_wg_local=10.0.0.1
237 1.1 riastrad local ip_wg_peer=10.0.0.2
238 1.1 riastrad local port=51820
239 1.1 riastrad local outfile=./out
240 1.1 riastrad local rekey_timeout=5
241 1.1 riastrad
242 1.1 riastrad setup_servers
243 1.1 riastrad
244 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
245 1.1 riastrad generate_keys
246 1.1 riastrad
247 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
248 1.1 riastrad setup_common shmif0 inet $ip_local 24
249 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
250 1.6 roy add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
251 1.6 roy $ifconfig -w 10
252 1.1 riastrad
253 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
254 1.1 riastrad setup_common shmif0 inet $ip_peer 24
255 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
256 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
257 1.6 roy $ifconfig -w 10
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.9 riastrad atf_expect_fail "PR kern/56252"
274 1.9 riastrad
275 1.1 riastrad extract_new_packets $BUS > $outfile
276 1.1 riastrad $DEBUG && cat $outfile
277 1.1 riastrad # XXX length 64 indicates the message is a cookie message
278 1.1 riastrad atf_check -s exit:0 \
279 1.1 riastrad -o match:"$ip_peer.$port > $ip_local.$port: UDP, length 64" \
280 1.1 riastrad cat $outfile
281 1.1 riastrad
282 1.2 riastrad $DEBUG && $HIJACKING wgconfig wg0 show all
283 1.4 tih atf_check -s exit:0 -o match:"latest-handshake: \(never\)" \
284 1.1 riastrad $HIJACKING wgconfig wg0
285 1.1 riastrad
286 1.1 riastrad # Wait for restarting a session
287 1.1 riastrad sleep $rekey_timeout
288 1.1 riastrad
289 1.1 riastrad # The second attempt should be success because the init message has
290 1.1 riastrad # a valid cookie.
291 1.1 riastrad $ping $ip_wg_peer
292 1.1 riastrad
293 1.2 riastrad $DEBUG && $HIJACKING wgconfig wg0 show all
294 1.4 tih atf_check -s exit:0 -o not-match:"latest-handshake: \(never\)" \
295 1.1 riastrad $HIJACKING wgconfig wg0
296 1.1 riastrad
297 1.1 riastrad destroy_wg_interfaces
298 1.9 riastrad
299 1.9 riastrad atf_fail "failed to trigger PR kern/56252"
300 1.1 riastrad }
301 1.1 riastrad
302 1.1 riastrad wg_cookie_cleanup()
303 1.1 riastrad {
304 1.1 riastrad
305 1.1 riastrad $DEBUG && dump
306 1.1 riastrad cleanup
307 1.1 riastrad }
308 1.1 riastrad
309 1.1 riastrad atf_test_case wg_mobility cleanup
310 1.1 riastrad wg_mobility_head()
311 1.1 riastrad {
312 1.1 riastrad
313 1.1 riastrad atf_set "descr" "tests of the mobility of wg(4)"
314 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
315 1.1 riastrad }
316 1.1 riastrad
317 1.1 riastrad wg_mobility_body()
318 1.1 riastrad {
319 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
320 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
321 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
322 1.1 riastrad local ip_local=192.168.1.1
323 1.1 riastrad local ip_peer=192.168.1.2
324 1.1 riastrad local ip_peer_new=192.168.1.3
325 1.1 riastrad local ip_wg_local=10.0.0.1
326 1.1 riastrad local ip_wg_peer=10.0.0.2
327 1.1 riastrad local port=51820
328 1.1 riastrad local outfile=./out
329 1.1 riastrad
330 1.1 riastrad setup_servers
331 1.1 riastrad
332 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
333 1.1 riastrad generate_keys
334 1.1 riastrad
335 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
336 1.1 riastrad setup_common shmif0 inet $ip_local 24
337 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
338 1.6 roy # Initially, the local doesn't know the endpoint of the peer
339 1.6 roy add_peer wg0 peer0 $key_pub_peer "" $ip_wg_peer/32
340 1.6 roy $ifconfig -w 10
341 1.1 riastrad
342 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
343 1.1 riastrad setup_common shmif0 inet $ip_peer 24
344 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
345 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
346 1.6 roy $ifconfig -w 10
347 1.1 riastrad
348 1.1 riastrad extract_new_packets $BUS > $outfile
349 1.1 riastrad $DEBUG && cat $outfile
350 1.1 riastrad
351 1.1 riastrad # Ping from the local to the peer doesn't work because the local
352 1.1 riastrad # doesn't know the endpoint of the peer
353 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
354 1.1 riastrad $ping_fail $ip_wg_peer
355 1.1 riastrad
356 1.9 riastrad atf_expect_fail "PR kern/56252"
357 1.9 riastrad
358 1.1 riastrad extract_new_packets $BUS > $outfile
359 1.1 riastrad $DEBUG && cat $outfile
360 1.1 riastrad
361 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
362 1.1 riastrad $ping $ip_wg_local
363 1.1 riastrad
364 1.1 riastrad extract_new_packets $BUS > $outfile
365 1.1 riastrad $DEBUG && cat $outfile
366 1.1 riastrad
367 1.1 riastrad atf_check -s exit:0 -o match:"$ip_local.$port > $ip_peer.$port" cat $outfile
368 1.1 riastrad
369 1.1 riastrad # Change the IP address of the peer
370 1.1 riastrad setup_common shmif0 inet $ip_peer_new 24
371 1.6 roy $ifconfig -w 10
372 1.1 riastrad
373 1.1 riastrad # Ping from the local to the peer doesn't work because the local
374 1.1 riastrad # doesn't know the change of the IP address of the peer
375 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
376 1.1 riastrad $ping_fail $ip_wg_peer
377 1.1 riastrad
378 1.1 riastrad extract_new_packets $BUS > $outfile
379 1.1 riastrad $DEBUG && cat $outfile
380 1.1 riastrad
381 1.1 riastrad atf_check -s exit:0 -o match:"$ip_local.$port > $ip_peer.$port" cat $outfile
382 1.1 riastrad
383 1.1 riastrad # Ping from the peer to the local works because the local notices
384 1.1 riastrad # the change and updates the IP address of the peer
385 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
386 1.1 riastrad $ping $ip_wg_local
387 1.1 riastrad
388 1.1 riastrad extract_new_packets $BUS > $outfile
389 1.1 riastrad $DEBUG && cat $outfile
390 1.1 riastrad
391 1.1 riastrad atf_check -s exit:0 -o match:"$ip_local.$port > $ip_peer_new.$port" cat $outfile
392 1.1 riastrad atf_check -s exit:0 -o match:"$ip_peer_new.$port > $ip_local.$port" cat $outfile
393 1.1 riastrad atf_check -s exit:0 -o not-match:"$ip_local.$port > $ip_peer.$port" cat $outfile
394 1.1 riastrad
395 1.1 riastrad destroy_wg_interfaces
396 1.9 riastrad
397 1.9 riastrad atf_fail "failed to trigger PR kern/56252"
398 1.1 riastrad }
399 1.1 riastrad
400 1.1 riastrad wg_mobility_cleanup()
401 1.1 riastrad {
402 1.1 riastrad
403 1.1 riastrad $DEBUG && dump
404 1.1 riastrad cleanup
405 1.1 riastrad }
406 1.1 riastrad
407 1.1 riastrad atf_test_case wg_keepalive cleanup
408 1.1 riastrad wg_keepalive_head()
409 1.1 riastrad {
410 1.1 riastrad
411 1.1 riastrad atf_set "descr" "tests keepalive messages"
412 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
413 1.1 riastrad }
414 1.1 riastrad
415 1.1 riastrad wg_keepalive_body()
416 1.1 riastrad {
417 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
418 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
419 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
420 1.1 riastrad local ip_local=192.168.1.1
421 1.1 riastrad local ip_peer=192.168.1.2
422 1.1 riastrad local ip_peer_new=192.168.1.3
423 1.1 riastrad local ip_wg_local=10.0.0.1
424 1.1 riastrad local ip_wg_peer=10.0.0.2
425 1.1 riastrad local port=51820
426 1.1 riastrad local outfile=./out
427 1.1 riastrad local keepalive_timeout=3
428 1.1 riastrad
429 1.1 riastrad setup_servers
430 1.1 riastrad
431 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
432 1.1 riastrad generate_keys
433 1.1 riastrad
434 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
435 1.1 riastrad setup_common shmif0 inet $ip_local 24
436 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
437 1.6 roy add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
438 1.8 riastrad $ifconfig -w 10
439 1.1 riastrad
440 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
441 1.1 riastrad setup_common shmif0 inet $ip_peer 24
442 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
443 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
444 1.6 roy $ifconfig -w 10
445 1.1 riastrad
446 1.1 riastrad # Shorten keepalive_timeout of the peer
447 1.1 riastrad atf_check -s exit:0 -o ignore \
448 1.1 riastrad rump.sysctl -w net.wg.keepalive_timeout=$keepalive_timeout
449 1.1 riastrad
450 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
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 $ping $ip_wg_peer
456 1.1 riastrad
457 1.1 riastrad extract_new_packets $BUS > $outfile
458 1.1 riastrad $DEBUG && cat $outfile
459 1.1 riastrad
460 1.1 riastrad sleep $((keepalive_timeout + 1))
461 1.1 riastrad
462 1.1 riastrad $ping $ip_wg_peer
463 1.1 riastrad
464 1.1 riastrad extract_new_packets $BUS > $outfile
465 1.1 riastrad $DEBUG && cat $outfile
466 1.1 riastrad
467 1.1 riastrad # XXX length 32 indicates the message is a keepalive (empty) message
468 1.1 riastrad atf_check -s exit:0 -o match:"$ip_peer.$port > $ip_local.$port: UDP, length 32" \
469 1.1 riastrad cat $outfile
470 1.1 riastrad
471 1.1 riastrad destroy_wg_interfaces
472 1.1 riastrad }
473 1.1 riastrad
474 1.1 riastrad wg_keepalive_cleanup()
475 1.1 riastrad {
476 1.1 riastrad
477 1.1 riastrad $DEBUG && dump
478 1.1 riastrad cleanup
479 1.1 riastrad }
480 1.1 riastrad
481 1.1 riastrad atf_test_case wg_psk cleanup
482 1.1 riastrad wg_psk_head()
483 1.1 riastrad {
484 1.1 riastrad
485 1.1 riastrad atf_set "descr" "tests preshared-key"
486 1.1 riastrad atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
487 1.1 riastrad }
488 1.1 riastrad
489 1.1 riastrad test_psk_common()
490 1.1 riastrad {
491 1.1 riastrad }
492 1.1 riastrad
493 1.1 riastrad wg_psk_body()
494 1.1 riastrad {
495 1.1 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
496 1.1 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
497 1.1 riastrad local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
498 1.1 riastrad local ip_local=192.168.1.1
499 1.1 riastrad local ip_peer=192.168.1.2
500 1.1 riastrad local ip_peer_new=192.168.1.3
501 1.1 riastrad local ip_wg_local=10.0.0.1
502 1.1 riastrad local ip_wg_peer=10.0.0.2
503 1.1 riastrad local port=51820
504 1.1 riastrad local outfile=./out
505 1.1 riastrad local pskfile=./psk
506 1.1 riastrad local rekey_after_time=3
507 1.1 riastrad
508 1.1 riastrad setup_servers
509 1.1 riastrad
510 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
511 1.1 riastrad atf_check -s exit:0 -o ignore \
512 1.1 riastrad rump.sysctl -w net.wg.rekey_after_time=$rekey_after_time
513 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
514 1.1 riastrad atf_check -s exit:0 -o ignore \
515 1.1 riastrad rump.sysctl -w net.wg.rekey_after_time=$rekey_after_time
516 1.1 riastrad
517 1.1 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
518 1.1 riastrad generate_keys
519 1.1 riastrad key_psk=$(wg-keygen --psk)
520 1.1 riastrad $DEBUG && echo $key_psk
521 1.1 riastrad
522 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
523 1.1 riastrad setup_common shmif0 inet $ip_local 24
524 1.1 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
525 1.1 riastrad
526 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
527 1.1 riastrad setup_common shmif0 inet $ip_peer 24
528 1.1 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
529 1.1 riastrad
530 1.1 riastrad echo "$key_psk" > $pskfile
531 1.1 riastrad
532 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
533 1.1 riastrad
534 1.1 riastrad # The local always has the preshared key
535 1.1 riastrad add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32 \
536 1.1 riastrad $pskfile "$key_psk"
537 1.6 roy $ifconfig -w 10
538 1.1 riastrad
539 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
540 1.1 riastrad
541 1.1 riastrad # First, try the peer without the preshared key
542 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
543 1.6 roy $ifconfig -w 10
544 1.1 riastrad
545 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
546 1.1 riastrad
547 1.1 riastrad extract_new_packets $BUS > $outfile
548 1.1 riastrad $DEBUG && cat $outfile
549 1.1 riastrad
550 1.1 riastrad $ping_fail $ip_wg_peer
551 1.1 riastrad
552 1.1 riastrad extract_new_packets $BUS > $outfile
553 1.1 riastrad $DEBUG && cat $outfile
554 1.1 riastrad
555 1.1 riastrad # Next, try with the preshared key
556 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
557 1.1 riastrad delete_peer wg0 peer0
558 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32 \
559 1.1 riastrad $pskfile "$key_psk"
560 1.6 roy $ifconfig -w 10
561 1.1 riastrad
562 1.1 riastrad # Need a rekey
563 1.1 riastrad atf_check -s exit:0 sleep $((rekey_after_time + 1))
564 1.1 riastrad
565 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
566 1.1 riastrad
567 1.1 riastrad extract_new_packets $BUS > $outfile
568 1.1 riastrad $DEBUG && cat $outfile
569 1.1 riastrad
570 1.1 riastrad $ping $ip_wg_peer
571 1.1 riastrad
572 1.1 riastrad extract_new_packets $BUS > $outfile
573 1.1 riastrad $DEBUG && cat $outfile
574 1.1 riastrad
575 1.1 riastrad # Then, try again without the preshared key just in case
576 1.1 riastrad export RUMP_SERVER=$SOCK_PEER
577 1.1 riastrad delete_peer wg0 peer0
578 1.1 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
579 1.6 roy $ifconfig -w 10
580 1.1 riastrad
581 1.1 riastrad # Need a rekey
582 1.1 riastrad atf_check -s exit:0 sleep $((rekey_after_time + 1))
583 1.1 riastrad
584 1.1 riastrad export RUMP_SERVER=$SOCK_LOCAL
585 1.1 riastrad $ping_fail $ip_wg_peer
586 1.1 riastrad
587 1.1 riastrad rm -f $pskfile
588 1.1 riastrad
589 1.1 riastrad destroy_wg_interfaces
590 1.1 riastrad }
591 1.1 riastrad
592 1.1 riastrad wg_psk_cleanup()
593 1.1 riastrad {
594 1.1 riastrad
595 1.1 riastrad $DEBUG && dump
596 1.1 riastrad cleanup
597 1.1 riastrad }
598 1.1 riastrad
599 1.3 riastrad atf_test_case wg_malformed cleanup
600 1.3 riastrad wg_malformed_head()
601 1.3 riastrad {
602 1.3 riastrad
603 1.3 riastrad atf_set "descr" "tests malformed packet headers"
604 1.3 riastrad atf_set "require.progs" "nc" "rump_server" "wgconfig" "wg-keygen"
605 1.12 martin atf_set "timeout" "100"
606 1.3 riastrad }
607 1.3 riastrad
608 1.3 riastrad wg_malformed_body()
609 1.3 riastrad {
610 1.3 riastrad local ifconfig="atf_check -s exit:0 rump.ifconfig"
611 1.3 riastrad local ping="atf_check -s exit:0 -o ignore rump.ping -n -c 1 -w 1"
612 1.3 riastrad local ip_local=192.168.1.1
613 1.3 riastrad local ip_peer=192.168.1.2
614 1.3 riastrad local ip_wg_local=10.0.0.1
615 1.3 riastrad local ip_wg_peer=10.0.0.2
616 1.3 riastrad local port=51820
617 1.3 riastrad setup_servers
618 1.3 riastrad
619 1.3 riastrad # It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
620 1.3 riastrad generate_keys
621 1.3 riastrad
622 1.3 riastrad export RUMP_SERVER=$SOCK_LOCAL
623 1.3 riastrad setup_common shmif0 inet $ip_local 24
624 1.3 riastrad setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
625 1.6 roy add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
626 1.6 roy $ifconfig -w 10
627 1.3 riastrad
628 1.3 riastrad export RUMP_SERVER=$SOCK_PEER
629 1.3 riastrad setup_common shmif0 inet $ip_peer 24
630 1.3 riastrad setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
631 1.3 riastrad add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
632 1.6 roy $ifconfig -w 10
633 1.3 riastrad
634 1.3 riastrad export RUMP_SERVER=$SOCK_LOCAL
635 1.3 riastrad
636 1.3 riastrad $ping $ip_wg_peer
637 1.3 riastrad
638 1.3 riastrad printf 'send malformed packets\n'
639 1.3 riastrad
640 1.3 riastrad $HIJACKING ping -c 1 -n $ip_peer
641 1.3 riastrad
642 1.3 riastrad printf 'x' | $HIJACKING nc -Nu -w 0 $ip_peer $port
643 1.3 riastrad printf 'xy' | $HIJACKING nc -Nu -w 0 $ip_peer $port
644 1.3 riastrad printf 'xyz' | $HIJACKING nc -Nu -w 0 $ip_peer $port
645 1.3 riastrad printf 'xyzw' | $HIJACKING nc -Nu -w 0 $ip_peer $port
646 1.3 riastrad printf '\x00\x00\x00\x00' | $HIJACKING nc -Nu -w 0 $ip_peer $port
647 1.3 riastrad printf '\x00\x00\x00\x00z' | $HIJACKING nc -Nu -w 0 $ip_peer $port
648 1.3 riastrad printf '\x01\x00\x00\x00' | $HIJACKING nc -Nu -w 0 $ip_peer $port
649 1.3 riastrad printf '\x01\x00\x00\x00z' | $HIJACKING nc -Nu -w 0 $ip_peer $port
650 1.3 riastrad printf '\x02\x00\x00\x00' | $HIJACKING nc -Nu -w 0 $ip_peer $port
651 1.3 riastrad printf '\x02\x00\x00\x00z' | $HIJACKING nc -Nu -w 0 $ip_peer $port
652 1.3 riastrad printf '\x03\x00\x00\x00' | $HIJACKING nc -Nu -w 0 $ip_peer $port
653 1.3 riastrad printf '\x03\x00\x00\x00z' | $HIJACKING nc -Nu -w 0 $ip_peer $port
654 1.3 riastrad printf '\x04\x00\x00\x00' | $HIJACKING nc -Nu -w 0 $ip_peer $port
655 1.3 riastrad printf '\x04\x00\x00\x00z' | $HIJACKING nc -Nu -w 0 $ip_peer $port
656 1.3 riastrad
657 1.3 riastrad printf 'done sending malformed packets\n'
658 1.3 riastrad
659 1.3 riastrad $ping $ip_wg_peer
660 1.3 riastrad }
661 1.3 riastrad
662 1.3 riastrad wg_malformed_cleanup()
663 1.3 riastrad {
664 1.3 riastrad
665 1.3 riastrad $DEBUG && dump
666 1.3 riastrad cleanup
667 1.3 riastrad }
668 1.3 riastrad
669 1.1 riastrad atf_init_test_cases()
670 1.1 riastrad {
671 1.1 riastrad
672 1.1 riastrad atf_add_test_case wg_rekey
673 1.1 riastrad atf_add_test_case wg_handshake_timeout
674 1.1 riastrad atf_add_test_case wg_cookie
675 1.1 riastrad atf_add_test_case wg_mobility
676 1.1 riastrad atf_add_test_case wg_keepalive
677 1.1 riastrad atf_add_test_case wg_psk
678 1.3 riastrad atf_add_test_case wg_malformed
679 1.1 riastrad }
680