2019-02-04 16:29:52 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018-2019 WireGuard LLC. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package tun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-05-02 22:11:59 +00:00
|
|
|
"fmt"
|
2019-02-04 16:29:52 +00:00
|
|
|
"os"
|
2019-07-11 08:35:47 +00:00
|
|
|
"sync/atomic"
|
2019-03-20 20:45:40 +00:00
|
|
|
"time"
|
2019-02-22 15:16:14 +00:00
|
|
|
"unsafe"
|
2019-02-04 16:29:52 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2019-07-05 05:54:25 +00:00
|
|
|
|
2019-02-19 17:49:24 +00:00
|
|
|
"golang.zx2c4.com/wireguard/tun/wintun"
|
2019-02-04 16:29:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-07-11 08:35:47 +00:00
|
|
|
packetAlignment uint32 = 4 // Number of bytes packets are aligned to in rings
|
2019-07-19 11:51:56 +00:00
|
|
|
packetSizeMax = 0xffff // Maximum packet size
|
|
|
|
packetCapacity = 0x800000 // Ring capacity, 8MiB
|
|
|
|
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)
|
2019-02-04 16:29:52 +00:00
|
|
|
)
|
|
|
|
|
2019-07-11 08:35:47 +00:00
|
|
|
type packetHeader struct {
|
|
|
|
size uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
type packet struct {
|
|
|
|
packetHeader
|
|
|
|
data [packetSizeMax]byte
|
2019-02-20 10:41:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 08:35:47 +00: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 10:41:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 23:05:57 +00:00
|
|
|
type NativeTun struct {
|
2019-07-11 08:35:47 +00:00
|
|
|
wt *wintun.Wintun
|
2019-07-19 11:51:56 +00:00
|
|
|
handle windows.Handle
|
2019-07-11 08:35:47 +00:00
|
|
|
close bool
|
|
|
|
rings ringDescriptor
|
|
|
|
events chan Event
|
|
|
|
errors chan error
|
|
|
|
forcedMTU int
|
2019-02-04 16:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 17:49:24 +00:00
|
|
|
func packetAlign(size uint32) uint32 {
|
2019-07-11 08:35:47 +00:00
|
|
|
return (size + (packetAlignment - 1)) &^ (packetAlignment - 1)
|
2019-02-04 16:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 13:27:16 +00: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 21:33:40 +00:00
|
|
|
func CreateTUN(ifname string) (Device, error) {
|
2019-06-09 17:20:17 +00: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 21:33:40 +00:00
|
|
|
func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID) (Device, error) {
|
2019-05-02 22:11:59 +00:00
|
|
|
var err error
|
|
|
|
var wt *wintun.Wintun
|
|
|
|
|
2019-05-09 08:11:15 +00:00
|
|
|
// Does an interface with this name already exist?
|
2019-06-10 09:02:18 +00:00
|
|
|
wt, err = wintun.GetInterface(ifname)
|
2019-05-17 14:06:02 +00:00
|
|
|
if err == nil {
|
|
|
|
// If so, we delete it, in case it has weird residual configuration.
|
2019-06-10 09:02:18 +00:00
|
|
|
_, err = wt.DeleteInterface()
|
2019-02-06 21:30:14 +00:00
|
|
|
if err != nil {
|
2019-05-17 14:06:02 +00:00
|
|
|
return nil, fmt.Errorf("Unable to delete already existing Wintun interface: %v", err)
|
2019-02-06 21:30:14 +00:00
|
|
|
}
|
2019-05-24 07:28:50 +00:00
|
|
|
} else if err == windows.ERROR_ALREADY_EXISTS {
|
|
|
|
return nil, fmt.Errorf("Foreign network interface with the same name exists")
|
2019-02-07 21:02:51 +00:00
|
|
|
}
|
2019-06-10 09:02:18 +00:00
|
|
|
wt, _, err = wintun.CreateInterface("WireGuard Tunnel Adapter", requestedGUID)
|
2019-03-31 08:17:11 +00:00
|
|
|
if err != nil {
|
2019-05-17 14:06:02 +00:00
|
|
|
return nil, fmt.Errorf("Unable to create Wintun interface: %v", err)
|
2019-03-31 08:17:11 +00:00
|
|
|
}
|
2019-02-07 21:02:51 +00:00
|
|
|
|
2019-05-17 14:06:02 +00:00
|
|
|
err = wt.SetInterfaceName(ifname)
|
2019-02-07 17:24:28 +00:00
|
|
|
if err != nil {
|
2019-06-10 09:02:18 +00:00
|
|
|
wt.DeleteInterface()
|
2019-05-17 14:06:02 +00:00
|
|
|
return nil, fmt.Errorf("Unable to set name of Wintun interface: %v", err)
|
2019-02-07 17:24:28 +00:00
|
|
|
}
|
2019-02-06 21:30:14 +00:00
|
|
|
|
2019-07-11 08:35:47 +00:00
|
|
|
tun := &NativeTun{
|
2019-03-18 08:42:00 +00:00
|
|
|
wt: wt,
|
2019-07-23 09:45:48 +00:00
|
|
|
handle: windows.InvalidHandle,
|
2019-06-10 21:33:40 +00:00
|
|
|
events: make(chan Event, 10),
|
2019-03-18 08:42:00 +00:00
|
|
|
errors: make(chan error, 1),
|
2019-05-16 08:33:47 +00:00
|
|
|
forcedMTU: 1500,
|
2019-07-11 08:35:47 +00: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 {
|
2019-07-19 11:51:56 +00:00
|
|
|
tun.Close()
|
2019-07-11 08:35:47 +00:00
|
|
|
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 {
|
2019-07-19 11:51:56 +00:00
|
|
|
tun.Close()
|
2019-07-11 08:35:47 +00:00
|
|
|
return nil, fmt.Errorf("Error creating event: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
tun.handle, err = tun.wt.AdapterHandle()
|
2019-07-18 17:26:25 +00:00
|
|
|
if err != nil {
|
2019-07-19 11:51:56 +00:00
|
|
|
tun.Close()
|
2019-07-18 17:26:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
var bytesReturned uint32
|
|
|
|
err = windows.DeviceIoControl(tun.handle, ioctlRegisterRings, (*byte)(unsafe.Pointer(&tun.rings)), uint32(unsafe.Sizeof(tun.rings)), nil, 0, &bytesReturned, nil)
|
2019-07-11 08:35:47 +00:00
|
|
|
if err != nil {
|
2019-07-19 11:51:56 +00:00
|
|
|
tun.Close()
|
|
|
|
return nil, fmt.Errorf("Error registering rings: %v", err)
|
2019-07-11 08:35:47 +00:00
|
|
|
}
|
2019-07-19 11:51:56 +00:00
|
|
|
return tun, nil
|
2019-02-20 12:12:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 23:05:57 +00:00
|
|
|
func (tun *NativeTun) Name() (string, error) {
|
2019-05-22 17:31:52 +00:00
|
|
|
return tun.wt.InterfaceName()
|
2019-02-04 16:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 23:05:57 +00:00
|
|
|
func (tun *NativeTun) File() *os.File {
|
2019-02-04 16:29:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-10 21:33:40 +00:00
|
|
|
func (tun *NativeTun) Events() chan Event {
|
2019-02-04 16:29:52 +00:00
|
|
|
return tun.events
|
|
|
|
}
|
|
|
|
|
2019-02-28 23:05:57 +00:00
|
|
|
func (tun *NativeTun) Close() error {
|
2019-03-20 20:45:40 +00:00
|
|
|
tun.close = true
|
2019-07-19 11:51:56 +00:00
|
|
|
if tun.rings.send.tailMoved != 0 {
|
|
|
|
windows.SetEvent(tun.rings.send.tailMoved) // wake the reader if it's sleeping
|
2019-02-04 16:29:52 +00:00
|
|
|
}
|
2019-07-19 11:51:56 +00:00
|
|
|
if tun.handle != windows.InvalidHandle {
|
|
|
|
windows.CloseHandle(tun.handle)
|
2019-07-11 08:35:47 +00:00
|
|
|
}
|
2019-07-19 11:51:56 +00:00
|
|
|
if tun.rings.send.tailMoved != 0 {
|
|
|
|
windows.CloseHandle(tun.rings.send.tailMoved)
|
2019-07-11 08:35:47 +00:00
|
|
|
}
|
2019-07-19 11:51:56 +00:00
|
|
|
if tun.rings.send.tailMoved != 0 {
|
|
|
|
windows.CloseHandle(tun.rings.receive.tailMoved)
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if tun.wt != nil {
|
|
|
|
_, err = tun.wt.DeleteInterface()
|
2019-03-26 14:57:53 +00:00
|
|
|
}
|
2019-07-22 07:37:20 +00:00
|
|
|
close(tun.events)
|
2019-07-11 08:35:47 +00:00
|
|
|
return err
|
2019-02-04 16:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 23:05:57 +00:00
|
|
|
func (tun *NativeTun) MTU() (int, error) {
|
2019-05-16 08:33:47 +00:00
|
|
|
return tun.forcedMTU, nil
|
2019-03-13 08:52:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 21:00:15 +00: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 08:33:47 +00:00
|
|
|
func (tun *NativeTun) ForceMTU(mtu int) {
|
|
|
|
tun.forcedMTU = mtu
|
2019-02-04 16:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 07:06:34 +00:00
|
|
|
//go:linkname procyield runtime.procyield
|
|
|
|
func procyield(cycles uint32)
|
|
|
|
|
2019-07-11 08:35:47 +00:00
|
|
|
// Note: Read() and Write() assume the caller comes only from a single thread; there's no locking.
|
|
|
|
|
2019-02-28 23:05:57 +00:00
|
|
|
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
2019-07-19 11:51:56 +00:00
|
|
|
retry:
|
2019-02-04 16:29:52 +00:00
|
|
|
select {
|
|
|
|
case err := <-tun.errors:
|
|
|
|
return 0, err
|
|
|
|
default:
|
2019-02-08 13:31:05 +00:00
|
|
|
}
|
2019-07-19 11:51:56 +00:00
|
|
|
if tun.close {
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2019-02-04 16:29:52 +00:00
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
buffHead := atomic.LoadUint32(&tun.rings.send.ring.head)
|
|
|
|
if buffHead >= packetCapacity {
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2019-07-11 08:35:47 +00:00
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
start := time.Now()
|
|
|
|
var buffTail uint32
|
|
|
|
for {
|
|
|
|
buffTail = atomic.LoadUint32(&tun.rings.send.ring.tail)
|
|
|
|
if buffHead != buffTail {
|
|
|
|
break
|
2019-07-11 08:35:47 +00:00
|
|
|
}
|
2019-07-19 11:51:56 +00:00
|
|
|
if tun.close {
|
|
|
|
return 0, os.ErrClosed
|
2019-02-08 13:31:05 +00:00
|
|
|
}
|
2019-08-03 17:05:53 +00:00
|
|
|
if time.Since(start) >= time.Millisecond/80 /* ~1gbit/s */ {
|
2019-07-19 11:51:56 +00:00
|
|
|
windows.WaitForSingleObject(tun.rings.send.tailMoved, windows.INFINITE)
|
|
|
|
goto retry
|
2019-05-31 13:40:08 +00:00
|
|
|
}
|
2019-07-19 11:51:56 +00:00
|
|
|
procyield(1)
|
|
|
|
}
|
|
|
|
if buffTail >= packetCapacity {
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2019-07-11 08:35:47 +00:00
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
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-07-11 08:35:47 +00:00
|
|
|
|
2019-07-19 11:51:56 +00: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")
|
|
|
|
}
|
2019-07-11 08:35:47 +00:00
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
alignedPacketSize := packetAlign(uint32(unsafe.Sizeof(packetHeader{})) + packet.size)
|
|
|
|
if alignedPacketSize > buffContent {
|
|
|
|
return 0, errors.New("incomplete packet in send ring")
|
2019-02-04 16:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
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-07-11 08:35:47 +00:00
|
|
|
}
|
2019-02-07 03:08:05 +00:00
|
|
|
|
2019-03-21 20:43:04 +00:00
|
|
|
func (tun *NativeTun) Flush() error {
|
2019-07-11 08:35:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-21 20:43:04 +00:00
|
|
|
|
2019-07-11 08:35:47 +00:00
|
|
|
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
2019-07-19 11:51:56 +00:00
|
|
|
if tun.close {
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2019-02-20 12:12:08 +00:00
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
packetSize := uint32(len(buff) - offset)
|
|
|
|
alignedPacketSize := packetAlign(uint32(unsafe.Sizeof(packetHeader{})) + packetSize)
|
2019-02-04 16:29:52 +00:00
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
buffHead := atomic.LoadUint32(&tun.rings.receive.ring.head)
|
|
|
|
if buffHead >= packetCapacity {
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2019-02-04 16:29:52 +00:00
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
buffTail := atomic.LoadUint32(&tun.rings.receive.ring.tail)
|
|
|
|
if buffTail >= packetCapacity {
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2019-02-04 16:29:52 +00:00
|
|
|
|
2019-07-19 11:51:56 +00:00
|
|
|
buffSpace := tun.rings.receive.ring.wrap(buffHead - buffTail - packetAlignment)
|
|
|
|
if alignedPacketSize > buffSpace {
|
|
|
|
return 0, nil // Dropping when ring is full.
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-02-04 16:29:52 +00:00
|
|
|
}
|
2019-07-19 11:51:56 +00:00
|
|
|
return int(packetSize), nil
|
2019-02-04 16:29:52 +00:00
|
|
|
}
|
2019-02-28 23:11:12 +00:00
|
|
|
|
2019-05-17 12:26:46 +00:00
|
|
|
// LUID returns Windows adapter instance ID.
|
2019-05-10 19:30:23 +00:00
|
|
|
func (tun *NativeTun) LUID() uint64 {
|
2019-05-17 12:26:46 +00:00
|
|
|
return tun.wt.LUID()
|
2019-05-10 19:30:23 +00:00
|
|
|
}
|
2019-07-11 08:35:47 +00:00
|
|
|
|
|
|
|
// wrap returns value modulo ring capacity
|
|
|
|
func (rb *ring) wrap(value uint32) uint32 {
|
|
|
|
return value & (packetCapacity - 1)
|
|
|
|
}
|