1*25154f5fSriastradh# $NetBSD: common.sh,v 1.1 2020/08/26 16:03:42 riastradh Exp $ 2*25154f5fSriastradh# 3*25154f5fSriastradh# Copyright (c) 2018 Ryota Ozaki <ozaki.ryota@gmail.com> 4*25154f5fSriastradh# All rights reserved. 5*25154f5fSriastradh# 6*25154f5fSriastradh# Redistribution and use in source and binary forms, with or without 7*25154f5fSriastradh# modification, are permitted provided that the following conditions 8*25154f5fSriastradh# are met: 9*25154f5fSriastradh# 1. Redistributions of source code must retain the above copyright 10*25154f5fSriastradh# notice, this list of conditions and the following disclaimer. 11*25154f5fSriastradh# 2. Redistributions in binary form must reproduce the above copyright 12*25154f5fSriastradh# notice, this list of conditions and the following disclaimer in the 13*25154f5fSriastradh# documentation and/or other materials provided with the distribution. 14*25154f5fSriastradh# 15*25154f5fSriastradh# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16*25154f5fSriastradh# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17*25154f5fSriastradh# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18*25154f5fSriastradh# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19*25154f5fSriastradh# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20*25154f5fSriastradh# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21*25154f5fSriastradh# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22*25154f5fSriastradh# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23*25154f5fSriastradh# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24*25154f5fSriastradh# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25*25154f5fSriastradh# POSSIBILITY OF SUCH DAMAGE. 26*25154f5fSriastradh# 27*25154f5fSriastradh 28*25154f5fSriastradhescape_key() 29*25154f5fSriastradh{ 30*25154f5fSriastradh 31*25154f5fSriastradh echo $1 | sed 's/\+/\\+/g' | sed 's|\/|\\/|g' 32*25154f5fSriastradh} 33*25154f5fSriastradh 34*25154f5fSriastradhsetup_servers() 35*25154f5fSriastradh{ 36*25154f5fSriastradh 37*25154f5fSriastradh rump_server_crypto_start $SOCK_LOCAL netinet6 wg 38*25154f5fSriastradh rump_server_add_iface $SOCK_LOCAL shmif0 $BUS 39*25154f5fSriastradh 40*25154f5fSriastradh rump_server_crypto_start $SOCK_PEER netinet6 wg 41*25154f5fSriastradh rump_server_add_iface $SOCK_PEER shmif0 $BUS 42*25154f5fSriastradh} 43*25154f5fSriastradh 44*25154f5fSriastradhcheck_conf_port() 45*25154f5fSriastradh{ 46*25154f5fSriastradh local ifname=$1 47*25154f5fSriastradh local port=$2 48*25154f5fSriastradh 49*25154f5fSriastradh atf_check -s exit:0 -o match:"listen-port: $port" \ 50*25154f5fSriastradh $HIJACKING wgconfig $ifname 51*25154f5fSriastradh} 52*25154f5fSriastradh 53*25154f5fSriastradhcheck_conf_privkey() 54*25154f5fSriastradh{ 55*25154f5fSriastradh local ifname=$1 56*25154f5fSriastradh local key_priv="$2" 57*25154f5fSriastradh 58*25154f5fSriastradh atf_check -s exit:0 -o match:"private-key: $(escape_key $key_priv)" \ 59*25154f5fSriastradh $HIJACKING wgconfig $ifname show private-key 60*25154f5fSriastradh} 61*25154f5fSriastradh 62*25154f5fSriastradhsetup_common() 63*25154f5fSriastradh{ 64*25154f5fSriastradh local ifconfig="atf_check -s exit:0 rump.ifconfig" 65*25154f5fSriastradh local ifname=$1 66*25154f5fSriastradh local proto=$2 67*25154f5fSriastradh local ip=$3 68*25154f5fSriastradh local prefix=$4 69*25154f5fSriastradh 70*25154f5fSriastradh $ifconfig $ifname $proto $ip/$prefix 71*25154f5fSriastradh} 72*25154f5fSriastradh 73*25154f5fSriastradhsetup_wg_common() 74*25154f5fSriastradh{ 75*25154f5fSriastradh local ifconfig="atf_check -s exit:0 rump.ifconfig" 76*25154f5fSriastradh local wgconfig="atf_check -s exit:0 $HIJACKING wgconfig" 77*25154f5fSriastradh local ifname=$1 78*25154f5fSriastradh local proto=$2 79*25154f5fSriastradh local ip=$3 80*25154f5fSriastradh local prefix=$4 81*25154f5fSriastradh local port=$5 82*25154f5fSriastradh local key_priv="$6" 83*25154f5fSriastradh local tun=$7 84*25154f5fSriastradh local privfile=./tmp 85*25154f5fSriastradh 86*25154f5fSriastradh $ifconfig $ifname create 87*25154f5fSriastradh if [ -n "$tun" ]; then 88*25154f5fSriastradh $ifconfig $ifname linkstr $tun 89*25154f5fSriastradh fi 90*25154f5fSriastradh $ifconfig $ifname $proto $ip/$prefix 91*25154f5fSriastradh $DEBUG && rump.netstat -nr 92*25154f5fSriastradh echo $key_priv > $privfile 93*25154f5fSriastradh $wgconfig $ifname set private-key $privfile 94*25154f5fSriastradh $wgconfig $ifname set listen-port $port 95*25154f5fSriastradh rm -f $privfile 96*25154f5fSriastradh $ifconfig $ifname up 97*25154f5fSriastradh $DEBUG && rump.ifconfig $ifname 98*25154f5fSriastradh 99*25154f5fSriastradh check_conf_port $ifname $port 100*25154f5fSriastradh check_conf_privkey $ifname "$key_priv" 101*25154f5fSriastradh} 102*25154f5fSriastradh 103*25154f5fSriastradhcheck_ping() 104*25154f5fSriastradh{ 105*25154f5fSriastradh local proto=$1 106*25154f5fSriastradh local ip=$2 107*25154f5fSriastradh local ping= 108*25154f5fSriastradh 109*25154f5fSriastradh if [ $proto = inet ]; then 110*25154f5fSriastradh ping="atf_check -s exit:0 -o ignore rump.ping -n -i 0.1 -c 3 -w 1" 111*25154f5fSriastradh else 112*25154f5fSriastradh ping="atf_check -s exit:0 -o ignore rump.ping6 -n -i 0.1 -c 3 -X 1" 113*25154f5fSriastradh fi 114*25154f5fSriastradh 115*25154f5fSriastradh $ping $ip 116*25154f5fSriastradh} 117*25154f5fSriastradh 118*25154f5fSriastradhcheck_ping_fail() 119*25154f5fSriastradh{ 120*25154f5fSriastradh local proto=$1 121*25154f5fSriastradh local ip=$2 122*25154f5fSriastradh local ping= 123*25154f5fSriastradh 124*25154f5fSriastradh if [ $proto = inet ]; then 125*25154f5fSriastradh ping="atf_check -s not-exit:0 -o ignore rump.ping -n -c 1 -w 1" 126*25154f5fSriastradh else 127*25154f5fSriastradh ping="atf_check -s not-exit:0 -o ignore rump.ping6 -n -c 1 -X 1" 128*25154f5fSriastradh fi 129*25154f5fSriastradh 130*25154f5fSriastradh $ping $ip 131*25154f5fSriastradh} 132*25154f5fSriastradh 133*25154f5fSriastradhdestroy_wg_interfaces() 134*25154f5fSriastradh{ 135*25154f5fSriastradh local ifconfig="atf_check -s exit:0 rump.ifconfig" 136*25154f5fSriastradh 137*25154f5fSriastradh export RUMP_SERVER=$SOCK_LOCAL 138*25154f5fSriastradh $ifconfig wg0 destroy 139*25154f5fSriastradh export RUMP_SERVER=$SOCK_PEER 140*25154f5fSriastradh $ifconfig wg0 destroy 141*25154f5fSriastradh} 142*25154f5fSriastradh 143*25154f5fSriastradhadd_peer() 144*25154f5fSriastradh{ 145*25154f5fSriastradh local wgconfig="atf_check -s exit:0 $HIJACKING wgconfig" 146*25154f5fSriastradh local ifname=$1 147*25154f5fSriastradh local peername=$2 148*25154f5fSriastradh local key=$3 149*25154f5fSriastradh local endpoint=$4 150*25154f5fSriastradh local allowedips=$5 151*25154f5fSriastradh local pskfile=$6 152*25154f5fSriastradh local key_psk="$7" 153*25154f5fSriastradh local pskopt= 154*25154f5fSriastradh local endpoint_opts= 155*25154f5fSriastradh 156*25154f5fSriastradh if [ -n "$pskfile" ]; then 157*25154f5fSriastradh pskopt="--preshared-key=$pskfile" 158*25154f5fSriastradh fi 159*25154f5fSriastradh 160*25154f5fSriastradh if [ -n "$endpoint" ]; then 161*25154f5fSriastradh endpoint_opts="--endpoint=$endpoint" 162*25154f5fSriastradh fi 163*25154f5fSriastradh 164*25154f5fSriastradh $wgconfig $ifname add peer $peername $key $endpoint_opts \ 165*25154f5fSriastradh --allowed-ips=$allowedips $pskopt 166*25154f5fSriastradh atf_check -s exit:0 -o match:"allowed-ips: $allowedips" \ 167*25154f5fSriastradh $HIJACKING wgconfig $ifname show peer $peername 168*25154f5fSriastradh if [ -n "$key_psk" ]; then 169*25154f5fSriastradh atf_check -s exit:0 \ 170*25154f5fSriastradh -o match:"preshared-key: $(escape_key $key_psk)" \ 171*25154f5fSriastradh $HIJACKING wgconfig $ifname show peer $peername \ 172*25154f5fSriastradh --show-preshared-key 173*25154f5fSriastradh else 174*25154f5fSriastradh atf_check -s exit:0 -o match:"preshared-key: \(none\)" \ 175*25154f5fSriastradh $HIJACKING wgconfig $ifname show peer $peername \ 176*25154f5fSriastradh --show-preshared-key 177*25154f5fSriastradh fi 178*25154f5fSriastradh} 179*25154f5fSriastradh 180*25154f5fSriastradhdelete_peer() 181*25154f5fSriastradh{ 182*25154f5fSriastradh local wgconfig="atf_check -s exit:0 $HIJACKING wgconfig" 183*25154f5fSriastradh local ifname=$1 184*25154f5fSriastradh local peername=$2 185*25154f5fSriastradh 186*25154f5fSriastradh $wgconfig $ifname delete peer $peername 187*25154f5fSriastradh atf_check -s exit:0 -o not-match:"peer: $peername" \ 188*25154f5fSriastradh $HIJACKING wgconfig $ifname 189*25154f5fSriastradh} 190*25154f5fSriastradh 191*25154f5fSriastradhgenerate_keys() 192*25154f5fSriastradh{ 193*25154f5fSriastradh 194*25154f5fSriastradh key_priv_local=$(wg-keygen) 195*25154f5fSriastradh key_pub_local=$(echo $key_priv_local| wg-keygen --pub) 196*25154f5fSriastradh key_priv_peer=$(wg-keygen) 197*25154f5fSriastradh key_pub_peer=$(echo $key_priv_peer| wg-keygen --pub) 198*25154f5fSriastradh 199*25154f5fSriastradh export key_priv_local key_pub_local key_priv_peer key_pub_peer 200*25154f5fSriastradh} 201