Skip to content

Commit 1bab46f

Browse files
committed
feat: set path MTU discover on Linux
1 parent bede4fb commit 1bab46f

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
package magicsock
22

3+
import (
4+
"net"
5+
6+
"golang.org/x/sys/unix"
7+
"tailscale.com/types/logger"
8+
"tailscale.com/types/nettype"
9+
)
10+
311
func trySetPathMTUDiscover(pconn nettype.PacketConn, logf logger.Logf, network string) {
4-
// TODO: implement
5-
logf("magicsock: failed to set Path MTU Discover: not implemented")
12+
if c, ok := pconn.(*net.UDPConn); ok {
13+
s, err := c.SyscallConn()
14+
if err != nil {
15+
logf("magicsock: failed to set Path MTU Discover: get syscall conn: %v", err)
16+
}
17+
level := unix.IPPROTO_IP
18+
option := unix.IP_MTU_DISCOVER
19+
if network == "udp6" {
20+
level = unix.IPPROTO_IPV6
21+
option = unix.IPV6_MTU_DISCOVER
22+
}
23+
err = s.Control(func(fd uintptr) {
24+
err := unix.SetsockoptInt(int(fd), level, option, unix.IP_PMTUDISC_DO)
25+
if err != nil {
26+
logf("magicsock: failed to set Path MTU Discover: SetsockoptInt failed: %v", err)
27+
}
28+
})
29+
if err != nil {
30+
logf("magicsock: failed to set Path MTU Discover: control connection: %v", err)
31+
}
32+
logf("magicsock: successfully set Path MTU Discover on %s", pconn.LocalAddr().String())
33+
return
34+
}
35+
logf("magicsock: failed to set Path MTU Discover: not a UDPConn")
636
}

wgengine/magicsock/magicsock_linux.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,28 @@ func init() {
406406
}
407407

408408
func trySetPathMTUDiscover(pconn nettype.PacketConn, logf logger.Logf, network string) {
409-
// TODO: implement
410-
logf("magicsock: failed to set Path MTU Discover: not implemented")
409+
if c, ok := pconn.(*net.UDPConn); ok {
410+
s, err := c.SyscallConn()
411+
if err != nil {
412+
logf("magicsock: failed to set Path MTU Discover: get syscall conn: %v", err)
413+
}
414+
level := unix.IPPROTO_IP
415+
option := unix.IP_MTU_DISCOVER
416+
if network == "udp6" {
417+
level = unix.IPPROTO_IPV6
418+
option = unix.IPV6_MTU_DISCOVER
419+
}
420+
err = s.Control(func(fd uintptr) {
421+
err := unix.SetsockoptInt(int(fd), level, option, unix.IP_PMTUDISC_DO)
422+
if err != nil {
423+
logf("magicsock: failed to set Path MTU Discover: SetsockoptInt failed: %v", err)
424+
}
425+
})
426+
if err != nil {
427+
logf("magicsock: failed to set Path MTU Discover: control connection: %v", err)
428+
}
429+
logf("magicsock: successfully set Path MTU Discover on %s", pconn.LocalAddr().String())
430+
return
431+
}
432+
logf("magicsock: failed to set Path MTU Discover: not a UDPConn")
411433
}

wgengine/magicsock/magicsock_windows.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const (
2222
)
2323

2424
func trySetPathMTUDiscover(pconn nettype.PacketConn, logf logger.Logf, network string) {
25-
logf("setting Path MTU Discover on %s", pconn.LocalAddr().String())
2625
if c, ok := pconn.(*net.UDPConn); ok {
2726
s, err := c.SyscallConn()
2827
if err != nil {
@@ -41,7 +40,7 @@ func trySetPathMTUDiscover(pconn nettype.PacketConn, logf logger.Logf, network s
4140
if err != nil {
4241
logf("magicsock: failed to set Path MTU Discover: control connection: %v", err)
4342
}
44-
logf("sucessfully set Path MTU Discover on %s", pconn.LocalAddr().String())
43+
logf("magicsock: successfully set Path MTU Discover on %s", pconn.LocalAddr().String())
4544
return
4645
}
4746
logf("magicsock: failed to set Path MTU Discover: not a UDPConn")

0 commit comments

Comments
 (0)