]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
tun: windows: serialize write calls
authorJason A. Donenfeld <Jason@zx2c4.com>
Tue, 7 Jan 2020 16:40:45 +0000 (11:40 -0500)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 7 Jan 2020 16:40:45 +0000 (11:40 -0500)
tun/tun_windows.go

index 8fc51742c74b53be317bd39eef9f58ad3bcc03fb..b16dbb748f87d78a10afe9b64cede158b739e4d5 100644 (file)
@@ -9,6 +9,7 @@ import (
        "errors"
        "fmt"
        "os"
+       "sync"
        "sync/atomic"
        "time"
        "unsafe"
@@ -40,6 +41,7 @@ type NativeTun struct {
        forcedMTU int
        rate      rateJuggler
        rings     *wintun.RingDescriptor
+       writeLock sync.Mutex
 }
 
 const WintunPool = wintun.Pool("WireGuard")
@@ -219,6 +221,9 @@ func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
        tun.rate.update(uint64(packetSize))
        alignedPacketSize := wintun.PacketAlign(uint32(unsafe.Sizeof(wintun.PacketHeader{})) + packetSize)
 
+       tun.writeLock.Lock()
+       defer tun.writeLock.Unlock()
+
        buffHead := atomic.LoadUint32(&tun.rings.Receive.Ring.Head)
        if buffHead >= wintun.PacketCapacity {
                return 0, os.ErrClosed