Skip to content

Connect via Tailscale over tailscale-rs (all three platforms, incl. visionOS), experimental - #13

Open
jhen0409 wants to merge 13 commits into
mainfrom
tailscale-rs-host-option
Open

jhen0409 wants to merge 13 commits into
mainfrom
tailscale-rs-host-option

Conversation

@jhen0409

@jhen0409 jhen0409 commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

What

A second, parallel Connect via Tailscale host option — SSH through an embedded userspace tailnet node — backed by tailscale-rs (Rust) instead of the Go libtailscale used in #12. The point: Rust has an aarch64-apple-visionos target, so this works on Vision Pro — the platform the Go path can structurally never reach. No system VPN, no NetworkExtension.

Full record + build matrix: local-plan/tailscale-rs-investigation.md (gitignored; §7a has the v0.6.1 re-evaluation, not yet updated for v0.7.1).

Upstream: tailscale-rs v0.7.1

Pinned to tag v0.7.1 (a2a56c4, 2026-11-06), unpatched: the local ts_netmon cfg patch landed upstream in v0.7.0, so Vendor/tailscale-rs/patches is gone. The C ABI is unchanged from v0.6.1 (the vendored tailscale.h still matches); upstream only moved ts_ffi to crates/bindings/ts_ffi.

  • Crypto audited. v0.6.0 remediated a third-party audit of ts_tunnel; upstream now recommends it from a security perspective. The TS_RS_EXPERIMENT env gate is gone (the app no longer sets it).
  • Direct connections now form "in many cases", with DERP relay as the fallback. Upstream's network monitor covers macOS/Linux/Windows only, so whether direct paths form on iOS/visionOS is unverified until a device run.
  • iOS is on upstream's unsupported-platform list (visionOS isn't mentioned). Both build and link regardless.
  • ABI drift: only ts_config.ephemeral. The node persists its key, so it registers as a durable machine (ephemeral = false, set explicitly).
  • Still pre-1.0 (no API stability), no MagicDNS / peer lookup by name.

Backend deltas from #12 (all from the rs C ABI, CTailscaleRS)

  • App-owned node identity → no plaintext state dir. tailscale-rs takes the three 32-byte node keys as input, so the app generates them with SecRandom and keeps the 96 bytes in the Keychain as a device-only item on its own service. Each device is its own tailnet node, while the auth key syncs through iCloud Keychain like other secrets.
  • Handle, not fd. A tailnet connection is an opaque handle with blocking ts_tcp_send/recv, so the one-shot loopback relay's remote side is a TailscaleRelayRemote protocol (handle-backed for real dials, fd-backed for fake-dial). Citadel still dials 127.0.0.1 through its ordinary bootstrap — the connect(on:) inEventLoop constraint that forced the relay in Experimental: Connect via Tailscale host option (iPhone/iPad) #12 is unchanged. The handle has no half-close, so teardown rides relay.close().
  • Host-key pinning applies through the relay. Pins are keyed by host id, so the 127.0.0.1 hop verifies against the same HostKeyVerifier as a direct dial.
  • A rejected auth key hangs startup, and the C ABI reports no auth verdict. At v0.7.1 ts_init returns a device in about 2 s and ts_ipv4_addr then blocks for good (measured with an invalid key against a macOS build of the archive; through v0.6.1 it was ts_init that hung). The 30 s deadline now covers ts_init and the address read together.
  • Peer resolution: no MagicDNS, so a hostname resolves via ts_peer_ipv4_addr → dial by IP; literals dial directly (TailscaleRSDialAddress, pure + tested).
  • ts_sockaddr_set_port is a no-op upstream (writes a by-value union copy); the tunnel sets sin_port/sin6_port directly.

UI

Ported onto main's UIKit forms: a Connect via Tailscale row in Add Host › Transport (mutually exclusive with mosh, missing-auth-key tip) and a Settings › Tailscale section (auth key, optional Headscale control URL, live node status; saved on Done). Copy is labelled experimental and localized (zh-Hant/ja, needs_review).

Scope

  • All three platforms.
  • Mosh mutually exclusive for v1. The rs ABI has datagram UDP (ts_udp_*), so mosh-over-tailnet is buildable later — the thing Go could never do — but it's deferred.
  • Auth-key-only login, nothing in the widget projection.

Verified

  • visionOS and iPad build and link the v0.7.1 archives; all four slices (iOS device, universal iOS sim, visionOS device + sim) rebuilt with Tools/build-tailscale-rs.sh.
  • SwiftLint strict: 0 violations. Unit tests: 1509 on visionOS, 1518 on iPad, no failures (after merging main at 1.5.2).
  • Rejected-key run on the visionOS simulator (MULTIPLEX_SEED_TAILSCALE=1 + an invalid MULTIPLEX_TAILSCALE_AUTHKEY): the terminal ends with "Tailscale startup timed out after 30 seconds. Check the auth key." A plain-SSH attach on the same simulator still goes LIVE.
  • Earlier: fake-dial E2E on an iOS 26.4 simulator (tmux attached through the loopback relay via MULTIPLEX_TAILSCALE_FAKE_DIAL); the investigation proved the C ABI reaches Tailscale's control plane and gets an auth verdict.
  • Not yet: a real-tailnet run on device (needs a reusable auth key), and a direct-connection check on iOS/visionOS.

