mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
If people ran derper on a multi-NIC or multi-address host, STUN replies could go out from the wrong address. The wildcard UDP socket let the kernel pick the reply's source address by routing to the client, which means the default route's address rather than the one the request came in on. With connmark-based policy routing (e.g. DNAT through a tunnel), the reply then doesn't match the inbound conntrack entry, goes out the wrong interface, and the client never sees it. DERP over TCP was fine, since accepted sockets are pinned to the local address. Add net/pktinfo, a small Linux-only package that uses IP_PKTINFO and IPV6_RECVPKTINFO to learn each datagram's destination address and to reply from it, and use it in the STUN server. Only the source address is pinned; routing still picks the interface. Other platforms are unchanged. Fixes #21404 Signed-off-by: Brad Fitzpatrick <[email protected]> Change-Id: I7b3e9c2d41a8f60e5d9c1b2a3f4e5d6c7b8a9f01
55 lines
2.0 KiB
Go
55 lines
2.0 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package pktinfo lets a UDP server learn the local address each datagram
|
|
// was sent to and reply from that same address.
|
|
//
|
|
// A server on a wildcard socket otherwise replies from whatever source
|
|
// address the kernel picks by routing to the client. On a multi-homed host
|
|
// that is the default route's address, not necessarily the one the request
|
|
// arrived on, which breaks clients and middleboxes (conntrack, policy
|
|
// routing) that expect the reply to come from the address they sent to.
|
|
//
|
|
// The functions operate on raw socket control message bytes, as used by
|
|
// [net.UDPConn.ReadMsgUDPAddrPort], [net.UDPConn.WriteMsgUDPAddrPort], and
|
|
// the OOB fields of golang.org/x/net/ipv4 and ipv6 batch messages.
|
|
//
|
|
// It is only implemented on Linux. Elsewhere, [Enable] returns
|
|
// [errors.ErrUnsupported].
|
|
package pktinfo
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
)
|
|
|
|
// Enable asks the kernel to include, in the control messages of each
|
|
// datagram received on pc, the local address the datagram was sent to.
|
|
// See [Dst]. A wildcard "udp" socket is dual-stack and receives IPv4
|
|
// datagrams too; both families are handled.
|
|
func Enable(pc *net.UDPConn) error {
|
|
return enable(pc)
|
|
}
|
|
|
|
// Dst returns the local destination address recorded in the control
|
|
// messages oob of a received datagram, or the zero [netip.Addr] if there
|
|
// is none. IPv4 addresses received on a dual-stack socket are returned
|
|
// unmapped.
|
|
func Dst(oob []byte) netip.Addr {
|
|
return dst(oob)
|
|
}
|
|
|
|
// AppendSrc appends to b the control message that makes a datagram sent
|
|
// with it be sent from the local address src. It returns b unchanged if
|
|
// src isn't a usable source address (invalid, unspecified, or multicast).
|
|
//
|
|
// Only the source address is pinned. The interface index is left zero so
|
|
// that routing (including policy routing) still decides the egress
|
|
// interface.
|
|
func AppendSrc(b []byte, src netip.Addr) []byte {
|
|
if !src.IsValid() || src.IsUnspecified() || src.IsMulticast() {
|
|
return b
|
|
}
|
|
return appendSrc(b, src)
|
|
}
|