2019-02-04 17:29:52 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018-2019 WireGuard LLC. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package tun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-05-03 00:11:59 +02:00
|
|
|
"fmt"
|
2019-02-04 17:29:52 +01:00
|
|
|
"os"
|
2019-02-20 13:12:08 +01:00
|
|
|
"sync"
|
2019-07-11 10:35:47 +02:00
|
|
|
"sync/atomic"
|
2019-03-20 21:45:40 +01:00
|
|
|
"time"
|
2019-02-22 16:16:14 +01:00
|
|
|
"unsafe"
|
2019-02-04 17:29:52 +01:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2019-07-05 07:54:25 +02:00
|
|
|
|
2019-02-19 18:49:24 +01:00
|
|
|
"golang.zx2c4.com/wireguard/tun/wintun"
|
2019-02-04 17:29:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-07-11 10:35:47 +02:00
|
|
|
packetAlignment uint32 = 4 // Number of bytes packets are aligned to in rings
|
|
|
|
packetSizeMax uint32 = 0xffff // Maximum packet size
|
|
|
|
packetCapacity uint32 = 0x100000 // Ring capacity (defaults to 1MiB, must be a power of 2)
|
|
|
|
packetTrailingSize uint32 = uint32(unsafe.Sizeof(packetHeader{})) + ((packetSizeMax + (packetAlignment - 1)) &^ (packetAlignment - 1)) - packetAlignment
|
|
|
|
|
|
|
|
ioctlRegisterRings uint32 = (0x22 /*FILE_DEVICE_UNKNOWN*/ << 16) | (0x800 << 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
|
2019-02-04 17:29:52 +01:00
|
|
|
)
|
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
type packetHeader struct {
|
|
|
|
size uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
type packet struct {
|
|
|
|
packetHeader
|
|
|
|
data [packetSizeMax]byte
|
2019-02-20 11:41:37 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
type ring struct {
|
|
|
|
head uint32
|
|
|
|
tail uint32
|
|
|
|
alertable int32
|
|
|
|
data [packetCapacity + packetTrailingSize]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type ringDescriptor struct {
|
|
|
|
send, receive struct {
|
|
|
|
size uint32
|
|
|
|
ring *ring
|
|
|
|
tailMoved windows.Handle
|
|
|
|
}
|
2019-02-20 11:41:37 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
type NativeTun struct {
|
2019-07-11 10:35:47 +02:00
|
|
|
wt *wintun.Wintun
|
|
|
|
tunDev windows.Handle
|
|
|
|
tunLock sync.Mutex
|
|
|
|
close bool
|
|
|
|
rings ringDescriptor
|
|
|
|
events chan Event
|
|
|
|
errors chan error
|
|
|
|
forcedMTU int
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 18:49:24 +01:00
|
|
|
func packetAlign(size uint32) uint32 {
|
2019-07-11 10:35:47 +02:00
|
|
|
return (size + (packetAlignment - 1)) &^ (packetAlignment - 1)
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-06-18 16:08:28 +02:00
|
|
|
var shouldRetryOpen = windows.RtlGetVersion().MajorVersion < 10
|
|
|
|
|
|
|
|
func maybeRetry(x int) int {
|
|
|
|
if shouldRetryOpen {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-03-04 14:27:16 +01:00
|
|
|
//
|
|
|
|
// CreateTUN creates a Wintun adapter with the given name. Should a Wintun
|
|
|
|
// adapter with the same name exist, it is reused.
|
|
|
|
//
|
2019-06-10 23:33:40 +02:00
|
|
|
func CreateTUN(ifname string) (Device, error) {
|
2019-06-09 19:20:17 +02:00
|
|
|
return CreateTUNWithRequestedGUID(ifname, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// CreateTUNWithRequestedGUID creates a Wintun adapter with the given name and
|
|
|
|
// a requested GUID. Should a Wintun adapter with the same name exist, it is reused.
|
|
|
|
//
|
2019-06-10 23:33:40 +02:00
|
|
|
func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID) (Device, error) {
|
2019-05-03 00:11:59 +02:00
|
|
|
var err error
|
|
|
|
var wt *wintun.Wintun
|
|
|
|
|
2019-05-09 10:11:15 +02:00
|
|
|
// Does an interface with this name already exist?
|
2019-06-10 11:02:18 +02:00
|
|
|
wt, err = wintun.GetInterface(ifname)
|
2019-05-17 16:06:02 +02:00
|
|
|
if err == nil {
|
|
|
|
// If so, we delete it, in case it has weird residual configuration.
|
2019-06-10 11:02:18 +02:00
|
|
|
_, err = wt.DeleteInterface()
|
2019-02-06 22:30:14 +01:00
|
|
|
if err != nil {
|
2019-05-17 16:06:02 +02:00
|
|
|
return nil, fmt.Errorf("Unable to delete already existing Wintun interface: %v", err)
|
2019-02-06 22:30:14 +01:00
|
|
|
}
|
2019-05-24 09:28:50 +02:00
|
|
|
} else if err == windows.ERROR_ALREADY_EXISTS {
|
|
|
|
return nil, fmt.Errorf("Foreign network interface with the same name exists")
|
2019-02-07 22:02:51 +01:00
|
|
|
}
|
2019-06-10 11:02:18 +02:00
|
|
|
wt, _, err = wintun.CreateInterface("WireGuard Tunnel Adapter", requestedGUID)
|
2019-03-31 10:17:11 +02:00
|
|
|
if err != nil {
|
2019-05-17 16:06:02 +02:00
|
|
|
return nil, fmt.Errorf("Unable to create Wintun interface: %v", err)
|
2019-03-31 10:17:11 +02:00
|
|
|
}
|
2019-02-07 22:02:51 +01:00
|
|
|
|
2019-05-17 16:06:02 +02:00
|
|
|
err = wt.SetInterfaceName(ifname)
|
2019-02-07 18:24:28 +01:00
|
|
|
if err != nil {
|
2019-06-10 11:02:18 +02:00
|
|
|
wt.DeleteInterface()
|
2019-05-17 16:06:02 +02:00
|
|
|
return nil, fmt.Errorf("Unable to set name of Wintun interface: %v", err)
|
2019-02-07 18:24:28 +01:00
|
|
|
}
|
2019-02-06 22:30:14 +01:00
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
tun := &NativeTun{
|
2019-03-18 09:42:00 +01:00
|
|
|
wt: wt,
|
2019-07-11 10:35:47 +02:00
|
|
|
tunDev: windows.InvalidHandle,
|
2019-06-10 23:33:40 +02:00
|
|
|
events: make(chan Event, 10),
|
2019-03-18 09:42:00 +01:00
|
|
|
errors: make(chan error, 1),
|
2019-05-16 10:33:47 +02:00
|
|
|
forcedMTU: 1500,
|
2019-07-11 10:35:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tun.rings.send.size = uint32(unsafe.Sizeof(ring{}))
|
|
|
|
tun.rings.send.ring = &ring{}
|
|
|
|
tun.rings.send.tailMoved, err = windows.CreateEvent(nil, 0, 0, nil)
|
|
|
|
if err != nil {
|
|
|
|
wt.DeleteInterface()
|
|
|
|
return nil, fmt.Errorf("Error creating event: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tun.rings.receive.size = uint32(unsafe.Sizeof(ring{}))
|
|
|
|
tun.rings.receive.ring = &ring{}
|
|
|
|
tun.rings.receive.tailMoved, err = windows.CreateEvent(nil, 0, 0, nil)
|
|
|
|
if err != nil {
|
|
|
|
windows.CloseHandle(tun.rings.send.tailMoved)
|
|
|
|
wt.DeleteInterface()
|
|
|
|
return nil, fmt.Errorf("Error creating event: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tun, nil
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 21:45:40 +01:00
|
|
|
func (tun *NativeTun) openTUN() error {
|
2019-07-18 12:26:57 +02:00
|
|
|
filename, err := tun.wt.NdisFileName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-18 16:08:28 +02:00
|
|
|
retries := maybeRetry(retryTimeout * retryRate)
|
2019-03-21 22:20:09 +01:00
|
|
|
if tun.close {
|
|
|
|
return os.ErrClosed
|
|
|
|
}
|
2019-03-20 21:45:40 +01:00
|
|
|
|
2019-07-18 12:26:57 +02:00
|
|
|
name, err := windows.UTF16PtrFromString(filename)
|
2019-07-11 10:35:47 +02:00
|
|
|
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)
|
2019-03-21 22:20:09 +01:00
|
|
|
if err != nil {
|
|
|
|
if retries > 0 && !tun.close {
|
|
|
|
time.Sleep(time.Second / retryRate)
|
|
|
|
retries--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-07-11 10:35:47 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
err = windows.DeviceIoControl(tun.tunDev, ioctlRegisterRings, (*byte)(unsafe.Pointer(&tun.rings)), uint32(unsafe.Sizeof(tun.rings)), nil, 0, &bytesReturned, nil)
|
2019-02-04 17:29:52 +01:00
|
|
|
if err != nil {
|
2019-07-11 10:35:47 +02:00
|
|
|
return fmt.Errorf("Error registering rings: %v", err)
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-21 22:20:09 +01:00
|
|
|
return nil
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) closeTUN() (err error) {
|
2019-07-11 10:35:47 +02:00
|
|
|
for tun.tunDev != windows.InvalidHandle {
|
2019-03-21 22:20:09 +01:00
|
|
|
tun.tunLock.Lock()
|
2019-07-11 10:35:47 +02:00
|
|
|
if tun.tunDev == windows.InvalidHandle {
|
2019-03-21 22:20:09 +01:00
|
|
|
tun.tunLock.Unlock()
|
|
|
|
break
|
|
|
|
}
|
2019-07-11 10:35:47 +02:00
|
|
|
t := tun.tunDev
|
|
|
|
tun.tunDev = windows.InvalidHandle
|
|
|
|
err = windows.CloseHandle(t)
|
2019-03-21 22:20:09 +01:00
|
|
|
tun.tunLock.Unlock()
|
|
|
|
break
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
func (tun *NativeTun) getTUN() (handle windows.Handle, err error) {
|
|
|
|
handle = tun.tunDev
|
|
|
|
if handle == windows.InvalidHandle {
|
2019-03-18 09:42:00 +01:00
|
|
|
tun.tunLock.Lock()
|
2019-07-11 10:35:47 +02:00
|
|
|
if tun.tunDev != windows.InvalidHandle {
|
|
|
|
handle = tun.tunDev
|
2019-03-21 22:20:09 +01:00
|
|
|
tun.tunLock.Unlock()
|
|
|
|
return
|
2019-03-20 21:45:40 +01:00
|
|
|
}
|
2019-03-21 22:20:09 +01:00
|
|
|
err = tun.openTUN()
|
|
|
|
if err == nil {
|
2019-07-11 10:35:47 +02:00
|
|
|
handle = tun.tunDev
|
2019-03-21 22:20:09 +01:00
|
|
|
}
|
|
|
|
tun.tunLock.Unlock()
|
|
|
|
return
|
2019-02-20 13:12:08 +01:00
|
|
|
}
|
2019-03-21 22:20:09 +01:00
|
|
|
return
|
2019-02-20 13:12:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) Name() (string, error) {
|
2019-05-22 19:31:52 +02:00
|
|
|
return tun.wt.InterfaceName()
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) File() *os.File {
|
2019-02-04 17:29:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-10 23:33:40 +02:00
|
|
|
func (tun *NativeTun) Events() chan Event {
|
2019-02-04 17:29:52 +01:00
|
|
|
return tun.events
|
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) Close() error {
|
2019-03-20 21:45:40 +01:00
|
|
|
tun.close = true
|
2019-07-11 10:35:47 +02:00
|
|
|
windows.SetEvent(tun.rings.send.tailMoved) // wake the reader if it's sleeping
|
|
|
|
var err, err2 error
|
|
|
|
err = tun.closeTUN()
|
2019-03-26 15:57:53 +01:00
|
|
|
|
2019-02-04 17:29:52 +01:00
|
|
|
if tun.events != nil {
|
|
|
|
close(tun.events)
|
|
|
|
}
|
2019-03-26 15:57:53 +01:00
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
err2 = windows.CloseHandle(tun.rings.receive.tailMoved)
|
|
|
|
if err == nil {
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
|
|
|
|
err2 = windows.CloseHandle(tun.rings.send.tailMoved)
|
|
|
|
if err == nil {
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err2 = tun.wt.DeleteInterface()
|
|
|
|
if err == nil {
|
|
|
|
err = err2
|
2019-03-26 15:57:53 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
return err
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) MTU() (int, error) {
|
2019-05-16 10:33:47 +02:00
|
|
|
return tun.forcedMTU, nil
|
2019-03-13 09:52:32 +01:00
|
|
|
}
|
|
|
|
|
2019-06-06 23:00:15 +02:00
|
|
|
// TODO: This is a temporary hack. We really need to be monitoring the interface in real time and adapting to MTU changes.
|
2019-05-16 10:33:47 +02:00
|
|
|
func (tun *NativeTun) ForceMTU(mtu int) {
|
|
|
|
tun.forcedMTU = mtu
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 09:06:34 +02:00
|
|
|
//go:linkname procyield runtime.procyield
|
|
|
|
func procyield(cycles uint32)
|
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
// Note: Read() and Write() assume the caller comes only from a single thread; there's no locking.
|
|
|
|
|
2019-03-01 00:05:57 +01:00
|
|
|
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
2019-02-04 17:29:52 +01:00
|
|
|
select {
|
|
|
|
case err := <-tun.errors:
|
|
|
|
return 0, err
|
|
|
|
default:
|
2019-02-08 14:31:05 +01:00
|
|
|
}
|
2019-02-04 17:29:52 +01:00
|
|
|
|
2019-06-18 16:08:28 +02:00
|
|
|
retries := maybeRetry(1000)
|
2019-07-17 09:06:34 +02:00
|
|
|
top:
|
2019-07-11 10:35:47 +02:00
|
|
|
for !tun.close {
|
|
|
|
_, err := tun.getTUN()
|
2019-02-20 13:12:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
buffHead := atomic.LoadUint32(&tun.rings.send.ring.head)
|
|
|
|
if buffHead >= packetCapacity {
|
|
|
|
return 0, errors.New("send ring head out of bounds")
|
|
|
|
}
|
|
|
|
|
2019-07-17 09:06:34 +02:00
|
|
|
start := time.Now()
|
|
|
|
var buffTail uint32
|
|
|
|
for {
|
|
|
|
buffTail = atomic.LoadUint32(&tun.rings.send.ring.tail)
|
|
|
|
if buffHead != buffTail {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if tun.close {
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
|
|
|
if time.Since(start) >= time.Millisecond*50 {
|
|
|
|
windows.WaitForSingleObject(tun.rings.send.tailMoved, windows.INFINITE)
|
|
|
|
continue top
|
|
|
|
}
|
|
|
|
procyield(1)
|
2019-07-11 10:35:47 +02:00
|
|
|
}
|
|
|
|
if buffTail >= packetCapacity {
|
|
|
|
if retries > 0 {
|
2019-05-31 15:40:08 +02:00
|
|
|
tun.closeTUN()
|
|
|
|
time.Sleep(time.Millisecond * 2)
|
2019-07-11 10:35:47 +02:00
|
|
|
retries--
|
2019-05-31 15:40:08 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-07-11 10:35:47 +02:00
|
|
|
return 0, errors.New("send ring tail out of bounds")
|
2019-02-08 14:31:05 +01:00
|
|
|
}
|
2019-07-11 10:35:47 +02:00
|
|
|
retries = maybeRetry(1000)
|
|
|
|
|
|
|
|
buffContent := tun.rings.send.ring.wrap(buffTail - buffHead)
|
|
|
|
if buffContent < uint32(unsafe.Sizeof(packetHeader{})) {
|
|
|
|
return 0, errors.New("incomplete packet header in send ring")
|
2019-05-31 15:40:08 +02:00
|
|
|
}
|
2019-07-11 10:35:47 +02:00
|
|
|
|
|
|
|
packet := (*packet)(unsafe.Pointer(&tun.rings.send.ring.data[buffHead]))
|
|
|
|
if packet.size > packetSizeMax {
|
|
|
|
return 0, errors.New("packet too big in send ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
alignedPacketSize := packetAlign(uint32(unsafe.Sizeof(packetHeader{})) + packet.size)
|
|
|
|
if alignedPacketSize > buffContent {
|
|
|
|
return 0, errors.New("incomplete packet in send ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(buff[offset:], packet.data[:packet.size])
|
|
|
|
buffHead = tun.rings.send.ring.wrap(buffHead + alignedPacketSize)
|
|
|
|
atomic.StoreUint32(&tun.rings.send.ring.head, buffHead)
|
|
|
|
return int(packet.size), nil
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2019-02-07 04:08:05 +01:00
|
|
|
|
2019-03-21 21:43:04 +01:00
|
|
|
func (tun *NativeTun) Flush() error {
|
2019-07-11 10:35:47 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-21 21:43:04 +01:00
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
|
|
|
retries := maybeRetry(1000)
|
2019-03-22 12:44:14 +01:00
|
|
|
for {
|
2019-07-11 10:35:47 +02:00
|
|
|
_, err := tun.getTUN()
|
2019-03-22 12:44:14 +01:00
|
|
|
if err != nil {
|
2019-07-11 10:35:47 +02:00
|
|
|
return 0, err
|
2019-03-22 12:44:14 +01:00
|
|
|
}
|
2019-02-20 13:12:08 +01:00
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
packetSize := uint32(len(buff) - offset)
|
|
|
|
alignedPacketSize := packetAlign(uint32(unsafe.Sizeof(packetHeader{})) + packetSize)
|
|
|
|
|
|
|
|
buffHead := atomic.LoadUint32(&tun.rings.receive.ring.head)
|
|
|
|
if buffHead >= packetCapacity {
|
|
|
|
if retries > 0 {
|
|
|
|
tun.closeTUN()
|
|
|
|
time.Sleep(time.Millisecond * 2)
|
|
|
|
retries--
|
|
|
|
continue
|
2019-03-22 12:44:14 +01:00
|
|
|
}
|
2019-07-11 10:35:47 +02:00
|
|
|
return 0, errors.New("receive ring head out of bounds")
|
2019-03-20 21:45:40 +01:00
|
|
|
}
|
2019-07-11 10:35:47 +02:00
|
|
|
retries = maybeRetry(1000)
|
2019-02-04 17:29:52 +01:00
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
buffTail := atomic.LoadUint32(&tun.rings.receive.ring.tail)
|
|
|
|
if buffTail >= packetCapacity {
|
|
|
|
return 0, errors.New("receive ring tail out of bounds")
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
buffSpace := tun.rings.receive.ring.wrap(buffHead - buffTail - packetAlignment)
|
|
|
|
if alignedPacketSize > buffSpace {
|
|
|
|
return 0, errors.New("receive ring full")
|
|
|
|
}
|
2019-02-04 17:29:52 +01:00
|
|
|
|
2019-07-11 10:35:47 +02:00
|
|
|
packet := (*packet)(unsafe.Pointer(&tun.rings.receive.ring.data[buffTail]))
|
|
|
|
packet.size = packetSize
|
|
|
|
copy(packet.data[:packetSize], buff[offset:])
|
|
|
|
atomic.StoreUint32(&tun.rings.receive.ring.tail, tun.rings.receive.ring.wrap(buffTail+alignedPacketSize))
|
|
|
|
if atomic.LoadInt32(&tun.rings.receive.ring.alertable) != 0 {
|
|
|
|
windows.SetEvent(tun.rings.receive.tailMoved)
|
|
|
|
}
|
|
|
|
return int(packetSize), nil
|
2019-02-04 17:29:52 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-01 00:11:12 +01:00
|
|
|
|
2019-05-17 14:26:46 +02:00
|
|
|
// LUID returns Windows adapter instance ID.
|
2019-05-10 21:30:23 +02:00
|
|
|
func (tun *NativeTun) LUID() uint64 {
|
2019-05-17 14:26:46 +02:00
|
|
|
return tun.wt.LUID()
|
2019-05-10 21:30:23 +02:00
|
|
|
}
|
2019-07-11 10:35:47 +02:00
|
|
|
|
|
|
|
// wrap returns value modulo ring capacity
|
|
|
|
func (rb *ring) wrap(value uint32) uint32 {
|
|
|
|
return value & (packetCapacity - 1)
|
|
|
|
}
|