2019-03-03 05:12:09 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-06-09 01:14:34 +00:00
|
|
|
*
|
2019-01-02 00:56:33 +00:00
|
|
|
* Copyright (C) 2018-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2018-06-09 01:14:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2018-08-31 16:29:27 +00:00
|
|
|
// #include <stdlib.h>
|
2018-06-09 01:14:34 +00:00
|
|
|
// #include <sys/types.h>
|
2018-10-31 02:13:00 +00:00
|
|
|
// static void callLogger(void *func, int level, const char *msg)
|
2018-06-09 01:14:34 +00:00
|
|
|
// {
|
2018-10-31 02:13:00 +00:00
|
|
|
// ((void(*)(int, const char *))func)(level, msg);
|
2018-06-09 01:14:34 +00:00
|
|
|
// }
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-01-23 21:15:01 +00:00
|
|
|
"bytes"
|
2018-11-06 14:46:44 +00:00
|
|
|
"errors"
|
2018-06-09 01:14:34 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2019-03-03 05:12:09 +00:00
|
|
|
"golang.zx2c4.com/wireguard/device"
|
2019-02-27 05:24:56 +00:00
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
2018-06-09 01:14:34 +00:00
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
var loggerFunc unsafe.Pointer
|
2018-08-27 03:11:39 +00:00
|
|
|
var versionString *C.char
|
2018-06-09 01:14:34 +00:00
|
|
|
|
|
|
|
type CLogger struct {
|
2018-12-21 21:05:47 +00:00
|
|
|
level C.int
|
2018-06-09 01:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *CLogger) Write(p []byte) (int, error) {
|
|
|
|
if uintptr(loggerFunc) == 0 {
|
|
|
|
return 0, errors.New("No logger initialized")
|
|
|
|
}
|
2018-12-21 21:05:47 +00:00
|
|
|
message := C.CString(string(p))
|
2018-10-31 02:13:00 +00:00
|
|
|
C.callLogger(loggerFunc, l.level, message)
|
2018-08-27 03:11:39 +00:00
|
|
|
C.free(unsafe.Pointer(message))
|
2018-06-09 01:14:34 +00:00
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2019-03-03 05:12:09 +00:00
|
|
|
type tunnelHandle struct {
|
|
|
|
*device.Device
|
|
|
|
*device.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
var tunnelHandles = make(map[int32]tunnelHandle)
|
2018-06-09 01:14:34 +00:00
|
|
|
|
|
|
|
func init() {
|
2019-03-03 05:12:09 +00:00
|
|
|
versionString = C.CString(device.WireGuardGoVersion)
|
|
|
|
device.RoamingDisabled = true
|
2018-06-09 01:14:34 +00: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)
|
|
|
|
buf[n] = 0
|
|
|
|
if uintptr(loggerFunc) != 0 {
|
2019-02-27 05:24:56 +00:00
|
|
|
C.callLogger(loggerFunc, 0, (*C.char)(unsafe.Pointer(&buf[0])))
|
2018-06-09 01:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-01-22 12:09:38 +00:00
|
|
|
//export wgEnableRoaming
|
|
|
|
func wgEnableRoaming(enabled bool) {
|
2019-03-03 05:12:09 +00:00
|
|
|
device.RoamingDisabled = !enabled
|
2019-01-22 12:09:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 01:14:34 +00:00
|
|
|
//export wgSetLogger
|
|
|
|
func wgSetLogger(loggerFn uintptr) {
|
|
|
|
loggerFunc = unsafe.Pointer(loggerFn)
|
|
|
|
}
|
|
|
|
|
|
|
|
//export wgTurnOn
|
2018-12-21 21:05:47 +00:00
|
|
|
func wgTurnOn(settings string, tunFd int32) int32 {
|
2019-03-03 05:12:09 +00:00
|
|
|
logger := &device.Logger{
|
2018-12-21 21:05:47 +00:00
|
|
|
Debug: log.New(&CLogger{level: 0}, "", 0),
|
|
|
|
Info: log.New(&CLogger{level: 1}, "", 0),
|
|
|
|
Error: log.New(&CLogger{level: 2}, "", 0),
|
2018-06-09 01:14:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 05:12:09 +00:00
|
|
|
err := unix.SetNonblock(int(tunFd), true)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error.Println(err)
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
tun, err := tun.CreateTUNFromFile(os.NewFile(uintptr(tunFd), "/dev/tun"), 0)
|
2018-11-06 14:46:44 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error.Println(err)
|
|
|
|
return -1
|
|
|
|
}
|
2018-06-09 01:14:34 +00:00
|
|
|
logger.Info.Println("Attaching to interface")
|
2019-03-03 05:12:09 +00:00
|
|
|
device := device.NewDevice(tun, logger)
|
2018-06-09 01:14:34 +00:00
|
|
|
|
2019-03-03 05:12:09 +00:00
|
|
|
setError := device.IpcSetOperation(bufio.NewReader(strings.NewReader(settings)))
|
2018-06-09 01:14:34 +00:00
|
|
|
if setError != nil {
|
|
|
|
logger.Error.Println(setError)
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
device.Up()
|
|
|
|
logger.Info.Println("Device started")
|
|
|
|
|
|
|
|
var i int32
|
|
|
|
for i = 0; i < math.MaxInt32; i++ {
|
|
|
|
if _, exists := tunnelHandles[i]; !exists {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == math.MaxInt32 {
|
|
|
|
return -1
|
|
|
|
}
|
2019-03-03 05:12:09 +00:00
|
|
|
tunnelHandles[i] = tunnelHandle{device, logger}
|
2018-06-09 01:14:34 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
//export wgTurnOff
|
|
|
|
func wgTurnOff(tunnelHandle int32) {
|
2018-12-11 21:21:33 +00:00
|
|
|
device, ok := tunnelHandles[tunnelHandle]
|
2018-06-09 01:14:34 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(tunnelHandles, tunnelHandle)
|
2018-12-11 21:21:33 +00:00
|
|
|
device.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
//export wgSetConfig
|
2018-12-11 23:45:50 +00:00
|
|
|
func wgSetConfig(tunnelHandle int32, settings string) int64 {
|
2018-12-11 21:21:33 +00:00
|
|
|
device, ok := tunnelHandles[tunnelHandle]
|
|
|
|
if !ok {
|
2018-12-11 23:45:50 +00:00
|
|
|
return 0
|
2018-12-11 21:21:33 +00:00
|
|
|
}
|
2019-03-03 05:12:09 +00:00
|
|
|
err := device.IpcSetOperation(bufio.NewReader(strings.NewReader(settings)))
|
2018-12-11 21:21:33 +00:00
|
|
|
if err != nil {
|
2019-03-03 05:12:09 +00:00
|
|
|
device.Error.Println(err)
|
|
|
|
return err.ErrorCode()
|
2018-12-11 23:45:50 +00:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-01-23 21:15:01 +00:00
|
|
|
//export wgGetConfig
|
|
|
|
func wgGetConfig(tunnelHandle int32) *C.char {
|
|
|
|
device, ok := tunnelHandles[tunnelHandle]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
settings := new(bytes.Buffer)
|
|
|
|
writer := bufio.NewWriter(settings)
|
2019-03-03 05:12:09 +00:00
|
|
|
err := device.IpcGetOperation(writer)
|
2019-01-23 21:15:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
writer.Flush()
|
|
|
|
return C.CString(settings.String())
|
|
|
|
}
|
|
|
|
|
2018-12-25 21:38:32 +00:00
|
|
|
//export wgBindInterfaceScope
|
|
|
|
func wgBindInterfaceScope(tunnelHandle int32, ifscope int32) {
|
2018-12-11 23:45:50 +00:00
|
|
|
device, ok := tunnelHandles[tunnelHandle]
|
|
|
|
if !ok {
|
2018-12-25 21:38:32 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-03 05:12:09 +00:00
|
|
|
device.Info.Printf("Binding sockets to interface %d\n", ifscope)
|
|
|
|
err := device.BindSocketToInterface4(uint32(ifscope))
|
|
|
|
if err != nil {
|
|
|
|
device.Error.Printf("Unable to bind v4 socket to interface:", err)
|
2018-12-25 21:38:32 +00:00
|
|
|
}
|
2019-03-03 05:12:09 +00:00
|
|
|
err = device.BindSocketToInterface6(uint32(ifscope))
|
|
|
|
if err != nil {
|
|
|
|
device.Error.Printf("Unable to bind v6 socket to interface:", err)
|
2018-12-07 20:47:19 +00:00
|
|
|
}
|
2018-06-09 01:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//export wgVersion
|
|
|
|
func wgVersion() *C.char {
|
2018-08-27 03:11:39 +00:00
|
|
|
return versionString
|
2018-06-09 01:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {}
|