2018-07-06 04:09:48 +02:00
|
|
|
/* SPDX-License-Identifier: Apache-2.0
|
2018-05-02 21:40:01 +02:00
|
|
|
*
|
2019-01-02 01:57:15 +01:00
|
|
|
* Copyright (C) 2017-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2018-05-02 21:40:01 +02:00
|
|
|
*/
|
|
|
|
|
2018-02-07 19:19:20 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -llog
|
|
|
|
// #include <android/log.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2021-01-27 01:00:49 +01:00
|
|
|
"fmt"
|
2018-02-07 19:19:20 +01:00
|
|
|
"math"
|
2018-07-24 14:53:43 +02:00
|
|
|
"net"
|
2018-02-07 19:19:20 +01:00
|
|
|
"os"
|
2018-05-21 14:13:55 +02:00
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
|
|
|
"unsafe"
|
2020-06-22 02:58:20 +02:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"golang.zx2c4.com/wireguard/conn"
|
|
|
|
"golang.zx2c4.com/wireguard/device"
|
|
|
|
"golang.zx2c4.com/wireguard/ipc"
|
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
2018-02-07 19:19:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type AndroidLogger struct {
|
2021-01-27 01:00:49 +01:00
|
|
|
level C.int
|
|
|
|
tag *C.char
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 01:00:49 +01:00
|
|
|
func cstring(s string) *C.char {
|
|
|
|
b, err := unix.BytePtrFromString(s)
|
|
|
|
if err != nil {
|
|
|
|
b := [1]C.char{}
|
|
|
|
return &b[0]
|
|
|
|
}
|
|
|
|
return (*C.char)(unsafe.Pointer(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l AndroidLogger) Printf(format string, args ...interface{}) {
|
|
|
|
C.__android_log_write(l.level, l.tag, cstring(fmt.Sprintf(format, args...)))
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
|
2018-07-24 14:53:43 +02:00
|
|
|
type TunnelHandle struct {
|
2019-03-03 04:47:11 +01:00
|
|
|
device *device.Device
|
2018-07-24 14:53:43 +02:00
|
|
|
uapi net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
var tunnelHandles map[int32]TunnelHandle
|
2018-02-07 19:19:20 +01:00
|
|
|
|
|
|
|
func init() {
|
2018-07-24 14:53:43 +02:00
|
|
|
tunnelHandles = make(map[int32]TunnelHandle)
|
2018-05-21 14:13:55 +02:00
|
|
|
signals := make(chan os.Signal)
|
|
|
|
signal.Notify(signals, unix.SIGUSR2)
|
|
|
|
go func() {
|
|
|
|
buf := make([]byte, os.Getpagesize())
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-signals:
|
|
|
|
n := runtime.Stack(buf, true)
|
2021-01-27 01:00:49 +01:00
|
|
|
if n == len(buf) {
|
|
|
|
n--
|
|
|
|
}
|
2018-05-21 14:13:55 +02:00
|
|
|
buf[n] = 0
|
2021-01-27 01:00:49 +01:00
|
|
|
C.__android_log_write(C.ANDROID_LOG_ERROR, cstring("WireGuard/GoBackend/Stacktrace"), (*C.char)(unsafe.Pointer(&buf[0])))
|
2018-05-21 14:13:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//export wgTurnOn
|
2021-01-27 01:00:49 +01:00
|
|
|
func wgTurnOn(interfaceName string, tunFd int32, settings string) int32 {
|
|
|
|
tag := cstring("WireGuard/GoBackend/" + interfaceName)
|
2019-03-03 04:47:11 +01:00
|
|
|
logger := &device.Logger{
|
2021-01-27 01:00:49 +01:00
|
|
|
Verbosef: AndroidLogger{level: C.ANDROID_LOG_DEBUG, tag: tag}.Printf,
|
|
|
|
Errorf: AndroidLogger{level: C.ANDROID_LOG_ERROR, tag: tag}.Printf,
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 04:47:11 +01:00
|
|
|
tun, name, err := tun.CreateUnmonitoredTUNFromFD(int(tunFd))
|
2018-04-18 16:44:05 +02:00
|
|
|
if err != nil {
|
2018-11-06 15:43:09 +01:00
|
|
|
unix.Close(int(tunFd))
|
2021-01-27 01:00:49 +01:00
|
|
|
logger.Errorf("CreateUnmonitoredTUNFromFD: %v", err)
|
2018-04-19 07:55:24 +02:00
|
|
|
return -1
|
2018-04-18 16:44:05 +02:00
|
|
|
}
|
2018-05-23 03:52:26 +02:00
|
|
|
|
2021-01-27 01:00:49 +01:00
|
|
|
logger.Verbosef("Attaching to interface %v", name)
|
2019-03-03 04:47:11 +01:00
|
|
|
device := device.NewDevice(tun, logger)
|
2018-05-23 03:52:26 +02:00
|
|
|
|
2021-01-27 01:00:49 +01:00
|
|
|
err = device.IpcSet(settings)
|
|
|
|
if err != nil {
|
2018-11-06 15:43:09 +01:00
|
|
|
unix.Close(int(tunFd))
|
2021-01-27 01:00:49 +01:00
|
|
|
logger.Errorf("IpcSet: %v", err)
|
2018-02-07 19:19:20 +01:00
|
|
|
return -1
|
|
|
|
}
|
2020-10-07 10:21:02 +02:00
|
|
|
device.DisableSomeRoamingForBrokenMobileSemantics()
|
2018-02-07 19:19:20 +01:00
|
|
|
|
2019-02-27 05:13:37 +01:00
|
|
|
var uapi net.Listener
|
2018-07-31 14:27:29 +02:00
|
|
|
|
2019-03-03 04:47:11 +01:00
|
|
|
uapiFile, err := ipc.UAPIOpen(name)
|
2018-07-24 14:53:43 +02:00
|
|
|
if err != nil {
|
2021-01-27 01:00:49 +01:00
|
|
|
logger.Errorf("UAPIOpen: %v", err)
|
2018-07-31 14:27:29 +02:00
|
|
|
} else {
|
2019-03-03 04:47:11 +01:00
|
|
|
uapi, err = ipc.UAPIListen(name, uapiFile)
|
2018-07-31 14:27:29 +02:00
|
|
|
if err != nil {
|
|
|
|
uapiFile.Close()
|
2021-01-27 01:00:49 +01:00
|
|
|
logger.Errorf("UAPIListen: %v", err)
|
2018-07-31 14:27:29 +02:00
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := uapi.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-03-03 04:47:11 +01:00
|
|
|
go device.IpcHandle(conn)
|
2018-07-31 14:27:29 +02:00
|
|
|
}
|
|
|
|
}()
|
2018-07-24 14:53:43 +02:00
|
|
|
}
|
2018-07-31 14:27:29 +02:00
|
|
|
}
|
2018-07-24 14:53:43 +02:00
|
|
|
|
2021-02-11 16:00:28 +01:00
|
|
|
err = device.Up()
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Unable to bring up device: %v", err)
|
|
|
|
uapiFile.Close()
|
|
|
|
device.Close()
|
|
|
|
return -1
|
|
|
|
}
|
2021-01-27 01:00:49 +01:00
|
|
|
logger.Verbosef("Device started")
|
2018-02-07 19:19:20 +01:00
|
|
|
|
|
|
|
var i int32
|
|
|
|
for i = 0; i < math.MaxInt32; i++ {
|
|
|
|
if _, exists := tunnelHandles[i]; !exists {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == math.MaxInt32 {
|
2021-02-11 16:00:28 +01:00
|
|
|
logger.Errorf("Unable to find empty handle")
|
|
|
|
uapiFile.Close()
|
|
|
|
device.Close()
|
2018-02-07 19:19:20 +01:00
|
|
|
return -1
|
|
|
|
}
|
2018-07-24 14:53:43 +02:00
|
|
|
tunnelHandles[i] = TunnelHandle{device: device, uapi: uapi}
|
2018-02-07 19:19:20 +01:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
//export wgTurnOff
|
|
|
|
func wgTurnOff(tunnelHandle int32) {
|
2018-07-24 14:53:43 +02:00
|
|
|
handle, ok := tunnelHandles[tunnelHandle]
|
2018-02-07 19:19:20 +01:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(tunnelHandles, tunnelHandle)
|
2018-07-31 14:27:29 +02:00
|
|
|
if handle.uapi != nil {
|
|
|
|
handle.uapi.Close()
|
|
|
|
}
|
2018-07-24 14:53:43 +02:00
|
|
|
handle.device.Close()
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//export wgGetSocketV4
|
|
|
|
func wgGetSocketV4(tunnelHandle int32) int32 {
|
2018-07-24 14:53:43 +02:00
|
|
|
handle, ok := tunnelHandles[tunnelHandle]
|
2018-02-07 19:19:20 +01:00
|
|
|
if !ok {
|
|
|
|
return -1
|
|
|
|
}
|
2020-06-22 02:58:20 +02:00
|
|
|
bind, _ := handle.device.Bind().(conn.PeekLookAtSocketFd)
|
|
|
|
if bind == nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
fd, err := bind.PeekLookAtSocketFd4()
|
2018-05-26 03:01:16 +02:00
|
|
|
if err != nil {
|
2018-02-07 19:19:20 +01:00
|
|
|
return -1
|
|
|
|
}
|
2019-03-03 04:47:11 +01:00
|
|
|
return int32(fd)
|
2018-05-23 17:56:39 +02:00
|
|
|
}
|
|
|
|
|
2018-05-26 03:01:16 +02:00
|
|
|
//export wgGetSocketV6
|
|
|
|
func wgGetSocketV6(tunnelHandle int32) int32 {
|
2018-07-24 14:53:43 +02:00
|
|
|
handle, ok := tunnelHandles[tunnelHandle]
|
2018-05-23 17:56:39 +02:00
|
|
|
if !ok {
|
2018-05-26 03:01:16 +02:00
|
|
|
return -1
|
2018-05-22 19:42:12 +02:00
|
|
|
}
|
2020-06-22 02:58:20 +02:00
|
|
|
bind, _ := handle.device.Bind().(conn.PeekLookAtSocketFd)
|
|
|
|
if bind == nil {
|
|
|
|
return -1
|
|
|
|
}
|
2020-09-16 15:28:04 +02:00
|
|
|
fd, err := bind.PeekLookAtSocketFd6()
|
2018-05-26 03:01:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return -1
|
2018-05-22 19:42:12 +02:00
|
|
|
}
|
2019-03-03 04:47:11 +01:00
|
|
|
return int32(fd)
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 11:44:34 +01:00
|
|
|
//export wgGetConfig
|
|
|
|
func wgGetConfig(tunnelHandle int32) *C.char {
|
|
|
|
handle, ok := tunnelHandles[tunnelHandle]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-27 01:00:49 +01:00
|
|
|
settings, err := handle.device.IpcGet()
|
2019-11-20 11:44:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-27 01:00:49 +01:00
|
|
|
return C.CString(settings)
|
2019-11-20 11:44:34 +01:00
|
|
|
}
|
|
|
|
|
2018-06-01 16:06:56 +02:00
|
|
|
//export wgVersion
|
2018-06-05 02:17:50 +02:00
|
|
|
func wgVersion() *C.char {
|
2019-03-03 04:47:11 +01:00
|
|
|
return C.CString(device.WireGuardGoVersion)
|
2018-06-01 16:06:56 +02:00
|
|
|
}
|
|
|
|
|
2018-02-07 19:19:20 +01:00
|
|
|
func main() {}
|