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