Home | History | Annotate | Line # | Download | only in if_wg
t_basic.sh revision 1.5
      1 #	$NetBSD: t_basic.sh,v 1.5 2024/10/08 02:28:43 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 		atf_expect_fail "PR kern/58688:" \
     64 		    " userland panic of kernel via wg(4)"
     65 	fi
     66 }
     67 
     68 test_common()
     69 {
     70 	local type=$1
     71 	local outer_proto=$2
     72 	local inner_proto=$3
     73 	local ifconfig="atf_check -s exit:0 rump.ifconfig"
     74 	local port=51820
     75 	local ip_local= ip_peer=
     76 	local ip_wg_local= ip_wg_peer=
     77 	local outer_prefix= outer_prefixall=
     78 	local inner_prefix= inner_prefixall=
     79 
     80 	if [ $outer_proto = inet ]; then
     81 		ip_local=192.168.1.1
     82 		ip_peer=192.168.1.2
     83 		outer_prefix=24
     84 		outer_prefixall=32
     85 	else
     86 		ip_local=fc00::1
     87 		ip_peer=fc00::2
     88 		outer_prefix=64
     89 		outer_prefixall=128
     90 	fi
     91 
     92 	if [ $inner_proto = inet ]; then
     93 		ip_wg_local=10.0.0.1
     94 		ip_wg_peer=10.0.0.2
     95 		inner_prefix=24
     96 		inner_prefixall=32
     97 	else
     98 		ip_wg_local=fd00::1
     99 		ip_wg_peer=fd00::2
    100 		inner_prefix=64
    101 		inner_prefixall=128
    102 	fi
    103 
    104 	setup_servers
    105 
    106 	# It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
    107 	generate_keys
    108 
    109 	export RUMP_SERVER=$SOCK_LOCAL
    110 	setup_common shmif0 $outer_proto $ip_local $outer_prefix
    111 	setup_wg_common wg0 $inner_proto $ip_wg_local $inner_prefix $port "$key_priv_local"
    112 	add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/$inner_prefixall
    113 	$ifconfig -w 10
    114 
    115 	export RUMP_SERVER=$SOCK_PEER
    116 	setup_common shmif0 $outer_proto $ip_peer $outer_prefix
    117 	setup_wg_common wg0 $inner_proto $ip_wg_peer $inner_prefix $port "$key_priv_peer"
    118 	add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/$inner_prefixall
    119 	$ifconfig -w 10
    120 
    121 	if [ $type = basic ]; then
    122 		export RUMP_SERVER=$SOCK_LOCAL
    123 		check_ping $inner_proto $ip_wg_peer
    124 	elif [ $type = payload ]; then
    125 		export RUMP_SERVER=$SOCK_LOCAL
    126 		check_ping_payload $inner_proto $ip_wg_peer
    127 	elif [ $type = badudp ]; then
    128 		export RUMP_SERVER=$SOCK_LOCAL
    129 		check_badudp $outer_proto $ip_peer
    130 	fi
    131 
    132 	destroy_wg_interfaces
    133 }
    134 
    135 atf_test_case wg_create_destroy cleanup
    136 wg_create_destroy_head()
    137 {
    138 
    139 	atf_set "descr" "tests to create/destroy wg(4) interfaces"
    140 	atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
    141 }
    142 
    143 wg_create_destroy_body()
    144 {
    145 
    146 	rump_server_crypto_start $SOCK_LOCAL netinet6 wg
    147 
    148 	test_create_destroy_common $SOCK_LOCAL wg0 true
    149 }
    150 
    151 wg_create_destroy_cleanup()
    152 {
    153 
    154 	$DEBUG && dump
    155 	cleanup
    156 }
    157 
    158 wg_create_destroy_peers_common()
    159 {
    160 	local proto=$1
    161 	local ifconfig="atf_check -s exit:0 rump.ifconfig"
    162 	local port=51820
    163 	local ip_local= ip_peer=
    164 	local ip_wg_local= ip_wg_peer=
    165 	local outer_prefix= outer_prefixall=
    166 	local inner_prefix= inner_prefixall=
    167 
    168 	if [ $proto = inet ]; then
    169 		ip_local=192.168.1.1
    170 		ip_peer=192.168.1.2
    171 		outer_prefix=24
    172 		outer_prefixall=32
    173 		ip_wg_local=10.0.0.1
    174 		ip_wg_peer=10.0.0.2
    175 		inner_prefix=24
    176 		inner_prefixall=32
    177 	else
    178 		ip_local=fc00::1
    179 		ip_peer=fc00::2
    180 		outer_prefix=64
    181 		outer_prefixall=128
    182 		ip_wg_local=fd00::1
    183 		ip_wg_peer=fd00::2
    184 		inner_prefix=64
    185 		inner_prefixall=128
    186 	fi
    187 
    188 	rump_server_crypto_start $SOCK_LOCAL netinet6 wg
    189 	rump_server_add_iface $SOCK_LOCAL shmif0 $BUS
    190 
    191 	# It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
    192 	generate_keys
    193 
    194 	export RUMP_SERVER=$SOCK_LOCAL
    195 	setup_common shmif0 $proto $ip_local $outer_prefix
    196 	setup_wg_common wg0 $proto $ip_wg_local $inner_prefix $port "$key_priv_local"
    197 
    198 	add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/$inner_prefixall
    199 
    200 	delete_peer wg0 peer0
    201 }
    202 
    203 atf_test_case wg_create_destroy_peers_ipv4 cleanup
    204 wg_create_destroy_peers_ipv4_head()
    205 {
    206 
    207 	atf_set "descr" "tests to create/destroy peers (IPv4)"
    208 	atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
    209 }
    210 
    211 wg_create_destroy_peers_ipv4_body()
    212 {
    213 
    214 	wg_create_destroy_peers_common inet
    215 }
    216 
    217 wg_create_destroy_peers_ipv4_cleanup()
    218 {
    219 
    220 	$DEBUG && dump
    221 	cleanup
    222 }
    223 
    224 atf_test_case wg_create_destroy_peers_ipv6 cleanup
    225 wg_create_destroy_peers_ipv6_head()
    226 {
    227 
    228 	atf_set "descr" "tests to create/destroy peers (IPv6)"
    229 	atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
    230 }
    231 
    232 wg_create_destroy_peers_ipv6_body()
    233 {
    234 
    235 	wg_create_destroy_peers_common inet6
    236 }
    237 
    238 wg_create_destroy_peers_ipv6_cleanup()
    239 {
    240 
    241 	$DEBUG && dump
    242 	cleanup
    243 }
    244 
    245 add_basic_test()
    246 {
    247 	local inner=$1
    248 	local outer=$2
    249 	local ipv4=inet
    250 	local ipv6=inet6
    251 
    252 	name="wg_basic_${inner}_over_${outer}"
    253 	fulldesc="Test wg(4) with ${inner} over ${outer}"
    254 
    255 	eval inner=\$$inner
    256 	eval outer=\$$outer
    257 
    258 	atf_test_case ${name} cleanup
    259 	eval "
    260 		${name}_head() {
    261 			atf_set descr \"${fulldesc}\"
    262 			atf_set require.progs rump_server wgconfig wg-keygen
    263 		}
    264 		${name}_body() {
    265 			test_common basic $outer $inner
    266 			rump_server_destroy_ifaces
    267 		}
    268 		${name}_cleanup() {
    269 			\$DEBUG && dump
    270 			cleanup
    271 		}"
    272 	atf_add_test_case ${name}
    273 }
    274 
    275 add_payload_sizes_test()
    276 {
    277 	local inner=$1
    278 	local outer=$2
    279 	local ipv4=inet
    280 	local ipv6=inet6
    281 
    282 	name="wg_payload_sizes_${inner}_over_${outer}"
    283 	fulldesc="Test wg(4) with ${inner} over ${outer} with various payload sizes"
    284 
    285 	eval inner=\$$inner
    286 	eval outer=\$$outer
    287 
    288 	atf_test_case ${name} cleanup
    289 	eval "
    290 		${name}_head() {
    291 			atf_set descr \"${fulldesc}\"
    292 			atf_set require.progs rump_server wgconfig wg-keygen
    293 		}
    294 		${name}_body() {
    295 			test_common payload $outer $inner
    296 			rump_server_destroy_ifaces
    297 		}
    298 		${name}_cleanup() {
    299 			\$DEBUG && dump
    300 			cleanup
    301 		}"
    302 	atf_add_test_case ${name}
    303 }
    304 
    305 add_badudp_test()
    306 {
    307 	local inner=$1
    308 	local outer=$2
    309 	local ipv4=inet
    310 	local ipv6=inet6
    311 
    312 	name="wg_badudp_${inner}_over_${outer}"
    313 	fulldesc="Test wg(4) with ${inner} over ${outer} with bad UDP packets"
    314 
    315 	eval inner=\$$inner
    316 	eval outer=\$$outer
    317 
    318 	atf_test_case ${name} cleanup
    319 	eval "
    320 		${name}_head() {
    321 			atf_set descr \"${fulldesc}\"
    322 			atf_set require.progs rump_server wgconfig wg-keygen nc
    323 		}
    324 		${name}_body() {
    325 			test_common badudp $outer $inner
    326 			rump_server_destroy_ifaces
    327 		}
    328 		${name}_cleanup() {
    329 			\$DEBUG && dump
    330 			cleanup
    331 		}"
    332 	atf_add_test_case ${name}
    333 }
    334 
    335 atf_test_case wg_multiple_interfaces cleanup
    336 wg_multiple_interfaces_head()
    337 {
    338 
    339 	atf_set "descr" "tests multiple wg(4) interfaces"
    340 	atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
    341 }
    342 
    343 wg_multiple_interfaces_body()
    344 {
    345 	local ifconfig="atf_check -s exit:0 rump.ifconfig"
    346 	local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
    347 	local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
    348 	local key_priv_peer2=
    349 	local key_pub_peer2=
    350 	local ip_local=192.168.1.1
    351 	local ip_local2=192.168.2.1
    352 	local ip_peer=192.168.1.2
    353 	local ip_peer2=192.168.2.2
    354 	local ip_wg_local=10.0.0.1
    355 	local ip_wg_local2=10.0.1.1
    356 	local ip_wg_peer=10.0.0.2
    357 	local ip_wg_peer2=10.0.1.2
    358 	local port=51820
    359 	local port2=51821
    360 	local outfile=./out
    361 
    362 	setup_servers
    363 	rump_server_add_iface $SOCK_LOCAL shmif1 $BUS
    364 
    365 	rump_server_crypto_start $SOCK_PEER2 netinet6 wg
    366 	rump_server_add_iface $SOCK_PEER2 shmif0 $BUS
    367 
    368 	# It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
    369 	generate_keys
    370 	key_priv_peer2=$(wg-keygen)
    371 	key_pub_peer2=$(echo $key_priv_peer2| wg-keygen --pub)
    372 
    373 	export RUMP_SERVER=$SOCK_LOCAL
    374 	setup_common shmif0 inet $ip_local 24
    375 	setup_common shmif1 inet $ip_local2 24
    376 	setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
    377 	setup_wg_common wg1 inet $ip_wg_local2 24 $port2 "$key_priv_local"
    378 	add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
    379 	add_peer wg1 peer0 $key_pub_peer2 $ip_peer2:$port2 $ip_wg_peer2/32
    380 	$ifconfig -w 10
    381 
    382 	export RUMP_SERVER=$SOCK_PEER
    383 	setup_common shmif0 inet $ip_peer 24
    384 	setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
    385 	add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
    386 	$ifconfig -w 10
    387 
    388 	export RUMP_SERVER=$SOCK_PEER2
    389 	setup_common shmif0 inet $ip_peer2 24
    390 	setup_wg_common wg0 inet $ip_wg_peer2 24 $port2 "$key_priv_peer2"
    391 	add_peer wg0 peer0 $key_pub_local $ip_local2:$port2 $ip_wg_local2/32
    392 	$ifconfig -w 10
    393 
    394 	export RUMP_SERVER=$SOCK_LOCAL
    395 
    396 	extract_new_packets $BUS > $outfile
    397 	$DEBUG && cat $outfile
    398 
    399 	$ping $ip_wg_peer
    400 
    401 	extract_new_packets $BUS > $outfile
    402 	$DEBUG && cat $outfile
    403 
    404 	$ping $ip_wg_peer2
    405 
    406 	extract_new_packets $BUS > $outfile
    407 	$DEBUG && cat $outfile
    408 
    409 	export RUMP_SERVER=$SOCK_LOCAL
    410 	$ifconfig wg0 destroy
    411 	$ifconfig wg1 destroy
    412 	export RUMP_SERVER=$SOCK_PEER
    413 	$ifconfig wg0 destroy
    414 	export RUMP_SERVER=$SOCK_PEER2
    415 	$ifconfig wg0 destroy
    416 }
    417 
    418 wg_multiple_interfaces_cleanup()
    419 {
    420 
    421 	$DEBUG && dump
    422 	cleanup
    423 }
    424 
    425 atf_test_case wg_multiple_peers cleanup
    426 wg_multiple_peers_head()
    427 {
    428 
    429 	atf_set "descr" "tests multiple wg(4) peers"
    430 	atf_set "require.progs" "rump_server" "wgconfig" "wg-keygen"
    431 }
    432 
    433 wg_multiple_peers_body()
    434 {
    435 	local ifconfig="atf_check -s exit:0 rump.ifconfig"
    436 	local ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1"
    437 	local ping_fail="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1"
    438 	local key_priv_peer2=
    439 	local key_pub_peer2=
    440 	local ip_local=192.168.1.1
    441 	local ip_peer=192.168.1.2
    442 	local ip_peer2=192.168.1.3
    443 	local ip_wg_local=10.0.0.1
    444 	local ip_wg_peer=10.0.0.2
    445 	local ip_wg_peer2=10.0.0.3
    446 	local port=51820
    447 	local outfile=./out
    448 
    449 	setup_servers
    450 	rump_server_add_iface $SOCK_LOCAL shmif1 $BUS
    451 
    452 	rump_server_crypto_start $SOCK_PEER2 netinet6 wg
    453 	rump_server_add_iface $SOCK_PEER2 shmif0 $BUS
    454 
    455 	# It sets key_priv_local key_pub_local key_priv_peer key_pub_peer
    456 	generate_keys
    457 	key_priv_peer2=$(wg-keygen)
    458 	key_pub_peer2=$(echo $key_priv_peer2| wg-keygen --pub)
    459 
    460 	export RUMP_SERVER=$SOCK_LOCAL
    461 	setup_common shmif0 inet $ip_local 24
    462 	setup_wg_common wg0 inet $ip_wg_local 24 $port "$key_priv_local"
    463 	add_peer wg0 peer0 $key_pub_peer $ip_peer:$port $ip_wg_peer/32
    464 	add_peer wg0 peer1 $key_pub_peer2 $ip_peer2:$port $ip_wg_peer2/32
    465 	$ifconfig -w 10
    466 
    467 	export RUMP_SERVER=$SOCK_PEER
    468 	setup_common shmif0 inet $ip_peer 24
    469 	setup_wg_common wg0 inet $ip_wg_peer 24 $port "$key_priv_peer"
    470 	add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
    471 	$ifconfig -w 10
    472 
    473 	export RUMP_SERVER=$SOCK_PEER2
    474 	setup_common shmif0 inet $ip_peer2 24
    475 	setup_wg_common wg0 inet $ip_wg_peer2 24 $port "$key_priv_peer2"
    476 	add_peer wg0 peer0 $key_pub_local $ip_local:$port $ip_wg_local/32
    477 	$ifconfig -w 10
    478 
    479 	export RUMP_SERVER=$SOCK_LOCAL
    480 
    481 	extract_new_packets $BUS > $outfile
    482 	$DEBUG && cat $outfile
    483 
    484 	$ping $ip_wg_peer
    485 
    486 	extract_new_packets $BUS > $outfile
    487 	$DEBUG && cat $outfile
    488 
    489 	$ping $ip_wg_peer2
    490 
    491 	extract_new_packets $BUS > $outfile
    492 	$DEBUG && cat $outfile
    493 
    494 	export RUMP_SERVER=$SOCK_LOCAL
    495 	$ifconfig wg0 destroy
    496 	export RUMP_SERVER=$SOCK_PEER
    497 	$ifconfig wg0 destroy
    498 	export RUMP_SERVER=$SOCK_PEER2
    499 	$ifconfig wg0 destroy
    500 }
    501 
    502 wg_multiple_peers_cleanup()
    503 {
    504 
    505 	$DEBUG && dump
    506 	cleanup
    507 }
    508 
    509 atf_init_test_cases()
    510 {
    511 
    512 	add_badudp_test ipv4 ipv4
    513 	add_badudp_test ipv4 ipv6
    514 	add_badudp_test ipv6 ipv4
    515 	add_badudp_test ipv6 ipv6
    516 
    517 	add_basic_test ipv4 ipv4
    518 	add_basic_test ipv4 ipv6
    519 	add_basic_test ipv6 ipv4
    520 	add_basic_test ipv6 ipv6
    521 
    522 	add_payload_sizes_test ipv4 ipv4
    523 	add_payload_sizes_test ipv4 ipv6
    524 	add_payload_sizes_test ipv6 ipv4
    525 	add_payload_sizes_test ipv6 ipv6
    526 
    527 	atf_add_test_case wg_create_destroy
    528 	atf_add_test_case wg_create_destroy_peers_ipv4
    529 	atf_add_test_case wg_create_destroy_peers_ipv6
    530 	atf_add_test_case wg_multiple_interfaces
    531 	atf_add_test_case wg_multiple_peers
    532 }
    533