From e4bb2dbd5557c9417b30cf9fcd9032fd6fea5366 Mon Sep 17 00:00:00 2001 From: Zxilly <31370133+Zxilly@users.noreply.github.com> Date: Wed, 30 Apr 2025 10:30:35 +0800 Subject: [PATCH] fix parsing of interface index in packet info cmsg on big endian architectures (#5094) --- sys_conn_helper_darwin.go | 2 +- sys_conn_helper_linux.go | 2 +- sys_conn_oob.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sys_conn_helper_darwin.go b/sys_conn_helper_darwin.go index 545502dda..a04bfb3ce 100644 --- a/sys_conn_helper_darwin.go +++ b/sys_conn_helper_darwin.go @@ -30,7 +30,7 @@ func parseIPv4PktInfo(body []byte) (ip netip.Addr, ifIndex uint32, ok bool) { if len(body) != 12 { return netip.Addr{}, 0, false } - return netip.AddrFrom4(*(*[4]byte)(body[8:12])), binary.LittleEndian.Uint32(body), true + return netip.AddrFrom4(*(*[4]byte)(body[8:12])), binary.NativeEndian.Uint32(body), true } func isGSOEnabled(syscall.RawConn) bool { return false } diff --git a/sys_conn_helper_linux.go b/sys_conn_helper_linux.go index eec127197..9a890cbe1 100644 --- a/sys_conn_helper_linux.go +++ b/sys_conn_helper_linux.go @@ -58,7 +58,7 @@ func parseIPv4PktInfo(body []byte) (ip netip.Addr, ifIndex uint32, ok bool) { if len(body) != 12 { return netip.Addr{}, 0, false } - return netip.AddrFrom4(*(*[4]byte)(body[8:12])), binary.LittleEndian.Uint32(body), true + return netip.AddrFrom4(*(*[4]byte)(body[8:12])), binary.NativeEndian.Uint32(body), true } // isGSOEnabled tests if the kernel supports GSO. diff --git a/sys_conn_oob.go b/sys_conn_oob.go index 5ed1b656e..974ff0499 100644 --- a/sys_conn_oob.go +++ b/sys_conn_oob.go @@ -222,7 +222,7 @@ func (c *oobConn) ReadPacket() (receivedPacket, error) { // }; if len(body) == 20 { p.info.addr = netip.AddrFrom16(*(*[16]byte)(body[:16])).Unmap() - p.info.ifIndex = binary.LittleEndian.Uint32(body[16:]) + p.info.ifIndex = binary.NativeEndian.Uint32(body[16:]) } else { invalidCmsgOnceV6.Do(func() { log.Printf("Received invalid IPv6 packet info control message: %+x. "+