diff --git a/internal/pmtud/ip/ipv6_darwin.go b/internal/pmtud/ip/ipv6_darwin.go deleted file mode 100644 index 40d8b03d..00000000 --- a/internal/pmtud/ip/ipv6_darwin.go +++ /dev/null @@ -1,5 +0,0 @@ -package ip - -func SetIPv6HeaderIncluded(fd int) error { - panic("darwin does not allow an application to build IPv6 headers") -} diff --git a/internal/pmtud/ip/ipv6_linux.go b/internal/pmtud/ip/ipv6_linux.go deleted file mode 100644 index d0e151b4..00000000 --- a/internal/pmtud/ip/ipv6_linux.go +++ /dev/null @@ -1,7 +0,0 @@ -package ip - -import "golang.org/x/sys/unix" - -func SetIPv6HeaderIncluded(fd int) error { - return unix.SetsockoptInt(fd, unix.IPPROTO_IPV6, unix.IPV6_HDRINCL, 1) -} diff --git a/internal/pmtud/ip/ipv6_unspecified.go b/internal/pmtud/ip/ipv6_unspecified.go deleted file mode 100644 index b4ea2b2a..00000000 --- a/internal/pmtud/ip/ipv6_unspecified.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build !linux && !windows && !darwin - -package ip - -func SetIPv6HeaderIncluded(fd int) error { - panic("not implemented") -} diff --git a/internal/pmtud/ip/ipv6_windows.go b/internal/pmtud/ip/ipv6_windows.go deleted file mode 100644 index 8f4b757e..00000000 --- a/internal/pmtud/ip/ipv6_windows.go +++ /dev/null @@ -1,7 +0,0 @@ -package ip - -import "golang.org/x/sys/windows" - -func SetIPv6HeaderIncluded(fd windows.Handle) error { - panic("windows does not allow an application to build IPv6 headers") -} diff --git a/internal/pmtud/ip/source_unix.go b/internal/pmtud/ip/source_unix.go index 8207fa92..5a9b0b38 100644 --- a/internal/pmtud/ip/source_unix.go +++ b/internal/pmtud/ip/source_unix.go @@ -29,7 +29,7 @@ func makeSockAddr(ip netip.Addr, port uint16) unix.Sockaddr { } } return &unix.SockaddrInet6{ - Port: int(port), + Port: 0, Addr: ip.As16(), } } diff --git a/internal/pmtud/tcp/packet.go b/internal/pmtud/tcp/packet.go index e3135bfe..03018f9d 100644 --- a/internal/pmtud/tcp/packet.go +++ b/internal/pmtud/tcp/packet.go @@ -65,13 +65,15 @@ func createPacket(src, dst netip.AddrPort, if dst.Addr().Is4() { ipHeader = ip.HeaderV4(src.Addr(), dst.Addr(), payloadLength) } else { + // Pseudo-header, this is actually not part of the packet since + // the kernel will calculate and add it itself to the packet; + // it is only used for calculating the TCP checksum. ipHeader = ip.HeaderV6(src.Addr(), dst.Addr(), uint16(payloadLength), byte(constants.IPPROTO_TCP)) //nolint:gosec } tcpHeader := makeTCPHeader(src.Port(), dst.Port(), seq, ack, flags) - // data is just zeroes dataLength := int(payloadLength - constants.BaseTCPHeaderLength) var data []byte if dataLength > 0 { @@ -81,10 +83,18 @@ func createPacket(src, dst netip.AddrPort, tcpHeader[16] = byte(checksum >> 8) //nolint:mnd tcpHeader[17] = byte(checksum & 0xff) //nolint:mnd - packet := make([]byte, len(ipHeader)+int(constants.BaseTCPHeaderLength)+dataLength) - copy(packet, ipHeader) - copy(packet[len(ipHeader):], tcpHeader) - copy(packet[len(ipHeader)+int(constants.BaseTCPHeaderLength):], data) + var packet []byte + i := 0 + if dst.Addr().Is4() { + packet = make([]byte, len(ipHeader)+int(constants.BaseTCPHeaderLength)+dataLength) + copy(packet, ipHeader) + i += len(ipHeader) + } else { + packet = make([]byte, int(constants.BaseTCPHeaderLength)+dataLength) + } + copy(packet[i:], tcpHeader) + i += int(constants.BaseTCPHeaderLength) + copy(packet[i:], data) return packet } diff --git a/internal/pmtud/tcp/tcp.go b/internal/pmtud/tcp/tcp.go index 03931f98..d5b585a2 100644 --- a/internal/pmtud/tcp/tcp.go +++ b/internal/pmtud/tcp/tcp.go @@ -47,12 +47,10 @@ func startRawSocket(family, excludeMark int) (fd fileDescriptor, stop func(), er if family == constants.AF_INET { err = ip.SetIPv4HeaderIncluded(fdPlatform) - } else { - err = ip.SetIPv6HeaderIncluded(fdPlatform) - } - if err != nil { - _ = closeSocket(fdPlatform) - return 0, nil, fmt.Errorf("setting header option on raw socket: %w", err) + if err != nil { + _ = closeSocket(fdPlatform) + return 0, nil, fmt.Errorf("setting header option on raw socket: %w", err) + } } // Allow sending packets larger than cached PMTU (for PMTUD probing) diff --git a/internal/pmtud/tcp/tcp_unix.go b/internal/pmtud/tcp/tcp_unix.go index 61c468fd..a0fb4a2d 100644 --- a/internal/pmtud/tcp/tcp_unix.go +++ b/internal/pmtud/tcp/tcp_unix.go @@ -45,7 +45,7 @@ func makeSockAddr(addr netip.AddrPort) unix.Sockaddr { } } return &unix.SockaddrInet6{ - Port: int(addr.Port()), + Port: 0, Addr: addr.Addr().As16(), } }