Skip to main content

ts_runtime/
lib.rs

1#![doc = include_str!("../README.md")]
2
3extern crate ts_netstack_smoltcp as netstack;
4
5use core::time::Duration;
6use std::sync::Arc;
7
8use kameo::{
9    actor::{ActorRef, Spawn, WeakActorRef},
10    mailbox::Signal,
11};
12use netstack::netcore::Channel;
13use tokio::sync::watch;
14
15use crate::{
16    control_runner::ControlRunner, dataplane::DataplaneActor, direct::DirectManager,
17    forwarder_actor::ForwarderActor, multiderp::Multiderp, netstack_actor::NetstackActor,
18};
19
20/// Pcap stream framer for debug packet capture (`CapturePcap`).
21pub mod capture;
22/// Control runner.
23pub mod control_runner;
24mod dataplane;
25mod derp_latency;
26/// Device connection-state tracking ([`DeviceState`]) and typed registration outcome
27/// ([`RegistrationError`]).
28pub mod device_state;
29mod direct;
30mod env;
31mod error;
32/// Exit-node suggestion algorithm (the classic DERP-region-latency path, Go
33/// `suggestExitNodeUsingDERP`) and its result/error types.
34pub mod exit_node_suggest;
35/// Fallback TCP handler registry (`tsnet.Server.RegisterFallbackTCPHandler` parity).
36pub mod fallback_tcp;
37mod forwarder_actor;
38/// Client-side Funnel ingress termination (`tsnet`'s `ListenFunnel` data path).
39pub mod funnel;
40/// Unified IPN notification bus ([`Notify`] / [`watch_ipn_bus`](Runtime::watch_ipn_bus)), mirroring
41/// Go `ipn` `LocalBackend.WatchNotifications` / the `WatchIPNBus` LocalAPI.
42pub mod ipn_bus;
43mod magic_dns;
44pub use magic_dns::DnsQueryResult;
45mod multiderp;
46/// OS network-link-change supervisor (opt-in `network-monitor` feature): re-binds + re-probes
47/// connectivity on a link change. Compiled out entirely when the feature is off.
48#[cfg(feature = "network-monitor")]
49mod netmon;
50mod netstack_actor;
51mod packetfilter;
52pub mod peer_tracker;
53mod peerapi;
54mod peerapi_doh;
55mod route_updater;
56/// Stored Serve config + accept-loop runtime (`tsnet`'s `Get/SetServeConfig` + serving runtime).
57pub mod serve;
58mod src_filter;
59/// Netmap status snapshot, WhoIs, and watcher types.
60pub mod status;
61/// Taildrop peer-to-peer file transfer store.
62pub mod taildrop;
63pub mod taildrop_send;
64/// Tailnet-Lock (TKA) chain-sync orchestration: bootstrap + offer/send driver (the runtime layer
65/// that bridges the `ts_control` sync RPCs and the `ts_tka` chain logic).
66mod tka_sync;
67#[cfg(feature = "tun")]
68mod tun_actor;
69
70pub use device_state::{DeviceState, RegistrationError};
71pub(crate) use env::Env;
72pub use error::{Error, ErrorKind};
73pub use exit_node_suggest::{ExitNodeSuggestion, SuggestExitNodeError};
74pub use ipn_bus::{IpnBusWatcher, Notify, NotifyWatchOpt};
75pub use status::{FileTarget, NetcheckReport, RegionLatency, Status, StatusNode, WhoIs};
76pub use ts_dataplane::{CaptureHook, CapturePath};
77
78use crate::peer_tracker::PeerTracker;
79
80/// The runtime for a tailscale device.
81pub struct Runtime {
82    /// Reference to the control actor.
83    pub control: ActorRef<ControlRunner>,
84    dataplane: ActorRef<DataplaneActor>,
85    /// Reference to the direct (disco/UDP underlay) manager, retained so [`Runtime::rebind`] can
86    /// ask it to re-bind the underlay socket on a network/link change.
87    direct: ActorRef<DirectManager>,
88    /// Reference to the application netstack actor. `None` in TUN transport mode, where there is
89    /// no userspace application netstack (the application data path is a real kernel TUN device).
90    netstack: Option<WeakActorRef<NetstackActor>>,
91    /// Reference to the peer tracker for peer lookups.
92    pub peer_tracker: WeakActorRef<PeerTracker>,
93    /// Fallback TCP handler registry, bound to the application netstack. `None` in TUN transport
94    /// mode (no application netstack exists to attach it to).
95    fallback_tcp: Option<fallback_tcp::FallbackTcpManager>,
96    /// Reference to the MagicDNS responder, retained so [`Runtime::query_dns`] can run a query
97    /// through the live `100.100.100.100` forward path. `None` in TUN transport mode (no
98    /// `MagicDnsActor` is spawned there — TUN-mode MagicDNS is an in-packet intercept, not an actor).
99    magic_dns: Option<ActorRef<magic_dns::MagicDnsActor>>,
100    /// Reference to the forwarder actor, retained so [`Runtime::set_advertise_routes`] can push a
101    /// new accept/dial route table onto the running forwarder (the local half of advertising
102    /// routes). Without this the strong ref would drop after the startup `GetChannel` and the
103    /// forwarder would be reachable only via the message bus.
104    forwarder: ActorRef<ForwarderActor>,
105    /// Reference to the multiderp manager, retained so [`Runtime::status`] can resolve each
106    /// relayed peer's DERP region id to its region **code** (`ipnstate.PeerStatus.Relay`). Without
107    /// this the strong ref would drop after startup (it is cloned into the direct manager + route
108    /// updater) and the region-code map would be unreachable.
109    multiderp: ActorRef<Multiderp>,
110    env: Env,
111    shutdown: watch::Sender<bool>,
112    /// Sender side of the exit-node selector `watch` cell. Held privately here (not on the cloned
113    /// `Env`, which keeps only the read side) so that only `Runtime::set_exit_node` can mutate the
114    /// selection; the route updater and source filter re-read it via [`Env::exit_node`].
115    exit_node_tx: watch::Sender<Option<ts_control::ExitNodeSelector>>,
116    /// Sender side of the accept-routes preference `watch` cell. Held privately here (same rationale
117    /// as [`exit_node_tx`](Self::exit_node_tx)) so that only [`Runtime::set_accept_routes`] can
118    /// toggle it; the route updater and source filter re-read it via [`Env::accept_routes`].
119    accept_routes_tx: watch::Sender<bool>,
120    /// Sender side of the accept-dns preference `watch` cell. Held privately here (same rationale as
121    /// [`accept_routes_tx`](Self::accept_routes_tx)) so that only [`Runtime::set_accept_dns`] can
122    /// toggle it; the MagicDNS responder re-reads it via [`Env::accept_dns`] when it rebuilds its
123    /// view (the republish that `set_accept_dns` triggers).
124    accept_dns_tx: watch::Sender<bool>,
125    /// Receiver mirroring the *active* (resolved + fail-closed) exit node's stable id, fed by the
126    /// route updater. Read by [`Runtime::status`] / [`Runtime::active_exit_node`] to report which
127    /// exit node traffic is actually egressing through (vs. the merely-configured selector).
128    active_exit_rx: watch::Receiver<Option<ts_control::StableNodeId>>,
129    /// Receiver for the device connection-state cell, fed by the control runner. Read by
130    /// [`Runtime::watch_state`] and [`Runtime::wait_until_running`].
131    state_rx: watch::Receiver<DeviceState>,
132    /// Receiver for the retained peer-capability grants, fed by the packet-filter updater. Read by
133    /// [`Runtime::whois`] to resolve the flow-scoped cap map (Go `apitype.WhoIsResponse.CapMap`).
134    cap_grants_rx: watch::Receiver<packetfilter::CapGrants>,
135    /// Live advertised-route preference (explicit subnet routes + the exit-node flag), seeded from
136    /// the startup config. [`Runtime::set_advertise_routes`] and [`set_advertise_exit_node`] each
137    /// mutate their part under this lock then re-send the composed set, so the two compose.
138    advertise: std::sync::Mutex<AdvertiseState>,
139    /// The most recent exit-node suggestion's stable id (Go `LocalBackend.lastSuggestedExitNode`),
140    /// remembered across calls so [`Runtime::suggest_exit_node`] can apply *stickiness* — if the
141    /// previously-suggested node is still an eligible candidate in the winning region it is kept, to
142    /// avoid the suggestion flapping between equally-good ties on each call. `None` until the first
143    /// suggestion. Held behind a `Mutex` (same rationale as [`advertise`](Self::advertise): a
144    /// cheap, infrequently-touched bit of mutable runtime state, not worth an actor round-trip).
145    prev_suggestion: std::sync::Mutex<Option<ts_control::StableNodeId>>,
146    /// Background task that periodically reaps abandoned taildrop `.partial` files (Go
147    /// `feature/taildrop/delete.go` `fileDeleter`). `None` when no taildrop store is configured.
148    /// Aborted on [`Drop`] so it cannot outlive the runtime (the `reauth_bridge` pattern).
149    taildrop_reaper: Option<tokio::task::JoinHandle<()>>,
150    /// The opt-in OS network-link-change supervisor (`network-monitor` feature + the
151    /// `Config::network_monitor` flag). Retained so the actor — and thus the
152    /// `LinkMonitorHandle` it holds, which aborts the monitor's watcher task on drop — lives for the
153    /// device's life and is torn down when the runtime drops. `None` when the flag is off. Only
154    /// present when built with the `network-monitor` feature.
155    #[cfg(feature = "network-monitor")]
156    #[allow(dead_code)]
157    netmon_supervisor: Option<ActorRef<netmon::NetmonSupervisor>>,
158}
159
160impl Runtime {
161    /// Spawn a new runtime with the given parameters for connecting to a tailnet.
162    pub async fn spawn(
163        config: ts_control::Config,
164        auth_key: Option<String>,
165        keys: ts_keys::NodeState,
166    ) -> Result<Self, Error> {
167        let (shutdown_tx, shutdown_rx) = watch::channel(false);
168
169        // The exit-node selector, accept-routes, and accept-dns preferences are live `watch` cells so
170        // `Device::set_exit_node` / `set_accept_routes` / `set_accept_dns` can change them at runtime.
171        // `new_with_runtime_txs` returns each `Sender` (mutation capability) grouped in `pref_cells`
172        // so they are retained privately on the `Runtime`, while only the `Receiver`s (the readers'
173        // contract) live on the cloned `Env`. Initial values come from `ForwarderConfig`.
174        let (env, pref_cells) = Env::new_with_runtime_txs(
175            keys,
176            shutdown_rx,
177            env::ForwarderConfig::from_control_config(&config),
178        );
179
180        // Both userspace netstacks (application + forwarder) share one netstack config. Honor the
181        // per-deployment TCP buffer knob, and set the netstack MTU to the overlay/tunnel MTU so the
182        // advertised MSS fits the tunnel — leaving it at the netstack's generic 1500 default would
183        // emit over-1280 segments into the WireGuard path. The MTU comes from a `Tun` transport's
184        // `TunConfig` when one is configured (so the netstack and the TUN agree), else the 1280
185        // overlay default (the `Netstack` userspace mode — the common case — has no per-OS MTU knob,
186        // but the tailnet overlay MTU is still 1280).
187        let configured_mtu = match &config.transport_mode {
188            ts_control::TransportMode::Tun(tun_cfg) => tun_cfg.mtu,
189            ts_control::TransportMode::Netstack => None,
190        };
191        let netstack_config = netstack_config_from(config.tcp_buffer_size, configured_mtu);
192
193        let dataplane = DataplaneActor::spawn(env.clone());
194
195        let (netstack_id, netstack_up, netstack_down) =
196            dataplane.ask(dataplane::NewOverlayTransport).await?;
197
198        // A second overlay transport feeds the dedicated any-IP forwarder netstack. Inbound packets
199        // for advertised subnet routes / the exit-node default route are routed here (see
200        // `route_updater`), keeping forwarded flows off the application netstack.
201        let (forwarder_id, forwarder_up, forwarder_down) =
202            dataplane.ask(dataplane::NewOverlayTransport).await?;
203
204        // The selected DERP home region (Go `report.PreferredDERP`): the control runner is the sole
205        // writer (it applies the netcheck `bestRecent` + hysteresis smoothing), and `Multiderp`
206        // reads it to drive the local home relay — so the relay follows the SAME smoothed home the
207        // runner advertises to control, instead of picking it from the raw per-cycle latency minimum
208        // (which flapped on jitter and could disagree with the advertised home). Created here so it
209        // outlives both actors; `None` until the first home is chosen.
210        let (home_region_tx, home_region_rx) = watch::channel::<Option<ts_derp::RegionId>>(None);
211
212        let multiderp = Multiderp::spawn((env.clone(), dataplane.clone(), home_region_rx));
213
214        // Spawn the direct (disco) underlay manager before the route updater. Its `on_start`
215        // binds the UDP socket and registers its transport synchronously, so by the time the
216        // route updater asks it for the direct transport id it is guaranteed to be available.
217        let direct = DirectManager::spawn((env.clone(), dataplane.clone(), multiderp.clone()));
218
219        // Spawn the forwarder before the route updater. Its `on_start` builds the forwarder
220        // netstack, enables any-IP acceptance, and starts the per-port accept loops synchronously,
221        // so by the time the route updater begins delivering advertised prefixes to
222        // `forwarder_id` the netstack is already draining its transport.
223        let forwarder = ForwarderActor::spawn((
224            env.clone(),
225            netstack_config.clone(),
226            forwarder_up,
227            forwarder_down,
228        ));
229        // Force `on_start` to finish (any-IP enabled, accept loops live) before the route updater
230        // can route the first inbound flow to `forwarder_id`: an `ask` blocks until the actor has
231        // started.
232        //
233        // The forwarder netstack's overlay `Channel` is reused by the TUN application path for
234        // recursive / exit-node-DoH MagicDNS forwarding (TUN mode has no application netstack of its
235        // own, but the forwarder netstack runs in both modes and egresses over the overlay — the
236        // anti-leak property `forward_query`/`forward_doh` require). Only the `tun` Tun arm consumes
237        // it, so it is unused when the `tun` feature is off — allow that without warn-as-error.
238        #[cfg_attr(not(feature = "tun"), allow(unused_variables))]
239        let (forwarder_channel,) = forwarder.ask(forwarder_actor::GetChannel).await?;
240
241        // The route updater is the single authoritative resolver of the active (resolved,
242        // fail-closed) exit node; it publishes the resolved stable id into this watch cell so
243        // `Runtime::status` can report which exit is actually engaged (not just configured).
244        let (active_exit_tx, active_exit_rx) = watch::channel(None);
245        route_updater::RouteUpdater::spawn((
246            multiderp.clone(),
247            direct.clone(),
248            env.clone(),
249            netstack_id,
250            forwarder_id,
251            active_exit_tx,
252        ));
253        // The packet-filter updater also surfaces the retained cap-grants (for flow-scoped WhoIs)
254        // through a `watch` cell whose receiver the `Runtime` holds — the bus has no replay, so a
255        // `watch` is how `Runtime::whois` reads the current grants on demand.
256        let (cap_grants_tx, cap_grants_rx) = watch::channel(Default::default());
257        packetfilter::PacketfilterUpdater::spawn((env.clone(), cap_grants_tx));
258        src_filter::SourceFilterUpdater::spawn(env.clone());
259        // TKA enforcement-authority cell (Go `tkaFilterNetmapLocked`). Created here — before both
260        // actors spawn — so the control runner (sole writer, `Sender`) and the peer tracker (reader,
261        // `Receiver`) share one `watch` cell. A `watch` (not a bus message) is the transport for this
262        // security-critical state: last-write-wins, never dropped under load, ordered by the control
263        // runner's writes, so a disable (`None`) can never be reordered behind or dropped before a
264        // stale `Some`. `None` = no lock synced / disabled (admit all).
265        let (tka_authority_tx, tka_authority_rx) =
266            watch::channel::<Option<std::sync::Arc<ts_tka::Authority>>>(None);
267        let peer_tracker = PeerTracker::spawn((env.clone(), tka_authority_rx)).downgrade();
268
269        // Select the application data path from the transport mode. The forwarder/egress path
270        // above is UNCHANGED in both modes — TUN mode only swaps the application data path, never
271        // the forwarder. `config` is moved into `ControlRunner::spawn` below, so branch on a
272        // borrow and clone the small `TunConfig` where needed before the move.
273        //
274        // - Netstack (the default, and the only reachable arm when the `tun` feature is off):
275        //   spawn the application netstack + MagicDNS responder + fallback-TCP registry, all on
276        //   the `netstack_up`/`netstack_down` overlay seam.
277        // - Tun: spawn `TunActor` on that same overlay seam instead; no application netstack and
278        //   no MagicDNS responder exist, and `netstack`/`fallback_tcp` are `None`.
279        // - Tun requested but built without the `tun` feature: hard-error (a config/build
280        //   mismatch knowable at spawn time). NEVER silently fall back to netstack.
281        let (netstack, fallback_tcp, magic_dns) = match &config.transport_mode {
282            ts_control::TransportMode::Netstack => {
283                let netstack = NetstackActor::spawn((
284                    env.clone(),
285                    netstack_config,
286                    netstack_up,
287                    netstack_down,
288                ));
289
290                // Fetch the netstack channel while we still hold the strong ActorRef, then spawn
291                // the MagicDNS responder on it. Its ActorRef is retained on `Runtime` so
292                // `query_dns` can drive the live forward path; the serve loop itself is owned by the
293                // actor's internal JoinSet.
294                let (channel,) = netstack.ask(netstack_actor::GetChannel).await?;
295                // The fallback-TCP registry attaches to the application netstack — the same one
296                // that carries the embedder's explicit `Device::tcp_listen` sockets — so a
297                // fallback handler sees exactly the inbound flows no explicit listener matched.
298                let fallback_tcp = fallback_tcp::FallbackTcpManager::new(channel.clone());
299                let magic_dns = magic_dns::MagicDnsActor::spawn((env.clone(), channel));
300
301                (
302                    Some(netstack.downgrade()),
303                    Some(fallback_tcp),
304                    Some(magic_dns),
305                )
306            }
307
308            #[cfg(feature = "tun")]
309            ts_control::TransportMode::Tun(tun_cfg) => {
310                // Reuse the same `netstack_up`/`netstack_down` overlay-transport pair that would
311                // have fed the netstack — it is just the application-side overlay seam (the name
312                // is historical). No NetstackActor / MagicDnsActor is spawned.
313                tun_actor::TunActor::spawn((
314                    env.clone(),
315                    tun_cfg.clone(),
316                    netstack_up,
317                    netstack_down,
318                    // Reuse the forwarder netstack's overlay `Channel` for recursive / exit-node-DoH
319                    // MagicDNS forwarding in the TUN datapath (TUN mode has no application netstack
320                    // Channel of its own). Egresses over the overlay — anti-leak preserved.
321                    //
322                    // Host-route gating (subnet routes gated on `--accept-routes`, the host `/0` from
323                    // the selected exit peer) is no longer snapshotted here: `TunActor` reads the live
324                    // `Env` cells (`accept_routes`/`exit_node`) on every host-FIB apply — both the
325                    // device-build path and the `PeerState` re-apply path — and folds the union of
326                    // peers' AllowedIPs (see `tun_actor::host_routes_from_node`). A runtime
327                    // `set_accept_routes` / `set_exit_node` toggle re-broadcasts the peer state, so the
328                    // host routing table is re-steered live (no device rebuild needed).
329                    forwarder_channel.clone(),
330                ));
331
332                (None, None, None)
333            }
334
335            #[cfg(not(feature = "tun"))]
336            ts_control::TransportMode::Tun(_) => {
337                return Err(Error {
338                    kind: ErrorKind::TunUnavailable,
339                    target_actor: None,
340                    message_ty: None,
341                });
342            }
343        };
344
345        // Device connection-state cell. Created here (not inside the actor) so the control runner's
346        // `on_start` can publish `Failed`/`NeedsLogin` and still return `Err` without the sender
347        // being tied to a `Self` that never gets constructed on a hard registration failure.
348        let (state_tx, state_rx) = watch::channel(DeviceState::Connecting);
349
350        // Seed the live advertised-route preference from the startup config before `config` moves
351        // into the control runner, so the runtime setters compose against the configured baseline.
352        let advertise = std::sync::Mutex::new(AdvertiseState {
353            routes: config.advertise_routes.clone(),
354            exit_node: config.advertise_exit_node,
355        });
356
357        // Unbounded mailbox (not the default bounded-64): the control runner SELF-messages — a
358        // spawned TKA sync task delivers its result back via `self_ref.tell(TkaSynced)`, and the
359        // netmap stream pump tells `StreamMessage::Next` onto the same mailbox. The stall path: the
360        // netmap handler ends by parking on `env.publish().await` into the bounded-64 *bus* (a slow
361        // bus subscriber, e.g. a busy TKA-enforcing peer tracker, holds the bus full); while it is
362        // parked, a concurrently-finishing sync task's `TkaSynced` self-tell queues behind a full
363        // *ControlRunner* mailbox and blocks waiting for capacity, delaying the verified-authority
364        // (or lock-disable) write to the enforcement cell — i.e. stale TKA enforcement under churn.
365        // kameo gates its self-tell deadlock warning on `is_current()`, which is false for the
366        // detached sync task, so the stall is silent. An unbounded mailbox lets the self-tell and the
367        // stream pump enqueue without ever awaiting capacity (kameo's documented choice for a
368        // self-messaging actor); the runner's inputs are control-paced (the netmap stream + a few RPC
369        // replies; the bus delivers best-effort and never backpressures this mailbox), not an attacker
370        // flood, so unbounded growth is not a practical exposure.
371        let control = ControlRunner::spawn_with_mailbox(
372            control_runner::Params {
373                config,
374                auth_key,
375                env: env.clone(),
376                state_tx,
377                tka_authority: tka_authority_tx,
378                home_region: home_region_tx,
379            },
380            kameo::mailbox::unbounded(),
381        );
382
383        // Spawn the taildrop partial-reaper if a store is configured; it sweeps abandoned `.partial`
384        // files every `DELETE_DELAY` and exits on shutdown (the handle is aborted in `Drop`).
385        let taildrop_reaper = env.taildrop_store.as_ref().map(|store| {
386            crate::taildrop::spawn_partial_reaper(store.clone(), shutdown_tx.subscribe())
387        });
388
389        // Opt-in OS network-link monitor (`Config::network_monitor`, default off). When enabled it
390        // spawns a `NetmonSupervisor` that, on a coalesced link change, asks the direct manager to
391        // rebind + re-probe and republishes `MeasureNow` for a re-netcheck — the auto-recovery a
392        // real `tailscaled` performs and the engine otherwise leaves to the embedder. When the flag
393        // is off this is a complete no-op: zero extra threads/sockets, byte-for-byte today's
394        // behavior. The manual `Device::rebind` path is unchanged either way.
395        //
396        // Feature gating is strict and never silent: with the `network-monitor` feature ON the
397        // supervisor (and its `ts_netmon` dep) compile in and spawn when the flag is set; with the
398        // feature OFF, setting the flag is a HARD error at spawn (mirrors the `TransportMode::Tun`
399        // without-`tun`-feature error above), so a build that cannot honor the request fails loudly
400        // rather than booting a node that silently won't auto-recover.
401        #[cfg(feature = "network-monitor")]
402        let netmon_supervisor = if env.network_monitor {
403            // Slice (a): no OS event-source backend is wired yet (the Linux netlink / macOS
404            // PF_ROUTE backends are later slices), so the supervisor runs against a `NoopLinkMonitor`
405            // — it is live and correctly shaped (it will react the moment a real backend feeds it),
406            // it just never sees a synthetic/OS event in this build. Production end-to-end reaction
407            // is proven in the integration test via a `ManualLinkMonitor`.
408            let monitor: std::sync::Arc<dyn ts_netmon::LinkMonitor> =
409                std::sync::Arc::new(ts_netmon::NoopLinkMonitor);
410            Some(netmon::NetmonSupervisor::spawn(
411                netmon::NetmonSupervisorArgs {
412                    monitor,
413                    direct: direct.clone(),
414                    env: env.clone(),
415                },
416            ))
417        } else {
418            None
419        };
420
421        #[cfg(not(feature = "network-monitor"))]
422        if env.network_monitor {
423            // The flag is set but this build cannot honor it. Fail loudly (never a silent no-op).
424            return Err(Error {
425                kind: ErrorKind::NetworkMonitorUnavailable,
426                target_actor: None,
427                message_ty: None,
428            });
429        }
430
431        Ok(Self {
432            control,
433            dataplane,
434            direct,
435            peer_tracker,
436            fallback_tcp,
437            magic_dns,
438            forwarder,
439            multiderp,
440            netstack,
441            env,
442            shutdown: shutdown_tx,
443            exit_node_tx: pref_cells.exit_node,
444            accept_routes_tx: pref_cells.accept_routes,
445            accept_dns_tx: pref_cells.accept_dns,
446            active_exit_rx,
447            state_rx,
448            cap_grants_rx,
449            advertise,
450            prev_suggestion: std::sync::Mutex::new(None),
451            taildrop_reaper,
452            #[cfg(feature = "network-monitor")]
453            netmon_supervisor,
454        })
455    }
456
457    /// Register a fallback TCP handler consulted for every inbound TCP flow that matches no
458    /// explicit listener (`tsnet.Server.RegisterFallbackTCPHandler` parity).
459    ///
460    /// The returned [`fallback_tcp::FallbackTcpHandle`] deregisters the handler when dropped. See
461    /// [`fallback_tcp`] for the dispatch contract and anti-leak guarantees.
462    ///
463    /// Returns [`ErrorKind::UnsupportedInTunMode`] in TUN transport mode, where there is no
464    /// application netstack to attach a fallback handler to.
465    pub fn register_fallback_tcp_handler(
466        &self,
467        cb: Arc<
468            dyn Fn(core::net::SocketAddr, core::net::SocketAddr) -> fallback_tcp::FallbackDecision
469                + Send
470                + Sync,
471        >,
472    ) -> Result<fallback_tcp::FallbackTcpHandle, Error> {
473        Ok(self
474            .fallback_tcp
475            .as_ref()
476            .ok_or(Error {
477                kind: ErrorKind::UnsupportedInTunMode,
478                target_actor: None,
479                message_ty: None,
480            })?
481            .register(cb))
482    }
483
484    /// Get a channel to send commands to the netstack.
485    ///
486    /// Returns [`ErrorKind::UnsupportedInTunMode`] in TUN transport mode, where there is no
487    /// application netstack.
488    pub async fn channel(&self) -> Result<Channel, Error> {
489        let (channel,) = self
490            .netstack
491            .as_ref()
492            .ok_or(Error {
493                kind: ErrorKind::UnsupportedInTunMode,
494                target_actor: None,
495                message_ty: None,
496            })?
497            .upgrade()
498            .ok_or(Error {
499                kind: ErrorKind::ActorGone,
500                target_actor: None,
501                message_ty: None,
502            })?
503            .ask(netstack_actor::GetChannel)
504            .await?;
505
506        Ok(channel)
507    }
508
509    /// Resolve `name` for `qtype` through the live MagicDNS responder (the `100.100.100.100`
510    /// forward path), returning the raw DNS response, its RCODE, and the upstream resolver(s)
511    /// consulted (analogue of Go `LocalClient.QueryDNS`).
512    ///
513    /// This drives the *real* responder — the same `decide`/forward logic an on-the-wire query
514    /// hits — so the answer and its anti-leak posture (a tailnet-suffix name never egresses; a
515    /// recursive forward delegates to the active exit node's DoH; only IPv4 upstreams are dialed)
516    /// match exactly what a tailnet client observes. `qtype` is the raw RFC 1035 TYPE (`1`=A,
517    /// `28`=AAAA, `12`=PTR, or any other).
518    ///
519    /// Returns [`ErrorKind::UnsupportedInTunMode`] in TUN transport mode, where MagicDNS is an
520    /// in-packet intercept on the host's own resolver rather than an actor that can be queried, and
521    /// [`ErrorKind::ActorGone`] if the responder has shut down.
522    pub async fn query_dns(
523        &self,
524        name: &str,
525        qtype: u16,
526    ) -> Result<magic_dns::DnsQueryResult, Error> {
527        let result = self
528            .magic_dns
529            .as_ref()
530            .ok_or(Error {
531                kind: ErrorKind::UnsupportedInTunMode,
532                target_actor: None,
533                message_ty: None,
534            })?
535            .ask(magic_dns::Query {
536                name: name.to_owned(),
537                qtype,
538            })
539            .await?;
540
541        Ok(result)
542    }
543
544    /// The Taildrop file store, if Taildrop is enabled (`taildrop_dir` configured and the store
545    /// initialized). `None` when disabled — fail-closed. Shared with the peerAPI Taildrop server so
546    /// the embedder's read APIs and the receive path see the same on-disk store.
547    pub fn taildrop_store(&self) -> Option<Arc<crate::taildrop::TaildropStore>> {
548        self.env.taildrop_store.clone()
549    }
550
551    /// The shared Funnel ingress slot the peerAPI `/v0/ingress` route reads per connection.
552    ///
553    /// `Device::listen_funnel` installs a [`FunnelManager`](crate::funnel::FunnelManager)'s sink here
554    /// to make the route live (the peerAPI server is already running from startup). Returns a clone of
555    /// the runtime-lifetime `Arc` so the device can write the slot without restarting the server. See
556    /// [`crate::funnel`] for the ingress data path.
557    pub fn funnel_ingress_slot(&self) -> crate::funnel::FunnelIngressSlot {
558        self.env.funnel_ingress.clone()
559    }
560
561    /// The shared "Funnel ingress listener active" flag (the same `Arc` the control session reads to
562    /// set `HostInfo.IngressEnabled`). `Device::listen_funnel` flips it `true` while a funnel listener
563    /// is up so control routes Funnel traffic to this node; clearing it advertises no live endpoint.
564    pub fn ingress_active_flag(&self) -> std::sync::Arc<std::sync::atomic::AtomicBool> {
565        self.env.ingress_active.clone()
566    }
567
568    /// Install (`Some`) or clear (`None`) the debug packet-capture hook on the running dataplane.
569    /// `Some(hook)` tees every plaintext packet crossing the datapath to `hook` until it is cleared;
570    /// `None` stops capture. Mirrors Go `tstun.Wrapper.InstallCaptureHook` / `ClearCaptureSink`.
571    pub async fn install_capture(
572        &self,
573        hook: Option<ts_dataplane::CaptureHook>,
574    ) -> Result<(), Error> {
575        self.dataplane
576            .ask(dataplane::InstallCapture { hook })
577            .await
578            .map_err(Into::into)
579    }
580
581    /// Re-bind the underlay UDP socket after a network/link change (Wi-Fi switch, sleep/wake). The
582    /// embedder's own link monitor calls this (the engine owns the socket re-bind; the embedder owns
583    /// OS netmon). Re-binds the socket (same-port-preferred, IPv4-only invariant preserved) and
584    /// resets the now-stale local NAT mapping — clearing learned reflexive addresses and every
585    /// confirmed direct path while keeping candidate endpoints, so peers re-probe over the new socket
586    /// and relay over DERP (never a direct host dial) until a path re-confirms. Peers, control, the
587    /// netmap, disco state, and DERP are untouched. A no-op when the underlay is inert (bind failed
588    /// at startup, DERP-only). Mirrors Go magicsock `Conn.Rebind` + `resetEndpointStates`.
589    pub async fn rebind(&self) -> Result<(), Error> {
590        self.direct.ask(direct::Rebind).await.map_err(Error::from)
591    }
592
593    /// Force an immediate STUN / endpoint re-probe **without** rebinding the underlay socket —
594    /// Go magicsock's `Conn.ReSTUN`. Asks the `DirectManager` to run one STUN sweep now (re-learn
595    /// our reflexive/public address) while leaving the socket, its NAT mapping, learned paths, peers,
596    /// control, and DERP untouched. Lighter than [`rebind`](Self::rebind): no socket swap, no
597    /// re-ping. A no-op when the underlay is inert (bind failed at startup, DERP-only). No control
598    /// round-trip.
599    pub async fn re_stun(&self) -> Result<(), Error> {
600        self.direct.ask(direct::ReStun).await.map_err(Error::from)
601    }
602
603    /// A snapshot of the local netmap: this node plus every known peer.
604    ///
605    /// Combines the self node held by the control runner with the peer set held by the peer
606    /// tracker. Mirrors tsnet's `LocalClient::Status`.
607    ///
608    /// `self_node` is `None` until the first netmap update has been received from control. Peer
609    /// entries carry no online/user/capability data (see the [`status`] module docs for that gap).
610    pub async fn status(&self) -> Result<Status, Error> {
611        let self_node_domain = self.control.ask(control_runner::SelfNode).await?;
612        // The MagicDNS suffix is the self node's FQDN minus its host label — already split into
613        // `Node.tailnet` at decode time (Go derives it the same way in `NetworkMap.MagicDNSSuffix`).
614        // Capture it before the domain `Node` is mapped away into a `StatusNode`.
615        let magic_dns_suffix = self_node_domain.as_ref().and_then(|n| n.tailnet.clone());
616        let self_node = self_node_domain.as_ref().map(StatusNode::from_node);
617
618        let peers_with_ids = self
619            .peer_tracker
620            .upgrade()
621            .ok_or(Error {
622                kind: ErrorKind::ActorGone,
623                target_actor: None,
624                message_ty: None,
625            })?
626            .ask(peer_tracker::GetStatus)
627            .await?;
628
629        // Join per-peer connectivity (Go `PeerStatus.CurAddr`): one batched query to the direct
630        // manager for every peer's current trusted direct endpoint, then fill `cur_addr` on each
631        // `StatusNode`. A peer absent from the map is relayed via DERP (`cur_addr = None`). This is a
632        // live snapshot — the direct path can expire/re-confirm between calls (matches Go's snapshot
633        // semantics). The `watch_netmap` stream intentionally carries no connectivity (it is a netmap
634        // watch, not a path-state watch, and does not re-fire on direct↔relay flips).
635        let ids: Vec<ts_transport::PeerId> = peers_with_ids.iter().map(|(id, _)| *id).collect();
636        let best_addrs = self
637            .direct
638            .ask(direct::BestAddrs { ids: ids.clone() })
639            .await
640            .unwrap_or_default();
641
642        // For the peers with NO direct path (relayed via DERP), resolve the region CODE they relay
643        // through (Go `PeerStatus.Relay`). One batched ask to multiderp; `cur_addr` and `relay` are
644        // mutually exclusive for a routed peer, mirroring Go's empty-vs-set strings.
645        let relay_ids: Vec<ts_transport::PeerId> = ids
646            .into_iter()
647            .filter(|id| !best_addrs.contains_key(id))
648            .collect();
649        let relay_codes = if relay_ids.is_empty() {
650            Default::default()
651        } else {
652            self.multiderp
653                .ask(multiderp::RelayCodesForPeers { ids: relay_ids })
654                .await
655                .unwrap_or_default()
656        };
657
658        let peers = peers_with_ids
659            .into_iter()
660            .map(|(id, mut node)| match best_addrs.get(&id).copied() {
661                Some(addr) => {
662                    node.cur_addr = Some(addr);
663                    node
664                }
665                None => {
666                    node.relay = relay_codes.get(&id).cloned();
667                    node
668                }
669            })
670            .collect();
671
672        Ok(Status {
673            self_node,
674            peers,
675            active_exit_node: self.active_exit_node(),
676            magic_dns_suffix,
677        })
678    }
679
680    /// Suggest a reasonably good exit node to use, from the current netmap + latest netcheck report
681    /// (Go `LocalBackend.SuggestExitNode`). The Phase-1 classic DERP-region-latency path; see the
682    /// [`exit_node_suggest`] module for the algorithm, scope, and the IPv4-only parity deviation.
683    ///
684    /// Gathers the inputs the way [`status`](Self::status) and [`file_targets`](Self::file_targets)
685    /// do — the latest [`NetcheckReport`] from the control runner (an immediate, non-blocking borrow
686    /// of the last DERP-latency measurement) and every peer [`Node`](ts_control::Node) from the peer
687    /// tracker — then runs the pure algorithm with the production uniform-random selectors
688    /// (`random_region` / `random_node`). The returned suggestion's id is remembered in the runtime's
689    /// `prev_suggestion` cell so the next call is *sticky* (Go `lastSuggestedExitNode`).
690    ///
691    /// Returns `Ok(None)` when no peer is an eligible candidate (Go's empty response), and
692    /// `Err(`[`SuggestExitNodeError::NoPreferredDerp`]`)` when there is no netcheck report yet (Go's
693    /// `ErrNoPreferredDERP`, "try again later").
694    pub async fn suggest_exit_node(
695        &self,
696    ) -> Result<Result<Option<ExitNodeSuggestion>, SuggestExitNodeError>, Error> {
697        use ts_control::NODE_ATTR_SUGGEST_EXIT_NODE;
698
699        // The latest netcheck report (Go `MagicConn().GetLastNetcheckReport`): an immediate borrow of
700        // the control runner's published measurement (the same value `Device::netcheck` surfaces).
701        let report = self.control.ask(control_runner::Netcheck).await?;
702
703        // Every known peer (Go reads the netmap peers via `AppendMatchingPeers`); the domain `Node`
704        // retains the cap map, home DERP region, online state, and accepted routes the predicate
705        // needs.
706        let peers = self
707            .peer_tracker
708            .upgrade()
709            .ok_or(Error {
710                kind: ErrorKind::ActorGone,
711                target_actor: None,
712                message_ty: None,
713            })?
714            .ask(peer_tracker::AllPeers)
715            .await?;
716
717        // Project each peer into the algorithm's candidate inputs. The eligibility predicate runs
718        // inside the pure function, so every peer is passed (self is naturally absent from the peer
719        // set). `derp_region`/`online`/`cap_map`/`accepted_routes` map straight off the domain node;
720        // the exit-route check is the fork's family-agnostic `prefix_len == 0` (IPv4-only parity —
721        // see `exit_node_suggest::suggest_exit_node`).
722        let candidates: Vec<exit_node_suggest::ExitNodeCandidate> = peers
723            .iter()
724            .map(|peer| exit_node_suggest::ExitNodeCandidate {
725                stable_id: peer.stable_id.clone(),
726                name: peer
727                    .fqdn_opt(false)
728                    .unwrap_or_else(|| peer.hostname.clone()),
729                derp_region: peer.derp_region,
730                online: peer.online,
731                advertises_exit_route: peer
732                    .accepted_routes
733                    .iter()
734                    .any(|route| route.prefix_len() == 0),
735                has_suggest_cap: peer.has_node_attr(NODE_ATTR_SUGGEST_EXIT_NODE),
736            })
737            .collect();
738
739        // Read the sticky previous suggestion, run the pure algorithm with the production
740        // uniform-random selectors, then update the sticky value to the new result. This mirrors Go
741        // `suggestExitNodeLocked` (`ipn/ipnlocal/local.go`), which assigns `b.lastSuggestedExitNode =
742        // res.ID` on **every** no-error return — INCLUDING the empty/no-candidate result, where it
743        // clears the sticky id to "". So a successful suggestion sets stickiness, an empty result
744        // CLEARS it (a peer that dropped out of candidacy stops being preferred), and only an `Err`
745        // (`NoPreferredDerp` — no netcheck yet) returns before the assignment and leaves it untouched.
746        let prev = self.prev_suggestion.lock().unwrap().clone();
747        let outcome = exit_node_suggest::suggest_exit_node(
748            &report,
749            &candidates,
750            prev.as_ref(),
751            &exit_node_suggest::random_region,
752            &exit_node_suggest::random_node,
753        );
754        *self.prev_suggestion.lock().unwrap() = exit_node_suggest::next_sticky(prev, &outcome);
755        Ok(outcome)
756    }
757
758    /// List the tailnet peers this node can Taildrop a file *to* (Go LocalAPI `FileTargets`).
759    ///
760    /// Mirrors the upstream send-path filter (`feature/taildrop` `Extension::FileTargets`): a peer
761    /// qualifies when it advertises a reachable peerAPI **and** is either owned by the same user as
762    /// this node **or** explicitly granted the file-sharing-target capability. The whole list is
763    /// gated on this node holding the file-sharing capability (control sets it when the admin enables
764    /// Taildrop) — absent that, an empty list (fail-closed, not an error, matching how the receive
765    /// store returns empty when disabled). Results are sorted by the peer's MagicDNS name.
766    ///
767    /// Targets are listed regardless of current online state (upstream's `FileTargets` does not gate
768    /// on online either; an offline target's send will simply time out). The self node is never
769    /// included. Returns empty before the first netmap.
770    ///
771    /// Divergence from Go: the upstream filter also excludes `tvOS` peers, which this fork cannot
772    /// reproduce (the domain node carries no OS string); the impact is negligible — the actual send
773    /// fail-closes if such a peer refused the transfer.
774    pub async fn file_targets(&self) -> Result<Vec<FileTarget>, Error> {
775        // Node-level gate: this node must hold the file-sharing capability (Taildrop enabled by the
776        // admin). Read it off the self node's cap map, like Go's `hasCapFileSharing()`.
777        let self_node = self.control.ask(control_runner::SelfNode).await?;
778        let Some(self_node) = self_node else {
779            return Ok(Vec::new()); // no netmap yet
780        };
781        if !self_node.can_share_files() {
782            return Ok(Vec::new()); // Taildrop not enabled for the tailnet — fail-closed
783        }
784        let self_user_id = self_node.user_id;
785
786        let peers = self
787            .peer_tracker
788            .upgrade()
789            .ok_or(Error {
790                kind: ErrorKind::ActorGone,
791                target_actor: None,
792                message_ty: None,
793            })?
794            .ask(peer_tracker::AllPeers)
795            .await?;
796
797        // Eligibility + ordering live in `build_file_targets` (pure, unit-tested in `status`).
798        Ok(status::build_file_targets(peers, self_user_id))
799    }
800
801    /// The stable id of the exit node traffic is currently egressing through, or `None` if none is
802    /// engaged. This is the route updater's resolved + fail-closed answer (see
803    /// [`Status::active_exit_node`](crate::status::Status::active_exit_node)): it differs from the
804    /// configured [`exit_node`](Self::exit_node) selector, which may name a peer that is absent or
805    /// no longer advertising a default route (in which case egress is dropped and this returns
806    /// `None`).
807    pub fn active_exit_node(&self) -> Option<ts_control::StableNodeId> {
808        self.active_exit_rx.borrow().clone()
809    }
810
811    /// Request an OIDC ID token from control scoped to `audience` (workload-identity federation).
812    ///
813    /// Returns the signed JWT, or the token RPC's own [`ts_control::IdTokenError`]. The kameo
814    /// delegated-reply send error is flattened: a handler error carries the real `IdTokenError`,
815    /// any other send failure (actor shutdown / mailbox closed) is surfaced as
816    /// [`ts_control::IdTokenError::NetworkError`].
817    pub async fn fetch_id_token(
818        &self,
819        audience: String,
820    ) -> Result<String, ts_control::IdTokenError> {
821        self.control
822            .ask(control_runner::FetchIdToken { audience })
823            .await
824            .map_err(flatten_send_err)
825    }
826
827    /// Log this node out of the tailnet: deregister it by expiring its current node key.
828    ///
829    /// Forwards to the control runner, which re-POSTs `/machine/register` with a past expiry over a
830    /// fresh Noise channel. This is a control-plane state change only — it does NOT shut the runtime
831    /// down (the caller follows with [`graceful_shutdown`](Self::graceful_shutdown)) and does not
832    /// touch the on-disk node key. The kameo delegated-reply send error is flattened the same way as
833    /// `fetch_id_token`: a handler error carries the real
834    /// [`ts_control::LogoutError`]; any other send failure (actor shutdown / mailbox closed) is
835    /// surfaced as [`ts_control::LogoutError::NetworkError`].
836    pub async fn logout(&self) -> Result<(), ts_control::LogoutError> {
837        self.control
838            .ask(control_runner::Logout)
839            .await
840            .map_err(flatten_logout_send_err)
841    }
842
843    /// Publish a `TXT` DNS record for this node via control's `/machine/set-dns` (Go
844    /// `LocalClient.SetDNS`).
845    ///
846    /// Forwards to the control runner, which POSTs the record over a fresh Noise channel. The kameo
847    /// delegated-reply send error is flattened the same way as `fetch_id_token`:
848    /// a handler error carries the real [`ts_control::SetDnsError`]; any other send failure (actor
849    /// shutdown / mailbox closed) is surfaced as [`ts_control::SetDnsError::NetworkError`].
850    pub async fn set_dns(
851        &self,
852        name: String,
853        value: String,
854    ) -> Result<(), ts_control::SetDnsError> {
855        self.control
856            .ask(control_runner::SetDns { name, value })
857            .await
858            .map_err(flatten_set_dns_send_err)
859    }
860
861    /// Sign `node_key` with this node's network-lock key and submit the signature to control
862    /// (Go `tka.sign` Direct case → `/machine/tka/sign`).
863    ///
864    /// Submits only — the local [`Authority`](ts_tka::Authority) is **not** mutated here; it advances
865    /// via the existing verified-sync path. A handler error carries the real [`ts_control::TkaSyncError`];
866    /// any other send failure (actor shutdown / mailbox closed) is surfaced as
867    /// [`ts_control::TkaSyncError::NetworkError`].
868    pub async fn tka_sign(&self, node_key: [u8; 32]) -> Result<(), ts_control::TkaSyncError> {
869        self.control
870            .ask(control_runner::TkaSign { node_key })
871            .await
872            .map_err(flatten_tka_send_err)
873    }
874
875    /// Disable Tailnet Lock by presenting the `disablement_secret` to control (Go `tka.disable` →
876    /// `/machine/tka/disable`), targeting the current authority head.
877    ///
878    /// Submits only — the local [`Authority`](ts_tka::Authority) is **not** mutated here. A handler
879    /// error carries the real [`ts_control::TkaSyncError`] (incl.
880    /// [`Unsupported`](ts_control::TkaSyncError::Unsupported) when there is no known TKA head to
881    /// disable); any other send failure collapses to
882    /// [`NetworkError`](ts_control::TkaSyncError::NetworkError).
883    pub async fn tka_disable(
884        &self,
885        disablement_secret: Vec<u8>,
886    ) -> Result<(), ts_control::TkaSyncError> {
887        self.control
888            .ask(control_runner::TkaDisable { disablement_secret })
889            .await
890            .map_err(flatten_tka_send_err)
891    }
892
893    /// Initialize Tailnet Lock with this node as the sole initial trusted key, gated by
894    /// `disablement_secret` (Go `tka` init → `/machine/tka/init/{begin,finish}`).
895    ///
896    /// Submits only — does not seed the local [`Authority`](ts_tka::Authority); the node picks up the
897    /// new lock via the existing verified netmap-sync. A handler error carries the real
898    /// [`ts_control::TkaSyncError`] ([`Unsupported`](ts_control::TkaSyncError::Unsupported) if
899    /// control needs other nodes re-signed — the single-node "lock yourself in" subset only); any
900    /// other send failure collapses to [`NetworkError`](ts_control::TkaSyncError::NetworkError).
901    pub async fn tka_init(
902        &self,
903        disablement_secret: Vec<u8>,
904    ) -> Result<(), ts_control::TkaSyncError> {
905        self.control
906            .ask(control_runner::TkaInit { disablement_secret })
907            .await
908            .map_err(flatten_tka_send_err)
909    }
910
911    /// Issue a real Let's Encrypt certificate for this node's MagicDNS `name` (`acme` feature).
912    ///
913    /// Mirrors `fetch_id_token`: forwards to the control runner, which runs
914    /// the client-side ACME DNS-01 flow on a spawned task and publishes the challenge TXT via the
915    /// node's set-dns RPC. The kameo delegated-reply send error is flattened — a handler error
916    /// carries the real [`ts_control::CertError`]; any other send failure (actor shutdown / mailbox
917    /// closed) is surfaced as a [`ts_control::CertError::Io`]. SaaS-only: a self-hosted control
918    /// plane 501s on set-dns.
919    #[cfg(feature = "acme")]
920    pub async fn get_certificate(
921        &self,
922        name: String,
923    ) -> Result<ts_control::tls::CertifiedKey, ts_control::CertError> {
924        self.control
925            .ask(control_runner::GetCertificate { name })
926            .await
927            .map_err(flatten_cert_send_err)
928    }
929
930    /// Issue a real Let's Encrypt certificate for this node's MagicDNS `name` and return the
931    /// **PEM pair** `(cert_chain_pem, key_pem)` — the analog of Go's
932    /// `LocalClient.CertPairWithValidity`, for writing the daemon's on-disk `.crt` + `.key`
933    /// (`tnet cert`). `acme` feature.
934    ///
935    /// Same issuance as [`get_certificate`](Self::get_certificate) (one client-side ACME DNS-01
936    /// order, challenge published via the node's set-dns RPC) — only the result shape differs: this
937    /// returns the leaf+chain PEM and the leaf-key PEM instead of the opaque
938    /// [`CertifiedKey`](ts_control::tls::CertifiedKey). The second element is the **leaf private
939    /// key** PEM; it is never logged anywhere on this path.
940    ///
941    /// **`min_validity` (honest "always fresh").** Go's `CertPairWithValidity` reuses a cached cert
942    /// when it has at least `min_validity` of its lifetime left, and re-issues otherwise. This fork
943    /// has **no cert cache** — every call performs a fresh issuance — so `min_validity` is accepted
944    /// for signature compatibility but does not change behavior: a freshly issued cert (full
945    /// lifetime) trivially satisfies any `min_validity`. A reuse cache is separate future work; this
946    /// does NOT fake one.
947    ///
948    /// Mirrors [`get_certificate`](Self::get_certificate)'s error handling: the kameo
949    /// delegated-reply send error is flattened — a handler error carries the real
950    /// [`ts_control::CertError`]; any other send failure (actor shutdown / mailbox closed) collapses
951    /// to a [`ts_control::CertError::Io`]. SaaS-only: a self-hosted control plane 501s on set-dns.
952    #[cfg(feature = "acme")]
953    pub async fn cert_pair(
954        &self,
955        name: String,
956        min_validity: Option<Duration>,
957    ) -> Result<(String, String), ts_control::CertError> {
958        // No cert cache exists in this fork (every issuance is fresh), so `min_validity` is honored
959        // trivially by always issuing a full-lifetime cert. Bound (unused beyond this contract) so
960        // the parameter is explicitly accounted for rather than silently ignored.
961        let _ = min_validity;
962        self.control
963            .ask(control_runner::GetCertPair { name })
964            .await
965            .map_err(flatten_cert_send_err)
966    }
967
968    /// Resolve which node owns a tailnet source address.
969    ///
970    /// Maps the destination IP of `addr` to its owning node. Mirrors tsnet's `LocalClient::WhoIs`.
971    /// Returns `None` if no peer holds that tailnet IP.
972    ///
973    /// The returned [`WhoIs`] additionally carries the **flow-scoped** peer-capability grants
974    /// ([`WhoIs::cap_map`], Go `apitype.WhoIsResponse.CapMap`): the caps control's packet-filter
975    /// application rules authorize for traffic from THIS node (the flow source) to `addr` (the
976    /// destination). Empty when no grant matches. (The node-level cap map rides
977    /// [`WhoIs::capabilities`].)
978    pub async fn whois(&self, addr: core::net::SocketAddr) -> Result<Option<WhoIs>, Error> {
979        let whois = self
980            .peer_tracker
981            .upgrade()
982            .ok_or(Error {
983                kind: ErrorKind::ActorGone,
984                target_actor: None,
985                message_ty: None,
986            })?
987            .ask(peer_tracker::Whois { addr })
988            .await?;
989
990        let Some(mut whois) = whois else {
991            return Ok(None);
992        };
993
994        // Fill the flow-scoped cap map: src = this node's own tailnet IP (of the dst's family),
995        // dst = the queried address. A grant applies when its source matches the flow source — `src`
996        // ∈ its src prefixes OR this node holds one of its source node-caps — AND `dst` ∈ its dst
997        // prefixes (Go `Filter.CapsWithValues`). Resolve our own IP + cap map from the self node; if
998        // it isn't known yet, leave the map empty (no grants resolvable without a source).
999        let dst = addr.ip();
1000        if let Some(self_node) = self.control.ask(control_runner::SelfNode).await? {
1001            let src: core::net::IpAddr = if dst.is_ipv6() {
1002                self_node.tailnet_address.ipv6.addr().into()
1003            } else {
1004                self_node.tailnet_address.ipv4.addr().into()
1005            };
1006            let grants = self.cap_grants_rx.borrow();
1007            whois.cap_map = ts_packetfilter_state::caps_for(&grants, src, dst, |cap| {
1008                self_node.has_node_attr(cap)
1009            });
1010        }
1011
1012        Ok(Some(whois))
1013    }
1014
1015    /// The current direct-path status to the peer holding tailnet IP `dst`: its confirmed direct UDP
1016    /// endpoint and that path's last-measured RTT, or `None` when there is no direct path right now
1017    /// (the peer is relayed via DERP, is unknown, or has no disco key).
1018    ///
1019    /// The latency is the RTT of the most recent disco ping/pong that confirmed the path — a live
1020    /// snapshot up to one probe interval stale, NOT a fresh on-demand round-trip (that is a separate,
1021    /// heavier capability). Mirrors the direct-path latency Go surfaces for `ipnstate.PeerStatus`.
1022    pub async fn direct_path(
1023        &self,
1024        dst: core::net::IpAddr,
1025    ) -> Result<Option<(core::net::SocketAddr, Duration)>, Error> {
1026        let peer_tracker = self.peer_tracker.upgrade().ok_or(Error {
1027            kind: ErrorKind::ActorGone,
1028            target_actor: None,
1029            message_ty: None,
1030        })?;
1031
1032        // Resolve the tailnet IP to its node, then to its disco key. No node / no disco key ⇒ no
1033        // direct path is possible (a peer with no disco key can only be reached via DERP).
1034        let Some(node) = peer_tracker
1035            .ask(peer_tracker::PeerByTailnetIp { ip: dst })
1036            .await?
1037        else {
1038            return Ok(None);
1039        };
1040        let Some(disco) = node.disco_key else {
1041            return Ok(None);
1042        };
1043
1044        self.direct
1045            .ask(direct::DirectPathLatency { disco })
1046            .await
1047            .map_err(Into::into)
1048    }
1049
1050    /// Send a disco ping to the peer holding tailnet IP `dst` **now** and await the pong, returning
1051    /// the fresh round-trip latency and the endpoint that answered, or `None` if no pong arrives
1052    /// within `timeout` (or the peer is unknown / has no disco key / no candidate path). This is the
1053    /// true on-demand `PingType::Disco` (Go `tailscale ping`), as opposed to
1054    /// [`direct_path`](Self::direct_path) which reports the last periodic probe's RTT.
1055    ///
1056    /// The ping round-trip is awaited OFF the direct manager's mailbox (we take a `MagicSock` handle
1057    /// and await on it directly), so a slow/timing-out ping never blocks the actor.
1058    pub async fn ping_disco(
1059        &self,
1060        dst: core::net::IpAddr,
1061        timeout: Duration,
1062    ) -> Result<Option<(core::net::SocketAddr, Duration)>, Error> {
1063        let peer_tracker = self.peer_tracker.upgrade().ok_or(Error {
1064            kind: ErrorKind::ActorGone,
1065            target_actor: None,
1066            message_ty: None,
1067        })?;
1068
1069        let Some(node) = peer_tracker
1070            .ask(peer_tracker::PeerByTailnetIp { ip: dst })
1071            .await?
1072        else {
1073            return Ok(None);
1074        };
1075        let Some(disco) = node.disco_key else {
1076            return Ok(None);
1077        };
1078
1079        // Cheap synchronous handle fetch, then await the ping OFF the actor mailbox.
1080        let Some(sock) = self.direct.ask(direct::SockHandle).await? else {
1081            return Ok(None);
1082        };
1083        // A `ping_now` error is an underlay UDP send failure (not an actor problem); surface it as a
1084        // reply-level error. A timed-out / unanswered ping is `Ok(None)`, not an error.
1085        sock.ping_now(&disco, timeout).await.map_err(|_| Error {
1086            kind: ErrorKind::ReplyErr,
1087            target_actor: None,
1088            message_ty: None,
1089        })
1090    }
1091
1092    /// Change the selected exit node at runtime (the equivalent of Go `tsnet`'s
1093    /// `LocalClient.EditPrefs(ExitNodeID/ExitNodeIP)`), without recreating the device.
1094    ///
1095    /// Updates the live exit-node selector, then asks the peer tracker to re-broadcast the current
1096    /// peer set so the route updater and source filter re-resolve the new selector immediately.
1097    /// `None` clears the exit node (internet-bound traffic is then dropped, fail-closed, unless this
1098    /// node egresses directly). The selection is re-resolved against the live peer set, so passing a
1099    /// selector for a peer not yet in the netmap simply takes effect once that peer appears.
1100    pub async fn set_exit_node(
1101        &self,
1102        selector: Option<ts_control::ExitNodeSelector>,
1103    ) -> Result<(), Error> {
1104        // Update the live cell every reader borrows from. `send_replace` keeps the value current
1105        // even with no active receivers (none can have dropped while the runtime is up, but it is
1106        // the right non-failing primitive here).
1107        self.exit_node_tx.send_replace(selector);
1108
1109        // Trigger an immediate re-resolution: the route updater (outbound routes + DoH delegation)
1110        // and the source filter (inbound validation) both recompute on an `Arc<PeerState>`, so a
1111        // re-broadcast applies the new exit without waiting for the next netmap update.
1112        self.peer_tracker
1113            .upgrade()
1114            .ok_or(Error {
1115                kind: ErrorKind::ActorGone,
1116                target_actor: None,
1117                message_ty: None,
1118            })?
1119            .ask(peer_tracker::RepublishState)
1120            .await
1121            .map_err(Into::into)
1122    }
1123
1124    /// The currently-selected exit node, or `None` if none is selected.
1125    pub fn exit_node(&self) -> Option<ts_control::ExitNodeSelector> {
1126        self.env.exit_node()
1127    }
1128
1129    /// Toggle whether this node accepts peer-advertised subnet routes at runtime (the equivalent of
1130    /// Go `tsnet`'s `LocalClient.EditPrefs(RouteAll)` / `tailscale set --accept-routes`), without
1131    /// recreating the device.
1132    ///
1133    /// `accept-routes` is a purely **local** preference — unlike advertised routes it is never
1134    /// reported to control (no `Hostinfo` / MapRequest side), so this only re-runs the local
1135    /// route/source-filter recompute, mirroring [`set_exit_node`](Self::set_exit_node) rather than
1136    /// [`set_advertise_routes`](Self::set_advertise_routes). Updates the live cell, then asks the peer
1137    /// tracker to re-broadcast the current peer set so the route updater (outbound routes) and the
1138    /// source filter (inbound validation) re-filter against the new value immediately: turning it on
1139    /// installs newly-accepted subnet routes (and widens the source filter to match); turning it off
1140    /// removes them from BOTH in lock-step (never accepting a source for a route no longer installed).
1141    /// Self routes and the exit-node default `/0` are unaffected (the latter is gated by the exit-node
1142    /// selection, not this flag).
1143    ///
1144    /// In TUN transport mode the host routing table is also re-steered live: the `RepublishState`
1145    /// kicked below re-broadcasts the peer set to the `TunActor`, whose `PeerState` handler re-reads
1146    /// `accept_routes` (and the exit selection) from `Env` and re-applies the host routes — so the
1147    /// toggle takes effect without rebuilding the device (the apply is an idempotent add-new/
1148    /// remove-gone diff). The exit-node default `/0` is still keyed on the exit selection, not this flag.
1149    pub async fn set_accept_routes(&self, accept: bool) -> Result<(), Error> {
1150        // Update the live cell every reader borrows from (same primitive/rationale as set_exit_node).
1151        self.accept_routes_tx.send_replace(accept);
1152
1153        // Trigger an immediate re-filter: the route updater and source filter both recompute on an
1154        // `Arc<PeerState>`, so a re-broadcast applies the new preference without waiting for the next
1155        // netmap update. Both re-read the same live cell, so the outbound route set and the inbound
1156        // source filter stay coupled (the anti-leak invariant).
1157        self.peer_tracker
1158            .upgrade()
1159            .ok_or(Error {
1160                kind: ErrorKind::ActorGone,
1161                target_actor: None,
1162                message_ty: None,
1163            })?
1164            .ask(peer_tracker::RepublishState)
1165            .await
1166            .map_err(Into::into)
1167    }
1168
1169    /// Whether this node currently accepts peer-advertised subnet routes (`--accept-routes`).
1170    pub fn accept_routes(&self) -> bool {
1171        self.env.accept_routes()
1172    }
1173
1174    /// Toggle whether this node accepts the tailnet's DNS configuration at runtime (the equivalent of
1175    /// Go `tsnet`'s `LocalClient.EditPrefs(CorpDNS)` / `tailscale set --accept-dns`), without
1176    /// recreating the device.
1177    ///
1178    /// Like [`set_accept_routes`](Self::set_accept_routes), `accept-dns` is a purely **local**
1179    /// preference — it is never reported to control (no `Hostinfo` / MapRequest side), so this only
1180    /// re-runs the local MagicDNS view rebuild. Updates the live cell, then asks the peer tracker to
1181    /// re-broadcast the current peer set; the resulting `PeerState` rebuild re-applies the gate on the
1182    /// MagicDNS responder (and the peerAPI DoH server that shares its view). When `false`, the
1183    /// responder ignores the control-pushed DNS config and answers every query `REFUSED`, mirroring Go
1184    /// applying an empty `dns.Config` when `CorpDNS` is off; flipping it back to `true` restores
1185    /// serving from the still-current config (the real config is never destroyed — only gated at the
1186    /// read site), so the OFF→ON restore is automatic.
1187    pub async fn set_accept_dns(&self, accept: bool) -> Result<(), Error> {
1188        // Update the live cell every reader borrows from (same primitive/rationale as set_accept_routes).
1189        self.accept_dns_tx.send_replace(accept);
1190
1191        // Trigger an immediate view rebuild: the MagicDNS responder re-reads `Env::accept_dns()` when
1192        // it handles a `PeerState`, so a re-broadcast re-applies the gate on both the netstack
1193        // responder and the peerAPI DoH server (which share the view) without waiting for the next
1194        // control/peer update. Mirrors `set_accept_routes`'s republish.
1195        self.peer_tracker
1196            .upgrade()
1197            .ok_or(Error {
1198                kind: ErrorKind::ActorGone,
1199                target_actor: None,
1200                message_ty: None,
1201            })?
1202            .ask(peer_tracker::RepublishState)
1203            .await
1204            .map_err(Into::into)
1205    }
1206
1207    /// Whether this node currently accepts the tailnet's DNS configuration (`--accept-dns` / `CorpDNS`).
1208    pub fn accept_dns(&self) -> bool {
1209        self.env.accept_dns()
1210    }
1211
1212    /// Change the set of subnet routes this node advertises at runtime (Go `tailscale set
1213    /// --advertise-routes`). Applies BOTH halves together so the wire and the data path agree:
1214    ///
1215    /// 1. **Wire** — re-advertise `Hostinfo.RoutableIPs` to control on the live map-poll connection
1216    ///    (so control grants the node the subnet-router role for exactly these prefixes).
1217    /// 2. **Local** — swap the forwarder's accept/dial route table (so the node actually forwards the
1218    ///    prefixes it advertises). New flows see the new set; in-flight flows keep their routing.
1219    ///
1220    /// `routes` is filtered to the IPv4-only, deduplicated set this fork can honor (IPv6 prefixes are
1221    /// dropped under the IPv6-off posture — we never advertise a route we won't forward), so the wire
1222    /// and forwarder are fed the identical final set. This sets the explicit subnet prefixes only; it
1223    /// does NOT touch the exit-node `0.0.0.0/0` advertisement (a separate concern).
1224    pub async fn set_advertise_routes(&self, routes: Vec<ipnet::IpNet>) -> Result<(), Error> {
1225        // Update the explicit-subnet part of the live preference, keep the exit-node flag, and
1226        // re-send the composed set. Composes with `set_advertise_exit_node` (neither clobbers the
1227        // other's contribution to `Hostinfo.RoutableIPs`).
1228        let composed = {
1229            let mut adv = self.advertise.lock().unwrap_or_else(|p| p.into_inner());
1230            adv.routes = routes;
1231            compose_advertised_routes(adv.routes.clone(), adv.exit_node)
1232        };
1233        self.apply_advertised_routes(composed).await
1234    }
1235
1236    /// Advertise (or stop advertising) this node as an **exit node** — the `0.0.0.0/0` default route
1237    /// (Go `tailscale set --advertise-exit-node`). Composes with
1238    /// [`set_advertise_routes`](Self::set_advertise_routes): toggling the exit node re-sends the
1239    /// explicit subnet routes plus (when `enable`) `0.0.0.0/0`, so the two preferences are
1240    /// independent. Like `set_advertise_routes`, this both re-advertises `Hostinfo.RoutableIPs` to
1241    /// control AND updates the forwarder's accept/dial set, applied together. Control still gates
1242    /// whether the advertised exit node is actually *usable* by peers (this only advertises it).
1243    pub async fn set_advertise_exit_node(&self, enable: bool) -> Result<(), Error> {
1244        let composed = {
1245            let mut adv = self.advertise.lock().unwrap_or_else(|p| p.into_inner());
1246            adv.exit_node = enable;
1247            compose_advertised_routes(adv.routes.clone(), adv.exit_node)
1248        };
1249        self.apply_advertised_routes(composed).await
1250    }
1251
1252    /// Push a freshly-composed advertised-route set to BOTH halves: the forwarder's accept/dial
1253    /// table (local) FIRST — so the node forwards a prefix before control grants it, never the
1254    /// reverse — then re-advertise `Hostinfo.RoutableIPs` to control on the live map-poll connection
1255    /// (wire). `composed` is already filtered + exit-node-folded by [`compose_advertised_routes`].
1256    async fn apply_advertised_routes(&self, composed: Vec<ipnet::IpNet>) -> Result<(), Error> {
1257        self.forwarder
1258            .ask(forwarder_actor::UpdateRoutes {
1259                routes: composed.clone(),
1260            })
1261            .await?;
1262        self.control
1263            .ask(control_runner::SetAdvertiseRoutes { routes: composed })
1264            .await
1265            .map_err(Into::into)
1266    }
1267
1268    /// Change this node's hostname at runtime (Go `tailscale set --hostname`), re-reporting
1269    /// `Hostinfo.Hostname` to control on the live map-poll connection. Hostname is display-only
1270    /// (control reflects it in the netmap), so there is no dataplane half. The new value is also
1271    /// what a subsequent re-registration reports, so it persists across a reconnect.
1272    pub async fn set_hostname(&self, hostname: String) -> Result<(), Error> {
1273        self.control
1274            .ask(control_runner::SetHostname { hostname })
1275            .await
1276            .map_err(Into::into)
1277    }
1278
1279    /// Subscribe to netmap peer-change events: the **narrow** peer-set view.
1280    ///
1281    /// Returns a [`watch::Receiver`] whose value is the current set of peer [`StatusNode`]s,
1282    /// updated on every netmap state update from control. Await
1283    /// [`watch::Receiver::changed`](tokio::sync::watch::Receiver::changed) to react to peers
1284    /// joining, leaving, or changing. For the unified Go-`WatchIPNBus` feed that merges this with
1285    /// device-state and the interactive-login URL, see [`watch_ipn_bus`](Self::watch_ipn_bus); this
1286    /// method is the peer-only projection of the same underlying cell.
1287    pub async fn watch_netmap(&self) -> Result<watch::Receiver<Vec<StatusNode>>, Error> {
1288        self.peer_tracker
1289            .upgrade()
1290            .ok_or(Error {
1291                kind: ErrorKind::ActorGone,
1292                target_actor: None,
1293                message_ty: None,
1294            })?
1295            .ask(peer_tracker::WatchNetmap)
1296            .await
1297            .map_err(Into::into)
1298    }
1299
1300    /// The current device connection-[`DeviceState`].
1301    pub fn device_state(&self) -> DeviceState {
1302        self.state_rx.borrow().clone()
1303    }
1304
1305    /// Watch the device connection-[`DeviceState`] (`Connecting` → `Running` / `NeedsLogin` /
1306    /// `Expired` / `Failed`).
1307    ///
1308    /// Returns a [`watch::Receiver`]; await
1309    /// [`changed`](tokio::sync::watch::Receiver::changed) to react push-style to control connection
1310    /// transitions instead of polling [`status`](Self::status). The initial value is the current
1311    /// state. Note: a transient per-reconnect dip back to `Connecting` is **not** currently
1312    /// emitted (control transparently reconnects below this layer); the state reflects registration
1313    /// outcome and node-key expiry.
1314    pub fn watch_state(&self) -> watch::Receiver<DeviceState> {
1315        self.state_rx.clone()
1316    }
1317
1318    /// Wait until the device finishes registering, returning a typed outcome.
1319    ///
1320    /// Resolves `Ok(())` once the device reaches [`DeviceState::Running`]. Returns a typed
1321    /// [`RegistrationError`] otherwise — the actionable distinction between "retry", "re-pair", and
1322    /// "drive interactive login" that replaces polling the device's `ipv4_addr` in a loop:
1323    /// - `AuthRejected` — bad/expired/unknown auth key. **Permanent** (re-pair).
1324    /// - `NeedsLogin(url)` — interactive authorization required (no usable auth key). **Not
1325    ///   permanent**: the runtime keeps retrying and will reach `Running` once the user authorizes
1326    ///   the URL. An **auth-key** caller should treat this as a failure; an **interactive** caller
1327    ///   should ignore this return and instead drive the flow via [`watch_state`](Self::watch_state)
1328    ///   (this method returns the URL eagerly rather than blocking for the whole login).
1329    /// - `NetworkUnreachable` — control unreachable. **Transient** (retry).
1330    /// - `Timeout` — no settled state within `timeout`.
1331    ///
1332    /// `KeyExpired` is not produced by this initial wait (a node key expires only *after* it has
1333    /// come up); observe post-registration expiry via [`watch_state`](Self::watch_state).
1334    /// `timeout` of `None` waits indefinitely for a settled state.
1335    pub async fn wait_until_running(
1336        &self,
1337        timeout: Option<Duration>,
1338    ) -> Result<(), RegistrationError> {
1339        device_state::wait_for_running(self.state_rx.clone(), timeout).await
1340    }
1341
1342    /// Subscribe to the unified IPN notification bus (Go `ipn` `WatchIPNBus` /
1343    /// `LocalBackend.WatchNotifications`).
1344    ///
1345    /// Returns an [`IpnBusWatcher`]; await [`next`](IpnBusWatcher::next) to receive [`Notify`]
1346    /// events that coalesce device-[`DeviceState`] changes (including the interactive-login URL as
1347    /// `browse_to_url`) and netmap peer-set changes into one feed. `mask`
1348    /// ([`NotifyWatchOpt`]) selects which current-state fields are front-loaded as an initial
1349    /// snapshot on subscribe (`INITIAL_STATE` / `INITIAL_NETMAP`), exactly like Go's
1350    /// `NotifyInitialState` / `NotifyInitialNetMap`.
1351    ///
1352    /// This composes the same `watch` cells as [`watch_state`](Self::watch_state),
1353    /// [`watch_netmap`](Self::watch_netmap), and `pop_browser_url` — one source of truth, so the
1354    /// merged feed cannot diverge from those narrow views. Besides the registration-time login URL
1355    /// (carried by `NeedsLogin`), `browse_to_url` also streams the mid-session
1356    /// `MapResponse.PopBrowserURL` (re-auth / consent on an already-running node). Delivery is
1357    /// best-effort/lossy (a bounded per-watcher buffer; a notification is dropped rather than
1358    /// blocking the runtime if a slow consumer's buffer fills), matching Go's bus. The stream ends
1359    /// (`next` returns `None`) on runtime shutdown or when the watcher is dropped.
1360    pub async fn watch_ipn_bus(&self, mask: NotifyWatchOpt) -> Result<IpnBusWatcher, Error> {
1361        // The peer-set cell lives on the peer-tracker actor; obtain a receiver the same way
1362        // `watch_netmap` does. State + shutdown cells are held here.
1363        let peer_rx = self
1364            .peer_tracker
1365            .upgrade()
1366            .ok_or(Error {
1367                kind: ErrorKind::ActorGone,
1368                target_actor: None,
1369                message_ty: None,
1370            })?
1371            .ask(peer_tracker::WatchNetmap)
1372            .await?;
1373        // The running-node consent-URL cell lives on the control runner; obtain its receiver the
1374        // same way (the control actor ref is strong, so no upgrade needed).
1375        let browser_rx = self.control.ask(control_runner::WatchBrowserUrl).await?;
1376        Ok(ipn_bus::spawn_watcher(
1377            mask,
1378            self.state_rx.clone(),
1379            peer_rx,
1380            browser_rx,
1381            self.shutdown.subscribe(),
1382        ))
1383    }
1384
1385    /// Attempt to shut down the runtime gracefully.
1386    ///
1387    /// Returns false if the shutdown timed out. It is still shut down if it timed out, just
1388    /// more violently and with possible resource leaks.
1389    pub async fn graceful_shutdown(self, timeout: Option<Duration>) -> bool {
1390        self.shutdown.send_replace(true);
1391
1392        async fn _shutdown_all(runtime: Runtime) {
1393            // See the note in `Drop` for why we only need to stop these actors to bring down the
1394            // whole runtime.
1395
1396            let _ignore = runtime.control.stop_gracefully().await;
1397            let _ignore = runtime.dataplane.stop_gracefully().await;
1398            let _ignore = runtime.env.bus.stop_gracefully().await;
1399
1400            tokio::join![
1401                runtime.control.wait_for_shutdown(),
1402                runtime.dataplane.wait_for_shutdown(),
1403                runtime.env.bus.wait_for_shutdown(),
1404            ];
1405        }
1406
1407        let fut = _shutdown_all(self);
1408
1409        match timeout {
1410            Some(timeout) => tokio::time::timeout(timeout, fut).await.is_ok(),
1411            None => {
1412                fut.await;
1413                true
1414            }
1415        }
1416    }
1417}
1418
1419impl Drop for Runtime {
1420    fn drop(&mut self) {
1421        // Stop the taildrop reaper so it cannot outlive the runtime (the `reauth_bridge` pattern). It
1422        // also self-exits when `shutdown` flips below, but aborting is immediate and covers the
1423        // already-shutdown early-return path too.
1424        if let Some(reaper) = self.taildrop_reaper.take() {
1425            reaper.abort();
1426        }
1427
1428        // We must have already run `graceful_shutdown`: on the happy path, this does nothing, but
1429        // if it timed out, we need to make sure the actors are dead so we don't leak them and their
1430        // dependents.
1431        if *self.shutdown.borrow() {
1432            self.control.kill();
1433            self.dataplane.kill();
1434            self.env.bus.kill();
1435            return;
1436        }
1437
1438        self.shutdown.send_replace(true);
1439
1440        // Actors shut down when the last ActorRef to them is dropped (as nothing can send them
1441        // messages anymore). If we don't hold an ActorRef in Runtime, in general the only thing
1442        // that has one is the MessageBus, which each actor subscribes to for a subset of messages.
1443        // Hence, if we shut down the bus, most actors die as well.
1444
1445        // First shut down the actors we have an ActorRef to:
1446        try_shutdown(&self.control);
1447        try_shutdown(&self.dataplane);
1448
1449        // Then shutdown the message bus, stopping the rest of the actors:
1450        try_shutdown(&self.env.bus);
1451    }
1452}
1453
1454fn try_shutdown(a: &ActorRef<impl kameo::Actor>) {
1455    if let Err(e) = a.mailbox_sender().try_send(Signal::Stop) {
1456        tracing::error!(error = %e, "graceful shutdown failed, killing actor");
1457        a.kill();
1458    }
1459}
1460
1461/// Tailscale's overlay MTU. The userspace netstacks MUST advertise an MSS that fits this so they
1462/// never hand the WireGuard encrypt path an IP packet larger than the tunnel can carry (the netstack
1463/// has no PMTU discovery and nothing re-segments between it and the 1280-MTU TUN). This is the same
1464/// default the TUN device uses (`tun_config_from_control`); both are derived from this value so the
1465/// netstack and the TUN always agree.
1466///
1467/// This is the **inner** IP-packet budget. The WireGuard transport header (a 16-byte
1468/// `TransportDataHeader` + the 16-byte AEAD tag = 32 bytes) is added by `TransmitSession::encrypt`
1469/// *after* the netstack produces the inner packet, and the outer UDP/IP headers ride on top of that.
1470/// So do NOT subtract the WireGuard overhead here — that would be a double-subtraction that
1471/// under-fills the tunnel and diverges from the TUN's MTU. The assert below documents that the outer
1472/// datagram still fits a conventional 1500-byte physical path with margin (1280 + 32 WG + 8 UDP +
1473/// 20 outer-IP = 1340).
1474const DEFAULT_OVERLAY_MTU: u16 = 1280;
1475
1476const _: () = assert!(
1477    DEFAULT_OVERLAY_MTU as usize + 32 + 8 + 20 <= 1500,
1478    "inner overlay MTU + WireGuard(32) + UDP(8) + outer-IP(20) must fit a 1500-byte physical path"
1479);
1480
1481/// Build the netstack config shared by both userspace netstacks (application + forwarder) from the
1482/// per-deployment `tcp_buffer_size` and `mtu` knobs.
1483///
1484/// `tcp_buffer_size`: `None` keeps the netstack default (256 KiB/direction); `Some(n)` overrides it
1485/// (e.g. a smaller window on a memory-constrained exit node forwarding many concurrent flows — see
1486/// [`netstack::netcore::Config::tcp_buffer_size`]).
1487///
1488/// `mtu`: the overlay/tunnel MTU. `None` (and a stray `0`) falls back to [`DEFAULT_OVERLAY_MTU`]
1489/// (1280), exactly as the TUN device does, so the netstack's advertised MSS fits the tunnel. Leaving
1490/// this at the netstack's generic 1500 default (the prior behavior) made smoltcp advertise MSS ~1460
1491/// and segment to ~1500 B, which then overflowed the 1280 TUN — a PMTU black-hole / throughput cliff.
1492///
1493/// Factored out of [`Runtime::spawn`] so the mapping is unit-testable without standing up the actors.
1494fn netstack_config_from(
1495    tcp_buffer_size: Option<usize>,
1496    mtu: Option<u16>,
1497) -> netstack::netcore::Config {
1498    let mut c = netstack::netcore::Config::default();
1499    if let Some(tcp_buffer_size) = tcp_buffer_size {
1500        c.tcp_buffer_size = tcp_buffer_size;
1501    }
1502    // `0` is not a usable MTU; treat it like `None` and fall back to the overlay default, mirroring
1503    // the TUN's `and_then(NonZeroU16::new).unwrap_or(1280)`.
1504    let mtu = mtu.filter(|&m| m != 0).unwrap_or(DEFAULT_OVERLAY_MTU);
1505    c.mtu = usize::from(mtu);
1506    c
1507}
1508
1509/// Filter a requested advertise-route set to the IPv4-only, deduplicated set this fork can honor,
1510/// mirroring [`ts_control::Config::advertised_routes`] so a runtime `set_advertise_routes` feeds the
1511/// wire (control grant) and the forwarder (accept/dial table) the identical final set. IPv6 prefixes
1512/// are dropped under the IPv6-off posture — we never advertise a route we won't forward. Order is
1513/// preserved (first occurrence wins). Factored out so the filter is unit-testable without an actor.
1514fn filter_advertise_routes(routes: Vec<ipnet::IpNet>) -> Vec<ipnet::IpNet> {
1515    let mut filtered: Vec<ipnet::IpNet> = Vec::new();
1516    for net in routes {
1517        if matches!(net, ipnet::IpNet::V4(_)) {
1518            if !filtered.contains(&net) {
1519                filtered.push(net);
1520            }
1521        } else {
1522            tracing::warn!(prefix = %net, "dropping IPv6 advertise route (IPv6-off posture)");
1523        }
1524    }
1525    filtered
1526}
1527
1528/// Compose the final advertised-route set from the explicit subnet `routes` and the exit-node flag,
1529/// mirroring [`ts_control::Config::advertised_routes`]: the IPv4-only, deduplicated subnet prefixes,
1530/// plus `0.0.0.0/0` appended when `exit_node` is set. This is the single source of truth both
1531/// runtime advertise mutators (`set_advertise_routes`, `set_advertise_exit_node`) feed, so the two
1532/// compose instead of clobbering. Factored out so the composition is unit-testable without an actor.
1533fn compose_advertised_routes(routes: Vec<ipnet::IpNet>, exit_node: bool) -> Vec<ipnet::IpNet> {
1534    let mut filtered = filter_advertise_routes(routes);
1535    if exit_node {
1536        let default_v4 = ipnet::IpNet::V4(
1537            ipnet::Ipv4Net::new(core::net::Ipv4Addr::UNSPECIFIED, 0)
1538                .expect("0.0.0.0/0 is a valid prefix"),
1539        );
1540        if !filtered.contains(&default_v4) {
1541            filtered.push(default_v4);
1542        }
1543    }
1544    filtered
1545}
1546
1547/// The runtime's live advertised-route preference: the explicit subnet routes plus whether this node
1548/// advertises itself as an exit node. Held behind a `Mutex` on the [`Runtime`] so
1549/// [`Runtime::set_advertise_routes`] and [`Runtime::set_advertise_exit_node`] each mutate their own
1550/// part and re-send the composed set — they compose rather than clobber (Go `EditPrefs` keeps
1551/// `AdvertiseRoutes` and the exit-node advertisement as independent prefs that both feed
1552/// `Hostinfo.RoutableIPs`).
1553#[derive(Debug, Default, Clone)]
1554struct AdvertiseState {
1555    /// The explicit subnet prefixes (pre-filter; the last value passed to `set_advertise_routes`).
1556    routes: Vec<ipnet::IpNet>,
1557    /// Whether this node advertises the exit-node default route (`0.0.0.0/0`).
1558    exit_node: bool,
1559}
1560
1561/// Flatten a kameo delegated-reply [`SendError`] for the id-token RPC into the RPC's own
1562/// [`ts_control::IdTokenError`].
1563///
1564/// A [`SendError::HandlerError`](kameo::error::SendError::HandlerError) carries the real
1565/// `IdTokenError` produced by the handler and is surfaced verbatim. Any other send failure (actor
1566/// not running / stopped, mailbox full, send timeout) is a delivery problem rather than an RPC
1567/// result, so it collapses to a transient [`ts_control::IdTokenError::NetworkError`]. Factored out
1568/// of [`Runtime::fetch_id_token`] so this mapping is unit-testable without standing up an actor.
1569fn flatten_send_err<M>(
1570    e: kameo::error::SendError<M, ts_control::IdTokenError>,
1571) -> ts_control::IdTokenError {
1572    match e {
1573        kameo::error::SendError::HandlerError(err) => err,
1574        _ => ts_control::IdTokenError::NetworkError,
1575    }
1576}
1577
1578/// Flatten a kameo `SendError` from the `Logout` ask into a [`ts_control::LogoutError`].
1579///
1580/// A `HandlerError` carries the real `LogoutError` from the control RPC and is surfaced verbatim;
1581/// any other send failure (actor not running / stopped, mailbox full, send timeout) — a delivery
1582/// problem, not a logout result — collapses to the transient [`ts_control::LogoutError::NetworkError`]
1583/// (logout is idempotent, so a retry after a delivery failure is safe). Factored out of
1584/// [`Runtime::logout`] so the mapping is unit-testable without standing up an actor.
1585fn flatten_logout_send_err<M>(
1586    e: kameo::error::SendError<M, ts_control::LogoutError>,
1587) -> ts_control::LogoutError {
1588    match e {
1589        kameo::error::SendError::HandlerError(err) => err,
1590        _ => ts_control::LogoutError::NetworkError,
1591    }
1592}
1593
1594/// Flatten a kameo `SendError` from the `SetDns` ask into a [`ts_control::SetDnsError`].
1595///
1596/// A `HandlerError` carries the real `SetDnsError` from the set-dns RPC and is surfaced verbatim;
1597/// any other send failure (actor not running / stopped, mailbox full, send timeout) — a delivery
1598/// problem, not a publish result — collapses to the transient
1599/// [`ts_control::SetDnsError::NetworkError`]. Factored out of [`Runtime::set_dns`] so the mapping is
1600/// unit-testable without standing up an actor.
1601fn flatten_set_dns_send_err<M>(
1602    e: kameo::error::SendError<M, ts_control::SetDnsError>,
1603) -> ts_control::SetDnsError {
1604    match e {
1605        kameo::error::SendError::HandlerError(err) => err,
1606        _ => ts_control::SetDnsError::NetworkError,
1607    }
1608}
1609
1610/// Flatten a kameo `SendError` from a TKA mutation ask (`TkaSign`/`TkaDisable`) into a
1611/// [`ts_control::TkaSyncError`]. A `HandlerError` carries the real RPC error; any other send failure
1612/// (actor shutdown / mailbox closed) is surfaced as the transient
1613/// [`ts_control::TkaSyncError::NetworkError`]. Generic over the message type so both share it.
1614fn flatten_tka_send_err<M>(
1615    e: kameo::error::SendError<M, ts_control::TkaSyncError>,
1616) -> ts_control::TkaSyncError {
1617    match e {
1618        kameo::error::SendError::HandlerError(err) => err,
1619        _ => ts_control::TkaSyncError::NetworkError,
1620    }
1621}
1622
1623/// Flatten a kameo `SendError` from the `GetCertificate` / `GetCertPair` ask into a
1624/// [`ts_control::CertError`].
1625///
1626/// A `HandlerError` carries the real `CertError` produced by the ACME issuance and is surfaced
1627/// verbatim. `CertError` has no transient-network variant, so any other send failure (actor not
1628/// running / stopped, mailbox full, send timeout) — a delivery problem rather than an issuance
1629/// result — collapses to a [`ts_control::CertError::Io`]. Generic over the message type, so it
1630/// serves both [`Runtime::get_certificate`] and [`Runtime::cert_pair`]; factored out so the mapping
1631/// is unit-testable without standing up an actor.
1632#[cfg(feature = "acme")]
1633fn flatten_cert_send_err<M>(
1634    e: kameo::error::SendError<M, ts_control::CertError>,
1635) -> ts_control::CertError {
1636    match e {
1637        kameo::error::SendError::HandlerError(err) => err,
1638        _ => ts_control::CertError::Io(std::io::Error::other(
1639            "control runner unavailable for certificate issuance",
1640        )),
1641    }
1642}
1643
1644#[cfg(test)]
1645mod tests {
1646    use super::*;
1647
1648    /// `None` must leave the netstack's own default TCP window in place (the 256 KiB throughput
1649    /// default), and must not silently coerce to some other value.
1650    #[test]
1651    fn netstack_config_none_uses_netstack_default() {
1652        let default = netstack::netcore::Config::default();
1653        let built = netstack_config_from(None, None);
1654        assert_eq!(
1655            built.tcp_buffer_size, default.tcp_buffer_size,
1656            "None must inherit the netstack default TCP buffer size"
1657        );
1658    }
1659
1660    #[test]
1661    fn netstack_config_mtu_defaults_to_overlay_not_generic_1500() {
1662        // The crux of the fix: with no explicit MTU, the netstack must use the 1280 overlay MTU, NOT
1663        // smoltcp's generic 1500 default — otherwise it advertises an MSS that overflows the tunnel.
1664        let built = netstack_config_from(None, None);
1665        assert_eq!(
1666            built.mtu,
1667            usize::from(DEFAULT_OVERLAY_MTU),
1668            "netstack MTU must default to the 1280 overlay MTU, not the 1500 netstack default"
1669        );
1670        assert_ne!(built.mtu, 1500, "must not leave the generic 1500 default");
1671    }
1672
1673    #[test]
1674    fn netstack_config_honors_explicit_mtu_and_rejects_zero() {
1675        // An explicit (control-supplied) MTU is honored verbatim.
1676        assert_eq!(netstack_config_from(None, Some(1400)).mtu, 1400);
1677        // A stray 0 is not a usable MTU; fall back to the overlay default (mirrors the TUN).
1678        assert_eq!(
1679            netstack_config_from(None, Some(0)).mtu,
1680            usize::from(DEFAULT_OVERLAY_MTU)
1681        );
1682    }
1683
1684    #[test]
1685    fn netstack_config_overlay_mtu_matches_tun_default() {
1686        // The netstack MTU default and the TUN MTU default must be the same value, or the two
1687        // netstacks and the TUN would disagree on the segment size budget.
1688        assert_eq!(
1689            DEFAULT_OVERLAY_MTU, 1280,
1690            "overlay MTU must match the TUN device default (tun_config_from_control)"
1691        );
1692    }
1693
1694    /// `Some(n)` must override the TCP window (the memory-vs-throughput knob exit-node operators
1695    /// reach for), reaching the config that both netstacks are built from.
1696    #[test]
1697    fn netstack_config_some_overrides_buffer() {
1698        let built = netstack_config_from(Some(64 * 1024), None);
1699        assert_eq!(
1700            built.tcp_buffer_size,
1701            64 * 1024,
1702            "Some(n) must override the TCP buffer size that both netstacks use"
1703        );
1704    }
1705
1706    /// `set_advertise_routes` must feed the wire and the forwarder the IDENTICAL filtered set:
1707    /// IPv4-only (IPv6 dropped under the IPv6-off posture), deduplicated, order preserved.
1708    #[test]
1709    fn filter_advertise_routes_keeps_v4_dedups_drops_v6() {
1710        let v4a: ipnet::IpNet = "10.0.0.0/24".parse().unwrap();
1711        let v4b: ipnet::IpNet = "192.168.1.0/24".parse().unwrap();
1712        let v6: ipnet::IpNet = "2001:db8::/32".parse().unwrap();
1713
1714        // Mixed input with a duplicate v4 and a v6 prefix.
1715        let out = filter_advertise_routes(vec![v4a, v6, v4b, v4a]);
1716
1717        assert_eq!(
1718            out,
1719            vec![v4a, v4b],
1720            "v6 dropped, duplicate v4 collapsed, first-occurrence order preserved"
1721        );
1722    }
1723
1724    /// An all-IPv6 request filters to empty (we never advertise a route we won't forward) rather
1725    /// than erroring — clearing the advertised set is a legitimate outcome.
1726    #[test]
1727    fn filter_advertise_routes_all_v6_is_empty() {
1728        let v6: ipnet::IpNet = "2001:db8::/32".parse().unwrap();
1729        assert!(filter_advertise_routes(vec![v6]).is_empty());
1730    }
1731
1732    /// `compose_advertised_routes` folds the exit-node `0.0.0.0/0` onto the filtered subnet routes
1733    /// when (and only when) the exit-node flag is set — so `set_advertise_routes` and
1734    /// `set_advertise_exit_node` compose. The two preferences are independent.
1735    #[test]
1736    fn compose_advertised_routes_folds_exit_node() {
1737        let subnet: ipnet::IpNet = "10.0.0.0/24".parse().unwrap();
1738        let default_v4: ipnet::IpNet = "0.0.0.0/0".parse().unwrap();
1739
1740        // Exit node off: just the (filtered) subnet routes.
1741        assert_eq!(
1742            compose_advertised_routes(vec![subnet], false),
1743            vec![subnet],
1744            "exit-node off ⇒ no default route"
1745        );
1746        // Exit node on: subnet routes PLUS 0.0.0.0/0.
1747        assert_eq!(
1748            compose_advertised_routes(vec![subnet], true),
1749            vec![subnet, default_v4],
1750            "exit-node on ⇒ 0.0.0.0/0 appended"
1751        );
1752        // Exit node on with NO subnet routes: just the default route.
1753        assert_eq!(
1754            compose_advertised_routes(vec![], true),
1755            vec![default_v4],
1756            "exit-node alone advertises only 0.0.0.0/0"
1757        );
1758        // Idempotent: an explicit 0.0.0.0/0 already in the routes isn't duplicated by the fold.
1759        assert_eq!(
1760            compose_advertised_routes(vec![default_v4], true),
1761            vec![default_v4],
1762            "the exit-node fold dedups against an explicit default route"
1763        );
1764    }
1765
1766    /// A `HandlerError` carries the real `IdTokenError` from the RPC handler and must pass through
1767    /// verbatim, not be flattened to a generic network error. Using an `Internal(_)` payload (not
1768    /// `NetworkError`) makes the passthrough observable: a buggy flatten that always returned
1769    /// `NetworkError` would fail this assertion.
1770    #[test]
1771    fn flatten_send_err_handler_error_passes_through() {
1772        // Build an `Internal(_)` payload via the public `From<Utf8Error>` conversion (no extra
1773        // deps): it is distinct from the `_ => NetworkError` fallback, so a buggy flatten that
1774        // always returned `NetworkError` would fail this assertion.
1775        // Route the invalid bytes through a runtime Vec so the `invalid_from_utf8` lint (which only
1776        // fires on compile-time-known literals) doesn't flag this intentional bad input.
1777        let bytes = vec![0xffu8, 0xfe];
1778        let utf8_err = core::str::from_utf8(&bytes).unwrap_err();
1779        let inner = ts_control::IdTokenError::from(utf8_err);
1780        assert!(matches!(inner, ts_control::IdTokenError::Internal(_)));
1781        let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
1782            kameo::error::SendError::HandlerError(inner.clone());
1783        assert_eq!(flatten_send_err(e), inner);
1784    }
1785
1786    /// A non-handler send failure (actor stopped) is a delivery problem, not an RPC result, so it
1787    /// must collapse to a transient `NetworkError`.
1788    #[test]
1789    fn flatten_send_err_actor_stopped_is_network_error() {
1790        let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
1791            kameo::error::SendError::ActorStopped;
1792        assert_eq!(flatten_send_err(e), ts_control::IdTokenError::NetworkError);
1793    }
1794
1795    /// `ActorNotRunning` (the message bounces back undelivered) is likewise a delivery failure and
1796    /// must map to a transient `NetworkError`.
1797    #[test]
1798    fn flatten_send_err_actor_not_running_is_network_error() {
1799        let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
1800            kameo::error::SendError::ActorNotRunning(control_runner::FetchIdToken {
1801                audience: "sts.amazonaws.com".to_string(),
1802            });
1803        assert_eq!(flatten_send_err(e), ts_control::IdTokenError::NetworkError);
1804    }
1805
1806    /// A `HandlerError` from the logout RPC carries the real `LogoutError` and must pass through
1807    /// verbatim. An `Internal(_)` payload (distinct from the `_ => NetworkError` fallback) makes the
1808    /// passthrough observable.
1809    #[test]
1810    fn flatten_logout_send_err_handler_error_passes_through() {
1811        let inner = ts_control::LogoutError::Internal(ts_control::LogoutInternalErrorKind::Http);
1812        assert!(matches!(inner, ts_control::LogoutError::Internal(_)));
1813        let e: kameo::error::SendError<control_runner::Logout, ts_control::LogoutError> =
1814            kameo::error::SendError::HandlerError(inner.clone());
1815        assert_eq!(flatten_logout_send_err(e), inner);
1816    }
1817
1818    /// A non-handler send failure (actor stopped) is a delivery problem, not a logout result, and
1819    /// collapses to a transient `NetworkError` (logout is idempotent, so a retry is safe).
1820    #[test]
1821    fn flatten_logout_send_err_actor_stopped_is_network_error() {
1822        let e: kameo::error::SendError<control_runner::Logout, ts_control::LogoutError> =
1823            kameo::error::SendError::ActorStopped;
1824        assert_eq!(
1825            flatten_logout_send_err(e),
1826            ts_control::LogoutError::NetworkError
1827        );
1828    }
1829
1830    /// A `HandlerError` from the set-dns RPC carries the real `SetDnsError` and must pass through
1831    /// verbatim. An `Internal(_)` payload (distinct from the `_ => NetworkError` fallback) makes the
1832    /// passthrough observable.
1833    #[test]
1834    fn flatten_set_dns_send_err_handler_error_passes_through() {
1835        let inner = ts_control::SetDnsError::Internal(ts_control::SetDnsInternalErrorKind::Http);
1836        assert!(matches!(inner, ts_control::SetDnsError::Internal(_)));
1837        let e: kameo::error::SendError<control_runner::SetDns, ts_control::SetDnsError> =
1838            kameo::error::SendError::HandlerError(inner.clone());
1839        assert_eq!(flatten_set_dns_send_err(e), inner);
1840    }
1841
1842    /// A non-handler send failure (actor stopped) is a delivery problem, not a publish result, and
1843    /// collapses to a transient `NetworkError`.
1844    #[test]
1845    fn flatten_set_dns_send_err_actor_stopped_is_network_error() {
1846        let e: kameo::error::SendError<control_runner::SetDns, ts_control::SetDnsError> =
1847            kameo::error::SendError::ActorStopped;
1848        assert_eq!(
1849            flatten_set_dns_send_err(e),
1850            ts_control::SetDnsError::NetworkError
1851        );
1852    }
1853
1854    /// A `HandlerError` from a TKA mutation RPC carries the real `TkaSyncError` and must pass through
1855    /// verbatim (an `Unsupported` payload makes the passthrough observable, distinct from the
1856    /// `_ => NetworkError` fallback).
1857    #[test]
1858    fn flatten_tka_send_err_handler_error_passes_through() {
1859        let e: kameo::error::SendError<control_runner::TkaSign, ts_control::TkaSyncError> =
1860            kameo::error::SendError::HandlerError(ts_control::TkaSyncError::Unsupported);
1861        assert_eq!(
1862            flatten_tka_send_err(e),
1863            ts_control::TkaSyncError::Unsupported
1864        );
1865    }
1866
1867    /// A non-handler send failure (actor stopped) collapses to a transient `NetworkError`.
1868    #[test]
1869    fn flatten_tka_send_err_actor_stopped_is_network_error() {
1870        let e: kameo::error::SendError<control_runner::TkaSign, ts_control::TkaSyncError> =
1871            kameo::error::SendError::ActorStopped;
1872        assert_eq!(
1873            flatten_tka_send_err(e),
1874            ts_control::TkaSyncError::NetworkError
1875        );
1876    }
1877
1878    /// The same flatten works for the `TkaDisable` message type (the helper is generic over `M`).
1879    #[test]
1880    fn flatten_tka_send_err_works_for_disable() {
1881        let e: kameo::error::SendError<control_runner::TkaDisable, ts_control::TkaSyncError> =
1882            kameo::error::SendError::HandlerError(ts_control::TkaSyncError::Unsupported);
1883        assert_eq!(
1884            flatten_tka_send_err(e),
1885            ts_control::TkaSyncError::Unsupported
1886        );
1887    }
1888}