Home | History | Annotate | Line # | Download | only in if_wg
t_basic.sh revision 1.6
      1 #	$NetBSD: t_basic.sh,v 1.6 2024/10/08 02:29:40 riastradh Exp $
      2 #
      3 # Copyright (c) 2018 Ryota Ozaki <ozaki.ryota (at] gmail.com>
      4 # All rights reserved.
      5 #
      6 # Redistribution and use in source and binary forms, with or without
      7 # modification, are permitted provided that the following conditions
      8 # are met:
      9 # 1. Redistributions of source code must retain the above copyright
     10 #    notice, this list of conditions and the following disclaimer.
     11 # 2. Redistributions in binary form must reproduce the above copyright
     12 #    notice, this list of conditions and the following disclaimer in the
     13 #    documentation and/or other materials provided with the distribution.
     14 #
     15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25 # POSSIBILITY OF SUCH DAMAGE.
     26 #
     27 
     28 BUS=bus
     29 SOCK_LOCAL=unix://wg_local
     30 SOCK_PEER=unix://wg_peer
     31 SOCK_PEER2=unix://wg_peer2
     32 
     33 
     34 check_ping_payload()
     35 {
     36 	local proto=$1
     37 	local ip=$2
     38 	local ping= size=
     39 
     40 	if [ $proto = inet ]; then
     41 		ping="atf_check -s exit:0 -o ignore rump.ping -n -c 1 -w 1"
     42 	else
     43 		ping="atf_check -s exit:0 -o ignore rump.ping6 -n -c 1 -X 1"
     44 	fi
     45 
     46 	for size in $(seq 1 100) $(seq 450 550) $(seq 1400 1500); do
     47 		$ping -s $size $ip
     48 	done
     49 }
     50 
     51 check_badudp()
     52 {
     53 	local proto=$1
     54 	local ip=$2
     55 	local port=51820        # XXX parametrize more clearly
     56 
     57 	if [ $proto = inet ]; then
     58 		atf_check -o ignore -e ignore \
     59 		    $HIJACKING nc -4uv -w1 $ip $port </dev/null
     60 	else
     61 		atf_check -o ignore -e ignore \
     62 		    $HIJACKING nc -6uv -w1 $ip $port </dev/null
     63 	fi
     64 }
     65 
     66 test_common()
     67 {
     68 	local type=$1
     69 	local outer_proto=$2
     70 	local inner_proto=$3
     71 	local ifconfig="atf_check -s exit:0 rump.ifconfig"
     72 	local port=51820
     73 	local ip_local= ip_peer=
     74 	local ip_wg_local= ip_wg_peer=
     75 	local outer_prefix= outer_prefixall=
     76 	local inner_prefix= inner_prefixall=
     77 
     78 	if [ $outer_proto = inet ]; then
     79 		ip_local=192.168.1.1
     80 		ip_peer=192.168.1.2
     81 		outer_prefix=24
     82 		outer_prefixall=32
     83 	else
     84 		ip_local=fc00::1
     85 		ip_peer=fc00::2
     86 		outer_prefix=64
     87 		outer_prefixall=128
     88 	fi
     89 
     90 	if [ $inner_proto = inet ]; then
     91 		ip_wg_local=10.0.0.1
     92 		ip_wg_peer=10.0.0.2
     93 		inner_prefix=24
     94 		inner_prefixall=32
     95 	else
     96 		ip_wg_local=fd00::1
     97 		ip_wg_peer=fd00::2
     98 		inner_prefix=64
     99 		inner_prefixall=128
    100 	fi
    101 
    102 	setup_servers
    103 
    104 	# It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
    105 	generate_keys
    106 
    107 	export RUMP_SERVER=$SOCK_LOCAL
    108 	setup_common shmif0 $outer_proto $ip_local $outer_prefix
    109 	setup_wg_common wg0 $inner_proto $ip_wg_local $inner_prefix $port "$key_priv_local"
    110 	add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/$inner_prefixall
    111 	$ifconfig -w 10
    112 
    113 	export RUMP_SERVER=$SOCK_PEER
    114 	setup_common shmif0 $outer_proto $ip_peer $outer_prefix
    115 	setup_wg_common wg0 $inner_proto $ip_wg_peer $inner_prefix $port "$key_priv_peer"
    116 	add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/$inner_prefixall
    117 	$ifconfig -w 10
    118 
    119 	if [ $type = basic ]; then
    120 		export RUMP_SERVER=$SOCK_LOCAL
    121 		check_ping $inner_proto $ip_wg_peer
    122 	elif [ $type = payload ]; then
    123 		export RUMP_SERVER=$SOCK_LOCAL
    124 		check_ping_payload $inner_proto $ip_wg_peer
    125 	elif [ $type = badudp ]; then
    126 		export RUMP_SERVER=$SOCK_LOCAL
    127 		check_badudp $outer_proto $ip_peer
    128 	fi
    129 
    130 	destroy_wg_interfaces
    131 }
    132 
    133 atf_test_case wg_create_destroy cleanup
    134 wg_create_destroy_head()
    135 {
    136 
    137 	atf_set "descr" "tests to create/destroy wg(4) interfaces"
    138 	atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
    139 }
    140 
    141 wg_create_destroy_body()
    142 {
    143 
    144 	rump_server_crypto_start $SOCK_LOCAL netinet6 wg
    145 
    146 	test_create_destroy_common $SOCK_LOCAL wg0 true
    147 }
    148 
    149 wg_create_destroy_cleanup()
    150 {
    151 
    152 	$DEBUG && dump
    153 	cleanup
    154 }
    155 
    156 wg_create_destroy_peers_common()
    157 {
    158 	local proto=$1
    159 	local ifconfig="atf_check -s exit:0 rump.ifconfig"
    160 	local port=51820
    161 	local ip_local= ip_peer=
    162 	local ip_wg_local= ip_wg_peer=
    163 	local outer_prefix= outer_prefixall=
    164 	local inner_prefix= inner_prefixall=
    165 
    166 	if [ $proto = inet ]; then
    167 		ip_local=192.168.1.1
    168 		ip_peer=192.168.1.2
    169 		outer_prefix=24
    170 		outer_prefixall=32
    171 		ip_wg_local=10.0.0.1
    172 		ip_wg_peer=10.0.0.2
    173 		inner_prefix=24
    174 		inner_prefixall=32
    175 	else
    176 		ip_local=fc00::1
    177 		ip_peer=fc00::2
    178 		outer_prefix=64
    179 		outer_prefixall=128
    180 		ip_wg_local=fd00::1
    181 		ip_wg_peer=fd00::2
    182 		inner_prefix=64
    183 		inner_prefixall=128
    184 	fi
    185 
    186 	rump_server_crypto_start $SOCK_LOCAL netinet6 wg
    187 	rump_server_add_iface $SOCK_LOCAL shmif0 $BUS
    188 
    189 	# It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
    190 	generate_keys
    191 
    192 	export RUMP_SERVER=$SOCK_LOCAL
    193 	setup_common shmif0 $proto $ip_local $outer_prefix
    194 	setup_wg_common wg0 $proto $ip_wg_local $inner_prefix $port "$key_priv_local"
    195 
    196 	add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/$inner_prefixall
    197 
    198 	delete_peer wg0 peer0
    199 }
    200 
    201 atf_test_case wg_create_destroy_peers_ipv4 cleanup
    202 wg_create_destroy_peers_ipv4_head()
    203 {
    204 
    205 	atf_set "descr" "tests to create/destroy peers (IPv4)"
    206 	atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
    207 }
    208 
    209 wg_create_destroy_peers_ipv4_body()
    210 {
    211 
    212 	wg_create_destroy_peers_common inet
    213 }
    214 
    215 wg_create_destroy_peers_ipv4_cleanup()
    216 {
    217 
    218 	$DEBUG && dump
    219 	cleanup
    220 }
    221 
    222 atf_test_case wg_create_destroy_peers_ipv6 cleanup
    223 wg_create_destroy_peers_ipv6_head()
    224 {
    225 
    226 	atf_set "descr" "tests to create/destroy peers (IPv6)"
    227 	atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
    228 }
    229 
    230 wg_create_destroy_peers_ipv6_body()
    231 {
    232 
    233 	wg_create_destroy_peers_common inet6
    234 }
    235 
    236 wg_create_destroy_peers_ipv6_cleanup()
    237 {
    238 
    239 	$DEBUG && dump
    240 	cleanup
    241 }
    242 
    243 add_basic_test()
    244 {
    245 	local inner=$1
    246 	local outer=$2
    247 	local ipv4=inet
    248 	local ipv6=inet6
    249 
    250 	name="wg_basic_${inner}_over_${outer}"
    251 	fulldesc="Test wg(4) with ${inner} over ${outer}"
    252 
    253 	eval inner=\$$inner
    254 	eval outer=\$$outer
    255 
    256 	atf_test_case ${name} cleanup
    257 	eval "
    258 		${name}_head() {
    259 			atf_set descr \"${fulldesc}\"
    260 			atf_set require.progs rump_server wgconfig wg-keygen
    261 		}
    262 		${name}_body() {
    263 			test_common basic $outer $inner
    264 			rump_server_destroy_ifaces
    265 		}
    266 		${name}_cleanup() {
    267 			\$DEBUG && dump
    268 			cleanup
    269 		}"
    270 	atf_add_test_case ${name}
    271 }
    272 
    273 add_payload_sizes_test()
    274 {
    275 	local inner=$1
    276 	local outer=$2
    277 	local ipv4=inet
    278 	local ipv6=inet6
    279 
    280 	name="wg_payload_sizes_${inner}_over_${outer}"
    281 	fulldesc="Test wg(4) with ${inner} over ${outer} with various payload sizes"
    282 
    283 	eval inner=\$$inner
    284 	eval outer=\$$outer
    285 
    286 	atf_test_case ${name} cleanup
    287 	eval "
    288 		${name}_head() {
    289 			atf_set descr \"${fulldesc}\"
    290 			atf_set require.progs rump_server wgconfig wg-keygen
    291 		}
    292 		${name}_body() {
    293 			test_common payload $outer $inner
    294 			rump_server_destroy_ifaces
    295 		}
    296 		${name}_cleanup() {
    297 			\$DEBUG && dump
    298 			cleanup
    299 		}"
    300 	atf_add_test_case ${name}
    301 }
    302 
    303 add_badudp_test()
    304 {
    305 	local inner=$1
    306 	local outer=$2
    307 	local ipv4=inet
    308 	local ipv6=inet6
    309 
    310 	name="wg_badudp_${inner}_over_${outer}"
    311 	fulldesc="Test wg(4) with ${inner} over ${outer} with bad UDP packets"
    312 
    313 	eval inner=\$$inner
    314 	eval outer=\$$outer
    315 
    316 	atf_test_case ${name} cleanup
    317 	eval "
    318 		${name}_head() {
    319 			atf_set descr \"${fulldesc}\"
    320 			atf_set require.progs rump_server wgconfig wg-keygen nc
    321 		}
    322 		${name}_body() {
    323 			test_common badudp $outer $inner
    324 			rump_server_destroy_ifaces
    325 		}
    326 		${name}_cleanup() {
    327 			\$DEBUG && dump
    328 			cleanup
    329 		}"
    330 	atf_add_test_case ${name}
    331 }
    332 
    333 atf_test_case wg_multiple_interfaces cleanup
    334 wg_multiple_interfaces_head()
    335 {
    336 
    337 	atf_set "descr" "tests multiple wg(4) interfaces"
    338 	atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
    339 }
    340 
    341 wg_multiple_interfaces_body()
    342 {
    343 	local ifconfig="atf_check -s exit:0 rump.ifconfig"
    344 	local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
    345 	local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
    346 	local key_priv_peer2=
    347 	local key_pub_peer2=
    348 	local ip_local=192.168.1.1
    349 	local ip_local2=192.168.2.1
    350 	local ip_peer=192.168.1.2
    351 	local ip_peer2=192.168.2.2
    352 	local ip_wg_local=10.0.0.1
    353 	local ip_wg_local2=10.0.1.1
    354 	local ip_wg_peer=10.0.0.2
    355 	local ip_wg_peer2=10.0.1.2
    356 	local port=51820
    357 	local port2=51821
    358 	local outfile=./out
    359 
    360 	setup_servers
    361 	rump_server_add_iface $SOCK_LOCAL shmif1 $BUS
    362 
    363 	rump_server_crypto_start $SOCK_PEER2 netinet6 wg
    364 	rump_server_add_iface $SOCK_PEER2 shmif0 $BUS
    365 
    366 	# It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
    367 	generate_keys
    368 	key_priv_peer2=$(wg-keygen)
    369 	key_pub_peer2=$(echo $key_priv_peer2| wg-keygen --pub)
    370 
    371 	export RUMP_SERVER=$SOCK_LOCAL
    372 	setup_common shmif0 inet $ip_local 24
    373 	setup_common shmif1 inet $ip_local2 24
    374 	setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
    375 	setup_wg_common wg1 inet $ip_wg_local2 24 $port2 "$key_priv_local"
    376 	add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
    377 	add_peer wg1 peer0 $key_pub_peer2 $ip_peer2:$port2 $ip_wg_peer2/32
    378 	$ifconfig -w 10
    379 
    380 	export RUMP_SERVER=$SOCK_PEER
    381 	setup_common shmif0 inet $ip_peer 24
    382 	setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
    383 	add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
    384 	$ifconfig -w 10
    385 
    386 	export RUMP_SERVER=$SOCK_PEER2
    387 	setup_common shmif0 inet $ip_peer2 24
    388 	setup_wg_common wg0 inet $ip_wg_peer2 24 $port2 "$key_priv_peer2"
    389 	add_peer wg0 peer0 $key_pub_local $ip_local2:$port2 $ip_wg_local2/32
    390 	$ifconfig -w 10
    391 
    392 	export RUMP_SERVER=$SOCK_LOCAL
    393 
    394 	extract_new_packets $BUS > $outfile
    395 	$DEBUG && cat $outfile
    396 
    397 	$ping $ip_wg_peer
    398 
    399 	extract_new_packets $BUS > $outfile
    400 	$DEBUG && cat $outfile
    401 
    402 	$ping $ip_wg_peer2
    403 
    404 	extract_new_packets $BUS > $outfile
    405 	$DEBUG && cat $outfile
    406 
    407 	export RUMP_SERVER=$SOCK_LOCAL
    408 	$ifconfig wg0 destroy
    409 	$ifconfig wg1 destroy
    410 	export RUMP_SERVER=$SOCK_PEER
    411 	$ifconfig wg0 destroy
    412 	export RUMP_SERVER=$SOCK_PEER2
    413 	$ifconfig wg0 destroy
    414 }
    415 
    416 wg_multiple_interfaces_cleanup()
    417 {
    418 
    419 	$DEBUG && dump
    420 	cleanup
    421 }
    422 
    423 atf_test_case wg_multiple_peers cleanup
    424 wg_multiple_peers_head()
    425 {
    426 
    427 	atf_set "descr" "tests multiple wg(4) peers"
    428 	atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
    429 }
    430 
    431 wg_multiple_peers_body()
    432 {
    433 	local ifconfig="atf_check -s exit:0 rump.ifconfig"
    434 	local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
    435 	local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
    436 	local key_priv_peer2=
    437 	local key_pub_peer2=
    438 	local ip_local=192.168.1.1
    439 	local ip_peer=192.168.1.2
    440 	local ip_peer2=192.168.1.3
    441 	local ip_wg_local=10.0.0.1
    442 	local ip_wg_peer=10.0.0.2
    443 	local ip_wg_peer2=10.0.0.3
    444 	local port=51820
    445 	local outfile=./out
    446 
    447 	setup_servers
    448 	rump_server_add_iface $SOCK_LOCAL shmif1 $BUS
    449 
    450 	rump_server_crypto_start $SOCK_PEER2 netinet6 wg
    451 	rump_server_add_iface $SOCK_PEER2 shmif0 $BUS
    452 
    453 	# It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
    454 	generate_keys
    455 	key_priv_peer2=$(wg-keygen)
    456 	key_pub_peer2=$(echo $key_priv_peer2| wg-keygen --pub)
    457 
    458 	export RUMP_SERVER=$SOCK_LOCAL
    459 	setup_common shmif0 inet $ip_local 24
    460 	setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
    461 	add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
    462 	add_peer wg0 peer1 $key_pub_peer2 $ip_peer2:$port $ip_wg_peer2/32
    463 	$ifconfig -w 10
    464 
    465 	export RUMP_SERVER=$SOCK_PEER
    466 	setup_common shmif0 inet $ip_peer 24
    467 	setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
    468 	add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
    469 	$ifconfig -w 10
    470 
    471 	export RUMP_SERVER=$SOCK_PEER2
    472 	setup_common shmif0 inet $ip_peer2 24
    473 	setup_wg_common wg0 inet $ip_wg_peer2 24 $port "$key_priv_peer2"
    474 	add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
    475 	$ifconfig -w 10
    476 
    477 	export RUMP_SERVER=$SOCK_LOCAL
    478 
    479 	extract_new_packets $BUS > $outfile
    480 	$DEBUG && cat $outfile
    481 
    482 	$ping $ip_wg_peer
    483 
    484 	extract_new_packets $BUS > $outfile
    485 	$DEBUG && cat $outfile
    486 
    487 	$ping $ip_wg_peer2
    488 
    489 	extract_new_packets $BUS > $outfile
    490 	$DEBUG && cat $outfile
    491 
    492 	export RUMP_SERVER=$SOCK_LOCAL
    493 	$ifconfig wg0 destroy
    494 	export RUMP_SERVER=$SOCK_PEER
    495 	$ifconfig wg0 destroy
    496 	export RUMP_SERVER=$SOCK_PEER2
    497 	$ifconfig wg0 destroy
    498 }
    499 
    500 wg_multiple_peers_cleanup()
    501 {
    502 
    503 	$DEBUG && dump
    504 	cleanup
    505 }
    506 
    507 atf_init_test_cases()
    508 {
    509 
    510 	add_badudp_test ipv4 ipv4
    511 	add_badudp_test ipv4 ipv6
    512 	add_badudp_test ipv6 ipv4
    513 	add_badudp_test ipv6 ipv6
    514 
    515 	add_basic_test ipv4 ipv4
    516 	add_basic_test ipv4 ipv6
    517 	add_basic_test ipv6 ipv4
    518 	add_basic_test ipv6 ipv6
    519 
    520 	add_payload_sizes_test ipv4 ipv4
    521 	add_payload_sizes_test ipv4 ipv6
    522 	add_payload_sizes_test ipv6 ipv4
    523 	add_payload_sizes_test ipv6 ipv6
    524 
    525 	atf_add_test_case wg_create_destroy
    526 	atf_add_test_case wg_create_destroy_peers_ipv4
    527 	atf_add_test_case wg_create_destroy_peers_ipv6
    528 	atf_add_test_case wg_multiple_interfaces
    529 	atf_add_test_case wg_multiple_peers
    530 }
    531