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