wintun: Adopt new packet data alignment

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2019-02-20 19:56:10 +01:00
parent 040da43889
commit 8091c6474a

View file

@ -16,9 +16,9 @@ import (
) )
const ( const (
packetSizeMax uint32 = 0xeffc // Maximum packet size: 4 + packetSizeMax == 0xf000
packetExchangeMax uint32 = 256 // Number of packets that may be written at a time packetExchangeMax uint32 = 256 // Number of packets that may be written at a time
packetExchangeAlignment uint32 = 16 // Number of bytes packets are aligned to in exchange buffers packetExchangeAlignment uint32 = 16 // Number of bytes packets are aligned to in exchange buffers
packetSizeMax uint32 = 0xf000 - packetExchangeAlignment // Maximum packet size
packetExchangeSizeRead uint32 = 0x100000 // Read exchange buffer size (defaults to 1MiB) packetExchangeSizeRead uint32 = 0x100000 // Read exchange buffer size (defaults to 1MiB)
packetExchangeSizeWrite uint32 = 0x10000 // Write exchange buffer size (defaults to 64kiB) packetExchangeSizeWrite uint32 = 0x10000 // Write exchange buffer size (defaults to 64kiB)
) )
@ -228,10 +228,10 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
} }
for { for {
if tun.rdBuff.offset+4 <= tun.rdBuff.avail { if tun.rdBuff.offset+packetExchangeAlignment <= tun.rdBuff.avail {
// Get packet from the exchange buffer. // Get packet from the exchange buffer.
size := *(*uint32)(unsafe.Pointer(&tun.rdBuff.data[tun.rdBuff.offset])) size := *(*uint32)(unsafe.Pointer(&tun.rdBuff.data[tun.rdBuff.offset]))
pSize := packetAlign(4 + size) pSize := packetAlign(packetExchangeAlignment + size)
if packetSizeMax < size || tun.rdBuff.avail < tun.rdBuff.offset+pSize { if packetSizeMax < size || tun.rdBuff.avail < tun.rdBuff.offset+pSize {
// Invalid packet size. // Invalid packet size.
tun.rdBuff.avail = 0 tun.rdBuff.avail = 0
@ -239,7 +239,7 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
} }
// Copy data. // Copy data.
copy(buff[offset:], tun.rdBuff.data[tun.rdBuff.offset+4:][:size]) copy(buff[offset:], tun.rdBuff.data[tun.rdBuff.offset+packetExchangeAlignment:][:size])
tun.rdBuff.offset += pSize tun.rdBuff.offset += pSize
return int(size), nil return int(size), nil
} }
@ -319,7 +319,7 @@ func (tun *nativeTun) putTunPacket(buff []byte) error {
if size > packetSizeMax { if size > packetSizeMax {
return errors.New("Packet too big") return errors.New("Packet too big")
} }
pSize := packetAlign(4 + size) pSize := packetAlign(packetExchangeAlignment + size)
if tun.wrBuff.packetNum >= packetExchangeMax || tun.wrBuff.offset+pSize >= packetExchangeSizeWrite { if tun.wrBuff.packetNum >= packetExchangeMax || tun.wrBuff.offset+pSize >= packetExchangeSizeWrite {
// Exchange buffer is full -> flush first. // Exchange buffer is full -> flush first.
@ -331,7 +331,7 @@ func (tun *nativeTun) putTunPacket(buff []byte) error {
// Write packet to the exchange buffer. // Write packet to the exchange buffer.
*(*uint32)(unsafe.Pointer(&tun.wrBuff.data[tun.wrBuff.offset])) = size *(*uint32)(unsafe.Pointer(&tun.wrBuff.data[tun.wrBuff.offset])) = size
copy(tun.wrBuff.data[tun.wrBuff.offset+4:][:size], buff) copy(tun.wrBuff.data[tun.wrBuff.offset+packetExchangeAlignment:][:size], buff)
tun.wrBuff.packetNum++ tun.wrBuff.packetNum++
tun.wrBuff.offset += pSize tun.wrBuff.offset += pSize