mirror of
https://git.zx2c4.com/wireguard-go
synced 2024-11-15 09:15:14 +01:00
tun: windows: get rid of retry logic
Things work fine on Windows 8.
This commit is contained in:
parent
1b550f6583
commit
3341e2d444
@ -9,7 +9,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@ -21,14 +20,10 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
packetAlignment uint32 = 4 // Number of bytes packets are aligned to in rings
|
packetAlignment uint32 = 4 // Number of bytes packets are aligned to in rings
|
||||||
packetSizeMax uint32 = 0xffff // Maximum packet size
|
packetSizeMax = 0xffff // Maximum packet size
|
||||||
packetCapacity uint32 = 0x800000 // Ring capacity, 8MiB
|
packetCapacity = 0x800000 // Ring capacity, 8MiB
|
||||||
packetTrailingSize uint32 = uint32(unsafe.Sizeof(packetHeader{})) + ((packetSizeMax + (packetAlignment - 1)) &^ (packetAlignment - 1)) - packetAlignment
|
packetTrailingSize = uint32(unsafe.Sizeof(packetHeader{})) + ((packetSizeMax + (packetAlignment - 1)) &^ (packetAlignment - 1)) - packetAlignment
|
||||||
|
ioctlRegisterRings = (51820 << 16) | (0x970 << 2) | 0 /*METHOD_BUFFERED*/ | (0x3 /*FILE_READ_DATA | FILE_WRITE_DATA*/ << 14)
|
||||||
ioctlRegisterRings uint32 = (51820 << 16) | (0x970 << 2) | 0 /*METHOD_BUFFERED*/ | (0x3 /*FILE_READ_DATA | FILE_WRITE_DATA*/ << 14)
|
|
||||||
|
|
||||||
retryRate = 4 // Number of retries per second to reopen device pipe
|
|
||||||
retryTimeout = 30 // Number of seconds to tolerate adapter unavailable
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type packetHeader struct {
|
type packetHeader struct {
|
||||||
@ -57,8 +52,7 @@ type ringDescriptor struct {
|
|||||||
|
|
||||||
type NativeTun struct {
|
type NativeTun struct {
|
||||||
wt *wintun.Wintun
|
wt *wintun.Wintun
|
||||||
tunDev windows.Handle
|
handle windows.Handle
|
||||||
tunLock sync.Mutex
|
|
||||||
close bool
|
close bool
|
||||||
rings ringDescriptor
|
rings ringDescriptor
|
||||||
events chan Event
|
events chan Event
|
||||||
@ -70,15 +64,6 @@ func packetAlign(size uint32) uint32 {
|
|||||||
return (size + (packetAlignment - 1)) &^ (packetAlignment - 1)
|
return (size + (packetAlignment - 1)) &^ (packetAlignment - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
var shouldRetryOpen = windows.RtlGetVersion().MajorVersion < 10
|
|
||||||
|
|
||||||
func maybeRetry(x int) int {
|
|
||||||
if shouldRetryOpen {
|
|
||||||
return x
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// CreateTUN creates a Wintun adapter with the given name. Should a Wintun
|
// CreateTUN creates a Wintun adapter with the given name. Should a Wintun
|
||||||
// adapter with the same name exist, it is reused.
|
// adapter with the same name exist, it is reused.
|
||||||
@ -119,7 +104,7 @@ func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID) (Dev
|
|||||||
|
|
||||||
tun := &NativeTun{
|
tun := &NativeTun{
|
||||||
wt: wt,
|
wt: wt,
|
||||||
tunDev: windows.InvalidHandle,
|
handle: windows.InvalidHandle,
|
||||||
events: make(chan Event, 10),
|
events: make(chan Event, 10),
|
||||||
errors: make(chan error, 1),
|
errors: make(chan error, 1),
|
||||||
forcedMTU: 1500,
|
forcedMTU: 1500,
|
||||||
@ -129,7 +114,7 @@ func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID) (Dev
|
|||||||
tun.rings.send.ring = &ring{}
|
tun.rings.send.ring = &ring{}
|
||||||
tun.rings.send.tailMoved, err = windows.CreateEvent(nil, 0, 0, nil)
|
tun.rings.send.tailMoved, err = windows.CreateEvent(nil, 0, 0, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
wt.DeleteInterface()
|
tun.Close()
|
||||||
return nil, fmt.Errorf("Error creating event: %v", err)
|
return nil, fmt.Errorf("Error creating event: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,98 +122,23 @@ func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID) (Dev
|
|||||||
tun.rings.receive.ring = &ring{}
|
tun.rings.receive.ring = &ring{}
|
||||||
tun.rings.receive.tailMoved, err = windows.CreateEvent(nil, 0, 0, nil)
|
tun.rings.receive.tailMoved, err = windows.CreateEvent(nil, 0, 0, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
windows.CloseHandle(tun.rings.send.tailMoved)
|
tun.Close()
|
||||||
wt.DeleteInterface()
|
|
||||||
return nil, fmt.Errorf("Error creating event: %v", err)
|
return nil, fmt.Errorf("Error creating event: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = tun.getTUN()
|
tun.handle, err = tun.wt.AdapterHandle()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
windows.CloseHandle(tun.rings.send.tailMoved)
|
tun.Close()
|
||||||
windows.CloseHandle(tun.rings.receive.tailMoved)
|
|
||||||
tun.closeTUN()
|
|
||||||
wt.DeleteInterface()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return tun, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tun *NativeTun) openTUN() error {
|
|
||||||
filename, err := tun.wt.NdisFileName()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
retries := maybeRetry(retryTimeout * retryRate)
|
|
||||||
if tun.close {
|
|
||||||
return os.ErrClosed
|
|
||||||
}
|
|
||||||
|
|
||||||
name, err := windows.UTF16PtrFromString(filename)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for tun.tunDev == windows.InvalidHandle {
|
|
||||||
tun.tunDev, err = windows.CreateFile(name, windows.GENERIC_READ|windows.GENERIC_WRITE, 0, nil, windows.OPEN_EXISTING, 0, 0)
|
|
||||||
if err != nil {
|
|
||||||
if retries > 0 && !tun.close {
|
|
||||||
time.Sleep(time.Second / retryRate)
|
|
||||||
retries--
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
atomic.StoreUint32(&tun.rings.send.ring.head, 0)
|
|
||||||
atomic.StoreUint32(&tun.rings.send.ring.tail, 0)
|
|
||||||
atomic.StoreInt32(&tun.rings.send.ring.alertable, 0)
|
|
||||||
atomic.StoreUint32(&tun.rings.receive.ring.head, 0)
|
|
||||||
atomic.StoreUint32(&tun.rings.receive.ring.tail, 0)
|
|
||||||
atomic.StoreInt32(&tun.rings.receive.ring.alertable, 0)
|
|
||||||
|
|
||||||
var bytesReturned uint32
|
var bytesReturned uint32
|
||||||
err = windows.DeviceIoControl(tun.tunDev, ioctlRegisterRings, (*byte)(unsafe.Pointer(&tun.rings)), uint32(unsafe.Sizeof(tun.rings)), nil, 0, &bytesReturned, nil)
|
err = windows.DeviceIoControl(tun.handle, ioctlRegisterRings, (*byte)(unsafe.Pointer(&tun.rings)), uint32(unsafe.Sizeof(tun.rings)), nil, 0, &bytesReturned, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error registering rings: %v", err)
|
tun.Close()
|
||||||
|
return nil, fmt.Errorf("Error registering rings: %v", err)
|
||||||
}
|
}
|
||||||
}
|
return tun, nil
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tun *NativeTun) closeTUN() (err error) {
|
|
||||||
for tun.tunDev != windows.InvalidHandle {
|
|
||||||
tun.tunLock.Lock()
|
|
||||||
if tun.tunDev == windows.InvalidHandle {
|
|
||||||
tun.tunLock.Unlock()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
t := tun.tunDev
|
|
||||||
tun.tunDev = windows.InvalidHandle
|
|
||||||
err = windows.CloseHandle(t)
|
|
||||||
tun.tunLock.Unlock()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tun *NativeTun) getTUN() (handle windows.Handle, err error) {
|
|
||||||
handle = tun.tunDev
|
|
||||||
if handle == windows.InvalidHandle {
|
|
||||||
tun.tunLock.Lock()
|
|
||||||
if tun.tunDev != windows.InvalidHandle {
|
|
||||||
handle = tun.tunDev
|
|
||||||
tun.tunLock.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = tun.openTUN()
|
|
||||||
if err == nil {
|
|
||||||
handle = tun.tunDev
|
|
||||||
}
|
|
||||||
tun.tunLock.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *NativeTun) Name() (string, error) {
|
func (tun *NativeTun) Name() (string, error) {
|
||||||
@ -245,29 +155,22 @@ func (tun *NativeTun) Events() chan Event {
|
|||||||
|
|
||||||
func (tun *NativeTun) Close() error {
|
func (tun *NativeTun) Close() error {
|
||||||
tun.close = true
|
tun.close = true
|
||||||
|
if tun.rings.send.tailMoved != 0 {
|
||||||
windows.SetEvent(tun.rings.send.tailMoved) // wake the reader if it's sleeping
|
windows.SetEvent(tun.rings.send.tailMoved) // wake the reader if it's sleeping
|
||||||
var err, err2 error
|
|
||||||
err = tun.closeTUN()
|
|
||||||
|
|
||||||
if tun.events != nil {
|
|
||||||
close(tun.events)
|
|
||||||
}
|
}
|
||||||
|
if tun.handle != windows.InvalidHandle {
|
||||||
err2 = windows.CloseHandle(tun.rings.receive.tailMoved)
|
windows.CloseHandle(tun.handle)
|
||||||
if err == nil {
|
|
||||||
err = err2
|
|
||||||
}
|
}
|
||||||
|
if tun.rings.send.tailMoved != 0 {
|
||||||
err2 = windows.CloseHandle(tun.rings.send.tailMoved)
|
windows.CloseHandle(tun.rings.send.tailMoved)
|
||||||
if err == nil {
|
|
||||||
err = err2
|
|
||||||
}
|
}
|
||||||
|
if tun.rings.send.tailMoved != 0 {
|
||||||
_, err2 = tun.wt.DeleteInterface()
|
windows.CloseHandle(tun.rings.receive.tailMoved)
|
||||||
if err == nil {
|
}
|
||||||
err = err2
|
var err error
|
||||||
|
if tun.wt != nil {
|
||||||
|
_, err = tun.wt.DeleteInterface()
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,23 +189,19 @@ func procyield(cycles uint32)
|
|||||||
// Note: Read() and Write() assume the caller comes only from a single thread; there's no locking.
|
// Note: Read() and Write() assume the caller comes only from a single thread; there's no locking.
|
||||||
|
|
||||||
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||||
|
retry:
|
||||||
select {
|
select {
|
||||||
case err := <-tun.errors:
|
case err := <-tun.errors:
|
||||||
return 0, err
|
return 0, err
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
if tun.close {
|
||||||
retries := maybeRetry(1000)
|
return 0, os.ErrClosed
|
||||||
top:
|
|
||||||
for !tun.close {
|
|
||||||
_, err := tun.getTUN()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buffHead := atomic.LoadUint32(&tun.rings.send.ring.head)
|
buffHead := atomic.LoadUint32(&tun.rings.send.ring.head)
|
||||||
if buffHead >= packetCapacity {
|
if buffHead >= packetCapacity {
|
||||||
return 0, errors.New("send ring head out of bounds")
|
return 0, os.ErrClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
@ -317,20 +216,13 @@ top:
|
|||||||
}
|
}
|
||||||
if time.Since(start) >= time.Millisecond*50 {
|
if time.Since(start) >= time.Millisecond*50 {
|
||||||
windows.WaitForSingleObject(tun.rings.send.tailMoved, windows.INFINITE)
|
windows.WaitForSingleObject(tun.rings.send.tailMoved, windows.INFINITE)
|
||||||
continue top
|
goto retry
|
||||||
}
|
}
|
||||||
procyield(1)
|
procyield(1)
|
||||||
}
|
}
|
||||||
if buffTail >= packetCapacity {
|
if buffTail >= packetCapacity {
|
||||||
if retries > 0 {
|
return 0, os.ErrClosed
|
||||||
tun.closeTUN()
|
|
||||||
time.Sleep(time.Millisecond * 2)
|
|
||||||
retries--
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
return 0, errors.New("send ring tail out of bounds")
|
|
||||||
}
|
|
||||||
retries = maybeRetry(1000)
|
|
||||||
|
|
||||||
buffContent := tun.rings.send.ring.wrap(buffTail - buffHead)
|
buffContent := tun.rings.send.ring.wrap(buffTail - buffHead)
|
||||||
if buffContent < uint32(unsafe.Sizeof(packetHeader{})) {
|
if buffContent < uint32(unsafe.Sizeof(packetHeader{})) {
|
||||||
@ -353,19 +245,13 @@ top:
|
|||||||
return int(packet.size), nil
|
return int(packet.size), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, os.ErrClosed
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tun *NativeTun) Flush() error {
|
func (tun *NativeTun) Flush() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
||||||
retries := maybeRetry(1000)
|
if tun.close {
|
||||||
for {
|
return 0, os.ErrClosed
|
||||||
_, err := tun.getTUN()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
packetSize := uint32(len(buff) - offset)
|
packetSize := uint32(len(buff) - offset)
|
||||||
@ -373,19 +259,12 @@ func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
|||||||
|
|
||||||
buffHead := atomic.LoadUint32(&tun.rings.receive.ring.head)
|
buffHead := atomic.LoadUint32(&tun.rings.receive.ring.head)
|
||||||
if buffHead >= packetCapacity {
|
if buffHead >= packetCapacity {
|
||||||
if retries > 0 {
|
return 0, os.ErrClosed
|
||||||
tun.closeTUN()
|
|
||||||
time.Sleep(time.Millisecond * 2)
|
|
||||||
retries--
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
return 0, errors.New("receive ring head out of bounds")
|
|
||||||
}
|
|
||||||
retries = maybeRetry(1000)
|
|
||||||
|
|
||||||
buffTail := atomic.LoadUint32(&tun.rings.receive.ring.tail)
|
buffTail := atomic.LoadUint32(&tun.rings.receive.ring.tail)
|
||||||
if buffTail >= packetCapacity {
|
if buffTail >= packetCapacity {
|
||||||
return 0, errors.New("receive ring tail out of bounds")
|
return 0, os.ErrClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
buffSpace := tun.rings.receive.ring.wrap(buffHead - buffTail - packetAlignment)
|
buffSpace := tun.rings.receive.ring.wrap(buffHead - buffTail - packetAlignment)
|
||||||
@ -402,7 +281,6 @@ func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
|||||||
}
|
}
|
||||||
return int(packetSize), nil
|
return int(packetSize), nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// LUID returns Windows adapter instance ID.
|
// LUID returns Windows adapter instance ID.
|
||||||
func (tun *NativeTun) LUID() uint64 {
|
func (tun *NativeTun) LUID() uint64 {
|
||||||
|
@ -612,21 +612,25 @@ func (wintun *Wintun) deviceData() (setupapi.DevInfo, *setupapi.DevInfoData, err
|
|||||||
return 0, nil, windows.ERROR_OBJECT_NOT_FOUND
|
return 0, nil, windows.ERROR_OBJECT_NOT_FOUND
|
||||||
}
|
}
|
||||||
|
|
||||||
// NdisFileName returns the Wintun NDIS device object name.
|
// AdapterHandle returns a handle to the adapter device object.
|
||||||
func (wintun *Wintun) NdisFileName() (string, error) {
|
func (wintun *Wintun) AdapterHandle() (windows.Handle, error) {
|
||||||
key, err := registry.OpenKey(registry.LOCAL_MACHINE, wintun.netRegKeyName(), registry.QUERY_VALUE)
|
key, err := registry.OpenKey(registry.LOCAL_MACHINE, wintun.netRegKeyName(), registry.QUERY_VALUE)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Network-specific registry key open failed: %v", err)
|
return windows.InvalidHandle, fmt.Errorf("Network-specific registry key open failed: %v", err)
|
||||||
}
|
}
|
||||||
defer key.Close()
|
defer key.Close()
|
||||||
|
|
||||||
// Get the interface name.
|
// Get the interface name.
|
||||||
pnpInstanceID, err := registryEx.GetStringValue(key, "PnPInstanceId")
|
pnpInstanceID, err := registryEx.GetStringValue(key, "PnPInstanceId")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("PnpInstanceId registry key read failed: %v", err)
|
return windows.InvalidHandle, fmt.Errorf("PnPInstanceId registry key read failed: %v", err)
|
||||||
}
|
}
|
||||||
mangledPnpNode := strings.ReplaceAll(fmt.Sprintf("%s\\{cac88484-7515-4c03-82e6-71a87abac361}", pnpInstanceID), "\\", "#")
|
mangledPnpNode := strings.ReplaceAll(fmt.Sprintf("%s\\{cac88484-7515-4c03-82e6-71a87abac361}", pnpInstanceID), "\\", "#")
|
||||||
return fmt.Sprintf("\\\\.\\Global\\%s", mangledPnpNode), nil
|
handle, err := windows.CreateFile(windows.StringToUTF16Ptr(fmt.Sprintf("\\\\.\\Global\\%s", mangledPnpNode)), windows.GENERIC_READ|windows.GENERIC_WRITE, 0, nil, windows.OPEN_EXISTING, 0, 0)
|
||||||
|
if err != nil {
|
||||||
|
return windows.InvalidHandle, fmt.Errorf("CreateFile on mangled PnPInstanceId path failed: %v", err)
|
||||||
|
}
|
||||||
|
return handle, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GUID returns the GUID of the interface.
|
// GUID returns the GUID of the interface.
|
||||||
|
Loading…
Reference in New Issue
Block a user