From 8028d708cbf7d19f4bb6312eac731ad8c16182f0 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 24 Jul 2018 14:53:43 +0200 Subject: [PATCH] tools: let wg(8) play with userspace implementation Signed-off-by: Jason A. Donenfeld --- app/tools/CMakeLists.txt | 1 + app/tools/libwg-go/Makefile | 2 +- .../git.zx2c4.com/wireguard-go/api-android.go | 48 +++++++++++++++---- app/tools/wireguard-go | 2 +- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app/tools/CMakeLists.txt b/app/tools/CMakeLists.txt index bc6e6745..840e9f59 100644 --- a/app/tools/CMakeLists.txt +++ b/app/tools/CMakeLists.txt @@ -21,6 +21,7 @@ add_custom_target(libwg-go.so WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib ANDROID_TOOLCHAIN_ROOT=${ANDROID_TOOLCHAIN_ROOT} ANDROID_LLVM_TRIPLE=${ANDROID_LLVM_TRIPLE} ANDROID_SYSROOT=${ANDROID_SYSROOT} + ANDROID_PACKAGE_NAME=${ANDROID_PACKAGE_NAME} CFLAGS=${CMAKE_C_FLAGS}\ -Wno-unused-command-line-argument LDFLAGS=${CMAKE_SHARED_LINKER_FLAGS}\ -fuse-ld=gold DESTDIR=${CMAKE_LIBRARY_OUTPUT_DIRECTORY} diff --git a/app/tools/libwg-go/Makefile b/app/tools/libwg-go/Makefile index 31f41a46..4e86a546 100644 --- a/app/tools/libwg-go/Makefile +++ b/app/tools/libwg-go/Makefile @@ -54,5 +54,5 @@ $(DESTDIR)/libwg-go.so: $(FILES) src/git.zx2c4.com/wireguard-go/api-android.go s mkdir -p $(subst ../wireguard-go/,./src/git.zx2c4.com/wireguard-go/,$(dir $(FILES))) $(foreach FILE,$(FILES),ln -sfrt $(subst ../wireguard-go/,./src/git.zx2c4.com/wireguard-go/,$(dir $(FILE))) $(FILE);) GOPATH=$(PWD) go get -v -d git.zx2c4.com/wireguard-go - GOPATH=$(PWD) go build -v -o $(DESTDIR)/libwg-go.so -buildmode c-shared git.zx2c4.com/wireguard-go + GOPATH=$(PWD) go build -ldflags="-X main.socketDirectory=/data/data/$(ANDROID_PACKAGE_NAME)/cache/wireguard" -v -o $(DESTDIR)/libwg-go.so -buildmode c-shared git.zx2c4.com/wireguard-go go version > .gobuildversion diff --git a/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go index d1b53849..4d87acc8 100644 --- a/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go +++ b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go @@ -16,6 +16,7 @@ import ( "io/ioutil" "log" "math" + "net" "os" "os/signal" "runtime" @@ -33,11 +34,16 @@ func (l AndroidLogger) Write(p []byte) (int, error) { return len(p), nil } -var tunnelHandles map[int32]*Device +type TunnelHandle struct { + device *Device + uapi net.Listener +} + +var tunnelHandles map[int32]TunnelHandle func init() { roamingDisabled = true - tunnelHandles = make(map[int32]*Device) + tunnelHandles = make(map[int32]TunnelHandle) signals := make(chan os.Signal) signal.Notify(signals, unix.SIGUSR2) go func() { @@ -85,6 +91,29 @@ func wgTurnOn(ifnameRef string, tun_fd int32, settings string) int32 { return -1 } + uapiFile, err := UAPIOpen(name) + if err != nil { + unix.Close(int(tun_fd)) + logger.Error.Println(err) + return -1 + } + uapi, err := UAPIListen(name, uapiFile) + if err != nil { + uapiFile.Close() + unix.Close(int(tun_fd)) + logger.Error.Println(err) + return -1 + } + go func() { + for { + conn, err := uapi.Accept() + if err != nil { + return + } + go ipcHandle(device, conn) + } + }() + device.Up() logger.Info.Println("Device started") @@ -98,27 +127,28 @@ func wgTurnOn(ifnameRef string, tun_fd int32, settings string) int32 { unix.Close(int(tun_fd)) return -1 } - tunnelHandles[i] = device + tunnelHandles[i] = TunnelHandle{device: device, uapi: uapi} return i } //export wgTurnOff func wgTurnOff(tunnelHandle int32) { - device, ok := tunnelHandles[tunnelHandle] + handle, ok := tunnelHandles[tunnelHandle] if !ok { return } delete(tunnelHandles, tunnelHandle) - device.Close() + handle.uapi.Close() + handle.device.Close() } //export wgGetSocketV4 func wgGetSocketV4(tunnelHandle int32) int32 { - device, ok := tunnelHandles[tunnelHandle] + handle, ok := tunnelHandles[tunnelHandle] if !ok { return -1 } - native, ok := device.net.bind.(*NativeBind) + native, ok := handle.device.net.bind.(*NativeBind) if !ok { return -1 } @@ -138,11 +168,11 @@ func wgGetSocketV4(tunnelHandle int32) int32 { //export wgGetSocketV6 func wgGetSocketV6(tunnelHandle int32) int32 { - device, ok := tunnelHandles[tunnelHandle] + handle, ok := tunnelHandles[tunnelHandle] if !ok { return -1 } - native, ok := device.net.bind.(*NativeBind) + native, ok := handle.device.net.bind.(*NativeBind) if !ok { return -1 } diff --git a/app/tools/wireguard-go b/app/tools/wireguard-go index 6b3b1c3b..3ad3e83c 160000 --- a/app/tools/wireguard-go +++ b/app/tools/wireguard-go @@ -1 +1 @@ -Subproject commit 6b3b1c3b918fcb9bbf1d876ad6d58c38932a2469 +Subproject commit 3ad3e83c7aea762c7030b7aa7485f48083d7d9a9