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