wgengine/magicsock: send CallMeMaybe to peers whose endpoints we don't know (#22902)

magicsock only sent a CallMeMaybe after disco-pinging at least one of
the peer's endpoints. So if neither node's netmap had the other's
endpoints, neither ever started discovery, and traffic between them
stayed on DERP for good, even though each had the other's disco key and
home DERP and could have told the other its endpoints over DERP.

Control sends Funnel targets to Tailscale's ingress nodes without their
endpoints, and sent the ingress nodes to the targets without endpoints
too, so all Funnel traffic was relayed over DERP.

When we know none of a peer's endpoints, send it a CallMeMaybe anyway,
at most once per discoPingInterval. The peer then pings our endpoints,
which tells us its endpoints, and normal discovery takes over. Only one
of the two nodes needs this change.

Add TestMemnetDirectWithoutEndpoints, which fails without this change.

Updates #22576

Change-Id: I098ca7023f38070eb6e27aff876d3030d9fdbadf

Signed-off-by: Mike O'Driscoll <[email protected]>
This commit is contained in:
Mike O'Driscoll authored and GitHub committed 2026-12-04 13:43:24 -05:00
1 parent 63a8c8dd39
commit 87e4d40046
2 files changed
+109 -9

No files matched your search

+85 -8
View File
@@ -64,8 +64,8 @@ func TestMemnetPathFailover(t *testing.T) {
n1 := newMemnetNode(t, nw, derpMap, "n1", netip.MustParseAddr("100.64.0.1"))
n2 := newMemnetNode(t, nw, derpMap, "n2", netip.MustParseAddr("100.64.0.2"))
n1.setPeer(t, n2)
n2.setPeer(t, n1)
n1.setPeer(t, n2, true)
n2.setPeer(t, n1, true)
// n2 echoes lines back on port 80.
ln, err := n2.ns.ListenTCP("tcp4", netip.AddrPortFrom(n2.ip, 80).String())
@@ -153,6 +153,80 @@ func TestMemnetPathFailover(t *testing.T) {
})
}
// TestMemnetDirectWithoutEndpoints checks that two nodes find a direct UDP
// path even when neither node's netmap has the other's endpoints, as long as
// each has the other's disco key and home DERP. Their CallMeMaybe messages
// over DERP, which carry the sender's endpoints, should be enough.
//
// Control sends Funnel targets to Tailscale's ingress nodes without their
// endpoints, and (until the ingress nodes report theirs) sends the ingress
// nodes to the targets without endpoints too.
func TestMemnetDirectWithoutEndpoints(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
nw := new(memnet.Network)
derpMap := runMemnetDERPAndSTUN(t, nw)
n1 := newMemnetNode(t, nw, derpMap, "n1", netip.MustParseAddr("100.64.0.1"))
n2 := newMemnetNode(t, nw, derpMap, "n2", netip.MustParseAddr("100.64.0.2"))
// Each node discovers its own endpoints, but control doesn't
// tell the other node about them.
n1.endpoints(t)
n2.endpoints(t)
n1.setPeer(t, n2, false)
n2.setPeer(t, n1, false)
ln, err := n2.ns.ListenTCP("tcp4", netip.AddrPortFrom(n2.ip, 80).String())
if err != nil {
t.Fatal(err)
}
defer ln.Close()
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go func() {
defer c.Close()
io.Copy(c, c)
}()
}
}()
ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
defer cancel()
conn, err := n1.ns.DialContextTCP(ctx, netip.AddrPortFrom(n2.ip, 80))
if err != nil {
t.Fatalf("DialContextTCP: %v", err)
}
defer conn.Close()
br := bufio.NewReader(conn)
// Keep a stream busy for up to a minute, so both nodes keep
// trying to find a direct path.
t0 := time.Now()
for seq := 1; ; seq++ {
line := fmt.Sprintf("line %d\n", seq)
conn.SetDeadline(time.Now().Add(30 * time.Second))
if _, err := io.WriteString(conn, line); err != nil {
t.Fatalf("write %d: %v", seq, err)
}
if got, err := br.ReadString('\n'); err != nil || got != line {
t.Fatalf("read %d = %q, %v; want %q", seq, got, err, line)
}
p1, p2 := n1.pathTo(n2), n2.pathTo(n1)
if p1 != "" && p2 != "" {
t.Logf("direct after %v: %s, %s", time.Since(t0), p1, p2)
return
}
if time.Since(t0) > time.Minute {
t.Fatalf("after %v, paths are %q and %q; want both direct", time.Since(t0), p1, p2)
}
time.Sleep(time.Second)
}
})
}
// runMemnetDERPAndSTUN runs a DERP server (over TLS) and a STUN server on
// nw and returns a DERP map for them. They stop when t's test ends.
func runMemnetDERPAndSTUN(t *testing.T, nw *memnet.Network) *tailcfg.DERPMap {
@@ -301,15 +375,15 @@ func (n *memnetNode) endpoints(t *testing.T) []netip.AddrPort {
return nil
}
// setPeer configures n with peer as its only peer, including peer's UDP
// endpoints, as a control server would.
func (n *memnetNode) setPeer(t *testing.T, peer *memnetNode) {
// setPeer configures n with peer as its only peer, as a control server would,
// including peer's UDP endpoints if withEndpoints.
func (n *memnetNode) setPeer(t *testing.T, peer *memnetNode, withEndpoints bool) {
self := (&tailcfg.Node{
Key: n.priv.Public(),
Addresses: []netip.Prefix{netip.PrefixFrom(n.ip, 32)},
}).View()
peerPrefix := netip.PrefixFrom(peer.ip, 32)
pn := (&tailcfg.Node{
peerNode := &tailcfg.Node{
ID: 1,
StableID: "1",
Name: peer.name + ".",
@@ -318,8 +392,11 @@ func (n *memnetNode) setPeer(t *testing.T, peer *memnetNode) {
Addresses: []netip.Prefix{peerPrefix},
AllowedIPs: []netip.Prefix{peerPrefix},
HomeDERP: 1,
Endpoints: peer.endpoints(t),
}).View()
}
if withEndpoints {
peerNode.Endpoints = peer.endpoints(t)
}
pn := peerNode.View()
n.mc.SetNetworkMap(self, []tailcfg.NodeView{pn})
n.eng.SetSelfNode(self)
+24 -1
View File
@@ -87,6 +87,7 @@ type endpoint struct {
lastSendAny mono.Time // last time there were outgoing packets sent this peer from any trigger, internal or external to magicsock
lastFullPing mono.Time // last time we pinged all disco or wireguard only endpoints
lastUDPRelayPathDiscovery mono.Time // last time we ran UDP relay path discovery
lastBlindCallMeMaybe mono.Time // last time we sent a CallMeMaybe while knowing none of the peer's endpoints; see sendDiscoPingsLocked
derpAddr netip.AddrPort // fallback/bootstrap path, if non-zero (non-zero for well-behaved clients)
bestAddr addrQuality // best non-DERP path; zero if none; mutate via setBestAddrLocked()
@@ -1453,13 +1454,35 @@ func (de *endpoint) sendDiscoPingsLocked(now mono.Time, sendCallMeMaybe bool) {
de.startDiscoPingLocked(epAddr{ap: ep}, now, pingDiscovery, 0, nil)
}
derpAddr := de.derpAddr
if buildfeatures.HasNATTraversal && sentAny && sendCallMeMaybe && derpAddr.IsValid() {
if !buildfeatures.HasNATTraversal || !sendCallMeMaybe || !derpAddr.IsValid() {
return
}
if sentAny {
// Have our magicsock.Conn figure out its STUN endpoint (if
// it doesn't know already) and then send a CallMeMaybe
// message to our peer via DERP informing them that we've
// sent so our firewall ports are probably open and now
// would be a good time for them to connect.
go de.c.enqueueCallMeMaybe(derpAddr, de)
return
}
if len(de.endpointState) == 0 && (de.lastBlindCallMeMaybe.IsZero() || now.Sub(de.lastBlindCallMeMaybe) >= discoPingInterval) {
// We know none of the peer's endpoints, so there was nothing to
// ping. Send a CallMeMaybe anyway, at most once per
// discoPingInterval, so the peer learns our endpoints and pings
// them. Its pings tell us its endpoints (see
// [Conn.handlePingLocked]), and we can then upgrade from DERP to
// a direct path. Without this, if the peer doesn't know our
// endpoints either (such as when control sends neither node the
// other's), neither side would ever start discovery.
//
// Our firewall hasn't been opened for the peer, so its pings
// only get through if we're directly reachable or our NAT
// doesn't filter by endpoint. That's still better than staying
// on DERP forever, and once we learn the peer's endpoints, the
// usual discovery takes over.
de.lastBlindCallMeMaybe = now
go de.c.enqueueCallMeMaybe(derpAddr, de)
}
}