Build notes

The four static archives (~20–50 MB each) are git-ignored; Tools/build-tailscale-rs.sh rebuilds them at the pin. iOS slices use Rust 1.98.1 (upstream's pin); the xros slices need nightly-2026-09-17 + -Zbuild-std while aarch64-apple-visionos stays tier 3.

Relationship to #12

#12 (Go) and this (Rust) are two backends for the same per-host option; if this proves out on device across all three platforms, it supersedes #12. Not intended to merge both.

jhen0409 added 2 commits July 23, 2026 20:37
A second, parallel embedded-Tailscale host option backed by tailscale-rs
(Rust) rather than Go libtailscale (PR #12). Because Rust has an
aarch64-apple-visionos target, the feature links and runs on Vision Pro —
the platform the Go path can never reach. SSH is dialed through an
in-process tsnet node; no system VPN, no NetworkExtension.

Backend deltas from the Go path, all driven by the rs C ABI (CTailscaleRS):

- The node identity is app-owned. tailscale-rs takes the three 32-byte
  node keys as an input and never exports them, so the app generates them
  with SecRandom and persists the 96 bytes in the Keychain — the tailnet
  node survives launches with no plaintext state directory, satisfying the
  keychain-only house rule the Go path had to except. The Go force-login
  constructor hack is gone (the auth key is a ts_init parameter).
- A tailnet connection is an opaque handle with blocking send/recv, not an
  fd, so the one-shot loopback relay's remote side is now a
  TailscaleRelayRemote protocol (handle-backed for real dials, fd-backed
  for the DEBUG fake dial). Citadel still dials 127.0.0.1 through its
  ordinary bootstrap — the inEventLoop constraint that forced the relay is
  unchanged. Half-close forwards through the seam; the handle has no
  half-close so full teardown rides relay.close() on SSHConnection.close().
- ts_init hangs on a rejected auth key, so init is raced against a 30 s
  deadline. TS_RS_EXPERIMENT is set before init (upstream gates the FFI
  on it); all peer traffic relays through public DERP today.

The UI compiles on all three platforms (no visionOS-disabled apology),
carries experimental/relayed copy and the missing-auth-key tip, and keeps
mosh mutually exclusive for v1 (the rs UDP ABI can lift that later). The
four static archives (incl. xros) are git-ignored; Tools/build-tailscale-rs.sh
rebuilds them at the pinned commit + one upstream patch. Full record:
local-plan/tailscale-rs-investigation.md.
Real tailnet dials failed with "unaddressable destination" and a
remote_endpoint of <peer-ip>:0 — the port never made it onto the
sockaddr. ts_parse_ip fills the address with port 0, and tailscale-rs's
ts_sockaddr_set_port is a no-op: it mutates a by-value copy of the union
field (`unsafe { addr.sa_data.sockaddr_in }.sin_port = port`, net_types.rs
:361/:365) and discards it. So the port stayed 0 for every dial.

Write sin_port/sin6_port directly onto the struct after parsing, host byte
order, keyed off the ffi's own TS_AF_INET/TS_AF_INET6 values — the pattern
the C examples use (tcp_echo.c sets .sin_port = 1234 and prints it with
%u). The fake-dial DEBUG path uses a plain kernel socket and never hit
this. Verified by build on all platforms + the upstream source; needs a
real tailnet to exercise end to end.
@jhen0409

Copy link
Copy Markdown
Member Author

Fixed the `unaddressable destination` / `:0` dial failure (`e0a9682`).

Root cause is an upstream tailscale-rs bug, not the app: `ts_sockaddr_set_port` is a no-op. It does `unsafe { addr.sa_data.sockaddr_in }.sin_port = port` — that copies the union field out by value, sets the port on the throwaway copy, and discards it (net_types.rs:361/365, both the v4 and v6 arms). `ts_parse_ip` fills the address with port 0, so with the setter doing nothing, every dial went out as `:0` and smoltcp rejected it.

The app now writes `sin_port`/`sin6_port` directly onto the sockaddr after parsing — host byte order, keyed off the ffi's own `TS_AF_INET`/`TS_AF_INET6` values (2/23), matching the pattern the C examples use (`tcp_echo.c` sets `.sin_port = 1234` and prints it with `%u`). The broken ffi setter is bypassed entirely.

Worth reporting upstream (their own examples avoid the setter, which is probably why it slipped through). All three platforms still build; the DEBUG fake-dial path uses a plain kernel socket so it never exercised this — needs a real tailnet dial to confirm, which is your retest.

After the port fix, real dials reached the connect stage but failed with
"no region stored in multiderp, no underlay route" then connection reset —
the peer's DERP region is in the netmap, but the local node hadn't yet
registered a transport for it. tailscale-rs establishes its DERP underlay
(the only data path today) asynchronously after the netmap, and the C ABI
exposes no readiness signal beyond ts_ipv4_addr (which unblocks at the
netmap, earlier). So a dial right after startup races DERP setup.

connectWithRetry retries the dial with backoff (8 × 900 ms) so the
transient window self-heals as the region's Uniderp spawns and reports its
transport. Bounded, so a genuinely unroutable peer still fails in ~7 s
rather than hanging; each attempt logs to the tailscale category. If DERP
never establishes on a device that's an upstream limit (DERP-only, no
Apple netmon), not a race — needs on-device confirmation that later
attempts land.
@jhen0409

Copy link
Copy Markdown
Member Author

Progress on the real-tailnet dial (`e590a39`).

The port fix worked — your second log shows `remote_endpoint: 100.77.122.118:22` (was `:0`). The new failure is a DERP-underlay readiness race, not addressing:

```
route_updater: no region stored in multiderp, no underlay route
tcp: connecting socket was reset or closed → connection reset
```

The peer's DERP region is in the netmap, but the local node hadn't registered a transport for that region yet. tailscale-rs brings up its DERP underlay (the only data path today — no direct connections) asynchronously after the netmap, and its C ABI has no "underlay ready" signal — `ts_ipv4_addr` unblocks at the netmap, which is earlier. So a dial fired right after node startup races DERP setup. The node starts lazily on the first dial, so the first attach races it hardest.

Mitigation: `connectWithRetry` retries the dial with backoff (8 × 900 ms ≈ 7 s) so the window self-heals once the region's Uniderp connects and reports its transport. Each attempt logs to the `tailscale` category (stderr / Console).

Please retest and watch Console: if a later attempt lands (attach succeeds within a few seconds), the race is covered. If it retries all 8 and still fails, DERP genuinely isn't establishing on your setup — that's the experimental backend's DERP-only + no-Apple-netmon limitation (investigation §1/§6), an upstream wall to raise with Tailscale rather than something I can fix app-side. The `RUST_LOG` lines (whether Uniderp connects to a DERP region at all) will tell us which.

@jhen0409
jhen0409 marked this pull request as draft July 23, 2026 13:26
Main moved Add Host and Settings to UIKit, so the Tailscale toggle,
missing-auth-key tip, and Settings section are ported onto the native
forms (SettingsTailscaleSection). The relay dial now uses the host-key
pin verifier instead of acceptAnything; pins are keyed by host id, so
they bind through the 127.0.0.1 hop. New copy is localized
(zh-Hant/ja, needs_review) and the DEBUG env hooks move to
docs/agents/e2e-headless.md.
The node key state was a synchronizable Keychain item, so iCloud
Keychain would hand every device the same node identity, and two
devices running one node key fight over it. It is now a device-only
item on its own service: migrateDeviceOnlyItems re-syncs every
device-local item under the secrets service, so it cannot live there.
Upstream's crypto passed a third-party audit and the TS_RS_EXPERIMENT
gate is gone, so the tunnel stops setting it. The one ABI change is
ts_config.ephemeral; the node persists its key, so it registers as a
durable (non-ephemeral) machine. Toolchains move to Rust 1.98.1 and
nightly-2026-09-17 for the xros slices. The build script checks the
header after the first cargo build, because ts_ffi's build.rs generates
it and a fresh clone has none. The ts_netmon patch still applies and is
still needed.
@jhen0409 jhen0409 changed the title Connect via Tailscale over tailscale-rs (all three platforms, incl. visionOS) Connect via Tailscale over tailscale-rs (all three platforms, incl. visionOS), experimental Sep 30, 2026
# Conflicts:
#	Multiplex/Views/Settings/SettingsView.swift
#	MultiplexTests/SettingsUIKitTests.swift
@jhen0409
jhen0409 marked this pull request as ready for review October 6, 2026 13:06
# Conflicts:
#	MultiplexTests/SettingsUIKitTests.swift
#	fastlane/testflight-whats-new.txt
The ts_netmon cfg patch landed upstream in v0.7.0, so the pin is now
unpatched and Vendor/tailscale-rs/patches is gone (the CI cache key
drops it too). Upstream moved its crates into category directories;
the build script reads the generated header from crates/bindings/ts_ffi.
The C ABI itself is unchanged: the vendored tailscale.h still matches.

A rejected auth key behaves differently at this pin. ts_init now
returns a device in about 2 s, and ts_ipv4_addr then waits for a netmap
that never arrives (measured with an invalid key against a macOS build
of the archive). The address read ran outside the 30 s startup
deadline, so the node would have sat in STARTING for good. The deadline
now covers ts_init and the address read together.

MULTIPLEX_SEED_TAILSCALE=0 turns the seeded host's flag back off: it
persists on the simulator's host and seed.json never reset it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant