mirror of
https://github.com/jmcorgan/fips.git
synced 2026-08-09 08:14:42 +00:00
Implement periodic full rekey at both protocol layers using fresh DH key exchanges. Uses the existing K-bit flag (FLAG_KEY_EPOCH / FSP_FLAG_K) to coordinate cutover between peers. FMP layer (IK pattern): - ActivePeer gains rekey state: pending/previous sessions, K-bit epoch tracking, drain window, dampening timer - Handshake state stored on ActivePeer with msg1 sent on existing link - Encrypted frame handler detects K-bit flips, promotes pending sessions, falls back to previous session during drain - Handshake handlers distinguish rekey from new connections using addr_to_link lookup with identity-based fallback - Free all session indices (current, rekey, pending, previous) on peer removal FSP layer (XK pattern): - SessionEntry gains parallel rekey fields with XK-specific state for the 3-message handshake - Route availability check before FSP rekey initiation - Encrypted session handler adds K-bit flip detection and dual-session decrypt fallback - SessionSetup/Ack/Msg3 handlers extended for rekey paths Defense-in-depth: - Consecutive decryption failure detector (threshold=20) triggers forced peer removal instead of waiting for link-dead timeout - Identity-based rekey detection as fallback when addr_to_link doesn't match (e.g., TCP ephemeral ports) Configuration: RekeyConfig with enabled flag, after_secs (default 120), and after_messages (default 65536) thresholds. Logging: info for successful K-bit cutover completions, warn for failures, debug for intermediate handshake steps, trace for routine operations (resends, drain cleanup). Rekey lifecycle: 1. Timer/counter fires -> initiator starts new handshake 2. Old session continues handling traffic during handshake 3. Handshake completes -> initiator cuts over, flips K-bit 4. Responder sees flipped K-bit -> promotes new session 5. Both keep old session for 10s drain window 6. After drain, old session discarded Integration test: Docker-based multi-phase test exercising both FMP and FSP rekey with aggressive timers (35s). Verifies connectivity across all 20 directed pairs survives two consecutive rekey cycles. Includes rekey topology, docker-compose profile, and CI matrix entry. Increase ping test convergence wait from 3s to 5s for CI reliability.
169 lines
6.1 KiB
Rust
169 lines
6.1 KiB
Rust
//! Link message dispatch and peer removal.
|
|
|
|
use crate::node::Node;
|
|
use crate::NodeAddr;
|
|
use tracing::{debug, info, trace};
|
|
|
|
impl Node {
|
|
/// Dispatch a decrypted link message to the appropriate handler.
|
|
///
|
|
/// Link messages are protocol messages exchanged between authenticated peers.
|
|
pub(in crate::node) async fn dispatch_link_message(&mut self, from: &NodeAddr, plaintext: &[u8], ce_flag: bool) {
|
|
if plaintext.is_empty() {
|
|
return;
|
|
}
|
|
|
|
let msg_type = plaintext[0];
|
|
let payload = &plaintext[1..];
|
|
|
|
match msg_type {
|
|
0x00 => {
|
|
// SessionDatagram
|
|
self.handle_session_datagram(from, payload, ce_flag).await;
|
|
}
|
|
0x01 => {
|
|
// SenderReport
|
|
self.handle_sender_report(from, payload);
|
|
}
|
|
0x02 => {
|
|
// ReceiverReport
|
|
self.handle_receiver_report(from, payload);
|
|
}
|
|
0x10 => {
|
|
// TreeAnnounce
|
|
self.handle_tree_announce(from, payload).await;
|
|
}
|
|
0x20 => {
|
|
// FilterAnnounce
|
|
self.handle_filter_announce(from, payload).await;
|
|
}
|
|
0x30 => {
|
|
// LookupRequest
|
|
self.handle_lookup_request(from, payload).await;
|
|
}
|
|
0x31 => {
|
|
// LookupResponse
|
|
self.handle_lookup_response(from, payload).await;
|
|
}
|
|
0x50 => {
|
|
// Disconnect
|
|
self.handle_disconnect(from, payload);
|
|
}
|
|
0x51 => {
|
|
// Heartbeat — no-op, last_recv_time already updated by record_recv()
|
|
trace!(peer = %self.peer_display_name(from), "Received heartbeat");
|
|
}
|
|
_ => {
|
|
debug!(msg_type = msg_type, "Unknown link message type");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Handle a Disconnect notification from a peer.
|
|
///
|
|
/// The peer is signaling an orderly departure. We immediately remove
|
|
/// them from all state rather than waiting for timeout detection.
|
|
fn handle_disconnect(&mut self, from: &NodeAddr, payload: &[u8]) {
|
|
let disconnect = match crate::protocol::Disconnect::decode(payload) {
|
|
Ok(msg) => msg,
|
|
Err(e) => {
|
|
debug!(from = %self.peer_display_name(from), error = %e, "Malformed disconnect message");
|
|
return;
|
|
}
|
|
};
|
|
|
|
info!(
|
|
peer = %self.peer_display_name(from),
|
|
reason = %disconnect.reason,
|
|
"Peer sent disconnect notification"
|
|
);
|
|
|
|
self.remove_active_peer(from);
|
|
}
|
|
|
|
/// Remove an active peer and clean up all associated state.
|
|
///
|
|
/// Frees session index, removes link and address mappings. Used for
|
|
/// both graceful disconnect and timeout-based eviction.
|
|
///
|
|
/// Also handles tree state cleanup: if the removed peer was our parent,
|
|
/// selects an alternative or becomes root, and marks remaining peers
|
|
/// for pending tree announce (delivered on next tick).
|
|
pub(in crate::node) fn remove_active_peer(&mut self, node_addr: &NodeAddr) {
|
|
let peer = match self.peers.remove(node_addr) {
|
|
Some(p) => p,
|
|
None => {
|
|
debug!(peer = %self.peer_display_name(node_addr), "Peer already removed");
|
|
return;
|
|
}
|
|
};
|
|
|
|
// Log suppressed replay detection summary before teardown
|
|
let suppressed = peer.replay_suppressed_count();
|
|
if suppressed > 0 {
|
|
debug!(
|
|
peer = %self.peer_display_name(node_addr),
|
|
count = suppressed,
|
|
"Suppressed replay detections during link transition"
|
|
);
|
|
}
|
|
|
|
// MMP teardown log (before we drop the peer)
|
|
if let Some(mmp) = peer.mmp() {
|
|
let name = self.peer_aliases.get(node_addr)
|
|
.cloned()
|
|
.unwrap_or_else(|| peer.identity().short_npub());
|
|
Self::log_mmp_teardown(&name, mmp);
|
|
}
|
|
|
|
let link_id = peer.link_id();
|
|
let transport_id = peer.transport_id();
|
|
|
|
// Free session indices (current, rekey, pending, previous)
|
|
if let Some(tid) = transport_id {
|
|
if let Some(idx) = peer.our_index() {
|
|
self.peers_by_index.remove(&(tid, idx.as_u32()));
|
|
let _ = self.index_allocator.free(idx);
|
|
}
|
|
if let Some(idx) = peer.rekey_our_index() {
|
|
self.pending_outbound.remove(&(tid, idx.as_u32()));
|
|
self.peers_by_index.remove(&(tid, idx.as_u32()));
|
|
let _ = self.index_allocator.free(idx);
|
|
}
|
|
if let Some(idx) = peer.pending_our_index() {
|
|
self.peers_by_index.remove(&(tid, idx.as_u32()));
|
|
let _ = self.index_allocator.free(idx);
|
|
}
|
|
if let Some(idx) = peer.previous_our_index() {
|
|
self.peers_by_index.remove(&(tid, idx.as_u32()));
|
|
let _ = self.index_allocator.free(idx);
|
|
}
|
|
}
|
|
|
|
// Remove link and address mapping
|
|
self.remove_link(&link_id);
|
|
|
|
// Tree state cleanup
|
|
let tree_changed = self.handle_peer_removal_tree_cleanup(node_addr);
|
|
if tree_changed {
|
|
// Mark all remaining peers for pending tree announce.
|
|
// These will be sent on the next tick via check_tree_state().
|
|
for peer in self.peers.values_mut() {
|
|
peer.mark_tree_announce_pending();
|
|
}
|
|
}
|
|
|
|
// Bloom filter cleanup: clear state for removed peer, mark all remaining peers
|
|
self.bloom_state.remove_peer_state(node_addr);
|
|
let remaining_peers: Vec<NodeAddr> = self.peers.keys().copied().collect();
|
|
self.bloom_state.mark_all_updates_needed(remaining_peers);
|
|
|
|
info!(
|
|
peer = %self.peer_display_name(node_addr),
|
|
link_id = %link_id,
|
|
tree_changed = tree_changed,
|
|
"Peer removed and state cleaned up"
|
|
);
|
|
}
|
|
}
|