1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
3f08c3bdfSopenharmony_ci# Copyright (c) Linux Test Project, 2022
4f08c3bdfSopenharmony_ci# Copyright (c) 2020 Oracle and/or its affiliates. All Rights Reserved.
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ciTST_NEEDS_TMPDIR=1
7f08c3bdfSopenharmony_ciTST_NEEDS_CMDS="$TST_NEEDS_CMDS wg"
8f08c3bdfSopenharmony_ciTST_TESTFUNC=${TST_TESTFUNC:-virt_netperf_msg_sizes}
9f08c3bdfSopenharmony_ciTST_SETUP=${TST_SETUP:-wireguard_lib_setup}
10f08c3bdfSopenharmony_ciTST_CLEANUP=${TST_CLEANUP:-wireguard_lib_cleanup}
11f08c3bdfSopenharmony_ciTST_NEEDS_DRIVERS="wireguard"
12f08c3bdfSopenharmony_ciVIRT_PERF_THRESHOLD_MIN=${VIRT_PERF_THRESHOLD_MIN:-200}
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_civirt_type="wireguard"
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci# Usage: wireguard_lib_setup [TYPE]
17f08c3bdfSopenharmony_ci# TYPE: [ default | invalid_allowed_ips | invalid_pub_keys ]
18f08c3bdfSopenharmony_ciwireguard_lib_setup()
19f08c3bdfSopenharmony_ci{
20f08c3bdfSopenharmony_ci	local type="${1:-default}"
21f08c3bdfSopenharmony_ci	local pub_key0="$(wg genkey | tee wg0.key | wg pubkey)"
22f08c3bdfSopenharmony_ci	local pub_key1="$(wg genkey | tee wg1.key | wg pubkey)"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci	local port_loc="$(tst_get_unused_port ipv${TST_IPVER} dgram)"
25f08c3bdfSopenharmony_ci	local port_rmt=$(tst_rhost_run -c "tst_get_unused_port ipv${TST_IPVER} dgram")
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	# copy private key to remote host
28f08c3bdfSopenharmony_ci	tst_rhost_run -s -c "echo '$(cat wg1.key)' > wg1.key"
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	tst_res TINFO "setup wireguard UDPv${TST_IPVER} tunnel, port $port_loc/$port_rmt"
31f08c3bdfSopenharmony_ci	tst_res TINFO "lhost[$(tst_ipaddr)] <-> rhost[$(tst_ipaddr rhost)]"
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	virt_setup
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	local ka_opt="persistent-keepalive 1"
36f08c3bdfSopenharmony_ci	local allow_ip_loc="${ip_virt_local}/32,${ip6_virt_local}/128"
37f08c3bdfSopenharmony_ci	local allow_ip_rmt="${ip_virt_remote}/32,${ip6_virt_remote}/128"
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	case $type in
40f08c3bdfSopenharmony_ci	invalid_allowed_ips)
41f08c3bdfSopenharmony_ci		allow_ip_loc="${ip_virt_remote}/32,${ip6_virt_remote}/128"
42f08c3bdfSopenharmony_ci		allow_ip_rmt="${ip_virt_local}/32,${ip6_virt_local}/128"
43f08c3bdfSopenharmony_ci		tst_res TINFO "Allowed IPs are source IPs only"
44f08c3bdfSopenharmony_ci		;;
45f08c3bdfSopenharmony_ci	invalid_pub_keys)
46f08c3bdfSopenharmony_ci		pub_key0="$(wg genkey | wg pubkey)"
47f08c3bdfSopenharmony_ci		tst_res TINFO "Invalid peer public key of lhost"
48f08c3bdfSopenharmony_ci		;;
49f08c3bdfSopenharmony_ci	esac
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	ROD wg set ltp_v0 listen-port $port_loc private-key wg0.key
52f08c3bdfSopenharmony_ci	ROD wg set ltp_v0 peer $pub_key1 endpoint \
53f08c3bdfSopenharmony_ci		$(tst_ipaddr rhost):$port_rmt $ka_opt \
54f08c3bdfSopenharmony_ci		allowed-ips $allow_ip_rmt
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	tst_rhost_run -s -c \
57f08c3bdfSopenharmony_ci		"wg set ltp_v0 listen-port $port_rmt private-key wg1.key"
58f08c3bdfSopenharmony_ci	tst_rhost_run -s -c "wg set ltp_v0 peer $pub_key0 \
59f08c3bdfSopenharmony_ci		endpoint $(tst_ipaddr):$port_loc $ka_opt \
60f08c3bdfSopenharmony_ci		allowed-ips $allow_ip_loc"
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ciwireguard_lib_cleanup()
64f08c3bdfSopenharmony_ci{
65f08c3bdfSopenharmony_ci	virt_cleanup
66f08c3bdfSopenharmony_ci}
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci. virt_lib.sh
69