Skip to content

Commit 50da265

Browse files
committed
net/netns: add post-review comments
Follow-up to tailscale#7065 with some comments from Brad's review. Signed-off-by: Andrew Dunham <[email protected]> Change-Id: Ia1219f4fa25479b2dada38ffe421065b408c5954
1 parent 2fc8de4 commit 50da265

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

net/netns/netns_darwin.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ func interfaceIndexFor(addr netip.Addr, canRecurse bool) (int, error) {
115115
}
116116

117117
rm := route.RouteMessage{
118-
Version: unix.RTM_VERSION,
118+
// NOTE: This is unix.RTM_VERSION, but we want to pin this to a
119+
// particular constant so that it doesn't change under us if
120+
// the x/sys/unix package changes down the road. Currently this
121+
// is 0x5 on both Darwin x86 and ARM64.
122+
Version: 0x5,
119123
Type: unix.RTM_GET,
120124
Flags: unix.RTF_UP,
121125
ID: uintptr(os.Getpid()),
@@ -132,6 +136,25 @@ func interfaceIndexFor(addr netip.Addr, canRecurse bool) (int, error) {
132136
if err != nil {
133137
return 0, fmt.Errorf("writing message: %w", err)
134138
}
139+
140+
// On macOS, the RTM_GET call should return exactly one route message.
141+
// Given the following sizes and constants:
142+
// - sizeof(struct rt_msghdr) = 92
143+
// - RTAX_MAX = 8
144+
// - sizeof(struct sockaddr_in6) = 28
145+
// - sizeof(struct sockaddr_in) = 16
146+
// - sizeof(struct sockaddr_dl) = 20
147+
//
148+
// The maximum buffer size should be:
149+
// sizeof(struct rt_msghdr) + RTAX_MAX*sizeof(struct sockaddr_in6)
150+
// = 92 + 8*28
151+
// = 316
152+
//
153+
// During my testing, responses are typically ~120 bytes.
154+
//
155+
// We provide a much larger buffer just in case we're off by a bit, or
156+
// the kernel decides to return more than one message; 2048 bytes
157+
// should be plenty here. This also means we can do a single Read.
135158
var buf [2048]byte
136159
n, err := unix.Read(fd, buf[:])
137160
if err != nil {

0 commit comments

Comments
 (0)