mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-20 00:31:17 -08:00
net/{pktinfo,stunserver}: reply from the address the request was sent to
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
This commit is contained in:
1 parent
816b661f0d
commit
ee029383f7
9 files changed
+529
-12
No files matched your search
@@ -107,6 +107,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
|
||||
💣 tailscale.com/net/netns from tailscale.com/derp/derphttp
|
||||
tailscale.com/net/netutil from tailscale.com/client/local+
|
||||
tailscale.com/net/netx from tailscale.com/net/dnscache+
|
||||
tailscale.com/net/pktinfo from tailscale.com/net/stunserver
|
||||
tailscale.com/net/routecheck from tailscale.com/client/local
|
||||
tailscale.com/net/routecheck/peernode from tailscale.com/net/routecheck
|
||||
tailscale.com/net/sockstats from tailscale.com/derp/derphttp
|
||||
|
||||
@@ -53,6 +53,7 @@ tailscale.com/cmd/stund dependencies: (generated by github.com/tailscale/depawar
|
||||
tailscale.com/kube/kubetypes from tailscale.com/envknob
|
||||
tailscale.com/metrics from tailscale.com/net/stunserver+
|
||||
tailscale.com/net/netaddr from tailscale.com/net/tsaddr
|
||||
tailscale.com/net/pktinfo from tailscale.com/net/stunserver
|
||||
tailscale.com/net/stun from tailscale.com/net/stunserver
|
||||
tailscale.com/net/stunserver from tailscale.com/cmd/stund
|
||||
tailscale.com/net/tsaddr from tailscale.com/tsweb
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// 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)
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package pktinfo
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math/bits"
|
||||
"net"
|
||||
"net/netip"
|
||||
"slices"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// cmsgLenSize is the size of struct cmsghdr's cmsg_len field, a size_t,
|
||||
// which is the word size on every Linux ABI Go supports.
|
||||
const cmsgLenSize = bits.UintSize / 8
|
||||
|
||||
// appendSrc assumes cmsg_len is followed by exactly the two int32 fields
|
||||
// cmsg_level and cmsg_type. Fail to compile otherwise.
|
||||
var _ [0]struct{} = [unix.SizeofCmsghdr - cmsgLenSize - 8]struct{}{}
|
||||
|
||||
func enable(pc *net.UDPConn) error {
|
||||
// A wildcard "udp" socket is AF_INET6 (dual-stack) and its
|
||||
// LocalAddr is "[::]"; a "udp4" or IPv4-bound socket is AF_INET.
|
||||
v6 := true
|
||||
if la, ok := pc.LocalAddr().(*net.UDPAddr); ok && la.IP.To4() != nil {
|
||||
v6 = false
|
||||
}
|
||||
rc, err := pc.SyscallConn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var sockErr error
|
||||
err = rc.Control(func(fd uintptr) {
|
||||
if v6 {
|
||||
sockErr = unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_RECVPKTINFO, 1)
|
||||
if sockErr != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
// On a dual-stack socket, IPV6_RECVPKTINFO alone reports
|
||||
// IPv4 datagrams' header destination as a v4-mapped address,
|
||||
// which for a broadcast datagram is the broadcast address:
|
||||
// useless as a reply source. IP_PKTINFO applies to the IPv4
|
||||
// datagrams on the same socket and carries ipi_spec_dst, the
|
||||
// unicast address the kernel considers ours for replying.
|
||||
sockErr = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_PKTINFO, 1)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return sockErr
|
||||
}
|
||||
|
||||
func dst(oob []byte) netip.Addr {
|
||||
msgs, err := unix.ParseSocketControlMessage(oob)
|
||||
if err != nil {
|
||||
return netip.Addr{}
|
||||
}
|
||||
var v6 netip.Addr
|
||||
for _, m := range msgs {
|
||||
switch {
|
||||
case m.Header.Level == unix.IPPROTO_IP && m.Header.Type == unix.IP_PKTINFO:
|
||||
// struct in_pktinfo is ipi_ifindex, ipi_spec_dst, ipi_addr.
|
||||
// This is the preferred answer; see enable.
|
||||
if len(m.Data) >= unix.SizeofInet4Pktinfo {
|
||||
return netip.AddrFrom4([4]byte(m.Data[4:8]))
|
||||
}
|
||||
case m.Header.Level == unix.IPPROTO_IPV6 && m.Header.Type == unix.IPV6_PKTINFO:
|
||||
// struct in6_pktinfo is ipi6_addr followed by ipi6_ifindex.
|
||||
if len(m.Data) >= unix.SizeofInet6Pktinfo {
|
||||
v6 = netip.AddrFrom16([16]byte(m.Data[:16])).Unmap()
|
||||
}
|
||||
}
|
||||
}
|
||||
return v6
|
||||
}
|
||||
|
||||
// appendSrc builds the control message by hand rather than with
|
||||
// golang.org/x/net/ipv6, whose ControlMessage silently drops a v4-mapped
|
||||
// Src, or with [unix.PktInfo4] and [unix.PktInfo6], which allocate per call.
|
||||
//
|
||||
// The cmsg level follows the address family of src, not the socket's.
|
||||
// Linux accepts an IP_PKTINFO cmsg on a dual-stack AF_INET6 socket when
|
||||
// the destination is IPv4, since such sends go through the IPv4 path.
|
||||
func appendSrc(b []byte, src netip.Addr) []byte {
|
||||
var level, typ, dataLen int
|
||||
if src.Is4() {
|
||||
level, typ, dataLen = unix.SOL_IP, unix.IP_PKTINFO, unix.SizeofInet4Pktinfo
|
||||
} else {
|
||||
level, typ, dataLen = unix.SOL_IPV6, unix.IPV6_PKTINFO, unix.SizeofInet6Pktinfo
|
||||
}
|
||||
start := len(b)
|
||||
b = slices.Grow(b, unix.CmsgSpace(dataLen))[:start+unix.CmsgSpace(dataLen)]
|
||||
clear(b[start:])
|
||||
|
||||
hdr := b[start:]
|
||||
switch cmsgLenSize {
|
||||
case 8:
|
||||
binary.NativeEndian.PutUint64(hdr, uint64(unix.CmsgLen(dataLen)))
|
||||
case 4:
|
||||
binary.NativeEndian.PutUint32(hdr, uint32(unix.CmsgLen(dataLen)))
|
||||
}
|
||||
binary.NativeEndian.PutUint32(hdr[cmsgLenSize:], uint32(level))
|
||||
binary.NativeEndian.PutUint32(hdr[cmsgLenSize+4:], uint32(typ))
|
||||
|
||||
data := hdr[unix.CmsgLen(0):]
|
||||
if src.Is4() {
|
||||
// struct in_pktinfo is ipi_ifindex, ipi_spec_dst, ipi_addr.
|
||||
a := src.As4()
|
||||
copy(data[4:], a[:])
|
||||
} else {
|
||||
// struct in6_pktinfo is ipi6_addr followed by ipi6_ifindex.
|
||||
a := src.As16()
|
||||
copy(data, a[:])
|
||||
}
|
||||
return b
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package pktinfo
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/netip"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
"tailscale.com/util/cibuild"
|
||||
"tailscale.com/util/must"
|
||||
)
|
||||
|
||||
// TestReplySourceAddr verifies that an echo server using this package
|
||||
// replies from the address the request was sent to, not from whichever
|
||||
// address the kernel would otherwise pick for the reply's destination.
|
||||
// It sends from 127.0.0.1 to the server at 127.0.0.2 (all of 127/8 is
|
||||
// local on Linux); without pktinfo the kernel replies from 127.0.0.1.
|
||||
func TestReplySourceAddr(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
network string // "udp" for a dual-stack socket, "udp4" for AF_INET
|
||||
pktInfo bool // whether to use pktinfo; false is the negative control
|
||||
}{
|
||||
{"dualstack", "udp", true},
|
||||
{"dualstack-nopktinfo", "udp", false},
|
||||
{"v4", "udp4", true},
|
||||
{"v4-nopktinfo", "udp4", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.network == "udp" {
|
||||
// The dual-stack cases need an AF_INET6 socket,
|
||||
// which net.ListenUDP("udp") quietly won't
|
||||
// create on a host with IPv6 disabled.
|
||||
if c, err := net.ListenPacket("udp6", "[::1]:0"); err != nil {
|
||||
if !cibuild.On() {
|
||||
t.Skipf("IPv6 not supported: %v", err)
|
||||
}
|
||||
t.Fatalf("IPv6 not supported: %v", err)
|
||||
} else {
|
||||
c.Close()
|
||||
}
|
||||
}
|
||||
srv := must.Get(net.ListenUDP(tt.network, nil))
|
||||
defer srv.Close()
|
||||
if tt.pktInfo {
|
||||
if err := Enable(srv); err != nil {
|
||||
t.Fatalf("Enable: %v", err)
|
||||
}
|
||||
}
|
||||
go func() {
|
||||
var buf, oob [1500]byte
|
||||
n, oobn, _, from, err := srv.ReadMsgUDPAddrPort(buf[:], oob[:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
srv.WriteMsgUDPAddrPort(buf[:n], AppendSrc(nil, Dst(oob[:oobn])), from)
|
||||
}()
|
||||
port := uint16(srv.LocalAddr().(*net.UDPAddr).Port)
|
||||
serverAddr := netip.AddrPortFrom(netip.MustParseAddr("127.0.0.2"), port)
|
||||
|
||||
c := must.Get(net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)}))
|
||||
defer c.Close()
|
||||
c.SetDeadline(time.Now().Add(5 * time.Second))
|
||||
must.Get(c.WriteToUDPAddrPort([]byte("hello"), serverAddr))
|
||||
|
||||
var buf [1500]byte
|
||||
n, from, err := c.ReadFromUDPAddrPort(buf[:])
|
||||
if err != nil {
|
||||
t.Fatalf("reading reply: %v", err)
|
||||
}
|
||||
if string(buf[:n]) != "hello" {
|
||||
t.Errorf("reply = %q; want %q", buf[:n], "hello")
|
||||
}
|
||||
wantFrom := serverAddr
|
||||
if !tt.pktInfo {
|
||||
wantFrom = netip.AddrPortFrom(netip.MustParseAddr("127.0.0.1"), port)
|
||||
}
|
||||
if from != wantFrom {
|
||||
t.Errorf("reply came from %v; want %v", from, wantFrom)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAppendSrcMatchesUnix checks the hand-built control messages against
|
||||
// the ones x/sys/unix builds.
|
||||
func TestAppendSrcMatchesUnix(t *testing.T) {
|
||||
v4 := netip.MustParseAddr("192.0.2.1")
|
||||
v6 := netip.MustParseAddr("2001:db8::1")
|
||||
|
||||
want4 := unix.PktInfo4(&unix.Inet4Pktinfo{Spec_dst: v4.As4()})
|
||||
if got := AppendSrc(nil, v4); string(got) != string(want4) {
|
||||
t.Errorf("v4: got % x, want % x", got, want4)
|
||||
}
|
||||
want6 := unix.PktInfo6(&unix.Inet6Pktinfo{Addr: v6.As16()})
|
||||
if got := AppendSrc(nil, v6); string(got) != string(want6) {
|
||||
t.Errorf("v6: got % x, want % x", got, want6)
|
||||
}
|
||||
if got := AppendSrc([]byte("prefix"), v4); string(got) != "prefix"+string(want4) {
|
||||
t.Errorf("append: got % x", got)
|
||||
}
|
||||
for _, a := range []netip.Addr{{}, netip.IPv4Unspecified(), netip.IPv6Unspecified(), netip.MustParseAddr("224.0.0.1"), netip.MustParseAddr("ff02::1")} {
|
||||
if got := AppendSrc(nil, a); got != nil {
|
||||
t.Errorf("AppendSrc(%v) = % x; want nil", a, got)
|
||||
}
|
||||
}
|
||||
if got := Dst(want4); got != v4 {
|
||||
t.Errorf("Dst(v4 pktinfo) = %v; want %v", got, v4)
|
||||
}
|
||||
if got := Dst(want6); got != v6 {
|
||||
t.Errorf("Dst(v6 pktinfo) = %v; want %v", got, v6)
|
||||
}
|
||||
mapped := unix.PktInfo6(&unix.Inet6Pktinfo{Addr: v4.As16()})
|
||||
if got := Dst(mapped); got != v4 {
|
||||
t.Errorf("Dst(mapped v4 pktinfo) = %v; want %v", got, v4)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBroadcastDst verifies that an IPv4 broadcast datagram received on a
|
||||
// dual-stack socket reports a unicast reply address, not the broadcast
|
||||
// address, and that the reply then actually gets out. It needs a
|
||||
// broadcast-capable interface and skips without one.
|
||||
func TestBroadcastDst(t *testing.T) {
|
||||
var local, bcast netip.Addr
|
||||
ifs := must.Get(net.Interfaces())
|
||||
for _, ifi := range ifs {
|
||||
const want = net.FlagUp | net.FlagBroadcast
|
||||
if ifi.Flags&net.FlagLoopback != 0 || ifi.Flags&want != want {
|
||||
continue
|
||||
}
|
||||
addrs, _ := ifi.Addrs()
|
||||
for _, a := range addrs {
|
||||
ipn, ok := a.(*net.IPNet)
|
||||
if !ok || ipn.IP.To4() == nil {
|
||||
continue
|
||||
}
|
||||
pfx := must.Get(netip.ParsePrefix(ipn.String())).Masked()
|
||||
if pfx.Bits() > 30 {
|
||||
continue // no directed broadcast address
|
||||
}
|
||||
local, _ = netip.AddrFromSlice(ipn.IP.To4())
|
||||
b := pfx.Addr().As4()
|
||||
for i := range b {
|
||||
b[i] |= ^ipn.Mask[i]
|
||||
}
|
||||
bcast = netip.AddrFrom4(b)
|
||||
break
|
||||
}
|
||||
if bcast.IsValid() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !bcast.IsValid() {
|
||||
t.Skip("no broadcast-capable IPv4 interface")
|
||||
}
|
||||
t.Logf("using local %v, broadcast %v", local, bcast)
|
||||
|
||||
srv := must.Get(net.ListenUDP("udp", nil))
|
||||
defer srv.Close()
|
||||
must.Do(Enable(srv))
|
||||
port := uint16(srv.LocalAddr().(*net.UDPAddr).Port)
|
||||
gotDst := make(chan netip.Addr, 1)
|
||||
go func() {
|
||||
var buf, oob [1500]byte
|
||||
n, oobn, _, from, err := srv.ReadMsgUDPAddrPort(buf[:], oob[:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dst := Dst(oob[:oobn])
|
||||
gotDst <- dst
|
||||
srv.WriteMsgUDPAddrPort(buf[:n], AppendSrc(nil, dst), from)
|
||||
}()
|
||||
|
||||
c := must.Get(net.ListenUDP("udp4", &net.UDPAddr{IP: local.AsSlice()}))
|
||||
defer c.Close()
|
||||
rc := must.Get(c.SyscallConn())
|
||||
rc.Control(func(fd uintptr) {
|
||||
must.Do(unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BROADCAST, 1))
|
||||
})
|
||||
c.SetDeadline(time.Now().Add(5 * time.Second))
|
||||
must.Get(c.WriteToUDPAddrPort([]byte("hello"), netip.AddrPortFrom(bcast, port)))
|
||||
|
||||
var buf [1500]byte
|
||||
_, from, err := c.ReadFromUDPAddrPort(buf[:])
|
||||
if err != nil {
|
||||
t.Fatalf("reading reply: %v", err)
|
||||
}
|
||||
if dst := <-gotDst; dst != local {
|
||||
t.Errorf("Dst = %v; want %v", dst, local)
|
||||
}
|
||||
if from.Addr() != local {
|
||||
t.Errorf("reply came from %v; want %v", from.Addr(), local)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
//go:build !linux
|
||||
|
||||
package pktinfo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
func enable(pc *net.UDPConn) error { return errors.ErrUnsupported }
|
||||
|
||||
func dst(oob []byte) netip.Addr { return netip.Addr{} }
|
||||
|
||||
func appendSrc(b []byte, src netip.Addr) []byte { return b }
|
||||
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package stunserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/netip"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"tailscale.com/net/stun"
|
||||
"tailscale.com/util/cibuild"
|
||||
"tailscale.com/util/must"
|
||||
)
|
||||
|
||||
// TestReplySourceAddr verifies that a response is sent from the address
|
||||
// the request was sent to. It sends from 127.0.0.1 to the server at
|
||||
// 127.0.0.2 (all of 127/8 is local on Linux); without pktinfo the kernel
|
||||
// would reply from 127.0.0.1 instead. See the tests in net/pktinfo for the
|
||||
// negative control and the v4-only socket case.
|
||||
func TestReplySourceAddr(t *testing.T) {
|
||||
// Listen always creates a dual-stack socket, which needs IPv6.
|
||||
if c, err := net.ListenPacket("udp6", "[::1]:0"); err != nil {
|
||||
if !cibuild.On() {
|
||||
t.Skipf("IPv6 not supported: %v", err)
|
||||
}
|
||||
t.Fatalf("IPv6 not supported: %v", err)
|
||||
} else {
|
||||
c.Close()
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
s := New(ctx)
|
||||
must.Do(s.Listen(":0"))
|
||||
if !s.pktInfo {
|
||||
t.Fatal("pktinfo not enabled")
|
||||
}
|
||||
go s.Serve()
|
||||
port := uint16(s.LocalAddr().(*net.UDPAddr).Port)
|
||||
serverAddr := netip.AddrPortFrom(netip.MustParseAddr("127.0.0.2"), port)
|
||||
|
||||
c := must.Get(net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)}))
|
||||
defer c.Close()
|
||||
c.SetDeadline(time.Now().Add(5 * time.Second))
|
||||
txid := stun.NewTxID()
|
||||
must.Get(c.WriteToUDPAddrPort(stun.Request(txid), serverAddr))
|
||||
|
||||
var buf [1500]byte
|
||||
n, from, err := c.ReadFromUDPAddrPort(buf[:])
|
||||
if err != nil {
|
||||
t.Fatalf("reading STUN response: %v", err)
|
||||
}
|
||||
if from != serverAddr {
|
||||
t.Errorf("response came from %v; want %v", from, serverAddr)
|
||||
}
|
||||
tid, mapped, err := stun.ParseResponse(buf[:n])
|
||||
if err != nil {
|
||||
t.Fatalf("parsing STUN response: %v", err)
|
||||
}
|
||||
if tid != txid {
|
||||
t.Errorf("wrong transaction ID")
|
||||
}
|
||||
clientAddr := c.LocalAddr().(*net.UDPAddr).AddrPort()
|
||||
if mapped != clientAddr {
|
||||
t.Errorf("mapped address = %v; want %v", mapped, clientAddr)
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ package stunserver
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"expvar"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tailscale.com/metrics"
|
||||
"tailscale.com/net/pktinfo"
|
||||
"tailscale.com/net/stun"
|
||||
)
|
||||
|
||||
@@ -29,11 +31,31 @@ var (
|
||||
|
||||
stunIPv4 = stunAddrFamily.Get("ipv4")
|
||||
stunIPv6 = stunAddrFamily.Get("ipv6")
|
||||
|
||||
// stunNoLocalAddr counts requests for which the kernel didn't report
|
||||
// the local address the request was sent to, despite pktinfo being
|
||||
// enabled, so the response was sent from a kernel-chosen source
|
||||
// address instead.
|
||||
stunNoLocalAddr = newCounter("counter_no_local_addr")
|
||||
)
|
||||
|
||||
func newCounter(name string) *expvar.Int {
|
||||
v := new(expvar.Int)
|
||||
stats.Set(name, v)
|
||||
return v
|
||||
}
|
||||
|
||||
type STUNServer struct {
|
||||
ctx context.Context // ctx signals service shutdown
|
||||
pc *net.UDPConn // pc is the UDP listener
|
||||
|
||||
// pktInfo is whether the kernel reports the destination address of
|
||||
// each request (see [pktinfo.Enable]). When it does, the response is
|
||||
// sent from that same address rather than from whatever source
|
||||
// address a route lookup on the reply's destination would pick.
|
||||
// On a multi-homed server those differ, and replies from the wrong
|
||||
// address break conntrack-based policy routing (tailscale/tailscale#21404).
|
||||
pktInfo bool
|
||||
}
|
||||
|
||||
// New creates a new STUN server. The server is shutdown when ctx is done.
|
||||
@@ -51,6 +73,11 @@ func (s *STUNServer) Listen(listenAddr string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := pktinfo.Enable(s.pc); err == nil {
|
||||
s.pktInfo = true
|
||||
} else if !errors.Is(err, errors.ErrUnsupported) {
|
||||
log.Printf("STUN server: pktinfo unavailable; responses will use the kernel-chosen source address: %v", err)
|
||||
}
|
||||
log.Printf("STUN server listening on %v", s.LocalAddr())
|
||||
// close the listener on shutdown in order to break out of the read loop
|
||||
go func() {
|
||||
@@ -63,13 +90,9 @@ func (s *STUNServer) Listen(listenAddr string) error {
|
||||
// Serve starts serving responses to STUN requests. Listen must be called before Serve.
|
||||
func (s *STUNServer) Serve() error {
|
||||
var buf [64 << 10]byte
|
||||
var (
|
||||
n int
|
||||
ua *net.UDPAddr
|
||||
err error
|
||||
)
|
||||
var oob, oobOut [256]byte
|
||||
for {
|
||||
n, ua, err = s.pc.ReadFromUDP(buf[:])
|
||||
n, remote, local, err := s.readFrom(buf[:], oob[:])
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) {
|
||||
return nil
|
||||
@@ -89,15 +112,17 @@ func (s *STUNServer) Serve() error {
|
||||
stunNotSTUN.Add(1)
|
||||
continue
|
||||
}
|
||||
if ua.IP.To4() != nil {
|
||||
remote = netip.AddrPortFrom(remote.Addr().Unmap(), remote.Port())
|
||||
if remote.Addr().Is4() {
|
||||
stunIPv4.Add(1)
|
||||
} else {
|
||||
stunIPv6.Add(1)
|
||||
}
|
||||
addr, _ := netip.AddrFromSlice(ua.IP)
|
||||
res := stun.Response(txid, netip.AddrPortFrom(addr, uint16(ua.Port)))
|
||||
_, err = s.pc.WriteTo(res, ua)
|
||||
if err != nil {
|
||||
if s.pktInfo && !local.IsValid() {
|
||||
stunNoLocalAddr.Add(1)
|
||||
}
|
||||
res := stun.Response(txid, remote)
|
||||
if err := s.writeTo(res, remote, local, oobOut[:0]); err != nil {
|
||||
stunWriteError.Add(1)
|
||||
} else {
|
||||
stunSuccess.Add(1)
|
||||
@@ -105,6 +130,34 @@ func (s *STUNServer) Serve() error {
|
||||
}
|
||||
}
|
||||
|
||||
// readFrom reads one datagram into buf, returning its length, its sender,
|
||||
// and the local address it was sent to (the zero [netip.Addr] if unknown).
|
||||
// oob is scratch space for control messages.
|
||||
func (s *STUNServer) readFrom(buf, oob []byte) (n int, remote netip.AddrPort, local netip.Addr, err error) {
|
||||
if !s.pktInfo {
|
||||
n, remote, err = s.pc.ReadFromUDPAddrPort(buf)
|
||||
return n, remote, netip.Addr{}, err
|
||||
}
|
||||
n, oobn, _, remote, err := s.pc.ReadMsgUDPAddrPort(buf, oob)
|
||||
if err != nil {
|
||||
return 0, remote, netip.Addr{}, err
|
||||
}
|
||||
return n, remote, pktinfo.Dst(oob[:oobn]), nil
|
||||
}
|
||||
|
||||
// writeTo sends b to remote from the local address local, if known.
|
||||
// Otherwise the kernel picks the source address. oob is scratch space for
|
||||
// control messages.
|
||||
func (s *STUNServer) writeTo(b []byte, remote netip.AddrPort, local netip.Addr, oob []byte) error {
|
||||
oob = pktinfo.AppendSrc(oob, local)
|
||||
if len(oob) == 0 {
|
||||
_, err := s.pc.WriteToUDPAddrPort(b, remote)
|
||||
return err
|
||||
}
|
||||
_, _, err := s.pc.WriteMsgUDPAddrPort(b, oob, remote)
|
||||
return err
|
||||
}
|
||||
|
||||
// ListenAndServe starts the STUN server on listenAddr.
|
||||
func (s *STUNServer) ListenAndServe(listenAddr string) error {
|
||||
if err := s.Listen(listenAddr); err != nil {
|
||||
|
||||
@@ -879,7 +879,8 @@ func (s *Server) packetReadLoop(readFromSocket, otherSocket batching.Conn, readF
|
||||
writeBuffsByDest := make(map[netip.AddrPort][][]byte, batching.MaximumWriteBatchSize)
|
||||
|
||||
for {
|
||||
// TODO: extract laddr from IP_PKTINFO for use in reply
|
||||
// TODO: extract laddr from IP_PKTINFO for use in reply, using
|
||||
// tailscale.com/net/pktinfo.
|
||||
// ReadBatch will split coalesced datagrams before returning, which
|
||||
// WriteBatchTo will re-coalesce further down. We _could_ be more
|
||||
// efficient and not split datagrams that belong to the same VNI if they
|
||||
|
||||
Reference in new issue
Block a user