From e34f0a83e4489ba269741463385336c6ef7d0800 Mon Sep 17 00:00:00 2001 From: Jordan Whited Date: Wed, 14 Dec 2022 17:02:04 -0800 Subject: [PATCH] tun: replace use of binary package with copy() in virtioNetHdr encoding This is a cherry-pick of 798bed0b5d9283a45d9f3e78da7f5acd7477b7a6. Signed-off-by: Jordan Whited --- endian/big.go | 8 -------- endian/little.go | 8 -------- tun/tcp_offload_linux.go | 15 ++------------- 3 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 endian/big.go delete mode 100644 endian/little.go diff --git a/endian/big.go b/endian/big.go deleted file mode 100644 index b53035d6e..000000000 --- a/endian/big.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build mips || mips64 || ppc64 || s390x - -package endian - -import "encoding/binary" - -// Native is the platform's native byte order. -var Native = binary.LittleEndian diff --git a/endian/little.go b/endian/little.go deleted file mode 100644 index e1090071b..000000000 --- a/endian/little.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build 386 || amd64 || arm || arm64 || mips64le || mipsle || ppc64le || riscv64 || wasm || loong64 - -package endian - -import "encoding/binary" - -// Native is the platform's native byte order. -var Native = binary.LittleEndian diff --git a/tun/tcp_offload_linux.go b/tun/tcp_offload_linux.go index 9f54e996e..757f6730f 100644 --- a/tun/tcp_offload_linux.go +++ b/tun/tcp_offload_linux.go @@ -7,7 +7,6 @@ import ( "unsafe" "github.com/tailscale/wireguard-go/conn" - "github.com/tailscale/wireguard-go/endian" "golang.org/x/sys/unix" ) @@ -50,12 +49,7 @@ func (v *virtioNetHdr) decode(b []byte) error { if len(b) < virtioNetHdrLen { return errors.New("too short") } - v.flags = b[0] - v.gsoType = b[1] - v.hdrLen = endian.Native.Uint16(b[2:]) - v.gsoSize = endian.Native.Uint16(b[4:]) - v.csumStart = endian.Native.Uint16(b[6:]) - v.csumOffset = endian.Native.Uint16(b[8:]) + copy(unsafe.Slice((*byte)(unsafe.Pointer(v)), virtioNetHdrLen), b[:virtioNetHdrLen]) return nil } @@ -63,12 +57,7 @@ func (v *virtioNetHdr) encode(b []byte) error { if len(b) < virtioNetHdrLen { return errors.New("too short") } - b[0] = v.flags - b[1] = v.gsoType - endian.Native.PutUint16(b[2:], v.hdrLen) - endian.Native.PutUint16(b[4:], v.gsoSize) - endian.Native.PutUint16(b[6:], v.csumStart) - endian.Native.PutUint16(b[8:], v.csumOffset) + copy(b[:virtioNetHdrLen], unsafe.Slice((*byte)(unsafe.Pointer(v)), virtioNetHdrLen)) return nil }