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/// Fallback TCP handler registry (`tsnet.Server.RegisterFallbackTCPHandler` parity).
33pub mod fallback_tcp;
34mod forwarder_actor;
35/// Client-side Funnel ingress termination (`tsnet`'s `ListenFunnel` data path).
36pub mod funnel;
37mod magic_dns;
38mod multiderp;
39mod netstack_actor;
40mod packetfilter;
41pub mod peer_tracker;
42mod peerapi;
43mod peerapi_doh;
44mod route_updater;
45/// Stored Serve config + accept-loop runtime (`tsnet`'s `Get/SetServeConfig` + serving runtime).
46pub mod serve;
47mod src_filter;
48/// Netmap status snapshot, WhoIs, and watcher types.
49pub mod status;
50/// Taildrop peer-to-peer file transfer store.
51pub mod taildrop;
52pub mod taildrop_send;
53#[cfg(feature = "tun")]
54mod tun_actor;
55
56pub use device_state::{DeviceState, RegistrationError};
57pub(crate) use env::Env;
58pub use error::{Error, ErrorKind};
59pub use status::{FileTarget, Status, StatusNode, WhoIs};
60pub use ts_dataplane::{CaptureHook, CapturePath};
61
62use crate::peer_tracker::PeerTracker;
63
64/// The runtime for a tailscale device.
65pub struct Runtime {
66 /// Reference to the control actor.
67 pub control: ActorRef<ControlRunner>,
68 dataplane: ActorRef<DataplaneActor>,
69 /// Reference to the direct (disco/UDP underlay) manager, retained so [`Runtime::rebind`] can
70 /// ask it to re-bind the underlay socket on a network/link change.
71 direct: ActorRef<DirectManager>,
72 /// Reference to the application netstack actor. `None` in TUN transport mode, where there is
73 /// no userspace application netstack (the application data path is a real kernel TUN device).
74 netstack: Option<WeakActorRef<NetstackActor>>,
75 /// Reference to the peer tracker for peer lookups.
76 pub peer_tracker: WeakActorRef<PeerTracker>,
77 /// Fallback TCP handler registry, bound to the application netstack. `None` in TUN transport
78 /// mode (no application netstack exists to attach it to).
79 fallback_tcp: Option<fallback_tcp::FallbackTcpManager>,
80 env: Env,
81 shutdown: watch::Sender<bool>,
82 /// Sender side of the exit-node selector `watch` cell. Held privately here (not on the cloned
83 /// `Env`, which keeps only the read side) so that only `Runtime::set_exit_node` can mutate the
84 /// selection; the route updater and source filter re-read it via [`Env::exit_node`].
85 exit_node_tx: watch::Sender<Option<ts_control::ExitNodeSelector>>,
86 /// Receiver mirroring the *active* (resolved + fail-closed) exit node's stable id, fed by the
87 /// route updater. Read by [`Runtime::status`] / [`Runtime::active_exit_node`] to report which
88 /// exit node traffic is actually egressing through (vs. the merely-configured selector).
89 active_exit_rx: watch::Receiver<Option<ts_control::StableNodeId>>,
90 /// Receiver for the device connection-state cell, fed by the control runner. Read by
91 /// [`Runtime::watch_state`] and [`Runtime::wait_until_running`].
92 state_rx: watch::Receiver<DeviceState>,
93}
94
95impl Runtime {
96 /// Spawn a new runtime with the given parameters for connecting to a tailnet.
97 pub async fn spawn(
98 config: ts_control::Config,
99 auth_key: Option<String>,
100 keys: ts_keys::NodeState,
101 ) -> Result<Self, Error> {
102 let (shutdown_tx, shutdown_rx) = watch::channel(false);
103
104 // The exit-node selector is a live `watch` cell so `Device::set_exit_node` can change it at
105 // runtime. `new_with_exit_tx` returns the `Sender` (mutation capability) separately so it is
106 // retained privately on the `Runtime`, while only the `Receiver` (the readers' contract)
107 // lives on the cloned `Env`. The initial value comes from `ForwarderConfig.exit_node`.
108 let (env, exit_node_tx) = Env::new_with_exit_tx(
109 keys,
110 shutdown_rx,
111 env::ForwarderConfig::from_control_config(&config),
112 );
113
114 // Both userspace netstacks (application + forwarder) share one netstack config. Honor the
115 // per-deployment TCP buffer knob when set, otherwise fall back to the netstack default.
116 let netstack_config = netstack_config_from(config.tcp_buffer_size);
117
118 let dataplane = DataplaneActor::spawn(env.clone());
119
120 let (netstack_id, netstack_up, netstack_down) =
121 dataplane.ask(dataplane::NewOverlayTransport).await?;
122
123 // A second overlay transport feeds the dedicated any-IP forwarder netstack. Inbound packets
124 // for advertised subnet routes / the exit-node default route are routed here (see
125 // `route_updater`), keeping forwarded flows off the application netstack.
126 let (forwarder_id, forwarder_up, forwarder_down) =
127 dataplane.ask(dataplane::NewOverlayTransport).await?;
128
129 let multiderp = Multiderp::spawn((env.clone(), dataplane.clone()));
130
131 // Spawn the direct (disco) underlay manager before the route updater. Its `on_start`
132 // binds the UDP socket and registers its transport synchronously, so by the time the
133 // route updater asks it for the direct transport id it is guaranteed to be available.
134 let direct = DirectManager::spawn((env.clone(), dataplane.clone(), multiderp.clone()));
135
136 // Spawn the forwarder before the route updater. Its `on_start` builds the forwarder
137 // netstack, enables any-IP acceptance, and starts the per-port accept loops synchronously,
138 // so by the time the route updater begins delivering advertised prefixes to
139 // `forwarder_id` the netstack is already draining its transport.
140 let forwarder = ForwarderActor::spawn((
141 env.clone(),
142 netstack_config.clone(),
143 forwarder_up,
144 forwarder_down,
145 ));
146 // Force `on_start` to finish (any-IP enabled, accept loops live) before the route updater
147 // can route the first inbound flow to `forwarder_id`: an `ask` blocks until the actor has
148 // started.
149 //
150 // The forwarder netstack's overlay `Channel` is reused by the TUN application path for
151 // recursive / exit-node-DoH MagicDNS forwarding (TUN mode has no application netstack of its
152 // own, but the forwarder netstack runs in both modes and egresses over the overlay — the
153 // anti-leak property `forward_query`/`forward_doh` require). Only the `tun` Tun arm consumes
154 // it, so it is unused when the `tun` feature is off — allow that without warn-as-error.
155 #[cfg_attr(not(feature = "tun"), allow(unused_variables))]
156 let (forwarder_channel,) = forwarder.ask(forwarder_actor::GetChannel).await?;
157
158 // The route updater is the single authoritative resolver of the active (resolved,
159 // fail-closed) exit node; it publishes the resolved stable id into this watch cell so
160 // `Runtime::status` can report which exit is actually engaged (not just configured).
161 let (active_exit_tx, active_exit_rx) = watch::channel(None);
162 route_updater::RouteUpdater::spawn((
163 multiderp.clone(),
164 direct.clone(),
165 env.clone(),
166 netstack_id,
167 forwarder_id,
168 active_exit_tx,
169 ));
170 packetfilter::PacketfilterUpdater::spawn(env.clone());
171 src_filter::SourceFilterUpdater::spawn(env.clone());
172 let peer_tracker = PeerTracker::spawn(env.clone()).downgrade();
173
174 // Select the application data path from the transport mode. The forwarder/egress path
175 // above is UNCHANGED in both modes — TUN mode only swaps the application data path, never
176 // the forwarder. `config` is moved into `ControlRunner::spawn` below, so branch on a
177 // borrow and clone the small `TunConfig` where needed before the move.
178 //
179 // - Netstack (the default, and the only reachable arm when the `tun` feature is off):
180 // spawn the application netstack + MagicDNS responder + fallback-TCP registry, all on
181 // the `netstack_up`/`netstack_down` overlay seam.
182 // - Tun: spawn `TunActor` on that same overlay seam instead; no application netstack and
183 // no MagicDNS responder exist, and `netstack`/`fallback_tcp` are `None`.
184 // - Tun requested but built without the `tun` feature: hard-error (a config/build
185 // mismatch knowable at spawn time). NEVER silently fall back to netstack.
186 let (netstack, fallback_tcp) = match &config.transport_mode {
187 ts_control::TransportMode::Netstack => {
188 let netstack = NetstackActor::spawn((
189 env.clone(),
190 netstack_config,
191 netstack_up,
192 netstack_down,
193 ));
194
195 // Fetch the netstack channel while we still hold the strong ActorRef, then spawn
196 // the MagicDNS responder on it. Fire-and-forget: like src_filter/route_updater,
197 // it's owned by the message bus and isn't stored on `Runtime`.
198 let (channel,) = netstack.ask(netstack_actor::GetChannel).await?;
199 // The fallback-TCP registry attaches to the application netstack — the same one
200 // that carries the embedder's explicit `Device::tcp_listen` sockets — so a
201 // fallback handler sees exactly the inbound flows no explicit listener matched.
202 let fallback_tcp = fallback_tcp::FallbackTcpManager::new(channel.clone());
203 magic_dns::MagicDnsActor::spawn((env.clone(), channel));
204
205 (Some(netstack.downgrade()), Some(fallback_tcp))
206 }
207
208 #[cfg(feature = "tun")]
209 ts_control::TransportMode::Tun(tun_cfg) => {
210 // Reuse the same `netstack_up`/`netstack_down` overlay-transport pair that would
211 // have fed the netstack — it is just the application-side overlay seam (the name
212 // is historical). No NetstackActor / MagicDnsActor is spawned.
213 tun_actor::TunActor::spawn((
214 env.clone(),
215 tun_cfg.clone(),
216 netstack_up,
217 netstack_down,
218 // Host-route gating inputs derived from `Env`: subnet routes are only steered
219 // into the TUN when `--accept-routes` is set, and the host `/0` only when the
220 // embedder configured an exit node. See `tun_actor::host_routes_from_node`.
221 tun_actor::HostRouteGating {
222 accept_routes: env.accept_routes,
223 exit_node_configured: env.exit_node().is_some(),
224 },
225 // Reuse the forwarder netstack's overlay `Channel` for recursive / exit-node-DoH
226 // MagicDNS forwarding in the TUN datapath (TUN mode has no application netstack
227 // Channel of its own). Egresses over the overlay — anti-leak preserved.
228 forwarder_channel.clone(),
229 ));
230
231 (None, None)
232 }
233
234 #[cfg(not(feature = "tun"))]
235 ts_control::TransportMode::Tun(_) => {
236 return Err(Error {
237 kind: ErrorKind::TunUnavailable,
238 target_actor: None,
239 message_ty: None,
240 });
241 }
242 };
243
244 // Device connection-state cell. Created here (not inside the actor) so the control runner's
245 // `on_start` can publish `Failed`/`NeedsLogin` and still return `Err` without the sender
246 // being tied to a `Self` that never gets constructed on a hard registration failure.
247 let (state_tx, state_rx) = watch::channel(DeviceState::Connecting);
248
249 let control = ControlRunner::spawn(control_runner::Params {
250 config,
251 auth_key,
252 env: env.clone(),
253 state_tx,
254 });
255
256 Ok(Self {
257 control,
258 dataplane,
259 direct,
260 peer_tracker,
261 fallback_tcp,
262 netstack,
263 env,
264 shutdown: shutdown_tx,
265 exit_node_tx,
266 active_exit_rx,
267 state_rx,
268 })
269 }
270
271 /// Register a fallback TCP handler consulted for every inbound TCP flow that matches no
272 /// explicit listener (`tsnet.Server.RegisterFallbackTCPHandler` parity).
273 ///
274 /// The returned [`fallback_tcp::FallbackTcpHandle`] deregisters the handler when dropped. See
275 /// [`fallback_tcp`] for the dispatch contract and anti-leak guarantees.
276 ///
277 /// Returns [`ErrorKind::UnsupportedInTunMode`] in TUN transport mode, where there is no
278 /// application netstack to attach a fallback handler to.
279 pub fn register_fallback_tcp_handler(
280 &self,
281 cb: Arc<
282 dyn Fn(core::net::SocketAddr, core::net::SocketAddr) -> fallback_tcp::FallbackDecision
283 + Send
284 + Sync,
285 >,
286 ) -> Result<fallback_tcp::FallbackTcpHandle, Error> {
287 Ok(self
288 .fallback_tcp
289 .as_ref()
290 .ok_or(Error {
291 kind: ErrorKind::UnsupportedInTunMode,
292 target_actor: None,
293 message_ty: None,
294 })?
295 .register(cb))
296 }
297
298 /// Get a channel to send commands to the netstack.
299 ///
300 /// Returns [`ErrorKind::UnsupportedInTunMode`] in TUN transport mode, where there is no
301 /// application netstack.
302 pub async fn channel(&self) -> Result<Channel, Error> {
303 let (channel,) = self
304 .netstack
305 .as_ref()
306 .ok_or(Error {
307 kind: ErrorKind::UnsupportedInTunMode,
308 target_actor: None,
309 message_ty: None,
310 })?
311 .upgrade()
312 .ok_or(Error {
313 kind: ErrorKind::ActorGone,
314 target_actor: None,
315 message_ty: None,
316 })?
317 .ask(netstack_actor::GetChannel)
318 .await?;
319
320 Ok(channel)
321 }
322
323 /// The Taildrop file store, if Taildrop is enabled (`taildrop_dir` configured and the store
324 /// initialized). `None` when disabled — fail-closed. Shared with the peerAPI Taildrop server so
325 /// the embedder's read APIs and the receive path see the same on-disk store.
326 pub fn taildrop_store(&self) -> Option<Arc<crate::taildrop::TaildropStore>> {
327 self.env.taildrop_store.clone()
328 }
329
330 /// The shared Funnel ingress slot the peerAPI `/v0/ingress` route reads per connection.
331 ///
332 /// `Device::listen_funnel` installs a [`FunnelManager`](crate::funnel::FunnelManager)'s sink here
333 /// to make the route live (the peerAPI server is already running from startup). Returns a clone of
334 /// the runtime-lifetime `Arc` so the device can write the slot without restarting the server. See
335 /// [`crate::funnel`] for the ingress data path.
336 pub fn funnel_ingress_slot(&self) -> crate::funnel::FunnelIngressSlot {
337 self.env.funnel_ingress.clone()
338 }
339
340 /// The shared "Funnel ingress listener active" flag (the same `Arc` the control session reads to
341 /// set `HostInfo.IngressEnabled`). `Device::listen_funnel` flips it `true` while a funnel listener
342 /// is up so control routes Funnel traffic to this node; clearing it advertises no live endpoint.
343 pub fn ingress_active_flag(&self) -> std::sync::Arc<std::sync::atomic::AtomicBool> {
344 self.env.ingress_active.clone()
345 }
346
347 /// Install (`Some`) or clear (`None`) the debug packet-capture hook on the running dataplane.
348 /// `Some(hook)` tees every plaintext packet crossing the datapath to `hook` until it is cleared;
349 /// `None` stops capture. Mirrors Go `tstun.Wrapper.InstallCaptureHook` / `ClearCaptureSink`.
350 pub async fn install_capture(
351 &self,
352 hook: Option<ts_dataplane::CaptureHook>,
353 ) -> Result<(), Error> {
354 self.dataplane
355 .ask(dataplane::InstallCapture { hook })
356 .await
357 .map_err(Into::into)
358 }
359
360 /// Re-bind the underlay UDP socket after a network/link change (Wi-Fi switch, sleep/wake). The
361 /// embedder's own link monitor calls this (the engine owns the socket re-bind; the embedder owns
362 /// OS netmon). Re-binds the socket (same-port-preferred, IPv4-only invariant preserved) and
363 /// resets the now-stale local NAT mapping — clearing learned reflexive addresses and every
364 /// confirmed direct path while keeping candidate endpoints, so peers re-probe over the new socket
365 /// and relay over DERP (never a direct host dial) until a path re-confirms. Peers, control, the
366 /// netmap, disco state, and DERP are untouched. A no-op when the underlay is inert (bind failed
367 /// at startup, DERP-only). Mirrors Go magicsock `Conn.Rebind` + `resetEndpointStates`.
368 pub async fn rebind(&self) -> Result<(), Error> {
369 self.direct.ask(direct::Rebind).await.map_err(Error::from)
370 }
371
372 /// A snapshot of the local netmap: this node plus every known peer.
373 ///
374 /// Combines the self node held by the control runner with the peer set held by the peer
375 /// tracker. Mirrors tsnet's `LocalClient::Status`.
376 ///
377 /// `self_node` is `None` until the first netmap update has been received from control. Peer
378 /// entries carry no online/user/capability data (see the [`status`] module docs for that gap).
379 pub async fn status(&self) -> Result<Status, Error> {
380 let self_node_domain = self.control.ask(control_runner::SelfNode).await?;
381 // The MagicDNS suffix is the self node's FQDN minus its host label — already split into
382 // `Node.tailnet` at decode time (Go derives it the same way in `NetworkMap.MagicDNSSuffix`).
383 // Capture it before the domain `Node` is mapped away into a `StatusNode`.
384 let magic_dns_suffix = self_node_domain.as_ref().and_then(|n| n.tailnet.clone());
385 let self_node = self_node_domain.as_ref().map(StatusNode::from_node);
386
387 let peers = self
388 .peer_tracker
389 .upgrade()
390 .ok_or(Error {
391 kind: ErrorKind::ActorGone,
392 target_actor: None,
393 message_ty: None,
394 })?
395 .ask(peer_tracker::GetStatus)
396 .await?;
397
398 Ok(Status {
399 self_node,
400 peers,
401 active_exit_node: self.active_exit_node(),
402 magic_dns_suffix,
403 })
404 }
405
406 /// List the tailnet peers this node can Taildrop a file *to* (Go LocalAPI `FileTargets`).
407 ///
408 /// Mirrors the upstream send-path filter (`feature/taildrop` `Extension::FileTargets`): a peer
409 /// qualifies when it advertises a reachable peerAPI **and** is either owned by the same user as
410 /// this node **or** explicitly granted the file-sharing-target capability. The whole list is
411 /// gated on this node holding the file-sharing capability (control sets it when the admin enables
412 /// Taildrop) — absent that, an empty list (fail-closed, not an error, matching how the receive
413 /// store returns empty when disabled). Results are sorted by the peer's MagicDNS name.
414 ///
415 /// Targets are listed regardless of current online state (upstream's `FileTargets` does not gate
416 /// on online either; an offline target's send will simply time out). The self node is never
417 /// included. Returns empty before the first netmap.
418 ///
419 /// Divergence from Go: the upstream filter also excludes `tvOS` peers, which this fork cannot
420 /// reproduce (the domain node carries no OS string); the impact is negligible — the actual send
421 /// fail-closes if such a peer refused the transfer.
422 pub async fn file_targets(&self) -> Result<Vec<FileTarget>, Error> {
423 // Node-level gate: this node must hold the file-sharing capability (Taildrop enabled by the
424 // admin). Read it off the self node's cap map, like Go's `hasCapFileSharing()`.
425 let self_node = self.control.ask(control_runner::SelfNode).await?;
426 let Some(self_node) = self_node else {
427 return Ok(Vec::new()); // no netmap yet
428 };
429 if !self_node.can_share_files() {
430 return Ok(Vec::new()); // Taildrop not enabled for the tailnet — fail-closed
431 }
432 let self_user_id = self_node.user_id;
433
434 let peers = self
435 .peer_tracker
436 .upgrade()
437 .ok_or(Error {
438 kind: ErrorKind::ActorGone,
439 target_actor: None,
440 message_ty: None,
441 })?
442 .ask(peer_tracker::AllPeers)
443 .await?;
444
445 // Eligibility + ordering live in `build_file_targets` (pure, unit-tested in `status`).
446 Ok(status::build_file_targets(peers, self_user_id))
447 }
448
449 /// The stable id of the exit node traffic is currently egressing through, or `None` if none is
450 /// engaged. This is the route updater's resolved + fail-closed answer (see
451 /// [`Status::active_exit_node`](crate::status::Status::active_exit_node)): it differs from the
452 /// configured [`exit_node`](Self::exit_node) selector, which may name a peer that is absent or
453 /// no longer advertising a default route (in which case egress is dropped and this returns
454 /// `None`).
455 pub fn active_exit_node(&self) -> Option<ts_control::StableNodeId> {
456 self.active_exit_rx.borrow().clone()
457 }
458
459 /// Request an OIDC ID token from control scoped to `audience` (workload-identity federation).
460 ///
461 /// Returns the signed JWT, or the token RPC's own [`ts_control::IdTokenError`]. The kameo
462 /// delegated-reply send error is flattened: a handler error carries the real `IdTokenError`,
463 /// any other send failure (actor shutdown / mailbox closed) is surfaced as
464 /// [`ts_control::IdTokenError::NetworkError`].
465 pub async fn fetch_id_token(
466 &self,
467 audience: String,
468 ) -> Result<String, ts_control::IdTokenError> {
469 self.control
470 .ask(control_runner::FetchIdToken { audience })
471 .await
472 .map_err(flatten_send_err)
473 }
474
475 /// Log this node out of the tailnet: deregister it by expiring its current node key.
476 ///
477 /// Forwards to the control runner, which re-POSTs `/machine/register` with a past expiry over a
478 /// fresh Noise channel. This is a control-plane state change only — it does NOT shut the runtime
479 /// down (the caller follows with [`graceful_shutdown`](Self::graceful_shutdown)) and does not
480 /// touch the on-disk node key. The kameo delegated-reply send error is flattened the same way as
481 /// [`fetch_id_token`](Self::fetch_id_token): a handler error carries the real
482 /// [`ts_control::LogoutError`]; any other send failure (actor shutdown / mailbox closed) is
483 /// surfaced as [`ts_control::LogoutError::NetworkError`].
484 pub async fn logout(&self) -> Result<(), ts_control::LogoutError> {
485 self.control
486 .ask(control_runner::Logout)
487 .await
488 .map_err(flatten_logout_send_err)
489 }
490
491 /// Issue a real Let's Encrypt certificate for this node's MagicDNS `name` (`acme` feature).
492 ///
493 /// Mirrors [`fetch_id_token`](Self::fetch_id_token): forwards to the control runner, which runs
494 /// the client-side ACME DNS-01 flow on a spawned task and publishes the challenge TXT via the
495 /// node's set-dns RPC. The kameo delegated-reply send error is flattened — a handler error
496 /// carries the real [`ts_control::CertError`]; any other send failure (actor shutdown / mailbox
497 /// closed) is surfaced as a [`ts_control::CertError::Io`]. SaaS-only: a self-hosted control
498 /// plane 501s on set-dns.
499 #[cfg(feature = "acme")]
500 pub async fn get_certificate(
501 &self,
502 name: String,
503 ) -> Result<ts_control::tls::CertifiedKey, ts_control::CertError> {
504 self.control
505 .ask(control_runner::GetCertificate { name })
506 .await
507 .map_err(flatten_cert_send_err)
508 }
509
510 /// Resolve which node owns a tailnet source address.
511 ///
512 /// Maps the source IP of `addr` to its owning node. Mirrors tsnet's `LocalClient::WhoIs`.
513 /// Returns `None` if no peer holds that tailnet IP. The returned [`WhoIs`] carries no
514 /// user/login or capability data in this fork (see the [`status`] module docs).
515 pub async fn whois(&self, addr: core::net::SocketAddr) -> Result<Option<WhoIs>, Error> {
516 self.peer_tracker
517 .upgrade()
518 .ok_or(Error {
519 kind: ErrorKind::ActorGone,
520 target_actor: None,
521 message_ty: None,
522 })?
523 .ask(peer_tracker::Whois { addr })
524 .await
525 .map_err(Into::into)
526 }
527
528 /// Change the selected exit node at runtime (the equivalent of Go `tsnet`'s
529 /// `LocalClient.EditPrefs(ExitNodeID/ExitNodeIP)`), without recreating the device.
530 ///
531 /// Updates the live exit-node selector, then asks the peer tracker to re-broadcast the current
532 /// peer set so the route updater and source filter re-resolve the new selector immediately.
533 /// `None` clears the exit node (internet-bound traffic is then dropped, fail-closed, unless this
534 /// node egresses directly). The selection is re-resolved against the live peer set, so passing a
535 /// selector for a peer not yet in the netmap simply takes effect once that peer appears.
536 pub async fn set_exit_node(
537 &self,
538 selector: Option<ts_control::ExitNodeSelector>,
539 ) -> Result<(), Error> {
540 // Update the live cell every reader borrows from. `send_replace` keeps the value current
541 // even with no active receivers (none can have dropped while the runtime is up, but it is
542 // the right non-failing primitive here).
543 self.exit_node_tx.send_replace(selector);
544
545 // Trigger an immediate re-resolution: the route updater (outbound routes + DoH delegation)
546 // and the source filter (inbound validation) both recompute on an `Arc<PeerState>`, so a
547 // re-broadcast applies the new exit without waiting for the next netmap update.
548 self.peer_tracker
549 .upgrade()
550 .ok_or(Error {
551 kind: ErrorKind::ActorGone,
552 target_actor: None,
553 message_ty: None,
554 })?
555 .ask(peer_tracker::RepublishState)
556 .await
557 .map_err(Into::into)
558 }
559
560 /// The currently-selected exit node, or `None` if none is selected.
561 pub fn exit_node(&self) -> Option<ts_control::ExitNodeSelector> {
562 self.env.exit_node()
563 }
564
565 /// Subscribe to netmap peer-change events.
566 ///
567 /// Returns a [`watch::Receiver`] whose value is the current set of peer [`StatusNode`]s,
568 /// updated on every netmap state update from control. Mirrors tsnet's `WatchIPNBus`. Await
569 /// [`watch::Receiver::changed`](tokio::sync::watch::Receiver::changed) to react to peers
570 /// joining, leaving, or changing.
571 pub async fn watch_netmap(&self) -> Result<watch::Receiver<Vec<StatusNode>>, Error> {
572 self.peer_tracker
573 .upgrade()
574 .ok_or(Error {
575 kind: ErrorKind::ActorGone,
576 target_actor: None,
577 message_ty: None,
578 })?
579 .ask(peer_tracker::WatchNetmap)
580 .await
581 .map_err(Into::into)
582 }
583
584 /// The current device connection-[`DeviceState`].
585 pub fn device_state(&self) -> DeviceState {
586 self.state_rx.borrow().clone()
587 }
588
589 /// Watch the device connection-[`DeviceState`] (`Connecting` → `Running` / `NeedsLogin` /
590 /// `Expired` / `Failed`).
591 ///
592 /// Returns a [`watch::Receiver`]; await
593 /// [`changed`](tokio::sync::watch::Receiver::changed) to react push-style to control connection
594 /// transitions instead of polling [`status`](Self::status). The initial value is the current
595 /// state. Note: a transient per-reconnect dip back to `Connecting` is **not** currently
596 /// emitted (control transparently reconnects below this layer); the state reflects registration
597 /// outcome and node-key expiry.
598 pub fn watch_state(&self) -> watch::Receiver<DeviceState> {
599 self.state_rx.clone()
600 }
601
602 /// Wait until the device finishes registering, returning a typed outcome.
603 ///
604 /// Resolves `Ok(())` once the device reaches [`DeviceState::Running`]. Returns a typed
605 /// [`RegistrationError`] otherwise — the actionable distinction between "retry", "re-pair", and
606 /// "drive interactive login" that replaces polling [`ipv4_addr`](Self::ipv4_addr) in a loop:
607 /// - `AuthRejected` — bad/expired/unknown auth key. **Permanent** (re-pair).
608 /// - `NeedsLogin(url)` — interactive authorization required (no usable auth key). **Not
609 /// permanent**: the runtime keeps retrying and will reach `Running` once the user authorizes
610 /// the URL. An **auth-key** caller should treat this as a failure; an **interactive** caller
611 /// should ignore this return and instead drive the flow via [`watch_state`](Self::watch_state)
612 /// (this method returns the URL eagerly rather than blocking for the whole login).
613 /// - `NetworkUnreachable` — control unreachable. **Transient** (retry).
614 /// - `Timeout` — no settled state within `timeout`.
615 ///
616 /// `KeyExpired` is not produced by this initial wait (a node key expires only *after* it has
617 /// come up); observe post-registration expiry via [`watch_state`](Self::watch_state).
618 /// `timeout` of `None` waits indefinitely for a settled state.
619 pub async fn wait_until_running(
620 &self,
621 timeout: Option<Duration>,
622 ) -> Result<(), RegistrationError> {
623 device_state::wait_for_running(self.state_rx.clone(), timeout).await
624 }
625
626 /// Attempt to shut down the runtime gracefully.
627 ///
628 /// Returns false if the shutdown timed out. It is still shut down if it timed out, just
629 /// more violently and with possible resource leaks.
630 pub async fn graceful_shutdown(self, timeout: Option<Duration>) -> bool {
631 self.shutdown.send_replace(true);
632
633 async fn _shutdown_all(runtime: Runtime) {
634 // See the note in `Drop` for why we only need to stop these actors to bring down the
635 // whole runtime.
636
637 let _ignore = runtime.control.stop_gracefully().await;
638 let _ignore = runtime.dataplane.stop_gracefully().await;
639 let _ignore = runtime.env.bus.stop_gracefully().await;
640
641 tokio::join![
642 runtime.control.wait_for_shutdown(),
643 runtime.dataplane.wait_for_shutdown(),
644 runtime.env.bus.wait_for_shutdown(),
645 ];
646 }
647
648 let fut = _shutdown_all(self);
649
650 match timeout {
651 Some(timeout) => tokio::time::timeout(timeout, fut).await.is_ok(),
652 None => {
653 fut.await;
654 true
655 }
656 }
657 }
658}
659
660impl Drop for Runtime {
661 fn drop(&mut self) {
662 // We must have already run `graceful_shutdown`: on the happy path, this does nothing, but
663 // if it timed out, we need to make sure the actors are dead so we don't leak them and their
664 // dependents.
665 if *self.shutdown.borrow() {
666 self.control.kill();
667 self.dataplane.kill();
668 self.env.bus.kill();
669 return;
670 }
671
672 self.shutdown.send_replace(true);
673
674 // Actors shut down when the last ActorRef to them is dropped (as nothing can send them
675 // messages anymore). If we don't hold an ActorRef in Runtime, in general the only thing
676 // that has one is the MessageBus, which each actor subscribes to for a subset of messages.
677 // Hence, if we shut down the bus, most actors die as well.
678
679 // First shut down the actors we have an ActorRef to:
680 try_shutdown(&self.control);
681 try_shutdown(&self.dataplane);
682
683 // Then shutdown the message bus, stopping the rest of the actors:
684 try_shutdown(&self.env.bus);
685 }
686}
687
688fn try_shutdown(a: &ActorRef<impl kameo::Actor>) {
689 if let Err(e) = a.mailbox_sender().try_send(Signal::Stop) {
690 tracing::error!(error = %e, "graceful shutdown failed, killing actor");
691 a.kill();
692 }
693}
694
695/// Build the netstack config shared by both userspace netstacks (application + forwarder) from the
696/// per-deployment `tcp_buffer_size` knob.
697///
698/// `None` keeps the netstack default (256 KiB/direction); `Some(n)` overrides it (e.g. a smaller
699/// window on a memory-constrained exit node forwarding many concurrent flows — see
700/// [`netstack::netcore::Config::tcp_buffer_size`]). Factored out of [`Runtime::spawn`] so the
701/// None-default / Some-override mapping is unit-testable without standing up the actor system.
702fn netstack_config_from(tcp_buffer_size: Option<usize>) -> netstack::netcore::Config {
703 let mut c = netstack::netcore::Config::default();
704 if let Some(tcp_buffer_size) = tcp_buffer_size {
705 c.tcp_buffer_size = tcp_buffer_size;
706 }
707 c
708}
709
710/// Flatten a kameo delegated-reply [`SendError`] for the id-token RPC into the RPC's own
711/// [`ts_control::IdTokenError`].
712///
713/// A [`SendError::HandlerError`](kameo::error::SendError::HandlerError) carries the real
714/// `IdTokenError` produced by the handler and is surfaced verbatim. Any other send failure (actor
715/// not running / stopped, mailbox full, send timeout) is a delivery problem rather than an RPC
716/// result, so it collapses to a transient [`ts_control::IdTokenError::NetworkError`]. Factored out
717/// of [`Runtime::fetch_id_token`] so this mapping is unit-testable without standing up an actor.
718fn flatten_send_err<M>(
719 e: kameo::error::SendError<M, ts_control::IdTokenError>,
720) -> ts_control::IdTokenError {
721 match e {
722 kameo::error::SendError::HandlerError(err) => err,
723 _ => ts_control::IdTokenError::NetworkError,
724 }
725}
726
727/// Flatten a kameo `SendError` from the `Logout` ask into a [`ts_control::LogoutError`].
728///
729/// A `HandlerError` carries the real `LogoutError` from the control RPC and is surfaced verbatim;
730/// any other send failure (actor not running / stopped, mailbox full, send timeout) — a delivery
731/// problem, not a logout result — collapses to the transient [`ts_control::LogoutError::NetworkError`]
732/// (logout is idempotent, so a retry after a delivery failure is safe). Factored out of
733/// [`Runtime::logout`] so the mapping is unit-testable without standing up an actor.
734fn flatten_logout_send_err<M>(
735 e: kameo::error::SendError<M, ts_control::LogoutError>,
736) -> ts_control::LogoutError {
737 match e {
738 kameo::error::SendError::HandlerError(err) => err,
739 _ => ts_control::LogoutError::NetworkError,
740 }
741}
742
743/// Flatten a kameo `SendError` from the `GetCertificate` ask into a [`ts_control::CertError`].
744///
745/// A `HandlerError` carries the real `CertError` produced by the ACME issuance and is surfaced
746/// verbatim. `CertError` has no transient-network variant, so any other send failure (actor not
747/// running / stopped, mailbox full, send timeout) — a delivery problem rather than an issuance
748/// result — collapses to a [`ts_control::CertError::Io`]. Factored out of
749/// [`Runtime::get_certificate`] so this mapping is unit-testable without standing up an actor.
750#[cfg(feature = "acme")]
751fn flatten_cert_send_err<M>(
752 e: kameo::error::SendError<M, ts_control::CertError>,
753) -> ts_control::CertError {
754 match e {
755 kameo::error::SendError::HandlerError(err) => err,
756 _ => ts_control::CertError::Io(std::io::Error::other(
757 "control runner unavailable for certificate issuance",
758 )),
759 }
760}
761
762#[cfg(test)]
763mod tests {
764 use super::*;
765
766 /// `None` must leave the netstack's own default TCP window in place (the 256 KiB throughput
767 /// default), and must not silently coerce to some other value.
768 #[test]
769 fn netstack_config_none_uses_netstack_default() {
770 let default = netstack::netcore::Config::default();
771 let built = netstack_config_from(None);
772 assert_eq!(
773 built.tcp_buffer_size, default.tcp_buffer_size,
774 "None must inherit the netstack default TCP buffer size"
775 );
776 }
777
778 /// `Some(n)` must override the TCP window (the memory-vs-throughput knob exit-node operators
779 /// reach for), reaching the config that both netstacks are built from.
780 #[test]
781 fn netstack_config_some_overrides_buffer() {
782 let built = netstack_config_from(Some(64 * 1024));
783 assert_eq!(
784 built.tcp_buffer_size,
785 64 * 1024,
786 "Some(n) must override the TCP buffer size that both netstacks use"
787 );
788 }
789
790 /// A `HandlerError` carries the real `IdTokenError` from the RPC handler and must pass through
791 /// verbatim, not be flattened to a generic network error. Using an `Internal(_)` payload (not
792 /// `NetworkError`) makes the passthrough observable: a buggy flatten that always returned
793 /// `NetworkError` would fail this assertion.
794 #[test]
795 fn flatten_send_err_handler_error_passes_through() {
796 // Build an `Internal(_)` payload via the public `From<Utf8Error>` conversion (no extra
797 // deps): it is distinct from the `_ => NetworkError` fallback, so a buggy flatten that
798 // always returned `NetworkError` would fail this assertion.
799 // Route the invalid bytes through a runtime Vec so the `invalid_from_utf8` lint (which only
800 // fires on compile-time-known literals) doesn't flag this intentional bad input.
801 let bytes = vec![0xffu8, 0xfe];
802 let utf8_err = core::str::from_utf8(&bytes).unwrap_err();
803 let inner = ts_control::IdTokenError::from(utf8_err);
804 assert!(matches!(inner, ts_control::IdTokenError::Internal(_)));
805 let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
806 kameo::error::SendError::HandlerError(inner.clone());
807 assert_eq!(flatten_send_err(e), inner);
808 }
809
810 /// A non-handler send failure (actor stopped) is a delivery problem, not an RPC result, so it
811 /// must collapse to a transient `NetworkError`.
812 #[test]
813 fn flatten_send_err_actor_stopped_is_network_error() {
814 let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
815 kameo::error::SendError::ActorStopped;
816 assert_eq!(flatten_send_err(e), ts_control::IdTokenError::NetworkError);
817 }
818
819 /// `ActorNotRunning` (the message bounces back undelivered) is likewise a delivery failure and
820 /// must map to a transient `NetworkError`.
821 #[test]
822 fn flatten_send_err_actor_not_running_is_network_error() {
823 let e: kameo::error::SendError<control_runner::FetchIdToken, ts_control::IdTokenError> =
824 kameo::error::SendError::ActorNotRunning(control_runner::FetchIdToken {
825 audience: "sts.amazonaws.com".to_string(),
826 });
827 assert_eq!(flatten_send_err(e), ts_control::IdTokenError::NetworkError);
828 }
829
830 /// A `HandlerError` from the logout RPC carries the real `LogoutError` and must pass through
831 /// verbatim. An `Internal(_)` payload (distinct from the `_ => NetworkError` fallback) makes the
832 /// passthrough observable.
833 #[test]
834 fn flatten_logout_send_err_handler_error_passes_through() {
835 let inner = ts_control::LogoutError::Internal(ts_control::LogoutInternalErrorKind::Http);
836 assert!(matches!(inner, ts_control::LogoutError::Internal(_)));
837 let e: kameo::error::SendError<control_runner::Logout, ts_control::LogoutError> =
838 kameo::error::SendError::HandlerError(inner.clone());
839 assert_eq!(flatten_logout_send_err(e), inner);
840 }
841
842 /// A non-handler send failure (actor stopped) is a delivery problem, not a logout result, and
843 /// collapses to a transient `NetworkError` (logout is idempotent, so a retry is safe).
844 #[test]
845 fn flatten_logout_send_err_actor_stopped_is_network_error() {
846 let e: kameo::error::SendError<control_runner::Logout, ts_control::LogoutError> =
847 kameo::error::SendError::ActorStopped;
848 assert_eq!(
849 flatten_logout_send_err(e),
850 ts_control::LogoutError::NetworkError
851 );
852 }
853}