2019-02-07 04:18:27 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
2021-01-28 17:52:15 +01:00
|
|
|
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
2019-02-07 04:18:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package wintun
|
|
|
|
|
|
|
|
import (
|
2020-07-22 09:15:49 +02:00
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
"syscall"
|
2019-02-07 04:18:27 +01:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
)
|
|
|
|
|
2020-07-22 09:15:49 +02:00
|
|
|
type loggerLevel int
|
2019-02-07 04:18:27 +01:00
|
|
|
|
2019-06-10 11:02:18 +02:00
|
|
|
const (
|
2020-07-22 09:15:49 +02:00
|
|
|
logInfo loggerLevel = iota
|
|
|
|
logWarn
|
|
|
|
logErr
|
2019-06-10 11:02:18 +02:00
|
|
|
)
|
2019-02-07 04:18:27 +01:00
|
|
|
|
2021-10-12 08:26:46 +02:00
|
|
|
const AdapterNameMax = 128
|
2019-02-07 22:02:51 +01:00
|
|
|
|
2020-07-22 09:15:49 +02:00
|
|
|
type Adapter struct {
|
|
|
|
handle uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2021-10-12 08:26:46 +02:00
|
|
|
modwintun = newLazyDLL("wintun.dll", setupLogger)
|
2020-07-22 09:15:49 +02:00
|
|
|
procWintunCreateAdapter = modwintun.NewProc("WintunCreateAdapter")
|
|
|
|
procWintunOpenAdapter = modwintun.NewProc("WintunOpenAdapter")
|
2021-10-12 08:26:46 +02:00
|
|
|
procWintunCloseAdapter = modwintun.NewProc("WintunCloseAdapter")
|
|
|
|
procWintunDeleteDriver = modwintun.NewProc("WintunDeleteDriver")
|
2020-07-22 09:15:49 +02:00
|
|
|
procWintunGetAdapterLUID = modwintun.NewProc("WintunGetAdapterLUID")
|
|
|
|
procWintunGetRunningDriverVersion = modwintun.NewProc("WintunGetRunningDriverVersion")
|
|
|
|
)
|
2019-02-07 04:18:27 +01:00
|
|
|
|
2021-10-12 08:26:46 +02:00
|
|
|
type TimestampedWriter interface {
|
|
|
|
WriteWithTimestamp(p []byte, ts int64) (n int, err error)
|
2019-02-07 04:18:27 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 08:26:46 +02:00
|
|
|
func logMessage(level loggerLevel, timestamp uint64, msg *uint16) int {
|
|
|
|
if tw, ok := log.Default().Writer().(TimestampedWriter); ok {
|
|
|
|
tw.WriteWithTimestamp([]byte(log.Default().Prefix()+windows.UTF16PtrToString(msg)), (int64(timestamp)-116444736000000000)*100)
|
|
|
|
} else {
|
|
|
|
log.Println(windows.UTF16PtrToString(msg))
|
2019-08-20 15:48:08 +02:00
|
|
|
}
|
2021-10-12 08:26:46 +02:00
|
|
|
return 0
|
2020-07-22 09:15:49 +02:00
|
|
|
}
|
2019-08-20 15:48:08 +02:00
|
|
|
|
2021-10-12 08:26:46 +02:00
|
|
|
func setupLogger(dll *lazyDLL) {
|
|
|
|
var callback uintptr
|
|
|
|
if runtime.GOARCH == "386" || runtime.GOARCH == "arm" {
|
|
|
|
callback = windows.NewCallback(func(level loggerLevel, timestampLow, timestampHigh uint32, msg *uint16) int {
|
|
|
|
return logMessage(level, uint64(timestampHigh)<<32|uint64(timestampLow), msg)
|
|
|
|
})
|
|
|
|
} else if runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64" {
|
|
|
|
callback = windows.NewCallback(logMessage)
|
|
|
|
}
|
|
|
|
syscall.Syscall(dll.NewProc("WintunSetLogger").Addr(), 1, callback, 0, 0)
|
2020-07-22 09:15:49 +02:00
|
|
|
}
|
2019-05-09 10:11:15 +02:00
|
|
|
|
2021-10-12 08:26:46 +02:00
|
|
|
func closeAdapter(wintun *Adapter) {
|
|
|
|
syscall.Syscall(procWintunCloseAdapter.Addr(), 1, wintun.handle, 0, 0)
|
2020-07-22 09:15:49 +02:00
|
|
|
}
|
2019-05-09 10:11:15 +02:00
|
|
|
|
2021-10-12 08:26:46 +02:00
|
|
|
// CreateAdapter creates a Wintun adapter. name is the cosmetic name of the adapter.
|
|
|
|
// tunnelType represents the type of adapter and should be "Wintun". requestedGUID is
|
|
|
|
// the GUID of the created network adapter, which then influences NLA generation
|
|
|
|
// deterministically. If it is set to nil, the GUID is chosen by the system at random,
|
|
|
|
// and hence a new NLA entry is created for each new adapter.
|
|
|
|
func CreateAdapter(name string, tunnelType string, requestedGUID *windows.GUID) (wintun *Adapter, err error) {
|
|
|
|
var name16 *uint16
|
|
|
|
name16, err = windows.UTF16PtrFromString(name)
|
2019-08-02 14:53:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
2019-02-07 04:18:27 +01:00
|
|
|
}
|
2021-10-12 08:26:46 +02:00
|
|
|
var tunnelType16 *uint16
|
|
|
|
tunnelType16, err = windows.UTF16PtrFromString(tunnelType)
|
2019-08-02 14:53:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
2019-02-07 04:18:27 +01:00
|
|
|
}
|
2021-10-12 08:26:46 +02:00
|
|
|
r0, _, e1 := syscall.Syscall(procWintunCreateAdapter.Addr(), 3, uintptr(unsafe.Pointer(name16)), uintptr(unsafe.Pointer(tunnelType16)), uintptr(unsafe.Pointer(requestedGUID)))
|
2020-07-22 09:15:49 +02:00
|
|
|
if r0 == 0 {
|
|
|
|
err = e1
|
2019-08-30 01:42:28 +02:00
|
|
|
return
|
|
|
|
}
|
2021-10-12 08:26:46 +02:00
|
|
|
wintun = &Adapter{handle: r0}
|
|
|
|
runtime.SetFinalizer(wintun, closeAdapter)
|
2020-07-22 09:15:49 +02:00
|
|
|
return
|
2019-02-07 04:18:27 +01:00
|
|
|
}
|
|
|
|
|
2021-10-12 08:26:46 +02:00
|
|
|
// OpenAdapter opens an existing Wintun adapter by name.
|
|
|
|
func OpenAdapter(name string) (wintun *Adapter, err error) {
|
|
|
|
var name16 *uint16
|
|
|
|
name16, err = windows.UTF16PtrFromString(name)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2019-06-10 11:20:49 +02:00
|
|
|
}
|
2021-10-12 08:26:46 +02:00
|
|
|
r0, _, e1 := syscall.Syscall(procWintunOpenAdapter.Addr(), 1, uintptr(unsafe.Pointer(name16)), 0, 0)
|
|
|
|
if r0 == 0 {
|
2020-07-22 09:15:49 +02:00
|
|
|
err = e1
|
|
|
|
return
|
2019-08-29 18:00:44 +02:00
|
|
|
}
|
2021-10-12 08:26:46 +02:00
|
|
|
wintun = &Adapter{handle: r0}
|
|
|
|
runtime.SetFinalizer(wintun, closeAdapter)
|
2020-07-22 09:15:49 +02:00
|
|
|
return
|
2019-08-29 18:00:44 +02:00
|
|
|
}
|
|
|
|
|
2021-10-12 08:26:46 +02:00
|
|
|
// Close closes a Wintun adapter.
|
|
|
|
func (wintun *Adapter) Close() (err error) {
|
|
|
|
runtime.SetFinalizer(wintun, nil)
|
|
|
|
r1, _, e1 := syscall.Syscall(procWintunCloseAdapter.Addr(), 1, wintun.handle, 0, 0)
|
2020-07-22 09:15:49 +02:00
|
|
|
if r1 == 0 {
|
|
|
|
err = e1
|
2019-02-07 04:18:27 +01:00
|
|
|
}
|
2020-07-22 09:15:49 +02:00
|
|
|
return
|
2019-06-04 13:57:36 +02:00
|
|
|
}
|
|
|
|
|
2021-10-12 08:26:46 +02:00
|
|
|
// Uninstall removes the driver from the system if no drivers are currently in use.
|
|
|
|
func Uninstall() (err error) {
|
|
|
|
r1, _, e1 := syscall.Syscall(procWintunDeleteDriver.Addr(), 0, 0, 0, 0)
|
2020-07-22 09:15:49 +02:00
|
|
|
if r1 == 0 {
|
|
|
|
err = e1
|
2019-08-19 09:20:23 +02:00
|
|
|
}
|
2019-10-08 09:58:58 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-22 09:15:49 +02:00
|
|
|
// RunningVersion returns the version of the running Wintun driver.
|
|
|
|
func RunningVersion() (version uint32, err error) {
|
|
|
|
r0, _, e1 := syscall.Syscall(procWintunGetRunningDriverVersion.Addr(), 0, 0, 0, 0)
|
|
|
|
version = uint32(r0)
|
|
|
|
if version == 0 {
|
|
|
|
err = e1
|
2019-06-03 14:16:34 +02:00
|
|
|
}
|
2020-07-22 09:15:49 +02:00
|
|
|
return
|
2019-06-03 14:16:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 09:15:49 +02:00
|
|
|
// LUID returns the LUID of the adapter.
|
|
|
|
func (wintun *Adapter) LUID() (luid uint64) {
|
|
|
|
syscall.Syscall(procWintunGetAdapterLUID.Addr(), 2, uintptr(wintun.handle), uintptr(unsafe.Pointer(&luid)), 0)
|
|
|
|
return
|
2020-05-02 08:49:35 +02:00
|
|
|
}
|