fips_core/node/mod.rs
1//! FIPS Node Entity
2//!
3//! Top-level structure representing a running FIPS instance. The Node
4//! holds all state required for mesh routing: identity, tree state,
5//! Bloom filters, coordinate caches, transports, links, and peers.
6
7mod accessors_impl;
8mod acl;
9mod bloom;
10mod core_impl;
11mod decrypt_worker;
12mod discovery_rate_limit;
13mod encrypt_worker;
14mod endpoint_event;
15mod endpoint_traffic;
16mod error;
17mod handlers;
18mod identity_cache;
19mod io_impl;
20mod lifecycle;
21mod link_registry;
22mod peer_lifecycle;
23mod peer_runtime;
24mod rate_limit;
25mod recent_requests;
26mod retry;
27mod route_impl;
28mod routing;
29mod routing_error_rate_limit;
30mod send_impl;
31pub(crate) mod session;
32mod session_access_impl;
33mod session_registry;
34pub(crate) mod session_wire;
35mod state;
36pub(crate) mod stats;
37pub(crate) mod stats_history;
38mod support_state;
39#[cfg(test)]
40mod tests;
41mod tree;
42pub(crate) mod wire;
43
44pub use endpoint_event::ExternalPacketIo;
45pub use endpoint_traffic::{
46 EndpointPayloadClass, EndpointPayloadLane, classify_endpoint_payload,
47 endpoint_payload_is_latency_sensitive,
48};
49pub use error::NodeError;
50pub use identity_cache::NodeDeliveredPacket;
51pub use state::NodeState;
52
53pub(crate) use endpoint_event::EndpointBulkSendFeedback;
54#[cfg(test)]
55pub(in crate::node) use endpoint_event::EndpointEventDequeueCounts;
56pub(in crate::node) use endpoint_event::EndpointEventRuntime;
57#[cfg(test)]
58pub(in crate::node) use endpoint_event::release_endpoint_event_messages;
59#[cfg(unix)]
60pub(in crate::node) use endpoint_event::{
61 EndpointBulkSendFeedbackRecord, EndpointBulkSendSessionBookkeeping,
62};
63#[cfg(unix)]
64pub(crate) use endpoint_event::{
65 EndpointBulkSendFmpLease, EndpointBulkSendFspLease, EndpointBulkSendLease,
66 EndpointBulkSendRuntime,
67};
68pub(crate) use endpoint_event::{
69 EndpointDataDelivery, EndpointDataIo, EndpointEventReceiver, EndpointEventSender,
70 EndpointSendBatchCommand, EndpointSendCommand, NodeEndpointCommand, NodeEndpointEvent,
71 NodeEndpointPeer, NodeEndpointRelayStatus, UpdatePeersOutcome, endpoint_data_command_capacity,
72};
73#[cfg(unix)]
74pub(in crate::node) use endpoint_traffic::reserve_fmp_worker_send;
75pub(crate) use endpoint_traffic::{
76 EndpointCommandLane, EndpointDataPayload, EndpointDataSend, PendingSessionTrafficQueues,
77};
78#[cfg(test)]
79pub(crate) use endpoint_traffic::{PendingEndpointDataQueue, PendingTunPacketQueue};
80#[cfg(unix)]
81pub(in crate::node) use endpoint_traffic::{
82 classify_fmp_plaintext_traffic, endpoint_flow_dispatch_key,
83};
84#[cfg(test)]
85pub(in crate::node) use endpoint_traffic::{
86 endpoint_command_lane_for_payload, endpoint_payload_is_tcp,
87 fmp_plaintext_is_bulk_session_datagram,
88};
89pub(in crate::node) use identity_cache::IdentityCache;
90#[cfg(test)]
91pub(in crate::node) use link_registry::LinkAddressIndex;
92pub(in crate::node) use link_registry::{LinkRegistry, PendingConnect, TransportDropTracker};
93pub(in crate::node) use peer_lifecycle::*;
94pub(in crate::node) use peer_runtime::*;
95#[cfg(test)]
96pub(crate) use recent_requests::RecentRequest;
97pub(crate) use recent_requests::{RecentDiscoveryRequests, RecentResponseForward};
98pub(in crate::node) use session_registry::*;
99pub(in crate::node) use support_state::{
100 BootstrapTransports, DiscoveryFallbackTransit, LocalSendFailures, SessionDirectDegradation,
101};
102
103use self::decrypt_worker::DecryptSessionKey;
104use self::discovery_rate_limit::{DiscoveryBackoff, DiscoveryForwardRateLimiter};
105use self::rate_limit::HandshakeRateLimiter;
106use self::routing::{LearnedRouteTable, LearnedRouteTableSnapshot};
107use self::routing_error_rate_limit::RoutingErrorRateLimiter;
108#[cfg(unix)]
109use self::wire::ESTABLISHED_HEADER_SIZE;
110use self::wire::{
111 FLAG_CE, FLAG_KEY_EPOCH, FLAG_SP, build_encrypted, build_established_header,
112 prepend_inner_header,
113};
114use crate::bloom::{BloomFilter, BloomState};
115use crate::cache::CoordCache;
116use crate::config::{NostrDiscoveryPolicy, PeerConfig, RoutingMode};
117#[cfg(unix)]
118use crate::node::session::FspSendReservation;
119use crate::node::session::SessionEntry;
120use crate::node::session_wire::{FSP_PHASE_ESTABLISHED, FspCommonPrefix};
121use crate::peer::{ActivePeer, PeerConnection};
122#[cfg(any(target_os = "linux", target_os = "macos"))]
123use crate::transport::ethernet::EthernetTransport;
124use crate::transport::tcp::TcpTransport;
125use crate::transport::tor::TorTransport;
126use crate::transport::udp::UdpTransport;
127#[cfg(feature = "webrtc-transport")]
128use crate::transport::webrtc::WebRtcTransport;
129use crate::transport::{
130 ConnectionState, Link, LinkId, PacketRx, PacketTx, TransportAddr, TransportError,
131 TransportHandle, TransportId,
132};
133use crate::tree::TreeState;
134use crate::upper::hosts::HostMap;
135use crate::upper::icmp_rate_limit::IcmpRateLimiter;
136use crate::upper::tun::{TunError, TunOutboundRx, TunState, TunTx};
137use crate::utils::index::{IndexAllocator, SessionIndex};
138use crate::{
139 Config, ConfigError, FipsAddress, Identity, IdentityError, LinkMessageType, NodeAddr,
140 PeerIdentity, encode_npub,
141};
142use rand::Rng;
143use std::collections::{HashMap, HashSet, VecDeque};
144use std::fmt;
145use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
146use std::sync::{Arc, Condvar, Mutex as StdMutex};
147use std::thread::JoinHandle;
148use thiserror::Error;
149use tracing::{debug, warn};
150
151const LOCAL_SEND_FAILURE_FAST_DEAD_WINDOW: std::time::Duration = std::time::Duration::from_secs(3);
152pub(crate) const ENDPOINT_EVENT_PRIORITY_MAX_LEN: usize = 512;
153const SESSION_DIRECT_DEGRADED_HOLD_MS: u64 = 20_000;
154const SESSION_DIRECT_DEGRADED_MIN_SAMPLE: u64 = 16;
155const SESSION_DIRECT_DEGRADED_LOSS_THRESHOLD: f64 = 0.08;
156const SESSION_DIRECT_RECOVERY_LOSS_THRESHOLD: f64 = 0.02;
157const SESSION_DIRECT_MIN_EXCLUSIVE_TRUST_MS: u64 = 6_500;
158const ROUTING_FALLBACK_MIN_COST_ADVANTAGE: f64 = 0.25;
159const ENDPOINT_EVENT_BACKLOG_HIGH_WATER: usize = 4096;
160
161/// Half-range of the symmetric jitter applied to per-session rekey timers.
162///
163/// Each FMP/FSP session draws an offset uniformly from
164/// `[-REKEY_JITTER_SECS, +REKEY_JITTER_SECS]` seconds at construction and
165/// after each cutover. This preserves the configured mean interval while
166/// reducing dual-initiation bursts in symmetric-start meshes.
167pub(crate) const REKEY_JITTER_SECS: i64 = 15;
168
169/// A running FIPS node instance.
170///
171/// This is the top-level container holding all node state.
172///
173/// ## Peer Lifecycle
174///
175/// Peers go through two phases:
176/// 1. **Connection phase** (`connections`): Handshake in progress, indexed by LinkId
177/// 2. **Active phase** (`peers`): Authenticated, indexed by NodeAddr
178///
179/// The link registry dispatches incoming packets to the right connection before
180/// authentication completes.
181// Discovery lookup constants moved to config: node.discovery.attempt_timeouts_secs, node.discovery.ttl
182pub struct Node {
183 // === Identity ===
184 /// This node's cryptographic identity.
185 identity: Identity,
186
187 /// Random epoch generated at startup for peer restart detection.
188 /// Exchanged inside Noise handshake messages so peers can detect restarts.
189 startup_epoch: [u8; 8],
190
191 /// Instant when the node was created, for uptime reporting.
192 started_at: std::time::Instant,
193
194 // === Configuration ===
195 /// Loaded configuration.
196 config: Config,
197
198 // === State ===
199 /// Node operational state.
200 state: NodeState,
201
202 /// Whether this is a leaf-only node.
203 is_leaf_only: bool,
204
205 // === Spanning Tree ===
206 /// Local spanning tree state.
207 tree_state: TreeState,
208
209 // === Bloom Filter ===
210 /// Local Bloom filter state.
211 bloom_state: BloomState,
212
213 // === Routing ===
214 /// Address -> coordinates cache (from session setup and discovery).
215 coord_cache: CoordCache,
216 /// Locally learned reverse-path next-hop hints.
217 learned_routes: LearnedRouteTable,
218 /// Destinations whose direct first-hop path is temporarily suspect because
219 /// session-layer MMP observed sustained loss while using that direct path.
220 session_direct_degradation: SessionDirectDegradation,
221 /// Recent discovery requests for dedup and reverse-path forwarding.
222 recent_requests: RecentDiscoveryRequests,
223 /// Per-destination path MTU lookup, keyed by FipsAddress (mirrors
224 /// `coord_cache.entries[*].path_mtu`). Sync read-only access from
225 /// the TUN reader/writer threads at TCP MSS clamp time so the
226 /// SYN/SYN-ACK clamp can use the smaller of the local-egress floor
227 /// and the learned per-destination path MTU.
228 path_mtu_lookup: Arc<std::sync::RwLock<HashMap<crate::FipsAddress, u16>>>,
229
230 // === Transports & Links ===
231 /// Active transports (owned by Node).
232 transports: HashMap<TransportId, TransportHandle>,
233 /// Per-transport kernel drop tracking for congestion detection.
234 transport_drops: TransportDropTracker,
235 /// Per-transport wildcard socket-local drop tracking for observability.
236 transport_socket_drops: TransportDropTracker,
237 /// Per-transport Linux namespace receive-buffer error tracking for observability.
238 transport_namespace_drops: TransportDropTracker,
239 /// Active links plus reverse address dispatch index.
240 links: LinkRegistry,
241
242 // === Packet Channel ===
243 /// Packet sender for transports.
244 packet_tx: Option<PacketTx>,
245 /// Packet receiver (for event loop).
246 packet_rx: Option<PacketRx>,
247
248 // === Peer Lifecycle ===
249 /// Pending handshake connections plus authenticated peers.
250 peers: PeerLifecycleRegistry,
251
252 // === End-to-End Sessions ===
253 /// Session table for end-to-end encrypted sessions.
254 /// Keyed by remote NodeAddr.
255 sessions: SessionRegistry,
256
257 // === Identity Cache ===
258 /// Maps FipsAddress prefix bytes (bytes 1-15) to cached peer identity data.
259 /// Enables reverse lookup from IPv6 destination to session/routing identity.
260 identity_cache: IdentityCache,
261
262 // === Pending TUN Packets ===
263 /// TUN packets and endpoint payloads queued while waiting for session establishment.
264 pending_session_traffic: PendingSessionTrafficQueues,
265 // === Pending Discovery Lookups ===
266 /// Tracks in-flight discovery lookups and owns dedupe/cap admission.
267 pending_lookups: handlers::discovery::PendingDiscoveryLookups,
268
269 // === Resource Limits ===
270 /// Maximum connections (0 = unlimited).
271 max_connections: usize,
272 /// Maximum peers (0 = unlimited).
273 max_peers: usize,
274 /// Maximum links (0 = unlimited).
275 max_links: usize,
276
277 // === Counters ===
278 /// Next link ID to allocate.
279 next_link_id: u64,
280 /// Next transport ID to allocate.
281 next_transport_id: u32,
282
283 // === Node Statistics ===
284 /// Routing, forwarding, discovery, and error signal counters.
285 stats: stats::NodeStats,
286
287 /// Time-series history of node-level metrics (1s/1m rings).
288 stats_history: stats_history::StatsHistory,
289
290 // === TUN Interface ===
291 /// TUN device state.
292 tun_state: TunState,
293 /// TUN interface name (for cleanup).
294 tun_name: Option<String>,
295 /// TUN packet sender channel.
296 tun_tx: Option<TunTx>,
297 /// Receiver for outbound packets from the TUN reader.
298 tun_outbound_rx: Option<TunOutboundRx>,
299 /// App-owned packet sink used by embedded/no-TUN integrations.
300 external_packet_tx: Option<tokio::sync::mpsc::Sender<NodeDeliveredPacket>>,
301 /// Endpoint data command receiver used by embedded/no-daemon integrations.
302 endpoint_priority_command_rx: Option<tokio::sync::mpsc::Receiver<NodeEndpointCommand>>,
303 /// Bulk endpoint data command receiver used by embedded/no-daemon integrations.
304 endpoint_command_rx: Option<tokio::sync::mpsc::Receiver<NodeEndpointCommand>>,
305 /// Endpoint data event delivery runtime used by embedded/no-daemon integrations.
306 endpoint_events: EndpointEventRuntime,
307 /// Priority feedback from endpoint-side bulk-send leases. The endpoint
308 /// mover must report FMP/FSP send bookkeeping before it dispatches worker
309 /// jobs; rx_loop applies this lane ahead of bulk endpoint commands so
310 /// MMP/liveness/accounting do not starve behind bulk traffic.
311 endpoint_bulk_feedback_rx: Option<tokio::sync::mpsc::Receiver<EndpointBulkSendFeedback>>,
312 /// Shared lease publisher for endpoint-side bulk sends.
313 #[cfg(unix)]
314 endpoint_bulk_send_runtime: Option<EndpointBulkSendRuntime>,
315 /// Off-task FMP-encrypt + UDP-send worker pool. `None` if not yet
316 /// spawned (set up in `start()` once transports are running).
317 /// `Some(pool)` once available; the pool internally holds
318 /// per-worker mpsc senders and round-robins jobs across them.
319 /// See `node::encrypt_worker` for the rationale and layout.
320 encrypt_workers: Option<encrypt_worker::EncryptWorkerPool>,
321 /// Off-task FMP + FSP decrypt + delivery worker pool. Mirror of
322 /// `encrypt_workers` for the receive side.
323 decrypt_workers: Option<decrypt_worker::DecryptWorkerPool>,
324 /// Decrypt-worker return channel. Compact authenticated receive metadata,
325 /// direct local FSP completions, and fallback plaintext all return here so
326 /// rx_loop can apply node-owned bookkeeping and any remaining legacy link
327 /// dispatch. Drained with a bounded priority lane ahead of bounded
328 /// authenticated and fallback bulk lanes.
329 decrypt_fallback_rx: Option<decrypt_worker::DecryptWorkerFallbackReceivers>,
330 decrypt_fallback_tx: decrypt_worker::DecryptWorkerFallbackSender,
331 /// TUN reader thread handle.
332 tun_reader_handle: Option<JoinHandle<()>>,
333 /// TUN writer thread handle.
334 tun_writer_handle: Option<JoinHandle<()>>,
335 /// Shutdown pipe: writing to this fd unblocks the TUN reader thread on macOS.
336 /// On Linux, deleting the interface via netlink serves the same purpose.
337 #[cfg(target_os = "macos")]
338 tun_shutdown_fd: Option<std::os::unix::io::RawFd>,
339
340 // === DNS Responder ===
341 /// Receiver for resolved identities from the DNS responder.
342 dns_identity_rx: Option<crate::upper::dns::DnsIdentityRx>,
343 /// DNS responder task handle.
344 dns_task: Option<tokio::task::JoinHandle<()>>,
345
346 // === Index-Based Session Dispatch ===
347 /// Allocator for session indices.
348 index_allocator: IndexAllocator,
349 /// Pending outbound handshakes by our sender_idx.
350 /// Tracks which LinkId corresponds to which session index.
351 pending_outbound: PendingOutboundHandshakes,
352
353 // === Rate Limiting ===
354 /// Rate limiter for msg1 processing (DoS protection).
355 msg1_rate_limiter: HandshakeRateLimiter,
356 /// Rate limiter for ICMP Packet Too Big messages.
357 icmp_rate_limiter: IcmpRateLimiter,
358 /// Rate limiter for routing error signals (CoordsRequired / PathBroken).
359 routing_error_rate_limiter: RoutingErrorRateLimiter,
360 /// Rate limiter for source-side CoordsRequired/PathBroken responses.
361 coords_response_rate_limiter: RoutingErrorRateLimiter,
362 /// Backoff for failed discovery lookups (originator-side).
363 discovery_backoff: DiscoveryBackoff,
364 /// Rate limiter for forwarded discovery requests (transit-side).
365 discovery_forward_limiter: DiscoveryForwardRateLimiter,
366
367 // === Pending Transport Connects ===
368 /// Links waiting for transport-level connection establishment before
369 /// sending handshake msg1. For connection-oriented transports (TCP, Tor),
370 /// the transport connect runs in the background; the tick handler polls
371 /// connection_state() and initiates the handshake when connected.
372 pending_connects: Vec<PendingConnect>,
373
374 // === Connection Retry ===
375 /// Retry state for peers whose outbound connections have failed.
376 /// Keyed by NodeAddr. Entries are created when a handshake times out
377 /// or fails, and removed on successful promotion or when max retries
378 /// are exhausted.
379 retry_pending: retry::PendingRouteRetries,
380
381 /// Optional Nostr/STUN overlay discovery coordinator for `udp:nat` peers.
382 nostr_discovery: Option<Arc<crate::discovery::nostr::NostrDiscovery>>,
383 /// mDNS / DNS-SD responder + browser for local-link peer discovery.
384 /// Identity is unverified at this layer — the Noise XX handshake
385 /// initiated against an mDNS-observed endpoint is what proves the
386 /// peer holds the matching private key.
387 lan_discovery: Option<Arc<crate::discovery::lan::LanDiscovery>>,
388 /// Same-host JSON registry under `~/.fips/instances`. Records are
389 /// loopback routing hints only; peer identity is still verified by the
390 /// Noise handshake.
391 local_instance_registry: Option<crate::discovery::local::LocalInstanceRegistry>,
392 local_instance_started_at_ms: Option<u64>,
393 last_local_instance_publish_ms: Option<u64>,
394 last_local_instance_scan_ms: Option<u64>,
395 /// Wall-clock ms when Nostr discovery successfully started, used to
396 /// schedule the one-shot startup advert sweep after a settle delay.
397 /// `None` until discovery comes up; remains `None` if discovery is
398 /// disabled or failed to start.
399 nostr_discovery_started_at_ms: Option<u64>,
400 /// Whether the one-shot startup advert sweep has run. Set to true
401 /// after the first sweep fires (under `policy: open`); thereafter
402 /// only the per-tick `queue_open_discovery_retries` continues.
403 startup_open_discovery_sweep_done: bool,
404 /// Per-peer UDP transports adopted from NAT traversal handoff plus the
405 /// originating peer npub for protocol-mismatch cooldown bookkeeping.
406 bootstrap_transports: BootstrapTransports,
407 /// Peers that should not be used as reply-learned fallback transit for
408 /// other destinations. Direct lookups to the peer are still permitted.
409 discovery_fallback_transit: DiscoveryFallbackTransit,
410
411 // === Periodic Parent Re-evaluation ===
412 /// Timestamp of last periodic parent re-evaluation (for pacing).
413 last_parent_reeval: Option<crate::time::Instant>,
414
415 // === Congestion Logging ===
416 /// Timestamp of last congestion detection log (rate-limited to 5s).
417 last_congestion_log: Option<std::time::Instant>,
418
419 // === Mesh Size Estimate ===
420 /// Cached estimated mesh size (computed once per tick from bloom filters).
421 estimated_mesh_size: Option<u64>,
422 /// Timestamp of last mesh size log emission.
423 last_mesh_size_log: Option<std::time::Instant>,
424
425 // === Bloom Self-Plausibility ===
426 /// Rate-limit state for the self-plausibility WARN. Fires at most
427 /// once per 60s globally when our own outgoing FilterAnnounce has
428 /// an FPR above `node.bloom.max_inbound_fpr`, signalling either
429 /// aggregation drift or an ingress bypass.
430 last_self_warn: Option<std::time::Instant>,
431
432 // === Local Outbound Liveness ===
433 /// Set per peer when a `transport.send` returned a local-side io error
434 /// (`NetworkUnreachable` / `HostUnreachable` / `AddrNotAvailable`),
435 /// cleared on the next successful send to that peer. Used by
436 /// `check_link_heartbeats` to compress only that peer's dead-timeout to
437 /// `fast_link_dead_timeout_secs` while its outbound is observed broken.
438 local_send_failures: LocalSendFailures,
439 /// Set when the rx loop could not complete its 1s maintenance work
440 /// inside the watchdog timeout. Link-dead detection may be valid during
441 /// overload, but traversal cooldown should not punish a path just because
442 /// our own scheduler/worker queue was late.
443 last_rx_loop_maintenance_timeout_at: Option<std::time::Instant>,
444
445 // === Display Names ===
446 /// Human-readable names for configured peers (alias or short npub).
447 /// Populated at startup from peer config.
448 peer_aliases: HashMap<NodeAddr, String>,
449 /// Scheduler weight for explicitly configured peers. Built when config
450 /// changes so the packet hot path only does a NodeAddr hash lookup.
451 configured_peer_send_weights: ConfiguredPeerSendWeights,
452
453 /// Reloadable peer ACL state from standard allow/deny files.
454 peer_acl: acl::PeerAclReloader,
455
456 // === Host Map ===
457 /// Static hostname → npub mapping for DNS resolution.
458 /// Built at construction from peer aliases and /etc/fips/hosts.
459 host_map: Arc<HostMap>,
460}
461
462impl fmt::Debug for Node {
463 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
464 f.debug_struct("Node")
465 .field("node_addr", self.node_addr())
466 .field("state", &self.state)
467 .field("is_leaf_only", &self.is_leaf_only)
468 .field("connections", &self.connection_count())
469 .field("peers", &self.peer_count())
470 .field("links", &self.link_count())
471 .field("transports", &self.transport_count())
472 .finish()
473 }
474}