Skip to content

Commit 9c0565a

Browse files
neildgopherbot
authored andcommitted
ipv4: fix TTL control message parsing on big-endian Linux and Solaris
On Linux, IP_TTL ancillary data is a 4-byte native-endian int rather than a 1-byte value; reading only the first byte misparsed the TTL as 0 on big-endian architectures. On Solaris, IP_RECVTTL ancillary data is a 1-byte uint8_t, not 4 bytes. Change-Id: I3ccebc693bc3aa377890bc4ebc8e7cc66a6a6964 Reviewed-on: https://go-review.googlesource.com/c/net/+/852827 Auto-Submit: Damien Neil <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 9425eb3 commit 9c0565a

4 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎ipv4/control_unix.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
package ipv4
88

99
import (
10-
"unsafe"
10+
"encoding/binary"
11+
"runtime"
1112

1213
"golang.org/x/net/internal/iana"
1314
"golang.org/x/net/internal/socket"
@@ -66,10 +67,18 @@ func setControlMessage(c *socket.Conn, opt *rawOpt, cf ControlFlags, on bool) er
6667

6768
func marshalTTL(b []byte, cm *ControlMessage) []byte {
6869
m := socket.ControlMessage(b)
70+
if runtime.GOOS == "linux" {
71+
m.MarshalHeader(iana.ProtocolIP, unix.IP_TTL, 4)
72+
return m.Next(4)
73+
}
6974
m.MarshalHeader(iana.ProtocolIP, unix.IP_RECVTTL, 1)
7075
return m.Next(1)
7176
}
7277

7378
func parseTTL(cm *ControlMessage, b []byte) {
74-
cm.TTL = int(*(*byte)(unsafe.Pointer(&b[:1][0])))
79+
if runtime.GOOS == "linux" {
80+
cm.TTL = int(binary.NativeEndian.Uint32(b[:4]))
81+
return
82+
}
83+
cm.TTL = int(b[0])
7584
}

‎ipv4/sys_linux.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
var (
1919
ctlOpts = [ctlMax]ctlOpt{
20-
ctlTTL: {unix.IP_TTL, 1, marshalTTL, parseTTL},
20+
ctlTTL: {unix.IP_TTL, 4, marshalTTL, parseTTL},
2121
ctlPacketInfo: {unix.IP_PKTINFO, sizeofInetPktinfo, marshalPacketInfo, parsePacketInfo},
2222
}
2323

‎ipv4/sys_solaris.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const sockoptReceiveInterface = unix.IP_RECVIF
1919

2020
var (
2121
ctlOpts = [ctlMax]ctlOpt{
22-
ctlTTL: {unix.IP_RECVTTL, 4, marshalTTL, parseTTL},
22+
ctlTTL: {unix.IP_RECVTTL, 1, marshalTTL, parseTTL},
2323
ctlPacketInfo: {unix.IP_PKTINFO, sizeofInetPktinfo, marshalPacketInfo, parsePacketInfo},
2424
}
2525

‎ipv4/unicast_test.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
4949
}
5050
t.Fatal(err)
5151
}
52-
p.SetTTL(i + 1)
52+
ttl := i + 1
53+
p.SetTTL(ttl)
5354

5455
backoff := time.Millisecond
5556
for {
@@ -69,10 +70,12 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
6970
}
7071

7172
rb := make([]byte, 128)
72-
if n, _, _, err := p.ReadFrom(rb); err != nil {
73+
if n, cm, _, err := p.ReadFrom(rb); err != nil {
7374
t.Fatal(err)
7475
} else if !bytes.Equal(rb[:n], wb) {
7576
t.Fatalf("got %v; want %v", rb[:n], wb)
77+
} else if toggle && runtime.GOOS != "zos" && (cm == nil || cm.TTL != ttl) {
78+
t.Fatalf("got %v; want ttl=%d", cm, ttl)
7679
}
7780
}
7881
}

0 commit comments

Comments
 (0)